| 31 | const rootIndexCache = new Map<string, { content: string; expiresAt: number }>(); |
| 32 | |
| 33 | async function getRootIndexContent(workspace: string | undefined): Promise<string> { |
| 34 | const key = workspace ?? "__default__"; |
| 35 | const now = Date.now(); |
| 36 | const cached = rootIndexCache.get(key); |
| 37 | if (cached && cached.expiresAt > now) return cached.content; |
| 38 | try { |
| 39 | // executeTool 从 workspaceContext 读 ws(本函数在 run(ws) 作用域内被调) |
| 40 | const r = await executeTool("read_page", { path: "wiki/root_index.md" }); |
| 41 | if (!r.ok || !r.data) return ""; |
| 42 | const d = r.data as { content?: string }; |
| 43 | const content = d.content || ""; |
| 44 | rootIndexCache.set(key, { content, expiresAt: now + ROOT_INDEX_TTL_MS }); |
| 45 | return content; |
| 46 | } catch { |
| 47 | return ""; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** 在 system prompt 末尾 append root_index 全文,让 AI 不需要花一轮调 read_page 读它 */ |
| 52 | function appendPrewarmedIndex(baseSystem: string, indexContent: string): string { |