@every-app/sdk 0.3.0-rc.3 → 0.3.0-rc.5

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 (100) hide show
  1. package/README.md +1 -1
  2. package/dist/cloudflare/catalogSettings.generated.d.ts +95 -0
  3. package/dist/cloudflare/catalogSettings.generated.d.ts.map +1 -0
  4. package/dist/cloudflare/catalogSettings.generated.js +221 -0
  5. package/dist/cloudflare/index.d.ts +2 -1
  6. package/dist/cloudflare/index.d.ts.map +1 -1
  7. package/dist/cloudflare/index.js +3 -0
  8. package/dist/cloudflare/sentinel.d.ts +21 -4
  9. package/dist/cloudflare/sentinel.d.ts.map +1 -1
  10. package/dist/cloudflare/sentinel.js +8 -4
  11. package/dist/cloudflare/worker.d.ts +195 -103
  12. package/dist/cloudflare/worker.d.ts.map +1 -1
  13. package/dist/cloudflare/worker.js +220 -137
  14. package/dist/dev/index.d.ts +1 -1
  15. package/dist/dev/index.d.ts.map +1 -1
  16. package/dist/dev/index.js +1 -1
  17. package/dist/dev/keypair.d.ts +2 -0
  18. package/dist/dev/keypair.d.ts.map +1 -1
  19. package/dist/dev/keypair.js +2 -0
  20. package/dist/index.d.ts +6 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +5 -0
  23. package/dist/internal/capabilities.d.ts +1 -1
  24. package/dist/internal/catalog/index.d.ts +1 -1
  25. package/dist/internal/catalog/inference.d.ts +1 -1
  26. package/dist/internal/catalog/inference.js +3 -3
  27. package/dist/internal/events.d.ts +1 -1
  28. package/dist/internal/events.d.ts.map +1 -1
  29. package/dist/internal/events.js +23 -1
  30. package/dist/internal/gatewaySettings.d.ts +7 -1
  31. package/dist/internal/gatewaySettings.d.ts.map +1 -1
  32. package/dist/internal/gatewaySettings.js +12 -1
  33. package/dist/internal/identity.d.ts +29 -3
  34. package/dist/internal/identity.d.ts.map +1 -1
  35. package/dist/internal/identity.js +37 -9
  36. package/dist/internal/index.d.ts +1 -1
  37. package/dist/internal/index.d.ts.map +1 -1
  38. package/dist/internal/index.js +1 -1
  39. package/dist/internal/members.d.ts +3 -2
  40. package/dist/internal/members.d.ts.map +1 -1
  41. package/dist/internal/protocol.d.ts +25 -1
  42. package/dist/internal/protocol.d.ts.map +1 -1
  43. package/dist/internal/protocol.js +21 -1
  44. package/dist/manifest.d.ts +205 -0
  45. package/dist/manifest.d.ts.map +1 -0
  46. package/dist/manifest.js +30 -0
  47. package/dist/server/ai.d.ts +21 -0
  48. package/dist/server/ai.d.ts.map +1 -1
  49. package/dist/server/ai.js +37 -8
  50. package/dist/server/aiBinding.d.ts +68 -0
  51. package/dist/server/aiBinding.d.ts.map +1 -0
  52. package/dist/server/aiBinding.js +294 -0
  53. package/dist/server/attribution.d.ts +9 -5
  54. package/dist/server/attribution.d.ts.map +1 -1
  55. package/dist/server/attribution.js +12 -4
  56. package/dist/server/context.d.ts +68 -0
  57. package/dist/server/context.d.ts.map +1 -0
  58. package/dist/server/context.js +117 -0
  59. package/dist/server/emailBinding.d.ts +72 -0
  60. package/dist/server/emailBinding.d.ts.map +1 -0
  61. package/dist/server/emailBinding.js +124 -0
  62. package/dist/server/emit.d.ts +1 -1
  63. package/dist/server/emit.d.ts.map +1 -1
  64. package/dist/server/emit.js +4 -4
  65. package/dist/server/env.d.ts +41 -0
  66. package/dist/server/env.d.ts.map +1 -0
  67. package/dist/server/env.js +1 -0
  68. package/dist/server/everyApp.d.ts +27 -7
  69. package/dist/server/everyApp.d.ts.map +1 -1
  70. package/dist/server/everyApp.js +106 -5
  71. package/dist/server/fromMcpTools.d.ts +89 -0
  72. package/dist/server/fromMcpTools.d.ts.map +1 -0
  73. package/dist/server/fromMcpTools.js +192 -0
  74. package/dist/server/gatewayFetch.d.ts +24 -18
  75. package/dist/server/gatewayFetch.d.ts.map +1 -1
  76. package/dist/server/gatewayFetch.js +9 -9
  77. package/dist/server/gatewayUrl.d.ts +13 -0
  78. package/dist/server/gatewayUrl.d.ts.map +1 -0
  79. package/dist/server/gatewayUrl.js +34 -0
  80. package/dist/server/index.d.ts +11 -3
  81. package/dist/server/index.d.ts.map +1 -1
  82. package/dist/server/index.js +9 -2
  83. package/dist/server/integrationFetch.d.ts +28 -7
  84. package/dist/server/integrationFetch.d.ts.map +1 -1
  85. package/dist/server/integrationFetch.js +24 -28
  86. package/dist/server/listMembers.d.ts +1 -1
  87. package/dist/server/listMembers.d.ts.map +1 -1
  88. package/dist/server/listMembers.js +5 -8
  89. package/dist/server/sendEmail.d.ts +1 -1
  90. package/dist/server/sendEmail.d.ts.map +1 -1
  91. package/dist/server/sendEmail.js +4 -4
  92. package/dist/server/toolsEndpoint.d.ts.map +1 -1
  93. package/dist/server/toolsEndpoint.js +11 -5
  94. package/dist/tanstack-start/index.d.ts +9 -0
  95. package/dist/tanstack-start/index.d.ts.map +1 -0
  96. package/dist/tanstack-start/index.js +29 -0
  97. package/dist/testing/index.d.ts +93 -0
  98. package/dist/testing/index.d.ts.map +1 -0
  99. package/dist/testing/index.js +156 -0
  100. package/package.json +25 -4
@@ -1,5 +1,5 @@
1
1
  import { ConfigurationError, serviceRoutePath, } from "../internal/index.js";
2
- import { applyAttribution, checkAttribution, isRequest, } from "./attribution.js";
2
+ import { applyAttribution, attributionFor, isRequest, } from "./attribution.js";
3
3
  import { getEveryAppUser } from "./everyApp.js";
4
4
  import { BINDING_ORIGIN } from "./gatewayFetch.js";
5
5
  import { GatewayRefusalError, gatewayRefusal } from "./integrationFetch.js";
@@ -19,8 +19,8 @@ import { GatewayRefusalError, gatewayRefusal } from "./integrationFetch.js";
19
19
  * for a request answers its signed-in user alone, and background work
20
20
  * answers no one.
21
21
  */
22
- export async function listMembers({ env, for: target, }) {
23
- checkAttribution(env, target, "listMembers");
22
+ export async function listMembers({ env, for: explicitTarget, }) {
23
+ const target = attributionFor(env, explicitTarget, "listMembers");
24
24
  const values = env;
25
25
  const binding = values["EVERY_APP_GATEWAY"];
26
26
  if (binding) {
@@ -48,7 +48,7 @@ async function listThroughGateway(binding, target) {
48
48
  throw new GatewayRefusalError({
49
49
  status: response.status,
50
50
  code: "members_list_failed",
51
- message: `The Every App gateway answered ${response.status} to the members list.`,
51
+ message: `The EveryApp gateway answered ${response.status} to the members list.`,
52
52
  });
53
53
  }
54
54
  return (await response.json());
@@ -64,11 +64,8 @@ async function listInDev(env, target) {
64
64
  id: user.id,
65
65
  email: user.email,
66
66
  name: "",
67
- role: memberRole(user.orgRole),
67
+ role: user.orgRole,
68
68
  },
69
69
  ],
70
70
  };
71
71
  }
72
- function memberRole(role) {
73
- return role === "owner" || role === "admin" ? role : "member";
74
- }
@@ -35,5 +35,5 @@ export interface SendEmailOptions {
35
35
  * `.dev.vars` it goes out for real through Email Sending's REST API, from
36
36
  * the developer's own account.
37
37
  */
38
- export declare function sendEmail({ env, for: target, message, }: SendEmailOptions): Promise<SendEmailResult>;
38
+ export declare function sendEmail({ env, for: explicitTarget, message, }: SendEmailOptions): Promise<SendEmailResult>;
39
39
  //# sourceMappingURL=sendEmail.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sendEmail.d.ts","sourceRoot":"","sources":["../../src/server/sendEmail.ts"],"names":[],"mappings":"AAAA,OAAO,EAeL,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACrB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAGL,KAAK,cAAc,EACpB,MAAM,kBAAkB,CAAC;AAI1B,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;;;;;OAMG;IACH,GAAG,CAAC,EAAE,cAAc,CAAC;IACrB,OAAO,EAAE,gBAAgB,CAAC;CAC3B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,SAAS,CAAC,EAC9B,GAAG,EACH,GAAG,EAAE,MAAM,EACX,OAAO,GACR,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CAoB7C"}
1
+ {"version":3,"file":"sendEmail.d.ts","sourceRoot":"","sources":["../../src/server/sendEmail.ts"],"names":[],"mappings":"AAAA,OAAO,EAeL,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACrB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAGL,KAAK,cAAc,EACpB,MAAM,kBAAkB,CAAC;AAI1B,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;;;;;OAMG;IACH,GAAG,CAAC,EAAE,cAAc,CAAC;IACrB,OAAO,EAAE,gBAAgB,CAAC;CAC3B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,SAAS,CAAC,EAC9B,GAAG,EACH,GAAG,EAAE,cAAc,EACnB,OAAO,GACR,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CAoB7C"}
@@ -1,5 +1,5 @@
1
1
  import { ConfigurationError, EMAIL_SENDERS_ENV, emailDomain, emailProviderRefusal, emailRestBody, emailRestUrl, emailSenderRefusal, parseEmailList, parseEmailSender, parseSendEmailMessage, readEmailRestResponse, senderAllowed, serviceRoutePath, } from "../internal/index.js";
2
- import { applyAttribution, checkAttribution, } from "./attribution.js";
2
+ import { applyAttribution, attributionFor, } from "./attribution.js";
3
3
  import { BINDING_ORIGIN } from "./gatewayFetch.js";
4
4
  import { GatewayRefusalError, gatewayRefusal } from "./integrationFetch.js";
5
5
  /**
@@ -19,8 +19,8 @@ import { GatewayRefusalError, gatewayRefusal } from "./integrationFetch.js";
19
19
  * `.dev.vars` it goes out for real through Email Sending's REST API, from
20
20
  * the developer's own account.
21
21
  */
22
- export async function sendEmail({ env, for: target, message, }) {
23
- checkAttribution(env, target, "sendEmail");
22
+ export async function sendEmail({ env, for: explicitTarget, message, }) {
23
+ const target = attributionFor(env, explicitTarget, "sendEmail");
24
24
  const values = env;
25
25
  const binding = values["EVERY_APP_GATEWAY"];
26
26
  if (binding) {
@@ -49,7 +49,7 @@ async function sendThroughGateway(binding, target, message) {
49
49
  throw new GatewayRefusalError({
50
50
  status: response.status,
51
51
  code: "email_send_failed",
52
- message: `The Every App gateway answered ${response.status} to the send.`,
52
+ message: `The EveryApp gateway answered ${response.status} to the send.`,
53
53
  });
54
54
  }
55
55
  return (await response.json());
@@ -1 +1 @@
1
- {"version":3,"file":"toolsEndpoint.d.ts","sourceRoot":"","sources":["../../src/server/toolsEndpoint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAcL,KAAK,sBAAsB,EAO5B,MAAM,sBAAsB,CAAC;AAC9B,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"}
1
+ {"version":3,"file":"toolsEndpoint.d.ts","sourceRoot":"","sources":["../../src/server/toolsEndpoint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;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"}
@@ -14,7 +14,8 @@
14
14
  * The perimeter also refuses these paths from outside and strips inbound
15
15
  * `x-everyapp-*` headers, so only the gateway's ToolBus ever reaches them.
16
16
  */
17
- import { ConfigurationError, IDENTITY_HEADER, IdentityError, SYSTEM_HEADER, TOOLS_CALL_PATH, TOOLS_LIST_PATH, TOOLS_PROTOCOL_VERSION, catalogHash, requiredScopeForRisk, resolveVerifyOptions, verifyIdentityJwt, verifySystemJwt, } from "../internal/index.js";
17
+ import { ConfigurationError, IDENTITY_HEADER, IdentityError, SYSTEM_HEADER, TOOLS_CALL_PATH, TOOLS_LIST_PATH, TOOLS_PROTOCOL_VERSION, assertOrganizationPin, catalogHash, requiredScopeForRisk, resolveVerifyOptions, verifyIdentity, verifySystemJwt, } from "../internal/index.js";
18
+ import { serveRequest } from "./context.js";
18
19
  import { SDK_VERSION, ToolError, toToolResult, toolSetInternals, validateWithSchema, } from "./tools.js";
19
20
  function toolsJson(status, body, headers = {}) {
20
21
  return Response.json(body, {
@@ -148,9 +149,12 @@ function parseCallRequest(body) {
148
149
  return { v: TOOLS_PROTOCOL_VERSION, name, arguments: args, callId };
149
150
  }
150
151
  async function callTool(request, env, ctx, verify, options) {
151
- let user;
152
+ let user, expiresAt;
152
153
  try {
153
- user = await verifyIdentityJwt(request.headers.get(IDENTITY_HEADER) ?? "", verify);
154
+ ({ user, expiresAt } = await verifyIdentity(request.headers.get(IDENTITY_HEADER) ?? "", verify));
155
+ // The same pin as the handler path: a call for another organization's
156
+ // install never runs a tool here.
157
+ assertOrganizationPin(user, options.identity.organizationId);
154
158
  }
155
159
  catch (error) {
156
160
  if (error instanceof IdentityError) {
@@ -194,13 +198,15 @@ async function callTool(request, env, ctx, verify, options) {
194
198
  });
195
199
  let result;
196
200
  try {
197
- const value = await tool.handler(input.value, {
201
+ // The call runs in the request context like a page would: currentUser()
202
+ // is the caller, and its Gateway calls are attributed to them.
203
+ const value = await serveRequest({ request, user, expiresAt }, () => tool.handler(input.value, {
198
204
  user,
199
205
  env: env,
200
206
  ctx,
201
207
  request,
202
208
  callId: call.callId,
203
- });
209
+ }));
204
210
  result = await toToolResult(tool, value);
205
211
  }
206
212
  catch (error) {
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Puts the person the Gateway signed in on `context.user` (an
3
+ * `EveryAppUser`), verified from the identity the Gateway sent; throws the
4
+ * 401 Response when there is none (a public route, anonymous).
5
+ */
6
+ export declare const ensureUserMiddleware: import("@tanstack/react-start").FunctionMiddlewareAfterServer<{}, unknown, undefined, {
7
+ user: import("../internal/protocol.js").EveryAppUser;
8
+ }, undefined, undefined, undefined>;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tanstack-start/index.ts"],"names":[],"mappings":"AAmBA;;;;GAIG;AACH,eAAO,MAAM,oBAAoB;;mCAK/B,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @every-app/sdk/tanstack-start — identity for a TanStack Start app's server
3
+ * functions:
4
+ *
5
+ * import { ensureUserMiddleware } from "@every-app/sdk/tanstack-start";
6
+ *
7
+ * export const getTodos = createServerFn()
8
+ * .middleware([ensureUserMiddleware])
9
+ * .handler(({ context: { user } }) => listTodos(user.id));
10
+ *
11
+ * The SDK lists `@tanstack/react-start` as a peer dependency, so the Start
12
+ * plugin compiles this module like the app's own: the `.server()` part never
13
+ * reaches the browser.
14
+ */
15
+ import { createMiddleware } from "@tanstack/react-start";
16
+ import { getRequest } from "@tanstack/react-start/server";
17
+ import { env } from "cloudflare:workers";
18
+ import { requireEveryAppUser } from "../server/everyApp.js";
19
+ /**
20
+ * Puts the person the Gateway signed in on `context.user` (an
21
+ * `EveryAppUser`), verified from the identity the Gateway sent; throws the
22
+ * 401 Response when there is none (a public route, anonymous).
23
+ */
24
+ export const ensureUserMiddleware = createMiddleware({
25
+ type: "function",
26
+ }).server(async ({ next }) => {
27
+ const user = await requireEveryAppUser(getRequest(), env);
28
+ return next({ context: { user } });
29
+ });
@@ -0,0 +1,93 @@
1
+ import { IDENTITY_HEADER, type IdentityChannel, type ToolCallResultV1, type ToolsCallResponseV1, type ToolsListResponseV1 } from "../internal/index.js";
2
+ import type { ToolSet } from "../server/tools.js";
3
+ export { IDENTITY_HEADER };
4
+ export type { ToolCallResultV1 as ToolCallResult, ToolsCallResponseV1 as ToolCallResponse, ToolsListResponseV1 as ToolsListResponse, };
5
+ /** The person a test signs in; each field left out takes {@link TEST_USER}'s. */
6
+ export interface TestUser {
7
+ id?: string;
8
+ email?: string;
9
+ orgId?: string;
10
+ /** `owner`, `admin` or `member`, as the Gateway signs it. */
11
+ orgRole?: string;
12
+ }
13
+ /** The person a test signs in by default: the owner of `org_test`. */
14
+ export declare const TEST_USER: Readonly<Required<TestUser>>;
15
+ /** The app id {@link testEnv} uses when none is given. */
16
+ export declare const TEST_APP_ID = "test-app";
17
+ /** What an identity is minted for, beside the person. */
18
+ export interface TestIdentityOptions {
19
+ user?: TestUser;
20
+ /** The app the identity is for (the token's audience). Default: {@link TEST_APP_ID}. */
21
+ appId?: string;
22
+ /** Default `["*"]`: a browser session, which holds every scope. */
23
+ scopes?: string[];
24
+ /** Default `"web"`. */
25
+ channel?: IdentityChannel;
26
+ }
27
+ /**
28
+ * The Worker env `everyApp()` needs to trust test identities: the app id,
29
+ * the dev issuer and key, `EVERYAPP_DEV`, and a `GATEWAY_URL`. `overrides`
30
+ * adds the app's own bindings (a fake `DB`, a secret) or replaces any of
31
+ * these.
32
+ */
33
+ export declare function testEnv<T extends object = Record<never, never>>(overrides?: {
34
+ appId?: string;
35
+ } & T): {
36
+ EVERYAPP_APP_ID: string;
37
+ EVERYAPP_DEV: string;
38
+ EVERYAPP_IDENTITY_ISSUER: string;
39
+ EVERYAPP_IDENTITY_PUBLIC_KEYS: string;
40
+ GATEWAY_URL: string;
41
+ } & Omit<T, "appId">;
42
+ /**
43
+ * An `ExecutionContext` for calling a Worker's `fetch` in a test:
44
+ * `waitUntil` keeps each promise in `waited`, so a test can await them.
45
+ */
46
+ export declare function testContext(): ExecutionContext & {
47
+ waited: Promise<unknown>[];
48
+ };
49
+ /** An identity token for `user`, as the Gateway sends in the identity header. */
50
+ export declare function mintTestIdentity(options?: TestIdentityOptions & {
51
+ toolCall?: {
52
+ id: string;
53
+ name: string;
54
+ };
55
+ }): Promise<string>;
56
+ /**
57
+ * A request as the Gateway forwards it: `user` signed in (the default
58
+ * person when left out), or nobody with `user: null`, as on a public route.
59
+ */
60
+ export declare function testRequest(input: string | URL, init?: RequestInit & Omit<TestIdentityOptions, "user"> & {
61
+ user?: TestUser | null;
62
+ }): Promise<Request>;
63
+ /** A Worker as `everyApp()` returns it. */
64
+ export interface TestWorker {
65
+ fetch(request: Request, env: never, ctx: ExecutionContext): Response | Promise<Response>;
66
+ }
67
+ export interface CallToolOptions {
68
+ user?: TestUser;
69
+ /**
70
+ * The caller's scopes. Default: `mcp:read`, `mcp:write` and
71
+ * `mcp:destructive`, so every tool may be called; pass fewer to test a
72
+ * refusal.
73
+ */
74
+ scopes?: string[];
75
+ /** Default: {@link testEnv}() for the worker's app. */
76
+ env?: object;
77
+ ctx?: ExecutionContext;
78
+ }
79
+ /**
80
+ * Call one tool as the Gateway does for an AI client: a `chan: "mcp"`
81
+ * identity minted for this call, through the app's tools endpoint, so the
82
+ * SDK checks the scope, validates input and output and maps errors as it
83
+ * does deployed. `app` is the Worker `everyApp()` returned, or the
84
+ * `defineTools()` set alone. The answer is `{ result }`, or `{ error }`
85
+ * when the SDK refused the call.
86
+ */
87
+ export declare function callTool(app: TestWorker | ToolSet, name: string, args?: Record<string, unknown>, options?: CallToolOptions): Promise<ToolsCallResponseV1>;
88
+ /** The tool catalog, read as the Gateway reads it when the app deploys. */
89
+ export declare function listTools(app: TestWorker | ToolSet, options?: {
90
+ env?: object;
91
+ ctx?: ExecutionContext;
92
+ }): Promise<ToolsListResponseV1>;
93
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,156 @@
1
+ /**
2
+ * @every-app/sdk/testing — an app's tests, without a Gateway: a signed-in
3
+ * person for a request, and the tools called the way the Gateway calls them.
4
+ *
5
+ * import { callTool, testContext, testEnv, testRequest } from "@every-app/sdk/testing";
6
+ *
7
+ * const worker = everyApp(handler, { tools });
8
+ * const response = await worker.fetch(
9
+ * await testRequest("https://todo.test/api/todos", { user: { id: "ana" } }),
10
+ * testEnv(),
11
+ * testContext(),
12
+ * );
13
+ * const result = await callTool(worker, "list_todos", {}, { user: { id: "ana" } });
14
+ *
15
+ * Identities are signed with the well-known dev keypair, which `everyApp()`
16
+ * trusts only with `EVERYAPP_DEV` set, as {@link testEnv} sets it: never in a
17
+ * deployed Worker. For tests only; a Worker never imports this module.
18
+ */
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, MCP_DESTRUCTIVE_SCOPE, MCP_READ_SCOPE, MCP_WRITE_SCOPE, SYSTEM_HEADER, TOOLS_CALL_PATH, TOOLS_LIST_PATH, mintIdentityJwt, mintSystemJwt, } from "../internal/index.js";
21
+ import { everyApp } from "../server/everyApp.js";
22
+ export { IDENTITY_HEADER };
23
+ /** The person a test signs in by default: the owner of `org_test`. */
24
+ export const TEST_USER = Object.freeze({
25
+ id: "user_test",
26
+ email: "test@example.com",
27
+ orgId: "org_test",
28
+ orgRole: "owner",
29
+ });
30
+ /** The app id {@link testEnv} uses when none is given. */
31
+ export const TEST_APP_ID = "test-app";
32
+ /**
33
+ * The Worker env `everyApp()` needs to trust test identities: the app id,
34
+ * the dev issuer and key, `EVERYAPP_DEV`, and a `GATEWAY_URL`. `overrides`
35
+ * adds the app's own bindings (a fake `DB`, a secret) or replaces any of
36
+ * these.
37
+ */
38
+ export function testEnv(overrides = {}) {
39
+ const { appId = TEST_APP_ID, ...rest } = overrides;
40
+ return {
41
+ EVERYAPP_APP_ID: appId,
42
+ EVERYAPP_DEV: "1",
43
+ EVERYAPP_IDENTITY_ISSUER: DEV_ISSUER,
44
+ EVERYAPP_IDENTITY_PUBLIC_KEYS: JSON.stringify([DEV_PUBLIC_KEY_PEM]),
45
+ GATEWAY_URL: "https://gateway.test",
46
+ ...rest,
47
+ };
48
+ }
49
+ /**
50
+ * An `ExecutionContext` for calling a Worker's `fetch` in a test:
51
+ * `waitUntil` keeps each promise in `waited`, so a test can await them.
52
+ */
53
+ export function testContext() {
54
+ const waited = [];
55
+ return {
56
+ waited,
57
+ waitUntil: (promise) => void waited.push(promise),
58
+ passThroughOnException: () => { },
59
+ props: {},
60
+ };
61
+ }
62
+ /** An identity token for `user`, as the Gateway sends in the identity header. */
63
+ export async function mintTestIdentity(options = {}) {
64
+ const user = { ...TEST_USER, ...options.user };
65
+ return mintIdentityJwt(DEV_PRIVATE_KEY_PEM, {
66
+ subject: {
67
+ sub: user.id,
68
+ email: user.email,
69
+ orgId: user.orgId,
70
+ orgRole: user.orgRole,
71
+ },
72
+ audience: options.appId ?? TEST_APP_ID,
73
+ issuer: DEV_ISSUER,
74
+ keyId: IDENTITY_DEV_KEY_ID,
75
+ channel: options.channel,
76
+ scopes: options.scopes,
77
+ toolCall: options.toolCall,
78
+ });
79
+ }
80
+ /**
81
+ * A request as the Gateway forwards it: `user` signed in (the default
82
+ * person when left out), or nobody with `user: null`, as on a public route.
83
+ */
84
+ export async function testRequest(input, init = {}) {
85
+ const { user, appId, scopes, channel, ...requestInit } = init;
86
+ const headers = new Headers(requestInit.headers);
87
+ if (user !== null) {
88
+ headers.set(IDENTITY_HEADER, await mintTestIdentity({ user, appId, scopes, channel }));
89
+ }
90
+ return new Request(input, { ...requestInit, headers });
91
+ }
92
+ /**
93
+ * Call one tool as the Gateway does for an AI client: a `chan: "mcp"`
94
+ * identity minted for this call, through the app's tools endpoint, so the
95
+ * SDK checks the scope, validates input and output and maps errors as it
96
+ * does deployed. `app` is the Worker `everyApp()` returned, or the
97
+ * `defineTools()` set alone. The answer is `{ result }`, or `{ error }`
98
+ * when the SDK refused the call.
99
+ */
100
+ export async function callTool(app, name, args = {}, options = {}) {
101
+ const env = options.env ?? testEnv();
102
+ const callId = crypto.randomUUID();
103
+ const identity = await mintTestIdentity({
104
+ user: options.user,
105
+ appId: audienceOf(env),
106
+ channel: "mcp",
107
+ scopes: options.scopes ?? [
108
+ MCP_READ_SCOPE,
109
+ MCP_WRITE_SCOPE,
110
+ MCP_DESTRUCTIVE_SCOPE,
111
+ ],
112
+ toolCall: { id: callId, name },
113
+ });
114
+ const response = await workerOf(app).fetch(new Request(`https://app.test${TOOLS_CALL_PATH}`, {
115
+ method: "POST",
116
+ headers: { [IDENTITY_HEADER]: identity },
117
+ body: JSON.stringify({ v: 1, name, arguments: args, callId }),
118
+ }), env, options.ctx ?? testContext());
119
+ return (await response.json());
120
+ }
121
+ /** The tool catalog, read as the Gateway reads it when the app deploys. */
122
+ export async function listTools(app, options = {}) {
123
+ const env = options.env ?? testEnv();
124
+ const token = await mintSystemJwt(DEV_PRIVATE_KEY_PEM, {
125
+ audience: audienceOf(env),
126
+ issuer: DEV_ISSUER,
127
+ purpose: "tools.list",
128
+ keyId: IDENTITY_DEV_KEY_ID,
129
+ });
130
+ const response = await workerOf(app).fetch(new Request(`https://app.test${TOOLS_LIST_PATH}`, {
131
+ method: "POST",
132
+ headers: { [SYSTEM_HEADER]: token },
133
+ body: JSON.stringify({ v: 1 }),
134
+ }), env, options.ctx ?? testContext());
135
+ if (!response.ok) {
136
+ throw new Error(`the tools list answered ${response.status}: ${await response.text()}`);
137
+ }
138
+ return (await response.json());
139
+ }
140
+ /** Who the app's identities are minted for: the deployed audience, else the app id. */
141
+ function audienceOf(env) {
142
+ const values = env;
143
+ for (const key of [AUDIENCE_ENV, APP_ID_ENV]) {
144
+ const value = values[key];
145
+ if (typeof value === "string" && value)
146
+ return value;
147
+ }
148
+ return TEST_APP_ID;
149
+ }
150
+ function workerOf(app) {
151
+ if ("fetch" in app)
152
+ return app;
153
+ return everyApp(() => new Response("Not found", { status: 404 }), {
154
+ tools: app,
155
+ });
156
+ }
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@every-app/sdk",
3
- "version": "0.3.0-rc.3",
3
+ "version": "0.3.0-rc.5",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
10
+ },
7
11
  "./server": {
8
12
  "types": "./dist/server/index.d.ts",
9
13
  "default": "./dist/server/index.js"
@@ -24,19 +28,30 @@
24
28
  "types": "./dist/dev/index.d.ts",
25
29
  "default": "./dist/dev/index.js"
26
30
  },
31
+ "./tanstack-start": {
32
+ "types": "./dist/tanstack-start/index.d.ts",
33
+ "default": "./dist/tanstack-start/index.js"
34
+ },
35
+ "./testing": {
36
+ "types": "./dist/testing/index.d.ts",
37
+ "default": "./dist/testing/index.js"
38
+ },
27
39
  "./package.json": "./package.json"
28
40
  },
29
41
  "files": [
30
42
  "dist",
31
43
  "README.md"
32
44
  ],
33
- "peerDependencies": {
45
+ "dependencies": {
34
46
  "jose": "^6.2.5"
35
47
  },
48
+ "peerDependencies": {
49
+ "@tanstack/react-start": "^1.168.61"
50
+ },
36
51
  "devDependencies": {
37
52
  "@cloudflare/workers-types": "^4.20251014.0",
53
+ "@tanstack/react-start": "1.168.61",
38
54
  "@types/node": "^22.18.13",
39
- "jose": "^6.2.12",
40
55
  "prettier": "^3.6.2",
41
56
  "typescript": "^5.9.3",
42
57
  "vite": "^7.1.2",
@@ -44,6 +59,11 @@
44
59
  "vitest": "^3.2.6",
45
60
  "zod": "^4.2.0"
46
61
  },
62
+ "peerDependenciesMeta": {
63
+ "@tanstack/react-start": {
64
+ "optional": true
65
+ }
66
+ },
47
67
  "scripts": {
48
68
  "prebuild": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
49
69
  "build": "tsc -p tsconfig.build.json",
@@ -52,6 +72,7 @@
52
72
  "format:write": "prettier . --write",
53
73
  "test": "vitest run --silent",
54
74
  "test:watch": "vitest",
55
- "check:esm-relative-imports": "node ./scripts/check-esm-relative-imports.mjs"
75
+ "check:esm-relative-imports": "node ./scripts/check-esm-relative-imports.mjs",
76
+ "catalog:update": "UPDATE_CATALOG=1 vitest run src/cloudflare/catalogSettings.test.ts --silent"
56
77
  }
57
78
  }