apps 0.0.1-beta.12 → 0.0.1-beta.121

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 (199) hide show
  1. package/README.md +60 -42
  2. package/framework-reference.json +1 -1
  3. package/js/{chunk-ODEIDRGW.js → chunk-2EEUUSL7.js} +807 -179
  4. package/js/chunk-3KNB4HRA.js +95 -0
  5. package/js/chunk-3OADIFKV.js +576 -0
  6. package/js/chunk-573E3S2U.js +5339 -0
  7. package/js/{chunk-RFVIONFX.js → chunk-5IWCEMLY.js} +136 -45
  8. package/js/{chunk-UK573G7W.js → chunk-5TYZML5C.js} +87 -51
  9. package/js/chunk-AUZGGAPP.js +449 -0
  10. package/js/chunk-C23S3WPT.js +175 -0
  11. package/js/chunk-CCJUTN55.js +88 -0
  12. package/js/chunk-CL3K7BJD.js +49 -0
  13. package/js/{chunk-5T57N25Q.js → chunk-DA6RXOMJ.js} +643 -69
  14. package/js/chunk-DONRL25A.js +713 -0
  15. package/js/{chunk-57RKNWL2.js → chunk-EAPSQBFN.js} +4 -4
  16. package/js/{chunk-JU7VHODU.js → chunk-EPZ3P7IX.js} +65 -10
  17. package/js/{chunk-YQNLXHHM.js → chunk-GU25DFBC.js} +931 -289
  18. package/js/{chunk-VFZKSM3T.js → chunk-KJW5IPM4.js} +3 -3
  19. package/js/chunk-KY4KYPUZ.js +335 -0
  20. package/js/{chunk-KUMBKVJD.js → chunk-MJUHAHDG.js} +2 -2
  21. package/js/chunk-NCOPALJZ.js +90 -0
  22. package/js/chunk-OD3KFKZD.js +1617 -0
  23. package/js/chunk-OLUDW4HL.js +1016 -0
  24. package/js/{chunk-M3B3DE4K.js → chunk-QUFLMMTG.js} +463 -218
  25. package/js/{chunk-IPI4MFSN.js → chunk-SO33LZ7J.js} +2 -2
  26. package/js/{chunk-CDL5PM2Q.js → chunk-U2GHFWX2.js} +19257 -18404
  27. package/js/{chunk-WVVV4TX2.js → chunk-UPICNVJO.js} +4 -3
  28. package/js/{chunk-IJX4CYZO.js → chunk-VXBCDIWD.js} +228 -264
  29. package/js/client.js +148 -84
  30. package/js/contracts.js +152 -21
  31. package/js/effect.js +18 -19
  32. package/js/{es-APDZKREY.js → es-DIULA3ZF.js} +62 -1659
  33. package/js/graphql.js +231 -188
  34. package/js/host.js +116 -78
  35. package/js/index.js +150 -300
  36. package/js/mcp/effect.js +21 -21
  37. package/js/mcp.js +91 -57
  38. package/js/openapi.js +4357 -1415
  39. package/js/react.js +2 -3
  40. package/js/skills/effect.js +6 -3
  41. package/js/skills.js +220 -81
  42. package/js/storage/facet.js +1 -540
  43. package/js/ui/auth/contracts.js +2 -2
  44. package/js/ui/auth.js +5 -5
  45. package/js/ui/contracts.js +9 -8
  46. package/js/ui/serving.js +8618 -235
  47. package/package.json +3 -8
  48. package/runtime.json +1 -1
  49. package/types/app-cache/src/contracts/cache.d.ts +18 -0
  50. package/types/app-cache/src/index.d.ts +34 -2
  51. package/types/apps/src/contracts/api-response-error.d.ts +4 -0
  52. package/types/apps/src/contracts/app.d.ts +56 -11
  53. package/types/apps/src/contracts/cache.d.ts +20 -0
  54. package/types/apps/src/contracts/context.d.ts +60 -17
  55. package/types/apps/src/contracts/elicitation.d.ts +23 -7
  56. package/types/apps/src/contracts/events.d.ts +94 -0
  57. package/types/apps/src/contracts/failure.d.ts +30 -1
  58. package/types/apps/src/contracts/graphql.d.ts +40 -2
  59. package/types/apps/src/contracts/host.d.ts +1457 -20
  60. package/types/apps/src/contracts/icons.d.ts +28 -0
  61. package/types/apps/src/contracts/live.d.ts +2 -0
  62. package/types/apps/src/contracts/mcp.d.ts +81 -20
  63. package/types/apps/src/contracts/network.d.ts +120 -0
  64. package/types/apps/src/contracts/openapi-compile.d.ts +82 -3
  65. package/types/apps/src/contracts/openapi-document.d.ts +1 -1
  66. package/types/apps/src/contracts/openapi.d.ts +41 -9
  67. package/types/apps/src/contracts/operations.d.ts +14 -7
  68. package/types/apps/src/contracts/placement.d.ts +102 -0
  69. package/types/apps/src/contracts/protocol-version.d.ts +2 -2
  70. package/types/apps/src/contracts/protocols/1.d.ts +733 -81
  71. package/types/apps/src/contracts/protocols/10.d.ts +1450 -0
  72. package/types/apps/src/contracts/protocols/11.d.ts +1149 -0
  73. package/types/apps/src/contracts/protocols/12.d.ts +3310 -0
  74. package/types/apps/src/contracts/protocols/13.d.ts +6001 -0
  75. package/types/apps/src/contracts/protocols/2.d.ts +69 -26
  76. package/types/apps/src/contracts/protocols/3.d.ts +133 -33
  77. package/types/apps/src/contracts/protocols/4.d.ts +122 -45
  78. package/types/apps/src/contracts/protocols/5.d.ts +157 -47
  79. package/types/apps/src/contracts/protocols/6.d.ts +1629 -0
  80. package/types/apps/src/contracts/protocols/7.d.ts +1785 -0
  81. package/types/apps/src/contracts/protocols/8.d.ts +1182 -0
  82. package/types/apps/src/contracts/protocols/9.d.ts +1104 -0
  83. package/types/apps/src/contracts/protocols/current.d.ts +6870 -0
  84. package/types/apps/src/contracts/protocols/oauth-6.d.ts +74 -0
  85. package/types/apps/src/contracts/protocols/oauth.d.ts +134 -0
  86. package/types/apps/src/contracts/provider-error.d.ts +9 -0
  87. package/types/apps/src/contracts/provider.d.ts +82 -5
  88. package/types/apps/src/contracts/router.d.ts +2 -1
  89. package/types/apps/src/contracts/schedules.d.ts +7 -1
  90. package/types/apps/src/contracts/schema.d.ts +21 -0
  91. package/types/apps/src/contracts/shared-catalog.d.ts +11 -0
  92. package/types/apps/src/contracts/skills.d.ts +50 -13
  93. package/types/apps/src/contracts/sql.d.ts +63 -0
  94. package/types/apps/src/contracts/ui.d.ts +70 -30
  95. package/types/apps/src/contracts/webhooks.d.ts +24 -7
  96. package/types/apps/src/contracts/workflows.d.ts +56 -34
  97. package/types/apps/src/facet.d.ts +4 -5
  98. package/types/apps/src/host.d.ts +5 -0
  99. package/types/apps/src/implementation/account-router.d.ts +20 -3
  100. package/types/apps/src/implementation/app.d.ts +41 -14
  101. package/types/apps/src/implementation/authoring.d.ts +25 -5
  102. package/types/apps/src/implementation/browser-scripts.gen.d.ts +6 -0
  103. package/types/apps/src/implementation/cache.d.ts +29 -2
  104. package/types/apps/src/implementation/catalog-cache.d.ts +35 -4
  105. package/types/apps/src/implementation/elicitation.d.ts +18 -15
  106. package/types/apps/src/implementation/events.d.ts +64 -0
  107. package/types/apps/src/implementation/failure-detail.d.ts +28 -5
  108. package/types/apps/src/implementation/graphql-catalog.d.ts +3 -3
  109. package/types/apps/src/implementation/graphql.d.ts +4 -3
  110. package/types/apps/src/implementation/input-problems.d.ts +6 -2
  111. package/types/apps/src/implementation/invocation-timing.d.ts +18 -0
  112. package/types/apps/src/implementation/live.d.ts +2 -2
  113. package/types/apps/src/implementation/mcp-call.d.ts +6 -5
  114. package/types/apps/src/implementation/mcp-catalog.d.ts +6 -4
  115. package/types/apps/src/implementation/mcp-client.d.ts +17 -9
  116. package/types/apps/src/implementation/mcp-tools.d.ts +2 -1
  117. package/types/apps/src/implementation/mcp.d.ts +20 -9
  118. package/types/apps/src/implementation/network.d.ts +16 -0
  119. package/types/apps/src/implementation/openapi-compile.d.ts +38 -7
  120. package/types/apps/src/implementation/openapi-document.d.ts +50 -20
  121. package/types/apps/src/implementation/openapi-names.d.ts +9 -4
  122. package/types/apps/src/implementation/openapi-request.d.ts +10 -2
  123. package/types/apps/src/implementation/openapi-source.d.ts +23 -1
  124. package/types/apps/src/implementation/openapi-upgrade.d.ts +4 -0
  125. package/types/apps/src/implementation/operations.d.ts +58 -7
  126. package/types/apps/src/implementation/optimistic.d.ts +1 -1
  127. package/types/apps/src/implementation/placement.d.ts +60 -0
  128. package/types/apps/src/implementation/protocol-operations.d.ts +3 -0
  129. package/types/apps/src/implementation/provider-error.d.ts +28 -1
  130. package/types/apps/src/implementation/provider.d.ts +19 -4
  131. package/types/apps/src/implementation/query-commit.d.ts +1 -1
  132. package/types/apps/src/implementation/router-catalog.d.ts +19 -94
  133. package/types/apps/src/implementation/router.d.ts +26 -6
  134. package/types/apps/src/implementation/runtime-yield.d.ts +7 -0
  135. package/types/apps/src/implementation/schedules.d.ts +39 -8
  136. package/types/apps/src/implementation/schema.d.ts +108 -11
  137. package/types/apps/src/implementation/skills.d.ts +18 -24
  138. package/types/apps/src/implementation/sql.d.ts +71 -0
  139. package/types/apps/src/implementation/ui-account-notice.d.ts +9 -0
  140. package/types/apps/src/implementation/ui-auth.d.ts +1 -1
  141. package/types/apps/src/implementation/ui-client.d.ts +3 -3
  142. package/types/apps/src/implementation/ui-document.d.ts +5 -0
  143. package/types/apps/src/implementation/ui-errors.d.ts +1 -1
  144. package/types/apps/src/implementation/ui-serving.d.ts +38 -12
  145. package/types/apps/src/implementation/upstream-error.d.ts +17 -0
  146. package/types/apps/src/implementation/webhooks.d.ts +15 -6
  147. package/types/apps/src/implementation/workflow-context.d.ts +6 -2
  148. package/types/apps/src/implementation/workflows.d.ts +11 -4
  149. package/types/apps/src/index.d.ts +51 -16
  150. package/types/apps/src/mcp.d.ts +20 -1
  151. package/types/apps/src/openapi.d.ts +22 -1
  152. package/types/apps/src/react.d.ts +1 -1
  153. package/types/apps/src/skills.d.ts +10 -8
  154. package/types/apps/src/ui-serving.d.ts +1 -2
  155. package/types/telemetry/src/app-records.d.ts +42 -0
  156. package/types/telemetry/src/browser-operations.d.ts +2 -2
  157. package/types/telemetry/src/browser.d.ts +8 -3
  158. package/types/telemetry/src/config.d.ts +2 -2
  159. package/types/telemetry/src/index.d.ts +2 -1
  160. package/types/telemetry/src/layer.d.ts +10 -4
  161. package/types/telemetry/src/measurements.d.ts +1 -1
  162. package/types/telemetry/src/ownership.d.ts +60 -0
  163. package/types/telemetry/src/recorded-failure.d.ts +29 -0
  164. package/types/telemetry/src/relay.d.ts +16 -6
  165. package/types/telemetry/src/span-attributes.d.ts +17 -1
  166. package/types/telemetry/src/transport.d.ts +8 -1
  167. package/types/utils/src/api-error.d.ts +55 -0
  168. package/types/utils/src/connection-failure.d.ts +33 -0
  169. package/types/utils/src/legacy-keys.d.ts +20 -0
  170. package/types/utils/src/recorded-message.d.ts +18 -0
  171. package/types/utils/src/schema-definitions.d.ts +13 -0
  172. package/types/utils/src/user-facing-error.d.ts +83 -0
  173. package/js/chunk-27F7W2LG.js +0 -34
  174. package/js/chunk-GPFVWWDZ.js +0 -209
  175. package/js/chunk-KRSCJDTW.js +0 -2668
  176. package/js/chunk-MKWZPDR6.js +0 -63
  177. package/js/chunk-N7YDVXYJ.js +0 -186
  178. package/js/chunk-NKLWZJLE.js +0 -172
  179. package/js/chunk-PJJ24GU7.js +0 -877
  180. package/js/chunk-PZB6T6KU.js +0 -19
  181. package/js/chunk-R3EV5FTO.js +0 -97
  182. package/js/chunk-S6YVNPFS.js +0 -224
  183. package/js/chunk-TYVDBQSI.js +0 -242
  184. package/js/chunk-Y32GRPVD.js +0 -299
  185. package/js/chunk-Z2QY3YTF.js +0 -507
  186. package/js/chunk-ZQINWV4W.js +0 -630
  187. package/js/mcp/stdio.js +0 -128
  188. package/types/app-data/src/implementation/cursor.d.ts +0 -10
  189. package/types/app-data/src/implementation/index-key.d.ts +0 -7
  190. package/types/app-data/src/implementation/promise.d.ts +0 -68
  191. package/types/app-data/src/implementation/range.d.ts +0 -8
  192. package/types/app-data/src/implementation/schema.d.ts +0 -18
  193. package/types/app-data/src/implementation/sqlite.d.ts +0 -11
  194. package/types/app-data/src/index.d.ts +0 -4
  195. package/types/apps/src/contracts/storage.d.ts +0 -68
  196. package/types/apps/src/implementation/mcp-stdio.d.ts +0 -4
  197. package/types/apps/src/implementation/storage.d.ts +0 -15
  198. package/types/apps/src/implementation/ui-watch.d.ts +0 -2
  199. package/types/apps/src/mcp-stdio.d.ts +0 -5
@@ -0,0 +1,95 @@
1
+ import {
2
+ JsonValue,
3
+ withLegacyKeys
4
+ } from "./chunk-NCOPALJZ.js";
5
+ import {
6
+ HttpApiEndpoint_exports,
7
+ HttpApiGroup_exports,
8
+ HttpApiSchema_exports,
9
+ HttpApi_exports
10
+ } from "./chunk-VXBCDIWD.js";
11
+ import {
12
+ Schema_exports
13
+ } from "./chunk-U2GHFWX2.js";
14
+
15
+ // src/contracts/ui.ts
16
+ var UiContextFields = Schema_exports.Struct({
17
+ version: Schema_exports.NonEmptyString,
18
+ accounts: Schema_exports.optional(
19
+ Schema_exports.Record(Schema_exports.NonEmptyString, Schema_exports.Array(Schema_exports.NonEmptyString))
20
+ )
21
+ });
22
+ var UiContext = UiContextFields.pipe(withLegacyKeys({ version: "deployment" }));
23
+ var UiOperation = Schema_exports.Struct({
24
+ ...UiContextFields.fields,
25
+ name: Schema_exports.NonEmptyString,
26
+ input: JsonValue
27
+ }).pipe(withLegacyKeys({ version: "deployment" }));
28
+ var UiUnauthorized = class extends Schema_exports.TaggedError()(
29
+ "UiUnauthorized",
30
+ {},
31
+ { httpApiStatus: 401 }
32
+ ) {
33
+ };
34
+ var UiForbidden = class extends Schema_exports.TaggedError()(
35
+ "UiForbidden",
36
+ {},
37
+ { httpApiStatus: 403 }
38
+ ) {
39
+ };
40
+ var UiVersionChanged = class extends Schema_exports.TaggedError()(
41
+ "UiDeploymentChanged",
42
+ {},
43
+ { httpApiStatus: 409 }
44
+ ) {
45
+ };
46
+ var UiFailed = class extends Schema_exports.TaggedError()(
47
+ "UiFailed",
48
+ {
49
+ reason: Schema_exports.Literals(["unavailable", "operation_failed", "account_required"])
50
+ },
51
+ { httpApiStatus: 422 }
52
+ ) {
53
+ };
54
+ var UiSnapshot = Schema_exports.Union([
55
+ Schema_exports.Struct({
56
+ type: Schema_exports.Literal("snapshot"),
57
+ value: JsonValue,
58
+ trace: Schema_exports.optional(Schema_exports.Unknown)
59
+ }),
60
+ Schema_exports.Struct({ type: Schema_exports.Literal("failure"), error: UiFailed }),
61
+ Schema_exports.Struct({ type: Schema_exports.Literal("heartbeat") })
62
+ ]);
63
+ var errors = [UiUnauthorized, UiForbidden, UiVersionChanged, UiFailed];
64
+ var AppUiApi = HttpApi_exports.make("app-ui").add(
65
+ HttpApiGroup_exports.make("ui").add(
66
+ HttpApiEndpoint_exports.post("query", "/_executor/api/query", {
67
+ payload: UiOperation,
68
+ success: JsonValue,
69
+ error: errors
70
+ })
71
+ ).add(
72
+ HttpApiEndpoint_exports.post("mutate", "/_executor/api/mutate", {
73
+ payload: UiOperation,
74
+ success: JsonValue,
75
+ error: errors
76
+ })
77
+ ).add(
78
+ HttpApiEndpoint_exports.post("subscribe", "/_executor/api/subscribe", {
79
+ payload: UiOperation,
80
+ success: HttpApiSchema_exports.StreamSse({ data: UiSnapshot, error: Schema_exports.Union(errors) }),
81
+ error: errors
82
+ })
83
+ )
84
+ );
85
+
86
+ export {
87
+ UiContext,
88
+ UiOperation,
89
+ UiUnauthorized,
90
+ UiForbidden,
91
+ UiVersionChanged,
92
+ UiFailed,
93
+ UiSnapshot,
94
+ AppUiApi
95
+ };
@@ -0,0 +1,576 @@
1
+ import {
2
+ accountProviderError,
3
+ fromPromise
4
+ } from "./chunk-C23S3WPT.js";
5
+ import {
6
+ HostedTool,
7
+ HostedToolSummary,
8
+ RouterKey,
9
+ RouterMeta,
10
+ reservedRouterKeys
11
+ } from "./chunk-573E3S2U.js";
12
+ import {
13
+ combineJsonSchemas,
14
+ decoderOf,
15
+ importedJsonSchema,
16
+ once,
17
+ schemaArgument,
18
+ withLazyJsonSchemaDocument
19
+ } from "./chunk-DA6RXOMJ.js";
20
+ import {
21
+ JsonObject
22
+ } from "./chunk-NCOPALJZ.js";
23
+ import {
24
+ Effect_exports,
25
+ Result_exports,
26
+ Schema_exports
27
+ } from "./chunk-U2GHFWX2.js";
28
+
29
+ // src/implementation/operations.ts
30
+ var NativeOperation = /* @__PURE__ */ Symbol("apps.Operation");
31
+ var NativeRouterKey = /* @__PURE__ */ Symbol("apps.Router");
32
+ var operationDeclaration = (operation) => ({
33
+ kind: operation.kind,
34
+ [NativeOperation]: operation
35
+ });
36
+ var nativeOperation = (value) => {
37
+ if (typeof value !== "object" || value === null || !(NativeOperation in value)) return void 0;
38
+ return value[NativeOperation];
39
+ };
40
+ var operationOptions = (options) => {
41
+ const approval = options.approval;
42
+ return {
43
+ ...options.description === void 0 ? {} : { description: options.description },
44
+ ...options.title === void 0 ? {} : { title: options.title },
45
+ ...options.annotations === void 0 ? {} : { annotations: options.annotations },
46
+ ...options._meta === void 0 ? {} : { _meta: options._meta },
47
+ input: decoderOf(options.input),
48
+ ...approval === void 0 ? {} : {
49
+ approval: (context) => fromPromise(async () => approval(context), "approval")()
50
+ }
51
+ };
52
+ };
53
+ var approvedOperation = (operation, approval) => {
54
+ const copy = Object.defineProperties({}, Object.getOwnPropertyDescriptors(operation));
55
+ return Object.defineProperty(copy, "approval", {
56
+ enumerable: true,
57
+ value: (context) => fromPromise(async () => approval(context), "approval")()
58
+ });
59
+ };
60
+ var withApproval = (operation, approval) => (
61
+ // SAFETY: `Approved` is a type-only mark; the native operation now carries the approval.
62
+ {
63
+ ...operation,
64
+ [NativeOperation]: approvedOperation(operation[NativeOperation], approval)
65
+ }
66
+ );
67
+ var toolAnnotations = (operation) => nativeOperation(operation)?.annotations;
68
+ var make = (kind, options, run) => operationDeclaration({
69
+ ...operationOptions({ ...options, input: schemaArgument(options.input, `The ${kind} input`) }),
70
+ kind,
71
+ // The host always validates JSON serialization, even without a stronger output declaration.
72
+ ...options.output === void 0 ? {} : { output: decoderOf(schemaArgument(options.output, `The ${kind} output`)) },
73
+ // SAFETY: defineApp checks the handler context against its requirements.
74
+ // The host validates account bindings and creates the matching storage facade.
75
+ run: (context, input) => fromPromise(run, "handler")(context, input)
76
+ });
77
+ var query = (options, run) => make("query", options, run);
78
+ function mutation(options, run) {
79
+ return make("mutation", options, run);
80
+ }
81
+
82
+ // src/implementation/router.ts
83
+ var routerDeclaration = (node) => ({
84
+ [NativeRouterKey]: node
85
+ });
86
+ var nativeRouter = (value) => {
87
+ if (typeof value !== "object" || value === null || !(NativeRouterKey in value)) return void 0;
88
+ return value[NativeRouterKey];
89
+ };
90
+ var nativeNode = (value) => nativeOperation(value) ?? nativeRouter(value);
91
+ var metaOf = (options) => {
92
+ if (options === void 0) return void 0;
93
+ const defined = Object.fromEntries(
94
+ Object.entries(options).filter(([, value]) => value !== void 0)
95
+ );
96
+ return Object.keys(defined).length === 0 ? void 0 : Schema_exports.decodeUnknownSync(RouterMeta)(defined);
97
+ };
98
+ var mergeMeta = (base, override) => base === void 0 ? override : override === void 0 ? base : { ...base, ...override };
99
+ function router(children, options) {
100
+ const meta = metaOf(options);
101
+ const existing = nativeRouter(children);
102
+ if (existing !== void 0)
103
+ return routerDeclaration(
104
+ existing.kind === "router" ? { ...existing, ...withMeta(mergeMeta(existing.meta, meta)) } : { ...existing, ...withOverrides(mergeMeta(existing.overrides, meta)) }
105
+ );
106
+ const nodes = {};
107
+ for (const [key, value] of Object.entries(children)) {
108
+ if (reservedRouterKeys.has(key)) throw new Error(`Router key "${key}" is reserved`);
109
+ if (!Schema_exports.is(RouterKey)(key))
110
+ throw new Error(`Router key "${key}" must start with a letter or _ and contain no dots`);
111
+ const node = nativeNode(value);
112
+ if (node === void 0)
113
+ throw new Error(`Router key "${key}" must be a query, mutation or router`);
114
+ nodes[key] = node;
115
+ }
116
+ return routerDeclaration({ kind: "router", ...withMeta(meta), children: nodes });
117
+ }
118
+ var withMeta = (meta) => meta === void 0 ? {} : { meta };
119
+ var withOverrides = (overrides) => overrides === void 0 ? {} : { overrides };
120
+ var dynamicRouter = (source) => {
121
+ const meta = source.meta;
122
+ return routerDeclaration({
123
+ kind: "dynamic",
124
+ ...meta === void 0 ? {} : {
125
+ meta: () => fromPromise(async () => meta(), "router")().pipe(
126
+ Effect_exports.flatMap((options) => Schema_exports.decodeUnknownEffect(RouterMeta)(options))
127
+ )
128
+ },
129
+ list: () => fromPromise(async () => source.list(), "router")().pipe(
130
+ Effect_exports.flatMap(Schema_exports.decodeUnknownEffect(Schema_exports.Array(HostedTool)))
131
+ ),
132
+ resolve: (name) => fromPromise(async () => source.resolve(name), "router")().pipe(
133
+ Effect_exports.map((value) => {
134
+ if (value === void 0) return void 0;
135
+ const operation = nativeOperation(value);
136
+ if (operation === void 0) throw new Error("Expected an operation declaration");
137
+ return operation;
138
+ })
139
+ )
140
+ });
141
+ };
142
+ var joinPath = (path, name) => path === "" ? name : `${path}.${name}`;
143
+ var locate = (root, name) => {
144
+ if (root?.kind === "dynamic") return { kind: "dynamic", source: root, router: "", name };
145
+ let current = root;
146
+ let path = "";
147
+ let rest = name;
148
+ while (current !== void 0) {
149
+ const dot = rest.indexOf(".");
150
+ const key = dot < 0 ? rest : rest.slice(0, dot);
151
+ const child = Object.hasOwn(current.children, key) ? current.children[key] : void 0;
152
+ if (child === void 0) return void 0;
153
+ const remaining = dot < 0 ? void 0 : rest.slice(dot + 1);
154
+ switch (child.kind) {
155
+ case "query":
156
+ case "mutation":
157
+ return remaining === void 0 ? { kind: "operation", operation: child, router: path } : void 0;
158
+ case "dynamic":
159
+ return remaining === void 0 || remaining === "" ? void 0 : { kind: "dynamic", source: child, router: joinPath(path, key), name: remaining };
160
+ case "router":
161
+ if (remaining === void 0) return void 0;
162
+ current = child;
163
+ path = joinPath(path, key);
164
+ rest = remaining;
165
+ }
166
+ }
167
+ return void 0;
168
+ };
169
+ var declaredOperations = (root, path = "") => Object.entries(root?.children ?? {}).flatMap(([key, child]) => {
170
+ const name = joinPath(path, key);
171
+ switch (child.kind) {
172
+ case "query":
173
+ case "mutation":
174
+ return [{ name, router: path, operation: child }];
175
+ case "router":
176
+ return declaredOperations(child, name);
177
+ case "dynamic":
178
+ return [];
179
+ }
180
+ });
181
+ var withApprovals = (source, policy) => {
182
+ const native = nativeRouter(source);
183
+ if (native === void 0) throw new Error("withApprovals expects a router");
184
+ const operation = (node, name) => {
185
+ const approval = policy(operationDeclaration(node), name);
186
+ return approval === void 0 ? node : approvedOperation(node, approval);
187
+ };
188
+ const child = (node, name) => {
189
+ switch (node.kind) {
190
+ case "query":
191
+ case "mutation":
192
+ return operation(node, name);
193
+ case "router":
194
+ case "dynamic":
195
+ return walk(node, name);
196
+ }
197
+ };
198
+ const walk = (node, path) => node.kind === "dynamic" ? {
199
+ ...node,
200
+ resolve: (name) => node.resolve(name).pipe(
201
+ Effect_exports.flatMap(
202
+ (resolved) => resolved === void 0 ? Effect_exports.succeed(void 0) : Effect_exports.try({
203
+ try: () => operation(resolved, joinPath(path, name)),
204
+ catch: (error) => error
205
+ })
206
+ )
207
+ )
208
+ } : {
209
+ ...node,
210
+ children: Object.fromEntries(
211
+ Object.entries(node.children).map(([key, value]) => [
212
+ key,
213
+ child(value, joinPath(path, key))
214
+ ])
215
+ )
216
+ };
217
+ return routerDeclaration(walk(native, ""));
218
+ };
219
+ var renameTools = (source, rename) => {
220
+ const native = nativeRouter(source);
221
+ if (native?.kind !== "dynamic") throw new Error("renameTools expects a protocol router");
222
+ const exposed = (tools) => {
223
+ const seen = /* @__PURE__ */ new Set();
224
+ return tools.flatMap((tool) => {
225
+ const name = rename(tool.name);
226
+ if (name === void 0 || seen.has(name)) return [];
227
+ seen.add(name);
228
+ return [{ ...tool, name, source: tool.name }];
229
+ });
230
+ };
231
+ const listed = native.summaries ?? native.list;
232
+ const original = (name) => listed().pipe(Effect_exports.map((tools) => exposed(tools).find((tool) => tool.name === name)?.source));
233
+ const strip = ({
234
+ source: _source,
235
+ ...tool
236
+ }) => tool;
237
+ return routerDeclaration({
238
+ ...native,
239
+ list: () => native.list().pipe(Effect_exports.map((tools) => exposed(tools).map(strip))),
240
+ ...native.summaries === void 0 ? {} : {
241
+ summaries: () => listed().pipe(Effect_exports.map((tools) => exposed(tools).map(strip)))
242
+ },
243
+ describe: (name) => original(name).pipe(
244
+ Effect_exports.flatMap(
245
+ (found) => found === void 0 ? Effect_exports.succeed(void 0) : native.describe?.(found) ?? native.list().pipe(Effect_exports.map((tools) => tools.find((tool) => tool.name === found)))
246
+ ),
247
+ Effect_exports.map((tool) => tool === void 0 ? void 0 : { ...tool, name })
248
+ ),
249
+ resolve: (name) => original(name).pipe(
250
+ Effect_exports.flatMap(
251
+ (found) => found === void 0 ? Effect_exports.succeed(void 0) : native.resolve(found)
252
+ )
253
+ )
254
+ });
255
+ };
256
+
257
+ // src/implementation/account-router.ts
258
+ var PartialListing = class extends Schema_exports.TaggedError()("PartialListing", {
259
+ tools: Schema_exports.Array(Schema_exports.Unknown),
260
+ error: Schema_exports.Unknown
261
+ }) {
262
+ };
263
+ var decoderDocument = (decoder) => {
264
+ const imported = importedJsonSchema(decoder);
265
+ if (imported !== void 0) return Schema_exports.decodeUnknownSync(JsonObject)(imported);
266
+ const document = Schema_exports.toJsonSchemaDocument(decoder);
267
+ return Schema_exports.decodeUnknownSync(JsonObject)({ ...document.schema, $defs: document.definitions });
268
+ };
269
+ var accountBranches = (variants) => {
270
+ const groups = /* @__PURE__ */ new Map();
271
+ for (const [accountId, schema] of variants) {
272
+ const key = JSON.stringify(schema);
273
+ const group = groups.get(key);
274
+ if (group === void 0) groups.set(key, { schema, first: accountId, rest: [] });
275
+ else group.rest.push(accountId);
276
+ }
277
+ const branches = [...groups.values()];
278
+ const { $defs, schemas } = combineJsonSchemas(
279
+ branches.map(({ schema }) => schema),
280
+ (index) => `#/anyOf/${index}/properties/input`
281
+ );
282
+ return {
283
+ type: "object",
284
+ ...$defs === void 0 ? {} : { $defs },
285
+ anyOf: schemas.map((input, index) => {
286
+ const { first, rest } = branches[index];
287
+ return {
288
+ type: "object",
289
+ properties: {
290
+ accountId: rest.length === 0 ? { type: "string", const: first } : { type: "string", enum: [first, ...rest] },
291
+ input
292
+ },
293
+ required: ["accountId", "input"]
294
+ };
295
+ })
296
+ };
297
+ };
298
+ var outputBranches = (outputs) => {
299
+ const { $defs, schemas } = combineJsonSchemas(
300
+ [...new Map(outputs.map((output) => [JSON.stringify(output), output])).values()],
301
+ (index) => `#/anyOf/${index}`
302
+ );
303
+ return { ...$defs === void 0 ? {} : { $defs }, anyOf: schemas };
304
+ };
305
+ var declaresOutput = (operation) => {
306
+ if (operation.output !== void 0) return true;
307
+ const property = Object.getOwnPropertyDescriptor(operation, "outputSchema");
308
+ return property !== void 0 && (property.get !== void 0 || property.value !== void 0);
309
+ };
310
+ var outputDocument = (operation) => operation.output === void 0 ? operation.outputSchema : decoderDocument(operation.output);
311
+ var combineOperation = (accounts, failed = /* @__PURE__ */ new Map()) => {
312
+ const variants = [...accounts.values()];
313
+ const first = variants[0];
314
+ if (first === void 0) throw new Error("An account operation must have at least one account");
315
+ const kind = variants.every((operation) => operation.kind === "query") ? "query" : "mutation";
316
+ const select = (input2) => {
317
+ const operation = accounts.get(input2.accountId);
318
+ if (operation === void 0)
319
+ throw new Error("Account selection must be decoded before execution");
320
+ return operation;
321
+ };
322
+ const input = Schema_exports.Union([
323
+ ...[...accounts].map(
324
+ ([accountId, operation]) => Schema_exports.Struct({ accountId: Schema_exports.Literal(accountId), input: operation.input })
325
+ ),
326
+ ...[...failed.keys()].map(
327
+ (accountId) => Schema_exports.Struct({ accountId: Schema_exports.Literal(accountId), input: Schema_exports.Unknown })
328
+ )
329
+ ]);
330
+ const inputSchema = () => accountBranches(
331
+ [...accounts].map(([accountId, operation]) => [accountId, decoderDocument(operation.input)])
332
+ );
333
+ const declaration = operationDeclaration({
334
+ kind,
335
+ ...first.description === void 0 ? {} : { description: first.description },
336
+ ...first.title === void 0 ? {} : { title: first.title },
337
+ ...first.annotations === void 0 || !variants.every(
338
+ (operation) => JSON.stringify(operation.annotations) === JSON.stringify(first.annotations)
339
+ ) ? {} : { annotations: first.annotations },
340
+ ...first._meta === void 0 || !variants.every((operation) => JSON.stringify(operation._meta) === JSON.stringify(first._meta)) ? {} : { _meta: first._meta },
341
+ input: withLazyJsonSchemaDocument(input, inputSchema),
342
+ approval: (context) => {
343
+ if (failed.has(context.toolInput.accountId)) return Effect_exports.succeed("approved");
344
+ const operation = select(context.toolInput);
345
+ return operation.approval === void 0 ? Effect_exports.succeed("approved") : operation.approval({ ...context, toolInput: context.toolInput.input });
346
+ },
347
+ run: (context, input2) => {
348
+ if (failed.has(input2.accountId))
349
+ return Effect_exports.fail(accountProviderError(failed.get(input2.accountId), input2.accountId));
350
+ const operation = select(input2);
351
+ return operation.run(context, input2.input).pipe(
352
+ Effect_exports.mapError((error) => accountProviderError(error, input2.accountId)),
353
+ Effect_exports.flatMap(
354
+ (output) => operation.output === void 0 ? Effect_exports.succeed(output) : Schema_exports.decodeUnknownEffect(operation.output)(output)
355
+ )
356
+ );
357
+ }
358
+ });
359
+ if (variants.every(declaresOutput)) {
360
+ const outputSchema = once(
361
+ () => outputBranches(
362
+ variants.flatMap((operation) => {
363
+ const output = outputDocument(operation);
364
+ return output === void 0 ? [] : [output];
365
+ })
366
+ )
367
+ );
368
+ const native2 = nativeOperation(declaration);
369
+ if (native2 !== void 0)
370
+ Object.defineProperty(native2, "outputSchema", { enumerable: true, get: outputSchema });
371
+ }
372
+ const native = nativeOperation(declaration);
373
+ if (native === void 0) throw new Error("Expected a combined operation");
374
+ return native;
375
+ };
376
+ var sameAcross = (key, variants) => {
377
+ const first = variants[0]?.[1][key];
378
+ return first !== void 0 && variants.every(([, tool]) => JSON.stringify(tool[key]) === JSON.stringify(first)) ? { [key]: first } : {};
379
+ };
380
+ var mergeSummary = (name, variants) => {
381
+ const first = variants[0]?.[1];
382
+ if (first === void 0) throw new Error("Expected a source operation");
383
+ return Schema_exports.decodeUnknownSync(HostedToolSummary)({
384
+ name,
385
+ description: first.description,
386
+ ...first.title === void 0 ? {} : { title: first.title },
387
+ readOnly: variants.every(([, tool]) => tool.readOnly === true),
388
+ ...variants.every(([, tool]) => tool.kindConfirmed === true) ? { kindConfirmed: true } : {},
389
+ ...sameAcross("annotations", variants)
390
+ });
391
+ };
392
+ var merge = (name, variants) => Schema_exports.decodeUnknownSync(HostedTool)({
393
+ ...mergeSummary(name, variants),
394
+ inputSchema: accountBranches(
395
+ variants.map(([accountId, tool]) => [accountId, tool.inputSchema])
396
+ ),
397
+ ...variants.every(([, tool]) => tool.outputSchema !== void 0) ? {
398
+ outputSchema: outputBranches(
399
+ variants.flatMap(
400
+ ([, tool]) => tool.outputSchema === void 0 ? [] : [tool.outputSchema]
401
+ )
402
+ )
403
+ } : {},
404
+ ...sameAcross("_meta", variants)
405
+ });
406
+ var accountConcurrency = 8;
407
+ var eachAccount = (sources, read) => Effect_exports.gen(function* () {
408
+ const results = yield* Effect_exports.forEach(
409
+ sources,
410
+ ([accountId, source]) => Effect_exports.result(
411
+ read(source).pipe(Effect_exports.mapError((error) => accountProviderError(error, accountId)))
412
+ ).pipe(Effect_exports.map((result) => [accountId, result])),
413
+ { concurrency: accountConcurrency }
414
+ );
415
+ const values = /* @__PURE__ */ new Map();
416
+ const failed = /* @__PURE__ */ new Map();
417
+ for (const [accountId, result] of results)
418
+ if (Result_exports.isSuccess(result)) values.set(accountId, result.success);
419
+ else failed.set(accountId, result.failure);
420
+ return { values, failed };
421
+ });
422
+ var firstFailure = (failed) => [...failed.values()][0];
423
+ var combineDynamic = (sources) => {
424
+ const first = [...sources.values()][0];
425
+ const grouped = (read, merged) => Effect_exports.gen(function* () {
426
+ const { values: listed, failed } = yield* eachAccount(sources, read);
427
+ if (listed.size === 0 && failed.size > 0) return yield* Effect_exports.fail(firstFailure(failed));
428
+ const groups = /* @__PURE__ */ new Map();
429
+ for (const [accountId, tools2] of listed)
430
+ for (const tool of tools2) {
431
+ const variants = groups.get(tool.name) ?? [];
432
+ if (variants.some(([id]) => id === accountId))
433
+ return yield* Effect_exports.die(new Error("Duplicate source operation"));
434
+ variants.push([accountId, tool]);
435
+ groups.set(tool.name, variants);
436
+ }
437
+ const tools = [...groups].map(([name, variants]) => merged(name, variants));
438
+ if (failed.size === 0) return tools;
439
+ return yield* new PartialListing({ tools, error: firstFailure(failed) });
440
+ });
441
+ return {
442
+ kind: "dynamic",
443
+ // Accounts share one source, so the first account that can describe it describes the router.
444
+ ...first?.meta === void 0 ? {} : {
445
+ meta: () => Effect_exports.gen(function* () {
446
+ const failures = [];
447
+ for (const source of sources.values()) {
448
+ if (source.meta === void 0) continue;
449
+ const result = yield* Effect_exports.result(source.meta());
450
+ if (Result_exports.isSuccess(result)) return result.success;
451
+ failures.push(result.failure);
452
+ }
453
+ return yield* Effect_exports.fail(failures[0]);
454
+ })
455
+ },
456
+ ...first?.overrides === void 0 ? {} : { overrides: first.overrides },
457
+ list: () => grouped((source) => source.list(), merge),
458
+ summaries: () => grouped(
459
+ (source) => source.summaries === void 0 ? source.list() : source.summaries(),
460
+ mergeSummary
461
+ ),
462
+ describe: (name) => eachAccount(
463
+ sources,
464
+ (source) => source.describe === void 0 ? source.list().pipe(Effect_exports.map((tools) => tools.find((tool) => tool.name === name))) : source.describe(name)
465
+ ).pipe(
466
+ Effect_exports.flatMap(({ values, failed }) => {
467
+ const variants = [...values].flatMap(
468
+ ([accountId, tool]) => tool === void 0 ? [] : [[accountId, tool]]
469
+ );
470
+ if (variants.length === 0)
471
+ return failed.size > 0 ? Effect_exports.fail(firstFailure(failed)) : Effect_exports.succeed(void 0);
472
+ return Effect_exports.succeed(merge(name, variants));
473
+ })
474
+ ),
475
+ resolve: (name) => eachAccount(sources, (source) => source.resolve(name)).pipe(
476
+ Effect_exports.flatMap(({ values, failed }) => {
477
+ const operations = new Map(
478
+ [...values].flatMap(
479
+ ([accountId, operation]) => operation === void 0 ? [] : [[accountId, operation]]
480
+ )
481
+ );
482
+ if (operations.size === 0)
483
+ return failed.size > 0 ? Effect_exports.fail(firstFailure(failed)) : Effect_exports.succeed(void 0);
484
+ return Effect_exports.succeed(combineOperation(operations, failed));
485
+ })
486
+ )
487
+ };
488
+ };
489
+ var combineNode = (variants) => {
490
+ const nodes = [...variants.values()];
491
+ const first = nodes[0];
492
+ if (first === void 0) throw new Error("An account operation must have at least one account");
493
+ switch (first.kind) {
494
+ case "query":
495
+ case "mutation":
496
+ return combineOperation(
497
+ narrow(
498
+ variants,
499
+ (node) => node.kind === "query" || node.kind === "mutation"
500
+ )
501
+ );
502
+ case "dynamic":
503
+ return combineDynamic(
504
+ narrow(variants, (node) => node.kind === "dynamic")
505
+ );
506
+ case "router": {
507
+ const routers = narrow(variants, (node) => node.kind === "router");
508
+ const keys = new Set([...routers.values()].flatMap((router2) => Object.keys(router2.children)));
509
+ return {
510
+ kind: "router",
511
+ ...first.meta === void 0 ? {} : { meta: first.meta },
512
+ children: Object.fromEntries(
513
+ [...keys].map((key) => {
514
+ const children = /* @__PURE__ */ new Map();
515
+ for (const [accountId, router2] of routers) {
516
+ const child = Object.hasOwn(router2.children, key) ? router2.children[key] : void 0;
517
+ if (child !== void 0) children.set(accountId, child);
518
+ }
519
+ return [key, combineNode(children)];
520
+ })
521
+ )
522
+ };
523
+ }
524
+ }
525
+ };
526
+ var narrow = (variants, is) => {
527
+ const narrowed = /* @__PURE__ */ new Map();
528
+ for (const [accountId, node] of variants) {
529
+ if (!is(node)) throw new Error("Each account's router must have the same shape");
530
+ narrowed.set(accountId, node);
531
+ }
532
+ return narrowed;
533
+ };
534
+ var accountRouter = (accounts, discover, options) => Effect_exports.runPromise(
535
+ Effect_exports.gen(function* () {
536
+ const routers = /* @__PURE__ */ new Map();
537
+ for (const account of accounts) {
538
+ const declaration = yield* Effect_exports.tryPromise({
539
+ try: async () => discover(account),
540
+ catch: (error) => accountProviderError(error, account.id)
541
+ });
542
+ if (routers.has(account.id))
543
+ return yield* Effect_exports.die(new Error("Account selections must be unique"));
544
+ const router2 = nativeRouter(declaration);
545
+ if (router2 === void 0) return yield* Effect_exports.die(new Error("Expected a protocol router"));
546
+ routers.set(account.id, router2);
547
+ }
548
+ if (routers.size === 0) return routerDeclaration({ kind: "router", children: {} });
549
+ const combined = combineNode(routers);
550
+ if (combined.kind !== "router" && combined.kind !== "dynamic")
551
+ return yield* Effect_exports.die(new Error("Expected a protocol router"));
552
+ return routerDeclaration(combined);
553
+ }),
554
+ options
555
+ );
556
+
557
+ export {
558
+ operationDeclaration,
559
+ nativeOperation,
560
+ withApproval,
561
+ toolAnnotations,
562
+ query,
563
+ mutation,
564
+ routerDeclaration,
565
+ nativeRouter,
566
+ mergeMeta,
567
+ router,
568
+ dynamicRouter,
569
+ joinPath,
570
+ locate,
571
+ declaredOperations,
572
+ withApprovals,
573
+ renameTools,
574
+ PartialListing,
575
+ accountRouter
576
+ };