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,1362 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 交互注册表(0.27):从已删除的执行引擎中搬出的权限 / ask 职责。
|
|
3
|
+
*
|
|
4
|
+
* 只负责交互记录,不负责调度、run 队列或执行:
|
|
5
|
+
* - pending / answered / orphaned 三态;`interactionRecords` 是唯一事实源;
|
|
6
|
+
* - 按 tenant 落盘(`InteractionsStore`),启动 hydrate 时把无 run 的 pending 标 orphaned;
|
|
7
|
+
* - 应答只发布到请求产生时的 tenant 流;tenant 不一致一律 `not_found`;
|
|
8
|
+
* - run 终态(`run.completed|failed|cancelled`)把该 run 的 pending 转 orphaned;
|
|
9
|
+
* 不再有旧 turn 生命周期事件 / 成员特判(F11 已删除)。
|
|
10
|
+
*
|
|
11
|
+
* @module @vessel-agent/serve/interactions-registry
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { EventStream, RunEvent } from '@vessel-agent/core';
|
|
15
|
+
import {
|
|
16
|
+
InteractionsStore,
|
|
17
|
+
type StoredInteraction,
|
|
18
|
+
type StoredInteractionAnswer,
|
|
19
|
+
type StoredInteractionStatus,
|
|
20
|
+
} from './interactions-store.js';
|
|
21
|
+
import { logEvent } from './log-event.js';
|
|
22
|
+
import { RpcError } from './rpc.js';
|
|
23
|
+
|
|
24
|
+
/** 权限应答 scope(DECISION-024 §2.1)。 */
|
|
25
|
+
export type PermissionScope = 'once' | 'session' | 'always';
|
|
26
|
+
|
|
27
|
+
/** 待答权限请求 */
|
|
28
|
+
export interface PendingPermission {
|
|
29
|
+
run_id: string;
|
|
30
|
+
tool: string;
|
|
31
|
+
input: unknown;
|
|
32
|
+
ts: number;
|
|
33
|
+
/** 事件携带的 deadline_at(兼容保留);不再作为过期依据。 */
|
|
34
|
+
deadline_at?: number;
|
|
35
|
+
session_id?: string;
|
|
36
|
+
/**
|
|
37
|
+
* bridge 请求的父 **agent**(0.51 additive;事件 `parent_agent_id`)—— ask 链路的
|
|
38
|
+
* **唯一**归属维度。父会话由读侧从它 + roster 上溯出来(`apps/web/src/domains/pending/
|
|
39
|
+
* attribution.ts`),本层不再记父会话 id:那是展示信息,多存一处就是第二个事实源。
|
|
40
|
+
*/
|
|
41
|
+
parent_agent_id?: string;
|
|
42
|
+
tool_call_id?: string;
|
|
43
|
+
source?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 应答者归属;agent 不是合法值(only-human)。 */
|
|
47
|
+
export interface InteractionActor {
|
|
48
|
+
answered_by: 'human' | 'policy';
|
|
49
|
+
answered_via?: 'tui' | 'rpc' | 'cli' | 'policy';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 0.52:应答的「来路」上下文(additive,只影响答案投放的分组)。
|
|
54
|
+
*
|
|
55
|
+
* `batch_id` = 待批页的一次提交(一簇里每条 ask 各发一次 `ask.respond`,但那是
|
|
56
|
+
* 一次人类动作)。它落进 `answer.batch_id`,投放实现按 (agent, session, batch_id)
|
|
57
|
+
* 短窗口合并成一条消息 + 一次 run;缺省 = 单条应答(旧行为)。
|
|
58
|
+
*/
|
|
59
|
+
export interface AskRespondContext {
|
|
60
|
+
batch_id?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** `permission.respond` 的结果(additive)。 */
|
|
64
|
+
export interface PermissionRespondResult {
|
|
65
|
+
ok: true;
|
|
66
|
+
decision: 'allow' | 'deny';
|
|
67
|
+
scope: PermissionScope;
|
|
68
|
+
/** 重复应答:沿用第一次的结果,不再发布事件。 */
|
|
69
|
+
duplicate?: true;
|
|
70
|
+
answered_by?: 'human' | 'policy';
|
|
71
|
+
answered_via?: 'tui' | 'rpc' | 'cli' | 'policy';
|
|
72
|
+
latency_ms?: number;
|
|
73
|
+
team_id?: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** `ask.respond` 的结果(与权限同一套幂等语义)。 */
|
|
77
|
+
export interface AskRespondResult {
|
|
78
|
+
ok: true;
|
|
79
|
+
duplicate?: true;
|
|
80
|
+
answered_by: 'human' | 'policy';
|
|
81
|
+
answered_via?: 'tui' | 'rpc' | 'cli' | 'policy';
|
|
82
|
+
latency_ms?: number;
|
|
83
|
+
team_id?: string;
|
|
84
|
+
/** 这次真正写进去的题 id(additive:分批提交时只含本批)。 */
|
|
85
|
+
answered_questions?: string[];
|
|
86
|
+
/** 仍未答的题 id(additive;空 = 这条请求已收口,卡片该消失了)。 */
|
|
87
|
+
remaining_questions?: string[];
|
|
88
|
+
/** 已答过、本次被跳过的题 id(additive;幂等重复提交会全在这里)。 */
|
|
89
|
+
skipped_questions?: string[];
|
|
90
|
+
/** 这条请求是不是已经全部答完(additive;缺省 = 不知道 / 旧服务端)。 */
|
|
91
|
+
final?: boolean;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** `notify.ack` 的结果(与权限/ask 同一套幂等语义;ack 纯人类侧记账,不回流模型)。 */
|
|
95
|
+
export interface NotifyAckResult {
|
|
96
|
+
ok: true;
|
|
97
|
+
duplicate?: true;
|
|
98
|
+
answered_by: 'human' | 'policy';
|
|
99
|
+
answered_via?: 'tui' | 'rpc' | 'cli' | 'policy';
|
|
100
|
+
latency_ms?: number;
|
|
101
|
+
team_id?: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 待答 ask 请求(legacy 只读视图,由 interactionRecords 派生)。 */
|
|
105
|
+
export interface PendingAsk {
|
|
106
|
+
run_id: string;
|
|
107
|
+
ts: number;
|
|
108
|
+
session_id?: string;
|
|
109
|
+
/** 父 agent id(0.51 additive;ask 链路的唯一归属维度)。 */
|
|
110
|
+
parent_agent_id?: string;
|
|
111
|
+
/** 原始 questions;只用于 `interactions.list` 展示,不持久化。 */
|
|
112
|
+
questions?: unknown;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 注册表选项。 */
|
|
116
|
+
export interface InteractionsRegistryOptions {
|
|
117
|
+
/**
|
|
118
|
+
* 单运行时事件流:权限 / ask 应答直接投递到这里。
|
|
119
|
+
* 找不到流说明 run 已结束,应答返回 `run_ended`。
|
|
120
|
+
*/
|
|
121
|
+
eventsFor: () => EventStream | undefined;
|
|
122
|
+
/** 单 id:只用于交互记录文件名(R1 路径形状不变)。 */
|
|
123
|
+
singleTenantId: string;
|
|
124
|
+
/** 交互持久化目录;缺省 = 不落盘。 */
|
|
125
|
+
interactionsDir?: string;
|
|
126
|
+
/**
|
|
127
|
+
* 启动 hydrate 时如何对待磁盘上的 `pending`。
|
|
128
|
+
*
|
|
129
|
+
* gateway 的语义是「本进程没有对应 run」→ `orphaned`(默认);
|
|
130
|
+
* 离线 attention 只读写文件、没有 run,但请求可能属于一个 `interrupted`
|
|
131
|
+
* 的 headless run,仍可被人类回答 → `pending`。
|
|
132
|
+
*/
|
|
133
|
+
hydratePendingAs?: 'orphaned' | 'pending';
|
|
134
|
+
/**
|
|
135
|
+
* 0.52:async ask 拿到**第一次**答案时通知调用方(投放答案 / 起新 run)。
|
|
136
|
+
*
|
|
137
|
+
* **投放的唯一触发点**:RPC 应答(`respondAsk`)与流上应答
|
|
138
|
+
* (`ask.user.answered`,TUI / policy)两条路共用 `settleAnswer` 这一个出口,
|
|
139
|
+
* 而出口只在「记录此刻还是 pending」的那一次迁移上通知(见 `settleAnswer`)。
|
|
140
|
+
* 装配层不要在同一个注册表上挂两份,也不要再加第二条触发路径(比如让
|
|
141
|
+
* `ask.respond` 的 handler 自己再投一次)。同一条运行时流上并存多个 gateway
|
|
142
|
+
* 装配时,重复通知由 gateway 的进程级单飞去重(`asyncAnswerDeliveries`)。
|
|
143
|
+
* 实现方必须自吞全部错误(答案已经落定,投放失败只退化为「只记录答案」)。
|
|
144
|
+
*/
|
|
145
|
+
onAsyncAskAnswered?: (record: StoredInteraction) => void;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** 交互注册表(serve 内唯一交互状态)。 */
|
|
149
|
+
export interface InteractionsRegistry {
|
|
150
|
+
/** 内存 pending 权限索引;真相源是 `interactionRecords`。 */
|
|
151
|
+
readonly pendingPermissions: Map<string, PendingPermission>;
|
|
152
|
+
/** requestId → 持久化交互记录(pending / answered / orphaned)。 */
|
|
153
|
+
readonly interactionRecords: Map<string, StoredInteraction>;
|
|
154
|
+
/** pending ask 的 legacy 只读视图;由 `interactionRecords` 派生。 */
|
|
155
|
+
readonly pendingAsks: Map<string, PendingAsk>;
|
|
156
|
+
/** 由 gateway 的 hub 原始订阅调用。 */
|
|
157
|
+
handleEvent(event: RunEvent): void;
|
|
158
|
+
respondPermission(
|
|
159
|
+
requestId: string,
|
|
160
|
+
decision: 'allow' | 'deny',
|
|
161
|
+
remember: boolean,
|
|
162
|
+
scope?: PermissionScope,
|
|
163
|
+
actor?: InteractionActor,
|
|
164
|
+
): PermissionRespondResult;
|
|
165
|
+
respondAsk(
|
|
166
|
+
requestId: string,
|
|
167
|
+
answers: unknown[],
|
|
168
|
+
actor?: InteractionActor,
|
|
169
|
+
context?: AskRespondContext,
|
|
170
|
+
): AskRespondResult;
|
|
171
|
+
/**
|
|
172
|
+
* q-human-notify P1:把一条未读通知标记为「已读/确认」(ack)。
|
|
173
|
+
*
|
|
174
|
+
* 复用三态:`kind:'notify' ∧ status:'answered'` = 已读(不新增 status)。幂等:
|
|
175
|
+
* 重复 ack 沿用第一次结果,不再发事件。**不回流模型**(fire-and-forget):不投放、
|
|
176
|
+
* 不起新 run、不投 inbox —— 只移出未读 + 发 `notify.user.acked` 供各端收卡。
|
|
177
|
+
*/
|
|
178
|
+
ackNotify(requestId: string, actor?: InteractionActor): NotifyAckResult;
|
|
179
|
+
/**
|
|
180
|
+
* 0.52:把「答案已投放」写进记录(幂等标记,`answer.delivered_at`)。
|
|
181
|
+
* 返回 false = 记录不存在或还不是 answered。
|
|
182
|
+
*/
|
|
183
|
+
markAnswerDelivered(requestId: string, deliveredAt: number): boolean;
|
|
184
|
+
/**
|
|
185
|
+
* 0.52:从**持久化文件**重读一条记录(只读,不改内存、不改格式)。
|
|
186
|
+
*
|
|
187
|
+
* 用途是「跨进程 / 跨重启兜底」:`interactionRecords` 只在启动 hydrate 时与磁盘
|
|
188
|
+
* 对齐,另一个进程(或本进程重启前的自己)写下的 `answer.delivered_at` 只存在于
|
|
189
|
+
* 文件里。投放前用它确认一次「这条答案是不是已经被投过了」。
|
|
190
|
+
* 读失败 / 无 store 一律返回 undefined(不阻断投放)。
|
|
191
|
+
*/
|
|
192
|
+
peekPersistedAnswer(requestId: string): StoredInteraction | undefined;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** 记录上限(防止长时间进程无界增长);淘汰只碰非 pending。 */
|
|
196
|
+
const MAX_RECORDS = 2000;
|
|
197
|
+
|
|
198
|
+
/** q-human-notify P1(D7):每 agent 未读通知上限;满则拒新(存储有界闸)。 */
|
|
199
|
+
const NOTIFY_UNREAD_CAP = 50;
|
|
200
|
+
|
|
201
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
202
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function nonEmptyString(value: unknown): string | undefined {
|
|
206
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function eventTs(event: RunEvent): number {
|
|
210
|
+
return typeof event.ts === 'number' && Number.isFinite(event.ts) && event.ts > 0
|
|
211
|
+
? event.ts
|
|
212
|
+
: Date.now();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** answers 是模型 / UI 产出的任意 JSON;按结构比较,不按 key 顺序。 */
|
|
216
|
+
function jsonEqual(left: unknown, right: unknown): boolean {
|
|
217
|
+
if (left === right) return true;
|
|
218
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
219
|
+
return (
|
|
220
|
+
left.length === right.length && left.every((item, index) => jsonEqual(item, right[index]))
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
if (isRecord(left) && isRecord(right)) {
|
|
224
|
+
const leftKeys = Object.keys(left);
|
|
225
|
+
const rightKeys = Object.keys(right);
|
|
226
|
+
if (leftKeys.length !== rightKeys.length) return false;
|
|
227
|
+
return leftKeys.every((key) => Object.hasOwn(right, key) && jsonEqual(left[key], right[key]));
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** `answered_by` 缺省视为 human;非法值(含伪造的 agent)返回 undefined。 */
|
|
233
|
+
function readAnsweredBy(value: unknown): 'human' | 'policy' | undefined {
|
|
234
|
+
if (value === undefined) return 'human';
|
|
235
|
+
return value === 'human' || value === 'policy' ? value : undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ── 逐题(问题是一等公民) ────────────────────────────
|
|
239
|
+
|
|
240
|
+
/** 一条请求里的一道题(题 id + 用来给旧答案兜底匹配的两个文本键)。 */
|
|
241
|
+
interface AskQuestionSlot {
|
|
242
|
+
id: string;
|
|
243
|
+
question?: string;
|
|
244
|
+
header?: string;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 题 id 的**唯一**推导规则:`q1` / `q2` / …(与工具写侧的 `questionIdOfIndex` 同值)。
|
|
249
|
+
*
|
|
250
|
+
* 旧请求(事件里的 `questions[]` 没有 id)与会话里既有的历史记录都靠这一条算回来,
|
|
251
|
+
* 所以「已答 / 未答」对老数据同样成立,不需要迁移。
|
|
252
|
+
*/
|
|
253
|
+
export function questionIdOfIndex(index: number): string {
|
|
254
|
+
return `q${index + 1}`;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** 把 `input`(= 事件的 `questions`)摊成题槽;不是数组就当作「没有可枚举的题」。 */
|
|
258
|
+
function askQuestionSlots(input: unknown): AskQuestionSlot[] {
|
|
259
|
+
if (!Array.isArray(input)) return [];
|
|
260
|
+
return input.map((item, index) => {
|
|
261
|
+
const record = isRecord(item) ? item : {};
|
|
262
|
+
const explicit = nonEmptyString(record.id);
|
|
263
|
+
const question = nonEmptyString(record.question);
|
|
264
|
+
const header = nonEmptyString(record.header);
|
|
265
|
+
return {
|
|
266
|
+
id: explicit ?? questionIdOfIndex(index),
|
|
267
|
+
...(question !== undefined ? { question } : {}),
|
|
268
|
+
...(header !== undefined ? { header } : {}),
|
|
269
|
+
};
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* 答案归一:把一条答案绑到具体题上(认不出来才返回 undefined)。
|
|
275
|
+
*
|
|
276
|
+
* 顺序:
|
|
277
|
+
* 1. `question_id`(新客户端 / 新历史,唯一可靠的一步);
|
|
278
|
+
* 2. `question` 正文全等(旧客户端带了正文,最常见);
|
|
279
|
+
* 3. `header` 全等(正文被 UI 改写时的兜底);
|
|
280
|
+
* 4. 同一位置上那道题(旧客户端把 N 题按题序一次发来,第 i 条就是第 i 题)。
|
|
281
|
+
*
|
|
282
|
+
* 两处刻意与旧实现不同,都是为了「归一」与「去重」分成两步:
|
|
283
|
+
* - 认出了题、但那题已经答过时**照样返回题号**,不再折叠成 undefined —— 折叠之后
|
|
284
|
+
* 调用方无从区分「重复」与「认不出来」,重复行会被当成一条没有题号的新答案写进
|
|
285
|
+
* 记录(0.52 落盘里那条 question_id 为 null 的答案就是这么来的);
|
|
286
|
+
* - 位置兜底只认「答案自己的序号」,不再用「第一道没被占用的题」:分批之后一次提交
|
|
287
|
+
* 里的第 i 条与题序不再一一对齐(可能只带后面几题),空位兜底会把重投的旧答案
|
|
288
|
+
* 硬塞给还没答的题。
|
|
289
|
+
*/
|
|
290
|
+
function questionIdForAnswer(
|
|
291
|
+
answer: unknown,
|
|
292
|
+
slots: readonly AskQuestionSlot[],
|
|
293
|
+
index: number,
|
|
294
|
+
): string | undefined {
|
|
295
|
+
const identified = identifyQuestion(answer, slots);
|
|
296
|
+
if (identified !== undefined) return identified;
|
|
297
|
+
// 位置兜底:旧客户端把 N 题按题序一次发来,第 i 条就是第 i 题。
|
|
298
|
+
return slots[index]?.id;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* 只按答案自带的身份键认题(不看位置):`question_id` → `question` 正文 → `header`。
|
|
303
|
+
*
|
|
304
|
+
* 与 {@link questionIdForAnswer} 分开的理由:判断「这行是不是重复」时不能有位置兜底 ——
|
|
305
|
+
* 一份重复提交的三行答案会被位置兜底认成同一题,于是 skipped 里出现三个 `q1`。
|
|
306
|
+
*/
|
|
307
|
+
function identifyQuestion(answer: unknown, slots: readonly AskQuestionSlot[]): string | undefined {
|
|
308
|
+
const record = isRecord(answer) ? answer : {};
|
|
309
|
+
const explicit = nonEmptyString(record.question_id);
|
|
310
|
+
if (explicit !== undefined && slots.some((slot) => slot.id === explicit)) return explicit;
|
|
311
|
+
const question = nonEmptyString(record.question);
|
|
312
|
+
if (question !== undefined) {
|
|
313
|
+
const hit = slots.find((slot) => slot.question === question);
|
|
314
|
+
if (hit !== undefined) return hit.id;
|
|
315
|
+
}
|
|
316
|
+
const header = nonEmptyString(record.header);
|
|
317
|
+
if (header !== undefined) {
|
|
318
|
+
const hit = slots.find((slot) => slot.header === header);
|
|
319
|
+
if (hit !== undefined) return hit.id;
|
|
320
|
+
}
|
|
321
|
+
return undefined;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** 一批提交的逐题计划(纯函数:不碰记录、不落盘,好被测试直接钉)。 */
|
|
325
|
+
export interface AskAnswerPlan {
|
|
326
|
+
/** 本批真正新写入的答案(已补上 `question_id`)。 */
|
|
327
|
+
fresh: unknown[];
|
|
328
|
+
/** 已答过、本次被跳过的题 id(幂等,不覆盖)。 */
|
|
329
|
+
skipped: string[];
|
|
330
|
+
/** 合并后已答的题 id(旧 + 本批)。 */
|
|
331
|
+
answered: string[];
|
|
332
|
+
/** 仍未答的题 id。 */
|
|
333
|
+
remaining: string[];
|
|
334
|
+
/** 整条请求是否已全部答完。 */
|
|
335
|
+
final: boolean;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 把一批答案规划到「题」上。
|
|
340
|
+
*
|
|
341
|
+
* 四条不变量:
|
|
342
|
+
* - **未答的题不会因为别人被答而被带走**:`answered` 只加不减,`remaining` 只减不增;
|
|
343
|
+
* - **同一题不得被答两次**:已答过的题进 `skipped`,不覆盖、不重投;
|
|
344
|
+
* - **先归一,再去重**:每条答案先绑到具体题上(缺 question_id 的旧客户端按正文 /
|
|
345
|
+
* 自己的题序位置回退),绑不上题的行不落笔 —— 记录里不允许出现没有题号的答案,
|
|
346
|
+
* 否则重复提交会变成一条独立的新答案(这就是分批提交导致数据漂移的入口);
|
|
347
|
+
* - **没有可枚举的题**(`input` 不是数组 / 空数组)时退回旧语义:本批答案全部算数,
|
|
348
|
+
* 整条请求当场收口 —— 历史记录与异常形状都走这条,行为与分批之前一致。
|
|
349
|
+
*/
|
|
350
|
+
export function planAskAnswers(
|
|
351
|
+
record: StoredInteraction,
|
|
352
|
+
answers: readonly unknown[],
|
|
353
|
+
): AskAnswerPlan {
|
|
354
|
+
const slots = askQuestionSlots(record.input);
|
|
355
|
+
const answeredBefore = record.answered_questions ?? [];
|
|
356
|
+
const taken = new Set(answeredBefore);
|
|
357
|
+
const fresh: unknown[] = [];
|
|
358
|
+
const skipped: string[] = [];
|
|
359
|
+
|
|
360
|
+
// 没有可枚举的题时只能整批收下,但**已经记过的同一份答案不能再记一遍**:这条路径
|
|
361
|
+
// 同时被 RPC 和流上回声调用(echo 先落盘、RPC 兜底再来一次),不判等就是一份答案
|
|
362
|
+
// 记两次 —— 空题号版本的数据漂移在旧形状的 ask 上是同一个病。
|
|
363
|
+
const recorded = (record.answer?.answers ?? []) as readonly unknown[];
|
|
364
|
+
|
|
365
|
+
for (const [index, answer] of answers.entries()) {
|
|
366
|
+
if (slots.length === 0) {
|
|
367
|
+
if (!recorded.some((previous) => jsonEqual(previous, answer))) fresh.push(answer);
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
const explicit = isRecord(answer) ? nonEmptyString(answer.question_id) : undefined;
|
|
371
|
+
// 自报题号且这题已答过:这就是重复提交,直接抄进 skipped(不要再猜别的题)。
|
|
372
|
+
if (explicit !== undefined && taken.has(explicit)) {
|
|
373
|
+
skipped.push(explicit);
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (slots.every((slot) => taken.has(slot.id))) {
|
|
377
|
+
// 题已全被答过:任何还来的一行只能是重复(或对不上题的尾巴)。
|
|
378
|
+
// 绝不追加成「新答案」—— 那会让一次双击把整条请求再投一遍。
|
|
379
|
+
const id = explicit ?? identifyQuestion(answer, slots);
|
|
380
|
+
if (id !== undefined) skipped.push(id);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
// 先归一(补上题号)再按题号去重,顺序不能反:先判重就得先知道「这是哪题」,
|
|
384
|
+
// 而缺 question_id 的旧客户端只能靠正文 / 位置归一。旧的实现把「认出了但已答过」
|
|
385
|
+
// 也折成「认不出来」,于是重投的旧答案被原样追加成一条没有题号的新答案。
|
|
386
|
+
const questionId = questionIdForAnswer(answer, slots, index);
|
|
387
|
+
if (questionId === undefined) continue;
|
|
388
|
+
if (taken.has(questionId)) {
|
|
389
|
+
skipped.push(questionId);
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
taken.add(questionId);
|
|
393
|
+
fresh.push(isRecord(answer) ? { ...answer, question_id: questionId } : answer);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const answered = slots.map((slot) => slot.id).filter((id) => taken.has(id));
|
|
397
|
+
const remaining = slots.map((slot) => slot.id).filter((id) => !taken.has(id));
|
|
398
|
+
return {
|
|
399
|
+
fresh,
|
|
400
|
+
skipped: [...new Set(skipped)],
|
|
401
|
+
answered,
|
|
402
|
+
remaining,
|
|
403
|
+
final: slots.length === 0 ? true : remaining.length === 0,
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** `q3` → 3(题号);不是这个形状(旧答案 / 手写值)→ undefined。 */
|
|
408
|
+
function questionIndexOfId(questionId: string | undefined): number | undefined {
|
|
409
|
+
if (questionId === undefined) return undefined;
|
|
410
|
+
const match = /^q(\d+)$/.exec(questionId);
|
|
411
|
+
if (match === null) return undefined;
|
|
412
|
+
const parsed = Number(match[1]);
|
|
413
|
+
return Number.isInteger(parsed) && parsed > 0 ? parsed : undefined;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* 累计答案按**题序**排:分批到达的答案在记录里保持「第 1、2、3 题」的顺序,
|
|
418
|
+
* 与到达先后无关(答复卡 / resume 注入 / 模型文本都按这个顺序读)。
|
|
419
|
+
*
|
|
420
|
+
* 认不出题号的答案(旧记录 / 手写)排在最后并保持它们自己的相对顺序 —— 绝不重排
|
|
421
|
+
* 老数据,否则「读旧记录」会和分批之前长得不一样。
|
|
422
|
+
*/
|
|
423
|
+
function sortAnswersByQuestion(answers: readonly unknown[]): unknown[] {
|
|
424
|
+
const decorated = answers.map((answer, index) => ({
|
|
425
|
+
answer,
|
|
426
|
+
index,
|
|
427
|
+
order: isRecord(answer) ? questionIndexOfId(nonEmptyString(answer.question_id)) : undefined,
|
|
428
|
+
}));
|
|
429
|
+
decorated.sort((left, right) => {
|
|
430
|
+
if (left.order !== undefined && right.order !== undefined && left.order !== right.order) {
|
|
431
|
+
return left.order - right.order;
|
|
432
|
+
}
|
|
433
|
+
if (left.order !== undefined && right.order === undefined) return -1;
|
|
434
|
+
if (left.order === undefined && right.order !== undefined) return 1;
|
|
435
|
+
return left.index - right.index;
|
|
436
|
+
});
|
|
437
|
+
return decorated.map((item) => item.answer);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** 创建交互注册表。 */
|
|
441
|
+
export function createInteractionsRegistry(
|
|
442
|
+
options: InteractionsRegistryOptions,
|
|
443
|
+
): InteractionsRegistry {
|
|
444
|
+
const { eventsFor, singleTenantId } = options;
|
|
445
|
+
|
|
446
|
+
/** 内存 pending 权限索引;真相源是 `interactionRecords`。 */
|
|
447
|
+
const pendingPermissions = new Map<string, PendingPermission>();
|
|
448
|
+
/** requestId → 持久化交互记录(pending / answered / orphaned)。 */
|
|
449
|
+
const interactionRecords = new Map<string, StoredInteraction>();
|
|
450
|
+
/**
|
|
451
|
+
* headless 因等不到人类而挂起的 ask(0.33)。
|
|
452
|
+
*
|
|
453
|
+
* 这类请求的 run 会以 `run.failed` 收口(checkpoint 是 `interrupted`),
|
|
454
|
+
* 不能按普通终态转 `orphaned`——人类稍后仍要回答它。
|
|
455
|
+
*/
|
|
456
|
+
const suspendedAskIds = new Set<string>();
|
|
457
|
+
const interactionsStore =
|
|
458
|
+
options.interactionsDir !== undefined && options.interactionsDir !== ''
|
|
459
|
+
? new InteractionsStore(options.interactionsDir)
|
|
460
|
+
: undefined;
|
|
461
|
+
|
|
462
|
+
/** 记录有界:只淘汰非 pending 的记录,绝不丢未应答请求。 */
|
|
463
|
+
function rememberRecord(record: StoredInteraction): void {
|
|
464
|
+
interactionRecords.set(record.requestId, record);
|
|
465
|
+
if (interactionRecords.size <= MAX_RECORDS) return;
|
|
466
|
+
for (const [id, item] of interactionRecords) {
|
|
467
|
+
if (interactionRecords.size <= MAX_RECORDS) break;
|
|
468
|
+
if (item.status !== 'pending') interactionRecords.delete(id);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* 落盘串行链(0.33)+ trailing debounce 合并(gw-F1)。
|
|
474
|
+
*
|
|
475
|
+
* `save` 是 fire-and-forget;并发 atomicWrite 可能乱序落盘,
|
|
476
|
+
* 让较早的快照(例如 `suspended` 之前的 ask)覆盖较新的状态。串行化后,
|
|
477
|
+
* 最后一次调用的快照一定最后落盘。
|
|
478
|
+
*
|
|
479
|
+
* gw-F1:每次交互变更都全量重写 + durable fsync(112 条时一条 ~2.7 KB 的应答
|
|
480
|
+
* 触发 300 KB 重写,112×;2000 条上限时 ~14 ms/次)。突发 N 次变更过去 = N 次
|
|
481
|
+
* 全量写排队;现在 50 ms trailing debounce 合并成**一次**最新快照写。
|
|
482
|
+
* 快照在定时器触发时取(不是调用时),合并窗口内的中间状态不必落盘。
|
|
483
|
+
*
|
|
484
|
+
* durability 口径不变差到可感知:落盘本来就是 fire-and-forget(崩溃可能丢在途
|
|
485
|
+
* 写);离线 attention CLI 在 respond 后**自己 await 一次 save**(ask-channel.ts,
|
|
486
|
+
* 不依赖本链),跨进程 delivered_at 兜底语义是「宁可多投一次」(gateway.ts
|
|
487
|
+
* alreadyDeliveredOnDisk 注释)。定时器 unref:不拖住进程退出。
|
|
488
|
+
*/
|
|
489
|
+
let persistChain: Promise<void> = Promise.resolve();
|
|
490
|
+
let persistTimer: ReturnType<typeof setTimeout> | undefined;
|
|
491
|
+
const PERSIST_DEBOUNCE_MS = 50;
|
|
492
|
+
|
|
493
|
+
/** 立即把当前内存快照排进串行链(debounce 到期 / 需要必写时调用)。 */
|
|
494
|
+
function persistNow(): void {
|
|
495
|
+
const store = interactionsStore;
|
|
496
|
+
if (!store) return;
|
|
497
|
+
const records = [...interactionRecords.values()].sort((a, b) => a.created_at - b.created_at);
|
|
498
|
+
const next = persistChain
|
|
499
|
+
.catch(() => undefined) // benign: 只等链排空;前次写失败已经 store.persist_failed logEvent 可见。
|
|
500
|
+
.then(() => store.save(singleTenantId, records))
|
|
501
|
+
.catch(() => undefined); // benign: 写失败内部 logEvent('error','interactions.store.persist_failed');RPC 不因此失败(设计)。
|
|
502
|
+
persistChain = next;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** 重写交互记录文件;写失败只记 stderr(RPC 不因此失败)。 */
|
|
506
|
+
function persist(): void {
|
|
507
|
+
if (!interactionsStore) return;
|
|
508
|
+
if (persistTimer !== undefined) clearTimeout(persistTimer);
|
|
509
|
+
persistTimer = setTimeout(() => {
|
|
510
|
+
persistTimer = undefined;
|
|
511
|
+
persistNow();
|
|
512
|
+
}, PERSIST_DEBOUNCE_MS);
|
|
513
|
+
persistTimer.unref?.();
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* 启动时加载持久化记录。
|
|
518
|
+
*
|
|
519
|
+
* gateway:本进程尚未执行任何 run,加载到的 `pending` 一定没有对应 run,
|
|
520
|
+
* 标记为 `orphaned`,应答返回 `run_ended`,不假装可执行。
|
|
521
|
+
* 离线 attention:`hydratePendingAs: 'pending'`,让 headless run 挂起后
|
|
522
|
+
* 仍可被人类回答(文件是唯一事实源)。
|
|
523
|
+
*/
|
|
524
|
+
function hydrateInteractions(): void {
|
|
525
|
+
if (!interactionsStore) return;
|
|
526
|
+
const pendingStatus = options.hydratePendingAs ?? 'orphaned';
|
|
527
|
+
let changed = false;
|
|
528
|
+
for (const loaded of interactionsStore.loadAllSync()) {
|
|
529
|
+
// 0.33:headless 挂起的 ask(suspended)即使重启后也要保持 pending,
|
|
530
|
+
// 否则人类永远答不了、--resume-run 也拿不到答案。
|
|
531
|
+
// 0.52:async ask 同理 —— 它的 run 早就在工具返回后正常结束了,请求仍然有效。
|
|
532
|
+
// 分批:**部分已答**的 ask 也一样 —— 已答的题不许随着重启被孤儿化,
|
|
533
|
+
// 否则「剩下那 1 题」从此再也答不了。
|
|
534
|
+
const keepPending =
|
|
535
|
+
loaded.status === 'pending' &&
|
|
536
|
+
(pendingStatus === 'pending' ||
|
|
537
|
+
// q-human-notify P1:未读通知跨重启保持 pending(人稍后仍要看,同 async ask 豁免)。
|
|
538
|
+
loaded.kind === 'notify' ||
|
|
539
|
+
(loaded.kind === 'ask' &&
|
|
540
|
+
(loaded.suspended === true ||
|
|
541
|
+
loaded.mode === 'async' ||
|
|
542
|
+
(loaded.answered_questions?.length ?? 0) > 0)));
|
|
543
|
+
const record: StoredInteraction =
|
|
544
|
+
loaded.status === 'pending' && !keepPending
|
|
545
|
+
? { ...loaded, status: 'orphaned' as StoredInteractionStatus }
|
|
546
|
+
: loaded;
|
|
547
|
+
if (record.status !== loaded.status) changed = true;
|
|
548
|
+
interactionRecords.set(record.requestId, record);
|
|
549
|
+
}
|
|
550
|
+
if (changed) persist();
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
hydrateInteractions();
|
|
554
|
+
|
|
555
|
+
/** 应答投递目标:单运行时流(工具 / 权限策略正在等它)。 */
|
|
556
|
+
function answerStream(): EventStream {
|
|
557
|
+
const stream = eventsFor();
|
|
558
|
+
if (stream === undefined) {
|
|
559
|
+
throw new RpcError('run_ended', 'Runtime event stream is no longer available');
|
|
560
|
+
}
|
|
561
|
+
return stream;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** 这条记录是不是「部分已答」(还有题在等人)。 */
|
|
565
|
+
function hasPartialAnswers(record: StoredInteraction): boolean {
|
|
566
|
+
return (record.answered_questions?.length ?? 0) > 0;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/** 一批答案(`ask.respond` 或流上的 `ask.user.answered` 都是这一形状)。 */
|
|
570
|
+
interface AnswerBatch {
|
|
571
|
+
answers: unknown[];
|
|
572
|
+
answered_at: number;
|
|
573
|
+
answered_by: 'human' | 'policy';
|
|
574
|
+
answered_via?: StoredInteractionAnswer['answered_via'];
|
|
575
|
+
latency_ms?: number;
|
|
576
|
+
batch_id?: string;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** 收口结果:本批真的写进去了什么 + 逐题进度。 */
|
|
580
|
+
interface SettleOutcome extends AskAnswerPlan {
|
|
581
|
+
/** false = 幂等重放 / 全部重复:一个字节都没改,也没有投递。 */
|
|
582
|
+
applied: boolean;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* 答案写入的唯一收口:**逐题**累积 + 按题幂等 + 只在需要时通知投放。
|
|
587
|
+
*
|
|
588
|
+
* 批量语义(用户裁决):
|
|
589
|
+
* - 一次提交只写「这次答了的题」,其余题**仍是 `pending`**(这就是「未答的题不能被
|
|
590
|
+
* 提交带走」的落点:状态机不新增档,pending + `answered_questions` 就够了);
|
|
591
|
+
* - 同一题重复提交不覆盖、不重投,进 `skipped`;
|
|
592
|
+
* - 全部题答完才 `pending → answered`(这时才写 `answer.delivered_at`,
|
|
593
|
+
* 见 `markAnswerDelivered` —— gateway 那边现成的「终结后不再重投」因此自动成立)。
|
|
594
|
+
*
|
|
595
|
+
* 投放通知的判据(为什么不是「每次迁移」):
|
|
596
|
+
* - **拒绝回答**(`answers: []`):整条收口,照旧通知(旧行为);
|
|
597
|
+
* - `async`:每一批都要通知(提问的 run 早就结束了,没人等着接答案);
|
|
598
|
+
* - `sync`:**第一批不通知** —— 它由还在 `waitFor` 的 ask 工具当场取走,再投一次就是
|
|
599
|
+
* 同一批答案两份(模型读两遍、还多起一次 run)。从第二批起工具已经返回了,
|
|
600
|
+
* 只能走 inbox 消息,所以通知。
|
|
601
|
+
*
|
|
602
|
+
* 幂等靠 `planAskAnswers` 的 `fresh`:两条应答路径(RPC 与流上回声)都调这里,第二次
|
|
603
|
+
* 拿到的 `fresh` 一定是空的 —— 既不会重复投放,也不需要「是否仍 pending」这种和状态
|
|
604
|
+
* 绑死的判定(分批之后 pending 不再是「还没人答」的意思)。
|
|
605
|
+
*/
|
|
606
|
+
function settleAnswer(record: StoredInteraction, batch: AnswerBatch): SettleOutcome {
|
|
607
|
+
const current = interactionRecords.get(record.requestId) ?? record;
|
|
608
|
+
const declined = batch.answers.length === 0;
|
|
609
|
+
const plan = planAskAnswers(current, batch.answers);
|
|
610
|
+
const applied = plan.fresh.length > 0 || (declined && current.status === 'pending');
|
|
611
|
+
if (!applied) return { ...plan, applied: false };
|
|
612
|
+
|
|
613
|
+
// 拒绝回答(`answers: []`)不清空已记录的答案:人先前答过的那几题是事实,抹掉它
|
|
614
|
+
// 等于让答复记录变成「什么都没答」。投递层看的是 `batch_answers`(拒绝时为空)→
|
|
615
|
+
// 模型仍然读到「人类拒绝回答」那句原文。
|
|
616
|
+
const mergedAnswers = declined
|
|
617
|
+
? (current.answer?.answers ?? [])
|
|
618
|
+
: sortAnswersByQuestion([...(current.answer?.answers ?? []), ...plan.fresh]);
|
|
619
|
+
const answer: StoredInteractionAnswer = {
|
|
620
|
+
...(current.answer ?? {}),
|
|
621
|
+
answered_at: batch.answered_at,
|
|
622
|
+
answered_by: batch.answered_by,
|
|
623
|
+
...(batch.answered_via !== undefined ? { answered_via: batch.answered_via } : {}),
|
|
624
|
+
...(batch.latency_ms !== undefined ? { latency_ms: batch.latency_ms } : {}),
|
|
625
|
+
...(batch.batch_id !== undefined ? { batch_id: batch.batch_id } : {}),
|
|
626
|
+
answers: mergedAnswers,
|
|
627
|
+
// 本批新增的那些:投放只该带这一批,否则第二批会重发第一批的答案。
|
|
628
|
+
batch_answers: declined ? [] : plan.fresh,
|
|
629
|
+
};
|
|
630
|
+
const final = declined || plan.final;
|
|
631
|
+
const next: StoredInteraction = {
|
|
632
|
+
...current,
|
|
633
|
+
status: final ? ('answered' as const) : ('pending' as const),
|
|
634
|
+
answered_questions: plan.answered,
|
|
635
|
+
answer,
|
|
636
|
+
};
|
|
637
|
+
if (final) suspendedAskIds.delete(current.requestId);
|
|
638
|
+
rememberRecord(next);
|
|
639
|
+
persist();
|
|
640
|
+
|
|
641
|
+
const wasAnsweredBefore = hasPartialAnswers(current);
|
|
642
|
+
const shouldDeliver =
|
|
643
|
+
declined ||
|
|
644
|
+
(plan.fresh.length > 0 &&
|
|
645
|
+
(current.kind ?? 'permission') === 'ask' &&
|
|
646
|
+
(current.mode === 'async' || wasAnsweredBefore));
|
|
647
|
+
if (shouldDeliver) options.onAsyncAskAnswered?.(next);
|
|
648
|
+
return { ...plan, final, applied: true };
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/** 直接发布到流上的应答(TUI / policy 定时器)也要落审计;不重新发布。 */
|
|
652
|
+
function recordStreamResponse(event: RunEvent): void {
|
|
653
|
+
const data = isRecord(event.data) ? event.data : {};
|
|
654
|
+
const requestId = nonEmptyString(data.requestId);
|
|
655
|
+
if (requestId === undefined) return;
|
|
656
|
+
const record = interactionRecords.get(requestId);
|
|
657
|
+
if (record === undefined || record.status !== 'pending') return;
|
|
658
|
+
const isAsk = event.type === 'ask.user.answered';
|
|
659
|
+
if (((record.kind ?? 'permission') === 'ask') !== isAsk) return;
|
|
660
|
+
const answeredBy = readAnsweredBy(data.answered_by);
|
|
661
|
+
// 非法归属(含伪造的 agent)不得写进审计。
|
|
662
|
+
if (answeredBy === undefined) return;
|
|
663
|
+
const answers = Array.isArray(data.answers) ? data.answers : undefined;
|
|
664
|
+
const decision =
|
|
665
|
+
data.decision === 'allow' || data.decision === 'deny' ? data.decision : undefined;
|
|
666
|
+
if (isAsk) {
|
|
667
|
+
if (answers === undefined) return;
|
|
668
|
+
} else if (decision === undefined) {
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
const answeredAt = eventTs(event);
|
|
673
|
+
const rawScope = data.scope;
|
|
674
|
+
const scope: PermissionScope =
|
|
675
|
+
rawScope === 'once' || rawScope === 'session' || rawScope === 'always' ? rawScope : 'once';
|
|
676
|
+
const answeredVia =
|
|
677
|
+
data.answered_via === 'tui' ||
|
|
678
|
+
data.answered_via === 'rpc' ||
|
|
679
|
+
data.answered_via === 'cli' ||
|
|
680
|
+
data.answered_via === 'policy'
|
|
681
|
+
? data.answered_via
|
|
682
|
+
: undefined;
|
|
683
|
+
const policy =
|
|
684
|
+
data.policy === 'deny_after' || data.policy === 'auto_allow' ? data.policy : undefined;
|
|
685
|
+
const batchId = nonEmptyString(data.batch_id);
|
|
686
|
+
const latency = Math.max(0, answeredAt - record.created_at);
|
|
687
|
+
pendingPermissions.delete(requestId);
|
|
688
|
+
if (!isAsk) {
|
|
689
|
+
settlePermission(record, {
|
|
690
|
+
decision: decision as 'allow' | 'deny',
|
|
691
|
+
scope,
|
|
692
|
+
answered_at: answeredAt,
|
|
693
|
+
answered_by: answeredBy,
|
|
694
|
+
latency_ms: latency,
|
|
695
|
+
...(answeredVia !== undefined ? { answered_via: answeredVia } : {}),
|
|
696
|
+
...(policy !== undefined ? { policy } : {}),
|
|
697
|
+
});
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
settleAnswer(record, {
|
|
701
|
+
answers: answers as unknown[],
|
|
702
|
+
answered_at: answeredAt,
|
|
703
|
+
answered_by: answeredBy,
|
|
704
|
+
latency_ms: latency,
|
|
705
|
+
...(answeredVia !== undefined ? { answered_via: answeredVia } : {}),
|
|
706
|
+
...(batchId !== undefined ? { batch_id: batchId } : {}),
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* 权限应答的收口(`answered` 是唯一终态,没有逐题概念)。
|
|
712
|
+
*
|
|
713
|
+
* 与 ask 的 {@link settleAnswer} 分家:权限没有「部分已答」,把两者塞进一个函数
|
|
714
|
+
* 会让 `planAskAnswers` 的逐题语义渗进只看 `decision` 的权限记录里。
|
|
715
|
+
*/
|
|
716
|
+
function settlePermission(record: StoredInteraction, answer: StoredInteractionAnswer): void {
|
|
717
|
+
suspendedAskIds.delete(record.requestId);
|
|
718
|
+
rememberRecord({ ...record, status: 'answered' as const, answer });
|
|
719
|
+
persist();
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* q-human-notify P1:通知收口(ack)的唯一落记录出口。
|
|
724
|
+
*
|
|
725
|
+
* 与权限 {@link settlePermission} 同形(通知没有「部分已答」概念):复用三态,
|
|
726
|
+
* `kind:'notify' ∧ status:'answered'` = 已读。幂等:只收 pending,已 answered 不动。
|
|
727
|
+
* **不通知投放**(fire-and-forget,ack 不回流模型)—— 不调 onAsyncAskAnswered。
|
|
728
|
+
*/
|
|
729
|
+
function settleNotify(record: StoredInteraction, answer: StoredInteractionAnswer): void {
|
|
730
|
+
if (record.status !== 'pending') return;
|
|
731
|
+
rememberRecord({ ...record, status: 'answered' as const, answer });
|
|
732
|
+
persist();
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
function handleEvent(event: RunEvent): void {
|
|
736
|
+
if (event.type === 'tool.permission.request') {
|
|
737
|
+
const data = isRecord(event.data) ? event.data : {};
|
|
738
|
+
const requestId = nonEmptyString(data.requestId);
|
|
739
|
+
if (requestId !== undefined) {
|
|
740
|
+
const requestTs = eventTs(event);
|
|
741
|
+
const tool = typeof data.tool === 'string' ? data.tool : 'tool';
|
|
742
|
+
const sessionId = nonEmptyString(data.session_id);
|
|
743
|
+
const toolCallId = nonEmptyString(data.tool_call_id);
|
|
744
|
+
const parentAgentId = nonEmptyString(data.parent_agent_id);
|
|
745
|
+
const agentId = nonEmptyString(data.agent_id);
|
|
746
|
+
const teamId = nonEmptyString(data.team_id);
|
|
747
|
+
const deadlineAt =
|
|
748
|
+
typeof data.deadline_at === 'number' &&
|
|
749
|
+
Number.isFinite(data.deadline_at) &&
|
|
750
|
+
data.deadline_at > 0
|
|
751
|
+
? data.deadline_at
|
|
752
|
+
: undefined;
|
|
753
|
+
pendingPermissions.set(requestId, {
|
|
754
|
+
run_id: event.run_id,
|
|
755
|
+
tool,
|
|
756
|
+
input: data.input,
|
|
757
|
+
ts: requestTs,
|
|
758
|
+
...(deadlineAt !== undefined ? { deadline_at: deadlineAt } : {}),
|
|
759
|
+
...(sessionId !== undefined ? { session_id: sessionId } : {}),
|
|
760
|
+
...(parentAgentId !== undefined ? { parent_agent_id: parentAgentId } : {}),
|
|
761
|
+
...(toolCallId !== undefined ? { tool_call_id: toolCallId } : {}),
|
|
762
|
+
...(typeof data.source === 'string' && data.source.length > 0
|
|
763
|
+
? { source: data.source }
|
|
764
|
+
: {}),
|
|
765
|
+
});
|
|
766
|
+
rememberRecord({
|
|
767
|
+
requestId,
|
|
768
|
+
run_id: event.run_id,
|
|
769
|
+
kind: 'permission',
|
|
770
|
+
...(agentId !== undefined ? { agent_id: agentId } : {}),
|
|
771
|
+
...(teamId !== undefined ? { team_id: teamId } : {}),
|
|
772
|
+
...(sessionId !== undefined ? { session_id: sessionId } : {}),
|
|
773
|
+
...(parentAgentId !== undefined ? { parent_agent_id: parentAgentId } : {}),
|
|
774
|
+
...(toolCallId !== undefined ? { tool_call_id: toolCallId } : {}),
|
|
775
|
+
tool,
|
|
776
|
+
input: data.input,
|
|
777
|
+
created_at: requestTs,
|
|
778
|
+
status: 'pending',
|
|
779
|
+
});
|
|
780
|
+
persist();
|
|
781
|
+
}
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
if (event.type === 'tool.permission.expired') {
|
|
785
|
+
// q-card-robust P-B:审批超时收口。此前注册表不处理 expired —— 记录仍
|
|
786
|
+
// pending 直到 run 终态才 orphaned,本端浏览器靠 rememberExpiredRequestId
|
|
787
|
+
// 不复活死卡,但**换设备/换浏览器刷新**会在「expired 之后、run 终态之前」
|
|
788
|
+
// 的窗口里经 interactions.list 补回一张点了也没用的卡。这里把记录收口为
|
|
789
|
+
// orphaned(+原因),让任何端的快照都收敛;迟到应答走 respondPermission
|
|
790
|
+
// 的 run_ended 分支,不再投递进已无人等待的流。
|
|
791
|
+
// 幂等:只收 pending 的 permission 记录;已 answered / 已 orphaned(含
|
|
792
|
+
// run 终态孤儿化)原样保留,终态清理循环也只碰 pending,互不覆盖。
|
|
793
|
+
const data = isRecord(event.data) ? event.data : {};
|
|
794
|
+
const requestId = nonEmptyString(data.requestId);
|
|
795
|
+
if (requestId !== undefined) {
|
|
796
|
+
pendingPermissions.delete(requestId);
|
|
797
|
+
const record = interactionRecords.get(requestId);
|
|
798
|
+
if (
|
|
799
|
+
record !== undefined &&
|
|
800
|
+
record.status === 'pending' &&
|
|
801
|
+
(record.kind ?? 'permission') === 'permission'
|
|
802
|
+
) {
|
|
803
|
+
rememberRecord({
|
|
804
|
+
...record,
|
|
805
|
+
status: 'orphaned' as StoredInteractionStatus,
|
|
806
|
+
orphan_reason: nonEmptyString(data.reason) ?? 'expired',
|
|
807
|
+
});
|
|
808
|
+
persist();
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
if (event.type === 'ask.user.requested') {
|
|
814
|
+
const data = isRecord(event.data) ? event.data : {};
|
|
815
|
+
const requestId = nonEmptyString(data.requestId);
|
|
816
|
+
if (requestId !== undefined) {
|
|
817
|
+
const sessionId = nonEmptyString(data.session_id);
|
|
818
|
+
const parentAgentId = nonEmptyString(data.parent_agent_id);
|
|
819
|
+
const agentId = nonEmptyString(data.agent_id);
|
|
820
|
+
const teamId = nonEmptyString(data.team_id);
|
|
821
|
+
const toolCallId = nonEmptyString(data.tool_call_id);
|
|
822
|
+
rememberRecord({
|
|
823
|
+
requestId,
|
|
824
|
+
run_id: event.run_id,
|
|
825
|
+
kind: 'ask',
|
|
826
|
+
...(agentId !== undefined ? { agent_id: agentId } : {}),
|
|
827
|
+
...(teamId !== undefined ? { team_id: teamId } : {}),
|
|
828
|
+
...(sessionId !== undefined ? { session_id: sessionId } : {}),
|
|
829
|
+
...(parentAgentId !== undefined ? { parent_agent_id: parentAgentId } : {}),
|
|
830
|
+
...(toolCallId !== undefined ? { tool_call_id: toolCallId } : {}),
|
|
831
|
+
// 0.52:投递语义跟着请求走;缺省不写该键 = sync(旧事件逐字节不变)。
|
|
832
|
+
...(data.mode === 'async' ? { mode: 'async' as const } : {}),
|
|
833
|
+
// 读侧兜底:事件没带 tool 时按现行工具名写;历史事件里该字段是 `ask_user`,
|
|
834
|
+
// 原样保留(记录只被读、不被比较)。
|
|
835
|
+
tool: typeof data.tool === 'string' && data.tool.length > 0 ? data.tool : 'ask',
|
|
836
|
+
input: data.questions,
|
|
837
|
+
created_at: eventTs(event),
|
|
838
|
+
status: 'pending',
|
|
839
|
+
});
|
|
840
|
+
persist();
|
|
841
|
+
}
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
if (event.type === 'notify.user.requested') {
|
|
845
|
+
const data = isRecord(event.data) ? event.data : {};
|
|
846
|
+
const requestId = nonEmptyString(data.requestId);
|
|
847
|
+
if (requestId !== undefined) {
|
|
848
|
+
const sessionId = nonEmptyString(data.session_id);
|
|
849
|
+
const parentAgentId = nonEmptyString(data.parent_agent_id);
|
|
850
|
+
const agentId = nonEmptyString(data.agent_id);
|
|
851
|
+
const teamId = nonEmptyString(data.team_id);
|
|
852
|
+
const toolCallId = nonEmptyString(data.tool_call_id);
|
|
853
|
+
// D7 预算:每 agent 未读通知 cap 50,满则拒新(不静默丢人的注意力,logEvent 可见)。
|
|
854
|
+
// 同内容短窗去重 / 每 run 速率闸在生产工具侧(模型可见的显式拒绝);这里是存储有界闸。
|
|
855
|
+
let unread = 0;
|
|
856
|
+
for (const item of interactionRecords.values()) {
|
|
857
|
+
if (item.kind === 'notify' && item.status === 'pending' && item.agent_id === agentId) {
|
|
858
|
+
unread += 1;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
if (unread >= NOTIFY_UNREAD_CAP) {
|
|
862
|
+
logEvent('warn', 'interactions.notify.full', {
|
|
863
|
+
request_id: requestId,
|
|
864
|
+
agent_id: agentId,
|
|
865
|
+
cap: NOTIFY_UNREAD_CAP,
|
|
866
|
+
});
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
rememberRecord({
|
|
870
|
+
requestId,
|
|
871
|
+
run_id: event.run_id,
|
|
872
|
+
kind: 'notify',
|
|
873
|
+
...(agentId !== undefined ? { agent_id: agentId } : {}),
|
|
874
|
+
...(teamId !== undefined ? { team_id: teamId } : {}),
|
|
875
|
+
...(sessionId !== undefined ? { session_id: sessionId } : {}),
|
|
876
|
+
...(parentAgentId !== undefined ? { parent_agent_id: parentAgentId } : {}),
|
|
877
|
+
...(toolCallId !== undefined ? { tool_call_id: toolCallId } : {}),
|
|
878
|
+
tool: typeof data.tool === 'string' && data.tool.length > 0 ? data.tool : 'notify',
|
|
879
|
+
// 通知 payload 复用 input 装载(零新字段):title/body/refs/salience。
|
|
880
|
+
input: {
|
|
881
|
+
title: typeof data.title === 'string' ? data.title : '',
|
|
882
|
+
body: typeof data.body === 'string' ? data.body : '',
|
|
883
|
+
...(Array.isArray(data.refs) ? { refs: data.refs } : {}),
|
|
884
|
+
...(data.salience === 'high' ? { salience: 'high' as const } : {}),
|
|
885
|
+
},
|
|
886
|
+
created_at: eventTs(event),
|
|
887
|
+
status: 'pending',
|
|
888
|
+
});
|
|
889
|
+
persist();
|
|
890
|
+
}
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
893
|
+
if (event.type === 'notify.user.acked') {
|
|
894
|
+
// 流上回声(RPC ackNotify 发布):幂等收口为 answered(已读)。重复回声 no-op。
|
|
895
|
+
const data = isRecord(event.data) ? event.data : {};
|
|
896
|
+
const requestId = nonEmptyString(data.requestId);
|
|
897
|
+
if (requestId !== undefined) {
|
|
898
|
+
const record = interactionRecords.get(requestId);
|
|
899
|
+
if (record !== undefined && record.kind === 'notify' && record.status === 'pending') {
|
|
900
|
+
const answeredBy = readAnsweredBy(data.answered_by) ?? 'human';
|
|
901
|
+
settleNotify(record, {
|
|
902
|
+
answered_at: eventTs(event),
|
|
903
|
+
answered_by: answeredBy,
|
|
904
|
+
...(data.answered_via === 'tui' ||
|
|
905
|
+
data.answered_via === 'rpc' ||
|
|
906
|
+
data.answered_via === 'cli' ||
|
|
907
|
+
data.answered_via === 'policy'
|
|
908
|
+
? { answered_via: data.answered_via }
|
|
909
|
+
: {}),
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
915
|
+
if (event.type === 'ask.user.timeout') {
|
|
916
|
+
// headless 等不到人类:run 即将以 `interrupted` 收口。标住 requestId,
|
|
917
|
+
// 下面的终态清理会保留它以等待迟到的回答;同时把 suspended 写进记录,
|
|
918
|
+
// 让下一个 gateway 进程启动 hydrate 时也不把它转 orphaned。
|
|
919
|
+
const data = isRecord(event.data) ? event.data : {};
|
|
920
|
+
const requestId = nonEmptyString(data.requestId);
|
|
921
|
+
if (requestId !== undefined) {
|
|
922
|
+
suspendedAskIds.add(requestId);
|
|
923
|
+
const record = interactionRecords.get(requestId);
|
|
924
|
+
if (
|
|
925
|
+
record !== undefined &&
|
|
926
|
+
record.status === 'pending' &&
|
|
927
|
+
(record.kind ?? 'permission') === 'ask' &&
|
|
928
|
+
record.suspended !== true
|
|
929
|
+
) {
|
|
930
|
+
rememberRecord({ ...record, suspended: true });
|
|
931
|
+
persist();
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
if (event.type === 'tool.permission.response' || event.type === 'ask.user.answered') {
|
|
937
|
+
recordStreamResponse(event);
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
if (
|
|
941
|
+
event.type === 'run.completed' ||
|
|
942
|
+
event.type === 'run.failed' ||
|
|
943
|
+
event.type === 'run.cancelled'
|
|
944
|
+
) {
|
|
945
|
+
// run 终态:该 run 的 pending(permission + ask)全部转 orphaned;
|
|
946
|
+
// 迟到应答返回 run_ended,不再假装可以继续。
|
|
947
|
+
for (const [id, record] of interactionRecords) {
|
|
948
|
+
if (record.status !== 'pending' || record.run_id !== event.run_id) continue;
|
|
949
|
+
// q-human-notify P1:通知不随 run 终态孤儿化(run 早已收口,未读通知仍有效)。
|
|
950
|
+
if (record.kind === 'notify') continue;
|
|
951
|
+
if ((record.kind ?? 'permission') === 'ask') {
|
|
952
|
+
// headless ask 的 run 以 interrupted 收口:请求仍然有效,保留 pending。
|
|
953
|
+
if (suspendedAskIds.has(id)) continue;
|
|
954
|
+
// 0.52:async ask 的 run 在工具返回后照常收口,请求本身还没被回答 —— 保留 pending。
|
|
955
|
+
if (record.mode === 'async') continue;
|
|
956
|
+
// 分批:**部分已答**的 ask 也保留 —— 提问的 run 已经拿到首批答案继续跑了,
|
|
957
|
+
// 剩下那几题还在等人;孤儿化会让「第 3 题」永远答不了。
|
|
958
|
+
if (hasPartialAnswers(record)) continue;
|
|
959
|
+
}
|
|
960
|
+
pendingPermissions.delete(id);
|
|
961
|
+
rememberRecord({ ...record, status: 'orphaned' as StoredInteractionStatus });
|
|
962
|
+
}
|
|
963
|
+
persist();
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
function respondPermission(
|
|
968
|
+
requestId: string,
|
|
969
|
+
decision: 'allow' | 'deny',
|
|
970
|
+
remember: boolean,
|
|
971
|
+
scope?: PermissionScope,
|
|
972
|
+
actor?: InteractionActor,
|
|
973
|
+
): PermissionRespondResult {
|
|
974
|
+
// scope 优先;remember 是旧客户端的兼容写法(等价 always)。
|
|
975
|
+
const resolvedScope: PermissionScope = scope ?? (remember ? 'always' : 'once');
|
|
976
|
+
const attribution: InteractionActor = actor ?? { answered_by: 'human' };
|
|
977
|
+
const pending = pendingPermissions.get(requestId);
|
|
978
|
+
if (pending !== undefined) {
|
|
979
|
+
const record = interactionRecords.get(requestId);
|
|
980
|
+
const now = Date.now();
|
|
981
|
+
const latency = Math.max(0, now - (record?.created_at ?? pending.ts));
|
|
982
|
+
// 先投递;流缺失抛 run_ended,pending 保持不变(迟到的流仍可重试)。
|
|
983
|
+
answerStream().publish({
|
|
984
|
+
type: 'tool.permission.response',
|
|
985
|
+
run_id: pending.run_id,
|
|
986
|
+
data: {
|
|
987
|
+
requestId,
|
|
988
|
+
decision,
|
|
989
|
+
scope: resolvedScope,
|
|
990
|
+
...(resolvedScope === 'always' ? { remember: true } : {}),
|
|
991
|
+
...(pending.session_id !== undefined ? { session_id: pending.session_id } : {}),
|
|
992
|
+
answered_by: attribution.answered_by,
|
|
993
|
+
...(attribution.answered_via !== undefined
|
|
994
|
+
? { answered_via: attribution.answered_via }
|
|
995
|
+
: {}),
|
|
996
|
+
latency_ms: latency,
|
|
997
|
+
...(record?.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
998
|
+
},
|
|
999
|
+
ts: now,
|
|
1000
|
+
});
|
|
1001
|
+
pendingPermissions.delete(requestId);
|
|
1002
|
+
rememberRecord({
|
|
1003
|
+
...(record ?? {
|
|
1004
|
+
requestId,
|
|
1005
|
+
run_id: pending.run_id,
|
|
1006
|
+
kind: 'permission' as const,
|
|
1007
|
+
tool: pending.tool,
|
|
1008
|
+
input: pending.input,
|
|
1009
|
+
created_at: pending.ts,
|
|
1010
|
+
}),
|
|
1011
|
+
status: 'answered',
|
|
1012
|
+
answer: {
|
|
1013
|
+
decision,
|
|
1014
|
+
scope: resolvedScope,
|
|
1015
|
+
answered_at: now,
|
|
1016
|
+
answered_by: attribution.answered_by,
|
|
1017
|
+
...(attribution.answered_via !== undefined
|
|
1018
|
+
? { answered_via: attribution.answered_via }
|
|
1019
|
+
: {}),
|
|
1020
|
+
latency_ms: latency,
|
|
1021
|
+
},
|
|
1022
|
+
});
|
|
1023
|
+
persist();
|
|
1024
|
+
return {
|
|
1025
|
+
ok: true,
|
|
1026
|
+
decision,
|
|
1027
|
+
scope: resolvedScope,
|
|
1028
|
+
...(actor !== undefined
|
|
1029
|
+
? {
|
|
1030
|
+
answered_by: attribution.answered_by,
|
|
1031
|
+
...(attribution.answered_via !== undefined
|
|
1032
|
+
? { answered_via: attribution.answered_via }
|
|
1033
|
+
: {}),
|
|
1034
|
+
latency_ms: latency,
|
|
1035
|
+
...(record?.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
1036
|
+
}
|
|
1037
|
+
: {}),
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
// 幂等:首次应答生效,重复应答返回上次结果(不发布新事件)。
|
|
1042
|
+
const record = interactionRecords.get(requestId);
|
|
1043
|
+
if (record !== undefined) {
|
|
1044
|
+
if (record.status === 'answered' && record.answer !== undefined) {
|
|
1045
|
+
if (record.answer.decision !== decision) {
|
|
1046
|
+
throw new RpcError(
|
|
1047
|
+
'conflict',
|
|
1048
|
+
`Permission request already answered as ${record.answer.decision}: ${requestId}`,
|
|
1049
|
+
);
|
|
1050
|
+
}
|
|
1051
|
+
return {
|
|
1052
|
+
ok: true,
|
|
1053
|
+
decision,
|
|
1054
|
+
scope: record.answer.scope ?? 'once',
|
|
1055
|
+
duplicate: true,
|
|
1056
|
+
...(actor !== undefined
|
|
1057
|
+
? {
|
|
1058
|
+
answered_by: record.answer.answered_by ?? 'human',
|
|
1059
|
+
...(record.answer.answered_via !== undefined
|
|
1060
|
+
? { answered_via: record.answer.answered_via }
|
|
1061
|
+
: {}),
|
|
1062
|
+
...(record.answer.latency_ms !== undefined
|
|
1063
|
+
? { latency_ms: record.answer.latency_ms }
|
|
1064
|
+
: {}),
|
|
1065
|
+
...(record.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
1066
|
+
}
|
|
1067
|
+
: {}),
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
// run 已终止 / 进程重启后找不到 run:明确告知,不伪装成 not_found。
|
|
1071
|
+
throw new RpcError(
|
|
1072
|
+
'run_ended',
|
|
1073
|
+
`Run has ended; permission request is no longer valid: ${requestId}`,
|
|
1074
|
+
{
|
|
1075
|
+
status: record.status === 'pending' ? 'orphaned' : record.status,
|
|
1076
|
+
run_id: record.run_id,
|
|
1077
|
+
requestId,
|
|
1078
|
+
},
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
throw new RpcError('not_found', `Permission request not found: ${requestId}`);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
function respondAsk(
|
|
1086
|
+
requestId: string,
|
|
1087
|
+
answers: unknown[],
|
|
1088
|
+
actor?: InteractionActor,
|
|
1089
|
+
context?: AskRespondContext,
|
|
1090
|
+
): AskRespondResult {
|
|
1091
|
+
const attribution: InteractionActor = actor ?? { answered_by: 'human' };
|
|
1092
|
+
const record = interactionRecords.get(requestId);
|
|
1093
|
+
if (record === undefined || (record.kind ?? 'permission') !== 'ask') {
|
|
1094
|
+
throw new RpcError('not_found', `Ask request not found: ${requestId}`);
|
|
1095
|
+
}
|
|
1096
|
+
const batchId =
|
|
1097
|
+
context?.batch_id !== undefined && context.batch_id !== '' ? context.batch_id : undefined;
|
|
1098
|
+
if (record.status === 'pending') {
|
|
1099
|
+
const now = Date.now();
|
|
1100
|
+
const latency = Math.max(0, now - record.created_at);
|
|
1101
|
+
const declined = answers.length === 0;
|
|
1102
|
+
const plan = planAskAnswers(record, answers);
|
|
1103
|
+
if (!declined && plan.fresh.length === 0) {
|
|
1104
|
+
// 每一题都已经答过:这是幂等重放(双击 / 多端重复提交)。
|
|
1105
|
+
// 不发事件、不落盘、不投放 —— 只把「跳过了哪些题」明确回给调用方,
|
|
1106
|
+
// 免得前端把一次重复提交当成「又答了一遍」。
|
|
1107
|
+
return {
|
|
1108
|
+
ok: true,
|
|
1109
|
+
duplicate: true,
|
|
1110
|
+
answered_by: attribution.answered_by,
|
|
1111
|
+
...(attribution.answered_via !== undefined
|
|
1112
|
+
? { answered_via: attribution.answered_via }
|
|
1113
|
+
: {}),
|
|
1114
|
+
latency_ms: Math.max(0, now - (record.answer?.answered_at ?? record.created_at)),
|
|
1115
|
+
...(record.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
1116
|
+
answered_questions: [],
|
|
1117
|
+
remaining_questions: plan.remaining,
|
|
1118
|
+
// 旧客户端的三行答案认不出题(没有 question_id 也没有题序可依)时,
|
|
1119
|
+
// 用「已答的题」回答「跳过的题」—— 语义上就是那几题不重复记录。
|
|
1120
|
+
skipped_questions: plan.skipped.length > 0 ? plan.skipped : plan.answered,
|
|
1121
|
+
final: plan.final,
|
|
1122
|
+
};
|
|
1123
|
+
}
|
|
1124
|
+
// 直接投递到单运行时流(ask 工具在那里等待)。
|
|
1125
|
+
//
|
|
1126
|
+
// 注意:这次 publish 是**同步**扇出的 —— 原始订阅者(hub → gateway → 本注册表)
|
|
1127
|
+
// 会立刻走 `recordStreamResponse` → `settleAnswer`,也就是「流上回声」这一次才是
|
|
1128
|
+
// 真正落记录 + 通知投放。下面那次 `settleAnswer` 用的是 publish 之前的快照,
|
|
1129
|
+
// 它被 `fresh` 幂等判定挡成重放(`applied: false`),不会二次投放。
|
|
1130
|
+
//
|
|
1131
|
+
// 事件里只带**本批**答案(`plan.fresh`):`final: false` 时它就是「先答了这几题」,
|
|
1132
|
+
// 未答的题仍在待批队列;工具拿到这一批就返回,不等其余题。
|
|
1133
|
+
answerStream().publish({
|
|
1134
|
+
type: 'ask.user.answered',
|
|
1135
|
+
run_id: record.run_id,
|
|
1136
|
+
data: {
|
|
1137
|
+
requestId,
|
|
1138
|
+
answers: declined ? [] : plan.fresh,
|
|
1139
|
+
final: declined || plan.final,
|
|
1140
|
+
...(declined || plan.final ? {} : { remaining: plan.remaining }),
|
|
1141
|
+
...(batchId !== undefined ? { batch_id: batchId } : {}),
|
|
1142
|
+
...(record.session_id !== undefined ? { session_id: record.session_id } : {}),
|
|
1143
|
+
answered_by: attribution.answered_by,
|
|
1144
|
+
...(attribution.answered_via !== undefined
|
|
1145
|
+
? { answered_via: attribution.answered_via }
|
|
1146
|
+
: {}),
|
|
1147
|
+
latency_ms: latency,
|
|
1148
|
+
...(record.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
1149
|
+
},
|
|
1150
|
+
ts: now,
|
|
1151
|
+
});
|
|
1152
|
+
// publish 之后的本地兜底:装配里没有原始订阅者时(离线 attention / 测试)要靠
|
|
1153
|
+
// 这一句落记录。`settleAnswer` 自己按 `fresh` 幂等,所以回声已落盘时它是空操作。
|
|
1154
|
+
settleAnswer(record, {
|
|
1155
|
+
answers: declined ? [] : plan.fresh,
|
|
1156
|
+
answered_at: now,
|
|
1157
|
+
answered_by: attribution.answered_by,
|
|
1158
|
+
...(attribution.answered_via !== undefined
|
|
1159
|
+
? { answered_via: attribution.answered_via }
|
|
1160
|
+
: {}),
|
|
1161
|
+
latency_ms: latency,
|
|
1162
|
+
...(batchId !== undefined ? { batch_id: batchId } : {}),
|
|
1163
|
+
});
|
|
1164
|
+
// 返回体描述的是**这次提交**写成了什么(不是上面那次空转):题号来自本批的规划,
|
|
1165
|
+
// 其余字段以落定后的记录为准。客户端据此决定「卡片还能不能收」。
|
|
1166
|
+
const answeredBefore = new Set(record.answered_questions ?? []);
|
|
1167
|
+
const writtenNow = declined ? [] : plan.answered.filter((id) => !answeredBefore.has(id));
|
|
1168
|
+
return {
|
|
1169
|
+
ok: true,
|
|
1170
|
+
answered_by: attribution.answered_by,
|
|
1171
|
+
...(attribution.answered_via !== undefined
|
|
1172
|
+
? { answered_via: attribution.answered_via }
|
|
1173
|
+
: {}),
|
|
1174
|
+
latency_ms: latency,
|
|
1175
|
+
...(record.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
1176
|
+
answered_questions: writtenNow,
|
|
1177
|
+
remaining_questions: declined ? [] : plan.remaining,
|
|
1178
|
+
...(plan.skipped.length > 0 ? { skipped_questions: plan.skipped } : {}),
|
|
1179
|
+
final: declined || plan.final,
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
if (record.status === 'answered') {
|
|
1183
|
+
// 已收口的请求:同题再来 = 幂等(duplicate + skipped);不同答案仍是冲突。
|
|
1184
|
+
const plan = planAskAnswers(record, answers);
|
|
1185
|
+
const recordedAnswers = record.answer?.answers ?? [];
|
|
1186
|
+
const sameAnswers = jsonEqual(recordedAnswers, answers);
|
|
1187
|
+
// 逐题幂等:这一批里一题都没新增,且至少认出了一题是「已答过」的。
|
|
1188
|
+
const perQuestionDuplicate = plan.fresh.length === 0 && plan.skipped.length > 0;
|
|
1189
|
+
if (sameAnswers || perQuestionDuplicate) {
|
|
1190
|
+
return {
|
|
1191
|
+
ok: true,
|
|
1192
|
+
duplicate: true,
|
|
1193
|
+
answered_by: record.answer?.answered_by ?? 'human',
|
|
1194
|
+
...(record.answer?.answered_via !== undefined
|
|
1195
|
+
? { answered_via: record.answer.answered_via }
|
|
1196
|
+
: {}),
|
|
1197
|
+
...(record.answer?.latency_ms !== undefined
|
|
1198
|
+
? { latency_ms: record.answer.latency_ms }
|
|
1199
|
+
: {}),
|
|
1200
|
+
...(record.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
1201
|
+
answered_questions: [],
|
|
1202
|
+
remaining_questions: [],
|
|
1203
|
+
...(plan.skipped.length > 0 ? { skipped_questions: plan.skipped } : {}),
|
|
1204
|
+
final: true,
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
throw new RpcError(
|
|
1208
|
+
'conflict',
|
|
1209
|
+
`Ask request already answered with different answers: ${requestId}`,
|
|
1210
|
+
);
|
|
1211
|
+
}
|
|
1212
|
+
throw new RpcError('run_ended', `Run has ended; ask request is no longer valid: ${requestId}`, {
|
|
1213
|
+
status: record.status,
|
|
1214
|
+
run_id: record.run_id,
|
|
1215
|
+
requestId,
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
function ackNotify(requestId: string, actor?: InteractionActor): NotifyAckResult {
|
|
1220
|
+
const attribution: InteractionActor = actor ?? { answered_by: 'human' };
|
|
1221
|
+
const record = interactionRecords.get(requestId);
|
|
1222
|
+
if (record === undefined || record.kind !== 'notify') {
|
|
1223
|
+
throw new RpcError('not_found', `Notify request not found: ${requestId}`);
|
|
1224
|
+
}
|
|
1225
|
+
if (record.status === 'answered') {
|
|
1226
|
+
// 幂等:重复 ack 沿用第一次结果,不再发事件。
|
|
1227
|
+
return {
|
|
1228
|
+
ok: true,
|
|
1229
|
+
duplicate: true,
|
|
1230
|
+
answered_by: record.answer?.answered_by ?? 'human',
|
|
1231
|
+
...(record.answer?.answered_via !== undefined
|
|
1232
|
+
? { answered_via: record.answer.answered_via }
|
|
1233
|
+
: {}),
|
|
1234
|
+
...(record.answer?.latency_ms !== undefined
|
|
1235
|
+
? { latency_ms: record.answer.latency_ms }
|
|
1236
|
+
: {}),
|
|
1237
|
+
...(record.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
1238
|
+
};
|
|
1239
|
+
}
|
|
1240
|
+
if (record.status !== 'pending') {
|
|
1241
|
+
throw new RpcError('run_ended', `Notify request is no longer valid: ${requestId}`, {
|
|
1242
|
+
status: record.status,
|
|
1243
|
+
run_id: record.run_id,
|
|
1244
|
+
requestId,
|
|
1245
|
+
});
|
|
1246
|
+
}
|
|
1247
|
+
const now = Date.now();
|
|
1248
|
+
const latency = Math.max(0, now - record.created_at);
|
|
1249
|
+
const answer: StoredInteractionAnswer = {
|
|
1250
|
+
answered_at: now,
|
|
1251
|
+
answered_by: attribution.answered_by,
|
|
1252
|
+
...(attribution.answered_via !== undefined ? { answered_via: attribution.answered_via } : {}),
|
|
1253
|
+
latency_ms: latency,
|
|
1254
|
+
answers: [],
|
|
1255
|
+
};
|
|
1256
|
+
// 发 `notify.user.acked` 供各端收卡(best-effort:无流时仍落记录,ack 是纯人类记账)。
|
|
1257
|
+
// 流上回声会再走一次 settleNotify,但它按 pending 幂等,回声已落盘时是空操作。
|
|
1258
|
+
const stream = eventsFor();
|
|
1259
|
+
if (stream !== undefined) {
|
|
1260
|
+
stream.publish({
|
|
1261
|
+
type: 'notify.user.acked',
|
|
1262
|
+
run_id: record.run_id,
|
|
1263
|
+
data: {
|
|
1264
|
+
requestId,
|
|
1265
|
+
...(record.session_id !== undefined ? { session_id: record.session_id } : {}),
|
|
1266
|
+
answered_by: attribution.answered_by,
|
|
1267
|
+
...(attribution.answered_via !== undefined
|
|
1268
|
+
? { answered_via: attribution.answered_via }
|
|
1269
|
+
: {}),
|
|
1270
|
+
latency_ms: latency,
|
|
1271
|
+
...(record.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
1272
|
+
},
|
|
1273
|
+
ts: now,
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
settleNotify(record, answer);
|
|
1277
|
+
return {
|
|
1278
|
+
ok: true,
|
|
1279
|
+
answered_by: attribution.answered_by,
|
|
1280
|
+
...(attribution.answered_via !== undefined ? { answered_via: attribution.answered_via } : {}),
|
|
1281
|
+
latency_ms: latency,
|
|
1282
|
+
...(record.team_id !== undefined ? { team_id: record.team_id } : {}),
|
|
1283
|
+
};
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
/**
|
|
1287
|
+
* 0.52:async 答案投放成功后写幂等标记(`answer.delivered_at`)。
|
|
1288
|
+
*
|
|
1289
|
+
* 只认已经 answered 的记录;返回 false 表示没有可标记的状态(记录不存在 /
|
|
1290
|
+
* 还没答完 / 已被别处投放)。重复应答时调用方据此跳过第二次投放。
|
|
1291
|
+
*
|
|
1292
|
+
* **分批的一部分**:部分已答的记录仍是 `pending`(还有题在等人),这里于是拒写标记 ——
|
|
1293
|
+
* 下一批答案因此还能再投一次。这不是漏洞,正是「每批各投一次」的实现方式:
|
|
1294
|
+
* 整条请求一收口(`answered`)就写标记,终结之后的重复应答不再重投。
|
|
1295
|
+
*/
|
|
1296
|
+
function markAnswerDelivered(requestId: string, deliveredAt: number): boolean {
|
|
1297
|
+
const record = interactionRecords.get(requestId);
|
|
1298
|
+
if (record === undefined || record.status !== 'answered' || record.answer === undefined) {
|
|
1299
|
+
return false;
|
|
1300
|
+
}
|
|
1301
|
+
if (record.answer.delivered_at !== undefined) return true;
|
|
1302
|
+
rememberRecord({ ...record, answer: { ...record.answer, delivered_at: deliveredAt } });
|
|
1303
|
+
persist();
|
|
1304
|
+
return true;
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
/**
|
|
1308
|
+
* 0.52:从磁盘重读一条记录(只读;见接口注释里的跨进程用途)。
|
|
1309
|
+
*
|
|
1310
|
+
* 整文件读一次而不是缓存:这条路径每次 async 答案最多走一次,换「永不陈旧」。
|
|
1311
|
+
* 任何读失败都当作「没有这条记录」,不阻断投放(宁可多投一次也不丢答案)。
|
|
1312
|
+
*/
|
|
1313
|
+
function peekPersistedAnswer(requestId: string): StoredInteraction | undefined {
|
|
1314
|
+
const store = interactionsStore;
|
|
1315
|
+
if (store === undefined) return undefined;
|
|
1316
|
+
try {
|
|
1317
|
+
return store.loadAllSync().find((record) => record.requestId === requestId);
|
|
1318
|
+
} catch (error) {
|
|
1319
|
+
// 0.60:单行 key=value(唯一排版口),不再把对象直接丢给 console。
|
|
1320
|
+
logEvent('warn', 'interactions.registry.reload_failed', {
|
|
1321
|
+
request_id: requestId,
|
|
1322
|
+
error,
|
|
1323
|
+
});
|
|
1324
|
+
return undefined;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
/** pending ask 只读视图:从 interactionRecords 派生,不维护第二份状态。 */
|
|
1329
|
+
function pendingAskView(): Map<string, PendingAsk> {
|
|
1330
|
+
const view = new Map<string, PendingAsk>();
|
|
1331
|
+
for (const [requestId, record] of interactionRecords) {
|
|
1332
|
+
if ((record.kind ?? 'permission') !== 'ask' || record.status !== 'pending') continue;
|
|
1333
|
+
view.set(requestId, {
|
|
1334
|
+
run_id: record.run_id,
|
|
1335
|
+
ts: record.created_at,
|
|
1336
|
+
...(record.session_id !== undefined ? { session_id: record.session_id } : {}),
|
|
1337
|
+
...(record.parent_agent_id !== undefined
|
|
1338
|
+
? { parent_agent_id: record.parent_agent_id }
|
|
1339
|
+
: {}),
|
|
1340
|
+
...(record.input !== undefined ? { questions: record.input } : {}),
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
return view;
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
return {
|
|
1347
|
+
pendingPermissions,
|
|
1348
|
+
interactionRecords,
|
|
1349
|
+
get pendingAsks() {
|
|
1350
|
+
return pendingAskView();
|
|
1351
|
+
},
|
|
1352
|
+
handleEvent,
|
|
1353
|
+
respondPermission,
|
|
1354
|
+
respondAsk,
|
|
1355
|
+
ackNotify,
|
|
1356
|
+
markAnswerDelivered,
|
|
1357
|
+
peekPersistedAnswer,
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
/** 导出给 web / 测试使用的状态类型别名。 */
|
|
1362
|
+
export type { StoredInteraction, StoredInteractionStatus };
|