@qvac/core 0.5.0 → 0.6.1
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 +92 -1
- package/dist-lib/lib/api/agents.js +3 -2
- package/dist-lib/lib/api/app-config.d.ts +1 -1
- package/dist-lib/lib/api/app-config.js +54 -15
- package/dist-lib/lib/api/attachments.d.ts +6 -5
- package/dist-lib/lib/api/attachments.js +4 -2
- package/dist-lib/lib/api/chat.d.ts +7 -3
- package/dist-lib/lib/api/chat.js +4 -4
- package/dist-lib/lib/api/chunks.js +3 -3
- package/dist-lib/lib/api/config.js +2 -2
- package/dist-lib/lib/api/current-mesh.d.ts +3 -4
- package/dist-lib/lib/api/current-mesh.js +9 -4
- package/dist-lib/lib/api/custom-models.d.ts +8 -1
- package/dist-lib/lib/api/custom-models.js +53 -16
- package/dist-lib/lib/api/devices.d.ts +14 -5
- package/dist-lib/lib/api/devices.js +13 -8
- package/dist-lib/lib/api/diagnostics.d.ts +1 -0
- package/dist-lib/lib/api/diagnostics.js +1 -1
- package/dist-lib/lib/api/effective-ctx-size.d.ts +6 -1
- package/dist-lib/lib/api/effective-ctx-size.js +5 -3
- package/dist-lib/lib/api/execution.js +2 -1
- package/dist-lib/lib/api/files.js +2 -1
- package/dist-lib/lib/api/index.d.ts +5 -1
- package/dist-lib/lib/api/index.js +101 -102
- package/dist-lib/lib/api/instrument.js +29 -20
- package/dist-lib/lib/api/knowledge.d.ts +6 -5
- package/dist-lib/lib/api/knowledge.js +8 -6
- package/dist-lib/lib/api/local-files.js +1 -1
- package/dist-lib/lib/api/logs.js +6 -3
- package/dist-lib/lib/api/members.js +22 -13
- package/dist-lib/lib/api/mesh.js +3 -3
- package/dist-lib/lib/api/model-download.js +1 -1
- package/dist-lib/lib/api/network.d.ts +1 -0
- package/dist-lib/lib/api/network.js +1 -1
- package/dist-lib/lib/api/notifications.js +23 -13
- package/dist-lib/lib/api/rag.d.ts +6 -1
- package/dist-lib/lib/api/rag.js +20 -11
- package/dist-lib/lib/api/range.d.ts +2 -2
- package/dist-lib/lib/api/sync.js +1 -1
- package/dist-lib/lib/api/tool-activity.js +7 -3
- package/dist-lib/lib/api/tool-configs.js +2 -4
- package/dist-lib/lib/api/upload-frames.d.ts +7 -8
- package/dist-lib/lib/api/upload-frames.js +2 -1
- package/dist-lib/lib/api/watchable.d.ts +6 -1
- package/dist-lib/lib/api/watchable.js +23 -14
- package/dist-lib/lib/assistant/fetch-body.js +4 -2
- package/dist-lib/lib/assistant/harness.d.ts +48 -27
- package/dist-lib/lib/assistant/harness.js +40 -16
- package/dist-lib/lib/assistant/history.d.ts +9 -7
- package/dist-lib/lib/assistant/history.js +77 -42
- package/dist-lib/lib/assistant/message.d.ts +5 -3
- package/dist-lib/lib/assistant/message.js +4 -2
- package/dist-lib/lib/assistant/skill-encode.d.ts +3 -1
- package/dist-lib/lib/assistant/skill-encode.js +52 -62
- package/dist-lib/lib/assistant-client.js +7 -4
- package/dist-lib/lib/assistant-tree.js +27 -11
- package/dist-lib/lib/assistant.d.ts +31 -11
- package/dist-lib/lib/assistant.js +846 -390
- package/dist-lib/lib/audio-format.d.ts +1 -1
- package/dist-lib/lib/audio-format.js +1 -1
- package/dist-lib/lib/chat-title.js +9 -4
- package/dist-lib/lib/client.js +3 -2
- package/dist-lib/lib/collabora/convert.d.ts +2 -2
- package/dist-lib/lib/collabora/convert.js +6 -3
- package/dist-lib/lib/collabora/entry.js +9 -5
- package/dist-lib/lib/collabora/protocol.d.ts +2 -2
- package/dist-lib/lib/collabora/protocol.js +14 -7
- package/dist-lib/lib/collabora/worker.js +14 -7
- package/dist-lib/lib/compaction.d.ts +5 -6
- package/dist-lib/lib/compaction.js +14 -7
- package/dist-lib/lib/core/account-recovery.d.ts +4 -3
- package/dist-lib/lib/core/account-recovery.js +7 -4
- package/dist-lib/lib/core/agent.js +17 -15
- package/dist-lib/lib/core/approval-rules.d.ts +7 -6
- package/dist-lib/lib/core/approval-rules.js +12 -6
- package/dist-lib/lib/core/archived-mesh.d.ts +7 -5
- package/dist-lib/lib/core/archived-mesh.js +25 -22
- package/dist-lib/lib/core/async-iterable.js +10 -5
- package/dist-lib/lib/core/attachment-link.d.ts +2 -2
- package/dist-lib/lib/core/audio-attachment.js +1 -1
- package/dist-lib/lib/core/blind-peers.d.ts +2 -2
- package/dist-lib/lib/core/chat-activity.d.ts +2 -2
- package/dist-lib/lib/core/chat-activity.js +42 -21
- package/dist-lib/lib/core/chat-group.d.ts +8 -6
- package/dist-lib/lib/core/chat-group.js +17 -10
- package/dist-lib/lib/core/chat-order.d.ts +11 -9
- package/dist-lib/lib/core/chat-order.js +1 -3
- package/dist-lib/lib/core/chat-search.d.ts +4 -4
- package/dist-lib/lib/core/chat-search.js +38 -24
- package/dist-lib/lib/core/chat.d.ts +20 -14
- package/dist-lib/lib/core/chat.js +50 -31
- package/dist-lib/lib/core/chunk.d.ts +19 -23
- package/dist-lib/lib/core/chunk.js +27 -21
- package/dist-lib/lib/core/collections.d.ts +70 -0
- package/dist-lib/lib/core/collections.js +70 -0
- package/dist-lib/lib/core/contract/activity.d.ts +6 -2
- package/dist-lib/lib/core/contract/activity.js +15 -12
- package/dist-lib/lib/core/contract/agents.js +3 -2
- package/dist-lib/lib/core/contract/approval-rules.js +1 -1
- package/dist-lib/lib/core/contract/attestations.js +1 -1
- package/dist-lib/lib/core/contract/chat-group.js +1 -1
- package/dist-lib/lib/core/contract/chat.d.ts +2 -0
- package/dist-lib/lib/core/contract/chat.js +67 -1
- package/dist-lib/lib/core/contract/chunks.js +15 -9
- package/dist-lib/lib/core/contract/compute.d.ts +1 -1
- package/dist-lib/lib/core/contract/compute.js +4 -2
- package/dist-lib/lib/core/contract/devices.js +6 -5
- package/dist-lib/lib/core/contract/grant-order.js +5 -6
- package/dist-lib/lib/core/contract/index.js +4 -2
- package/dist-lib/lib/core/contract/indexing.js +3 -3
- package/dist-lib/lib/core/contract/invites.d.ts +8 -3
- package/dist-lib/lib/core/contract/invites.js +52 -26
- package/dist-lib/lib/core/contract/joined-meshes.js +3 -3
- package/dist-lib/lib/core/contract/knowledge.js +5 -5
- package/dist-lib/lib/core/contract/members.d.ts +13 -2
- package/dist-lib/lib/core/contract/members.js +46 -25
- package/dist-lib/lib/core/contract/mesh-kind.js +9 -8
- package/dist-lib/lib/core/contract/seal.js +1 -1
- package/dist-lib/lib/core/contract/seed-defaults.js +1 -3
- package/dist-lib/lib/core/contract/tool-activity.js +10 -5
- package/dist-lib/lib/core/contract/tool-configs.js +1 -2
- package/dist-lib/lib/core/contract/writers.d.ts +6 -1
- package/dist-lib/lib/core/contract/writers.js +55 -34
- package/dist-lib/lib/core/departures.js +16 -8
- package/dist-lib/lib/core/device-invites.js +3 -2
- package/dist-lib/lib/core/devices.js +6 -3
- package/dist-lib/lib/core/document-attachment.d.ts +20 -11
- package/dist-lib/lib/core/document-attachment.js +10 -5
- package/dist-lib/lib/core/files/attachments.d.ts +2 -5
- package/dist-lib/lib/core/files/attachments.js +6 -3
- package/dist-lib/lib/core/files/index.d.ts +46 -52
- package/dist-lib/lib/core/files/index.js +149 -94
- package/dist-lib/lib/core/files/knowledge.d.ts +2 -5
- package/dist-lib/lib/core/files/local-index.d.ts +4 -1
- package/dist-lib/lib/core/files/local-index.js +9 -5
- package/dist-lib/lib/core/files/preview/image.d.ts +0 -1
- package/dist-lib/lib/core/files/preview/image.js +9 -6
- package/dist-lib/lib/core/files/preview/index.d.ts +8 -11
- package/dist-lib/lib/core/files/preview/index.js +20 -15
- package/dist-lib/lib/core/files/processing/claims.d.ts +6 -5
- package/dist-lib/lib/core/files/processing/claims.js +16 -8
- package/dist-lib/lib/core/files/processing/document.d.ts +21 -50
- package/dist-lib/lib/core/files/processing/index.d.ts +21 -13
- package/dist-lib/lib/core/files/processing/index.js +74 -37
- package/dist-lib/lib/core/files/processing/ownership.d.ts +8 -1
- package/dist-lib/lib/core/files/processing/ownership.js +1 -1
- package/dist-lib/lib/core/files/processing/protocol.d.ts +19 -19
- package/dist-lib/lib/core/files/processing/protocol.js +5 -5
- package/dist-lib/lib/core/files/processing/settle.d.ts +10 -7
- package/dist-lib/lib/core/files/processing/settle.js +22 -11
- package/dist-lib/lib/core/files/processing/source.js +14 -8
- package/dist-lib/lib/core/files/processing/state.d.ts +14 -11
- package/dist-lib/lib/core/files/processing/state.js +24 -12
- package/dist-lib/lib/core/files/rag/build.d.ts +2 -2
- package/dist-lib/lib/core/files/rag/build.js +2 -1
- package/dist-lib/lib/core/files/rag/document-types.d.ts +4 -6
- package/dist-lib/lib/core/files/rag/index.d.ts +12 -13
- package/dist-lib/lib/core/files/rag/index.js +9 -2
- package/dist-lib/lib/core/files/rag/load.d.ts +10 -9
- package/dist-lib/lib/core/files/rag/load.js +26 -13
- package/dist-lib/lib/core/files/rag/policy.js +2 -1
- package/dist-lib/lib/core/files/rag/produce.js +33 -19
- package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +5 -3
- package/dist-lib/lib/core/files/rag/retrieval/full-text.js +30 -10
- package/dist-lib/lib/core/files/records/chunks.js +4 -2
- package/dist-lib/lib/core/files/records/index.d.ts +1 -0
- package/dist-lib/lib/core/files/records/index.js +1 -0
- package/dist-lib/lib/core/files/records/knowledge.d.ts +10 -11
- package/dist-lib/lib/core/files/records/knowledge.js +9 -8
- package/dist-lib/lib/core/files/records/linkage.js +6 -3
- package/dist-lib/lib/core/files/records/output.d.ts +5 -4
- package/dist-lib/lib/core/files/records/output.js +8 -4
- package/dist-lib/lib/core/files/records/purge.d.ts +4 -0
- package/dist-lib/lib/core/files/records/purge.js +108 -0
- package/dist-lib/lib/core/files/records/reclaim.js +43 -25
- package/dist-lib/lib/core/files/records/routing.d.ts +4 -6
- package/dist-lib/lib/core/files/records/routing.js +15 -8
- package/dist-lib/lib/core/files/records/source.d.ts +5 -4
- package/dist-lib/lib/core/files/records/source.js +20 -14
- package/dist-lib/lib/core/files/records/sweep.js +1 -3
- package/dist-lib/lib/core/files/store/blob.d.ts +11 -10
- package/dist-lib/lib/core/files/store/blob.js +54 -19
- package/dist-lib/lib/core/files/store/index.d.ts +2 -6
- package/dist-lib/lib/core/files/store/index.js +10 -5
- package/dist-lib/lib/core/files/store/inline.d.ts +2 -2
- package/dist-lib/lib/core/files/store/inline.js +6 -3
- package/dist-lib/lib/core/files/store/reference.d.ts +6 -9
- package/dist-lib/lib/core/files/store/reference.js +25 -17
- package/dist-lib/lib/core/files/store/server.d.ts +7 -6
- package/dist-lib/lib/core/files/store/server.js +22 -11
- package/dist-lib/lib/core/files/store/sweep.js +13 -7
- package/dist-lib/lib/core/files/upload-input.d.ts +4 -12
- package/dist-lib/lib/core/files/upload-input.js +10 -5
- package/dist-lib/lib/core/files/upload.d.ts +27 -24
- package/dist-lib/lib/core/files/upload.js +74 -51
- package/dist-lib/lib/core/fs-drives.js +4 -2
- package/dist-lib/lib/core/grant-expiry.js +12 -7
- package/dist-lib/lib/core/handoff-channel.js +27 -13
- package/dist-lib/lib/core/harness-loan.js +35 -17
- package/dist-lib/lib/core/helpers/index.js +6 -2
- package/dist-lib/lib/core/hosted-agents.js +19 -12
- package/dist-lib/lib/core/id.js +1 -1
- package/dist-lib/lib/core/identity.js +6 -3
- package/dist-lib/lib/core/image/cache-sweep.d.ts +12 -10
- package/dist-lib/lib/core/image/cache-sweep.js +11 -6
- package/dist-lib/lib/core/image/normalize.d.ts +6 -0
- package/dist-lib/lib/core/image/normalize.js +12 -2
- package/dist-lib/lib/core/image/sniff.js +42 -21
- package/dist-lib/lib/core/inference-calls-receiver.js +7 -4
- package/dist-lib/lib/core/invite-grants.js +4 -2
- package/dist-lib/lib/core/invite.js +2 -1
- package/dist-lib/lib/core/local/files.d.ts +2 -0
- package/dist-lib/lib/core/local/files.js +51 -19
- package/dist-lib/lib/core/local/index.d.ts +69 -46
- package/dist-lib/lib/core/local/index.js +146 -129
- package/dist-lib/lib/core/membership.d.ts +12 -2
- package/dist-lib/lib/core/membership.js +55 -33
- package/dist-lib/lib/core/mesh-seed-handoff.js +7 -1
- package/dist-lib/lib/core/mesh.d.ts +37 -27
- package/dist-lib/lib/core/mesh.js +131 -73
- package/dist-lib/lib/core/migrations/agent-catalog.js +3 -2
- package/dist-lib/lib/core/migrations/agent-indexes.js +3 -3
- package/dist-lib/lib/core/migrations/file-index.js +7 -4
- package/dist-lib/lib/core/migrations/index.d.ts +4 -3
- package/dist-lib/lib/core/migrations/membership.js +9 -4
- package/dist-lib/lib/core/migrations/seal.js +3 -2
- package/dist-lib/lib/core/min-version.js +4 -2
- package/dist-lib/lib/core/next-frame.d.ts +5 -4
- package/dist-lib/lib/core/next-frame.js +6 -3
- package/dist-lib/lib/core/notification.d.ts +5 -1
- package/dist-lib/lib/core/notification.js +89 -77
- package/dist-lib/lib/core/ota-config.js +2 -1
- package/dist-lib/lib/core/pdf/rasterize.d.ts +18 -0
- package/dist-lib/lib/core/pdf/rasterize.js +57 -0
- package/dist-lib/lib/core/prompt-history.js +4 -2
- package/dist-lib/lib/core/range.d.ts +4 -4
- package/dist-lib/lib/core/range.js +12 -6
- package/dist-lib/lib/core/rank.d.ts +5 -4
- package/dist-lib/lib/core/rank.js +2 -1
- package/dist-lib/lib/core/send.d.ts +4 -4
- package/dist-lib/lib/core/send.js +6 -3
- package/dist-lib/lib/core/skill-overlay-receiver.js +7 -4
- package/dist-lib/lib/core/skill-secrets.js +16 -9
- package/dist-lib/lib/core/sync/chunks.js +2 -1
- package/dist-lib/lib/core/sync/devices.js +3 -1
- package/dist-lib/lib/core/sync/files.d.ts +2 -4
- package/dist-lib/lib/core/sync/files.js +28 -20
- package/dist-lib/lib/core/sync/index.d.ts +2 -5
- package/dist-lib/lib/core/sync/index.js +33 -15
- package/dist-lib/lib/core/sync-status.js +10 -7
- package/dist-lib/lib/core/system-info.js +4 -2
- package/dist-lib/lib/core/tool-activity.d.ts +4 -3
- package/dist-lib/lib/core/tool-activity.js +1 -1
- package/dist-lib/lib/core/tool-configs.js +12 -7
- package/dist-lib/lib/core.d.ts +38 -26
- package/dist-lib/lib/core.js +1009 -541
- package/dist-lib/lib/credential-pusher.d.ts +20 -20
- package/dist-lib/lib/credential-pusher.js +65 -43
- package/dist-lib/lib/custom-model-pusher.d.ts +7 -3
- package/dist-lib/lib/custom-model-pusher.js +15 -7
- package/dist-lib/lib/custom-model-task.d.ts +1 -0
- package/dist-lib/lib/custom-model-task.js +12 -0
- package/dist-lib/lib/deadline.js +4 -2
- package/dist-lib/lib/diagnostics.d.ts +1 -5
- package/dist-lib/lib/frame-readable.d.ts +3 -0
- package/dist-lib/lib/frame-readable.js +3 -0
- package/dist-lib/lib/fs-drive-bridge.js +24 -14
- package/dist-lib/lib/fs-grants.js +13 -8
- package/dist-lib/lib/guards.d.ts +3 -0
- package/dist-lib/lib/guards.js +9 -0
- package/dist-lib/lib/inference-calls.d.ts +28 -23
- package/dist-lib/lib/inference-calls.js +25 -13
- package/dist-lib/lib/integrations/mcp-providers.d.ts +2 -2
- package/dist-lib/lib/integrations/setup-routes.d.ts +2 -2
- package/dist-lib/lib/knowledge-search.d.ts +6 -5
- package/dist-lib/lib/knowledge-search.js +26 -18
- package/dist-lib/lib/kv-cache-reclaim.js +4 -2
- package/dist-lib/lib/kv-cache.js +8 -4
- package/dist-lib/lib/legacy-core-domain.d.ts +42 -42
- package/dist-lib/lib/log-buffer.js +2 -1
- package/dist-lib/lib/log.js +25 -15
- package/dist-lib/lib/mesh-scoped-engine.js +12 -4
- package/dist-lib/lib/model-catalog-revision.js +6 -3
- package/dist-lib/lib/model-clamp.js +6 -3
- package/dist-lib/lib/model-context.d.ts +8 -3
- package/dist-lib/lib/model-context.js +13 -5
- package/dist-lib/lib/model-download-status.js +4 -2
- package/dist-lib/lib/model-download.d.ts +12 -4
- package/dist-lib/lib/model-download.js +200 -103
- package/dist-lib/lib/model-idle-pusher.d.ts +2 -2
- package/dist-lib/lib/model-idle-pusher.js +3 -4
- package/dist-lib/lib/oauth/browser-connect.d.ts +10 -8
- package/dist-lib/lib/oauth/browser-connect.js +33 -20
- package/dist-lib/lib/oauth/client.d.ts +2 -2
- package/dist-lib/lib/oauth/flow-core.js +23 -9
- package/dist-lib/lib/oauth/flow.d.ts +1 -3
- package/dist-lib/lib/oauth/flow.js +9 -5
- package/dist-lib/lib/oauth/keys.d.ts +4 -4
- package/dist-lib/lib/oauth/keys.js +18 -12
- package/dist-lib/lib/oauth/loopback.d.ts +2 -2
- package/dist-lib/lib/oauth/loopback.js +147 -107
- package/dist-lib/lib/oauth/loopback.stub.d.ts +2 -2
- package/dist-lib/lib/oauth/loopback.stub.js +1 -1
- package/dist-lib/lib/oauth/pkce.d.ts +4 -2
- package/dist-lib/lib/oauth/pkce.js +10 -0
- package/dist-lib/lib/oauth/providers.js +5 -5
- package/dist-lib/lib/oauth/redirect-types.d.ts +12 -7
- package/dist-lib/lib/oauth/register.js +3 -2
- package/dist-lib/lib/oauth/registry.d.ts +2 -2
- package/dist-lib/lib/oauth/registry.js +2 -1
- package/dist-lib/lib/oauth/resolve.d.ts +30 -10
- package/dist-lib/lib/oauth/resolve.js +12 -6
- package/dist-lib/lib/oauth/token-exchange-core.d.ts +12 -4
- package/dist-lib/lib/oauth/token-exchange-core.js +1 -1
- package/dist-lib/lib/oauth/types.d.ts +12 -12
- package/dist-lib/lib/oauth.d.ts +4 -5
- package/dist-lib/lib/oauth.js +2 -3
- package/dist-lib/lib/reconnecting-watch.d.ts +1 -1
- package/dist-lib/lib/reconnecting-watch.js +6 -6
- package/dist-lib/lib/retry-budget.js +6 -3
- package/dist-lib/lib/run-notices.d.ts +1 -0
- package/dist-lib/lib/run-notices.js +5 -2
- package/dist-lib/lib/run-transcript.d.ts +2 -4
- package/dist-lib/lib/run-transcript.js +4 -2
- package/dist-lib/lib/skill-readiness.js +4 -2
- package/dist-lib/lib/skill-types.d.ts +16 -16
- package/dist-lib/lib/suspend-lifecycle.d.ts +4 -4
- package/dist-lib/lib/suspend-lifecycle.js +10 -5
- package/dist-lib/lib/task.js +22 -12
- package/dist-lib/lib/unref.d.ts +3 -2
- package/dist-lib/lib/unref.js +2 -1
- package/dist-lib/lib/wav-duration.js +10 -5
- package/dist-lib/lib/write-queue.d.ts +5 -4
- package/dist-lib/lib/write-queue.js +2 -1
- package/dist-lib/schema/build.js +10 -6
- package/dist-lib/schema/config.d.ts +2 -3
- package/dist-lib/schema/content.d.ts +17 -2
- package/dist-lib/schema/content.js +2 -1
- package/dist-lib/schema/engine.d.ts +3 -2
- package/dist-lib/schema/engine.js +95 -6
- package/dist-lib/schema/field-order.js +22 -11
- package/dist-lib/schema/indexes.d.ts +22 -0
- package/dist-lib/schema/{helpers.js → indexes.js} +14 -57
- package/dist-lib/schema/local.d.ts +3 -2
- package/dist-lib/schema/local.js +7 -2
- package/dist-lib/schema/media-kind.d.ts +8 -0
- package/dist-lib/schema/media-kind.js +52 -0
- package/dist-lib/schema/qvac.d.ts +12 -9
- package/dist-lib/schema/qvac.js +18 -4
- package/dist-lib/spec/bind.js +12 -0
- package/dist-lib/spec/calls.js +4 -0
- package/dist-lib/spec/capabilities.d.ts +12 -4
- package/dist-lib/spec/hrpc/hrpc.json +54 -2
- package/dist-lib/spec/hrpc/index.js +53 -5
- package/dist-lib/spec/hrpc/messages.js +783 -494
- package/dist-lib/spec/hyperschema/index.js +783 -494
- package/dist-lib/spec/hyperschema/schema.json +247 -1
- package/dist-lib/spec/hyperschema/types.d.ts +69 -1
- package/dist-lib/spec/local/hyperdb/index.js +1 -1
- package/dist-lib/spec/local/hyperdb/messages.js +82 -14
- package/dist-lib/spec/local/hyperschema/index.js +42 -8
- package/dist-lib/spec/local/hyperschema/schema.json +27 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +6 -1
- package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
- package/dist-lib/spec/mesh/hyperdb/index.js +72 -3
- package/dist-lib/spec/mesh/hyperdb/messages.js +400 -221
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
- package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +117 -2
- package/dist-lib/spec/mesh/hyperschema/index.js +117 -2
- package/dist-lib/spec/mesh/hyperschema/schema.json +65 -1
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +19 -1
- package/package.json +8 -4
- package/tui/app.mjs +2424 -941
- package/tui/boot.mjs +18 -49
- package/tui/index.mjs +10 -5
- package/tui/lib/boot-timing.mjs +8 -8
- package/tui/lib/busy-words.mjs +21 -22
- package/tui/lib/export.mjs +100 -32
- package/tui/lib/knowledge-command.mjs +64 -19
- package/tui/lib/knowledge-import.mjs +52 -18
- package/tui/lib/render.mjs +65 -33
- package/tui/lib/save-file.mjs +30 -13
- package/tui/lib/select.mjs +23 -5
- package/tui/lib/selection.mjs +6 -4
- package/tui/lib/setup.mjs +11 -19
- package/tui/lib/sidebar.mjs +23 -13
- package/tui/lib/splash.mjs +6 -7
- package/tui/lib/upload-frames.mjs +1 -2
- package/tui/lib/usage.mjs +13 -9
- package/tui/lib/wrap.mjs +35 -6
- package/tui/main.mjs +54 -78
- package/worker/assistant-sidecar.mjs +5 -10
- package/worker/stow-entry.mjs +7 -4
- package/dist-lib/lib/custom-model-source.d.ts +0 -15
- package/dist-lib/lib/custom-model-source.js +0 -46
- package/dist-lib/lib/oauth/common.d.ts +0 -38
- package/dist-lib/lib/oauth/common.js +0 -34
- package/dist-lib/schema/helpers.d.ts +0 -33
|
@@ -7,6 +7,7 @@ import os from 'os';
|
|
|
7
7
|
import path from 'path';
|
|
8
8
|
import { mediaModelNameFor } from '@qvac/harness/lib/models/model-catalog.ts';
|
|
9
9
|
import { connectHarness } from '@qvac/harness';
|
|
10
|
+
import * as z from 'zod';
|
|
10
11
|
import { loanFailureNotice } from './core/harness-loan.js';
|
|
11
12
|
import { SKILL_STATE } from '@qvac/harness/lib/tools/skills/skill-state.ts';
|
|
12
13
|
import { toolsetEnabled } from '@qvac/harness/lib/tools/toolsets.ts';
|
|
@@ -25,6 +26,7 @@ import { attachTurnDocumentIds, formatRagContext, chooseRagNotice, RAG_KNOWLEDGE
|
|
|
25
26
|
import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason } from './core/document-attachment.js';
|
|
26
27
|
import { imageCacheKey, imageCacheName, sweepImageCache } from './core/image/cache-sweep.js';
|
|
27
28
|
import { IMAGE_MIMES, normalizeImageForModel } from './core/image/normalize.js';
|
|
29
|
+
import { MAX_PDF_PAGE_IMAGES, pdfRasterizer } from './core/pdf/rasterize.js';
|
|
28
30
|
import { summarizeRuns } from './diagnostics.js';
|
|
29
31
|
import { resolveFsGrants } from './fs-drive-bridge.js';
|
|
30
32
|
import { InferenceCallStore } from './inference-calls.js';
|
|
@@ -41,11 +43,12 @@ import { RunTranscript } from './run-transcript.js';
|
|
|
41
43
|
import { searchKnowledge } from './knowledge-search.js';
|
|
42
44
|
import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
|
|
43
45
|
import { encodeAgentSkillReadiness } from './skill-readiness.js';
|
|
44
|
-
import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, imageDownloadProgress, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
|
|
46
|
+
import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
|
|
45
47
|
import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, nameSenders } from './assistant/message.js';
|
|
46
48
|
import { drainBody, raceInactivity } from './assistant/fetch-body.js';
|
|
47
49
|
import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, couldNameSenders, historyFromChunks, inputAvailableToRun, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds } from './assistant/history.js';
|
|
48
|
-
|
|
50
|
+
import { errorMessage, isRecord } from './guards.js';
|
|
51
|
+
export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
|
|
49
52
|
export {} from './assistant/skill-encode.js';
|
|
50
53
|
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders } from './assistant/message.js';
|
|
51
54
|
export { couldNameSenders, historyFromChunks, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds } from './assistant/history.js';
|
|
@@ -65,18 +68,50 @@ function settledWithin(write, ms) {
|
|
|
65
68
|
function isDeniedResult(result) {
|
|
66
69
|
if (!result || typeof result !== 'object')
|
|
67
70
|
return false;
|
|
68
|
-
return result.denied === true;
|
|
71
|
+
return 'denied' in result && result.denied === true;
|
|
69
72
|
}
|
|
70
73
|
const TITLE_RUN_ID = 'chat-title';
|
|
74
|
+
const RunOccupancySchema = z.object({
|
|
75
|
+
cacheTokens: z.number().optional().catch(undefined),
|
|
76
|
+
ctxSize: z.number().optional().catch(undefined)
|
|
77
|
+
});
|
|
71
78
|
export const CAPTURE_INFERENCE_CALLS_KEY = 'inspector.captureInferenceCalls';
|
|
72
79
|
export const CAPTURE_ON = 'on';
|
|
73
80
|
export const CAPTURE_OFF = 'off';
|
|
81
|
+
function attachPages(turn, files) {
|
|
82
|
+
if (!files.length)
|
|
83
|
+
return;
|
|
84
|
+
turn.attachments = [...(turn.attachments ?? []), ...files.map((file) => ({ path: file }))];
|
|
85
|
+
}
|
|
86
|
+
function noBytes() {
|
|
87
|
+
return Promise.resolve(null);
|
|
88
|
+
}
|
|
89
|
+
function pdfPageCacheName(pdf, page) {
|
|
90
|
+
return imageCacheName({ blobId: pdf.blobId, fileName: `${pdf.fileName}.p${page}.jpg` });
|
|
91
|
+
}
|
|
92
|
+
async function cacheOrTouch(file, produce) {
|
|
93
|
+
const cached = await fs.promises.stat(file).catch(noop);
|
|
94
|
+
if (cached?.size) {
|
|
95
|
+
const now = new Date();
|
|
96
|
+
await fs.promises.utimes(file, now, now).catch(noop);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
const bytes = await produce();
|
|
100
|
+
if (!bytes) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const staging = `${file}.partial-${b4a.toHex(crypto.randomBytes(4))}`;
|
|
104
|
+
await fs.promises.writeFile(staging, bytes, { mode: 0o600 });
|
|
105
|
+
await fs.promises.rename(staging, file);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
74
108
|
const BLOB_READ_TIMEOUT_MS = 15_000;
|
|
75
109
|
const SUSPENDED = RUN_ABORT_REASONS.suspended;
|
|
76
110
|
const OWNERSHIP_LOST = RUN_ABORT_REASONS.ownershipLost;
|
|
77
111
|
const RUN_SETTLED = RUN_ABORT_REASONS.runSettled;
|
|
78
112
|
const RUNNER_DIED = RUN_ABORT_REASONS.runnerDied;
|
|
79
113
|
const RUNNER_CLOSED = RUN_ABORT_REASONS.runnerClosed;
|
|
114
|
+
const MODEL_UNLOADED = RUN_ABORT_REASONS.modelUnloaded;
|
|
80
115
|
function isSetUpHere(status) {
|
|
81
116
|
return status === SKILL_STATE.ready || status === SKILL_STATE.incompatibleModel;
|
|
82
117
|
}
|
|
@@ -88,6 +123,11 @@ function withConfigDefaults(agent, config) {
|
|
|
88
123
|
return agent;
|
|
89
124
|
return { ...agent, ctxSize: config.ctxSize };
|
|
90
125
|
}
|
|
126
|
+
function thinkingDefault(config) {
|
|
127
|
+
if (!config)
|
|
128
|
+
return undefined;
|
|
129
|
+
return config.thinkingEnabled ? 'enabled' : 'disabled';
|
|
130
|
+
}
|
|
91
131
|
const RAG_SOURCE_BOUNDS = {
|
|
92
132
|
maxSources: MAX_RAG_SOURCES_PER_CHUNK,
|
|
93
133
|
excerptChars: RAG_SOURCE_EXCERPT_CHARS
|
|
@@ -108,7 +148,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
108
148
|
_preloadCurrentAgentModel;
|
|
109
149
|
_drives;
|
|
110
150
|
_blobReadTimeoutMs;
|
|
151
|
+
_pdfRasterizer;
|
|
111
152
|
_warmAbort = new AbortController();
|
|
153
|
+
_unloading = new Map();
|
|
112
154
|
_warmedModel = false;
|
|
113
155
|
_warmedModelName = null;
|
|
114
156
|
_modelStatusLogged = new Map();
|
|
@@ -141,6 +183,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
141
183
|
_inferenceCalls;
|
|
142
184
|
_lifecycle;
|
|
143
185
|
_audioTranscripts;
|
|
186
|
+
_pdfPages;
|
|
144
187
|
_chats;
|
|
145
188
|
_runImageCacheNames;
|
|
146
189
|
_reconciledCompactions;
|
|
@@ -148,7 +191,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
148
191
|
_modelCatalog = new ModelCatalogRevisionHub();
|
|
149
192
|
_onHarnessStatus;
|
|
150
193
|
_onModelCatalogEvent;
|
|
151
|
-
constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, borrowHarness, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS } = {}) {
|
|
194
|
+
constructor(engine, harness, { logger = null, imageCacheDir, inferenceCallsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, borrowHarness, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS, pdfRasterizer: pdfRasterizerOption = pdfRasterizer } = {}) {
|
|
152
195
|
super();
|
|
153
196
|
this._engine = engine;
|
|
154
197
|
this._harness = harness;
|
|
@@ -163,6 +206,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
163
206
|
this._mobileClass = isMobilePlatform(platform);
|
|
164
207
|
this._preloadCurrentAgentModel = preloadCurrentAgentModel;
|
|
165
208
|
this._blobReadTimeoutMs = blobReadTimeoutMs;
|
|
209
|
+
this._pdfRasterizer = pdfRasterizerOption;
|
|
166
210
|
this._deviceId = null;
|
|
167
211
|
this._info = { toolNames: [], skills: [], supportsImages: false };
|
|
168
212
|
this._readySkills = null;
|
|
@@ -185,6 +229,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
185
229
|
this._inferenceCalls = new InferenceCallStore(inferenceCallsDir);
|
|
186
230
|
this._lifecycle = new AbortController();
|
|
187
231
|
this._audioTranscripts = new Map();
|
|
232
|
+
this._pdfPages = new Map();
|
|
188
233
|
this._chats = new Map();
|
|
189
234
|
this._runImageCacheNames = new Map();
|
|
190
235
|
this._reconciledCompactions = new Map();
|
|
@@ -193,8 +238,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
193
238
|
this._log?.warn('assistant', 'harness status', status);
|
|
194
239
|
if (harnessDied(status)) {
|
|
195
240
|
this._harnessDeath = harnessDeathReason(status);
|
|
196
|
-
for (const run of this._runs.values())
|
|
241
|
+
for (const run of this._runs.values()) {
|
|
197
242
|
run.controller.abort(RUNNER_DIED);
|
|
243
|
+
}
|
|
198
244
|
}
|
|
199
245
|
else {
|
|
200
246
|
this._harnessDeath = null;
|
|
@@ -203,13 +249,20 @@ export class QvacAssistant extends ReadyResource {
|
|
|
203
249
|
};
|
|
204
250
|
this._onModelCatalogEvent = (state) => {
|
|
205
251
|
this._logModelStatus(state);
|
|
206
|
-
if (modelCatalogChanged(state))
|
|
252
|
+
if (modelCatalogChanged(state)) {
|
|
207
253
|
this._modelCatalog.notify();
|
|
254
|
+
}
|
|
208
255
|
const progress = imageDownloadProgress(state);
|
|
209
|
-
if (progress)
|
|
256
|
+
if (progress) {
|
|
210
257
|
this._downloads?.observeImageProgress(progress);
|
|
258
|
+
}
|
|
211
259
|
};
|
|
212
260
|
}
|
|
261
|
+
_requireDeviceId() {
|
|
262
|
+
if (!this._deviceId)
|
|
263
|
+
throw new Error('assistant device id read before open');
|
|
264
|
+
return this._deviceId;
|
|
265
|
+
}
|
|
213
266
|
async _open() {
|
|
214
267
|
this._log?.info('assistant', 'host platform resolved', {
|
|
215
268
|
platform: this._platform,
|
|
@@ -248,21 +301,25 @@ export class QvacAssistant extends ReadyResource {
|
|
|
248
301
|
void this._warmCurrentAgentModel().catch((err) => this._log?.warn('assistant', 'current agent model warm failed', err));
|
|
249
302
|
}
|
|
250
303
|
async _warmCurrentAgentModel() {
|
|
251
|
-
if (this._warmedModel)
|
|
304
|
+
if (this._warmedModel || !this._deviceId)
|
|
252
305
|
return;
|
|
253
306
|
const deviceId = this._deviceId;
|
|
254
307
|
const config = await this._engine.getConfig({}).catch(noop);
|
|
255
308
|
const agent = await this._currentAgent(config?.activeAgentId ?? null, deviceId);
|
|
256
|
-
if (!agent?.modelName ||
|
|
309
|
+
if (!agent?.modelName ||
|
|
310
|
+
!agent.providerDeviceId ||
|
|
311
|
+
!b4a.equals(agent.providerDeviceId, deviceId) ||
|
|
312
|
+
this.suspended) {
|
|
257
313
|
return;
|
|
314
|
+
}
|
|
258
315
|
const model = runnableModelName(agent.modelName, await this._ensureCapability(), this._mobileClass);
|
|
259
|
-
|
|
260
|
-
if (!rows?.some((row) => row.name === model && row.isCached)) {
|
|
316
|
+
if (!(await this._isCached(model))) {
|
|
261
317
|
this._log?.info('models', 'model warm skipped', { model, reason: 'not cached' });
|
|
262
318
|
return;
|
|
263
319
|
}
|
|
264
|
-
if (this._warmedModel)
|
|
320
|
+
if (this._warmedModel) {
|
|
265
321
|
return;
|
|
322
|
+
}
|
|
266
323
|
this._warmedModel = true;
|
|
267
324
|
this._warmedModelName = model;
|
|
268
325
|
this._log?.info('models', 'model warm started', { model, agentModel: agent.modelName });
|
|
@@ -276,17 +333,20 @@ export class QvacAssistant extends ReadyResource {
|
|
|
276
333
|
}
|
|
277
334
|
async _currentAgent(activeAgentId, deviceId) {
|
|
278
335
|
const meshId = this._accountMeshId;
|
|
279
|
-
if (!meshId)
|
|
336
|
+
if (!meshId) {
|
|
280
337
|
return null;
|
|
338
|
+
}
|
|
281
339
|
if (activeAgentId) {
|
|
282
340
|
const active = await this._engine.getAgent({ agentId: activeAgentId, meshId }).catch(noop);
|
|
283
|
-
if (active)
|
|
341
|
+
if (active) {
|
|
284
342
|
return active;
|
|
343
|
+
}
|
|
285
344
|
}
|
|
286
345
|
try {
|
|
287
346
|
const own = await this._engine.getDefaultAgent({ meshId, providerDeviceId: deviceId });
|
|
288
|
-
if (own.agent)
|
|
347
|
+
if (own.agent) {
|
|
289
348
|
return own.agent;
|
|
349
|
+
}
|
|
290
350
|
const any = await this._engine.getDefaultAgent({ meshId });
|
|
291
351
|
return any.agent ?? null;
|
|
292
352
|
}
|
|
@@ -330,8 +390,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
330
390
|
try {
|
|
331
391
|
const name = model || mediaModelNameFor(category, await this._persistedProfile());
|
|
332
392
|
const config = await this._engine.getConfig({});
|
|
333
|
-
if (!(config.pausedModels ?? []).includes(name))
|
|
393
|
+
if (!(config.pausedModels ?? []).includes(name)) {
|
|
334
394
|
return true;
|
|
395
|
+
}
|
|
335
396
|
await this._engine.setModelPaused({ name, paused: false });
|
|
336
397
|
return true;
|
|
337
398
|
}
|
|
@@ -344,8 +405,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
344
405
|
await this._accountSeen.promise;
|
|
345
406
|
const deviceId = this._deviceId;
|
|
346
407
|
const meshId = this._accountMeshId;
|
|
347
|
-
if (!deviceId || !meshId)
|
|
408
|
+
if (!deviceId || !meshId) {
|
|
348
409
|
return DEFAULT_PROFILE;
|
|
410
|
+
}
|
|
349
411
|
try {
|
|
350
412
|
const self = await this._engine.getDevice({ meshId, deviceId });
|
|
351
413
|
return self.capabilityProfile ?? DEFAULT_PROFILE;
|
|
@@ -355,14 +417,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
355
417
|
return DEFAULT_PROFILE;
|
|
356
418
|
}
|
|
357
419
|
}
|
|
358
|
-
async _sweepImageCache() {
|
|
420
|
+
async _sweepImageCache(gone = new Set()) {
|
|
359
421
|
try {
|
|
360
|
-
const keep = await this._referencedImageNames();
|
|
422
|
+
const keep = await this._referencedImageNames(gone);
|
|
361
423
|
await sweepImageCache(this._imageCacheDir, {
|
|
362
424
|
maxBytes: this._imageCacheMaxBytes,
|
|
363
425
|
maxAgeMs: this._imageCacheMaxAgeMs,
|
|
364
426
|
inUse: (name) => this._imageCacheInUse(name),
|
|
365
|
-
|
|
427
|
+
keep: keep ?? undefined
|
|
366
428
|
});
|
|
367
429
|
}
|
|
368
430
|
catch (err) {
|
|
@@ -372,25 +434,29 @@ export class QvacAssistant extends ReadyResource {
|
|
|
372
434
|
async _sweepInferenceCalls() {
|
|
373
435
|
try {
|
|
374
436
|
const { removed } = await this._inferenceCalls.sweep();
|
|
375
|
-
if (removed > 0)
|
|
437
|
+
if (removed > 0) {
|
|
376
438
|
this._log?.info('assistant', 'inspector records swept', { removed });
|
|
439
|
+
}
|
|
377
440
|
}
|
|
378
441
|
catch (err) {
|
|
379
442
|
this._log?.warn('assistant', 'inspector record sweep failed', err);
|
|
380
443
|
}
|
|
381
444
|
}
|
|
382
445
|
_imageCacheInUse(name) {
|
|
383
|
-
for (const names of this._runImageCacheNames.values())
|
|
446
|
+
for (const names of this._runImageCacheNames.values()) {
|
|
384
447
|
if (names.has(name))
|
|
385
448
|
return true;
|
|
449
|
+
}
|
|
386
450
|
return false;
|
|
387
451
|
}
|
|
388
|
-
async _referencedImageNames() {
|
|
452
|
+
async _referencedImageNames(gone) {
|
|
389
453
|
try {
|
|
390
454
|
const { files } = await this._engine.listLocalFiles({});
|
|
391
|
-
if (!files?.length)
|
|
455
|
+
if (!files?.length) {
|
|
392
456
|
return null;
|
|
393
|
-
|
|
457
|
+
}
|
|
458
|
+
const live = files.filter((file) => !file.chatId || !gone.has(file.chatId));
|
|
459
|
+
return new Set(live.map((file) => imageCacheKey(file.blobId)));
|
|
394
460
|
}
|
|
395
461
|
catch (err) {
|
|
396
462
|
this._log?.warn('assistant', 'reading the local file index failed', err);
|
|
@@ -479,20 +545,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
479
545
|
toolCatalog
|
|
480
546
|
]);
|
|
481
547
|
const meshId = this._accountMeshId;
|
|
482
|
-
if (!meshId)
|
|
548
|
+
if (!meshId) {
|
|
483
549
|
return;
|
|
550
|
+
}
|
|
484
551
|
const readySkillNames = this._readySkills;
|
|
485
552
|
const report = {
|
|
486
553
|
installedToolNames: this._info.toolNames,
|
|
487
554
|
installedSkillNames: this._info.skills,
|
|
488
555
|
installedModelNames,
|
|
489
556
|
cachedModelNames,
|
|
490
|
-
|
|
491
|
-
|
|
557
|
+
readySkillNames: readySkillNames ?? undefined,
|
|
558
|
+
toolCatalog: toolCatalog ?? undefined
|
|
492
559
|
};
|
|
493
560
|
await this._reportInto(meshId, key, report);
|
|
494
|
-
if (!readySkillNames)
|
|
561
|
+
if (!readySkillNames) {
|
|
495
562
|
return;
|
|
563
|
+
}
|
|
496
564
|
for (const [shared, hosted] of this._hostedIn) {
|
|
497
565
|
if (shared === meshId)
|
|
498
566
|
continue;
|
|
@@ -507,30 +575,36 @@ export class QvacAssistant extends ReadyResource {
|
|
|
507
575
|
this._lastCapabilities.set(meshId, key);
|
|
508
576
|
}
|
|
509
577
|
catch (err) {
|
|
510
|
-
if (isMissingMesh(err))
|
|
578
|
+
if (isMissingMesh(err)) {
|
|
511
579
|
return;
|
|
580
|
+
}
|
|
512
581
|
this._log?.error('assistant', 'saveDeviceCapabilities failed', err);
|
|
513
582
|
}
|
|
514
583
|
}
|
|
515
584
|
_followHosted(meshId, agents) {
|
|
516
585
|
const deviceId = this._deviceId;
|
|
517
|
-
if (!deviceId || !this._sharedMeshes.has(meshId))
|
|
586
|
+
if (!deviceId || !this._sharedMeshes.has(meshId)) {
|
|
518
587
|
return;
|
|
588
|
+
}
|
|
519
589
|
const hosted = agents
|
|
520
|
-
.filter((agent) => agent.providerDeviceId
|
|
590
|
+
.filter((agent) => !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId))
|
|
521
591
|
.map((agent) => agent.id)
|
|
522
592
|
.sort();
|
|
523
|
-
if (sameNameList(this._hostedIn.get(meshId) ?? [], hosted))
|
|
593
|
+
if (sameNameList(this._hostedIn.get(meshId) ?? [], hosted)) {
|
|
524
594
|
return;
|
|
525
|
-
|
|
595
|
+
}
|
|
596
|
+
if (hosted.length > 0) {
|
|
526
597
|
this._hostedIn.set(meshId, hosted);
|
|
527
|
-
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
528
600
|
this._hostedIn.delete(meshId);
|
|
601
|
+
}
|
|
529
602
|
void this._reportHosted().catch(noop);
|
|
530
603
|
}
|
|
531
604
|
async _reportHosted() {
|
|
532
|
-
if (this._readySkills)
|
|
605
|
+
if (this._readySkills) {
|
|
533
606
|
await this._recordSkillReadiness();
|
|
607
|
+
}
|
|
534
608
|
await this._reportCapabilities();
|
|
535
609
|
}
|
|
536
610
|
async _ensureCapability() {
|
|
@@ -544,35 +618,39 @@ export class QvacAssistant extends ReadyResource {
|
|
|
544
618
|
this._log?.warn('assistant', '_reconcileDeviceProfile: harness.capability failed', err);
|
|
545
619
|
return null;
|
|
546
620
|
}
|
|
547
|
-
if (!probed?.recommendedModelName)
|
|
621
|
+
if (!probed?.recommendedModelName) {
|
|
548
622
|
return null;
|
|
623
|
+
}
|
|
549
624
|
this._capability = probed;
|
|
550
625
|
return probed;
|
|
551
626
|
}
|
|
552
627
|
async _reconcileDeviceProfile() {
|
|
553
628
|
const deviceId = this._deviceId;
|
|
554
629
|
const meshId = this._accountMeshId;
|
|
555
|
-
if (!deviceId || !meshId || this._reconciling)
|
|
630
|
+
if (!deviceId || !meshId || this._reconciling) {
|
|
556
631
|
return;
|
|
632
|
+
}
|
|
557
633
|
this._reconciling = true;
|
|
558
634
|
try {
|
|
559
635
|
const capability = await this._ensureCapability();
|
|
560
|
-
if (!capability)
|
|
636
|
+
if (!capability) {
|
|
561
637
|
return;
|
|
638
|
+
}
|
|
562
639
|
const self = await this._engine.getDevice({ meshId, deviceId }).catch(noop);
|
|
563
|
-
if (!self)
|
|
640
|
+
if (!self) {
|
|
564
641
|
return;
|
|
642
|
+
}
|
|
565
643
|
const profileUpdated = !(self.capabilityProfile === capability.capabilityProfile &&
|
|
566
644
|
self.recommendedModelName === capability.recommendedModelName &&
|
|
567
645
|
self.platform === this._platform);
|
|
568
646
|
const profile = {
|
|
569
|
-
publicKey: b4a.
|
|
647
|
+
publicKey: b4a.toHex(deviceId),
|
|
570
648
|
name: self.name,
|
|
571
649
|
platform: this._platform,
|
|
572
650
|
mobileClass: this._mobileClass,
|
|
573
651
|
capabilityProfile: capability.capabilityProfile,
|
|
574
652
|
recommendedModelName: capability.recommendedModelName,
|
|
575
|
-
|
|
653
|
+
memoryBudget: capability.memoryBudget
|
|
576
654
|
};
|
|
577
655
|
if (profileUpdated) {
|
|
578
656
|
try {
|
|
@@ -600,8 +678,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
600
678
|
this._harness.off?.('model', this._onModelCatalogEvent);
|
|
601
679
|
this._warmAbort.abort(RUNNER_CLOSED);
|
|
602
680
|
this._onlineBoot.destroy(new Error('assistant is closing'));
|
|
603
|
-
for (const run of this._runs.values())
|
|
681
|
+
for (const run of this._runs.values()) {
|
|
604
682
|
run.controller.abort(RUNNER_CLOSED);
|
|
683
|
+
}
|
|
605
684
|
await this._downloads?.close();
|
|
606
685
|
this._modelCatalog.close();
|
|
607
686
|
this._meshList?.destroy();
|
|
@@ -620,18 +699,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
620
699
|
}
|
|
621
700
|
async suspend() {
|
|
622
701
|
this._log?.info('assistant', 'suspend called');
|
|
623
|
-
if (this._state === QvacState.SUSPEND)
|
|
702
|
+
if (this._state === QvacState.SUSPEND) {
|
|
624
703
|
return;
|
|
625
|
-
|
|
704
|
+
}
|
|
705
|
+
if (this.closing) {
|
|
626
706
|
return;
|
|
707
|
+
}
|
|
627
708
|
this._state = QvacState.SUSPEND;
|
|
628
709
|
this._log?.info('assistant', 'suspending', { runs: this._runs.size });
|
|
629
710
|
this._stopMeshIntake();
|
|
630
711
|
await this._downloads?.suspend();
|
|
631
712
|
this._meshList?.destroy();
|
|
632
713
|
this._accountFrames?.destroy();
|
|
633
|
-
for (const run of this._runs.values())
|
|
714
|
+
for (const run of this._runs.values()) {
|
|
634
715
|
run.controller.abort(SUSPENDED);
|
|
716
|
+
}
|
|
635
717
|
this._warmedModel = false;
|
|
636
718
|
this._warmedModelName = null;
|
|
637
719
|
this._modelCatalog.notify();
|
|
@@ -646,10 +728,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
646
728
|
}
|
|
647
729
|
async resume() {
|
|
648
730
|
this._log?.info('assistant', 'resume called');
|
|
649
|
-
if (this._state === QvacState.RESUME)
|
|
731
|
+
if (this._state === QvacState.RESUME) {
|
|
650
732
|
return;
|
|
651
|
-
|
|
733
|
+
}
|
|
734
|
+
if (this.closing) {
|
|
652
735
|
return;
|
|
736
|
+
}
|
|
653
737
|
if (this._state === QvacState.PRESUSPEND) {
|
|
654
738
|
this._state = QvacState.RESUME;
|
|
655
739
|
return;
|
|
@@ -658,8 +742,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
658
742
|
this._log?.info('assistant', 'resuming');
|
|
659
743
|
await Promise.allSettled([...this._runs.values()].map((run) => run.done));
|
|
660
744
|
this._downloads?.resume();
|
|
661
|
-
if (!this.opened)
|
|
745
|
+
if (!this.opened) {
|
|
662
746
|
return;
|
|
747
|
+
}
|
|
663
748
|
this._meshListWatch.start();
|
|
664
749
|
this._accountWatch.start();
|
|
665
750
|
}
|
|
@@ -667,14 +752,92 @@ export class QvacAssistant extends ReadyResource {
|
|
|
667
752
|
return (await this._harness.list?.(options)) ?? [];
|
|
668
753
|
}
|
|
669
754
|
async deleteModel(name) {
|
|
670
|
-
|
|
755
|
+
const removeFiles = async () => (await this._harness.deleteModel?.(name)) ?? { name, removed: 0, freedBytes: 0 };
|
|
756
|
+
let summary;
|
|
757
|
+
if (this._downloads) {
|
|
758
|
+
summary = await this._downloads.release(name, removeFiles);
|
|
759
|
+
}
|
|
760
|
+
else {
|
|
761
|
+
summary = await removeFiles();
|
|
762
|
+
await this._engine.setModelReleased({ name, released: true });
|
|
763
|
+
}
|
|
764
|
+
this._modelCatalog.notify();
|
|
765
|
+
this._reportCapabilities().catch(noop);
|
|
766
|
+
return summary;
|
|
671
767
|
}
|
|
672
768
|
modelCatalogWatch() {
|
|
673
769
|
return this._modelCatalog.watch();
|
|
674
770
|
}
|
|
771
|
+
async loadModel(name) {
|
|
772
|
+
if (!this._harness.loadModel) {
|
|
773
|
+
throw new Error('the model runtime cannot preload a model');
|
|
774
|
+
}
|
|
775
|
+
if (!(await this._isCached(name))) {
|
|
776
|
+
throw new Error(`${name} is not downloaded on this device`);
|
|
777
|
+
}
|
|
778
|
+
const config = await this._engine.getConfig({}).catch(noop);
|
|
779
|
+
const agent = await this._agentRunning(name, config?.activeAgentId ?? null);
|
|
780
|
+
const knobs = agent
|
|
781
|
+
? withConfigDefaults(agent, config)
|
|
782
|
+
: { ctxSize: config?.ctxSize || undefined };
|
|
783
|
+
const { loaded, reason } = await this._harness.loadModel({ model: name, agent: knobs });
|
|
784
|
+
this._log?.info('models', 'model load requested', { model: name, loaded, reason });
|
|
785
|
+
return { loaded, reason };
|
|
786
|
+
}
|
|
787
|
+
async _isCached(model) {
|
|
788
|
+
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
789
|
+
return rows?.some((row) => row.name === model && row.isCached) === true;
|
|
790
|
+
}
|
|
791
|
+
async _agentRunning(model, activeAgentId) {
|
|
792
|
+
const deviceId = this._requireDeviceId();
|
|
793
|
+
const meshId = this._accountMeshId;
|
|
794
|
+
if (!meshId) {
|
|
795
|
+
return null;
|
|
796
|
+
}
|
|
797
|
+
const capability = await this._ensureCapability();
|
|
798
|
+
const runs = (agent) => runnableModelName(agent.modelName, capability, this._mobileClass) === model;
|
|
799
|
+
const active = await this._currentAgent(activeAgentId, deviceId);
|
|
800
|
+
if (active?.providerDeviceId && b4a.equals(active.providerDeviceId, deviceId) && runs(active)) {
|
|
801
|
+
return active;
|
|
802
|
+
}
|
|
803
|
+
const { agents } = await this._engine.listAgents({ meshId, providerDeviceId: deviceId });
|
|
804
|
+
return (agents ?? []).find(runs) ?? null;
|
|
805
|
+
}
|
|
806
|
+
unloadModel(name) {
|
|
807
|
+
const pending = this._unloading.get(name);
|
|
808
|
+
if (pending) {
|
|
809
|
+
return pending;
|
|
810
|
+
}
|
|
811
|
+
const unloading = Promise.resolve()
|
|
812
|
+
.then(() => this._unloadModel(name))
|
|
813
|
+
.finally(() => this._unloading.delete(name));
|
|
814
|
+
this._unloading.set(name, unloading);
|
|
815
|
+
return unloading;
|
|
816
|
+
}
|
|
817
|
+
async _unloadModel(name) {
|
|
818
|
+
if (!this._harness.unloadModel) {
|
|
819
|
+
throw new Error('the model runtime cannot unload a model');
|
|
820
|
+
}
|
|
821
|
+
const holders = [...this._runs.values()].filter((run) => run.executing && !run.borrowed && run.model === name);
|
|
822
|
+
for (const run of holders) {
|
|
823
|
+
run.controller.abort(MODEL_UNLOADED);
|
|
824
|
+
}
|
|
825
|
+
await Promise.allSettled(holders.map((run) => run.done));
|
|
826
|
+
const { unloaded } = await this._harness.unloadModel({ model: name });
|
|
827
|
+
this._log?.info('models', 'model unload requested', {
|
|
828
|
+
model: name,
|
|
829
|
+
unloaded,
|
|
830
|
+
requeued: holders.length
|
|
831
|
+
});
|
|
832
|
+
return { unloaded };
|
|
833
|
+
}
|
|
834
|
+
notifyModelCatalog() {
|
|
835
|
+
this._modelCatalog.notify();
|
|
836
|
+
}
|
|
675
837
|
async retryModelDownload(name) {
|
|
676
|
-
if (!this._downloads)
|
|
838
|
+
if (!this._downloads) {
|
|
677
839
|
throw new Error('model download reconciler is unavailable');
|
|
840
|
+
}
|
|
678
841
|
await this._downloads.retry(name);
|
|
679
842
|
}
|
|
680
843
|
async skills(agentId, modelName, options) {
|
|
@@ -684,7 +847,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
684
847
|
const meshId = options?.meshId ?? this._accountMeshId;
|
|
685
848
|
const agent = meshId ? await this._engine.getAgent({ agentId, meshId }).catch(noop) : null;
|
|
686
849
|
const deviceId = this._deviceId;
|
|
687
|
-
if (agent &&
|
|
850
|
+
if (agent &&
|
|
851
|
+
deviceId &&
|
|
852
|
+
(!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, deviceId))) {
|
|
688
853
|
agentId = null;
|
|
689
854
|
options = { ...options, skipCredentialProbe: true };
|
|
690
855
|
}
|
|
@@ -695,37 +860,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
695
860
|
}
|
|
696
861
|
async _recordSkillReadiness() {
|
|
697
862
|
this._readinessDirty = true;
|
|
698
|
-
if (this._recordingReadiness)
|
|
863
|
+
if (this._recordingReadiness) {
|
|
699
864
|
return;
|
|
865
|
+
}
|
|
700
866
|
this._recordingReadiness = true;
|
|
701
867
|
try {
|
|
702
868
|
while (this._readinessDirty) {
|
|
703
869
|
this._readinessDirty = false;
|
|
704
|
-
const
|
|
705
|
-
|
|
706
|
-
.filter((skill) => isSetUpHere(skill.status))
|
|
707
|
-
.map((skill) => skill.name);
|
|
708
|
-
const deviceId = this._deviceId;
|
|
709
|
-
const meshId = this._accountMeshId;
|
|
710
|
-
if (deviceId && meshId) {
|
|
711
|
-
const { agents } = await this._engine.listAgents({ meshId, providerDeviceId: deviceId });
|
|
712
|
-
const hosted = await Promise.all([...this._hostedIn.keys()]
|
|
713
|
-
.filter((shared) => shared !== meshId)
|
|
714
|
-
.map((shared) => this._engine.listAgents({ meshId: shared, providerDeviceId: deviceId }).catch(noop)));
|
|
715
|
-
const seen = new Set();
|
|
716
|
-
for (const agent of [agents ?? [], ...hosted.map((list) => list?.agents ?? [])].flat()) {
|
|
717
|
-
if (seen.has(agent.id))
|
|
718
|
-
continue;
|
|
719
|
-
seen.add(agent.id);
|
|
720
|
-
const resolved = (await this._harness.skills?.(agent.id, agent.modelName, {
|
|
721
|
-
skipCredentialProbe: true
|
|
722
|
-
})) ?? [];
|
|
723
|
-
ready.push(...encodeAgentSkillReadiness(agent.id, resolved.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name)));
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
ready.sort();
|
|
727
|
-
if (this._readySkills && sameNameList(this._readySkills, ready))
|
|
870
|
+
const ready = await this._readySkillNames();
|
|
871
|
+
if (this._readySkills && sameNameList(this._readySkills, ready)) {
|
|
728
872
|
continue;
|
|
873
|
+
}
|
|
729
874
|
this._readySkills = ready;
|
|
730
875
|
await this._reportCapabilities();
|
|
731
876
|
}
|
|
@@ -734,6 +879,29 @@ export class QvacAssistant extends ReadyResource {
|
|
|
734
879
|
this._recordingReadiness = false;
|
|
735
880
|
}
|
|
736
881
|
}
|
|
882
|
+
async _readySkillNames() {
|
|
883
|
+
const unscoped = (await this._harness.skills?.(null, null, { skipCredentialProbe: true })) ?? [];
|
|
884
|
+
const ready = unscoped.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name);
|
|
885
|
+
const deviceId = this._deviceId;
|
|
886
|
+
const meshId = this._accountMeshId;
|
|
887
|
+
if (deviceId && meshId) {
|
|
888
|
+
const { agents } = await this._engine.listAgents({ meshId, providerDeviceId: deviceId });
|
|
889
|
+
const hosted = await Promise.all([...this._hostedIn.keys()]
|
|
890
|
+
.filter((shared) => shared !== meshId)
|
|
891
|
+
.map((shared) => this._engine.listAgents({ meshId: shared, providerDeviceId: deviceId }).catch(noop)));
|
|
892
|
+
const seen = new Set();
|
|
893
|
+
for (const agent of [agents ?? [], ...hosted.map((list) => list?.agents ?? [])].flat()) {
|
|
894
|
+
if (seen.has(agent.id))
|
|
895
|
+
continue;
|
|
896
|
+
seen.add(agent.id);
|
|
897
|
+
const resolved = (await this._harness.skills?.(agent.id, agent.modelName, {
|
|
898
|
+
skipCredentialProbe: true
|
|
899
|
+
})) ?? [];
|
|
900
|
+
ready.push(...encodeAgentSkillReadiness(agent.id, resolved.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name)));
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
return ready.sort();
|
|
904
|
+
}
|
|
737
905
|
async tools() {
|
|
738
906
|
return (await this._harness.tools?.()) ?? [];
|
|
739
907
|
}
|
|
@@ -745,13 +913,15 @@ export class QvacAssistant extends ReadyResource {
|
|
|
745
913
|
if (err !== null && !isMissingMesh(err)) {
|
|
746
914
|
this._log?.error('assistant', `${label} watch failed`, err);
|
|
747
915
|
}
|
|
748
|
-
if (again)
|
|
916
|
+
if (again) {
|
|
749
917
|
start();
|
|
918
|
+
}
|
|
750
919
|
};
|
|
751
920
|
const start = () => {
|
|
752
921
|
again = false;
|
|
753
|
-
if (this.closing || this.suspended)
|
|
922
|
+
if (this.closing || this.suspended) {
|
|
754
923
|
return;
|
|
924
|
+
}
|
|
755
925
|
if (running) {
|
|
756
926
|
again = true;
|
|
757
927
|
return;
|
|
@@ -767,21 +937,30 @@ export class QvacAssistant extends ReadyResource {
|
|
|
767
937
|
if (this.closing || this.suspended)
|
|
768
938
|
return;
|
|
769
939
|
const ids = (frame.chats ?? []).map((chat) => chat.id);
|
|
940
|
+
const gone = new Set(known === null ? [] : disappearedChatIds(known, ids));
|
|
770
941
|
if (known !== null) {
|
|
771
|
-
for (const chatId of
|
|
772
|
-
this.
|
|
773
|
-
this._chatAttribution.delete(chatId);
|
|
774
|
-
void this._deleteChatKvCache(chatId, 'chat deleted');
|
|
775
|
-
void this._inferenceCalls
|
|
776
|
-
.forget(chatId)
|
|
777
|
-
.catch((err) => this._log?.warn('assistant', 'inspector records not dropped', { err }));
|
|
942
|
+
for (const chatId of gone) {
|
|
943
|
+
this._dropDeletedChat(chatId);
|
|
778
944
|
}
|
|
779
945
|
}
|
|
780
946
|
known = new Set(ids);
|
|
781
|
-
await this._sweepImageCache();
|
|
947
|
+
await this._sweepImageCache(gone);
|
|
782
948
|
await this._sweepInferenceCalls();
|
|
783
949
|
}
|
|
784
950
|
}
|
|
951
|
+
_dropDeletedChat(chatId) {
|
|
952
|
+
for (const run of this._runs.values()) {
|
|
953
|
+
if (run.chatId === chatId) {
|
|
954
|
+
void RUN_SETTLED;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
this._reconciledCompactions.delete(chatId);
|
|
958
|
+
this._chatAttribution.delete(chatId);
|
|
959
|
+
void this._deleteChatKvCache(chatId, 'chat deleted');
|
|
960
|
+
void this._inferenceCalls
|
|
961
|
+
.forget(chatId)
|
|
962
|
+
.catch((err) => this._log?.warn('assistant', 'inspector records not dropped', { err }));
|
|
963
|
+
}
|
|
785
964
|
async _reclaimRekeyedAgents(meshId, frames) {
|
|
786
965
|
let known = null;
|
|
787
966
|
for await (const frame of frames) {
|
|
@@ -790,12 +969,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
790
969
|
const agents = frame.agents ?? [];
|
|
791
970
|
this._followHosted(meshId, agents);
|
|
792
971
|
const next = new Map(agents.map((agent) => [agent.id, agentRekeyFingerprint(agent)]));
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
void this._reclaimAgentChatCaches(meshId, agent.id);
|
|
798
|
-
}
|
|
972
|
+
for (const agent of agents) {
|
|
973
|
+
const previous = known?.get(agent.id);
|
|
974
|
+
if (previous !== undefined && previous !== next.get(agent.id)) {
|
|
975
|
+
void this._reclaimAgentChatCaches(meshId, agent.id);
|
|
799
976
|
}
|
|
800
977
|
}
|
|
801
978
|
known = next;
|
|
@@ -805,8 +982,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
805
982
|
try {
|
|
806
983
|
const { chats } = await this._engine.listChats({ meshId });
|
|
807
984
|
for (const chat of chats ?? []) {
|
|
808
|
-
if (chat.agentId === agentId)
|
|
985
|
+
if (chat.agentId === agentId) {
|
|
809
986
|
await this._deleteChatKvCache(chat.id, 'agent re-key');
|
|
987
|
+
}
|
|
810
988
|
}
|
|
811
989
|
}
|
|
812
990
|
catch (err) {
|
|
@@ -814,25 +992,31 @@ export class QvacAssistant extends ReadyResource {
|
|
|
814
992
|
}
|
|
815
993
|
}
|
|
816
994
|
async _reconcileCompaction(engine, chatId) {
|
|
817
|
-
const deviceId = this.
|
|
995
|
+
const deviceId = this._requireDeviceId();
|
|
818
996
|
const { chunks } = await engine
|
|
819
997
|
.listChunks({ chatId, type: 'compaction' })
|
|
820
998
|
.catch(() => ({ chunks: [] }));
|
|
821
999
|
let newest = null;
|
|
822
|
-
for (const chunk of chunks ?? [])
|
|
823
|
-
if (!newest || chunk.seq > newest.seq)
|
|
1000
|
+
for (const chunk of chunks ?? []) {
|
|
1001
|
+
if (!newest || chunk.seq > newest.seq) {
|
|
824
1002
|
newest = chunk;
|
|
825
|
-
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
if (!newest || (newest.originDeviceId && b4a.equals(newest.originDeviceId, deviceId))) {
|
|
826
1006
|
return;
|
|
827
|
-
|
|
1007
|
+
}
|
|
1008
|
+
if (this._reconciledCompactions.get(chatId) === newest.id) {
|
|
828
1009
|
return;
|
|
1010
|
+
}
|
|
829
1011
|
const gone = await this._deleteChatKvCache(chatId, 'compaction from another device');
|
|
830
|
-
if (gone)
|
|
1012
|
+
if (gone) {
|
|
831
1013
|
this._reconciledCompactions.set(chatId, newest.id);
|
|
1014
|
+
}
|
|
832
1015
|
}
|
|
833
1016
|
async _senderNames(engine, chunks) {
|
|
834
|
-
if (!couldNameSenders(chunks))
|
|
1017
|
+
if (!couldNameSenders(chunks)) {
|
|
835
1018
|
return new Map();
|
|
1019
|
+
}
|
|
836
1020
|
const stamped = chunks.every((chunk) => chunk.type !== 'user-message' || chunk.senderMemberId);
|
|
837
1021
|
const [{ members }, { devices }] = await Promise.all([
|
|
838
1022
|
engine.listMembers({}).catch(() => ({ members: [] })),
|
|
@@ -843,15 +1027,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
843
1027
|
async _reconcileAttribution(chatId, senders) {
|
|
844
1028
|
const attribution = senderAttribution(senders);
|
|
845
1029
|
const seen = this._chatAttribution.get(chatId);
|
|
846
|
-
if (seen === attribution)
|
|
1030
|
+
if (seen === attribution) {
|
|
847
1031
|
return;
|
|
1032
|
+
}
|
|
848
1033
|
if (seen === undefined && !attribution) {
|
|
849
1034
|
this._chatAttribution.set(chatId, attribution);
|
|
850
1035
|
return;
|
|
851
1036
|
}
|
|
852
1037
|
const gone = await this._deleteChatKvCache(chatId, 'sender attribution changed');
|
|
853
|
-
if (gone)
|
|
1038
|
+
if (gone) {
|
|
854
1039
|
this._chatAttribution.set(chatId, attribution);
|
|
1040
|
+
}
|
|
855
1041
|
}
|
|
856
1042
|
async deleteChatKvCache(chatId) {
|
|
857
1043
|
return ((await this._harness.deleteKvCache?.(chatKvCacheKey(chatId))) ?? {
|
|
@@ -930,8 +1116,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
930
1116
|
this._advertisedOn = null;
|
|
931
1117
|
return;
|
|
932
1118
|
}
|
|
933
|
-
if (on === this._advertisedOn)
|
|
1119
|
+
if (on === this._advertisedOn) {
|
|
934
1120
|
return;
|
|
1121
|
+
}
|
|
935
1122
|
this._advertisedOn = on;
|
|
936
1123
|
this._downloads?.meshAdopted();
|
|
937
1124
|
this._lastCapabilities.clear();
|
|
@@ -969,16 +1156,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
969
1156
|
if (this.closing || this.suspended)
|
|
970
1157
|
return;
|
|
971
1158
|
const chunks = frame.chunks ?? [];
|
|
972
|
-
for (const chunk of chunks)
|
|
1159
|
+
for (const chunk of chunks) {
|
|
973
1160
|
this._dispatch(chunk, meshId);
|
|
1161
|
+
}
|
|
974
1162
|
this._pruneSeen(meshId, new Set(chunks.map((chunk) => chunk.runId)));
|
|
975
1163
|
}
|
|
976
1164
|
}
|
|
977
1165
|
_stopMeshIntake() {
|
|
978
1166
|
this._meshListWatch.stop();
|
|
979
1167
|
this._accountWatch.stop();
|
|
980
|
-
for (const consumer of this._meshConsumers.values())
|
|
1168
|
+
for (const consumer of this._meshConsumers.values()) {
|
|
981
1169
|
consumer.stop();
|
|
1170
|
+
}
|
|
982
1171
|
this._meshConsumers.clear();
|
|
983
1172
|
this._advertisedOn = null;
|
|
984
1173
|
}
|
|
@@ -991,8 +1180,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
991
1180
|
}
|
|
992
1181
|
_lenderFor(agent) {
|
|
993
1182
|
const lender = agent.lenderDeviceId;
|
|
994
|
-
if (!lender || !this._borrowHarness)
|
|
1183
|
+
if (!lender || !this._borrowHarness) {
|
|
995
1184
|
return null;
|
|
1185
|
+
}
|
|
996
1186
|
const id = b4a.toHex(lender);
|
|
997
1187
|
const own = this._deviceId;
|
|
998
1188
|
return own && id === b4a.toHex(own) ? null : id;
|
|
@@ -1000,41 +1190,47 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1000
1190
|
_harnessFor(agent) {
|
|
1001
1191
|
const id = this._lenderFor(agent);
|
|
1002
1192
|
const borrow = this._borrowHarness;
|
|
1003
|
-
if (!id || !borrow)
|
|
1193
|
+
if (!id || !borrow) {
|
|
1004
1194
|
return this._harness;
|
|
1195
|
+
}
|
|
1005
1196
|
const held = this._borrowed.get(id);
|
|
1006
|
-
if (held)
|
|
1197
|
+
if (held) {
|
|
1007
1198
|
return held;
|
|
1199
|
+
}
|
|
1008
1200
|
const remote = connectHarness(() => borrow(id));
|
|
1009
|
-
remote.on('status', (
|
|
1010
|
-
if (status.state === 'died')
|
|
1201
|
+
remote.on('status', (status) => {
|
|
1202
|
+
if (status.state === 'died') {
|
|
1011
1203
|
this._borrowed.delete(id);
|
|
1012
|
-
|
|
1204
|
+
}
|
|
1205
|
+
});
|
|
1013
1206
|
const harness = remote;
|
|
1014
1207
|
this._borrowed.set(id, harness);
|
|
1015
1208
|
return harness;
|
|
1016
1209
|
}
|
|
1017
1210
|
_dispatch(request, meshId) {
|
|
1018
1211
|
const runId = request.runId;
|
|
1019
|
-
if (!runId || this._runs.has(runId) || this._seen.has(runId))
|
|
1212
|
+
if (!runId || this._runs.has(runId) || this._seen.has(runId)) {
|
|
1020
1213
|
return;
|
|
1214
|
+
}
|
|
1021
1215
|
this._seen.set(runId, meshId);
|
|
1022
1216
|
const engine = meshScoped(this._engine, meshId);
|
|
1023
1217
|
const settle = (async () => {
|
|
1024
1218
|
const agentId = request.runRequest?.agentId;
|
|
1025
|
-
const agent = await engine.getAgent({ agentId }).catch(noop);
|
|
1219
|
+
const agent = agentId ? await engine.getAgent({ agentId }).catch(noop) : undefined;
|
|
1026
1220
|
if (!agent) {
|
|
1027
1221
|
this._seen.delete(runId);
|
|
1028
1222
|
return;
|
|
1029
1223
|
}
|
|
1030
|
-
if (!agent.providerDeviceId
|
|
1224
|
+
if (!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, this._requireDeviceId())) {
|
|
1031
1225
|
return;
|
|
1032
|
-
|
|
1226
|
+
}
|
|
1227
|
+
if (await this._isHandled(engine, request)) {
|
|
1033
1228
|
return;
|
|
1229
|
+
}
|
|
1034
1230
|
this._log?.info('runs', 'run claimed', { runId, chatId: request.chatId, agentId });
|
|
1035
1231
|
const controller = new AbortController();
|
|
1036
1232
|
const prior = this._chats.get(request.chatId) ?? Promise.resolve();
|
|
1037
|
-
const done = prior.then(() => this._execute(request, agent, controller, meshId));
|
|
1233
|
+
const done = prior.then(() => this._execute(request, agent, { controller, meshId, runId }));
|
|
1038
1234
|
const link = done.catch(noop);
|
|
1039
1235
|
this._chats.set(request.chatId, link);
|
|
1040
1236
|
this._runs.set(runId, {
|
|
@@ -1042,7 +1238,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1042
1238
|
done,
|
|
1043
1239
|
chatId: request.chatId,
|
|
1044
1240
|
model: agent.modelName,
|
|
1045
|
-
executing: false
|
|
1241
|
+
executing: false,
|
|
1242
|
+
borrowed: false
|
|
1046
1243
|
});
|
|
1047
1244
|
try {
|
|
1048
1245
|
await done;
|
|
@@ -1051,17 +1248,23 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1051
1248
|
this._runImageCacheNames.delete(runId);
|
|
1052
1249
|
this._runs.delete(runId);
|
|
1053
1250
|
this._yieldDownloads();
|
|
1054
|
-
if (this._chats.get(request.chatId) === link)
|
|
1251
|
+
if (this._chats.get(request.chatId) === link) {
|
|
1055
1252
|
this._chats.delete(request.chatId);
|
|
1253
|
+
}
|
|
1056
1254
|
void this._reportCapabilities();
|
|
1255
|
+
const requeued = controller.signal.reason === MODEL_UNLOADED && !this._seen.has(runId);
|
|
1256
|
+
if (requeued && !this.closing) {
|
|
1257
|
+
this._dispatch(request, meshId);
|
|
1258
|
+
}
|
|
1057
1259
|
}
|
|
1058
1260
|
})();
|
|
1059
1261
|
settle.catch((err) => this._log?.error('assistant', `run ${runId} failed`, err));
|
|
1060
1262
|
}
|
|
1061
1263
|
async _citeWebSources(transcript, runId, result) {
|
|
1062
1264
|
const records = webSourcesFromToolResult(result, WEB_RAG_SOURCE_BOUNDS);
|
|
1063
|
-
if (records.length === 0)
|
|
1265
|
+
if (records.length === 0) {
|
|
1064
1266
|
return;
|
|
1267
|
+
}
|
|
1065
1268
|
try {
|
|
1066
1269
|
await transcript.chunk({ type: 'sources', sources: { sources: records } });
|
|
1067
1270
|
}
|
|
@@ -1069,7 +1272,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1069
1272
|
this._log?.warn('assistant', 'web sources chunk skipped', { runId, err });
|
|
1070
1273
|
}
|
|
1071
1274
|
}
|
|
1072
|
-
async _searchKnowledge(engine,
|
|
1275
|
+
async _searchKnowledge(engine, messages, { chatId, attachmentIds, signal, preparing }) {
|
|
1073
1276
|
let lastUser;
|
|
1074
1277
|
for (let index = messages.length - 1; index >= 0; index--) {
|
|
1075
1278
|
if (messages[index].role === 'user') {
|
|
@@ -1078,27 +1281,77 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1078
1281
|
}
|
|
1079
1282
|
}
|
|
1080
1283
|
const query = lastUser?.content.trim();
|
|
1081
|
-
if (!lastUser || !query)
|
|
1284
|
+
if (!lastUser || !query) {
|
|
1082
1285
|
return null;
|
|
1286
|
+
}
|
|
1083
1287
|
try {
|
|
1084
1288
|
const search = await searchKnowledge(engine, {
|
|
1085
1289
|
chatId,
|
|
1086
1290
|
query,
|
|
1087
1291
|
attachmentIds: attachmentIds.length > 0 ? [...attachmentIds] : undefined
|
|
1088
1292
|
}, { signal, preparing });
|
|
1089
|
-
if (!search.enabled)
|
|
1293
|
+
if (!search.enabled) {
|
|
1090
1294
|
return null;
|
|
1091
|
-
|
|
1295
|
+
}
|
|
1296
|
+
if (!search.hasKnowledge) {
|
|
1092
1297
|
return null;
|
|
1298
|
+
}
|
|
1093
1299
|
return { search, lastUser, hits: search.hits ?? [] };
|
|
1094
1300
|
}
|
|
1095
1301
|
catch (err) {
|
|
1096
|
-
if (err instanceof AbortedError)
|
|
1302
|
+
if (err instanceof AbortedError) {
|
|
1097
1303
|
return null;
|
|
1304
|
+
}
|
|
1098
1305
|
this._log?.warn('assistant', 'knowledge search unavailable', err);
|
|
1099
1306
|
return null;
|
|
1100
1307
|
}
|
|
1101
1308
|
}
|
|
1309
|
+
async _injectKnowledge(knowledge, sinks) {
|
|
1310
|
+
const injected = { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
1311
|
+
try {
|
|
1312
|
+
if (knowledge.search.errorMessage) {
|
|
1313
|
+
this._log?.warn('assistant', 'knowledge search failed', {
|
|
1314
|
+
error: knowledge.search.errorMessage
|
|
1315
|
+
});
|
|
1316
|
+
}
|
|
1317
|
+
const fullTextOnly = knowledge.hits.length > 0 && knowledge.hits.every(isFullTextHit);
|
|
1318
|
+
if (fullTextOnly) {
|
|
1319
|
+
knowledge.lastUser.content = `${formatFullDocumentContext(knowledge.hits)}\n\nThe user's message:\n${knowledge.lastUser.content}`;
|
|
1320
|
+
injected.documentsInjected = true;
|
|
1321
|
+
if (knowledge.hits.some(isTruncatedFullTextHit)) {
|
|
1322
|
+
await sinks.transcript.chunk({
|
|
1323
|
+
type: 'sources',
|
|
1324
|
+
sources: { sources: [], notice: RAG_TRUNCATED_NOTICE }
|
|
1325
|
+
});
|
|
1326
|
+
injected.sourcesWritten = true;
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
else {
|
|
1330
|
+
await injectRetrievedKnowledge(knowledge, { ...sinks, injected });
|
|
1331
|
+
}
|
|
1332
|
+
if (knowledge.search.pendingFiles?.length) {
|
|
1333
|
+
knowledge.lastUser.content += `\n\n[Files not yet available for this reply: ${knowledge.search.pendingFiles.join(', ')}.]`;
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
catch (err) {
|
|
1337
|
+
this._log?.warn('assistant', 'knowledge injection skipped', err);
|
|
1338
|
+
}
|
|
1339
|
+
return injected;
|
|
1340
|
+
}
|
|
1341
|
+
async _writeUnreadNotice(transcript, reason) {
|
|
1342
|
+
try {
|
|
1343
|
+
await transcript.chunk({
|
|
1344
|
+
type: 'sources',
|
|
1345
|
+
sources: {
|
|
1346
|
+
sources: [],
|
|
1347
|
+
notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE
|
|
1348
|
+
}
|
|
1349
|
+
});
|
|
1350
|
+
}
|
|
1351
|
+
catch (err) {
|
|
1352
|
+
this._log?.warn('assistant', 'unread-document notice skipped', err);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1102
1355
|
async _isHandled(engine, request) {
|
|
1103
1356
|
const { chunks } = await engine.listChunks({
|
|
1104
1357
|
chatId: request.chatId,
|
|
@@ -1106,28 +1359,41 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1106
1359
|
});
|
|
1107
1360
|
return (chunks ?? []).some((c) => c.runId === request.runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
1108
1361
|
}
|
|
1109
|
-
async _execute(request, agent, controller, meshId) {
|
|
1362
|
+
async _execute(request, agent, { controller, meshId, runId }) {
|
|
1110
1363
|
const engine = meshScoped(this._engine, meshId);
|
|
1111
1364
|
const { chatId } = request;
|
|
1112
|
-
const
|
|
1113
|
-
const deviceId = this._deviceId;
|
|
1365
|
+
const deviceId = this._requireDeviceId();
|
|
1114
1366
|
if (this.suspended) {
|
|
1115
1367
|
this._seen.delete(runId);
|
|
1116
1368
|
return;
|
|
1117
1369
|
}
|
|
1118
|
-
if (await this._isHandled(engine, request))
|
|
1370
|
+
if (await this._isHandled(engine, request)) {
|
|
1119
1371
|
return;
|
|
1372
|
+
}
|
|
1120
1373
|
const fresh = await engine.getAgent({ agentId: agent.id }).catch(noop);
|
|
1121
|
-
if (!fresh ||
|
|
1374
|
+
if (!fresh ||
|
|
1375
|
+
fresh.deletedAt ||
|
|
1376
|
+
!fresh.providerDeviceId ||
|
|
1377
|
+
!b4a.equals(fresh.providerDeviceId, deviceId)) {
|
|
1122
1378
|
return;
|
|
1379
|
+
}
|
|
1123
1380
|
agent = fresh;
|
|
1124
1381
|
const capability = request.capability;
|
|
1125
1382
|
if (capability) {
|
|
1126
|
-
return this._invokeCapability({
|
|
1383
|
+
return this._invokeCapability({
|
|
1384
|
+
engine,
|
|
1385
|
+
request,
|
|
1386
|
+
agent,
|
|
1387
|
+
capability,
|
|
1388
|
+
controller,
|
|
1389
|
+
meshId,
|
|
1390
|
+
runId
|
|
1391
|
+
});
|
|
1127
1392
|
}
|
|
1128
1393
|
await this._reconcileCompaction(engine, chatId);
|
|
1129
|
-
if (request.parentId)
|
|
1394
|
+
if (request.parentId) {
|
|
1130
1395
|
await this._deleteChatKvCache(chatId, 'superseding run');
|
|
1396
|
+
}
|
|
1131
1397
|
const lender = this._lenderFor(agent);
|
|
1132
1398
|
const model = lender
|
|
1133
1399
|
? agent.modelName
|
|
@@ -1139,9 +1405,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1139
1405
|
requested: agent.modelName,
|
|
1140
1406
|
model
|
|
1141
1407
|
});
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1408
|
+
}
|
|
1409
|
+
const claimed = this._runs.get(runId);
|
|
1410
|
+
if (claimed) {
|
|
1411
|
+
this._runs.set(runId, { ...claimed, model, borrowed: lender !== null });
|
|
1145
1412
|
}
|
|
1146
1413
|
const config = await engine.getConfig({}).catch(noop);
|
|
1147
1414
|
const configuredAgent = withConfigDefaults(agent, config);
|
|
@@ -1174,7 +1441,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1174
1441
|
? all.find((chunk) => chunk.id === currentRepair.parentId)
|
|
1175
1442
|
: undefined;
|
|
1176
1443
|
const acceptedRepairs = acceptedRepairCount(branches, runId);
|
|
1177
|
-
const history = all.filter((chunk) =>
|
|
1444
|
+
const history = all.filter((chunk) => !!chunk.compaction ||
|
|
1178
1445
|
(chunk.runId !== runId &&
|
|
1179
1446
|
inputAvailableToRun(chunk, request.seq) &&
|
|
1180
1447
|
(!repairParent || chunk.seq <= repairParent.seq || chunk.runId === repairParent.runId) &&
|
|
@@ -1193,8 +1460,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1193
1460
|
await this._reconcileAttribution(chatId, senders);
|
|
1194
1461
|
}
|
|
1195
1462
|
const messages = historyFromChunks(history, staleToolRunIds(history, priorAsks ?? [], runId), senders);
|
|
1196
|
-
if (currentRepair)
|
|
1463
|
+
if (currentRepair) {
|
|
1197
1464
|
messages.push({ role: 'user', content: currentRepair.promptText });
|
|
1465
|
+
}
|
|
1198
1466
|
const diagramRepair = currentRepair !== undefined;
|
|
1199
1467
|
const runAgent = diagramRepair
|
|
1200
1468
|
? {
|
|
@@ -1209,6 +1477,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1209
1477
|
? this._harness.supportsImagesFor(model)
|
|
1210
1478
|
: (this._harness.supportsImages ?? this._info.supportsImages) !== false;
|
|
1211
1479
|
let unreadableImages = new Map();
|
|
1480
|
+
let renderedPdfIds = new Set();
|
|
1212
1481
|
if (!seesImages) {
|
|
1213
1482
|
const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
|
|
1214
1483
|
if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
|
|
@@ -1220,17 +1489,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1220
1489
|
turn.content += `\n[Attached image${turn.images.length > 1 ? 's' : ''}: ${names} — not visible to this model]`;
|
|
1221
1490
|
}
|
|
1222
1491
|
delete turn.images;
|
|
1492
|
+
delete turn.pdfs;
|
|
1223
1493
|
}
|
|
1224
1494
|
}
|
|
1225
1495
|
else {
|
|
1226
1496
|
unreadableImages = await this._hydrateImages(messages, controller.signal, runId);
|
|
1227
1497
|
}
|
|
1228
|
-
const settledWatch = this._watchSettled(engine, chatId, runId, controller);
|
|
1498
|
+
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
1229
1499
|
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
1230
1500
|
let readinessError = null;
|
|
1231
1501
|
try {
|
|
1232
|
-
if (!lender)
|
|
1502
|
+
if (!lender) {
|
|
1233
1503
|
await this._downloads?.waitUntilCached(model, controller.signal);
|
|
1504
|
+
}
|
|
1234
1505
|
}
|
|
1235
1506
|
catch (err) {
|
|
1236
1507
|
readinessError = err instanceof Error ? err : new Error(String(err));
|
|
@@ -1238,8 +1509,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1238
1509
|
if (controller.signal.aborted) {
|
|
1239
1510
|
try {
|
|
1240
1511
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1241
|
-
if (outcome.kind === 'replay')
|
|
1512
|
+
if (outcome.kind === 'replay') {
|
|
1242
1513
|
this._seen.delete(runId);
|
|
1514
|
+
}
|
|
1243
1515
|
if (outcome.kind === 'failed') {
|
|
1244
1516
|
await transcript.status({ state: 'failed', reason: outcome.reason });
|
|
1245
1517
|
}
|
|
@@ -1263,21 +1535,27 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1263
1535
|
return;
|
|
1264
1536
|
}
|
|
1265
1537
|
const inflight = this._runs.get(runId);
|
|
1266
|
-
if (inflight)
|
|
1538
|
+
if (inflight) {
|
|
1267
1539
|
this._runs.set(runId, { ...inflight, executing: true });
|
|
1540
|
+
}
|
|
1268
1541
|
this._yieldDownloads();
|
|
1269
1542
|
const busyChatId = this._modelHolder(runId, chatId, model);
|
|
1270
1543
|
const executing = await transcript.status(busyChatId
|
|
1271
1544
|
? { state: 'executing', progress: { phase: 'queued', busyChatId } }
|
|
1272
1545
|
: { state: 'executing' });
|
|
1273
1546
|
const runStatusId = executing.id;
|
|
1274
|
-
const approvals = this._createApprove(engine,
|
|
1547
|
+
const approvals = this._createApprove(engine, {
|
|
1548
|
+
chatId,
|
|
1549
|
+
runId,
|
|
1550
|
+
deviceId,
|
|
1551
|
+
agentId: agent.id,
|
|
1552
|
+
hasRun: () => ran
|
|
1553
|
+
});
|
|
1275
1554
|
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
|
|
1276
1555
|
let metrics;
|
|
1277
1556
|
const sourceTagFilter = createRagSourceTagFilter();
|
|
1278
1557
|
const retrievedRecords = [];
|
|
1279
1558
|
let answerText = '';
|
|
1280
|
-
let ragInjected = false;
|
|
1281
1559
|
const flushTranscript = async () => {
|
|
1282
1560
|
const tail = sourceTagFilter.flush();
|
|
1283
1561
|
if (tail) {
|
|
@@ -1289,13 +1567,74 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1289
1567
|
let titleOutcome = null;
|
|
1290
1568
|
let runFailure = null;
|
|
1291
1569
|
let runVerdict = 'completed';
|
|
1570
|
+
const writeText = async (type, raw) => {
|
|
1571
|
+
const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
|
|
1572
|
+
if (!text) {
|
|
1573
|
+
return;
|
|
1574
|
+
}
|
|
1575
|
+
if (type === 'content') {
|
|
1576
|
+
answerText += text;
|
|
1577
|
+
}
|
|
1578
|
+
await transcript.text(type, text);
|
|
1579
|
+
};
|
|
1580
|
+
const failRun = async (message) => {
|
|
1581
|
+
const folded = isContextOverflowFailure(message) &&
|
|
1582
|
+
(await this._runCompaction({
|
|
1583
|
+
chatId,
|
|
1584
|
+
transcript,
|
|
1585
|
+
trigger: 'overflow',
|
|
1586
|
+
signal: controller.signal,
|
|
1587
|
+
meshId
|
|
1588
|
+
}));
|
|
1589
|
+
runFailure = runFailureNotice(message);
|
|
1590
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
1591
|
+
if (folded) {
|
|
1592
|
+
await engine
|
|
1593
|
+
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
1594
|
+
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
1595
|
+
}
|
|
1596
|
+
titleOutcome = 'ended';
|
|
1597
|
+
};
|
|
1598
|
+
const endAbortedRun = async () => {
|
|
1599
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1600
|
+
if (outcome.kind === 'replay') {
|
|
1601
|
+
runVerdict = 'replay';
|
|
1602
|
+
this._seen.delete(runId);
|
|
1603
|
+
return;
|
|
1604
|
+
}
|
|
1605
|
+
if (outcome.kind === 'handoff') {
|
|
1606
|
+
runVerdict = 'handoff';
|
|
1607
|
+
return;
|
|
1608
|
+
}
|
|
1609
|
+
runFailure = outcome.reason;
|
|
1610
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1611
|
+
titleOutcome = 'ended';
|
|
1612
|
+
};
|
|
1613
|
+
const recordToolResult = async (event) => {
|
|
1614
|
+
if (isDeniedResult(event.result)) {
|
|
1615
|
+
this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
|
|
1616
|
+
}
|
|
1617
|
+
else if (event.name !== SKILL_TOOL) {
|
|
1618
|
+
ran = true;
|
|
1619
|
+
}
|
|
1620
|
+
if (WEB_TOOLS.includes(String(event.name))) {
|
|
1621
|
+
await this._citeWebSources(transcript, runId, event.result);
|
|
1622
|
+
}
|
|
1623
|
+
await transcript.chunk(toolResultChunk(event));
|
|
1624
|
+
};
|
|
1292
1625
|
try {
|
|
1293
1626
|
const attachTurnIds = attachTurnDocumentIds(history);
|
|
1294
|
-
if (attachTurnIds.length > 0)
|
|
1627
|
+
if (attachTurnIds.length > 0) {
|
|
1295
1628
|
backfillDocumentOnlyTurn(messages);
|
|
1629
|
+
}
|
|
1296
1630
|
const knowledge = !diagramRepair &&
|
|
1297
1631
|
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
1298
|
-
? await this._searchKnowledge(engine,
|
|
1632
|
+
? await this._searchKnowledge(engine, messages, {
|
|
1633
|
+
chatId,
|
|
1634
|
+
attachmentIds: attachTurnIds,
|
|
1635
|
+
signal: controller.signal,
|
|
1636
|
+
preparing: modelWatch.documents
|
|
1637
|
+
})
|
|
1299
1638
|
: null;
|
|
1300
1639
|
if (controller.signal.aborted) {
|
|
1301
1640
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
@@ -1312,97 +1651,35 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1312
1651
|
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1313
1652
|
return;
|
|
1314
1653
|
}
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
try {
|
|
1319
|
-
if (knowledge.search.errorMessage) {
|
|
1320
|
-
this._log?.warn('assistant', 'knowledge search failed', {
|
|
1321
|
-
error: knowledge.search.errorMessage
|
|
1322
|
-
});
|
|
1323
|
-
}
|
|
1324
|
-
const fullTextOnly = knowledge.hits.length > 0 && knowledge.hits.every(isFullTextHit);
|
|
1325
|
-
if (fullTextOnly) {
|
|
1326
|
-
knowledge.lastUser.content = `${formatFullDocumentContext(knowledge.hits)}\n\nThe user's message:\n${knowledge.lastUser.content}`;
|
|
1327
|
-
documentsInjected = true;
|
|
1328
|
-
if (knowledge.hits.some(isTruncatedFullTextHit)) {
|
|
1329
|
-
await transcript.chunk({
|
|
1330
|
-
type: 'sources',
|
|
1331
|
-
sources: { sources: [], notice: RAG_TRUNCATED_NOTICE }
|
|
1332
|
-
});
|
|
1333
|
-
sourcesWritten = true;
|
|
1334
|
-
}
|
|
1335
|
-
}
|
|
1336
|
-
else {
|
|
1337
|
-
const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
|
|
1338
|
-
const notice = chooseRagNotice({
|
|
1339
|
-
errorMessage: knowledge.search.errorMessage,
|
|
1340
|
-
hitCount: records.length,
|
|
1341
|
-
searchedCount: knowledge.search.searchedCount ?? 0,
|
|
1342
|
-
indexEmpty: Boolean(knowledge.search.indexEmpty),
|
|
1343
|
-
hasKnowledge: Boolean(knowledge.search.hasKnowledge),
|
|
1344
|
-
pendingReferenced: Boolean(knowledge.search.pendingReferenced),
|
|
1345
|
-
failedFiles: knowledge.search.failedFiles ?? [],
|
|
1346
|
-
pendingFiles: knowledge.search.pendingFiles ?? []
|
|
1347
|
-
});
|
|
1348
|
-
sourceTagFilter.add(records.map((record) => record.title));
|
|
1349
|
-
retrievedRecords.push(...records.map((record, index) => ({
|
|
1350
|
-
...record,
|
|
1351
|
-
excerpt: knowledge.hits[index]?.content ?? record.excerpt
|
|
1352
|
-
})));
|
|
1353
|
-
await transcript.chunk({
|
|
1354
|
-
type: 'sources',
|
|
1355
|
-
sources: { sources: records, notice: notice ?? undefined }
|
|
1356
|
-
});
|
|
1357
|
-
sourcesWritten = true;
|
|
1358
|
-
if (records.length > 0) {
|
|
1359
|
-
knowledge.lastUser.content = `${formatRagContext(knowledge.hits, knowledge.search.availableFiles ?? [])}\n\nThe user's message:\n${knowledge.lastUser.content}\n\n${RAG_CITATION_LINE}`;
|
|
1360
|
-
ragInjected = true;
|
|
1361
|
-
documentsInjected = true;
|
|
1362
|
-
}
|
|
1363
|
-
else if (knowledge.search.availableFiles?.length) {
|
|
1364
|
-
knowledge.lastUser.content += `\n\n[Files indexed and available now: ${knowledge.search.availableFiles.join(', ')}. No relevant excerpts were retrieved for this question. Their current availability supersedes earlier indexing notices.]`;
|
|
1365
|
-
}
|
|
1366
|
-
}
|
|
1367
|
-
if (knowledge.search.pendingFiles?.length) {
|
|
1368
|
-
knowledge.lastUser.content += `\n\n[Files not yet available for this reply: ${knowledge.search.pendingFiles.join(', ')}.]`;
|
|
1369
|
-
}
|
|
1370
|
-
}
|
|
1371
|
-
catch (err) {
|
|
1372
|
-
this._log?.warn('assistant', 'knowledge injection skipped', err);
|
|
1373
|
-
}
|
|
1374
|
-
}
|
|
1654
|
+
const injected = knowledge
|
|
1655
|
+
? await this._injectKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords })
|
|
1656
|
+
: { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
1375
1657
|
const hasAudio = messages.some((turn) => turn.audioAttachments?.length);
|
|
1376
|
-
if (hasAudio)
|
|
1658
|
+
if (hasAudio) {
|
|
1377
1659
|
modelWatch.transcribe(true);
|
|
1660
|
+
}
|
|
1378
1661
|
await this._hydrateAudio(messages, controller.signal);
|
|
1379
|
-
if (hasAudio)
|
|
1662
|
+
if (hasAudio) {
|
|
1380
1663
|
modelWatch.transcribe(false);
|
|
1664
|
+
}
|
|
1381
1665
|
backfillImageOnlyTurns(messages);
|
|
1666
|
+
if (seesImages) {
|
|
1667
|
+
renderedPdfIds = await this._hydratePdfPages(messages, controller.signal, runId);
|
|
1668
|
+
}
|
|
1382
1669
|
annotateDocumentAttachments(messages);
|
|
1383
|
-
|
|
1670
|
+
const unreadIds = attachTurnIds.filter((id) => !renderedPdfIds.has(id));
|
|
1671
|
+
if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
|
|
1384
1672
|
const reason = documentUnreadReason({
|
|
1385
1673
|
knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
|
|
1386
1674
|
failedFiles: knowledge?.search?.failedFiles,
|
|
1387
|
-
attachedCount:
|
|
1675
|
+
attachedCount: unreadIds.length
|
|
1388
1676
|
});
|
|
1389
1677
|
const lastUser = messages.findLast((turn) => turn.role === 'user');
|
|
1390
1678
|
if (lastUser) {
|
|
1391
|
-
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason,
|
|
1679
|
+
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
|
|
1392
1680
|
}
|
|
1393
|
-
if (!sourcesWritten) {
|
|
1394
|
-
|
|
1395
|
-
await transcript.chunk({
|
|
1396
|
-
type: 'sources',
|
|
1397
|
-
sources: {
|
|
1398
|
-
sources: [],
|
|
1399
|
-
notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE
|
|
1400
|
-
}
|
|
1401
|
-
});
|
|
1402
|
-
}
|
|
1403
|
-
catch (err) {
|
|
1404
|
-
this._log?.warn('assistant', 'unread-document notice skipped', err);
|
|
1405
|
-
}
|
|
1681
|
+
if (!injected.sourcesWritten) {
|
|
1682
|
+
await this._writeUnreadNotice(transcript, reason);
|
|
1406
1683
|
}
|
|
1407
1684
|
}
|
|
1408
1685
|
annotateUnreadableImages(unreadableImages);
|
|
@@ -1410,11 +1687,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1410
1687
|
const { roots, write } = await fsGrantsPromise;
|
|
1411
1688
|
const { grantedDirs, grantedDrives, driveOp } = await resolveFsGrants(roots, this._drives, (key, err) => this._log?.warn('assistant', 'drive grant dropped', {
|
|
1412
1689
|
key,
|
|
1413
|
-
error:
|
|
1690
|
+
error: errorMessage(err)
|
|
1414
1691
|
}));
|
|
1415
|
-
if (await this._isHandled(engine, request))
|
|
1692
|
+
if (await this._isHandled(engine, request)) {
|
|
1416
1693
|
return;
|
|
1694
|
+
}
|
|
1417
1695
|
const cacheKey = diagramRepair ? undefined : chatRepairKvCacheKey(chatId, acceptedRepairs);
|
|
1696
|
+
const unloading = lender ? undefined : this._unloading.get(model);
|
|
1697
|
+
if (unloading) {
|
|
1698
|
+
await raceAbort(unloading.catch(noop), controller.signal);
|
|
1699
|
+
}
|
|
1418
1700
|
this._log?.info('runs', 'completion requested', {
|
|
1419
1701
|
runId,
|
|
1420
1702
|
model,
|
|
@@ -1426,14 +1708,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1426
1708
|
agent: runAgent,
|
|
1427
1709
|
messages: nameSenders(messages),
|
|
1428
1710
|
cacheKey,
|
|
1429
|
-
thinkingDefault:
|
|
1711
|
+
thinkingDefault: thinkingDefault(config),
|
|
1430
1712
|
toolConfigs,
|
|
1431
1713
|
grantedDirs,
|
|
1432
1714
|
grantedDrives,
|
|
1433
1715
|
driveOp,
|
|
1434
1716
|
grantedWriteDirs: write,
|
|
1435
1717
|
approve: approvals.approve,
|
|
1436
|
-
attach: (input) => this._uploadGenerated(meshId, chatId, runId, deviceId
|
|
1718
|
+
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
1437
1719
|
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
1438
1720
|
ragSearch: async (input) => {
|
|
1439
1721
|
const found = await raceAbort(engine.ragSearch({
|
|
@@ -1478,15 +1760,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1478
1760
|
for await (const event of events) {
|
|
1479
1761
|
harnessEvents++;
|
|
1480
1762
|
modelWatch.clear();
|
|
1481
|
-
if (event
|
|
1482
|
-
|
|
1483
|
-
? sourceTagFilter.push(event.text)
|
|
1484
|
-
: event.text;
|
|
1485
|
-
if (text) {
|
|
1486
|
-
if (event.type === 'content')
|
|
1487
|
-
answerText += text;
|
|
1488
|
-
await transcript.text(event.type, text);
|
|
1489
|
-
}
|
|
1763
|
+
if (isHarnessEvent(event, 'content') || isHarnessEvent(event, 'thinking')) {
|
|
1764
|
+
await writeText(event.type, event.text);
|
|
1490
1765
|
continue;
|
|
1491
1766
|
}
|
|
1492
1767
|
if (event.type === 'metrics') {
|
|
@@ -1516,57 +1791,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1516
1791
|
}
|
|
1517
1792
|
await flushTranscript();
|
|
1518
1793
|
if (event.type === 'error') {
|
|
1519
|
-
|
|
1520
|
-
(await this._runCompaction({
|
|
1521
|
-
chatId,
|
|
1522
|
-
transcript,
|
|
1523
|
-
trigger: 'overflow',
|
|
1524
|
-
signal: controller.signal,
|
|
1525
|
-
meshId
|
|
1526
|
-
}));
|
|
1527
|
-
runFailure = runFailureNotice(event.message);
|
|
1528
|
-
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
1529
|
-
if (folded) {
|
|
1530
|
-
await engine
|
|
1531
|
-
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
1532
|
-
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
1533
|
-
}
|
|
1534
|
-
titleOutcome = 'ended';
|
|
1794
|
+
await failRun(event.message);
|
|
1535
1795
|
return;
|
|
1536
1796
|
}
|
|
1537
1797
|
if (event.type === 'aborted') {
|
|
1538
|
-
|
|
1539
|
-
if (outcome.kind === 'replay') {
|
|
1540
|
-
runVerdict = 'replay';
|
|
1541
|
-
this._seen.delete(runId);
|
|
1542
|
-
return;
|
|
1543
|
-
}
|
|
1544
|
-
if (outcome.kind === 'handoff') {
|
|
1545
|
-
runVerdict = 'handoff';
|
|
1546
|
-
return;
|
|
1547
|
-
}
|
|
1548
|
-
runFailure = outcome.reason;
|
|
1549
|
-
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1550
|
-
titleOutcome = 'ended';
|
|
1798
|
+
await endAbortedRun();
|
|
1551
1799
|
return;
|
|
1552
1800
|
}
|
|
1553
|
-
if (event
|
|
1801
|
+
if (isHarnessEvent(event, 'tool-call')) {
|
|
1554
1802
|
this._log?.info('runs', 'tool call', { runId, name: event.name });
|
|
1555
1803
|
await transcript.chunk(toolCallChunk(event));
|
|
1556
1804
|
}
|
|
1557
|
-
else if (event
|
|
1558
|
-
|
|
1559
|
-
this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
|
|
1560
|
-
}
|
|
1561
|
-
else if (event.name !== SKILL_TOOL) {
|
|
1562
|
-
ran = true;
|
|
1563
|
-
}
|
|
1564
|
-
if (WEB_TOOLS.includes(String(event.name))) {
|
|
1565
|
-
await this._citeWebSources(transcript, runId, event.result);
|
|
1566
|
-
}
|
|
1567
|
-
await transcript.chunk(toolResultChunk(event));
|
|
1805
|
+
else if (isHarnessEvent(event, 'tool-result')) {
|
|
1806
|
+
await recordToolResult(event);
|
|
1568
1807
|
}
|
|
1569
|
-
else if (event
|
|
1808
|
+
else if (isHarnessEvent(event, 'tool-activity')) {
|
|
1570
1809
|
void engine
|
|
1571
1810
|
.recordToolActivity({
|
|
1572
1811
|
agentId: runAgent.id,
|
|
@@ -1595,7 +1834,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1595
1834
|
pending: transcript.pending()
|
|
1596
1835
|
});
|
|
1597
1836
|
}
|
|
1598
|
-
const citedTitles = ragInjected
|
|
1837
|
+
const citedTitles = injected.ragInjected
|
|
1838
|
+
? citedByPhraseOverlap(answerText, retrievedRecords)
|
|
1839
|
+
: null;
|
|
1599
1840
|
await transcript.status({
|
|
1600
1841
|
state: 'completed',
|
|
1601
1842
|
metrics: citedTitles ? { ...metrics, citedTitles } : metrics
|
|
@@ -1635,14 +1876,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1635
1876
|
await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
|
|
1636
1877
|
settledWatch.stop();
|
|
1637
1878
|
if (titleOutcome && !diagramRepair) {
|
|
1638
|
-
void this._autoTitle(engine,
|
|
1879
|
+
void this._autoTitle(engine, {
|
|
1880
|
+
chatId,
|
|
1881
|
+
messages,
|
|
1882
|
+
model,
|
|
1883
|
+
agent: runAgent,
|
|
1884
|
+
probe: titleOutcome === 'completed'
|
|
1885
|
+
});
|
|
1639
1886
|
}
|
|
1640
1887
|
}
|
|
1641
1888
|
}
|
|
1642
|
-
async _invokeCapability({ engine, request, agent, capability, controller, meshId }) {
|
|
1889
|
+
async _invokeCapability({ engine, request, agent, capability, controller, meshId, runId }) {
|
|
1643
1890
|
const { chatId } = request;
|
|
1644
|
-
const
|
|
1645
|
-
const deviceId = this._deviceId;
|
|
1891
|
+
const deviceId = this._requireDeviceId();
|
|
1646
1892
|
const harness = this._harnessFor(agent);
|
|
1647
1893
|
const transcript = new RunTranscript(engine, {
|
|
1648
1894
|
chatId,
|
|
@@ -1652,14 +1898,24 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1652
1898
|
log: this._log
|
|
1653
1899
|
});
|
|
1654
1900
|
const inflight = this._runs.get(runId);
|
|
1655
|
-
if (inflight)
|
|
1901
|
+
if (inflight) {
|
|
1656
1902
|
this._runs.set(runId, { ...inflight, executing: true, model: '' });
|
|
1903
|
+
}
|
|
1657
1904
|
this._yieldDownloads();
|
|
1658
1905
|
const executing = await transcript.status({ state: 'executing' });
|
|
1659
1906
|
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(executing.id, runStatus));
|
|
1660
|
-
const settledWatch = this._watchSettled(engine, chatId, runId, controller);
|
|
1907
|
+
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
1661
1908
|
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
1662
1909
|
let metrics;
|
|
1910
|
+
const endAbortedCapability = async () => {
|
|
1911
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1912
|
+
if (outcome.kind === 'replay') {
|
|
1913
|
+
this._seen.delete(runId);
|
|
1914
|
+
}
|
|
1915
|
+
if (outcome.kind === 'failed') {
|
|
1916
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1917
|
+
}
|
|
1918
|
+
};
|
|
1663
1919
|
this._log?.info('runs', 'capability requested', { runId, capability });
|
|
1664
1920
|
try {
|
|
1665
1921
|
if (!harness.invoke) {
|
|
@@ -1671,10 +1927,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1671
1927
|
}
|
|
1672
1928
|
const events = harness.invoke({
|
|
1673
1929
|
capability,
|
|
1674
|
-
args: (request.capabilityArgs
|
|
1930
|
+
args: isRecord(request.capabilityArgs) ? request.capabilityArgs : {},
|
|
1675
1931
|
enhance: request.enhance ?? 'never',
|
|
1676
1932
|
agent,
|
|
1677
|
-
attach: (input) => this._uploadGenerated(meshId, chatId, runId, deviceId
|
|
1933
|
+
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
1678
1934
|
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
1679
1935
|
progress: (update) => modelWatch.image(update),
|
|
1680
1936
|
signal: controller.signal
|
|
@@ -1695,18 +1951,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1695
1951
|
return;
|
|
1696
1952
|
}
|
|
1697
1953
|
if (event.type === 'aborted') {
|
|
1698
|
-
|
|
1699
|
-
if (outcome.kind === 'replay')
|
|
1700
|
-
this._seen.delete(runId);
|
|
1701
|
-
if (outcome.kind === 'failed') {
|
|
1702
|
-
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1703
|
-
}
|
|
1954
|
+
await endAbortedCapability();
|
|
1704
1955
|
return;
|
|
1705
1956
|
}
|
|
1706
|
-
if (event
|
|
1957
|
+
if (isHarnessEvent(event, 'tool-call')) {
|
|
1707
1958
|
await transcript.chunk(toolCallChunk(event));
|
|
1708
1959
|
}
|
|
1709
|
-
else if (event
|
|
1960
|
+
else if (isHarnessEvent(event, 'tool-result')) {
|
|
1710
1961
|
await transcript.chunk(toolResultChunk(event));
|
|
1711
1962
|
}
|
|
1712
1963
|
else {
|
|
@@ -1723,8 +1974,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1723
1974
|
this._seen.delete(runId);
|
|
1724
1975
|
return;
|
|
1725
1976
|
}
|
|
1726
|
-
if (outcome?.kind === 'handoff')
|
|
1977
|
+
if (outcome?.kind === 'handoff') {
|
|
1727
1978
|
return;
|
|
1979
|
+
}
|
|
1728
1980
|
await transcript.status({
|
|
1729
1981
|
state: 'failed',
|
|
1730
1982
|
reason: outcome?.reason ??
|
|
@@ -1747,7 +1999,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1747
1999
|
});
|
|
1748
2000
|
const map = {};
|
|
1749
2001
|
for (const row of configs ?? []) {
|
|
1750
|
-
if (row?.toolName && row.settings
|
|
2002
|
+
if (row?.toolName && isRecord(row.settings)) {
|
|
1751
2003
|
map[row.toolName] = row.settings;
|
|
1752
2004
|
}
|
|
1753
2005
|
}
|
|
@@ -1788,8 +2040,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1788
2040
|
})
|
|
1789
2041
|
.at(-1)?.id;
|
|
1790
2042
|
}
|
|
1791
|
-
if (!attachmentId)
|
|
2043
|
+
if (!attachmentId) {
|
|
1792
2044
|
return null;
|
|
2045
|
+
}
|
|
1793
2046
|
const stream = engine.fileRead({ chatId, fileId: attachmentId, metadataOnly: meta });
|
|
1794
2047
|
const abort = () => stream.destroy();
|
|
1795
2048
|
const pieces = [];
|
|
@@ -1797,19 +2050,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1797
2050
|
stream.on('error', noop);
|
|
1798
2051
|
signal?.addEventListener('abort', abort, { once: true });
|
|
1799
2052
|
try {
|
|
1800
|
-
if (signal?.aborted)
|
|
2053
|
+
if (signal?.aborted) {
|
|
1801
2054
|
throw new AbortedError();
|
|
2055
|
+
}
|
|
1802
2056
|
for await (const frame of stream) {
|
|
1803
2057
|
if (frame.metadata) {
|
|
1804
2058
|
const { id, fileName, mimeType, byteLength } = frame.metadata;
|
|
1805
2059
|
head = { id, fileName, mimeType, byteLength };
|
|
1806
2060
|
}
|
|
1807
|
-
if (frame.data)
|
|
2061
|
+
if (frame.data) {
|
|
1808
2062
|
pieces.push(frame.data);
|
|
2063
|
+
}
|
|
1809
2064
|
}
|
|
1810
|
-
if (signal?.aborted)
|
|
2065
|
+
if (signal?.aborted) {
|
|
1811
2066
|
throw new AbortedError();
|
|
1812
|
-
|
|
2067
|
+
}
|
|
2068
|
+
return head && (meta ? head : { ...head, bytes: b4a.concat(pieces) });
|
|
1813
2069
|
}
|
|
1814
2070
|
finally {
|
|
1815
2071
|
signal?.removeEventListener('abort', abort);
|
|
@@ -1832,29 +2088,30 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1832
2088
|
return null;
|
|
1833
2089
|
}
|
|
1834
2090
|
}
|
|
1835
|
-
async _uploadGenerated(
|
|
2091
|
+
async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId }) {
|
|
1836
2092
|
const duplex = this._engine.uploadAttachment();
|
|
1837
2093
|
duplex.write({ chatId, runId, originDeviceId: deviceId, fileName, mimeType, raw, meshId });
|
|
1838
|
-
const buf =
|
|
1839
|
-
? bytes
|
|
1840
|
-
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
2094
|
+
const buf = b4a.toBuffer(bytes);
|
|
1841
2095
|
for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
|
|
1842
2096
|
duplex.write({ data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
|
|
1843
2097
|
}
|
|
1844
2098
|
duplex.end();
|
|
1845
2099
|
let attachment = null;
|
|
1846
2100
|
for await (const frame of duplex) {
|
|
1847
|
-
if (frame.attachment)
|
|
2101
|
+
if (frame.attachment) {
|
|
1848
2102
|
attachment = frame.attachment;
|
|
2103
|
+
}
|
|
1849
2104
|
}
|
|
1850
|
-
if (!attachment)
|
|
2105
|
+
if (!attachment) {
|
|
1851
2106
|
throw new Error('attachment upload ended without a record');
|
|
2107
|
+
}
|
|
1852
2108
|
return attachment;
|
|
1853
2109
|
}
|
|
1854
2110
|
async _hydrateAudio(messages, signal) {
|
|
1855
2111
|
const turns = messages.filter((turn) => turn.audioAttachments?.length);
|
|
1856
|
-
if (!turns.length)
|
|
2112
|
+
if (!turns.length) {
|
|
1857
2113
|
return;
|
|
2114
|
+
}
|
|
1858
2115
|
for (const turn of turns) {
|
|
1859
2116
|
for (const ref of turn.audioAttachments ?? []) {
|
|
1860
2117
|
if (signal.aborted)
|
|
@@ -1884,15 +2141,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1884
2141
|
const model = mediaModelNameFor('transcription', await this._persistedProfile());
|
|
1885
2142
|
await this.ensureMediaModelUnpaused('transcription', model);
|
|
1886
2143
|
await this._downloads?.waitUntilCached(model, signal);
|
|
1887
|
-
const text = (await this._harness.transcribe({ model: '', audio: bytes, signal })).trim();
|
|
2144
|
+
const text = (await this._harness.transcribe({ model: '', audio: b4a.toBuffer(bytes), signal })).trim();
|
|
1888
2145
|
this._audioTranscripts.set(ref.blobId, text);
|
|
1889
|
-
if (!text)
|
|
2146
|
+
if (!text) {
|
|
1890
2147
|
return silentAudioNotice(ref.fileName);
|
|
2148
|
+
}
|
|
1891
2149
|
return audioTranscriptBlock(ref.fileName, text);
|
|
1892
2150
|
}
|
|
1893
2151
|
catch (err) {
|
|
1894
|
-
if (signal.aborted)
|
|
2152
|
+
if (signal.aborted) {
|
|
1895
2153
|
throw err;
|
|
2154
|
+
}
|
|
1896
2155
|
this._log?.warn('assistant', `audio transcription failed — ${ref.fileName}`, err);
|
|
1897
2156
|
return unheardAudioNotice(label, 'transcription failed on this device');
|
|
1898
2157
|
}
|
|
@@ -1900,57 +2159,177 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1900
2159
|
async _hydrateImages(messages, signal, runId) {
|
|
1901
2160
|
const unreadable = new Map();
|
|
1902
2161
|
const turns = messages.filter((turn) => turn.images?.length);
|
|
1903
|
-
if (!turns.length)
|
|
2162
|
+
if (!turns.length) {
|
|
1904
2163
|
return unreadable;
|
|
2164
|
+
}
|
|
1905
2165
|
const inUse = new Set(turns.flatMap((turn) => (turn.images ?? []).map(imageCacheName)));
|
|
1906
2166
|
this._runImageCacheNames.set(runId, inUse);
|
|
1907
2167
|
const dir = this._imageCacheDir;
|
|
1908
2168
|
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
1909
2169
|
for (const turn of turns) {
|
|
1910
|
-
const dropped =
|
|
1911
|
-
|
|
1912
|
-
try {
|
|
1913
|
-
const file = await this._hydrateImage(dir, image, signal);
|
|
1914
|
-
turn.attachments = [...(turn.attachments ?? []), { path: file }];
|
|
1915
|
-
}
|
|
1916
|
-
catch (err) {
|
|
1917
|
-
if (signal.aborted)
|
|
1918
|
-
throw err;
|
|
1919
|
-
this._log?.error('assistant', `image hydration failed — skipped ${image.fileName}`, err);
|
|
1920
|
-
dropped.push(image.fileName);
|
|
1921
|
-
}
|
|
1922
|
-
}
|
|
1923
|
-
if (dropped.length)
|
|
2170
|
+
const dropped = await this._hydrateTurnImages(turn, { dir, signal });
|
|
2171
|
+
if (dropped.length) {
|
|
1924
2172
|
unreadable.set(turn, dropped);
|
|
2173
|
+
}
|
|
1925
2174
|
delete turn.images;
|
|
1926
2175
|
}
|
|
1927
2176
|
return unreadable;
|
|
1928
2177
|
}
|
|
2178
|
+
async _hydrateTurnImages(turn, { dir, signal }) {
|
|
2179
|
+
const dropped = [];
|
|
2180
|
+
for (const image of turn.images ?? []) {
|
|
2181
|
+
try {
|
|
2182
|
+
const file = await this._hydrateImage(dir, image, signal);
|
|
2183
|
+
turn.attachments = [...(turn.attachments ?? []), { path: file }];
|
|
2184
|
+
}
|
|
2185
|
+
catch (err) {
|
|
2186
|
+
if (signal.aborted) {
|
|
2187
|
+
throw err;
|
|
2188
|
+
}
|
|
2189
|
+
this._log?.error('assistant', `image hydration failed — skipped ${image.fileName}`, err);
|
|
2190
|
+
dropped.push(image.fileName);
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
return dropped;
|
|
2194
|
+
}
|
|
1929
2195
|
async _hydrateImage(dir, image, signal) {
|
|
1930
2196
|
const file = path.join(dir, imageCacheName(image));
|
|
1931
|
-
|
|
1932
|
-
if (!cached?.size) {
|
|
2197
|
+
await cacheOrTouch(file, async () => {
|
|
1933
2198
|
const raw = await this._readBlob(image.url, signal);
|
|
1934
|
-
if (signal.aborted)
|
|
2199
|
+
if (signal.aborted) {
|
|
1935
2200
|
throw new Error('hydration aborted');
|
|
1936
|
-
|
|
2201
|
+
}
|
|
2202
|
+
if (!raw) {
|
|
1937
2203
|
throw new Error(`empty download for ${image.fileName}`);
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
2204
|
+
}
|
|
2205
|
+
return normalizeImageForModel(raw);
|
|
2206
|
+
});
|
|
2207
|
+
return file;
|
|
2208
|
+
}
|
|
2209
|
+
async _hydratePdfPages(messages, signal, runId) {
|
|
2210
|
+
const rendered = new Set();
|
|
2211
|
+
const turns = messages.filter((turn) => turn.pdfs?.length);
|
|
2212
|
+
if (!turns.length) {
|
|
2213
|
+
return rendered;
|
|
1942
2214
|
}
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
2215
|
+
const dir = this._imageCacheDir;
|
|
2216
|
+
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
2217
|
+
const pinned = this._runImageCacheNames.get(runId) ?? new Set();
|
|
2218
|
+
this._runImageCacheNames.set(runId, pinned);
|
|
2219
|
+
const latestUser = messages.findLast((turn) => turn.role === 'user');
|
|
2220
|
+
for (const turn of turns) {
|
|
2221
|
+
let budget = MAX_PDF_PAGE_IMAGES;
|
|
2222
|
+
for (const pdf of turn.pdfs ?? []) {
|
|
2223
|
+
if (budget <= 0)
|
|
2224
|
+
break;
|
|
2225
|
+
const pages = await this._tryHydratePdf(dir, turn, {
|
|
2226
|
+
pdf,
|
|
2227
|
+
pinned,
|
|
2228
|
+
maxPages: budget,
|
|
2229
|
+
signal
|
|
2230
|
+
});
|
|
2231
|
+
budget -= pages.tried;
|
|
2232
|
+
if (turn === latestUser && pages.names.length) {
|
|
2233
|
+
rendered.add(pdf.id);
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
delete turn.pdfs;
|
|
2237
|
+
}
|
|
2238
|
+
return rendered;
|
|
2239
|
+
}
|
|
2240
|
+
async _tryHydratePdf(dir, turn, opts) {
|
|
2241
|
+
try {
|
|
2242
|
+
return await this._hydratePdf(dir, turn, opts);
|
|
2243
|
+
}
|
|
2244
|
+
catch (err) {
|
|
2245
|
+
if (opts.signal.aborted) {
|
|
2246
|
+
throw err;
|
|
2247
|
+
}
|
|
2248
|
+
this._log?.warn('assistant', `pdf rasterization failed — ${opts.pdf.fileName}`, err);
|
|
2249
|
+
return { names: [], tried: 0 };
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2252
|
+
async _hydratePdf(dir, turn, { pdf, pinned, maxPages, signal }) {
|
|
2253
|
+
const key = `${pdf.blobId}:${maxPages}`;
|
|
2254
|
+
const memo = this._pdfPages.get(key);
|
|
2255
|
+
if (memo) {
|
|
2256
|
+
for (const name of memo.names) {
|
|
2257
|
+
pinned.add(name);
|
|
2258
|
+
}
|
|
2259
|
+
const files = memo.names.map((name) => path.join(dir, name));
|
|
2260
|
+
if ((await Promise.all(files.map((file) => cacheOrTouch(file, noBytes)))).every(Boolean)) {
|
|
2261
|
+
attachPages(turn, files);
|
|
2262
|
+
return memo;
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
const pages = await this._renderPdf(dir, { pdf, pinned, maxPages, signal });
|
|
2266
|
+
if (!pages) {
|
|
2267
|
+
return { names: [], tried: 0 };
|
|
2268
|
+
}
|
|
2269
|
+
this._pdfPages.set(key, pages);
|
|
2270
|
+
attachPages(turn, pages.names.map((name) => path.join(dir, name)));
|
|
2271
|
+
return pages;
|
|
2272
|
+
}
|
|
2273
|
+
async _renderPdf(dir, { pdf, pinned, maxPages, signal }) {
|
|
2274
|
+
const bytes = await this._readBlob(pdf.url, signal);
|
|
2275
|
+
if (signal.aborted) {
|
|
2276
|
+
throw new Error('hydration aborted');
|
|
2277
|
+
}
|
|
2278
|
+
if (!bytes) {
|
|
2279
|
+
return null;
|
|
2280
|
+
}
|
|
2281
|
+
const doc = await this._pdfRasterizer.open(bytes);
|
|
2282
|
+
if (!doc) {
|
|
2283
|
+
return { names: [], tried: 0 };
|
|
2284
|
+
}
|
|
2285
|
+
try {
|
|
2286
|
+
const names = [];
|
|
2287
|
+
let tried = 0;
|
|
2288
|
+
const count = doc.pageCount();
|
|
2289
|
+
for (let page = 0; page < count && tried < maxPages; page++) {
|
|
2290
|
+
if (signal.aborted)
|
|
2291
|
+
throw new Error('hydration aborted');
|
|
2292
|
+
const outcome = await this._renderPdfPage(doc, { dir, pdf, page, pinned, signal });
|
|
2293
|
+
if (outcome.tried) {
|
|
2294
|
+
tried++;
|
|
2295
|
+
}
|
|
2296
|
+
if (outcome.name) {
|
|
2297
|
+
names.push(outcome.name);
|
|
2298
|
+
}
|
|
2299
|
+
}
|
|
2300
|
+
return { names, tried };
|
|
2301
|
+
}
|
|
2302
|
+
finally {
|
|
2303
|
+
doc.close();
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
async _renderPdfPage(doc, { dir, pdf, page, pinned, signal }) {
|
|
2307
|
+
let tried = false;
|
|
2308
|
+
try {
|
|
2309
|
+
const flags = doc.pageFlags(page);
|
|
2310
|
+
if (flags.hasText && !flags.hasImage) {
|
|
2311
|
+
return { tried };
|
|
2312
|
+
}
|
|
2313
|
+
tried = true;
|
|
2314
|
+
const name = pdfPageCacheName(pdf, page);
|
|
2315
|
+
pinned.add(name);
|
|
2316
|
+
const cached = await cacheOrTouch(path.join(dir, name), () => doc.renderPage(page));
|
|
2317
|
+
return { tried, name: cached ? name : undefined };
|
|
2318
|
+
}
|
|
2319
|
+
catch (err) {
|
|
2320
|
+
if (signal.aborted) {
|
|
2321
|
+
throw err;
|
|
2322
|
+
}
|
|
2323
|
+
this._log?.warn('assistant', `pdf page skipped — ${pdf.fileName} p${page}`, err);
|
|
2324
|
+
return { tried };
|
|
1946
2325
|
}
|
|
1947
|
-
return file;
|
|
1948
2326
|
}
|
|
1949
2327
|
async _compactIfNearFull(params) {
|
|
1950
2328
|
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
1951
2329
|
const metrics = await this._lastRunMetrics(chatId, meshId);
|
|
1952
|
-
if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize))
|
|
2330
|
+
if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize)) {
|
|
1953
2331
|
return false;
|
|
2332
|
+
}
|
|
1954
2333
|
if (!canCompact(metrics.ctxSize)) {
|
|
1955
2334
|
this._log?.debug('runs', 'chat near its window but under the compaction floor', {
|
|
1956
2335
|
chatId,
|
|
@@ -2007,7 +2386,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2007
2386
|
}
|
|
2008
2387
|
}
|
|
2009
2388
|
async _lastRunMetrics(chatId, meshId) {
|
|
2010
|
-
const deviceId = this.
|
|
2389
|
+
const deviceId = this._requireDeviceId();
|
|
2011
2390
|
const { chunks } = await meshScoped(this._engine, meshId)
|
|
2012
2391
|
.listChunks({ chatId, type: 'run-status' })
|
|
2013
2392
|
.catch(() => ({ chunks: [] }));
|
|
@@ -2015,12 +2394,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2015
2394
|
for (let index = statuses.length - 1; index >= 0; index--) {
|
|
2016
2395
|
const chunk = statuses[index];
|
|
2017
2396
|
const metrics = chunk.runStatus?.metrics;
|
|
2018
|
-
if (chunk.runStatus?.state !== 'completed' || !metrics)
|
|
2397
|
+
if (chunk.runStatus?.state !== 'completed' || !metrics) {
|
|
2019
2398
|
continue;
|
|
2020
|
-
|
|
2399
|
+
}
|
|
2400
|
+
if (!chunk.originDeviceId || !b4a.equals(chunk.originDeviceId, deviceId)) {
|
|
2401
|
+
return {};
|
|
2402
|
+
}
|
|
2403
|
+
const parsed = RunOccupancySchema.safeParse(metrics);
|
|
2404
|
+
if (!parsed.success) {
|
|
2021
2405
|
return {};
|
|
2022
|
-
|
|
2023
|
-
return { cacheTokens: parsed.cacheTokens, ctxSize: parsed.ctxSize };
|
|
2406
|
+
}
|
|
2407
|
+
return { cacheTokens: parsed.data.cacheTokens, ctxSize: parsed.data.ctxSize };
|
|
2024
2408
|
}
|
|
2025
2409
|
return {};
|
|
2026
2410
|
}
|
|
@@ -2028,12 +2412,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2028
2412
|
if (!this.opened)
|
|
2029
2413
|
await this.ready();
|
|
2030
2414
|
const engine = meshScoped(this._engine, options.meshId);
|
|
2031
|
-
const deviceId = this.
|
|
2415
|
+
const deviceId = this._requireDeviceId();
|
|
2032
2416
|
const chat = await engine.getChat({ chatId }).catch(noop);
|
|
2033
|
-
if (!chat)
|
|
2417
|
+
if (!chat) {
|
|
2034
2418
|
return { compacted: false, reason: 'unknown chat' };
|
|
2419
|
+
}
|
|
2035
2420
|
const agent = await engine.getAgent({ agentId: chat.agentId }).catch(noop);
|
|
2036
|
-
if (!agent || !agent.providerDeviceId
|
|
2421
|
+
if (!agent || !agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, deviceId)) {
|
|
2037
2422
|
return { compacted: false, reason: 'not the provider device' };
|
|
2038
2423
|
}
|
|
2039
2424
|
const model = runnableModelName(agent.modelName, await this._ensureCapability(), this._mobileClass);
|
|
@@ -2056,20 +2441,27 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2056
2441
|
const senders = await this._senderNames(engine, chunks);
|
|
2057
2442
|
for (;;) {
|
|
2058
2443
|
const boundary = compactionBoundary(chunks, keepTurns);
|
|
2059
|
-
if (!boundary)
|
|
2444
|
+
if (!boundary) {
|
|
2060
2445
|
return { compacted: false, reason: 'nothing-to-fold' };
|
|
2446
|
+
}
|
|
2061
2447
|
const through = chunks.find((chunk) => chunk.id === boundary.throughId);
|
|
2062
|
-
if (!through)
|
|
2448
|
+
if (!through) {
|
|
2063
2449
|
return { compacted: false, reason: 'nothing-to-fold' };
|
|
2064
|
-
|
|
2450
|
+
}
|
|
2451
|
+
if (through.seq <= lastThroughSeq) {
|
|
2065
2452
|
return { compacted: false, reason: 'already-compacted' };
|
|
2453
|
+
}
|
|
2066
2454
|
const inRange = chunks.filter((chunk) => chunk.seq <= through.seq);
|
|
2067
2455
|
const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), senders);
|
|
2068
2456
|
if (!announced) {
|
|
2069
2457
|
await options.onFold?.();
|
|
2070
2458
|
announced = true;
|
|
2071
2459
|
}
|
|
2072
|
-
const attempt = await this._summarize(folded,
|
|
2460
|
+
const attempt = await this._summarize(folded, {
|
|
2461
|
+
model,
|
|
2462
|
+
agent: withConfigDefaults(agent, config),
|
|
2463
|
+
signal: options.signal
|
|
2464
|
+
});
|
|
2073
2465
|
if (!attempt.summary) {
|
|
2074
2466
|
if (attempt.overflowed && boundary.foldedTurns > 1) {
|
|
2075
2467
|
keepTurns = userTurns - Math.floor(boundary.foldedTurns / 2);
|
|
@@ -2081,8 +2473,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2081
2473
|
if (!foldReducesPrompt(folded, summary)) {
|
|
2082
2474
|
return { compacted: false, reason: 'summary-not-smaller' };
|
|
2083
2475
|
}
|
|
2084
|
-
if (options.signal?.aborted)
|
|
2476
|
+
if (options.signal?.aborted) {
|
|
2085
2477
|
return { compacted: false, reason: 'canceled' };
|
|
2478
|
+
}
|
|
2086
2479
|
await engine.appendChunk({
|
|
2087
2480
|
chatId,
|
|
2088
2481
|
type: 'compaction',
|
|
@@ -2098,7 +2491,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2098
2491
|
return { compacted: true, foldedTurns: boundary.foldedTurns };
|
|
2099
2492
|
}
|
|
2100
2493
|
}
|
|
2101
|
-
async _summarize(turns, model, agent, signal) {
|
|
2494
|
+
async _summarize(turns, { model, agent, signal }) {
|
|
2102
2495
|
const hasPriorSummary = turns[0]?.content.startsWith(COMPACTION_TURN_PREFIX) === true;
|
|
2103
2496
|
const events = this._harnessFor(agent).completion({
|
|
2104
2497
|
model,
|
|
@@ -2121,8 +2514,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2121
2514
|
let text = '';
|
|
2122
2515
|
let failed = null;
|
|
2123
2516
|
for await (const event of events) {
|
|
2124
|
-
if (event.type === 'content' && typeof event.text === 'string')
|
|
2517
|
+
if (event.type === 'content' && typeof event.text === 'string') {
|
|
2125
2518
|
text += event.text;
|
|
2519
|
+
}
|
|
2126
2520
|
else if (event.type === 'error') {
|
|
2127
2521
|
failed = typeof event.message === 'string' ? event.message : 'summarizer failed';
|
|
2128
2522
|
break;
|
|
@@ -2131,8 +2525,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2131
2525
|
failed = 'summarizer aborted';
|
|
2132
2526
|
break;
|
|
2133
2527
|
}
|
|
2134
|
-
else if (event.type === 'done')
|
|
2528
|
+
else if (event.type === 'done') {
|
|
2135
2529
|
break;
|
|
2530
|
+
}
|
|
2136
2531
|
}
|
|
2137
2532
|
if (failed !== null) {
|
|
2138
2533
|
this._log?.warn('runs', 'summarizer failed mid-stream', { chars: text.length, failed });
|
|
@@ -2140,14 +2535,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2140
2535
|
}
|
|
2141
2536
|
return { summary: parseCompactionSummary(text), overflowed: false };
|
|
2142
2537
|
}
|
|
2143
|
-
async _autoTitle(engine, chatId, messages, model, agent, probe) {
|
|
2538
|
+
async _autoTitle(engine, { chatId, messages, model, agent, probe }) {
|
|
2144
2539
|
try {
|
|
2145
2540
|
const first = messages.find((turn) => turn.role === 'user' && turn.content.trim());
|
|
2146
|
-
if (!first)
|
|
2541
|
+
if (!first) {
|
|
2147
2542
|
return;
|
|
2543
|
+
}
|
|
2148
2544
|
const chat = await engine.getChat({ chatId });
|
|
2149
|
-
if (!isDefaultChatTitle(chat?.title))
|
|
2545
|
+
if (!isDefaultChatTitle(chat?.title)) {
|
|
2150
2546
|
return;
|
|
2547
|
+
}
|
|
2151
2548
|
await this._generateChatTitle({ engine, chatId, input: first.content, model, agent, probe });
|
|
2152
2549
|
}
|
|
2153
2550
|
catch (err) {
|
|
@@ -2157,8 +2554,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2157
2554
|
async _generateChatTitle(params) {
|
|
2158
2555
|
const { engine, chatId, input, model, agent, probe } = params;
|
|
2159
2556
|
const prompt = input.trim();
|
|
2160
|
-
if (!prompt)
|
|
2557
|
+
if (!prompt) {
|
|
2161
2558
|
return null;
|
|
2559
|
+
}
|
|
2162
2560
|
let generated = null;
|
|
2163
2561
|
if (probe && !this.closing) {
|
|
2164
2562
|
try {
|
|
@@ -2175,8 +2573,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2175
2573
|
}
|
|
2176
2574
|
}
|
|
2177
2575
|
const title = generated ?? deriveChatTitle(prompt);
|
|
2178
|
-
if (!title)
|
|
2576
|
+
if (!title) {
|
|
2179
2577
|
return null;
|
|
2578
|
+
}
|
|
2180
2579
|
await engine.renameChat({ chatId, config: { title } });
|
|
2181
2580
|
this._log?.info('runs', 'chat titled', {
|
|
2182
2581
|
chatId,
|
|
@@ -2215,19 +2614,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2215
2614
|
});
|
|
2216
2615
|
let text = '';
|
|
2217
2616
|
for await (const event of events) {
|
|
2218
|
-
if (event.type === 'content' && typeof event.text === 'string')
|
|
2617
|
+
if (event.type === 'content' && typeof event.text === 'string') {
|
|
2219
2618
|
text += event.text;
|
|
2619
|
+
}
|
|
2220
2620
|
else if (event.type === 'inference-call') {
|
|
2221
2621
|
void this._inferenceCalls
|
|
2222
2622
|
.record(chatId, { runId: TITLE_RUN_ID, at: Date.now(), call: asRecord(event.call) })
|
|
2223
2623
|
.catch((err) => this._log?.warn('assistant', 'title call not recorded', { err }));
|
|
2224
2624
|
}
|
|
2225
|
-
else if (event.type === 'error' || event.type === 'aborted' || event.type === 'done')
|
|
2625
|
+
else if (event.type === 'error' || event.type === 'aborted' || event.type === 'done') {
|
|
2226
2626
|
break;
|
|
2627
|
+
}
|
|
2227
2628
|
}
|
|
2228
2629
|
return parseTitleResponse(text);
|
|
2229
2630
|
}
|
|
2230
|
-
_createApprove(engine, chatId, runId, deviceId, agentId, hasRun) {
|
|
2631
|
+
_createApprove(engine, { chatId, runId, deviceId, agentId, hasRun }) {
|
|
2231
2632
|
const waiters = new Map();
|
|
2232
2633
|
let latestChunks = [];
|
|
2233
2634
|
let watcher = null;
|
|
@@ -2235,16 +2636,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2235
2636
|
const pump = async (source) => {
|
|
2236
2637
|
while (true) {
|
|
2237
2638
|
const { value, done } = await source.next();
|
|
2238
|
-
if (done)
|
|
2639
|
+
if (done) {
|
|
2239
2640
|
return;
|
|
2641
|
+
}
|
|
2240
2642
|
latestChunks = value.chunks ?? [];
|
|
2241
2643
|
for (const chunk of latestChunks) {
|
|
2242
2644
|
const status = chunk.approvalRequest?.status;
|
|
2243
|
-
if (!status || status === 'pending')
|
|
2645
|
+
if (!status || status === 'pending') {
|
|
2244
2646
|
continue;
|
|
2647
|
+
}
|
|
2245
2648
|
const waiter = waiters.get(chunk.id);
|
|
2246
|
-
if (!waiter)
|
|
2649
|
+
if (!waiter) {
|
|
2247
2650
|
continue;
|
|
2651
|
+
}
|
|
2248
2652
|
waiters.delete(chunk.id);
|
|
2249
2653
|
this._log?.info('approvals', `answer ${status}`, {
|
|
2250
2654
|
runId,
|
|
@@ -2264,10 +2668,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2264
2668
|
watcher = opened;
|
|
2265
2669
|
pump(opened)
|
|
2266
2670
|
.catch((err) => {
|
|
2267
|
-
if (closing)
|
|
2671
|
+
if (closing) {
|
|
2268
2672
|
this._log?.debug('assistant', 'approval watch closed', err);
|
|
2269
|
-
|
|
2673
|
+
}
|
|
2674
|
+
else {
|
|
2270
2675
|
this._log?.error('assistant', 'approval watch failed', err);
|
|
2676
|
+
}
|
|
2271
2677
|
})
|
|
2272
2678
|
.finally(() => {
|
|
2273
2679
|
for (const [id, waiter] of waiters) {
|
|
@@ -2278,8 +2684,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2278
2684
|
});
|
|
2279
2685
|
waiter.resolve(false);
|
|
2280
2686
|
}
|
|
2281
|
-
if (watcher === opened)
|
|
2687
|
+
if (watcher === opened) {
|
|
2282
2688
|
watcher = null;
|
|
2689
|
+
}
|
|
2283
2690
|
});
|
|
2284
2691
|
};
|
|
2285
2692
|
const asksForRun = async () => {
|
|
@@ -2290,8 +2697,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2290
2697
|
};
|
|
2291
2698
|
const sameCall = (chunk, call) => {
|
|
2292
2699
|
const ask = chunk.approvalRequest;
|
|
2293
|
-
if (!ask || ask.name !== call.name)
|
|
2700
|
+
if (!ask || ask.name !== call.name) {
|
|
2294
2701
|
return false;
|
|
2702
|
+
}
|
|
2295
2703
|
return ((ask.resource?.kind ?? null) === (call.resource?.kind ?? null) &&
|
|
2296
2704
|
(ask.resource?.label ?? null) === (call.resource?.label ?? null) &&
|
|
2297
2705
|
JSON.stringify(ask.arguments) === JSON.stringify(call.arguments));
|
|
@@ -2391,8 +2799,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2391
2799
|
});
|
|
2392
2800
|
await engine.resolveApproval({ chunkId: ask.id, approved: false });
|
|
2393
2801
|
}
|
|
2394
|
-
for (const waiter of waiters.values())
|
|
2802
|
+
for (const waiter of waiters.values()) {
|
|
2395
2803
|
waiter.resolve(false);
|
|
2804
|
+
}
|
|
2396
2805
|
}
|
|
2397
2806
|
waiters.clear();
|
|
2398
2807
|
closing = true;
|
|
@@ -2406,22 +2815,25 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2406
2815
|
if (reason === RUNNER_DIED && this._harnessDeath) {
|
|
2407
2816
|
return { kind: 'failed', reason: this._harnessDeath };
|
|
2408
2817
|
}
|
|
2409
|
-
if (known)
|
|
2818
|
+
if (known) {
|
|
2410
2819
|
return known;
|
|
2411
|
-
|
|
2820
|
+
}
|
|
2821
|
+
if (this.suspended) {
|
|
2412
2822
|
return { kind: 'replay' };
|
|
2823
|
+
}
|
|
2413
2824
|
if (reason !== undefined) {
|
|
2414
2825
|
this._log?.warn('runs', 'run aborted for an unrecognised reason', { runId, reason });
|
|
2415
2826
|
}
|
|
2416
2827
|
return { kind: 'failed', reason: RUN_FAILURE_REASONS.runtimeEndedTurn };
|
|
2417
2828
|
}
|
|
2418
|
-
_watchSettled(engine, chatId, runId, controller) {
|
|
2829
|
+
_watchSettled(engine, { chatId, runId, controller }) {
|
|
2419
2830
|
const it = engine.chunksWatch({ chatId, type: 'run-status' })[Symbol.asyncIterator]();
|
|
2420
2831
|
const watching = (async () => {
|
|
2421
2832
|
while (true) {
|
|
2422
2833
|
const { value, done } = await it.next();
|
|
2423
|
-
if (done)
|
|
2834
|
+
if (done) {
|
|
2424
2835
|
return;
|
|
2836
|
+
}
|
|
2425
2837
|
const settled = (value.chunks ?? []).some((c) => c.runId === runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
2426
2838
|
if (settled) {
|
|
2427
2839
|
controller.abort(RUN_SETTLED);
|
|
@@ -2441,15 +2853,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2441
2853
|
};
|
|
2442
2854
|
}
|
|
2443
2855
|
_watchOwnership(engine, agentId, controller) {
|
|
2444
|
-
const deviceId = this.
|
|
2856
|
+
const deviceId = this._requireDeviceId();
|
|
2445
2857
|
const it = engine.agentsWatch({})[Symbol.asyncIterator]();
|
|
2446
2858
|
const watching = (async () => {
|
|
2447
2859
|
while (true) {
|
|
2448
2860
|
const { value, done } = await it.next();
|
|
2449
|
-
if (done)
|
|
2861
|
+
if (done) {
|
|
2450
2862
|
return;
|
|
2863
|
+
}
|
|
2451
2864
|
const row = (value.agents ?? []).find((a) => a.id === agentId);
|
|
2452
|
-
if (!row ||
|
|
2865
|
+
if (!row ||
|
|
2866
|
+
row.deletedAt ||
|
|
2867
|
+
!row.providerDeviceId ||
|
|
2868
|
+
!b4a.equals(row.providerDeviceId, deviceId)) {
|
|
2453
2869
|
controller.abort(OWNERSHIP_LOST);
|
|
2454
2870
|
return;
|
|
2455
2871
|
}
|
|
@@ -2474,11 +2890,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2474
2890
|
return;
|
|
2475
2891
|
if (!('status' in state) || typeof state.status !== 'string')
|
|
2476
2892
|
return;
|
|
2477
|
-
if (!('model' in state) || typeof state.model !== 'string')
|
|
2893
|
+
if (!('model' in state) || typeof state.model !== 'string') {
|
|
2478
2894
|
return;
|
|
2895
|
+
}
|
|
2479
2896
|
const { status, model } = state;
|
|
2480
|
-
if (status === 'downloading' || this._modelStatusLogged.get(model) === status)
|
|
2897
|
+
if (status === 'downloading' || this._modelStatusLogged.get(model) === status) {
|
|
2481
2898
|
return;
|
|
2899
|
+
}
|
|
2482
2900
|
this._modelStatusLogged.set(model, status);
|
|
2483
2901
|
this._log?.info('models', `model ${status}`, { model });
|
|
2484
2902
|
}
|
|
@@ -2500,11 +2918,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2500
2918
|
const runStatus = progress
|
|
2501
2919
|
? { state: 'executing', progress }
|
|
2502
2920
|
: { state: 'executing' };
|
|
2503
|
-
if (documents)
|
|
2921
|
+
if (documents) {
|
|
2504
2922
|
runStatus.reason = 'preparing-documents';
|
|
2923
|
+
}
|
|
2505
2924
|
const key = JSON.stringify(runStatus);
|
|
2506
|
-
if (key === last)
|
|
2925
|
+
if (key === last) {
|
|
2507
2926
|
return;
|
|
2927
|
+
}
|
|
2508
2928
|
last = key;
|
|
2509
2929
|
showing = progress !== null || documents;
|
|
2510
2930
|
update(runStatus).catch(noop);
|
|
@@ -2512,17 +2932,20 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2512
2932
|
const onModel = (state) => {
|
|
2513
2933
|
if (sampling)
|
|
2514
2934
|
return;
|
|
2515
|
-
if (state.status === 'loading')
|
|
2935
|
+
if (state.status === 'loading') {
|
|
2516
2936
|
apply({ phase: 'loading', model: state.model });
|
|
2937
|
+
}
|
|
2517
2938
|
else if (state.status === 'downloading') {
|
|
2518
2939
|
const percentage = Math.floor((state.percentage ?? 0) / 10) * 10;
|
|
2519
2940
|
apply({ phase: 'downloading', model: state.model, percentage });
|
|
2520
2941
|
}
|
|
2521
|
-
else
|
|
2942
|
+
else {
|
|
2522
2943
|
apply(transcribing ? { phase: 'transcribing' } : null);
|
|
2944
|
+
}
|
|
2523
2945
|
};
|
|
2524
|
-
if (this._harness.on && this._harness.off)
|
|
2946
|
+
if (this._harness.on && this._harness.off) {
|
|
2525
2947
|
this._harness.on('model', onModel);
|
|
2948
|
+
}
|
|
2526
2949
|
return {
|
|
2527
2950
|
documents: (active) => {
|
|
2528
2951
|
documents = active;
|
|
@@ -2534,15 +2957,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2534
2957
|
},
|
|
2535
2958
|
image: (progress) => {
|
|
2536
2959
|
sampling = progress !== null;
|
|
2537
|
-
if (!progress)
|
|
2960
|
+
if (!progress) {
|
|
2538
2961
|
return apply(null);
|
|
2962
|
+
}
|
|
2539
2963
|
apply({ phase: 'generating', percentage: Math.floor(progress.percentage / 10) * 10 });
|
|
2540
2964
|
},
|
|
2541
2965
|
stop: () => this._harness.off?.('model', onModel),
|
|
2542
2966
|
clear: () => {
|
|
2543
2967
|
sampling = false;
|
|
2544
|
-
if (!showing)
|
|
2968
|
+
if (!showing) {
|
|
2545
2969
|
return;
|
|
2970
|
+
}
|
|
2546
2971
|
showing = false;
|
|
2547
2972
|
last = JSON.stringify({ state: 'executing' });
|
|
2548
2973
|
update({ state: 'executing' }).catch(noop);
|
|
@@ -2550,6 +2975,37 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2550
2975
|
};
|
|
2551
2976
|
}
|
|
2552
2977
|
}
|
|
2978
|
+
async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, injected }) {
|
|
2979
|
+
const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
|
|
2980
|
+
const notice = chooseRagNotice({
|
|
2981
|
+
errorMessage: knowledge.search.errorMessage,
|
|
2982
|
+
hitCount: records.length,
|
|
2983
|
+
searchedCount: knowledge.search.searchedCount ?? 0,
|
|
2984
|
+
indexEmpty: Boolean(knowledge.search.indexEmpty),
|
|
2985
|
+
hasKnowledge: Boolean(knowledge.search.hasKnowledge),
|
|
2986
|
+
pendingReferenced: Boolean(knowledge.search.pendingReferenced),
|
|
2987
|
+
failedFiles: knowledge.search.failedFiles ?? [],
|
|
2988
|
+
pendingFiles: knowledge.search.pendingFiles ?? []
|
|
2989
|
+
});
|
|
2990
|
+
sourceTagFilter.add(records.map((record) => record.title));
|
|
2991
|
+
retrievedRecords.push(...records.map((record, index) => ({
|
|
2992
|
+
...record,
|
|
2993
|
+
excerpt: knowledge.hits[index]?.content ?? record.excerpt
|
|
2994
|
+
})));
|
|
2995
|
+
await transcript.chunk({
|
|
2996
|
+
type: 'sources',
|
|
2997
|
+
sources: { sources: records, notice: notice ?? undefined }
|
|
2998
|
+
});
|
|
2999
|
+
injected.sourcesWritten = true;
|
|
3000
|
+
if (records.length > 0) {
|
|
3001
|
+
knowledge.lastUser.content = `${formatRagContext(knowledge.hits, knowledge.search.availableFiles ?? [])}\n\nThe user's message:\n${knowledge.lastUser.content}\n\n${RAG_CITATION_LINE}`;
|
|
3002
|
+
injected.ragInjected = true;
|
|
3003
|
+
injected.documentsInjected = true;
|
|
3004
|
+
}
|
|
3005
|
+
else if (knowledge.search.availableFiles?.length) {
|
|
3006
|
+
knowledge.lastUser.content += `\n\n[Files indexed and available now: ${knowledge.search.availableFiles.join(', ')}. No relevant excerpts were retrieved for this question. Their current availability supersedes earlier indexing notices.]`;
|
|
3007
|
+
}
|
|
3008
|
+
}
|
|
2553
3009
|
function noop() { }
|
|
2554
3010
|
function toolCallChunk(event) {
|
|
2555
3011
|
return {
|
|
@@ -2580,8 +3036,8 @@ function runMetricsSummary(metrics) {
|
|
|
2580
3036
|
};
|
|
2581
3037
|
}
|
|
2582
3038
|
function asRecord(value) {
|
|
2583
|
-
return
|
|
3039
|
+
return isRecord(value) ? { ...value } : {};
|
|
2584
3040
|
}
|
|
2585
3041
|
function isStreamDestroyed(err) {
|
|
2586
|
-
return
|
|
3042
|
+
return isRecord(err) && err.code === 'STREAM_DESTROYED';
|
|
2587
3043
|
}
|