@qvac/core 1.0.0-rc.1 → 1.0.0-rc.11
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 +1205 -1
- package/README.md +56 -45
- package/dist-lib/index.d.ts +3 -2
- package/dist-lib/index.js +1 -1
- package/dist-lib/lib/api/agent-folders.d.ts +22 -0
- package/dist-lib/lib/api/agent-folders.js +115 -0
- package/dist-lib/lib/api/agents.d.ts +20 -2
- package/dist-lib/lib/api/agents.js +128 -24
- package/dist-lib/lib/api/app-config.d.ts +7 -1
- package/dist-lib/lib/api/app-config.js +37 -6
- package/dist-lib/lib/api/approval-rules.d.ts +1 -1
- package/dist-lib/lib/api/approval-rules.js +12 -5
- package/dist-lib/lib/api/attachments.d.ts +1 -1
- package/dist-lib/lib/api/attachments.js +14 -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 +12 -2
- package/dist-lib/lib/api/chat.js +100 -9
- package/dist-lib/lib/api/chunk-delta.d.ts +12 -0
- package/dist-lib/lib/api/chunk-delta.js +115 -0
- package/dist-lib/lib/api/chunks.d.ts +19 -2
- package/dist-lib/lib/api/chunks.js +73 -13
- package/dist-lib/lib/api/config.d.ts +11 -3
- package/dist-lib/lib/api/config.js +51 -5
- package/dist-lib/lib/api/current-mesh.d.ts +13 -5
- package/dist-lib/lib/api/current-mesh.js +15 -11
- package/dist-lib/lib/api/custom-models.d.ts +7 -1
- package/dist-lib/lib/api/custom-models.js +12 -12
- package/dist-lib/lib/api/device-routes.d.ts +14 -0
- package/dist-lib/lib/api/device-routes.js +144 -0
- package/dist-lib/lib/api/devices.d.ts +3 -2
- package/dist-lib/lib/api/devices.js +28 -28
- 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 +16 -1
- package/dist-lib/lib/api/execution.js +72 -16
- package/dist-lib/lib/api/files.d.ts +1 -1
- package/dist-lib/lib/api/files.js +65 -8
- package/dist-lib/lib/api/host-run-log.d.ts +2 -41
- package/dist-lib/lib/api/host-run-log.js +35 -83
- package/dist-lib/lib/api/index.d.ts +39 -7
- package/dist-lib/lib/api/index.js +116 -36
- package/dist-lib/lib/api/instrument.d.ts +2 -2
- package/dist-lib/lib/api/instrument.js +24 -48
- package/dist-lib/lib/api/knowledge.d.ts +4 -1
- package/dist-lib/lib/api/knowledge.js +25 -7
- package/dist-lib/lib/api/local-files.d.ts +5 -1
- package/dist-lib/lib/api/local-files.js +9 -5
- package/dist-lib/lib/api/logs.js +3 -3
- package/dist-lib/lib/api/members.d.ts +10 -1
- package/dist-lib/lib/api/members.js +69 -44
- package/dist-lib/lib/api/mesh.d.ts +15 -6
- package/dist-lib/lib/api/mesh.js +13 -9
- package/dist-lib/lib/api/model-download.d.ts +2 -1
- package/dist-lib/lib/api/model-download.js +2 -2
- package/dist-lib/lib/api/network.d.ts +10 -2
- package/dist-lib/lib/api/network.js +8 -2
- package/dist-lib/lib/api/notifications.d.ts +3 -1
- package/dist-lib/lib/api/notifications.js +14 -9
- package/dist-lib/lib/api/projects.d.ts +20 -0
- package/dist-lib/lib/api/projects.js +172 -0
- package/dist-lib/lib/api/rag-plugin.d.ts +17 -0
- package/dist-lib/lib/api/rag-plugin.js +44 -0
- package/dist-lib/lib/api/rag.d.ts +0 -1
- package/dist-lib/lib/api/rag.js +106 -33
- package/dist-lib/lib/api/served.d.ts +17 -0
- package/dist-lib/lib/api/served.js +65 -0
- package/dist-lib/lib/api/sync.d.ts +2 -1
- package/dist-lib/lib/api/sync.js +13 -3
- 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 +12 -0
- package/dist-lib/lib/api/workspace.js +61 -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 +48 -31
- package/dist-lib/lib/assistant/harness.js +51 -10
- package/dist-lib/lib/assistant/history.d.ts +37 -17
- package/dist-lib/lib/assistant/history.js +212 -87
- package/dist-lib/lib/assistant/host-run-log.d.ts +6 -1
- package/dist-lib/lib/assistant/host-run-log.js +24 -36
- package/dist-lib/lib/assistant/media-round.d.ts +61 -0
- package/dist-lib/lib/assistant/media-round.js +111 -0
- package/dist-lib/lib/assistant/message.d.ts +28 -1
- package/dist-lib/lib/assistant/message.js +65 -2
- package/dist-lib/lib/assistant/model-round.d.ts +118 -0
- package/dist-lib/lib/assistant/model-round.js +706 -0
- package/dist-lib/lib/assistant/pdf-budget.d.ts +11 -0
- package/dist-lib/lib/assistant/pdf-budget.js +12 -0
- package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
- package/dist-lib/lib/assistant/pdf-pages.js +75 -0
- package/dist-lib/lib/assistant/round-host.d.ts +65 -0
- package/dist-lib/lib/assistant/round-host.js +332 -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/upload.d.ts +12 -0
- package/dist-lib/lib/assistant/upload.js +36 -0
- 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 +5 -1
- package/dist-lib/lib/assistant-client.js +54 -14
- package/dist-lib/lib/assistant-tree.d.ts +14 -3
- package/dist-lib/lib/assistant-tree.js +113 -21
- package/dist-lib/lib/assistant.d.ts +115 -25
- package/dist-lib/lib/assistant.js +2409 -1245
- 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 +3 -2
- package/dist-lib/lib/client.js +18 -3
- package/dist-lib/lib/compaction.d.ts +3 -1
- package/dist-lib/lib/compaction.js +20 -6
- package/dist-lib/lib/constants.d.ts +10 -0
- package/dist-lib/lib/constants.js +10 -0
- package/dist-lib/lib/core/agent-copies.d.ts +9 -0
- package/dist-lib/lib/core/agent-copies.js +47 -0
- package/dist-lib/lib/core/agent-hosting.d.ts +25 -0
- package/dist-lib/lib/core/agent-hosting.js +76 -0
- package/dist-lib/lib/core/agent.d.ts +9 -1
- package/dist-lib/lib/core/agent.js +58 -63
- package/dist-lib/lib/core/approval-rules.d.ts +5 -1
- package/dist-lib/lib/core/approval-rules.js +17 -9
- package/dist-lib/lib/core/archived-mesh.d.ts +14 -7
- package/dist-lib/lib/core/archived-mesh.js +75 -59
- package/dist-lib/lib/core/async-iterable.js +1 -1
- package/dist-lib/lib/core/blind-peers.d.ts +1 -0
- package/dist-lib/lib/core/blind-peers.js +1 -0
- package/dist-lib/lib/core/chat-activity.d.ts +1 -1
- package/dist-lib/lib/core/chat-activity.js +45 -34
- package/dist-lib/lib/core/chat-copy.d.ts +42 -0
- package/dist-lib/lib/core/chat-copy.js +404 -0
- package/dist-lib/lib/core/chat-fork.d.ts +16 -0
- package/dist-lib/lib/core/chat-fork.js +246 -0
- package/dist-lib/lib/core/chat-group.js +10 -22
- package/dist-lib/lib/core/chat-move.d.ts +77 -0
- package/dist-lib/lib/core/chat-move.js +601 -0
- package/dist-lib/lib/core/chat-order.d.ts +2 -1
- package/dist-lib/lib/core/chat-order.js +28 -19
- 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 +32 -4
- package/dist-lib/lib/core/chat.js +135 -61
- package/dist-lib/lib/core/chunk.d.ts +35 -7
- package/dist-lib/lib/core/chunk.js +271 -72
- package/dist-lib/lib/core/collections.d.ts +41 -1
- package/dist-lib/lib/core/collections.js +41 -1
- package/dist-lib/lib/core/config-value.d.ts +7 -0
- package/dist-lib/lib/core/config-value.js +15 -0
- package/dist-lib/lib/core/contract/activity.d.ts +7 -2
- package/dist-lib/lib/core/contract/activity.js +71 -23
- package/dist-lib/lib/core/contract/agent-join.d.ts +10 -0
- package/dist-lib/lib/core/contract/agent-join.js +104 -0
- package/dist-lib/lib/core/contract/agents.d.ts +5 -0
- package/dist-lib/lib/core/contract/agents.js +142 -3
- package/dist-lib/lib/core/contract/chat.d.ts +4 -0
- package/dist-lib/lib/core/contract/chat.js +53 -3
- package/dist-lib/lib/core/contract/chunks.d.ts +7 -0
- package/dist-lib/lib/core/contract/chunks.js +125 -38
- package/dist-lib/lib/core/contract/devices.js +6 -4
- package/dist-lib/lib/core/contract/index.d.ts +2 -3
- package/dist-lib/lib/core/contract/index.js +38 -11
- package/dist-lib/lib/core/contract/indexing.d.ts +1 -0
- package/dist-lib/lib/core/contract/indexing.js +51 -7
- package/dist-lib/lib/core/contract/invites.d.ts +3 -14
- package/dist-lib/lib/core/contract/invites.js +26 -36
- package/dist-lib/lib/core/contract/joined-meshes.js +4 -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 +48 -12
- package/dist-lib/lib/core/contract/mesh-kind.d.ts +3 -3
- package/dist-lib/lib/core/contract/mesh-kind.js +20 -16
- package/dist-lib/lib/core/contract/mesh-name.d.ts +3 -0
- package/dist-lib/lib/core/contract/mesh-name.js +31 -0
- package/dist-lib/lib/core/contract/mesh-rules.d.ts +8 -0
- package/dist-lib/lib/core/contract/mesh-rules.js +35 -0
- package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
- package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
- package/dist-lib/lib/core/contract/model-requests.d.ts +26 -0
- package/dist-lib/lib/core/contract/model-requests.js +280 -0
- package/dist-lib/lib/core/contract/oldest-version.d.ts +9 -0
- package/dist-lib/lib/core/contract/oldest-version.js +20 -0
- package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
- package/dist-lib/lib/core/contract/open-join.js +77 -0
- package/dist-lib/lib/core/contract/project-device-keys.d.ts +9 -0
- package/dist-lib/lib/core/contract/project-device-keys.js +38 -0
- package/dist-lib/lib/core/contract/project-host-keys.d.ts +9 -0
- package/dist-lib/lib/core/contract/project-host-keys.js +52 -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 +23 -0
- package/dist-lib/lib/core/contract/projects.js +339 -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 +123 -0
- package/dist-lib/lib/core/contract/run-requests.d.ts +11 -0
- package/dist-lib/lib/core/contract/run-requests.js +114 -0
- package/dist-lib/lib/core/contract/seed-defaults.d.ts +3 -1
- package/dist-lib/lib/core/contract/seed-defaults.js +17 -4
- package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
- package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
- package/dist-lib/lib/core/contract/skill-secrets.d.ts +3 -0
- package/dist-lib/lib/core/contract/skill-secrets.js +32 -0
- package/dist-lib/lib/core/contract/tool-activity.js +12 -1
- package/dist-lib/lib/core/contract/tool-configs.js +2 -1
- package/dist-lib/lib/core/contract/types.d.ts +2 -2
- package/dist-lib/lib/core/contract/workspace.d.ts +31 -0
- package/dist-lib/lib/core/contract/workspace.js +311 -0
- package/dist-lib/lib/core/contract/writers.js +100 -16
- package/dist-lib/lib/core/deleted-agent-runs.d.ts +8 -0
- package/dist-lib/lib/core/deleted-agent-runs.js +57 -0
- package/dist-lib/lib/core/departures.d.ts +18 -5
- package/dist-lib/lib/core/departures.js +82 -6
- package/dist-lib/lib/core/device-control.d.ts +21 -0
- package/dist-lib/lib/core/device-control.js +91 -0
- package/dist-lib/lib/core/device-seal.d.ts +4 -0
- package/dist-lib/lib/core/device-seal.js +22 -0
- package/dist-lib/lib/core/devices.d.ts +2 -1
- package/dist-lib/lib/core/devices.js +6 -11
- package/dist-lib/lib/core/document-attachment.d.ts +10 -0
- package/dist-lib/lib/core/document-attachment.js +14 -0
- package/dist-lib/lib/core/files/attachments.d.ts +2 -0
- package/dist-lib/lib/core/files/attachments.js +23 -7
- package/dist-lib/lib/core/files/index.d.ts +28 -15
- package/dist-lib/lib/core/files/index.js +222 -43
- package/dist-lib/lib/core/files/knowledge.d.ts +10 -1
- package/dist-lib/lib/core/files/knowledge.js +77 -5
- package/dist-lib/lib/core/files/preview/index.js +6 -2
- package/dist-lib/lib/core/files/processing/claims.d.ts +7 -0
- package/dist-lib/lib/core/files/processing/claims.js +33 -7
- package/dist-lib/lib/core/files/processing/document.d.ts +8 -0
- package/dist-lib/lib/core/files/processing/document.js +26 -2
- package/dist-lib/lib/core/files/processing/index.d.ts +12 -3
- package/dist-lib/lib/core/files/processing/index.js +122 -73
- package/dist-lib/lib/core/files/processing/protocol.d.ts +7 -1
- package/dist-lib/lib/core/files/processing/protocol.js +27 -37
- package/dist-lib/lib/core/files/processing/source.d.ts +4 -0
- package/dist-lib/lib/core/files/processing/source.js +12 -4
- package/dist-lib/lib/core/files/processing/state.d.ts +5 -1
- package/dist-lib/lib/core/files/processing/state.js +9 -1
- package/dist-lib/lib/core/files/rag/build.d.ts +15 -2
- package/dist-lib/lib/core/files/rag/build.js +119 -21
- package/dist-lib/lib/core/files/rag/failure.d.ts +20 -0
- package/dist-lib/lib/core/files/rag/failure.js +58 -0
- package/dist-lib/lib/core/files/rag/index.d.ts +21 -5
- package/dist-lib/lib/core/files/rag/index.js +38 -9
- 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.d.ts +3 -3
- package/dist-lib/lib/core/files/rag/load.js +6 -3
- package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
- package/dist-lib/lib/core/files/rag/plugin-index.js +120 -0
- 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 +3 -4
- package/dist-lib/lib/core/files/rag/produce.js +31 -18
- package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
- package/dist-lib/lib/core/files/rag/retrieval/full-text.js +8 -2
- package/dist-lib/lib/core/files/rag/similarity.d.ts +2 -2
- package/dist-lib/lib/core/files/rag/similarity.js +2 -3
- package/dist-lib/lib/core/files/records/chunks.js +22 -1
- package/dist-lib/lib/core/files/records/index.d.ts +1 -1
- package/dist-lib/lib/core/files/records/index.js +1 -1
- package/dist-lib/lib/core/files/records/knowledge.d.ts +12 -4
- package/dist-lib/lib/core/files/records/knowledge.js +34 -30
- package/dist-lib/lib/core/files/records/purge.js +13 -8
- package/dist-lib/lib/core/files/records/reclaim.js +21 -55
- 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 +117 -51
- package/dist-lib/lib/core/first-event.d.ts +6 -0
- package/dist-lib/lib/core/first-event.js +13 -0
- package/dist-lib/lib/core/fs-drives.d.ts +6 -3
- package/dist-lib/lib/core/fs-drives.js +35 -35
- package/dist-lib/lib/core/helpers/index.d.ts +1 -1
- package/dist-lib/lib/core/helpers/index.js +2 -2
- package/dist-lib/lib/core/id.d.ts +1 -0
- package/dist-lib/lib/core/id.js +3 -0
- package/dist-lib/lib/core/identity.d.ts +3 -1
- package/dist-lib/lib/core/identity.js +25 -4
- package/dist-lib/lib/core/invite.d.ts +6 -2
- package/dist-lib/lib/core/invite.js +22 -8
- package/dist-lib/lib/core/local/files.d.ts +2 -1
- package/dist-lib/lib/core/local/files.js +5 -7
- package/dist-lib/lib/core/local/index.d.ts +93 -8
- package/dist-lib/lib/core/local/index.js +327 -28
- 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 +132 -29
- package/dist-lib/lib/core/mesh.js +775 -215
- package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
- package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
- package/dist-lib/lib/core/migrations/agent-tools-scope.js +18 -0
- package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
- package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
- package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
- package/dist-lib/lib/core/migrations/file-index.js +8 -6
- package/dist-lib/lib/core/migrations/index.d.ts +2 -1
- package/dist-lib/lib/core/migrations/index.js +69 -8
- package/dist-lib/lib/core/migrations/writer-links.d.ts +3 -0
- package/dist-lib/lib/core/migrations/writer-links.js +30 -0
- package/dist-lib/lib/core/model-requests.d.ts +13 -0
- package/dist-lib/lib/core/model-requests.js +65 -0
- package/dist-lib/lib/core/next-frame.js +2 -1
- package/dist-lib/lib/core/notification.js +100 -25
- package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
- package/dist-lib/lib/core/pdf/figure.js +64 -0
- package/dist-lib/lib/core/pdf/rasterize.d.ts +12 -3
- package/dist-lib/lib/core/pdf/rasterize.js +77 -28
- package/dist-lib/lib/core/peer-duplex.d.ts +34 -0
- package/dist-lib/lib/core/{harness-loan.js → peer-duplex.js} +104 -45
- package/dist-lib/lib/core/project-keys.d.ts +16 -0
- package/dist-lib/lib/core/project-keys.js +65 -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 +25 -0
- package/dist-lib/lib/core/public-mesh.js +81 -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 +14 -0
- package/dist-lib/lib/core/run-requests.js +78 -0
- package/dist-lib/lib/core/send.d.ts +2 -0
- package/dist-lib/lib/core/send.js +17 -4
- package/dist-lib/lib/core/senders.d.ts +19 -0
- package/dist-lib/lib/core/senders.js +45 -0
- package/dist-lib/lib/core/shared-agents.d.ts +40 -0
- package/dist-lib/lib/core/shared-agents.js +283 -0
- package/dist-lib/lib/core/skill-availability.d.ts +33 -0
- package/dist-lib/lib/core/skill-availability.js +67 -0
- package/dist-lib/lib/core/skill-secrets.d.ts +13 -18
- package/dist-lib/lib/core/skill-secrets.js +93 -72
- package/dist-lib/lib/core/sync/background.d.ts +34 -0
- package/dist-lib/lib/core/sync/background.js +86 -0
- package/dist-lib/lib/core/sync/changed.d.ts +20 -0
- package/dist-lib/lib/core/sync/changed.js +7 -0
- package/dist-lib/lib/core/sync/devices.js +6 -3
- package/dist-lib/lib/core/sync/files.d.ts +1 -0
- package/dist-lib/lib/core/sync/files.js +50 -72
- package/dist-lib/lib/core/sync/index.d.ts +1 -1
- package/dist-lib/lib/core/sync/index.js +193 -81
- package/dist-lib/lib/core/sync/view-updates.d.ts +39 -0
- package/dist-lib/lib/core/sync/view-updates.js +124 -0
- package/dist-lib/lib/core/sync-status.js +99 -43
- 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-activity.js +6 -12
- package/dist-lib/lib/core/tool-configs.d.ts +5 -3
- package/dist-lib/lib/core/tool-configs.js +27 -39
- 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/upgrade-summary.d.ts +19 -0
- package/dist-lib/lib/core/upgrade-summary.js +67 -0
- package/dist-lib/lib/core/vault-managed-skills.d.ts +18 -0
- package/dist-lib/lib/core/vault-managed-skills.js +105 -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/view-read.d.ts +4 -0
- package/dist-lib/lib/core/view-read.js +23 -0
- package/dist-lib/lib/core/workspace-upgrade.d.ts +23 -0
- package/dist-lib/lib/core/workspace-upgrade.js +51 -0
- package/dist-lib/lib/core.d.ts +345 -88
- package/dist-lib/lib/core.js +4275 -1513
- package/dist-lib/lib/credential-pusher.d.ts +17 -7
- package/dist-lib/lib/credential-pusher.js +111 -62
- package/dist-lib/lib/custom-model-pusher.d.ts +2 -4
- package/dist-lib/lib/custom-model-task.d.ts +2 -1
- package/dist-lib/lib/custom-model-task.js +2 -2
- package/dist-lib/lib/device-tree.d.ts +18 -0
- package/dist-lib/lib/device-tree.js +32 -0
- package/dist-lib/lib/diagnostics.d.ts +7 -0
- package/dist-lib/lib/diagnostics.js +22 -0
- package/dist-lib/lib/doctor/checks.d.ts +15 -0
- package/dist-lib/lib/doctor/checks.js +272 -0
- package/dist-lib/lib/doctor/redact.d.ts +4 -0
- package/dist-lib/lib/doctor/redact.js +31 -0
- package/dist-lib/lib/doctor/render.d.ts +8 -0
- package/dist-lib/lib/doctor/render.js +83 -0
- package/dist-lib/lib/doctor/tree.d.ts +5 -0
- package/dist-lib/lib/doctor/tree.js +167 -0
- package/dist-lib/lib/downloads-allowed-pusher.d.ts +8 -0
- package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
- package/dist-lib/lib/errors.d.ts +814 -0
- package/dist-lib/lib/errors.js +748 -0
- package/dist-lib/lib/fs-drive-bridge.d.ts +8 -1
- package/dist-lib/lib/fs-drive-bridge.js +312 -31
- package/dist-lib/lib/guards.d.ts +1 -0
- package/dist-lib/lib/guards.js +4 -0
- package/dist-lib/lib/inference-calls.d.ts +1 -2
- package/dist-lib/lib/inference-calls.js +2 -10
- package/dist-lib/lib/integrations/setup-routes.js +7 -5
- package/dist-lib/lib/knowledge-search.d.ts +1 -1
- package/dist-lib/lib/knowledge-search.js +5 -51
- package/dist-lib/lib/kv-cache-ledger.d.ts +22 -0
- package/dist-lib/lib/kv-cache-ledger.js +72 -0
- package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
- package/dist-lib/lib/kv-cache-reclaim.js +30 -0
- package/dist-lib/lib/kv-cache-retention.d.ts +36 -0
- package/dist-lib/lib/kv-cache-retention.js +101 -0
- package/dist-lib/lib/latest-frame.d.ts +10 -0
- package/dist-lib/lib/latest-frame.js +48 -0
- package/dist-lib/lib/llm-model-names.d.ts +1 -1
- package/dist-lib/lib/llm-model-names.js +1 -1
- 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 +47 -2
- package/dist-lib/lib/model-context.js +1 -2
- package/dist-lib/lib/model-download-status.d.ts +3 -3
- package/dist-lib/lib/model-download.d.ts +22 -13
- package/dist-lib/lib/model-download.js +106 -37
- package/dist-lib/lib/model-idle-pusher.d.ts +2 -4
- package/dist-lib/lib/model-memory-cap.d.ts +30 -0
- package/dist-lib/lib/model-memory-cap.js +78 -0
- package/dist-lib/lib/models-watch.d.ts +1 -1
- package/dist-lib/lib/models-watch.js +4 -4
- package/dist-lib/lib/oauth/browser-connect.js +3 -2
- package/dist-lib/lib/oauth/flow-core.js +5 -3
- package/dist-lib/lib/oauth/flow.js +1 -1
- package/dist-lib/lib/oauth/loopback.d.ts +1 -0
- package/dist-lib/lib/oauth/loopback.js +57 -12
- package/dist-lib/lib/oauth/providers.js +4 -2
- package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
- package/dist-lib/lib/oauth/register.js +35 -19
- package/dist-lib/lib/oauth/request.d.ts +10 -0
- package/dist-lib/lib/oauth/request.js +22 -0
- package/dist-lib/lib/oauth/resolve.js +42 -13
- package/dist-lib/lib/oauth/token-exchange-core.js +40 -31
- 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 +8 -0
- package/dist-lib/lib/probe-rounds.js +10 -0
- package/dist-lib/lib/reconnecting-watch.js +19 -11
- package/dist-lib/lib/run-input.d.ts +14 -0
- package/dist-lib/lib/run-input.js +33 -0
- package/dist-lib/lib/run-notices.d.ts +5 -0
- package/dist-lib/lib/run-notices.js +13 -2
- package/dist-lib/lib/run-replay.d.ts +45 -0
- package/dist-lib/lib/run-replay.js +169 -0
- package/dist-lib/lib/run-transcript.d.ts +13 -12
- package/dist-lib/lib/run-transcript.js +20 -19
- package/dist-lib/lib/suspend-lifecycle.js +15 -5
- package/dist-lib/lib/task.js +6 -4
- package/dist-lib/lib/test-env.d.ts +1 -0
- package/dist-lib/lib/test-env.js +4 -0
- package/dist-lib/schema/build.js +66 -55
- package/dist-lib/schema/config.d.ts +1 -1
- package/dist-lib/schema/config.js +1 -1
- package/dist-lib/schema/content.d.ts +5 -1
- package/dist-lib/schema/content.js +60 -12
- package/dist-lib/schema/engine.d.ts +1 -0
- package/dist-lib/schema/engine.js +941 -152
- package/dist-lib/schema/field-order.js +8 -1
- package/dist-lib/schema/indexes.d.ts +10 -0
- package/dist-lib/schema/indexes.js +53 -3
- package/dist-lib/schema/local.js +83 -5
- package/dist-lib/schema/qvac.d.ts +4 -1
- package/dist-lib/schema/qvac.js +558 -16
- package/dist-lib/spec/bind.js +261 -48
- package/dist-lib/spec/calls.js +56 -5
- package/dist-lib/spec/capabilities.d.ts +129 -20
- package/dist-lib/spec/config/hyperconf/index.js +1 -1
- package/dist-lib/spec/hrpc/hrpc.json +870 -207
- package/dist-lib/spec/hrpc/index.js +969 -347
- package/dist-lib/spec/hrpc/messages.js +7590 -4896
- package/dist-lib/spec/hyperschema/index.js +7590 -4896
- package/dist-lib/spec/hyperschema/schema.json +5316 -3224
- package/dist-lib/spec/hyperschema/types.d.ts +576 -62
- package/dist-lib/spec/local/hyperdb/db.json +95 -1
- package/dist-lib/spec/local/hyperdb/index.js +459 -2
- package/dist-lib/spec/local/hyperdb/messages.js +1588 -488
- package/dist-lib/spec/local/hyperschema/index.js +584 -16
- package/dist-lib/spec/local/hyperschema/schema.json +369 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +88 -2
- package/dist-lib/spec/mesh/hyperdb/db.json +711 -9
- package/dist-lib/spec/mesh/hyperdb/index.js +2769 -7
- package/dist-lib/spec/mesh/hyperdb/messages.js +7140 -1298
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +205 -1
- package/dist-lib/spec/mesh/hyperdispatch/index.js +579 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +4017 -267
- package/dist-lib/spec/mesh/hyperschema/index.js +4017 -267
- package/dist-lib/spec/mesh/hyperschema/schema.json +2193 -19
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +550 -6
- package/package.json +136 -90
- package/tui/README.md +119 -20
- package/tui/app.mjs +2000 -621
- package/tui/boot.mjs +76 -9
- package/tui/cli.mjs +93 -0
- package/tui/index.mjs +2 -51
- package/tui/lib/connect-sheet.mjs +350 -0
- package/tui/lib/device-target.mjs +64 -0
- package/tui/lib/folders-command.mjs +84 -0
- package/tui/lib/fork-command.mjs +43 -0
- package/tui/lib/knowledge-import.mjs +3 -21
- package/tui/lib/local-path.mjs +21 -0
- package/tui/lib/move-command.mjs +17 -0
- package/tui/lib/render.mjs +30 -1
- package/tui/lib/setup.mjs +59 -25
- package/tui/lib/skill-connect.mjs +145 -0
- package/tui/lib/updater.mjs +86 -0
- package/tui/main.mjs +44 -18
- package/tui/plugins.mjs +158 -0
- package/worker/assistant-sidecar.mjs +28 -4
- package/worker/stow-entry.mjs +3 -50
- package/dist-lib/lib/core/contract/compute.d.ts +0 -11
- package/dist-lib/lib/core/contract/compute.js +0 -48
- package/dist-lib/lib/core/device-invites.d.ts +0 -11
- package/dist-lib/lib/core/device-invites.js +0 -63
- package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
- package/dist-lib/lib/core/files/processing/settle.js +0 -86
- package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
- package/dist-lib/lib/core/grant-expiry.js +0 -66
- package/dist-lib/lib/core/handoff-channel.d.ts +0 -47
- package/dist-lib/lib/core/handoff-channel.js +0 -123
- package/dist-lib/lib/core/harness-loan.d.ts +0 -29
- package/dist-lib/lib/core/host-run-log-store.d.ts +0 -114
- package/dist-lib/lib/core/host-run-log-store.js +0 -976
- package/dist-lib/lib/core/hosted-agents.d.ts +0 -38
- package/dist-lib/lib/core/hosted-agents.js +0 -140
- package/dist-lib/lib/core/inference-calls-handoff.d.ts +0 -20
- package/dist-lib/lib/core/inference-calls-handoff.js +0 -37
- package/dist-lib/lib/core/inference-calls-receiver.d.ts +0 -14
- package/dist-lib/lib/core/inference-calls-receiver.js +0 -42
- package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -25
- package/dist-lib/lib/core/mesh-seed-handoff.js +0 -52
- package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
- package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
- package/dist-lib/lib/core/secret-handoff.d.ts +0 -17
- package/dist-lib/lib/core/secret-handoff.js +0 -29
- package/dist-lib/lib/core/skill-overlay-handoff.d.ts +0 -25
- package/dist-lib/lib/core/skill-overlay-handoff.js +0 -50
- package/dist-lib/lib/core/skill-overlay-receiver.d.ts +0 -8
- package/dist-lib/lib/core/skill-overlay-receiver.js +0 -23
- package/dist-lib/lib/image-cache-dir.d.ts +0 -1
- package/dist-lib/lib/image-cache-dir.js +0 -5
- package/dist-lib/lib/log-levels.d.ts +0 -7
- package/dist-lib/lib/log-levels.js +0 -1
- package/dist-lib/lib/logger.d.ts +0 -5
- package/dist-lib/lib/logger.js +0 -18
- package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
- package/dist-lib/lib/oauth/loopback.stub.js +0 -6
|
@@ -5,59 +5,101 @@ 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 {
|
|
9
|
-
import { isMobilePlatform, mediaModelNameFor, modelsForAgent, runnableModelName } from '@qvac/harness/lib/models/model-catalog.ts';
|
|
10
|
-
import { connectHarness } from '@qvac/harness';
|
|
8
|
+
import { isMobilePlatform, mediaModelNameFor, modelsForAgent, runnableModelName, skillModelsForAgent } from '@qvac/harness/lib/models/model-catalog.ts';
|
|
11
9
|
import * as z from 'zod';
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import { SKILL_STATE } from '@qvac/harness/
|
|
10
|
+
import { createHostRunLogAdapter, loggedMessages } from './assistant/host-run-log.js';
|
|
11
|
+
import { readUploadAttachment, writeUploadFrame } from './assistant/upload.js';
|
|
12
|
+
import { SKILL_STATE } from '@qvac/harness/skill-state';
|
|
15
13
|
import { toolsetEnabled } from '@qvac/harness/lib/tools/toolsets.ts';
|
|
16
14
|
import { RAG_CITATION_LINE } from '@qvac/harness/lib/prompt.ts';
|
|
17
15
|
import { MAX_RAG_SOURCE_URL_CHARS, MAX_RAG_SOURCES_PER_CHUNK, RAG_SOURCE_EXCERPT_CHARS } from '../schema/qvac.js';
|
|
18
|
-
import {
|
|
16
|
+
import { historyBranches, isPromptChunk } from './core/prompt-history.js';
|
|
17
|
+
import { drivenBy, handedOff, offeredTo } from './core/run-driver.js';
|
|
19
18
|
import ReadyGuard from 'ready-guard';
|
|
20
19
|
import ReadyResource from 'ready-resource';
|
|
21
20
|
import { local } from '../spec/index.js';
|
|
22
|
-
import { isMissingMesh } from './api/current-mesh.js';
|
|
23
21
|
import { meshScoped } from './mesh-scoped-engine.js';
|
|
24
22
|
import { buildTitlePrompt, deriveChatTitle, isDefaultChatTitle, parseTitleResponse } from './chat-title.js';
|
|
23
|
+
import { buildSuggestionPrompt, parseSuggestionResponse } from './chat-suggestion.js';
|
|
25
24
|
import { matchApprovalRule } from './core/approval-rules.js';
|
|
26
25
|
import { isAudioAttachment } from './core/audio-attachment.js';
|
|
27
|
-
import { attachTurnDocumentIds, formatRagContext, chooseRagNotice, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, createRagSourceTagFilter, citedByPhraseOverlap, formatFullDocumentContext, isFullTextHit, isTruncatedFullTextHit, sourcesFromHits, WEB_TOOLS, webSourcesFromToolResult, withoutDataChannel } from './core/files/index.js';
|
|
28
|
-
import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason } from './core/document-attachment.js';
|
|
26
|
+
import { attachTurnDocumentIds, formatRagContext, chooseRagNotice, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, createRagSourceTagFilter, citedByPhraseOverlap, formatFullDocumentContext, fullTextAttachmentId, isFullTextHit, isTruncatedFullTextHit, sourcesFromHits, WEB_TOOLS, webSourcesFromToolResult, withoutDataChannel } from './core/files/index.js';
|
|
27
|
+
import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason, PDF_PAGES_SOURCE_LINE, pdfPagesCutNotice, pdfPagesShownNotice } from './core/document-attachment.js';
|
|
29
28
|
import { imageCacheKey, imageCacheName, sweepImageCache } from './core/image/cache-sweep.js';
|
|
30
29
|
import { IMAGE_MIMES, normalizeImageForModel } from './core/image/normalize.js';
|
|
31
|
-
import {
|
|
32
|
-
import { summarizeRuns } from './diagnostics.js';
|
|
30
|
+
import { createPdfRasterizer } from './core/pdf/rasterize.js';
|
|
31
|
+
import { recordBackends, summarizeRuns } from './diagnostics.js';
|
|
33
32
|
import { resolveFsGrants } from './fs-drive-bridge.js';
|
|
34
33
|
import { InferenceCallStore } from './inference-calls.js';
|
|
34
|
+
import { LOCAL_HOLDER, RunReplayStore, trackSentRun } from './run-replay.js';
|
|
35
|
+
import { RunInputQueue, STEERED } from './run-input.js';
|
|
35
36
|
import { fsGrantKey, fsWriteGrantKey } from './fs-grant-keys.js';
|
|
36
37
|
import { fsGrantsFrom, grantedDirsFrom } from './fs-grants.js';
|
|
37
|
-
import {
|
|
38
|
+
import { persistentDir } from './persistent-dir.js';
|
|
38
39
|
import { ModelCatalogRevisionHub } from './model-catalog-revision.js';
|
|
39
|
-
import { agentRekeyFingerprint, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
|
|
40
|
-
import {
|
|
40
|
+
import { agentRekeyFingerprint, agentSwitched, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
|
|
41
|
+
import { KvCacheRetention } from './kv-cache-retention.js';
|
|
42
|
+
import { KvCacheLedger, orphanedChats } from './kv-cache-ledger.js';
|
|
43
|
+
import { KV_CACHE_RETENTION_KEY } from './constants.js';
|
|
44
|
+
import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_TURNS, COMPACTION_TURN_PREFIX, isContextOverflowFailure, nearContextLimit, compactionBoundary, foldsThrough, lastCompactedThroughId, parseCompactionSummary } from './compaction.js';
|
|
41
45
|
import { ModelDownloadReconciler, musicModelChoiceOf } from './model-download.js';
|
|
42
46
|
import { AbortedError, raceAbort } from '@qvac/harness/race-abort';
|
|
43
47
|
import { RunTranscript } from './run-transcript.js';
|
|
48
|
+
import { enabledSkillsOf, loadableSkills } from './core/skill-availability.js';
|
|
49
|
+
import { OwnerRounds, listRoundChunks, wholeImageId } from './assistant/model-round.js';
|
|
50
|
+
import { RoundHost } from './assistant/round-host.js';
|
|
44
51
|
import { searchKnowledge } from './knowledge-search.js';
|
|
45
52
|
import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
|
|
53
|
+
import { unref } from './unref.js';
|
|
46
54
|
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';
|
|
55
|
+
import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, syncedSkills, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, heldLoadModel, droppedHeldLoadModel, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, customModelReportEntries, toolReportEntry } from './assistant/harness.js';
|
|
56
|
+
import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, askedIn, claimNewImages, earlierImages, nameSenders, restoreSentContent, hiddenVideoNotice, unreadVideoNotice, unsyncedVideoNotice, VIDEO_NO_AUDIO, VIDEO_SILENT, videoAnnotation, videoTranscriptBlock } from './assistant/message.js';
|
|
57
|
+
import { frameTimes, probeVideo, sampleFrames } from './core/video/frames.js';
|
|
58
|
+
import { packVideoDigest, parseVideoDigestHeader, unpackVideoDigest, VIDEO_DIGEST_MIME, videoDigestFileName } from './core/video/digest.js';
|
|
59
|
+
import { VIDEO_FRAME_EDGE, videoFrameCount } from './assistant/video-budget.js';
|
|
49
60
|
import { drainBody, raceInactivity } from './assistant/fetch-body.js';
|
|
50
|
-
import {
|
|
61
|
+
import { pdfImageRoom } from './assistant/pdf-budget.js';
|
|
62
|
+
import { packPdfSelection, parsePdfSelection, PDF_PAGES_MIME, pdfSelectionFileName, selectPdfPages } from './assistant/pdf-pages.js';
|
|
63
|
+
import { effectiveCtxSize } from './api/effective-ctx-size.js';
|
|
64
|
+
import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, carriedInput, carriedRun, foldableView, historyFromChunks, listHistoryChunks, replayFromChunks, runPromptView, runView, senderOf, staleToolRunIds, viewOutgrewCache } from './assistant/history.js';
|
|
51
65
|
import { errorMessage, isRecord } from './guards.js';
|
|
52
|
-
|
|
66
|
+
import { CoreError, isCoreError } from './errors.js';
|
|
67
|
+
export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, syncedSkills, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
|
|
53
68
|
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();
|
|
69
|
+
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders, restoreSentContent } from './assistant/message.js';
|
|
70
|
+
export { historyFromChunks, listHistoryChunks, staleToolRunIds } from './assistant/history.js';
|
|
57
71
|
const DEFAULT_PROFILE = 'mid';
|
|
58
72
|
const QvacState = local.getEnum('@local/qvac-state');
|
|
59
73
|
const FINAL_FLUSH_MS = 10_000;
|
|
60
74
|
const UPLOAD_FRAME_BYTES = 64 * 1024;
|
|
75
|
+
const RUN_IDLE_TIMEOUT_MS = 15 * 60_000;
|
|
76
|
+
const MODEL_RELOAD_DELAY_MS = 2_500;
|
|
77
|
+
function idleWatch(controller, ms) {
|
|
78
|
+
let last = 0;
|
|
79
|
+
let parked = 0;
|
|
80
|
+
const timer = setInterval(() => {
|
|
81
|
+
if (last && !parked && Date.now() - last >= ms) {
|
|
82
|
+
controller.abort(RUN_ABORT_REASONS.stalled);
|
|
83
|
+
}
|
|
84
|
+
}, Math.min(ms, 30_000));
|
|
85
|
+
unref(timer);
|
|
86
|
+
return {
|
|
87
|
+
touch: () => {
|
|
88
|
+
last = Date.now();
|
|
89
|
+
},
|
|
90
|
+
parks: (fn) => async (...args) => {
|
|
91
|
+
parked++;
|
|
92
|
+
try {
|
|
93
|
+
return await fn(...args);
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
parked--;
|
|
97
|
+
last = Date.now();
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
stop: () => clearInterval(timer)
|
|
101
|
+
};
|
|
102
|
+
}
|
|
61
103
|
function settledWithin(write, ms) {
|
|
62
104
|
let timer;
|
|
63
105
|
const expired = new Promise((resolve) => {
|
|
@@ -71,7 +113,18 @@ function isDeniedResult(result) {
|
|
|
71
113
|
return false;
|
|
72
114
|
return 'denied' in result && result.denied === true;
|
|
73
115
|
}
|
|
74
|
-
const
|
|
116
|
+
const TITLE_PROBE = {
|
|
117
|
+
kind: 'title',
|
|
118
|
+
name: 'chat_title',
|
|
119
|
+
field: 'title',
|
|
120
|
+
runId: 'chat-title'
|
|
121
|
+
};
|
|
122
|
+
const SUGGESTION_PROBE = {
|
|
123
|
+
kind: 'suggestion',
|
|
124
|
+
name: 'next_message',
|
|
125
|
+
field: 'prompt',
|
|
126
|
+
runId: 'chat-suggestion'
|
|
127
|
+
};
|
|
75
128
|
const RunOccupancySchema = z.object({
|
|
76
129
|
cacheTokens: z.number().optional().catch(undefined),
|
|
77
130
|
ctxSize: z.number().optional().catch(undefined)
|
|
@@ -79,6 +132,20 @@ const RunOccupancySchema = z.object({
|
|
|
79
132
|
export const CAPTURE_INFERENCE_CALLS_KEY = 'inspector.captureInferenceCalls';
|
|
80
133
|
export const CAPTURE_ON = 'on';
|
|
81
134
|
export const CAPTURE_OFF = 'off';
|
|
135
|
+
export const SUGGEST_PROMPTS_KEY = 'assistant.suggestPrompts';
|
|
136
|
+
export const SUGGEST_PROMPTS_ON = 'on';
|
|
137
|
+
const SUGGESTION_SUPERSEDED = "superseded by the chat's next run";
|
|
138
|
+
const NO_PDF_PAGES = { selection: { pageCount: 0, shots: [] }, names: [], labels: [] };
|
|
139
|
+
function pdfNotices(fileName, pages, wholeText) {
|
|
140
|
+
const { cutAt, pageCount } = pages.selection;
|
|
141
|
+
const shown = pages.names.length;
|
|
142
|
+
return [
|
|
143
|
+
...(shown ? [pdfPagesShownNotice(fileName, pages.labels)] : []),
|
|
144
|
+
...(cutAt === undefined
|
|
145
|
+
? []
|
|
146
|
+
: [pdfPagesCutNotice({ fileName, cutAt, pageCount, shown, wholeText })])
|
|
147
|
+
];
|
|
148
|
+
}
|
|
82
149
|
function attachPages(turn, files) {
|
|
83
150
|
if (!files.length)
|
|
84
151
|
return;
|
|
@@ -87,8 +154,40 @@ function attachPages(turn, files) {
|
|
|
87
154
|
function noBytes() {
|
|
88
155
|
return Promise.resolve(null);
|
|
89
156
|
}
|
|
90
|
-
function
|
|
91
|
-
return imageCacheName({ blobId:
|
|
157
|
+
function videoFrameCacheName(video, count, frame) {
|
|
158
|
+
return imageCacheName({ blobId: video.blobId, fileName: `frames.${count}.f${frame}.jpg` });
|
|
159
|
+
}
|
|
160
|
+
function videoHeaderCacheName(digestBlobId) {
|
|
161
|
+
return imageCacheName({ blobId: digestBlobId, fileName: 'frames.json' });
|
|
162
|
+
}
|
|
163
|
+
function videoAudioBlock(fileName, { audio, transcript }) {
|
|
164
|
+
if (audio === 'none') {
|
|
165
|
+
return VIDEO_NO_AUDIO;
|
|
166
|
+
}
|
|
167
|
+
if (audio === 'silent') {
|
|
168
|
+
return VIDEO_SILENT;
|
|
169
|
+
}
|
|
170
|
+
if (audio === 'speech' && transcript) {
|
|
171
|
+
return videoTranscriptBlock(transcript);
|
|
172
|
+
}
|
|
173
|
+
return unheardAudioNotice(`The audio of the attached video "${fileName}"`, 'transcription failed');
|
|
174
|
+
}
|
|
175
|
+
async function cachedPdfPages(selection, { dir, pdf, pinned }) {
|
|
176
|
+
const names = selection.shots.map((shot) => pdfShotCacheName(pdf, shot));
|
|
177
|
+
for (const name of names) {
|
|
178
|
+
pinned.add(name);
|
|
179
|
+
}
|
|
180
|
+
const files = names.map((name) => path.join(dir, name));
|
|
181
|
+
const cached = await Promise.all(files.map((file) => cacheOrTouch(file, noBytes)));
|
|
182
|
+
return cached.every(Boolean)
|
|
183
|
+
? { selection, names, labels: selection.shots.map((shot) => shot.label) }
|
|
184
|
+
: null;
|
|
185
|
+
}
|
|
186
|
+
function pdfShotCacheName(pdf, shot) {
|
|
187
|
+
const box = shot.clip
|
|
188
|
+
? `.c${[shot.clip.x, shot.clip.y, shot.clip.width, shot.clip.height].map(Math.round).join('_')}`
|
|
189
|
+
: '';
|
|
190
|
+
return imageCacheName({ blobId: pdf.blobId, fileName: `${pdf.fileName}.p${shot.page}${box}.jpg` });
|
|
92
191
|
}
|
|
93
192
|
async function cacheOrTouch(file, produce) {
|
|
94
193
|
const cached = await fs.promises.stat(file).catch(noop);
|
|
@@ -110,15 +209,73 @@ const BLOB_READ_TIMEOUT_MS = 15_000;
|
|
|
110
209
|
const SUSPENDED = RUN_ABORT_REASONS.suspended;
|
|
111
210
|
const OWNERSHIP_LOST = RUN_ABORT_REASONS.ownershipLost;
|
|
112
211
|
const RUN_SETTLED = RUN_ABORT_REASONS.runSettled;
|
|
212
|
+
const AGENT_REMOVED = RUN_ABORT_REASONS.agentRemoved;
|
|
113
213
|
const RUNNER_DIED = RUN_ABORT_REASONS.runnerDied;
|
|
114
214
|
const RUNNER_CLOSED = RUN_ABORT_REASONS.runnerClosed;
|
|
115
215
|
const MODEL_UNLOADED = RUN_ABORT_REASONS.modelUnloaded;
|
|
216
|
+
const AGENT_READ_CUT = 'agent-read-cut';
|
|
217
|
+
const RUN_PAUSED = 'paused on a saved checkpoint; it resumes from there';
|
|
218
|
+
function recoveryReason(event) {
|
|
219
|
+
const operation = isRecord(event.operation) ? event.operation.name : undefined;
|
|
220
|
+
const call = typeof operation === 'string' ? ` of ${operation}` : '';
|
|
221
|
+
return `the outcome${call} is unknown, so it is not run again: decide before this run continues`;
|
|
222
|
+
}
|
|
223
|
+
function keepsRunLog(capability) {
|
|
224
|
+
return capability?.hostRunLog === true && capability.hostRunLogVersion === 1;
|
|
225
|
+
}
|
|
116
226
|
function isSetUpHere(status) {
|
|
117
227
|
return status === SKILL_STATE.ready || status === SKILL_STATE.incompatibleModel;
|
|
118
228
|
}
|
|
119
229
|
function sameNameList(left, right) {
|
|
120
230
|
return left.length === right.length && left.every((name, index) => name === right[index]);
|
|
121
231
|
}
|
|
232
|
+
function sameLoadSettings(a, b) {
|
|
233
|
+
return ((a.ctxSize ?? 0) === (b.ctxSize ?? 0) &&
|
|
234
|
+
(a.kvCacheK ?? '') === (b.kvCacheK ?? '') &&
|
|
235
|
+
(a.kvCacheV ?? '') === (b.kvCacheV ?? ''));
|
|
236
|
+
}
|
|
237
|
+
function modelLeft(before, after, deviceId) {
|
|
238
|
+
if (!deviceId || !before.modelName || !runsHere(before, deviceId))
|
|
239
|
+
return null;
|
|
240
|
+
const stays = after && runsHere(after, deviceId) && after.modelName === before.modelName;
|
|
241
|
+
return stays ? null : before.modelName;
|
|
242
|
+
}
|
|
243
|
+
function agentRefused(err) {
|
|
244
|
+
return isCoreError(err, 'AGENT_NOT_FOUND') || isCoreError(err, 'CHAT_AGENT_UNAVAILABLE');
|
|
245
|
+
}
|
|
246
|
+
function watchEdge(streams) {
|
|
247
|
+
const armed = Promise.withResolvers();
|
|
248
|
+
const fired = Promise.withResolvers();
|
|
249
|
+
let opening = streams.length;
|
|
250
|
+
for (const frames of streams) {
|
|
251
|
+
void (async () => {
|
|
252
|
+
let first = true;
|
|
253
|
+
for await (const _ of frames) {
|
|
254
|
+
if (!first) {
|
|
255
|
+
fired.resolve(true);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
first = false;
|
|
259
|
+
if (--opening === 0) {
|
|
260
|
+
armed.resolve(true);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
})()
|
|
264
|
+
.catch(noop)
|
|
265
|
+
.finally(() => {
|
|
266
|
+
armed.resolve(false);
|
|
267
|
+
fired.resolve(false);
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
armed: armed.promise,
|
|
272
|
+
fired: fired.promise,
|
|
273
|
+
stop: () => streams.forEach((frames) => frames.destroy())
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
function runsHere(agent, deviceId) {
|
|
277
|
+
return (!agent.deletedAt && !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId));
|
|
278
|
+
}
|
|
122
279
|
function withConfigDefaults(agent, config) {
|
|
123
280
|
if (agent.ctxSize || !config?.ctxSize)
|
|
124
281
|
return agent;
|
|
@@ -137,8 +294,6 @@ const WEB_RAG_SOURCE_BOUNDS = { ...RAG_SOURCE_BOUNDS, maxUrlChars: MAX_RAG_SOURC
|
|
|
137
294
|
export class QvacAssistant extends ReadyResource {
|
|
138
295
|
_engine;
|
|
139
296
|
_harness;
|
|
140
|
-
_borrowHarness;
|
|
141
|
-
_borrowed = new Map();
|
|
142
297
|
_log;
|
|
143
298
|
_imageCacheDir;
|
|
144
299
|
_imageCacheMaxBytes;
|
|
@@ -149,6 +304,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
149
304
|
_preloadCurrentAgentModel;
|
|
150
305
|
_drives;
|
|
151
306
|
_blobReadTimeoutMs;
|
|
307
|
+
_runIdleTimeoutMs;
|
|
308
|
+
_modelReloadDelayMs;
|
|
309
|
+
_reloads = new Map();
|
|
310
|
+
_releases = new Map();
|
|
152
311
|
_pdfRasterizer;
|
|
153
312
|
_warmAbort = new AbortController();
|
|
154
313
|
_unloading = new Map();
|
|
@@ -158,20 +317,23 @@ export class QvacAssistant extends ReadyResource {
|
|
|
158
317
|
_deviceId;
|
|
159
318
|
_info;
|
|
160
319
|
_lastCapabilities = new Map();
|
|
161
|
-
|
|
162
|
-
|
|
320
|
+
_listed = [];
|
|
321
|
+
_accountHosted = [];
|
|
163
322
|
_readySkills;
|
|
323
|
+
_skillsHere = null;
|
|
164
324
|
_recordingReadiness = false;
|
|
165
325
|
_readinessDirty = false;
|
|
166
326
|
_reconciling;
|
|
167
327
|
_onlineBoot = new ReadyGuard();
|
|
168
328
|
_accountSeen = Promise.withResolvers();
|
|
329
|
+
_meshesSeen = Promise.withResolvers();
|
|
169
330
|
_capability;
|
|
170
331
|
_downloads;
|
|
171
332
|
_state;
|
|
172
333
|
_harnessDiagnostics;
|
|
173
334
|
_harnessDeath;
|
|
174
335
|
_refreshingDiagnostics;
|
|
336
|
+
_backendUse = new Map();
|
|
175
337
|
_meshList;
|
|
176
338
|
_accountFrames;
|
|
177
339
|
_accountMeshId;
|
|
@@ -179,27 +341,42 @@ export class QvacAssistant extends ReadyResource {
|
|
|
179
341
|
_meshListWatch;
|
|
180
342
|
_accountWatch;
|
|
181
343
|
_meshConsumers;
|
|
344
|
+
_servedChats = new Map();
|
|
182
345
|
_runs;
|
|
346
|
+
_steered = new Map();
|
|
347
|
+
_idleWaiters = new Set();
|
|
183
348
|
_seen;
|
|
349
|
+
_parked;
|
|
350
|
+
_retakes;
|
|
351
|
+
_handedOff = new Map();
|
|
352
|
+
_backgroundReplays;
|
|
184
353
|
_inferenceCalls;
|
|
354
|
+
_runReplay;
|
|
355
|
+
_roundHost;
|
|
356
|
+
_kvRetention;
|
|
357
|
+
_kvLedger;
|
|
358
|
+
_orphanSweep = Promise.resolve();
|
|
185
359
|
_lifecycle;
|
|
360
|
+
_suggesting = new Map();
|
|
186
361
|
_audioTranscripts;
|
|
187
|
-
|
|
362
|
+
_videoProbes;
|
|
363
|
+
_pdfSelections;
|
|
364
|
+
_mediaRefs;
|
|
188
365
|
_chats;
|
|
366
|
+
_intake = new Map();
|
|
189
367
|
_runImageCacheNames;
|
|
190
368
|
_reconciledCompactions;
|
|
191
|
-
|
|
369
|
+
_knowledgeCaps = new Map();
|
|
192
370
|
_modelCatalog = new ModelCatalogRevisionHub();
|
|
193
371
|
_onHarnessStatus;
|
|
194
372
|
_onModelCatalogEvent;
|
|
195
|
-
constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives,
|
|
373
|
+
constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, kvCacheChatsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, runIdleTimeoutMs = RUN_IDLE_TIMEOUT_MS, modelReloadDelayMs = MODEL_RELOAD_DELAY_MS, pdfRasterizer: pdfRasterizerOption } = {}) {
|
|
196
374
|
super();
|
|
197
375
|
this._engine = engine;
|
|
198
376
|
this._harness = harness;
|
|
199
|
-
this._borrowHarness = borrowHarness ?? null;
|
|
200
377
|
this._drives = drives;
|
|
201
378
|
this._log = logger;
|
|
202
|
-
this._imageCacheDir = imageCacheDir ??
|
|
379
|
+
this._imageCacheDir = imageCacheDir ?? persistentDir('image-cache');
|
|
203
380
|
this._imageCacheMaxBytes = imageCacheMaxBytes;
|
|
204
381
|
this._imageCacheMaxAgeMs = imageCacheMaxAgeMs;
|
|
205
382
|
this._downloadRetryMs = downloadRetryMs;
|
|
@@ -207,7 +384,31 @@ export class QvacAssistant extends ReadyResource {
|
|
|
207
384
|
this._mobileClass = isMobilePlatform(platform);
|
|
208
385
|
this._preloadCurrentAgentModel = preloadCurrentAgentModel;
|
|
209
386
|
this._blobReadTimeoutMs = blobReadTimeoutMs;
|
|
210
|
-
this.
|
|
387
|
+
this._runIdleTimeoutMs = runIdleTimeoutMs;
|
|
388
|
+
this._modelReloadDelayMs = modelReloadDelayMs;
|
|
389
|
+
this._pdfRasterizer = pdfRasterizerOption ?? createPdfRasterizer(logger);
|
|
390
|
+
const serve = harness.serveModelRound?.bind(harness);
|
|
391
|
+
const generate = harness.serveMediaRound?.bind(harness);
|
|
392
|
+
this._roundHost = serve
|
|
393
|
+
? new RoundHost({
|
|
394
|
+
engine,
|
|
395
|
+
serve,
|
|
396
|
+
deviceId: () => this._requireDeviceId(),
|
|
397
|
+
media: {
|
|
398
|
+
resolve: (sources, served) => this._servedMedia(sources, served),
|
|
399
|
+
release: (id) => this._runImageCacheNames.delete(`round:${id}`)
|
|
400
|
+
},
|
|
401
|
+
load: (scoped, agentId) => this._servedLoad(scoped, agentId),
|
|
402
|
+
built: (meshId, chatId) => this._recordKvCache(meshId, chatId),
|
|
403
|
+
generation: generate
|
|
404
|
+
? {
|
|
405
|
+
serve: generate,
|
|
406
|
+
ready: (wait) => this._generationReady(wait)
|
|
407
|
+
}
|
|
408
|
+
: null,
|
|
409
|
+
log: logger
|
|
410
|
+
})
|
|
411
|
+
: null;
|
|
211
412
|
this._deviceId = null;
|
|
212
413
|
this._info = { toolNames: [], skills: [], supportsImages: false };
|
|
213
414
|
this._readySkills = null;
|
|
@@ -227,14 +428,27 @@ export class QvacAssistant extends ReadyResource {
|
|
|
227
428
|
this._meshConsumers = new Map();
|
|
228
429
|
this._runs = new Map();
|
|
229
430
|
this._seen = new Map();
|
|
230
|
-
this.
|
|
431
|
+
this._parked = new Map();
|
|
432
|
+
this._retakes = new Map();
|
|
433
|
+
this._backgroundReplays = new Set();
|
|
434
|
+
this._inferenceCalls = new InferenceCallStore(inferenceCallsDir ?? persistentDir('inspector'));
|
|
435
|
+
this._runReplay = new RunReplayStore(runReplayDir ?? persistentDir('run-replay'));
|
|
436
|
+
this._kvLedger = new KvCacheLedger(kvCacheChatsDir ?? persistentDir('kv-cache-chats'));
|
|
437
|
+
this._kvRetention = new KvCacheRetention(engine, {
|
|
438
|
+
active: () => !this.closing && !this.suspended,
|
|
439
|
+
running: (chatId) => this._kvCacheInUse(chatId),
|
|
440
|
+
deleteChatKvCache: (chatId) => this._deleteChatKvCache(chatId, 'idle past retention'),
|
|
441
|
+
defaultMs: kvCacheRetentionMs,
|
|
442
|
+
logger
|
|
443
|
+
});
|
|
231
444
|
this._lifecycle = new AbortController();
|
|
232
445
|
this._audioTranscripts = new Map();
|
|
233
|
-
this.
|
|
446
|
+
this._videoProbes = new Map();
|
|
447
|
+
this._pdfSelections = new Map();
|
|
448
|
+
this._mediaRefs = new Map();
|
|
234
449
|
this._chats = new Map();
|
|
235
450
|
this._runImageCacheNames = new Map();
|
|
236
451
|
this._reconciledCompactions = new Map();
|
|
237
|
-
this._chatAttribution = new Map();
|
|
238
452
|
this._onHarnessStatus = (status) => {
|
|
239
453
|
this._log?.warn('assistant', 'harness status', status);
|
|
240
454
|
if (harnessDied(status)) {
|
|
@@ -257,6 +471,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
257
471
|
if (progress) {
|
|
258
472
|
this._downloads?.observeImageProgress(progress);
|
|
259
473
|
}
|
|
474
|
+
const held = heldLoadModel(state);
|
|
475
|
+
if (held) {
|
|
476
|
+
this._downloads?.observeHeldLoad(held);
|
|
477
|
+
}
|
|
478
|
+
const dropped = droppedHeldLoadModel(state);
|
|
479
|
+
if (dropped) {
|
|
480
|
+
this._downloads?.observeDroppedHeldLoad(dropped);
|
|
481
|
+
}
|
|
260
482
|
};
|
|
261
483
|
}
|
|
262
484
|
_requireDeviceId() {
|
|
@@ -274,6 +496,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
274
496
|
this._harness.on?.('status', this._onHarnessStatus);
|
|
275
497
|
this._harness.on?.('model', this._onModelCatalogEvent);
|
|
276
498
|
this._info = await this._describeHarness();
|
|
499
|
+
await this._clearPreReplayCaches();
|
|
277
500
|
await this._clearPersistedPauses();
|
|
278
501
|
this._downloads = new ModelDownloadReconciler({
|
|
279
502
|
engine: this._engine,
|
|
@@ -292,6 +515,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
292
515
|
this._downloads.start();
|
|
293
516
|
this._meshListWatch.start();
|
|
294
517
|
this._accountWatch.start();
|
|
518
|
+
this._kvRetention.start(() => this._meshConsumers.keys());
|
|
295
519
|
void this._bootOnline();
|
|
296
520
|
this._triggerModelWarm();
|
|
297
521
|
void this.onlineReady().then(() => this._triggerModelWarm(), noop);
|
|
@@ -370,6 +594,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
370
594
|
finally {
|
|
371
595
|
this._onlineBoot.exit();
|
|
372
596
|
}
|
|
597
|
+
await this._meshesSeen.promise;
|
|
598
|
+
await this._reclaimOrphanedKvCaches();
|
|
373
599
|
}
|
|
374
600
|
async onlineReady() {
|
|
375
601
|
if (!this.opened)
|
|
@@ -501,7 +727,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
501
727
|
assistant: summarizeRuns(runs, this.suspended),
|
|
502
728
|
platform: this._platform,
|
|
503
729
|
mobileClass: this._mobileClass,
|
|
504
|
-
memoryBudget: this._capability?.memoryBudget ?? null
|
|
730
|
+
memoryBudget: this._capability?.memoryBudget ?? null,
|
|
731
|
+
backendUse: Object.fromEntries([...this._backendUse].map(([tool, uses]) => [tool, [...uses]]))
|
|
505
732
|
};
|
|
506
733
|
}
|
|
507
734
|
async _refreshHarnessDiagnostics() {
|
|
@@ -528,6 +755,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
528
755
|
}
|
|
529
756
|
const installedModelNames = models.map((model) => model.name);
|
|
530
757
|
const cachedModelNames = models.filter((model) => model.isCached).map((model) => model.name);
|
|
758
|
+
const customModels = customModelReportEntries(models);
|
|
531
759
|
let toolCatalog = null;
|
|
532
760
|
if (this._harness.tools) {
|
|
533
761
|
try {
|
|
@@ -543,30 +771,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
543
771
|
installedModelNames,
|
|
544
772
|
cachedModelNames,
|
|
545
773
|
this._readySkills,
|
|
546
|
-
toolCatalog
|
|
774
|
+
toolCatalog,
|
|
775
|
+
customModels
|
|
547
776
|
]);
|
|
548
777
|
const meshId = this._accountMeshId;
|
|
549
778
|
if (!meshId) {
|
|
550
779
|
return;
|
|
551
780
|
}
|
|
552
|
-
|
|
553
|
-
const report = {
|
|
781
|
+
await this._reportInto(meshId, key, {
|
|
554
782
|
installedToolNames: this._info.toolNames,
|
|
555
783
|
installedSkillNames: this._info.skills,
|
|
556
784
|
installedModelNames,
|
|
557
785
|
cachedModelNames,
|
|
558
|
-
readySkillNames:
|
|
559
|
-
toolCatalog: toolCatalog ?? undefined
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
if (!readySkillNames) {
|
|
563
|
-
return;
|
|
564
|
-
}
|
|
565
|
-
for (const [shared, hosted] of this._hostedIn) {
|
|
566
|
-
if (shared === meshId)
|
|
567
|
-
continue;
|
|
568
|
-
await this._reportInto(shared, JSON.stringify([readySkillNames, hosted]), report);
|
|
569
|
-
}
|
|
786
|
+
readySkillNames: this._readySkills ?? undefined,
|
|
787
|
+
toolCatalog: toolCatalog ?? undefined,
|
|
788
|
+
customModels
|
|
789
|
+
});
|
|
570
790
|
}
|
|
571
791
|
async _reportInto(meshId, key, report) {
|
|
572
792
|
if (this._lastCapabilities.get(meshId) === key)
|
|
@@ -576,7 +796,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
576
796
|
this._lastCapabilities.set(meshId, key);
|
|
577
797
|
}
|
|
578
798
|
catch (err) {
|
|
579
|
-
if (
|
|
799
|
+
if (isCoreError(err, 'MESH_NOT_FOUND') || isCoreError(err, 'MESH_STILL_JOINING')) {
|
|
580
800
|
return;
|
|
581
801
|
}
|
|
582
802
|
this._log?.error('assistant', 'saveDeviceCapabilities failed', err);
|
|
@@ -584,22 +804,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
584
804
|
}
|
|
585
805
|
_followHosted(meshId, agents) {
|
|
586
806
|
const deviceId = this._deviceId;
|
|
587
|
-
if (!deviceId ||
|
|
807
|
+
if (!deviceId || meshId !== this._accountMeshId) {
|
|
588
808
|
return;
|
|
589
809
|
}
|
|
590
810
|
const hosted = agents
|
|
591
811
|
.filter((agent) => !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId))
|
|
592
812
|
.map((agent) => agent.id)
|
|
593
813
|
.sort();
|
|
594
|
-
if (sameNameList(this.
|
|
814
|
+
if (sameNameList(this._accountHosted, hosted)) {
|
|
595
815
|
return;
|
|
596
816
|
}
|
|
597
|
-
|
|
598
|
-
this._hostedIn.set(meshId, hosted);
|
|
599
|
-
}
|
|
600
|
-
else {
|
|
601
|
-
this._hostedIn.delete(meshId);
|
|
602
|
-
}
|
|
817
|
+
this._accountHosted = hosted;
|
|
603
818
|
void this._reportHosted().catch(noop);
|
|
604
819
|
}
|
|
605
820
|
async _reportHosted() {
|
|
@@ -675,10 +890,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
675
890
|
}
|
|
676
891
|
async _close() {
|
|
677
892
|
this._stopMeshIntake();
|
|
893
|
+
this._kvRetention.stop();
|
|
678
894
|
this._harness.off?.('status', this._onHarnessStatus);
|
|
679
895
|
this._harness.off?.('model', this._onModelCatalogEvent);
|
|
680
896
|
this._warmAbort.abort(RUNNER_CLOSED);
|
|
681
|
-
this.
|
|
897
|
+
this._cancelModelReloads();
|
|
898
|
+
this._onlineBoot.destroy(new CoreError('ASSISTANT_CLOSING'));
|
|
682
899
|
for (const run of this._runs.values()) {
|
|
683
900
|
run.controller.abort(RUNNER_CLOSED);
|
|
684
901
|
}
|
|
@@ -687,11 +904,20 @@ export class QvacAssistant extends ReadyResource {
|
|
|
687
904
|
this._meshList?.destroy();
|
|
688
905
|
this._accountFrames?.destroy();
|
|
689
906
|
this._lifecycle.abort(RUNNER_CLOSED);
|
|
907
|
+
for (const pending of this._suggesting.values()) {
|
|
908
|
+
pending.abort(RUNNER_CLOSED);
|
|
909
|
+
}
|
|
690
910
|
await Promise.allSettled([...this._runs.values()].map((run) => run.done));
|
|
691
911
|
}
|
|
912
|
+
get mobileClass() {
|
|
913
|
+
return this._mobileClass;
|
|
914
|
+
}
|
|
692
915
|
get suspended() {
|
|
693
916
|
return this._state === QvacState.SUSPEND;
|
|
694
917
|
}
|
|
918
|
+
get _backgrounded() {
|
|
919
|
+
return this._state === QvacState.PRESUSPEND || this._state === QvacState.SUSPEND;
|
|
920
|
+
}
|
|
695
921
|
async presuspend() {
|
|
696
922
|
if (this.closing)
|
|
697
923
|
return;
|
|
@@ -708,7 +934,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
708
934
|
}
|
|
709
935
|
this._state = QvacState.SUSPEND;
|
|
710
936
|
this._log?.info('assistant', 'suspending', { runs: this._runs.size });
|
|
937
|
+
this._parked.clear();
|
|
711
938
|
this._stopMeshIntake();
|
|
939
|
+
this._cancelModelReloads();
|
|
712
940
|
await this._downloads?.suspend();
|
|
713
941
|
this._meshList?.destroy();
|
|
714
942
|
this._accountFrames?.destroy();
|
|
@@ -737,6 +965,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
737
965
|
}
|
|
738
966
|
if (this._state === QvacState.PRESUSPEND) {
|
|
739
967
|
this._state = QvacState.RESUME;
|
|
968
|
+
this._parked.forEach((replay) => replay());
|
|
969
|
+
this._parked.clear();
|
|
970
|
+
this._takeBackHandedOff();
|
|
740
971
|
return;
|
|
741
972
|
}
|
|
742
973
|
this._state = QvacState.RESUME;
|
|
@@ -771,10 +1002,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
771
1002
|
}
|
|
772
1003
|
async loadModel(name) {
|
|
773
1004
|
if (!this._harness.loadModel) {
|
|
774
|
-
throw new
|
|
1005
|
+
throw new CoreError('MODEL_RUNTIME_UNSUPPORTED', ['preload']);
|
|
775
1006
|
}
|
|
776
1007
|
if (!(await this._isCached(name))) {
|
|
777
|
-
throw new
|
|
1008
|
+
throw new CoreError('MODEL_NOT_DOWNLOADED', [name]);
|
|
778
1009
|
}
|
|
779
1010
|
const config = await this._engine.getConfig({}).catch(noop);
|
|
780
1011
|
const agent = await this._agentRunning(name, config?.activeAgentId ?? null);
|
|
@@ -785,10 +1016,89 @@ export class QvacAssistant extends ReadyResource {
|
|
|
785
1016
|
this._log?.info('models', 'model load requested', { model: name, loaded, reason });
|
|
786
1017
|
return { loaded, reason };
|
|
787
1018
|
}
|
|
1019
|
+
async _servedLoad(engine, agentId) {
|
|
1020
|
+
const meshId = this._accountMeshId;
|
|
1021
|
+
const agent = (await engine.getAgent({ agentId }).catch(noop)) ??
|
|
1022
|
+
(meshId ? await this._engine.getAgent({ agentId, meshId }).catch(noop) : undefined);
|
|
1023
|
+
if (!agent) {
|
|
1024
|
+
return undefined;
|
|
1025
|
+
}
|
|
1026
|
+
const config = await this._engine.getConfig({}).catch(noop);
|
|
1027
|
+
const { ctxSize, kvCacheK, kvCacheV } = withConfigDefaults(agent, config);
|
|
1028
|
+
return { ctxSize, kvCacheK, kvCacheV };
|
|
1029
|
+
}
|
|
788
1030
|
async _isCached(model) {
|
|
789
1031
|
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
790
1032
|
return rows?.some((row) => row.name === model && row.isCached) === true;
|
|
791
1033
|
}
|
|
1034
|
+
_scheduleModelReload(agent) {
|
|
1035
|
+
const deviceId = this._deviceId;
|
|
1036
|
+
if (!this._harness.loadModel ||
|
|
1037
|
+
!deviceId ||
|
|
1038
|
+
!agent.modelName ||
|
|
1039
|
+
!agent.providerDeviceId ||
|
|
1040
|
+
!b4a.equals(agent.providerDeviceId, deviceId)) {
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
const name = agent.modelName;
|
|
1044
|
+
clearTimeout(this._reloads.get(name));
|
|
1045
|
+
const timer = setTimeout(() => {
|
|
1046
|
+
this._reloads.delete(name);
|
|
1047
|
+
this._reloadLoadedModel(agent).catch((err) => this._log?.warn('models', 'model reload failed', { model: name, err }));
|
|
1048
|
+
}, this._modelReloadDelayMs);
|
|
1049
|
+
unref(timer);
|
|
1050
|
+
this._reloads.set(name, timer);
|
|
1051
|
+
}
|
|
1052
|
+
async _reloadLoadedModel(agent) {
|
|
1053
|
+
if (this.closing || this.suspended || !this._harness.loadModel)
|
|
1054
|
+
return;
|
|
1055
|
+
const model = runnableModelName(agent.modelName, await this._runnableHost());
|
|
1056
|
+
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
1057
|
+
const busy = [...this._runs.values()].some((run) => run.executing && run.model === model);
|
|
1058
|
+
if (busy || !rows?.some((row) => row.name === model && row.isLoaded)) {
|
|
1059
|
+
return;
|
|
1060
|
+
}
|
|
1061
|
+
const config = await this._engine.getConfig({}).catch(noop);
|
|
1062
|
+
const knobs = withConfigDefaults(agent, config);
|
|
1063
|
+
const { loaded, reason } = await this._harness.loadModel({ model, agent: knobs });
|
|
1064
|
+
this._log?.info('models', 'model reload requested', { model, loaded, reason });
|
|
1065
|
+
}
|
|
1066
|
+
_scheduleModelRelease(modelName) {
|
|
1067
|
+
clearTimeout(this._releases.get(modelName));
|
|
1068
|
+
const timer = setTimeout(() => {
|
|
1069
|
+
this._releases.delete(modelName);
|
|
1070
|
+
this._releaseUnusedModel(modelName).catch((err) => this._log?.warn('models', 'model release failed', { model: modelName, err }));
|
|
1071
|
+
}, this._modelReloadDelayMs);
|
|
1072
|
+
unref(timer);
|
|
1073
|
+
this._releases.set(modelName, timer);
|
|
1074
|
+
}
|
|
1075
|
+
async _releaseUnusedModel(modelName) {
|
|
1076
|
+
if (this.closing || this.suspended || !this._harness.unloadModel)
|
|
1077
|
+
return;
|
|
1078
|
+
const model = runnableModelName(modelName, await this._runnableHost());
|
|
1079
|
+
const holders = [...this._runs.values()].filter((run) => run.executing && run.model === model);
|
|
1080
|
+
await Promise.allSettled(holders.map((run) => run.done));
|
|
1081
|
+
if (this.closing || this.suspended) {
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
1085
|
+
if (!rows?.some((row) => row.name === model && row.isLoaded)) {
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
const { agents } = await this.modelUsers(model);
|
|
1089
|
+
if (agents?.length) {
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1092
|
+
const { unloaded } = await this.unloadModel(model);
|
|
1093
|
+
this._log?.info('models', 'unused model released', { model, unloaded });
|
|
1094
|
+
}
|
|
1095
|
+
_cancelModelReloads() {
|
|
1096
|
+
for (const timer of [...this._reloads.values(), ...this._releases.values()]) {
|
|
1097
|
+
clearTimeout(timer);
|
|
1098
|
+
}
|
|
1099
|
+
this._reloads.clear();
|
|
1100
|
+
this._releases.clear();
|
|
1101
|
+
}
|
|
792
1102
|
async _agentRunning(model, activeAgentId) {
|
|
793
1103
|
const deviceId = this._requireDeviceId();
|
|
794
1104
|
const meshId = this._accountMeshId;
|
|
@@ -817,9 +1127,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
817
1127
|
}
|
|
818
1128
|
async _unloadModel(name) {
|
|
819
1129
|
if (!this._harness.unloadModel) {
|
|
820
|
-
throw new
|
|
1130
|
+
throw new CoreError('MODEL_RUNTIME_UNSUPPORTED', ['unload']);
|
|
821
1131
|
}
|
|
822
|
-
const holders = [...this._runs.values()].filter((run) => run.executing &&
|
|
1132
|
+
const holders = [...this._runs.values()].filter((run) => run.executing && run.model === name);
|
|
823
1133
|
for (const run of holders) {
|
|
824
1134
|
run.controller.abort(MODEL_UNLOADED);
|
|
825
1135
|
}
|
|
@@ -837,15 +1147,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
837
1147
|
const host = await this._runnableHost();
|
|
838
1148
|
const { meshes } = await this._engine.listMeshes({});
|
|
839
1149
|
const scanned = await Promise.all((meshes ?? [])
|
|
840
|
-
.filter((mesh) => mesh.open && !mesh.
|
|
1150
|
+
.filter((mesh) => mesh.open && !mesh.workspace)
|
|
841
1151
|
.map((mesh) => this._meshModelUsers(name, { meshId: mesh.meshId, providerDeviceId, host })));
|
|
842
1152
|
return { agents: scanned.flat() };
|
|
843
1153
|
}
|
|
844
1154
|
async _meshModelUsers(name, { meshId, providerDeviceId, host }) {
|
|
845
1155
|
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) => {
|
|
1156
|
+
const perAgent = await Promise.all((agents ?? []).map(async (agent) => {
|
|
849
1157
|
let models = modelsForAgent(agent, host);
|
|
850
1158
|
if (models.some((model) => model.use === 'music')) {
|
|
851
1159
|
const configs = await this._engine
|
|
@@ -865,6 +1173,30 @@ export class QvacAssistant extends ReadyResource {
|
|
|
865
1173
|
}));
|
|
866
1174
|
return perAgent.flat();
|
|
867
1175
|
}
|
|
1176
|
+
async _generationReady({ engine, agentId, kind, signal }) {
|
|
1177
|
+
const agent = await engine.getAgent({ agentId }).catch(() => null);
|
|
1178
|
+
if (!agent) {
|
|
1179
|
+
return false;
|
|
1180
|
+
}
|
|
1181
|
+
const configs = kind === 'music'
|
|
1182
|
+
? await engine
|
|
1183
|
+
.listToolConfigs({ agentId })
|
|
1184
|
+
.then((result) => result.configs ?? [])
|
|
1185
|
+
.catch(() => [])
|
|
1186
|
+
: [];
|
|
1187
|
+
const musicModelChoice = musicModelChoiceOf(configs);
|
|
1188
|
+
const model = skillModelsForAgent({ ...agent, musicModelChoice }).find((entry) => entry.use === kind);
|
|
1189
|
+
if (!model) {
|
|
1190
|
+
return false;
|
|
1191
|
+
}
|
|
1192
|
+
await this._downloads?.waitUntilCached(model.name, signal);
|
|
1193
|
+
return !signal.aborted;
|
|
1194
|
+
}
|
|
1195
|
+
async _runModel(agent, deviceId) {
|
|
1196
|
+
if (!b4a.equals(agent.providerDeviceId, deviceId))
|
|
1197
|
+
return agent.modelName;
|
|
1198
|
+
return runnableModelName(agent.modelName, await this._runnableHost());
|
|
1199
|
+
}
|
|
868
1200
|
async _runnableHost() {
|
|
869
1201
|
const capability = await this._ensureCapability();
|
|
870
1202
|
return capability && { ...capability, mobile: this._mobileClass };
|
|
@@ -872,11 +1204,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
872
1204
|
notifyModelCatalog() {
|
|
873
1205
|
this._modelCatalog.notify();
|
|
874
1206
|
}
|
|
875
|
-
|
|
1207
|
+
cachedModelsChanged() {
|
|
1208
|
+
this._modelCatalog.notify();
|
|
1209
|
+
this._reportCapabilities().catch(noop);
|
|
1210
|
+
}
|
|
1211
|
+
async retryModelDownload(name, options) {
|
|
876
1212
|
if (!this._downloads) {
|
|
877
|
-
throw new
|
|
1213
|
+
throw new CoreError('MODEL_DOWNLOAD_UNAVAILABLE');
|
|
878
1214
|
}
|
|
879
|
-
await this._downloads.retry(name);
|
|
1215
|
+
await this._downloads.retry(name, options);
|
|
1216
|
+
}
|
|
1217
|
+
siblingResumedDownload(name) {
|
|
1218
|
+
this._downloads?.siblingResumed(name);
|
|
880
1219
|
}
|
|
881
1220
|
async skills(agentId, modelName, options) {
|
|
882
1221
|
if (!this.opened)
|
|
@@ -896,6 +1235,36 @@ export class QvacAssistant extends ReadyResource {
|
|
|
896
1235
|
this._recordSkillReadiness().catch(noop);
|
|
897
1236
|
return resolved;
|
|
898
1237
|
}
|
|
1238
|
+
async _skillsNotHere(agent) {
|
|
1239
|
+
const all = agent.skillsScope === 'all';
|
|
1240
|
+
if (!this._mobileClass || !this._harness.skills || (!all && !agent.enabledSkills?.length)) {
|
|
1241
|
+
return [];
|
|
1242
|
+
}
|
|
1243
|
+
const here = this._skillsHere ?? (await this._readSkillsHere());
|
|
1244
|
+
if (!here) {
|
|
1245
|
+
return [];
|
|
1246
|
+
}
|
|
1247
|
+
const provider = all ? await this._skillsOn(agent.providerDeviceId) : null;
|
|
1248
|
+
return enabledSkillsOf(agent, here, provider).filter((name) => !here.has(name));
|
|
1249
|
+
}
|
|
1250
|
+
async _skillsOn(deviceId) {
|
|
1251
|
+
const meshId = this._accountMeshId;
|
|
1252
|
+
const device = meshId
|
|
1253
|
+
? await this._engine.getDevice({ meshId, deviceId }).catch(noop)
|
|
1254
|
+
: undefined;
|
|
1255
|
+
const names = device?.installedSkillNames ?? [];
|
|
1256
|
+
return names.length ? new Set(names) : null;
|
|
1257
|
+
}
|
|
1258
|
+
async _readSkillsHere() {
|
|
1259
|
+
try {
|
|
1260
|
+
const catalog = await this._harness.skills?.(null, null, { skipCredentialProbe: true });
|
|
1261
|
+
this._skillsHere = catalog ? loadableSkills(catalog) : null;
|
|
1262
|
+
}
|
|
1263
|
+
catch (err) {
|
|
1264
|
+
this._log?.warn('runs', 'skill catalog unread, the run records no missing skill', err);
|
|
1265
|
+
}
|
|
1266
|
+
return this._skillsHere;
|
|
1267
|
+
}
|
|
899
1268
|
async _recordSkillReadiness() {
|
|
900
1269
|
this._readinessDirty = true;
|
|
901
1270
|
if (this._recordingReadiness) {
|
|
@@ -919,19 +1288,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
919
1288
|
}
|
|
920
1289
|
async _readySkillNames() {
|
|
921
1290
|
const unscoped = (await this._harness.skills?.(null, null, { skipCredentialProbe: true })) ?? [];
|
|
1291
|
+
this._skillsHere = loadableSkills(unscoped);
|
|
922
1292
|
const ready = unscoped.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name);
|
|
923
1293
|
const deviceId = this._deviceId;
|
|
924
1294
|
const meshId = this._accountMeshId;
|
|
925
1295
|
if (deviceId && meshId) {
|
|
926
1296
|
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);
|
|
1297
|
+
for (const agent of agents ?? []) {
|
|
935
1298
|
const resolved = (await this._harness.skills?.(agent.id, agent.modelName, {
|
|
936
1299
|
skipCredentialProbe: true
|
|
937
1300
|
})) ?? [];
|
|
@@ -946,9 +1309,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
946
1309
|
_supervisedWatch(label, run) {
|
|
947
1310
|
let running = false;
|
|
948
1311
|
let again = false;
|
|
1312
|
+
let stopped = false;
|
|
949
1313
|
const settle = (err) => {
|
|
950
1314
|
running = false;
|
|
951
|
-
if (err !== null && !
|
|
1315
|
+
if (err !== null && !stopped && !isCoreError(err, 'MESH_NOT_FOUND')) {
|
|
952
1316
|
this._log?.error('assistant', `${label} watch failed`, err);
|
|
953
1317
|
}
|
|
954
1318
|
if (again) {
|
|
@@ -965,12 +1329,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
965
1329
|
return;
|
|
966
1330
|
}
|
|
967
1331
|
running = true;
|
|
1332
|
+
stopped = false;
|
|
968
1333
|
run().then(() => settle(null), (err) => settle(err));
|
|
969
1334
|
};
|
|
970
|
-
|
|
1335
|
+
const stop = () => {
|
|
1336
|
+
again = false;
|
|
1337
|
+
stopped = true;
|
|
1338
|
+
};
|
|
1339
|
+
return { start, stop };
|
|
971
1340
|
}
|
|
972
|
-
async _reclaimDeletedChats(frames) {
|
|
1341
|
+
async _reclaimDeletedChats(frames, meshId, served) {
|
|
973
1342
|
let known = null;
|
|
1343
|
+
let agents = new Map();
|
|
974
1344
|
for await (const frame of frames) {
|
|
975
1345
|
if (this.closing || this.suspended)
|
|
976
1346
|
return;
|
|
@@ -978,64 +1348,112 @@ export class QvacAssistant extends ReadyResource {
|
|
|
978
1348
|
const gone = new Set(known === null ? [] : disappearedChatIds(known, ids));
|
|
979
1349
|
if (known !== null) {
|
|
980
1350
|
for (const chatId of gone) {
|
|
981
|
-
this._dropDeletedChat(chatId);
|
|
1351
|
+
this._dropDeletedChat(meshId, chatId);
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
for (const chat of frame.chats ?? []) {
|
|
1355
|
+
const before = agents.get(chat.id);
|
|
1356
|
+
if (before && before !== chat.agentId) {
|
|
1357
|
+
void this._dropServedKvCache(chat.id, before);
|
|
982
1358
|
}
|
|
983
1359
|
}
|
|
984
1360
|
known = new Set(ids);
|
|
1361
|
+
agents = new Map((frame.chats ?? []).map((chat) => [chat.id, chat.agentId]));
|
|
1362
|
+
if (served) {
|
|
1363
|
+
this._servedChats.set(meshId, known);
|
|
1364
|
+
}
|
|
985
1365
|
await this._sweepImageCache(gone);
|
|
986
1366
|
await this._sweepInferenceCalls();
|
|
987
1367
|
}
|
|
988
1368
|
}
|
|
989
|
-
|
|
1369
|
+
async _dropServedKvCache(chatId, agentId) {
|
|
1370
|
+
for (const key of this._roundHost?.left(chatId, agentId) ?? []) {
|
|
1371
|
+
try {
|
|
1372
|
+
await this._harness.deleteKvCache?.(key);
|
|
1373
|
+
}
|
|
1374
|
+
catch (err) {
|
|
1375
|
+
this._log?.warn('assistant', 'served kv-cache deletion failed', { chatId, err });
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
async _sweepIdleChatCaches(meshId, frames) {
|
|
1380
|
+
for await (const _frame of frames) {
|
|
1381
|
+
if (this.closing || this.suspended)
|
|
1382
|
+
return;
|
|
1383
|
+
void this._kvRetention.sweep(meshId);
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
_dropDeletedChat(meshId, chatId) {
|
|
990
1387
|
for (const run of this._runs.values()) {
|
|
991
|
-
if (run.chatId === chatId) {
|
|
992
|
-
|
|
1388
|
+
if (run.meshId === meshId && run.chatId === chatId) {
|
|
1389
|
+
run.controller.abort(RUN_SETTLED);
|
|
993
1390
|
}
|
|
994
1391
|
}
|
|
995
1392
|
this._reconciledCompactions.delete(chatId);
|
|
996
|
-
this.
|
|
997
|
-
|
|
1393
|
+
void this._deleteChatKvCache(chatId, 'chat deleted').finally(() => this._runReplay
|
|
1394
|
+
.forget(chatId)
|
|
1395
|
+
.catch((err) => this._log?.warn('assistant', 'run replay not dropped', { err })));
|
|
998
1396
|
void this._inferenceCalls
|
|
999
1397
|
.forget(chatId)
|
|
1000
1398
|
.catch((err) => this._log?.warn('assistant', 'inspector records not dropped', { err }));
|
|
1001
1399
|
}
|
|
1002
|
-
async
|
|
1400
|
+
async _followAgentEdits(meshId, frames) {
|
|
1003
1401
|
let known = null;
|
|
1004
1402
|
for await (const frame of frames) {
|
|
1005
1403
|
if (this.closing || this.suspended)
|
|
1006
1404
|
return;
|
|
1007
1405
|
const agents = frame.agents ?? [];
|
|
1008
1406
|
this._followHosted(meshId, agents);
|
|
1009
|
-
const next = new Map(agents.map((agent) => [agent.id, agentRekeyFingerprint(agent)]));
|
|
1010
1407
|
for (const agent of agents) {
|
|
1011
1408
|
const previous = known?.get(agent.id);
|
|
1012
|
-
if (previous
|
|
1013
|
-
|
|
1409
|
+
if (!previous) {
|
|
1410
|
+
continue;
|
|
1411
|
+
}
|
|
1412
|
+
if (agentRekeyFingerprint(previous) !== agentRekeyFingerprint(agent)) {
|
|
1413
|
+
void this._reclaimAgentChatCaches(meshId, agent);
|
|
1414
|
+
}
|
|
1415
|
+
if (!sameLoadSettings(previous, agent)) {
|
|
1416
|
+
this._scheduleModelReload(agent);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
const next = new Map(agents.map((agent) => [agent.id, agent]));
|
|
1420
|
+
for (const previous of known?.values() ?? []) {
|
|
1421
|
+
const model = modelLeft(previous, next.get(previous.id), this._deviceId);
|
|
1422
|
+
if (model) {
|
|
1423
|
+
this._scheduleModelRelease(model);
|
|
1014
1424
|
}
|
|
1015
1425
|
}
|
|
1016
1426
|
known = next;
|
|
1017
1427
|
}
|
|
1018
1428
|
}
|
|
1019
|
-
async _reclaimAgentChatCaches(meshId,
|
|
1429
|
+
async _reclaimAgentChatCaches(meshId, agent) {
|
|
1020
1430
|
try {
|
|
1021
1431
|
const { chats } = await this._engine.listChats({ meshId });
|
|
1022
1432
|
for (const chat of chats ?? []) {
|
|
1023
|
-
if (chat.agentId ===
|
|
1433
|
+
if (chat.agentId === agent.id) {
|
|
1024
1434
|
await this._deleteChatKvCache(chat.id, 'agent re-key');
|
|
1025
1435
|
}
|
|
1026
1436
|
}
|
|
1027
1437
|
}
|
|
1028
1438
|
catch (err) {
|
|
1029
|
-
this._log?.warn('assistant', 'agent re-key cache reclaim failed', { agentId, err });
|
|
1439
|
+
this._log?.warn('assistant', 'agent re-key cache reclaim failed', { agentId: agent.id, err });
|
|
1030
1440
|
}
|
|
1031
1441
|
}
|
|
1032
1442
|
async _reconcileCompaction(engine, chatId) {
|
|
1033
1443
|
const deviceId = this._requireDeviceId();
|
|
1034
|
-
const {
|
|
1035
|
-
.
|
|
1036
|
-
|
|
1444
|
+
const chunks = await engine.listChunks({ chatId, type: 'compaction' }).then((listed) => listed.chunks ?? [], (err) => {
|
|
1445
|
+
this._log?.warn('assistant', 'compaction state unreadable, dropping the cache', {
|
|
1446
|
+
chatId,
|
|
1447
|
+
err
|
|
1448
|
+
});
|
|
1449
|
+
return null;
|
|
1450
|
+
});
|
|
1451
|
+
if (!chunks) {
|
|
1452
|
+
await this._deleteChatKvCache(chatId, 'compaction state unreadable');
|
|
1453
|
+
return;
|
|
1454
|
+
}
|
|
1037
1455
|
let newest = null;
|
|
1038
|
-
for (const chunk of chunks
|
|
1456
|
+
for (const chunk of chunks) {
|
|
1039
1457
|
if (!newest || chunk.seq > newest.seq) {
|
|
1040
1458
|
newest = chunk;
|
|
1041
1459
|
}
|
|
@@ -1051,41 +1469,156 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1051
1469
|
this._reconciledCompactions.set(chatId, newest.id);
|
|
1052
1470
|
}
|
|
1053
1471
|
}
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1472
|
+
deleteChatKvCache(chatId) {
|
|
1473
|
+
return this._reclaimChatKvCache(chatId, 'deleted by request');
|
|
1474
|
+
}
|
|
1475
|
+
async _reclaimChatKvCache(chatId, reason) {
|
|
1476
|
+
const result = (await this._harness.deleteKvCache?.(chatKvCacheKey(chatId))) ?? {
|
|
1477
|
+
status: 'unsupported',
|
|
1478
|
+
error: 'Harness does not support KV cache deletion'
|
|
1479
|
+
};
|
|
1480
|
+
if (result.status === 'success') {
|
|
1481
|
+
await this._runReplay.forget(chatId, { holder: LOCAL_HOLDER, reason });
|
|
1482
|
+
await this._kvLedger.forget(chatId);
|
|
1483
|
+
}
|
|
1484
|
+
return result;
|
|
1485
|
+
}
|
|
1486
|
+
async clearKvCache() {
|
|
1487
|
+
const result = await harnessClearKvCache(this._harness)();
|
|
1488
|
+
if (result.status === 'success') {
|
|
1489
|
+
await this._runReplay.clear(LOCAL_HOLDER);
|
|
1490
|
+
await this._kvLedger.clear();
|
|
1491
|
+
}
|
|
1492
|
+
return result;
|
|
1493
|
+
}
|
|
1494
|
+
_recordKvCache(meshId, chatId) {
|
|
1495
|
+
const served = this._listed.some((mesh) => mesh.meshId === meshId && mesh.served);
|
|
1496
|
+
void this._kvLedger
|
|
1497
|
+
.record({ meshId, chatId, served })
|
|
1498
|
+
.catch((err) => this._log?.warn('assistant', 'kv-cache chat not recorded', { chatId, err }));
|
|
1499
|
+
}
|
|
1500
|
+
_reclaimOrphanedKvCaches(gone = new Set()) {
|
|
1501
|
+
this._orphanSweep = this._orphanSweep
|
|
1502
|
+
.then(() => this._sweepOrphanedKvCaches(gone))
|
|
1503
|
+
.catch((err) => this._log?.warn('assistant', 'orphaned kv-cache sweep failed', { err }));
|
|
1504
|
+
return this._orphanSweep;
|
|
1505
|
+
}
|
|
1506
|
+
async _sweepOrphanedKvCaches(gone) {
|
|
1507
|
+
if (this.closing || this.suspended)
|
|
1508
|
+
return;
|
|
1509
|
+
if ((await this._kvRetention.retentionMs()) === null)
|
|
1510
|
+
return;
|
|
1511
|
+
const entries = await this._kvLedger.entries();
|
|
1512
|
+
if (entries.length === 0) {
|
|
1513
|
+
return;
|
|
1514
|
+
}
|
|
1515
|
+
for (const chatId of orphanedChats(entries, { ...(await this._knownChats()), gone })) {
|
|
1516
|
+
if (this.closing || this.suspended)
|
|
1517
|
+
return;
|
|
1518
|
+
if (!this._kvCacheInUse(chatId)) {
|
|
1519
|
+
await this._deleteChatKvCache(chatId, 'no mesh lists the chat');
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
async _knownChats() {
|
|
1524
|
+
const live = new Set();
|
|
1525
|
+
const read = new Set();
|
|
1526
|
+
const closed = new Set();
|
|
1527
|
+
for (const mesh of this._listed) {
|
|
1528
|
+
if (mesh.account)
|
|
1529
|
+
continue;
|
|
1530
|
+
if (!mesh.open) {
|
|
1531
|
+
closed.add(mesh.meshId);
|
|
1532
|
+
continue;
|
|
1533
|
+
}
|
|
1534
|
+
const { chats } = await this._engine.listChats({ meshId: mesh.meshId });
|
|
1535
|
+
read.add(mesh.meshId);
|
|
1536
|
+
for (const chat of chats ?? []) {
|
|
1537
|
+
live.add(chat.id);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
return { live, read, closed };
|
|
1541
|
+
}
|
|
1542
|
+
_kvCacheInUse(chatId) {
|
|
1543
|
+
for (const run of this._runs.values()) {
|
|
1544
|
+
if (run.chatId === chatId)
|
|
1545
|
+
return true;
|
|
1057
1546
|
}
|
|
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 ?? []);
|
|
1547
|
+
return this._roundHost?.holds(chatId) ?? false;
|
|
1064
1548
|
}
|
|
1065
|
-
async
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1549
|
+
async _clearPreReplayCaches() {
|
|
1550
|
+
if (await this._runReplay.started())
|
|
1551
|
+
return;
|
|
1552
|
+
const result = await this.clearKvCache();
|
|
1553
|
+
if (result.status === 'failure') {
|
|
1554
|
+
this._log?.warn('assistant', 'pre-replay kv caches not cleared', { error: result.error });
|
|
1069
1555
|
return;
|
|
1070
1556
|
}
|
|
1071
|
-
|
|
1072
|
-
|
|
1557
|
+
await this._runReplay.start();
|
|
1558
|
+
}
|
|
1559
|
+
_inspectCall(inspecting, chatId, entry) {
|
|
1560
|
+
if (!inspecting)
|
|
1073
1561
|
return;
|
|
1562
|
+
void this._inferenceCalls
|
|
1563
|
+
.record(chatId, entry)
|
|
1564
|
+
.catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
|
|
1565
|
+
}
|
|
1566
|
+
async _inspectRound(chatId, entry) {
|
|
1567
|
+
this._inspectCall(await this._capturingInferenceCalls(), chatId, entry);
|
|
1568
|
+
}
|
|
1569
|
+
_inspectSent(inspecting, chatId, sent) {
|
|
1570
|
+
if (!inspecting)
|
|
1571
|
+
return;
|
|
1572
|
+
void this._inferenceCalls
|
|
1573
|
+
.recordSent(chatId, sent)
|
|
1574
|
+
.catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
|
|
1575
|
+
}
|
|
1576
|
+
async _sentRuns(chatId, rounds) {
|
|
1577
|
+
const served = replayFromChunks(rounds);
|
|
1578
|
+
try {
|
|
1579
|
+
const { sentRuns, askedRuns } = await this._runReplay.loadReplay({
|
|
1580
|
+
holder: LOCAL_HOLDER,
|
|
1581
|
+
chatId
|
|
1582
|
+
});
|
|
1583
|
+
for (const [runId, turns] of served.sentRuns) {
|
|
1584
|
+
sentRuns.set(runId, turns);
|
|
1585
|
+
}
|
|
1586
|
+
return {
|
|
1587
|
+
sentRuns,
|
|
1588
|
+
askedRuns,
|
|
1589
|
+
readRuns: served.readRuns,
|
|
1590
|
+
rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
|
|
1591
|
+
};
|
|
1074
1592
|
}
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
this.
|
|
1593
|
+
catch (err) {
|
|
1594
|
+
this._log?.warn('assistant', 'run replay unreadable', { chatId, err });
|
|
1595
|
+
await this._deleteChatKvCache(chatId, 'run replay unreadable');
|
|
1596
|
+
return {
|
|
1597
|
+
sentRuns: new Map(),
|
|
1598
|
+
askedRuns: new Map(),
|
|
1599
|
+
readRuns: new Map(),
|
|
1600
|
+
rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
|
|
1601
|
+
};
|
|
1078
1602
|
}
|
|
1079
1603
|
}
|
|
1080
|
-
async
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1604
|
+
async _recordSentRun(chatId, { runId, turns, asked }) {
|
|
1605
|
+
if (turns) {
|
|
1606
|
+
try {
|
|
1607
|
+
await this._runReplay.save({ holder: LOCAL_HOLDER, chatId }, runId, {
|
|
1608
|
+
messages: turns,
|
|
1609
|
+
asked
|
|
1610
|
+
});
|
|
1611
|
+
return;
|
|
1612
|
+
}
|
|
1613
|
+
catch (err) {
|
|
1614
|
+
this._log?.warn('assistant', 'run replay not saved', { chatId, runId, err });
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
await this._deleteChatKvCache(chatId, 'run not replayable as sent');
|
|
1085
1618
|
}
|
|
1086
1619
|
async _deleteChatKvCache(chatId, reason) {
|
|
1087
1620
|
try {
|
|
1088
|
-
const result = await this.
|
|
1621
|
+
const result = await this._reclaimChatKvCache(chatId, reason);
|
|
1089
1622
|
if (result.status === 'failure') {
|
|
1090
1623
|
this._log?.warn('assistant', 'chat kv-cache deletion failed', {
|
|
1091
1624
|
chatId,
|
|
@@ -1103,10 +1636,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1103
1636
|
}
|
|
1104
1637
|
async _watchMeshList() {
|
|
1105
1638
|
this._meshList = this._engine.meshesWatch({});
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1639
|
+
try {
|
|
1640
|
+
for await (const { meshes } of this._meshList) {
|
|
1641
|
+
if (this.closing || this.suspended)
|
|
1642
|
+
return;
|
|
1643
|
+
const before = this._listed;
|
|
1644
|
+
this._syncMeshConsumers(meshes ?? []);
|
|
1645
|
+
this._meshesSeen.resolve();
|
|
1646
|
+
const listed = new Set(this._listed.map((mesh) => mesh.meshId));
|
|
1647
|
+
const gone = new Set(before.filter((mesh) => !listed.has(mesh.meshId)).map((mesh) => mesh.meshId));
|
|
1648
|
+
if (gone.size > 0) {
|
|
1649
|
+
void this._reclaimOrphanedKvCaches(gone);
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
finally {
|
|
1654
|
+
this._meshesSeen.resolve();
|
|
1110
1655
|
}
|
|
1111
1656
|
}
|
|
1112
1657
|
async _watchAccount() {
|
|
@@ -1124,25 +1669,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1124
1669
|
}
|
|
1125
1670
|
}
|
|
1126
1671
|
_syncMeshConsumers(meshes) {
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
.filter((mesh) => mesh.open && !mesh.account && !mesh.compute)
|
|
1130
|
-
.map((mesh) => mesh.meshId));
|
|
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
|
-
}
|
|
1672
|
+
this._listed = meshes;
|
|
1673
|
+
const consumed = new Map(meshes.filter((mesh) => mesh.open).map((mesh) => [mesh.meshId, mesh]));
|
|
1137
1674
|
for (const [meshId, consumer] of this._meshConsumers) {
|
|
1138
|
-
if (
|
|
1675
|
+
if (consumed.has(meshId))
|
|
1139
1676
|
continue;
|
|
1140
1677
|
consumer.stop();
|
|
1678
|
+
this._roundHost?.drop(meshId);
|
|
1141
1679
|
this._meshConsumers.delete(meshId);
|
|
1142
1680
|
this._pruneSeen(meshId, new Set());
|
|
1681
|
+
for (const chatId of this._servedChats.get(meshId) ?? []) {
|
|
1682
|
+
void this._deleteChatKvCache(chatId, 'served mesh left');
|
|
1683
|
+
}
|
|
1684
|
+
this._servedChats.delete(meshId);
|
|
1143
1685
|
}
|
|
1144
|
-
for (const meshId of
|
|
1145
|
-
const consumer = this._meshConsumers.get(meshId) ?? this._meshConsumer(
|
|
1686
|
+
for (const [meshId, entry] of consumed) {
|
|
1687
|
+
const consumer = this._meshConsumers.get(meshId) ?? this._meshConsumer(entry);
|
|
1146
1688
|
this._meshConsumers.set(meshId, consumer);
|
|
1147
1689
|
consumer.start();
|
|
1148
1690
|
}
|
|
@@ -1164,12 +1706,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1164
1706
|
void this._reconcileDeviceProfile();
|
|
1165
1707
|
this._triggerModelWarm();
|
|
1166
1708
|
}
|
|
1167
|
-
_meshConsumer(meshId) {
|
|
1709
|
+
_meshConsumer({ meshId, served = false, account }) {
|
|
1710
|
+
if (account) {
|
|
1711
|
+
const agents = this._streamWatch(`agent:${meshId}`, () => this._engine.agentsWatch({ meshId }), (frames) => this._followAgentEdits(meshId, frames));
|
|
1712
|
+
return { start: () => agents.start(), stop: () => agents.stop() };
|
|
1713
|
+
}
|
|
1168
1714
|
const watches = [
|
|
1169
|
-
this._streamWatch(`
|
|
1170
|
-
this._streamWatch(`
|
|
1171
|
-
this._streamWatch(`agent:${meshId}`, () => this._engine.agentsWatch({ meshId }), (frames) => this._reclaimRekeyedAgents(meshId, frames))
|
|
1715
|
+
this._streamWatch(`chat-list:${meshId}`, () => this._engine.chatsWatch({ meshId }), (frames) => this._reclaimDeletedChats(frames, meshId, served)),
|
|
1716
|
+
this._streamWatch(`kv-cache-retention:${meshId}`, () => this._engine.configWatch({ key: KV_CACHE_RETENTION_KEY }), (frames) => this._sweepIdleChatCaches(meshId, frames))
|
|
1172
1717
|
];
|
|
1718
|
+
if (!served) {
|
|
1719
|
+
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)));
|
|
1720
|
+
}
|
|
1721
|
+
const host = this._roundHost;
|
|
1722
|
+
if (host) {
|
|
1723
|
+
watches.push(this._streamWatch(`model-requests:${meshId}`, () => this._engine.modelRequestsWatch({ meshId }), (frames) => host.follow(meshId, frames)));
|
|
1724
|
+
}
|
|
1173
1725
|
return {
|
|
1174
1726
|
start: () => watches.forEach((watch) => watch.start()),
|
|
1175
1727
|
stop: () => watches.forEach((watch) => watch.stop())
|
|
@@ -1190,22 +1742,47 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1190
1742
|
};
|
|
1191
1743
|
}
|
|
1192
1744
|
async _dispatchRunRequests(meshId, frames) {
|
|
1745
|
+
let opened = true;
|
|
1193
1746
|
for await (const frame of frames) {
|
|
1194
1747
|
if (this.closing || this.suspended)
|
|
1195
1748
|
return;
|
|
1196
1749
|
const chunks = frame.chunks ?? [];
|
|
1197
|
-
|
|
1750
|
+
const deviceId = this._requireDeviceId();
|
|
1751
|
+
const handed = chunks.filter((chunk) => handedOff(chunk, deviceId));
|
|
1752
|
+
this._handedOff.set(meshId, handed);
|
|
1753
|
+
if (opened) {
|
|
1754
|
+
this._takeBackHandedOff(meshId);
|
|
1755
|
+
}
|
|
1756
|
+
opened = false;
|
|
1757
|
+
for (const chunk of chunks.filter((chunk) => !handed.includes(chunk))) {
|
|
1198
1758
|
this._dispatch(chunk, meshId);
|
|
1199
1759
|
}
|
|
1200
1760
|
this._pruneSeen(meshId, new Set(chunks.map((chunk) => chunk.runId)));
|
|
1201
1761
|
}
|
|
1202
1762
|
}
|
|
1763
|
+
_takeBackHandedOff(only) {
|
|
1764
|
+
for (const [meshId, requests] of this._handedOff) {
|
|
1765
|
+
if (only && only !== meshId)
|
|
1766
|
+
continue;
|
|
1767
|
+
const engine = meshScoped(this._engine, meshId);
|
|
1768
|
+
for (const request of requests) {
|
|
1769
|
+
engine.takeRunRequest({ chunkId: request.id }).catch((err) => {
|
|
1770
|
+
this._log?.debug('runs', 'taking a handed-off run back failed', err);
|
|
1771
|
+
});
|
|
1772
|
+
}
|
|
1773
|
+
this._handedOff.delete(meshId);
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1203
1776
|
_stopMeshIntake() {
|
|
1777
|
+
this._roundHost?.stop();
|
|
1204
1778
|
this._meshListWatch.stop();
|
|
1205
1779
|
this._accountWatch.stop();
|
|
1206
1780
|
for (const consumer of this._meshConsumers.values()) {
|
|
1207
1781
|
consumer.stop();
|
|
1208
1782
|
}
|
|
1783
|
+
for (const edge of this._retakes.values()) {
|
|
1784
|
+
edge.stop();
|
|
1785
|
+
}
|
|
1209
1786
|
this._meshConsumers.clear();
|
|
1210
1787
|
this._advertisedOn = null;
|
|
1211
1788
|
}
|
|
@@ -1216,35 +1793,6 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1216
1793
|
this._seen.delete(runId);
|
|
1217
1794
|
}
|
|
1218
1795
|
}
|
|
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
1796
|
_dispatch(request, meshId) {
|
|
1249
1797
|
const runId = request.runId;
|
|
1250
1798
|
if (!runId || this._runs.has(runId) || this._seen.has(runId)) {
|
|
@@ -1252,59 +1800,164 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1252
1800
|
}
|
|
1253
1801
|
this._seen.set(runId, meshId);
|
|
1254
1802
|
const engine = meshScoped(this._engine, meshId);
|
|
1255
|
-
const
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
if (!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, this._requireDeviceId())) {
|
|
1263
|
-
return;
|
|
1803
|
+
const chat = `${meshId}:${request.chatId}`;
|
|
1804
|
+
const scheduled = (this._intake.get(chat) ?? Promise.resolve()).then(() => this._schedule(engine, request, { runId, meshId, chat }));
|
|
1805
|
+
const turn = scheduled.then(noop, noop);
|
|
1806
|
+
this._intake.set(chat, turn);
|
|
1807
|
+
void turn.then(() => {
|
|
1808
|
+
if (this._intake.get(chat) === turn) {
|
|
1809
|
+
this._intake.delete(chat);
|
|
1264
1810
|
}
|
|
1265
|
-
|
|
1811
|
+
});
|
|
1812
|
+
const settle = (async () => {
|
|
1813
|
+
const run = await scheduled;
|
|
1814
|
+
if (!run) {
|
|
1266
1815
|
return;
|
|
1267
1816
|
}
|
|
1268
|
-
this._log?.info('runs', 'run claimed', { runId, chatId: request.chatId, agentId });
|
|
1269
|
-
const controller = new AbortController();
|
|
1270
|
-
const prior = this._chats.get(request.chatId) ?? Promise.resolve();
|
|
1271
|
-
const done = prior.then(() => this._execute(request, agent, { controller, meshId, runId }));
|
|
1272
|
-
const link = done.catch(noop);
|
|
1273
|
-
this._chats.set(request.chatId, link);
|
|
1274
|
-
this._runs.set(runId, {
|
|
1275
|
-
controller,
|
|
1276
|
-
done,
|
|
1277
|
-
chatId: request.chatId,
|
|
1278
|
-
model: agent.modelName,
|
|
1279
|
-
executing: false,
|
|
1280
|
-
borrowed: false
|
|
1281
|
-
});
|
|
1282
1817
|
try {
|
|
1283
|
-
await done;
|
|
1818
|
+
await run.done;
|
|
1284
1819
|
}
|
|
1285
1820
|
finally {
|
|
1286
1821
|
this._runImageCacheNames.delete(runId);
|
|
1287
1822
|
this._runs.delete(runId);
|
|
1288
|
-
this.
|
|
1289
|
-
if (this._chats.get(
|
|
1290
|
-
this._chats.delete(
|
|
1823
|
+
this._runsChanged();
|
|
1824
|
+
if (this._chats.get(chat) === run.link) {
|
|
1825
|
+
this._chats.delete(chat);
|
|
1291
1826
|
}
|
|
1292
1827
|
void this._reportCapabilities();
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1828
|
+
this._requeue(runId, {
|
|
1829
|
+
meshId,
|
|
1830
|
+
reason: run.controller.signal.reason,
|
|
1831
|
+
replay: () => this._dispatch(run.request, meshId)
|
|
1832
|
+
});
|
|
1297
1833
|
}
|
|
1298
1834
|
})();
|
|
1299
1835
|
settle.catch((err) => this._log?.error('assistant', `run ${runId} failed`, err));
|
|
1300
1836
|
}
|
|
1837
|
+
async _schedule(engine, request, { runId, meshId, chat }) {
|
|
1838
|
+
const claim = await this._claim(engine, request, { runId, meshId });
|
|
1839
|
+
if (!claim) {
|
|
1840
|
+
return undefined;
|
|
1841
|
+
}
|
|
1842
|
+
const { agent } = claim;
|
|
1843
|
+
const controller = new AbortController();
|
|
1844
|
+
const prior = this._chats.get(chat) ?? Promise.resolve();
|
|
1845
|
+
const done = prior.then(() => this._execute(claim.request, agent, { controller, meshId, runId }));
|
|
1846
|
+
const link = done.catch(noop);
|
|
1847
|
+
this._chats.set(chat, link);
|
|
1848
|
+
this._runs.set(runId, {
|
|
1849
|
+
controller,
|
|
1850
|
+
done,
|
|
1851
|
+
meshId,
|
|
1852
|
+
chatId: claim.request.chatId,
|
|
1853
|
+
agentId: agent.id,
|
|
1854
|
+
model: agent.modelName,
|
|
1855
|
+
executing: false
|
|
1856
|
+
});
|
|
1857
|
+
return { request: claim.request, controller, done, link };
|
|
1858
|
+
}
|
|
1859
|
+
async _claim(engine, request, { runId, meshId }) {
|
|
1860
|
+
const agentId = request.runRequest?.agentId;
|
|
1861
|
+
let agent;
|
|
1862
|
+
try {
|
|
1863
|
+
agent = agentId ? await engine.getAgent({ agentId }) : undefined;
|
|
1864
|
+
}
|
|
1865
|
+
catch (err) {
|
|
1866
|
+
this._seen.delete(runId);
|
|
1867
|
+
if (!agentRefused(err)) {
|
|
1868
|
+
this._retakeOnSync(runId, meshId, () => this._dispatch(request, meshId));
|
|
1869
|
+
}
|
|
1870
|
+
return undefined;
|
|
1871
|
+
}
|
|
1872
|
+
if (!agent) {
|
|
1873
|
+
this._seen.delete(runId);
|
|
1874
|
+
return undefined;
|
|
1875
|
+
}
|
|
1876
|
+
const deviceId = this._requireDeviceId();
|
|
1877
|
+
if (offeredTo(request, deviceId) && !(await this._isHandled(engine, request))) {
|
|
1878
|
+
if (this._backgrounded) {
|
|
1879
|
+
this._seen.delete(runId);
|
|
1880
|
+
if (this._state === QvacState.PRESUSPEND) {
|
|
1881
|
+
this._parked.set(runId, () => this._dispatch(request, meshId));
|
|
1882
|
+
}
|
|
1883
|
+
return undefined;
|
|
1884
|
+
}
|
|
1885
|
+
const taken = await engine.takeRunRequest({ chunkId: request.id }).catch(noop);
|
|
1886
|
+
if (!taken) {
|
|
1887
|
+
this._seen.delete(runId);
|
|
1888
|
+
return undefined;
|
|
1889
|
+
}
|
|
1890
|
+
request = taken;
|
|
1891
|
+
}
|
|
1892
|
+
if (!drivenBy({ request, agent }, deviceId)) {
|
|
1893
|
+
return undefined;
|
|
1894
|
+
}
|
|
1895
|
+
if (await this._isHandled(engine, request)) {
|
|
1896
|
+
return undefined;
|
|
1897
|
+
}
|
|
1898
|
+
this._log?.info('runs', 'run claimed', { runId, chatId: request.chatId, agentId });
|
|
1899
|
+
if (!request.capability) {
|
|
1900
|
+
await this._offerSteer(engine, request, agent.id);
|
|
1901
|
+
}
|
|
1902
|
+
return { request, agent };
|
|
1903
|
+
}
|
|
1904
|
+
async _offerSteer(engine, request, agentId) {
|
|
1905
|
+
const running = [...this._runs.values()].find((run) => run.chatId === request.chatId && run.agentId === agentId && run.steering);
|
|
1906
|
+
if (!running?.steering || !request.runId) {
|
|
1907
|
+
return;
|
|
1908
|
+
}
|
|
1909
|
+
const asked = await engine.listChunks({ chatId: request.chatId, type: 'user-message' });
|
|
1910
|
+
const message = (asked.chunks ?? []).findLast((chunk) => chunk.seq < request.seq);
|
|
1911
|
+
const text = message?.userMessage?.text;
|
|
1912
|
+
if (text) {
|
|
1913
|
+
const [turn] = nameSenders([{ role: 'user', content: text, sender: senderOf(message) }]);
|
|
1914
|
+
running.steering.push({ mode: 'steer', text: turn.content, id: request.runId });
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
_requeue(runId, { meshId, reason, replay }) {
|
|
1918
|
+
const background = this._backgroundReplays.delete(runId);
|
|
1919
|
+
if (this._seen.has(runId) || this.closing) {
|
|
1920
|
+
return;
|
|
1921
|
+
}
|
|
1922
|
+
if (background && this._state === QvacState.PRESUSPEND) {
|
|
1923
|
+
this._parked.set(runId, replay);
|
|
1924
|
+
}
|
|
1925
|
+
else if ((background && !this._backgrounded) || reason === MODEL_UNLOADED) {
|
|
1926
|
+
replay();
|
|
1927
|
+
}
|
|
1928
|
+
else if (reason === AGENT_READ_CUT) {
|
|
1929
|
+
this._retakeOnSync(runId, meshId, replay);
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
_retakeOnSync(runId, meshId, replay) {
|
|
1933
|
+
const probe = !this._retakes.has(runId);
|
|
1934
|
+
this._retakes.get(runId)?.stop();
|
|
1935
|
+
const edge = watchEdge([
|
|
1936
|
+
this._engine.syncStatusWatch({ meshId }),
|
|
1937
|
+
this._engine.meshStatusWatch({})
|
|
1938
|
+
]);
|
|
1939
|
+
this._retakes.set(runId, edge);
|
|
1940
|
+
void (async () => {
|
|
1941
|
+
if (probe && (await edge.armed)) {
|
|
1942
|
+
replay();
|
|
1943
|
+
}
|
|
1944
|
+
const fired = await edge.fired;
|
|
1945
|
+
edge.stop();
|
|
1946
|
+
if (this._retakes.get(runId) === edge) {
|
|
1947
|
+
this._retakes.delete(runId);
|
|
1948
|
+
}
|
|
1949
|
+
if (fired) {
|
|
1950
|
+
replay();
|
|
1951
|
+
}
|
|
1952
|
+
})();
|
|
1953
|
+
}
|
|
1301
1954
|
async _citeWebSources(transcript, runId, result) {
|
|
1302
1955
|
const records = webSourcesFromToolResult(result, WEB_RAG_SOURCE_BOUNDS);
|
|
1303
1956
|
if (records.length === 0) {
|
|
1304
1957
|
return;
|
|
1305
1958
|
}
|
|
1306
1959
|
try {
|
|
1307
|
-
await transcript.chunk({ type: 'sources', sources: { sources: records } });
|
|
1960
|
+
await transcript.chunk({ type: 'sources', sources: { sources: chunkSources(records) } });
|
|
1308
1961
|
}
|
|
1309
1962
|
catch (err) {
|
|
1310
1963
|
this._log?.warn('assistant', 'web sources chunk skipped', { runId, err });
|
|
@@ -1331,7 +1984,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1331
1984
|
if (!search.enabled) {
|
|
1332
1985
|
return null;
|
|
1333
1986
|
}
|
|
1334
|
-
if (!search.hasKnowledge) {
|
|
1987
|
+
if (!search.hasKnowledge && !notReadyCount(search)) {
|
|
1335
1988
|
return null;
|
|
1336
1989
|
}
|
|
1337
1990
|
return { search, lastUser, hits: search.hits ?? [] };
|
|
@@ -1359,7 +2012,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1359
2012
|
if (knowledge.hits.some(isTruncatedFullTextHit)) {
|
|
1360
2013
|
await sinks.transcript.chunk({
|
|
1361
2014
|
type: 'sources',
|
|
1362
|
-
sources: { sources: [], notice: RAG_TRUNCATED_NOTICE }
|
|
2015
|
+
sources: { sources: [], notice: RAG_TRUNCATED_NOTICE, ...notReady(knowledge.search) }
|
|
1363
2016
|
});
|
|
1364
2017
|
injected.sourcesWritten = true;
|
|
1365
2018
|
}
|
|
@@ -1367,8 +2020,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1367
2020
|
else {
|
|
1368
2021
|
await injectRetrievedKnowledge(knowledge, { ...sinks, injected });
|
|
1369
2022
|
}
|
|
1370
|
-
|
|
1371
|
-
|
|
2023
|
+
const line = notReadyLine(knowledge.search);
|
|
2024
|
+
if (line) {
|
|
2025
|
+
knowledge.lastUser.content += `\n\n${line}`;
|
|
1372
2026
|
}
|
|
1373
2027
|
}
|
|
1374
2028
|
catch (err) {
|
|
@@ -1376,13 +2030,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1376
2030
|
}
|
|
1377
2031
|
return injected;
|
|
1378
2032
|
}
|
|
1379
|
-
async _writeUnreadNotice(transcript, reason) {
|
|
2033
|
+
async _writeUnreadNotice(transcript, reason, search) {
|
|
1380
2034
|
try {
|
|
1381
2035
|
await transcript.chunk({
|
|
1382
2036
|
type: 'sources',
|
|
1383
2037
|
sources: {
|
|
1384
2038
|
sources: [],
|
|
1385
|
-
notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE
|
|
2039
|
+
notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE,
|
|
2040
|
+
...(search && notReady(search))
|
|
1386
2041
|
}
|
|
1387
2042
|
});
|
|
1388
2043
|
}
|
|
@@ -1390,64 +2045,66 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1390
2045
|
this._log?.warn('assistant', 'unread-document notice skipped', err);
|
|
1391
2046
|
}
|
|
1392
2047
|
}
|
|
1393
|
-
async
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
return (chunks ?? []).some((c) => c.runId === request.runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
1399
|
-
}
|
|
1400
|
-
async _recordRunBoundaryStatus(engine, { request, runId, agent, deviceId, state, reason }) {
|
|
2048
|
+
async _openRunLog(engine, runId) {
|
|
2049
|
+
if (!keepsRunLog(await this._ensureCapability())) {
|
|
2050
|
+
return null;
|
|
2051
|
+
}
|
|
2052
|
+
const runLog = createHostRunLogAdapter(engine);
|
|
1401
2053
|
try {
|
|
1402
|
-
|
|
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
|
-
}
|
|
2054
|
+
return { runLog, lease: await runLog.begin(runId) };
|
|
1411
2055
|
}
|
|
1412
|
-
catch (
|
|
1413
|
-
this._log?.warn('runs', '
|
|
1414
|
-
|
|
1415
|
-
error
|
|
1416
|
-
});
|
|
2056
|
+
catch (err) {
|
|
2057
|
+
this._log?.warn('runs', 'run log not opened', { runId, err });
|
|
2058
|
+
return null;
|
|
1417
2059
|
}
|
|
1418
|
-
|
|
2060
|
+
}
|
|
2061
|
+
async _callsOf(engine, chatId, runId) {
|
|
2062
|
+
const { chunks } = await engine.listChunks({ chatId, type: 'tool-call' });
|
|
2063
|
+
return (chunks ?? []).filter((chunk) => chunk.runId === runId);
|
|
2064
|
+
}
|
|
2065
|
+
async _isHandled(engine, request) {
|
|
2066
|
+
const { chunks } = await engine.listChunks({
|
|
1419
2067
|
chatId: request.chatId,
|
|
1420
|
-
|
|
1421
|
-
deviceId,
|
|
1422
|
-
model: agent.modelName,
|
|
1423
|
-
log: this._log
|
|
2068
|
+
type: 'run-status'
|
|
1424
2069
|
});
|
|
1425
|
-
|
|
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
|
-
}
|
|
2070
|
+
return (chunks ?? []).some((c) => c.runId === request.runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
1435
2071
|
}
|
|
1436
2072
|
async _execute(request, agent, { controller, meshId, runId }) {
|
|
1437
2073
|
const engine = meshScoped(this._engine, meshId);
|
|
2074
|
+
const { chatId } = request;
|
|
2075
|
+
this._suggesting.get(chatId)?.abort(SUGGESTION_SUPERSEDED);
|
|
1438
2076
|
const deviceId = this._requireDeviceId();
|
|
1439
|
-
if (this.
|
|
2077
|
+
if (this._backgrounded) {
|
|
1440
2078
|
this._seen.delete(runId);
|
|
2079
|
+
this._backgroundReplays.add(runId);
|
|
1441
2080
|
return;
|
|
1442
2081
|
}
|
|
1443
2082
|
if (await this._isHandled(engine, request)) {
|
|
1444
2083
|
return;
|
|
1445
2084
|
}
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
fresh.
|
|
1449
|
-
|
|
1450
|
-
|
|
2085
|
+
let fresh;
|
|
2086
|
+
try {
|
|
2087
|
+
fresh = await engine.getAgent({ agentId: agent.id });
|
|
2088
|
+
}
|
|
2089
|
+
catch (err) {
|
|
2090
|
+
if (agentRefused(err)) {
|
|
2091
|
+
const refused = new RunTranscript(engine, {
|
|
2092
|
+
chatId,
|
|
2093
|
+
runId,
|
|
2094
|
+
deviceId,
|
|
2095
|
+
model: agent.modelName,
|
|
2096
|
+
log: this._log
|
|
2097
|
+
});
|
|
2098
|
+
refused.close();
|
|
2099
|
+
await refused.status({ state: 'failed', reason: runFailureNotice(err) });
|
|
2100
|
+
return;
|
|
2101
|
+
}
|
|
2102
|
+
this._log?.debug('runs', 'agent read cut short', { runId, err });
|
|
2103
|
+
this._seen.delete(runId);
|
|
2104
|
+
controller.abort(AGENT_READ_CUT);
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
if (fresh.deletedAt || !drivenBy({ request, agent: fresh }, deviceId)) {
|
|
1451
2108
|
return;
|
|
1452
2109
|
}
|
|
1453
2110
|
agent = fresh;
|
|
@@ -1463,121 +2120,40 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1463
2120
|
runId
|
|
1464
2121
|
});
|
|
1465
2122
|
}
|
|
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,
|
|
2123
|
+
const steeredInto = this._steered.get(runId);
|
|
2124
|
+
if (steeredInto) {
|
|
2125
|
+
this._steered.delete(runId);
|
|
2126
|
+
const steered = new RunTranscript(engine, {
|
|
2127
|
+
chatId,
|
|
1498
2128
|
runId,
|
|
1499
|
-
agent,
|
|
1500
2129
|
deviceId,
|
|
1501
|
-
|
|
1502
|
-
|
|
2130
|
+
model: agent.modelName,
|
|
2131
|
+
log: this._log
|
|
1503
2132
|
});
|
|
2133
|
+
steered.close();
|
|
2134
|
+
await steered.status({ state: 'completed', reason: STEERED, metrics: { steeredInto } });
|
|
1504
2135
|
return;
|
|
1505
2136
|
}
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
}
|
|
1518
|
-
catch (error) {
|
|
1519
|
-
if (controller.signal.aborted) {
|
|
1520
|
-
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1521
|
-
if (outcome.kind === 'replay') {
|
|
1522
|
-
this._seen.delete(runId);
|
|
1523
|
-
return;
|
|
1524
|
-
}
|
|
1525
|
-
if (outcome.kind === 'handoff') {
|
|
1526
|
-
return;
|
|
1527
|
-
}
|
|
1528
|
-
await this._recordRunBoundaryStatus(engine, {
|
|
1529
|
-
request,
|
|
1530
|
-
runId,
|
|
1531
|
-
agent,
|
|
1532
|
-
deviceId,
|
|
1533
|
-
state: 'failed',
|
|
1534
|
-
reason: outcome.reason
|
|
1535
|
-
});
|
|
1536
|
-
return;
|
|
1537
|
-
}
|
|
1538
|
-
await this._recordRunBoundaryStatus(engine, {
|
|
1539
|
-
request,
|
|
1540
|
-
runId,
|
|
1541
|
-
agent,
|
|
1542
|
-
deviceId,
|
|
1543
|
-
state: hostLease.boot?.metadata ? 'recovery-required' : 'failed',
|
|
1544
|
-
reason: runFailureNotice(error)
|
|
1545
|
-
});
|
|
1546
|
-
}
|
|
1547
|
-
finally {
|
|
1548
|
-
await runLog
|
|
1549
|
-
.release(runId, hostLease.generation)
|
|
1550
|
-
.catch((error) => this._log?.warn('runs', 'run-log lease release failed', { runId, error }));
|
|
1551
|
-
}
|
|
1552
|
-
}
|
|
1553
|
-
async _executePrimary({ request, agent, controller, meshId, runId, engine, runLog, hostLease }) {
|
|
1554
|
-
const { chatId } = request;
|
|
1555
|
-
const deviceId = this._requireDeviceId();
|
|
1556
|
-
const { boot } = hostLease;
|
|
1557
|
-
const resumed = Boolean(boot?.metadata);
|
|
1558
|
-
const savedRequest = resumed && boot ? asRecord(requestFromBoot(boot)) : null;
|
|
1559
|
-
if (resumed) {
|
|
1560
|
-
const { chunks } = await engine.listChunks({ chatId, type: 'run-request' });
|
|
1561
|
-
const persistedRequest = (chunks ?? []).find((chunk) => chunk.id === request.id);
|
|
1562
|
-
if (!persistedRequest ||
|
|
1563
|
-
persistedRequest.chatId !== chatId ||
|
|
1564
|
-
persistedRequest.runId !== runId ||
|
|
1565
|
-
persistedRequest.seq !== request.seq ||
|
|
1566
|
-
persistedRequest.parentId !== request.parentId ||
|
|
1567
|
-
persistedRequest.runRequest?.agentId !== agent.id) {
|
|
1568
|
-
throw new Error('The original run request is no longer present in its chat history');
|
|
1569
|
-
}
|
|
1570
|
-
}
|
|
2137
|
+
const hold = await this._openRunLog(engine, runId);
|
|
2138
|
+
const runLog = hold?.runLog;
|
|
2139
|
+
const logged = Boolean(hold?.lease.boot?.metadata);
|
|
2140
|
+
const carried = logged || !request.runnerDeviceId
|
|
2141
|
+
? null
|
|
2142
|
+
: carriedRun(await listRoundChunks(engine, chatId), runId);
|
|
2143
|
+
const resumed = logged || carried !== null;
|
|
2144
|
+
const releaseRunLog = async () => {
|
|
2145
|
+
await hold?.runLog
|
|
2146
|
+
.release(runId, hold.lease.generation)
|
|
2147
|
+
.catch((err) => this._log?.warn('runs', 'run log not released', { runId, err }));
|
|
2148
|
+
};
|
|
1571
2149
|
if (!resumed) {
|
|
1572
2150
|
await this._reconcileCompaction(engine, chatId);
|
|
2151
|
+
if (request.parentId) {
|
|
2152
|
+
await this._deleteChatKvCache(chatId, 'superseding run');
|
|
2153
|
+
}
|
|
1573
2154
|
}
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
}
|
|
1577
|
-
const lender = this._lenderFor(agent);
|
|
1578
|
-
const model = lender
|
|
1579
|
-
? agent.modelName
|
|
1580
|
-
: runnableModelName(agent.modelName, await this._runnableHost());
|
|
2155
|
+
const servedElsewhere = !b4a.equals(agent.providerDeviceId, deviceId);
|
|
2156
|
+
const model = await this._runModel(agent, deviceId);
|
|
1581
2157
|
if (model !== agent.modelName) {
|
|
1582
2158
|
this._log?.warn('runs', 'model clamped to what this device can run', {
|
|
1583
2159
|
runId,
|
|
@@ -1588,7 +2164,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1588
2164
|
}
|
|
1589
2165
|
const claimed = this._runs.get(runId);
|
|
1590
2166
|
if (claimed) {
|
|
1591
|
-
this._runs.set(runId, { ...claimed, model
|
|
2167
|
+
this._runs.set(runId, { ...claimed, model });
|
|
1592
2168
|
}
|
|
1593
2169
|
const config = await engine.getConfig({}).catch(noop);
|
|
1594
2170
|
const configuredAgent = withConfigDefaults(agent, config);
|
|
@@ -1603,28 +2179,138 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1603
2179
|
runId,
|
|
1604
2180
|
deviceId,
|
|
1605
2181
|
model,
|
|
2182
|
+
unavailableSkills: await this._skillsNotHere(agent),
|
|
1606
2183
|
log: this._log
|
|
1607
2184
|
});
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
2185
|
+
const rounds = new OwnerRounds({
|
|
2186
|
+
engine,
|
|
2187
|
+
chatId,
|
|
2188
|
+
runId,
|
|
2189
|
+
agentId: agent.id,
|
|
2190
|
+
deviceId,
|
|
2191
|
+
from: request.seq,
|
|
2192
|
+
media: (file) => this._mediaRefs.get(path.basename(file)),
|
|
2193
|
+
meshId,
|
|
2194
|
+
local: this._harness.serveMediaRound?.bind(this._harness),
|
|
2195
|
+
inspect: (call) => void this._inspectRound(chatId, { runId, at: Date.now(), call })
|
|
2196
|
+
});
|
|
2197
|
+
await rounds.retire();
|
|
2198
|
+
const folded = !resumed &&
|
|
2199
|
+
(await this._compactIfNearFull({
|
|
2200
|
+
chatId,
|
|
2201
|
+
transcript,
|
|
2202
|
+
trigger: 'before-run',
|
|
2203
|
+
signal: controller.signal,
|
|
2204
|
+
meshId,
|
|
2205
|
+
run: { request, runId },
|
|
2206
|
+
rounds
|
|
2207
|
+
}));
|
|
2208
|
+
const toolConfigsPromise = this._loadToolConfigs(engine, agent.id);
|
|
2209
|
+
const fsGrantsPromise = this._loadFsGrants(agent.id);
|
|
2210
|
+
const all = await listHistoryChunks(engine, chatId);
|
|
2211
|
+
const { chunks: switches } = await engine.listChunks({ chatId, type: 'agent-switch' });
|
|
2212
|
+
const ranAndSwitched = [...all, ...(switches ?? [])].sort((a, b) => a.seq - b.seq);
|
|
2213
|
+
if (!resumed && agentSwitched(ranAndSwitched, request, deviceId)) {
|
|
2214
|
+
await this._deleteChatKvCache(chatId, 'agent switched');
|
|
2215
|
+
}
|
|
2216
|
+
const { chunks: priorAsks } = await engine.listChunks({
|
|
2217
|
+
chatId,
|
|
2218
|
+
type: 'approval-request'
|
|
2219
|
+
});
|
|
2220
|
+
const { history, stale, repair: currentRepair, acceptedRepairs } = runPromptView(all, priorAsks ?? [], { request, runId });
|
|
2221
|
+
const priorResults = all.filter((chunk) => chunk.type === 'tool-result');
|
|
2222
|
+
let ran = priorResults.some((chunk) => chunk.runId === runId &&
|
|
2223
|
+
chunk.toolResult?.name !== SKILL_TOOL &&
|
|
2224
|
+
!isDeniedResult(chunk.toolResult?.result));
|
|
2225
|
+
let replay = await this._sentRuns(chatId, await listRoundChunks(engine, chatId));
|
|
2226
|
+
if (!resumed && viewOutgrewCache(all, { request, runId }, replay.sentRuns)) {
|
|
2227
|
+
await this._deleteChatKvCache(chatId, 'a run outside the cached view');
|
|
2228
|
+
replay = {
|
|
2229
|
+
sentRuns: new Map(),
|
|
2230
|
+
askedRuns: new Map(),
|
|
2231
|
+
readRuns: new Map(),
|
|
2232
|
+
rebuild: await this._runReplay.rebuild({ holder: LOCAL_HOLDER, chatId })
|
|
2233
|
+
};
|
|
2234
|
+
}
|
|
2235
|
+
const messages = historyFromChunks(history, stale, replay);
|
|
2236
|
+
const typedText = currentRepair
|
|
2237
|
+
? undefined
|
|
2238
|
+
: history.findLast((chunk) => chunk.userMessage)?.userMessage?.text;
|
|
2239
|
+
if (currentRepair) {
|
|
2240
|
+
messages.push({ role: 'user', content: currentRepair.promptText });
|
|
2241
|
+
}
|
|
2242
|
+
const diagramRepair = currentRepair !== undefined;
|
|
2243
|
+
const runAgent = diagramRepair
|
|
2244
|
+
? {
|
|
2245
|
+
...configuredAgent,
|
|
2246
|
+
toolsScope: 'selected',
|
|
2247
|
+
enabledTools: [],
|
|
2248
|
+
skillsScope: 'selected',
|
|
2249
|
+
enabledSkills: []
|
|
2250
|
+
}
|
|
2251
|
+
: configuredAgent;
|
|
2252
|
+
const carriesMedia = messages.some((turn) => turn.images?.length || turn.pdfs?.length || turn.videos?.length);
|
|
2253
|
+
const seesImages = carriesMedia && (await this._seesImages(model));
|
|
2254
|
+
let unreadableImages = new Map();
|
|
2255
|
+
const sampled = [];
|
|
2256
|
+
rounds.share = async (agentId) => {
|
|
2257
|
+
const host = await engine.getAgent({ agentId }).catch(() => null);
|
|
2258
|
+
if (sampled.length && host && !b4a.equals(host.providerDeviceId, deviceId)) {
|
|
2259
|
+
await this._storeVideoDigests(sampled.splice(0), { meshId, chatId, runId, deviceId });
|
|
2260
|
+
}
|
|
2261
|
+
};
|
|
2262
|
+
let pdfPages = { rendered: new Set(), notices: [] };
|
|
2263
|
+
const selectedPdfs = [];
|
|
2264
|
+
if (!seesImages) {
|
|
2265
|
+
const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
|
|
2266
|
+
if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
|
|
2267
|
+
this._log?.warn('assistant', `no projection pairing for ${model} — dropped ${dropped} image(s)`);
|
|
2268
|
+
}
|
|
2269
|
+
for (const turn of messages) {
|
|
2270
|
+
if (turn.images?.length) {
|
|
2271
|
+
const names = turn.images.map((image) => image.fileName).join(', ');
|
|
2272
|
+
turn.content += `\n[Attached image${turn.images.length > 1 ? 's' : ''}: ${names} — not visible to this model]`;
|
|
2273
|
+
}
|
|
2274
|
+
delete turn.images;
|
|
2275
|
+
delete turn.pdfs;
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
else {
|
|
2279
|
+
unreadableImages = await this._hydrateImages(messages, controller.signal, runId);
|
|
2280
|
+
}
|
|
2281
|
+
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
2282
|
+
const ownershipWatch = this._watchOwnership(engine, { request, agentId: agent.id, controller });
|
|
2283
|
+
let readinessError = null;
|
|
2284
|
+
const held = { status: null };
|
|
2285
|
+
let shown = '';
|
|
2286
|
+
const showWait = (progress) => {
|
|
2287
|
+
const key = JSON.stringify(progress);
|
|
2288
|
+
if (key === shown || (!progress && !held.status)) {
|
|
1616
2289
|
return;
|
|
1617
2290
|
}
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
2291
|
+
shown = key;
|
|
2292
|
+
const runStatus = progress
|
|
2293
|
+
? { state: 'executing', progress }
|
|
2294
|
+
: { state: 'executing' };
|
|
2295
|
+
if (!held.status) {
|
|
2296
|
+
held.status = transcript.status(runStatus, { resumed });
|
|
2297
|
+
return;
|
|
2298
|
+
}
|
|
2299
|
+
held.status.then(({ id }) => transcript.progress(id, runStatus)).catch(noop);
|
|
1626
2300
|
};
|
|
1627
|
-
|
|
2301
|
+
try {
|
|
2302
|
+
if (!servedElsewhere) {
|
|
2303
|
+
await this._downloads?.waitUntilCached(model, controller.signal, {
|
|
2304
|
+
onHold: () => showWait({ phase: 'awaiting-download', model }),
|
|
2305
|
+
onProgress: (percentage) => showWait(downloadingProgress(model, percentage)),
|
|
2306
|
+
onStall: () => showWait(null)
|
|
2307
|
+
});
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
catch (err) {
|
|
2311
|
+
readinessError = err instanceof Error ? err : new Error(String(err));
|
|
2312
|
+
}
|
|
2313
|
+
if (controller.signal.aborted) {
|
|
1628
2314
|
try {
|
|
1629
2315
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1630
2316
|
if (outcome.kind === 'replay') {
|
|
@@ -1635,603 +2321,562 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1635
2321
|
}
|
|
1636
2322
|
}
|
|
1637
2323
|
finally {
|
|
1638
|
-
|
|
2324
|
+
transcript.close();
|
|
2325
|
+
ownershipWatch.stop();
|
|
2326
|
+
settledWatch.stop();
|
|
2327
|
+
await releaseRunLog();
|
|
2328
|
+
}
|
|
2329
|
+
return;
|
|
2330
|
+
}
|
|
2331
|
+
if (readinessError) {
|
|
2332
|
+
try {
|
|
2333
|
+
await transcript.status({ state: 'failed', reason: runFailureNotice(readinessError) });
|
|
2334
|
+
}
|
|
2335
|
+
finally {
|
|
2336
|
+
transcript.close();
|
|
2337
|
+
ownershipWatch.stop();
|
|
2338
|
+
settledWatch.stop();
|
|
2339
|
+
await releaseRunLog();
|
|
2340
|
+
}
|
|
2341
|
+
return;
|
|
2342
|
+
}
|
|
2343
|
+
const inflight = this._runs.get(runId);
|
|
2344
|
+
if (inflight) {
|
|
2345
|
+
this._runs.set(runId, { ...inflight, executing: true });
|
|
2346
|
+
}
|
|
2347
|
+
this._runsChanged();
|
|
2348
|
+
const busyChatId = this._modelHolder(runId, chatId, model);
|
|
2349
|
+
const runStatus = busyChatId
|
|
2350
|
+
? { state: 'executing', progress: { phase: 'queued', busyChatId } }
|
|
2351
|
+
: { state: 'executing' };
|
|
2352
|
+
const runStatusId = (await (held.status ?? transcript.status(runStatus, { resumed }))).id;
|
|
2353
|
+
if (held.status) {
|
|
2354
|
+
await transcript.progress(runStatusId, runStatus);
|
|
2355
|
+
}
|
|
2356
|
+
const earlierCalls = carried ? await this._callsOf(engine, chatId, runId) : [];
|
|
2357
|
+
const roundBase = carried?.firstRound ?? 0;
|
|
2358
|
+
const approvals = this._createApprove(engine, {
|
|
2359
|
+
chatId,
|
|
2360
|
+
runId,
|
|
2361
|
+
deviceId,
|
|
2362
|
+
agentId: agent.id,
|
|
2363
|
+
hasRun: () => ran,
|
|
2364
|
+
repeats: (call) => earlierCalls.some((chunk) => sameToolCall(chunk, call))
|
|
2365
|
+
});
|
|
2366
|
+
const idle = idleWatch(controller, this._runIdleTimeoutMs);
|
|
2367
|
+
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
|
|
2368
|
+
let metrics;
|
|
2369
|
+
const sourceTagFilter = createRagSourceTagFilter();
|
|
2370
|
+
const retrievedRecords = [];
|
|
2371
|
+
const answers = new Map();
|
|
2372
|
+
let round;
|
|
2373
|
+
const answer = (text) => {
|
|
2374
|
+
answers.set(round, (answers.get(round) ?? '') + text);
|
|
2375
|
+
};
|
|
2376
|
+
const flushTranscript = async () => {
|
|
2377
|
+
const tail = sourceTagFilter.flush();
|
|
2378
|
+
if (tail) {
|
|
2379
|
+
answer(tail);
|
|
2380
|
+
await transcript.text('content', tail, round);
|
|
1639
2381
|
}
|
|
2382
|
+
await transcript.flush();
|
|
1640
2383
|
};
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
2384
|
+
let titleOutcome = null;
|
|
2385
|
+
let runFailure = null;
|
|
2386
|
+
let runVerdict = 'completed';
|
|
2387
|
+
let sentRun = null;
|
|
2388
|
+
let steering = null;
|
|
2389
|
+
const taken = new Set();
|
|
2390
|
+
let inputLength = 0;
|
|
2391
|
+
let asked;
|
|
2392
|
+
let primeRound;
|
|
2393
|
+
let primeTokens;
|
|
2394
|
+
let injectedHits = 0;
|
|
2395
|
+
const writeText = async (type, raw) => {
|
|
2396
|
+
const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
|
|
2397
|
+
if (!text) {
|
|
1648
2398
|
return;
|
|
1649
2399
|
}
|
|
1650
|
-
if (
|
|
1651
|
-
|
|
2400
|
+
if (type === 'content') {
|
|
2401
|
+
answer(text);
|
|
2402
|
+
}
|
|
2403
|
+
await transcript.text(type, text, round);
|
|
2404
|
+
};
|
|
2405
|
+
const writeRoundText = async (type, text) => {
|
|
2406
|
+
const own = rounds.unechoed(type, text);
|
|
2407
|
+
if (type === 'content' && own.length < text.length) {
|
|
2408
|
+
answer(text.slice(0, text.length - own.length));
|
|
2409
|
+
}
|
|
2410
|
+
if (own) {
|
|
2411
|
+
await writeText(type, own);
|
|
2412
|
+
}
|
|
2413
|
+
};
|
|
2414
|
+
const failRun = async (message) => {
|
|
2415
|
+
const overflowed = isContextOverflowFailure(message);
|
|
2416
|
+
const folded = overflowed &&
|
|
2417
|
+
(await this._runCompaction({
|
|
1652
2418
|
chatId,
|
|
1653
2419
|
transcript,
|
|
1654
|
-
trigger: '
|
|
2420
|
+
trigger: 'overflow',
|
|
1655
2421
|
signal: controller.signal,
|
|
1656
|
-
meshId
|
|
1657
|
-
|
|
2422
|
+
meshId,
|
|
2423
|
+
run: { request, runId },
|
|
2424
|
+
rounds
|
|
2425
|
+
}));
|
|
2426
|
+
const shrunk = overflowed && !folded && injectedHits > 1;
|
|
2427
|
+
if (shrunk) {
|
|
2428
|
+
this._knowledgeCaps.set(chatId, Math.floor(injectedHits / 2));
|
|
2429
|
+
this._log?.info('runs', 'knowledge shrunk for retry', { runId, hits: injectedHits });
|
|
2430
|
+
}
|
|
2431
|
+
runFailure = runFailureNotice(message);
|
|
2432
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
2433
|
+
if (folded || shrunk) {
|
|
2434
|
+
await engine
|
|
2435
|
+
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
2436
|
+
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
2437
|
+
}
|
|
2438
|
+
titleOutcome = 'ended';
|
|
2439
|
+
};
|
|
2440
|
+
const endAbortedRun = async () => {
|
|
2441
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
2442
|
+
if (outcome.kind === 'replay') {
|
|
2443
|
+
runVerdict = 'replay';
|
|
2444
|
+
this._seen.delete(runId);
|
|
2445
|
+
return;
|
|
1658
2446
|
}
|
|
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);
|
|
2447
|
+
if (outcome.kind === 'handoff') {
|
|
2448
|
+
runVerdict = 'handoff';
|
|
2449
|
+
return;
|
|
1735
2450
|
}
|
|
1736
|
-
|
|
1737
|
-
|
|
2451
|
+
runFailure = outcome.reason;
|
|
2452
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
2453
|
+
titleOutcome = 'ended';
|
|
2454
|
+
};
|
|
2455
|
+
const pauseRun = async () => {
|
|
2456
|
+
if (this._abortOutcome(runId, controller.signal.reason).kind === 'replay') {
|
|
2457
|
+
await transcript.status({ state: 'paused', reason: RUN_PAUSED });
|
|
1738
2458
|
}
|
|
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
|
-
}
|
|
2459
|
+
await endAbortedRun();
|
|
2460
|
+
if (runVerdict === 'replay') {
|
|
2461
|
+
runVerdict = 'paused';
|
|
1748
2462
|
}
|
|
1749
|
-
|
|
1750
|
-
|
|
2463
|
+
};
|
|
2464
|
+
const resetRound = async (event) => {
|
|
2465
|
+
round = roundOf(event);
|
|
2466
|
+
const replaced = replacedTypes(event);
|
|
2467
|
+
if (replaced.includes('content')) {
|
|
2468
|
+
answers.delete(round);
|
|
2469
|
+
}
|
|
2470
|
+
if (!rounds.retook()) {
|
|
2471
|
+
await transcript.roundReset(round ?? 0, replaced);
|
|
1751
2472
|
}
|
|
2473
|
+
};
|
|
2474
|
+
const holdForRecovery = async (event) => {
|
|
2475
|
+
runVerdict = 'recovery-required';
|
|
2476
|
+
await transcript.status({ state: 'recovery-required', reason: recoveryReason(event) });
|
|
2477
|
+
titleOutcome = 'ended';
|
|
2478
|
+
};
|
|
2479
|
+
const recordToolResult = async (event) => {
|
|
2480
|
+
if (isDeniedResult(event.result)) {
|
|
2481
|
+
this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
|
|
2482
|
+
}
|
|
2483
|
+
else if (event.name !== SKILL_TOOL) {
|
|
2484
|
+
ran = true;
|
|
2485
|
+
}
|
|
2486
|
+
if (WEB_TOOLS.includes(String(event.name))) {
|
|
2487
|
+
await this._citeWebSources(transcript, runId, event.result);
|
|
2488
|
+
}
|
|
2489
|
+
await transcript.chunk(toolResultChunk(event));
|
|
2490
|
+
};
|
|
2491
|
+
try {
|
|
2492
|
+
const attachTurnIds = resumed ? [] : attachTurnDocumentIds(history);
|
|
2493
|
+
if (attachTurnIds.length > 0) {
|
|
2494
|
+
backfillDocumentOnlyTurn(messages);
|
|
2495
|
+
}
|
|
2496
|
+
const knowledge = !resumed &&
|
|
2497
|
+
!diagramRepair &&
|
|
2498
|
+
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
2499
|
+
? await this._searchKnowledge(engine, messages, {
|
|
2500
|
+
chatId,
|
|
2501
|
+
attachmentIds: attachTurnIds,
|
|
2502
|
+
signal: controller.signal,
|
|
2503
|
+
preparing: (active) => {
|
|
2504
|
+
this._markPreparing(runId, active);
|
|
2505
|
+
modelWatch.documents(active);
|
|
2506
|
+
}
|
|
2507
|
+
})
|
|
2508
|
+
: null;
|
|
1752
2509
|
if (controller.signal.aborted) {
|
|
1753
|
-
|
|
2510
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
2511
|
+
if (outcome.kind === 'replay') {
|
|
2512
|
+
runVerdict = 'replay';
|
|
2513
|
+
this._seen.delete(runId);
|
|
2514
|
+
return;
|
|
2515
|
+
}
|
|
2516
|
+
if (outcome.kind === 'handoff') {
|
|
2517
|
+
runVerdict = 'handoff';
|
|
2518
|
+
return;
|
|
2519
|
+
}
|
|
2520
|
+
runFailure = outcome.reason;
|
|
2521
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1754
2522
|
return;
|
|
1755
2523
|
}
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
2524
|
+
const cap = this._knowledgeCaps.get(chatId);
|
|
2525
|
+
this._knowledgeCaps.delete(chatId);
|
|
2526
|
+
const fitted = knowledge && cap ? { ...knowledge, hits: knowledge.hits.slice(0, cap) } : knowledge;
|
|
2527
|
+
const injected = fitted
|
|
2528
|
+
? await this._injectKnowledge(fitted, {
|
|
2529
|
+
transcript,
|
|
2530
|
+
sourceTagFilter,
|
|
2531
|
+
retrievedRecords,
|
|
2532
|
+
pictured: seesImages && messages.some((turn) => turn.pdfs?.length)
|
|
2533
|
+
})
|
|
2534
|
+
: { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
2535
|
+
injectedHits = injected.ragInjected && fitted ? fitted.hits.length : 0;
|
|
2536
|
+
const hasAudio = messages.some((turn) => turn.audioAttachments?.length || turn.videos?.length);
|
|
2537
|
+
if (hasAudio) {
|
|
2538
|
+
modelWatch.transcribe(true);
|
|
2539
|
+
}
|
|
2540
|
+
await this._hydrateAudio(messages, controller.signal);
|
|
2541
|
+
if (messages.some((turn) => turn.videos?.length)) {
|
|
2542
|
+
await this._hydrateVideos(messages, {
|
|
2543
|
+
signal: controller.signal,
|
|
2544
|
+
runId,
|
|
2545
|
+
seesImages,
|
|
2546
|
+
reading: await this._contextReading({ chatId, meshId, folded }, configuredAgent.ctxSize),
|
|
2547
|
+
sampled
|
|
2548
|
+
});
|
|
2549
|
+
}
|
|
2550
|
+
if (hasAudio) {
|
|
2551
|
+
modelWatch.transcribe(false);
|
|
2552
|
+
}
|
|
2553
|
+
backfillImageOnlyTurns(messages);
|
|
2554
|
+
if (seesImages) {
|
|
2555
|
+
const hits = injected.documentsInjected && fitted ? fitted.hits : [];
|
|
2556
|
+
pdfPages = await this._hydratePdfPages(messages, {
|
|
2557
|
+
signal: controller.signal,
|
|
2558
|
+
runId,
|
|
2559
|
+
window: { ctxSize: configuredAgent.ctxSize, modelName: model },
|
|
2560
|
+
reading: { chatId, meshId, folded },
|
|
2561
|
+
textChars: hits.reduce((chars, hit) => chars + hit.content.length, 0),
|
|
2562
|
+
wholeIds: new Set(hits
|
|
2563
|
+
.filter((hit) => !isTruncatedFullTextHit(hit))
|
|
2564
|
+
.map(fullTextAttachmentId)
|
|
2565
|
+
.filter((id) => id !== null)),
|
|
2566
|
+
selected: selectedPdfs
|
|
2567
|
+
});
|
|
2568
|
+
}
|
|
2569
|
+
annotateDocumentAttachments(messages);
|
|
2570
|
+
for (const { turn, notice } of pdfPages.notices) {
|
|
2571
|
+
turn.content = `${turn.content}\n${notice}`;
|
|
2572
|
+
}
|
|
2573
|
+
const unreadIds = attachTurnIds.filter((id) => !pdfPages.rendered.has(id));
|
|
2574
|
+
if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
|
|
2575
|
+
const reason = documentUnreadReason({
|
|
2576
|
+
knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
|
|
2577
|
+
failedFiles: knowledge?.search?.failedFiles,
|
|
2578
|
+
attachedCount: unreadIds.length
|
|
2579
|
+
});
|
|
2580
|
+
const lastUser = messages.findLast((turn) => turn.role === 'user');
|
|
2581
|
+
if (lastUser) {
|
|
2582
|
+
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
|
|
1759
2583
|
}
|
|
1760
|
-
|
|
1761
|
-
await
|
|
2584
|
+
if (!injected.sourcesWritten) {
|
|
2585
|
+
await this._writeUnreadNotice(transcript, reason, knowledge?.search);
|
|
1762
2586
|
}
|
|
2587
|
+
}
|
|
2588
|
+
annotateUnreadableImages(unreadableImages);
|
|
2589
|
+
const toolConfigs = await toolConfigsPromise;
|
|
2590
|
+
const { roots, write } = await fsGrantsPromise;
|
|
2591
|
+
const { grantedDirs, grantedDrives, driveOp } = await resolveFsGrants(roots, this._drives, (key, err) => this._log?.warn('assistant', 'drive grant dropped', {
|
|
2592
|
+
key,
|
|
2593
|
+
error: errorMessage(err)
|
|
2594
|
+
}));
|
|
2595
|
+
if (await this._isHandled(engine, request)) {
|
|
1763
2596
|
return;
|
|
1764
2597
|
}
|
|
2598
|
+
const cacheKey = diagramRepair || stale.runIds.size > 0
|
|
2599
|
+
? undefined
|
|
2600
|
+
: chatRepairKvCacheKey(chatId, acceptedRepairs);
|
|
2601
|
+
sentRun = trackSentRun(cacheKey, (file) => wholeImageId(this._mediaRefs.get(path.basename(file))));
|
|
2602
|
+
if (cacheKey) {
|
|
2603
|
+
this._recordKvCache(meshId, chatId);
|
|
2604
|
+
}
|
|
2605
|
+
const unloading = this._unloading.get(model);
|
|
2606
|
+
if (unloading) {
|
|
2607
|
+
await raceAbort(unloading.catch(noop), controller.signal);
|
|
2608
|
+
}
|
|
2609
|
+
this._log?.info('runs', 'completion requested', {
|
|
2610
|
+
runId,
|
|
2611
|
+
model,
|
|
2612
|
+
cacheKey,
|
|
2613
|
+
turns: messages.length
|
|
2614
|
+
});
|
|
2615
|
+
const resent = loggedMessages(hold?.lease.boot);
|
|
2616
|
+
if (resent) {
|
|
2617
|
+
asked = askedIn(resent, messages.findLast((turn) => turn.role === 'user')?.sender);
|
|
2618
|
+
}
|
|
2619
|
+
else {
|
|
2620
|
+
restoreSentContent(messages);
|
|
2621
|
+
asked = messages.findLast((turn) => turn.role === 'user')?.content;
|
|
2622
|
+
}
|
|
2623
|
+
const named = nameSenders(messages);
|
|
2624
|
+
const input = resent ?? (carried ? carriedInput(named, carried) : named);
|
|
2625
|
+
inputLength = resent ? input.length : named.length;
|
|
2626
|
+
rounds.inputLength = inputLength;
|
|
2627
|
+
rounds.roundBase = roundBase;
|
|
2628
|
+
const sentImages = earlierImages(messages);
|
|
2629
|
+
const inspecting = await this._capturingInferenceCalls();
|
|
2630
|
+
steering = new RunInputQueue();
|
|
1765
2631
|
const inflight = this._runs.get(runId);
|
|
1766
2632
|
if (inflight) {
|
|
1767
|
-
|
|
2633
|
+
inflight.steering = steering;
|
|
1768
2634
|
}
|
|
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,
|
|
2635
|
+
idle.touch();
|
|
2636
|
+
const events = this._harness.completion({
|
|
1787
2637
|
runId,
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
2638
|
+
runLog,
|
|
2639
|
+
model,
|
|
2640
|
+
agent: runAgent,
|
|
2641
|
+
messages: input,
|
|
2642
|
+
typedText,
|
|
2643
|
+
cacheKey,
|
|
2644
|
+
replaysContext: true,
|
|
2645
|
+
deferTools: runAgent.shared === true || !!request.requesterMemberId,
|
|
2646
|
+
modelRound: idle.parks(rounds.port),
|
|
2647
|
+
mediaRound: b4a.equals(agent.providerDeviceId, deviceId)
|
|
2648
|
+
? undefined
|
|
2649
|
+
: idle.parks(rounds.media),
|
|
2650
|
+
session: chatKvCacheKey(chatId),
|
|
2651
|
+
thinkingDefault: thinkingDefault(config),
|
|
2652
|
+
toolConfigs,
|
|
2653
|
+
grantedDirs,
|
|
2654
|
+
grantedDrives,
|
|
2655
|
+
driveOp,
|
|
2656
|
+
grantedWriteDirs: write,
|
|
2657
|
+
approve: idle.parks(approvals.approve),
|
|
2658
|
+
attach: (input) => this._uploadGenerated(input, {
|
|
2659
|
+
meshId,
|
|
2660
|
+
chatId,
|
|
2661
|
+
runId,
|
|
2662
|
+
deviceId,
|
|
2663
|
+
blobId: rounds.generated(input.bytes)
|
|
2664
|
+
}),
|
|
2665
|
+
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
2666
|
+
showImage: ({ attachmentId }) => this._showImage(engine, { chatId, runId, model, signal: controller.signal }, attachmentId),
|
|
2667
|
+
progress: (update) => {
|
|
2668
|
+
idle.touch();
|
|
2669
|
+
modelWatch.image(update);
|
|
2670
|
+
},
|
|
2671
|
+
generationParams: diagramRepair ? { reasoning_budget: 0 } : undefined,
|
|
2672
|
+
captureInferenceCalls: true,
|
|
2673
|
+
input: steering,
|
|
2674
|
+
signal: controller.signal
|
|
1791
2675
|
});
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
};
|
|
1803
|
-
const flushTranscript = async () => {
|
|
1804
|
-
const tail = sourceTagFilter.flush();
|
|
1805
|
-
if (tail) {
|
|
1806
|
-
appendAnswer(activeRound, tail);
|
|
1807
|
-
await transcript.text('content', tail, activeRound);
|
|
2676
|
+
let harnessEvents = 0;
|
|
2677
|
+
for await (const raw of events) {
|
|
2678
|
+
const event = afterRounds(raw, roundBase);
|
|
2679
|
+
harnessEvents++;
|
|
2680
|
+
idle.touch();
|
|
2681
|
+
modelWatch.clear();
|
|
2682
|
+
if (isHarnessEvent(event, 'content') || isHarnessEvent(event, 'thinking')) {
|
|
2683
|
+
round = roundOf(event) ?? round;
|
|
2684
|
+
await writeRoundText(event.type, event.text);
|
|
2685
|
+
continue;
|
|
1808
2686
|
}
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
2687
|
+
if (event.type === 'run-started' ||
|
|
2688
|
+
event.type === 'run-resumed' ||
|
|
2689
|
+
event.type === 'context') {
|
|
2690
|
+
continue;
|
|
2691
|
+
}
|
|
2692
|
+
if (event.type === 'input' && typeof event.id === 'string') {
|
|
2693
|
+
taken.add(event.id);
|
|
2694
|
+
continue;
|
|
2695
|
+
}
|
|
2696
|
+
if (isHarnessEvent(event, 'metrics')) {
|
|
2697
|
+
const { type, ...rest } = event;
|
|
2698
|
+
metrics = rest;
|
|
2699
|
+
primeTokens = rest.rounds?.find((each) => each.round === primeRound)?.promptTokens;
|
|
2700
|
+
recordBackends(this._backendUse, rest.backends);
|
|
2701
|
+
this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
|
|
2702
|
+
continue;
|
|
2703
|
+
}
|
|
2704
|
+
if (event.type === 'inference-sent' && event.outcome === 'overflowed') {
|
|
2705
|
+
this._log?.info('runs', 'context overflow', { runId, reason: event.reason });
|
|
2706
|
+
}
|
|
2707
|
+
if (event.type === 'inference-sent') {
|
|
2708
|
+
this._inspectSent(inspecting, chatId, {
|
|
2709
|
+
runId,
|
|
2710
|
+
round: Number(event.round ?? 0),
|
|
2711
|
+
messages: event.messages,
|
|
2712
|
+
tokens: event.tokens,
|
|
2713
|
+
outcome: event.outcome,
|
|
2714
|
+
reason: event.reason
|
|
2715
|
+
});
|
|
2716
|
+
continue;
|
|
2717
|
+
}
|
|
2718
|
+
if (event.type === 'inference-call') {
|
|
2719
|
+
const call = asRecord(event.call);
|
|
2720
|
+
sentRun?.observe(call);
|
|
2721
|
+
primeRound ??= sentRun?.onKey ? Number(call.round ?? 0) : undefined;
|
|
2722
|
+
modelWatch.request(call.messages, sentImages);
|
|
2723
|
+
this._inspectCall(inspecting, chatId, { runId, at: Date.now(), call });
|
|
2724
|
+
continue;
|
|
2725
|
+
}
|
|
2726
|
+
await flushTranscript();
|
|
2727
|
+
if (event.type === 'error' && !this._backgrounded) {
|
|
2728
|
+
await failRun(event.message);
|
|
1826
2729
|
return;
|
|
1827
2730
|
}
|
|
1828
|
-
if (type === '
|
|
1829
|
-
|
|
2731
|
+
if (event.type === 'error') {
|
|
2732
|
+
this._log?.info('runs', 'background error replayed', { runId, message: event.message });
|
|
1830
2733
|
}
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
const failRun = async (message) => {
|
|
1834
|
-
const folded = isContextOverflowFailure(message) &&
|
|
1835
|
-
(await this._runCompaction({
|
|
1836
|
-
chatId,
|
|
1837
|
-
transcript,
|
|
1838
|
-
trigger: 'overflow',
|
|
1839
|
-
signal: controller.signal,
|
|
1840
|
-
meshId
|
|
1841
|
-
}));
|
|
1842
|
-
runFailure = runFailureNotice(message);
|
|
1843
|
-
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
1844
|
-
if (folded) {
|
|
1845
|
-
await engine
|
|
1846
|
-
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
1847
|
-
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
1848
|
-
}
|
|
1849
|
-
titleOutcome = 'ended';
|
|
1850
|
-
};
|
|
1851
|
-
const endAbortedRun = async () => {
|
|
1852
|
-
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1853
|
-
if (outcome.kind === 'replay') {
|
|
1854
|
-
runVerdict = 'replay';
|
|
1855
|
-
this._seen.delete(runId);
|
|
2734
|
+
if (event.type === 'error' || event.type === 'aborted') {
|
|
2735
|
+
await endAbortedRun();
|
|
1856
2736
|
return;
|
|
1857
2737
|
}
|
|
1858
|
-
if (
|
|
1859
|
-
|
|
2738
|
+
if (event.type === 'paused') {
|
|
2739
|
+
await pauseRun();
|
|
1860
2740
|
return;
|
|
1861
2741
|
}
|
|
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 });
|
|
2742
|
+
if (event.type === 'recovery-required') {
|
|
2743
|
+
await holdForRecovery(event);
|
|
2744
|
+
return;
|
|
1874
2745
|
}
|
|
1875
|
-
|
|
1876
|
-
|
|
2746
|
+
if (event.type === 'round-reset') {
|
|
2747
|
+
await resetRound(event);
|
|
2748
|
+
continue;
|
|
1877
2749
|
}
|
|
1878
|
-
if (
|
|
1879
|
-
|
|
2750
|
+
if (isHarnessEvent(event, 'tool-call')) {
|
|
2751
|
+
this._log?.info('runs', 'tool call', { runId, name: event.name });
|
|
2752
|
+
await transcript.chunk(toolCallChunk(event));
|
|
1880
2753
|
}
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
mainOwnsCleanup = true;
|
|
1884
|
-
const runCompletion = async () => {
|
|
1885
|
-
try {
|
|
1886
|
-
const attachTurnIds = resumed ? [] : attachTurnDocumentIds(history);
|
|
1887
|
-
if (attachTurnIds.length > 0) {
|
|
1888
|
-
backfillDocumentOnlyTurn(messages);
|
|
1889
|
-
}
|
|
1890
|
-
const knowledge = !resumed &&
|
|
1891
|
-
!diagramRepair &&
|
|
1892
|
-
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
1893
|
-
? await this._searchKnowledge(engine, messages, {
|
|
1894
|
-
chatId,
|
|
1895
|
-
attachmentIds: attachTurnIds,
|
|
1896
|
-
signal: controller.signal,
|
|
1897
|
-
preparing: modelWatch.documents
|
|
1898
|
-
})
|
|
1899
|
-
: null;
|
|
1900
|
-
if (controller.signal.aborted) {
|
|
1901
|
-
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1902
|
-
if (outcome.kind === 'replay') {
|
|
1903
|
-
runVerdict = 'replay';
|
|
1904
|
-
this._seen.delete(runId);
|
|
1905
|
-
return;
|
|
1906
|
-
}
|
|
1907
|
-
if (outcome.kind === 'handoff') {
|
|
1908
|
-
runVerdict = 'handoff';
|
|
1909
|
-
return;
|
|
1910
|
-
}
|
|
1911
|
-
runFailure = outcome.reason;
|
|
1912
|
-
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1913
|
-
return;
|
|
1914
|
-
}
|
|
1915
|
-
const injected = knowledge
|
|
1916
|
-
? await this._injectKnowledge(knowledge, {
|
|
1917
|
-
transcript,
|
|
1918
|
-
sourceTagFilter,
|
|
1919
|
-
retrievedRecords
|
|
1920
|
-
})
|
|
1921
|
-
: { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
1922
|
-
if (!resumed) {
|
|
1923
|
-
const hasAudio = messages.some((turn) => turn.audioAttachments?.length);
|
|
1924
|
-
if (hasAudio) {
|
|
1925
|
-
modelWatch.transcribe(true);
|
|
1926
|
-
}
|
|
1927
|
-
await this._hydrateAudio(messages, controller.signal);
|
|
1928
|
-
if (hasAudio) {
|
|
1929
|
-
modelWatch.transcribe(false);
|
|
1930
|
-
}
|
|
1931
|
-
backfillImageOnlyTurns(messages);
|
|
1932
|
-
if (seesImages) {
|
|
1933
|
-
renderedPdfIds = await this._hydratePdfPages(messages, controller.signal, runId);
|
|
1934
|
-
}
|
|
1935
|
-
annotateDocumentAttachments(messages);
|
|
1936
|
-
}
|
|
1937
|
-
const unreadIds = attachTurnIds.filter((id) => !renderedPdfIds.has(id));
|
|
1938
|
-
if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
|
|
1939
|
-
const reason = documentUnreadReason({
|
|
1940
|
-
knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
|
|
1941
|
-
failedFiles: knowledge?.search?.failedFiles,
|
|
1942
|
-
attachedCount: unreadIds.length
|
|
1943
|
-
});
|
|
1944
|
-
const lastUser = messages.findLast((turn) => turn.role === 'user');
|
|
1945
|
-
if (lastUser) {
|
|
1946
|
-
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
|
|
1947
|
-
}
|
|
1948
|
-
if (!injected.sourcesWritten) {
|
|
1949
|
-
await this._writeUnreadNotice(transcript, reason);
|
|
1950
|
-
}
|
|
1951
|
-
}
|
|
1952
|
-
if (!resumed) {
|
|
1953
|
-
annotateUnreadableImages(unreadableImages);
|
|
1954
|
-
}
|
|
1955
|
-
const toolConfigs = await toolConfigsPromise;
|
|
1956
|
-
const { roots, write } = await fsGrantsPromise;
|
|
1957
|
-
const { grantedDirs, grantedDrives, driveOp } = await resolveFsGrants(roots, this._drives, (key, err) => this._log?.warn('assistant', 'drive grant dropped', {
|
|
1958
|
-
key,
|
|
1959
|
-
error: errorMessage(err)
|
|
1960
|
-
}));
|
|
1961
|
-
if (await this._isHandled(engine, request)) {
|
|
1962
|
-
return;
|
|
1963
|
-
}
|
|
1964
|
-
let cacheKey;
|
|
1965
|
-
if (resumed) {
|
|
1966
|
-
cacheKey =
|
|
1967
|
-
typeof savedRequest?.cacheKey === 'string' ? savedRequest.cacheKey : undefined;
|
|
1968
|
-
}
|
|
1969
|
-
else if (!diagramRepair) {
|
|
1970
|
-
cacheKey = chatRepairKvCacheKey(chatId, acceptedRepairs);
|
|
1971
|
-
}
|
|
1972
|
-
const unloading = lender ? undefined : this._unloading.get(model);
|
|
1973
|
-
if (unloading) {
|
|
1974
|
-
await raceAbort(unloading.catch(noop), controller.signal);
|
|
1975
|
-
}
|
|
1976
|
-
this._log?.info('runs', 'completion requested', {
|
|
1977
|
-
runId,
|
|
1978
|
-
model,
|
|
1979
|
-
cacheKey,
|
|
1980
|
-
turns: messages.length
|
|
1981
|
-
});
|
|
1982
|
-
const harness = this._harnessFor(runAgent);
|
|
1983
|
-
const completionContext = {
|
|
1984
|
-
runId,
|
|
1985
|
-
runLog,
|
|
1986
|
-
resume: resumed,
|
|
1987
|
-
model,
|
|
1988
|
-
agent: runAgent,
|
|
1989
|
-
messages: resumed ? messages : nameSenders(messages),
|
|
1990
|
-
cacheKey,
|
|
1991
|
-
thinkingDefault: thinkingDefault(config),
|
|
1992
|
-
toolConfigs,
|
|
1993
|
-
grantedDirs,
|
|
1994
|
-
grantedDrives,
|
|
1995
|
-
driveOp,
|
|
1996
|
-
grantedWriteDirs: write,
|
|
1997
|
-
approve: approvals.approve,
|
|
1998
|
-
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
1999
|
-
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
2000
|
-
ragSearch: async (input) => {
|
|
2001
|
-
const found = await raceAbort(engine.ragSearch({
|
|
2002
|
-
chatId,
|
|
2003
|
-
query: input.query,
|
|
2004
|
-
mode: 'tool',
|
|
2005
|
-
limit: input.limit
|
|
2006
|
-
}), controller.signal);
|
|
2007
|
-
const hits = found.hits ?? [];
|
|
2008
|
-
const records = sourcesFromHits(hits, RAG_SOURCE_BOUNDS);
|
|
2009
|
-
if (records.length > 0) {
|
|
2010
|
-
try {
|
|
2011
|
-
sourceTagFilter.add(records.map((record) => record.title));
|
|
2012
|
-
retrievedRecords.push(...records.map((record, index) => ({
|
|
2013
|
-
...record,
|
|
2014
|
-
excerpt: hits[index]?.content ?? record.excerpt
|
|
2015
|
-
})));
|
|
2016
|
-
await transcript.chunk({
|
|
2017
|
-
type: 'sources',
|
|
2018
|
-
sources: { sources: records }
|
|
2019
|
-
});
|
|
2020
|
-
}
|
|
2021
|
-
catch (err) {
|
|
2022
|
-
this._log?.warn('assistant', 'rag tool sources chunk skipped', { runId, err });
|
|
2023
|
-
}
|
|
2024
|
-
}
|
|
2025
|
-
return {
|
|
2026
|
-
enabled: found.enabled === true,
|
|
2027
|
-
indexEmpty: found.indexEmpty === true,
|
|
2028
|
-
hasKnowledge: found.hasKnowledge === true,
|
|
2029
|
-
pendingReferenced: found.pendingReferenced === true,
|
|
2030
|
-
errorMessage: found.errorMessage ?? undefined,
|
|
2031
|
-
hits
|
|
2032
|
-
};
|
|
2033
|
-
},
|
|
2034
|
-
progress: (update) => modelWatch.image(update),
|
|
2035
|
-
generationParams: diagramRepair ? { reasoning_budget: 0 } : undefined,
|
|
2036
|
-
captureInferenceCalls: await this._capturingInferenceCalls(),
|
|
2037
|
-
signal: controller.signal
|
|
2038
|
-
};
|
|
2039
|
-
const events = harness.completion(completionContext);
|
|
2040
|
-
let harnessEvents = 0;
|
|
2041
|
-
for await (const event of events) {
|
|
2042
|
-
harnessEvents++;
|
|
2043
|
-
modelWatch.clear();
|
|
2044
|
-
if (event.type === 'run-started' && resumed) {
|
|
2045
|
-
await requireRecovery('The worker started a fresh run instead of resuming the saved run.');
|
|
2046
|
-
return;
|
|
2047
|
-
}
|
|
2048
|
-
if (event.type === 'run-started') {
|
|
2049
|
-
continue;
|
|
2050
|
-
}
|
|
2051
|
-
if (event.type === 'run-resumed' && !resumeAccepted) {
|
|
2052
|
-
const executing = await transcript.status({ state: 'executing' }, { resumed: true });
|
|
2053
|
-
runStatusId = executing.id;
|
|
2054
|
-
resumeAccepted = true;
|
|
2055
|
-
}
|
|
2056
|
-
if (event.type === 'run-resumed') {
|
|
2057
|
-
continue;
|
|
2058
|
-
}
|
|
2059
|
-
if (resumed && !resumeAccepted && event.type === 'error') {
|
|
2060
|
-
await requireRecovery(resumeSetupFailureNotice(event));
|
|
2061
|
-
return;
|
|
2062
|
-
}
|
|
2063
|
-
if (resumed && !resumeAccepted && event.type !== 'aborted' && event.type !== 'paused') {
|
|
2064
|
-
await requireRecovery('The worker did not confirm the saved run before producing new events.');
|
|
2065
|
-
return;
|
|
2066
|
-
}
|
|
2067
|
-
if (event.type === 'round-reset') {
|
|
2068
|
-
const replacedTypes = Array.isArray(event.replacedTypes)
|
|
2069
|
-
? event.replacedTypes.filter(isReplacedType)
|
|
2070
|
-
: undefined;
|
|
2071
|
-
const round = typeof event.round === 'number' ? event.round : 0;
|
|
2072
|
-
await resetRound(round, replacedTypes);
|
|
2073
|
-
continue;
|
|
2074
|
-
}
|
|
2075
|
-
if (isHarnessEvent(event, 'content') || isHarnessEvent(event, 'thinking')) {
|
|
2076
|
-
const round = typeof event.round === 'number' ? event.round : activeRound;
|
|
2077
|
-
await writeText(event.type, event.text, round);
|
|
2078
|
-
continue;
|
|
2079
|
-
}
|
|
2080
|
-
if (event.type === 'metrics') {
|
|
2081
|
-
const { type, ...rest } = event;
|
|
2082
|
-
metrics = rest;
|
|
2083
|
-
this._log?.info('runs', 'run metrics', { runId, ...runMetricsSummary(rest) });
|
|
2084
|
-
continue;
|
|
2085
|
-
}
|
|
2086
|
-
if (event.type === 'inference-sent') {
|
|
2087
|
-
void this._inferenceCalls
|
|
2088
|
-
.recordSent(chatId, {
|
|
2089
|
-
runId,
|
|
2090
|
-
round: Number(event.round ?? 0),
|
|
2091
|
-
messages: event.messages,
|
|
2092
|
-
tokens: event.tokens,
|
|
2093
|
-
outcome: event.outcome,
|
|
2094
|
-
reason: event.reason
|
|
2095
|
-
})
|
|
2096
|
-
.catch((err) => this._log?.warn('assistant', 'sent slice not recorded', { err }));
|
|
2097
|
-
continue;
|
|
2098
|
-
}
|
|
2099
|
-
if (event.type === 'inference-call') {
|
|
2100
|
-
void this._inferenceCalls
|
|
2101
|
-
.record(chatId, { runId, at: Date.now(), call: asRecord(event.call) })
|
|
2102
|
-
.catch((err) => this._log?.warn('assistant', 'inference call not recorded', { err }));
|
|
2103
|
-
continue;
|
|
2104
|
-
}
|
|
2105
|
-
await flushTranscript();
|
|
2106
|
-
if (event.type === 'error') {
|
|
2107
|
-
await failRun(event.message);
|
|
2108
|
-
return;
|
|
2109
|
-
}
|
|
2110
|
-
if (event.type === 'aborted') {
|
|
2111
|
-
await endAbortedRun();
|
|
2112
|
-
return;
|
|
2113
|
-
}
|
|
2114
|
-
if (event.type === 'paused' && controller.signal.reason === SUSPENDED) {
|
|
2115
|
-
this._seen.delete(runId);
|
|
2116
|
-
}
|
|
2117
|
-
if (event.type === 'paused' || event.type === 'recovery-required') {
|
|
2118
|
-
runVerdict = event.type;
|
|
2119
|
-
const operationName = asRecord(event.operation).name;
|
|
2120
|
-
const reason = event.type === 'paused'
|
|
2121
|
-
? 'The run paused after saving a checkpoint.'
|
|
2122
|
-
: `Recovery decision required${typeof operationName === 'string' ? ` for ${operationName}` : ''}.`;
|
|
2123
|
-
await transcript.status({ state: event.type, reason });
|
|
2124
|
-
titleOutcome = 'ended';
|
|
2125
|
-
return;
|
|
2126
|
-
}
|
|
2127
|
-
if (isHarnessEvent(event, 'tool-call')) {
|
|
2128
|
-
this._log?.info('runs', 'tool call', { runId, name: event.name });
|
|
2129
|
-
await transcript.chunk(toolCallChunk(event));
|
|
2130
|
-
}
|
|
2131
|
-
else if (isHarnessEvent(event, 'tool-result')) {
|
|
2132
|
-
await recordToolResult(event);
|
|
2133
|
-
}
|
|
2134
|
-
else if (isHarnessEvent(event, 'tool-activity')) {
|
|
2135
|
-
void engine
|
|
2136
|
-
.recordToolActivity({
|
|
2137
|
-
agentId: runAgent.id,
|
|
2138
|
-
toolName: event.name,
|
|
2139
|
-
decision: event.decision,
|
|
2140
|
-
effect: event.effect,
|
|
2141
|
-
arguments: event.args,
|
|
2142
|
-
resource: event.resource ?? undefined,
|
|
2143
|
-
chatId,
|
|
2144
|
-
runId,
|
|
2145
|
-
at: Date.now()
|
|
2146
|
-
})
|
|
2147
|
-
.catch((err) => this._log?.warn('runs', 'tool activity record failed', { runId, err }));
|
|
2148
|
-
}
|
|
2149
|
-
else {
|
|
2150
|
-
this._log?.warn('assistant', 'unknown harness event dropped', { type: event.type });
|
|
2151
|
-
}
|
|
2152
|
-
}
|
|
2153
|
-
if (resumed && !resumeAccepted) {
|
|
2154
|
-
await requireRecovery('The worker ended without confirming the saved run was resumed.');
|
|
2155
|
-
return;
|
|
2156
|
-
}
|
|
2157
|
-
this._log?.info('runs', 'harness stream ended', { runId, events: harnessEvents });
|
|
2158
|
-
if (await settledWithin(flushTranscript(), FINAL_FLUSH_MS)) {
|
|
2159
|
-
this._log?.info('runs', 'final flush done', { runId });
|
|
2160
|
-
}
|
|
2161
|
-
else {
|
|
2162
|
-
this._log?.warn('runs', 'completing over a stranded flush', {
|
|
2163
|
-
runId,
|
|
2164
|
-
pending: transcript.pending()
|
|
2165
|
-
});
|
|
2166
|
-
}
|
|
2167
|
-
const answerText = [...answerTextByRound.entries()]
|
|
2168
|
-
.sort(([left], [right]) => left - right)
|
|
2169
|
-
.map(([, text]) => text)
|
|
2170
|
-
.join('');
|
|
2171
|
-
const citedTitles = injected.ragInjected
|
|
2172
|
-
? citedByPhraseOverlap(answerText, retrievedRecords)
|
|
2173
|
-
: null;
|
|
2174
|
-
await transcript.status({
|
|
2175
|
-
state: 'completed',
|
|
2176
|
-
metrics: citedTitles ? { ...metrics, citedTitles } : metrics
|
|
2177
|
-
});
|
|
2178
|
-
titleOutcome = 'completed';
|
|
2754
|
+
else if (isHarnessEvent(event, 'tool-result')) {
|
|
2755
|
+
await recordToolResult(event);
|
|
2179
2756
|
}
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
:
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
runVerdict = 'handoff';
|
|
2191
|
-
return;
|
|
2192
|
-
}
|
|
2193
|
-
await flushTranscript();
|
|
2194
|
-
runFailure =
|
|
2195
|
-
(this._lenderFor(runAgent) ? loanFailureNotice(err) : null) ?? runFailureNotice(err);
|
|
2196
|
-
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
2197
|
-
titleOutcome = 'ended';
|
|
2198
|
-
}
|
|
2199
|
-
finally {
|
|
2200
|
-
void this._inferenceCalls
|
|
2201
|
-
.recordOutcome(chatId, {
|
|
2757
|
+
else if (isHarnessEvent(event, 'tool-activity')) {
|
|
2758
|
+
void engine
|
|
2759
|
+
.recordToolActivity({
|
|
2760
|
+
agentId: runAgent.id,
|
|
2761
|
+
toolName: event.name,
|
|
2762
|
+
decision: event.decision,
|
|
2763
|
+
effect: event.effect,
|
|
2764
|
+
arguments: event.args,
|
|
2765
|
+
resource: event.resource ?? undefined,
|
|
2766
|
+
chatId,
|
|
2202
2767
|
runId,
|
|
2203
|
-
|
|
2204
|
-
reason: runFailure
|
|
2768
|
+
at: Date.now()
|
|
2205
2769
|
})
|
|
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
|
-
}
|
|
2770
|
+
.catch((err) => this._log?.warn('runs', 'tool activity record failed', { runId, err }));
|
|
2221
2771
|
}
|
|
2222
|
-
|
|
2223
|
-
|
|
2772
|
+
else {
|
|
2773
|
+
this._log?.warn('assistant', 'unknown harness event dropped', { type: event.type });
|
|
2774
|
+
}
|
|
2775
|
+
}
|
|
2776
|
+
this._log?.info('runs', 'harness stream ended', { runId, events: harnessEvents });
|
|
2777
|
+
if (await settledWithin(flushTranscript(), FINAL_FLUSH_MS)) {
|
|
2778
|
+
this._log?.info('runs', 'final flush done', { runId });
|
|
2779
|
+
}
|
|
2780
|
+
else {
|
|
2781
|
+
this._log?.warn('runs', 'completing over a stranded flush', {
|
|
2782
|
+
runId,
|
|
2783
|
+
pending: transcript.pending()
|
|
2784
|
+
});
|
|
2785
|
+
}
|
|
2786
|
+
const citedTitles = injected.ragInjected
|
|
2787
|
+
? citedByPhraseOverlap([...answers.values()].join(''), retrievedRecords)
|
|
2788
|
+
: null;
|
|
2789
|
+
const primedHere = !!sentRun?.used && rounds.ranHere(primeRound);
|
|
2790
|
+
const primed = primedHere ? replay.rebuild : null;
|
|
2791
|
+
if (primedHere) {
|
|
2792
|
+
await this._runReplay.consume({ holder: LOCAL_HOLDER, chatId });
|
|
2793
|
+
}
|
|
2794
|
+
if (sentRun?.used && !rounds.rounds) {
|
|
2795
|
+
await this._recordSentRun(chatId, { runId, turns: sentRun.turns(inputLength), asked });
|
|
2796
|
+
}
|
|
2797
|
+
sentRun = null;
|
|
2798
|
+
await transcript.status({
|
|
2799
|
+
state: 'completed',
|
|
2800
|
+
metrics: withCacheRebuild(citedTitles ? { ...metrics, citedTitles } : metrics, primed, primeTokens ?? null)
|
|
2801
|
+
});
|
|
2802
|
+
titleOutcome = 'completed';
|
|
2803
|
+
for (const id of taken) {
|
|
2804
|
+
this._steered.set(id, runId);
|
|
2805
|
+
}
|
|
2806
|
+
await this._storeVideoDigests(sampled, { meshId, chatId, runId, deviceId });
|
|
2807
|
+
await this._storePdfSelections(selectedPdfs, { meshId, chatId, runId, deviceId });
|
|
2808
|
+
}
|
|
2809
|
+
catch (err) {
|
|
2810
|
+
const outcome = controller.signal.aborted
|
|
2811
|
+
? this._abortOutcome(runId, controller.signal.reason)
|
|
2812
|
+
: null;
|
|
2813
|
+
if (outcome?.kind === 'replay') {
|
|
2814
|
+
runVerdict = 'replay';
|
|
2815
|
+
this._seen.delete(runId);
|
|
2816
|
+
return;
|
|
2817
|
+
}
|
|
2818
|
+
if (outcome?.kind === 'handoff') {
|
|
2819
|
+
runVerdict = 'handoff';
|
|
2820
|
+
return;
|
|
2821
|
+
}
|
|
2822
|
+
await flushTranscript();
|
|
2823
|
+
runFailure = runFailureNotice(err);
|
|
2824
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
2825
|
+
titleOutcome = 'ended';
|
|
2224
2826
|
}
|
|
2225
2827
|
finally {
|
|
2226
|
-
|
|
2227
|
-
|
|
2828
|
+
steering?.close();
|
|
2829
|
+
if (sentRun?.used) {
|
|
2830
|
+
await this._recordSentRun(chatId, { runId, turns: null });
|
|
2831
|
+
}
|
|
2832
|
+
void this._inferenceCalls
|
|
2833
|
+
.recordOutcome(chatId, {
|
|
2834
|
+
runId,
|
|
2835
|
+
outcome: runFailure ? 'failed' : runVerdict,
|
|
2836
|
+
reason: runFailure
|
|
2837
|
+
})
|
|
2838
|
+
.catch((err) => this._log?.warn('assistant', 'run outcome not recorded', { err }));
|
|
2839
|
+
transcript.close();
|
|
2840
|
+
idle.stop();
|
|
2841
|
+
modelWatch.stop();
|
|
2842
|
+
ownershipWatch.stop();
|
|
2843
|
+
const handedOn = controller.signal.reason === OWNERSHIP_LOST;
|
|
2844
|
+
await approvals.settle({
|
|
2845
|
+
preservePending: handedOn || isLifecycleTeardown(controller.signal.reason)
|
|
2846
|
+
});
|
|
2847
|
+
settledWatch.stop();
|
|
2848
|
+
await releaseRunLog();
|
|
2849
|
+
if (titleOutcome && !diagramRepair) {
|
|
2850
|
+
const completed = titleOutcome === 'completed';
|
|
2851
|
+
const answerText = [...answers.values()].join('');
|
|
2852
|
+
void this._autoTitle(engine, {
|
|
2853
|
+
chatId,
|
|
2854
|
+
history,
|
|
2855
|
+
messages,
|
|
2856
|
+
model,
|
|
2857
|
+
agent: runAgent,
|
|
2858
|
+
probe: completed,
|
|
2859
|
+
rounds
|
|
2860
|
+
})
|
|
2861
|
+
.then(() => completed && answerText.trim()
|
|
2862
|
+
? this._suggestNext(engine, {
|
|
2863
|
+
chatId,
|
|
2864
|
+
runId,
|
|
2865
|
+
messages,
|
|
2866
|
+
answer: answerText,
|
|
2867
|
+
model,
|
|
2868
|
+
agent: runAgent,
|
|
2869
|
+
rounds
|
|
2870
|
+
})
|
|
2871
|
+
: undefined)
|
|
2872
|
+
.catch((err) => this._log?.warn('runs', 'title or suggestion skipped', { chatId, err }));
|
|
2228
2873
|
}
|
|
2229
2874
|
}
|
|
2230
2875
|
}
|
|
2231
2876
|
async _invokeCapability({ engine, request, agent, capability, controller, meshId, runId }) {
|
|
2232
2877
|
const { chatId } = request;
|
|
2233
2878
|
const deviceId = this._requireDeviceId();
|
|
2234
|
-
const harness = this.
|
|
2879
|
+
const harness = this._harness;
|
|
2235
2880
|
const transcript = new RunTranscript(engine, {
|
|
2236
2881
|
chatId,
|
|
2237
2882
|
runId,
|
|
@@ -2243,11 +2888,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2243
2888
|
if (inflight) {
|
|
2244
2889
|
this._runs.set(runId, { ...inflight, executing: true, model: '' });
|
|
2245
2890
|
}
|
|
2246
|
-
this.
|
|
2891
|
+
this._runsChanged();
|
|
2247
2892
|
const executing = await transcript.status({ state: 'executing' });
|
|
2248
2893
|
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(executing.id, runStatus));
|
|
2249
2894
|
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
2250
|
-
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
2895
|
+
const ownershipWatch = this._watchOwnership(engine, { request, agentId: agent.id, controller });
|
|
2896
|
+
const idle = idleWatch(controller, this._runIdleTimeoutMs);
|
|
2897
|
+
idle.touch();
|
|
2251
2898
|
let metrics;
|
|
2252
2899
|
const endAbortedCapability = async () => {
|
|
2253
2900
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
@@ -2274,17 +2921,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2274
2921
|
agent,
|
|
2275
2922
|
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
2276
2923
|
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
2277
|
-
progress: (update) =>
|
|
2924
|
+
progress: (update) => {
|
|
2925
|
+
idle.touch();
|
|
2926
|
+
return modelWatch.image(update);
|
|
2927
|
+
},
|
|
2278
2928
|
signal: controller.signal
|
|
2279
2929
|
});
|
|
2280
2930
|
for await (const event of events) {
|
|
2931
|
+
idle.touch();
|
|
2281
2932
|
if (event.type === 'metrics') {
|
|
2282
2933
|
const { type, ...rest } = event;
|
|
2283
2934
|
metrics = rest;
|
|
2284
2935
|
this._log?.info('runs', 'capability metrics', { runId, capability, ...rest });
|
|
2285
2936
|
continue;
|
|
2286
2937
|
}
|
|
2287
|
-
if (event.type === 'error') {
|
|
2938
|
+
if (event.type === 'error' && !this._backgrounded) {
|
|
2288
2939
|
await transcript.status({
|
|
2289
2940
|
state: 'failed',
|
|
2290
2941
|
reason: runFailureNotice(event.message),
|
|
@@ -2292,7 +2943,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2292
2943
|
});
|
|
2293
2944
|
return;
|
|
2294
2945
|
}
|
|
2295
|
-
if (event.type === '
|
|
2946
|
+
if (event.type === 'error') {
|
|
2947
|
+
this._log?.info('runs', 'background error replayed', { runId, message: event.message });
|
|
2948
|
+
}
|
|
2949
|
+
if (event.type === 'error' || event.type === 'aborted') {
|
|
2296
2950
|
await endAbortedCapability();
|
|
2297
2951
|
return;
|
|
2298
2952
|
}
|
|
@@ -2321,14 +2975,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2321
2975
|
}
|
|
2322
2976
|
await transcript.status({
|
|
2323
2977
|
state: 'failed',
|
|
2324
|
-
reason: outcome?.reason ??
|
|
2325
|
-
(this._lenderFor(agent) ? loanFailureNotice(err) : null) ??
|
|
2326
|
-
runFailureNotice(err),
|
|
2978
|
+
reason: outcome?.reason ?? runFailureNotice(err),
|
|
2327
2979
|
metrics
|
|
2328
2980
|
});
|
|
2329
2981
|
}
|
|
2330
2982
|
finally {
|
|
2331
2983
|
transcript.close();
|
|
2984
|
+
idle.stop();
|
|
2332
2985
|
modelWatch.stop();
|
|
2333
2986
|
settledWatch.stop();
|
|
2334
2987
|
ownershipWatch.stop();
|
|
@@ -2413,89 +3066,361 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2413
3066
|
signal?.removeEventListener('abort', abort);
|
|
2414
3067
|
stream.destroy();
|
|
2415
3068
|
}
|
|
2416
|
-
}
|
|
2417
|
-
async _readBlob(url, signal) {
|
|
2418
|
-
if (!url || signal?.aborted)
|
|
3069
|
+
}
|
|
3070
|
+
async _readBlob(url, signal) {
|
|
3071
|
+
if (!url || signal?.aborted)
|
|
3072
|
+
return null;
|
|
3073
|
+
const pending = defaultFetch(url).catch(noop);
|
|
3074
|
+
try {
|
|
3075
|
+
const response = await raceInactivity(pending, signal, this._blobReadTimeoutMs);
|
|
3076
|
+
const bytes = response?.ok
|
|
3077
|
+
? await drainBody(response.body, signal, this._blobReadTimeoutMs)
|
|
3078
|
+
: null;
|
|
3079
|
+
return bytes?.length ? bytes : null;
|
|
3080
|
+
}
|
|
3081
|
+
catch {
|
|
3082
|
+
pending.then((response) => response?.body?.cancel(), noop);
|
|
3083
|
+
return null;
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId, sourceId, blobId }) {
|
|
3087
|
+
const duplex = this._engine.uploadAttachment();
|
|
3088
|
+
const response = readUploadAttachment(duplex);
|
|
3089
|
+
void response.catch(() => duplex.destroy());
|
|
3090
|
+
try {
|
|
3091
|
+
await writeUploadFrame(duplex, {
|
|
3092
|
+
chatId,
|
|
3093
|
+
runId,
|
|
3094
|
+
originDeviceId: deviceId,
|
|
3095
|
+
fileName,
|
|
3096
|
+
mimeType,
|
|
3097
|
+
raw,
|
|
3098
|
+
meshId,
|
|
3099
|
+
sourceId,
|
|
3100
|
+
blobId,
|
|
3101
|
+
totalBytes: blobId ? bytes.byteLength : undefined
|
|
3102
|
+
});
|
|
3103
|
+
const buf = blobId ? b4a.alloc(0) : b4a.toBuffer(bytes);
|
|
3104
|
+
for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
|
|
3105
|
+
await writeUploadFrame(duplex, { data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
|
|
3106
|
+
}
|
|
3107
|
+
duplex.end();
|
|
3108
|
+
}
|
|
3109
|
+
catch (error) {
|
|
3110
|
+
duplex.destroy();
|
|
3111
|
+
await response;
|
|
3112
|
+
throw error;
|
|
3113
|
+
}
|
|
3114
|
+
const attachment = await response;
|
|
3115
|
+
if (!attachment) {
|
|
3116
|
+
throw new Error('attachment upload ended without a record');
|
|
3117
|
+
}
|
|
3118
|
+
return attachment;
|
|
3119
|
+
}
|
|
3120
|
+
async _hydrateAudio(messages, signal) {
|
|
3121
|
+
const turns = messages.filter((turn) => turn.audioAttachments?.length);
|
|
3122
|
+
if (!turns.length) {
|
|
3123
|
+
return;
|
|
3124
|
+
}
|
|
3125
|
+
for (const turn of turns) {
|
|
3126
|
+
for (const ref of turn.audioAttachments ?? []) {
|
|
3127
|
+
if (signal.aborted)
|
|
3128
|
+
return;
|
|
3129
|
+
const block = await this._transcribeAttachment(ref, signal);
|
|
3130
|
+
turn.content = turn.content.trim() ? `${turn.content}\n\n${block}` : block;
|
|
3131
|
+
}
|
|
3132
|
+
delete turn.audioAttachments;
|
|
3133
|
+
}
|
|
3134
|
+
}
|
|
3135
|
+
async _transcribeAttachment(ref, signal) {
|
|
3136
|
+
const label = `Attached audio "${ref.fileName}"`;
|
|
3137
|
+
const cached = this._audioTranscripts.get(ref.blobId);
|
|
3138
|
+
if (cached !== undefined) {
|
|
3139
|
+
return cached ? audioTranscriptBlock(ref.fileName, cached) : silentAudioNotice(ref.fileName);
|
|
3140
|
+
}
|
|
3141
|
+
if (!this._harness.transcribe) {
|
|
3142
|
+
return unheardAudioNotice(label, 'transcription is unavailable on this device', {
|
|
3143
|
+
retryable: false
|
|
3144
|
+
});
|
|
3145
|
+
}
|
|
3146
|
+
try {
|
|
3147
|
+
const bytes = await this._readBlob(ref.url, signal);
|
|
3148
|
+
if (!bytes) {
|
|
3149
|
+
return unheardAudioNotice(label, 'its content has not reached this device — the upload may still be syncing');
|
|
3150
|
+
}
|
|
3151
|
+
const model = mediaModelNameFor('transcription', await this._persistedProfile());
|
|
3152
|
+
await this.ensureMediaModelUnpaused('transcription', model);
|
|
3153
|
+
await this._downloads?.waitUntilCached(model, signal);
|
|
3154
|
+
const text = (await this._harness.transcribe({ model: '', audio: b4a.toBuffer(bytes), signal })).trim();
|
|
3155
|
+
this._audioTranscripts.set(ref.blobId, text);
|
|
3156
|
+
if (!text) {
|
|
3157
|
+
return silentAudioNotice(ref.fileName);
|
|
3158
|
+
}
|
|
3159
|
+
return audioTranscriptBlock(ref.fileName, text);
|
|
3160
|
+
}
|
|
3161
|
+
catch (err) {
|
|
3162
|
+
if (signal.aborted) {
|
|
3163
|
+
throw err;
|
|
3164
|
+
}
|
|
3165
|
+
this._log?.warn('assistant', `audio transcription failed — ${ref.fileName}`, err);
|
|
3166
|
+
return unheardAudioNotice(label, 'transcription failed on this device');
|
|
3167
|
+
}
|
|
3168
|
+
}
|
|
3169
|
+
async _seesImages(model) {
|
|
3170
|
+
if (this._harness.supportsImagesFor) {
|
|
3171
|
+
return this._harness.supportsImagesFor(model);
|
|
3172
|
+
}
|
|
3173
|
+
const anyModel = (this._harness.supportsImages ?? this._info.supportsImages) !== false;
|
|
3174
|
+
if (!anyModel || !this._harness.list) {
|
|
3175
|
+
return anyModel;
|
|
3176
|
+
}
|
|
3177
|
+
const rows = await this._harness.list({ type: 'llm' }).catch(noop);
|
|
3178
|
+
if (!rows) {
|
|
3179
|
+
return anyModel;
|
|
3180
|
+
}
|
|
3181
|
+
const row = rows.find((candidate) => candidate.name === model);
|
|
3182
|
+
return row ? (row.supportsImages ?? anyModel) : false;
|
|
3183
|
+
}
|
|
3184
|
+
async _hydrateVideos(messages, run) {
|
|
3185
|
+
const { signal, runId, seesImages, reading, sampled } = run;
|
|
3186
|
+
const turns = messages.filter((turn) => turn.videos?.length);
|
|
3187
|
+
if (!turns.length) {
|
|
3188
|
+
return;
|
|
3189
|
+
}
|
|
3190
|
+
const asked = messages.findLast((turn) => turn.role === 'user')?.videos ?? [];
|
|
3191
|
+
const dir = this._imageCacheDir;
|
|
3192
|
+
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
3193
|
+
const pinned = this._runImageCacheNames.get(runId) ?? new Set();
|
|
3194
|
+
this._runImageCacheNames.set(runId, pinned);
|
|
3195
|
+
const unsampled = turns.flatMap((turn) => turn.videos ?? []).filter((ref) => !ref.digest);
|
|
3196
|
+
const budget = { ...reading, videos: unsampled.length };
|
|
3197
|
+
for (const turn of turns) {
|
|
3198
|
+
for (const ref of turn.videos ?? []) {
|
|
3199
|
+
if (signal.aborted)
|
|
3200
|
+
return;
|
|
3201
|
+
const hydration = { dir, ref, pinned, seesImages, signal, budget, sampled };
|
|
3202
|
+
const blocks = await this._hydrateVideo(turn, hydration);
|
|
3203
|
+
turn.content = [turn.content.trim(), ...blocks].filter(Boolean).join('\n\n');
|
|
3204
|
+
}
|
|
3205
|
+
delete turn.videos;
|
|
3206
|
+
}
|
|
3207
|
+
const pictured = asked.some((ref) => ref.digest || this._videoProbes.get(ref.blobId)?.hasVideo);
|
|
3208
|
+
if (!seesImages && pictured) {
|
|
3209
|
+
throw new Error(RUN_FAILURE_REASONS.videoNeedsVision);
|
|
3210
|
+
}
|
|
3211
|
+
}
|
|
3212
|
+
async _hydrateVideo(turn, hydration) {
|
|
3213
|
+
const { ref, seesImages, signal } = hydration;
|
|
3214
|
+
const source = { opened: null };
|
|
3215
|
+
const open = () => (source.opened ??= this._openVideo(hydration));
|
|
3216
|
+
try {
|
|
3217
|
+
const video = await this._readVideo(hydration, open);
|
|
3218
|
+
if (video === 'unsynced') {
|
|
3219
|
+
return [unsyncedVideoNotice(ref.fileName)];
|
|
3220
|
+
}
|
|
3221
|
+
if (video === 'audio') {
|
|
3222
|
+
return [await this._transcribeAttachment(ref, signal)];
|
|
3223
|
+
}
|
|
3224
|
+
if (!seesImages || !('files' in video)) {
|
|
3225
|
+
return [hiddenVideoNotice(ref.fileName), videoAudioBlock(ref.fileName, video)];
|
|
3226
|
+
}
|
|
3227
|
+
attachPages(turn, video.files);
|
|
3228
|
+
const annotation = videoAnnotation({
|
|
3229
|
+
fileName: ref.fileName,
|
|
3230
|
+
duration: video.duration,
|
|
3231
|
+
times: video.times
|
|
3232
|
+
});
|
|
3233
|
+
return [annotation, videoAudioBlock(ref.fileName, video)];
|
|
3234
|
+
}
|
|
3235
|
+
catch (err) {
|
|
3236
|
+
if (signal.aborted) {
|
|
3237
|
+
throw err;
|
|
3238
|
+
}
|
|
3239
|
+
this._log?.warn('assistant', `video hydration failed — ${ref.fileName}`, err);
|
|
3240
|
+
return [unreadVideoNotice(ref.fileName)];
|
|
3241
|
+
}
|
|
3242
|
+
finally {
|
|
3243
|
+
const video = await source.opened?.catch(noop);
|
|
3244
|
+
if (video) {
|
|
3245
|
+
await fs.promises.unlink(video.file).catch(noop);
|
|
3246
|
+
}
|
|
3247
|
+
}
|
|
3248
|
+
}
|
|
3249
|
+
async _readVideo(hydration, open) {
|
|
3250
|
+
const { ref, seesImages, signal } = hydration;
|
|
3251
|
+
const stored = ref.digest ? await this._storedVideo(ref.digest, hydration) : null;
|
|
3252
|
+
if (stored) {
|
|
3253
|
+
return stored;
|
|
3254
|
+
}
|
|
3255
|
+
const probe = await this._probeVideo(ref, open);
|
|
3256
|
+
if (!probe) {
|
|
3257
|
+
return 'unsynced';
|
|
3258
|
+
}
|
|
3259
|
+
if (!probe.hasVideo && !probe.hasAudio) {
|
|
3260
|
+
throw new Error('no video or audio stream');
|
|
3261
|
+
}
|
|
3262
|
+
if (!probe.hasVideo) {
|
|
3263
|
+
return 'audio';
|
|
3264
|
+
}
|
|
3265
|
+
if (!seesImages) {
|
|
3266
|
+
return probe.hasAudio ? this._hearVideo(ref, open, signal) : { audio: 'none' };
|
|
3267
|
+
}
|
|
3268
|
+
const video = await open();
|
|
3269
|
+
if (!video) {
|
|
3270
|
+
return 'unsynced';
|
|
3271
|
+
}
|
|
3272
|
+
return this._sampleVideo(video, probe, hydration);
|
|
3273
|
+
}
|
|
3274
|
+
async _probeVideo(ref, open) {
|
|
3275
|
+
const known = this._videoProbes.get(ref.blobId);
|
|
3276
|
+
if (known) {
|
|
3277
|
+
return known;
|
|
3278
|
+
}
|
|
3279
|
+
const video = await open();
|
|
3280
|
+
if (!video) {
|
|
3281
|
+
return null;
|
|
3282
|
+
}
|
|
3283
|
+
const probe = await probeVideo(video.file);
|
|
3284
|
+
this._videoProbes.set(ref.blobId, probe);
|
|
3285
|
+
return probe;
|
|
3286
|
+
}
|
|
3287
|
+
async _storedVideo(digest, { dir, ref, pinned, signal }) {
|
|
3288
|
+
const headerName = videoHeaderCacheName(digest.blobId);
|
|
3289
|
+
pinned.add(headerName);
|
|
3290
|
+
const header = await fs.promises
|
|
3291
|
+
.readFile(path.join(dir, headerName), 'utf8')
|
|
3292
|
+
.then(parseVideoDigestHeader, noop);
|
|
3293
|
+
if (header) {
|
|
3294
|
+
const files = header.times.map((_, i) => {
|
|
3295
|
+
const name = videoFrameCacheName(ref, header.times.length, i);
|
|
3296
|
+
pinned.add(name);
|
|
3297
|
+
this._mediaRefs.set(name, { id: ref.id, frame: i, frames: header.times.length });
|
|
3298
|
+
return path.join(dir, name);
|
|
3299
|
+
});
|
|
3300
|
+
const cached = await Promise.all(files.map((file) => cacheOrTouch(file, noBytes)));
|
|
3301
|
+
if (cached.every(Boolean)) {
|
|
3302
|
+
return { ...header, files };
|
|
3303
|
+
}
|
|
3304
|
+
}
|
|
3305
|
+
const bytes = await this._readBlob(digest.url, signal);
|
|
3306
|
+
if (!bytes) {
|
|
2419
3307
|
return null;
|
|
2420
|
-
|
|
3308
|
+
}
|
|
3309
|
+
const target = { dir, pinned, digestBlobId: digest.blobId };
|
|
2421
3310
|
try {
|
|
2422
|
-
|
|
2423
|
-
const bytes = response?.ok
|
|
2424
|
-
? await drainBody(response.body, signal, this._blobReadTimeoutMs)
|
|
2425
|
-
: null;
|
|
2426
|
-
return bytes?.length ? bytes : null;
|
|
3311
|
+
return await this._cacheVideo(ref, unpackVideoDigest(bytes), target);
|
|
2427
3312
|
}
|
|
2428
|
-
catch {
|
|
2429
|
-
|
|
3313
|
+
catch (err) {
|
|
3314
|
+
this._log?.warn('assistant', `video digest unreadable — ${ref.fileName}`, err);
|
|
2430
3315
|
return null;
|
|
2431
3316
|
}
|
|
2432
3317
|
}
|
|
2433
|
-
async
|
|
2434
|
-
const
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
3318
|
+
async _cacheVideo(ref, digest, { dir, pinned, digestBlobId }) {
|
|
3319
|
+
const { frames, ...header } = digest;
|
|
3320
|
+
const files = frames.map((_, i) => {
|
|
3321
|
+
const name = videoFrameCacheName(ref, frames.length, i);
|
|
3322
|
+
pinned.add(name);
|
|
3323
|
+
this._mediaRefs.set(name, { id: ref.id, frame: i, frames: frames.length });
|
|
3324
|
+
return path.join(dir, name);
|
|
3325
|
+
});
|
|
3326
|
+
await Promise.all(frames.map((frame, i) => cacheOrTouch(files[i], () => Promise.resolve(frame))));
|
|
3327
|
+
if (digestBlobId) {
|
|
3328
|
+
const headerName = videoHeaderCacheName(digestBlobId);
|
|
3329
|
+
pinned.add(headerName);
|
|
3330
|
+
await cacheOrTouch(path.join(dir, headerName), () => Promise.resolve(b4a.from(JSON.stringify(header))));
|
|
2439
3331
|
}
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
3332
|
+
return { ...header, files };
|
|
3333
|
+
}
|
|
3334
|
+
async _sampleVideo(video, probe, hydration) {
|
|
3335
|
+
const { ref, budget, signal } = hydration;
|
|
3336
|
+
const started = Date.now();
|
|
3337
|
+
const count = videoFrameCount(probe, budget);
|
|
3338
|
+
const times = frameTimes(probe.duration, count);
|
|
3339
|
+
const frames = await sampleFrames(video.file, { times, maxEdge: VIDEO_FRAME_EDGE, probe });
|
|
3340
|
+
const sampleMs = Date.now() - started;
|
|
3341
|
+
const heard = probe.hasAudio
|
|
3342
|
+
? await this._hearVideo(ref, () => Promise.resolve(video), signal)
|
|
3343
|
+
: { audio: 'none' };
|
|
3344
|
+
const digest = {
|
|
3345
|
+
duration: probe.duration,
|
|
3346
|
+
times: frames.map((frame) => frame.time),
|
|
3347
|
+
...heard,
|
|
3348
|
+
frames: frames.map((frame) => frame.jpeg)
|
|
3349
|
+
};
|
|
3350
|
+
if (!hydration.ref.digest) {
|
|
3351
|
+
hydration.sampled.push({ ref, digest });
|
|
3352
|
+
}
|
|
3353
|
+
this._log?.info('assistant', 'video frames', {
|
|
3354
|
+
fileName: ref.fileName,
|
|
3355
|
+
duration: probe.duration,
|
|
3356
|
+
frames: frames.length,
|
|
3357
|
+
budget: count,
|
|
3358
|
+
ctxSize: budget.ctxSize ?? null,
|
|
3359
|
+
cacheTokens: budget.cacheTokens ?? null,
|
|
3360
|
+
audio: heard.audio,
|
|
3361
|
+
sampleMs
|
|
3362
|
+
});
|
|
3363
|
+
return this._cacheVideo(ref, digest, hydration);
|
|
3364
|
+
}
|
|
3365
|
+
async _storeVideoDigests(sampled, target) {
|
|
3366
|
+
const dir = this._imageCacheDir;
|
|
3367
|
+
const pinned = this._runImageCacheNames.get(target.runId) ?? new Set();
|
|
3368
|
+
for (const { ref, digest } of sampled) {
|
|
3369
|
+
try {
|
|
3370
|
+
const blobId = await this._uploadVideoDigest(ref, digest, target);
|
|
3371
|
+
await this._cacheVideo(ref, digest, { dir, pinned, digestBlobId: blobId });
|
|
3372
|
+
}
|
|
3373
|
+
catch (err) {
|
|
3374
|
+
this._log?.warn('assistant', `video digest not stored — ${ref.fileName}`, err);
|
|
2445
3375
|
}
|
|
2446
3376
|
}
|
|
2447
|
-
if (!attachment) {
|
|
2448
|
-
throw new Error('attachment upload ended without a record');
|
|
2449
|
-
}
|
|
2450
|
-
return attachment;
|
|
2451
3377
|
}
|
|
2452
|
-
async
|
|
2453
|
-
const
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
3378
|
+
async _uploadVideoDigest(ref, digest, target) {
|
|
3379
|
+
const attachment = await this._uploadGenerated({
|
|
3380
|
+
fileName: videoDigestFileName(ref.fileName),
|
|
3381
|
+
mimeType: VIDEO_DIGEST_MIME,
|
|
3382
|
+
bytes: packVideoDigest(digest),
|
|
3383
|
+
raw: true
|
|
3384
|
+
}, { ...target, sourceId: ref.id });
|
|
3385
|
+
return attachment.blobId;
|
|
3386
|
+
}
|
|
3387
|
+
async _openVideo({ dir, ref, pinned, signal }) {
|
|
3388
|
+
const bytes = await this._readBlob(ref.url, signal);
|
|
3389
|
+
if (!bytes) {
|
|
3390
|
+
return null;
|
|
2465
3391
|
}
|
|
3392
|
+
const name = `${imageCacheName(ref)}.source`;
|
|
3393
|
+
pinned.add(name);
|
|
3394
|
+
const file = path.join(dir, name);
|
|
3395
|
+
await fs.promises.writeFile(file, bytes, { mode: 0o600 });
|
|
3396
|
+
return { bytes, file };
|
|
2466
3397
|
}
|
|
2467
|
-
async
|
|
2468
|
-
const label = `Attached audio "${ref.fileName}"`;
|
|
3398
|
+
async _hearVideo(ref, open, signal) {
|
|
2469
3399
|
const cached = this._audioTranscripts.get(ref.blobId);
|
|
2470
3400
|
if (cached !== undefined) {
|
|
2471
|
-
return cached ?
|
|
3401
|
+
return cached ? { audio: 'speech', transcript: cached } : { audio: 'silent' };
|
|
2472
3402
|
}
|
|
2473
3403
|
if (!this._harness.transcribe) {
|
|
2474
|
-
return
|
|
2475
|
-
retryable: false
|
|
2476
|
-
});
|
|
3404
|
+
return { audio: 'unheard' };
|
|
2477
3405
|
}
|
|
2478
3406
|
try {
|
|
2479
|
-
const
|
|
2480
|
-
if (!
|
|
2481
|
-
return
|
|
3407
|
+
const video = await open();
|
|
3408
|
+
if (!video) {
|
|
3409
|
+
return { audio: 'unheard' };
|
|
2482
3410
|
}
|
|
2483
3411
|
const model = mediaModelNameFor('transcription', await this._persistedProfile());
|
|
2484
3412
|
await this.ensureMediaModelUnpaused('transcription', model);
|
|
2485
3413
|
await this._downloads?.waitUntilCached(model, signal);
|
|
2486
|
-
const
|
|
2487
|
-
this._audioTranscripts.set(ref.blobId,
|
|
2488
|
-
|
|
2489
|
-
return silentAudioNotice(ref.fileName);
|
|
2490
|
-
}
|
|
2491
|
-
return audioTranscriptBlock(ref.fileName, text);
|
|
3414
|
+
const transcript = (await this._harness.transcribe({ model: '', audio: b4a.toBuffer(video.bytes), signal })).trim();
|
|
3415
|
+
this._audioTranscripts.set(ref.blobId, transcript);
|
|
3416
|
+
return transcript ? { audio: 'speech', transcript } : { audio: 'silent' };
|
|
2492
3417
|
}
|
|
2493
3418
|
catch (err) {
|
|
2494
3419
|
if (signal.aborted) {
|
|
2495
3420
|
throw err;
|
|
2496
3421
|
}
|
|
2497
|
-
this._log?.warn('assistant', `
|
|
2498
|
-
return
|
|
3422
|
+
this._log?.warn('assistant', `video transcription failed — ${ref.fileName}`, err);
|
|
3423
|
+
return { audio: 'unheard' };
|
|
2499
3424
|
}
|
|
2500
3425
|
}
|
|
2501
3426
|
async _hydrateImages(messages, signal, runId) {
|
|
@@ -2517,6 +3442,31 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2517
3442
|
}
|
|
2518
3443
|
return unreadable;
|
|
2519
3444
|
}
|
|
3445
|
+
async _showImage(engine, { chatId, runId, model, signal }, attachmentId) {
|
|
3446
|
+
const { chunks } = await engine.listChunks({ chatId, type: 'attachment' });
|
|
3447
|
+
const image = (chunks ?? []).find((chunk) => !chunk.sourceId && chunk.attachment?.id === attachmentId)?.attachment;
|
|
3448
|
+
if (!image || image.deletedAt || !IMAGE_MIMES.has(image.mimeType.toLowerCase())) {
|
|
3449
|
+
return null;
|
|
3450
|
+
}
|
|
3451
|
+
if (!(await this._seesImages(model))) {
|
|
3452
|
+
return null;
|
|
3453
|
+
}
|
|
3454
|
+
const pinned = this._runImageCacheNames.get(runId) ?? new Set();
|
|
3455
|
+
this._runImageCacheNames.set(runId, pinned);
|
|
3456
|
+
pinned.add(imageCacheName(image));
|
|
3457
|
+
const dir = this._imageCacheDir;
|
|
3458
|
+
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
3459
|
+
try {
|
|
3460
|
+
return await this._hydrateImage(dir, image, signal);
|
|
3461
|
+
}
|
|
3462
|
+
catch (err) {
|
|
3463
|
+
if (signal.aborted) {
|
|
3464
|
+
throw err;
|
|
3465
|
+
}
|
|
3466
|
+
this._log?.error('assistant', `image hydration failed — not shown ${image.fileName}`, err);
|
|
3467
|
+
return null;
|
|
3468
|
+
}
|
|
3469
|
+
}
|
|
2520
3470
|
async _hydrateTurnImages(turn, { dir, signal }) {
|
|
2521
3471
|
const dropped = [];
|
|
2522
3472
|
for (const image of turn.images ?? []) {
|
|
@@ -2534,8 +3484,70 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2534
3484
|
}
|
|
2535
3485
|
return dropped;
|
|
2536
3486
|
}
|
|
3487
|
+
async _servedMedia(sources, { id, modelId, signal }) {
|
|
3488
|
+
if (!(await this._seesImages(modelId))) {
|
|
3489
|
+
return sources.map(() => null);
|
|
3490
|
+
}
|
|
3491
|
+
const dir = this._imageCacheDir;
|
|
3492
|
+
const pinned = new Set();
|
|
3493
|
+
this._runImageCacheNames.set(`round:${id}`, pinned);
|
|
3494
|
+
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
3495
|
+
const docs = new Map();
|
|
3496
|
+
try {
|
|
3497
|
+
const files = [];
|
|
3498
|
+
for (const source of sources) {
|
|
3499
|
+
files.push(await this._servedFile(source, { dir, pinned, docs, signal }).catch(() => null));
|
|
3500
|
+
}
|
|
3501
|
+
return files;
|
|
3502
|
+
}
|
|
3503
|
+
finally {
|
|
3504
|
+
for (const doc of docs.values()) {
|
|
3505
|
+
void doc.then((open) => open?.close(), noop);
|
|
3506
|
+
}
|
|
3507
|
+
}
|
|
3508
|
+
}
|
|
3509
|
+
_servedFile(source, served) {
|
|
3510
|
+
const { ref, attachment } = source;
|
|
3511
|
+
if (ref.frame !== undefined && ref.frames) {
|
|
3512
|
+
return this._servedFrame(source, { frame: ref.frame, frames: ref.frames }, served);
|
|
3513
|
+
}
|
|
3514
|
+
if (ref.page !== undefined) {
|
|
3515
|
+
return this._servedPage(attachment, { page: ref.page, clip: ref.clip }, served);
|
|
3516
|
+
}
|
|
3517
|
+
served.pinned.add(imageCacheName(attachment));
|
|
3518
|
+
return this._hydrateImage(served.dir, attachment, served.signal);
|
|
3519
|
+
}
|
|
3520
|
+
async _servedFrame({ attachment, digest }, { frame, frames }, { dir, pinned, signal }) {
|
|
3521
|
+
const file = path.join(dir, videoFrameCacheName(attachment, frames, frame));
|
|
3522
|
+
pinned.add(path.basename(file));
|
|
3523
|
+
if (await cacheOrTouch(file, noBytes)) {
|
|
3524
|
+
return file;
|
|
3525
|
+
}
|
|
3526
|
+
const bytes = digest ? await this._readBlob(digest.url, signal) : null;
|
|
3527
|
+
const video = bytes && unpackVideoDigest(bytes);
|
|
3528
|
+
if (!digest || !video || video.frames.length !== frames) {
|
|
3529
|
+
return null;
|
|
3530
|
+
}
|
|
3531
|
+
const target = { dir, pinned, digestBlobId: digest.blobId };
|
|
3532
|
+
return (await this._cacheVideo(attachment, video, target)).files[frame] ?? null;
|
|
3533
|
+
}
|
|
3534
|
+
async _servedPage(pdf, { page, clip }, { dir, pinned, docs, signal }) {
|
|
3535
|
+
const name = pdfShotCacheName(pdf, { page, clip });
|
|
3536
|
+
pinned.add(name);
|
|
3537
|
+
const file = path.join(dir, name);
|
|
3538
|
+
const rendered = await cacheOrTouch(file, async () => {
|
|
3539
|
+
let doc = docs.get(pdf.id);
|
|
3540
|
+
if (!doc) {
|
|
3541
|
+
doc = this._readBlob(pdf.url, signal).then((bytes) => bytes ? this._pdfRasterizer.open(bytes) : null);
|
|
3542
|
+
docs.set(pdf.id, doc);
|
|
3543
|
+
}
|
|
3544
|
+
return (await doc)?.renderPage(page, clip) ?? null;
|
|
3545
|
+
});
|
|
3546
|
+
return rendered ? file : null;
|
|
3547
|
+
}
|
|
2537
3548
|
async _hydrateImage(dir, image, signal) {
|
|
2538
3549
|
const file = path.join(dir, imageCacheName(image));
|
|
3550
|
+
this._mediaRefs.set(imageCacheName(image), { id: image.id });
|
|
2539
3551
|
await cacheOrTouch(file, async () => {
|
|
2540
3552
|
const raw = await this._readBlob(image.url, signal);
|
|
2541
3553
|
if (signal.aborted) {
|
|
@@ -2548,126 +3560,153 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2548
3560
|
});
|
|
2549
3561
|
return file;
|
|
2550
3562
|
}
|
|
2551
|
-
async _hydratePdfPages(messages, signal, runId) {
|
|
3563
|
+
async _hydratePdfPages(messages, { signal, runId, window, reading, textChars, wholeIds, selected }) {
|
|
2552
3564
|
const rendered = new Set();
|
|
3565
|
+
const notices = [];
|
|
2553
3566
|
const turns = messages.filter((turn) => turn.pdfs?.length);
|
|
2554
3567
|
if (!turns.length) {
|
|
2555
|
-
return rendered;
|
|
3568
|
+
return { rendered, notices };
|
|
2556
3569
|
}
|
|
2557
3570
|
const dir = this._imageCacheDir;
|
|
2558
3571
|
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
2559
3572
|
const pinned = this._runImageCacheNames.get(runId) ?? new Set();
|
|
2560
3573
|
this._runImageCacheNames.set(runId, pinned);
|
|
2561
3574
|
const latestUser = messages.findLast((turn) => turn.role === 'user');
|
|
3575
|
+
const measured = await this._pdfReading(window, reading);
|
|
2562
3576
|
for (const turn of turns) {
|
|
2563
|
-
|
|
3577
|
+
const room = pdfImageRoom(measured, turn === latestUser ? textChars : 0);
|
|
2564
3578
|
for (const pdf of turn.pdfs ?? []) {
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
pdf,
|
|
2569
|
-
pinned,
|
|
2570
|
-
maxPages: budget,
|
|
2571
|
-
signal
|
|
2572
|
-
});
|
|
2573
|
-
budget -= pages.tried;
|
|
2574
|
-
if (turn === latestUser && pages.names.length) {
|
|
3579
|
+
const pages = await this._tryHydratePdf({ dir, pdf, pinned, room, signal, selected });
|
|
3580
|
+
attachPages(turn, pages.names.map((name) => path.join(dir, name)));
|
|
3581
|
+
if (pages.names.length && turn === latestUser) {
|
|
2575
3582
|
rendered.add(pdf.id);
|
|
2576
3583
|
}
|
|
3584
|
+
const whole = turn === latestUser && wholeIds.has(pdf.id);
|
|
3585
|
+
for (const notice of pdfNotices(pdf.fileName, pages, whole)) {
|
|
3586
|
+
notices.push({ turn, notice });
|
|
3587
|
+
}
|
|
2577
3588
|
}
|
|
2578
3589
|
delete turn.pdfs;
|
|
2579
3590
|
}
|
|
2580
|
-
return rendered;
|
|
3591
|
+
return { rendered, notices };
|
|
3592
|
+
}
|
|
3593
|
+
async _pdfReading({ ctxSize, modelName }, options) {
|
|
3594
|
+
const custom = await this._customModel(modelName);
|
|
3595
|
+
const window = effectiveCtxSize({ ctxSize, modelName }, { appCtxSize: 0, platform: this._platform, custom });
|
|
3596
|
+
const { cacheTokens } = await this._contextReading(options, window);
|
|
3597
|
+
return { ctxSize: window, cacheTokens };
|
|
3598
|
+
}
|
|
3599
|
+
async _customModel(name) {
|
|
3600
|
+
for await (const list of this._engine.customModelsWatch({})) {
|
|
3601
|
+
return list.models?.find((row) => row.name === name) ?? null;
|
|
3602
|
+
}
|
|
3603
|
+
return null;
|
|
2581
3604
|
}
|
|
2582
|
-
async _tryHydratePdf(
|
|
3605
|
+
async _tryHydratePdf(opts) {
|
|
2583
3606
|
try {
|
|
2584
|
-
return await this._hydratePdf(
|
|
3607
|
+
return await this._hydratePdf(opts);
|
|
2585
3608
|
}
|
|
2586
3609
|
catch (err) {
|
|
2587
3610
|
if (opts.signal.aborted) {
|
|
2588
3611
|
throw err;
|
|
2589
3612
|
}
|
|
2590
3613
|
this._log?.warn('assistant', `pdf rasterization failed — ${opts.pdf.fileName}`, err);
|
|
2591
|
-
return
|
|
3614
|
+
return NO_PDF_PAGES;
|
|
2592
3615
|
}
|
|
2593
3616
|
}
|
|
2594
|
-
async _hydratePdf(
|
|
2595
|
-
const
|
|
2596
|
-
const
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
3617
|
+
async _hydratePdf(hydration) {
|
|
3618
|
+
const { dir, pdf, pinned, room, signal, selected } = hydration;
|
|
3619
|
+
const key = pdf.pages?.blobId ?? pdf.id;
|
|
3620
|
+
const remember = (selection) => {
|
|
3621
|
+
this._pdfSelections.set(key, selection);
|
|
3622
|
+
for (const { page, clip } of selection.shots) {
|
|
3623
|
+
this._mediaRefs.set(pdfShotCacheName(pdf, { page, clip }), { id: pdf.id, page, clip });
|
|
2600
3624
|
}
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
3625
|
+
if (!pdf.pages) {
|
|
3626
|
+
selected.push({ ref: pdf, selection });
|
|
3627
|
+
}
|
|
3628
|
+
};
|
|
3629
|
+
const known = this._pdfSelections.get(key) ?? (await this._storedPdfSelection(pdf, signal));
|
|
3630
|
+
if (known) {
|
|
3631
|
+
remember(known);
|
|
3632
|
+
const cached = await cachedPdfPages(known, { dir, pdf, pinned });
|
|
3633
|
+
if (cached) {
|
|
3634
|
+
return cached;
|
|
2605
3635
|
}
|
|
2606
3636
|
}
|
|
2607
|
-
const pages = await this._renderPdf(dir, { pdf, pinned, maxPages, signal });
|
|
2608
|
-
if (!pages) {
|
|
2609
|
-
return { names: [], tried: 0 };
|
|
2610
|
-
}
|
|
2611
|
-
this._pdfPages.set(key, pages);
|
|
2612
|
-
attachPages(turn, pages.names.map((name) => path.join(dir, name)));
|
|
2613
|
-
return pages;
|
|
2614
|
-
}
|
|
2615
|
-
async _renderPdf(dir, { pdf, pinned, maxPages, signal }) {
|
|
2616
3637
|
const bytes = await this._readBlob(pdf.url, signal);
|
|
2617
3638
|
if (signal.aborted) {
|
|
2618
3639
|
throw new Error('hydration aborted');
|
|
2619
3640
|
}
|
|
2620
|
-
|
|
2621
|
-
return null;
|
|
2622
|
-
}
|
|
2623
|
-
const doc = await this._pdfRasterizer.open(bytes);
|
|
3641
|
+
const doc = bytes ? await this._pdfRasterizer.open(bytes) : null;
|
|
2624
3642
|
if (!doc) {
|
|
2625
|
-
return
|
|
3643
|
+
return NO_PDF_PAGES;
|
|
2626
3644
|
}
|
|
2627
3645
|
try {
|
|
2628
|
-
const
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
for (let page = 0; page < count && tried < maxPages; page++) {
|
|
2632
|
-
if (signal.aborted)
|
|
2633
|
-
throw new Error('hydration aborted');
|
|
2634
|
-
const outcome = await this._renderPdfPage(doc, { dir, pdf, page, pinned, signal });
|
|
2635
|
-
if (outcome.tried) {
|
|
2636
|
-
tried++;
|
|
2637
|
-
}
|
|
2638
|
-
if (outcome.name) {
|
|
2639
|
-
names.push(outcome.name);
|
|
2640
|
-
}
|
|
3646
|
+
const selection = known ?? (await selectPdfPages(doc, room));
|
|
3647
|
+
if (!known) {
|
|
3648
|
+
remember(selection);
|
|
2641
3649
|
}
|
|
2642
|
-
return
|
|
3650
|
+
return await this._renderPdfShots(doc, selection, hydration);
|
|
2643
3651
|
}
|
|
2644
3652
|
finally {
|
|
2645
3653
|
doc.close();
|
|
2646
3654
|
}
|
|
2647
3655
|
}
|
|
2648
|
-
async
|
|
2649
|
-
|
|
3656
|
+
async _storedPdfSelection(pdf, signal) {
|
|
3657
|
+
const bytes = pdf.pages ? await this._readBlob(pdf.pages.url, signal) : null;
|
|
3658
|
+
if (!bytes) {
|
|
3659
|
+
return null;
|
|
3660
|
+
}
|
|
2650
3661
|
try {
|
|
2651
|
-
|
|
2652
|
-
if (flags.hasText && !flags.hasImage) {
|
|
2653
|
-
return { tried };
|
|
2654
|
-
}
|
|
2655
|
-
tried = true;
|
|
2656
|
-
const name = pdfPageCacheName(pdf, page);
|
|
2657
|
-
pinned.add(name);
|
|
2658
|
-
const cached = await cacheOrTouch(path.join(dir, name), () => doc.renderPage(page));
|
|
2659
|
-
return { tried, name: cached ? name : undefined };
|
|
3662
|
+
return parsePdfSelection(bytes);
|
|
2660
3663
|
}
|
|
2661
3664
|
catch (err) {
|
|
2662
|
-
|
|
2663
|
-
|
|
3665
|
+
this._log?.warn('assistant', `pdf page choice unreadable — ${pdf.fileName}`, err);
|
|
3666
|
+
return null;
|
|
3667
|
+
}
|
|
3668
|
+
}
|
|
3669
|
+
async _renderPdfShots(doc, selection, { dir, pdf, pinned, signal }) {
|
|
3670
|
+
const names = [];
|
|
3671
|
+
const labels = [];
|
|
3672
|
+
for (const shot of selection.shots) {
|
|
3673
|
+
if (signal.aborted)
|
|
3674
|
+
throw new Error('hydration aborted');
|
|
3675
|
+
const name = pdfShotCacheName(pdf, shot);
|
|
3676
|
+
pinned.add(name);
|
|
3677
|
+
try {
|
|
3678
|
+
const file = path.join(dir, name);
|
|
3679
|
+
if (await cacheOrTouch(file, () => doc.renderPage(shot.page, shot.clip))) {
|
|
3680
|
+
names.push(name);
|
|
3681
|
+
labels.push(shot.label);
|
|
3682
|
+
}
|
|
3683
|
+
}
|
|
3684
|
+
catch (err) {
|
|
3685
|
+
if (signal.aborted) {
|
|
3686
|
+
throw err;
|
|
3687
|
+
}
|
|
3688
|
+
this._log?.warn('assistant', `pdf page skipped — ${pdf.fileName} p${shot.page}`, err);
|
|
3689
|
+
}
|
|
3690
|
+
}
|
|
3691
|
+
return { selection, names, labels };
|
|
3692
|
+
}
|
|
3693
|
+
async _storePdfSelections(selected, target) {
|
|
3694
|
+
for (const { ref, selection } of selected) {
|
|
3695
|
+
try {
|
|
3696
|
+
await this._uploadGenerated({
|
|
3697
|
+
fileName: pdfSelectionFileName(ref.fileName),
|
|
3698
|
+
mimeType: PDF_PAGES_MIME,
|
|
3699
|
+
bytes: packPdfSelection(selection),
|
|
3700
|
+
raw: true
|
|
3701
|
+
}, { ...target, sourceId: ref.id });
|
|
3702
|
+
}
|
|
3703
|
+
catch (err) {
|
|
3704
|
+
this._log?.warn('assistant', `pdf page choice not stored — ${ref.fileName}`, err);
|
|
2664
3705
|
}
|
|
2665
|
-
this._log?.warn('assistant', `pdf page skipped — ${pdf.fileName} p${page}`, err);
|
|
2666
|
-
return { tried };
|
|
2667
3706
|
}
|
|
2668
3707
|
}
|
|
2669
3708
|
async _compactIfNearFull(params) {
|
|
2670
|
-
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
3709
|
+
const { chatId, transcript, trigger, signal, meshId, run, rounds } = params;
|
|
2671
3710
|
const metrics = await this._lastRunMetrics(chatId, meshId);
|
|
2672
3711
|
if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize)) {
|
|
2673
3712
|
return false;
|
|
@@ -2685,11 +3724,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2685
3724
|
trigger,
|
|
2686
3725
|
signal,
|
|
2687
3726
|
occupancy: metrics,
|
|
2688
|
-
meshId
|
|
3727
|
+
meshId,
|
|
3728
|
+
run,
|
|
3729
|
+
rounds
|
|
2689
3730
|
});
|
|
2690
3731
|
}
|
|
2691
3732
|
async _runCompaction(params) {
|
|
2692
|
-
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
3733
|
+
const { chatId, transcript, trigger, signal, meshId, run, rounds } = params;
|
|
2693
3734
|
let statusId = null;
|
|
2694
3735
|
const started = Date.now();
|
|
2695
3736
|
try {
|
|
@@ -2702,7 +3743,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2702
3743
|
statusId = status.id;
|
|
2703
3744
|
},
|
|
2704
3745
|
meshId,
|
|
2705
|
-
signal
|
|
3746
|
+
signal,
|
|
3747
|
+
run,
|
|
3748
|
+
rounds
|
|
2706
3749
|
});
|
|
2707
3750
|
this._log?.info('runs', 'compaction', {
|
|
2708
3751
|
chatId,
|
|
@@ -2727,6 +3770,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2727
3770
|
}
|
|
2728
3771
|
}
|
|
2729
3772
|
}
|
|
3773
|
+
async _contextReading({ chatId, meshId, folded }, ctxSize) {
|
|
3774
|
+
const last = folded ? {} : await this._lastRunMetrics(chatId, meshId);
|
|
3775
|
+
const window = ctxSize || last.ctxSize;
|
|
3776
|
+
if (!window) {
|
|
3777
|
+
return {};
|
|
3778
|
+
}
|
|
3779
|
+
return last.ctxSize === window
|
|
3780
|
+
? { ctxSize: window, cacheTokens: last.cacheTokens }
|
|
3781
|
+
: { ctxSize: window };
|
|
3782
|
+
}
|
|
2730
3783
|
async _lastRunMetrics(chatId, meshId) {
|
|
2731
3784
|
const deviceId = this._requireDeviceId();
|
|
2732
3785
|
const { chunks } = await meshScoped(this._engine, meshId)
|
|
@@ -2760,29 +3813,30 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2760
3813
|
return { compacted: false, reason: 'unknown chat' };
|
|
2761
3814
|
}
|
|
2762
3815
|
const agent = await engine.getAgent({ agentId: chat.agentId }).catch(noop);
|
|
2763
|
-
if (!agent || !
|
|
3816
|
+
if (!agent || !(options.run || b4a.equals(agent.providerDeviceId, deviceId))) {
|
|
2764
3817
|
return { compacted: false, reason: 'not the provider device' };
|
|
2765
3818
|
}
|
|
2766
|
-
const model =
|
|
3819
|
+
const model = await this._runModel(agent, deviceId);
|
|
2767
3820
|
const config = await engine.getConfig({}).catch(noop);
|
|
2768
3821
|
const ctxSize = agent.ctxSize || config?.ctxSize || undefined;
|
|
2769
3822
|
if (ctxSize !== undefined && !canCompact(ctxSize)) {
|
|
2770
3823
|
return { compacted: false, reason: 'below-floor' };
|
|
2771
3824
|
}
|
|
2772
|
-
const
|
|
3825
|
+
const all = await listHistoryChunks(engine, chatId);
|
|
3826
|
+
const stored = foldableView(all, options.run);
|
|
2773
3827
|
const branches = historyBranches(stored);
|
|
2774
3828
|
const chunks = stored.filter((chunk) => isPromptChunk(chunk, branches));
|
|
2775
3829
|
const { chunks: priorAsks } = await engine
|
|
2776
3830
|
.listChunks({ chatId, type: 'approval-request' })
|
|
2777
3831
|
.catch(() => ({ chunks: [] }));
|
|
2778
3832
|
const lastThroughId = lastCompactedThroughId(chunks);
|
|
2779
|
-
const lastThroughSeq =
|
|
2780
|
-
const
|
|
3833
|
+
const lastThroughSeq = all.find((chunk) => chunk.id === lastThroughId)?.seq ?? -1;
|
|
3834
|
+
const beforeSeq = options.run?.request.seq ?? Infinity;
|
|
3835
|
+
const userTurns = chunks.filter((chunk) => chunk.userMessage && chunk.seq < beforeSeq).length;
|
|
2781
3836
|
let announced = false;
|
|
2782
3837
|
let keepTurns = COMPACTION_KEEP_TURNS;
|
|
2783
|
-
const senders = await this._senderNames(engine, chunks);
|
|
2784
3838
|
for (;;) {
|
|
2785
|
-
const boundary = compactionBoundary(chunks, keepTurns);
|
|
3839
|
+
const boundary = compactionBoundary(chunks, keepTurns, beforeSeq);
|
|
2786
3840
|
if (!boundary) {
|
|
2787
3841
|
return { compacted: false, reason: 'nothing-to-fold' };
|
|
2788
3842
|
}
|
|
@@ -2793,8 +3847,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2793
3847
|
if (through.seq <= lastThroughSeq) {
|
|
2794
3848
|
return { compacted: false, reason: 'already-compacted' };
|
|
2795
3849
|
}
|
|
2796
|
-
const
|
|
2797
|
-
const
|
|
3850
|
+
const folds = foldsThrough(chunks, through);
|
|
3851
|
+
const inRange = chunks.filter((chunk) => chunk.compaction || folds(chunk));
|
|
3852
|
+
const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''));
|
|
2798
3853
|
if (!announced) {
|
|
2799
3854
|
await options.onFold?.();
|
|
2800
3855
|
announced = true;
|
|
@@ -2802,7 +3857,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2802
3857
|
const attempt = await this._summarize(folded, {
|
|
2803
3858
|
model,
|
|
2804
3859
|
agent: withConfigDefaults(agent, config),
|
|
2805
|
-
signal: options.signal
|
|
3860
|
+
signal: options.signal,
|
|
3861
|
+
rounds: options.rounds
|
|
2806
3862
|
});
|
|
2807
3863
|
if (!attempt.summary) {
|
|
2808
3864
|
if (attempt.overflowed && boundary.foldedTurns > 1) {
|
|
@@ -2833,9 +3889,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2833
3889
|
return { compacted: true, foldedTurns: boundary.foldedTurns };
|
|
2834
3890
|
}
|
|
2835
3891
|
}
|
|
2836
|
-
async _summarize(turns, { model, agent, signal }) {
|
|
3892
|
+
async _summarize(turns, { model, agent, signal, rounds }) {
|
|
2837
3893
|
const hasPriorSummary = turns[0]?.content.startsWith(COMPACTION_TURN_PREFIX) === true;
|
|
2838
|
-
const
|
|
3894
|
+
const prompt = buildCompactionPrompt(nameSenders(turns).map((turn) => ({ role: turn.role, content: turn.content })), hasPriorSummary);
|
|
3895
|
+
const round = this._probeRounds(rounds);
|
|
3896
|
+
if (round) {
|
|
3897
|
+
return this._summarizeInRound(round, { prompt, model, signal });
|
|
3898
|
+
}
|
|
3899
|
+
const events = this._harness.completion({
|
|
2839
3900
|
model,
|
|
2840
3901
|
agent: {
|
|
2841
3902
|
...agent,
|
|
@@ -2844,12 +3905,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2844
3905
|
skillsScope: 'selected',
|
|
2845
3906
|
enabledSkills: []
|
|
2846
3907
|
},
|
|
2847
|
-
messages: [
|
|
2848
|
-
{
|
|
2849
|
-
role: 'user',
|
|
2850
|
-
content: buildCompactionPrompt(nameSenders(turns).map((turn) => ({ role: turn.role, content: turn.content })), hasPriorSummary)
|
|
2851
|
-
}
|
|
2852
|
-
],
|
|
3908
|
+
messages: [{ role: 'user', content: prompt }],
|
|
2853
3909
|
generationParams: { reasoning_budget: 0 },
|
|
2854
3910
|
signal: signal ?? this._lifecycle.signal
|
|
2855
3911
|
});
|
|
@@ -2877,9 +3933,26 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2877
3933
|
}
|
|
2878
3934
|
return { summary: parseCompactionSummary(text), overflowed: false };
|
|
2879
3935
|
}
|
|
2880
|
-
async
|
|
3936
|
+
async _summarizeInRound(rounds, { prompt, model, signal }) {
|
|
2881
3937
|
try {
|
|
2882
|
-
const
|
|
3938
|
+
const text = await rounds.probe({
|
|
3939
|
+
kind: 'fold',
|
|
3940
|
+
prompt,
|
|
3941
|
+
modelId: model,
|
|
3942
|
+
signal: signal ?? this._lifecycle.signal
|
|
3943
|
+
});
|
|
3944
|
+
return { summary: parseCompactionSummary(text), overflowed: false };
|
|
3945
|
+
}
|
|
3946
|
+
catch (err) {
|
|
3947
|
+
const failed = errorMessage(err);
|
|
3948
|
+
this._log?.warn('runs', 'summarizer round failed', { failed });
|
|
3949
|
+
return { summary: null, overflowed: isContextOverflowFailure(failed) };
|
|
3950
|
+
}
|
|
3951
|
+
}
|
|
3952
|
+
async _autoTitle(engine, { chatId, history, messages, model, agent, probe, rounds }) {
|
|
3953
|
+
try {
|
|
3954
|
+
const first = history.find((chunk) => chunk.userMessage?.text.trim())?.userMessage?.text ??
|
|
3955
|
+
messages.find((turn) => turn.role === 'user' && turn.content.trim())?.content;
|
|
2883
3956
|
if (!first) {
|
|
2884
3957
|
return;
|
|
2885
3958
|
}
|
|
@@ -2887,14 +3960,62 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2887
3960
|
if (!isDefaultChatTitle(chat?.title)) {
|
|
2888
3961
|
return;
|
|
2889
3962
|
}
|
|
2890
|
-
await this._generateChatTitle({ engine, chatId, input: first
|
|
3963
|
+
await this._generateChatTitle({ engine, chatId, input: first, model, agent, probe, rounds });
|
|
2891
3964
|
}
|
|
2892
3965
|
catch (err) {
|
|
2893
3966
|
this._log?.warn('runs', 'auto-title skipped', { chatId, err });
|
|
2894
3967
|
}
|
|
2895
3968
|
}
|
|
3969
|
+
_probeRounds(rounds) {
|
|
3970
|
+
return this._roundHost ? rounds : undefined;
|
|
3971
|
+
}
|
|
3972
|
+
async _suggestNext(engine, { chatId, runId, messages, answer, model, agent, rounds }) {
|
|
3973
|
+
const question = messages.findLast((turn) => turn.role === 'user' && turn.content.trim());
|
|
3974
|
+
if (!question || this.closing || !(await this._suggestingPrompts())) {
|
|
3975
|
+
return;
|
|
3976
|
+
}
|
|
3977
|
+
const { chunks } = await engine.listChunks({ chatId, type: 'run-request' });
|
|
3978
|
+
if ((chunks ?? []).at(-1)?.runId !== runId) {
|
|
3979
|
+
return;
|
|
3980
|
+
}
|
|
3981
|
+
const controller = new AbortController();
|
|
3982
|
+
this._suggesting.get(chatId)?.abort(SUGGESTION_SUPERSEDED);
|
|
3983
|
+
this._suggesting.set(chatId, controller);
|
|
3984
|
+
try {
|
|
3985
|
+
const raw = await this._probeCompletion({
|
|
3986
|
+
chatId,
|
|
3987
|
+
input: buildSuggestionPrompt(question.content, answer),
|
|
3988
|
+
model,
|
|
3989
|
+
agent,
|
|
3990
|
+
signal: controller.signal,
|
|
3991
|
+
rounds
|
|
3992
|
+
}, SUGGESTION_PROBE);
|
|
3993
|
+
const text = parseSuggestionResponse(raw);
|
|
3994
|
+
if (text && !controller.signal.aborted) {
|
|
3995
|
+
await engine.setChatSuggestion({ chatId, suggestion: { text, runId } });
|
|
3996
|
+
}
|
|
3997
|
+
}
|
|
3998
|
+
catch (err) {
|
|
3999
|
+
this._log?.warn('runs', 'next-message suggestion skipped', { chatId, err });
|
|
4000
|
+
}
|
|
4001
|
+
finally {
|
|
4002
|
+
if (this._suggesting.get(chatId) === controller) {
|
|
4003
|
+
this._suggesting.delete(chatId);
|
|
4004
|
+
}
|
|
4005
|
+
}
|
|
4006
|
+
}
|
|
4007
|
+
async _suggestingPrompts() {
|
|
4008
|
+
try {
|
|
4009
|
+
const { value } = await this._engine.configGet({ key: SUGGEST_PROMPTS_KEY });
|
|
4010
|
+
return value === SUGGEST_PROMPTS_ON;
|
|
4011
|
+
}
|
|
4012
|
+
catch (err) {
|
|
4013
|
+
this._log?.warn('assistant', 'suggestion setting unreadable', { err });
|
|
4014
|
+
return false;
|
|
4015
|
+
}
|
|
4016
|
+
}
|
|
2896
4017
|
async _generateChatTitle(params) {
|
|
2897
|
-
const { engine, chatId, input, model, agent, probe } = params;
|
|
4018
|
+
const { engine, chatId, input, model, agent, probe, rounds } = params;
|
|
2898
4019
|
const prompt = input.trim();
|
|
2899
4020
|
if (!prompt) {
|
|
2900
4021
|
return null;
|
|
@@ -2907,7 +4028,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2907
4028
|
input: prompt,
|
|
2908
4029
|
model,
|
|
2909
4030
|
agent,
|
|
2910
|
-
signal: this._lifecycle.signal
|
|
4031
|
+
signal: this._lifecycle.signal,
|
|
4032
|
+
rounds
|
|
2911
4033
|
});
|
|
2912
4034
|
}
|
|
2913
4035
|
catch (err) {
|
|
@@ -2915,7 +4037,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2915
4037
|
}
|
|
2916
4038
|
}
|
|
2917
4039
|
const title = generated ?? deriveChatTitle(prompt);
|
|
2918
|
-
if (!title) {
|
|
4040
|
+
if (!title || !isDefaultChatTitle((await engine.getChat({ chatId }))?.title)) {
|
|
2919
4041
|
return null;
|
|
2920
4042
|
}
|
|
2921
4043
|
await engine.renameChat({ chatId, config: { title } });
|
|
@@ -2927,8 +4049,29 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2927
4049
|
return title;
|
|
2928
4050
|
}
|
|
2929
4051
|
async _titleFromCompletion(params) {
|
|
2930
|
-
const
|
|
2931
|
-
|
|
4052
|
+
const prompt = buildTitlePrompt(params.input);
|
|
4053
|
+
return parseTitleResponse(await this._probeCompletion({ ...params, input: prompt }, TITLE_PROBE));
|
|
4054
|
+
}
|
|
4055
|
+
async _probeCompletion(params, { kind, field, name, runId }) {
|
|
4056
|
+
const { chatId, input, model, agent, signal, rounds } = params;
|
|
4057
|
+
const responseFormat = {
|
|
4058
|
+
type: 'json_schema',
|
|
4059
|
+
json_schema: {
|
|
4060
|
+
name,
|
|
4061
|
+
schema: {
|
|
4062
|
+
type: 'object',
|
|
4063
|
+
properties: { [field]: { type: 'string' } },
|
|
4064
|
+
required: [field],
|
|
4065
|
+
additionalProperties: false
|
|
4066
|
+
}
|
|
4067
|
+
}
|
|
4068
|
+
};
|
|
4069
|
+
const round = this._probeRounds(rounds);
|
|
4070
|
+
if (round) {
|
|
4071
|
+
const probed = { kind, prompt: input, modelId: model, responseFormat };
|
|
4072
|
+
return round.probe({ ...probed, signal: signal ?? this._lifecycle.signal });
|
|
4073
|
+
}
|
|
4074
|
+
const events = this._harness.completion({
|
|
2932
4075
|
model,
|
|
2933
4076
|
agent: {
|
|
2934
4077
|
...agent,
|
|
@@ -2937,21 +4080,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2937
4080
|
skillsScope: 'selected',
|
|
2938
4081
|
enabledSkills: []
|
|
2939
4082
|
},
|
|
2940
|
-
messages: [{ role: 'user', content:
|
|
4083
|
+
messages: [{ role: 'user', content: input }],
|
|
2941
4084
|
generationParams: { reasoning_budget: 0 },
|
|
2942
4085
|
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
|
-
},
|
|
4086
|
+
responseFormat,
|
|
2955
4087
|
signal
|
|
2956
4088
|
});
|
|
2957
4089
|
let text = '';
|
|
@@ -2961,16 +4093,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2961
4093
|
}
|
|
2962
4094
|
else if (event.type === 'inference-call') {
|
|
2963
4095
|
void this._inferenceCalls
|
|
2964
|
-
.record(chatId, { runId
|
|
2965
|
-
.catch((err) => this._log?.warn('assistant', '
|
|
4096
|
+
.record(chatId, { runId, at: Date.now(), call: asRecord(event.call) })
|
|
4097
|
+
.catch((err) => this._log?.warn('assistant', 'probe call not recorded', { runId, err }));
|
|
2966
4098
|
}
|
|
2967
4099
|
else if (event.type === 'error' || event.type === 'aborted' || event.type === 'done') {
|
|
2968
4100
|
break;
|
|
2969
4101
|
}
|
|
2970
4102
|
}
|
|
2971
|
-
return
|
|
4103
|
+
return text;
|
|
2972
4104
|
}
|
|
2973
|
-
_createApprove(engine, { chatId, runId, deviceId, agentId, hasRun }) {
|
|
4105
|
+
_createApprove(engine, { chatId, runId, deviceId, agentId, hasRun, repeats }) {
|
|
2974
4106
|
const waiters = new Map();
|
|
2975
4107
|
let latestChunks = [];
|
|
2976
4108
|
let watcher = null;
|
|
@@ -3042,14 +4174,6 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3042
4174
|
if (!ask || ask.name !== call.name) {
|
|
3043
4175
|
return false;
|
|
3044
4176
|
}
|
|
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
4177
|
return ((ask.resource?.kind ?? null) === (call.resource?.kind ?? null) &&
|
|
3054
4178
|
(ask.resource?.label ?? null) === (call.resource?.label ?? null) &&
|
|
3055
4179
|
JSON.stringify(ask.arguments) === JSON.stringify(call.arguments));
|
|
@@ -3067,12 +4191,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3067
4191
|
this._log?.warn('approvals', 'rule lookup failed, asking instead', { runId, err });
|
|
3068
4192
|
return { rules: [], ourMemberId: undefined };
|
|
3069
4193
|
});
|
|
3070
|
-
const
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
4194
|
+
const repeated = repeats(call);
|
|
4195
|
+
const rule = repeated
|
|
4196
|
+
? null
|
|
4197
|
+
: matchApprovalRule(rules ?? [], {
|
|
4198
|
+
toolName: call.name,
|
|
4199
|
+
resource: call.resource,
|
|
4200
|
+
agentId,
|
|
4201
|
+
chatId,
|
|
4202
|
+
memberId: ourMemberId
|
|
4203
|
+
});
|
|
3076
4204
|
if (rule) {
|
|
3077
4205
|
this._log?.info('approvals', `rule ${rule.decision}`, {
|
|
3078
4206
|
runId,
|
|
@@ -3095,12 +4223,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3095
4223
|
});
|
|
3096
4224
|
return park(prior.id, call.name);
|
|
3097
4225
|
}
|
|
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()) {
|
|
4226
|
+
if (!hasRun() && !repeated) {
|
|
3104
4227
|
this._log?.info('approvals', `answer ${status} (from before the app closed)`, {
|
|
3105
4228
|
runId,
|
|
3106
4229
|
chatId,
|
|
@@ -3109,7 +4232,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3109
4232
|
});
|
|
3110
4233
|
return status === 'approved';
|
|
3111
4234
|
}
|
|
3112
|
-
this._log?.info('approvals', 'asking again:
|
|
4235
|
+
this._log?.info('approvals', 'asking again: the replay would repeat a call that ran', {
|
|
3113
4236
|
runId,
|
|
3114
4237
|
chatId,
|
|
3115
4238
|
priorChunkId: prior.id,
|
|
@@ -3123,13 +4246,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3123
4246
|
resource: call.resource ?? undefined,
|
|
3124
4247
|
agentId
|
|
3125
4248
|
};
|
|
3126
|
-
const identity = callIdentity(call);
|
|
3127
4249
|
const chunk = await engine.appendChunk({
|
|
3128
4250
|
chatId,
|
|
3129
4251
|
runId,
|
|
3130
4252
|
type: 'approval-request',
|
|
3131
|
-
callId: identity.callId,
|
|
3132
|
-
round: identity.round,
|
|
3133
4253
|
approvalRequest,
|
|
3134
4254
|
originDeviceId: deviceId,
|
|
3135
4255
|
effect: call.effect === 'read' ? 'read' : 'write',
|
|
@@ -3176,7 +4296,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3176
4296
|
if (known) {
|
|
3177
4297
|
return known;
|
|
3178
4298
|
}
|
|
3179
|
-
if (this.
|
|
4299
|
+
if (this._backgrounded) {
|
|
4300
|
+
this._backgroundReplays.add(runId);
|
|
3180
4301
|
return { kind: 'replay' };
|
|
3181
4302
|
}
|
|
3182
4303
|
if (reason !== undefined) {
|
|
@@ -3210,21 +4331,39 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3210
4331
|
}
|
|
3211
4332
|
};
|
|
3212
4333
|
}
|
|
3213
|
-
_watchOwnership(engine, agentId, controller) {
|
|
4334
|
+
_watchOwnership(engine, { request, agentId, controller }) {
|
|
3214
4335
|
const deviceId = this._requireDeviceId();
|
|
3215
|
-
const
|
|
4336
|
+
const watches = [
|
|
4337
|
+
this._abortOnLoss(engine.agentsWatch({}), controller, async ({ agents }) => {
|
|
4338
|
+
const row = (agents ?? []).find((a) => a.id === agentId);
|
|
4339
|
+
if (row && !row.deletedAt) {
|
|
4340
|
+
return drivenBy({ request, agent: row }, deviceId) ? null : OWNERSHIP_LOST;
|
|
4341
|
+
}
|
|
4342
|
+
const chat = await engine.getChat({ chatId: request.chatId }).catch(noop);
|
|
4343
|
+
return !chat || chat.agentId === agentId ? AGENT_REMOVED : null;
|
|
4344
|
+
})
|
|
4345
|
+
];
|
|
4346
|
+
if (request.runnerDeviceId) {
|
|
4347
|
+
watches.push(this._abortOnLoss(engine.runRequestsWatch({}), controller, ({ chunks }) => {
|
|
4348
|
+
const now = (chunks ?? []).find((chunk) => chunk.id === request.id);
|
|
4349
|
+
return !now?.runTakenBy || !b4a.equals(now.runTakenBy, deviceId) ? OWNERSHIP_LOST : null;
|
|
4350
|
+
}));
|
|
4351
|
+
}
|
|
4352
|
+
return {
|
|
4353
|
+
stop: () => watches.forEach((stop) => stop())
|
|
4354
|
+
};
|
|
4355
|
+
}
|
|
4356
|
+
_abortOnLoss(frames, controller, lost) {
|
|
4357
|
+
const it = frames[Symbol.asyncIterator]();
|
|
3216
4358
|
const watching = (async () => {
|
|
3217
4359
|
while (true) {
|
|
3218
4360
|
const { value, done } = await it.next();
|
|
3219
4361
|
if (done) {
|
|
3220
4362
|
return;
|
|
3221
4363
|
}
|
|
3222
|
-
const
|
|
3223
|
-
if (
|
|
3224
|
-
|
|
3225
|
-
!row.providerDeviceId ||
|
|
3226
|
-
!b4a.equals(row.providerDeviceId, deviceId)) {
|
|
3227
|
-
controller.abort(OWNERSHIP_LOST);
|
|
4364
|
+
const reason = await lost(value);
|
|
4365
|
+
if (reason) {
|
|
4366
|
+
controller.abort(reason);
|
|
3228
4367
|
return;
|
|
3229
4368
|
}
|
|
3230
4369
|
}
|
|
@@ -3234,14 +4373,43 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3234
4373
|
return;
|
|
3235
4374
|
this._log?.error('assistant', 'ownership watch failed', err);
|
|
3236
4375
|
});
|
|
3237
|
-
return {
|
|
3238
|
-
|
|
3239
|
-
it.return?.()?.catch?.(noop);
|
|
3240
|
-
}
|
|
4376
|
+
return () => {
|
|
4377
|
+
it.return?.()?.catch?.(noop);
|
|
3241
4378
|
};
|
|
3242
4379
|
}
|
|
3243
|
-
|
|
4380
|
+
_runsChanged() {
|
|
3244
4381
|
this._downloads?.yieldToRuns([...this._runs.values()].filter((run) => run.executing).map((run) => run.model));
|
|
4382
|
+
if (this._generating()) {
|
|
4383
|
+
return;
|
|
4384
|
+
}
|
|
4385
|
+
for (const wake of [...this._idleWaiters]) {
|
|
4386
|
+
wake();
|
|
4387
|
+
}
|
|
4388
|
+
}
|
|
4389
|
+
_generating() {
|
|
4390
|
+
return [...this._runs.values()].some((run) => run.executing && !run.preparing);
|
|
4391
|
+
}
|
|
4392
|
+
_markPreparing(runId, preparing) {
|
|
4393
|
+
const run = this._runs.get(runId);
|
|
4394
|
+
if (!run) {
|
|
4395
|
+
return;
|
|
4396
|
+
}
|
|
4397
|
+
this._runs.set(runId, { ...run, preparing });
|
|
4398
|
+
this._runsChanged();
|
|
4399
|
+
}
|
|
4400
|
+
idle(signal) {
|
|
4401
|
+
if (signal.aborted || !this._generating()) {
|
|
4402
|
+
return Promise.resolve();
|
|
4403
|
+
}
|
|
4404
|
+
return new Promise((resolve) => {
|
|
4405
|
+
const wake = () => {
|
|
4406
|
+
this._idleWaiters.delete(wake);
|
|
4407
|
+
signal.removeEventListener('abort', wake);
|
|
4408
|
+
resolve();
|
|
4409
|
+
};
|
|
4410
|
+
this._idleWaiters.add(wake);
|
|
4411
|
+
signal.addEventListener('abort', wake, { once: true });
|
|
4412
|
+
});
|
|
3245
4413
|
}
|
|
3246
4414
|
_logModelStatus(state) {
|
|
3247
4415
|
if (typeof state !== 'object' || state === null)
|
|
@@ -3272,6 +4440,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3272
4440
|
let transcribing = false;
|
|
3273
4441
|
let documents = false;
|
|
3274
4442
|
let sampling = false;
|
|
4443
|
+
let analyzing = false;
|
|
3275
4444
|
const apply = (progress) => {
|
|
3276
4445
|
const runStatus = progress
|
|
3277
4446
|
? { state: 'executing', progress }
|
|
@@ -3279,12 +4448,15 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3279
4448
|
if (documents) {
|
|
3280
4449
|
runStatus.reason = 'preparing-documents';
|
|
3281
4450
|
}
|
|
4451
|
+
else if (analyzing && !progress) {
|
|
4452
|
+
runStatus.reason = 'analyzing-images';
|
|
4453
|
+
}
|
|
3282
4454
|
const key = JSON.stringify(runStatus);
|
|
3283
4455
|
if (key === last) {
|
|
3284
4456
|
return;
|
|
3285
4457
|
}
|
|
3286
4458
|
last = key;
|
|
3287
|
-
showing = progress !== null ||
|
|
4459
|
+
showing = progress !== null || runStatus.reason !== undefined;
|
|
3288
4460
|
update(runStatus).catch(noop);
|
|
3289
4461
|
};
|
|
3290
4462
|
const onModel = (state) => {
|
|
@@ -3293,9 +4465,11 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3293
4465
|
if (state.status === 'loading') {
|
|
3294
4466
|
apply({ phase: 'loading', model: state.model });
|
|
3295
4467
|
}
|
|
4468
|
+
else if (state.status === 'held') {
|
|
4469
|
+
apply({ phase: 'awaiting-download', model: state.model });
|
|
4470
|
+
}
|
|
3296
4471
|
else if (state.status === 'downloading') {
|
|
3297
|
-
|
|
3298
|
-
apply({ phase: 'downloading', model: state.model, percentage });
|
|
4472
|
+
apply(downloadingProgress(state.model, state.percentage ?? 0));
|
|
3299
4473
|
}
|
|
3300
4474
|
else {
|
|
3301
4475
|
apply(transcribing ? { phase: 'transcribing' } : null);
|
|
@@ -3320,9 +4494,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3320
4494
|
}
|
|
3321
4495
|
apply({ phase: 'generating', percentage: Math.floor(progress.percentage / 10) * 10 });
|
|
3322
4496
|
},
|
|
4497
|
+
request: (messages, sent) => {
|
|
4498
|
+
if (claimNewImages(messages, sent)) {
|
|
4499
|
+
analyzing = true;
|
|
4500
|
+
apply(null);
|
|
4501
|
+
}
|
|
4502
|
+
},
|
|
3323
4503
|
stop: () => this._harness.off?.('model', onModel),
|
|
3324
4504
|
clear: () => {
|
|
3325
4505
|
sampling = false;
|
|
4506
|
+
analyzing = false;
|
|
3326
4507
|
if (!showing) {
|
|
3327
4508
|
return;
|
|
3328
4509
|
}
|
|
@@ -3333,14 +4514,47 @@ export class QvacAssistant extends ReadyResource {
|
|
|
3333
4514
|
};
|
|
3334
4515
|
}
|
|
3335
4516
|
}
|
|
3336
|
-
|
|
4517
|
+
function notReady(search) {
|
|
4518
|
+
return {
|
|
4519
|
+
chatFilesNotReady: search.chatFilesNotReady ?? 0,
|
|
4520
|
+
projectFilesNotReady: search.projectFilesNotReady ?? 0
|
|
4521
|
+
};
|
|
4522
|
+
}
|
|
4523
|
+
function notReadyCount(search) {
|
|
4524
|
+
const { chatFilesNotReady, projectFilesNotReady } = notReady(search);
|
|
4525
|
+
return chatFilesNotReady + projectFilesNotReady;
|
|
4526
|
+
}
|
|
4527
|
+
function notReadyLine(search) {
|
|
4528
|
+
const { projectFilesNotReady } = notReady(search);
|
|
4529
|
+
const chat = search.pendingFiles ?? [];
|
|
4530
|
+
const parts = [];
|
|
4531
|
+
if (chat.length) {
|
|
4532
|
+
parts.push(`${filesCount(chat.length, 'chat')} (${chat.join(', ')})`);
|
|
4533
|
+
}
|
|
4534
|
+
if (projectFilesNotReady) {
|
|
4535
|
+
parts.push(filesCount(projectFilesNotReady, 'project'));
|
|
4536
|
+
}
|
|
4537
|
+
return parts.length
|
|
4538
|
+
? `[Still indexing and not used for this reply: ${parts.join(' and ')}.]`
|
|
4539
|
+
: null;
|
|
4540
|
+
}
|
|
4541
|
+
function filesCount(count, source) {
|
|
4542
|
+
return `${count} ${source} file${count === 1 ? '' : 's'}`;
|
|
4543
|
+
}
|
|
4544
|
+
function chunkSources(records) {
|
|
4545
|
+
return records.map((record) => ({ ...record, page: record.page ?? 0 }));
|
|
4546
|
+
}
|
|
4547
|
+
function downloadingProgress(model, percentage) {
|
|
4548
|
+
return { phase: 'downloading', model, percentage: Math.floor(percentage / 10) * 10 };
|
|
4549
|
+
}
|
|
4550
|
+
async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, pictured, injected }) {
|
|
3337
4551
|
const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
|
|
3338
4552
|
const notice = chooseRagNotice({
|
|
3339
4553
|
errorMessage: knowledge.search.errorMessage,
|
|
3340
4554
|
hitCount: records.length,
|
|
3341
4555
|
searchedCount: knowledge.search.searchedCount ?? 0,
|
|
3342
4556
|
indexEmpty: Boolean(knowledge.search.indexEmpty),
|
|
3343
|
-
hasKnowledge:
|
|
4557
|
+
hasKnowledge: !!knowledge.search.hasKnowledge || notReadyCount(knowledge.search) > 0,
|
|
3344
4558
|
pendingReferenced: Boolean(knowledge.search.pendingReferenced),
|
|
3345
4559
|
failedFiles: knowledge.search.failedFiles ?? [],
|
|
3346
4560
|
pendingFiles: knowledge.search.pendingFiles ?? []
|
|
@@ -3352,11 +4566,17 @@ async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter
|
|
|
3352
4566
|
})));
|
|
3353
4567
|
await transcript.chunk({
|
|
3354
4568
|
type: 'sources',
|
|
3355
|
-
sources: {
|
|
4569
|
+
sources: {
|
|
4570
|
+
sources: chunkSources(records),
|
|
4571
|
+
notice: notice ?? undefined,
|
|
4572
|
+
...notReady(knowledge.search)
|
|
4573
|
+
}
|
|
3356
4574
|
});
|
|
3357
4575
|
injected.sourcesWritten = true;
|
|
3358
4576
|
if (records.length > 0) {
|
|
3359
|
-
|
|
4577
|
+
const framing = formatRagContext(knowledge.hits, knowledge.search.availableFiles ?? []);
|
|
4578
|
+
const pages = pictured ? `\n${PDF_PAGES_SOURCE_LINE}` : '';
|
|
4579
|
+
knowledge.lastUser.content = `${framing}${pages}\n\nThe user's message:\n${knowledge.lastUser.content}\n\n${RAG_CITATION_LINE}`;
|
|
3360
4580
|
injected.ragInjected = true;
|
|
3361
4581
|
injected.documentsInjected = true;
|
|
3362
4582
|
}
|
|
@@ -3369,21 +4589,41 @@ function toolCallChunk(event) {
|
|
|
3369
4589
|
return {
|
|
3370
4590
|
type: 'tool-call',
|
|
3371
4591
|
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
4592
|
effect: event.effect === 'read' ? 'read' : 'write',
|
|
3375
4593
|
display: event.display,
|
|
3376
|
-
rawCall: typeof event.rawCall === 'string' ? event.rawCall : undefined
|
|
4594
|
+
rawCall: typeof event.rawCall === 'string' ? event.rawCall : undefined,
|
|
4595
|
+
...callOf(event)
|
|
3377
4596
|
};
|
|
3378
4597
|
}
|
|
3379
4598
|
function toolResultChunk(event) {
|
|
3380
4599
|
return {
|
|
3381
4600
|
type: 'tool-result',
|
|
3382
|
-
toolResult: { name: event.name, result: withoutDataChannel(event.result) },
|
|
3383
|
-
|
|
4601
|
+
toolResult: { name: event.name, result: withoutDataChannel(event.result) ?? null },
|
|
4602
|
+
...callOf(event)
|
|
4603
|
+
};
|
|
4604
|
+
}
|
|
4605
|
+
const ROUND_WRITES = new Set(['content', 'thinking', 'round-reset', 'tool-call', 'tool-result']);
|
|
4606
|
+
function afterRounds(event, base) {
|
|
4607
|
+
const shifts = base > 0 && ROUND_WRITES.has(event.type) && typeof event.round === 'number';
|
|
4608
|
+
return shifts ? { ...event, round: Number(event.round) + base } : event;
|
|
4609
|
+
}
|
|
4610
|
+
function sameToolCall(chunk, call) {
|
|
4611
|
+
return (chunk.toolCall?.name === call.name &&
|
|
4612
|
+
JSON.stringify(chunk.toolCall.arguments ?? null) === JSON.stringify(call.arguments ?? null));
|
|
4613
|
+
}
|
|
4614
|
+
function roundOf(event) {
|
|
4615
|
+
return typeof event.round === 'number' ? event.round : undefined;
|
|
4616
|
+
}
|
|
4617
|
+
function callOf(event) {
|
|
4618
|
+
return {
|
|
4619
|
+
round: roundOf(event),
|
|
3384
4620
|
callId: typeof event.callId === 'string' ? event.callId : undefined
|
|
3385
4621
|
};
|
|
3386
4622
|
}
|
|
4623
|
+
function replacedTypes(event) {
|
|
4624
|
+
const types = Array.isArray(event.replacedTypes) ? event.replacedTypes : ['content', 'thinking'];
|
|
4625
|
+
return types.filter((type) => type === 'content' || type === 'thinking');
|
|
4626
|
+
}
|
|
3387
4627
|
function runMetricsSummary(metrics) {
|
|
3388
4628
|
const { timeToFirstToken, promptTokens, promptTokensPerSecond, cacheTokens, tokensPerSecond, totalTokens, durationMs, stopReason } = metrics;
|
|
3389
4629
|
return {
|
|
@@ -3397,88 +4637,12 @@ function runMetricsSummary(metrics) {
|
|
|
3397
4637
|
stopReason
|
|
3398
4638
|
};
|
|
3399
4639
|
}
|
|
4640
|
+
function withCacheRebuild(metrics, reason, tokens) {
|
|
4641
|
+
return reason === null ? metrics : { ...metrics, cacheRebuild: { reason, tokens } };
|
|
4642
|
+
}
|
|
3400
4643
|
function asRecord(value) {
|
|
3401
4644
|
return isRecord(value) ? { ...value } : {};
|
|
3402
4645
|
}
|
|
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
4646
|
function isStreamDestroyed(err) {
|
|
3483
4647
|
return isRecord(err) && err.code === 'STREAM_DESTROYED';
|
|
3484
4648
|
}
|