@gaoding/cli 1.9.1 → 2.1.0
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/README.md +5 -0
- package/contracts/operations/credits.get/input.schema.json +9 -0
- package/contracts/operations/credits.get/output.schema.json +107 -0
- package/contracts/operations/credits.usage.get/input.schema.json +18 -0
- package/contracts/operations/credits.usage.get/output.schema.json +53 -0
- package/contracts/operations/credits.usage.list/input.schema.json +32 -0
- package/contracts/operations/credits.usage.list/output.schema.json +144 -0
- package/dist/bin/gd-cli.js +2 -2
- package/dist/bin/insmind-cli.js +2 -2
- package/dist/src/bootstrap/create-cli.js +28 -24
- package/dist/src/bootstrap/create-runtime.js +31 -18
- package/dist/src/bootstrap/run-task-worker.js +2 -1
- package/dist/src/bootstrap/validators.js +7 -2
- package/dist/src/cli/action-binding.js +42 -13
- package/dist/src/cli/agent-commands.js +8 -12
- package/dist/src/cli/credits-command.js +127 -16
- package/dist/src/cli/dam-commands.js +3 -2
- package/dist/src/cli/editor-commands.js +10 -5
- package/dist/src/cli/host-input.js +34 -0
- package/dist/src/cli/model-commands.js +4 -3
- package/dist/src/cli/presenter.js +4 -0
- package/dist/src/cli/tool-commands.js +7 -9
- package/dist/src/cli/update-command.js +24 -1
- package/dist/src/contracts/creation-input.js +20 -0
- package/dist/src/features/agent/creative-protocol.js +12 -14
- package/dist/src/features/agent/use-cases.js +6 -31
- package/dist/src/features/auth/access-policy.js +1 -3
- package/dist/src/features/auth/sso-service.js +6 -4
- package/dist/src/features/auth/use-cases.js +2 -1
- package/dist/src/features/credits/amount.js +51 -0
- package/dist/src/features/credits/response.js +11 -0
- package/dist/src/features/credits/use-cases.js +174 -31
- package/dist/src/features/dam/file-downloader.js +2 -1
- package/dist/src/features/dam/use-cases.js +7 -7
- package/dist/src/features/editor/bridge-client.js +1 -1
- package/dist/src/features/editor/use-cases.js +6 -8
- package/dist/src/features/org/use-cases.js +3 -3
- package/dist/src/features/skill/installer.js +24 -22
- package/dist/src/features/task/store.js +26 -12
- package/dist/src/features/task/use-cases.js +97 -82
- package/dist/src/features/tool/canvas-models.js +21 -4
- package/dist/src/features/tool/canvas-tool-adapter.js +29 -20
- package/dist/src/features/tool/canvas-video.js +2 -4
- package/dist/src/features/tool/dynamic-schema.js +29 -64
- package/dist/src/features/tool/mns-catalog-adapter.js +9 -2
- package/dist/src/features/tool/scene-catalog-adapter.js +0 -1
- package/dist/src/features/tool/tool-api-adapter.js +2 -2
- package/dist/src/features/tool/use-cases.js +24 -35
- package/dist/src/features/update/check-schema.js +17 -0
- package/dist/src/features/update/host-receipt.js +30 -0
- package/dist/src/features/update/notice-output.js +30 -0
- package/dist/src/features/update/update-lock.js +80 -0
- package/dist/src/features/update/update-notification.js +129 -240
- package/dist/src/platform/json-input.js +1 -1
- package/dist/src/platform/safe-upload-file.js +6 -0
- package/dist/src/platform/signature.js +3 -1
- package/dist/src/platform/signed-http-transport.js +34 -32
- package/dist/src/telemetry/host-diagnostics.js +2 -2
- package/dist/src/telemetry/invocation.js +87 -24
- package/dist/src/telemetry/process-errors.js +36 -3
- package/dist/src/telemetry/sls-sink.js +7 -10
- package/package.json +4 -4
- package/skills/gd-cli/SKILL.md +2 -0
- package/skills/gd-cli/references/editor.md +1 -1
- package/skills/gd-cli/references/generation-constraints.md +3 -1
- package/skills/gd-cli/references/tool.md +2 -3
- package/skills/gd-cli/references/update.md +6 -0
|
@@ -1,122 +1,151 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import * as fileSystem from "node:fs/promises";
|
|
3
1
|
import { homedir } from "node:os";
|
|
4
2
|
import { join } from "node:path";
|
|
5
3
|
import { gt, valid } from "semver";
|
|
6
4
|
import { createLocalJsonFile } from "../../platform/local-json-file.js";
|
|
7
5
|
import { fetchLatestVersion } from "./latest-version.js";
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const STALE_LOCK_MS = 30_000;
|
|
6
|
+
import { createLocalUpdateNotificationLock } from "./update-lock.js";
|
|
7
|
+
const HOUR = 60 * 60 * 1_000;
|
|
8
|
+
const RETRY = 15 * 60 * 1_000;
|
|
12
9
|
export function createUpdateNotificationService(options) {
|
|
13
|
-
const
|
|
10
|
+
const { currentVersion } = options;
|
|
11
|
+
const command = options.commandName ?? "gd-cli";
|
|
14
12
|
const now = options.now ?? (() => new Date());
|
|
15
13
|
const env = options.env ?? process.env;
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
const directory = join(options.homeDirectory ?? homedir(), options.stateDirectoryName ?? ".gd");
|
|
15
|
+
const file = options.stateFile ?? createLocalJsonFile({ directory, filename: "update-check.json" });
|
|
16
|
+
const lock = options.lock ?? createLocalUpdateNotificationLock({ directory, now: () => new Date() });
|
|
17
|
+
const host = env.CLI_HOST_APPLICATION?.toLowerCase();
|
|
18
|
+
const surface = host && /^[a-z0-9_-]{1,48}$/.test(host) ? host : "terminal";
|
|
19
|
+
const unknown = { status: "unknown", currentVersion };
|
|
20
|
+
async function refresh(state, time, signal) {
|
|
21
|
+
if (Date.parse(state.next_check_at ?? "") > time.getTime())
|
|
22
|
+
return state;
|
|
23
|
+
const timeout = signalWithTimeout(signal, 2_000);
|
|
24
|
+
try {
|
|
25
|
+
const fetchLatest = () => fetchLatestVersion({
|
|
26
|
+
fetch: options.fetch ?? globalThis.fetch, signal: timeout.signal,
|
|
27
|
+
...(options.registryUrl ? { registryUrl: options.registryUrl } : {}),
|
|
28
|
+
...(state.etag ? { etag: state.etag } : {})
|
|
29
|
+
});
|
|
30
|
+
const result = options.telemetry ? await options.telemetry.stage("update_check.registry", fetchLatest) : await fetchLatest();
|
|
31
|
+
const latest = result.status === "modified" ? result.version : state.latest_version;
|
|
32
|
+
if (!latest || !valid(latest))
|
|
33
|
+
throw new Error("Missing cached Registry version");
|
|
34
|
+
const next = { ...state, latest_version: latest, last_success_check_at: time.toISOString(),
|
|
35
|
+
next_check_at: new Date(time.getTime() + HOUR).toISOString() };
|
|
36
|
+
if (result.status === "modified") {
|
|
37
|
+
if (result.etag)
|
|
38
|
+
next.etag = result.etag;
|
|
39
|
+
else
|
|
40
|
+
delete next.etag;
|
|
41
|
+
}
|
|
42
|
+
return next;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
signal.throwIfAborted();
|
|
46
|
+
// A failed refresh cannot establish what the latest version is.
|
|
47
|
+
const { latest_version, etag, ...prior } = state;
|
|
48
|
+
return { ...prior, next_check_at: new Date(time.getTime() + RETRY).toISOString() };
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
timeout.dispose();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function report(signal, channel, deliver, remember = true) {
|
|
55
|
+
if (options.installationOwner === "workbuddy")
|
|
56
|
+
return deliver({ status: "managed", currentVersion });
|
|
57
|
+
if (options.installationOwner === "invalid" || env.CI === "true")
|
|
58
|
+
return deliver(unknown);
|
|
59
|
+
const release = await lock.acquire().catch(() => null);
|
|
60
|
+
if (!release)
|
|
61
|
+
return deliver(unknown);
|
|
62
|
+
try {
|
|
63
|
+
let state;
|
|
64
|
+
try {
|
|
65
|
+
signal.throwIfAborted();
|
|
66
|
+
const prior = await readState(file);
|
|
67
|
+
state = await refresh(prior, now(), signal);
|
|
68
|
+
if (state !== prior)
|
|
69
|
+
await save(file, state);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
signal.throwIfAborted();
|
|
73
|
+
return await deliver(unknown);
|
|
74
|
+
}
|
|
75
|
+
const latest = valid(state.latest_version);
|
|
76
|
+
if (!latest)
|
|
77
|
+
return await deliver(unknown);
|
|
78
|
+
if (!gt(latest, currentVersion))
|
|
79
|
+
return await deliver({ status: "current", currentVersion, latestVersion: latest });
|
|
80
|
+
const available = { status: "available", currentVersion, latestVersion: latest, updateCommand: command + " update" };
|
|
81
|
+
const windowStart = now().getTime() - HOUR;
|
|
82
|
+
const recent = Object.fromEntries(Object.entries(state.notified_at ?? {}).filter(([, at]) => at > windowStart));
|
|
83
|
+
if (!channel || Object.hasOwn(recent, channel))
|
|
84
|
+
return await deliver(available);
|
|
85
|
+
const notice = command === "gd-cli"
|
|
86
|
+
? `gd-cli 有新版本 ${latest},当前版本为 ${currentVersion}。运行 gd-cli update 即可更新。`
|
|
87
|
+
: `${command} ${latest} is available; current version is ${currentVersion}. Run ${command} update to update.`;
|
|
88
|
+
await deliver({ ...available, notice });
|
|
89
|
+
if (remember)
|
|
90
|
+
await save(file, { ...state, notified_at: { ...recent, [channel]: now().getTime() } }).catch(() => undefined);
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
await release().catch(() => undefined);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
25
96
|
return {
|
|
26
|
-
async check(signal) {
|
|
27
|
-
if (
|
|
97
|
+
async check(signal, output = "machine") {
|
|
98
|
+
if (env.CI === "true" || env.CLI_INVOCATION_PURPOSE === "auth_status" || env.CLI_INVOCATION_PURPOSE === "version_check")
|
|
28
99
|
return;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
100
|
+
// An unobserved stderr pipe must not spend the next visible text/Skill reminder.
|
|
101
|
+
const channel = output === "machine" && options.diagnosticOnly ? "stderr:" + surface : "display:" + surface;
|
|
102
|
+
const run = () => report(signal, channel, async (result) => {
|
|
103
|
+
if (result.status === "available" && result.notice)
|
|
104
|
+
await options.notify(result.notice, output);
|
|
105
|
+
}, env.CLI_UPDATE_NOTICE_FORMAT !== "offer");
|
|
32
106
|
try {
|
|
33
|
-
const run = async () => {
|
|
34
|
-
signal.throwIfAborted();
|
|
35
|
-
lease.release = await lock.acquire();
|
|
36
|
-
if (lease.release === null)
|
|
37
|
-
return;
|
|
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 = {
|
|
86
|
-
...state,
|
|
87
|
-
schema_version: 1,
|
|
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);
|
|
105
|
-
};
|
|
106
107
|
if (options.telemetry)
|
|
107
108
|
await options.telemetry.stage("update_check", run);
|
|
108
109
|
else
|
|
109
110
|
await run();
|
|
110
111
|
}
|
|
111
|
-
catch
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
await lease.release?.().catch(() => undefined);
|
|
116
|
-
}
|
|
112
|
+
catch { /* Optional notices never change a business command's result. */ }
|
|
113
|
+
},
|
|
114
|
+
report(signal, forDisplay, deliver) {
|
|
115
|
+
return report(signal, forDisplay ? "display:" + surface : undefined, deliver);
|
|
117
116
|
}
|
|
118
117
|
};
|
|
119
118
|
}
|
|
119
|
+
async function readState(file) {
|
|
120
|
+
const empty = { schema_version: 1 };
|
|
121
|
+
const content = await file.readText();
|
|
122
|
+
if (!content)
|
|
123
|
+
return empty;
|
|
124
|
+
try {
|
|
125
|
+
const value = JSON.parse(content);
|
|
126
|
+
if (!isRecord(value) || value.schema_version !== 1)
|
|
127
|
+
return empty;
|
|
128
|
+
const state = { ...empty };
|
|
129
|
+
for (const key of ["next_check_at", "latest_version", "etag", "last_success_check_at"]) {
|
|
130
|
+
if (typeof value[key] === "string")
|
|
131
|
+
state[key] = value[key];
|
|
132
|
+
}
|
|
133
|
+
// v1 date-only reminders have no time of day. Start the rolling window on first delivery.
|
|
134
|
+
if (isRecord(value.notified_at)) {
|
|
135
|
+
state.notified_at = Object.fromEntries(Object.entries(value.notified_at).filter((entry) => typeof entry[1] === "number" && Number.isFinite(entry[1])));
|
|
136
|
+
}
|
|
137
|
+
return state;
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
return empty;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function isRecord(value) {
|
|
144
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
145
|
+
}
|
|
146
|
+
function save(file, state) {
|
|
147
|
+
return file.writeTextAtomically(JSON.stringify(state, null, 2) + "\n");
|
|
148
|
+
}
|
|
120
149
|
function signalWithTimeout(parent, milliseconds) {
|
|
121
150
|
const controller = new AbortController();
|
|
122
151
|
const forwardAbort = () => { controller.abort(parent.reason); };
|
|
@@ -136,143 +165,3 @@ function signalWithTimeout(parent, milliseconds) {
|
|
|
136
165
|
}
|
|
137
166
|
};
|
|
138
167
|
}
|
|
139
|
-
function createLocalUpdateNotificationLock(options) {
|
|
140
|
-
const path = join(options.directory, "update-check.lock");
|
|
141
|
-
return {
|
|
142
|
-
async acquire() {
|
|
143
|
-
await fileSystem.mkdir(options.directory, { mode: 0o700, recursive: true });
|
|
144
|
-
await fileSystem.chmod(options.directory, 0o700);
|
|
145
|
-
const first = await tryCreateLock(path);
|
|
146
|
-
if (first)
|
|
147
|
-
return releaseLock(path, first);
|
|
148
|
-
if (!await isStaleLock(path, options.now()))
|
|
149
|
-
return null;
|
|
150
|
-
await fileSystem.unlink(path).catch((error) => {
|
|
151
|
-
if (!isMissing(error))
|
|
152
|
-
throw error;
|
|
153
|
-
});
|
|
154
|
-
const recovered = await tryCreateLock(path);
|
|
155
|
-
return recovered ? releaseLock(path, recovered) : null;
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
async function tryCreateLock(path) {
|
|
160
|
-
const token = randomUUID();
|
|
161
|
-
let handle;
|
|
162
|
-
try {
|
|
163
|
-
handle = await fileSystem.open(path, "wx", 0o600);
|
|
164
|
-
await handle.writeFile(token, "utf8");
|
|
165
|
-
await handle.sync();
|
|
166
|
-
await handle.close();
|
|
167
|
-
return token;
|
|
168
|
-
}
|
|
169
|
-
catch (error) {
|
|
170
|
-
await handle?.close().catch(() => undefined);
|
|
171
|
-
if (isExists(error))
|
|
172
|
-
return null;
|
|
173
|
-
throw error;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
function releaseLock(path, token) {
|
|
177
|
-
return async () => {
|
|
178
|
-
let current;
|
|
179
|
-
try {
|
|
180
|
-
current = await fileSystem.readFile(path, "utf8");
|
|
181
|
-
}
|
|
182
|
-
catch (error) {
|
|
183
|
-
if (isMissing(error))
|
|
184
|
-
return;
|
|
185
|
-
throw error;
|
|
186
|
-
}
|
|
187
|
-
if (current === token)
|
|
188
|
-
await fileSystem.unlink(path);
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
async function isStaleLock(path, now) {
|
|
192
|
-
try {
|
|
193
|
-
const stats = await fileSystem.stat(path);
|
|
194
|
-
return stats.mtimeMs + STALE_LOCK_MS <= now.getTime();
|
|
195
|
-
}
|
|
196
|
-
catch (error) {
|
|
197
|
-
if (isMissing(error))
|
|
198
|
-
return true;
|
|
199
|
-
throw error;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
async function readState(file) {
|
|
203
|
-
const text = await file.readText();
|
|
204
|
-
if (text === null)
|
|
205
|
-
return { schema_version: 1 };
|
|
206
|
-
try {
|
|
207
|
-
const value = JSON.parse(text);
|
|
208
|
-
return parseState(value) ?? { schema_version: 1 };
|
|
209
|
-
}
|
|
210
|
-
catch {
|
|
211
|
-
return { schema_version: 1 };
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
function parseState(value) {
|
|
215
|
-
if (!isRecord(value) || value.schema_version !== 1)
|
|
216
|
-
return null;
|
|
217
|
-
for (const key of [
|
|
218
|
-
"last_success_check_at",
|
|
219
|
-
"next_check_at",
|
|
220
|
-
"latest_version",
|
|
221
|
-
"last_notified_on",
|
|
222
|
-
"last_notified_version",
|
|
223
|
-
"etag"
|
|
224
|
-
]) {
|
|
225
|
-
if (value[key] !== undefined && typeof value[key] !== "string")
|
|
226
|
-
return null;
|
|
227
|
-
}
|
|
228
|
-
return value;
|
|
229
|
-
}
|
|
230
|
-
async function writeState(file, state) {
|
|
231
|
-
await file.writeTextAtomically(`${JSON.stringify(state, null, 2)}\n`);
|
|
232
|
-
}
|
|
233
|
-
function withReminder(state, today, version) {
|
|
234
|
-
return {
|
|
235
|
-
...state,
|
|
236
|
-
schema_version: 1,
|
|
237
|
-
last_notified_on: today,
|
|
238
|
-
last_notified_version: version
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
function notify(options, latest) {
|
|
242
|
-
if (options.commandName && options.commandName !== "gd-cli") {
|
|
243
|
-
options.notify(`${options.commandName} ${latest} is available; current version is ` +
|
|
244
|
-
`${options.currentVersion}. Run ${options.commandName} update to update.`);
|
|
245
|
-
}
|
|
246
|
-
else {
|
|
247
|
-
options.notify(`gd-cli 有新版本 ${latest},当前版本为 ${options.currentVersion}。` +
|
|
248
|
-
"运行 gd-cli update 即可更新。");
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
function isNewer(version, currentVersion) {
|
|
252
|
-
return version !== undefined && valid(version) !== null && gt(version, currentVersion);
|
|
253
|
-
}
|
|
254
|
-
function isDue(nextCheckAt, now) {
|
|
255
|
-
if (nextCheckAt === undefined)
|
|
256
|
-
return true;
|
|
257
|
-
const timestamp = Date.parse(nextCheckAt);
|
|
258
|
-
return !Number.isFinite(timestamp) || timestamp <= now.getTime();
|
|
259
|
-
}
|
|
260
|
-
function addMilliseconds(value, milliseconds) {
|
|
261
|
-
return new Date(value.getTime() + milliseconds).toISOString();
|
|
262
|
-
}
|
|
263
|
-
function localDate(value) {
|
|
264
|
-
return [
|
|
265
|
-
String(value.getFullYear()).padStart(4, "0"),
|
|
266
|
-
String(value.getMonth() + 1).padStart(2, "0"),
|
|
267
|
-
String(value.getDate()).padStart(2, "0")
|
|
268
|
-
].join("-");
|
|
269
|
-
}
|
|
270
|
-
function isRecord(value) {
|
|
271
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
272
|
-
}
|
|
273
|
-
function isMissing(error) {
|
|
274
|
-
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
275
|
-
}
|
|
276
|
-
function isExists(error) {
|
|
277
|
-
return error instanceof Error && "code" in error && error.code === "EEXIST";
|
|
278
|
-
}
|
|
@@ -86,6 +86,12 @@ function detectedFormat(header) {
|
|
|
86
86
|
return "webp";
|
|
87
87
|
if (header.length >= 12 && header.subarray(4, 8).toString("ascii") === "ftyp") {
|
|
88
88
|
const brand = header.subarray(8, 12).toString("ascii");
|
|
89
|
+
if (["isom", "iso2", "iso5", "iso6", "mp41", "mp42", "avc1", "dash"].includes(brand))
|
|
90
|
+
return "mp4";
|
|
91
|
+
if (brand === "qt ")
|
|
92
|
+
return "mov";
|
|
93
|
+
if (brand === "M4V ")
|
|
94
|
+
return "m4v";
|
|
89
95
|
if (brand === "avif" || brand === "avis")
|
|
90
96
|
return "avif";
|
|
91
97
|
if (["heic", "heix", "hevc", "hevx"].includes(brand))
|
|
@@ -7,7 +7,9 @@ export function canonicalizeQuery(entries) {
|
|
|
7
7
|
.replaceAll("%3A", ":");
|
|
8
8
|
}
|
|
9
9
|
export function createSigningString(input) {
|
|
10
|
-
|
|
10
|
+
// The gateway signs literal base64 padding; keep its wire query URL-encoded.
|
|
11
|
+
const signingQuery = input.canonicalQuery.replaceAll("%3D", "=");
|
|
12
|
+
return `${input.method.toUpperCase()}@${input.pathname}@${signingQuery}@${input.timestamp}`;
|
|
11
13
|
}
|
|
12
14
|
export function signRequest(input) {
|
|
13
15
|
return hmac(input.secretKey, createSigningString(input));
|
|
@@ -131,40 +131,37 @@ export function createSignedHttpTransport(options) {
|
|
|
131
131
|
let dispatched;
|
|
132
132
|
try {
|
|
133
133
|
// Invoke fetch at this boundary; diagnostic persistence never gates dispatch.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
};
|
|
150
|
-
seenResponses.add(response);
|
|
151
|
-
try {
|
|
152
|
-
request.onResponse?.(response.status, received.diagnostics());
|
|
153
|
-
}
|
|
154
|
-
catch { /* Receipt diagnostics only. */ }
|
|
155
|
-
return received;
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
finally {
|
|
134
|
+
const pending = options.fetch(url, {
|
|
135
|
+
method,
|
|
136
|
+
headers,
|
|
137
|
+
...(body === undefined ? {} : { body }),
|
|
138
|
+
signal
|
|
139
|
+
}).then(response => {
|
|
140
|
+
// Capture ownership facts before waiting for unrelated dispatch diagnostics.
|
|
141
|
+
const received = {
|
|
142
|
+
response,
|
|
143
|
+
bodyUsedAtReceipt: response.bodyUsed,
|
|
144
|
+
bodyLockedAtReceipt: response.body?.locked ?? false,
|
|
145
|
+
reusedResponse: seenResponses.has(response),
|
|
146
|
+
diagnostics: () => ({ ...diagnostics(), ...responseDiagnostics(response) })
|
|
147
|
+
};
|
|
148
|
+
seenResponses.add(response);
|
|
159
149
|
try {
|
|
160
|
-
|
|
150
|
+
request.onResponse?.(response.status, received.diagnostics());
|
|
161
151
|
}
|
|
162
|
-
catch { /*
|
|
152
|
+
catch { /* Receipt diagnostics only. */ }
|
|
153
|
+
return received;
|
|
154
|
+
});
|
|
155
|
+
try {
|
|
156
|
+
dispatched = request.onDispatch?.(options.now().getTime()).catch(() => undefined);
|
|
163
157
|
}
|
|
158
|
+
catch { /* Diagnostic only. */ }
|
|
164
159
|
const [received] = await Promise.all([pending, dispatched]);
|
|
165
160
|
return received;
|
|
166
161
|
}
|
|
167
162
|
catch (error) {
|
|
163
|
+
// A rejected fetch must not overtake persistence of the dispatch that caused it.
|
|
164
|
+
await dispatched;
|
|
168
165
|
const diagnosticSummary = diagnostics();
|
|
169
166
|
if (isInterruption(error, request.signal)) {
|
|
170
167
|
const reason = request.signal.reason;
|
|
@@ -198,24 +195,29 @@ export function createSignedHttpTransport(options) {
|
|
|
198
195
|
}, error, diagnosticReason, { ...received.diagnostics(), ...summary });
|
|
199
196
|
}
|
|
200
197
|
}
|
|
201
|
-
async function json(received) {
|
|
198
|
+
async function json(received, sensitiveResponse = false, parseJson = JSON.parse) {
|
|
202
199
|
const { response } = received;
|
|
203
200
|
const body = await readText(received);
|
|
204
|
-
const
|
|
201
|
+
const originalEvidence = remoteErrorEvidence(body);
|
|
202
|
+
const evidence = sensitiveResponse ? {
|
|
203
|
+
...(originalEvidence.remoteCode === undefined ? {} : { remoteCode: originalEvidence.remoteCode }),
|
|
204
|
+
...(originalEvidence.diagnosticReason === undefined ? {} : { diagnosticReason: originalEvidence.diagnosticReason })
|
|
205
|
+
} : originalEvidence;
|
|
205
206
|
if (!response.ok)
|
|
206
207
|
throw new RemoteRequestError(response.status, false, evidence, undefined, undefined, received.diagnostics());
|
|
207
208
|
try {
|
|
208
|
-
return
|
|
209
|
+
return parseJson(body);
|
|
209
210
|
}
|
|
210
211
|
catch (error) {
|
|
211
|
-
|
|
212
|
+
// SyntaxError messages can embed raw JSON fragments, including private ledger data.
|
|
213
|
+
throw Object.assign(new RemoteProtocolError(evidence, sensitiveResponse ? undefined : error, "RESPONSE_JSON_INVALID"), {
|
|
212
214
|
status: response.status, diagnosticSummary: received.diagnostics()
|
|
213
215
|
});
|
|
214
216
|
}
|
|
215
217
|
}
|
|
216
218
|
async function getJsonResponse(request) {
|
|
217
219
|
const received = await send("GET", request, "application/json", undefined, true);
|
|
218
|
-
return { body: await json(received), headers: received.response.headers, status: received.response.status };
|
|
220
|
+
return { body: await json(received, request.sensitiveResponse, request.parseJson), headers: received.response.headers, status: received.response.status };
|
|
219
221
|
}
|
|
220
222
|
return {
|
|
221
223
|
async getJson(request) {
|
|
@@ -17,7 +17,7 @@ export async function runHostDiagnosticReporter(options) {
|
|
|
17
17
|
output.on("error", () => { });
|
|
18
18
|
const sink = createSlsTelemetrySink({ fetch: options.fetch ?? globalThis.fetch,
|
|
19
19
|
trackingUrl: options.trackingUrl ?? options.product.telemetryUrl,
|
|
20
|
-
directory: join(options.homeDirectory ?? homedir(), options.product.stateDirectory, "telemetry") });
|
|
20
|
+
directory: join(options.homeDirectory ?? homedir(), options.product.stateDirectory, "telemetry", "v6") });
|
|
21
21
|
const cliVersion = options.cliVersion ?? await packageVersion();
|
|
22
22
|
// A wrapper may be killed with stdin still open. This helper is always bounded;
|
|
23
23
|
// queued start evidence survives, and no permanent background process is added.
|
|
@@ -55,7 +55,7 @@ export async function runHostDiagnosticReporter(options) {
|
|
|
55
55
|
return;
|
|
56
56
|
if (typeof raw.occurred_at_ms !== "number" || !Number.isSafeInteger(raw.occurred_at_ms) || raw.occurred_at_ms < 0)
|
|
57
57
|
return;
|
|
58
|
-
const event = { ...context, schema_version: "
|
|
58
|
+
const event = { ...context, schema_version: "6", started_at_ms: raw.occurred_at_ms, command_kind: "technical", cli_name: options.product.commandName,
|
|
59
59
|
cli_version: cliVersion, env: env.CLI_TELEMETRY_ENV === "test" ? "test" : "production",
|
|
60
60
|
event_name: "host_diagnostic", process_role: "host_connector", trace_id: context.host_invocation_id,
|
|
61
61
|
record_seq: ++sequence, occurred_at_ms: raw.occurred_at_ms,
|