@v587d/capital-generation 2.19.2 → 2.20.0

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.
@@ -14,12 +14,9 @@ import {
14
14
  } from './schema.js'
15
15
  import { sanitizeMessage } from './redact.js'
16
16
 
17
- export const BRIEF_PROMPT_VERSION = 6
17
+ export const BRIEF_PROMPT_VERSION = 7
18
18
  export const BRIEF_PENDING_STALE_MS = 10 * 60_000
19
19
  export const BRIEF_MAX_ATTEMPTS = 5
20
- export const BRIEF_MAX_ANSWER_CHARS = 12_000
21
- export const BRIEF_HEAD_CHARS = 6_000
22
- export const BRIEF_TAIL_CHARS = 6_000
23
20
  export const BRIEF_TIMEOUT_MS = 90_000
24
21
  // Reasoning models spend 1.5–2.5k thinking tokens before the JSON; 2.4k finishes at `length`.
25
22
  const BRIEF_MAX_TOKENS = 8_000
@@ -154,14 +151,17 @@ export function extractSessionAnswer(snapshot) {
154
151
  }
155
152
 
156
153
  /** Build the one input object the model sees. sessionId is traceability, not extra knowledge. */
157
- export function sourceBlocksOf(text) {
154
+ export function sourceBlocksOf(text, maxLineChars = Number.POSITIVE_INFINITY) {
158
155
  const lines = String(text ?? '')
159
156
  .split('\n')
160
157
  .map((line) => line.trim())
161
158
  .filter((line) => line.length > 0)
162
159
  .filter((line) => !line.includes('……(中间省略)……'))
163
160
  .filter((line) => !/^\|?[\s:|-]+\|?$/.test(line))
164
- return lines.map((line, index) => ({ id: index + 1, text: line.slice(0, 600) }))
161
+ return lines.map((line, index) => ({
162
+ id: index + 1,
163
+ text: Number.isFinite(maxLineChars) ? line.slice(0, maxLineChars) : line,
164
+ }))
165
165
  }
166
166
 
167
167
  export function buildBriefPayload({ sessionId, task, run, answer, coverage }) {
@@ -176,23 +176,15 @@ export function buildBriefPayload({ sessionId, task, run, answer, coverage }) {
176
176
  }
177
177
  }
178
178
 
179
- export function clipFinalAnswer(text) {
180
- const source = String(text ?? '').replace(/\u0000/g, '').trim()
181
- const chars = Array.from(source)
182
- if (chars.length <= BRIEF_MAX_ANSWER_CHARS) return { text: source, coverage: 'full' }
183
- const head = chars.slice(0, BRIEF_HEAD_CHARS).join('')
184
- const tail = chars.slice(-BRIEF_TAIL_CHARS).join('')
185
- return {
186
- text: `${head}\n\n……(中间省略)……\n\n${tail}`,
187
- coverage: 'partial',
188
- }
179
+ export function normalizeFinalAnswer(text) {
180
+ return { text: String(text ?? '').replace(/\u0000/g, '').trim(), coverage: 'full' }
189
181
  }
190
182
 
191
183
  export function sourceHashOf(text) {
192
184
  return createHash('sha256').update(String(text ?? '')).digest('hex').slice(0, 16)
193
185
  }
194
186
 
195
- function normalizeQuote(text) {
187
+ export function normalizeQuote(text) {
196
188
  // Compare content only: NFKC folds full-width forms, then drop all whitespace,
197
189
  // punctuation and symbols. Models often add periods/quotes/ellipses or rewrite
198
190
  // table pipes when quoting; those must not read as a fabricated citation.
@@ -216,7 +208,7 @@ function optionalDate(value) {
216
208
  return value
217
209
  }
218
210
 
219
- function includesQuote(source, quote) {
211
+ export function includesQuote(source, quote) {
220
212
  const normalized = normalizeQuote(source)
221
213
  const wanted = normalizeQuote(quote)
222
214
  return wanted.length > 0 && normalized.includes(wanted)
@@ -418,7 +410,7 @@ async function readSessionAnswer(sessionQuery, sessionId) {
418
410
  throw new BriefValidationError('session_unavailable', `读取 session 失败:${String(lastError?.message ?? lastError)}`)
419
411
  }
420
412
 
421
- async function streamText(llm, options) {
413
+ export async function streamText(llm, options) {
422
414
  const stream = llm.stream(options)
423
415
  let text = ''
424
416
  for await (const chunk of stream) {
@@ -465,6 +457,7 @@ export function createBriefService(options) {
465
457
  scheduler,
466
458
  now = () => Date.now(),
467
459
  logger,
460
+ onBriefReady,
468
461
  promptVersion = BRIEF_PROMPT_VERSION,
469
462
  maxActive = BRIEF_MAX_ACTIVE_PER_PROFILE,
470
463
  recoveryIntervalMs = BRIEF_RECOVERY_INTERVAL_MS,
@@ -551,23 +544,31 @@ export function createBriefService(options) {
551
544
  }
552
545
  if (!owner()) return
553
546
 
554
- const clipped = clipFinalAnswer(extracted.text)
555
- const sourceHash = sourceHashOf(clipped.text)
547
+ const normalized = normalizeFinalAnswer(extracted.text)
548
+ const sourceHash = sourceHashOf(normalized.text)
556
549
  const task = await store.getTask(run.taskId).catch(() => undefined)
557
550
  const payload = buildBriefPayload({
558
551
  sessionId: run.sessionId,
559
552
  task,
560
553
  run,
561
- answer: clipped.text,
562
- coverage: clipped.coverage,
554
+ answer: normalized.text,
555
+ coverage: normalized.coverage,
563
556
  })
564
557
  const route = extracted.provider !== undefined && extracted.model !== undefined
565
558
  ? { provider: extracted.provider, model: extracted.model }
566
559
  : fallbackModelRoute(ctx)
567
560
  if (route === undefined) {
568
- await markFailed(runId, new Error('没有可用的模型路由'), { sourceHash, coverage: clipped.coverage, promptVersion })
561
+ await markFailed(runId, new Error('没有可用的模型路由'), { sourceHash, coverage: normalized.coverage, promptVersion })
569
562
  return
570
563
  }
564
+ // Evaluate TODO conditions from the same answer even when brief generation later fails or skips.
565
+ if (typeof onBriefReady === 'function') {
566
+ try {
567
+ await onBriefReady({ run, task, text: normalized.text, route })
568
+ } catch (error) {
569
+ logger?.warn?.(`capital-automation: TODO 核验入队失败:${String(error)}`)
570
+ }
571
+ }
571
572
 
572
573
  let attempts = brief.attempts
573
574
  let raw
@@ -586,17 +587,17 @@ export function createBriefService(options) {
586
587
  }
587
588
  }
588
589
  if (raw === undefined) {
589
- await markFailed(runId, callError ?? new Error('模型调用失败'), { attempts, model: routeLabel(route), sourceHash, coverage: clipped.coverage, promptVersion })
590
+ await markFailed(runId, callError ?? new Error('模型调用失败'), { attempts, model: routeLabel(route), sourceHash, coverage: normalized.coverage, promptVersion })
590
591
  return
591
592
  }
592
593
  if (!owner()) return
593
594
 
594
595
  let parsed
595
596
  try {
596
- parsed = parseBriefOutput(raw, clipped.text, payload.sourceBlocks)
597
+ parsed = parseBriefOutput(raw, normalized.text, payload.sourceBlocks)
597
598
  } catch (firstError) {
598
599
  if (attempts >= BRIEF_MAX_ATTEMPTS) {
599
- await markFailed(runId, firstError, { attempts, model: routeLabel(route), sourceHash, coverage: clipped.coverage, promptVersion })
600
+ await markFailed(runId, firstError, { attempts, model: routeLabel(route), sourceHash, coverage: normalized.coverage, promptVersion })
600
601
  return
601
602
  }
602
603
  try {
@@ -616,9 +617,9 @@ export function createBriefService(options) {
616
617
  },
617
618
  },
618
619
  })
619
- parsed = parseBriefOutput(repair, clipped.text, payload.sourceBlocks)
620
+ parsed = parseBriefOutput(repair, normalized.text, payload.sourceBlocks)
620
621
  } catch (secondError) {
621
- await markFailed(runId, secondError, { attempts, model: routeLabel(route), sourceHash, coverage: clipped.coverage, promptVersion })
622
+ await markFailed(runId, secondError, { attempts, model: routeLabel(route), sourceHash, coverage: normalized.coverage, promptVersion })
622
623
  return
623
624
  }
624
625
  }
@@ -631,7 +632,7 @@ export function createBriefService(options) {
631
632
  model: routeLabel(route),
632
633
  promptVersion,
633
634
  sourceHash,
634
- coverage: clipped.coverage,
635
+ coverage: normalized.coverage,
635
636
  generatedAt: iso(now()),
636
637
  })
637
638
  return
@@ -647,7 +648,7 @@ export function createBriefService(options) {
647
648
  model: routeLabel(route),
648
649
  promptVersion,
649
650
  sourceHash,
650
- coverage: clipped.coverage,
651
+ coverage: normalized.coverage,
651
652
  generatedAt: iso(now()),
652
653
  })
653
654
  }