nova-personal-agent-server 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +30 -2
- package/bin/novapersonal-server.mjs +4 -0
- package/package.json +61 -4
- package/resources/endpointing/volcengine-v1/LICENSE.silero-vad.txt +21 -0
- package/resources/endpointing/volcengine-v1/MANIFEST.json +31 -0
- package/resources/endpointing/volcengine-v1/silence-16k-s16le.pcm +0 -0
- package/resources/endpointing/volcengine-v1/speech-16k-s16le.pcm +0 -0
- package/resources/native/codex-sandbox-probe +0 -0
- package/resources/native/project-native/nova_project_native.node +0 -0
- package/resources/native-resources-v1.json +1 -0
- package/runtime/dist/eval/demos.js +156 -0
- package/runtime/dist/eval/fixture-host.js +385 -0
- package/runtime/dist/eval/fixtures.js +319 -0
- package/runtime/dist/eval/realtime/session-fixtures.js +474 -0
- package/runtime/dist/eval/scorecard.js +285 -0
- package/runtime/dist/eval/sim.js +43 -0
- package/runtime/dist/src/cascaded-text-provider.js +31 -0
- package/runtime/dist/src/cli.js +96 -0
- package/runtime/dist/src/composition/assembly.js +352 -0
- package/runtime/dist/src/composition/cascaded-realtime-assembly.js +455 -0
- package/runtime/dist/src/composition/production-composition.js +389 -0
- package/runtime/dist/src/composition/realtime-assembly.js +1330 -0
- package/runtime/dist/src/config/capability-registry.js +315 -0
- package/runtime/dist/src/config/cascaded-realtime-config.js +146 -0
- package/runtime/dist/src/config/coding-backends.js +8 -0
- package/runtime/dist/src/config/coding-profiles.js +98 -0
- package/runtime/dist/src/config/config.js +857 -0
- package/runtime/dist/src/config/diagnostics.js +139 -0
- package/runtime/dist/src/config/environment-contract.js +202 -0
- package/runtime/dist/src/config/self-hosted.js +23 -0
- package/runtime/dist/src/connectors/composio/client.js +195 -0
- package/runtime/dist/src/connectors/composio/google.js +208 -0
- package/runtime/dist/src/connectors/composio/index.js +334 -0
- package/runtime/dist/src/connectors/feishu/cli.js +220 -0
- package/runtime/dist/src/connectors/feishu/index.js +980 -0
- package/runtime/dist/src/connectors/macos/calendar.js +35 -0
- package/runtime/dist/src/connectors/macos/mail.js +32 -0
- package/runtime/dist/src/connectors/macos/reader.js +62 -0
- package/runtime/dist/src/conversation-voice-provider.js +41 -0
- package/runtime/dist/src/core/approval-port.js +9 -0
- package/runtime/dist/src/core/approval.js +1328 -0
- package/runtime/dist/src/core/calls.js +35 -0
- package/runtime/dist/src/core/camera-session.js +32 -0
- package/runtime/dist/src/core/causal-runtime.js +650 -0
- package/runtime/dist/src/core/clock.js +139 -0
- package/runtime/dist/src/core/context-view.js +156 -0
- package/runtime/dist/src/core/effects.js +62 -0
- package/runtime/dist/src/core/events.js +217 -0
- package/runtime/dist/src/core/executor-diagnostic.js +9 -0
- package/runtime/dist/src/core/ids.js +32 -0
- package/runtime/dist/src/core/media-store.js +104 -0
- package/runtime/dist/src/core/memory.js +296 -0
- package/runtime/dist/src/core/pending-decision.js +64 -0
- package/runtime/dist/src/core/ports.js +125 -0
- package/runtime/dist/src/core/runtime.js +1459 -0
- package/runtime/dist/src/core/schedule-tools.js +15 -0
- package/runtime/dist/src/core/sims.js +186 -0
- package/runtime/dist/src/core/slots.js +97 -0
- package/runtime/dist/src/core/source-processing-instructions.js +3 -0
- package/runtime/dist/src/core/source-processing-tools.js +12 -0
- package/runtime/dist/src/core/suggestions.js +143 -0
- package/runtime/dist/src/core/task-tools.js +22 -0
- package/runtime/dist/src/core/tool-schema.js +212 -0
- package/runtime/dist/src/core/trace.js +34 -0
- package/runtime/dist/src/core/work-tools.js +94 -0
- package/runtime/dist/src/desktop/desktop-camera-source.js +31 -0
- package/runtime/dist/src/desktop/desktop-camera.js +281 -0
- package/runtime/dist/src/desktop/desktop-control.js +140 -0
- package/runtime/dist/src/desktop/desktop-progress.js +162 -0
- package/runtime/dist/src/desktop/desktop-session.js +2106 -0
- package/runtime/dist/src/desktop/desktop-tasks.js +80 -0
- package/runtime/dist/src/desktop/desktop-wire.js +605 -0
- package/runtime/dist/src/desktop/phone-desktop-entry.js +17 -0
- package/runtime/dist/src/desktop/startup-error.js +18 -0
- package/runtime/dist/src/desktop/workbench-acceptance.js +262 -0
- package/runtime/dist/src/desktop-entry.js +106 -0
- package/runtime/dist/src/desktop.js +1092 -0
- package/runtime/dist/src/executors/acp/backends.js +105 -0
- package/runtime/dist/src/executors/acp/mcp.js +81 -0
- package/runtime/dist/src/executors/acp/preflight.js +14 -0
- package/runtime/dist/src/executors/acp/process-owner.js +112 -0
- package/runtime/dist/src/executors/acp/transport.js +760 -0
- package/runtime/dist/src/executors/acp/windows-launch.js +68 -0
- package/runtime/dist/src/executors/agent-controller.js +244 -0
- package/runtime/dist/src/executors/autoglm.js +376 -0
- package/runtime/dist/src/executors/browser-device.js +102 -0
- package/runtime/dist/src/executors/browser.js +61 -0
- package/runtime/dist/src/executors/camera.js +101 -0
- package/runtime/dist/src/executors/chromium-frame-source.js +173 -0
- package/runtime/dist/src/executors/codex/adapter-live.js +292 -0
- package/runtime/dist/src/executors/codex/adapter-project.js +1306 -0
- package/runtime/dist/src/executors/codex/app-server-schema.js +712 -0
- package/runtime/dist/src/executors/codex/app-server-transport.js +1798 -0
- package/runtime/dist/src/executors/codex/approval-protocol.js +439 -0
- package/runtime/dist/src/executors/codex/common.js +655 -0
- package/runtime/dist/src/executors/codex/contract.js +581 -0
- package/runtime/dist/src/executors/codex/controller.js +141 -0
- package/runtime/dist/src/executors/codex/credential-snapshot.js +195 -0
- package/runtime/dist/src/executors/codex/factory.js +463 -0
- package/runtime/dist/src/executors/codex/host-config.js +238 -0
- package/runtime/dist/src/executors/codex/host.js +17 -0
- package/runtime/dist/src/executors/codex/index.js +19 -0
- package/runtime/dist/src/executors/codex/launch-profile.js +37 -0
- package/runtime/dist/src/executors/codex/local-sessions.js +119 -0
- package/runtime/dist/src/executors/codex/managed-mcp.js +196 -0
- package/runtime/dist/src/executors/codex/process-owner.js +398 -0
- package/runtime/dist/src/executors/codex/production-host.js +887 -0
- package/runtime/dist/src/executors/codex/progress-instructions.js +10 -0
- package/runtime/dist/src/executors/codex/protocol.js +581 -0
- package/runtime/dist/src/executors/codex/safe-json.js +87 -0
- package/runtime/dist/src/executors/codex/session-title.js +61 -0
- package/runtime/dist/src/executors/codex/shared-home.js +29 -0
- package/runtime/dist/src/executors/codex/spawn-env.js +162 -0
- package/runtime/dist/src/executors/codex/turn-projection.js +435 -0
- package/runtime/dist/src/executors/codex/version.js +38 -0
- package/runtime/dist/src/executors/codex/windows-guardian.js +716 -0
- package/runtime/dist/src/executors/coding/intake-model.js +103 -0
- package/runtime/dist/src/executors/coding/intake.js +824 -0
- package/runtime/dist/src/executors/coding/json-completion.js +14 -0
- package/runtime/dist/src/executors/coding/target-resolution.js +61 -0
- package/runtime/dist/src/executors/coding/work-order.js +50 -0
- package/runtime/dist/src/executors/coding-executor.js +23 -0
- package/runtime/dist/src/executors/computer-backend.js +188 -0
- package/runtime/dist/src/executors/computer-device.js +187 -0
- package/runtime/dist/src/executors/computer-worker.js +125 -0
- package/runtime/dist/src/executors/computer.js +100 -0
- package/runtime/dist/src/executors/device-lock.js +127 -0
- package/runtime/dist/src/executors/frame-source.js +29 -0
- package/runtime/dist/src/executors/gui-evidence.js +30 -0
- package/runtime/dist/src/executors/gui-execution.js +237 -0
- package/runtime/dist/src/executors/gui-runner.js +88 -0
- package/runtime/dist/src/executors/host-executor-capability.js +12 -0
- package/runtime/dist/src/executors/index.js +13 -0
- package/runtime/dist/src/executors/mcp-client.js +198 -0
- package/runtime/dist/src/executors/mcp.js +210 -0
- package/runtime/dist/src/executors/mobile-android.js +180 -0
- package/runtime/dist/src/executors/mobile-ios.js +303 -0
- package/runtime/dist/src/executors/mobile.js +78 -0
- package/runtime/dist/src/executors/search-mcp.js +146 -0
- package/runtime/dist/src/executors/search.js +524 -0
- package/runtime/dist/src/executors/task-resources.js +38 -0
- package/runtime/dist/src/executors/vision/assess.js +296 -0
- package/runtime/dist/src/executors/vision/controller-core.js +373 -0
- package/runtime/dist/src/executors/vision/controller.js +164 -0
- package/runtime/dist/src/executors/vision/lifecycle.js +69 -0
- package/runtime/dist/src/executors/vision.js +5 -0
- package/runtime/dist/src/executors/watcher.js +718 -0
- package/runtime/dist/src/http/bounded-response.js +37 -0
- package/runtime/dist/src/index.js +56 -0
- package/runtime/dist/src/knowledge/assembly.js +60 -0
- package/runtime/dist/src/knowledge/documents.js +520 -0
- package/runtime/dist/src/knowledge/embeddings.js +201 -0
- package/runtime/dist/src/knowledge/mcp.js +218 -0
- package/runtime/dist/src/knowledge/parse-worker.js +122 -0
- package/runtime/dist/src/knowledge/references.js +51 -0
- package/runtime/dist/src/knowledge/service.js +526 -0
- package/runtime/dist/src/knowledge/store-client.js +185 -0
- package/runtime/dist/src/knowledge/store-worker.js +843 -0
- package/runtime/dist/src/knowledge/types.js +2 -0
- package/runtime/dist/src/mcp/http-server.js +177 -0
- package/runtime/dist/src/mem0/resource.js +35 -0
- package/runtime/dist/src/mem0/store-worker.js +330 -0
- package/runtime/dist/src/memory/blackboard-session.js +205 -0
- package/runtime/dist/src/memory/blackboard-store.js +194 -0
- package/runtime/dist/src/memory/blackboard-worker.js +369 -0
- package/runtime/dist/src/memory/entry.js +39 -0
- package/runtime/dist/src/memory/factory.js +20 -0
- package/runtime/dist/src/memory/personal-memory-inspection.js +18 -0
- package/runtime/dist/src/memory/personal-memory.js +10 -0
- package/runtime/dist/src/memory/prerecall.js +20 -0
- package/runtime/dist/src/memory/provenance.js +17 -0
- package/runtime/dist/src/memory/remote-personal-memory.js +194 -0
- package/runtime/dist/src/memory/retrieval.js +235 -0
- package/runtime/dist/src/memory/sensitivity.js +261 -0
- package/runtime/dist/src/memory-ledger/models.js +98 -0
- package/runtime/dist/src/memory-ledger/store-client.js +210 -0
- package/runtime/dist/src/memory-ledger/store-worker.js +130 -0
- package/runtime/dist/src/memory-ledger/store.js +597 -0
- package/runtime/dist/src/memory-substrate/candidates.js +16 -0
- package/runtime/dist/src/memory-substrate/consolidation.js +124 -0
- package/runtime/dist/src/memory-substrate/file-authority.js +111 -0
- package/runtime/dist/src/memory-substrate/life-operations.js +280 -0
- package/runtime/dist/src/memory-substrate/markdown-repository.js +610 -0
- package/runtime/dist/src/memory-substrate/migration.js +87 -0
- package/runtime/dist/src/memory-substrate/profile-source-input.js +9 -0
- package/runtime/dist/src/memory-substrate/purge.js +398 -0
- package/runtime/dist/src/memory-substrate/resolution.js +32 -0
- package/runtime/dist/src/memory-substrate/resource.js +867 -0
- package/runtime/dist/src/memory-substrate/retrieval.js +85 -0
- package/runtime/dist/src/memory-substrate/source-operations.js +306 -0
- package/runtime/dist/src/memory-substrate/source-processing.js +326 -0
- package/runtime/dist/src/memory-substrate/source-state.js +93 -0
- package/runtime/dist/src/memory-substrate/store.js +534 -0
- package/runtime/dist/src/memory-substrate/workspace-projections.js +106 -0
- package/runtime/dist/src/memory-substrate/workspace.js +21 -0
- package/runtime/dist/src/model/jev-client.js +25 -0
- package/runtime/dist/src/model/model-adapters.js +64 -0
- package/runtime/dist/src/model/model-gateway.js +399 -0
- package/runtime/dist/src/model/model-lane.js +34 -0
- package/runtime/dist/src/model/model-purpose.js +5 -0
- package/runtime/dist/src/model/personal-writer.js +361 -0
- package/runtime/dist/src/model/proactivity.js +72 -0
- package/runtime/dist/src/model/prompting.js +279 -0
- package/runtime/dist/src/model/task-verifier.js +217 -0
- package/runtime/dist/src/model/vision-capability.js +10 -0
- package/runtime/dist/src/news/feeds.js +114 -0
- package/runtime/dist/src/news/jev-ranking.js +25 -0
- package/runtime/dist/src/news/ranking.js +26 -0
- package/runtime/dist/src/news/service.js +262 -0
- package/runtime/dist/src/notifications/adapters/feishu.js +10 -0
- package/runtime/dist/src/notifications/adapters/nova.js +15 -0
- package/runtime/dist/src/notifications/content.js +15 -0
- package/runtime/dist/src/notifications/contracts.js +9 -0
- package/runtime/dist/src/notifications/feed.js +8 -0
- package/runtime/dist/src/notifications/service.js +412 -0
- package/runtime/dist/src/notifications/store.js +4 -0
- package/runtime/dist/src/personal-agent/app-context.js +113 -0
- package/runtime/dist/src/personal-agent/approval-scope.js +8 -0
- package/runtime/dist/src/personal-agent/coding-targets.js +82 -0
- package/runtime/dist/src/personal-agent/context-candidates.js +94 -0
- package/runtime/dist/src/personal-agent/contracts.js +32 -0
- package/runtime/dist/src/personal-agent/conversation-runtime.js +536 -0
- package/runtime/dist/src/personal-agent/conversations.js +187 -0
- package/runtime/dist/src/personal-agent/daily-brief.js +53 -0
- package/runtime/dist/src/personal-agent/host.js +2403 -0
- package/runtime/dist/src/personal-agent/intake-task.js +37 -0
- package/runtime/dist/src/personal-agent/life.js +260 -0
- package/runtime/dist/src/personal-agent/memory-overview.js +19 -0
- package/runtime/dist/src/personal-agent/profile-sources.js +34 -0
- package/runtime/dist/src/personal-agent/profile-warmup.js +244 -0
- package/runtime/dist/src/personal-agent/project-digests.js +242 -0
- package/runtime/dist/src/personal-agent/sampling.js +20 -0
- package/runtime/dist/src/personal-agent/schedule-agent.js +60 -0
- package/runtime/dist/src/personal-agent/schedule-api.js +61 -0
- package/runtime/dist/src/personal-agent/schedule-presets.js +13 -0
- package/runtime/dist/src/personal-agent/schedule-reference.js +3 -0
- package/runtime/dist/src/personal-agent/scheduled-response-scope.js +38 -0
- package/runtime/dist/src/personal-agent/scheduled-run-authority.js +44 -0
- package/runtime/dist/src/personal-agent/scheduled-tasks.js +280 -0
- package/runtime/dist/src/personal-agent/scheduled-time.js +99 -0
- package/runtime/dist/src/personal-agent/scheduled-tool-runtime.js +109 -0
- package/runtime/dist/src/personal-agent/source-priority.js +142 -0
- package/runtime/dist/src/personal-agent/source-processing-agent.js +158 -0
- package/runtime/dist/src/personal-agent/source-walk.js +25 -0
- package/runtime/dist/src/personal-agent/sources.js +1484 -0
- package/runtime/dist/src/personal-agent/store-lock-worker.js +17 -0
- package/runtime/dist/src/personal-agent/store.js +146 -0
- package/runtime/dist/src/personal-agent/task-loop.js +163 -0
- package/runtime/dist/src/personal-agent/tasks.js +920 -0
- package/runtime/dist/src/personal-agent/turn-deadline.js +55 -0
- package/runtime/dist/src/personal-agent/understanding.js +160 -0
- package/runtime/dist/src/personal-agent/workbench-context.js +239 -0
- package/runtime/dist/src/projects/confirmed-project-capability.js +60 -0
- package/runtime/dist/src/projects/host-paths.js +133 -0
- package/runtime/dist/src/projects/managed-workspace-maintenance.js +351 -0
- package/runtime/dist/src/projects/project-confirmation.js +486 -0
- package/runtime/dist/src/projects/project-native-resource.js +284 -0
- package/runtime/dist/src/projects/project-node-files.js +169 -0
- package/runtime/dist/src/projects/project-root-file.js +16 -0
- package/runtime/dist/src/projects/project-state.js +504 -0
- package/runtime/dist/src/projects/project-store-files.js +1271 -0
- package/runtime/dist/src/projects/project-store.js +1345 -0
- package/runtime/dist/src/realtime/aoq.js +141 -0
- package/runtime/dist/src/realtime/bridge.js +652 -0
- package/runtime/dist/src/realtime/cascaded/adapter.js +1669 -0
- package/runtime/dist/src/realtime/cascaded/ark-llm.js +219 -0
- package/runtime/dist/src/realtime/cascaded/chat-completions-llm.js +478 -0
- package/runtime/dist/src/realtime/cascaded/gemini-speech.js +232 -0
- package/runtime/dist/src/realtime/cascaded/llm.js +37 -0
- package/runtime/dist/src/realtime/cascaded/ports.js +2 -0
- package/runtime/dist/src/realtime/cascaded/provider.js +216 -0
- package/runtime/dist/src/realtime/cascaded/qwen-llm.js +7 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-asr.js +110 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-tts.js +137 -0
- package/runtime/dist/src/realtime/cascaded/transcribe.js +42 -0
- package/runtime/dist/src/realtime/coding-progress-narration.js +28 -0
- package/runtime/dist/src/realtime/confirmation-turn-isolation.js +586 -0
- package/runtime/dist/src/realtime/dictation.js +28 -0
- package/runtime/dist/src/realtime/evidence.js +276 -0
- package/runtime/dist/src/realtime/floor.js +43 -0
- package/runtime/dist/src/realtime/frontend-instructions.js +195 -0
- package/runtime/dist/src/realtime/gemini.js +517 -0
- package/runtime/dist/src/realtime/history.js +122 -0
- package/runtime/dist/src/realtime/host-delivery.js +1711 -0
- package/runtime/dist/src/realtime/integrated-wire-profile.js +128 -0
- package/runtime/dist/src/realtime/memory-board.js +105 -0
- package/runtime/dist/src/realtime/openai-wire-profile.js +46 -0
- package/runtime/dist/src/realtime/playback.js +242 -0
- package/runtime/dist/src/realtime/project-confirmation-flow.js +1428 -0
- package/runtime/dist/src/realtime/prompt-language.js +17 -0
- package/runtime/dist/src/realtime/protocol.js +428 -0
- package/runtime/dist/src/realtime/provider-projection.js +526 -0
- package/runtime/dist/src/realtime/provider-session.js +491 -0
- package/runtime/dist/src/realtime/qwen-transport.js +144 -0
- package/runtime/dist/src/realtime/qwen.js +1377 -0
- package/runtime/dist/src/realtime/recall.js +341 -0
- package/runtime/dist/src/realtime/service-ports.js +8 -0
- package/runtime/dist/src/realtime/service-state.js +259 -0
- package/runtime/dist/src/realtime/service.js +1726 -0
- package/runtime/dist/src/realtime/session-state.js +684 -0
- package/runtime/dist/src/realtime/session.js +1976 -0
- package/runtime/dist/src/realtime/speech-prep.js +63 -0
- package/runtime/dist/src/realtime/streaming-speech.js +120 -0
- package/runtime/dist/src/realtime/system-prompts.en.js +136 -0
- package/runtime/dist/src/realtime/telemetry.js +144 -0
- package/runtime/dist/src/realtime/tool-continuations.js +1510 -0
- package/runtime/dist/src/realtime/transport.js +8 -0
- package/runtime/dist/src/realtime/usage.js +55 -0
- package/runtime/dist/src/realtime/user-origin-binding.js +173 -0
- package/runtime/dist/src/realtime/volcengine/ark.js +428 -0
- package/runtime/dist/src/realtime/volcengine/asr.js +573 -0
- package/runtime/dist/src/realtime/volcengine/audio.js +39 -0
- package/runtime/dist/src/realtime/volcengine/endpointing-capability.js +834 -0
- package/runtime/dist/src/realtime/volcengine/livekit-endpointing.js +714 -0
- package/runtime/dist/src/realtime/volcengine/local-eot-executor.js +33 -0
- package/runtime/dist/src/realtime/volcengine/protocol.js +247 -0
- package/runtime/dist/src/realtime/volcengine/silence-endpointing.js +237 -0
- package/runtime/dist/src/realtime/volcengine/tts.js +444 -0
- package/runtime/dist/src/realtime/volcengine/websocket.js +293 -0
- package/runtime/dist/src/server/aoq-chat-server.js +497 -0
- package/runtime/dist/src/server/client-pairing.js +192 -0
- package/runtime/dist/src/server/client-protocol.js +87 -0
- package/runtime/dist/src/server/client-server.js +312 -0
- package/runtime/dist/src/server/server-config.js +72 -0
- package/runtime/dist/src/server-entry.js +135 -0
- package/runtime/dist/src/storage/bounded-json.js +63 -0
- package/runtime/dist/src/storage/native-file-lock.js +8 -0
- package/runtime/dist/src/storage/native-resource-snapshot.js +41 -0
- package/runtime/dist/src/storage/private-database.js +120 -0
- package/runtime/dist/src/text/canonical-json.js +60 -0
- package/runtime/dist/src/text/python-number.js +18 -0
- package/runtime/dist/src/text/python-text.js +92 -0
- package/runtime/dist/src/text/unicode-casefold.js +29 -0
- package/runtime/dist/src/text/unicode-normalize.js +138 -0
- package/runtime/dist/src/text/unicode-tables.js +210 -0
- package/runtime/dist/src/understanding/candidates.js +77 -0
- package/runtime/dist/src/understanding/fixture.js +7 -0
- package/runtime/dist/src/understanding/jev.js +20 -0
- package/runtime/dist/src/understanding/pipeline.js +38 -0
- package/runtime/dist/src/voicemem/store-client.js +476 -0
- package/runtime/dist/src/voicemem/store-worker.js +380 -0
- package/runtime/dist/src/voicemem/versioned-memory.js +170 -0
- package/runtime/scripts/pair-device.mjs +109 -0
- package/src/command.mjs +48 -0
|
@@ -0,0 +1,824 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { RealClock, raceDeadline } from '../../core/clock.js';
|
|
3
|
+
import { GatewayError } from '../../model/model-gateway.js';
|
|
4
|
+
import { assessSchema, planSchema } from './intake-model.js';
|
|
5
|
+
import { renderWorkOrder } from './work-order.js';
|
|
6
|
+
import { ProjectResolutionError, } from '../coding-executor.js';
|
|
7
|
+
import { collapsePythonWhitespace, stripLikePython } from '../../text/python-text.js';
|
|
8
|
+
import { deriveSessionTitle } from '../../core/work-tools.js';
|
|
9
|
+
const emptySlots = () => ({
|
|
10
|
+
goal: { state: 'missing', note: '' }, scope: { state: 'missing', note: '' },
|
|
11
|
+
acceptance: { state: 'missing', note: '' }, constraints: { state: 'missing', note: '' },
|
|
12
|
+
});
|
|
13
|
+
const limit = (value, count) => [...value].slice(0, count).join('');
|
|
14
|
+
/** Only fixed classifications enter memory or speech; never provider bodies or error messages. */
|
|
15
|
+
function failureReason(error) {
|
|
16
|
+
if (error instanceof GatewayError) {
|
|
17
|
+
if (['HTTPStatus401', 'HTTPStatus403'].includes(error.classification))
|
|
18
|
+
return 'authentication';
|
|
19
|
+
if (error.classification === 'HTTPStatus429')
|
|
20
|
+
return 'rate_limit';
|
|
21
|
+
if (/^HTTPStatus5\d\d$/u.test(error.classification))
|
|
22
|
+
return 'server_error';
|
|
23
|
+
if (error.classification === 'TimeoutError')
|
|
24
|
+
return 'timeout';
|
|
25
|
+
if (error.classification === 'TransportError')
|
|
26
|
+
return 'transport';
|
|
27
|
+
if (error.classification === 'InvalidResponse')
|
|
28
|
+
return 'invalid_output';
|
|
29
|
+
return 'provider_rejected';
|
|
30
|
+
}
|
|
31
|
+
if (error instanceof SyntaxError || error instanceof z.ZodError
|
|
32
|
+
|| (error instanceof TypeError && error.message === 'intake_output_too_large'))
|
|
33
|
+
return 'invalid_output';
|
|
34
|
+
if (error instanceof Error && error.name === 'TimeoutError')
|
|
35
|
+
return 'timeout';
|
|
36
|
+
return 'internal_error';
|
|
37
|
+
}
|
|
38
|
+
/** Schema paths, issue codes and error classes only: never model text, values or messages. */
|
|
39
|
+
function failureDetail(error) {
|
|
40
|
+
const safe = (value) => value.replace(/[^A-Za-z0-9_.:>|-]/gu, '_').slice(0, 300);
|
|
41
|
+
if (error instanceof z.ZodError) {
|
|
42
|
+
return safe(error.issues.slice(0, 6).map(issue => `${issue.path.join('.') || '$'}:${issue.code}`
|
|
43
|
+
+ (issue.code === 'invalid_type' ? `>${String(issue.expected)}` : '')).join('|'));
|
|
44
|
+
}
|
|
45
|
+
if (error instanceof SyntaxError)
|
|
46
|
+
return 'json_parse';
|
|
47
|
+
if (error instanceof TypeError && error.message === 'intake_output_too_large')
|
|
48
|
+
return 'output_too_large';
|
|
49
|
+
if (error instanceof GatewayError)
|
|
50
|
+
return ['authentication', 'rate_limit', 'timeout', 'transport'].includes(failureReason(error)) ? null : safe(error.classification);
|
|
51
|
+
if (error instanceof Error && failureReason(error) === 'internal_error')
|
|
52
|
+
return safe(`error:${error.name}`);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The quoted span must occur in an utterance *and* overlap (one contains the other) exactly one roster
|
|
57
|
+
* name, which must be the selected one: a filler like "改" vouches for nothing, and a shared prefix like
|
|
58
|
+
* "pricing" for `pricing-page` / `pricing-svc` vouches for neither. Every utterance counts: binding to
|
|
59
|
+
* the latest turn alone would loop on multi-turn clarifications. Whitespace- and case-insensitive.
|
|
60
|
+
*/
|
|
61
|
+
function evidenceOccurs(evidence, project, utterances, roster) {
|
|
62
|
+
const normalize = (value) => collapsePythonWhitespace(stripLikePython(value)).toLowerCase();
|
|
63
|
+
const span = normalize(evidence);
|
|
64
|
+
if (span === '' || !utterances.some(text => normalize(text).includes(span)))
|
|
65
|
+
return false;
|
|
66
|
+
const named = roster.map(normalize).filter(name => span.includes(name) || name.includes(span));
|
|
67
|
+
return named.length === 1 && named[0] === normalize(project);
|
|
68
|
+
}
|
|
69
|
+
// ponytail: recognize explicit directive clauses only; uncertain paraphrases retain confirmation.
|
|
70
|
+
const directiveText = (text) => text.replace(/"[^"]*"|“[^”]*”|‘[^’]*’|「[^」]*」|`[^`]*`|(?<![\p{L}\p{N}])'[^']*'(?![\p{L}\p{N}])/gu, '');
|
|
71
|
+
const directStartRequested = (text) => {
|
|
72
|
+
const plain = directiveText(text);
|
|
73
|
+
if (/[??]|先(?:问|确认)|不允许|不准|禁止|\b(?:before|forbidden|not allowed)\b/iu.test(plain))
|
|
74
|
+
return false;
|
|
75
|
+
return plain.split(/[,,。.!!;;\n]/u).some(clause => /^(?:请|现在|这次|那就)?(?:(?:直接开始|直接做)(?:(?:吧|即可|就行)|(?:修复|修改|实现|处理|执行).*)?|不用再确认|无需再确认|go ahead|start directly|proceed without confirmation)$/iu.test(clause.trim()));
|
|
76
|
+
};
|
|
77
|
+
// A later session directive replaces earlier authority; ordinary factual answers preserve it.
|
|
78
|
+
const latestSessionDirective = (texts) => {
|
|
79
|
+
let latest = '';
|
|
80
|
+
for (const text of texts.map(directiveText)) {
|
|
81
|
+
if (/继续|接着|会话|\b(?:continue|resume|session|thread)\b/iu.test(text)
|
|
82
|
+
|| (latest && /先(?:问|确认)|不确定/iu.test(text)))
|
|
83
|
+
latest = text;
|
|
84
|
+
}
|
|
85
|
+
return latest;
|
|
86
|
+
};
|
|
87
|
+
const continuationIntent = (texts) => {
|
|
88
|
+
const plain = latestSessionDirective(texts);
|
|
89
|
+
if (/(?:不要|别|不再|不是|不)(?:再)?继续|(?:新开|重新开|另开).{0,6}会话|新会话|\b(?:new|fresh) (?:session|thread)\b|\b(?:do not|don't) (?:continue|resume)\b/iu.test(plain))
|
|
90
|
+
return 'new';
|
|
91
|
+
if (/继续|接着|先(?:问|确认)|不确定|\b(?:continue|resume)\b/iu.test(plain)) {
|
|
92
|
+
return !/[??]|之前|先(?:问|确认)|不允许|不确定|是否/iu.test(plain)
|
|
93
|
+
&& /(?:^|[,,。.!!;;\n])\s*(?:请|现在|那就)?(?:在(?:当前项目|当前工作区|本项目|这个项目)(?:里|中)?\s*)?(?:(?:继续|接着)(?=[^,,。.!!;;\n]*(?:任务|会话|工作区|项目|session\b|thread\b))|(?:continue|resume)\b)/iu.test(plain) ? 'latest' : 'unclear';
|
|
94
|
+
}
|
|
95
|
+
return 'none';
|
|
96
|
+
};
|
|
97
|
+
const currentWorkspaceReference = (project) => project !== null
|
|
98
|
+
&& /^(?:当前项目|当前工作区|本项目|这个项目|current project|current workspace)$/iu.test(stripLikePython(project)) ? null : project;
|
|
99
|
+
/** Missing/misbound model fields are service failures, never additional user questions. */
|
|
100
|
+
function projectConfirmationProblem(result, snapshot, active, roster) {
|
|
101
|
+
const turns = snapshot.turns;
|
|
102
|
+
if (result.abandon)
|
|
103
|
+
return null;
|
|
104
|
+
const confirmation = result.project_confirmation;
|
|
105
|
+
const latestProjectTurn = turns.findLastIndex(turn => turn.project_question !== undefined);
|
|
106
|
+
if (latestProjectTurn === turns.length - 1 && latestProjectTurn >= 0 && confirmation === null) {
|
|
107
|
+
return 'Interpret the latest project_question answer and return project_confirmation with its turn_index, decision and exact answer evidence.';
|
|
108
|
+
}
|
|
109
|
+
if (confirmation === null) {
|
|
110
|
+
if (!['unclear', 'create'].includes(result.kind) && result.project_evidence === null
|
|
111
|
+
&& snapshot.confirmed_project?.project !== (result.project ?? active)
|
|
112
|
+
&& turns.some(turn => turn.project_question === (result.project ?? active))) {
|
|
113
|
+
return 'Preserve a still-valid project confirmation by referencing its question turn and exact answer; otherwise resolve the changed target.';
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const turn = turns[confirmation.turn_index];
|
|
118
|
+
if (!turn?.project_question || turn.project_confirmation_superseded || confirmation.turn_index !== latestProjectTurn || !turn.answer.includes(confirmation.evidence)) {
|
|
119
|
+
return 'project_confirmation must quote the answer to the most recent host-owned project_question; never invent or reuse superseded evidence.';
|
|
120
|
+
}
|
|
121
|
+
const selected = result.project ?? active;
|
|
122
|
+
if (confirmation.decision === 'confirmed') {
|
|
123
|
+
if (['create', 'unclear'].includes(result.kind) || selected !== turn.project_question)
|
|
124
|
+
return 'A confirmed project_question must select that same existing project.';
|
|
125
|
+
}
|
|
126
|
+
else if (confirmation.decision === 'redirected') {
|
|
127
|
+
if (result.kind === 'unclear' || result.project === null || selected === turn.project_question) {
|
|
128
|
+
return 'A redirected answer must explicitly select a different target; otherwise return rejected/unclear with kind unclear.';
|
|
129
|
+
}
|
|
130
|
+
if (result.kind !== 'create' && !evidenceOccurs(result.project_evidence ?? '', result.project, [turn.answer], roster)) {
|
|
131
|
+
return 'Redirecting to an existing project requires project_evidence naming that project in the answer to this question.';
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else if (result.kind !== 'unclear') {
|
|
135
|
+
return 'Rejected or unclear project answers require kind unclear. Do not create a workspace or fall back to the active project. Use redirected only for an explicit replacement target.';
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
/** Spoken rendering of a resolution error / cancel result: code first, then what the model needs to offer. */
|
|
140
|
+
export function renderResolutionError(error) {
|
|
141
|
+
const detail = error.detail;
|
|
142
|
+
const text = (value) => typeof value === 'string' ? value : '';
|
|
143
|
+
// A list of names, or of `{project, title}` records, whichever the adapter had at hand.
|
|
144
|
+
const list = (value) => Array.isArray(value)
|
|
145
|
+
? value.map(item => typeof item === 'object' && item !== null && !Array.isArray(item)
|
|
146
|
+
? `${text(item.project)}/${text(item.title)}`
|
|
147
|
+
: text(item)).join('、')
|
|
148
|
+
: '';
|
|
149
|
+
if (error.code === 'unknown_session' && detail.reason === 'continuation_target_required')
|
|
150
|
+
return 'code=unknown_session:请在“Codex 会话(续接目标)”里选择要继续的会话,或明确说出会话名称。任务尚未执行。';
|
|
151
|
+
if (error.code === 'unknown_project' && detail.reason === 'explicit_project_required')
|
|
152
|
+
return 'code=unknown_project:这个对话还没有选择执行工作区,请在“执行工作区”里选择已注册目录,或明确说出项目名。任务尚未执行。';
|
|
153
|
+
if (error.code === 'unknown_session')
|
|
154
|
+
return `code=unknown_session:没有找到“${text(detail.title)}”这个可继续的会话,请明确项目和会话名称。任务尚未执行。`;
|
|
155
|
+
if (error.code === 'unknown_project') {
|
|
156
|
+
const suggestions = list(detail.suggestions);
|
|
157
|
+
return `code=unknown_project:没有叫“${text(detail.project)}”的项目${suggestions === '' ? '' : `,相近的有:${suggestions}`}。可以请用户确认项目名,或明确要求新建。任务尚未执行。`;
|
|
158
|
+
}
|
|
159
|
+
if (error.code === 'ambiguous_project') {
|
|
160
|
+
return `code=ambiguous_project:“${text(detail.project)}”匹配多个项目:${list(detail.candidates)}。请用户说明是哪一个。任务尚未执行。`;
|
|
161
|
+
}
|
|
162
|
+
if (error.code === 'busy_project') {
|
|
163
|
+
return `code=busy_project:项目“${text(detail.project)}”正在执行“${text(detail.title)}”。可以追加要求(steer),或先取消再重新开始(cancel)。新任务尚未执行。`;
|
|
164
|
+
}
|
|
165
|
+
return `code=capacity:同时运行的任务已达上限,正在跑:${list(detail.running)}。可以先取消一个。新任务尚未执行。`;
|
|
166
|
+
}
|
|
167
|
+
/** Two controller-owned single-flight slots; latest revision replaces pending work, never active work. */
|
|
168
|
+
export class IntakeController {
|
|
169
|
+
#taskContext;
|
|
170
|
+
#taskAuthority;
|
|
171
|
+
#options;
|
|
172
|
+
#clock;
|
|
173
|
+
#session = null;
|
|
174
|
+
#assessing = null;
|
|
175
|
+
#planning = null;
|
|
176
|
+
#assessPending = false;
|
|
177
|
+
#planPending = false;
|
|
178
|
+
#userInputPending = false;
|
|
179
|
+
#failedUserInput = false;
|
|
180
|
+
#workspaceId = undefined;
|
|
181
|
+
#abort = new Set();
|
|
182
|
+
#modelResults = new Map();
|
|
183
|
+
#modelBudgets = new Map();
|
|
184
|
+
constructor(options) { this.#options = options; this.#clock = options.clock ?? new RealClock(); }
|
|
185
|
+
get view() { return this.#session === null ? null : structuredClone(this.#session); }
|
|
186
|
+
get active() { return this.#session !== null && this.#session.state !== 'closed'; }
|
|
187
|
+
get preparing() {
|
|
188
|
+
return this.active && !['failed', 'dispatch_unknown'].includes(this.#session.state)
|
|
189
|
+
&& (this.#assessPending || this.#planPending || this.#assessing !== null
|
|
190
|
+
|| this.#planning !== null || this.#session?.state === 'committing');
|
|
191
|
+
}
|
|
192
|
+
userInputStarted() { if (this.active)
|
|
193
|
+
this.#userInputPending = true; }
|
|
194
|
+
userInputEnded() {
|
|
195
|
+
// A transcript is context, not an amendment decision. Hold old async work until dispatch.
|
|
196
|
+
if (!this.active || this.#session?.proposal_id !== null)
|
|
197
|
+
this.#userInputPending = false;
|
|
198
|
+
}
|
|
199
|
+
userInputFailed() {
|
|
200
|
+
if (!this.active || ['committing', 'dispatch_unknown'].includes(this.#session.state))
|
|
201
|
+
return;
|
|
202
|
+
this.userInputEnded();
|
|
203
|
+
// A missing amendment must not be bypassed by an unrelated conversation turn.
|
|
204
|
+
if (this.#session?.proposal_id === null && !this.#failedUserInput) {
|
|
205
|
+
this.#userInputPending = true;
|
|
206
|
+
this.#failedUserInput = true;
|
|
207
|
+
this.#options.fact(this.#session, '刚才没听清,原需求已保留,任务尚未执行。请再说一次。');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
userResponseCompleted() {
|
|
211
|
+
if (this.#failedUserInput || !this.#userInputPending || !this.active || this.#session?.proposal_id !== null)
|
|
212
|
+
return;
|
|
213
|
+
this.#userInputPending = false;
|
|
214
|
+
if (['failed', 'dispatch_unknown', 'committing'].includes(this.#session.state))
|
|
215
|
+
return;
|
|
216
|
+
if (this.#assessing === null && this.#planning === null) {
|
|
217
|
+
if ((this.#session.plan_revision === this.#session.revision && this.#session.work_order !== null) || this.#session.state === 'ready_to_plan')
|
|
218
|
+
this.#planPending = true;
|
|
219
|
+
else if (this.#session.pending_question === null)
|
|
220
|
+
this.#assessPending = true;
|
|
221
|
+
}
|
|
222
|
+
this.#pump();
|
|
223
|
+
}
|
|
224
|
+
workspaceChanged(workspaceId) {
|
|
225
|
+
if (this.#workspaceId !== undefined && this.#workspaceId !== workspaceId
|
|
226
|
+
&& this.#session?.state !== 'committing')
|
|
227
|
+
this.cancel();
|
|
228
|
+
this.#workspaceId = workspaceId;
|
|
229
|
+
}
|
|
230
|
+
factEligible(eventId, sessionEpoch) {
|
|
231
|
+
const intake = this.#session;
|
|
232
|
+
return intake?.session_id === String(sessionEpoch)
|
|
233
|
+
&& eventId.startsWith(`intake:${intake.intake_id}:${intake.revision}:`)
|
|
234
|
+
&& intake.outcome !== 'cancelled'
|
|
235
|
+
&& (!eventId.endsWith(':accepted') || (this.preparing && intake.plan_revision === null && intake.state !== 'committing' && !this.#userInputPending));
|
|
236
|
+
}
|
|
237
|
+
open(request, text, originRef, sessionId, taskContext, inputOriginRef = originRef) {
|
|
238
|
+
const previous = this.#session?.task_fence;
|
|
239
|
+
if (this.active && taskContext && (previous?.task_id !== taskContext.fence.task_id || previous.goal_revision !== taskContext.fence.goal_revision || previous.control_revision !== taskContext.fence.control_revision))
|
|
240
|
+
this.cancel();
|
|
241
|
+
this.#taskContext = taskContext;
|
|
242
|
+
this.#taskAuthority = taskContext?.stillWanted;
|
|
243
|
+
this.#userInputPending = false;
|
|
244
|
+
this.#failedUserInput = false;
|
|
245
|
+
if (this.active && this.#session.session_id !== sessionId)
|
|
246
|
+
this.cancel();
|
|
247
|
+
if (this.active && (this.#session.state === 'committing' || this.#session.state === 'dispatch_unknown'))
|
|
248
|
+
return 'intake_in_progress';
|
|
249
|
+
if (this.active && !this.#bindingCurrent(this.#session))
|
|
250
|
+
this.cancel();
|
|
251
|
+
if (this.active) {
|
|
252
|
+
const current = this.#session;
|
|
253
|
+
// A provider repeats the draft for an already-ingested answer: one content revision per turn.
|
|
254
|
+
if ((current.input_origin_ref ?? current.origin_ref) !== inputOriginRef)
|
|
255
|
+
this.#revise(text, inputOriginRef, sessionId);
|
|
256
|
+
return 'intake_in_progress';
|
|
257
|
+
}
|
|
258
|
+
this.#session = {
|
|
259
|
+
...(taskContext ? { task_fence: { ...taskContext.fence } } : {}),
|
|
260
|
+
intake_id: this.#options.idFactory(), revision: 1, plan_revision: null, proposal_id: null,
|
|
261
|
+
workspace: null, session_id: sessionId,
|
|
262
|
+
...(this.#options.boundTarget ? { bound_target: { ...this.#options.boundTarget() } } : {}),
|
|
263
|
+
origin_ref: originRef, input_origin_ref: inputOriginRef, state: 'open', outcome: null, delegate_id: null,
|
|
264
|
+
request: structuredClone(request), opening: limit(text, 4000), turns: [], slots: emptySlots(), discovery: [],
|
|
265
|
+
questions_asked: 0, intent_to_proceed: false, stop_asking: false, pending_question: null, pending_project_question: null, confirmed_project: null,
|
|
266
|
+
missing_goal_grace: null, kind: null, decision: null, target: null, work_order: null, title: null,
|
|
267
|
+
};
|
|
268
|
+
this.#assessPending = true;
|
|
269
|
+
this.#pump();
|
|
270
|
+
this.#options.fact(this.#session, '收到,我来处理。', 'accepted');
|
|
271
|
+
return 'intake_opened';
|
|
272
|
+
}
|
|
273
|
+
#revise(text, originRef, sessionId) {
|
|
274
|
+
const current = this.#session;
|
|
275
|
+
if (current === null || !this.active)
|
|
276
|
+
return;
|
|
277
|
+
if (current.session_id !== sessionId) {
|
|
278
|
+
this.cancel();
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if ((current.input_origin_ref ?? current.origin_ref) === originRef)
|
|
282
|
+
return;
|
|
283
|
+
if (stripLikePython(text) === '') {
|
|
284
|
+
this.cancel();
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
current.turns.push({ question: current.pending_question, answer: limit(text, 2000),
|
|
288
|
+
...(current.pending_project_question === null ? {} : { project_question: current.pending_project_question }) });
|
|
289
|
+
if (current.turns.length > 8) {
|
|
290
|
+
this.#close('abandoned');
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
this.#modelResults.clear();
|
|
294
|
+
this.#modelBudgets.clear();
|
|
295
|
+
current.revision += 1;
|
|
296
|
+
for (const abort of this.#abort)
|
|
297
|
+
abort.abort();
|
|
298
|
+
current.input_origin_ref = originRef;
|
|
299
|
+
if (!current.task_fence)
|
|
300
|
+
current.origin_ref = originRef;
|
|
301
|
+
current.plan_revision = null;
|
|
302
|
+
current.work_order = null;
|
|
303
|
+
current.title = null;
|
|
304
|
+
if (current.proposal_id !== null)
|
|
305
|
+
this.#options.invalidateProposal();
|
|
306
|
+
current.proposal_id = null;
|
|
307
|
+
current.pending_question = null;
|
|
308
|
+
current.pending_project_question = null;
|
|
309
|
+
current.stop_asking = current.questions_asked >= this.#budget();
|
|
310
|
+
current.state = 'clarifying';
|
|
311
|
+
this.#planPending = false;
|
|
312
|
+
this.#assessPending = true;
|
|
313
|
+
this.#pump();
|
|
314
|
+
}
|
|
315
|
+
cancel() { if (this.active)
|
|
316
|
+
this.#close('cancelled'); }
|
|
317
|
+
beginConfirmed(operation) {
|
|
318
|
+
const current = this.#session;
|
|
319
|
+
if (this.#taskAuthority?.() === false || this.#userInputPending || current?.state !== 'readback' || current.proposal_id !== operation.proposal_id
|
|
320
|
+
|| current.plan_revision !== current.revision || operation.intake_id !== current.intake_id
|
|
321
|
+
|| operation.plan_revision !== current.plan_revision || operation.work_order !== current.work_order)
|
|
322
|
+
return false;
|
|
323
|
+
if (!this.#bindingCurrent(current)) {
|
|
324
|
+
this.cancel();
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
current.state = 'committing';
|
|
328
|
+
this.#options.onStateChanged?.();
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
settleConfirmed(result) {
|
|
332
|
+
if (this.#session?.state !== 'committing')
|
|
333
|
+
return;
|
|
334
|
+
if (result.code === 'callback_failed')
|
|
335
|
+
this.#failure(this.#session, 'dispatch', new Error('commit receipt unavailable'));
|
|
336
|
+
else
|
|
337
|
+
this.#settle(result);
|
|
338
|
+
}
|
|
339
|
+
decline(proposalId) {
|
|
340
|
+
if (this.#session?.proposal_id === proposalId && this.#session.state === 'readback')
|
|
341
|
+
this.cancel();
|
|
342
|
+
}
|
|
343
|
+
/** Deterministic test drain; event handlers never await model completion. */
|
|
344
|
+
async settled() {
|
|
345
|
+
while (this.#assessing !== null || this.#planning !== null) {
|
|
346
|
+
await Promise.all([this.#assessing, this.#planning]);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
#live(id, revision) {
|
|
350
|
+
const current = this.#session;
|
|
351
|
+
return (this.#taskAuthority?.() ?? true) && current?.intake_id === id && current.revision === revision && current.state !== 'closed' ? current : null;
|
|
352
|
+
}
|
|
353
|
+
#current(id, revision) {
|
|
354
|
+
const current = this.#live(id, revision);
|
|
355
|
+
if (current && current.state !== 'committing' && !this.#bindingCurrent(current)) {
|
|
356
|
+
this.cancel();
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
if (current === null)
|
|
360
|
+
this.#options.diagnostic('intake_stale_result');
|
|
361
|
+
return current;
|
|
362
|
+
}
|
|
363
|
+
#bindingCurrent(current) {
|
|
364
|
+
return current.bound_target === undefined || current.bound_target.revision === this.#options.boundTarget?.().revision;
|
|
365
|
+
}
|
|
366
|
+
#userEvidence(current) {
|
|
367
|
+
const quotes = current.request.source_quotes;
|
|
368
|
+
return [...(Array.isArray(quotes) ? quotes.filter((quote) => typeof quote === 'string') : []),
|
|
369
|
+
current.opening, ...current.turns.map(turn => turn.answer)];
|
|
370
|
+
}
|
|
371
|
+
#input(current) {
|
|
372
|
+
return {
|
|
373
|
+
intake_id: current.intake_id, revision: current.revision, opening: current.opening,
|
|
374
|
+
instruction: current.request.work_order ?? current.request.instruction ?? null,
|
|
375
|
+
conversation_context: current.request.conversation_context ?? [],
|
|
376
|
+
source_quotes: current.request.source_quotes ?? [],
|
|
377
|
+
turns: structuredClone(current.turns), confirmed_project: structuredClone(current.confirmed_project), slots: structuredClone(current.slots),
|
|
378
|
+
discovery: [...current.discovery], intent_to_proceed: current.intent_to_proceed,
|
|
379
|
+
questions_asked: current.questions_asked, question_budget: this.#budget(),
|
|
380
|
+
roster: this.#options.roster(), active_project: this.#options.activeProject(),
|
|
381
|
+
running: this.#options.running(),
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
#pump() {
|
|
385
|
+
this.#options.onStateChanged?.();
|
|
386
|
+
if (!this.active || this.#userInputPending || ['failed', 'dispatch_unknown'].includes(this.#session.state))
|
|
387
|
+
return;
|
|
388
|
+
if (this.#assessPending && this.#assessing === null) {
|
|
389
|
+
this.#assessPending = false;
|
|
390
|
+
const snapshot = structuredClone(this.#session);
|
|
391
|
+
this.#assessing = this.#assess(snapshot).finally(() => { this.#assessing = null; this.#pump(); });
|
|
392
|
+
}
|
|
393
|
+
if (this.#planPending && this.#planning === null && !this.#assessPending && this.#assessing === null) {
|
|
394
|
+
this.#planPending = false;
|
|
395
|
+
const snapshot = structuredClone(this.#session);
|
|
396
|
+
this.#session.state = 'planning';
|
|
397
|
+
this.#planning = this.#plan(snapshot).finally(() => { this.#planning = null; this.#pump(); });
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
async #assess(snapshot) {
|
|
401
|
+
const abort = new AbortController();
|
|
402
|
+
this.#abort.add(abort);
|
|
403
|
+
let stage = 'assess';
|
|
404
|
+
try {
|
|
405
|
+
const input = this.#input(snapshot);
|
|
406
|
+
const result = await this.#model(snapshot, 'assess', assessSchema.transform(result => ({ ...result, project: currentWorkspaceReference(result.project) })).superRefine((result, ctx) => {
|
|
407
|
+
// A stale binding cannot answer this revision's project question; discard it below.
|
|
408
|
+
if (result.intake_id !== snapshot.intake_id || result.revision !== snapshot.revision)
|
|
409
|
+
return;
|
|
410
|
+
const problem = projectConfirmationProblem(result, snapshot, input.active_project, input.roster.map(entry => entry.name));
|
|
411
|
+
if (problem !== null)
|
|
412
|
+
ctx.addIssue({ code: 'custom', message: problem, path: ['project_confirmation'] });
|
|
413
|
+
}), input, abort);
|
|
414
|
+
if (result === null)
|
|
415
|
+
return;
|
|
416
|
+
let current = this.#current(snapshot.intake_id, snapshot.revision);
|
|
417
|
+
if (current === null)
|
|
418
|
+
return;
|
|
419
|
+
let sessionTitle = result.session.mode === 'named' ? result.session.title : null;
|
|
420
|
+
if (result.intake_id !== snapshot.intake_id || result.revision !== snapshot.revision) {
|
|
421
|
+
this.#options.diagnostic('intake_stale_result');
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
if (result.abandon) {
|
|
425
|
+
this.cancel();
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
this.#options.record(current, 'intake.assess', { ...result, input_active_project: input.active_project });
|
|
429
|
+
current.slots = result.slots;
|
|
430
|
+
current.execution_mode = result.execution_mode;
|
|
431
|
+
current.direct_start_revision = result.early_exit && directStartRequested(current.turns.at(-1)?.answer ?? current.opening) ? current.revision : undefined;
|
|
432
|
+
current.intent_to_proceed = result.intent_to_proceed || result.early_exit;
|
|
433
|
+
current.discovery = [...new Set([...current.discovery, ...result.discovery,
|
|
434
|
+
...(result.candidate_question?.owner === 'repo' ? [result.candidate_question.text] : [])])].slice(0, 12);
|
|
435
|
+
let kind = result.kind;
|
|
436
|
+
let question = result.candidate_question?.owner === 'user' ? result.candidate_question.text : null;
|
|
437
|
+
const active = this.#options.activeProject();
|
|
438
|
+
const project = result.project ?? (kind === 'create' ? null : active);
|
|
439
|
+
// The model interprets the answer; the host validates the exact question/answer provenance.
|
|
440
|
+
const confirmation = result.project_confirmation;
|
|
441
|
+
if (confirmation !== null && confirmation.decision !== 'confirmed') {
|
|
442
|
+
current.turns[confirmation.turn_index].project_confirmation_superseded = true;
|
|
443
|
+
}
|
|
444
|
+
if (kind === 'unclear' && confirmation === null) {
|
|
445
|
+
for (const turn of current.turns)
|
|
446
|
+
if (turn.project_question !== undefined)
|
|
447
|
+
turn.project_confirmation_superseded = true;
|
|
448
|
+
}
|
|
449
|
+
const prior = current.confirmed_project;
|
|
450
|
+
if (prior !== null && (kind === 'unclear' || kind === 'create' || project !== prior.project
|
|
451
|
+
|| (confirmation !== null && confirmation.decision !== 'confirmed'))) {
|
|
452
|
+
current.turns[prior.turn_index].project_confirmation_superseded = true;
|
|
453
|
+
current.confirmed_project = null;
|
|
454
|
+
}
|
|
455
|
+
if (confirmation?.decision === 'confirmed' && !['unclear', 'create'].includes(kind)) {
|
|
456
|
+
current.confirmed_project = { project: project, turn_index: confirmation.turn_index, evidence: confirmation.evidence };
|
|
457
|
+
}
|
|
458
|
+
const affirmed = current.confirmed_project?.project === project;
|
|
459
|
+
let projectQuestion;
|
|
460
|
+
if (kind === 'unclear' && confirmation?.decision === 'unclear'
|
|
461
|
+
&& project === current.turns[confirmation.turn_index]?.project_question) {
|
|
462
|
+
question = `是在 ${project} 里做吗?`;
|
|
463
|
+
projectQuestion = project ?? undefined;
|
|
464
|
+
}
|
|
465
|
+
const namedSessionEvidence = project !== null && this.#namedSessionEvidence(project, sessionTitle, current);
|
|
466
|
+
if (kind !== 'create' && kind !== 'unclear' && project !== null && project !== active && !affirmed
|
|
467
|
+
&& !evidenceOccurs(result.project_evidence ?? '', project, this.#userEvidence(current), this.#options.roster().map(entry => entry.name))
|
|
468
|
+
&& !namedSessionEvidence) {
|
|
469
|
+
this.#options.diagnostic('intake_project_evidence_missing');
|
|
470
|
+
kind = 'unclear';
|
|
471
|
+
question = `是在 ${project} 里做吗?`;
|
|
472
|
+
projectQuestion = project;
|
|
473
|
+
}
|
|
474
|
+
const continuation = continuationIntent([current.opening, ...current.turns.map(turn => turn.answer)]);
|
|
475
|
+
// An explicit "continue the original session" may come back named with that project's latest title: same session.
|
|
476
|
+
// Literal title text in the user's words stays a named claim that needs evidence.
|
|
477
|
+
const latestNamed = continuation === 'latest' && sessionTitle !== null && !namedSessionEvidence
|
|
478
|
+
&& this.#options.roster().find(entry => entry.name === project)?.last_session_title === sessionTitle
|
|
479
|
+
&& ![current.opening, ...current.turns.map(turn => turn.answer)].some(text => text.includes(sessionTitle));
|
|
480
|
+
if (latestNamed)
|
|
481
|
+
sessionTitle = null;
|
|
482
|
+
const sessionMode = latestNamed ? 'latest' : result.session.mode;
|
|
483
|
+
if ((kind === 'work' && ((continuation === 'unclear' && sessionMode !== 'new' && !namedSessionEvidence) || (continuation === 'latest' && sessionMode === 'new')))
|
|
484
|
+
|| (sessionTitle && project !== null && (continuation === 'new' || !namedSessionEvidence))) {
|
|
485
|
+
kind = 'unclear';
|
|
486
|
+
question = '请明确要继续的项目和会话名称。';
|
|
487
|
+
projectQuestion = undefined;
|
|
488
|
+
}
|
|
489
|
+
current.kind = kind;
|
|
490
|
+
if (kind === 'unclear' || (kind === 'create' && project === null)) {
|
|
491
|
+
this.#ask(current, kind === 'create' ? '新项目叫什么名字?' : question ?? '请说明要在哪个项目里做什么。', projectQuestion);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
// Keep the user's request and later corrections together. A project affirmation is target
|
|
495
|
+
// evidence, not a replacement instruction. The executor still enforces its input bound.
|
|
496
|
+
const userText = [...(Array.isArray(current.request.source_quotes) ? current.request.source_quotes.filter((quote) => typeof quote === 'string') : []), current.opening, ...current.turns.map(turn => turn.question === null
|
|
497
|
+
? `用户补充:${turn.answer}`
|
|
498
|
+
: `宿主追问:${turn.question}\n用户补充:${turn.answer}`)].join('\n');
|
|
499
|
+
// Local onset precedes final ASR and does not advance the intake revision yet.
|
|
500
|
+
if (kind === 'steer' && this.#userInputPending)
|
|
501
|
+
return;
|
|
502
|
+
if (kind === 'steer') {
|
|
503
|
+
if (!this.#options.running().some(work => work.project === project)) {
|
|
504
|
+
this.#route(current, 'code=no_active_turn:目标工作区没有正在执行的任务,本次追加要求未执行。');
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
const wanted = this.#launchWanted(current);
|
|
508
|
+
stage = 'steer';
|
|
509
|
+
current.state = 'committing';
|
|
510
|
+
const admission = await this.#options.steer(current, project, userText, wanted);
|
|
511
|
+
if (this.#current(snapshot.intake_id, snapshot.revision) !== current)
|
|
512
|
+
return;
|
|
513
|
+
this.#options.record(current, 'intake.steer', { accepted: admission.accepted, delegate_id: admission.delegate_id ?? null });
|
|
514
|
+
this.#route(current, admission.accepted
|
|
515
|
+
? 'code=steered:已把追加要求交给正在执行的任务,等待宿主进度。'
|
|
516
|
+
: `code=steer_failed:追加要求未送达:${admission.problem ?? admission.code ?? 'runtime_rejected'}。`);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const decision = { kind: kind === 'switch' ? 'switch' : kind === 'create' ? 'create' : 'work', project, session: sessionTitle || (sessionMode === 'latest' && continuation === 'latest') ? 'latest' : 'new', ...(sessionTitle ? { session_title: sessionTitle } : {}) };
|
|
520
|
+
let target;
|
|
521
|
+
stage = 'resolve';
|
|
522
|
+
try {
|
|
523
|
+
target = await raceDeadline(this.#options.resolveTarget(decision, this.#taskContext), this.#clock, 30, abort.signal, () => new DOMException('resolution deadline', 'TimeoutError'));
|
|
524
|
+
}
|
|
525
|
+
catch (error) {
|
|
526
|
+
current = this.#current(snapshot.intake_id, snapshot.revision);
|
|
527
|
+
if (current === null)
|
|
528
|
+
return;
|
|
529
|
+
if (!(error instanceof ProjectResolutionError))
|
|
530
|
+
throw error;
|
|
531
|
+
this.#options.record(current, 'intake.resolution_error', { code: error.code, ...error.detail });
|
|
532
|
+
this.#route(current, renderResolutionError(error));
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
current = this.#current(snapshot.intake_id, snapshot.revision);
|
|
536
|
+
if (current === null)
|
|
537
|
+
return;
|
|
538
|
+
current.decision = decision;
|
|
539
|
+
current.target = target;
|
|
540
|
+
current.workspace = target.workspace;
|
|
541
|
+
// The host computes readiness; a model cannot open the gate by inflating its score.
|
|
542
|
+
const readiness = Object.values(result.slots).filter(value => value.state !== 'missing').length / 4;
|
|
543
|
+
current.stop_asking ||= result.early_exit || current.questions_asked >= this.#budget()
|
|
544
|
+
|| readiness >= (this.#options.settings.clarification_depth === 'thorough' ? 1 : .75);
|
|
545
|
+
if (kind === 'switch' || (kind === 'create' && current.slots.goal.state === 'missing' && question === null)) {
|
|
546
|
+
stage = 'prepare';
|
|
547
|
+
// No plan cycle, still confirmed: every change of the active project is confirmed by the user
|
|
548
|
+
// before any side effect (decision 2026-09-04), and creating a workspace is irreversible.
|
|
549
|
+
current.plan_revision = current.revision;
|
|
550
|
+
current.work_order = null;
|
|
551
|
+
current.title = null;
|
|
552
|
+
if (this.#userInputPending)
|
|
553
|
+
return;
|
|
554
|
+
this.#propose(current, `${kind === 'switch' ? '切换到' : '新建'}项目“${target.workspace_display_name}”,不派任务`);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
if (!current.stop_asking && question !== null) {
|
|
558
|
+
this.#ask(current, question);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
// No user-owned blocker: repository discovery never holds up planning.
|
|
562
|
+
current.stop_asking = true;
|
|
563
|
+
if (current.slots.goal.state !== 'stated') {
|
|
564
|
+
if (current.missing_goal_grace !== null && current.revision > current.missing_goal_grace) {
|
|
565
|
+
this.#close('abandoned');
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
current.missing_goal_grace ??= current.revision;
|
|
569
|
+
current.state = 'clarifying';
|
|
570
|
+
this.#options.fact(current, '还缺少要完成的具体目标,请说明希望实现或修复什么。暂不规划或执行。');
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
current.state = 'ready_to_plan';
|
|
574
|
+
this.#planPending = true;
|
|
575
|
+
}
|
|
576
|
+
catch (error) {
|
|
577
|
+
if (!abort.signal.aborted)
|
|
578
|
+
this.#failure(snapshot, stage, error);
|
|
579
|
+
}
|
|
580
|
+
finally {
|
|
581
|
+
abort.abort();
|
|
582
|
+
this.#abort.delete(abort);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
#namedSessionEvidence(project, title, current) {
|
|
586
|
+
if (!title)
|
|
587
|
+
return false;
|
|
588
|
+
const roster = this.#options.roster();
|
|
589
|
+
const evidence = [current.opening, ...current.turns.map(turn => turn.answer)].map(directiveText);
|
|
590
|
+
const namedProject = evidence.some(text => text.includes(project));
|
|
591
|
+
if (!namedProject && roster.filter(entry => entry.sessions?.includes(title)).length !== 1)
|
|
592
|
+
return false;
|
|
593
|
+
const text = latestSessionDirective(evidence), index = text.indexOf(title);
|
|
594
|
+
if (!roster.some(entry => entry.name === project && entry.sessions?.includes(title))
|
|
595
|
+
|| index < 0 || /[??]|不确定|是否|之前|先(?:问|确认)|\b(?:not sure|before)\b/iu.test(text)
|
|
596
|
+
|| /(?:不允许|不要|别(?!的)|不准|禁止|\bdo not|\bdon't)[^,,。.!!;;\n]*$/iu.test(text.slice(0, index)))
|
|
597
|
+
return false;
|
|
598
|
+
const before = text.slice(0, index).trimEnd(), after = text.slice(index + title.length).trimStart();
|
|
599
|
+
return /(?:继续|接着|\bcontinue|\bresume)\s*$/iu.test(before)
|
|
600
|
+
|| (/(?:在|用|使用|切换到|\bin(?: the)?)\s*$/iu.test(before) && /^(?:(?:这个)?会话|session\b)/iu.test(after));
|
|
601
|
+
}
|
|
602
|
+
async #plan(snapshot) {
|
|
603
|
+
const abort = new AbortController();
|
|
604
|
+
this.#abort.add(abort);
|
|
605
|
+
let stage = 'plan';
|
|
606
|
+
try {
|
|
607
|
+
const generatePlan = this.#options.settings.generate_plan !== false && snapshot.execution_mode !== 'direct';
|
|
608
|
+
const result = !generatePlan ? {
|
|
609
|
+
intake_id: snapshot.intake_id, revision: snapshot.revision,
|
|
610
|
+
work_order: { objective: snapshot.slots.goal.note, scope_in: [], scope_out: [], acceptance: [], constraints: [], discovery: [], assumptions: [] },
|
|
611
|
+
} : await this.#model(snapshot, 'plan', planSchema, this.#input(snapshot), abort);
|
|
612
|
+
if (result === null)
|
|
613
|
+
return;
|
|
614
|
+
const current = this.#current(snapshot.intake_id, snapshot.revision);
|
|
615
|
+
if (current === null)
|
|
616
|
+
return;
|
|
617
|
+
if (result.intake_id !== current.intake_id || result.revision !== current.revision) {
|
|
618
|
+
this.#options.diagnostic('intake_stale_result');
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
const slots = current.slots;
|
|
622
|
+
const order = {
|
|
623
|
+
...result.work_order,
|
|
624
|
+
objective: slots.goal.note,
|
|
625
|
+
scope_in: slots.scope.state === 'stated' ? [slots.scope.note] : [],
|
|
626
|
+
scope_out: slots.scope.state === 'stated' ? result.work_order.scope_out : [],
|
|
627
|
+
acceptance: slots.acceptance.state === 'stated' ? [slots.acceptance.note] : ['User did not specify; propose and report'],
|
|
628
|
+
constraints: slots.constraints.state === 'stated' ? [slots.constraints.note] : [],
|
|
629
|
+
discovery: [...new Set([...current.discovery, ...result.work_order.discovery])].slice(0, 12),
|
|
630
|
+
assumptions: [...new Set([...Object.values(slots).filter(slot => slot.state === 'inferred').map(slot => slot.note), ...result.work_order.assumptions])].slice(0, 12),
|
|
631
|
+
};
|
|
632
|
+
if (current.plan_revision !== current.revision) {
|
|
633
|
+
stage = 'evidence';
|
|
634
|
+
const evidenceStarted = this.#clock.now();
|
|
635
|
+
const evidence = this.#options.attachEvidence === undefined ? undefined
|
|
636
|
+
: await raceDeadline(this.#options.attachEvidence(order, current.workspace, abort.signal, current.target), this.#clock, 30, abort.signal, () => new DOMException('evidence deadline', 'TimeoutError'));
|
|
637
|
+
if (this.#current(snapshot.intake_id, snapshot.revision) !== current || abort.signal.aborted)
|
|
638
|
+
return;
|
|
639
|
+
this.#options.record(current, 'intake.timing', { stage: 'evidence', elapsed_ms: Math.round((this.#clock.now() - evidenceStarted) * 1000) });
|
|
640
|
+
current.work_order = renderWorkOrder({ ...order, ...evidence });
|
|
641
|
+
current.title = deriveSessionTitle(order.objective);
|
|
642
|
+
current.plan_revision = current.revision;
|
|
643
|
+
current.state = 'readback';
|
|
644
|
+
this.#options.record(current, 'plan.compile', { work_order: current.work_order, generated: generatePlan });
|
|
645
|
+
}
|
|
646
|
+
// A spoken amendment may still be awaiting ASR. Never execute the old plan in that gap.
|
|
647
|
+
if (this.#userInputPending)
|
|
648
|
+
return;
|
|
649
|
+
const project = current.target?.workspace_display_name ?? '';
|
|
650
|
+
// The readback line always names the project. A plan that changes the active project (create, or
|
|
651
|
+
// work quoted into another project) is confirmed under every `plan_readback` (decision 2026-09-04).
|
|
652
|
+
const direct = current.direct_start_revision === current.revision && current.kind === 'work'
|
|
653
|
+
&& current.bound_target?.workspace_id != null && current.bound_target.workspace_id === current.target?.workspace_id
|
|
654
|
+
&& (current.target.action === 'reuse' || current.target.action === 'resume');
|
|
655
|
+
if ((this.#options.settings.plan_readback === 'confirm' && !direct) || project !== this.#options.activeProject()) {
|
|
656
|
+
stage = 'prepare';
|
|
657
|
+
this.#propose(current, `计划(项目 ${project}):${limit(order.objective, 200)}`);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
current.state = 'committing';
|
|
661
|
+
const wanted = this.#launchWanted(current);
|
|
662
|
+
stage = 'dispatch';
|
|
663
|
+
const admission = await this.#options.dispatch(current, wanted);
|
|
664
|
+
if (this.#current(snapshot.intake_id, snapshot.revision) === current)
|
|
665
|
+
this.#settle(admission);
|
|
666
|
+
}
|
|
667
|
+
catch (error) {
|
|
668
|
+
if (!abort.signal.aborted)
|
|
669
|
+
this.#failure(snapshot, stage, error);
|
|
670
|
+
}
|
|
671
|
+
finally {
|
|
672
|
+
abort.abort();
|
|
673
|
+
this.#abort.delete(abort);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
#ask(current, question, project) {
|
|
677
|
+
if (current.questions_asked >= this.#budget()) {
|
|
678
|
+
this.#close('abandoned');
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
current.questions_asked += 1;
|
|
682
|
+
current.pending_question = question;
|
|
683
|
+
current.pending_project_question = project ?? null;
|
|
684
|
+
current.state = 'clarifying';
|
|
685
|
+
this.#options.fact(current, `只问下面这一个问题,不调用编码工具:${question}`);
|
|
686
|
+
}
|
|
687
|
+
#propose(current, summary) {
|
|
688
|
+
const proposal = this.#options.prepare(current);
|
|
689
|
+
current.proposal_id = proposal.proposal_id;
|
|
690
|
+
current.state = 'readback';
|
|
691
|
+
this.#options.fact(current, `${proposal.confirmation_prompt} ${summary}。id=${proposal.proposal_id};仅通过 confirm(id, accepted) 回答;修改需求会使此提议失效。`);
|
|
692
|
+
}
|
|
693
|
+
/** Closed without a plan cycle: the fact carries the adapter's structured code. */
|
|
694
|
+
#route(current, text) {
|
|
695
|
+
this.#close('routed');
|
|
696
|
+
this.#options.fact(current, text);
|
|
697
|
+
}
|
|
698
|
+
#launchWanted(current) {
|
|
699
|
+
const revision = current.revision;
|
|
700
|
+
// Admission transfers ownership to the delegate. Completing intake does not cancel it.
|
|
701
|
+
const taskAuthority = this.#taskAuthority;
|
|
702
|
+
return () => (taskAuthority?.() ?? true) && current.revision === revision && (current.outcome === 'dispatched' || current.outcome === 'routed'
|
|
703
|
+
|| (this.#session === current && current.state !== 'closed' && !this.#userInputPending));
|
|
704
|
+
}
|
|
705
|
+
#settle(result) {
|
|
706
|
+
const current = this.#session;
|
|
707
|
+
current.delegate_id = result.delegate_id ?? null;
|
|
708
|
+
this.#options.record(current, 'intake.dispatch', {
|
|
709
|
+
accepted: result.accepted, delegate_id: current.delegate_id,
|
|
710
|
+
questions_asked: current.questions_asked, work_order_chars: current.work_order?.length ?? 0,
|
|
711
|
+
});
|
|
712
|
+
this.#close(result.accepted ? 'dispatched' : 'admission_refused');
|
|
713
|
+
if (!result.accepted)
|
|
714
|
+
this.#options.fact(current, `任务尚未执行:${result.problem ?? result.code ?? 'runtime_rejected'}。可重新提出任务;若结果未知,先验证再重试。`);
|
|
715
|
+
}
|
|
716
|
+
#budget() { return { minimal: 1, balanced: 3, thorough: 5 }[this.#options.settings.clarification_depth]; }
|
|
717
|
+
/** Retry only pure model calls, never resolution, confirmation, steering or admission. */
|
|
718
|
+
async #model(snapshot, stage, schema, input, abort) {
|
|
719
|
+
const key = `${snapshot.intake_id}:${snapshot.revision}:${stage}`;
|
|
720
|
+
if (this.#modelResults.has(key))
|
|
721
|
+
return schema.parse(this.#modelResults.get(key));
|
|
722
|
+
const budget = this.#modelBudgets.get(key) ?? { attempts: 0, deadline: this.#clock.now() + 30 };
|
|
723
|
+
this.#modelBudgets.set(key, budget);
|
|
724
|
+
const deadline = budget.deadline;
|
|
725
|
+
let modelInput = input;
|
|
726
|
+
while (budget.attempts < 2) {
|
|
727
|
+
if (abort.signal.aborted || this.#live(snapshot.intake_id, snapshot.revision) === null)
|
|
728
|
+
return null;
|
|
729
|
+
if (this.#clock.now() >= deadline) {
|
|
730
|
+
this.#failure(snapshot, stage, new DOMException('model deadline', 'TimeoutError'), budget.attempts);
|
|
731
|
+
return null;
|
|
732
|
+
}
|
|
733
|
+
const requestInput = modelInput;
|
|
734
|
+
const started = this.#clock.now();
|
|
735
|
+
const attempt = ++budget.attempts;
|
|
736
|
+
const timeout = new AbortController();
|
|
737
|
+
const signal = AbortSignal.any([abort.signal, timeout.signal]);
|
|
738
|
+
try {
|
|
739
|
+
const raw = await raceDeadline(this.#options.models[stage](requestInput, signal), this.#clock, Math.max(0, deadline - this.#clock.now()), abort.signal, () => { timeout.abort(); return new DOMException('model deadline', 'TimeoutError'); });
|
|
740
|
+
if (this.#current(snapshot.intake_id, snapshot.revision) === null)
|
|
741
|
+
return null;
|
|
742
|
+
const result = schema.parse(raw);
|
|
743
|
+
this.#modelResults.set(key, result);
|
|
744
|
+
this.#modelBudgets.delete(key);
|
|
745
|
+
return result;
|
|
746
|
+
}
|
|
747
|
+
catch (error) {
|
|
748
|
+
if (abort.signal.aborted || this.#live(snapshot.intake_id, snapshot.revision) === null)
|
|
749
|
+
return null;
|
|
750
|
+
const retrying = attempt < 2 && this.#clock.now() + 1 < deadline
|
|
751
|
+
&& ['rate_limit', 'server_error', 'transport', 'timeout', 'invalid_output'].includes(failureReason(error));
|
|
752
|
+
if (error instanceof z.ZodError)
|
|
753
|
+
modelInput = { ...input, validation_feedback: error.issues.map(issue => `${issue.path.join('.')}: ${issue.message}`).join('; ').slice(0, 2000) };
|
|
754
|
+
this.#failure(snapshot, stage, error, attempt, retrying);
|
|
755
|
+
if (!retrying)
|
|
756
|
+
return null;
|
|
757
|
+
try {
|
|
758
|
+
await this.#clock.sleep(1, abort.signal);
|
|
759
|
+
}
|
|
760
|
+
catch {
|
|
761
|
+
return null;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
finally {
|
|
765
|
+
timeout.abort();
|
|
766
|
+
const current = this.#live(snapshot.intake_id, snapshot.revision);
|
|
767
|
+
const model = stage === 'assess' ? this.#options.settings.support_model
|
|
768
|
+
: (this.#options.settings.planner_model ?? '') !== ''
|
|
769
|
+
? this.#options.settings.planner_model : this.#options.settings.fast_model;
|
|
770
|
+
if (current !== null)
|
|
771
|
+
this.#options.record(current, 'intake.timing', { stage, attempt,
|
|
772
|
+
...(model === undefined ? {} : { model }), input_chars: JSON.stringify(requestInput).length,
|
|
773
|
+
elapsed_ms: Math.round((this.#clock.now() - started) * 1000) });
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return null;
|
|
777
|
+
}
|
|
778
|
+
#failure(snapshot, stage, error, attempt = 1, retrying = false) {
|
|
779
|
+
const current = this.#live(snapshot.intake_id, snapshot.revision);
|
|
780
|
+
if (current === null)
|
|
781
|
+
return;
|
|
782
|
+
const reason = failureReason(error);
|
|
783
|
+
const detail = failureDetail(error);
|
|
784
|
+
const unknown = stage === 'dispatch' || stage === 'steer';
|
|
785
|
+
if (!retrying) {
|
|
786
|
+
if (!unknown && current.proposal_id !== null) {
|
|
787
|
+
this.#options.invalidateProposal();
|
|
788
|
+
current.proposal_id = null;
|
|
789
|
+
}
|
|
790
|
+
current.state = unknown ? 'dispatch_unknown' : 'failed';
|
|
791
|
+
this.#assessPending = false;
|
|
792
|
+
this.#planPending = false;
|
|
793
|
+
}
|
|
794
|
+
this.#options.record(current, 'intake.failure', { stage, reason, attempt, retrying, ...(detail === null ? {} : { detail }) });
|
|
795
|
+
this.#options.diagnostic(`intake_${stage}_${reason}`);
|
|
796
|
+
if (detail !== null)
|
|
797
|
+
this.#options.diagnostic(`intake_failure_detail stage=${stage} attempt=${attempt} detail=${detail}`);
|
|
798
|
+
if (retrying)
|
|
799
|
+
return;
|
|
800
|
+
this.#options.onStateChanged?.();
|
|
801
|
+
this.#options.fact(current, unknown
|
|
802
|
+
? '派单结果暂时无法确认,任务可能已开始。需要先核实执行状态,不要重复派单。'
|
|
803
|
+
: reason === 'authentication' || reason === 'provider_rejected'
|
|
804
|
+
? '计划服务的连接或配置需要检查。原需求已保留,任务尚未执行,修复后可以继续。'
|
|
805
|
+
: '这次计划暂时未能生成,原需求已保留,任务尚未执行,可以稍后继续,无需重述需求。');
|
|
806
|
+
}
|
|
807
|
+
#close(outcome) {
|
|
808
|
+
const current = this.#session;
|
|
809
|
+
this.#modelResults.clear();
|
|
810
|
+
this.#modelBudgets.clear();
|
|
811
|
+
current.state = 'closed';
|
|
812
|
+
current.outcome = outcome;
|
|
813
|
+
this.#assessPending = false;
|
|
814
|
+
this.#planPending = false;
|
|
815
|
+
for (const abort of this.#abort)
|
|
816
|
+
abort.abort();
|
|
817
|
+
if (current.proposal_id !== null && outcome !== 'dispatched')
|
|
818
|
+
this.#options.invalidateProposal();
|
|
819
|
+
this.#options.onStateChanged?.();
|
|
820
|
+
if (outcome === 'abandoned')
|
|
821
|
+
this.#options.fact(current, '抱歉,仍未能形成明确工作单,本次需求已结束,尚未执行。请重新说明具体目标。');
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
//# sourceMappingURL=intake.js.map
|