@alexkroman1/aai-cli 14.0.0 → 15.1.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/dist/_artifacts-BJOYGQPp.mjs +21 -0
- package/dist/_artifacts.d.ts +16 -0
- package/dist/_build-target.d.ts +172 -0
- package/dist/{_bundler-DolUCMxu.mjs → _bundler-DM0d0M7m.mjs} +1 -1
- package/dist/{_dev-server-CSMqF8PN.mjs → _dev-server-BzWB6-4y.mjs} +9 -6
- package/dist/_e2e-test-utils.d.ts +1 -1
- package/dist/{_init-CQ8idAwo.mjs → _init-Bsi3DZNJ.mjs} +1 -1
- package/dist/_server-common-De0haHr9.mjs +70 -0
- package/dist/_server-common.d.ts +20 -1
- package/dist/{_templates-CK4oKoeX.mjs → _templates-CIlJ3Vay.mjs} +1 -1
- package/dist/_templates.d.ts +1 -1
- package/dist/_vercel-output.d.ts +63 -0
- package/dist/build-BhEaxBPu.mjs +481 -0
- package/dist/build.d.ts +18 -10
- package/dist/cli.mjs +51 -23
- package/dist/{client-bundler-BJgREAh6.mjs → client-bundler-6mTLs6ny.mjs} +4 -4
- package/dist/client-bundler.d.ts +1 -1
- package/dist/client-bundler.mjs +1 -1
- package/dist/{deploy-uAJ4NukN.mjs → deploy-CGqPU5U-.mjs} +2 -2
- package/dist/{dev-DApPSaE_.mjs → dev-Bx9gYBHM.mjs} +1 -1
- package/dist/{eval-BK47A_K5.mjs → eval-B3I7FqN9.mjs} +1 -1
- package/dist/{init-DukDxECd.mjs → init-CQbj3ycf.mjs} +49 -39
- package/dist/init.d.ts +19 -10
- package/dist/scaffold/CLAUDE.md +73 -22
- package/dist/scaffold/package.json +6 -6
- package/dist/start.d.ts +112 -0
- package/dist/start.mjs +156 -0
- package/dist/{studio-CpHlNHUZ.mjs → studio-C_zuRC_z.mjs} +2 -2
- package/dist/templates/briefing-desk/agent.eval.test.ts +156 -0
- package/dist/templates/code-interpreter/agent.test.ts +103 -0
- package/dist/templates/link-digest/client.tsx +55 -3
- package/dist/templates/math-buddy/agent.test.ts +126 -0
- package/dist/templates/personal-finance/agent.test.ts +127 -0
- package/dist/templates/support-line/agent.ts +8 -0
- package/dist/templates/travel-concierge/routing.ts +64 -55
- package/dist/templates/travel-concierge/tools/cancel_action.ts +3 -1
- package/dist/templates/travel-concierge/tools/complete_or_escalate.ts +3 -1
- package/dist/templates/travel-concierge/tools/confirm_action.ts +3 -1
- package/dist/templates/web-researcher/agent.test.ts +130 -0
- package/dist/worker-bundler.d.ts +1 -1
- package/dist/worker-bundler.mjs +7 -7
- package/package.json +9 -4
- package/dist/_server-common-vILJp3it.mjs +0 -43
- package/dist/build-Mxk8gWvX.mjs +0 -108
- package/dist/scaffold/server.mjs +0 -308
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { a as ok, t as CliError } from "./_output-DBP9Op_d.mjs";
|
|
3
|
+
import { n as log, r as notify } from "./_ui-DCt4qZrk.mjs";
|
|
4
|
+
import { n as withPreservedNodeEnv } from "./_vite-env-kejF0p2L.mjs";
|
|
5
|
+
import { n as WORKER_ARTIFACT_REL, t as CLIENT_ARTIFACT_REL } from "./_artifacts-BJOYGQPp.mjs";
|
|
6
|
+
import { r as evalWorkerBundle, t as buildAgentBundle } from "./_bundler-DM0d0M7m.mjs";
|
|
7
|
+
import { assertTypechecks } from "./_typecheck-gate-U3jcil8L.mjs";
|
|
8
|
+
import { n as scanWorkflowDeterminism, t as determinismWarnings } from "./_workflow-determinism-CvpqMpdh.mjs";
|
|
9
|
+
import { TEST_FILES, classifyVitestError, runVitest } from "./test-D032V2cA.mjs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import fs from "node:fs/promises";
|
|
12
|
+
import { invariant } from "@alexkroman1/aai/internal";
|
|
13
|
+
import { build } from "vite";
|
|
14
|
+
import { defaultClientDir } from "@alexkroman1/aai-ui/client-dir";
|
|
15
|
+
import { agentConfigWarnings } from "@alexkroman1/aai/manifest";
|
|
16
|
+
import { DEFAULT_SYSTEM_PROMPT } from "@alexkroman1/aai";
|
|
17
|
+
//#region src/_build-target.ts
|
|
18
|
+
/**
|
|
19
|
+
* Deployment TARGETS — what `aai build` emits beside the worker so a host can
|
|
20
|
+
* run this project without the project holding anything host-specific.
|
|
21
|
+
*
|
|
22
|
+
* ## Why a target rather than a file in the scaffold
|
|
23
|
+
*
|
|
24
|
+
* Every host wants a different entry shape: Vercel wants a module whose default
|
|
25
|
+
* export is an `http.Server` it binds itself, a container wants a long-lived
|
|
26
|
+
* process, another platform wants something else again. Committing one of those
|
|
27
|
+
* to the scaffold makes every project assert a fact that is load-bearing on
|
|
28
|
+
* exactly one host — the same objection that removed `server.mjs`.
|
|
29
|
+
*
|
|
30
|
+
* Nitro is the worked precedent: one codebase, a preset per provider, and the
|
|
31
|
+
* preset EMITS the host's expected shape into the build directory. `node-server`
|
|
32
|
+
* is its default and Vercel/Netlify/Cloudflare are detected from the CI
|
|
33
|
+
* environment with no configuration. Next does the same narrower thing with
|
|
34
|
+
* `output: "standalone"` — it generates a `server.js` rather than asking anyone
|
|
35
|
+
* to write one. Either way the user's repository contains no host file, which is
|
|
36
|
+
* the property to preserve here.
|
|
37
|
+
*
|
|
38
|
+
* ## Auto-detection, and why it is safe
|
|
39
|
+
*
|
|
40
|
+
* A target is chosen with `--target`, or detected from the environment when the
|
|
41
|
+
* flag is absent. Detection reads the variables the hosts set on their own build
|
|
42
|
+
* containers ({@link TARGET_ENV_MARKERS}), so it only ever fires where the build
|
|
43
|
+
* is genuinely running on that host — a laptop sets none of them and gets
|
|
44
|
+
* {@link DEFAULT_BUILD_TARGET}, which emits nothing extra and is what every
|
|
45
|
+
* existing project already does.
|
|
46
|
+
*/
|
|
47
|
+
/** The targets `aai build --target` accepts. */
|
|
48
|
+
const BUILD_TARGETS = ["node", "vercel"];
|
|
49
|
+
/**
|
|
50
|
+
* What a build with no `--target` and no host environment produces: the worker
|
|
51
|
+
* and the client, and nothing else.
|
|
52
|
+
*
|
|
53
|
+
* `node` rather than a `"none"` sentinel because it NAMES the deployment it
|
|
54
|
+
* serves — a long-lived process running `aai start` — which is what every
|
|
55
|
+
* container platform wants and what the scaffold's own `start` script runs.
|
|
56
|
+
*/
|
|
57
|
+
const DEFAULT_BUILD_TARGET = "node";
|
|
58
|
+
/**
|
|
59
|
+
* The environment variable each host sets on its own build container.
|
|
60
|
+
*
|
|
61
|
+
* `VERCEL` is set for every Vercel build and deployment. Detection is per host
|
|
62
|
+
* rather than a single "am I in CI" test, because a GitHub Action building a
|
|
63
|
+
* container image is CI too and wants the default.
|
|
64
|
+
*/
|
|
65
|
+
const TARGET_ENV_MARKERS = { VERCEL: "vercel" };
|
|
66
|
+
function isBuildTarget(value) {
|
|
67
|
+
return BUILD_TARGETS.includes(value);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Resolve the target for this build: an explicit flag wins, then the
|
|
71
|
+
* environment, then {@link DEFAULT_BUILD_TARGET}.
|
|
72
|
+
*
|
|
73
|
+
* An unrecognised `--target` is REFUSED naming what is accepted, rather than
|
|
74
|
+
* falling back to the default — a typo'd target that silently built the default
|
|
75
|
+
* would deploy a project missing the entry its host needs, and the failure would
|
|
76
|
+
* arrive as a 404 from the platform rather than as an error from the build.
|
|
77
|
+
*/
|
|
78
|
+
function resolveBuildTarget(explicit, env = process.env) {
|
|
79
|
+
if (explicit !== void 0) {
|
|
80
|
+
if (!isBuildTarget(explicit)) throw new Error(`Unknown build target "${explicit}". Accepted: ${BUILD_TARGETS.join(", ")}.`);
|
|
81
|
+
return explicit;
|
|
82
|
+
}
|
|
83
|
+
for (const [marker, target] of Object.entries(TARGET_ENV_MARKERS)) if (env[marker]) return target;
|
|
84
|
+
return DEFAULT_BUILD_TARGET;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Where a PREBUILT Vercel deployment lives, relative to the project root.
|
|
88
|
+
*
|
|
89
|
+
* The Build Output API rather than an `api/` entry plus a `vercel.json`, and
|
|
90
|
+
* the reason is ORDERING. Vercel reads `vercel.json` and decides what to build
|
|
91
|
+
* BEFORE it runs the build command, so a `vercel.json` that the build WRITES
|
|
92
|
+
* configures the NEXT deployment and not this one — a clean clone deploys with
|
|
93
|
+
* no rewrite and no function at all. The `api/` shape only ever appeared to
|
|
94
|
+
* work because a previous local `aai build --target vercel` had left both
|
|
95
|
+
* files in the working tree, which is a property of one laptop rather than of
|
|
96
|
+
* the repository. `.vercel/output/` is read AFTER the build command; it is the
|
|
97
|
+
* only place a build can describe its own deployment.
|
|
98
|
+
*
|
|
99
|
+
* Two more things fall out of owning the directory, both of which the `api/`
|
|
100
|
+
* shape got wrong and could not fix. The function bundle is ASSEMBLED here
|
|
101
|
+
* rather than traced, so `.aai/worker.mjs` — loaded through a dynamic
|
|
102
|
+
* `import(pathToFileURL(...))` that no static tracer can follow — and
|
|
103
|
+
* `.env.example` — the file that DECLARES which variables become `ctx.env` —
|
|
104
|
+
* are present because they were copied in. And the built client is served by
|
|
105
|
+
* the CDN out of `static/` instead of through the function.
|
|
106
|
+
*
|
|
107
|
+
* Nitro's vercel preset is the worked precedent and lands in exactly here:
|
|
108
|
+
* `output.dir = {{rootDir}}/.vercel/output`, `serverDir` a `.func` under it.
|
|
109
|
+
*
|
|
110
|
+
* @see https://vercel.com/docs/build-output-api/v3
|
|
111
|
+
*/
|
|
112
|
+
const VERCEL_OUTPUT_DIR = path.join(".vercel", "output");
|
|
113
|
+
/**
|
|
114
|
+
* The one function every request that is not a static file reaches.
|
|
115
|
+
*
|
|
116
|
+
* The Build Output API derives a function's ROUTE from its path, so the
|
|
117
|
+
* directory name IS a URL and must not collide with one the static output
|
|
118
|
+
* claims. **`index.func` collides**, which a deployment is the only way to
|
|
119
|
+
* find out: it is served at `/index`, and Vercel's directory index resolves
|
|
120
|
+
* `/` to the extensionless `/index` — so the function won `/`, every other
|
|
121
|
+
* asset came off the CDN correctly, and the home page 500'd on a deployment
|
|
122
|
+
* whose static output was perfect. Measured on a real preview:
|
|
123
|
+
* `/favicon.ico`, `/index.html` and both hashed `/assets/*` returned 200
|
|
124
|
+
* while `/` and `/index` did not.
|
|
125
|
+
*
|
|
126
|
+
* `__server` is Nitro's answer to the same problem (`__server.func`) and the
|
|
127
|
+
* reason is this one: a double-underscore prefix is not a path any bundler
|
|
128
|
+
* emits, so no static file can ever take the name.
|
|
129
|
+
*/
|
|
130
|
+
const VERCEL_FUNCTION_DIR = path.join(VERCEL_OUTPUT_DIR, "functions", "__server.func");
|
|
131
|
+
/**
|
|
132
|
+
* The route {@link VERCEL_FUNCTION_DIR} is served at — its directory name
|
|
133
|
+
* without `.func`, which is how the Build Output API names a function.
|
|
134
|
+
*/
|
|
135
|
+
const VERCEL_FUNCTION_ROUTE = "/__server";
|
|
136
|
+
/** Static assets the Vercel CDN serves directly, never reaching the function. */
|
|
137
|
+
const VERCEL_STATIC_DIR = path.join(VERCEL_OUTPUT_DIR, "static");
|
|
138
|
+
/**
|
|
139
|
+
* Node versions Vercel offers. A build on anything newer picks the newest of
|
|
140
|
+
* these rather than naming a runtime the platform will reject.
|
|
141
|
+
*
|
|
142
|
+
* @see https://vercel.com/docs/functions/runtimes/node-js/node-js-versions
|
|
143
|
+
*/
|
|
144
|
+
const SUPPORTED_NODE_MAJORS = [
|
|
145
|
+
20,
|
|
146
|
+
22,
|
|
147
|
+
24
|
|
148
|
+
];
|
|
149
|
+
/** `nodejs<major>.x` for the Node running this build, clamped to what Vercel offers. */
|
|
150
|
+
function vercelNodeRuntime(version = process.versions.node) {
|
|
151
|
+
const major = Number.parseInt(version.split(".")[0] ?? "", 10);
|
|
152
|
+
return `nodejs${Number.isNaN(major) ? 22 : SUPPORTED_NODE_MAJORS.findLast((v) => v <= major) ?? SUPPORTED_NODE_MAJORS[0]}.x`;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The Vercel function entry, emitted into {@link VERCEL_FUNCTION_DIR}.
|
|
156
|
+
*
|
|
157
|
+
* ## Why a `(req, res)` handler and not `export default server`
|
|
158
|
+
*
|
|
159
|
+
* `export default <http.Server>` is what Vercel's own `@vercel/node` BUILDER
|
|
160
|
+
* accepts, and it is the shape the previous `api/index.mjs` used. The Build
|
|
161
|
+
* Output API has no builder in the path: `launcherType: "Nodejs"` invokes the
|
|
162
|
+
* module's default export as a request handler, so the server never gets
|
|
163
|
+
* bound and there is nothing to raise an `upgrade` event on it.
|
|
164
|
+
*
|
|
165
|
+
* ## How a WebSocket survives that
|
|
166
|
+
*
|
|
167
|
+
* Vercel hands a Node function the raw upgrade through its PER-REQUEST
|
|
168
|
+
* context — `globalThis[Symbol.for("@vercel/request-context")].get()
|
|
169
|
+
* .upgradeWebSocket()` returns the `{ req, socket, head }` triple — rather
|
|
170
|
+
* than as an event. Nitro reaches it through `crossws/adapters/vercel`; here
|
|
171
|
+
* the adapter is three lines, because {@link AgentServer.node} is a real
|
|
172
|
+
* `http.Server` that already has both an `upgrade` and a `request` listener
|
|
173
|
+
* registered. Re-emitting onto it is the whole translation, and it means the
|
|
174
|
+
* deployed path through `server.ts` is the same one `aai dev` and `aai start`
|
|
175
|
+
* take — no second WebSocket entry point to keep in step.
|
|
176
|
+
*
|
|
177
|
+
* The `204` afterwards is what the launcher needs to consider the invocation
|
|
178
|
+
* finished; the socket the agent is now talking on is not this `res`.
|
|
179
|
+
*
|
|
180
|
+
* ## `import.meta.dirname`, not `process.cwd()`
|
|
181
|
+
*
|
|
182
|
+
* The function's working directory belongs to the platform, but `.aai/` and
|
|
183
|
+
* `.env.example` were copied in BESIDE this file. Resolving from the module
|
|
184
|
+
* keeps that a fact about the bundle rather than about how Vercel happens to
|
|
185
|
+
* invoke it.
|
|
186
|
+
*/
|
|
187
|
+
const VERCEL_ENTRY_SOURCE = `// Generated by \`aai build --target vercel\` — do not edit, and do not commit.
|
|
188
|
+
// Vercel invokes this handler per request and delivers a WebSocket upgrade
|
|
189
|
+
// through its request context. See @alexkroman1/aai-cli/start.
|
|
190
|
+
import { createProjectServer } from "@alexkroman1/aai-cli/start";
|
|
191
|
+
|
|
192
|
+
const server = (await createProjectServer({ cwd: import.meta.dirname })).node;
|
|
193
|
+
|
|
194
|
+
const REQUEST_CONTEXT = Symbol.for("@vercel/request-context");
|
|
195
|
+
|
|
196
|
+
export default function handler(req, res) {
|
|
197
|
+
if (req.method === "GET" && req.headers.upgrade?.toLowerCase() === "websocket") {
|
|
198
|
+
const upgrade = globalThis[REQUEST_CONTEXT]?.get?.()?.upgradeWebSocket?.();
|
|
199
|
+
if (upgrade) {
|
|
200
|
+
server.emit("upgrade", upgrade.req, upgrade.socket, upgrade.head);
|
|
201
|
+
if (!res.headersSent && !res.writableEnded) {
|
|
202
|
+
res.statusCode = 204;
|
|
203
|
+
res.end();
|
|
204
|
+
}
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
server.emit("request", req, res);
|
|
209
|
+
}
|
|
210
|
+
`;
|
|
211
|
+
/**
|
|
212
|
+
* `.vc-config.json` — how the platform runs {@link VERCEL_ENTRY_SOURCE}.
|
|
213
|
+
*
|
|
214
|
+
* `supportsResponseStreaming` is not optional here: an agent streams TTS audio
|
|
215
|
+
* and SSE workflow events, and without it the platform buffers a response to
|
|
216
|
+
* completion, which for a stream that ends when the call does means it never
|
|
217
|
+
* arrives. `shouldAddHelpers` stays off — the entry speaks `node:http`, and
|
|
218
|
+
* the helpers exist to bolt Express-shaped sugar onto a handler that does not.
|
|
219
|
+
*/
|
|
220
|
+
function vercelFunctionConfigSource(runtime = vercelNodeRuntime()) {
|
|
221
|
+
return `${JSON.stringify({
|
|
222
|
+
runtime,
|
|
223
|
+
handler: "index.mjs",
|
|
224
|
+
launcherType: "Nodejs",
|
|
225
|
+
shouldAddHelpers: false,
|
|
226
|
+
supportsResponseStreaming: true
|
|
227
|
+
}, null, 2)}\n`;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* `config.json` — the routing table, and the reason static assets stop paying
|
|
231
|
+
* for a function invocation.
|
|
232
|
+
*
|
|
233
|
+
* `{ "handle": "filesystem" }` serves anything present in
|
|
234
|
+
* {@link VERCEL_STATIC_DIR} from the CDN and only then falls through, so the
|
|
235
|
+
* client bundle, its assets and the worklets are edge-served while
|
|
236
|
+
* `/client-config`, `/websocket`, `/workflows/*` and the webhook route reach
|
|
237
|
+
* the agent. The `api/` shape routed EVERY request through the function, which
|
|
238
|
+
* this file's earlier revision noted as deliberate and "not what makes a
|
|
239
|
+
* deployment work or not" — true of correctness, false of cost, and free here
|
|
240
|
+
* because the Build Output API already separates the two directories.
|
|
241
|
+
*/
|
|
242
|
+
const VERCEL_BUILD_CONFIG_SOURCE = `${JSON.stringify({
|
|
243
|
+
version: 3,
|
|
244
|
+
routes: [{ handle: "filesystem" }, {
|
|
245
|
+
src: "/(.*)",
|
|
246
|
+
dest: VERCEL_FUNCTION_ROUTE
|
|
247
|
+
}]
|
|
248
|
+
}, null, 2)}\n`;
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/_vercel-output.ts
|
|
251
|
+
/**
|
|
252
|
+
* Assemble a PREBUILT Vercel deployment — `.vercel/output/` — from what
|
|
253
|
+
* `aai build` just produced.
|
|
254
|
+
*
|
|
255
|
+
* The argument for the Build Output API over an `api/` entry is in
|
|
256
|
+
* `_build-target.ts` at {@link VERCEL_OUTPUT_DIR}; what this module adds is the
|
|
257
|
+
* consequence of it. A `.func` directory is a directory WE fill, so every file
|
|
258
|
+
* the server reads at runtime is present because it was copied in, and nothing
|
|
259
|
+
* depends on a static tracer following a path it structurally cannot:
|
|
260
|
+
*
|
|
261
|
+
* - **`.aai/worker.mjs`** is loaded through `import(pathToFileURL(...))`
|
|
262
|
+
* (`start.ts`), which `@vercel/nft` cannot resolve. Under the `api/` shape
|
|
263
|
+
* the build was green and the function 500'd on its first request with
|
|
264
|
+
* "No built agent at .aai/worker.mjs".
|
|
265
|
+
* - **`.env.example`** is not documentation here. `resolveServerEnv` treats it
|
|
266
|
+
* as the DECLARATION of which variables become `ctx.env` (see
|
|
267
|
+
* `DEPLOY_ENV_FILES`), so a function without it hands every tool an empty
|
|
268
|
+
* env while the Vercel project has the values set — a failure that looks
|
|
269
|
+
* like a credential problem and is a packaging one.
|
|
270
|
+
* - **The client** is copied to `static/`, where the CDN serves it, and also
|
|
271
|
+
* beside the worker, so `resolveClientDir` finds a real directory rather
|
|
272
|
+
* than reaching into a `node_modules` that the bundle replaced.
|
|
273
|
+
*
|
|
274
|
+
* The entry is BUNDLED rather than shipped with a `node_modules`. Same call as
|
|
275
|
+
* the worker (`ssr: { noExternal: true }`, `root: cwd`), so it resolves the
|
|
276
|
+
* project's own installed SDK and pulls the runtime, `ws` and `pg` in with it
|
|
277
|
+
* — which is already the arrangement a deployed worker runs under, and it is
|
|
278
|
+
* what makes the function independent of whether the host's install left a
|
|
279
|
+
* usable, hoisted `node_modules` behind.
|
|
280
|
+
*/
|
|
281
|
+
/**
|
|
282
|
+
* Files copied verbatim into the function, each one read at RUNTIME by a path
|
|
283
|
+
* no bundler can see. A missing one is skipped rather than fatal.
|
|
284
|
+
*
|
|
285
|
+
* **`.env` is deliberately NOT here.** `resolveServerEnv` reads
|
|
286
|
+
* {@link DEPLOY_ENV_FILES} — `.env.example` then `.env` — but only the first is
|
|
287
|
+
* a DECLARATION; the second holds a developer's own keys, and copying it would
|
|
288
|
+
* bake them into a deployment artifact and let them silently win over the
|
|
289
|
+
* values set in the Vercel project. Declarations ship, values come from the
|
|
290
|
+
* platform environment. Verified by building the `simple` template: a local
|
|
291
|
+
* `.env` with live credentials landed in the function until this list dropped
|
|
292
|
+
* it.
|
|
293
|
+
*/
|
|
294
|
+
const RUNTIME_FILES = [WORKER_ARTIFACT_REL, ".env.example"];
|
|
295
|
+
/**
|
|
296
|
+
* Write `.vercel/output/` for this project.
|
|
297
|
+
*
|
|
298
|
+
* The directory is REMOVED first. It is not addressed by content, so a
|
|
299
|
+
* function or a static asset left by an earlier build with a different shape
|
|
300
|
+
* would be deployed alongside this one — and `vercel deploy --prebuilt`
|
|
301
|
+
* uploads whatever is there.
|
|
302
|
+
*/
|
|
303
|
+
async function emitVercelOutput(cwd, options = {}) {
|
|
304
|
+
const outputDir = path.join(cwd, VERCEL_OUTPUT_DIR);
|
|
305
|
+
const functionDir = path.join(cwd, VERCEL_FUNCTION_DIR);
|
|
306
|
+
const staticDir = path.join(cwd, VERCEL_STATIC_DIR);
|
|
307
|
+
await fs.rm(outputDir, {
|
|
308
|
+
recursive: true,
|
|
309
|
+
force: true
|
|
310
|
+
});
|
|
311
|
+
await fs.mkdir(functionDir, { recursive: true });
|
|
312
|
+
await fs.writeFile(path.join(outputDir, "config.json"), VERCEL_BUILD_CONFIG_SOURCE, "utf-8");
|
|
313
|
+
await fs.writeFile(path.join(functionDir, ".vc-config.json"), vercelFunctionConfigSource(), "utf-8");
|
|
314
|
+
const bundle = options.bundle ?? bundleEntry;
|
|
315
|
+
await fs.writeFile(path.join(functionDir, "index.mjs"), await bundle(cwd), "utf-8");
|
|
316
|
+
for (const rel of RUNTIME_FILES) {
|
|
317
|
+
const from = path.join(cwd, rel);
|
|
318
|
+
if (!await exists(from)) continue;
|
|
319
|
+
const to = path.join(functionDir, rel);
|
|
320
|
+
await fs.mkdir(path.dirname(to), { recursive: true });
|
|
321
|
+
await fs.copyFile(from, to);
|
|
322
|
+
}
|
|
323
|
+
const clientSource = await exists(path.join(cwd, CLIENT_ARTIFACT_REL, "index.html")) ? path.join(cwd, CLIENT_ARTIFACT_REL) : defaultClientDir();
|
|
324
|
+
await fs.cp(clientSource, staticDir, { recursive: true });
|
|
325
|
+
await fs.cp(clientSource, path.join(functionDir, CLIENT_ARTIFACT_REL), { recursive: true });
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Bundle {@link VERCEL_ENTRY_SOURCE} and everything it imports into one ESM
|
|
329
|
+
* file.
|
|
330
|
+
*
|
|
331
|
+
* The entry is written INTO the project rather than a temp directory, because
|
|
332
|
+
* that is what makes `@alexkroman1/aai-cli/start` resolve against the user's
|
|
333
|
+
* install — the deployed server is then the version their lockfile pins, which
|
|
334
|
+
* is the same guarantee `aai publish` gives. Removed in a `finally`: a build
|
|
335
|
+
* that throws must not leave a file that looks authored.
|
|
336
|
+
*/
|
|
337
|
+
async function bundleEntry(cwd) {
|
|
338
|
+
const entryPath = path.join(cwd, ".aai", "vercel-entry.mjs");
|
|
339
|
+
await fs.mkdir(path.dirname(entryPath), { recursive: true });
|
|
340
|
+
await fs.writeFile(entryPath, VERCEL_ENTRY_SOURCE, "utf-8");
|
|
341
|
+
let result;
|
|
342
|
+
try {
|
|
343
|
+
result = await withPreservedNodeEnv(() => build({
|
|
344
|
+
root: cwd,
|
|
345
|
+
logLevel: "silent",
|
|
346
|
+
configFile: false,
|
|
347
|
+
ssr: { noExternal: true },
|
|
348
|
+
build: {
|
|
349
|
+
ssr: true,
|
|
350
|
+
lib: {
|
|
351
|
+
entry: entryPath,
|
|
352
|
+
formats: ["es"],
|
|
353
|
+
fileName: "index"
|
|
354
|
+
},
|
|
355
|
+
target: "node20",
|
|
356
|
+
minify: false,
|
|
357
|
+
write: false,
|
|
358
|
+
rollupOptions: { output: {
|
|
359
|
+
entryFileNames: "[name].mjs",
|
|
360
|
+
codeSplitting: false
|
|
361
|
+
} }
|
|
362
|
+
}
|
|
363
|
+
}));
|
|
364
|
+
} finally {
|
|
365
|
+
await fs.rm(entryPath, { force: true }).catch(() => void 0);
|
|
366
|
+
}
|
|
367
|
+
const output = Array.isArray(result) ? result[0] : result;
|
|
368
|
+
invariant(output !== void 0, "vercel.entry.output");
|
|
369
|
+
const chunk = output.output.find((o) => o.type === "chunk" && o.isEntry);
|
|
370
|
+
invariant(chunk !== void 0, "vercel.entry.chunk", () => ({ kinds: output.output.map((o) => o.type) }));
|
|
371
|
+
return chunk.code;
|
|
372
|
+
}
|
|
373
|
+
async function exists(target) {
|
|
374
|
+
return await fs.stat(target).then(() => true, () => false);
|
|
375
|
+
}
|
|
376
|
+
//#endregion
|
|
377
|
+
//#region src/build.ts
|
|
378
|
+
/**
|
|
379
|
+
* `aai build` — bundle the agent without deploying, behind the same gates
|
|
380
|
+
* deploy runs (tests, then typecheck), so the command previews the deploy
|
|
381
|
+
* artifact and its failures alike.
|
|
382
|
+
*
|
|
383
|
+
* It also LEAVES that artifact on disk, at {@link WORKER_ARTIFACT_REL}, which is
|
|
384
|
+
* what makes self-hosting work: `aai start` loads the built worker rather than
|
|
385
|
+
* `agent.ts`, because a tool is discovered by the bundler enumerating `tools/`
|
|
386
|
+
* and no un-bundled loader can see that directory. See `start.ts` and
|
|
387
|
+
* "Self-hosting is the scaffold's default" in `packages/aai-cli/CLAUDE.md`.
|
|
388
|
+
*
|
|
389
|
+
* `--target` decides what else it emits — see `_build-target.ts`.
|
|
390
|
+
*/
|
|
391
|
+
/**
|
|
392
|
+
* The prose slot, named by convention. Mirrors `worker-bundler.ts`'s constant of
|
|
393
|
+
* the same name — the two cannot share one without an import cycle
|
|
394
|
+
* (`build.ts` → `_bundler.ts` → `worker-bundler.ts`), so `_build.test.ts` pins
|
|
395
|
+
* the pair end to end instead: a build whose prompt really came from the file
|
|
396
|
+
* has to report the file.
|
|
397
|
+
*/
|
|
398
|
+
const SYSTEM_PROMPT_FILE = "system-prompt.md";
|
|
399
|
+
/** What {@link systemPromptSource} reports when nothing declared a prompt. */
|
|
400
|
+
const FRAMEWORK_DEFAULT_PROMPT_SOURCE = "the framework default (DEFAULT_SYSTEM_PROMPT)";
|
|
401
|
+
/**
|
|
402
|
+
* Which of the three possible authors of this agent's system prompt actually
|
|
403
|
+
* wrote the one in the bundle.
|
|
404
|
+
*
|
|
405
|
+
* `aai build` reported `name` and `workerBytes` and nothing about the prompt,
|
|
406
|
+
* and the prompt is the one field that can change COMPLETELY without any
|
|
407
|
+
* config changing: deleting `system-prompt.md` swaps in
|
|
408
|
+
* `DEFAULT_SYSTEM_PROMPT` — a total personality change — with exit 0 and no
|
|
409
|
+
* line anywhere. `withSystemPrompt` refuses the case where a file exists and
|
|
410
|
+
* nothing reads it; it cannot refuse a file that is simply GONE, because that
|
|
411
|
+
* is what an agent with no file legitimately looks like. So this is a report
|
|
412
|
+
* rather than a check.
|
|
413
|
+
*
|
|
414
|
+
* Decided by comparing VALUES against the built agent's resolved prompt, which
|
|
415
|
+
* is the same method `withSystemPrompt` uses and for the same reason — the
|
|
416
|
+
* alternative asks the bundler's module graph a question, and the author may
|
|
417
|
+
* have imported the file and composed it, which is neither "the file" nor
|
|
418
|
+
* "agent.ts" alone.
|
|
419
|
+
*/
|
|
420
|
+
async function systemPromptSource(cwd, resolved) {
|
|
421
|
+
if (resolved === DEFAULT_SYSTEM_PROMPT) return FRAMEWORK_DEFAULT_PROMPT_SOURCE;
|
|
422
|
+
const trimmed = (await fs.readFile(path.join(cwd, SYSTEM_PROMPT_FILE), "utf-8").catch(() => void 0))?.trim();
|
|
423
|
+
if (trimmed === void 0 || trimmed === "" || !resolved.includes(trimmed)) return "agent.ts";
|
|
424
|
+
return resolved.trim() === trimmed ? SYSTEM_PROMPT_FILE : `agent.ts (composing ${SYSTEM_PROMPT_FILE})`;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Map a {@link runVitest} failure to a CliError — distinguishing a real test
|
|
428
|
+
* failure (test_failed) from the runner not spawning (spawn_failed) instead
|
|
429
|
+
* of a generic command_failed.
|
|
430
|
+
*/
|
|
431
|
+
function testGateError(err) {
|
|
432
|
+
const { code, message } = classifyVitestError(err);
|
|
433
|
+
return new CliError(code, message, "Re-run with --skipTests to build without tests", { cause: err });
|
|
434
|
+
}
|
|
435
|
+
async function executeBuild(opts) {
|
|
436
|
+
const { cwd } = opts;
|
|
437
|
+
const target = resolveBuildTarget(opts.target);
|
|
438
|
+
if (!opts.skipTests) try {
|
|
439
|
+
runVitest(cwd, {
|
|
440
|
+
candidates: TEST_FILES,
|
|
441
|
+
all: true
|
|
442
|
+
});
|
|
443
|
+
} catch (err) {
|
|
444
|
+
throw testGateError(err);
|
|
445
|
+
}
|
|
446
|
+
await assertTypechecks(cwd, { skip: opts.skipTypecheck });
|
|
447
|
+
const bundle = await buildAgentBundle(cwd, { minify: true });
|
|
448
|
+
const agentDef = await evalWorkerBundle(bundle.worker);
|
|
449
|
+
for (const warning of agentConfigWarnings(agentDef)) notify("warn", warning);
|
|
450
|
+
for (const warning of determinismWarnings(await scanWorkflowDeterminism(cwd))) notify("warn", warning);
|
|
451
|
+
const worker = path.join(cwd, WORKER_ARTIFACT_REL);
|
|
452
|
+
await fs.mkdir(path.dirname(worker), { recursive: true });
|
|
453
|
+
await fs.writeFile(worker, bundle.worker, "utf-8");
|
|
454
|
+
await emitTargetFiles(cwd, target);
|
|
455
|
+
const systemPrompt = await systemPromptSource(cwd, agentDef.systemPrompt);
|
|
456
|
+
log.info(`System prompt: ${systemPrompt}`);
|
|
457
|
+
log.success("Build complete");
|
|
458
|
+
return ok({
|
|
459
|
+
name: agentDef.name,
|
|
460
|
+
workerBytes: bundle.worker.length,
|
|
461
|
+
worker,
|
|
462
|
+
systemPrompt,
|
|
463
|
+
target
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Write what a target needs, if it needs anything.
|
|
468
|
+
*
|
|
469
|
+
* `node` writes nothing: a long-lived process runs `aai start`, which needs no
|
|
470
|
+
* generated file. `vercel` writes a complete prebuilt deployment under
|
|
471
|
+
* `.vercel/output/` — see `_vercel-output.ts`, and `VERCEL_OUTPUT_DIR` in
|
|
472
|
+
* `_build-target.ts` for why that directory and not an `api/` entry beside a
|
|
473
|
+
* generated `vercel.json`.
|
|
474
|
+
*/
|
|
475
|
+
async function emitTargetFiles(cwd, target) {
|
|
476
|
+
if (target === "node") return;
|
|
477
|
+
await emitVercelOutput(cwd);
|
|
478
|
+
log.info(`Target ${target}: wrote ${VERCEL_OUTPUT_DIR}`);
|
|
479
|
+
}
|
|
480
|
+
//#endregion
|
|
481
|
+
export { executeBuild };
|
package/dist/build.d.ts
CHANGED
|
@@ -4,23 +4,25 @@
|
|
|
4
4
|
* artifact and its failures alike.
|
|
5
5
|
*
|
|
6
6
|
* It also LEAVES that artifact on disk, at {@link WORKER_ARTIFACT_REL}, which is
|
|
7
|
-
* what makes self-hosting work: `
|
|
7
|
+
* what makes self-hosting work: `aai start` loads the built worker rather than
|
|
8
8
|
* `agent.ts`, because a tool is discovered by the bundler enumerating `tools/`
|
|
9
|
-
* and no un-bundled loader can see that directory. See
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* and no un-bundled loader can see that directory. See `start.ts` and
|
|
10
|
+
* "Self-hosting is the scaffold's default" in `packages/aai-cli/CLAUDE.md`.
|
|
11
|
+
*
|
|
12
|
+
* `--target` decides what else it emits — see `_build-target.ts`.
|
|
12
13
|
*/
|
|
14
|
+
import { type BuildTarget } from "./_build-target.ts";
|
|
13
15
|
import { type CommandResult } from "./_output.ts";
|
|
14
16
|
/**
|
|
15
17
|
* Where the built worker lands, relative to the project root — under `.aai/`,
|
|
16
18
|
* beside the built client, so one gitignore rule covers every build output.
|
|
17
19
|
*
|
|
18
|
-
* `
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
20
|
+
* `start.ts` imports this constant rather than restating the path, which it
|
|
21
|
+
* could not do while the loader was a `.mjs` file in the user's project. The
|
|
22
|
+
* pair is covered end-to-end by the `npm start` leg of `e2e.test.ts` — the only
|
|
23
|
+
* tier that runs both halves as a user does.
|
|
22
24
|
*/
|
|
23
|
-
export
|
|
25
|
+
export { WORKER_ARTIFACT_REL } from "./_artifacts.ts";
|
|
24
26
|
type BuildData = {
|
|
25
27
|
name: string;
|
|
26
28
|
workerBytes: number;
|
|
@@ -31,10 +33,16 @@ type BuildData = {
|
|
|
31
33
|
* {@link systemPromptSource}.
|
|
32
34
|
*/
|
|
33
35
|
systemPrompt: string;
|
|
36
|
+
/** Which deployment shape this build emitted — see `_build-target.ts`. */
|
|
37
|
+
target: BuildTarget;
|
|
34
38
|
};
|
|
35
39
|
export declare function executeBuild(opts: {
|
|
36
40
|
cwd: string;
|
|
37
41
|
skipTests?: boolean | undefined;
|
|
38
42
|
skipTypecheck?: boolean | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Which deployment shape to emit beside the worker. Absent, it is detected
|
|
45
|
+
* from the host's own build environment — see `resolveBuildTarget`.
|
|
46
|
+
*/
|
|
47
|
+
target?: string | undefined;
|
|
39
48
|
}): Promise<CommandResult<BuildData>>;
|
|
40
|
-
export {};
|