@every-app/sdk 0.3.0-rc.10 → 0.3.0-rc.12
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.
- package/README.md +67 -67
- package/dist/cloudflare/catalogSettings.generated.d.ts +7 -4
- package/dist/cloudflare/catalogSettings.generated.d.ts.map +1 -1
- package/dist/cloudflare/catalogSettings.generated.js +7 -6
- package/dist/cloudflare/index.d.ts +1 -1
- package/dist/cloudflare/index.d.ts.map +1 -1
- package/dist/cloudflare/worker.d.ts +36 -2
- package/dist/cloudflare/worker.d.ts.map +1 -1
- package/dist/cloudflare/worker.js +32 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/internal/aiDefault.d.ts +36 -79
- package/dist/internal/aiDefault.d.ts.map +1 -1
- package/dist/internal/aiDefault.js +47 -149
- package/dist/internal/aiProviders.d.ts.map +1 -1
- package/dist/internal/aiProviders.js +2 -2
- package/dist/internal/appSettings.d.ts +14 -17
- package/dist/internal/appSettings.d.ts.map +1 -1
- package/dist/internal/appSettings.js +2 -16
- package/dist/internal/capabilities.d.ts +106 -489
- package/dist/internal/capabilities.d.ts.map +1 -1
- package/dist/internal/capabilities.js +49 -550
- package/dist/internal/catalog/commerce.js +1 -1
- package/dist/internal/catalog/index.d.ts +35 -0
- package/dist/internal/catalog/index.d.ts.map +1 -1
- package/dist/internal/catalog/infra.d.ts +35 -0
- package/dist/internal/catalog/infra.d.ts.map +1 -1
- package/dist/internal/catalog/infra.js +65 -0
- package/dist/internal/events.d.ts +52 -1
- package/dist/internal/events.d.ts.map +1 -1
- package/dist/internal/events.js +65 -3
- package/dist/internal/gatewaySettings.d.ts +2 -2
- package/dist/internal/gatewaySettings.d.ts.map +1 -1
- package/dist/internal/gatewaySettings.js +0 -6
- package/dist/internal/oauthBuiltIns.d.ts +101 -0
- package/dist/internal/oauthBuiltIns.d.ts.map +1 -0
- package/dist/internal/oauthBuiltIns.js +122 -0
- package/dist/internal/oauthClient.d.ts +7 -47
- package/dist/internal/oauthClient.d.ts.map +1 -1
- package/dist/internal/oauthClient.js +1 -115
- package/dist/internal/oauthPresets.d.ts +14 -15
- package/dist/internal/oauthPresets.d.ts.map +1 -1
- package/dist/internal/oauthPresets.js +42 -853
- package/dist/internal/oauthScopes.d.ts +43 -155
- package/dist/internal/oauthScopes.d.ts.map +1 -1
- package/dist/internal/oauthScopes.js +82 -475
- package/dist/internal/oauthSpec.d.ts +72 -71
- package/dist/internal/oauthSpec.d.ts.map +1 -1
- package/dist/internal/oauthSpec.js +178 -447
- package/dist/internal/protocol.d.ts +24 -14
- package/dist/internal/protocol.d.ts.map +1 -1
- package/dist/internal/protocol.js +26 -7
- package/dist/internal/providerRegistry.d.ts +1 -61
- package/dist/internal/providerRegistry.d.ts.map +1 -1
- package/dist/internal/providerRegistry.js +8 -115
- package/dist/internal/providerSpec.d.ts.map +1 -1
- package/dist/internal/providerSpec.js +14 -36
- package/dist/internal/remoteMcp.d.ts +4 -6
- package/dist/internal/remoteMcp.d.ts.map +1 -1
- package/dist/internal/remoteMcp.js +8 -15
- package/dist/internal/tools.d.ts +12 -7
- package/dist/internal/tools.d.ts.map +1 -1
- package/dist/internal/tools.js +9 -8
- package/dist/manifest.d.ts +41 -28
- package/dist/manifest.d.ts.map +1 -1
- package/dist/manifest.js +1 -1
- package/dist/server/ai.d.ts +1 -93
- package/dist/server/ai.d.ts.map +1 -1
- package/dist/server/ai.js +17 -111
- package/dist/server/attribution.d.ts +10 -1
- package/dist/server/attribution.d.ts.map +1 -1
- package/dist/server/attribution.js +7 -1
- package/dist/server/connectUrl.d.ts +4 -9
- package/dist/server/connectUrl.d.ts.map +1 -1
- package/dist/server/connectUrl.js +17 -14
- package/dist/server/context.d.ts +12 -14
- package/dist/server/context.d.ts.map +1 -1
- package/dist/server/context.js +23 -25
- package/dist/server/eventsEndpoint.d.ts +48 -0
- package/dist/server/eventsEndpoint.d.ts.map +1 -0
- package/dist/server/eventsEndpoint.js +145 -0
- package/dist/server/everyApp.d.ts +16 -26
- package/dist/server/everyApp.d.ts.map +1 -1
- package/dist/server/everyApp.js +22 -46
- package/dist/server/gatewayFetch.d.ts +1 -1
- package/dist/server/gatewayFetch.js +1 -1
- package/dist/server/index.d.ts +6 -9
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -8
- package/dist/server/integrationFetch.d.ts +60 -72
- package/dist/server/integrationFetch.d.ts.map +1 -1
- package/dist/server/integrationFetch.js +117 -392
- package/dist/server/refusal.d.ts +0 -9
- package/dist/server/refusal.d.ts.map +1 -1
- package/dist/server/refusal.js +0 -22
- package/dist/server/tools.d.ts +13 -5
- package/dist/server/tools.d.ts.map +1 -1
- package/dist/server/tools.js +10 -3
- package/dist/server/toolsEndpoint.d.ts +2 -1
- package/dist/server/toolsEndpoint.d.ts.map +1 -1
- package/dist/server/toolsEndpoint.js +4 -3
- package/dist/server/webhooks.d.ts +56 -0
- package/dist/server/webhooks.d.ts.map +1 -0
- package/dist/server/webhooks.js +205 -0
- package/dist/testing/index.d.ts +2 -3
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +2 -6
- package/package.json +1 -1
- package/dist/server/aiBinding.d.ts +0 -68
- package/dist/server/aiBinding.d.ts.map +0 -1
- package/dist/server/aiBinding.js +0 -294
- package/dist/server/authorizeUrl.d.ts +0 -43
- package/dist/server/authorizeUrl.d.ts.map +0 -1
- package/dist/server/authorizeUrl.js +0 -106
- package/dist/server/emailBinding.d.ts +0 -72
- package/dist/server/emailBinding.d.ts.map +0 -1
- package/dist/server/emailBinding.js +0 -124
- package/dist/server/fromMcpTools.d.ts +0 -89
- package/dist/server/fromMcpTools.d.ts.map +0 -1
- package/dist/server/fromMcpTools.js +0 -192
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refusal.d.ts","sourceRoot":"","sources":["../../src/server/refusal.ts"],"names":[],"mappings":"AAgBA,wDAAwD;AACxD,MAAM,WAAW,SAAS;IACxB,kG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AAG,MAAM,GAAG,SAAS,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC;CAS5E;AAED;;;;GAIG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,QAAQ,GACjB,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAKhC;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,cAAc,GAAG,IAAI,CAWtE"}
|
package/dist/server/refusal.js
CHANGED
|
@@ -13,11 +13,6 @@
|
|
|
13
13
|
* match the message text or hardcode a setting name or a command.
|
|
14
14
|
*/
|
|
15
15
|
import { ConfigurationError, GATEWAY_ERROR_HEADER } from "../internal/index.js";
|
|
16
|
-
/** The refusal codes that mean no model is set up for the call. */
|
|
17
|
-
export const AI_NOT_CONFIGURED_CODES = [
|
|
18
|
-
"ai_provider_not_configured",
|
|
19
|
-
"ai_default_not_configured",
|
|
20
|
-
];
|
|
21
16
|
/**
|
|
22
17
|
* A refusal the AppGateway binding made, thrown: what `sendEmail()` throws
|
|
23
18
|
* when the gateway won't send (`email_sender_not_allowed`,
|
|
@@ -73,23 +68,6 @@ export function refusalFromError(error) {
|
|
|
73
68
|
}
|
|
74
69
|
return null;
|
|
75
70
|
}
|
|
76
|
-
/**
|
|
77
|
-
* Whether `error` (or a refusal already read) means no model is set up:
|
|
78
|
-
* `ai_provider_not_configured` or `ai_default_not_configured`, from the
|
|
79
|
-
* Gateway or, in local development, from the SDK with no key in
|
|
80
|
-
* `.dev.vars`. Show `refusalFromError(error).setup` to the person.
|
|
81
|
-
*/
|
|
82
|
-
export function isAiNotConfigured(error) {
|
|
83
|
-
const refusal = isRefusal(error) ? error : refusalFromError(error);
|
|
84
|
-
return refusal !== null && AI_NOT_CONFIGURED_CODES.includes(refusal.code);
|
|
85
|
-
}
|
|
86
|
-
function isRefusal(value) {
|
|
87
|
-
return (!!value &&
|
|
88
|
-
typeof value === "object" &&
|
|
89
|
-
!(value instanceof Error) &&
|
|
90
|
-
typeof value.code === "string" &&
|
|
91
|
-
typeof value.status === "number");
|
|
92
|
-
}
|
|
93
71
|
function refusalHere(error) {
|
|
94
72
|
// A Response is read with gatewayRefusal(), which can read its body.
|
|
95
73
|
if (error instanceof Response)
|
package/dist/server/tools.d.ts
CHANGED
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
*
|
|
18
18
|
* Apps never speak MCP. The gateway snapshots this catalog at deploy, serves
|
|
19
19
|
* MCP clients from the snapshot, and calls a tool over the internal tools
|
|
20
|
-
* contract (../internal/tools.ts) with a verified `chan: "mcp"`
|
|
20
|
+
* contract (../internal/tools.ts) with a verified `chan: "mcp"`
|
|
21
|
+
* identity.
|
|
21
22
|
*
|
|
22
23
|
* `input` and `output` accept any validator implementing Standard Schema and
|
|
23
24
|
* Standard JSON Schema (zod >= 4.2 does both), or a plain
|
|
@@ -81,7 +82,10 @@ export interface PlainToolSchema<Output = unknown> {
|
|
|
81
82
|
export type ToolSchema<Output = unknown> = StandardSchemaWithJSON<unknown, Output> | PlainToolSchema<Output>;
|
|
82
83
|
/** What a tool handler receives besides its validated input. */
|
|
83
84
|
export interface ToolContext<TEnv = Cloudflare.Env> {
|
|
84
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* The verified caller. `channel` is "mcp" (an AI client); `toolCall`
|
|
87
|
+
* names this call.
|
|
88
|
+
*/
|
|
85
89
|
user: EveryAppUser;
|
|
86
90
|
env: TEnv;
|
|
87
91
|
ctx: ExecutionContextLike;
|
|
@@ -95,11 +99,15 @@ export interface ToolDefinition<TInput = unknown> {
|
|
|
95
99
|
/** What the tool does and when to use it. The model reads this. */
|
|
96
100
|
description: string;
|
|
97
101
|
/**
|
|
98
|
-
* `read` changes nothing
|
|
99
|
-
*
|
|
100
|
-
* destructive tools until approvals exist.
|
|
102
|
+
* `read` changes nothing; read-only connections and tokens may call it.
|
|
103
|
+
* `write` creates, changes or deletes data.
|
|
101
104
|
*/
|
|
102
105
|
risk: ToolRisk;
|
|
106
|
+
/**
|
|
107
|
+
* A write tool that deletes or can't be undone. Clients see
|
|
108
|
+
* `destructiveHint: true` and ask the person before calling it.
|
|
109
|
+
*/
|
|
110
|
+
destructive?: boolean;
|
|
103
111
|
/** Arguments schema; must describe an object. */
|
|
104
112
|
input: ToolSchema<TInput>;
|
|
105
113
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/server/tools.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/server/tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,EAIL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,KAAK,QAAQ,EACd,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAE1D,4DAA4D;AAC5D,eAAO,MAAM,WAAW,UAAU,CAAC;AASnC;;;;GAIG;AACH,qBAAa,SAAU,SAAQ,KAAK;gBACtB,OAAO,EAAE,MAAM;CAI5B;AAID,KAAK,mBAAmB,GAAG,WAAW,GAAG;IAAE,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAA;CAAE,CAAC;AAEvE,UAAU,aAAa;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,aAAa,CAAC,mBAAmB,CAAC,GAAG,SAAS,CAAC;CAChE;AAED,KAAK,cAAc,CAAC,MAAM,IACtB;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAA;CAAE,GACvD;IAAE,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,aAAa,CAAC,CAAA;CAAE,CAAC;AAEtD,UAAU,mBAAmB,CAAC,K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AE,WAAW,EAAE,CAAA;CAAE,CAAC;AAEzC,qCAAqC;AACrC,wBAAsB,kBAAkB,CAAC,CAAC,EACxC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,OAAO,GACb,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAQ/B;AAiBD;;;GAGG;AACH,wBAAsB,YAAY,CAChC,UAAU,EAAE,cAAc,EAC1B,KAAK,EAAE,OAAO,GACb,OAAO,CAAC,gBAAgB,CAAC,CAuC3B"}
|
package/dist/server/tools.js
CHANGED
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
*
|
|
18
18
|
* Apps never speak MCP. The gateway snapshots this catalog at deploy, serves
|
|
19
19
|
* MCP clients from the snapshot, and calls a tool over the internal tools
|
|
20
|
-
* contract (../internal/tools.ts) with a verified `chan: "mcp"`
|
|
20
|
+
* contract (../internal/tools.ts) with a verified `chan: "mcp"`
|
|
21
|
+
* identity.
|
|
21
22
|
*
|
|
22
23
|
* `input` and `output` accept any validator implementing Standard Schema and
|
|
23
24
|
* Standard JSON Schema (zod >= 4.2 does both), or a plain
|
|
@@ -114,7 +115,9 @@ function assertToolDefinition(name, definition) {
|
|
|
114
115
|
throw problem("needs a description");
|
|
115
116
|
}
|
|
116
117
|
if (!isToolRisk(definition.risk)) {
|
|
117
|
-
throw problem(
|
|
118
|
+
throw problem(definition.risk === "destructive"
|
|
119
|
+
? 'needs a risk: "read" or "write"; a tool that deletes is risk: "write" with destructive: true'
|
|
120
|
+
: 'needs a risk: "read" or "write"');
|
|
118
121
|
}
|
|
119
122
|
if (!isToolSchema(definition.input)) {
|
|
120
123
|
throw problem("input must be a Standard Schema with JSON Schema support (zod >= 4.2) or { jsonSchema, validate }");
|
|
@@ -128,12 +131,15 @@ function assertToolDefinition(name, definition) {
|
|
|
128
131
|
if (definition.title !== undefined && typeof definition.title !== "string") {
|
|
129
132
|
throw problem("title must be a string");
|
|
130
133
|
}
|
|
131
|
-
for (const flag of ["idempotent", "openWorld"]) {
|
|
134
|
+
for (const flag of ["destructive", "idempotent", "openWorld"]) {
|
|
132
135
|
if (definition[flag] !== undefined &&
|
|
133
136
|
typeof definition[flag] !== "boolean") {
|
|
134
137
|
throw problem(`${flag} must be a boolean`);
|
|
135
138
|
}
|
|
136
139
|
}
|
|
140
|
+
if (definition.destructive && definition.risk === "read") {
|
|
141
|
+
throw problem('is destructive, so its risk is "write"');
|
|
142
|
+
}
|
|
137
143
|
}
|
|
138
144
|
function isRecord(value) {
|
|
139
145
|
return ((typeof value === "object" || typeof value === "function") && value !== null);
|
|
@@ -196,6 +202,7 @@ function describeTool(name, definition) {
|
|
|
196
202
|
if (typeof outputSchema === "string")
|
|
197
203
|
return outputSchema;
|
|
198
204
|
const annotations = {
|
|
205
|
+
...(definition.destructive ? { destructiveHint: true } : {}),
|
|
199
206
|
...(definition.idempotent !== undefined
|
|
200
207
|
? { idempotentHint: definition.idempotent }
|
|
201
208
|
: {}),
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* - `/__everyapp/tools/list` needs the gateway's system token for this app
|
|
8
8
|
* (purpose "tools.list"). It describes tools; it never runs them.
|
|
9
9
|
* - `/__everyapp/tools/call` needs an identity token minted for this exact
|
|
10
|
-
* call: `chan: "mcp"` plus a `tc` claim
|
|
10
|
+
* call: `chan: "mcp"` (an AI client's MCP connection) plus a `tc` claim
|
|
11
|
+
* naming the tool and call id. The
|
|
11
12
|
* token's scopes must include the scope the tool's risk requires; `*` does
|
|
12
13
|
* not count, because the ToolBus only ever mints explicit scopes.
|
|
13
14
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toolsEndpoint.d.ts","sourceRoot":"","sources":["../../src/server/toolsEndpoint.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"toolsEndpoint.d.ts","sourceRoot":"","sources":["../../src/server/toolsEndpoint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAcL,KAAK,sBAAsB,EAO5B,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAOL,KAAK,OAAO,EACb,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,oBAAoB;IACnC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,iDAAiD;IACjD,KAAK,EAAE,MAAM,CAAC;IACd,sEAAsE;IACtE,QAAQ,EAAE,sBAAsB,CAAC;CAClC;AAoFD;;;GAGG;AACH,wBAAsB,kBAAkB,CACtC,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,oBAAoB,EACzB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAyB1B"}
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* - `/__everyapp/tools/list` needs the gateway's system token for this app
|
|
8
8
|
* (purpose "tools.list"). It describes tools; it never runs them.
|
|
9
9
|
* - `/__everyapp/tools/call` needs an identity token minted for this exact
|
|
10
|
-
* call: `chan: "mcp"` plus a `tc` claim
|
|
10
|
+
* call: `chan: "mcp"` (an AI client's MCP connection) plus a `tc` claim
|
|
11
|
+
* naming the tool and call id. The
|
|
11
12
|
* token's scopes must include the scope the tool's risk requires; `*` does
|
|
12
13
|
* not count, because the ToolBus only ever mints explicit scopes.
|
|
13
14
|
*
|
|
@@ -198,8 +199,8 @@ async function callTool(request, env, ctx, verify, options) {
|
|
|
198
199
|
});
|
|
199
200
|
let result;
|
|
200
201
|
try {
|
|
201
|
-
// The call runs in the request context like a page would:
|
|
202
|
-
//
|
|
202
|
+
// The call runs in the request context like a page would: its Gateway
|
|
203
|
+
// calls are attributed to the caller.
|
|
203
204
|
const value = await serveRequest({ request, user, expiresAt }, () => tool.handler(input.value, {
|
|
204
205
|
user,
|
|
205
206
|
env: env,
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export type WebhookProvider = "slack" | "github" | "everyapp";
|
|
2
|
+
export interface VerifyWebhookOptions {
|
|
3
|
+
provider: WebhookProvider;
|
|
4
|
+
/** The signing secret, or several while one rotates. Empty ones are ignored. */
|
|
5
|
+
secret: string | readonly string[];
|
|
6
|
+
/** How far a signed timestamp may be from now (Slack, EveryApp). Default 300 s. */
|
|
7
|
+
toleranceSeconds?: number;
|
|
8
|
+
/** The largest body read before giving up. Default 5 MiB. */
|
|
9
|
+
maxBytes?: number;
|
|
10
|
+
/** The clock, for tests. */
|
|
11
|
+
now?: Date;
|
|
12
|
+
}
|
|
13
|
+
export type WebhookRefusalReason =
|
|
14
|
+
/** No usable secret was passed: a configuration fault, never the sender's. */
|
|
15
|
+
"no_secret"
|
|
16
|
+
/** The provider's signature header is absent. */
|
|
17
|
+
| "missing_signature"
|
|
18
|
+
/** A signature or timestamp header that isn't the provider's shape. */
|
|
19
|
+
| "malformed"
|
|
20
|
+
/** The signed timestamp is outside the tolerance. */
|
|
21
|
+
| "expired"
|
|
22
|
+
/** The body is larger than `maxBytes`. */
|
|
23
|
+
| "too_large"
|
|
24
|
+
/** The body was already read, or its stream failed mid-read. */
|
|
25
|
+
| "unreadable"
|
|
26
|
+
/** No secret verifies the signature. */
|
|
27
|
+
| "mismatch";
|
|
28
|
+
export type WebhookVerification = {
|
|
29
|
+
ok: true;
|
|
30
|
+
/** The body exactly as signed. The request's own body is consumed: parse this. */
|
|
31
|
+
body: string;
|
|
32
|
+
/** The provider's id for this delivery, for deduplication; null when it sends none. */
|
|
33
|
+
deliveryId: string | null;
|
|
34
|
+
/** The signed timestamp in seconds; null for GitHub, which signs none. */
|
|
35
|
+
timestamp: number | null;
|
|
36
|
+
} | {
|
|
37
|
+
ok: false;
|
|
38
|
+
reason: WebhookRefusalReason;
|
|
39
|
+
};
|
|
40
|
+
/** The default body limit: well above Slack's payloads and most of GitHub's (whose cap is 25 MB). */
|
|
41
|
+
export declare const WEBHOOK_MAX_BYTES_DEFAULT: number;
|
|
42
|
+
/**
|
|
43
|
+
* Verify `request` as a `provider` webhook signed with `secret`. Reads the
|
|
44
|
+
* body (at most `maxBytes`) and returns it on success; never throws for a
|
|
45
|
+
* bad delivery, so a handler answers `401` on `{ ok: false }`. Throws a
|
|
46
|
+
* `TypeError` only for a programming error: an unknown `provider`, or a
|
|
47
|
+
* `toleranceSeconds` or `maxBytes` that is negative, NaN or infinite.
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* const check = await verifyWebhook(request, { provider: "slack", secret: env.SLACK_SIGNING_SECRET });
|
|
51
|
+
* if (!check.ok) return new Response(null, { status: 401 });
|
|
52
|
+
* const params = new URLSearchParams(check.body);
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function verifyWebhook(request: Request, options: VerifyWebhookOptions): Promise<WebhookVerification>;
|
|
56
|
+
//# sourceMappingURL=webhooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhooks.d.ts","sourceRoot":"","sources":["../../src/server/webhooks.ts"],"names":[],"mappings":"AAiCA,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,QAAQ,GAAG,UAAU,CAAC;AAE9D,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,eAAe,CAAC;IAC1B,gFAAgF;IAChF,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IACnC,mFAAmF;IACnF,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,6DAA6D;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,4BAA4B;IAC5B,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ;AAED,MAAM,MAAM,oBAAoB;AAC9B,8EAA8E;AAC5E,WAAW;AACb,iDAAiD;GAC/C,mBAAmB;AACrB,uEAAuE;GACrE,WAAW;AACb,qDAAqD;GACnD,SAAS;AACX,0CAA0C;GACxC,WAAW;AACb,gEAAgE;GAC9D,YAAY;AACd,wCAAwC;GACtC,UAAU,CAAC;AAEf,MAAM,MAAM,mBAAmB,GAC3B;IACE,EAAE,EAAE,IAAI,CAAC;IACT,kFAAkF;IAClF,IAAI,EAAE,MAAM,CAAC;IACb,uFAAuF;IACvF,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,0EAA0E;IAC1E,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,GACD;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,oBAAoB,CAAA;CAAE,CAAC;AAEhD,qGAAqG;AACrG,eAAO,MAAM,yBAAyB,QAAkB,CAAC;AAMzD;;;;;;;;;;;;GAYG;AACH,wBAAsB,aAAa,CACjC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,mBAAmB,CAAC,CA4D9B"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `verifyWebhook()`: check an inbound provider webhook on a route the
|
|
3
|
+
* manifest declares `public`, the way the provider signs it.
|
|
4
|
+
*
|
|
5
|
+
* The Gateway forwards a public route's body and headers untouched and
|
|
6
|
+
* verifies nothing (landing-page/content/agent/manifest.md, "Why a route is
|
|
7
|
+
* public"): the app checks the provider's signature itself, with the signing
|
|
8
|
+
* secret it declares in `secrets`. Every app that takes a webhook would
|
|
9
|
+
* otherwise write this HMAC code again, and the easy mistakes (a `===`
|
|
10
|
+
* compare, no timestamp window, verifying a re-serialized body, reading an
|
|
11
|
+
* unbounded body first) are the ones that matter. Proposed in
|
|
12
|
+
* docs/design/2026-11-birds-forcing-function.md (limitation 6).
|
|
13
|
+
*
|
|
14
|
+
* Supported signing schemes:
|
|
15
|
+
* - `slack`: `X-Slack-Signature: v0=<hex>` over `v0:<timestamp>:<body>`,
|
|
16
|
+
* with `X-Slack-Request-Timestamp` inside the tolerance;
|
|
17
|
+
* - `github`: `X-Hub-Signature-256: sha256=<hex>` over the body. GitHub
|
|
18
|
+
* signs no timestamp, so a replay is the receiver's to drop: dedupe on
|
|
19
|
+
* `deliveryId` (`X-GitHub-Delivery`);
|
|
20
|
+
* - `everyapp`: a Gateway's own event delivery, `X-EveryApp-Signature:
|
|
21
|
+
* t=<seconds>,v1=<hex>` over `<t>.<body>` (the same check as the
|
|
22
|
+
* Gateway's `verifyEventSignature`), deduped on `X-EveryApp-Event-Id`.
|
|
23
|
+
*
|
|
24
|
+
* WebCrypto only. The compare is `crypto.subtle.verify`, constant time.
|
|
25
|
+
* Several secrets may be given, so a secret rotates without a gap.
|
|
26
|
+
*/
|
|
27
|
+
import { EVENT_HEADERS, EVENT_SIGNATURE_TOLERANCE_SECONDS, eventSigningInput, parseEventSignature, } from "../internal/events.js";
|
|
28
|
+
/** The default body limit: well above Slack's payloads and most of GitHub's (whose cap is 25 MB). */
|
|
29
|
+
export const WEBHOOK_MAX_BYTES_DEFAULT = 5 * 1024 * 1024;
|
|
30
|
+
const HEX_SHA256 = /^[0-9a-f]{64}$/i;
|
|
31
|
+
const MAX_EVENT_SIGNATURES = 4;
|
|
32
|
+
const encoder = new TextEncoder();
|
|
33
|
+
/**
|
|
34
|
+
* Verify `request` as a `provider` webhook signed with `secret`. Reads the
|
|
35
|
+
* body (at most `maxBytes`) and returns it on success; never throws for a
|
|
36
|
+
* bad delivery, so a handler answers `401` on `{ ok: false }`. Throws a
|
|
37
|
+
* `TypeError` only for a programming error: an unknown `provider`, or a
|
|
38
|
+
* `toleranceSeconds` or `maxBytes` that is negative, NaN or infinite.
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* const check = await verifyWebhook(request, { provider: "slack", secret: env.SLACK_SIGNING_SECRET });
|
|
42
|
+
* if (!check.ok) return new Response(null, { status: 401 });
|
|
43
|
+
* const params = new URLSearchParams(check.body);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export async function verifyWebhook(request, options) {
|
|
47
|
+
const secrets = (typeof options.secret === "string" ? [options.secret] : options.secret).filter((secret) => typeof secret === "string" && secret.length > 0);
|
|
48
|
+
if (secrets.length === 0)
|
|
49
|
+
return refused("no_secret");
|
|
50
|
+
// A NaN or infinite tolerance or limit would quietly turn the window or the
|
|
51
|
+
// read bound off (every comparison with NaN is false): refuse the call.
|
|
52
|
+
const tolerance = checkedOption("toleranceSeconds", options.toleranceSeconds, EVENT_SIGNATURE_TOLERANCE_SECONDS);
|
|
53
|
+
const maxBytes = checkedOption("maxBytes", options.maxBytes, WEBHOOK_MAX_BYTES_DEFAULT);
|
|
54
|
+
const signed = readSignature(request.headers, options.provider);
|
|
55
|
+
if (!signed.ok)
|
|
56
|
+
return signed;
|
|
57
|
+
if (signed.timestamp !== null) {
|
|
58
|
+
const nowSeconds = Math.floor((options.now ?? new Date()).getTime() / 1000);
|
|
59
|
+
if (Math.abs(nowSeconds - signed.timestamp) > tolerance) {
|
|
60
|
+
return refused("expired");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const bytes = await readBody(request, maxBytes);
|
|
64
|
+
if (bytes === "unreadable")
|
|
65
|
+
return refused("unreadable");
|
|
66
|
+
if (bytes === null)
|
|
67
|
+
return refused("too_large");
|
|
68
|
+
// The HMAC runs over the bytes received, never a decoded and re-encoded copy.
|
|
69
|
+
const prefix = encoder.encode(signed.prefix);
|
|
70
|
+
const data = new Uint8Array(new ArrayBuffer(prefix.byteLength + bytes.byteLength));
|
|
71
|
+
data.set(prefix, 0);
|
|
72
|
+
data.set(bytes, prefix.byteLength);
|
|
73
|
+
for (const secret of secrets) {
|
|
74
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["verify"]);
|
|
75
|
+
for (const candidate of signed.digests) {
|
|
76
|
+
if (await crypto.subtle.verify("HMAC", key, fromHex(candidate), data)) {
|
|
77
|
+
return {
|
|
78
|
+
ok: true,
|
|
79
|
+
body: new TextDecoder().decode(bytes),
|
|
80
|
+
deliveryId: signed.deliveryId,
|
|
81
|
+
timestamp: signed.timestamp,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return refused("mismatch");
|
|
87
|
+
}
|
|
88
|
+
function readSignature(headers, provider) {
|
|
89
|
+
switch (provider) {
|
|
90
|
+
case "slack": {
|
|
91
|
+
const header = headers.get("x-slack-signature");
|
|
92
|
+
if (!header)
|
|
93
|
+
return refused("missing_signature");
|
|
94
|
+
const rawTimestamp = headers.get("x-slack-request-timestamp");
|
|
95
|
+
const digest = header.startsWith("v0=") ? header.slice(3) : "";
|
|
96
|
+
if (!HEX_SHA256.test(digest) ||
|
|
97
|
+
!rawTimestamp ||
|
|
98
|
+
!/^\d{1,12}$/.test(rawTimestamp)) {
|
|
99
|
+
return refused("malformed");
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
ok: true,
|
|
103
|
+
digests: [digest.toLowerCase()],
|
|
104
|
+
timestamp: Number(rawTimestamp),
|
|
105
|
+
deliveryId: null,
|
|
106
|
+
prefix: `v0:${rawTimestamp}:`,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
case "github": {
|
|
110
|
+
const header = headers.get("x-hub-signature-256");
|
|
111
|
+
if (!header)
|
|
112
|
+
return refused("missing_signature");
|
|
113
|
+
const digest = header.startsWith("sha256=") ? header.slice(7) : "";
|
|
114
|
+
if (!HEX_SHA256.test(digest))
|
|
115
|
+
return refused("malformed");
|
|
116
|
+
return {
|
|
117
|
+
ok: true,
|
|
118
|
+
digests: [digest.toLowerCase()],
|
|
119
|
+
timestamp: null,
|
|
120
|
+
deliveryId: headers.get("x-github-delivery"),
|
|
121
|
+
prefix: "",
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
case "everyapp": {
|
|
125
|
+
const header = headers.get(EVENT_HEADERS.signature);
|
|
126
|
+
if (!header)
|
|
127
|
+
return refused("missing_signature");
|
|
128
|
+
const parsed = parseEventSignature(header);
|
|
129
|
+
// The Gateway sends one v1 (two while a secret rotates): a header with
|
|
130
|
+
// many isn't one it sent, and each would cost an HMAC per secret.
|
|
131
|
+
if (!parsed || parsed.v1.length > MAX_EVENT_SIGNATURES) {
|
|
132
|
+
return refused("malformed");
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
ok: true,
|
|
136
|
+
digests: parsed.v1,
|
|
137
|
+
timestamp: parsed.timestamp,
|
|
138
|
+
deliveryId: headers.get(EVENT_HEADERS.eventId),
|
|
139
|
+
prefix: eventSigningInput(parsed.timestamp, ""),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
default: {
|
|
143
|
+
const unknown = provider;
|
|
144
|
+
throw new TypeError(`verifyWebhook: unknown provider "${String(unknown)}"`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** A finite, non-negative option, or its default when not given. */
|
|
149
|
+
function checkedOption(name, value, fallback) {
|
|
150
|
+
if (value === undefined)
|
|
151
|
+
return fallback;
|
|
152
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
153
|
+
throw new TypeError(`verifyWebhook: ${name} must be a finite number of at least 0`);
|
|
154
|
+
}
|
|
155
|
+
return value;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The body's bytes; null once it passes `maxBytes` (read no further);
|
|
159
|
+
* "unreadable" when it was already consumed or its stream fails.
|
|
160
|
+
*/
|
|
161
|
+
async function readBody(request, maxBytes) {
|
|
162
|
+
const declared = Number(request.headers.get("content-length"));
|
|
163
|
+
if (Number.isFinite(declared) && declared > maxBytes)
|
|
164
|
+
return null;
|
|
165
|
+
if (request.bodyUsed)
|
|
166
|
+
return "unreadable";
|
|
167
|
+
if (!request.body)
|
|
168
|
+
return new Uint8Array(new ArrayBuffer(0));
|
|
169
|
+
const chunks = [];
|
|
170
|
+
let total = 0;
|
|
171
|
+
try {
|
|
172
|
+
const reader = request.body.getReader();
|
|
173
|
+
for (;;) {
|
|
174
|
+
const { done, value } = await reader.read();
|
|
175
|
+
if (done)
|
|
176
|
+
break;
|
|
177
|
+
total += value.byteLength;
|
|
178
|
+
if (total > maxBytes) {
|
|
179
|
+
await reader.cancel().catch(() => undefined);
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
chunks.push(value);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
return "unreadable";
|
|
187
|
+
}
|
|
188
|
+
const bytes = new Uint8Array(new ArrayBuffer(total));
|
|
189
|
+
let offset = 0;
|
|
190
|
+
for (const chunk of chunks) {
|
|
191
|
+
bytes.set(chunk, offset);
|
|
192
|
+
offset += chunk.byteLength;
|
|
193
|
+
}
|
|
194
|
+
return bytes;
|
|
195
|
+
}
|
|
196
|
+
function fromHex(hex) {
|
|
197
|
+
const bytes = new Uint8Array(new ArrayBuffer(hex.length / 2));
|
|
198
|
+
for (let index = 0; index < bytes.length; index += 1) {
|
|
199
|
+
bytes[index] = Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16);
|
|
200
|
+
}
|
|
201
|
+
return bytes;
|
|
202
|
+
}
|
|
203
|
+
function refused(reason) {
|
|
204
|
+
return { ok: false, reason };
|
|
205
|
+
}
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -67,9 +67,8 @@ export interface TestWorker {
|
|
|
67
67
|
export interface CallToolOptions {
|
|
68
68
|
user?: TestUser;
|
|
69
69
|
/**
|
|
70
|
-
* The caller's scopes. Default: `mcp:read
|
|
71
|
-
*
|
|
72
|
-
* refusal.
|
|
70
|
+
* The caller's scopes. Default: `mcp:read` and `mcp:write`, so every
|
|
71
|
+
* tool may be called; pass `["mcp:read"]` to test a refusal.
|
|
73
72
|
*/
|
|
74
73
|
scopes?: string[];
|
|
75
74
|
/** Default: {@link testEnv}() for the worker's app. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAuBA,OAAO,EAIL,eAAe,
|
|
1
|
+
{"version":3,"file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AM,EAAE,OAAO,CAAM,EAClC,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,mBAAmB,CAAC,CAoB9B;AAED,2EAA2E;AAC3E,wBAAsB,SAAS,CAC7B,GAAG,EAAE,UAAU,GAAG,OAAO,EACzB,OAAO,GAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,gBAAgB,CAAA;CAAO,GACrD,OAAO,CAAC,mBAAmB,CAAC,CAuB9B"}
|
package/dist/testing/index.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* deployed Worker. For tests only; a Worker never imports this module.
|
|
18
18
|
*/
|
|
19
19
|
import { DEV_ISSUER, DEV_PRIVATE_KEY_PEM, DEV_PUBLIC_KEY_PEM, } from "../dev/keypair.js";
|
|
20
|
-
import { APP_ID_ENV, AUDIENCE_ENV, IDENTITY_DEV_KEY_ID, IDENTITY_HEADER,
|
|
20
|
+
import { APP_ID_ENV, AUDIENCE_ENV, IDENTITY_DEV_KEY_ID, IDENTITY_HEADER, MCP_READ_SCOPE, MCP_WRITE_SCOPE, SYSTEM_HEADER, TOOLS_CALL_PATH, TOOLS_LIST_PATH, mintIdentityJwt, mintSystemJwt, } from "../internal/index.js";
|
|
21
21
|
import { everyApp } from "../server/everyApp.js";
|
|
22
22
|
export { IDENTITY_HEADER };
|
|
23
23
|
/** The person a test signs in by default: the owner of `org_test`. */
|
|
@@ -104,11 +104,7 @@ export async function callTool(app, name, args = {}, options = {}) {
|
|
|
104
104
|
user: options.user,
|
|
105
105
|
appId: audienceOf(env),
|
|
106
106
|
channel: "mcp",
|
|
107
|
-
scopes: options.scopes ?? [
|
|
108
|
-
MCP_READ_SCOPE,
|
|
109
|
-
MCP_WRITE_SCOPE,
|
|
110
|
-
MCP_DESTRUCTIVE_SCOPE,
|
|
111
|
-
],
|
|
107
|
+
scopes: options.scopes ?? [MCP_READ_SCOPE, MCP_WRITE_SCOPE],
|
|
112
108
|
toolCall: { id: callId, name },
|
|
113
109
|
});
|
|
114
110
|
const response = await workerOf(app).fetch(new Request(`https://app.test${TOOLS_CALL_PATH}`, {
|
package/package.json
CHANGED
|
@@ -1,68 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|