@p-dsh-market/conversation-knowledge-map 0.1.14 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/generation-orchestrator.js +43 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ DSH 的“知识视图”插件,把同一工作路径下用户明确选择的
|
|
|
11
11
|
|
|
12
12
|
配置面板会列出运行时可用的 Provider / Model,并默认带入 DSH 默认模型;本次选择会绑定到确认令牌、Agent 调用和 `manifest.json`,不会因为默认模型变化而被静默替换。提交任务后配置对话框立即关闭,失败原因会显示在知识视图页的任务条中。
|
|
13
13
|
|
|
14
|
-
摘要阶段按对话并行处理,并发上限固定为 3;每个对话按约 5000
|
|
14
|
+
摘要阶段按对话并行处理,并发上限固定为 3;每个对话按约 5000 字、优先在完整段落边界分段,只有单段本身超长时才按句末或硬边界拆分。同一对话的分段保持顺序处理。思维导图使用合并后的会话摘要;知识图谱直接使用保留来源的分段摘要,避免长对话在二次压缩后丢失实体。知识图谱根据已读取文本量和有效分段数动态计算上限,范围为 30–100 个实体、最多 200 条关系,并要求逐分段检查覆盖范围。结构化输出失败时会完全重置并最多重试 3 次;单个对话连续失败后只跳过该对话,不阻断其他对话和最终报告。“同时生成”模式下,一个最终视图失败也不会阻断另一个视图保存。进度区域和最终知识视图都会按时间线显示读取、摘要、具体失败原因、重试、跳过、合并、生成及保存过程;知识图谱时间线还会显示模型返回数量和来源校验后的保留数量。模型可以保留 thinking,但结果提取只读取最终文本并过滤 reasoning 内容;摘要和最终视图分别预留 12000 与 24000 个输出 Token。最终模型若返回未选择的 Session 引用,会过滤该引用;严格模式下无有效来源的内容项会被跳过,最终页面列出实际总结的对话、失败对话和过滤数量。
|
|
15
15
|
|
|
16
16
|
生成过程时间线支持折叠和展开:进行中的任务默认展开,已完成结果中的历史时间线默认折叠。思维导图使用递归树形布局并绘制父子连接线。知识图谱按关联度使用中心节点与内外双环布局,节点卡片内显示类型和最多两行名称,关系以带方向箭头的弱化曲线呈现,并通过描边区分推测或冲突内容;画布提供 50%–200% 缩放和一键恢复 100%,缩放不会影响实体筛选、点击和详情查看,也不会产生横向滚动条。
|
|
17
17
|
|
|
@@ -10,6 +10,9 @@ const SUMMARY_MAX_TOKENS = 12000
|
|
|
10
10
|
const VIEW_MAX_TOKENS = 24000
|
|
11
11
|
const SUMMARY_CONCURRENCY = 3
|
|
12
12
|
const MAX_MODEL_RETRIES = 3
|
|
13
|
+
const MIN_GRAPH_ENTITIES = 30
|
|
14
|
+
const MAX_GRAPH_ENTITIES = 100
|
|
15
|
+
const MAX_GRAPH_RELATIONS = 200
|
|
13
16
|
|
|
14
17
|
function modelLabel(selection) {
|
|
15
18
|
const provider = String(selection?.provider || '').trim()
|
|
@@ -154,7 +157,7 @@ function normalizeSummary(value, source, chunk) {
|
|
|
154
157
|
const summary = shortText(result.summary || result.narrative || result.text, MAX_SUMMARY_CHARS)
|
|
155
158
|
if (!summary) throw new Error(`对话 ${source.sessionId} 的摘要为空。`)
|
|
156
159
|
const keyPoints = Array.isArray(result.keyPoints)
|
|
157
|
-
? result.keyPoints.map((item) => shortText(item, 500)).filter(Boolean).slice(0,
|
|
160
|
+
? result.keyPoints.map((item) => shortText(item, 500)).filter(Boolean).slice(0, 24)
|
|
158
161
|
: []
|
|
159
162
|
const sourceRefs = sanitizeSourceRefs(result.sourceRefs, source, sourceRefsFromChunk(source, chunk))
|
|
160
163
|
return {
|
|
@@ -177,6 +180,31 @@ function mergeSourceSummaries(source, chunkSummaries) {
|
|
|
177
180
|
return { sessionId: source.sessionId, title: source.title, summary, keyPoints, sourceRefs }
|
|
178
181
|
}
|
|
179
182
|
|
|
183
|
+
function graphGenerationContext(successfulResults) {
|
|
184
|
+
return successfulResults.flatMap((result) => (result.chunkSummaries || []).map((summary, index) => ({
|
|
185
|
+
sessionId: summary.sessionId,
|
|
186
|
+
title: summary.title,
|
|
187
|
+
chunk: index + 1,
|
|
188
|
+
summary: summary.summary,
|
|
189
|
+
keyPoints: summary.keyPoints,
|
|
190
|
+
sourceRefs: summary.sourceRefs
|
|
191
|
+
})))
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function graphGenerationBudget(sources, context) {
|
|
195
|
+
const sourceChars = sources.reduce((total, source) => total + String(source?.text || '').length, 0)
|
|
196
|
+
const evidenceUnits = Math.max(sources.length, context.length)
|
|
197
|
+
const entities = Math.min(MAX_GRAPH_ENTITIES, Math.max(
|
|
198
|
+
MIN_GRAPH_ENTITIES,
|
|
199
|
+
Math.ceil(sourceChars / 1200),
|
|
200
|
+
evidenceUnits * 6
|
|
201
|
+
))
|
|
202
|
+
return {
|
|
203
|
+
entities,
|
|
204
|
+
relations: Math.min(MAX_GRAPH_RELATIONS, Math.max(entities, Math.ceil(entities * 1.8)))
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
180
208
|
async function mapWithConcurrency(items, limit, mapper) {
|
|
181
209
|
const results = new Array(items.length)
|
|
182
210
|
let nextIndex = 0
|
|
@@ -300,7 +328,7 @@ function assertOutputSourceRefs(value, sources) {
|
|
|
300
328
|
for (const relation of value?.knowledgeGraph?.relations || []) for (const ref of relation.evidence || []) check(ref, `知识图谱关系 ${relation.id}`)
|
|
301
329
|
}
|
|
302
330
|
|
|
303
|
-
function outputPrompt(kind, summaries, prompt, strict) {
|
|
331
|
+
function outputPrompt(kind, summaries, prompt, strict, options = {}) {
|
|
304
332
|
const rules = [
|
|
305
333
|
'只输出一个 JSON 对象,不要 Markdown 代码围栏,不要额外解释。',
|
|
306
334
|
'所有 sourceRefs/evidence 必须使用给定的 sessionId 和 eventSeqs,不能虚构来源。',
|
|
@@ -317,13 +345,16 @@ function outputPrompt(kind, summaries, prompt, strict) {
|
|
|
317
345
|
`对话摘要:${context}`
|
|
318
346
|
].join('\n\n')
|
|
319
347
|
}
|
|
348
|
+
const graphBudget = options.graphBudget || { entities: MIN_GRAPH_ENTITIES, relations: Math.ceil(MIN_GRAPH_ENTITIES * 1.8) }
|
|
320
349
|
return [
|
|
321
|
-
'
|
|
322
|
-
|
|
350
|
+
'请根据以下多个对话分段证据生成高召回的静态知识图谱。系统性抽取人物/组织、实体、概念、模块、接口、数据对象、工具、外部系统、需求、约束、决策、结论、风险、问题和行动项,并只建立有依据的关系。',
|
|
351
|
+
`本次内容规模对应的上限为 ${graphBudget.entities} 个实体、${graphBudget.relations} 条关系。这是防止输出失控的上限,不是要求凑数;在有依据的前提下应尽量完整覆盖每个对话和分段,不要只保留少数总括性主题。实体 summary 控制在 220 字以内。`,
|
|
352
|
+
'同名同义实体应合并,但不同模块、接口、决策、风险或行动项不能因为属于同一主题就合并。优先保留能承载具体关系的细粒度实体;每个分段都要检查是否存在尚未覆盖的新实体和关系。',
|
|
353
|
+
'不要输出 schema 之外的字段。确保最终内容是可被 JSON.parse 直接解析的完整 JSON。',
|
|
323
354
|
'JSON 形状:{"entities":[{"id":"...","type":"...","name":"...","summary":"...","confidence":"confirmed|inferred|conflicted","sourceRefs":[{"sessionId":"...","eventSeqs":[1]}]}],"relations":[{"id":"...","from":"...","to":"...","type":"depends_on|calls|supports|constrains|belongs_to|derived_from","confidence":"confirmed|inferred|conflicted","evidence":[{"sessionId":"...","eventSeqs":[1]}]}]}。',
|
|
324
355
|
...rules,
|
|
325
356
|
`额外要求:${shortText(prompt, 2000) || '没有额外要求。'}`,
|
|
326
|
-
|
|
357
|
+
`对话分段证据:${context}`
|
|
327
358
|
].join('\n\n')
|
|
328
359
|
}
|
|
329
360
|
|
|
@@ -331,7 +362,7 @@ function summaryPrompt(source, chunk, strict) {
|
|
|
331
362
|
return [
|
|
332
363
|
'请把一段 DSH 对话整理成带来源的结构化摘要。不要复述完整聊天,不要添加对话中没有的事实。',
|
|
333
364
|
'只输出 JSON:{"summary":"完整阶段性说明","keyPoints":["..."],"sourceRefs":[{"sessionId":"...","eventSeqs":[1]}]}。',
|
|
334
|
-
'summary 控制在 1200 字以内;keyPoints 最多
|
|
365
|
+
'summary 控制在 1200 字以内;keyPoints 最多 16 条,每条不超过 160 字。keyPoints 要优先保留具名的人物/组织、模块、接口、数据对象、工具、外部系统、需求、约束、决策、风险、问题、行动项以及它们之间的关系,不要只写总括性主题。不要增加其他字段。字符串中的双引号必须正确转义,确保整个输出可以被 JSON.parse 直接解析。',
|
|
335
366
|
strict ? '严格模式:每个关键点都要能回指给定事件。' : '允许标记尚未确认的冲突,但不能编造事件序号。',
|
|
336
367
|
`Session:${source.sessionId}`,
|
|
337
368
|
`标题:${source.title}`,
|
|
@@ -684,7 +715,7 @@ export class KnowledgeGenerationOrchestrator {
|
|
|
684
715
|
sourceCount: sources.length,
|
|
685
716
|
progress: { percent: 10 + Math.floor(60 * completedSources / sources.length), current: completedSources, total: sources.length, label: `已完成摘要:${source.title}` }
|
|
686
717
|
})
|
|
687
|
-
return { source, error: '', summary: mergeSourceSummaries(source, chunkSummaries) }
|
|
718
|
+
return { source, error: '', summary: mergeSourceSummaries(source, chunkSummaries), chunkSummaries }
|
|
688
719
|
})
|
|
689
720
|
this.assertNotCancelled(task)
|
|
690
721
|
const successfulResults = summaryResults.filter((result) => result?.summary)
|
|
@@ -694,6 +725,8 @@ export class KnowledgeGenerationOrchestrator {
|
|
|
694
725
|
if (!successfulResults.length) throw new Error(`所选 ${sources.length} 个对话均未能生成合法摘要,已分别重试 ${MAX_MODEL_RETRIES} 次。`)
|
|
695
726
|
const summarizedSources = successfulResults.map((result) => result.source)
|
|
696
727
|
const summaries = successfulResults.map((result) => result.summary)
|
|
728
|
+
const graphContext = graphGenerationContext(successfulResults)
|
|
729
|
+
const graphBudget = graphGenerationBudget(summarizedSources, graphContext)
|
|
697
730
|
const sourceSessionIds = summarizedSources.map((source) => source.sessionId)
|
|
698
731
|
this.recordTimeline(task, 'merge', `已合并 ${summaries.length} 个对话摘要${failedSources.length ? `,跳过 ${failedSources.length} 个失败对话` : ''},开始生成最终视图。`)
|
|
699
732
|
let mindMap = null
|
|
@@ -740,17 +773,18 @@ export class KnowledgeGenerationOrchestrator {
|
|
|
740
773
|
if (request.outputMode === 'knowledge-graph' || request.outputMode === 'both') {
|
|
741
774
|
this.recordTimeline(task, 'view-start', '开始生成知识图谱。')
|
|
742
775
|
this.update(task, 'building-knowledge-graph', { progress: { percent: 86, current: sources.length, total: sources.length, label: '生成知识图谱' } })
|
|
743
|
-
const baseGraphPrompt = outputPrompt('knowledge-graph',
|
|
776
|
+
const baseGraphPrompt = outputPrompt('knowledge-graph', graphContext, request.prompt, request.strict === true, { graphBudget })
|
|
744
777
|
let graphError = null
|
|
745
778
|
for (let attempt = 0; attempt <= MAX_MODEL_RETRIES; attempt += 1) {
|
|
746
779
|
try {
|
|
747
|
-
const prompt = attempt === 0 ? baseGraphPrompt : `${baseGraphPrompt}\n\n第 ${attempt} 次输出未通过校验:${shortText(errorMessage(graphError), 500)}
|
|
780
|
+
const prompt = attempt === 0 ? baseGraphPrompt : `${baseGraphPrompt}\n\n第 ${attempt} 次输出未通过校验:${shortText(errorMessage(graphError), 500)}。请完全重置并从头生成,修正结构或来源错误,同时保持对各分段的完整覆盖;只有确实超过本次上限时才精简。只输出完整、严格合法的 JSON。`
|
|
748
781
|
const value = await this.runModel({
|
|
749
782
|
kind: 'knowledge-graph', prompt, cwd: request.cwd, strict: request.strict === true,
|
|
750
783
|
selectedSessionIds: sourceSessionIds, model: request.model, signal: task.controller.signal
|
|
751
784
|
})
|
|
752
785
|
const sanitized = sanitizeKnowledgeGraphSources(value, summarizedSources, request.strict === true)
|
|
753
786
|
knowledgeGraph = validateKnowledgeGraph(sanitized.value, { selectedSessionIds: sourceSessionIds, strict: request.strict === true })
|
|
787
|
+
this.recordTimeline(task, 'graph-coverage', `知识图谱模型返回 ${Array.isArray(value?.entities) ? value.entities.length : 0} 个实体、${Array.isArray(value?.relations) ? value.relations.length : 0} 条关系;来源校验后保留 ${knowledgeGraph.entities.length} 个实体、${knowledgeGraph.relations.length} 条关系。`, { entityLimit: graphBudget.entities, relationLimit: graphBudget.relations })
|
|
754
788
|
sourceWarnings.skippedRefs += sanitized.skippedRefs
|
|
755
789
|
sourceWarnings.skippedItems += sanitized.skippedItems
|
|
756
790
|
graphError = null
|