dsh-taskbridge 1.7.1 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -21
- package/README.md +733 -877
- package/THIRD_PARTY_NOTICES.txt +23 -23
- package/antigravity/agents/dsh-taskbridge.md +32 -0
- package/antigravity/skills/dsh-taskbridge/SKILL.md +51 -0
- package/codex/dsh-taskbridge.toml +181 -631
- package/cordis.patch.yml +6 -6
- package/docs/INTRODUCTION.md +122 -295
- package/docs/INTRODUCTION.zh.md +134 -279
- package/docs/antigravity-integration.md +42 -0
- package/docs/auth-contract.md +138 -138
- package/docs/claude-code.md +6 -11
- package/docs/codex-integration.md +2 -2
- package/docs/contracts-stage-b.md +443 -443
- package/docs/conversation-placement.md +7 -6
- package/docs/plugin-market.md +30 -30
- package/docs/redaction-contract.md +170 -170
- package/docs/releases/1.7.0.zh.md +11 -11
- package/docs/releases/1.8.0.zh.md +16 -0
- package/docs/releases/1.8.1.zh.md +24 -0
- package/docs/runtime-compatibility.md +1 -1
- package/docs/taskbridge-upgrade.md +28 -28
- package/docs/taskdispatch-upgrade.md +25 -25
- package/docs/workspace-contract.md +538 -538
- package/docs/workspace-grouping.md +126 -126
- package/icon.svg +1 -1
- package/lib/client.d.ts +804 -699
- package/lib/client.js +7 -7
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +150 -144
- package/src/antigravity/installer.js +1068 -0
- package/src/antigravity/seam.js +142 -0
- package/src/auth/fence.js +361 -361
- package/src/auth/grants.js +789 -789
- package/src/client/api.js +266 -3
- package/src/client/index.jsx +224 -11
- package/src/client/sidebar.jsx +862 -862
- package/src/client/styles.js +624 -624
- package/src/client/task-cleanup.js +429 -304
- package/src/client/task-records-table.jsx +217 -217
- package/src/client/task-view.js +307 -307
- package/src/client/workspace-groups.js +482 -482
- package/src/client/workspaces-browser.jsx +770 -770
- package/src/codex/agent-ownership.js +270 -270
- package/src/codex/installer.js +6 -0
- package/src/codex/toml.js +784 -784
- package/src/config/defaults.js +11 -15
- package/src/config/redact.js +245 -245
- package/src/config/store.js +186 -186
- package/src/credentials/seam.js +202 -202
- package/src/http/respond.js +6 -5
- package/src/http/user-plane.js +83 -1
- package/src/index.js +25 -0
- package/src/jobs/diagnostics.js +15 -15
- package/src/jobs/engine.js +1 -0
- package/src/jobs/history.js +1 -1
- package/src/jobs/plan-store.js +1103 -1020
- package/src/jobs/record-cleanup.js +452 -7
- package/src/jobs/review-note.js +63 -63
- package/src/jobs/task-descriptions.js +82 -82
- package/src/jobs/task-display.js +73 -73
- package/src/jobs/task-tree.js +786 -786
- package/src/jobs/title.js +134 -134
- package/src/mcp/continuation.js +317 -317
- package/src/mcp/server.js +34 -27
- package/src/mcp/wait.js +34 -15
- package/src/model/connectivity.js +371 -371
- package/src/model/discovery.js +582 -582
- package/src/model/native-catalog.js +68 -68
- package/src/model/openai-adapter.js +671 -671
- package/src/model/probe.js +264 -264
- package/src/model/providers.js +756 -756
- package/src/model/routing.js +321 -321
- package/src/runtime/agent-runner.js +1 -0
- package/src/runtime/permissions.js +1619 -1619
- package/src/runtime/question-call.js +162 -162
- package/src/runtime/questions.js +909 -909
- package/src/runtime/session-placement.js +36 -1
- package/src/runtime/worker-prompt.js +61 -55
- package/src/workspace/audit.js +775 -775
- package/src/workspace/preflight.js +424 -424
- package/src/workspace/worktrees.js +833 -679
package/src/model/probe.js
CHANGED
|
@@ -1,264 +1,264 @@
|
|
|
1
|
-
// Model reachability probe.
|
|
2
|
-
//
|
|
3
|
-
// This exists because "the endpoint answered" and "the model answered" are
|
|
4
|
-
// different facts, and conflating them produces the classic false negative: a
|
|
5
|
-
// reasoning model is asked for one token, spends its entire budget thinking,
|
|
6
|
-
// emits no visible text, and looks broken. The probe therefore:
|
|
7
|
-
// * sends a real request with a practical output budget;
|
|
8
|
-
// * treats a reasoning-only response as a working model;
|
|
9
|
-
// * treats a budget ending as a working model whenever the response carried
|
|
10
|
-
// anything at all, because a provider that truncated the answer still
|
|
11
|
-
// answered;
|
|
12
|
-
// * reports what it observed instead of a bare boolean.
|
|
13
|
-
|
|
14
|
-
import { diagnostic } from '../config/redact.js';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Default probe budget. Large enough that a reasoning model can finish.
|
|
18
|
-
*
|
|
19
|
-
* This is the whole point of the constant: the historical failure was a probe
|
|
20
|
-
* that asked for one token, so a reasoning model spent it on thinking and the
|
|
21
|
-
* check reported a timeout for a provider that was working perfectly.
|
|
22
|
-
*/
|
|
23
|
-
export const PROBE_MAX_TOKENS = 2048;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Per-request budget for one reasoning effort, when the model declares a small
|
|
27
|
-
* `maxTokens` capacity. Never below {@link PROBE_MIN_TOKENS}: a budget that
|
|
28
|
-
* cannot hold a thought is the false negative this module exists to prevent.
|
|
29
|
-
*/
|
|
30
|
-
export const PROBE_MIN_TOKENS = 256;
|
|
31
|
-
|
|
32
|
-
/** Finish reasons that mean "the provider answered and stopped": everything else is a failure. */
|
|
33
|
-
const PRODUCTIVE_FINISH = new Set(['stop', 'max-tokens', 'tool-calls']);
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Read the probe budget from the options, falling back to the default.
|
|
37
|
-
* @param {object} options - probe options.
|
|
38
|
-
* @returns {number} a usable token budget.
|
|
39
|
-
*/
|
|
40
|
-
function budgetOf(options) {
|
|
41
|
-
const configured = options?.maxTokens;
|
|
42
|
-
if (Number.isInteger(configured) && configured <= 0) return PROBE_MAX_TOKENS;
|
|
43
|
-
if (Number.isInteger(configured)) return Math.max(configured, PROBE_MIN_TOKENS);
|
|
44
|
-
return PROBE_MAX_TOKENS;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Normalize a streamed finish reason.
|
|
49
|
-
*
|
|
50
|
-
* The native protocol is `FinishReasonMap` (`{ type: 'finish', reason: { kind } }`),
|
|
51
|
-
* and pi-ai maps a provider's `length` stop to `max-tokens`; a double-underscore
|
|
52
|
-
* or snake-case spelling is accepted as well so a provider that reports its own
|
|
53
|
-
* stop reason cannot be misread as "no finish at all".
|
|
54
|
-
* @param {unknown} reason - chunk reason as a string or object.
|
|
55
|
-
* @returns {string | null} normalized reason kind.
|
|
56
|
-
*/
|
|
57
|
-
export function normalizeFinishReason(reason) {
|
|
58
|
-
const raw = typeof reason === 'string'
|
|
59
|
-
? reason
|
|
60
|
-
: (reason !== null && typeof reason === 'object' && typeof reason.kind === 'string' ? reason.kind : null);
|
|
61
|
-
if (raw === null || raw === '') return null;
|
|
62
|
-
const normalized = raw.toLowerCase().replace(/_/g, '-');
|
|
63
|
-
if (normalized === 'length' || normalized === 'max-tokens' || normalized === 'maxtokens') return 'max-tokens';
|
|
64
|
-
if (normalized === 'end-turn' || normalized === 'stop' || normalized === 'stop-sequence') return 'stop';
|
|
65
|
-
if (normalized === 'tool-use' || normalized === 'tool-calls') return 'tool-calls';
|
|
66
|
-
if (normalized === 'aborted' || normalized === 'canceled' || normalized === 'cancelled') return 'aborted';
|
|
67
|
-
return normalized;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Whether one terminal finish reason means the provider answered.
|
|
72
|
-
*
|
|
73
|
-
* `max-tokens` belongs here on purpose: the response exists and the provider
|
|
74
|
-
* stopped it at the budget. Only an explicit error or abort is a failure.
|
|
75
|
-
* @param {string | null} kind - normalized reason kind.
|
|
76
|
-
* @returns {boolean} true when the response counts as an answer.
|
|
77
|
-
*/
|
|
78
|
-
export function isProductiveFinish(kind) {
|
|
79
|
-
return kind === null || PRODUCTIVE_FINISH.has(kind);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Classify a finished probe stream.
|
|
84
|
-
*
|
|
85
|
-
* Exported so the classification is testable without a stream, and so a caller
|
|
86
|
-
* that already collected text/reasoning can reuse exactly this decision.
|
|
87
|
-
* @param {object} input - `{ text, reasoningText, finishKind, usage }`.
|
|
88
|
-
* @returns {object} the verdict fields of a probe result.
|
|
89
|
-
*/
|
|
90
|
-
export function classifyProbeResponse(input) {
|
|
91
|
-
const text = typeof input?.text === 'string' ? input.text : '';
|
|
92
|
-
const reasoningText = typeof input?.reasoningText === 'string' ? input.reasoningText : '';
|
|
93
|
-
const finishKind = input?.finishKind ?? null;
|
|
94
|
-
const visible = text.trim();
|
|
95
|
-
const reasoningOnly = visible === '' && reasoningText.trim() !== '';
|
|
96
|
-
const answered = visible !== '' || reasoningOnly;
|
|
97
|
-
const productive = isProductiveFinish(finishKind);
|
|
98
|
-
const maxTokens = Number.isInteger(input?.maxTokens) ? input.maxTokens : PROBE_MAX_TOKENS;
|
|
99
|
-
|
|
100
|
-
let code;
|
|
101
|
-
if (answered) {
|
|
102
|
-
// The model answered. A budget ending is reported as a budget fact rather
|
|
103
|
-
// than as a failure, because the provider demonstrably works.
|
|
104
|
-
code = 'PROBE_OK';
|
|
105
|
-
} else if (productive) {
|
|
106
|
-
// The provider completed the request and the answer carried nothing at all.
|
|
107
|
-
code = 'PROBE_EMPTY_RESPONSE';
|
|
108
|
-
} else {
|
|
109
|
-
code = finishKind === 'aborted' ? 'PROBE_TIMEOUT' : 'PROBE_FAILED';
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
ok: code === 'PROBE_OK',
|
|
114
|
-
detail: {
|
|
115
|
-
code,
|
|
116
|
-
finishReason: finishKind,
|
|
117
|
-
reasoningOnly,
|
|
118
|
-
maxTokens,
|
|
119
|
-
// Stated as its own observation: an operator raising the budget reads it
|
|
120
|
-
// here instead of inferring it from a truncated answer.
|
|
121
|
-
budgetExhausted: finishKind === 'max-tokens',
|
|
122
|
-
...(code === 'PROBE_OK' ? {} : { message: messageFor(code, finishKind) }),
|
|
123
|
-
},
|
|
124
|
-
text: visible.slice(0, 200),
|
|
125
|
-
reasoningOnly,
|
|
126
|
-
reasoningChars: reasoningText.length,
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Operator-facing text for a non-OK probe code.
|
|
132
|
-
* @param {string} code - probe code.
|
|
133
|
-
* @param {string | null} finishKind - normalized finish reason.
|
|
134
|
-
* @returns {string} bounded message.
|
|
135
|
-
*/
|
|
136
|
-
function messageFor(code, finishKind) {
|
|
137
|
-
if (code === 'PROBE_EMPTY_RESPONSE') return 'the model completed the request without any visible or reasoning content';
|
|
138
|
-
if (code === 'PROBE_TIMEOUT') return 'the model request was aborted before an answer arrived';
|
|
139
|
-
return `the model request ended without an answer (finish reason ${finishKind ?? 'unreported'})`;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Send one short request through the native LLM seam and classify the response.
|
|
144
|
-
* @param {object} deps - `llm` native service.
|
|
145
|
-
* @param {object} route - value returned by `resolveRoute`.
|
|
146
|
-
* @param {object} [options] - `maxTokens`, `signal`, `timeoutMs`.
|
|
147
|
-
* @returns {Promise<object>} probe result with explicit observations.
|
|
148
|
-
*/
|
|
149
|
-
export async function probeModel(deps, route, options = {}) {
|
|
150
|
-
const llm = deps?.llm;
|
|
151
|
-
if (llm === undefined || llm === null || typeof llm.stream !== 'function') {
|
|
152
|
-
return {
|
|
153
|
-
ok: false,
|
|
154
|
-
detail: { code: 'PROBE_UNAVAILABLE', message: 'the native llm service cannot stream a request' },
|
|
155
|
-
text: '',
|
|
156
|
-
reasoningOnly: false,
|
|
157
|
-
reasoningChars: 0,
|
|
158
|
-
tokens: null,
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const maxTokens = budgetOf(options);
|
|
163
|
-
const controller = new AbortController();
|
|
164
|
-
const timeoutMs = Number.isInteger(options.timeoutMs) && options.timeoutMs > 0 ? options.timeoutMs : 120_000;
|
|
165
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
166
|
-
timer.unref?.();
|
|
167
|
-
const onExternalAbort = () => controller.abort();
|
|
168
|
-
options.signal?.addEventListener?.('abort', onExternalAbort, { once: true });
|
|
169
|
-
|
|
170
|
-
let text = '';
|
|
171
|
-
let reasoningText = '';
|
|
172
|
-
let usage = null;
|
|
173
|
-
let finishKind = null;
|
|
174
|
-
|
|
175
|
-
try {
|
|
176
|
-
const stream = llm.stream({
|
|
177
|
-
provider: route.providerRoute,
|
|
178
|
-
model: route.model,
|
|
179
|
-
...(route.reasoningEffort === null || route.reasoningEffort === undefined
|
|
180
|
-
? {}
|
|
181
|
-
: { reasoningEffort: route.reasoningEffort }),
|
|
182
|
-
maxTokens,
|
|
183
|
-
messages: [{
|
|
184
|
-
role: 'user',
|
|
185
|
-
content: [{ type: 'text', text: 'Reply with the single word: ready' }],
|
|
186
|
-
}],
|
|
187
|
-
signal: controller.signal,
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
for await (const chunk of stream) {
|
|
191
|
-
if (chunk?.type === 'text-delta' && typeof chunk.text === 'string') text += chunk.text;
|
|
192
|
-
if (chunk?.type === 'reasoning-delta' && typeof chunk.text === 'string') reasoningText += chunk.text;
|
|
193
|
-
if (chunk?.type === 'usage' && chunk.usage !== null && typeof chunk.usage === 'object') usage = chunk.usage;
|
|
194
|
-
// `block-end` carries the assembled block, which is the only place a
|
|
195
|
-
// provider that streams no deltas (or drops them) is visible.
|
|
196
|
-
if (chunk?.type === 'block-end' && chunk.block !== null && typeof chunk.block === 'object') {
|
|
197
|
-
if (chunk.block.type === 'text' && typeof chunk.block.text === 'string' && text.trim() === '') {
|
|
198
|
-
text += chunk.block.text;
|
|
199
|
-
}
|
|
200
|
-
if (chunk.block.type === 'reasoning' && typeof chunk.block.text === 'string' && reasoningText.trim() === '') {
|
|
201
|
-
reasoningText += chunk.block.text;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
if (chunk?.type === 'finish') finishKind = normalizeFinishReason(chunk.reason);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const verdict = classifyProbeResponse({ text, reasoningText, finishKind, maxTokens });
|
|
208
|
-
return {
|
|
209
|
-
...verdict,
|
|
210
|
-
tokens: usage === null ? null : {
|
|
211
|
-
inputTokens: usage.inputTokens ?? null,
|
|
212
|
-
outputTokens: usage.outputTokens ?? null,
|
|
213
|
-
reasoningTokens: usage.reasoningTokens ?? null,
|
|
214
|
-
},
|
|
215
|
-
};
|
|
216
|
-
} catch (error) {
|
|
217
|
-
const aborted = controller.signal.aborted === true;
|
|
218
|
-
// A stream that threw after delivering reasoning still answered the request.
|
|
219
|
-
const delivered = text.trim() !== '' || reasoningText.trim() !== '';
|
|
220
|
-
if (delivered) {
|
|
221
|
-
const verdict = classifyProbeResponse({
|
|
222
|
-
text,
|
|
223
|
-
reasoningText,
|
|
224
|
-
// The failure happened after content arrived, so the answer is real and
|
|
225
|
-
// the reason is a transport fact worth reporting beside it.
|
|
226
|
-
finishKind: null,
|
|
227
|
-
maxTokens,
|
|
228
|
-
});
|
|
229
|
-
return {
|
|
230
|
-
...verdict,
|
|
231
|
-
detail: {
|
|
232
|
-
...verdict.detail,
|
|
233
|
-
streamError: {
|
|
234
|
-
aborted,
|
|
235
|
-
message: diagnostic(error?.message ?? error, 300),
|
|
236
|
-
cause: typeof error?.code === 'string' ? error.code : null,
|
|
237
|
-
},
|
|
238
|
-
},
|
|
239
|
-
tokens: usage === null ? null : {
|
|
240
|
-
inputTokens: usage.inputTokens ?? null,
|
|
241
|
-
outputTokens: usage.outputTokens ?? null,
|
|
242
|
-
reasoningTokens: usage.reasoningTokens ?? null,
|
|
243
|
-
},
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
return {
|
|
247
|
-
ok: false,
|
|
248
|
-
detail: {
|
|
249
|
-
code: aborted ? 'PROBE_TIMEOUT' : 'PROBE_FAILED',
|
|
250
|
-
message: diagnostic(error?.message ?? error, 300) ?? 'the model request failed',
|
|
251
|
-
cause: typeof error?.code === 'string' ? error.code : null,
|
|
252
|
-
timeoutMs: aborted ? timeoutMs : null,
|
|
253
|
-
maxTokens,
|
|
254
|
-
},
|
|
255
|
-
text: text.trim().slice(0, 200),
|
|
256
|
-
reasoningOnly: false,
|
|
257
|
-
reasoningChars: reasoningText.length,
|
|
258
|
-
tokens: null,
|
|
259
|
-
};
|
|
260
|
-
} finally {
|
|
261
|
-
clearTimeout(timer);
|
|
262
|
-
options.signal?.removeEventListener?.('abort', onExternalAbort);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
1
|
+
// Model reachability probe.
|
|
2
|
+
//
|
|
3
|
+
// This exists because "the endpoint answered" and "the model answered" are
|
|
4
|
+
// different facts, and conflating them produces the classic false negative: a
|
|
5
|
+
// reasoning model is asked for one token, spends its entire budget thinking,
|
|
6
|
+
// emits no visible text, and looks broken. The probe therefore:
|
|
7
|
+
// * sends a real request with a practical output budget;
|
|
8
|
+
// * treats a reasoning-only response as a working model;
|
|
9
|
+
// * treats a budget ending as a working model whenever the response carried
|
|
10
|
+
// anything at all, because a provider that truncated the answer still
|
|
11
|
+
// answered;
|
|
12
|
+
// * reports what it observed instead of a bare boolean.
|
|
13
|
+
|
|
14
|
+
import { diagnostic } from '../config/redact.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Default probe budget. Large enough that a reasoning model can finish.
|
|
18
|
+
*
|
|
19
|
+
* This is the whole point of the constant: the historical failure was a probe
|
|
20
|
+
* that asked for one token, so a reasoning model spent it on thinking and the
|
|
21
|
+
* check reported a timeout for a provider that was working perfectly.
|
|
22
|
+
*/
|
|
23
|
+
export const PROBE_MAX_TOKENS = 2048;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Per-request budget for one reasoning effort, when the model declares a small
|
|
27
|
+
* `maxTokens` capacity. Never below {@link PROBE_MIN_TOKENS}: a budget that
|
|
28
|
+
* cannot hold a thought is the false negative this module exists to prevent.
|
|
29
|
+
*/
|
|
30
|
+
export const PROBE_MIN_TOKENS = 256;
|
|
31
|
+
|
|
32
|
+
/** Finish reasons that mean "the provider answered and stopped": everything else is a failure. */
|
|
33
|
+
const PRODUCTIVE_FINISH = new Set(['stop', 'max-tokens', 'tool-calls']);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Read the probe budget from the options, falling back to the default.
|
|
37
|
+
* @param {object} options - probe options.
|
|
38
|
+
* @returns {number} a usable token budget.
|
|
39
|
+
*/
|
|
40
|
+
function budgetOf(options) {
|
|
41
|
+
const configured = options?.maxTokens;
|
|
42
|
+
if (Number.isInteger(configured) && configured <= 0) return PROBE_MAX_TOKENS;
|
|
43
|
+
if (Number.isInteger(configured)) return Math.max(configured, PROBE_MIN_TOKENS);
|
|
44
|
+
return PROBE_MAX_TOKENS;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Normalize a streamed finish reason.
|
|
49
|
+
*
|
|
50
|
+
* The native protocol is `FinishReasonMap` (`{ type: 'finish', reason: { kind } }`),
|
|
51
|
+
* and pi-ai maps a provider's `length` stop to `max-tokens`; a double-underscore
|
|
52
|
+
* or snake-case spelling is accepted as well so a provider that reports its own
|
|
53
|
+
* stop reason cannot be misread as "no finish at all".
|
|
54
|
+
* @param {unknown} reason - chunk reason as a string or object.
|
|
55
|
+
* @returns {string | null} normalized reason kind.
|
|
56
|
+
*/
|
|
57
|
+
export function normalizeFinishReason(reason) {
|
|
58
|
+
const raw = typeof reason === 'string'
|
|
59
|
+
? reason
|
|
60
|
+
: (reason !== null && typeof reason === 'object' && typeof reason.kind === 'string' ? reason.kind : null);
|
|
61
|
+
if (raw === null || raw === '') return null;
|
|
62
|
+
const normalized = raw.toLowerCase().replace(/_/g, '-');
|
|
63
|
+
if (normalized === 'length' || normalized === 'max-tokens' || normalized === 'maxtokens') return 'max-tokens';
|
|
64
|
+
if (normalized === 'end-turn' || normalized === 'stop' || normalized === 'stop-sequence') return 'stop';
|
|
65
|
+
if (normalized === 'tool-use' || normalized === 'tool-calls') return 'tool-calls';
|
|
66
|
+
if (normalized === 'aborted' || normalized === 'canceled' || normalized === 'cancelled') return 'aborted';
|
|
67
|
+
return normalized;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether one terminal finish reason means the provider answered.
|
|
72
|
+
*
|
|
73
|
+
* `max-tokens` belongs here on purpose: the response exists and the provider
|
|
74
|
+
* stopped it at the budget. Only an explicit error or abort is a failure.
|
|
75
|
+
* @param {string | null} kind - normalized reason kind.
|
|
76
|
+
* @returns {boolean} true when the response counts as an answer.
|
|
77
|
+
*/
|
|
78
|
+
export function isProductiveFinish(kind) {
|
|
79
|
+
return kind === null || PRODUCTIVE_FINISH.has(kind);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Classify a finished probe stream.
|
|
84
|
+
*
|
|
85
|
+
* Exported so the classification is testable without a stream, and so a caller
|
|
86
|
+
* that already collected text/reasoning can reuse exactly this decision.
|
|
87
|
+
* @param {object} input - `{ text, reasoningText, finishKind, usage }`.
|
|
88
|
+
* @returns {object} the verdict fields of a probe result.
|
|
89
|
+
*/
|
|
90
|
+
export function classifyProbeResponse(input) {
|
|
91
|
+
const text = typeof input?.text === 'string' ? input.text : '';
|
|
92
|
+
const reasoningText = typeof input?.reasoningText === 'string' ? input.reasoningText : '';
|
|
93
|
+
const finishKind = input?.finishKind ?? null;
|
|
94
|
+
const visible = text.trim();
|
|
95
|
+
const reasoningOnly = visible === '' && reasoningText.trim() !== '';
|
|
96
|
+
const answered = visible !== '' || reasoningOnly;
|
|
97
|
+
const productive = isProductiveFinish(finishKind);
|
|
98
|
+
const maxTokens = Number.isInteger(input?.maxTokens) ? input.maxTokens : PROBE_MAX_TOKENS;
|
|
99
|
+
|
|
100
|
+
let code;
|
|
101
|
+
if (answered) {
|
|
102
|
+
// The model answered. A budget ending is reported as a budget fact rather
|
|
103
|
+
// than as a failure, because the provider demonstrably works.
|
|
104
|
+
code = 'PROBE_OK';
|
|
105
|
+
} else if (productive) {
|
|
106
|
+
// The provider completed the request and the answer carried nothing at all.
|
|
107
|
+
code = 'PROBE_EMPTY_RESPONSE';
|
|
108
|
+
} else {
|
|
109
|
+
code = finishKind === 'aborted' ? 'PROBE_TIMEOUT' : 'PROBE_FAILED';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
ok: code === 'PROBE_OK',
|
|
114
|
+
detail: {
|
|
115
|
+
code,
|
|
116
|
+
finishReason: finishKind,
|
|
117
|
+
reasoningOnly,
|
|
118
|
+
maxTokens,
|
|
119
|
+
// Stated as its own observation: an operator raising the budget reads it
|
|
120
|
+
// here instead of inferring it from a truncated answer.
|
|
121
|
+
budgetExhausted: finishKind === 'max-tokens',
|
|
122
|
+
...(code === 'PROBE_OK' ? {} : { message: messageFor(code, finishKind) }),
|
|
123
|
+
},
|
|
124
|
+
text: visible.slice(0, 200),
|
|
125
|
+
reasoningOnly,
|
|
126
|
+
reasoningChars: reasoningText.length,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Operator-facing text for a non-OK probe code.
|
|
132
|
+
* @param {string} code - probe code.
|
|
133
|
+
* @param {string | null} finishKind - normalized finish reason.
|
|
134
|
+
* @returns {string} bounded message.
|
|
135
|
+
*/
|
|
136
|
+
function messageFor(code, finishKind) {
|
|
137
|
+
if (code === 'PROBE_EMPTY_RESPONSE') return 'the model completed the request without any visible or reasoning content';
|
|
138
|
+
if (code === 'PROBE_TIMEOUT') return 'the model request was aborted before an answer arrived';
|
|
139
|
+
return `the model request ended without an answer (finish reason ${finishKind ?? 'unreported'})`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Send one short request through the native LLM seam and classify the response.
|
|
144
|
+
* @param {object} deps - `llm` native service.
|
|
145
|
+
* @param {object} route - value returned by `resolveRoute`.
|
|
146
|
+
* @param {object} [options] - `maxTokens`, `signal`, `timeoutMs`.
|
|
147
|
+
* @returns {Promise<object>} probe result with explicit observations.
|
|
148
|
+
*/
|
|
149
|
+
export async function probeModel(deps, route, options = {}) {
|
|
150
|
+
const llm = deps?.llm;
|
|
151
|
+
if (llm === undefined || llm === null || typeof llm.stream !== 'function') {
|
|
152
|
+
return {
|
|
153
|
+
ok: false,
|
|
154
|
+
detail: { code: 'PROBE_UNAVAILABLE', message: 'the native llm service cannot stream a request' },
|
|
155
|
+
text: '',
|
|
156
|
+
reasoningOnly: false,
|
|
157
|
+
reasoningChars: 0,
|
|
158
|
+
tokens: null,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const maxTokens = budgetOf(options);
|
|
163
|
+
const controller = new AbortController();
|
|
164
|
+
const timeoutMs = Number.isInteger(options.timeoutMs) && options.timeoutMs > 0 ? options.timeoutMs : 120_000;
|
|
165
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
166
|
+
timer.unref?.();
|
|
167
|
+
const onExternalAbort = () => controller.abort();
|
|
168
|
+
options.signal?.addEventListener?.('abort', onExternalAbort, { once: true });
|
|
169
|
+
|
|
170
|
+
let text = '';
|
|
171
|
+
let reasoningText = '';
|
|
172
|
+
let usage = null;
|
|
173
|
+
let finishKind = null;
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
const stream = llm.stream({
|
|
177
|
+
provider: route.providerRoute,
|
|
178
|
+
model: route.model,
|
|
179
|
+
...(route.reasoningEffort === null || route.reasoningEffort === undefined
|
|
180
|
+
? {}
|
|
181
|
+
: { reasoningEffort: route.reasoningEffort }),
|
|
182
|
+
maxTokens,
|
|
183
|
+
messages: [{
|
|
184
|
+
role: 'user',
|
|
185
|
+
content: [{ type: 'text', text: 'Reply with the single word: ready' }],
|
|
186
|
+
}],
|
|
187
|
+
signal: controller.signal,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
for await (const chunk of stream) {
|
|
191
|
+
if (chunk?.type === 'text-delta' && typeof chunk.text === 'string') text += chunk.text;
|
|
192
|
+
if (chunk?.type === 'reasoning-delta' && typeof chunk.text === 'string') reasoningText += chunk.text;
|
|
193
|
+
if (chunk?.type === 'usage' && chunk.usage !== null && typeof chunk.usage === 'object') usage = chunk.usage;
|
|
194
|
+
// `block-end` carries the assembled block, which is the only place a
|
|
195
|
+
// provider that streams no deltas (or drops them) is visible.
|
|
196
|
+
if (chunk?.type === 'block-end' && chunk.block !== null && typeof chunk.block === 'object') {
|
|
197
|
+
if (chunk.block.type === 'text' && typeof chunk.block.text === 'string' && text.trim() === '') {
|
|
198
|
+
text += chunk.block.text;
|
|
199
|
+
}
|
|
200
|
+
if (chunk.block.type === 'reasoning' && typeof chunk.block.text === 'string' && reasoningText.trim() === '') {
|
|
201
|
+
reasoningText += chunk.block.text;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (chunk?.type === 'finish') finishKind = normalizeFinishReason(chunk.reason);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const verdict = classifyProbeResponse({ text, reasoningText, finishKind, maxTokens });
|
|
208
|
+
return {
|
|
209
|
+
...verdict,
|
|
210
|
+
tokens: usage === null ? null : {
|
|
211
|
+
inputTokens: usage.inputTokens ?? null,
|
|
212
|
+
outputTokens: usage.outputTokens ?? null,
|
|
213
|
+
reasoningTokens: usage.reasoningTokens ?? null,
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
} catch (error) {
|
|
217
|
+
const aborted = controller.signal.aborted === true;
|
|
218
|
+
// A stream that threw after delivering reasoning still answered the request.
|
|
219
|
+
const delivered = text.trim() !== '' || reasoningText.trim() !== '';
|
|
220
|
+
if (delivered) {
|
|
221
|
+
const verdict = classifyProbeResponse({
|
|
222
|
+
text,
|
|
223
|
+
reasoningText,
|
|
224
|
+
// The failure happened after content arrived, so the answer is real and
|
|
225
|
+
// the reason is a transport fact worth reporting beside it.
|
|
226
|
+
finishKind: null,
|
|
227
|
+
maxTokens,
|
|
228
|
+
});
|
|
229
|
+
return {
|
|
230
|
+
...verdict,
|
|
231
|
+
detail: {
|
|
232
|
+
...verdict.detail,
|
|
233
|
+
streamError: {
|
|
234
|
+
aborted,
|
|
235
|
+
message: diagnostic(error?.message ?? error, 300),
|
|
236
|
+
cause: typeof error?.code === 'string' ? error.code : null,
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
tokens: usage === null ? null : {
|
|
240
|
+
inputTokens: usage.inputTokens ?? null,
|
|
241
|
+
outputTokens: usage.outputTokens ?? null,
|
|
242
|
+
reasoningTokens: usage.reasoningTokens ?? null,
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
ok: false,
|
|
248
|
+
detail: {
|
|
249
|
+
code: aborted ? 'PROBE_TIMEOUT' : 'PROBE_FAILED',
|
|
250
|
+
message: diagnostic(error?.message ?? error, 300) ?? 'the model request failed',
|
|
251
|
+
cause: typeof error?.code === 'string' ? error.code : null,
|
|
252
|
+
timeoutMs: aborted ? timeoutMs : null,
|
|
253
|
+
maxTokens,
|
|
254
|
+
},
|
|
255
|
+
text: text.trim().slice(0, 200),
|
|
256
|
+
reasoningOnly: false,
|
|
257
|
+
reasoningChars: reasoningText.length,
|
|
258
|
+
tokens: null,
|
|
259
|
+
};
|
|
260
|
+
} finally {
|
|
261
|
+
clearTimeout(timer);
|
|
262
|
+
options.signal?.removeEventListener?.('abort', onExternalAbort);
|
|
263
|
+
}
|
|
264
|
+
}
|