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

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 (113) hide show
  1. package/README.md +608 -2
  2. package/dist/cloudflare/index.d.ts +22 -0
  3. package/dist/cloudflare/index.d.ts.map +1 -0
  4. package/dist/cloudflare/index.js +21 -0
  5. package/dist/cloudflare/sentinel.d.ts +69 -0
  6. package/dist/cloudflare/sentinel.d.ts.map +1 -0
  7. package/dist/cloudflare/sentinel.js +50 -0
  8. package/dist/cloudflare/worker.d.ts +511 -0
  9. package/dist/cloudflare/worker.d.ts.map +1 -0
  10. package/dist/cloudflare/worker.js +738 -0
  11. package/dist/dev/index.d.ts +8 -0
  12. package/dist/dev/index.d.ts.map +1 -0
  13. package/dist/dev/index.js +7 -0
  14. package/dist/dev/keypair.d.ts +24 -0
  15. package/dist/dev/keypair.d.ts.map +1 -0
  16. package/dist/dev/keypair.js +60 -0
  17. package/dist/internal/aiProviders.d.ts +67 -0
  18. package/dist/internal/aiProviders.d.ts.map +1 -0
  19. package/dist/internal/aiProviders.js +220 -0
  20. package/dist/internal/appSettings.d.ts +237 -0
  21. package/dist/internal/appSettings.d.ts.map +1 -0
  22. package/dist/internal/appSettings.js +374 -0
  23. package/dist/internal/capabilities.d.ts +1344 -0
  24. package/dist/internal/capabilities.d.ts.map +1 -0
  25. package/dist/internal/capabilities.js +1521 -0
  26. package/dist/internal/devProviders.d.ts +49 -0
  27. package/dist/internal/devProviders.d.ts.map +1 -0
  28. package/dist/internal/devProviders.js +62 -0
  29. package/dist/internal/email.d.ts +280 -0
  30. package/dist/internal/email.d.ts.map +1 -0
  31. package/dist/internal/email.js +613 -0
  32. package/dist/internal/events.d.ts +182 -0
  33. package/dist/internal/events.d.ts.map +1 -0
  34. package/dist/internal/events.js +324 -0
  35. package/dist/internal/gatewaySettings.d.ts +82 -0
  36. package/dist/internal/gatewaySettings.d.ts.map +1 -0
  37. package/dist/internal/gatewaySettings.js +209 -0
  38. package/dist/internal/identity.d.ts +18 -3
  39. package/dist/internal/identity.d.ts.map +1 -1
  40. package/dist/internal/identity.js +53 -14
  41. package/dist/internal/index.d.ts +17 -1
  42. package/dist/internal/index.d.ts.map +1 -1
  43. package/dist/internal/index.js +17 -1
  44. package/dist/internal/members.d.ts +35 -0
  45. package/dist/internal/members.d.ts.map +1 -0
  46. package/dist/internal/members.js +7 -0
  47. package/dist/internal/oauthClient.d.ts +63 -0
  48. package/dist/internal/oauthClient.d.ts.map +1 -0
  49. package/dist/internal/oauthClient.js +194 -0
  50. package/dist/internal/oauthScopes.d.ts +117 -0
  51. package/dist/internal/oauthScopes.d.ts.map +1 -0
  52. package/dist/internal/oauthScopes.js +416 -0
  53. package/dist/internal/organizationName.d.ts +18 -0
  54. package/dist/internal/organizationName.d.ts.map +1 -0
  55. package/dist/internal/organizationName.js +24 -0
  56. package/dist/internal/protocol.d.ts +86 -2
  57. package/dist/internal/protocol.d.ts.map +1 -1
  58. package/dist/internal/protocol.js +112 -1
  59. package/dist/internal/providerApps.d.ts +75 -0
  60. package/dist/internal/providerApps.d.ts.map +1 -0
  61. package/dist/internal/providerApps.js +59 -0
  62. package/dist/internal/providerRegistry.d.ts +147 -0
  63. package/dist/internal/providerRegistry.d.ts.map +1 -0
  64. package/dist/internal/providerRegistry.js +262 -0
  65. package/dist/internal/providerSpec.d.ts +55 -0
  66. package/dist/internal/providerSpec.d.ts.map +1 -0
  67. package/dist/internal/providerSpec.js +746 -0
  68. package/dist/internal/remoteMcp.d.ts +188 -0
  69. package/dist/internal/remoteMcp.d.ts.map +1 -0
  70. package/dist/internal/remoteMcp.js +543 -0
  71. package/dist/internal/tools.d.ts +115 -0
  72. package/dist/internal/tools.d.ts.map +1 -0
  73. package/dist/internal/tools.js +80 -0
  74. package/dist/server/attribution.d.ts +39 -0
  75. package/dist/server/attribution.d.ts.map +1 -0
  76. package/dist/server/attribution.js +67 -0
  77. package/dist/server/authorizeUrl.d.ts +43 -0
  78. package/dist/server/authorizeUrl.d.ts.map +1 -0
  79. package/dist/server/authorizeUrl.js +110 -0
  80. package/dist/server/emit.d.ts +63 -0
  81. package/dist/server/emit.d.ts.map +1 -0
  82. package/dist/server/emit.js +101 -0
  83. package/dist/server/everyApp.d.ts +38 -12
  84. package/dist/server/everyApp.d.ts.map +1 -1
  85. package/dist/server/everyApp.js +92 -28
  86. package/dist/server/gatewayFetch.d.ts +24 -8
  87. package/dist/server/gatewayFetch.d.ts.map +1 -1
  88. package/dist/server/gatewayFetch.js +81 -36
  89. package/dist/server/index.d.ts +10 -1
  90. package/dist/server/index.d.ts.map +1 -1
  91. package/dist/server/index.js +8 -1
  92. package/dist/server/integrationFetch.d.ts +131 -0
  93. package/dist/server/integrationFetch.d.ts.map +1 -0
  94. package/dist/server/integrationFetch.js +526 -0
  95. package/dist/server/listMembers.d.ts +32 -0
  96. package/dist/server/listMembers.d.ts.map +1 -0
  97. package/dist/server/listMembers.js +74 -0
  98. package/dist/server/nodeServer.d.ts +48 -0
  99. package/dist/server/nodeServer.d.ts.map +1 -0
  100. package/dist/server/nodeServer.js +45 -0
  101. package/dist/server/sendEmail.d.ts +39 -0
  102. package/dist/server/sendEmail.d.ts.map +1 -0
  103. package/dist/server/sendEmail.js +114 -0
  104. package/dist/server/tools.d.ts +184 -0
  105. package/dist/server/tools.d.ts.map +1 -0
  106. package/dist/server/tools.js +347 -0
  107. package/dist/server/toolsEndpoint.d.ts +32 -0
  108. package/dist/server/toolsEndpoint.d.ts.map +1 -0
  109. package/dist/server/toolsEndpoint.js +231 -0
  110. package/package.json +30 -17
  111. package/dist/server/mcp.d.ts +0 -66
  112. package/dist/server/mcp.d.ts.map +0 -1
  113. package/dist/server/mcp.js +0 -147
@@ -0,0 +1,746 @@
1
+ /**
2
+ * A provider spec: the shape `everyapp provider add` takes as JSON (and
3
+ * `everyapp_provider_add` as a tool argument), and the shape every
4
+ * built-in catalog entry has in TypeScript (./capabilities.ts), so a
5
+ * catalog entry is nothing a row couldn't be. `parseProviderSpec` turns
6
+ * JSON into an {@link ApiKeyCapability} or names every problem; nothing
7
+ * here reads a value: a spec names Gateway settings, never holds one.
8
+ *
9
+ * This slice parses API-key providers (`auth.kind` bearer, header, query
10
+ * or basic). An AI provider or an OAuth client is a catalog entry only;
11
+ * the parser says so rather than guessing.
12
+ *
13
+ * The rules that make a row safe on a particular gateway (its secret names
14
+ * are its own, its hosts are public, it shadows no catalog entry) are the
15
+ * registry's (./providerRegistry.ts), run after this parse.
16
+ */
17
+ import { DEFAULT_WRITE_METHODS, authSettingNames, } from "./capabilities.js";
18
+ /** A provider id: lowercase, a letter first, 2 to 40 characters. */
19
+ export const PROVIDER_ID = /^[a-z][a-z0-9-]{1,39}$/;
20
+ /** A Gateway setting name a provider may hold: upper case, 3 to 64 characters. */
21
+ export const PROVIDER_SECRET_NAME = /^[A-Z][A-Z0-9_]{2,63}$/;
22
+ /** A bare lowercase host with at least one dot: labels of letters, digits and hyphens. */
23
+ export const PROVIDER_HOST = /^(?=.{1,253}$)(?!-)[a-z0-9-]{1,63}(?<!-)(?:\.(?!-)[a-z0-9-]{1,63}(?<!-))+$/;
24
+ const METHODS = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"];
25
+ const SEGMENT = /^[a-z0-9_-]{1,64}$/;
26
+ const HEADER_NAME = /^[A-Za-z0-9-]{1,64}$/;
27
+ const QUERY_PARAM = /^[A-Za-z0-9_.-]{1,64}$/;
28
+ const SCHEME = /^[A-Za-z][A-Za-z0-9-]{0,31}$/;
29
+ const RESPONSE_FIELD = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
30
+ const DATE = /^\d{4}-\d{2}-\d{2}$/;
31
+ const TAG = /^[a-z][a-z0-9-]{0,31}$/;
32
+ /** Control and format characters: in no text a spec carries. */
33
+ const UNPRINTABLE = /[\p{Cc}\p{Cf}]/u;
34
+ /** Headers the proxy sets or strips itself: a spec can't put the key in one. */
35
+ const RESERVED_HEADERS = new Set([
36
+ "host",
37
+ "cookie",
38
+ "content-length",
39
+ "content-type",
40
+ "transfer-encoding",
41
+ "connection",
42
+ "upgrade",
43
+ "te",
44
+ "trailer",
45
+ "keep-alive",
46
+ "proxy-authorization",
47
+ "proxy-authenticate",
48
+ "forwarded",
49
+ "x-forwarded-for",
50
+ "x-forwarded-host",
51
+ "x-forwarded-proto",
52
+ ]);
53
+ const MAX_HOSTS = 20;
54
+ const MAX_ACCOUNT_WIDE_PATHS = 50;
55
+ const KNOWN_FIELDS = new Set([
56
+ "id",
57
+ "mode",
58
+ "name",
59
+ "description",
60
+ "docs",
61
+ "api",
62
+ "hosts",
63
+ "hostSetting",
64
+ "data",
65
+ "auth",
66
+ "secrets",
67
+ "access",
68
+ "readMethods",
69
+ "writeMethods",
70
+ "accountWidePaths",
71
+ "hint",
72
+ "smoke",
73
+ "cost",
74
+ "rate",
75
+ "example",
76
+ "verified",
77
+ "tags",
78
+ ]);
79
+ function isRecord(value) {
80
+ return typeof value === "object" && value !== null && !Array.isArray(value);
81
+ }
82
+ function text(value, path, problems, { max, min = 1 }) {
83
+ if (value === undefined)
84
+ return undefined;
85
+ if (typeof value !== "string") {
86
+ problems.push({ path, message: "must be a string" });
87
+ return undefined;
88
+ }
89
+ if (value.length < min || value.length > max) {
90
+ problems.push({
91
+ path,
92
+ message: `must be ${min} to ${max} characters`,
93
+ });
94
+ return undefined;
95
+ }
96
+ if (UNPRINTABLE.test(value)) {
97
+ problems.push({ path, message: "holds a control character" });
98
+ return undefined;
99
+ }
100
+ return value;
101
+ }
102
+ function httpsUrl(value, path, problems) {
103
+ const url = text(value, path, problems, { max: 500 });
104
+ if (url === undefined)
105
+ return undefined;
106
+ let parsed;
107
+ try {
108
+ parsed = new URL(url);
109
+ }
110
+ catch {
111
+ problems.push({ path, message: "must be an https URL" });
112
+ return undefined;
113
+ }
114
+ if (parsed.protocol !== "https:") {
115
+ problems.push({ path, message: "must be an https URL" });
116
+ return undefined;
117
+ }
118
+ return url;
119
+ }
120
+ function stringList(value, path, problems, { max, each }) {
121
+ if (value === undefined)
122
+ return undefined;
123
+ if (!Array.isArray(value)) {
124
+ problems.push({ path, message: "must be a list" });
125
+ return undefined;
126
+ }
127
+ if (value.length > max) {
128
+ problems.push({ path, message: `at most ${max} entries` });
129
+ return undefined;
130
+ }
131
+ const out = [];
132
+ value.forEach((item, index) => {
133
+ if (typeof item !== "string") {
134
+ problems.push({ path: `${path}.${index}`, message: "must be a string" });
135
+ return;
136
+ }
137
+ each(item, `${path}.${index}`);
138
+ out.push(item);
139
+ });
140
+ if (new Set(out).size !== out.length) {
141
+ problems.push({ path, message: "lists an entry twice" });
142
+ }
143
+ return out;
144
+ }
145
+ function methods(value, path, problems) {
146
+ return stringList(value, path, problems, {
147
+ max: METHODS.length,
148
+ each: (method, at) => {
149
+ if (!METHODS.includes(method)) {
150
+ problems.push({
151
+ path: at,
152
+ message: `must be one of ${METHODS.join(", ")}`,
153
+ });
154
+ }
155
+ },
156
+ });
157
+ }
158
+ function secretName(value, path, problems) {
159
+ if (typeof value !== "string" || !PROVIDER_SECRET_NAME.test(value)) {
160
+ problems.push({
161
+ path,
162
+ message: "must name a Gateway setting: upper case letters, digits and underscores, a letter first, 3 to 64 characters (STRIPE_API_KEY)",
163
+ });
164
+ return undefined;
165
+ }
166
+ return value;
167
+ }
168
+ function auth(value, problems) {
169
+ if (!isRecord(value)) {
170
+ problems.push({
171
+ path: "auth",
172
+ message: "must be an object with a kind: bearer, header, query or basic",
173
+ });
174
+ return undefined;
175
+ }
176
+ const kind = value["kind"];
177
+ const secret = secretName(value["secret"], "auth.secret", problems);
178
+ const known = (fields) => {
179
+ for (const field of Object.keys(value)) {
180
+ if (!fields.includes(field)) {
181
+ problems.push({
182
+ path: `auth.${field}`,
183
+ message: `not a field of auth.kind ${String(kind)}`,
184
+ });
185
+ }
186
+ }
187
+ };
188
+ switch (kind) {
189
+ case "bearer": {
190
+ known(["kind", "secret", "scheme"]);
191
+ const scheme = text(value["scheme"], "auth.scheme", problems, {
192
+ max: 32,
193
+ });
194
+ if (scheme !== undefined && !SCHEME.test(scheme)) {
195
+ problems.push({
196
+ path: "auth.scheme",
197
+ message: "must be a token such as Bearer, Token or Api-Key",
198
+ });
199
+ }
200
+ if (!secret)
201
+ return undefined;
202
+ return { kind, secret, ...(scheme === undefined ? {} : { scheme }) };
203
+ }
204
+ case "header": {
205
+ known(["kind", "secret", "header", "prefix"]);
206
+ const header = text(value["header"], "auth.header", problems, {
207
+ max: 64,
208
+ });
209
+ if (header !== undefined) {
210
+ if (!HEADER_NAME.test(header)) {
211
+ problems.push({
212
+ path: "auth.header",
213
+ message: "must be a header name (letters, digits, hyphens)",
214
+ });
215
+ }
216
+ else if (RESERVED_HEADERS.has(header.toLowerCase()) ||
217
+ header.toLowerCase().startsWith("x-everyapp-")) {
218
+ problems.push({
219
+ path: "auth.header",
220
+ message: `${header} is a header the Gateway sets or strips itself`,
221
+ });
222
+ }
223
+ }
224
+ const prefix = text(value["prefix"], "auth.prefix", problems, {
225
+ max: 32,
226
+ min: 0,
227
+ });
228
+ if (!secret || header === undefined)
229
+ return undefined;
230
+ return {
231
+ kind,
232
+ secret,
233
+ header,
234
+ ...(prefix === undefined ? {} : { prefix }),
235
+ };
236
+ }
237
+ case "query": {
238
+ known(["kind", "secret", "param"]);
239
+ const param = text(value["param"], "auth.param", problems, { max: 64 });
240
+ if (param !== undefined && !QUERY_PARAM.test(param)) {
241
+ problems.push({
242
+ path: "auth.param",
243
+ message: "must be a query parameter name",
244
+ });
245
+ }
246
+ if (!secret || param === undefined)
247
+ return undefined;
248
+ return { kind, secret, param };
249
+ }
250
+ case "basic": {
251
+ known(["kind", "secret", "as", "user"]);
252
+ const as = value["as"];
253
+ if (as !== "password" && as !== "username" && as !== "pair") {
254
+ problems.push({
255
+ path: "auth.as",
256
+ message: 'must say where the key goes: "password" (beside user), "username" (with an empty password), or "pair" (the key is login:password)',
257
+ });
258
+ return undefined;
259
+ }
260
+ if (as !== "password") {
261
+ if (value["user"] !== undefined) {
262
+ problems.push({
263
+ path: "auth.user",
264
+ message: `not a field of auth.as ${as}`,
265
+ });
266
+ }
267
+ if (!secret)
268
+ return undefined;
269
+ return { kind, secret, as };
270
+ }
271
+ const raw = value["user"];
272
+ let user;
273
+ if (typeof raw === "string") {
274
+ const literal = text(raw, "auth.user", problems, { max: 128 });
275
+ if (literal !== undefined && literal.includes(":")) {
276
+ problems.push({ path: "auth.user", message: "may not hold a colon" });
277
+ }
278
+ user = literal;
279
+ }
280
+ else if (isRecord(raw) && Object.keys(raw).join() === "secret") {
281
+ const named = secretName(raw["secret"], "auth.user.secret", problems);
282
+ if (named !== undefined && named === secret) {
283
+ problems.push({
284
+ path: "auth.user.secret",
285
+ message: "must be another setting than auth.secret",
286
+ });
287
+ }
288
+ user = named === undefined ? undefined : { secret: named };
289
+ }
290
+ else {
291
+ problems.push({
292
+ path: "auth.user",
293
+ message: "required with as: password: a string, or { secret: NAME }",
294
+ });
295
+ }
296
+ if (!secret || user === undefined)
297
+ return undefined;
298
+ return { kind, secret, as, user };
299
+ }
300
+ default:
301
+ problems.push({
302
+ path: "auth.kind",
303
+ message: "must be bearer, header, query or basic (an OAuth client or an AI provider is a catalog entry, not a spec this gateway takes)",
304
+ });
305
+ return undefined;
306
+ }
307
+ }
308
+ function secretDetails(value, names, problems) {
309
+ if (value === undefined)
310
+ return undefined;
311
+ if (!Array.isArray(value)) {
312
+ problems.push({ path: "secrets", message: "must be a list" });
313
+ return undefined;
314
+ }
315
+ const out = [];
316
+ value.forEach((item, index) => {
317
+ const at = `secrets.${index}`;
318
+ if (!isRecord(item)) {
319
+ problems.push({ path: at, message: "must be an object" });
320
+ return;
321
+ }
322
+ for (const field of Object.keys(item)) {
323
+ if (!["name", "hint"].includes(field)) {
324
+ problems.push({ path: `${at}.${field}`, message: "unknown field" });
325
+ }
326
+ }
327
+ const name = secretName(item["name"], `${at}.name`, problems);
328
+ if (name !== undefined && !names.includes(name)) {
329
+ problems.push({
330
+ path: `${at}.name`,
331
+ message: `auth names no setting ${name}`,
332
+ });
333
+ }
334
+ const hint = text(item["hint"], `${at}.hint`, problems, { max: 300 });
335
+ if (name === undefined)
336
+ return;
337
+ out.push({ name, ...(hint === undefined ? {} : { hint }) });
338
+ });
339
+ if (new Set(out.map((detail) => detail.name)).size !== out.length) {
340
+ problems.push({ path: "secrets", message: "names a setting twice" });
341
+ }
342
+ return out;
343
+ }
344
+ function accountWidePaths(value, problems) {
345
+ if (value === undefined)
346
+ return undefined;
347
+ if (!Array.isArray(value)) {
348
+ problems.push({
349
+ path: "accountWidePaths",
350
+ message: 'must be a list of segment runs: [["v1", "account"]]',
351
+ });
352
+ return undefined;
353
+ }
354
+ if (value.length > MAX_ACCOUNT_WIDE_PATHS) {
355
+ problems.push({
356
+ path: "accountWidePaths",
357
+ message: `at most ${MAX_ACCOUNT_WIDE_PATHS} entries`,
358
+ });
359
+ return undefined;
360
+ }
361
+ const out = [];
362
+ value.forEach((run, index) => {
363
+ const at = `accountWidePaths.${index}`;
364
+ if (!Array.isArray(run) || run.length === 0) {
365
+ problems.push({
366
+ path: at,
367
+ message: "must be a non-empty list of segments",
368
+ });
369
+ return;
370
+ }
371
+ const segments = [];
372
+ run.forEach((segment, offset) => {
373
+ if (typeof segment !== "string" || !SEGMENT.test(segment)) {
374
+ problems.push({
375
+ path: `${at}.${offset}`,
376
+ message: "must be a lowercase path segment (letters, digits, _ and -)",
377
+ });
378
+ return;
379
+ }
380
+ segments.push(segment);
381
+ });
382
+ if (segments.length === run.length)
383
+ out.push(segments);
384
+ });
385
+ return out;
386
+ }
387
+ function smoke(value, hosts, problems) {
388
+ if (value === undefined)
389
+ return undefined;
390
+ if (!isRecord(value)) {
391
+ problems.push({ path: "smoke", message: "must be an object" });
392
+ return undefined;
393
+ }
394
+ for (const field of Object.keys(value)) {
395
+ if (!["method", "host", "path", "expect", "free", "note"].includes(field)) {
396
+ problems.push({ path: `smoke.${field}`, message: "unknown field" });
397
+ }
398
+ }
399
+ const method = value["method"];
400
+ if (method !== "GET" && method !== "HEAD" && method !== "POST") {
401
+ problems.push({
402
+ path: "smoke.method",
403
+ message: "must be GET, HEAD or POST",
404
+ });
405
+ }
406
+ const host = text(value["host"], "smoke.host", problems, { max: 253 });
407
+ if (host !== undefined && !hosts.includes(host)) {
408
+ problems.push({ path: "smoke.host", message: "must be one of hosts" });
409
+ }
410
+ const path = text(value["path"], "smoke.path", problems, { max: 500 });
411
+ if (path !== undefined && (!path.startsWith("/") || path.startsWith("//"))) {
412
+ problems.push({ path: "smoke.path", message: "must start with one /" });
413
+ }
414
+ const expect = value["expect"];
415
+ const statuses = Array.isArray(expect) &&
416
+ expect.length > 0 &&
417
+ expect.every((status) => Number.isInteger(status) &&
418
+ status >= 100 &&
419
+ status <= 599)
420
+ ? expect
421
+ : undefined;
422
+ if (!statuses) {
423
+ problems.push({
424
+ path: "smoke.expect",
425
+ message: "must list the HTTP statuses that mean the key works",
426
+ });
427
+ }
428
+ const free = value["free"];
429
+ if (typeof free !== "boolean") {
430
+ problems.push({ path: "smoke.free", message: "must be true or false" });
431
+ }
432
+ const note = text(value["note"], "smoke.note", problems, { max: 200 });
433
+ if ((method !== "GET" && method !== "HEAD" && method !== "POST") ||
434
+ path === undefined ||
435
+ !statuses ||
436
+ typeof free !== "boolean") {
437
+ return undefined;
438
+ }
439
+ return {
440
+ method,
441
+ ...(host === undefined ? {} : { host }),
442
+ path,
443
+ expect: statuses,
444
+ free,
445
+ ...(note === undefined ? {} : { note }),
446
+ };
447
+ }
448
+ function nonNegative(value, path, problems) {
449
+ if (value === undefined)
450
+ return undefined;
451
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
452
+ problems.push({ path, message: "must be a number, zero or more" });
453
+ return undefined;
454
+ }
455
+ return value;
456
+ }
457
+ function cost(value, problems) {
458
+ if (value === undefined)
459
+ return undefined;
460
+ if (!isRecord(value)) {
461
+ problems.push({ path: "cost", message: "must be an object" });
462
+ return undefined;
463
+ }
464
+ for (const field of Object.keys(value)) {
465
+ if (!["unit", "usd", "note", "responseField"].includes(field)) {
466
+ problems.push({ path: `cost.${field}`, message: "unknown field" });
467
+ }
468
+ }
469
+ const unit = value["unit"];
470
+ if (unit !== "call" && unit !== "result" && unit !== "token") {
471
+ problems.push({
472
+ path: "cost.unit",
473
+ message: "must be call, result or token",
474
+ });
475
+ }
476
+ const usd = nonNegative(value["usd"], "cost.usd", problems);
477
+ const note = text(value["note"], "cost.note", problems, { max: 200 });
478
+ const responseField = text(value["responseField"], "cost.responseField", problems, { max: 64 });
479
+ if (responseField !== undefined && !RESPONSE_FIELD.test(responseField)) {
480
+ problems.push({
481
+ path: "cost.responseField",
482
+ message: "must be a JSON field name",
483
+ });
484
+ }
485
+ if (unit !== "call" && unit !== "result" && unit !== "token")
486
+ return undefined;
487
+ return {
488
+ unit,
489
+ ...(usd === undefined ? {} : { usd }),
490
+ ...(note === undefined ? {} : { note }),
491
+ ...(responseField === undefined ? {} : { responseField }),
492
+ };
493
+ }
494
+ function rate(value, problems) {
495
+ if (value === undefined)
496
+ return undefined;
497
+ if (!isRecord(value)) {
498
+ problems.push({ path: "rate", message: "must be an object" });
499
+ return undefined;
500
+ }
501
+ for (const field of Object.keys(value)) {
502
+ if (!["perSecond", "perMinute", "perDay", "note"].includes(field)) {
503
+ problems.push({ path: `rate.${field}`, message: "unknown field" });
504
+ }
505
+ }
506
+ const perSecond = nonNegative(value["perSecond"], "rate.perSecond", problems);
507
+ const perMinute = nonNegative(value["perMinute"], "rate.perMinute", problems);
508
+ const perDay = nonNegative(value["perDay"], "rate.perDay", problems);
509
+ const note = text(value["note"], "rate.note", problems, { max: 200 });
510
+ return {
511
+ ...(perSecond === undefined ? {} : { perSecond }),
512
+ ...(perMinute === undefined ? {} : { perMinute }),
513
+ ...(perDay === undefined ? {} : { perDay }),
514
+ ...(note === undefined ? {} : { note }),
515
+ };
516
+ }
517
+ function verified(value, problems) {
518
+ if (value === undefined)
519
+ return undefined;
520
+ if (value === false)
521
+ return false;
522
+ if (isRecord(value) &&
523
+ typeof value["on"] === "string" &&
524
+ DATE.test(value["on"]) &&
525
+ (value["how"] === "docs" || value["how"] === "live") &&
526
+ Object.keys(value).length === 2) {
527
+ return { on: value["on"], how: value["how"] };
528
+ }
529
+ problems.push({
530
+ path: "verified",
531
+ message: 'must be false, or { "on": "YYYY-MM-DD", "how": "docs" | "live" }',
532
+ });
533
+ return undefined;
534
+ }
535
+ /**
536
+ * Parse a provider spec. Every problem is reported, with its path, so a
537
+ * person fixes the file once. On success the id and the catalog-shaped
538
+ * capability, defaults filled in (`access` read, `readMethods` GET and
539
+ * HEAD, no account-wide paths).
540
+ */
541
+ export function parseProviderSpec(input) {
542
+ const problems = [];
543
+ if (!isRecord(input)) {
544
+ return {
545
+ ok: false,
546
+ problems: [{ path: "(root)", message: "must be a JSON object" }],
547
+ };
548
+ }
549
+ for (const field of Object.keys(input)) {
550
+ if (!KNOWN_FIELDS.has(field)) {
551
+ problems.push({ path: field, message: "unknown field" });
552
+ }
553
+ }
554
+ const id = input["id"];
555
+ if (typeof id !== "string" || !PROVIDER_ID.test(id)) {
556
+ problems.push({
557
+ path: "id",
558
+ message: "must be lowercase letters, digits and hyphens, a letter first, 2 to 40 characters (stripe)",
559
+ });
560
+ }
561
+ if (input["mode"] !== undefined && input["mode"] !== "api_key") {
562
+ problems.push({
563
+ path: "mode",
564
+ message: "this gateway registers api_key providers; an AI provider or an OAuth client is a catalog entry",
565
+ });
566
+ }
567
+ const name = text(input["name"], "name", problems, { max: 64 });
568
+ if (input["name"] === undefined) {
569
+ problems.push({ path: "name", message: "required" });
570
+ }
571
+ const description = text(input["description"], "description", problems, {
572
+ max: 200,
573
+ });
574
+ const docs = httpsUrl(input["docs"], "docs", problems);
575
+ if (input["docs"] === undefined) {
576
+ problems.push({
577
+ path: "docs",
578
+ message: "required: where the person gets the key, an https URL",
579
+ });
580
+ }
581
+ const api = httpsUrl(input["api"], "api", problems);
582
+ const hosts = stringList(input["hosts"], "hosts", problems, {
583
+ max: MAX_HOSTS,
584
+ each: (host, at) => {
585
+ if (!PROVIDER_HOST.test(host)) {
586
+ problems.push({
587
+ path: at,
588
+ message: "must be a bare lowercase host with a dot and no port, path or scheme (api.stripe.com)",
589
+ });
590
+ }
591
+ },
592
+ });
593
+ const hostSetting = input["hostSetting"] === undefined
594
+ ? undefined
595
+ : secretName(input["hostSetting"], "hostSetting", problems);
596
+ if (hostSetting === undefined &&
597
+ (input["hosts"] === undefined || (hosts && hosts.length === 0))) {
598
+ problems.push({
599
+ path: "hosts",
600
+ message: "required: at least one host, or a hostSetting naming one",
601
+ });
602
+ }
603
+ const rawData = input["data"];
604
+ if (rawData !== undefined &&
605
+ rawData !== "per-call" &&
606
+ rawData !== "account") {
607
+ problems.push({ path: "data", message: "must be per-call or account" });
608
+ }
609
+ const data = rawData === "per-call" || rawData === "account" ? rawData : undefined;
610
+ const parsedAuth = auth(input["auth"], problems);
611
+ if (input["auth"] === undefined) {
612
+ problems.push({ path: "auth", message: "required" });
613
+ }
614
+ if (hostSetting !== undefined &&
615
+ parsedAuth &&
616
+ authSettingNames(parsedAuth).includes(hostSetting)) {
617
+ problems.push({
618
+ path: "hostSetting",
619
+ message: "must be another setting than the key",
620
+ });
621
+ }
622
+ const secrets = secretDetails(input["secrets"], [
623
+ ...(parsedAuth ? authSettingNames(parsedAuth) : []),
624
+ ...(hostSetting === undefined ? [] : [hostSetting]),
625
+ ], problems);
626
+ const access = stringList(input["access"], "access", problems, {
627
+ max: 2,
628
+ each: (level, at) => {
629
+ if (level !== "read" && level !== "write") {
630
+ problems.push({ path: at, message: "must be read or write" });
631
+ }
632
+ },
633
+ });
634
+ if (access && access.length === 0) {
635
+ problems.push({ path: "access", message: "must list read, write or both" });
636
+ }
637
+ const readMethods = methods(input["readMethods"], "readMethods", problems);
638
+ if (readMethods && readMethods.length === 0) {
639
+ problems.push({ path: "readMethods", message: "must list a method" });
640
+ }
641
+ const writeMethods = methods(input["writeMethods"], "writeMethods", problems);
642
+ const paths = accountWidePaths(input["accountWidePaths"], problems);
643
+ const hint = text(input["hint"], "hint", problems, { max: 500 });
644
+ const smokeRequest = smoke(input["smoke"], hosts ?? [], problems);
645
+ const parsedCost = cost(input["cost"], problems);
646
+ const parsedRate = rate(input["rate"], problems);
647
+ const example = text(input["example"], "example", problems, { max: 200 });
648
+ const parsedVerified = verified(input["verified"], problems);
649
+ const tags = stringList(input["tags"], "tags", problems, {
650
+ max: 10,
651
+ each: (tag, at) => {
652
+ if (!TAG.test(tag)) {
653
+ problems.push({ path: at, message: "must be a lowercase word" });
654
+ }
655
+ },
656
+ });
657
+ if (problems.length > 0 ||
658
+ typeof id !== "string" ||
659
+ name === undefined ||
660
+ docs === undefined ||
661
+ (!hosts && hostSetting === undefined) ||
662
+ !parsedAuth) {
663
+ return { ok: false, problems };
664
+ }
665
+ const capability = {
666
+ mode: "api_key",
667
+ label: name,
668
+ auth: parsedAuth,
669
+ docs,
670
+ hosts: hosts ?? [],
671
+ ...(hostSetting === undefined ? {} : { hostSetting }),
672
+ ...(data === undefined ? {} : { data }),
673
+ access: access ?? ["read"],
674
+ readMethods: readMethods ?? ["GET", "HEAD"],
675
+ accountWidePaths: paths ?? [],
676
+ ...(description === undefined ? {} : { description }),
677
+ ...(api === undefined ? {} : { api }),
678
+ ...(secrets === undefined ? {} : { secrets }),
679
+ ...(writeMethods === undefined ? {} : { writeMethods }),
680
+ ...(hint === undefined ? {} : { hint }),
681
+ ...(smokeRequest === undefined ? {} : { smoke: smokeRequest }),
682
+ ...(parsedCost === undefined ? {} : { cost: parsedCost }),
683
+ ...(parsedRate === undefined ? {} : { rate: parsedRate }),
684
+ ...(example === undefined ? {} : { example }),
685
+ ...(parsedVerified === undefined ? {} : { verified: parsedVerified }),
686
+ ...(tags === undefined ? {} : { tags }),
687
+ };
688
+ return { ok: true, id, capability };
689
+ }
690
+ /** The problems as one line: `auth.secret: must name a Gateway setting; hosts.0: …`. */
691
+ export function describeSpecProblems(problems) {
692
+ return problems
693
+ .map((problem) => `${problem.path}: ${problem.message}`)
694
+ .join("; ");
695
+ }
696
+ /**
697
+ * The spec of an API-key capability as JSON (`provider show`, the
698
+ * catalog's round trip): the entry's fields in the spec's names, with the
699
+ * defaults the parser fills in spelled out, so `parseProviderSpec` of it
700
+ * gives the entry back.
701
+ */
702
+ export function providerSpecOf(id, capability) {
703
+ return {
704
+ id,
705
+ name: capability.label,
706
+ ...(capability.description === undefined
707
+ ? {}
708
+ : { description: capability.description }),
709
+ docs: capability.docs,
710
+ ...(capability.api === undefined ? {} : { api: capability.api }),
711
+ hosts: [...capability.hosts],
712
+ ...(capability.hostSetting === undefined
713
+ ? {}
714
+ : { hostSetting: capability.hostSetting }),
715
+ ...(capability.data === undefined ? {} : { data: capability.data }),
716
+ auth: { ...capability.auth },
717
+ ...(capability.secrets === undefined
718
+ ? {}
719
+ : { secrets: capability.secrets.map((detail) => ({ ...detail })) }),
720
+ access: [...capability.access],
721
+ readMethods: [...capability.readMethods],
722
+ ...(capability.writeMethods === undefined
723
+ ? {}
724
+ : { writeMethods: [...capability.writeMethods] }),
725
+ accountWidePaths: capability.accountWidePaths.map((run) => [...run]),
726
+ ...(capability.hint === undefined ? {} : { hint: capability.hint }),
727
+ ...(capability.smoke === undefined
728
+ ? {}
729
+ : {
730
+ smoke: { ...capability.smoke, expect: [...capability.smoke.expect] },
731
+ }),
732
+ ...(capability.cost === undefined ? {} : { cost: { ...capability.cost } }),
733
+ ...(capability.rate === undefined ? {} : { rate: { ...capability.rate } }),
734
+ ...(capability.example === undefined
735
+ ? {}
736
+ : { example: capability.example }),
737
+ ...(capability.verified === undefined
738
+ ? {}
739
+ : { verified: capability.verified }),
740
+ ...(capability.tags === undefined ? {} : { tags: [...capability.tags] }),
741
+ };
742
+ }
743
+ /** The methods an app with `access: "write"` may use on `capability` besides its read methods. */
744
+ export function writeMethodsOf(capability) {
745
+ return capability.writeMethods ?? DEFAULT_WRITE_METHODS;
746
+ }