@b3os/sdk 0.1.0 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +171 -42
- package/dist/cjs/core/bytes.d.ts +30 -0
- package/dist/cjs/core/bytes.js +115 -0
- package/dist/cjs/core/bytes.js.map +1 -0
- package/dist/cjs/core/client.d.ts +61 -3
- package/dist/cjs/core/client.js +211 -33
- package/dist/cjs/core/client.js.map +1 -1
- package/dist/cjs/core/errors.d.ts +16 -4
- package/dist/cjs/core/errors.js +9 -6
- package/dist/cjs/core/errors.js.map +1 -1
- package/dist/cjs/core/handle.d.ts +31 -0
- package/dist/cjs/core/handle.js +86 -0
- package/dist/cjs/core/handle.js.map +1 -0
- package/dist/cjs/core/index.d.ts +6 -4
- package/dist/cjs/core/index.js +8 -1
- package/dist/cjs/core/index.js.map +1 -1
- package/dist/cjs/core/login.d.ts +24 -0
- package/dist/cjs/core/login.js +93 -0
- package/dist/cjs/core/login.js.map +1 -0
- package/dist/cjs/core/namespace.d.ts +1 -1
- package/dist/cjs/core/namespace.js +1 -1
- package/dist/cjs/core/op.d.ts +11 -1
- package/dist/cjs/core/timing.d.ts +1 -1
- package/dist/cjs/core/timing.js +8 -1
- package/dist/cjs/core/timing.js.map +1 -1
- package/dist/cjs/core/transport.js +1 -1
- package/dist/cjs/core/transport.js.map +1 -1
- package/dist/cjs/core/util.d.ts +15 -3
- package/dist/cjs/core/util.js +54 -2
- package/dist/cjs/core/util.js.map +1 -1
- package/dist/cjs/core/ws.d.ts +4 -0
- package/dist/cjs/core/ws.js +7 -4
- package/dist/cjs/core/ws.js.map +1 -1
- package/dist/cjs/generated/client.d.ts +2 -2
- package/dist/cjs/generated/client.js +2 -2
- package/dist/cjs/generated/errors.d.ts +1 -1
- package/dist/cjs/generated/errors.js +30 -0
- package/dist/cjs/generated/errors.js.map +1 -1
- package/dist/cjs/generated/jobs.js +1 -1
- package/dist/cjs/generated/ns/actions.d.ts +48 -6
- package/dist/cjs/generated/ns/actions.js +59 -6
- package/dist/cjs/generated/ns/actions.js.map +1 -1
- package/dist/cjs/generated/ns/infer.d.ts +185 -54
- package/dist/cjs/generated/ns/infer.js +28 -74
- package/dist/cjs/generated/ns/infer.js.map +1 -1
- package/dist/cjs/generated/ns/keys.d.ts +12 -0
- package/dist/cjs/generated/ns/keys.js +22 -0
- package/dist/cjs/generated/ns/keys.js.map +1 -1
- package/dist/cjs/generated/ns/render.d.ts +95 -6
- package/dist/cjs/generated/ns/render.js +124 -7
- package/dist/cjs/generated/ns/render.js.map +1 -1
- package/dist/cjs/generated/ns/requests.d.ts +3 -1
- package/dist/cjs/generated/ns/requests.js +3 -1
- package/dist/cjs/generated/ns/requests.js.map +1 -1
- package/dist/cjs/generated/ns/routines.d.ts +131 -9
- package/dist/cjs/generated/ns/routines.js +186 -9
- package/dist/cjs/generated/ns/routines.js.map +1 -1
- package/dist/cjs/generated/ns/sandboxes.d.ts +298 -31
- package/dist/cjs/generated/ns/sandboxes.js +368 -29
- package/dist/cjs/generated/ns/sandboxes.js.map +1 -1
- package/dist/cjs/generated/ns/vision.d.ts +133 -14
- package/dist/cjs/generated/ns/vision.js +158 -12
- package/dist/cjs/generated/ns/vision.js.map +1 -1
- package/dist/cjs/generated/ns/webhooks.d.ts +8 -5
- package/dist/cjs/generated/ns/webhooks.js +8 -5
- package/dist/cjs/generated/ns/webhooks.js.map +1 -1
- package/dist/cjs/generated/types.d.ts +1956 -301
- package/dist/cjs/helpers/agents.d.ts +95 -0
- package/dist/cjs/helpers/agents.js +205 -0
- package/dist/cjs/helpers/agents.js.map +1 -0
- package/dist/cjs/helpers/render.d.ts +15 -0
- package/dist/cjs/helpers/render.js +74 -0
- package/dist/cjs/helpers/render.js.map +1 -0
- package/dist/cjs/helpers/routines.d.ts +61 -0
- package/dist/cjs/helpers/routines.js +234 -0
- package/dist/cjs/helpers/routines.js.map +1 -0
- package/dist/cjs/helpers/sandboxes.d.ts +237 -0
- package/dist/cjs/helpers/sandboxes.js +608 -0
- package/dist/cjs/helpers/sandboxes.js.map +1 -0
- package/dist/cjs/index.d.ts +4 -1
- package/dist/cjs/index.js +8 -5
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/routines.d.ts +1 -0
- package/dist/cjs/routines.js +21 -0
- package/dist/cjs/routines.js.map +1 -0
- package/dist/cjs/webhooks.js.map +1 -1
- package/dist/esm/core/bytes.d.ts +30 -0
- package/dist/esm/core/bytes.js +111 -0
- package/dist/esm/core/bytes.js.map +1 -0
- package/dist/esm/core/client.d.ts +61 -3
- package/dist/esm/core/client.js +208 -31
- package/dist/esm/core/client.js.map +1 -1
- package/dist/esm/core/errors.d.ts +16 -4
- package/dist/esm/core/errors.js +9 -6
- package/dist/esm/core/errors.js.map +1 -1
- package/dist/esm/core/handle.d.ts +31 -0
- package/dist/esm/core/handle.js +82 -0
- package/dist/esm/core/handle.js.map +1 -0
- package/dist/esm/core/index.d.ts +6 -4
- package/dist/esm/core/index.js +4 -2
- package/dist/esm/core/index.js.map +1 -1
- package/dist/esm/core/login.d.ts +24 -0
- package/dist/esm/core/login.js +85 -0
- package/dist/esm/core/login.js.map +1 -0
- package/dist/esm/core/namespace.d.ts +1 -1
- package/dist/esm/core/namespace.js +1 -1
- package/dist/esm/core/op.d.ts +11 -1
- package/dist/esm/core/timing.d.ts +1 -1
- package/dist/esm/core/timing.js +8 -1
- package/dist/esm/core/timing.js.map +1 -1
- package/dist/esm/core/transport.js +1 -1
- package/dist/esm/core/transport.js.map +1 -1
- package/dist/esm/core/util.d.ts +15 -3
- package/dist/esm/core/util.js +48 -1
- package/dist/esm/core/util.js.map +1 -1
- package/dist/esm/core/ws.d.ts +4 -0
- package/dist/esm/core/ws.js +7 -4
- package/dist/esm/core/ws.js.map +1 -1
- package/dist/esm/generated/client.d.ts +2 -2
- package/dist/esm/generated/client.js +2 -2
- package/dist/esm/generated/errors.d.ts +1 -1
- package/dist/esm/generated/errors.js +30 -0
- package/dist/esm/generated/errors.js.map +1 -1
- package/dist/esm/generated/jobs.js +1 -1
- package/dist/esm/generated/ns/actions.d.ts +48 -6
- package/dist/esm/generated/ns/actions.js +59 -6
- package/dist/esm/generated/ns/actions.js.map +1 -1
- package/dist/esm/generated/ns/infer.d.ts +185 -54
- package/dist/esm/generated/ns/infer.js +28 -74
- package/dist/esm/generated/ns/infer.js.map +1 -1
- package/dist/esm/generated/ns/keys.d.ts +12 -0
- package/dist/esm/generated/ns/keys.js +22 -0
- package/dist/esm/generated/ns/keys.js.map +1 -1
- package/dist/esm/generated/ns/render.d.ts +95 -6
- package/dist/esm/generated/ns/render.js +124 -7
- package/dist/esm/generated/ns/render.js.map +1 -1
- package/dist/esm/generated/ns/requests.d.ts +3 -1
- package/dist/esm/generated/ns/requests.js +3 -1
- package/dist/esm/generated/ns/requests.js.map +1 -1
- package/dist/esm/generated/ns/routines.d.ts +131 -9
- package/dist/esm/generated/ns/routines.js +186 -9
- package/dist/esm/generated/ns/routines.js.map +1 -1
- package/dist/esm/generated/ns/sandboxes.d.ts +298 -31
- package/dist/esm/generated/ns/sandboxes.js +368 -29
- package/dist/esm/generated/ns/sandboxes.js.map +1 -1
- package/dist/esm/generated/ns/vision.d.ts +133 -14
- package/dist/esm/generated/ns/vision.js +158 -12
- package/dist/esm/generated/ns/vision.js.map +1 -1
- package/dist/esm/generated/ns/webhooks.d.ts +8 -5
- package/dist/esm/generated/ns/webhooks.js +8 -5
- package/dist/esm/generated/ns/webhooks.js.map +1 -1
- package/dist/esm/generated/types.d.ts +1956 -301
- package/dist/esm/helpers/agents.d.ts +95 -0
- package/dist/esm/helpers/agents.js +199 -0
- package/dist/esm/helpers/agents.js.map +1 -0
- package/dist/esm/helpers/render.d.ts +15 -0
- package/dist/esm/helpers/render.js +70 -0
- package/dist/esm/helpers/render.js.map +1 -0
- package/dist/esm/helpers/routines.d.ts +61 -0
- package/dist/esm/helpers/routines.js +226 -0
- package/dist/esm/helpers/routines.js.map +1 -0
- package/dist/esm/helpers/sandboxes.d.ts +237 -0
- package/dist/esm/helpers/sandboxes.js +600 -0
- package/dist/esm/helpers/sandboxes.js.map +1 -0
- package/dist/esm/index.d.ts +4 -1
- package/dist/esm/index.js +6 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/routines.d.ts +1 -0
- package/dist/esm/routines.js +5 -0
- package/dist/esm/routines.js.map +1 -0
- package/dist/esm/webhooks.js.map +1 -1
- package/package.json +14 -3
- package/src/core/bytes.ts +112 -0
- package/src/core/client.ts +253 -33
- package/src/core/errors.ts +27 -10
- package/src/core/handle.ts +97 -0
- package/src/core/index.ts +7 -2
- package/src/core/login.ts +98 -0
- package/src/core/namespace.ts +1 -1
- package/src/core/op.ts +11 -1
- package/src/core/timing.ts +7 -2
- package/src/core/transport.ts +1 -1
- package/src/core/util.ts +53 -3
- package/src/core/ws.ts +11 -4
- package/src/generated/client.ts +2 -2
- package/src/generated/errors.ts +60 -0
- package/src/generated/jobs.ts +1 -1
- package/src/generated/ns/actions.ts +69 -6
- package/src/generated/ns/infer.ts +229 -61
- package/src/generated/ns/keys.ts +23 -0
- package/src/generated/ns/render.ts +152 -7
- package/src/generated/ns/requests.ts +3 -1
- package/src/generated/ns/routines.ts +205 -9
- package/src/generated/ns/sandboxes.ts +434 -34
- package/src/generated/ns/vision.ts +168 -14
- package/src/generated/ns/webhooks.ts +8 -5
- package/src/generated/types.ts +2211 -422
- package/src/helpers/agents.ts +300 -0
- package/src/helpers/render.ts +91 -0
- package/src/helpers/routines.ts +278 -0
- package/src/helpers/sandboxes.ts +788 -0
- package/src/index.ts +29 -5
- package/src/routines.ts +5 -0
- package/src/webhooks.ts +5 -1
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// @b3os/sdk: one client for every B3OS product.
|
|
2
2
|
//
|
|
3
3
|
// import { B3OS } from "@b3os/sdk";
|
|
4
|
-
// const
|
|
5
|
-
// const page = await
|
|
6
|
-
//
|
|
7
|
-
// const out = await
|
|
4
|
+
// const b3os = new B3OS(); // B3OS_API_KEY, your b3os login, or the node socket / sandbox URL
|
|
5
|
+
// const page = await b3os.vision.scrape({ url: "https://example.com" });
|
|
6
|
+
// await using sbx = await b3os.sandboxes.create({ template: "base" }); // deleted when the scope ends
|
|
7
|
+
// const out = await sbx.exec("python3 -c 'print(2 + 2)'");
|
|
8
8
|
export * from "./core/index.js";
|
|
9
9
|
export { B3OS } from "./generated/client.js";
|
|
10
10
|
export { ERROR_CATALOG } from "./generated/errors.js";
|
|
@@ -24,5 +24,6 @@ export { UsageAPI, usageOps } from "./generated/ns/usage.js";
|
|
|
24
24
|
export { VaultAPI, vaultOps } from "./generated/ns/vault.js";
|
|
25
25
|
export { VisionAPI, visionOps } from "./generated/ns/vision.js";
|
|
26
26
|
export { WebhooksAPI, webhooksOps } from "./generated/ns/webhooks.js";
|
|
27
|
-
export {
|
|
27
|
+
export { Sandbox, } from "./helpers/sandboxes.js";
|
|
28
|
+
export { signWebhook, verifyWebhook, WebhookVerificationError } from "./webhooks.js";
|
|
28
29
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD,EAAE;AACF,sCAAsC;AACtC
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD,EAAE;AACF,sCAAsC;AACtC,kHAAkH;AAClH,2EAA2E;AAC3E,wGAAwG;AACxG,6DAA6D;AAE7D,cAAc,iBAAiB,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAE7C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAiB,IAAI,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAClF,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AACzE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAatE,OAAO,EAKL,OAAO,GAOR,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./helpers/routines.ts";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// @b3os/sdk/routines (package.json "./routines"): the routine builder is in helpers/routines.ts, which the routines
|
|
2
|
+
// product owns. This file stays the subpath's entry, so `import … from "@b3os/sdk/routines"` and imports of
|
|
3
|
+
// src/routines.ts keep working.
|
|
4
|
+
export * from "./helpers/routines.js";
|
|
5
|
+
//# sourceMappingURL=routines.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routines.js","sourceRoot":"","sources":["../../src/routines.ts"],"names":[],"mappings":"AAAA,oHAAoH;AACpH,4GAA4G;AAC5G,gCAAgC;AAEhC,cAAc,uBAAuB,CAAC"}
|
package/dist/esm/webhooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../../src/webhooks.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,+GAA+G;AAC/G,uEAAuE;AAEvE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AASvE,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACxC,MAAM,CAAiB;IAChC,YAAY,MAAsB,EAAE,OAAe;QACjD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF;AASD,SAAS,WAAW,CAAC,MAAc;IACjC,IAAI,CAAqB,CAAC;IAC1B,MAAM,EAAE,GAAa,EAAE,CAAC;IACxB,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACrC,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,EAAE,GAAG,CAAC;YAAE,SAAS;QACrB,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QACnC,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QACpC,IAAI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AG,IAAI,CAAC;IAEzC,MAAM,IAAI,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IACjF,OAAO,KAAK,CAAC,OAAO,MAAM,OAAO,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;AAChG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@b3os/sdk",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "TypeScript SDK for B3OS beta: browsers,
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "TypeScript SDK for B3OS beta: vision (web pages and browsers), render, infer, sandboxes, routines, actions and vault, on the machine that runs the model.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"sideEffects": false,
|
|
8
8
|
"engines": {
|
|
9
|
-
"node": ">=
|
|
9
|
+
"node": ">=22.3"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"dist",
|
|
@@ -62,6 +62,17 @@
|
|
|
62
62
|
"default": "./dist/cjs/webhooks.js"
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
|
+
"./routines": {
|
|
66
|
+
"b3os-source": "./src/routines.ts",
|
|
67
|
+
"import": {
|
|
68
|
+
"types": "./dist/esm/routines.d.ts",
|
|
69
|
+
"default": "./dist/esm/routines.js"
|
|
70
|
+
},
|
|
71
|
+
"require": {
|
|
72
|
+
"types": "./dist/cjs/routines.d.ts",
|
|
73
|
+
"default": "./dist/cjs/routines.js"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
65
76
|
"./*": {
|
|
66
77
|
"b3os-source": "./src/generated/ns/*.ts",
|
|
67
78
|
"import": {
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// A response body read as it arrives (R-infer-37): `infer.createSpeech({ ..., stream_format: "audio" })` returns
|
|
2
|
+
// one, so the first audio plays before the last is made.
|
|
3
|
+
//
|
|
4
|
+
// const audio = b3os.infer.createSpeech({ model: "kokoro-82m", voice: "af_heart", input, stream_format: "audio" });
|
|
5
|
+
// for await (const chunk of audio) player.write(chunk);
|
|
6
|
+
// (await audio.response()).timing.phases.ttfa; // ms from the request to the first audio (Server-Timing)
|
|
7
|
+
|
|
8
|
+
import { B3OSAbortError, B3OSConnectionError, B3OSError, B3OSTimeoutError } from "./errors.ts";
|
|
9
|
+
import type { ResponseInfo } from "./response.ts";
|
|
10
|
+
import { type TransportResponse, TransportTimeout } from "./transport.ts";
|
|
11
|
+
import { describeErr } from "./util.ts";
|
|
12
|
+
|
|
13
|
+
/** What opens the request: the client's send, under the stream's own signal. */
|
|
14
|
+
export type ByteStreamOpen = (signal: AbortSignal) => Promise<{ res: TransportResponse; info: ResponseInfo }>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The bytes of one response, chunk by chunk. The request starts on the first use (`for await`, `response()`,
|
|
18
|
+
* `bytes()`); an error before the first byte is the operation's own `B3OSError`. Iterate once.
|
|
19
|
+
*/
|
|
20
|
+
export class ByteStream implements AsyncIterable<Uint8Array> {
|
|
21
|
+
private readonly controller = new AbortController();
|
|
22
|
+
private opened: Promise<{ res: TransportResponse; info: ResponseInfo }> | undefined;
|
|
23
|
+
private iterating = false;
|
|
24
|
+
private closed = false;
|
|
25
|
+
/** True once the whole body arrived. */
|
|
26
|
+
completed = false;
|
|
27
|
+
|
|
28
|
+
private readonly operationId: string;
|
|
29
|
+
private readonly open: ByteStreamOpen;
|
|
30
|
+
|
|
31
|
+
constructor(operationId: string, open: ByteStreamOpen, signal?: AbortSignal) {
|
|
32
|
+
this.operationId = operationId;
|
|
33
|
+
this.open = open;
|
|
34
|
+
if (signal) {
|
|
35
|
+
if (signal.aborted) this.controller.abort(signal.reason);
|
|
36
|
+
else signal.addEventListener("abort", () => this.controller.abort(signal.reason), { once: true });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private start(): Promise<{ res: TransportResponse; info: ResponseInfo }> {
|
|
41
|
+
this.opened ??= this.open(this.controller.signal);
|
|
42
|
+
return this.opened;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The response head: status, headers and Server-Timing (`ttfa` for speech). Starts the request. */
|
|
46
|
+
async response(): Promise<ResponseInfo> {
|
|
47
|
+
return (await this.start()).info;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Stops reading and closes the connection; a loop iterating the stream ends without an error. */
|
|
51
|
+
close(): void {
|
|
52
|
+
this.closed = true;
|
|
53
|
+
this.controller.abort(new B3OSAbortError({ detail: "The byte stream was closed", operationId: this.operationId }));
|
|
54
|
+
void this.opened?.then(({ res }) => res.cancel()).catch(() => {});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Reads the whole body into one array. */
|
|
58
|
+
async bytes(): Promise<Uint8Array> {
|
|
59
|
+
const parts: Uint8Array[] = [];
|
|
60
|
+
let n = 0;
|
|
61
|
+
for await (const c of this) {
|
|
62
|
+
parts.push(c);
|
|
63
|
+
n += c.length;
|
|
64
|
+
}
|
|
65
|
+
const out = new Uint8Array(n);
|
|
66
|
+
let o = 0;
|
|
67
|
+
for (const p of parts) {
|
|
68
|
+
out.set(p, o);
|
|
69
|
+
o += p.length;
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async *[Symbol.asyncIterator](): AsyncIterator<Uint8Array> {
|
|
75
|
+
if (this.iterating) throw new Error("@b3os/sdk: a ByteStream can be iterated only once");
|
|
76
|
+
this.iterating = true;
|
|
77
|
+
let res: TransportResponse;
|
|
78
|
+
try {
|
|
79
|
+
({ res } = await this.start());
|
|
80
|
+
} catch (e) {
|
|
81
|
+
if (this.closed) return;
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
for await (const chunk of res.chunks()) {
|
|
86
|
+
if (this.closed) return;
|
|
87
|
+
if (chunk.length) yield chunk;
|
|
88
|
+
}
|
|
89
|
+
this.completed = !this.closed;
|
|
90
|
+
} catch (e) {
|
|
91
|
+
if (this.closed) return;
|
|
92
|
+
if (e instanceof B3OSError) throw e;
|
|
93
|
+
if (this.controller.signal.aborted) {
|
|
94
|
+
throw new B3OSAbortError({ operationId: this.operationId, cause: this.controller.signal.reason });
|
|
95
|
+
}
|
|
96
|
+
if (e instanceof TransportTimeout) {
|
|
97
|
+
throw new B3OSTimeoutError({
|
|
98
|
+
detail: `${this.operationId}: the body stopped arriving`,
|
|
99
|
+
operationId: this.operationId,
|
|
100
|
+
cause: e,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
throw new B3OSConnectionError({
|
|
104
|
+
detail: `The response body of ${this.operationId} broke: ${describeErr(e)}`,
|
|
105
|
+
operationId: this.operationId,
|
|
106
|
+
cause: e,
|
|
107
|
+
});
|
|
108
|
+
} finally {
|
|
109
|
+
res.cancel();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
package/src/core/client.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// pipeline (idempotency keys, retries with jittered backoff, timeouts, typed errors, Server-Timing), and the job
|
|
3
3
|
// and stream plumbing the generated namespaces call. It knows no product; src/generated/ns/* do.
|
|
4
4
|
|
|
5
|
+
import { ByteStream } from "./bytes.ts";
|
|
5
6
|
import {
|
|
6
7
|
B3OSAbortError,
|
|
7
8
|
B3OSConnectionError,
|
|
@@ -11,6 +12,7 @@ import {
|
|
|
11
12
|
NEVER_REACHED_STATUSES,
|
|
12
13
|
parseErrorBody,
|
|
13
14
|
} from "./errors.ts";
|
|
15
|
+
import { announceLogin, canReadLogin, loginBlockedBy, readStoredLogin } from "./login.ts";
|
|
14
16
|
import type { CallArgs, JobOps, Op } from "./op.ts";
|
|
15
17
|
import {
|
|
16
18
|
APIPromise,
|
|
@@ -37,17 +39,58 @@ import {
|
|
|
37
39
|
type TransportResponse,
|
|
38
40
|
TransportTimeout,
|
|
39
41
|
} from "./transport.ts";
|
|
40
|
-
import {
|
|
42
|
+
import {
|
|
43
|
+
backoffMs,
|
|
44
|
+
describeErr,
|
|
45
|
+
env,
|
|
46
|
+
isObject,
|
|
47
|
+
newTraceId,
|
|
48
|
+
parseRetryAfter,
|
|
49
|
+
runtime,
|
|
50
|
+
runtimeLabel,
|
|
51
|
+
SDK_VERSION,
|
|
52
|
+
sleep,
|
|
53
|
+
traceIdOf,
|
|
54
|
+
traceparentIn,
|
|
55
|
+
uuid,
|
|
56
|
+
} from "./util.ts";
|
|
41
57
|
|
|
42
58
|
/** A binary request body (file uploads, pack bundles). */
|
|
43
59
|
export type BinaryInput = RequestBody;
|
|
44
60
|
|
|
45
61
|
export const DEFAULT_BASE_URL = "https://beta-api.b3os.org";
|
|
62
|
+
/** Where API keys are made (the console's API keys page). */
|
|
63
|
+
export const KEYS_PAGE = "https://beta.b3os.org/app/keys";
|
|
46
64
|
/** b3osd's Unix socket on a B3OS node (ARCHITECTURE §1). */
|
|
47
65
|
export const DEFAULT_SOCKET_PATH = "/run/b3os/b3osd/b3osd.sock";
|
|
48
66
|
|
|
67
|
+
/** What `GET /v1/ping` answers (requests.yaml `Ping`). */
|
|
68
|
+
export interface Ping {
|
|
69
|
+
object: "ping";
|
|
70
|
+
/** The node that answered (`n1`). */
|
|
71
|
+
node: string;
|
|
72
|
+
/** The node's clock when it answered, RFC 3339 in UTC with milliseconds. */
|
|
73
|
+
ts: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* `platformPing` from packages/spec/dist (routes.json has no SDK name for it: it belongs to no namespace), so the
|
|
78
|
+
* core carries it; test/ping.test.ts checks it against routes.json.
|
|
79
|
+
*/
|
|
80
|
+
export const PING_OP: Op = {
|
|
81
|
+
id: "platformPing",
|
|
82
|
+
method: "GET",
|
|
83
|
+
path: "/v1/ping",
|
|
84
|
+
timeoutMs: 5000,
|
|
85
|
+
scope: "authenticated",
|
|
86
|
+
};
|
|
87
|
+
|
|
49
88
|
export interface ClientOptions {
|
|
50
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* API key (`b3os_sk_…`) or CLI token (`b3os_pat_…`). Default: `B3OS_API_KEY`, else, on a developer's machine, the
|
|
91
|
+
* token `b3os login` stored (never in CI, on GitHub Actions or in a B3OS sandbox; one stderr line says when it is
|
|
92
|
+
* used). `null` sends no key and skips the stored login.
|
|
93
|
+
*/
|
|
51
94
|
apiKey?: string | null;
|
|
52
95
|
/** The API host for edge-served operations and remote calls. Default: `B3OS_BASE_URL`, else beta-api.b3os.org. */
|
|
53
96
|
baseUrl?: string;
|
|
@@ -83,13 +126,20 @@ export interface ClientOptions {
|
|
|
83
126
|
}
|
|
84
127
|
|
|
85
128
|
export interface RequestOptions {
|
|
86
|
-
/**
|
|
129
|
+
/**
|
|
130
|
+
* `Idempotency-Key` for this call; generated when the operation takes one and you pass none, except for
|
|
131
|
+
* operations whose effect cannot be repeated (`x-b3os-idempotency: caller`, `sandboxes.exec`): those send only
|
|
132
|
+
* the key you pass.
|
|
133
|
+
*/
|
|
87
134
|
idempotencyKey?: string;
|
|
88
135
|
timeoutMs?: number;
|
|
89
136
|
maxRetries?: number;
|
|
90
137
|
signal?: AbortSignal;
|
|
91
138
|
headers?: Record<string, string>;
|
|
92
|
-
/**
|
|
139
|
+
/**
|
|
140
|
+
* W3C trace context, propagated into product calls and routine steps. Sent as given; without one, each call
|
|
141
|
+
* gets its own trace (every attempt a new span in it), and calls through a resource handle share the handle's.
|
|
142
|
+
*/
|
|
93
143
|
traceparent?: string;
|
|
94
144
|
}
|
|
95
145
|
|
|
@@ -155,6 +205,23 @@ const REJECTED_BEFORE_WORK = new Set([
|
|
|
155
205
|
"auth_unavailable",
|
|
156
206
|
"idempotency_request_in_progress",
|
|
157
207
|
]);
|
|
208
|
+
/**
|
|
209
|
+
* Codes in REJECTED_BEFORE_WORK that the edge also sends after it forwarded the request, so a B3OS body with one
|
|
210
|
+
* does not show that the request did no work: 503 `node_unavailable` covers a tunnel or a node that failed while it
|
|
211
|
+
* held the request (apps/edge/src/gateway/forward.ts maps every fetch failure and every answer without the node's
|
|
212
|
+
* request id to it). A caller-keyed operation (sandboxesExec) is not resent after one; other operations still are.
|
|
213
|
+
*/
|
|
214
|
+
const MAYBE_FORWARDED = new Set(["node_unavailable"]);
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* True when the answer shows that the request was refused before any work, so even a call that is not retry-safe
|
|
218
|
+
* may be sent again.
|
|
219
|
+
*/
|
|
220
|
+
function rejectedBeforeWorkFor(op: Op, code: string, status: number, codeFromStatus: boolean | undefined): boolean {
|
|
221
|
+
if (!REJECTED_BEFORE_WORK.has(code)) return false;
|
|
222
|
+
if (codeFromStatus) return NEVER_REACHED_STATUSES.has(status);
|
|
223
|
+
return !(op.callerKey && MAYBE_FORWARDED.has(code));
|
|
224
|
+
}
|
|
158
225
|
|
|
159
226
|
function trimSlash(s: string): string {
|
|
160
227
|
return s.endsWith("/") ? s.replace(/\/+$/, "") : s;
|
|
@@ -240,15 +307,41 @@ function buildPath(op: Op, values: readonly (string | number)[] | undefined): st
|
|
|
240
307
|
});
|
|
241
308
|
}
|
|
242
309
|
|
|
243
|
-
|
|
310
|
+
const isPlainObject = (v: unknown): v is Record<string, unknown> =>
|
|
311
|
+
typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Date);
|
|
312
|
+
const queryValue = (v: unknown): string => (v instanceof Date ? v.toISOString() : String(v));
|
|
313
|
+
|
|
314
|
+
/** `name[key]=value` for each member of a deepObject value (nested objects add a bracket; arrays repeat the key). */
|
|
315
|
+
function appendDeep(sp: URLSearchParams, name: string, v: unknown): void {
|
|
316
|
+
if (v === undefined || v === null) return;
|
|
317
|
+
if (isPlainObject(v)) for (const [k, x] of Object.entries(v)) appendDeep(sp, `${name}[${k}]`, x);
|
|
318
|
+
else if (Array.isArray(v)) for (const x of v) appendDeep(sp, name, x);
|
|
319
|
+
else sp.append(name, queryValue(v));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The query string. Arrays repeat their key (`style: form`, `explode: true`); an object is sent as `name[key]=value`
|
|
324
|
+
* pairs when the operation declares the parameter `style: deepObject` (`metadata[team]=infra`), and is refused
|
|
325
|
+
* otherwise rather than sent as "[object Object]". A boolean for one of the operation's 0/1 flags is sent as `1` or
|
|
326
|
+
* `0` (`list: true` is `list=1`); other booleans as `true` or `false`.
|
|
327
|
+
*/
|
|
328
|
+
export function buildQuery(
|
|
329
|
+
query: object | undefined,
|
|
330
|
+
extra?: Record<string, string>,
|
|
331
|
+
op?: Pick<Op, "id" | "deepObject" | "flags">,
|
|
332
|
+
): string {
|
|
244
333
|
let sp: URLSearchParams | undefined;
|
|
245
334
|
if (query) {
|
|
246
335
|
for (const [k, v] of Object.entries(query)) {
|
|
247
336
|
if (v === undefined || v === null) continue;
|
|
248
337
|
sp ??= new URLSearchParams();
|
|
249
|
-
if (
|
|
250
|
-
|
|
251
|
-
|
|
338
|
+
if (isPlainObject(v)) {
|
|
339
|
+
if (!op?.deepObject?.includes(k))
|
|
340
|
+
throw new TypeError(`b3os: ${op?.id ?? "request"}: the query parameter ${k} does not take an object`);
|
|
341
|
+
appendDeep(sp, k, v);
|
|
342
|
+
} else if (Array.isArray(v)) for (const x of v) sp.append(k, queryValue(x));
|
|
343
|
+
else if (typeof v === "boolean" && op?.flags?.includes(k)) sp.append(k, v ? "1" : "0");
|
|
344
|
+
else sp.append(k, queryValue(v));
|
|
252
345
|
}
|
|
253
346
|
}
|
|
254
347
|
if (extra) {
|
|
@@ -278,18 +371,36 @@ function connectRefused(err: unknown): boolean {
|
|
|
278
371
|
return code === "ENOENT" || code === "ECONNREFUSED" || code === "EACCES" || code === "EPERM";
|
|
279
372
|
}
|
|
280
373
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
374
|
+
/**
|
|
375
|
+
* Why a call to a caller-keyed operation (`x-b3os-idempotency: caller`, sandboxesExec) was not sent again: `what`
|
|
376
|
+
* happened after the request may have reached the server, so its effect may have happened.
|
|
377
|
+
*/
|
|
378
|
+
function mayHaveRun(op: Op, key: string | undefined, what: string): string | undefined {
|
|
379
|
+
if (!op.callerKey) return undefined;
|
|
380
|
+
const again = key
|
|
381
|
+
? `Call it again with the same idempotencyKey ("${key}") to get its recorded outcome rather than run it twice`
|
|
382
|
+
: "Check its effect before running it again, or pass an idempotencyKey so a second call returns the recorded outcome";
|
|
383
|
+
return `${op.id} may have run: ${what}, so the SDK did not send it again. ${again}`;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** `b3os_sk_…abcd`: enough to tell keys apart in a log, never enough to use one. */
|
|
387
|
+
function redactKey(key: string | null): string | null {
|
|
388
|
+
if (!key) return key;
|
|
389
|
+
const prefix = /^b3os_[a-z]+_/.exec(key)?.[0] ?? "";
|
|
390
|
+
return `${prefix}…${key.slice(-4)}`;
|
|
289
391
|
}
|
|
290
392
|
|
|
291
393
|
export class B3OSCore {
|
|
292
|
-
|
|
394
|
+
/**
|
|
395
|
+
* The key this client sends. Readable, but not enumerable: `JSON.stringify`, `console.log` and `util.inspect` of a
|
|
396
|
+
* client show only its last four characters (see `toJSON`).
|
|
397
|
+
*/
|
|
398
|
+
declare readonly apiKey: string | null;
|
|
399
|
+
/**
|
|
400
|
+
* The sandbox this code runs in (`B3OS_SANDBOX_ID`, set inside every B3OS sandbox with `B3OS_API_URL`), or null.
|
|
401
|
+
* Useful as the id for operations on the sandbox itself: `b3os.sandboxes.createSnapshot(b3os.sandboxId!)`.
|
|
402
|
+
*/
|
|
403
|
+
readonly sandboxId: string | null;
|
|
293
404
|
readonly baseUrl: string;
|
|
294
405
|
readonly maxRetries: number;
|
|
295
406
|
readonly maxRetryDelayMs: number;
|
|
@@ -298,10 +409,67 @@ export class B3OSCore {
|
|
|
298
409
|
private node: Route;
|
|
299
410
|
private readonly headers: Record<string, string>;
|
|
300
411
|
private readonly onResponse: ((info: ResponseInfo) => void) | undefined;
|
|
412
|
+
/** What a call without a key says: why no key was found, and how to give one. */
|
|
413
|
+
readonly #noKeyHint: string;
|
|
414
|
+
/**
|
|
415
|
+
* Whether a call that needs a key and has none is answered here, with no request: only for the B3OS API host
|
|
416
|
+
* (which answers every such call 401), with the SDK's own fetch, outside a B3OS sandbox. Another host may be a mock
|
|
417
|
+
* or a node that answers first, a custom fetch may add the credential, and a sandbox's egress proxy may.
|
|
418
|
+
*/
|
|
419
|
+
readonly #keylessAnswersLocally: boolean;
|
|
420
|
+
|
|
421
|
+
/** What `JSON.stringify(client)` prints: the settings, with the key redacted. */
|
|
422
|
+
toJSON(): Record<string, unknown> {
|
|
423
|
+
return {
|
|
424
|
+
baseUrl: this.baseUrl,
|
|
425
|
+
apiKey: redactKey(this.apiKey),
|
|
426
|
+
sandboxId: this.sandboxId,
|
|
427
|
+
maxRetries: this.maxRetries,
|
|
428
|
+
maxRetryDelayMs: this.maxRetryDelayMs,
|
|
429
|
+
timeoutMs: this.timeoutMs,
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** What `console.log(client)` and `util.inspect(client)` print on Node, Bun and Deno: the same as `toJSON`. */
|
|
434
|
+
[Symbol.for("nodejs.util.inspect.custom")](): Record<string, unknown> {
|
|
435
|
+
return this.toJSON();
|
|
436
|
+
}
|
|
301
437
|
|
|
302
438
|
constructor(options: ClientOptions = {}) {
|
|
303
|
-
|
|
304
|
-
|
|
439
|
+
let apiKey = options.apiKey !== undefined ? options.apiKey : (env("B3OS_API_KEY") ?? null);
|
|
440
|
+
let explicitBase = options.baseUrl ?? env("B3OS_BASE_URL");
|
|
441
|
+
let noKeyHint = "No API key is set: set B3OS_API_KEY or pass apiKey";
|
|
442
|
+
if (options.apiKey === null) {
|
|
443
|
+
noKeyHint =
|
|
444
|
+
"No API key is set: this client was made with apiKey: null. Pass apiKey, or leave it out to use " +
|
|
445
|
+
"B3OS_API_KEY or your b3os login";
|
|
446
|
+
} else if (apiKey === null) {
|
|
447
|
+
// The stored `b3os login`, on a developer's machine only, and only for the API host it was issued by.
|
|
448
|
+
const blocked = loginBlockedBy();
|
|
449
|
+
const login = blocked ? undefined : readStoredLogin();
|
|
450
|
+
const loginBase = login?.baseUrl ?? DEFAULT_BASE_URL;
|
|
451
|
+
if (blocked) {
|
|
452
|
+
noKeyHint += ` (the SDK does not read the b3os login in ${blocked})`;
|
|
453
|
+
} else if (login && (explicitBase === undefined || trimSlash(explicitBase) === loginBase)) {
|
|
454
|
+
apiKey = login.token;
|
|
455
|
+
if (explicitBase === undefined && loginBase !== DEFAULT_BASE_URL) explicitBase = loginBase;
|
|
456
|
+
announceLogin(login, redactKey(login.token) ?? "");
|
|
457
|
+
} else if (login) {
|
|
458
|
+
noKeyHint += ` (your b3os login is for ${loginBase}, not ${trimSlash(explicitBase ?? "")})`;
|
|
459
|
+
} else if (canReadLogin()) {
|
|
460
|
+
noKeyHint = "Not signed in: run b3os login, set B3OS_API_KEY or pass apiKey";
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const onApiHost = trimSlash(explicitBase ?? DEFAULT_BASE_URL) === DEFAULT_BASE_URL;
|
|
464
|
+
this.#noKeyHint = onApiHost ? `${noKeyHint}. Make a key at ${KEYS_PAGE}` : noKeyHint;
|
|
465
|
+
this.#keylessAnswersLocally =
|
|
466
|
+
onApiHost && !options.fetch && !env("B3OS_SANDBOX_ID") && !env("B3OS_API_URL") && !apiKey;
|
|
467
|
+
Object.defineProperty(this, "apiKey", {
|
|
468
|
+
value: apiKey,
|
|
469
|
+
enumerable: false,
|
|
470
|
+
writable: false,
|
|
471
|
+
configurable: false,
|
|
472
|
+
});
|
|
305
473
|
this.baseUrl = trimSlash(explicitBase ?? DEFAULT_BASE_URL);
|
|
306
474
|
this.maxRetries = options.maxRetries ?? 2;
|
|
307
475
|
this.maxRetryDelayMs = options.maxRetryDelayMs ?? 60_000;
|
|
@@ -322,6 +490,8 @@ export class B3OSCore {
|
|
|
322
490
|
const sockEnv = env("B3OS_SOCKET");
|
|
323
491
|
const sockDisabled = options.socketPath === false || sockEnv === "0" || sockEnv === "off";
|
|
324
492
|
const nodeUrl = options.nodeUrl !== undefined ? options.nodeUrl : (env("B3OS_API_URL") ?? null);
|
|
493
|
+
const sbx = env("B3OS_SANDBOX_ID");
|
|
494
|
+
this.sandboxId = sbx && /^sbx_[0-9a-hjkmnp-tv-z]{26}$/.test(sbx) ? sbx : null;
|
|
325
495
|
let node: Route = this.edge;
|
|
326
496
|
if (pref !== "http") {
|
|
327
497
|
const explicitSock =
|
|
@@ -379,8 +549,22 @@ export class B3OSCore {
|
|
|
379
549
|
}
|
|
380
550
|
|
|
381
551
|
/** Where node-served and edge-served operations go (`unix:/run/b3os/b3osd/b3osd.sock`, a URL). */
|
|
382
|
-
get routes(): { node: string; edge: string; nodeKind: Route["kind"] } {
|
|
383
|
-
return {
|
|
552
|
+
get routes(): { node: string; edge: string; nodeKind: Route["kind"]; sandboxId: string | null } {
|
|
553
|
+
return {
|
|
554
|
+
node: this.node.transport.label,
|
|
555
|
+
edge: this.edge.transport.label,
|
|
556
|
+
nodeKind: this.node.kind,
|
|
557
|
+
sandboxId: this.sandboxId,
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* The smallest call the node serves, `GET /v1/ping`: the node's id and clock once the credential is checked,
|
|
563
|
+
* and nothing else. Any valid credential may call it. Its time is the round trip to the node (the "edge round
|
|
564
|
+
* trip" that latency targets are written against); `.withResponse()` gives the Server-Timing phases.
|
|
565
|
+
*/
|
|
566
|
+
ping(options?: RequestOptions): APIPromise<Ping> {
|
|
567
|
+
return this._call<Ping>(PING_OP, {}, options);
|
|
384
568
|
}
|
|
385
569
|
|
|
386
570
|
/** Closes pooled connections (the Unix socket agent). Optional; idle sockets do not keep the process alive. */
|
|
@@ -392,10 +576,10 @@ export class B3OSCore {
|
|
|
392
576
|
// ------------------------------------------------------------------------------------------------------------
|
|
393
577
|
// The request pipeline
|
|
394
578
|
|
|
395
|
-
private headersFor(route: Route, op: Op, o: RequestOptions, accept: string): Record<string, string> {
|
|
579
|
+
private headersFor(route: Route, op: Op, o: RequestOptions, accept: string, traceId: string): Record<string, string> {
|
|
396
580
|
const h: Record<string, string> = { ...this.headers, accept };
|
|
397
581
|
if (route.auth && this.apiKey) h.authorization = `Bearer ${this.apiKey}`;
|
|
398
|
-
|
|
582
|
+
h.traceparent = o.traceparent ?? traceparentIn(traceId);
|
|
399
583
|
if (o.headers) for (const [k, v] of Object.entries(o.headers)) h[k.toLowerCase()] = v;
|
|
400
584
|
void op;
|
|
401
585
|
return h;
|
|
@@ -435,9 +619,7 @@ export class B3OSCore {
|
|
|
435
619
|
? `${op.id} is served by ${this.baseUrl}`
|
|
436
620
|
: `${op.id} is served by ${this.baseUrl}; set B3OS_API_KEY (or pass apiKey) so the SDK can send it there`;
|
|
437
621
|
}
|
|
438
|
-
if (err.status === 401 && route.auth && !this.apiKey)
|
|
439
|
-
return "No API key is configured: set B3OS_API_KEY or pass apiKey";
|
|
440
|
-
}
|
|
622
|
+
if (err.status === 401 && route.auth && !this.apiKey) return this.#noKeyHint;
|
|
441
623
|
if (err.status === 403 && err.reason === "scope" && route.kind === "uds") {
|
|
442
624
|
return (
|
|
443
625
|
"Over the node's Unix socket this process acts as the local principal its uid maps to in b3osd.toml " +
|
|
@@ -451,18 +633,26 @@ export class B3OSCore {
|
|
|
451
633
|
private async send(op: Op, args: CallArgs, o: RequestOptions, x: SendExtras): Promise<Sent> {
|
|
452
634
|
const method = op.method;
|
|
453
635
|
const pathStr = buildPath(op, args.path);
|
|
454
|
-
const query = buildQuery(args.query, x.extraQuery);
|
|
636
|
+
const query = buildQuery(args.query, x.extraQuery, op);
|
|
455
637
|
let idemKey = o.idempotencyKey;
|
|
456
|
-
if (!idemKey && op.idem && method !== "GET") idemKey = uuid();
|
|
638
|
+
if (!idemKey && op.idem && !op.callerKey && method !== "GET") idemKey = uuid();
|
|
457
639
|
const streamBody = typeof ReadableStream !== "undefined" && args.body instanceof ReadableStream;
|
|
640
|
+
// A caller-keyed operation (sandboxesExec) is resent only after an answer that shows it did no work, key or not:
|
|
641
|
+
// the node cannot always tell whether a command it handed on ran, and a retry must never run it twice.
|
|
458
642
|
const retrySafe =
|
|
459
|
-
!streamBody &&
|
|
643
|
+
!streamBody &&
|
|
644
|
+
!op.callerKey &&
|
|
645
|
+
(method === "GET" || method === "HEAD" || method === "PUT" || method === "DELETE" || !!idemKey);
|
|
460
646
|
const maxRetries = streamBody ? 0 : (o.maxRetries ?? this.maxRetries);
|
|
461
647
|
const perAttempt = o.timeoutMs ?? this.timeoutMs ?? (x.sse ? 30_000 : Math.min(op.timeoutMs, 65_000) + 5_000);
|
|
462
648
|
let route = op.edge ? this.edge : this.node;
|
|
463
649
|
let attempt = 0;
|
|
650
|
+
// One trace per call (attempts are spans in it) unless the caller sent a traceparent, which is kept as is. A
|
|
651
|
+
// client made with a `traceparent` header (an MCP server's per-request client) joins that trace: each call is a
|
|
652
|
+
// new span in it, so the header's own span id is never sent twice.
|
|
653
|
+
const traceId = traceIdOf(this.headers.traceparent) ?? newTraceId();
|
|
464
654
|
for (;;) {
|
|
465
|
-
const headers = this.headersFor(route, op, o, x.accept);
|
|
655
|
+
const headers = this.headersFor(route, op, o, x.accept, traceId);
|
|
466
656
|
if (idemKey) headers["idempotency-key"] = idemKey;
|
|
467
657
|
if (op.headerParams && args.headerParams) {
|
|
468
658
|
for (const [k, name] of Object.entries(op.headerParams)) {
|
|
@@ -470,6 +660,17 @@ export class B3OSCore {
|
|
|
470
660
|
if (v !== undefined && v !== null) headers[name.toLowerCase()] = String(v);
|
|
471
661
|
}
|
|
472
662
|
}
|
|
663
|
+
// No key, and the API host would answer 401: say so without the round trip. Scope `public` needs no key, and
|
|
664
|
+
// `unknown` is a raw `request()`, which may be to a route that needs none (the device login).
|
|
665
|
+
if (
|
|
666
|
+
route.auth &&
|
|
667
|
+
this.#keylessAnswersLocally &&
|
|
668
|
+
!headers.authorization &&
|
|
669
|
+
op.scope !== "public" &&
|
|
670
|
+
op.scope !== "unknown"
|
|
671
|
+
) {
|
|
672
|
+
throw new B3OSError({ code: "unauthorized", detail: this.#noKeyHint, operationId: op.id });
|
|
673
|
+
}
|
|
473
674
|
const body = this.encodeBody(op, args, headers);
|
|
474
675
|
const path = pathStr + query;
|
|
475
676
|
const url = route.base + path;
|
|
@@ -501,11 +702,15 @@ export class B3OSCore {
|
|
|
501
702
|
? new B3OSTimeoutError({
|
|
502
703
|
detail: `${op.id} did not answer within ${perAttempt} ms (${route.transport.label})`,
|
|
503
704
|
operationId: op.id,
|
|
705
|
+
hint: mayHaveRun(op, idemKey, "it did not answer in time"),
|
|
504
706
|
cause,
|
|
505
707
|
})
|
|
506
708
|
: new B3OSConnectionError({
|
|
507
709
|
detail: `Could not reach ${route.transport.label}: ${describeErr(cause)}`,
|
|
508
710
|
operationId: op.id,
|
|
711
|
+
hint: connectRefused(cause)
|
|
712
|
+
? undefined
|
|
713
|
+
: mayHaveRun(op, idemKey, "the connection failed after the request may have been sent"),
|
|
509
714
|
cause,
|
|
510
715
|
});
|
|
511
716
|
// Timeouts are not retried: the budget is spent. A refused connection never reached the server.
|
|
@@ -562,7 +767,12 @@ export class B3OSCore {
|
|
|
562
767
|
retryAfterMs,
|
|
563
768
|
operationId: op.id,
|
|
564
769
|
});
|
|
565
|
-
const
|
|
770
|
+
const rejectedBeforeWork = rejectedBeforeWorkFor(op, err0.code, res.status, parsed.codeFromStatus);
|
|
771
|
+
const hint =
|
|
772
|
+
this.hint(route, op, err0) ??
|
|
773
|
+
(rejectedBeforeWork || res.status < 500
|
|
774
|
+
? undefined
|
|
775
|
+
: mayHaveRun(op, idemKey, `the answer was ${res.status} ${err0.code}`));
|
|
566
776
|
const err = hint
|
|
567
777
|
? new B3OSError({
|
|
568
778
|
code: err0.code,
|
|
@@ -580,8 +790,6 @@ export class B3OSCore {
|
|
|
580
790
|
hint,
|
|
581
791
|
})
|
|
582
792
|
: err0;
|
|
583
|
-
const rejectedBeforeWork =
|
|
584
|
-
REJECTED_BEFORE_WORK.has(err.code) && (!parsed.codeFromStatus || NEVER_REACHED_STATUSES.has(res.status));
|
|
585
793
|
const retryable =
|
|
586
794
|
rejectedBeforeWork ||
|
|
587
795
|
(retrySafe && err.retryable && (err.status ?? 0) >= 500 && !NO_RETRY_TIMEOUT_CODES.has(err.code));
|
|
@@ -765,6 +973,18 @@ export class B3OSCore {
|
|
|
765
973
|
});
|
|
766
974
|
}
|
|
767
975
|
|
|
976
|
+
/**
|
|
977
|
+
* @internal A response body read as it arrives (speech with `stream_format: "audio"`): the per-attempt timeout
|
|
978
|
+
* covers the response head only, and nothing is retried once bytes have been handed out.
|
|
979
|
+
*/
|
|
980
|
+
_postBytes(op: Op, args: CallArgs, options: RequestOptions = {}, accept = "application/octet-stream"): ByteStream {
|
|
981
|
+
return new ByteStream(
|
|
982
|
+
op.id,
|
|
983
|
+
(signal) => this.send(op, args, { ...options, signal }, { accept, sse: true }),
|
|
984
|
+
options.signal,
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
|
|
768
988
|
/** @internal A job-creating operation (`x-b3os-job`). */
|
|
769
989
|
_job<J extends JobLike, E>(op: Op, args: CallArgs, options: RequestOptions = {}): JobPromise<J, E> {
|
|
770
990
|
const ops = op.job;
|