@withgateway/sdk 0.6.0 → 0.9.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.
Files changed (150) hide show
  1. package/dist/agents/index.d.mts +581 -0
  2. package/dist/agents/index.d.ts +581 -0
  3. package/dist/agents/index.js +1473 -0
  4. package/dist/agents/index.js.map +1 -0
  5. package/dist/agents/index.mjs +1424 -0
  6. package/dist/agents/index.mjs.map +1 -0
  7. package/dist/apiError-DHpfbmdx.d.mts +17 -0
  8. package/dist/apiError-DHpfbmdx.d.ts +17 -0
  9. package/dist/cli.js +11611 -6845
  10. package/dist/cli.js.map +1 -1
  11. package/dist/cli.mjs +10090 -5325
  12. package/dist/cli.mjs.map +1 -1
  13. package/dist/files/index.d.mts +3 -1
  14. package/dist/files/index.d.ts +3 -1
  15. package/dist/files/index.js +371 -102
  16. package/dist/files/index.js.map +1 -1
  17. package/dist/files/index.mjs +371 -102
  18. package/dist/files/index.mjs.map +1 -1
  19. package/dist/hooks/index.js +346 -136
  20. package/dist/hooks/index.js.map +1 -1
  21. package/dist/hooks/index.mjs +346 -136
  22. package/dist/hooks/index.mjs.map +1 -1
  23. package/dist/{index-LA-PMRJT.d.ts → index-DIN14Lhg.d.ts} +1 -1
  24. package/dist/{index-Di9nNqOY.d.mts → index-DuB-eQ0Z.d.mts} +1 -1
  25. package/dist/index.d.mts +7 -4
  26. package/dist/index.d.ts +7 -4
  27. package/dist/index.js +3177 -748
  28. package/dist/index.js.map +1 -1
  29. package/dist/index.mjs +2987 -590
  30. package/dist/index.mjs.map +1 -1
  31. package/dist/media-CQUC7TkS.d.mts +39 -0
  32. package/dist/media-DQnBloaX.d.ts +39 -0
  33. package/dist/traces/index.d.mts +3 -1
  34. package/dist/traces/index.d.ts +3 -1
  35. package/dist/traces/index.js +371 -102
  36. package/dist/traces/index.js.map +1 -1
  37. package/dist/traces/index.mjs +371 -102
  38. package/dist/traces/index.mjs.map +1 -1
  39. package/dist/tracing/index.d.mts +3 -2
  40. package/dist/tracing/index.d.ts +3 -2
  41. package/dist/tracing/index.js +257 -16
  42. package/dist/tracing/index.js.map +1 -1
  43. package/dist/tracing/index.mjs +257 -16
  44. package/dist/tracing/index.mjs.map +1 -1
  45. package/dist/transport-CpRe7zgd.d.mts +123 -0
  46. package/dist/transport-CpRe7zgd.d.ts +123 -0
  47. package/dist/worlds/index.d.mts +1986 -1746
  48. package/dist/worlds/index.d.ts +1986 -1746
  49. package/dist/worlds/index.js +1470 -290
  50. package/dist/worlds/index.js.map +1 -1
  51. package/dist/worlds/index.mjs +1465 -286
  52. package/dist/worlds/index.mjs.map +1 -1
  53. package/package.json +6 -1
  54. package/runtime/gatewaysdk/environment/bundle_manifest.py +531 -0
  55. package/runtime/gatewaysdk/environment/factory_documents.py +670 -0
  56. package/runtime/gatewaysdk/environment/factory_rules.py +973 -0
  57. package/runtime/gatewaysdk/environment/factory_shapes.py +253 -0
  58. package/runtime/gatewaysdk/environment/factory_spine.py +1155 -0
  59. package/runtime/gatewaysdk/environment/factory_spine_shapes.py +480 -0
  60. package/runtime/gatewaysdk/environment/schema/__main__.py +2 -2
  61. package/runtime/gatewaysdk/environment/schema/_slack_fidelity.py +8 -23
  62. package/runtime/gatewaysdk/environment/schema/admission.py +61 -59
  63. package/runtime/gatewaysdk/environment/schema/api.py +3 -4
  64. package/runtime/gatewaysdk/environment/schema/assertions.py +7 -1
  65. package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +1 -0
  66. package/runtime/gatewaysdk/environment/schema/call_log.py +20 -0
  67. package/runtime/gatewaysdk/environment/schema/cast.py +133 -67
  68. package/runtime/gatewaysdk/environment/schema/compiler.py +16 -17
  69. package/runtime/gatewaysdk/environment/schema/database/base.py +2 -2
  70. package/runtime/gatewaysdk/environment/schema/handler_errors.py +58 -4
  71. package/runtime/gatewaysdk/environment/schema/host.py +94 -12
  72. package/runtime/gatewaysdk/environment/schema/host_browser.py +48 -0
  73. package/runtime/gatewaysdk/environment/schema/host_control.py +35 -2
  74. package/runtime/gatewaysdk/environment/schema/host_handoff.py +4 -1
  75. package/runtime/gatewaysdk/environment/schema/host_loops.py +27 -4
  76. package/runtime/gatewaysdk/environment/schema/host_metrics.py +69 -0
  77. package/runtime/gatewaysdk/environment/schema/host_requests.py +35 -0
  78. package/runtime/gatewaysdk/environment/schema/host_session.py +20 -2
  79. package/runtime/gatewaysdk/environment/schema/host_surfaces.py +6 -0
  80. package/runtime/gatewaysdk/environment/schema/host_versions.py +100 -19
  81. package/runtime/gatewaysdk/environment/schema/host_workers.py +172 -50
  82. package/runtime/gatewaysdk/environment/schema/ingest.py +12 -3
  83. package/runtime/gatewaysdk/environment/schema/journal.py +7 -2
  84. package/runtime/gatewaysdk/environment/schema/local.py +32 -7
  85. package/runtime/gatewaysdk/environment/schema/mapping.py +1 -1
  86. package/runtime/gatewaysdk/environment/schema/measured_tests.py +187 -0
  87. package/runtime/gatewaysdk/environment/schema/page_checks.py +123 -0
  88. package/runtime/gatewaysdk/environment/schema/platform.py +80 -270
  89. package/runtime/gatewaysdk/environment/schema/platform_query.py +4 -3
  90. package/runtime/gatewaysdk/environment/schema/platform_templates.py +420 -0
  91. package/runtime/gatewaysdk/environment/schema/process_memory.py +76 -0
  92. package/runtime/gatewaysdk/environment/schema/redaction.py +13 -0
  93. package/runtime/gatewaysdk/environment/schema/request_ledger.py +4 -1
  94. package/runtime/gatewaysdk/environment/schema/run_parameters.py +25 -0
  95. package/runtime/gatewaysdk/environment/schema/runtime.py +57 -5
  96. package/runtime/gatewaysdk/environment/schema/scaffold.py +386 -219
  97. package/runtime/gatewaysdk/environment/schema/session_files.py +13 -2
  98. package/runtime/gatewaysdk/environment/schema/shape_catalog.py +493 -0
  99. package/runtime/gatewaysdk/environment/schema/shape_declarations.py +219 -0
  100. package/runtime/gatewaysdk/environment/schema/skill.py +1 -0
  101. package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +1 -1
  102. package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +2 -2
  103. package/runtime/gatewaysdk/environment/schema/skills/factory-orchestrator/SKILL.md +147 -0
  104. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +5 -1
  105. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +8 -6
  106. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +9 -9
  107. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +33 -2
  108. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +6 -6
  109. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/journal-checks.md +17 -2
  110. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/new-world-sandbox.md +1 -1
  111. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/shape-catalog.md +59 -0
  112. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/ui-map.md +3 -2
  113. package/runtime/gatewaysdk/environment/schema/slack.py +55 -10
  114. package/runtime/gatewaysdk/environment/schema/snapshot.py +28 -2
  115. package/runtime/gatewaysdk/environment/schema/tasks.py +25 -2
  116. package/runtime/gatewaysdk/environment/schema/template_record.py +131 -0
  117. package/runtime/gatewaysdk/environment/schema/tools_world.py +7 -5
  118. package/runtime/gatewaysdk/environment/schema/ui.py +54 -33
  119. package/runtime/gatewaysdk/environment/schema/ui_interactions.py +419 -0
  120. package/runtime/gatewaysdk/environment/schema/ui_recorder.js +895 -0
  121. package/runtime/gatewaysdk/environment/schema/ui_replay.py +366 -0
  122. package/runtime/gatewaysdk/environment/schema/ui_replay_record.LICENSE +25 -0
  123. package/runtime/gatewaysdk/environment/schema/ui_replay_record.js +109 -0
  124. package/runtime/gatewaysdk/environment/schema/validation.py +24 -1
  125. package/runtime/gatewaysdk/environment/schema/worker.py +12 -5
  126. package/runtime/gatewaysdk/environment/schema/worker_key.py +38 -0
  127. package/runtime/gatewaysdk/environment/schema/worker_peaks.py +38 -0
  128. package/runtime/gatewaysdk/environment/schema/worker_watch.py +50 -0
  129. package/runtime/gatewaysdk/environment/schema/workers.py +245 -69
  130. package/runtime/gatewaysdk/environment/schema/world_host.py +96 -73
  131. package/runtime/gatewaysdk/environment/schema/world_tests.py +3 -1
  132. package/runtime/gatewaysdk/environment/secret_values.py +1 -1
  133. package/runtime/gatewaysdk/environment/world.py +34 -43
  134. package/runtime/gatewaysdk/environment/world_session_contract.py +7 -34
  135. package/skills/benchmarks-getting-started/SKILL.md +1 -1
  136. package/skills/connector-schema-authoring/SKILL.md +2 -2
  137. package/skills/connector-schema-authoring/connector-worlds.md +3 -2
  138. package/skills/connector-schema-authoring/world-schema.md +36 -23
  139. package/skills/factory-orchestrator/SKILL.md +147 -0
  140. package/skills/world-data-ingestion/ROWS.md +5 -1
  141. package/skills/world-data-ingestion/SKILL.md +8 -6
  142. package/skills/worlds-getting-started/SKILL.md +9 -9
  143. package/skills/worlds-getting-started/references/cli-mcp-map.md +33 -2
  144. package/skills/worlds-getting-started/references/gotchas.md +6 -6
  145. package/skills/worlds-getting-started/references/journal-checks.md +17 -2
  146. package/skills/worlds-getting-started/references/new-world-sandbox.md +1 -1
  147. package/skills/worlds-getting-started/references/shape-catalog.md +59 -0
  148. package/skills/worlds-getting-started/references/ui-map.md +3 -2
  149. package/dist/media-BIApiPzS.d.mts +0 -102
  150. package/dist/media-BIApiPzS.d.ts +0 -102
@@ -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.6.0",
23
+ version: "0.9.3",
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.endpoint = endpoint;
1854
- this.timeoutMs = timeoutMs;
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.timeoutMs);
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(`${this.endpoint.host}${route}`, {
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.endpoint.host, method, route, cause);
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";