@alexkroman1/aai-cli 17.0.0 → 18.0.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/_console-audio.d.ts +17 -0
- package/dist/_console-session.d.ts +70 -0
- package/dist/{_dev-server-CLU9xjH5.mjs → _dev-server-Dtp4aAlo.mjs} +1 -1
- package/dist/_dev-server.d.ts +1 -0
- package/dist/_templates-DXadVNsL.mjs +135 -0
- package/dist/_templates.d.ts +11 -34
- package/dist/cli.mjs +27 -7
- package/dist/console-BsyE3DyB.mjs +422 -0
- package/dist/console.d.ts +37 -0
- package/dist/{dev-ChsrNHpK.mjs → dev-BGWU2zPP.mjs} +1 -1
- package/dist/{init-W-4k-lCF.mjs → init-CbOhyNYe.mjs} +1 -1
- package/dist/scaffold/CLAUDE.md +110 -102
- package/dist/scaffold/package.json +4 -4
- package/dist/start.d.ts +3 -3
- package/dist/{studio-DEAJGBXa.mjs → studio-BRJsw7lr.mjs} +1 -1
- package/dist/templates/applicant-screening-agent/agent.test.ts +17 -13
- package/dist/templates/applicant-screening-agent/crews.ts +4 -4
- package/dist/templates/code-interpreter-agent/agent.test.ts +2 -2
- package/dist/templates/coding-agent/agent.ts +1 -1
- package/dist/templates/custom-pipeline-agent/agent.ts +2 -2
- package/dist/templates/custom-pipeline-agent/modes.test.ts +4 -4
- package/dist/templates/custom-pipeline-agent/stages.test.ts +75 -83
- package/dist/templates/emergency-dispatch-agent/agent.eval.test.ts +1 -1
- package/dist/templates/entertainment-picks-agent/agent.eval.test.ts +1 -1
- package/dist/templates/executive-inbox-agent/agent.test.ts +35 -25
- package/dist/templates/executive-inbox-agent/calendar.test.ts +13 -7
- package/dist/templates/executive-inbox-agent/meeting.ts +2 -2
- package/dist/templates/front-desk-agent/agent.eval.test.ts +174 -0
- package/dist/templates/front-desk-agent/agent.test.ts +150 -0
- package/dist/templates/front-desk-agent/agent.ts +22 -0
- package/dist/templates/front-desk-agent/shared.ts +234 -0
- package/dist/templates/front-desk-agent/system-prompt.md +13 -0
- package/dist/templates/front-desk-agent/tools/verify_account.ts +41 -0
- package/dist/templates/hotel-reception-agent/agent.test.ts +1 -2
- package/dist/templates/hotel-reception-agent/desk-seams.test.ts +1 -2
- package/dist/templates/medication-safety-agent/agent.test.ts +3 -4
- package/dist/templates/pizza-ordering-agent/agent.eval.test.ts +68 -2
- package/dist/templates/push-to-talk-agent/agent.eval.test.ts +74 -0
- package/dist/templates/push-to-talk-agent/agent.test.ts +61 -0
- package/dist/templates/push-to-talk-agent/agent.ts +22 -0
- package/dist/templates/push-to-talk-agent/client.tsx +124 -0
- package/dist/templates/push-to-talk-agent/shared.ts +34 -0
- package/dist/templates/push-to-talk-agent/system-prompt.md +11 -0
- package/dist/templates/push-to-talk-agent/tools/list_notes.ts +15 -0
- package/dist/templates/push-to-talk-agent/tools/save_note.ts +26 -0
- package/dist/templates/quickstart-agent/agent.eval.test.ts +52 -0
- package/dist/templates/research-handoff-agent/agent.eval.test.ts +1 -1
- package/dist/templates/research-handoff-agent/agent.test.ts +34 -26
- package/dist/templates/research-planner-agent/agent.test.ts +26 -16
- package/dist/templates/retail-orders-agent/agent.eval.test.ts +1 -1
- package/dist/templates/roadside-assistance-agent/agent.eval.test.ts +87 -0
- package/dist/templates/roadside-assistance-agent/agent.test.ts +1 -1
- package/dist/templates/technical-support-agent/agent.test.ts +26 -20
- package/dist/templates/technical-support-agent/procedure.ts +3 -3
- package/dist/templates/topic-briefing-agent/agent.test.ts +38 -25
- package/dist/templates/topic-briefing-agent/shared.ts +10 -9
- package/dist/templates/travel-concierge-agent/agent.eval.test.ts +9 -7
- package/dist/templates/travel-concierge-agent/agent.test.ts +1 -2
- package/dist/templates/word-game-agent/agent.test.ts +3 -1
- package/package.json +4 -4
- package/dist/_templates-IMeKMVIj.mjs +0 -291
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { a as ok, n as fail } from "./_output-DBP9Op_d.mjs";
|
|
3
|
+
import { n as log } from "./_ui-DCt4qZrk.mjs";
|
|
4
|
+
import { o as errorDetail, s as errorMessage } from "./_utils-D5JGcjiW.mjs";
|
|
5
|
+
import { n as createWorkerEvaluator } from "./_bundler-DM0d0M7m.mjs";
|
|
6
|
+
import { loadWorker, resolveAgentEnv } from "./_dev-server-Dtp4aAlo.mjs";
|
|
7
|
+
import { styleText } from "node:util";
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import { connectSession, createRuntime, ensureSessionStateSchema, ensureWorkflowJournalSchema, withHostCredentialFallback } from "@alexkroman1/aai-runtime";
|
|
10
|
+
import pTimeout from "p-timeout";
|
|
11
|
+
//#region src/_console-audio.ts
|
|
12
|
+
/**
|
|
13
|
+
* `aai console`'s audio devices, as two SoX subprocesses.
|
|
14
|
+
*
|
|
15
|
+
* SoX rather than a native addon: `rec` and `play` open the system's DEFAULT
|
|
16
|
+
* input and output on macOS (CoreAudio), Linux (ALSA/PulseAudio) and Windows
|
|
17
|
+
* with one command line, and a subprocess costs this package no install-time
|
|
18
|
+
* build step and no binary per platform. The price is that SoX must be
|
|
19
|
+
* installed, which {@link missingSoxMessage} says in so many words.
|
|
20
|
+
*
|
|
21
|
+
* Both run in raw PCM16LE mono (`-t raw -b 16 -e signed-integer -c 1 -L`),
|
|
22
|
+
* the format the session speaks, at the rate the session announced.
|
|
23
|
+
*/
|
|
24
|
+
const RAW_PCM16 = [
|
|
25
|
+
"-t",
|
|
26
|
+
"raw",
|
|
27
|
+
"-b",
|
|
28
|
+
"16",
|
|
29
|
+
"-e",
|
|
30
|
+
"signed-integer",
|
|
31
|
+
"-c",
|
|
32
|
+
"1",
|
|
33
|
+
"-L"
|
|
34
|
+
];
|
|
35
|
+
/** What to tell a developer whose machine has no SoX. */
|
|
36
|
+
function missingSoxMessage(binary) {
|
|
37
|
+
return `\`aai console\` needs SoX for microphone and speaker access, and \`${binary}\` was not found. Install it (macOS: \`brew install sox\`; Debian/Ubuntu: \`sudo apt install sox\`; Windows: https://sourceforge.net/projects/sox/) and try again.`;
|
|
38
|
+
}
|
|
39
|
+
function spawnError(binary, err) {
|
|
40
|
+
return err.code === "ENOENT" ? new Error(missingSoxMessage(binary)) : err;
|
|
41
|
+
}
|
|
42
|
+
function kill(child, signal) {
|
|
43
|
+
if (child.exitCode === null && !child.killed) child.kill(signal);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Spawn `play` so that its stdin is a real PIPE.
|
|
47
|
+
*
|
|
48
|
+
* libuv backs `stdio: "pipe"` with a socketpair, and SoX treats a socket stdin
|
|
49
|
+
* with no bytes ready as end of input: the moment the device drains — the end
|
|
50
|
+
* of the greeting, i.e. the first time the agent stops talking — `play` exits
|
|
51
|
+
* 0 with its stdin still open. Measured on macOS with SoX 14.4.2: a 100 ms gap
|
|
52
|
+
* is enough. A shell pipe does not do it, so bash puts one in between.
|
|
53
|
+
* The `exec` makes the PID we hold `play` itself, so kills and exit codes
|
|
54
|
+
* are its own; the `cat` exits on EOF once Node closes the socket at exit.
|
|
55
|
+
*
|
|
56
|
+
* Windows keeps the direct spawn: it has no bash, and its stdio pipes are
|
|
57
|
+
* named pipes rather than sockets.
|
|
58
|
+
*/
|
|
59
|
+
function spawnPlay(args) {
|
|
60
|
+
const stdio = [
|
|
61
|
+
"pipe",
|
|
62
|
+
"ignore",
|
|
63
|
+
"ignore"
|
|
64
|
+
];
|
|
65
|
+
if (process.platform === "win32") return spawn("play", args, { stdio });
|
|
66
|
+
return spawn("bash", [
|
|
67
|
+
"-c",
|
|
68
|
+
"exec play \"$@\" < <(exec cat)",
|
|
69
|
+
"play",
|
|
70
|
+
...args
|
|
71
|
+
], { stdio });
|
|
72
|
+
}
|
|
73
|
+
/** bash's exit status for a command it could not find — the spawn's ENOENT, one shell down. */
|
|
74
|
+
const COMMAND_NOT_FOUND = 127;
|
|
75
|
+
/** Open the default microphone. */
|
|
76
|
+
function startCapture(sampleRate, onChunk, onError) {
|
|
77
|
+
const child = spawn("rec", [
|
|
78
|
+
"-q",
|
|
79
|
+
...RAW_PCM16,
|
|
80
|
+
"-r",
|
|
81
|
+
String(sampleRate),
|
|
82
|
+
"-"
|
|
83
|
+
], { stdio: [
|
|
84
|
+
"ignore",
|
|
85
|
+
"pipe",
|
|
86
|
+
"ignore"
|
|
87
|
+
] });
|
|
88
|
+
let stopped = false;
|
|
89
|
+
child.on("error", (err) => onError(spawnError("rec", err)));
|
|
90
|
+
child.stdout?.on("data", (chunk) => {
|
|
91
|
+
onChunk(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
|
92
|
+
});
|
|
93
|
+
child.on("exit", (code) => {
|
|
94
|
+
if (!stopped && code !== 0 && code !== null) onError(/* @__PURE__ */ new Error(`the microphone process exited with code ${code}`));
|
|
95
|
+
});
|
|
96
|
+
return { stop() {
|
|
97
|
+
stopped = true;
|
|
98
|
+
kill(child);
|
|
99
|
+
} };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Open the default speaker.
|
|
103
|
+
*
|
|
104
|
+
* `flush` KILLS the process and opens a new one, because that is the only way
|
|
105
|
+
* to empty what SoX and the device already hold: there is no "discard buffer"
|
|
106
|
+
* verb on a pipe. The respawn costs a few tens of ms of device open, paid only
|
|
107
|
+
* on a barge-in, when the agent has just stopped talking anyway.
|
|
108
|
+
*
|
|
109
|
+
* The kill is a SIGKILL. SoX traps SIGTERM and only looks at the flag
|
|
110
|
+
* between reads, so a `play` blocked on an idle pipe — every moment the agent
|
|
111
|
+
* is not talking — ignores it and outlives the console.
|
|
112
|
+
*/
|
|
113
|
+
function startPlayback(sampleRate, onError) {
|
|
114
|
+
let stopped = false;
|
|
115
|
+
let child = open();
|
|
116
|
+
function open() {
|
|
117
|
+
const next = spawnPlay([
|
|
118
|
+
"-q",
|
|
119
|
+
...RAW_PCM16,
|
|
120
|
+
"-r",
|
|
121
|
+
String(sampleRate),
|
|
122
|
+
"-"
|
|
123
|
+
]);
|
|
124
|
+
next.on("error", (err) => onError(spawnError("play", err)));
|
|
125
|
+
next.on("exit", (code) => {
|
|
126
|
+
if (stopped || next !== child) return;
|
|
127
|
+
if (code === COMMAND_NOT_FOUND) return onError(new Error(missingSoxMessage("play")));
|
|
128
|
+
onError(/* @__PURE__ */ new Error(`the speaker process exited${code === null ? "" : ` with code ${code}`}`));
|
|
129
|
+
});
|
|
130
|
+
next.stdin?.on("error", () => void 0);
|
|
131
|
+
return next;
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
write(pcm16) {
|
|
135
|
+
if (stopped) return;
|
|
136
|
+
child.stdin?.write(pcm16);
|
|
137
|
+
},
|
|
138
|
+
flush() {
|
|
139
|
+
if (stopped) return;
|
|
140
|
+
const old = child;
|
|
141
|
+
child = open();
|
|
142
|
+
kill(old, "SIGKILL");
|
|
143
|
+
},
|
|
144
|
+
stop() {
|
|
145
|
+
stopped = true;
|
|
146
|
+
child.stdin?.end();
|
|
147
|
+
kill(child, "SIGKILL");
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** The SoX-backed devices. */
|
|
152
|
+
const soxAudio = {
|
|
153
|
+
startCapture,
|
|
154
|
+
startPlayback
|
|
155
|
+
};
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/_console-session.ts
|
|
158
|
+
/**
|
|
159
|
+
* The sink the session writes to: agent audio to the speaker, the transcript
|
|
160
|
+
* to the terminal.
|
|
161
|
+
*
|
|
162
|
+
* `onClosed` fires when the RUNTIME closes the sink — a session that failed to
|
|
163
|
+
* start, or one resumed elsewhere — so the command can stop the devices and
|
|
164
|
+
* exit rather than sit with a live microphone feeding nothing.
|
|
165
|
+
*
|
|
166
|
+
* `onFatal` fires on a FATAL `error.reported`, which the protocol defines as
|
|
167
|
+
* "the session is over". A browser shows the banner and the person hangs up;
|
|
168
|
+
* the telephony bridge ends the call; the console has to end itself too, or a
|
|
169
|
+
* session that can never speak (a provider refusing the key) leaves the
|
|
170
|
+
* developer talking to silence.
|
|
171
|
+
*/
|
|
172
|
+
function createConsoleSink(opts) {
|
|
173
|
+
const { player, print } = opts;
|
|
174
|
+
let open = true;
|
|
175
|
+
/** The reply's text so far — what to print if it is cut off before it commits. */
|
|
176
|
+
let pendingAgentText = "";
|
|
177
|
+
return {
|
|
178
|
+
get open() {
|
|
179
|
+
return open;
|
|
180
|
+
},
|
|
181
|
+
event(e) {
|
|
182
|
+
switch (e.type) {
|
|
183
|
+
case "user-transcript.committed":
|
|
184
|
+
if (e.text.trim()) print.user(e.text);
|
|
185
|
+
return;
|
|
186
|
+
case "agent-transcript.updated":
|
|
187
|
+
pendingAgentText = e.text;
|
|
188
|
+
return;
|
|
189
|
+
case "agent-transcript.committed":
|
|
190
|
+
pendingAgentText = "";
|
|
191
|
+
if (e.text.trim()) print.agent(e.text);
|
|
192
|
+
return;
|
|
193
|
+
case "reply.cancelled":
|
|
194
|
+
player.flush();
|
|
195
|
+
if (pendingAgentText.trim()) print.agent(pendingAgentText, true);
|
|
196
|
+
pendingAgentText = "";
|
|
197
|
+
return;
|
|
198
|
+
case "session.reset":
|
|
199
|
+
player.flush();
|
|
200
|
+
pendingAgentText = "";
|
|
201
|
+
return;
|
|
202
|
+
case "tool.called":
|
|
203
|
+
print.tool(e.toolName, e.args);
|
|
204
|
+
return;
|
|
205
|
+
case "error.reported":
|
|
206
|
+
print.error(e.message, e.fatal);
|
|
207
|
+
if (e.fatal) opts.onFatal(e.message);
|
|
208
|
+
return;
|
|
209
|
+
default: return;
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
playAudioChunk(chunk) {
|
|
213
|
+
player.write(chunk);
|
|
214
|
+
},
|
|
215
|
+
close(reason) {
|
|
216
|
+
if (!open) return;
|
|
217
|
+
open = false;
|
|
218
|
+
opts.onClosed(reason);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Feed a byte stream into a connection as whole PCM16 samples.
|
|
224
|
+
*
|
|
225
|
+
* A pipe hands over reads of whatever length it likes, and an odd one would
|
|
226
|
+
* split a sample across two chunks — every sample after it would then be read
|
|
227
|
+
* with its bytes swapped, which is loud noise rather than a subtle error.
|
|
228
|
+
*/
|
|
229
|
+
function createSampleAligner(connection) {
|
|
230
|
+
let carry = null;
|
|
231
|
+
return (bytes) => {
|
|
232
|
+
if (bytes.byteLength === 0) return;
|
|
233
|
+
let data = bytes;
|
|
234
|
+
if (carry !== null) {
|
|
235
|
+
const joined = new Uint8Array(bytes.byteLength + 1);
|
|
236
|
+
joined[0] = carry;
|
|
237
|
+
joined.set(bytes, 1);
|
|
238
|
+
data = joined;
|
|
239
|
+
carry = null;
|
|
240
|
+
}
|
|
241
|
+
const whole = data.byteLength - data.byteLength % 2;
|
|
242
|
+
if (whole < data.byteLength) carry = data[data.byteLength - 1] ?? null;
|
|
243
|
+
if (whole > 0) connection.sendAudio(data.subarray(0, whole));
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/console.ts
|
|
248
|
+
/**
|
|
249
|
+
* `aai console` — talk to the agent through this machine's microphone and
|
|
250
|
+
* speakers, with the conversation printed in the terminal. No server, no
|
|
251
|
+
* browser, no socket.
|
|
252
|
+
*
|
|
253
|
+
* It is the smallest real consumer of `connectSession`: the agent is loaded
|
|
254
|
+
* exactly as `aai dev` loads it (same bundle, same `.env` resolution, same
|
|
255
|
+
* shell-credential fallback), a runtime is built in-process, and the session
|
|
256
|
+
* runs over a {@link ClientSink} that plays audio and prints transcripts. Every
|
|
257
|
+
* behaviour a browser session has — turn-taking, barge-in, tools, pacing —
|
|
258
|
+
* comes from the runtime, not from here.
|
|
259
|
+
*/
|
|
260
|
+
/**
|
|
261
|
+
* The pacing lead for a speaker on this machine, in ms.
|
|
262
|
+
*
|
|
263
|
+
* Well under the browser's `CLIENT_AUDIO_LEAD_MS` (1500), because what that
|
|
264
|
+
* lead buys — riding out a lossy network — does not exist between a process
|
|
265
|
+
* and its own sound card, while what it costs does: audio already handed to
|
|
266
|
+
* the device is audio a barge-in has to kill the player to discard.
|
|
267
|
+
*/
|
|
268
|
+
const CONSOLE_AUDIO_LEAD_MS = 400;
|
|
269
|
+
/**
|
|
270
|
+
* How long to wait for the microphone's first bytes before opening the speaker
|
|
271
|
+
* anyway, in ms. Measured on AirPods: 0.74 s. Past this the speaker opens
|
|
272
|
+
* regardless — a slow mic is a possibly-wrong rate, a hang is certainly wrong.
|
|
273
|
+
*/
|
|
274
|
+
const MIC_OPEN_TIMEOUT_MS = 3e3;
|
|
275
|
+
/** The runtime's own lines go to stderr, and its chatty ones only when asked. */
|
|
276
|
+
function consoleRuntimeLogger(verbose) {
|
|
277
|
+
const write = (msg, ctx) => {
|
|
278
|
+
process.stderr.write(`${styleText("dim", msg)}${ctx === void 0 ? "" : ` ${styleText("dim", JSON.stringify(ctx))}`}\n`);
|
|
279
|
+
};
|
|
280
|
+
const quiet = () => void 0;
|
|
281
|
+
return {
|
|
282
|
+
info: verbose ? write : quiet,
|
|
283
|
+
debug: verbose ? write : quiet,
|
|
284
|
+
warn: write,
|
|
285
|
+
error: write
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
/** Print the conversation to stdout. */
|
|
289
|
+
function terminalPrinter(write) {
|
|
290
|
+
return {
|
|
291
|
+
user: (text) => write(`${styleText(["bold", "cyan"], "you ")} ${text}`),
|
|
292
|
+
agent: (text, interrupted) => write(`${styleText(["bold", "green"], "agent ")} ${text}${interrupted ? styleText("dim", " [interrupted]") : ""}`),
|
|
293
|
+
tool: (name, args) => write(styleText("dim", ` → ${name}(${JSON.stringify(args)})`)),
|
|
294
|
+
error: (message, fatal) => write(styleText(fatal ? "red" : "yellow", ` ${fatal ? "error" : "warning"}: ${message}`))
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Run one console session until Ctrl-C, or until the session ends itself.
|
|
299
|
+
*
|
|
300
|
+
* `audio` is a parameter so the spec can drive it without a sound card.
|
|
301
|
+
*/
|
|
302
|
+
async function executeConsole(opts) {
|
|
303
|
+
const audio = opts.audio ?? soxAudio;
|
|
304
|
+
const logger = consoleRuntimeLogger(opts.verbose === true);
|
|
305
|
+
log.step("Bundling agent…");
|
|
306
|
+
let agentDef;
|
|
307
|
+
try {
|
|
308
|
+
agentDef = await loadWorker(opts.cwd, createWorkerEvaluator());
|
|
309
|
+
} catch (err) {
|
|
310
|
+
return fail("build_failed", `Could not load agent.ts: ${errorMessage(err)}`);
|
|
311
|
+
}
|
|
312
|
+
if (agentDef.page === "static") return fail("not_a_voice_agent", "This agent declares `page: \"static\"` — a workflow app has no voice session to talk to.", "Run `aai dev` and open the page instead.");
|
|
313
|
+
const env = await resolveAgentEnv(opts.cwd, agentDef);
|
|
314
|
+
if (env.DATABASE_URL) {
|
|
315
|
+
await ensureSessionStateSchema({
|
|
316
|
+
url: env.DATABASE_URL,
|
|
317
|
+
logger
|
|
318
|
+
});
|
|
319
|
+
await ensureWorkflowJournalSchema({
|
|
320
|
+
url: env.DATABASE_URL,
|
|
321
|
+
logger
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
const runtime = createRuntime({
|
|
325
|
+
agent: agentDef,
|
|
326
|
+
env,
|
|
327
|
+
providerEnv: withHostCredentialFallback(env),
|
|
328
|
+
logger
|
|
329
|
+
});
|
|
330
|
+
const { sampleRate, ttsSampleRate } = runtime.readyConfig;
|
|
331
|
+
let deviceError;
|
|
332
|
+
let endNow = () => void 0;
|
|
333
|
+
const ended = new Promise((resolve) => {
|
|
334
|
+
endNow = resolve;
|
|
335
|
+
});
|
|
336
|
+
const onDeviceError = (err) => {
|
|
337
|
+
deviceError ??= err;
|
|
338
|
+
endNow();
|
|
339
|
+
};
|
|
340
|
+
let forward = () => void 0;
|
|
341
|
+
let micOpened = () => void 0;
|
|
342
|
+
const micReady = new Promise((resolve) => {
|
|
343
|
+
micOpened = resolve;
|
|
344
|
+
});
|
|
345
|
+
const capture = audio.startCapture(sampleRate, (bytes) => {
|
|
346
|
+
micOpened();
|
|
347
|
+
forward(bytes);
|
|
348
|
+
}, onDeviceError);
|
|
349
|
+
await pTimeout(Promise.race([micReady, ended]), {
|
|
350
|
+
milliseconds: MIC_OPEN_TIMEOUT_MS,
|
|
351
|
+
fallback: () => void 0
|
|
352
|
+
});
|
|
353
|
+
const player = audio.startPlayback(ttsSampleRate, onDeviceError);
|
|
354
|
+
const out = opts.mode === "json" ? process.stderr : process.stdout;
|
|
355
|
+
const print = terminalPrinter((line) => out.write(`${line}\n`));
|
|
356
|
+
let closedReason;
|
|
357
|
+
let fatalMessage;
|
|
358
|
+
const sink = createConsoleSink({
|
|
359
|
+
player,
|
|
360
|
+
print,
|
|
361
|
+
onClosed: (reason) => {
|
|
362
|
+
closedReason = reason;
|
|
363
|
+
endNow();
|
|
364
|
+
},
|
|
365
|
+
onFatal: (message) => {
|
|
366
|
+
fatalMessage ??= message;
|
|
367
|
+
endNow();
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
const connection = connectSession(runtime, sink, {
|
|
371
|
+
audioLeadMs: CONSOLE_AUDIO_LEAD_MS,
|
|
372
|
+
logContext: { transport: "console" }
|
|
373
|
+
});
|
|
374
|
+
forward = createSampleAligner(connection);
|
|
375
|
+
connection.sendCommand({ type: "audio_ready" });
|
|
376
|
+
log.success(`Talking to ${agentDef.name}. Press Ctrl-C to hang up.`);
|
|
377
|
+
log.info("Use headphones: without echo cancellation the agent hears itself and interrupts.");
|
|
378
|
+
const quit = opts.untilQuit === void 0 ? signalled() : void 0;
|
|
379
|
+
try {
|
|
380
|
+
await Promise.race([
|
|
381
|
+
opts.untilQuit ?? quit?.signal,
|
|
382
|
+
ended,
|
|
383
|
+
connection.ended
|
|
384
|
+
]);
|
|
385
|
+
} finally {
|
|
386
|
+
quit?.dispose();
|
|
387
|
+
}
|
|
388
|
+
capture.stop();
|
|
389
|
+
connection.close();
|
|
390
|
+
await connection.ended;
|
|
391
|
+
player.stop();
|
|
392
|
+
try {
|
|
393
|
+
await runtime.shutdown();
|
|
394
|
+
} catch (err) {
|
|
395
|
+
logger.warn(`Runtime shutdown failed: ${errorDetail(err)}`);
|
|
396
|
+
}
|
|
397
|
+
if (deviceError) return fail("audio_device", deviceError.message);
|
|
398
|
+
if (fatalMessage !== void 0) return fail("session_failed", fatalMessage);
|
|
399
|
+
if (closedReason === "session start failed") return fail("session_failed", "The session failed to start — see the error above.");
|
|
400
|
+
return ok({ sessionId: connection.id });
|
|
401
|
+
}
|
|
402
|
+
/** The first SIGINT or SIGTERM, and a way to stop listening for it. */
|
|
403
|
+
function signalled() {
|
|
404
|
+
let dispose = () => void 0;
|
|
405
|
+
return {
|
|
406
|
+
signal: new Promise((resolve) => {
|
|
407
|
+
const done = () => {
|
|
408
|
+
dispose();
|
|
409
|
+
resolve();
|
|
410
|
+
};
|
|
411
|
+
dispose = () => {
|
|
412
|
+
process.off("SIGINT", done);
|
|
413
|
+
process.off("SIGTERM", done);
|
|
414
|
+
};
|
|
415
|
+
process.on("SIGINT", done);
|
|
416
|
+
process.on("SIGTERM", done);
|
|
417
|
+
}),
|
|
418
|
+
dispose
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
//#endregion
|
|
422
|
+
export { executeConsole };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `aai console` — talk to the agent through this machine's microphone and
|
|
3
|
+
* speakers, with the conversation printed in the terminal. No server, no
|
|
4
|
+
* browser, no socket.
|
|
5
|
+
*
|
|
6
|
+
* It is the smallest real consumer of `connectSession`: the agent is loaded
|
|
7
|
+
* exactly as `aai dev` loads it (same bundle, same `.env` resolution, same
|
|
8
|
+
* shell-credential fallback), a runtime is built in-process, and the session
|
|
9
|
+
* runs over a {@link ClientSink} that plays audio and prints transcripts. Every
|
|
10
|
+
* behaviour a browser session has — turn-taking, barge-in, tools, pacing —
|
|
11
|
+
* comes from the runtime, not from here.
|
|
12
|
+
*/
|
|
13
|
+
import { type ConsoleAudio, type ConsolePrinter } from "./_console-session.ts";
|
|
14
|
+
import { type CommandResult, type OutputMode } from "./_output.ts";
|
|
15
|
+
type ConsoleData = {
|
|
16
|
+
sessionId: string;
|
|
17
|
+
};
|
|
18
|
+
/** Print the conversation to stdout. */
|
|
19
|
+
export declare function terminalPrinter(write: (line: string) => void): ConsolePrinter;
|
|
20
|
+
/**
|
|
21
|
+
* Run one console session until Ctrl-C, or until the session ends itself.
|
|
22
|
+
*
|
|
23
|
+
* `audio` is a parameter so the spec can drive it without a sound card.
|
|
24
|
+
*/
|
|
25
|
+
export declare function executeConsole(opts: {
|
|
26
|
+
cwd: string;
|
|
27
|
+
verbose?: boolean | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* JSON mode owes stdout exactly one result line, and JSON mode is
|
|
30
|
+
* auto-selected on a pipe — so there the conversation goes to stderr.
|
|
31
|
+
*/
|
|
32
|
+
mode?: OutputMode;
|
|
33
|
+
audio?: ConsoleAudio;
|
|
34
|
+
/** Resolves when the user asks to quit. Defaults to the first SIGINT/SIGTERM. */
|
|
35
|
+
untilQuit?: Promise<void>;
|
|
36
|
+
}): Promise<CommandResult<ConsoleData>>;
|
|
37
|
+
export {};
|
|
@@ -13,7 +13,7 @@ import { startTracing } from "@alexkroman1/aai-runtime/tracing";
|
|
|
13
13
|
async function executeDev(opts) {
|
|
14
14
|
const port = parsePort(opts.port);
|
|
15
15
|
const agentName = path.basename(path.resolve(opts.cwd));
|
|
16
|
-
const { startDevServer } = await import("./_dev-server-
|
|
16
|
+
const { startDevServer } = await import("./_dev-server-Dtp4aAlo.mjs");
|
|
17
17
|
let cleanup;
|
|
18
18
|
let shuttingDown = false;
|
|
19
19
|
const onSignal = () => {
|
|
@@ -3,7 +3,7 @@ import { a as ok } from "./_output-DBP9Op_d.mjs";
|
|
|
3
3
|
import { n as log, s as unwrapCancel } from "./_ui-DCt4qZrk.mjs";
|
|
4
4
|
import { c as fileExists, d as readJson, g as writeJson, i as compareCodeUnits, p as resolveCwd, s as errorMessage, t as AGENT_ENTRY, u as isEexist } from "./_utils-D5JGcjiW.mjs";
|
|
5
5
|
import { r as isDevMode, t as getMonorepoRoot } from "./_agent-BzUeqOdj.mjs";
|
|
6
|
-
import { REPO_URL, downloadAndMergeTemplate, listTemplates } from "./_templates-
|
|
6
|
+
import { REPO_URL, downloadAndMergeTemplate, listTemplates } from "./_templates-DXadVNsL.mjs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { styleText } from "node:util";
|
|
9
9
|
import * as p from "@clack/prompts";
|