@mongodb-js/agent-engine-runner-shared 0.11.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 (220) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/LICENSE.md +201 -0
  3. package/README.md +29 -0
  4. package/dist/agent_config.d.ts +167 -0
  5. package/dist/agent_config.d.ts.map +1 -0
  6. package/dist/agent_config.js +544 -0
  7. package/dist/call_interrupted.d.ts +12 -0
  8. package/dist/call_interrupted.d.ts.map +1 -0
  9. package/dist/call_interrupted.js +11 -0
  10. package/dist/checkpoint_workspace.d.ts +25 -0
  11. package/dist/checkpoint_workspace.d.ts.map +1 -0
  12. package/dist/checkpoint_workspace.js +44 -0
  13. package/dist/context.d.ts +235 -0
  14. package/dist/context.d.ts.map +1 -0
  15. package/dist/context.js +322 -0
  16. package/dist/db_config.d.ts +28 -0
  17. package/dist/db_config.d.ts.map +1 -0
  18. package/dist/db_config.js +66 -0
  19. package/dist/db_naming.d.ts +54 -0
  20. package/dist/db_naming.d.ts.map +1 -0
  21. package/dist/db_naming.js +94 -0
  22. package/dist/error_reporting.d.ts +67 -0
  23. package/dist/error_reporting.d.ts.map +1 -0
  24. package/dist/error_reporting.js +311 -0
  25. package/dist/generated/workflow/v1/activity_pb.d.ts +342 -0
  26. package/dist/generated/workflow/v1/activity_pb.d.ts.map +1 -0
  27. package/dist/generated/workflow/v1/activity_pb.js +115 -0
  28. package/dist/generated/workflow/v1/common_pb.d.ts +184 -0
  29. package/dist/generated/workflow/v1/common_pb.d.ts.map +1 -0
  30. package/dist/generated/workflow/v1/common_pb.js +86 -0
  31. package/dist/generated/workflow/v1/runtime_pb.d.ts +200 -0
  32. package/dist/generated/workflow/v1/runtime_pb.d.ts.map +1 -0
  33. package/dist/generated/workflow/v1/runtime_pb.js +40 -0
  34. package/dist/generated/workflow/v1/state_pb.d.ts +254 -0
  35. package/dist/generated/workflow/v1/state_pb.d.ts.map +1 -0
  36. package/dist/generated/workflow/v1/state_pb.js +68 -0
  37. package/dist/guardrails_evaluator/core.d.ts +23 -0
  38. package/dist/guardrails_evaluator/core.d.ts.map +1 -0
  39. package/dist/guardrails_evaluator/core.js +122 -0
  40. package/dist/guardrails_evaluator/index.d.ts +10 -0
  41. package/dist/guardrails_evaluator/index.d.ts.map +1 -0
  42. package/dist/guardrails_evaluator/index.js +11 -0
  43. package/dist/guardrails_evaluator/regex.d.ts +20 -0
  44. package/dist/guardrails_evaluator/regex.d.ts.map +1 -0
  45. package/dist/guardrails_evaluator/regex.js +233 -0
  46. package/dist/hooks.d.ts +109 -0
  47. package/dist/hooks.d.ts.map +1 -0
  48. package/dist/hooks.js +216 -0
  49. package/dist/http_path.d.ts +18 -0
  50. package/dist/http_path.d.ts.map +1 -0
  51. package/dist/http_path.js +53 -0
  52. package/dist/index.d.ts +35 -0
  53. package/dist/index.d.ts.map +1 -0
  54. package/dist/index.js +41 -0
  55. package/dist/launcher.d.ts +130 -0
  56. package/dist/launcher.d.ts.map +1 -0
  57. package/dist/launcher.js +325 -0
  58. package/dist/logger.d.ts +96 -0
  59. package/dist/logger.d.ts.map +1 -0
  60. package/dist/logger.js +204 -0
  61. package/dist/mcp_oauth.d.ts +51 -0
  62. package/dist/mcp_oauth.d.ts.map +1 -0
  63. package/dist/mcp_oauth.js +389 -0
  64. package/dist/mcp_oauth_secret.d.ts +21 -0
  65. package/dist/mcp_oauth_secret.d.ts.map +1 -0
  66. package/dist/mcp_oauth_secret.js +122 -0
  67. package/dist/mcp_tools.d.ts +71 -0
  68. package/dist/mcp_tools.d.ts.map +1 -0
  69. package/dist/mcp_tools.js +301 -0
  70. package/dist/memory_appbound.d.ts +42 -0
  71. package/dist/memory_appbound.d.ts.map +1 -0
  72. package/dist/memory_appbound.js +159 -0
  73. package/dist/memory_writer.d.ts +49 -0
  74. package/dist/memory_writer.d.ts.map +1 -0
  75. package/dist/memory_writer.js +171 -0
  76. package/dist/metrics.d.ts +84 -0
  77. package/dist/metrics.d.ts.map +1 -0
  78. package/dist/metrics.js +205 -0
  79. package/dist/models.d.ts +1458 -0
  80. package/dist/models.d.ts.map +1 -0
  81. package/dist/models.js +1726 -0
  82. package/dist/node_logger.d.ts +43 -0
  83. package/dist/node_logger.d.ts.map +1 -0
  84. package/dist/node_logger.js +158 -0
  85. package/dist/owner_callback.d.ts +16 -0
  86. package/dist/owner_callback.d.ts.map +1 -0
  87. package/dist/owner_callback.js +40 -0
  88. package/dist/progress.d.ts +57 -0
  89. package/dist/progress.d.ts.map +1 -0
  90. package/dist/progress.js +140 -0
  91. package/dist/runtime.d.ts +131 -0
  92. package/dist/runtime.d.ts.map +1 -0
  93. package/dist/runtime.js +351 -0
  94. package/dist/secure_llm_proxy.d.ts +115 -0
  95. package/dist/secure_llm_proxy.d.ts.map +1 -0
  96. package/dist/secure_llm_proxy.js +922 -0
  97. package/dist/secure_wrapper.d.ts +332 -0
  98. package/dist/secure_wrapper.d.ts.map +1 -0
  99. package/dist/secure_wrapper.js +1249 -0
  100. package/dist/server/aer.d.ts +61 -0
  101. package/dist/server/aer.d.ts.map +1 -0
  102. package/dist/server/aer.js +1124 -0
  103. package/dist/server/auth.d.ts +56 -0
  104. package/dist/server/auth.d.ts.map +1 -0
  105. package/dist/server/auth.js +132 -0
  106. package/dist/server/base.d.ts +104 -0
  107. package/dist/server/base.d.ts.map +1 -0
  108. package/dist/server/base.js +150 -0
  109. package/dist/server/callInterrupt.d.ts +49 -0
  110. package/dist/server/callInterrupt.d.ts.map +1 -0
  111. package/dist/server/callInterrupt.js +68 -0
  112. package/dist/server/callback_delivery.d.ts +14 -0
  113. package/dist/server/callback_delivery.d.ts.map +1 -0
  114. package/dist/server/callback_delivery.js +141 -0
  115. package/dist/server/chunk_types.d.ts +50 -0
  116. package/dist/server/chunk_types.d.ts.map +1 -0
  117. package/dist/server/chunk_types.js +62 -0
  118. package/dist/server/cors.d.ts +52 -0
  119. package/dist/server/cors.d.ts.map +1 -0
  120. package/dist/server/cors.js +107 -0
  121. package/dist/server/drain.d.ts +169 -0
  122. package/dist/server/drain.d.ts.map +1 -0
  123. package/dist/server/drain.js +455 -0
  124. package/dist/server/function.d.ts +77 -0
  125. package/dist/server/function.d.ts.map +1 -0
  126. package/dist/server/function.js +337 -0
  127. package/dist/server/http_retry.d.ts +37 -0
  128. package/dist/server/http_retry.d.ts.map +1 -0
  129. package/dist/server/http_retry.js +157 -0
  130. package/dist/server/index.d.ts +7 -0
  131. package/dist/server/index.d.ts.map +1 -0
  132. package/dist/server/index.js +5 -0
  133. package/dist/server/metadata.d.ts +50 -0
  134. package/dist/server/metadata.d.ts.map +1 -0
  135. package/dist/server/metadata.js +193 -0
  136. package/dist/server/oe_url.d.ts +36 -0
  137. package/dist/server/oe_url.d.ts.map +1 -0
  138. package/dist/server/oe_url.js +50 -0
  139. package/dist/server/owner_url.d.ts +35 -0
  140. package/dist/server/owner_url.d.ts.map +1 -0
  141. package/dist/server/owner_url.js +146 -0
  142. package/dist/server/query.d.ts +42 -0
  143. package/dist/server/query.d.ts.map +1 -0
  144. package/dist/server/query.js +28 -0
  145. package/dist/server/tool.d.ts +138 -0
  146. package/dist/server/tool.d.ts.map +1 -0
  147. package/dist/server/tool.js +1017 -0
  148. package/dist/span_names.d.ts +21 -0
  149. package/dist/span_names.d.ts.map +1 -0
  150. package/dist/span_names.js +31 -0
  151. package/dist/structured_logging/constants.d.ts +17 -0
  152. package/dist/structured_logging/constants.d.ts.map +1 -0
  153. package/dist/structured_logging/constants.js +71 -0
  154. package/dist/structured_logging/env.d.ts +18 -0
  155. package/dist/structured_logging/env.d.ts.map +1 -0
  156. package/dist/structured_logging/env.js +39 -0
  157. package/dist/structured_logging/install.d.ts +56 -0
  158. package/dist/structured_logging/install.d.ts.map +1 -0
  159. package/dist/structured_logging/install.js +107 -0
  160. package/dist/structured_logging/layout.d.ts +9 -0
  161. package/dist/structured_logging/layout.d.ts.map +1 -0
  162. package/dist/structured_logging/layout.js +144 -0
  163. package/dist/structured_logging/serialize.d.ts +27 -0
  164. package/dist/structured_logging/serialize.d.ts.map +1 -0
  165. package/dist/structured_logging/serialize.js +61 -0
  166. package/dist/structured_logging/stdio_capture.d.ts +59 -0
  167. package/dist/structured_logging/stdio_capture.d.ts.map +1 -0
  168. package/dist/structured_logging/stdio_capture.js +164 -0
  169. package/dist/structured_logging/uncaught.d.ts +14 -0
  170. package/dist/structured_logging/uncaught.d.ts.map +1 -0
  171. package/dist/structured_logging/uncaught.js +58 -0
  172. package/dist/structured_logging.d.ts +48 -0
  173. package/dist/structured_logging.d.ts.map +1 -0
  174. package/dist/structured_logging.js +47 -0
  175. package/dist/tls_client.d.ts +61 -0
  176. package/dist/tls_client.d.ts.map +1 -0
  177. package/dist/tls_client.js +298 -0
  178. package/dist/tool_api_error.d.ts +62 -0
  179. package/dist/tool_api_error.d.ts.map +1 -0
  180. package/dist/tool_api_error.js +399 -0
  181. package/dist/tool_memory_ownership.d.ts +10 -0
  182. package/dist/tool_memory_ownership.d.ts.map +1 -0
  183. package/dist/tool_memory_ownership.js +36 -0
  184. package/dist/toolpod_handlers.d.ts +126 -0
  185. package/dist/toolpod_handlers.d.ts.map +1 -0
  186. package/dist/toolpod_handlers.js +1016 -0
  187. package/dist/tracing/exporters.d.ts +51 -0
  188. package/dist/tracing/exporters.d.ts.map +1 -0
  189. package/dist/tracing/exporters.js +327 -0
  190. package/dist/tracing/index.d.ts +3 -0
  191. package/dist/tracing/index.d.ts.map +1 -0
  192. package/dist/tracing/index.js +2 -0
  193. package/dist/tracing/setup.d.ts +76 -0
  194. package/dist/tracing/setup.d.ts.map +1 -0
  195. package/dist/tracing/setup.js +436 -0
  196. package/dist/utils.d.ts +204 -0
  197. package/dist/utils.d.ts.map +1 -0
  198. package/dist/utils.js +867 -0
  199. package/dist/workflow/activity.d.ts +71 -0
  200. package/dist/workflow/activity.d.ts.map +1 -0
  201. package/dist/workflow/activity.js +357 -0
  202. package/dist/workflow/attempt.d.ts +12 -0
  203. package/dist/workflow/attempt.d.ts.map +1 -0
  204. package/dist/workflow/attempt.js +96 -0
  205. package/dist/workflow/client.d.ts +46 -0
  206. package/dist/workflow/client.d.ts.map +1 -0
  207. package/dist/workflow/client.js +299 -0
  208. package/dist/workflow/context.d.ts +37 -0
  209. package/dist/workflow/context.d.ts.map +1 -0
  210. package/dist/workflow/context.js +350 -0
  211. package/dist/workflow/heartbeat.d.ts +15 -0
  212. package/dist/workflow/heartbeat.d.ts.map +1 -0
  213. package/dist/workflow/heartbeat.js +78 -0
  214. package/dist/workflow/index.d.ts +14 -0
  215. package/dist/workflow/index.d.ts.map +1 -0
  216. package/dist/workflow/index.js +10 -0
  217. package/dist/workflow/memory.d.ts +17 -0
  218. package/dist/workflow/memory.d.ts.map +1 -0
  219. package/dist/workflow/memory.js +184 -0
  220. package/package.json +73 -0
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Tool Executor Pod server — executes tool functions in isolated pods.
3
+ *
4
+ * Responsibilities:
5
+ * - Execute registered tool functions
6
+ * - Invoke LLMs routed from AER via SecureWrappedLLM
7
+ * - Return results to OE
8
+ *
9
+ * Mirrors Python's `ToolServer` in `server/tool.py`.
10
+ */
11
+ import type { ServerResponse } from "node:http";
12
+ import type { FastifyInstance } from "fastify";
13
+ import { BaseServer, type ITenantRuntime } from "./base.js";
14
+ /**
15
+ * Max time to wait for a slow SSE client to drain before aborting the stream.
16
+ * Prevents unbounded buffering and long-lived holds on the credential gate
17
+ * when a peer never reads.
18
+ */
19
+ export declare const SSE_DRAIN_TIMEOUT_MS = 30000;
20
+ /**
21
+ * Pipe SSE `data:` lines to a hijacked Node response with disconnect cancel,
22
+ * backpressure, and no process-crashing unhandled `error` events.
23
+ *
24
+ * Fastify's `reply.hijack()` removes framework lifecycle management; this
25
+ * helper owns the raw socket for the rest of the stream.
26
+ *
27
+ * Disconnect is detected via the *response* `close` event when the peer drops
28
+ * before `end()` — not via `IncomingMessage` `close`, which also fires after a
29
+ * normally-consumed POST body and would abort a valid stream.
30
+ *
31
+ * `lines` may be an AsyncIterable or a factory that receives the pipe's
32
+ * AbortSignal so upstream generation (retries/backoff) can stop promptly.
33
+ *
34
+ * Force-close paths (peer drop, drain timeout, socket error) call
35
+ * `res.destroy()` rather than `res.end()`: end only queues FIN behind data
36
+ * already stuck in the writable buffer, which never drains for a never-reading
37
+ * peer and leaks the socket/FD indefinitely.
38
+ */
39
+ export declare function pipeSseLinesToResponse(res: ServerResponse, lines: AsyncIterable<string> | ((signal: AbortSignal) => AsyncIterable<string>), opts?: {
40
+ drainTimeoutMs?: number;
41
+ signal?: AbortSignal;
42
+ onGeneratorSettled?: () => void;
43
+ }): Promise<void>;
44
+ /**
45
+ * Promise-chain mutex serializing async work within one ToolServer instance.
46
+ * Mirrors Python's asyncio.Lock on ToolServer._execute_gate.
47
+ * Exported for tests that build a ToolServer without its constructor.
48
+ */
49
+ export declare class AsyncMutex {
50
+ private locked;
51
+ private acquire;
52
+ runExclusive<T>(fn: () => Promise<T>): Promise<T>;
53
+ /**
54
+ * Generator form of runExclusive: the lock is held for the lifetime of the
55
+ * iteration and released when it completes, throws, or is closed early.
56
+ */
57
+ runExclusiveGen<T>(genFactory: () => AsyncGenerator<T>): AsyncGenerator<T>;
58
+ }
59
+ /**
60
+ * Thrown when an LLM lookup fails because the entrypoint registry load failed.
61
+ *
62
+ * The named-LLM registry is populated by running the user entrypoint. When that
63
+ * run throws, the registry is left empty (or holding only import-time
64
+ * registrations) and every lookup of an LLM the entrypoint would have
65
+ * registered fails with a bare "not registered" error -- which points the agent
66
+ * developer at their `app.llm()` calls instead of at the transient cause that
67
+ * actually broke the load. The real cause is named in the message only; it is
68
+ * deliberately not attached as `cause`, because the caller-facing response is
69
+ * rendered by `formatLlmError`, which walks `cause` and would dump a raw
70
+ * provider `.details`/`.body` from there, bypassing the message redaction.
71
+ * Mirrors Python's LLMRegistryLoadError.
72
+ */
73
+ export declare class LLMRegistryLoadError extends Error {
74
+ constructor(message: string, options?: ErrorOptions);
75
+ }
76
+ export declare class ToolServer extends BaseServer {
77
+ private readonly executeGate;
78
+ private readonly restrictionDisabled;
79
+ private readonly llmRegistryLock;
80
+ private llmRegistryLoaded;
81
+ private llmRegistryLoadError;
82
+ constructor(runtime: ITenantRuntime);
83
+ /**
84
+ * Run `fn` under restricted or unrestricted secret handling.
85
+ *
86
+ * Restricted (default): take `executeGate` and apply/restore metadata env.
87
+ * Unrestricted: merge metadata without restore and skip the gate.
88
+ */
89
+ private withCredentialIsolation;
90
+ /**
91
+ * Streaming variant of `withCredentialIsolation` for `/invoke_llm/stream`.
92
+ */
93
+ private withCredentialIsolationGen;
94
+ get modeName(): string;
95
+ onStartup(): Promise<void>;
96
+ /**
97
+ * Populate the registry at startup, retrying failed construction on requests.
98
+ * The lock ensures subsequent calls reuse successful preparation.
99
+ */
100
+ private ensureLlmRegistryLoaded;
101
+ onShutdown(): Promise<void>;
102
+ getHealthDetails(): Record<string, unknown>;
103
+ registerRoutes(app: FastifyInstance): void;
104
+ private handleGuardrailsCheck;
105
+ private handleExecute;
106
+ private doHandleExecute;
107
+ /**
108
+ * Resolve a tool name to a callable, falling back to a configured MCP tool.
109
+ *
110
+ * Mirrors Python's `ToolServer._resolve_tool()`. AER discovers remote MCP
111
+ * schemas at startup and forwards `mcp_server`/`mcp_tool` call metadata
112
+ * when invoking a Tool-Pod-dispatched MCP tool; the direct registry lookup
113
+ * misses for those names because MCP tools aren't pre-registered on the
114
+ * Tool Pod side (it skips `tools/list` startup discovery).
115
+ */
116
+ private resolveTool;
117
+ /**
118
+ * Create a BaseLLM-compatible instance for tool pod execution.
119
+ *
120
+ * Retrieves the named LLM from the registry (registered via
121
+ * `app.llm(llm, { llmId })`) and wraps it through the registered adapter
122
+ * factory. No SecureWrappedLLM needed — OE approval already happened in AER.
123
+ */
124
+ private createLlmForPod;
125
+ private handleInvokeLlm;
126
+ private doHandleInvokeLlm;
127
+ /**
128
+ * ensureLlmRegistryLoaded() then streamLlmChunks(), both inside the
129
+ * request's credential isolation window. Registry loading is normally a
130
+ * no-op after startup; the gate still covers the complete stream.
131
+ */
132
+ private loadRegistryThenStreamLlmChunks;
133
+ private registryLoadedLlmChunks;
134
+ private streamLlmChunks;
135
+ private handleInvokeLlmStream;
136
+ private invokeLlmStreamEvents;
137
+ }
138
+ //# sourceMappingURL=tool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/server/tool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAiE/C,OAAO,EAAE,UAAU,EAAE,KAAK,cAAc,EAAqB,MAAM,WAAW,CAAC;AAW/E;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,QAAS,CAAC;AAE3C;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,sBAAsB,CAC1C,GAAG,EAAE,cAAc,EACnB,KAAK,EACD,aAAa,CAAC,MAAM,CAAC,GACrB,CAAC,CAAC,MAAM,EAAE,WAAW,KAAK,aAAa,CAAC,MAAM,CAAC,CAAC,EACpD,IAAI,GAAE;IACJ,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,WAAW,CAAC;IAMrB,kBAAkB,CAAC,EAAE,MAAM,IAAI,CAAC;CAC5B,GACL,OAAO,CAAC,IAAI,CAAC,CA2Hf;AAqGD;;;;GAIG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAoC;YAEpC,OAAO;IAUf,YAAY,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IASvD;;;OAGG;IACI,eAAe,CAAC,CAAC,EACtB,UAAU,EAAE,MAAM,cAAc,CAAC,CAAC,CAAC,GAClC,cAAc,CAAC,CAAC,CAAC;CAQrB;AA0LD;;;;;;;;;;;;;GAaG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;gBACjC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD;AAED,qBAAa,UAAW,SAAQ,UAAU;IAKxC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;IAChD,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAU;IAE9C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAoB;IACpD,OAAO,CAAC,iBAAiB,CAAS;IAOlC,OAAO,CAAC,oBAAoB,CAAmC;gBAEnD,OAAO,EAAE,cAAc;IAKnC;;;;;OAKG;YACW,uBAAuB;IAQrC;;OAEG;YACY,0BAA0B;IAazC,IAAI,QAAQ,IAAI,MAAM,CAErB;IAEK,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAmChC;;;OAGG;YACW,uBAAuB;IA2B/B,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAKjC,gBAAgB,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAO3C,cAAc,CAAC,GAAG,EAAE,eAAe,GAAG,IAAI;IAuH1C,OAAO,CAAC,qBAAqB,CAK3B;IAEF,OAAO,CAAC,aAAa,CAInB;YAEY,eAAe;IAyG7B;;;;;;;;OAQG;IACH,OAAO,CAAC,WAAW;IAwCnB;;;;;;OAMG;IACH,OAAO,CAAC,eAAe;IAsCvB,OAAO,CAAC,eAAe,CAKrB;YAEY,iBAAiB;IA2D/B;;;;OAIG;YACY,+BAA+B;YAO/B,uBAAuB;YAQvB,eAAe;YAgFf,qBAAqB;YA0BrB,qBAAqB;CA0DrC"}