@superforge/infra 0.1.1 → 0.2.0

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.
@@ -1,24 +1,23 @@
1
+ import { type AppPaths } from "./contract/index.js";
1
2
  import type { Infra } from "./infra.js";
2
- /**
3
- * Infra's API, which a project's keys call. A server can be pointed at
4
- * another Infra with `INFRA_URL` (choose.ts).
5
- */
3
+ /** Infra's API, which a project's keys call */
6
4
  export declare const INFRA_URL = "https://infra.superforge.studio/v1";
7
5
  /**
8
6
  * Infra, called over HTTP with a project's key: a secret key in a build, a
9
7
  * test key in a dev server. No call is retried, since none is safe to send
10
8
  * twice (a second start sends a second email, a second verify finds the
11
- * code spent, and a comment sent again is kept twice); the visitor asks
12
- * again instead. `send` is `fetch`, or a test's own.
9
+ * code spent, and a comment sent again is kept twice); the person asks
10
+ * again instead. `url` is Infra's own, but for Infra's tests and a dev
11
+ * server working on Infra; `send` is `fetch`, or a test's own; `paths` is
12
+ * where the app answers Claude's login, when not `APP_PATHS`.
13
13
  *
14
- * Every call says which version of the package made it (`Infra-Client`,
15
- * version.ts). Every answer is logged with its call and status, and a
16
- * refusal with its `error` and Infra's `message`, which says what went
17
- * wrong and what fixes it (Infra's README, "Errors"), so an app's logs say
18
- * why a call failed.
14
+ * Every call says which release of the package made it (`Infra-Client`).
15
+ * Every answer is logged with its call and status, and a refusal with its
16
+ * reason and Infra's message, so an app's logs say why a call failed.
19
17
  */
20
- export declare function infraClient({ key, url, send }: {
18
+ export declare function infraClient({ key, url, send, paths }: {
21
19
  key: string;
22
20
  url?: string;
23
21
  send?: typeof fetch;
22
+ paths?: AppPaths;
24
23
  }): Infra;
package/output/client.js CHANGED
@@ -1,11 +1,8 @@
1
- import { agent_connections, agent_ended, agent_redirect, agent_refused, agent_request, checkToken, infraKeys, passThrough } from "./agents.js";
2
- import { any_refusal, comment_refused, infra_project, kept_comment, start_refused, started_login, verified_login, verify_refused } from "./answers.js";
1
+ import { checkToken, infraKeys, passOAuth } from "./agents.js";
2
+ import { APP_PATHS, agent_connections, agent_ended, agent_redirect, agent_refused, agent_request, CLIENT_HEADER, comment_refused, infra_failure, infra_project, kept_comment, start_refused, started_login, verified_login, verify_refused } from "./contract/index.js";
3
3
  import { SECOND } from "./primitives.js";
4
- import { CLIENT, CLIENT_HEADER } from "./version.js";
5
- /**
6
- * Infra's API, which a project's keys call. A server can be pointed at
7
- * another Infra with `INFRA_URL` (choose.ts).
8
- */
4
+ import { CLIENT } from "./version.js";
5
+ /** Infra's API, which a project's keys call */
9
6
  export const INFRA_URL = "https://infra.superforge.studio/v1";
10
7
  // How long a call waits for Infra's answer. Starting a login waits on
11
8
  // Infra sending the email, which takes a second or two.
@@ -14,31 +11,29 @@ const TIMEOUT = 15 * SECOND;
14
11
  * Infra, called over HTTP with a project's key: a secret key in a build, a
15
12
  * test key in a dev server. No call is retried, since none is safe to send
16
13
  * twice (a second start sends a second email, a second verify finds the
17
- * code spent, and a comment sent again is kept twice); the visitor asks
18
- * again instead. `send` is `fetch`, or a test's own.
14
+ * code spent, and a comment sent again is kept twice); the person asks
15
+ * again instead. `url` is Infra's own, but for Infra's tests and a dev
16
+ * server working on Infra; `send` is `fetch`, or a test's own; `paths` is
17
+ * where the app answers Claude's login, when not `APP_PATHS`.
19
18
  *
20
- * Every call says which version of the package made it (`Infra-Client`,
21
- * version.ts). Every answer is logged with its call and status, and a
22
- * refusal with its `error` and Infra's `message`, which says what went
23
- * wrong and what fixes it (Infra's README, "Errors"), so an app's logs say
24
- * why a call failed.
19
+ * Every call says which release of the package made it (`Infra-Client`).
20
+ * Every answer is logged with its call and status, and a refusal with its
21
+ * reason and Infra's message, so an app's logs say why a call failed.
25
22
  */
26
- export function infraClient({ key, url = INFRA_URL, send = fetch }) {
23
+ export function infraClient({ key, url = INFRA_URL, send = fetch, paths = APP_PATHS }) {
27
24
  // One call: its status and its JSON, or why there's none, which is
28
25
  // logged
29
26
  async function ask(method, call, body) {
27
+ const headers = {
28
+ Authorization: `Bearer ${key}`,
29
+ [CLIENT_HEADER]: CLIENT
30
+ };
31
+ if (body !== undefined) {
32
+ headers["Content-Type"] = "application/json";
33
+ }
30
34
  const response = await send(`${url}${call}`, {
31
35
  method,
32
- headers: body === undefined
33
- ? {
34
- Authorization: `Bearer ${key}`,
35
- [CLIENT_HEADER]: CLIENT
36
- }
37
- : {
38
- Authorization: `Bearer ${key}`,
39
- [CLIENT_HEADER]: CLIENT,
40
- "Content-Type": "application/json"
41
- },
36
+ headers,
42
37
  body: body === undefined ? undefined : JSON.stringify(body),
43
38
  signal: AbortSignal.timeout(TIMEOUT)
44
39
  }).catch((error) => {
@@ -46,7 +41,7 @@ export function infraClient({ key, url = INFRA_URL, send = fetch }) {
46
41
  return null;
47
42
  });
48
43
  if (response === null) {
49
- return unavailable(`Infra didn't answer ${call} at ${url}. It may be down, or INFRA_URL may point somewhere it isn't; trying again in a few minutes may work.`);
44
+ return unavailable(`Infra didn't answer ${call} at ${url}. It may be down; trying again in a few minutes may work.`);
50
45
  }
51
46
  const json = await response.json().catch((error) => {
52
47
  console.error("Infra answered without JSON", {
@@ -57,15 +52,15 @@ export function infraClient({ key, url = INFRA_URL, send = fetch }) {
57
52
  return undefined;
58
53
  });
59
54
  if (json === undefined) {
60
- return unavailable(`Infra answered ${call} with ${response.status} and no JSON, which usually means it's down or INFRA_URL points somewhere that isn't Infra.`);
55
+ return unavailable(`Infra answered ${call} with ${response.status} and no JSON, which usually means it's down. Trying again in a few minutes may work.`);
61
56
  }
62
- const refusal = any_refusal.safeParse(json);
63
- if (response.status >= 400 && refusal.success) {
57
+ const failure = infra_failure.safeParse(json);
58
+ if (response.status >= 400 && failure.success) {
64
59
  console.info("Infra refused a call", {
65
60
  call,
66
61
  status: response.status,
67
- error: refusal.data.error,
68
- message: refusal.data.message
62
+ reason: failure.data.error.reason,
63
+ message: failure.data.error.message
69
64
  });
70
65
  }
71
66
  else {
@@ -73,28 +68,27 @@ export function infraClient({ key, url = INFRA_URL, send = fetch }) {
73
68
  }
74
69
  return { ok: true, data: { status: response.status, json } };
75
70
  }
76
- const post = (call, body) => ask("POST", call, body);
77
- // An answer read with `schema` when it's `status`, else as `refused`,
78
- // else as one the client doesn't know
71
+ // An answer read with `schema` when it's `status`, else as one of the
72
+ // call's `refused`, else as one the client can't act on: unavailable,
73
+ // in Infra's own words
79
74
  async function answered(asked, call, expected, refused) {
80
- if (!asked.ok) {
81
- return asked;
75
+ const answer = await asked;
76
+ if (!answer.ok) {
77
+ return answer;
82
78
  }
83
- const answer = asked.data;
84
- if (answer.status === expected.status) {
85
- const parsed = expected.schema.safeParse(answer.json);
79
+ const { status, json } = answer.data;
80
+ if (status === expected.status) {
81
+ const parsed = expected.schema.safeParse(json);
86
82
  return parsed.success
87
83
  ? { ok: true, data: parsed.data }
88
- : unreadable(call, answer);
84
+ : unreadable(call, answer.data);
89
85
  }
90
- else if (refused !== undefined) {
91
- const parsed = refused.safeParse(answer.json);
92
- return parsed.success
93
- ? { ok: false, error: parsed.data }
94
- : unexpected(call, answer);
86
+ const parsed = refused?.safeParse(json);
87
+ if (parsed?.success) {
88
+ return { ok: false, error: parsed.data };
95
89
  }
96
90
  else {
97
- return unexpected(call, answer);
91
+ return unexpected(call, answer.data);
98
92
  }
99
93
  }
100
94
  // Claude's login (agents.ts): Infra's public keys, fetched once and
@@ -102,33 +96,28 @@ export function infraClient({ key, url = INFRA_URL, send = fetch }) {
102
96
  // is, which every token must be for, asked once
103
97
  const keys = infraKeys(url);
104
98
  let project;
105
- async function projectOf() {
99
+ function projectOf() {
106
100
  const call = "/project";
107
- return answered(await ask("GET", call), call, {
101
+ return answered(ask("GET", call), call, {
108
102
  status: 200,
109
103
  schema: infra_project
110
104
  });
111
105
  }
112
- // A call about Claude's requests and connections: its answer, or why
113
- // Infra refused it, or that it couldn't be asked
114
- async function agents(method, call, schema, body) {
115
- return answered(await ask(method, call, body), call, { status: 200, schema }, agent_refused);
116
- }
117
106
  return {
118
107
  project() {
119
108
  return projectOf();
120
109
  },
121
- async startLogin({ email, ip, origin }) {
110
+ startLogin(request) {
122
111
  const call = "/login/start";
123
- return answered(await post(call, origin === null ? { email, ip } : { email, ip, origin }), call, { status: 200, schema: started_login }, start_refused);
112
+ return answered(ask("POST", call, request), call, { status: 200, schema: started_login }, start_refused);
124
113
  },
125
- async verifyLogin(proof) {
114
+ verifyLogin(proof) {
126
115
  const call = "/login/verify";
127
- return answered(await post(call, proof), call, { status: 200, schema: verified_login }, verify_refused);
116
+ return answered(ask("POST", call, proof), call, { status: 200, schema: verified_login }, verify_refused);
128
117
  },
129
118
  async fileComment(comment) {
130
119
  const call = "/comments";
131
- const filed = await answered(await post(call, comment), call, { status: 201, schema: kept_comment }, comment_refused);
120
+ const filed = await answered(ask("POST", call, comment), call, { status: 201, schema: kept_comment }, comment_refused);
132
121
  if (!filed.ok && filed.error.reason !== "unavailable") {
133
122
  // What the app sent is wrong, which only a change to it
134
123
  // fixes
@@ -139,44 +128,46 @@ export function infraClient({ key, url = INFRA_URL, send = fetch }) {
139
128
  }
140
129
  return filed;
141
130
  },
142
- passOAuth(request, path, origin) {
143
- return passThrough({ url, key, send, request, path, origin });
131
+ passOAuth(request, call, origin) {
132
+ return passOAuth({ url, key, send, paths, request, call, origin });
144
133
  },
145
134
  async agentRequest(request_id) {
146
- const asked = await agents("GET", `/agents/requests/${request_id}`, agent_request);
135
+ const call = `/agents/requests/${request_id}`;
136
+ const asked = await answered(ask("GET", call), call, { status: 200, schema: agent_request }, agent_refused);
147
137
  return asked.ok ? { ok: true, data: asked.data.request } : asked;
148
138
  },
149
139
  allowAgent(request_id, allowed) {
150
- return agents("POST", `/agents/requests/${request_id}/allow`, agent_redirect, allowed);
140
+ const call = `/agents/requests/${request_id}/allow`;
141
+ return answered(ask("POST", call, allowed), call, { status: 200, schema: agent_redirect }, agent_refused);
151
142
  },
152
143
  denyAgent(request_id) {
153
- return agents("POST", `/agents/requests/${request_id}/deny`, agent_redirect, {});
144
+ const call = `/agents/requests/${request_id}/deny`;
145
+ return answered(ask("POST", call, {}), call, { status: 200, schema: agent_redirect }, agent_refused);
154
146
  },
155
- async agentConnections(email) {
156
- const query = new URLSearchParams({ email });
157
- const listed = await agents("GET", `/agents/connections?${query}`, agent_connections);
158
- if (listed.ok) {
159
- return { ok: true, data: listed.data.connections };
160
- }
161
- else {
162
- return { ok: false, error: asUnavailable(listed.error) };
163
- }
147
+ async agentConnections(user_id) {
148
+ const call = `/agents/connections?${new URLSearchParams({ user_id })}`;
149
+ const listed = await answered(ask("GET", call), call, {
150
+ status: 200,
151
+ schema: agent_connections
152
+ });
153
+ return listed.ok
154
+ ? { ok: true, data: listed.data.connections }
155
+ : listed;
164
156
  },
165
157
  async endAgentConnection(connection_id) {
166
- const ended = await agents("DELETE", `/agents/connections/${connection_id}`, agent_ended);
158
+ const call = `/agents/connections/${connection_id}`;
159
+ const ended = await answered(ask("DELETE", call), call, { status: 200, schema: agent_ended }, agent_refused);
167
160
  return ended.ok ? { ok: true, data: null } : ended;
168
161
  },
169
- async endAgentConnections(email) {
170
- const query = new URLSearchParams({ email });
171
- const ended = await agents("DELETE", `/agents/connections?${query}`, agent_ended);
172
- if (ended.ok) {
173
- return { ok: true, data: ended.data.ended };
174
- }
175
- else {
176
- return { ok: false, error: asUnavailable(ended.error) };
177
- }
162
+ async endAgentConnections(user_id) {
163
+ const call = `/agents/connections?${new URLSearchParams({ user_id })}`;
164
+ const ended = await answered(ask("DELETE", call), call, {
165
+ status: 200,
166
+ schema: agent_ended
167
+ });
168
+ return ended.ok ? { ok: true, data: ended.data.ended } : ended;
178
169
  },
179
- async checkAgentToken(token, origin) {
170
+ async checkAgentToken(token, expected) {
180
171
  project ??= projectOf();
181
172
  const known = await project;
182
173
  if (!known.ok) {
@@ -188,7 +179,7 @@ export function infraClient({ key, url = INFRA_URL, send = fetch }) {
188
179
  }
189
180
  else {
190
181
  return await checkToken(keys, token, {
191
- origin,
182
+ ...expected,
192
183
  project: known.data.project.id,
193
184
  mode: known.data.mode
194
185
  });
@@ -200,42 +191,23 @@ export function infraClient({ key, url = INFRA_URL, send = fetch }) {
200
191
  function unavailable(message) {
201
192
  return { ok: false, error: { reason: "unavailable", message } };
202
193
  }
203
- // A refusal a call that only lists or ends things can't otherwise answer,
204
- // read as unavailable with Infra's own words
205
- function asUnavailable(refused) {
206
- if ("reason" in refused) {
207
- return refused;
208
- }
209
- else {
210
- return { reason: "unavailable", message: refused.message };
211
- }
212
- }
213
- // An answer the client doesn't expect: a key Infra refused (401, which an
214
- // owner fixes in Railway), a body this app sent wrong (400 `invalid`, a
215
- // bug), or an error Infra added after this version of the package. Logged
216
- // with what it said, and unavailable, with Infra's own words for why when
217
- // it gave them, which an app shows its team.
194
+ // A refusal the app has no branch for: a key Infra refused (401, which an
195
+ // owner fixes in Railway) or a body this app sent wrong (400 `invalid`, a
196
+ // bug). Logged, and unavailable, in Infra's own words, which an app shows
197
+ // its team.
218
198
  function unexpected(call, answer) {
219
- const refusal = any_refusal.safeParse(answer.json);
220
- const error = refusal.success ? refusal.data.error : null;
221
- const said = refusal.success ? refusal.data.message : undefined;
222
- console.error("Infra answered what the client doesn't expect", {
199
+ console.error("Infra refused a call the app can't act on", {
223
200
  call,
224
201
  status: answer.status,
225
202
  answer: answer.json
226
203
  });
227
- if (said !== undefined) {
228
- return unavailable(said);
229
- }
230
- else if (answer.status === 401) {
231
- return unavailable(`Infra refused this app's key${error === null ? "" : ` (${error})`}. Make a secret key on superforge.studio, in the project's Settings, and paste it into INFRA_SECRET_KEY on the app's service in Railway.`);
232
- }
233
- else {
234
- return unavailable(`Infra answered ${call} with ${answer.status}${error === null ? "" : ` ${error}`}, which this version of @superforge/infra doesn't know. Ask Claude to update @superforge/infra in the app.`);
235
- }
204
+ const failure = infra_failure.safeParse(answer.json);
205
+ return unavailable(failure.success
206
+ ? failure.data.error.message
207
+ : `Infra answered ${call} with ${answer.status}, in a shape this version of @superforge/infra can't read. Ask Claude to update @superforge/infra in the app.`);
236
208
  }
237
- // A success the client can't read: Infra changed what a call answers since
238
- // this version of the package
209
+ // A success the client can't read: Infra answers a shape this release of
210
+ // the package doesn't know
239
211
  function unreadable(call, answer) {
240
212
  console.error("Infra answered in a shape the client can't read", {
241
213
  call,
@@ -0,0 +1,203 @@
1
+ import { z } from "zod";
2
+ /** What someone on the app's team lets Claude do through its MCP */
3
+ export declare const AGENT_SCOPES: readonly ["read", "act"];
4
+ export declare const agent_scope: z.ZodEnum<{
5
+ read: "read";
6
+ act: "act";
7
+ }>;
8
+ export type AgentScope = z.infer<typeof agent_scope>;
9
+ /**
10
+ * The header the app's server sends Claude's OAuth calls on with: where
11
+ * Claude reached the app, `https://vmdsystem.com`, its tokens' issuer.
12
+ * Only a header can carry it, since the call's body and query are Claude's.
13
+ */
14
+ export declare const APP_ORIGIN_HEADER = "App-Origin";
15
+ /**
16
+ * Where the app answers Claude's login, which its OAuth metadata names and
17
+ * its authorize call sends the browser to: its OAuth calls (`/oauth`, then
18
+ * `/authorize`, `/token`, `/register` and `/revoke`), and its Allow page
19
+ * (`/connect`, then the request's ID). An app with others gives its own to
20
+ * `chooseInfra`; Infra keeps none.
21
+ */
22
+ export type AppPaths = {
23
+ oauth: string;
24
+ allow: string;
25
+ };
26
+ export declare const APP_PATHS: AppPaths;
27
+ /** The OAuth calls Claude makes, which the app passes on */
28
+ export declare const OAUTH_CALLS: readonly ["authorize", "token", "register", "revoke"];
29
+ export type OAuthCall = (typeof OAUTH_CALLS)[number];
30
+ /**
31
+ * What `GET /v1/agents/oauth/authorize` answers the app: the request Claude
32
+ * made, for the browser to go to the app's Allow page with, or where it
33
+ * goes back to Claude with an error
34
+ */
35
+ export declare const agent_authorized: z.ZodUnion<readonly [z.ZodObject<{
36
+ request: z.ZodObject<{
37
+ id: z.ZodUUID;
38
+ }, z.core.$strip>;
39
+ }, z.core.$strip>, z.ZodObject<{
40
+ redirect_to: z.ZodURL;
41
+ }, z.core.$strip>]>;
42
+ /** Why authorize sends nothing back to Claude: answered as plain words */
43
+ export declare const authorize_refused: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
44
+ error: z.ZodObject<{
45
+ reason: z.ZodLiteral<"unknown-client">;
46
+ message: z.ZodString;
47
+ }, z.core.$strip>;
48
+ }, z.core.$strip>, z.ZodTransform<{
49
+ reason: "unknown-client";
50
+ message: string;
51
+ }, {
52
+ error: {
53
+ reason: "unknown-client";
54
+ message: string;
55
+ };
56
+ }>>, z.ZodPipe<z.ZodObject<{
57
+ error: z.ZodObject<{
58
+ reason: z.ZodLiteral<"unknown-origin">;
59
+ message: z.ZodString;
60
+ }, z.core.$strip>;
61
+ }, z.core.$strip>, z.ZodTransform<{
62
+ reason: "unknown-origin";
63
+ message: string;
64
+ }, {
65
+ error: {
66
+ reason: "unknown-origin";
67
+ message: string;
68
+ };
69
+ }>>, z.ZodPipe<z.ZodObject<{
70
+ error: z.ZodObject<{
71
+ reason: z.ZodLiteral<"invalid">;
72
+ message: z.ZodString;
73
+ issues: z.ZodArray<z.ZodObject<{
74
+ path: z.ZodString;
75
+ message: z.ZodString;
76
+ }, z.core.$strip>>;
77
+ }, z.core.$strip>;
78
+ }, z.core.$strip>, z.ZodTransform<{
79
+ reason: "invalid";
80
+ message: string;
81
+ issues: {
82
+ path: string;
83
+ message: string;
84
+ }[];
85
+ }, {
86
+ error: {
87
+ reason: "invalid";
88
+ message: string;
89
+ issues: {
90
+ path: string;
91
+ message: string;
92
+ }[];
93
+ };
94
+ }>>]>;
95
+ /** `GET /v1/agents/requests/:request_id`: what the Allow page shows */
96
+ export declare const agent_request: z.ZodObject<{
97
+ request: z.ZodObject<{
98
+ id: z.ZodString;
99
+ client_name: z.ZodString;
100
+ scopes: z.ZodArray<z.ZodEnum<{
101
+ read: "read";
102
+ act: "act";
103
+ }>>;
104
+ resource: z.ZodString;
105
+ expires_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
106
+ }, z.core.$strip>;
107
+ }, z.core.$strip>;
108
+ export type AgentRequest = z.output<typeof agent_request>["request"];
109
+ /** `POST /v1/agents/requests/:request_id/allow`: who allowed what */
110
+ export declare const agent_allow: z.ZodObject<{
111
+ user_id: z.ZodString;
112
+ scopes: z.ZodArray<z.ZodEnum<{
113
+ read: "read";
114
+ act: "act";
115
+ }>>;
116
+ }, z.core.$strip>;
117
+ export type AgentAllow = z.input<typeof agent_allow>;
118
+ /** What allowing or denying answers: where the browser goes back to Claude */
119
+ export declare const agent_redirect: z.ZodObject<{
120
+ redirect_to: z.ZodURL;
121
+ }, z.core.$strip>;
122
+ /** `GET` and `DELETE /v1/agents/connections?user_id=…`: whose */
123
+ export declare const connections_query: z.ZodObject<{
124
+ user_id: z.ZodString;
125
+ }, z.core.$strip>;
126
+ /** One of Claude's connections, as the app's Connected list shows it */
127
+ export declare const agent_connection: z.ZodObject<{
128
+ id: z.ZodString;
129
+ user_id: z.ZodString;
130
+ client_name: z.ZodString;
131
+ scopes: z.ZodArray<z.ZodEnum<{
132
+ read: "read";
133
+ act: "act";
134
+ }>>;
135
+ resource: z.ZodString;
136
+ last_used_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
137
+ created_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
138
+ }, z.core.$strip>;
139
+ export type AgentConnection = z.output<typeof agent_connection>;
140
+ /** `GET /v1/agents/connections?user_id=…`: theirs, oldest first */
141
+ export declare const agent_connections: z.ZodObject<{
142
+ connections: z.ZodArray<z.ZodObject<{
143
+ id: z.ZodString;
144
+ user_id: z.ZodString;
145
+ client_name: z.ZodString;
146
+ scopes: z.ZodArray<z.ZodEnum<{
147
+ read: "read";
148
+ act: "act";
149
+ }>>;
150
+ resource: z.ZodString;
151
+ last_used_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
152
+ created_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
153
+ }, z.core.$strip>>;
154
+ }, z.core.$strip>;
155
+ /** What ending connections answers: how many it ended */
156
+ export declare const agent_ended: z.ZodObject<{
157
+ ended: z.ZodNumber;
158
+ }, z.core.$strip>;
159
+ /** Why Infra refused a call about a request or a connection */
160
+ export declare const agent_refused: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
161
+ error: z.ZodObject<{
162
+ reason: z.ZodLiteral<"not-found">;
163
+ message: z.ZodString;
164
+ }, z.core.$strip>;
165
+ }, z.core.$strip>, z.ZodTransform<{
166
+ reason: "not-found";
167
+ message: string;
168
+ }, {
169
+ error: {
170
+ reason: "not-found";
171
+ message: string;
172
+ };
173
+ }>>, z.ZodPipe<z.ZodObject<{
174
+ error: z.ZodObject<{
175
+ reason: z.ZodLiteral<"not-asked">;
176
+ message: z.ZodString;
177
+ }, z.core.$strip>;
178
+ }, z.core.$strip>, z.ZodTransform<{
179
+ reason: "not-asked";
180
+ message: string;
181
+ }, {
182
+ error: {
183
+ reason: "not-asked";
184
+ message: string;
185
+ };
186
+ }>>]>;
187
+ export type AgentRefused = z.output<typeof agent_refused>;
188
+ /**
189
+ * What an access token Infra signs says, beyond its issuer (the app's
190
+ * origin), audience (the address it's for), and expiry: whom Claude acts as
191
+ * (`sub`, the app's user ID), what they allowed (`scope`, space-separated),
192
+ * the project and mode, and the connection it's from
193
+ */
194
+ export declare const agent_claims: z.ZodObject<{
195
+ sub: z.ZodString;
196
+ scope: z.ZodString;
197
+ project: z.ZodString;
198
+ mode: z.ZodEnum<{
199
+ live: "live";
200
+ test: "test";
201
+ }>;
202
+ connection: z.ZodString;
203
+ }, z.core.$strip>;
@@ -0,0 +1,94 @@
1
+ import { z } from "zod";
2
+ import { invalid_refusal, not_found_refusal, refusal } from "./errors.js";
3
+ import { user_id } from "./people.js";
4
+ import { key_mode } from "./project.js";
5
+ // Claude logging in to an app's own MCP, through Infra, under
6
+ // `/v1/agents`: Infra is the OAuth server every app shares, so the app
7
+ // never builds one. Claude only ever talks to the app; the app's server
8
+ // passes its OAuth calls on to `/v1/agents/oauth/*`, its Allow page and
9
+ // account page call the rest, and it checks the tokens Infra signs with
10
+ // Infra's public keys, without calling Infra. Infra's README, "Claude",
11
+ // has the flow.
12
+ /** What someone on the app's team lets Claude do through its MCP */
13
+ export const AGENT_SCOPES = ["read", "act"];
14
+ export const agent_scope = z.enum(AGENT_SCOPES);
15
+ /**
16
+ * The header the app's server sends Claude's OAuth calls on with: where
17
+ * Claude reached the app, `https://vmdsystem.com`, its tokens' issuer.
18
+ * Only a header can carry it, since the call's body and query are Claude's.
19
+ */
20
+ export const APP_ORIGIN_HEADER = "App-Origin";
21
+ export const APP_PATHS = { oauth: "/oauth", allow: "/connect" };
22
+ /** The OAuth calls Claude makes, which the app passes on */
23
+ export const OAUTH_CALLS = ["authorize", "token", "register", "revoke"];
24
+ /**
25
+ * What `GET /v1/agents/oauth/authorize` answers the app: the request Claude
26
+ * made, for the browser to go to the app's Allow page with, or where it
27
+ * goes back to Claude with an error
28
+ */
29
+ export const agent_authorized = z.union([
30
+ z.object({ request: z.object({ id: z.uuid() }) }),
31
+ z.object({ redirect_to: z.url() })
32
+ ]);
33
+ /** Why authorize sends nothing back to Claude: answered as plain words */
34
+ export const authorize_refused = z.union([
35
+ refusal("unknown-client", {}),
36
+ refusal("unknown-origin", {}),
37
+ invalid_refusal
38
+ ]);
39
+ /** `GET /v1/agents/requests/:request_id`: what the Allow page shows */
40
+ export const agent_request = z.object({
41
+ request: z.object({
42
+ id: z.string(),
43
+ client_name: z.string(),
44
+ scopes: z.array(agent_scope),
45
+ // The address Claude asked to use, which its tokens are good for:
46
+ // the app's MCP
47
+ resource: z.string(),
48
+ expires_at: z.iso.datetime().transform((value) => new Date(value))
49
+ })
50
+ });
51
+ /** `POST /v1/agents/requests/:request_id/allow`: who allowed what */
52
+ export const agent_allow = z.object({
53
+ user_id,
54
+ scopes: z.array(agent_scope).min(1)
55
+ });
56
+ /** What allowing or denying answers: where the browser goes back to Claude */
57
+ export const agent_redirect = z.object({ redirect_to: z.url() });
58
+ /** `GET` and `DELETE /v1/agents/connections?user_id=…`: whose */
59
+ export const connections_query = z.object({ user_id });
60
+ /** One of Claude's connections, as the app's Connected list shows it */
61
+ export const agent_connection = z.object({
62
+ id: z.string(),
63
+ user_id: z.string(),
64
+ client_name: z.string(),
65
+ scopes: z.array(agent_scope),
66
+ resource: z.string(),
67
+ // When Claude last refreshed it, to within about ten minutes
68
+ last_used_at: z.iso.datetime().transform((value) => new Date(value)),
69
+ created_at: z.iso.datetime().transform((value) => new Date(value))
70
+ });
71
+ /** `GET /v1/agents/connections?user_id=…`: theirs, oldest first */
72
+ export const agent_connections = z.object({
73
+ connections: z.array(agent_connection)
74
+ });
75
+ /** What ending connections answers: how many it ended */
76
+ export const agent_ended = z.object({ ended: z.number().int() });
77
+ /** Why Infra refused a call about a request or a connection */
78
+ export const agent_refused = z.union([
79
+ not_found_refusal,
80
+ refusal("not-asked", {})
81
+ ]);
82
+ /**
83
+ * What an access token Infra signs says, beyond its issuer (the app's
84
+ * origin), audience (the address it's for), and expiry: whom Claude acts as
85
+ * (`sub`, the app's user ID), what they allowed (`scope`, space-separated),
86
+ * the project and mode, and the connection it's from
87
+ */
88
+ export const agent_claims = z.object({
89
+ sub: z.string(),
90
+ scope: z.string(),
91
+ project: z.string(),
92
+ mode: key_mode,
93
+ connection: z.string()
94
+ });