// dashboard/src/lib/server/dream-kb-sync.ts // // 会话内「沉淀到知识库」:把一个 dream 抽取任务(skill/profile)的产出,按会话 // 合并成单个文档写入知识库;同会话重复提取时用确定性文件名整体覆盖旧文件。 // 不依赖 KB 的 subspace / metadata schema —— 任意知识库均可作为目标。 import type { KnowledgeBaseClient, MemoryClient, } from "@tablestore/tablestore-agent-sdk"; import type { DreamDepositResult, DreamExtractSpec, DreamKind, DreamScope, } from "@/lib/dream-kb/types"; import { buildScopeKey } from "@/lib/dream-kb/scope"; import { buildSessionDreamDocFileName } from "@/lib/dream-kb/filename"; import { buildSessionDreamDoc } from "@/lib/dream-kb/format"; const KIND_TO_ACTION: Record = { skill: "EMIT_SKILL", profile: "EMIT_PROFILE", }; /** 拉取某个 dream 任务本身产出的、已生效的 EMIT_{kind} 抽取内容 + 其目标 scope。 */ async function fetchTaskExtracts( memory: MemoryClient, memoryStoreName: string, dreamId: string, kind: DreamKind, ): Promise<{ specs: DreamExtractSpec[]; scope: DreamScope | null }> { const specs: DreamExtractSpec[] = []; let scope: DreamScope | null = null; let nextToken: string | undefined; do { const req: Record = { memoryStoreName, dreamId, action: KIND_TO_ACTION[kind], status: "applied", // 只取已生效的产出 limit: 100, }; if (nextToken) req.nextToken = nextToken; const resp = await memory.listMemoryDreamActions(req); if (resp.code !== "SUCCESS") { throw new Error( resp.message ?? `listMemoryDreamActions failed (${resp.code})`, ); } const data = (resp.data ?? {}) as { actions?: Array>; nextToken?: string; }; for (const row of data.actions ?? []) { const ex = row.newExtract as | { kind?: string; name?: string; description?: string; instructions?: string; } | undefined; if (!ex || !ex.instructions) continue; specs.push({ kind, name: ex.name ?? (kind === "profile" ? "用户画像" : "未命名技能"), description: ex.description ?? "", instructions: ex.instructions, }); if (!scope) { const ts = row.targetScope as Partial | undefined; if (ts) { scope = { appId: ts.appId ?? "", tenantId: ts.tenantId ?? "", agentId: ts.agentId ?? "", runId: ts.runId ?? "", }; } } } nextToken = typeof data.nextToken === "string" && data.nextToken ? data.nextToken : undefined; } while (nextToken); return { specs, scope }; } /** * 把指定 dream 任务的产出(按 kinds)落到知识库。每种类型一个会话文件,overwrite。 * 逐类型独立 try/catch:一类失败不影响另一类。 */ export async function depositDreamTaskToKb(args: { memory: MemoryClient; kb: KnowledgeBaseClient; memoryStoreName: string; dreamId: string; knowledgeBaseName: string; kinds: DreamKind[]; }): Promise { const result: DreamDepositResult = { deposited: [], failures: [] }; for (const kind of args.kinds) { try { const { specs, scope } = await fetchTaskExtracts( args.memory, args.memoryStoreName, args.dreamId, kind, ); if (specs.length === 0 || !scope) continue; // 本次没提取到该类型内容 const scopeKey = buildScopeKey(scope); const { content } = buildSessionDreamDoc({ kind, specs, dreamId: args.dreamId, scopeKey, }); const fileName = buildSessionDreamDocFileName(kind, scopeKey); const resp = await args.kb.uploadContentAndRegister({ knowledgeBaseName: args.knowledgeBaseName, fileName, content, overwrite: true, // 同会话重复提取时覆盖旧文件 }); if (resp.code !== "SUCCESS") { throw new Error(resp.message ?? `upload failed (${resp.code})`); } result.deposited.push({ kind, fileName, count: specs.length }); } catch (err) { result.failures.push({ kind, error: err instanceof Error ? err.message : String(err), }); } } return result; }