@alexkroman1/aai-cli 16.1.0 → 17.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/README.md +2 -1
- package/dist/{_build-target-ClEEuUuW.mjs → _build-target-C08aM-Es.mjs} +504 -43
- package/dist/_build-target.d.ts +98 -19
- package/dist/_deno-target.d.ts +15 -14
- package/dist/_dev-env.d.ts +19 -13
- package/dist/{_dev-server-D1wKiA0_.mjs → _dev-server-CLU9xjH5.mjs} +25 -17
- package/dist/_dev-server.d.ts +4 -3
- package/dist/_init.d.ts +39 -0
- package/dist/_modal-target.d.ts +11 -6
- package/dist/_preflight-CnW9QiE7.mjs +116 -0
- package/dist/_preflight.d.ts +110 -5
- package/dist/_target-bundle.d.ts +112 -0
- package/dist/_target-entry.d.ts +81 -0
- package/dist/{_templates-CuP74F8D.mjs → _templates-IMeKMVIj.mjs} +1 -1
- package/dist/{_vitest-runner-BiAMIir1.mjs → _vitest-runner-CZNGj0ZC.mjs} +21 -16
- package/dist/_vitest-runner.d.ts +35 -23
- package/dist/{build-O6BT51x_.mjs → build-DrQTSc2R.mjs} +128 -293
- package/dist/build.d.ts +34 -4
- package/dist/cli.mjs +20 -16
- package/dist/{deploy-lldQyJVj.mjs → deploy-B68-x7xM.mjs} +12 -4
- package/dist/deploy.d.ts +9 -0
- package/dist/{dev-Du8pCWFq.mjs → dev-ChsrNHpK.mjs} +1 -1
- package/dist/dev.d.ts +1 -1
- package/dist/{eval-Ducmkt1n.mjs → eval-DB07kTxO.mjs} +1 -1
- package/dist/init-W-4k-lCF.mjs +559 -0
- package/dist/init.d.ts +43 -3
- package/dist/scaffold/CLAUDE.md +51 -64
- package/dist/scaffold/package.json +6 -6
- package/dist/scaffold/vite.config.ts +29 -1
- package/dist/{studio-It7SRfUN.mjs → studio-DEAJGBXa.mjs} +19 -9
- package/dist/templates/applicant-screening-agent/agent.eval.test.ts +20 -6
- package/dist/templates/applicant-screening-agent/agent.ts +28 -0
- package/dist/templates/applicant-screening-agent/shared.ts +11 -11
- package/dist/templates/call-audit-workflow/agent.ts +2 -0
- package/dist/templates/code-interpreter-agent/agent.eval.test.ts +3 -2
- package/dist/templates/code-interpreter-agent/agent.ts +1 -0
- package/dist/templates/coding-agent/agent.eval.test.ts +136 -0
- package/dist/templates/coding-agent/agent.test.ts +109 -0
- package/dist/templates/coding-agent/agent.ts +45 -0
- package/dist/templates/coding-agent/chat.ts +63 -0
- package/dist/templates/coding-agent/shared.ts +46 -0
- package/dist/templates/coding-agent/system-prompt.md +32 -0
- package/dist/templates/coding-agent/tools/bash.ts +6 -0
- package/dist/templates/coding-agent/tools/delete_file.ts +6 -0
- package/dist/templates/coding-agent/tools/edit_file.ts +6 -0
- package/dist/templates/coding-agent/tools/glob.ts +6 -0
- package/dist/templates/coding-agent/tools/grep.ts +6 -0
- package/dist/templates/coding-agent/tools/list_files.ts +6 -0
- package/dist/templates/coding-agent/tools/read_file.ts +6 -0
- package/dist/templates/coding-agent/tools/todo_write.ts +6 -0
- package/dist/templates/coding-agent/tools/write_file.ts +6 -0
- package/dist/templates/custom-pipeline-agent/agent.ts +1 -0
- package/dist/templates/document-redline-workflow/agent.ts +2 -0
- package/dist/templates/emergency-dispatch-agent/agent.test.ts +103 -0
- package/dist/templates/emergency-dispatch-agent/agent.ts +15 -0
- package/dist/templates/emergency-dispatch-agent/guardrails.ts +105 -0
- package/dist/templates/emergency-dispatch-agent/system-prompt.md +1 -0
- package/dist/templates/entertainment-picks-agent/agent.eval.test.ts +8 -1
- package/dist/templates/entertainment-picks-agent/agent.ts +1 -0
- package/dist/templates/entertainment-picks-agent/system-prompt.md +6 -0
- package/dist/templates/entertainment-picks-agent/tools/revisit.ts +7 -0
- package/dist/templates/executive-inbox-agent/agent.eval.test.ts +84 -23
- package/dist/templates/executive-inbox-agent/agent.ts +1 -0
- package/dist/templates/executive-inbox-agent/meeting.ts +1 -1
- package/dist/templates/executive-inbox-agent/prompts.ts +3 -1
- package/dist/templates/executive-inbox-agent/system-prompt.md +3 -3
- package/dist/templates/executive-inbox-agent/tools/meeting_assistant.ts +3 -3
- package/dist/templates/executive-inbox-agent/tools/open_email.ts +8 -16
- package/dist/templates/hotel-reception-agent/agent.eval.test.ts +85 -21
- package/dist/templates/hotel-reception-agent/agent.test.ts +2 -1
- package/dist/templates/hotel-reception-agent/agent.ts +6 -3
- package/dist/templates/hotel-reception-agent/client.tsx +14 -2
- package/dist/templates/hotel-reception-agent/concierge.ts +2 -1
- package/dist/templates/hotel-reception-agent/desk-seams.test.ts +2 -1
- package/dist/templates/hotel-reception-agent/disputes.ts +9 -11
- package/dist/templates/hotel-reception-agent/events.ts +2 -1
- package/dist/templates/hotel-reception-agent/session.ts +56 -0
- package/dist/templates/hotel-reception-agent/shared.ts +20 -21
- package/dist/templates/hotel-reception-agent/system-prompt.md +1 -1
- package/dist/templates/hotel-reception-agent/tools/abandon_booking.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/add_to_waitlist.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/book_airport_car.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/cancel_restaurant_reservation.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/cancel_room_booking.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/check_restaurant_availability.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/check_room_availability.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/choose_room.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/confirm_booking.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/dispatch_emergency.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/dispute_charge.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/flag_late_arrival.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/lookup_booking.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/lookup_guest_history.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/lookup_invoice.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/lookup_restaurant_reservation.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/modify_restaurant_reservation.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/record_card.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/record_followup.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/record_group_inquiry.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/record_guest_details.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/reinstate_booking.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/request_flight_reconfirmation.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/resend_confirmation.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/reserve_table.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/resolve_room_conflict.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/schedule_wakeup_call.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/set_do_not_disturb.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/set_extras.ts +1 -1
- package/dist/templates/hotel-reception-agent/tools/set_stay.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/start_booking_modification.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/start_room_booking.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/take_guest_message.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/transfer_call.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/update_card.ts +2 -1
- package/dist/templates/hotel-reception-agent/tools/verify_booking.ts +2 -1
- package/dist/templates/link-digest-workflow/agent.eval.test.ts +16 -1
- package/dist/templates/link-digest-workflow/agent.ts +5 -0
- package/dist/templates/medication-safety-agent/agent.test.ts +54 -0
- package/dist/templates/medication-safety-agent/agent.ts +48 -1
- package/dist/templates/meeting-recap-agent/agent.test.ts +2 -2
- package/dist/templates/meeting-recap-agent/agent.ts +4 -0
- package/dist/templates/pizza-ordering-agent/agent.ts +3 -0
- package/dist/templates/podcast-digest-workflow/agent.ts +4 -0
- package/dist/templates/quickstart-agent/agent.eval.test.ts +39 -3
- package/dist/templates/quickstart-agent/agent.test.ts +22 -0
- package/dist/templates/quickstart-agent/agent.ts +18 -2
- package/dist/templates/quickstart-agent/system-prompt.md +8 -0
- package/dist/templates/quickstart-agent/tools/get_weather.ts +59 -0
- package/dist/templates/research-handoff-agent/agent.ts +4 -0
- package/dist/templates/research-planner-agent/agent.eval.test.ts +24 -2
- package/dist/templates/research-planner-agent/agent.ts +3 -0
- package/dist/templates/research-planner-agent/system-prompt.md +1 -1
- package/dist/templates/retail-orders-agent/agent.ts +32 -3
- package/dist/templates/retail-orders-agent/keyterms.ts +154 -0
- package/dist/templates/retail-orders-agent/resolve.ts +18 -3
- package/dist/templates/roadside-assistance-agent/agent.eval.test.ts +37 -5
- package/dist/templates/roadside-assistance-agent/agent.test.ts +38 -6
- package/dist/templates/roadside-assistance-agent/agent.ts +4 -0
- package/dist/templates/roadside-assistance-agent/shared.ts +12 -0
- package/dist/templates/roadside-assistance-agent/tools/acknowledge_disclosure.ts +14 -0
- package/dist/templates/roadside-assistance-agent/tools/lookup_coverage.ts +11 -0
- package/dist/templates/roadside-assistance-agent/tools/service_disclosure.ts +17 -11
- package/dist/templates/roadside-assistance-agent/yard.test.ts +4 -0
- package/dist/templates/spoken-summary-workflow/agent.ts +4 -0
- package/dist/templates/tabletop-rpg-agent/agent.eval.test.ts +6 -0
- package/dist/templates/tabletop-rpg-agent/agent.test.ts +125 -1
- package/dist/templates/tabletop-rpg-agent/agent.ts +30 -1
- package/dist/templates/tabletop-rpg-agent/shared.ts +81 -0
- package/dist/templates/tabletop-rpg-agent/system-prompt.md +1 -1
- package/dist/templates/tabletop-rpg-agent/tools/check_state.ts +16 -7
- package/dist/templates/technical-support-agent/agent.test.ts +3 -3
- package/dist/templates/technical-support-agent/agent.ts +5 -1
- package/dist/templates/technical-support-agent/knowledge.ts +165 -0
- package/dist/templates/technical-support-agent/nodes.ts +3 -2
- package/dist/templates/technical-support-agent/procedure.ts +4 -3
- package/dist/templates/technical-support-agent/shared.ts +10 -146
- package/dist/templates/technical-support-agent/tools/list_topics.ts +2 -1
- package/dist/templates/text-adventure-agent/agent.eval.test.ts +39 -6
- package/dist/templates/text-adventure-agent/agent.test.ts +79 -1
- package/dist/templates/text-adventure-agent/agent.ts +30 -1
- package/dist/templates/text-adventure-agent/shared.ts +37 -0
- package/dist/templates/text-adventure-agent/system-prompt.md +9 -2
- package/dist/templates/topic-briefing-agent/agent.eval.test.ts +18 -4
- package/dist/templates/topic-briefing-agent/agent.test.ts +23 -11
- package/dist/templates/topic-briefing-agent/agent.ts +4 -1
- package/dist/templates/topic-briefing-agent/shared.ts +10 -10
- package/dist/templates/topic-briefing-agent/system-prompt.md +13 -8
- package/dist/templates/topic-briefing-agent/tools/send_briefing.ts +43 -5
- package/dist/templates/transcription-workflow/agent.ts +3 -0
- package/dist/templates/travel-concierge-agent/agent.ts +4 -0
- package/dist/templates/web-research-agent/agent.eval.test.ts +31 -5
- package/dist/templates/web-research-agent/agent.test.ts +16 -0
- package/dist/templates/web-research-agent/agent.ts +30 -0
- package/dist/templates/web-research-agent/system-prompt.md +8 -2
- package/dist/templates/word-game-agent/agent.eval.test.ts +79 -18
- package/dist/templates/word-game-agent/agent.ts +20 -0
- package/dist/test-CjKPQ9_B.mjs +102 -0
- package/dist/test.d.ts +37 -12
- package/package.json +5 -5
- package/dist/_init-Bp1CjRrU.mjs +0 -199
- package/dist/_preflight-CcAR577V.mjs +0 -46
- package/dist/init-CY4NAXAA.mjs +0 -183
- package/dist/test-PHHv0Vj6.mjs +0 -83
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What this template CAN and CANNOT reach.
|
|
3
|
+
*
|
|
4
|
+
* A coding agent's tools take a path from a model, so the claims worth pinning
|
|
5
|
+
* are about the boundary: what `WORKSPACE_DIR` selects, that nothing outside it
|
|
6
|
+
* is reachable by any of the nine, and that a failure comes back as a sentence
|
|
7
|
+
* the model can act on rather than as a thrown `ENOENT`. The behaviour of each
|
|
8
|
+
* tool is the SDK's and is covered there; what belongs to this template is the
|
|
9
|
+
* wiring.
|
|
10
|
+
*
|
|
11
|
+
* `vi.hoisted` runs before the imports below, which is the only way to point
|
|
12
|
+
* `shared.ts` at a scratch directory: it reads the variable once, at module
|
|
13
|
+
* scope, so a `beforeEach` would be far too late.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import agentDef from "virtual:aai/agent";
|
|
19
|
+
import { expectDeployable, toolRunner } from "@alexkroman1/aai/testing";
|
|
20
|
+
import { afterAll, beforeEach, describe, expect, test, vi } from "vitest";
|
|
21
|
+
import { WORKSPACE_DIR } from "./shared.ts";
|
|
22
|
+
|
|
23
|
+
const workspace = vi.hoisted(() => {
|
|
24
|
+
// Hoisted above the imports, so it cannot use `node:os` — the temp root is
|
|
25
|
+
// read the way `os.tmpdir()` reads it. The directory need not exist yet:
|
|
26
|
+
// `shared.ts` records the path and touches nothing until a tool runs.
|
|
27
|
+
const root = process.env.TMPDIR ?? process.env.TMP ?? "/tmp";
|
|
28
|
+
const dir = `${root}/coding-agent-template-${process.pid}`;
|
|
29
|
+
process.env.WORKSPACE_DIR = dir;
|
|
30
|
+
return dir;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterAll(() => {
|
|
34
|
+
rmSync(workspace, { recursive: true, force: true });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const run = toolRunner(agentDef);
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
mkdirSync(workspace, { recursive: true });
|
|
41
|
+
writeFileSync(path.join(workspace, "cart.ts"), "export const total = 1;\n", "utf-8");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("coding-agent template", () => {
|
|
45
|
+
test("is deployable, and is a TEXT agent — no audio stage anywhere", () => {
|
|
46
|
+
const config = expectDeployable(agentDef);
|
|
47
|
+
// The mode is the template's point: `createRuntime` refuses a text agent by
|
|
48
|
+
// name, so this is what says it runs on `createTextAgent` instead. A
|
|
49
|
+
// provider triple appearing here would mean the mode had been lost.
|
|
50
|
+
expect(config.text).toBe(true);
|
|
51
|
+
expect(config.s2s).toBeUndefined();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("declares the nine workspace tools, by the names the model calls", () => {
|
|
55
|
+
expect(Object.keys(agentDef.tools).sort()).toEqual([
|
|
56
|
+
"bash",
|
|
57
|
+
"delete_file",
|
|
58
|
+
"edit_file",
|
|
59
|
+
"glob",
|
|
60
|
+
"grep",
|
|
61
|
+
"list_files",
|
|
62
|
+
"read_file",
|
|
63
|
+
"todo_write",
|
|
64
|
+
"write_file",
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("the tools work in WORKSPACE_DIR, which is the one thing to configure", async () => {
|
|
69
|
+
expect(WORKSPACE_DIR).toBe(workspace);
|
|
70
|
+
expect(String(await run("list_files", {}))).toContain("cart.ts");
|
|
71
|
+
expect(String(await run("read_file", { path: "cart.ts" }))).toContain("export const total");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("nothing outside the workspace is reachable, by any of the path tools", async () => {
|
|
75
|
+
// A traversal reads as a file that is not there — the same answer a typo
|
|
76
|
+
// gets, which is the recovery the model already knows.
|
|
77
|
+
expect(String(await run("read_file", { path: "../../etc/passwd" }))).toContain(
|
|
78
|
+
"Error: no such file",
|
|
79
|
+
);
|
|
80
|
+
// A WRITE that escapes THROWS rather than answering, which is deliberate:
|
|
81
|
+
// in a real turn the runtime's executor shapes a thrown tool into an error
|
|
82
|
+
// result, so the model sees a refusal it cannot mistake for a file it has
|
|
83
|
+
// yet to create. Here there is no executor, so the throw is what a spec
|
|
84
|
+
// sees.
|
|
85
|
+
await expect(run("write_file", { path: "../escaped.ts", content: "x" })).rejects.toThrow(
|
|
86
|
+
/escapes the workspace/,
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("an edit is applied to disk and reported as a diff", async () => {
|
|
91
|
+
const out = String(
|
|
92
|
+
await run("edit_file", {
|
|
93
|
+
path: "cart.ts",
|
|
94
|
+
oldText: "const total = 1;",
|
|
95
|
+
newText: "const total = 2;",
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
expect(out).toContain("Edited cart.ts");
|
|
99
|
+
expect(readFileSync(path.join(workspace, "cart.ts"), "utf-8")).toBe(
|
|
100
|
+
"export const total = 2;\n",
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("a failed match is advice, not an exception — the agent can recover", async () => {
|
|
105
|
+
expect(
|
|
106
|
+
String(await run("edit_file", { path: "cart.ts", oldText: "nope", newText: "x" })),
|
|
107
|
+
).toContain("Could not find that text");
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A coding agent: it reads a codebase, changes it, and runs commands in it.
|
|
3
|
+
*
|
|
4
|
+
* `text: true` is the whole shape of it. A text agent has no audio path at all
|
|
5
|
+
* — no microphone, no speech, no transport — so what runs it is
|
|
6
|
+
* `createTextAgent` rather than a voice session, and a turn is a message in and
|
|
7
|
+
* a reply out. That is the right mode for work measured in files rather than in
|
|
8
|
+
* seconds of silence: a type check that takes 40 seconds is a fine tool call
|
|
9
|
+
* here and a failed turn on a phone call.
|
|
10
|
+
*
|
|
11
|
+
* The tools are the nine in `tools/`, all of them the SDK's
|
|
12
|
+
* (`@alexkroman1/aai/coding-tools`) over one directory — see `shared.ts` for
|
|
13
|
+
* which directory, which is the one decision worth making deliberately before
|
|
14
|
+
* running this.
|
|
15
|
+
*
|
|
16
|
+
* ## Making it yours
|
|
17
|
+
*
|
|
18
|
+
* - **`system-prompt.md` is the agent.** The tools are generic; how carefully
|
|
19
|
+
* it reads before it edits, whether it runs the tests, what it does when it
|
|
20
|
+
* is unsure — all of that is prose in that file, and it is where the first
|
|
21
|
+
* twenty edits go.
|
|
22
|
+
* - **Add a tool by adding a FILE** to `tools/`. The nine here re-export a
|
|
23
|
+
* shared registry; yours is an ordinary `tool({ … })`, and the build
|
|
24
|
+
* enumerates the directory either way.
|
|
25
|
+
* - **`maxSteps` is a budget for a whole task**, not a turn of conversation.
|
|
26
|
+
* Reading four files, editing two and running the tests is already eight
|
|
27
|
+
* calls, and the default (10) is sized for a voice agent's single answer.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { agent } from "@alexkroman1/aai";
|
|
31
|
+
|
|
32
|
+
export default agent({
|
|
33
|
+
name: "Coding Agent",
|
|
34
|
+
description: "Reads, edits and runs a codebase from a terminal chat",
|
|
35
|
+
// No `llm` declared, so it runs the default AssemblyAI LLM gateway on
|
|
36
|
+
// ASSEMBLYAI_API_KEY. Swap in `anthropicLlm({ model: "claude-opus-5" })` (or
|
|
37
|
+
// any provider from `@alexkroman1/aai/llm`) when a task needs a stronger
|
|
38
|
+
// model — the credential follows the descriptor.
|
|
39
|
+
text: true,
|
|
40
|
+
// Long, deliberately: a real change is read-read-edit-run-fix, and a budget
|
|
41
|
+
// that runs out mid-task leaves a half-applied edit behind. The last step is
|
|
42
|
+
// always spent ANSWERING rather than calling another tool, so a capped task
|
|
43
|
+
// ends with the agent saying where it got to.
|
|
44
|
+
maxSteps: 60,
|
|
45
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run the agent from a terminal: `node chat.ts` (Node 24 runs TypeScript
|
|
3
|
+
* directly), then type a request and press enter.
|
|
4
|
+
*
|
|
5
|
+
* A text agent has no session, so this file is its front door rather than
|
|
6
|
+
* `aai dev`: `createRuntime` REFUSES `text: true` by name, because there are no
|
|
7
|
+
* speech stages for a transport to sit between and no socket for a browser to
|
|
8
|
+
* open. What runs it instead is `createTextAgent` — a message list and a model.
|
|
9
|
+
* Everything else an agent has is the same code a voice agent runs: the tools,
|
|
10
|
+
* the tool executor and its per-call deadline, `ctx`, the step budget, and the
|
|
11
|
+
* last step reserved for an answer.
|
|
12
|
+
*
|
|
13
|
+
* Four lines are doing the interesting work:
|
|
14
|
+
*
|
|
15
|
+
* - `withToolsDir` is what makes a tool a FILE here. A deployed agent gets its
|
|
16
|
+
* tools from the bundler, which enumerates `tools/` at build time; a plain
|
|
17
|
+
* Node process has no bundler, so it reads the directory itself. Without it,
|
|
18
|
+
* `agent.ts`'s default export is an agent with no tools at all.
|
|
19
|
+
* - `env` is where the LLM's credential is read from, and it is also what tool
|
|
20
|
+
* code sees as `ctx.env` — so pass what the agent needs and nothing else.
|
|
21
|
+
* - `toolTimeoutMs` replaces the SDK's default of 30 seconds, which is a VOICE
|
|
22
|
+
* budget: past it a caller is listening to silence, so a slow tool is already
|
|
23
|
+
* a failed turn. Nobody is on a phone here, and a type check or an install is
|
|
24
|
+
* a minute of honest work.
|
|
25
|
+
* - The conversation is a `ModelMessage[]` this file keeps. One `createTextAgent`
|
|
26
|
+
* is one conversation, and what carries the history between turns is the
|
|
27
|
+
* array — so this is also where you would trim it, persist it, or start over.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import path from "node:path";
|
|
31
|
+
import { createInterface } from "node:readline/promises";
|
|
32
|
+
import { createTextAgent, withToolsDir } from "@alexkroman1/aai-runtime";
|
|
33
|
+
import agentDef from "./agent.ts";
|
|
34
|
+
import { WORKSPACE_DIR } from "./shared.ts";
|
|
35
|
+
|
|
36
|
+
const agent = await withToolsDir(agentDef, path.join(import.meta.dirname, "tools"));
|
|
37
|
+
|
|
38
|
+
const chat = createTextAgent({
|
|
39
|
+
agent,
|
|
40
|
+
env: { ASSEMBLYAI_API_KEY: process.env.ASSEMBLYAI_API_KEY ?? "" },
|
|
41
|
+
toolTimeoutMs: 120_000,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// The turn's own message type, read off `stream` rather than imported from
|
|
45
|
+
// `ai` — a project scaffolded from this template declares the SDK packages and
|
|
46
|
+
// not the vendor's, and this needs no second dependency to say the same thing.
|
|
47
|
+
const messages: Parameters<typeof chat.stream>[0]["messages"] = [];
|
|
48
|
+
const io = createInterface({ input: process.stdin, output: process.stdout });
|
|
49
|
+
console.log(`Working in ${WORKSPACE_DIR}\n(ctrl-c to quit)\n`);
|
|
50
|
+
|
|
51
|
+
for (;;) {
|
|
52
|
+
const line = (await io.question("> ")).trim();
|
|
53
|
+
if (line.length === 0) continue;
|
|
54
|
+
messages.push({ role: "user", content: line });
|
|
55
|
+
const result = chat.stream({ messages });
|
|
56
|
+
// Printed as it arrives: a turn that reads four files and edits two is a
|
|
57
|
+
// long wait, and a spinner tells you less than the reply does.
|
|
58
|
+
for await (const delta of result.textStream) process.stdout.write(delta);
|
|
59
|
+
// Every message the turn produced — the assistant's own, plus one per tool
|
|
60
|
+
// call and result — so the next turn sees what this one did.
|
|
61
|
+
messages.push(...(await result.response).messages);
|
|
62
|
+
process.stdout.write("\n\n");
|
|
63
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createCodingTools } from "@alexkroman1/aai/coding-tools";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Where the agent may read and write.
|
|
5
|
+
*
|
|
6
|
+
* Everything the tools do is resolved against this directory and nothing
|
|
7
|
+
* outside it is reachable — a path that escapes is refused rather than
|
|
8
|
+
* followed, whatever the model asks for. So this is the ONE line that decides
|
|
9
|
+
* how much of the machine this agent can touch, and it is worth being
|
|
10
|
+
* deliberate about it: point it at a project, a scratch checkout, or a
|
|
11
|
+
* container's `/workspace`, and not at a home directory.
|
|
12
|
+
*
|
|
13
|
+
* `bash` is the reason to say that plainly. It runs whatever the model wrote
|
|
14
|
+
* with the authority of this process — which is the same authority the write
|
|
15
|
+
* and delete tools already have, so it grants nothing new; what it does is make
|
|
16
|
+
* the grant obvious. Run this agent inside a container and treat the container
|
|
17
|
+
* as the boundary.
|
|
18
|
+
*/
|
|
19
|
+
export const WORKSPACE_DIR = process.env.WORKSPACE_DIR ?? process.cwd();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The agent's tools, built ONCE over {@link WORKSPACE_DIR}.
|
|
23
|
+
*
|
|
24
|
+
* A tool is normally a FILE — `agent()` takes no `tools` argument, and the
|
|
25
|
+
* build enumerates `tools/`. These nine are files too (see the directory
|
|
26
|
+
* beside this one), but each one re-exports an entry from this registry rather
|
|
27
|
+
* than declaring a tool of its own, because every one of them closes over the
|
|
28
|
+
* same directory: nine `createCodingTools` calls would be nine chances for one
|
|
29
|
+
* of them to be pointed somewhere else.
|
|
30
|
+
*
|
|
31
|
+
* Three seams are worth knowing about before you extend this
|
|
32
|
+
* (`CodingToolsOptions` carries the rest):
|
|
33
|
+
*
|
|
34
|
+
* - `validate` refuses a write BEFORE it lands — return a message and the file
|
|
35
|
+
* on disk is untouched. Parsing the content is the useful check: a file that
|
|
36
|
+
* does not parse cannot be edited back into shape by text matching, so
|
|
37
|
+
* writing it strands the turn.
|
|
38
|
+
* - `afterWrite` appends to a write that SUCCEEDED, which is where a type
|
|
39
|
+
* checker or a linter's output belongs — the agent reads it inside the
|
|
40
|
+
* result of the write that caused it, and fixes it on the next step instead
|
|
41
|
+
* of discovering it a build later.
|
|
42
|
+
* - `env` is what `bash` runs with. It defaults to this process's environment;
|
|
43
|
+
* hand it an allow-list when this agent is running somewhere that holds
|
|
44
|
+
* credentials it has no business reading.
|
|
45
|
+
*/
|
|
46
|
+
export const codingTools = createCodingTools({ dir: WORKSPACE_DIR });
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
You are a coding agent working in one directory. You read code, change it, and run commands in it, and you report what you did in plain prose.
|
|
2
|
+
|
|
3
|
+
You are working with a colleague, not performing for one. Say what you found and what you changed. Do not narrate every tool call, and do not summarize the conversation back at the end of every turn.
|
|
4
|
+
|
|
5
|
+
HOW TO WORK
|
|
6
|
+
|
|
7
|
+
- READ BEFORE YOU EDIT. Never change a file you have not read this conversation, and never write one from what you assume its contents to be. `grep` and `glob` find things far more cheaply than reading whole files — use them to locate, then read the file that matters.
|
|
8
|
+
- PREFER `edit_file`. It replaces one exact snippet and shows you the diff. `write_file` is for a new file or a genuine wholesale rewrite; using it for a small change is how code that was supposed to stay gets silently dropped.
|
|
9
|
+
- MATCH THE CODE AROUND YOU. Read a neighbouring file before adding one. Naming, error handling, comment density, and test style are decisions this project already made — follow them rather than importing your own.
|
|
10
|
+
- VERIFY WHAT YOU CHANGED. If the project has tests, a linter or a type checker, run them with `bash` and fix what they report. A change you have not run is a change you are guessing about, and saying so is better than implying otherwise.
|
|
11
|
+
- WORK IN PARALLEL WHERE IT IS SAFE. Independent reads, searches and writes can be issued in one step; edits to the same file cannot.
|
|
12
|
+
|
|
13
|
+
WHEN SOMETHING IS UNCLEAR
|
|
14
|
+
|
|
15
|
+
- Ask, rather than guessing, when two readings of the request would produce materially different work. Ask once, in one sentence, and say what you would do by default.
|
|
16
|
+
- Do the part you are sure of first. If a request has four steps and one is ambiguous, do the three and name the fourth.
|
|
17
|
+
- If a request would break something you can see, say so in a sentence, then do what was asked unless it is destructive.
|
|
18
|
+
|
|
19
|
+
WHAT NOT TO DO
|
|
20
|
+
|
|
21
|
+
- Do not delete or overwrite files that were not part of the request.
|
|
22
|
+
- Do not run destructive shell commands (`rm -rf`, `git reset --hard`, anything that force-pushes or drops data) unless the request is explicitly about that.
|
|
23
|
+
- Do not disable, skip or delete a failing test to make a check pass. A failing test is a finding; report it.
|
|
24
|
+
- Do not add dependencies, reformat files wholesale, or "clean up" code the request did not mention.
|
|
25
|
+
|
|
26
|
+
PLANNING
|
|
27
|
+
|
|
28
|
+
Use `todo_write` for work with several distinct steps — a change plus its tests, or three separate capabilities. Write the steps up front, keep exactly one `in_progress`, and resend the list as statuses change; the person you are working with reads it as your progress report. Skip it entirely for a question or a one-file change.
|
|
29
|
+
|
|
30
|
+
REPORTING
|
|
31
|
+
|
|
32
|
+
When you finish, say what you changed and where, in one short paragraph or a few lines — file paths, not a diff, which they can read for themselves. If you left something out, or a check is still failing, say that too, plainly and without apologising for it.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { codingTools } from "../shared.ts";
|
|
2
|
+
|
|
3
|
+
// A tool is its FILE, so the name the model calls is this file's name. The
|
|
4
|
+
// definition itself is one entry of the shared registry — see `../shared.ts`
|
|
5
|
+
// for why all nine are built together rather than one per file.
|
|
6
|
+
export default codingTools.bash;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { codingTools } from "../shared.ts";
|
|
2
|
+
|
|
3
|
+
// A tool is its FILE, so the name the model calls is this file's name. The
|
|
4
|
+
// definition itself is one entry of the shared registry — see `../shared.ts`
|
|
5
|
+
// for why all nine are built together rather than one per file.
|
|
6
|
+
export default codingTools.delete_file;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { codingTools } from "../shared.ts";
|
|
2
|
+
|
|
3
|
+
// A tool is its FILE, so the name the model calls is this file's name. The
|
|
4
|
+
// definition itself is one entry of the shared registry — see `../shared.ts`
|
|
5
|
+
// for why all nine are built together rather than one per file.
|
|
6
|
+
export default codingTools.edit_file;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { codingTools } from "../shared.ts";
|
|
2
|
+
|
|
3
|
+
// A tool is its FILE, so the name the model calls is this file's name. The
|
|
4
|
+
// definition itself is one entry of the shared registry — see `../shared.ts`
|
|
5
|
+
// for why all nine are built together rather than one per file.
|
|
6
|
+
export default codingTools.glob;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { codingTools } from "../shared.ts";
|
|
2
|
+
|
|
3
|
+
// A tool is its FILE, so the name the model calls is this file's name. The
|
|
4
|
+
// definition itself is one entry of the shared registry — see `../shared.ts`
|
|
5
|
+
// for why all nine are built together rather than one per file.
|
|
6
|
+
export default codingTools.grep;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { codingTools } from "../shared.ts";
|
|
2
|
+
|
|
3
|
+
// A tool is its FILE, so the name the model calls is this file's name. The
|
|
4
|
+
// definition itself is one entry of the shared registry — see `../shared.ts`
|
|
5
|
+
// for why all nine are built together rather than one per file.
|
|
6
|
+
export default codingTools.list_files;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { codingTools } from "../shared.ts";
|
|
2
|
+
|
|
3
|
+
// A tool is its FILE, so the name the model calls is this file's name. The
|
|
4
|
+
// definition itself is one entry of the shared registry — see `../shared.ts`
|
|
5
|
+
// for why all nine are built together rather than one per file.
|
|
6
|
+
export default codingTools.read_file;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { codingTools } from "../shared.ts";
|
|
2
|
+
|
|
3
|
+
// A tool is its FILE, so the name the model calls is this file's name. The
|
|
4
|
+
// definition itself is one entry of the shared registry — see `../shared.ts`
|
|
5
|
+
// for why all nine are built together rather than one per file.
|
|
6
|
+
export default codingTools.todo_write;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { codingTools } from "../shared.ts";
|
|
2
|
+
|
|
3
|
+
// A tool is its FILE, so the name the model calls is this file's name. The
|
|
4
|
+
// definition itself is one entry of the shared registry — see `../shared.ts`
|
|
5
|
+
// for why all nine are built together rather than one per file.
|
|
6
|
+
export default codingTools.write_file;
|
|
@@ -3,6 +3,7 @@ import { anthropicLlm } from "@alexkroman1/aai/llm";
|
|
|
3
3
|
|
|
4
4
|
export default agent({
|
|
5
5
|
name: "Custom Pipeline Assistant",
|
|
6
|
+
description: "Answers general questions on a pipeline whose stages you swap one at a time",
|
|
6
7
|
greeting: "Hi! I'm running in pipeline mode. What can I help with?",
|
|
7
8
|
// Declare only the stage you want elsewhere — here the LLM. Every stage
|
|
8
9
|
// left unset (STT and TTS here) runs on the AssemblyAI default, billed to
|
|
@@ -103,6 +103,8 @@ export const redline = workflow({
|
|
|
103
103
|
|
|
104
104
|
export default workflowApp({
|
|
105
105
|
name: "Redline",
|
|
106
|
+
description:
|
|
107
|
+
"Takes a brief or a draft and returns a piece that has been through rounds of critique",
|
|
106
108
|
workflows: { redline },
|
|
107
109
|
// Checked at deploy time, so a missing key is a warning naming it rather than
|
|
108
110
|
// a run that fails on its first step.
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
} from "@alexkroman1/aai/testing";
|
|
22
22
|
import { describe, expect, test, vi } from "vitest";
|
|
23
23
|
import { DISPATCH_EVENTS } from "./events.ts";
|
|
24
|
+
import { claimsUnitsMoving } from "./guardrails.ts";
|
|
24
25
|
import { callFlow, callSpec, dispatchSlot } from "./shared.ts";
|
|
25
26
|
import incidentAddNote from "./tools/incident_add_note.ts";
|
|
26
27
|
import incidentCreate from "./tools/incident_create.ts";
|
|
@@ -471,3 +472,105 @@ describe("a dropped 911 call", () => {
|
|
|
471
472
|
expect(Object.keys(dispatchSlot.get(ctx).incidents)).toEqual([]);
|
|
472
473
|
});
|
|
473
474
|
});
|
|
475
|
+
|
|
476
|
+
describe("the desk's own output guardrail", () => {
|
|
477
|
+
/**
|
|
478
|
+
* The guardrail as the runtime runs it: the list off the DEF, not off the
|
|
479
|
+
* resolved config.
|
|
480
|
+
*
|
|
481
|
+
* `outputGuardrails` is host-only — `toAgentConfig` strips it with `tools`,
|
|
482
|
+
* `events` and `dialogs`, because a function cannot cross the wire — so
|
|
483
|
+
* `expectDeployable` has nothing to say about it and the check that it is
|
|
484
|
+
* wired at all has to be this one. Looked up and CHECKED rather than reached
|
|
485
|
+
* through `?.[0]`, for the reason `hangUpOn` is: a def that lost the field
|
|
486
|
+
* would leave every assertion below passing against nothing.
|
|
487
|
+
*/
|
|
488
|
+
function say(ctx: ToolContext, text: string): true | string {
|
|
489
|
+
const [guardrail, ...rest] = dispatchAgent.outputGuardrails ?? [];
|
|
490
|
+
if (guardrail === undefined) throw new Error("the def declares no outputGuardrails");
|
|
491
|
+
// One rule, deliberately: a second one would be judged here and asserted
|
|
492
|
+
// nowhere. Adding one is a real edit and should say so by failing.
|
|
493
|
+
if (rest.length > 0) throw new Error(`the def declares ${rest.length + 1} outputGuardrails`);
|
|
494
|
+
const verdict = guardrail(text, ctx);
|
|
495
|
+
// Synchronous by construction here — it reads the board and a regex. An
|
|
496
|
+
// awaited promise would make the assertions below pass on `{}`.
|
|
497
|
+
if (typeof verdict === "object") throw new Error("this guardrail must not be async");
|
|
498
|
+
return verdict;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
test("blocks 'units are on the way' while nothing is assigned, naming the tool", async () => {
|
|
502
|
+
const ctx = createToolContext();
|
|
503
|
+
await createIncidentFor(ctx);
|
|
504
|
+
|
|
505
|
+
const verdict = say(ctx, "Copy that. Medic-1 is en route, ETA four minutes.");
|
|
506
|
+
|
|
507
|
+
expect(verdict).not.toBe(true);
|
|
508
|
+
// The verdict is SPOKEN in place of the reply, so it has to be usable to
|
|
509
|
+
// the dispatcher hearing it — which means naming what to do, not what went
|
|
510
|
+
// wrong.
|
|
511
|
+
expect(verdict).toMatch(/resources_dispatch/);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
test("passes the same sentence once a unit really is assigned", async () => {
|
|
515
|
+
const ctx = createToolContext();
|
|
516
|
+
const incidentId = await createIncidentFor(ctx);
|
|
517
|
+
expectToolOk(await resourcesDispatch.execute({ incidentId, callsigns: ["Medic-1"] }, ctx));
|
|
518
|
+
|
|
519
|
+
// The STATE half is the whole point: the same words are a lie on one board
|
|
520
|
+
// and a report on the other, and only the board can tell them apart.
|
|
521
|
+
expect(say(ctx, "Copy that. Medic-1 is en route, ETA four minutes.")).toBe(true);
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
test("lets the desk SAY that nothing is rolling", async () => {
|
|
525
|
+
const ctx = createToolContext();
|
|
526
|
+
await createIncidentFor(ctx);
|
|
527
|
+
|
|
528
|
+
// The false positive that would matter most: on a shift with nothing
|
|
529
|
+
// assigned, "no units are en route yet" is the truthful sentence and is
|
|
530
|
+
// exactly the one a keyword filter blocks.
|
|
531
|
+
expect(say(ctx, "Negative — no units are en route yet.")).toBe(true);
|
|
532
|
+
expect(say(ctx, "I'll get Engine-7 rolling once you confirm the address.")).toBe(true);
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
test("claimsUnitsMoving is biased toward missing a claim, never toward a false block", () => {
|
|
536
|
+
// The detector alone, away from the board — the half worth pinning, since
|
|
537
|
+
// the guardrail around it is two branches.
|
|
538
|
+
expect(claimsUnitsMoving("Engine-7 is on the way.")).toBe(true);
|
|
539
|
+
expect(claimsUnitsMoving("Units are responding now.")).toBe(true);
|
|
540
|
+
|
|
541
|
+
expect(claimsUnitsMoving("Nothing is rolling yet.")).toBe(false);
|
|
542
|
+
expect(claimsUnitsMoving("Once Medic-2 is en route I'll update you.")).toBe(false);
|
|
543
|
+
expect(
|
|
544
|
+
claimsUnitsMoving("Confirmed, 400 Oak Street. What's the nature of the emergency?"),
|
|
545
|
+
).toBe(false);
|
|
546
|
+
|
|
547
|
+
// Per SENTENCE, so a hedge in one clause does not excuse the next.
|
|
548
|
+
expect(claimsUnitsMoving("No one is assigned yet. Ladder-2 is inbound.")).toBe(true);
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
test("a NEGATED claim is not a claim — the contraction is the whole hedge", () => {
|
|
552
|
+
// `\bn't\b` cannot match: there is no word boundary between the `n` and the
|
|
553
|
+
// `'` of "isn't", so that alternative was dead and every one of these read
|
|
554
|
+
// as a claim — the guardrail speaking a correction over a dispatcher who
|
|
555
|
+
// said the true thing, which inverts this file's stated bias. Each sentence
|
|
556
|
+
// here carries the contraction and NO other hedge, so a `NOT_A_CLAIM` that
|
|
557
|
+
// cannot see one fails on it.
|
|
558
|
+
expect(claimsUnitsMoving("Medic-1 isn't en route.")).toBe(false);
|
|
559
|
+
expect(claimsUnitsMoving("Engine-7 wasn't rolling.")).toBe(false);
|
|
560
|
+
expect(claimsUnitsMoving("I can't confirm anyone is responding.")).toBe(false);
|
|
561
|
+
expect(claimsUnitsMoving("We don't have anyone inbound.")).toBe(false);
|
|
562
|
+
// The typographic apostrophe a model writes as readily as the straight one.
|
|
563
|
+
expect(claimsUnitsMoving("Medic-1 isn\u2019t en route.")).toBe(false);
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
test("the desk can say a negated one out loud, on a board with nothing assigned", async () => {
|
|
567
|
+
const ctx = createToolContext();
|
|
568
|
+
await createIncidentFor(ctx);
|
|
569
|
+
|
|
570
|
+
// The end-to-end shape of the same bug: a true sentence, spoken over. No
|
|
571
|
+
// other hedge in either — take the contraction out and the desk is
|
|
572
|
+
// corrected for reporting the board accurately.
|
|
573
|
+
expect(say(ctx, "Negative — Medic-1 isn't en route.")).toBe(true);
|
|
574
|
+
expect(say(ctx, "We don't have anyone inbound.")).toBe(true);
|
|
575
|
+
});
|
|
576
|
+
});
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { agent } from "@alexkroman1/aai";
|
|
2
2
|
import { DISPATCH_EVENTS } from "./events.ts";
|
|
3
|
+
import { DISPATCH_GUARDRAILS } from "./guardrails.ts";
|
|
3
4
|
import { callFlow, dashboardProjection } from "./shared.ts";
|
|
4
5
|
|
|
5
6
|
export default agent({
|
|
6
7
|
name: "Dispatch Command Center",
|
|
8
|
+
description: "Runs a 911 dispatch shift: logs incidents, triages them, and assigns units",
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* Declaring the flow is what makes its instructions reach a turn that calls
|
|
@@ -42,6 +44,19 @@ export default agent({
|
|
|
42
44
|
*/
|
|
43
45
|
events: DISPATCH_EVENTS,
|
|
44
46
|
|
|
47
|
+
/**
|
|
48
|
+
* The desk's own last check on itself, and the third of the three things
|
|
49
|
+
* `agent()` can declare about a turn — the gates decide which tools are
|
|
50
|
+
* LEGAL, `events` OBSERVES what happens, and this is the only one that can
|
|
51
|
+
* stop the desk speaking.
|
|
52
|
+
*
|
|
53
|
+
* One rule, and it is the one the tool gates structurally cannot hold: never
|
|
54
|
+
* say help is coming before a unit has been assigned. See `guardrails.ts`,
|
|
55
|
+
* which also carries what the hold costs and why a desk that talks in radio
|
|
56
|
+
* traffic can afford it.
|
|
57
|
+
*/
|
|
58
|
+
outputGuardrails: DISPATCH_GUARDRAILS,
|
|
59
|
+
|
|
45
60
|
// The system prompt instructs the model to use web_search and run_code, so
|
|
46
61
|
// they must be enabled here — the default builtin set does not include them.
|
|
47
62
|
builtinTools: ["think", "remember", "recall", "calculate", "web_search", "run_code"],
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { AgentGuardrail } from "@alexkroman1/aai";
|
|
2
|
+
import { callInHand, dispatchSlot } from "./shared.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The one rule on this desk that the tool gates cannot hold: **never say help
|
|
6
|
+
* is coming before a unit has been assigned.**
|
|
7
|
+
*
|
|
8
|
+
* Everything else here is enforced structurally. Six mutating tools refuse
|
|
9
|
+
* until something has been logged, every one of them is addressed by incident
|
|
10
|
+
* id, and `callFlow`'s `dispatching` state says "never leave a critical
|
|
11
|
+
* incident without at least one" on every turn. None of that reaches the
|
|
12
|
+
* SPEECH: a model that has been told to dispatch and has not can still tell the
|
|
13
|
+
* room that Medic-1 is rolling, and the gates have nothing to say about it,
|
|
14
|
+
* because no tool ran. That is the hole `outputGuardrails` closes — it is the
|
|
15
|
+
* only declaration on an agent that can stop a turn, and `agent({ events })`
|
|
16
|
+
* deliberately cannot (a handler's return value is ignored).
|
|
17
|
+
*
|
|
18
|
+
* **It is pipeline-mode only, and refused at config time in the other two.**
|
|
19
|
+
* S2S synthesizes provider-side, so the words reach this runtime after the
|
|
20
|
+
* caller has heard them; text mode hands its caller the model stream directly
|
|
21
|
+
* and owns no point at which to hold a reply. `agent-guardrails.ts` argues
|
|
22
|
+
* both.
|
|
23
|
+
*
|
|
24
|
+
* ## What it costs, and why this desk can pay it
|
|
25
|
+
*
|
|
26
|
+
* A reply that has to be judged whole cannot be spoken as it arrives, so
|
|
27
|
+
* declaring one trades time-to-first-word for the check — the whole reply is
|
|
28
|
+
* held until the model finishes. That is a real regression on a live call and
|
|
29
|
+
* it is not free here either. What makes it bearable is the shape of this
|
|
30
|
+
* agent's speech: `system-prompt.md` asks for radio traffic ("Medic-1, respond
|
|
31
|
+
* priority one to 400 Oak Street"), so a reply is one or two short lines rather
|
|
32
|
+
* than a paragraph, and the dead-air cover is exempt from the hold and fills
|
|
33
|
+
* the gap exactly as it does during a tool chain.
|
|
34
|
+
*
|
|
35
|
+
* A desk that spoke at length would be the wrong place for this. The test is
|
|
36
|
+
* whether the sentence being checked is worth waiting for the whole of, and on
|
|
37
|
+
* a dispatch floor it is.
|
|
38
|
+
*/
|
|
39
|
+
export const DISPATCH_GUARDRAILS: readonly AgentGuardrail[] = [
|
|
40
|
+
(text, ctx) => {
|
|
41
|
+
// The STATE half, and it is what keeps this from being a keyword filter: an
|
|
42
|
+
// `AgentSessionContext` carries `slots`, so the check can ask the board
|
|
43
|
+
// whether the claim is true rather than whether the sentence looks bad.
|
|
44
|
+
// `callInHand` is the same reading of "which incident" that `events.ts`
|
|
45
|
+
// uses for a dropped call — the most recently touched open one.
|
|
46
|
+
const incident = callInHand(dispatchSlot.get(ctx));
|
|
47
|
+
if (incident !== undefined && incident.assignedResources.length > 0) return true;
|
|
48
|
+
if (!claimsUnitsMoving(text)) return true;
|
|
49
|
+
return (
|
|
50
|
+
"Correction — nothing is rolling on that call yet. Assign a unit with " +
|
|
51
|
+
"resources_dispatch, then say so."
|
|
52
|
+
);
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
/** One sentence at a time, so a hedge in one clause does not excuse the next. */
|
|
57
|
+
const SENTENCES = /[^.!?]+[.!?]?/g;
|
|
58
|
+
|
|
59
|
+
/** The desk asserting that something is already moving. */
|
|
60
|
+
const MOVING = /\b(?:on (?:the|their) way|en ?route|rolling|responding|inbound|underway)\b/i;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Words that make the same sentence a denial, a condition or a plan rather than
|
|
64
|
+
* a claim — "no units are en route yet", "once Medic-1 is rolling", "I'll get
|
|
65
|
+
* someone responding".
|
|
66
|
+
*
|
|
67
|
+
* **The contractions sit OUTSIDE the leading `\b`, and that is not a style
|
|
68
|
+
* choice.** `\bn't\b` can never match: `\b` is a boundary between a word
|
|
69
|
+
* character and a non-word one, and there is none between the `n` and the `'`
|
|
70
|
+
* of "isn't". The alternative was dead for as long as it was written that way,
|
|
71
|
+
* so "Medic-1 isn't en route" read as a CLAIM and the desk was corrected for
|
|
72
|
+
* saying the true thing — the exact inversion of this file's stated bias. Every
|
|
73
|
+
* other alternative begins and ends with a word character, so the shared `\b`
|
|
74
|
+
* pair is right for them; `n't` and `'ll` carry the boundary they actually
|
|
75
|
+
* have, which is the trailing one. The curly apostrophe is accepted too: a model
|
|
76
|
+
* writes "isn’t" often enough, and a guardrail that misses the typographic form
|
|
77
|
+
* is the same bug in a different font.
|
|
78
|
+
*
|
|
79
|
+
* `n['’]t` also covers "won't", "can't" and "doesn't", none of which any other
|
|
80
|
+
* alternative here reaches.
|
|
81
|
+
*/
|
|
82
|
+
const NOT_A_CLAIM =
|
|
83
|
+
/(?:\b(?:no|not|none|nothing|never|nobody|yet|once|when|if|until|before|after|will|going to|need|should|about to)|n['’]t|['’]ll)\b/i;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Does this reply tell the room that units are already moving?
|
|
87
|
+
*
|
|
88
|
+
* A keyword heuristic, and the bias is deliberate: `NOT_A_CLAIM` is broad, so
|
|
89
|
+
* the check MISSES a claim far more often than it blocks an innocent sentence.
|
|
90
|
+
* That is the right way round for a guardrail whose verdict is spoken over the
|
|
91
|
+
* top of a dispatcher — a missed claim leaves the desk where it was, and a
|
|
92
|
+
* false block interrupts a shift with a correction that is not true.
|
|
93
|
+
*
|
|
94
|
+
* The state gate above is what makes the false-positive rate tolerable at all:
|
|
95
|
+
* this function is only ever consulted on an incident with nothing assigned to
|
|
96
|
+
* it, which on a working shift is a minority of turns.
|
|
97
|
+
*
|
|
98
|
+
* Exported because it is the half worth testing directly — the guardrail around
|
|
99
|
+
* it is two branches over the board.
|
|
100
|
+
*/
|
|
101
|
+
export function claimsUnitsMoving(text: string): boolean {
|
|
102
|
+
return (text.match(SENTENCES) ?? []).some(
|
|
103
|
+
(sentence) => MOVING.test(sentence) && !NOT_A_CLAIM.test(sentence),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
@@ -32,6 +32,7 @@ Operational rules:
|
|
|
32
32
|
- Location is always the first priority in any emergency call
|
|
33
33
|
- Critical incidents get immediate dispatch, triage can happen simultaneously
|
|
34
34
|
- Never leave a critical incident without at least one resource dispatched
|
|
35
|
+
- Never say units are moving until resources_dispatch has actually assigned one. The desk checks this before it speaks: a reply that claims help is rolling on an incident with nothing on it is replaced by a correction the room hears instead
|
|
35
36
|
- Monitor resource utilization. If it exceeds 65 percent, warn about degraded capacity
|
|
36
37
|
- At red alert level, recommend mutual aid from neighboring jurisdictions
|
|
37
38
|
- Track time on all incidents. Escalate if critical incidents have no on-scene resources within 8 minutes
|