@insmind/cli 1.5.1 → 1.5.3
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/contracts/operations/auth.status/output.schema.json +3 -4
- package/dist/bin/insmind-cli.js +12 -7
- package/dist/bin/postinstall.js +7 -5
- package/dist/bin/{agent-worker.js → task-worker.js} +5 -4
- package/dist/src/analytics/device.js +23 -0
- package/dist/src/analytics/wind.js +54 -0
- package/dist/src/bootstrap/create-cli.js +33 -19
- package/dist/src/bootstrap/create-runtime.js +45 -16
- package/dist/src/bootstrap/{run-agent-worker.js → run-task-worker.js} +20 -10
- package/dist/src/bootstrap/validators.js +10 -5
- package/dist/src/cli/action-binding.js +14 -0
- package/dist/src/cli/agent-commands.js +9 -4
- package/dist/src/cli/auth-commands.js +5 -3
- package/dist/src/cli/credits-command.js +2 -2
- package/dist/src/cli/dam-commands.js +51 -52
- package/dist/src/cli/editor-commands.js +13 -15
- package/dist/src/cli/errors.js +97 -44
- package/dist/src/cli/model-commands.js +6 -4
- package/dist/src/cli/presenter.js +13 -10
- package/dist/src/cli/task-commands.js +13 -5
- package/dist/src/cli/tool-commands.js +11 -5
- package/dist/src/features/agent/use-cases.js +11 -5
- package/dist/src/features/auth/access-policy.js +2 -0
- package/dist/src/features/auth/credential-store.js +2 -2
- package/dist/src/features/auth/sso-service.js +5 -4
- package/dist/src/features/auth/state.js +11 -1
- package/dist/src/features/auth/use-cases.js +43 -40
- package/dist/src/features/credits/use-cases.js +2 -0
- package/dist/src/features/dam/file-downloader.js +38 -16
- package/dist/src/features/dam/object-storage.js +23 -11
- package/dist/src/features/dam/operation-executor.js +46 -37
- package/dist/src/features/dam/registered-uploader.js +39 -29
- package/dist/src/features/dam/storage-upload.js +5 -4
- package/dist/src/features/dam/use-cases.js +81 -60
- package/dist/src/features/editor/bridge-client.js +6 -6
- package/dist/src/features/editor/bridge-process.js +2 -1
- package/dist/src/features/editor/session-state.js +6 -4
- package/dist/src/features/editor/session.js +26 -8
- package/dist/src/features/editor/use-cases.js +10 -3
- package/dist/src/features/org/use-cases.js +2 -1
- package/dist/src/features/task/store.js +4 -0
- package/dist/src/features/task/{agent-background.js → task-background.js} +5 -4
- package/dist/src/features/task/use-cases.js +123 -28
- package/dist/src/features/tool/catalog.js +11 -5
- package/dist/src/features/tool/dynamic-schema.js +9 -7
- package/dist/src/features/tool/mns-catalog-adapter.js +1 -0
- package/dist/src/features/tool/tool-api-adapter.js +8 -7
- package/dist/src/features/tool/use-cases.js +8 -4
- package/dist/src/features/update/update-notification.js +75 -70
- package/dist/src/features/update/update-service.js +24 -20
- package/dist/src/platform/file-lock.js +36 -9
- package/dist/src/platform/interruption.js +13 -0
- package/dist/src/platform/json-input.js +12 -9
- package/dist/src/platform/local-json-file.js +8 -4
- package/dist/src/platform/redact.js +7 -1
- package/dist/src/platform/remote-protocol.js +2 -2
- package/dist/src/platform/safe-upload-file.js +2 -1
- package/dist/src/platform/schema-issues.js +8 -0
- package/dist/src/platform/signed-http-transport.js +8 -7
- package/dist/src/platform/text-output.js +33 -0
- package/dist/src/telemetry/invocation.js +19 -4
- package/dist/src/telemetry/process-errors.js +14 -4
- package/dist/src/telemetry/sanitize.js +4 -2
- package/dist/src/telemetry/sls-sink.js +36 -20
- package/dist/vendor/README.md +10 -0
- package/dist/vendor/gd-tracker-node.mjs +1175 -0
- package/package.json +4 -4
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { redactSensitiveText } from "../../platform/redact.js";
|
|
2
|
-
import { taskScope } from "./store.js";
|
|
2
|
+
import { TaskStoreError, taskScope } from "./store.js";
|
|
3
|
+
import { LocalFileError } from "../../platform/local-json-file.js";
|
|
3
4
|
export class TaskNotFoundError extends Error {
|
|
4
|
-
constructor(message) { super(message); }
|
|
5
|
+
constructor(message) { super(message); this.name = "TaskNotFoundError"; }
|
|
5
6
|
}
|
|
6
7
|
// Cached results are private output data, but expiring resource credentials are
|
|
7
8
|
// removed. Query delivery uses the existing authorization adapter again.
|
|
@@ -36,6 +37,15 @@ const LEASE_MS = 15_000;
|
|
|
36
37
|
export function createTaskUseCases(dependencies) {
|
|
37
38
|
const { text, commandName } = dependencies;
|
|
38
39
|
const telemetry = dependencies.telemetry;
|
|
40
|
+
const warnedTasks = new Set();
|
|
41
|
+
const stage = (name, run) => telemetry ? telemetry.stage(name, run) : run();
|
|
42
|
+
function cacheWarning(id) {
|
|
43
|
+
if (warnedTasks.has(id))
|
|
44
|
+
return;
|
|
45
|
+
warnedTasks.add(id);
|
|
46
|
+
telemetry?.annotate({ warning_codes: ["TASK_CACHE_UNAVAILABLE"] });
|
|
47
|
+
(dependencies.warning ?? dependencies.notice)(text(`任务 ${id} 的本地记录未能保存,task get 可能无法恢复。请保留本次输出,不要重新提交创作。`, `The local record for task ${id} could not be saved; task get may not recover it. Keep this output and do not submit the creation again.`));
|
|
48
|
+
}
|
|
39
49
|
async function fact(scope, record, name, at) {
|
|
40
50
|
if (!telemetry || !record.telemetry)
|
|
41
51
|
return;
|
|
@@ -49,19 +59,18 @@ export function createTaskUseCases(dependencies) {
|
|
|
49
59
|
const candidate = { ...context, event_name: name, occurred_at_ms: at,
|
|
50
60
|
record_seq: name === "request_sent" ? 1 : 2 };
|
|
51
61
|
try {
|
|
52
|
-
const updated = await dependencies.store.update(scope, record.task.taskId, stored => {
|
|
62
|
+
const updated = await stage("telemetry.task", () => dependencies.store.update(scope, record.task.taskId, stored => {
|
|
53
63
|
if (stored.telemetry && !stored.telemetry[name]) {
|
|
54
64
|
stored.telemetry.context = context;
|
|
55
65
|
stored.telemetry[name] = candidate;
|
|
56
66
|
}
|
|
57
|
-
});
|
|
67
|
+
}));
|
|
58
68
|
if (updated.telemetry)
|
|
59
69
|
record.telemetry = updated.telemetry;
|
|
60
70
|
}
|
|
61
|
-
catch
|
|
71
|
+
catch {
|
|
62
72
|
// Business persistence has its own failure path. Keep the already known fact.
|
|
63
73
|
record.telemetry[name] ??= candidate;
|
|
64
|
-
await telemetry.stage("telemetry.task", () => { throw error; }).catch(() => undefined);
|
|
65
74
|
}
|
|
66
75
|
await telemetry.emit(record.telemetry[name] ?? candidate);
|
|
67
76
|
}
|
|
@@ -95,7 +104,62 @@ export function createTaskUseCases(dependencies) {
|
|
|
95
104
|
...(dependencies.creationVersion ? { cli_version: dependencies.creationVersion } : {}),
|
|
96
105
|
...(input.state.account ? { account_id: input.state.account.id } : {}), organization_id: input.state.organization.id } };
|
|
97
106
|
}
|
|
98
|
-
|
|
107
|
+
// The task execution owns Wind. Recovery reads and SLS replay never call it.
|
|
108
|
+
const analytics = input.state.account ? await dependencies.analytics?.prepare({
|
|
109
|
+
userId: input.state.account.id, organizationId: input.state.organization.id
|
|
110
|
+
}).catch(() => undefined) : undefined;
|
|
111
|
+
if (analytics)
|
|
112
|
+
record.analytics = { context: analytics.context };
|
|
113
|
+
async function analyticsFact(event) {
|
|
114
|
+
if (!analytics || !record.analytics)
|
|
115
|
+
return;
|
|
116
|
+
try {
|
|
117
|
+
let claimed = false;
|
|
118
|
+
const updated = await stage("wind.record", () => dependencies.store.update(scope, record.task.taskId, stored => {
|
|
119
|
+
if (!stored.analytics || stored.analytics[event])
|
|
120
|
+
return;
|
|
121
|
+
if (event === "started") {
|
|
122
|
+
const generation = record.progress.generation;
|
|
123
|
+
let details;
|
|
124
|
+
if (input.operation === "agent.send")
|
|
125
|
+
details = { tool_type: "agent", tool_name: "agent" };
|
|
126
|
+
else if (generation?.model && (generation.tool === "image.generate" || generation.tool === "video.generate")) {
|
|
127
|
+
const name = generation.tool === "image.generate" ? "AI绘图" : "AI视频生成";
|
|
128
|
+
details = { tool_type: name, tool_name: name, model_name: generation.model };
|
|
129
|
+
}
|
|
130
|
+
else
|
|
131
|
+
return;
|
|
132
|
+
stored.analytics.details = details;
|
|
133
|
+
}
|
|
134
|
+
else if (!stored.analytics.started)
|
|
135
|
+
return;
|
|
136
|
+
stored.analytics[event] = true;
|
|
137
|
+
claimed = true;
|
|
138
|
+
}));
|
|
139
|
+
if (updated.analytics)
|
|
140
|
+
record.analytics = updated.analytics;
|
|
141
|
+
if (claimed && updated.analytics?.details)
|
|
142
|
+
analytics.track(event, updated.analytics.details);
|
|
143
|
+
}
|
|
144
|
+
catch { /* Failed analytics persistence cannot change a creation result. */ }
|
|
145
|
+
}
|
|
146
|
+
let dispatched = false;
|
|
147
|
+
let durableAcceptance = false;
|
|
148
|
+
function storageContext(error, phase) {
|
|
149
|
+
if (error instanceof TaskStoreError)
|
|
150
|
+
error.context = { cli_task_id: record.task.taskId, phase,
|
|
151
|
+
request_sent: dispatched, accepted: record.progress.accepted === true || !!record.progress.remoteTaskId,
|
|
152
|
+
result_state: record.task.state };
|
|
153
|
+
return error;
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
await stage("task.create", () => dependencies.store.write(scope, record));
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
record.task.state = "failed";
|
|
160
|
+
telemetry?.annotate({ result_state: "failed" });
|
|
161
|
+
throw storageContext(error, "before_dispatch");
|
|
162
|
+
}
|
|
99
163
|
input.onUpdate?.(structuredClone(record.task), { ...record.progress });
|
|
100
164
|
dependencies.notice(text(`任务 ID: ${record.task.taskId};结果未收到时执行 ${commandName} task get ${record.task.taskId}`, `Task ID: ${record.task.taskId}; retrieve missing output with ${commandName} task get ${record.task.taskId}`));
|
|
101
165
|
let writes = Promise.resolve();
|
|
@@ -107,53 +171,76 @@ export function createTaskUseCases(dependencies) {
|
|
|
107
171
|
};
|
|
108
172
|
let lastHeartbeatError = -Infinity;
|
|
109
173
|
const timer = setInterval(() => {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (
|
|
113
|
-
|
|
114
|
-
lastHeartbeatError = at;
|
|
115
|
-
await telemetry?.stage("task.heartbeat", () => { throw error; }).catch(() => undefined);
|
|
174
|
+
const report = dependencies.now().getTime() - lastHeartbeatError >= 60_000;
|
|
175
|
+
void (report ? stage("task.heartbeat", save) : save()).catch(() => {
|
|
176
|
+
if (report)
|
|
177
|
+
lastHeartbeatError = dependencies.now().getTime();
|
|
116
178
|
});
|
|
117
179
|
}, HEARTBEAT_MS);
|
|
118
180
|
timer.unref();
|
|
119
181
|
try {
|
|
120
182
|
const result = await input.run(async (progress) => {
|
|
121
183
|
if (progress.dispatchedAt !== undefined) {
|
|
184
|
+
dispatched = true;
|
|
122
185
|
await fact(scope, record, "request_sent", progress.dispatchedAt);
|
|
186
|
+
await analyticsFact("started");
|
|
123
187
|
return;
|
|
124
188
|
}
|
|
125
189
|
Object.assign(record.progress, progress);
|
|
126
190
|
if (progress.contextId)
|
|
127
191
|
record.task.contextId = progress.contextId;
|
|
128
|
-
|
|
192
|
+
try {
|
|
193
|
+
await stage("task.progress", save);
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (!(error instanceof LocalFileError) || !dispatched || (input.executionMode === "async" && !durableAcceptance)) {
|
|
197
|
+
throw storageContext(error, dispatched ? "after_dispatch" : "before_dispatch");
|
|
198
|
+
}
|
|
199
|
+
cacheWarning(record.task.taskId);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
durableAcceptance ||= record.progress.accepted === true || !!record.progress.remoteTaskId;
|
|
129
203
|
input.onUpdate?.(structuredClone(record.task), { ...record.progress });
|
|
130
204
|
});
|
|
131
205
|
clearInterval(timer);
|
|
132
|
-
finish(record, result);
|
|
206
|
+
await stage("task.validate", async () => { finish(record, result); });
|
|
133
207
|
telemetry?.annotate({ result_state: record.task.state });
|
|
134
|
-
if (record.task.state === "completed")
|
|
208
|
+
if (record.task.state === "completed") {
|
|
135
209
|
await fact(scope, record, "result_ready", dependencies.now().getTime());
|
|
136
|
-
|
|
210
|
+
await analyticsFact("completed");
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
await stage("task.persist", save);
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
if (!(error instanceof LocalFileError) || record.task.state !== "completed" || input.executionMode === "async") {
|
|
217
|
+
throw storageContext(error, "final_persist");
|
|
218
|
+
}
|
|
219
|
+
cacheWarning(record.task.taskId);
|
|
220
|
+
}
|
|
137
221
|
return { task: record.task, result };
|
|
138
222
|
}
|
|
139
223
|
catch (error) {
|
|
140
224
|
clearInterval(timer);
|
|
141
225
|
if (record.task.state !== "completed") {
|
|
142
|
-
|
|
143
|
-
record.task.
|
|
226
|
+
const mayBeRunning = dispatched || record.progress.submissionStarted;
|
|
227
|
+
record.task.state = mayBeRunning ? "unknown" : "failed";
|
|
228
|
+
record.task.note = mayBeRunning
|
|
144
229
|
? text("CLI 未取得完整回执或结果;远端可能仍在执行。查询原任务,不自动重新生成。", "The CLI did not receive a complete receipt or result; the service may still be running. Query this task instead of submitting again.")
|
|
145
230
|
: text("CLI 在提交创作前结束;具体错误见原命令。", "The CLI stopped before submitting the creation. See the original command for the error.");
|
|
146
231
|
}
|
|
147
232
|
const detail = dependencies.failureNote?.(error);
|
|
148
233
|
if (detail)
|
|
149
|
-
record.task.note =
|
|
150
|
-
|
|
234
|
+
record.task.note = [record.task.note, redactSensitiveText(detail)].filter(Boolean).join(" ");
|
|
235
|
+
telemetry?.annotate({ result_state: record.task.state });
|
|
236
|
+
await stage("task.persist", save).catch(() => undefined);
|
|
151
237
|
throw error;
|
|
152
238
|
}
|
|
153
239
|
}
|
|
154
240
|
return {
|
|
155
241
|
async track(input) {
|
|
156
242
|
return (await execute(input, (record, result) => {
|
|
243
|
+
dependencies.validateResult?.(result);
|
|
157
244
|
record.task.state = "completed";
|
|
158
245
|
record.task.result = withoutResourceKeys(result);
|
|
159
246
|
if ("message" in result)
|
|
@@ -164,39 +251,47 @@ export function createTaskUseCases(dependencies) {
|
|
|
164
251
|
return (await execute({ ...input, executionMode: "async" }, record => { record.progress.detached = true; })).task;
|
|
165
252
|
},
|
|
166
253
|
async list({ state, limit, operation }) {
|
|
167
|
-
const records = await dependencies.store.list(taskScope(state));
|
|
254
|
+
const records = await stage("task.read", () => dependencies.store.list(taskScope(state)));
|
|
168
255
|
return { tasks: records.filter(r => !operation || r.task.operation === operation).slice(0, limit).map(summary) };
|
|
169
256
|
},
|
|
170
257
|
async get({ state, id, signal }) {
|
|
171
258
|
signal.throwIfAborted();
|
|
172
259
|
const scope = taskScope(state);
|
|
173
|
-
|
|
260
|
+
telemetry?.annotate({ cli_task_id: id });
|
|
261
|
+
const record = await stage("task.read", () => dependencies.store.read(scope, id));
|
|
174
262
|
if (!record)
|
|
175
263
|
throw new TaskNotFoundError(text(`当前身份中没有该任务。请执行 ${commandName} task list 查找。`, `No task was found in the current identity context. Use ${commandName} task list to find it.`));
|
|
176
264
|
telemetry?.annotate({ cli_task_id: id, result_state: record.task.state });
|
|
177
265
|
await replay(record);
|
|
178
266
|
if (record.task.state === "completed" && record.task.result) {
|
|
179
|
-
return { ...record.task, result: await dependencies.recovery.authorize(record.task.result, state, signal) };
|
|
267
|
+
return { ...record.task, result: await stage("task.authorize", () => dependencies.recovery.authorize(record.task.result, state, signal)) };
|
|
180
268
|
}
|
|
181
269
|
const task = summary(record);
|
|
182
270
|
if (task.state === "failed" || (task.state === "running" && !record.progress.detached))
|
|
183
271
|
return task;
|
|
184
|
-
const recovered = await dependencies.recovery[task.operation === "agent.send" ? "agent" : "tool"](record.progress, state, signal);
|
|
272
|
+
const recovered = await stage("task.recover", () => dependencies.recovery[task.operation === "agent.send" ? "agent" : "tool"](record.progress, state, signal));
|
|
185
273
|
const { note: _note, ...metadata } = task;
|
|
186
274
|
const result = { ...metadata, ...recovered };
|
|
187
275
|
if (result.state === "unknown" && task.note) {
|
|
188
276
|
result.note = [task.note, recovered.note].filter(Boolean).join(" ");
|
|
189
277
|
}
|
|
190
|
-
telemetry?.annotate({ result_state: result.state });
|
|
191
278
|
if (result.state === "completed" && result.result) {
|
|
192
|
-
dependencies.validateResult?.(result.result);
|
|
279
|
+
await stage("task.validate", async () => { dependencies.validateResult?.(result.result); });
|
|
193
280
|
await fact(scope, record, "result_ready", dependencies.now().getTime());
|
|
194
281
|
}
|
|
282
|
+
telemetry?.annotate({ result_state: result.state });
|
|
195
283
|
// Store merging protects terminal results and facts against stale heartbeats.
|
|
196
284
|
if (result.state === "completed" || result.state === "failed") {
|
|
197
285
|
result.updatedAt = dependencies.now().toISOString();
|
|
198
286
|
record.task = withoutResourceKeys(result);
|
|
199
|
-
|
|
287
|
+
try {
|
|
288
|
+
await stage("task.persist", () => dependencies.store.write(scope, record));
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
if (!(error instanceof LocalFileError) || result.state !== "completed" || !result.result)
|
|
292
|
+
throw error;
|
|
293
|
+
cacheWarning(id);
|
|
294
|
+
}
|
|
200
295
|
}
|
|
201
296
|
return result;
|
|
202
297
|
}
|
|
@@ -11,8 +11,14 @@ export const defaultToolDefinitions = [
|
|
|
11
11
|
}
|
|
12
12
|
];
|
|
13
13
|
export class ToolInputError extends Error {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
reason;
|
|
15
|
+
field;
|
|
16
|
+
issues;
|
|
17
|
+
constructor(reason = "invalid_tool_input", field, issues = [], cause) {
|
|
18
|
+
super(`Tool 输入不可用:${reason}${field ? ` (${field})` : ""}${issues.length ? ` ${JSON.stringify(issues)}` : ""}`, { cause });
|
|
19
|
+
this.reason = reason;
|
|
20
|
+
this.field = field;
|
|
21
|
+
this.issues = issues;
|
|
16
22
|
this.name = "ToolInputError";
|
|
17
23
|
}
|
|
18
24
|
}
|
|
@@ -42,7 +48,7 @@ export function projectToolList(catalog, tools = defaultToolDefinitions) {
|
|
|
42
48
|
}
|
|
43
49
|
export function projectModelList(catalog, tool, tools = defaultToolDefinitions) {
|
|
44
50
|
if (tool !== undefined && !catalog.models.some((model) => model.tool === tool)) {
|
|
45
|
-
throw new ToolInputError();
|
|
51
|
+
throw new ToolInputError("tool_not_found", "/tool");
|
|
46
52
|
}
|
|
47
53
|
return {
|
|
48
54
|
models: catalog.models
|
|
@@ -54,7 +60,7 @@ export function projectModelList(catalog, tool, tools = defaultToolDefinitions)
|
|
|
54
60
|
export function findModel(catalog, model) {
|
|
55
61
|
const found = catalog.models.find((candidate) => candidate.model === model);
|
|
56
62
|
if (found === undefined)
|
|
57
|
-
throw new ToolInputError();
|
|
63
|
+
throw new ToolInputError("model_not_found", "/model");
|
|
58
64
|
return found;
|
|
59
65
|
}
|
|
60
66
|
export function projectModelDetail(catalog, model) {
|
|
@@ -73,6 +79,6 @@ export function projectModelDetail(catalog, model) {
|
|
|
73
79
|
export function findToolModel(catalog, tool, model) {
|
|
74
80
|
const found = findModel(catalog, model);
|
|
75
81
|
if (found.tool !== tool)
|
|
76
|
-
throw new ToolInputError();
|
|
82
|
+
throw new ToolInputError("model_tool_mismatch", "/model");
|
|
77
83
|
return found;
|
|
78
84
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
2
2
|
import addFormatsImport, {} from "ajv-formats";
|
|
3
|
+
import { validationIssues } from "../../platform/schema-issues.js";
|
|
3
4
|
import { ToolInputError } from "./catalog.js";
|
|
4
5
|
function parameterSchema(parameter, includeBounds = false) {
|
|
5
6
|
const common = {
|
|
@@ -55,14 +56,14 @@ function mergeSchema(current, next) {
|
|
|
55
56
|
enum: [...new Set([...current.enum, ...next.enum])]
|
|
56
57
|
};
|
|
57
58
|
}
|
|
58
|
-
export function buildToolArgumentsSchema(catalog, tool) {
|
|
59
|
+
export function buildToolArgumentsSchema(catalog, tool, modelDescription = "要调用的模型机器标识;取值来自 model list。") {
|
|
59
60
|
const models = catalog.models.filter((model) => model.tool === tool);
|
|
60
61
|
if (models.length === 0)
|
|
61
|
-
throw new ToolInputError();
|
|
62
|
+
throw new ToolInputError("tool_not_found", "/tool");
|
|
62
63
|
const properties = {
|
|
63
64
|
model: {
|
|
64
65
|
type: "string",
|
|
65
|
-
description:
|
|
66
|
+
description: modelDescription
|
|
66
67
|
}
|
|
67
68
|
};
|
|
68
69
|
for (const model of models) {
|
|
@@ -125,10 +126,11 @@ function controlArgumentsValidator(model) {
|
|
|
125
126
|
});
|
|
126
127
|
}
|
|
127
128
|
export function assertModelArguments(model, value) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
const validate = validator(model);
|
|
130
|
+
if (!validate(value))
|
|
131
|
+
throw new ToolInputError("model_parameters", "/arguments", validationIssues(validate.errors));
|
|
132
|
+
if (!sliderStepsAreValid(model, value)) {
|
|
133
|
+
throw new ToolInputError("model_parameters", "/arguments");
|
|
132
134
|
}
|
|
133
135
|
}
|
|
134
136
|
function sliderStepsAreValid(model, value) {
|
|
@@ -248,6 +248,7 @@ export function createMnsCatalogAdapter(dependencies) {
|
|
|
248
248
|
|| (left.model < right.model ? -1 : left.model > right.model ? 1 : 0));
|
|
249
249
|
return {
|
|
250
250
|
models,
|
|
251
|
+
...(invalidCount > 0 ? { invalidModelCount: invalidCount } : {}),
|
|
251
252
|
warnings: invalidCount === 0
|
|
252
253
|
? []
|
|
253
254
|
: [dependencies.invalidModelWarning?.(invalidCount)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isInterruption } from "../../platform/interruption.js";
|
|
1
2
|
import { randomInt } from "node:crypto";
|
|
2
3
|
import { setTimeout as delay } from "node:timers/promises";
|
|
3
4
|
import { RemoteRequestError } from "../../platform/signed-http-transport.js";
|
|
@@ -252,11 +253,11 @@ async function safePost(transport, request) {
|
|
|
252
253
|
return await transport.postJson(request);
|
|
253
254
|
}
|
|
254
255
|
catch (error) {
|
|
255
|
-
if (request.signal
|
|
256
|
+
if (isInterruption(error, request.signal))
|
|
256
257
|
throw request.signal.reason;
|
|
257
258
|
if (error instanceof RemoteRequestError || error instanceof RemoteProtocolError)
|
|
258
259
|
throw error;
|
|
259
|
-
throw new RemoteRequestError();
|
|
260
|
+
throw new RemoteRequestError(undefined, false, {}, error);
|
|
260
261
|
}
|
|
261
262
|
}
|
|
262
263
|
async function defaultSleep(milliseconds, signal) {
|
|
@@ -264,10 +265,10 @@ async function defaultSleep(milliseconds, signal) {
|
|
|
264
265
|
try {
|
|
265
266
|
await delay(milliseconds, undefined, { signal });
|
|
266
267
|
}
|
|
267
|
-
catch {
|
|
268
|
-
if (signal
|
|
268
|
+
catch (error) {
|
|
269
|
+
if (isInterruption(error, signal))
|
|
269
270
|
throw signal.reason;
|
|
270
|
-
throw new RemoteRequestError();
|
|
271
|
+
throw new RemoteRequestError(undefined, false, {}, error);
|
|
271
272
|
}
|
|
272
273
|
}
|
|
273
274
|
function defaultContentId() {
|
|
@@ -344,9 +345,9 @@ export function createToolApiAdapter(dependencies) {
|
|
|
344
345
|
transientFailures = 0;
|
|
345
346
|
}
|
|
346
347
|
catch (error) {
|
|
347
|
-
if (signal
|
|
348
|
+
if (isInterruption(error, signal))
|
|
348
349
|
throw signal.reason;
|
|
349
|
-
if (!(error instanceof RemoteRequestError) || !error.transient)
|
|
350
|
+
if (signal.aborted || !(error instanceof RemoteRequestError) || !error.transient)
|
|
350
351
|
throw error;
|
|
351
352
|
transientFailures += 1;
|
|
352
353
|
if (transientFailures > 3)
|
|
@@ -41,7 +41,7 @@ function remoteMedia(value, name) {
|
|
|
41
41
|
}
|
|
42
42
|
catch (error) {
|
|
43
43
|
if (error instanceof UrlSafetyError)
|
|
44
|
-
throw new ToolInputError();
|
|
44
|
+
throw new ToolInputError("invalid_asset_url", `/arguments/${name}`, [], error);
|
|
45
45
|
throw error;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -50,16 +50,20 @@ export function createToolUseCases(dependencies) {
|
|
|
50
50
|
signal.throwIfAborted();
|
|
51
51
|
const catalog = await dependencies.telemetry.stage("catalog", () => (dependencies.catalog.load({ access, signal })));
|
|
52
52
|
signal.throwIfAborted();
|
|
53
|
+
if (catalog.warnings.length)
|
|
54
|
+
dependencies.telemetry.annotate({ warning_codes: ["MODEL_CATALOG_INCOMPLETE"], invalid_model_count: catalog.invalidModelCount });
|
|
53
55
|
for (const warning of catalog.warnings)
|
|
54
56
|
dependencies.warn(warning);
|
|
55
57
|
return catalog;
|
|
56
58
|
}
|
|
57
59
|
async function prepare({ input, access, signal, observe }) {
|
|
60
|
+
dependencies.telemetry.annotate({ tool_name: input.name,
|
|
61
|
+
...(typeof input.arguments.model === "string" ? { model_id: input.arguments.model } : {}) });
|
|
58
62
|
const catalog = await load(access, signal);
|
|
59
63
|
const model = await dependencies.telemetry.stage("input", () => {
|
|
60
64
|
const modelName = input.arguments.model;
|
|
61
65
|
if (typeof modelName !== "string")
|
|
62
|
-
throw new ToolInputError();
|
|
66
|
+
throw new ToolInputError("model_required", "/arguments/model");
|
|
63
67
|
const selected = findToolModel(catalog, input.name, modelName);
|
|
64
68
|
assertModelArguments(selected, input.arguments);
|
|
65
69
|
return selected;
|
|
@@ -151,7 +155,7 @@ export function createToolUseCases(dependencies) {
|
|
|
151
155
|
model: model.model,
|
|
152
156
|
...(model.cost === undefined ? {} : { cost: model.cost })
|
|
153
157
|
};
|
|
154
|
-
await observe?.({ toolUsage: usage });
|
|
158
|
+
await observe?.({ toolUsage: usage, generation: { tool: model.tool, model: model.model } });
|
|
155
159
|
return { invocation, usage };
|
|
156
160
|
}
|
|
157
161
|
return {
|
|
@@ -165,7 +169,7 @@ export function createToolUseCases(dependencies) {
|
|
|
165
169
|
return projectModelDetail(await load(access, signal), input.model);
|
|
166
170
|
},
|
|
167
171
|
async getArgumentsSchema({ name, access, signal }) {
|
|
168
|
-
return buildToolArgumentsSchema(await load(access, signal), name);
|
|
172
|
+
return buildToolArgumentsSchema(await load(access, signal), name, dependencies.modelDescription);
|
|
169
173
|
},
|
|
170
174
|
async call(input) {
|
|
171
175
|
const { invocation, usage } = await prepare(input);
|
|
@@ -28,86 +28,91 @@ export function createUpdateNotificationService(options) {
|
|
|
28
28
|
return;
|
|
29
29
|
if (env.CI === "true")
|
|
30
30
|
return;
|
|
31
|
-
|
|
31
|
+
const lease = { release: null };
|
|
32
32
|
try {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const checkedAt = now();
|
|
38
|
-
const today = localDate(checkedAt);
|
|
39
|
-
const state = await readState(stateFile);
|
|
40
|
-
if (isNewer(state.latest_version, options.currentVersion)
|
|
41
|
-
&& state.last_notified_on !== today) {
|
|
42
|
-
const reminded = withReminder(state, today, state.latest_version);
|
|
43
|
-
await writeState(stateFile, reminded);
|
|
44
|
-
notify(options, state.latest_version);
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
if (!isDue(state.next_check_at, checkedAt))
|
|
48
|
-
return;
|
|
49
|
-
let latestResult;
|
|
50
|
-
const timeout = signalWithTimeout(signal, REGISTRY_TIMEOUT_MS);
|
|
51
|
-
try {
|
|
52
|
-
latestResult = await fetchLatestVersion({
|
|
53
|
-
fetch: fetchPackage,
|
|
54
|
-
signal: timeout.signal,
|
|
55
|
-
...(options.registryUrl ? { registryUrl: options.registryUrl } : {}),
|
|
56
|
-
...(state.etag ? { etag: state.etag } : {})
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
await options.reportError?.(error).catch(() => undefined);
|
|
61
|
-
if (signal.aborted)
|
|
33
|
+
const run = async () => {
|
|
34
|
+
signal.throwIfAborted();
|
|
35
|
+
lease.release = await lock.acquire();
|
|
36
|
+
if (lease.release === null)
|
|
62
37
|
return;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
38
|
+
const checkedAt = now();
|
|
39
|
+
const today = localDate(checkedAt);
|
|
40
|
+
const state = await readState(stateFile);
|
|
41
|
+
if (isNewer(state.latest_version, options.currentVersion)
|
|
42
|
+
&& state.last_notified_on !== today) {
|
|
43
|
+
const reminded = withReminder(state, today, state.latest_version);
|
|
44
|
+
await writeState(stateFile, reminded);
|
|
45
|
+
notify(options, state.latest_version);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (!isDue(state.next_check_at, checkedAt))
|
|
49
|
+
return;
|
|
50
|
+
let latestResult;
|
|
51
|
+
const timeout = signalWithTimeout(signal, REGISTRY_TIMEOUT_MS);
|
|
52
|
+
try {
|
|
53
|
+
const fetchLatest = () => fetchLatestVersion({
|
|
54
|
+
fetch: fetchPackage,
|
|
55
|
+
signal: timeout.signal,
|
|
56
|
+
...(options.registryUrl ? { registryUrl: options.registryUrl } : {}),
|
|
57
|
+
...(state.etag ? { etag: state.etag } : {})
|
|
58
|
+
});
|
|
59
|
+
latestResult = options.telemetry ? await options.telemetry.stage("update_check.registry", fetchLatest) : await fetchLatest();
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (signal.aborted)
|
|
63
|
+
return;
|
|
64
|
+
await writeState(stateFile, {
|
|
65
|
+
...state,
|
|
66
|
+
schema_version: 1,
|
|
67
|
+
next_check_at: addMilliseconds(checkedAt, FAILED_CHECK_INTERVAL_MS)
|
|
68
|
+
});
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
timeout.dispose();
|
|
73
|
+
}
|
|
74
|
+
const latest = latestResult.status === "not_modified"
|
|
75
|
+
? state.latest_version
|
|
76
|
+
: latestResult.version;
|
|
77
|
+
if (latest === undefined || valid(latest) === null) {
|
|
78
|
+
await writeState(stateFile, {
|
|
79
|
+
...state,
|
|
80
|
+
schema_version: 1,
|
|
81
|
+
next_check_at: addMilliseconds(checkedAt, FAILED_CHECK_INTERVAL_MS)
|
|
82
|
+
});
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
let nextState = {
|
|
78
86
|
...state,
|
|
79
87
|
schema_version: 1,
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
last_success_check_at: checkedAt.toISOString(),
|
|
89
|
+
next_check_at: addMilliseconds(checkedAt, SUCCESS_CHECK_INTERVAL_MS),
|
|
90
|
+
latest_version: latest,
|
|
91
|
+
...(latestResult.status === "modified" && latestResult.etag
|
|
92
|
+
? { etag: latestResult.etag }
|
|
93
|
+
: {})
|
|
94
|
+
};
|
|
95
|
+
if (latestResult.status === "modified" && latestResult.etag === undefined) {
|
|
96
|
+
delete nextState.etag;
|
|
97
|
+
}
|
|
98
|
+
const shouldNotify = gt(latest, options.currentVersion)
|
|
99
|
+
&& nextState.last_notified_on !== today;
|
|
100
|
+
if (shouldNotify)
|
|
101
|
+
nextState = withReminder(nextState, today, latest);
|
|
102
|
+
await writeState(stateFile, nextState);
|
|
103
|
+
if (shouldNotify)
|
|
104
|
+
notify(options, latest);
|
|
93
105
|
};
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
&& nextState.last_notified_on !== today;
|
|
99
|
-
if (shouldNotify)
|
|
100
|
-
nextState = withReminder(nextState, today, latest);
|
|
101
|
-
await writeState(stateFile, nextState);
|
|
102
|
-
if (shouldNotify)
|
|
103
|
-
notify(options, latest);
|
|
106
|
+
if (options.telemetry)
|
|
107
|
+
await options.telemetry.stage("update_check", run);
|
|
108
|
+
else
|
|
109
|
+
await run();
|
|
104
110
|
}
|
|
105
111
|
catch (error) {
|
|
106
|
-
await options.reportError?.(error).catch(() => undefined);
|
|
107
112
|
// Availability checks are best-effort and never change command behavior.
|
|
108
113
|
}
|
|
109
114
|
finally {
|
|
110
|
-
await release?.().catch(() => undefined);
|
|
115
|
+
await lease.release?.().catch(() => undefined);
|
|
111
116
|
}
|
|
112
117
|
}
|
|
113
118
|
};
|