apps 0.0.1-beta.0

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 (135) hide show
  1. package/README.md +191 -0
  2. package/check-publish.mjs +10 -0
  3. package/framework-reference.json +1 -0
  4. package/js/chunk-22WRQZT7.js +389 -0
  5. package/js/chunk-2OTCEKL2.js +29480 -0
  6. package/js/chunk-36DNRFZQ.js +34 -0
  7. package/js/chunk-3ANFZFCV.js +1799 -0
  8. package/js/chunk-7JXHF5IF.js +138 -0
  9. package/js/chunk-BCSNKUAC.js +0 -0
  10. package/js/chunk-BKVL7XAJ.js +47 -0
  11. package/js/chunk-BPEFS4YF.js +1323 -0
  12. package/js/chunk-C24KEZXR.js +59 -0
  13. package/js/chunk-D6BKYP2L.js +877 -0
  14. package/js/chunk-EP6AEUZO.js +19 -0
  15. package/js/chunk-HP7E42X6.js +1719 -0
  16. package/js/chunk-KWTCHVLT.js +284 -0
  17. package/js/chunk-LOKGDT6B.js +28 -0
  18. package/js/chunk-MLKGABMK.js +9 -0
  19. package/js/chunk-OWD2YICM.js +2486 -0
  20. package/js/chunk-Q2FD5IBM.js +899 -0
  21. package/js/chunk-R6JMJSVT.js +590 -0
  22. package/js/chunk-SBKHUC5L.js +56 -0
  23. package/js/chunk-VFUZI3NY.js +75 -0
  24. package/js/chunk-W7Q5EOTS.js +4375 -0
  25. package/js/chunk-WIDTG42X.js +171 -0
  26. package/js/chunk-X3YBZCJW.js +246 -0
  27. package/js/chunk-YDR4CPJA.js +2357 -0
  28. package/js/client.js +1023 -0
  29. package/js/contracts.js +163 -0
  30. package/js/effect.js +44 -0
  31. package/js/graphql.js +392 -0
  32. package/js/host.js +193 -0
  33. package/js/index.js +284 -0
  34. package/js/mcp/effect.js +23 -0
  35. package/js/mcp/stdio.js +123 -0
  36. package/js/mcp.js +37 -0
  37. package/js/openapi.js +246 -0
  38. package/js/operations/approval.js +9 -0
  39. package/js/react.js +37 -0
  40. package/js/storage/facet.js +546 -0
  41. package/js/ui/auth/contracts.js +19 -0
  42. package/js/ui/auth.js +28 -0
  43. package/js/ui/contracts.js +24 -0
  44. package/js/ui/serving.js +286 -0
  45. package/package.json +116 -0
  46. package/runtime.json +1 -0
  47. package/types/app-data/src/contracts/database.d.ts +193 -0
  48. package/types/app-data/src/implementation/cursor.d.ts +10 -0
  49. package/types/app-data/src/implementation/index-key.d.ts +7 -0
  50. package/types/app-data/src/implementation/promise.d.ts +68 -0
  51. package/types/app-data/src/implementation/range.d.ts +8 -0
  52. package/types/app-data/src/implementation/schema.d.ts +33 -0
  53. package/types/app-data/src/implementation/sqlite.d.ts +11 -0
  54. package/types/app-data/src/index.d.ts +4 -0
  55. package/types/apps/src/approval.d.ts +10 -0
  56. package/types/apps/src/client.d.ts +5 -0
  57. package/types/apps/src/contracts/app.d.ts +58 -0
  58. package/types/apps/src/contracts/approval.d.ts +14 -0
  59. package/types/apps/src/contracts/context.d.ts +25 -0
  60. package/types/apps/src/contracts/elicitation.d.ts +86 -0
  61. package/types/apps/src/contracts/graphql.d.ts +102 -0
  62. package/types/apps/src/contracts/host.d.ts +568 -0
  63. package/types/apps/src/contracts/http.d.ts +20 -0
  64. package/types/apps/src/contracts/live.d.ts +26 -0
  65. package/types/apps/src/contracts/mcp.d.ts +93 -0
  66. package/types/apps/src/contracts/openapi.d.ts +105 -0
  67. package/types/apps/src/contracts/operations.d.ts +30 -0
  68. package/types/apps/src/contracts/optimistic.d.ts +22 -0
  69. package/types/apps/src/contracts/provider.d.ts +96 -0
  70. package/types/apps/src/contracts/schedules.d.ts +51 -0
  71. package/types/apps/src/contracts/schema.d.ts +20 -0
  72. package/types/apps/src/contracts/storage.d.ts +68 -0
  73. package/types/apps/src/contracts/tools.d.ts +11 -0
  74. package/types/apps/src/contracts/ui-auth.d.ts +28 -0
  75. package/types/apps/src/contracts/ui.d.ts +90 -0
  76. package/types/apps/src/contracts/webhook-protocol.d.ts +102 -0
  77. package/types/apps/src/contracts/webhooks.d.ts +53 -0
  78. package/types/apps/src/contracts/workflows.d.ts +277 -0
  79. package/types/apps/src/effect.d.ts +10 -0
  80. package/types/apps/src/facet.d.ts +5 -0
  81. package/types/apps/src/graphql.d.ts +10 -0
  82. package/types/apps/src/host.d.ts +16 -0
  83. package/types/apps/src/implementation/account-operations.d.ts +32 -0
  84. package/types/apps/src/implementation/app.d.ts +40 -0
  85. package/types/apps/src/implementation/authoring.d.ts +12 -0
  86. package/types/apps/src/implementation/elicitation.d.ts +103 -0
  87. package/types/apps/src/implementation/graphql.d.ts +5 -0
  88. package/types/apps/src/implementation/host.d.ts +3 -0
  89. package/types/apps/src/implementation/http.d.ts +5 -0
  90. package/types/apps/src/implementation/live.d.ts +18 -0
  91. package/types/apps/src/implementation/mcp-call.d.ts +19 -0
  92. package/types/apps/src/implementation/mcp-client.d.ts +50 -0
  93. package/types/apps/src/implementation/mcp-stdio.d.ts +4 -0
  94. package/types/apps/src/implementation/mcp-tools.d.ts +6 -0
  95. package/types/apps/src/implementation/mcp.d.ts +4 -0
  96. package/types/apps/src/implementation/openapi-request.d.ts +11 -0
  97. package/types/apps/src/implementation/openapi.d.ts +5 -0
  98. package/types/apps/src/implementation/operations.d.ts +65 -0
  99. package/types/apps/src/implementation/optimistic.d.ts +30 -0
  100. package/types/apps/src/implementation/protocol-operations.d.ts +24 -0
  101. package/types/apps/src/implementation/provider.d.ts +16 -0
  102. package/types/apps/src/implementation/query-commit.d.ts +10 -0
  103. package/types/apps/src/implementation/schedules.d.ts +16 -0
  104. package/types/apps/src/implementation/schema.d.ts +93 -0
  105. package/types/apps/src/implementation/storage.d.ts +13 -0
  106. package/types/apps/src/implementation/ui-auth.d.ts +13 -0
  107. package/types/apps/src/implementation/ui-client.d.ts +16 -0
  108. package/types/apps/src/implementation/ui-errors.d.ts +2 -0
  109. package/types/apps/src/implementation/ui-serving.d.ts +16 -0
  110. package/types/apps/src/implementation/ui-watch.d.ts +2 -0
  111. package/types/apps/src/implementation/webhooks.d.ts +29 -0
  112. package/types/apps/src/implementation/workflow-context.d.ts +8 -0
  113. package/types/apps/src/implementation/workflow-rpc.d.ts +5 -0
  114. package/types/apps/src/implementation/workflows.d.ts +27 -0
  115. package/types/apps/src/index.d.ts +58 -0
  116. package/types/apps/src/mcp-stdio.d.ts +8 -0
  117. package/types/apps/src/mcp.d.ts +10 -0
  118. package/types/apps/src/openapi.d.ts +10 -0
  119. package/types/apps/src/react.d.ts +7 -0
  120. package/types/apps/src/ui-auth.d.ts +3 -0
  121. package/types/apps/src/ui-serving.d.ts +3 -0
  122. package/types/telemetry/src/browser-operations.d.ts +16 -0
  123. package/types/telemetry/src/browser-performance.d.ts +11 -0
  124. package/types/telemetry/src/browser-request-timing.d.ts +2 -0
  125. package/types/telemetry/src/browser.d.ts +27 -0
  126. package/types/telemetry/src/config.d.ts +83 -0
  127. package/types/telemetry/src/context.d.ts +14 -0
  128. package/types/telemetry/src/index.d.ts +9 -0
  129. package/types/telemetry/src/layer.d.ts +7 -0
  130. package/types/telemetry/src/measurements.d.ts +8 -0
  131. package/types/telemetry/src/pending-span.d.ts +7 -0
  132. package/types/telemetry/src/relay.d.ts +28 -0
  133. package/types/telemetry/src/span-attributes.d.ts +29 -0
  134. package/types/telemetry/src/trace-context.d.ts +20 -0
  135. package/types/telemetry/src/transport.d.ts +6 -0
@@ -0,0 +1,899 @@
1
+ import {
2
+ authorDatabase,
3
+ isApp,
4
+ toEffectApp,
5
+ toPromise,
6
+ unavailableStorage
7
+ } from "./chunk-X3YBZCJW.js";
8
+ import {
9
+ nativeOperation
10
+ } from "./chunk-C24KEZXR.js";
11
+ import {
12
+ DeclaredProvider,
13
+ DeclaredRequirements,
14
+ HostAccountsInvalid,
15
+ HostDeclarationInvalid,
16
+ HostError,
17
+ HostEvaluationFailed,
18
+ HostInputInvalid,
19
+ HostOperationFailed,
20
+ HostOperationNotFound,
21
+ HostOutputInvalid,
22
+ HostRequest,
23
+ HostRequestInvalid,
24
+ HostToolApprovalRequired,
25
+ HostToolBlocked,
26
+ HostToolNotFound,
27
+ HostToolPolicyFailed,
28
+ HostedTool,
29
+ HostedWebhook,
30
+ ManualWebhookSetup,
31
+ OperationToolPrefixes,
32
+ ResolvedAccounts,
33
+ ToolResultObservation,
34
+ TrustedToolApproval,
35
+ WebhookResponseData,
36
+ defaultWebhookTransportLimits
37
+ } from "./chunk-22WRQZT7.js";
38
+ import {
39
+ ElicitationFailed,
40
+ ElicitationResponse,
41
+ FormElicitation,
42
+ HostedWorkflow,
43
+ ManyAccounts,
44
+ NonRetryableError,
45
+ WorkflowDuration,
46
+ WorkflowFailure,
47
+ WorkflowName,
48
+ WorkflowReplay,
49
+ WorkflowStepOptions,
50
+ WorkflowValue,
51
+ approvalElicitation,
52
+ workflowDurationMillis
53
+ } from "./chunk-HP7E42X6.js";
54
+ import {
55
+ captureTelemetry,
56
+ invocationFetch
57
+ } from "./chunk-OWD2YICM.js";
58
+ import {
59
+ importedJsonSchema,
60
+ jsonSchemaDecoder
61
+ } from "./chunk-BPEFS4YF.js";
62
+ import {
63
+ JsonObject,
64
+ JsonValue
65
+ } from "./chunk-36DNRFZQ.js";
66
+ import {
67
+ Cause_exports,
68
+ Effect_exports,
69
+ Encoding_exports,
70
+ Match_exports,
71
+ Option_exports,
72
+ Redacted_exports,
73
+ Schema_exports,
74
+ Stream_exports
75
+ } from "./chunk-2OTCEKL2.js";
76
+
77
+ // src/implementation/elicitation.ts
78
+ var prepareElicitation = (input) => Effect_exports.gen(function* () {
79
+ const request = yield* Schema_exports.decodeUnknownEffect(FormElicitation)(input, {
80
+ onExcessProperty: "error"
81
+ }).pipe(Effect_exports.mapError(() => new ElicitationFailed({ reason: "invalid-request" })));
82
+ const content = yield* jsonSchemaDecoder(request.requestedSchema).pipe(
83
+ Effect_exports.mapError(() => new ElicitationFailed({ reason: "invalid-request" }))
84
+ );
85
+ const respond = (input2) => Effect_exports.gen(function* () {
86
+ const response = yield* Schema_exports.decodeUnknownEffect(ElicitationResponse)(input2).pipe(
87
+ Effect_exports.catchTag(
88
+ "SchemaError",
89
+ () => Effect_exports.fail(new ElicitationFailed({ reason: "invalid-response" }))
90
+ )
91
+ );
92
+ if (response.action === "accept") {
93
+ const fields = response.content === void 0 ? {} : response.content;
94
+ yield* Schema_exports.decodeUnknownEffect(content)(fields).pipe(
95
+ Effect_exports.mapError(() => new ElicitationFailed({ reason: "invalid-response" }))
96
+ );
97
+ return { ...response, content: fields };
98
+ }
99
+ return response;
100
+ });
101
+ return { request, respond };
102
+ });
103
+ var makeElicit = (handler, signal) => toPromise(
104
+ (input) => Effect_exports.gen(function* () {
105
+ if (handler === void 0) return yield* new ElicitationFailed({ reason: "unavailable" });
106
+ const form = yield* prepareElicitation(input);
107
+ return yield* handler(form.request, signal).pipe(Effect_exports.flatMap(form.respond));
108
+ }),
109
+ signal
110
+ );
111
+
112
+ // src/contracts/approval.ts
113
+ var ApprovalDecision = Schema_exports.Literals(["approved", "denied", "user-approval"]);
114
+
115
+ // src/implementation/workflow-context.ts
116
+ var workflowSafe = (work) => work.pipe(
117
+ Effect_exports.catchCause((cause) => {
118
+ if (Cause_exports.hasInterrupts(cause)) return Effect_exports.interrupt;
119
+ const error = Cause_exports.squash(cause);
120
+ if (Schema_exports.is(WorkflowFailure)(error)) return Effect_exports.fail(error);
121
+ return Effect_exports.fail(
122
+ new WorkflowFailure({
123
+ reason: "execution",
124
+ retryable: !(error instanceof NonRetryableError)
125
+ })
126
+ );
127
+ })
128
+ );
129
+ var makeWorkflowContext = (execution, definition, fresh, signal) => Effect_exports.gen(function* () {
130
+ const services = yield* Effect_exports.context();
131
+ const counts = /* @__PURE__ */ new Map();
132
+ const identify = (kind, name) => Effect_exports.gen(function* () {
133
+ yield* Schema_exports.decodeUnknownEffect(WorkflowName)(name).pipe(
134
+ Effect_exports.mapError(() => new WorkflowFailure({ reason: "input", retryable: false }))
135
+ );
136
+ const key = JSON.stringify([kind, name]);
137
+ const count = (counts.get(key) ?? 0) + 1;
138
+ counts.set(key, count);
139
+ const digest = yield* Effect_exports.tryPromise({
140
+ try: () => crypto.subtle.digest(
141
+ "SHA-256",
142
+ new TextEncoder().encode(JSON.stringify([execution.runId, kind, name, count]))
143
+ ),
144
+ catch: () => new WorkflowFailure({ reason: "execution", retryable: false })
145
+ });
146
+ return Array.from(
147
+ new Uint8Array(digest),
148
+ (byte) => byte.toString(16).padStart(2, "0")
149
+ ).join("");
150
+ });
151
+ const attempt = (options, work) => {
152
+ const timeout = workflowDurationMillis(options.timeout ?? "10 minutes");
153
+ if (timeout === void 0)
154
+ return Effect_exports.fail(new WorkflowFailure({ reason: "input", retryable: false }));
155
+ return work.pipe(
156
+ Effect_exports.timeout(timeout),
157
+ Effect_exports.catchTag(
158
+ "TimeoutError",
159
+ () => Effect_exports.fail(new WorkflowFailure({ reason: "execution", retryable: true }))
160
+ )
161
+ );
162
+ };
163
+ const runStep = (name, options, callback) => Effect_exports.gen(function* () {
164
+ const stepId = yield* identify("do", name);
165
+ const parsed = yield* Schema_exports.decodeUnknownEffect(WorkflowStepOptions)(options).pipe(
166
+ Effect_exports.mapError(() => new WorkflowFailure({ reason: "input", retryable: false }))
167
+ );
168
+ return yield* execution.driver.do(
169
+ `do:${name.slice(0, 100)}:${stepId}`,
170
+ parsed,
171
+ () => attempt(
172
+ parsed,
173
+ Effect_exports.scoped(
174
+ Effect_exports.gen(function* () {
175
+ const controller = yield* Effect_exports.acquireRelease(
176
+ Effect_exports.sync(() => new AbortController()),
177
+ (controller2) => Effect_exports.sync(() => controller2.abort())
178
+ );
179
+ const ctx = yield* fresh(stepId, AbortSignal.any([signal, controller.signal]));
180
+ const result = yield* workflowSafe(
181
+ Effect_exports.tryPromise({ try: () => callback(ctx), catch: (error) => error })
182
+ );
183
+ return yield* Schema_exports.decodeUnknownEffect(WorkflowValue)(result).pipe(
184
+ Effect_exports.mapError(
185
+ () => new WorkflowFailure({ reason: "output", retryable: false })
186
+ )
187
+ );
188
+ })
189
+ ).pipe(Effect_exports.provideContext(services))
190
+ )
191
+ );
192
+ });
193
+ const operationStep = (kind, name, operation, input, options) => Effect_exports.gen(function* () {
194
+ const target = nativeOperation(operation);
195
+ const catalog = kind === "query" ? definition.queries : definition.mutations;
196
+ const match = Object.entries(catalog ?? {}).find(
197
+ ([, value]) => value === target && value.kind === kind
198
+ );
199
+ if (target === void 0 || match === void 0)
200
+ return yield* new WorkflowFailure({ reason: "operation", retryable: false });
201
+ const args = yield* Schema_exports.decodeUnknownEffect(JsonValue)(input).pipe(
202
+ Effect_exports.mapError(() => new WorkflowFailure({ reason: "input", retryable: false }))
203
+ );
204
+ const parsed = yield* Schema_exports.decodeUnknownEffect(WorkflowStepOptions)(options).pipe(
205
+ Effect_exports.mapError(() => new WorkflowFailure({ reason: "input", retryable: false }))
206
+ );
207
+ const stepId = yield* identify(kind, name);
208
+ const timeout = workflowDurationMillis(parsed.timeout ?? "10 minutes");
209
+ if (timeout === void 0)
210
+ return yield* new WorkflowFailure({ reason: "input", retryable: false });
211
+ return yield* execution.driver.do(
212
+ `${kind}:${name.slice(0, 100)}:${stepId}`,
213
+ parsed,
214
+ () => attempt(
215
+ parsed,
216
+ execution.invoke({ kind, name: match[0], input: args, stepId, timeout }).pipe(Effect_exports.provideContext(services))
217
+ )
218
+ );
219
+ });
220
+ const invoke = (effect) => toPromise(() => effect.pipe(Effect_exports.provideContext(services)), signal)();
221
+ const step = {
222
+ do: (name, options, run) => {
223
+ const callback = typeof options === "function" ? options : run;
224
+ if (callback === void 0)
225
+ return Promise.reject(new WorkflowFailure({ reason: "input", retryable: false }));
226
+ return invoke(runStep(name, typeof options === "function" ? {} : options, callback));
227
+ },
228
+ runQuery: (name, operation, input, options = {}) => invoke(operationStep("query", name, operation, input, options)),
229
+ runMutation: (name, operation, input, options = {}) => invoke(operationStep("mutation", name, operation, input, options)),
230
+ sleep: (name, duration) => invoke(
231
+ Schema_exports.decodeUnknownEffect(WorkflowDuration)(duration).pipe(
232
+ Effect_exports.mapError(() => new WorkflowFailure({ reason: "input", retryable: false })),
233
+ Effect_exports.flatMap(
234
+ (parsed) => identify("sleep", name).pipe(
235
+ Effect_exports.flatMap(
236
+ (id) => execution.driver.sleep(`sleep:${name.slice(0, 100)}:${id}`, parsed)
237
+ )
238
+ )
239
+ )
240
+ )
241
+ ),
242
+ sleepUntil: (name, timestamp) => invoke(
243
+ identify("until", name).pipe(
244
+ Effect_exports.flatMap(
245
+ (id) => execution.driver.sleepUntil(
246
+ `until:${name.slice(0, 100)}:${id}`,
247
+ timestamp instanceof Date ? timestamp.getTime() : timestamp
248
+ )
249
+ )
250
+ )
251
+ )
252
+ };
253
+ return { runId: execution.runId, step };
254
+ });
255
+
256
+ // src/implementation/webhooks.ts
257
+ var safe = (work, error) => Effect_exports.suspend(work).pipe(
258
+ Effect_exports.catchCause(
259
+ (cause) => Cause_exports.hasInterrupts(cause) ? Effect_exports.interrupt : Effect_exports.fail(error)
260
+ )
261
+ );
262
+ var dispatchWebhook = (definition, command, context, accounts) => Effect_exports.gen(function* () {
263
+ const hooks = definition.webhooks ?? {};
264
+ if (command.operation === "webhooks") {
265
+ return yield* Effect_exports.forEach(
266
+ Object.entries(hooks),
267
+ ([name, hook2]) => safe(
268
+ () => Effect_exports.gen(function* () {
269
+ if (!Object.hasOwn(accounts, hook2.account))
270
+ return yield* new HostDeclarationInvalid();
271
+ const describe = (schema) => {
272
+ const imported = importedJsonSchema(schema);
273
+ if (imported !== void 0) return Schema_exports.decodeUnknownEffect(JsonObject)(imported);
274
+ const document = Schema_exports.toJsonSchemaDocument(schema);
275
+ return Schema_exports.decodeUnknownEffect(JsonObject)({
276
+ ...document.schema,
277
+ $defs: document.definitions
278
+ });
279
+ };
280
+ const configSchema = yield* describe(hook2.config);
281
+ if (hook2.setup === void 0 ? hook2.register === void 0 || hook2.unregister === void 0 : hook2.register !== void 0 || hook2.unregister !== void 0)
282
+ return yield* new HostDeclarationInvalid();
283
+ const setup = hook2.setup === void 0 ? void 0 : {
284
+ ...yield* Schema_exports.decodeUnknownEffect(ManualWebhookSetup)(hook2.setup),
285
+ stateSchema: yield* describe(hook2.state)
286
+ };
287
+ return yield* Schema_exports.decodeUnknownEffect(HostedWebhook)({
288
+ name,
289
+ account: hook2.account,
290
+ configSchema,
291
+ ...setup === void 0 ? {} : { setup }
292
+ });
293
+ }),
294
+ new HostDeclarationInvalid()
295
+ )
296
+ );
297
+ }
298
+ const hook = Object.hasOwn(hooks, command.name) ? hooks[command.name] : void 0;
299
+ if (hook === void 0) return yield* new HostOperationNotFound();
300
+ const selected = accounts[hook.account];
301
+ const source = Array.isArray(selected) ? selected.find((account2) => account2.id === command.sourceAccount) : selected;
302
+ if (source === void 0 || source.id !== command.sourceAccount)
303
+ return yield* new HostInputInvalid();
304
+ const bound = context.accounts[hook.account];
305
+ const account = Array.isArray(bound) ? bound.find(
306
+ (value) => typeof value === "object" && value !== null && "id" in value && value.id === source.id
307
+ ) : bound;
308
+ const config = yield* safe(
309
+ () => Schema_exports.decodeUnknownEffect(hook.config)(command.config),
310
+ new HostInputInvalid()
311
+ );
312
+ if (command.operation === "webhook-validate")
313
+ return yield* safe(
314
+ () => Schema_exports.decodeUnknownEffect(JsonValue)(config),
315
+ new HostInputInvalid()
316
+ );
317
+ const input = {
318
+ config,
319
+ subscriptionId: command.subscriptionId,
320
+ account,
321
+ callbackUrl: command.callbackUrl,
322
+ secret: command.secret
323
+ };
324
+ const invoke = (callback, value) => safe(() => callback(context, value), new HostOperationFailed());
325
+ if (command.operation === "webhook-complete") {
326
+ if (hook.setup === void 0) return yield* new HostInputInvalid();
327
+ return yield* safe(
328
+ () => Schema_exports.decodeUnknownEffect(hook.state)(command.state).pipe(
329
+ Effect_exports.flatMap(Schema_exports.decodeUnknownEffect(JsonValue))
330
+ ),
331
+ new HostInputInvalid()
332
+ );
333
+ }
334
+ if (command.operation === "webhook-register") {
335
+ if (hook.register === void 0 || hook.setup !== void 0)
336
+ return yield* new HostOperationFailed();
337
+ const state2 = yield* invoke(hook.register, input);
338
+ return yield* safe(
339
+ () => Schema_exports.decodeUnknownEffect(hook.state)(state2).pipe(
340
+ Effect_exports.flatMap(Schema_exports.decodeUnknownEffect(JsonValue))
341
+ ),
342
+ new HostOutputInvalid()
343
+ );
344
+ }
345
+ const state = command.state === null ? null : yield* safe(
346
+ () => Schema_exports.decodeUnknownEffect(hook.state)(command.state),
347
+ new HostInputInvalid()
348
+ );
349
+ if (command.operation === "webhook-unregister") {
350
+ if (hook.unregister === void 0 || hook.setup !== void 0)
351
+ return yield* new HostOperationFailed();
352
+ yield* invoke(hook.unregister, { ...input, state });
353
+ return null;
354
+ }
355
+ const body = yield* Effect_exports.fromResult(Encoding_exports.decodeBase64(command.request.body)).pipe(
356
+ Effect_exports.mapError(() => new HostInputInvalid())
357
+ );
358
+ if (body.byteLength > defaultWebhookTransportLimits.maxBodyBytes)
359
+ return yield* new HostInputInvalid();
360
+ const request = yield* safe(
361
+ () => Effect_exports.sync(
362
+ () => new Request(command.request.url, {
363
+ method: command.request.method,
364
+ headers: command.request.headers,
365
+ ...context.signal === void 0 ? {} : { signal: context.signal },
366
+ ...command.request.method === "GET" || command.request.method === "HEAD" ? {} : { body: Uint8Array.from(body) }
367
+ })
368
+ ),
369
+ new HostInputInvalid()
370
+ );
371
+ const response = yield* invoke(hook.handle, { ...input, state, request });
372
+ if (!(response instanceof Response)) return yield* new HostOutputInvalid();
373
+ const chunks = [];
374
+ let size = 0;
375
+ const stream = response.body;
376
+ if (stream !== null)
377
+ yield* Stream_exports.fromReadableStream({
378
+ evaluate: () => stream,
379
+ onError: () => new HostOutputInvalid()
380
+ }).pipe(
381
+ Stream_exports.runForEach(
382
+ (chunk) => Effect_exports.gen(function* () {
383
+ size += chunk.byteLength;
384
+ if (size > defaultWebhookTransportLimits.maxBodyBytes)
385
+ return yield* new HostOutputInvalid();
386
+ chunks.push(chunk);
387
+ })
388
+ )
389
+ );
390
+ const bytes = new Uint8Array(size);
391
+ let offset = 0;
392
+ for (const chunk of chunks) {
393
+ bytes.set(chunk, offset);
394
+ offset += chunk.byteLength;
395
+ }
396
+ const headers = {};
397
+ response.headers.forEach((value, name) => {
398
+ headers[name] = value;
399
+ });
400
+ return yield* safe(
401
+ () => Schema_exports.decodeUnknownEffect(WebhookResponseData)({
402
+ status: response.status,
403
+ headers,
404
+ body: Encoding_exports.encodeBase64(new Uint8Array(bytes))
405
+ }),
406
+ new HostOutputInvalid()
407
+ );
408
+ });
409
+
410
+ // src/implementation/host.ts
411
+ function safe2(work, failure) {
412
+ return Effect_exports.suspend(work).pipe(
413
+ Effect_exports.catchCause(
414
+ (cause) => Cause_exports.hasInterrupts(cause) ? Effect_exports.interrupt : Effect_exports.fail(failure)
415
+ )
416
+ );
417
+ }
418
+ function jsonSchema(decoder) {
419
+ const imported = importedJsonSchema(decoder);
420
+ if (imported !== void 0) return Schema_exports.decodeUnknownEffect(JsonObject)(imported);
421
+ const document = Schema_exports.toJsonSchemaDocument(decoder);
422
+ return Schema_exports.decodeUnknownEffect(JsonObject)({
423
+ ...document.schema,
424
+ $defs: document.definitions,
425
+ $schema: "https://json-schema.org/draft/2020-12/schema"
426
+ });
427
+ }
428
+ function providerDeclaration(provider) {
429
+ return safe2(
430
+ () => Effect_exports.gen(function* () {
431
+ const auth = /* @__PURE__ */ new Map();
432
+ for (const [name, method] of Object.entries(provider.auth)) {
433
+ switch (method._tag) {
434
+ case "secrets":
435
+ auth.set(name, {
436
+ type: "secrets",
437
+ label: method.label,
438
+ fields: yield* jsonSchema(method.fields)
439
+ });
440
+ break;
441
+ case "oauth2":
442
+ auth.set(name, {
443
+ type: "oauth2",
444
+ ...method.config,
445
+ response: yield* jsonSchema(method.response)
446
+ });
447
+ break;
448
+ }
449
+ }
450
+ return yield* Schema_exports.decodeUnknownEffect(DeclaredProvider)({
451
+ name: provider.name,
452
+ auth: Object.fromEntries(auth)
453
+ });
454
+ }),
455
+ new HostDeclarationInvalid()
456
+ );
457
+ }
458
+ function requirements(slots, database) {
459
+ return Effect_exports.gen(function* () {
460
+ const accounts = /* @__PURE__ */ new Map();
461
+ for (const [slot, selection] of Object.entries(slots)) {
462
+ accounts.set(slot, {
463
+ cardinality: selection instanceof ManyAccounts ? "many" : "one",
464
+ definition: yield* providerDeclaration(
465
+ selection instanceof ManyAccounts ? selection.provider : selection
466
+ )
467
+ });
468
+ }
469
+ return yield* Schema_exports.decodeUnknownEffect(DeclaredRequirements)({
470
+ accounts: Object.fromEntries(accounts),
471
+ ...database === void 0 ? {} : { database }
472
+ }).pipe(Effect_exports.mapError(() => new HostDeclarationInvalid()));
473
+ });
474
+ }
475
+ function canonical(value) {
476
+ if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
477
+ if (value !== null && typeof value === "object") {
478
+ return `{${Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`).join(",")}}`;
479
+ }
480
+ return JSON.stringify(value);
481
+ }
482
+ function bindAccounts(slots, declarations, context) {
483
+ return safe2(
484
+ () => Effect_exports.gen(function* () {
485
+ const selected = yield* Schema_exports.decodeUnknownEffect(ResolvedAccounts)(
486
+ Redacted_exports.value(context.accounts)
487
+ );
488
+ if (Object.keys(selected).some((slot) => !Object.hasOwn(slots, slot))) {
489
+ return yield* Effect_exports.fail(new HostAccountsInvalid());
490
+ }
491
+ const bound = /* @__PURE__ */ new Map();
492
+ for (const [slot, selection] of Object.entries(slots)) {
493
+ const requirement = declarations.accounts[slot];
494
+ const supplied = Object.hasOwn(selected, slot) ? selected[slot] : void 0;
495
+ if (requirement === void 0 || supplied === void 0)
496
+ return yield* Effect_exports.fail(new HostAccountsInvalid());
497
+ const provider = selection instanceof ManyAccounts ? selection.provider : selection;
498
+ const many = selection instanceof ManyAccounts;
499
+ if (many !== Array.isArray(supplied))
500
+ return yield* Effect_exports.fail(new HostAccountsInvalid());
501
+ const accounts = Array.isArray(supplied) ? supplied : [supplied];
502
+ const ids = /* @__PURE__ */ new Set();
503
+ const values = [];
504
+ for (const account of accounts) {
505
+ if (ids.has(account.id)) return yield* Effect_exports.fail(new HostAccountsInvalid());
506
+ ids.add(account.id);
507
+ if (canonical(account.provider) !== canonical(requirement.definition) || !Object.hasOwn(provider.auth, account.method))
508
+ return yield* Effect_exports.fail(new HostAccountsInvalid());
509
+ const method = provider.auth[account.method];
510
+ if (method === void 0) return yield* Effect_exports.fail(new HostAccountsInvalid());
511
+ const fields = yield* Schema_exports.decodeUnknownEffect(
512
+ method._tag === "secrets" ? method.fields : method.response
513
+ )(account.fields);
514
+ values.push({ id: account.id, method: account.method, fields });
515
+ }
516
+ if (many) bound.set(slot, values);
517
+ else {
518
+ const account = values[0];
519
+ if (account === void 0) return yield* Effect_exports.fail(new HostAccountsInvalid());
520
+ bound.set(slot, account);
521
+ }
522
+ }
523
+ return { accounts: Object.fromEntries(bound) };
524
+ }),
525
+ new HostAccountsInvalid()
526
+ );
527
+ }
528
+ function dispatch(app, request, context, signal) {
529
+ return Effect_exports.scoped(
530
+ Effect_exports.gen(function* () {
531
+ if (!isApp(app)) return yield* Effect_exports.fail(new HostDeclarationInvalid());
532
+ const native = toEffectApp(app);
533
+ const declared = yield* requirements(native.accounts, native.database?.schema);
534
+ if (request.operation === "requirements")
535
+ return yield* safe2(
536
+ () => Schema_exports.decodeUnknownEffect(JsonValue)(declared),
537
+ new HostDeclarationInvalid()
538
+ );
539
+ const lifetime = yield* Effect_exports.acquireRelease(
540
+ Effect_exports.sync(() => new AbortController()),
541
+ (controller) => Effect_exports.sync(() => controller.abort())
542
+ );
543
+ const invocationSignal = AbortSignal.any([signal, lifetime.signal]);
544
+ let running;
545
+ let transactionOpen = false;
546
+ const delivery = (request2, signal2) => Effect_exports.suspend(
547
+ () => transactionOpen ? Effect_exports.fail(new ElicitationFailed({ reason: "transaction" })) : running !== void 0 && context.elicitation !== void 0 ? context.elicitation(request2, signal2).pipe(
548
+ Effect_exports.withSpan("app.tool.elicitation"),
549
+ Effect_exports.provideContext(running.context)
550
+ ) : Effect_exports.fail(new ElicitationFailed({ reason: "unavailable" }))
551
+ );
552
+ const unavailableWorkflow = () => Effect_exports.fail(new WorkflowFailure({ reason: "unavailable", retryable: false }));
553
+ const workflowControls = {
554
+ start: toPromise(
555
+ (input2) => context.workflowControls === void 0 ? unavailableWorkflow() : context.workflowControls.start(input2),
556
+ invocationSignal
557
+ ),
558
+ get: toPromise(
559
+ (input2) => context.workflowControls === void 0 ? unavailableWorkflow() : context.workflowControls.get(input2),
560
+ invocationSignal
561
+ ),
562
+ list: toPromise(
563
+ (input2) => context.workflowControls === void 0 ? unavailableWorkflow() : context.workflowControls.list(input2),
564
+ invocationSignal
565
+ ),
566
+ terminate: toPromise(
567
+ (input2) => context.workflowControls === void 0 ? unavailableWorkflow() : context.workflowControls.terminate(input2),
568
+ invocationSignal
569
+ )
570
+ };
571
+ const workflowReads = {
572
+ get: workflowControls.get,
573
+ list: workflowControls.list
574
+ };
575
+ const bound = {
576
+ ...yield* bindAccounts(native.accounts, declared, context).pipe(
577
+ Effect_exports.withSpan("app.accounts.bind")
578
+ ),
579
+ workflows: workflowReads,
580
+ signal: invocationSignal,
581
+ fetch: yield* invocationFetch(invocationSignal),
582
+ elicit: makeElicit(delivery, invocationSignal)
583
+ };
584
+ const definition = yield* safe2(
585
+ () => native.evaluate(bound).pipe(Effect_exports.withSpan("app.evaluate")),
586
+ new HostEvaluationFailed()
587
+ );
588
+ if (request.operation === "workflows") {
589
+ return yield* Effect_exports.forEach(
590
+ Object.entries(definition.workflows ?? {}),
591
+ ([name2, entry]) => safe2(
592
+ () => Effect_exports.gen(function* () {
593
+ return yield* Schema_exports.decodeUnknownEffect(HostedWorkflow)({
594
+ name: name2,
595
+ ...entry.description === void 0 ? {} : { description: entry.description },
596
+ inputSchema: yield* jsonSchema(entry.input),
597
+ ...entry.output === void 0 ? {} : { outputSchema: yield* jsonSchema(entry.output) }
598
+ });
599
+ }),
600
+ new HostDeclarationInvalid()
601
+ )
602
+ );
603
+ }
604
+ if (request.operation === "workflow-validate" || request.operation === "workflow-run") {
605
+ const entry = definition.workflows !== void 0 && Object.hasOwn(definition.workflows, request.name) ? definition.workflows[request.name] : void 0;
606
+ if (entry === void 0)
607
+ return yield* new WorkflowFailure({ reason: "not_found", retryable: false });
608
+ const input2 = yield* safe2(
609
+ () => Schema_exports.decodeUnknownEffect(entry.input)(request.input),
610
+ new WorkflowFailure({ reason: "input", retryable: false })
611
+ );
612
+ if (request.operation === "workflow-validate")
613
+ return yield* safe2(
614
+ () => Schema_exports.decodeUnknownEffect(WorkflowValue)(input2),
615
+ new WorkflowFailure({ reason: "input", retryable: false })
616
+ );
617
+ const execution = context.workflow;
618
+ if (execution === void 0)
619
+ return yield* new WorkflowFailure({ reason: "unavailable", retryable: false });
620
+ const workflowContext = yield* makeWorkflowContext(
621
+ execution,
622
+ definition,
623
+ (stepId, signal2) => Effect_exports.gen(function* () {
624
+ const current = yield* execution.resolve();
625
+ const accounts = yield* safe2(
626
+ () => bindAccounts(native.accounts, declared, current),
627
+ new WorkflowFailure({ reason: "credentials", retryable: false })
628
+ );
629
+ return {
630
+ ...accounts,
631
+ fetch: yield* invocationFetch(signal2),
632
+ signal: signal2,
633
+ runId: execution.runId,
634
+ stepId,
635
+ idempotencyKey: stepId
636
+ };
637
+ }),
638
+ invocationSignal
639
+ );
640
+ const output = yield* workflowSafe(entry.run(workflowContext, input2));
641
+ const outputSchema = entry.output;
642
+ const decoded = outputSchema === void 0 ? output : yield* safe2(
643
+ () => Schema_exports.decodeUnknownEffect(outputSchema)(output),
644
+ new WorkflowFailure({ reason: "output", retryable: false })
645
+ );
646
+ return yield* safe2(
647
+ () => Schema_exports.decodeUnknownEffect(WorkflowValue)(decoded),
648
+ new WorkflowFailure({ reason: "output", retryable: false })
649
+ );
650
+ }
651
+ if (request.operation === "inspect") {
652
+ const metadata = [];
653
+ for (const [prefix, readOnly, catalog2] of [
654
+ [OperationToolPrefixes.query, true, definition.queries],
655
+ [OperationToolPrefixes.mutate, false, definition.mutations]
656
+ ]) {
657
+ for (const [name2, operation] of Object.entries(catalog2 ?? {})) {
658
+ metadata.push(
659
+ yield* safe2(
660
+ () => Effect_exports.gen(function* () {
661
+ return yield* Schema_exports.decodeUnknownEffect(HostedTool)({
662
+ name: `${prefix}${name2}`,
663
+ schedules: Object.entries(definition.schedules ?? {}).filter(([, schedule]) => schedule.tool === `${prefix}${name2}`).map(([name3, { tool: _tool, ...schedule }]) => ({ name: name3, ...schedule })),
664
+ description: operation.description ?? `${readOnly ? "Query" : "Mutate"} ${name2}`,
665
+ ...operation.title === void 0 ? {} : { title: operation.title },
666
+ inputSchema: yield* jsonSchema(operation.input),
667
+ ...operation.output === void 0 ? operation.outputSchema === void 0 ? {} : { outputSchema: operation.outputSchema } : { outputSchema: yield* jsonSchema(operation.output) },
668
+ readOnly,
669
+ annotations: { ...operation.annotations, readOnlyHint: readOnly },
670
+ ...operation._meta === void 0 ? {} : { _meta: operation._meta }
671
+ });
672
+ }),
673
+ new HostDeclarationInvalid()
674
+ )
675
+ );
676
+ }
677
+ }
678
+ return metadata;
679
+ }
680
+ if (request.operation === "webhook-complete" || request.operation === "webhook-validate" || request.operation === "webhooks" || request.operation === "webhook-register" || request.operation === "webhook-handle" || request.operation === "webhook-unregister") {
681
+ const executeWebhook = (db) => dispatchWebhook(
682
+ definition,
683
+ request,
684
+ {
685
+ accounts: bound.accounts,
686
+ workflows: workflowControls,
687
+ signal: bound.signal,
688
+ fetch: bound.fetch,
689
+ ...db === void 0 || native.database === void 0 ? {} : {
690
+ db: authorDatabase(native.database.tables, db, invocationSignal, true)
691
+ }
692
+ },
693
+ Redacted_exports.value(context.accounts)
694
+ );
695
+ if (native.database === void 0 || !["webhook-register", "webhook-handle", "webhook-unregister"].includes(request.operation))
696
+ return yield* executeWebhook();
697
+ const storage2 = context.storage ?? unavailableStorage;
698
+ return yield* storage2.mutate(native.database.schema, executeWebhook).pipe(
699
+ Effect_exports.catchTags({
700
+ AppDatabaseError: () => Effect_exports.fail(new HostOperationFailed()),
701
+ AppStorageUnavailable: () => Effect_exports.fail(new HostOperationFailed()),
702
+ AppStorageError: () => Effect_exports.fail(new HostOperationFailed())
703
+ })
704
+ );
705
+ }
706
+ const kind = request.operation === "call" ? request.tool.startsWith(OperationToolPrefixes.query) ? "query" : request.tool.startsWith(OperationToolPrefixes.mutate) ? "mutate" : void 0 : request.operation;
707
+ if (kind === void 0) return yield* new HostToolNotFound();
708
+ const name = request.operation === "call" ? request.tool.slice(OperationToolPrefixes[kind].length) : request.name;
709
+ const toolName = `${OperationToolPrefixes[kind]}${name}`;
710
+ const catalog = kind === "query" ? definition.queries : definition.mutations;
711
+ const tool = catalog !== void 0 && Object.hasOwn(catalog, name) ? catalog[name] : void 0;
712
+ if (tool === void 0)
713
+ return yield* request.operation === "call" ? new HostToolNotFound() : new HostOperationNotFound();
714
+ const input = yield* safe2(
715
+ () => Schema_exports.decodeUnknownEffect(tool.input)(request.input),
716
+ new HostInputInvalid()
717
+ );
718
+ if (context.approval !== void 0) {
719
+ const approved = yield* safe2(
720
+ () => Schema_exports.decodeUnknownEffect(TrustedToolApproval)(context.approval),
721
+ new HostInputInvalid()
722
+ );
723
+ const decoded = yield* safe2(
724
+ () => Schema_exports.decodeUnknownEffect(JsonValue)(input),
725
+ new HostInputInvalid()
726
+ );
727
+ if (approved.tool !== toolName || !Schema_exports.toEquivalence(JsonValue)(approved.input, decoded)) {
728
+ return yield* new HostInputInvalid();
729
+ }
730
+ } else if (tool.approval !== void 0) {
731
+ const approval = tool.approval;
732
+ const decision = yield* safe2(
733
+ () => approval({ toolName, toolInput: input, signal: invocationSignal }).pipe(
734
+ Effect_exports.withSpan("app.tool.approval", {
735
+ attributes: { "executor.tool.name": toolName }
736
+ }),
737
+ Effect_exports.flatMap(Schema_exports.decodeUnknownEffect(ApprovalDecision))
738
+ ),
739
+ new HostToolPolicyFailed()
740
+ );
741
+ yield* Match_exports.value(decision).pipe(
742
+ Match_exports.when("approved", () => Effect_exports.void),
743
+ Match_exports.when("denied", () => Effect_exports.fail(new HostToolBlocked())),
744
+ Match_exports.when(
745
+ "user-approval",
746
+ () => safe2(() => Schema_exports.decodeUnknownEffect(JsonValue)(input), new HostInputInvalid()).pipe(
747
+ Effect_exports.flatMap(
748
+ (input2) => Effect_exports.fail(
749
+ new HostToolApprovalRequired({
750
+ input: input2,
751
+ elicitation: approvalElicitation(toolName, input2)
752
+ })
753
+ )
754
+ )
755
+ )
756
+ ),
757
+ Match_exports.exhaustive
758
+ );
759
+ }
760
+ const execute = (db) => Effect_exports.gen(function* () {
761
+ transactionOpen = db !== void 0;
762
+ const output = yield* Effect_exports.gen(function* () {
763
+ running = yield* captureTelemetry;
764
+ const fetch = yield* invocationFetch(invocationSignal);
765
+ return yield* tool.run(
766
+ {
767
+ ...bound,
768
+ fetch,
769
+ workflows: kind === "mutate" ? workflowControls : workflowReads,
770
+ ...db === void 0 || native.database === void 0 ? {} : {
771
+ db: authorDatabase(
772
+ native.database.tables,
773
+ db,
774
+ invocationSignal,
775
+ kind === "mutate"
776
+ )
777
+ }
778
+ },
779
+ input
780
+ );
781
+ }).pipe(
782
+ Effect_exports.withSpan("app.operation.execute", {
783
+ attributes: { "executor.tool.name": toolName, "executor.operation": kind }
784
+ }),
785
+ Effect_exports.ensuring(
786
+ Effect_exports.sync(() => {
787
+ running = void 0;
788
+ })
789
+ ),
790
+ Effect_exports.catchCause((cause) => {
791
+ if (Cause_exports.hasInterrupts(cause)) return Effect_exports.interrupt;
792
+ const failure = Schema_exports.decodeUnknownOption(ElicitationFailed)(Cause_exports.squash(cause));
793
+ return Effect_exports.fail(
794
+ Option_exports.isSome(failure) ? failure.value : new HostOperationFailed()
795
+ );
796
+ })
797
+ );
798
+ const outputSchema = tool.output;
799
+ const decoded = outputSchema === void 0 ? output : yield* safe2(
800
+ () => Schema_exports.decodeUnknownEffect(outputSchema)(output),
801
+ new HostOutputInvalid()
802
+ );
803
+ return yield* safe2(
804
+ () => Schema_exports.decodeUnknownEffect(JsonValue)(decoded),
805
+ new HostOutputInvalid()
806
+ );
807
+ });
808
+ const replay = context.replay === void 0 ? void 0 : yield* safe2(
809
+ () => Schema_exports.decodeUnknownEffect(WorkflowReplay)(context.replay),
810
+ new HostInputInvalid()
811
+ );
812
+ if (replay !== void 0 && kind !== "mutate") return yield* new HostInputInvalid();
813
+ if (native.database === void 0) return yield* execute();
814
+ const storage = context.storage ?? unavailableStorage;
815
+ return yield* storage[kind === "query" ? "read" : "mutate"](
816
+ native.database.schema,
817
+ (db) => replay === void 0 ? execute(db) : db.once(replay.key, replay.fingerprint, () => execute(db))
818
+ ).pipe(
819
+ Effect_exports.catchTags({
820
+ AppDatabaseError: () => Effect_exports.fail(new HostOperationFailed()),
821
+ AppStorageUnavailable: () => Effect_exports.fail(new HostOperationFailed()),
822
+ AppStorageError: () => Effect_exports.fail(new HostOperationFailed())
823
+ })
824
+ );
825
+ })
826
+ );
827
+ }
828
+ var errorStatus = Match_exports.type().pipe(
829
+ Match_exports.tagsExhaustive({
830
+ WorkflowFailure: () => 422,
831
+ HostRequestInvalid: () => 400,
832
+ HostAccountsInvalid: () => 422,
833
+ HostInputInvalid: () => 422,
834
+ HostOperationNotFound: () => 404,
835
+ HostToolNotFound: () => 404,
836
+ HostToolBlocked: () => 403,
837
+ HostToolApprovalRequired: () => 409,
838
+ HostToolPolicyFailed: () => 500,
839
+ HostOperationFailed: () => 500,
840
+ HostDeclarationInvalid: () => 500,
841
+ HostEvaluationFailed: () => 500,
842
+ HostOutputInvalid: () => 500,
843
+ ElicitationFailed: () => 422
844
+ })
845
+ );
846
+ var createAppHandler = (app) => (request, context) => Effect_exports.gen(function* () {
847
+ if (request.method !== "POST") return yield* Effect_exports.fail(new HostRequestInvalid());
848
+ const input = yield* Effect_exports.tryPromise({
849
+ try: () => request.json(),
850
+ catch: () => new HostRequestInvalid()
851
+ });
852
+ const command = yield* Schema_exports.decodeUnknownEffect(HostRequest)(input, {
853
+ onExcessProperty: "error"
854
+ }).pipe(Effect_exports.mapError(() => new HostRequestInvalid()));
855
+ yield* Effect_exports.annotateCurrentSpan({
856
+ "executor.operation": command.operation,
857
+ ...command.operation === "call" ? { "executor.tool.name": command.tool } : {}
858
+ });
859
+ let toolError = false;
860
+ const value = yield* dispatch(app, command, context, request.signal).pipe(
861
+ Effect_exports.provideService(ToolResultObservation, {
862
+ failed: () => {
863
+ toolError = true;
864
+ }
865
+ }),
866
+ Effect_exports.withSpan(`app.${command.operation}`)
867
+ );
868
+ if (toolError)
869
+ yield* Effect_exports.annotateCurrentSpan({
870
+ "executor.outcome": "failed",
871
+ "error.type": "McpToolError"
872
+ });
873
+ return Response.json({ ok: true, value, ...toolError ? { toolError: true } : {} });
874
+ }).pipe(
875
+ Effect_exports.catch(
876
+ (error) => Schema_exports.encodeEffect(HostError)(error).pipe(
877
+ Effect_exports.map(
878
+ (encoded) => Response.json({ ok: false, error: encoded }, { status: errorStatus(error) })
879
+ ),
880
+ Effect_exports.orDie
881
+ )
882
+ ),
883
+ Effect_exports.catchCause(
884
+ (cause) => Cause_exports.hasInterrupts(cause) ? Effect_exports.interrupt : Effect_exports.logError(cause).pipe(
885
+ Effect_exports.as(
886
+ Response.json(
887
+ { ok: false, error: { _tag: "HostDeclarationInvalid" } },
888
+ { status: 500 }
889
+ )
890
+ )
891
+ )
892
+ )
893
+ );
894
+
895
+ export {
896
+ prepareElicitation,
897
+ ApprovalDecision,
898
+ createAppHandler
899
+ };