@thincoder/core 0.11.1 → 0.11.3
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/CHANGELOG.md +64 -2
- package/advisor/compaction.mjs +7 -5
- package/advisor/convergence.mjs +34 -3
- package/advisor/history.mjs +24 -11
- package/advisor/loop.mjs +49 -12
- package/advisor/messages.mjs +114 -33
- package/advisor/project-context.mjs +171 -42
- package/advisor/run.mjs +24 -10
- package/advisor.mjs +36 -46
- package/agent/batch-attribution.mjs +87 -0
- package/agent/batch-changed-surface.mjs +129 -0
- package/agent/chat-call.mjs +14 -9
- package/agent/completion.mjs +70 -41
- package/agent/continue-decision.mjs +14 -8
- package/agent/digest-account.mjs +133 -59
- package/agent/dispatch-gates.mjs +6 -0
- package/agent/dispatch-run.mjs +1 -1
- package/agent/dispatch.mjs +10 -4
- package/agent/family-tools.mjs +37 -23
- package/agent/helpers.mjs +139 -73
- package/agent/image-heal.mjs +18 -4
- package/agent/offload.mjs +25 -29
- package/agent/post-turn.mjs +36 -35
- package/agent/record-results.mjs +39 -7
- package/agent/run-stages.mjs +132 -52
- package/agent/run-start.mjs +150 -78
- package/agent/setup-reminders.mjs +194 -91
- package/agent/setup.mjs +219 -140
- package/agent/spawn-child.mjs +40 -3
- package/agent/suspension.mjs +43 -15
- package/agent/timers.mjs +9 -4
- package/agent/turn-domain-text.mjs +105 -0
- package/agent/turn-loop.mjs +84 -27
- package/agent/turn-projection.mjs +286 -0
- package/agent/turn-task-state.mjs +274 -0
- package/agent/turn-tool-process.mjs +163 -0
- package/agent/write-gate-ledger.mjs +569 -0
- package/agent/write-gate.mjs +29 -352
- package/agent-tools/advisor-async.mjs +35 -172
- package/agent-tools/advisor-launch.mjs +178 -0
- package/agent-tools/advisor-settle.mjs +28 -1
- package/agent-tools/advisor.mjs +37 -48
- package/agent-tools/async-discard.mjs +7 -2
- package/agent-tools/async-settle.mjs +18 -12
- package/agent-tools/audit-block.mjs +6 -6
- package/agent-tools/bash-async.mjs +5 -2
- package/agent-tools/batch-closeout.mjs +223 -35
- package/agent-tools/batch-gate.mjs +142 -342
- package/agent-tools/batch-ledger-bind.mjs +240 -20
- package/agent-tools/batch-ledger-view.mjs +109 -0
- package/agent-tools/batch-lifecycle.mjs +197 -48
- package/agent-tools/batch-paths.mjs +29 -8
- package/agent-tools/batch-read.mjs +151 -4
- package/agent-tools/batch-record-write.mjs +417 -0
- package/agent-tools/batch-regions.mjs +215 -0
- package/agent-tools/batch-reopen.mjs +23 -11
- package/agent-tools/batch-segment-vocab.mjs +459 -0
- package/agent-tools/batch-skeleton.mjs +196 -239
- package/agent-tools/batch-supersede.mjs +232 -0
- package/agent-tools/batch-tier-gate.mjs +214 -0
- package/agent-tools/batch.mjs +192 -439
- package/agent-tools/browser-async.mjs +5 -2
- package/agent-tools/consult.mjs +17 -11
- package/agent-tools/context.mjs +76 -33
- package/agent-tools/design-review-record.mjs +225 -0
- package/agent-tools/design-slots.mjs +1 -1
- package/agent-tools/design-token.mjs +279 -81
- package/agent-tools/digest-budget.mjs +10 -9
- package/agent-tools/doc-check.mjs +43 -33
- package/agent-tools/doc-graph.mjs +140 -0
- package/agent-tools/eng.mjs +12 -12
- package/agent-tools/escalate-async.mjs +5 -5
- package/agent-tools/goal.mjs +5 -2
- package/agent-tools/judge-domain.mjs +347 -0
- package/agent-tools/manifest.mjs +34 -18
- package/agent-tools/parent-channel.mjs +6 -3
- package/agent-tools/plan.mjs +11 -10
- package/agent-tools/read-history.mjs +112 -11
- package/agent-tools/review-facts.mjs +19 -6
- package/agent-tools/settings.mjs +23 -2
- package/agent-tools/skill.mjs +7 -8
- package/agent-tools/spawn-gates.mjs +14 -24
- package/agent-tools/ssh-session.mjs +8 -5
- package/agent-tools/ssh-text.mjs +155 -0
- package/agent-tools/ssh-transfer.mjs +1 -1
- package/agent-tools/ssh-transport.mjs +19 -148
- package/agent-tools/subagent-actions-query.mjs +10 -6
- package/agent-tools/subagent-actions.mjs +2 -2
- package/agent-tools/subagent-async.mjs +19 -17
- package/agent-tools/subagent-brief.mjs +76 -0
- package/agent-tools/subagent-design-token.mjs +168 -0
- package/agent-tools/subagent-run.mjs +1 -1
- package/agent-tools/subagent-scheduler.mjs +13 -13
- package/agent-tools/subagent-spawn.mjs +92 -112
- package/agent-tools/subagent.mjs +16 -28
- package/agent-tools/task.mjs +0 -8
- package/agent-tools/taskbook-block.mjs +94 -0
- package/agent-tools/timer.mjs +12 -8
- package/agent-tools/verify.mjs +13 -4
- package/agent-tools.mjs +7 -3
- package/agent.mjs +26 -7
- package/attachments.mjs +3 -3
- package/auto-think.mjs +5 -2
- package/browser/actions.mjs +9 -3
- package/browser/cdp.mjs +45 -17
- package/browser/launch.mjs +25 -43
- package/browser/pool.mjs +299 -0
- package/browser/queue.mjs +2 -0
- package/browser/session-state.mjs +450 -0
- package/browser/session.mjs +127 -151
- package/browser/snapshot.mjs +89 -0
- package/client-identity.mjs +1 -1
- package/compaction-work-state.mjs +230 -0
- package/compress-form.mjs +41 -6
- package/config-defaults.mjs +7 -3
- package/config-migrate.mjs +40 -0
- package/config-normalize.mjs +28 -2
- package/config-reload.mjs +59 -0
- package/config.mjs +110 -110
- package/context-degrade.mjs +93 -34
- package/context-echo.mjs +6 -15
- package/context-push.mjs +328 -31
- package/context.mjs +298 -170
- package/conventions.mjs +11 -9
- package/declaration.mjs +126 -22
- package/declared-repos.mjs +94 -0
- package/doc-check/doc-names.mjs +73 -0
- package/doc-check/doc-tokens.mjs +110 -0
- package/doc-check/existence.mjs +160 -0
- package/doc-check/index.mjs +309 -213
- package/doc-check/line-refs.mjs +27 -101
- package/doc-check/paragraph.mjs +218 -102
- package/doc-graph/anchors.mjs +474 -0
- package/doc-graph/doc-batch-face.mjs +59 -0
- package/doc-graph/doc-suffix.mjs +27 -0
- package/doc-graph/graph-tokens.mjs +160 -0
- package/doc-graph.mjs +778 -0
- package/escape.mjs +6 -5
- package/explore-distill.mjs +41 -8
- package/generate-title.mjs +10 -1
- package/i18n.mjs +1 -1
- package/instance-inbox-watch.mjs +272 -0
- package/instance-inbox.mjs +218 -0
- package/instance-registry.mjs +179 -0
- package/knowledge/README.md +3 -0
- package/knowledge/prompt-resources.md +10 -0
- package/ledger-close-proof.mjs +100 -49
- package/ledger-close.mjs +188 -0
- package/ledger-cmd.mjs +47 -91
- package/ledger-consistency.mjs +113 -0
- package/ledger-db.mjs +47 -15
- package/ledger-executors.mjs +1 -1
- package/ledger-tools.mjs +121 -62
- package/ledger-write-ops.mjs +162 -46
- package/ledger.mjs +1 -1
- package/manifest-discovery.mjs +3 -1
- package/manifest-schema.mjs +27 -5
- package/memory/docs.mjs +51 -15
- package/memory/file-list.mjs +6 -0
- package/memory/schema.mjs +2 -2
- package/model-specs.mjs +87 -21
- package/origin-guard.mjs +190 -0
- package/package.json +3 -1
- package/path-resolve.mjs +338 -57
- package/prompt-files.mjs +114 -2
- package/prompt-overlays.mjs +21 -11
- package/prompts/advisor-design.md +96 -36
- package/prompts/advisor-round1.md +48 -4
- package/prompts/advisor-round2.md +54 -10
- package/prompts/advisor-round3.md +53 -9
- package/prompts/common.md +99 -434
- package/prompts/consult-base.md +7 -4
- package/prompts/discipline-engineering.md +504 -295
- package/prompts/discipline-normal.md +106 -281
- package/prompts/knowledge/business-gates.md +30 -0
- package/prompts/knowledge/decision-quality.md +21 -0
- package/prompts/knowledge/doc-lookup.md +30 -0
- package/prompts/knowledge/document-writing.md +33 -0
- package/prompts/knowledge/documentation-evaluation.md +48 -0
- package/prompts/knowledge/evidence-discipline.md +38 -0
- package/prompts/knowledge/normal-coding-workflow.md +110 -0
- package/prompts/knowledge/normal-delivery-collaboration.md +124 -0
- package/prompts/knowledge/normal-work-management.md +60 -0
- package/prompts/knowledge/product-security.md +28 -0
- package/prompts/knowledge/project-records.md +73 -0
- package/prompts/knowledge/tool-advisor.md +29 -0
- package/prompts/knowledge/tool-apply-patch.md +17 -0
- package/prompts/knowledge/tool-bash.md +48 -0
- package/prompts/knowledge/tool-batch-eng-coder.md +19 -0
- package/prompts/knowledge/tool-batch-eng-designer.md +20 -0
- package/prompts/knowledge/tool-batch-review.md +18 -0
- package/prompts/knowledge/tool-batch.md +94 -0
- package/prompts/knowledge/tool-browser.md +68 -0
- package/prompts/knowledge/tool-code-search.md +4 -0
- package/prompts/knowledge/tool-consult-start.md +12 -0
- package/prompts/knowledge/tool-consult-stop.md +6 -0
- package/prompts/knowledge/tool-context.md +6 -0
- package/prompts/knowledge/tool-delete.md +21 -0
- package/prompts/knowledge/tool-doc-check.md +71 -0
- package/prompts/knowledge/tool-doc-graph-read.md +20 -0
- package/prompts/knowledge/tool-doc-graph.md +49 -0
- package/prompts/knowledge/tool-doc-search.md +6 -0
- package/prompts/knowledge/tool-edit.md +41 -0
- package/prompts/knowledge/tool-eng.md +2 -0
- package/prompts/knowledge/tool-execute.md +21 -0
- package/prompts/knowledge/tool-fetch.md +18 -0
- package/prompts/knowledge/tool-file-ops.md +21 -0
- package/prompts/knowledge/tool-get-current-time.md +7 -0
- package/prompts/knowledge/tool-git-read.md +21 -0
- package/prompts/knowledge/tool-git.md +29 -0
- package/prompts/knowledge/tool-glob.md +17 -0
- package/prompts/knowledge/tool-goal.md +8 -0
- package/prompts/knowledge/tool-grep.md +21 -0
- package/prompts/knowledge/tool-hashline-edit.md +16 -0
- package/prompts/knowledge/tool-insert-after.md +17 -0
- package/prompts/knowledge/tool-instance-list.md +6 -0
- package/prompts/knowledge/tool-instance-send.md +10 -0
- package/prompts/knowledge/tool-knowledge.md +18 -0
- package/prompts/knowledge/tool-ledger-read.md +28 -0
- package/prompts/knowledge/tool-ledger.md +46 -0
- package/prompts/knowledge/tool-lint.md +16 -0
- package/prompts/knowledge/tool-ls.md +15 -0
- package/prompts/knowledge/tool-lsp.md +11 -0
- package/prompts/knowledge/tool-main-history.md +2 -0
- package/prompts/knowledge/tool-manifest-read.md +14 -0
- package/prompts/knowledge/tool-manifest.md +34 -0
- package/prompts/knowledge/tool-memory-read.md +20 -0
- package/prompts/knowledge/tool-memory.md +19 -0
- package/prompts/knowledge/tool-notify-parent.md +12 -0
- package/prompts/knowledge/tool-operations.md +102 -0
- package/prompts/knowledge/tool-peer-instances.md +2 -0
- package/prompts/knowledge/tool-plan.md +5 -0
- package/prompts/knowledge/tool-process-read.md +13 -0
- package/prompts/knowledge/tool-process.md +15 -0
- package/prompts/knowledge/tool-question.md +16 -0
- package/prompts/knowledge/tool-read-history-batch.md +22 -0
- package/prompts/knowledge/tool-read-history.md +35 -0
- package/prompts/knowledge/tool-read-image.md +11 -0
- package/prompts/knowledge/tool-read.md +22 -0
- package/prompts/knowledge/tool-recent-changes.md +6 -0
- package/prompts/knowledge/tool-repo-outline.md +4 -0
- package/prompts/knowledge/tool-settings-read.md +12 -0
- package/prompts/knowledge/tool-settings.md +17 -0
- package/prompts/knowledge/tool-skill.md +6 -0
- package/prompts/knowledge/tool-ssh.md +74 -0
- package/prompts/knowledge/tool-subagent.md +57 -0
- package/prompts/knowledge/tool-task.md +14 -0
- package/prompts/knowledge/tool-team-ledger-read.md +11 -0
- package/prompts/knowledge/tool-team-ledger.md +15 -0
- package/prompts/knowledge/tool-timer.md +23 -0
- package/prompts/knowledge/tool-tree.md +16 -0
- package/prompts/knowledge/tool-verify.md +19 -0
- package/prompts/knowledge/tool-wait-for.md +25 -0
- package/prompts/knowledge/tool-websearch.md +19 -0
- package/prompts/knowledge/tool-write.md +12 -0
- package/prompts/knowledge/upstream-protocol.md +45 -0
- package/prompts/persona-coder.md +1 -3
- package/prompts/persona-eng-coder.md +24 -6
- package/prompts/persona-eng-designer.md +150 -171
- package/prompts/persona-engineering.md +531 -442
- package/prompts/persona-explore.md +1 -3
- package/prompts/persona-normal.md +17 -9
- package/prompts/persona-plan.md +5 -3
- package/prompts/persona-tester.md +0 -2
- package/prompts/subagent-base.md +2 -2
- package/provider/anthropic.mjs +10 -6
- package/provider/core.mjs +8 -5
- package/provider/errors.mjs +63 -4
- package/provider/google.mjs +29 -10
- package/provider/reasoning-echo.mjs +16 -0
- package/provider/responses.mjs +7 -3
- package/provider/sse.mjs +11 -6
- package/provider-flows.mjs +2 -2
- package/rules.mjs +11 -10
- package/session-index-build.mjs +1 -1
- package/session-index-pass.mjs +1 -1
- package/session-index-query.mjs +1 -1
- package/session-index.mjs +1 -1
- package/session-lifecycle.mjs +131 -42
- package/session-slot-write.mjs +38 -6
- package/session.mjs +30 -11
- package/shell-identity.mjs +129 -22
- package/skip-dirs.mjs +26 -2
- package/team-ledger.mjs +34 -30
- package/text-budget.mjs +1 -1
- package/think-off.mjs +27 -0
- package/token-ttl.mjs +74 -5
- package/token-window.mjs +371 -47
- package/tool-docs/advisor.md +2 -27
- package/tool-docs/apply_patch.md +1 -15
- package/tool-docs/bash.md +2 -43
- package/tool-docs/batch-eng-coder.md +3 -0
- package/tool-docs/batch-eng-designer.md +3 -0
- package/tool-docs/batch-review.md +3 -0
- package/tool-docs/batch.md +2 -84
- package/tool-docs/browser.md +2 -49
- package/tool-docs/code_search.md +2 -2
- package/tool-docs/consult_start.md +2 -8
- package/tool-docs/consult_stop.md +2 -4
- package/tool-docs/context.md +1 -5
- package/tool-docs/delete.md +2 -17
- package/tool-docs/doc_check.md +2 -107
- package/tool-docs/doc_graph-read.md +3 -0
- package/tool-docs/doc_graph.md +3 -0
- package/tool-docs/doc_search.md +2 -4
- package/tool-docs/edit.md +1 -38
- package/tool-docs/eng.md +3 -1
- package/tool-docs/execute.md +2 -18
- package/tool-docs/fetch.md +2 -14
- package/tool-docs/file_ops.md +2 -18
- package/tool-docs/get_current_time.md +1 -6
- package/tool-docs/git-read.md +3 -0
- package/tool-docs/git.md +2 -35
- package/tool-docs/glob.md +2 -10
- package/tool-docs/goal.md +1 -6
- package/tool-docs/grep.md +2 -18
- package/tool-docs/hashline_edit.md +2 -15
- package/tool-docs/insert_after.md +2 -16
- package/tool-docs/instance_list.md +3 -0
- package/tool-docs/instance_send.md +3 -0
- package/tool-docs/knowledge.md +3 -0
- package/tool-docs/ledger-read.md +3 -0
- package/tool-docs/ledger.md +3 -8
- package/tool-docs/lint.md +2 -10
- package/tool-docs/ls.md +2 -11
- package/tool-docs/lsp.md +2 -9
- package/tool-docs/main_history.md +3 -1
- package/tool-docs/manifest-read.md +2 -8
- package/tool-docs/manifest.md +2 -35
- package/tool-docs/memory-read.md +3 -0
- package/tool-docs/memory.md +2 -20
- package/tool-docs/notify_parent.md +2 -13
- package/tool-docs/peer_instances.md +3 -1
- package/tool-docs/plan.md +1 -5
- package/tool-docs/process-read.md +3 -0
- package/tool-docs/process.md +2 -13
- package/tool-docs/question.md +3 -17
- package/tool-docs/read.md +2 -19
- package/tool-docs/read_history-batch.md +3 -0
- package/tool-docs/read_history.md +2 -18
- package/tool-docs/read_image.md +2 -9
- package/tool-docs/recent_changes.md +2 -2
- package/tool-docs/repo_outline.md +2 -2
- package/tool-docs/settings-read.md +3 -0
- package/tool-docs/settings.md +1 -12
- package/tool-docs/skill.md +2 -6
- package/tool-docs/ssh.md +1 -62
- package/tool-docs/subagent.md +2 -100
- package/tool-docs/task.md +1 -7
- package/tool-docs/team_ledger-read.md +3 -0
- package/tool-docs/team_ledger.md +3 -9
- package/tool-docs/timer.md +2 -19
- package/tool-docs/tree.md +2 -13
- package/tool-docs/verify.md +2 -12
- package/tool-docs/wait_for.md +2 -32
- package/tool-docs/websearch.md +2 -19
- package/tool-docs/write.md +2 -12
- package/tools/browser.mjs +54 -9
- package/tools/doc-write-gate.mjs +73 -0
- package/tools/edit-batch.mjs +170 -92
- package/tools/edit-diff.mjs +78 -62
- package/tools/edit-receipt.mjs +39 -0
- package/tools/execute.mjs +3 -3
- package/tools/file.mjs +28 -14
- package/tools/git.mjs +36 -21
- package/tools/index.mjs +6 -0
- package/tools/knowledge.mjs +143 -0
- package/tools/linter.mjs +3 -3
- package/tools/ops.mjs +5 -3
- package/tools/patch.mjs +124 -44
- package/tools/process-doc-mark.mjs +33 -4
- package/tools/repomap.mjs +25 -3
- package/tools/search.mjs +74 -26
- package/tools/shared.mjs +29 -27
- package/tools/ssh.mjs +5 -5
- package/tools/tree.mjs +32 -10
- package/tools/web-deepseek.mjs +1 -1
- package/tools/web.mjs +2 -2
- package/tools/write-gate-params.mjs +31 -0
- package/tools/write-path.mjs +16 -4
- package/window-policy.mjs +243 -0
- package/doc-check/anchors.mjs +0 -235
package/context-push.mjs
CHANGED
|
@@ -1,30 +1,58 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* context-push.mjs —
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* `
|
|
2
|
+
* context-push.mjs — 消息双线写入单点 ∥ 请求装配单点(写缝面 · 自 `context.mjs` 迁出 · 2026-10-01 core 拆分批 #755 ∥ #786;
|
|
3
|
+
* 2026-11-17 context-usage-reading 批改为「机读线按轮嵌套 + 注入三段」——设计 = `docs/core/design/CONTEXT-COMPACTION.md` §1.1)。
|
|
4
|
+
*
|
|
5
|
+
* 一个档管两件事:消息怎么进线(`beginTurn` ∥ `pushReal` ∥ `pushInject` ∥ `setInject` ∥ `retain` ∥ `pushRecord`)、
|
|
6
|
+
* 怎么出线(`assembleRequestMessages` ∥ `flattenHistory`)。两向各一个实现点,消费面不自拼。
|
|
7
|
+
*
|
|
8
|
+
* 机读线形状:外层每个元素是一轮 `{ messages: Message[] }`,内层是该轮自己的消息;外层只有这一种元素。
|
|
9
|
+
* 轮的位置由结构给出(第 k 轮 = 外层第 k 个元素),消息上不带 `turn`,也不另立下标账。
|
|
10
|
+
*
|
|
11
|
+
* 注入一条都不落 `agent.history`,按存活期分三段(判据与格位表 = 设计档 §1「注入的三段:按存活期分」):
|
|
12
|
+
* A 当前状态 → `agent._inject`(固定 10 格,写即覆盖;`INJECT_SLOTS` 的次序即装配次序,
|
|
13
|
+
* 唯一例外 = `compaction-summary` 一格,它装配在对话之前)
|
|
14
|
+
* B 一次性事件 → `agent._pendingInject`(回合结束时由调用方清空)
|
|
15
|
+
* C 累积内容 → `agent._retained`(按名字追加、同名替换)
|
|
16
|
+
*
|
|
17
|
+
* 会话头块另存 `agent._sessionHead` 一个字符串(不落三段、不落机读线),装配期贴到第一条真实用户消息的
|
|
18
|
+
* 副本末尾;A 段其余 9 格并成一块贴到最后一条用户消息的副本末尾。两份文本都只贴副本,线上消息逐字不动
|
|
19
|
+
* (机制 = 设计档「会话头块」∥「注入贴在哪一条消息上」)。
|
|
20
|
+
*
|
|
21
|
+
* 人读线 `agent._fullHistory` 与记录存储仍由真实消息双写(`appendHumanLine` 是它们的单点);`pushRecord`
|
|
22
|
+
* 只写人读线、机读线零触——它不再构造假载体去复用 `pushReal`(占位依赖:机读线那一半一旦多写一处,
|
|
23
|
+
* 弃数组承接就悄悄丢一条,而没有任何断言看得见)。
|
|
6
24
|
*/
|
|
7
25
|
|
|
26
|
+
import { specForModel } from "./model-specs.mjs"
|
|
27
|
+
import { logOriginQuarantine, quarantineForeignOrigin, tagOriginProvider } from "./origin-guard.mjs"
|
|
28
|
+
import { isThinkingRequest } from "./think-off.mjs"
|
|
29
|
+
|
|
30
|
+
/** A 段格位闭集(次序 = 装配次序,唯一例外 = `compaction-summary` 一格装配在对话之前)。
|
|
31
|
+
* 一格一个写入方,格位才替代得了消息上的身份字段。
|
|
32
|
+
* `turn-domain` ∥ `run-overlay` 相邻成对:前者是这一轮为什么开始(只活一个请求),后者是端 overlay
|
|
33
|
+
* (整个 run 都成立);两格由 run 起点同一步写入,分居两格才活得下来各自的存活期
|
|
34
|
+
* (`docs/core/design/AGENT-LOOP-ASYNC-POOL.md` §8.6「两格:域文本与端 overlay」)。 */
|
|
35
|
+
export const INJECT_SLOTS = Object.freeze([
|
|
36
|
+
"compaction-summary", "tasks", "plan", "work-state", "peer", "time", "mode", "turn-domain", "run-overlay", "editor",
|
|
37
|
+
])
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
8
41
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* TUI-OOM-ROOTCAUSE 批(SESSION.md §6.14)——人读线内存有界 + 磁盘为准:
|
|
22
|
-
* ① `agent._recordStore?.append(msg)`:记录同步追加(磁盘为准——append-only sidecar);
|
|
23
|
-
* ② 窗口驱逐:绑定态(agent._historyWindow = 200)下 _fullHistory 只保最近窗口条——
|
|
24
|
-
* 更早内容仅存磁盘(翻页/检索/保存从盘按需读)。未绑定(模式 F)不驱逐(零回归)。
|
|
42
|
+
* 起一个轮元素(run 入口调用一次,续跑而外层为空时同样调用)。
|
|
43
|
+
* 轮界与账界是同一件事:`agent/run-start.mjs` 的 `beginTurnRecord` 与建账同一个位置、同一个条件。
|
|
44
|
+
*/
|
|
45
|
+
export function beginTurn(agent) {
|
|
46
|
+
if (!Array.isArray(agent.history)) agent.history = []
|
|
47
|
+
agent.history.push({ messages: [] })
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 人读线三段(档内私有):`ts` 打点 ∥ 记录存储追加(`_recordStore?.append`——SESSION.md §6.14)∥ 尾窗驱逐。
|
|
52
|
+
* `pushReal` 与 `pushRecord` 共用它——两条入口各写一半的历史到此为止。
|
|
25
53
|
* 追加失败不阻断回合(独立 try/catch——尽力面 N-S6;store 内部另置 degraded 并停写)。
|
|
26
54
|
*/
|
|
27
|
-
|
|
55
|
+
function appendHumanLine(agent, msg) {
|
|
28
56
|
if (!Array.isArray(agent._fullHistory)) agent._fullHistory = []
|
|
29
57
|
if (msg && msg.ts === undefined) msg.ts = Date.now()
|
|
30
58
|
agent._fullHistory.push(msg)
|
|
@@ -33,19 +61,288 @@ export function pushReal(agent, msg) {
|
|
|
33
61
|
if (win > 0 && agent._fullHistory.length > win) {
|
|
34
62
|
agent._fullHistory.splice(0, agent._fullHistory.length - win)
|
|
35
63
|
}
|
|
36
|
-
agent.history.push(msg)
|
|
37
64
|
}
|
|
38
65
|
|
|
39
66
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
67
|
+
* pushReal — the single entry point for REAL conversation messages.
|
|
68
|
+
* 真实消息(用户输入、模型回复、工具结果、多模态图像)追加到**外层最后一个轮元素**的内层 `messages`,
|
|
69
|
+
* 并双写入人读线 `agent._fullHistory` 与记录存储(`appendHumanLine`)。
|
|
70
|
+
*
|
|
71
|
+
* 它只做这一件事:不读任何账、不判轮界、不起轮元素——外层为空即这一轮没有起点,属编码错误,抛出而不就地补一个
|
|
72
|
+
* (就地补出来的轮元素会把两条不相干的对话并进同一轮,而没有任何断言看得见)。
|
|
73
|
+
* 消息时间戳(SESSION.md §6.9)在此打一次;`ts` 是仅本地字段,发送前由 `stripLocalMessageFields` 剥掉。
|
|
74
|
+
* 同一步给 assistant 消息打产出来源 `originProvider`(设计 `docs/core/design/AGENT-LOOP.md` §5.5.3–§5.5.4 写点):判据单源 = `origin-guard.mjs`
|
|
75
|
+
* 的 `tagOriginProvider`;provider 名不可得(`agent.provider?.name` 缺)时不打标,出生点没有判据可写就不猜一个名字。
|
|
76
|
+
*/
|
|
77
|
+
export function pushReal(agent, msg) {
|
|
78
|
+
const last = Array.isArray(agent?.history) ? agent.history.at(-1) : null
|
|
79
|
+
if (!last || !Array.isArray(last.messages)) {
|
|
80
|
+
throw new Error("pushReal: agent.history has no turn element — start one with beginTurn at the run entry")
|
|
81
|
+
}
|
|
82
|
+
tagOriginProvider(msg, agent.provider?.name)
|
|
83
|
+
appendHumanLine(agent, msg)
|
|
84
|
+
last.messages.push(msg)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* pushInject — B 段(一次性事件):进 `agent._pendingInject`,不进人读线、不进机读线。
|
|
89
|
+
* 条目形 = 消息 `{ role, content }`(`role` 缺省时按 `"user"` 的既有注入形态补上——三段载体里装的都是消息)。
|
|
90
|
+
* 回合收尾时由调用方清空队列;清的是投递副本,异步结果的账本 `_pendingAsyncResults` 一条不动。
|
|
91
|
+
*/
|
|
92
|
+
export function pushInject(agent, msg) {
|
|
93
|
+
if (!msg || typeof msg !== "object" || Array.isArray(msg)) {
|
|
94
|
+
throw new Error(`pushInject: a message object { role, content } is required — got ${JSON.stringify(msg)}`)
|
|
95
|
+
}
|
|
96
|
+
if (!Array.isArray(agent._pendingInject)) agent._pendingInject = []
|
|
97
|
+
agent._pendingInject.push(msg.role === undefined ? { role: "user", ...msg } : msg)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* setInject — A 段(当前状态):把第 `slot` 格写成 `{ role: "user", content, transient: true }`。
|
|
102
|
+
* 有旧的就地覆盖、没有就占格;`content` 为空(`null` ∥ `undefined` ∥ 空串)⇒ 该格写成 `null`(清掉旧值)。
|
|
103
|
+
* `slot` 必填、无默认、且在 `INJECT_SLOTS` 闭集内:默认一格就是替调用方决定它往哪儿写,写错名字则静默丢状态。
|
|
104
|
+
*/
|
|
105
|
+
export function setInject(agent, slot, content) {
|
|
106
|
+
const at = INJECT_SLOTS.indexOf(slot)
|
|
107
|
+
if (at < 0) {
|
|
108
|
+
throw new Error(`setInject: unknown slot ${JSON.stringify(slot)} — one of ${INJECT_SLOTS.join(" | ")} is required (no default)`)
|
|
109
|
+
}
|
|
110
|
+
if (!Array.isArray(agent._inject)) agent._inject = []
|
|
111
|
+
agent._inject[at] = typeof content === "string" && content.length > 0
|
|
112
|
+
? { role: "user", content, transient: true }
|
|
113
|
+
: null
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* setSessionHead — 会话头块的唯一写入口:只写 `agent._sessionHead` 这一个字段。
|
|
118
|
+
* 整段替换(不追加、不与旧值拼接)⇒ 同一份八项重算两次的结果逐字相同,字段上不会长成两份叠加。
|
|
119
|
+
* 它不进人读线、不进记录存储、不进会话存储,也不进 A 段格位——`read_history` 读到的因此仍是真实对话;
|
|
120
|
+
* 装配期由 `assembleRequestMessages` 贴到第一条真实用户消息的副本上(设计档「会话头块」)。
|
|
121
|
+
* `content` 必填、无默认:空串 = 这一趟没有头块,装配时原样返回、不贴。
|
|
122
|
+
*/
|
|
123
|
+
export function setSessionHead(agent, content) {
|
|
124
|
+
if (typeof content !== "string") {
|
|
125
|
+
throw new Error(`setSessionHead: a string is required (no default) — got ${JSON.stringify(content)}`)
|
|
126
|
+
}
|
|
127
|
+
if (!agent || typeof agent !== "object") {
|
|
128
|
+
throw new Error("setSessionHead: an agent object is required")
|
|
129
|
+
}
|
|
130
|
+
agent._sessionHead = content
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* retain — C 段(累积内容):按名字追加;同名替换那一条,不动别的条目。
|
|
135
|
+
* 条目形 = `{ name, message: { role: "user", content } }`(`assembleRequestMessages` 取 `message`)。
|
|
136
|
+
*/
|
|
137
|
+
export function retain(agent, name, content) {
|
|
138
|
+
if (typeof name !== "string" || !name) {
|
|
139
|
+
throw new Error(`retain: a non-empty entry name is required (no default) — got ${JSON.stringify(name)}`)
|
|
140
|
+
}
|
|
141
|
+
if (!Array.isArray(agent._retained)) agent._retained = []
|
|
142
|
+
const message = { role: "user", content: typeof content === "string" ? content : String(content ?? "") }
|
|
143
|
+
const at = agent._retained.findIndex((e) => e?.name === name)
|
|
144
|
+
if (at >= 0) agent._retained[at] = { name, message }
|
|
145
|
+
else agent._retained.push({ name, message })
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* pushRecord — 人读线留档记录单点(消化生命周期面 · #726;机器线零触)。
|
|
150
|
+
* `ts` 打点 ∥ 记录存储追加 ∥ 尾窗驱逐三面与 `pushReal` 同源(`appendHumanLine`);`agent.history` 一条不写。
|
|
151
|
+
* 未绑定(`_recordStore` 缺)⇒ 人读线追加照常 ∥ 存储分支空转(零抛——模式 F 零回归);
|
|
152
|
+
* 载体缺位(无活跃会话)⇒ 零动作(零抛——日志归端侧)。
|
|
45
153
|
*/
|
|
46
154
|
export function pushRecord(agent, record) {
|
|
47
|
-
if (!agent || typeof agent !== "object") return
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
155
|
+
if (!agent || typeof agent !== "object") return
|
|
156
|
+
appendHumanLine(agent, record)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* flattenHistory — 拍平的只读投影(单点)。外层按序,每个元素的 `messages` 按内层顺序展开。
|
|
161
|
+
* 一次性读点(收一个数组、当场用完)传它的结果;跨调用水位不用它(位置 = `{ turn, msg }`)。
|
|
162
|
+
* 非轮元素(不是 `{ messages: [...] }`)说明线上的形状坏了 —— 抛出并点名那一位,不静默跳过:
|
|
163
|
+
* 跳过会让「少了几条消息」与「本来就没有」在读数上同形。
|
|
164
|
+
*/
|
|
165
|
+
export function flattenHistory(history) {
|
|
166
|
+
const out = []
|
|
167
|
+
const list = Array.isArray(history) ? history : []
|
|
168
|
+
for (let i = 0; i < list.length; i++) {
|
|
169
|
+
const messages = list[i]?.messages
|
|
170
|
+
if (!Array.isArray(messages)) {
|
|
171
|
+
throw new Error(`flattenHistory: element ${i} is not a turn { messages: [...] } — the machine line must be nested by turn`)
|
|
172
|
+
}
|
|
173
|
+
for (const m of messages) out.push(m)
|
|
174
|
+
}
|
|
175
|
+
return out
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 外层轮元素的条数(拍平条数与轮数不是同一个量:数 `length` 数到的是轮数)。 */
|
|
179
|
+
function turnCount(history) {
|
|
180
|
+
return Array.isArray(history) ? history.length : 0
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function messagesOf(history, turn) {
|
|
184
|
+
const messages = Array.isArray(history) ? history[turn]?.messages : null
|
|
185
|
+
return Array.isArray(messages) ? messages : []
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* flatIndexOf — 位置 `{ turn, msg }` → 拍平下标(两向换算之一,另一向 = `positionOf`;两向同住一档)。
|
|
190
|
+
* 喂 `splitHistory` 的 `protectFrom`、比水位先后都用它。非法位置抛出并点名,不夹到边界。
|
|
191
|
+
*/
|
|
192
|
+
export function flatIndexOf(history, pos) {
|
|
193
|
+
if (!pos || typeof pos !== "object" || Array.isArray(pos)) {
|
|
194
|
+
throw new Error(`flatIndexOf: a position { turn, msg } is required (no default) — got ${JSON.stringify(pos)}`)
|
|
195
|
+
}
|
|
196
|
+
const { turn, msg } = pos
|
|
197
|
+
if (!Number.isInteger(turn) || turn < 0 || turn > turnCount(history)) {
|
|
198
|
+
throw new Error(`flatIndexOf: invalid turn ${JSON.stringify(turn)} (0..${turnCount(history)})`)
|
|
199
|
+
}
|
|
200
|
+
if (!Number.isInteger(msg) || msg < 0 || msg > messagesOf(history, turn).length) {
|
|
201
|
+
throw new Error(`flatIndexOf: invalid msg ${JSON.stringify(msg)} in turn ${turn} (0..${messagesOf(history, turn).length})`)
|
|
202
|
+
}
|
|
203
|
+
let n = 0
|
|
204
|
+
for (let i = 0; i < turn; i++) n += messagesOf(history, i).length
|
|
205
|
+
return n + msg
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* positionOf — 拍平下标 → 位置 `{ turn, msg }`(`applyCompression` 收的仍是两个整数下标,函数体开头换算一次)。
|
|
210
|
+
* 三档:`0` ⇒ `{ turn: 0, msg: 0 }`;等于拍平总条数 ⇒ 线尾 `{ turn: 外层轮元素数, msg: 0 }`;
|
|
211
|
+
* 小于 0 或大于总条数 ⇒ 抛出并点名该参数——夹一下就把越界算成合法切点。
|
|
212
|
+
*/
|
|
213
|
+
export function positionOf(history, flatIndex) {
|
|
214
|
+
const total = flatIndexOf(history, { turn: turnCount(history), msg: 0 })
|
|
215
|
+
if (!Number.isInteger(flatIndex) || flatIndex < 0 || flatIndex > total) {
|
|
216
|
+
throw new Error(`positionOf: invalid flat index ${JSON.stringify(flatIndex)} (0..${total})`)
|
|
217
|
+
}
|
|
218
|
+
if (flatIndex === 0) return { turn: 0, msg: 0 }
|
|
219
|
+
if (flatIndex === total) return { turn: turnCount(history), msg: 0 }
|
|
220
|
+
let remaining = flatIndex
|
|
221
|
+
for (let i = 0; i < turnCount(history); i++) {
|
|
222
|
+
const len = messagesOf(history, i).length
|
|
223
|
+
if (remaining < len) return { turn: i, msg: remaining }
|
|
224
|
+
remaining -= len
|
|
225
|
+
}
|
|
226
|
+
throw new Error(`positionOf: flat index ${flatIndex} is past the end (${total} messages)`)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 会话头块的锚点判据(档内私有,判据正文 = 设计档「会话头块」):这一条是不是「第一条真实用户消息」。
|
|
231
|
+
* `role === "user"`,且内容不是机器行——字符串形不以 `[System reminder:` 开头;分片数组形取第一个
|
|
232
|
+
* `type === "text"` 分片判同一前缀,一个 text 分片都没有的不合格。内容为空不参与判据(自动回合的用户
|
|
233
|
+
* 消息就是空串,它仍是这一轮的真实用户位置)。
|
|
234
|
+
* `existing but unusable:` 查过 `thincoder-core/history-window.mjs` 的 `isRealUserMsg`——它只认字符串内容,
|
|
235
|
+
* 分片数组形整条落空,直接复用会把那一类用户消息漏掉。
|
|
236
|
+
*/
|
|
237
|
+
function isSessionHeadAnchor(m) {
|
|
238
|
+
if (m?.role !== "user") return false
|
|
239
|
+
const content = m?.content
|
|
240
|
+
if (typeof content === "string") return !content.startsWith("[System reminder:")
|
|
241
|
+
if (Array.isArray(content)) {
|
|
242
|
+
const first = content.find((p) => p?.type === "text")
|
|
243
|
+
return typeof first?.text === "string" && !first.text.startsWith("[System reminder:")
|
|
244
|
+
}
|
|
245
|
+
return false
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 在一段既有 `content` 末尾接一段文本(档内私有;`text` 为空时原样返回)。内容两形各一贴法:
|
|
250
|
+
* 字符串 ⇒ 换行接一段;分片数组 ⇒ 末尾加一个 `{ type: "text", text }` 分片(不再拆合已有分片)。
|
|
251
|
+
* `none found:` 查过本档全部导出——没有一条「在一段既有 `content` 末尾接一段文本」的现成面。
|
|
252
|
+
*/
|
|
253
|
+
function appendBlock(content, text) {
|
|
254
|
+
if (!text) return content
|
|
255
|
+
if (Array.isArray(content)) return [...content, { type: "text", text }]
|
|
256
|
+
if (typeof content === "string") return content ? `${content}\n${text}` : text
|
|
257
|
+
return text
|
|
51
258
|
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* assembleRequestMessages — 请求消息的**唯一构造点**(system → C → 压缩摘要 → 对话(头块贴首条用户消息)→ A 其余 9 格并成一块 → B)。
|
|
262
|
+
* `toolsDeclared` 是实际请求是否携带工具声明的必填布尔值;缺参或非布尔在入口点名抛错。它参与完整历史缺回声退轮,
|
|
263
|
+
* 有正文的退轮保留,纯工具空轮退出本次请求。
|
|
264
|
+
* 次序的理由:稳定前缀 = `system` ∥ C ∥ 压缩摘要 ∥ 对话——C 只在装技能时变,压缩摘要只在压缩时变,对话只追加;
|
|
265
|
+
* A 其余的 9 格里有每回合都变的(`time`),B 每回合进出。块落在本轮用户消息上,因此变化的字节只落在当前
|
|
266
|
+
* 这一轮里,旧轮次不被改动,前缀缓存不因它们失效。
|
|
267
|
+
* `compaction-summary` 是这条前缀规则的唯一例外:它代表被摘要覆盖的那段最早历史,语义位置在对话开头,
|
|
268
|
+
* 排在对话之后就成了一条「比全部真实对话都新」的消息。例外只此一格,因此按格名摘这一个格,
|
|
269
|
+
* 不另立「哪几格该提前」的名单——闭集次序表仍是唯一顺序源。
|
|
270
|
+
* **注入不单独成消息**:A 段其余 9 格并成一块、会话头块自成一串,两份都贴到线里某条用户消息的**副本**上
|
|
271
|
+
* (线上那条逐字不动),因此一次请求里没有一条独立的注入消息(机制 = 设计档「注入贴在哪一条消息上」)。
|
|
272
|
+
* 角色不补:A 与 C 的条目已经是 `role: "user"` 的消息;不换角色,也不新造第二条 `system`。
|
|
273
|
+
* `stripLocalMessageFields`(`thincoder-core/escape.mjs`)在压平之后、格式分发之前剥本地字段——
|
|
274
|
+
* 这与「载体项不新造身份字段」是同一条:剥离表本批只增了 `originProvider` 一项(消息产出来源,判据面),
|
|
275
|
+
* 多一枚未登记字段就是严格 provider 的 400。
|
|
276
|
+
*/
|
|
277
|
+
export function assembleRequestMessages(agent, { systemPrompt, toolsDeclared } = {}) {
|
|
278
|
+
if (typeof toolsDeclared !== "boolean") {
|
|
279
|
+
throw new Error(`assembleRequestMessages: toolsDeclared 必填且必须是布尔值——实收 ${JSON.stringify(toolsDeclared)}`)
|
|
280
|
+
}
|
|
281
|
+
// 三个载体各自判「在不在」(`Array.isArray`)再逐格判「空不空」(`if (m)`):缺前者,`_inject` 为 `null`
|
|
282
|
+
// 时取下标与遍历当场抛;缺后者,没写过的格是 `null`,会照样进请求。
|
|
283
|
+
const summaryAt = INJECT_SLOTS.indexOf("compaction-summary")
|
|
284
|
+
const slots = Array.isArray(agent?._inject) ? agent._inject : []
|
|
285
|
+
const out = [{ role: "system", content: systemPrompt }]
|
|
286
|
+
for (const entry of Array.isArray(agent?._retained) ? agent._retained : []) {
|
|
287
|
+
if (entry?.message) out.push(entry.message)
|
|
288
|
+
}
|
|
289
|
+
// 压缩摘要:会话历史流的头部(`system` 与累积段之后、对话第一条消息之前)
|
|
290
|
+
if (slots[summaryAt]) out.push(slots[summaryAt])
|
|
291
|
+
// 拍平一次:下面三处都读这一份(每条对话消息因此恰出现一次——非锚点原样进、锚点进它的副本)。
|
|
292
|
+
// 跨 provider 历史来源隔离(设计 `AGENT-LOOP.md` §5.5.4 读点、§5.5.5 隔离表):压平之后、注入贴块之前隔离异 provider
|
|
293
|
+
// 回声,并按本次思考模式与工具声明退掉完整历史里的缺回声工具轮。空轮退出后两个锚点在退场后的线里重找。
|
|
294
|
+
const flat = flattenHistory(agent?.history)
|
|
295
|
+
const spec = specForModel(agent.provider?.model)
|
|
296
|
+
const { messages: line, reading } = quarantineForeignOrigin(flat, {
|
|
297
|
+
providerName: agent.provider?.name,
|
|
298
|
+
spec,
|
|
299
|
+
thinkingMode: isThinkingRequest(agent.provider ?? null, spec),
|
|
300
|
+
toolsDeclared,
|
|
301
|
+
})
|
|
302
|
+
logOriginQuarantine({ site: "turn-request", providerName: agent.provider?.name, reading })
|
|
303
|
+
// A 其余 9 格并成一块(格序即 `INJECT_SLOTS` 的次序)。
|
|
304
|
+
const turnBlock = slots.filter((m, i) => i !== summaryAt && m).map((m) => m.content).join("\n")
|
|
305
|
+
// 两个锚点:头块贴第一条真实用户消息(判据见 `isSessionHeadAnchor`),每轮块贴最后一条用户消息。
|
|
306
|
+
const headAt = line.findIndex(isSessionHeadAnchor)
|
|
307
|
+
const turnAt = line.findLastIndex((m) => m?.role === "user")
|
|
308
|
+
for (let i = 0; i < line.length; i += 1) {
|
|
309
|
+
const m = line[i]
|
|
310
|
+
if (i !== headAt && i !== turnAt) { out.push(m); continue }
|
|
311
|
+
// 两个锚点落在同一条上(线里只有一条合格用户消息)时:副本先接头块、再接每轮块。
|
|
312
|
+
out.push({
|
|
313
|
+
...m,
|
|
314
|
+
content: appendBlock(appendBlock(m.content, i === headAt ? agent?._sessionHead : ""), i === turnAt ? turnBlock : ""),
|
|
315
|
+
})
|
|
316
|
+
}
|
|
317
|
+
for (const m of Array.isArray(agent?._pendingInject) ? agent._pendingInject : []) out.push(m)
|
|
318
|
+
return out
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* 槽里的机读线两形态归一(设计档「槽与迁移」)——**两条装回路径共用的唯一实现点**
|
|
322
|
+
* (核 `session-lifecycle.mjs` 的 `applySession` 与端侧 `thincoder-vscode/src/agent/setup.mjs` 都调它):
|
|
323
|
+
* - 已是嵌套轮(某个元素带 `messages` 数组)⇒ 逐元素归一,白名单只有 `messages`——**按原样装回,不筛不丢**;
|
|
324
|
+
* - 扁平消息数组(本批之前的槽)⇒ 按消息上的 `turn` 分组重建轮元素,随后删掉该字段;
|
|
325
|
+
* 没有 `turn` 的那批旧注入跟前面那一轮同轮、原样留下(不筛、不丢弃、不重建)。
|
|
326
|
+
* 空 ∥ 非数组 ⇒ 空数组(「槽里没有机读线」与「空机读线」在调用方同形)。
|
|
327
|
+
* 为什么必须有这一步:装配面(`flattenHistory`)只认 `{ messages }` 轮元素,扁平数组送进去当场抛。
|
|
328
|
+
*/
|
|
329
|
+
export function normalizeMachineLine(raw) {
|
|
330
|
+
const list = Array.isArray(raw) ? raw : []
|
|
331
|
+
if (list.some((entry) => Array.isArray(entry?.messages))) {
|
|
332
|
+
return list.map((turn) => ({ messages: Array.isArray(turn?.messages) ? turn.messages : [] }))
|
|
333
|
+
}
|
|
334
|
+
const turns = []
|
|
335
|
+
let current = null
|
|
336
|
+
for (const m of list) {
|
|
337
|
+
if (!m || typeof m !== "object") continue
|
|
338
|
+
const label = Number.isInteger(m.turn) ? m.turn : null
|
|
339
|
+
if (!current || (label !== null && label !== current.label)) {
|
|
340
|
+
current = { label: label ?? turns.length, messages: [] }
|
|
341
|
+
turns.push(current)
|
|
342
|
+
}
|
|
343
|
+
const copy = { ...m }
|
|
344
|
+
delete copy.turn
|
|
345
|
+
current.messages.push(copy)
|
|
346
|
+
}
|
|
347
|
+
return turns.map((turn) => ({ messages: turn.messages }))
|
|
348
|
+
}
|