@withgateway/sdk 0.4.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/index.d.mts +573 -0
- package/dist/agents/index.d.ts +573 -0
- package/dist/agents/index.js +1443 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/index.mjs +1394 -0
- package/dist/agents/index.mjs.map +1 -0
- package/dist/apiError-DHpfbmdx.d.mts +17 -0
- package/dist/apiError-DHpfbmdx.d.ts +17 -0
- package/dist/cli.js +5655 -1890
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +5769 -2005
- package/dist/cli.mjs.map +1 -1
- package/dist/files/index.d.mts +3 -1
- package/dist/files/index.d.ts +3 -1
- package/dist/files/index.js +418 -120
- package/dist/files/index.js.map +1 -1
- package/dist/files/index.mjs +418 -120
- package/dist/files/index.mjs.map +1 -1
- package/dist/hooks/index.js +346 -136
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +346 -136
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/{index-DUHmS74p.d.ts → index-DIN14Lhg.d.ts} +27 -4
- package/dist/{index-mJhG3vZU.d.mts → index-DuB-eQ0Z.d.mts} +27 -4
- package/dist/index.d.mts +7 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.js +2862 -438
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2820 -431
- package/dist/index.mjs.map +1 -1
- package/dist/media-CQUC7TkS.d.mts +39 -0
- package/dist/media-DQnBloaX.d.ts +39 -0
- package/dist/replay/index.d.mts +1 -1
- package/dist/replay/index.d.ts +1 -1
- package/dist/replay/index.js.map +1 -1
- package/dist/replay/index.mjs.map +1 -1
- package/dist/traces/index.d.mts +3 -1
- package/dist/traces/index.d.ts +3 -1
- package/dist/traces/index.js +418 -120
- package/dist/traces/index.js.map +1 -1
- package/dist/traces/index.mjs +418 -120
- package/dist/traces/index.mjs.map +1 -1
- package/dist/tracing/index.d.mts +3 -2
- package/dist/tracing/index.d.ts +3 -2
- package/dist/tracing/index.js +257 -16
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +257 -16
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/transport-CpRe7zgd.d.mts +123 -0
- package/dist/transport-CpRe7zgd.d.ts +123 -0
- package/dist/worlds/index.d.mts +2086 -1749
- package/dist/worlds/index.d.ts +2086 -1749
- package/dist/worlds/index.js +1471 -269
- package/dist/worlds/index.js.map +1 -1
- package/dist/worlds/index.mjs +1466 -265
- package/dist/worlds/index.mjs.map +1 -1
- package/package.json +6 -1
- package/runtime/gatewaysdk/environment/_bundle.py +4 -6
- package/runtime/gatewaysdk/environment/_world.py +24 -1
- package/runtime/gatewaysdk/environment/bundle_manifest.py +243 -0
- package/runtime/gatewaysdk/environment/runtime.py +45 -2
- package/runtime/gatewaysdk/environment/schema/__main__.py +2 -2
- package/runtime/gatewaysdk/environment/schema/_slack_fidelity.py +8 -23
- package/runtime/gatewaysdk/environment/schema/api.py +27 -7
- package/runtime/gatewaysdk/environment/schema/assertions.py +7 -1
- package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +2 -0
- package/runtime/gatewaysdk/environment/schema/call_capture.py +13 -1
- package/runtime/gatewaysdk/environment/schema/call_log.py +66 -0
- package/runtime/gatewaysdk/environment/schema/call_redaction.py +6 -0
- package/runtime/gatewaysdk/environment/schema/compiler.py +4 -1
- package/runtime/gatewaysdk/environment/schema/conform.py +32 -8
- package/runtime/gatewaysdk/environment/schema/connector.py +5 -0
- package/runtime/gatewaysdk/environment/schema/connectors/custom/handlers.py +2 -1
- package/runtime/gatewaysdk/environment/schema/connectors/github/api/handlers.py +217 -58
- package/runtime/gatewaysdk/environment/schema/connectors/github/connector.toml +9 -19
- package/runtime/gatewaysdk/environment/schema/connectors/github/scenarios/default/seed/github_webhooks.json +10 -2
- package/runtime/gatewaysdk/environment/schema/connectors/github/tests/webhooks.json +47 -0
- package/runtime/gatewaysdk/environment/schema/connectors/github/world.json +21 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/api/handlers.py +185 -18
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/connector.toml +29 -14
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/provenance.json +43 -12
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/scenarios/default/seed/jamf_webhooks.json +2 -2
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/scope.toml +4 -17
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/tests/webhooks.json +107 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/vendor-openapi.json +1 -1
- package/runtime/gatewaysdk/environment/schema/connectors/linear/api/handlers.py +85 -1
- package/runtime/gatewaysdk/environment/schema/connectors/linear/connector.toml +19 -31
- package/runtime/gatewaysdk/environment/schema/connectors/linear/provenance.json +18 -8
- package/runtime/gatewaysdk/environment/schema/connectors/linear/scenarios/default/seed/webhooks.json +19 -3
- package/runtime/gatewaysdk/environment/schema/connectors/linear/tests/test_webhooks.py +71 -0
- package/runtime/gatewaysdk/environment/schema/database/base.py +2 -2
- package/runtime/gatewaysdk/environment/schema/database/guard.py +2 -1
- package/runtime/gatewaysdk/environment/schema/handler_errors.py +27 -1
- package/runtime/gatewaysdk/environment/schema/host.py +74 -11
- package/runtime/gatewaysdk/environment/schema/host_browser.py +48 -0
- package/runtime/gatewaysdk/environment/schema/host_control.py +33 -2
- package/runtime/gatewaysdk/environment/schema/host_handoff.py +4 -1
- package/runtime/gatewaysdk/environment/schema/host_requests.py +14 -0
- package/runtime/gatewaysdk/environment/schema/host_session.py +13 -2
- package/runtime/gatewaysdk/environment/schema/host_surfaces.py +6 -0
- package/runtime/gatewaysdk/environment/schema/host_versions.py +96 -19
- package/runtime/gatewaysdk/environment/schema/journal.py +10 -4
- package/runtime/gatewaysdk/environment/schema/local.py +33 -7
- package/runtime/gatewaysdk/environment/schema/openapi_schemas.py +20 -5
- package/runtime/gatewaysdk/environment/schema/page_checks.py +123 -0
- package/runtime/gatewaysdk/environment/schema/platform.py +33 -480
- package/runtime/gatewaysdk/environment/schema/platform_templates.py +332 -0
- package/runtime/gatewaysdk/environment/schema/request_ledger.py +4 -1
- package/runtime/gatewaysdk/environment/schema/run_parameters.py +25 -0
- package/runtime/gatewaysdk/environment/schema/runtime.py +49 -4
- package/runtime/gatewaysdk/environment/schema/scaffold.py +369 -218
- package/runtime/gatewaysdk/environment/schema/session_files.py +13 -2
- package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +1 -1
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/CONNECTORS.md +3 -3
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +20 -24
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +11 -11
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +9 -9
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +45 -12
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +40 -9
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/journal-checks.md +17 -2
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/new-world-sandbox.md +1 -1
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/ui-map.md +3 -2
- package/runtime/gatewaysdk/environment/schema/slack.py +55 -10
- package/runtime/gatewaysdk/environment/schema/snapshot.py +30 -4
- package/runtime/gatewaysdk/environment/schema/tasks.py +25 -2
- package/runtime/gatewaysdk/environment/schema/template_record.py +131 -0
- package/runtime/gatewaysdk/environment/schema/tools_world.py +26 -5
- package/runtime/gatewaysdk/environment/schema/ui.py +54 -33
- package/runtime/gatewaysdk/environment/schema/ui_interactions.py +419 -0
- package/runtime/gatewaysdk/environment/schema/ui_recorder.js +895 -0
- package/runtime/gatewaysdk/environment/schema/ui_replay.py +366 -0
- package/runtime/gatewaysdk/environment/schema/ui_replay_record.LICENSE +25 -0
- package/runtime/gatewaysdk/environment/schema/ui_replay_record.js +109 -0
- package/runtime/gatewaysdk/environment/schema/validation.py +10 -1
- package/runtime/gatewaysdk/environment/schema/verbs.py +6 -0
- package/runtime/gatewaysdk/environment/schema/webhooks.py +687 -0
- package/runtime/gatewaysdk/environment/schema/worker.py +5 -1
- package/runtime/gatewaysdk/environment/schema/workers.py +8 -1
- package/runtime/gatewaysdk/environment/schema/world_host.py +64 -18
- package/runtime/gatewaysdk/environment/schema/world_tests.py +74 -0
- package/runtime/gatewaysdk/environment/secret_values.py +1 -1
- package/runtime/gatewaysdk/environment/world.py +34 -43
- package/runtime/gatewaysdk/environment/world_session_contract.py +7 -34
- package/skills/benchmarks-getting-started/SKILL.md +1 -1
- package/skills/connector-schema-authoring/CONNECTORS.md +3 -3
- package/skills/connector-schema-authoring/SKILL.md +20 -24
- package/skills/connector-schema-authoring/connector-worlds.md +155 -13
- package/skills/connector-schema-authoring/world-schema.md +36 -29
- package/skills/world-data-ingestion/SKILL.md +11 -11
- package/skills/worlds-getting-started/SKILL.md +9 -9
- package/skills/worlds-getting-started/references/cli-mcp-map.md +45 -12
- package/skills/worlds-getting-started/references/gotchas.md +40 -9
- package/skills/worlds-getting-started/references/journal-checks.md +17 -2
- package/skills/worlds-getting-started/references/new-world-sandbox.md +1 -1
- package/skills/worlds-getting-started/references/ui-map.md +3 -2
- package/dist/media-BIApiPzS.d.mts +0 -102
- package/dist/media-BIApiPzS.d.ts +0 -102
package/dist/tracing/index.mjs
CHANGED
|
@@ -20,7 +20,7 @@ var require_package = __commonJS({
|
|
|
20
20
|
"package.json"(exports, module) {
|
|
21
21
|
module.exports = {
|
|
22
22
|
name: "@withgateway/sdk",
|
|
23
|
-
version: "0.
|
|
23
|
+
version: "0.9.1",
|
|
24
24
|
description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
|
|
25
25
|
main: "./dist/index.js",
|
|
26
26
|
module: "./dist/index.mjs",
|
|
@@ -101,6 +101,11 @@ var require_package = __commonJS({
|
|
|
101
101
|
import: "./dist/hooks/index.mjs",
|
|
102
102
|
require: "./dist/hooks/index.js"
|
|
103
103
|
},
|
|
104
|
+
"./agents": {
|
|
105
|
+
types: "./dist/agents/index.d.ts",
|
|
106
|
+
import: "./dist/agents/index.mjs",
|
|
107
|
+
require: "./dist/agents/index.js"
|
|
108
|
+
},
|
|
104
109
|
"./cli": {
|
|
105
110
|
types: "./dist/cli.d.ts",
|
|
106
111
|
import: "./dist/cli.mjs",
|
|
@@ -1726,6 +1731,106 @@ async function* observeClaude(stream, options) {
|
|
|
1726
1731
|
// src/tracing/media.ts
|
|
1727
1732
|
import { createHash as createHash2 } from "crypto";
|
|
1728
1733
|
|
|
1734
|
+
// src/worlds/apiError.ts
|
|
1735
|
+
var ApiError = class extends Error {
|
|
1736
|
+
constructor(status, method, path, text) {
|
|
1737
|
+
const parsed = parseErrorBody(text, status);
|
|
1738
|
+
super(`${method} ${path} -> ${status}: ${parsed.detail}`);
|
|
1739
|
+
this.status = status;
|
|
1740
|
+
this.text = text;
|
|
1741
|
+
this.name = "ApiError";
|
|
1742
|
+
this.code = parsed.code;
|
|
1743
|
+
}
|
|
1744
|
+
status;
|
|
1745
|
+
text;
|
|
1746
|
+
code;
|
|
1747
|
+
};
|
|
1748
|
+
var DETAIL_LIMIT = 600;
|
|
1749
|
+
var EDGE_ANSWERS = {
|
|
1750
|
+
502: "Bad Gateway from the load balancer in front of the platform, not the platform's answer: the web server dropped the connection or restarted mid-request. Retry; if it repeats, the platform is restarting or overloaded.",
|
|
1751
|
+
503: "Service Unavailable from the load balancer in front of the platform, not the platform's answer: no web server was ready to take the request (a deploy or a restart). Retry in a minute.",
|
|
1752
|
+
504: "Gateway Time-out from the load balancer in front of the platform, not the platform's answer: the request sent nothing back within the load balancer's idle timeout (60 s unless the deployment raises it: Helm ingress.idleTimeoutSeconds) and was cut there, though the platform may still be running it. Check what it changed before you retry."
|
|
1753
|
+
};
|
|
1754
|
+
var EDGE_FAILURE = "The platform failed without its JSON error body: a proxy in front of it answered, or the server stopped before its error handler ran. Retry; if it repeats, report the request and the time.";
|
|
1755
|
+
var EDGE_PAGE = "An HTML page answered instead of the platform's JSON: a proxy in front of the platform, or the configured host is not the platform.";
|
|
1756
|
+
function edgeAnswer(text, status) {
|
|
1757
|
+
if (status !== void 0 && status >= 500) return EDGE_ANSWERS[status] ?? EDGE_FAILURE;
|
|
1758
|
+
return /^\s*</.test(text) ? EDGE_PAGE : null;
|
|
1759
|
+
}
|
|
1760
|
+
function parseErrorBody(text, status) {
|
|
1761
|
+
const raw = { detail: text.slice(0, DETAIL_LIMIT), code: null };
|
|
1762
|
+
let parsed;
|
|
1763
|
+
try {
|
|
1764
|
+
parsed = JSON.parse(text);
|
|
1765
|
+
} catch {
|
|
1766
|
+
return { detail: (edgeAnswer(text, status) ?? text).slice(0, DETAIL_LIMIT), code: null };
|
|
1767
|
+
}
|
|
1768
|
+
if (parsed === null || typeof parsed !== "object") return raw;
|
|
1769
|
+
const { message, code, error } = parsed;
|
|
1770
|
+
const stableCode = typeof code === "string" ? code : null;
|
|
1771
|
+
if (typeof message !== "string") return { ...raw, code: stableCode };
|
|
1772
|
+
const fields = invalidFields(error);
|
|
1773
|
+
const said = fields ? `${message}: ${fields}` : message;
|
|
1774
|
+
if (stableCode === null) return { detail: said.slice(0, DETAIL_LIMIT), code: null };
|
|
1775
|
+
return {
|
|
1776
|
+
detail: `${said} (${stableCode})`.slice(0, DETAIL_LIMIT),
|
|
1777
|
+
code: stableCode
|
|
1778
|
+
};
|
|
1779
|
+
}
|
|
1780
|
+
function invalidFields(error) {
|
|
1781
|
+
const issues = Array.isArray(error) ? error : [];
|
|
1782
|
+
return issues.filter(
|
|
1783
|
+
(issue) => issue !== null && typeof issue === "object" && typeof issue.message === "string"
|
|
1784
|
+
).map((issue) => {
|
|
1785
|
+
const path = Array.isArray(issue.path) ? issue.path.map(String).join(".") : "";
|
|
1786
|
+
return path ? `${path}: ${issue.message}` : issue.message;
|
|
1787
|
+
}).join("; ");
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
// src/worlds/personalToken.ts
|
|
1791
|
+
var TOKEN_NOT_PERSONAL = "GATEWAY_TOKEN takes a personal access token: pat_ followed by letters and digits only.";
|
|
1792
|
+
var TOKEN_NEEDS_PROJECT = "GATEWAY_TOKEN acts in the project GATEWAY_PROJECT_ID names: set it to the official project's id (letters, digits, - and _).";
|
|
1793
|
+
var TOKEN_NEEDS_HOST = "GATEWAY_TOKEN goes only to the host GATEWAY_HOST names: set GATEWAY_HOST.";
|
|
1794
|
+
var TOKEN_OTHER_HOST = "GATEWAY_TOKEN goes only to the host GATEWAY_HOST names, and this call names another host: point the call at GATEWAY_HOST or unset GATEWAY_TOKEN.";
|
|
1795
|
+
var TOKEN_BESIDE_KEYS = "GATEWAY_TOKEN is set beside a project key (GATEWAY_PUBLIC_KEY, GATEWAY_SECRET_KEY, GATEWAY_API_KEY or a key passed in): unset one of them. Neither replaces the other.";
|
|
1796
|
+
var PERSONAL = /^pat_[A-Za-z0-9]+$/;
|
|
1797
|
+
var PROJECT = /^[A-Za-z0-9_-]+$/;
|
|
1798
|
+
var ENV_KEYS = ["GATEWAY_PUBLIC_KEY", "GATEWAY_SECRET_KEY", "GATEWAY_API_KEY"];
|
|
1799
|
+
var PersonalTokenRefused = class extends Error {
|
|
1800
|
+
};
|
|
1801
|
+
var bare = (host) => host.replace(/\/+$/, "");
|
|
1802
|
+
var PersonalToken = class _PersonalToken {
|
|
1803
|
+
constructor(value, project, host) {
|
|
1804
|
+
this.value = value;
|
|
1805
|
+
this.project = project;
|
|
1806
|
+
this.host = host;
|
|
1807
|
+
}
|
|
1808
|
+
value;
|
|
1809
|
+
project;
|
|
1810
|
+
host;
|
|
1811
|
+
static fromEnv(env, request = {}) {
|
|
1812
|
+
const value = env.GATEWAY_TOKEN;
|
|
1813
|
+
if (!value) return null;
|
|
1814
|
+
if (request.keys || ENV_KEYS.some((name) => env[name])) throw new PersonalTokenRefused(TOKEN_BESIDE_KEYS);
|
|
1815
|
+
const host = bare(env.GATEWAY_HOST ?? "");
|
|
1816
|
+
if (!host) throw new PersonalTokenRefused(TOKEN_NEEDS_HOST);
|
|
1817
|
+
if (request.host && bare(request.host) !== host) throw new PersonalTokenRefused(TOKEN_OTHER_HOST);
|
|
1818
|
+
if (!PERSONAL.test(value)) throw new PersonalTokenRefused(TOKEN_NOT_PERSONAL);
|
|
1819
|
+
const project = env.GATEWAY_PROJECT_ID ?? "";
|
|
1820
|
+
if (!PROJECT.test(project)) throw new PersonalTokenRefused(TOKEN_NEEDS_PROJECT);
|
|
1821
|
+
return new _PersonalToken(value, project, host);
|
|
1822
|
+
}
|
|
1823
|
+
toJSON() {
|
|
1824
|
+
return { project: this.project, host: this.host };
|
|
1825
|
+
}
|
|
1826
|
+
[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
|
|
1827
|
+
return `PersonalToken { project: ${this.project}, host: ${this.host} }`;
|
|
1828
|
+
}
|
|
1829
|
+
static scrubbed(env) {
|
|
1830
|
+
return Object.fromEntries(Object.entries(env).filter(([key]) => key !== "GATEWAY_TOKEN"));
|
|
1831
|
+
}
|
|
1832
|
+
};
|
|
1833
|
+
|
|
1729
1834
|
// src/worlds/transport.ts
|
|
1730
1835
|
var PLATFORM_TIMEOUT_ENV = "GATEWAY_TIMEOUT_MS";
|
|
1731
1836
|
var PLATFORM_TIMEOUT_DEFAULT_MS = 12e4;
|
|
@@ -1773,6 +1878,13 @@ var TransportCause = class _TransportCause {
|
|
|
1773
1878
|
`Retry; if the platform is slow, raise ${PLATFORM_TIMEOUT_ENV} (milliseconds per request).`
|
|
1774
1879
|
);
|
|
1775
1880
|
}
|
|
1881
|
+
static idle(idleMs) {
|
|
1882
|
+
return new _TransportCause(
|
|
1883
|
+
`no data for ${idleMs / 1e3}s`,
|
|
1884
|
+
null,
|
|
1885
|
+
"The stream resumes from its last event; if it repeats, check the network between here and the platform."
|
|
1886
|
+
);
|
|
1887
|
+
}
|
|
1776
1888
|
static of(error) {
|
|
1777
1889
|
const cause = error instanceof Error && error.cause instanceof Error ? error.cause : void 0;
|
|
1778
1890
|
const code = cause && "code" in cause && typeof cause.code === "string" ? cause.code : null;
|
|
@@ -1848,45 +1960,174 @@ var Attempt = class _Attempt {
|
|
|
1848
1960
|
throw this.error;
|
|
1849
1961
|
}
|
|
1850
1962
|
};
|
|
1963
|
+
var PLATFORM_STREAM_IDLE_MS = 6e4;
|
|
1964
|
+
var REFUSAL_TEXT_LIMIT = 64 * 1024;
|
|
1965
|
+
var KEYED_REDIRECTS = "manual";
|
|
1966
|
+
function platformAuthorization(endpoint, url) {
|
|
1967
|
+
const { token } = endpoint;
|
|
1968
|
+
if (!token)
|
|
1969
|
+
return { authorization: "Basic " + Buffer.from(`${endpoint.publicKey}:${endpoint.secretKey}`).toString("base64") };
|
|
1970
|
+
if (endpoint.publicKey || endpoint.secretKey) throw new PersonalTokenRefused(TOKEN_BESIDE_KEYS);
|
|
1971
|
+
if (!url.startsWith(`${token.host}/`)) throw new PersonalTokenRefused(TOKEN_OTHER_HOST);
|
|
1972
|
+
return { authorization: `Bearer ${token.value}`, "x-gateway-project": token.project };
|
|
1973
|
+
}
|
|
1851
1974
|
var PlatformTransport = class {
|
|
1975
|
+
#endpoint;
|
|
1976
|
+
#timeoutMs;
|
|
1852
1977
|
constructor(endpoint, timeoutMs = platformTimeoutMs()) {
|
|
1853
|
-
this
|
|
1854
|
-
this
|
|
1978
|
+
this.#endpoint = endpoint;
|
|
1979
|
+
this.#timeoutMs = timeoutMs;
|
|
1855
1980
|
}
|
|
1856
|
-
endpoint;
|
|
1857
|
-
timeoutMs;
|
|
1858
1981
|
/** A request that legitimately takes longer (a sandbox exec, a world create) waits its own budget; every other one waits the default. */
|
|
1859
1982
|
budget(ownMs) {
|
|
1860
|
-
return Math.max(ownMs ?? 0, this
|
|
1983
|
+
return Math.max(ownMs ?? 0, this.#timeoutMs);
|
|
1861
1984
|
}
|
|
1985
|
+
/** One request; the caller's `signal` aborts it in flight and throws the signal's reason, never an `UnreachableError`. */
|
|
1862
1986
|
async send(method, route, body, opts = {}) {
|
|
1863
1987
|
const timeoutMs = this.budget(opts.timeoutMs);
|
|
1864
|
-
return (opts.retry ?? RetryPolicy.NONE).run(() => this.once(method, route, body, timeoutMs));
|
|
1988
|
+
return (opts.retry ?? RetryPolicy.NONE).run(() => this.once(method, route, body, timeoutMs, opts.signal));
|
|
1989
|
+
}
|
|
1990
|
+
/** `send`, then the 2xx body as JSON; any other answer throws `ApiError` with its status and stable `code`. */
|
|
1991
|
+
async json(method, route, body, opts = {}) {
|
|
1992
|
+
const reply = await this.send(method, route, body, opts);
|
|
1993
|
+
if (!reply.ok) throw new ApiError(reply.status, method, route, reply.text);
|
|
1994
|
+
return JSON.parse(reply.text);
|
|
1995
|
+
}
|
|
1996
|
+
/** The streaming twin of `send` for a `GET` event stream: nothing is buffered, `after` travels as `Last-Event-ID`, and each wait for the platform (the headers, every chunk) may last `idleMs` (default 60 s) instead of the request budget. */
|
|
1997
|
+
async stream(route, opts = {}) {
|
|
1998
|
+
const watch = new StreamWatch(opts.signal, opts.idleMs ?? PLATFORM_STREAM_IDLE_MS, this.#endpoint.host, route);
|
|
1999
|
+
const headers = this.headers(route, "text/event-stream", opts.after ? { "last-event-id": opts.after } : {});
|
|
2000
|
+
try {
|
|
2001
|
+
const response = await watch.within(
|
|
2002
|
+
() => fetch(this.url(route), { method: "GET", headers, signal: watch.signal, redirect: KEYED_REDIRECTS })
|
|
2003
|
+
);
|
|
2004
|
+
return new PlatformStream(response, watch);
|
|
2005
|
+
} catch (error) {
|
|
2006
|
+
watch.close();
|
|
2007
|
+
throw error;
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
url(route) {
|
|
2011
|
+
return `${this.#endpoint.host}${route}`;
|
|
2012
|
+
}
|
|
2013
|
+
headers(route, accept, extra = {}) {
|
|
2014
|
+
return {
|
|
2015
|
+
"content-type": "application/json",
|
|
2016
|
+
accept,
|
|
2017
|
+
...platformAuthorization(this.#endpoint, this.url(route)),
|
|
2018
|
+
...extra
|
|
2019
|
+
};
|
|
1865
2020
|
}
|
|
1866
|
-
async once(method, route, body, timeoutMs) {
|
|
2021
|
+
async once(method, route, body, timeoutMs, caller) {
|
|
2022
|
+
if (caller?.aborted) throw caller.reason;
|
|
1867
2023
|
const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
|
|
2024
|
+
const headers = this.headers(route, "application/json");
|
|
1868
2025
|
const controller = new AbortController();
|
|
1869
2026
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
2027
|
+
const relay = () => controller.abort(caller?.reason);
|
|
2028
|
+
caller?.addEventListener("abort", relay, { once: true });
|
|
1870
2029
|
try {
|
|
1871
|
-
const res = await fetch(
|
|
2030
|
+
const res = await fetch(this.url(route), {
|
|
1872
2031
|
method,
|
|
1873
|
-
headers
|
|
1874
|
-
"content-type": "application/json",
|
|
1875
|
-
accept: "application/json",
|
|
1876
|
-
authorization: "Basic " + Buffer.from(`${this.endpoint.publicKey}:${this.endpoint.secretKey}`).toString("base64")
|
|
1877
|
-
},
|
|
2032
|
+
headers,
|
|
1878
2033
|
...payload,
|
|
1879
|
-
signal: controller.signal
|
|
2034
|
+
signal: controller.signal,
|
|
2035
|
+
redirect: KEYED_REDIRECTS
|
|
1880
2036
|
});
|
|
1881
2037
|
return { status: res.status, ok: res.ok, text: await res.text() };
|
|
1882
2038
|
} catch (error) {
|
|
2039
|
+
if (caller?.aborted) throw caller.reason;
|
|
1883
2040
|
const cause = controller.signal.aborted ? TransportCause.timeout(timeoutMs) : TransportCause.of(error);
|
|
1884
|
-
throw new UnreachableError(this
|
|
2041
|
+
throw new UnreachableError(this.#endpoint.host, method, route, cause);
|
|
1885
2042
|
} finally {
|
|
1886
2043
|
clearTimeout(timer);
|
|
2044
|
+
caller?.removeEventListener("abort", relay);
|
|
1887
2045
|
}
|
|
1888
2046
|
}
|
|
1889
2047
|
};
|
|
2048
|
+
var StreamWatch = class {
|
|
2049
|
+
constructor(caller, idleMs, host, route) {
|
|
2050
|
+
this.idleMs = idleMs;
|
|
2051
|
+
this.host = host;
|
|
2052
|
+
this.route = route;
|
|
2053
|
+
this.#caller = caller;
|
|
2054
|
+
caller?.addEventListener("abort", this.#relay, { once: true });
|
|
2055
|
+
if (caller?.aborted) this.#relay();
|
|
2056
|
+
}
|
|
2057
|
+
idleMs;
|
|
2058
|
+
host;
|
|
2059
|
+
route;
|
|
2060
|
+
#controller = new AbortController();
|
|
2061
|
+
#caller;
|
|
2062
|
+
#relay = () => this.#controller.abort(this.#caller?.reason);
|
|
2063
|
+
#idled = false;
|
|
2064
|
+
get signal() {
|
|
2065
|
+
return this.#controller.signal;
|
|
2066
|
+
}
|
|
2067
|
+
async within(wait) {
|
|
2068
|
+
const timer = setTimeout(() => this.idle(), this.idleMs);
|
|
2069
|
+
try {
|
|
2070
|
+
return await wait();
|
|
2071
|
+
} catch (error) {
|
|
2072
|
+
throw this.failure(error);
|
|
2073
|
+
} finally {
|
|
2074
|
+
clearTimeout(timer);
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
close() {
|
|
2078
|
+
this.#caller?.removeEventListener("abort", this.#relay);
|
|
2079
|
+
this.#controller.abort();
|
|
2080
|
+
}
|
|
2081
|
+
idle() {
|
|
2082
|
+
this.#idled = true;
|
|
2083
|
+
this.#controller.abort();
|
|
2084
|
+
}
|
|
2085
|
+
failure(error) {
|
|
2086
|
+
if (this.#caller?.aborted) return this.#caller.reason;
|
|
2087
|
+
const cause = this.#idled ? TransportCause.idle(this.idleMs) : TransportCause.of(error);
|
|
2088
|
+
return new UnreachableError(this.host, "GET", this.route, cause);
|
|
2089
|
+
}
|
|
2090
|
+
};
|
|
2091
|
+
var PlatformStream = class {
|
|
2092
|
+
#response;
|
|
2093
|
+
#watch;
|
|
2094
|
+
constructor(response, watch) {
|
|
2095
|
+
this.#response = response;
|
|
2096
|
+
this.#watch = watch;
|
|
2097
|
+
}
|
|
2098
|
+
get status() {
|
|
2099
|
+
return this.#response.status;
|
|
2100
|
+
}
|
|
2101
|
+
get ok() {
|
|
2102
|
+
return this.#response.ok;
|
|
2103
|
+
}
|
|
2104
|
+
/** The body as text, for a refusal: at most the first 64 KiB of characters; closes the stream. */
|
|
2105
|
+
async text() {
|
|
2106
|
+
const decoder = new TextDecoder();
|
|
2107
|
+
let text = "";
|
|
2108
|
+
for await (const chunk of this.chunks()) {
|
|
2109
|
+
text += decoder.decode(chunk, { stream: true });
|
|
2110
|
+
if (text.length >= REFUSAL_TEXT_LIMIT) return text.slice(0, REFUSAL_TEXT_LIMIT);
|
|
2111
|
+
}
|
|
2112
|
+
return text + decoder.decode();
|
|
2113
|
+
}
|
|
2114
|
+
/** The body's bytes as they arrive; the idle budget runs only while waiting for the platform, never while the caller holds a chunk. */
|
|
2115
|
+
async *chunks() {
|
|
2116
|
+
const reader = this.#response.body?.getReader();
|
|
2117
|
+
try {
|
|
2118
|
+
for (; ; ) {
|
|
2119
|
+
const next = reader ? await this.#watch.within(() => reader.read()) : { done: true };
|
|
2120
|
+
if (next.done) return;
|
|
2121
|
+
yield next.value;
|
|
2122
|
+
}
|
|
2123
|
+
} finally {
|
|
2124
|
+
this.close();
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
close() {
|
|
2128
|
+
this.#watch.close();
|
|
2129
|
+
}
|
|
2130
|
+
};
|
|
1890
2131
|
|
|
1891
2132
|
// src/tracing/media.ts
|
|
1892
2133
|
var MEDIA_PATH = "/api/public/media";
|