从 tool result 提取路径加入注册表
(toolName: string, args: Record<string, unknown>, data?: unknown)
| 86 | |
| 87 | /** 从 tool result 提取路径加入注册表 */ |
| 88 | function recordSource(toolName: string, args: Record<string, unknown>, data?: unknown) { |
| 89 | const p = |
| 90 | typeof args.path === "string" |
| 91 | ? args.path |
| 92 | : typeof args.file_path === "string" |
| 93 | ? args.file_path |
| 94 | : null; |
| 95 | if (p) availableSources.add(p); |
| 96 | // backlinks / outlinks 的结果里也包含有效路径 |
| 97 | if ((toolName === "backlinks" || toolName === "outlinks") && data) { |
| 98 | const field = toolName === "backlinks" ? "from_path" : "target"; |
| 99 | let arr: unknown = data; |
| 100 | if (!Array.isArray(arr) && typeof arr === "object") { |
| 101 | const obj = arr as Record<string, unknown>; |
| 102 | if (Array.isArray(obj.hits)) arr = obj.hits; |
| 103 | else if (Array.isArray(obj.results)) arr = obj.results; |
| 104 | } |
| 105 | if (Array.isArray(arr)) { |
| 106 | for (const item of arr) { |
| 107 | if (item && typeof item === "object") { |
| 108 | const path = (item as Record<string, unknown>)[field]; |
| 109 | if (typeof path === "string") availableSources.add(path); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** ANSWER 后验:扫描所有引用,验证是否有效 */ |
| 117 | async function* validateAnswerRefs(text: string): AsyncGenerator<AgentEvent> { |