@every-app/sdk 0.3.0-rc.1 → 0.3.0-rc.11

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 (181) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +62 -64
  3. package/dist/cloudflare/catalogSettings.generated.d.ts +84 -0
  4. package/dist/cloudflare/catalogSettings.generated.d.ts.map +1 -0
  5. package/dist/cloudflare/catalogSettings.generated.js +175 -0
  6. package/dist/cloudflare/index.d.ts +3 -2
  7. package/dist/cloudflare/index.d.ts.map +1 -1
  8. package/dist/cloudflare/index.js +4 -1
  9. package/dist/cloudflare/sentinel.d.ts +60 -2
  10. package/dist/cloudflare/sentinel.d.ts.map +1 -1
  11. package/dist/cloudflare/sentinel.js +37 -2
  12. package/dist/cloudflare/worker.d.ts +391 -63
  13. package/dist/cloudflare/worker.d.ts.map +1 -1
  14. package/dist/cloudflare/worker.js +394 -108
  15. package/dist/dev/index.d.ts +1 -1
  16. package/dist/dev/index.d.ts.map +1 -1
  17. package/dist/dev/index.js +1 -1
  18. package/dist/dev/keypair.d.ts +2 -0
  19. package/dist/dev/keypair.d.ts.map +1 -1
  20. package/dist/dev/keypair.js +2 -0
  21. package/dist/index.d.ts +6 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +5 -0
  24. package/dist/internal/aiDefault.d.ts +205 -0
  25. package/dist/internal/aiDefault.d.ts.map +1 -0
  26. package/dist/internal/aiDefault.js +335 -0
  27. package/dist/internal/aiProviders.d.ts +7 -2
  28. package/dist/internal/aiProviders.d.ts.map +1 -1
  29. package/dist/internal/aiProviders.js +19 -6
  30. package/dist/internal/appSettings.d.ts +82 -4
  31. package/dist/internal/appSettings.d.ts.map +1 -1
  32. package/dist/internal/appSettings.js +204 -29
  33. package/dist/internal/audit.d.ts +108 -0
  34. package/dist/internal/audit.d.ts.map +1 -0
  35. package/dist/internal/audit.js +235 -0
  36. package/dist/internal/capabilities.d.ts +2261 -815
  37. package/dist/internal/capabilities.d.ts.map +1 -1
  38. package/dist/internal/capabilities.js +122 -1034
  39. package/dist/internal/catalog/commerce.d.ts +394 -0
  40. package/dist/internal/catalog/commerce.d.ts.map +1 -0
  41. package/dist/internal/catalog/commerce.js +477 -0
  42. package/dist/internal/catalog/comms.d.ts +375 -0
  43. package/dist/internal/catalog/comms.d.ts.map +1 -0
  44. package/dist/internal/catalog/comms.js +520 -0
  45. package/dist/internal/catalog/index.d.ts +2008 -0
  46. package/dist/internal/catalog/index.d.ts.map +1 -0
  47. package/dist/internal/catalog/index.js +19 -0
  48. package/dist/internal/catalog/inference.d.ts +393 -0
  49. package/dist/internal/catalog/inference.d.ts.map +1 -0
  50. package/dist/internal/catalog/inference.js +460 -0
  51. package/dist/internal/catalog/infra.d.ts +458 -0
  52. package/dist/internal/catalog/infra.d.ts.map +1 -0
  53. package/dist/internal/catalog/infra.js +608 -0
  54. package/dist/internal/catalog/search.d.ts +400 -0
  55. package/dist/internal/catalog/search.d.ts.map +1 -0
  56. package/dist/internal/catalog/search.js +463 -0
  57. package/dist/internal/devProviders.d.ts +2 -2
  58. package/dist/internal/devProviders.d.ts.map +1 -1
  59. package/dist/internal/devProviders.js +19 -1
  60. package/dist/internal/events.d.ts +65 -3
  61. package/dist/internal/events.d.ts.map +1 -1
  62. package/dist/internal/events.js +110 -5
  63. package/dist/internal/gatewaySettings.d.ts +3 -2
  64. package/dist/internal/gatewaySettings.d.ts.map +1 -1
  65. package/dist/internal/gatewaySettings.js +12 -6
  66. package/dist/internal/identity.d.ts +41 -4
  67. package/dist/internal/identity.d.ts.map +1 -1
  68. package/dist/internal/identity.js +48 -10
  69. package/dist/internal/index.d.ts +6 -1
  70. package/dist/internal/index.d.ts.map +1 -1
  71. package/dist/internal/index.js +6 -1
  72. package/dist/internal/members.d.ts +3 -2
  73. package/dist/internal/members.d.ts.map +1 -1
  74. package/dist/internal/oauthBuiltIns.d.ts +101 -0
  75. package/dist/internal/oauthBuiltIns.d.ts.map +1 -0
  76. package/dist/internal/oauthBuiltIns.js +122 -0
  77. package/dist/internal/oauthClient.d.ts +8 -50
  78. package/dist/internal/oauthClient.d.ts.map +1 -1
  79. package/dist/internal/oauthClient.js +2 -177
  80. package/dist/internal/oauthPresets.d.ts +32 -0
  81. package/dist/internal/oauthPresets.d.ts.map +1 -0
  82. package/dist/internal/oauthPresets.js +253 -0
  83. package/dist/internal/oauthScopes.d.ts +43 -100
  84. package/dist/internal/oauthScopes.d.ts.map +1 -1
  85. package/dist/internal/oauthScopes.js +83 -382
  86. package/dist/internal/oauthSpec.d.ts +103 -0
  87. package/dist/internal/oauthSpec.d.ts.map +1 -0
  88. package/dist/internal/oauthSpec.js +520 -0
  89. package/dist/internal/protocol.d.ts +97 -5
  90. package/dist/internal/protocol.d.ts.map +1 -1
  91. package/dist/internal/protocol.js +89 -1
  92. package/dist/internal/providerRegistry.d.ts +17 -6
  93. package/dist/internal/providerRegistry.d.ts.map +1 -1
  94. package/dist/internal/providerRegistry.js +71 -8
  95. package/dist/internal/providerSpec.d.ts +18 -7
  96. package/dist/internal/providerSpec.d.ts.map +1 -1
  97. package/dist/internal/providerSpec.js +420 -34
  98. package/dist/internal/remoteMcp.d.ts.map +1 -1
  99. package/dist/internal/remoteMcp.js +1 -5
  100. package/dist/internal/signInProviders.d.ts +104 -0
  101. package/dist/internal/signInProviders.d.ts.map +1 -0
  102. package/dist/internal/signInProviders.js +245 -0
  103. package/dist/internal/tools.d.ts +5 -4
  104. package/dist/internal/tools.d.ts.map +1 -1
  105. package/dist/internal/tools.js +4 -3
  106. package/dist/manifest.d.ts +267 -0
  107. package/dist/manifest.d.ts.map +1 -0
  108. package/dist/manifest.js +30 -0
  109. package/dist/server/ai.d.ts +111 -0
  110. package/dist/server/ai.d.ts.map +1 -0
  111. package/dist/server/ai.js +145 -0
  112. package/dist/server/aiBinding.d.ts +68 -0
  113. package/dist/server/aiBinding.d.ts.map +1 -0
  114. package/dist/server/aiBinding.js +294 -0
  115. package/dist/server/attribution.d.ts +18 -5
  116. package/dist/server/attribution.d.ts.map +1 -1
  117. package/dist/server/attribution.js +19 -5
  118. package/dist/server/connectUrl.d.ts +16 -0
  119. package/dist/server/connectUrl.d.ts.map +1 -0
  120. package/dist/server/connectUrl.js +40 -0
  121. package/dist/server/context.d.ts +68 -0
  122. package/dist/server/context.d.ts.map +1 -0
  123. package/dist/server/context.js +117 -0
  124. package/dist/server/emailBinding.d.ts +72 -0
  125. package/dist/server/emailBinding.d.ts.map +1 -0
  126. package/dist/server/emailBinding.js +124 -0
  127. package/dist/server/emit.d.ts +1 -1
  128. package/dist/server/emit.d.ts.map +1 -1
  129. package/dist/server/emit.js +7 -7
  130. package/dist/server/env.d.ts +44 -0
  131. package/dist/server/env.d.ts.map +1 -0
  132. package/dist/server/env.js +1 -0
  133. package/dist/server/eventsEndpoint.d.ts +48 -0
  134. package/dist/server/eventsEndpoint.d.ts.map +1 -0
  135. package/dist/server/eventsEndpoint.js +145 -0
  136. package/dist/server/everyApp.d.ts +40 -11
  137. package/dist/server/everyApp.d.ts.map +1 -1
  138. package/dist/server/everyApp.js +116 -6
  139. package/dist/server/fromMcpTools.d.ts +89 -0
  140. package/dist/server/fromMcpTools.d.ts.map +1 -0
  141. package/dist/server/fromMcpTools.js +192 -0
  142. package/dist/server/gatewayFetch.d.ts +42 -18
  143. package/dist/server/gatewayFetch.d.ts.map +1 -1
  144. package/dist/server/gatewayFetch.js +138 -19
  145. package/dist/server/gatewayUrl.d.ts +13 -0
  146. package/dist/server/gatewayUrl.d.ts.map +1 -0
  147. package/dist/server/gatewayUrl.js +34 -0
  148. package/dist/server/index.d.ts +16 -4
  149. package/dist/server/index.d.ts.map +1 -1
  150. package/dist/server/index.js +13 -3
  151. package/dist/server/integrationFetch.d.ts +92 -73
  152. package/dist/server/integrationFetch.d.ts.map +1 -1
  153. package/dist/server/integrationFetch.js +206 -319
  154. package/dist/server/listMembers.d.ts +1 -1
  155. package/dist/server/listMembers.d.ts.map +1 -1
  156. package/dist/server/listMembers.js +8 -11
  157. package/dist/server/refusal.d.ts +55 -0
  158. package/dist/server/refusal.d.ts.map +1 -0
  159. package/dist/server/refusal.js +193 -0
  160. package/dist/server/sendEmail.d.ts +1 -1
  161. package/dist/server/sendEmail.d.ts.map +1 -1
  162. package/dist/server/sendEmail.js +7 -7
  163. package/dist/server/tools.d.ts +7 -2
  164. package/dist/server/tools.d.ts.map +1 -1
  165. package/dist/server/tools.js +2 -1
  166. package/dist/server/toolsEndpoint.d.ts +3 -1
  167. package/dist/server/toolsEndpoint.d.ts.map +1 -1
  168. package/dist/server/toolsEndpoint.js +19 -7
  169. package/dist/server/webhooks.d.ts +56 -0
  170. package/dist/server/webhooks.d.ts.map +1 -0
  171. package/dist/server/webhooks.js +205 -0
  172. package/dist/tanstack-start/index.d.ts +9 -0
  173. package/dist/tanstack-start/index.d.ts.map +1 -0
  174. package/dist/tanstack-start/index.js +29 -0
  175. package/dist/testing/index.d.ts +93 -0
  176. package/dist/testing/index.d.ts.map +1 -0
  177. package/dist/testing/index.js +156 -0
  178. package/package.json +35 -14
  179. package/dist/server/authorizeUrl.d.ts +0 -43
  180. package/dist/server/authorizeUrl.d.ts.map +0 -1
  181. package/dist/server/authorizeUrl.js +0 -110
@@ -0,0 +1,267 @@
1
+ /**
2
+ * The manifest an app author writes as `everyapp.config.ts`, typed:
3
+ *
4
+ * import { defineEveryApp } from "@every-app/sdk";
5
+ *
6
+ * export default defineEveryApp({
7
+ * id: "notes",
8
+ * name: "Notes",
9
+ * resources: { d1: ["DB"] },
10
+ * });
11
+ *
12
+ * Types only, and a leaf module: `cloudflare.config.ts` imports the manifest,
13
+ * and the Cloudflare Vite plugin hides from the Worker every file the config
14
+ * imports. The rules beyond the shape (which ids exist, which names are
15
+ * reserved, which scopes a provider takes) are the CLI's: `everyapp deploy`
16
+ * and `everyapp dev` validate the manifest and say what is wrong. The
17
+ * perimeter's schema (`@every-app/perimeter/manifest`) is held to this type
18
+ * by a test there.
19
+ *
20
+ * The Worker reads the manifest back as `env.EVERYAPP_MANIFEST`, which
21
+ * `everyAppWorker()` sets and `cf workers types` types from the file.
22
+ */
23
+ /** Why a setting is needed and whether the app opens without it (the settings model). */
24
+ export interface EveryAppSettingNeed {
25
+ /** Why the app needs it, for whoever sets it. At most 200 characters. */
26
+ readonly why?: string;
27
+ /** An https page that says where the key or client comes from. */
28
+ readonly docs?: string;
29
+ /** The app opens without it; the feature using it says what is missing. */
30
+ readonly optional?: boolean;
31
+ /** The group of which one member is enough ("AI model"). */
32
+ readonly anyOf?: string;
33
+ }
34
+ /** A route reachable without a signed-in member. */
35
+ export interface EveryAppPublicRoute {
36
+ /** Absolute glob: `*` matches one segment, `**` one or more. */
37
+ readonly path: string;
38
+ /** The methods it answers; `["GET"]` when left out. */
39
+ readonly methods?: readonly string[];
40
+ }
41
+ /** A Durable Object or Workflow class: its binding, the class, and the helper Worker that defines it. */
42
+ export interface EveryAppClassResource {
43
+ readonly name: string;
44
+ readonly className: string;
45
+ readonly worker?: string;
46
+ }
47
+ /** A Cloudflare Queue's consumer settings. */
48
+ export interface EveryAppQueueConsumer {
49
+ readonly worker?: string;
50
+ readonly maxBatchSize?: number;
51
+ readonly maxBatchTimeout?: number;
52
+ readonly maxRetries?: number;
53
+ readonly maxConcurrency?: number | null;
54
+ readonly retryDelay?: number;
55
+ readonly visibilityTimeoutMs?: number;
56
+ readonly deadLetterQueue?: string;
57
+ }
58
+ /** Cloudflare's limits for one of the app's Workers. */
59
+ export interface EveryAppWorkerLimits {
60
+ /** CPU milliseconds an invocation may use, up to 300000. */
61
+ readonly cpuMs?: number;
62
+ }
63
+ /** The Cloudflare resources the app's Workers bind; the deploy creates them. */
64
+ export interface EveryAppResources {
65
+ readonly d1?: readonly string[];
66
+ readonly kv?: readonly string[];
67
+ readonly r2?: readonly string[];
68
+ readonly durableObjects?: readonly EveryAppClassResource[];
69
+ readonly workflows?: readonly EveryAppClassResource[];
70
+ readonly hyperdrive?: readonly {
71
+ readonly name: string;
72
+ readonly worker?: string;
73
+ readonly caching?: "off" | "default";
74
+ }[];
75
+ readonly queues?: readonly {
76
+ readonly name: string;
77
+ readonly consumer?: true | EveryAppQueueConsumer;
78
+ }[];
79
+ readonly ai?: readonly string[];
80
+ readonly browser?: readonly string[];
81
+ readonly images?: readonly string[];
82
+ readonly vectorize?: readonly {
83
+ readonly name: string;
84
+ readonly dimensions?: number;
85
+ readonly metric?: "cosine" | "euclidean" | "dot-product";
86
+ /** A Workers AI embedding model's shape, instead of `dimensions` and `metric`. */
87
+ readonly preset?: "@cf/baai/bge-small-en-v1.5" | "@cf/baai/bge-base-en-v1.5" | "@cf/baai/bge-large-en-v1.5" | "openai/text-embedding-ada-002" | "cohere/embed-multilingual-v2.0";
88
+ }[];
89
+ readonly aiGateway?: readonly {
90
+ readonly name: string;
91
+ readonly collectLogs?: boolean;
92
+ readonly cacheTtl?: number;
93
+ }[];
94
+ readonly sendEmail?: readonly {
95
+ readonly name: string;
96
+ readonly allowedSenderAddresses: readonly string[];
97
+ readonly destinationAddress?: string;
98
+ readonly allowedDestinationAddresses?: readonly string[];
99
+ }[];
100
+ readonly secretsStore?: readonly {
101
+ readonly name: string;
102
+ }[];
103
+ readonly analyticsEngine?: readonly {
104
+ readonly name: string;
105
+ readonly dataset?: string;
106
+ }[];
107
+ readonly artifacts?: readonly {
108
+ readonly name: string;
109
+ }[];
110
+ }
111
+ /**
112
+ * One capability the Gateway serves the app with: an API-key provider or
113
+ * a service, by `access`; an OAuth provider, by the provider's own scopes.
114
+ */
115
+ export type EveryAppCapabilityEntry = EveryAppKeyCapabilityEntry | EveryAppOAuthCapabilityEntry;
116
+ /** A provider whose key the Gateway holds, or a Gateway service. */
117
+ export interface EveryAppKeyCapabilityEntry extends EveryAppSettingNeed {
118
+ readonly access: "read" | "write";
119
+ readonly scopes?: never;
120
+ }
121
+ /**
122
+ * An OAuth provider (`google-workspace`, `github`): the provider's own
123
+ * scope strings the app needs, short (`gmail.modify`) or in full. A person
124
+ * connects in the app for exactly these; the Gateway adds the scopes that
125
+ * name the account, and refuses a token that carries more.
126
+ */
127
+ export interface EveryAppOAuthCapabilityEntry extends EveryAppSettingNeed {
128
+ readonly scopes: readonly string[];
129
+ /** Why the app needs it, shown where people connect. At most 200 characters. */
130
+ readonly why: string;
131
+ readonly access?: never;
132
+ }
133
+ /**
134
+ * One of the app's plain settings: a value that isn't a secret and
135
+ * differs per install (a PostHog host, a region), set with `everyapp
136
+ * configure <app>` and read from `env`.
137
+ */
138
+ export interface EveryAppSettingEntry {
139
+ readonly name: string;
140
+ /** Why the app needs it: what whoever installs the app reads. */
141
+ readonly why: string;
142
+ readonly docs?: string;
143
+ readonly optional?: boolean;
144
+ /** What it takes; text when not given. */
145
+ readonly type?: "text" | "url" | "number" | "boolean";
146
+ /** The values a text setting may take. */
147
+ readonly values?: readonly string[];
148
+ /** What the app reads while nobody has set it; the deploy writes it. */
149
+ readonly default?: string;
150
+ }
151
+ /** One of the app's own secrets, with its need. */
152
+ export interface EveryAppSecretEntry extends EveryAppSettingNeed {
153
+ readonly name: string;
154
+ /** Its guide in the everyapp skill (`"setup/main-secret.md"`). */
155
+ readonly setup?: string;
156
+ }
157
+ /** `everyapp.config.ts`. */
158
+ export interface EveryAppManifest {
159
+ /** Stable id, kebab-case: the app's hostname and Worker. */
160
+ readonly id: string;
161
+ readonly name?: string;
162
+ /** The one line the Gateway's app library shows under the name. */
163
+ readonly description?: string;
164
+ /** The path of an SVG the app serves (`"/everyapp-icon.svg"`). */
165
+ readonly icon?: string;
166
+ /** Legacy: the entry comes from `cloudflare.config.ts`. */
167
+ readonly main?: string;
168
+ /** YYYY-MM-DD. */
169
+ readonly compatibilityDate?: string;
170
+ readonly resources?: EveryAppResources;
171
+ /** Helper Workers deployed beside the app's as `<app worker>--<name>`. */
172
+ readonly workers?: readonly {
173
+ readonly name: string;
174
+ readonly binding?: string;
175
+ readonly limits?: EveryAppWorkerLimits;
176
+ }[];
177
+ readonly database?: {
178
+ readonly provider: "d1" | "postgres";
179
+ /** A cloud region such as `"aws:us-east-1"`. */
180
+ readonly region?: string;
181
+ };
182
+ readonly limits?: EveryAppWorkerLimits;
183
+ /** Routes open without a signed-in member. Default: none. */
184
+ readonly public?: readonly EveryAppPublicRoute[];
185
+ /**
186
+ * Static files of the asset bundle anyone may fetch (favicons, icons, an
187
+ * OG image): path patterns, GET and HEAD, never Worker code.
188
+ */
189
+ readonly publicAssets?: readonly string[];
190
+ /**
191
+ * Routes a bearer client (a native app, a script with a personal token)
192
+ * reaches with its token only: `paths` in `public`'s glob syntax, every
193
+ * method, and `why` the app needs them, shown to the operator. The
194
+ * Gateway answers 401 to a request carrying its session cookie there,
195
+ * and `everyapp access on` lets these paths through Cloudflare Access.
196
+ * Default: none.
197
+ */
198
+ readonly bearerRoutes?: {
199
+ readonly paths: readonly string[];
200
+ readonly why: string;
201
+ };
202
+ /** AI providers the Gateway calls for the app, by id or with their need. */
203
+ readonly providers?: readonly (string | (EveryAppSettingNeed & {
204
+ readonly id: string;
205
+ }))[];
206
+ /** Capabilities by id (`members`, `email`, `dataforseo`, …). */
207
+ readonly capabilities?: {
208
+ readonly [id: string]: EveryAppCapabilityEntry;
209
+ };
210
+ /** The app's own secrets, by name or with their need. */
211
+ readonly secrets?: readonly (string | EveryAppSecretEntry)[];
212
+ /** The app's plain settings: per-install values that aren't secrets. */
213
+ readonly settings?: readonly EveryAppSettingEntry[];
214
+ readonly ai?: {
215
+ /** Model ids the app may request; any other is `model_not_allowed`. */
216
+ readonly models?: readonly string[];
217
+ /** OpenAI API paths beyond inference (`"/v1/files"`). */
218
+ readonly routes?: readonly string[];
219
+ /** The app takes the Gateway's default chat model (`ai()`). */
220
+ readonly chat?: boolean | EveryAppSettingNeed;
221
+ };
222
+ /** `inbound: true`: the Worker exports an `email()` handler. */
223
+ readonly email?: {
224
+ readonly inbound?: boolean;
225
+ };
226
+ /** Who can open the app once registered; `"admins"` by default. */
227
+ readonly access?: "everyone" | "admins";
228
+ readonly serverFnGuard?: boolean;
229
+ readonly serverFnBase?: string;
230
+ /** Shell command that builds the app; `cf build` by default. */
231
+ readonly build?: string;
232
+ /** Shell command `everyapp dev` starts; it must listen on PORT. */
233
+ readonly dev?: string;
234
+ /** Legacy: the port comes from PORT. */
235
+ readonly devPort?: number;
236
+ /** Dependency install command; false skips it. */
237
+ readonly install?: false | string;
238
+ readonly migrations?: {
239
+ readonly engine: "drizzle" | "d1-sql" | "prisma";
240
+ readonly dialect?: "sqlite" | "postgresql";
241
+ readonly dir?: string;
242
+ readonly config?: string;
243
+ readonly binding?: string;
244
+ };
245
+ /** Who answers `/mcp` on the app's own host, and why it needs to. */
246
+ readonly mcp?: {
247
+ readonly by: "gateway" | "app";
248
+ readonly why: string;
249
+ };
250
+ /**
251
+ * The Gateway events the app receives at `everyApp(handler, { events: {
252
+ * on } })`: types or patterns from `member.*` (but `member.password_reset`),
253
+ * `group.*`, `app.*`, `mcp_app.*`, `connection.*` and `tool.*`, for the
254
+ * app's own organization. Anything else is refused at deploy.
255
+ */
256
+ readonly events?: {
257
+ readonly subscribe: readonly string[];
258
+ readonly why: string;
259
+ };
260
+ }
261
+ /**
262
+ * Type `everyapp.config.ts` without widening it: the literal binding names
263
+ * stay, so `everyAppWorker()` types `env.DB` and `env.EVERYAPP_MANIFEST`
264
+ * from them. Returns the manifest as given.
265
+ */
266
+ export declare function defineEveryApp<const M extends EveryAppManifest>(manifest: M): M;
267
+ //# sourceMappingURL=manifest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,yFAAyF;AACzF,MAAM,WAAW,mBAAmB;IAClC,yEAAyE;IACzE,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,kEAAkE;IAClE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,2EAA2E;IAC3E,QAAQ,CAAC,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AC,EAAE;QAChB,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;QACtC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;KACtB,CAAC;CACH;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,CAAC,CAAC,SAAS,gBAAgB,EAC7D,QAAQ,EAAE,CAAC,GACV,CAAC,CAEH"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The manifest an app author writes as `everyapp.config.ts`, typed:
3
+ *
4
+ * import { defineEveryApp } from "@every-app/sdk";
5
+ *
6
+ * export default defineEveryApp({
7
+ * id: "notes",
8
+ * name: "Notes",
9
+ * resources: { d1: ["DB"] },
10
+ * });
11
+ *
12
+ * Types only, and a leaf module: `cloudflare.config.ts` imports the manifest,
13
+ * and the Cloudflare Vite plugin hides from the Worker every file the config
14
+ * imports. The rules beyond the shape (which ids exist, which names are
15
+ * reserved, which scopes a provider takes) are the CLI's: `everyapp deploy`
16
+ * and `everyapp dev` validate the manifest and say what is wrong. The
17
+ * perimeter's schema (`@every-app/perimeter/manifest`) is held to this type
18
+ * by a test there.
19
+ *
20
+ * The Worker reads the manifest back as `env.EVERYAPP_MANIFEST`, which
21
+ * `everyAppWorker()` sets and `cf workers types` types from the file.
22
+ */
23
+ /**
24
+ * Type `everyapp.config.ts` without widening it: the literal binding names
25
+ * stay, so `everyAppWorker()` types `env.DB` and `env.EVERYAPP_MANIFEST`
26
+ * from them. Returns the manifest as given.
27
+ */
28
+ export function defineEveryApp(manifest) {
29
+ return manifest;
30
+ }
@@ -0,0 +1,111 @@
1
+ /**
2
+ * `ai()`: the Gateway's default chat model for an app that needs "a model"
3
+ * rather than one provider (docs/design/2026-10-model-providers.md). The
4
+ * manifest says `ai: { chat: true }` (or `{ chat: { why } }`); the Gateway's
5
+ * operator decides which provider and model that is (`everyapp ai
6
+ * default`, else OpenRouter → Workers AI → OpenAI → …, the first with its
7
+ * key set), and the app never names either: it sends OpenAI-shaped chat
8
+ * completions (and embeddings) to `/api/ai/default/v1/…` with `model:
9
+ * "default"` or no model at all, and the Gateway fills the model in.
10
+ *
11
+ * An OpenAI client takes `baseURL` and `fetch` as they are:
12
+ *
13
+ * const { baseURL, fetch, model } = ai({ env, for: request });
14
+ * const client = new OpenAI({ baseURL, fetch, apiKey: "gateway-managed" });
15
+ * await client.chat.completions.create({ model, messages });
16
+ *
17
+ * In local development with no Gateway, the developer's own key in
18
+ * `.dev.vars` decides the provider by the same order (createGatewayFetch's
19
+ * fallback). An app that needs one provider in particular keeps listing it
20
+ * under `providers` and calling `/api/ai/<provider>/…`.
21
+ */
22
+ import { AI_DEFAULT_MODEL } from "../internal/index.js";
23
+ import { type GatewayCallFor } from "./attribution.js";
24
+ import { type CreateGatewayFetchOptions } from "./gatewayFetch.js";
25
+ import type { SetupInfo } from "./refusal.js";
26
+ export interface AiClient {
27
+ /** `http://every-app-gateway.internal/api/ai/default/v1`: an OpenAI client's base URL. */
28
+ baseURL: string;
29
+ /** The fetch that reaches the Gateway (or, in dev, the provider directly). */
30
+ fetch: typeof fetch;
31
+ /** The model name that means "whatever the Gateway's default is": `default`. */
32
+ model: typeof AI_DEFAULT_MODEL;
33
+ /** The default route's chat completions URL, for a plain `fetch`. */
34
+ chatCompletionsUrl: string;
35
+ /** The default route's embeddings URL, for a plain `fetch`. */
36
+ embeddingsUrl: string;
37
+ }
38
+ /** The base URL of the default route, as the SDK and the docs name it. */
39
+ export declare const AI_DEFAULT_BASE_URL = "http://every-app-gateway.internal/api/ai/default/v1";
40
+ /**
41
+ * The Gateway's default chat model as a client: a base URL, a fetch and
42
+ * the model name to send. Throws `ConfigurationError` where
43
+ * `createGatewayFetch` does (no binding and no dev key) and
44
+ * `AttributionError` where a shared app's call says nobody's.
45
+ */
46
+ export declare function ai(options: CreateGatewayFetchOptions & {
47
+ optional: true;
48
+ }): AiClient | null;
49
+ export declare function ai(options: CreateGatewayFetchOptions): AiClient;
50
+ /** Whether model calls to a provider (or the default) would go through. */
51
+ export interface AiStatus extends SetupInfo {
52
+ /** The id asked about: `default`, or a provider's (`openrouter`, `workers-ai`, …). */
53
+ provider: string;
54
+ /** The manifest asks for it: `ai.chat` for `default`, else `providers`. */
55
+ requested: boolean;
56
+ /** The Gateway (in local dev, `.dev.vars`) holds every setting it needs. */
57
+ configured: boolean;
58
+ /** Calls would go through. */
59
+ ready: boolean;
60
+ /**
61
+ * For `default`: the provider and model calls go to now, or null while
62
+ * none is configured. Null for a named provider.
63
+ */
64
+ resolved: {
65
+ provider: string;
66
+ label: string;
67
+ model: string;
68
+ } | null;
69
+ }
70
+ /**
71
+ * Whether the app can use a model right now, without calling one: gate
72
+ * an AI feature and show `setup` ("A chat model isn't set up on this
73
+ * Gateway: it needs OPENROUTER_API_KEY. An admin runs: pnpm everyapp
74
+ * configure inbox") up front instead of failing mid-task. `provider`
75
+ * defaults to `default`, the Gateway's default model (`ai: { chat: true }`);
76
+ * name one the manifest lists under `providers` to ask about it instead.
77
+ * The Gateway owns the wording: show `setup` and `command` as they are.
78
+ *
79
+ * Over the binding it sends `GET /api/ai/<provider>`; in local dev it
80
+ * answers from `.dev.vars` by the same rule `createGatewayFetch()` uses.
81
+ */
82
+ export declare function aiStatus({ env, provider, for: explicitTarget, }: {
83
+ env: object;
84
+ provider?: string;
85
+ /**
86
+ * Whom the status is for: the default resolves on that organization's
87
+ * choice (`everyapp ai default`). Left out, the request `everyApp()` is
88
+ * serving or the organization `withOrganization()` named, as on every
89
+ * call.
90
+ */
91
+ for?: GatewayCallFor;
92
+ }): Promise<AiStatus>;
93
+ /**
94
+ * A provider's root behind the Gateway, `http://every-app-gateway.internal/api/ai/<provider>`:
95
+ * join the provider's own paths onto it (`/api/v1/chat/completions` for
96
+ * OpenRouter) and send them with `createGatewayFetch()`. For an OpenAI
97
+ * client's `baseURL`, use {@link aiBaseUrl}.
98
+ */
99
+ export declare function providerBaseUrl(provider: string): string;
100
+ /**
101
+ * The OpenAI-compatible base URL of `provider` behind the Gateway, for an
102
+ * OpenAI client's `baseURL` (with `fetch: createGatewayFetch(...)`):
103
+ * `default` (the default) is {@link AI_DEFAULT_BASE_URL},
104
+ * `…/api/ai/default/v1`; a catalog provider's is its root and the prefix
105
+ * of its chat route (`…/api/ai/openrouter/api/v1`, `…/api/ai/workers-ai/v1`,
106
+ * `…/api/ai/openai/v1`). Throws `ConfigurationError` for a provider with
107
+ * no OpenAI-compatible chat route (Cohere): call {@link providerBaseUrl}
108
+ * with its own paths instead.
109
+ */
110
+ export declare function aiBaseUrl(provider?: string): string;
111
+ //# sourceMappingURL=ai.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../src/server/ai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,EACL,gBAAgB,EAOjB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAGL,KAAK,cAAc,EACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAIL,KAAK,yBAAyB,EAC/B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,MAAM,WAAW,QAAQ;IACvB,0FAA0F;IAC1F,OAAO,EAAE,MAAM,CAAC;IAChB,8EAA8E;IAC9E,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,gFAAgF;IAChF,KAAK,EAAE,OAAO,gBAAgB,CAAC;IAC/B,qEAAqE;IACrE,kBAAkB,EAAE,MAAM,CAAC;IAC3B,+DAA+D;IAC/D,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,0EAA0E;AAC1E,eAAO,MAAM,mBAAmB,wDAAuD,CAAC;AAExF;;;;;GAKG;AACH,wBAAgB,EAAE,CAChB,OAAO,EAAE,yBAAyB,GAAG;IAAE,QAAQ,EAAE,IAAI,CAAA;CAAE,GACtD,QAAQ,GAAG,IAAI,CAAC;AACnB,wBAAgB,EAAE,CAAC,OAAO,EAAE,yBAAyB,GAAG,QAAQ,CAAC;AAajE,2EAA2E;AAC3E,MAAM,WAAW,QAAS,SAAQ,SAAS;IACzC,sFAAsF;IACtF,QAAQ,EAAE,MAAM,CAAC;IACjB,2EAA2E;IAC3E,SAAS,EAAE,OAAO,CAAC;IACnB,4EAA4E;IAC5E,UAAU,EAAE,OAAO,CAAC;IACpB,8BAA8B;IAC9B,KAAK,EAAE,OAAO,CAAC;IACf;;;OAGG;IACH,QAAQ,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;CACrE;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,QAAQ,CAAC,EAC7B,GAAG,EACH,QAA8B,EAC9B,GAAG,EAAE,cAAc,GACpB,EAAE;IACD,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;OAKG;IACH,GAAG,CAAC,EAAE,cAAc,CAAC;CACtB,GAAG,OAAO,CAAC,QAAQ,CAAC,CA+EpB;AAID;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAExD;AAED;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,QAAQ,GAAE,MAA4B,GAAG,MAAM,CAaxE"}
@@ -0,0 +1,145 @@
1
+ /**
2
+ * `ai()`: the Gateway's default chat model for an app that needs "a model"
3
+ * rather than one provider (docs/design/2026-10-model-providers.md). The
4
+ * manifest says `ai: { chat: true }` (or `{ chat: { why } }`); the Gateway's
5
+ * operator decides which provider and model that is (`everyapp ai
6
+ * default`, else OpenRouter → Workers AI → OpenAI → …, the first with its
7
+ * key set), and the app never names either: it sends OpenAI-shaped chat
8
+ * completions (and embeddings) to `/api/ai/default/v1/…` with `model:
9
+ * "default"` or no model at all, and the Gateway fills the model in.
10
+ *
11
+ * An OpenAI client takes `baseURL` and `fetch` as they are:
12
+ *
13
+ * const { baseURL, fetch, model } = ai({ env, for: request });
14
+ * const client = new OpenAI({ baseURL, fetch, apiKey: "gateway-managed" });
15
+ * await client.chat.completions.create({ model, messages });
16
+ *
17
+ * In local development with no Gateway, the developer's own key in
18
+ * `.dev.vars` decides the provider by the same order (createGatewayFetch's
19
+ * fallback). An app that needs one provider in particular keeps listing it
20
+ * under `providers` and calling `/api/ai/<provider>/…`.
21
+ */
22
+ import { AI_DEFAULT_MODEL, AI_DEFAULT_PROVIDER, CATALOG, ConfigurationError, aiProviderSettings, devAiProvider, resolveAiDefault, } from "../internal/index.js";
23
+ import { applyAttribution, attributionFor, } from "./attribution.js";
24
+ import { BINDING_ORIGIN, createGatewayFetch, gatewayBinding, } from "./gatewayFetch.js";
25
+ /** The base URL of the default route, as the SDK and the docs name it. */
26
+ export const AI_DEFAULT_BASE_URL = `${BINDING_ORIGIN}/api/ai/${AI_DEFAULT_PROVIDER}/v1`;
27
+ export function ai(options) {
28
+ const fetch = createGatewayFetch(options);
29
+ if (!fetch)
30
+ return null;
31
+ return {
32
+ baseURL: AI_DEFAULT_BASE_URL,
33
+ fetch,
34
+ model: AI_DEFAULT_MODEL,
35
+ chatCompletionsUrl: `${AI_DEFAULT_BASE_URL}/chat/completions`,
36
+ embeddingsUrl: `${AI_DEFAULT_BASE_URL}/embeddings`,
37
+ };
38
+ }
39
+ /**
40
+ * Whether the app can use a model right now, without calling one: gate
41
+ * an AI feature and show `setup` ("A chat model isn't set up on this
42
+ * Gateway: it needs OPENROUTER_API_KEY. An admin runs: pnpm everyapp
43
+ * configure inbox") up front instead of failing mid-task. `provider`
44
+ * defaults to `default`, the Gateway's default model (`ai: { chat: true }`);
45
+ * name one the manifest lists under `providers` to ask about it instead.
46
+ * The Gateway owns the wording: show `setup` and `command` as they are.
47
+ *
48
+ * Over the binding it sends `GET /api/ai/<provider>`; in local dev it
49
+ * answers from `.dev.vars` by the same rule `createGatewayFetch()` uses.
50
+ */
51
+ export async function aiStatus({ env, provider = AI_DEFAULT_PROVIDER, for: explicitTarget, }) {
52
+ const target = attributionFor(env, explicitTarget, "aiStatus");
53
+ const values = env;
54
+ const binding = gatewayBinding(env);
55
+ const unavailable = {
56
+ provider,
57
+ requested: false,
58
+ configured: false,
59
+ ready: false,
60
+ resolved: null,
61
+ settings: [],
62
+ setup: null,
63
+ command: null,
64
+ };
65
+ if (binding && typeof binding.fetch === "function") {
66
+ const headers = new Headers();
67
+ if (target !== undefined)
68
+ applyAttribution(headers, target);
69
+ const response = await binding.fetch(`${BINDING_ORIGIN}/api/ai/${encodeURIComponent(provider)}`, { headers });
70
+ if (!response.ok)
71
+ return unavailable;
72
+ const body = (await response.json());
73
+ return {
74
+ ...unavailable,
75
+ ...body,
76
+ provider,
77
+ settings: Array.isArray(body.settings) ? body.settings : [],
78
+ setup: typeof body.setup === "string" ? body.setup : null,
79
+ command: typeof body.command === "string" ? body.command : null,
80
+ resolved: body.resolved ?? null,
81
+ };
82
+ }
83
+ if (!values["EVERYAPP_DEV"])
84
+ return unavailable;
85
+ const isSet = (name) => typeof values[name] === "string" && values[name].trim() !== "";
86
+ const dev = (label, settings, configured, resolved, also = "") => ({
87
+ provider,
88
+ requested: true,
89
+ configured,
90
+ ready: configured,
91
+ resolved,
92
+ settings: configured ? [] : [...settings],
93
+ setup: configured
94
+ ? null
95
+ : `${label} isn't set up for local development: set ${settings.join(" and ")}${also} in .dev.vars.`,
96
+ command: null,
97
+ });
98
+ if (provider === AI_DEFAULT_PROVIDER) {
99
+ const resolution = resolveAiDefault({ provider: null, isSet });
100
+ return dev("A chat model", resolution.settings, resolution.configured, resolution.configured
101
+ ? {
102
+ provider: resolution.provider,
103
+ label: resolution.label,
104
+ model: resolution.model,
105
+ }
106
+ : null, " (or another model provider's key)");
107
+ }
108
+ const spec = devAiProvider(provider);
109
+ if (!spec)
110
+ return unavailable;
111
+ const settings = aiProviderSettings(spec);
112
+ return dev(CATALOG.get(provider)?.label ?? provider, settings, settings.every(isSet), null);
113
+ }
114
+ const CHAT_COMPLETIONS = "/chat/completions";
115
+ /**
116
+ * A provider's root behind the Gateway, `http://every-app-gateway.internal/api/ai/<provider>`:
117
+ * join the provider's own paths onto it (`/api/v1/chat/completions` for
118
+ * OpenRouter) and send them with `createGatewayFetch()`. For an OpenAI
119
+ * client's `baseURL`, use {@link aiBaseUrl}.
120
+ */
121
+ export function providerBaseUrl(provider) {
122
+ return `${BINDING_ORIGIN}/api/ai/${encodeURIComponent(provider)}`;
123
+ }
124
+ /**
125
+ * The OpenAI-compatible base URL of `provider` behind the Gateway, for an
126
+ * OpenAI client's `baseURL` (with `fetch: createGatewayFetch(...)`):
127
+ * `default` (the default) is {@link AI_DEFAULT_BASE_URL},
128
+ * `…/api/ai/default/v1`; a catalog provider's is its root and the prefix
129
+ * of its chat route (`…/api/ai/openrouter/api/v1`, `…/api/ai/workers-ai/v1`,
130
+ * `…/api/ai/openai/v1`). Throws `ConfigurationError` for a provider with
131
+ * no OpenAI-compatible chat route (Cohere): call {@link providerBaseUrl}
132
+ * with its own paths instead.
133
+ */
134
+ export function aiBaseUrl(provider = AI_DEFAULT_PROVIDER) {
135
+ if (provider === AI_DEFAULT_PROVIDER)
136
+ return AI_DEFAULT_BASE_URL;
137
+ const capability = CATALOG.get(provider);
138
+ const chat = capability?.mode === "ai" && "chat" in capability
139
+ ? capability.chat
140
+ : undefined;
141
+ if (!chat?.path.endsWith(CHAT_COMPLETIONS)) {
142
+ throw new ConfigurationError(`${provider} has no OpenAI-compatible chat route in the catalog: join its own paths onto providerBaseUrl("${provider}") instead.`);
143
+ }
144
+ return `${providerBaseUrl(provider)}${chat.path.slice(0, -CHAT_COMPLETIONS.length)}`;
145
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * `gatewayAiBinding(env)`: the Gateway's AI providers as a Workers AI
3
+ * binding, for code written against `env.AI.run(model, inputs, options)`.
4
+ *
5
+ * An app on Every App needs no `ai` binding of its own to call models: the
6
+ * Gateway holds the keys, counts the calls and keeps the app to `ai.models`.
7
+ * Code that already calls `env.AI.run()` (upstream code a port keeps, the
8
+ * Agents SDK, `workers-ai-provider`) gets this object in its place:
9
+ *
10
+ * const AI = gatewayAiBinding(env, { for: request });
11
+ * await AI.run("@cf/meta/llama-3.1-8b-instruct-fast", { messages });
12
+ *
13
+ * Each `run()` becomes an OpenAI-compatible call on the provider's route
14
+ * through the Gateway (`createGatewayFetch`): text generation (`messages`
15
+ * or `prompt`) on its chat completions route, text embeddings (`text`) on
16
+ * its embeddings route, answered in Workers AI's shapes (`{ response,
17
+ * tool_calls, usage }`, `{ shape, data }`, or a stream of `data:
18
+ * {"response": …}` events with `stream: true`). In local development
19
+ * without a Gateway, the developer's own key in `.dev.vars` is used, as
20
+ * `createGatewayFetch` does (WORKERS_AI_API_TOKEN and CLOUDFLARE_ACCOUNT_ID
21
+ * for Workers AI).
22
+ */
23
+ import { AI_DEFAULT_PROVIDER, type AiProviderId } from "../internal/index.js";
24
+ import type { GatewayCallFor } from "./attribution.js";
25
+ export interface GatewayAiBindingOptions {
26
+ /**
27
+ * Whom the calls are for: the inbound request, or `{ background: true,
28
+ * organizationId }` (createGatewayFetch's `for`).
29
+ */
30
+ for?: GatewayCallFor;
31
+ /**
32
+ * The Gateway provider the calls go to: an `ai` entry of the catalog
33
+ * with OpenAI-compatible routes (`workers-ai`, `openrouter`, `openai`,
34
+ * …), or `default`, the operator's model (`ai: { chat: true }`).
35
+ * Default: `workers-ai`, the provider `env.AI` reaches, so `@cf/…` model
36
+ * ids work as they are. The manifest lists the provider under
37
+ * `providers` (or says `ai.chat` for `default`).
38
+ */
39
+ provider?: AiProviderId | typeof AI_DEFAULT_PROVIDER;
40
+ /**
41
+ * The model id to send for a model the code names, e.g.
42
+ * `{ "@cf/meta/llama-3.1-8b-instruct-fast": "meta-llama/llama-3.1-8b-instruct" }`
43
+ * with `provider: "openrouter"`. A model not listed is sent as it is,
44
+ * except that on `default` an `@cf/…` id becomes `default` (the
45
+ * operator's model).
46
+ */
47
+ models?: Readonly<Record<string, string>>;
48
+ }
49
+ /**
50
+ * A Workers AI binding (`Ai`) over the Gateway: pass it where code takes
51
+ * `env.AI`. `run()` covers text generation (messages or prompt; tools;
52
+ * `stream: true`; `returnRawResponse`, which returns the provider's
53
+ * OpenAI-shaped `Response`) and text embeddings; the binding's other
54
+ * methods (`gateway()`, `autorag()`, `models()`, `toMarkdown()`, …) and
55
+ * other tasks (images, audio, batches, websockets) throw
56
+ * `ConfigurationError`. `options.gateway` is ignored: the Gateway's
57
+ * operator decides whether calls go through Cloudflare AI Gateway
58
+ * (`everyapp ai gateway on`).
59
+ *
60
+ * Errors are thrown as a binding throws them: `GatewayRefusalError` when
61
+ * the Gateway refused (`ai_provider_not_configured`, `model_not_allowed`,
62
+ * …), `ConfigurationError` where createGatewayFetch throws one (no binding
63
+ * and no dev key), and an `Error` naming the status when the provider
64
+ * answered with an error. Nothing is checked when the binding is made, so
65
+ * making one per request costs nothing.
66
+ */
67
+ export declare function gatewayAiBinding(env: object, options?: GatewayAiBindingOptions): Ai;
68
+ //# sourceMappingURL=aiBinding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aiBinding.d.ts","sourceRoot":"","sources":["../../src/server/aiBinding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,EAEL,mBAAmB,EAInB,KAAK,YAAY,EAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAIvD,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,GAAG,CAAC,EAAE,cAAc,CAAC;IACrB;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,YAAY,GAAG,OAAO,mBAAmB,CAAC;IACrD;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CAC3C;AAUD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,uBAA4B,GACpC,EAAE,CAoEJ"}