| 11 | * 管理三层记忆系统的所有操作 |
| 12 | */ |
| 13 | export class MemoryResource extends BaseResource { |
| 14 | constructor(options: ClientOptions) { |
| 15 | super(options); |
| 16 | } |
| 17 | |
| 18 | // ========================================================================== |
| 19 | // Working Memory API |
| 20 | // ========================================================================== |
| 21 | |
| 22 | /** |
| 23 | * Working Memory 操作 |
| 24 | * LLM 可主动更新的工作记忆,支持 Thread/Resource 双作用域 |
| 25 | */ |
| 26 | working = { |
| 27 | /** |
| 28 | * 获取 Working Memory 值 |
| 29 | * @param key 键名 |
| 30 | * @param scope 作用域(默认: thread) |
| 31 | * @returns 值 |
| 32 | */ |
| 33 | get: async (key: string, scope?: WorkingMemoryScope): Promise<any> => { |
| 34 | const params: any = { key }; |
| 35 | if (scope) params.scope = scope; |
| 36 | const result = await this.request<{ value: any }>("/v1/memory/working", { |
| 37 | params, |
| 38 | }); |
| 39 | return result.value; |
| 40 | }, |
| 41 | |
| 42 | /** |
| 43 | * 设置 Working Memory 值 |
| 44 | * @param key 键名 |
| 45 | * @param value 值 |
| 46 | * @param options 选项(作用域、TTL、Schema) |
| 47 | */ |
| 48 | set: async (key: string, value: any, options?: WorkingMemorySetOptions): Promise<void> => { |
| 49 | await this.request("/v1/memory/working", { |
| 50 | method: "POST", |
| 51 | body: { |
| 52 | key, |
| 53 | value, |
| 54 | scope: options?.scope ?? "thread", |
| 55 | ttl: options?.ttl, |
| 56 | schema: options?.schema, |
| 57 | }, |
| 58 | }); |
| 59 | }, |
| 60 | |
| 61 | /** |
| 62 | * 删除 Working Memory 值 |
| 63 | * @param key 键名 |
| 64 | * @param scope 作用域(默认: thread) |
| 65 | */ |
| 66 | delete: async (key: string, scope?: WorkingMemoryScope): Promise<void> => { |
| 67 | const params = scope ? { scope } : undefined; |
| 68 | await this.request(`/v1/memory/working/${key}`, { |
| 69 | method: "DELETE", |
| 70 | params, |