@insmind/cli 1.5.8 → 1.5.10
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/dist/src/bootstrap/run-task-worker.js +4 -2
- package/dist/src/cli/errors.js +23 -1
- package/dist/src/features/agent/creative-agent-adapter.js +3 -3
- package/dist/src/features/agent/creative-protocol.js +42 -9
- package/dist/src/features/task/task-background.js +55 -3
- package/dist/src/features/task/use-cases.js +16 -5
- package/dist/src/features/tool/tool-api-adapter.js +8 -2
- package/dist/src/platform/remote-protocol.js +3 -1
- package/dist/src/platform/signed-http-transport.js +41 -18
- package/dist/src/telemetry/invocation.js +13 -10
- package/package.json +4 -4
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { requireUserIdentity } from "../features/auth/state.js";
|
|
2
2
|
import { isInterruption } from "../platform/interruption.js";
|
|
3
|
-
import {
|
|
3
|
+
import { taskWorkerDiagnostics } from "../features/task/task-background.js";
|
|
4
|
+
import { classifyTelemetryError, mapCliError } from "../cli/errors.js";
|
|
4
5
|
import { redact } from "../platform/redact.js";
|
|
5
6
|
export async function runTaskWorker(options) {
|
|
6
7
|
const runtime = options.runtime;
|
|
@@ -47,7 +48,8 @@ export async function runTaskWorker(options) {
|
|
|
47
48
|
completion = outcome.kind === "interrupted" ? { outcome: "interrupted", error }
|
|
48
49
|
: { outcome: "failure", error, errorCode: outcome.failure.code, errorMessage: outcome.failure.message };
|
|
49
50
|
if (outcome.kind === "failure")
|
|
50
|
-
send({ type: "error", failure: outcome.failure, exitCode: outcome.exitCode
|
|
51
|
+
send({ type: "error", failure: outcome.failure, exitCode: outcome.exitCode,
|
|
52
|
+
diagnostics: taskWorkerDiagnostics(error, classifyTelemetryError(error, { product: runtime.product }), runtime.telemetry.failureStage(error)) });
|
|
51
53
|
}
|
|
52
54
|
finally {
|
|
53
55
|
await runtime.analytics?.shutdown().catch(() => undefined);
|
package/dist/src/cli/errors.js
CHANGED
|
@@ -9,6 +9,7 @@ import { AuthorizedAccountContextMissingError, DeviceAuthorizationExpiredError }
|
|
|
9
9
|
import { AgentInputError } from "../features/agent/use-cases.js";
|
|
10
10
|
import { ToolInputError } from "../features/tool/catalog.js";
|
|
11
11
|
import { ToolTaskFailedError } from "../features/tool/tool-api-adapter.js";
|
|
12
|
+
import { AgentAuditRejectedError } from "../features/agent/creative-protocol.js";
|
|
12
13
|
import { ObjectStorageUploadError } from "../features/dam/object-storage.js";
|
|
13
14
|
import { DamAssetNotDownloadableError, DamAssetNotFoundError, DamFolderNotFoundError } from "../features/dam/dam-api-adapter.js";
|
|
14
15
|
import { DamDownloadError, DamDownloadDestinationExistsError } from "../features/dam/file-downloader.js";
|
|
@@ -132,6 +133,15 @@ export function mapCliError(error, options) {
|
|
|
132
133
|
return failure("INSUFFICIENT_CREDITS", "There are not enough insMind credits to complete this creation.", 1, [`Buy credits at ${options.product.pricingUrl.href}`, "Retry after adding credits."]);
|
|
133
134
|
return failure("INSUFFICIENT_CREDITS", "当前组织稿豆不足,无法完成本次创作。", 1, INSUFFICIENT_CREDITS_NEXT_STEPS);
|
|
134
135
|
}
|
|
136
|
+
if (error instanceof AgentAuditRejectedError) {
|
|
137
|
+
const reason = error.phase === undefined ? "audit_rejected" : `${error.phase}_audit_rejected`;
|
|
138
|
+
const message = error.phase === "input"
|
|
139
|
+
? text("输入内容未通过审核,本次创作已拒绝。", "The input did not pass content review. This creation was rejected.")
|
|
140
|
+
: error.phase === "output"
|
|
141
|
+
? text("生成内容未通过审核,无法交付本次结果。", "The generated content did not pass review. This result cannot be delivered.")
|
|
142
|
+
: text("本次创作被内容审核拒绝。", "This creation was rejected by content review.");
|
|
143
|
+
return failure("REMOTE_REQUEST_FAILED", message, 1, [text("请检查并调整创作内容;不要重复提交相同请求。", "Review and adjust the creation content; do not resubmit the same request.")], { reason });
|
|
144
|
+
}
|
|
135
145
|
if (error instanceof RemoteProtocolError) {
|
|
136
146
|
return failure("REMOTE_PROTOCOL_INCOMPATIBLE", text("稿定服务返回了当前 CLI 无法安全处理的结果;为避免重复创作,已停止处理。", "The insMind service returned a result this CLI cannot safely process; processing stopped to avoid duplicate creation."), 1, [text(`保留诊断 ID。若已有任务 ID,用 ${command} task get <id> 查询原任务;查询异常不能证明生成失败,不要重新提交。`, `Keep the diagnostic ID. If a Task ID is available, use ${command} task get <id>. A query error does not prove generation failed; do not resubmit.`)]);
|
|
137
147
|
}
|
|
@@ -225,6 +235,16 @@ export function classifyTelemetryError(error, options = {}) {
|
|
|
225
235
|
return { outcome: "failure", errorCode: error.code,
|
|
226
236
|
...(error.status === undefined ? {} : { httpStatus: error.status }) };
|
|
227
237
|
const outcome = mapCliError(error, { ...options, aborted: options.aborted === true });
|
|
238
|
+
if (error instanceof TaskBackgroundError) {
|
|
239
|
+
const diagnostics = error.diagnostics;
|
|
240
|
+
return { outcome: outcome.kind === "interrupted" ? "interrupted" : "failure",
|
|
241
|
+
...(outcome.kind === "failure" ? { errorCode: outcome.failure.code } : {}),
|
|
242
|
+
...(diagnostics?.error_type ? { errorType: diagnostics.error_type } : {}),
|
|
243
|
+
...(diagnostics?.failure_stage ? { failureStage: diagnostics.failure_stage } : {}),
|
|
244
|
+
...(diagnostics?.http_status === undefined ? {} : { httpStatus: diagnostics.http_status }),
|
|
245
|
+
...(diagnostics?.remote_error_code ? { remoteErrorCode: diagnostics.remote_error_code } : {}),
|
|
246
|
+
...(diagnostics?.diagnostic_reason ? { diagnosticReason: diagnostics.diagnostic_reason } : {}) };
|
|
247
|
+
}
|
|
228
248
|
let remoteError;
|
|
229
249
|
for (let cause = error, depth = 0; cause instanceof Error && depth < 10; cause = cause.cause, depth++) {
|
|
230
250
|
if (cause instanceof RemoteRequestError || cause instanceof RemoteProtocolError || cause instanceof ToolTaskFailedError
|
|
@@ -244,7 +264,9 @@ export function classifyTelemetryError(error, options = {}) {
|
|
|
244
264
|
: { remoteErrorCode: remoteError.remoteCode }),
|
|
245
265
|
...(remoteError?.remoteBody === undefined
|
|
246
266
|
? {}
|
|
247
|
-
: { remoteErrorBody: remoteError.remoteBody })
|
|
267
|
+
: { remoteErrorBody: remoteError.remoteBody }),
|
|
268
|
+
...((remoteError instanceof RemoteRequestError || remoteError instanceof RemoteProtocolError) && remoteError.diagnosticReason
|
|
269
|
+
? { diagnosticReason: remoteError.diagnosticReason } : {})
|
|
248
270
|
};
|
|
249
271
|
}
|
|
250
272
|
function failure(code, message, exitCode, nextSteps, details) {
|
|
@@ -11,7 +11,7 @@ export function createCreativeAgentAdapter(dependencies) {
|
|
|
11
11
|
}
|
|
12
12
|
: { localMessageId: dependencies.idFactory() };
|
|
13
13
|
await observe?.({ localMessageId: ids.localMessageId, ...(message.contextId ? { contextId: message.contextId } : {}) });
|
|
14
|
-
const streamed = await dependencies.telemetry.stage("execute", async () => {
|
|
14
|
+
const { streamed, contextId } = await dependencies.telemetry.stage("execute", async () => {
|
|
15
15
|
await observe?.({ submissionStarted: true });
|
|
16
16
|
const stream = await dependencies.transport.postStream({
|
|
17
17
|
path: "/ai-agent/v1/thread/completion",
|
|
@@ -23,9 +23,9 @@ export function createCreativeAgentAdapter(dependencies) {
|
|
|
23
23
|
signal
|
|
24
24
|
});
|
|
25
25
|
await observe?.({ accepted: true });
|
|
26
|
-
|
|
26
|
+
const streamed = await parseCreativeStream(stream, signal, observe ? async (contextId) => observe({ contextId }) : undefined);
|
|
27
|
+
return { streamed, contextId: creativeContextId(streamed) };
|
|
27
28
|
});
|
|
28
|
-
const contextId = creativeContextId(streamed);
|
|
29
29
|
return dependencies.telemetry.stage("result", async () => {
|
|
30
30
|
const messages = await dependencies.transport.getJson({
|
|
31
31
|
path: `/ai-agent/v1/thread/${encodeURIComponent(contextId)}/messages`,
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import { RemoteProtocolError } from "../../platform/remote-protocol.js";
|
|
2
|
+
import { RemoteRequestError } from "../../platform/signed-http-transport.js";
|
|
3
|
+
export class AgentAuditRejectedError extends RemoteRequestError {
|
|
4
|
+
phase;
|
|
5
|
+
constructor(revokedMessageId) {
|
|
6
|
+
const phase = typeof revokedMessageId === "string" ? revokedMessageId === "" ? "input" : "output" : undefined;
|
|
7
|
+
super(undefined, false, { remoteCode: "-1006", remoteBody: '{"code":"-1006"}' }, undefined, phase === "input" ? "AGENT_INPUT_AUDIT_REJECTED" : phase === "output" ? "AGENT_OUTPUT_AUDIT_REJECTED" : "AGENT_AUDIT_REJECTED");
|
|
8
|
+
this.name = "AgentAuditRejectedError";
|
|
9
|
+
this.message = "Agent content review rejected this creation.";
|
|
10
|
+
this.phase = phase;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
2
13
|
export function buildCreativeRequest(message, ids) {
|
|
3
14
|
const prompt = [];
|
|
4
15
|
const textParts = [];
|
|
@@ -104,20 +115,38 @@ function parseJson(value) {
|
|
|
104
115
|
function nonEmptyString(value) {
|
|
105
116
|
return typeof value === "string" && value.trim() !== "" ? value : undefined;
|
|
106
117
|
}
|
|
107
|
-
function fail() {
|
|
108
|
-
throw new RemoteProtocolError();
|
|
118
|
+
function fail(diagnosticReason, summary) {
|
|
119
|
+
throw new RemoteProtocolError(summary === undefined ? {} : { remoteBody: JSON.stringify(summary) }, undefined, diagnosticReason);
|
|
109
120
|
}
|
|
110
121
|
export function creativeContextId(messages) {
|
|
111
|
-
|
|
112
|
-
|
|
122
|
+
// A pre-run refusal is streamed before the user input is persisted. Checking
|
|
123
|
+
// status first prevents an empty messages history from hiding that decision.
|
|
124
|
+
for (const message of messages) {
|
|
125
|
+
const content = contentOf(message);
|
|
126
|
+
if (message.role !== "status" || content?.type !== "status")
|
|
127
|
+
continue;
|
|
128
|
+
const status = record(parseJson(content.text));
|
|
129
|
+
if (status?.code === "-1006" || status?.code === -1006) {
|
|
130
|
+
throw new AgentAuditRejectedError(record(message.extra)?.revoked_message_id);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
113
133
|
const contextId = messages
|
|
114
134
|
.map((message) => nonEmptyString(message.thread_id))
|
|
115
135
|
.find((value) => value !== undefined);
|
|
116
|
-
|
|
136
|
+
const systemErrorCount = messages.filter((message) => message.event === "system_error").length;
|
|
137
|
+
const summary = { messageCount: messages.length, contextIdPresent: contextId !== undefined, systemErrorCount };
|
|
138
|
+
if (systemErrorCount > 0) {
|
|
139
|
+
throw new RemoteRequestError(undefined, false, { remoteBody: JSON.stringify(summary) }, undefined, "AGENT_SYSTEM_ERROR");
|
|
140
|
+
}
|
|
141
|
+
return contextId ?? fail("AGENT_CONTEXT_ID_MISSING", summary);
|
|
117
142
|
}
|
|
118
143
|
export function creativeTurnMessages(value, localMessageId) {
|
|
119
|
-
if (!Array.isArray(value)
|
|
120
|
-
return fail();
|
|
144
|
+
if (!Array.isArray(value)) {
|
|
145
|
+
return fail("AGENT_MESSAGES_NOT_ARRAY", { valueType: value === null ? "null" : typeof value });
|
|
146
|
+
}
|
|
147
|
+
const invalidMessageCount = value.filter((message) => record(message) === undefined).length;
|
|
148
|
+
if (invalidMessageCount > 0) {
|
|
149
|
+
return fail("AGENT_MESSAGE_INVALID", { messageCount: value.length, invalidMessageCount });
|
|
121
150
|
}
|
|
122
151
|
const messages = value;
|
|
123
152
|
const start = messages.findIndex((message) => {
|
|
@@ -125,8 +154,12 @@ export function creativeTurnMessages(value, localMessageId) {
|
|
|
125
154
|
return false;
|
|
126
155
|
return message.role === "user" || contentOf(message)?.type === "function_response";
|
|
127
156
|
});
|
|
128
|
-
if (start < 0)
|
|
129
|
-
|
|
157
|
+
if (start < 0) {
|
|
158
|
+
const matchingLocalMessageCount = messages.filter((message) => message.local_message_id === localMessageId).length;
|
|
159
|
+
return fail(matchingLocalMessageCount > 0 ? "AGENT_TURN_ANCHOR_INVALID" : "AGENT_TURN_ANCHOR_MISSING", {
|
|
160
|
+
messageCount: messages.length, matchingLocalMessageCount
|
|
161
|
+
});
|
|
162
|
+
}
|
|
130
163
|
const nextInput = messages.findIndex((message, index) => index > start
|
|
131
164
|
&& typeof message.local_message_id === "string" && message.local_message_id !== ""
|
|
132
165
|
&& message.local_message_id !== localMessageId
|
|
@@ -1,12 +1,64 @@
|
|
|
1
1
|
import { fork } from "node:child_process";
|
|
2
|
+
function diagnosticToken(value, pattern) {
|
|
3
|
+
return typeof value === "string" && pattern.test(value) ? value : undefined;
|
|
4
|
+
}
|
|
5
|
+
function safeDiagnostics(value) {
|
|
6
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
7
|
+
return undefined;
|
|
8
|
+
const record = value;
|
|
9
|
+
const error_type = diagnosticToken(record.error_type, /^(?:[A-Za-z][A-Za-z0-9_]{0,79}|object|string|number|undefined)$/u);
|
|
10
|
+
const remote_error_code = diagnosticToken(record.remote_error_code, /^(?:-?\d{1,16}|[A-Z][A-Z0-9_.:-]{0,79})$/u);
|
|
11
|
+
const diagnostic_reason = diagnosticToken(record.diagnostic_reason, /^[A-Z][A-Z0-9_]{0,79}$/u);
|
|
12
|
+
const failure_stage = diagnosticToken(record.failure_stage, /^[a-z][a-z0-9_.-]{0,79}$/u);
|
|
13
|
+
const http_status = typeof record.http_status === "number" && Number.isInteger(record.http_status)
|
|
14
|
+
&& record.http_status >= 100 && record.http_status <= 599 ? record.http_status : undefined;
|
|
15
|
+
const causes = Array.isArray(record.causes) ? record.causes.slice(0, 10).flatMap(cause => {
|
|
16
|
+
if (!cause || typeof cause !== "object" || Array.isArray(cause))
|
|
17
|
+
return [];
|
|
18
|
+
const item = cause;
|
|
19
|
+
const error_type = diagnosticToken(item.error_type, /^[A-Za-z][A-Za-z0-9_]{0,79}$/u);
|
|
20
|
+
const system_code = diagnosticToken(item.system_code, /^(?:E[A-Z0-9_]{1,40}|UND_ERR_[A-Z0-9_]{1,40})$/u);
|
|
21
|
+
return error_type ? [{ error_type, ...(system_code ? { system_code } : {}) }] : [];
|
|
22
|
+
}) : undefined;
|
|
23
|
+
return {
|
|
24
|
+
...(error_type ? { error_type } : {}), ...(http_status === undefined ? {} : { http_status }),
|
|
25
|
+
...(remote_error_code ? { remote_error_code } : {}), ...(diagnostic_reason ? { diagnostic_reason } : {}),
|
|
26
|
+
...(failure_stage ? { failure_stage } : {}), ...(causes?.length ? { causes } : {})
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function taskWorkerDiagnostics(error, classification, failureStage) {
|
|
30
|
+
const causes = [];
|
|
31
|
+
const seen = new Set();
|
|
32
|
+
for (let cause = error; cause instanceof Error && causes.length < 10 && !seen.has(cause); cause = cause.cause) {
|
|
33
|
+
seen.add(cause);
|
|
34
|
+
causes.push({ error_type: cause.name, ...("code" in cause && typeof cause.code === "string" ? { system_code: cause.code } : {}) });
|
|
35
|
+
}
|
|
36
|
+
return safeDiagnostics({ error_type: error instanceof Error ? error.name : typeof error,
|
|
37
|
+
http_status: classification.httpStatus, remote_error_code: classification.remoteErrorCode,
|
|
38
|
+
diagnostic_reason: classification.diagnosticReason, failure_stage: failureStage, causes }) ?? {};
|
|
39
|
+
}
|
|
40
|
+
function diagnosticCause(diagnostics) {
|
|
41
|
+
let cause;
|
|
42
|
+
for (const item of [...diagnostics?.causes ?? []].reverse()) {
|
|
43
|
+
const error = new Error("Background task failure.", { cause });
|
|
44
|
+
error.name = item.error_type;
|
|
45
|
+
if (item.system_code)
|
|
46
|
+
Object.assign(error, { code: item.system_code });
|
|
47
|
+
cause = error;
|
|
48
|
+
}
|
|
49
|
+
return cause;
|
|
50
|
+
}
|
|
2
51
|
export class TaskBackgroundError extends Error {
|
|
3
52
|
failure;
|
|
4
53
|
exitCode;
|
|
5
|
-
|
|
6
|
-
|
|
54
|
+
diagnostics;
|
|
55
|
+
constructor(failure, exitCode = 1, diagnostics) {
|
|
56
|
+
const safe = safeDiagnostics(diagnostics);
|
|
57
|
+
super(failure.message, { cause: diagnosticCause(safe) });
|
|
7
58
|
this.failure = failure;
|
|
8
59
|
this.exitCode = exitCode;
|
|
9
60
|
this.name = "TaskBackgroundError";
|
|
61
|
+
this.diagnostics = safe;
|
|
10
62
|
}
|
|
11
63
|
}
|
|
12
64
|
export function createTaskBackgroundLauncher(options) {
|
|
@@ -53,7 +105,7 @@ export function createTaskBackgroundLauncher(options) {
|
|
|
53
105
|
else if (message.type === "accepted")
|
|
54
106
|
finish(message.task);
|
|
55
107
|
else if (message.type === "error")
|
|
56
|
-
finish(new TaskBackgroundError(message.failure, message.exitCode));
|
|
108
|
+
finish(new TaskBackgroundError(message.failure, message.exitCode, message.diagnostics));
|
|
57
109
|
});
|
|
58
110
|
signal.addEventListener("abort", aborted, { once: true });
|
|
59
111
|
child.send(job, error => { if (error)
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { redactSensitiveText } from "../../platform/redact.js";
|
|
2
2
|
import { TaskStoreError, taskScope } from "./store.js";
|
|
3
3
|
import { LocalFileError } from "../../platform/local-json-file.js";
|
|
4
|
+
import { ToolTaskFailedError } from "../tool/tool-api-adapter.js";
|
|
5
|
+
import { AgentAuditRejectedError } from "../agent/creative-protocol.js";
|
|
4
6
|
export class TaskNotFoundError extends Error {
|
|
5
7
|
constructor(message) { super(message); this.name = "TaskNotFoundError"; }
|
|
6
8
|
}
|
|
@@ -50,11 +52,14 @@ export function createTaskUseCases(dependencies) {
|
|
|
50
52
|
if (!telemetry || !record.telemetry)
|
|
51
53
|
return;
|
|
52
54
|
let context = record.telemetry.context;
|
|
53
|
-
if (name === "request_sent") {
|
|
55
|
+
if (name === "request_sent" && !record.telemetry.request_sent) {
|
|
54
56
|
const current = telemetry.creationContext();
|
|
55
57
|
context = { ...context,
|
|
56
58
|
...(current.tool_name ? { tool_name: current.tool_name } : {}),
|
|
57
59
|
...(current.model_id ? { model_id: current.model_id } : {}) };
|
|
60
|
+
// Fix the creation identity at dispatch even if the fact cannot be saved.
|
|
61
|
+
// Later completion and recovery must not use mutable command annotations.
|
|
62
|
+
record.telemetry.context = Object.freeze(context);
|
|
58
63
|
}
|
|
59
64
|
const candidate = { ...context, event_name: name, occurred_at_ms: at,
|
|
60
65
|
record_seq: name === "request_sent" ? 1 : 2 };
|
|
@@ -224,10 +229,16 @@ export function createTaskUseCases(dependencies) {
|
|
|
224
229
|
clearInterval(timer);
|
|
225
230
|
if (record.task.state !== "completed") {
|
|
226
231
|
const mayBeRunning = dispatched || record.progress.submissionStarted;
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
232
|
+
const auditRejected = error instanceof AgentAuditRejectedError;
|
|
233
|
+
const confirmedFailure = error instanceof ToolTaskFailedError || auditRejected;
|
|
234
|
+
record.task.state = confirmedFailure || !mayBeRunning ? "failed" : "unknown";
|
|
235
|
+
record.task.note = auditRejected
|
|
236
|
+
? text("内容未通过审核,本次创作已拒绝;具体错误见原命令。", "Content review rejected this creation. See the original command for the error.")
|
|
237
|
+
: confirmedFailure
|
|
238
|
+
? text("服务已明确返回本次 Tool 任务失败;具体错误见原命令。", "The service explicitly reported that this Tool task failed. See the original command for the error.")
|
|
239
|
+
: mayBeRunning
|
|
240
|
+
? 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.")
|
|
241
|
+
: text("CLI 在提交创作前结束;具体错误见原命令。", "The CLI stopped before submitting the creation. See the original command for the error.");
|
|
231
242
|
}
|
|
232
243
|
const detail = dependencies.failureNote?.(error);
|
|
233
244
|
if (detail)
|
|
@@ -174,8 +174,14 @@ function isJsonRpcError(value) {
|
|
|
174
174
|
|| !isRecord(value.error)) {
|
|
175
175
|
return false;
|
|
176
176
|
}
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
const code = value.error.code;
|
|
178
|
+
// Some Tool backends serialize JSON-RPC integer codes as decimal strings.
|
|
179
|
+
// Normalize only at this error boundary; malformed envelopes remain errors.
|
|
180
|
+
const numericCode = typeof code === "string" && /^-?(?:0|[1-9]\d*)$/u.test(code)
|
|
181
|
+
? Number(code)
|
|
182
|
+
: code;
|
|
183
|
+
return typeof numericCode === "number"
|
|
184
|
+
&& Number.isSafeInteger(numericCode)
|
|
179
185
|
&& typeof value.error.message === "string"
|
|
180
186
|
&& value.error.message.trim() !== "";
|
|
181
187
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
export class RemoteProtocolError extends Error {
|
|
2
2
|
remoteCode;
|
|
3
3
|
remoteBody;
|
|
4
|
-
|
|
4
|
+
diagnosticReason;
|
|
5
|
+
constructor(evidence = {}, cause, diagnosticReason) {
|
|
5
6
|
super("稿定服务响应与当前 CLI 不兼容。", { cause });
|
|
6
7
|
this.name = "RemoteProtocolError";
|
|
7
8
|
this.remoteCode = evidence.remoteCode;
|
|
8
9
|
this.remoteBody = evidence.remoteBody;
|
|
10
|
+
this.diagnosticReason = diagnosticReason;
|
|
9
11
|
}
|
|
10
12
|
}
|
|
@@ -7,13 +7,15 @@ export class RemoteRequestError extends Error {
|
|
|
7
7
|
transient;
|
|
8
8
|
remoteCode;
|
|
9
9
|
remoteBody;
|
|
10
|
-
|
|
10
|
+
diagnosticReason;
|
|
11
|
+
constructor(status, transient = false, evidence = {}, cause, diagnosticReason) {
|
|
11
12
|
super(status === undefined ? "无法连接稿定服务。" : `稿定服务请求失败(HTTP ${status})。`, { cause });
|
|
12
13
|
this.name = "RemoteRequestError";
|
|
13
14
|
this.status = status;
|
|
14
15
|
this.transient = transient;
|
|
15
16
|
this.remoteCode = evidence.remoteCode;
|
|
16
17
|
this.remoteBody = evidence.remoteBody;
|
|
18
|
+
this.diagnosticReason = diagnosticReason;
|
|
17
19
|
}
|
|
18
20
|
}
|
|
19
21
|
const TRANSIENT_NETWORK_CODES = new Set([
|
|
@@ -45,6 +47,7 @@ export function createSignedHttpTransport(options) {
|
|
|
45
47
|
const baseUrl = new URL(options.baseUrl);
|
|
46
48
|
if (!baseUrl.pathname.endsWith("/"))
|
|
47
49
|
baseUrl.pathname += "/";
|
|
50
|
+
const seenResponses = new WeakSet();
|
|
48
51
|
async function send(method, request, accept, body, timeout) {
|
|
49
52
|
request.signal.throwIfAborted();
|
|
50
53
|
const url = new URL(request.path.replace(/^\//, ""), baseUrl);
|
|
@@ -89,6 +92,16 @@ export function createSignedHttpTransport(options) {
|
|
|
89
92
|
headers,
|
|
90
93
|
...(body === undefined ? {} : { body }),
|
|
91
94
|
signal
|
|
95
|
+
}).then(response => {
|
|
96
|
+
// Capture ownership facts before waiting for unrelated dispatch diagnostics.
|
|
97
|
+
const received = {
|
|
98
|
+
response,
|
|
99
|
+
bodyUsedAtReceipt: response.bodyUsed,
|
|
100
|
+
bodyLockedAtReceipt: response.body?.locked ?? false,
|
|
101
|
+
reusedResponse: seenResponses.has(response)
|
|
102
|
+
};
|
|
103
|
+
seenResponses.add(response);
|
|
104
|
+
return received;
|
|
92
105
|
});
|
|
93
106
|
}
|
|
94
107
|
finally {
|
|
@@ -97,8 +110,8 @@ export function createSignedHttpTransport(options) {
|
|
|
97
110
|
}
|
|
98
111
|
catch { /* Diagnostic only. */ }
|
|
99
112
|
}
|
|
100
|
-
const [
|
|
101
|
-
return
|
|
113
|
+
const [received] = await Promise.all([pending, dispatched]);
|
|
114
|
+
return received;
|
|
102
115
|
}
|
|
103
116
|
catch (error) {
|
|
104
117
|
if (isInterruption(error, request.signal))
|
|
@@ -106,14 +119,29 @@ export function createSignedHttpTransport(options) {
|
|
|
106
119
|
throw new RemoteRequestError(undefined, isTransientNetworkFailure(error), {}, error);
|
|
107
120
|
}
|
|
108
121
|
}
|
|
109
|
-
async function
|
|
110
|
-
|
|
122
|
+
async function readText(received) {
|
|
123
|
+
const { response } = received;
|
|
124
|
+
const summary = {
|
|
125
|
+
bodyUsedAtReceipt: received.bodyUsedAtReceipt,
|
|
126
|
+
bodyLockedAtReceipt: received.bodyLockedAtReceipt,
|
|
127
|
+
bodyUsedBeforeRead: response.bodyUsed,
|
|
128
|
+
bodyLockedBeforeRead: response.body?.locked ?? false,
|
|
129
|
+
reusedResponse: received.reusedResponse
|
|
130
|
+
};
|
|
111
131
|
try {
|
|
112
|
-
|
|
132
|
+
return await response.text();
|
|
113
133
|
}
|
|
114
134
|
catch (error) {
|
|
115
|
-
|
|
135
|
+
const diagnosticReason = summary.bodyUsedBeforeRead ? "RESPONSE_BODY_ALREADY_USED"
|
|
136
|
+
: summary.bodyLockedBeforeRead ? "RESPONSE_BODY_LOCKED" : "RESPONSE_BODY_READ_FAILED";
|
|
137
|
+
throw new RemoteRequestError(response.status, false, {
|
|
138
|
+
remoteBody: JSON.stringify(summary)
|
|
139
|
+
}, error, diagnosticReason);
|
|
116
140
|
}
|
|
141
|
+
}
|
|
142
|
+
async function json(received) {
|
|
143
|
+
const { response } = received;
|
|
144
|
+
const body = await readText(received);
|
|
117
145
|
const evidence = remoteErrorEvidence(body);
|
|
118
146
|
if (!response.ok)
|
|
119
147
|
throw new RemoteRequestError(response.status, false, evidence);
|
|
@@ -121,12 +149,12 @@ export function createSignedHttpTransport(options) {
|
|
|
121
149
|
return JSON.parse(body);
|
|
122
150
|
}
|
|
123
151
|
catch (error) {
|
|
124
|
-
throw new RemoteProtocolError(evidence, error);
|
|
152
|
+
throw new RemoteProtocolError(evidence, error, "RESPONSE_JSON_INVALID");
|
|
125
153
|
}
|
|
126
154
|
}
|
|
127
155
|
async function getJsonResponse(request) {
|
|
128
|
-
const
|
|
129
|
-
return { body: await json(
|
|
156
|
+
const received = await send("GET", request, "application/json", undefined, true);
|
|
157
|
+
return { body: await json(received), headers: received.response.headers };
|
|
130
158
|
}
|
|
131
159
|
return {
|
|
132
160
|
async getJson(request) {
|
|
@@ -140,15 +168,10 @@ export function createSignedHttpTransport(options) {
|
|
|
140
168
|
return json(await send("PUT", request, "application/json", JSON.stringify(request.body), true));
|
|
141
169
|
},
|
|
142
170
|
async postStream(request) {
|
|
143
|
-
const
|
|
171
|
+
const received = await send("POST", request, request.accept, JSON.stringify(request.body), false);
|
|
172
|
+
const { response } = received;
|
|
144
173
|
if (!response.ok) {
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
body = await response.text();
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
throw new RemoteRequestError(response.status, false, {}, error);
|
|
151
|
-
}
|
|
174
|
+
const body = await readText(received);
|
|
152
175
|
throw new RemoteRequestError(response.status, false, remoteErrorEvidence(body));
|
|
153
176
|
}
|
|
154
177
|
if (!response.body)
|
|
@@ -33,10 +33,20 @@ export function createTelemetryInvocation(options) {
|
|
|
33
33
|
function errorFields(error) {
|
|
34
34
|
const c = classify(error);
|
|
35
35
|
return { ...errorEvidence(error),
|
|
36
|
+
...(c.errorType === undefined ? {} : { error_type: c.errorType }),
|
|
36
37
|
...(c.errorCode === undefined ? {} : { error_code: c.errorCode }),
|
|
37
38
|
...(c.httpStatus === undefined ? {} : { http_status: c.httpStatus }),
|
|
38
39
|
...(c.remoteErrorCode === undefined ? {} : { remote_error_code: c.remoteErrorCode }),
|
|
39
|
-
...(c.remoteErrorBody === undefined ? {} : { remote_error_body: c.remoteErrorBody })
|
|
40
|
+
...(c.remoteErrorBody === undefined ? {} : { remote_error_body: c.remoteErrorBody }),
|
|
41
|
+
...(c.diagnosticReason === undefined ? {} : { diagnostic_reason: c.diagnosticReason }) };
|
|
42
|
+
}
|
|
43
|
+
function failureStage(error) {
|
|
44
|
+
const causes = [];
|
|
45
|
+
for (let depth = 0; error !== undefined && depth < 10 && !causes.includes(error); depth++) {
|
|
46
|
+
causes.push(error);
|
|
47
|
+
error = error instanceof Error ? error.cause : undefined;
|
|
48
|
+
}
|
|
49
|
+
return failures.find(f => causes.includes(f.error))?.name;
|
|
40
50
|
}
|
|
41
51
|
function remember(event) {
|
|
42
52
|
const safe = cleanDiagnostic(event);
|
|
@@ -93,22 +103,15 @@ export function createTelemetryInvocation(options) {
|
|
|
93
103
|
throw error;
|
|
94
104
|
}
|
|
95
105
|
},
|
|
106
|
+
failureStage,
|
|
96
107
|
finish(completion) {
|
|
97
108
|
if (finished || !definition)
|
|
98
109
|
return [];
|
|
99
110
|
finished = true;
|
|
100
|
-
const causes = [];
|
|
101
|
-
if (completion.outcome !== "success") {
|
|
102
|
-
let error = completion.error;
|
|
103
|
-
for (let depth = 0; error !== undefined && depth < 10 && !causes.includes(error); depth++) {
|
|
104
|
-
causes.push(error);
|
|
105
|
-
error = error instanceof Error ? error.cause : undefined;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
111
|
remember({ ...base(), event_name: "command_finished", outcome: completion.outcome,
|
|
109
112
|
duration_ms: Math.max(0, options.now() - startedAt),
|
|
110
113
|
...(completion.outcome === "success" ? {} : {
|
|
111
|
-
failure_stage:
|
|
114
|
+
failure_stage: classify(completion.error).failureStage ?? failureStage(completion.error) ?? "unknown"
|
|
112
115
|
}),
|
|
113
116
|
...(completion.outcome === "failure" ? errorFields(completion.error) : completion.outcome === "interrupted" && completion.error !== undefined ? errorEvidence(completion.error) : {}),
|
|
114
117
|
...(completion.outcome === "failure" ? {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@insmind/cli",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.10",
|
|
4
4
|
"description": "insMind command-line interface for agents and people.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"@gaoding/cli": "workspace:*"
|
|
47
47
|
},
|
|
48
48
|
"gaodingRelease": {
|
|
49
|
-
"sourceCommit": "
|
|
50
|
-
"pipelineId": "
|
|
51
|
-
"pipelineUrl": "https://git.intra.gaoding.com/gtt/gaoding-cli/-/pipelines/
|
|
49
|
+
"sourceCommit": "8f232cb9bd2962a1581239c76a3731869f83dee3",
|
|
50
|
+
"pipelineId": "213536",
|
|
51
|
+
"pipelineUrl": "https://git.intra.gaoding.com/gtt/gaoding-cli/-/pipelines/213536"
|
|
52
52
|
}
|
|
53
53
|
}
|