@qvac/core 1.0.0-rc.1 → 1.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +852 -1
- package/README.md +40 -81
- package/dist-lib/index.d.ts +0 -1
- package/dist-lib/index.js +0 -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 +7 -0
- package/dist-lib/lib/api/chunk-delta.js +33 -0
- package/dist-lib/lib/api/chunks.d.ts +17 -2
- package/dist-lib/lib/api/chunks.js +64 -13
- package/dist-lib/lib/api/config.d.ts +2 -1
- package/dist-lib/lib/api/config.js +3 -2
- 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 +10 -8
- 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 +20 -10
- 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/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 +12 -44
- 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 +64 -17
- package/dist-lib/lib/api/mesh.d.ts +12 -2
- package/dist-lib/lib/api/mesh.js +10 -6
- 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 +6 -5
- package/dist-lib/lib/api/projects.d.ts +20 -0
- package/dist-lib/lib/api/projects.js +165 -0
- package/dist-lib/lib/api/rag.d.ts +0 -1
- package/dist-lib/lib/api/rag.js +47 -23
- package/dist-lib/lib/api/served.d.ts +17 -0
- package/dist-lib/lib/api/served.js +47 -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 +48 -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 +29 -29
- package/dist-lib/lib/assistant/harness.js +34 -6
- package/dist-lib/lib/assistant/history.d.ts +30 -17
- package/dist-lib/lib/assistant/history.js +168 -82
- 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 +21 -0
- package/dist-lib/lib/assistant/message.js +43 -0
- package/dist-lib/lib/assistant/model-round.d.ts +95 -0
- package/dist-lib/lib/assistant/model-round.js +591 -0
- package/dist-lib/lib/assistant/pdf-budget.d.ts +3 -0
- package/dist-lib/lib/assistant/pdf-budget.js +13 -0
- package/dist-lib/lib/assistant/round-host.d.ts +42 -0
- package/dist-lib/lib/assistant/round-host.js +239 -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 +2 -0
- package/dist-lib/lib/assistant-client.js +4 -3
- package/dist-lib/lib/assistant-tree.d.ts +8 -3
- package/dist-lib/lib/assistant-tree.js +58 -18
- package/dist-lib/lib/assistant.d.ts +76 -20
- package/dist-lib/lib/assistant.js +1659 -1070
- 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/constants.d.ts +7 -0
- package/dist-lib/lib/constants.js +7 -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 +36 -22
- 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 +245 -0
- package/dist-lib/lib/core/chat-group.js +8 -7
- package/dist-lib/lib/core/chat-move.d.ts +71 -0
- package/dist-lib/lib/core/chat-move.js +567 -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 +34 -4
- package/dist-lib/lib/core/chat.js +122 -39
- package/dist-lib/lib/core/chunk.d.ts +21 -4
- package/dist-lib/lib/core/chunk.js +138 -23
- package/dist-lib/lib/core/collections.d.ts +28 -0
- package/dist-lib/lib/core/collections.js +28 -0
- package/dist-lib/lib/core/contract/agent-join.d.ts +10 -0
- package/dist-lib/lib/core/contract/agent-join.js +108 -0
- package/dist-lib/lib/core/contract/agents.d.ts +5 -0
- package/dist-lib/lib/core/contract/agents.js +141 -3
- package/dist-lib/lib/core/contract/chat.d.ts +1 -0
- package/dist-lib/lib/core/contract/chat.js +42 -3
- package/dist-lib/lib/core/contract/chunks.d.ts +7 -0
- package/dist-lib/lib/core/contract/chunks.js +112 -35
- package/dist-lib/lib/core/contract/index.d.ts +2 -3
- package/dist-lib/lib/core/contract/index.js +35 -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 +18 -9
- package/dist-lib/lib/core/contract/joined-meshes.js +2 -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 +31 -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 +279 -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 +29 -0
- package/dist-lib/lib/core/contract/project-host-keys.d.ts +7 -0
- package/dist-lib/lib/core/contract/project-host-keys.js +40 -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 +328 -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 +120 -0
- package/dist-lib/lib/core/contract/run-requests.d.ts +10 -0
- package/dist-lib/lib/core/contract/run-requests.js +75 -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/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 +317 -0
- package/dist-lib/lib/core/contract/writers.js +82 -12
- 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.js +1 -1
- package/dist-lib/lib/core/document-attachment.d.ts +7 -0
- package/dist-lib/lib/core/document-attachment.js +9 -0
- package/dist-lib/lib/core/files/attachments.js +5 -4
- package/dist-lib/lib/core/files/index.d.ts +21 -11
- package/dist-lib/lib/core/files/index.js +114 -28
- 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 +7 -2
- package/dist-lib/lib/core/files/processing/index.js +91 -71
- 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 +3 -0
- package/dist-lib/lib/core/files/processing/source.js +9 -4
- package/dist-lib/lib/core/files/processing/state.d.ts +4 -1
- package/dist-lib/lib/core/files/processing/state.js +2 -1
- package/dist-lib/lib/core/files/rag/build.d.ts +4 -1
- package/dist-lib/lib/core/files/rag/build.js +18 -7
- 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 +12 -1
- package/dist-lib/lib/core/files/rag/index.js +29 -7
- 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 +30 -18
- package/dist-lib/lib/core/files/rag/retrieval/full-text.js +1 -1
- 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/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 +4 -0
- package/dist-lib/lib/core/invite.js +19 -6
- 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 +76 -6
- package/dist-lib/lib/core/local/index.js +209 -19
- 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 +94 -28
- package/dist-lib/lib/core/mesh.js +527 -144
- 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/file-index.js +2 -4
- package/dist-lib/lib/core/migrations/index.js +6 -2
- 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 +4 -2
- package/dist-lib/lib/core/pdf/rasterize.d.ts +2 -2
- package/dist-lib/lib/core/pdf/rasterize.js +5 -6
- package/dist-lib/lib/core/peer-duplex.d.ts +34 -0
- package/dist-lib/lib/core/{harness-loan.js → peer-duplex.js} +47 -40
- 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 +88 -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 +59 -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 +18 -0
- package/dist-lib/lib/core/senders.js +52 -0
- package/dist-lib/lib/core/shared-agents.d.ts +39 -0
- package/dist-lib/lib/core/shared-agents.js +255 -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 +47 -0
- package/dist-lib/lib/core/sync/background.js +162 -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 +1 -1
- 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 +154 -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/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 +249 -79
- package/dist-lib/lib/core.js +3369 -1460
- package/dist-lib/lib/credential-pusher.d.ts +6 -0
- package/dist-lib/lib/credential-pusher.js +77 -60
- 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/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 +790 -0
- package/dist-lib/lib/errors.js +724 -0
- package/dist-lib/lib/fs-drive-bridge.d.ts +1 -1
- package/dist-lib/lib/fs-drive-bridge.js +4 -1
- 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/knowledge-search.js +13 -18
- 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/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 +11 -12
- package/dist-lib/lib/model-download.js +63 -22
- 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/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/run-input.d.ts +14 -0
- package/dist-lib/lib/run-input.js +33 -0
- package/dist-lib/lib/run-notices.d.ts +3 -0
- package/dist-lib/lib/run-notices.js +8 -2
- package/dist-lib/lib/run-replay.d.ts +32 -0
- package/dist-lib/lib/run-replay.js +121 -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 +3 -1
- 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 +58 -11
- package/dist-lib/schema/engine.d.ts +1 -0
- package/dist-lib/schema/engine.js +709 -116
- package/dist-lib/schema/field-order.js +2 -1
- package/dist-lib/schema/indexes.d.ts +3 -0
- package/dist-lib/schema/indexes.js +30 -3
- package/dist-lib/schema/local.js +72 -4
- package/dist-lib/schema/qvac.d.ts +4 -1
- package/dist-lib/schema/qvac.js +364 -12
- package/dist-lib/spec/bind.js +209 -48
- package/dist-lib/spec/calls.js +45 -5
- package/dist-lib/spec/capabilities.d.ts +98 -18
- package/dist-lib/spec/config/hyperconf/index.js +1 -1
- package/dist-lib/spec/hrpc/hrpc.json +726 -206
- package/dist-lib/spec/hrpc/index.js +832 -345
- package/dist-lib/spec/hrpc/messages.js +6906 -4832
- package/dist-lib/spec/hyperschema/index.js +6906 -4832
- package/dist-lib/spec/hyperschema/schema.json +4744 -3139
- package/dist-lib/spec/hyperschema/types.d.ts +461 -61
- 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 +1506 -484
- package/dist-lib/spec/local/hyperschema/index.js +542 -13
- package/dist-lib/spec/local/hyperschema/schema.json +319 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +78 -2
- package/dist-lib/spec/mesh/hyperdb/db.json +371 -5
- package/dist-lib/spec/mesh/hyperdb/index.js +1564 -5
- package/dist-lib/spec/mesh/hyperdb/messages.js +6023 -1246
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +187 -1
- package/dist-lib/spec/mesh/hyperdispatch/index.js +528 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +3449 -217
- package/dist-lib/spec/mesh/hyperschema/index.js +3449 -217
- package/dist-lib/spec/mesh/hyperschema/schema.json +1865 -19
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +470 -6
- package/package.json +50 -35
- package/worker/assistant-sidecar.mjs +15 -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/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/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,95 @@ 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
25
|
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';
|
|
26
|
+
import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason, pdfPagesCutNotice, pdfPagesSkippedNotice } 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';
|
|
39
|
+
import { agentRekeyFingerprint, agentSwitched, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
|
|
40
40
|
import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_TURNS, COMPACTION_TURN_PREFIX, isContextOverflowFailure, nearContextLimit, compactionBoundary, lastCompactedThroughId, parseCompactionSummary } from './compaction.js';
|
|
41
41
|
import { ModelDownloadReconciler, musicModelChoiceOf } from './model-download.js';
|
|
42
42
|
import { AbortedError, raceAbort } from '@qvac/harness/race-abort';
|
|
43
43
|
import { RunTranscript } from './run-transcript.js';
|
|
44
|
+
import { OwnerRounds, listRoundChunks } from './assistant/model-round.js';
|
|
45
|
+
import { RoundHost } from './assistant/round-host.js';
|
|
44
46
|
import { searchKnowledge } from './knowledge-search.js';
|
|
45
47
|
import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
|
|
48
|
+
import { unref } from './unref.js';
|
|
46
49
|
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';
|
|
50
|
+
import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, heldLoadModel, droppedHeldLoadModel, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
|
|
51
|
+
import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, askedIn, nameSenders, restoreSentContent, hiddenVideoNotice, unreadVideoNotice, unsyncedVideoNotice, VIDEO_NO_AUDIO, VIDEO_SILENT, videoAnnotation, videoTranscriptBlock } from './assistant/message.js';
|
|
52
|
+
import { frameTimes, probeVideo, sampleFrames } from './core/video/frames.js';
|
|
53
|
+
import { packVideoDigest, parseVideoDigestHeader, unpackVideoDigest, VIDEO_DIGEST_MIME, videoDigestFileName } from './core/video/digest.js';
|
|
54
|
+
import { VIDEO_FRAME_EDGE, videoFrameCount } from './assistant/video-budget.js';
|
|
49
55
|
import { drainBody, raceInactivity } from './assistant/fetch-body.js';
|
|
50
|
-
import {
|
|
56
|
+
import { pdfPageBudget } from './assistant/pdf-budget.js';
|
|
57
|
+
import { effectiveCtxSize } from './api/effective-ctx-size.js';
|
|
58
|
+
import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, foldableView, historyFromChunks, listHistoryChunks, replayFromChunks, runPromptView, runView, senderOf, staleToolRunIds, viewOutgrewCache } from './assistant/history.js';
|
|
51
59
|
import { errorMessage, isRecord } from './guards.js';
|
|
60
|
+
import { CoreError, isCoreError } from './errors.js';
|
|
52
61
|
export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
|
|
53
62
|
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();
|
|
63
|
+
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders, restoreSentContent } from './assistant/message.js';
|
|
64
|
+
export { historyFromChunks, listHistoryChunks, staleToolRunIds } from './assistant/history.js';
|
|
57
65
|
const DEFAULT_PROFILE = 'mid';
|
|
58
66
|
const QvacState = local.getEnum('@local/qvac-state');
|
|
59
67
|
const FINAL_FLUSH_MS = 10_000;
|
|
60
68
|
const UPLOAD_FRAME_BYTES = 64 * 1024;
|
|
69
|
+
const RUN_IDLE_TIMEOUT_MS = 15 * 60_000;
|
|
70
|
+
const MODEL_RELOAD_DELAY_MS = 2_500;
|
|
71
|
+
function idleWatch(controller, ms) {
|
|
72
|
+
let last = 0;
|
|
73
|
+
let parked = 0;
|
|
74
|
+
const timer = setInterval(() => {
|
|
75
|
+
if (last && !parked && Date.now() - last >= ms) {
|
|
76
|
+
controller.abort(RUN_ABORT_REASONS.stalled);
|
|
77
|
+
}
|
|
78
|
+
}, Math.min(ms, 30_000));
|
|
79
|
+
unref(timer);
|
|
80
|
+
return {
|
|
81
|
+
touch: () => {
|
|
82
|
+
last = Date.now();
|
|
83
|
+
},
|
|
84
|
+
parks: (fn) => async (...args) => {
|
|
85
|
+
parked++;
|
|
86
|
+
try {
|
|
87
|
+
return await fn(...args);
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
parked--;
|
|
91
|
+
last = Date.now();
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
stop: () => clearInterval(timer)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
61
97
|
function settledWithin(write, ms) {
|
|
62
98
|
let timer;
|
|
63
99
|
const expired = new Promise((resolve) => {
|
|
@@ -71,7 +107,18 @@ function isDeniedResult(result) {
|
|
|
71
107
|
return false;
|
|
72
108
|
return 'denied' in result && result.denied === true;
|
|
73
109
|
}
|
|
74
|
-
const
|
|
110
|
+
const TITLE_PROBE = {
|
|
111
|
+
kind: 'title',
|
|
112
|
+
name: 'chat_title',
|
|
113
|
+
field: 'title',
|
|
114
|
+
runId: 'chat-title'
|
|
115
|
+
};
|
|
116
|
+
const SUGGESTION_PROBE = {
|
|
117
|
+
kind: 'suggestion',
|
|
118
|
+
name: 'next_message',
|
|
119
|
+
field: 'prompt',
|
|
120
|
+
runId: 'chat-suggestion'
|
|
121
|
+
};
|
|
75
122
|
const RunOccupancySchema = z.object({
|
|
76
123
|
cacheTokens: z.number().optional().catch(undefined),
|
|
77
124
|
ctxSize: z.number().optional().catch(undefined)
|
|
@@ -79,6 +126,23 @@ const RunOccupancySchema = z.object({
|
|
|
79
126
|
export const CAPTURE_INFERENCE_CALLS_KEY = 'inspector.captureInferenceCalls';
|
|
80
127
|
export const CAPTURE_ON = 'on';
|
|
81
128
|
export const CAPTURE_OFF = 'off';
|
|
129
|
+
export const SUGGEST_PROMPTS_KEY = 'assistant.suggestPrompts';
|
|
130
|
+
export const SUGGEST_PROMPTS_ON = 'on';
|
|
131
|
+
const SUGGESTION_SUPERSEDED = "superseded by the chat's next run";
|
|
132
|
+
function pdfCut(doc, from) {
|
|
133
|
+
const pageCount = doc.pageCount();
|
|
134
|
+
for (let page = from; page < pageCount; page++) {
|
|
135
|
+
try {
|
|
136
|
+
const flags = doc.pageFlags(page);
|
|
137
|
+
if (flags.hasImage || !flags.hasText) {
|
|
138
|
+
return { lastPage: page, pageCount };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
82
146
|
function attachPages(turn, files) {
|
|
83
147
|
if (!files.length)
|
|
84
148
|
return;
|
|
@@ -87,6 +151,24 @@ function attachPages(turn, files) {
|
|
|
87
151
|
function noBytes() {
|
|
88
152
|
return Promise.resolve(null);
|
|
89
153
|
}
|
|
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
|
+
}
|
|
90
172
|
function pdfPageCacheName(pdf, page) {
|
|
91
173
|
return imageCacheName({ blobId: pdf.blobId, fileName: `${pdf.fileName}.p${page}.jpg` });
|
|
92
174
|
}
|
|
@@ -113,12 +195,26 @@ const RUN_SETTLED = RUN_ABORT_REASONS.runSettled;
|
|
|
113
195
|
const RUNNER_DIED = RUN_ABORT_REASONS.runnerDied;
|
|
114
196
|
const RUNNER_CLOSED = RUN_ABORT_REASONS.runnerClosed;
|
|
115
197
|
const MODEL_UNLOADED = RUN_ABORT_REASONS.modelUnloaded;
|
|
198
|
+
const RUN_PAUSED = 'paused on a saved checkpoint; it resumes from there';
|
|
199
|
+
function recoveryReason(event) {
|
|
200
|
+
const operation = isRecord(event.operation) ? event.operation.name : undefined;
|
|
201
|
+
const call = typeof operation === 'string' ? ` of ${operation}` : '';
|
|
202
|
+
return `the outcome${call} is unknown, so it is not run again: decide before this run continues`;
|
|
203
|
+
}
|
|
204
|
+
function keepsRunLog(capability) {
|
|
205
|
+
return capability?.hostRunLog === true && capability.hostRunLogVersion === 1;
|
|
206
|
+
}
|
|
116
207
|
function isSetUpHere(status) {
|
|
117
208
|
return status === SKILL_STATE.ready || status === SKILL_STATE.incompatibleModel;
|
|
118
209
|
}
|
|
119
210
|
function sameNameList(left, right) {
|
|
120
211
|
return left.length === right.length && left.every((name, index) => name === right[index]);
|
|
121
212
|
}
|
|
213
|
+
function sameLoadSettings(a, b) {
|
|
214
|
+
return ((a.ctxSize ?? 0) === (b.ctxSize ?? 0) &&
|
|
215
|
+
(a.kvCacheK ?? '') === (b.kvCacheK ?? '') &&
|
|
216
|
+
(a.kvCacheV ?? '') === (b.kvCacheV ?? ''));
|
|
217
|
+
}
|
|
122
218
|
function withConfigDefaults(agent, config) {
|
|
123
219
|
if (agent.ctxSize || !config?.ctxSize)
|
|
124
220
|
return agent;
|
|
@@ -137,8 +233,6 @@ const WEB_RAG_SOURCE_BOUNDS = { ...RAG_SOURCE_BOUNDS, maxUrlChars: MAX_RAG_SOURC
|
|
|
137
233
|
export class QvacAssistant extends ReadyResource {
|
|
138
234
|
_engine;
|
|
139
235
|
_harness;
|
|
140
|
-
_borrowHarness;
|
|
141
|
-
_borrowed = new Map();
|
|
142
236
|
_log;
|
|
143
237
|
_imageCacheDir;
|
|
144
238
|
_imageCacheMaxBytes;
|
|
@@ -149,6 +243,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
149
243
|
_preloadCurrentAgentModel;
|
|
150
244
|
_drives;
|
|
151
245
|
_blobReadTimeoutMs;
|
|
246
|
+
_runIdleTimeoutMs;
|
|
247
|
+
_modelReloadDelayMs;
|
|
248
|
+
_reloads = new Map();
|
|
152
249
|
_pdfRasterizer;
|
|
153
250
|
_warmAbort = new AbortController();
|
|
154
251
|
_unloading = new Map();
|
|
@@ -158,8 +255,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
158
255
|
_deviceId;
|
|
159
256
|
_info;
|
|
160
257
|
_lastCapabilities = new Map();
|
|
161
|
-
|
|
162
|
-
|
|
258
|
+
_ownWorkspace = null;
|
|
259
|
+
_listed = [];
|
|
260
|
+
_accountHosted = [];
|
|
163
261
|
_readySkills;
|
|
164
262
|
_recordingReadiness = false;
|
|
165
263
|
_readinessDirty = false;
|
|
@@ -172,6 +270,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
172
270
|
_harnessDiagnostics;
|
|
173
271
|
_harnessDeath;
|
|
174
272
|
_refreshingDiagnostics;
|
|
273
|
+
_backendUse = new Map();
|
|
175
274
|
_meshList;
|
|
176
275
|
_accountFrames;
|
|
177
276
|
_accountMeshId;
|
|
@@ -179,27 +278,36 @@ export class QvacAssistant extends ReadyResource {
|
|
|
179
278
|
_meshListWatch;
|
|
180
279
|
_accountWatch;
|
|
181
280
|
_meshConsumers;
|
|
281
|
+
_servedChats = new Map();
|
|
182
282
|
_runs;
|
|
283
|
+
_steered = new Map();
|
|
284
|
+
_idleWaiters = new Set();
|
|
183
285
|
_seen;
|
|
286
|
+
_parked;
|
|
287
|
+
_handedOff = new Map();
|
|
288
|
+
_backgroundReplays;
|
|
184
289
|
_inferenceCalls;
|
|
290
|
+
_runReplay;
|
|
291
|
+
_roundHost;
|
|
185
292
|
_lifecycle;
|
|
293
|
+
_suggesting = new Map();
|
|
186
294
|
_audioTranscripts;
|
|
295
|
+
_videoProbes;
|
|
187
296
|
_pdfPages;
|
|
297
|
+
_mediaRefs;
|
|
188
298
|
_chats;
|
|
189
299
|
_runImageCacheNames;
|
|
190
300
|
_reconciledCompactions;
|
|
191
|
-
_chatAttribution;
|
|
192
301
|
_modelCatalog = new ModelCatalogRevisionHub();
|
|
193
302
|
_onHarnessStatus;
|
|
194
303
|
_onModelCatalogEvent;
|
|
195
|
-
constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives,
|
|
304
|
+
constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, imageCacheMaxBytes, imageCacheMaxAgeMs, 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
305
|
super();
|
|
197
306
|
this._engine = engine;
|
|
198
307
|
this._harness = harness;
|
|
199
|
-
this._borrowHarness = borrowHarness ?? null;
|
|
200
308
|
this._drives = drives;
|
|
201
309
|
this._log = logger;
|
|
202
|
-
this._imageCacheDir = imageCacheDir ??
|
|
310
|
+
this._imageCacheDir = imageCacheDir ?? persistentDir('image-cache');
|
|
203
311
|
this._imageCacheMaxBytes = imageCacheMaxBytes;
|
|
204
312
|
this._imageCacheMaxAgeMs = imageCacheMaxAgeMs;
|
|
205
313
|
this._downloadRetryMs = downloadRetryMs;
|
|
@@ -207,7 +315,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
207
315
|
this._mobileClass = isMobilePlatform(platform);
|
|
208
316
|
this._preloadCurrentAgentModel = preloadCurrentAgentModel;
|
|
209
317
|
this._blobReadTimeoutMs = blobReadTimeoutMs;
|
|
210
|
-
this.
|
|
318
|
+
this._runIdleTimeoutMs = runIdleTimeoutMs;
|
|
319
|
+
this._modelReloadDelayMs = modelReloadDelayMs;
|
|
320
|
+
this._pdfRasterizer = pdfRasterizerOption ?? createPdfRasterizer(logger);
|
|
321
|
+
const serve = harness.serveModelRound?.bind(harness);
|
|
322
|
+
this._roundHost = serve
|
|
323
|
+
? new RoundHost({
|
|
324
|
+
engine,
|
|
325
|
+
serve,
|
|
326
|
+
deviceId: () => this._requireDeviceId(),
|
|
327
|
+
media: {
|
|
328
|
+
resolve: (sources, served) => this._servedMedia(sources, served),
|
|
329
|
+
release: (id) => this._runImageCacheNames.delete(`round:${id}`)
|
|
330
|
+
},
|
|
331
|
+
log: logger
|
|
332
|
+
})
|
|
333
|
+
: null;
|
|
211
334
|
this._deviceId = null;
|
|
212
335
|
this._info = { toolNames: [], skills: [], supportsImages: false };
|
|
213
336
|
this._readySkills = null;
|
|
@@ -227,14 +350,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
227
350
|
this._meshConsumers = new Map();
|
|
228
351
|
this._runs = new Map();
|
|
229
352
|
this._seen = new Map();
|
|
230
|
-
this.
|
|
353
|
+
this._parked = new Map();
|
|
354
|
+
this._backgroundReplays = new Set();
|
|
355
|
+
this._inferenceCalls = new InferenceCallStore(inferenceCallsDir ?? persistentDir('inspector'));
|
|
356
|
+
this._runReplay = new RunReplayStore(runReplayDir ?? persistentDir('run-replay'));
|
|
231
357
|
this._lifecycle = new AbortController();
|
|
232
358
|
this._audioTranscripts = new Map();
|
|
359
|
+
this._videoProbes = new Map();
|
|
233
360
|
this._pdfPages = new Map();
|
|
361
|
+
this._mediaRefs = new Map();
|
|
234
362
|
this._chats = new Map();
|
|
235
363
|
this._runImageCacheNames = new Map();
|
|
236
364
|
this._reconciledCompactions = new Map();
|
|
237
|
-
this._chatAttribution = new Map();
|
|
238
365
|
this._onHarnessStatus = (status) => {
|
|
239
366
|
this._log?.warn('assistant', 'harness status', status);
|
|
240
367
|
if (harnessDied(status)) {
|
|
@@ -257,6 +384,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
257
384
|
if (progress) {
|
|
258
385
|
this._downloads?.observeImageProgress(progress);
|
|
259
386
|
}
|
|
387
|
+
const held = heldLoadModel(state);
|
|
388
|
+
if (held) {
|
|
389
|
+
this._downloads?.observeHeldLoad(held);
|
|
390
|
+
}
|
|
391
|
+
const dropped = droppedHeldLoadModel(state);
|
|
392
|
+
if (dropped) {
|
|
393
|
+
this._downloads?.observeDroppedHeldLoad(dropped);
|
|
394
|
+
}
|
|
260
395
|
};
|
|
261
396
|
}
|
|
262
397
|
_requireDeviceId() {
|
|
@@ -274,6 +409,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
274
409
|
this._harness.on?.('status', this._onHarnessStatus);
|
|
275
410
|
this._harness.on?.('model', this._onModelCatalogEvent);
|
|
276
411
|
this._info = await this._describeHarness();
|
|
412
|
+
await this._clearPreReplayCaches();
|
|
277
413
|
await this._clearPersistedPauses();
|
|
278
414
|
this._downloads = new ModelDownloadReconciler({
|
|
279
415
|
engine: this._engine,
|
|
@@ -501,7 +637,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
501
637
|
assistant: summarizeRuns(runs, this.suspended),
|
|
502
638
|
platform: this._platform,
|
|
503
639
|
mobileClass: this._mobileClass,
|
|
504
|
-
memoryBudget: this._capability?.memoryBudget ?? null
|
|
640
|
+
memoryBudget: this._capability?.memoryBudget ?? null,
|
|
641
|
+
backendUse: Object.fromEntries([...this._backendUse].map(([tool, uses]) => [tool, [...uses]]))
|
|
505
642
|
};
|
|
506
643
|
}
|
|
507
644
|
async _refreshHarnessDiagnostics() {
|
|
@@ -549,24 +686,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
549
686
|
if (!meshId) {
|
|
550
687
|
return;
|
|
551
688
|
}
|
|
552
|
-
|
|
553
|
-
const report = {
|
|
689
|
+
await this._reportInto(meshId, key, {
|
|
554
690
|
installedToolNames: this._info.toolNames,
|
|
555
691
|
installedSkillNames: this._info.skills,
|
|
556
692
|
installedModelNames,
|
|
557
693
|
cachedModelNames,
|
|
558
|
-
readySkillNames:
|
|
694
|
+
readySkillNames: this._readySkills ?? undefined,
|
|
559
695
|
toolCatalog: toolCatalog ?? undefined
|
|
560
|
-
};
|
|
561
|
-
await this._reportInto(meshId, key, report);
|
|
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
|
-
}
|
|
696
|
+
});
|
|
570
697
|
}
|
|
571
698
|
async _reportInto(meshId, key, report) {
|
|
572
699
|
if (this._lastCapabilities.get(meshId) === key)
|
|
@@ -576,7 +703,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
576
703
|
this._lastCapabilities.set(meshId, key);
|
|
577
704
|
}
|
|
578
705
|
catch (err) {
|
|
579
|
-
if (
|
|
706
|
+
if (isCoreError(err, 'MESH_NOT_FOUND')) {
|
|
580
707
|
return;
|
|
581
708
|
}
|
|
582
709
|
this._log?.error('assistant', 'saveDeviceCapabilities failed', err);
|
|
@@ -584,22 +711,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
584
711
|
}
|
|
585
712
|
_followHosted(meshId, agents) {
|
|
586
713
|
const deviceId = this._deviceId;
|
|
587
|
-
if (!deviceId ||
|
|
714
|
+
if (!deviceId || meshId !== this._accountMeshId) {
|
|
588
715
|
return;
|
|
589
716
|
}
|
|
590
717
|
const hosted = agents
|
|
591
718
|
.filter((agent) => !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId))
|
|
592
719
|
.map((agent) => agent.id)
|
|
593
720
|
.sort();
|
|
594
|
-
if (sameNameList(this.
|
|
721
|
+
if (sameNameList(this._accountHosted, hosted)) {
|
|
595
722
|
return;
|
|
596
723
|
}
|
|
597
|
-
|
|
598
|
-
this._hostedIn.set(meshId, hosted);
|
|
599
|
-
}
|
|
600
|
-
else {
|
|
601
|
-
this._hostedIn.delete(meshId);
|
|
602
|
-
}
|
|
724
|
+
this._accountHosted = hosted;
|
|
603
725
|
void this._reportHosted().catch(noop);
|
|
604
726
|
}
|
|
605
727
|
async _reportHosted() {
|
|
@@ -678,7 +800,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
678
800
|
this._harness.off?.('status', this._onHarnessStatus);
|
|
679
801
|
this._harness.off?.('model', this._onModelCatalogEvent);
|
|
680
802
|
this._warmAbort.abort(RUNNER_CLOSED);
|
|
681
|
-
this.
|
|
803
|
+
this._cancelModelReloads();
|
|
804
|
+
this._onlineBoot.destroy(new CoreError('ASSISTANT_CLOSING'));
|
|
682
805
|
for (const run of this._runs.values()) {
|
|
683
806
|
run.controller.abort(RUNNER_CLOSED);
|
|
684
807
|
}
|
|
@@ -687,11 +810,20 @@ export class QvacAssistant extends ReadyResource {
|
|
|
687
810
|
this._meshList?.destroy();
|
|
688
811
|
this._accountFrames?.destroy();
|
|
689
812
|
this._lifecycle.abort(RUNNER_CLOSED);
|
|
813
|
+
for (const pending of this._suggesting.values()) {
|
|
814
|
+
pending.abort(RUNNER_CLOSED);
|
|
815
|
+
}
|
|
690
816
|
await Promise.allSettled([...this._runs.values()].map((run) => run.done));
|
|
691
817
|
}
|
|
818
|
+
get mobileClass() {
|
|
819
|
+
return this._mobileClass;
|
|
820
|
+
}
|
|
692
821
|
get suspended() {
|
|
693
822
|
return this._state === QvacState.SUSPEND;
|
|
694
823
|
}
|
|
824
|
+
get _backgrounded() {
|
|
825
|
+
return this._state === QvacState.PRESUSPEND || this._state === QvacState.SUSPEND;
|
|
826
|
+
}
|
|
695
827
|
async presuspend() {
|
|
696
828
|
if (this.closing)
|
|
697
829
|
return;
|
|
@@ -708,7 +840,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
708
840
|
}
|
|
709
841
|
this._state = QvacState.SUSPEND;
|
|
710
842
|
this._log?.info('assistant', 'suspending', { runs: this._runs.size });
|
|
843
|
+
this._parked.clear();
|
|
711
844
|
this._stopMeshIntake();
|
|
845
|
+
this._cancelModelReloads();
|
|
712
846
|
await this._downloads?.suspend();
|
|
713
847
|
this._meshList?.destroy();
|
|
714
848
|
this._accountFrames?.destroy();
|
|
@@ -737,6 +871,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
737
871
|
}
|
|
738
872
|
if (this._state === QvacState.PRESUSPEND) {
|
|
739
873
|
this._state = QvacState.RESUME;
|
|
874
|
+
this._parked.forEach((replay) => replay());
|
|
875
|
+
this._parked.clear();
|
|
876
|
+
this._takeBackHandedOff();
|
|
740
877
|
return;
|
|
741
878
|
}
|
|
742
879
|
this._state = QvacState.RESUME;
|
|
@@ -771,10 +908,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
771
908
|
}
|
|
772
909
|
async loadModel(name) {
|
|
773
910
|
if (!this._harness.loadModel) {
|
|
774
|
-
throw new
|
|
911
|
+
throw new CoreError('MODEL_RUNTIME_UNSUPPORTED', ['preload']);
|
|
775
912
|
}
|
|
776
913
|
if (!(await this._isCached(name))) {
|
|
777
|
-
throw new
|
|
914
|
+
throw new CoreError('MODEL_NOT_DOWNLOADED', [name]);
|
|
778
915
|
}
|
|
779
916
|
const config = await this._engine.getConfig({}).catch(noop);
|
|
780
917
|
const agent = await this._agentRunning(name, config?.activeAgentId ?? null);
|
|
@@ -789,6 +926,44 @@ export class QvacAssistant extends ReadyResource {
|
|
|
789
926
|
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
790
927
|
return rows?.some((row) => row.name === model && row.isCached) === true;
|
|
791
928
|
}
|
|
929
|
+
_scheduleModelReload(agent) {
|
|
930
|
+
const deviceId = this._deviceId;
|
|
931
|
+
if (!this._harness.loadModel ||
|
|
932
|
+
!deviceId ||
|
|
933
|
+
!agent.modelName ||
|
|
934
|
+
!agent.providerDeviceId ||
|
|
935
|
+
!b4a.equals(agent.providerDeviceId, deviceId)) {
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
const name = agent.modelName;
|
|
939
|
+
clearTimeout(this._reloads.get(name));
|
|
940
|
+
const timer = setTimeout(() => {
|
|
941
|
+
this._reloads.delete(name);
|
|
942
|
+
this._reloadLoadedModel(agent).catch((err) => this._log?.warn('models', 'model reload failed', { model: name, err }));
|
|
943
|
+
}, this._modelReloadDelayMs);
|
|
944
|
+
unref(timer);
|
|
945
|
+
this._reloads.set(name, timer);
|
|
946
|
+
}
|
|
947
|
+
async _reloadLoadedModel(agent) {
|
|
948
|
+
if (this.closing || this.suspended || !this._harness.loadModel)
|
|
949
|
+
return;
|
|
950
|
+
const model = runnableModelName(agent.modelName, await this._runnableHost());
|
|
951
|
+
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
952
|
+
const busy = [...this._runs.values()].some((run) => run.executing && run.model === model);
|
|
953
|
+
if (busy || !rows?.some((row) => row.name === model && row.isLoaded)) {
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
const config = await this._engine.getConfig({}).catch(noop);
|
|
957
|
+
const knobs = withConfigDefaults(agent, config);
|
|
958
|
+
const { loaded, reason } = await this._harness.loadModel({ model, agent: knobs });
|
|
959
|
+
this._log?.info('models', 'model reload requested', { model, loaded, reason });
|
|
960
|
+
}
|
|
961
|
+
_cancelModelReloads() {
|
|
962
|
+
for (const timer of this._reloads.values()) {
|
|
963
|
+
clearTimeout(timer);
|
|
964
|
+
}
|
|
965
|
+
this._reloads.clear();
|
|
966
|
+
}
|
|
792
967
|
async _agentRunning(model, activeAgentId) {
|
|
793
968
|
const deviceId = this._requireDeviceId();
|
|
794
969
|
const meshId = this._accountMeshId;
|
|
@@ -817,9 +992,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
817
992
|
}
|
|
818
993
|
async _unloadModel(name) {
|
|
819
994
|
if (!this._harness.unloadModel) {
|
|
820
|
-
throw new
|
|
995
|
+
throw new CoreError('MODEL_RUNTIME_UNSUPPORTED', ['unload']);
|
|
821
996
|
}
|
|
822
|
-
const holders = [...this._runs.values()].filter((run) => run.executing &&
|
|
997
|
+
const holders = [...this._runs.values()].filter((run) => run.executing && run.model === name);
|
|
823
998
|
for (const run of holders) {
|
|
824
999
|
run.controller.abort(MODEL_UNLOADED);
|
|
825
1000
|
}
|
|
@@ -837,15 +1012,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
837
1012
|
const host = await this._runnableHost();
|
|
838
1013
|
const { meshes } = await this._engine.listMeshes({});
|
|
839
1014
|
const scanned = await Promise.all((meshes ?? [])
|
|
840
|
-
.filter((mesh) => mesh.open && !mesh.
|
|
1015
|
+
.filter((mesh) => mesh.open && !mesh.workspace)
|
|
841
1016
|
.map((mesh) => this._meshModelUsers(name, { meshId: mesh.meshId, providerDeviceId, host })));
|
|
842
1017
|
return { agents: scanned.flat() };
|
|
843
1018
|
}
|
|
844
1019
|
async _meshModelUsers(name, { meshId, providerDeviceId, host }) {
|
|
845
1020
|
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) => {
|
|
1021
|
+
const perAgent = await Promise.all((agents ?? []).map(async (agent) => {
|
|
849
1022
|
let models = modelsForAgent(agent, host);
|
|
850
1023
|
if (models.some((model) => model.use === 'music')) {
|
|
851
1024
|
const configs = await this._engine
|
|
@@ -872,9 +1045,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
872
1045
|
notifyModelCatalog() {
|
|
873
1046
|
this._modelCatalog.notify();
|
|
874
1047
|
}
|
|
1048
|
+
cachedModelsChanged() {
|
|
1049
|
+
this._modelCatalog.notify();
|
|
1050
|
+
this._reportCapabilities().catch(noop);
|
|
1051
|
+
}
|
|
875
1052
|
async retryModelDownload(name) {
|
|
876
1053
|
if (!this._downloads) {
|
|
877
|
-
throw new
|
|
1054
|
+
throw new CoreError('MODEL_DOWNLOAD_UNAVAILABLE');
|
|
878
1055
|
}
|
|
879
1056
|
await this._downloads.retry(name);
|
|
880
1057
|
}
|
|
@@ -924,14 +1101,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
924
1101
|
const meshId = this._accountMeshId;
|
|
925
1102
|
if (deviceId && meshId) {
|
|
926
1103
|
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);
|
|
1104
|
+
for (const agent of agents ?? []) {
|
|
935
1105
|
const resolved = (await this._harness.skills?.(agent.id, agent.modelName, {
|
|
936
1106
|
skipCredentialProbe: true
|
|
937
1107
|
})) ?? [];
|
|
@@ -946,9 +1116,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
946
1116
|
_supervisedWatch(label, run) {
|
|
947
1117
|
let running = false;
|
|
948
1118
|
let again = false;
|
|
1119
|
+
let stopped = false;
|
|
949
1120
|
const settle = (err) => {
|
|
950
1121
|
running = false;
|
|
951
|
-
if (err !== null && !
|
|
1122
|
+
if (err !== null && !stopped && !isCoreError(err, 'MESH_NOT_FOUND')) {
|
|
952
1123
|
this._log?.error('assistant', `${label} watch failed`, err);
|
|
953
1124
|
}
|
|
954
1125
|
if (again) {
|
|
@@ -965,12 +1136,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
965
1136
|
return;
|
|
966
1137
|
}
|
|
967
1138
|
running = true;
|
|
1139
|
+
stopped = false;
|
|
968
1140
|
run().then(() => settle(null), (err) => settle(err));
|
|
969
1141
|
};
|
|
970
|
-
|
|
1142
|
+
const stop = () => {
|
|
1143
|
+
again = false;
|
|
1144
|
+
stopped = true;
|
|
1145
|
+
};
|
|
1146
|
+
return { start, stop };
|
|
971
1147
|
}
|
|
972
|
-
async _reclaimDeletedChats(frames) {
|
|
1148
|
+
async _reclaimDeletedChats(frames, servedIn) {
|
|
973
1149
|
let known = null;
|
|
1150
|
+
let agents = new Map();
|
|
974
1151
|
for await (const frame of frames) {
|
|
975
1152
|
if (this.closing || this.suspended)
|
|
976
1153
|
return;
|
|
@@ -981,52 +1158,78 @@ export class QvacAssistant extends ReadyResource {
|
|
|
981
1158
|
this._dropDeletedChat(chatId);
|
|
982
1159
|
}
|
|
983
1160
|
}
|
|
1161
|
+
for (const chat of frame.chats ?? []) {
|
|
1162
|
+
const before = agents.get(chat.id);
|
|
1163
|
+
if (before && before !== chat.agentId) {
|
|
1164
|
+
void this._dropServedKvCache(chat.id, before);
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
984
1167
|
known = new Set(ids);
|
|
1168
|
+
agents = new Map((frame.chats ?? []).map((chat) => [chat.id, chat.agentId]));
|
|
1169
|
+
if (servedIn) {
|
|
1170
|
+
this._servedChats.set(servedIn, known);
|
|
1171
|
+
}
|
|
985
1172
|
await this._sweepImageCache(gone);
|
|
986
1173
|
await this._sweepInferenceCalls();
|
|
987
1174
|
}
|
|
988
1175
|
}
|
|
1176
|
+
async _dropServedKvCache(chatId, agentId) {
|
|
1177
|
+
for (const key of this._roundHost?.left(chatId, agentId) ?? []) {
|
|
1178
|
+
try {
|
|
1179
|
+
await this._harness.deleteKvCache?.(key);
|
|
1180
|
+
}
|
|
1181
|
+
catch (err) {
|
|
1182
|
+
this._log?.warn('assistant', 'served kv-cache deletion failed', { chatId, err });
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
989
1186
|
_dropDeletedChat(chatId) {
|
|
990
1187
|
for (const run of this._runs.values()) {
|
|
991
1188
|
if (run.chatId === chatId) {
|
|
992
|
-
|
|
1189
|
+
run.controller.abort(RUN_SETTLED);
|
|
993
1190
|
}
|
|
994
1191
|
}
|
|
995
1192
|
this._reconciledCompactions.delete(chatId);
|
|
996
|
-
this.
|
|
997
|
-
|
|
1193
|
+
void this._deleteChatKvCache(chatId, 'chat deleted').finally(() => this._runReplay
|
|
1194
|
+
.forget(chatId)
|
|
1195
|
+
.catch((err) => this._log?.warn('assistant', 'run replay not dropped', { err })));
|
|
998
1196
|
void this._inferenceCalls
|
|
999
1197
|
.forget(chatId)
|
|
1000
1198
|
.catch((err) => this._log?.warn('assistant', 'inspector records not dropped', { err }));
|
|
1001
1199
|
}
|
|
1002
|
-
async
|
|
1200
|
+
async _followAgentEdits(meshId, frames) {
|
|
1003
1201
|
let known = null;
|
|
1004
1202
|
for await (const frame of frames) {
|
|
1005
1203
|
if (this.closing || this.suspended)
|
|
1006
1204
|
return;
|
|
1007
1205
|
const agents = frame.agents ?? [];
|
|
1008
1206
|
this._followHosted(meshId, agents);
|
|
1009
|
-
const next = new Map(agents.map((agent) => [agent.id, agentRekeyFingerprint(agent)]));
|
|
1010
1207
|
for (const agent of agents) {
|
|
1011
1208
|
const previous = known?.get(agent.id);
|
|
1012
|
-
if (previous
|
|
1013
|
-
|
|
1209
|
+
if (!previous) {
|
|
1210
|
+
continue;
|
|
1211
|
+
}
|
|
1212
|
+
if (agentRekeyFingerprint(previous) !== agentRekeyFingerprint(agent)) {
|
|
1213
|
+
void this._reclaimAgentChatCaches(meshId, agent);
|
|
1214
|
+
}
|
|
1215
|
+
if (!sameLoadSettings(previous, agent)) {
|
|
1216
|
+
this._scheduleModelReload(agent);
|
|
1014
1217
|
}
|
|
1015
1218
|
}
|
|
1016
|
-
known =
|
|
1219
|
+
known = new Map(agents.map((agent) => [agent.id, agent]));
|
|
1017
1220
|
}
|
|
1018
1221
|
}
|
|
1019
|
-
async _reclaimAgentChatCaches(meshId,
|
|
1222
|
+
async _reclaimAgentChatCaches(meshId, agent) {
|
|
1020
1223
|
try {
|
|
1021
1224
|
const { chats } = await this._engine.listChats({ meshId });
|
|
1022
1225
|
for (const chat of chats ?? []) {
|
|
1023
|
-
if (chat.agentId ===
|
|
1226
|
+
if (chat.agentId === agent.id) {
|
|
1024
1227
|
await this._deleteChatKvCache(chat.id, 'agent re-key');
|
|
1025
1228
|
}
|
|
1026
1229
|
}
|
|
1027
1230
|
}
|
|
1028
1231
|
catch (err) {
|
|
1029
|
-
this._log?.warn('assistant', 'agent re-key cache reclaim failed', { agentId, err });
|
|
1232
|
+
this._log?.warn('assistant', 'agent re-key cache reclaim failed', { agentId: agent.id, err });
|
|
1030
1233
|
}
|
|
1031
1234
|
}
|
|
1032
1235
|
async _reconcileCompaction(engine, chatId) {
|
|
@@ -1051,37 +1254,80 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1051
1254
|
this._reconciledCompactions.set(chatId, newest.id);
|
|
1052
1255
|
}
|
|
1053
1256
|
}
|
|
1054
|
-
async
|
|
1055
|
-
|
|
1056
|
-
|
|
1257
|
+
async deleteChatKvCache(chatId) {
|
|
1258
|
+
const result = (await this._harness.deleteKvCache?.(chatKvCacheKey(chatId))) ?? {
|
|
1259
|
+
status: 'unsupported',
|
|
1260
|
+
error: 'Harness does not support KV cache deletion'
|
|
1261
|
+
};
|
|
1262
|
+
if (result.status === 'success') {
|
|
1263
|
+
await this._runReplay.forget(chatId, LOCAL_HOLDER);
|
|
1057
1264
|
}
|
|
1058
|
-
|
|
1059
|
-
const [{ members }, { devices }] = await Promise.all([
|
|
1060
|
-
engine.listMembers({}).catch(() => ({ members: [] })),
|
|
1061
|
-
stamped ? { devices: [] } : engine.listDevices({}).catch(() => ({ devices: [] }))
|
|
1062
|
-
]);
|
|
1063
|
-
return senderNamesFrom(chunks, members ?? [], devices ?? []);
|
|
1265
|
+
return result;
|
|
1064
1266
|
}
|
|
1065
|
-
async
|
|
1066
|
-
const
|
|
1067
|
-
|
|
1068
|
-
|
|
1267
|
+
async clearKvCache() {
|
|
1268
|
+
const result = await harnessClearKvCache(this._harness)();
|
|
1269
|
+
if (result.status === 'success') {
|
|
1270
|
+
await this._runReplay.clear(LOCAL_HOLDER);
|
|
1271
|
+
}
|
|
1272
|
+
return result;
|
|
1273
|
+
}
|
|
1274
|
+
async _clearPreReplayCaches() {
|
|
1275
|
+
if (await this._runReplay.started())
|
|
1276
|
+
return;
|
|
1277
|
+
const result = await this.clearKvCache();
|
|
1278
|
+
if (result.status === 'failure') {
|
|
1279
|
+
this._log?.warn('assistant', 'pre-replay kv caches not cleared', { error: result.error });
|
|
1069
1280
|
return;
|
|
1070
1281
|
}
|
|
1071
|
-
|
|
1072
|
-
|
|
1282
|
+
await this._runReplay.start();
|
|
1283
|
+
}
|
|
1284
|
+
_inspectCall(inspecting, chatId, entry) {
|
|
1285
|
+
if (!inspecting)
|
|
1073
1286
|
return;
|
|
1287
|
+
void this._inferenceCalls
|
|
1288
|
+
.record(chatId, entry)
|
|
1289
|
+
.catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
|
|
1290
|
+
}
|
|
1291
|
+
_inspectSent(inspecting, chatId, sent) {
|
|
1292
|
+
if (!inspecting)
|
|
1293
|
+
return;
|
|
1294
|
+
void this._inferenceCalls
|
|
1295
|
+
.recordSent(chatId, sent)
|
|
1296
|
+
.catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
|
|
1297
|
+
}
|
|
1298
|
+
async _sentRuns(chatId, rounds) {
|
|
1299
|
+
const served = replayFromChunks(rounds);
|
|
1300
|
+
try {
|
|
1301
|
+
const sentRuns = await this._runReplay.load({ holder: LOCAL_HOLDER, chatId });
|
|
1302
|
+
for (const [runId, turns] of served.sentRuns) {
|
|
1303
|
+
sentRuns.set(runId, turns);
|
|
1304
|
+
}
|
|
1305
|
+
return {
|
|
1306
|
+
sentRuns,
|
|
1307
|
+
askedRuns: await this._runReplay.loadAsked({ holder: LOCAL_HOLDER, chatId }),
|
|
1308
|
+
readRuns: served.readRuns
|
|
1309
|
+
};
|
|
1074
1310
|
}
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
this.
|
|
1311
|
+
catch (err) {
|
|
1312
|
+
this._log?.warn('assistant', 'run replay unreadable', { chatId, err });
|
|
1313
|
+
await this._deleteChatKvCache(chatId, 'run replay unreadable');
|
|
1314
|
+
return { sentRuns: new Map(), askedRuns: new Map(), readRuns: new Map() };
|
|
1078
1315
|
}
|
|
1079
1316
|
}
|
|
1080
|
-
async
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1317
|
+
async _recordSentRun(chatId, { runId, turns, asked }) {
|
|
1318
|
+
if (turns) {
|
|
1319
|
+
try {
|
|
1320
|
+
await this._runReplay.save({ holder: LOCAL_HOLDER, chatId }, runId, {
|
|
1321
|
+
messages: turns,
|
|
1322
|
+
asked
|
|
1323
|
+
});
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
1326
|
+
catch (err) {
|
|
1327
|
+
this._log?.warn('assistant', 'run replay not saved', { chatId, runId, err });
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
await this._deleteChatKvCache(chatId, 'run not replayable as sent');
|
|
1085
1331
|
}
|
|
1086
1332
|
async _deleteChatKvCache(chatId, reason) {
|
|
1087
1333
|
try {
|
|
@@ -1124,31 +1370,33 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1124
1370
|
}
|
|
1125
1371
|
}
|
|
1126
1372
|
_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
|
-
}
|
|
1373
|
+
this._listed = meshes;
|
|
1374
|
+
const consumed = meshes.filter((mesh) => mesh.open && mesh.meshId !== this._ownWorkspace);
|
|
1375
|
+
const open = new Set(consumed.map((mesh) => mesh.meshId));
|
|
1376
|
+
const served = new Set(consumed.filter((mesh) => mesh.served).map((mesh) => mesh.meshId));
|
|
1137
1377
|
for (const [meshId, consumer] of this._meshConsumers) {
|
|
1138
1378
|
if (open.has(meshId))
|
|
1139
1379
|
continue;
|
|
1140
1380
|
consumer.stop();
|
|
1141
1381
|
this._meshConsumers.delete(meshId);
|
|
1142
1382
|
this._pruneSeen(meshId, new Set());
|
|
1383
|
+
for (const chatId of this._servedChats.get(meshId) ?? []) {
|
|
1384
|
+
void this._deleteChatKvCache(chatId, 'served mesh left');
|
|
1385
|
+
}
|
|
1386
|
+
this._servedChats.delete(meshId);
|
|
1143
1387
|
}
|
|
1144
1388
|
for (const meshId of open) {
|
|
1145
|
-
const consumer = this._meshConsumers.get(meshId) ?? this._meshConsumer(meshId);
|
|
1389
|
+
const consumer = this._meshConsumers.get(meshId) ?? this._meshConsumer(meshId, served.has(meshId));
|
|
1146
1390
|
this._meshConsumers.set(meshId, consumer);
|
|
1147
1391
|
consumer.start();
|
|
1148
1392
|
}
|
|
1149
1393
|
}
|
|
1150
1394
|
_followAccount(account) {
|
|
1151
1395
|
this._accountMeshId = account.meshId ?? null;
|
|
1396
|
+
if ((account.ownWorkspace ?? null) !== this._ownWorkspace) {
|
|
1397
|
+
this._ownWorkspace = account.ownWorkspace ?? null;
|
|
1398
|
+
this._syncMeshConsumers(this._listed);
|
|
1399
|
+
}
|
|
1152
1400
|
const on = account.meshId ? `${account.meshId}:${account.writable}` : null;
|
|
1153
1401
|
if (on === null) {
|
|
1154
1402
|
this._advertisedOn = null;
|
|
@@ -1164,12 +1412,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1164
1412
|
void this._reconcileDeviceProfile();
|
|
1165
1413
|
this._triggerModelWarm();
|
|
1166
1414
|
}
|
|
1167
|
-
_meshConsumer(meshId) {
|
|
1415
|
+
_meshConsumer(meshId, served) {
|
|
1168
1416
|
const watches = [
|
|
1169
|
-
this._streamWatch(`
|
|
1170
|
-
this._streamWatch(`chat-list:${meshId}`, () => this._engine.chatsWatch({ meshId }), (frames) => this._reclaimDeletedChats(frames)),
|
|
1171
|
-
this._streamWatch(`agent:${meshId}`, () => this._engine.agentsWatch({ meshId }), (frames) => this._reclaimRekeyedAgents(meshId, frames))
|
|
1417
|
+
this._streamWatch(`chat-list:${meshId}`, () => this._engine.chatsWatch({ meshId }), (frames) => this._reclaimDeletedChats(frames, served ? meshId : null))
|
|
1172
1418
|
];
|
|
1419
|
+
if (!served) {
|
|
1420
|
+
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)));
|
|
1421
|
+
}
|
|
1422
|
+
const host = this._roundHost;
|
|
1423
|
+
if (host) {
|
|
1424
|
+
watches.push(this._streamWatch(`model-requests:${meshId}`, () => this._engine.modelRequestsWatch({ meshId }), (frames) => host.follow(meshId, frames)));
|
|
1425
|
+
}
|
|
1173
1426
|
return {
|
|
1174
1427
|
start: () => watches.forEach((watch) => watch.start()),
|
|
1175
1428
|
stop: () => watches.forEach((watch) => watch.stop())
|
|
@@ -1190,17 +1443,39 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1190
1443
|
};
|
|
1191
1444
|
}
|
|
1192
1445
|
async _dispatchRunRequests(meshId, frames) {
|
|
1446
|
+
let opened = true;
|
|
1193
1447
|
for await (const frame of frames) {
|
|
1194
1448
|
if (this.closing || this.suspended)
|
|
1195
1449
|
return;
|
|
1196
1450
|
const chunks = frame.chunks ?? [];
|
|
1197
|
-
|
|
1451
|
+
const deviceId = this._requireDeviceId();
|
|
1452
|
+
const handed = chunks.filter((chunk) => handedOff(chunk, deviceId));
|
|
1453
|
+
this._handedOff.set(meshId, handed);
|
|
1454
|
+
if (opened) {
|
|
1455
|
+
this._takeBackHandedOff(meshId);
|
|
1456
|
+
}
|
|
1457
|
+
opened = false;
|
|
1458
|
+
for (const chunk of chunks.filter((chunk) => !handed.includes(chunk))) {
|
|
1198
1459
|
this._dispatch(chunk, meshId);
|
|
1199
1460
|
}
|
|
1200
1461
|
this._pruneSeen(meshId, new Set(chunks.map((chunk) => chunk.runId)));
|
|
1201
1462
|
}
|
|
1202
1463
|
}
|
|
1464
|
+
_takeBackHandedOff(only) {
|
|
1465
|
+
for (const [meshId, requests] of this._handedOff) {
|
|
1466
|
+
if (only && only !== meshId)
|
|
1467
|
+
continue;
|
|
1468
|
+
const engine = meshScoped(this._engine, meshId);
|
|
1469
|
+
for (const request of requests) {
|
|
1470
|
+
engine.takeRunRequest({ chunkId: request.id }).catch((err) => {
|
|
1471
|
+
this._log?.debug('runs', 'taking a handed-off run back failed', err);
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1474
|
+
this._handedOff.delete(meshId);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1203
1477
|
_stopMeshIntake() {
|
|
1478
|
+
this._roundHost?.stop();
|
|
1204
1479
|
this._meshListWatch.stop();
|
|
1205
1480
|
this._accountWatch.stop();
|
|
1206
1481
|
for (const consumer of this._meshConsumers.values()) {
|
|
@@ -1216,35 +1491,6 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1216
1491
|
this._seen.delete(runId);
|
|
1217
1492
|
}
|
|
1218
1493
|
}
|
|
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
1494
|
_dispatch(request, meshId) {
|
|
1249
1495
|
const runId = request.runId;
|
|
1250
1496
|
if (!runId || this._runs.has(runId) || this._seen.has(runId)) {
|
|
@@ -1259,13 +1505,32 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1259
1505
|
this._seen.delete(runId);
|
|
1260
1506
|
return;
|
|
1261
1507
|
}
|
|
1262
|
-
|
|
1508
|
+
const deviceId = this._requireDeviceId();
|
|
1509
|
+
if (offeredTo(request, deviceId) && !(await this._isHandled(engine, request))) {
|
|
1510
|
+
if (this._backgrounded) {
|
|
1511
|
+
this._seen.delete(runId);
|
|
1512
|
+
if (this._state === QvacState.PRESUSPEND) {
|
|
1513
|
+
this._parked.set(runId, () => this._dispatch(request, meshId));
|
|
1514
|
+
}
|
|
1515
|
+
return;
|
|
1516
|
+
}
|
|
1517
|
+
const taken = await engine.takeRunRequest({ chunkId: request.id }).catch(noop);
|
|
1518
|
+
if (!taken) {
|
|
1519
|
+
this._seen.delete(runId);
|
|
1520
|
+
return;
|
|
1521
|
+
}
|
|
1522
|
+
request = taken;
|
|
1523
|
+
}
|
|
1524
|
+
if (!drivenBy({ request, agent }, deviceId)) {
|
|
1263
1525
|
return;
|
|
1264
1526
|
}
|
|
1265
1527
|
if (await this._isHandled(engine, request)) {
|
|
1266
1528
|
return;
|
|
1267
1529
|
}
|
|
1268
1530
|
this._log?.info('runs', 'run claimed', { runId, chatId: request.chatId, agentId });
|
|
1531
|
+
if (!request.capability) {
|
|
1532
|
+
await this._offerSteer(engine, request, agent.id);
|
|
1533
|
+
}
|
|
1269
1534
|
const controller = new AbortController();
|
|
1270
1535
|
const prior = this._chats.get(request.chatId) ?? Promise.resolve();
|
|
1271
1536
|
const done = prior.then(() => this._execute(request, agent, { controller, meshId, runId }));
|
|
@@ -1275,9 +1540,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1275
1540
|
controller,
|
|
1276
1541
|
done,
|
|
1277
1542
|
chatId: request.chatId,
|
|
1543
|
+
agentId: agent.id,
|
|
1278
1544
|
model: agent.modelName,
|
|
1279
|
-
executing: false
|
|
1280
|
-
borrowed: false
|
|
1545
|
+
executing: false
|
|
1281
1546
|
});
|
|
1282
1547
|
try {
|
|
1283
1548
|
await done;
|
|
@@ -1285,19 +1550,41 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1285
1550
|
finally {
|
|
1286
1551
|
this._runImageCacheNames.delete(runId);
|
|
1287
1552
|
this._runs.delete(runId);
|
|
1288
|
-
this.
|
|
1553
|
+
this._runsChanged();
|
|
1289
1554
|
if (this._chats.get(request.chatId) === link) {
|
|
1290
1555
|
this._chats.delete(request.chatId);
|
|
1291
1556
|
}
|
|
1292
1557
|
void this._reportCapabilities();
|
|
1293
|
-
|
|
1294
|
-
if (requeued && !this.closing) {
|
|
1295
|
-
this._dispatch(request, meshId);
|
|
1296
|
-
}
|
|
1558
|
+
this._requeue(runId, controller.signal.reason, () => this._dispatch(request, meshId));
|
|
1297
1559
|
}
|
|
1298
1560
|
})();
|
|
1299
1561
|
settle.catch((err) => this._log?.error('assistant', `run ${runId} failed`, err));
|
|
1300
1562
|
}
|
|
1563
|
+
async _offerSteer(engine, request, agentId) {
|
|
1564
|
+
const running = [...this._runs.values()].find((run) => run.chatId === request.chatId && run.agentId === agentId && run.steering);
|
|
1565
|
+
if (!running?.steering || !request.runId) {
|
|
1566
|
+
return;
|
|
1567
|
+
}
|
|
1568
|
+
const asked = await engine.listChunks({ chatId: request.chatId, type: 'user-message' });
|
|
1569
|
+
const message = (asked.chunks ?? []).findLast((chunk) => chunk.seq < request.seq);
|
|
1570
|
+
const text = message?.userMessage?.text;
|
|
1571
|
+
if (text) {
|
|
1572
|
+
const [turn] = nameSenders([{ role: 'user', content: text, sender: senderOf(message) }]);
|
|
1573
|
+
running.steering.push({ mode: 'steer', text: turn.content, id: request.runId });
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
_requeue(runId, reason, replay) {
|
|
1577
|
+
const background = this._backgroundReplays.delete(runId);
|
|
1578
|
+
if (this._seen.has(runId) || this.closing) {
|
|
1579
|
+
return;
|
|
1580
|
+
}
|
|
1581
|
+
if (background && this._state === QvacState.PRESUSPEND) {
|
|
1582
|
+
this._parked.set(runId, replay);
|
|
1583
|
+
}
|
|
1584
|
+
else if ((background && !this._backgrounded) || reason === MODEL_UNLOADED) {
|
|
1585
|
+
replay();
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1301
1588
|
async _citeWebSources(transcript, runId, result) {
|
|
1302
1589
|
const records = webSourcesFromToolResult(result, WEB_RAG_SOURCE_BOUNDS);
|
|
1303
1590
|
if (records.length === 0) {
|
|
@@ -1331,7 +1618,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1331
1618
|
if (!search.enabled) {
|
|
1332
1619
|
return null;
|
|
1333
1620
|
}
|
|
1334
|
-
if (!search.hasKnowledge) {
|
|
1621
|
+
if (!search.hasKnowledge && !notReadyCount(search)) {
|
|
1335
1622
|
return null;
|
|
1336
1623
|
}
|
|
1337
1624
|
return { search, lastUser, hits: search.hits ?? [] };
|
|
@@ -1359,7 +1646,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1359
1646
|
if (knowledge.hits.some(isTruncatedFullTextHit)) {
|
|
1360
1647
|
await sinks.transcript.chunk({
|
|
1361
1648
|
type: 'sources',
|
|
1362
|
-
sources: { sources: [], notice: RAG_TRUNCATED_NOTICE }
|
|
1649
|
+
sources: { sources: [], notice: RAG_TRUNCATED_NOTICE, ...notReady(knowledge.search) }
|
|
1363
1650
|
});
|
|
1364
1651
|
injected.sourcesWritten = true;
|
|
1365
1652
|
}
|
|
@@ -1367,8 +1654,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1367
1654
|
else {
|
|
1368
1655
|
await injectRetrievedKnowledge(knowledge, { ...sinks, injected });
|
|
1369
1656
|
}
|
|
1370
|
-
|
|
1371
|
-
|
|
1657
|
+
const line = notReadyLine(knowledge.search);
|
|
1658
|
+
if (line) {
|
|
1659
|
+
knowledge.lastUser.content += `\n\n${line}`;
|
|
1372
1660
|
}
|
|
1373
1661
|
}
|
|
1374
1662
|
catch (err) {
|
|
@@ -1376,13 +1664,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1376
1664
|
}
|
|
1377
1665
|
return injected;
|
|
1378
1666
|
}
|
|
1379
|
-
async _writeUnreadNotice(transcript, reason) {
|
|
1667
|
+
async _writeUnreadNotice(transcript, reason, search) {
|
|
1380
1668
|
try {
|
|
1381
1669
|
await transcript.chunk({
|
|
1382
1670
|
type: 'sources',
|
|
1383
1671
|
sources: {
|
|
1384
1672
|
sources: [],
|
|
1385
|
-
notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE
|
|
1673
|
+
notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE,
|
|
1674
|
+
...(search && notReady(search))
|
|
1386
1675
|
}
|
|
1387
1676
|
});
|
|
1388
1677
|
}
|
|
@@ -1390,6 +1679,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1390
1679
|
this._log?.warn('assistant', 'unread-document notice skipped', err);
|
|
1391
1680
|
}
|
|
1392
1681
|
}
|
|
1682
|
+
async _openRunLog(engine, runId) {
|
|
1683
|
+
if (!keepsRunLog(await this._ensureCapability())) {
|
|
1684
|
+
return null;
|
|
1685
|
+
}
|
|
1686
|
+
const runLog = createHostRunLogAdapter(engine);
|
|
1687
|
+
try {
|
|
1688
|
+
return { runLog, lease: await runLog.begin(runId) };
|
|
1689
|
+
}
|
|
1690
|
+
catch (err) {
|
|
1691
|
+
this._log?.warn('runs', 'run log not opened', { runId, err });
|
|
1692
|
+
return null;
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1393
1695
|
async _isHandled(engine, request) {
|
|
1394
1696
|
const { chunks } = await engine.listChunks({
|
|
1395
1697
|
chatId: request.chatId,
|
|
@@ -1397,57 +1699,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1397
1699
|
});
|
|
1398
1700
|
return (chunks ?? []).some((c) => c.runId === request.runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
1399
1701
|
}
|
|
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
1702
|
async _execute(request, agent, { controller, meshId, runId }) {
|
|
1437
1703
|
const engine = meshScoped(this._engine, meshId);
|
|
1704
|
+
const { chatId } = request;
|
|
1705
|
+
this._suggesting.get(chatId)?.abort(SUGGESTION_SUPERSEDED);
|
|
1438
1706
|
const deviceId = this._requireDeviceId();
|
|
1439
|
-
if (this.
|
|
1707
|
+
if (this._backgrounded) {
|
|
1440
1708
|
this._seen.delete(runId);
|
|
1709
|
+
this._backgroundReplays.add(runId);
|
|
1441
1710
|
return;
|
|
1442
1711
|
}
|
|
1443
1712
|
if (await this._isHandled(engine, request)) {
|
|
1444
1713
|
return;
|
|
1445
1714
|
}
|
|
1446
1715
|
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)) {
|
|
1716
|
+
if (!fresh || fresh.deletedAt || !drivenBy({ request, agent: fresh }, deviceId)) {
|
|
1451
1717
|
return;
|
|
1452
1718
|
}
|
|
1453
1719
|
agent = fresh;
|
|
@@ -1463,121 +1729,35 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1463
1729
|
runId
|
|
1464
1730
|
});
|
|
1465
1731
|
}
|
|
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,
|
|
1732
|
+
const steeredInto = this._steered.get(runId);
|
|
1733
|
+
if (steeredInto) {
|
|
1734
|
+
this._steered.delete(runId);
|
|
1735
|
+
const steered = new RunTranscript(engine, {
|
|
1736
|
+
chatId,
|
|
1498
1737
|
runId,
|
|
1499
|
-
agent,
|
|
1500
1738
|
deviceId,
|
|
1501
|
-
|
|
1502
|
-
|
|
1739
|
+
model: agent.modelName,
|
|
1740
|
+
log: this._log
|
|
1503
1741
|
});
|
|
1742
|
+
steered.close();
|
|
1743
|
+
await steered.status({ state: 'completed', reason: STEERED, metrics: { steeredInto } });
|
|
1504
1744
|
return;
|
|
1505
1745
|
}
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
runId,
|
|
1513
|
-
|
|
1514
|
-
runLog,
|
|
1515
|
-
hostLease
|
|
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
|
-
}
|
|
1746
|
+
const hold = await this._openRunLog(engine, runId);
|
|
1747
|
+
const runLog = hold?.runLog;
|
|
1748
|
+
const resumed = Boolean(hold?.lease.boot?.metadata);
|
|
1749
|
+
const releaseRunLog = async () => {
|
|
1750
|
+
await hold?.runLog
|
|
1751
|
+
.release(runId, hold.lease.generation)
|
|
1752
|
+
.catch((err) => this._log?.warn('runs', 'run log not released', { runId, err }));
|
|
1753
|
+
};
|
|
1571
1754
|
if (!resumed) {
|
|
1572
1755
|
await this._reconcileCompaction(engine, chatId);
|
|
1756
|
+
if (request.parentId) {
|
|
1757
|
+
await this._deleteChatKvCache(chatId, 'superseding run');
|
|
1758
|
+
}
|
|
1573
1759
|
}
|
|
1574
|
-
|
|
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());
|
|
1760
|
+
const model = runnableModelName(agent.modelName, await this._runnableHost());
|
|
1581
1761
|
if (model !== agent.modelName) {
|
|
1582
1762
|
this._log?.warn('runs', 'model clamped to what this device can run', {
|
|
1583
1763
|
runId,
|
|
@@ -1588,7 +1768,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1588
1768
|
}
|
|
1589
1769
|
const claimed = this._runs.get(runId);
|
|
1590
1770
|
if (claimed) {
|
|
1591
|
-
this._runs.set(runId, { ...claimed, model
|
|
1771
|
+
this._runs.set(runId, { ...claimed, model });
|
|
1592
1772
|
}
|
|
1593
1773
|
const config = await engine.getConfig({}).catch(noop);
|
|
1594
1774
|
const configuredAgent = withConfigDefaults(agent, config);
|
|
@@ -1605,26 +1785,107 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1605
1785
|
model,
|
|
1606
1786
|
log: this._log
|
|
1607
1787
|
});
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1788
|
+
const rounds = new OwnerRounds({
|
|
1789
|
+
engine,
|
|
1790
|
+
chatId,
|
|
1791
|
+
runId,
|
|
1792
|
+
agentId: agent.id,
|
|
1793
|
+
deviceId,
|
|
1794
|
+
from: request.seq,
|
|
1795
|
+
media: (file) => this._mediaRefs.get(path.basename(file))
|
|
1796
|
+
});
|
|
1797
|
+
const folded = !resumed &&
|
|
1798
|
+
(await this._compactIfNearFull({
|
|
1799
|
+
chatId,
|
|
1800
|
+
transcript,
|
|
1801
|
+
trigger: 'before-run',
|
|
1802
|
+
signal: controller.signal,
|
|
1803
|
+
meshId,
|
|
1804
|
+
run: { request, runId },
|
|
1805
|
+
rounds
|
|
1806
|
+
}));
|
|
1807
|
+
const toolConfigsPromise = this._loadToolConfigs(engine, agent.id);
|
|
1808
|
+
const fsGrantsPromise = this._loadFsGrants(agent.id);
|
|
1809
|
+
const all = await listHistoryChunks(engine, chatId);
|
|
1810
|
+
const { chunks: switches } = await engine.listChunks({ chatId, type: 'agent-switch' });
|
|
1811
|
+
const ranAndSwitched = [...all, ...(switches ?? [])].sort((a, b) => a.seq - b.seq);
|
|
1812
|
+
if (!resumed && agentSwitched(ranAndSwitched, request, deviceId)) {
|
|
1813
|
+
await this._deleteChatKvCache(chatId, 'agent switched');
|
|
1814
|
+
}
|
|
1815
|
+
const { chunks: priorAsks } = await engine.listChunks({
|
|
1816
|
+
chatId,
|
|
1817
|
+
type: 'approval-request'
|
|
1818
|
+
});
|
|
1819
|
+
const { history, stale, repair: currentRepair, acceptedRepairs } = runPromptView(all, priorAsks ?? [], { request, runId });
|
|
1820
|
+
const { chunks: priorResults } = await engine.listChunks({ chatId, type: 'tool-result' });
|
|
1821
|
+
let ran = (priorResults ?? []).some((chunk) => chunk.runId === runId &&
|
|
1822
|
+
chunk.toolResult?.name !== SKILL_TOOL &&
|
|
1823
|
+
!isDeniedResult(chunk.toolResult?.result));
|
|
1824
|
+
let replay = await this._sentRuns(chatId, await listRoundChunks(engine, chatId));
|
|
1825
|
+
if (!resumed && viewOutgrewCache(all, { request, runId }, replay.sentRuns)) {
|
|
1826
|
+
await this._deleteChatKvCache(chatId, 'a run outside the cached view');
|
|
1827
|
+
replay = { sentRuns: new Map(), askedRuns: new Map(), readRuns: new Map() };
|
|
1828
|
+
}
|
|
1829
|
+
const messages = historyFromChunks(history, stale, replay);
|
|
1830
|
+
const typedText = currentRepair
|
|
1831
|
+
? undefined
|
|
1832
|
+
: history.findLast((chunk) => chunk.userMessage)?.userMessage?.text;
|
|
1833
|
+
if (currentRepair) {
|
|
1834
|
+
messages.push({ role: 'user', content: currentRepair.promptText });
|
|
1835
|
+
}
|
|
1836
|
+
const diagramRepair = currentRepair !== undefined;
|
|
1837
|
+
const runAgent = diagramRepair
|
|
1838
|
+
? {
|
|
1839
|
+
...configuredAgent,
|
|
1840
|
+
toolsScope: 'selected',
|
|
1841
|
+
enabledTools: [],
|
|
1842
|
+
skillsScope: 'selected',
|
|
1843
|
+
enabledSkills: []
|
|
1844
|
+
}
|
|
1845
|
+
: configuredAgent;
|
|
1846
|
+
const carriesMedia = messages.some((turn) => turn.images?.length || turn.pdfs?.length || turn.videos?.length);
|
|
1847
|
+
const seesImages = carriesMedia && (await this._seesImages(model));
|
|
1848
|
+
let unreadableImages = new Map();
|
|
1849
|
+
const sampled = [];
|
|
1850
|
+
rounds.share = async (agentId) => {
|
|
1851
|
+
const host = await engine.getAgent({ agentId }).catch(() => null);
|
|
1852
|
+
if (sampled.length && host && !b4a.equals(host.providerDeviceId, deviceId)) {
|
|
1853
|
+
await this._storeVideoDigests(sampled.splice(0), { meshId, chatId, runId, deviceId });
|
|
1617
1854
|
}
|
|
1618
|
-
preflightCleanupDone = true;
|
|
1619
|
-
transcript.close();
|
|
1620
|
-
preflightModelWatch?.stop();
|
|
1621
|
-
preflightOwnershipWatch?.stop();
|
|
1622
|
-
preflightSettledWatch?.stop();
|
|
1623
|
-
await preflightApprovals
|
|
1624
|
-
?.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) })
|
|
1625
|
-
.catch((cleanupError) => this._log?.warn('approvals', 'setup cleanup failed', { runId, cleanupError }));
|
|
1626
1855
|
};
|
|
1627
|
-
|
|
1856
|
+
let pdfPages = { rendered: new Set(), cuts: [] };
|
|
1857
|
+
if (!seesImages) {
|
|
1858
|
+
const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
|
|
1859
|
+
if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
|
|
1860
|
+
this._log?.warn('assistant', `no projection pairing for ${model} — dropped ${dropped} image(s)`);
|
|
1861
|
+
}
|
|
1862
|
+
for (const turn of messages) {
|
|
1863
|
+
if (turn.images?.length) {
|
|
1864
|
+
const names = turn.images.map((image) => image.fileName).join(', ');
|
|
1865
|
+
turn.content += `\n[Attached image${turn.images.length > 1 ? 's' : ''}: ${names} — not visible to this model]`;
|
|
1866
|
+
}
|
|
1867
|
+
delete turn.images;
|
|
1868
|
+
delete turn.pdfs;
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
else {
|
|
1872
|
+
unreadableImages = await this._hydrateImages(messages, controller.signal, runId);
|
|
1873
|
+
}
|
|
1874
|
+
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
1875
|
+
const ownershipWatch = this._watchOwnership(engine, { request, agentId: agent.id, controller });
|
|
1876
|
+
let readinessError = null;
|
|
1877
|
+
const held = { status: null };
|
|
1878
|
+
try {
|
|
1879
|
+
await this._downloads?.waitUntilCached(model, controller.signal, {
|
|
1880
|
+
onHold: () => {
|
|
1881
|
+
held.status = transcript.status({ state: 'executing', progress: { phase: 'awaiting-download', model } }, { resumed });
|
|
1882
|
+
}
|
|
1883
|
+
});
|
|
1884
|
+
}
|
|
1885
|
+
catch (err) {
|
|
1886
|
+
readinessError = err instanceof Error ? err : new Error(String(err));
|
|
1887
|
+
}
|
|
1888
|
+
if (controller.signal.aborted) {
|
|
1628
1889
|
try {
|
|
1629
1890
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1630
1891
|
if (outcome.kind === 'replay') {
|
|
@@ -1635,603 +1896,494 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1635
1896
|
}
|
|
1636
1897
|
}
|
|
1637
1898
|
finally {
|
|
1638
|
-
|
|
1899
|
+
transcript.close();
|
|
1900
|
+
ownershipWatch.stop();
|
|
1901
|
+
settledWatch.stop();
|
|
1902
|
+
await releaseRunLog();
|
|
1639
1903
|
}
|
|
1904
|
+
return;
|
|
1905
|
+
}
|
|
1906
|
+
if (readinessError) {
|
|
1907
|
+
try {
|
|
1908
|
+
await transcript.status({ state: 'failed', reason: runFailureNotice(readinessError) });
|
|
1909
|
+
}
|
|
1910
|
+
finally {
|
|
1911
|
+
transcript.close();
|
|
1912
|
+
ownershipWatch.stop();
|
|
1913
|
+
settledWatch.stop();
|
|
1914
|
+
await releaseRunLog();
|
|
1915
|
+
}
|
|
1916
|
+
return;
|
|
1917
|
+
}
|
|
1918
|
+
const inflight = this._runs.get(runId);
|
|
1919
|
+
if (inflight) {
|
|
1920
|
+
this._runs.set(runId, { ...inflight, executing: true });
|
|
1921
|
+
}
|
|
1922
|
+
this._runsChanged();
|
|
1923
|
+
const busyChatId = this._modelHolder(runId, chatId, model);
|
|
1924
|
+
const runStatus = busyChatId
|
|
1925
|
+
? { state: 'executing', progress: { phase: 'queued', busyChatId } }
|
|
1926
|
+
: { state: 'executing' };
|
|
1927
|
+
const runStatusId = (await (held.status ?? transcript.status(runStatus, { resumed }))).id;
|
|
1928
|
+
if (held.status) {
|
|
1929
|
+
await transcript.progress(runStatusId, runStatus);
|
|
1930
|
+
}
|
|
1931
|
+
const approvals = this._createApprove(engine, {
|
|
1932
|
+
chatId,
|
|
1933
|
+
runId,
|
|
1934
|
+
deviceId,
|
|
1935
|
+
agentId: agent.id,
|
|
1936
|
+
hasRun: () => ran
|
|
1937
|
+
});
|
|
1938
|
+
const idle = idleWatch(controller, this._runIdleTimeoutMs);
|
|
1939
|
+
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
|
|
1940
|
+
let metrics;
|
|
1941
|
+
const sourceTagFilter = createRagSourceTagFilter();
|
|
1942
|
+
const retrievedRecords = [];
|
|
1943
|
+
const answers = new Map();
|
|
1944
|
+
let round;
|
|
1945
|
+
const answer = (text) => {
|
|
1946
|
+
answers.set(round, (answers.get(round) ?? '') + text);
|
|
1640
1947
|
};
|
|
1641
|
-
|
|
1642
|
-
const
|
|
1643
|
-
if (
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1948
|
+
const flushTranscript = async () => {
|
|
1949
|
+
const tail = sourceTagFilter.flush();
|
|
1950
|
+
if (tail) {
|
|
1951
|
+
answer(tail);
|
|
1952
|
+
await transcript.text('content', tail, round);
|
|
1953
|
+
}
|
|
1954
|
+
await transcript.flush();
|
|
1955
|
+
};
|
|
1956
|
+
let titleOutcome = null;
|
|
1957
|
+
let runFailure = null;
|
|
1958
|
+
let runVerdict = 'completed';
|
|
1959
|
+
let sentRun = null;
|
|
1960
|
+
let steering = null;
|
|
1961
|
+
const taken = new Set();
|
|
1962
|
+
let inputLength = 0;
|
|
1963
|
+
let asked;
|
|
1964
|
+
const writeText = async (type, raw) => {
|
|
1965
|
+
const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
|
|
1966
|
+
if (!text) {
|
|
1648
1967
|
return;
|
|
1649
1968
|
}
|
|
1650
|
-
if (
|
|
1651
|
-
|
|
1969
|
+
if (type === 'content') {
|
|
1970
|
+
answer(text);
|
|
1971
|
+
}
|
|
1972
|
+
await transcript.text(type, text, round);
|
|
1973
|
+
};
|
|
1974
|
+
const writeRoundText = async (type, text) => {
|
|
1975
|
+
if (!rounds.echoed(type, text)) {
|
|
1976
|
+
await writeText(type, text);
|
|
1977
|
+
}
|
|
1978
|
+
else if (type === 'content') {
|
|
1979
|
+
answer(text);
|
|
1980
|
+
}
|
|
1981
|
+
};
|
|
1982
|
+
const failRun = async (message) => {
|
|
1983
|
+
const folded = isContextOverflowFailure(message) &&
|
|
1984
|
+
(await this._runCompaction({
|
|
1652
1985
|
chatId,
|
|
1653
1986
|
transcript,
|
|
1654
|
-
trigger: '
|
|
1987
|
+
trigger: 'overflow',
|
|
1655
1988
|
signal: controller.signal,
|
|
1656
|
-
meshId
|
|
1657
|
-
|
|
1989
|
+
meshId,
|
|
1990
|
+
run: { request, runId },
|
|
1991
|
+
rounds
|
|
1992
|
+
}));
|
|
1993
|
+
runFailure = runFailureNotice(message);
|
|
1994
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
1995
|
+
if (folded) {
|
|
1996
|
+
await engine
|
|
1997
|
+
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
1998
|
+
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
1999
|
+
}
|
|
2000
|
+
titleOutcome = 'ended';
|
|
2001
|
+
};
|
|
2002
|
+
const endAbortedRun = async () => {
|
|
2003
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
2004
|
+
if (outcome.kind === 'replay') {
|
|
2005
|
+
runVerdict = 'replay';
|
|
2006
|
+
this._seen.delete(runId);
|
|
2007
|
+
return;
|
|
1658
2008
|
}
|
|
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);
|
|
2009
|
+
if (outcome.kind === 'handoff') {
|
|
2010
|
+
runVerdict = 'handoff';
|
|
2011
|
+
return;
|
|
1735
2012
|
}
|
|
1736
|
-
|
|
1737
|
-
|
|
2013
|
+
runFailure = outcome.reason;
|
|
2014
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
2015
|
+
titleOutcome = 'ended';
|
|
2016
|
+
};
|
|
2017
|
+
const pauseRun = async () => {
|
|
2018
|
+
if (this._abortOutcome(runId, controller.signal.reason).kind === 'replay') {
|
|
2019
|
+
await transcript.status({ state: 'paused', reason: RUN_PAUSED });
|
|
1738
2020
|
}
|
|
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
|
-
}
|
|
2021
|
+
await endAbortedRun();
|
|
2022
|
+
if (runVerdict === 'replay') {
|
|
2023
|
+
runVerdict = 'paused';
|
|
1748
2024
|
}
|
|
1749
|
-
|
|
1750
|
-
|
|
2025
|
+
};
|
|
2026
|
+
const resetRound = async (event) => {
|
|
2027
|
+
round = roundOf(event);
|
|
2028
|
+
const replaced = replacedTypes(event);
|
|
2029
|
+
if (replaced.includes('content')) {
|
|
2030
|
+
answers.delete(round);
|
|
2031
|
+
}
|
|
2032
|
+
if (!rounds.retook()) {
|
|
2033
|
+
await transcript.roundReset(round ?? 0, replaced);
|
|
1751
2034
|
}
|
|
2035
|
+
};
|
|
2036
|
+
const holdForRecovery = async (event) => {
|
|
2037
|
+
runVerdict = 'recovery-required';
|
|
2038
|
+
await transcript.status({ state: 'recovery-required', reason: recoveryReason(event) });
|
|
2039
|
+
titleOutcome = 'ended';
|
|
2040
|
+
};
|
|
2041
|
+
const recordToolResult = async (event) => {
|
|
2042
|
+
if (isDeniedResult(event.result)) {
|
|
2043
|
+
this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
|
|
2044
|
+
}
|
|
2045
|
+
else if (event.name !== SKILL_TOOL) {
|
|
2046
|
+
ran = true;
|
|
2047
|
+
}
|
|
2048
|
+
if (WEB_TOOLS.includes(String(event.name))) {
|
|
2049
|
+
await this._citeWebSources(transcript, runId, event.result);
|
|
2050
|
+
}
|
|
2051
|
+
await transcript.chunk(toolResultChunk(event));
|
|
2052
|
+
};
|
|
2053
|
+
try {
|
|
2054
|
+
const attachTurnIds = resumed ? [] : attachTurnDocumentIds(history);
|
|
2055
|
+
if (attachTurnIds.length > 0) {
|
|
2056
|
+
backfillDocumentOnlyTurn(messages);
|
|
2057
|
+
}
|
|
2058
|
+
const knowledge = !resumed &&
|
|
2059
|
+
!diagramRepair &&
|
|
2060
|
+
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
2061
|
+
? await this._searchKnowledge(engine, messages, {
|
|
2062
|
+
chatId,
|
|
2063
|
+
attachmentIds: attachTurnIds,
|
|
2064
|
+
signal: controller.signal,
|
|
2065
|
+
preparing: modelWatch.documents
|
|
2066
|
+
})
|
|
2067
|
+
: null;
|
|
1752
2068
|
if (controller.signal.aborted) {
|
|
1753
|
-
|
|
2069
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
2070
|
+
if (outcome.kind === 'replay') {
|
|
2071
|
+
runVerdict = 'replay';
|
|
2072
|
+
this._seen.delete(runId);
|
|
2073
|
+
return;
|
|
2074
|
+
}
|
|
2075
|
+
if (outcome.kind === 'handoff') {
|
|
2076
|
+
runVerdict = 'handoff';
|
|
2077
|
+
return;
|
|
2078
|
+
}
|
|
2079
|
+
runFailure = outcome.reason;
|
|
2080
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1754
2081
|
return;
|
|
1755
2082
|
}
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
2083
|
+
const injected = knowledge
|
|
2084
|
+
? await this._injectKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords })
|
|
2085
|
+
: { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
2086
|
+
const hasAudio = messages.some((turn) => turn.audioAttachments?.length || turn.videos?.length);
|
|
2087
|
+
if (hasAudio) {
|
|
2088
|
+
modelWatch.transcribe(true);
|
|
2089
|
+
}
|
|
2090
|
+
await this._hydrateAudio(messages, controller.signal);
|
|
2091
|
+
if (messages.some((turn) => turn.videos?.length)) {
|
|
2092
|
+
await this._hydrateVideos(messages, {
|
|
2093
|
+
signal: controller.signal,
|
|
2094
|
+
runId,
|
|
2095
|
+
seesImages,
|
|
2096
|
+
reading: await this._contextReading({ chatId, meshId, folded }, configuredAgent.ctxSize),
|
|
2097
|
+
sampled
|
|
2098
|
+
});
|
|
2099
|
+
}
|
|
2100
|
+
if (hasAudio) {
|
|
2101
|
+
modelWatch.transcribe(false);
|
|
2102
|
+
}
|
|
2103
|
+
backfillImageOnlyTurns(messages);
|
|
2104
|
+
if (seesImages) {
|
|
2105
|
+
pdfPages = await this._hydratePdfPages(messages, {
|
|
2106
|
+
signal: controller.signal,
|
|
2107
|
+
runId,
|
|
2108
|
+
window: { ctxSize: configuredAgent.ctxSize, modelName: model }
|
|
2109
|
+
});
|
|
2110
|
+
}
|
|
2111
|
+
annotateDocumentAttachments(messages);
|
|
2112
|
+
for (const { turn, notice } of pdfPages.cuts) {
|
|
2113
|
+
turn.content = `${turn.content}\n${notice}`;
|
|
2114
|
+
}
|
|
2115
|
+
const unreadIds = attachTurnIds.filter((id) => !pdfPages.rendered.has(id));
|
|
2116
|
+
if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
|
|
2117
|
+
const reason = documentUnreadReason({
|
|
2118
|
+
knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
|
|
2119
|
+
failedFiles: knowledge?.search?.failedFiles,
|
|
2120
|
+
attachedCount: unreadIds.length
|
|
2121
|
+
});
|
|
2122
|
+
const lastUser = messages.findLast((turn) => turn.role === 'user');
|
|
2123
|
+
if (lastUser) {
|
|
2124
|
+
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
|
|
1759
2125
|
}
|
|
1760
|
-
|
|
1761
|
-
await
|
|
2126
|
+
if (!injected.sourcesWritten) {
|
|
2127
|
+
await this._writeUnreadNotice(transcript, reason, knowledge?.search);
|
|
1762
2128
|
}
|
|
2129
|
+
}
|
|
2130
|
+
annotateUnreadableImages(unreadableImages);
|
|
2131
|
+
const toolConfigs = await toolConfigsPromise;
|
|
2132
|
+
const { roots, write } = await fsGrantsPromise;
|
|
2133
|
+
const { grantedDirs, grantedDrives, driveOp } = await resolveFsGrants(roots, this._drives, (key, err) => this._log?.warn('assistant', 'drive grant dropped', {
|
|
2134
|
+
key,
|
|
2135
|
+
error: errorMessage(err)
|
|
2136
|
+
}));
|
|
2137
|
+
if (await this._isHandled(engine, request)) {
|
|
1763
2138
|
return;
|
|
1764
2139
|
}
|
|
2140
|
+
const cacheKey = diagramRepair || stale.runIds.size > 0
|
|
2141
|
+
? undefined
|
|
2142
|
+
: chatRepairKvCacheKey(chatId, acceptedRepairs);
|
|
2143
|
+
sentRun = trackSentRun(cacheKey);
|
|
2144
|
+
const unloading = this._unloading.get(model);
|
|
2145
|
+
if (unloading) {
|
|
2146
|
+
await raceAbort(unloading.catch(noop), controller.signal);
|
|
2147
|
+
}
|
|
2148
|
+
this._log?.info('runs', 'completion requested', {
|
|
2149
|
+
runId,
|
|
2150
|
+
model,
|
|
2151
|
+
cacheKey,
|
|
2152
|
+
turns: messages.length
|
|
2153
|
+
});
|
|
2154
|
+
const resent = loggedMessages(hold?.lease.boot);
|
|
2155
|
+
if (resent) {
|
|
2156
|
+
asked = askedIn(resent, messages.findLast((turn) => turn.role === 'user')?.sender);
|
|
2157
|
+
}
|
|
2158
|
+
else {
|
|
2159
|
+
restoreSentContent(messages);
|
|
2160
|
+
asked = messages.findLast((turn) => turn.role === 'user')?.content;
|
|
2161
|
+
}
|
|
2162
|
+
const input = resent ?? nameSenders(messages);
|
|
2163
|
+
inputLength = input.length;
|
|
2164
|
+
rounds.inputLength = inputLength;
|
|
2165
|
+
const inspecting = await this._capturingInferenceCalls();
|
|
2166
|
+
steering = new RunInputQueue();
|
|
1765
2167
|
const inflight = this._runs.get(runId);
|
|
1766
2168
|
if (inflight) {
|
|
1767
|
-
|
|
2169
|
+
inflight.steering = steering;
|
|
1768
2170
|
}
|
|
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,
|
|
2171
|
+
idle.touch();
|
|
2172
|
+
const events = this._harness.completion({
|
|
1787
2173
|
runId,
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
2174
|
+
runLog,
|
|
2175
|
+
model,
|
|
2176
|
+
agent: runAgent,
|
|
2177
|
+
messages: input,
|
|
2178
|
+
typedText,
|
|
2179
|
+
cacheKey,
|
|
2180
|
+
replaysContext: true,
|
|
2181
|
+
deferTools: runAgent.shared === true || !!request.requesterMemberId,
|
|
2182
|
+
modelRound: idle.parks(rounds.port),
|
|
2183
|
+
session: chatKvCacheKey(chatId),
|
|
2184
|
+
thinkingDefault: thinkingDefault(config),
|
|
2185
|
+
toolConfigs,
|
|
2186
|
+
grantedDirs,
|
|
2187
|
+
grantedDrives,
|
|
2188
|
+
driveOp,
|
|
2189
|
+
grantedWriteDirs: write,
|
|
2190
|
+
approve: idle.parks(approvals.approve),
|
|
2191
|
+
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
2192
|
+
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
2193
|
+
progress: (update) => {
|
|
2194
|
+
idle.touch();
|
|
2195
|
+
modelWatch.image(update);
|
|
2196
|
+
},
|
|
2197
|
+
generationParams: diagramRepair ? { reasoning_budget: 0 } : undefined,
|
|
2198
|
+
captureInferenceCalls: true,
|
|
2199
|
+
input: steering,
|
|
2200
|
+
signal: controller.signal
|
|
1791
2201
|
});
|
|
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);
|
|
2202
|
+
let harnessEvents = 0;
|
|
2203
|
+
for await (const event of events) {
|
|
2204
|
+
harnessEvents++;
|
|
2205
|
+
idle.touch();
|
|
2206
|
+
modelWatch.clear();
|
|
2207
|
+
if (isHarnessEvent(event, 'content') || isHarnessEvent(event, 'thinking')) {
|
|
2208
|
+
round = roundOf(event) ?? round;
|
|
2209
|
+
await writeRoundText(event.type, event.text);
|
|
2210
|
+
continue;
|
|
1808
2211
|
}
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
let runVerdict = 'completed';
|
|
1814
|
-
const resetRound = async (round, replacedTypes) => {
|
|
1815
|
-
if (replacedTypes === undefined || replacedTypes.includes('content')) {
|
|
1816
|
-
sourceTagFilter = createRagSourceTagFilter();
|
|
1817
|
-
sourceTagFilter.add(retrievedRecords.map((record) => record.title));
|
|
1818
|
-
answerTextByRound.delete(round);
|
|
1819
|
-
}
|
|
1820
|
-
activeRound = round;
|
|
1821
|
-
await transcript.roundReset(round, replacedTypes);
|
|
1822
|
-
};
|
|
1823
|
-
const writeText = async (type, raw, round) => {
|
|
1824
|
-
const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
|
|
1825
|
-
if (!text) {
|
|
1826
|
-
return;
|
|
2212
|
+
if (event.type === 'run-started' ||
|
|
2213
|
+
event.type === 'run-resumed' ||
|
|
2214
|
+
event.type === 'context') {
|
|
2215
|
+
continue;
|
|
1827
2216
|
}
|
|
1828
|
-
if (type === '
|
|
1829
|
-
|
|
2217
|
+
if (event.type === 'input' && typeof event.id === 'string') {
|
|
2218
|
+
taken.add(event.id);
|
|
2219
|
+
continue;
|
|
1830
2220
|
}
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
}
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
2221
|
+
if (event.type === 'metrics') {
|
|
2222
|
+
const { type, ...rest } = event;
|
|
2223
|
+
metrics = rest;
|
|
2224
|
+
recordBackends(this._backendUse, rest.backends);
|
|
2225
|
+
this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
|
|
2226
|
+
continue;
|
|
2227
|
+
}
|
|
2228
|
+
if (event.type === 'inference-sent') {
|
|
2229
|
+
this._inspectSent(inspecting, chatId, {
|
|
2230
|
+
runId,
|
|
2231
|
+
round: Number(event.round ?? 0),
|
|
2232
|
+
messages: event.messages,
|
|
2233
|
+
tokens: event.tokens,
|
|
2234
|
+
outcome: event.outcome,
|
|
2235
|
+
reason: event.reason
|
|
2236
|
+
});
|
|
2237
|
+
continue;
|
|
2238
|
+
}
|
|
2239
|
+
if (event.type === 'inference-call') {
|
|
2240
|
+
const call = asRecord(event.call);
|
|
2241
|
+
sentRun?.observe(call);
|
|
2242
|
+
this._inspectCall(inspecting, chatId, { runId, at: Date.now(), call });
|
|
2243
|
+
continue;
|
|
2244
|
+
}
|
|
2245
|
+
await flushTranscript();
|
|
2246
|
+
if (event.type === 'error' && !this._backgrounded) {
|
|
2247
|
+
await failRun(event.message);
|
|
1856
2248
|
return;
|
|
1857
2249
|
}
|
|
1858
|
-
if (
|
|
1859
|
-
|
|
2250
|
+
if (event.type === 'error') {
|
|
2251
|
+
this._log?.info('runs', 'background error replayed', { runId, message: event.message });
|
|
2252
|
+
}
|
|
2253
|
+
if (event.type === 'error' || event.type === 'aborted') {
|
|
2254
|
+
await endAbortedRun();
|
|
1860
2255
|
return;
|
|
1861
2256
|
}
|
|
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 });
|
|
2257
|
+
if (event.type === 'paused') {
|
|
2258
|
+
await pauseRun();
|
|
2259
|
+
return;
|
|
1874
2260
|
}
|
|
1875
|
-
|
|
1876
|
-
|
|
2261
|
+
if (event.type === 'recovery-required') {
|
|
2262
|
+
await holdForRecovery(event);
|
|
2263
|
+
return;
|
|
1877
2264
|
}
|
|
1878
|
-
if (
|
|
1879
|
-
await
|
|
2265
|
+
if (event.type === 'round-reset') {
|
|
2266
|
+
await resetRound(event);
|
|
2267
|
+
continue;
|
|
1880
2268
|
}
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
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';
|
|
2269
|
+
if (isHarnessEvent(event, 'tool-call')) {
|
|
2270
|
+
this._log?.info('runs', 'tool call', { runId, name: event.name });
|
|
2271
|
+
await transcript.chunk(toolCallChunk(event));
|
|
2179
2272
|
}
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
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, {
|
|
2273
|
+
else if (isHarnessEvent(event, 'tool-result')) {
|
|
2274
|
+
await recordToolResult(event);
|
|
2275
|
+
}
|
|
2276
|
+
else if (isHarnessEvent(event, 'tool-activity')) {
|
|
2277
|
+
void engine
|
|
2278
|
+
.recordToolActivity({
|
|
2279
|
+
agentId: runAgent.id,
|
|
2280
|
+
toolName: event.name,
|
|
2281
|
+
decision: event.decision,
|
|
2282
|
+
effect: event.effect,
|
|
2283
|
+
arguments: event.args,
|
|
2284
|
+
resource: event.resource ?? undefined,
|
|
2285
|
+
chatId,
|
|
2202
2286
|
runId,
|
|
2203
|
-
|
|
2204
|
-
reason: runFailure
|
|
2287
|
+
at: Date.now()
|
|
2205
2288
|
})
|
|
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
|
-
}
|
|
2289
|
+
.catch((err) => this._log?.warn('runs', 'tool activity record failed', { runId, err }));
|
|
2221
2290
|
}
|
|
2222
|
-
|
|
2223
|
-
|
|
2291
|
+
else {
|
|
2292
|
+
this._log?.warn('assistant', 'unknown harness event dropped', { type: event.type });
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
this._log?.info('runs', 'harness stream ended', { runId, events: harnessEvents });
|
|
2296
|
+
if (await settledWithin(flushTranscript(), FINAL_FLUSH_MS)) {
|
|
2297
|
+
this._log?.info('runs', 'final flush done', { runId });
|
|
2298
|
+
}
|
|
2299
|
+
else {
|
|
2300
|
+
this._log?.warn('runs', 'completing over a stranded flush', {
|
|
2301
|
+
runId,
|
|
2302
|
+
pending: transcript.pending()
|
|
2303
|
+
});
|
|
2304
|
+
}
|
|
2305
|
+
const citedTitles = injected.ragInjected
|
|
2306
|
+
? citedByPhraseOverlap([...answers.values()].join(''), retrievedRecords)
|
|
2307
|
+
: null;
|
|
2308
|
+
if (sentRun?.used && !rounds.rounds) {
|
|
2309
|
+
await this._recordSentRun(chatId, { runId, turns: sentRun.turns(inputLength), asked });
|
|
2310
|
+
}
|
|
2311
|
+
sentRun = null;
|
|
2312
|
+
await transcript.status({
|
|
2313
|
+
state: 'completed',
|
|
2314
|
+
metrics: citedTitles ? { ...metrics, citedTitles } : metrics
|
|
2315
|
+
});
|
|
2316
|
+
titleOutcome = 'completed';
|
|
2317
|
+
for (const id of taken) {
|
|
2318
|
+
this._steered.set(id, runId);
|
|
2319
|
+
}
|
|
2320
|
+
await this._storeVideoDigests(sampled, { meshId, chatId, runId, deviceId });
|
|
2321
|
+
}
|
|
2322
|
+
catch (err) {
|
|
2323
|
+
const outcome = controller.signal.aborted
|
|
2324
|
+
? this._abortOutcome(runId, controller.signal.reason)
|
|
2325
|
+
: null;
|
|
2326
|
+
if (outcome?.kind === 'replay') {
|
|
2327
|
+
runVerdict = 'replay';
|
|
2328
|
+
this._seen.delete(runId);
|
|
2329
|
+
return;
|
|
2330
|
+
}
|
|
2331
|
+
if (outcome?.kind === 'handoff') {
|
|
2332
|
+
runVerdict = 'handoff';
|
|
2333
|
+
return;
|
|
2334
|
+
}
|
|
2335
|
+
await flushTranscript();
|
|
2336
|
+
runFailure = runFailureNotice(err);
|
|
2337
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
2338
|
+
titleOutcome = 'ended';
|
|
2224
2339
|
}
|
|
2225
2340
|
finally {
|
|
2226
|
-
|
|
2227
|
-
|
|
2341
|
+
steering?.close();
|
|
2342
|
+
if (sentRun?.used) {
|
|
2343
|
+
await this._recordSentRun(chatId, { runId, turns: null });
|
|
2344
|
+
}
|
|
2345
|
+
void this._inferenceCalls
|
|
2346
|
+
.recordOutcome(chatId, {
|
|
2347
|
+
runId,
|
|
2348
|
+
outcome: runFailure ? 'failed' : runVerdict,
|
|
2349
|
+
reason: runFailure
|
|
2350
|
+
})
|
|
2351
|
+
.catch((err) => this._log?.warn('assistant', 'run outcome not recorded', { err }));
|
|
2352
|
+
transcript.close();
|
|
2353
|
+
idle.stop();
|
|
2354
|
+
modelWatch.stop();
|
|
2355
|
+
ownershipWatch.stop();
|
|
2356
|
+
await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
|
|
2357
|
+
settledWatch.stop();
|
|
2358
|
+
await releaseRunLog();
|
|
2359
|
+
if (titleOutcome && !diagramRepair) {
|
|
2360
|
+
const completed = titleOutcome === 'completed';
|
|
2361
|
+
const answerText = [...answers.values()].join('');
|
|
2362
|
+
void this._autoTitle(engine, {
|
|
2363
|
+
chatId,
|
|
2364
|
+
messages,
|
|
2365
|
+
model,
|
|
2366
|
+
agent: runAgent,
|
|
2367
|
+
probe: completed,
|
|
2368
|
+
rounds
|
|
2369
|
+
}).then(() => completed && answerText.trim()
|
|
2370
|
+
? this._suggestNext(engine, {
|
|
2371
|
+
chatId,
|
|
2372
|
+
runId,
|
|
2373
|
+
messages,
|
|
2374
|
+
answer: answerText,
|
|
2375
|
+
model,
|
|
2376
|
+
agent: runAgent,
|
|
2377
|
+
rounds
|
|
2378
|
+
})
|
|
2379
|
+
: undefined);
|
|
2228
2380
|
}
|
|
2229
2381
|
}
|
|
2230
2382
|
}
|
|
2231
2383
|
async _invokeCapability({ engine, request, agent, capability, controller, meshId, runId }) {
|
|
2232
2384
|
const { chatId } = request;
|
|
2233
2385
|
const deviceId = this._requireDeviceId();
|
|
2234
|
-
const harness = this.
|
|
2386
|
+
const harness = this._harness;
|
|
2235
2387
|
const transcript = new RunTranscript(engine, {
|
|
2236
2388
|
chatId,
|
|
2237
2389
|
runId,
|
|
@@ -2243,11 +2395,11 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2243
2395
|
if (inflight) {
|
|
2244
2396
|
this._runs.set(runId, { ...inflight, executing: true, model: '' });
|
|
2245
2397
|
}
|
|
2246
|
-
this.
|
|
2398
|
+
this._runsChanged();
|
|
2247
2399
|
const executing = await transcript.status({ state: 'executing' });
|
|
2248
2400
|
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(executing.id, runStatus));
|
|
2249
2401
|
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
2250
|
-
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
2402
|
+
const ownershipWatch = this._watchOwnership(engine, { request, agentId: agent.id, controller });
|
|
2251
2403
|
let metrics;
|
|
2252
2404
|
const endAbortedCapability = async () => {
|
|
2253
2405
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
@@ -2284,7 +2436,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2284
2436
|
this._log?.info('runs', 'capability metrics', { runId, capability, ...rest });
|
|
2285
2437
|
continue;
|
|
2286
2438
|
}
|
|
2287
|
-
if (event.type === 'error') {
|
|
2439
|
+
if (event.type === 'error' && !this._backgrounded) {
|
|
2288
2440
|
await transcript.status({
|
|
2289
2441
|
state: 'failed',
|
|
2290
2442
|
reason: runFailureNotice(event.message),
|
|
@@ -2292,7 +2444,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2292
2444
|
});
|
|
2293
2445
|
return;
|
|
2294
2446
|
}
|
|
2295
|
-
if (event.type === '
|
|
2447
|
+
if (event.type === 'error') {
|
|
2448
|
+
this._log?.info('runs', 'background error replayed', { runId, message: event.message });
|
|
2449
|
+
}
|
|
2450
|
+
if (event.type === 'error' || event.type === 'aborted') {
|
|
2296
2451
|
await endAbortedCapability();
|
|
2297
2452
|
return;
|
|
2298
2453
|
}
|
|
@@ -2321,9 +2476,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2321
2476
|
}
|
|
2322
2477
|
await transcript.status({
|
|
2323
2478
|
state: 'failed',
|
|
2324
|
-
reason: outcome?.reason ??
|
|
2325
|
-
(this._lenderFor(agent) ? loanFailureNotice(err) : null) ??
|
|
2326
|
-
runFailureNotice(err),
|
|
2479
|
+
reason: outcome?.reason ?? runFailureNotice(err),
|
|
2327
2480
|
metrics
|
|
2328
2481
|
});
|
|
2329
2482
|
}
|
|
@@ -2430,9 +2583,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2430
2583
|
return null;
|
|
2431
2584
|
}
|
|
2432
2585
|
}
|
|
2433
|
-
async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId }) {
|
|
2586
|
+
async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId, sourceId }) {
|
|
2434
2587
|
const duplex = this._engine.uploadAttachment();
|
|
2435
|
-
duplex.write({
|
|
2588
|
+
duplex.write({
|
|
2589
|
+
chatId,
|
|
2590
|
+
runId,
|
|
2591
|
+
originDeviceId: deviceId,
|
|
2592
|
+
fileName,
|
|
2593
|
+
mimeType,
|
|
2594
|
+
raw,
|
|
2595
|
+
meshId,
|
|
2596
|
+
sourceId
|
|
2597
|
+
});
|
|
2436
2598
|
const buf = b4a.toBuffer(bytes);
|
|
2437
2599
|
for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
|
|
2438
2600
|
duplex.write({ data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
|
|
@@ -2498,6 +2660,260 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2498
2660
|
return unheardAudioNotice(label, 'transcription failed on this device');
|
|
2499
2661
|
}
|
|
2500
2662
|
}
|
|
2663
|
+
async _seesImages(model) {
|
|
2664
|
+
if (this._harness.supportsImagesFor) {
|
|
2665
|
+
return this._harness.supportsImagesFor(model);
|
|
2666
|
+
}
|
|
2667
|
+
const anyModel = (this._harness.supportsImages ?? this._info.supportsImages) !== false;
|
|
2668
|
+
if (!anyModel || !this._harness.list) {
|
|
2669
|
+
return anyModel;
|
|
2670
|
+
}
|
|
2671
|
+
const rows = await this._harness.list({ type: 'llm' }).catch(noop);
|
|
2672
|
+
if (!rows) {
|
|
2673
|
+
return anyModel;
|
|
2674
|
+
}
|
|
2675
|
+
const row = rows.find((candidate) => candidate.name === model);
|
|
2676
|
+
return row ? (row.supportsImages ?? anyModel) : false;
|
|
2677
|
+
}
|
|
2678
|
+
async _hydrateVideos(messages, run) {
|
|
2679
|
+
const { signal, runId, seesImages, reading, sampled } = run;
|
|
2680
|
+
const turns = messages.filter((turn) => turn.videos?.length);
|
|
2681
|
+
if (!turns.length) {
|
|
2682
|
+
return;
|
|
2683
|
+
}
|
|
2684
|
+
const asked = messages.findLast((turn) => turn.role === 'user')?.videos ?? [];
|
|
2685
|
+
const dir = this._imageCacheDir;
|
|
2686
|
+
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
2687
|
+
const pinned = this._runImageCacheNames.get(runId) ?? new Set();
|
|
2688
|
+
this._runImageCacheNames.set(runId, pinned);
|
|
2689
|
+
const unsampled = turns.flatMap((turn) => turn.videos ?? []).filter((ref) => !ref.digest);
|
|
2690
|
+
const budget = { ...reading, videos: unsampled.length };
|
|
2691
|
+
for (const turn of turns) {
|
|
2692
|
+
for (const ref of turn.videos ?? []) {
|
|
2693
|
+
if (signal.aborted)
|
|
2694
|
+
return;
|
|
2695
|
+
const hydration = { dir, ref, pinned, seesImages, signal, budget, sampled };
|
|
2696
|
+
const blocks = await this._hydrateVideo(turn, hydration);
|
|
2697
|
+
turn.content = [turn.content.trim(), ...blocks].filter(Boolean).join('\n\n');
|
|
2698
|
+
}
|
|
2699
|
+
delete turn.videos;
|
|
2700
|
+
}
|
|
2701
|
+
const pictured = asked.some((ref) => ref.digest || this._videoProbes.get(ref.blobId)?.hasVideo);
|
|
2702
|
+
if (!seesImages && pictured) {
|
|
2703
|
+
throw new Error(RUN_FAILURE_REASONS.videoNeedsVision);
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
async _hydrateVideo(turn, hydration) {
|
|
2707
|
+
const { ref, seesImages, signal } = hydration;
|
|
2708
|
+
const source = { opened: null };
|
|
2709
|
+
const open = () => (source.opened ??= this._openVideo(hydration));
|
|
2710
|
+
try {
|
|
2711
|
+
const video = await this._readVideo(hydration, open);
|
|
2712
|
+
if (video === 'unsynced') {
|
|
2713
|
+
return [unsyncedVideoNotice(ref.fileName)];
|
|
2714
|
+
}
|
|
2715
|
+
if (video === 'audio') {
|
|
2716
|
+
return [await this._transcribeAttachment(ref, signal)];
|
|
2717
|
+
}
|
|
2718
|
+
if (!seesImages || !('files' in video)) {
|
|
2719
|
+
return [hiddenVideoNotice(ref.fileName), videoAudioBlock(ref.fileName, video)];
|
|
2720
|
+
}
|
|
2721
|
+
attachPages(turn, video.files);
|
|
2722
|
+
const annotation = videoAnnotation({
|
|
2723
|
+
fileName: ref.fileName,
|
|
2724
|
+
duration: video.duration,
|
|
2725
|
+
times: video.times
|
|
2726
|
+
});
|
|
2727
|
+
return [annotation, videoAudioBlock(ref.fileName, video)];
|
|
2728
|
+
}
|
|
2729
|
+
catch (err) {
|
|
2730
|
+
if (signal.aborted) {
|
|
2731
|
+
throw err;
|
|
2732
|
+
}
|
|
2733
|
+
this._log?.warn('assistant', `video hydration failed — ${ref.fileName}`, err);
|
|
2734
|
+
return [unreadVideoNotice(ref.fileName)];
|
|
2735
|
+
}
|
|
2736
|
+
finally {
|
|
2737
|
+
const video = await source.opened?.catch(noop);
|
|
2738
|
+
if (video) {
|
|
2739
|
+
await fs.promises.unlink(video.file).catch(noop);
|
|
2740
|
+
}
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
async _readVideo(hydration, open) {
|
|
2744
|
+
const { ref, seesImages, signal } = hydration;
|
|
2745
|
+
const stored = ref.digest ? await this._storedVideo(ref.digest, hydration) : null;
|
|
2746
|
+
if (stored) {
|
|
2747
|
+
return stored;
|
|
2748
|
+
}
|
|
2749
|
+
const probe = await this._probeVideo(ref, open);
|
|
2750
|
+
if (!probe) {
|
|
2751
|
+
return 'unsynced';
|
|
2752
|
+
}
|
|
2753
|
+
if (!probe.hasVideo) {
|
|
2754
|
+
return 'audio';
|
|
2755
|
+
}
|
|
2756
|
+
if (!seesImages) {
|
|
2757
|
+
return probe.hasAudio ? this._hearVideo(ref, open, signal) : { audio: 'none' };
|
|
2758
|
+
}
|
|
2759
|
+
const video = await open();
|
|
2760
|
+
if (!video) {
|
|
2761
|
+
return 'unsynced';
|
|
2762
|
+
}
|
|
2763
|
+
return this._sampleVideo(video, probe, hydration);
|
|
2764
|
+
}
|
|
2765
|
+
async _probeVideo(ref, open) {
|
|
2766
|
+
const known = this._videoProbes.get(ref.blobId);
|
|
2767
|
+
if (known) {
|
|
2768
|
+
return known;
|
|
2769
|
+
}
|
|
2770
|
+
const video = await open();
|
|
2771
|
+
if (!video) {
|
|
2772
|
+
return null;
|
|
2773
|
+
}
|
|
2774
|
+
const probe = await probeVideo(video.file);
|
|
2775
|
+
this._videoProbes.set(ref.blobId, probe);
|
|
2776
|
+
return probe;
|
|
2777
|
+
}
|
|
2778
|
+
async _storedVideo(digest, { dir, ref, pinned, signal }) {
|
|
2779
|
+
const headerName = videoHeaderCacheName(digest.blobId);
|
|
2780
|
+
pinned.add(headerName);
|
|
2781
|
+
const header = await fs.promises
|
|
2782
|
+
.readFile(path.join(dir, headerName), 'utf8')
|
|
2783
|
+
.then(parseVideoDigestHeader, noop);
|
|
2784
|
+
if (header) {
|
|
2785
|
+
const files = header.times.map((_, i) => {
|
|
2786
|
+
const name = videoFrameCacheName(ref, header.times.length, i);
|
|
2787
|
+
pinned.add(name);
|
|
2788
|
+
this._mediaRefs.set(name, { id: ref.id, frame: i, frames: header.times.length });
|
|
2789
|
+
return path.join(dir, name);
|
|
2790
|
+
});
|
|
2791
|
+
const cached = await Promise.all(files.map((file) => cacheOrTouch(file, noBytes)));
|
|
2792
|
+
if (cached.every(Boolean)) {
|
|
2793
|
+
return { ...header, files };
|
|
2794
|
+
}
|
|
2795
|
+
}
|
|
2796
|
+
const bytes = await this._readBlob(digest.url, signal);
|
|
2797
|
+
if (!bytes) {
|
|
2798
|
+
return null;
|
|
2799
|
+
}
|
|
2800
|
+
const target = { dir, pinned, digestBlobId: digest.blobId };
|
|
2801
|
+
try {
|
|
2802
|
+
return await this._cacheVideo(ref, unpackVideoDigest(bytes), target);
|
|
2803
|
+
}
|
|
2804
|
+
catch (err) {
|
|
2805
|
+
this._log?.warn('assistant', `video digest unreadable — ${ref.fileName}`, err);
|
|
2806
|
+
return null;
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
async _cacheVideo(ref, digest, { dir, pinned, digestBlobId }) {
|
|
2810
|
+
const { frames, ...header } = digest;
|
|
2811
|
+
const files = frames.map((_, i) => {
|
|
2812
|
+
const name = videoFrameCacheName(ref, frames.length, i);
|
|
2813
|
+
pinned.add(name);
|
|
2814
|
+
this._mediaRefs.set(name, { id: ref.id, frame: i, frames: frames.length });
|
|
2815
|
+
return path.join(dir, name);
|
|
2816
|
+
});
|
|
2817
|
+
await Promise.all(frames.map((frame, i) => cacheOrTouch(files[i], () => Promise.resolve(frame))));
|
|
2818
|
+
if (digestBlobId) {
|
|
2819
|
+
const headerName = videoHeaderCacheName(digestBlobId);
|
|
2820
|
+
pinned.add(headerName);
|
|
2821
|
+
await cacheOrTouch(path.join(dir, headerName), () => Promise.resolve(b4a.from(JSON.stringify(header))));
|
|
2822
|
+
}
|
|
2823
|
+
return { ...header, files };
|
|
2824
|
+
}
|
|
2825
|
+
async _sampleVideo(video, probe, hydration) {
|
|
2826
|
+
const { ref, budget, signal } = hydration;
|
|
2827
|
+
const started = Date.now();
|
|
2828
|
+
const count = videoFrameCount(probe, budget);
|
|
2829
|
+
const times = frameTimes(probe.duration, count);
|
|
2830
|
+
const frames = await sampleFrames(video.file, { times, maxEdge: VIDEO_FRAME_EDGE, probe });
|
|
2831
|
+
const sampleMs = Date.now() - started;
|
|
2832
|
+
const heard = probe.hasAudio
|
|
2833
|
+
? await this._hearVideo(ref, () => Promise.resolve(video), signal)
|
|
2834
|
+
: { audio: 'none' };
|
|
2835
|
+
const digest = {
|
|
2836
|
+
duration: probe.duration,
|
|
2837
|
+
times: frames.map((frame) => frame.time),
|
|
2838
|
+
...heard,
|
|
2839
|
+
frames: frames.map((frame) => frame.jpeg)
|
|
2840
|
+
};
|
|
2841
|
+
if (!hydration.ref.digest) {
|
|
2842
|
+
hydration.sampled.push({ ref, digest });
|
|
2843
|
+
}
|
|
2844
|
+
this._log?.info('assistant', 'video frames', {
|
|
2845
|
+
fileName: ref.fileName,
|
|
2846
|
+
duration: probe.duration,
|
|
2847
|
+
frames: frames.length,
|
|
2848
|
+
budget: count,
|
|
2849
|
+
ctxSize: budget.ctxSize ?? null,
|
|
2850
|
+
cacheTokens: budget.cacheTokens ?? null,
|
|
2851
|
+
audio: heard.audio,
|
|
2852
|
+
sampleMs
|
|
2853
|
+
});
|
|
2854
|
+
return this._cacheVideo(ref, digest, hydration);
|
|
2855
|
+
}
|
|
2856
|
+
async _storeVideoDigests(sampled, target) {
|
|
2857
|
+
const dir = this._imageCacheDir;
|
|
2858
|
+
const pinned = this._runImageCacheNames.get(target.runId) ?? new Set();
|
|
2859
|
+
for (const { ref, digest } of sampled) {
|
|
2860
|
+
try {
|
|
2861
|
+
const blobId = await this._uploadVideoDigest(ref, digest, target);
|
|
2862
|
+
await this._cacheVideo(ref, digest, { dir, pinned, digestBlobId: blobId });
|
|
2863
|
+
}
|
|
2864
|
+
catch (err) {
|
|
2865
|
+
this._log?.warn('assistant', `video digest not stored — ${ref.fileName}`, err);
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
async _uploadVideoDigest(ref, digest, target) {
|
|
2870
|
+
const attachment = await this._uploadGenerated({
|
|
2871
|
+
fileName: videoDigestFileName(ref.fileName),
|
|
2872
|
+
mimeType: VIDEO_DIGEST_MIME,
|
|
2873
|
+
bytes: packVideoDigest(digest),
|
|
2874
|
+
raw: true
|
|
2875
|
+
}, { ...target, sourceId: ref.id });
|
|
2876
|
+
return attachment.blobId;
|
|
2877
|
+
}
|
|
2878
|
+
async _openVideo({ dir, ref, pinned, signal }) {
|
|
2879
|
+
const bytes = await this._readBlob(ref.url, signal);
|
|
2880
|
+
if (!bytes) {
|
|
2881
|
+
return null;
|
|
2882
|
+
}
|
|
2883
|
+
const name = `${imageCacheName(ref)}.source`;
|
|
2884
|
+
pinned.add(name);
|
|
2885
|
+
const file = path.join(dir, name);
|
|
2886
|
+
await fs.promises.writeFile(file, bytes, { mode: 0o600 });
|
|
2887
|
+
return { bytes, file };
|
|
2888
|
+
}
|
|
2889
|
+
async _hearVideo(ref, open, signal) {
|
|
2890
|
+
const cached = this._audioTranscripts.get(ref.blobId);
|
|
2891
|
+
if (cached !== undefined) {
|
|
2892
|
+
return cached ? { audio: 'speech', transcript: cached } : { audio: 'silent' };
|
|
2893
|
+
}
|
|
2894
|
+
if (!this._harness.transcribe) {
|
|
2895
|
+
return { audio: 'unheard' };
|
|
2896
|
+
}
|
|
2897
|
+
try {
|
|
2898
|
+
const video = await open();
|
|
2899
|
+
if (!video) {
|
|
2900
|
+
return { audio: 'unheard' };
|
|
2901
|
+
}
|
|
2902
|
+
const model = mediaModelNameFor('transcription', await this._persistedProfile());
|
|
2903
|
+
await this.ensureMediaModelUnpaused('transcription', model);
|
|
2904
|
+
await this._downloads?.waitUntilCached(model, signal);
|
|
2905
|
+
const transcript = (await this._harness.transcribe({ model: '', audio: b4a.toBuffer(video.bytes), signal })).trim();
|
|
2906
|
+
this._audioTranscripts.set(ref.blobId, transcript);
|
|
2907
|
+
return transcript ? { audio: 'speech', transcript } : { audio: 'silent' };
|
|
2908
|
+
}
|
|
2909
|
+
catch (err) {
|
|
2910
|
+
if (signal.aborted) {
|
|
2911
|
+
throw err;
|
|
2912
|
+
}
|
|
2913
|
+
this._log?.warn('assistant', `video transcription failed — ${ref.fileName}`, err);
|
|
2914
|
+
return { audio: 'unheard' };
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2501
2917
|
async _hydrateImages(messages, signal, runId) {
|
|
2502
2918
|
const unreadable = new Map();
|
|
2503
2919
|
const turns = messages.filter((turn) => turn.images?.length);
|
|
@@ -2534,8 +2950,70 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2534
2950
|
}
|
|
2535
2951
|
return dropped;
|
|
2536
2952
|
}
|
|
2953
|
+
async _servedMedia(sources, { id, modelId, signal }) {
|
|
2954
|
+
if (!(await this._seesImages(modelId))) {
|
|
2955
|
+
return sources.map(() => null);
|
|
2956
|
+
}
|
|
2957
|
+
const dir = this._imageCacheDir;
|
|
2958
|
+
const pinned = new Set();
|
|
2959
|
+
this._runImageCacheNames.set(`round:${id}`, pinned);
|
|
2960
|
+
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
2961
|
+
const docs = new Map();
|
|
2962
|
+
try {
|
|
2963
|
+
const files = [];
|
|
2964
|
+
for (const source of sources) {
|
|
2965
|
+
files.push(await this._servedFile(source, { dir, pinned, docs, signal }).catch(() => null));
|
|
2966
|
+
}
|
|
2967
|
+
return files;
|
|
2968
|
+
}
|
|
2969
|
+
finally {
|
|
2970
|
+
for (const doc of docs.values()) {
|
|
2971
|
+
void doc.then((open) => open?.close(), noop);
|
|
2972
|
+
}
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
_servedFile(source, served) {
|
|
2976
|
+
const { ref, attachment } = source;
|
|
2977
|
+
if (ref.frame !== undefined && ref.frames) {
|
|
2978
|
+
return this._servedFrame(source, { frame: ref.frame, frames: ref.frames }, served);
|
|
2979
|
+
}
|
|
2980
|
+
if (ref.page !== undefined) {
|
|
2981
|
+
return this._servedPage(attachment, ref.page, served);
|
|
2982
|
+
}
|
|
2983
|
+
served.pinned.add(imageCacheName(attachment));
|
|
2984
|
+
return this._hydrateImage(served.dir, attachment, served.signal);
|
|
2985
|
+
}
|
|
2986
|
+
async _servedFrame({ attachment, digest }, { frame, frames }, { dir, pinned, signal }) {
|
|
2987
|
+
const file = path.join(dir, videoFrameCacheName(attachment, frames, frame));
|
|
2988
|
+
pinned.add(path.basename(file));
|
|
2989
|
+
if (await cacheOrTouch(file, noBytes)) {
|
|
2990
|
+
return file;
|
|
2991
|
+
}
|
|
2992
|
+
const bytes = digest ? await this._readBlob(digest.url, signal) : null;
|
|
2993
|
+
const video = bytes && unpackVideoDigest(bytes);
|
|
2994
|
+
if (!digest || !video || video.frames.length !== frames) {
|
|
2995
|
+
return null;
|
|
2996
|
+
}
|
|
2997
|
+
const target = { dir, pinned, digestBlobId: digest.blobId };
|
|
2998
|
+
return (await this._cacheVideo(attachment, video, target)).files[frame] ?? null;
|
|
2999
|
+
}
|
|
3000
|
+
async _servedPage(pdf, page, { dir, pinned, docs, signal }) {
|
|
3001
|
+
const name = pdfPageCacheName(pdf, page);
|
|
3002
|
+
pinned.add(name);
|
|
3003
|
+
const file = path.join(dir, name);
|
|
3004
|
+
const rendered = await cacheOrTouch(file, async () => {
|
|
3005
|
+
let doc = docs.get(pdf.id);
|
|
3006
|
+
if (!doc) {
|
|
3007
|
+
doc = this._readBlob(pdf.url, signal).then((bytes) => bytes ? this._pdfRasterizer.open(bytes) : null);
|
|
3008
|
+
docs.set(pdf.id, doc);
|
|
3009
|
+
}
|
|
3010
|
+
return (await doc)?.renderPage(page) ?? null;
|
|
3011
|
+
});
|
|
3012
|
+
return rendered ? file : null;
|
|
3013
|
+
}
|
|
2537
3014
|
async _hydrateImage(dir, image, signal) {
|
|
2538
3015
|
const file = path.join(dir, imageCacheName(image));
|
|
3016
|
+
this._mediaRefs.set(imageCacheName(image), { id: image.id });
|
|
2539
3017
|
await cacheOrTouch(file, async () => {
|
|
2540
3018
|
const raw = await this._readBlob(image.url, signal);
|
|
2541
3019
|
if (signal.aborted) {
|
|
@@ -2548,36 +3026,66 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2548
3026
|
});
|
|
2549
3027
|
return file;
|
|
2550
3028
|
}
|
|
2551
|
-
async _hydratePdfPages(messages, signal, runId) {
|
|
3029
|
+
async _hydratePdfPages(messages, { signal, runId, window }) {
|
|
2552
3030
|
const rendered = new Set();
|
|
3031
|
+
const cuts = [];
|
|
2553
3032
|
const turns = messages.filter((turn) => turn.pdfs?.length);
|
|
2554
3033
|
if (!turns.length) {
|
|
2555
|
-
return rendered;
|
|
3034
|
+
return { rendered, cuts };
|
|
2556
3035
|
}
|
|
2557
3036
|
const dir = this._imageCacheDir;
|
|
2558
3037
|
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
2559
3038
|
const pinned = this._runImageCacheNames.get(runId) ?? new Set();
|
|
2560
3039
|
this._runImageCacheNames.set(runId, pinned);
|
|
2561
3040
|
const latestUser = messages.findLast((turn) => turn.role === 'user');
|
|
3041
|
+
const maxPages = await this._pdfPageBudget(window);
|
|
2562
3042
|
for (const turn of turns) {
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
});
|
|
2573
|
-
budget -= pages.tried;
|
|
2574
|
-
if (turn === latestUser && pages.names.length) {
|
|
2575
|
-
rendered.add(pdf.id);
|
|
3043
|
+
const { shown, notices } = await this._hydrateTurnPdfs(turn, {
|
|
3044
|
+
dir,
|
|
3045
|
+
pinned,
|
|
3046
|
+
signal,
|
|
3047
|
+
maxPages
|
|
3048
|
+
});
|
|
3049
|
+
if (turn === latestUser) {
|
|
3050
|
+
for (const id of shown) {
|
|
3051
|
+
rendered.add(id);
|
|
2576
3052
|
}
|
|
2577
3053
|
}
|
|
2578
|
-
|
|
3054
|
+
cuts.push(...notices.map((notice) => ({ turn, notice })));
|
|
2579
3055
|
}
|
|
2580
|
-
return rendered;
|
|
3056
|
+
return { rendered, cuts };
|
|
3057
|
+
}
|
|
3058
|
+
async _pdfPageBudget({ ctxSize, modelName }) {
|
|
3059
|
+
const custom = await this._customModel(modelName);
|
|
3060
|
+
const window = effectiveCtxSize({ ctxSize, modelName }, { appCtxSize: 0, platform: this._platform, custom });
|
|
3061
|
+
return pdfPageBudget(window);
|
|
3062
|
+
}
|
|
3063
|
+
async _customModel(name) {
|
|
3064
|
+
for await (const list of this._engine.customModelsWatch({})) {
|
|
3065
|
+
return list.models?.find((row) => row.name === name) ?? null;
|
|
3066
|
+
}
|
|
3067
|
+
return null;
|
|
3068
|
+
}
|
|
3069
|
+
async _hydrateTurnPdfs(turn, { dir, pinned, signal, maxPages }) {
|
|
3070
|
+
const shown = [];
|
|
3071
|
+
const notices = [];
|
|
3072
|
+
let budget = maxPages;
|
|
3073
|
+
for (const pdf of turn.pdfs ?? []) {
|
|
3074
|
+
if (budget <= 0) {
|
|
3075
|
+
notices.push(pdfPagesSkippedNotice(pdf.fileName));
|
|
3076
|
+
continue;
|
|
3077
|
+
}
|
|
3078
|
+
const pages = await this._tryHydratePdf(dir, turn, { pdf, pinned, maxPages: budget, signal });
|
|
3079
|
+
budget -= pages.tried;
|
|
3080
|
+
if (pages.names.length) {
|
|
3081
|
+
shown.push(pdf.id);
|
|
3082
|
+
}
|
|
3083
|
+
if (pages.cut) {
|
|
3084
|
+
notices.push(pdfPagesCutNotice({ fileName: pdf.fileName, ...pages.cut }));
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
delete turn.pdfs;
|
|
3088
|
+
return { shown, notices };
|
|
2581
3089
|
}
|
|
2582
3090
|
async _tryHydratePdf(dir, turn, opts) {
|
|
2583
3091
|
try {
|
|
@@ -2627,8 +3135,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2627
3135
|
try {
|
|
2628
3136
|
const names = [];
|
|
2629
3137
|
let tried = 0;
|
|
2630
|
-
|
|
2631
|
-
|
|
3138
|
+
let page = 0;
|
|
3139
|
+
const pageCount = doc.pageCount();
|
|
3140
|
+
for (; page < pageCount && tried < maxPages; page++) {
|
|
2632
3141
|
if (signal.aborted)
|
|
2633
3142
|
throw new Error('hydration aborted');
|
|
2634
3143
|
const outcome = await this._renderPdfPage(doc, { dir, pdf, page, pinned, signal });
|
|
@@ -2639,7 +3148,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2639
3148
|
names.push(outcome.name);
|
|
2640
3149
|
}
|
|
2641
3150
|
}
|
|
2642
|
-
return { names, tried };
|
|
3151
|
+
return { names, tried, cut: pdfCut(doc, page) };
|
|
2643
3152
|
}
|
|
2644
3153
|
finally {
|
|
2645
3154
|
doc.close();
|
|
@@ -2655,6 +3164,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2655
3164
|
tried = true;
|
|
2656
3165
|
const name = pdfPageCacheName(pdf, page);
|
|
2657
3166
|
pinned.add(name);
|
|
3167
|
+
this._mediaRefs.set(name, { id: pdf.id, page });
|
|
2658
3168
|
const cached = await cacheOrTouch(path.join(dir, name), () => doc.renderPage(page));
|
|
2659
3169
|
return { tried, name: cached ? name : undefined };
|
|
2660
3170
|
}
|
|
@@ -2667,7 +3177,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2667
3177
|
}
|
|
2668
3178
|
}
|
|
2669
3179
|
async _compactIfNearFull(params) {
|
|
2670
|
-
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
3180
|
+
const { chatId, transcript, trigger, signal, meshId, run, rounds } = params;
|
|
2671
3181
|
const metrics = await this._lastRunMetrics(chatId, meshId);
|
|
2672
3182
|
if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize)) {
|
|
2673
3183
|
return false;
|
|
@@ -2685,11 +3195,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2685
3195
|
trigger,
|
|
2686
3196
|
signal,
|
|
2687
3197
|
occupancy: metrics,
|
|
2688
|
-
meshId
|
|
3198
|
+
meshId,
|
|
3199
|
+
run,
|
|
3200
|
+
rounds
|
|
2689
3201
|
});
|
|
2690
3202
|
}
|
|
2691
3203
|
async _runCompaction(params) {
|
|
2692
|
-
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
3204
|
+
const { chatId, transcript, trigger, signal, meshId, run, rounds } = params;
|
|
2693
3205
|
let statusId = null;
|
|
2694
3206
|
const started = Date.now();
|
|
2695
3207
|
try {
|
|
@@ -2702,7 +3214,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2702
3214
|
statusId = status.id;
|
|
2703
3215
|
},
|
|
2704
3216
|
meshId,
|
|
2705
|
-
signal
|
|
3217
|
+
signal,
|
|
3218
|
+
run,
|
|
3219
|
+
rounds
|
|
2706
3220
|
});
|
|
2707
3221
|
this._log?.info('runs', 'compaction', {
|
|
2708
3222
|
chatId,
|
|
@@ -2727,6 +3241,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2727
3241
|
}
|
|
2728
3242
|
}
|
|
2729
3243
|
}
|
|
3244
|
+
async _contextReading({ chatId, meshId, folded }, ctxSize) {
|
|
3245
|
+
const last = folded ? {} : await this._lastRunMetrics(chatId, meshId);
|
|
3246
|
+
const window = ctxSize || last.ctxSize;
|
|
3247
|
+
if (!window) {
|
|
3248
|
+
return {};
|
|
3249
|
+
}
|
|
3250
|
+
return last.ctxSize === window
|
|
3251
|
+
? { ctxSize: window, cacheTokens: last.cacheTokens }
|
|
3252
|
+
: { ctxSize: window };
|
|
3253
|
+
}
|
|
2730
3254
|
async _lastRunMetrics(chatId, meshId) {
|
|
2731
3255
|
const deviceId = this._requireDeviceId();
|
|
2732
3256
|
const { chunks } = await meshScoped(this._engine, meshId)
|
|
@@ -2760,7 +3284,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2760
3284
|
return { compacted: false, reason: 'unknown chat' };
|
|
2761
3285
|
}
|
|
2762
3286
|
const agent = await engine.getAgent({ agentId: chat.agentId }).catch(noop);
|
|
2763
|
-
if (!agent || !
|
|
3287
|
+
if (!agent || !(options.run || b4a.equals(agent.providerDeviceId, deviceId))) {
|
|
2764
3288
|
return { compacted: false, reason: 'not the provider device' };
|
|
2765
3289
|
}
|
|
2766
3290
|
const model = runnableModelName(agent.modelName, await this._runnableHost());
|
|
@@ -2769,18 +3293,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2769
3293
|
if (ctxSize !== undefined && !canCompact(ctxSize)) {
|
|
2770
3294
|
return { compacted: false, reason: 'below-floor' };
|
|
2771
3295
|
}
|
|
2772
|
-
const
|
|
3296
|
+
const all = await listHistoryChunks(engine, chatId);
|
|
3297
|
+
const stored = options.run ? foldableView(all, options.run) : all;
|
|
2773
3298
|
const branches = historyBranches(stored);
|
|
2774
3299
|
const chunks = stored.filter((chunk) => isPromptChunk(chunk, branches));
|
|
2775
3300
|
const { chunks: priorAsks } = await engine
|
|
2776
3301
|
.listChunks({ chatId, type: 'approval-request' })
|
|
2777
3302
|
.catch(() => ({ chunks: [] }));
|
|
2778
3303
|
const lastThroughId = lastCompactedThroughId(chunks);
|
|
2779
|
-
const lastThroughSeq =
|
|
3304
|
+
const lastThroughSeq = all.find((chunk) => chunk.id === lastThroughId)?.seq ?? -1;
|
|
2780
3305
|
const userTurns = chunks.filter((chunk) => chunk.userMessage).length;
|
|
2781
3306
|
let announced = false;
|
|
2782
3307
|
let keepTurns = COMPACTION_KEEP_TURNS;
|
|
2783
|
-
const senders = await this._senderNames(engine, chunks);
|
|
2784
3308
|
for (;;) {
|
|
2785
3309
|
const boundary = compactionBoundary(chunks, keepTurns);
|
|
2786
3310
|
if (!boundary) {
|
|
@@ -2794,7 +3318,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2794
3318
|
return { compacted: false, reason: 'already-compacted' };
|
|
2795
3319
|
}
|
|
2796
3320
|
const inRange = chunks.filter((chunk) => chunk.seq <= through.seq);
|
|
2797
|
-
const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], '')
|
|
3321
|
+
const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''));
|
|
2798
3322
|
if (!announced) {
|
|
2799
3323
|
await options.onFold?.();
|
|
2800
3324
|
announced = true;
|
|
@@ -2802,7 +3326,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2802
3326
|
const attempt = await this._summarize(folded, {
|
|
2803
3327
|
model,
|
|
2804
3328
|
agent: withConfigDefaults(agent, config),
|
|
2805
|
-
signal: options.signal
|
|
3329
|
+
signal: options.signal,
|
|
3330
|
+
rounds: options.rounds
|
|
2806
3331
|
});
|
|
2807
3332
|
if (!attempt.summary) {
|
|
2808
3333
|
if (attempt.overflowed && boundary.foldedTurns > 1) {
|
|
@@ -2833,9 +3358,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2833
3358
|
return { compacted: true, foldedTurns: boundary.foldedTurns };
|
|
2834
3359
|
}
|
|
2835
3360
|
}
|
|
2836
|
-
async _summarize(turns, { model, agent, signal }) {
|
|
3361
|
+
async _summarize(turns, { model, agent, signal, rounds }) {
|
|
2837
3362
|
const hasPriorSummary = turns[0]?.content.startsWith(COMPACTION_TURN_PREFIX) === true;
|
|
2838
|
-
const
|
|
3363
|
+
const prompt = buildCompactionPrompt(nameSenders(turns).map((turn) => ({ role: turn.role, content: turn.content })), hasPriorSummary);
|
|
3364
|
+
const round = this._probeRounds(rounds);
|
|
3365
|
+
if (round) {
|
|
3366
|
+
return this._summarizeInRound(round, { prompt, model, signal });
|
|
3367
|
+
}
|
|
3368
|
+
const events = this._harness.completion({
|
|
2839
3369
|
model,
|
|
2840
3370
|
agent: {
|
|
2841
3371
|
...agent,
|
|
@@ -2844,12 +3374,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2844
3374
|
skillsScope: 'selected',
|
|
2845
3375
|
enabledSkills: []
|
|
2846
3376
|
},
|
|
2847
|
-
messages: [
|
|
2848
|
-
{
|
|
2849
|
-
role: 'user',
|
|
2850
|
-
content: buildCompactionPrompt(nameSenders(turns).map((turn) => ({ role: turn.role, content: turn.content })), hasPriorSummary)
|
|
2851
|
-
}
|
|
2852
|
-
],
|
|
3377
|
+
messages: [{ role: 'user', content: prompt }],
|
|
2853
3378
|
generationParams: { reasoning_budget: 0 },
|
|
2854
3379
|
signal: signal ?? this._lifecycle.signal
|
|
2855
3380
|
});
|
|
@@ -2877,7 +3402,23 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2877
3402
|
}
|
|
2878
3403
|
return { summary: parseCompactionSummary(text), overflowed: false };
|
|
2879
3404
|
}
|
|
2880
|
-
async
|
|
3405
|
+
async _summarizeInRound(rounds, { prompt, model, signal }) {
|
|
3406
|
+
try {
|
|
3407
|
+
const text = await rounds.probe({
|
|
3408
|
+
kind: 'fold',
|
|
3409
|
+
prompt,
|
|
3410
|
+
modelId: model,
|
|
3411
|
+
signal: signal ?? this._lifecycle.signal
|
|
3412
|
+
});
|
|
3413
|
+
return { summary: parseCompactionSummary(text), overflowed: false };
|
|
3414
|
+
}
|
|
3415
|
+
catch (err) {
|
|
3416
|
+
const failed = errorMessage(err);
|
|
3417
|
+
this._log?.warn('runs', 'summarizer round failed', { failed });
|
|
3418
|
+
return { summary: null, overflowed: isContextOverflowFailure(failed) };
|
|
3419
|
+
}
|
|
3420
|
+
}
|
|
3421
|
+
async _autoTitle(engine, { chatId, messages, model, agent, probe, rounds }) {
|
|
2881
3422
|
try {
|
|
2882
3423
|
const first = messages.find((turn) => turn.role === 'user' && turn.content.trim());
|
|
2883
3424
|
if (!first) {
|
|
@@ -2887,14 +3428,70 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2887
3428
|
if (!isDefaultChatTitle(chat?.title)) {
|
|
2888
3429
|
return;
|
|
2889
3430
|
}
|
|
2890
|
-
await this._generateChatTitle({
|
|
3431
|
+
await this._generateChatTitle({
|
|
3432
|
+
engine,
|
|
3433
|
+
chatId,
|
|
3434
|
+
input: first.content,
|
|
3435
|
+
model,
|
|
3436
|
+
agent,
|
|
3437
|
+
probe,
|
|
3438
|
+
rounds
|
|
3439
|
+
});
|
|
2891
3440
|
}
|
|
2892
3441
|
catch (err) {
|
|
2893
3442
|
this._log?.warn('runs', 'auto-title skipped', { chatId, err });
|
|
2894
3443
|
}
|
|
2895
3444
|
}
|
|
3445
|
+
_probeRounds(rounds) {
|
|
3446
|
+
return this._roundHost ? rounds : undefined;
|
|
3447
|
+
}
|
|
3448
|
+
async _suggestNext(engine, { chatId, runId, messages, answer, model, agent, rounds }) {
|
|
3449
|
+
const question = messages.findLast((turn) => turn.role === 'user' && turn.content.trim());
|
|
3450
|
+
if (!question || this.closing || !(await this._suggestingPrompts())) {
|
|
3451
|
+
return;
|
|
3452
|
+
}
|
|
3453
|
+
const { chunks } = await engine.listChunks({ chatId, type: 'run-request' });
|
|
3454
|
+
if ((chunks ?? []).at(-1)?.runId !== runId) {
|
|
3455
|
+
return;
|
|
3456
|
+
}
|
|
3457
|
+
const controller = new AbortController();
|
|
3458
|
+
this._suggesting.get(chatId)?.abort(SUGGESTION_SUPERSEDED);
|
|
3459
|
+
this._suggesting.set(chatId, controller);
|
|
3460
|
+
try {
|
|
3461
|
+
const raw = await this._probeCompletion({
|
|
3462
|
+
chatId,
|
|
3463
|
+
input: buildSuggestionPrompt(question.content, answer),
|
|
3464
|
+
model,
|
|
3465
|
+
agent,
|
|
3466
|
+
signal: controller.signal,
|
|
3467
|
+
rounds
|
|
3468
|
+
}, SUGGESTION_PROBE);
|
|
3469
|
+
const text = parseSuggestionResponse(raw);
|
|
3470
|
+
if (text && !controller.signal.aborted) {
|
|
3471
|
+
await engine.setChatSuggestion({ chatId, suggestion: { text, runId } });
|
|
3472
|
+
}
|
|
3473
|
+
}
|
|
3474
|
+
catch (err) {
|
|
3475
|
+
this._log?.warn('runs', 'next-message suggestion skipped', { chatId, err });
|
|
3476
|
+
}
|
|
3477
|
+
finally {
|
|
3478
|
+
if (this._suggesting.get(chatId) === controller) {
|
|
3479
|
+
this._suggesting.delete(chatId);
|
|
3480
|
+
}
|
|
3481
|
+
}
|
|
3482
|
+
}
|
|
3483
|
+
async _suggestingPrompts() {
|
|
3484
|
+
try {
|
|
3485
|
+
const { value } = await this._engine.configGet({ key: SUGGEST_PROMPTS_KEY });
|
|
3486
|
+
return value === SUGGEST_PROMPTS_ON;
|
|
3487
|
+
}
|
|
3488
|
+
catch (err) {
|
|
3489
|
+
this._log?.warn('assistant', 'suggestion setting unreadable', { err });
|
|
3490
|
+
return false;
|
|
3491
|
+
}
|
|
3492
|
+
}
|
|
2896
3493
|
async _generateChatTitle(params) {
|
|
2897
|
-
const { engine, chatId, input, model, agent, probe } = params;
|
|
3494
|
+
const { engine, chatId, input, model, agent, probe, rounds } = params;
|
|
2898
3495
|
const prompt = input.trim();
|
|
2899
3496
|
if (!prompt) {
|
|
2900
3497
|
return null;
|
|
@@ -2907,7 +3504,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2907
3504
|
input: prompt,
|
|
2908
3505
|
model,
|
|
2909
3506
|
agent,
|
|
2910
|
-
signal: this._lifecycle.signal
|
|
3507
|
+
signal: this._lifecycle.signal,
|
|
3508
|
+
rounds
|
|
2911
3509
|
});
|
|
2912
3510
|
}
|
|
2913
3511
|
catch (err) {
|
|
@@ -2927,8 +3525,29 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2927
3525
|
return title;
|
|
2928
3526
|
}
|
|
2929
3527
|
async _titleFromCompletion(params) {
|
|
2930
|
-
const
|
|
2931
|
-
|
|
3528
|
+
const prompt = buildTitlePrompt(params.input);
|
|
3529
|
+
return parseTitleResponse(await this._probeCompletion({ ...params, input: prompt }, TITLE_PROBE));
|
|
3530
|
+
}
|
|
3531
|
+
async _probeCompletion(params, { kind, field, name, runId }) {
|
|
3532
|
+
const { chatId, input, model, agent, signal, rounds } = params;
|
|
3533
|
+
const responseFormat = {
|
|
3534
|
+
type: 'json_schema',
|
|
3535
|
+
json_schema: {
|
|
3536
|
+
name,
|
|
3537
|
+
schema: {
|
|
3538
|
+
type: 'object',
|
|
3539
|
+
properties: { [field]: { type: 'string' } },
|
|
3540
|
+
required: [field],
|
|
3541
|
+
additionalProperties: false
|
|
3542
|
+
}
|
|
3543
|
+
}
|
|
3544
|
+
};
|
|
3545
|
+
const round = this._probeRounds(rounds);
|
|
3546
|
+
if (round) {
|
|
3547
|
+
const probed = { kind, prompt: input, modelId: model, responseFormat };
|
|
3548
|
+
return round.probe({ ...probed, signal: signal ?? this._lifecycle.signal });
|
|
3549
|
+
}
|
|
3550
|
+
const events = this._harness.completion({
|
|
2932
3551
|
model,
|
|
2933
3552
|
agent: {
|
|
2934
3553
|
...agent,
|
|
@@ -2937,21 +3556,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2937
3556
|
skillsScope: 'selected',
|
|
2938
3557
|
enabledSkills: []
|
|
2939
3558
|
},
|
|
2940
|
-
messages: [{ role: 'user', content:
|
|
3559
|
+
messages: [{ role: 'user', content: input }],
|
|
2941
3560
|
generationParams: { reasoning_budget: 0 },
|
|
2942
3561
|
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
|
-
},
|
|
3562
|
+
responseFormat,
|
|
2955
3563
|
signal
|
|
2956
3564
|
});
|
|
2957
3565
|
let text = '';
|
|
@@ -2961,14 +3569,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2961
3569
|
}
|
|
2962
3570
|
else if (event.type === 'inference-call') {
|
|
2963
3571
|
void this._inferenceCalls
|
|
2964
|
-
.record(chatId, { runId
|
|
2965
|
-
.catch((err) => this._log?.warn('assistant', '
|
|
3572
|
+
.record(chatId, { runId, at: Date.now(), call: asRecord(event.call) })
|
|
3573
|
+
.catch((err) => this._log?.warn('assistant', 'probe call not recorded', { runId, err }));
|
|
2966
3574
|
}
|
|
2967
3575
|
else if (event.type === 'error' || event.type === 'aborted' || event.type === 'done') {
|
|
2968
3576
|
break;
|
|
2969
3577
|
}
|
|
2970
3578
|
}
|
|
2971
|
-
return
|
|
3579
|
+
return text;
|
|
2972
3580
|
}
|
|
2973
3581
|
_createApprove(engine, { chatId, runId, deviceId, agentId, hasRun }) {
|
|
2974
3582
|
const waiters = new Map();
|
|
@@ -3042,14 +3650,6 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3042
3650
|
if (!ask || ask.name !== call.name) {
|
|
3043
3651
|
return false;
|
|
3044
3652
|
}
|
|
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
3653
|
return ((ask.resource?.kind ?? null) === (call.resource?.kind ?? null) &&
|
|
3054
3654
|
(ask.resource?.label ?? null) === (call.resource?.label ?? null) &&
|
|
3055
3655
|
JSON.stringify(ask.arguments) === JSON.stringify(call.arguments));
|
|
@@ -3095,12 +3695,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3095
3695
|
});
|
|
3096
3696
|
return park(prior.id, call.name);
|
|
3097
3697
|
}
|
|
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()) {
|
|
3698
|
+
if (!hasRun()) {
|
|
3104
3699
|
this._log?.info('approvals', `answer ${status} (from before the app closed)`, {
|
|
3105
3700
|
runId,
|
|
3106
3701
|
chatId,
|
|
@@ -3109,7 +3704,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3109
3704
|
});
|
|
3110
3705
|
return status === 'approved';
|
|
3111
3706
|
}
|
|
3112
|
-
this._log?.info('approvals', 'asking again:
|
|
3707
|
+
this._log?.info('approvals', 'asking again: the replay would repeat a call that ran', {
|
|
3113
3708
|
runId,
|
|
3114
3709
|
chatId,
|
|
3115
3710
|
priorChunkId: prior.id,
|
|
@@ -3123,13 +3718,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3123
3718
|
resource: call.resource ?? undefined,
|
|
3124
3719
|
agentId
|
|
3125
3720
|
};
|
|
3126
|
-
const identity = callIdentity(call);
|
|
3127
3721
|
const chunk = await engine.appendChunk({
|
|
3128
3722
|
chatId,
|
|
3129
3723
|
runId,
|
|
3130
3724
|
type: 'approval-request',
|
|
3131
|
-
callId: identity.callId,
|
|
3132
|
-
round: identity.round,
|
|
3133
3725
|
approvalRequest,
|
|
3134
3726
|
originDeviceId: deviceId,
|
|
3135
3727
|
effect: call.effect === 'read' ? 'read' : 'write',
|
|
@@ -3176,7 +3768,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3176
3768
|
if (known) {
|
|
3177
3769
|
return known;
|
|
3178
3770
|
}
|
|
3179
|
-
if (this.
|
|
3771
|
+
if (this._backgrounded) {
|
|
3772
|
+
this._backgroundReplays.add(runId);
|
|
3180
3773
|
return { kind: 'replay' };
|
|
3181
3774
|
}
|
|
3182
3775
|
if (reason !== undefined) {
|
|
@@ -3210,20 +3803,33 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3210
3803
|
}
|
|
3211
3804
|
};
|
|
3212
3805
|
}
|
|
3213
|
-
_watchOwnership(engine, agentId, controller) {
|
|
3806
|
+
_watchOwnership(engine, { request, agentId, controller }) {
|
|
3214
3807
|
const deviceId = this._requireDeviceId();
|
|
3215
|
-
const
|
|
3808
|
+
const watches = [
|
|
3809
|
+
this._abortOnLoss(engine.agentsWatch({}), controller, ({ agents }) => {
|
|
3810
|
+
const row = (agents ?? []).find((a) => a.id === agentId);
|
|
3811
|
+
return !row || !!row.deletedAt || !drivenBy({ request, agent: row }, deviceId);
|
|
3812
|
+
})
|
|
3813
|
+
];
|
|
3814
|
+
if (request.runnerDeviceId) {
|
|
3815
|
+
watches.push(this._abortOnLoss(engine.runRequestsWatch({}), controller, ({ chunks }) => {
|
|
3816
|
+
const now = (chunks ?? []).find((chunk) => chunk.id === request.id);
|
|
3817
|
+
return !now?.runTakenBy || !b4a.equals(now.runTakenBy, deviceId);
|
|
3818
|
+
}));
|
|
3819
|
+
}
|
|
3820
|
+
return {
|
|
3821
|
+
stop: () => watches.forEach((stop) => stop())
|
|
3822
|
+
};
|
|
3823
|
+
}
|
|
3824
|
+
_abortOnLoss(frames, controller, lost) {
|
|
3825
|
+
const it = frames[Symbol.asyncIterator]();
|
|
3216
3826
|
const watching = (async () => {
|
|
3217
3827
|
while (true) {
|
|
3218
3828
|
const { value, done } = await it.next();
|
|
3219
3829
|
if (done) {
|
|
3220
3830
|
return;
|
|
3221
3831
|
}
|
|
3222
|
-
|
|
3223
|
-
if (!row ||
|
|
3224
|
-
row.deletedAt ||
|
|
3225
|
-
!row.providerDeviceId ||
|
|
3226
|
-
!b4a.equals(row.providerDeviceId, deviceId)) {
|
|
3832
|
+
if (lost(value)) {
|
|
3227
3833
|
controller.abort(OWNERSHIP_LOST);
|
|
3228
3834
|
return;
|
|
3229
3835
|
}
|
|
@@ -3234,14 +3840,35 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3234
3840
|
return;
|
|
3235
3841
|
this._log?.error('assistant', 'ownership watch failed', err);
|
|
3236
3842
|
});
|
|
3237
|
-
return {
|
|
3238
|
-
|
|
3239
|
-
it.return?.()?.catch?.(noop);
|
|
3240
|
-
}
|
|
3843
|
+
return () => {
|
|
3844
|
+
it.return?.()?.catch?.(noop);
|
|
3241
3845
|
};
|
|
3242
3846
|
}
|
|
3243
|
-
|
|
3847
|
+
_runsChanged() {
|
|
3244
3848
|
this._downloads?.yieldToRuns([...this._runs.values()].filter((run) => run.executing).map((run) => run.model));
|
|
3849
|
+
if (this._generating()) {
|
|
3850
|
+
return;
|
|
3851
|
+
}
|
|
3852
|
+
for (const wake of [...this._idleWaiters]) {
|
|
3853
|
+
wake();
|
|
3854
|
+
}
|
|
3855
|
+
}
|
|
3856
|
+
_generating() {
|
|
3857
|
+
return [...this._runs.values()].some((run) => run.executing);
|
|
3858
|
+
}
|
|
3859
|
+
idle(signal) {
|
|
3860
|
+
if (signal.aborted || !this._generating()) {
|
|
3861
|
+
return Promise.resolve();
|
|
3862
|
+
}
|
|
3863
|
+
return new Promise((resolve) => {
|
|
3864
|
+
const wake = () => {
|
|
3865
|
+
this._idleWaiters.delete(wake);
|
|
3866
|
+
signal.removeEventListener('abort', wake);
|
|
3867
|
+
resolve();
|
|
3868
|
+
};
|
|
3869
|
+
this._idleWaiters.add(wake);
|
|
3870
|
+
signal.addEventListener('abort', wake, { once: true });
|
|
3871
|
+
});
|
|
3245
3872
|
}
|
|
3246
3873
|
_logModelStatus(state) {
|
|
3247
3874
|
if (typeof state !== 'object' || state === null)
|
|
@@ -3293,6 +3920,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3293
3920
|
if (state.status === 'loading') {
|
|
3294
3921
|
apply({ phase: 'loading', model: state.model });
|
|
3295
3922
|
}
|
|
3923
|
+
else if (state.status === 'held') {
|
|
3924
|
+
apply({ phase: 'awaiting-download', model: state.model });
|
|
3925
|
+
}
|
|
3296
3926
|
else if (state.status === 'downloading') {
|
|
3297
3927
|
const percentage = Math.floor((state.percentage ?? 0) / 10) * 10;
|
|
3298
3928
|
apply({ phase: 'downloading', model: state.model, percentage });
|
|
@@ -3333,6 +3963,33 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3333
3963
|
};
|
|
3334
3964
|
}
|
|
3335
3965
|
}
|
|
3966
|
+
function notReady(search) {
|
|
3967
|
+
return {
|
|
3968
|
+
chatFilesNotReady: search.chatFilesNotReady ?? 0,
|
|
3969
|
+
projectFilesNotReady: search.projectFilesNotReady ?? 0
|
|
3970
|
+
};
|
|
3971
|
+
}
|
|
3972
|
+
function notReadyCount(search) {
|
|
3973
|
+
const { chatFilesNotReady, projectFilesNotReady } = notReady(search);
|
|
3974
|
+
return chatFilesNotReady + projectFilesNotReady;
|
|
3975
|
+
}
|
|
3976
|
+
function notReadyLine(search) {
|
|
3977
|
+
const { projectFilesNotReady } = notReady(search);
|
|
3978
|
+
const chat = search.pendingFiles ?? [];
|
|
3979
|
+
const parts = [];
|
|
3980
|
+
if (chat.length) {
|
|
3981
|
+
parts.push(`${filesCount(chat.length, 'chat')} (${chat.join(', ')})`);
|
|
3982
|
+
}
|
|
3983
|
+
if (projectFilesNotReady) {
|
|
3984
|
+
parts.push(filesCount(projectFilesNotReady, 'project'));
|
|
3985
|
+
}
|
|
3986
|
+
return parts.length
|
|
3987
|
+
? `[Still indexing and not used for this reply: ${parts.join(' and ')}.]`
|
|
3988
|
+
: null;
|
|
3989
|
+
}
|
|
3990
|
+
function filesCount(count, source) {
|
|
3991
|
+
return `${count} ${source} file${count === 1 ? '' : 's'}`;
|
|
3992
|
+
}
|
|
3336
3993
|
async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, injected }) {
|
|
3337
3994
|
const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
|
|
3338
3995
|
const notice = chooseRagNotice({
|
|
@@ -3340,7 +3997,7 @@ async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter
|
|
|
3340
3997
|
hitCount: records.length,
|
|
3341
3998
|
searchedCount: knowledge.search.searchedCount ?? 0,
|
|
3342
3999
|
indexEmpty: Boolean(knowledge.search.indexEmpty),
|
|
3343
|
-
hasKnowledge:
|
|
4000
|
+
hasKnowledge: !!knowledge.search.hasKnowledge || notReadyCount(knowledge.search) > 0,
|
|
3344
4001
|
pendingReferenced: Boolean(knowledge.search.pendingReferenced),
|
|
3345
4002
|
failedFiles: knowledge.search.failedFiles ?? [],
|
|
3346
4003
|
pendingFiles: knowledge.search.pendingFiles ?? []
|
|
@@ -3352,7 +4009,7 @@ async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter
|
|
|
3352
4009
|
})));
|
|
3353
4010
|
await transcript.chunk({
|
|
3354
4011
|
type: 'sources',
|
|
3355
|
-
sources: { sources: records, notice: notice ?? undefined }
|
|
4012
|
+
sources: { sources: records, notice: notice ?? undefined, ...notReady(knowledge.search) }
|
|
3356
4013
|
});
|
|
3357
4014
|
injected.sourcesWritten = true;
|
|
3358
4015
|
if (records.length > 0) {
|
|
@@ -3369,21 +4026,32 @@ function toolCallChunk(event) {
|
|
|
3369
4026
|
return {
|
|
3370
4027
|
type: 'tool-call',
|
|
3371
4028
|
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
4029
|
effect: event.effect === 'read' ? 'read' : 'write',
|
|
3375
4030
|
display: event.display,
|
|
3376
|
-
rawCall: typeof event.rawCall === 'string' ? event.rawCall : undefined
|
|
4031
|
+
rawCall: typeof event.rawCall === 'string' ? event.rawCall : undefined,
|
|
4032
|
+
...callOf(event)
|
|
3377
4033
|
};
|
|
3378
4034
|
}
|
|
3379
4035
|
function toolResultChunk(event) {
|
|
3380
4036
|
return {
|
|
3381
4037
|
type: 'tool-result',
|
|
3382
4038
|
toolResult: { name: event.name, result: withoutDataChannel(event.result) },
|
|
3383
|
-
|
|
4039
|
+
...callOf(event)
|
|
4040
|
+
};
|
|
4041
|
+
}
|
|
4042
|
+
function roundOf(event) {
|
|
4043
|
+
return typeof event.round === 'number' ? event.round : undefined;
|
|
4044
|
+
}
|
|
4045
|
+
function callOf(event) {
|
|
4046
|
+
return {
|
|
4047
|
+
round: roundOf(event),
|
|
3384
4048
|
callId: typeof event.callId === 'string' ? event.callId : undefined
|
|
3385
4049
|
};
|
|
3386
4050
|
}
|
|
4051
|
+
function replacedTypes(event) {
|
|
4052
|
+
const types = Array.isArray(event.replacedTypes) ? event.replacedTypes : ['content', 'thinking'];
|
|
4053
|
+
return types.filter((type) => type === 'content' || type === 'thinking');
|
|
4054
|
+
}
|
|
3387
4055
|
function runMetricsSummary(metrics) {
|
|
3388
4056
|
const { timeToFirstToken, promptTokens, promptTokensPerSecond, cacheTokens, tokensPerSecond, totalTokens, durationMs, stopReason } = metrics;
|
|
3389
4057
|
return {
|
|
@@ -3400,85 +4068,6 @@ function runMetricsSummary(metrics) {
|
|
|
3400
4068
|
function asRecord(value) {
|
|
3401
4069
|
return isRecord(value) ? { ...value } : {};
|
|
3402
4070
|
}
|
|
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
4071
|
function isStreamDestroyed(err) {
|
|
3483
4072
|
return isRecord(err) && err.code === 'STREAM_DESTROYED';
|
|
3484
4073
|
}
|