|
| 1 | +import { agentOsRequest, readAgentOsErrorMessage } from '@/composables/request' |
| 2 | +import { APIRoutes } from './routes' |
| 3 | + |
| 4 | +export interface UserMemory { |
| 5 | + memory_id: string |
| 6 | + memory: string |
| 7 | + topics?: string[] | null |
| 8 | + agent_id?: string | null |
| 9 | + team_id?: string | null |
| 10 | + user_id?: string | null |
| 11 | + updated_at?: string | null |
| 12 | +} |
| 13 | + |
| 14 | +export interface UserMemoryCreate { |
| 15 | + memory: string |
| 16 | + user_id?: string | null |
| 17 | + topics?: string[] | null |
| 18 | +} |
| 19 | + |
| 20 | +export interface UserStats { |
| 21 | + user_id: string |
| 22 | + total_memories: number |
| 23 | + last_memory_updated_at?: string | null |
| 24 | +} |
| 25 | + |
| 26 | +export interface PaginationInfo { |
| 27 | + page: number |
| 28 | + limit: number |
| 29 | + total_pages: number |
| 30 | + total_count: number |
| 31 | + search_time_ms: number |
| 32 | +} |
| 33 | + |
| 34 | +export interface PaginatedResponse<T> { |
| 35 | + data: T[] |
| 36 | + meta: PaginationInfo |
| 37 | +} |
| 38 | + |
| 39 | +export type ApiResult<T> = |
| 40 | + | { ok: true; data: T } |
| 41 | + | { ok: false; status: number; message: string } |
| 42 | + |
| 43 | +export type ApiVoidResult = |
| 44 | + | { ok: true } |
| 45 | + | { ok: false; status: number; message: string } |
| 46 | + |
| 47 | +export async function getUserMemoryStatsAPI( |
| 48 | + base: string, |
| 49 | + authToken: string | undefined, |
| 50 | + params: { limit?: number; page?: number; user_id?: string } = {}, |
| 51 | +): Promise<ApiResult<PaginatedResponse<UserStats>>> { |
| 52 | + const sp = new URLSearchParams() |
| 53 | + if (params.limit != null) sp.set('limit', String(params.limit)) |
| 54 | + if (params.page != null) sp.set('page', String(params.page)) |
| 55 | + if (params.user_id?.trim()) sp.set('user_id', params.user_id.trim()) |
| 56 | + const q = sp.toString() |
| 57 | + const path = APIRoutes.GetUserMemoryStats(base) |
| 58 | + const href = q ? `${path}?${q}` : path |
| 59 | + try { |
| 60 | + const res = await agentOsRequest(href, { method: 'GET', authToken }) |
| 61 | + if (!res.ok) { |
| 62 | + const message = await readAgentOsErrorMessage(res) |
| 63 | + return { ok: false, status: res.status, message } |
| 64 | + } |
| 65 | + const data = (await res.json()) as PaginatedResponse<UserStats> |
| 66 | + return { ok: true, data } |
| 67 | + } catch (e) { |
| 68 | + return { ok: false, status: 0, message: e instanceof Error ? e.message : 'Network error' } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export async function listMemoriesAPI( |
| 73 | + base: string, |
| 74 | + authToken: string | undefined, |
| 75 | + params: { |
| 76 | + user_id?: string |
| 77 | + limit?: number |
| 78 | + page?: number |
| 79 | + sort_by?: string |
| 80 | + sort_order?: 'asc' | 'desc' |
| 81 | + } = {}, |
| 82 | +): Promise<ApiResult<PaginatedResponse<UserMemory>>> { |
| 83 | + const sp = new URLSearchParams() |
| 84 | + if (params.user_id?.trim()) sp.set('user_id', params.user_id.trim()) |
| 85 | + if (params.limit != null) sp.set('limit', String(params.limit)) |
| 86 | + if (params.page != null) sp.set('page', String(params.page)) |
| 87 | + if (params.sort_by) sp.set('sort_by', params.sort_by) |
| 88 | + if (params.sort_order) sp.set('sort_order', params.sort_order) |
| 89 | + const q = sp.toString() |
| 90 | + const path = APIRoutes.ListMemories(base) |
| 91 | + const href = q ? `${path}?${q}` : path |
| 92 | + try { |
| 93 | + const res = await agentOsRequest(href, { method: 'GET', authToken }) |
| 94 | + if (!res.ok) { |
| 95 | + const message = await readAgentOsErrorMessage(res) |
| 96 | + return { ok: false, status: res.status, message } |
| 97 | + } |
| 98 | + const data = (await res.json()) as PaginatedResponse<UserMemory> |
| 99 | + return { ok: true, data } |
| 100 | + } catch (e) { |
| 101 | + return { ok: false, status: 0, message: e instanceof Error ? e.message : 'Network error' } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export async function createMemoryAPI( |
| 106 | + base: string, |
| 107 | + authToken: string | undefined, |
| 108 | + body: UserMemoryCreate, |
| 109 | +): Promise<ApiResult<UserMemory>> { |
| 110 | + try { |
| 111 | + const res = await agentOsRequest(APIRoutes.CreateMemory(base), { |
| 112 | + method: 'POST', |
| 113 | + authToken, |
| 114 | + body: JSON.stringify(body), |
| 115 | + }) |
| 116 | + if (!res.ok) { |
| 117 | + const message = await readAgentOsErrorMessage(res) |
| 118 | + return { ok: false, status: res.status, message } |
| 119 | + } |
| 120 | + const data = (await res.json()) as UserMemory |
| 121 | + return { ok: true, data } |
| 122 | + } catch (e) { |
| 123 | + return { ok: false, status: 0, message: e instanceof Error ? e.message : 'Network error' } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +export async function updateMemoryAPI( |
| 128 | + base: string, |
| 129 | + authToken: string | undefined, |
| 130 | + memoryId: string, |
| 131 | + body: UserMemoryCreate, |
| 132 | +): Promise<ApiResult<UserMemory>> { |
| 133 | + try { |
| 134 | + const res = await agentOsRequest(APIRoutes.UpdateMemory(base, memoryId), { |
| 135 | + method: 'PATCH', |
| 136 | + authToken, |
| 137 | + body: JSON.stringify(body), |
| 138 | + }) |
| 139 | + if (!res.ok) { |
| 140 | + const message = await readAgentOsErrorMessage(res) |
| 141 | + return { ok: false, status: res.status, message } |
| 142 | + } |
| 143 | + const data = (await res.json()) as UserMemory |
| 144 | + return { ok: true, data } |
| 145 | + } catch (e) { |
| 146 | + return { ok: false, status: 0, message: e instanceof Error ? e.message : 'Network error' } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +export async function deleteMemoriesBatchAPI( |
| 151 | + base: string, |
| 152 | + authToken: string | undefined, |
| 153 | + memoryIds: string[], |
| 154 | + userId?: string, |
| 155 | +): Promise<ApiVoidResult> { |
| 156 | + try { |
| 157 | + const res = await agentOsRequest(APIRoutes.DeleteMemories(base), { |
| 158 | + method: 'DELETE', |
| 159 | + authToken, |
| 160 | + body: JSON.stringify({ |
| 161 | + memory_ids: memoryIds, |
| 162 | + ...(userId?.trim() ? { user_id: userId.trim() } : {}), |
| 163 | + }), |
| 164 | + }) |
| 165 | + if (!res.ok) { |
| 166 | + const message = await readAgentOsErrorMessage(res) |
| 167 | + return { ok: false, status: res.status, message } |
| 168 | + } |
| 169 | + return { ok: true } |
| 170 | + } catch (e) { |
| 171 | + return { ok: false, status: 0, message: e instanceof Error ? e.message : 'Network error' } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +export async function deleteMemoryAPI( |
| 176 | + base: string, |
| 177 | + authToken: string | undefined, |
| 178 | + memoryId: string, |
| 179 | + userId?: string, |
| 180 | +): Promise<ApiVoidResult> { |
| 181 | + const sp = new URLSearchParams() |
| 182 | + if (userId?.trim()) sp.set('user_id', userId.trim()) |
| 183 | + const q = sp.toString() |
| 184 | + const path = APIRoutes.DeleteMemory(base, memoryId) |
| 185 | + const href = q ? `${path}?${q}` : path |
| 186 | + try { |
| 187 | + const res = await agentOsRequest(href, { method: 'DELETE', authToken }) |
| 188 | + if (!res.ok) { |
| 189 | + const message = await readAgentOsErrorMessage(res) |
| 190 | + return { ok: false, status: res.status, message } |
| 191 | + } |
| 192 | + return { ok: true } |
| 193 | + } catch (e) { |
| 194 | + return { ok: false, status: 0, message: e instanceof Error ? e.message : 'Network error' } |
| 195 | + } |
| 196 | +} |
0 commit comments