@neta-art/cohub-cli 10.23.0 → 10.25.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 +18 -5
- package/bin/cohub.js +4 -1
- package/dist/board-gpu/page.js +1566 -71230
- package/dist/bootstrap.js +67 -27
- package/dist/build.json +1 -1
- package/dist/{board-kernel/FilterPipe.js → chunks/Geometry.js} +10447 -10469
- package/dist/chunks/about.js +102 -0
- package/dist/chunks/app-realtime.js +2216 -0
- package/dist/chunks/app-realtime2.js +2 -0
- package/dist/chunks/app-ref.js +88 -0
- package/dist/chunks/apps.js +1423 -0
- package/dist/chunks/auth.js +175 -0
- package/dist/chunks/avatar.js +146 -0
- package/dist/chunks/board-export-resources.js +232 -0
- package/dist/chunks/board-export.js +874 -0
- package/dist/chunks/board-export2.js +2 -0
- package/dist/{board-kernel.js → chunks/board-kernel.js} +18032 -28512
- package/dist/chunks/board-kernel2.js +4 -0
- package/dist/chunks/board-profile.js +69 -0
- package/dist/chunks/board-simulate.js +315 -0
- package/dist/chunks/board-time.js +34 -0
- package/dist/chunks/boards.js +1943 -0
- package/dist/{board-kernel → chunks}/browserAll.js +9 -8
- package/dist/chunks/channels.js +64 -0
- package/dist/chunks/client.js +12606 -0
- package/dist/chunks/command-input.js +17 -0
- package/dist/chunks/commander.js +2962 -0
- package/dist/chunks/cron-jobs.js +163 -0
- package/dist/chunks/daemon.js +545 -0
- package/dist/chunks/daemon2.js +2 -0
- package/dist/chunks/desktop.js +172 -0
- package/dist/chunks/endpoint-options.js +26 -0
- package/dist/chunks/env-proxy.js +35 -0
- package/dist/chunks/environment.js +154 -0
- package/dist/chunks/feedback.js +120 -0
- package/dist/chunks/generations.js +379 -0
- package/dist/chunks/host.js +9 -0
- package/dist/chunks/http-put.js +131 -0
- package/dist/chunks/id.js +2416 -0
- package/dist/{board-kernel → chunks}/init.js +1 -1
- package/dist/chunks/install.js +11959 -0
- package/dist/chunks/launch.js +374 -0
- package/dist/chunks/local-media.js +47 -0
- package/dist/chunks/me.js +134 -0
- package/dist/chunks/media.js +315 -0
- package/dist/chunks/models.js +242 -0
- package/dist/chunks/operands.js +46 -0
- package/dist/chunks/package-paths.js +13 -0
- package/dist/chunks/policy.js +221 -0
- package/dist/chunks/preferences.js +29 -0
- package/dist/chunks/profile.js +64 -0
- package/dist/chunks/prompts.js +41 -0
- package/dist/chunks/public.js +2 -0
- package/dist/chunks/public2.js +244 -0
- package/dist/chunks/references.js +197 -0
- package/dist/chunks/referrals.js +90 -0
- package/dist/chunks/rolldown-runtime.js +26 -0
- package/dist/chunks/rooms-connect.js +2 -0
- package/dist/chunks/rooms-connect2.js +151 -0
- package/dist/chunks/rooms-rtc.js +309 -0
- package/dist/chunks/rooms.js +211 -0
- package/dist/chunks/run.js +220 -0
- package/dist/chunks/runtime.js +638 -0
- package/dist/chunks/runtime2.js +286 -0
- package/dist/chunks/runtimes.js +48 -0
- package/dist/chunks/safe-remote-image.js +198 -0
- package/dist/chunks/schemas.js +6017 -0
- package/dist/chunks/search.js +113 -0
- package/dist/chunks/self-update.js +238 -0
- package/dist/chunks/serve.js +1246 -0
- package/dist/chunks/session-status.js +24 -0
- package/dist/chunks/skills.js +35 -0
- package/dist/chunks/spaces.js +5080 -0
- package/dist/chunks/src.js +405 -0
- package/dist/chunks/supervisor.js +2511 -0
- package/dist/chunks/supervisor2.js +2 -0
- package/dist/chunks/tasks.js +82 -0
- package/dist/chunks/themes.js +2898 -0
- package/dist/chunks/version.js +2 -0
- package/dist/chunks/version2.js +22 -0
- package/dist/chunks/version3.js +18 -0
- package/dist/chunks/web.js +7 -0
- package/dist/{board-kernel → chunks}/webworkerAll.js +1 -1
- package/dist/runtime/native/pi-extension.js +174 -166
- package/dist/runtime/worker.js +27 -16
- package/package.json +8 -9
- package/dist/about.d.ts +0 -2
- package/dist/about.js +0 -71
- package/dist/app-download.d.ts +0 -11
- package/dist/app-download.js +0 -252
- package/dist/app-ref.d.ts +0 -5
- package/dist/app-ref.js +0 -8
- package/dist/app-target.d.ts +0 -26
- package/dist/app-target.js +0 -55
- package/dist/auth.d.ts +0 -45
- package/dist/auth.js +0 -307
- package/dist/avatar.d.ts +0 -55
- package/dist/avatar.js +0 -104
- package/dist/board-command-support.d.ts +0 -6
- package/dist/board-command-support.js +0 -88
- package/dist/board-export-resources.d.ts +0 -12
- package/dist/board-export-resources.js +0 -51
- package/dist/board-export.d.ts +0 -110
- package/dist/board-export.js +0 -592
- package/dist/board-fonts.d.ts +0 -11
- package/dist/board-fonts.js +0 -10
- package/dist/board-gpu/daemon.d.ts +0 -11
- package/dist/board-gpu/daemon.js +0 -295
- package/dist/board-gpu/index.d.ts +0 -33
- package/dist/board-gpu/index.js +0 -189
- package/dist/board-gpu/protocol.d.ts +0 -77
- package/dist/board-gpu/protocol.js +0 -1
- package/dist/board-kernel.d.ts +0 -5
- package/dist/board-media-export.d.ts +0 -17
- package/dist/board-media-export.js +0 -164
- package/dist/board-media.d.ts +0 -5
- package/dist/board-media.js +0 -66
- package/dist/board-profile.d.ts +0 -21
- package/dist/board-profile.js +0 -67
- package/dist/board-simulate.d.ts +0 -67
- package/dist/board-simulate.js +0 -259
- package/dist/board-time.d.ts +0 -3
- package/dist/board-time.js +0 -34
- package/dist/bootstrap.d.ts +0 -2
- package/dist/client.d.ts +0 -5
- package/dist/client.js +0 -24
- package/dist/command-input.d.ts +0 -4
- package/dist/command-input.js +0 -16
- package/dist/commands/app-commerce.d.ts +0 -2
- package/dist/commands/app-commerce.js +0 -228
- package/dist/commands/app-domains.d.ts +0 -2
- package/dist/commands/app-domains.js +0 -133
- package/dist/commands/apps.d.ts +0 -4
- package/dist/commands/apps.js +0 -827
- package/dist/commands/auth.d.ts +0 -2
- package/dist/commands/auth.js +0 -186
- package/dist/commands/boards/authoring.d.ts +0 -2
- package/dist/commands/boards/authoring.js +0 -166
- package/dist/commands/boards/examples.d.ts +0 -10
- package/dist/commands/boards/examples.js +0 -118
- package/dist/commands/boards.d.ts +0 -13
- package/dist/commands/boards.js +0 -836
- package/dist/commands/channels.d.ts +0 -2
- package/dist/commands/channels.js +0 -74
- package/dist/commands/cron-jobs.d.ts +0 -2
- package/dist/commands/cron-jobs.js +0 -155
- package/dist/commands/decision.d.ts +0 -4
- package/dist/commands/decision.js +0 -113
- package/dist/commands/desktop.d.ts +0 -10
- package/dist/commands/desktop.js +0 -233
- package/dist/commands/feedback.d.ts +0 -2
- package/dist/commands/feedback.js +0 -131
- package/dist/commands/generations.d.ts +0 -2
- package/dist/commands/generations.js +0 -427
- package/dist/commands/host.d.ts +0 -2
- package/dist/commands/host.js +0 -11
- package/dist/commands/me.d.ts +0 -2
- package/dist/commands/me.js +0 -118
- package/dist/commands/models.d.ts +0 -28
- package/dist/commands/models.js +0 -271
- package/dist/commands/profile.d.ts +0 -2
- package/dist/commands/profile.js +0 -81
- package/dist/commands/prompts.d.ts +0 -2
- package/dist/commands/prompts.js +0 -37
- package/dist/commands/public.d.ts +0 -20
- package/dist/commands/public.js +0 -291
- package/dist/commands/references.d.ts +0 -2
- package/dist/commands/references.js +0 -193
- package/dist/commands/referrals.d.ts +0 -2
- package/dist/commands/referrals.js +0 -88
- package/dist/commands/rooms-connect.d.ts +0 -17
- package/dist/commands/rooms-connect.js +0 -140
- package/dist/commands/rooms-rtc-binary.d.ts +0 -2
- package/dist/commands/rooms-rtc-binary.js +0 -107
- package/dist/commands/rooms-rtc.d.ts +0 -21
- package/dist/commands/rooms-rtc.js +0 -171
- package/dist/commands/rooms.d.ts +0 -3
- package/dist/commands/rooms.js +0 -277
- package/dist/commands/run.d.ts +0 -13
- package/dist/commands/run.js +0 -246
- package/dist/commands/runtime.d.ts +0 -3
- package/dist/commands/runtime.js +0 -320
- package/dist/commands/sandboxd-binary.d.ts +0 -11
- package/dist/commands/sandboxd-binary.js +0 -238
- package/dist/commands/search.d.ts +0 -2
- package/dist/commands/search.js +0 -114
- package/dist/commands/session-access.d.ts +0 -2
- package/dist/commands/session-access.js +0 -70
- package/dist/commands/skills.d.ts +0 -2
- package/dist/commands/skills.js +0 -34
- package/dist/commands/space-activity.d.ts +0 -26
- package/dist/commands/space-activity.js +0 -147
- package/dist/commands/space-commerce.d.ts +0 -2
- package/dist/commands/space-commerce.js +0 -514
- package/dist/commands/space-displays.d.ts +0 -2
- package/dist/commands/space-displays.js +0 -620
- package/dist/commands/space-files.d.ts +0 -5
- package/dist/commands/space-files.js +0 -215
- package/dist/commands/space-invitations.d.ts +0 -29
- package/dist/commands/space-invitations.js +0 -189
- package/dist/commands/space-turns.d.ts +0 -49
- package/dist/commands/space-turns.js +0 -205
- package/dist/commands/space-wallet.d.ts +0 -2
- package/dist/commands/space-wallet.js +0 -64
- package/dist/commands/space-webhooks.d.ts +0 -7
- package/dist/commands/space-webhooks.js +0 -73
- package/dist/commands/spaces.d.ts +0 -25
- package/dist/commands/spaces.js +0 -2139
- package/dist/commands/tasks.d.ts +0 -8
- package/dist/commands/tasks.js +0 -80
- package/dist/commands/version.d.ts +0 -2
- package/dist/commands/version.js +0 -21
- package/dist/endpoint-options.d.ts +0 -4
- package/dist/endpoint-options.js +0 -26
- package/dist/env-proxy.d.ts +0 -5
- package/dist/env-proxy.js +0 -20
- package/dist/help-path.d.ts +0 -18
- package/dist/help-path.js +0 -223
- package/dist/host/auth.d.ts +0 -20
- package/dist/host/auth.js +0 -119
- package/dist/host/login-env.d.ts +0 -1
- package/dist/host/login-env.js +0 -31
- package/dist/host/runtime.d.ts +0 -35
- package/dist/host/runtime.js +0 -395
- package/dist/host/runtimes.d.ts +0 -9
- package/dist/host/runtimes.js +0 -42
- package/dist/host/serve.d.ts +0 -2
- package/dist/host/serve.js +0 -117
- package/dist/host/workspaces.d.ts +0 -2
- package/dist/host/workspaces.js +0 -18
- package/dist/http-put.d.ts +0 -25
- package/dist/http-put.js +0 -132
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -129
- package/dist/launcher.d.ts +0 -4
- package/dist/launcher.js +0 -38
- package/dist/local-media.d.ts +0 -2
- package/dist/local-media.js +0 -30
- package/dist/network-error.d.ts +0 -3
- package/dist/network-error.js +0 -22
- package/dist/output.d.ts +0 -46
- package/dist/output.js +0 -244
- package/dist/runtime/archive-store.d.ts +0 -54
- package/dist/runtime/archive-store.js +0 -379
- package/dist/runtime/cli-path.d.ts +0 -5
- package/dist/runtime/cli-path.js +0 -27
- package/dist/runtime/codex-usage.d.ts +0 -7
- package/dist/runtime/codex-usage.js +0 -11
- package/dist/runtime/connection.d.ts +0 -42
- package/dist/runtime/connection.js +0 -789
- package/dist/runtime/diagnostics.d.ts +0 -107
- package/dist/runtime/diagnostics.js +0 -392
- package/dist/runtime/harnesses/chained-projection.d.ts +0 -40
- package/dist/runtime/harnesses/chained-projection.js +0 -95
- package/dist/runtime/harnesses/chained-transcript.d.ts +0 -24
- package/dist/runtime/harnesses/chained-transcript.js +0 -62
- package/dist/runtime/harnesses/claude-approval.d.ts +0 -34
- package/dist/runtime/harnesses/claude-approval.js +0 -124
- package/dist/runtime/harnesses/claude-transcript.d.ts +0 -8
- package/dist/runtime/harnesses/claude-transcript.js +0 -138
- package/dist/runtime/harnesses/claude.d.ts +0 -8
- package/dist/runtime/harnesses/claude.js +0 -225
- package/dist/runtime/harnesses/codex-approval.d.ts +0 -25
- package/dist/runtime/harnesses/codex-approval.js +0 -69
- package/dist/runtime/harnesses/codex.d.ts +0 -4
- package/dist/runtime/harnesses/codex.js +0 -269
- package/dist/runtime/harnesses/cursor-transcript.d.ts +0 -37
- package/dist/runtime/harnesses/cursor-transcript.js +0 -183
- package/dist/runtime/harnesses/cursor.d.ts +0 -24
- package/dist/runtime/harnesses/cursor.js +0 -620
- package/dist/runtime/harnesses/index.d.ts +0 -16
- package/dist/runtime/harnesses/index.js +0 -73
- package/dist/runtime/harnesses/pi.d.ts +0 -4
- package/dist/runtime/harnesses/pi.js +0 -247
- package/dist/runtime/harnesses/process.d.ts +0 -31
- package/dist/runtime/harnesses/process.js +0 -75
- package/dist/runtime/harnesses/stream-json.d.ts +0 -48
- package/dist/runtime/harnesses/stream-json.js +0 -336
- package/dist/runtime/harnesses/types.d.ts +0 -117
- package/dist/runtime/harnesses/types.js +0 -1
- package/dist/runtime/harnesses/workbuddy-transcript.d.ts +0 -8
- package/dist/runtime/harnesses/workbuddy-transcript.js +0 -127
- package/dist/runtime/harnesses/workbuddy.d.ts +0 -21
- package/dist/runtime/harnesses/workbuddy.js +0 -123
- package/dist/runtime/instance.d.ts +0 -20
- package/dist/runtime/instance.js +0 -247
- package/dist/runtime/json-rpc.d.ts +0 -53
- package/dist/runtime/json-rpc.js +0 -212
- package/dist/runtime/launch.d.ts +0 -25
- package/dist/runtime/launch.js +0 -247
- package/dist/runtime/local-workspace.d.ts +0 -15
- package/dist/runtime/local-workspace.js +0 -52
- package/dist/runtime/model-catalog.d.ts +0 -12
- package/dist/runtime/model-catalog.js +0 -84
- package/dist/runtime/native/adapters.d.ts +0 -15
- package/dist/runtime/native/adapters.js +0 -11
- package/dist/runtime/native/approvals.d.ts +0 -18
- package/dist/runtime/native/approvals.js +0 -54
- package/dist/runtime/native/archive.d.ts +0 -19
- package/dist/runtime/native/archive.js +0 -142
- package/dist/runtime/native/attach.d.ts +0 -36
- package/dist/runtime/native/attach.js +0 -128
- package/dist/runtime/native/codex-link.d.ts +0 -60
- package/dist/runtime/native/codex-link.js +0 -322
- package/dist/runtime/native/config.d.ts +0 -16
- package/dist/runtime/native/config.js +0 -24
- package/dist/runtime/native/daemon.d.ts +0 -92
- package/dist/runtime/native/daemon.js +0 -380
- package/dist/runtime/native/execution.d.ts +0 -26
- package/dist/runtime/native/execution.js +0 -34
- package/dist/runtime/native/identity.d.ts +0 -9
- package/dist/runtime/native/identity.js +0 -15
- package/dist/runtime/native/ingest.d.ts +0 -263
- package/dist/runtime/native/ingest.js +0 -884
- package/dist/runtime/native/install.d.ts +0 -9
- package/dist/runtime/native/install.js +0 -113
- package/dist/runtime/native/paths.d.ts +0 -2
- package/dist/runtime/native/paths.js +0 -7
- package/dist/runtime/native/pi-extension.d.ts +0 -36
- package/dist/runtime/native/pi-link.d.ts +0 -69
- package/dist/runtime/native/pi-link.js +0 -269
- package/dist/runtime/native/results.d.ts +0 -41
- package/dist/runtime/native/results.js +0 -128
- package/dist/runtime/native/sessions.d.ts +0 -45
- package/dist/runtime/native/sessions.js +0 -87
- package/dist/runtime/native/transcript.d.ts +0 -89
- package/dist/runtime/native/transcript.js +0 -656
- package/dist/runtime/native/translate.d.ts +0 -46
- package/dist/runtime/native/translate.js +0 -299
- package/dist/runtime/native/transport.d.ts +0 -7
- package/dist/runtime/native/transport.js +0 -16
- package/dist/runtime/native/version.d.ts +0 -5
- package/dist/runtime/native/version.js +0 -21
- package/dist/runtime/preferences.d.ts +0 -7
- package/dist/runtime/preferences.js +0 -22
- package/dist/runtime/presentation.d.ts +0 -32
- package/dist/runtime/presentation.js +0 -124
- package/dist/runtime/process-group.d.ts +0 -5
- package/dist/runtime/process-group.js +0 -161
- package/dist/runtime/projection-store.d.ts +0 -27
- package/dist/runtime/projection-store.js +0 -30
- package/dist/runtime/space-binding.d.ts +0 -48
- package/dist/runtime/space-binding.js +0 -342
- package/dist/runtime/supervisor.d.ts +0 -19
- package/dist/runtime/supervisor.js +0 -441
- package/dist/runtime/turn-projection.d.ts +0 -41
- package/dist/runtime/turn-projection.js +0 -120
- package/dist/runtime/worker.d.ts +0 -1
- package/dist/safe-remote-image.d.ts +0 -36
- package/dist/safe-remote-image.js +0 -191
- package/dist/self-update.d.ts +0 -7
- package/dist/self-update.js +0 -202
- package/dist/session-status.d.ts +0 -6
- package/dist/session-status.js +0 -16
- package/dist/space-files/limit.d.ts +0 -2
- package/dist/space-files/limit.js +0 -16
- package/dist/space-files/operands.d.ts +0 -17
- package/dist/space-files/operands.js +0 -33
- package/dist/space-files/transfer.d.ts +0 -53
- package/dist/space-files/transfer.js +0 -297
- package/dist/space-files/upload.d.ts +0 -17
- package/dist/space-files/upload.js +0 -76
- package/dist/space.d.ts +0 -44
- package/dist/space.js +0 -192
- package/dist/version.d.ts +0 -2
- package/dist/version.js +0 -17
- package/dist/web.d.ts +0 -1
- package/dist/web.js +0 -4
- /package/dist/{board-kernel → chunks}/lexer.js +0 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { A as spinner, C as error, D as json, E as handleHttp, O as jsonRequested, b as resolveSpace, k as ok, t as createClient } from "./client.js";
|
|
2
|
+
import { o as mimeTypeForPath } from "./id.js";
|
|
3
|
+
import { a as parseGenerationPolicyFromEnv, n as assertGenerationRequestAllowedByPolicy, t as GenerationPolicyError } from "./policy.js";
|
|
4
|
+
import { a as probeMediaInfo } from "./media.js";
|
|
5
|
+
import { openAsBlob } from "node:fs";
|
|
6
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
7
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
8
|
+
//#region src/commands/generations.ts
|
|
9
|
+
const frameMediaRoles = /* @__PURE__ */ new Set(["first_frame", "last_frame"]);
|
|
10
|
+
const referenceMediaRoles = /* @__PURE__ */ new Set([
|
|
11
|
+
"reference_image",
|
|
12
|
+
"reference_video",
|
|
13
|
+
"reference_audio"
|
|
14
|
+
]);
|
|
15
|
+
const rolesByMediaType = {
|
|
16
|
+
image: /* @__PURE__ */ new Set([...frameMediaRoles, "reference_image"]),
|
|
17
|
+
video: /* @__PURE__ */ new Set(["reference_video"]),
|
|
18
|
+
audio: /* @__PURE__ */ new Set(["reference_audio"])
|
|
19
|
+
};
|
|
20
|
+
const mediaRoles = /* @__PURE__ */ new Set([...frameMediaRoles, ...referenceMediaRoles]);
|
|
21
|
+
function parseValue(value) {
|
|
22
|
+
const trimmed = value.trim();
|
|
23
|
+
if (trimmed === "true") return true;
|
|
24
|
+
if (trimmed === "false") return false;
|
|
25
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed)) return Number(trimmed);
|
|
26
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}") || trimmed.startsWith("[") && trimmed.endsWith("]")) try {
|
|
27
|
+
return JSON.parse(trimmed);
|
|
28
|
+
} catch {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
function parseTextInputs(values) {
|
|
34
|
+
return values.map((value) => {
|
|
35
|
+
const index = value.indexOf("=");
|
|
36
|
+
if (index < 0) return error("Invalid --text", "Pass --text <role>=<text>.");
|
|
37
|
+
const role = value.slice(0, index).trim();
|
|
38
|
+
if (!role) return error("Invalid --text", "Pass a role: --text <role>=<text>.");
|
|
39
|
+
const text = value.slice(index + 1);
|
|
40
|
+
if (!text) return error("Invalid --text", "Pass text after \"=\": --text <role>=<text>.");
|
|
41
|
+
return {
|
|
42
|
+
role,
|
|
43
|
+
text
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function parseParams(param, parameters) {
|
|
48
|
+
const result = parameters ? JSON.parse(parameters) : {};
|
|
49
|
+
for (const item of param ?? []) {
|
|
50
|
+
const index = item.indexOf("=");
|
|
51
|
+
if (index <= 0) throw new Error(`Invalid --param value: ${item}`);
|
|
52
|
+
result[item.slice(0, index)] = parseValue(item.slice(index + 1));
|
|
53
|
+
}
|
|
54
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
55
|
+
}
|
|
56
|
+
async function pathExists(path) {
|
|
57
|
+
return Boolean(await stat(path).catch(() => null));
|
|
58
|
+
}
|
|
59
|
+
async function parseMediaInput(type, rawValue) {
|
|
60
|
+
const separator = rawValue.indexOf("=");
|
|
61
|
+
if (separator <= 0) return {
|
|
62
|
+
type,
|
|
63
|
+
value: rawValue
|
|
64
|
+
};
|
|
65
|
+
const role = rawValue.slice(0, separator).trim();
|
|
66
|
+
if (!mediaRoles.has(role)) return {
|
|
67
|
+
type,
|
|
68
|
+
value: rawValue
|
|
69
|
+
};
|
|
70
|
+
if (await pathExists(rawValue)) return {
|
|
71
|
+
type,
|
|
72
|
+
value: rawValue
|
|
73
|
+
};
|
|
74
|
+
if (!rolesByMediaType[type].has(role)) return error("Invalid media role", `${role} cannot be used with --${type}`);
|
|
75
|
+
const value = rawValue.slice(separator + 1).trim();
|
|
76
|
+
if (!value) return error("Invalid media input", `--${type} ${role}= requires a path or URL`);
|
|
77
|
+
return {
|
|
78
|
+
type,
|
|
79
|
+
value,
|
|
80
|
+
role
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Local inputs upload to an unlisted public URL so tasks store a reference, not
|
|
85
|
+
* megabytes of base64. Inline data remains for `--inline`, types the upload
|
|
86
|
+
* does not accept, and failed uploads.
|
|
87
|
+
*/
|
|
88
|
+
async function mediaContent({ type, value, role }, upload) {
|
|
89
|
+
const block = (source) => ({
|
|
90
|
+
type,
|
|
91
|
+
source,
|
|
92
|
+
...role ? { meta: { role } } : {}
|
|
93
|
+
});
|
|
94
|
+
if (/^https?:\/\//.test(value)) return block({
|
|
95
|
+
type: "url",
|
|
96
|
+
url: value
|
|
97
|
+
});
|
|
98
|
+
const mediaType = mimeTypeForPath(value);
|
|
99
|
+
if (mediaType && /^(image|video|audio)\//.test(mediaType) && upload) try {
|
|
100
|
+
return block({
|
|
101
|
+
type: "url",
|
|
102
|
+
url: await upload(value, mediaType)
|
|
103
|
+
});
|
|
104
|
+
} catch (uploadError) {
|
|
105
|
+
const reason = uploadError instanceof Error ? uploadError.message : String(uploadError);
|
|
106
|
+
process.stderr.write(` Could not upload ${value} (${reason}); sending it inline.\n`);
|
|
107
|
+
}
|
|
108
|
+
const data = await readFile(value);
|
|
109
|
+
return block({
|
|
110
|
+
type: "base64",
|
|
111
|
+
mediaType: mediaType ?? "application/octet-stream",
|
|
112
|
+
data: data.toString("base64")
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function validateMediaRoleModes(inputs) {
|
|
116
|
+
const roles = inputs.flatMap(({ role }) => role ? [role] : []);
|
|
117
|
+
const hasFrameRole = roles.some((role) => frameMediaRoles.has(role));
|
|
118
|
+
const hasReferenceRole = roles.some((role) => referenceMediaRoles.has(role));
|
|
119
|
+
if (hasFrameRole && hasReferenceRole) error("Invalid media role mix", "Use first_frame/last_frame or reference_image/reference_video/reference_audio, not both.");
|
|
120
|
+
}
|
|
121
|
+
async function saveOutputs(output, outputPath) {
|
|
122
|
+
const outputs = output.filter((block) => block.type === "text" || block.type === "image" || block.type === "video" || block.type === "audio");
|
|
123
|
+
if (outputs.length === 0) return [];
|
|
124
|
+
const info = await stat(outputPath).catch(() => null);
|
|
125
|
+
const isSingleFile = outputs.length === 1 && !(info?.isDirectory() ?? false);
|
|
126
|
+
const targetPath = isSingleFile ? outputPath : await resolveOutputDirectory(outputPath, info);
|
|
127
|
+
if (isSingleFile) await mkdir(dirname(targetPath), { recursive: true });
|
|
128
|
+
else await mkdir(targetPath, { recursive: true });
|
|
129
|
+
const savedPaths = [];
|
|
130
|
+
for (const [i, block] of outputs.entries()) {
|
|
131
|
+
if (block.type === "text") {
|
|
132
|
+
const target = isSingleFile ? targetPath : join(targetPath, `generation-${i + 1}.txt`);
|
|
133
|
+
await writeFile(target, block.text, "utf-8");
|
|
134
|
+
savedPaths.push(target);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const source = block.source;
|
|
138
|
+
const target = isSingleFile ? targetPath : join(targetPath, outputName(block, source.type === "url" ? source.url : void 0, i));
|
|
139
|
+
if (source.type === "url") {
|
|
140
|
+
const response = await fetch(source.url);
|
|
141
|
+
if (!response.ok) throw new Error(`Failed to download ${source.url}: HTTP ${response.status}`);
|
|
142
|
+
await writeFile(target, Buffer.from(await response.arrayBuffer()));
|
|
143
|
+
savedPaths.push(target);
|
|
144
|
+
} else {
|
|
145
|
+
await writeFile(target, Buffer.from(source.data, "base64"));
|
|
146
|
+
savedPaths.push(target);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return savedPaths;
|
|
150
|
+
}
|
|
151
|
+
async function resolveOutputDirectory(outputPath, info) {
|
|
152
|
+
if (info?.isDirectory() || !info && !extname(outputPath)) return outputPath;
|
|
153
|
+
const ext = extname(outputPath);
|
|
154
|
+
const stem = ext ? basename(outputPath, ext) : basename(outputPath);
|
|
155
|
+
const parent = dirname(outputPath);
|
|
156
|
+
const base = join(parent, `${stem}-outputs`);
|
|
157
|
+
for (let i = 0;; i += 1) {
|
|
158
|
+
const candidate = i === 0 ? base : `${base}-${i + 1}`;
|
|
159
|
+
const candidateInfo = await stat(candidate).catch(() => null);
|
|
160
|
+
if (!candidateInfo || candidateInfo.isDirectory()) return candidate;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function outputName(block, url, index) {
|
|
164
|
+
const fromUrl = url ? basename(new URL(url).pathname) : "";
|
|
165
|
+
const label = slugOutputLabel(block);
|
|
166
|
+
if (fromUrl?.includes(".")) return `generation-${index + 1}-${label}-${fromUrl}`;
|
|
167
|
+
const ext = block.type === "video" ? "mp4" : block.type === "audio" ? "bin" : block.type === "text" ? "txt" : "png";
|
|
168
|
+
return `generation-${index + 1}-${label}.${ext}`;
|
|
169
|
+
}
|
|
170
|
+
function metaRole(block) {
|
|
171
|
+
const role = block.meta?.role;
|
|
172
|
+
return typeof role === "string" && role.length > 0 ? role : void 0;
|
|
173
|
+
}
|
|
174
|
+
function humanizeRole(role) {
|
|
175
|
+
return role.replaceAll("_", " ").replaceAll("-", " ");
|
|
176
|
+
}
|
|
177
|
+
function slugify(value) {
|
|
178
|
+
return value.toLowerCase().replaceAll(/[^a-z0-9]+/g, "-").replaceAll(/^-|-$/g, "") || "output";
|
|
179
|
+
}
|
|
180
|
+
function formatOutputLabel(block) {
|
|
181
|
+
const role = metaRole(block);
|
|
182
|
+
if (!role) return block.type;
|
|
183
|
+
const label = humanizeRole(role);
|
|
184
|
+
return block.type === "image" && [
|
|
185
|
+
"first_frame",
|
|
186
|
+
"last_frame",
|
|
187
|
+
"reference_image"
|
|
188
|
+
].includes(role) ? label : `${block.type} (role: ${role})`;
|
|
189
|
+
}
|
|
190
|
+
function slugOutputLabel(block) {
|
|
191
|
+
return slugify(metaRole(block) ?? block.type);
|
|
192
|
+
}
|
|
193
|
+
const PROBE_TIMEOUT_MS = 3e3;
|
|
194
|
+
/** Size, duration, and frames of image, video, and audio URL outputs, probed in parallel; best effort. */
|
|
195
|
+
async function probeOutputs(output) {
|
|
196
|
+
const signal = AbortSignal.timeout(PROBE_TIMEOUT_MS);
|
|
197
|
+
return (await Promise.all(output.map(async (block, index) => {
|
|
198
|
+
if (block.type !== "image" && block.type !== "video" && block.type !== "audio") return null;
|
|
199
|
+
if (block.source.type !== "url") return null;
|
|
200
|
+
const info = await probeMediaInfo(block.source.url, {
|
|
201
|
+
type: block.type,
|
|
202
|
+
signal
|
|
203
|
+
});
|
|
204
|
+
return Object.keys(info).length > 0 ? {
|
|
205
|
+
index,
|
|
206
|
+
...info
|
|
207
|
+
} : null;
|
|
208
|
+
}))).filter((media) => media !== null);
|
|
209
|
+
}
|
|
210
|
+
function mediaSummary(info) {
|
|
211
|
+
const parts = [info?.width && info.height ? `${info.width}×${info.height}` : null, info?.durationMs ? `${(info.durationMs / 1e3).toFixed(1)}s` : null].filter(Boolean);
|
|
212
|
+
return parts.length > 0 ? ` ${parts.join(" · ")}` : "";
|
|
213
|
+
}
|
|
214
|
+
function printGeneration(output, media) {
|
|
215
|
+
const byIndex = new Map(media.map((info) => [info.index, info]));
|
|
216
|
+
for (const [index, block] of output.entries()) if (block.type === "text") console.log(block.text);
|
|
217
|
+
else if (block.source.type === "url") {
|
|
218
|
+
const info = byIndex.get(index);
|
|
219
|
+
console.log(`${formatOutputLabel(block)}${mediaSummary(info)}: ${block.source.url}`);
|
|
220
|
+
if (info?.lastFrameUrl) console.log(` last frame: ${info.lastFrameUrl}`);
|
|
221
|
+
} else console.log(`${formatOutputLabel(block)}: base64 ${block.source.mediaType} (${block.source.data.length} chars)`);
|
|
222
|
+
}
|
|
223
|
+
function resumeHint(taskRunId) {
|
|
224
|
+
return `Use \`cohub tasks get ${taskRunId} --json\` to inspect the task later.`;
|
|
225
|
+
}
|
|
226
|
+
function formatElapsed(ms) {
|
|
227
|
+
if (ms < 1e3) return `${ms}ms`;
|
|
228
|
+
const seconds = Math.floor(ms / 1e3);
|
|
229
|
+
if (seconds < 60) return `${seconds}s`;
|
|
230
|
+
const minutes = Math.floor(seconds / 60);
|
|
231
|
+
const restSeconds = seconds % 60;
|
|
232
|
+
return restSeconds > 0 ? `${minutes}m ${restSeconds}s` : `${minutes}m`;
|
|
233
|
+
}
|
|
234
|
+
function parseTimeoutMs(value) {
|
|
235
|
+
if (!value) return void 0;
|
|
236
|
+
if (!/^\d+$/.test(value.trim())) return error("Invalid timeout", "--timeout-ms must be a positive integer");
|
|
237
|
+
const timeoutMs = Number.parseInt(value, 10);
|
|
238
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0) return error("Invalid timeout", "--timeout-ms must be a positive integer");
|
|
239
|
+
return timeoutMs;
|
|
240
|
+
}
|
|
241
|
+
function envValue(name) {
|
|
242
|
+
return process.env[name]?.trim() || void 0;
|
|
243
|
+
}
|
|
244
|
+
function parseMeta(value) {
|
|
245
|
+
if (!value) return void 0;
|
|
246
|
+
let parsed;
|
|
247
|
+
try {
|
|
248
|
+
parsed = JSON.parse(value);
|
|
249
|
+
} catch {
|
|
250
|
+
return error("Invalid meta", "--meta must be a JSON object");
|
|
251
|
+
}
|
|
252
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return error("Invalid meta", "--meta must be a JSON object");
|
|
253
|
+
return parsed;
|
|
254
|
+
}
|
|
255
|
+
function registerGenerations(program) {
|
|
256
|
+
program.command("generate").description("Generate multimodal outputs").argument("[prompt]", "Prompt text; optional when --text is given").requiredOption("-m, --model <model>", "Multimodal model ID from `cohub models ls --model-type multimodal`").option("--text <role=text>", "Text input with a role from the model's text inputs (`cohub models show <model>`); repeatable, split at the first =, text sent unchanged", collect, []).option("--image <path-or-url>", "Image input file path or URL; prefix with first_frame=, last_frame=, or reference_image= when needed; repeatable", collect, []).option("--video <path-or-url>", "Video input file path or URL; prefix with reference_video= when needed; repeatable", collect, []).option("--audio <path-or-url>", "Audio input file path or URL; prefix with reference_audio= when needed; repeatable", collect, []).option("--param <key=value>", "Generation parameter; repeatable, values may be JSON/number/boolean", collect, []).option("--parameters <json>", "Generation parameters as a JSON object").option("--meta <json>", "Meta as a JSON object").option("-o, --output <path>", "Save generated output to a file or directory").option("--inline", "Send local input files inline instead of uploading them to an unlisted public URL").option("--async", "Queue the generation task and return immediately").option("--timeout-ms <ms>", "Maximum time to wait in synchronous mode").option("--json", "Output as JSON").addHelpText("after", `
|
|
257
|
+
|
|
258
|
+
Local input files upload to an unlisted public URL before the task is created;
|
|
259
|
+
pass --inline to keep them inside the task instead.
|
|
260
|
+
|
|
261
|
+
Examples:
|
|
262
|
+
cohub models ls --model-type multimodal
|
|
263
|
+
cohub -s <space-id> generate "A calm lake at sunrise" -m <model> -o lake.png
|
|
264
|
+
COHUB_SPACE_ID=<space-id> cohub generate "Restyle this image" -m <model> --image input.png
|
|
265
|
+
cohub -s <space-id> generate "Smooth transition" -m seedance-2-0-fast --image first_frame=https://example.com/first.png --image last_frame=https://example.com/last.png
|
|
266
|
+
cohub -s <space-id> generate "Use these references" -m seedance-2-0-fast --image reference_image=https://example.com/a.png --image reference_image=https://example.com/b.png
|
|
267
|
+
cohub -s <space-id> generate "Lip-sync to the reference take" -m seedance-2-0-fast --image reference_image=https://example.com/portrait.png --audio reference_audio=https://example.com/speech.mp3
|
|
268
|
+
cohub -s <space-id> generate -m song2 --text "prompt=upbeat synthwave" --text "lyrics=[Verse] Neon lights" --param seed=42
|
|
269
|
+
cohub -s <space-id> generate "A calm lake" -m <model> --async
|
|
270
|
+
`).action(async (prompt, opts) => {
|
|
271
|
+
try {
|
|
272
|
+
const texts = parseTextInputs(opts.text);
|
|
273
|
+
if (prompt === void 0 && texts.length === 0) return error("Missing prompt", "Pass a prompt or at least one --text <role>=<text>.");
|
|
274
|
+
const spaceId = await resolveSpace(program, { home: true });
|
|
275
|
+
const sessionId = envValue("COHUB_SESSION_ID");
|
|
276
|
+
const inputs = await Promise.all([
|
|
277
|
+
...opts.image.map((value) => parseMediaInput("image", value)),
|
|
278
|
+
...opts.video.map((value) => parseMediaInput("video", value)),
|
|
279
|
+
...opts.audio.map((value) => parseMediaInput("audio", value))
|
|
280
|
+
]);
|
|
281
|
+
validateMediaRoleModes(inputs);
|
|
282
|
+
for (const { value } of inputs) if (!/^https?:\/\//.test(value) && !await pathExists(value)) return error("Input not found", value);
|
|
283
|
+
const parameters = parseParams(opts.param, opts.parameters);
|
|
284
|
+
try {
|
|
285
|
+
assertGenerationRequestAllowedByPolicy({
|
|
286
|
+
policy: parseGenerationPolicyFromEnv(process.env),
|
|
287
|
+
model: opts.model,
|
|
288
|
+
parameters
|
|
289
|
+
});
|
|
290
|
+
} catch (policyError) {
|
|
291
|
+
if (policyError instanceof GenerationPolicyError) return error("Generation settings", policyError.message);
|
|
292
|
+
throw policyError;
|
|
293
|
+
}
|
|
294
|
+
const meta = parseMeta(opts.meta);
|
|
295
|
+
const client = createClient();
|
|
296
|
+
const upload = opts.inline ? null : async (path, mimeType) => {
|
|
297
|
+
const asset = await client.publicAssets.uploadGenerationInput({
|
|
298
|
+
spaceId,
|
|
299
|
+
sessionId,
|
|
300
|
+
file: await openAsBlob(path, { type: mimeType }),
|
|
301
|
+
mimeType,
|
|
302
|
+
filename: basename(path)
|
|
303
|
+
});
|
|
304
|
+
process.stderr.write(` Uploaded ${path} to an unlisted public URL; use --inline to keep inputs private.\n`);
|
|
305
|
+
return asset.publicUrl;
|
|
306
|
+
};
|
|
307
|
+
const content = [
|
|
308
|
+
...prompt === void 0 ? [] : [{
|
|
309
|
+
type: "text",
|
|
310
|
+
text: prompt
|
|
311
|
+
}],
|
|
312
|
+
...texts.map(({ role, text }) => ({
|
|
313
|
+
type: "text",
|
|
314
|
+
text,
|
|
315
|
+
meta: { role }
|
|
316
|
+
})),
|
|
317
|
+
...await Promise.all(inputs.map((input) => mediaContent(input, upload)))
|
|
318
|
+
];
|
|
319
|
+
const created = await client.generations.create({
|
|
320
|
+
spaceId,
|
|
321
|
+
sessionId,
|
|
322
|
+
turnId: envValue("COHUB_TURN_ID"),
|
|
323
|
+
model: opts.model,
|
|
324
|
+
content,
|
|
325
|
+
parameters,
|
|
326
|
+
meta
|
|
327
|
+
});
|
|
328
|
+
if (opts.async) {
|
|
329
|
+
if (jsonRequested(opts)) return json(created);
|
|
330
|
+
const reserved = created.reservation ? `\n reserved: ${created.reservation.amountUsd} USD` : "";
|
|
331
|
+
return ok(`Generation queued — task ID: ${created.taskRunId}${reserved}\n ${resumeHint(created.taskRunId)}`);
|
|
332
|
+
}
|
|
333
|
+
const spin = spinner();
|
|
334
|
+
let pollCount = 0;
|
|
335
|
+
const waitStartedAt = Date.now();
|
|
336
|
+
if (!jsonRequested(opts)) {
|
|
337
|
+
process.stderr.write(` Generation queued — task ID: ${created.taskRunId}\n`);
|
|
338
|
+
process.stderr.write(` ${resumeHint(created.taskRunId)}\n`);
|
|
339
|
+
spin.start("Generating...");
|
|
340
|
+
}
|
|
341
|
+
const result = await client.generations.wait(created.taskRunId, {
|
|
342
|
+
timeoutMs: parseTimeoutMs(opts.timeoutMs),
|
|
343
|
+
onPoll: () => {
|
|
344
|
+
pollCount += 1;
|
|
345
|
+
spin.update(`Generating... ${formatElapsed(Date.now() - waitStartedAt)}, ${pollCount} polls`);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
if (!jsonRequested(opts)) spin.stop(`Generation completed — task ID: ${created.taskRunId}, ${formatElapsed(Date.now() - waitStartedAt)}, ${pollCount} polls`);
|
|
349
|
+
const [savedPaths, outputMedia] = await Promise.all([opts.output ? saveOutputs(result.output, opts.output) : Promise.resolve([]), probeOutputs(result.output)]);
|
|
350
|
+
if (jsonRequested(opts)) return json({
|
|
351
|
+
...result,
|
|
352
|
+
taskRunId: created.taskRunId,
|
|
353
|
+
...outputMedia.length > 0 ? { outputMedia } : {},
|
|
354
|
+
...savedPaths.length > 0 ? { savedPaths } : {}
|
|
355
|
+
});
|
|
356
|
+
printGeneration(result.output, outputMedia);
|
|
357
|
+
if (result.requestId || result.cost !== void 0 || result.billing) {
|
|
358
|
+
const details = [
|
|
359
|
+
result.requestId ? `request ID: ${result.requestId}` : null,
|
|
360
|
+
result.cost !== void 0 ? `official cost: ${result.cost}` : null,
|
|
361
|
+
result.billing ? `billing: ${result.billing.status}` : null,
|
|
362
|
+
result.billing ? `effective amount: ${result.billing.amountUsd}` : null,
|
|
363
|
+
result.billing?.discountMultiplier !== void 0 ? `multiplier: ${result.billing.discountMultiplier}` : null,
|
|
364
|
+
result.billing?.reason ? `billing reason: ${result.billing.reason}` : null
|
|
365
|
+
].filter(Boolean).join(", ");
|
|
366
|
+
if (details) process.stderr.write(` ${details}\n`);
|
|
367
|
+
}
|
|
368
|
+
if (savedPaths.length > 0) ok(`Saved to ${savedPaths.join(", ")}`);
|
|
369
|
+
} catch (e) {
|
|
370
|
+
handleHttp(e);
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
function collect(value, previous) {
|
|
375
|
+
previous.push(value);
|
|
376
|
+
return previous;
|
|
377
|
+
}
|
|
378
|
+
//#endregion
|
|
379
|
+
export { registerGenerations };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/commands/host.ts
|
|
2
|
+
function registerHost(program) {
|
|
3
|
+
program.command("host", { hidden: true }).description("Machine interface for native desktop hosts").command("serve").description("Answer host bridge requests over stdio, one JSON message per line").action(async () => {
|
|
4
|
+
const { serveHost } = await import("./serve.js");
|
|
5
|
+
await serveHost();
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { registerHost };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
//#region src/http-put.ts
|
|
3
|
+
const DEFAULT_ATTEMPTS = 3;
|
|
4
|
+
const DEFAULT_DELAY_MS = 200;
|
|
5
|
+
const RETRYABLE_STATUS = /* @__PURE__ */ new Set([
|
|
6
|
+
408,
|
|
7
|
+
411,
|
|
8
|
+
425,
|
|
9
|
+
429,
|
|
10
|
+
500,
|
|
11
|
+
502,
|
|
12
|
+
503,
|
|
13
|
+
504
|
|
14
|
+
]);
|
|
15
|
+
const RETRYABLE_CODES = /* @__PURE__ */ new Set([
|
|
16
|
+
"BodyTimeoutError",
|
|
17
|
+
"ConnectTimeoutError",
|
|
18
|
+
"EAI_AGAIN",
|
|
19
|
+
"ECONNREFUSED",
|
|
20
|
+
"ECONNRESET",
|
|
21
|
+
"EHOSTUNREACH",
|
|
22
|
+
"ENETUNREACH",
|
|
23
|
+
"ENOTFOUND",
|
|
24
|
+
"EPIPE",
|
|
25
|
+
"ETIMEDOUT",
|
|
26
|
+
"HeadersTimeoutError",
|
|
27
|
+
"UND_ERR_BODY_TIMEOUT",
|
|
28
|
+
"UND_ERR_CONNECT",
|
|
29
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
30
|
+
"UND_ERR_HEADERS_TIMEOUT",
|
|
31
|
+
"UND_ERR_SOCKET"
|
|
32
|
+
]);
|
|
33
|
+
var HttpPutError = class extends Error {
|
|
34
|
+
status;
|
|
35
|
+
body;
|
|
36
|
+
constructor(message, status, body = "") {
|
|
37
|
+
super(message);
|
|
38
|
+
this.name = "HttpPutError";
|
|
39
|
+
this.status = status;
|
|
40
|
+
this.body = body;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
function withContentLength(headers, contentLength) {
|
|
44
|
+
const result = new Headers(headers);
|
|
45
|
+
if (contentLength !== void 0 && !result.has("content-length")) result.set("content-length", String(contentLength));
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
function errorCode(error) {
|
|
49
|
+
if (!error || typeof error !== "object") return void 0;
|
|
50
|
+
const record = error;
|
|
51
|
+
if (typeof record.code === "string") return record.code;
|
|
52
|
+
if (typeof record.cause?.code === "string") return record.cause.code;
|
|
53
|
+
if (typeof record.cause?.name === "string") return record.cause.name;
|
|
54
|
+
if (typeof record.name === "string") return record.name;
|
|
55
|
+
}
|
|
56
|
+
function isRetryableError(error) {
|
|
57
|
+
const code = errorCode(error);
|
|
58
|
+
return Boolean(code && RETRYABLE_CODES.has(code));
|
|
59
|
+
}
|
|
60
|
+
function isRetryableStatus(status) {
|
|
61
|
+
return RETRYABLE_STATUS.has(status);
|
|
62
|
+
}
|
|
63
|
+
function defaultSleep(ms) {
|
|
64
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
65
|
+
}
|
|
66
|
+
function failureMessage(label, status, detail) {
|
|
67
|
+
return `Failed to upload ${label}: HTTP ${status}${detail ? ` — ${detail}` : ""}`;
|
|
68
|
+
}
|
|
69
|
+
async function putWithRetry(input) {
|
|
70
|
+
const attempts = input.attempts ?? DEFAULT_ATTEMPTS;
|
|
71
|
+
const delayMs = input.delayMs ?? DEFAULT_DELAY_MS;
|
|
72
|
+
const sleep = input.sleep ?? defaultSleep;
|
|
73
|
+
const fetchImpl = input.fetch ?? fetch;
|
|
74
|
+
let lastError;
|
|
75
|
+
for (let attempt = 0; attempt < attempts; attempt += 1) try {
|
|
76
|
+
const response = await fetchImpl(input.url, {
|
|
77
|
+
method: "PUT",
|
|
78
|
+
headers: withContentLength(input.headers, input.contentLength),
|
|
79
|
+
body: input.body(),
|
|
80
|
+
...input.duplex ? { duplex: "half" } : {}
|
|
81
|
+
});
|
|
82
|
+
if (response.ok) return response;
|
|
83
|
+
const detail = await response.text().catch(() => "");
|
|
84
|
+
const error = new HttpPutError(failureMessage(input.label, response.status, detail), response.status, detail);
|
|
85
|
+
if (attempt < attempts - 1 && isRetryableStatus(response.status)) {
|
|
86
|
+
lastError = error;
|
|
87
|
+
await sleep(delayMs * 2 ** attempt);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
throw error;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (error instanceof HttpPutError) throw error;
|
|
93
|
+
lastError = error;
|
|
94
|
+
if (attempt < attempts - 1 && isRetryableError(error)) {
|
|
95
|
+
await sleep(delayMs * 2 ** attempt);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
throw lastError;
|
|
101
|
+
}
|
|
102
|
+
async function putLocalFile(input) {
|
|
103
|
+
await putWithRetry({
|
|
104
|
+
url: input.url,
|
|
105
|
+
body: () => createReadStream(input.filePath),
|
|
106
|
+
headers: input.headers,
|
|
107
|
+
contentLength: input.size,
|
|
108
|
+
duplex: true,
|
|
109
|
+
label: input.label,
|
|
110
|
+
attempts: input.attempts,
|
|
111
|
+
delayMs: input.delayMs,
|
|
112
|
+
sleep: input.sleep,
|
|
113
|
+
fetch: input.fetch
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async function putBytes(input) {
|
|
117
|
+
const body = input.body;
|
|
118
|
+
await putWithRetry({
|
|
119
|
+
url: input.url,
|
|
120
|
+
body: () => body,
|
|
121
|
+
headers: input.headers,
|
|
122
|
+
contentLength: input.size,
|
|
123
|
+
label: input.label,
|
|
124
|
+
attempts: input.attempts,
|
|
125
|
+
delayMs: input.delayMs,
|
|
126
|
+
sleep: input.sleep,
|
|
127
|
+
fetch: input.fetch
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
//#endregion
|
|
131
|
+
export { putBytes as n, putLocalFile as r, HttpPutError as t };
|