@qvac/core 1.0.0-rc.1 → 1.0.0-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1008 -1
- package/README.md +40 -81
- package/dist-lib/index.d.ts +1 -1
- package/dist-lib/index.js +1 -1
- package/dist-lib/lib/api/agent-folders.d.ts +22 -0
- package/dist-lib/lib/api/agent-folders.js +115 -0
- package/dist-lib/lib/api/agents.d.ts +19 -2
- package/dist-lib/lib/api/agents.js +121 -24
- package/dist-lib/lib/api/app-config.d.ts +4 -1
- package/dist-lib/lib/api/app-config.js +17 -6
- package/dist-lib/lib/api/attachments.js +3 -2
- package/dist-lib/lib/api/chat-move.d.ts +4 -0
- package/dist-lib/lib/api/chat-move.js +7 -0
- package/dist-lib/lib/api/chat.d.ts +13 -2
- package/dist-lib/lib/api/chat.js +114 -9
- package/dist-lib/lib/api/chunk-delta.d.ts +12 -0
- package/dist-lib/lib/api/chunk-delta.js +115 -0
- package/dist-lib/lib/api/chunks.d.ts +17 -2
- package/dist-lib/lib/api/chunks.js +70 -13
- package/dist-lib/lib/api/config.d.ts +6 -2
- package/dist-lib/lib/api/config.js +47 -5
- package/dist-lib/lib/api/current-mesh.d.ts +13 -5
- package/dist-lib/lib/api/current-mesh.js +15 -11
- package/dist-lib/lib/api/custom-models.d.ts +7 -1
- package/dist-lib/lib/api/custom-models.js +12 -12
- package/dist-lib/lib/api/device-routes.d.ts +9 -0
- package/dist-lib/lib/api/device-routes.js +123 -0
- package/dist-lib/lib/api/devices.d.ts +3 -2
- package/dist-lib/lib/api/devices.js +23 -11
- package/dist-lib/lib/api/diagnostics.d.ts +2 -1
- package/dist-lib/lib/api/diagnostics.js +2 -2
- package/dist-lib/lib/api/execution.d.ts +12 -1
- package/dist-lib/lib/api/execution.js +44 -14
- package/dist-lib/lib/api/files.d.ts +1 -1
- package/dist-lib/lib/api/files.js +65 -8
- package/dist-lib/lib/api/host-run-log.d.ts +2 -41
- package/dist-lib/lib/api/host-run-log.js +35 -83
- package/dist-lib/lib/api/index.d.ts +28 -6
- package/dist-lib/lib/api/index.js +101 -33
- package/dist-lib/lib/api/instrument.d.ts +2 -2
- package/dist-lib/lib/api/instrument.js +24 -48
- package/dist-lib/lib/api/knowledge.d.ts +4 -1
- package/dist-lib/lib/api/knowledge.js +25 -7
- package/dist-lib/lib/api/local-files.d.ts +5 -1
- package/dist-lib/lib/api/local-files.js +9 -5
- package/dist-lib/lib/api/logs.js +3 -3
- package/dist-lib/lib/api/members.d.ts +10 -1
- package/dist-lib/lib/api/members.js +67 -26
- package/dist-lib/lib/api/mesh.d.ts +12 -4
- package/dist-lib/lib/api/mesh.js +12 -8
- package/dist-lib/lib/api/model-download.d.ts +2 -1
- package/dist-lib/lib/api/model-download.js +2 -2
- package/dist-lib/lib/api/network.d.ts +10 -2
- package/dist-lib/lib/api/network.js +8 -2
- package/dist-lib/lib/api/notifications.d.ts +3 -1
- package/dist-lib/lib/api/notifications.js +14 -9
- package/dist-lib/lib/api/projects.d.ts +20 -0
- package/dist-lib/lib/api/projects.js +169 -0
- package/dist-lib/lib/api/rag.d.ts +0 -1
- package/dist-lib/lib/api/rag.js +71 -34
- package/dist-lib/lib/api/served.d.ts +17 -0
- package/dist-lib/lib/api/served.js +48 -0
- package/dist-lib/lib/api/sync.d.ts +2 -1
- package/dist-lib/lib/api/sync.js +2 -2
- package/dist-lib/lib/api/tool-activity.d.ts +2 -1
- package/dist-lib/lib/api/tool-activity.js +4 -3
- package/dist-lib/lib/api/tool-configs.d.ts +2 -1
- package/dist-lib/lib/api/tool-configs.js +6 -9
- package/dist-lib/lib/api/upload-frames.d.ts +0 -1
- package/dist-lib/lib/api/upload-frames.js +0 -1
- package/dist-lib/lib/api/watchable.d.ts +3 -2
- package/dist-lib/lib/api/watchable.js +2 -3
- package/dist-lib/lib/api/workspace.d.ts +5 -0
- package/dist-lib/lib/api/workspace.js +47 -0
- package/dist-lib/lib/assistant/fake-models.d.ts +20 -0
- package/dist-lib/lib/assistant/fake-models.js +112 -0
- package/dist-lib/lib/assistant/harness.d.ts +37 -31
- package/dist-lib/lib/assistant/harness.js +51 -10
- package/dist-lib/lib/assistant/history.d.ts +30 -17
- package/dist-lib/lib/assistant/history.js +179 -87
- package/dist-lib/lib/assistant/host-run-log.d.ts +6 -1
- package/dist-lib/lib/assistant/host-run-log.js +24 -36
- package/dist-lib/lib/assistant/message.d.ts +27 -1
- package/dist-lib/lib/assistant/message.js +65 -2
- package/dist-lib/lib/assistant/model-round.d.ts +100 -0
- package/dist-lib/lib/assistant/model-round.js +619 -0
- package/dist-lib/lib/assistant/pdf-budget.d.ts +11 -0
- package/dist-lib/lib/assistant/pdf-budget.js +12 -0
- package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
- package/dist-lib/lib/assistant/pdf-pages.js +75 -0
- package/dist-lib/lib/assistant/round-host.d.ts +42 -0
- package/dist-lib/lib/assistant/round-host.js +241 -0
- package/dist-lib/lib/assistant/skill-encode.d.ts +2 -1
- package/dist-lib/lib/assistant/skill-encode.js +2 -1
- package/dist-lib/lib/assistant/video-budget.d.ts +13 -0
- package/dist-lib/lib/assistant/video-budget.js +29 -0
- package/dist-lib/lib/assistant-client.d.ts +4 -1
- package/dist-lib/lib/assistant-client.js +27 -8
- package/dist-lib/lib/assistant-tree.d.ts +10 -3
- package/dist-lib/lib/assistant-tree.js +63 -20
- package/dist-lib/lib/assistant.d.ts +93 -24
- package/dist-lib/lib/assistant.js +2048 -1231
- package/dist-lib/lib/audio-format.js +1 -1
- package/dist-lib/lib/chat-suggestion.d.ts +3 -0
- package/dist-lib/lib/chat-suggestion.js +47 -0
- package/dist-lib/lib/client.d.ts +2 -2
- package/dist-lib/lib/client.js +10 -1
- package/dist-lib/lib/compaction.d.ts +3 -1
- package/dist-lib/lib/compaction.js +18 -4
- package/dist-lib/lib/constants.d.ts +10 -0
- package/dist-lib/lib/constants.js +10 -0
- package/dist-lib/lib/core/agent-copies.d.ts +9 -0
- package/dist-lib/lib/core/agent-copies.js +40 -0
- package/dist-lib/lib/core/agent-hosting.d.ts +25 -0
- package/dist-lib/lib/core/agent-hosting.js +71 -0
- package/dist-lib/lib/core/agent.d.ts +9 -1
- package/dist-lib/lib/core/agent.js +45 -19
- package/dist-lib/lib/core/archived-mesh.d.ts +11 -6
- package/dist-lib/lib/core/archived-mesh.js +77 -58
- package/dist-lib/lib/core/async-iterable.js +1 -1
- package/dist-lib/lib/core/blind-peers.d.ts +1 -0
- package/dist-lib/lib/core/blind-peers.js +1 -0
- package/dist-lib/lib/core/chat-activity.d.ts +1 -1
- package/dist-lib/lib/core/chat-activity.js +45 -34
- package/dist-lib/lib/core/chat-copy.d.ts +42 -0
- package/dist-lib/lib/core/chat-copy.js +395 -0
- package/dist-lib/lib/core/chat-fork.d.ts +16 -0
- package/dist-lib/lib/core/chat-fork.js +246 -0
- package/dist-lib/lib/core/chat-group.js +8 -7
- package/dist-lib/lib/core/chat-move.d.ts +75 -0
- package/dist-lib/lib/core/chat-move.js +598 -0
- package/dist-lib/lib/core/chat-order.d.ts +2 -1
- package/dist-lib/lib/core/chat-order.js +19 -4
- package/dist-lib/lib/core/chat-project.d.ts +2 -0
- package/dist-lib/lib/core/chat-project.js +15 -0
- package/dist-lib/lib/core/chat-read.d.ts +4 -0
- package/dist-lib/lib/core/chat-read.js +49 -0
- package/dist-lib/lib/core/chat-search.js +14 -12
- package/dist-lib/lib/core/chat.d.ts +33 -4
- package/dist-lib/lib/core/chat.js +122 -39
- package/dist-lib/lib/core/chunk.d.ts +35 -7
- package/dist-lib/lib/core/chunk.js +250 -45
- package/dist-lib/lib/core/collections.d.ts +40 -1
- package/dist-lib/lib/core/collections.js +40 -1
- package/dist-lib/lib/core/config-value.d.ts +6 -0
- package/dist-lib/lib/core/config-value.js +14 -0
- package/dist-lib/lib/core/contract/activity.d.ts +3 -2
- package/dist-lib/lib/core/contract/activity.js +39 -21
- package/dist-lib/lib/core/contract/agent-join.d.ts +10 -0
- package/dist-lib/lib/core/contract/agent-join.js +104 -0
- package/dist-lib/lib/core/contract/agents.d.ts +5 -0
- package/dist-lib/lib/core/contract/agents.js +142 -3
- package/dist-lib/lib/core/contract/chat.d.ts +4 -0
- package/dist-lib/lib/core/contract/chat.js +53 -3
- package/dist-lib/lib/core/contract/chunks.d.ts +7 -0
- package/dist-lib/lib/core/contract/chunks.js +125 -38
- package/dist-lib/lib/core/contract/devices.js +6 -4
- package/dist-lib/lib/core/contract/index.d.ts +2 -3
- package/dist-lib/lib/core/contract/index.js +36 -11
- package/dist-lib/lib/core/contract/indexing.d.ts +1 -0
- package/dist-lib/lib/core/contract/indexing.js +51 -7
- package/dist-lib/lib/core/contract/invites.d.ts +3 -14
- package/dist-lib/lib/core/contract/invites.js +26 -36
- package/dist-lib/lib/core/contract/joined-meshes.js +3 -1
- package/dist-lib/lib/core/contract/knowledge.d.ts +1 -0
- package/dist-lib/lib/core/contract/knowledge.js +57 -6
- package/dist-lib/lib/core/contract/members.d.ts +3 -1
- package/dist-lib/lib/core/contract/members.js +34 -4
- package/dist-lib/lib/core/contract/mesh-kind.d.ts +3 -3
- package/dist-lib/lib/core/contract/mesh-kind.js +20 -16
- package/dist-lib/lib/core/contract/mesh-name.d.ts +3 -0
- package/dist-lib/lib/core/contract/mesh-name.js +31 -0
- package/dist-lib/lib/core/contract/mesh-rules.d.ts +8 -0
- package/dist-lib/lib/core/contract/mesh-rules.js +35 -0
- package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
- package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
- package/dist-lib/lib/core/contract/model-requests.d.ts +26 -0
- package/dist-lib/lib/core/contract/model-requests.js +280 -0
- package/dist-lib/lib/core/contract/oldest-version.d.ts +9 -0
- package/dist-lib/lib/core/contract/oldest-version.js +20 -0
- package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
- package/dist-lib/lib/core/contract/open-join.js +77 -0
- package/dist-lib/lib/core/contract/project-device-keys.d.ts +7 -0
- package/dist-lib/lib/core/contract/project-device-keys.js +38 -0
- package/dist-lib/lib/core/contract/project-host-keys.d.ts +7 -0
- package/dist-lib/lib/core/contract/project-host-keys.js +41 -0
- package/dist-lib/lib/core/contract/project-order.d.ts +2 -0
- package/dist-lib/lib/core/contract/project-order.js +25 -0
- package/dist-lib/lib/core/contract/projects.d.ts +21 -0
- package/dist-lib/lib/core/contract/projects.js +327 -0
- package/dist-lib/lib/core/contract/public-mesh.d.ts +3 -0
- package/dist-lib/lib/core/contract/public-mesh.js +31 -0
- package/dist-lib/lib/core/contract/public-seeds.d.ts +9 -0
- package/dist-lib/lib/core/contract/public-seeds.js +43 -0
- package/dist-lib/lib/core/contract/roles.d.ts +10 -0
- package/dist-lib/lib/core/contract/roles.js +121 -0
- package/dist-lib/lib/core/contract/run-requests.d.ts +10 -0
- package/dist-lib/lib/core/contract/run-requests.js +81 -0
- package/dist-lib/lib/core/contract/seed-defaults.d.ts +3 -1
- package/dist-lib/lib/core/contract/seed-defaults.js +17 -4
- package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
- package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
- package/dist-lib/lib/core/contract/skill-secrets.d.ts +3 -0
- package/dist-lib/lib/core/contract/skill-secrets.js +32 -0
- package/dist-lib/lib/core/contract/tool-activity.js +12 -1
- package/dist-lib/lib/core/contract/tool-configs.js +2 -1
- package/dist-lib/lib/core/contract/types.d.ts +2 -2
- package/dist-lib/lib/core/contract/workspace.d.ts +31 -0
- package/dist-lib/lib/core/contract/workspace.js +311 -0
- package/dist-lib/lib/core/contract/writers.js +100 -16
- package/dist-lib/lib/core/departures.d.ts +16 -5
- package/dist-lib/lib/core/departures.js +79 -6
- package/dist-lib/lib/core/device-control.d.ts +21 -0
- package/dist-lib/lib/core/device-control.js +91 -0
- package/dist-lib/lib/core/device-seal.d.ts +4 -0
- package/dist-lib/lib/core/device-seal.js +22 -0
- package/dist-lib/lib/core/devices.d.ts +2 -1
- package/dist-lib/lib/core/devices.js +5 -3
- package/dist-lib/lib/core/document-attachment.d.ts +10 -0
- package/dist-lib/lib/core/document-attachment.js +14 -0
- package/dist-lib/lib/core/files/attachments.js +8 -7
- package/dist-lib/lib/core/files/index.d.ts +27 -14
- package/dist-lib/lib/core/files/index.js +220 -43
- package/dist-lib/lib/core/files/knowledge.d.ts +10 -1
- package/dist-lib/lib/core/files/knowledge.js +77 -5
- package/dist-lib/lib/core/files/preview/index.js +6 -2
- package/dist-lib/lib/core/files/processing/claims.d.ts +7 -0
- package/dist-lib/lib/core/files/processing/claims.js +33 -7
- package/dist-lib/lib/core/files/processing/document.d.ts +8 -0
- package/dist-lib/lib/core/files/processing/document.js +26 -2
- package/dist-lib/lib/core/files/processing/index.d.ts +8 -3
- package/dist-lib/lib/core/files/processing/index.js +99 -73
- package/dist-lib/lib/core/files/processing/protocol.d.ts +7 -1
- package/dist-lib/lib/core/files/processing/protocol.js +14 -3
- package/dist-lib/lib/core/files/processing/source.d.ts +4 -0
- package/dist-lib/lib/core/files/processing/source.js +12 -4
- package/dist-lib/lib/core/files/processing/state.d.ts +5 -1
- package/dist-lib/lib/core/files/processing/state.js +9 -1
- package/dist-lib/lib/core/files/rag/build.d.ts +5 -1
- package/dist-lib/lib/core/files/rag/build.js +32 -8
- package/dist-lib/lib/core/files/rag/failure.d.ts +20 -0
- package/dist-lib/lib/core/files/rag/failure.js +57 -0
- package/dist-lib/lib/core/files/rag/index.d.ts +13 -2
- package/dist-lib/lib/core/files/rag/index.js +30 -8
- package/dist-lib/lib/core/files/rag/joined-index.d.ts +6 -0
- package/dist-lib/lib/core/files/rag/joined-index.js +35 -0
- package/dist-lib/lib/core/files/rag/limits.d.ts +1 -0
- package/dist-lib/lib/core/files/rag/limits.js +1 -0
- package/dist-lib/lib/core/files/rag/load.js +6 -3
- package/dist-lib/lib/core/files/rag/policy.d.ts +10 -1
- package/dist-lib/lib/core/files/rag/policy.js +23 -2
- package/dist-lib/lib/core/files/rag/produce.d.ts +1 -1
- package/dist-lib/lib/core/files/rag/produce.js +31 -18
- package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
- package/dist-lib/lib/core/files/rag/retrieval/full-text.js +8 -2
- package/dist-lib/lib/core/files/rag/similarity.d.ts +2 -2
- package/dist-lib/lib/core/files/rag/similarity.js +2 -3
- package/dist-lib/lib/core/files/records/chunks.js +22 -1
- package/dist-lib/lib/core/files/records/index.d.ts +1 -1
- package/dist-lib/lib/core/files/records/index.js +1 -1
- package/dist-lib/lib/core/files/records/knowledge.d.ts +11 -1
- package/dist-lib/lib/core/files/records/knowledge.js +40 -3
- package/dist-lib/lib/core/files/records/purge.js +13 -8
- package/dist-lib/lib/core/files/records/reclaim.js +20 -48
- package/dist-lib/lib/core/files/records/routing.d.ts +1 -1
- package/dist-lib/lib/core/files/records/routing.js +6 -2
- package/dist-lib/lib/core/files/records/source.js +3 -5
- package/dist-lib/lib/core/files/records/sweep.d.ts +3 -0
- package/dist-lib/lib/core/files/records/sweep.js +9 -3
- package/dist-lib/lib/core/files/store/blob.d.ts +2 -0
- package/dist-lib/lib/core/files/store/blob.js +2 -2
- package/dist-lib/lib/core/files/store/index.d.ts +3 -1
- package/dist-lib/lib/core/files/store/index.js +4 -1
- package/dist-lib/lib/core/files/store/reference.d.ts +3 -0
- package/dist-lib/lib/core/files/store/reference.js +12 -2
- package/dist-lib/lib/core/files/store/sweep.js +3 -1
- package/dist-lib/lib/core/files/upload.d.ts +5 -1
- package/dist-lib/lib/core/files/upload.js +53 -14
- package/dist-lib/lib/core/first-event.d.ts +6 -0
- package/dist-lib/lib/core/first-event.js +13 -0
- package/dist-lib/lib/core/fs-drives.d.ts +6 -3
- package/dist-lib/lib/core/fs-drives.js +30 -18
- package/dist-lib/lib/core/id.d.ts +1 -0
- package/dist-lib/lib/core/id.js +3 -0
- package/dist-lib/lib/core/identity.d.ts +3 -1
- package/dist-lib/lib/core/identity.js +25 -4
- package/dist-lib/lib/core/invite.d.ts +6 -2
- package/dist-lib/lib/core/invite.js +22 -8
- package/dist-lib/lib/core/local/files.d.ts +2 -1
- package/dist-lib/lib/core/local/files.js +5 -7
- package/dist-lib/lib/core/local/index.d.ts +86 -7
- package/dist-lib/lib/core/local/index.js +312 -27
- package/dist-lib/lib/core/local/run-logs.d.ts +38 -0
- package/dist-lib/lib/core/local/run-logs.js +132 -0
- package/dist-lib/lib/core/member-key.d.ts +7 -0
- package/dist-lib/lib/core/member-key.js +24 -0
- package/dist-lib/lib/core/membership.d.ts +3 -14
- package/dist-lib/lib/core/membership.js +3 -96
- package/dist-lib/lib/core/mesh.d.ts +107 -30
- package/dist-lib/lib/core/mesh.js +625 -171
- package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
- package/dist-lib/lib/core/migrations/agent-tools-scope.js +24 -0
- package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
- package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
- package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
- package/dist-lib/lib/core/migrations/file-index.js +8 -6
- package/dist-lib/lib/core/migrations/index.d.ts +2 -1
- package/dist-lib/lib/core/migrations/index.js +69 -8
- package/dist-lib/lib/core/model-requests.d.ts +13 -0
- package/dist-lib/lib/core/model-requests.js +65 -0
- package/dist-lib/lib/core/next-frame.js +2 -1
- package/dist-lib/lib/core/notification.js +100 -25
- package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
- package/dist-lib/lib/core/pdf/figure.js +64 -0
- package/dist-lib/lib/core/pdf/rasterize.d.ts +12 -3
- package/dist-lib/lib/core/pdf/rasterize.js +77 -28
- package/dist-lib/lib/core/peer-duplex.d.ts +34 -0
- package/dist-lib/lib/core/{harness-loan.js → peer-duplex.js} +104 -45
- package/dist-lib/lib/core/project-keys.d.ts +13 -0
- package/dist-lib/lib/core/project-keys.js +56 -0
- package/dist-lib/lib/core/prompt-history.js +4 -4
- package/dist-lib/lib/core/protocol-pairing.d.ts +11 -0
- package/dist-lib/lib/core/protocol-pairing.js +22 -0
- package/dist-lib/lib/core/public-mesh.d.ts +26 -0
- package/dist-lib/lib/core/public-mesh.js +94 -0
- package/dist-lib/lib/core/purge.d.ts +3 -0
- package/dist-lib/lib/core/purge.js +7 -0
- package/dist-lib/lib/core/run-driver.d.ts +17 -0
- package/dist-lib/lib/core/run-driver.js +20 -0
- package/dist-lib/lib/core/run-requests.d.ts +11 -0
- package/dist-lib/lib/core/run-requests.js +58 -0
- package/dist-lib/lib/core/send.d.ts +2 -0
- package/dist-lib/lib/core/send.js +17 -4
- package/dist-lib/lib/core/senders.d.ts +19 -0
- package/dist-lib/lib/core/senders.js +45 -0
- package/dist-lib/lib/core/shared-agents.d.ts +40 -0
- package/dist-lib/lib/core/shared-agents.js +283 -0
- package/dist-lib/lib/core/skill-secrets.d.ts +10 -16
- package/dist-lib/lib/core/skill-secrets.js +90 -72
- package/dist-lib/lib/core/sync/background.d.ts +35 -0
- package/dist-lib/lib/core/sync/background.js +85 -0
- package/dist-lib/lib/core/sync/changed.d.ts +20 -0
- package/dist-lib/lib/core/sync/changed.js +7 -0
- package/dist-lib/lib/core/sync/devices.js +6 -3
- package/dist-lib/lib/core/sync/files.d.ts +1 -0
- package/dist-lib/lib/core/sync/files.js +50 -72
- package/dist-lib/lib/core/sync/index.d.ts +1 -1
- package/dist-lib/lib/core/sync/index.js +164 -81
- package/dist-lib/lib/core/sync/view-updates.d.ts +33 -0
- package/dist-lib/lib/core/sync/view-updates.js +133 -0
- package/dist-lib/lib/core/sync-status.js +3 -3
- package/dist-lib/lib/core/system-info.d.ts +2 -2
- package/dist-lib/lib/core/system-info.js +10 -11
- package/dist-lib/lib/core/tool-configs.d.ts +5 -3
- package/dist-lib/lib/core/tool-configs.js +11 -11
- package/dist-lib/lib/core/transcript-visibility.d.ts +5 -5
- package/dist-lib/lib/core/transcript-visibility.js +27 -31
- package/dist-lib/lib/core/upgrade-agents.d.ts +11 -0
- package/dist-lib/lib/core/upgrade-agents.js +78 -0
- package/dist-lib/lib/core/video/attachment.d.ts +1 -0
- package/dist-lib/lib/core/video/attachment.js +7 -0
- package/dist-lib/lib/core/video/digest.d.ts +16 -0
- package/dist-lib/lib/core/video/digest.js +57 -0
- package/dist-lib/lib/core/video/frames.d.ts +32 -0
- package/dist-lib/lib/core/video/frames.js +224 -0
- package/dist-lib/lib/core/workspace-upgrade.d.ts +22 -0
- package/dist-lib/lib/core/workspace-upgrade.js +50 -0
- package/dist-lib/lib/core.d.ts +303 -84
- package/dist-lib/lib/core.js +3952 -1511
- package/dist-lib/lib/credential-pusher.d.ts +17 -7
- package/dist-lib/lib/credential-pusher.js +111 -62
- package/dist-lib/lib/custom-model-pusher.d.ts +2 -4
- package/dist-lib/lib/custom-model-task.d.ts +2 -1
- package/dist-lib/lib/custom-model-task.js +2 -2
- package/dist-lib/lib/device-tree.d.ts +18 -0
- package/dist-lib/lib/device-tree.js +32 -0
- package/dist-lib/lib/diagnostics.d.ts +7 -0
- package/dist-lib/lib/diagnostics.js +22 -0
- package/dist-lib/lib/doctor/checks.d.ts +15 -0
- package/dist-lib/lib/doctor/checks.js +272 -0
- package/dist-lib/lib/doctor/redact.d.ts +4 -0
- package/dist-lib/lib/doctor/redact.js +31 -0
- package/dist-lib/lib/doctor/render.d.ts +8 -0
- package/dist-lib/lib/doctor/render.js +83 -0
- package/dist-lib/lib/doctor/tree.d.ts +5 -0
- package/dist-lib/lib/doctor/tree.js +167 -0
- package/dist-lib/lib/downloads-allowed-pusher.d.ts +8 -0
- package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
- package/dist-lib/lib/errors.d.ts +794 -0
- package/dist-lib/lib/errors.js +728 -0
- package/dist-lib/lib/fs-drive-bridge.d.ts +8 -1
- package/dist-lib/lib/fs-drive-bridge.js +312 -31
- package/dist-lib/lib/guards.d.ts +1 -0
- package/dist-lib/lib/guards.js +4 -0
- package/dist-lib/lib/inference-calls.d.ts +1 -2
- package/dist-lib/lib/inference-calls.js +2 -10
- package/dist-lib/lib/integrations/setup-routes.js +7 -5
- package/dist-lib/lib/knowledge-search.d.ts +1 -1
- package/dist-lib/lib/knowledge-search.js +5 -51
- package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
- package/dist-lib/lib/kv-cache-reclaim.js +30 -0
- package/dist-lib/lib/kv-cache-retention.d.ts +35 -0
- package/dist-lib/lib/kv-cache-retention.js +98 -0
- package/dist-lib/lib/latest-frame.d.ts +10 -0
- package/dist-lib/lib/latest-frame.js +48 -0
- package/dist-lib/lib/log.d.ts +7 -6
- package/dist-lib/lib/log.js +14 -7
- package/dist-lib/lib/logger-type.d.ts +2 -5
- package/dist-lib/lib/mesh-scoped-engine.js +42 -2
- package/dist-lib/lib/model-download-status.d.ts +3 -3
- package/dist-lib/lib/model-download.d.ts +16 -12
- package/dist-lib/lib/model-download.js +90 -34
- package/dist-lib/lib/model-idle-pusher.d.ts +2 -4
- package/dist-lib/lib/model-memory-cap.d.ts +27 -0
- package/dist-lib/lib/model-memory-cap.js +69 -0
- package/dist-lib/lib/models-watch.d.ts +1 -1
- package/dist-lib/lib/models-watch.js +4 -4
- package/dist-lib/lib/oauth/browser-connect.js +3 -2
- package/dist-lib/lib/oauth/flow-core.js +5 -3
- package/dist-lib/lib/oauth/flow.js +1 -1
- package/dist-lib/lib/oauth/loopback.d.ts +1 -0
- package/dist-lib/lib/oauth/loopback.js +57 -12
- package/dist-lib/lib/oauth/providers.js +4 -2
- package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
- package/dist-lib/lib/oauth/register.js +35 -19
- package/dist-lib/lib/oauth/request.d.ts +10 -0
- package/dist-lib/lib/oauth/request.js +22 -0
- package/dist-lib/lib/oauth/resolve.js +13 -5
- package/dist-lib/lib/oauth/token-exchange-core.js +31 -33
- package/dist-lib/lib/oauth/types.d.ts +3 -0
- package/dist-lib/lib/oauth.d.ts +2 -1
- package/dist-lib/lib/oauth.js +1 -0
- package/dist-lib/lib/persistent-dir.d.ts +1 -0
- package/dist-lib/lib/persistent-dir.js +9 -0
- package/dist-lib/lib/probe-rounds.d.ts +3 -0
- package/dist-lib/lib/probe-rounds.js +5 -0
- package/dist-lib/lib/reconnecting-watch.js +19 -11
- package/dist-lib/lib/run-input.d.ts +14 -0
- package/dist-lib/lib/run-input.js +33 -0
- package/dist-lib/lib/run-notices.d.ts +5 -0
- package/dist-lib/lib/run-notices.js +13 -2
- package/dist-lib/lib/run-replay.d.ts +44 -0
- package/dist-lib/lib/run-replay.js +158 -0
- package/dist-lib/lib/run-transcript.d.ts +11 -12
- package/dist-lib/lib/run-transcript.js +16 -18
- package/dist-lib/lib/suspend-lifecycle.js +15 -5
- package/dist-lib/lib/task.js +6 -4
- package/dist-lib/lib/test-env.d.ts +1 -0
- package/dist-lib/lib/test-env.js +4 -0
- package/dist-lib/schema/build.js +66 -55
- package/dist-lib/schema/config.d.ts +1 -1
- package/dist-lib/schema/config.js +1 -1
- package/dist-lib/schema/content.d.ts +5 -1
- package/dist-lib/schema/content.js +60 -12
- package/dist-lib/schema/engine.d.ts +1 -0
- package/dist-lib/schema/engine.js +780 -155
- package/dist-lib/schema/field-order.js +8 -1
- package/dist-lib/schema/indexes.d.ts +7 -0
- package/dist-lib/schema/indexes.js +48 -3
- package/dist-lib/schema/local.js +80 -5
- package/dist-lib/schema/qvac.d.ts +4 -1
- package/dist-lib/schema/qvac.js +474 -14
- package/dist-lib/spec/bind.js +212 -48
- package/dist-lib/spec/calls.js +46 -5
- package/dist-lib/spec/capabilities.d.ts +100 -18
- package/dist-lib/spec/config/hyperconf/index.js +1 -1
- package/dist-lib/spec/hrpc/hrpc.json +739 -206
- package/dist-lib/spec/hrpc/index.js +844 -345
- package/dist-lib/spec/hrpc/messages.js +3906 -1749
- package/dist-lib/spec/hyperschema/index.js +3906 -1749
- package/dist-lib/spec/hyperschema/schema.json +4809 -3119
- package/dist-lib/spec/hyperschema/types.d.ts +482 -62
- package/dist-lib/spec/local/hyperdb/db.json +95 -1
- package/dist-lib/spec/local/hyperdb/index.js +459 -2
- package/dist-lib/spec/local/hyperdb/messages.js +1565 -487
- package/dist-lib/spec/local/hyperschema/index.js +573 -16
- package/dist-lib/spec/local/hyperschema/schema.json +354 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +85 -2
- package/dist-lib/spec/mesh/hyperdb/db.json +577 -7
- package/dist-lib/spec/mesh/hyperdb/index.js +2224 -6
- package/dist-lib/spec/mesh/hyperdb/messages.js +6476 -1266
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +193 -1
- package/dist-lib/spec/mesh/hyperdispatch/index.js +545 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +3655 -265
- package/dist-lib/spec/mesh/hyperschema/index.js +3655 -265
- package/dist-lib/spec/mesh/hyperschema/schema.json +1974 -19
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +498 -6
- package/package.json +64 -43
- package/worker/assistant-sidecar.mjs +18 -2
- package/worker/stow-entry.mjs +2 -0
- package/dist-lib/lib/core/contract/compute.d.ts +0 -11
- package/dist-lib/lib/core/contract/compute.js +0 -48
- package/dist-lib/lib/core/device-invites.d.ts +0 -11
- package/dist-lib/lib/core/device-invites.js +0 -63
- package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
- package/dist-lib/lib/core/files/processing/settle.js +0 -86
- package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
- package/dist-lib/lib/core/grant-expiry.js +0 -66
- package/dist-lib/lib/core/handoff-channel.d.ts +0 -47
- package/dist-lib/lib/core/handoff-channel.js +0 -123
- package/dist-lib/lib/core/harness-loan.d.ts +0 -29
- package/dist-lib/lib/core/host-run-log-store.d.ts +0 -114
- package/dist-lib/lib/core/host-run-log-store.js +0 -976
- package/dist-lib/lib/core/hosted-agents.d.ts +0 -38
- package/dist-lib/lib/core/hosted-agents.js +0 -140
- package/dist-lib/lib/core/inference-calls-handoff.d.ts +0 -20
- package/dist-lib/lib/core/inference-calls-handoff.js +0 -37
- package/dist-lib/lib/core/inference-calls-receiver.d.ts +0 -14
- package/dist-lib/lib/core/inference-calls-receiver.js +0 -42
- package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -25
- package/dist-lib/lib/core/mesh-seed-handoff.js +0 -52
- package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
- package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
- package/dist-lib/lib/core/secret-handoff.d.ts +0 -17
- package/dist-lib/lib/core/secret-handoff.js +0 -29
- package/dist-lib/lib/core/skill-overlay-handoff.d.ts +0 -25
- package/dist-lib/lib/core/skill-overlay-handoff.js +0 -50
- package/dist-lib/lib/core/skill-overlay-receiver.d.ts +0 -8
- package/dist-lib/lib/core/skill-overlay-receiver.js +0 -23
- package/dist-lib/lib/image-cache-dir.d.ts +0 -1
- package/dist-lib/lib/image-cache-dir.js +0 -5
- package/dist-lib/lib/log-levels.d.ts +0 -7
- package/dist-lib/lib/log-levels.js +0 -1
- package/dist-lib/lib/logger.d.ts +0 -5
- package/dist-lib/lib/logger.js +0 -18
- package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
- package/dist-lib/lib/oauth/loopback.stub.js +0 -6
- package/tui/README.md +0 -142
- package/tui/app.mjs +0 -6939
- package/tui/boot.mjs +0 -111
- package/tui/index.mjs +0 -53
- package/tui/lib/boot-timing.mjs +0 -41
- package/tui/lib/busy-words.mjs +0 -116
- package/tui/lib/export.mjs +0 -194
- package/tui/lib/knowledge-command.mjs +0 -190
- package/tui/lib/knowledge-import.mjs +0 -171
- package/tui/lib/render.mjs +0 -196
- package/tui/lib/save-file.mjs +0 -88
- package/tui/lib/select.mjs +0 -81
- package/tui/lib/selection.mjs +0 -18
- package/tui/lib/setup.mjs +0 -101
- package/tui/lib/sidebar.mjs +0 -63
- package/tui/lib/splash-art.mjs +0 -107
- package/tui/lib/splash.mjs +0 -18
- package/tui/lib/upload-frames.mjs +0 -9
- package/tui/lib/usage.mjs +0 -59
- package/tui/lib/wrap.mjs +0 -143
- package/tui/main.mjs +0 -264
|
@@ -5,59 +5,98 @@ import fs from 'fs';
|
|
|
5
5
|
import crypto from 'hypercore-crypto';
|
|
6
6
|
import os from 'os';
|
|
7
7
|
import path from 'path';
|
|
8
|
-
import { requestFromBoot } from '@qvac/harness/run-log';
|
|
9
8
|
import { isMobilePlatform, mediaModelNameFor, modelsForAgent, runnableModelName } from '@qvac/harness/lib/models/model-catalog.ts';
|
|
10
|
-
import { connectHarness } from '@qvac/harness';
|
|
11
9
|
import * as z from 'zod';
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import { SKILL_STATE } from '@qvac/harness/lib/tools/skills/skill-state.ts';
|
|
10
|
+
import { createHostRunLogAdapter, loggedMessages } from './assistant/host-run-log.js';
|
|
11
|
+
import { SKILL_STATE } from '@qvac/harness/skill-state';
|
|
15
12
|
import { toolsetEnabled } from '@qvac/harness/lib/tools/toolsets.ts';
|
|
16
13
|
import { RAG_CITATION_LINE } from '@qvac/harness/lib/prompt.ts';
|
|
17
14
|
import { MAX_RAG_SOURCE_URL_CHARS, MAX_RAG_SOURCES_PER_CHUNK, RAG_SOURCE_EXCERPT_CHARS } from '../schema/qvac.js';
|
|
18
|
-
import {
|
|
15
|
+
import { historyBranches, isPromptChunk } from './core/prompt-history.js';
|
|
16
|
+
import { drivenBy, handedOff, offeredTo } from './core/run-driver.js';
|
|
19
17
|
import ReadyGuard from 'ready-guard';
|
|
20
18
|
import ReadyResource from 'ready-resource';
|
|
21
19
|
import { local } from '../spec/index.js';
|
|
22
|
-
import { isMissingMesh } from './api/current-mesh.js';
|
|
23
20
|
import { meshScoped } from './mesh-scoped-engine.js';
|
|
24
21
|
import { buildTitlePrompt, deriveChatTitle, isDefaultChatTitle, parseTitleResponse } from './chat-title.js';
|
|
22
|
+
import { buildSuggestionPrompt, parseSuggestionResponse } from './chat-suggestion.js';
|
|
25
23
|
import { matchApprovalRule } from './core/approval-rules.js';
|
|
26
24
|
import { isAudioAttachment } from './core/audio-attachment.js';
|
|
27
|
-
import { attachTurnDocumentIds, formatRagContext, chooseRagNotice, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, createRagSourceTagFilter, citedByPhraseOverlap, formatFullDocumentContext, isFullTextHit, isTruncatedFullTextHit, sourcesFromHits, WEB_TOOLS, webSourcesFromToolResult, withoutDataChannel } from './core/files/index.js';
|
|
28
|
-
import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason } from './core/document-attachment.js';
|
|
25
|
+
import { attachTurnDocumentIds, formatRagContext, chooseRagNotice, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, createRagSourceTagFilter, citedByPhraseOverlap, formatFullDocumentContext, fullTextAttachmentId, isFullTextHit, isTruncatedFullTextHit, sourcesFromHits, WEB_TOOLS, webSourcesFromToolResult, withoutDataChannel } from './core/files/index.js';
|
|
26
|
+
import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason, PDF_PAGES_SOURCE_LINE, pdfPagesCutNotice, pdfPagesShownNotice } from './core/document-attachment.js';
|
|
29
27
|
import { imageCacheKey, imageCacheName, sweepImageCache } from './core/image/cache-sweep.js';
|
|
30
28
|
import { IMAGE_MIMES, normalizeImageForModel } from './core/image/normalize.js';
|
|
31
|
-
import {
|
|
32
|
-
import { summarizeRuns } from './diagnostics.js';
|
|
29
|
+
import { createPdfRasterizer } from './core/pdf/rasterize.js';
|
|
30
|
+
import { recordBackends, summarizeRuns } from './diagnostics.js';
|
|
33
31
|
import { resolveFsGrants } from './fs-drive-bridge.js';
|
|
34
32
|
import { InferenceCallStore } from './inference-calls.js';
|
|
33
|
+
import { LOCAL_HOLDER, RunReplayStore, trackSentRun } from './run-replay.js';
|
|
34
|
+
import { RunInputQueue, STEERED } from './run-input.js';
|
|
35
35
|
import { fsGrantKey, fsWriteGrantKey } from './fs-grant-keys.js';
|
|
36
36
|
import { fsGrantsFrom, grantedDirsFrom } from './fs-grants.js';
|
|
37
|
-
import {
|
|
37
|
+
import { persistentDir } from './persistent-dir.js';
|
|
38
38
|
import { ModelCatalogRevisionHub } from './model-catalog-revision.js';
|
|
39
|
-
import { agentRekeyFingerprint, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
|
|
40
|
-
import {
|
|
39
|
+
import { agentRekeyFingerprint, agentSwitched, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
|
|
40
|
+
import { KvCacheRetention } from './kv-cache-retention.js';
|
|
41
|
+
import { KV_CACHE_RETENTION_KEY } from './constants.js';
|
|
42
|
+
import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_TURNS, COMPACTION_TURN_PREFIX, isContextOverflowFailure, nearContextLimit, compactionBoundary, foldsThrough, lastCompactedThroughId, parseCompactionSummary } from './compaction.js';
|
|
41
43
|
import { ModelDownloadReconciler, musicModelChoiceOf } from './model-download.js';
|
|
42
44
|
import { AbortedError, raceAbort } from '@qvac/harness/race-abort';
|
|
43
45
|
import { RunTranscript } from './run-transcript.js';
|
|
46
|
+
import { OwnerRounds, listRoundChunks } from './assistant/model-round.js';
|
|
47
|
+
import { RoundHost } from './assistant/round-host.js';
|
|
44
48
|
import { searchKnowledge } from './knowledge-search.js';
|
|
45
49
|
import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
|
|
50
|
+
import { unref } from './unref.js';
|
|
46
51
|
import { encodeAgentSkillReadiness } from './skill-readiness.js';
|
|
47
|
-
import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
|
|
48
|
-
import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, nameSenders } from './assistant/message.js';
|
|
52
|
+
import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, syncedSkills, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, heldLoadModel, droppedHeldLoadModel, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, customModelReportEntries, toolReportEntry } from './assistant/harness.js';
|
|
53
|
+
import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, askedIn, claimNewImages, earlierImages, nameSenders, restoreSentContent, hiddenVideoNotice, unreadVideoNotice, unsyncedVideoNotice, VIDEO_NO_AUDIO, VIDEO_SILENT, videoAnnotation, videoTranscriptBlock } from './assistant/message.js';
|
|
54
|
+
import { frameTimes, probeVideo, sampleFrames } from './core/video/frames.js';
|
|
55
|
+
import { packVideoDigest, parseVideoDigestHeader, unpackVideoDigest, VIDEO_DIGEST_MIME, videoDigestFileName } from './core/video/digest.js';
|
|
56
|
+
import { VIDEO_FRAME_EDGE, videoFrameCount } from './assistant/video-budget.js';
|
|
49
57
|
import { drainBody, raceInactivity } from './assistant/fetch-body.js';
|
|
50
|
-
import {
|
|
58
|
+
import { pdfImageRoom } from './assistant/pdf-budget.js';
|
|
59
|
+
import { packPdfSelection, parsePdfSelection, PDF_PAGES_MIME, pdfSelectionFileName, selectPdfPages } from './assistant/pdf-pages.js';
|
|
60
|
+
import { effectiveCtxSize } from './api/effective-ctx-size.js';
|
|
61
|
+
import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, foldableView, historyFromChunks, listHistoryChunks, replayFromChunks, runPromptView, runView, senderOf, staleToolRunIds, viewOutgrewCache } from './assistant/history.js';
|
|
51
62
|
import { errorMessage, isRecord } from './guards.js';
|
|
52
|
-
|
|
63
|
+
import { CoreError, isCoreError } from './errors.js';
|
|
64
|
+
export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, syncedSkills, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
|
|
53
65
|
export {} from './assistant/skill-encode.js';
|
|
54
|
-
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders } from './assistant/message.js';
|
|
55
|
-
export {
|
|
56
|
-
const NO_SENDERS = new Map();
|
|
66
|
+
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders, restoreSentContent } from './assistant/message.js';
|
|
67
|
+
export { historyFromChunks, listHistoryChunks, staleToolRunIds } from './assistant/history.js';
|
|
57
68
|
const DEFAULT_PROFILE = 'mid';
|
|
58
69
|
const QvacState = local.getEnum('@local/qvac-state');
|
|
59
70
|
const FINAL_FLUSH_MS = 10_000;
|
|
60
71
|
const UPLOAD_FRAME_BYTES = 64 * 1024;
|
|
72
|
+
const RUN_IDLE_TIMEOUT_MS = 15 * 60_000;
|
|
73
|
+
const MODEL_RELOAD_DELAY_MS = 2_500;
|
|
74
|
+
function idleWatch(controller, ms) {
|
|
75
|
+
let last = 0;
|
|
76
|
+
let parked = 0;
|
|
77
|
+
const timer = setInterval(() => {
|
|
78
|
+
if (last && !parked && Date.now() - last >= ms) {
|
|
79
|
+
controller.abort(RUN_ABORT_REASONS.stalled);
|
|
80
|
+
}
|
|
81
|
+
}, Math.min(ms, 30_000));
|
|
82
|
+
unref(timer);
|
|
83
|
+
return {
|
|
84
|
+
touch: () => {
|
|
85
|
+
last = Date.now();
|
|
86
|
+
},
|
|
87
|
+
parks: (fn) => async (...args) => {
|
|
88
|
+
parked++;
|
|
89
|
+
try {
|
|
90
|
+
return await fn(...args);
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
parked--;
|
|
94
|
+
last = Date.now();
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
stop: () => clearInterval(timer)
|
|
98
|
+
};
|
|
99
|
+
}
|
|
61
100
|
function settledWithin(write, ms) {
|
|
62
101
|
let timer;
|
|
63
102
|
const expired = new Promise((resolve) => {
|
|
@@ -71,7 +110,18 @@ function isDeniedResult(result) {
|
|
|
71
110
|
return false;
|
|
72
111
|
return 'denied' in result && result.denied === true;
|
|
73
112
|
}
|
|
74
|
-
const
|
|
113
|
+
const TITLE_PROBE = {
|
|
114
|
+
kind: 'title',
|
|
115
|
+
name: 'chat_title',
|
|
116
|
+
field: 'title',
|
|
117
|
+
runId: 'chat-title'
|
|
118
|
+
};
|
|
119
|
+
const SUGGESTION_PROBE = {
|
|
120
|
+
kind: 'suggestion',
|
|
121
|
+
name: 'next_message',
|
|
122
|
+
field: 'prompt',
|
|
123
|
+
runId: 'chat-suggestion'
|
|
124
|
+
};
|
|
75
125
|
const RunOccupancySchema = z.object({
|
|
76
126
|
cacheTokens: z.number().optional().catch(undefined),
|
|
77
127
|
ctxSize: z.number().optional().catch(undefined)
|
|
@@ -79,6 +129,20 @@ const RunOccupancySchema = z.object({
|
|
|
79
129
|
export const CAPTURE_INFERENCE_CALLS_KEY = 'inspector.captureInferenceCalls';
|
|
80
130
|
export const CAPTURE_ON = 'on';
|
|
81
131
|
export const CAPTURE_OFF = 'off';
|
|
132
|
+
export const SUGGEST_PROMPTS_KEY = 'assistant.suggestPrompts';
|
|
133
|
+
export const SUGGEST_PROMPTS_ON = 'on';
|
|
134
|
+
const SUGGESTION_SUPERSEDED = "superseded by the chat's next run";
|
|
135
|
+
const NO_PDF_PAGES = { selection: { pageCount: 0, shots: [] }, names: [], labels: [] };
|
|
136
|
+
function pdfNotices(fileName, pages, wholeText) {
|
|
137
|
+
const { cutAt, pageCount } = pages.selection;
|
|
138
|
+
const shown = pages.names.length;
|
|
139
|
+
return [
|
|
140
|
+
...(shown ? [pdfPagesShownNotice(fileName, pages.labels)] : []),
|
|
141
|
+
...(cutAt === undefined
|
|
142
|
+
? []
|
|
143
|
+
: [pdfPagesCutNotice({ fileName, cutAt, pageCount, shown, wholeText })])
|
|
144
|
+
];
|
|
145
|
+
}
|
|
82
146
|
function attachPages(turn, files) {
|
|
83
147
|
if (!files.length)
|
|
84
148
|
return;
|
|
@@ -87,8 +151,40 @@ function attachPages(turn, files) {
|
|
|
87
151
|
function noBytes() {
|
|
88
152
|
return Promise.resolve(null);
|
|
89
153
|
}
|
|
90
|
-
function
|
|
91
|
-
return imageCacheName({ blobId:
|
|
154
|
+
function videoFrameCacheName(video, count, frame) {
|
|
155
|
+
return imageCacheName({ blobId: video.blobId, fileName: `frames.${count}.f${frame}.jpg` });
|
|
156
|
+
}
|
|
157
|
+
function videoHeaderCacheName(digestBlobId) {
|
|
158
|
+
return imageCacheName({ blobId: digestBlobId, fileName: 'frames.json' });
|
|
159
|
+
}
|
|
160
|
+
function videoAudioBlock(fileName, { audio, transcript }) {
|
|
161
|
+
if (audio === 'none') {
|
|
162
|
+
return VIDEO_NO_AUDIO;
|
|
163
|
+
}
|
|
164
|
+
if (audio === 'silent') {
|
|
165
|
+
return VIDEO_SILENT;
|
|
166
|
+
}
|
|
167
|
+
if (audio === 'speech' && transcript) {
|
|
168
|
+
return videoTranscriptBlock(transcript);
|
|
169
|
+
}
|
|
170
|
+
return unheardAudioNotice(`The audio of the attached video "${fileName}"`, 'transcription failed');
|
|
171
|
+
}
|
|
172
|
+
async function cachedPdfPages(selection, { dir, pdf, pinned }) {
|
|
173
|
+
const names = selection.shots.map((shot) => pdfShotCacheName(pdf, shot));
|
|
174
|
+
for (const name of names) {
|
|
175
|
+
pinned.add(name);
|
|
176
|
+
}
|
|
177
|
+
const files = names.map((name) => path.join(dir, name));
|
|
178
|
+
const cached = await Promise.all(files.map((file) => cacheOrTouch(file, noBytes)));
|
|
179
|
+
return cached.every(Boolean)
|
|
180
|
+
? { selection, names, labels: selection.shots.map((shot) => shot.label) }
|
|
181
|
+
: null;
|
|
182
|
+
}
|
|
183
|
+
function pdfShotCacheName(pdf, shot) {
|
|
184
|
+
const box = shot.clip
|
|
185
|
+
? `.c${[shot.clip.x, shot.clip.y, shot.clip.width, shot.clip.height].map(Math.round).join('_')}`
|
|
186
|
+
: '';
|
|
187
|
+
return imageCacheName({ blobId: pdf.blobId, fileName: `${pdf.fileName}.p${shot.page}${box}.jpg` });
|
|
92
188
|
}
|
|
93
189
|
async function cacheOrTouch(file, produce) {
|
|
94
190
|
const cached = await fs.promises.stat(file).catch(noop);
|
|
@@ -110,15 +206,39 @@ const BLOB_READ_TIMEOUT_MS = 15_000;
|
|
|
110
206
|
const SUSPENDED = RUN_ABORT_REASONS.suspended;
|
|
111
207
|
const OWNERSHIP_LOST = RUN_ABORT_REASONS.ownershipLost;
|
|
112
208
|
const RUN_SETTLED = RUN_ABORT_REASONS.runSettled;
|
|
209
|
+
const AGENT_REMOVED = RUN_ABORT_REASONS.agentRemoved;
|
|
113
210
|
const RUNNER_DIED = RUN_ABORT_REASONS.runnerDied;
|
|
114
211
|
const RUNNER_CLOSED = RUN_ABORT_REASONS.runnerClosed;
|
|
115
212
|
const MODEL_UNLOADED = RUN_ABORT_REASONS.modelUnloaded;
|
|
213
|
+
const RUN_PAUSED = 'paused on a saved checkpoint; it resumes from there';
|
|
214
|
+
function recoveryReason(event) {
|
|
215
|
+
const operation = isRecord(event.operation) ? event.operation.name : undefined;
|
|
216
|
+
const call = typeof operation === 'string' ? ` of ${operation}` : '';
|
|
217
|
+
return `the outcome${call} is unknown, so it is not run again: decide before this run continues`;
|
|
218
|
+
}
|
|
219
|
+
function keepsRunLog(capability) {
|
|
220
|
+
return capability?.hostRunLog === true && capability.hostRunLogVersion === 1;
|
|
221
|
+
}
|
|
116
222
|
function isSetUpHere(status) {
|
|
117
223
|
return status === SKILL_STATE.ready || status === SKILL_STATE.incompatibleModel;
|
|
118
224
|
}
|
|
119
225
|
function sameNameList(left, right) {
|
|
120
226
|
return left.length === right.length && left.every((name, index) => name === right[index]);
|
|
121
227
|
}
|
|
228
|
+
function sameLoadSettings(a, b) {
|
|
229
|
+
return ((a.ctxSize ?? 0) === (b.ctxSize ?? 0) &&
|
|
230
|
+
(a.kvCacheK ?? '') === (b.kvCacheK ?? '') &&
|
|
231
|
+
(a.kvCacheV ?? '') === (b.kvCacheV ?? ''));
|
|
232
|
+
}
|
|
233
|
+
function modelLeft(before, after, deviceId) {
|
|
234
|
+
if (!deviceId || !before.modelName || !runsHere(before, deviceId))
|
|
235
|
+
return null;
|
|
236
|
+
const stays = after && runsHere(after, deviceId) && after.modelName === before.modelName;
|
|
237
|
+
return stays ? null : before.modelName;
|
|
238
|
+
}
|
|
239
|
+
function runsHere(agent, deviceId) {
|
|
240
|
+
return (!agent.deletedAt && !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId));
|
|
241
|
+
}
|
|
122
242
|
function withConfigDefaults(agent, config) {
|
|
123
243
|
if (agent.ctxSize || !config?.ctxSize)
|
|
124
244
|
return agent;
|
|
@@ -137,8 +257,6 @@ const WEB_RAG_SOURCE_BOUNDS = { ...RAG_SOURCE_BOUNDS, maxUrlChars: MAX_RAG_SOURC
|
|
|
137
257
|
export class QvacAssistant extends ReadyResource {
|
|
138
258
|
_engine;
|
|
139
259
|
_harness;
|
|
140
|
-
_borrowHarness;
|
|
141
|
-
_borrowed = new Map();
|
|
142
260
|
_log;
|
|
143
261
|
_imageCacheDir;
|
|
144
262
|
_imageCacheMaxBytes;
|
|
@@ -149,6 +267,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
149
267
|
_preloadCurrentAgentModel;
|
|
150
268
|
_drives;
|
|
151
269
|
_blobReadTimeoutMs;
|
|
270
|
+
_runIdleTimeoutMs;
|
|
271
|
+
_modelReloadDelayMs;
|
|
272
|
+
_reloads = new Map();
|
|
273
|
+
_releases = new Map();
|
|
152
274
|
_pdfRasterizer;
|
|
153
275
|
_warmAbort = new AbortController();
|
|
154
276
|
_unloading = new Map();
|
|
@@ -158,8 +280,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
158
280
|
_deviceId;
|
|
159
281
|
_info;
|
|
160
282
|
_lastCapabilities = new Map();
|
|
161
|
-
|
|
162
|
-
|
|
283
|
+
_ownWorkspace = null;
|
|
284
|
+
_listed = [];
|
|
285
|
+
_accountHosted = [];
|
|
163
286
|
_readySkills;
|
|
164
287
|
_recordingReadiness = false;
|
|
165
288
|
_readinessDirty = false;
|
|
@@ -172,6 +295,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
172
295
|
_harnessDiagnostics;
|
|
173
296
|
_harnessDeath;
|
|
174
297
|
_refreshingDiagnostics;
|
|
298
|
+
_backendUse = new Map();
|
|
175
299
|
_meshList;
|
|
176
300
|
_accountFrames;
|
|
177
301
|
_accountMeshId;
|
|
@@ -179,27 +303,39 @@ export class QvacAssistant extends ReadyResource {
|
|
|
179
303
|
_meshListWatch;
|
|
180
304
|
_accountWatch;
|
|
181
305
|
_meshConsumers;
|
|
306
|
+
_servedChats = new Map();
|
|
182
307
|
_runs;
|
|
308
|
+
_steered = new Map();
|
|
309
|
+
_idleWaiters = new Set();
|
|
183
310
|
_seen;
|
|
311
|
+
_parked;
|
|
312
|
+
_handedOff = new Map();
|
|
313
|
+
_backgroundReplays;
|
|
184
314
|
_inferenceCalls;
|
|
315
|
+
_runReplay;
|
|
316
|
+
_roundHost;
|
|
317
|
+
_kvRetention;
|
|
185
318
|
_lifecycle;
|
|
319
|
+
_suggesting = new Map();
|
|
186
320
|
_audioTranscripts;
|
|
187
|
-
|
|
321
|
+
_videoProbes;
|
|
322
|
+
_pdfSelections;
|
|
323
|
+
_mediaRefs;
|
|
188
324
|
_chats;
|
|
325
|
+
_intake = new Map();
|
|
189
326
|
_runImageCacheNames;
|
|
190
327
|
_reconciledCompactions;
|
|
191
|
-
|
|
328
|
+
_knowledgeCaps = new Map();
|
|
192
329
|
_modelCatalog = new ModelCatalogRevisionHub();
|
|
193
330
|
_onHarnessStatus;
|
|
194
331
|
_onModelCatalogEvent;
|
|
195
|
-
constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives,
|
|
332
|
+
constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, runIdleTimeoutMs = RUN_IDLE_TIMEOUT_MS, modelReloadDelayMs = MODEL_RELOAD_DELAY_MS, pdfRasterizer: pdfRasterizerOption } = {}) {
|
|
196
333
|
super();
|
|
197
334
|
this._engine = engine;
|
|
198
335
|
this._harness = harness;
|
|
199
|
-
this._borrowHarness = borrowHarness ?? null;
|
|
200
336
|
this._drives = drives;
|
|
201
337
|
this._log = logger;
|
|
202
|
-
this._imageCacheDir = imageCacheDir ??
|
|
338
|
+
this._imageCacheDir = imageCacheDir ?? persistentDir('image-cache');
|
|
203
339
|
this._imageCacheMaxBytes = imageCacheMaxBytes;
|
|
204
340
|
this._imageCacheMaxAgeMs = imageCacheMaxAgeMs;
|
|
205
341
|
this._downloadRetryMs = downloadRetryMs;
|
|
@@ -207,7 +343,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
207
343
|
this._mobileClass = isMobilePlatform(platform);
|
|
208
344
|
this._preloadCurrentAgentModel = preloadCurrentAgentModel;
|
|
209
345
|
this._blobReadTimeoutMs = blobReadTimeoutMs;
|
|
210
|
-
this.
|
|
346
|
+
this._runIdleTimeoutMs = runIdleTimeoutMs;
|
|
347
|
+
this._modelReloadDelayMs = modelReloadDelayMs;
|
|
348
|
+
this._pdfRasterizer = pdfRasterizerOption ?? createPdfRasterizer(logger);
|
|
349
|
+
const serve = harness.serveModelRound?.bind(harness);
|
|
350
|
+
this._roundHost = serve
|
|
351
|
+
? new RoundHost({
|
|
352
|
+
engine,
|
|
353
|
+
serve,
|
|
354
|
+
deviceId: () => this._requireDeviceId(),
|
|
355
|
+
media: {
|
|
356
|
+
resolve: (sources, served) => this._servedMedia(sources, served),
|
|
357
|
+
release: (id) => this._runImageCacheNames.delete(`round:${id}`)
|
|
358
|
+
},
|
|
359
|
+
log: logger
|
|
360
|
+
})
|
|
361
|
+
: null;
|
|
211
362
|
this._deviceId = null;
|
|
212
363
|
this._info = { toolNames: [], skills: [], supportsImages: false };
|
|
213
364
|
this._readySkills = null;
|
|
@@ -227,14 +378,25 @@ export class QvacAssistant extends ReadyResource {
|
|
|
227
378
|
this._meshConsumers = new Map();
|
|
228
379
|
this._runs = new Map();
|
|
229
380
|
this._seen = new Map();
|
|
230
|
-
this.
|
|
381
|
+
this._parked = new Map();
|
|
382
|
+
this._backgroundReplays = new Set();
|
|
383
|
+
this._inferenceCalls = new InferenceCallStore(inferenceCallsDir ?? persistentDir('inspector'));
|
|
384
|
+
this._runReplay = new RunReplayStore(runReplayDir ?? persistentDir('run-replay'));
|
|
385
|
+
this._kvRetention = new KvCacheRetention(engine, {
|
|
386
|
+
active: () => !this.closing && !this.suspended,
|
|
387
|
+
running: (chatId) => [...this._runs.values()].some((run) => run.chatId === chatId),
|
|
388
|
+
deleteChatKvCache: (chatId) => this._deleteChatKvCache(chatId, 'idle past retention'),
|
|
389
|
+
defaultMs: kvCacheRetentionMs,
|
|
390
|
+
logger
|
|
391
|
+
});
|
|
231
392
|
this._lifecycle = new AbortController();
|
|
232
393
|
this._audioTranscripts = new Map();
|
|
233
|
-
this.
|
|
394
|
+
this._videoProbes = new Map();
|
|
395
|
+
this._pdfSelections = new Map();
|
|
396
|
+
this._mediaRefs = new Map();
|
|
234
397
|
this._chats = new Map();
|
|
235
398
|
this._runImageCacheNames = new Map();
|
|
236
399
|
this._reconciledCompactions = new Map();
|
|
237
|
-
this._chatAttribution = new Map();
|
|
238
400
|
this._onHarnessStatus = (status) => {
|
|
239
401
|
this._log?.warn('assistant', 'harness status', status);
|
|
240
402
|
if (harnessDied(status)) {
|
|
@@ -257,6 +419,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
257
419
|
if (progress) {
|
|
258
420
|
this._downloads?.observeImageProgress(progress);
|
|
259
421
|
}
|
|
422
|
+
const held = heldLoadModel(state);
|
|
423
|
+
if (held) {
|
|
424
|
+
this._downloads?.observeHeldLoad(held);
|
|
425
|
+
}
|
|
426
|
+
const dropped = droppedHeldLoadModel(state);
|
|
427
|
+
if (dropped) {
|
|
428
|
+
this._downloads?.observeDroppedHeldLoad(dropped);
|
|
429
|
+
}
|
|
260
430
|
};
|
|
261
431
|
}
|
|
262
432
|
_requireDeviceId() {
|
|
@@ -274,6 +444,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
274
444
|
this._harness.on?.('status', this._onHarnessStatus);
|
|
275
445
|
this._harness.on?.('model', this._onModelCatalogEvent);
|
|
276
446
|
this._info = await this._describeHarness();
|
|
447
|
+
await this._clearPreReplayCaches();
|
|
277
448
|
await this._clearPersistedPauses();
|
|
278
449
|
this._downloads = new ModelDownloadReconciler({
|
|
279
450
|
engine: this._engine,
|
|
@@ -292,6 +463,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
292
463
|
this._downloads.start();
|
|
293
464
|
this._meshListWatch.start();
|
|
294
465
|
this._accountWatch.start();
|
|
466
|
+
this._kvRetention.start(() => this._meshConsumers.keys());
|
|
295
467
|
void this._bootOnline();
|
|
296
468
|
this._triggerModelWarm();
|
|
297
469
|
void this.onlineReady().then(() => this._triggerModelWarm(), noop);
|
|
@@ -501,7 +673,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
501
673
|
assistant: summarizeRuns(runs, this.suspended),
|
|
502
674
|
platform: this._platform,
|
|
503
675
|
mobileClass: this._mobileClass,
|
|
504
|
-
memoryBudget: this._capability?.memoryBudget ?? null
|
|
676
|
+
memoryBudget: this._capability?.memoryBudget ?? null,
|
|
677
|
+
backendUse: Object.fromEntries([...this._backendUse].map(([tool, uses]) => [tool, [...uses]]))
|
|
505
678
|
};
|
|
506
679
|
}
|
|
507
680
|
async _refreshHarnessDiagnostics() {
|
|
@@ -528,6 +701,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
528
701
|
}
|
|
529
702
|
const installedModelNames = models.map((model) => model.name);
|
|
530
703
|
const cachedModelNames = models.filter((model) => model.isCached).map((model) => model.name);
|
|
704
|
+
const customModels = customModelReportEntries(models);
|
|
531
705
|
let toolCatalog = null;
|
|
532
706
|
if (this._harness.tools) {
|
|
533
707
|
try {
|
|
@@ -543,30 +717,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
543
717
|
installedModelNames,
|
|
544
718
|
cachedModelNames,
|
|
545
719
|
this._readySkills,
|
|
546
|
-
toolCatalog
|
|
720
|
+
toolCatalog,
|
|
721
|
+
customModels
|
|
547
722
|
]);
|
|
548
723
|
const meshId = this._accountMeshId;
|
|
549
724
|
if (!meshId) {
|
|
550
725
|
return;
|
|
551
726
|
}
|
|
552
|
-
|
|
553
|
-
const report = {
|
|
727
|
+
await this._reportInto(meshId, key, {
|
|
554
728
|
installedToolNames: this._info.toolNames,
|
|
555
729
|
installedSkillNames: this._info.skills,
|
|
556
730
|
installedModelNames,
|
|
557
731
|
cachedModelNames,
|
|
558
|
-
readySkillNames:
|
|
559
|
-
toolCatalog: toolCatalog ?? undefined
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
if (!readySkillNames) {
|
|
563
|
-
return;
|
|
564
|
-
}
|
|
565
|
-
for (const [shared, hosted] of this._hostedIn) {
|
|
566
|
-
if (shared === meshId)
|
|
567
|
-
continue;
|
|
568
|
-
await this._reportInto(shared, JSON.stringify([readySkillNames, hosted]), report);
|
|
569
|
-
}
|
|
732
|
+
readySkillNames: this._readySkills ?? undefined,
|
|
733
|
+
toolCatalog: toolCatalog ?? undefined,
|
|
734
|
+
customModels
|
|
735
|
+
});
|
|
570
736
|
}
|
|
571
737
|
async _reportInto(meshId, key, report) {
|
|
572
738
|
if (this._lastCapabilities.get(meshId) === key)
|
|
@@ -576,7 +742,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
576
742
|
this._lastCapabilities.set(meshId, key);
|
|
577
743
|
}
|
|
578
744
|
catch (err) {
|
|
579
|
-
if (
|
|
745
|
+
if (isCoreError(err, 'MESH_NOT_FOUND') || isCoreError(err, 'MESH_STILL_JOINING')) {
|
|
580
746
|
return;
|
|
581
747
|
}
|
|
582
748
|
this._log?.error('assistant', 'saveDeviceCapabilities failed', err);
|
|
@@ -584,22 +750,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
584
750
|
}
|
|
585
751
|
_followHosted(meshId, agents) {
|
|
586
752
|
const deviceId = this._deviceId;
|
|
587
|
-
if (!deviceId ||
|
|
753
|
+
if (!deviceId || meshId !== this._accountMeshId) {
|
|
588
754
|
return;
|
|
589
755
|
}
|
|
590
756
|
const hosted = agents
|
|
591
757
|
.filter((agent) => !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId))
|
|
592
758
|
.map((agent) => agent.id)
|
|
593
759
|
.sort();
|
|
594
|
-
if (sameNameList(this.
|
|
760
|
+
if (sameNameList(this._accountHosted, hosted)) {
|
|
595
761
|
return;
|
|
596
762
|
}
|
|
597
|
-
|
|
598
|
-
this._hostedIn.set(meshId, hosted);
|
|
599
|
-
}
|
|
600
|
-
else {
|
|
601
|
-
this._hostedIn.delete(meshId);
|
|
602
|
-
}
|
|
763
|
+
this._accountHosted = hosted;
|
|
603
764
|
void this._reportHosted().catch(noop);
|
|
604
765
|
}
|
|
605
766
|
async _reportHosted() {
|
|
@@ -675,10 +836,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
675
836
|
}
|
|
676
837
|
async _close() {
|
|
677
838
|
this._stopMeshIntake();
|
|
839
|
+
this._kvRetention.stop();
|
|
678
840
|
this._harness.off?.('status', this._onHarnessStatus);
|
|
679
841
|
this._harness.off?.('model', this._onModelCatalogEvent);
|
|
680
842
|
this._warmAbort.abort(RUNNER_CLOSED);
|
|
681
|
-
this.
|
|
843
|
+
this._cancelModelReloads();
|
|
844
|
+
this._onlineBoot.destroy(new CoreError('ASSISTANT_CLOSING'));
|
|
682
845
|
for (const run of this._runs.values()) {
|
|
683
846
|
run.controller.abort(RUNNER_CLOSED);
|
|
684
847
|
}
|
|
@@ -687,11 +850,20 @@ export class QvacAssistant extends ReadyResource {
|
|
|
687
850
|
this._meshList?.destroy();
|
|
688
851
|
this._accountFrames?.destroy();
|
|
689
852
|
this._lifecycle.abort(RUNNER_CLOSED);
|
|
853
|
+
for (const pending of this._suggesting.values()) {
|
|
854
|
+
pending.abort(RUNNER_CLOSED);
|
|
855
|
+
}
|
|
690
856
|
await Promise.allSettled([...this._runs.values()].map((run) => run.done));
|
|
691
857
|
}
|
|
858
|
+
get mobileClass() {
|
|
859
|
+
return this._mobileClass;
|
|
860
|
+
}
|
|
692
861
|
get suspended() {
|
|
693
862
|
return this._state === QvacState.SUSPEND;
|
|
694
863
|
}
|
|
864
|
+
get _backgrounded() {
|
|
865
|
+
return this._state === QvacState.PRESUSPEND || this._state === QvacState.SUSPEND;
|
|
866
|
+
}
|
|
695
867
|
async presuspend() {
|
|
696
868
|
if (this.closing)
|
|
697
869
|
return;
|
|
@@ -708,7 +880,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
708
880
|
}
|
|
709
881
|
this._state = QvacState.SUSPEND;
|
|
710
882
|
this._log?.info('assistant', 'suspending', { runs: this._runs.size });
|
|
883
|
+
this._parked.clear();
|
|
711
884
|
this._stopMeshIntake();
|
|
885
|
+
this._cancelModelReloads();
|
|
712
886
|
await this._downloads?.suspend();
|
|
713
887
|
this._meshList?.destroy();
|
|
714
888
|
this._accountFrames?.destroy();
|
|
@@ -737,6 +911,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
737
911
|
}
|
|
738
912
|
if (this._state === QvacState.PRESUSPEND) {
|
|
739
913
|
this._state = QvacState.RESUME;
|
|
914
|
+
this._parked.forEach((replay) => replay());
|
|
915
|
+
this._parked.clear();
|
|
916
|
+
this._takeBackHandedOff();
|
|
740
917
|
return;
|
|
741
918
|
}
|
|
742
919
|
this._state = QvacState.RESUME;
|
|
@@ -771,10 +948,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
771
948
|
}
|
|
772
949
|
async loadModel(name) {
|
|
773
950
|
if (!this._harness.loadModel) {
|
|
774
|
-
throw new
|
|
951
|
+
throw new CoreError('MODEL_RUNTIME_UNSUPPORTED', ['preload']);
|
|
775
952
|
}
|
|
776
953
|
if (!(await this._isCached(name))) {
|
|
777
|
-
throw new
|
|
954
|
+
throw new CoreError('MODEL_NOT_DOWNLOADED', [name]);
|
|
778
955
|
}
|
|
779
956
|
const config = await this._engine.getConfig({}).catch(noop);
|
|
780
957
|
const agent = await this._agentRunning(name, config?.activeAgentId ?? null);
|
|
@@ -789,6 +966,74 @@ export class QvacAssistant extends ReadyResource {
|
|
|
789
966
|
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
790
967
|
return rows?.some((row) => row.name === model && row.isCached) === true;
|
|
791
968
|
}
|
|
969
|
+
_scheduleModelReload(agent) {
|
|
970
|
+
const deviceId = this._deviceId;
|
|
971
|
+
if (!this._harness.loadModel ||
|
|
972
|
+
!deviceId ||
|
|
973
|
+
!agent.modelName ||
|
|
974
|
+
!agent.providerDeviceId ||
|
|
975
|
+
!b4a.equals(agent.providerDeviceId, deviceId)) {
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
const name = agent.modelName;
|
|
979
|
+
clearTimeout(this._reloads.get(name));
|
|
980
|
+
const timer = setTimeout(() => {
|
|
981
|
+
this._reloads.delete(name);
|
|
982
|
+
this._reloadLoadedModel(agent).catch((err) => this._log?.warn('models', 'model reload failed', { model: name, err }));
|
|
983
|
+
}, this._modelReloadDelayMs);
|
|
984
|
+
unref(timer);
|
|
985
|
+
this._reloads.set(name, timer);
|
|
986
|
+
}
|
|
987
|
+
async _reloadLoadedModel(agent) {
|
|
988
|
+
if (this.closing || this.suspended || !this._harness.loadModel)
|
|
989
|
+
return;
|
|
990
|
+
const model = runnableModelName(agent.modelName, await this._runnableHost());
|
|
991
|
+
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
992
|
+
const busy = [...this._runs.values()].some((run) => run.executing && run.model === model);
|
|
993
|
+
if (busy || !rows?.some((row) => row.name === model && row.isLoaded)) {
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
const config = await this._engine.getConfig({}).catch(noop);
|
|
997
|
+
const knobs = withConfigDefaults(agent, config);
|
|
998
|
+
const { loaded, reason } = await this._harness.loadModel({ model, agent: knobs });
|
|
999
|
+
this._log?.info('models', 'model reload requested', { model, loaded, reason });
|
|
1000
|
+
}
|
|
1001
|
+
_scheduleModelRelease(modelName) {
|
|
1002
|
+
clearTimeout(this._releases.get(modelName));
|
|
1003
|
+
const timer = setTimeout(() => {
|
|
1004
|
+
this._releases.delete(modelName);
|
|
1005
|
+
this._releaseUnusedModel(modelName).catch((err) => this._log?.warn('models', 'model release failed', { model: modelName, err }));
|
|
1006
|
+
}, this._modelReloadDelayMs);
|
|
1007
|
+
unref(timer);
|
|
1008
|
+
this._releases.set(modelName, timer);
|
|
1009
|
+
}
|
|
1010
|
+
async _releaseUnusedModel(modelName) {
|
|
1011
|
+
if (this.closing || this.suspended || !this._harness.unloadModel)
|
|
1012
|
+
return;
|
|
1013
|
+
const model = runnableModelName(modelName, await this._runnableHost());
|
|
1014
|
+
const holders = [...this._runs.values()].filter((run) => run.executing && run.model === model);
|
|
1015
|
+
await Promise.allSettled(holders.map((run) => run.done));
|
|
1016
|
+
if (this.closing || this.suspended) {
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
1020
|
+
if (!rows?.some((row) => row.name === model && row.isLoaded)) {
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
1023
|
+
const { agents } = await this.modelUsers(model);
|
|
1024
|
+
if (agents?.length) {
|
|
1025
|
+
return;
|
|
1026
|
+
}
|
|
1027
|
+
const { unloaded } = await this.unloadModel(model);
|
|
1028
|
+
this._log?.info('models', 'unused model released', { model, unloaded });
|
|
1029
|
+
}
|
|
1030
|
+
_cancelModelReloads() {
|
|
1031
|
+
for (const timer of [...this._reloads.values(), ...this._releases.values()]) {
|
|
1032
|
+
clearTimeout(timer);
|
|
1033
|
+
}
|
|
1034
|
+
this._reloads.clear();
|
|
1035
|
+
this._releases.clear();
|
|
1036
|
+
}
|
|
792
1037
|
async _agentRunning(model, activeAgentId) {
|
|
793
1038
|
const deviceId = this._requireDeviceId();
|
|
794
1039
|
const meshId = this._accountMeshId;
|
|
@@ -817,9 +1062,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
817
1062
|
}
|
|
818
1063
|
async _unloadModel(name) {
|
|
819
1064
|
if (!this._harness.unloadModel) {
|
|
820
|
-
throw new
|
|
1065
|
+
throw new CoreError('MODEL_RUNTIME_UNSUPPORTED', ['unload']);
|
|
821
1066
|
}
|
|
822
|
-
const holders = [...this._runs.values()].filter((run) => run.executing &&
|
|
1067
|
+
const holders = [...this._runs.values()].filter((run) => run.executing && run.model === name);
|
|
823
1068
|
for (const run of holders) {
|
|
824
1069
|
run.controller.abort(MODEL_UNLOADED);
|
|
825
1070
|
}
|
|
@@ -837,15 +1082,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
837
1082
|
const host = await this._runnableHost();
|
|
838
1083
|
const { meshes } = await this._engine.listMeshes({});
|
|
839
1084
|
const scanned = await Promise.all((meshes ?? [])
|
|
840
|
-
.filter((mesh) => mesh.open && !mesh.
|
|
1085
|
+
.filter((mesh) => mesh.open && !mesh.workspace)
|
|
841
1086
|
.map((mesh) => this._meshModelUsers(name, { meshId: mesh.meshId, providerDeviceId, host })));
|
|
842
1087
|
return { agents: scanned.flat() };
|
|
843
1088
|
}
|
|
844
1089
|
async _meshModelUsers(name, { meshId, providerDeviceId, host }) {
|
|
845
1090
|
const { agents } = await this._engine.listAgents({ meshId, providerDeviceId });
|
|
846
|
-
const perAgent = await Promise.all((agents ?? [])
|
|
847
|
-
.filter((agent) => !this._lenderFor(agent))
|
|
848
|
-
.map(async (agent) => {
|
|
1091
|
+
const perAgent = await Promise.all((agents ?? []).map(async (agent) => {
|
|
849
1092
|
let models = modelsForAgent(agent, host);
|
|
850
1093
|
if (models.some((model) => model.use === 'music')) {
|
|
851
1094
|
const configs = await this._engine
|
|
@@ -872,9 +1115,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
872
1115
|
notifyModelCatalog() {
|
|
873
1116
|
this._modelCatalog.notify();
|
|
874
1117
|
}
|
|
1118
|
+
cachedModelsChanged() {
|
|
1119
|
+
this._modelCatalog.notify();
|
|
1120
|
+
this._reportCapabilities().catch(noop);
|
|
1121
|
+
}
|
|
875
1122
|
async retryModelDownload(name) {
|
|
876
1123
|
if (!this._downloads) {
|
|
877
|
-
throw new
|
|
1124
|
+
throw new CoreError('MODEL_DOWNLOAD_UNAVAILABLE');
|
|
878
1125
|
}
|
|
879
1126
|
await this._downloads.retry(name);
|
|
880
1127
|
}
|
|
@@ -924,14 +1171,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
924
1171
|
const meshId = this._accountMeshId;
|
|
925
1172
|
if (deviceId && meshId) {
|
|
926
1173
|
const { agents } = await this._engine.listAgents({ meshId, providerDeviceId: deviceId });
|
|
927
|
-
const
|
|
928
|
-
.filter((shared) => shared !== meshId)
|
|
929
|
-
.map((shared) => this._engine.listAgents({ meshId: shared, providerDeviceId: deviceId }).catch(noop)));
|
|
930
|
-
const seen = new Set();
|
|
931
|
-
for (const agent of [agents ?? [], ...hosted.map((list) => list?.agents ?? [])].flat()) {
|
|
932
|
-
if (seen.has(agent.id))
|
|
933
|
-
continue;
|
|
934
|
-
seen.add(agent.id);
|
|
1174
|
+
for (const agent of agents ?? []) {
|
|
935
1175
|
const resolved = (await this._harness.skills?.(agent.id, agent.modelName, {
|
|
936
1176
|
skipCredentialProbe: true
|
|
937
1177
|
})) ?? [];
|
|
@@ -946,9 +1186,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
946
1186
|
_supervisedWatch(label, run) {
|
|
947
1187
|
let running = false;
|
|
948
1188
|
let again = false;
|
|
1189
|
+
let stopped = false;
|
|
949
1190
|
const settle = (err) => {
|
|
950
1191
|
running = false;
|
|
951
|
-
if (err !== null && !
|
|
1192
|
+
if (err !== null && !stopped && !isCoreError(err, 'MESH_NOT_FOUND')) {
|
|
952
1193
|
this._log?.error('assistant', `${label} watch failed`, err);
|
|
953
1194
|
}
|
|
954
1195
|
if (again) {
|
|
@@ -965,12 +1206,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
965
1206
|
return;
|
|
966
1207
|
}
|
|
967
1208
|
running = true;
|
|
1209
|
+
stopped = false;
|
|
968
1210
|
run().then(() => settle(null), (err) => settle(err));
|
|
969
1211
|
};
|
|
970
|
-
|
|
1212
|
+
const stop = () => {
|
|
1213
|
+
again = false;
|
|
1214
|
+
stopped = true;
|
|
1215
|
+
};
|
|
1216
|
+
return { start, stop };
|
|
971
1217
|
}
|
|
972
|
-
async _reclaimDeletedChats(frames) {
|
|
1218
|
+
async _reclaimDeletedChats(frames, meshId, served) {
|
|
973
1219
|
let known = null;
|
|
1220
|
+
let agents = new Map();
|
|
974
1221
|
for await (const frame of frames) {
|
|
975
1222
|
if (this.closing || this.suspended)
|
|
976
1223
|
return;
|
|
@@ -978,55 +1225,95 @@ export class QvacAssistant extends ReadyResource {
|
|
|
978
1225
|
const gone = new Set(known === null ? [] : disappearedChatIds(known, ids));
|
|
979
1226
|
if (known !== null) {
|
|
980
1227
|
for (const chatId of gone) {
|
|
981
|
-
this._dropDeletedChat(chatId);
|
|
1228
|
+
this._dropDeletedChat(meshId, chatId);
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
for (const chat of frame.chats ?? []) {
|
|
1232
|
+
const before = agents.get(chat.id);
|
|
1233
|
+
if (before && before !== chat.agentId) {
|
|
1234
|
+
void this._dropServedKvCache(chat.id, before);
|
|
982
1235
|
}
|
|
983
1236
|
}
|
|
984
1237
|
known = new Set(ids);
|
|
1238
|
+
agents = new Map((frame.chats ?? []).map((chat) => [chat.id, chat.agentId]));
|
|
1239
|
+
if (served) {
|
|
1240
|
+
this._servedChats.set(meshId, known);
|
|
1241
|
+
}
|
|
985
1242
|
await this._sweepImageCache(gone);
|
|
986
1243
|
await this._sweepInferenceCalls();
|
|
987
1244
|
}
|
|
988
1245
|
}
|
|
989
|
-
|
|
1246
|
+
async _dropServedKvCache(chatId, agentId) {
|
|
1247
|
+
for (const key of this._roundHost?.left(chatId, agentId) ?? []) {
|
|
1248
|
+
try {
|
|
1249
|
+
await this._harness.deleteKvCache?.(key);
|
|
1250
|
+
}
|
|
1251
|
+
catch (err) {
|
|
1252
|
+
this._log?.warn('assistant', 'served kv-cache deletion failed', { chatId, err });
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
async _sweepIdleChatCaches(meshId, frames) {
|
|
1257
|
+
for await (const _frame of frames) {
|
|
1258
|
+
if (this.closing || this.suspended)
|
|
1259
|
+
return;
|
|
1260
|
+
void this._kvRetention.sweep(meshId);
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
_dropDeletedChat(meshId, chatId) {
|
|
990
1264
|
for (const run of this._runs.values()) {
|
|
991
|
-
if (run.chatId === chatId) {
|
|
992
|
-
|
|
1265
|
+
if (run.meshId === meshId && run.chatId === chatId) {
|
|
1266
|
+
run.controller.abort(RUN_SETTLED);
|
|
993
1267
|
}
|
|
994
1268
|
}
|
|
995
1269
|
this._reconciledCompactions.delete(chatId);
|
|
996
|
-
this.
|
|
997
|
-
|
|
1270
|
+
void this._deleteChatKvCache(chatId, 'chat deleted').finally(() => this._runReplay
|
|
1271
|
+
.forget(chatId)
|
|
1272
|
+
.catch((err) => this._log?.warn('assistant', 'run replay not dropped', { err })));
|
|
998
1273
|
void this._inferenceCalls
|
|
999
1274
|
.forget(chatId)
|
|
1000
1275
|
.catch((err) => this._log?.warn('assistant', 'inspector records not dropped', { err }));
|
|
1001
1276
|
}
|
|
1002
|
-
async
|
|
1277
|
+
async _followAgentEdits(meshId, frames) {
|
|
1003
1278
|
let known = null;
|
|
1004
1279
|
for await (const frame of frames) {
|
|
1005
1280
|
if (this.closing || this.suspended)
|
|
1006
1281
|
return;
|
|
1007
1282
|
const agents = frame.agents ?? [];
|
|
1008
1283
|
this._followHosted(meshId, agents);
|
|
1009
|
-
const next = new Map(agents.map((agent) => [agent.id, agentRekeyFingerprint(agent)]));
|
|
1010
1284
|
for (const agent of agents) {
|
|
1011
1285
|
const previous = known?.get(agent.id);
|
|
1012
|
-
if (previous
|
|
1013
|
-
|
|
1286
|
+
if (!previous) {
|
|
1287
|
+
continue;
|
|
1288
|
+
}
|
|
1289
|
+
if (agentRekeyFingerprint(previous) !== agentRekeyFingerprint(agent)) {
|
|
1290
|
+
void this._reclaimAgentChatCaches(meshId, agent);
|
|
1291
|
+
}
|
|
1292
|
+
if (!sameLoadSettings(previous, agent)) {
|
|
1293
|
+
this._scheduleModelReload(agent);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
const next = new Map(agents.map((agent) => [agent.id, agent]));
|
|
1297
|
+
for (const previous of known?.values() ?? []) {
|
|
1298
|
+
const model = modelLeft(previous, next.get(previous.id), this._deviceId);
|
|
1299
|
+
if (model) {
|
|
1300
|
+
this._scheduleModelRelease(model);
|
|
1014
1301
|
}
|
|
1015
1302
|
}
|
|
1016
1303
|
known = next;
|
|
1017
1304
|
}
|
|
1018
1305
|
}
|
|
1019
|
-
async _reclaimAgentChatCaches(meshId,
|
|
1306
|
+
async _reclaimAgentChatCaches(meshId, agent) {
|
|
1020
1307
|
try {
|
|
1021
1308
|
const { chats } = await this._engine.listChats({ meshId });
|
|
1022
1309
|
for (const chat of chats ?? []) {
|
|
1023
|
-
if (chat.agentId ===
|
|
1310
|
+
if (chat.agentId === agent.id) {
|
|
1024
1311
|
await this._deleteChatKvCache(chat.id, 'agent re-key');
|
|
1025
1312
|
}
|
|
1026
1313
|
}
|
|
1027
1314
|
}
|
|
1028
1315
|
catch (err) {
|
|
1029
|
-
this._log?.warn('assistant', 'agent re-key cache reclaim failed', { agentId, err });
|
|
1316
|
+
this._log?.warn('assistant', 'agent re-key cache reclaim failed', { agentId: agent.id, err });
|
|
1030
1317
|
}
|
|
1031
1318
|
}
|
|
1032
1319
|
async _reconcileCompaction(engine, chatId) {
|
|
@@ -1051,41 +1338,99 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1051
1338
|
this._reconciledCompactions.set(chatId, newest.id);
|
|
1052
1339
|
}
|
|
1053
1340
|
}
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1341
|
+
deleteChatKvCache(chatId) {
|
|
1342
|
+
return this._reclaimChatKvCache(chatId, 'deleted by request');
|
|
1343
|
+
}
|
|
1344
|
+
async _reclaimChatKvCache(chatId, reason) {
|
|
1345
|
+
const result = (await this._harness.deleteKvCache?.(chatKvCacheKey(chatId))) ?? {
|
|
1346
|
+
status: 'unsupported',
|
|
1347
|
+
error: 'Harness does not support KV cache deletion'
|
|
1348
|
+
};
|
|
1349
|
+
if (result.status === 'success') {
|
|
1350
|
+
await this._runReplay.forget(chatId, { holder: LOCAL_HOLDER, reason });
|
|
1057
1351
|
}
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1352
|
+
return result;
|
|
1353
|
+
}
|
|
1354
|
+
async clearKvCache() {
|
|
1355
|
+
const result = await harnessClearKvCache(this._harness)();
|
|
1356
|
+
if (result.status === 'success') {
|
|
1357
|
+
await this._runReplay.clear(LOCAL_HOLDER);
|
|
1358
|
+
}
|
|
1359
|
+
return result;
|
|
1064
1360
|
}
|
|
1065
|
-
async
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1361
|
+
async _clearPreReplayCaches() {
|
|
1362
|
+
if (await this._runReplay.started())
|
|
1363
|
+
return;
|
|
1364
|
+
const result = await this.clearKvCache();
|
|
1365
|
+
if (result.status === 'failure') {
|
|
1366
|
+
this._log?.warn('assistant', 'pre-replay kv caches not cleared', { error: result.error });
|
|
1069
1367
|
return;
|
|
1070
1368
|
}
|
|
1071
|
-
|
|
1072
|
-
|
|
1369
|
+
await this._runReplay.start();
|
|
1370
|
+
}
|
|
1371
|
+
_inspectCall(inspecting, chatId, entry) {
|
|
1372
|
+
if (!inspecting)
|
|
1373
|
+
return;
|
|
1374
|
+
void this._inferenceCalls
|
|
1375
|
+
.record(chatId, entry)
|
|
1376
|
+
.catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
|
|
1377
|
+
}
|
|
1378
|
+
async _inspectRound(chatId, entry) {
|
|
1379
|
+
this._inspectCall(await this._capturingInferenceCalls(), chatId, entry);
|
|
1380
|
+
}
|
|
1381
|
+
_inspectSent(inspecting, chatId, sent) {
|
|
1382
|
+
if (!inspecting)
|
|
1073
1383
|
return;
|
|
1384
|
+
void this._inferenceCalls
|
|
1385
|
+
.recordSent(chatId, sent)
|
|
1386
|
+
.catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
|
|
1387
|
+
}
|
|
1388
|
+
async _sentRuns(chatId, rounds) {
|
|
1389
|
+
const served = replayFromChunks(rounds);
|
|
1390
|
+
try {
|
|
1391
|
+
const { sentRuns, askedRuns } = await this._runReplay.loadReplay({
|
|
1392
|
+
holder: LOCAL_HOLDER,
|
|
1393
|
+
chatId
|
|
1394
|
+
});
|
|
1395
|
+
for (const [runId, turns] of served.sentRuns) {
|
|
1396
|
+
sentRuns.set(runId, turns);
|
|
1397
|
+
}
|
|
1398
|
+
return {
|
|
1399
|
+
sentRuns,
|
|
1400
|
+
askedRuns,
|
|
1401
|
+
readRuns: served.readRuns,
|
|
1402
|
+
rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
|
|
1403
|
+
};
|
|
1074
1404
|
}
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
this.
|
|
1405
|
+
catch (err) {
|
|
1406
|
+
this._log?.warn('assistant', 'run replay unreadable', { chatId, err });
|
|
1407
|
+
await this._deleteChatKvCache(chatId, 'run replay unreadable');
|
|
1408
|
+
return {
|
|
1409
|
+
sentRuns: new Map(),
|
|
1410
|
+
askedRuns: new Map(),
|
|
1411
|
+
readRuns: new Map(),
|
|
1412
|
+
rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
|
|
1413
|
+
};
|
|
1078
1414
|
}
|
|
1079
1415
|
}
|
|
1080
|
-
async
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1416
|
+
async _recordSentRun(chatId, { runId, turns, asked }) {
|
|
1417
|
+
if (turns) {
|
|
1418
|
+
try {
|
|
1419
|
+
await this._runReplay.save({ holder: LOCAL_HOLDER, chatId }, runId, {
|
|
1420
|
+
messages: turns,
|
|
1421
|
+
asked
|
|
1422
|
+
});
|
|
1423
|
+
return;
|
|
1424
|
+
}
|
|
1425
|
+
catch (err) {
|
|
1426
|
+
this._log?.warn('assistant', 'run replay not saved', { chatId, runId, err });
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
await this._deleteChatKvCache(chatId, 'run not replayable as sent');
|
|
1085
1430
|
}
|
|
1086
1431
|
async _deleteChatKvCache(chatId, reason) {
|
|
1087
1432
|
try {
|
|
1088
|
-
const result = await this.
|
|
1433
|
+
const result = await this._reclaimChatKvCache(chatId, reason);
|
|
1089
1434
|
if (result.status === 'failure') {
|
|
1090
1435
|
this._log?.warn('assistant', 'chat kv-cache deletion failed', {
|
|
1091
1436
|
chatId,
|
|
@@ -1124,31 +1469,33 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1124
1469
|
}
|
|
1125
1470
|
}
|
|
1126
1471
|
_syncMeshConsumers(meshes) {
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
for (const meshId of this._hostedIn.keys()) {
|
|
1132
|
-
if (this._sharedMeshes.has(meshId))
|
|
1133
|
-
continue;
|
|
1134
|
-
this._hostedIn.delete(meshId);
|
|
1135
|
-
this._lastCapabilities.delete(meshId);
|
|
1136
|
-
}
|
|
1472
|
+
this._listed = meshes;
|
|
1473
|
+
const consumed = meshes.filter((mesh) => mesh.open && mesh.meshId !== this._ownWorkspace);
|
|
1474
|
+
const open = new Set(consumed.map((mesh) => mesh.meshId));
|
|
1475
|
+
const served = new Set(consumed.filter((mesh) => mesh.served).map((mesh) => mesh.meshId));
|
|
1137
1476
|
for (const [meshId, consumer] of this._meshConsumers) {
|
|
1138
1477
|
if (open.has(meshId))
|
|
1139
1478
|
continue;
|
|
1140
1479
|
consumer.stop();
|
|
1141
1480
|
this._meshConsumers.delete(meshId);
|
|
1142
1481
|
this._pruneSeen(meshId, new Set());
|
|
1482
|
+
for (const chatId of this._servedChats.get(meshId) ?? []) {
|
|
1483
|
+
void this._deleteChatKvCache(chatId, 'served mesh left');
|
|
1484
|
+
}
|
|
1485
|
+
this._servedChats.delete(meshId);
|
|
1143
1486
|
}
|
|
1144
1487
|
for (const meshId of open) {
|
|
1145
|
-
const consumer = this._meshConsumers.get(meshId) ?? this._meshConsumer(meshId);
|
|
1488
|
+
const consumer = this._meshConsumers.get(meshId) ?? this._meshConsumer(meshId, served.has(meshId));
|
|
1146
1489
|
this._meshConsumers.set(meshId, consumer);
|
|
1147
1490
|
consumer.start();
|
|
1148
1491
|
}
|
|
1149
1492
|
}
|
|
1150
1493
|
_followAccount(account) {
|
|
1151
1494
|
this._accountMeshId = account.meshId ?? null;
|
|
1495
|
+
if ((account.ownWorkspace ?? null) !== this._ownWorkspace) {
|
|
1496
|
+
this._ownWorkspace = account.ownWorkspace ?? null;
|
|
1497
|
+
this._syncMeshConsumers(this._listed);
|
|
1498
|
+
}
|
|
1152
1499
|
const on = account.meshId ? `${account.meshId}:${account.writable}` : null;
|
|
1153
1500
|
if (on === null) {
|
|
1154
1501
|
this._advertisedOn = null;
|
|
@@ -1164,12 +1511,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1164
1511
|
void this._reconcileDeviceProfile();
|
|
1165
1512
|
this._triggerModelWarm();
|
|
1166
1513
|
}
|
|
1167
|
-
_meshConsumer(meshId) {
|
|
1514
|
+
_meshConsumer(meshId, served) {
|
|
1168
1515
|
const watches = [
|
|
1169
|
-
this._streamWatch(`
|
|
1170
|
-
this._streamWatch(`
|
|
1171
|
-
this._streamWatch(`agent:${meshId}`, () => this._engine.agentsWatch({ meshId }), (frames) => this._reclaimRekeyedAgents(meshId, frames))
|
|
1516
|
+
this._streamWatch(`chat-list:${meshId}`, () => this._engine.chatsWatch({ meshId }), (frames) => this._reclaimDeletedChats(frames, meshId, served)),
|
|
1517
|
+
this._streamWatch(`kv-cache-retention:${meshId}`, () => this._engine.configWatch({ key: KV_CACHE_RETENTION_KEY }), (frames) => this._sweepIdleChatCaches(meshId, frames))
|
|
1172
1518
|
];
|
|
1519
|
+
if (!served) {
|
|
1520
|
+
watches.push(this._streamWatch(`run-requests:${meshId}`, () => this._engine.runRequestsWatch({ meshId }), (frames) => this._dispatchRunRequests(meshId, frames)), this._streamWatch(`agent:${meshId}`, () => this._engine.agentsWatch({ meshId }), (frames) => this._followAgentEdits(meshId, frames)));
|
|
1521
|
+
}
|
|
1522
|
+
const host = this._roundHost;
|
|
1523
|
+
if (host) {
|
|
1524
|
+
watches.push(this._streamWatch(`model-requests:${meshId}`, () => this._engine.modelRequestsWatch({ meshId }), (frames) => host.follow(meshId, frames)));
|
|
1525
|
+
}
|
|
1173
1526
|
return {
|
|
1174
1527
|
start: () => watches.forEach((watch) => watch.start()),
|
|
1175
1528
|
stop: () => watches.forEach((watch) => watch.stop())
|
|
@@ -1190,17 +1543,39 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1190
1543
|
};
|
|
1191
1544
|
}
|
|
1192
1545
|
async _dispatchRunRequests(meshId, frames) {
|
|
1546
|
+
let opened = true;
|
|
1193
1547
|
for await (const frame of frames) {
|
|
1194
1548
|
if (this.closing || this.suspended)
|
|
1195
1549
|
return;
|
|
1196
1550
|
const chunks = frame.chunks ?? [];
|
|
1197
|
-
|
|
1551
|
+
const deviceId = this._requireDeviceId();
|
|
1552
|
+
const handed = chunks.filter((chunk) => handedOff(chunk, deviceId));
|
|
1553
|
+
this._handedOff.set(meshId, handed);
|
|
1554
|
+
if (opened) {
|
|
1555
|
+
this._takeBackHandedOff(meshId);
|
|
1556
|
+
}
|
|
1557
|
+
opened = false;
|
|
1558
|
+
for (const chunk of chunks.filter((chunk) => !handed.includes(chunk))) {
|
|
1198
1559
|
this._dispatch(chunk, meshId);
|
|
1199
1560
|
}
|
|
1200
1561
|
this._pruneSeen(meshId, new Set(chunks.map((chunk) => chunk.runId)));
|
|
1201
1562
|
}
|
|
1202
1563
|
}
|
|
1564
|
+
_takeBackHandedOff(only) {
|
|
1565
|
+
for (const [meshId, requests] of this._handedOff) {
|
|
1566
|
+
if (only && only !== meshId)
|
|
1567
|
+
continue;
|
|
1568
|
+
const engine = meshScoped(this._engine, meshId);
|
|
1569
|
+
for (const request of requests) {
|
|
1570
|
+
engine.takeRunRequest({ chunkId: request.id }).catch((err) => {
|
|
1571
|
+
this._log?.debug('runs', 'taking a handed-off run back failed', err);
|
|
1572
|
+
});
|
|
1573
|
+
}
|
|
1574
|
+
this._handedOff.delete(meshId);
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1203
1577
|
_stopMeshIntake() {
|
|
1578
|
+
this._roundHost?.stop();
|
|
1204
1579
|
this._meshListWatch.stop();
|
|
1205
1580
|
this._accountWatch.stop();
|
|
1206
1581
|
for (const consumer of this._meshConsumers.values()) {
|
|
@@ -1216,35 +1591,6 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1216
1591
|
this._seen.delete(runId);
|
|
1217
1592
|
}
|
|
1218
1593
|
}
|
|
1219
|
-
_lenderFor(agent) {
|
|
1220
|
-
const lender = agent.lenderDeviceId;
|
|
1221
|
-
if (!lender || !this._borrowHarness) {
|
|
1222
|
-
return null;
|
|
1223
|
-
}
|
|
1224
|
-
const id = b4a.toHex(lender);
|
|
1225
|
-
const own = this._deviceId;
|
|
1226
|
-
return own && id === b4a.toHex(own) ? null : id;
|
|
1227
|
-
}
|
|
1228
|
-
_harnessFor(agent) {
|
|
1229
|
-
const id = this._lenderFor(agent);
|
|
1230
|
-
const borrow = this._borrowHarness;
|
|
1231
|
-
if (!id || !borrow) {
|
|
1232
|
-
return this._harness;
|
|
1233
|
-
}
|
|
1234
|
-
const held = this._borrowed.get(id);
|
|
1235
|
-
if (held) {
|
|
1236
|
-
return held;
|
|
1237
|
-
}
|
|
1238
|
-
const remote = connectHarness(() => borrow(id));
|
|
1239
|
-
remote.on('status', (status) => {
|
|
1240
|
-
if (status.state === 'died') {
|
|
1241
|
-
this._borrowed.delete(id);
|
|
1242
|
-
}
|
|
1243
|
-
});
|
|
1244
|
-
const harness = remote;
|
|
1245
|
-
this._borrowed.set(id, harness);
|
|
1246
|
-
return harness;
|
|
1247
|
-
}
|
|
1248
1594
|
_dispatch(request, meshId) {
|
|
1249
1595
|
const runId = request.runId;
|
|
1250
1596
|
if (!runId || this._runs.has(runId) || this._seen.has(runId)) {
|
|
@@ -1252,59 +1598,125 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1252
1598
|
}
|
|
1253
1599
|
this._seen.set(runId, meshId);
|
|
1254
1600
|
const engine = meshScoped(this._engine, meshId);
|
|
1255
|
-
const
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
if (!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, this._requireDeviceId())) {
|
|
1263
|
-
return;
|
|
1601
|
+
const chat = `${meshId}:${request.chatId}`;
|
|
1602
|
+
const scheduled = (this._intake.get(chat) ?? Promise.resolve()).then(() => this._schedule(engine, request, { runId, meshId, chat }));
|
|
1603
|
+
const turn = scheduled.then(noop, noop);
|
|
1604
|
+
this._intake.set(chat, turn);
|
|
1605
|
+
void turn.then(() => {
|
|
1606
|
+
if (this._intake.get(chat) === turn) {
|
|
1607
|
+
this._intake.delete(chat);
|
|
1264
1608
|
}
|
|
1265
|
-
|
|
1609
|
+
});
|
|
1610
|
+
const settle = (async () => {
|
|
1611
|
+
const run = await scheduled;
|
|
1612
|
+
if (!run) {
|
|
1266
1613
|
return;
|
|
1267
1614
|
}
|
|
1268
|
-
this._log?.info('runs', 'run claimed', { runId, chatId: request.chatId, agentId });
|
|
1269
|
-
const controller = new AbortController();
|
|
1270
|
-
const prior = this._chats.get(request.chatId) ?? Promise.resolve();
|
|
1271
|
-
const done = prior.then(() => this._execute(request, agent, { controller, meshId, runId }));
|
|
1272
|
-
const link = done.catch(noop);
|
|
1273
|
-
this._chats.set(request.chatId, link);
|
|
1274
|
-
this._runs.set(runId, {
|
|
1275
|
-
controller,
|
|
1276
|
-
done,
|
|
1277
|
-
chatId: request.chatId,
|
|
1278
|
-
model: agent.modelName,
|
|
1279
|
-
executing: false,
|
|
1280
|
-
borrowed: false
|
|
1281
|
-
});
|
|
1282
1615
|
try {
|
|
1283
|
-
await done;
|
|
1616
|
+
await run.done;
|
|
1284
1617
|
}
|
|
1285
1618
|
finally {
|
|
1286
1619
|
this._runImageCacheNames.delete(runId);
|
|
1287
1620
|
this._runs.delete(runId);
|
|
1288
|
-
this.
|
|
1289
|
-
if (this._chats.get(
|
|
1290
|
-
this._chats.delete(
|
|
1621
|
+
this._runsChanged();
|
|
1622
|
+
if (this._chats.get(chat) === run.link) {
|
|
1623
|
+
this._chats.delete(chat);
|
|
1291
1624
|
}
|
|
1292
1625
|
void this._reportCapabilities();
|
|
1293
|
-
|
|
1294
|
-
if (requeued && !this.closing) {
|
|
1295
|
-
this._dispatch(request, meshId);
|
|
1296
|
-
}
|
|
1626
|
+
this._requeue(runId, run.controller.signal.reason, () => this._dispatch(run.request, meshId));
|
|
1297
1627
|
}
|
|
1298
1628
|
})();
|
|
1299
1629
|
settle.catch((err) => this._log?.error('assistant', `run ${runId} failed`, err));
|
|
1300
1630
|
}
|
|
1631
|
+
async _schedule(engine, request, { runId, meshId, chat }) {
|
|
1632
|
+
const claim = await this._claim(engine, request, { runId, meshId });
|
|
1633
|
+
if (!claim) {
|
|
1634
|
+
return undefined;
|
|
1635
|
+
}
|
|
1636
|
+
const { agent } = claim;
|
|
1637
|
+
const controller = new AbortController();
|
|
1638
|
+
const prior = this._chats.get(chat) ?? Promise.resolve();
|
|
1639
|
+
const done = prior.then(() => this._execute(claim.request, agent, { controller, meshId, runId }));
|
|
1640
|
+
const link = done.catch(noop);
|
|
1641
|
+
this._chats.set(chat, link);
|
|
1642
|
+
this._runs.set(runId, {
|
|
1643
|
+
controller,
|
|
1644
|
+
done,
|
|
1645
|
+
meshId,
|
|
1646
|
+
chatId: claim.request.chatId,
|
|
1647
|
+
agentId: agent.id,
|
|
1648
|
+
model: agent.modelName,
|
|
1649
|
+
executing: false
|
|
1650
|
+
});
|
|
1651
|
+
return { request: claim.request, controller, done, link };
|
|
1652
|
+
}
|
|
1653
|
+
async _claim(engine, request, { runId, meshId }) {
|
|
1654
|
+
const agentId = request.runRequest?.agentId;
|
|
1655
|
+
const agent = agentId ? await engine.getAgent({ agentId }).catch(noop) : undefined;
|
|
1656
|
+
if (!agent) {
|
|
1657
|
+
this._seen.delete(runId);
|
|
1658
|
+
return undefined;
|
|
1659
|
+
}
|
|
1660
|
+
const deviceId = this._requireDeviceId();
|
|
1661
|
+
if (offeredTo(request, deviceId) && !(await this._isHandled(engine, request))) {
|
|
1662
|
+
if (this._backgrounded) {
|
|
1663
|
+
this._seen.delete(runId);
|
|
1664
|
+
if (this._state === QvacState.PRESUSPEND) {
|
|
1665
|
+
this._parked.set(runId, () => this._dispatch(request, meshId));
|
|
1666
|
+
}
|
|
1667
|
+
return undefined;
|
|
1668
|
+
}
|
|
1669
|
+
const taken = await engine.takeRunRequest({ chunkId: request.id }).catch(noop);
|
|
1670
|
+
if (!taken) {
|
|
1671
|
+
this._seen.delete(runId);
|
|
1672
|
+
return undefined;
|
|
1673
|
+
}
|
|
1674
|
+
request = taken;
|
|
1675
|
+
}
|
|
1676
|
+
if (!drivenBy({ request, agent }, deviceId)) {
|
|
1677
|
+
return undefined;
|
|
1678
|
+
}
|
|
1679
|
+
if (await this._isHandled(engine, request)) {
|
|
1680
|
+
return undefined;
|
|
1681
|
+
}
|
|
1682
|
+
this._log?.info('runs', 'run claimed', { runId, chatId: request.chatId, agentId });
|
|
1683
|
+
if (!request.capability) {
|
|
1684
|
+
await this._offerSteer(engine, request, agent.id);
|
|
1685
|
+
}
|
|
1686
|
+
return { request, agent };
|
|
1687
|
+
}
|
|
1688
|
+
async _offerSteer(engine, request, agentId) {
|
|
1689
|
+
const running = [...this._runs.values()].find((run) => run.chatId === request.chatId && run.agentId === agentId && run.steering);
|
|
1690
|
+
if (!running?.steering || !request.runId) {
|
|
1691
|
+
return;
|
|
1692
|
+
}
|
|
1693
|
+
const asked = await engine.listChunks({ chatId: request.chatId, type: 'user-message' });
|
|
1694
|
+
const message = (asked.chunks ?? []).findLast((chunk) => chunk.seq < request.seq);
|
|
1695
|
+
const text = message?.userMessage?.text;
|
|
1696
|
+
if (text) {
|
|
1697
|
+
const [turn] = nameSenders([{ role: 'user', content: text, sender: senderOf(message) }]);
|
|
1698
|
+
running.steering.push({ mode: 'steer', text: turn.content, id: request.runId });
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
_requeue(runId, reason, replay) {
|
|
1702
|
+
const background = this._backgroundReplays.delete(runId);
|
|
1703
|
+
if (this._seen.has(runId) || this.closing) {
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
if (background && this._state === QvacState.PRESUSPEND) {
|
|
1707
|
+
this._parked.set(runId, replay);
|
|
1708
|
+
}
|
|
1709
|
+
else if ((background && !this._backgrounded) || reason === MODEL_UNLOADED) {
|
|
1710
|
+
replay();
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1301
1713
|
async _citeWebSources(transcript, runId, result) {
|
|
1302
1714
|
const records = webSourcesFromToolResult(result, WEB_RAG_SOURCE_BOUNDS);
|
|
1303
1715
|
if (records.length === 0) {
|
|
1304
1716
|
return;
|
|
1305
1717
|
}
|
|
1306
1718
|
try {
|
|
1307
|
-
await transcript.chunk({ type: 'sources', sources: { sources: records } });
|
|
1719
|
+
await transcript.chunk({ type: 'sources', sources: { sources: chunkSources(records) } });
|
|
1308
1720
|
}
|
|
1309
1721
|
catch (err) {
|
|
1310
1722
|
this._log?.warn('assistant', 'web sources chunk skipped', { runId, err });
|
|
@@ -1331,7 +1743,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1331
1743
|
if (!search.enabled) {
|
|
1332
1744
|
return null;
|
|
1333
1745
|
}
|
|
1334
|
-
if (!search.hasKnowledge) {
|
|
1746
|
+
if (!search.hasKnowledge && !notReadyCount(search)) {
|
|
1335
1747
|
return null;
|
|
1336
1748
|
}
|
|
1337
1749
|
return { search, lastUser, hits: search.hits ?? [] };
|
|
@@ -1359,7 +1771,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1359
1771
|
if (knowledge.hits.some(isTruncatedFullTextHit)) {
|
|
1360
1772
|
await sinks.transcript.chunk({
|
|
1361
1773
|
type: 'sources',
|
|
1362
|
-
sources: { sources: [], notice: RAG_TRUNCATED_NOTICE }
|
|
1774
|
+
sources: { sources: [], notice: RAG_TRUNCATED_NOTICE, ...notReady(knowledge.search) }
|
|
1363
1775
|
});
|
|
1364
1776
|
injected.sourcesWritten = true;
|
|
1365
1777
|
}
|
|
@@ -1367,8 +1779,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1367
1779
|
else {
|
|
1368
1780
|
await injectRetrievedKnowledge(knowledge, { ...sinks, injected });
|
|
1369
1781
|
}
|
|
1370
|
-
|
|
1371
|
-
|
|
1782
|
+
const line = notReadyLine(knowledge.search);
|
|
1783
|
+
if (line) {
|
|
1784
|
+
knowledge.lastUser.content += `\n\n${line}`;
|
|
1372
1785
|
}
|
|
1373
1786
|
}
|
|
1374
1787
|
catch (err) {
|
|
@@ -1376,13 +1789,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1376
1789
|
}
|
|
1377
1790
|
return injected;
|
|
1378
1791
|
}
|
|
1379
|
-
async _writeUnreadNotice(transcript, reason) {
|
|
1792
|
+
async _writeUnreadNotice(transcript, reason, search) {
|
|
1380
1793
|
try {
|
|
1381
1794
|
await transcript.chunk({
|
|
1382
1795
|
type: 'sources',
|
|
1383
1796
|
sources: {
|
|
1384
1797
|
sources: [],
|
|
1385
|
-
notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE
|
|
1798
|
+
notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE,
|
|
1799
|
+
...(search && notReady(search))
|
|
1386
1800
|
}
|
|
1387
1801
|
});
|
|
1388
1802
|
}
|
|
@@ -1390,6 +1804,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1390
1804
|
this._log?.warn('assistant', 'unread-document notice skipped', err);
|
|
1391
1805
|
}
|
|
1392
1806
|
}
|
|
1807
|
+
async _openRunLog(engine, runId) {
|
|
1808
|
+
if (!keepsRunLog(await this._ensureCapability())) {
|
|
1809
|
+
return null;
|
|
1810
|
+
}
|
|
1811
|
+
const runLog = createHostRunLogAdapter(engine);
|
|
1812
|
+
try {
|
|
1813
|
+
return { runLog, lease: await runLog.begin(runId) };
|
|
1814
|
+
}
|
|
1815
|
+
catch (err) {
|
|
1816
|
+
this._log?.warn('runs', 'run log not opened', { runId, err });
|
|
1817
|
+
return null;
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1393
1820
|
async _isHandled(engine, request) {
|
|
1394
1821
|
const { chunks } = await engine.listChunks({
|
|
1395
1822
|
chatId: request.chatId,
|
|
@@ -1397,57 +1824,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1397
1824
|
});
|
|
1398
1825
|
return (chunks ?? []).some((c) => c.runId === request.runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
1399
1826
|
}
|
|
1400
|
-
async _recordRunBoundaryStatus(engine, { request, runId, agent, deviceId, state, reason }) {
|
|
1401
|
-
try {
|
|
1402
|
-
const { chunks } = await engine.listChunks({
|
|
1403
|
-
chatId: request.chatId,
|
|
1404
|
-
type: 'run-status'
|
|
1405
|
-
});
|
|
1406
|
-
if ((chunks ?? []).some((chunk) => chunk.runId === runId &&
|
|
1407
|
-
(TERMINAL_STATES.has(chunk.runStatus?.state ?? '') ||
|
|
1408
|
-
chunk.runStatus?.state === 'recovery-required'))) {
|
|
1409
|
-
return;
|
|
1410
|
-
}
|
|
1411
|
-
}
|
|
1412
|
-
catch (error) {
|
|
1413
|
-
this._log?.warn('runs', 'could not inspect prior run status before setup failure', {
|
|
1414
|
-
runId,
|
|
1415
|
-
error
|
|
1416
|
-
});
|
|
1417
|
-
}
|
|
1418
|
-
const transcript = new RunTranscript(engine, {
|
|
1419
|
-
chatId: request.chatId,
|
|
1420
|
-
runId,
|
|
1421
|
-
deviceId,
|
|
1422
|
-
model: agent.modelName,
|
|
1423
|
-
log: this._log
|
|
1424
|
-
});
|
|
1425
|
-
try {
|
|
1426
|
-
await transcript.status({ state, reason });
|
|
1427
|
-
}
|
|
1428
|
-
catch (error) {
|
|
1429
|
-
this._seen.delete(runId);
|
|
1430
|
-
this._log?.error('runs', 'could not record run setup failure', { runId, error });
|
|
1431
|
-
}
|
|
1432
|
-
finally {
|
|
1433
|
-
transcript.close();
|
|
1434
|
-
}
|
|
1435
|
-
}
|
|
1436
1827
|
async _execute(request, agent, { controller, meshId, runId }) {
|
|
1437
1828
|
const engine = meshScoped(this._engine, meshId);
|
|
1829
|
+
const { chatId } = request;
|
|
1830
|
+
this._suggesting.get(chatId)?.abort(SUGGESTION_SUPERSEDED);
|
|
1438
1831
|
const deviceId = this._requireDeviceId();
|
|
1439
|
-
if (this.
|
|
1832
|
+
if (this._backgrounded) {
|
|
1440
1833
|
this._seen.delete(runId);
|
|
1834
|
+
this._backgroundReplays.add(runId);
|
|
1441
1835
|
return;
|
|
1442
1836
|
}
|
|
1443
1837
|
if (await this._isHandled(engine, request)) {
|
|
1444
1838
|
return;
|
|
1445
1839
|
}
|
|
1446
1840
|
const fresh = await engine.getAgent({ agentId: agent.id }).catch(noop);
|
|
1447
|
-
if (!fresh ||
|
|
1448
|
-
fresh.deletedAt ||
|
|
1449
|
-
!fresh.providerDeviceId ||
|
|
1450
|
-
!b4a.equals(fresh.providerDeviceId, deviceId)) {
|
|
1841
|
+
if (!fresh || fresh.deletedAt || !drivenBy({ request, agent: fresh }, deviceId)) {
|
|
1451
1842
|
return;
|
|
1452
1843
|
}
|
|
1453
1844
|
agent = fresh;
|
|
@@ -1463,132 +1854,46 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1463
1854
|
runId
|
|
1464
1855
|
});
|
|
1465
1856
|
}
|
|
1466
|
-
const
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
}
|
|
1472
|
-
catch (error) {
|
|
1473
|
-
if (controller.signal.aborted) {
|
|
1474
|
-
pendingLease
|
|
1475
|
-
.then((lateLease) => runLog.release(runId, lateLease.generation))
|
|
1476
|
-
.catch((releaseError) => this._log?.warn('runs', 'late run-log begin cleanup failed', {
|
|
1477
|
-
runId,
|
|
1478
|
-
error: releaseError
|
|
1479
|
-
}));
|
|
1480
|
-
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1481
|
-
if (outcome.kind === 'replay') {
|
|
1482
|
-
this._seen.delete(runId);
|
|
1483
|
-
}
|
|
1484
|
-
if (outcome.kind === 'failed') {
|
|
1485
|
-
await this._recordRunBoundaryStatus(engine, {
|
|
1486
|
-
request,
|
|
1487
|
-
runId,
|
|
1488
|
-
agent,
|
|
1489
|
-
deviceId,
|
|
1490
|
-
state: 'failed',
|
|
1491
|
-
reason: outcome.reason
|
|
1492
|
-
});
|
|
1493
|
-
}
|
|
1494
|
-
return;
|
|
1495
|
-
}
|
|
1496
|
-
await this._recordRunBoundaryStatus(engine, {
|
|
1497
|
-
request,
|
|
1857
|
+
const steeredInto = this._steered.get(runId);
|
|
1858
|
+
if (steeredInto) {
|
|
1859
|
+
this._steered.delete(runId);
|
|
1860
|
+
const steered = new RunTranscript(engine, {
|
|
1861
|
+
chatId,
|
|
1498
1862
|
runId,
|
|
1499
|
-
agent,
|
|
1500
1863
|
deviceId,
|
|
1501
|
-
|
|
1502
|
-
|
|
1864
|
+
model: agent.modelName,
|
|
1865
|
+
log: this._log
|
|
1503
1866
|
});
|
|
1867
|
+
steered.close();
|
|
1868
|
+
await steered.status({ state: 'completed', reason: STEERED, metrics: { steeredInto } });
|
|
1504
1869
|
return;
|
|
1505
1870
|
}
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1871
|
+
const hold = await this._openRunLog(engine, runId);
|
|
1872
|
+
const runLog = hold?.runLog;
|
|
1873
|
+
const resumed = Boolean(hold?.lease.boot?.metadata);
|
|
1874
|
+
const releaseRunLog = async () => {
|
|
1875
|
+
await hold?.runLog
|
|
1876
|
+
.release(runId, hold.lease.generation)
|
|
1877
|
+
.catch((err) => this._log?.warn('runs', 'run log not released', { runId, err }));
|
|
1878
|
+
};
|
|
1879
|
+
if (!resumed) {
|
|
1880
|
+
await this._reconcileCompaction(engine, chatId);
|
|
1881
|
+
if (request.parentId) {
|
|
1882
|
+
await this._deleteChatKvCache(chatId, 'superseding run');
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
const model = runnableModelName(agent.modelName, await this._runnableHost());
|
|
1886
|
+
if (model !== agent.modelName) {
|
|
1887
|
+
this._log?.warn('runs', 'model clamped to what this device can run', {
|
|
1512
1888
|
runId,
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
});
|
|
1517
|
-
}
|
|
1518
|
-
catch (error) {
|
|
1519
|
-
if (controller.signal.aborted) {
|
|
1520
|
-
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1521
|
-
if (outcome.kind === 'replay') {
|
|
1522
|
-
this._seen.delete(runId);
|
|
1523
|
-
return;
|
|
1524
|
-
}
|
|
1525
|
-
if (outcome.kind === 'handoff') {
|
|
1526
|
-
return;
|
|
1527
|
-
}
|
|
1528
|
-
await this._recordRunBoundaryStatus(engine, {
|
|
1529
|
-
request,
|
|
1530
|
-
runId,
|
|
1531
|
-
agent,
|
|
1532
|
-
deviceId,
|
|
1533
|
-
state: 'failed',
|
|
1534
|
-
reason: outcome.reason
|
|
1535
|
-
});
|
|
1536
|
-
return;
|
|
1537
|
-
}
|
|
1538
|
-
await this._recordRunBoundaryStatus(engine, {
|
|
1539
|
-
request,
|
|
1540
|
-
runId,
|
|
1541
|
-
agent,
|
|
1542
|
-
deviceId,
|
|
1543
|
-
state: hostLease.boot?.metadata ? 'recovery-required' : 'failed',
|
|
1544
|
-
reason: runFailureNotice(error)
|
|
1545
|
-
});
|
|
1546
|
-
}
|
|
1547
|
-
finally {
|
|
1548
|
-
await runLog
|
|
1549
|
-
.release(runId, hostLease.generation)
|
|
1550
|
-
.catch((error) => this._log?.warn('runs', 'run-log lease release failed', { runId, error }));
|
|
1551
|
-
}
|
|
1552
|
-
}
|
|
1553
|
-
async _executePrimary({ request, agent, controller, meshId, runId, engine, runLog, hostLease }) {
|
|
1554
|
-
const { chatId } = request;
|
|
1555
|
-
const deviceId = this._requireDeviceId();
|
|
1556
|
-
const { boot } = hostLease;
|
|
1557
|
-
const resumed = Boolean(boot?.metadata);
|
|
1558
|
-
const savedRequest = resumed && boot ? asRecord(requestFromBoot(boot)) : null;
|
|
1559
|
-
if (resumed) {
|
|
1560
|
-
const { chunks } = await engine.listChunks({ chatId, type: 'run-request' });
|
|
1561
|
-
const persistedRequest = (chunks ?? []).find((chunk) => chunk.id === request.id);
|
|
1562
|
-
if (!persistedRequest ||
|
|
1563
|
-
persistedRequest.chatId !== chatId ||
|
|
1564
|
-
persistedRequest.runId !== runId ||
|
|
1565
|
-
persistedRequest.seq !== request.seq ||
|
|
1566
|
-
persistedRequest.parentId !== request.parentId ||
|
|
1567
|
-
persistedRequest.runRequest?.agentId !== agent.id) {
|
|
1568
|
-
throw new Error('The original run request is no longer present in its chat history');
|
|
1569
|
-
}
|
|
1570
|
-
}
|
|
1571
|
-
if (!resumed) {
|
|
1572
|
-
await this._reconcileCompaction(engine, chatId);
|
|
1573
|
-
}
|
|
1574
|
-
if (request.parentId && !resumed) {
|
|
1575
|
-
await this._deleteChatKvCache(chatId, 'superseding run');
|
|
1576
|
-
}
|
|
1577
|
-
const lender = this._lenderFor(agent);
|
|
1578
|
-
const model = lender
|
|
1579
|
-
? agent.modelName
|
|
1580
|
-
: runnableModelName(agent.modelName, await this._runnableHost());
|
|
1581
|
-
if (model !== agent.modelName) {
|
|
1582
|
-
this._log?.warn('runs', 'model clamped to what this device can run', {
|
|
1583
|
-
runId,
|
|
1584
|
-
chatId,
|
|
1585
|
-
requested: agent.modelName,
|
|
1586
|
-
model
|
|
1889
|
+
chatId,
|
|
1890
|
+
requested: agent.modelName,
|
|
1891
|
+
model
|
|
1587
1892
|
});
|
|
1588
1893
|
}
|
|
1589
1894
|
const claimed = this._runs.get(runId);
|
|
1590
1895
|
if (claimed) {
|
|
1591
|
-
this._runs.set(runId, { ...claimed, model
|
|
1896
|
+
this._runs.set(runId, { ...claimed, model });
|
|
1592
1897
|
}
|
|
1593
1898
|
const config = await engine.getConfig({}).catch(noop);
|
|
1594
1899
|
const configuredAgent = withConfigDefaults(agent, config);
|
|
@@ -1605,26 +1910,130 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1605
1910
|
model,
|
|
1606
1911
|
log: this._log
|
|
1607
1912
|
});
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1913
|
+
const rounds = new OwnerRounds({
|
|
1914
|
+
engine,
|
|
1915
|
+
chatId,
|
|
1916
|
+
runId,
|
|
1917
|
+
agentId: agent.id,
|
|
1918
|
+
deviceId,
|
|
1919
|
+
from: request.seq,
|
|
1920
|
+
media: (file) => this._mediaRefs.get(path.basename(file)),
|
|
1921
|
+
inspect: (call) => void this._inspectRound(chatId, { runId, at: Date.now(), call })
|
|
1922
|
+
});
|
|
1923
|
+
const folded = !resumed &&
|
|
1924
|
+
(await this._compactIfNearFull({
|
|
1925
|
+
chatId,
|
|
1926
|
+
transcript,
|
|
1927
|
+
trigger: 'before-run',
|
|
1928
|
+
signal: controller.signal,
|
|
1929
|
+
meshId,
|
|
1930
|
+
run: { request, runId },
|
|
1931
|
+
rounds
|
|
1932
|
+
}));
|
|
1933
|
+
const toolConfigsPromise = this._loadToolConfigs(engine, agent.id);
|
|
1934
|
+
const fsGrantsPromise = this._loadFsGrants(agent.id);
|
|
1935
|
+
const all = await listHistoryChunks(engine, chatId);
|
|
1936
|
+
const { chunks: switches } = await engine.listChunks({ chatId, type: 'agent-switch' });
|
|
1937
|
+
const ranAndSwitched = [...all, ...(switches ?? [])].sort((a, b) => a.seq - b.seq);
|
|
1938
|
+
if (!resumed && agentSwitched(ranAndSwitched, request, deviceId)) {
|
|
1939
|
+
await this._deleteChatKvCache(chatId, 'agent switched');
|
|
1940
|
+
}
|
|
1941
|
+
const { chunks: priorAsks } = await engine.listChunks({
|
|
1942
|
+
chatId,
|
|
1943
|
+
type: 'approval-request'
|
|
1944
|
+
});
|
|
1945
|
+
const { history, stale, repair: currentRepair, acceptedRepairs } = runPromptView(all, priorAsks ?? [], { request, runId });
|
|
1946
|
+
const priorResults = all.filter((chunk) => chunk.type === 'tool-result');
|
|
1947
|
+
let ran = priorResults.some((chunk) => chunk.runId === runId &&
|
|
1948
|
+
chunk.toolResult?.name !== SKILL_TOOL &&
|
|
1949
|
+
!isDeniedResult(chunk.toolResult?.result));
|
|
1950
|
+
let replay = await this._sentRuns(chatId, await listRoundChunks(engine, chatId));
|
|
1951
|
+
if (!resumed && viewOutgrewCache(all, { request, runId }, replay.sentRuns)) {
|
|
1952
|
+
await this._deleteChatKvCache(chatId, 'a run outside the cached view');
|
|
1953
|
+
replay = {
|
|
1954
|
+
sentRuns: new Map(),
|
|
1955
|
+
askedRuns: new Map(),
|
|
1956
|
+
readRuns: new Map(),
|
|
1957
|
+
rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
|
|
1958
|
+
};
|
|
1959
|
+
}
|
|
1960
|
+
const messages = historyFromChunks(history, stale, replay);
|
|
1961
|
+
const typedText = currentRepair
|
|
1962
|
+
? undefined
|
|
1963
|
+
: history.findLast((chunk) => chunk.userMessage)?.userMessage?.text;
|
|
1964
|
+
if (currentRepair) {
|
|
1965
|
+
messages.push({ role: 'user', content: currentRepair.promptText });
|
|
1966
|
+
}
|
|
1967
|
+
const diagramRepair = currentRepair !== undefined;
|
|
1968
|
+
const runAgent = diagramRepair
|
|
1969
|
+
? {
|
|
1970
|
+
...configuredAgent,
|
|
1971
|
+
toolsScope: 'selected',
|
|
1972
|
+
enabledTools: [],
|
|
1973
|
+
skillsScope: 'selected',
|
|
1974
|
+
enabledSkills: []
|
|
1975
|
+
}
|
|
1976
|
+
: configuredAgent;
|
|
1977
|
+
const carriesMedia = messages.some((turn) => turn.images?.length || turn.pdfs?.length || turn.videos?.length);
|
|
1978
|
+
const seesImages = carriesMedia && (await this._seesImages(model));
|
|
1979
|
+
let unreadableImages = new Map();
|
|
1980
|
+
const sampled = [];
|
|
1981
|
+
rounds.share = async (agentId) => {
|
|
1982
|
+
const host = await engine.getAgent({ agentId }).catch(() => null);
|
|
1983
|
+
if (sampled.length && host && !b4a.equals(host.providerDeviceId, deviceId)) {
|
|
1984
|
+
await this._storeVideoDigests(sampled.splice(0), { meshId, chatId, runId, deviceId });
|
|
1985
|
+
}
|
|
1986
|
+
};
|
|
1987
|
+
let pdfPages = { rendered: new Set(), notices: [] };
|
|
1988
|
+
const selectedPdfs = [];
|
|
1989
|
+
if (!seesImages) {
|
|
1990
|
+
const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
|
|
1991
|
+
if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
|
|
1992
|
+
this._log?.warn('assistant', `no projection pairing for ${model} — dropped ${dropped} image(s)`);
|
|
1993
|
+
}
|
|
1994
|
+
for (const turn of messages) {
|
|
1995
|
+
if (turn.images?.length) {
|
|
1996
|
+
const names = turn.images.map((image) => image.fileName).join(', ');
|
|
1997
|
+
turn.content += `\n[Attached image${turn.images.length > 1 ? 's' : ''}: ${names} — not visible to this model]`;
|
|
1998
|
+
}
|
|
1999
|
+
delete turn.images;
|
|
2000
|
+
delete turn.pdfs;
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
else {
|
|
2004
|
+
unreadableImages = await this._hydrateImages(messages, controller.signal, runId);
|
|
2005
|
+
}
|
|
2006
|
+
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
2007
|
+
const ownershipWatch = this._watchOwnership(engine, { request, agentId: agent.id, controller });
|
|
2008
|
+
let readinessError = null;
|
|
2009
|
+
const held = { status: null };
|
|
2010
|
+
let shown = '';
|
|
2011
|
+
const showWait = (progress) => {
|
|
2012
|
+
const key = JSON.stringify(progress);
|
|
2013
|
+
if (key === shown || (!progress && !held.status)) {
|
|
1616
2014
|
return;
|
|
1617
2015
|
}
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
2016
|
+
shown = key;
|
|
2017
|
+
const runStatus = progress
|
|
2018
|
+
? { state: 'executing', progress }
|
|
2019
|
+
: { state: 'executing' };
|
|
2020
|
+
if (!held.status) {
|
|
2021
|
+
held.status = transcript.status(runStatus, { resumed });
|
|
2022
|
+
return;
|
|
2023
|
+
}
|
|
2024
|
+
held.status.then(({ id }) => transcript.progress(id, runStatus)).catch(noop);
|
|
1626
2025
|
};
|
|
1627
|
-
|
|
2026
|
+
try {
|
|
2027
|
+
await this._downloads?.waitUntilCached(model, controller.signal, {
|
|
2028
|
+
onHold: () => showWait({ phase: 'awaiting-download', model }),
|
|
2029
|
+
onProgress: (percentage) => showWait(downloadingProgress(model, percentage)),
|
|
2030
|
+
onStall: () => showWait(null)
|
|
2031
|
+
});
|
|
2032
|
+
}
|
|
2033
|
+
catch (err) {
|
|
2034
|
+
readinessError = err instanceof Error ? err : new Error(String(err));
|
|
2035
|
+
}
|
|
2036
|
+
if (controller.signal.aborted) {
|
|
1628
2037
|
try {
|
|
1629
2038
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1630
2039
|
if (outcome.kind === 'replay') {
|
|
@@ -1635,603 +2044,539 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1635
2044
|
}
|
|
1636
2045
|
}
|
|
1637
2046
|
finally {
|
|
1638
|
-
|
|
2047
|
+
transcript.close();
|
|
2048
|
+
ownershipWatch.stop();
|
|
2049
|
+
settledWatch.stop();
|
|
2050
|
+
await releaseRunLog();
|
|
2051
|
+
}
|
|
2052
|
+
return;
|
|
2053
|
+
}
|
|
2054
|
+
if (readinessError) {
|
|
2055
|
+
try {
|
|
2056
|
+
await transcript.status({ state: 'failed', reason: runFailureNotice(readinessError) });
|
|
2057
|
+
}
|
|
2058
|
+
finally {
|
|
2059
|
+
transcript.close();
|
|
2060
|
+
ownershipWatch.stop();
|
|
2061
|
+
settledWatch.stop();
|
|
2062
|
+
await releaseRunLog();
|
|
2063
|
+
}
|
|
2064
|
+
return;
|
|
2065
|
+
}
|
|
2066
|
+
const inflight = this._runs.get(runId);
|
|
2067
|
+
if (inflight) {
|
|
2068
|
+
this._runs.set(runId, { ...inflight, executing: true });
|
|
2069
|
+
}
|
|
2070
|
+
this._runsChanged();
|
|
2071
|
+
const busyChatId = this._modelHolder(runId, chatId, model);
|
|
2072
|
+
const runStatus = busyChatId
|
|
2073
|
+
? { state: 'executing', progress: { phase: 'queued', busyChatId } }
|
|
2074
|
+
: { state: 'executing' };
|
|
2075
|
+
const runStatusId = (await (held.status ?? transcript.status(runStatus, { resumed }))).id;
|
|
2076
|
+
if (held.status) {
|
|
2077
|
+
await transcript.progress(runStatusId, runStatus);
|
|
2078
|
+
}
|
|
2079
|
+
const approvals = this._createApprove(engine, {
|
|
2080
|
+
chatId,
|
|
2081
|
+
runId,
|
|
2082
|
+
deviceId,
|
|
2083
|
+
agentId: agent.id,
|
|
2084
|
+
hasRun: () => ran
|
|
2085
|
+
});
|
|
2086
|
+
const idle = idleWatch(controller, this._runIdleTimeoutMs);
|
|
2087
|
+
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
|
|
2088
|
+
let metrics;
|
|
2089
|
+
const sourceTagFilter = createRagSourceTagFilter();
|
|
2090
|
+
const retrievedRecords = [];
|
|
2091
|
+
const answers = new Map();
|
|
2092
|
+
let round;
|
|
2093
|
+
const answer = (text) => {
|
|
2094
|
+
answers.set(round, (answers.get(round) ?? '') + text);
|
|
2095
|
+
};
|
|
2096
|
+
const flushTranscript = async () => {
|
|
2097
|
+
const tail = sourceTagFilter.flush();
|
|
2098
|
+
if (tail) {
|
|
2099
|
+
answer(tail);
|
|
2100
|
+
await transcript.text('content', tail, round);
|
|
1639
2101
|
}
|
|
2102
|
+
await transcript.flush();
|
|
1640
2103
|
};
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
2104
|
+
let titleOutcome = null;
|
|
2105
|
+
let runFailure = null;
|
|
2106
|
+
let runVerdict = 'completed';
|
|
2107
|
+
let sentRun = null;
|
|
2108
|
+
let steering = null;
|
|
2109
|
+
const taken = new Set();
|
|
2110
|
+
let inputLength = 0;
|
|
2111
|
+
let asked;
|
|
2112
|
+
let primeRound;
|
|
2113
|
+
let primeTokens;
|
|
2114
|
+
let injectedHits = 0;
|
|
2115
|
+
const writeText = async (type, raw) => {
|
|
2116
|
+
const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
|
|
2117
|
+
if (!text) {
|
|
1648
2118
|
return;
|
|
1649
2119
|
}
|
|
1650
|
-
if (
|
|
1651
|
-
|
|
2120
|
+
if (type === 'content') {
|
|
2121
|
+
answer(text);
|
|
2122
|
+
}
|
|
2123
|
+
await transcript.text(type, text, round);
|
|
2124
|
+
};
|
|
2125
|
+
const writeRoundText = async (type, text) => {
|
|
2126
|
+
if (!rounds.echoed(type, text)) {
|
|
2127
|
+
await writeText(type, text);
|
|
2128
|
+
}
|
|
2129
|
+
else if (type === 'content') {
|
|
2130
|
+
answer(text);
|
|
2131
|
+
}
|
|
2132
|
+
};
|
|
2133
|
+
const failRun = async (message) => {
|
|
2134
|
+
const overflowed = isContextOverflowFailure(message);
|
|
2135
|
+
const folded = overflowed &&
|
|
2136
|
+
(await this._runCompaction({
|
|
1652
2137
|
chatId,
|
|
1653
2138
|
transcript,
|
|
1654
|
-
trigger: '
|
|
2139
|
+
trigger: 'overflow',
|
|
1655
2140
|
signal: controller.signal,
|
|
1656
|
-
meshId
|
|
1657
|
-
|
|
2141
|
+
meshId,
|
|
2142
|
+
run: { request, runId },
|
|
2143
|
+
rounds
|
|
2144
|
+
}));
|
|
2145
|
+
const shrunk = overflowed && !folded && injectedHits > 1;
|
|
2146
|
+
if (shrunk) {
|
|
2147
|
+
this._knowledgeCaps.set(chatId, Math.floor(injectedHits / 2));
|
|
2148
|
+
this._log?.info('runs', 'knowledge shrunk for retry', { runId, hits: injectedHits });
|
|
2149
|
+
}
|
|
2150
|
+
runFailure = runFailureNotice(message);
|
|
2151
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
2152
|
+
if (folded || shrunk) {
|
|
2153
|
+
await engine
|
|
2154
|
+
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
2155
|
+
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
2156
|
+
}
|
|
2157
|
+
titleOutcome = 'ended';
|
|
2158
|
+
};
|
|
2159
|
+
const endAbortedRun = async () => {
|
|
2160
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
2161
|
+
if (outcome.kind === 'replay') {
|
|
2162
|
+
runVerdict = 'replay';
|
|
2163
|
+
this._seen.delete(runId);
|
|
2164
|
+
return;
|
|
1658
2165
|
}
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
void fsGrantsPromise.catch(noop);
|
|
1663
|
-
const all = resumed ? [] : await listHistoryChunks(engine, chatId);
|
|
1664
|
-
const branches = historyBranches(all, runId);
|
|
1665
|
-
const currentRepair = branches.repairByRunId.get(runId);
|
|
1666
|
-
const repairParent = currentRepair
|
|
1667
|
-
? all.find((chunk) => chunk.id === currentRepair.parentId)
|
|
1668
|
-
: undefined;
|
|
1669
|
-
const acceptedRepairs = acceptedRepairCount(branches, runId);
|
|
1670
|
-
const history = all.filter((chunk) => !!chunk.compaction ||
|
|
1671
|
-
(chunk.runId !== runId &&
|
|
1672
|
-
inputAvailableToRun(chunk, request.seq) &&
|
|
1673
|
-
(!repairParent ||
|
|
1674
|
-
chunk.seq <= repairParent.seq ||
|
|
1675
|
-
chunk.runId === repairParent.runId) &&
|
|
1676
|
-
isPromptChunk(chunk, branches)));
|
|
1677
|
-
const { chunks: priorAsks } = await engine.listChunks({
|
|
1678
|
-
chatId,
|
|
1679
|
-
type: 'approval-request'
|
|
1680
|
-
});
|
|
1681
|
-
const { chunks: priorResults } = await engine.listChunks({ chatId, type: 'tool-result' });
|
|
1682
|
-
let ran = (priorResults ?? []).some((chunk) => chunk.runId === runId &&
|
|
1683
|
-
chunk.toolResult?.name !== SKILL_TOOL &&
|
|
1684
|
-
!isDeniedResult(chunk.toolResult?.result));
|
|
1685
|
-
const named = couldNameSenders(history);
|
|
1686
|
-
const senders = named ? await this._senderNames(engine, history) : NO_SENDERS;
|
|
1687
|
-
if (!resumed && (named || this._chatAttribution.get(chatId))) {
|
|
1688
|
-
await this._reconcileAttribution(chatId, senders);
|
|
1689
|
-
}
|
|
1690
|
-
const messages = resumed
|
|
1691
|
-
? savedMessagesOf(savedRequest)
|
|
1692
|
-
: historyFromChunks(history, staleToolRunIds(history, priorAsks ?? [], runId), senders);
|
|
1693
|
-
if (currentRepair) {
|
|
1694
|
-
messages.push({ role: 'user', content: currentRepair.promptText });
|
|
1695
|
-
}
|
|
1696
|
-
let diagramRepair = currentRepair !== undefined;
|
|
1697
|
-
let runAgent = diagramRepair
|
|
1698
|
-
? {
|
|
1699
|
-
...configuredAgent,
|
|
1700
|
-
toolsScope: 'selected',
|
|
1701
|
-
enabledTools: [],
|
|
1702
|
-
skillsScope: 'selected',
|
|
1703
|
-
enabledSkills: []
|
|
1704
|
-
}
|
|
1705
|
-
: configuredAgent;
|
|
1706
|
-
if (resumed && savedRequest && isRecord(savedRequest.agent)) {
|
|
1707
|
-
const savedAgent = savedRequest.agent;
|
|
1708
|
-
const repairAgent = {
|
|
1709
|
-
...configuredAgent,
|
|
1710
|
-
toolsScope: 'selected',
|
|
1711
|
-
enabledTools: [],
|
|
1712
|
-
skillsScope: 'selected',
|
|
1713
|
-
enabledSkills: []
|
|
1714
|
-
};
|
|
1715
|
-
if (!sameSerializedValue(savedAgent, configuredAgent) &&
|
|
1716
|
-
sameSerializedValue(savedAgent, repairAgent)) {
|
|
1717
|
-
runAgent = repairAgent;
|
|
1718
|
-
diagramRepair = true;
|
|
1719
|
-
}
|
|
1720
|
-
}
|
|
1721
|
-
const seesImages = this._harness.supportsImagesFor
|
|
1722
|
-
? this._harness.supportsImagesFor(model)
|
|
1723
|
-
: (this._harness.supportsImages ?? this._info.supportsImages) !== false;
|
|
1724
|
-
let unreadableImages = new Map();
|
|
1725
|
-
let renderedPdfIds = new Set();
|
|
1726
|
-
if (resumed) {
|
|
1727
|
-
unreadableImages = new Map();
|
|
1728
|
-
}
|
|
1729
|
-
else if (!seesImages) {
|
|
1730
|
-
const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
|
|
1731
|
-
if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
|
|
1732
|
-
this._log?.warn('assistant', `no projection pairing for ${model} — dropped ${dropped} image(s)`);
|
|
1733
|
-
}
|
|
1734
|
-
stripUnseenImages(messages);
|
|
2166
|
+
if (outcome.kind === 'handoff') {
|
|
2167
|
+
runVerdict = 'handoff';
|
|
2168
|
+
return;
|
|
1735
2169
|
}
|
|
1736
|
-
|
|
1737
|
-
|
|
2170
|
+
runFailure = outcome.reason;
|
|
2171
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
2172
|
+
titleOutcome = 'ended';
|
|
2173
|
+
};
|
|
2174
|
+
const pauseRun = async () => {
|
|
2175
|
+
if (this._abortOutcome(runId, controller.signal.reason).kind === 'replay') {
|
|
2176
|
+
await transcript.status({ state: 'paused', reason: RUN_PAUSED });
|
|
1738
2177
|
}
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
preflightOwnershipWatch = ownershipWatch;
|
|
1743
|
-
let readinessError = null;
|
|
1744
|
-
try {
|
|
1745
|
-
if (!lender && !resumed) {
|
|
1746
|
-
await this._downloads?.waitUntilCached(model, controller.signal);
|
|
1747
|
-
}
|
|
2178
|
+
await endAbortedRun();
|
|
2179
|
+
if (runVerdict === 'replay') {
|
|
2180
|
+
runVerdict = 'paused';
|
|
1748
2181
|
}
|
|
1749
|
-
|
|
1750
|
-
|
|
2182
|
+
};
|
|
2183
|
+
const resetRound = async (event) => {
|
|
2184
|
+
round = roundOf(event);
|
|
2185
|
+
const replaced = replacedTypes(event);
|
|
2186
|
+
if (replaced.includes('content')) {
|
|
2187
|
+
answers.delete(round);
|
|
1751
2188
|
}
|
|
2189
|
+
if (!rounds.retook()) {
|
|
2190
|
+
await transcript.roundReset(round ?? 0, replaced);
|
|
2191
|
+
}
|
|
2192
|
+
};
|
|
2193
|
+
const holdForRecovery = async (event) => {
|
|
2194
|
+
runVerdict = 'recovery-required';
|
|
2195
|
+
await transcript.status({ state: 'recovery-required', reason: recoveryReason(event) });
|
|
2196
|
+
titleOutcome = 'ended';
|
|
2197
|
+
};
|
|
2198
|
+
const recordToolResult = async (event) => {
|
|
2199
|
+
if (isDeniedResult(event.result)) {
|
|
2200
|
+
this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
|
|
2201
|
+
}
|
|
2202
|
+
else if (event.name !== SKILL_TOOL) {
|
|
2203
|
+
ran = true;
|
|
2204
|
+
}
|
|
2205
|
+
if (WEB_TOOLS.includes(String(event.name))) {
|
|
2206
|
+
await this._citeWebSources(transcript, runId, event.result);
|
|
2207
|
+
}
|
|
2208
|
+
await transcript.chunk(toolResultChunk(event));
|
|
2209
|
+
};
|
|
2210
|
+
try {
|
|
2211
|
+
const attachTurnIds = resumed ? [] : attachTurnDocumentIds(history);
|
|
2212
|
+
if (attachTurnIds.length > 0) {
|
|
2213
|
+
backfillDocumentOnlyTurn(messages);
|
|
2214
|
+
}
|
|
2215
|
+
const knowledge = !resumed &&
|
|
2216
|
+
!diagramRepair &&
|
|
2217
|
+
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
2218
|
+
? await this._searchKnowledge(engine, messages, {
|
|
2219
|
+
chatId,
|
|
2220
|
+
attachmentIds: attachTurnIds,
|
|
2221
|
+
signal: controller.signal,
|
|
2222
|
+
preparing: (active) => {
|
|
2223
|
+
this._markPreparing(runId, active);
|
|
2224
|
+
modelWatch.documents(active);
|
|
2225
|
+
}
|
|
2226
|
+
})
|
|
2227
|
+
: null;
|
|
1752
2228
|
if (controller.signal.aborted) {
|
|
1753
|
-
|
|
2229
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
2230
|
+
if (outcome.kind === 'replay') {
|
|
2231
|
+
runVerdict = 'replay';
|
|
2232
|
+
this._seen.delete(runId);
|
|
2233
|
+
return;
|
|
2234
|
+
}
|
|
2235
|
+
if (outcome.kind === 'handoff') {
|
|
2236
|
+
runVerdict = 'handoff';
|
|
2237
|
+
return;
|
|
2238
|
+
}
|
|
2239
|
+
runFailure = outcome.reason;
|
|
2240
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1754
2241
|
return;
|
|
1755
2242
|
}
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
2243
|
+
const cap = this._knowledgeCaps.get(chatId);
|
|
2244
|
+
this._knowledgeCaps.delete(chatId);
|
|
2245
|
+
const fitted = knowledge && cap ? { ...knowledge, hits: knowledge.hits.slice(0, cap) } : knowledge;
|
|
2246
|
+
const injected = fitted
|
|
2247
|
+
? await this._injectKnowledge(fitted, {
|
|
2248
|
+
transcript,
|
|
2249
|
+
sourceTagFilter,
|
|
2250
|
+
retrievedRecords,
|
|
2251
|
+
pictured: seesImages && messages.some((turn) => turn.pdfs?.length)
|
|
2252
|
+
})
|
|
2253
|
+
: { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
2254
|
+
injectedHits = injected.ragInjected && fitted ? fitted.hits.length : 0;
|
|
2255
|
+
const hasAudio = messages.some((turn) => turn.audioAttachments?.length || turn.videos?.length);
|
|
2256
|
+
if (hasAudio) {
|
|
2257
|
+
modelWatch.transcribe(true);
|
|
2258
|
+
}
|
|
2259
|
+
await this._hydrateAudio(messages, controller.signal);
|
|
2260
|
+
if (messages.some((turn) => turn.videos?.length)) {
|
|
2261
|
+
await this._hydrateVideos(messages, {
|
|
2262
|
+
signal: controller.signal,
|
|
2263
|
+
runId,
|
|
2264
|
+
seesImages,
|
|
2265
|
+
reading: await this._contextReading({ chatId, meshId, folded }, configuredAgent.ctxSize),
|
|
2266
|
+
sampled
|
|
2267
|
+
});
|
|
2268
|
+
}
|
|
2269
|
+
if (hasAudio) {
|
|
2270
|
+
modelWatch.transcribe(false);
|
|
2271
|
+
}
|
|
2272
|
+
backfillImageOnlyTurns(messages);
|
|
2273
|
+
if (seesImages) {
|
|
2274
|
+
const hits = injected.documentsInjected && fitted ? fitted.hits : [];
|
|
2275
|
+
pdfPages = await this._hydratePdfPages(messages, {
|
|
2276
|
+
signal: controller.signal,
|
|
2277
|
+
runId,
|
|
2278
|
+
window: { ctxSize: configuredAgent.ctxSize, modelName: model },
|
|
2279
|
+
reading: { chatId, meshId, folded },
|
|
2280
|
+
textChars: hits.reduce((chars, hit) => chars + hit.content.length, 0),
|
|
2281
|
+
wholeIds: new Set(hits
|
|
2282
|
+
.filter((hit) => !isTruncatedFullTextHit(hit))
|
|
2283
|
+
.map(fullTextAttachmentId)
|
|
2284
|
+
.filter((id) => id !== null)),
|
|
2285
|
+
selected: selectedPdfs
|
|
2286
|
+
});
|
|
2287
|
+
}
|
|
2288
|
+
annotateDocumentAttachments(messages);
|
|
2289
|
+
for (const { turn, notice } of pdfPages.notices) {
|
|
2290
|
+
turn.content = `${turn.content}\n${notice}`;
|
|
2291
|
+
}
|
|
2292
|
+
const unreadIds = attachTurnIds.filter((id) => !pdfPages.rendered.has(id));
|
|
2293
|
+
if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
|
|
2294
|
+
const reason = documentUnreadReason({
|
|
2295
|
+
knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
|
|
2296
|
+
failedFiles: knowledge?.search?.failedFiles,
|
|
2297
|
+
attachedCount: unreadIds.length
|
|
2298
|
+
});
|
|
2299
|
+
const lastUser = messages.findLast((turn) => turn.role === 'user');
|
|
2300
|
+
if (lastUser) {
|
|
2301
|
+
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
|
|
1759
2302
|
}
|
|
1760
|
-
|
|
1761
|
-
await
|
|
2303
|
+
if (!injected.sourcesWritten) {
|
|
2304
|
+
await this._writeUnreadNotice(transcript, reason, knowledge?.search);
|
|
1762
2305
|
}
|
|
2306
|
+
}
|
|
2307
|
+
annotateUnreadableImages(unreadableImages);
|
|
2308
|
+
const toolConfigs = await toolConfigsPromise;
|
|
2309
|
+
const { roots, write } = await fsGrantsPromise;
|
|
2310
|
+
const { grantedDirs, grantedDrives, driveOp } = await resolveFsGrants(roots, this._drives, (key, err) => this._log?.warn('assistant', 'drive grant dropped', {
|
|
2311
|
+
key,
|
|
2312
|
+
error: errorMessage(err)
|
|
2313
|
+
}));
|
|
2314
|
+
if (await this._isHandled(engine, request)) {
|
|
1763
2315
|
return;
|
|
1764
2316
|
}
|
|
2317
|
+
const cacheKey = diagramRepair || stale.runIds.size > 0
|
|
2318
|
+
? undefined
|
|
2319
|
+
: chatRepairKvCacheKey(chatId, acceptedRepairs);
|
|
2320
|
+
sentRun = trackSentRun(cacheKey);
|
|
2321
|
+
const unloading = this._unloading.get(model);
|
|
2322
|
+
if (unloading) {
|
|
2323
|
+
await raceAbort(unloading.catch(noop), controller.signal);
|
|
2324
|
+
}
|
|
2325
|
+
this._log?.info('runs', 'completion requested', {
|
|
2326
|
+
runId,
|
|
2327
|
+
model,
|
|
2328
|
+
cacheKey,
|
|
2329
|
+
turns: messages.length
|
|
2330
|
+
});
|
|
2331
|
+
const resent = loggedMessages(hold?.lease.boot);
|
|
2332
|
+
if (resent) {
|
|
2333
|
+
asked = askedIn(resent, messages.findLast((turn) => turn.role === 'user')?.sender);
|
|
2334
|
+
}
|
|
2335
|
+
else {
|
|
2336
|
+
restoreSentContent(messages);
|
|
2337
|
+
asked = messages.findLast((turn) => turn.role === 'user')?.content;
|
|
2338
|
+
}
|
|
2339
|
+
const input = resent ?? nameSenders(messages);
|
|
2340
|
+
inputLength = input.length;
|
|
2341
|
+
rounds.inputLength = inputLength;
|
|
2342
|
+
const sentImages = earlierImages(messages);
|
|
2343
|
+
const inspecting = await this._capturingInferenceCalls();
|
|
2344
|
+
steering = new RunInputQueue();
|
|
1765
2345
|
const inflight = this._runs.get(runId);
|
|
1766
2346
|
if (inflight) {
|
|
1767
|
-
|
|
2347
|
+
inflight.steering = steering;
|
|
1768
2348
|
}
|
|
1769
|
-
|
|
1770
|
-
const
|
|
1771
|
-
let runStatusId;
|
|
1772
|
-
let resumeAccepted = !resumed;
|
|
1773
|
-
if (!resumed) {
|
|
1774
|
-
try {
|
|
1775
|
-
const executing = await transcript.status(busyChatId
|
|
1776
|
-
? { state: 'executing', progress: { phase: 'queued', busyChatId } }
|
|
1777
|
-
: { state: 'executing' });
|
|
1778
|
-
runStatusId = executing.id;
|
|
1779
|
-
}
|
|
1780
|
-
catch (error) {
|
|
1781
|
-
await cleanupPreflight();
|
|
1782
|
-
throw error;
|
|
1783
|
-
}
|
|
1784
|
-
}
|
|
1785
|
-
const approvals = this._createApprove(engine, {
|
|
1786
|
-
chatId,
|
|
2349
|
+
idle.touch();
|
|
2350
|
+
const events = this._harness.completion({
|
|
1787
2351
|
runId,
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
2352
|
+
runLog,
|
|
2353
|
+
model,
|
|
2354
|
+
agent: runAgent,
|
|
2355
|
+
messages: input,
|
|
2356
|
+
typedText,
|
|
2357
|
+
cacheKey,
|
|
2358
|
+
replaysContext: true,
|
|
2359
|
+
deferTools: runAgent.shared === true || !!request.requesterMemberId,
|
|
2360
|
+
modelRound: idle.parks(rounds.port),
|
|
2361
|
+
session: chatKvCacheKey(chatId),
|
|
2362
|
+
thinkingDefault: thinkingDefault(config),
|
|
2363
|
+
toolConfigs,
|
|
2364
|
+
grantedDirs,
|
|
2365
|
+
grantedDrives,
|
|
2366
|
+
driveOp,
|
|
2367
|
+
grantedWriteDirs: write,
|
|
2368
|
+
approve: idle.parks(approvals.approve),
|
|
2369
|
+
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
2370
|
+
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
2371
|
+
progress: (update) => {
|
|
2372
|
+
idle.touch();
|
|
2373
|
+
modelWatch.image(update);
|
|
2374
|
+
},
|
|
2375
|
+
generationParams: diagramRepair ? { reasoning_budget: 0 } : undefined,
|
|
2376
|
+
captureInferenceCalls: true,
|
|
2377
|
+
input: steering,
|
|
2378
|
+
signal: controller.signal
|
|
1791
2379
|
});
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
answerTextByRound.set(round, (answerTextByRound.get(round) ?? '') + text);
|
|
1802
|
-
};
|
|
1803
|
-
const flushTranscript = async () => {
|
|
1804
|
-
const tail = sourceTagFilter.flush();
|
|
1805
|
-
if (tail) {
|
|
1806
|
-
appendAnswer(activeRound, tail);
|
|
1807
|
-
await transcript.text('content', tail, activeRound);
|
|
2380
|
+
let harnessEvents = 0;
|
|
2381
|
+
for await (const event of events) {
|
|
2382
|
+
harnessEvents++;
|
|
2383
|
+
idle.touch();
|
|
2384
|
+
modelWatch.clear();
|
|
2385
|
+
if (isHarnessEvent(event, 'content') || isHarnessEvent(event, 'thinking')) {
|
|
2386
|
+
round = roundOf(event) ?? round;
|
|
2387
|
+
await writeRoundText(event.type, event.text);
|
|
2388
|
+
continue;
|
|
1808
2389
|
}
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
2390
|
+
if (event.type === 'run-started' ||
|
|
2391
|
+
event.type === 'run-resumed' ||
|
|
2392
|
+
event.type === 'context') {
|
|
2393
|
+
continue;
|
|
2394
|
+
}
|
|
2395
|
+
if (event.type === 'input' && typeof event.id === 'string') {
|
|
2396
|
+
taken.add(event.id);
|
|
2397
|
+
continue;
|
|
2398
|
+
}
|
|
2399
|
+
if (isHarnessEvent(event, 'metrics')) {
|
|
2400
|
+
const { type, ...rest } = event;
|
|
2401
|
+
metrics = rest;
|
|
2402
|
+
primeTokens = rest.rounds?.find((each) => each.round === primeRound)?.promptTokens;
|
|
2403
|
+
recordBackends(this._backendUse, rest.backends);
|
|
2404
|
+
this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
|
|
2405
|
+
continue;
|
|
2406
|
+
}
|
|
2407
|
+
if (event.type === 'inference-sent' && event.outcome === 'overflowed') {
|
|
2408
|
+
this._log?.info('runs', 'context overflow', { runId, reason: event.reason });
|
|
2409
|
+
}
|
|
2410
|
+
if (event.type === 'inference-sent') {
|
|
2411
|
+
this._inspectSent(inspecting, chatId, {
|
|
2412
|
+
runId,
|
|
2413
|
+
round: Number(event.round ?? 0),
|
|
2414
|
+
messages: event.messages,
|
|
2415
|
+
tokens: event.tokens,
|
|
2416
|
+
outcome: event.outcome,
|
|
2417
|
+
reason: event.reason
|
|
2418
|
+
});
|
|
2419
|
+
continue;
|
|
2420
|
+
}
|
|
2421
|
+
if (event.type === 'inference-call') {
|
|
2422
|
+
const call = asRecord(event.call);
|
|
2423
|
+
sentRun?.observe(call);
|
|
2424
|
+
primeRound ??= sentRun?.onKey ? Number(call.round ?? 0) : undefined;
|
|
2425
|
+
modelWatch.request(call.messages, sentImages);
|
|
2426
|
+
this._inspectCall(inspecting, chatId, { runId, at: Date.now(), call });
|
|
2427
|
+
continue;
|
|
2428
|
+
}
|
|
2429
|
+
await flushTranscript();
|
|
2430
|
+
if (event.type === 'error' && !this._backgrounded) {
|
|
2431
|
+
await failRun(event.message);
|
|
1826
2432
|
return;
|
|
1827
2433
|
}
|
|
1828
|
-
if (type === '
|
|
1829
|
-
|
|
2434
|
+
if (event.type === 'error') {
|
|
2435
|
+
this._log?.info('runs', 'background error replayed', { runId, message: event.message });
|
|
1830
2436
|
}
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
const failRun = async (message) => {
|
|
1834
|
-
const folded = isContextOverflowFailure(message) &&
|
|
1835
|
-
(await this._runCompaction({
|
|
1836
|
-
chatId,
|
|
1837
|
-
transcript,
|
|
1838
|
-
trigger: 'overflow',
|
|
1839
|
-
signal: controller.signal,
|
|
1840
|
-
meshId
|
|
1841
|
-
}));
|
|
1842
|
-
runFailure = runFailureNotice(message);
|
|
1843
|
-
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
1844
|
-
if (folded) {
|
|
1845
|
-
await engine
|
|
1846
|
-
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
1847
|
-
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
1848
|
-
}
|
|
1849
|
-
titleOutcome = 'ended';
|
|
1850
|
-
};
|
|
1851
|
-
const endAbortedRun = async () => {
|
|
1852
|
-
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1853
|
-
if (outcome.kind === 'replay') {
|
|
1854
|
-
runVerdict = 'replay';
|
|
1855
|
-
this._seen.delete(runId);
|
|
2437
|
+
if (event.type === 'error' || event.type === 'aborted') {
|
|
2438
|
+
await endAbortedRun();
|
|
1856
2439
|
return;
|
|
1857
2440
|
}
|
|
1858
|
-
if (
|
|
1859
|
-
|
|
2441
|
+
if (event.type === 'paused') {
|
|
2442
|
+
await pauseRun();
|
|
1860
2443
|
return;
|
|
1861
2444
|
}
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
};
|
|
1866
|
-
const requireRecovery = async (reason) => {
|
|
1867
|
-
runVerdict = 'recovery-required';
|
|
1868
|
-
await transcript.status({ state: 'recovery-required', reason });
|
|
1869
|
-
titleOutcome = 'ended';
|
|
1870
|
-
};
|
|
1871
|
-
const recordToolResult = async (event) => {
|
|
1872
|
-
if (isDeniedResult(event.result)) {
|
|
1873
|
-
this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
|
|
2445
|
+
if (event.type === 'recovery-required') {
|
|
2446
|
+
await holdForRecovery(event);
|
|
2447
|
+
return;
|
|
1874
2448
|
}
|
|
1875
|
-
|
|
1876
|
-
|
|
2449
|
+
if (event.type === 'round-reset') {
|
|
2450
|
+
await resetRound(event);
|
|
2451
|
+
continue;
|
|
1877
2452
|
}
|
|
1878
|
-
if (
|
|
1879
|
-
|
|
2453
|
+
if (isHarnessEvent(event, 'tool-call')) {
|
|
2454
|
+
this._log?.info('runs', 'tool call', { runId, name: event.name });
|
|
2455
|
+
await transcript.chunk(toolCallChunk(event));
|
|
1880
2456
|
}
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
mainOwnsCleanup = true;
|
|
1884
|
-
const runCompletion = async () => {
|
|
1885
|
-
try {
|
|
1886
|
-
const attachTurnIds = resumed ? [] : attachTurnDocumentIds(history);
|
|
1887
|
-
if (attachTurnIds.length > 0) {
|
|
1888
|
-
backfillDocumentOnlyTurn(messages);
|
|
1889
|
-
}
|
|
1890
|
-
const knowledge = !resumed &&
|
|
1891
|
-
!diagramRepair &&
|
|
1892
|
-
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
1893
|
-
? await this._searchKnowledge(engine, messages, {
|
|
1894
|
-
chatId,
|
|
1895
|
-
attachmentIds: attachTurnIds,
|
|
1896
|
-
signal: controller.signal,
|
|
1897
|
-
preparing: modelWatch.documents
|
|
1898
|
-
})
|
|
1899
|
-
: null;
|
|
1900
|
-
if (controller.signal.aborted) {
|
|
1901
|
-
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1902
|
-
if (outcome.kind === 'replay') {
|
|
1903
|
-
runVerdict = 'replay';
|
|
1904
|
-
this._seen.delete(runId);
|
|
1905
|
-
return;
|
|
1906
|
-
}
|
|
1907
|
-
if (outcome.kind === 'handoff') {
|
|
1908
|
-
runVerdict = 'handoff';
|
|
1909
|
-
return;
|
|
1910
|
-
}
|
|
1911
|
-
runFailure = outcome.reason;
|
|
1912
|
-
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1913
|
-
return;
|
|
1914
|
-
}
|
|
1915
|
-
const injected = knowledge
|
|
1916
|
-
? await this._injectKnowledge(knowledge, {
|
|
1917
|
-
transcript,
|
|
1918
|
-
sourceTagFilter,
|
|
1919
|
-
retrievedRecords
|
|
1920
|
-
})
|
|
1921
|
-
: { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
1922
|
-
if (!resumed) {
|
|
1923
|
-
const hasAudio = messages.some((turn) => turn.audioAttachments?.length);
|
|
1924
|
-
if (hasAudio) {
|
|
1925
|
-
modelWatch.transcribe(true);
|
|
1926
|
-
}
|
|
1927
|
-
await this._hydrateAudio(messages, controller.signal);
|
|
1928
|
-
if (hasAudio) {
|
|
1929
|
-
modelWatch.transcribe(false);
|
|
1930
|
-
}
|
|
1931
|
-
backfillImageOnlyTurns(messages);
|
|
1932
|
-
if (seesImages) {
|
|
1933
|
-
renderedPdfIds = await this._hydratePdfPages(messages, controller.signal, runId);
|
|
1934
|
-
}
|
|
1935
|
-
annotateDocumentAttachments(messages);
|
|
1936
|
-
}
|
|
1937
|
-
const unreadIds = attachTurnIds.filter((id) => !renderedPdfIds.has(id));
|
|
1938
|
-
if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
|
|
1939
|
-
const reason = documentUnreadReason({
|
|
1940
|
-
knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
|
|
1941
|
-
failedFiles: knowledge?.search?.failedFiles,
|
|
1942
|
-
attachedCount: unreadIds.length
|
|
1943
|
-
});
|
|
1944
|
-
const lastUser = messages.findLast((turn) => turn.role === 'user');
|
|
1945
|
-
if (lastUser) {
|
|
1946
|
-
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
|
|
1947
|
-
}
|
|
1948
|
-
if (!injected.sourcesWritten) {
|
|
1949
|
-
await this._writeUnreadNotice(transcript, reason);
|
|
1950
|
-
}
|
|
1951
|
-
}
|
|
1952
|
-
if (!resumed) {
|
|
1953
|
-
annotateUnreadableImages(unreadableImages);
|
|
1954
|
-
}
|
|
1955
|
-
const toolConfigs = await toolConfigsPromise;
|
|
1956
|
-
const { roots, write } = await fsGrantsPromise;
|
|
1957
|
-
const { grantedDirs, grantedDrives, driveOp } = await resolveFsGrants(roots, this._drives, (key, err) => this._log?.warn('assistant', 'drive grant dropped', {
|
|
1958
|
-
key,
|
|
1959
|
-
error: errorMessage(err)
|
|
1960
|
-
}));
|
|
1961
|
-
if (await this._isHandled(engine, request)) {
|
|
1962
|
-
return;
|
|
1963
|
-
}
|
|
1964
|
-
let cacheKey;
|
|
1965
|
-
if (resumed) {
|
|
1966
|
-
cacheKey =
|
|
1967
|
-
typeof savedRequest?.cacheKey === 'string' ? savedRequest.cacheKey : undefined;
|
|
1968
|
-
}
|
|
1969
|
-
else if (!diagramRepair) {
|
|
1970
|
-
cacheKey = chatRepairKvCacheKey(chatId, acceptedRepairs);
|
|
1971
|
-
}
|
|
1972
|
-
const unloading = lender ? undefined : this._unloading.get(model);
|
|
1973
|
-
if (unloading) {
|
|
1974
|
-
await raceAbort(unloading.catch(noop), controller.signal);
|
|
1975
|
-
}
|
|
1976
|
-
this._log?.info('runs', 'completion requested', {
|
|
1977
|
-
runId,
|
|
1978
|
-
model,
|
|
1979
|
-
cacheKey,
|
|
1980
|
-
turns: messages.length
|
|
1981
|
-
});
|
|
1982
|
-
const harness = this._harnessFor(runAgent);
|
|
1983
|
-
const completionContext = {
|
|
1984
|
-
runId,
|
|
1985
|
-
runLog,
|
|
1986
|
-
resume: resumed,
|
|
1987
|
-
model,
|
|
1988
|
-
agent: runAgent,
|
|
1989
|
-
messages: resumed ? messages : nameSenders(messages),
|
|
1990
|
-
cacheKey,
|
|
1991
|
-
thinkingDefault: thinkingDefault(config),
|
|
1992
|
-
toolConfigs,
|
|
1993
|
-
grantedDirs,
|
|
1994
|
-
grantedDrives,
|
|
1995
|
-
driveOp,
|
|
1996
|
-
grantedWriteDirs: write,
|
|
1997
|
-
approve: approvals.approve,
|
|
1998
|
-
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
1999
|
-
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
2000
|
-
ragSearch: async (input) => {
|
|
2001
|
-
const found = await raceAbort(engine.ragSearch({
|
|
2002
|
-
chatId,
|
|
2003
|
-
query: input.query,
|
|
2004
|
-
mode: 'tool',
|
|
2005
|
-
limit: input.limit
|
|
2006
|
-
}), controller.signal);
|
|
2007
|
-
const hits = found.hits ?? [];
|
|
2008
|
-
const records = sourcesFromHits(hits, RAG_SOURCE_BOUNDS);
|
|
2009
|
-
if (records.length > 0) {
|
|
2010
|
-
try {
|
|
2011
|
-
sourceTagFilter.add(records.map((record) => record.title));
|
|
2012
|
-
retrievedRecords.push(...records.map((record, index) => ({
|
|
2013
|
-
...record,
|
|
2014
|
-
excerpt: hits[index]?.content ?? record.excerpt
|
|
2015
|
-
})));
|
|
2016
|
-
await transcript.chunk({
|
|
2017
|
-
type: 'sources',
|
|
2018
|
-
sources: { sources: records }
|
|
2019
|
-
});
|
|
2020
|
-
}
|
|
2021
|
-
catch (err) {
|
|
2022
|
-
this._log?.warn('assistant', 'rag tool sources chunk skipped', { runId, err });
|
|
2023
|
-
}
|
|
2024
|
-
}
|
|
2025
|
-
return {
|
|
2026
|
-
enabled: found.enabled === true,
|
|
2027
|
-
indexEmpty: found.indexEmpty === true,
|
|
2028
|
-
hasKnowledge: found.hasKnowledge === true,
|
|
2029
|
-
pendingReferenced: found.pendingReferenced === true,
|
|
2030
|
-
errorMessage: found.errorMessage ?? undefined,
|
|
2031
|
-
hits
|
|
2032
|
-
};
|
|
2033
|
-
},
|
|
2034
|
-
progress: (update) => modelWatch.image(update),
|
|
2035
|
-
generationParams: diagramRepair ? { reasoning_budget: 0 } : undefined,
|
|
2036
|
-
captureInferenceCalls: await this._capturingInferenceCalls(),
|
|
2037
|
-
signal: controller.signal
|
|
2038
|
-
};
|
|
2039
|
-
const events = harness.completion(completionContext);
|
|
2040
|
-
let harnessEvents = 0;
|
|
2041
|
-
for await (const event of events) {
|
|
2042
|
-
harnessEvents++;
|
|
2043
|
-
modelWatch.clear();
|
|
2044
|
-
if (event.type === 'run-started' && resumed) {
|
|
2045
|
-
await requireRecovery('The worker started a fresh run instead of resuming the saved run.');
|
|
2046
|
-
return;
|
|
2047
|
-
}
|
|
2048
|
-
if (event.type === 'run-started') {
|
|
2049
|
-
continue;
|
|
2050
|
-
}
|
|
2051
|
-
if (event.type === 'run-resumed' && !resumeAccepted) {
|
|
2052
|
-
const executing = await transcript.status({ state: 'executing' }, { resumed: true });
|
|
2053
|
-
runStatusId = executing.id;
|
|
2054
|
-
resumeAccepted = true;
|
|
2055
|
-
}
|
|
2056
|
-
if (event.type === 'run-resumed') {
|
|
2057
|
-
continue;
|
|
2058
|
-
}
|
|
2059
|
-
if (resumed && !resumeAccepted && event.type === 'error') {
|
|
2060
|
-
await requireRecovery(resumeSetupFailureNotice(event));
|
|
2061
|
-
return;
|
|
2062
|
-
}
|
|
2063
|
-
if (resumed && !resumeAccepted && event.type !== 'aborted' && event.type !== 'paused') {
|
|
2064
|
-
await requireRecovery('The worker did not confirm the saved run before producing new events.');
|
|
2065
|
-
return;
|
|
2066
|
-
}
|
|
2067
|
-
if (event.type === 'round-reset') {
|
|
2068
|
-
const replacedTypes = Array.isArray(event.replacedTypes)
|
|
2069
|
-
? event.replacedTypes.filter(isReplacedType)
|
|
2070
|
-
: undefined;
|
|
2071
|
-
const round = typeof event.round === 'number' ? event.round : 0;
|
|
2072
|
-
await resetRound(round, replacedTypes);
|
|
2073
|
-
continue;
|
|
2074
|
-
}
|
|
2075
|
-
if (isHarnessEvent(event, 'content') || isHarnessEvent(event, 'thinking')) {
|
|
2076
|
-
const round = typeof event.round === 'number' ? event.round : activeRound;
|
|
2077
|
-
await writeText(event.type, event.text, round);
|
|
2078
|
-
continue;
|
|
2079
|
-
}
|
|
2080
|
-
if (event.type === 'metrics') {
|
|
2081
|
-
const { type, ...rest } = event;
|
|
2082
|
-
metrics = rest;
|
|
2083
|
-
this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
|
|
2084
|
-
continue;
|
|
2085
|
-
}
|
|
2086
|
-
if (event.type === 'inference-sent') {
|
|
2087
|
-
void this._inferenceCalls
|
|
2088
|
-
.recordSent(chatId, {
|
|
2089
|
-
runId,
|
|
2090
|
-
round: Number(event.round ?? 0),
|
|
2091
|
-
messages: event.messages,
|
|
2092
|
-
tokens: event.tokens,
|
|
2093
|
-
outcome: event.outcome,
|
|
2094
|
-
reason: event.reason
|
|
2095
|
-
})
|
|
2096
|
-
.catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
|
|
2097
|
-
continue;
|
|
2098
|
-
}
|
|
2099
|
-
if (event.type === 'inference-call') {
|
|
2100
|
-
void this._inferenceCalls
|
|
2101
|
-
.record(chatId, { runId, at: Date.now(), call: asRecord(event.call) })
|
|
2102
|
-
.catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
|
|
2103
|
-
continue;
|
|
2104
|
-
}
|
|
2105
|
-
await flushTranscript();
|
|
2106
|
-
if (event.type === 'error') {
|
|
2107
|
-
await failRun(event.message);
|
|
2108
|
-
return;
|
|
2109
|
-
}
|
|
2110
|
-
if (event.type === 'aborted') {
|
|
2111
|
-
await endAbortedRun();
|
|
2112
|
-
return;
|
|
2113
|
-
}
|
|
2114
|
-
if (event.type === 'paused' && controller.signal.reason === SUSPENDED) {
|
|
2115
|
-
this._seen.delete(runId);
|
|
2116
|
-
}
|
|
2117
|
-
if (event.type === 'paused' || event.type === 'recovery-required') {
|
|
2118
|
-
runVerdict = event.type;
|
|
2119
|
-
const operationName = asRecord(event.operation).name;
|
|
2120
|
-
const reason = event.type === 'paused'
|
|
2121
|
-
? 'The run paused after saving a checkpoint.'
|
|
2122
|
-
: `Recovery decision required${typeof operationName === 'string' ? ` for ${operationName}` : ''}.`;
|
|
2123
|
-
await transcript.status({ state: event.type, reason });
|
|
2124
|
-
titleOutcome = 'ended';
|
|
2125
|
-
return;
|
|
2126
|
-
}
|
|
2127
|
-
if (isHarnessEvent(event, 'tool-call')) {
|
|
2128
|
-
this._log?.info('runs', 'tool call', { runId, name: event.name });
|
|
2129
|
-
await transcript.chunk(toolCallChunk(event));
|
|
2130
|
-
}
|
|
2131
|
-
else if (isHarnessEvent(event, 'tool-result')) {
|
|
2132
|
-
await recordToolResult(event);
|
|
2133
|
-
}
|
|
2134
|
-
else if (isHarnessEvent(event, 'tool-activity')) {
|
|
2135
|
-
void engine
|
|
2136
|
-
.recordToolActivity({
|
|
2137
|
-
agentId: runAgent.id,
|
|
2138
|
-
toolName: event.name,
|
|
2139
|
-
decision: event.decision,
|
|
2140
|
-
effect: event.effect,
|
|
2141
|
-
arguments: event.args,
|
|
2142
|
-
resource: event.resource ?? undefined,
|
|
2143
|
-
chatId,
|
|
2144
|
-
runId,
|
|
2145
|
-
at: Date.now()
|
|
2146
|
-
})
|
|
2147
|
-
.catch((err) => this._log?.warn('runs', 'tool activity record failed', { runId, err }));
|
|
2148
|
-
}
|
|
2149
|
-
else {
|
|
2150
|
-
this._log?.warn('assistant', 'unknown harness event dropped', { type: event.type });
|
|
2151
|
-
}
|
|
2152
|
-
}
|
|
2153
|
-
if (resumed && !resumeAccepted) {
|
|
2154
|
-
await requireRecovery('The worker ended without confirming the saved run was resumed.');
|
|
2155
|
-
return;
|
|
2156
|
-
}
|
|
2157
|
-
this._log?.info('runs', 'harness stream ended', { runId, events: harnessEvents });
|
|
2158
|
-
if (await settledWithin(flushTranscript(), FINAL_FLUSH_MS)) {
|
|
2159
|
-
this._log?.info('runs', 'final flush done', { runId });
|
|
2160
|
-
}
|
|
2161
|
-
else {
|
|
2162
|
-
this._log?.warn('runs', 'completing over a stranded flush', {
|
|
2163
|
-
runId,
|
|
2164
|
-
pending: transcript.pending()
|
|
2165
|
-
});
|
|
2166
|
-
}
|
|
2167
|
-
const answerText = [...answerTextByRound.entries()]
|
|
2168
|
-
.sort(([left], [right]) => left - right)
|
|
2169
|
-
.map(([, text]) => text)
|
|
2170
|
-
.join('');
|
|
2171
|
-
const citedTitles = injected.ragInjected
|
|
2172
|
-
? citedByPhraseOverlap(answerText, retrievedRecords)
|
|
2173
|
-
: null;
|
|
2174
|
-
await transcript.status({
|
|
2175
|
-
state: 'completed',
|
|
2176
|
-
metrics: citedTitles ? { ...metrics, citedTitles } : metrics
|
|
2177
|
-
});
|
|
2178
|
-
titleOutcome = 'completed';
|
|
2457
|
+
else if (isHarnessEvent(event, 'tool-result')) {
|
|
2458
|
+
await recordToolResult(event);
|
|
2179
2459
|
}
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
:
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
runVerdict = 'handoff';
|
|
2191
|
-
return;
|
|
2192
|
-
}
|
|
2193
|
-
await flushTranscript();
|
|
2194
|
-
runFailure =
|
|
2195
|
-
(this._lenderFor(runAgent) ? loanFailureNotice(err) : null) ?? runFailureNotice(err);
|
|
2196
|
-
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
2197
|
-
titleOutcome = 'ended';
|
|
2198
|
-
}
|
|
2199
|
-
finally {
|
|
2200
|
-
void this._inferenceCalls
|
|
2201
|
-
.recordOutcome(chatId, {
|
|
2460
|
+
else if (isHarnessEvent(event, 'tool-activity')) {
|
|
2461
|
+
void engine
|
|
2462
|
+
.recordToolActivity({
|
|
2463
|
+
agentId: runAgent.id,
|
|
2464
|
+
toolName: event.name,
|
|
2465
|
+
decision: event.decision,
|
|
2466
|
+
effect: event.effect,
|
|
2467
|
+
arguments: event.args,
|
|
2468
|
+
resource: event.resource ?? undefined,
|
|
2469
|
+
chatId,
|
|
2202
2470
|
runId,
|
|
2203
|
-
|
|
2204
|
-
reason: runFailure
|
|
2471
|
+
at: Date.now()
|
|
2205
2472
|
})
|
|
2206
|
-
.catch((err) => this._log?.warn('
|
|
2207
|
-
transcript.close();
|
|
2208
|
-
modelWatch.stop();
|
|
2209
|
-
ownershipWatch.stop();
|
|
2210
|
-
await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
|
|
2211
|
-
settledWatch.stop();
|
|
2212
|
-
if (titleOutcome && !diagramRepair) {
|
|
2213
|
-
void this._autoTitle(engine, {
|
|
2214
|
-
chatId,
|
|
2215
|
-
messages,
|
|
2216
|
-
model,
|
|
2217
|
-
agent: runAgent,
|
|
2218
|
-
probe: titleOutcome === 'completed'
|
|
2219
|
-
});
|
|
2220
|
-
}
|
|
2473
|
+
.catch((err) => this._log?.warn('runs', 'tool activity record failed', { runId, err }));
|
|
2221
2474
|
}
|
|
2222
|
-
|
|
2223
|
-
|
|
2475
|
+
else {
|
|
2476
|
+
this._log?.warn('assistant', 'unknown harness event dropped', { type: event.type });
|
|
2477
|
+
}
|
|
2478
|
+
}
|
|
2479
|
+
this._log?.info('runs', 'harness stream ended', { runId, events: harnessEvents });
|
|
2480
|
+
if (await settledWithin(flushTranscript(), FINAL_FLUSH_MS)) {
|
|
2481
|
+
this._log?.info('runs', 'final flush done', { runId });
|
|
2482
|
+
}
|
|
2483
|
+
else {
|
|
2484
|
+
this._log?.warn('runs', 'completing over a stranded flush', {
|
|
2485
|
+
runId,
|
|
2486
|
+
pending: transcript.pending()
|
|
2487
|
+
});
|
|
2488
|
+
}
|
|
2489
|
+
const citedTitles = injected.ragInjected
|
|
2490
|
+
? citedByPhraseOverlap([...answers.values()].join(''), retrievedRecords)
|
|
2491
|
+
: null;
|
|
2492
|
+
const primedHere = !!sentRun?.used && rounds.ranHere(primeRound);
|
|
2493
|
+
const primed = primedHere ? replay.rebuild : null;
|
|
2494
|
+
if (primedHere) {
|
|
2495
|
+
await this._runReplay.consume({ holder: LOCAL_HOLDER, chatId });
|
|
2496
|
+
}
|
|
2497
|
+
if (sentRun?.used && !rounds.rounds) {
|
|
2498
|
+
await this._recordSentRun(chatId, { runId, turns: sentRun.turns(inputLength), asked });
|
|
2499
|
+
}
|
|
2500
|
+
sentRun = null;
|
|
2501
|
+
await transcript.status({
|
|
2502
|
+
state: 'completed',
|
|
2503
|
+
metrics: withCacheRebuild(citedTitles ? { ...metrics, citedTitles } : metrics, primed, primeTokens ?? null)
|
|
2504
|
+
});
|
|
2505
|
+
titleOutcome = 'completed';
|
|
2506
|
+
for (const id of taken) {
|
|
2507
|
+
this._steered.set(id, runId);
|
|
2508
|
+
}
|
|
2509
|
+
await this._storeVideoDigests(sampled, { meshId, chatId, runId, deviceId });
|
|
2510
|
+
await this._storePdfSelections(selectedPdfs, { meshId, chatId, runId, deviceId });
|
|
2511
|
+
}
|
|
2512
|
+
catch (err) {
|
|
2513
|
+
const outcome = controller.signal.aborted
|
|
2514
|
+
? this._abortOutcome(runId, controller.signal.reason)
|
|
2515
|
+
: null;
|
|
2516
|
+
if (outcome?.kind === 'replay') {
|
|
2517
|
+
runVerdict = 'replay';
|
|
2518
|
+
this._seen.delete(runId);
|
|
2519
|
+
return;
|
|
2520
|
+
}
|
|
2521
|
+
if (outcome?.kind === 'handoff') {
|
|
2522
|
+
runVerdict = 'handoff';
|
|
2523
|
+
return;
|
|
2524
|
+
}
|
|
2525
|
+
await flushTranscript();
|
|
2526
|
+
runFailure = runFailureNotice(err);
|
|
2527
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
2528
|
+
titleOutcome = 'ended';
|
|
2224
2529
|
}
|
|
2225
2530
|
finally {
|
|
2226
|
-
|
|
2227
|
-
|
|
2531
|
+
steering?.close();
|
|
2532
|
+
if (sentRun?.used) {
|
|
2533
|
+
await this._recordSentRun(chatId, { runId, turns: null });
|
|
2534
|
+
}
|
|
2535
|
+
void this._inferenceCalls
|
|
2536
|
+
.recordOutcome(chatId, {
|
|
2537
|
+
runId,
|
|
2538
|
+
outcome: runFailure ? 'failed' : runVerdict,
|
|
2539
|
+
reason: runFailure
|
|
2540
|
+
})
|
|
2541
|
+
.catch((err) => this._log?.warn('assistant', 'run outcome not recorded', { err }));
|
|
2542
|
+
transcript.close();
|
|
2543
|
+
idle.stop();
|
|
2544
|
+
modelWatch.stop();
|
|
2545
|
+
ownershipWatch.stop();
|
|
2546
|
+
await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
|
|
2547
|
+
settledWatch.stop();
|
|
2548
|
+
await releaseRunLog();
|
|
2549
|
+
if (titleOutcome && !diagramRepair) {
|
|
2550
|
+
const completed = titleOutcome === 'completed';
|
|
2551
|
+
const answerText = [...answers.values()].join('');
|
|
2552
|
+
void this._autoTitle(engine, {
|
|
2553
|
+
chatId,
|
|
2554
|
+
history,
|
|
2555
|
+
messages,
|
|
2556
|
+
model,
|
|
2557
|
+
agent: runAgent,
|
|
2558
|
+
probe: completed,
|
|
2559
|
+
rounds
|
|
2560
|
+
})
|
|
2561
|
+
.then(() => completed && answerText.trim()
|
|
2562
|
+
? this._suggestNext(engine, {
|
|
2563
|
+
chatId,
|
|
2564
|
+
runId,
|
|
2565
|
+
messages,
|
|
2566
|
+
answer: answerText,
|
|
2567
|
+
model,
|
|
2568
|
+
agent: runAgent,
|
|
2569
|
+
rounds
|
|
2570
|
+
})
|
|
2571
|
+
: undefined)
|
|
2572
|
+
.catch((err) => this._log?.warn('runs', 'title or suggestion skipped', { chatId, err }));
|
|
2228
2573
|
}
|
|
2229
2574
|
}
|
|
2230
2575
|
}
|
|
2231
2576
|
async _invokeCapability({ engine, request, agent, capability, controller, meshId, runId }) {
|
|
2232
2577
|
const { chatId } = request;
|
|
2233
2578
|
const deviceId = this._requireDeviceId();
|
|
2234
|
-
const harness = this.
|
|
2579
|
+
const harness = this._harness;
|
|
2235
2580
|
const transcript = new RunTranscript(engine, {
|
|
2236
2581
|
chatId,
|
|
2237
2582
|
runId,
|
|
@@ -2243,11 +2588,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2243
2588
|
if (inflight) {
|
|
2244
2589
|
this._runs.set(runId, { ...inflight, executing: true, model: '' });
|
|
2245
2590
|
}
|
|
2246
|
-
this.
|
|
2591
|
+
this._runsChanged();
|
|
2247
2592
|
const executing = await transcript.status({ state: 'executing' });
|
|
2248
2593
|
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(executing.id, runStatus));
|
|
2249
2594
|
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
2250
|
-
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
2595
|
+
const ownershipWatch = this._watchOwnership(engine, { request, agentId: agent.id, controller });
|
|
2596
|
+
const idle = idleWatch(controller, this._runIdleTimeoutMs);
|
|
2597
|
+
idle.touch();
|
|
2251
2598
|
let metrics;
|
|
2252
2599
|
const endAbortedCapability = async () => {
|
|
2253
2600
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
@@ -2274,17 +2621,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2274
2621
|
agent,
|
|
2275
2622
|
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
2276
2623
|
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
2277
|
-
progress: (update) =>
|
|
2624
|
+
progress: (update) => {
|
|
2625
|
+
idle.touch();
|
|
2626
|
+
return modelWatch.image(update);
|
|
2627
|
+
},
|
|
2278
2628
|
signal: controller.signal
|
|
2279
2629
|
});
|
|
2280
2630
|
for await (const event of events) {
|
|
2631
|
+
idle.touch();
|
|
2281
2632
|
if (event.type === 'metrics') {
|
|
2282
2633
|
const { type, ...rest } = event;
|
|
2283
2634
|
metrics = rest;
|
|
2284
2635
|
this._log?.info('runs', 'capability metrics', { runId, capability, ...rest });
|
|
2285
2636
|
continue;
|
|
2286
2637
|
}
|
|
2287
|
-
if (event.type === 'error') {
|
|
2638
|
+
if (event.type === 'error' && !this._backgrounded) {
|
|
2288
2639
|
await transcript.status({
|
|
2289
2640
|
state: 'failed',
|
|
2290
2641
|
reason: runFailureNotice(event.message),
|
|
@@ -2292,7 +2643,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2292
2643
|
});
|
|
2293
2644
|
return;
|
|
2294
2645
|
}
|
|
2295
|
-
if (event.type === '
|
|
2646
|
+
if (event.type === 'error') {
|
|
2647
|
+
this._log?.info('runs', 'background error replayed', { runId, message: event.message });
|
|
2648
|
+
}
|
|
2649
|
+
if (event.type === 'error' || event.type === 'aborted') {
|
|
2296
2650
|
await endAbortedCapability();
|
|
2297
2651
|
return;
|
|
2298
2652
|
}
|
|
@@ -2321,14 +2675,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2321
2675
|
}
|
|
2322
2676
|
await transcript.status({
|
|
2323
2677
|
state: 'failed',
|
|
2324
|
-
reason: outcome?.reason ??
|
|
2325
|
-
(this._lenderFor(agent) ? loanFailureNotice(err) : null) ??
|
|
2326
|
-
runFailureNotice(err),
|
|
2678
|
+
reason: outcome?.reason ?? runFailureNotice(err),
|
|
2327
2679
|
metrics
|
|
2328
2680
|
});
|
|
2329
2681
|
}
|
|
2330
2682
|
finally {
|
|
2331
2683
|
transcript.close();
|
|
2684
|
+
idle.stop();
|
|
2332
2685
|
modelWatch.stop();
|
|
2333
2686
|
settledWatch.stop();
|
|
2334
2687
|
ownershipWatch.stop();
|
|
@@ -2407,95 +2760,358 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2407
2760
|
if (signal?.aborted) {
|
|
2408
2761
|
throw new AbortedError();
|
|
2409
2762
|
}
|
|
2410
|
-
return head && (meta ? head : { ...head, bytes: b4a.concat(pieces) });
|
|
2763
|
+
return head && (meta ? head : { ...head, bytes: b4a.concat(pieces) });
|
|
2764
|
+
}
|
|
2765
|
+
finally {
|
|
2766
|
+
signal?.removeEventListener('abort', abort);
|
|
2767
|
+
stream.destroy();
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
async _readBlob(url, signal) {
|
|
2771
|
+
if (!url || signal?.aborted)
|
|
2772
|
+
return null;
|
|
2773
|
+
const pending = defaultFetch(url).catch(noop);
|
|
2774
|
+
try {
|
|
2775
|
+
const response = await raceInactivity(pending, signal, this._blobReadTimeoutMs);
|
|
2776
|
+
const bytes = response?.ok
|
|
2777
|
+
? await drainBody(response.body, signal, this._blobReadTimeoutMs)
|
|
2778
|
+
: null;
|
|
2779
|
+
return bytes?.length ? bytes : null;
|
|
2780
|
+
}
|
|
2781
|
+
catch {
|
|
2782
|
+
pending.then((response) => response?.body?.cancel(), noop);
|
|
2783
|
+
return null;
|
|
2784
|
+
}
|
|
2785
|
+
}
|
|
2786
|
+
async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId, sourceId }) {
|
|
2787
|
+
const duplex = this._engine.uploadAttachment();
|
|
2788
|
+
duplex.write({
|
|
2789
|
+
chatId,
|
|
2790
|
+
runId,
|
|
2791
|
+
originDeviceId: deviceId,
|
|
2792
|
+
fileName,
|
|
2793
|
+
mimeType,
|
|
2794
|
+
raw,
|
|
2795
|
+
meshId,
|
|
2796
|
+
sourceId
|
|
2797
|
+
});
|
|
2798
|
+
const buf = b4a.toBuffer(bytes);
|
|
2799
|
+
for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
|
|
2800
|
+
duplex.write({ data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
|
|
2801
|
+
}
|
|
2802
|
+
duplex.end();
|
|
2803
|
+
let attachment = null;
|
|
2804
|
+
for await (const frame of duplex) {
|
|
2805
|
+
if (frame.attachment) {
|
|
2806
|
+
attachment = frame.attachment;
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
if (!attachment) {
|
|
2810
|
+
throw new Error('attachment upload ended without a record');
|
|
2811
|
+
}
|
|
2812
|
+
return attachment;
|
|
2813
|
+
}
|
|
2814
|
+
async _hydrateAudio(messages, signal) {
|
|
2815
|
+
const turns = messages.filter((turn) => turn.audioAttachments?.length);
|
|
2816
|
+
if (!turns.length) {
|
|
2817
|
+
return;
|
|
2818
|
+
}
|
|
2819
|
+
for (const turn of turns) {
|
|
2820
|
+
for (const ref of turn.audioAttachments ?? []) {
|
|
2821
|
+
if (signal.aborted)
|
|
2822
|
+
return;
|
|
2823
|
+
const block = await this._transcribeAttachment(ref, signal);
|
|
2824
|
+
turn.content = turn.content.trim() ? `${turn.content}\n\n${block}` : block;
|
|
2825
|
+
}
|
|
2826
|
+
delete turn.audioAttachments;
|
|
2827
|
+
}
|
|
2828
|
+
}
|
|
2829
|
+
async _transcribeAttachment(ref, signal) {
|
|
2830
|
+
const label = `Attached audio "${ref.fileName}"`;
|
|
2831
|
+
const cached = this._audioTranscripts.get(ref.blobId);
|
|
2832
|
+
if (cached !== undefined) {
|
|
2833
|
+
return cached ? audioTranscriptBlock(ref.fileName, cached) : silentAudioNotice(ref.fileName);
|
|
2834
|
+
}
|
|
2835
|
+
if (!this._harness.transcribe) {
|
|
2836
|
+
return unheardAudioNotice(label, 'transcription is unavailable on this device', {
|
|
2837
|
+
retryable: false
|
|
2838
|
+
});
|
|
2839
|
+
}
|
|
2840
|
+
try {
|
|
2841
|
+
const bytes = await this._readBlob(ref.url, signal);
|
|
2842
|
+
if (!bytes) {
|
|
2843
|
+
return unheardAudioNotice(label, 'its content has not reached this device — the upload may still be syncing');
|
|
2844
|
+
}
|
|
2845
|
+
const model = mediaModelNameFor('transcription', await this._persistedProfile());
|
|
2846
|
+
await this.ensureMediaModelUnpaused('transcription', model);
|
|
2847
|
+
await this._downloads?.waitUntilCached(model, signal);
|
|
2848
|
+
const text = (await this._harness.transcribe({ model: '', audio: b4a.toBuffer(bytes), signal })).trim();
|
|
2849
|
+
this._audioTranscripts.set(ref.blobId, text);
|
|
2850
|
+
if (!text) {
|
|
2851
|
+
return silentAudioNotice(ref.fileName);
|
|
2852
|
+
}
|
|
2853
|
+
return audioTranscriptBlock(ref.fileName, text);
|
|
2854
|
+
}
|
|
2855
|
+
catch (err) {
|
|
2856
|
+
if (signal.aborted) {
|
|
2857
|
+
throw err;
|
|
2858
|
+
}
|
|
2859
|
+
this._log?.warn('assistant', `audio transcription failed — ${ref.fileName}`, err);
|
|
2860
|
+
return unheardAudioNotice(label, 'transcription failed on this device');
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
async _seesImages(model) {
|
|
2864
|
+
if (this._harness.supportsImagesFor) {
|
|
2865
|
+
return this._harness.supportsImagesFor(model);
|
|
2866
|
+
}
|
|
2867
|
+
const anyModel = (this._harness.supportsImages ?? this._info.supportsImages) !== false;
|
|
2868
|
+
if (!anyModel || !this._harness.list) {
|
|
2869
|
+
return anyModel;
|
|
2870
|
+
}
|
|
2871
|
+
const rows = await this._harness.list({ type: 'llm' }).catch(noop);
|
|
2872
|
+
if (!rows) {
|
|
2873
|
+
return anyModel;
|
|
2874
|
+
}
|
|
2875
|
+
const row = rows.find((candidate) => candidate.name === model);
|
|
2876
|
+
return row ? (row.supportsImages ?? anyModel) : false;
|
|
2877
|
+
}
|
|
2878
|
+
async _hydrateVideos(messages, run) {
|
|
2879
|
+
const { signal, runId, seesImages, reading, sampled } = run;
|
|
2880
|
+
const turns = messages.filter((turn) => turn.videos?.length);
|
|
2881
|
+
if (!turns.length) {
|
|
2882
|
+
return;
|
|
2883
|
+
}
|
|
2884
|
+
const asked = messages.findLast((turn) => turn.role === 'user')?.videos ?? [];
|
|
2885
|
+
const dir = this._imageCacheDir;
|
|
2886
|
+
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
2887
|
+
const pinned = this._runImageCacheNames.get(runId) ?? new Set();
|
|
2888
|
+
this._runImageCacheNames.set(runId, pinned);
|
|
2889
|
+
const unsampled = turns.flatMap((turn) => turn.videos ?? []).filter((ref) => !ref.digest);
|
|
2890
|
+
const budget = { ...reading, videos: unsampled.length };
|
|
2891
|
+
for (const turn of turns) {
|
|
2892
|
+
for (const ref of turn.videos ?? []) {
|
|
2893
|
+
if (signal.aborted)
|
|
2894
|
+
return;
|
|
2895
|
+
const hydration = { dir, ref, pinned, seesImages, signal, budget, sampled };
|
|
2896
|
+
const blocks = await this._hydrateVideo(turn, hydration);
|
|
2897
|
+
turn.content = [turn.content.trim(), ...blocks].filter(Boolean).join('\n\n');
|
|
2898
|
+
}
|
|
2899
|
+
delete turn.videos;
|
|
2900
|
+
}
|
|
2901
|
+
const pictured = asked.some((ref) => ref.digest || this._videoProbes.get(ref.blobId)?.hasVideo);
|
|
2902
|
+
if (!seesImages && pictured) {
|
|
2903
|
+
throw new Error(RUN_FAILURE_REASONS.videoNeedsVision);
|
|
2904
|
+
}
|
|
2905
|
+
}
|
|
2906
|
+
async _hydrateVideo(turn, hydration) {
|
|
2907
|
+
const { ref, seesImages, signal } = hydration;
|
|
2908
|
+
const source = { opened: null };
|
|
2909
|
+
const open = () => (source.opened ??= this._openVideo(hydration));
|
|
2910
|
+
try {
|
|
2911
|
+
const video = await this._readVideo(hydration, open);
|
|
2912
|
+
if (video === 'unsynced') {
|
|
2913
|
+
return [unsyncedVideoNotice(ref.fileName)];
|
|
2914
|
+
}
|
|
2915
|
+
if (video === 'audio') {
|
|
2916
|
+
return [await this._transcribeAttachment(ref, signal)];
|
|
2917
|
+
}
|
|
2918
|
+
if (!seesImages || !('files' in video)) {
|
|
2919
|
+
return [hiddenVideoNotice(ref.fileName), videoAudioBlock(ref.fileName, video)];
|
|
2920
|
+
}
|
|
2921
|
+
attachPages(turn, video.files);
|
|
2922
|
+
const annotation = videoAnnotation({
|
|
2923
|
+
fileName: ref.fileName,
|
|
2924
|
+
duration: video.duration,
|
|
2925
|
+
times: video.times
|
|
2926
|
+
});
|
|
2927
|
+
return [annotation, videoAudioBlock(ref.fileName, video)];
|
|
2928
|
+
}
|
|
2929
|
+
catch (err) {
|
|
2930
|
+
if (signal.aborted) {
|
|
2931
|
+
throw err;
|
|
2932
|
+
}
|
|
2933
|
+
this._log?.warn('assistant', `video hydration failed — ${ref.fileName}`, err);
|
|
2934
|
+
return [unreadVideoNotice(ref.fileName)];
|
|
2411
2935
|
}
|
|
2412
2936
|
finally {
|
|
2413
|
-
|
|
2414
|
-
|
|
2937
|
+
const video = await source.opened?.catch(noop);
|
|
2938
|
+
if (video) {
|
|
2939
|
+
await fs.promises.unlink(video.file).catch(noop);
|
|
2940
|
+
}
|
|
2415
2941
|
}
|
|
2416
2942
|
}
|
|
2417
|
-
async
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
const response = await raceInactivity(pending, signal, this._blobReadTimeoutMs);
|
|
2423
|
-
const bytes = response?.ok
|
|
2424
|
-
? await drainBody(response.body, signal, this._blobReadTimeoutMs)
|
|
2425
|
-
: null;
|
|
2426
|
-
return bytes?.length ? bytes : null;
|
|
2943
|
+
async _readVideo(hydration, open) {
|
|
2944
|
+
const { ref, seesImages, signal } = hydration;
|
|
2945
|
+
const stored = ref.digest ? await this._storedVideo(ref.digest, hydration) : null;
|
|
2946
|
+
if (stored) {
|
|
2947
|
+
return stored;
|
|
2427
2948
|
}
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
return
|
|
2949
|
+
const probe = await this._probeVideo(ref, open);
|
|
2950
|
+
if (!probe) {
|
|
2951
|
+
return 'unsynced';
|
|
2952
|
+
}
|
|
2953
|
+
if (!probe.hasVideo) {
|
|
2954
|
+
return 'audio';
|
|
2955
|
+
}
|
|
2956
|
+
if (!seesImages) {
|
|
2957
|
+
return probe.hasAudio ? this._hearVideo(ref, open, signal) : { audio: 'none' };
|
|
2431
2958
|
}
|
|
2959
|
+
const video = await open();
|
|
2960
|
+
if (!video) {
|
|
2961
|
+
return 'unsynced';
|
|
2962
|
+
}
|
|
2963
|
+
return this._sampleVideo(video, probe, hydration);
|
|
2432
2964
|
}
|
|
2433
|
-
async
|
|
2434
|
-
const
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
|
|
2438
|
-
duplex.write({ data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
|
|
2965
|
+
async _probeVideo(ref, open) {
|
|
2966
|
+
const known = this._videoProbes.get(ref.blobId);
|
|
2967
|
+
if (known) {
|
|
2968
|
+
return known;
|
|
2439
2969
|
}
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2970
|
+
const video = await open();
|
|
2971
|
+
if (!video) {
|
|
2972
|
+
return null;
|
|
2973
|
+
}
|
|
2974
|
+
const probe = await probeVideo(video.file);
|
|
2975
|
+
this._videoProbes.set(ref.blobId, probe);
|
|
2976
|
+
return probe;
|
|
2977
|
+
}
|
|
2978
|
+
async _storedVideo(digest, { dir, ref, pinned, signal }) {
|
|
2979
|
+
const headerName = videoHeaderCacheName(digest.blobId);
|
|
2980
|
+
pinned.add(headerName);
|
|
2981
|
+
const header = await fs.promises
|
|
2982
|
+
.readFile(path.join(dir, headerName), 'utf8')
|
|
2983
|
+
.then(parseVideoDigestHeader, noop);
|
|
2984
|
+
if (header) {
|
|
2985
|
+
const files = header.times.map((_, i) => {
|
|
2986
|
+
const name = videoFrameCacheName(ref, header.times.length, i);
|
|
2987
|
+
pinned.add(name);
|
|
2988
|
+
this._mediaRefs.set(name, { id: ref.id, frame: i, frames: header.times.length });
|
|
2989
|
+
return path.join(dir, name);
|
|
2990
|
+
});
|
|
2991
|
+
const cached = await Promise.all(files.map((file) => cacheOrTouch(file, noBytes)));
|
|
2992
|
+
if (cached.every(Boolean)) {
|
|
2993
|
+
return { ...header, files };
|
|
2445
2994
|
}
|
|
2446
2995
|
}
|
|
2447
|
-
|
|
2448
|
-
|
|
2996
|
+
const bytes = await this._readBlob(digest.url, signal);
|
|
2997
|
+
if (!bytes) {
|
|
2998
|
+
return null;
|
|
2999
|
+
}
|
|
3000
|
+
const target = { dir, pinned, digestBlobId: digest.blobId };
|
|
3001
|
+
try {
|
|
3002
|
+
return await this._cacheVideo(ref, unpackVideoDigest(bytes), target);
|
|
3003
|
+
}
|
|
3004
|
+
catch (err) {
|
|
3005
|
+
this._log?.warn('assistant', `video digest unreadable — ${ref.fileName}`, err);
|
|
3006
|
+
return null;
|
|
2449
3007
|
}
|
|
2450
|
-
return attachment;
|
|
2451
3008
|
}
|
|
2452
|
-
async
|
|
2453
|
-
const
|
|
2454
|
-
|
|
2455
|
-
|
|
3009
|
+
async _cacheVideo(ref, digest, { dir, pinned, digestBlobId }) {
|
|
3010
|
+
const { frames, ...header } = digest;
|
|
3011
|
+
const files = frames.map((_, i) => {
|
|
3012
|
+
const name = videoFrameCacheName(ref, frames.length, i);
|
|
3013
|
+
pinned.add(name);
|
|
3014
|
+
this._mediaRefs.set(name, { id: ref.id, frame: i, frames: frames.length });
|
|
3015
|
+
return path.join(dir, name);
|
|
3016
|
+
});
|
|
3017
|
+
await Promise.all(frames.map((frame, i) => cacheOrTouch(files[i], () => Promise.resolve(frame))));
|
|
3018
|
+
if (digestBlobId) {
|
|
3019
|
+
const headerName = videoHeaderCacheName(digestBlobId);
|
|
3020
|
+
pinned.add(headerName);
|
|
3021
|
+
await cacheOrTouch(path.join(dir, headerName), () => Promise.resolve(b4a.from(JSON.stringify(header))));
|
|
2456
3022
|
}
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
3023
|
+
return { ...header, files };
|
|
3024
|
+
}
|
|
3025
|
+
async _sampleVideo(video, probe, hydration) {
|
|
3026
|
+
const { ref, budget, signal } = hydration;
|
|
3027
|
+
const started = Date.now();
|
|
3028
|
+
const count = videoFrameCount(probe, budget);
|
|
3029
|
+
const times = frameTimes(probe.duration, count);
|
|
3030
|
+
const frames = await sampleFrames(video.file, { times, maxEdge: VIDEO_FRAME_EDGE, probe });
|
|
3031
|
+
const sampleMs = Date.now() - started;
|
|
3032
|
+
const heard = probe.hasAudio
|
|
3033
|
+
? await this._hearVideo(ref, () => Promise.resolve(video), signal)
|
|
3034
|
+
: { audio: 'none' };
|
|
3035
|
+
const digest = {
|
|
3036
|
+
duration: probe.duration,
|
|
3037
|
+
times: frames.map((frame) => frame.time),
|
|
3038
|
+
...heard,
|
|
3039
|
+
frames: frames.map((frame) => frame.jpeg)
|
|
3040
|
+
};
|
|
3041
|
+
if (!hydration.ref.digest) {
|
|
3042
|
+
hydration.sampled.push({ ref, digest });
|
|
3043
|
+
}
|
|
3044
|
+
this._log?.info('assistant', 'video frames', {
|
|
3045
|
+
fileName: ref.fileName,
|
|
3046
|
+
duration: probe.duration,
|
|
3047
|
+
frames: frames.length,
|
|
3048
|
+
budget: count,
|
|
3049
|
+
ctxSize: budget.ctxSize ?? null,
|
|
3050
|
+
cacheTokens: budget.cacheTokens ?? null,
|
|
3051
|
+
audio: heard.audio,
|
|
3052
|
+
sampleMs
|
|
3053
|
+
});
|
|
3054
|
+
return this._cacheVideo(ref, digest, hydration);
|
|
3055
|
+
}
|
|
3056
|
+
async _storeVideoDigests(sampled, target) {
|
|
3057
|
+
const dir = this._imageCacheDir;
|
|
3058
|
+
const pinned = this._runImageCacheNames.get(target.runId) ?? new Set();
|
|
3059
|
+
for (const { ref, digest } of sampled) {
|
|
3060
|
+
try {
|
|
3061
|
+
const blobId = await this._uploadVideoDigest(ref, digest, target);
|
|
3062
|
+
await this._cacheVideo(ref, digest, { dir, pinned, digestBlobId: blobId });
|
|
3063
|
+
}
|
|
3064
|
+
catch (err) {
|
|
3065
|
+
this._log?.warn('assistant', `video digest not stored — ${ref.fileName}`, err);
|
|
2463
3066
|
}
|
|
2464
|
-
delete turn.audioAttachments;
|
|
2465
3067
|
}
|
|
2466
3068
|
}
|
|
2467
|
-
async
|
|
2468
|
-
const
|
|
3069
|
+
async _uploadVideoDigest(ref, digest, target) {
|
|
3070
|
+
const attachment = await this._uploadGenerated({
|
|
3071
|
+
fileName: videoDigestFileName(ref.fileName),
|
|
3072
|
+
mimeType: VIDEO_DIGEST_MIME,
|
|
3073
|
+
bytes: packVideoDigest(digest),
|
|
3074
|
+
raw: true
|
|
3075
|
+
}, { ...target, sourceId: ref.id });
|
|
3076
|
+
return attachment.blobId;
|
|
3077
|
+
}
|
|
3078
|
+
async _openVideo({ dir, ref, pinned, signal }) {
|
|
3079
|
+
const bytes = await this._readBlob(ref.url, signal);
|
|
3080
|
+
if (!bytes) {
|
|
3081
|
+
return null;
|
|
3082
|
+
}
|
|
3083
|
+
const name = `${imageCacheName(ref)}.source`;
|
|
3084
|
+
pinned.add(name);
|
|
3085
|
+
const file = path.join(dir, name);
|
|
3086
|
+
await fs.promises.writeFile(file, bytes, { mode: 0o600 });
|
|
3087
|
+
return { bytes, file };
|
|
3088
|
+
}
|
|
3089
|
+
async _hearVideo(ref, open, signal) {
|
|
2469
3090
|
const cached = this._audioTranscripts.get(ref.blobId);
|
|
2470
3091
|
if (cached !== undefined) {
|
|
2471
|
-
return cached ?
|
|
3092
|
+
return cached ? { audio: 'speech', transcript: cached } : { audio: 'silent' };
|
|
2472
3093
|
}
|
|
2473
3094
|
if (!this._harness.transcribe) {
|
|
2474
|
-
return
|
|
2475
|
-
retryable: false
|
|
2476
|
-
});
|
|
3095
|
+
return { audio: 'unheard' };
|
|
2477
3096
|
}
|
|
2478
3097
|
try {
|
|
2479
|
-
const
|
|
2480
|
-
if (!
|
|
2481
|
-
return
|
|
3098
|
+
const video = await open();
|
|
3099
|
+
if (!video) {
|
|
3100
|
+
return { audio: 'unheard' };
|
|
2482
3101
|
}
|
|
2483
3102
|
const model = mediaModelNameFor('transcription', await this._persistedProfile());
|
|
2484
3103
|
await this.ensureMediaModelUnpaused('transcription', model);
|
|
2485
3104
|
await this._downloads?.waitUntilCached(model, signal);
|
|
2486
|
-
const
|
|
2487
|
-
this._audioTranscripts.set(ref.blobId,
|
|
2488
|
-
|
|
2489
|
-
return silentAudioNotice(ref.fileName);
|
|
2490
|
-
}
|
|
2491
|
-
return audioTranscriptBlock(ref.fileName, text);
|
|
3105
|
+
const transcript = (await this._harness.transcribe({ model: '', audio: b4a.toBuffer(video.bytes), signal })).trim();
|
|
3106
|
+
this._audioTranscripts.set(ref.blobId, transcript);
|
|
3107
|
+
return transcript ? { audio: 'speech', transcript } : { audio: 'silent' };
|
|
2492
3108
|
}
|
|
2493
3109
|
catch (err) {
|
|
2494
3110
|
if (signal.aborted) {
|
|
2495
3111
|
throw err;
|
|
2496
3112
|
}
|
|
2497
|
-
this._log?.warn('assistant', `
|
|
2498
|
-
return
|
|
3113
|
+
this._log?.warn('assistant', `video transcription failed — ${ref.fileName}`, err);
|
|
3114
|
+
return { audio: 'unheard' };
|
|
2499
3115
|
}
|
|
2500
3116
|
}
|
|
2501
3117
|
async _hydrateImages(messages, signal, runId) {
|
|
@@ -2534,8 +3150,70 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2534
3150
|
}
|
|
2535
3151
|
return dropped;
|
|
2536
3152
|
}
|
|
3153
|
+
async _servedMedia(sources, { id, modelId, signal }) {
|
|
3154
|
+
if (!(await this._seesImages(modelId))) {
|
|
3155
|
+
return sources.map(() => null);
|
|
3156
|
+
}
|
|
3157
|
+
const dir = this._imageCacheDir;
|
|
3158
|
+
const pinned = new Set();
|
|
3159
|
+
this._runImageCacheNames.set(`round:${id}`, pinned);
|
|
3160
|
+
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
3161
|
+
const docs = new Map();
|
|
3162
|
+
try {
|
|
3163
|
+
const files = [];
|
|
3164
|
+
for (const source of sources) {
|
|
3165
|
+
files.push(await this._servedFile(source, { dir, pinned, docs, signal }).catch(() => null));
|
|
3166
|
+
}
|
|
3167
|
+
return files;
|
|
3168
|
+
}
|
|
3169
|
+
finally {
|
|
3170
|
+
for (const doc of docs.values()) {
|
|
3171
|
+
void doc.then((open) => open?.close(), noop);
|
|
3172
|
+
}
|
|
3173
|
+
}
|
|
3174
|
+
}
|
|
3175
|
+
_servedFile(source, served) {
|
|
3176
|
+
const { ref, attachment } = source;
|
|
3177
|
+
if (ref.frame !== undefined && ref.frames) {
|
|
3178
|
+
return this._servedFrame(source, { frame: ref.frame, frames: ref.frames }, served);
|
|
3179
|
+
}
|
|
3180
|
+
if (ref.page !== undefined) {
|
|
3181
|
+
return this._servedPage(attachment, { page: ref.page, clip: ref.clip }, served);
|
|
3182
|
+
}
|
|
3183
|
+
served.pinned.add(imageCacheName(attachment));
|
|
3184
|
+
return this._hydrateImage(served.dir, attachment, served.signal);
|
|
3185
|
+
}
|
|
3186
|
+
async _servedFrame({ attachment, digest }, { frame, frames }, { dir, pinned, signal }) {
|
|
3187
|
+
const file = path.join(dir, videoFrameCacheName(attachment, frames, frame));
|
|
3188
|
+
pinned.add(path.basename(file));
|
|
3189
|
+
if (await cacheOrTouch(file, noBytes)) {
|
|
3190
|
+
return file;
|
|
3191
|
+
}
|
|
3192
|
+
const bytes = digest ? await this._readBlob(digest.url, signal) : null;
|
|
3193
|
+
const video = bytes && unpackVideoDigest(bytes);
|
|
3194
|
+
if (!digest || !video || video.frames.length !== frames) {
|
|
3195
|
+
return null;
|
|
3196
|
+
}
|
|
3197
|
+
const target = { dir, pinned, digestBlobId: digest.blobId };
|
|
3198
|
+
return (await this._cacheVideo(attachment, video, target)).files[frame] ?? null;
|
|
3199
|
+
}
|
|
3200
|
+
async _servedPage(pdf, { page, clip }, { dir, pinned, docs, signal }) {
|
|
3201
|
+
const name = pdfShotCacheName(pdf, { page, clip });
|
|
3202
|
+
pinned.add(name);
|
|
3203
|
+
const file = path.join(dir, name);
|
|
3204
|
+
const rendered = await cacheOrTouch(file, async () => {
|
|
3205
|
+
let doc = docs.get(pdf.id);
|
|
3206
|
+
if (!doc) {
|
|
3207
|
+
doc = this._readBlob(pdf.url, signal).then((bytes) => bytes ? this._pdfRasterizer.open(bytes) : null);
|
|
3208
|
+
docs.set(pdf.id, doc);
|
|
3209
|
+
}
|
|
3210
|
+
return (await doc)?.renderPage(page, clip) ?? null;
|
|
3211
|
+
});
|
|
3212
|
+
return rendered ? file : null;
|
|
3213
|
+
}
|
|
2537
3214
|
async _hydrateImage(dir, image, signal) {
|
|
2538
3215
|
const file = path.join(dir, imageCacheName(image));
|
|
3216
|
+
this._mediaRefs.set(imageCacheName(image), { id: image.id });
|
|
2539
3217
|
await cacheOrTouch(file, async () => {
|
|
2540
3218
|
const raw = await this._readBlob(image.url, signal);
|
|
2541
3219
|
if (signal.aborted) {
|
|
@@ -2548,126 +3226,153 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2548
3226
|
});
|
|
2549
3227
|
return file;
|
|
2550
3228
|
}
|
|
2551
|
-
async _hydratePdfPages(messages, signal, runId) {
|
|
3229
|
+
async _hydratePdfPages(messages, { signal, runId, window, reading, textChars, wholeIds, selected }) {
|
|
2552
3230
|
const rendered = new Set();
|
|
3231
|
+
const notices = [];
|
|
2553
3232
|
const turns = messages.filter((turn) => turn.pdfs?.length);
|
|
2554
3233
|
if (!turns.length) {
|
|
2555
|
-
return rendered;
|
|
3234
|
+
return { rendered, notices };
|
|
2556
3235
|
}
|
|
2557
3236
|
const dir = this._imageCacheDir;
|
|
2558
3237
|
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
2559
3238
|
const pinned = this._runImageCacheNames.get(runId) ?? new Set();
|
|
2560
3239
|
this._runImageCacheNames.set(runId, pinned);
|
|
2561
3240
|
const latestUser = messages.findLast((turn) => turn.role === 'user');
|
|
3241
|
+
const measured = await this._pdfReading(window, reading);
|
|
2562
3242
|
for (const turn of turns) {
|
|
2563
|
-
|
|
3243
|
+
const room = pdfImageRoom(measured, turn === latestUser ? textChars : 0);
|
|
2564
3244
|
for (const pdf of turn.pdfs ?? []) {
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
pdf,
|
|
2569
|
-
pinned,
|
|
2570
|
-
maxPages: budget,
|
|
2571
|
-
signal
|
|
2572
|
-
});
|
|
2573
|
-
budget -= pages.tried;
|
|
2574
|
-
if (turn === latestUser && pages.names.length) {
|
|
3245
|
+
const pages = await this._tryHydratePdf({ dir, pdf, pinned, room, signal, selected });
|
|
3246
|
+
attachPages(turn, pages.names.map((name) => path.join(dir, name)));
|
|
3247
|
+
if (pages.names.length && turn === latestUser) {
|
|
2575
3248
|
rendered.add(pdf.id);
|
|
2576
3249
|
}
|
|
3250
|
+
const whole = turn === latestUser && wholeIds.has(pdf.id);
|
|
3251
|
+
for (const notice of pdfNotices(pdf.fileName, pages, whole)) {
|
|
3252
|
+
notices.push({ turn, notice });
|
|
3253
|
+
}
|
|
2577
3254
|
}
|
|
2578
3255
|
delete turn.pdfs;
|
|
2579
3256
|
}
|
|
2580
|
-
return rendered;
|
|
3257
|
+
return { rendered, notices };
|
|
2581
3258
|
}
|
|
2582
|
-
async
|
|
3259
|
+
async _pdfReading({ ctxSize, modelName }, options) {
|
|
3260
|
+
const custom = await this._customModel(modelName);
|
|
3261
|
+
const window = effectiveCtxSize({ ctxSize, modelName }, { appCtxSize: 0, platform: this._platform, custom });
|
|
3262
|
+
const { cacheTokens } = await this._contextReading(options, window);
|
|
3263
|
+
return { ctxSize: window, cacheTokens };
|
|
3264
|
+
}
|
|
3265
|
+
async _customModel(name) {
|
|
3266
|
+
for await (const list of this._engine.customModelsWatch({})) {
|
|
3267
|
+
return list.models?.find((row) => row.name === name) ?? null;
|
|
3268
|
+
}
|
|
3269
|
+
return null;
|
|
3270
|
+
}
|
|
3271
|
+
async _tryHydratePdf(opts) {
|
|
2583
3272
|
try {
|
|
2584
|
-
return await this._hydratePdf(
|
|
3273
|
+
return await this._hydratePdf(opts);
|
|
2585
3274
|
}
|
|
2586
3275
|
catch (err) {
|
|
2587
3276
|
if (opts.signal.aborted) {
|
|
2588
3277
|
throw err;
|
|
2589
3278
|
}
|
|
2590
3279
|
this._log?.warn('assistant', `pdf rasterization failed — ${opts.pdf.fileName}`, err);
|
|
2591
|
-
return
|
|
3280
|
+
return NO_PDF_PAGES;
|
|
2592
3281
|
}
|
|
2593
3282
|
}
|
|
2594
|
-
async _hydratePdf(
|
|
2595
|
-
const
|
|
2596
|
-
const
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
3283
|
+
async _hydratePdf(hydration) {
|
|
3284
|
+
const { dir, pdf, pinned, room, signal, selected } = hydration;
|
|
3285
|
+
const key = pdf.pages?.blobId ?? pdf.id;
|
|
3286
|
+
const remember = (selection) => {
|
|
3287
|
+
this._pdfSelections.set(key, selection);
|
|
3288
|
+
for (const { page, clip } of selection.shots) {
|
|
3289
|
+
this._mediaRefs.set(pdfShotCacheName(pdf, { page, clip }), { id: pdf.id, page, clip });
|
|
2600
3290
|
}
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
3291
|
+
if (!pdf.pages) {
|
|
3292
|
+
selected.push({ ref: pdf, selection });
|
|
3293
|
+
}
|
|
3294
|
+
};
|
|
3295
|
+
const known = this._pdfSelections.get(key) ?? (await this._storedPdfSelection(pdf, signal));
|
|
3296
|
+
if (known) {
|
|
3297
|
+
remember(known);
|
|
3298
|
+
const cached = await cachedPdfPages(known, { dir, pdf, pinned });
|
|
3299
|
+
if (cached) {
|
|
3300
|
+
return cached;
|
|
2605
3301
|
}
|
|
2606
3302
|
}
|
|
2607
|
-
const pages = await this._renderPdf(dir, { pdf, pinned, maxPages, signal });
|
|
2608
|
-
if (!pages) {
|
|
2609
|
-
return { names: [], tried: 0 };
|
|
2610
|
-
}
|
|
2611
|
-
this._pdfPages.set(key, pages);
|
|
2612
|
-
attachPages(turn, pages.names.map((name) => path.join(dir, name)));
|
|
2613
|
-
return pages;
|
|
2614
|
-
}
|
|
2615
|
-
async _renderPdf(dir, { pdf, pinned, maxPages, signal }) {
|
|
2616
3303
|
const bytes = await this._readBlob(pdf.url, signal);
|
|
2617
3304
|
if (signal.aborted) {
|
|
2618
3305
|
throw new Error('hydration aborted');
|
|
2619
3306
|
}
|
|
2620
|
-
|
|
2621
|
-
return null;
|
|
2622
|
-
}
|
|
2623
|
-
const doc = await this._pdfRasterizer.open(bytes);
|
|
3307
|
+
const doc = bytes ? await this._pdfRasterizer.open(bytes) : null;
|
|
2624
3308
|
if (!doc) {
|
|
2625
|
-
return
|
|
3309
|
+
return NO_PDF_PAGES;
|
|
2626
3310
|
}
|
|
2627
3311
|
try {
|
|
2628
|
-
const
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
for (let page = 0; page < count && tried < maxPages; page++) {
|
|
2632
|
-
if (signal.aborted)
|
|
2633
|
-
throw new Error('hydration aborted');
|
|
2634
|
-
const outcome = await this._renderPdfPage(doc, { dir, pdf, page, pinned, signal });
|
|
2635
|
-
if (outcome.tried) {
|
|
2636
|
-
tried++;
|
|
2637
|
-
}
|
|
2638
|
-
if (outcome.name) {
|
|
2639
|
-
names.push(outcome.name);
|
|
2640
|
-
}
|
|
3312
|
+
const selection = known ?? (await selectPdfPages(doc, room));
|
|
3313
|
+
if (!known) {
|
|
3314
|
+
remember(selection);
|
|
2641
3315
|
}
|
|
2642
|
-
return
|
|
3316
|
+
return await this._renderPdfShots(doc, selection, hydration);
|
|
2643
3317
|
}
|
|
2644
3318
|
finally {
|
|
2645
3319
|
doc.close();
|
|
2646
3320
|
}
|
|
2647
3321
|
}
|
|
2648
|
-
async
|
|
2649
|
-
|
|
3322
|
+
async _storedPdfSelection(pdf, signal) {
|
|
3323
|
+
const bytes = pdf.pages ? await this._readBlob(pdf.pages.url, signal) : null;
|
|
3324
|
+
if (!bytes) {
|
|
3325
|
+
return null;
|
|
3326
|
+
}
|
|
2650
3327
|
try {
|
|
2651
|
-
|
|
2652
|
-
if (flags.hasText && !flags.hasImage) {
|
|
2653
|
-
return { tried };
|
|
2654
|
-
}
|
|
2655
|
-
tried = true;
|
|
2656
|
-
const name = pdfPageCacheName(pdf, page);
|
|
2657
|
-
pinned.add(name);
|
|
2658
|
-
const cached = await cacheOrTouch(path.join(dir, name), () => doc.renderPage(page));
|
|
2659
|
-
return { tried, name: cached ? name : undefined };
|
|
3328
|
+
return parsePdfSelection(bytes);
|
|
2660
3329
|
}
|
|
2661
3330
|
catch (err) {
|
|
2662
|
-
|
|
2663
|
-
|
|
3331
|
+
this._log?.warn('assistant', `pdf page choice unreadable — ${pdf.fileName}`, err);
|
|
3332
|
+
return null;
|
|
3333
|
+
}
|
|
3334
|
+
}
|
|
3335
|
+
async _renderPdfShots(doc, selection, { dir, pdf, pinned, signal }) {
|
|
3336
|
+
const names = [];
|
|
3337
|
+
const labels = [];
|
|
3338
|
+
for (const shot of selection.shots) {
|
|
3339
|
+
if (signal.aborted)
|
|
3340
|
+
throw new Error('hydration aborted');
|
|
3341
|
+
const name = pdfShotCacheName(pdf, shot);
|
|
3342
|
+
pinned.add(name);
|
|
3343
|
+
try {
|
|
3344
|
+
const file = path.join(dir, name);
|
|
3345
|
+
if (await cacheOrTouch(file, () => doc.renderPage(shot.page, shot.clip))) {
|
|
3346
|
+
names.push(name);
|
|
3347
|
+
labels.push(shot.label);
|
|
3348
|
+
}
|
|
3349
|
+
}
|
|
3350
|
+
catch (err) {
|
|
3351
|
+
if (signal.aborted) {
|
|
3352
|
+
throw err;
|
|
3353
|
+
}
|
|
3354
|
+
this._log?.warn('assistant', `pdf page skipped — ${pdf.fileName} p${shot.page}`, err);
|
|
3355
|
+
}
|
|
3356
|
+
}
|
|
3357
|
+
return { selection, names, labels };
|
|
3358
|
+
}
|
|
3359
|
+
async _storePdfSelections(selected, target) {
|
|
3360
|
+
for (const { ref, selection } of selected) {
|
|
3361
|
+
try {
|
|
3362
|
+
await this._uploadGenerated({
|
|
3363
|
+
fileName: pdfSelectionFileName(ref.fileName),
|
|
3364
|
+
mimeType: PDF_PAGES_MIME,
|
|
3365
|
+
bytes: packPdfSelection(selection),
|
|
3366
|
+
raw: true
|
|
3367
|
+
}, { ...target, sourceId: ref.id });
|
|
3368
|
+
}
|
|
3369
|
+
catch (err) {
|
|
3370
|
+
this._log?.warn('assistant', `pdf page choice not stored — ${ref.fileName}`, err);
|
|
2664
3371
|
}
|
|
2665
|
-
this._log?.warn('assistant', `pdf page skipped — ${pdf.fileName} p${page}`, err);
|
|
2666
|
-
return { tried };
|
|
2667
3372
|
}
|
|
2668
3373
|
}
|
|
2669
3374
|
async _compactIfNearFull(params) {
|
|
2670
|
-
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
3375
|
+
const { chatId, transcript, trigger, signal, meshId, run, rounds } = params;
|
|
2671
3376
|
const metrics = await this._lastRunMetrics(chatId, meshId);
|
|
2672
3377
|
if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize)) {
|
|
2673
3378
|
return false;
|
|
@@ -2685,11 +3390,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2685
3390
|
trigger,
|
|
2686
3391
|
signal,
|
|
2687
3392
|
occupancy: metrics,
|
|
2688
|
-
meshId
|
|
3393
|
+
meshId,
|
|
3394
|
+
run,
|
|
3395
|
+
rounds
|
|
2689
3396
|
});
|
|
2690
3397
|
}
|
|
2691
3398
|
async _runCompaction(params) {
|
|
2692
|
-
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
3399
|
+
const { chatId, transcript, trigger, signal, meshId, run, rounds } = params;
|
|
2693
3400
|
let statusId = null;
|
|
2694
3401
|
const started = Date.now();
|
|
2695
3402
|
try {
|
|
@@ -2702,7 +3409,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2702
3409
|
statusId = status.id;
|
|
2703
3410
|
},
|
|
2704
3411
|
meshId,
|
|
2705
|
-
signal
|
|
3412
|
+
signal,
|
|
3413
|
+
run,
|
|
3414
|
+
rounds
|
|
2706
3415
|
});
|
|
2707
3416
|
this._log?.info('runs', 'compaction', {
|
|
2708
3417
|
chatId,
|
|
@@ -2727,6 +3436,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2727
3436
|
}
|
|
2728
3437
|
}
|
|
2729
3438
|
}
|
|
3439
|
+
async _contextReading({ chatId, meshId, folded }, ctxSize) {
|
|
3440
|
+
const last = folded ? {} : await this._lastRunMetrics(chatId, meshId);
|
|
3441
|
+
const window = ctxSize || last.ctxSize;
|
|
3442
|
+
if (!window) {
|
|
3443
|
+
return {};
|
|
3444
|
+
}
|
|
3445
|
+
return last.ctxSize === window
|
|
3446
|
+
? { ctxSize: window, cacheTokens: last.cacheTokens }
|
|
3447
|
+
: { ctxSize: window };
|
|
3448
|
+
}
|
|
2730
3449
|
async _lastRunMetrics(chatId, meshId) {
|
|
2731
3450
|
const deviceId = this._requireDeviceId();
|
|
2732
3451
|
const { chunks } = await meshScoped(this._engine, meshId)
|
|
@@ -2760,7 +3479,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2760
3479
|
return { compacted: false, reason: 'unknown chat' };
|
|
2761
3480
|
}
|
|
2762
3481
|
const agent = await engine.getAgent({ agentId: chat.agentId }).catch(noop);
|
|
2763
|
-
if (!agent || !
|
|
3482
|
+
if (!agent || !(options.run || b4a.equals(agent.providerDeviceId, deviceId))) {
|
|
2764
3483
|
return { compacted: false, reason: 'not the provider device' };
|
|
2765
3484
|
}
|
|
2766
3485
|
const model = runnableModelName(agent.modelName, await this._runnableHost());
|
|
@@ -2769,20 +3488,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2769
3488
|
if (ctxSize !== undefined && !canCompact(ctxSize)) {
|
|
2770
3489
|
return { compacted: false, reason: 'below-floor' };
|
|
2771
3490
|
}
|
|
2772
|
-
const
|
|
3491
|
+
const all = await listHistoryChunks(engine, chatId);
|
|
3492
|
+
const stored = foldableView(all, options.run);
|
|
2773
3493
|
const branches = historyBranches(stored);
|
|
2774
3494
|
const chunks = stored.filter((chunk) => isPromptChunk(chunk, branches));
|
|
2775
3495
|
const { chunks: priorAsks } = await engine
|
|
2776
3496
|
.listChunks({ chatId, type: 'approval-request' })
|
|
2777
3497
|
.catch(() => ({ chunks: [] }));
|
|
2778
3498
|
const lastThroughId = lastCompactedThroughId(chunks);
|
|
2779
|
-
const lastThroughSeq =
|
|
2780
|
-
const
|
|
3499
|
+
const lastThroughSeq = all.find((chunk) => chunk.id === lastThroughId)?.seq ?? -1;
|
|
3500
|
+
const beforeSeq = options.run?.request.seq ?? Infinity;
|
|
3501
|
+
const userTurns = chunks.filter((chunk) => chunk.userMessage && chunk.seq < beforeSeq).length;
|
|
2781
3502
|
let announced = false;
|
|
2782
3503
|
let keepTurns = COMPACTION_KEEP_TURNS;
|
|
2783
|
-
const senders = await this._senderNames(engine, chunks);
|
|
2784
3504
|
for (;;) {
|
|
2785
|
-
const boundary = compactionBoundary(chunks, keepTurns);
|
|
3505
|
+
const boundary = compactionBoundary(chunks, keepTurns, beforeSeq);
|
|
2786
3506
|
if (!boundary) {
|
|
2787
3507
|
return { compacted: false, reason: 'nothing-to-fold' };
|
|
2788
3508
|
}
|
|
@@ -2793,8 +3513,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2793
3513
|
if (through.seq <= lastThroughSeq) {
|
|
2794
3514
|
return { compacted: false, reason: 'already-compacted' };
|
|
2795
3515
|
}
|
|
2796
|
-
const
|
|
2797
|
-
const
|
|
3516
|
+
const folds = foldsThrough(chunks, through);
|
|
3517
|
+
const inRange = chunks.filter((chunk) => chunk.compaction || folds(chunk));
|
|
3518
|
+
const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''));
|
|
2798
3519
|
if (!announced) {
|
|
2799
3520
|
await options.onFold?.();
|
|
2800
3521
|
announced = true;
|
|
@@ -2802,7 +3523,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2802
3523
|
const attempt = await this._summarize(folded, {
|
|
2803
3524
|
model,
|
|
2804
3525
|
agent: withConfigDefaults(agent, config),
|
|
2805
|
-
signal: options.signal
|
|
3526
|
+
signal: options.signal,
|
|
3527
|
+
rounds: options.rounds
|
|
2806
3528
|
});
|
|
2807
3529
|
if (!attempt.summary) {
|
|
2808
3530
|
if (attempt.overflowed && boundary.foldedTurns > 1) {
|
|
@@ -2833,9 +3555,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2833
3555
|
return { compacted: true, foldedTurns: boundary.foldedTurns };
|
|
2834
3556
|
}
|
|
2835
3557
|
}
|
|
2836
|
-
async _summarize(turns, { model, agent, signal }) {
|
|
3558
|
+
async _summarize(turns, { model, agent, signal, rounds }) {
|
|
2837
3559
|
const hasPriorSummary = turns[0]?.content.startsWith(COMPACTION_TURN_PREFIX) === true;
|
|
2838
|
-
const
|
|
3560
|
+
const prompt = buildCompactionPrompt(nameSenders(turns).map((turn) => ({ role: turn.role, content: turn.content })), hasPriorSummary);
|
|
3561
|
+
const round = this._probeRounds(rounds);
|
|
3562
|
+
if (round) {
|
|
3563
|
+
return this._summarizeInRound(round, { prompt, model, signal });
|
|
3564
|
+
}
|
|
3565
|
+
const events = this._harness.completion({
|
|
2839
3566
|
model,
|
|
2840
3567
|
agent: {
|
|
2841
3568
|
...agent,
|
|
@@ -2844,12 +3571,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2844
3571
|
skillsScope: 'selected',
|
|
2845
3572
|
enabledSkills: []
|
|
2846
3573
|
},
|
|
2847
|
-
messages: [
|
|
2848
|
-
{
|
|
2849
|
-
role: 'user',
|
|
2850
|
-
content: buildCompactionPrompt(nameSenders(turns).map((turn) => ({ role: turn.role, content: turn.content })), hasPriorSummary)
|
|
2851
|
-
}
|
|
2852
|
-
],
|
|
3574
|
+
messages: [{ role: 'user', content: prompt }],
|
|
2853
3575
|
generationParams: { reasoning_budget: 0 },
|
|
2854
3576
|
signal: signal ?? this._lifecycle.signal
|
|
2855
3577
|
});
|
|
@@ -2877,9 +3599,26 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2877
3599
|
}
|
|
2878
3600
|
return { summary: parseCompactionSummary(text), overflowed: false };
|
|
2879
3601
|
}
|
|
2880
|
-
async
|
|
3602
|
+
async _summarizeInRound(rounds, { prompt, model, signal }) {
|
|
3603
|
+
try {
|
|
3604
|
+
const text = await rounds.probe({
|
|
3605
|
+
kind: 'fold',
|
|
3606
|
+
prompt,
|
|
3607
|
+
modelId: model,
|
|
3608
|
+
signal: signal ?? this._lifecycle.signal
|
|
3609
|
+
});
|
|
3610
|
+
return { summary: parseCompactionSummary(text), overflowed: false };
|
|
3611
|
+
}
|
|
3612
|
+
catch (err) {
|
|
3613
|
+
const failed = errorMessage(err);
|
|
3614
|
+
this._log?.warn('runs', 'summarizer round failed', { failed });
|
|
3615
|
+
return { summary: null, overflowed: isContextOverflowFailure(failed) };
|
|
3616
|
+
}
|
|
3617
|
+
}
|
|
3618
|
+
async _autoTitle(engine, { chatId, history, messages, model, agent, probe, rounds }) {
|
|
2881
3619
|
try {
|
|
2882
|
-
const first =
|
|
3620
|
+
const first = history.find((chunk) => chunk.userMessage?.text.trim())?.userMessage?.text ??
|
|
3621
|
+
messages.find((turn) => turn.role === 'user' && turn.content.trim())?.content;
|
|
2883
3622
|
if (!first) {
|
|
2884
3623
|
return;
|
|
2885
3624
|
}
|
|
@@ -2887,14 +3626,62 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2887
3626
|
if (!isDefaultChatTitle(chat?.title)) {
|
|
2888
3627
|
return;
|
|
2889
3628
|
}
|
|
2890
|
-
await this._generateChatTitle({ engine, chatId, input: first
|
|
3629
|
+
await this._generateChatTitle({ engine, chatId, input: first, model, agent, probe, rounds });
|
|
2891
3630
|
}
|
|
2892
3631
|
catch (err) {
|
|
2893
3632
|
this._log?.warn('runs', 'auto-title skipped', { chatId, err });
|
|
2894
3633
|
}
|
|
2895
3634
|
}
|
|
3635
|
+
_probeRounds(rounds) {
|
|
3636
|
+
return this._roundHost ? rounds : undefined;
|
|
3637
|
+
}
|
|
3638
|
+
async _suggestNext(engine, { chatId, runId, messages, answer, model, agent, rounds }) {
|
|
3639
|
+
const question = messages.findLast((turn) => turn.role === 'user' && turn.content.trim());
|
|
3640
|
+
if (!question || this.closing || !(await this._suggestingPrompts())) {
|
|
3641
|
+
return;
|
|
3642
|
+
}
|
|
3643
|
+
const { chunks } = await engine.listChunks({ chatId, type: 'run-request' });
|
|
3644
|
+
if ((chunks ?? []).at(-1)?.runId !== runId) {
|
|
3645
|
+
return;
|
|
3646
|
+
}
|
|
3647
|
+
const controller = new AbortController();
|
|
3648
|
+
this._suggesting.get(chatId)?.abort(SUGGESTION_SUPERSEDED);
|
|
3649
|
+
this._suggesting.set(chatId, controller);
|
|
3650
|
+
try {
|
|
3651
|
+
const raw = await this._probeCompletion({
|
|
3652
|
+
chatId,
|
|
3653
|
+
input: buildSuggestionPrompt(question.content, answer),
|
|
3654
|
+
model,
|
|
3655
|
+
agent,
|
|
3656
|
+
signal: controller.signal,
|
|
3657
|
+
rounds
|
|
3658
|
+
}, SUGGESTION_PROBE);
|
|
3659
|
+
const text = parseSuggestionResponse(raw);
|
|
3660
|
+
if (text && !controller.signal.aborted) {
|
|
3661
|
+
await engine.setChatSuggestion({ chatId, suggestion: { text, runId } });
|
|
3662
|
+
}
|
|
3663
|
+
}
|
|
3664
|
+
catch (err) {
|
|
3665
|
+
this._log?.warn('runs', 'next-message suggestion skipped', { chatId, err });
|
|
3666
|
+
}
|
|
3667
|
+
finally {
|
|
3668
|
+
if (this._suggesting.get(chatId) === controller) {
|
|
3669
|
+
this._suggesting.delete(chatId);
|
|
3670
|
+
}
|
|
3671
|
+
}
|
|
3672
|
+
}
|
|
3673
|
+
async _suggestingPrompts() {
|
|
3674
|
+
try {
|
|
3675
|
+
const { value } = await this._engine.configGet({ key: SUGGEST_PROMPTS_KEY });
|
|
3676
|
+
return value === SUGGEST_PROMPTS_ON;
|
|
3677
|
+
}
|
|
3678
|
+
catch (err) {
|
|
3679
|
+
this._log?.warn('assistant', 'suggestion setting unreadable', { err });
|
|
3680
|
+
return false;
|
|
3681
|
+
}
|
|
3682
|
+
}
|
|
2896
3683
|
async _generateChatTitle(params) {
|
|
2897
|
-
const { engine, chatId, input, model, agent, probe } = params;
|
|
3684
|
+
const { engine, chatId, input, model, agent, probe, rounds } = params;
|
|
2898
3685
|
const prompt = input.trim();
|
|
2899
3686
|
if (!prompt) {
|
|
2900
3687
|
return null;
|
|
@@ -2907,7 +3694,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2907
3694
|
input: prompt,
|
|
2908
3695
|
model,
|
|
2909
3696
|
agent,
|
|
2910
|
-
signal: this._lifecycle.signal
|
|
3697
|
+
signal: this._lifecycle.signal,
|
|
3698
|
+
rounds
|
|
2911
3699
|
});
|
|
2912
3700
|
}
|
|
2913
3701
|
catch (err) {
|
|
@@ -2927,8 +3715,29 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2927
3715
|
return title;
|
|
2928
3716
|
}
|
|
2929
3717
|
async _titleFromCompletion(params) {
|
|
2930
|
-
const
|
|
2931
|
-
|
|
3718
|
+
const prompt = buildTitlePrompt(params.input);
|
|
3719
|
+
return parseTitleResponse(await this._probeCompletion({ ...params, input: prompt }, TITLE_PROBE));
|
|
3720
|
+
}
|
|
3721
|
+
async _probeCompletion(params, { kind, field, name, runId }) {
|
|
3722
|
+
const { chatId, input, model, agent, signal, rounds } = params;
|
|
3723
|
+
const responseFormat = {
|
|
3724
|
+
type: 'json_schema',
|
|
3725
|
+
json_schema: {
|
|
3726
|
+
name,
|
|
3727
|
+
schema: {
|
|
3728
|
+
type: 'object',
|
|
3729
|
+
properties: { [field]: { type: 'string' } },
|
|
3730
|
+
required: [field],
|
|
3731
|
+
additionalProperties: false
|
|
3732
|
+
}
|
|
3733
|
+
}
|
|
3734
|
+
};
|
|
3735
|
+
const round = this._probeRounds(rounds);
|
|
3736
|
+
if (round) {
|
|
3737
|
+
const probed = { kind, prompt: input, modelId: model, responseFormat };
|
|
3738
|
+
return round.probe({ ...probed, signal: signal ?? this._lifecycle.signal });
|
|
3739
|
+
}
|
|
3740
|
+
const events = this._harness.completion({
|
|
2932
3741
|
model,
|
|
2933
3742
|
agent: {
|
|
2934
3743
|
...agent,
|
|
@@ -2937,21 +3746,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2937
3746
|
skillsScope: 'selected',
|
|
2938
3747
|
enabledSkills: []
|
|
2939
3748
|
},
|
|
2940
|
-
messages: [{ role: 'user', content:
|
|
3749
|
+
messages: [{ role: 'user', content: input }],
|
|
2941
3750
|
generationParams: { reasoning_budget: 0 },
|
|
2942
3751
|
captureInferenceCalls: await this._capturingInferenceCalls(),
|
|
2943
|
-
responseFormat
|
|
2944
|
-
type: 'json_schema',
|
|
2945
|
-
json_schema: {
|
|
2946
|
-
name: 'chat_title',
|
|
2947
|
-
schema: {
|
|
2948
|
-
type: 'object',
|
|
2949
|
-
properties: { title: { type: 'string' } },
|
|
2950
|
-
required: ['title'],
|
|
2951
|
-
additionalProperties: false
|
|
2952
|
-
}
|
|
2953
|
-
}
|
|
2954
|
-
},
|
|
3752
|
+
responseFormat,
|
|
2955
3753
|
signal
|
|
2956
3754
|
});
|
|
2957
3755
|
let text = '';
|
|
@@ -2961,14 +3759,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2961
3759
|
}
|
|
2962
3760
|
else if (event.type === 'inference-call') {
|
|
2963
3761
|
void this._inferenceCalls
|
|
2964
|
-
.record(chatId, { runId
|
|
2965
|
-
.catch((err) => this._log?.warn('assistant', '
|
|
3762
|
+
.record(chatId, { runId, at: Date.now(), call: asRecord(event.call) })
|
|
3763
|
+
.catch((err) => this._log?.warn('assistant', 'probe call not recorded', { runId, err }));
|
|
2966
3764
|
}
|
|
2967
3765
|
else if (event.type === 'error' || event.type === 'aborted' || event.type === 'done') {
|
|
2968
3766
|
break;
|
|
2969
3767
|
}
|
|
2970
3768
|
}
|
|
2971
|
-
return
|
|
3769
|
+
return text;
|
|
2972
3770
|
}
|
|
2973
3771
|
_createApprove(engine, { chatId, runId, deviceId, agentId, hasRun }) {
|
|
2974
3772
|
const waiters = new Map();
|
|
@@ -3042,14 +3840,6 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3042
3840
|
if (!ask || ask.name !== call.name) {
|
|
3043
3841
|
return false;
|
|
3044
3842
|
}
|
|
3045
|
-
const currentIdentity = callIdentity(call);
|
|
3046
|
-
const currentHasIdentity = typeof currentIdentity.callId === 'string' && typeof currentIdentity.round === 'number';
|
|
3047
|
-
const previousHasIdentity = typeof chunk.callId === 'string' && typeof chunk.round === 'number';
|
|
3048
|
-
if (currentHasIdentity !== previousHasIdentity ||
|
|
3049
|
-
(currentHasIdentity &&
|
|
3050
|
-
(chunk.callId !== currentIdentity.callId || chunk.round !== currentIdentity.round))) {
|
|
3051
|
-
return false;
|
|
3052
|
-
}
|
|
3053
3843
|
return ((ask.resource?.kind ?? null) === (call.resource?.kind ?? null) &&
|
|
3054
3844
|
(ask.resource?.label ?? null) === (call.resource?.label ?? null) &&
|
|
3055
3845
|
JSON.stringify(ask.arguments) === JSON.stringify(call.arguments));
|
|
@@ -3095,12 +3885,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3095
3885
|
});
|
|
3096
3886
|
return park(prior.id, call.name);
|
|
3097
3887
|
}
|
|
3098
|
-
|
|
3099
|
-
const sameEngineCall = typeof identity.callId === 'string' &&
|
|
3100
|
-
typeof identity.round === 'number' &&
|
|
3101
|
-
prior.callId === identity.callId &&
|
|
3102
|
-
prior.round === identity.round;
|
|
3103
|
-
if (sameEngineCall || !hasRun()) {
|
|
3888
|
+
if (!hasRun()) {
|
|
3104
3889
|
this._log?.info('approvals', `answer ${status} (from before the app closed)`, {
|
|
3105
3890
|
runId,
|
|
3106
3891
|
chatId,
|
|
@@ -3109,7 +3894,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3109
3894
|
});
|
|
3110
3895
|
return status === 'approved';
|
|
3111
3896
|
}
|
|
3112
|
-
this._log?.info('approvals', 'asking again:
|
|
3897
|
+
this._log?.info('approvals', 'asking again: the replay would repeat a call that ran', {
|
|
3113
3898
|
runId,
|
|
3114
3899
|
chatId,
|
|
3115
3900
|
priorChunkId: prior.id,
|
|
@@ -3123,13 +3908,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3123
3908
|
resource: call.resource ?? undefined,
|
|
3124
3909
|
agentId
|
|
3125
3910
|
};
|
|
3126
|
-
const identity = callIdentity(call);
|
|
3127
3911
|
const chunk = await engine.appendChunk({
|
|
3128
3912
|
chatId,
|
|
3129
3913
|
runId,
|
|
3130
3914
|
type: 'approval-request',
|
|
3131
|
-
callId: identity.callId,
|
|
3132
|
-
round: identity.round,
|
|
3133
3915
|
approvalRequest,
|
|
3134
3916
|
originDeviceId: deviceId,
|
|
3135
3917
|
effect: call.effect === 'read' ? 'read' : 'write',
|
|
@@ -3176,7 +3958,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3176
3958
|
if (known) {
|
|
3177
3959
|
return known;
|
|
3178
3960
|
}
|
|
3179
|
-
if (this.
|
|
3961
|
+
if (this._backgrounded) {
|
|
3962
|
+
this._backgroundReplays.add(runId);
|
|
3180
3963
|
return { kind: 'replay' };
|
|
3181
3964
|
}
|
|
3182
3965
|
if (reason !== undefined) {
|
|
@@ -3210,21 +3993,39 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3210
3993
|
}
|
|
3211
3994
|
};
|
|
3212
3995
|
}
|
|
3213
|
-
_watchOwnership(engine, agentId, controller) {
|
|
3996
|
+
_watchOwnership(engine, { request, agentId, controller }) {
|
|
3214
3997
|
const deviceId = this._requireDeviceId();
|
|
3215
|
-
const
|
|
3998
|
+
const watches = [
|
|
3999
|
+
this._abortOnLoss(engine.agentsWatch({}), controller, async ({ agents }) => {
|
|
4000
|
+
const row = (agents ?? []).find((a) => a.id === agentId);
|
|
4001
|
+
if (row && !row.deletedAt) {
|
|
4002
|
+
return drivenBy({ request, agent: row }, deviceId) ? null : OWNERSHIP_LOST;
|
|
4003
|
+
}
|
|
4004
|
+
const chat = await engine.getChat({ chatId: request.chatId }).catch(noop);
|
|
4005
|
+
return !chat || chat.agentId === agentId ? AGENT_REMOVED : null;
|
|
4006
|
+
})
|
|
4007
|
+
];
|
|
4008
|
+
if (request.runnerDeviceId) {
|
|
4009
|
+
watches.push(this._abortOnLoss(engine.runRequestsWatch({}), controller, ({ chunks }) => {
|
|
4010
|
+
const now = (chunks ?? []).find((chunk) => chunk.id === request.id);
|
|
4011
|
+
return !now?.runTakenBy || !b4a.equals(now.runTakenBy, deviceId) ? OWNERSHIP_LOST : null;
|
|
4012
|
+
}));
|
|
4013
|
+
}
|
|
4014
|
+
return {
|
|
4015
|
+
stop: () => watches.forEach((stop) => stop())
|
|
4016
|
+
};
|
|
4017
|
+
}
|
|
4018
|
+
_abortOnLoss(frames, controller, lost) {
|
|
4019
|
+
const it = frames[Symbol.asyncIterator]();
|
|
3216
4020
|
const watching = (async () => {
|
|
3217
4021
|
while (true) {
|
|
3218
4022
|
const { value, done } = await it.next();
|
|
3219
4023
|
if (done) {
|
|
3220
4024
|
return;
|
|
3221
4025
|
}
|
|
3222
|
-
const
|
|
3223
|
-
if (
|
|
3224
|
-
|
|
3225
|
-
!row.providerDeviceId ||
|
|
3226
|
-
!b4a.equals(row.providerDeviceId, deviceId)) {
|
|
3227
|
-
controller.abort(OWNERSHIP_LOST);
|
|
4026
|
+
const reason = await lost(value);
|
|
4027
|
+
if (reason) {
|
|
4028
|
+
controller.abort(reason);
|
|
3228
4029
|
return;
|
|
3229
4030
|
}
|
|
3230
4031
|
}
|
|
@@ -3234,14 +4035,43 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3234
4035
|
return;
|
|
3235
4036
|
this._log?.error('assistant', 'ownership watch failed', err);
|
|
3236
4037
|
});
|
|
3237
|
-
return {
|
|
3238
|
-
|
|
3239
|
-
it.return?.()?.catch?.(noop);
|
|
3240
|
-
}
|
|
4038
|
+
return () => {
|
|
4039
|
+
it.return?.()?.catch?.(noop);
|
|
3241
4040
|
};
|
|
3242
4041
|
}
|
|
3243
|
-
|
|
4042
|
+
_runsChanged() {
|
|
3244
4043
|
this._downloads?.yieldToRuns([...this._runs.values()].filter((run) => run.executing).map((run) => run.model));
|
|
4044
|
+
if (this._generating()) {
|
|
4045
|
+
return;
|
|
4046
|
+
}
|
|
4047
|
+
for (const wake of [...this._idleWaiters]) {
|
|
4048
|
+
wake();
|
|
4049
|
+
}
|
|
4050
|
+
}
|
|
4051
|
+
_generating() {
|
|
4052
|
+
return [...this._runs.values()].some((run) => run.executing && !run.preparing);
|
|
4053
|
+
}
|
|
4054
|
+
_markPreparing(runId, preparing) {
|
|
4055
|
+
const run = this._runs.get(runId);
|
|
4056
|
+
if (!run) {
|
|
4057
|
+
return;
|
|
4058
|
+
}
|
|
4059
|
+
this._runs.set(runId, { ...run, preparing });
|
|
4060
|
+
this._runsChanged();
|
|
4061
|
+
}
|
|
4062
|
+
idle(signal) {
|
|
4063
|
+
if (signal.aborted || !this._generating()) {
|
|
4064
|
+
return Promise.resolve();
|
|
4065
|
+
}
|
|
4066
|
+
return new Promise((resolve) => {
|
|
4067
|
+
const wake = () => {
|
|
4068
|
+
this._idleWaiters.delete(wake);
|
|
4069
|
+
signal.removeEventListener('abort', wake);
|
|
4070
|
+
resolve();
|
|
4071
|
+
};
|
|
4072
|
+
this._idleWaiters.add(wake);
|
|
4073
|
+
signal.addEventListener('abort', wake, { once: true });
|
|
4074
|
+
});
|
|
3245
4075
|
}
|
|
3246
4076
|
_logModelStatus(state) {
|
|
3247
4077
|
if (typeof state !== 'object' || state === null)
|
|
@@ -3272,6 +4102,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3272
4102
|
let transcribing = false;
|
|
3273
4103
|
let documents = false;
|
|
3274
4104
|
let sampling = false;
|
|
4105
|
+
let analyzing = false;
|
|
3275
4106
|
const apply = (progress) => {
|
|
3276
4107
|
const runStatus = progress
|
|
3277
4108
|
? { state: 'executing', progress }
|
|
@@ -3279,12 +4110,15 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3279
4110
|
if (documents) {
|
|
3280
4111
|
runStatus.reason = 'preparing-documents';
|
|
3281
4112
|
}
|
|
4113
|
+
else if (analyzing && !progress) {
|
|
4114
|
+
runStatus.reason = 'analyzing-images';
|
|
4115
|
+
}
|
|
3282
4116
|
const key = JSON.stringify(runStatus);
|
|
3283
4117
|
if (key === last) {
|
|
3284
4118
|
return;
|
|
3285
4119
|
}
|
|
3286
4120
|
last = key;
|
|
3287
|
-
showing = progress !== null ||
|
|
4121
|
+
showing = progress !== null || runStatus.reason !== undefined;
|
|
3288
4122
|
update(runStatus).catch(noop);
|
|
3289
4123
|
};
|
|
3290
4124
|
const onModel = (state) => {
|
|
@@ -3293,9 +4127,11 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3293
4127
|
if (state.status === 'loading') {
|
|
3294
4128
|
apply({ phase: 'loading', model: state.model });
|
|
3295
4129
|
}
|
|
4130
|
+
else if (state.status === 'held') {
|
|
4131
|
+
apply({ phase: 'awaiting-download', model: state.model });
|
|
4132
|
+
}
|
|
3296
4133
|
else if (state.status === 'downloading') {
|
|
3297
|
-
|
|
3298
|
-
apply({ phase: 'downloading', model: state.model, percentage });
|
|
4134
|
+
apply(downloadingProgress(state.model, state.percentage ?? 0));
|
|
3299
4135
|
}
|
|
3300
4136
|
else {
|
|
3301
4137
|
apply(transcribing ? { phase: 'transcribing' } : null);
|
|
@@ -3320,9 +4156,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3320
4156
|
}
|
|
3321
4157
|
apply({ phase: 'generating', percentage: Math.floor(progress.percentage / 10) * 10 });
|
|
3322
4158
|
},
|
|
4159
|
+
request: (messages, sent) => {
|
|
4160
|
+
if (claimNewImages(messages, sent)) {
|
|
4161
|
+
analyzing = true;
|
|
4162
|
+
apply(null);
|
|
4163
|
+
}
|
|
4164
|
+
},
|
|
3323
4165
|
stop: () => this._harness.off?.('model', onModel),
|
|
3324
4166
|
clear: () => {
|
|
3325
4167
|
sampling = false;
|
|
4168
|
+
analyzing = false;
|
|
3326
4169
|
if (!showing) {
|
|
3327
4170
|
return;
|
|
3328
4171
|
}
|
|
@@ -3333,14 +4176,47 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3333
4176
|
};
|
|
3334
4177
|
}
|
|
3335
4178
|
}
|
|
3336
|
-
|
|
4179
|
+
function notReady(search) {
|
|
4180
|
+
return {
|
|
4181
|
+
chatFilesNotReady: search.chatFilesNotReady ?? 0,
|
|
4182
|
+
projectFilesNotReady: search.projectFilesNotReady ?? 0
|
|
4183
|
+
};
|
|
4184
|
+
}
|
|
4185
|
+
function notReadyCount(search) {
|
|
4186
|
+
const { chatFilesNotReady, projectFilesNotReady } = notReady(search);
|
|
4187
|
+
return chatFilesNotReady + projectFilesNotReady;
|
|
4188
|
+
}
|
|
4189
|
+
function notReadyLine(search) {
|
|
4190
|
+
const { projectFilesNotReady } = notReady(search);
|
|
4191
|
+
const chat = search.pendingFiles ?? [];
|
|
4192
|
+
const parts = [];
|
|
4193
|
+
if (chat.length) {
|
|
4194
|
+
parts.push(`${filesCount(chat.length, 'chat')} (${chat.join(', ')})`);
|
|
4195
|
+
}
|
|
4196
|
+
if (projectFilesNotReady) {
|
|
4197
|
+
parts.push(filesCount(projectFilesNotReady, 'project'));
|
|
4198
|
+
}
|
|
4199
|
+
return parts.length
|
|
4200
|
+
? `[Still indexing and not used for this reply: ${parts.join(' and ')}.]`
|
|
4201
|
+
: null;
|
|
4202
|
+
}
|
|
4203
|
+
function filesCount(count, source) {
|
|
4204
|
+
return `${count} ${source} file${count === 1 ? '' : 's'}`;
|
|
4205
|
+
}
|
|
4206
|
+
function chunkSources(records) {
|
|
4207
|
+
return records.map((record) => ({ ...record, page: record.page ?? 0 }));
|
|
4208
|
+
}
|
|
4209
|
+
function downloadingProgress(model, percentage) {
|
|
4210
|
+
return { phase: 'downloading', model, percentage: Math.floor(percentage / 10) * 10 };
|
|
4211
|
+
}
|
|
4212
|
+
async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, pictured, injected }) {
|
|
3337
4213
|
const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
|
|
3338
4214
|
const notice = chooseRagNotice({
|
|
3339
4215
|
errorMessage: knowledge.search.errorMessage,
|
|
3340
4216
|
hitCount: records.length,
|
|
3341
4217
|
searchedCount: knowledge.search.searchedCount ?? 0,
|
|
3342
4218
|
indexEmpty: Boolean(knowledge.search.indexEmpty),
|
|
3343
|
-
hasKnowledge:
|
|
4219
|
+
hasKnowledge: !!knowledge.search.hasKnowledge || notReadyCount(knowledge.search) > 0,
|
|
3344
4220
|
pendingReferenced: Boolean(knowledge.search.pendingReferenced),
|
|
3345
4221
|
failedFiles: knowledge.search.failedFiles ?? [],
|
|
3346
4222
|
pendingFiles: knowledge.search.pendingFiles ?? []
|
|
@@ -3352,11 +4228,17 @@ async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter
|
|
|
3352
4228
|
})));
|
|
3353
4229
|
await transcript.chunk({
|
|
3354
4230
|
type: 'sources',
|
|
3355
|
-
sources: {
|
|
4231
|
+
sources: {
|
|
4232
|
+
sources: chunkSources(records),
|
|
4233
|
+
notice: notice ?? undefined,
|
|
4234
|
+
...notReady(knowledge.search)
|
|
4235
|
+
}
|
|
3356
4236
|
});
|
|
3357
4237
|
injected.sourcesWritten = true;
|
|
3358
4238
|
if (records.length > 0) {
|
|
3359
|
-
|
|
4239
|
+
const framing = formatRagContext(knowledge.hits, knowledge.search.availableFiles ?? []);
|
|
4240
|
+
const pages = pictured ? `\n${PDF_PAGES_SOURCE_LINE}` : '';
|
|
4241
|
+
knowledge.lastUser.content = `${framing}${pages}\n\nThe user's message:\n${knowledge.lastUser.content}\n\n${RAG_CITATION_LINE}`;
|
|
3360
4242
|
injected.ragInjected = true;
|
|
3361
4243
|
injected.documentsInjected = true;
|
|
3362
4244
|
}
|
|
@@ -3369,21 +4251,32 @@ function toolCallChunk(event) {
|
|
|
3369
4251
|
return {
|
|
3370
4252
|
type: 'tool-call',
|
|
3371
4253
|
toolCall: { name: event.name, arguments: event.args },
|
|
3372
|
-
round: typeof event.round === 'number' ? event.round : undefined,
|
|
3373
|
-
callId: typeof event.callId === 'string' ? event.callId : undefined,
|
|
3374
4254
|
effect: event.effect === 'read' ? 'read' : 'write',
|
|
3375
4255
|
display: event.display,
|
|
3376
|
-
rawCall: typeof event.rawCall === 'string' ? event.rawCall : undefined
|
|
4256
|
+
rawCall: typeof event.rawCall === 'string' ? event.rawCall : undefined,
|
|
4257
|
+
...callOf(event)
|
|
3377
4258
|
};
|
|
3378
4259
|
}
|
|
3379
4260
|
function toolResultChunk(event) {
|
|
3380
4261
|
return {
|
|
3381
4262
|
type: 'tool-result',
|
|
3382
|
-
toolResult: { name: event.name, result: withoutDataChannel(event.result) },
|
|
3383
|
-
|
|
4263
|
+
toolResult: { name: event.name, result: withoutDataChannel(event.result) ?? null },
|
|
4264
|
+
...callOf(event)
|
|
4265
|
+
};
|
|
4266
|
+
}
|
|
4267
|
+
function roundOf(event) {
|
|
4268
|
+
return typeof event.round === 'number' ? event.round : undefined;
|
|
4269
|
+
}
|
|
4270
|
+
function callOf(event) {
|
|
4271
|
+
return {
|
|
4272
|
+
round: roundOf(event),
|
|
3384
4273
|
callId: typeof event.callId === 'string' ? event.callId : undefined
|
|
3385
4274
|
};
|
|
3386
4275
|
}
|
|
4276
|
+
function replacedTypes(event) {
|
|
4277
|
+
const types = Array.isArray(event.replacedTypes) ? event.replacedTypes : ['content', 'thinking'];
|
|
4278
|
+
return types.filter((type) => type === 'content' || type === 'thinking');
|
|
4279
|
+
}
|
|
3387
4280
|
function runMetricsSummary(metrics) {
|
|
3388
4281
|
const { timeToFirstToken, promptTokens, promptTokensPerSecond, cacheTokens, tokensPerSecond, totalTokens, durationMs, stopReason } = metrics;
|
|
3389
4282
|
return {
|
|
@@ -3397,88 +4290,12 @@ function runMetricsSummary(metrics) {
|
|
|
3397
4290
|
stopReason
|
|
3398
4291
|
};
|
|
3399
4292
|
}
|
|
4293
|
+
function withCacheRebuild(metrics, reason, tokens) {
|
|
4294
|
+
return reason === null ? metrics : { ...metrics, cacheRebuild: { reason, tokens } };
|
|
4295
|
+
}
|
|
3400
4296
|
function asRecord(value) {
|
|
3401
4297
|
return isRecord(value) ? { ...value } : {};
|
|
3402
4298
|
}
|
|
3403
|
-
function sameSerializedValue(left, right) {
|
|
3404
|
-
return (JSON.stringify(normalizeSerializedValue(left)) ===
|
|
3405
|
-
JSON.stringify(normalizeSerializedValue(right)));
|
|
3406
|
-
}
|
|
3407
|
-
function isReplacedType(type) {
|
|
3408
|
-
return type === 'content' || type === 'thinking';
|
|
3409
|
-
}
|
|
3410
|
-
function resumeSetupFailureNotice(event) {
|
|
3411
|
-
if (event.name === 'RunIncompatible' &&
|
|
3412
|
-
(event.kind === 'machine' || event.kind === 'log' || event.kind === 'xstate') &&
|
|
3413
|
-
typeof event.found === 'string' &&
|
|
3414
|
-
event.found.length <= 40 &&
|
|
3415
|
-
/^\d{1,10}(?:\.\d{1,10}){0,3}(?:-[A-Za-z0-9.-]{1,16})?$/.test(event.found) &&
|
|
3416
|
-
typeof event.expected === 'string' &&
|
|
3417
|
-
event.expected.length <= 40 &&
|
|
3418
|
-
/^\d{1,10}(?:\.\d{1,10}){0,3}(?:-[A-Za-z0-9.-]{1,16})?$/.test(event.expected)) {
|
|
3419
|
-
return `The saved ${event.kind} version ${event.found} is incompatible; this worker expects ${event.expected}.`;
|
|
3420
|
-
}
|
|
3421
|
-
if (event.code === 'PENDING_ARGS_REDACTED') {
|
|
3422
|
-
return 'Pending tool arguments were redacted, so this run needs an explicit recovery decision.';
|
|
3423
|
-
}
|
|
3424
|
-
if (event.name === 'RunIncompatible') {
|
|
3425
|
-
return 'This worker cannot resume the saved run because its run-log version is incompatible.';
|
|
3426
|
-
}
|
|
3427
|
-
if (event.message === 'Resume requires the original durable request digest key') {
|
|
3428
|
-
return 'The saved request digest key is unavailable, so this run needs recovery.';
|
|
3429
|
-
}
|
|
3430
|
-
if (event.message === 'Resume requires the original tool configuration') {
|
|
3431
|
-
return 'The saved tool configuration no longer matches. Restore it to resume the run.';
|
|
3432
|
-
}
|
|
3433
|
-
if (event.message ===
|
|
3434
|
-
'Resume cannot replace the original model, conversation, or execution configuration') {
|
|
3435
|
-
return 'The original model, conversation, or execution configuration changed; restore it to resume.';
|
|
3436
|
-
}
|
|
3437
|
-
return 'The saved run identity or current execution policy no longer matches. Restore the original model, agent, and tool configuration to resume.';
|
|
3438
|
-
}
|
|
3439
|
-
function savedMessagesOf(saved) {
|
|
3440
|
-
const messages = saved?.messages;
|
|
3441
|
-
return Array.isArray(messages) ? messages : [];
|
|
3442
|
-
}
|
|
3443
|
-
function stripUnseenImages(messages) {
|
|
3444
|
-
for (const turn of messages) {
|
|
3445
|
-
if (turn.images?.length) {
|
|
3446
|
-
const names = turn.images.map((image) => image.fileName).join(', ');
|
|
3447
|
-
turn.content += `\n[Attached image${turn.images.length > 1 ? 's' : ''}: ${names} — not visible to this model]`;
|
|
3448
|
-
}
|
|
3449
|
-
delete turn.images;
|
|
3450
|
-
delete turn.pdfs;
|
|
3451
|
-
}
|
|
3452
|
-
}
|
|
3453
|
-
function callIdentity(call) {
|
|
3454
|
-
const { callId, round } = asRecord(call);
|
|
3455
|
-
return {
|
|
3456
|
-
callId: typeof callId === 'string' ? callId : undefined,
|
|
3457
|
-
round: typeof round === 'number' ? round : undefined
|
|
3458
|
-
};
|
|
3459
|
-
}
|
|
3460
|
-
function normalizeSerializedValue(value) {
|
|
3461
|
-
if (value instanceof Uint8Array) {
|
|
3462
|
-
return { type: 'Buffer', data: Array.from(value) };
|
|
3463
|
-
}
|
|
3464
|
-
if (Array.isArray(value)) {
|
|
3465
|
-
return value.map(normalizeSerializedValue);
|
|
3466
|
-
}
|
|
3467
|
-
if (!isRecord(value)) {
|
|
3468
|
-
return value;
|
|
3469
|
-
}
|
|
3470
|
-
const record = value;
|
|
3471
|
-
if (record.type === 'Buffer' && Array.isArray(record.data)) {
|
|
3472
|
-
return { type: 'Buffer', data: record.data.map(normalizeSerializedValue) };
|
|
3473
|
-
}
|
|
3474
|
-
const normalized = {};
|
|
3475
|
-
for (const key of Object.keys(record).sort()) {
|
|
3476
|
-
if (record[key] !== undefined) {
|
|
3477
|
-
normalized[key] = normalizeSerializedValue(record[key]);
|
|
3478
|
-
}
|
|
3479
|
-
}
|
|
3480
|
-
return normalized;
|
|
3481
|
-
}
|
|
3482
4299
|
function isStreamDestroyed(err) {
|
|
3483
4300
|
return isRecord(err) && err.code === 'STREAM_DESTROYED';
|
|
3484
4301
|
}
|