@qvac/harness 1.0.0-rc.10 → 1.0.0-rc.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +78 -0
- package/dist-lib/index.d.ts +2 -12
- package/dist-lib/index.js +8 -2
- package/dist-lib/lib/agent/resolve.js +9 -2
- package/dist-lib/lib/agent/tools.d.ts +1 -0
- package/dist-lib/lib/agent/tools.js +1 -1
- package/dist-lib/lib/capabilities/agent-ports.d.ts +1 -1
- package/dist-lib/lib/capabilities/agent-ports.js +7 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/executing.js +4 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +3 -1
- package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +12 -5
- package/dist-lib/lib/capabilities/completion-engine/actors/run-prepare.js +36 -22
- package/dist-lib/lib/capabilities/completion-engine/actors/skill-step.js +24 -5
- package/dist-lib/lib/capabilities/completion-engine/actors/stream.d.ts +3 -2
- package/dist-lib/lib/capabilities/completion-engine/actors/stream.js +18 -3
- package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +3 -0
- package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +1 -0
- package/dist-lib/lib/capabilities/completion-engine/driver.js +3 -2
- package/dist-lib/lib/capabilities/completion-engine/loop.d.ts +2 -0
- package/dist-lib/lib/capabilities/completion-engine/model-round.d.ts +2 -1
- package/dist-lib/lib/capabilities/completion-engine/prepare-step.d.ts +2 -1
- package/dist-lib/lib/capabilities/completion-engine/round.d.ts +9 -0
- package/dist-lib/lib/capabilities/completion-engine/round.js +12 -8
- package/dist-lib/lib/capabilities/completion-engine/tool-execution.js +15 -1
- package/dist-lib/lib/capabilities/completion-engine/turns.js +5 -2
- package/dist-lib/lib/capabilities/skill-entry.d.ts +4 -0
- package/dist-lib/lib/capabilities/skill-entry.js +28 -1
- package/dist-lib/lib/capabilities/tool-failed-note.d.ts +1 -0
- package/dist-lib/lib/capabilities/tool-failed-note.js +1 -0
- package/dist-lib/lib/capabilities/tool-turns.d.ts +5 -1
- package/dist-lib/lib/capabilities/tool-turns.js +21 -2
- package/dist-lib/lib/context-window.d.ts +1 -0
- package/dist-lib/lib/context-window.js +6 -0
- package/dist-lib/lib/contract/agent.d.ts +4 -0
- package/dist-lib/lib/contract/completion.d.ts +2 -0
- package/dist-lib/lib/contract/models.d.ts +3 -1
- package/dist-lib/lib/contract/run.d.ts +1 -0
- package/dist-lib/lib/machine/completion/actions/completion.d.ts +3 -1
- package/dist-lib/lib/machine/completion/actions/completion.js +43 -7
- package/dist-lib/lib/machine/completion/actions/tools.js +4 -5
- package/dist-lib/lib/machine/completion/context.js +1 -0
- package/dist-lib/lib/machine/completion/guards/retry.d.ts +2 -1
- package/dist-lib/lib/machine/completion/guards/retry.js +4 -3
- package/dist-lib/lib/machine/completion/implementations.d.ts +2 -0
- package/dist-lib/lib/machine/completion/implementations.js +7 -3
- package/dist-lib/lib/machine/completion/machine.d.ts +6 -0
- package/dist-lib/lib/machine/completion/machine.js +12 -1
- package/dist-lib/lib/machine/completion/rules.d.ts +1 -0
- package/dist-lib/lib/machine/completion/rules.js +3 -0
- package/dist-lib/lib/machine/completion/types.d.ts +5 -0
- package/dist-lib/lib/machine/index.d.ts +6 -0
- package/dist-lib/lib/models/custom-model-cache.d.ts +1 -0
- package/dist-lib/lib/models/custom-model-cache.js +3 -0
- package/dist-lib/lib/models/gguf-header.d.ts +6 -0
- package/dist-lib/lib/models/gguf-header.js +38 -1
- package/dist-lib/lib/models/kv-cache.d.ts +2 -0
- package/dist-lib/lib/models/kv-cache.js +8 -0
- package/dist-lib/lib/models/load-requests.d.ts +9 -0
- package/dist-lib/lib/models/load-requests.js +42 -0
- package/dist-lib/lib/models/model-handle.d.ts +1 -0
- package/dist-lib/lib/models/model-pool.d.ts +2 -1
- package/dist-lib/lib/models/model-pool.js +17 -7
- package/dist-lib/lib/no-redirect-fetch-impl.d.ts +1 -0
- package/dist-lib/lib/no-redirect-fetch-impl.js +32 -1
- package/dist-lib/lib/prompt.d.ts +4 -2
- package/dist-lib/lib/prompt.js +16 -5
- package/dist-lib/lib/residency/completion-runtime.d.ts +6 -5
- package/dist-lib/lib/residency/completion-runtime.js +3 -7
- package/dist-lib/lib/residency/index.d.ts +9 -1
- package/dist-lib/lib/residency/index.js +38 -8
- package/dist-lib/lib/skills-host/index.d.ts +1 -0
- package/dist-lib/lib/skills-host/index.js +3 -0
- package/dist-lib/lib/tools/attachments/staging.d.ts +1 -0
- package/dist-lib/lib/tools/attachments/staging.js +11 -0
- package/dist-lib/lib/tools/http-request/request.d.ts +3 -1
- package/dist-lib/lib/tools/http-request/request.js +2 -1
- package/dist-lib/lib/tools/http-request/truncate-response.d.ts +16 -1
- package/dist-lib/lib/tools/http-request/truncate-response.js +45 -9
- package/dist-lib/lib/tools/http-request.js +12 -6
- package/dist-lib/lib/tools/office/ops.d.ts +1 -0
- package/dist-lib/lib/tools/office/ops.js +5 -1
- package/dist-lib/lib/tools/office.d.ts +1 -0
- package/dist-lib/lib/tools/office.js +151 -21
- package/dist-lib/lib/tools/operations/compile.js +4 -4
- package/dist-lib/lib/tools/skills/credential-check.d.ts +1 -0
- package/dist-lib/lib/tools/skills/credential-check.js +3 -0
- package/dist-lib/lib/tools/skills/manifest-parse.js +27 -3
- package/dist-lib/lib/tools/skills/manifest.d.ts +1 -0
- package/dist-lib/plugins/image/edit-image.js +4 -3
- package/dist-lib/plugins/image/generate-image.d.ts +3 -2
- package/dist-lib/plugins/image/generate-image.js +87 -64
- package/dist-lib/plugins/image/index.js +1 -0
- package/dist-lib/plugins/music/generate-music.d.ts +3 -2
- package/dist-lib/plugins/music/generate-music.js +68 -47
- package/dist-lib/plugins/music/index.js +1 -0
- package/dist-lib/plugins/run-report/story.js +4 -1
- package/dist-lib/plugins/sandbox-windows/prebuilds/win32-arm64/qvac-sandbox.exe +0 -0
- package/dist-lib/plugins/sandbox-windows/prebuilds/win32-x64/qvac-sandbox.exe +0 -0
- package/package.json +5 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { assign, enqueueActions } from 'xstate';
|
|
2
|
+
import { isFailureResultReference } from '../../helpers/failure-result.js';
|
|
2
3
|
import { approveCompletion, requestApproval } from './actions/approval.js';
|
|
3
|
-
import { addKnowledge, answerCompletion, continueGoal, continueStop, continueWithInput, failCompletion, finishWrapRound, finishRound,
|
|
4
|
+
import { addKnowledge, answerCompletion, continueGoal, continueStalledGoal, continueStop, continueWithInput, failCompletion, finishWrapRound, finishRound, markGoalStalled, retryCappedSearch, retryCutRound, retryMalformedRound, retryMuteRound, retryNarratedRound, settleGoal, startInitialRound, startNextRound, wrapCompletion, wrapCutCompletion, wrapDeliveredCompletion, wrapExhaustedCompletion, wrapMuteCompletion, wrapSearchCappedCompletion } from './actions/completion.js';
|
|
4
5
|
import { authorizePrepare, requestPrepare, settlePrepare, settlePrepareFailure } from './actions/prepare.js';
|
|
5
6
|
import { finishBatch, finishDirectTool, finishTool, endRunOnTool, queueDirectSearch, rejectCutOffTool, rejectDeniedApproval, rejectPlanModeWrite, rejectRepeatedTool, rejectTool, resolveTools, selectNextTool, startDirectTool, startTool } from './actions/tools.js';
|
|
6
7
|
import { allResolvedToolsCapped, canMuteRetry, canRetryCut, canRetryMalformed, canRetryNarrated, canRetrySearchCap, hasQueuedTool, hasRoundsLeft, repeatsBlocked, searchPending } from './guards/retry.js';
|
|
@@ -104,6 +105,8 @@ export const completionActions = {
|
|
|
104
105
|
addKnowledge: assignAction(addKnowledge),
|
|
105
106
|
continueStop: assignAction(continueStop),
|
|
106
107
|
continueGoal: goalAction(continueGoal),
|
|
108
|
+
continueStalledGoal: goalAction(continueStalledGoal),
|
|
109
|
+
markGoalStalled: assignAction(markGoalStalled),
|
|
107
110
|
settleGoal: goalAction(settleGoal),
|
|
108
111
|
retryMalformedRound: assignAction(retryMalformedRound),
|
|
109
112
|
retryMuteRound: assignAction(retryMuteRound),
|
|
@@ -212,8 +215,9 @@ function goalAction(action) {
|
|
|
212
215
|
turn: goal.turns,
|
|
213
216
|
round: update.round ?? context.round
|
|
214
217
|
};
|
|
215
|
-
|
|
216
|
-
|
|
218
|
+
const turn = update.history?.at(-1);
|
|
219
|
+
if (goal.status === 'open' && turn && !isFailureResultReference(turn)) {
|
|
220
|
+
event.text = turn.content;
|
|
217
221
|
}
|
|
218
222
|
if (goal.checks.length) {
|
|
219
223
|
event.checks = goal.checks.map(({ name }) => name);
|
|
@@ -135,6 +135,9 @@ declare const createCompletionMachineImplementation: (context?: CompletionContex
|
|
|
135
135
|
} | {
|
|
136
136
|
type: "continueGoal";
|
|
137
137
|
params: undefined;
|
|
138
|
+
} | {
|
|
139
|
+
type: "continueStalledGoal";
|
|
140
|
+
params: undefined;
|
|
138
141
|
} | {
|
|
139
142
|
type: "continueStop";
|
|
140
143
|
params: import("./types.ts").AnsweredPayload;
|
|
@@ -169,6 +172,9 @@ declare const createCompletionMachineImplementation: (context?: CompletionContex
|
|
|
169
172
|
} | {
|
|
170
173
|
type: "finishWrapRound";
|
|
171
174
|
params: import("./types.ts").RoundDonePayload;
|
|
175
|
+
} | {
|
|
176
|
+
type: "markGoalStalled";
|
|
177
|
+
params: undefined;
|
|
172
178
|
} | {
|
|
173
179
|
type: "markTimeUp";
|
|
174
180
|
params: undefined;
|
|
@@ -437,10 +437,21 @@ const createCompletionMachineImplementation = (context, now = noClock) => setup(
|
|
|
437
437
|
target: '#completion.active.wrapping',
|
|
438
438
|
actions: [sameRoundAck, 'wrapDeliveredCompletion']
|
|
439
439
|
},
|
|
440
|
+
{
|
|
441
|
+
guard: and([
|
|
442
|
+
'repeatsBlocked',
|
|
443
|
+
'goalUnmet',
|
|
444
|
+
'hasRoundsLeft',
|
|
445
|
+
not('timeUp'),
|
|
446
|
+
'goalTokensLeft'
|
|
447
|
+
]),
|
|
448
|
+
target: '#completion.active.round',
|
|
449
|
+
actions: [nextRoundAck, 'continueStalledGoal']
|
|
450
|
+
},
|
|
440
451
|
{
|
|
441
452
|
guard: 'repeatsBlocked',
|
|
442
453
|
target: '#completion.active.wrapping',
|
|
443
|
-
actions: [sameRoundAck, 'wrapExhaustedCompletion']
|
|
454
|
+
actions: [sameRoundAck, 'wrapExhaustedCompletion', 'markGoalStalled']
|
|
444
455
|
},
|
|
445
456
|
{
|
|
446
457
|
guard: and(['allResolvedToolsCapped', 'canRetrySearchCap', 'hasRoundsLeft']),
|
|
@@ -14,6 +14,7 @@ export declare function goalMet({ plan, goal }: Readonly<CompletionContext>): bo
|
|
|
14
14
|
export declare function nudgeWithChecks(nudge: string, checks: readonly GoalCheck[], unnamed?: boolean): string;
|
|
15
15
|
export declare function goalTokensLeft({ goal, metrics }: Readonly<CompletionContext>): boolean;
|
|
16
16
|
export declare function goalStalled({ outcome }: Readonly<CompletionContext>): boolean;
|
|
17
|
+
export declare function settledSince(settled: CompletionContext['settledResults']): CompletionContext['settledResults'];
|
|
17
18
|
export declare function retainFailureResults(history: readonly CompletionHistoryEntry[], values: Readonly<Record<string, FailureResultValue>>, failedCalls: Readonly<Record<string, SettledCall>>): Record<string, FailureResultValue>;
|
|
18
19
|
export declare function activate(activated: string[], names: readonly string[]): string[];
|
|
19
20
|
export interface ApplyHistorySpliceOptions {
|
|
@@ -56,6 +56,9 @@ export function goalTokensLeft({ goal, metrics }) {
|
|
|
56
56
|
export function goalStalled({ outcome }) {
|
|
57
57
|
return outcome === 'exhausted';
|
|
58
58
|
}
|
|
59
|
+
export function settledSince(settled) {
|
|
60
|
+
return Object.fromEntries(Object.entries(settled).map(([key, result]) => [key, { ...result, repeats: 0 }]));
|
|
61
|
+
}
|
|
59
62
|
export function retainFailureResults(history, values, failedCalls) {
|
|
60
63
|
const retained = new Set();
|
|
61
64
|
for (const entry of history) {
|
|
@@ -150,6 +150,7 @@ export interface ToolDonePayload {
|
|
|
150
150
|
endedRun?: boolean;
|
|
151
151
|
answer?: string;
|
|
152
152
|
delivered?: boolean;
|
|
153
|
+
deliveryEndsRun?: boolean;
|
|
153
154
|
durationMs?: number;
|
|
154
155
|
activate?: string;
|
|
155
156
|
plan?: PlanStep[];
|
|
@@ -256,8 +257,10 @@ export interface RunPolicy {
|
|
|
256
257
|
unchangedMessage: string;
|
|
257
258
|
failingMessage?: string;
|
|
258
259
|
goalNudge: string;
|
|
260
|
+
goalStalledNudge: string;
|
|
259
261
|
}
|
|
260
262
|
export interface SettledResult {
|
|
263
|
+
name?: string;
|
|
261
264
|
digest: string;
|
|
262
265
|
repeats: number;
|
|
263
266
|
round: number;
|
|
@@ -276,6 +279,7 @@ export interface GoalState {
|
|
|
276
279
|
checks: GoalCheck[];
|
|
277
280
|
checkable?: boolean;
|
|
278
281
|
named?: boolean;
|
|
282
|
+
stalled?: true;
|
|
279
283
|
}
|
|
280
284
|
export interface QueuedInput {
|
|
281
285
|
id?: string;
|
|
@@ -316,6 +320,7 @@ export interface CompletionContext {
|
|
|
316
320
|
answer: string | null;
|
|
317
321
|
delivered: boolean;
|
|
318
322
|
deliveredThisRound: boolean;
|
|
323
|
+
deliveryEndsRun: boolean;
|
|
319
324
|
activated: string[];
|
|
320
325
|
outcome: RunOutcome | 'failed' | null;
|
|
321
326
|
metrics: CompletionMetrics;
|
|
@@ -139,6 +139,9 @@ export declare class CompletionMachine {
|
|
|
139
139
|
} | {
|
|
140
140
|
type: "continueGoal";
|
|
141
141
|
params: undefined;
|
|
142
|
+
} | {
|
|
143
|
+
type: "continueStalledGoal";
|
|
144
|
+
params: undefined;
|
|
142
145
|
} | {
|
|
143
146
|
type: "continueStop";
|
|
144
147
|
params: import("./completion/types.ts").AnsweredPayload;
|
|
@@ -173,6 +176,9 @@ export declare class CompletionMachine {
|
|
|
173
176
|
} | {
|
|
174
177
|
type: "finishWrapRound";
|
|
175
178
|
params: import("./completion/types.ts").RoundDonePayload;
|
|
179
|
+
} | {
|
|
180
|
+
type: "markGoalStalled";
|
|
181
|
+
params: undefined;
|
|
176
182
|
} | {
|
|
177
183
|
type: "markTimeUp";
|
|
178
184
|
params: undefined;
|
|
@@ -9,5 +9,6 @@ export interface CustomShardCache {
|
|
|
9
9
|
readonly files: ReadonlyArray<string>;
|
|
10
10
|
}
|
|
11
11
|
export declare function customShardCache(src: string, modelsDir: string | undefined): CustomShardCache | null;
|
|
12
|
+
export declare function localWeightFile(src: string, modelsDir: string | undefined): string | null;
|
|
12
13
|
export declare function customDownloads(entry: CustomModelEntry, modelsDir: string | undefined): Promise<CustomDownloads>;
|
|
13
14
|
export declare function isCustomModelCached(entry: CustomModelEntry, modelsDir: string | undefined): Promise<boolean>;
|
|
@@ -22,6 +22,9 @@ export function customShardCache(src, modelsDir) {
|
|
|
22
22
|
const dir = path.join(modelsDir, 'sharded', shortHash(`${set.baseUrl}/${set.base}`));
|
|
23
23
|
return { dir, files: set.shards.map((shard) => path.join(dir, shard.filename)) };
|
|
24
24
|
}
|
|
25
|
+
export function localWeightFile(src, modelsDir) {
|
|
26
|
+
return customShardCache(src, modelsDir)?.files[0] ?? customFileCachePath(src, modelsDir);
|
|
27
|
+
}
|
|
25
28
|
export async function customDownloads(entry, modelsDir) {
|
|
26
29
|
const remote = sourcesOf(entry).filter(isRemoteSource);
|
|
27
30
|
const shardDirs = remote.flatMap((src) => customShardCache(src, modelsDir)?.dir ?? []);
|
|
@@ -7,6 +7,12 @@ export interface GgufHeader {
|
|
|
7
7
|
readonly contextLength?: number;
|
|
8
8
|
readonly sizeLabel?: string;
|
|
9
9
|
readonly tags?: ReadonlyArray<string>;
|
|
10
|
+
readonly headDims?: HeadDims;
|
|
11
|
+
readonly chatTemplate?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface HeadDims {
|
|
14
|
+
readonly k?: number;
|
|
15
|
+
readonly v?: number;
|
|
10
16
|
}
|
|
11
17
|
export type GgufRangeFetch = (url: string, init: {
|
|
12
18
|
headers: Record<string, string>;
|
|
@@ -262,6 +262,7 @@ export function parseGgufHeader(bytes) {
|
|
|
262
262
|
const count = readU64(c);
|
|
263
263
|
const header = {};
|
|
264
264
|
const contextLengths = new Map();
|
|
265
|
+
const dims = new Map();
|
|
265
266
|
for (let i = 0; i < count; i += 1) {
|
|
266
267
|
const key = readString(c);
|
|
267
268
|
if (key === null || !has(c, 4)) {
|
|
@@ -289,15 +290,40 @@ export function parseGgufHeader(bytes) {
|
|
|
289
290
|
else if (key === 'general.tags') {
|
|
290
291
|
header.tags = value.strs;
|
|
291
292
|
}
|
|
293
|
+
else if (key === 'tokenizer.chat_template') {
|
|
294
|
+
header.chatTemplate = value.str;
|
|
295
|
+
}
|
|
292
296
|
else if (key.endsWith('.context_length')) {
|
|
293
297
|
contextLengths.set(key, value.num);
|
|
294
298
|
}
|
|
299
|
+
else if (HEAD_KEYS.some((suffix) => key.endsWith(suffix))) {
|
|
300
|
+
dims.set(key, value.num);
|
|
301
|
+
}
|
|
295
302
|
}
|
|
296
303
|
header.contextLength =
|
|
297
304
|
contextLengths.get(`${header.architecture}.context_length`) ??
|
|
298
305
|
contextLengths.values().next().value;
|
|
306
|
+
const headDims = headDimsOf(dims, header.architecture);
|
|
307
|
+
if (headDims) {
|
|
308
|
+
header.headDims = headDims;
|
|
309
|
+
}
|
|
299
310
|
return header;
|
|
300
311
|
}
|
|
312
|
+
const HEAD_KEYS = [
|
|
313
|
+
'.attention.key_length',
|
|
314
|
+
'.attention.value_length',
|
|
315
|
+
'.attention.head_count',
|
|
316
|
+
'.embedding_length'
|
|
317
|
+
];
|
|
318
|
+
function headDimsOf(dims, architecture) {
|
|
319
|
+
const of = (suffix) => dims.get(`${architecture}${suffix}`);
|
|
320
|
+
const embedding = of('.embedding_length');
|
|
321
|
+
const heads = of('.attention.head_count');
|
|
322
|
+
const split = embedding && heads ? embedding / heads : undefined;
|
|
323
|
+
const k = of('.attention.key_length') ?? split;
|
|
324
|
+
const v = of('.attention.value_length') ?? split;
|
|
325
|
+
return k || v ? { k, v } : undefined;
|
|
326
|
+
}
|
|
301
327
|
function readValue(c, type, collect = false) {
|
|
302
328
|
switch (type) {
|
|
303
329
|
case UINT8:
|
|
@@ -332,7 +358,7 @@ function readValue(c, type, collect = false) {
|
|
|
332
358
|
const length = readU64(c);
|
|
333
359
|
const strs = new Array();
|
|
334
360
|
for (let i = 0; i < length; i += 1) {
|
|
335
|
-
const element = readValue(c, elementType);
|
|
361
|
+
const element = elementType === STRING && !collect ? skipString(c) : readValue(c, elementType);
|
|
336
362
|
if (!element.ok) {
|
|
337
363
|
return { ok: false };
|
|
338
364
|
}
|
|
@@ -379,3 +405,14 @@ function readString(c) {
|
|
|
379
405
|
c.offset += length;
|
|
380
406
|
return str;
|
|
381
407
|
}
|
|
408
|
+
function skipString(c) {
|
|
409
|
+
if (!has(c, 8)) {
|
|
410
|
+
return { ok: false };
|
|
411
|
+
}
|
|
412
|
+
const length = readU64(c);
|
|
413
|
+
if (!has(c, length)) {
|
|
414
|
+
return { ok: false };
|
|
415
|
+
}
|
|
416
|
+
c.offset += length;
|
|
417
|
+
return { ok: true };
|
|
418
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type { HeadDims } from './gguf-header.ts';
|
|
1
2
|
export interface KvCacheTypes {
|
|
2
3
|
k: string | undefined;
|
|
3
4
|
v: string | undefined;
|
|
4
5
|
}
|
|
6
|
+
export declare function headSafeKvCache({ k, v }: KvCacheTypes, heads: HeadDims | undefined): KvCacheTypes;
|
|
5
7
|
export declare function adrenoSafeKvCache(device: 'cpu' | 'gpu' | undefined, { k, v }: KvCacheTypes, hostPlatform?: string): KvCacheTypes;
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import { platform } from 'os';
|
|
2
|
+
const QUANT_BLOCK = 32;
|
|
3
|
+
const UNBLOCKED = new Set(['f32', 'f16', 'bf16']);
|
|
4
|
+
export function headSafeKvCache({ k, v }, heads) {
|
|
5
|
+
return { k: blockFits(k, heads?.k) ? k : 'f16', v: blockFits(v, heads?.v) ? v : 'f16' };
|
|
6
|
+
}
|
|
7
|
+
function blockFits(type, width) {
|
|
8
|
+
return !type || !width || UNBLOCKED.has(type) || width % QUANT_BLOCK === 0;
|
|
9
|
+
}
|
|
2
10
|
export function adrenoSafeKvCache(device, { k, v }, hostPlatform = platform()) {
|
|
3
11
|
if (device === 'cpu' || hostPlatform !== 'android') {
|
|
4
12
|
return { k, v };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { NotifyFn } from './model-pool.ts';
|
|
2
|
+
export interface LoadRequests {
|
|
3
|
+
models(): string[];
|
|
4
|
+
around<T>(model: string, run: () => Promise<T>): Promise<T>;
|
|
5
|
+
start(model: string): void;
|
|
6
|
+
ready(model: string): void;
|
|
7
|
+
failed(model: string, err: unknown): void;
|
|
8
|
+
}
|
|
9
|
+
export declare function createLoadRequests(notify: NotifyFn): LoadRequests;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { errorMessage } from '../guards.js';
|
|
2
|
+
export function createLoadRequests(notify) {
|
|
3
|
+
const counts = new Map();
|
|
4
|
+
const start = (model) => {
|
|
5
|
+
counts.set(model, (counts.get(model) ?? 0) + 1);
|
|
6
|
+
notify({ status: 'loading', model, catalogChanged: true });
|
|
7
|
+
};
|
|
8
|
+
const end = (model) => {
|
|
9
|
+
const left = (counts.get(model) ?? 0) - 1;
|
|
10
|
+
if (left > 0) {
|
|
11
|
+
counts.set(model, left);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
counts.delete(model);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const failed = (model, err) => {
|
|
18
|
+
end(model);
|
|
19
|
+
notify({ status: 'error', model, message: errorMessage(err), catalogChanged: true });
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
models: () => [...counts.keys()],
|
|
23
|
+
around: async (model, run) => {
|
|
24
|
+
start(model);
|
|
25
|
+
try {
|
|
26
|
+
const handedOff = await run();
|
|
27
|
+
end(model);
|
|
28
|
+
return handedOff;
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
failed(model, err);
|
|
32
|
+
throw err;
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
start,
|
|
36
|
+
ready: (model) => {
|
|
37
|
+
end(model);
|
|
38
|
+
notify({ status: 'ready', model, catalogChanged: true });
|
|
39
|
+
},
|
|
40
|
+
failed
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -194,8 +194,9 @@ export interface ModelPoolOptions {
|
|
|
194
194
|
beforeLoad?: BeforeLoad;
|
|
195
195
|
chooseContext?: ChooseContext;
|
|
196
196
|
hold?: HoldFn;
|
|
197
|
+
modelsDir?: string | undefined;
|
|
197
198
|
}
|
|
198
|
-
export declare function createModelPool(loadSdk: LoadSdk, notify: NotifyFn, { deviceConfig, loads, onUnloadFailure, beforeLoad, chooseContext, hold }?: ModelPoolOptions): ModelPuddle;
|
|
199
|
+
export declare function createModelPool(loadSdk: LoadSdk, notify: NotifyFn, { deviceConfig, loads, onUnloadFailure, beforeLoad, chooseContext, hold, modelsDir }?: ModelPoolOptions): ModelPuddle;
|
|
199
200
|
export interface EmbedPoolOptions {
|
|
200
201
|
faults: GpuFaultState;
|
|
201
202
|
batchSize?: number;
|
|
@@ -5,7 +5,9 @@ import ResourcePuddle from 'resource-puddle';
|
|
|
5
5
|
import { ModelHandle } from './model-handle.js';
|
|
6
6
|
import { defaultContextSize } from './context.js';
|
|
7
7
|
import { customContextLength } from './model-catalog.js';
|
|
8
|
-
import {
|
|
8
|
+
import { localWeightFile } from './custom-model-cache.js';
|
|
9
|
+
import { GGUF_STUB_MAX_BYTES, parseGgufHeader, readGgufHeaderBytes } from './gguf-header.js';
|
|
10
|
+
import { adrenoSafeKvCache, headSafeKvCache } from './kv-cache.js';
|
|
9
11
|
import { DEFAULT_EMBED_BATCH_SIZE } from './embed-config.js';
|
|
10
12
|
import { errorMessage } from '../guards.js';
|
|
11
13
|
import { HarnessLog } from '../log.js';
|
|
@@ -404,22 +406,20 @@ export async function resolveCtxSize(loadSdk, model, load) {
|
|
|
404
406
|
const sdk = await loadSdk();
|
|
405
407
|
return contextSize(model, await resolveModelSrc(sdk, model), load);
|
|
406
408
|
}
|
|
407
|
-
export function createModelPool(loadSdk, notify, { deviceConfig = {}, loads = createLoadTracker(), onUnloadFailure, beforeLoad, chooseContext, hold } = {}) {
|
|
409
|
+
export function createModelPool(loadSdk, notify, { deviceConfig = {}, loads = createLoadTracker(), onUnloadFailure, beforeLoad, chooseContext, hold, modelsDir } = {}) {
|
|
408
410
|
const load = async (key, model, opts) => {
|
|
409
411
|
const sdk = await loadSdk();
|
|
410
412
|
const modelSrc = await resolveModelSrc(sdk, model);
|
|
411
413
|
const resolved = resolveLoadOptions(opts, deviceConfig);
|
|
412
414
|
const slots = slotCount(model, { opts: resolved, deviceConfig });
|
|
415
|
+
const header = await weightHeader(modelSrc, modelsDir);
|
|
413
416
|
const projectionModelSrc = resolved.projectionModel
|
|
414
417
|
? await resolveProjectionSrc(sdk, resolved.projectionModel)
|
|
415
418
|
: undefined;
|
|
416
419
|
const mainGpu = deviceConfig.device !== 'cpu' && deviceConfig.mainGpu
|
|
417
420
|
? await deviceConfig.mainGpu()
|
|
418
421
|
: undefined;
|
|
419
|
-
const kvCache = adrenoSafeKvCache(deviceConfig.device, {
|
|
420
|
-
k: resolved.kvCacheK,
|
|
421
|
-
v: resolved.kvCacheV
|
|
422
|
-
});
|
|
422
|
+
const kvCache = headSafeKvCache(adrenoSafeKvCache(deviceConfig.device, { k: resolved.kvCacheK, v: resolved.kvCacheV }), header?.headDims);
|
|
423
423
|
const openclCache = deviceConfig.device !== 'cpu' && deviceConfig.openclCacheDir
|
|
424
424
|
? deviceConfig.openclCacheDir()
|
|
425
425
|
: undefined;
|
|
@@ -466,7 +466,8 @@ export function createModelPool(loadSdk, notify, { deviceConfig = {}, loads = cr
|
|
|
466
466
|
ctxSize,
|
|
467
467
|
slots,
|
|
468
468
|
bytes: admission?.bytes,
|
|
469
|
-
fit: await loadFit(sdk, model, modelId)
|
|
469
|
+
fit: await loadFit(sdk, model, modelId),
|
|
470
|
+
canThink: canThink(header)
|
|
470
471
|
};
|
|
471
472
|
};
|
|
472
473
|
return new ModelPuddle({
|
|
@@ -475,6 +476,15 @@ export function createModelPool(loadSdk, notify, { deviceConfig = {}, loads = cr
|
|
|
475
476
|
...NO_IDLE_GC
|
|
476
477
|
});
|
|
477
478
|
}
|
|
479
|
+
async function weightHeader(modelSrc, modelsDir) {
|
|
480
|
+
const file = typeof modelSrc === 'string' ? localWeightFile(modelSrc, modelsDir) : null;
|
|
481
|
+
const bytes = file ? await readGgufHeaderBytes(file, { maxBytes: GGUF_STUB_MAX_BYTES }) : null;
|
|
482
|
+
return bytes && parseGgufHeader(bytes);
|
|
483
|
+
}
|
|
484
|
+
function canThink(header) {
|
|
485
|
+
const template = header?.chatTemplate;
|
|
486
|
+
return template === undefined ? undefined : /<think(?:ing)?>/.test(template);
|
|
487
|
+
}
|
|
478
488
|
export function createEmbedPool(loadSdk, notify, { faults, batchSize = DEFAULT_EMBED_BATCH_SIZE, openclCacheDir, loads = createLoadTracker(), onUnloadFailure, beforeLoad, hold }) {
|
|
479
489
|
const load = async (key, model) => {
|
|
480
490
|
const sdk = await loadSdk();
|
|
@@ -3,7 +3,22 @@ import { HarnessError } from './errors.js';
|
|
|
3
3
|
import { DEFAULT_USER_AGENT } from './user-agent.js';
|
|
4
4
|
const UA_SNIFFING_HOSTS = new Set(['wttr.in']);
|
|
5
5
|
export function runNoRedirectFetch(request, url, init = {}) {
|
|
6
|
-
return new Promise((
|
|
6
|
+
return new Promise((outerResolve, outerReject) => {
|
|
7
|
+
let idle = null;
|
|
8
|
+
const stopIdle = () => {
|
|
9
|
+
if (idle) {
|
|
10
|
+
clearTimeout(idle);
|
|
11
|
+
}
|
|
12
|
+
idle = null;
|
|
13
|
+
};
|
|
14
|
+
const resolve = (response) => {
|
|
15
|
+
stopIdle();
|
|
16
|
+
outerResolve(response);
|
|
17
|
+
};
|
|
18
|
+
const reject = (err) => {
|
|
19
|
+
stopIdle();
|
|
20
|
+
outerReject(err);
|
|
21
|
+
};
|
|
7
22
|
const headers = { ...init.headers };
|
|
8
23
|
if (init.body !== undefined &&
|
|
9
24
|
!Object.keys(headers).some((h) => h.toLowerCase() === 'content-length')) {
|
|
@@ -16,6 +31,7 @@ export function runNoRedirectFetch(request, url, init = {}) {
|
|
|
16
31
|
}
|
|
17
32
|
const method = init.method ?? 'GET';
|
|
18
33
|
const req = request(url, { method, headers, validatedAddresses: init.validatedAddresses }, (res) => {
|
|
34
|
+
armIdle();
|
|
19
35
|
const chunks = [];
|
|
20
36
|
let received = 0;
|
|
21
37
|
let settled = false;
|
|
@@ -45,6 +61,7 @@ export function runNoRedirectFetch(request, url, init = {}) {
|
|
|
45
61
|
reject(err);
|
|
46
62
|
};
|
|
47
63
|
res.on('data', (chunk) => {
|
|
64
|
+
armIdle();
|
|
48
65
|
received += chunk.byteLength;
|
|
49
66
|
if (init.maxBytes !== undefined && received > init.maxBytes) {
|
|
50
67
|
if (init.truncate) {
|
|
@@ -67,6 +84,20 @@ export function runNoRedirectFetch(request, url, init = {}) {
|
|
|
67
84
|
res.on('end', respond);
|
|
68
85
|
});
|
|
69
86
|
req.on('error', reject);
|
|
87
|
+
function armIdle() {
|
|
88
|
+
const ms = init.idleTimeoutMs;
|
|
89
|
+
if (ms === undefined) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
stopIdle();
|
|
93
|
+
idle = setTimeout(() => {
|
|
94
|
+
const err = new Error(`no data received for ${ms} ms`);
|
|
95
|
+
err.name = 'TimeoutError';
|
|
96
|
+
req.destroy(err);
|
|
97
|
+
reject(err);
|
|
98
|
+
}, ms);
|
|
99
|
+
}
|
|
100
|
+
armIdle();
|
|
70
101
|
if (init.signal) {
|
|
71
102
|
const abort = () => {
|
|
72
103
|
const err = new Error('This operation was aborted');
|
package/dist-lib/lib/prompt.d.ts
CHANGED
|
@@ -12,11 +12,13 @@ export interface SystemPromptInput {
|
|
|
12
12
|
bio?: string | null | undefined;
|
|
13
13
|
toolNames?: ReadonlyArray<string>;
|
|
14
14
|
skillsSection?: string | null;
|
|
15
|
+
platform?: string | null | undefined;
|
|
15
16
|
}
|
|
16
|
-
export declare function systemPromptParts({ bio, toolNames, skillsSection }: SystemPromptInput): SystemPromptParts;
|
|
17
|
+
export declare function systemPromptParts({ bio, toolNames, skillsSection, platform }: SystemPromptInput): SystemPromptParts;
|
|
17
18
|
export declare function buildSystemPrompt(input: SystemPromptInput): string;
|
|
18
19
|
export interface PromptSkill {
|
|
19
20
|
readonly name: string;
|
|
20
21
|
readonly description: string;
|
|
22
|
+
readonly note?: string | undefined;
|
|
21
23
|
}
|
|
22
|
-
export declare function skillsPromptSection(
|
|
24
|
+
export declare function skillsPromptSection(skills: ReadonlyArray<PromptSkill>): string | null;
|
package/dist-lib/lib/prompt.js
CHANGED
|
@@ -73,14 +73,23 @@ function buildGuidelines(active) {
|
|
|
73
73
|
}
|
|
74
74
|
return `**Guidelines:**\n${lines.map((line) => `- ${line}`).join('\n')}`;
|
|
75
75
|
}
|
|
76
|
-
|
|
76
|
+
const PLATFORM_LABELS = {
|
|
77
|
+
darwin: 'macOS',
|
|
78
|
+
linux: 'Linux',
|
|
79
|
+
win32: 'Windows',
|
|
80
|
+
ios: 'iOS',
|
|
81
|
+
android: 'Android'
|
|
82
|
+
};
|
|
83
|
+
export function systemPromptParts({ bio, toolNames = [], skillsSection, platform }) {
|
|
77
84
|
const active = new Set(toolNames);
|
|
78
85
|
const header = active.size
|
|
79
86
|
? 'You are a helpful AI assistant with access to tools.'
|
|
80
87
|
: 'You are a helpful AI assistant.';
|
|
81
88
|
const preamble = `${header}\n\n${METADATA_SECTION}`;
|
|
89
|
+
const label = platform ? PLATFORM_LABELS[platform] : undefined;
|
|
90
|
+
const host = label ? `You run on ${label}.` : null;
|
|
82
91
|
const skills = active.has('skill') ? skillsSection : null;
|
|
83
|
-
const text = [preamble, bio, skills, buildGuidelines(active)]
|
|
92
|
+
const text = [preamble, host, bio, skills, buildGuidelines(active)]
|
|
84
93
|
.filter((part) => Boolean(part))
|
|
85
94
|
.join('\n\n');
|
|
86
95
|
return { text, bioChars: (bio ?? '').length, skillsChars: (skills ?? '').length };
|
|
@@ -88,9 +97,11 @@ export function systemPromptParts({ bio, toolNames = [], skillsSection }) {
|
|
|
88
97
|
export function buildSystemPrompt(input) {
|
|
89
98
|
return systemPromptParts(input).text;
|
|
90
99
|
}
|
|
91
|
-
export function skillsPromptSection(
|
|
92
|
-
if (!
|
|
100
|
+
export function skillsPromptSection(skills) {
|
|
101
|
+
if (!skills.length)
|
|
93
102
|
return null;
|
|
94
|
-
const lines =
|
|
103
|
+
const lines = skills
|
|
104
|
+
.map((skill) => `- ${skill.name}: ${skill.description}${skill.note ? ` [${skill.note}]` : ''}`)
|
|
105
|
+
.join('\n');
|
|
95
106
|
return `Skills available this turn. You MUST call the \`skill\` tool with its name BEFORE using the capability it describes — load the skill first, then act.\n${lines}`;
|
|
96
107
|
}
|
|
@@ -11,6 +11,7 @@ interface RuntimeLease {
|
|
|
11
11
|
modelId: string;
|
|
12
12
|
ctxSize?: number | undefined;
|
|
13
13
|
slots?: number | undefined;
|
|
14
|
+
canThink?: boolean | undefined;
|
|
14
15
|
release(): void;
|
|
15
16
|
}
|
|
16
17
|
interface RuntimeAcquireOptions {
|
|
@@ -33,17 +34,17 @@ interface RuntimeResidency {
|
|
|
33
34
|
};
|
|
34
35
|
tools(): readonly ToolDefinition[];
|
|
35
36
|
skills(agentId: string | null, model: string): Promise<CompletionSkillEntry[]>;
|
|
36
|
-
|
|
37
|
+
platform(): string | undefined;
|
|
37
38
|
loadOptions(model: string, agent?: CompletionContext['agent']): ModelLoadOptions;
|
|
38
39
|
deviceConfig(): LlmDeviceConfig;
|
|
39
40
|
acquireLease(options: RuntimeAcquireOptions): Promise<RuntimeLease | null>;
|
|
40
|
-
|
|
41
|
+
roomForCompletion(model: string, options: {
|
|
41
42
|
loadOpts: ModelLoadOptions;
|
|
42
|
-
signal?: BareAbortSignal;
|
|
43
|
-
}): Promise<
|
|
43
|
+
signal?: BareAbortSignal | undefined;
|
|
44
|
+
}): Promise<void>;
|
|
44
45
|
loadLifecycleSdk(): Promise<ModelLifecycleSdk>;
|
|
45
46
|
}
|
|
46
|
-
export type RoundResidency = Pick<RuntimeResidency, 'pool' | 'gate' | 'sdk' | 'acquireLease' | '
|
|
47
|
+
export type RoundResidency = Pick<RuntimeResidency, 'pool' | 'gate' | 'sdk' | 'acquireLease' | 'roomForCompletion'>;
|
|
47
48
|
interface RoundTarget {
|
|
48
49
|
model: string;
|
|
49
50
|
key: string;
|
|
@@ -30,6 +30,7 @@ export async function prepareCompletionRuntime(residency, request) {
|
|
|
30
30
|
requestMessages: messages,
|
|
31
31
|
tools: withRunTools(residency.tools(), request.tools ?? []),
|
|
32
32
|
skillCatalog: await residency.skills(agent?.id ?? null, model),
|
|
33
|
+
platform: residency.platform(),
|
|
33
34
|
fsRoots: [
|
|
34
35
|
...(grantedDirs?.length ? await createLocalRoots(grantedDirs) : []),
|
|
35
36
|
...remoteRoots
|
|
@@ -52,13 +53,7 @@ export function roundAcquirer(residency, { model, key, loadOpts, signal }) {
|
|
|
52
53
|
open: () => pool.open(key, model, loadOpts),
|
|
53
54
|
signal: invocationSignal,
|
|
54
55
|
prepare: async () => {
|
|
55
|
-
|
|
56
|
-
await residency.makeRoomForCompletion(model, {
|
|
57
|
-
loadOpts,
|
|
58
|
-
signal: invocationSignal ?? signal
|
|
59
|
-
});
|
|
60
|
-
await residency.unloadIdleGenerators();
|
|
61
|
-
}
|
|
56
|
+
await residency.roomForCompletion(model, { loadOpts, signal: invocationSignal ?? signal });
|
|
62
57
|
return true;
|
|
63
58
|
}
|
|
64
59
|
});
|
|
@@ -75,6 +70,7 @@ export function roundAcquirer(residency, { model, key, loadOpts, signal }) {
|
|
|
75
70
|
modelId: lease.modelId,
|
|
76
71
|
ctxSize: lease.ctxSize,
|
|
77
72
|
slots: lease.slots,
|
|
73
|
+
canThink: lease.canThink,
|
|
78
74
|
sdk,
|
|
79
75
|
release: lease.release
|
|
80
76
|
};
|