@qvac/core 0.2.1 → 0.3.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/.claude/skills/compaction/SKILL.md +15 -6
- package/.claude/skills/js-house-style/SKILL.md +1 -0
- package/.claude/skills/pr-create/SKILL.md +66 -3
- package/.claude/skills/pr-review/SKILL.md +252 -40
- package/.claude/skills/release/SKILL.md +122 -0
- package/CLAUDE.md +8 -5
- package/README.md +3 -1
- package/dist-lib/client.js +1 -1
- package/dist-lib/index.js +8 -8
- package/dist-lib/lib/api/agents.d.ts +1 -1
- package/dist-lib/lib/api/agents.js +10 -10
- package/dist-lib/lib/api/app-config.js +1 -1
- package/dist-lib/lib/api/approval-rules.js +9 -5
- package/dist-lib/lib/api/attachments.js +25 -3
- package/dist-lib/lib/api/chat-groups.d.ts +1 -1
- package/dist-lib/lib/api/chat-groups.js +5 -4
- package/dist-lib/lib/api/chat.js +10 -10
- package/dist-lib/lib/api/chunks.d.ts +1 -1
- package/dist-lib/lib/api/chunks.js +11 -21
- package/dist-lib/lib/api/config.js +1 -1
- package/dist-lib/lib/api/current-mesh.d.ts +16 -1
- package/dist-lib/lib/api/current-mesh.js +18 -0
- package/dist-lib/lib/api/custom-models.js +2 -2
- package/dist-lib/lib/api/devices.d.ts +1 -1
- package/dist-lib/lib/api/devices.js +12 -9
- package/dist-lib/lib/api/diagnostics.js +1 -1
- package/dist-lib/lib/api/effective-ctx-size.js +1 -1
- package/dist-lib/lib/api/execution.js +4 -4
- package/dist-lib/lib/api/files.js +2 -2
- package/dist-lib/lib/api/index.d.ts +1 -0
- package/dist-lib/lib/api/index.js +34 -34
- package/dist-lib/lib/api/instrument.js +1 -1
- package/dist-lib/lib/api/knowledge.js +13 -9
- package/dist-lib/lib/api/logs.js +1 -1
- package/dist-lib/lib/api/members.d.ts +4 -1
- package/dist-lib/lib/api/members.js +87 -18
- package/dist-lib/lib/api/mesh.d.ts +13 -3
- package/dist-lib/lib/api/mesh.js +25 -2
- package/dist-lib/lib/api/model-download.js +1 -1
- package/dist-lib/lib/api/network.js +1 -1
- package/dist-lib/lib/api/notifications.js +3 -3
- package/dist-lib/lib/api/rag.js +7 -7
- package/dist-lib/lib/api/sync.d.ts +1 -1
- package/dist-lib/lib/api/sync.js +3 -2
- package/dist-lib/lib/api/tool-activity.js +5 -5
- package/dist-lib/lib/api/tool-configs.js +5 -5
- package/dist-lib/lib/api/watchable.d.ts +6 -1
- package/dist-lib/lib/api/watchable.js +14 -3
- package/dist-lib/lib/assistant/fetch-body.d.ts +5 -0
- package/dist-lib/lib/assistant/fetch-body.js +36 -0
- package/dist-lib/lib/assistant/harness.d.ts +225 -0
- package/dist-lib/lib/assistant/harness.js +154 -0
- package/dist-lib/lib/assistant/history.d.ts +31 -0
- package/dist-lib/lib/assistant/history.js +226 -0
- package/dist-lib/lib/assistant/message.d.ts +45 -0
- package/dist-lib/lib/assistant/message.js +47 -0
- package/dist-lib/lib/assistant/skill-encode.d.ts +21 -0
- package/dist-lib/lib/assistant/skill-encode.js +71 -0
- package/dist-lib/lib/assistant-client.js +2 -2
- package/dist-lib/lib/assistant-tree.js +10 -4
- package/dist-lib/lib/assistant.d.ts +28 -292
- package/dist-lib/lib/assistant.js +284 -716
- package/dist-lib/lib/audio-format.js +2 -2
- package/dist-lib/lib/client.js +2 -2
- package/dist-lib/lib/collabora/convert.js +1 -1
- package/dist-lib/lib/collabora/entry.js +1 -1
- package/dist-lib/lib/collabora/worker.js +1 -1
- package/dist-lib/lib/compaction.d.ts +1 -2
- package/dist-lib/lib/compaction.js +8 -6
- package/dist-lib/lib/constants.js +1 -1
- package/dist-lib/lib/core/account-recovery.d.ts +19 -0
- package/dist-lib/lib/core/account-recovery.js +50 -0
- package/dist-lib/lib/core/agent.js +8 -4
- package/dist-lib/lib/core/app-build.d.ts +1 -1
- package/dist-lib/lib/core/approval-rules.d.ts +1 -0
- package/dist-lib/lib/core/approval-rules.js +3 -1
- package/dist-lib/lib/core/archived-mesh.d.ts +25 -0
- package/dist-lib/lib/core/archived-mesh.js +162 -0
- package/dist-lib/lib/core/chat-activity.js +1 -1
- package/dist-lib/lib/core/chat-group.d.ts +1 -0
- package/dist-lib/lib/core/chat-group.js +24 -4
- package/dist-lib/lib/core/chat-search.js +2 -2
- package/dist-lib/lib/core/chat.js +5 -5
- package/dist-lib/lib/core/chunk.d.ts +2 -1
- package/dist-lib/lib/core/chunk.js +31 -4
- package/dist-lib/lib/core/contract/agents.js +1 -1
- package/dist-lib/lib/core/contract/approval-rules.js +6 -4
- 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 +2 -2
- package/dist-lib/lib/core/contract/compute.d.ts +11 -0
- package/dist-lib/lib/core/contract/compute.js +45 -0
- package/dist-lib/lib/core/contract/devices.js +19 -5
- package/dist-lib/lib/core/contract/index.d.ts +3 -1
- package/dist-lib/lib/core/contract/index.js +19 -16
- package/dist-lib/lib/core/contract/indexing.js +2 -2
- package/dist-lib/lib/core/contract/joined-meshes.js +1 -1
- package/dist-lib/lib/core/contract/knowledge.js +2 -2
- package/dist-lib/lib/core/contract/members.js +2 -2
- package/dist-lib/lib/core/contract/seal.d.ts +2 -0
- package/dist-lib/lib/core/contract/seal.js +8 -0
- package/dist-lib/lib/core/contract/seed-defaults.js +1 -1
- package/dist-lib/lib/core/contract/tool-activity.js +1 -1
- package/dist-lib/lib/core/contract/tool-configs.js +3 -3
- package/dist-lib/lib/core/contract/writers.js +66 -42
- package/dist-lib/lib/core/departures.js +1 -1
- package/dist-lib/lib/core/device-invites.js +1 -1
- package/dist-lib/lib/core/document-attachment.js +2 -2
- package/dist-lib/lib/core/files/attachments.js +3 -3
- package/dist-lib/lib/core/files/index.d.ts +2 -2
- package/dist-lib/lib/core/files/index.js +19 -19
- package/dist-lib/lib/core/files/knowledge.js +3 -3
- package/dist-lib/lib/core/files/preview/image.js +1 -1
- package/dist-lib/lib/core/files/preview/index.js +6 -6
- package/dist-lib/lib/core/files/processing/claims.js +1 -1
- package/dist-lib/lib/core/files/processing/document.js +5 -5
- package/dist-lib/lib/core/files/processing/index.js +8 -8
- package/dist-lib/lib/core/files/processing/ownership.js +1 -1
- package/dist-lib/lib/core/files/processing/protocol.js +3 -3
- package/dist-lib/lib/core/files/processing/source.js +1 -1
- package/dist-lib/lib/core/files/rag/artifact/codec.js +1 -1
- package/dist-lib/lib/core/files/rag/artifact/id.js +1 -1
- package/dist-lib/lib/core/files/rag/artifact/types.d.ts +4 -4
- package/dist-lib/lib/core/files/rag/build.js +2 -2
- package/dist-lib/lib/core/files/rag/chunk/chunking.js +2 -2
- package/dist-lib/lib/core/files/rag/document-types.d.ts +3 -3
- package/dist-lib/lib/core/files/rag/extract/document-types.d.ts +9 -9
- package/dist-lib/lib/core/files/rag/extract/extract.js +7 -7
- package/dist-lib/lib/core/files/rag/extract/html-text.js +1 -1
- package/dist-lib/lib/core/files/rag/extract/office-collabora.js +2 -2
- package/dist-lib/lib/core/files/rag/extract/pdf-platform.js +1 -1
- package/dist-lib/lib/core/files/rag/extract/pdf-text.js +1 -1
- package/dist-lib/lib/core/files/rag/extract/text-decoding.js +1 -1
- package/dist-lib/lib/core/files/rag/extract/types.d.ts +2 -2
- package/dist-lib/lib/core/files/rag/index.js +22 -22
- package/dist-lib/lib/core/files/rag/load.js +5 -5
- package/dist-lib/lib/core/files/rag/policy.js +2 -2
- package/dist-lib/lib/core/files/rag/produce.js +5 -5
- package/dist-lib/lib/core/files/rag/retrieval/coverage.js +2 -2
- package/dist-lib/lib/core/files/rag/retrieval/diversity.js +1 -1
- package/dist-lib/lib/core/files/rag/retrieval/floor.js +2 -2
- package/dist-lib/lib/core/files/rag/retrieval/format.js +1 -1
- package/dist-lib/lib/core/files/rag/retrieval/full-text.js +18 -11
- package/dist-lib/lib/core/files/rag/retrieval/knowledge.js +5 -5
- package/dist-lib/lib/core/files/rag/retrieval/scoring.js +2 -2
- package/dist-lib/lib/core/files/rag/retrieval/search.js +4 -4
- package/dist-lib/lib/core/files/rag/retrieval/selection.js +2 -2
- package/dist-lib/lib/core/files/rag/retrieval/sources.js +2 -2
- package/dist-lib/lib/core/files/rag/retrieval/terms.js +1 -1
- package/dist-lib/lib/core/files/rag/retrieval/web-sources.js +1 -1
- package/dist-lib/lib/core/files/records/chunks.js +1 -1
- package/dist-lib/lib/core/files/records/index.js +8 -8
- package/dist-lib/lib/core/files/records/knowledge.js +2 -2
- package/dist-lib/lib/core/files/records/output.js +1 -1
- package/dist-lib/lib/core/files/records/reclaim.js +5 -5
- package/dist-lib/lib/core/files/records/routing.js +7 -7
- package/dist-lib/lib/core/files/store/blob.js +2 -2
- package/dist-lib/lib/core/files/store/index.js +7 -7
- package/dist-lib/lib/core/files/store/reference.js +2 -2
- package/dist-lib/lib/core/files/store/server.js +1 -1
- package/dist-lib/lib/core/files/store/sweep.js +1 -1
- package/dist-lib/lib/core/files/upload-input.js +2 -2
- package/dist-lib/lib/core/files/upload.d.ts +1 -1
- package/dist-lib/lib/core/files/upload.js +6 -6
- package/dist-lib/lib/core/harness-loan.d.ts +29 -0
- package/dist-lib/lib/core/harness-loan.js +187 -0
- package/dist-lib/lib/core/helpers/index.d.ts +2 -1
- package/dist-lib/lib/core/helpers/index.js +4 -4
- package/dist-lib/lib/core/hosted-agents.js +2 -2
- package/dist-lib/lib/core/identity.d.ts +2 -0
- package/dist-lib/lib/core/identity.js +4 -2
- package/dist-lib/lib/core/invite.d.ts +1 -0
- package/dist-lib/lib/core/invite.js +4 -2
- package/dist-lib/lib/core/local/index.d.ts +12 -3
- package/dist-lib/lib/core/local/index.js +81 -16
- package/dist-lib/lib/core/membership.d.ts +3 -0
- package/dist-lib/lib/core/membership.js +15 -1
- package/dist-lib/lib/core/mesh-seed-handoff.js +1 -1
- package/dist-lib/lib/core/mesh.d.ts +9 -3
- package/dist-lib/lib/core/mesh.js +41 -26
- package/dist-lib/lib/core/migrations/agent-catalog.js +1 -1
- package/dist-lib/lib/core/migrations/index.js +5 -3
- package/dist-lib/lib/core/migrations/membership.js +2 -2
- package/dist-lib/lib/core/migrations/seal.d.ts +3 -0
- package/dist-lib/lib/core/migrations/seal.js +30 -0
- package/dist-lib/lib/core/next-frame.js +1 -1
- package/dist-lib/lib/core/notification.js +1 -1
- package/dist-lib/lib/core/ota-config.js +1 -1
- package/dist-lib/lib/core/secret-handoff.js +1 -1
- package/dist-lib/lib/core/send.js +3 -3
- package/dist-lib/lib/core/skill-overlay-handoff.js +1 -1
- package/dist-lib/lib/core/skill-overlay-receiver.js +2 -2
- package/dist-lib/lib/core/skill-secrets.js +3 -3
- package/dist-lib/lib/core/sync/devices.js +1 -1
- package/dist-lib/lib/core/sync/index.js +8 -5
- package/dist-lib/lib/core/sync-status.js +1 -1
- package/dist-lib/lib/core/system-info.js +1 -1
- package/dist-lib/lib/core/tool-activity.js +2 -2
- package/dist-lib/lib/core/tool-configs.js +2 -2
- package/dist-lib/lib/core.d.ts +42 -3
- package/dist-lib/lib/core.js +543 -100
- package/dist-lib/lib/credential-pusher.js +4 -4
- package/dist-lib/lib/deadline.js +1 -1
- package/dist-lib/lib/fs-drive-bridge.js +5 -3
- package/dist-lib/lib/integrations/index.js +3 -3
- package/dist-lib/lib/integrations/oauth-providers.js +1 -1
- package/dist-lib/lib/integrations/setup-routes.d.ts +5 -5
- package/dist-lib/lib/integrations/setup-routes.js +3 -3
- package/dist-lib/lib/knowledge-search.js +1 -1
- package/dist-lib/lib/log.js +3 -3
- package/dist-lib/lib/mesh-scoped-engine.d.ts +3 -0
- package/dist-lib/lib/mesh-scoped-engine.js +101 -0
- package/dist-lib/lib/model-context.d.ts +2 -2
- package/dist-lib/lib/model-context.js +1 -1
- package/dist-lib/lib/model-download.d.ts +2 -0
- package/dist-lib/lib/model-download.js +26 -11
- package/dist-lib/lib/oauth/asana.js +3 -3
- package/dist-lib/lib/oauth/browser-connect.js +1 -1
- package/dist-lib/lib/oauth/client.js +2 -2
- package/dist-lib/lib/oauth/common.d.ts +1 -1
- package/dist-lib/lib/oauth/common.js +1 -1
- package/dist-lib/lib/oauth/flow-core.js +3 -3
- package/dist-lib/lib/oauth/flow.js +2 -2
- package/dist-lib/lib/oauth/google.js +4 -4
- package/dist-lib/lib/oauth/host.js +3 -3
- package/dist-lib/lib/oauth/keys.js +1 -0
- package/dist-lib/lib/oauth/loopback.js +1 -1
- package/dist-lib/lib/oauth/notion.js +3 -3
- package/dist-lib/lib/oauth/providers.js +1 -1
- package/dist-lib/lib/oauth/resolve.js +1 -1
- package/dist-lib/lib/oauth/spotify.js +4 -3
- package/dist-lib/lib/oauth/token-exchange.js +1 -1
- package/dist-lib/lib/oauth/verify.d.ts +8 -0
- package/dist-lib/lib/oauth/verify.js +58 -0
- package/dist-lib/lib/oauth.client.js +2 -2
- package/dist-lib/lib/oauth.d.ts +1 -0
- package/dist-lib/lib/oauth.js +13 -12
- package/dist-lib/lib/run-notices.d.ts +7 -7
- package/dist-lib/lib/run-transcript.js +1 -1
- package/dist-lib/schema/build.js +12 -6
- package/dist-lib/schema/content.d.ts +11 -11
- package/dist-lib/schema/engine.js +193 -30
- package/dist-lib/schema/field-order.js +2 -2
- package/dist-lib/schema/local.js +18 -4
- package/dist-lib/schema/qvac.d.ts +3 -0
- package/dist-lib/schema/qvac.js +22 -6
- package/dist-lib/spec/bind.js +41 -0
- package/dist-lib/spec/calls.js +12 -0
- package/dist-lib/spec/capabilities.d.ts +36 -12
- package/dist-lib/spec/hrpc/hrpc.json +161 -5
- package/dist-lib/spec/hrpc/index.js +154 -9
- package/dist-lib/spec/hrpc/messages.js +549 -129
- package/dist-lib/spec/hyperschema/index.js +549 -129
- package/dist-lib/spec/hyperschema/schema.json +370 -40
- package/dist-lib/spec/hyperschema/types.d.ts +85 -1
- package/dist-lib/spec/local/hyperdb/db.json +15 -0
- package/dist-lib/spec/local/hyperdb/index.js +70 -1
- package/dist-lib/spec/local/hyperdb/messages.js +300 -117
- package/dist-lib/spec/local/hyperschema/index.js +97 -4
- package/dist-lib/spec/local/hyperschema/schema.json +58 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +14 -0
- package/dist-lib/spec/mesh/hyperdb/messages.js +393 -288
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +6 -0
- package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +176 -85
- package/dist-lib/spec/mesh/hyperschema/index.js +176 -85
- package/dist-lib/spec/mesh/hyperschema/schema.json +71 -21
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +14 -1
- package/package.json +14 -8
- package/tui/README.md +7 -0
- package/tui/app.mjs +633 -55
- package/tui/lib/busy-words.mjs +57 -5
- package/tui/lib/export.mjs +1 -0
- package/tui/lib/render.mjs +25 -2
- package/tui/lib/selection.mjs +16 -0
- package/tui/lib/usage.mjs +54 -0
- package/tui/main.mjs +5 -2
|
@@ -5,293 +5,51 @@ import fs from 'fs';
|
|
|
5
5
|
import crypto from 'hypercore-crypto';
|
|
6
6
|
import os from 'os';
|
|
7
7
|
import path from 'path';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
8
|
+
import { mediaModelNameFor } from '@qvac/harness/lib/models/model-catalog.ts';
|
|
9
|
+
import { connectHarness } from '@qvac/harness';
|
|
10
|
+
import { loanFailureNotice } from './core/harness-loan.js';
|
|
11
11
|
import { SKILL_STATE } from '@qvac/harness/lib/tools/skills/skill-state.ts';
|
|
12
12
|
import { toolsetEnabled } from '@qvac/harness/lib/tools/toolsets.ts';
|
|
13
13
|
import { RAG_CITATION_LINE } from '@qvac/harness/lib/prompt.ts';
|
|
14
|
-
import { acceptedRepairCount, historyBranches, isPromptChunk } from
|
|
14
|
+
import { acceptedRepairCount, historyBranches, isPromptChunk } from './core/prompt-history.js';
|
|
15
15
|
import ReadyGuard from 'ready-guard';
|
|
16
16
|
import ReadyResource from 'ready-resource';
|
|
17
17
|
import { local } from '../spec/index.js';
|
|
18
|
-
import { isMeshTransition } from
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import { isAudioAttachment } from
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import { imageCacheName, sweepImageCache } from
|
|
26
|
-
import { IMAGE_MIMES, normalizeImageForModel } from
|
|
27
|
-
import { summarizeRuns } from
|
|
28
|
-
import { resolveFsGrants } from
|
|
29
|
-
import { fsGrantKey, fsWriteGrantKey } from
|
|
30
|
-
import { fsGrantsFrom, grantedDirsFrom } from
|
|
31
|
-
import { defaultImageCacheDir } from
|
|
32
|
-
import { ModelCatalogRevisionHub } from
|
|
33
|
-
import { agentRekeyFingerprint, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from
|
|
34
|
-
import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_TURNS, COMPACTION_TURN_PREFIX, isContextOverflowFailure, nearContextLimit, compactionBoundary,
|
|
35
|
-
import { isMobilePlatform, runnableModelName } from
|
|
36
|
-
import { ModelDownloadReconciler } from
|
|
18
|
+
import { isMeshTransition } from './api/current-mesh.js';
|
|
19
|
+
import { meshScoped } from './mesh-scoped-engine.js';
|
|
20
|
+
import { buildTitlePrompt, deriveChatTitle, isDefaultChatTitle, parseTitleResponse } from './chat-title.js';
|
|
21
|
+
import { matchApprovalRule } from './core/approval-rules.js';
|
|
22
|
+
import { isAudioAttachment } from './core/audio-attachment.js';
|
|
23
|
+
import { attachTurnDocumentIds, formatRagContext, chooseRagNotice, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, createRagSourceTagFilter, citedByPhraseOverlap, formatFullDocumentContext, isFullTextHit, isTruncatedFullTextHit, sourcesFromHits, WEB_TOOLS, webSourcesFromToolResult, withoutDataChannel } from './core/files/index.js';
|
|
24
|
+
import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason } from './core/document-attachment.js';
|
|
25
|
+
import { imageCacheName, sweepImageCache } from './core/image/cache-sweep.js';
|
|
26
|
+
import { IMAGE_MIMES, normalizeImageForModel } from './core/image/normalize.js';
|
|
27
|
+
import { summarizeRuns } from './diagnostics.js';
|
|
28
|
+
import { resolveFsGrants } from './fs-drive-bridge.js';
|
|
29
|
+
import { fsGrantKey, fsWriteGrantKey } from './fs-grant-keys.js';
|
|
30
|
+
import { fsGrantsFrom, grantedDirsFrom } from './fs-grants.js';
|
|
31
|
+
import { defaultImageCacheDir } from './image-cache-dir.js';
|
|
32
|
+
import { ModelCatalogRevisionHub } from './model-catalog-revision.js';
|
|
33
|
+
import { agentRekeyFingerprint, chatKvCacheKey, chatRepairKvCacheKey, disappearedChatIds } from './kv-cache-reclaim.js';
|
|
34
|
+
import { buildCompactionPrompt, canCompact, foldReducesPrompt, COMPACTION_KEEP_TURNS, COMPACTION_TURN_PREFIX, isContextOverflowFailure, nearContextLimit, compactionBoundary, lastCompactedThroughId, parseCompactionSummary } from './compaction.js';
|
|
35
|
+
import { isMobilePlatform, runnableModelName } from './model-clamp.js';
|
|
36
|
+
import { ModelDownloadReconciler } from './model-download.js';
|
|
37
37
|
import { AbortedError, raceAbort } from '@qvac/harness/race-abort';
|
|
38
|
-
import { RunTranscript } from
|
|
39
|
-
import { searchKnowledge } from
|
|
40
|
-
import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from
|
|
41
|
-
import { encodeAgentSkillReadiness } from
|
|
38
|
+
import { RunTranscript } from './run-transcript.js';
|
|
39
|
+
import { searchKnowledge } from './knowledge-search.js';
|
|
40
|
+
import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
|
|
41
|
+
import { encodeAgentSkillReadiness } from './skill-readiness.js';
|
|
42
|
+
import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, imageDownloadProgress, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
|
|
43
|
+
import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice } from './assistant/message.js';
|
|
44
|
+
import { drainBody, raceInactivity } from './assistant/fetch-body.js';
|
|
45
|
+
import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, historyFromChunks, inputAvailableToRun, listHistoryChunks, staleToolRunIds } from './assistant/history.js';
|
|
46
|
+
export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
|
|
47
|
+
export {} from './assistant/skill-encode.js';
|
|
48
|
+
export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT } from './assistant/message.js';
|
|
49
|
+
export { historyFromChunks, listHistoryChunks, staleToolRunIds } from './assistant/history.js';
|
|
42
50
|
const QvacState = local.getEnum('@local/qvac-state');
|
|
43
|
-
export const IMAGE_ONLY_USER_PROMPT = 'The user sent one or more images without any accompanying text. Describe what the images show and respond to anything notable in them.';
|
|
44
|
-
export const DOCUMENT_ONLY_USER_PROMPT = 'The user sent one or more documents without any accompanying text. Summarize the attached documents.';
|
|
45
|
-
export const IMAGE_AND_DOCUMENT_USER_PROMPT = 'The user sent one or more images together with one or more documents, and no accompanying text. Describe what the images show and summarize the attached documents.';
|
|
46
|
-
function backfillDocumentOnlyTurn(messages) {
|
|
47
|
-
const last = messages.findLast((turn) => turn.role === 'user');
|
|
48
|
-
if (!last || last.content.trim())
|
|
49
|
-
return;
|
|
50
|
-
last.content = last.attachments?.length
|
|
51
|
-
? IMAGE_AND_DOCUMENT_USER_PROMPT
|
|
52
|
-
: DOCUMENT_ONLY_USER_PROMPT;
|
|
53
|
-
}
|
|
54
|
-
function backfillImageOnlyTurns(messages) {
|
|
55
|
-
for (const turn of messages) {
|
|
56
|
-
if (turn.role !== 'user' || turn.content.trim())
|
|
57
|
-
continue;
|
|
58
|
-
if (turn.attachments?.length)
|
|
59
|
-
turn.content = IMAGE_ONLY_USER_PROMPT;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
export function unreadableImagesNotice(fileNames) {
|
|
63
|
-
const plural = fileNames.length > 1;
|
|
64
|
-
return (`[Attached image${plural ? 's' : ''}: ${fileNames.join(', ')} — could not be read, so ` +
|
|
65
|
-
`${plural ? 'they are' : 'it is'} not visible to you. Tell the user the upload failed and ` +
|
|
66
|
-
`ask for a re-upload; never describe or recreate ${plural ? 'them' : 'it'}]`);
|
|
67
|
-
}
|
|
68
|
-
export function annotateUnreadableImages(unreadable) {
|
|
69
|
-
for (const [turn, names] of unreadable) {
|
|
70
|
-
const notice = unreadableImagesNotice(names);
|
|
71
|
-
turn.content = turn.content ? `${turn.content}\n${notice}` : notice;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
async function raceInactivity(promise, signal, timeoutMs) {
|
|
75
|
-
let timer = null;
|
|
76
|
-
const expired = new Promise((_, reject) => {
|
|
77
|
-
timer = setTimeout(() => reject(new Error('blob read timed out')), timeoutMs);
|
|
78
|
-
});
|
|
79
|
-
try {
|
|
80
|
-
return await raceAbort(Promise.race([promise, expired]), signal);
|
|
81
|
-
}
|
|
82
|
-
finally {
|
|
83
|
-
if (timer !== null)
|
|
84
|
-
clearTimeout(timer);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
async function drainBody(body, signal, timeoutMs) {
|
|
88
|
-
if (!body)
|
|
89
|
-
return null;
|
|
90
|
-
const reader = body.getReader();
|
|
91
|
-
const parts = [];
|
|
92
|
-
try {
|
|
93
|
-
for (;;) {
|
|
94
|
-
const read = await raceInactivity(reader.read(), signal, timeoutMs);
|
|
95
|
-
if (read.done)
|
|
96
|
-
break;
|
|
97
|
-
parts.push(b4a.from(read.value));
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
catch {
|
|
101
|
-
await reader.cancel();
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
return b4a.concat(parts);
|
|
105
|
-
}
|
|
106
|
-
export function unheardAudioNotice(label, reason, opts = {}) {
|
|
107
|
-
const retry = opts.retryable === false ? '' : ', and ask them to try again';
|
|
108
|
-
return (`${label} could not be transcribed (${reason}). Tell the user the audio was not ` +
|
|
109
|
-
`transcribed and why${retry}; never invent what it might contain.`);
|
|
110
|
-
}
|
|
111
|
-
function silentAudioNotice(fileName) {
|
|
112
|
-
return (`Attached audio "${fileName}" was transcribed and contained no detectable speech. ` +
|
|
113
|
-
'Tell the user; never invent what it might contain.');
|
|
114
|
-
}
|
|
115
|
-
function audioTranscriptBlock(fileName, transcript) {
|
|
116
|
-
return [
|
|
117
|
-
`The audio file "${fileName}" has already been transcribed on-device. You cannot open the file itself — use the transcript below as its full content and answer from it.`,
|
|
118
|
-
'Transcript:',
|
|
119
|
-
transcript
|
|
120
|
-
].join('\n');
|
|
121
|
-
}
|
|
122
|
-
export function harnessListModels(harness) {
|
|
123
|
-
return async ({ type, cachedOnly, fullRegistry } = {}) => ({
|
|
124
|
-
models: [
|
|
125
|
-
...((await harness.list?.({
|
|
126
|
-
type: type ?? undefined,
|
|
127
|
-
cachedOnly: cachedOnly ?? undefined,
|
|
128
|
-
fullRegistry: fullRegistry ?? undefined
|
|
129
|
-
})) ?? [])
|
|
130
|
-
]
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
export function harnessDeleteModel(harness) {
|
|
134
|
-
return async ({ name }) => (await harness.deleteModel?.(name)) ?? { name, removed: 0, freedBytes: 0 };
|
|
135
|
-
}
|
|
136
|
-
export function harnessClearKvCache(harness) {
|
|
137
|
-
return async () => (await harness.clearKvCache?.()) ?? {
|
|
138
|
-
status: 'unsupported',
|
|
139
|
-
error: 'Harness does not support KV cache deletion'
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
export function harnessTranscribe(harness) {
|
|
143
|
-
return async ({ audio, model }) => {
|
|
144
|
-
if (!harness.transcribe)
|
|
145
|
-
throw new Error('harness does not support transcribe');
|
|
146
|
-
return { text: await harness.transcribe({ audio, model: model ?? '' }) };
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
export function harnessSpeak(harness) {
|
|
150
|
-
return async ({ text, model }) => {
|
|
151
|
-
if (!harness.speak)
|
|
152
|
-
throw new Error('harness does not support speak');
|
|
153
|
-
const { audio, sampleRate } = await harness.speak({ text, model: model ?? '' });
|
|
154
|
-
return { audio, sampleRate };
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
export function harnessSkillCatalog(harness, syncCredentials) {
|
|
158
|
-
return async (agentId, modelName, options) => {
|
|
159
|
-
if (options?.refresh)
|
|
160
|
-
await syncCredentials?.();
|
|
161
|
-
return {
|
|
162
|
-
skills: ((await harness.skills?.(agentId, modelName, options)) ?? []).map(toEngineSkill)
|
|
163
|
-
};
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
export function harnessSkillManagement(harness) {
|
|
167
|
-
return {
|
|
168
|
-
async listManagedSkills() {
|
|
169
|
-
return {
|
|
170
|
-
skills: ((await harness.listManagedSkills?.()) ?? []).map((skill) => ({
|
|
171
|
-
name: skill.name,
|
|
172
|
-
origin: skill.origin,
|
|
173
|
-
overridesBase: skill.overridesBase
|
|
174
|
-
}))
|
|
175
|
-
};
|
|
176
|
-
},
|
|
177
|
-
async readSkillFile(name) {
|
|
178
|
-
const file = await harness.readSkillFile?.(name);
|
|
179
|
-
return file ? { found: true, content: file.content, origin: file.origin } : { found: false };
|
|
180
|
-
},
|
|
181
|
-
async writeSkillFile(name, content) {
|
|
182
|
-
if (!harness.writeSkillFile)
|
|
183
|
-
return { ok: false };
|
|
184
|
-
return harness.writeSkillFile(name, content);
|
|
185
|
-
},
|
|
186
|
-
async copySkillForEdit(name) {
|
|
187
|
-
return { copied: (await harness.copyBaseForEdit?.(name))?.copied ?? false };
|
|
188
|
-
},
|
|
189
|
-
async deleteOverlaySkill(name) {
|
|
190
|
-
return { removed: (await harness.deleteOverlaySkill?.(name))?.removed ?? false };
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
export function harnessTools(harness) {
|
|
195
|
-
return async () => ({
|
|
196
|
-
tools: ((await harness.tools?.()) ?? []).map((tool) => ({
|
|
197
|
-
...toolReportEntry(tool),
|
|
198
|
-
description: tool.description
|
|
199
|
-
}))
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
function toolReportEntry(tool) {
|
|
203
|
-
return {
|
|
204
|
-
name: tool.name,
|
|
205
|
-
promptChars: tool.promptChars ?? 0,
|
|
206
|
-
toolset: tool.toolset ?? null,
|
|
207
|
-
skillGated: tool.skillGated === true,
|
|
208
|
-
scopeRequired: tool.scopeRequired === true,
|
|
209
|
-
rootsRequired: tool.rootsRequired === true,
|
|
210
|
-
writeRootsRequired: tool.writeRootsRequired === true
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
export function harnessSpeakStream(harness) {
|
|
214
|
-
return async function* (text) {
|
|
215
|
-
if (!harness.speakStream)
|
|
216
|
-
throw new Error('harness does not support speakStream');
|
|
217
|
-
for await (const { audio, sampleRate } of harness.speakStream({ model: '', text })) {
|
|
218
|
-
yield { audio, sampleRate };
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
function toEngineSkill(skill) {
|
|
223
|
-
const setup = normalizeSkillSetup(skill.setup);
|
|
224
|
-
return {
|
|
225
|
-
name: skill.name,
|
|
226
|
-
description: skill.description,
|
|
227
|
-
tools: [...skill.tools],
|
|
228
|
-
credentials: [...skill.credentials],
|
|
229
|
-
allowList: [...skill.allowList],
|
|
230
|
-
platforms: [...skill.platform],
|
|
231
|
-
status: skill.status,
|
|
232
|
-
...(skill.requires ? { requirements: toEngineSkillRequirements(skill.requires) } : {}),
|
|
233
|
-
...(skill.install ? { install: skill.install.map(toEngineInstallStep) } : {}),
|
|
234
|
-
...(setup ? { setup: toEngineSkillSetup(setup) } : {}),
|
|
235
|
-
...(skill.stub !== undefined ? { stub: skill.stub } : {}),
|
|
236
|
-
...(skill.minCapabilityProfile ? { minCapabilityProfile: skill.minCapabilityProfile } : {}),
|
|
237
|
-
...(skill.emoji ? { emoji: skill.emoji } : {})
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
function toEngineSkillRequirements(requires) {
|
|
241
|
-
return {
|
|
242
|
-
binaries: (requires.bins ?? []).map((bin) => ({
|
|
243
|
-
bin,
|
|
244
|
-
minVersion: requires.binMinVersions?.[bin]?.min,
|
|
245
|
-
command: requires.binMinVersions?.[bin]?.command
|
|
246
|
-
})),
|
|
247
|
-
credentials: [...(requires.credentials ?? [])]
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
function toEngineInstallStep(step) {
|
|
251
|
-
return {
|
|
252
|
-
id: step.id,
|
|
253
|
-
kind: step.kind,
|
|
254
|
-
label: step.label,
|
|
255
|
-
...(step.formula ? { formula: step.formula } : {}),
|
|
256
|
-
...(step.command ? { command: step.command } : {}),
|
|
257
|
-
...(step.manual !== undefined ? { manual: step.manual } : {}),
|
|
258
|
-
...(step.bins ? { bins: [...step.bins] } : {})
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
function toEngineSkillSetup(setup) {
|
|
262
|
-
return {
|
|
263
|
-
...(setup.summary ? { summary: setup.summary } : {}),
|
|
264
|
-
...(setup.routes
|
|
265
|
-
? {
|
|
266
|
-
routes: setup.routes.map((route) => ({
|
|
267
|
-
kind: route.kind,
|
|
268
|
-
label: route.label,
|
|
269
|
-
...(route.provider ? { provider: route.provider } : {}),
|
|
270
|
-
...(route.credentialKey ? { credentialKey: route.credentialKey } : {}),
|
|
271
|
-
...(route.description ? { description: route.description } : {}),
|
|
272
|
-
...(route.helpUrl ? { helpUrl: route.helpUrl } : {}),
|
|
273
|
-
...(route.steps ? { steps: [...route.steps] } : {}),
|
|
274
|
-
...(route.configSteps ? { configSteps: [...route.configSteps] } : {}),
|
|
275
|
-
...(route.fields
|
|
276
|
-
? {
|
|
277
|
-
fields: route.fields.map((field) => ({
|
|
278
|
-
key: field.key,
|
|
279
|
-
label: field.label,
|
|
280
|
-
...(field.placeholder ? { placeholder: field.placeholder } : {}),
|
|
281
|
-
...(field.secret !== undefined ? { secret: field.secret } : {}),
|
|
282
|
-
...(field.required !== undefined ? { required: field.required } : {})
|
|
283
|
-
}))
|
|
284
|
-
}
|
|
285
|
-
: {}),
|
|
286
|
-
...(route.satisfied !== undefined ? { satisfied: route.satisfied } : {})
|
|
287
|
-
}))
|
|
288
|
-
}
|
|
289
|
-
: {})
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
51
|
const FINAL_FLUSH_MS = 10_000;
|
|
293
52
|
const UPLOAD_FRAME_BYTES = 64 * 1024;
|
|
294
|
-
const TERMINAL_STATES = new Set(['completed', 'failed', 'canceled']);
|
|
295
53
|
function settledWithin(write, ms) {
|
|
296
54
|
let timer;
|
|
297
55
|
const expired = new Promise((resolve) => {
|
|
@@ -305,19 +63,12 @@ function isDeniedResult(result) {
|
|
|
305
63
|
return false;
|
|
306
64
|
return result.denied === true;
|
|
307
65
|
}
|
|
308
|
-
const CONSUME_RETRY_MS = 1000;
|
|
309
|
-
const CONSUME_RETRY_MAX_MS = 30000;
|
|
310
66
|
const BLOB_READ_TIMEOUT_MS = 15_000;
|
|
311
|
-
function nextRetryDelay(err, current) {
|
|
312
|
-
return isMeshTransition(err) ? Math.min(current * 2, CONSUME_RETRY_MAX_MS) : CONSUME_RETRY_MS;
|
|
313
|
-
}
|
|
314
67
|
const SUSPENDED = RUN_ABORT_REASONS.suspended;
|
|
315
68
|
const OWNERSHIP_LOST = RUN_ABORT_REASONS.ownershipLost;
|
|
316
69
|
const RUN_SETTLED = RUN_ABORT_REASONS.runSettled;
|
|
317
70
|
const RUNNER_DIED = RUN_ABORT_REASONS.runnerDied;
|
|
318
71
|
const RUNNER_CLOSED = RUN_ABORT_REASONS.runnerClosed;
|
|
319
|
-
const HISTORY_TYPES = HISTORY_CHUNK_TYPES;
|
|
320
|
-
const SKILL_TOOL = 'skill';
|
|
321
72
|
function isSetUpHere(status) {
|
|
322
73
|
return status === SKILL_STATE.ready || status === SKILL_STATE.incompatibleModel;
|
|
323
74
|
}
|
|
@@ -329,229 +80,11 @@ function withConfigDefaults(agent, config) {
|
|
|
329
80
|
return agent;
|
|
330
81
|
return { ...agent, ctxSize: config.ctxSize };
|
|
331
82
|
}
|
|
332
|
-
export async function listHistoryChunks(engine, chatId) {
|
|
333
|
-
const [history, toolResults, runStatuses] = await Promise.all([
|
|
334
|
-
engine.listChunks({ chatId, types: HISTORY_TYPES }),
|
|
335
|
-
engine.listChunks({ chatId, type: 'tool-result' }),
|
|
336
|
-
engine.listChunks({ chatId, type: 'run-status' })
|
|
337
|
-
]);
|
|
338
|
-
return [
|
|
339
|
-
...(history.chunks ?? []),
|
|
340
|
-
...(toolResults.chunks ?? []),
|
|
341
|
-
...(runStatuses.chunks ?? [])
|
|
342
|
-
].sort((a, b) => a.seq - b.seq);
|
|
343
|
-
}
|
|
344
|
-
function latestAttemptStarts(chunks) {
|
|
345
|
-
const starts = new Map();
|
|
346
|
-
for (const chunk of chunks) {
|
|
347
|
-
if (chunk.type !== 'run-status' || !chunk.runId)
|
|
348
|
-
continue;
|
|
349
|
-
if (TERMINAL_STATES.has(chunk.runStatus?.state ?? ''))
|
|
350
|
-
continue;
|
|
351
|
-
starts.set(chunk.runId, chunk.seq);
|
|
352
|
-
}
|
|
353
|
-
return starts;
|
|
354
|
-
}
|
|
355
|
-
function beforeLatestAttempt(chunk, starts) {
|
|
356
|
-
if (!chunk.runId)
|
|
357
|
-
return false;
|
|
358
|
-
const start = starts.get(chunk.runId);
|
|
359
|
-
return start !== undefined && chunk.seq < start;
|
|
360
|
-
}
|
|
361
|
-
export function historyFromChunks(chunks, stale = {}) {
|
|
362
|
-
const attemptStarts = latestAttemptStarts(chunks);
|
|
363
|
-
const staleRunIds = stale.runIds ?? new Set();
|
|
364
|
-
const staleQuestionChunkIds = stale.questionChunkIds ?? new Set();
|
|
365
|
-
const folded = foldCompactedChunks(chunks);
|
|
366
|
-
const source = folded.chunks;
|
|
367
|
-
const attachments = [];
|
|
368
|
-
const userMessages = [];
|
|
369
|
-
for (const chunk of source) {
|
|
370
|
-
if (chunk.type === 'attachment')
|
|
371
|
-
attachments.push(chunk);
|
|
372
|
-
else if (chunk.userMessage)
|
|
373
|
-
userMessages.push(chunk);
|
|
374
|
-
}
|
|
375
|
-
const byMessage = groupAttachmentsByMessage(attachments, userMessages);
|
|
376
|
-
const history = [];
|
|
377
|
-
const replayedSkills = new Set();
|
|
378
|
-
if (folded.summary)
|
|
379
|
-
history.push({ role: 'user', content: compactionTurn(folded.summary) });
|
|
380
|
-
const byRun = new Map();
|
|
381
|
-
const anchor = (runId) => {
|
|
382
|
-
let turn = byRun.get(runId);
|
|
383
|
-
if (!turn) {
|
|
384
|
-
turn = { role: 'assistant', content: '' };
|
|
385
|
-
byRun.set(runId, turn);
|
|
386
|
-
history.push(turn);
|
|
387
|
-
}
|
|
388
|
-
return turn;
|
|
389
|
-
};
|
|
390
|
-
for (const chunk of source) {
|
|
391
|
-
if (chunk.userMessage) {
|
|
392
|
-
if (staleQuestionChunkIds.has(chunk.id))
|
|
393
|
-
continue;
|
|
394
|
-
const turn = { role: 'user', content: chunk.userMessage.text };
|
|
395
|
-
const refs = (byMessage.get(chunk.id) ?? [])
|
|
396
|
-
.map((attachmentChunk) => attachmentChunk.attachment)
|
|
397
|
-
.filter((ref) => !ref.deletedAt);
|
|
398
|
-
const images = refs.filter((ref) => IMAGE_MIMES.has(ref.mimeType.toLowerCase()));
|
|
399
|
-
const audio = refs.filter((ref) => isAudioAttachment(ref.fileName, ref.mimeType));
|
|
400
|
-
const documents = refs.filter((ref) => isDocumentAttachment(ref));
|
|
401
|
-
if (images.length)
|
|
402
|
-
turn.images = images;
|
|
403
|
-
if (audio.length)
|
|
404
|
-
turn.audioAttachments = audio;
|
|
405
|
-
if (documents.length)
|
|
406
|
-
turn.documents = documents;
|
|
407
|
-
history.push(turn);
|
|
408
|
-
continue;
|
|
409
|
-
}
|
|
410
|
-
if (chunk.attachment)
|
|
411
|
-
continue;
|
|
412
|
-
if (chunk.type === 'run-request' && chunk.runId) {
|
|
413
|
-
anchor(chunk.runId);
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
if (chunk.toolResult?.name === SKILL_TOOL) {
|
|
417
|
-
if (chunk.runId && staleRunIds.has(chunk.runId))
|
|
418
|
-
continue;
|
|
419
|
-
if (beforeLatestAttempt(chunk, attemptStarts))
|
|
420
|
-
continue;
|
|
421
|
-
const loaded = chunk.toolResult.result?.name;
|
|
422
|
-
if (typeof loaded === 'string' && !replayedSkills.has(loaded)) {
|
|
423
|
-
replayedSkills.add(loaded);
|
|
424
|
-
const at = history.indexOf(anchor(chunk.runId)) + 1;
|
|
425
|
-
history.splice(at, 0, { role: 'tool', content: skillLoadedTurn(loaded) });
|
|
426
|
-
}
|
|
427
|
-
continue;
|
|
428
|
-
}
|
|
429
|
-
if (chunk.toolResult) {
|
|
430
|
-
if (chunk.runId && staleRunIds.has(chunk.runId))
|
|
431
|
-
continue;
|
|
432
|
-
if (beforeLatestAttempt(chunk, attemptStarts))
|
|
433
|
-
continue;
|
|
434
|
-
const produced = attachmentTurn(chunk.toolResult.name, chunk.toolResult.result);
|
|
435
|
-
if (produced)
|
|
436
|
-
history.push({ role: 'tool', content: produced });
|
|
437
|
-
continue;
|
|
438
|
-
}
|
|
439
|
-
if (!chunk.content)
|
|
440
|
-
continue;
|
|
441
|
-
if (chunk.runId && staleRunIds.has(chunk.runId))
|
|
442
|
-
continue;
|
|
443
|
-
if (beforeLatestAttempt(chunk, attemptStarts))
|
|
444
|
-
continue;
|
|
445
|
-
anchor(chunk.runId).content += chunk.content.text;
|
|
446
|
-
}
|
|
447
|
-
return history.filter((turn) => turn.role === 'user' || turn.content.length > 0);
|
|
448
|
-
}
|
|
449
|
-
function withoutOccupancy(metrics) {
|
|
450
|
-
if (!metrics)
|
|
451
|
-
return metrics;
|
|
452
|
-
const { cacheTokens, promptTokens, ...rest } = metrics;
|
|
453
|
-
return rest;
|
|
454
|
-
}
|
|
455
|
-
function inputAvailableToRun(chunk, requestSeq) {
|
|
456
|
-
const userInput = Boolean(chunk.userMessage || (chunk.attachment && !chunk.runId));
|
|
457
|
-
return !userInput || chunk.seq < requestSeq;
|
|
458
|
-
}
|
|
459
|
-
function normalizeQuestion(text) {
|
|
460
|
-
return text.trim().toLowerCase().replace(/\s+/g, ' ');
|
|
461
|
-
}
|
|
462
|
-
const GENERIC_ACKNOWLEDGEMENTS = new Set([
|
|
463
|
-
'yes',
|
|
464
|
-
'y',
|
|
465
|
-
'yeah',
|
|
466
|
-
'yep',
|
|
467
|
-
'yup',
|
|
468
|
-
'ok',
|
|
469
|
-
'okay',
|
|
470
|
-
'k',
|
|
471
|
-
'sure',
|
|
472
|
-
'fine',
|
|
473
|
-
'no',
|
|
474
|
-
'n',
|
|
475
|
-
'nope',
|
|
476
|
-
'nah',
|
|
477
|
-
'again',
|
|
478
|
-
'continue',
|
|
479
|
-
'go on',
|
|
480
|
-
'go ahead',
|
|
481
|
-
'keep going',
|
|
482
|
-
'proceed',
|
|
483
|
-
'do it',
|
|
484
|
-
'more',
|
|
485
|
-
'please',
|
|
486
|
-
'thanks',
|
|
487
|
-
'thank you',
|
|
488
|
-
'retry',
|
|
489
|
-
'redo'
|
|
490
|
-
]);
|
|
491
|
-
function isGenericAcknowledgement(question) {
|
|
492
|
-
return GENERIC_ACKNOWLEDGEMENTS.has(question.replace(/[?.!]+$/, '').trim());
|
|
493
|
-
}
|
|
494
|
-
export function staleToolRunIds(historyChunks, approvalChunks, currentRunId) {
|
|
495
|
-
const empty = { runIds: new Set(), questionChunkIds: new Set() };
|
|
496
|
-
const toolRuns = new Set();
|
|
497
|
-
for (const chunk of approvalChunks) {
|
|
498
|
-
if (chunk.runId)
|
|
499
|
-
toolRuns.add(chunk.runId);
|
|
500
|
-
}
|
|
501
|
-
if (!toolRuns.size)
|
|
502
|
-
return empty;
|
|
503
|
-
const turnOrder = [];
|
|
504
|
-
const turnText = new Map();
|
|
505
|
-
const turnRuns = new Map();
|
|
506
|
-
const runChunk = new Map();
|
|
507
|
-
let lastUser = null;
|
|
508
|
-
for (const chunk of historyChunks) {
|
|
509
|
-
if (chunk.userMessage) {
|
|
510
|
-
lastUser = { text: chunk.userMessage.text, chunkId: chunk.id };
|
|
511
|
-
continue;
|
|
512
|
-
}
|
|
513
|
-
if (chunk.type === 'run-request' && chunk.runId && lastUser !== null) {
|
|
514
|
-
const chunkId = lastUser.chunkId;
|
|
515
|
-
if (!turnText.has(chunkId)) {
|
|
516
|
-
turnOrder.push(chunkId);
|
|
517
|
-
turnText.set(chunkId, lastUser.text);
|
|
518
|
-
}
|
|
519
|
-
turnRuns.set(chunkId, [...(turnRuns.get(chunkId) ?? []), chunk.runId]);
|
|
520
|
-
runChunk.set(chunk.runId, chunkId);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
const currentChunkId = runChunk.get(currentRunId) ?? lastUser?.chunkId;
|
|
524
|
-
if (currentChunkId === undefined)
|
|
525
|
-
return empty;
|
|
526
|
-
const currentText = turnText.get(currentChunkId) ?? lastUser?.text;
|
|
527
|
-
if (currentText === undefined)
|
|
528
|
-
return empty;
|
|
529
|
-
const target = normalizeQuestion(currentText);
|
|
530
|
-
if (isGenericAcknowledgement(target))
|
|
531
|
-
return empty;
|
|
532
|
-
const currentIndex = turnOrder.indexOf(currentChunkId);
|
|
533
|
-
const start = currentIndex === -1 ? turnOrder.length - 1 : currentIndex - 1;
|
|
534
|
-
const runIds = new Set();
|
|
535
|
-
const questionChunkIds = new Set();
|
|
536
|
-
let blockHadApproval = false;
|
|
537
|
-
for (let i = start; i >= 0; i--) {
|
|
538
|
-
const chunkId = turnOrder[i];
|
|
539
|
-
if (normalizeQuestion(turnText.get(chunkId)) !== target)
|
|
540
|
-
break;
|
|
541
|
-
questionChunkIds.add(chunkId);
|
|
542
|
-
for (const rid of turnRuns.get(chunkId) ?? []) {
|
|
543
|
-
runIds.add(rid);
|
|
544
|
-
if (toolRuns.has(rid))
|
|
545
|
-
blockHadApproval = true;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
if (!blockHadApproval)
|
|
549
|
-
return empty;
|
|
550
|
-
return { runIds, questionChunkIds };
|
|
551
|
-
}
|
|
552
83
|
export class QvacAssistant extends ReadyResource {
|
|
553
84
|
_engine;
|
|
554
85
|
_harness;
|
|
86
|
+
_borrowHarness;
|
|
87
|
+
_borrowed = new Map();
|
|
555
88
|
_log;
|
|
556
89
|
_imageCacheDir;
|
|
557
90
|
_imageCacheMaxBytes;
|
|
@@ -580,13 +113,14 @@ export class QvacAssistant extends ReadyResource {
|
|
|
580
113
|
_harnessDiagnostics;
|
|
581
114
|
_harnessDeath;
|
|
582
115
|
_refreshingDiagnostics;
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
_retryMs = CONSUME_RETRY_MS;
|
|
116
|
+
_meshList;
|
|
117
|
+
_advertisedOn;
|
|
586
118
|
_chatList;
|
|
587
119
|
_agentList;
|
|
588
120
|
_chatListWatch;
|
|
589
121
|
_agentWatch;
|
|
122
|
+
_meshListWatch;
|
|
123
|
+
_runConsumers;
|
|
590
124
|
_runs;
|
|
591
125
|
_seen;
|
|
592
126
|
_lifecycle;
|
|
@@ -597,10 +131,11 @@ export class QvacAssistant extends ReadyResource {
|
|
|
597
131
|
_modelCatalog = new ModelCatalogRevisionHub();
|
|
598
132
|
_onHarnessStatus;
|
|
599
133
|
_onModelCatalogEvent;
|
|
600
|
-
constructor(engine, harness, { logger = null, imageCacheDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS } = {}) {
|
|
134
|
+
constructor(engine, harness, { logger = null, imageCacheDir, imageCacheMaxBytes, imageCacheMaxAgeMs, downloadRetryMs, platform = os.platform(), preloadCurrentAgentModel = true, drives, borrowHarness, blobReadTimeoutMs = BLOB_READ_TIMEOUT_MS } = {}) {
|
|
601
135
|
super();
|
|
602
136
|
this._engine = engine;
|
|
603
137
|
this._harness = harness;
|
|
138
|
+
this._borrowHarness = borrowHarness ?? null;
|
|
604
139
|
this._drives = drives;
|
|
605
140
|
this._log = logger;
|
|
606
141
|
this._imageCacheDir = imageCacheDir ?? defaultImageCacheDir();
|
|
@@ -622,14 +157,16 @@ export class QvacAssistant extends ReadyResource {
|
|
|
622
157
|
this._harnessDiagnostics = null;
|
|
623
158
|
this._harnessDeath = null;
|
|
624
159
|
this._refreshingDiagnostics = false;
|
|
625
|
-
this.
|
|
626
|
-
this.
|
|
160
|
+
this._meshList = null;
|
|
161
|
+
this._advertisedOn = null;
|
|
627
162
|
this._chatList = null;
|
|
628
163
|
this._agentList = null;
|
|
629
|
-
this._chatListWatch = this.
|
|
630
|
-
this._agentWatch = this.
|
|
164
|
+
this._chatListWatch = this._supervisedWatch('chat-list', () => this._watchChatList());
|
|
165
|
+
this._agentWatch = this._supervisedWatch('agent', () => this._watchAgents());
|
|
166
|
+
this._meshListWatch = this._supervisedWatch('mesh-list', () => this._watchMeshList());
|
|
167
|
+
this._runConsumers = new Map();
|
|
631
168
|
this._runs = new Map();
|
|
632
|
-
this._seen = new
|
|
169
|
+
this._seen = new Map();
|
|
633
170
|
this._lifecycle = new AbortController();
|
|
634
171
|
this._audioTranscripts = new Map();
|
|
635
172
|
this._chats = new Map();
|
|
@@ -681,9 +218,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
681
218
|
mobileClass: this._mobileClass
|
|
682
219
|
});
|
|
683
220
|
this._downloads.start();
|
|
684
|
-
this._startConsume();
|
|
685
221
|
this._chatListWatch.start();
|
|
686
222
|
this._agentWatch.start();
|
|
223
|
+
this._meshListWatch.start();
|
|
687
224
|
void this._bootOnline();
|
|
688
225
|
this._triggerModelWarm();
|
|
689
226
|
void this.onlineReady().then(() => this._triggerModelWarm(), noop);
|
|
@@ -697,12 +234,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
697
234
|
if (this._warmedModel)
|
|
698
235
|
return;
|
|
699
236
|
const deviceId = this._deviceId;
|
|
700
|
-
const config = await this._engine.getConfig({}).catch(
|
|
237
|
+
const config = await this._engine.getConfig({}).catch(noop);
|
|
701
238
|
const agent = await this._currentAgent(config?.activeAgentId ?? null, deviceId);
|
|
702
239
|
if (!agent?.modelName || !agent.providerDeviceId?.equals(deviceId) || this.suspended)
|
|
703
240
|
return;
|
|
704
241
|
const model = runnableModelName(agent.modelName, await this._ensureCapability(), this._mobileClass);
|
|
705
|
-
const rows = await this._harness.list?.({ cachedOnly: true }).catch(
|
|
242
|
+
const rows = await this._harness.list?.({ cachedOnly: true }).catch(noop);
|
|
706
243
|
if (!rows?.some((row) => row.name === model && row.isCached)) {
|
|
707
244
|
this._log?.info('models', 'model warm skipped', { model, reason: 'not cached' });
|
|
708
245
|
return;
|
|
@@ -722,7 +259,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
722
259
|
}
|
|
723
260
|
async _currentAgent(activeAgentId, deviceId) {
|
|
724
261
|
if (activeAgentId) {
|
|
725
|
-
const agent = await this._engine.getAgent({ id: activeAgentId }).catch(
|
|
262
|
+
const agent = await this._engine.getAgent({ id: activeAgentId }).catch(noop);
|
|
726
263
|
if (agent && !agent.deletedAt)
|
|
727
264
|
return agent;
|
|
728
265
|
}
|
|
@@ -969,10 +506,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
969
506
|
}
|
|
970
507
|
}
|
|
971
508
|
async _close() {
|
|
972
|
-
if (this._retry)
|
|
973
|
-
clearTimeout(this._retry);
|
|
974
509
|
this._chatListWatch.stop();
|
|
975
510
|
this._agentWatch.stop();
|
|
511
|
+
this._stopMeshIntake();
|
|
976
512
|
this._harness.off?.('status', this._onHarnessStatus);
|
|
977
513
|
this._harness.off?.('model', this._onModelCatalogEvent);
|
|
978
514
|
this._warmAbort.abort(RUNNER_CLOSED);
|
|
@@ -981,7 +517,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
981
517
|
run.controller.abort(RUNNER_CLOSED);
|
|
982
518
|
await this._downloads?.close();
|
|
983
519
|
this._modelCatalog.close();
|
|
984
|
-
await this.
|
|
520
|
+
await this._meshList?.return?.();
|
|
985
521
|
await this._chatList?.return?.();
|
|
986
522
|
await this._agentList?.return?.();
|
|
987
523
|
this._lifecycle.abort(RUNNER_CLOSED);
|
|
@@ -1004,12 +540,11 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1004
540
|
return;
|
|
1005
541
|
this._state = QvacState.SUSPEND;
|
|
1006
542
|
this._log?.info('assistant', 'suspending', { runs: this._runs.size });
|
|
1007
|
-
if (this._retry)
|
|
1008
|
-
clearTimeout(this._retry);
|
|
1009
543
|
this._chatListWatch.stop();
|
|
1010
544
|
this._agentWatch.stop();
|
|
545
|
+
this._stopMeshIntake();
|
|
1011
546
|
await this._downloads?.suspend();
|
|
1012
|
-
await this.
|
|
547
|
+
await this._meshList?.return?.();
|
|
1013
548
|
await this._chatList?.return?.();
|
|
1014
549
|
await this._agentList?.return?.();
|
|
1015
550
|
for (const run of this._runs.values())
|
|
@@ -1041,9 +576,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1041
576
|
await Promise.allSettled([...this._runs.values()].map((run) => run.done));
|
|
1042
577
|
this._downloads?.resume();
|
|
1043
578
|
if (this.opened) {
|
|
1044
|
-
this._startConsume();
|
|
1045
579
|
this._chatListWatch.start();
|
|
1046
580
|
this._agentWatch.start();
|
|
581
|
+
this._meshListWatch.start();
|
|
1047
582
|
}
|
|
1048
583
|
}
|
|
1049
584
|
async listModels(options) {
|
|
@@ -1064,7 +599,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1064
599
|
if (!this.opened)
|
|
1065
600
|
await this.ready();
|
|
1066
601
|
if (agentId) {
|
|
1067
|
-
const agent = await this._engine.getAgent({ id: agentId }).catch(
|
|
602
|
+
const agent = await this._engine.getAgent({ id: agentId }).catch(noop);
|
|
1068
603
|
const deviceId = this._deviceId;
|
|
1069
604
|
if (agent && deviceId && !agent.providerDeviceId?.equals(deviceId)) {
|
|
1070
605
|
agentId = null;
|
|
@@ -1112,32 +647,29 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1112
647
|
async tools() {
|
|
1113
648
|
return (await this._harness.tools?.()) ?? [];
|
|
1114
649
|
}
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
let timer = null;
|
|
1121
|
-
const rearm = (err) => {
|
|
1122
|
-
if (this.closing || this.suspended)
|
|
1123
|
-
return;
|
|
650
|
+
_supervisedWatch(label, run) {
|
|
651
|
+
let running = false;
|
|
652
|
+
let again = false;
|
|
653
|
+
const settle = (err) => {
|
|
654
|
+
running = false;
|
|
1124
655
|
if (err !== null && !isMeshTransition(err)) {
|
|
1125
|
-
this._log?.error('assistant', `${label} watch failed
|
|
656
|
+
this._log?.error('assistant', `${label} watch failed`, err);
|
|
1126
657
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
timer = setTimeout(start, delay);
|
|
658
|
+
if (again)
|
|
659
|
+
start();
|
|
1130
660
|
};
|
|
1131
661
|
const start = () => {
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
clearTimeout(timer);
|
|
662
|
+
again = false;
|
|
663
|
+
if (this.closing || this.suspended)
|
|
664
|
+
return;
|
|
665
|
+
if (running) {
|
|
666
|
+
again = true;
|
|
667
|
+
return;
|
|
1139
668
|
}
|
|
669
|
+
running = true;
|
|
670
|
+
run().then(() => settle(null), (err) => settle(err));
|
|
1140
671
|
};
|
|
672
|
+
return { start, stop: () => void (again = false) };
|
|
1141
673
|
}
|
|
1142
674
|
async _watchChatList() {
|
|
1143
675
|
const stream = this._engine.chatsWatch({});
|
|
@@ -1191,9 +723,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1191
723
|
this._log?.warn('assistant', 'agent re-key cache reclaim failed', { agentId, err });
|
|
1192
724
|
}
|
|
1193
725
|
}
|
|
1194
|
-
async _reconcileCompaction(chatId) {
|
|
726
|
+
async _reconcileCompaction(engine, chatId) {
|
|
1195
727
|
const deviceId = this._deviceId;
|
|
1196
|
-
const { chunks } = await
|
|
728
|
+
const { chunks } = await engine
|
|
1197
729
|
.listChunks({ chatId, type: 'compaction' })
|
|
1198
730
|
.catch(() => ({ chunks: [] }));
|
|
1199
731
|
let newest = null;
|
|
@@ -1232,60 +764,132 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1232
764
|
return false;
|
|
1233
765
|
}
|
|
1234
766
|
}
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
767
|
+
async _watchMeshList() {
|
|
768
|
+
const stream = this._engine.meshesWatch({});
|
|
769
|
+
this._meshList = stream[Symbol.asyncIterator]();
|
|
770
|
+
while (true) {
|
|
771
|
+
const { value, done } = await this._meshList.next();
|
|
772
|
+
if (done || this.closing || this.suspended)
|
|
773
|
+
return;
|
|
774
|
+
const meshes = value.meshes ?? [];
|
|
775
|
+
this._syncRunConsumers(meshes);
|
|
776
|
+
this._followSelectedMesh(meshes);
|
|
1240
777
|
}
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
778
|
+
}
|
|
779
|
+
_syncRunConsumers(meshes) {
|
|
780
|
+
const open = new Set(meshes.filter((mesh) => mesh.open).map((mesh) => mesh.meshId));
|
|
781
|
+
for (const [meshId, consumer] of this._runConsumers) {
|
|
782
|
+
if (open.has(meshId))
|
|
783
|
+
continue;
|
|
784
|
+
consumer.stop();
|
|
785
|
+
this._runConsumers.delete(meshId);
|
|
786
|
+
this._pruneSeen(meshId, new Set());
|
|
787
|
+
}
|
|
788
|
+
for (const meshId of open) {
|
|
789
|
+
const consumer = this._runConsumers.get(meshId) ?? this._meshRunConsumer(meshId);
|
|
790
|
+
this._runConsumers.set(meshId, consumer);
|
|
791
|
+
consumer.start();
|
|
1245
792
|
}
|
|
1246
|
-
this._lastCapabilities = null;
|
|
1247
|
-
const delay = isMeshTransition(err) ? this._retryMs : CONSUME_RETRY_MS;
|
|
1248
|
-
this._retryMs = nextRetryDelay(err, delay);
|
|
1249
|
-
this._retry = setTimeout(() => this._startConsume(), delay);
|
|
1250
793
|
}
|
|
1251
|
-
|
|
1252
|
-
const
|
|
1253
|
-
|
|
794
|
+
_followSelectedMesh(meshes) {
|
|
795
|
+
const selected = meshes.find((mesh) => mesh.active);
|
|
796
|
+
const on = selected ? `${selected.meshId}:${selected.writable}` : null;
|
|
797
|
+
if (on === null) {
|
|
798
|
+
this._advertisedOn = null;
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
if (on === this._advertisedOn)
|
|
802
|
+
return;
|
|
803
|
+
this._advertisedOn = on;
|
|
804
|
+
this._chatListWatch.start();
|
|
805
|
+
this._agentWatch.start();
|
|
806
|
+
this._downloads?.meshSelected();
|
|
807
|
+
this._lastCapabilities = null;
|
|
1254
808
|
this._reportCapabilities().catch(noop);
|
|
1255
809
|
this._reconcileDeviceProfile().catch(noop);
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
const
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
810
|
+
}
|
|
811
|
+
_meshRunConsumer(meshId) {
|
|
812
|
+
let requests = null;
|
|
813
|
+
const watch = this._supervisedWatch(`run-requests:${meshId}`, async () => {
|
|
814
|
+
const stream = this._engine.runRequestsWatch({ meshId });
|
|
815
|
+
requests = stream[Symbol.asyncIterator]();
|
|
816
|
+
while (true) {
|
|
817
|
+
const { value, done } = await requests.next();
|
|
818
|
+
if (done || this.closing || this.suspended)
|
|
819
|
+
return;
|
|
820
|
+
const chunks = value.chunks ?? [];
|
|
821
|
+
for (const chunk of chunks)
|
|
822
|
+
this._dispatch(chunk, meshId);
|
|
823
|
+
this._pruneSeen(meshId, new Set(chunks.map((chunk) => chunk.runId)));
|
|
824
|
+
}
|
|
825
|
+
});
|
|
826
|
+
return {
|
|
827
|
+
start: () => watch.start(),
|
|
828
|
+
stop: () => {
|
|
829
|
+
watch.stop();
|
|
830
|
+
void requests?.return?.();
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
_stopMeshIntake() {
|
|
835
|
+
this._meshListWatch.stop();
|
|
836
|
+
for (const consumer of this._runConsumers.values())
|
|
837
|
+
consumer.stop();
|
|
838
|
+
this._runConsumers.clear();
|
|
839
|
+
this._advertisedOn = null;
|
|
840
|
+
}
|
|
841
|
+
_pruneSeen(meshId, live) {
|
|
842
|
+
for (const [runId, from] of this._seen) {
|
|
843
|
+
if (from !== meshId || live.has(runId))
|
|
844
|
+
continue;
|
|
845
|
+
this._seen.delete(runId);
|
|
1267
846
|
}
|
|
1268
847
|
}
|
|
1269
|
-
|
|
848
|
+
_lenderFor(agent) {
|
|
849
|
+
const lender = agent.lenderDeviceId;
|
|
850
|
+
if (!lender || !this._borrowHarness)
|
|
851
|
+
return null;
|
|
852
|
+
const id = lender.toString('hex');
|
|
853
|
+
return id === this._deviceId?.toString('hex') ? null : id;
|
|
854
|
+
}
|
|
855
|
+
_harnessFor(agent) {
|
|
856
|
+
const id = this._lenderFor(agent);
|
|
857
|
+
const borrow = this._borrowHarness;
|
|
858
|
+
if (!id || !borrow)
|
|
859
|
+
return this._harness;
|
|
860
|
+
const held = this._borrowed.get(id);
|
|
861
|
+
if (held)
|
|
862
|
+
return held;
|
|
863
|
+
const remote = connectHarness(() => borrow(id));
|
|
864
|
+
remote.on('status', ((status) => {
|
|
865
|
+
if (status.state === 'died')
|
|
866
|
+
this._borrowed.delete(id);
|
|
867
|
+
}));
|
|
868
|
+
const harness = remote;
|
|
869
|
+
this._borrowed.set(id, harness);
|
|
870
|
+
return harness;
|
|
871
|
+
}
|
|
872
|
+
_dispatch(request, meshId) {
|
|
1270
873
|
const runId = request.runId;
|
|
1271
874
|
if (!runId || this._runs.has(runId) || this._seen.has(runId))
|
|
1272
875
|
return;
|
|
1273
|
-
this._seen.
|
|
876
|
+
this._seen.set(runId, meshId);
|
|
877
|
+
const engine = meshScoped(this._engine, meshId);
|
|
1274
878
|
const settle = (async () => {
|
|
1275
879
|
const agentId = request.runRequest?.agentId;
|
|
1276
|
-
const agent = await
|
|
880
|
+
const agent = await engine.getAgent({ id: agentId }).catch(noop);
|
|
1277
881
|
if (!agent) {
|
|
1278
882
|
this._seen.delete(runId);
|
|
1279
883
|
return;
|
|
1280
884
|
}
|
|
1281
885
|
if (!agent.providerDeviceId?.equals(this._deviceId))
|
|
1282
886
|
return;
|
|
1283
|
-
if (await this._isHandled(request))
|
|
887
|
+
if (await this._isHandled(engine, request))
|
|
1284
888
|
return;
|
|
1285
889
|
this._log?.info('runs', 'run claimed', { runId, chatId: request.chatId, agentId });
|
|
1286
890
|
const controller = new AbortController();
|
|
1287
891
|
const prior = this._chats.get(request.chatId) ?? Promise.resolve();
|
|
1288
|
-
const done = prior.then(() => this._execute(request, agent, controller));
|
|
892
|
+
const done = prior.then(() => this._execute(request, agent, controller, meshId));
|
|
1289
893
|
const link = done.catch(noop);
|
|
1290
894
|
this._chats.set(request.chatId, link);
|
|
1291
895
|
this._runs.set(runId, {
|
|
@@ -1320,7 +924,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1320
924
|
this._log?.warn('assistant', 'web sources chunk skipped', { runId, err });
|
|
1321
925
|
}
|
|
1322
926
|
}
|
|
1323
|
-
async _searchKnowledge(chatId, messages, attachmentIds, signal, preparing) {
|
|
927
|
+
async _searchKnowledge(engine, chatId, messages, attachmentIds, signal, preparing) {
|
|
1324
928
|
let lastUser;
|
|
1325
929
|
for (let index = messages.length - 1; index >= 0; index--) {
|
|
1326
930
|
if (messages[index].role === 'user') {
|
|
@@ -1332,7 +936,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1332
936
|
if (!lastUser || !query)
|
|
1333
937
|
return null;
|
|
1334
938
|
try {
|
|
1335
|
-
const search = await searchKnowledge(
|
|
939
|
+
const search = await searchKnowledge(engine, {
|
|
1336
940
|
chatId,
|
|
1337
941
|
query,
|
|
1338
942
|
attachmentIds: attachmentIds.length > 0 ? [...attachmentIds] : undefined
|
|
@@ -1350,14 +954,15 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1350
954
|
return null;
|
|
1351
955
|
}
|
|
1352
956
|
}
|
|
1353
|
-
async _isHandled(request) {
|
|
1354
|
-
const { chunks } = await
|
|
957
|
+
async _isHandled(engine, request) {
|
|
958
|
+
const { chunks } = await engine.listChunks({
|
|
1355
959
|
chatId: request.chatId,
|
|
1356
960
|
type: 'run-status'
|
|
1357
961
|
});
|
|
1358
962
|
return (chunks ?? []).some((c) => c.runId === request.runId && TERMINAL_STATES.has(c.runStatus?.state ?? ''));
|
|
1359
963
|
}
|
|
1360
|
-
async _execute(request, agent, controller) {
|
|
964
|
+
async _execute(request, agent, controller, meshId) {
|
|
965
|
+
const engine = meshScoped(this._engine, meshId);
|
|
1361
966
|
const { chatId } = request;
|
|
1362
967
|
const runId = request.runId;
|
|
1363
968
|
const deviceId = this._deviceId;
|
|
@@ -1365,16 +970,19 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1365
970
|
this._seen.delete(runId);
|
|
1366
971
|
return;
|
|
1367
972
|
}
|
|
1368
|
-
if (await this._isHandled(request))
|
|
973
|
+
if (await this._isHandled(engine, request))
|
|
1369
974
|
return;
|
|
1370
|
-
const fresh = await
|
|
975
|
+
const fresh = await engine.getAgent({ id: agent.id }).catch(noop);
|
|
1371
976
|
if (!fresh || fresh.deletedAt || !fresh.providerDeviceId?.equals(deviceId))
|
|
1372
977
|
return;
|
|
1373
978
|
agent = fresh;
|
|
1374
|
-
await this._reconcileCompaction(chatId);
|
|
979
|
+
await this._reconcileCompaction(engine, chatId);
|
|
1375
980
|
if (request.parentId)
|
|
1376
981
|
await this._deleteChatKvCache(chatId, 'superseding run');
|
|
1377
|
-
const
|
|
982
|
+
const lender = this._lenderFor(agent);
|
|
983
|
+
const model = lender
|
|
984
|
+
? agent.modelName
|
|
985
|
+
: runnableModelName(agent.modelName, await this._ensureCapability(), this._mobileClass);
|
|
1378
986
|
if (model !== agent.modelName) {
|
|
1379
987
|
this._log?.warn('runs', 'model clamped to what this device can run', {
|
|
1380
988
|
runId,
|
|
@@ -1386,7 +994,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1386
994
|
if (inflight)
|
|
1387
995
|
this._runs.set(runId, { ...inflight, model });
|
|
1388
996
|
}
|
|
1389
|
-
const config = await
|
|
997
|
+
const config = await engine.getConfig({}).catch(noop);
|
|
1390
998
|
const configuredAgent = withConfigDefaults(agent, config);
|
|
1391
999
|
this._log?.info('runs', 'run model resolved', {
|
|
1392
1000
|
runId,
|
|
@@ -1394,7 +1002,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1394
1002
|
agentModel: agent.modelName,
|
|
1395
1003
|
warmedModel: this._warmedModelName
|
|
1396
1004
|
});
|
|
1397
|
-
const transcript = new RunTranscript(
|
|
1005
|
+
const transcript = new RunTranscript(engine, {
|
|
1398
1006
|
chatId,
|
|
1399
1007
|
runId,
|
|
1400
1008
|
deviceId,
|
|
@@ -1405,11 +1013,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1405
1013
|
chatId,
|
|
1406
1014
|
transcript,
|
|
1407
1015
|
trigger: 'before-run',
|
|
1408
|
-
signal: controller.signal
|
|
1016
|
+
signal: controller.signal,
|
|
1017
|
+
meshId
|
|
1409
1018
|
});
|
|
1410
|
-
const toolConfigsPromise = this._loadToolConfigs(agent.id);
|
|
1019
|
+
const toolConfigsPromise = this._loadToolConfigs(engine, agent.id);
|
|
1411
1020
|
const fsGrantsPromise = this._loadFsGrants(agent.id);
|
|
1412
|
-
const all = await listHistoryChunks(
|
|
1021
|
+
const all = await listHistoryChunks(engine, chatId);
|
|
1413
1022
|
const branches = historyBranches(all, runId);
|
|
1414
1023
|
const currentRepair = branches.repairByRunId.get(runId);
|
|
1415
1024
|
const repairParent = currentRepair
|
|
@@ -1421,11 +1030,11 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1421
1030
|
inputAvailableToRun(chunk, request.seq) &&
|
|
1422
1031
|
(!repairParent || chunk.seq <= repairParent.seq || chunk.runId === repairParent.runId) &&
|
|
1423
1032
|
isPromptChunk(chunk, branches)));
|
|
1424
|
-
const { chunks: priorAsks } = await
|
|
1033
|
+
const { chunks: priorAsks } = await engine.listChunks({
|
|
1425
1034
|
chatId,
|
|
1426
1035
|
type: 'approval-request'
|
|
1427
1036
|
});
|
|
1428
|
-
const { chunks: priorResults } = await
|
|
1037
|
+
const { chunks: priorResults } = await engine.listChunks({ chatId, type: 'tool-result' });
|
|
1429
1038
|
let ran = (priorResults ?? []).some((chunk) => chunk.runId === runId &&
|
|
1430
1039
|
chunk.toolResult?.name !== SKILL_TOOL &&
|
|
1431
1040
|
!isDeniedResult(chunk.toolResult?.result));
|
|
@@ -1462,11 +1071,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1462
1071
|
else {
|
|
1463
1072
|
unreadableImages = await this._hydrateImages(messages, controller.signal, runId);
|
|
1464
1073
|
}
|
|
1465
|
-
const settledWatch = this._watchSettled(chatId, runId, controller);
|
|
1466
|
-
const ownershipWatch = this._watchOwnership(agent.id, controller);
|
|
1074
|
+
const settledWatch = this._watchSettled(engine, chatId, runId, controller);
|
|
1075
|
+
const ownershipWatch = this._watchOwnership(engine, agent.id, controller);
|
|
1467
1076
|
let readinessError = null;
|
|
1468
1077
|
try {
|
|
1469
|
-
|
|
1078
|
+
if (!lender)
|
|
1079
|
+
await this._downloads?.waitUntilCached(model, controller.signal);
|
|
1470
1080
|
}
|
|
1471
1081
|
catch (err) {
|
|
1472
1082
|
readinessError = err instanceof Error ? err : new Error(String(err));
|
|
@@ -1507,7 +1117,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1507
1117
|
? { state: 'executing', progress: { phase: 'queued', busyChatId } }
|
|
1508
1118
|
: { state: 'executing' });
|
|
1509
1119
|
const runStatusId = executing.id;
|
|
1510
|
-
const approvals = this._createApprove(chatId, runId, deviceId, agent.id, () => ran);
|
|
1120
|
+
const approvals = this._createApprove(engine, chatId, runId, deviceId, agent.id, () => ran);
|
|
1511
1121
|
const modelWatch = this._watchModelProgress((runStatus) => transcript.progress(runStatusId, runStatus), busyChatId !== null);
|
|
1512
1122
|
let metrics;
|
|
1513
1123
|
const sourceTagFilter = createRagSourceTagFilter();
|
|
@@ -1529,7 +1139,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1529
1139
|
backfillDocumentOnlyTurn(messages);
|
|
1530
1140
|
const knowledge = !diagramRepair &&
|
|
1531
1141
|
(toolsetEnabled('knowledge', runAgent.disabledToolsets) || attachTurnIds.length > 0)
|
|
1532
|
-
? await this._searchKnowledge(chatId, messages, attachTurnIds, controller.signal, modelWatch.documents)
|
|
1142
|
+
? await this._searchKnowledge(engine, chatId, messages, attachTurnIds, controller.signal, modelWatch.documents)
|
|
1533
1143
|
: null;
|
|
1534
1144
|
if (controller.signal.aborted) {
|
|
1535
1145
|
const outcome = this._abortOutcome(runId, controller.signal.reason);
|
|
@@ -1642,7 +1252,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1642
1252
|
key,
|
|
1643
1253
|
error: err instanceof Error ? err.message : String(err)
|
|
1644
1254
|
}));
|
|
1645
|
-
if (await this._isHandled(request))
|
|
1255
|
+
if (await this._isHandled(engine, request))
|
|
1646
1256
|
return;
|
|
1647
1257
|
const cacheKey = diagramRepair ? undefined : chatRepairKvCacheKey(chatId, acceptedRepairs);
|
|
1648
1258
|
this._log?.info('runs', 'completion requested', {
|
|
@@ -1651,7 +1261,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1651
1261
|
cacheKey,
|
|
1652
1262
|
turns: messages.length
|
|
1653
1263
|
});
|
|
1654
|
-
const events = this.
|
|
1264
|
+
const events = this._harnessFor(runAgent).completion({
|
|
1655
1265
|
model,
|
|
1656
1266
|
agent: runAgent,
|
|
1657
1267
|
messages,
|
|
@@ -1663,10 +1273,10 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1663
1273
|
driveOp,
|
|
1664
1274
|
grantedWriteDirs: write,
|
|
1665
1275
|
approve: approvals.approve,
|
|
1666
|
-
attach: (input) => this._uploadGenerated(chatId, runId, deviceId, input),
|
|
1667
|
-
fetchAttachment: (query) => this._fetchAttachment(chatId, query),
|
|
1276
|
+
attach: (input) => this._uploadGenerated(meshId, chatId, runId, deviceId, input),
|
|
1277
|
+
fetchAttachment: (query) => this._fetchAttachment(engine, chatId, query),
|
|
1668
1278
|
ragSearch: async (input) => {
|
|
1669
|
-
const found = await raceAbort(
|
|
1279
|
+
const found = await raceAbort(engine.ragSearch({
|
|
1670
1280
|
chatId,
|
|
1671
1281
|
query: input.query,
|
|
1672
1282
|
mode: 'tool',
|
|
@@ -1731,7 +1341,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1731
1341
|
chatId,
|
|
1732
1342
|
transcript,
|
|
1733
1343
|
trigger: 'overflow',
|
|
1734
|
-
signal: controller.signal
|
|
1344
|
+
signal: controller.signal,
|
|
1345
|
+
meshId
|
|
1735
1346
|
}));
|
|
1736
1347
|
await transcript.status({
|
|
1737
1348
|
state: 'failed',
|
|
@@ -1787,7 +1398,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1787
1398
|
});
|
|
1788
1399
|
}
|
|
1789
1400
|
else if (event.type === 'tool-activity') {
|
|
1790
|
-
void
|
|
1401
|
+
void engine
|
|
1791
1402
|
.recordToolActivity({
|
|
1792
1403
|
agentId: runAgent.id,
|
|
1793
1404
|
toolName: event.name,
|
|
@@ -1816,19 +1427,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1816
1427
|
});
|
|
1817
1428
|
}
|
|
1818
1429
|
const citedTitles = ragInjected ? citedByPhraseOverlap(answerText, retrievedRecords) : null;
|
|
1819
|
-
const foldedNow = metrics
|
|
1820
|
-
? await this._compactIfNearFull({
|
|
1821
|
-
chatId,
|
|
1822
|
-
transcript,
|
|
1823
|
-
trigger: 'turn-filled-window',
|
|
1824
|
-
metrics: metrics,
|
|
1825
|
-
signal: controller.signal
|
|
1826
|
-
})
|
|
1827
|
-
: false;
|
|
1828
|
-
const settled = foldedNow ? withoutOccupancy(metrics) : metrics;
|
|
1829
1430
|
await transcript.status({
|
|
1830
1431
|
state: 'completed',
|
|
1831
|
-
metrics: citedTitles ? { ...
|
|
1432
|
+
metrics: citedTitles ? { ...metrics, citedTitles } : metrics
|
|
1832
1433
|
});
|
|
1833
1434
|
titleOutcome = 'completed';
|
|
1834
1435
|
}
|
|
@@ -1843,7 +1444,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1843
1444
|
if (outcome?.kind === 'handoff')
|
|
1844
1445
|
return;
|
|
1845
1446
|
await flushTranscript();
|
|
1846
|
-
|
|
1447
|
+
const failure = (this._lenderFor(runAgent) ? loanFailureNotice(err) : null) ?? runFailureNotice(err);
|
|
1448
|
+
await transcript.status({ state: 'failed', reason: failure, metrics });
|
|
1847
1449
|
titleOutcome = 'ended';
|
|
1848
1450
|
}
|
|
1849
1451
|
finally {
|
|
@@ -1853,12 +1455,12 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1853
1455
|
await approvals.settle({ preservePending: isLifecycleTeardown(controller.signal.reason) });
|
|
1854
1456
|
settledWatch.stop();
|
|
1855
1457
|
if (titleOutcome && !diagramRepair) {
|
|
1856
|
-
void this._autoTitle(chatId, messages, model, runAgent, titleOutcome === 'completed');
|
|
1458
|
+
void this._autoTitle(engine, chatId, messages, model, runAgent, titleOutcome === 'completed');
|
|
1857
1459
|
}
|
|
1858
1460
|
}
|
|
1859
1461
|
}
|
|
1860
|
-
async _loadToolConfigs(agentId) {
|
|
1861
|
-
const { configs } = await
|
|
1462
|
+
async _loadToolConfigs(engine, agentId) {
|
|
1463
|
+
const { configs } = await engine.listToolConfigs({ agentId }).catch((err) => {
|
|
1862
1464
|
this._log?.warn('assistant', 'tool configs read failed', err);
|
|
1863
1465
|
return { configs: [] };
|
|
1864
1466
|
});
|
|
@@ -1891,17 +1493,23 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1891
1493
|
});
|
|
1892
1494
|
return grantedDirsFrom(value);
|
|
1893
1495
|
}
|
|
1894
|
-
async _fetchAttachment(chatId, { attachmentId, signal, meta } = {}) {
|
|
1496
|
+
async _fetchAttachment(engine, chatId, { attachmentId, kind = 'image', signal, meta } = {}) {
|
|
1895
1497
|
if (!attachmentId) {
|
|
1896
|
-
const { chunks } = await raceAbort(
|
|
1498
|
+
const { chunks } = await raceAbort(engine.listChunks({ chatId, type: 'attachment' }), signal);
|
|
1897
1499
|
attachmentId = (chunks ?? [])
|
|
1898
1500
|
.map((chunk) => chunk.attachment)
|
|
1899
|
-
.filter((file) =>
|
|
1501
|
+
.filter((file) => {
|
|
1502
|
+
if (!file || file.deletedAt)
|
|
1503
|
+
return false;
|
|
1504
|
+
return kind === 'audio'
|
|
1505
|
+
? isAudioAttachment(file.fileName, file.mimeType)
|
|
1506
|
+
: IMAGE_MIMES.has(file.mimeType.toLowerCase());
|
|
1507
|
+
})
|
|
1900
1508
|
.at(-1)?.id;
|
|
1901
1509
|
}
|
|
1902
1510
|
if (!attachmentId)
|
|
1903
1511
|
return null;
|
|
1904
|
-
const stream =
|
|
1512
|
+
const stream = engine.fileRead({ chatId, fileId: attachmentId, metadataOnly: meta });
|
|
1905
1513
|
const abort = () => stream.destroy();
|
|
1906
1514
|
const pieces = [];
|
|
1907
1515
|
let head = null;
|
|
@@ -1930,7 +1538,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1930
1538
|
async _readBlob(url, signal) {
|
|
1931
1539
|
if (!url || signal?.aborted)
|
|
1932
1540
|
return null;
|
|
1933
|
-
const pending = defaultFetch(url).catch(
|
|
1541
|
+
const pending = defaultFetch(url).catch(noop);
|
|
1934
1542
|
try {
|
|
1935
1543
|
const response = await raceInactivity(pending, signal, this._blobReadTimeoutMs);
|
|
1936
1544
|
const bytes = response?.ok
|
|
@@ -1943,9 +1551,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
1943
1551
|
return null;
|
|
1944
1552
|
}
|
|
1945
1553
|
}
|
|
1946
|
-
async _uploadGenerated(chatId, runId, deviceId, { fileName, mimeType, bytes, raw }) {
|
|
1554
|
+
async _uploadGenerated(meshId, chatId, runId, deviceId, { fileName, mimeType, bytes, raw }) {
|
|
1947
1555
|
const duplex = this._engine.uploadAttachment();
|
|
1948
|
-
duplex.write({ chatId, runId, originDeviceId: deviceId, fileName, mimeType, raw });
|
|
1556
|
+
duplex.write({ chatId, runId, originDeviceId: deviceId, fileName, mimeType, raw, meshId });
|
|
1949
1557
|
const buf = Buffer.isBuffer(bytes)
|
|
1950
1558
|
? bytes
|
|
1951
1559
|
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
@@ -2039,7 +1647,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2039
1647
|
}
|
|
2040
1648
|
async _hydrateImage(dir, image, signal) {
|
|
2041
1649
|
const file = path.join(dir, imageCacheName(image));
|
|
2042
|
-
const cached = await fs.promises.stat(file).catch(
|
|
1650
|
+
const cached = await fs.promises.stat(file).catch(noop);
|
|
2043
1651
|
if (!cached?.size) {
|
|
2044
1652
|
const raw = await this._readBlob(image.url, signal);
|
|
2045
1653
|
if (signal.aborted)
|
|
@@ -2053,13 +1661,13 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2053
1661
|
}
|
|
2054
1662
|
else {
|
|
2055
1663
|
const now = new Date();
|
|
2056
|
-
await fs.promises.utimes(file, now, now).catch(
|
|
1664
|
+
await fs.promises.utimes(file, now, now).catch(noop);
|
|
2057
1665
|
}
|
|
2058
1666
|
return file;
|
|
2059
1667
|
}
|
|
2060
1668
|
async _compactIfNearFull(params) {
|
|
2061
|
-
const { chatId, transcript, trigger, signal } = params;
|
|
2062
|
-
const metrics =
|
|
1669
|
+
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
1670
|
+
const metrics = await this._lastRunMetrics(chatId, meshId);
|
|
2063
1671
|
if (!nearContextLimit(metrics.cacheTokens, metrics.ctxSize))
|
|
2064
1672
|
return false;
|
|
2065
1673
|
if (!canCompact(metrics.ctxSize)) {
|
|
@@ -2069,10 +1677,17 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2069
1677
|
});
|
|
2070
1678
|
return false;
|
|
2071
1679
|
}
|
|
2072
|
-
return this._runCompaction({
|
|
1680
|
+
return this._runCompaction({
|
|
1681
|
+
chatId,
|
|
1682
|
+
transcript,
|
|
1683
|
+
trigger,
|
|
1684
|
+
signal,
|
|
1685
|
+
occupancy: metrics,
|
|
1686
|
+
meshId
|
|
1687
|
+
});
|
|
2073
1688
|
}
|
|
2074
1689
|
async _runCompaction(params) {
|
|
2075
|
-
const { chatId, transcript, trigger, signal } = params;
|
|
1690
|
+
const { chatId, transcript, trigger, signal, meshId } = params;
|
|
2076
1691
|
let statusId = null;
|
|
2077
1692
|
const started = Date.now();
|
|
2078
1693
|
try {
|
|
@@ -2084,7 +1699,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2084
1699
|
});
|
|
2085
1700
|
statusId = status.id;
|
|
2086
1701
|
},
|
|
2087
|
-
...(signal ? { signal } : {})
|
|
1702
|
+
...(signal ? { signal } : {}),
|
|
1703
|
+
...(meshId ? { meshId } : {})
|
|
2088
1704
|
});
|
|
2089
1705
|
this._log?.info('runs', 'compaction', {
|
|
2090
1706
|
chatId,
|
|
@@ -2109,9 +1725,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2109
1725
|
}
|
|
2110
1726
|
}
|
|
2111
1727
|
}
|
|
2112
|
-
async _lastRunMetrics(chatId) {
|
|
1728
|
+
async _lastRunMetrics(chatId, meshId) {
|
|
2113
1729
|
const deviceId = this._deviceId;
|
|
2114
|
-
const { chunks } = await this._engine
|
|
1730
|
+
const { chunks } = await meshScoped(this._engine, meshId)
|
|
2115
1731
|
.listChunks({ chatId, type: 'run-status' })
|
|
2116
1732
|
.catch(() => ({ chunks: [] }));
|
|
2117
1733
|
const statuses = chunks ?? [];
|
|
@@ -2130,24 +1746,25 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2130
1746
|
async compactChat(chatId, options = {}) {
|
|
2131
1747
|
if (!this.opened)
|
|
2132
1748
|
await this.ready();
|
|
1749
|
+
const engine = meshScoped(this._engine, options.meshId);
|
|
2133
1750
|
const deviceId = this._deviceId;
|
|
2134
|
-
const chat = await
|
|
1751
|
+
const chat = await engine.getChat({ id: chatId }).catch(noop);
|
|
2135
1752
|
if (!chat)
|
|
2136
1753
|
return { compacted: false, reason: 'unknown chat' };
|
|
2137
|
-
const agent = await
|
|
1754
|
+
const agent = await engine.getAgent({ id: chat.agentId }).catch(noop);
|
|
2138
1755
|
if (!agent || !agent.providerDeviceId?.equals(deviceId)) {
|
|
2139
1756
|
return { compacted: false, reason: 'not the provider device' };
|
|
2140
1757
|
}
|
|
2141
1758
|
const model = runnableModelName(agent.modelName, await this._ensureCapability(), this._mobileClass);
|
|
2142
|
-
const config = await
|
|
1759
|
+
const config = await engine.getConfig({}).catch(noop);
|
|
2143
1760
|
const ctxSize = agent.ctxSize || config?.ctxSize || undefined;
|
|
2144
1761
|
if (ctxSize !== undefined && !canCompact(ctxSize)) {
|
|
2145
1762
|
return { compacted: false, reason: 'below-floor' };
|
|
2146
1763
|
}
|
|
2147
|
-
const stored = await listHistoryChunks(
|
|
1764
|
+
const stored = await listHistoryChunks(engine, chatId);
|
|
2148
1765
|
const branches = historyBranches(stored);
|
|
2149
1766
|
const chunks = stored.filter((chunk) => isPromptChunk(chunk, branches));
|
|
2150
|
-
const { chunks: priorAsks } = await
|
|
1767
|
+
const { chunks: priorAsks } = await engine
|
|
2151
1768
|
.listChunks({ chatId, type: 'approval-request' })
|
|
2152
1769
|
.catch(() => ({ chunks: [] }));
|
|
2153
1770
|
const lastThroughId = lastCompactedThroughId(chunks);
|
|
@@ -2184,7 +1801,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2184
1801
|
}
|
|
2185
1802
|
if (options.signal?.aborted)
|
|
2186
1803
|
return { compacted: false, reason: 'canceled' };
|
|
2187
|
-
await
|
|
1804
|
+
await engine.appendChunk({
|
|
2188
1805
|
chatId,
|
|
2189
1806
|
type: 'compaction',
|
|
2190
1807
|
compaction: { summary, throughId: boundary.throughId },
|
|
@@ -2201,7 +1818,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2201
1818
|
}
|
|
2202
1819
|
async _summarize(turns, model, agent, signal) {
|
|
2203
1820
|
const hasPriorSummary = turns[0]?.content.startsWith(COMPACTION_TURN_PREFIX) === true;
|
|
2204
|
-
const events = this.
|
|
1821
|
+
const events = this._harnessFor(agent).completion({
|
|
2205
1822
|
model,
|
|
2206
1823
|
agent: {
|
|
2207
1824
|
...agent,
|
|
@@ -2241,22 +1858,22 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2241
1858
|
}
|
|
2242
1859
|
return { summary: parseCompactionSummary(text), overflowed: false };
|
|
2243
1860
|
}
|
|
2244
|
-
async _autoTitle(chatId, messages, model, agent, probe) {
|
|
1861
|
+
async _autoTitle(engine, chatId, messages, model, agent, probe) {
|
|
2245
1862
|
try {
|
|
2246
1863
|
const first = messages.find((turn) => turn.role === 'user' && turn.content.trim());
|
|
2247
1864
|
if (!first)
|
|
2248
1865
|
return;
|
|
2249
|
-
const chat = await
|
|
1866
|
+
const chat = await engine.getChat({ id: chatId });
|
|
2250
1867
|
if (!isDefaultChatTitle(chat?.title))
|
|
2251
1868
|
return;
|
|
2252
|
-
await this._generateChatTitle({ chatId, input: first.content, model, agent, probe });
|
|
1869
|
+
await this._generateChatTitle({ engine, chatId, input: first.content, model, agent, probe });
|
|
2253
1870
|
}
|
|
2254
1871
|
catch (err) {
|
|
2255
1872
|
this._log?.warn('runs', 'auto-title skipped', { chatId, err });
|
|
2256
1873
|
}
|
|
2257
1874
|
}
|
|
2258
1875
|
async _generateChatTitle(params) {
|
|
2259
|
-
const { chatId, input, model, agent, probe } = params;
|
|
1876
|
+
const { engine, chatId, input, model, agent, probe } = params;
|
|
2260
1877
|
const prompt = input.trim();
|
|
2261
1878
|
if (!prompt)
|
|
2262
1879
|
return null;
|
|
@@ -2277,7 +1894,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2277
1894
|
const title = generated ?? deriveChatTitle(prompt);
|
|
2278
1895
|
if (!title)
|
|
2279
1896
|
return null;
|
|
2280
|
-
await
|
|
1897
|
+
await engine.renameChat({ id: chatId, title });
|
|
2281
1898
|
this._log?.info('runs', 'chat titled', {
|
|
2282
1899
|
chatId,
|
|
2283
1900
|
title,
|
|
@@ -2287,7 +1904,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2287
1904
|
}
|
|
2288
1905
|
async _titleFromCompletion(params) {
|
|
2289
1906
|
const { input, model, agent, signal } = params;
|
|
2290
|
-
const events = this.
|
|
1907
|
+
const events = this._harnessFor(agent).completion({
|
|
2291
1908
|
model,
|
|
2292
1909
|
agent: {
|
|
2293
1910
|
...agent,
|
|
@@ -2321,7 +1938,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2321
1938
|
}
|
|
2322
1939
|
return parseTitleResponse(text);
|
|
2323
1940
|
}
|
|
2324
|
-
_createApprove(chatId, runId, deviceId, agentId, hasRun) {
|
|
1941
|
+
_createApprove(engine, chatId, runId, deviceId, agentId, hasRun) {
|
|
2325
1942
|
const waiters = new Map();
|
|
2326
1943
|
let latestChunks = [];
|
|
2327
1944
|
let watcher = null;
|
|
@@ -2353,7 +1970,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2353
1970
|
const ensureWatch = () => {
|
|
2354
1971
|
if (watcher)
|
|
2355
1972
|
return;
|
|
2356
|
-
const opened =
|
|
1973
|
+
const opened = engine
|
|
2357
1974
|
.chunksWatch({ chatId, type: 'approval-request' })[Symbol.asyncIterator]();
|
|
2358
1975
|
watcher = opened;
|
|
2359
1976
|
pump(opened)
|
|
@@ -2377,7 +1994,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2377
1994
|
});
|
|
2378
1995
|
};
|
|
2379
1996
|
const asksForRun = async () => {
|
|
2380
|
-
const { chunks } = await
|
|
1997
|
+
const { chunks } = await engine
|
|
2381
1998
|
.listChunks({ chatId, type: 'approval-request' })
|
|
2382
1999
|
.catch(() => ({ chunks: [] }));
|
|
2383
2000
|
return (chunks ?? []).filter((chunk) => chunk.runId === runId);
|
|
@@ -2399,14 +2016,15 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2399
2016
|
return new Promise((resolve) => waiters.set(chunkId, { resolve, tool }));
|
|
2400
2017
|
};
|
|
2401
2018
|
const approve = async (call) => {
|
|
2402
|
-
const { rules } = await
|
|
2019
|
+
const { rules, ourMemberId } = await engine.listApprovalRules({}).catch((err) => {
|
|
2403
2020
|
this._log?.warn('approvals', 'rule lookup failed, asking instead', { runId, err });
|
|
2404
|
-
return { rules: [] };
|
|
2021
|
+
return { rules: [], ourMemberId: undefined };
|
|
2405
2022
|
});
|
|
2406
2023
|
const rule = matchApprovalRule(rules ?? [], {
|
|
2407
2024
|
toolName: call.name,
|
|
2408
2025
|
resource: call.resource,
|
|
2409
|
-
agentId
|
|
2026
|
+
agentId,
|
|
2027
|
+
memberId: ourMemberId
|
|
2410
2028
|
});
|
|
2411
2029
|
if (rule) {
|
|
2412
2030
|
this._log?.info('approvals', `rule ${rule.decision}`, {
|
|
@@ -2453,7 +2071,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2453
2071
|
resource: call.resource ?? undefined,
|
|
2454
2072
|
agentId
|
|
2455
2073
|
};
|
|
2456
|
-
const chunk = await
|
|
2074
|
+
const chunk = await engine.appendChunk({
|
|
2457
2075
|
chatId,
|
|
2458
2076
|
runId,
|
|
2459
2077
|
type: 'approval-request',
|
|
@@ -2482,7 +2100,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2482
2100
|
chunkId: ask.id,
|
|
2483
2101
|
tool: ask.approvalRequest?.name
|
|
2484
2102
|
});
|
|
2485
|
-
await
|
|
2103
|
+
await engine.resolveApproval({ id: ask.id, approved: false });
|
|
2486
2104
|
}
|
|
2487
2105
|
for (const waiter of waiters.values())
|
|
2488
2106
|
waiter.resolve(false);
|
|
@@ -2508,8 +2126,8 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2508
2126
|
}
|
|
2509
2127
|
return { kind: 'failed', reason: RUN_FAILURE_REASONS.runtimeEndedTurn };
|
|
2510
2128
|
}
|
|
2511
|
-
_watchSettled(chatId, runId, controller) {
|
|
2512
|
-
const it =
|
|
2129
|
+
_watchSettled(engine, chatId, runId, controller) {
|
|
2130
|
+
const it = engine.chunksWatch({ chatId, type: 'run-status' })[Symbol.asyncIterator]();
|
|
2513
2131
|
const watching = (async () => {
|
|
2514
2132
|
while (true) {
|
|
2515
2133
|
const { value, done } = await it.next();
|
|
@@ -2533,9 +2151,9 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2533
2151
|
}
|
|
2534
2152
|
};
|
|
2535
2153
|
}
|
|
2536
|
-
_watchOwnership(agentId, controller) {
|
|
2154
|
+
_watchOwnership(engine, agentId, controller) {
|
|
2537
2155
|
const deviceId = this._deviceId;
|
|
2538
|
-
const it =
|
|
2156
|
+
const it = engine.agentsWatch({})[Symbol.asyncIterator]();
|
|
2539
2157
|
const watching = (async () => {
|
|
2540
2158
|
while (true) {
|
|
2541
2159
|
const { value, done } = await it.next();
|
|
@@ -2640,7 +2258,7 @@ export class QvacAssistant extends ReadyResource {
|
|
|
2640
2258
|
}
|
|
2641
2259
|
function noop() { }
|
|
2642
2260
|
function runMetricsSummary(metrics) {
|
|
2643
|
-
const { timeToFirstToken, promptTokens, promptTokensPerSecond, cacheTokens, tokensPerSecond, totalTokens, durationMs } = metrics;
|
|
2261
|
+
const { timeToFirstToken, promptTokens, promptTokensPerSecond, cacheTokens, tokensPerSecond, totalTokens, durationMs, stopReason } = metrics;
|
|
2644
2262
|
return {
|
|
2645
2263
|
timeToFirstToken,
|
|
2646
2264
|
promptTokens,
|
|
@@ -2648,60 +2266,10 @@ function runMetricsSummary(metrics) {
|
|
|
2648
2266
|
cacheTokens,
|
|
2649
2267
|
tokensPerSecond,
|
|
2650
2268
|
totalTokens,
|
|
2651
|
-
durationMs
|
|
2269
|
+
durationMs,
|
|
2270
|
+
stopReason
|
|
2652
2271
|
};
|
|
2653
2272
|
}
|
|
2654
2273
|
function isStreamDestroyed(err) {
|
|
2655
2274
|
return typeof err === 'object' && err !== null && 'code' in err && err.code === 'STREAM_DESTROYED';
|
|
2656
2275
|
}
|
|
2657
|
-
function harnessDeathReason(status) {
|
|
2658
|
-
const HARNESS_DIED = RUN_FAILURE_REASONS.harnessDied;
|
|
2659
|
-
if (typeof status !== 'object' || status === null)
|
|
2660
|
-
return HARNESS_DIED;
|
|
2661
|
-
const record = status;
|
|
2662
|
-
const detail = typeof record.error === 'string' && record.error ? record.error : null;
|
|
2663
|
-
const code = typeof record.code === 'string' && record.code ? record.code : null;
|
|
2664
|
-
if (!detail)
|
|
2665
|
-
return code ? `${HARNESS_DIED} (${code})` : HARNESS_DIED;
|
|
2666
|
-
return code ? `${HARNESS_DIED}: ${detail} (${code})` : `${HARNESS_DIED}: ${detail}`;
|
|
2667
|
-
}
|
|
2668
|
-
function harnessDied(status) {
|
|
2669
|
-
if (typeof status !== 'object' || status === null || !('state' in status))
|
|
2670
|
-
return false;
|
|
2671
|
-
return status.state === 'died';
|
|
2672
|
-
}
|
|
2673
|
-
function modelCatalogChanged(state) {
|
|
2674
|
-
return (typeof state === 'object' &&
|
|
2675
|
-
state !== null &&
|
|
2676
|
-
'catalogChanged' in state &&
|
|
2677
|
-
state.catalogChanged === true);
|
|
2678
|
-
}
|
|
2679
|
-
function imageDownloadProgress(state) {
|
|
2680
|
-
if (typeof state !== 'object' ||
|
|
2681
|
-
state === null ||
|
|
2682
|
-
!('status' in state) ||
|
|
2683
|
-
(state.status !== 'downloading' && state.status !== 'downloaded') ||
|
|
2684
|
-
!('model' in state) ||
|
|
2685
|
-
typeof state.model !== 'string' ||
|
|
2686
|
-
!('percentage' in state) ||
|
|
2687
|
-
typeof state.percentage !== 'number' ||
|
|
2688
|
-
!Number.isFinite(state.percentage) ||
|
|
2689
|
-
!('downloaded' in state) ||
|
|
2690
|
-
typeof state.downloaded !== 'number' ||
|
|
2691
|
-
!Number.isFinite(state.downloaded) ||
|
|
2692
|
-
!('total' in state) ||
|
|
2693
|
-
typeof state.total !== 'number' ||
|
|
2694
|
-
!Number.isFinite(state.total)) {
|
|
2695
|
-
return null;
|
|
2696
|
-
}
|
|
2697
|
-
const model = diffusionDownloadName(state.model);
|
|
2698
|
-
if (!model)
|
|
2699
|
-
return null;
|
|
2700
|
-
return {
|
|
2701
|
-
model,
|
|
2702
|
-
phase: state.status === 'downloading' ? 'downloading' : 'complete',
|
|
2703
|
-
percentage: state.percentage,
|
|
2704
|
-
downloaded: state.downloaded,
|
|
2705
|
-
total: state.total
|
|
2706
|
-
};
|
|
2707
|
-
}
|