@qvac/harness 0.7.1 → 1.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/README.md +224 -0
  2. package/dist-lib/index.d.ts +55 -3
  3. package/dist-lib/index.js +721 -236
  4. package/dist-lib/lib/capabilities/completion-engine/abort-polyfill.d.ts +1 -0
  5. package/dist-lib/lib/capabilities/completion-engine/abort-polyfill.js +10 -0
  6. package/dist-lib/lib/capabilities/completion-engine/actors/answering.d.ts +3 -0
  7. package/dist-lib/lib/capabilities/completion-engine/actors/answering.js +17 -0
  8. package/dist-lib/lib/capabilities/completion-engine/actors/approval.d.ts +4 -0
  9. package/dist-lib/lib/capabilities/completion-engine/actors/approval.js +29 -0
  10. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.d.ts +4 -0
  11. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.js +73 -0
  12. package/dist-lib/lib/capabilities/completion-engine/actors/execute.d.ts +41 -0
  13. package/dist-lib/lib/capabilities/completion-engine/actors/execute.js +49 -0
  14. package/dist-lib/lib/capabilities/completion-engine/actors/executing.d.ts +3 -0
  15. package/dist-lib/lib/capabilities/completion-engine/actors/executing.js +102 -0
  16. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.d.ts +47 -0
  17. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +87 -0
  18. package/dist-lib/lib/capabilities/completion-engine/actors/finishing.d.ts +2 -0
  19. package/dist-lib/lib/capabilities/completion-engine/actors/finishing.js +32 -0
  20. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-preload.d.ts +3 -0
  21. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-preload.js +75 -0
  22. package/dist-lib/lib/capabilities/completion-engine/actors/inspecting.d.ts +3 -0
  23. package/dist-lib/lib/capabilities/completion-engine/actors/inspecting.js +152 -0
  24. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.d.ts +3 -0
  25. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.js +94 -0
  26. package/dist-lib/lib/capabilities/completion-engine/actors/preload-failure.d.ts +9 -0
  27. package/dist-lib/lib/capabilities/completion-engine/actors/preload-failure.js +11 -0
  28. package/dist-lib/lib/capabilities/completion-engine/actors/preloading.d.ts +3 -0
  29. package/dist-lib/lib/capabilities/completion-engine/actors/preloading.js +114 -0
  30. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.d.ts +3 -0
  31. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +198 -0
  32. package/dist-lib/lib/capabilities/completion-engine/actors/rejecting.d.ts +3 -0
  33. package/dist-lib/lib/capabilities/completion-engine/actors/rejecting.js +71 -0
  34. package/dist-lib/lib/capabilities/completion-engine/actors/rejection.d.ts +18 -0
  35. package/dist-lib/lib/capabilities/completion-engine/actors/rejection.js +70 -0
  36. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.d.ts +3 -0
  37. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.js +60 -0
  38. package/dist-lib/lib/capabilities/completion-engine/actors/stream.d.ts +21 -0
  39. package/dist-lib/lib/capabilities/completion-engine/actors/stream.js +48 -0
  40. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.d.ts +3 -0
  41. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +156 -0
  42. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.d.ts +7 -0
  43. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +61 -0
  44. package/dist-lib/lib/capabilities/completion-engine/driver.d.ts +25 -0
  45. package/dist-lib/lib/capabilities/completion-engine/driver.js +553 -0
  46. package/dist-lib/lib/capabilities/completion-engine/durable.d.ts +61 -0
  47. package/dist-lib/lib/capabilities/completion-engine/durable.js +150 -0
  48. package/dist-lib/lib/capabilities/completion-engine/operations.d.ts +6 -0
  49. package/dist-lib/lib/capabilities/completion-engine/operations.js +239 -0
  50. package/dist-lib/lib/capabilities/completion-engine/ports.d.ts +3 -0
  51. package/dist-lib/lib/capabilities/completion-engine/ports.js +267 -0
  52. package/dist-lib/lib/capabilities/completion-engine/session.d.ts +76 -0
  53. package/dist-lib/lib/capabilities/completion-engine/session.js +70 -0
  54. package/dist-lib/lib/capabilities/completion-loop/index.d.ts +80 -0
  55. package/dist-lib/lib/capabilities/completion-loop/index.js +158 -0
  56. package/dist-lib/lib/capabilities/completion-loop/metrics.d.ts +5 -0
  57. package/dist-lib/lib/capabilities/completion-loop/metrics.js +29 -0
  58. package/dist-lib/lib/capabilities/completion-loop/round.d.ts +33 -0
  59. package/dist-lib/lib/capabilities/completion-loop/round.js +353 -0
  60. package/dist-lib/lib/capabilities/completion-loop/tool-execution.d.ts +60 -0
  61. package/dist-lib/lib/capabilities/completion-loop/tool-execution.js +270 -0
  62. package/dist-lib/lib/capabilities/completion-loop/turns.d.ts +23 -0
  63. package/dist-lib/lib/capabilities/completion-loop/turns.js +112 -0
  64. package/dist-lib/lib/capabilities/completion-runtime.d.ts +46 -0
  65. package/dist-lib/lib/capabilities/completion-runtime.js +74 -0
  66. package/dist-lib/lib/capabilities/completion.d.ts +18 -335
  67. package/dist-lib/lib/capabilities/completion.js +20 -1301
  68. package/dist-lib/lib/capabilities/download.d.ts +1 -1
  69. package/dist-lib/lib/capabilities/download.js +7 -2
  70. package/dist-lib/lib/capabilities/invoke.js +5 -3
  71. package/dist-lib/lib/capabilities/tool-plan.d.ts +35 -0
  72. package/dist-lib/lib/capabilities/tool-plan.js +66 -0
  73. package/dist-lib/lib/capabilities/tool-turns.d.ts +11 -2
  74. package/dist-lib/lib/capabilities/tool-turns.js +30 -2
  75. package/dist-lib/lib/capabilities/types.d.ts +287 -0
  76. package/dist-lib/lib/capabilities/types.js +1 -0
  77. package/dist-lib/lib/completion-lifecycle.d.ts +14 -0
  78. package/dist-lib/lib/completion-lifecycle.js +126 -0
  79. package/dist-lib/lib/connect.d.ts +10 -3
  80. package/dist-lib/lib/connect.js +329 -23
  81. package/dist-lib/lib/contract-types.d.ts +19 -2
  82. package/dist-lib/lib/harness-lifecycle.d.ts +66 -0
  83. package/dist-lib/lib/harness-lifecycle.js +129 -0
  84. package/dist-lib/lib/helpers/detach-json.d.ts +1 -0
  85. package/dist-lib/lib/helpers/detach-json.js +146 -0
  86. package/dist-lib/lib/helpers/markup-scanner.d.ts +26 -0
  87. package/dist-lib/lib/helpers/markup-scanner.js +156 -0
  88. package/dist-lib/lib/helpers/markup-stream-filter.js +138 -35
  89. package/dist-lib/lib/helpers/race-abort.d.ts +9 -4
  90. package/dist-lib/lib/helpers/race-abort.js +2 -7
  91. package/dist-lib/lib/helpers/recovery-required.d.ts +1 -0
  92. package/dist-lib/lib/helpers/recovery-required.js +1 -0
  93. package/dist-lib/lib/helpers/run-outcome.d.ts +10 -2
  94. package/dist-lib/lib/helpers/run-outcome.js +8 -5
  95. package/dist-lib/lib/helpers/tool-call-markup.d.ts +2 -0
  96. package/dist-lib/lib/helpers/tool-call-markup.js +197 -82
  97. package/dist-lib/lib/hooks/built-in/arg-repair.d.ts +1 -0
  98. package/dist-lib/lib/hooks/built-in/arg-repair.js +1 -0
  99. package/dist-lib/lib/hooks/built-in/egress.d.ts +1 -0
  100. package/dist-lib/lib/hooks/built-in/egress.js +2 -1
  101. package/dist-lib/lib/hooks/built-in/sandbox.d.ts +1 -0
  102. package/dist-lib/lib/hooks/built-in/sandbox.js +6 -1
  103. package/dist-lib/lib/hooks/built-in/skill-catalog.d.ts +1 -0
  104. package/dist-lib/lib/hooks/built-in/skill-catalog.js +2 -1
  105. package/dist-lib/lib/hooks/pipeline.d.ts +4 -1
  106. package/dist-lib/lib/hooks/pipeline.js +24 -2
  107. package/dist-lib/lib/hooks/run.d.ts +2 -1
  108. package/dist-lib/lib/hooks/run.js +19 -7
  109. package/dist-lib/lib/hooks/types.d.ts +17 -6
  110. package/dist-lib/lib/machine/completion/actions/approval.d.ts +3 -0
  111. package/dist-lib/lib/machine/completion/actions/approval.js +16 -0
  112. package/dist-lib/lib/machine/completion/actions/completion.d.ts +23 -0
  113. package/dist-lib/lib/machine/completion/actions/completion.js +142 -0
  114. package/dist-lib/lib/machine/completion/actions/skills.d.ts +9 -0
  115. package/dist-lib/lib/machine/completion/actions/skills.js +83 -0
  116. package/dist-lib/lib/machine/completion/actions/tools.d.ts +16 -0
  117. package/dist-lib/lib/machine/completion/actions/tools.js +173 -0
  118. package/dist-lib/lib/machine/completion/context.d.ts +9 -0
  119. package/dist-lib/lib/machine/completion/context.js +44 -0
  120. package/dist-lib/lib/machine/completion/contract.d.ts +6 -0
  121. package/dist-lib/lib/machine/completion/contract.js +1 -0
  122. package/dist-lib/lib/machine/completion/guards/retry.d.ts +9 -0
  123. package/dist-lib/lib/machine/completion/guards/retry.js +25 -0
  124. package/dist-lib/lib/machine/completion/implementations.d.ts +81 -0
  125. package/dist-lib/lib/machine/completion/implementations.js +152 -0
  126. package/dist-lib/lib/machine/completion/machine.d.ts +375 -0
  127. package/dist-lib/lib/machine/completion/machine.js +511 -0
  128. package/dist-lib/lib/machine/completion/ports.d.ts +65 -0
  129. package/dist-lib/lib/machine/completion/ports.js +224 -0
  130. package/dist-lib/lib/machine/completion/rules.d.ts +39 -0
  131. package/dist-lib/lib/machine/completion/rules.js +115 -0
  132. package/dist-lib/lib/machine/completion/types.d.ts +493 -0
  133. package/dist-lib/lib/machine/completion/types.js +1 -0
  134. package/dist-lib/lib/machine/engine.d.ts +93 -0
  135. package/dist-lib/lib/machine/engine.js +483 -0
  136. package/dist-lib/lib/machine/errors.d.ts +44 -0
  137. package/dist-lib/lib/machine/errors.js +373 -0
  138. package/dist-lib/lib/machine/guards.d.ts +2 -0
  139. package/dist-lib/lib/machine/guards.js +6 -0
  140. package/dist-lib/lib/machine/index.d.ts +20 -0
  141. package/dist-lib/lib/machine/index.js +11 -0
  142. package/dist-lib/lib/machine/replay-output.d.ts +24 -0
  143. package/dist-lib/lib/machine/replay-output.js +50 -0
  144. package/dist-lib/lib/machine/stable-stringify.d.ts +1 -0
  145. package/dist-lib/lib/machine/stable-stringify.js +11 -0
  146. package/dist-lib/lib/models/model-pool.d.ts +14 -6
  147. package/dist-lib/lib/models/model-pool.js +95 -30
  148. package/dist-lib/lib/run-log/history-codec.d.ts +23 -0
  149. package/dist-lib/lib/run-log/history-codec.js +973 -0
  150. package/dist-lib/lib/run-log/patch.d.ts +6 -0
  151. package/dist-lib/lib/run-log/patch.js +93 -0
  152. package/dist-lib/lib/run-log/persisted-actor.d.ts +18 -0
  153. package/dist-lib/lib/run-log/persisted-actor.js +218 -0
  154. package/dist-lib/lib/run-log/protocol-types.d.ts +153 -0
  155. package/dist-lib/lib/run-log/protocol-types.js +1 -0
  156. package/dist-lib/lib/run-log/protocol.d.ts +27 -0
  157. package/dist-lib/lib/run-log/protocol.js +1056 -0
  158. package/dist-lib/lib/run-log/replay.d.ts +25 -0
  159. package/dist-lib/lib/run-log/replay.js +129 -0
  160. package/dist-lib/lib/run-log/run-journal.d.ts +125 -0
  161. package/dist-lib/lib/run-log/run-journal.js +783 -0
  162. package/dist-lib/lib/run-log-inspect.d.ts +25 -0
  163. package/dist-lib/lib/run-log-inspect.js +627 -0
  164. package/dist-lib/lib/run-log-protocol.d.ts +2 -0
  165. package/dist-lib/lib/run-log-protocol.js +1 -0
  166. package/dist-lib/lib/run-log-redact.d.ts +27 -0
  167. package/dist-lib/lib/run-log-redact.js +1107 -0
  168. package/dist-lib/lib/serve.js +307 -11
  169. package/dist-lib/lib/spawn.js +3 -2
  170. package/dist-lib/lib/tool-call-display.d.ts +1 -21
  171. package/dist-lib/lib/tools/exec/adapters/python/wheels.js +2 -1
  172. package/dist-lib/lib/tools/exec.d.ts +2 -1
  173. package/dist-lib/lib/tools/exec.js +57 -2
  174. package/dist-lib/lib/tools/generate-image.js +1 -1
  175. package/dist-lib/lib/tools/generate-music.js +1 -1
  176. package/dist-lib/lib/tools/http-request.d.ts +2 -1
  177. package/dist-lib/lib/tools/http-request.js +2 -1
  178. package/dist-lib/lib/tools/mcp-call.js +8 -1
  179. package/dist-lib/lib/tools/operations/builders.d.ts +1 -0
  180. package/dist-lib/lib/tools/operations/builders.js +1 -0
  181. package/dist-lib/lib/tools/operations/compile.js +11 -1
  182. package/dist-lib/lib/tools/operations/manifest.js +1 -0
  183. package/dist-lib/lib/tools/outbound-guard.d.ts +8 -6
  184. package/dist-lib/lib/tools/outbound-guard.js +25 -8
  185. package/dist-lib/lib/tools/skills/catalog.d.ts +1 -0
  186. package/dist-lib/lib/tools/skills/catalog.js +35 -0
  187. package/dist-lib/lib/tools/skills/cli-schema-types.d.ts +1 -0
  188. package/dist-lib/lib/tools/skills/cli-schema.js +25 -0
  189. package/dist-lib/lib/wire.d.ts +5 -0
  190. package/dist-lib/lib/wire.js +128 -5
  191. package/dist-lib/schema/harness.js +20 -5
  192. package/dist-lib/spec/hrpc/messages.js +95 -15
  193. package/dist-lib/spec/hyperschema/index.js +95 -15
  194. package/dist-lib/spec/hyperschema/schema.json +69 -1
  195. package/dist-lib/spec/hyperschema/types.d.ts +10 -3
  196. package/package.json +20 -2
  197. package/patches/@qvac%2Finference@0.20.0.patch +67 -0
  198. package/patches/bare-stow@0.1.5.patch +68 -0
  199. package/patches/hypercore-blob-server@1.15.0.patch +15 -0
  200. package/patches/localwatch@1.4.3.patch +13 -0
  201. package/patches/pyodide@314.0.2.patch +144 -0
@@ -0,0 +1,23 @@
1
+ import type { TrustedSource } from '../../tools/outbound-guard.ts';
2
+ import type { Message, ToolDefinition } from '../types.ts';
3
+ import { type CompletionHistoryEntry } from '../../machine/index.ts';
4
+ export declare function trustedSourcesFrom(history: readonly Message[]): TrustedSource[];
5
+ export declare function sanitizeTurn(turn: Message): Message;
6
+ export declare function lastUserContent(messages: Message[]): string;
7
+ interface TurnImage {
8
+ path: string;
9
+ }
10
+ export declare function lastUserImages(messages: Message[]): TurnImage[];
11
+ export declare function explicitWebSearchQuery(text: string, tools: ReadonlyArray<ToolDefinition>, { skillPreloaded }: ExplicitWebSearchOptions): string | null;
12
+ interface ExplicitWebSearchOptions {
13
+ skillPreloaded: boolean;
14
+ }
15
+ export declare function directToolHistory(history: CompletionHistoryEntry[], args: Record<string, unknown>, output: unknown, resultRef?: string): CompletionHistoryEntry[];
16
+ interface AssistantToolCall {
17
+ name: string;
18
+ arguments?: Record<string, unknown>;
19
+ }
20
+ export declare function assistantToolTurn(contentText: string, calls: ReadonlyArray<AssistantToolCall>): string;
21
+ export declare function preloadLastUser(messages: readonly CompletionHistoryEntry[], name: string, body: string): CompletionHistoryEntry[];
22
+ export declare function annotateCompletionHistoryLastUserTime(history: readonly CompletionHistoryEntry[], now?: Date): CompletionHistoryEntry[];
23
+ export {};
@@ -0,0 +1,112 @@
1
+ import { stripControlTokens } from '../../helpers/control-tokens.js';
2
+ import { stripToolCallMarkup, toolCallMarkup } from '../../helpers/tool-call-markup.js';
3
+ import { skillPreloadMessage } from '../../tools/skills/mention.js';
4
+ import { SKILL_TOOL_NAME, WEB_SEARCH_TOOL_NAME } from '../tool-names.js';
5
+ import { DIRECT_CALL_ID, toolResultHistoryEntry, toolResultTurn } from '../tool-turns.js';
6
+ import { annotateLastUserTime } from '../../prompt.js';
7
+ import { isFailureResultReference } from '../../machine/index.js';
8
+ export function trustedSourcesFrom(history) {
9
+ const sources = [];
10
+ for (const entry of history) {
11
+ const text = String(entry.content ?? '');
12
+ if (entry.role === 'user') {
13
+ sources.push({ origin: 'user', text });
14
+ }
15
+ else if (entry.role === 'tool') {
16
+ sources.push({ origin: originOfToolTurn(text), text });
17
+ }
18
+ }
19
+ return sources;
20
+ }
21
+ function originOfToolTurn(text) {
22
+ return text.startsWith(`[Tool: ${SKILL_TOOL_NAME} (`) ? 'skill' : 'tool-result';
23
+ }
24
+ export function sanitizeTurn(turn) {
25
+ const stripped = stripControlTokens(turn.content);
26
+ const content = turn.role === 'assistant' ? stripToolCallMarkup(stripped) : stripped;
27
+ return content === turn.content ? turn : { ...turn, content };
28
+ }
29
+ export function lastUserContent(messages) {
30
+ for (let i = messages.length - 1; i >= 0; i--) {
31
+ const turn = messages[i];
32
+ if (turn?.role === 'user') {
33
+ return turn.content;
34
+ }
35
+ }
36
+ return '';
37
+ }
38
+ export function lastUserImages(messages) {
39
+ for (let i = messages.length - 1; i >= 0; i--) {
40
+ const turn = messages[i];
41
+ if (turn?.role === 'user') {
42
+ return turn.attachments ?? [];
43
+ }
44
+ }
45
+ return [];
46
+ }
47
+ export function explicitWebSearchQuery(text, tools, { skillPreloaded }) {
48
+ if (!tools.some((tool) => tool.name === WEB_SEARCH_TOOL_NAME)) {
49
+ return null;
50
+ }
51
+ if (explicitlyDisablesWebSearch(text)) {
52
+ return null;
53
+ }
54
+ const named = /\b(?:search|browse|look\s+up|find)\s+(?:the\s+)?(?:web|internet|online)\b/i.test(text);
55
+ const freshness = /\b(?:latest|current|recent)\s+(?:news|events)\b/i.test(text) ||
56
+ /\bwhat(?:'s|\s+is)\s+(?:the\s+)?(?:latest|current|recent)\b/i.test(text);
57
+ const explicit = named || (freshness && !skillPreloaded);
58
+ return explicit && text.trim() ? text.trim() : null;
59
+ }
60
+ function explicitlyDisablesWebSearch(text) {
61
+ return (/\b(?:do\s+not|don't|dont|never)\s+(?:search|browse|look\s+up|find|use)\s+(?:the\s+)?(?:web|internet|online)\b/i.test(text) ||
62
+ /\b(?:avoid|without)\s+(?:searching|browsing|using)\s+(?:the\s+)?(?:web|internet|online)\b/i.test(text));
63
+ }
64
+ export function directToolHistory(history, args, output, resultRef) {
65
+ return [
66
+ ...history,
67
+ {
68
+ role: 'assistant',
69
+ content: toolCallMarkup([{ name: WEB_SEARCH_TOOL_NAME, arguments: args }])
70
+ },
71
+ toolResultHistoryEntry({
72
+ name: WEB_SEARCH_TOOL_NAME,
73
+ id: DIRECT_CALL_ID,
74
+ output,
75
+ resultRef
76
+ })
77
+ ];
78
+ }
79
+ export function assistantToolTurn(contentText, calls) {
80
+ if (!calls.length || contentText.includes('<function=')) {
81
+ return contentText;
82
+ }
83
+ const markup = toolCallMarkup(calls);
84
+ return contentText ? `${contentText}\n${markup}` : markup;
85
+ }
86
+ export function preloadLastUser(messages, name, body) {
87
+ for (let i = messages.length - 1; i >= 0; i--) {
88
+ const turn = messages[i];
89
+ if (!turn || isFailureResultReference(turn)) {
90
+ continue;
91
+ }
92
+ if (turn?.role !== 'user') {
93
+ continue;
94
+ }
95
+ const next = [...messages];
96
+ next[i] = { ...turn, content: skillPreloadMessage(turn.content, name, body) };
97
+ return next;
98
+ }
99
+ return [...messages];
100
+ }
101
+ export function annotateCompletionHistoryLastUserTime(history, now) {
102
+ const result = [...history];
103
+ for (let index = result.length - 1; index >= 0; index--) {
104
+ const entry = result[index];
105
+ if (!entry || isFailureResultReference(entry) || entry.role !== 'user') {
106
+ continue;
107
+ }
108
+ result[index] = annotateLastUserTime([entry], now)[0] ?? entry;
109
+ break;
110
+ }
111
+ return result;
112
+ }
@@ -0,0 +1,46 @@
1
+ import type { AbortSignal as BareAbortSignal } from 'bare-abort-controller';
2
+ import type { CompletionArgs, CompletionSdk } from './completion.ts';
3
+ import type { CompletionContext } from '../../index.ts';
4
+ import type { CompletionSkillEntry } from './completion-loop/index.ts';
5
+ import { type ModelLoadOptions, type ModelLifecycleSdk, type PoolLike } from '../models/model-pool.ts';
6
+ export type CompletionRuntimeRequest = CompletionContext & {
7
+ ctxSize?: number;
8
+ };
9
+ interface RuntimeLease {
10
+ modelId: string;
11
+ release(): void;
12
+ }
13
+ interface RuntimeAcquireOptions {
14
+ pool: PoolLike;
15
+ key: string;
16
+ open(): ReturnType<PoolLike['open']>;
17
+ signal?: BareAbortSignal;
18
+ prepare: () => Promise<boolean>;
19
+ }
20
+ interface RuntimeHarness {
21
+ _models: PoolLike;
22
+ _sdk: {
23
+ load(): Promise<CompletionSdk>;
24
+ };
25
+ _unloadIdleGenerators(): Promise<void>;
26
+ _completionLoadOptions(model: string, agent?: CompletionContext['agent']): ModelLoadOptions;
27
+ _completionSkills(agentId: string | null, model: string): Promise<CompletionSkillEntry[]>;
28
+ _acquireModelLease(options: RuntimeAcquireOptions): Promise<RuntimeLease | null>;
29
+ _makeRoomForCompletion(model: string, options: {
30
+ ctxSize?: number | undefined;
31
+ signal?: BareAbortSignal;
32
+ }): Promise<boolean>;
33
+ _loadModelLifecycleSdk(): Promise<ModelLifecycleSdk>;
34
+ suspended: boolean;
35
+ _runtimeDraining: boolean;
36
+ _assertNotClosing(): void;
37
+ _tools: CompletionArgs['tools'];
38
+ }
39
+ interface PreparedRuntime {
40
+ key: string;
41
+ args: Partial<CompletionArgs> & {
42
+ acquireRound: NonNullable<CompletionArgs['acquireRound']>;
43
+ };
44
+ }
45
+ export declare function prepareCompletionRuntime(harness: RuntimeHarness, request: CompletionRuntimeRequest): Promise<PreparedRuntime>;
46
+ export {};
@@ -0,0 +1,74 @@
1
+ import { createLocalRoots } from '../tools/fs/local.js';
2
+ import { createRemoteRoots } from '../tools/fs/remote.js';
3
+ import { isResident, modelKey, resolveCtxSize } from '../models/model-pool.js';
4
+ import { hlog } from '../log.js';
5
+ export async function prepareCompletionRuntime(harness, request) {
6
+ const model = request.model ?? '';
7
+ const agent = request.agent;
8
+ const messages = request.messages ?? [];
9
+ const { signal, grantedDirs, grantedDrives, driveOp } = request;
10
+ const loadOpts = harness._completionLoadOptions(model, agent);
11
+ const projectionModel = loadOpts.projectionModel;
12
+ if (request.resume && request.ctxSize) {
13
+ loadOpts.ctxSize = request.ctxSize;
14
+ }
15
+ const key = modelKey(model, loadOpts);
16
+ const resident = isResident(harness._models, key);
17
+ hlog('completion start', { model, resident, projection: Boolean(projectionModel) });
18
+ const acquireRound = async (invocationSignal) => {
19
+ const pool = harness._models;
20
+ const lease = await harness._acquireModelLease({
21
+ pool,
22
+ key,
23
+ open: () => pool.open(key, model, loadOpts),
24
+ signal: invocationSignal,
25
+ prepare: async () => {
26
+ if (!isResident(pool, key)) {
27
+ await harness._makeRoomForCompletion(model, {
28
+ ctxSize: loadOpts.ctxSize,
29
+ signal: invocationSignal ?? signal
30
+ });
31
+ await harness._unloadIdleGenerators();
32
+ }
33
+ return true;
34
+ }
35
+ });
36
+ if (!lease) {
37
+ throw new Error('A completion round requires a model lease');
38
+ }
39
+ try {
40
+ const sdk = await harness._sdk.load();
41
+ if (harness.suspended || harness._runtimeDraining) {
42
+ throw new Error('harness is suspended');
43
+ }
44
+ harness._assertNotClosing();
45
+ return { modelId: lease.modelId, sdk, release: lease.release };
46
+ }
47
+ catch (error) {
48
+ lease.release();
49
+ throw error;
50
+ }
51
+ };
52
+ const remoteRoots = driveOp && grantedDrives?.length ? createRemoteRoots(grantedDrives, driveOp) : [];
53
+ return {
54
+ key,
55
+ args: {
56
+ acquireRound,
57
+ ctxSize: await resolveCtxSize(() => harness._loadModelLifecycleSdk(), model, loadOpts),
58
+ messages: projectionModel ? messages : messages.map(({ attachments, ...turn }) => turn),
59
+ requestMessages: messages,
60
+ tools: harness._tools,
61
+ skillCatalog: await harness._completionSkills(agent?.id ?? null, model),
62
+ fsRoots: [
63
+ ...(grantedDirs?.length ? await createLocalRoots(grantedDirs) : []),
64
+ ...remoteRoots
65
+ ],
66
+ fsWriteRoots: [
67
+ ...(request.grantedWriteDirs?.length
68
+ ? await createLocalRoots(request.grantedWriteDirs)
69
+ : []),
70
+ ...remoteRoots.filter((root) => root.writable)
71
+ ]
72
+ }
73
+ };
74
+ }
@@ -1,335 +1,18 @@
1
- import type { CompletionFinal, Tool as SdkTool, ToolDialect } from '@qvac/sdk';
2
- import type { AbortSignal as BareAbortSignal } from 'bare-abort-controller';
3
- import { type TextEvent } from '../helpers/thinking.ts';
4
- import type { ToolCallDisplay } from '../tool-call-display.ts';
5
- import { type LlmGenerationParams } from '../models/llm-presets.ts';
6
- import type { DriveRoot } from '../tools/fs/scope.ts';
7
- import { type UsageBreakdown } from './usage-breakdown.ts';
8
- import { type TrustedSource } from '../tools/outbound-guard.ts';
9
- import type { SkillCatalogEntry } from '../tools/skills/catalog.ts';
10
- import { type SkillState } from '../tools/skills/skill-state-constants.ts';
11
- import type { CliValidator } from '../tools/skills/cli-schema.ts';
12
- import { type Toolset } from '../tools/toolsets.ts';
13
- export interface MessageAttachment {
14
- path: string;
15
- }
16
- export interface Message {
17
- role: string;
18
- content: string;
19
- attachments?: MessageAttachment[];
20
- [key: string]: unknown;
21
- }
22
- export interface Agent {
23
- id?: string;
24
- name?: string;
25
- bio?: string;
26
- enabledTools?: string[] | null;
27
- enabledSkills?: string[] | null;
28
- toolsScope?: 'all' | 'selected' | null;
29
- skillsScope?: 'all' | 'selected' | null;
30
- reasoning?: 'inherit' | 'enabled' | 'disabled' | null;
31
- ctxSize?: number | null;
32
- kvCacheK?: string | null;
33
- kvCacheV?: string | null;
34
- imageModelChoice?: string | null;
35
- imageSteps?: number | null;
36
- imageWidth?: number | null;
37
- imageHeight?: number | null;
38
- approvalMode?: 'ask' | 'skip-all' | null;
39
- disabledToolsets?: string[] | null;
40
- sandboxMode?: boolean | null;
41
- }
42
- export interface ImageSettings {
43
- modelChoice?: string;
44
- steps?: number;
45
- width?: number;
46
- height?: number;
47
- }
48
- export declare function imageSettingsFrom(agent?: Agent): ImageSettings | undefined;
49
- export interface MusicSettings {
50
- modelChoice?: string;
51
- steps?: number;
52
- duration?: number;
53
- }
54
- export interface ResourceDescriptor {
55
- kind: string;
56
- label: string;
57
- }
58
- export type ToolEffect = 'read' | 'write';
59
- export type ApproveFn = (call: {
60
- name: string;
61
- arguments: Record<string, unknown>;
62
- resource?: ResourceDescriptor | null;
63
- effect?: ToolEffect;
64
- intent?: string | null;
65
- display?: ToolCallDisplay | null;
66
- }) => Promise<boolean>;
67
- export type ToolDecision = 'auto' | 'allowed' | 'denied' | 'skip-all' | 'blocked';
68
- export type ToolActivityFn = (entry: ToolActivityEvent) => void;
69
- export interface AttachInput {
70
- fileName: string;
71
- mimeType: string;
72
- bytes: Uint8Array;
73
- raw?: boolean;
74
- }
75
- export interface AttachmentRecord {
76
- id: string;
77
- fileName: string;
78
- mimeType: string;
79
- byteLength: number;
80
- }
81
- export type AttachFn = (input: AttachInput) => Promise<AttachmentRecord>;
82
- export interface AttachmentMeta {
83
- id: string;
84
- fileName: string;
85
- mimeType: string;
86
- byteLength: number;
87
- }
88
- export interface FetchedAttachment extends AttachmentMeta {
89
- bytes: Uint8Array;
90
- }
91
- export type FetchAttachmentFn = (query: {
92
- attachmentId?: string;
93
- signal?: BareAbortSignal;
94
- meta?: boolean;
95
- }) => Promise<FetchedAttachment | AttachmentMeta | null>;
96
- export type ProgressFn = (update: {
97
- percentage: number;
98
- } | null) => void;
99
- export type RagSearchFn = (input: {
100
- query: string;
101
- limit?: number;
102
- }) => Promise<RagSearchResult>;
103
- export interface RagSearchResult {
104
- enabled: boolean;
105
- indexEmpty: boolean;
106
- hasKnowledge: boolean;
107
- pendingReferenced?: boolean;
108
- errorMessage?: string | null;
109
- hits: ReadonlyArray<{
110
- content: string;
111
- metadataJson?: string | null;
112
- score: number;
113
- }>;
114
- }
115
- export type AskUserFn = (question: {
116
- question: string;
117
- options?: string[];
118
- }) => Promise<string | null>;
119
- export interface RunBudget {
120
- readonly deadlineMs: number;
121
- outputBytesUsed: number;
122
- }
123
- export interface ToolExecuteContext {
124
- agentId?: string | null | undefined;
125
- skills?: readonly string[] | undefined;
126
- ctxSize?: number | undefined;
127
- image?: ImageSettings | undefined;
128
- settings?: Record<string, unknown> | undefined;
129
- turnImages?: {
130
- path: string;
131
- }[] | undefined;
132
- attach?: AttachFn | undefined;
133
- fetchAttachment?: FetchAttachmentFn | undefined;
134
- ragSearch?: RagSearchFn | undefined;
135
- progress?: ProgressFn | undefined;
136
- askUser?: AskUserFn | undefined;
137
- signal?: BareAbortSignal | undefined;
138
- fsRoots?: readonly DriveRoot[] | undefined;
139
- fsWriteRoots?: readonly DriveRoot[] | undefined;
140
- toolOps?: ReadonlyMap<string, readonly CliValidator[]> | undefined;
141
- runBudget?: RunBudget | undefined;
142
- reportBackend?: ((use: RunBackendUse) => void) | undefined;
143
- uncappedResponse?: boolean | undefined;
144
- sandboxMode?: boolean | undefined;
145
- toolNames?: readonly string[] | undefined;
146
- approval?: ToolDecision | undefined;
147
- }
148
- export interface RunBackendUse {
149
- tool: string;
150
- requestedGpu: boolean;
151
- device: 'gpu' | 'cpu' | 'unknown';
152
- backend: string;
153
- }
154
- export type ToolParameterType = 'string' | 'number' | 'boolean' | 'object' | 'integer' | 'array' | 'null';
155
- export interface ToolParameterSchema {
156
- type: ToolParameterType | readonly ToolParameterType[];
157
- description?: string;
158
- enum?: string[];
159
- items?: ToolParameterSchema;
160
- properties?: Record<string, ToolParameterSchema>;
161
- required?: string[];
162
- }
163
- export interface ToolParameters {
164
- type: 'object';
165
- properties: Record<string, ToolParameterSchema>;
166
- required?: string[];
167
- }
168
- export interface ToolPromptDefinition {
169
- description?: string;
170
- parameters?: ToolParameters;
171
- }
172
- export interface ToolDefinition {
173
- name: string;
174
- description: string;
175
- parameters: ToolParameters;
176
- prompt?: ToolPromptDefinition;
177
- autoApprove?: boolean;
178
- skillGated?: boolean;
179
- rootsRequired?: boolean;
180
- writeRootsRequired?: boolean;
181
- externalWrite?: boolean;
182
- owningSkill?: string;
183
- toolset: Toolset | null;
184
- validate?: (args: Record<string, unknown>, ctx?: ToolExecuteContext) => string | null;
185
- effect: (args: Record<string, unknown>, ctx?: ToolExecuteContext) => ToolEffect;
186
- resource?: (args: Record<string, unknown>, ctx?: ToolExecuteContext) => ResourceDescriptor | null;
187
- compiled?: true;
188
- execute: (args: Record<string, unknown>, ctx?: ToolExecuteContext) => Promise<unknown>;
189
- }
190
- export interface ToolCallEvent {
191
- type: 'tool-call';
192
- name: string;
193
- args: Record<string, unknown>;
194
- rawCall?: string | undefined;
195
- effect?: ToolEffect | undefined;
196
- display?: ToolCallDisplay | undefined;
197
- }
198
- export interface ToolResultEvent {
199
- type: 'tool-result';
200
- name: string;
201
- result: unknown;
202
- }
203
- export interface ToolActivityEvent {
204
- type: 'tool-activity';
205
- name: string;
206
- args: Record<string, unknown>;
207
- resource: ResourceDescriptor | null;
208
- effect: ToolEffect;
209
- decision: ToolDecision;
210
- }
211
- export interface ErrorEvent {
212
- type: 'error';
213
- message: string;
214
- }
215
- export interface AbortedEvent {
216
- type: 'aborted';
217
- }
218
- export interface MetricsEvent {
219
- type: 'metrics';
220
- totalTokens: number;
221
- durationMs: number;
222
- timeToFirstToken?: number | undefined;
223
- tokensPerSecond?: number | undefined;
224
- promptTokensPerSecond?: number | undefined;
225
- promptTokens?: number | undefined;
226
- cacheTokens?: number | undefined;
227
- ctxSize?: number | undefined;
228
- backends?: RunBackendUse[] | undefined;
229
- usage?: UsageBreakdown | undefined;
230
- route?: 'direct' | 'enhanced' | undefined;
231
- routeModel?: string | undefined;
232
- stopReason?: string | undefined;
233
- }
234
- export interface InferenceCall {
235
- round: number;
236
- droppedTurns: number;
237
- modelId: string;
238
- messages: Message[];
239
- tools: SdkTool[] | undefined;
240
- generationParams: LlmGenerationParams | undefined;
241
- responseFormat: ResponseFormat | undefined;
242
- toolDialect: ToolDialect | undefined;
243
- cacheKey: string | undefined;
244
- promptPrefix: string;
245
- }
246
- export interface InferenceCallEvent {
247
- type: 'inference-call';
248
- call: InferenceCall;
249
- }
250
- export interface InferenceSentEvent {
251
- type: 'inference-sent';
252
- round: number;
253
- droppedTurns: number;
254
- messages: Message[] | undefined;
255
- tokens: InferenceTokens | undefined;
256
- outcome: 'completed' | 'overflowed' | 'failed';
257
- reason: string | undefined;
258
- }
259
- export interface InferenceTokens {
260
- prompt?: number | undefined;
261
- generated?: number | undefined;
262
- cached?: number | undefined;
263
- timeToFirstToken?: number | undefined;
264
- }
265
- export type CompletionEvent = TextEvent | ToolCallEvent | ToolResultEvent | ToolActivityEvent | ErrorEvent | AbortedEvent | MetricsEvent | InferenceCallEvent | InferenceSentEvent;
266
- export interface CompletionSdkResult {
267
- requestId?: string;
268
- tokenStream: AsyncIterable<string>;
269
- final: Promise<CompletionFinal>;
270
- }
271
- export type ResponseFormat = {
272
- type: 'text';
273
- } | {
274
- type: 'json_object';
275
- } | {
276
- type: 'json_schema';
277
- json_schema: {
278
- name: string;
279
- description?: string;
280
- schema: Record<string, unknown>;
281
- strict?: boolean;
282
- };
283
- };
284
- export interface CompletionSdk {
285
- completion(params: {
286
- modelId: string;
287
- history: Message[];
288
- stream: true;
289
- tools?: SdkTool[] | undefined;
290
- generationParams?: LlmGenerationParams | undefined;
291
- responseFormat?: ResponseFormat | undefined;
292
- toolDialect?: ToolDialect | undefined;
293
- kvCache?: string | boolean;
294
- }): CompletionSdkResult;
295
- cancel?(params: {
296
- requestId: string;
297
- }): Promise<void>;
298
- }
299
- export interface RoundModelLease {
300
- modelId: string;
301
- release: () => void;
302
- }
303
- interface CompletionArgs {
304
- modelId: string;
305
- acquireRound?: () => Promise<RoundModelLease>;
306
- model?: string;
307
- agent?: Agent;
308
- ctxSize?: number;
309
- cacheKey?: string;
310
- thinkingDefault?: string;
311
- generationParams?: LlmGenerationParams;
312
- responseFormat?: ResponseFormat;
313
- messages: Message[];
314
- tools?: ToolDefinition[];
315
- skillCatalog?: readonly CompletionSkillEntry[];
316
- approve?: ApproveFn;
317
- attach?: AttachFn;
318
- fetchAttachment?: FetchAttachmentFn;
319
- ragSearch?: RagSearchFn;
320
- progress?: ProgressFn;
321
- askUser?: AskUserFn;
322
- signal?: BareAbortSignal;
323
- toolConfigs?: Record<string, Record<string, unknown>>;
324
- fsRoots?: readonly DriveRoot[];
325
- fsWriteRoots?: readonly DriveRoot[];
326
- captureInferenceCalls?: boolean;
327
- }
328
- export declare const CONTEXT_OVERFLOW_MESSAGE = "This message is too large for the model context window.";
329
- export declare function trustedSourcesFrom(history: readonly Message[]): TrustedSource[];
330
- export declare function toSdkTool(tool: ToolDefinition): SdkTool;
331
- export type CompletionSkillEntry = SkillCatalogEntry & {
332
- readonly status?: SkillState;
333
- };
334
- export declare function completion(sdk: CompletionSdk, { modelId, acquireRound, model, agent, ctxSize, cacheKey, thinkingDefault, generationParams: generationParamsOverride, responseFormat, messages: storedMessages, tools: allTools, skillCatalog, approve, attach, fetchAttachment, ragSearch, progress, askUser, signal, toolConfigs, fsRoots, fsWriteRoots, captureInferenceCalls }: CompletionArgs): AsyncGenerator<CompletionEvent>;
335
- export {};
1
+ import { type CompletionRun, type CompletionRuntime } from './completion-engine/driver.ts';
2
+ import type { EngineBoot, EngineStep } from '../machine/index.ts';
3
+ import type { CompletionArgs } from './completion-loop/index.ts';
4
+ import type { CompletionSdk } from './types.ts';
5
+ export * from './types.ts';
6
+ export type { CompletionRun };
7
+ export type { CompletionArgs } from './completion-loop/index.ts';
8
+ interface CompletionRunOptions {
9
+ boot?: EngineBoot;
10
+ onStep?: (step: EngineStep) => Promise<void>;
11
+ hostAppendTimeoutMs?: number;
12
+ onDetached?: (pending: Promise<void>, runId?: string) => void;
13
+ requestDigestKey?: Uint8Array;
14
+ resumeRequest?: Partial<CompletionArgs>;
15
+ bootRequest?: unknown;
16
+ }
17
+ export { CONTEXT_OVERFLOW_MESSAGE, imageSettingsFrom, loadedSkillsFrom, skillInContext, skillLoadedTurn, toSdkTool, trustedSourcesFrom, unreadySkillState, type CompletionSkillEntry } from './completion-loop/index.ts';
18
+ export declare function completion(sdk: CompletionSdk | CompletionRuntime, args: CompletionArgs, options?: CompletionRunOptions): CompletionRun;