apps 0.0.1-beta.7 → 0.0.1-beta.73

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 (178) hide show
  1. package/README.md +25 -22
  2. package/framework-reference.json +1 -1
  3. package/js/{chunk-RJDDBDSN.js → chunk-3CVQ3VPP.js} +762 -171
  4. package/js/chunk-3R3KFCDU.js +426 -0
  5. package/js/chunk-5FHWNLCI.js +88 -0
  6. package/js/{chunk-M3B3DE4K.js → chunk-74YVXBKY.js} +463 -218
  7. package/js/chunk-7LMU3ER3.js +274 -0
  8. package/js/chunk-CDXQDN7W.js +92 -0
  9. package/js/{chunk-RFVIONFX.js → chunk-ECCWT5GJ.js} +79 -41
  10. package/js/{chunk-UK573G7W.js → chunk-EOPQ727L.js} +87 -51
  11. package/js/{chunk-CMYUWAX7.js → chunk-G7L5DHQX.js} +126 -31
  12. package/js/{chunk-27F7W2LG.js → chunk-H2CJWXA3.js} +2 -2
  13. package/js/{chunk-JU7VHODU.js → chunk-H72MEDMN.js} +65 -10
  14. package/js/{chunk-CDL5PM2Q.js → chunk-IFJU5SYQ.js} +19082 -18402
  15. package/js/{chunk-VFZKSM3T.js → chunk-JPEUKEV4.js} +3 -3
  16. package/js/{chunk-WLE7WX2U.js → chunk-JW5LYSER.js} +2262 -474
  17. package/js/{chunk-5T57N25Q.js → chunk-KGCTYL2S.js} +511 -63
  18. package/js/{chunk-KUMBKVJD.js → chunk-KLYSEHUD.js} +2 -2
  19. package/js/chunk-LTF4TLMO.js +347 -0
  20. package/js/chunk-MHB7IFG7.js +49 -0
  21. package/js/{chunk-FL2QR3XW.js → chunk-OBYMFSWA.js} +333 -69
  22. package/js/chunk-OD3KFKZD.js +1617 -0
  23. package/js/{chunk-57RKNWL2.js → chunk-PD6I2SDG.js} +4 -4
  24. package/js/{chunk-72JNVHPK.js → chunk-QGHGYH5V.js} +4 -3
  25. package/js/{chunk-NKLWZJLE.js → chunk-RUP4JMKO.js} +25 -14
  26. package/js/{chunk-IJX4CYZO.js → chunk-TUMGL2OC.js} +228 -264
  27. package/js/{chunk-UESPOTUI.js → chunk-WCVV4PPS.js} +899 -190
  28. package/js/{chunk-IPI4MFSN.js → chunk-XBG7HHXC.js} +2 -2
  29. package/js/{chunk-Z5M2LQOZ.js → chunk-ZEJ3IKEC.js} +67 -55
  30. package/js/chunk-ZR7QAG67.js +171 -0
  31. package/js/client.js +142 -94
  32. package/js/contracts.js +125 -26
  33. package/js/effect.js +19 -19
  34. package/js/{es-APDZKREY.js → es-DIULA3ZF.js} +62 -1659
  35. package/js/graphql.js +50 -40
  36. package/js/host.js +83 -58
  37. package/js/index.js +131 -46
  38. package/js/mcp/effect.js +23 -21
  39. package/js/mcp/stdio.js +44 -34
  40. package/js/mcp.js +76 -53
  41. package/js/openapi.js +4071 -1357
  42. package/js/react.js +2 -3
  43. package/js/skills/effect.js +6 -3
  44. package/js/skills.js +208 -81
  45. package/js/storage/facet.js +1 -540
  46. package/js/ui/auth/contracts.js +2 -2
  47. package/js/ui/auth.js +5 -5
  48. package/js/ui/contracts.js +5 -4
  49. package/js/ui/serving.js +8517 -214
  50. package/package.json +3 -4
  51. package/runtime.json +1 -1
  52. package/types/app-cache/src/contracts/cache.d.ts +2 -0
  53. package/types/app-cache/src/index.d.ts +10 -1
  54. package/types/apps/src/contracts/api-response-error.d.ts +4 -0
  55. package/types/apps/src/contracts/app.d.ts +7 -3
  56. package/types/apps/src/contracts/cache.d.ts +20 -0
  57. package/types/apps/src/contracts/context.d.ts +19 -11
  58. package/types/apps/src/contracts/elicitation.d.ts +6 -6
  59. package/types/apps/src/contracts/events.d.ts +93 -0
  60. package/types/apps/src/contracts/failure.d.ts +30 -1
  61. package/types/apps/src/contracts/graphql.d.ts +2 -1
  62. package/types/apps/src/contracts/host.d.ts +641 -16
  63. package/types/apps/src/contracts/live.d.ts +2 -0
  64. package/types/apps/src/contracts/mcp.d.ts +80 -10
  65. package/types/apps/src/contracts/network.d.ts +120 -0
  66. package/types/apps/src/contracts/openapi-compile.d.ts +82 -3
  67. package/types/apps/src/contracts/openapi.d.ts +41 -9
  68. package/types/apps/src/contracts/operations.d.ts +8 -4
  69. package/types/apps/src/contracts/placement.d.ts +102 -0
  70. package/types/apps/src/contracts/protocol-version.d.ts +2 -2
  71. package/types/apps/src/contracts/protocols/1.d.ts +733 -81
  72. package/types/apps/src/contracts/protocols/10.d.ts +1450 -0
  73. package/types/apps/src/contracts/protocols/2.d.ts +69 -26
  74. package/types/apps/src/contracts/protocols/3.d.ts +133 -33
  75. package/types/apps/src/contracts/protocols/4.d.ts +122 -45
  76. package/types/apps/src/contracts/protocols/5.d.ts +157 -47
  77. package/types/apps/src/contracts/protocols/6.d.ts +1629 -0
  78. package/types/apps/src/contracts/protocols/7.d.ts +1785 -0
  79. package/types/apps/src/contracts/protocols/8.d.ts +1182 -0
  80. package/types/apps/src/contracts/protocols/9.d.ts +1104 -0
  81. package/types/apps/src/contracts/protocols/current.d.ts +3842 -0
  82. package/types/apps/src/contracts/protocols/oauth-6.d.ts +74 -0
  83. package/types/apps/src/contracts/protocols/oauth.d.ts +134 -0
  84. package/types/apps/src/contracts/provider-error.d.ts +9 -0
  85. package/types/apps/src/contracts/provider.d.ts +90 -6
  86. package/types/apps/src/contracts/skills.d.ts +50 -13
  87. package/types/apps/src/contracts/sql.d.ts +63 -0
  88. package/types/apps/src/contracts/ui.d.ts +19 -1
  89. package/types/apps/src/contracts/workflows.d.ts +6 -0
  90. package/types/apps/src/facet.d.ts +4 -5
  91. package/types/apps/src/host.d.ts +5 -0
  92. package/types/apps/src/implementation/account-router.d.ts +4 -3
  93. package/types/apps/src/implementation/authoring.d.ts +25 -5
  94. package/types/apps/src/implementation/browser-scripts.gen.d.ts +6 -0
  95. package/types/apps/src/implementation/cache.d.ts +4 -0
  96. package/types/apps/src/implementation/catalog-cache.d.ts +35 -4
  97. package/types/apps/src/implementation/elicitation.d.ts +18 -15
  98. package/types/apps/src/implementation/events.d.ts +59 -0
  99. package/types/apps/src/implementation/failure-detail.d.ts +25 -3
  100. package/types/apps/src/implementation/graphql-catalog.d.ts +3 -3
  101. package/types/apps/src/implementation/graphql.d.ts +4 -3
  102. package/types/apps/src/implementation/input-problems.d.ts +6 -2
  103. package/types/apps/src/implementation/invocation-timing.d.ts +16 -0
  104. package/types/apps/src/implementation/live.d.ts +2 -2
  105. package/types/apps/src/implementation/mcp-call.d.ts +6 -5
  106. package/types/apps/src/implementation/mcp-catalog.d.ts +6 -4
  107. package/types/apps/src/implementation/mcp-client.d.ts +17 -9
  108. package/types/apps/src/implementation/mcp-stdio.d.ts +1 -1
  109. package/types/apps/src/implementation/mcp-tools.d.ts +2 -1
  110. package/types/apps/src/implementation/mcp.d.ts +20 -9
  111. package/types/apps/src/implementation/network.d.ts +16 -0
  112. package/types/apps/src/implementation/openapi-compile.d.ts +26 -6
  113. package/types/apps/src/implementation/openapi-document.d.ts +46 -19
  114. package/types/apps/src/implementation/openapi-names.d.ts +9 -4
  115. package/types/apps/src/implementation/openapi-request.d.ts +10 -2
  116. package/types/apps/src/implementation/openapi-source.d.ts +18 -0
  117. package/types/apps/src/implementation/openapi-upgrade.d.ts +4 -0
  118. package/types/apps/src/implementation/operations.d.ts +17 -2
  119. package/types/apps/src/implementation/optimistic.d.ts +1 -1
  120. package/types/apps/src/implementation/placement.d.ts +60 -0
  121. package/types/apps/src/implementation/protocol-operations.d.ts +3 -0
  122. package/types/apps/src/implementation/provider-error.d.ts +28 -1
  123. package/types/apps/src/implementation/provider.d.ts +18 -3
  124. package/types/apps/src/implementation/query-commit.d.ts +1 -1
  125. package/types/apps/src/implementation/router-catalog.d.ts +11 -13
  126. package/types/apps/src/implementation/router.d.ts +16 -5
  127. package/types/apps/src/implementation/runtime-yield.d.ts +7 -0
  128. package/types/apps/src/implementation/schema.d.ts +90 -10
  129. package/types/apps/src/implementation/skills.d.ts +18 -24
  130. package/types/apps/src/implementation/sql.d.ts +71 -0
  131. package/types/apps/src/implementation/ui-account-notice.d.ts +9 -0
  132. package/types/apps/src/implementation/ui-auth.d.ts +1 -1
  133. package/types/apps/src/implementation/ui-client.d.ts +2 -2
  134. package/types/apps/src/implementation/ui-document.d.ts +5 -0
  135. package/types/apps/src/implementation/ui-errors.d.ts +1 -1
  136. package/types/apps/src/implementation/ui-serving.d.ts +11 -5
  137. package/types/apps/src/implementation/ui-watch.d.ts +1 -1
  138. package/types/apps/src/implementation/upstream-error.d.ts +7 -0
  139. package/types/apps/src/implementation/webhooks.d.ts +1 -1
  140. package/types/apps/src/implementation/workflows.d.ts +1 -1
  141. package/types/apps/src/index.d.ts +55 -13
  142. package/types/apps/src/mcp.d.ts +20 -1
  143. package/types/apps/src/openapi.d.ts +1 -1
  144. package/types/apps/src/react.d.ts +1 -1
  145. package/types/apps/src/skills.d.ts +10 -8
  146. package/types/telemetry/src/app-records.d.ts +42 -0
  147. package/types/telemetry/src/browser-operations.d.ts +7 -4
  148. package/types/telemetry/src/browser.d.ts +8 -3
  149. package/types/telemetry/src/config.d.ts +2 -2
  150. package/types/telemetry/src/index.d.ts +2 -1
  151. package/types/telemetry/src/layer.d.ts +2 -2
  152. package/types/telemetry/src/measurements.d.ts +1 -1
  153. package/types/telemetry/src/ownership.d.ts +56 -0
  154. package/types/telemetry/src/recorded-failure.d.ts +29 -0
  155. package/types/telemetry/src/relay.d.ts +16 -6
  156. package/types/telemetry/src/span-attributes.d.ts +17 -1
  157. package/types/telemetry/src/transport.d.ts +8 -1
  158. package/types/utils/src/api-error.d.ts +55 -0
  159. package/types/utils/src/connection-failure.d.ts +33 -0
  160. package/types/utils/src/recorded-message.d.ts +18 -0
  161. package/types/utils/src/user-facing-error.d.ts +83 -0
  162. package/js/chunk-MKWZPDR6.js +0 -63
  163. package/js/chunk-N7YDVXYJ.js +0 -186
  164. package/js/chunk-OCYUEU47.js +0 -224
  165. package/js/chunk-PJJ24GU7.js +0 -877
  166. package/js/chunk-PZB6T6KU.js +0 -19
  167. package/js/chunk-R3EV5FTO.js +0 -97
  168. package/js/chunk-YXQHV65J.js +0 -242
  169. package/js/chunk-ZQINWV4W.js +0 -630
  170. package/types/app-data/src/implementation/cursor.d.ts +0 -10
  171. package/types/app-data/src/implementation/index-key.d.ts +0 -7
  172. package/types/app-data/src/implementation/promise.d.ts +0 -68
  173. package/types/app-data/src/implementation/range.d.ts +0 -8
  174. package/types/app-data/src/implementation/schema.d.ts +0 -18
  175. package/types/app-data/src/implementation/sqlite.d.ts +0 -11
  176. package/types/app-data/src/index.d.ts +0 -4
  177. package/types/apps/src/contracts/storage.d.ts +0 -68
  178. package/types/apps/src/implementation/storage.d.ts +0 -15
@@ -5,11 +5,11 @@ import {
5
5
  SkillDefinitionInvalid,
6
6
  SkillFile,
7
7
  SkillFilePath
8
- } from "./chunk-JU7VHODU.js";
8
+ } from "./chunk-H72MEDMN.js";
9
9
  import {
10
10
  Effect_exports,
11
11
  Schema_exports
12
- } from "./chunk-CDL5PM2Q.js";
12
+ } from "./chunk-IFJU5SYQ.js";
13
13
 
14
14
  // src/implementation/skill-files.ts
15
15
  var skillFromFiles = (input, source = {}) => Effect_exports.gen(function* () {
@@ -1,48 +1,20 @@
1
+ import {
2
+ fromPromise
3
+ } from "./chunk-ZR7QAG67.js";
1
4
  import {
2
5
  HostedTool,
3
6
  RouterKey,
4
7
  RouterMeta,
5
8
  reservedRouterKeys
6
- } from "./chunk-WLE7WX2U.js";
9
+ } from "./chunk-JW5LYSER.js";
7
10
  import {
8
- decoderOf
9
- } from "./chunk-5T57N25Q.js";
11
+ decoderOf,
12
+ schemaArgument
13
+ } from "./chunk-KGCTYL2S.js";
10
14
  import {
11
15
  Effect_exports,
12
16
  Schema_exports
13
- } from "./chunk-CDL5PM2Q.js";
14
-
15
- // src/implementation/authoring.ts
16
- var NativeCallback = /* @__PURE__ */ Symbol("apps.NativeCallback");
17
- var fromPromise = (callback) => (...args) => {
18
- if (NativeCallback in callback) {
19
- const native = callback[NativeCallback];
20
- return native(...args);
21
- }
22
- return Effect_exports.tryPromise({ try: () => callback(...args), catch: (error) => error });
23
- };
24
- var toPromise = (operation, signal) => {
25
- const native = (...args) => Effect_exports.suspend(() => {
26
- if (signal === void 0) return operation(...args);
27
- if (signal.aborted) return Effect_exports.interrupt;
28
- const cancelled = Effect_exports.callback((resume) => {
29
- if (signal.aborted) {
30
- resume(Effect_exports.interrupt);
31
- return;
32
- }
33
- const abort = () => resume(Effect_exports.interrupt);
34
- signal.addEventListener("abort", abort, { once: true });
35
- return Effect_exports.sync(() => signal.removeEventListener("abort", abort));
36
- });
37
- return Effect_exports.raceFirst(
38
- cancelled,
39
- Effect_exports.suspend(() => operation(...args))
40
- );
41
- });
42
- return Object.assign((...args) => Effect_exports.runPromise(native(...args)), {
43
- [NativeCallback]: native
44
- });
45
- };
17
+ } from "./chunk-IFJU5SYQ.js";
46
18
 
47
19
  // src/implementation/operations.ts
48
20
  var NativeOperation = /* @__PURE__ */ Symbol("apps.Operation");
@@ -64,28 +36,30 @@ var operationOptions = (options) => {
64
36
  ...options._meta === void 0 ? {} : { _meta: options._meta },
65
37
  input: decoderOf(options.input),
66
38
  ...approval === void 0 ? {} : {
67
- approval: (context) => Effect_exports.tryPromise({ try: async () => approval(context), catch: (error) => error })
39
+ approval: (context) => fromPromise(async () => approval(context), "approval")()
68
40
  }
69
41
  };
70
42
  };
43
+ var approvedOperation = (operation, approval) => {
44
+ const copy = Object.defineProperties({}, Object.getOwnPropertyDescriptors(operation));
45
+ return Object.defineProperty(copy, "approval", {
46
+ enumerable: true,
47
+ value: (context) => fromPromise(async () => approval(context), "approval")()
48
+ });
49
+ };
71
50
  var withApproval = (operation, approval) => ({
72
51
  ...operation,
73
- [NativeOperation]: {
74
- ...operation[NativeOperation],
75
- approval: (context) => Effect_exports.tryPromise({ try: async () => approval(context), catch: (error) => error })
76
- }
52
+ [NativeOperation]: approvedOperation(operation[NativeOperation], approval)
77
53
  });
54
+ var toolAnnotations = (operation) => nativeOperation(operation)?.annotations;
78
55
  var make = (kind, options, run) => operationDeclaration({
79
- ...operationOptions(options),
56
+ ...operationOptions({ ...options, input: schemaArgument(options.input, `The ${kind} input`) }),
80
57
  kind,
81
58
  // The host always validates JSON serialization, even without a stronger output declaration.
82
- ...options.output === void 0 ? {} : { output: decoderOf(options.output) },
83
- run: (context, input) => Effect_exports.tryPromise({
84
- // SAFETY: defineApp checks the handler context against its requirements.
85
- // The host validates account bindings and creates the matching storage facade.
86
- try: () => run(context, input),
87
- catch: (error) => error
88
- })
59
+ ...options.output === void 0 ? {} : { output: decoderOf(schemaArgument(options.output, `The ${kind} output`)) },
60
+ // SAFETY: defineApp checks the handler context against its requirements.
61
+ // The host validates account bindings and creates the matching storage facade.
62
+ run: (context, input) => fromPromise(run, "handler")(context, input)
89
63
  });
90
64
  var query = (options, run) => make("query", options, run);
91
65
  var mutation = (options, run) => make("mutation", options, run);
@@ -133,14 +107,14 @@ var dynamicRouter = (source) => {
133
107
  return routerDeclaration({
134
108
  kind: "dynamic",
135
109
  ...meta === void 0 ? {} : {
136
- meta: () => fromPromise(async () => meta())().pipe(
110
+ meta: () => fromPromise(async () => meta(), "router")().pipe(
137
111
  Effect_exports.flatMap((options) => Schema_exports.decodeUnknownEffect(RouterMeta)(options))
138
112
  )
139
113
  },
140
- list: () => fromPromise(async () => source.list())().pipe(
114
+ list: () => fromPromise(async () => source.list(), "router")().pipe(
141
115
  Effect_exports.flatMap(Schema_exports.decodeUnknownEffect(Schema_exports.Array(HostedTool)))
142
116
  ),
143
- resolve: (name) => fromPromise(async () => source.resolve(name))().pipe(
117
+ resolve: (name) => fromPromise(async () => source.resolve(name), "router")().pipe(
144
118
  Effect_exports.map((value) => {
145
119
  if (value === void 0) return void 0;
146
120
  const operation = nativeOperation(value);
@@ -189,13 +163,50 @@ var declaredOperations = (root, path = "") => Object.entries(root?.children ?? {
189
163
  return [];
190
164
  }
191
165
  });
166
+ var withApprovals = (source, policy) => {
167
+ const native = nativeRouter(source);
168
+ if (native === void 0) throw new Error("withApprovals expects a router");
169
+ const operation = (node, name) => {
170
+ const approval = policy(operationDeclaration(node), name);
171
+ return approval === void 0 ? node : approvedOperation(node, approval);
172
+ };
173
+ const child = (node, name) => {
174
+ switch (node.kind) {
175
+ case "query":
176
+ case "mutation":
177
+ return operation(node, name);
178
+ case "router":
179
+ case "dynamic":
180
+ return walk(node, name);
181
+ }
182
+ };
183
+ const walk = (node, path) => node.kind === "dynamic" ? {
184
+ ...node,
185
+ resolve: (name) => node.resolve(name).pipe(
186
+ Effect_exports.flatMap(
187
+ (resolved) => resolved === void 0 ? Effect_exports.succeed(void 0) : Effect_exports.try({
188
+ try: () => operation(resolved, joinPath(path, name)),
189
+ catch: (error) => error
190
+ })
191
+ )
192
+ )
193
+ } : {
194
+ ...node,
195
+ children: Object.fromEntries(
196
+ Object.entries(node.children).map(([key, value]) => [
197
+ key,
198
+ child(value, joinPath(path, key))
199
+ ])
200
+ )
201
+ };
202
+ return routerDeclaration(walk(native, ""));
203
+ };
192
204
 
193
205
  export {
194
- fromPromise,
195
- toPromise,
196
206
  operationDeclaration,
197
207
  nativeOperation,
198
208
  withApproval,
209
+ toolAnnotations,
199
210
  query,
200
211
  mutation,
201
212
  routerDeclaration,
@@ -205,5 +216,6 @@ export {
205
216
  dynamicRouter,
206
217
  joinPath,
207
218
  locate,
208
- declaredOperations
219
+ declaredOperations,
220
+ withApprovals
209
221
  };
@@ -0,0 +1,171 @@
1
+ import {
2
+ ProviderError,
3
+ maxUpstreamMessageLength
4
+ } from "./chunk-7LMU3ER3.js";
5
+ import {
6
+ owned
7
+ } from "./chunk-3CVQ3VPP.js";
8
+ import {
9
+ AccountId
10
+ } from "./chunk-H2CJWXA3.js";
11
+ import {
12
+ Effect_exports,
13
+ Option_exports,
14
+ Schema_exports
15
+ } from "./chunk-IFJU5SYQ.js";
16
+
17
+ // src/implementation/authoring.ts
18
+ var NativeCallback = /* @__PURE__ */ Symbol("apps.NativeCallback");
19
+ var appCode = (code) => owned("app", "app.code", { attributes: { "executor.app.code": code } });
20
+ var fromPromise = (callback, code) => (...args) => {
21
+ if (NativeCallback in callback) {
22
+ const native = callback[NativeCallback];
23
+ return native(...args);
24
+ }
25
+ return Effect_exports.tryPromise({ try: () => callback(...args), catch: (error) => error }).pipe(
26
+ appCode(code)
27
+ );
28
+ };
29
+ var method = (target, key) => {
30
+ const value = target[key];
31
+ const call = (...args) => Reflect.apply(value, target, args);
32
+ if (NativeCallback in value) Object.assign(call, { [NativeCallback]: value[NativeCallback] });
33
+ return call;
34
+ };
35
+ var toPromise = (operation, signalOf, services) => {
36
+ const native = (...args) => Effect_exports.suspend(() => {
37
+ const signal = typeof signalOf === "function" ? signalOf(...args) : signalOf;
38
+ if (signal === void 0) return operation(...args);
39
+ if (signal.aborted) return Effect_exports.interrupt;
40
+ const cancelled = Effect_exports.callback((resume) => {
41
+ if (signal.aborted) {
42
+ resume(Effect_exports.interrupt);
43
+ return;
44
+ }
45
+ const abort = () => resume(Effect_exports.interrupt);
46
+ signal.addEventListener("abort", abort, { once: true });
47
+ return Effect_exports.sync(() => signal.removeEventListener("abort", abort));
48
+ });
49
+ return Effect_exports.raceFirst(
50
+ cancelled,
51
+ Effect_exports.suspend(() => operation(...args))
52
+ );
53
+ });
54
+ return Object.assign(
55
+ (...args) => services === void 0 ? Effect_exports.runPromise(native(...args)) : Effect_exports.runPromiseWith(services)(native(...args)),
56
+ {
57
+ [NativeCallback]: native
58
+ }
59
+ );
60
+ };
61
+
62
+ // src/implementation/upstream-error.ts
63
+ var ErrorToken = Schema_exports.String.check(Schema_exports.isPattern(/^[A-Za-z0-9._-]{1,128}$/u));
64
+ var NestedError = Schema_exports.Struct({
65
+ error: Schema_exports.Struct({ code: Schema_exports.Union([Schema_exports.Int, ErrorToken]), message: Schema_exports.String })
66
+ });
67
+ var OAuthError = Schema_exports.Struct({
68
+ error: ErrorToken,
69
+ error_description: Schema_exports.optional(Schema_exports.String)
70
+ });
71
+ var bounded = (message) => message.length <= maxUpstreamMessageLength ? message : `${message.slice(0, maxUpstreamMessageLength - 1)}\u2026`;
72
+ var isShortenedUpstream = (message) => message.length === maxUpstreamMessageLength && message.endsWith("\u2026");
73
+ var stated = (code, message) => ({
74
+ code,
75
+ ...message === void 0 || message === "" ? {} : { message: bounded(message) }
76
+ });
77
+ var bodyUpstreamError = (json) => {
78
+ const nested = Schema_exports.decodeUnknownOption(NestedError)(json);
79
+ if (Option_exports.isSome(nested)) return stated(nested.value.error.code, nested.value.error.message);
80
+ const oauth = Schema_exports.decodeUnknownOption(OAuthError)(json);
81
+ if (Option_exports.isSome(oauth)) return stated(oauth.value.error, oauth.value.error_description);
82
+ return void 0;
83
+ };
84
+ var authParam = /([A-Za-z][A-Za-z0-9_-]*)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^\s,"]+))/g;
85
+ var challengeUpstreamError = (header) => {
86
+ const bearer = header === void 0 ? -1 : header.search(/\bBearer\b/i);
87
+ if (header === void 0 || bearer === -1) return void 0;
88
+ const params = /* @__PURE__ */ new Map();
89
+ for (const [, name, quoted, token] of header.slice(bearer + "Bearer".length).matchAll(authParam))
90
+ if (name !== void 0 && !params.has(name.toLowerCase()))
91
+ params.set(name.toLowerCase(), quoted?.replace(/\\(.)/g, "$1") ?? token ?? "");
92
+ const code = Schema_exports.decodeUnknownOption(ErrorToken)(params.get("error"));
93
+ return Option_exports.isNone(code) ? void 0 : stated(code.value, params.get("error_description"));
94
+ };
95
+
96
+ // src/implementation/provider-error.ts
97
+ var providerError = ({
98
+ reason,
99
+ status,
100
+ accountId,
101
+ phase,
102
+ upstream
103
+ }) => new ProviderError({
104
+ reason,
105
+ ...status === void 0 ? {} : { status },
106
+ ...accountId === void 0 ? {} : { accountId },
107
+ ...phase === void 0 ? {} : { phase },
108
+ ...upstream === void 0 ? {} : { upstream }
109
+ });
110
+ function parseProviderError(error) {
111
+ return Schema_exports.decodeUnknownOption(ProviderError)(error).pipe(Option_exports.map(providerError));
112
+ }
113
+ function providerErrorDetail(error, detail) {
114
+ return providerError({
115
+ ...error,
116
+ phase: error.phase ?? detail.phase,
117
+ upstream: error.upstream ?? detail.upstream
118
+ });
119
+ }
120
+ function httpProviderError(status, headers = {}) {
121
+ const upstream = challengeUpstreamError(headers["www-authenticate"]);
122
+ if (status >= 500 && status <= 599)
123
+ return providerError({ reason: "unavailable", status, upstream });
124
+ if (status === 401) return providerError({ reason: "unauthorized", status, upstream });
125
+ if (status === 429) return providerError({ reason: "rate_limited", status, upstream });
126
+ if (status !== 403) return void 0;
127
+ const retry = headers["retry-after"];
128
+ if (headers["x-ratelimit-remaining"] === "0" || retry !== void 0 && (/^\d+$/.test(retry) || Number.isFinite(Date.parse(retry))))
129
+ return providerError({ reason: "rate_limited", status, upstream });
130
+ if (typeof upstream?.code === "string" && upstream.code.toLowerCase() === "insufficient_scope")
131
+ return providerError({ reason: "forbidden", status, upstream });
132
+ return providerError({ reason: "rejected", status, upstream });
133
+ }
134
+ function accountProviderError(error, accountId) {
135
+ const parsed = parseProviderError(error);
136
+ const account = Schema_exports.decodeUnknownOption(AccountId)(accountId);
137
+ if (Option_exports.isNone(parsed) || Option_exports.isNone(account)) return error;
138
+ return providerError({ ...parsed.value, accountId: account.value });
139
+ }
140
+ function graphqlProviderError(errors, status) {
141
+ const codes = errors.flatMap((error) => {
142
+ const parsed = Schema_exports.decodeUnknownOption(
143
+ Schema_exports.Struct({
144
+ type: Schema_exports.optional(Schema_exports.String),
145
+ extensions: Schema_exports.optional(Schema_exports.Struct({ code: Schema_exports.optional(Schema_exports.String) }))
146
+ })
147
+ )(error);
148
+ return Option_exports.isSome(parsed) ? [parsed.value.type, parsed.value.extensions?.code] : [];
149
+ });
150
+ if (codes.includes("RATE_LIMITED") || codes.includes("TOO_MANY_REQUESTS"))
151
+ return new ProviderError({ reason: "rate_limited", status });
152
+ if (codes.includes("UNAUTHENTICATED"))
153
+ return new ProviderError({ reason: "unauthorized", status });
154
+ if (codes.includes("FORBIDDEN")) return new ProviderError({ reason: "forbidden", status });
155
+ return void 0;
156
+ }
157
+
158
+ export {
159
+ appCode,
160
+ fromPromise,
161
+ method,
162
+ toPromise,
163
+ isShortenedUpstream,
164
+ bodyUpstreamError,
165
+ providerError,
166
+ parseProviderError,
167
+ providerErrorDetail,
168
+ httpProviderError,
169
+ accountProviderError,
170
+ graphqlProviderError
171
+ };