glove-foundry 0.10.0 → 0.11.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/{chunk-J6NHL4SM.js → chunk-2JCISE2U.js} +103 -4
- package/dist/{chunk-T6YLIGER.js → chunk-4WBQ7XVF.js} +64 -4
- package/dist/{chunk-X3HZYVZS.js → chunk-LCDEHWRB.js} +1 -1
- package/dist/{chunk-CYOIHSUX.js → chunk-NWFOY3L7.js} +5 -0
- package/dist/{chunk-PMNRLJIQ.js → chunk-YCW66WKQ.js} +59 -6
- package/dist/cli.js +4 -4
- package/dist/{client-Cj2wDXuc.d.ts → client-B4Emyeek.d.ts} +10 -1
- package/dist/client.d.ts +2 -2
- package/dist/{event-delivery-a9B1Qq_u.d.ts → event-delivery-BfSL9sGo.d.ts} +148 -3
- package/dist/execution-agent.js +1 -1
- package/dist/execution-daemon.js +1 -1
- package/dist/execution-loader.js +108 -8
- package/dist/index.d.ts +9 -5
- package/dist/index.js +44 -5
- package/dist/voice.d.ts +81 -3
- package/dist/voice.js +322 -21
- package/docs/architecture.md +9 -1
- package/docs/building-with-foundry.md +1 -0
- package/package.json +4 -4
package/dist/voice.js
CHANGED
|
@@ -1,12 +1,273 @@
|
|
|
1
1
|
import {
|
|
2
|
+
eventReceiptStore,
|
|
3
|
+
eventTranscriptMessageId,
|
|
4
|
+
lateChatEffectId,
|
|
2
5
|
renderDeliveries,
|
|
3
6
|
renderMeshMessages
|
|
4
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-2JCISE2U.js";
|
|
5
8
|
|
|
6
9
|
// src/voice.ts
|
|
10
|
+
import { randomUUID } from "node:crypto";
|
|
7
11
|
import { createServer } from "node:http";
|
|
8
12
|
import { RealtimeAgent } from "glove-voice-s2s";
|
|
9
13
|
import { WebSocketServer } from "ws";
|
|
14
|
+
|
|
15
|
+
// src/voice-events.ts
|
|
16
|
+
var RESPONSE_WAIT_MS = 3e3;
|
|
17
|
+
function defaultEventTranscript(event, said) {
|
|
18
|
+
return `[event from ${event.source}] ${said}`;
|
|
19
|
+
}
|
|
20
|
+
function eventText(event) {
|
|
21
|
+
if (typeof event.content === "string") return event.content;
|
|
22
|
+
return event.content.flatMap((part) => part.type === "text" && part.text ? [part.text] : []).join("\n");
|
|
23
|
+
}
|
|
24
|
+
function message(cause) {
|
|
25
|
+
return cause instanceof Error ? cause.message : String(cause);
|
|
26
|
+
}
|
|
27
|
+
var VoiceEventDelivery = class {
|
|
28
|
+
constructor(options) {
|
|
29
|
+
this.options = options;
|
|
30
|
+
this.receipts = eventReceiptStore(options.context.data);
|
|
31
|
+
}
|
|
32
|
+
receipts;
|
|
33
|
+
queue = [];
|
|
34
|
+
pumping;
|
|
35
|
+
retry;
|
|
36
|
+
awaitingResponse;
|
|
37
|
+
ended = false;
|
|
38
|
+
/** Events told on this call. */
|
|
39
|
+
told = 0;
|
|
40
|
+
get generation() {
|
|
41
|
+
return `${this.options.context.runId}/${this.options.callId}`;
|
|
42
|
+
}
|
|
43
|
+
contextFor(handled) {
|
|
44
|
+
const { context, callId } = this.options;
|
|
45
|
+
return {
|
|
46
|
+
runId: context.runId,
|
|
47
|
+
callId,
|
|
48
|
+
agentId: context.agentId,
|
|
49
|
+
conversationId: context.conversationId,
|
|
50
|
+
workspaceId: context.workspaceId,
|
|
51
|
+
eventRecordId: handled.id,
|
|
52
|
+
signal: context.signal
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
emit(type, data) {
|
|
56
|
+
this.options.context.emit({ type: `foundry.voice.event.${type}`, data });
|
|
57
|
+
}
|
|
58
|
+
async put(receipt, patch) {
|
|
59
|
+
const next = { ...receipt, ...patch, updatedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
60
|
+
await this.receipts.put(next);
|
|
61
|
+
return next;
|
|
62
|
+
}
|
|
63
|
+
/** The `onEvent` handler: decide from the event's receipt what, if anything, is left to do. */
|
|
64
|
+
async handle(handled) {
|
|
65
|
+
let receipt = await this.receipts.get(handled.id);
|
|
66
|
+
if (!receipt) {
|
|
67
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
68
|
+
const fresh = {
|
|
69
|
+
id: handled.id,
|
|
70
|
+
eventId: handled.event.id,
|
|
71
|
+
agentId: this.options.context.agentId,
|
|
72
|
+
conversationId: this.options.context.conversationId,
|
|
73
|
+
workspaceId: this.options.context.workspaceId,
|
|
74
|
+
status: "queued",
|
|
75
|
+
generation: this.generation,
|
|
76
|
+
runId: this.options.context.runId,
|
|
77
|
+
createdAt: now,
|
|
78
|
+
updatedAt: now
|
|
79
|
+
};
|
|
80
|
+
receipt = await this.receipts.claim(fresh) ? fresh : await this.receipts.get(handled.id);
|
|
81
|
+
if (!receipt) return;
|
|
82
|
+
}
|
|
83
|
+
switch (receipt.status) {
|
|
84
|
+
case "accepted":
|
|
85
|
+
await this.repairTranscript(handled, receipt);
|
|
86
|
+
handled.ack();
|
|
87
|
+
return;
|
|
88
|
+
case "suppressed":
|
|
89
|
+
case "redirected":
|
|
90
|
+
handled.ack();
|
|
91
|
+
return;
|
|
92
|
+
case "redirecting":
|
|
93
|
+
await this.redirect(handled, receipt);
|
|
94
|
+
return;
|
|
95
|
+
case "dispatching":
|
|
96
|
+
if (receipt.generation === this.generation) return;
|
|
97
|
+
await this.uncertain(handled, receipt);
|
|
98
|
+
return;
|
|
99
|
+
case "queued":
|
|
100
|
+
if (!this.options.live || this.ended) {
|
|
101
|
+
await this.offCall(handled, receipt);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.queue.push({ handled, receipt, attempts: 0, notBefore: 0 });
|
|
105
|
+
this.emit("queued", { id: handled.id });
|
|
106
|
+
this.pump();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/** No call to tell it on: hand it to the late-chat policy, or keep it pending. */
|
|
110
|
+
async offCall(handled, receipt) {
|
|
111
|
+
if (this.options.events.afterCall) await this.redirect(handled, receipt);
|
|
112
|
+
else this.emit("retained", { id: handled.id, status: receipt.status });
|
|
113
|
+
}
|
|
114
|
+
async redirect(handled, receipt) {
|
|
115
|
+
const afterCall = this.options.events.afterCall;
|
|
116
|
+
if (!afterCall) {
|
|
117
|
+
this.emit("retained", { id: handled.id, status: receipt.status });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const effectId = receipt.effectId ?? lateChatEffectId(receipt.id);
|
|
121
|
+
if (receipt.status !== "redirecting") receipt = await this.put(receipt, { status: "redirecting", effectId, generation: this.generation, runId: this.options.context.runId });
|
|
122
|
+
try {
|
|
123
|
+
await afterCall(handled.event, { ...this.contextFor(handled), effectId });
|
|
124
|
+
} catch (cause) {
|
|
125
|
+
this.emit("redirect.failed", { id: handled.id, effectId, message: message(cause) });
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
await this.put(receipt, { status: "redirected", settledAt: (/* @__PURE__ */ new Date()).toISOString() });
|
|
129
|
+
handled.ack();
|
|
130
|
+
this.emit("redirected", { id: handled.id, effectId });
|
|
131
|
+
}
|
|
132
|
+
async uncertain(handled, receipt) {
|
|
133
|
+
let resolution;
|
|
134
|
+
try {
|
|
135
|
+
resolution = await this.options.events.resolveUncertain?.(receipt, this.contextFor(handled));
|
|
136
|
+
} catch (cause) {
|
|
137
|
+
this.emit("uncertain.failed", { id: handled.id, message: message(cause) });
|
|
138
|
+
}
|
|
139
|
+
if (resolution === "accepted") {
|
|
140
|
+
const accepted = await this.put(receipt, { status: "accepted", acceptedAt: (/* @__PURE__ */ new Date()).toISOString() });
|
|
141
|
+
await this.repairTranscript(handled, accepted);
|
|
142
|
+
handled.ack();
|
|
143
|
+
} else if (resolution === "suppressed") {
|
|
144
|
+
await this.put(receipt, { status: "suppressed", reason: "resolved after an uncertain dispatch", settledAt: (/* @__PURE__ */ new Date()).toISOString() });
|
|
145
|
+
handled.ack();
|
|
146
|
+
} else {
|
|
147
|
+
this.emit("uncertain", { id: handled.id, generation: receipt.generation });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** The provider took it but the transcript line may be missing: write it once, saying nothing. */
|
|
151
|
+
async repairTranscript(handled, receipt) {
|
|
152
|
+
if (!this.options.keepTranscript || receipt.transcriptMessageId || receipt.text === void 0) return;
|
|
153
|
+
await this.writeTranscript(handled, receipt, receipt.text);
|
|
154
|
+
this.emit("transcript.repaired", { id: handled.id });
|
|
155
|
+
}
|
|
156
|
+
async writeTranscript(handled, receipt, said) {
|
|
157
|
+
if (!this.options.keepTranscript) return;
|
|
158
|
+
const store = this.options.agent.store;
|
|
159
|
+
const id = eventTranscriptMessageId(receipt.id);
|
|
160
|
+
const existing = await store.getMessages();
|
|
161
|
+
if (!existing.some((item) => item.id === id)) {
|
|
162
|
+
const line = {
|
|
163
|
+
id,
|
|
164
|
+
sender: "user",
|
|
165
|
+
framework_context: "event",
|
|
166
|
+
text: (this.options.events.transcript ?? defaultEventTranscript)(handled.event, said)
|
|
167
|
+
};
|
|
168
|
+
await store.appendMessages([line]);
|
|
169
|
+
}
|
|
170
|
+
await this.put(receipt, { transcriptMessageId: id });
|
|
171
|
+
}
|
|
172
|
+
/** Speech paused (or something changed): tell what is due, one event at a time. */
|
|
173
|
+
pump() {
|
|
174
|
+
if (this.pumping || this.ended || !this.options.live) return;
|
|
175
|
+
this.pumping = this.drain().catch((cause) => this.emit("failed", { message: message(cause) })).finally(() => {
|
|
176
|
+
this.pumping = void 0;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
quiet() {
|
|
180
|
+
return !this.ended && !this.awaitingResponse && this.options.live.quiet();
|
|
181
|
+
}
|
|
182
|
+
/** The agent started answering a told event: the next may go at the next pause. */
|
|
183
|
+
responding() {
|
|
184
|
+
if (this.awaitingResponse) clearTimeout(this.awaitingResponse);
|
|
185
|
+
this.awaitingResponse = void 0;
|
|
186
|
+
}
|
|
187
|
+
scheduleRetry(at) {
|
|
188
|
+
if (this.retry || this.ended) return;
|
|
189
|
+
this.retry = setTimeout(() => {
|
|
190
|
+
this.retry = void 0;
|
|
191
|
+
this.pump();
|
|
192
|
+
}, Math.max(50, at - Date.now()));
|
|
193
|
+
this.retry.unref?.();
|
|
194
|
+
}
|
|
195
|
+
async drain() {
|
|
196
|
+
const live = this.options.live;
|
|
197
|
+
while (this.queue.length && this.quiet()) {
|
|
198
|
+
const now = Date.now();
|
|
199
|
+
const item = this.queue.find((candidate) => candidate.notBefore <= now);
|
|
200
|
+
if (!item) {
|
|
201
|
+
this.scheduleRetry(Math.min(...this.queue.map((candidate) => candidate.notBefore)));
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const { handled } = item;
|
|
205
|
+
let prepared;
|
|
206
|
+
try {
|
|
207
|
+
const prepare = this.options.events.prepare;
|
|
208
|
+
prepared = prepare ? await prepare(handled.event, this.contextFor(handled)) : { say: eventText(handled.event) };
|
|
209
|
+
} catch (cause) {
|
|
210
|
+
item.attempts++;
|
|
211
|
+
item.notBefore = Date.now() + Math.min(1e3 * 2 ** (item.attempts - 1), 6e4);
|
|
212
|
+
this.emit("prepare.failed", { id: handled.id, attempts: item.attempts, message: message(cause) });
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if ("suppress" in prepared) {
|
|
216
|
+
this.queue.splice(this.queue.indexOf(item), 1);
|
|
217
|
+
item.receipt = await this.put(item.receipt, { status: "suppressed", reason: prepared.suppress, settledAt: (/* @__PURE__ */ new Date()).toISOString() });
|
|
218
|
+
handled.ack();
|
|
219
|
+
this.emit("suppressed", { id: handled.id, reason: prepared.suppress });
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (!this.quiet()) return;
|
|
223
|
+
const text = prepared.say;
|
|
224
|
+
item.receipt = await this.put(item.receipt, {
|
|
225
|
+
status: "dispatching",
|
|
226
|
+
generation: this.generation,
|
|
227
|
+
runId: this.options.context.runId,
|
|
228
|
+
text,
|
|
229
|
+
dispatchedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
230
|
+
});
|
|
231
|
+
if (!this.quiet()) {
|
|
232
|
+
item.receipt = await this.put(item.receipt, { status: "queued" });
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
this.queue.splice(this.queue.indexOf(item), 1);
|
|
236
|
+
try {
|
|
237
|
+
live.inject(text, this.options.events.respond !== false);
|
|
238
|
+
} catch (cause) {
|
|
239
|
+
this.emit("uncertain", { id: handled.id, generation: this.generation, message: message(cause) });
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
this.awaitingResponse = setTimeout(() => {
|
|
243
|
+
this.awaitingResponse = void 0;
|
|
244
|
+
this.pump();
|
|
245
|
+
}, RESPONSE_WAIT_MS);
|
|
246
|
+
this.awaitingResponse.unref?.();
|
|
247
|
+
item.receipt = await this.put(item.receipt, { status: "accepted", acceptedAt: (/* @__PURE__ */ new Date()).toISOString() });
|
|
248
|
+
this.emit("accepted", { id: handled.id, heard: "unknown" });
|
|
249
|
+
await this.writeTranscript(handled, item.receipt, text);
|
|
250
|
+
this.told++;
|
|
251
|
+
handled.ack();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* The call is over. Waits for a telling in progress; then events never
|
|
256
|
+
* attempted go to the late-chat policy, or stay pending with a queued
|
|
257
|
+
* receipt as proof that nothing was said.
|
|
258
|
+
*/
|
|
259
|
+
async finish() {
|
|
260
|
+
this.ended = true;
|
|
261
|
+
if (this.retry) clearTimeout(this.retry);
|
|
262
|
+
if (this.awaitingResponse) clearTimeout(this.awaitingResponse);
|
|
263
|
+
this.retry = void 0;
|
|
264
|
+
this.awaitingResponse = void 0;
|
|
265
|
+
await this.pumping;
|
|
266
|
+
for (const item of this.queue.splice(0)) await this.offCall(item.handled, item.receipt);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// src/voice.ts
|
|
10
271
|
var VOICE_EXCLUDED_FOUNDRY_TOOLS = Object.freeze([
|
|
11
272
|
"glove_foundry_spawn",
|
|
12
273
|
"glove_foundry_sleep",
|
|
@@ -39,24 +300,24 @@ function announceDelegationUpdate(update) {
|
|
|
39
300
|
"Let the caller know, briefly."
|
|
40
301
|
].join("\n");
|
|
41
302
|
}
|
|
42
|
-
function announceMeshMessage(
|
|
43
|
-
const who = `${
|
|
44
|
-
if (
|
|
303
|
+
function announceMeshMessage(message2) {
|
|
304
|
+
const who = `${message2.from.name} (${message2.from.agentId})`;
|
|
305
|
+
if (message2.kind === "reply") {
|
|
45
306
|
return [
|
|
46
|
-
`${who} replied to what you sent them (thread ${
|
|
47
|
-
|
|
307
|
+
`${who} replied to what you sent them (thread ${message2.threadId}):`,
|
|
308
|
+
message2.content,
|
|
48
309
|
"Tell the caller what matters, briefly and in plain speech."
|
|
49
310
|
].join("\n");
|
|
50
311
|
}
|
|
51
|
-
if (
|
|
312
|
+
if (message2.kind === "failed") {
|
|
52
313
|
return [
|
|
53
|
-
`${who} could not finish (thread ${
|
|
314
|
+
`${who} could not finish (thread ${message2.threadId}): ${message2.content}`,
|
|
54
315
|
"Let the caller know, briefly."
|
|
55
316
|
].join("\n");
|
|
56
317
|
}
|
|
57
318
|
return [
|
|
58
|
-
`${who} asks (thread ${
|
|
59
|
-
`If you already know the answer, reply with glove_mesh_send_message to "${
|
|
319
|
+
`${who} asks (thread ${message2.threadId}): ${message2.content}`,
|
|
320
|
+
`If you already know the answer, reply with glove_mesh_send_message to "${message2.from.agentId}" with in_reply_to "${message2.id}".`,
|
|
60
321
|
"Otherwise ask the caller, then reply with their answer."
|
|
61
322
|
].join("\n");
|
|
62
323
|
}
|
|
@@ -70,6 +331,15 @@ function voiceCall(options) {
|
|
|
70
331
|
await agent.store.appendMessages([{ sender: "user", text: context.messageText }]);
|
|
71
332
|
return { endedBecause: "not a call", durationMs: 0, turns: 0, updatesSpoken: 0 };
|
|
72
333
|
}
|
|
334
|
+
const events = options.events;
|
|
335
|
+
const callId = randomUUID();
|
|
336
|
+
if (events && context.eventTurn && (context.events ?? []).length > 0 && (context.events ?? []).every((event) => events.accept(senderEvent(event)))) {
|
|
337
|
+
const offCall = new VoiceEventDelivery({ events, agent, context, callId, keepTranscript: options.transcript !== false });
|
|
338
|
+
const stop = context.onEvent((handled) => offCall.handle(handled), events.accept);
|
|
339
|
+
await stop();
|
|
340
|
+
await offCall.finish();
|
|
341
|
+
return { endedBecause: "not a call", durationMs: 0, turns: 0, updatesSpoken: 0, eventsTold: 0 };
|
|
342
|
+
}
|
|
73
343
|
const startedAt = Date.now();
|
|
74
344
|
const excludeTools = [.../* @__PURE__ */ new Set([
|
|
75
345
|
...VOICE_EXCLUDED_FOUNDRY_TOOLS,
|
|
@@ -92,6 +362,19 @@ function voiceCall(options) {
|
|
|
92
362
|
let updatesSpoken = 0;
|
|
93
363
|
let agentSpeaking = false;
|
|
94
364
|
let userSpeaking = false;
|
|
365
|
+
let connected = 0;
|
|
366
|
+
let callEnded = false;
|
|
367
|
+
const eventDelivery = events ? new VoiceEventDelivery({
|
|
368
|
+
events,
|
|
369
|
+
agent,
|
|
370
|
+
context,
|
|
371
|
+
callId,
|
|
372
|
+
keepTranscript,
|
|
373
|
+
live: {
|
|
374
|
+
quiet: () => !callEnded && realtime.isConnected && connected > 0 && !agentSpeaking && !userSpeaking,
|
|
375
|
+
inject: (text, respond) => realtime.inject(text, { respond, role: "system" })
|
|
376
|
+
}
|
|
377
|
+
}) : void 0;
|
|
95
378
|
realtime.on("user_said", (text) => save([{ sender: "user", text }]));
|
|
96
379
|
realtime.on("agent_said", (text) => {
|
|
97
380
|
turns++;
|
|
@@ -105,7 +388,7 @@ function voiceCall(options) {
|
|
|
105
388
|
const announce = options.announce ?? announceDelegationUpdate;
|
|
106
389
|
const announceMesh = options.announceMesh ?? announceMeshMessage;
|
|
107
390
|
const queue = [];
|
|
108
|
-
const meshSide = (
|
|
391
|
+
const meshSide = (message2) => message2.threadId === context.conversationId ? "recipient" : "starter";
|
|
109
392
|
const rendered = (batch) => {
|
|
110
393
|
const updates = batch.flatMap((item) => "update" in item ? [item.update] : []);
|
|
111
394
|
const messages = batch.flatMap((item) => "mesh" in item ? [item.mesh] : []);
|
|
@@ -143,10 +426,12 @@ function voiceCall(options) {
|
|
|
143
426
|
};
|
|
144
427
|
realtime.adapter.on("agent_speech_started", () => {
|
|
145
428
|
agentSpeaking = true;
|
|
429
|
+
eventDelivery?.responding();
|
|
146
430
|
});
|
|
147
431
|
realtime.adapter.on("agent_speech_stopped", () => {
|
|
148
432
|
agentSpeaking = false;
|
|
149
433
|
pump();
|
|
434
|
+
eventDelivery?.pump();
|
|
150
435
|
});
|
|
151
436
|
realtime.adapter.on("user_speech_started", () => {
|
|
152
437
|
userSpeaking = true;
|
|
@@ -154,6 +439,7 @@ function voiceCall(options) {
|
|
|
154
439
|
realtime.adapter.on("user_speech_stopped", () => {
|
|
155
440
|
userSpeaking = false;
|
|
156
441
|
pump();
|
|
442
|
+
eventDelivery?.pump();
|
|
157
443
|
});
|
|
158
444
|
realtime.adapter.on("interrupted", () => {
|
|
159
445
|
agentSpeaking = false;
|
|
@@ -165,8 +451,9 @@ function voiceCall(options) {
|
|
|
165
451
|
};
|
|
166
452
|
const stopUpdates = context.onDelegationUpdate((updates) => hear(updates.map((update) => ({ update }))));
|
|
167
453
|
const stopMesh = context.onMeshMessage((messages) => hear(messages.map((mesh) => ({ mesh }))));
|
|
454
|
+
const stopEvents = eventDelivery ? context.onEvent((handled) => eventDelivery.handle(handled), events.accept) : void 0;
|
|
168
455
|
const stopListening = async () => {
|
|
169
|
-
await Promise.all([stopUpdates(), stopMesh()]);
|
|
456
|
+
await Promise.all([stopUpdates(), stopMesh(), stopEvents?.()]);
|
|
170
457
|
};
|
|
171
458
|
const room = typeof options.room === "function" ? await options.room(context) : options.room;
|
|
172
459
|
const idleMs = options.idleMs ?? 12e4;
|
|
@@ -180,7 +467,6 @@ function voiceCall(options) {
|
|
|
180
467
|
resolveEnded(reason);
|
|
181
468
|
};
|
|
182
469
|
});
|
|
183
|
-
let connected = 0;
|
|
184
470
|
let greeted = false;
|
|
185
471
|
let idleTimer;
|
|
186
472
|
const armIdle = () => {
|
|
@@ -201,6 +487,7 @@ function voiceCall(options) {
|
|
|
201
487
|
realtime.inject(options.greeting, { respond: true });
|
|
202
488
|
}
|
|
203
489
|
pump();
|
|
490
|
+
eventDelivery?.pump();
|
|
204
491
|
},
|
|
205
492
|
left() {
|
|
206
493
|
connected = Math.max(0, connected - 1);
|
|
@@ -229,13 +516,21 @@ function voiceCall(options) {
|
|
|
229
516
|
context.emit({ type: "foundry.voice.call.open", data: { tools: realtime.exposedTools.map((tool) => tool.name) } });
|
|
230
517
|
const endedBecause = await ended;
|
|
231
518
|
context.emit({ type: "foundry.voice.call.ended", data: { endedBecause } });
|
|
232
|
-
return {
|
|
519
|
+
return {
|
|
520
|
+
endedBecause,
|
|
521
|
+
durationMs: Date.now() - startedAt,
|
|
522
|
+
turns,
|
|
523
|
+
updatesSpoken,
|
|
524
|
+
...eventDelivery ? { eventsTold: eventDelivery.told } : {}
|
|
525
|
+
};
|
|
233
526
|
} finally {
|
|
234
527
|
clearTimeout(maxTimer);
|
|
235
528
|
if (idleTimer) clearTimeout(idleTimer);
|
|
236
529
|
if (retry) clearTimeout(retry);
|
|
237
530
|
context.signal.removeEventListener("abort", onAbort);
|
|
531
|
+
callEnded = true;
|
|
238
532
|
await stopListening();
|
|
533
|
+
await eventDelivery?.finish();
|
|
239
534
|
if (queue.length > 0) save([{ sender: "user", text: rendered(queue.splice(0)) }]);
|
|
240
535
|
try {
|
|
241
536
|
await room.close();
|
|
@@ -246,6 +541,11 @@ function voiceCall(options) {
|
|
|
246
541
|
}
|
|
247
542
|
};
|
|
248
543
|
}
|
|
544
|
+
function senderEvent(event) {
|
|
545
|
+
if (!event.metadata || !("foundry_delivery" in event.metadata)) return event;
|
|
546
|
+
const { foundry_delivery: _delivery, ...metadata } = event.metadata;
|
|
547
|
+
return { ...event, ...Object.keys(metadata).length ? { metadata } : { metadata: void 0 } };
|
|
548
|
+
}
|
|
249
549
|
function resample(pcm, from, to) {
|
|
250
550
|
if (from === to) return pcm;
|
|
251
551
|
const output = new Int16Array(Math.floor(pcm.length * to / from));
|
|
@@ -273,8 +573,8 @@ function webSocketRoom(options) {
|
|
|
273
573
|
return {
|
|
274
574
|
async open(call) {
|
|
275
575
|
const { realtime } = call;
|
|
276
|
-
const send = (
|
|
277
|
-
if (caller?.readyState === 1) caller.send(JSON.stringify(
|
|
576
|
+
const send = (message2) => {
|
|
577
|
+
if (caller?.readyState === 1) caller.send(JSON.stringify(message2));
|
|
278
578
|
};
|
|
279
579
|
let turnId = 0;
|
|
280
580
|
let turnOpen = false;
|
|
@@ -343,18 +643,18 @@ function webSocketRoom(options) {
|
|
|
343
643
|
realtime.sendAudio(resample(pcm, sampleRate, realtime.adapter.inputFormat.sampleRate));
|
|
344
644
|
return;
|
|
345
645
|
}
|
|
346
|
-
let
|
|
646
|
+
let message2;
|
|
347
647
|
try {
|
|
348
|
-
|
|
648
|
+
message2 = JSON.parse(data.toString());
|
|
349
649
|
} catch {
|
|
350
650
|
send({ t: "error", message: "Ignored a message that was not JSON." });
|
|
351
651
|
return;
|
|
352
652
|
}
|
|
353
|
-
if (
|
|
354
|
-
const text =
|
|
653
|
+
if (message2.t === "say" && message2.text.trim()) {
|
|
654
|
+
const text = message2.text.trim();
|
|
355
655
|
send({ t: "utterance", text });
|
|
356
656
|
call.say(text);
|
|
357
|
-
} else if (
|
|
657
|
+
} else if (message2.t === "barge_in") {
|
|
358
658
|
realtime.adapter.interrupt();
|
|
359
659
|
}
|
|
360
660
|
});
|
|
@@ -384,6 +684,7 @@ export {
|
|
|
384
684
|
VOICE_EXCLUDED_FOUNDRY_TOOLS,
|
|
385
685
|
announceDelegationUpdate,
|
|
386
686
|
announceMeshMessage,
|
|
687
|
+
defaultEventTranscript,
|
|
387
688
|
voiceCall,
|
|
388
689
|
webSocketRoom
|
|
389
690
|
};
|
package/docs/architecture.md
CHANGED
|
@@ -173,9 +173,17 @@ The controller owns wake/defer deadlines and conversation admission. A live proc
|
|
|
173
173
|
|
|
174
174
|
A successful execution result carries consumed-event receipts. The controller reconciles those receipts before replaying unfinished delivery on startup. A crash can repeat work between a side effect and acknowledgment, so delivery is at least once and effects should use event IDs for idempotency. Archival retains pending events; worker purge removes the owned journal and deduplication records. See [durable subagents](./durable-subagents.md) for authoring, policy, permissions and lifecycle details.
|
|
175
175
|
|
|
176
|
+
## Handled events and delivery receipts
|
|
177
|
+
|
|
178
|
+
By default a run consumes the events it was given: the default turn when it injects them, a custom handler when it returns. `context.onEvent(handler, match?)` opts a run into explicit acknowledgement for addressed events (agent messages and transmissions; delegation, mesh and approval deliveries keep their own paths). Matching events that started the run, and matching events that arrive through the run's mailbox while the handler is registered, go to the handler one at a time. Only `ack()` adds an event to the run's consumed receipts. The run waits for handler calls in flight before it reports them. On close, an unacknowledged event that arrived mid-run is handed back to the runtime and follows its idle policy; one that started the run stays pending and is part of the next run's events, which avoids a wake loop.
|
|
179
|
+
|
|
180
|
+
`prepareEvent` on a definition runs when the run's Glove event queue is taken for a turn. `say` replaces the content the turn sees; `suppress` acknowledges without injecting; a throw keeps the event queued, unacknowledged, and reports it in the result's `retryEventIds`. The controller then delivers it again after an exponential delay (1s to 5 minutes), instead of handing it straight back to wake another run.
|
|
181
|
+
|
|
182
|
+
`voiceCall({ events })` combines both on a live call. Its handler records a delivery receipt per event in the data adapter's receipt namespace (`claimEventReceipt`, `putEventReceipt`, `getEventReceipt`, `listEventReceipts`), which is separate from workspace entries. The receipt is written before each step with an effect: `dispatching` before the provider is asked, `accepted` after it took the text, the transcript message id after the framework-event line is written, `redirecting` with an event-derived `effectId` before a late-chat effect, `redirected` after. A redelivered event is handled from its receipt: `accepted`, `suppressed` and `redirected` are acknowledged without speaking; `redirecting` retries the same effect; `dispatching` from another run or call generation is held, never replayed and never redirected; `queued` waits for a pause or for the late-chat policy. "Accepted" is local transport acceptance, not audible delivery. Receipts are written in the child process, so the data adapter must be constructible there from `foundry.application.ts`.
|
|
183
|
+
|
|
176
184
|
## Schedule occurrences
|
|
177
185
|
|
|
178
|
-
The controller owns every timer: one-shot schedules, sleeps and recurring schedules. Station's scheduler is not used, so no schedule state lives in the execution daemon. An activation persists `nextOccurrenceAt`, the scheduled time of the next occurrence it owes, together with `scheduleRevision`, a hash of the configuration that cursor belongs to (target, message, payload, timing). Status and timestamp writes never change the revision.
|
|
186
|
+
The controller owns every timer: one-shot schedules, sleeps and recurring schedules. Station's scheduler is not used, so no schedule state lives in the execution daemon. An activation persists `nextOccurrenceAt`, the scheduled time of the next occurrence it owes, together with `scheduleRevision`, a hash of the configuration that cursor belongs to (target, message, payload, timing). Status and timestamp writes never change the revision, and neither does the order a store returns object keys in: the hash is taken over JSON with every object's keys sorted, so a PostgreSQL `jsonb` column is fine. A revision stored by 0.10.0, which hashed keys in stored order, still matches its configuration (with the timing keys in any order) and is kept as stored, so the ids of occurrences claimed under it do not change.
|
|
179
187
|
|
|
180
188
|
When the cursor comes due, under the activation lock, the controller:
|
|
181
189
|
|
|
@@ -921,6 +921,7 @@ run cleanup. See the [document adapter](../../glove-env-documents/README.md).
|
|
|
921
921
|
- Transmissions own executable integration logic; playbooks remain serializable policy.
|
|
922
922
|
- Provider adapters own credential acquisition and refresh.
|
|
923
923
|
- Voice rooms own audio transport; `voiceCall` owns the session and the transcript; Foundry owns the durable run.
|
|
924
|
+
- Product events reach a call through `voiceCall({ events })`: told at a pause from freshly prepared facts, as system context, with a durable delivery receipt per event. `call.say` is caller input and never carries notices.
|
|
924
925
|
- A definition is what a kind of agent can be; each agent, its name and its job are instance data.
|
|
925
926
|
- Interactors keep the person's context; workers keep one task's. Delegation results and questions come back to the conversation that delegated.
|
|
926
927
|
- Agents that already exist as peers in one workspace can message each other on the hosted mesh. Replies come back to the conversation that sent the message.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "glove-foundry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "An Effect-native, file-routed framework for typed, observable Glove agent applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -78,14 +78,14 @@
|
|
|
78
78
|
"station-beacon": "^3.0.0",
|
|
79
79
|
"station-broadcast": "^3.0.0",
|
|
80
80
|
"glove-core": "4.8.0",
|
|
81
|
-
"glove-js": "0.5.7",
|
|
82
|
-
"glove-mcp": "1.2.7",
|
|
83
81
|
"glove-facts": "0.1.9",
|
|
82
|
+
"glove-mcp": "1.2.7",
|
|
84
83
|
"glove-lisp": "0.5.7",
|
|
84
|
+
"glove-js": "0.5.7",
|
|
85
|
+
"glove-voice-s2s": "0.5.0",
|
|
85
86
|
"glove-memory": "2.3.4",
|
|
86
87
|
"glove-mesh": "0.2.4",
|
|
87
88
|
"glove-python": "0.4.7",
|
|
88
|
-
"glove-voice-s2s": "0.4.7",
|
|
89
89
|
"glove-working-environment": "0.7.0"
|
|
90
90
|
},
|
|
91
91
|
"devDependencies": {
|