vessel-agent 0.0.0-stage → 2.3.0-beta.2
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/LICENSE +21 -0
- package/README.md +466 -2
- package/README.zh-CN.md +476 -0
- package/apps/web/dist/Vela.png +0 -0
- package/apps/web/dist/Vessel.svg +24 -0
- package/apps/web/dist/apple-touch-icon.png +0 -0
- package/apps/web/dist/assets/ConfigView-BKdxb1s5.js +1 -0
- package/apps/web/dist/assets/DesignLabContent-CY5HfVn_.js +4 -0
- package/apps/web/dist/assets/DesignLabView-DTBWv5-E.js +1 -0
- package/apps/web/dist/assets/action-menu.preview-CpIL3tay.js +1 -0
- package/apps/web/dist/assets/agent-message.preview-CSYnJxgr.js +1 -0
- package/apps/web/dist/assets/assistant-text.preview-Bc2Is6sW.js +2 -0
- package/apps/web/dist/assets/badges.preview-DP0oPcs5.js +1 -0
- package/apps/web/dist/assets/buttons.preview-Y84aT4k3.js +1 -0
- package/apps/web/dist/assets/card-shell.preview-mwRwhVb8.js +1 -0
- package/apps/web/dist/assets/chip-tones.preview-dQ_6pwft.js +1 -0
- package/apps/web/dist/assets/composer-controls.preview-CBtJVrMo.js +1 -0
- package/apps/web/dist/assets/confirm-dialog.preview-BkNEtLld.js +1 -0
- package/apps/web/dist/assets/data-table.preview-DPZ7Pnn8.js +1 -0
- package/apps/web/dist/assets/depth-color.preview-CtCbAgCH.js +1 -0
- package/apps/web/dist/assets/entity-header.preview-BxPLNUSH.js +1 -0
- package/apps/web/dist/assets/env-card.preview-Bv4UlMjr.js +1 -0
- package/apps/web/dist/assets/failure-card.preview-DD3-RMju.js +1 -0
- package/apps/web/dist/assets/filter-popover.preview-D7Q7Kws8.js +1 -0
- package/apps/web/dist/assets/form.preview-D_Qltx-j.js +1 -0
- package/apps/web/dist/assets/index-ee36IO2F.js +600 -0
- package/apps/web/dist/assets/index-uU9N5oTa.css +1 -0
- package/apps/web/dist/assets/key-value.preview-B0gzxwtK.js +1 -0
- package/apps/web/dist/assets/mention-popover.preview-Duza1RgL.js +1 -0
- package/apps/web/dist/assets/message-fold.preview-Cx-CD3Y4.js +1 -0
- package/apps/web/dist/assets/modal-shell.preview-DYKwUk2S.js +1 -0
- package/apps/web/dist/assets/nav-item.preview-FTbsBQ2g.js +1 -0
- package/apps/web/dist/assets/nav-status-legend.preview-bS4LDFbd.js +1 -0
- package/apps/web/dist/assets/page-scaffold.preview-bzBFkjZ6.js +1 -0
- package/apps/web/dist/assets/plugin-ui-BxTydP3l.js +1 -0
- package/apps/web/dist/assets/projection-card.preview-BV0P-y8e.js +6 -0
- package/apps/web/dist/assets/queued.preview-zQqFe4Zr.js +1 -0
- package/apps/web/dist/assets/react-vendor-CUu194FT.js +9 -0
- package/apps/web/dist/assets/reasoning.preview-DtHXgW_8.js +2 -0
- package/apps/web/dist/assets/section.preview-A-PRm-rh.js +1 -0
- package/apps/web/dist/assets/sidebar-shell.preview-DFKMwNoo.js +1 -0
- package/apps/web/dist/assets/stat.preview-BzChSFLL.js +1 -0
- package/apps/web/dist/assets/states.preview-CyXZWnZ-.js +1 -0
- package/apps/web/dist/assets/status-icons.preview-DHdWwUis.js +1 -0
- package/apps/web/dist/assets/tabs.preview-CHbK1Two.js +1 -0
- package/apps/web/dist/assets/tool-card.preview-Z48fekTD.js +2 -0
- package/apps/web/dist/assets/toolbar.preview-Bl6hLYXX.js +1 -0
- package/apps/web/dist/assets/tooltip.preview-GKGTGfMP.js +1 -0
- package/apps/web/dist/assets/user-bubble.preview-CHYvio9z.js +1 -0
- package/apps/web/dist/assets/vendor/jsx-runtime.mjs +7 -0
- package/apps/web/dist/assets/vendor/plugin-sdk.mjs +12 -0
- package/apps/web/dist/assets/vendor/react-dom-client.mjs +6 -0
- package/apps/web/dist/assets/vendor/react-dom.mjs +11 -0
- package/apps/web/dist/assets/vendor/react.mjs +39 -0
- package/apps/web/dist/build.json +5 -0
- package/apps/web/dist/favicon.ico +0 -0
- package/apps/web/dist/index.html +24 -0
- package/package.json +70 -4
- package/packages/agent/package.json +32 -0
- package/packages/agent/src/address-display.ts +141 -0
- package/packages/agent/src/conversation-budget.ts +651 -0
- package/packages/agent/src/conversation-store.ts +351 -0
- package/packages/agent/src/distress.ts +161 -0
- package/packages/agent/src/env-info-scope.ts +153 -0
- package/packages/agent/src/error-text.ts +28 -0
- package/packages/agent/src/inbox-source.ts +73 -0
- package/packages/agent/src/inbox.ts +2335 -0
- package/packages/agent/src/index.ts +273 -0
- package/packages/agent/src/member-cwd.ts +128 -0
- package/packages/agent/src/member-ports.ts +162 -0
- package/packages/agent/src/refs.ts +51 -0
- package/packages/agent/src/registry.ts +741 -0
- package/packages/agent/src/runtime.ts +2501 -0
- package/packages/agent/src/self-card-delta.ts +428 -0
- package/packages/agent/src/service.ts +9212 -0
- package/packages/agent/src/snapshot.ts +614 -0
- package/packages/agent/src/team-address.ts +96 -0
- package/packages/agent/src/team-log.ts +487 -0
- package/packages/agent/src/team-service.ts +1966 -0
- package/packages/agent/src/team-slug.ts +167 -0
- package/packages/agent/src/team-store.ts +619 -0
- package/packages/agent/src/tool.ts +1306 -0
- package/packages/agent/src/types/budget.ts +168 -0
- package/packages/agent/src/types/constants.ts +17 -0
- package/packages/agent/src/types/dispatch.ts +133 -0
- package/packages/agent/src/types/events.ts +132 -0
- package/packages/agent/src/types/execution.ts +289 -0
- package/packages/agent/src/types/lifecycle.ts +372 -0
- package/packages/agent/src/types/member.ts +221 -0
- package/packages/agent/src/types/message.ts +113 -0
- package/packages/agent/src/types/results.ts +64 -0
- package/packages/agent/src/types/roster.ts +137 -0
- package/packages/agent/src/types/service.ts +702 -0
- package/packages/agent/src/types/session.ts +102 -0
- package/packages/agent/src/types/team.ts +558 -0
- package/packages/agent/src/types.ts +22 -0
- package/packages/agent/tsconfig.json +8 -0
- package/packages/config/package.json +32 -0
- package/packages/config/src/auth-origins.ts +85 -0
- package/packages/config/src/auth-strategies/avatar-ref.ts +40 -0
- package/packages/config/src/auth-strategies/gitee.ts +72 -0
- package/packages/config/src/auth-strategies/github.ts +64 -0
- package/packages/config/src/auth-strategies/index.ts +47 -0
- package/packages/config/src/auth-strategies/match.ts +82 -0
- package/packages/config/src/auth-strategies/microsoft.ts +100 -0
- package/packages/config/src/auth-strategies/oauth-exchange.ts +192 -0
- package/packages/config/src/auth-strategies/password.ts +34 -0
- package/packages/config/src/auth-strategies/registry.ts +105 -0
- package/packages/config/src/auth-strategies/shapes.ts +138 -0
- package/packages/config/src/auth-strategies/types.ts +190 -0
- package/packages/config/src/auth.ts +305 -0
- package/packages/config/src/config-lock.ts +328 -0
- package/packages/config/src/defaults.ts +271 -0
- package/packages/config/src/env-file.ts +175 -0
- package/packages/config/src/env-injection.ts +117 -0
- package/packages/config/src/explain.ts +283 -0
- package/packages/config/src/gateway.ts +107 -0
- package/packages/config/src/index.ts +253 -0
- package/packages/config/src/loader.ts +871 -0
- package/packages/config/src/model-migration.ts +534 -0
- package/packages/config/src/model-registry.ts +374 -0
- package/packages/config/src/models-disk.ts +72 -0
- package/packages/config/src/models-validation.ts +467 -0
- package/packages/config/src/node-registration.ts +583 -0
- package/packages/config/src/root-secret.ts +402 -0
- package/packages/config/src/secrets.ts +452 -0
- package/packages/config/src/sources.ts +82 -0
- package/packages/config/src/team-policy.ts +422 -0
- package/packages/config/src/types/agent.ts +263 -0
- package/packages/config/src/types/attention.ts +28 -0
- package/packages/config/src/types/auth.ts +126 -0
- package/packages/config/src/types/connect.ts +21 -0
- package/packages/config/src/types/hub.ts +59 -0
- package/packages/config/src/types/models.ts +153 -0
- package/packages/config/src/types/node.ts +202 -0
- package/packages/config/src/types/observability.ts +84 -0
- package/packages/config/src/types/root.ts +240 -0
- package/packages/config/src/types/runtime.ts +123 -0
- package/packages/config/src/types/security.ts +43 -0
- package/packages/config/src/types/serve.ts +34 -0
- package/packages/config/src/types/snapshot.ts +28 -0
- package/packages/config/src/types/team.ts +112 -0
- package/packages/config/src/types/ui.ts +179 -0
- package/packages/config/src/types.ts +22 -0
- package/packages/config/src/user-config.ts +175 -0
- package/packages/config/src/utils.ts +55 -0
- package/packages/config/src/validator/auth.ts +302 -0
- package/packages/config/src/validator/hub.ts +329 -0
- package/packages/config/src/validator/models.ts +1304 -0
- package/packages/config/src/validator/serve.ts +784 -0
- package/packages/config/src/validator/settings.ts +244 -0
- package/packages/config/src/validator/shared.ts +304 -0
- package/packages/config/src/validator/tenant-keys.ts +91 -0
- package/packages/config/src/validator/unknown-keys.ts +452 -0
- package/packages/config/src/validator.ts +1146 -0
- package/packages/config/tsconfig.json +8 -0
- package/packages/core/package.json +38 -0
- package/packages/core/src/context/anchor.ts +57 -0
- package/packages/core/src/context/archive-notice.ts +283 -0
- package/packages/core/src/context/cleared-tool-result.ts +262 -0
- package/packages/core/src/context/compaction-index.ts +381 -0
- package/packages/core/src/context/composition.ts +251 -0
- package/packages/core/src/context/context-manager.ts +1585 -0
- package/packages/core/src/context/context-notice.ts +218 -0
- package/packages/core/src/context/index.ts +220 -0
- package/packages/core/src/context/model-change-notice.ts +81 -0
- package/packages/core/src/context/projection-policy.ts +719 -0
- package/packages/core/src/context/prompt-context.ts +66 -0
- package/packages/core/src/context/read-side-mask.ts +63 -0
- package/packages/core/src/context/request-sequence.ts +423 -0
- package/packages/core/src/context/request-view.ts +256 -0
- package/packages/core/src/context/session-breaker.ts +100 -0
- package/packages/core/src/context/tool-result-whitelist.ts +135 -0
- package/packages/core/src/events/event-stream.ts +271 -0
- package/packages/core/src/events/file-event-store.ts +119 -0
- package/packages/core/src/events/index.ts +15 -0
- package/packages/core/src/events/replay.ts +82 -0
- package/packages/core/src/events/summarize-event.ts +135 -0
- package/packages/core/src/index.ts +475 -0
- package/packages/core/src/kernel/cancellation.ts +54 -0
- package/packages/core/src/kernel/event-bus.ts +65 -0
- package/packages/core/src/kernel/index.ts +32 -0
- package/packages/core/src/kernel/internal.ts +32 -0
- package/packages/core/src/kernel/scheduler.ts +406 -0
- package/packages/core/src/kernel/types.ts +269 -0
- package/packages/core/src/limits/index.ts +23 -0
- package/packages/core/src/limits/limit-checker.ts +270 -0
- package/packages/core/src/limits/limit-error.ts +29 -0
- package/packages/core/src/log-format.ts +722 -0
- package/packages/core/src/log-hub.ts +278 -0
- package/packages/core/src/logging.ts +64 -0
- package/packages/core/src/observability/index.ts +24 -0
- package/packages/core/src/observability/metrics.ts +279 -0
- package/packages/core/src/observability/read-trace.ts +163 -0
- package/packages/core/src/observability/rebuild-metrics.ts +37 -0
- package/packages/core/src/observability/trace-prune.ts +248 -0
- package/packages/core/src/observability/trace-recorder.ts +484 -0
- package/packages/core/src/observability/trace-redactor.ts +93 -0
- package/packages/core/src/pii/index.ts +88 -0
- package/packages/core/src/pii/notice.ts +156 -0
- package/packages/core/src/pii/reveal.ts +198 -0
- package/packages/core/src/pii/rules.ts +545 -0
- package/packages/core/src/pii/types.ts +140 -0
- package/packages/core/src/pii/vault.ts +764 -0
- package/packages/core/src/provider/errors.ts +41 -0
- package/packages/core/src/provider/index.ts +19 -0
- package/packages/core/src/provider/providers.ts +1011 -0
- package/packages/core/src/provider/request-extras.ts +285 -0
- package/packages/core/src/provider/stream-accumulator.ts +168 -0
- package/packages/core/src/provider/usage.ts +112 -0
- package/packages/core/src/runtime/error-envelope.ts +451 -0
- package/packages/core/src/runtime/error-summary.ts +174 -0
- package/packages/core/src/runtime/execution-unit.ts +6675 -0
- package/packages/core/src/runtime/guardrail-runner.ts +86 -0
- package/packages/core/src/runtime/hook-runner.ts +137 -0
- package/packages/core/src/runtime/index.ts +123 -0
- package/packages/core/src/runtime/permission-policy.ts +267 -0
- package/packages/core/src/runtime/plugin-host.ts +489 -0
- package/packages/core/src/runtime/plugin-install.ts +144 -0
- package/packages/core/src/runtime/policy/index.ts +21 -0
- package/packages/core/src/runtime/prompt-prefix-guard.ts +166 -0
- package/packages/core/src/runtime/provider-retry.ts +220 -0
- package/packages/core/src/runtime/resume-markers.ts +114 -0
- package/packages/core/src/runtime/resume-recovery.ts +164 -0
- package/packages/core/src/runtime/run-journal.ts +382 -0
- package/packages/core/src/runtime/run-timing.ts +217 -0
- package/packages/core/src/runtime/runtime.ts +349 -0
- package/packages/core/src/runtime/safepoint-pause.ts +29 -0
- package/packages/core/src/runtime/tool-attachment.ts +170 -0
- package/packages/core/src/runtime/tool-calling-loop.ts +1219 -0
- package/packages/core/src/runtime/tool-name.ts +215 -0
- package/packages/core/src/runtime/tool-pipeline.ts +459 -0
- package/packages/core/src/runtime/tool-provenance.ts +48 -0
- package/packages/core/src/runtime/tool-result-gate.ts +329 -0
- package/packages/core/src/session/atomic-write.ts +335 -0
- package/packages/core/src/session/compaction-index-derive.ts +173 -0
- package/packages/core/src/session/compaction-index-store.ts +161 -0
- package/packages/core/src/session/compaction-recall.ts +375 -0
- package/packages/core/src/session/delete-session.ts +198 -0
- package/packages/core/src/session/file-backend.ts +1311 -0
- package/packages/core/src/session/index.ts +118 -0
- package/packages/core/src/session/message-identity.ts +272 -0
- package/packages/core/src/session/project-messages.ts +908 -0
- package/packages/core/src/session/projection-cache.ts +254 -0
- package/packages/core/src/session/prune.ts +153 -0
- package/packages/core/src/session/run-record-store.ts +307 -0
- package/packages/core/src/session/session-index.ts +158 -0
- package/packages/core/src/session/session-log-dir.ts +39 -0
- package/packages/core/src/session/session-store.ts +266 -0
- package/packages/core/src/session/sqlite-backend.ts +391 -0
- package/packages/core/src/socket-inherit-guard.ts +188 -0
- package/packages/core/src/storage/metadata-sidecar.ts +124 -0
- package/packages/core/src/tenant/index.ts +13 -0
- package/packages/core/src/tenant/resolve-tenant.ts +19 -0
- package/packages/core/src/tenant/tenant-paths.ts +83 -0
- package/packages/core/src/timeline/archive-audit.ts +316 -0
- package/packages/core/src/timeline/archive-lock.ts +124 -0
- package/packages/core/src/timeline/blocks.ts +1639 -0
- package/packages/core/src/timeline/enum-stats.ts +34 -0
- package/packages/core/src/timeline/holes.ts +431 -0
- package/packages/core/src/timeline/index.ts +387 -0
- package/packages/core/src/timeline/legacy-format.ts +97 -0
- package/packages/core/src/timeline/read-port.ts +582 -0
- package/packages/core/src/timeline/types.ts +371 -0
- package/packages/core/src/timeline/wal.ts +2485 -0
- package/packages/core/src/timeline/writer.ts +2079 -0
- package/packages/core/src/timeout.ts +93 -0
- package/packages/core/src/transport/in-process.ts +86 -0
- package/packages/core/src/transport/index.ts +6 -0
- package/packages/core/src/types/context.ts +229 -0
- package/packages/core/src/types/env-cadence.ts +35 -0
- package/packages/core/src/types/error-registry.ts +343 -0
- package/packages/core/src/types/error.ts +107 -0
- package/packages/core/src/types/event.ts +496 -0
- package/packages/core/src/types/guardrail.ts +35 -0
- package/packages/core/src/types/hook.ts +122 -0
- package/packages/core/src/types/index.ts +28 -0
- package/packages/core/src/types/lease.ts +91 -0
- package/packages/core/src/types/limits.ts +112 -0
- package/packages/core/src/types/logging.ts +54 -0
- package/packages/core/src/types/member-port.ts +194 -0
- package/packages/core/src/types/plugin.ts +392 -0
- package/packages/core/src/types/projection-cache.ts +80 -0
- package/packages/core/src/types/projection.ts +100 -0
- package/packages/core/src/types/provider.ts +319 -0
- package/packages/core/src/types/run-journal.ts +178 -0
- package/packages/core/src/types/run-record-store.ts +128 -0
- package/packages/core/src/types/run.ts +140 -0
- package/packages/core/src/types/session.ts +328 -0
- package/packages/core/src/types/source-module.ts +68 -0
- package/packages/core/src/types/tenant.ts +41 -0
- package/packages/core/src/types/tool-name.ts +57 -0
- package/packages/core/src/types/tool.ts +122 -0
- package/packages/core/src/types/trace.ts +38 -0
- package/packages/core/src/types/vision.ts +219 -0
- package/packages/core/src/vision/blobs.ts +328 -0
- package/packages/core/src/vision/index.ts +73 -0
- package/packages/core/src/vision/ingest.ts +126 -0
- package/packages/core/src/vision/notice.ts +58 -0
- package/packages/core/src/vision/plan.ts +358 -0
- package/packages/core/src/vision/staging.ts +81 -0
- package/packages/core/tsconfig.json +8 -0
- package/packages/permission/package.json +31 -0
- package/packages/permission/src/index.ts +60 -0
- package/packages/plugin-sdk/package.json +21 -0
- package/packages/plugin-sdk/src/index.ts +478 -0
- package/packages/plugin-sdk/src/semver.ts +154 -0
- package/packages/plugin-ui/package.json +33 -0
- package/packages/plugin-ui/src/Badge.tsx +41 -0
- package/packages/plugin-ui/src/Button.tsx +84 -0
- package/packages/plugin-ui/src/ComposerAttachments.tsx +87 -0
- package/packages/plugin-ui/src/ComposerContextMeter.tsx +209 -0
- package/packages/plugin-ui/src/ComposerToolbar.tsx +233 -0
- package/packages/plugin-ui/src/CountBadge.tsx +57 -0
- package/packages/plugin-ui/src/DataTable.tsx +168 -0
- package/packages/plugin-ui/src/Field.tsx +543 -0
- package/packages/plugin-ui/src/KeyValueList.tsx +32 -0
- package/packages/plugin-ui/src/Page.tsx +39 -0
- package/packages/plugin-ui/src/Pager.tsx +63 -0
- package/packages/plugin-ui/src/SearchInput.tsx +33 -0
- package/packages/plugin-ui/src/Section.tsx +47 -0
- package/packages/plugin-ui/src/Stat.tsx +25 -0
- package/packages/plugin-ui/src/TableToolbar.tsx +27 -0
- package/packages/plugin-ui/src/Tabs.tsx +77 -0
- package/packages/plugin-ui/src/card-surface.ts +11 -0
- package/packages/plugin-ui/src/cn.ts +7 -0
- package/packages/plugin-ui/src/dom-globals.d.ts +124 -0
- package/packages/plugin-ui/src/host.ts +270 -0
- package/packages/plugin-ui/src/index.ts +26 -0
- package/packages/plugin-ui/src/styles.css +386 -0
- package/packages/plugin-ui/src/useStaleWhileRevalidate.ts +211 -0
- package/packages/serve/package.json +32 -0
- package/packages/serve/src/agent-control.ts +595 -0
- package/packages/serve/src/agents-registry.ts +409 -0
- package/packages/serve/src/auth/avatar.ts +352 -0
- package/packages/serve/src/auth/bootstrap.ts +184 -0
- package/packages/serve/src/auth/credentials.ts +275 -0
- package/packages/serve/src/auth/github-oauth.ts +33 -0
- package/packages/serve/src/auth/index.ts +121 -0
- package/packages/serve/src/auth/oauth.ts +394 -0
- package/packages/serve/src/auth/origins.ts +145 -0
- package/packages/serve/src/auth/password.ts +187 -0
- package/packages/serve/src/auth/ratelimit.ts +249 -0
- package/packages/serve/src/auth/routes.ts +1757 -0
- package/packages/serve/src/auth/session.ts +407 -0
- package/packages/serve/src/auth/totp.ts +183 -0
- package/packages/serve/src/backpressure.ts +91 -0
- package/packages/serve/src/durable.ts +216 -0
- package/packages/serve/src/evals.ts +140 -0
- package/packages/serve/src/event-buffer.ts +92 -0
- package/packages/serve/src/event-summary.ts +11 -0
- package/packages/serve/src/federation.ts +1100 -0
- package/packages/serve/src/feishu.ts +51 -0
- package/packages/serve/src/gateway/auth.ts +773 -0
- package/packages/serve/src/gateway/errors.ts +21 -0
- package/packages/serve/src/gateway/frames.ts +195 -0
- package/packages/serve/src/gateway/methods/admin.ts +530 -0
- package/packages/serve/src/gateway/methods/agents.ts +1727 -0
- package/packages/serve/src/gateway/methods/attachments.ts +354 -0
- package/packages/serve/src/gateway/methods/common.ts +50 -0
- package/packages/serve/src/gateway/methods/config.ts +883 -0
- package/packages/serve/src/gateway/methods/evals.ts +87 -0
- package/packages/serve/src/gateway/methods/feishu.ts +52 -0
- package/packages/serve/src/gateway/methods/fleet.ts +225 -0
- package/packages/serve/src/gateway/methods/images.ts +196 -0
- package/packages/serve/src/gateway/methods/interactions.ts +721 -0
- package/packages/serve/src/gateway/methods/jobs.ts +148 -0
- package/packages/serve/src/gateway/methods/kanban.ts +637 -0
- package/packages/serve/src/gateway/methods/mcp.ts +82 -0
- package/packages/serve/src/gateway/methods/observability.ts +264 -0
- package/packages/serve/src/gateway/methods/permissions.ts +397 -0
- package/packages/serve/src/gateway/methods/rpc-namespaces.ts +80 -0
- package/packages/serve/src/gateway/methods/runs.ts +434 -0
- package/packages/serve/src/gateway/methods/schemas.ts +1196 -0
- package/packages/serve/src/gateway/methods/sessions.ts +2568 -0
- package/packages/serve/src/gateway/methods/subscription.ts +515 -0
- package/packages/serve/src/gateway/methods/teams.ts +638 -0
- package/packages/serve/src/gateway/sse.ts +130 -0
- package/packages/serve/src/gateway/static.ts +66 -0
- package/packages/serve/src/gateway/timeline-protocol.ts +532 -0
- package/packages/serve/src/gateway.ts +4974 -0
- package/packages/serve/src/hub-control.ts +86 -0
- package/packages/serve/src/hub-frames.ts +435 -0
- package/packages/serve/src/hub-protocol.ts +182 -0
- package/packages/serve/src/index.ts +511 -0
- package/packages/serve/src/interactions-registry.ts +1362 -0
- package/packages/serve/src/interactions-store.ts +360 -0
- package/packages/serve/src/local.ts +48 -0
- package/packages/serve/src/log-event.ts +57 -0
- package/packages/serve/src/mcp.ts +73 -0
- package/packages/serve/src/metrics.ts +137 -0
- package/packages/serve/src/platform/auth.ts +146 -0
- package/packages/serve/src/platform/index.ts +54 -0
- package/packages/serve/src/platform/listen-guard.ts +68 -0
- package/packages/serve/src/platform/metrics.ts +123 -0
- package/packages/serve/src/platform/proxy-config.ts +368 -0
- package/packages/serve/src/platform/proxy-ws.ts +737 -0
- package/packages/serve/src/platform/proxy.ts +909 -0
- package/packages/serve/src/platform/registry.ts +130 -0
- package/packages/serve/src/platform/rpc.ts +445 -0
- package/packages/serve/src/platform/server.ts +1148 -0
- package/packages/serve/src/platform/types.ts +276 -0
- package/packages/serve/src/plugin-host/catalog.ts +212 -0
- package/packages/serve/src/plugin-host/child.ts +767 -0
- package/packages/serve/src/plugin-host/dto.ts +191 -0
- package/packages/serve/src/plugin-host/errors.ts +50 -0
- package/packages/serve/src/plugin-host/events.ts +114 -0
- package/packages/serve/src/plugin-host/expose.ts +142 -0
- package/packages/serve/src/plugin-host/http.ts +823 -0
- package/packages/serve/src/plugin-host/index.ts +170 -0
- package/packages/serve/src/plugin-host/manager.ts +2027 -0
- package/packages/serve/src/plugin-host/ndjson.ts +136 -0
- package/packages/serve/src/plugin-host/proxy.ts +358 -0
- package/packages/serve/src/plugin-host/registry.ts +208 -0
- package/packages/serve/src/plugin-host/static.ts +162 -0
- package/packages/serve/src/plugin-host/types.ts +529 -0
- package/packages/serve/src/plugin-host/ws-proxy.ts +431 -0
- package/packages/serve/src/prometheus.ts +98 -0
- package/packages/serve/src/protocol.ts +332 -0
- package/packages/serve/src/replay-budget.ts +481 -0
- package/packages/serve/src/rpc.ts +614 -0
- package/packages/serve/src/run-control.ts +106 -0
- package/packages/serve/src/session-search.ts +142 -0
- package/packages/serve/src/shutdown-step.ts +128 -0
- package/packages/serve/src/team-control.ts +282 -0
- package/packages/serve/src/tenant-event-hub.ts +194 -0
- package/packages/serve/src/tenant.ts +169 -0
- package/packages/serve/src/tool-events.ts +244 -0
- package/packages/tui/package.json +40 -0
- package/packages/tui/src/commands/agents-command.ts +402 -0
- package/packages/tui/src/commands/capabilities-command.ts +356 -0
- package/packages/tui/src/commands/commands.ts +1361 -0
- package/packages/tui/src/commands/config-command.ts +541 -0
- package/packages/tui/src/commands/context-command.ts +348 -0
- package/packages/tui/src/commands/cost-command.ts +66 -0
- package/packages/tui/src/commands/details-command.ts +47 -0
- package/packages/tui/src/commands/errors-command.ts +205 -0
- package/packages/tui/src/commands/export-command.ts +193 -0
- package/packages/tui/src/commands/feishu-command.ts +101 -0
- package/packages/tui/src/commands/mcp-command.ts +319 -0
- package/packages/tui/src/commands/metrics-command.ts +314 -0
- package/packages/tui/src/commands/permissions-command.ts +117 -0
- package/packages/tui/src/commands/runs-command.ts +421 -0
- package/packages/tui/src/commands/sessions-command.ts +367 -0
- package/packages/tui/src/commands/settings-command.ts +180 -0
- package/packages/tui/src/commands/status-command.ts +30 -0
- package/packages/tui/src/commands/trace-command.ts +382 -0
- package/packages/tui/src/components/AskUserDialog.tsx +663 -0
- package/packages/tui/src/components/BottomBar.tsx +44 -0
- package/packages/tui/src/components/ConfirmDialog.tsx +175 -0
- package/packages/tui/src/components/DataTable.tsx +99 -0
- package/packages/tui/src/components/DetailPanel.tsx +80 -0
- package/packages/tui/src/components/EmptyState.tsx +27 -0
- package/packages/tui/src/components/InlineAutocomplete.tsx +319 -0
- package/packages/tui/src/components/InteractiveViewHost.tsx +652 -0
- package/packages/tui/src/components/KeyHints.tsx +41 -0
- package/packages/tui/src/components/Markdown.tsx +489 -0
- package/packages/tui/src/components/PanelFrame.tsx +113 -0
- package/packages/tui/src/components/QueuePreview.tsx +113 -0
- package/packages/tui/src/components/QuickHelp.tsx +91 -0
- package/packages/tui/src/components/ReasoningBlock.tsx +42 -0
- package/packages/tui/src/components/SessionTable.tsx +523 -0
- package/packages/tui/src/components/SpinnerWithVerb.tsx +116 -0
- package/packages/tui/src/components/StateTracker.ts +102 -0
- package/packages/tui/src/components/StatusBar.tsx +315 -0
- package/packages/tui/src/components/StreamOutput.tsx +302 -0
- package/packages/tui/src/components/SubagentActivity.tsx +57 -0
- package/packages/tui/src/components/TextViewer.tsx +100 -0
- package/packages/tui/src/components/ToolDisplay.tsx +248 -0
- package/packages/tui/src/components/ToolTrail.tsx +104 -0
- package/packages/tui/src/components/UiItemView.tsx +460 -0
- package/packages/tui/src/components/UpdateBanner.tsx +48 -0
- package/packages/tui/src/components/indicator.ts +61 -0
- package/packages/tui/src/error-classifier.ts +255 -0
- package/packages/tui/src/i18n/index.ts +67 -0
- package/packages/tui/src/i18n/messages/en.ts +543 -0
- package/packages/tui/src/i18n/messages/zh-CN.ts +528 -0
- package/packages/tui/src/index.ts +335 -0
- package/packages/tui/src/renderer/ask-user.ts +431 -0
- package/packages/tui/src/renderer/notify-user.ts +184 -0
- package/packages/tui/src/renderer/stream-renderer.ts +149 -0
- package/packages/tui/src/renderer/tool-confirm.ts +47 -0
- package/packages/tui/src/repl/agent-registry.ts +747 -0
- package/packages/tui/src/repl/cache-metrics.ts +241 -0
- package/packages/tui/src/repl/clear-on-start.ts +28 -0
- package/packages/tui/src/repl/error-archive.ts +95 -0
- package/packages/tui/src/repl/error-log.ts +212 -0
- package/packages/tui/src/repl/ink-repl.tsx +1735 -0
- package/packages/tui/src/repl/layout.ts +303 -0
- package/packages/tui/src/repl/limit-info.ts +630 -0
- package/packages/tui/src/repl/permission-pending.ts +49 -0
- package/packages/tui/src/repl/project/ui-item.ts +1307 -0
- package/packages/tui/src/repl/queue-info.ts +109 -0
- package/packages/tui/src/repl/resume-picker.ts +24 -0
- package/packages/tui/src/repl/run-turn.ts +60 -0
- package/packages/tui/src/repl/runtime-counters.ts +91 -0
- package/packages/tui/src/repl/startup-header.ts +144 -0
- package/packages/tui/src/repl/startup-notice.ts +100 -0
- package/packages/tui/src/repl/status-format.ts +513 -0
- package/packages/tui/src/repl/status-metrics.ts +88 -0
- package/packages/tui/src/repl/status-snapshot.ts +454 -0
- package/packages/tui/src/repl/use-repl-state.ts +548 -0
- package/packages/tui/src/repl-context.ts +349 -0
- package/packages/tui/src/rich-renderer.ts +63 -0
- package/packages/tui/src/rpc-events.ts +161 -0
- package/packages/tui/src/rpc.ts +149 -0
- package/packages/tui/src/theme.ts +180 -0
- package/packages/tui/src/types/events.ts +284 -0
- package/packages/tui/src/utils/dialog.ts +35 -0
- package/packages/tui/src/utils/diff.ts +124 -0
- package/packages/tui/src/utils/display-labels.ts +56 -0
- package/packages/tui/src/utils/file-reference.ts +180 -0
- package/packages/tui/src/utils/git.ts +24 -0
- package/packages/tui/src/utils/sanitize-terminal-text.ts +109 -0
- package/packages/tui/src/utils/text-layout.ts +127 -0
- package/packages/tui/src/utils/time.ts +25 -0
- package/packages/tui/src/utils/tool-input-summary.ts +70 -0
- package/packages/tui/src/views/keys.ts +344 -0
- package/packages/tui/src/views/layout.ts +224 -0
- package/packages/tui/src/views/render-text.ts +154 -0
- package/packages/tui/src/views/selection.ts +57 -0
- package/packages/tui/src/views/session-picker.ts +215 -0
- package/packages/tui/src/views/tones.ts +24 -0
- package/packages/tui/src/views/types.ts +200 -0
- package/packages/tui/src/wizard/setup-wizard.ts +439 -0
- package/packages/tui/tsconfig.json +10 -0
- package/plugins/context/doc-subscription/package.json +32 -0
- package/plugins/context/doc-subscription/src/agent.ts +151 -0
- package/plugins/context/doc-subscription/src/auto-sub.ts +80 -0
- package/plugins/context/doc-subscription/src/config.ts +358 -0
- package/plugins/context/doc-subscription/src/diff.ts +107 -0
- package/plugins/context/doc-subscription/src/friend-edges.ts +118 -0
- package/plugins/context/doc-subscription/src/health.ts +163 -0
- package/plugins/context/doc-subscription/src/hook.ts +1222 -0
- package/plugins/context/doc-subscription/src/implicit.ts +84 -0
- package/plugins/context/doc-subscription/src/index.ts +288 -0
- package/plugins/context/doc-subscription/src/lifecycle-listener.ts +58 -0
- package/plugins/context/doc-subscription/src/lifecycle.ts +186 -0
- package/plugins/context/doc-subscription/src/mute.ts +39 -0
- package/plugins/context/doc-subscription/src/notice.ts +347 -0
- package/plugins/context/doc-subscription/src/script.ts +288 -0
- package/plugins/context/doc-subscription/src/sources.ts +187 -0
- package/plugins/context/doc-subscription/src/store.ts +748 -0
- package/plugins/context/doc-subscription/src/team-log.ts +155 -0
- package/plugins/context/doc-subscription/src/tool.ts +2132 -0
- package/plugins/context/skills/package.json +182 -0
- package/plugins/context/skills/src/config.ts +178 -0
- package/plugins/context/skills/src/frontmatter.ts +166 -0
- package/plugins/context/skills/src/index.ts +144 -0
- package/plugins/context/skills/src/rpc.ts +274 -0
- package/plugins/context/skills/src/scanner.ts +262 -0
- package/plugins/context/skills/src/service.ts +277 -0
- package/plugins/context/skills/src/store.ts +333 -0
- package/plugins/context/skills/src/tool.ts +354 -0
- package/plugins/context/skills/src/trust.ts +71 -0
- package/plugins/context/skills/src/types.ts +130 -0
- package/plugins/context/skills/src/watch.ts +83 -0
- package/plugins/context/skills/tsconfig.web.json +29 -0
- package/plugins/context/skills/vite.config.embed.ts +107 -0
- package/plugins/context/skills/vite.config.ts +121 -0
- package/plugins/context/skills/web/assets/skills-8su2cNqF.css +2 -0
- package/plugins/context/skills/web/assets/skills-BhiDTAac.js +10 -0
- package/plugins/context/skills/web/embed/index.css +3 -0
- package/plugins/context/skills/web/embed/index.mjs +4251 -0
- package/plugins/context/skills/web/skills.html +13 -0
- package/plugins/context/skills/web-src/App.tsx +47 -0
- package/plugins/context/skills/web-src/SkillsView.tsx +649 -0
- package/plugins/context/skills/web-src/embed.css +153 -0
- package/plugins/context/skills/web-src/embed.tsx +36 -0
- package/plugins/context/skills/web-src/env.d.ts +2 -0
- package/plugins/context/skills/web-src/i18n.ts +195 -0
- package/plugins/context/skills/web-src/main.tsx +14 -0
- package/plugins/context/skills/web-src/shared.tsx +226 -0
- package/plugins/context/skills/web-src/skills.html +12 -0
- package/plugins/context/skills/web-src/styles.css +62 -0
- package/plugins/context/skills/web-src/types.ts +85 -0
- package/plugins/context/thinking-intensity/package.json +201 -0
- package/plugins/context/thinking-intensity/src/index.ts +285 -0
- package/plugins/context/thinking-intensity/src/mapping.ts +175 -0
- package/plugins/context/thinking-intensity/src/state.ts +134 -0
- package/plugins/evals/harness/package.json +32 -0
- package/plugins/evals/harness/src/assertions.ts +99 -0
- package/plugins/evals/harness/src/errors.ts +23 -0
- package/plugins/evals/harness/src/index.ts +48 -0
- package/plugins/evals/harness/src/mock-provider.ts +64 -0
- package/plugins/evals/harness/src/plugin.ts +48 -0
- package/plugins/evals/harness/src/report.ts +66 -0
- package/plugins/evals/harness/src/runner.ts +417 -0
- package/plugins/evals/harness/src/suite.ts +352 -0
- package/plugins/evals/harness/src/types.ts +154 -0
- package/plugins/fleet/compose/README.md +40 -0
- package/plugins/fleet/compose/package.json +172 -0
- package/plugins/fleet/compose/src/cli.ts +347 -0
- package/plugins/fleet/compose/src/i18n.ts +99 -0
- package/plugins/fleet/compose/src/index.ts +3 -0
- package/plugins/fleet/compose/src/manifest.ts +194 -0
- package/plugins/im/feishu/FEISHU.md +142 -0
- package/plugins/im/feishu/package.json +33 -0
- package/plugins/im/feishu/src/channel.ts +360 -0
- package/plugins/im/feishu/src/config.ts +133 -0
- package/plugins/im/feishu/src/dedupe.ts +30 -0
- package/plugins/im/feishu/src/errors.ts +21 -0
- package/plugins/im/feishu/src/index.ts +47 -0
- package/plugins/im/feishu/src/messages.ts +41 -0
- package/plugins/im/feishu/src/queue.ts +71 -0
- package/plugins/im/feishu/src/runner.ts +356 -0
- package/plugins/im/feishu/src/sdk-transport.ts +220 -0
- package/plugins/im/feishu/src/text.ts +56 -0
- package/plugins/im/feishu/src/types.ts +150 -0
- package/plugins/marketplace/marketplace/package.json +166 -0
- package/plugins/marketplace/marketplace/registry-template/README.md +105 -0
- package/plugins/marketplace/marketplace/registry-template/index.sample.json +71 -0
- package/plugins/marketplace/marketplace/registry-template/validate.mjs +272 -0
- package/plugins/marketplace/marketplace/registry-template/validate.yml +35 -0
- package/plugins/marketplace/marketplace/src/config-lock.ts +325 -0
- package/plugins/marketplace/marketplace/src/config.ts +432 -0
- package/plugins/marketplace/marketplace/src/errors.ts +79 -0
- package/plugins/marketplace/marketplace/src/index.ts +364 -0
- package/plugins/marketplace/marketplace/src/install.ts +930 -0
- package/plugins/marketplace/marketplace/src/jobs.ts +155 -0
- package/plugins/marketplace/marketplace/src/registry.ts +508 -0
- package/plugins/marketplace/marketplace/src/semver.ts +162 -0
- package/plugins/marketplace/marketplace/src/settings.ts +108 -0
- package/plugins/marketplace/marketplace/src/tar.ts +269 -0
- package/plugins/marketplace/marketplace/tsconfig.ui.json +26 -0
- package/plugins/marketplace/marketplace/vite.config.ts +107 -0
- package/plugins/marketplace/marketplace/web/assets/index-B2V7bx73.css +2 -0
- package/plugins/marketplace/marketplace/web/assets/index-B4NAz59m.js +13 -0
- package/plugins/marketplace/marketplace/web/index.html +13 -0
- package/plugins/marketplace/marketplace/web-src/App.tsx +193 -0
- package/plugins/marketplace/marketplace/web-src/api.ts +153 -0
- package/plugins/marketplace/marketplace/web-src/catalog.ts +27 -0
- package/plugins/marketplace/marketplace/web-src/components/ConfirmDialog.tsx +96 -0
- package/plugins/marketplace/marketplace/web-src/components/InstalledTable.tsx +152 -0
- package/plugins/marketplace/marketplace/web-src/components/JobStatus.tsx +70 -0
- package/plugins/marketplace/marketplace/web-src/components/Permissions.tsx +116 -0
- package/plugins/marketplace/marketplace/web-src/components/RegistryBanners.tsx +22 -0
- package/plugins/marketplace/marketplace/web-src/components/RegistrySetup.tsx +69 -0
- package/plugins/marketplace/marketplace/web-src/components/States.tsx +97 -0
- package/plugins/marketplace/marketplace/web-src/i18n.ts +337 -0
- package/plugins/marketplace/marketplace/web-src/index.html +12 -0
- package/plugins/marketplace/marketplace/web-src/main.tsx +17 -0
- package/plugins/marketplace/marketplace/web-src/styles.css +37 -0
- package/plugins/marketplace/marketplace/web-src/types.ts +175 -0
- package/plugins/marketplace/marketplace/web-src/views/InstalledView.tsx +150 -0
- package/plugins/marketplace/marketplace/web-src/views/MarketplaceView.tsx +479 -0
- package/plugins/marketplace/marketplace/web-src/views/UpdatesView.tsx +207 -0
- package/plugins/marketplace/marketplace/web-src/vite-env.d.ts +1 -0
- package/plugins/mcp/client/package.json +31 -0
- package/plugins/mcp/client/src/config-validate.ts +37 -0
- package/plugins/mcp/client/src/http-transport.ts +369 -0
- package/plugins/mcp/client/src/index.ts +35 -0
- package/plugins/mcp/client/src/json-rpc.ts +104 -0
- package/plugins/mcp/client/src/logging.ts +51 -0
- package/plugins/mcp/client/src/naming.ts +36 -0
- package/plugins/mcp/client/src/plugin.ts +310 -0
- package/plugins/mcp/client/src/registration.ts +143 -0
- package/plugins/mcp/client/src/server-connection.ts +556 -0
- package/plugins/mcp/client/src/stdio-transport.ts +377 -0
- package/plugins/mcp/client/src/transport.ts +11 -0
- package/plugins/mcp/client/src/types.ts +147 -0
- package/plugins/media/media/package.json +33 -0
- package/plugins/media/media/src/binaries.ts +88 -0
- package/plugins/media/media/src/config.ts +184 -0
- package/plugins/media/media/src/exec.ts +172 -0
- package/plugins/media/media/src/index.ts +125 -0
- package/plugins/media/media/src/tool.ts +640 -0
- package/plugins/observability/hook-logging/package.json +31 -0
- package/plugins/observability/hook-logging/src/index.ts +286 -0
- package/plugins/observability/usage/package.json +163 -0
- package/plugins/observability/usage/src/aggregate.ts +919 -0
- package/plugins/observability/usage/src/index.ts +362 -0
- package/plugins/observability/usage/src/query.ts +322 -0
- package/plugins/observability/usage/src/store.ts +1455 -0
- package/plugins/observability/usage/src/worker-client.ts +263 -0
- package/plugins/observability/usage/src/worker.ts +87 -0
- package/plugins/observability/usage/tsconfig.web.json +29 -0
- package/plugins/observability/usage/vite.config.embed.ts +105 -0
- package/plugins/observability/usage/vite.config.ts +121 -0
- package/plugins/observability/usage/web/assets/main-DD4g5VDL.css +2 -0
- package/plugins/observability/usage/web/assets/main-DVmWaf73.js +10 -0
- package/plugins/observability/usage/web/embed/index.css +3 -0
- package/plugins/observability/usage/web/embed/index.mjs +4560 -0
- package/plugins/observability/usage/web/logs.html +13 -0
- package/plugins/observability/usage/web/usage.html +13 -0
- package/plugins/observability/usage/web-src/App.tsx +54 -0
- package/plugins/observability/usage/web-src/LogsView.tsx +505 -0
- package/plugins/observability/usage/web-src/ModelTable.tsx +102 -0
- package/plugins/observability/usage/web-src/UsageView.tsx +210 -0
- package/plugins/observability/usage/web-src/embed.css +96 -0
- package/plugins/observability/usage/web-src/embed.tsx +75 -0
- package/plugins/observability/usage/web-src/env.d.ts +2 -0
- package/plugins/observability/usage/web-src/format.ts +88 -0
- package/plugins/observability/usage/web-src/i18n.ts +261 -0
- package/plugins/observability/usage/web-src/logs.html +12 -0
- package/plugins/observability/usage/web-src/main.tsx +19 -0
- package/plugins/observability/usage/web-src/shared.tsx +187 -0
- package/plugins/observability/usage/web-src/styles.css +22 -0
- package/plugins/observability/usage/web-src/usage.html +12 -0
- package/plugins/provider/anthropic/package.json +31 -0
- package/plugins/provider/anthropic/src/index.ts +75 -0
- package/plugins/provider/anthropic/src/retry.ts +266 -0
- package/plugins/provider/anthropic/src/timeout.ts +84 -0
- package/plugins/provider/model-registry/package.json +167 -0
- package/plugins/provider/model-registry/src/connectivity.ts +123 -0
- package/plugins/provider/model-registry/src/credential-store.ts +219 -0
- package/plugins/provider/model-registry/src/index.ts +453 -0
- package/plugins/provider/model-registry/src/registry-store.ts +223 -0
- package/plugins/provider/model-registry/tsconfig.web.json +29 -0
- package/plugins/provider/model-registry/vite.config.embed.ts +105 -0
- package/plugins/provider/model-registry/vite.config.ts +122 -0
- package/plugins/provider/model-registry/web/assets/main-BofPzD0n.js +10 -0
- package/plugins/provider/model-registry/web/assets/main-DkpGgdsZ.css +2 -0
- package/plugins/provider/model-registry/web/credentials.html +13 -0
- package/plugins/provider/model-registry/web/embed/index.css +3 -0
- package/plugins/provider/model-registry/web/embed/index.mjs +4887 -0
- package/plugins/provider/model-registry/web/models.html +13 -0
- package/plugins/provider/model-registry/web-src/App.tsx +54 -0
- package/plugins/provider/model-registry/web-src/CredentialsView.tsx +357 -0
- package/plugins/provider/model-registry/web-src/ModelForm.tsx +489 -0
- package/plugins/provider/model-registry/web-src/ModelsView.tsx +383 -0
- package/plugins/provider/model-registry/web-src/credentials.html +12 -0
- package/plugins/provider/model-registry/web-src/embed.css +96 -0
- package/plugins/provider/model-registry/web-src/embed.tsx +43 -0
- package/plugins/provider/model-registry/web-src/env.d.ts +2 -0
- package/plugins/provider/model-registry/web-src/i18n.ts +323 -0
- package/plugins/provider/model-registry/web-src/main.tsx +21 -0
- package/plugins/provider/model-registry/web-src/models.html +12 -0
- package/plugins/provider/model-registry/web-src/shared.tsx +147 -0
- package/plugins/provider/model-registry/web-src/styles.css +22 -0
- package/plugins/provider/model-registry/web-src/types.ts +120 -0
- package/plugins/provider/openai/package.json +31 -0
- package/plugins/provider/openai/src/index.ts +104 -0
- package/plugins/provider/openai/src/retry.ts +266 -0
- package/plugins/provider/openai/src/timeout.ts +84 -0
- package/plugins/provider/openai/src/vision.ts +121 -0
- package/plugins/security/guardrail-pii/package.json +31 -0
- package/plugins/security/guardrail-pii/src/index.ts +285 -0
- package/plugins/security/guardrail-pii/src/rules.ts +197 -0
- package/plugins/security/redact-secrets/package.json +31 -0
- package/plugins/security/redact-secrets/src/index.ts +145 -0
- package/plugins/security/redact-secrets/src/redactor.ts +83 -0
- package/plugins/security/redact-secrets/src/rules.ts +137 -0
- package/plugins/security/tool-policy/package.json +31 -0
- package/plugins/security/tool-policy/src/index.ts +184 -0
- package/plugins/tools/basic-tools/package.json +32 -0
- package/plugins/tools/basic-tools/src/background-jobs.ts +356 -0
- package/plugins/tools/basic-tools/src/bash-source.ts +58 -0
- package/plugins/tools/basic-tools/src/call-scope.ts +80 -0
- package/plugins/tools/basic-tools/src/config.ts +130 -0
- package/plugins/tools/basic-tools/src/index.ts +202 -0
- package/plugins/tools/basic-tools/src/path-boundary.ts +258 -0
- package/plugins/tools/basic-tools/src/shared.ts +221 -0
- package/plugins/tools/basic-tools/src/text.ts +49 -0
- package/plugins/tools/basic-tools/src/tools/bash-async.ts +1830 -0
- package/plugins/tools/basic-tools/src/tools/bash-exec.ts +567 -0
- package/plugins/tools/basic-tools/src/tools/bash.ts +300 -0
- package/plugins/tools/basic-tools/src/tools/edit.ts +160 -0
- package/plugins/tools/basic-tools/src/tools/grep.ts +601 -0
- package/plugins/tools/basic-tools/src/tools/read.ts +151 -0
- package/plugins/tools/basic-tools/src/tools/write.ts +494 -0
- package/plugins/tools/basic-tools/src/write-coord.ts +291 -0
- package/plugins/tools/basic-tools/src/write-lock.ts +353 -0
- package/plugins/tools/engine-client/package.json +34 -0
- package/plugins/tools/engine-client/src/client.ts +276 -0
- package/plugins/tools/engine-client/src/forward.ts +164 -0
- package/plugins/tools/engine-client/src/index.ts +18 -0
- package/plugins/tools/engine-client/src/pending.ts +101 -0
- package/plugins/tools/engine-client/src/pool.ts +1173 -0
- package/plugins/tools/engine-client/src/protocol.ts +249 -0
- package/plugins/tools/engine-client/src/registry.ts +138 -0
- package/plugins/tools/file-refs/README.md +68 -0
- package/plugins/tools/file-refs/package.json +135 -0
- package/plugins/tools/file-refs/src/index.ts +24 -0
- package/plugins/tools/file-refs/tsconfig.web.json +27 -0
- package/plugins/tools/file-refs/vite.config.embed.ts +88 -0
- package/plugins/tools/file-refs/web/embed/index.css +3 -0
- package/plugins/tools/file-refs/web/embed/index.mjs +879 -0
- package/plugins/tools/file-refs/web-src/AttachSheet.tsx +613 -0
- package/plugins/tools/file-refs/web-src/FileRefCard.tsx +206 -0
- package/plugins/tools/file-refs/web-src/FileRefLinkCard.tsx +90 -0
- package/plugins/tools/file-refs/web-src/attach-handler.tsx +116 -0
- package/plugins/tools/file-refs/web-src/attach-paths.ts +69 -0
- package/plugins/tools/file-refs/web-src/drop-handler.ts +15 -0
- package/plugins/tools/file-refs/web-src/embed.css +518 -0
- package/plugins/tools/file-refs/web-src/embed.tsx +18 -0
- package/plugins/tools/file-refs/web-src/env.d.ts +2 -0
- package/plugins/tools/file-refs/web-src/i18n.ts +137 -0
- package/plugins/tools/file-refs/web-src/icons.tsx +122 -0
- package/plugins/tools/file-refs/web-src/staging.ts +120 -0
- package/plugins/tools/file-refs/web-src/thumbnail.tsx +36 -0
- package/plugins/tools/recall/package.json +31 -0
- package/plugins/tools/recall/src/config.ts +74 -0
- package/plugins/tools/recall/src/index.ts +151 -0
- package/plugins/tools/recall/src/tool.ts +307 -0
- package/plugins/tools/web-tools/README.md +330 -0
- package/plugins/tools/web-tools/package.json +184 -0
- package/plugins/tools/web-tools/src/backends.ts +61 -0
- package/plugins/tools/web-tools/src/budget.ts +80 -0
- package/plugins/tools/web-tools/src/cache.ts +94 -0
- package/plugins/tools/web-tools/src/config.ts +412 -0
- package/plugins/tools/web-tools/src/deepseek-client.ts +406 -0
- package/plugins/tools/web-tools/src/deepseek-fetch.ts +168 -0
- package/plugins/tools/web-tools/src/extract.ts +373 -0
- package/plugins/tools/web-tools/src/fetch/local.ts +36 -0
- package/plugins/tools/web-tools/src/fetch/provider.ts +56 -0
- package/plugins/tools/web-tools/src/http.ts +415 -0
- package/plugins/tools/web-tools/src/index.ts +649 -0
- package/plugins/tools/web-tools/src/limits.ts +388 -0
- package/plugins/tools/web-tools/src/provider-http.ts +102 -0
- package/plugins/tools/web-tools/src/render.ts +248 -0
- package/plugins/tools/web-tools/src/robots.ts +187 -0
- package/plugins/tools/web-tools/src/search/custom.ts +595 -0
- package/plugins/tools/web-tools/src/search/deepseek.ts +111 -0
- package/plugins/tools/web-tools/src/search/index.ts +725 -0
- package/plugins/tools/web-tools/src/search/provider.ts +200 -0
- package/plugins/tools/web-tools/src/search/serper.ts +157 -0
- package/plugins/tools/web-tools/src/search/tavily.ts +146 -0
- package/plugins/tools/web-tools/src/secrets.ts +87 -0
- package/plugins/tools/web-tools/src/ssrf.ts +287 -0
- package/plugins/tools/web-tools/src/types.ts +137 -0
- package/skills/code-writing.md +33 -0
- package/src/agent/agent-control.ts +409 -0
- package/src/agent/agent-owner-client.ts +150 -0
- package/src/agent/agent-owner-server.ts +689 -0
- package/src/agent/agent-service-wiring.ts +675 -0
- package/src/agent/agent-wiring.ts +48 -0
- package/src/agent/ask-answer-delivery.ts +215 -0
- package/src/agent/local-run-adapter.ts +335 -0
- package/src/agent/peer-notices.ts +459 -0
- package/src/agent/peer-source.ts +63 -0
- package/src/agent/self-card.ts +52 -0
- package/src/agent/team-human-delivery.ts +144 -0
- package/src/assembly/collect.ts +161 -0
- package/src/assembly/feature.ts +133 -0
- package/src/assembly/features/attachments.ts +38 -0
- package/src/assembly/features/fleet.ts +31 -0
- package/src/assembly/features/jobs.ts +32 -0
- package/src/assembly/features/kanban.ts +56 -0
- package/src/assembly/features/restart.ts +35 -0
- package/src/assembly/features/subscription.ts +32 -0
- package/src/assembly/features.ts +30 -0
- package/src/cli/agent-call.ts +107 -0
- package/src/cli/args.ts +551 -0
- package/src/cli/ask-channel.ts +447 -0
- package/src/cli/command-spec/agent.ts +306 -0
- package/src/cli/command-spec/config.ts +179 -0
- package/src/cli/command-spec/define.ts +86 -0
- package/src/cli/command-spec/federation.ts +333 -0
- package/src/cli/command-spec/flags.ts +668 -0
- package/src/cli/command-spec/gateway.ts +350 -0
- package/src/cli/command-spec/hub.ts +290 -0
- package/src/cli/command-spec/kanban.ts +134 -0
- package/src/cli/command-spec/misc.ts +1032 -0
- package/src/cli/command-spec/model.ts +338 -0
- package/src/cli/command-spec/node.ts +160 -0
- package/src/cli/command-spec/plugin.ts +148 -0
- package/src/cli/command-spec/shared.ts +86 -0
- package/src/cli/command-spec/team.ts +582 -0
- package/src/cli/command-spec/token.ts +111 -0
- package/src/cli/command-spec.ts +270 -0
- package/src/cli/command-support.ts +53 -0
- package/src/cli/commands/agent/agent-command.ts +279 -0
- package/src/cli/commands/agent/agent-inbox-command.ts +141 -0
- package/src/cli/commands/agent/agents-command.ts +145 -0
- package/src/cli/commands/attention.ts +896 -0
- package/src/cli/commands/cli-update.ts +283 -0
- package/src/cli/commands/completion-command.ts +29 -0
- package/src/cli/commands/config-command.ts +545 -0
- package/src/cli/commands/dev-command.ts +429 -0
- package/src/cli/commands/doctor-command.ts +873 -0
- package/src/cli/commands/doctor-fleet.ts +410 -0
- package/src/cli/commands/errors-command.ts +469 -0
- package/src/cli/commands/eval-command.ts +168 -0
- package/src/cli/commands/federation-pair.ts +426 -0
- package/src/cli/commands/federation-peer.ts +445 -0
- package/src/cli/commands/federation.ts +527 -0
- package/src/cli/commands/fleet/fleet-inventory.ts +222 -0
- package/src/cli/commands/fleet/fleet-status-command.ts +433 -0
- package/src/cli/commands/fleet/ps-command.ts +387 -0
- package/src/cli/commands/fleet/restart-command.ts +745 -0
- package/src/cli/commands/fleet/start-command.ts +176 -0
- package/src/cli/commands/fleet/status-command.ts +318 -0
- package/src/cli/commands/fleet/stop-command.ts +548 -0
- package/src/cli/commands/gateway/gateway-command.ts +4204 -0
- package/src/cli/commands/gateway/gateway-nodes-command.ts +401 -0
- package/src/cli/commands/hub-command.ts +1007 -0
- package/src/cli/commands/kanban-command.ts +204 -0
- package/src/cli/commands/logs-command.ts +148 -0
- package/src/cli/commands/migrate-command.ts +131 -0
- package/src/cli/commands/model.ts +427 -0
- package/src/cli/commands/node-command.ts +805 -0
- package/src/cli/commands/permission/permission-show-command.ts +189 -0
- package/src/cli/commands/plugin/plugin-cli-command.ts +831 -0
- package/src/cli/commands/plugin/plugin-cli-local.ts +267 -0
- package/src/cli/commands/plugin/plugin-command.ts +407 -0
- package/src/cli/commands/prune-command.ts +233 -0
- package/src/cli/commands/resume-command.ts +100 -0
- package/src/cli/commands/rpc-command.ts +226 -0
- package/src/cli/commands/session/session-status-command.ts +352 -0
- package/src/cli/commands/team-command.ts +722 -0
- package/src/cli/commands/token-command.ts +494 -0
- package/src/cli/commands/trace-command.ts +133 -0
- package/src/cli/completion-script.ts +627 -0
- package/src/cli/completion-spec.ts +101 -0
- package/src/cli/control-plane-target.ts +295 -0
- package/src/cli/control-plane-types.ts +189 -0
- package/src/cli/feishu-command.ts +45 -0
- package/src/cli/headless-runner.ts +354 -0
- package/src/cli/help-search.ts +320 -0
- package/src/cli/help.ts +489 -0
- package/src/cli/output.ts +141 -0
- package/src/cli/parse-engine.ts +701 -0
- package/src/cli/parse-validators.ts +155 -0
- package/src/cli/project.ts +355 -0
- package/src/cli/registry.ts +25 -0
- package/src/cli.ts +2272 -0
- package/src/config/cli-overrides.ts +74 -0
- package/src/config/config-error-format.ts +44 -0
- package/src/config/config-snapshot.ts +15 -0
- package/src/config/config-watch.ts +436 -0
- package/src/config/config-write-apply.ts +98 -0
- package/src/config/empty-response-settings.ts +49 -0
- package/src/config/hot-apply-appliers.ts +527 -0
- package/src/config/hot-apply-assembly.ts +317 -0
- package/src/config/legacy-tool-names.ts +48 -0
- package/src/config/permission-defaults.ts +26 -0
- package/src/config/ui-settings.ts +568 -0
- package/src/config/vision-settings.ts +98 -0
- package/src/data/attachment-ingest.ts +101 -0
- package/src/data/attachment-staging.ts +377 -0
- package/src/data/attachments-list.ts +240 -0
- package/src/data/auto-prune.ts +342 -0
- package/src/data/data-lock.ts +580 -0
- package/src/data/data-paths.ts +265 -0
- package/src/data/image-blob-gc.ts +341 -0
- package/src/data/migrate.ts +480 -0
- package/src/data/path-resolution.ts +172 -0
- package/src/data/permission-store.ts +322 -0
- package/src/data/prune-wiring.ts +1037 -0
- package/src/data/refs-notice-hook.ts +417 -0
- package/src/federation/attention-client.ts +196 -0
- package/src/federation/attention-nodes.ts +142 -0
- package/src/federation/federation-address.ts +276 -0
- package/src/federation/federation-client.ts +620 -0
- package/src/federation/federation-config.ts +222 -0
- package/src/federation/federation-outbox.ts +374 -0
- package/src/federation/federation-pair-store.ts +236 -0
- package/src/federation/federation-pairing.ts +176 -0
- package/src/federation/federation-peer-store.ts +145 -0
- package/src/federation/federation-secrets.ts +140 -0
- package/src/federation/federation-send-port.ts +436 -0
- package/src/gateway/clean-spawn.ts +260 -0
- package/src/gateway/daemon.ts +336 -0
- package/src/gateway/fleet-index.ts +262 -0
- package/src/gateway/gateway-banner.ts +199 -0
- package/src/gateway/gateway-config-control.ts +53 -0
- package/src/gateway/gateway-reload-wiring.ts +512 -0
- package/src/gateway/gateway-wiring.ts +113 -0
- package/src/gateway/handoff.ts +131 -0
- package/src/gateway/instance-lock.ts +244 -0
- package/src/gateway/interactions-dir.ts +27 -0
- package/src/gateway/node-listen.ts +123 -0
- package/src/gateway/node-registry.ts +401 -0
- package/src/gateway/node-supervisor.ts +2226 -0
- package/src/gateway/pid.ts +827 -0
- package/src/gateway/pool-supervisor.ts +1150 -0
- package/src/gateway/reload.ts +238 -0
- package/src/gateway/restart-stop-exemption.ts +43 -0
- package/src/gateway/self-restart.ts +147 -0
- package/src/gateway/serve-auth.ts +184 -0
- package/src/gateway/serve-config.ts +135 -0
- package/src/gateway/service.ts +361 -0
- package/src/gateway/shutdown-report.ts +47 -0
- package/src/gateway/shutdown-sequence.ts +70 -0
- package/src/gateway/stop-drain.ts +299 -0
- package/src/gateway/stop-nodes-marker.ts +151 -0
- package/src/gateway/transport-factory.ts +53 -0
- package/src/gateway/watchdog.ts +622 -0
- package/src/gateway/web-dist.ts +1155 -0
- package/src/hub/client-protocol.ts +43 -0
- package/src/hub/derive-origins.ts +193 -0
- package/src/hub/hub-wiring.ts +310 -0
- package/src/hub/inbound-control.ts +432 -0
- package/src/hub/join-policy.ts +75 -0
- package/src/hub/join-store.ts +243 -0
- package/src/hub/node-credential-store.ts +295 -0
- package/src/hub/node-state.ts +279 -0
- package/src/hub/node-url.ts +33 -0
- package/src/hub/outbound-client.ts +509 -0
- package/src/hub/outbound-wiring.ts +168 -0
- package/src/hub/pairing-code.ts +27 -0
- package/src/hub/registry.ts +575 -0
- package/src/hub/runtime.ts +599 -0
- package/src/hub/secret-store.ts +571 -0
- package/src/hub/server.ts +1032 -0
- package/src/hub/snapshot-policy.ts +67 -0
- package/src/hub/types.ts +258 -0
- package/src/i18n/index.ts +94 -0
- package/src/i18n/messages/en.ts +1462 -0
- package/src/i18n/messages/zh-CN.ts +1323 -0
- package/src/mock/scenarios.ts +165 -0
- package/src/node/node-auto-resume.ts +264 -0
- package/src/node/node-drain.ts +520 -0
- package/src/node/node-identity.ts +240 -0
- package/src/node/node-resume.ts +511 -0
- package/src/node/resume-scan.ts +97 -0
- package/src/node/token-derive.ts +504 -0
- package/src/observability/logging.ts +72 -0
- package/src/observability/observability-wiring.ts +82 -0
- package/src/observability/startup-timing.ts +151 -0
- package/src/observability/trace-wiring.ts +250 -0
- package/src/observability/usage-pricing.ts +80 -0
- package/src/plugin/eval-wiring.ts +175 -0
- package/src/plugin/feishu-wiring.ts +250 -0
- package/src/plugin/mcp-wiring.ts +200 -0
- package/src/plugin/plugin-config.ts +491 -0
- package/src/plugin/plugin-host-assembly.ts +448 -0
- package/src/plugin/plugin-lifecycle.ts +130 -0
- package/src/plugin/plugin-loader.ts +788 -0
- package/src/plugin/plugin-manifest.ts +1936 -0
- package/src/plugin/plugin-selection.ts +200 -0
- package/src/plugin/plugin-surface.ts +207 -0
- package/src/plugin/plugin-watch.ts +114 -0
- package/src/rpc/http-client.ts +579 -0
- package/src/rpc/index.ts +34 -0
- package/src/rpc/local-client.ts +55 -0
- package/src/rpc/types.ts +125 -0
- package/src/run/connect-run-wait.ts +544 -0
- package/src/run/durable-wiring.ts +133 -0
- package/src/run/lease-service.ts +510 -0
- package/src/run/run-control.ts +160 -0
- package/src/run/run-record-store-factory.ts +89 -0
- package/src/runtime/agent-activity-watch.ts +335 -0
- package/src/runtime/agent-assembly.ts +818 -0
- package/src/runtime/agent-events-bridge.ts +86 -0
- package/src/runtime/bash-job-delivery.ts +291 -0
- package/src/runtime/bootstrap-connect.ts +644 -0
- package/src/runtime/bootstrap-types.ts +303 -0
- package/src/runtime/bootstrap.ts +1020 -0
- package/src/runtime/bus-source.ts +62 -0
- package/src/runtime/gc-assembly.ts +214 -0
- package/src/runtime/local-rpc-surface.ts +301 -0
- package/src/runtime/model-registry-wiring.ts +653 -0
- package/src/runtime/node-health-watch.ts +484 -0
- package/src/runtime/node-restart.ts +444 -0
- package/src/runtime/process-handlers.ts +164 -0
- package/src/runtime/provider-factory.ts +86 -0
- package/src/runtime/reasoning.ts +47 -0
- package/src/runtime/repl-context.ts +234 -0
- package/src/runtime/runtime-notice-hook.ts +357 -0
- package/src/runtime/runtime-notice.ts +273 -0
- package/src/runtime/session-id.ts +15 -0
- package/src/runtime/shutdown-sequence.ts +429 -0
- package/src/runtime/subscription-mute-bridge.ts +99 -0
- package/src/runtime/subscription-rpc-port.ts +597 -0
- package/src/runtime/team-log-bridge.ts +46 -0
- package/src/runtime/tenant-runtime-factory.ts +1838 -0
- package/src/runtime/thinking-agent-bridge.ts +57 -0
- package/src/runtime/tool-output-env.ts +80 -0
- package/src/runtime/vessel-meta.ts +338 -0
- package/src/session/context-composition.ts +147 -0
- package/src/session/context-ratio-source.ts +165 -0
- package/src/session/context-usage.ts +346 -0
- package/src/session/image-delivery.ts +297 -0
- package/src/session/session-control.ts +135 -0
- package/src/session/session-events.ts +144 -0
- package/src/session/session-factory.ts +73 -0
- package/src/session/session-liveness.ts +405 -0
- package/src/session/session-lock-runtime.ts +64 -0
- package/src/session/session-lock.ts +399 -0
- package/src/session/session-resume.ts +282 -0
- package/src/session/system-context.ts +235 -0
- package/src/session/timeline-dual-write.ts +2129 -0
- package/src/session/timeline-session-log.ts +140 -0
- package/src/shared/port-in-use.ts +27 -0
- package/src/shared/socket-inherit-guard.ts +13 -0
- package/src/shared/version.ts +38 -0
- package/src/tools-engine/engine.ts +286 -0
- package/src/tools-engine/entry.ts +757 -0
- package/src/tools-engine/mcp-engine-wiring.ts +354 -0
- package/src/tools-engine/mcp.ts +324 -0
- package/src/tools-engine/supervisor.ts +234 -0
- package/src/tools-engine/tools-list.ts +148 -0
- package/src/tools-engine/validate.ts +395 -0
- package/src/tools-engine/wiring.ts +88 -0
- package/src/tools-engine/worker.ts +110 -0
- package/src/update/background.ts +64 -0
- package/src/update/cache.ts +70 -0
- package/src/update/check.ts +138 -0
- package/src/update/gateway-check.ts +231 -0
- package/src/update/install-detect.ts +125 -0
- package/src/update/manifest.ts +132 -0
- package/src/update/semver.ts +70 -0
- package/src/update/stage.ts +219 -0
|
@@ -0,0 +1,908 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话事件 → `Message[]` 纯函数投影(0.18)。
|
|
3
|
+
*
|
|
4
|
+
* 输入是 `events.ndjson` 的记录(或任何 `{type, ts, data}` 序列),输出与
|
|
5
|
+
* ExecutionUnit 写入 WorkingContext 的消息形状一致(不含 system prompt;
|
|
6
|
+
* 调用方按 run 的 systemPrompt 自行补齐)。
|
|
7
|
+
*
|
|
8
|
+
* 覆盖的事实事件:
|
|
9
|
+
* - `user.message` → `{role:'user', content, refs?}`(`data.message` 优先;
|
|
10
|
+
* `refs` 逐条宽松解析,非法项丢弃)
|
|
11
|
+
* - `message.injected` → `data.message`(BeforeLlm / RunOptions 注入)
|
|
12
|
+
* - `llm.response` → assistant(content / reasoning_content / tool_calls)
|
|
13
|
+
* - `tool.call.started` → 用来修正 assistant.tool_calls 的实际 name/arguments
|
|
14
|
+
* - `tool.call.completed` → `{role:'tool', content: result}`
|
|
15
|
+
* - `tool.call.failed` → `{role:'tool', content: content ?? "Error: <error>"}`
|
|
16
|
+
* - `run.completed.output` → 覆盖最后一条无 tool_calls 的 assistant 文本
|
|
17
|
+
* - `message.compaction` → 压缩摘要(0.32 起由 runtime 记录;带 `kept` 边界时
|
|
18
|
+
* 按应用侧同一规则替换旧历史,否则只追加摘要)
|
|
19
|
+
* - `session.rewound` → 把当前投影截到 `keep_projected`(0.37 消息撤回;
|
|
20
|
+
* 全量重投影时不能复活已删除的消息)
|
|
21
|
+
*
|
|
22
|
+
* 两条**事实**(`kept` 裁尾 / `rewound` 截断)无条件应用;只有「把工具结果换成占位」
|
|
23
|
+
* 这一件**视图**事可以按调用方要求跳过(见 `ProjectMessagesOptions`)。
|
|
24
|
+
*
|
|
25
|
+
* 消息身份(additive `run_id` / `run_segment`):`run_id` 取自事件的 `data.run_id`,
|
|
26
|
+
* 段序由 `assignMessageIdentities` 在投影末尾统一补(与落盘漏斗同一个函数)。
|
|
27
|
+
* 缺 `run_id` 的旧事件两个字段都缺省 —— 消费端退回内容比较。
|
|
28
|
+
* assistant `message_id`(q-frontend-message-dedup 批 1):`llm.response` 载荷里写者
|
|
29
|
+
* 盖的原样带出;缺省时由 `assignMessageIdentities` 新盖段序的同一笔补。
|
|
30
|
+
*
|
|
31
|
+
* 不产生消息的事件(run.* / permission / context.* / limit 等)直接忽略。
|
|
32
|
+
*
|
|
33
|
+
* @module @vessel-agent/core/session
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import { formatClearedToolResult, isClearedToolResult } from '../context/index.js';
|
|
37
|
+
import {
|
|
38
|
+
MESSAGE_REF_PATH_MAX,
|
|
39
|
+
MESSAGE_REFS_MAX,
|
|
40
|
+
type Message,
|
|
41
|
+
type MessageRef,
|
|
42
|
+
type MessageRefKind,
|
|
43
|
+
} from '../types/provider.js';
|
|
44
|
+
import type { ToolCall } from '../types/tool.js';
|
|
45
|
+
import { assignMessageIdentities, withMessageId, withRunIdentity } from './message-identity.js';
|
|
46
|
+
|
|
47
|
+
/** 投影所需的最小事件形状(与 `SessionEventRecord` 结构兼容) */
|
|
48
|
+
export interface ProjectableEvent {
|
|
49
|
+
type: string;
|
|
50
|
+
ts: number;
|
|
51
|
+
data?: unknown;
|
|
52
|
+
/** 事件在日志里的 seq(增量续算要用;旧调用方传的事件没有它 = 不推进游标)。 */
|
|
53
|
+
seq?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
57
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function readString(value: unknown): string | undefined {
|
|
61
|
+
return typeof value === 'string' ? value : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readCount(value: unknown): number | undefined {
|
|
65
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0
|
|
66
|
+
? Math.floor(value)
|
|
67
|
+
: undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function readStringArray(value: unknown): string[] {
|
|
71
|
+
if (!Array.isArray(value)) return [];
|
|
72
|
+
const result: string[] = [];
|
|
73
|
+
for (const entry of value) {
|
|
74
|
+
if (typeof entry === 'string' && entry.length > 0) result.push(entry);
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const REF_KINDS: ReadonlySet<string> = new Set<MessageRefKind>(['file', 'image', 'document']);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 宽松解析 `Message.refs`(与 `timestamp` 同形的「能认就留、认不出就丢」)。
|
|
83
|
+
*
|
|
84
|
+
* 逐条校验、逐条丢弃:一条坏引用不能把整条消息(乃至整段历史)判成非法——
|
|
85
|
+
* 事件日志里的 refs 是写侧已经过闸门的形状,这里只防手改 / 旧版本 / 半成品数据。
|
|
86
|
+
* 条数与 `path` 长度按写侧同一上限截断,避免投影出比闸门更宽的历史。
|
|
87
|
+
*/
|
|
88
|
+
function asRefs(value: unknown): MessageRef[] | undefined {
|
|
89
|
+
if (!Array.isArray(value)) return undefined;
|
|
90
|
+
const refs: MessageRef[] = [];
|
|
91
|
+
for (const entry of value) {
|
|
92
|
+
if (refs.length >= MESSAGE_REFS_MAX) break;
|
|
93
|
+
if (!isRecord(entry)) continue;
|
|
94
|
+
const kind = entry.kind;
|
|
95
|
+
if (typeof kind !== 'string' || !REF_KINDS.has(kind)) continue;
|
|
96
|
+
const path = readString(entry.path);
|
|
97
|
+
if (path === undefined || path.length === 0 || path.length > MESSAGE_REF_PATH_MAX) continue;
|
|
98
|
+
const ref: MessageRef = { kind: kind as MessageRefKind, path };
|
|
99
|
+
// 0.56:内容地址必须活过重放(与 `packages/agent/src/refs.ts` 同一口径)。
|
|
100
|
+
// 丢在投影里 = 恢复后 refs 没有 sha256 ⇒ 图不内联(`not_ingested`)+ GC 把还在用的
|
|
101
|
+
// blob 当孤儿删掉(design §10 风险 3 的「最阴的一类 bug」)。
|
|
102
|
+
const sha256 = readString(entry.sha256);
|
|
103
|
+
if (sha256 !== undefined && /^[0-9a-f]{64}$/.test(sha256)) ref.sha256 = sha256;
|
|
104
|
+
const name = readString(entry.name);
|
|
105
|
+
if (name !== undefined) ref.name = name;
|
|
106
|
+
const mime = readString(entry.mime);
|
|
107
|
+
if (mime !== undefined) ref.mime = mime;
|
|
108
|
+
const bytes = readCount(entry.bytes);
|
|
109
|
+
if (bytes !== undefined) ref.bytes = bytes;
|
|
110
|
+
refs.push(ref);
|
|
111
|
+
}
|
|
112
|
+
return refs.length > 0 ? refs : undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 宽松校验一条 Message(只信结构,不做业务解释) */
|
|
116
|
+
function asMessage(value: unknown): Message | null {
|
|
117
|
+
if (!isRecord(value)) return null;
|
|
118
|
+
const role = value.role;
|
|
119
|
+
if (role !== 'system' && role !== 'user' && role !== 'assistant' && role !== 'tool') return null;
|
|
120
|
+
const content = typeof value.content === 'string' ? value.content : '';
|
|
121
|
+
const message: Message = { role, content };
|
|
122
|
+
if (Array.isArray(value.tool_calls)) message.tool_calls = value.tool_calls as ToolCall[];
|
|
123
|
+
const toolCallId = readString(value.tool_call_id);
|
|
124
|
+
if (toolCallId !== undefined) message.tool_call_id = toolCallId;
|
|
125
|
+
const name = readString(value.name);
|
|
126
|
+
if (name !== undefined) message.name = name;
|
|
127
|
+
const reasoning = readString(value.reasoning_content);
|
|
128
|
+
if (reasoning !== undefined) message.reasoning_content = reasoning;
|
|
129
|
+
if (typeof value.timestamp === 'number' && Number.isFinite(value.timestamp)) {
|
|
130
|
+
message.timestamp = value.timestamp;
|
|
131
|
+
}
|
|
132
|
+
if (value.compaction_summary === true) message.compaction_summary = true;
|
|
133
|
+
// 附件引用:只有路径与元信息。不补 = 落盘后(重放 / 会话历史)引用凭空消失。
|
|
134
|
+
const refs = asRefs(value.refs);
|
|
135
|
+
if (refs !== undefined) message.refs = refs;
|
|
136
|
+
// 消息身份(additive):事件里的消息载荷可能已经带 `run_id`(`context.changed`);
|
|
137
|
+
// `run_segment` 一律由投影末尾的 `assignMessageIdentities` 统一补,不从载荷里信。
|
|
138
|
+
const runId = readString(value.run_id);
|
|
139
|
+
if (runId !== undefined && runId.length > 0) message.run_id = runId;
|
|
140
|
+
// 消息身份(additive,0.56):`message_id` 是写入侧盖的**事实**,投影必须原样带出去 ——
|
|
141
|
+
// 丢在这里,历史那份就永远没有 id,前端只能退回内容比较(正是要根除的那件事)。
|
|
142
|
+
const messageId = readString(value.message_id);
|
|
143
|
+
if (messageId !== undefined && messageId.length > 0) message.message_id = messageId;
|
|
144
|
+
return message;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function dataOf(event: ProjectableEvent): Record<string, unknown> {
|
|
148
|
+
return isRecord(event.data) ? event.data : {};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function asToolCalls(value: unknown): ToolCall[] | undefined {
|
|
152
|
+
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
153
|
+
const calls: ToolCall[] = [];
|
|
154
|
+
for (const entry of value) {
|
|
155
|
+
if (!isRecord(entry)) continue;
|
|
156
|
+
const id = readString(entry.id);
|
|
157
|
+
const fn = entry.function;
|
|
158
|
+
if (id === undefined || !isRecord(fn)) continue;
|
|
159
|
+
const name = readString(fn.name);
|
|
160
|
+
const args = readString(fn.arguments);
|
|
161
|
+
if (name === undefined || args === undefined) continue;
|
|
162
|
+
calls.push({ id, type: 'function', function: { name, arguments: args } });
|
|
163
|
+
}
|
|
164
|
+
return calls.length > 0 ? calls : undefined;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 给投影出来的消息补 `run_id`(身份取自事件载荷的 `data.run_id`)。
|
|
169
|
+
*
|
|
170
|
+
* 为什么投影也要盖:resume / 崩溃恢复走的是「事件 → 消息」这条重建路径,投影不带身份
|
|
171
|
+
* 就等于刷新之后又没有身份可用(Web 认领退回内容比较 = 本条 bug 复现)。`run_id` 是事件
|
|
172
|
+
* 自带的字段,抄进消息不改任何内容;已带身份的(`data.message.run_id`)不覆盖。
|
|
173
|
+
*/
|
|
174
|
+
function withEventRunId(message: Message, data: Record<string, unknown>): Message {
|
|
175
|
+
const runId = readString(data.run_id);
|
|
176
|
+
return runId === undefined ? message : withRunIdentity(message, runId);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 带出 `llm.response` 载荷里写者盖的 `message_id`(q-frontend-message-dedup 批 1)。
|
|
181
|
+
*
|
|
182
|
+
* assistant 正文段的确定性身份由 loop 发射时盖(`assistantMessageIdOf`);投影是
|
|
183
|
+
* 纯读侧重建,必须把这份事实原样带出去(与 `asMessage` 对 `message_id` 的口径一致),
|
|
184
|
+
* 而不是从段序重算 —— 重算就是消费者自造幂等键(ADR-105 §8 禁止)。旧事件无此字段
|
|
185
|
+
* 时保持缺省,由 `assignMessageIdentities` 新盖段序时补(旧数据已有段序的不动)。
|
|
186
|
+
*/
|
|
187
|
+
function withPayloadMessageId(message: Message, data: Record<string, unknown>): Message {
|
|
188
|
+
const messageId = readString(data.message_id);
|
|
189
|
+
return messageId === undefined || messageId.length === 0
|
|
190
|
+
? message
|
|
191
|
+
: withMessageId(message, messageId);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 投影选项。
|
|
196
|
+
*
|
|
197
|
+
* 只有「清集替换」(把 `message.compaction.cleared_tool_call_ids` 命中的工具结果换成本段
|
|
198
|
+
* 占位文本)是**视图**行为,可以关掉;`kept` 裁尾与 `session.rewound` 截断是**事实**
|
|
199
|
+
* (压缩真的替换掉了旧历史、撤回真的删掉了尾部),永远照做。
|
|
200
|
+
*/
|
|
201
|
+
export interface ProjectMessagesOptions {
|
|
202
|
+
/**
|
|
203
|
+
* 是否应用清集替换(占位正文)。
|
|
204
|
+
*
|
|
205
|
+
* - `true`(缺省):既有行为,投影产物是**请求/展示**口径(占位在正文里)。
|
|
206
|
+
* - `false`:投影产物是**原文**口径 —— 恢复/加载路径必须用这个,否则占位会被
|
|
207
|
+
* `finalizeSessionState` 写回 `SessionState.messages` 并**永久粘住**(原文只剩
|
|
208
|
+
* 事件日志)。模型视图不靠它:请求构建层按本段冻结集合自己替换
|
|
209
|
+
* (`withClearedToolResults`)。
|
|
210
|
+
*/
|
|
211
|
+
applyClearedReplacements?: boolean;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 投影游标:把「事件 → 消息」这件事做成**可续算的一步**(第十一条:增量)。
|
|
216
|
+
*
|
|
217
|
+
* 存在理由(0.55 send-latency):`ExecutionUnit.loadStoredHistory` 每次 run 起点都要
|
|
218
|
+
* 「整份日志 → 消息视图」,0.54 B3 起那次读是**整份读**(58k 事件的会话实测 7.0 s,
|
|
219
|
+
* 占一次发送的 ~92%)。而窗口只会 append,投影本身是**折叠**(fold):上一段事件折出
|
|
220
|
+
* 的消息 + 尾巴那段事件 = 整份投影,逐条相同。
|
|
221
|
+
*
|
|
222
|
+
* 为什么必须放在本模块:投影口径只能有一份。`projectMessages()` 现在就是
|
|
223
|
+
* 「空游标 + 一次 extend」,两条路走同一个循环体,不可能漂。
|
|
224
|
+
*
|
|
225
|
+
* **共享对象的口径**:游标里的消息对象与调用方拿到的、窗口里的消息对象是**同一批**
|
|
226
|
+
* (不深拷贝 —— 一次会话视图 1.3 MB,每次发送复制一遍没有意义)。所以调用方必须把
|
|
227
|
+
* 投影产物当只读:目前唯一的就地写是 `assignMessageIdentities`(`run_segment`,幂等 +
|
|
228
|
+
* 续编),本模块每次 extend 末尾都会再跑一次,所以共享不会造成漂移。
|
|
229
|
+
*/
|
|
230
|
+
export interface ProjectionCursor {
|
|
231
|
+
/** 已折出的消息(顺序 = 事件顺序;与 `projectMessages` 的返回值同一口径)。 */
|
|
232
|
+
readonly messages: Message[];
|
|
233
|
+
/**
|
|
234
|
+
* 与 `messages` **逐位对齐**的「这条消息由哪条事件产出」的 seq(0.56 ctx-recall)。
|
|
235
|
+
*
|
|
236
|
+
* 为什么需要它:压缩索引要回答「被替换的 766 条原来在哪」,而消息对象上没有 seq
|
|
237
|
+
* (`Message` 是对 provider 的形状,不该塞内部寻址信息;加字段还会进落盘壳与 projection
|
|
238
|
+
* 快照)。窗口消息与投影消息是**同一批对象**(见 `ProjectionCursor` 注释),所以在投影
|
|
239
|
+
* 时记一份平行数组,运行时播种窗口时按内容对齐一次,压缩那一刻就查得到。
|
|
240
|
+
*
|
|
241
|
+
* 口径:
|
|
242
|
+
* - 一条 `llm.response` 产出的 assistant ⇒ 那条事件的 seq(`tool.call.started` 只回填实参,
|
|
243
|
+
* **不**改写这个号 —— 消息的身份仍是那次响应);
|
|
244
|
+
* - `message.compaction` 产出的摘要 ⇒ 该压缩事件的 seq;
|
|
245
|
+
* - `session.rewound` 截掉的消息与 seq 一起截(事实是「删尾部」);
|
|
246
|
+
* - 不产出消息的事件不占位。
|
|
247
|
+
*
|
|
248
|
+
* 谁也不许把它当判据做业务决策:它只服务「按区间读回被压缩的历史」这一件事。
|
|
249
|
+
*/
|
|
250
|
+
readonly messageSeqs: (number | undefined)[];
|
|
251
|
+
/** 当前「最后一条 assistant 正文(无 tool_calls)」—— `run.completed.output` 要就地改它。 */
|
|
252
|
+
lastAssistant: Message | undefined;
|
|
253
|
+
/** `tool_call_id` → `tool.call.started` 里的实际 name/arguments(跨段保留)。 */
|
|
254
|
+
readonly toolStarts: Map<string, { name: string; arguments: string }>;
|
|
255
|
+
/** 构造时钉死的清集替换口径(同一游标只服务一种口径)。 */
|
|
256
|
+
readonly applyClearedReplacements: boolean;
|
|
257
|
+
/** 已折叠的最后一条事件的 seq(没有 seq 的事件不推进它)。 */
|
|
258
|
+
seq: number | undefined;
|
|
259
|
+
/** 已折叠的事件条数(= 整份读的 `events.length`)。 */
|
|
260
|
+
count: number;
|
|
261
|
+
/** 最近一次 `llm.response.usage` 的 per-request 总量(旧 `lastRequestTokensFromEvents` 口径)。 */
|
|
262
|
+
lastRequestTokens: number | undefined;
|
|
263
|
+
/** 事件里出现过 `session.rewound`(`reconcileCachedProjection` 的 `replacementInLog`)。 */
|
|
264
|
+
rewoundSeen: boolean;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 一条游标里最多记多少个 tool_call 的实参(`PROJECTION_CURSOR_MAX_TOOL_CALLS`)。
|
|
269
|
+
*
|
|
270
|
+
* 它与消息视图同阶(每条 assistant 的 tool_calls 本来就带同一份实参);超过这个数
|
|
271
|
+
* (极端碎片会话)就不再缓存游标 —— 让内存与落盘快照都有界,代价只是下次整份读。
|
|
272
|
+
*
|
|
273
|
+
* 定义放在本模块(而不是 `runtime/execution-unit.ts`):落盘快照的序列化也要按它判上界,
|
|
274
|
+
* 而 session 层不许反向依赖 runtime。
|
|
275
|
+
*/
|
|
276
|
+
export const PROJECTION_CURSOR_MAX_TOOL_CALLS = 20_000;
|
|
277
|
+
|
|
278
|
+
/** 建一个空游标(`projectMessages` 的起点)。 */
|
|
279
|
+
export function createProjectionCursor(options?: ProjectMessagesOptions): ProjectionCursor {
|
|
280
|
+
return {
|
|
281
|
+
messages: [],
|
|
282
|
+
messageSeqs: [],
|
|
283
|
+
lastAssistant: undefined,
|
|
284
|
+
toolStarts: new Map(),
|
|
285
|
+
applyClearedReplacements: options?.applyClearedReplacements ?? true,
|
|
286
|
+
seq: undefined,
|
|
287
|
+
count: 0,
|
|
288
|
+
lastRequestTokens: undefined,
|
|
289
|
+
rewoundSeen: false,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* 把事件序列投影成 `Message[]`。
|
|
295
|
+
*
|
|
296
|
+
* 事件应已按 `seq` 升序;函数不排序、不读取文件。
|
|
297
|
+
*/
|
|
298
|
+
export function projectMessages(
|
|
299
|
+
events: Iterable<ProjectableEvent>,
|
|
300
|
+
options?: ProjectMessagesOptions,
|
|
301
|
+
): Message[] {
|
|
302
|
+
const cursor = createProjectionCursor(options);
|
|
303
|
+
extendProjection(cursor, events);
|
|
304
|
+
return cursor.messages;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 把一条 `tool.call.started` 的实参回填到**已经建好的** assistant 消息上。
|
|
309
|
+
*
|
|
310
|
+
* 只在一种形状下需要:分段折叠的切点正好落在 `llm.response` 与它的 `tool.call.started`
|
|
311
|
+
* 之间。整份投影的预扫描先收齐全部 started 再建消息,所以那条 assistant 的参数必然被
|
|
312
|
+
* started 覆盖;分段折叠时它已经在上一段建好了,只能回填。
|
|
313
|
+
*
|
|
314
|
+
* 找法:从尾部倒着找第一条**带这个 tool_call id** 的 assistant(id 唯一,命中即停)。
|
|
315
|
+
* 这条路径只在上述罕见切点发生,正常段一次都不会走到(`emittedHere` 已过滤)。
|
|
316
|
+
*/
|
|
317
|
+
function backfillToolCallStarts(
|
|
318
|
+
messages: readonly Message[],
|
|
319
|
+
toolCallId: string,
|
|
320
|
+
started: { name: string; arguments: string } | undefined,
|
|
321
|
+
): void {
|
|
322
|
+
if (started === undefined) return;
|
|
323
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
324
|
+
const message = messages[index];
|
|
325
|
+
if (message === undefined || message.role !== 'assistant') continue;
|
|
326
|
+
let hit = false;
|
|
327
|
+
for (const call of message.tool_calls ?? []) {
|
|
328
|
+
if (call.id !== toolCallId) continue;
|
|
329
|
+
call.function = { name: started.name, arguments: started.arguments };
|
|
330
|
+
hit = true;
|
|
331
|
+
}
|
|
332
|
+
if (hit) return;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* 把一段(**必须接在游标之后**的)事件折进游标;结果与「整份重投影」逐条相同。
|
|
338
|
+
*
|
|
339
|
+
* 调用方负责保证这段事件紧接 `cursor.seq`(连续性 / 到达日志头这两件事由读侧判,
|
|
340
|
+
* 见 `ExecutionUnit.loadStoredHistory`)——本函数不做猜测:喂进来什么就折什么。
|
|
341
|
+
*/
|
|
342
|
+
export function extendProjection(
|
|
343
|
+
cursor: ProjectionCursor,
|
|
344
|
+
events: Iterable<ProjectableEvent>,
|
|
345
|
+
): void {
|
|
346
|
+
const applyClearedReplacements = cursor.applyClearedReplacements;
|
|
347
|
+
const list = [...events];
|
|
348
|
+
cursor.count += list.length;
|
|
349
|
+
/** 本段里 `tool.call.started` 带来的 id(回填只需要看这一批,不看全部历史)。 */
|
|
350
|
+
const startedHere: string[] = [];
|
|
351
|
+
|
|
352
|
+
// 先收集工具调用的实际执行参数:guardrail / hook 可能改写 name/arguments。
|
|
353
|
+
// 跨段保留:一个 `llm.response` 的 `tool.call.started` 可能不在同一段里。
|
|
354
|
+
for (const event of list) {
|
|
355
|
+
if (typeof event.seq === 'number' && Number.isFinite(event.seq)) {
|
|
356
|
+
cursor.seq = cursor.seq === undefined ? event.seq : Math.max(cursor.seq, event.seq);
|
|
357
|
+
}
|
|
358
|
+
if (event.type === 'session.rewound') cursor.rewoundSeen = true;
|
|
359
|
+
if (event.type === 'llm.response') {
|
|
360
|
+
const usage = dataOf(event).usage;
|
|
361
|
+
if (isRecord(usage)) {
|
|
362
|
+
const prompt = typeof usage.prompt_tokens === 'number' ? usage.prompt_tokens : 0;
|
|
363
|
+
const completion =
|
|
364
|
+
typeof usage.completion_tokens === 'number' ? usage.completion_tokens : 0;
|
|
365
|
+
if (prompt + completion > 0) cursor.lastRequestTokens = prompt + completion;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (event.type !== 'tool.call.started') continue;
|
|
369
|
+
const data = dataOf(event);
|
|
370
|
+
const id = readString(data.tool_call_id);
|
|
371
|
+
const name = readString(data.tool_name);
|
|
372
|
+
if (id === undefined || name === undefined) continue;
|
|
373
|
+
const args = data.arguments;
|
|
374
|
+
startedHere.push(id);
|
|
375
|
+
cursor.toolStarts.set(id, {
|
|
376
|
+
name,
|
|
377
|
+
// started 事件里非法 JSON 时保留原字符串;projection 只保证结构一致。
|
|
378
|
+
arguments: typeof args === 'string' ? args : JSON.stringify(args ?? {}),
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const toolStarts = cursor.toolStarts;
|
|
383
|
+
const messages = cursor.messages;
|
|
384
|
+
/** 与 `messages` 平行的 seq 记录(口径见 `ProjectionCursor.messageSeqs`)。 */
|
|
385
|
+
const messageSeqs = cursor.messageSeqs;
|
|
386
|
+
let lastAssistant = cursor.lastAssistant;
|
|
387
|
+
/**
|
|
388
|
+
* 本段里**新写出**的 tool_call id。
|
|
389
|
+
*
|
|
390
|
+
* 用途只有一个:判断「有没有一条 started 事件属于**上一段**已经落成的 assistant」。
|
|
391
|
+
* 整份投影的预扫描把**全部** started 事件先收齐再构造消息,所以一条 `llm.response`
|
|
392
|
+
* 的参数总是被它后面的 `tool.call.started` 覆盖。分段折叠时,如果切点正好落在
|
|
393
|
+
* `llm.response` 与它的 `tool.call.started` 之间,那条 assistant 在上一段就已经建好了,
|
|
394
|
+
* 预扫描的新条目没有机会作用到它 —— 于是本段末尾补一次回填(见 `backfillStarts`)。
|
|
395
|
+
*/
|
|
396
|
+
const emittedHere = new Set<string>();
|
|
397
|
+
|
|
398
|
+
for (const event of list) {
|
|
399
|
+
const data = dataOf(event);
|
|
400
|
+
if (event.type === 'user.message') {
|
|
401
|
+
const message = withEventRunId(
|
|
402
|
+
asMessage(data.message) ?? {
|
|
403
|
+
role: 'user',
|
|
404
|
+
content: readString(data.content) ?? '',
|
|
405
|
+
},
|
|
406
|
+
data,
|
|
407
|
+
);
|
|
408
|
+
message.timestamp ??= event.ts;
|
|
409
|
+
messages.push(message);
|
|
410
|
+
messageSeqs.push(event.seq);
|
|
411
|
+
lastAssistant = undefined;
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
if (event.type === 'message.injected') {
|
|
415
|
+
const raw = asMessage(data.message);
|
|
416
|
+
if (raw !== null) {
|
|
417
|
+
const message = withEventRunId(raw, data);
|
|
418
|
+
message.timestamp ??= event.ts;
|
|
419
|
+
messages.push(message);
|
|
420
|
+
messageSeqs.push(event.seq);
|
|
421
|
+
lastAssistant = undefined;
|
|
422
|
+
}
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
if (event.type === 'session.rewound') {
|
|
426
|
+
// 0.37:撤回是「删除尾部」事实。`keep_projected` 是投影空间下标;
|
|
427
|
+
// 旧事件缺该字段时退回 `keep_messages`(两者在无 base system 时相同)。
|
|
428
|
+
const keepProjected = readCount(data.keep_projected) ?? readCount(data.keep_messages);
|
|
429
|
+
if (keepProjected !== undefined && messages.length > keepProjected) {
|
|
430
|
+
messages.length = keepProjected;
|
|
431
|
+
// 删尾部是事实:seq 记录跟着截,否则两份数组会错位(错位比缺 seq 更糟)。
|
|
432
|
+
messageSeqs.length = Math.min(messageSeqs.length, keepProjected);
|
|
433
|
+
}
|
|
434
|
+
lastAssistant = undefined;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (event.type === 'message.compaction') {
|
|
438
|
+
const message = asMessage(data.message) ?? {
|
|
439
|
+
role: 'system' as const,
|
|
440
|
+
content: readString(data.summary) ?? '',
|
|
441
|
+
compaction_summary: true,
|
|
442
|
+
};
|
|
443
|
+
message.timestamp ??= event.ts;
|
|
444
|
+
// 0.34 S1:先按 cleared_tool_call_ids 把已构建消息里命中的 tool 结果
|
|
445
|
+
// 换回确定性占位(与压缩时同一函数),再按 `kept` 裁剪。
|
|
446
|
+
// 已是占位的保持幂等;旧事件没有该字段时整段跳过。
|
|
447
|
+
//
|
|
448
|
+
// 0.54:这一步是**视图**(模型会看到什么),不是事实 —— 加载/恢复路径传
|
|
449
|
+
// `applyClearedReplacements: false` 跳过它,让 `SessionState.messages` 保持原文。
|
|
450
|
+
// 注意 `kept` 裁剪(下面一段)**不在**这个开关里:那是压缩真的替换掉了旧历史。
|
|
451
|
+
const clearedIds = applyClearedReplacements
|
|
452
|
+
? readStringArray(data.cleared_tool_call_ids)
|
|
453
|
+
: [];
|
|
454
|
+
if (clearedIds.length > 0) {
|
|
455
|
+
const wanted = new Set(clearedIds);
|
|
456
|
+
for (const entry of messages) {
|
|
457
|
+
if (entry.role !== 'tool') continue;
|
|
458
|
+
const id = entry.tool_call_id;
|
|
459
|
+
if (id === undefined || !wanted.has(id)) continue;
|
|
460
|
+
if (!entry.content || isClearedToolResult(entry.content)) continue;
|
|
461
|
+
entry.content = formatClearedToolResult(messages, id, entry.content);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
// 压缩是「替换」而不是「追加」:应用侧保留全部 system + 最近 `kept`
|
|
465
|
+
// 条非 system,并移除旧摘要。没有 `kept` 的旧事件只追加摘要。
|
|
466
|
+
const kept = readCount(data.kept);
|
|
467
|
+
if (kept === undefined) {
|
|
468
|
+
messages.push(message);
|
|
469
|
+
messageSeqs.push(event.seq);
|
|
470
|
+
} else {
|
|
471
|
+
// 按下标重建两份数组(消息 + seq):先按 `prior` 的**下标**分类,
|
|
472
|
+
// 再一次性重排,`messages` 的产物与旧实现逐条相同。
|
|
473
|
+
const keptSeqs: (number | undefined)[] = [];
|
|
474
|
+
const systemIndexes: number[] = [];
|
|
475
|
+
const nonSystemIndexes: number[] = [];
|
|
476
|
+
for (let index = 0; index < messages.length; index += 1) {
|
|
477
|
+
const entry = messages[index];
|
|
478
|
+
if (entry === undefined || entry.compaction_summary === true) continue;
|
|
479
|
+
if (entry.role === 'system') systemIndexes.push(index);
|
|
480
|
+
else nonSystemIndexes.push(index);
|
|
481
|
+
}
|
|
482
|
+
const tailIndexes =
|
|
483
|
+
kept >= nonSystemIndexes.length
|
|
484
|
+
? nonSystemIndexes
|
|
485
|
+
: nonSystemIndexes.slice(nonSystemIndexes.length - kept);
|
|
486
|
+
const nextMessages: Message[] = [];
|
|
487
|
+
for (const index of systemIndexes) {
|
|
488
|
+
const entry = messages[index];
|
|
489
|
+
if (entry !== undefined) {
|
|
490
|
+
nextMessages.push(entry);
|
|
491
|
+
keptSeqs.push(messageSeqs[index]);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
nextMessages.push(message);
|
|
495
|
+
keptSeqs.push(event.seq);
|
|
496
|
+
for (const index of tailIndexes) {
|
|
497
|
+
const entry = messages[index];
|
|
498
|
+
if (entry === undefined) continue;
|
|
499
|
+
nextMessages.push(entry);
|
|
500
|
+
keptSeqs.push(messageSeqs[index]);
|
|
501
|
+
}
|
|
502
|
+
messages.length = 0;
|
|
503
|
+
messageSeqs.length = 0;
|
|
504
|
+
messages.push(...nextMessages);
|
|
505
|
+
messageSeqs.push(...keptSeqs);
|
|
506
|
+
}
|
|
507
|
+
lastAssistant = undefined;
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (event.type === 'llm.response') {
|
|
511
|
+
const content = readString(data.content) ?? '';
|
|
512
|
+
const reasoning = readString(data.reasoning);
|
|
513
|
+
const toolCalls = asToolCalls(data.tool_calls);
|
|
514
|
+
if (toolCalls) {
|
|
515
|
+
const effective = toolCalls.map((call) => {
|
|
516
|
+
emittedHere.add(call.id);
|
|
517
|
+
const started = toolStarts.get(call.id);
|
|
518
|
+
if (!started) return call;
|
|
519
|
+
return {
|
|
520
|
+
...call,
|
|
521
|
+
function: { name: started.name, arguments: started.arguments },
|
|
522
|
+
};
|
|
523
|
+
});
|
|
524
|
+
const assistant = withPayloadMessageId(
|
|
525
|
+
withEventRunId(
|
|
526
|
+
{
|
|
527
|
+
role: 'assistant',
|
|
528
|
+
content,
|
|
529
|
+
tool_calls: effective,
|
|
530
|
+
timestamp: event.ts,
|
|
531
|
+
...(reasoning ? { reasoning_content: reasoning } : {}),
|
|
532
|
+
},
|
|
533
|
+
data,
|
|
534
|
+
),
|
|
535
|
+
data,
|
|
536
|
+
);
|
|
537
|
+
messages.push(assistant);
|
|
538
|
+
messageSeqs.push(event.seq);
|
|
539
|
+
lastAssistant = undefined;
|
|
540
|
+
} else {
|
|
541
|
+
const assistant = withPayloadMessageId(
|
|
542
|
+
withEventRunId(
|
|
543
|
+
{
|
|
544
|
+
role: 'assistant',
|
|
545
|
+
content,
|
|
546
|
+
timestamp: event.ts,
|
|
547
|
+
...(reasoning ? { reasoning_content: reasoning } : {}),
|
|
548
|
+
},
|
|
549
|
+
data,
|
|
550
|
+
),
|
|
551
|
+
data,
|
|
552
|
+
);
|
|
553
|
+
messages.push(assistant);
|
|
554
|
+
messageSeqs.push(event.seq);
|
|
555
|
+
lastAssistant = assistant;
|
|
556
|
+
}
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
if (event.type === 'tool.call.completed') {
|
|
560
|
+
const id = readString(data.tool_call_id);
|
|
561
|
+
if (id === undefined) continue;
|
|
562
|
+
messages.push(
|
|
563
|
+
withEventRunId(
|
|
564
|
+
{
|
|
565
|
+
role: 'tool',
|
|
566
|
+
tool_call_id: id,
|
|
567
|
+
content: readString(data.result) ?? '',
|
|
568
|
+
timestamp: event.ts,
|
|
569
|
+
},
|
|
570
|
+
data,
|
|
571
|
+
),
|
|
572
|
+
);
|
|
573
|
+
messageSeqs.push(event.seq);
|
|
574
|
+
lastAssistant = undefined;
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
if (event.type === 'tool.call.failed') {
|
|
578
|
+
const id = readString(data.tool_call_id);
|
|
579
|
+
if (id === undefined) continue;
|
|
580
|
+
const content =
|
|
581
|
+
readString(data.content) ?? `Error: ${readString(data.error) ?? 'tool call failed'}`;
|
|
582
|
+
messages.push(
|
|
583
|
+
withEventRunId(
|
|
584
|
+
{
|
|
585
|
+
role: 'tool',
|
|
586
|
+
tool_call_id: id,
|
|
587
|
+
content,
|
|
588
|
+
timestamp: event.ts,
|
|
589
|
+
},
|
|
590
|
+
data,
|
|
591
|
+
),
|
|
592
|
+
);
|
|
593
|
+
messageSeqs.push(event.seq);
|
|
594
|
+
lastAssistant = undefined;
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
if (event.type === 'run.completed') {
|
|
598
|
+
const output = readString(data.output);
|
|
599
|
+
// 输出 guardrail 可能改写最终文本;以终态 output 为准。
|
|
600
|
+
if (output !== undefined && lastAssistant !== undefined) {
|
|
601
|
+
lastAssistant.content = output;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
cursor.lastAssistant = lastAssistant;
|
|
607
|
+
// 两条数组必须等长:漏记一处 seq 会让后续的「按区间读回」整体错位(错位比缺 seq 更糟)。
|
|
608
|
+
// 短了补空位(`undefined` = 这条消息追不到 seq),长了截掉。
|
|
609
|
+
if (messageSeqs.length !== messages.length) messageSeqs.length = messages.length;
|
|
610
|
+
// 切点落在一对「response / 它的 started」之间时,回填那条 assistant 的实参
|
|
611
|
+
// —— 整份投影的预扫描本来就会覆盖它,这里让分段折叠与它逐条相同。
|
|
612
|
+
for (const id of startedHere) {
|
|
613
|
+
if (emittedHere.has(id)) continue;
|
|
614
|
+
backfillToolCallStarts(messages, id, toolStarts.get(id));
|
|
615
|
+
}
|
|
616
|
+
// 消息身份(additive):`run_id` 上面已按事件补齐,段序在这里统一补 —— 与落盘漏斗
|
|
617
|
+
// (`RunJournal.saveSessionState`)**同一个函数、同一个口径**,两条重建路径才认得出
|
|
618
|
+
// 同一段正文。旧事件没有 `run_id` 时这两个字段保持缺省(消费端退回内容比较)。
|
|
619
|
+
//
|
|
620
|
+
// 增量续算也照跑:`assignMessageIdentities` 幂等(已盖章的只抬水位),对「上一段
|
|
621
|
+
// 已经盖过章的消息 + 本段新消息」跑一遍,结果与整份重投影后跑一遍逐条相同。
|
|
622
|
+
assignMessageIdentities(cursor.messages);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* 0.37:`SessionState.messages` 下标 → `projectMessages` 投影空间下标。
|
|
627
|
+
*
|
|
628
|
+
* `projectMessages` 不产生 base system prompt;当 `messages[0]` 是非压缩 system
|
|
629
|
+
* 时它只存在于缓存,投影下标比 state 下标小 1。其余消息 1:1。
|
|
630
|
+
*/
|
|
631
|
+
export function projectedKeepCount(messages: readonly Message[], keep: number): number {
|
|
632
|
+
const first = messages[0];
|
|
633
|
+
const hasBaseSystem =
|
|
634
|
+
first !== undefined && first.role === 'system' && first.compaction_summary !== true;
|
|
635
|
+
return Math.max(0, keep - (hasBaseSystem ? 1 : 0));
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* 给一批消息补上「它由哪条事件产出」的 seq(0.56 ctx-recall)。
|
|
640
|
+
*
|
|
641
|
+
* 输入的三份东西口径不同,对齐**只能按内容**:
|
|
642
|
+
* - `target`:调用方手里的那份(窗口 / `SessionState.messages` / 对账后的历史)。
|
|
643
|
+
* 它是缓存对象或 `structuredClone` 出来的副本 ⇒ **对象身份对不上**,只能按签名对;
|
|
644
|
+
* - `projected`:游标里的投影消息(顺序 = 事件顺序);
|
|
645
|
+
* - `seqs`:与 `projected` 逐位对齐的 seq 记录(`ProjectionCursor.messageSeqs`)。
|
|
646
|
+
*
|
|
647
|
+
* 对齐规则与 `coverageCut` 同口径:两条指针都只前进,同签名的重复消息(比如两条
|
|
648
|
+
* 内容相同的工具结果)按出现顺序一一配对,不会把后一条配到前一条上。
|
|
649
|
+
* 配不上的 `target` 消息得到 `undefined`(缓存独有的基底 system / 运行时提示行,
|
|
650
|
+
* 它们本来就不是事件投影的产物)——**不许猜**一个相邻 seq:错位的区间读出来是别人的历史。
|
|
651
|
+
*
|
|
652
|
+
* 返回值与 `target` 等长、逐位对齐。纯函数,不读盘。
|
|
653
|
+
*/
|
|
654
|
+
export function alignMessageSeqs(
|
|
655
|
+
target: readonly Message[],
|
|
656
|
+
projected: readonly Message[],
|
|
657
|
+
seqs: readonly (number | undefined)[],
|
|
658
|
+
): (number | undefined)[] {
|
|
659
|
+
const positions = new Map<string, number[]>();
|
|
660
|
+
for (let index = 0; index < projected.length; index += 1) {
|
|
661
|
+
const message = projected[index];
|
|
662
|
+
if (message === undefined) continue;
|
|
663
|
+
const signature = messageSignature(message);
|
|
664
|
+
const list = positions.get(signature);
|
|
665
|
+
if (list === undefined) positions.set(signature, [index]);
|
|
666
|
+
else list.push(index);
|
|
667
|
+
}
|
|
668
|
+
/** 每个签名已经用掉到第几个候选(单调前进,避免同签名消息互相抢占)。 */
|
|
669
|
+
const used = new Map<string, number>();
|
|
670
|
+
const aligned: (number | undefined)[] = [];
|
|
671
|
+
let searchFrom = 0;
|
|
672
|
+
for (const message of target) {
|
|
673
|
+
const signature = messageSignature(message);
|
|
674
|
+
const list = positions.get(signature);
|
|
675
|
+
if (list === undefined) {
|
|
676
|
+
aligned.push(undefined);
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
let pointer = used.get(signature) ?? 0;
|
|
680
|
+
while (pointer < list.length && (list[pointer] as number) < searchFrom) pointer += 1;
|
|
681
|
+
const found = pointer < list.length ? list[pointer] : undefined;
|
|
682
|
+
if (found === undefined) {
|
|
683
|
+
used.set(signature, pointer);
|
|
684
|
+
aligned.push(undefined);
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
used.set(signature, pointer + 1);
|
|
688
|
+
searchFrom = found + 1;
|
|
689
|
+
aligned.push(seqs[found]);
|
|
690
|
+
}
|
|
691
|
+
return aligned;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* 消息内容签名(忽略 timestamp:事件时间与上下文写入时间会有毫秒差)。
|
|
696
|
+
*
|
|
697
|
+
* **也忽略 `run_id` / `run_segment`**(0.51):那是身份不是内容。旧缓存可能没有它们、
|
|
698
|
+
* 新投影有(或反之),把身份折进签名会让「缓存 ⟺ 投影」的尾部重叠匹配整体失配 ——
|
|
699
|
+
* 后果是同一段历史被追加两遍,比丢了身份更糟。
|
|
700
|
+
*/
|
|
701
|
+
function messageSignature(message: Message): string {
|
|
702
|
+
return [
|
|
703
|
+
message.role,
|
|
704
|
+
message.content,
|
|
705
|
+
message.tool_call_id ?? '',
|
|
706
|
+
message.name ?? '',
|
|
707
|
+
(message.tool_calls ?? [])
|
|
708
|
+
.map((call) => `${call.id}:${call.function.name}:${call.function.arguments}`)
|
|
709
|
+
.join('|'),
|
|
710
|
+
message.compaction_summary === true ? '1' : '0',
|
|
711
|
+
// 引用也是内容的一部分:两条只差 refs 的消息不能算同一条(否则增量合并会丢引用)。
|
|
712
|
+
(message.refs ?? []).map((ref) => `${ref.kind}:${ref.path}`).join('|'),
|
|
713
|
+
].join('\u0000');
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* 「会话壳里的缓存」⇄「事件日志的纯投影」对账(0.54 B3)。
|
|
718
|
+
*
|
|
719
|
+
* **这条函数不许读会话壳里的任何游标字段**(`derived_upto_seq` / `projection_cache_seq`
|
|
720
|
+
* 都不许)。为什么:那个数一旦参与判定,它就从「缓存覆盖到哪的提示」变回「事实水位」,
|
|
721
|
+
* 于是「壳落后于日志」时整段尾部事实对人不可见(P7:`derived=4 < last_seq=366` ⇒ 刷新
|
|
722
|
+
* 只剩 4 条),「壳说覆盖到 1e7」时撤回/压缩就不再生效。判据见
|
|
723
|
+
* `scripts/check-projection-cache-agnostic.ts`:把这个字段删掉 / 灌毒之后,本函数的
|
|
724
|
+
* 输出必须逐字节不变。
|
|
725
|
+
*
|
|
726
|
+
* **为什么不是「一律用投影」**:事件日志里的载荷是**脱敏之前**的那一份
|
|
727
|
+
* (`context.changed` / `tool.call.completed` 发的是 `identified`,脱敏发生在
|
|
728
|
+
* `ExecutionUnit#maskForAppend`,即写上下文那一刻)。投影直接顶替缓存会把真值/未中和的
|
|
729
|
+
* 同形 token 重新带回 store 与请求(`check-pii-vault` 的「历史消息逐条未改」当场红)。
|
|
730
|
+
* 所以缓存在**它已经覆盖的范围内是内容权威**,日志只用于「补出缓存还没有的尾部」。
|
|
731
|
+
*
|
|
732
|
+
* 规则(顺序即语义):
|
|
733
|
+
*
|
|
734
|
+
* 0. **替换语义先判**:投影头部是一条**缓存里没有的**压缩摘要 ⇒ 中间一定发生过压缩
|
|
735
|
+
* (压缩把历史换成一条摘要,摘要必然落在投影头部)⇒ 缓存正文作废、只留基底 system,
|
|
736
|
+
* 正文全部取投影。日志里出现 `session.rewound` 时同理(撤回 = 删尾部事实,缓存里那几条
|
|
737
|
+
* 必须消失)。这两条都只看**日志的形状与内容**,不看任何游标。
|
|
738
|
+
* 1. **对齐点按内容算**:把缓存里每条「非缓存专属」消息在投影里按顺序找一遍(跳过投影里
|
|
739
|
+
* 多出来的那条 —— 它可能是脱敏 / guardrail 改写过的同一句)。全部找到 ⇒ 覆盖到
|
|
740
|
+
* 「最后一条命中位置 + 1」;有**一条**找不到 ⇒ 缓存与事实无法对齐(外来缓存 / 事件里
|
|
741
|
+
* 根本没有它),此时**不动缓存**(宁可不补,也不改写已有正文:append-only 是硬约束)。
|
|
742
|
+
* 2. **补尾部**:`projected.slice(cut)` 整段追加。这是 P7 的修复点:壳落后时尾部事实照旧
|
|
743
|
+
* 可见,且**前缀一个字节都不变**(落盘序列因此是 append-only)。
|
|
744
|
+
* 3. 缓存专属消息(基底 system / 带 `name` 的运行时提示)永远留在原位:它们不是事件投影
|
|
745
|
+
* 的产物,丢了等于让用户可见的卡片在重启后消失。
|
|
746
|
+
*
|
|
747
|
+
* 幂等性:对「结果」再跑一遍结果不变(对齐点已到投影尾部 ⇒ 追加空段)。
|
|
748
|
+
*/
|
|
749
|
+
export function reconcileCachedProjection(
|
|
750
|
+
cached: readonly Message[],
|
|
751
|
+
projected: readonly Message[],
|
|
752
|
+
options?: { replacementInLog?: boolean },
|
|
753
|
+
): Message[] {
|
|
754
|
+
// 0. 替换语义:日志里有撤回、或投影头部是一条**缓存里没有的**压缩摘要 ⇒ 缓存正文作废
|
|
755
|
+
// (基底 system 除外)。只看日志与内容,不看任何游标。
|
|
756
|
+
if (options?.replacementInLog === true || compactionUnseenByCache(cached, projected)) {
|
|
757
|
+
return [...cached.filter(isBaseSystemMessage), ...projected];
|
|
758
|
+
}
|
|
759
|
+
// 1. 对齐点(内容算,不看游标)。
|
|
760
|
+
const cut = coverageCut(cached, projected);
|
|
761
|
+
// 2. 对不上 ⇒ 保守:不动缓存(改写已有正文等于回改历史,PII 与 append-only 都破)。
|
|
762
|
+
if (cut === undefined) return [...cached];
|
|
763
|
+
// 3. 补尾部(前缀逐字不变)。
|
|
764
|
+
if (cut >= projected.length) return [...cached];
|
|
765
|
+
return [...cached, ...projected.slice(cut)];
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* 投影头部是不是一条**缓存里没有的**压缩摘要。
|
|
770
|
+
*
|
|
771
|
+
* 为什么这条能精确识别「压缩把历史换掉了」:`message.compaction` 的投影会把摘要放在
|
|
772
|
+
* **最前面**(它是替换语义),所以「投影头是摘要、缓存里却没有它」只可能是「壳停在压缩
|
|
773
|
+
* 之前」。反过来,缓存头与投影头对不上还有别的正当原因(壳里的种子消息从来不是事件、
|
|
774
|
+
* append 侧脱敏改写过正文…)—— 那些**不是**替换,不许据此丢缓存正文。
|
|
775
|
+
*/
|
|
776
|
+
function compactionUnseenByCache(
|
|
777
|
+
cached: readonly Message[],
|
|
778
|
+
projected: readonly Message[],
|
|
779
|
+
): boolean {
|
|
780
|
+
const head = projected[0];
|
|
781
|
+
if (head === undefined) return false;
|
|
782
|
+
if (head.role !== 'system' || head.compaction_summary !== true) return false;
|
|
783
|
+
const signature = messageSignature(head);
|
|
784
|
+
return !cached.some((message) => messageSignature(message) === signature);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* 缓存已经覆盖到投影的第几条(返回该下标;`0` = 一条都没覆盖)。
|
|
789
|
+
*
|
|
790
|
+
* 缓存没有可比正文(只有基底 system / 暂无历史)⇒ 返回 `0`(投影整段都是新的)。
|
|
791
|
+
* 有任何一条缓存正文在投影里找不到 ⇒ 返回 `undefined`(无法对齐,调用方不许改缓存)。
|
|
792
|
+
* 复杂度:签名各算一次,两个指针单调前进(O(n + m))—— 会话可以有几万条事件。
|
|
793
|
+
*
|
|
794
|
+
* **带 `name` 的消息是「尽力对齐」,不是「一律跳过」**(0.56 修「落盘两份」的根)。
|
|
795
|
+
*
|
|
796
|
+
* 旧实现把「带 name」直接当成 `isCacheOnlyMessage` 跳过(`continue`),于是只要壳里的
|
|
797
|
+
* 正文整段都带 name,就没有一条参与对齐 ⇒ 返回 0 ⇒ 调用方把**整份投影**追加到壳后面。
|
|
798
|
+
* 壳里那几条(`vessel:source` 来源行 + 这一轮的输入本身)在投影里也有,结果就是同一条
|
|
799
|
+
* 输入写了两遍。触发条件很窄但完全真实:
|
|
800
|
+
*
|
|
801
|
+
* 1. A2A 投递(`agent:<alias>`)/ async ask 答案(`ask:answer`)/ rotate 交接嘱托的输入
|
|
802
|
+
* **本身带 name**(`inputNameOf`),来源行也带 name;
|
|
803
|
+
* 2. run 在**初始保存**(输入已进壳、assistant 还没有)之后被硬杀 / 超时 —— 壳就停在
|
|
804
|
+
* 「全是带 name 的消息」这个形状;
|
|
805
|
+
* 3. 下一次 run 的 `loadStoredHistory` 对账 ⇒ 旧实现 cut=0 ⇒ 整份投影再追加一遍。
|
|
806
|
+
*
|
|
807
|
+
* 判据回到「投影能不能产出它」这件事本身:带 name 的消息**也可能**是投影产物
|
|
808
|
+
* (`user.message` / `message.injected` 的事件载荷本来就带 name),所以先在投影里找;
|
|
809
|
+
* 只在**找不到**时才按缓存专属跳过(`appendNoticeToSnapshot` 那类不落事件的提示)。
|
|
810
|
+
* 找不到的**不具名正文**仍然返回 `undefined`(保守:无法对齐就不动缓存)。
|
|
811
|
+
*/
|
|
812
|
+
function coverageCut(
|
|
813
|
+
cached: readonly Message[],
|
|
814
|
+
projected: readonly Message[],
|
|
815
|
+
): number | undefined {
|
|
816
|
+
const projectedSignatures = projected.map((message) => messageSignature(message));
|
|
817
|
+
const cursorBySignature = new Map<string, number[]>();
|
|
818
|
+
for (let index = 0; index < projectedSignatures.length; index += 1) {
|
|
819
|
+
const signature = projectedSignatures[index];
|
|
820
|
+
if (signature === undefined) continue;
|
|
821
|
+
const list = cursorBySignature.get(signature);
|
|
822
|
+
if (list === undefined) cursorBySignature.set(signature, [index]);
|
|
823
|
+
else list.push(index);
|
|
824
|
+
}
|
|
825
|
+
let covered = 0;
|
|
826
|
+
let searchFrom = 0;
|
|
827
|
+
let matched = false;
|
|
828
|
+
for (const message of cached) {
|
|
829
|
+
// 基底 system prompt 是缓存独有的(投影永不产出):它不占投影位置,直接跳过。
|
|
830
|
+
if (isBaseSystemMessage(message)) continue;
|
|
831
|
+
const positions = cursorBySignature.get(messageSignature(message));
|
|
832
|
+
// 从当前位置往后找第一条还没用过的同签名投影消息(顺序对齐,不回头)。
|
|
833
|
+
let found: number | undefined;
|
|
834
|
+
for (const position of positions ?? []) {
|
|
835
|
+
if (position < searchFrom) continue;
|
|
836
|
+
found = position;
|
|
837
|
+
break;
|
|
838
|
+
}
|
|
839
|
+
if (found === undefined) {
|
|
840
|
+
// 投影里有它 ⇒ 上面已经对齐;这里只剩「投影里没有」两种情况:
|
|
841
|
+
// - 带 name 的运行时提示(`appendNoticeToSnapshot` 不落事件):缓存专属,跳过;
|
|
842
|
+
// - 不具名正文:对齐不了,交给调用方保守处理(不动缓存)。
|
|
843
|
+
if (isCacheOnlyMessage(message)) continue;
|
|
844
|
+
return undefined;
|
|
845
|
+
}
|
|
846
|
+
covered = found + 1;
|
|
847
|
+
searchFrom = found + 1;
|
|
848
|
+
matched = true;
|
|
849
|
+
}
|
|
850
|
+
return matched ? covered : 0;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/** 基底 system prompt(非压缩摘要):事件投影永远产不出它。 */
|
|
854
|
+
function isBaseSystemMessage(message: Message): boolean {
|
|
855
|
+
return message.role === 'system' && message.compaction_summary !== true;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* 这条消息是不是**只可能来自缓存**(事件投影永远产不出来)。
|
|
860
|
+
*
|
|
861
|
+
* 两类,都是 `projectMessages` 的产物集之外的:
|
|
862
|
+
* - 基底 system prompt(压缩摘要不算:那是 `message.compaction` 事件的投影);
|
|
863
|
+
* - 带 `name` 的运行时提示行(`vessel:notice` 环境卡 / limit / source / hook / subscription)。
|
|
864
|
+
*
|
|
865
|
+
* 只在对齐**找不到**时被 `coverageCut` 查询:「带 name」不足以证明它不来自事件
|
|
866
|
+
* (`user.message` / `message.injected` 的载荷本来就带 name),必须先按内容找一遍。
|
|
867
|
+
*/
|
|
868
|
+
function isCacheOnlyMessage(message: Message): boolean {
|
|
869
|
+
if (isBaseSystemMessage(message)) return true;
|
|
870
|
+
return message.role !== 'system' && message.name !== undefined && message.name.length > 0;
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* 合并「session 缓存」与「事件全量投影」(0.18)。
|
|
875
|
+
*
|
|
876
|
+
* 用于旧数据没有 `derived_upto_seq`(无法只重放增量)时:
|
|
877
|
+
* 找到缓存尾部与投影头部的最大重叠 k,只追加 `projected.slice(k)`;
|
|
878
|
+
* 系统消息与压缩摘要只来自缓存(事件日志不含它们)。
|
|
879
|
+
*
|
|
880
|
+
* 无任何重叠时:缓存不短于投影 → 认为缓存更新,原样返回;否则拼接
|
|
881
|
+
* (宁可多一条也不丢历史)。
|
|
882
|
+
*
|
|
883
|
+
* @deprecated 0.54 B3:读路径已改用 [`reconcileCachedProjection`](它不依赖任何游标
|
|
884
|
+
* 字段、且壳落后时以日志为准)。本函数保留只为不破坏已发布的导出;新代码不要用。
|
|
885
|
+
*/
|
|
886
|
+
export function mergeHistoryProjection(cached: Message[], projected: Message[]): Message[] {
|
|
887
|
+
if (projected.length === 0) return cached;
|
|
888
|
+
const cachedBody = cached.filter((message) => message.role !== 'system');
|
|
889
|
+
const max = Math.min(cachedBody.length, projected.length);
|
|
890
|
+
for (let overlap = max; overlap > 0; overlap -= 1) {
|
|
891
|
+
const tail = cachedBody.slice(cachedBody.length - overlap);
|
|
892
|
+
let matches = true;
|
|
893
|
+
for (let index = 0; index < overlap; index += 1) {
|
|
894
|
+
const cachedMessage = tail[index];
|
|
895
|
+
const projectedMessage = projected[index];
|
|
896
|
+
if (
|
|
897
|
+
cachedMessage === undefined ||
|
|
898
|
+
projectedMessage === undefined ||
|
|
899
|
+
messageSignature(cachedMessage) !== messageSignature(projectedMessage)
|
|
900
|
+
) {
|
|
901
|
+
matches = false;
|
|
902
|
+
break;
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
if (matches) return [...cached, ...projected.slice(overlap)];
|
|
906
|
+
}
|
|
907
|
+
return cachedBody.length >= projected.length ? cached : [...cached, ...projected];
|
|
908
|
+
}
|