ceulen 0.8.0 → 0.9.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/CHANGELOG.md +35 -0
- package/extensions/lib/registry.ts +8 -1
- package/extensions/modules/a2a/configPanel.ts +435 -0
- package/extensions/modules/a2a/index.ts +1117 -0
- package/extensions/modules/a2a/lib/activity.ts +173 -0
- package/extensions/modules/a2a/lib/client.ts +1061 -0
- package/extensions/modules/a2a/lib/config.ts +920 -0
- package/extensions/modules/a2a/lib/discovery.ts +158 -0
- package/extensions/modules/a2a/lib/gateway.ts +889 -0
- package/extensions/modules/a2a/lib/mdns.ts +233 -0
- package/extensions/modules/a2a/lib/outcome.ts +48 -0
- package/extensions/modules/a2a/lib/persistence.ts +123 -0
- package/extensions/modules/a2a/lib/protocol.ts +506 -0
- package/extensions/modules/a2a/lib/registry.ts +146 -0
- package/extensions/modules/a2a/lib/retry.ts +86 -0
- package/extensions/modules/a2a/lib/security.ts +394 -0
- package/extensions/modules/a2a/lib/server.ts +1436 -0
- package/extensions/modules/a2a/test/activity.test.ts +138 -0
- package/extensions/modules/a2a/test/bridge.test.ts +101 -0
- package/extensions/modules/a2a/test/broadcast-parse.test.ts +31 -0
- package/extensions/modules/a2a/test/chai.ts +101 -0
- package/extensions/modules/a2a/test/client.test.ts +856 -0
- package/extensions/modules/a2a/test/completion-guard.test.ts +73 -0
- package/extensions/modules/a2a/test/config.test.ts +1396 -0
- package/extensions/modules/a2a/test/configPanel.test.ts +393 -0
- package/extensions/modules/a2a/test/discovery.test.ts +144 -0
- package/extensions/modules/a2a/test/extension.test.ts +135 -0
- package/extensions/modules/a2a/test/gateway.test.ts +1449 -0
- package/extensions/modules/a2a/test/helpers.ts +39 -0
- package/extensions/modules/a2a/test/mdns.test.ts +131 -0
- package/extensions/modules/a2a/test/outcome.test.ts +61 -0
- package/extensions/modules/a2a/test/persistence.test.ts +92 -0
- package/extensions/modules/a2a/test/protocol.test.ts +217 -0
- package/extensions/modules/a2a/test/registry.test.ts +114 -0
- package/extensions/modules/a2a/test/retry.test.ts +136 -0
- package/extensions/modules/a2a/test/security.test.ts +281 -0
- package/extensions/modules/a2a/test/server.test.ts +1966 -0
- package/extensions/modules/a2a/test/tmp.ts +59 -0
- package/extensions/modules/a2a/test/vendor.test.ts +57 -0
- package/extensions/modules/a2a/types/bonjour-service.d.ts +22 -0
- package/extensions/modules/a2a/vendor/VENDORED.md +18 -0
- package/extensions/modules/a2a/vendor/package.json +1 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/LICENSE +23 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/index.d.ts +14 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/index.js +57 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/KeyValue.d.ts +4 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/KeyValue.js +3 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/bonjour.d.ts +13 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/bonjour.js +50 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/browser.d.ts +41 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/browser.js +209 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/dns-txt.d.ts +9 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/dns-txt.js +42 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/mdns-server.d.ts +14 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/mdns-server.js +119 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/registry.d.ts +14 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/registry.js +142 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service-types.d.ts +7 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service-types.js +53 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service.d.ts +58 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/service.js +130 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/dns-equal.d.ts +1 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/dns-equal.js +13 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/equal-txt.d.ts +1 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/equal-txt.js +17 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-service.d.ts +4 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-service.js +22 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-txt.d.ts +3 -0
- package/extensions/modules/a2a/vendor/vendored_deps/bonjour-service/dist/lib/utils/filter-txt.js +6 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/classes.js +23 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/index.js +1761 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/opcodes.js +50 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/optioncodes.js +59 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/rcodes.js +50 -0
- package/extensions/modules/a2a/vendor/vendored_deps/dns-packet/types.js +103 -0
- package/extensions/modules/a2a/vendor/vendored_deps/fast-deep-equal/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/fast-deep-equal/es6.js +73 -0
- package/extensions/modules/a2a/vendor/vendored_deps/fast-deep-equal/index.js +46 -0
- package/extensions/modules/a2a/vendor/vendored_deps/ip-codec/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/ip-codec/index.cjs +250 -0
- package/extensions/modules/a2a/vendor/vendored_deps/multicast-dns/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/multicast-dns/index.js +205 -0
- package/extensions/modules/a2a/vendor/vendored_deps/thunky/LICENSE +21 -0
- package/extensions/modules/a2a/vendor/vendored_deps/thunky/index.js +55 -0
- package/extensions/modules/composer/test/module.test.ts +2 -2
- package/extensions/modules/config/index.ts +2 -0
- package/extensions/modules/config/test/renderTools.test.ts +1 -1
- package/extensions/modules/todo/configPanel.ts +55 -0
- package/extensions/modules/todo/index.ts +46 -13
- package/extensions/modules/todo/lib/render.ts +271 -0
- package/extensions/modules/todo/lib/settings.ts +73 -0
- package/extensions/modules/todo/test/todo.test.ts +276 -0
- package/package.json +2 -2
- package/skills/a2a/SKILL.md +63 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mDNS / DNS-SD broadcast + discovery (optional).
|
|
3
|
+
*
|
|
4
|
+
* Dynamic-imports `bonjour-service` (RFC 6762/6763 in pure TS, no native
|
|
5
|
+
* bindings). If the package isn't installed → graceful no-op degrade; the file
|
|
6
|
+
* registry + enriched Agent Card still serve discovery. mDNS is the *network*
|
|
7
|
+
* layer; the file registry is the fast *local* one.
|
|
8
|
+
*
|
|
9
|
+
* Service type: `_a2a._tcp` (interoperable with non-Pi A2A peers on the LAN).
|
|
10
|
+
* TXT record carries url/cwd/model so peers can screen without a card fetch.
|
|
11
|
+
*
|
|
12
|
+
* Security/conflict note: bonjour-service defaults the announced host to
|
|
13
|
+
* `os.hostname()`, publishing A/AAAA records that CLAIM the OS local hostname
|
|
14
|
+
* (e.g. `MBP-Sao.local`). On macOS that fights mDNSResponder's ownership of
|
|
15
|
+
* the name and the OS renames the machine ("local hostname already in use").
|
|
16
|
+
* So we always announce a unique, non-OS name: `<hostname>-a2a-<pid>.local`.
|
|
17
|
+
* The instance name gets a pid suffix too, so multiple Pi sessions on one
|
|
18
|
+
* machine don't probe-collide on the same `<name>._a2a._tcp.local` fqdn.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { hostname } from "node:os";
|
|
22
|
+
import { createRequire } from "node:module";
|
|
23
|
+
|
|
24
|
+
export interface MdnsPeer {
|
|
25
|
+
name: string;
|
|
26
|
+
host: string;
|
|
27
|
+
port: number;
|
|
28
|
+
txt: Record<string, string>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface MdnsHandle {
|
|
32
|
+
/** Stop broadcast + discovery, release the multicast socket. */
|
|
33
|
+
stop(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface BonjourLike {
|
|
37
|
+
publish(opts: any): { start(cb?: (e?: Error) => void): void; stop(cb?: (e?: Error) => void): void };
|
|
38
|
+
find(opts: any, onUp?: (svc: any) => void): { on(ev: "up" | "down", cb: (svc: any) => void): void; stop(): void };
|
|
39
|
+
destroy(): Promise<void> | void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const NOOP_HANDLE: MdnsHandle = { stop: async () => {} };
|
|
43
|
+
|
|
44
|
+
// Type shim lives in ../types/bonjour-service.d.ts (ambient module
|
|
45
|
+
// declaration); the implementation is VENDORED at
|
|
46
|
+
// ../vendor/vendored_deps/bonjour-service (flat layout, no bare requires).
|
|
47
|
+
// ceulen port: dynamic "bonjour-service" import replaced with a lazy
|
|
48
|
+
// createRequire over the vendored dist (web-module pattern — keeps the CJS
|
|
49
|
+
// vendor off the entry import graph). Fail-open (require throws → null →
|
|
50
|
+
// file-registry-only) kept verbatim.
|
|
51
|
+
const requireVendored = createRequire(import.meta.url);
|
|
52
|
+
|
|
53
|
+
/** Resolves to a started bonjour instance, or null if the package is absent. */
|
|
54
|
+
async function loadBonjour(): Promise<BonjourLike | null> {
|
|
55
|
+
try {
|
|
56
|
+
const mod: any = requireVendored("./vendor/vendored_deps/bonjour-service/dist/index.js");
|
|
57
|
+
const Bonjour = mod.Bonjour ?? mod.default?.Bonjour ?? mod.default;
|
|
58
|
+
if (typeof Bonjour !== "function") return null;
|
|
59
|
+
// Pass an errorCallback: bonjour's default rethrows from an async dgram
|
|
60
|
+
// send callback (outside our try/catch) — a transient multicast error
|
|
61
|
+
// (interface flap, ENETUNREACH) would otherwise crash the host process.
|
|
62
|
+
return new Bonjour({}, () => {}) as unknown as BonjourLike;
|
|
63
|
+
} catch {
|
|
64
|
+
return null; // optional dep not installed
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Broadcast this session over mDNS. Returns a handle whose stop() is always
|
|
70
|
+
* safe to call (no-op if the package is absent).
|
|
71
|
+
*/
|
|
72
|
+
export async function startBroadcast(opts: {
|
|
73
|
+
serviceType: string;
|
|
74
|
+
name: string;
|
|
75
|
+
port: number;
|
|
76
|
+
txt: Record<string, string>;
|
|
77
|
+
}): Promise<MdnsHandle> {
|
|
78
|
+
const bonjour = await loadBonjour();
|
|
79
|
+
if (!bonjour) return NOOP_HANDLE;
|
|
80
|
+
try {
|
|
81
|
+
const svc = bonjour.publish({
|
|
82
|
+
type: opts.serviceType,
|
|
83
|
+
name: mdnsInstanceName(opts.name, process.pid),
|
|
84
|
+
host: mdnsInstanceHost(hostname(), process.pid),
|
|
85
|
+
port: opts.port,
|
|
86
|
+
txt: opts.txt,
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
stop: async () => {
|
|
90
|
+
try {
|
|
91
|
+
await new Promise<void>((r) => svc.stop(() => r()));
|
|
92
|
+
} catch {
|
|
93
|
+
/* ignore */
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
await bonjour.destroy();
|
|
97
|
+
} catch {
|
|
98
|
+
/* ignore */
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
} catch {
|
|
103
|
+
try {
|
|
104
|
+
await bonjour.destroy();
|
|
105
|
+
} catch {
|
|
106
|
+
/* ignore */
|
|
107
|
+
}
|
|
108
|
+
return NOOP_HANDLE;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Listen for peers advertising the A2A service type. `onUp` fires per peer.
|
|
114
|
+
* Returns a handle whose stop() is always safe.
|
|
115
|
+
*/
|
|
116
|
+
export async function startDiscovery(opts: {
|
|
117
|
+
serviceType: string;
|
|
118
|
+
onUp: (peer: MdnsPeer) => void;
|
|
119
|
+
/** Fired when a peer disappears (so callers can remove it from their list). */
|
|
120
|
+
onDown?: (peer: { name: string; host: string; port: number }) => void;
|
|
121
|
+
}): Promise<MdnsHandle> {
|
|
122
|
+
const bonjour = await loadBonjour();
|
|
123
|
+
if (!bonjour) return NOOP_HANDLE;
|
|
124
|
+
try {
|
|
125
|
+
const browser = bonjour.find({ type: opts.serviceType });
|
|
126
|
+
browser.on("up", (svc: any) => {
|
|
127
|
+
const peer: MdnsPeer = {
|
|
128
|
+
name: String(svc.name ?? ""),
|
|
129
|
+
host: String(svc.host ?? svc.referer?.address ?? ""),
|
|
130
|
+
port: Number(svc.port ?? 0),
|
|
131
|
+
txt: (svc.txt ?? {}) as Record<string, string>,
|
|
132
|
+
};
|
|
133
|
+
try {
|
|
134
|
+
opts.onUp(peer);
|
|
135
|
+
} catch {
|
|
136
|
+
/* listener best-effort */
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
if (opts.onDown) {
|
|
140
|
+
browser.on("down", (svc: any) => {
|
|
141
|
+
try {
|
|
142
|
+
opts.onDown!({
|
|
143
|
+
name: String(svc.name ?? ""),
|
|
144
|
+
host: String(svc.host ?? svc.referer?.address ?? ""),
|
|
145
|
+
port: Number(svc.port ?? 0),
|
|
146
|
+
});
|
|
147
|
+
} catch {
|
|
148
|
+
/* listener best-effort */
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
stop: async () => {
|
|
154
|
+
try {
|
|
155
|
+
browser.stop();
|
|
156
|
+
} catch {
|
|
157
|
+
/* ignore */
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
await bonjour.destroy();
|
|
161
|
+
} catch {
|
|
162
|
+
/* ignore */
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
} catch {
|
|
167
|
+
try {
|
|
168
|
+
await bonjour.destroy();
|
|
169
|
+
} catch {
|
|
170
|
+
/* ignore */
|
|
171
|
+
}
|
|
172
|
+
return NOOP_HANDLE;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** RFC 6762-safe DNS label: lowercase, [a-z0-9-] only, ≤63 octets. */
|
|
177
|
+
function label(s: string, fallback: string): string {
|
|
178
|
+
const out = String(s ?? "")
|
|
179
|
+
.toLowerCase()
|
|
180
|
+
.replace(/\./g, "-") // dots split labels — flatten
|
|
181
|
+
.replace(/[^a-z0-9-]/g, "-")
|
|
182
|
+
.replace(/-+/g, "-")
|
|
183
|
+
.replace(/^-|-$/g, "")
|
|
184
|
+
.slice(0, 63);
|
|
185
|
+
return out || fallback;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Unique per-session service instance name (`<agent>-<pid>`). Two Pi sessions
|
|
189
|
+
* on one machine (same agentName/hostname) would otherwise probe-collide on
|
|
190
|
+
* the same `<name>._a2a._tcp.local` fqdn — the second never advertises.
|
|
191
|
+
* Whole fqdn label must fit 63 octets: cap the base before adding the suffix
|
|
192
|
+
* (a long `agentName` — user/env-controlled, config.ts) would otherwise emit
|
|
193
|
+
* malformed DNS labels that peers/mDNSResponder silently reject). */
|
|
194
|
+
export function mdnsInstanceName(agentName: string, pid: number): string {
|
|
195
|
+
const suffix = `-${pid}`;
|
|
196
|
+
return `${label(agentName, "pi").slice(0, 63 - suffix.length).replace(/-+$/g, "") || "pi"}${suffix}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The hostname this broadcast CLAIMS in A/AAAA records. Deliberately never
|
|
200
|
+
* equals `os.hostname()` — claiming the OS local hostname makes macOS
|
|
201
|
+
* mDNSResponder rename the machine ("local hostname already in use"). Peers
|
|
202
|
+
* don't need it: discovery resolves the SRV target (unique name → LAN IPs)
|
|
203
|
+
* and a2a_peers uses the TXT `url` anyway. */
|
|
204
|
+
export function mdnsInstanceHost(host: string, pid: number): string {
|
|
205
|
+
// Whole fqdn label must fit 63 octets: cap the base before adding the suffix.
|
|
206
|
+
const suffix = `-a2a-${pid}.local`;
|
|
207
|
+
return `${label(host, "pi").slice(0, 63 - suffix.length).replace(/-+$/g, "") || "pi"}${suffix}`;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Stable dedup key for a discovered peer (URL, else name:host:port composite). */
|
|
211
|
+
export function mdnsPeerKey(p: { name: string; host: string; port: number; txt?: Record<string, string> }): string {
|
|
212
|
+
if (p.txt?.url) return String(p.txt.url).trim().replace(/\/+$/, "").toLowerCase();
|
|
213
|
+
return `${p.name}:${p.host}:${p.port}`;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Build the TXT record from session essentials. Each value is capped at 255
|
|
217
|
+
* bytes: RFC 1035 character-strings are max 255 bytes, and dns-packet writes
|
|
218
|
+
* the length into a single byte — a longer value (deep cwd, long publicUrl)
|
|
219
|
+
* would silently corrupt the whole TXT record peers receive. */
|
|
220
|
+
export function txtRecord(opts: {
|
|
221
|
+
url: string;
|
|
222
|
+
cwd: string;
|
|
223
|
+
model: string;
|
|
224
|
+
}): Record<string, string> {
|
|
225
|
+
return { url: txtValue(opts.url), cwd: txtValue(opts.cwd), model: txtValue(opts.model) };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Truncate to ≤255 UTF-8 bytes on a char boundary (values are tiny — O(n) fine). */
|
|
229
|
+
function txtValue(v: string): string {
|
|
230
|
+
let s = String(v ?? "");
|
|
231
|
+
while (Buffer.byteLength(s, "utf-8") > 255) s = s.slice(0, -1);
|
|
232
|
+
return s;
|
|
233
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal-outcome check for a dispatched child session (fleet tasks #314,
|
|
3
|
+
* #425). A child's run "finishes" whenever the agent loop ends, but that
|
|
4
|
+
* includes endings with no usable answer. Returning normally from those hands
|
|
5
|
+
* the dispatcher a COMPLETED task with a stale or empty "(no reply)" — the
|
|
6
|
+
* false-success trap. Each case here returns an error message (the runner
|
|
7
|
+
* throws it so the task maps to FAILED), or null when the run really produced
|
|
8
|
+
* an answer.
|
|
9
|
+
*/
|
|
10
|
+
export interface TerminalState {
|
|
11
|
+
/** stopReason of the LAST assistant message, if any arrived. */
|
|
12
|
+
stopReason?: string;
|
|
13
|
+
/** Whether that last assistant message carried any text. */
|
|
14
|
+
hadText: boolean;
|
|
15
|
+
/** Whether any assistant message arrived at all. */
|
|
16
|
+
sawAssistant: boolean;
|
|
17
|
+
/** errorMessage of the last assistant message (provider/transport error). */
|
|
18
|
+
errorMessage?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function terminalOutcomeError(s: TerminalState): string | null {
|
|
22
|
+
if (!s.sawAssistant) {
|
|
23
|
+
return "run ended without any assistant output — the model produced nothing (provider unreachable or request rejected before a response?)";
|
|
24
|
+
}
|
|
25
|
+
if (s.stopReason === "error") {
|
|
26
|
+
// The provider/transport failed the final turn (e.g. HTTP 503 "no available
|
|
27
|
+
// channel"). Any text in `reply` belongs to an EARLIER turn, so it is stale.
|
|
28
|
+
const detail = (s.errorMessage ?? "").trim().slice(0, 500);
|
|
29
|
+
return `model call failed on the final turn${detail ? `: ${detail}` : ""} — no usable reply was produced`;
|
|
30
|
+
}
|
|
31
|
+
if (s.stopReason === "length" && !s.hadText) {
|
|
32
|
+
return "run ended on a length stop with no assistant text — no usable reply was produced (output capped before any content; context-clamped max_tokens?)";
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Detect + strip a trailing [INPUT_REQUIRED] marker from a final assistant
|
|
39
|
+
* reply. Only a marker at the trimmed TAIL counts — a peer merely QUOTING the
|
|
40
|
+
* marker mid-text must neither flip the state nor mangle the quoted text, and
|
|
41
|
+
* only that one trailing occurrence is removed.
|
|
42
|
+
*/
|
|
43
|
+
export function stripInputRequired(reply: string): { text: string; inputRequired: boolean } {
|
|
44
|
+
const m = /\[INPUT_REQUIRED\]\s*$/i.exec(reply);
|
|
45
|
+
return m
|
|
46
|
+
? { text: reply.slice(0, m.index).trim(), inputRequired: true }
|
|
47
|
+
: { text: reply, inputRequired: false };
|
|
48
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A2A conversation persistence — every outbound exchange is written to
|
|
3
|
+
* <piDir>/a2a_conversations/<context>.jsonl so multi-turn conversations
|
|
4
|
+
* survive compaction/restarts (recallable via a2a_history).
|
|
5
|
+
*
|
|
6
|
+
* Pattern: append-only JSONL, one record per message. No schema migrations.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
|
|
12
|
+
interface PersistedMessage {
|
|
13
|
+
ts: string;
|
|
14
|
+
role: "user" | "agent";
|
|
15
|
+
text: string;
|
|
16
|
+
taskId?: string;
|
|
17
|
+
peer?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function dir(piDir: string): string {
|
|
21
|
+
return join(piDir, "a2a_conversations");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function fileFor(piDir: string, contextId: string): string {
|
|
25
|
+
// Sanitise: only allow context- prefix + hex/alphanum.
|
|
26
|
+
const safe = contextId.replace(/[^A-Za-z0-9_-]/g, "");
|
|
27
|
+
return join(dir(piDir), `${safe || "ctx-unknown"}.jsonl`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function persistMessage(opts: {
|
|
31
|
+
piDir: string;
|
|
32
|
+
contextId: string;
|
|
33
|
+
role: "user" | "agent";
|
|
34
|
+
text: string;
|
|
35
|
+
taskId?: string;
|
|
36
|
+
peer?: string;
|
|
37
|
+
}): void {
|
|
38
|
+
try {
|
|
39
|
+
const rec: PersistedMessage = {
|
|
40
|
+
ts: new Date().toISOString(),
|
|
41
|
+
role: opts.role,
|
|
42
|
+
text: opts.text,
|
|
43
|
+
taskId: opts.taskId,
|
|
44
|
+
peer: opts.peer,
|
|
45
|
+
};
|
|
46
|
+
const p = fileFor(opts.piDir, opts.contextId);
|
|
47
|
+
mkdirSync(dir(opts.piDir), { recursive: true });
|
|
48
|
+
appendFileSync(p, JSON.stringify(rec) + "\n", { encoding: "utf-8" });
|
|
49
|
+
} catch {
|
|
50
|
+
/* best-effort persistence */
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function loadConversation(piDir: string, contextId: string, limit = 50): PersistedMessage[] {
|
|
55
|
+
const p = fileFor(piDir, contextId);
|
|
56
|
+
if (!existsSync(p)) return [];
|
|
57
|
+
try {
|
|
58
|
+
const lines = readFileSync(p, "utf-8").split("\n").filter(Boolean);
|
|
59
|
+
const recs = lines
|
|
60
|
+
.map((l) => {
|
|
61
|
+
try {
|
|
62
|
+
return JSON.parse(l) as PersistedMessage;
|
|
63
|
+
} catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
.filter((x): x is PersistedMessage => x !== null);
|
|
68
|
+
return recs.slice(-limit);
|
|
69
|
+
} catch {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function listConversations(piDir: string): string[] {
|
|
75
|
+
const d = dir(piDir);
|
|
76
|
+
if (!existsSync(d)) return [];
|
|
77
|
+
try {
|
|
78
|
+
return readdirSync(d)
|
|
79
|
+
.filter((f) => f.endsWith(".jsonl"))
|
|
80
|
+
.map((f) => f.replace(/\.jsonl$/, ""))
|
|
81
|
+
.sort();
|
|
82
|
+
} catch {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
// Child-session transcripts (fleet task #252)
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
/** Directory holding dispatched child-session transcripts, one JSONL pi
|
|
92
|
+
* session per inbound task, named <timestamp>_<taskId>.jsonl. */
|
|
93
|
+
export function childTranscriptDir(piDir: string): string {
|
|
94
|
+
return join(piDir, "a2a_sessions");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Delete child transcripts older than `retentionDays` (by mtime). 0 or
|
|
98
|
+
* negative = keep forever. Returns the number of files removed. Never
|
|
99
|
+
* throws — retention is housekeeping, not a dependency. */
|
|
100
|
+
export function sweepChildTranscripts(dir: string, retentionDays: number): number {
|
|
101
|
+
if (!(retentionDays > 0)) return 0;
|
|
102
|
+
let removed = 0;
|
|
103
|
+
let files: string[];
|
|
104
|
+
try {
|
|
105
|
+
files = readdirSync(dir);
|
|
106
|
+
} catch {
|
|
107
|
+
return 0; // no dir yet (or unreadable) — nothing to sweep
|
|
108
|
+
}
|
|
109
|
+
const cutoff = Date.now() - retentionDays * 86_400_000;
|
|
110
|
+
for (const f of files) {
|
|
111
|
+
if (!f.endsWith(".jsonl")) continue;
|
|
112
|
+
const p = join(dir, f);
|
|
113
|
+
try {
|
|
114
|
+
if (statSync(p).mtimeMs < cutoff) {
|
|
115
|
+
rmSync(p);
|
|
116
|
+
removed += 1;
|
|
117
|
+
}
|
|
118
|
+
} catch {
|
|
119
|
+
/* racing deletion or unreadable file — skip */
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return removed;
|
|
123
|
+
}
|