@p-dsh-market/conversation-knowledge-map 0.1.4 → 0.1.6
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/lib/generation-orchestrator.js +51 -4
- package/lib/protocol.js +6 -1
- package/package.json +1 -1
|
@@ -6,6 +6,8 @@ import { clone, diagnosticSummary, errorMessage, logMessage, makeUserMessage, sh
|
|
|
6
6
|
import { chunkSourceText, readSelectedSurfaces } from './session-source.js'
|
|
7
7
|
|
|
8
8
|
const MAX_SUMMARY_CHARS = 2400
|
|
9
|
+
const SUMMARY_MAX_TOKENS = 8000
|
|
10
|
+
const VIEW_MAX_TOKENS = 12000
|
|
9
11
|
|
|
10
12
|
function modelLabel(selection) {
|
|
11
13
|
const provider = String(selection?.provider || '').trim()
|
|
@@ -251,6 +253,19 @@ function agentTurnError(event) {
|
|
|
251
253
|
return error
|
|
252
254
|
}
|
|
253
255
|
|
|
256
|
+
function agentTurnLimit(event) {
|
|
257
|
+
if (String(event?.type || '') !== 'turn/end') return null
|
|
258
|
+
const data = event?.data && typeof event.data === 'object' ? event.data : {}
|
|
259
|
+
const reason = data.reason && typeof data.reason === 'object' ? data.reason : null
|
|
260
|
+
if (reason?.kind !== 'max-tokens') return null
|
|
261
|
+
const error = new Error('模型输出达到最大 Token 限制,JSON 尚未完成。请减少所选对话数量或内容长度后重试。')
|
|
262
|
+
error.code = 'MAX_TOKENS'
|
|
263
|
+
error.agentTurn = true
|
|
264
|
+
error.tokenLimit = true
|
|
265
|
+
error.cause = reason
|
|
266
|
+
return error
|
|
267
|
+
}
|
|
268
|
+
|
|
254
269
|
function findAgentTurnError(surface) {
|
|
255
270
|
const events = Array.isArray(surface?.events) ? surface.events : []
|
|
256
271
|
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
@@ -260,6 +275,15 @@ function findAgentTurnError(surface) {
|
|
|
260
275
|
return null
|
|
261
276
|
}
|
|
262
277
|
|
|
278
|
+
function findAgentTurnLimit(surface) {
|
|
279
|
+
const events = Array.isArray(surface?.events) ? surface.events : []
|
|
280
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
281
|
+
const error = agentTurnLimit(events[index])
|
|
282
|
+
if (error) return error
|
|
283
|
+
}
|
|
284
|
+
return null
|
|
285
|
+
}
|
|
286
|
+
|
|
263
287
|
function wrapAgentRuntimeError(error) {
|
|
264
288
|
if (error?.agentTurn || /^Agent Runtime 生成失败/.test(errorMessage(error))) return error
|
|
265
289
|
const wrapped = new Error(`Agent Runtime 生成失败:${errorMessage(error)}`)
|
|
@@ -286,6 +310,13 @@ async function readAgentText(sessionQuery, sessionId, signal, { logger, diagnost
|
|
|
286
310
|
logMessage(logger, 'error', 'agent turn failure session=%s source=surface code=%s error=%s', logId(sessionId), surfaceError.code || '', errorMessage(surfaceError))
|
|
287
311
|
throw surfaceError
|
|
288
312
|
}
|
|
313
|
+
const surfaceLimit = findAgentTurnLimit(surface)
|
|
314
|
+
if (surfaceLimit && !stats.agentLimit) {
|
|
315
|
+
stats.agentLimit = surfaceLimit
|
|
316
|
+
stats.agentFailureSource = 'surface'
|
|
317
|
+
stats.agentFailureCode = surfaceLimit.code
|
|
318
|
+
logMessage(logger, 'warn', 'agent token limit session=%s source=surface error=%s', logId(sessionId), errorMessage(surfaceLimit))
|
|
319
|
+
}
|
|
289
320
|
if (text.trim()) return text
|
|
290
321
|
if (attempt < 5) await new Promise((resolve) => setTimeout(resolve, 40 * (attempt + 1)))
|
|
291
322
|
}
|
|
@@ -305,6 +336,13 @@ async function readAgentText(sessionQuery, sessionId, signal, { logger, diagnost
|
|
|
305
336
|
logMessage(logger, 'error', 'agent turn failure session=%s source=session code=%s error=%s', logId(sessionId), sessionError.code || '', errorMessage(sessionError))
|
|
306
337
|
throw sessionError
|
|
307
338
|
}
|
|
339
|
+
const sessionLimit = findAgentTurnLimit(log)
|
|
340
|
+
if (sessionLimit && !stats.agentLimit) {
|
|
341
|
+
stats.agentLimit = sessionLimit
|
|
342
|
+
stats.agentFailureSource = 'session'
|
|
343
|
+
stats.agentFailureCode = sessionLimit.code
|
|
344
|
+
logMessage(logger, 'warn', 'agent token limit session=%s source=session error=%s', logId(sessionId), errorMessage(sessionLimit))
|
|
345
|
+
}
|
|
308
346
|
if (text.trim()) return text
|
|
309
347
|
}
|
|
310
348
|
return ''
|
|
@@ -524,8 +562,9 @@ export class KnowledgeGenerationOrchestrator {
|
|
|
524
562
|
logMessage(this.logger, 'info', 'agent output parsed kind=%s source=%s value=%s result=%s', input.kind, source, diagnosticSummary(value), diagnosticSummary(result))
|
|
525
563
|
return result
|
|
526
564
|
} catch (error) {
|
|
527
|
-
|
|
528
|
-
|
|
565
|
+
const surfacedError = diagnostics.agentLimit || error
|
|
566
|
+
logMessage(this.logger, 'error', 'agent output parse failed kind=%s source=%s value=%s diagnostics=%s error=%s', input.kind, source, diagnosticSummary(value), JSON.stringify(diagnostics), errorMessage(surfacedError))
|
|
567
|
+
throw surfacedError
|
|
529
568
|
}
|
|
530
569
|
}
|
|
531
570
|
if (typeof this.modelRunner === 'function') {
|
|
@@ -562,7 +601,8 @@ export class KnowledgeGenerationOrchestrator {
|
|
|
562
601
|
lastSessionEventCount: 0,
|
|
563
602
|
agentFailureSource: '',
|
|
564
603
|
agentFailureCode: '',
|
|
565
|
-
agentFailure: null
|
|
604
|
+
agentFailure: null,
|
|
605
|
+
agentLimit: null
|
|
566
606
|
}
|
|
567
607
|
logMessage(this.logger, 'info', 'agent call start kind=%s session=%s provider=%s model=%s promptLength=%d', input.kind, logId(sessionId), provider, model, String(input.prompt || '').length)
|
|
568
608
|
let unsubscribe
|
|
@@ -584,6 +624,13 @@ export class KnowledgeGenerationOrchestrator {
|
|
|
584
624
|
diagnostics.agentFailureCode = failure.code || ''
|
|
585
625
|
logMessage(this.logger, 'error', 'agent turn failure session=%s source=live-event code=%s error=%s', logId(sessionId), failure.code || '', errorMessage(failure))
|
|
586
626
|
}
|
|
627
|
+
const limit = agentTurnLimit(event)
|
|
628
|
+
if (limit && !diagnostics.agentLimit) {
|
|
629
|
+
diagnostics.agentLimit = limit
|
|
630
|
+
diagnostics.agentFailureSource = 'live-event'
|
|
631
|
+
diagnostics.agentFailureCode = limit.code
|
|
632
|
+
logMessage(this.logger, 'warn', 'agent token limit session=%s source=live-event error=%s', logId(sessionId), errorMessage(limit))
|
|
633
|
+
}
|
|
587
634
|
})
|
|
588
635
|
logMessage(this.logger, 'info', 'agent event subscription ready session=%s', logId(sessionId))
|
|
589
636
|
} catch (error) {
|
|
@@ -596,7 +643,7 @@ export class KnowledgeGenerationOrchestrator {
|
|
|
596
643
|
handle = await this.agents.create({
|
|
597
644
|
sessionId,
|
|
598
645
|
meta: { cwd: input.cwd, origin: 'subagent' },
|
|
599
|
-
agentOptions: { provider, model, maxTokens: input.kind === 'summary' ?
|
|
646
|
+
agentOptions: { provider, model, maxTokens: input.kind === 'summary' ? SUMMARY_MAX_TOKENS : VIEW_MAX_TOKENS },
|
|
600
647
|
signal: input.signal,
|
|
601
648
|
setup: async (agentCtx) => {
|
|
602
649
|
agentCtx?.systemPrompt?.section?.({
|
package/lib/protocol.js
CHANGED
|
@@ -129,6 +129,11 @@ export function makeUserMessage(text, messageId = `knowledge-map-${Date.now()}`)
|
|
|
129
129
|
return {
|
|
130
130
|
id: messageId,
|
|
131
131
|
role: 'user',
|
|
132
|
-
content: [{ type: 'text', text: String(text || '') }]
|
|
132
|
+
content: [{ type: 'text', text: String(text || '') }],
|
|
133
|
+
source: {
|
|
134
|
+
kind: 'plugin',
|
|
135
|
+
plugin: '@p-dsh-market/conversation-knowledge-map',
|
|
136
|
+
form: 'generation'
|
|
137
|
+
}
|
|
133
138
|
}
|
|
134
139
|
}
|