stitchkit 0.67.0 → 0.68.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.
Files changed (226) hide show
  1. package/dist/agent-runtime/admission-lanes.d.ts +1 -1
  2. package/dist/agent-runtime/compaction.d.ts +2 -2
  3. package/dist/agent-runtime/events.d.ts +1 -1
  4. package/dist/agent-runtime/history.d.ts +1 -1
  5. package/dist/agent-runtime/history.d.ts.map +1 -1
  6. package/dist/agent-runtime/injection.d.ts +1 -1
  7. package/dist/agent-runtime/managed-tools.d.ts +3 -3
  8. package/dist/agent-runtime/models.d.ts +1 -1
  9. package/dist/agent-runtime/observability.d.ts +1 -1
  10. package/dist/agent-runtime/prompt.d.ts +1 -1
  11. package/dist/agent-runtime/prompt.d.ts.map +1 -1
  12. package/dist/agent-runtime/protocol.d.ts +19 -1
  13. package/dist/agent-runtime/protocol.d.ts.map +1 -1
  14. package/dist/agent-runtime/run-execution.d.ts +5 -5
  15. package/dist/agent-runtime/run-execution.d.ts.map +1 -1
  16. package/dist/agent-runtime/runtime-internals.d.ts +1 -1
  17. package/dist/agent-runtime/runtime-result.d.ts +1 -1
  18. package/dist/agent-runtime/runtime.d.ts +14 -12
  19. package/dist/agent-runtime/runtime.d.ts.map +1 -1
  20. package/dist/agent-runtime/store-driver.d.ts +2 -2
  21. package/dist/agent-runtime/store.d.ts +2 -2
  22. package/dist/agent-runtime/terminal-commit.d.ts +2 -2
  23. package/dist/agent-runtime/terminal-status.d.ts +1 -1
  24. package/dist/agent-runtime-openrouter.d.ts +1 -1
  25. package/dist/agent-runtime.d.ts +15 -15
  26. package/dist/agent-runtime.d.ts.map +1 -1
  27. package/dist/agent-runtime.js +90 -30
  28. package/dist/application/admission.d.ts +131 -0
  29. package/dist/application/admission.d.ts.map +1 -0
  30. package/dist/application/channel.d.ts +106 -0
  31. package/dist/application/channel.d.ts.map +1 -0
  32. package/dist/application/events.d.ts +2 -2
  33. package/dist/application/grammy.d.ts +2 -2
  34. package/dist/application/graph.d.ts +1 -1
  35. package/dist/application/health.d.ts +1 -1
  36. package/dist/application/kernel.d.ts +3 -3
  37. package/dist/application/latest-sink.d.ts.map +1 -1
  38. package/dist/application/opentelemetry.d.ts +3 -3
  39. package/dist/application/resource.d.ts +1 -1
  40. package/dist/application/schedule.d.ts +1 -1
  41. package/dist/application/server-resource.d.ts +7 -9
  42. package/dist/application/server-resource.d.ts.map +1 -1
  43. package/dist/application-grammy.d.ts +1 -1
  44. package/dist/application-grammy.js +1 -1
  45. package/dist/application-opentelemetry.d.ts +1 -1
  46. package/dist/application-opentelemetry.js +1 -1
  47. package/dist/application.d.ts +11 -9
  48. package/dist/application.d.ts.map +1 -1
  49. package/dist/application.js +393 -24
  50. package/dist/browser/client-multipart.d.ts +1 -1
  51. package/dist/browser/client-url.d.ts +2 -2
  52. package/dist/browser/client.d.ts +4 -5
  53. package/dist/browser/client.d.ts.map +1 -1
  54. package/dist/browser/contract-stream.d.ts +5 -0
  55. package/dist/browser/contract-stream.d.ts.map +1 -0
  56. package/dist/browser/http.d.ts +9 -6
  57. package/dist/browser/http.d.ts.map +1 -1
  58. package/dist/browser/socket-io.d.ts +12 -31
  59. package/dist/browser/socket-io.d.ts.map +1 -1
  60. package/dist/browser/stream.d.ts +5 -2
  61. package/dist/browser/stream.d.ts.map +1 -1
  62. package/dist/browser/transport.d.ts +3 -0
  63. package/dist/browser/transport.d.ts.map +1 -0
  64. package/dist/cli.d.ts +6 -6
  65. package/dist/cli.js +7 -7
  66. package/dist/contract/define.d.ts +34 -2
  67. package/dist/contract/define.d.ts.map +1 -1
  68. package/dist/contract/errors-factory.d.ts +1 -1
  69. package/dist/contract/errors.d.ts +4 -0
  70. package/dist/contract/errors.d.ts.map +1 -1
  71. package/dist/contract/factory.d.ts +1 -1
  72. package/dist/contract/index.d.ts +7 -6
  73. package/dist/contract/index.d.ts.map +1 -1
  74. package/dist/contract/index.js +5 -1
  75. package/dist/contract/stream.d.ts +19 -0
  76. package/dist/contract/stream.d.ts.map +1 -0
  77. package/dist/files/boundary.d.ts +1 -1
  78. package/dist/files.d.ts +2 -2
  79. package/dist/{index-xxye8j3k.js → index-0w9abg87.js} +4 -0
  80. package/dist/{index-svqhcrrj.js → index-2ve29dzn.js} +159 -13
  81. package/dist/index-69m2278y.js +115 -0
  82. package/dist/{index-physbf4z.js → index-6wzr93cg.js} +1 -1
  83. package/dist/index-7etq650s.js +75 -0
  84. package/dist/index-9ky9hhg3.js +484 -0
  85. package/dist/{index-413xk7ga.js → index-cq9q73nf.js} +50 -1
  86. package/dist/{index-1pgeyyee.js → index-cszjsxy5.js} +25 -88
  87. package/dist/{index-wrhf06ak.js → index-cz32amcb.js} +8 -8
  88. package/dist/{index-6taryy00.js → index-dafax5md.js} +1 -1
  89. package/dist/index-g2j2m6vv.js +63 -0
  90. package/dist/{index-8mxadm34.js → index-nck3yx3y.js} +1102 -22
  91. package/dist/{index-y91zd0ch.js → index-pr0qsmjy.js} +3 -2
  92. package/dist/index-v5bayx1z.js +138 -0
  93. package/dist/{index-mfw1pec7.js → index-wqyrvhzz.js} +3 -3
  94. package/dist/{index-x8v8b7rc.js → index-zpyj7hsv.js} +330 -58
  95. package/dist/index.d.ts +10 -10
  96. package/dist/index.js +274 -109
  97. package/dist/internal/bounded-lines.d.ts +4 -0
  98. package/dist/internal/bounded-lines.d.ts.map +1 -0
  99. package/dist/internal/errors.d.ts +1 -1
  100. package/dist/internal/observability-sink.d.ts +1 -1
  101. package/dist/node.d.ts +11 -10
  102. package/dist/node.d.ts.map +1 -1
  103. package/dist/node.js +11 -8
  104. package/dist/observability/audit.d.ts +5 -5
  105. package/dist/observability/context.d.ts +2 -2
  106. package/dist/observability/event.d.ts +2 -2
  107. package/dist/observability/index.d.ts +6 -6
  108. package/dist/observability/index.js +6 -7
  109. package/dist/react/entity-cache.d.ts +26 -1
  110. package/dist/react/entity-cache.d.ts.map +1 -1
  111. package/dist/react.d.ts +3 -3
  112. package/dist/react.d.ts.map +1 -1
  113. package/dist/react.js +136 -38
  114. package/dist/realtime/contract.d.ts +1 -1
  115. package/dist/realtime/index.d.ts +4 -4
  116. package/dist/realtime/rejection.d.ts +1 -1
  117. package/dist/realtime/request.d.ts +1 -1
  118. package/dist/realtime/socket.d.ts +2 -2
  119. package/dist/remote.d.ts +1 -1
  120. package/dist/remote.js +5 -6
  121. package/dist/server/bun.d.ts +3 -3
  122. package/dist/server/context-contribution.d.ts +1 -1
  123. package/dist/server/context.d.ts +4 -4
  124. package/dist/server/contract-stream.d.ts +5 -0
  125. package/dist/server/contract-stream.d.ts.map +1 -0
  126. package/dist/server/create.d.ts +1 -1
  127. package/dist/server/create.d.ts.map +1 -1
  128. package/dist/server/error-hook.d.ts +3 -3
  129. package/dist/server/file.d.ts +1 -1
  130. package/dist/server/implement.d.ts +2 -2
  131. package/dist/server/implement.d.ts.map +1 -1
  132. package/dist/server/index.d.ts +31 -30
  133. package/dist/server/index.d.ts.map +1 -1
  134. package/dist/server/index.js +44 -286
  135. package/dist/server/lifecycle.d.ts +1 -1
  136. package/dist/server/logging.d.ts +1 -1
  137. package/dist/server/middleware/auth.d.ts +2 -2
  138. package/dist/server/multipart.d.ts +2 -2
  139. package/dist/server/node.d.ts +2 -2
  140. package/dist/server/openapi.d.ts +1 -1
  141. package/dist/server/openapi.d.ts.map +1 -1
  142. package/dist/server/process-signal-common.d.ts +1 -1
  143. package/dist/server/process-signals.d.ts +1 -1
  144. package/dist/server/realtime.d.ts +4 -4
  145. package/dist/server/response-metadata.d.ts +1 -1
  146. package/dist/server/router.d.ts +1 -1
  147. package/dist/server/shutdown.d.ts +1 -1
  148. package/dist/server/socket-io-node.d.ts +3 -3
  149. package/dist/server/socket-io.d.ts +6 -6
  150. package/dist/server/stream.d.ts +3 -1
  151. package/dist/server/stream.d.ts.map +1 -1
  152. package/dist/server/streaming-route.d.ts +4 -2
  153. package/dist/server/streaming-route.d.ts.map +1 -1
  154. package/dist/server/types.d.ts +18 -8
  155. package/dist/server/types.d.ts.map +1 -1
  156. package/dist/server/unix-client-body.d.ts +4 -0
  157. package/dist/server/unix-client-body.d.ts.map +1 -0
  158. package/dist/server/unix-client-bun.d.ts +13 -0
  159. package/dist/server/unix-client-bun.d.ts.map +1 -0
  160. package/dist/server/unix-client-error.d.ts +9 -0
  161. package/dist/server/unix-client-error.d.ts.map +1 -0
  162. package/dist/server/unix-client.d.ts +25 -0
  163. package/dist/server/unix-client.d.ts.map +1 -0
  164. package/dist/testing/agent-store-conformance.d.ts +1 -1
  165. package/dist/testing/managed-resource-conformance-contract.d.ts +1 -1
  166. package/dist/testing/managed-resource-conformance-driver.d.ts +2 -2
  167. package/dist/testing/managed-resource-conformance-scenarios.d.ts +2 -2
  168. package/dist/testing/managed-resource-conformance.d.ts +2 -2
  169. package/dist/testing/surface-conformance.d.ts +3 -3
  170. package/dist/testing/surface-manifest.d.ts +5 -5
  171. package/dist/testing.d.ts +8 -8
  172. package/dist/testing.js +6 -5
  173. package/dist/tool-invoker.d.ts +9 -0
  174. package/dist/tool-invoker.d.ts.map +1 -0
  175. package/dist/tool-invoker.js +16 -0
  176. package/dist/tools/agent.d.ts +4 -4
  177. package/dist/tools/async-operation.d.ts +3 -3
  178. package/dist/tools/cli-command.d.ts +3 -3
  179. package/dist/tools/cli-format.d.ts +1 -1
  180. package/dist/tools/cli-policy.d.ts +1 -1
  181. package/dist/tools/cli-wait.d.ts +1 -1
  182. package/dist/tools/cli.d.ts +7 -7
  183. package/dist/tools/define-download-tool.d.ts +4 -4
  184. package/dist/tools/define-upload-tool.d.ts +3 -3
  185. package/dist/tools/define-view-file-tool.d.ts +3 -3
  186. package/dist/tools/define-wait-tool.d.ts +2 -2
  187. package/dist/tools/download-core.d.ts +2 -2
  188. package/dist/tools/execute.d.ts +2 -2
  189. package/dist/tools/flatten-join.d.ts +1 -1
  190. package/dist/tools/internal/surface-projector.d.ts +3 -3
  191. package/dist/tools/invoker.d.ts +4 -4
  192. package/dist/tools/lifecycle.d.ts +1 -1
  193. package/dist/tools/list-names.d.ts +2 -2
  194. package/dist/tools/managed-file-error.d.ts +1 -1
  195. package/dist/tools/manifest.d.ts +3 -3
  196. package/dist/tools/mcp-handler.d.ts +4 -4
  197. package/dist/tools/mcp-prepare.d.ts +8 -8
  198. package/dist/tools/mcp-round-policy.d.ts +1 -1
  199. package/dist/tools/mcp-round.d.ts +3 -3
  200. package/dist/tools/mcp-stdio-signals.d.ts +2 -2
  201. package/dist/tools/mcp-stdio.d.ts +3 -3
  202. package/dist/tools/mcp.d.ts +7 -7
  203. package/dist/tools/mount-download.d.ts +1 -1
  204. package/dist/tools/mount-upload.d.ts +1 -1
  205. package/dist/tools/mount.d.ts +5 -5
  206. package/dist/tools/native-definition.d.ts +2 -2
  207. package/dist/tools/native-mcp.d.ts +3 -3
  208. package/dist/tools/oauth-metadata.d.ts +1 -1
  209. package/dist/tools/oauth-provider.d.ts +1 -1
  210. package/dist/tools/presentation.d.ts +1 -1
  211. package/dist/tools/remote.d.ts +3 -3
  212. package/dist/tools/runtime-tool.d.ts +2 -2
  213. package/dist/tools/surface.d.ts +5 -5
  214. package/dist/tools/tool-logger.d.ts +1 -1
  215. package/dist/tools/toolkit.d.ts +7 -7
  216. package/dist/tools/transports.d.ts +2 -2
  217. package/dist/tools/upload-core.d.ts +1 -1
  218. package/dist/tools/view-file.d.ts +1 -1
  219. package/dist/tools.d.ts +42 -42
  220. package/dist/tools.js +27 -72
  221. package/llms-full.txt +506 -35
  222. package/package.json +8 -4
  223. package/dist/index-48ffdxgk.js +0 -6
  224. package/dist/index-pzyt11ch.js +0 -127
  225. package/dist/index-tvwcrx2d.js +0 -224
  226. package/dist/index-wd8g8z6e.js +0 -206
@@ -1,9 +1,14 @@
1
1
  import {
2
- inputIsQuery
3
- } from "./index-48ffdxgk.js";
2
+ DEFAULT_STREAM_LINE_BYTES,
3
+ inputIsQuery,
4
+ parseSSE,
5
+ readBoundedUtf8Lines
6
+ } from "./index-v5bayx1z.js";
4
7
  import {
8
+ ContractStreamFrameSchema,
9
+ DEFAULT_CONTRACT_STREAM_FRAME_BYTES,
5
10
  parseTrailingWildcard
6
- } from "./index-pzyt11ch.js";
11
+ } from "./index-9ky9hhg3.js";
7
12
  import {
8
13
  isRecord,
9
14
  mapObject,
@@ -250,8 +255,8 @@ class ApiError extends Error {
250
255
  details;
251
256
  hint;
252
257
  traceId;
253
- constructor(code, status = 0, details, message, hint, traceId) {
254
- super(message ?? `API Error: ${code}`);
258
+ constructor(code, status = 0, details, message, hint, traceId, options) {
259
+ super(message ?? `API Error: ${code}`, options);
255
260
  this.code = code;
256
261
  this.status = status;
257
262
  this.details = details;
@@ -277,10 +282,103 @@ function parseApiErrorBody(body) {
277
282
  hint: typeof error.hint === "string" ? error.hint : undefined
278
283
  };
279
284
  }
285
+ // src/browser/stream.ts
286
+ function parseFailure(raw, error, onParseError) {
287
+ const failure = error instanceof Error ? error : new Error(String(error));
288
+ if (!onParseError)
289
+ throw failure;
290
+ onParseError(raw, failure);
291
+ }
292
+ async function* parseNDJSON(response, options) {
293
+ try {
294
+ for await (const rawLine of readBoundedUtf8Lines(response, options?.maxLineBytes ?? DEFAULT_STREAM_LINE_BYTES)) {
295
+ const line = (rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine).trim();
296
+ if (line === "")
297
+ continue;
298
+ try {
299
+ yield JSON.parse(line);
300
+ } catch (error) {
301
+ parseFailure(line, error, options?.onParseError);
302
+ }
303
+ }
304
+ } catch (error) {
305
+ parseFailure("", error, options?.onParseError);
306
+ }
307
+ }
308
+
309
+ // src/browser/contract-stream.ts
310
+ function sourceFor(response, descriptor) {
311
+ const options = {
312
+ maxLineBytes: descriptor.maxFrameBytes ?? DEFAULT_CONTRACT_STREAM_FRAME_BYTES
313
+ };
314
+ return descriptor.format === "sse" ? parseSSE(response, options) : parseNDJSON(response, options);
315
+ }
316
+ async function* readContractStream(response, descriptor, abort) {
317
+ let ended = false;
318
+ let terminalSeen = descriptor.terminal === undefined;
319
+ try {
320
+ for await (const raw of sourceFor(response, descriptor)) {
321
+ const parsedFrame = ContractStreamFrameSchema.safeParse(raw);
322
+ if (!parsedFrame.success) {
323
+ throw new ApiError("STREAM_PROTOCOL_ERROR", 0, undefined, "Stream frame did not match the protocol envelope");
324
+ }
325
+ const frame = parsedFrame.data;
326
+ if (frame.type === "error") {
327
+ throw new ApiError(frame.error.code, 0, frame.error.details, frame.error.message, frame.error.hint);
328
+ }
329
+ if (frame.type === "end") {
330
+ if (!terminalSeen) {
331
+ throw new ApiError("STREAM_TERMINAL_MISSING", 0, undefined, "Stream completed before its declared terminal item");
332
+ }
333
+ ended = true;
334
+ return;
335
+ }
336
+ const parsedItem = descriptor.item.safeParse(frame.data);
337
+ if (!parsedItem.success) {
338
+ throw new ApiError("STREAM_ITEM_INVALID", 0, undefined, "Stream item did not match its contract");
339
+ }
340
+ const item = parsedItem.data;
341
+ if (descriptor.terminal?.safeParse(item).success)
342
+ terminalSeen = true;
343
+ yield item;
344
+ }
345
+ if (!ended) {
346
+ throw new ApiError("STREAM_TRUNCATED", 0, undefined, "Stream ended without its protocol terminal frame");
347
+ }
348
+ } finally {
349
+ abort();
350
+ }
351
+ }
352
+
353
+ class OwnedContractStream {
354
+ #inner;
355
+ #abort;
356
+ constructor(inner, abort) {
357
+ this.#inner = inner;
358
+ this.#abort = abort;
359
+ }
360
+ [Symbol.asyncIterator]() {
361
+ return this;
362
+ }
363
+ next() {
364
+ return this.#inner.next();
365
+ }
366
+ return() {
367
+ this.#abort();
368
+ return this.#inner.return(undefined);
369
+ }
370
+ throw(error) {
371
+ this.#abort();
372
+ return this.#inner.throw(error);
373
+ }
374
+ }
375
+ function parseContractStream(response, descriptor, abort) {
376
+ return new OwnedContractStream(readContractStream(response, descriptor, abort), abort);
377
+ }
280
378
 
281
379
  // src/browser/client.ts
282
380
  function withTimeout(options, endpoint) {
283
- const responseType = endpoint.rawResponse ? "response" : endpoint.output ? undefined : "void";
381
+ const responseType = endpoint.rawResponse || "stream" in endpoint ? "response" : endpoint.output ? undefined : "void";
284
382
  if (endpoint.timeout === undefined && responseType === undefined)
285
383
  return options;
286
384
  return {
@@ -289,9 +387,23 @@ function withTimeout(options, endpoint) {
289
387
  ...responseType && { responseType }
290
388
  };
291
389
  }
292
- function withOutput(endpoint, result) {
390
+ function withOutput(endpoint, result, abortStream) {
293
391
  if (endpoint.rawResponse)
294
392
  return result;
393
+ if ("stream" in endpoint && endpoint.stream) {
394
+ return result.then((value) => {
395
+ if (!(value instanceof Response)) {
396
+ abortStream?.();
397
+ throw new Error("Streaming endpoint did not return a Response");
398
+ }
399
+ return parseContractStream(value, endpoint.stream, abortStream ?? (() => {
400
+ return;
401
+ }));
402
+ }, (error) => {
403
+ abortStream?.();
404
+ throw error;
405
+ });
406
+ }
295
407
  const schema = endpoint.output;
296
408
  return result.then((value) => {
297
409
  if (!schema) {
@@ -341,20 +453,26 @@ function createHttpExecutor(endpoint, prefix, client, config) {
341
453
  const httpMethod = endpoint.method.toLowerCase();
342
454
  return (requestArgs, options) => {
343
455
  const plan = planClientRequest(endpoint, prefix, requestArgs, config);
456
+ const streamAbort = "stream" in endpoint ? new AbortController : undefined;
457
+ const requestOptions = streamAbort ? {
458
+ ...options,
459
+ signal: options?.signal ? AbortSignal.any([options.signal, streamAbort.signal]) : streamAbort.signal
460
+ } : options;
461
+ const finishStream = streamAbort ? () => streamAbort.abort() : undefined;
344
462
  if (endpoint.multipart) {
345
463
  if (httpMethod === "get" || httpMethod === "head" || httpMethod === "delete") {
346
464
  throw new Error(`Multipart endpoint ${endpoint.method} ${endpoint.path} must be POST / PUT / PATCH`);
347
465
  }
348
466
  const formData = buildMultipartForm(endpoint.multipart, plan.remainingArgs);
349
- return withOutput(endpoint, client[httpMethod](plan.relativeUrl, formData, withTimeout(options, endpoint)));
467
+ return withOutput(endpoint, client[httpMethod](plan.relativeUrl, formData, withTimeout(requestOptions, endpoint)), finishStream);
350
468
  }
351
469
  if (httpMethod === "get" || httpMethod === "head") {
352
- return withOutput(endpoint, client[httpMethod](plan.relativeUrl, withTimeout(options, endpoint)));
470
+ return withOutput(endpoint, client[httpMethod](plan.relativeUrl, withTimeout(requestOptions, endpoint)), finishStream);
353
471
  }
354
472
  if (httpMethod === "delete") {
355
- return withOutput(endpoint, client.delete(plan.relativeUrl, withTimeout(options, endpoint)));
473
+ return withOutput(endpoint, client.delete(plan.relativeUrl, withTimeout(requestOptions, endpoint)), finishStream);
356
474
  }
357
- return withOutput(endpoint, client[httpMethod](plan.relativeUrl, Object.keys(plan.remainingArgs).length > 0 ? plan.remainingArgs : undefined, withTimeout(options, endpoint)));
475
+ return withOutput(endpoint, client[httpMethod](plan.relativeUrl, Object.keys(plan.remainingArgs).length > 0 ? plan.remainingArgs : undefined, withTimeout(requestOptions, endpoint)), finishStream);
358
476
  };
359
477
  }
360
478
  function createFetchExecutor(endpoint, prefix, config, contractConfig) {
@@ -366,7 +484,32 @@ function createFetchExecutor(endpoint, prefix, config, contractConfig) {
366
484
  Accept: "application/json",
367
485
  ...typeof config.headers === "function" ? config.headers() : config.headers
368
486
  };
369
- const cancellation = createRequestCancellation(options?.signal, endpoint.timeout ?? config.timeout ?? 30000);
487
+ const streamAbort = "stream" in endpoint ? new AbortController : undefined;
488
+ const requestSignal = streamAbort ? options?.signal ? AbortSignal.any([options.signal, streamAbort.signal]) : streamAbort.signal : options?.signal;
489
+ const openTimeoutMs = endpoint.timeout ?? config.timeout ?? 30000;
490
+ const cancellation = streamAbort ? {
491
+ signal: requestSignal,
492
+ async run(operation) {
493
+ let timedOut = false;
494
+ const timer = setTimeout(() => {
495
+ timedOut = true;
496
+ streamAbort.abort(new DOMException("Request timed out", "TimeoutError"));
497
+ }, openTimeoutMs);
498
+ try {
499
+ if (options?.signal?.aborted)
500
+ throw new RequestCancellationError("caller");
501
+ return await operation(requestSignal);
502
+ } catch (error) {
503
+ if (timedOut)
504
+ throw new RequestCancellationError("timeout");
505
+ if (options?.signal?.aborted)
506
+ throw new RequestCancellationError("caller");
507
+ throw error;
508
+ } finally {
509
+ clearTimeout(timer);
510
+ }
511
+ }
512
+ } : createRequestCancellation(requestSignal, openTimeoutMs);
370
513
  const hasBody = endpoint.method !== "GET" && endpoint.method !== "HEAD" && endpoint.method !== "DELETE" && !endpoint.multipart && endpoint.input && Object.keys(plan.remainingArgs).length > 0;
371
514
  if (hasBody)
372
515
  headers["Content-Type"] = "application/json";
@@ -385,6 +528,9 @@ function createFetchExecutor(endpoint, prefix, config, contractConfig) {
385
528
  }
386
529
  if (endpoint.rawResponse)
387
530
  return res;
531
+ if ("stream" in endpoint && endpoint.stream) {
532
+ return parseContractStream(res, endpoint.stream, () => streamAbort?.abort());
533
+ }
388
534
  if (!endpoint.output) {
389
535
  const text = await res.text();
390
536
  if (text.length > 0) {
@@ -401,7 +547,7 @@ function createFetchExecutor(endpoint, prefix, config, contractConfig) {
401
547
  if (ApiError.is(error))
402
548
  throw error;
403
549
  const message = error instanceof Error ? error.message : undefined;
404
- throw new ApiError("UNKNOWN_ERROR", 0, message ? { message } : undefined, message);
550
+ throw new ApiError("UNKNOWN_ERROR", 0, message ? { message } : undefined, message, undefined, undefined, { cause: error });
405
551
  }
406
552
  };
407
553
  }
@@ -0,0 +1,115 @@
1
+ import {
2
+ isUnsafeKey,
3
+ resolveTraceContext
4
+ } from "./index-9ky9hhg3.js";
5
+ import {
6
+ isRecord
7
+ } from "./index-smpbdg6k.js";
8
+
9
+ // src/server/request.ts
10
+ function generateTraceId() {
11
+ return `${Date.now().toString(36)}-${crypto.randomUUID().slice(0, 8)}`;
12
+ }
13
+ function resolveTraceId(req) {
14
+ const header = req.headers.get("x-request-id") ?? req.headers.get("x-trace-id");
15
+ const trimmed = header?.trim();
16
+ if (trimmed && trimmed.length <= 128 && /^[\w.-]+$/.test(trimmed)) {
17
+ return trimmed;
18
+ }
19
+ return generateTraceId();
20
+ }
21
+ function resolveSocketIp(req, server) {
22
+ if (typeof server === "object" && server !== null && "requestIP" in server && typeof server.requestIP === "function") {
23
+ const addr = server.requestIP(req);
24
+ if (isRecord(addr) && typeof addr.address === "string" && addr.address) {
25
+ return addr.address;
26
+ }
27
+ }
28
+ if ("ip" in req && typeof req.ip === "string" && req.ip)
29
+ return req.ip;
30
+ return;
31
+ }
32
+ function extractIp(req, options = {}) {
33
+ if (options.trustProxy) {
34
+ const forwarded = req.headers.get("x-forwarded-for");
35
+ if (forwarded)
36
+ return (forwarded.split(",")[0] ?? "").trim().replace(/^::ffff:/, "");
37
+ const realIp = req.headers.get("x-real-ip");
38
+ if (realIp)
39
+ return realIp.trim().replace(/^::ffff:/, "");
40
+ }
41
+ return (options.socketIp ?? "").replace(/^::ffff:/, "");
42
+ }
43
+ function getClientInfo(req, options = {}) {
44
+ return {
45
+ ipAddress: extractIp(req, options) || undefined,
46
+ userAgent: req.headers.get("user-agent") ?? undefined
47
+ };
48
+ }
49
+ function parseQueryParams(url) {
50
+ const query = {};
51
+ for (const key of new Set(url.searchParams.keys())) {
52
+ if (isUnsafeKey(key))
53
+ continue;
54
+ const values = url.searchParams.getAll(key);
55
+ const [first] = values;
56
+ query[key] = values.length === 1 && first !== undefined ? first : values;
57
+ }
58
+ return query;
59
+ }
60
+
61
+ // src/observability/context.ts
62
+ import { AsyncLocalStorage } from "node:async_hooks";
63
+ var storage = new AsyncLocalStorage;
64
+ function runWithRequestContext(ctx, fn) {
65
+ return storage.run(ctx, fn);
66
+ }
67
+ function getRequestContext() {
68
+ return storage.getStore();
69
+ }
70
+ function getTraceId() {
71
+ return storage.getStore()?.trace.traceId;
72
+ }
73
+ function getUserId() {
74
+ return storage.getStore()?.userId;
75
+ }
76
+ function setRequestUser(userId) {
77
+ const ctx = storage.getStore();
78
+ if (ctx)
79
+ ctx.userId = userId;
80
+ }
81
+ function setRequestEndpoint(serviceName, action) {
82
+ const ctx = storage.getStore();
83
+ if (ctx) {
84
+ ctx.serviceName = serviceName;
85
+ ctx.action = action;
86
+ }
87
+ }
88
+ function setRequestDimensions(dimensions) {
89
+ const ctx = storage.getStore();
90
+ if (ctx)
91
+ ctx.dimensions = { ...ctx.dimensions, ...dimensions };
92
+ }
93
+ function setRequestError(error) {
94
+ const ctx = storage.getStore();
95
+ if (ctx)
96
+ ctx.error = error;
97
+ }
98
+ function wrapInRequestContext(handler, options = {}) {
99
+ return (req, server) => {
100
+ const ctx = {
101
+ trace: resolveTraceContext(req),
102
+ source: "http",
103
+ method: req.method,
104
+ path: new URL(req.url, "http://localhost").pathname,
105
+ startedAt: process.hrtime.bigint(),
106
+ ...getClientInfo(req, {
107
+ trustProxy: options.trustProxy,
108
+ socketIp: resolveSocketIp(req, server)
109
+ })
110
+ };
111
+ return runWithRequestContext(ctx, () => handler(req, server));
112
+ };
113
+ }
114
+
115
+ export { generateTraceId, resolveTraceId, resolveSocketIp, extractIp, getClientInfo, parseQueryParams, runWithRequestContext, getRequestContext, getTraceId, getUserId, setRequestUser, setRequestEndpoint, setRequestDimensions, setRequestError, wrapInRequestContext };
@@ -11,7 +11,7 @@ import {
11
11
  } from "./index-dk6e56g0.js";
12
12
  import {
13
13
  AppError
14
- } from "./index-xxye8j3k.js";
14
+ } from "./index-0w9abg87.js";
15
15
 
16
16
  // src/application/graph.ts
17
17
  function resolveResourceGraph(resources) {
@@ -0,0 +1,75 @@
1
+ import {
2
+ executeToolMethod
3
+ } from "./index-cz32amcb.js";
4
+ import {
5
+ assertToolExtensionCompatible,
6
+ projectToolSurface
7
+ } from "./index-vkk06pv1.js";
8
+ import {
9
+ mergeSchemas,
10
+ rebuildObject
11
+ } from "./index-6djpbnda.js";
12
+
13
+ // src/tools/mount.ts
14
+ import { z } from "zod";
15
+ function applyExtend(base, extra) {
16
+ if (base instanceof z.ZodObject) {
17
+ assertToolExtensionCompatible(base, extra);
18
+ return rebuildObject(base, { ...extra, ...base.shape });
19
+ }
20
+ return z.intersection(z.object(extra), base);
21
+ }
22
+ function contractToolMountable(projected, extend) {
23
+ const method = projected.source;
24
+ const baseArgumentSchema = mergeSchemas(method.paramsSchema, method.inputSchema);
25
+ const argumentSchema = projected.shouldExtend && extend ? applyExtend(baseArgumentSchema, extend.schema) : baseArgumentSchema;
26
+ return {
27
+ method,
28
+ name: projected.name,
29
+ argumentSchema,
30
+ presentationSchema: projected.presentationSchema,
31
+ shouldExtend: projected.shouldExtend
32
+ };
33
+ }
34
+ function collectTools(service, transport, config = {}) {
35
+ const { extend, flattenUnionInput = false, assertNames = true } = config;
36
+ const tools = [];
37
+ for (const projected of projectToolSurface({ services: [service] }, transport === "HTTP" ? "AGENT" : transport, {
38
+ extend,
39
+ flattenUnionInput,
40
+ assertNames,
41
+ assertUniqueNames: false
42
+ })) {
43
+ if (projected.kind !== "contract")
44
+ continue;
45
+ tools.push(contractToolMountable(projected, extend));
46
+ }
47
+ return tools;
48
+ }
49
+ function createToolRunner(config) {
50
+ const extension = config.extend ? {
51
+ schema: z.object(config.extend.schema),
52
+ resolve: config.extend.resolve
53
+ } : undefined;
54
+ return async function runOneToolCall(tool, rawArgs, context) {
55
+ return executeToolMethod(tool.method, tool.name, rawArgs, { ...config.context, ...context, source: config.source }, config.hooks, config.lifecycle, config.coerceJsonArgs ?? true, config.onOutputStrip ? (paths) => config.onOutputStrip?.(tool.name, paths) : undefined, tool.shouldExtend ? extension : undefined);
56
+ };
57
+ }
58
+ function formatToolError(result, toolName, errorHint) {
59
+ const err = { error: result.code };
60
+ if (result.details)
61
+ err.details = result.details;
62
+ const hints = [];
63
+ if (result.hint)
64
+ hints.push(result.hint);
65
+ if (errorHint && toolName) {
66
+ const global = errorHint(toolName, result.code);
67
+ if (global)
68
+ hints.push(global);
69
+ }
70
+ if (hints.length > 0)
71
+ err._hint = hints.join(" ");
72
+ return err;
73
+ }
74
+
75
+ export { contractToolMountable, collectTools, createToolRunner, formatToolError };