apps 1.0.0-beta.3 → 2.0.0-next.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.
- package/README.md +10 -48
- package/check-publish.mjs +7 -0
- package/package.json +27 -56
- package/src/Access.ts +37 -0
- package/src/Accounts.ts +468 -0
- package/src/App.ts +11 -0
- package/src/Ask.ts +156 -0
- package/src/Changes.ts +13 -0
- package/src/Http.ts +30 -0
- package/src/Provider.ts +146 -0
- package/src/Session.ts +52 -0
- package/src/Settings.ts +164 -0
- package/src/contract.ts +852 -0
- package/src/css-modules.d.ts +2 -0
- package/src/desktop/Desktop.ts +192 -0
- package/src/desktop/bridge.ts +62 -0
- package/src/desktop/errors.ts +88 -0
- package/src/desktop/index.ts +15 -0
- package/src/entry.ts +48 -0
- package/src/index.ts +16 -4
- package/src/internal/defects.ts +62 -0
- package/src/internal/files.ts +369 -0
- package/src/internal/host.ts +17 -0
- package/src/internal/query.ts +114 -0
- package/src/internal/records.ts +838 -0
- package/src/internal/retention.ts +87 -0
- package/src/internal/retries.ts +185 -0
- package/src/internal/telemetry.ts +167 -0
- package/src/migrations.ts +110 -0
- package/src/page/Accounts.ts +27 -0
- package/src/page/Changes.ts +126 -0
- package/src/page/Session.ts +46 -0
- package/src/page/client.ts +107 -0
- package/src/page/errors.ts +15 -0
- package/src/page/http.ts +21 -0
- package/src/page/index.ts +18 -0
- package/src/page/paths.ts +22 -0
- package/src/providers/anthropic.ts +26 -0
- package/src/providers/github.ts +49 -0
- package/src/providers/google.ts +67 -0
- package/src/providers/openai.ts +62 -0
- package/src/providers/slack.ts +81 -0
- package/src/runtime.ts +1540 -0
- package/src/testing.ts +660 -0
- package/src/text-modules.d.ts +5 -0
- package/src/theme.css +50 -0
- package/dist/authoring/index.d.ts +0 -393
- package/dist/authoring/index.js +0 -36
- package/dist/chunk-4OZFMCTT.js +0 -17
- package/dist/chunk-A3H32P2J.js +0 -143
- package/dist/chunk-B7NEUIJL.js +0 -14
- package/dist/chunk-F7GZYEAC.js +0 -41
- package/dist/chunk-MUFMJRXR.js +0 -308
- package/dist/chunk-S5KFP4JQ.js +0 -20
- package/dist/chunk-Y6LLTN7W.js +0 -99
- package/dist/chunk-YUZ4TXOY.js +0 -27
- package/dist/http/index.d.ts +0 -9
- package/dist/http/index.js +0 -6
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -48
- package/dist/manifest/contract.d.ts +0 -63
- package/dist/manifest/contract.js +0 -8
- package/dist/manifest/index.d.ts +0 -20
- package/dist/manifest/index.js +0 -16
- package/dist/mcp/client.d.ts +0 -9
- package/dist/mcp/index.d.ts +0 -11
- package/dist/mcp/index.js +0 -7
- package/dist/openapi/index.d.ts +0 -14
- package/dist/openapi/index.js +0 -7
- package/dist/openapi/mapper.d.ts +0 -7
- package/dist/runner/index.d.ts +0 -4
- package/dist/runner/index.js +0 -66
- package/dist/runner/protocol.d.ts +0 -33
- package/dist/runner/protocol.js +0 -8
- package/dist/runtime/index.d.ts +0 -83
- package/dist/runtime/index.js +0 -12
- package/dist/workerd/index.d.ts +0 -7
- package/dist/workerd/index.js +0 -10
- package/src/authoring/index.ts +0 -658
- package/src/http/index.ts +0 -26
- package/src/manifest/contract.ts +0 -64
- package/src/manifest/index.ts +0 -156
- package/src/mcp/client.ts +0 -59
- package/src/mcp/index.ts +0 -42
- package/src/openapi/index.ts +0 -70
- package/src/openapi/mapper.ts +0 -25
- package/src/runner/index.ts +0 -80
- package/src/runner/protocol.ts +0 -54
- package/src/runtime/index.ts +0 -550
- package/src/workerd/index.ts +0 -17
package/src/authoring/index.ts
DELETED
|
@@ -1,658 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Synchronous authoring primitives imported by app.ts and tools/*.ts.
|
|
3
|
-
* Authored modules record plain claims without I/O or user context; the
|
|
4
|
-
* manifest loader owns execution, validation, and effects.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// ── auth options (auth methods) ──────────────────────────────────────────────
|
|
8
|
-
//
|
|
9
|
-
// An auth method states the credential's kind, never its placement.
|
|
10
|
-
// bearer/basic/oauth have protocol-defined spend semantics. secrets()
|
|
11
|
-
// is the anonymous bag whose spend must be authored at the surface.
|
|
12
|
-
|
|
13
|
-
/** A single opaque credential with RFC 6750 bearer semantics. */
|
|
14
|
-
export interface BearerMethod {
|
|
15
|
-
readonly kind: "bearer";
|
|
16
|
-
readonly label: string;
|
|
17
|
-
readonly help?: string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** An RFC 7617 username/password pair. */
|
|
21
|
-
export interface BasicMethod {
|
|
22
|
-
readonly kind: "basic";
|
|
23
|
-
readonly label: string;
|
|
24
|
-
readonly help?: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface SecretsMethod<
|
|
28
|
-
F extends Readonly<Record<string, SecretField>> = Readonly<Record<string, SecretField>>,
|
|
29
|
-
> {
|
|
30
|
-
readonly kind: "secrets";
|
|
31
|
-
readonly label: string;
|
|
32
|
-
/** Markdown shown under this auth method on the connect page, for example:
|
|
33
|
-
* "Create a fine-grained token at github.com/settings/tokens." */
|
|
34
|
-
readonly help?: string;
|
|
35
|
-
readonly fields: F;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface SecretField {
|
|
39
|
-
readonly label: string;
|
|
40
|
-
/** Form masking. Stated, never guessed from the field name. */
|
|
41
|
-
readonly secret: boolean;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* An oauth auth method is a bearer method with acquisition strategies
|
|
46
|
-
* layered on: the material contract is always {token, refresh_token?,
|
|
47
|
-
* expires_at?}, and the host fills it by the best strategy it can offer
|
|
48
|
-
* — authorization-code ceremony, device-code ceremony, or paste. Paste
|
|
49
|
-
* is the always-on floor (a pasted token spends identically), so the
|
|
50
|
-
* auth method works on any deployment; ceremony is progressive enhancement.
|
|
51
|
-
* Handlers cannot tell how material arrived.
|
|
52
|
-
*/
|
|
53
|
-
export interface OAuthMethod {
|
|
54
|
-
readonly kind: "oauth";
|
|
55
|
-
readonly label: string;
|
|
56
|
-
/** Markdown shown under this auth method on the connect page, for example:
|
|
57
|
-
* "Create a fine-grained token at github.com/settings/tokens." */
|
|
58
|
-
readonly help?: string;
|
|
59
|
-
/** Endpoints only when the service publishes no well-knowns —
|
|
60
|
-
* discovery (RFC 9728 + AS metadata) outranks these when present.
|
|
61
|
-
* Authors state facts about the service, never about the client. */
|
|
62
|
-
readonly authorizationEndpoint?: string;
|
|
63
|
-
readonly tokenEndpoint?: string;
|
|
64
|
-
readonly scopes?: readonly string[];
|
|
65
|
-
/** Set false only when a user-pasted token genuinely cannot work
|
|
66
|
-
* (audience-bound tokens, DPoP). Stated, never inferred. */
|
|
67
|
-
readonly paste?: false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export type AuthMethod = BearerMethod | BasicMethod | SecretsMethod | OAuthMethod;
|
|
71
|
-
|
|
72
|
-
export const bearer = (opts: {
|
|
73
|
-
readonly label: string;
|
|
74
|
-
readonly help?: string;
|
|
75
|
-
}): BearerMethod => ({ kind: "bearer", ...opts });
|
|
76
|
-
|
|
77
|
-
export const basic = (opts: {
|
|
78
|
-
readonly label: string;
|
|
79
|
-
readonly help?: string;
|
|
80
|
-
}): BasicMethod => ({ kind: "basic", ...opts });
|
|
81
|
-
|
|
82
|
-
export const secrets = <const F extends Readonly<Record<string, SecretField>>>(opts: {
|
|
83
|
-
readonly label: string;
|
|
84
|
-
/** Markdown shown under this auth method on the connect page, for example:
|
|
85
|
-
* "Create a fine-grained token at github.com/settings/tokens." */
|
|
86
|
-
readonly help?: string;
|
|
87
|
-
readonly fields: F;
|
|
88
|
-
}): SecretsMethod<F> => ({ kind: "secrets", ...opts });
|
|
89
|
-
|
|
90
|
-
export const oauth = (opts: {
|
|
91
|
-
readonly label: string;
|
|
92
|
-
/** Markdown shown under this auth method on the connect page, for example:
|
|
93
|
-
* "Create a fine-grained token at github.com/settings/tokens." */
|
|
94
|
-
readonly help?: string;
|
|
95
|
-
readonly authorizationEndpoint?: string;
|
|
96
|
-
readonly tokenEndpoint?: string;
|
|
97
|
-
readonly scopes?: readonly string[];
|
|
98
|
-
readonly paste?: false;
|
|
99
|
-
}): OAuthMethod => ({ kind: "oauth", ...opts });
|
|
100
|
-
|
|
101
|
-
// ── services ──────────────────────────────────────────────────────────
|
|
102
|
-
|
|
103
|
-
export interface ServiceDecl {
|
|
104
|
-
/** Canonical origin, possibly templated: "https://{region}.posthog.com". */
|
|
105
|
-
readonly origin: string;
|
|
106
|
-
/** Hosts beyond the origin the credential legitimately travels to. */
|
|
107
|
-
readonly reach?: readonly string[];
|
|
108
|
-
/** Connection-scoped non-secret connect-form values; keys may fill
|
|
109
|
-
* origin templates. */
|
|
110
|
-
readonly params?: Readonly<Record<string, ParamSpec>>;
|
|
111
|
-
/** Named auth methods. Key = the grant name tools switch on. */
|
|
112
|
-
readonly auth?: Readonly<Record<string, AuthMethod>>;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export interface ParamSpec {
|
|
116
|
-
readonly label: string;
|
|
117
|
-
readonly options?: readonly string[];
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** What the manifest pipeline reads off a requirement value. */
|
|
121
|
-
export interface RequirementSpec {
|
|
122
|
-
readonly service: ServiceDecl;
|
|
123
|
-
readonly arity: "one" | "optional" | "many";
|
|
124
|
-
readonly grant?: string;
|
|
125
|
-
readonly description?: string;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const RequirementKey: unique symbol = Symbol.for("executor/apps/requirement");
|
|
129
|
-
|
|
130
|
-
declare const RequirementPhantom: unique symbol;
|
|
131
|
-
|
|
132
|
-
/** Requirement arity, tracked at the type level so handler contexts
|
|
133
|
-
* know whether a role arrives as one entry, maybe-one, or a list. */
|
|
134
|
-
export type RequirementArity = "one" | "optional" | "many";
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* A requirement value in a tool's `requirements` record. A Service IS
|
|
138
|
-
* a requirement (bare value = arity "one"); .optional()/.array()
|
|
139
|
-
* return terminal Requirements so nonsense chains do not typecheck.
|
|
140
|
-
* The phantom property is never assigned — it exists so auth-compose
|
|
141
|
-
* callbacks and handler contexts can recover the service's grants, a
|
|
142
|
-
* .grant() pin, and the arity at the type level.
|
|
143
|
-
*/
|
|
144
|
-
export interface Requirement<
|
|
145
|
-
D extends ServiceDecl = ServiceDecl,
|
|
146
|
-
G extends string | undefined = string | undefined,
|
|
147
|
-
A extends RequirementArity = RequirementArity,
|
|
148
|
-
> {
|
|
149
|
-
readonly [RequirementKey]: RequirementSpec;
|
|
150
|
-
readonly [RequirementPhantom]?: {
|
|
151
|
-
readonly decl: D;
|
|
152
|
-
readonly pinned: G;
|
|
153
|
-
readonly arity: A;
|
|
154
|
-
};
|
|
155
|
-
describe(text: string): Requirement<D, G, A>;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export interface Service<
|
|
159
|
-
D extends ServiceDecl = ServiceDecl,
|
|
160
|
-
G extends (keyof NonNullable<D["auth"]> & string) | undefined = undefined,
|
|
161
|
-
> extends Requirement<D, G, "one"> {
|
|
162
|
-
readonly decl: D;
|
|
163
|
-
optional(): Requirement<D, G, "optional">;
|
|
164
|
-
array(): Requirement<D, G, "many">;
|
|
165
|
-
/** Pin one auth method: this tool supports only the named grant. */
|
|
166
|
-
grant<N extends keyof NonNullable<D["auth"]> & string>(name: N): Service<D, N>;
|
|
167
|
-
describe(text: string): Service<D, G>;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/** A publish-time authoring problem accumulated without interrupting defines. */
|
|
171
|
-
export interface Problem {
|
|
172
|
-
readonly message: string;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// ── dynamic tool sets ─────────────────────────────────────────────────
|
|
176
|
-
|
|
177
|
-
/** One connected role, discriminated by the declared auth method kind. */
|
|
178
|
-
export type AuthRoleEntry<D extends ServiceDecl> = {
|
|
179
|
-
[N in keyof NonNullable<D["auth"]> & string]: NonNullable<D["auth"]>[N] extends SecretsMethod<
|
|
180
|
-
infer F
|
|
181
|
-
>
|
|
182
|
-
? {
|
|
183
|
-
readonly grant: N;
|
|
184
|
-
readonly kind: "secrets";
|
|
185
|
-
readonly secrets: { readonly [K in keyof F & string]: string };
|
|
186
|
-
}
|
|
187
|
-
: NonNullable<D["auth"]>[N] extends BasicMethod
|
|
188
|
-
? {
|
|
189
|
-
readonly grant: N;
|
|
190
|
-
readonly kind: "basic";
|
|
191
|
-
readonly username: string;
|
|
192
|
-
readonly password: string;
|
|
193
|
-
}
|
|
194
|
-
: { readonly grant: N; readonly kind: "bearer" | "oauth"; readonly token: string };
|
|
195
|
-
}[keyof NonNullable<D["auth"]> & string];
|
|
196
|
-
|
|
197
|
-
/** Kind-discriminated credential material accepted by surface helpers. */
|
|
198
|
-
export type RoleMaterial =
|
|
199
|
-
| { readonly kind: "bearer" | "oauth"; readonly token: string }
|
|
200
|
-
| { readonly kind: "basic"; readonly username: string; readonly password: string }
|
|
201
|
-
| { readonly kind: "secrets"; readonly secrets: Readonly<Record<string, string>> };
|
|
202
|
-
|
|
203
|
-
/** Recover the connected role type produced by a service requirement. */
|
|
204
|
-
export type RoleOf<
|
|
205
|
-
S,
|
|
206
|
-
G extends string | undefined = undefined,
|
|
207
|
-
> = S extends Requirement<infer D, infer Pin>
|
|
208
|
-
? [G] extends [string]
|
|
209
|
-
? Extract<AuthRoleEntry<D>, { grant: G }>
|
|
210
|
-
: Pin extends string
|
|
211
|
-
? Extract<AuthRoleEntry<D>, { grant: Pin }>
|
|
212
|
-
: AuthRoleEntry<D>
|
|
213
|
-
: never;
|
|
214
|
-
|
|
215
|
-
/** Role record received by an authored authentication placement callback. */
|
|
216
|
-
export type AuthRoles<Reqs extends Readonly<Record<string, Requirement>>> = {
|
|
217
|
-
readonly [K in keyof Reqs]: NonNullable<Reqs[K][typeof RequirementPhantom]> extends {
|
|
218
|
-
readonly decl: infer D extends ServiceDecl;
|
|
219
|
-
readonly pinned: infer G;
|
|
220
|
-
}
|
|
221
|
-
? G extends string
|
|
222
|
-
? Extract<AuthRoleEntry<D>, { grant: G }>
|
|
223
|
-
: AuthRoleEntry<D>
|
|
224
|
-
: never;
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
/** Request placement returned by an authored dynamic auth callback. */
|
|
228
|
-
export interface AuthPlacement {
|
|
229
|
-
readonly headers?: HeadersInit;
|
|
230
|
-
readonly searchParams?: URLSearchParams | Readonly<Record<string, string>>;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
export type AuthComposeFn<Reqs extends Readonly<Record<string, Requirement>> = never> = (
|
|
234
|
-
roles: AuthRoles<Reqs>,
|
|
235
|
-
) => AuthPlacement | undefined | void;
|
|
236
|
-
|
|
237
|
-
export type HasSecretsGrant<D extends ServiceDecl> = true extends (
|
|
238
|
-
NonNullable<D["auth"]>[keyof NonNullable<D["auth"]>] extends infer M
|
|
239
|
-
? M extends SecretsMethod
|
|
240
|
-
? true
|
|
241
|
-
: false
|
|
242
|
-
: never
|
|
243
|
-
)
|
|
244
|
-
? true
|
|
245
|
-
: false;
|
|
246
|
-
|
|
247
|
-
/** Whether an OpenAPI wrapper must state credential placement explicitly. */
|
|
248
|
-
export type RequiresAuthCompose<Reqs extends Readonly<Record<string, Requirement>>> = true extends {
|
|
249
|
-
[K in keyof Reqs]: NonNullable<Reqs[K][typeof RequirementPhantom]> extends {
|
|
250
|
-
readonly decl: infer D extends ServiceDecl;
|
|
251
|
-
readonly pinned: infer G;
|
|
252
|
-
}
|
|
253
|
-
? G extends keyof NonNullable<D["auth"]> & string
|
|
254
|
-
? NonNullable<D["auth"]>[G] extends SecretsMethod
|
|
255
|
-
? true
|
|
256
|
-
: false
|
|
257
|
-
: HasSecretsGrant<D>
|
|
258
|
-
: never;
|
|
259
|
-
}[keyof Reqs]
|
|
260
|
-
? true
|
|
261
|
-
: false;
|
|
262
|
-
|
|
263
|
-
/** Record a wrapper-specific publish problem on the active authored file. @internal */
|
|
264
|
-
export const recordAuthoringProblem = (message: string): void => {
|
|
265
|
-
const file = sourceFileFor("wrapper");
|
|
266
|
-
activeRegistry.problems.push({ message: `${file}: ${message}` });
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
/** Operation curation for a dynamic tool set. Exactly one of allow/block. */
|
|
270
|
-
export type OperationSelection =
|
|
271
|
-
| { readonly allow: readonly string[]; readonly block?: undefined }
|
|
272
|
-
| { readonly block: readonly string[]; readonly allow?: undefined };
|
|
273
|
-
|
|
274
|
-
/** One tool emitted by a dynamic discovery pass. */
|
|
275
|
-
export interface DiscoveredToolSpec {
|
|
276
|
-
readonly name: string;
|
|
277
|
-
readonly description: string;
|
|
278
|
-
readonly input: SchemaLike;
|
|
279
|
-
readonly annotations?: ToolAnnotations;
|
|
280
|
-
readonly detail?: unknown;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* The generic authored shape of a dynamic pair: ctx is inferred from
|
|
285
|
-
* the requirements record exactly as for static tools. Discovered
|
|
286
|
-
* inputs are runtime data (the schema arrives from the upstream at
|
|
287
|
-
* discover time), so `call`'s input is honestly `unknown` — the
|
|
288
|
-
* runtime validates against the discovered descriptor before calling.
|
|
289
|
-
*/
|
|
290
|
-
export interface DynamicToolsDecl<
|
|
291
|
-
Reqs extends Readonly<Record<string, Requirement>> | undefined =
|
|
292
|
-
| Readonly<Record<string, Requirement>>
|
|
293
|
-
| undefined,
|
|
294
|
-
> {
|
|
295
|
-
readonly description: string;
|
|
296
|
-
readonly requirements?: Reqs;
|
|
297
|
-
discover(ctx: HandlerCtx<Reqs>): Promise<readonly DiscoveredToolSpec[]>;
|
|
298
|
-
call(
|
|
299
|
-
tool: { readonly name: string; readonly detail: unknown },
|
|
300
|
-
input: unknown,
|
|
301
|
-
ctx: HandlerCtx<Reqs>,
|
|
302
|
-
): Promise<unknown>;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* The erased registry row of a dynamic pair — same storage boundary
|
|
307
|
-
* as ToolSpec. App code: runs where handlers run (in-process locally,
|
|
308
|
-
* inside the isolate on workerd) and places credentials itself from
|
|
309
|
-
* ctx, like any handler. `discover` interrogates the tool source;
|
|
310
|
-
* `call` executes one discovered tool from the identity and detail
|
|
311
|
-
* discover emitted.
|
|
312
|
-
*/
|
|
313
|
-
export interface DynamicToolsSpec {
|
|
314
|
-
readonly description: string;
|
|
315
|
-
readonly requirements?: Readonly<Record<string, Requirement>>;
|
|
316
|
-
discover(ctx: never): Promise<readonly DiscoveredToolSpec[]>;
|
|
317
|
-
call(
|
|
318
|
-
tool: { readonly name: string; readonly detail: unknown },
|
|
319
|
-
input: never,
|
|
320
|
-
ctx: never,
|
|
321
|
-
): Promise<unknown>;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
const DynamicKey: unique symbol = Symbol.for("executor/apps/dynamic");
|
|
325
|
-
|
|
326
|
-
/** The value defineDynamicTools returns: an opaque handle a tool file
|
|
327
|
-
* default-exports. The spec is undefined when the definition was
|
|
328
|
-
* invalid (a problem was recorded instead). */
|
|
329
|
-
export interface DynamicToolSet {
|
|
330
|
-
readonly [DynamicKey]: DynamicToolsSpec | undefined;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/** Reads a dynamic spec from a tool file's default export. @internal */
|
|
334
|
-
export const dynamicSpecOf = (value: unknown): DynamicToolsSpec | undefined =>
|
|
335
|
-
typeof value === "object" && value !== null && DynamicKey in value
|
|
336
|
-
? (value as DynamicToolSet)[DynamicKey]
|
|
337
|
-
: undefined;
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* The dynamic-tools primitive: an author-written discover/call pair
|
|
341
|
-
* whose tool list is interrogated at runtime, per connection, instead
|
|
342
|
-
* of authored one file at a time. The file's name becomes the address
|
|
343
|
-
* segment (appKey/<stem>/<toolName>); tools land in the catalog after
|
|
344
|
-
* discovery and never block a save. fromMCP/fromOpenAPI are shipped
|
|
345
|
-
* pairs over this same primitive.
|
|
346
|
-
*/
|
|
347
|
-
export const defineDynamicTools = <
|
|
348
|
-
const Reqs extends Readonly<Record<string, Requirement>> | undefined = undefined,
|
|
349
|
-
>(decl: DynamicToolsDecl<Reqs>): DynamicToolSet => {
|
|
350
|
-
// SAFETY: same erasure boundary as defineTool — members identical,
|
|
351
|
-
// handler parameter types narrow to the registry's untyped floor.
|
|
352
|
-
const spec = decl as unknown as DynamicToolsSpec;
|
|
353
|
-
const file = sourceFileFor("defineDynamicTools(...)");
|
|
354
|
-
const problems: string[] = [];
|
|
355
|
-
|
|
356
|
-
if (typeof spec.description !== "string" || spec.description.trim().length === 0)
|
|
357
|
-
problems.push("dynamic tool set description must not be empty");
|
|
358
|
-
|
|
359
|
-
const roles = Object.entries(spec.requirements ?? {});
|
|
360
|
-
if (roles.length > 1) problems.push("a dynamic tool set takes exactly one requirement role");
|
|
361
|
-
else if (roles.length === 1) {
|
|
362
|
-
const [role, value] = roles[0]!;
|
|
363
|
-
const requirementSpec = requirementSpecOf(value);
|
|
364
|
-
if (requirementSpec === undefined)
|
|
365
|
-
problems.push(`requirement '${role}' is not a service/requirement value`);
|
|
366
|
-
else if (requirementSpec.arity === "many")
|
|
367
|
-
problems.push(
|
|
368
|
-
`requirement '${role}' cannot be .array() — multiple accounts expand the catalog per connection, never merge one list`,
|
|
369
|
-
);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
if (problems.length > 0) {
|
|
373
|
-
activeRegistry.problems.push(...problems.map((message) => ({ message: `${file}: ${message}` })));
|
|
374
|
-
return { [DynamicKey]: undefined };
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
if (activeRegistry.dynamic.has(file))
|
|
378
|
-
activeRegistry.problems.push({
|
|
379
|
-
message: `${file}: one dynamic tool set per file — its address segment is the file's name`,
|
|
380
|
-
});
|
|
381
|
-
else activeRegistry.dynamic.set(file, { spec, file });
|
|
382
|
-
return { [DynamicKey]: spec };
|
|
383
|
-
};
|
|
384
|
-
|
|
385
|
-
/** A registered dynamic tool set and its source attribution. @internal */
|
|
386
|
-
export interface DynamicRegistration {
|
|
387
|
-
readonly spec: DynamicToolsSpec;
|
|
388
|
-
readonly file: string;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/** A registered service declaration and its source attribution. @internal */
|
|
392
|
-
export interface RegisteredService {
|
|
393
|
-
readonly decl: ServiceDecl;
|
|
394
|
-
readonly file: string;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/** A registered tool declaration and its source attribution. @internal */
|
|
398
|
-
export interface RegisteredTool {
|
|
399
|
-
readonly spec: ToolSpec;
|
|
400
|
-
readonly file: string;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/** Mutable claim registry populated while authored modules execute. @internal */
|
|
404
|
-
export interface Registry {
|
|
405
|
-
app: AppManifest | undefined;
|
|
406
|
-
readonly services: Map<string, RegisteredService>;
|
|
407
|
-
readonly tools: Map<string, RegisteredTool>;
|
|
408
|
-
readonly dynamic: Map<string, DynamicRegistration>;
|
|
409
|
-
readonly problems: Problem[];
|
|
410
|
-
readonly toolFiles: Map<
|
|
411
|
-
string,
|
|
412
|
-
{ definitions: number; hasUnnamedTool: boolean }
|
|
413
|
-
>;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
/** Creates an isolated empty authoring registry. @internal */
|
|
417
|
-
export const newRegistry = (): Registry => ({
|
|
418
|
-
app: undefined,
|
|
419
|
-
services: new Map(),
|
|
420
|
-
tools: new Map(),
|
|
421
|
-
dynamic: new Map(),
|
|
422
|
-
problems: [],
|
|
423
|
-
toolFiles: new Map(),
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
let activeRegistry = newRegistry();
|
|
427
|
-
let currentFile: string | undefined;
|
|
428
|
-
|
|
429
|
-
/** Swaps the registry receiving subsequent authoring claims. @internal */
|
|
430
|
-
export const setActiveRegistry = (registry: Registry): Registry => {
|
|
431
|
-
const previous = activeRegistry;
|
|
432
|
-
activeRegistry = registry;
|
|
433
|
-
return previous;
|
|
434
|
-
};
|
|
435
|
-
|
|
436
|
-
/** Sets source-file attribution for subsequent authoring claims. @internal */
|
|
437
|
-
export const __enterFile = (path: string): void => {
|
|
438
|
-
currentFile = path;
|
|
439
|
-
};
|
|
440
|
-
|
|
441
|
-
/** Clears source-file attribution after an authored module import. @internal */
|
|
442
|
-
export const __leaveFile = (): void => {
|
|
443
|
-
currentFile = undefined;
|
|
444
|
-
};
|
|
445
|
-
|
|
446
|
-
const sourceFileFor = (claim: string): string => {
|
|
447
|
-
if (currentFile !== undefined) return currentFile;
|
|
448
|
-
activeRegistry.problems.push({
|
|
449
|
-
message: `${claim} called outside an authored module import`,
|
|
450
|
-
});
|
|
451
|
-
return "<unattributed>";
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
const makeTerminal = <
|
|
455
|
-
D extends ServiceDecl,
|
|
456
|
-
G extends string | undefined,
|
|
457
|
-
A extends RequirementArity,
|
|
458
|
-
>(spec: RequirementSpec): Requirement<D, G, A> => ({
|
|
459
|
-
[RequirementKey]: spec,
|
|
460
|
-
describe: (text) => makeTerminal<D, G, A>({ ...spec, description: text }),
|
|
461
|
-
});
|
|
462
|
-
|
|
463
|
-
export const service = <const D extends ServiceDecl>(decl: D): Service<D> => {
|
|
464
|
-
const file = sourceFileFor("service(...)");
|
|
465
|
-
const make = <G extends (keyof NonNullable<D["auth"]> & string) | undefined>(
|
|
466
|
-
spec: RequirementSpec,
|
|
467
|
-
): Service<D, G> => ({
|
|
468
|
-
[RequirementKey]: spec,
|
|
469
|
-
decl,
|
|
470
|
-
optional: () => makeTerminal<D, G, "optional">({ ...spec, arity: "optional" }),
|
|
471
|
-
array: () => makeTerminal<D, G, "many">({ ...spec, arity: "many" }),
|
|
472
|
-
grant: <N extends keyof NonNullable<D["auth"]> & string>(name: N) =>
|
|
473
|
-
make<N>({ ...spec, grant: name }),
|
|
474
|
-
describe: (text) => make<G>({ ...spec, description: text }),
|
|
475
|
-
});
|
|
476
|
-
const value = make<undefined>({ service: decl, arity: "one" });
|
|
477
|
-
if (!activeRegistry.services.has(decl.origin))
|
|
478
|
-
activeRegistry.services.set(decl.origin, { decl, file });
|
|
479
|
-
return value;
|
|
480
|
-
};
|
|
481
|
-
|
|
482
|
-
const isRequirement = (value: unknown): value is Requirement =>
|
|
483
|
-
typeof value === "object" && value !== null && RequirementKey in value;
|
|
484
|
-
|
|
485
|
-
const isService = (value: unknown): value is Service =>
|
|
486
|
-
isRequirement(value) &&
|
|
487
|
-
"decl" in value &&
|
|
488
|
-
"optional" in value &&
|
|
489
|
-
"array" in value &&
|
|
490
|
-
"grant" in value;
|
|
491
|
-
|
|
492
|
-
export const requirementSpecOf = (value: unknown): RequirementSpec | undefined =>
|
|
493
|
-
isService(value) || isRequirement(value) ? value[RequirementKey] : undefined;
|
|
494
|
-
|
|
495
|
-
// ── apps ──────────────────────────────────────────────────────────────
|
|
496
|
-
|
|
497
|
-
export interface AppManifest {
|
|
498
|
-
readonly kind: "executor-app";
|
|
499
|
-
readonly key: string;
|
|
500
|
-
readonly label: string;
|
|
501
|
-
readonly description: string;
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
export const defineApp = (manifest: {
|
|
505
|
-
readonly key: string;
|
|
506
|
-
readonly label: string;
|
|
507
|
-
readonly description: string;
|
|
508
|
-
}): AppManifest => {
|
|
509
|
-
const file = sourceFileFor("defineApp(...)");
|
|
510
|
-
const value: AppManifest = { kind: "executor-app", ...manifest };
|
|
511
|
-
if (activeRegistry.app === undefined) activeRegistry.app = value;
|
|
512
|
-
else activeRegistry.problems.push({ message: `${file}: duplicate defineApp(...)` });
|
|
513
|
-
return value;
|
|
514
|
-
};
|
|
515
|
-
|
|
516
|
-
// ── tools ─────────────────────────────────────────────────────────────
|
|
517
|
-
|
|
518
|
-
/** Any Standard Schema (zod preferred) or a raw JSON-schema object. */
|
|
519
|
-
export type SchemaLike = object;
|
|
520
|
-
|
|
521
|
-
/**
|
|
522
|
-
* The validated value a Standard Schema produces, recovered from its
|
|
523
|
-
* phantom types (zod 4 and effect Schema both publish them). A raw
|
|
524
|
-
* JSON-schema object carries no phantom, so its input is `unknown` —
|
|
525
|
-
* stated weakness, not silent weakness.
|
|
526
|
-
*/
|
|
527
|
-
export type InferSchema<S> = S extends {
|
|
528
|
-
readonly "~standard": { readonly types?: infer T };
|
|
529
|
-
}
|
|
530
|
-
? NonNullable<T> extends { readonly output: infer O }
|
|
531
|
-
? O
|
|
532
|
-
: unknown
|
|
533
|
-
: unknown;
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* The capability a handler receives for one connected requirement
|
|
537
|
-
* role: the grant's kind-discriminated material (narrowed by any
|
|
538
|
-
* .grant() pin) plus the spending fetch. Arity shapes the value —
|
|
539
|
-
* `.optional()` may be absent, `.array()` is every connected account.
|
|
540
|
-
*/
|
|
541
|
-
export type HandlerRole<R> = R extends Requirement<infer D, infer G, infer A>
|
|
542
|
-
? [A] extends ["many"]
|
|
543
|
-
? ReadonlyArray<HandlerRoleEntry<D, G>>
|
|
544
|
-
: [A] extends ["optional"]
|
|
545
|
-
? HandlerRoleEntry<D, G> | undefined
|
|
546
|
-
: HandlerRoleEntry<D, G>
|
|
547
|
-
: never;
|
|
548
|
-
|
|
549
|
-
/** One connected role entry: declared-auth material + capability fetch. */
|
|
550
|
-
export type HandlerRoleEntry<
|
|
551
|
-
D extends ServiceDecl,
|
|
552
|
-
G extends string | undefined,
|
|
553
|
-
> = ([G] extends [string] ? Extract<AuthRoleEntry<D>, { grant: G }> : AuthRoleEntry<D>) & {
|
|
554
|
-
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
555
|
-
};
|
|
556
|
-
|
|
557
|
-
/** The ctx record a handler receives, keyed by requirement role. */
|
|
558
|
-
export type HandlerCtx<Reqs> = [Reqs] extends [Readonly<Record<string, Requirement>>]
|
|
559
|
-
? { readonly [K in keyof Reqs]: HandlerRole<Reqs[K]> }
|
|
560
|
-
: Readonly<Record<never, never>>;
|
|
561
|
-
|
|
562
|
-
export interface ToolAnnotations {
|
|
563
|
-
readonly readOnly?: boolean;
|
|
564
|
-
readonly destructive?: boolean;
|
|
565
|
-
/** Facts, not flow: shortfall surfaces as the scope challenge. */
|
|
566
|
-
readonly scopes?: readonly string[];
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
/**
|
|
570
|
-
* The generic authored shape: input/ctx/output are all inferred — the
|
|
571
|
-
* handler's input from the Standard Schema, ctx from the requirements
|
|
572
|
-
* record (grants, pins, arity), and the return type checked against
|
|
573
|
-
* the output schema when one is stated.
|
|
574
|
-
*/
|
|
575
|
-
export interface ToolDecl<
|
|
576
|
-
Input extends SchemaLike = SchemaLike,
|
|
577
|
-
Reqs extends Readonly<Record<string, Requirement>> | undefined =
|
|
578
|
-
| Readonly<Record<string, Requirement>>
|
|
579
|
-
| undefined,
|
|
580
|
-
Output extends SchemaLike | undefined = SchemaLike | undefined,
|
|
581
|
-
> {
|
|
582
|
-
/** Address segment. Single-tool files may omit it (filename). */
|
|
583
|
-
readonly name?: string;
|
|
584
|
-
readonly description: string;
|
|
585
|
-
readonly requirements?: Reqs;
|
|
586
|
-
readonly input: Input;
|
|
587
|
-
readonly output?: Output;
|
|
588
|
-
readonly annotations?: ToolAnnotations;
|
|
589
|
-
handler(
|
|
590
|
-
input: InferSchema<Input>,
|
|
591
|
-
ctx: HandlerCtx<Reqs>,
|
|
592
|
-
): Promise<Output extends SchemaLike ? InferSchema<Output> : unknown>;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
/**
|
|
596
|
-
* The erased registry row. Registration drops the generics — storage
|
|
597
|
-
* is untyped by design, the same boundary every schema library draws
|
|
598
|
-
* between its typed builders and its runtime representation. The
|
|
599
|
-
* runtime re-establishes safety by validating input before the
|
|
600
|
-
* handler runs and shaping ctx from resolved connections.
|
|
601
|
-
*/
|
|
602
|
-
export interface ToolSpec {
|
|
603
|
-
readonly name?: string;
|
|
604
|
-
readonly description: string;
|
|
605
|
-
readonly requirements?: Readonly<Record<string, Requirement>>;
|
|
606
|
-
readonly input: SchemaLike;
|
|
607
|
-
readonly output?: SchemaLike;
|
|
608
|
-
readonly annotations?: ToolAnnotations;
|
|
609
|
-
handler(input: never, ctx: never): Promise<unknown>;
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
const ToolKey: unique symbol = Symbol.for("executor/apps/tool");
|
|
613
|
-
|
|
614
|
-
export interface Tool {
|
|
615
|
-
readonly [ToolKey]: ToolSpec;
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
export const defineTool = <
|
|
619
|
-
const Input extends SchemaLike,
|
|
620
|
-
const Reqs extends Readonly<Record<string, Requirement>> | undefined = undefined,
|
|
621
|
-
const Output extends SchemaLike | undefined = undefined,
|
|
622
|
-
>(decl: ToolDecl<Input, Reqs, Output>): Tool => {
|
|
623
|
-
// SAFETY: the erased row keeps the same members; only the handler's
|
|
624
|
-
// parameter types narrow (never is the bottom of every inference).
|
|
625
|
-
const spec = decl as unknown as ToolSpec;
|
|
626
|
-
return registerTool(spec);
|
|
627
|
-
};
|
|
628
|
-
|
|
629
|
-
const registerTool = (spec: ToolSpec): Tool => {
|
|
630
|
-
const file = sourceFileFor("defineTool(...)");
|
|
631
|
-
const value: Tool = { [ToolKey]: spec };
|
|
632
|
-
const fileName = file.slice(file.lastIndexOf("/") + 1);
|
|
633
|
-
const stem = fileName.endsWith(".ts") ? fileName.slice(0, -3) : fileName;
|
|
634
|
-
const name = spec.name ?? stem;
|
|
635
|
-
const fileSummary = activeRegistry.toolFiles.get(file);
|
|
636
|
-
activeRegistry.toolFiles.set(file, {
|
|
637
|
-
definitions: (fileSummary?.definitions ?? 0) + 1,
|
|
638
|
-
hasUnnamedTool: fileSummary?.hasUnnamedTool === true || spec.name === undefined,
|
|
639
|
-
});
|
|
640
|
-
|
|
641
|
-
const existing = activeRegistry.tools.get(name);
|
|
642
|
-
if (existing !== undefined) {
|
|
643
|
-
activeRegistry.problems.push({
|
|
644
|
-
message: `${file}: tool name '${name}' is already defined in ${existing.file}`,
|
|
645
|
-
});
|
|
646
|
-
} else activeRegistry.tools.set(name, { spec, file });
|
|
647
|
-
|
|
648
|
-
return value;
|
|
649
|
-
};
|
|
650
|
-
|
|
651
|
-
const isTool = (value: unknown): value is Tool =>
|
|
652
|
-
typeof value === "object" && value !== null && ToolKey in value;
|
|
653
|
-
|
|
654
|
-
export const toolSpecOf = (value: unknown): ToolSpec | undefined =>
|
|
655
|
-
isTool(value) ? value[ToolKey] : undefined;
|
|
656
|
-
|
|
657
|
-
/** A tool file's default export. */
|
|
658
|
-
export type ToolExport = Tool | ReadonlyArray<Tool>;
|
package/src/http/index.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/** HTTP credential placement helpers for kind-bearing connected roles. */
|
|
2
|
-
|
|
3
|
-
import type { RoleMaterial } from "../authoring/index";
|
|
4
|
-
|
|
5
|
-
export type HttpSpendableRole = Extract<
|
|
6
|
-
RoleMaterial,
|
|
7
|
-
{ kind: "bearer" | "oauth" | "basic" }
|
|
8
|
-
>;
|
|
9
|
-
|
|
10
|
-
/** Build the protocol-defined Authorization header for a connected role. */
|
|
11
|
-
export const authHeaders = (role: HttpSpendableRole): { readonly authorization: string } => {
|
|
12
|
-
switch (role.kind) {
|
|
13
|
-
case "bearer":
|
|
14
|
-
case "oauth":
|
|
15
|
-
return { authorization: `Bearer ${role.token}` };
|
|
16
|
-
case "basic":
|
|
17
|
-
return { authorization: `Basic ${base64(`${role.username}:${role.password}`)}` };
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const base64 = (value: string): string => {
|
|
22
|
-
const bytes = new TextEncoder().encode(value);
|
|
23
|
-
let binary = "";
|
|
24
|
-
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
25
|
-
return btoa(binary);
|
|
26
|
-
};
|