@qvac/core 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +75 -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 +30 -11
- package/dist-lib/lib/assistant.js +824 -378
- 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 +42 -28
- 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 +69 -0
- package/dist-lib/lib/core/collections.js +69 -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.js +1 -1
- package/dist-lib/lib/core/contract/chunks.js +10 -9
- package/dist-lib/lib/core/contract/compute.js +2 -1
- 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 +5 -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 +44 -52
- package/dist-lib/lib/core/files/index.js +142 -93
- package/dist-lib/lib/core/files/knowledge.d.ts +2 -5
- 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 +18 -13
- package/dist-lib/lib/core/files/processing/index.js +65 -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 +13 -11
- package/dist-lib/lib/core/files/processing/state.js +19 -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 +10 -13
- package/dist-lib/lib/core/files/rag/index.js +3 -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/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/reclaim.js +41 -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 +10 -10
- package/dist-lib/lib/core/files/store/blob.js +25 -19
- package/dist-lib/lib/core/files/store/index.d.ts +1 -6
- package/dist-lib/lib/core/files/store/index.js +4 -4
- 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.js +20 -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 +20 -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 +987 -539
- package/dist-lib/lib/credential-pusher.d.ts +12 -17
- package/dist-lib/lib/credential-pusher.js +61 -42
- 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 +8 -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/index.js +1 -1
- package/package.json +7 -3
- package/tui/app.mjs +2424 -941
- package/tui/boot.mjs +17 -46
- 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;
|
|
@@ -362,7 +424,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
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,24 +434,27 @@ 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
452
|
async _referencedImageNames() {
|
|
389
453
|
try {
|
|
390
454
|
const { files } = await this._engine.listLocalFiles({});
|
|
391
|
-
if (!files?.length)
|
|
455
|
+
if (!files?.length) {
|
|
392
456
|
return null;
|
|
457
|
+
}
|
|
393
458
|
return new Set(files.map((file) => imageCacheKey(file.blobId)));
|
|
394
459
|
}
|
|
395
460
|
catch (err) {
|
|
@@ -479,20 +544,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
479
544
|
toolCatalog
|
|
480
545
|
]);
|
|
481
546
|
const meshId = this._accountMeshId;
|
|
482
|
-
if (!meshId)
|
|
547
|
+
if (!meshId) {
|
|
483
548
|
return;
|
|
549
|
+
}
|
|
484
550
|
const readySkillNames = this._readySkills;
|
|
485
551
|
const report = {
|
|
486
552
|
installedToolNames: this._info.toolNames,
|
|
487
553
|
installedSkillNames: this._info.skills,
|
|
488
554
|
installedModelNames,
|
|
489
555
|
cachedModelNames,
|
|
490
|
-
|
|
491
|
-
|
|
556
|
+
readySkillNames: readySkillNames ?? undefined,
|
|
557
|
+
toolCatalog: toolCatalog ?? undefined
|
|
492
558
|
};
|
|
493
559
|
await this._reportInto(meshId, key, report);
|
|
494
|
-
if (!readySkillNames)
|
|
560
|
+
if (!readySkillNames) {
|
|
495
561
|
return;
|
|
562
|
+
}
|
|
496
563
|
for (const [shared, hosted] of this._hostedIn) {
|
|
497
564
|
if (shared === meshId)
|
|
498
565
|
continue;
|
|
@@ -507,30 +574,36 @@ export class QvacAssistant extends ReadyResource {
|
|
|
507
574
|
this._lastCapabilities.set(meshId, key);
|
|
508
575
|
}
|
|
509
576
|
catch (err) {
|
|
510
|
-
if (isMissingMesh(err))
|
|
577
|
+
if (isMissingMesh(err)) {
|
|
511
578
|
return;
|
|
579
|
+
}
|
|
512
580
|
this._log?.error('assistant', 'saveDeviceCapabilities failed', err);
|
|
513
581
|
}
|
|
514
582
|
}
|
|
515
583
|
_followHosted(meshId, agents) {
|
|
516
584
|
const deviceId = this._deviceId;
|
|
517
|
-
if (!deviceId || !this._sharedMeshes.has(meshId))
|
|
585
|
+
if (!deviceId || !this._sharedMeshes.has(meshId)) {
|
|
518
586
|
return;
|
|
587
|
+
}
|
|
519
588
|
const hosted = agents
|
|
520
|
-
.filter((agent) => agent.providerDeviceId
|
|
589
|
+
.filter((agent) => !!agent.providerDeviceId && b4a.equals(agent.providerDeviceId, deviceId))
|
|
521
590
|
.map((agent) => agent.id)
|
|
522
591
|
.sort();
|
|
523
|
-
if (sameNameList(this._hostedIn.get(meshId) ?? [], hosted))
|
|
592
|
+
if (sameNameList(this._hostedIn.get(meshId) ?? [], hosted)) {
|
|
524
593
|
return;
|
|
525
|
-
|
|
594
|
+
}
|
|
595
|
+
if (hosted.length > 0) {
|
|
526
596
|
this._hostedIn.set(meshId, hosted);
|
|
527
|
-
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
528
599
|
this._hostedIn.delete(meshId);
|
|
600
|
+
}
|
|
529
601
|
void this._reportHosted().catch(noop);
|
|
530
602
|
}
|
|
531
603
|
async _reportHosted() {
|
|
532
|
-
if (this._readySkills)
|
|
604
|
+
if (this._readySkills) {
|
|
533
605
|
await this._recordSkillReadiness();
|
|
606
|
+
}
|
|
534
607
|
await this._reportCapabilities();
|
|
535
608
|
}
|
|
536
609
|
async _ensureCapability() {
|
|
@@ -544,35 +617,39 @@ export class QvacAssistant extends ReadyResource {
|
|
|
544
617
|
this._log?.warn('assistant', '_reconcileDeviceProfile: harness.capability failed', err);
|
|
545
618
|
return null;
|
|
546
619
|
}
|
|
547
|
-
if (!probed?.recommendedModelName)
|
|
620
|
+
if (!probed?.recommendedModelName) {
|
|
548
621
|
return null;
|
|
622
|
+
}
|
|
549
623
|
this._capability = probed;
|
|
550
624
|
return probed;
|
|
551
625
|
}
|
|
552
626
|
async _reconcileDeviceProfile() {
|
|
553
627
|
const deviceId = this._deviceId;
|
|
554
628
|
const meshId = this._accountMeshId;
|
|
555
|
-
if (!deviceId || !meshId || this._reconciling)
|
|
629
|
+
if (!deviceId || !meshId || this._reconciling) {
|
|
556
630
|
return;
|
|
631
|
+
}
|
|
557
632
|
this._reconciling = true;
|
|
558
633
|
try {
|
|
559
634
|
const capability = await this._ensureCapability();
|
|
560
|
-
if (!capability)
|
|
635
|
+
if (!capability) {
|
|
561
636
|
return;
|
|
637
|
+
}
|
|
562
638
|
const self = await this._engine.getDevice({ meshId, deviceId }).catch(noop);
|
|
563
|
-
if (!self)
|
|
639
|
+
if (!self) {
|
|
564
640
|
return;
|
|
641
|
+
}
|
|
565
642
|
const profileUpdated = !(self.capabilityProfile === capability.capabilityProfile &&
|
|
566
643
|
self.recommendedModelName === capability.recommendedModelName &&
|
|
567
644
|
self.platform === this._platform);
|
|
568
645
|
const profile = {
|
|
569
|
-
publicKey: b4a.
|
|
646
|
+
publicKey: b4a.toHex(deviceId),
|
|
570
647
|
name: self.name,
|
|
571
648
|
platform: this._platform,
|
|
572
649
|
mobileClass: this._mobileClass,
|
|
573
650
|
capabilityProfile: capability.capabilityProfile,
|
|
574
651
|
recommendedModelName: capability.recommendedModelName,
|
|
575
|
-
|
|
652
|
+
memoryBudget: capability.memoryBudget
|
|
576
653
|
};
|
|
577
654
|
if (profileUpdated) {
|
|
578
655
|
try {
|
|
@@ -600,8 +677,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
600
677
|
this._harness.off?.('model', this._onModelCatalogEvent);
|
|
601
678
|
this._warmAbort.abort(RUNNER_CLOSED);
|
|
602
679
|
this._onlineBoot.destroy(new Error('assistant is closing'));
|
|
603
|
-
for (const run of this._runs.values())
|
|
680
|
+
for (const run of this._runs.values()) {
|
|
604
681
|
run.controller.abort(RUNNER_CLOSED);
|
|
682
|
+
}
|
|
605
683
|
await this._downloads?.close();
|
|
606
684
|
this._modelCatalog.close();
|
|
607
685
|
this._meshList?.destroy();
|
|
@@ -620,18 +698,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
620
698
|
}
|
|
621
699
|
async suspend() {
|
|
622
700
|
this._log?.info('assistant', 'suspend called');
|
|
623
|
-
if (this._state === QvacState.SUSPEND)
|
|
701
|
+
if (this._state === QvacState.SUSPEND) {
|
|
624
702
|
return;
|
|
625
|
-
|
|
703
|
+
}
|
|
704
|
+
if (this.closing) {
|
|
626
705
|
return;
|
|
706
|
+
}
|
|
627
707
|
this._state = QvacState.SUSPEND;
|
|
628
708
|
this._log?.info('assistant', 'suspending', { runs: this._runs.size });
|
|
629
709
|
this._stopMeshIntake();
|
|
630
710
|
await this._downloads?.suspend();
|
|
631
711
|
this._meshList?.destroy();
|
|
632
712
|
this._accountFrames?.destroy();
|
|
633
|
-
for (const run of this._runs.values())
|
|
713
|
+
for (const run of this._runs.values()) {
|
|
634
714
|
run.controller.abort(SUSPENDED);
|
|
715
|
+
}
|
|
635
716
|
this._warmedModel = false;
|
|
636
717
|
this._warmedModelName = null;
|
|
637
718
|
this._modelCatalog.notify();
|
|
@@ -646,10 +727,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
646
727
|
}
|
|
647
728
|
async resume() {
|
|
648
729
|
this._log?.info('assistant', 'resume called');
|
|
649
|
-
if (this._state === QvacState.RESUME)
|
|
730
|
+
if (this._state === QvacState.RESUME) {
|
|
650
731
|
return;
|
|
651
|
-
|
|
732
|
+
}
|
|
733
|
+
if (this.closing) {
|
|
652
734
|
return;
|
|
735
|
+
}
|
|
653
736
|
if (this._state === QvacState.PRESUSPEND) {
|
|
654
737
|
this._state = QvacState.RESUME;
|
|
655
738
|
return;
|
|
@@ -658,8 +741,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
658
741
|
this._log?.info('assistant', 'resuming');
|
|
659
742
|
await Promise.allSettled([...this._runs.values()].map((run) => run.done));
|
|
660
743
|
this._downloads?.resume();
|
|
661
|
-
if (!this.opened)
|
|
744
|
+
if (!this.opened) {
|
|
662
745
|
return;
|
|
746
|
+
}
|
|
663
747
|
this._meshListWatch.start();
|
|
664
748
|
this._accountWatch.start();
|
|
665
749
|
}
|
|
@@ -667,14 +751,92 @@ export class QvacAssistant extends ReadyResource {
|
|
|
667
751
|
return (await this._harness.list?.(options)) ?? [];
|
|
668
752
|
}
|
|
669
753
|
async deleteModel(name) {
|
|
670
|
-
|
|
754
|
+
const removeFiles = async () => (await this._harness.deleteModel?.(name)) ?? { name, removed: 0, freedBytes: 0 };
|
|
755
|
+
let summary;
|
|
756
|
+
if (this._downloads) {
|
|
757
|
+
summary = await this._downloads.release(name, removeFiles);
|
|
758
|
+
}
|
|
759
|
+
else {
|
|
760
|
+
summary = await removeFiles();
|
|
761
|
+
await this._engine.setModelReleased({ name, released: true });
|
|
762
|
+
}
|
|
763
|
+
this._modelCatalog.notify();
|
|
764
|
+
this._reportCapabilities().catch(noop);
|
|
765
|
+
return summary;
|
|
671
766
|
}
|
|
672
767
|
modelCatalogWatch() {
|
|
673
768
|
return this._modelCatalog.watch();
|
|
674
769
|
}
|
|
770
|
+
async loadModel(name) {
|
|
771
|
+
if (!this._harness.loadModel) {
|
|
772
|
+
throw new Error('the model runtime cannot preload a model');
|
|
773
|
+
}
|
|
774
|
+
if (!(await this._isCached(name))) {
|
|
775
|
+
throw new Error(`${name} is not downloaded on this device`);
|
|
776
|
+
}
|
|
777
|
+
const config = await this._engine.getConfig({}).catch(noop);
|
|
778
|
+
const agent = await this._agentRunning(name, config?.activeAgentId ?? null);
|
|
779
|
+
const knobs = agent
|
|
780
|
+
? withConfigDefaults(agent, config)
|
|
781
|
+
: { ctxSize: config?.ctxSize || undefined };
|
|
782
|
+
const { loaded, reason } = await this._harness.loadModel({ model: name, agent: knobs });
|
|
783
|
+
this._log?.info('models', 'model load requested', { model: name, loaded, reason });
|
|
784
|
+
return { loaded, reason };
|
|
785
|
+
}
|
|
786
|
+
async _isCached(model) {
|
|
787
|
+
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
788
|
+
return rows?.some((row) => row.name === model && row.isCached) === true;
|
|
789
|
+
}
|
|
790
|
+
async _agentRunning(model, activeAgentId) {
|
|
791
|
+
const deviceId = this._requireDeviceId();
|
|
792
|
+
const meshId = this._accountMeshId;
|
|
793
|
+
if (!meshId) {
|
|
794
|
+
return null;
|
|
795
|
+
}
|
|
796
|
+
const capability = await this._ensureCapability();
|
|
797
|
+
const runs = (agent) => runnableModelName(agent.modelName, capability, this._mobileClass) === model;
|
|
798
|
+
const active = await this._currentAgent(activeAgentId, deviceId);
|
|
799
|
+
if (active?.providerDeviceId && b4a.equals(active.providerDeviceId, deviceId) && runs(active)) {
|
|
800
|
+
return active;
|
|
801
|
+
}
|
|
802
|
+
const { agents } = await this._engine.listAgents({ meshId, providerDeviceId: deviceId });
|
|
803
|
+
return (agents ?? []).find(runs) ?? null;
|
|
804
|
+
}
|
|
805
|
+
unloadModel(name) {
|
|
806
|
+
const pending = this._unloading.get(name);
|
|
807
|
+
if (pending) {
|
|
808
|
+
return pending;
|
|
809
|
+
}
|
|
810
|
+
const unloading = Promise.resolve()
|
|
811
|
+
.then(() => this._unloadModel(name))
|
|
812
|
+
.finally(() => this._unloading.delete(name));
|
|
813
|
+
this._unloading.set(name, unloading);
|
|
814
|
+
return unloading;
|
|
815
|
+
}
|
|
816
|
+
async _unloadModel(name) {
|
|
817
|
+
if (!this._harness.unloadModel) {
|
|
818
|
+
throw new Error('the model runtime cannot unload a model');
|
|
819
|
+
}
|
|
820
|
+
const holders = [...this._runs.values()].filter((run) => run.executing && !run.borrowed && run.model === name);
|
|
821
|
+
for (const run of holders) {
|
|
822
|
+
run.controller.abort(MODEL_UNLOADED);
|
|
823
|
+
}
|
|
824
|
+
await Promise.allSettled(holders.map((run) => run.done));
|
|
825
|
+
const { unloaded } = await this._harness.unloadModel({ model: name });
|
|
826
|
+
this._log?.info('models', 'model unload requested', {
|
|
827
|
+
model: name,
|
|
828
|
+
unloaded,
|
|
829
|
+
requeued: holders.length
|
|
830
|
+
});
|
|
831
|
+
return { unloaded };
|
|
832
|
+
}
|
|
833
|
+
notifyModelCatalog() {
|
|
834
|
+
this._modelCatalog.notify();
|
|
835
|
+
}
|
|
675
836
|
async retryModelDownload(name) {
|
|
676
|
-
if (!this._downloads)
|
|
837
|
+
if (!this._downloads) {
|
|
677
838
|
throw new Error('model download reconciler is unavailable');
|
|
839
|
+
}
|
|
678
840
|
await this._downloads.retry(name);
|
|
679
841
|
}
|
|
680
842
|
async skills(agentId, modelName, options) {
|
|
@@ -684,7 +846,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
684
846
|
const meshId = options?.meshId ?? this._accountMeshId;
|
|
685
847
|
const agent = meshId ? await this._engine.getAgent({ agentId, meshId }).catch(noop) : null;
|
|
686
848
|
const deviceId = this._deviceId;
|
|
687
|
-
if (agent &&
|
|
849
|
+
if (agent &&
|
|
850
|
+
deviceId &&
|
|
851
|
+
(!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, deviceId))) {
|
|
688
852
|
agentId = null;
|
|
689
853
|
options = { ...options, skipCredentialProbe: true };
|
|
690
854
|
}
|
|
@@ -695,37 +859,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
695
859
|
}
|
|
696
860
|
async _recordSkillReadiness() {
|
|
697
861
|
this._readinessDirty = true;
|
|
698
|
-
if (this._recordingReadiness)
|
|
862
|
+
if (this._recordingReadiness) {
|
|
699
863
|
return;
|
|
864
|
+
}
|
|
700
865
|
this._recordingReadiness = true;
|
|
701
866
|
try {
|
|
702
867
|
while (this._readinessDirty) {
|
|
703
868
|
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))
|
|
869
|
+
const ready = await this._readySkillNames();
|
|
870
|
+
if (this._readySkills && sameNameList(this._readySkills, ready)) {
|
|
728
871
|
continue;
|
|
872
|
+
}
|
|
729
873
|
this._readySkills = ready;
|
|
730
874
|
await this._reportCapabilities();
|
|
731
875
|
}
|
|
@@ -734,6 +878,29 @@ export class QvacAssistant extends ReadyResource {
|
|
|
734
878
|
this._recordingReadiness = false;
|
|
735
879
|
}
|
|
736
880
|
}
|
|
881
|
+
async _readySkillNames() {
|
|
882
|
+
const unscoped = (await this._harness.skills?.(null, null, { skipCredentialProbe: true })) ?? [];
|
|
883
|
+
const ready = unscoped.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name);
|
|
884
|
+
const deviceId = this._deviceId;
|
|
885
|
+
const meshId = this._accountMeshId;
|
|
886
|
+
if (deviceId && meshId) {
|
|
887
|
+
const { agents } = await this._engine.listAgents({ meshId, providerDeviceId: deviceId });
|
|
888
|
+
const hosted = await Promise.all([...this._hostedIn.keys()]
|
|
889
|
+
.filter((shared) => shared !== meshId)
|
|
890
|
+
.map((shared) => this._engine.listAgents({ meshId: shared, providerDeviceId: deviceId }).catch(noop)));
|
|
891
|
+
const seen = new Set();
|
|
892
|
+
for (const agent of [agents ?? [], ...hosted.map((list) => list?.agents ?? [])].flat()) {
|
|
893
|
+
if (seen.has(agent.id))
|
|
894
|
+
continue;
|
|
895
|
+
seen.add(agent.id);
|
|
896
|
+
const resolved = (await this._harness.skills?.(agent.id, agent.modelName, {
|
|
897
|
+
skipCredentialProbe: true
|
|
898
|
+
})) ?? [];
|
|
899
|
+
ready.push(...encodeAgentSkillReadiness(agent.id, resolved.filter((skill) => isSetUpHere(skill.status)).map((skill) => skill.name)));
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
return ready.sort();
|
|
903
|
+
}
|
|
737
904
|
async tools() {
|
|
738
905
|
return (await this._harness.tools?.()) ?? [];
|
|
739
906
|
}
|
|
@@ -745,13 +912,15 @@ export class QvacAssistant extends ReadyResource {
|
|
|
745
912
|
if (err !== null && !isMissingMesh(err)) {
|
|
746
913
|
this._log?.error('assistant', `${label} watch failed`, err);
|
|
747
914
|
}
|
|
748
|
-
if (again)
|
|
915
|
+
if (again) {
|
|
749
916
|
start();
|
|
917
|
+
}
|
|
750
918
|
};
|
|
751
919
|
const start = () => {
|
|
752
920
|
again = false;
|
|
753
|
-
if (this.closing || this.suspended)
|
|
921
|
+
if (this.closing || this.suspended) {
|
|
754
922
|
return;
|
|
923
|
+
}
|
|
755
924
|
if (running) {
|
|
756
925
|
again = true;
|
|
757
926
|
return;
|
|
@@ -790,12 +959,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
790
959
|
const agents = frame.agents ?? [];
|
|
791
960
|
this._followHosted(meshId, agents);
|
|
792
961
|
const next = new Map(agents.map((agent) => [agent.id, agentRekeyFingerprint(agent)]));
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
void this._reclaimAgentChatCaches(meshId, agent.id);
|
|
798
|
-
}
|
|
962
|
+
for (const agent of agents) {
|
|
963
|
+
const previous = known?.get(agent.id);
|
|
964
|
+
if (previous !== undefined && previous !== next.get(agent.id)) {
|
|
965
|
+
void this._reclaimAgentChatCaches(meshId, agent.id);
|
|
799
966
|
}
|
|
800
967
|
}
|
|
801
968
|
known = next;
|
|
@@ -805,8 +972,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
805
972
|
try {
|
|
806
973
|
const { chats } = await this._engine.listChats({ meshId });
|
|
807
974
|
for (const chat of chats ?? []) {
|
|
808
|
-
if (chat.agentId === agentId)
|
|
975
|
+
if (chat.agentId === agentId) {
|
|
809
976
|
await this._deleteChatKvCache(chat.id, 'agent re-key');
|
|
977
|
+
}
|
|
810
978
|
}
|
|
811
979
|
}
|
|
812
980
|
catch (err) {
|
|
@@ -814,25 +982,31 @@ export class QvacAssistant extends ReadyResource {
|
|
|
814
982
|
}
|
|
815
983
|
}
|
|
816
984
|
async _reconcileCompaction(engine, chatId) {
|
|
817
|
-
const deviceId = this.
|
|
985
|
+
const deviceId = this._requireDeviceId();
|
|
818
986
|
const { chunks } = await engine
|
|
819
987
|
.listChunks({ chatId, type: 'compaction' })
|
|
820
988
|
.catch(() => ({ chunks: [] }));
|
|
821
989
|
let newest = null;
|
|
822
|
-
for (const chunk of chunks ?? [])
|
|
823
|
-
if (!newest || chunk.seq > newest.seq)
|
|
990
|
+
for (const chunk of chunks ?? []) {
|
|
991
|
+
if (!newest || chunk.seq > newest.seq) {
|
|
824
992
|
newest = chunk;
|
|
825
|
-
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
if (!newest || (newest.originDeviceId && b4a.equals(newest.originDeviceId, deviceId))) {
|
|
826
996
|
return;
|
|
827
|
-
|
|
997
|
+
}
|
|
998
|
+
if (this._reconciledCompactions.get(chatId) === newest.id) {
|
|
828
999
|
return;
|
|
1000
|
+
}
|
|
829
1001
|
const gone = await this._deleteChatKvCache(chatId, 'compaction from another device');
|
|
830
|
-
if (gone)
|
|
1002
|
+
if (gone) {
|
|
831
1003
|
this._reconciledCompactions.set(chatId, newest.id);
|
|
1004
|
+
}
|
|
832
1005
|
}
|
|
833
1006
|
async _senderNames(engine, chunks) {
|
|
834
|
-
if (!couldNameSenders(chunks))
|
|
1007
|
+
if (!couldNameSenders(chunks)) {
|
|
835
1008
|
return new Map();
|
|
1009
|
+
}
|
|
836
1010
|
const stamped = chunks.every((chunk) => chunk.type !== 'user-message' || chunk.senderMemberId);
|
|
837
1011
|
const [{ members }, { devices }] = await Promise.all([
|
|
838
1012
|
engine.listMembers({}).catch(() => ({ members: [] })),
|
|
@@ -843,15 +1017,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
843
1017
|
async _reconcileAttribution(chatId, senders) {
|
|
844
1018
|
const attribution = senderAttribution(senders);
|
|
845
1019
|
const seen = this._chatAttribution.get(chatId);
|
|
846
|
-
if (seen === attribution)
|
|
1020
|
+
if (seen === attribution) {
|
|
847
1021
|
return;
|
|
1022
|
+
}
|
|
848
1023
|
if (seen === undefined && !attribution) {
|
|
849
1024
|
this._chatAttribution.set(chatId, attribution);
|
|
850
1025
|
return;
|
|
851
1026
|
}
|
|
852
1027
|
const gone = await this._deleteChatKvCache(chatId, 'sender attribution changed');
|
|
853
|
-
if (gone)
|
|
1028
|
+
if (gone) {
|
|
854
1029
|
this._chatAttribution.set(chatId, attribution);
|
|
1030
|
+
}
|
|
855
1031
|
}
|
|
856
1032
|
async deleteChatKvCache(chatId) {
|
|
857
1033
|
return ((await this._harness.deleteKvCache?.(chatKvCacheKey(chatId))) ?? {
|
|
@@ -930,8 +1106,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
930
1106
|
this._advertisedOn = null;
|
|
931
1107
|
return;
|
|
932
1108
|
}
|
|
933
|
-
if (on === this._advertisedOn)
|
|
1109
|
+
if (on === this._advertisedOn) {
|
|
934
1110
|
return;
|
|
1111
|
+
}
|
|
935
1112
|
this._advertisedOn = on;
|
|
936
1113
|
this._downloads?.meshAdopted();
|
|
937
1114
|
this._lastCapabilities.clear();
|
|
@@ -969,16 +1146,18 @@ export class QvacAssistant extends ReadyResource {
|
|
|
969
1146
|
if (this.closing || this.suspended)
|
|
970
1147
|
return;
|
|
971
1148
|
const chunks = frame.chunks ?? [];
|
|
972
|
-
for (const chunk of chunks)
|
|
1149
|
+
for (const chunk of chunks) {
|
|
973
1150
|
this._dispatch(chunk, meshId);
|
|
1151
|
+
}
|
|
974
1152
|
this._pruneSeen(meshId, new Set(chunks.map((chunk) => chunk.runId)));
|
|
975
1153
|
}
|
|
976
1154
|
}
|
|
977
1155
|
_stopMeshIntake() {
|
|
978
1156
|
this._meshListWatch.stop();
|
|
979
1157
|
this._accountWatch.stop();
|
|
980
|
-
for (const consumer of this._meshConsumers.values())
|
|
1158
|
+
for (const consumer of this._meshConsumers.values()) {
|
|
981
1159
|
consumer.stop();
|
|
1160
|
+
}
|
|
982
1161
|
this._meshConsumers.clear();
|
|
983
1162
|
this._advertisedOn = null;
|
|
984
1163
|
}
|
|
@@ -991,8 +1170,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
991
1170
|
}
|
|
992
1171
|
_lenderFor(agent) {
|
|
993
1172
|
const lender = agent.lenderDeviceId;
|
|
994
|
-
if (!lender || !this._borrowHarness)
|
|
1173
|
+
if (!lender || !this._borrowHarness) {
|
|
995
1174
|
return null;
|
|
1175
|
+
}
|
|
996
1176
|
const id = b4a.toHex(lender);
|
|
997
1177
|
const own = this._deviceId;
|
|
998
1178
|
return own && id === b4a.toHex(own) ? null : id;
|
|
@@ -1000,41 +1180,47 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1000
1180
|
_harnessFor(agent) {
|
|
1001
1181
|
const id = this._lenderFor(agent);
|
|
1002
1182
|
const borrow = this._borrowHarness;
|
|
1003
|
-
if (!id || !borrow)
|
|
1183
|
+
if (!id || !borrow) {
|
|
1004
1184
|
return this._harness;
|
|
1185
|
+
}
|
|
1005
1186
|
const held = this._borrowed.get(id);
|
|
1006
|
-
if (held)
|
|
1187
|
+
if (held) {
|
|
1007
1188
|
return held;
|
|
1189
|
+
}
|
|
1008
1190
|
const remote = connectHarness(() => borrow(id));
|
|
1009
|
-
remote.on('status', (
|
|
1010
|
-
if (status.state === 'died')
|
|
1191
|
+
remote.on('status', (status) => {
|
|
1192
|
+
if (status.state === 'died') {
|
|
1011
1193
|
this._borrowed.delete(id);
|
|
1012
|
-
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1013
1196
|
const harness = remote;
|
|
1014
1197
|
this._borrowed.set(id, harness);
|
|
1015
1198
|
return harness;
|
|
1016
1199
|
}
|
|
1017
1200
|
_dispatch(request, meshId) {
|
|
1018
1201
|
const runId = request.runId;
|
|
1019
|
-
if (!runId || this._runs.has(runId) || this._seen.has(runId))
|
|
1202
|
+
if (!runId || this._runs.has(runId) || this._seen.has(runId)) {
|
|
1020
1203
|
return;
|
|
1204
|
+
}
|
|
1021
1205
|
this._seen.set(runId, meshId);
|
|
1022
1206
|
const engine = meshScoped(this._engine, meshId);
|
|
1023
1207
|
const settle = (async () => {
|
|
1024
1208
|
const agentId = request.runRequest?.agentId;
|
|
1025
|
-
const agent = await engine.getAgent({ agentId }).catch(noop);
|
|
1209
|
+
const agent = agentId ? await engine.getAgent({ agentId }).catch(noop) : undefined;
|
|
1026
1210
|
if (!agent) {
|
|
1027
1211
|
this._seen.delete(runId);
|
|
1028
1212
|
return;
|
|
1029
1213
|
}
|
|
1030
|
-
if (!agent.providerDeviceId
|
|
1214
|
+
if (!agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, this._requireDeviceId())) {
|
|
1031
1215
|
return;
|
|
1032
|
-
|
|
1216
|
+
}
|
|
1217
|
+
if (await this._isHandled(engine, request)) {
|
|
1033
1218
|
return;
|
|
1219
|
+
}
|
|
1034
1220
|
this._log?.info('runs', 'run claimed', { runId, chatId: request.chatId, agentId });
|
|
1035
1221
|
const controller = new AbortController();
|
|
1036
1222
|
const prior = this._chats.get(request.chatId) ?? Promise.resolve();
|
|
1037
|
-
const done = prior.then(() => this._execute(request, agent, controller, meshId));
|
|
1223
|
+
const done = prior.then(() => this._execute(request, agent, { controller, meshId, runId }));
|
|
1038
1224
|
const link = done.catch(noop);
|
|
1039
1225
|
this._chats.set(request.chatId, link);
|
|
1040
1226
|
this._runs.set(runId, {
|
|
@@ -1042,7 +1228,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1042
1228
|
done,
|
|
1043
1229
|
chatId: request.chatId,
|
|
1044
1230
|
model: agent.modelName,
|
|
1045
|
-
executing: false
|
|
1231
|
+
executing: false,
|
|
1232
|
+
borrowed: false
|
|
1046
1233
|
});
|
|
1047
1234
|
try {
|
|
1048
1235
|
await done;
|
|
@@ -1051,17 +1238,23 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1051
1238
|
this._runImageCacheNames.delete(runId);
|
|
1052
1239
|
this._runs.delete(runId);
|
|
1053
1240
|
this._yieldDownloads();
|
|
1054
|
-
if (this._chats.get(request.chatId) === link)
|
|
1241
|
+
if (this._chats.get(request.chatId) === link) {
|
|
1055
1242
|
this._chats.delete(request.chatId);
|
|
1243
|
+
}
|
|
1056
1244
|
void this._reportCapabilities();
|
|
1245
|
+
const requeued = controller.signal.reason === MODEL_UNLOADED && !this._seen.has(runId);
|
|
1246
|
+
if (requeued && !this.closing) {
|
|
1247
|
+
this._dispatch(request, meshId);
|
|
1248
|
+
}
|
|
1057
1249
|
}
|
|
1058
1250
|
})();
|
|
1059
1251
|
settle.catch((err) => this._log?.error('assistant', `run ${runId} failed`, err));
|
|
1060
1252
|
}
|
|
1061
1253
|
async _citeWebSources(transcript, runId, result) {
|
|
1062
1254
|
const records = webSourcesFromToolResult(result, WEB_RAG_SOURCE_BOUNDS);
|
|
1063
|
-
if (records.length === 0)
|
|
1255
|
+
if (records.length === 0) {
|
|
1064
1256
|
return;
|
|
1257
|
+
}
|
|
1065
1258
|
try {
|
|
1066
1259
|
await transcript.chunk({ type: 'sources', sources: { sources: records } });
|
|
1067
1260
|
}
|
|
@@ -1069,7 +1262,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1069
1262
|
this._log?.warn('assistant', 'web sources chunk skipped', { runId, err });
|
|
1070
1263
|
}
|
|
1071
1264
|
}
|
|
1072
|
-
async _searchKnowledge(engine,
|
|
1265
|
+
async _searchKnowledge(engine, messages, { chatId, attachmentIds, signal, preparing }) {
|
|
1073
1266
|
let lastUser;
|
|
1074
1267
|
for (let index = messages.length - 1; index >= 0; index--) {
|
|
1075
1268
|
if (messages[index].role === 'user') {
|
|
@@ -1078,27 +1271,77 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1078
1271
|
}
|
|
1079
1272
|
}
|
|
1080
1273
|
const query = lastUser?.content.trim();
|
|
1081
|
-
if (!lastUser || !query)
|
|
1274
|
+
if (!lastUser || !query) {
|
|
1082
1275
|
return null;
|
|
1276
|
+
}
|
|
1083
1277
|
try {
|
|
1084
1278
|
const search = await searchKnowledge(engine, {
|
|
1085
1279
|
chatId,
|
|
1086
1280
|
query,
|
|
1087
1281
|
attachmentIds: attachmentIds.length > 0 ? [...attachmentIds] : undefined
|
|
1088
1282
|
}, { signal, preparing });
|
|
1089
|
-
if (!search.enabled)
|
|
1283
|
+
if (!search.enabled) {
|
|
1090
1284
|
return null;
|
|
1091
|
-
|
|
1285
|
+
}
|
|
1286
|
+
if (!search.hasKnowledge) {
|
|
1092
1287
|
return null;
|
|
1288
|
+
}
|
|
1093
1289
|
return { search, lastUser, hits: search.hits ?? [] };
|
|
1094
1290
|
}
|
|
1095
1291
|
catch (err) {
|
|
1096
|
-
if (err instanceof AbortedError)
|
|
1292
|
+
if (err instanceof AbortedError) {
|
|
1097
1293
|
return null;
|
|
1294
|
+
}
|
|
1098
1295
|
this._log?.warn('assistant', 'knowledge search unavailable', err);
|
|
1099
1296
|
return null;
|
|
1100
1297
|
}
|
|
1101
1298
|
}
|
|
1299
|
+
async _injectKnowledge(knowledge, sinks) {
|
|
1300
|
+
const injected = { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
1301
|
+
try {
|
|
1302
|
+
if (knowledge.search.errorMessage) {
|
|
1303
|
+
this._log?.warn('assistant', 'knowledge search failed', {
|
|
1304
|
+
error: knowledge.search.errorMessage
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
const fullTextOnly = knowledge.hits.length > 0 && knowledge.hits.every(isFullTextHit);
|
|
1308
|
+
if (fullTextOnly) {
|
|
1309
|
+
knowledge.lastUser.content = `${formatFullDocumentContext(knowledge.hits)}\n\nThe user's message:\n${knowledge.lastUser.content}`;
|
|
1310
|
+
injected.documentsInjected = true;
|
|
1311
|
+
if (knowledge.hits.some(isTruncatedFullTextHit)) {
|
|
1312
|
+
await sinks.transcript.chunk({
|
|
1313
|
+
type: 'sources',
|
|
1314
|
+
sources: { sources: [], notice: RAG_TRUNCATED_NOTICE }
|
|
1315
|
+
});
|
|
1316
|
+
injected.sourcesWritten = true;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
else {
|
|
1320
|
+
await injectRetrievedKnowledge(knowledge, { ...sinks, injected });
|
|
1321
|
+
}
|
|
1322
|
+
if (knowledge.search.pendingFiles?.length) {
|
|
1323
|
+
knowledge.lastUser.content += `\n\n[Files not yet available for this reply: ${knowledge.search.pendingFiles.join(', ')}.]`;
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
catch (err) {
|
|
1327
|
+
this._log?.warn('assistant', 'knowledge injection skipped', err);
|
|
1328
|
+
}
|
|
1329
|
+
return injected;
|
|
1330
|
+
}
|
|
1331
|
+
async _writeUnreadNotice(transcript, reason) {
|
|
1332
|
+
try {
|
|
1333
|
+
await transcript.chunk({
|
|
1334
|
+
type: 'sources',
|
|
1335
|
+
sources: {
|
|
1336
|
+
sources: [],
|
|
1337
|
+
notice: reason === 'knowledge-off' ? RAG_KNOWLEDGE_OFF_NOTICE : RAG_UNAVAILABLE_NOTICE
|
|
1338
|
+
}
|
|
1339
|
+
});
|
|
1340
|
+
}
|
|
1341
|
+
catch (err) {
|
|
1342
|
+
this._log?.warn('assistant', 'unread-document notice skipped', err);
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1102
1345
|
async _isHandled(engine, request) {
|
|
1103
1346
|
const { chunks } = await engine.listChunks({
|
|
1104
1347
|
chatId: request.chatId,
|
|
@@ -1106,28 +1349,41 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1106
1349
|
});
|
|
1107
1350
|
return (chunks ?? []).some((c) => c.runId === request.runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
1108
1351
|
}
|
|
1109
|
-
async _execute(request, agent, controller, meshId) {
|
|
1352
|
+
async _execute(request, agent, { controller, meshId, runId }) {
|
|
1110
1353
|
const engine = meshScoped(this._engine, meshId);
|
|
1111
1354
|
const { chatId } = request;
|
|
1112
|
-
const
|
|
1113
|
-
const deviceId = this._deviceId;
|
|
1355
|
+
const deviceId = this._requireDeviceId();
|
|
1114
1356
|
if (this.suspended) {
|
|
1115
1357
|
this._seen.delete(runId);
|
|
1116
1358
|
return;
|
|
1117
1359
|
}
|
|
1118
|
-
if (await this._isHandled(engine, request))
|
|
1360
|
+
if (await this._isHandled(engine, request)) {
|
|
1119
1361
|
return;
|
|
1362
|
+
}
|
|
1120
1363
|
const fresh = await engine.getAgent({ agentId: agent.id }).catch(noop);
|
|
1121
|
-
if (!fresh ||
|
|
1364
|
+
if (!fresh ||
|
|
1365
|
+
fresh.deletedAt ||
|
|
1366
|
+
!fresh.providerDeviceId ||
|
|
1367
|
+
!b4a.equals(fresh.providerDeviceId, deviceId)) {
|
|
1122
1368
|
return;
|
|
1369
|
+
}
|
|
1123
1370
|
agent = fresh;
|
|
1124
1371
|
const capability = request.capability;
|
|
1125
1372
|
if (capability) {
|
|
1126
|
-
return this._invokeCapability({
|
|
1373
|
+
return this._invokeCapability({
|
|
1374
|
+
engine,
|
|
1375
|
+
request,
|
|
1376
|
+
agent,
|
|
1377
|
+
capability,
|
|
1378
|
+
controller,
|
|
1379
|
+
meshId,
|
|
1380
|
+
runId
|
|
1381
|
+
});
|
|
1127
1382
|
}
|
|
1128
1383
|
await this._reconcileCompaction(engine, chatId);
|
|
1129
|
-
if (request.parentId)
|
|
1384
|
+
if (request.parentId) {
|
|
1130
1385
|
await this._deleteChatKvCache(chatId, 'superseding run');
|
|
1386
|
+
}
|
|
1131
1387
|
const lender = this._lenderFor(agent);
|
|
1132
1388
|
const model = lender
|
|
1133
1389
|
? agent.modelName
|
|
@@ -1139,9 +1395,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1139
1395
|
requested: agent.modelName,
|
|
1140
1396
|
model
|
|
1141
1397
|
});
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1398
|
+
}
|
|
1399
|
+
const claimed = this._runs.get(runId);
|
|
1400
|
+
if (claimed) {
|
|
1401
|
+
this._runs.set(runId, { ...claimed, model, borrowed: lender !== null });
|
|
1145
1402
|
}
|
|
1146
1403
|
const config = await engine.getConfig({}).catch(noop);
|
|
1147
1404
|
const configuredAgent = withConfigDefaults(agent, config);
|
|
@@ -1174,7 +1431,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1174
1431
|
? all.find((chunk) => chunk.id === currentRepair.parentId)
|
|
1175
1432
|
: undefined;
|
|
1176
1433
|
const acceptedRepairs = acceptedRepairCount(branches, runId);
|
|
1177
|
-
const history = all.filter((chunk) =>
|
|
1434
|
+
const history = all.filter((chunk) => !!chunk.compaction ||
|
|
1178
1435
|
(chunk.runId !== runId &&
|
|
1179
1436
|
inputAvailableToRun(chunk, request.seq) &&
|
|
1180
1437
|
(!repairParent || chunk.seq <= repairParent.seq || chunk.runId === repairParent.runId) &&
|
|
@@ -1193,8 +1450,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1193
1450
|
await this._reconcileAttribution(chatId, senders);
|
|
1194
1451
|
}
|
|
1195
1452
|
const messages = historyFromChunks(history, staleToolRunIds(history, priorAsks ?? [], runId), senders);
|
|
1196
|
-
if (currentRepair)
|
|
1453
|
+
if (currentRepair) {
|
|
1197
1454
|
messages.push({ role: 'user', content: currentRepair.promptText });
|
|
1455
|
+
}
|
|
1198
1456
|
const diagramRepair = currentRepair !== undefined;
|
|
1199
1457
|
const runAgent = diagramRepair
|
|
1200
1458
|
? {
|
|
@@ -1209,6 +1467,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1209
1467
|
? this._harness.supportsImagesFor(model)
|
|
1210
1468
|
: (this._harness.supportsImages ?? this._info.supportsImages) !== false;
|
|
1211
1469
|
let unreadableImages = new Map();
|
|
1470
|
+
let renderedPdfIds = new Set();
|
|
1212
1471
|
if (!seesImages) {
|
|
1213
1472
|
const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
|
|
1214
1473
|
if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
|
|
@@ -1220,17 +1479,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1220
1479
|
turn.content += `\n[Attached image${turn.images.length > 1 ? 's' : ''}: ${names} — not visible to this model]`;
|
|
1221
1480
|
}
|
|
1222
1481
|
delete turn.images;
|
|
1482
|
+
delete turn.pdfs;
|
|
1223
1483
|
}
|
|
1224
1484
|
}
|
|
1225
1485
|
else {
|
|
1226
1486
|
unreadableImages = await this._hydrateImages(messages, controller.signal, runId);
|
|
1227
1487
|
}
|
|
1228
|
-
const settledWatch = this._watchSettled(engine, chatId, runId, controller);
|
|
1488
|
+
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
1229
1489
|
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
1230
1490
|
let readinessError = null;
|
|
1231
1491
|
try {
|
|
1232
|
-
if (!lender)
|
|
1492
|
+
if (!lender) {
|
|
1233
1493
|
await this._downloads?.waitUntilCached(model, controller.signal);
|
|
1494
|
+
}
|
|
1234
1495
|
}
|
|
1235
1496
|
catch (err) {
|
|
1236
1497
|
readinessError = err instanceof Error ? err : new Error(String(err));
|
|
@@ -1238,8 +1499,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1238
1499
|
if (controller.signal.aborted) {
|
|
1239
1500
|
try {
|
|
1240
1501
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1241
|
-
if (outcome.kind === 'replay')
|
|
1502
|
+
if (outcome.kind === 'replay') {
|
|
1242
1503
|
this._seen.delete(runId);
|
|
1504
|
+
}
|
|
1243
1505
|
if (outcome.kind === 'failed') {
|
|
1244
1506
|
await transcript.status({ state: 'failed', reason: outcome.reason });
|
|
1245
1507
|
}
|
|
@@ -1263,21 +1525,27 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1263
1525
|
return;
|
|
1264
1526
|
}
|
|
1265
1527
|
const inflight = this._runs.get(runId);
|
|
1266
|
-
if (inflight)
|
|
1528
|
+
if (inflight) {
|
|
1267
1529
|
this._runs.set(runId, { ...inflight, executing: true });
|
|
1530
|
+
}
|
|
1268
1531
|
this._yieldDownloads();
|
|
1269
1532
|
const busyChatId = this._modelHolder(runId, chatId, model);
|
|
1270
1533
|
const executing = await transcript.status(busyChatId
|
|
1271
1534
|
? { state: 'executing', progress: { phase: 'queued', busyChatId } }
|
|
1272
1535
|
: { state: 'executing' });
|
|
1273
1536
|
const runStatusId = executing.id;
|
|
1274
|
-
const approvals = this._createApprove(engine,
|
|
1537
|
+
const approvals = this._createApprove(engine, {
|
|
1538
|
+
chatId,
|
|
1539
|
+
runId,
|
|
1540
|
+
deviceId,
|
|
1541
|
+
agentId: agent.id,
|
|
1542
|
+
hasRun: () => ran
|
|
1543
|
+
});
|
|
1275
1544
|
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
|
|
1276
1545
|
let metrics;
|
|
1277
1546
|
const sourceTagFilter = createRagSourceTagFilter();
|
|
1278
1547
|
const retrievedRecords = [];
|
|
1279
1548
|
let answerText = '';
|
|
1280
|
-
let ragInjected = false;
|
|
1281
1549
|
const flushTranscript = async () => {
|
|
1282
1550
|
const tail = sourceTagFilter.flush();
|
|
1283
1551
|
if (tail) {
|
|
@@ -1289,13 +1557,74 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1289
1557
|
let titleOutcome = null;
|
|
1290
1558
|
let runFailure = null;
|
|
1291
1559
|
let runVerdict = 'completed';
|
|
1560
|
+
const writeText = async (type, raw) => {
|
|
1561
|
+
const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
|
|
1562
|
+
if (!text) {
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
if (type === 'content') {
|
|
1566
|
+
answerText += text;
|
|
1567
|
+
}
|
|
1568
|
+
await transcript.text(type, text);
|
|
1569
|
+
};
|
|
1570
|
+
const failRun = async (message) => {
|
|
1571
|
+
const folded = isContextOverflowFailure(message) &&
|
|
1572
|
+
(await this._runCompaction({
|
|
1573
|
+
chatId,
|
|
1574
|
+
transcript,
|
|
1575
|
+
trigger: 'overflow',
|
|
1576
|
+
signal: controller.signal,
|
|
1577
|
+
meshId
|
|
1578
|
+
}));
|
|
1579
|
+
runFailure = runFailureNotice(message);
|
|
1580
|
+
await transcript.status({ state: 'failed', reason: runFailure, metrics });
|
|
1581
|
+
if (folded) {
|
|
1582
|
+
await engine
|
|
1583
|
+
.runAgent({ chatId, agentId: agent.id, parentRunId: runId })
|
|
1584
|
+
.catch((err) => this._log?.warn('runs', 'compaction retry not queued', { runId, err }));
|
|
1585
|
+
}
|
|
1586
|
+
titleOutcome = 'ended';
|
|
1587
|
+
};
|
|
1588
|
+
const endAbortedRun = async () => {
|
|
1589
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1590
|
+
if (outcome.kind === 'replay') {
|
|
1591
|
+
runVerdict = 'replay';
|
|
1592
|
+
this._seen.delete(runId);
|
|
1593
|
+
return;
|
|
1594
|
+
}
|
|
1595
|
+
if (outcome.kind === 'handoff') {
|
|
1596
|
+
runVerdict = 'handoff';
|
|
1597
|
+
return;
|
|
1598
|
+
}
|
|
1599
|
+
runFailure = outcome.reason;
|
|
1600
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1601
|
+
titleOutcome = 'ended';
|
|
1602
|
+
};
|
|
1603
|
+
const recordToolResult = async (event) => {
|
|
1604
|
+
if (isDeniedResult(event.result)) {
|
|
1605
|
+
this._log?.info('approvals', 'tool ran denied', { runId, tool: event.name });
|
|
1606
|
+
}
|
|
1607
|
+
else if (event.name !== SKILL_TOOL) {
|
|
1608
|
+
ran = true;
|
|
1609
|
+
}
|
|
1610
|
+
if (WEB_TOOLS.includes(String(event.name))) {
|
|
1611
|
+
await this._citeWebSources(transcript, runId, event.result);
|
|
1612
|
+
}
|
|
1613
|
+
await transcript.chunk(toolResultChunk(event));
|
|
1614
|
+
};
|
|
1292
1615
|
try {
|
|
1293
1616
|
const attachTurnIds = attachTurnDocumentIds(history);
|
|
1294
|
-
if (attachTurnIds.length > 0)
|
|
1617
|
+
if (attachTurnIds.length > 0) {
|
|
1295
1618
|
backfillDocumentOnlyTurn(messages);
|
|
1619
|
+
}
|
|
1296
1620
|
const knowledge = !diagramRepair &&
|
|
1297
1621
|
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
1298
|
-
? await this._searchKnowledge(engine,
|
|
1622
|
+
? await this._searchKnowledge(engine, messages, {
|
|
1623
|
+
chatId,
|
|
1624
|
+
attachmentIds: attachTurnIds,
|
|
1625
|
+
signal: controller.signal,
|
|
1626
|
+
preparing: modelWatch.documents
|
|
1627
|
+
})
|
|
1299
1628
|
: null;
|
|
1300
1629
|
if (controller.signal.aborted) {
|
|
1301
1630
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
@@ -1312,97 +1641,35 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1312
1641
|
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1313
1642
|
return;
|
|
1314
1643
|
}
|
|
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
|
-
}
|
|
1644
|
+
const injected = knowledge
|
|
1645
|
+
? await this._injectKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords })
|
|
1646
|
+
: { documentsInjected: false, sourcesWritten: false, ragInjected: false };
|
|
1375
1647
|
const hasAudio = messages.some((turn) => turn.audioAttachments?.length);
|
|
1376
|
-
if (hasAudio)
|
|
1648
|
+
if (hasAudio) {
|
|
1377
1649
|
modelWatch.transcribe(true);
|
|
1650
|
+
}
|
|
1378
1651
|
await this._hydrateAudio(messages, controller.signal);
|
|
1379
|
-
if (hasAudio)
|
|
1652
|
+
if (hasAudio) {
|
|
1380
1653
|
modelWatch.transcribe(false);
|
|
1654
|
+
}
|
|
1381
1655
|
backfillImageOnlyTurns(messages);
|
|
1656
|
+
if (seesImages) {
|
|
1657
|
+
renderedPdfIds = await this._hydratePdfPages(messages, controller.signal, runId);
|
|
1658
|
+
}
|
|
1382
1659
|
annotateDocumentAttachments(messages);
|
|
1383
|
-
|
|
1660
|
+
const unreadIds = attachTurnIds.filter((id) => !renderedPdfIds.has(id));
|
|
1661
|
+
if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
|
|
1384
1662
|
const reason = documentUnreadReason({
|
|
1385
1663
|
knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
|
|
1386
1664
|
failedFiles: knowledge?.search?.failedFiles,
|
|
1387
|
-
attachedCount:
|
|
1665
|
+
attachedCount: unreadIds.length
|
|
1388
1666
|
});
|
|
1389
1667
|
const lastUser = messages.findLast((turn) => turn.role === 'user');
|
|
1390
1668
|
if (lastUser) {
|
|
1391
|
-
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason,
|
|
1669
|
+
lastUser.content = `${lastUser.content}\n${documentUnreadNotice(reason, unreadIds.length > 1)}`;
|
|
1392
1670
|
}
|
|
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
|
-
}
|
|
1671
|
+
if (!injected.sourcesWritten) {
|
|
1672
|
+
await this._writeUnreadNotice(transcript, reason);
|
|
1406
1673
|
}
|
|
1407
1674
|
}
|
|
1408
1675
|
annotateUnreadableImages(unreadableImages);
|
|
@@ -1410,11 +1677,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1410
1677
|
const { roots, write } = await fsGrantsPromise;
|
|
1411
1678
|
const { grantedDirs, grantedDrives, driveOp } = await resolveFsGrants(roots, this._drives, (key, err) => this._log?.warn('assistant', 'drive grant dropped', {
|
|
1412
1679
|
key,
|
|
1413
|
-
error:
|
|
1680
|
+
error: errorMessage(err)
|
|
1414
1681
|
}));
|
|
1415
|
-
if (await this._isHandled(engine, request))
|
|
1682
|
+
if (await this._isHandled(engine, request)) {
|
|
1416
1683
|
return;
|
|
1684
|
+
}
|
|
1417
1685
|
const cacheKey = diagramRepair ? undefined : chatRepairKvCacheKey(chatId, acceptedRepairs);
|
|
1686
|
+
const unloading = lender ? undefined : this._unloading.get(model);
|
|
1687
|
+
if (unloading) {
|
|
1688
|
+
await raceAbort(unloading.catch(noop), controller.signal);
|
|
1689
|
+
}
|
|
1418
1690
|
this._log?.info('runs', 'completion requested', {
|
|
1419
1691
|
runId,
|
|
1420
1692
|
model,
|
|
@@ -1426,14 +1698,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1426
1698
|
agent: runAgent,
|
|
1427
1699
|
messages: nameSenders(messages),
|
|
1428
1700
|
cacheKey,
|
|
1429
|
-
thinkingDefault:
|
|
1701
|
+
thinkingDefault: thinkingDefault(config),
|
|
1430
1702
|
toolConfigs,
|
|
1431
1703
|
grantedDirs,
|
|
1432
1704
|
grantedDrives,
|
|
1433
1705
|
driveOp,
|
|
1434
1706
|
grantedWriteDirs: write,
|
|
1435
1707
|
approve: approvals.approve,
|
|
1436
|
-
attach: (input) => this._uploadGenerated(meshId, chatId, runId, deviceId
|
|
1708
|
+
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
1437
1709
|
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
1438
1710
|
ragSearch: async (input) => {
|
|
1439
1711
|
const found = await raceAbort(engine.ragSearch({
|
|
@@ -1478,15 +1750,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1478
1750
|
for await (const event of events) {
|
|
1479
1751
|
harnessEvents++;
|
|
1480
1752
|
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
|
-
}
|
|
1753
|
+
if (isHarnessEvent(event, 'content') || isHarnessEvent(event, 'thinking')) {
|
|
1754
|
+
await writeText(event.type, event.text);
|
|
1490
1755
|
continue;
|
|
1491
1756
|
}
|
|
1492
1757
|
if (event.type === 'metrics') {
|
|
@@ -1516,57 +1781,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1516
1781
|
}
|
|
1517
1782
|
await flushTranscript();
|
|
1518
1783
|
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';
|
|
1784
|
+
await failRun(event.message);
|
|
1535
1785
|
return;
|
|
1536
1786
|
}
|
|
1537
1787
|
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';
|
|
1788
|
+
await endAbortedRun();
|
|
1551
1789
|
return;
|
|
1552
1790
|
}
|
|
1553
|
-
if (event
|
|
1791
|
+
if (isHarnessEvent(event, 'tool-call')) {
|
|
1554
1792
|
this._log?.info('runs', 'tool call', { runId, name: event.name });
|
|
1555
1793
|
await transcript.chunk(toolCallChunk(event));
|
|
1556
1794
|
}
|
|
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));
|
|
1795
|
+
else if (isHarnessEvent(event, 'tool-result')) {
|
|
1796
|
+
await recordToolResult(event);
|
|
1568
1797
|
}
|
|
1569
|
-
else if (event
|
|
1798
|
+
else if (isHarnessEvent(event, 'tool-activity')) {
|
|
1570
1799
|
void engine
|
|
1571
1800
|
.recordToolActivity({
|
|
1572
1801
|
agentId: runAgent.id,
|
|
@@ -1595,7 +1824,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1595
1824
|
pending: transcript.pending()
|
|
1596
1825
|
});
|
|
1597
1826
|
}
|
|
1598
|
-
const citedTitles = ragInjected
|
|
1827
|
+
const citedTitles = injected.ragInjected
|
|
1828
|
+
? citedByPhraseOverlap(answerText, retrievedRecords)
|
|
1829
|
+
: null;
|
|
1599
1830
|
await transcript.status({
|
|
1600
1831
|
state: 'completed',
|
|
1601
1832
|
metrics: citedTitles ? { ...metrics, citedTitles } : metrics
|
|
@@ -1635,14 +1866,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1635
1866
|
await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
|
|
1636
1867
|
settledWatch.stop();
|
|
1637
1868
|
if (titleOutcome && !diagramRepair) {
|
|
1638
|
-
void this._autoTitle(engine,
|
|
1869
|
+
void this._autoTitle(engine, {
|
|
1870
|
+
chatId,
|
|
1871
|
+
messages,
|
|
1872
|
+
model,
|
|
1873
|
+
agent: runAgent,
|
|
1874
|
+
probe: titleOutcome === 'completed'
|
|
1875
|
+
});
|
|
1639
1876
|
}
|
|
1640
1877
|
}
|
|
1641
1878
|
}
|
|
1642
|
-
async _invokeCapability({ engine, request, agent, capability, controller, meshId }) {
|
|
1879
|
+
async _invokeCapability({ engine, request, agent, capability, controller, meshId, runId }) {
|
|
1643
1880
|
const { chatId } = request;
|
|
1644
|
-
const
|
|
1645
|
-
const deviceId = this._deviceId;
|
|
1881
|
+
const deviceId = this._requireDeviceId();
|
|
1646
1882
|
const harness = this._harnessFor(agent);
|
|
1647
1883
|
const transcript = new RunTranscript(engine, {
|
|
1648
1884
|
chatId,
|
|
@@ -1652,14 +1888,24 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1652
1888
|
log: this._log
|
|
1653
1889
|
});
|
|
1654
1890
|
const inflight = this._runs.get(runId);
|
|
1655
|
-
if (inflight)
|
|
1891
|
+
if (inflight) {
|
|
1656
1892
|
this._runs.set(runId, { ...inflight, executing: true, model: '' });
|
|
1893
|
+
}
|
|
1657
1894
|
this._yieldDownloads();
|
|
1658
1895
|
const executing = await transcript.status({ state: 'executing' });
|
|
1659
1896
|
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(executing.id, runStatus));
|
|
1660
|
-
const settledWatch = this._watchSettled(engine, chatId, runId, controller);
|
|
1897
|
+
const settledWatch = this._watchSettled(engine, { chatId, runId, controller });
|
|
1661
1898
|
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
1662
1899
|
let metrics;
|
|
1900
|
+
const endAbortedCapability = async () => {
|
|
1901
|
+
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
1902
|
+
if (outcome.kind === 'replay') {
|
|
1903
|
+
this._seen.delete(runId);
|
|
1904
|
+
}
|
|
1905
|
+
if (outcome.kind === 'failed') {
|
|
1906
|
+
await transcript.status({ state: 'failed', reason: outcome.reason, metrics });
|
|
1907
|
+
}
|
|
1908
|
+
};
|
|
1663
1909
|
this._log?.info('runs', 'capability requested', { runId, capability });
|
|
1664
1910
|
try {
|
|
1665
1911
|
if (!harness.invoke) {
|
|
@@ -1671,10 +1917,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1671
1917
|
}
|
|
1672
1918
|
const events = harness.invoke({
|
|
1673
1919
|
capability,
|
|
1674
|
-
args: (request.capabilityArgs
|
|
1920
|
+
args: isRecord(request.capabilityArgs) ? request.capabilityArgs : {},
|
|
1675
1921
|
enhance: request.enhance ?? 'never',
|
|
1676
1922
|
agent,
|
|
1677
|
-
attach: (input) => this._uploadGenerated(meshId, chatId, runId, deviceId
|
|
1923
|
+
attach: (input) => this._uploadGenerated(input, { meshId, chatId, runId, deviceId }),
|
|
1678
1924
|
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
1679
1925
|
progress: (update) => modelWatch.image(update),
|
|
1680
1926
|
signal: controller.signal
|
|
@@ -1695,18 +1941,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1695
1941
|
return;
|
|
1696
1942
|
}
|
|
1697
1943
|
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
|
-
}
|
|
1944
|
+
await endAbortedCapability();
|
|
1704
1945
|
return;
|
|
1705
1946
|
}
|
|
1706
|
-
if (event
|
|
1947
|
+
if (isHarnessEvent(event, 'tool-call')) {
|
|
1707
1948
|
await transcript.chunk(toolCallChunk(event));
|
|
1708
1949
|
}
|
|
1709
|
-
else if (event
|
|
1950
|
+
else if (isHarnessEvent(event, 'tool-result')) {
|
|
1710
1951
|
await transcript.chunk(toolResultChunk(event));
|
|
1711
1952
|
}
|
|
1712
1953
|
else {
|
|
@@ -1723,8 +1964,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1723
1964
|
this._seen.delete(runId);
|
|
1724
1965
|
return;
|
|
1725
1966
|
}
|
|
1726
|
-
if (outcome?.kind === 'handoff')
|
|
1967
|
+
if (outcome?.kind === 'handoff') {
|
|
1727
1968
|
return;
|
|
1969
|
+
}
|
|
1728
1970
|
await transcript.status({
|
|
1729
1971
|
state: 'failed',
|
|
1730
1972
|
reason: outcome?.reason ??
|
|
@@ -1747,7 +1989,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1747
1989
|
});
|
|
1748
1990
|
const map = {};
|
|
1749
1991
|
for (const row of configs ?? []) {
|
|
1750
|
-
if (row?.toolName && row.settings
|
|
1992
|
+
if (row?.toolName && isRecord(row.settings)) {
|
|
1751
1993
|
map[row.toolName] = row.settings;
|
|
1752
1994
|
}
|
|
1753
1995
|
}
|
|
@@ -1788,8 +2030,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1788
2030
|
})
|
|
1789
2031
|
.at(-1)?.id;
|
|
1790
2032
|
}
|
|
1791
|
-
if (!attachmentId)
|
|
2033
|
+
if (!attachmentId) {
|
|
1792
2034
|
return null;
|
|
2035
|
+
}
|
|
1793
2036
|
const stream = engine.fileRead({ chatId, fileId: attachmentId, metadataOnly: meta });
|
|
1794
2037
|
const abort = () => stream.destroy();
|
|
1795
2038
|
const pieces = [];
|
|
@@ -1797,19 +2040,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1797
2040
|
stream.on('error', noop);
|
|
1798
2041
|
signal?.addEventListener('abort', abort, { once: true });
|
|
1799
2042
|
try {
|
|
1800
|
-
if (signal?.aborted)
|
|
2043
|
+
if (signal?.aborted) {
|
|
1801
2044
|
throw new AbortedError();
|
|
2045
|
+
}
|
|
1802
2046
|
for await (const frame of stream) {
|
|
1803
2047
|
if (frame.metadata) {
|
|
1804
2048
|
const { id, fileName, mimeType, byteLength } = frame.metadata;
|
|
1805
2049
|
head = { id, fileName, mimeType, byteLength };
|
|
1806
2050
|
}
|
|
1807
|
-
if (frame.data)
|
|
2051
|
+
if (frame.data) {
|
|
1808
2052
|
pieces.push(frame.data);
|
|
2053
|
+
}
|
|
1809
2054
|
}
|
|
1810
|
-
if (signal?.aborted)
|
|
2055
|
+
if (signal?.aborted) {
|
|
1811
2056
|
throw new AbortedError();
|
|
1812
|
-
|
|
2057
|
+
}
|
|
2058
|
+
return head && (meta ? head : { ...head, bytes: b4a.concat(pieces) });
|
|
1813
2059
|
}
|
|
1814
2060
|
finally {
|
|
1815
2061
|
signal?.removeEventListener('abort', abort);
|
|
@@ -1832,29 +2078,30 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1832
2078
|
return null;
|
|
1833
2079
|
}
|
|
1834
2080
|
}
|
|
1835
|
-
async _uploadGenerated(
|
|
2081
|
+
async _uploadGenerated({ fileName, mimeType, bytes, raw }, { meshId, chatId, runId, deviceId }) {
|
|
1836
2082
|
const duplex = this._engine.uploadAttachment();
|
|
1837
2083
|
duplex.write({ chatId, runId, originDeviceId: deviceId, fileName, mimeType, raw, meshId });
|
|
1838
|
-
const buf =
|
|
1839
|
-
? bytes
|
|
1840
|
-
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
2084
|
+
const buf = b4a.toBuffer(bytes);
|
|
1841
2085
|
for (let i = 0; i < buf.length; i += UPLOAD_FRAME_BYTES) {
|
|
1842
2086
|
duplex.write({ data: buf.subarray(i, i + UPLOAD_FRAME_BYTES) });
|
|
1843
2087
|
}
|
|
1844
2088
|
duplex.end();
|
|
1845
2089
|
let attachment = null;
|
|
1846
2090
|
for await (const frame of duplex) {
|
|
1847
|
-
if (frame.attachment)
|
|
2091
|
+
if (frame.attachment) {
|
|
1848
2092
|
attachment = frame.attachment;
|
|
2093
|
+
}
|
|
1849
2094
|
}
|
|
1850
|
-
if (!attachment)
|
|
2095
|
+
if (!attachment) {
|
|
1851
2096
|
throw new Error('attachment upload ended without a record');
|
|
2097
|
+
}
|
|
1852
2098
|
return attachment;
|
|
1853
2099
|
}
|
|
1854
2100
|
async _hydrateAudio(messages, signal) {
|
|
1855
2101
|
const turns = messages.filter((turn) => turn.audioAttachments?.length);
|
|
1856
|
-
if (!turns.length)
|
|
2102
|
+
if (!turns.length) {
|
|
1857
2103
|
return;
|
|
2104
|
+
}
|
|
1858
2105
|
for (const turn of turns) {
|
|
1859
2106
|
for (const ref of turn.audioAttachments ?? []) {
|
|
1860
2107
|
if (signal.aborted)
|
|
@@ -1884,15 +2131,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1884
2131
|
const model = mediaModelNameFor('transcription', await this._persistedProfile());
|
|
1885
2132
|
await this.ensureMediaModelUnpaused('transcription', model);
|
|
1886
2133
|
await this._downloads?.waitUntilCached(model, signal);
|
|
1887
|
-
const text = (await this._harness.transcribe({ model: '', audio: bytes, signal })).trim();
|
|
2134
|
+
const text = (await this._harness.transcribe({ model: '', audio: b4a.toBuffer(bytes), signal })).trim();
|
|
1888
2135
|
this._audioTranscripts.set(ref.blobId, text);
|
|
1889
|
-
if (!text)
|
|
2136
|
+
if (!text) {
|
|
1890
2137
|
return silentAudioNotice(ref.fileName);
|
|
2138
|
+
}
|
|
1891
2139
|
return audioTranscriptBlock(ref.fileName, text);
|
|
1892
2140
|
}
|
|
1893
2141
|
catch (err) {
|
|
1894
|
-
if (signal.aborted)
|
|
2142
|
+
if (signal.aborted) {
|
|
1895
2143
|
throw err;
|
|
2144
|
+
}
|
|
1896
2145
|
this._log?.warn('assistant', `audio transcription failed — ${ref.fileName}`, err);
|
|
1897
2146
|
return unheardAudioNotice(label, 'transcription failed on this device');
|
|
1898
2147
|
}
|
|
@@ -1900,57 +2149,177 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1900
2149
|
async _hydrateImages(messages, signal, runId) {
|
|
1901
2150
|
const unreadable = new Map();
|
|
1902
2151
|
const turns = messages.filter((turn) => turn.images?.length);
|
|
1903
|
-
if (!turns.length)
|
|
2152
|
+
if (!turns.length) {
|
|
1904
2153
|
return unreadable;
|
|
2154
|
+
}
|
|
1905
2155
|
const inUse = new Set(turns.flatMap((turn) => (turn.images ?? []).map(imageCacheName)));
|
|
1906
2156
|
this._runImageCacheNames.set(runId, inUse);
|
|
1907
2157
|
const dir = this._imageCacheDir;
|
|
1908
2158
|
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
1909
2159
|
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)
|
|
2160
|
+
const dropped = await this._hydrateTurnImages(turn, { dir, signal });
|
|
2161
|
+
if (dropped.length) {
|
|
1924
2162
|
unreadable.set(turn, dropped);
|
|
2163
|
+
}
|
|
1925
2164
|
delete turn.images;
|
|
1926
2165
|
}
|
|
1927
2166
|
return unreadable;
|
|
1928
2167
|
}
|
|
2168
|
+
async _hydrateTurnImages(turn, { dir, signal }) {
|
|
2169
|
+
const dropped = [];
|
|
2170
|
+
for (const image of turn.images ?? []) {
|
|
2171
|
+
try {
|
|
2172
|
+
const file = await this._hydrateImage(dir, image, signal);
|
|
2173
|
+
turn.attachments = [...(turn.attachments ?? []), { path: file }];
|
|
2174
|
+
}
|
|
2175
|
+
catch (err) {
|
|
2176
|
+
if (signal.aborted) {
|
|
2177
|
+
throw err;
|
|
2178
|
+
}
|
|
2179
|
+
this._log?.error('assistant', `image hydration failed — skipped ${image.fileName}`, err);
|
|
2180
|
+
dropped.push(image.fileName);
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
return dropped;
|
|
2184
|
+
}
|
|
1929
2185
|
async _hydrateImage(dir, image, signal) {
|
|
1930
2186
|
const file = path.join(dir, imageCacheName(image));
|
|
1931
|
-
|
|
1932
|
-
if (!cached?.size) {
|
|
2187
|
+
await cacheOrTouch(file, async () => {
|
|
1933
2188
|
const raw = await this._readBlob(image.url, signal);
|
|
1934
|
-
if (signal.aborted)
|
|
2189
|
+
if (signal.aborted) {
|
|
1935
2190
|
throw new Error('hydration aborted');
|
|
1936
|
-
|
|
2191
|
+
}
|
|
2192
|
+
if (!raw) {
|
|
1937
2193
|
throw new Error(`empty download for ${image.fileName}`);
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
2194
|
+
}
|
|
2195
|
+
return normalizeImageForModel(raw);
|
|
2196
|
+
});
|
|
2197
|
+
return file;
|
|
2198
|
+
}
|
|
2199
|
+
async _hydratePdfPages(messages, signal, runId) {
|
|
2200
|
+
const rendered = new Set();
|
|
2201
|
+
const turns = messages.filter((turn) => turn.pdfs?.length);
|
|
2202
|
+
if (!turns.length) {
|
|
2203
|
+
return rendered;
|
|
1942
2204
|
}
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
2205
|
+
const dir = this._imageCacheDir;
|
|
2206
|
+
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
2207
|
+
const pinned = this._runImageCacheNames.get(runId) ?? new Set();
|
|
2208
|
+
this._runImageCacheNames.set(runId, pinned);
|
|
2209
|
+
const latestUser = messages.findLast((turn) => turn.role === 'user');
|
|
2210
|
+
for (const turn of turns) {
|
|
2211
|
+
let budget = MAX_PDF_PAGE_IMAGES;
|
|
2212
|
+
for (const pdf of turn.pdfs ?? []) {
|
|
2213
|
+
if (budget <= 0)
|
|
2214
|
+
break;
|
|
2215
|
+
const pages = await this._tryHydratePdf(dir, turn, {
|
|
2216
|
+
pdf,
|
|
2217
|
+
pinned,
|
|
2218
|
+
maxPages: budget,
|
|
2219
|
+
signal
|
|
2220
|
+
});
|
|
2221
|
+
budget -= pages.tried;
|
|
2222
|
+
if (turn === latestUser && pages.names.length) {
|
|
2223
|
+
rendered.add(pdf.id);
|
|
2224
|
+
}
|
|
2225
|
+
}
|
|
2226
|
+
delete turn.pdfs;
|
|
2227
|
+
}
|
|
2228
|
+
return rendered;
|
|
2229
|
+
}
|
|
2230
|
+
async _tryHydratePdf(dir, turn, opts) {
|
|
2231
|
+
try {
|
|
2232
|
+
return await this._hydratePdf(dir, turn, opts);
|
|
2233
|
+
}
|
|
2234
|
+
catch (err) {
|
|
2235
|
+
if (opts.signal.aborted) {
|
|
2236
|
+
throw err;
|
|
2237
|
+
}
|
|
2238
|
+
this._log?.warn('assistant', `pdf rasterization failed — ${opts.pdf.fileName}`, err);
|
|
2239
|
+
return { names: [], tried: 0 };
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
async _hydratePdf(dir, turn, { pdf, pinned, maxPages, signal }) {
|
|
2243
|
+
const key = `${pdf.blobId}:${maxPages}`;
|
|
2244
|
+
const memo = this._pdfPages.get(key);
|
|
2245
|
+
if (memo) {
|
|
2246
|
+
for (const name of memo.names) {
|
|
2247
|
+
pinned.add(name);
|
|
2248
|
+
}
|
|
2249
|
+
const files = memo.names.map((name) => path.join(dir, name));
|
|
2250
|
+
if ((await Promise.all(files.map((file) => cacheOrTouch(file, noBytes)))).every(Boolean)) {
|
|
2251
|
+
attachPages(turn, files);
|
|
2252
|
+
return memo;
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
const pages = await this._renderPdf(dir, { pdf, pinned, maxPages, signal });
|
|
2256
|
+
if (!pages) {
|
|
2257
|
+
return { names: [], tried: 0 };
|
|
2258
|
+
}
|
|
2259
|
+
this._pdfPages.set(key, pages);
|
|
2260
|
+
attachPages(turn, pages.names.map((name) => path.join(dir, name)));
|
|
2261
|
+
return pages;
|
|
2262
|
+
}
|
|
2263
|
+
async _renderPdf(dir, { pdf, pinned, maxPages, signal }) {
|
|
2264
|
+
const bytes = await this._readBlob(pdf.url, signal);
|
|
2265
|
+
if (signal.aborted) {
|
|
2266
|
+
throw new Error('hydration aborted');
|
|
2267
|
+
}
|
|
2268
|
+
if (!bytes) {
|
|
2269
|
+
return null;
|
|
2270
|
+
}
|
|
2271
|
+
const doc = await this._pdfRasterizer.open(bytes);
|
|
2272
|
+
if (!doc) {
|
|
2273
|
+
return { names: [], tried: 0 };
|
|
2274
|
+
}
|
|
2275
|
+
try {
|
|
2276
|
+
const names = [];
|
|
2277
|
+
let tried = 0;
|
|
2278
|
+
const count = doc.pageCount();
|
|
2279
|
+
for (let page = 0; page < count && tried < maxPages; page++) {
|
|
2280
|
+
if (signal.aborted)
|
|
2281
|
+
throw new Error('hydration aborted');
|
|
2282
|
+
const outcome = await this._renderPdfPage(doc, { dir, pdf, page, pinned, signal });
|
|
2283
|
+
if (outcome.tried) {
|
|
2284
|
+
tried++;
|
|
2285
|
+
}
|
|
2286
|
+
if (outcome.name) {
|
|
2287
|
+
names.push(outcome.name);
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
return { names, tried };
|
|
2291
|
+
}
|
|
2292
|
+
finally {
|
|
2293
|
+
doc.close();
|
|
2294
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
async _renderPdfPage(doc, { dir, pdf, page, pinned, signal }) {
|
|
2297
|
+
let tried = false;
|
|
2298
|
+
try {
|
|
2299
|
+
const flags = doc.pageFlags(page);
|
|
2300
|
+
if (flags.hasText && !flags.hasImage) {
|
|
2301
|
+
return { tried };
|
|
2302
|
+
}
|
|
2303
|
+
tried = true;
|
|
2304
|
+
const name = pdfPageCacheName(pdf, page);
|
|
2305
|
+
pinned.add(name);
|
|
2306
|
+
const cached = await cacheOrTouch(path.join(dir, name), () => doc.renderPage(page));
|
|
2307
|
+
return { tried, name: cached ? name : undefined };
|
|
2308
|
+
}
|
|
2309
|
+
catch (err) {
|
|
2310
|
+
if (signal.aborted) {
|
|
2311
|
+
throw err;
|
|
2312
|
+
}
|
|
2313
|
+
this._log?.warn('assistant', `pdf page skipped — ${pdf.fileName} p${page}`, err);
|
|
2314
|
+
return { tried };
|
|
1946
2315
|
}
|
|
1947
|
-
return file;
|
|
1948
2316
|
}
|
|
1949
2317
|
async _compactIfNearFull(params) {
|
|
1950
2318
|
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
1951
2319
|
const metrics = await this._lastRunMetrics(chatId, meshId);
|
|
1952
|
-
if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize))
|
|
2320
|
+
if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize)) {
|
|
1953
2321
|
return false;
|
|
2322
|
+
}
|
|
1954
2323
|
if (!canCompact(metrics.ctxSize)) {
|
|
1955
2324
|
this._log?.debug('runs', 'chat near its window but under the compaction floor', {
|
|
1956
2325
|
chatId,
|
|
@@ -2007,7 +2376,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2007
2376
|
}
|
|
2008
2377
|
}
|
|
2009
2378
|
async _lastRunMetrics(chatId, meshId) {
|
|
2010
|
-
const deviceId = this.
|
|
2379
|
+
const deviceId = this._requireDeviceId();
|
|
2011
2380
|
const { chunks } = await meshScoped(this._engine, meshId)
|
|
2012
2381
|
.listChunks({ chatId, type: 'run-status' })
|
|
2013
2382
|
.catch(() => ({ chunks: [] }));
|
|
@@ -2015,12 +2384,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2015
2384
|
for (let index = statuses.length - 1; index >= 0; index--) {
|
|
2016
2385
|
const chunk = statuses[index];
|
|
2017
2386
|
const metrics = chunk.runStatus?.metrics;
|
|
2018
|
-
if (chunk.runStatus?.state !== 'completed' || !metrics)
|
|
2387
|
+
if (chunk.runStatus?.state !== 'completed' || !metrics) {
|
|
2019
2388
|
continue;
|
|
2020
|
-
|
|
2389
|
+
}
|
|
2390
|
+
if (!chunk.originDeviceId || !b4a.equals(chunk.originDeviceId, deviceId)) {
|
|
2391
|
+
return {};
|
|
2392
|
+
}
|
|
2393
|
+
const parsed = RunOccupancySchema.safeParse(metrics);
|
|
2394
|
+
if (!parsed.success) {
|
|
2021
2395
|
return {};
|
|
2022
|
-
|
|
2023
|
-
return { cacheTokens: parsed.cacheTokens, ctxSize: parsed.ctxSize };
|
|
2396
|
+
}
|
|
2397
|
+
return { cacheTokens: parsed.data.cacheTokens, ctxSize: parsed.data.ctxSize };
|
|
2024
2398
|
}
|
|
2025
2399
|
return {};
|
|
2026
2400
|
}
|
|
@@ -2028,12 +2402,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2028
2402
|
if (!this.opened)
|
|
2029
2403
|
await this.ready();
|
|
2030
2404
|
const engine = meshScoped(this._engine, options.meshId);
|
|
2031
|
-
const deviceId = this.
|
|
2405
|
+
const deviceId = this._requireDeviceId();
|
|
2032
2406
|
const chat = await engine.getChat({ chatId }).catch(noop);
|
|
2033
|
-
if (!chat)
|
|
2407
|
+
if (!chat) {
|
|
2034
2408
|
return { compacted: false, reason: 'unknown chat' };
|
|
2409
|
+
}
|
|
2035
2410
|
const agent = await engine.getAgent({ agentId: chat.agentId }).catch(noop);
|
|
2036
|
-
if (!agent || !agent.providerDeviceId
|
|
2411
|
+
if (!agent || !agent.providerDeviceId || !b4a.equals(agent.providerDeviceId, deviceId)) {
|
|
2037
2412
|
return { compacted: false, reason: 'not the provider device' };
|
|
2038
2413
|
}
|
|
2039
2414
|
const model = runnableModelName(agent.modelName, await this._ensureCapability(), this._mobileClass);
|
|
@@ -2056,20 +2431,27 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2056
2431
|
const senders = await this._senderNames(engine, chunks);
|
|
2057
2432
|
for (;;) {
|
|
2058
2433
|
const boundary = compactionBoundary(chunks, keepTurns);
|
|
2059
|
-
if (!boundary)
|
|
2434
|
+
if (!boundary) {
|
|
2060
2435
|
return { compacted: false, reason: 'nothing-to-fold' };
|
|
2436
|
+
}
|
|
2061
2437
|
const through = chunks.find((chunk) => chunk.id === boundary.throughId);
|
|
2062
|
-
if (!through)
|
|
2438
|
+
if (!through) {
|
|
2063
2439
|
return { compacted: false, reason: 'nothing-to-fold' };
|
|
2064
|
-
|
|
2440
|
+
}
|
|
2441
|
+
if (through.seq <= lastThroughSeq) {
|
|
2065
2442
|
return { compacted: false, reason: 'already-compacted' };
|
|
2443
|
+
}
|
|
2066
2444
|
const inRange = chunks.filter((chunk) => chunk.seq <= through.seq);
|
|
2067
2445
|
const folded = historyFromChunks(inRange, staleToolRunIds(inRange, priorAsks ?? [], ''), senders);
|
|
2068
2446
|
if (!announced) {
|
|
2069
2447
|
await options.onFold?.();
|
|
2070
2448
|
announced = true;
|
|
2071
2449
|
}
|
|
2072
|
-
const attempt = await this._summarize(folded,
|
|
2450
|
+
const attempt = await this._summarize(folded, {
|
|
2451
|
+
model,
|
|
2452
|
+
agent: withConfigDefaults(agent, config),
|
|
2453
|
+
signal: options.signal
|
|
2454
|
+
});
|
|
2073
2455
|
if (!attempt.summary) {
|
|
2074
2456
|
if (attempt.overflowed && boundary.foldedTurns > 1) {
|
|
2075
2457
|
keepTurns = userTurns - Math.floor(boundary.foldedTurns / 2);
|
|
@@ -2081,8 +2463,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2081
2463
|
if (!foldReducesPrompt(folded, summary)) {
|
|
2082
2464
|
return { compacted: false, reason: 'summary-not-smaller' };
|
|
2083
2465
|
}
|
|
2084
|
-
if (options.signal?.aborted)
|
|
2466
|
+
if (options.signal?.aborted) {
|
|
2085
2467
|
return { compacted: false, reason: 'canceled' };
|
|
2468
|
+
}
|
|
2086
2469
|
await engine.appendChunk({
|
|
2087
2470
|
chatId,
|
|
2088
2471
|
type: 'compaction',
|
|
@@ -2098,7 +2481,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2098
2481
|
return { compacted: true, foldedTurns: boundary.foldedTurns };
|
|
2099
2482
|
}
|
|
2100
2483
|
}
|
|
2101
|
-
async _summarize(turns, model, agent, signal) {
|
|
2484
|
+
async _summarize(turns, { model, agent, signal }) {
|
|
2102
2485
|
const hasPriorSummary = turns[0]?.content.startsWith(COMPACTION_TURN_PREFIX) === true;
|
|
2103
2486
|
const events = this._harnessFor(agent).completion({
|
|
2104
2487
|
model,
|
|
@@ -2121,8 +2504,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2121
2504
|
let text = '';
|
|
2122
2505
|
let failed = null;
|
|
2123
2506
|
for await (const event of events) {
|
|
2124
|
-
if (event.type === 'content' && typeof event.text === 'string')
|
|
2507
|
+
if (event.type === 'content' && typeof event.text === 'string') {
|
|
2125
2508
|
text += event.text;
|
|
2509
|
+
}
|
|
2126
2510
|
else if (event.type === 'error') {
|
|
2127
2511
|
failed = typeof event.message === 'string' ? event.message : 'summarizer failed';
|
|
2128
2512
|
break;
|
|
@@ -2131,8 +2515,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2131
2515
|
failed = 'summarizer aborted';
|
|
2132
2516
|
break;
|
|
2133
2517
|
}
|
|
2134
|
-
else if (event.type === 'done')
|
|
2518
|
+
else if (event.type === 'done') {
|
|
2135
2519
|
break;
|
|
2520
|
+
}
|
|
2136
2521
|
}
|
|
2137
2522
|
if (failed !== null) {
|
|
2138
2523
|
this._log?.warn('runs', 'summarizer failed mid-stream', { chars: text.length, failed });
|
|
@@ -2140,14 +2525,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2140
2525
|
}
|
|
2141
2526
|
return { summary: parseCompactionSummary(text), overflowed: false };
|
|
2142
2527
|
}
|
|
2143
|
-
async _autoTitle(engine, chatId, messages, model, agent, probe) {
|
|
2528
|
+
async _autoTitle(engine, { chatId, messages, model, agent, probe }) {
|
|
2144
2529
|
try {
|
|
2145
2530
|
const first = messages.find((turn) => turn.role === 'user' && turn.content.trim());
|
|
2146
|
-
if (!first)
|
|
2531
|
+
if (!first) {
|
|
2147
2532
|
return;
|
|
2533
|
+
}
|
|
2148
2534
|
const chat = await engine.getChat({ chatId });
|
|
2149
|
-
if (!isDefaultChatTitle(chat?.title))
|
|
2535
|
+
if (!isDefaultChatTitle(chat?.title)) {
|
|
2150
2536
|
return;
|
|
2537
|
+
}
|
|
2151
2538
|
await this._generateChatTitle({ engine, chatId, input: first.content, model, agent, probe });
|
|
2152
2539
|
}
|
|
2153
2540
|
catch (err) {
|
|
@@ -2157,8 +2544,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2157
2544
|
async _generateChatTitle(params) {
|
|
2158
2545
|
const { engine, chatId, input, model, agent, probe } = params;
|
|
2159
2546
|
const prompt = input.trim();
|
|
2160
|
-
if (!prompt)
|
|
2547
|
+
if (!prompt) {
|
|
2161
2548
|
return null;
|
|
2549
|
+
}
|
|
2162
2550
|
let generated = null;
|
|
2163
2551
|
if (probe && !this.closing) {
|
|
2164
2552
|
try {
|
|
@@ -2175,8 +2563,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2175
2563
|
}
|
|
2176
2564
|
}
|
|
2177
2565
|
const title = generated ?? deriveChatTitle(prompt);
|
|
2178
|
-
if (!title)
|
|
2566
|
+
if (!title) {
|
|
2179
2567
|
return null;
|
|
2568
|
+
}
|
|
2180
2569
|
await engine.renameChat({ chatId, config: { title } });
|
|
2181
2570
|
this._log?.info('runs', 'chat titled', {
|
|
2182
2571
|
chatId,
|
|
@@ -2215,19 +2604,21 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2215
2604
|
});
|
|
2216
2605
|
let text = '';
|
|
2217
2606
|
for await (const event of events) {
|
|
2218
|
-
if (event.type === 'content' && typeof event.text === 'string')
|
|
2607
|
+
if (event.type === 'content' && typeof event.text === 'string') {
|
|
2219
2608
|
text += event.text;
|
|
2609
|
+
}
|
|
2220
2610
|
else if (event.type === 'inference-call') {
|
|
2221
2611
|
void this._inferenceCalls
|
|
2222
2612
|
.record(chatId, { runId: TITLE_RUN_ID, at: Date.now(), call: asRecord(event.call) })
|
|
2223
2613
|
.catch((err) => this._log?.warn('assistant', 'title call not recorded', { err }));
|
|
2224
2614
|
}
|
|
2225
|
-
else if (event.type === 'error' || event.type === 'aborted' || event.type === 'done')
|
|
2615
|
+
else if (event.type === 'error' || event.type === 'aborted' || event.type === 'done') {
|
|
2226
2616
|
break;
|
|
2617
|
+
}
|
|
2227
2618
|
}
|
|
2228
2619
|
return parseTitleResponse(text);
|
|
2229
2620
|
}
|
|
2230
|
-
_createApprove(engine, chatId, runId, deviceId, agentId, hasRun) {
|
|
2621
|
+
_createApprove(engine, { chatId, runId, deviceId, agentId, hasRun }) {
|
|
2231
2622
|
const waiters = new Map();
|
|
2232
2623
|
let latestChunks = [];
|
|
2233
2624
|
let watcher = null;
|
|
@@ -2235,16 +2626,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2235
2626
|
const pump = async (source) => {
|
|
2236
2627
|
while (true) {
|
|
2237
2628
|
const { value, done } = await source.next();
|
|
2238
|
-
if (done)
|
|
2629
|
+
if (done) {
|
|
2239
2630
|
return;
|
|
2631
|
+
}
|
|
2240
2632
|
latestChunks = value.chunks ?? [];
|
|
2241
2633
|
for (const chunk of latestChunks) {
|
|
2242
2634
|
const status = chunk.approvalRequest?.status;
|
|
2243
|
-
if (!status || status === 'pending')
|
|
2635
|
+
if (!status || status === 'pending') {
|
|
2244
2636
|
continue;
|
|
2637
|
+
}
|
|
2245
2638
|
const waiter = waiters.get(chunk.id);
|
|
2246
|
-
if (!waiter)
|
|
2639
|
+
if (!waiter) {
|
|
2247
2640
|
continue;
|
|
2641
|
+
}
|
|
2248
2642
|
waiters.delete(chunk.id);
|
|
2249
2643
|
this._log?.info('approvals', `answer ${status}`, {
|
|
2250
2644
|
runId,
|
|
@@ -2264,10 +2658,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2264
2658
|
watcher = opened;
|
|
2265
2659
|
pump(opened)
|
|
2266
2660
|
.catch((err) => {
|
|
2267
|
-
if (closing)
|
|
2661
|
+
if (closing) {
|
|
2268
2662
|
this._log?.debug('assistant', 'approval watch closed', err);
|
|
2269
|
-
|
|
2663
|
+
}
|
|
2664
|
+
else {
|
|
2270
2665
|
this._log?.error('assistant', 'approval watch failed', err);
|
|
2666
|
+
}
|
|
2271
2667
|
})
|
|
2272
2668
|
.finally(() => {
|
|
2273
2669
|
for (const [id, waiter] of waiters) {
|
|
@@ -2278,8 +2674,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2278
2674
|
});
|
|
2279
2675
|
waiter.resolve(false);
|
|
2280
2676
|
}
|
|
2281
|
-
if (watcher === opened)
|
|
2677
|
+
if (watcher === opened) {
|
|
2282
2678
|
watcher = null;
|
|
2679
|
+
}
|
|
2283
2680
|
});
|
|
2284
2681
|
};
|
|
2285
2682
|
const asksForRun = async () => {
|
|
@@ -2290,8 +2687,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2290
2687
|
};
|
|
2291
2688
|
const sameCall = (chunk, call) => {
|
|
2292
2689
|
const ask = chunk.approvalRequest;
|
|
2293
|
-
if (!ask || ask.name !== call.name)
|
|
2690
|
+
if (!ask || ask.name !== call.name) {
|
|
2294
2691
|
return false;
|
|
2692
|
+
}
|
|
2295
2693
|
return ((ask.resource?.kind ?? null) === (call.resource?.kind ?? null) &&
|
|
2296
2694
|
(ask.resource?.label ?? null) === (call.resource?.label ?? null) &&
|
|
2297
2695
|
JSON.stringify(ask.arguments) === JSON.stringify(call.arguments));
|
|
@@ -2391,8 +2789,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2391
2789
|
});
|
|
2392
2790
|
await engine.resolveApproval({ chunkId: ask.id, approved: false });
|
|
2393
2791
|
}
|
|
2394
|
-
for (const waiter of waiters.values())
|
|
2792
|
+
for (const waiter of waiters.values()) {
|
|
2395
2793
|
waiter.resolve(false);
|
|
2794
|
+
}
|
|
2396
2795
|
}
|
|
2397
2796
|
waiters.clear();
|
|
2398
2797
|
closing = true;
|
|
@@ -2406,22 +2805,25 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2406
2805
|
if (reason === RUNNER_DIED && this._harnessDeath) {
|
|
2407
2806
|
return { kind: 'failed', reason: this._harnessDeath };
|
|
2408
2807
|
}
|
|
2409
|
-
if (known)
|
|
2808
|
+
if (known) {
|
|
2410
2809
|
return known;
|
|
2411
|
-
|
|
2810
|
+
}
|
|
2811
|
+
if (this.suspended) {
|
|
2412
2812
|
return { kind: 'replay' };
|
|
2813
|
+
}
|
|
2413
2814
|
if (reason !== undefined) {
|
|
2414
2815
|
this._log?.warn('runs', 'run aborted for an unrecognised reason', { runId, reason });
|
|
2415
2816
|
}
|
|
2416
2817
|
return { kind: 'failed', reason: RUN_FAILURE_REASONS.runtimeEndedTurn };
|
|
2417
2818
|
}
|
|
2418
|
-
_watchSettled(engine, chatId, runId, controller) {
|
|
2819
|
+
_watchSettled(engine, { chatId, runId, controller }) {
|
|
2419
2820
|
const it = engine.chunksWatch({ chatId, type: 'run-status' })[Symbol.asyncIterator]();
|
|
2420
2821
|
const watching = (async () => {
|
|
2421
2822
|
while (true) {
|
|
2422
2823
|
const { value, done } = await it.next();
|
|
2423
|
-
if (done)
|
|
2824
|
+
if (done) {
|
|
2424
2825
|
return;
|
|
2826
|
+
}
|
|
2425
2827
|
const settled = (value.chunks ?? []).some((c) => c.runId === runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
2426
2828
|
if (settled) {
|
|
2427
2829
|
controller.abort(RUN_SETTLED);
|
|
@@ -2441,15 +2843,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2441
2843
|
};
|
|
2442
2844
|
}
|
|
2443
2845
|
_watchOwnership(engine, agentId, controller) {
|
|
2444
|
-
const deviceId = this.
|
|
2846
|
+
const deviceId = this._requireDeviceId();
|
|
2445
2847
|
const it = engine.agentsWatch({})[Symbol.asyncIterator]();
|
|
2446
2848
|
const watching = (async () => {
|
|
2447
2849
|
while (true) {
|
|
2448
2850
|
const { value, done } = await it.next();
|
|
2449
|
-
if (done)
|
|
2851
|
+
if (done) {
|
|
2450
2852
|
return;
|
|
2853
|
+
}
|
|
2451
2854
|
const row = (value.agents ?? []).find((a) => a.id === agentId);
|
|
2452
|
-
if (!row ||
|
|
2855
|
+
if (!row ||
|
|
2856
|
+
row.deletedAt ||
|
|
2857
|
+
!row.providerDeviceId ||
|
|
2858
|
+
!b4a.equals(row.providerDeviceId, deviceId)) {
|
|
2453
2859
|
controller.abort(OWNERSHIP_LOST);
|
|
2454
2860
|
return;
|
|
2455
2861
|
}
|
|
@@ -2474,11 +2880,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2474
2880
|
return;
|
|
2475
2881
|
if (!('status' in state) || typeof state.status !== 'string')
|
|
2476
2882
|
return;
|
|
2477
|
-
if (!('model' in state) || typeof state.model !== 'string')
|
|
2883
|
+
if (!('model' in state) || typeof state.model !== 'string') {
|
|
2478
2884
|
return;
|
|
2885
|
+
}
|
|
2479
2886
|
const { status, model } = state;
|
|
2480
|
-
if (status === 'downloading' || this._modelStatusLogged.get(model) === status)
|
|
2887
|
+
if (status === 'downloading' || this._modelStatusLogged.get(model) === status) {
|
|
2481
2888
|
return;
|
|
2889
|
+
}
|
|
2482
2890
|
this._modelStatusLogged.set(model, status);
|
|
2483
2891
|
this._log?.info('models', `model ${status}`, { model });
|
|
2484
2892
|
}
|
|
@@ -2500,11 +2908,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2500
2908
|
const runStatus = progress
|
|
2501
2909
|
? { state: 'executing', progress }
|
|
2502
2910
|
: { state: 'executing' };
|
|
2503
|
-
if (documents)
|
|
2911
|
+
if (documents) {
|
|
2504
2912
|
runStatus.reason = 'preparing-documents';
|
|
2913
|
+
}
|
|
2505
2914
|
const key = JSON.stringify(runStatus);
|
|
2506
|
-
if (key === last)
|
|
2915
|
+
if (key === last) {
|
|
2507
2916
|
return;
|
|
2917
|
+
}
|
|
2508
2918
|
last = key;
|
|
2509
2919
|
showing = progress !== null || documents;
|
|
2510
2920
|
update(runStatus).catch(noop);
|
|
@@ -2512,17 +2922,20 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2512
2922
|
const onModel = (state) => {
|
|
2513
2923
|
if (sampling)
|
|
2514
2924
|
return;
|
|
2515
|
-
if (state.status === 'loading')
|
|
2925
|
+
if (state.status === 'loading') {
|
|
2516
2926
|
apply({ phase: 'loading', model: state.model });
|
|
2927
|
+
}
|
|
2517
2928
|
else if (state.status === 'downloading') {
|
|
2518
2929
|
const percentage = Math.floor((state.percentage ?? 0) / 10) * 10;
|
|
2519
2930
|
apply({ phase: 'downloading', model: state.model, percentage });
|
|
2520
2931
|
}
|
|
2521
|
-
else
|
|
2932
|
+
else {
|
|
2522
2933
|
apply(transcribing ? { phase: 'transcribing' } : null);
|
|
2934
|
+
}
|
|
2523
2935
|
};
|
|
2524
|
-
if (this._harness.on && this._harness.off)
|
|
2936
|
+
if (this._harness.on && this._harness.off) {
|
|
2525
2937
|
this._harness.on('model', onModel);
|
|
2938
|
+
}
|
|
2526
2939
|
return {
|
|
2527
2940
|
documents: (active) => {
|
|
2528
2941
|
documents = active;
|
|
@@ -2534,15 +2947,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2534
2947
|
},
|
|
2535
2948
|
image: (progress) => {
|
|
2536
2949
|
sampling = progress !== null;
|
|
2537
|
-
if (!progress)
|
|
2950
|
+
if (!progress) {
|
|
2538
2951
|
return apply(null);
|
|
2952
|
+
}
|
|
2539
2953
|
apply({ phase: 'generating', percentage: Math.floor(progress.percentage / 10) * 10 });
|
|
2540
2954
|
},
|
|
2541
2955
|
stop: () => this._harness.off?.('model', onModel),
|
|
2542
2956
|
clear: () => {
|
|
2543
2957
|
sampling = false;
|
|
2544
|
-
if (!showing)
|
|
2958
|
+
if (!showing) {
|
|
2545
2959
|
return;
|
|
2960
|
+
}
|
|
2546
2961
|
showing = false;
|
|
2547
2962
|
last = JSON.stringify({ state: 'executing' });
|
|
2548
2963
|
update({ state: 'executing' }).catch(noop);
|
|
@@ -2550,6 +2965,37 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2550
2965
|
};
|
|
2551
2966
|
}
|
|
2552
2967
|
}
|
|
2968
|
+
async function injectRetrievedKnowledge(knowledge, { transcript, sourceTagFilter, retrievedRecords, injected }) {
|
|
2969
|
+
const records = sourcesFromHits(knowledge.hits, RAG_SOURCE_BOUNDS);
|
|
2970
|
+
const notice = chooseRagNotice({
|
|
2971
|
+
errorMessage: knowledge.search.errorMessage,
|
|
2972
|
+
hitCount: records.length,
|
|
2973
|
+
searchedCount: knowledge.search.searchedCount ?? 0,
|
|
2974
|
+
indexEmpty: Boolean(knowledge.search.indexEmpty),
|
|
2975
|
+
hasKnowledge: Boolean(knowledge.search.hasKnowledge),
|
|
2976
|
+
pendingReferenced: Boolean(knowledge.search.pendingReferenced),
|
|
2977
|
+
failedFiles: knowledge.search.failedFiles ?? [],
|
|
2978
|
+
pendingFiles: knowledge.search.pendingFiles ?? []
|
|
2979
|
+
});
|
|
2980
|
+
sourceTagFilter.add(records.map((record) => record.title));
|
|
2981
|
+
retrievedRecords.push(...records.map((record, index) => ({
|
|
2982
|
+
...record,
|
|
2983
|
+
excerpt: knowledge.hits[index]?.content ?? record.excerpt
|
|
2984
|
+
})));
|
|
2985
|
+
await transcript.chunk({
|
|
2986
|
+
type: 'sources',
|
|
2987
|
+
sources: { sources: records, notice: notice ?? undefined }
|
|
2988
|
+
});
|
|
2989
|
+
injected.sourcesWritten = true;
|
|
2990
|
+
if (records.length > 0) {
|
|
2991
|
+
knowledge.lastUser.content = `${formatRagContext(knowledge.hits, knowledge.search.availableFiles ?? [])}\n\nThe user's message:\n${knowledge.lastUser.content}\n\n${RAG_CITATION_LINE}`;
|
|
2992
|
+
injected.ragInjected = true;
|
|
2993
|
+
injected.documentsInjected = true;
|
|
2994
|
+
}
|
|
2995
|
+
else if (knowledge.search.availableFiles?.length) {
|
|
2996
|
+
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.]`;
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2553
2999
|
function noop() { }
|
|
2554
3000
|
function toolCallChunk(event) {
|
|
2555
3001
|
return {
|
|
@@ -2580,8 +3026,8 @@ function runMetricsSummary(metrics) {
|
|
|
2580
3026
|
};
|
|
2581
3027
|
}
|
|
2582
3028
|
function asRecord(value) {
|
|
2583
|
-
return
|
|
3029
|
+
return isRecord(value) ? { ...value } : {};
|
|
2584
3030
|
}
|
|
2585
3031
|
function isStreamDestroyed(err) {
|
|
2586
|
-
return
|
|
3032
|
+
return isRecord(err) && err.code === 'STREAM_DESTROYED';
|
|
2587
3033
|
}
|