@howaboua/pi-durable-gippity-voice 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/README.md +2 -2
- package/dist/activation.js +369 -148
- package/dist/index.js +369 -148
- package/dist/packages/execution/src/tool-contract.d.ts +2 -2
- package/dist/packages/gippity-voice/src/agent-observation.d.ts +13 -0
- package/dist/packages/gippity-voice/src/host.d.ts +6 -0
- package/dist/packages/gippity-voice/src/index.d.ts +2 -1
- package/dist/packages/gippity-voice/src/voice/conversation/peer.d.ts +4 -0
- package/dist/packages/gippity-voice/src/voice/conversation/public-turns.d.ts +2 -1
- package/dist/packages/gippity-voice/src/voice/conversation/session.d.ts +1 -0
- package/dist/packages/gippity-voice/src/voice/helper-protocol.d.ts +35 -0
- package/dist/packages/gippity-voice/src/voice/lan/browser-clients.d.ts +1 -1
- package/dist/packages/gippity-voice/src/voice/lan/browser-connections.d.ts +17 -0
- package/dist/packages/gippity-voice/src/voice/lan/browser-session.d.ts +4 -2
- package/dist/packages/gippity-voice/src/voice/lan/server.d.ts +4 -2
- package/dist/packages/gippity-voice/src/voice/realtime-auth.d.ts +3 -2
- package/dist/packages/gippity-voice/src/voice/turns.d.ts +2 -1
- package/dist/voice/bin/darwin-arm64/pi-codex-voice +0 -0
- package/dist/voice/bin/darwin-x64/pi-codex-voice +0 -0
- package/dist/voice/bin/linux-arm64/pi-codex-voice +0 -0
- package/dist/voice/bin/linux-x64/pi-codex-voice +0 -0
- package/dist/voice/bin/win32-arm64/pi-codex-voice.exe +0 -0
- package/dist/voice/bin/win32-x64/pi-codex-voice.exe +0 -0
- package/package.json +6 -5
- package/src/voice/rust/src/audio.rs +2 -2
- package/src/voice/rust/src/playout.rs +68 -8
- package/src/voice/rust/src/protocol.rs +43 -1
- package/src/voice/rust/src/v3_media.rs +138 -31
package/dist/activation.js
CHANGED
|
@@ -43,10 +43,11 @@ import {
|
|
|
43
43
|
withAbortSignal
|
|
44
44
|
} from "@earendil-works/chord/context";
|
|
45
45
|
import {
|
|
46
|
+
AgentDoc,
|
|
47
|
+
CompactionEntry,
|
|
46
48
|
defineExtension,
|
|
47
49
|
defineTask as defineTask2,
|
|
48
|
-
UserEntry
|
|
49
|
-
watchEvents
|
|
50
|
+
UserEntry
|
|
50
51
|
} from "@earendil-works/pi-durable";
|
|
51
52
|
|
|
52
53
|
// internal/settings/index.ts
|
|
@@ -156,7 +157,7 @@ function createSettingsContribution(options) {
|
|
|
156
157
|
import {
|
|
157
158
|
defineTask
|
|
158
159
|
} from "@earendil-works/pi-durable";
|
|
159
|
-
import {
|
|
160
|
+
import { compile as compile2 } from "@earendil-works/pi-schema";
|
|
160
161
|
|
|
161
162
|
// packages/execution/src/forwarding.ts
|
|
162
163
|
import { copyJson as copyJson2 } from "@earendil-works/chord";
|
|
@@ -1010,7 +1011,7 @@ import { validateToolArguments } from "@earendil-works/pi-ai/utils/validation";
|
|
|
1010
1011
|
import {
|
|
1011
1012
|
ToolTask as ToolTask3
|
|
1012
1013
|
} from "@earendil-works/pi-durable";
|
|
1013
|
-
import {
|
|
1014
|
+
import { compile } from "@earendil-works/pi-schema";
|
|
1014
1015
|
|
|
1015
1016
|
// packages/execution/src/tool-contract.ts
|
|
1016
1017
|
import { copyJson as copyJson5 } from "@earendil-works/chord";
|
|
@@ -1099,7 +1100,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1099
1100
|
if (typeof input !== "string")
|
|
1100
1101
|
return input;
|
|
1101
1102
|
const contract = readToolContract(tool);
|
|
1102
|
-
if (Boolean(
|
|
1103
|
+
if (Boolean(compile(tool.parameters).check(input)) || (typeof contract.inputSchema === "boolean" || typeof contract.inputSchema === "object" && contract.inputSchema !== null && !Array.isArray(contract.inputSchema)) && Boolean(compile(contract.inputSchema).check(input)))
|
|
1103
1104
|
return input;
|
|
1104
1105
|
const text = input.trim();
|
|
1105
1106
|
if (contract.discovery === "help" && text === "help")
|
|
@@ -1262,9 +1263,9 @@ function createNestedToolTask(name) {
|
|
|
1262
1263
|
return reject(runtime, call, executionError("tool_unavailable", `Tool ${call.name} is not available`), agent, tool, context);
|
|
1263
1264
|
let args;
|
|
1264
1265
|
try {
|
|
1265
|
-
const supplied = task.input.arguments === null && !
|
|
1266
|
+
const supplied = task.input.arguments === null && !compile2(tool.parameters).check(null) && compile2(tool.parameters).check({}) ? undefined : task.input.arguments;
|
|
1266
1267
|
const prepared = tool.prepareArguments === undefined ? prepareNestedInput(tool, supplied) : tool.prepareArguments(prepareNestedInput(tool, supplied));
|
|
1267
|
-
args = checked(tool, call, prepared === undefined &&
|
|
1268
|
+
args = checked(tool, call, prepared === undefined && compile2(tool.parameters).check({}) ? {} : prepared);
|
|
1268
1269
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1269
1270
|
call.arguments = args;
|
|
1270
1271
|
} catch (error) {
|
|
@@ -1404,7 +1405,7 @@ function finishResult(tool, name, result) {
|
|
|
1404
1405
|
} else if (final.structuredOutput === undefined) {
|
|
1405
1406
|
if (!final.isError)
|
|
1406
1407
|
throw new Error(`Tool ${name} returned no structuredOutput`);
|
|
1407
|
-
} else if (!
|
|
1408
|
+
} else if (!compile2(tool.structuredOutputSchema).check(final.structuredOutput)) {
|
|
1408
1409
|
throw new Error(`Tool ${name} returned structuredOutput that does not match its schema`);
|
|
1409
1410
|
}
|
|
1410
1411
|
} catch (error) {
|
|
@@ -1425,6 +1426,83 @@ function finishResult(tool, name, result) {
|
|
|
1425
1426
|
return final;
|
|
1426
1427
|
}
|
|
1427
1428
|
|
|
1429
|
+
// packages/gippity-voice/src/agent-observation.ts
|
|
1430
|
+
import {
|
|
1431
|
+
LiveDoc as LiveDoc3
|
|
1432
|
+
} from "@earendil-works/pi-durable";
|
|
1433
|
+
async function observeVoiceAgent(conversation, context, callbacks) {
|
|
1434
|
+
const watch = await conversation.watch(context);
|
|
1435
|
+
let newest = watch.value.entries.at(-1)?.id ?? watch.value.before;
|
|
1436
|
+
let previous = liveState(watch.value);
|
|
1437
|
+
let text = "";
|
|
1438
|
+
let stopped = false;
|
|
1439
|
+
let work = Promise.resolve();
|
|
1440
|
+
callbacks.state(Boolean(previous.run));
|
|
1441
|
+
async function advance(view) {
|
|
1442
|
+
if (stopped)
|
|
1443
|
+
return;
|
|
1444
|
+
const live = liveState(view);
|
|
1445
|
+
if (live.run)
|
|
1446
|
+
callbacks.state(true);
|
|
1447
|
+
const upTo = view.entries.at(-1)?.id;
|
|
1448
|
+
if (upTo !== undefined && (newest === undefined || upTo > newest)) {
|
|
1449
|
+
const from = newest;
|
|
1450
|
+
let cursor;
|
|
1451
|
+
do {
|
|
1452
|
+
const page = await conversation.entries({
|
|
1453
|
+
...from === undefined ? {} : { minEntryId: from },
|
|
1454
|
+
maxEntryId: upTo,
|
|
1455
|
+
order: "ascending"
|
|
1456
|
+
}, 128, cursor, context);
|
|
1457
|
+
if (stopped)
|
|
1458
|
+
return;
|
|
1459
|
+
for (const entry of page.items) {
|
|
1460
|
+
if (newest !== undefined && entry.id <= newest)
|
|
1461
|
+
continue;
|
|
1462
|
+
callbacks.entry(entry);
|
|
1463
|
+
newest = entry.id;
|
|
1464
|
+
}
|
|
1465
|
+
cursor = page.next;
|
|
1466
|
+
} while (cursor);
|
|
1467
|
+
}
|
|
1468
|
+
const partial = live.generation?.message;
|
|
1469
|
+
const nextText = partial?.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
1470
|
+
`) ?? "";
|
|
1471
|
+
if (previous.run?.taskId !== live.run?.taskId || previous.generation?.attempt !== live.generation?.attempt || !nextText.startsWith(text))
|
|
1472
|
+
text = "";
|
|
1473
|
+
callbacks.partial(nextText.slice(text.length), Boolean(partial?.content.some((part) => part.type === "thinking" || part.type === "toolCall")));
|
|
1474
|
+
text = nextText;
|
|
1475
|
+
for (const compact of live.compactions ?? []) {
|
|
1476
|
+
if (compact.trigger !== "manual" && !previous.compactions?.some((old) => old.taskId === compact.taskId))
|
|
1477
|
+
callbacks.compacting(compact.trigger === "overflow" ? "overflow" : "threshold");
|
|
1478
|
+
}
|
|
1479
|
+
if (!live.run)
|
|
1480
|
+
callbacks.state(false);
|
|
1481
|
+
previous = live;
|
|
1482
|
+
}
|
|
1483
|
+
watch.start((call) => {
|
|
1484
|
+
work = call.kind === "failed" ? Promise.reject(call.error) : advance(call.value);
|
|
1485
|
+
return work;
|
|
1486
|
+
});
|
|
1487
|
+
watch.closed.then((end) => {
|
|
1488
|
+
if (end.kind === "failed" || end.kind === "listener_error")
|
|
1489
|
+
callbacks.failed(end.error);
|
|
1490
|
+
});
|
|
1491
|
+
return {
|
|
1492
|
+
get view() {
|
|
1493
|
+
return watch.value;
|
|
1494
|
+
},
|
|
1495
|
+
async stop() {
|
|
1496
|
+
stopped = true;
|
|
1497
|
+
await watch.stop();
|
|
1498
|
+
await work;
|
|
1499
|
+
}
|
|
1500
|
+
};
|
|
1501
|
+
}
|
|
1502
|
+
function liveState(view) {
|
|
1503
|
+
return view.docs[LiveDoc3.definition.kind] ?? {};
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1428
1506
|
// packages/gippity-voice/src/config.ts
|
|
1429
1507
|
function normalizeVoiceApiProvider(value) {
|
|
1430
1508
|
return typeof value === "string" && value.trim() === value && value.length > 0 && Buffer.byteLength(value) <= 512 && !/[\u0000-\u001f\u007f]/u.test(value) ? value : undefined;
|
|
@@ -1862,6 +1940,42 @@ function voiceTranscriptSince(view, before) {
|
|
|
1862
1940
|
var MAX_REALTIME_SDP_BYTES = 256 * 1024;
|
|
1863
1941
|
|
|
1864
1942
|
// packages/gippity-voice/src/voice/helper-protocol.ts
|
|
1943
|
+
var AUDIO_COUNTERS = [
|
|
1944
|
+
"elapsed_ms",
|
|
1945
|
+
"transitions",
|
|
1946
|
+
"drained_packets",
|
|
1947
|
+
"release_drained_packets",
|
|
1948
|
+
"rejected_packets",
|
|
1949
|
+
"buffering_entries",
|
|
1950
|
+
"tail_flushes",
|
|
1951
|
+
"clock_rebases",
|
|
1952
|
+
"decode_failures",
|
|
1953
|
+
"received_packets",
|
|
1954
|
+
"missing_packets",
|
|
1955
|
+
"decoded_packets",
|
|
1956
|
+
"decoded_packet_samples",
|
|
1957
|
+
"min_decoded_packet_samples",
|
|
1958
|
+
"max_decoded_packet_samples",
|
|
1959
|
+
"late_packets",
|
|
1960
|
+
"duplicate_packets",
|
|
1961
|
+
"reordered_packets",
|
|
1962
|
+
"resyncs",
|
|
1963
|
+
"resync_discarded_packets",
|
|
1964
|
+
"read_gaps_over_40ms",
|
|
1965
|
+
"max_read_gap_ms",
|
|
1966
|
+
"max_playout_lateness_us",
|
|
1967
|
+
"bridge_frames",
|
|
1968
|
+
"bridge_channel_full",
|
|
1969
|
+
"bridge_sends_over_1ms",
|
|
1970
|
+
"max_bridge_send_us"
|
|
1971
|
+
];
|
|
1972
|
+
function validAudioDiagnostics(value) {
|
|
1973
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
1974
|
+
return false;
|
|
1975
|
+
const data = value;
|
|
1976
|
+
const counter = (item) => typeof item === "number" && Number.isInteger(item) && item >= 0 && item <= 4294967295;
|
|
1977
|
+
return data["epoch"] !== undefined && validEpoch(data["epoch"]) && typeof data["suppressed"] === "boolean" && AUDIO_COUNTERS.every((key) => counter(data[key])) && (data["first_packet_ms"] === null || counter(data["first_packet_ms"])) && Object.keys(data).every((key) => key === "epoch" || key === "suppressed" || key === "first_packet_ms" || AUDIO_COUNTERS.some((field) => field === key));
|
|
1978
|
+
}
|
|
1865
1979
|
var MAX_PCM_BYTES = 64 * 1024;
|
|
1866
1980
|
var MAX_DATA_MESSAGE_BYTES = 64 * 1024;
|
|
1867
1981
|
var MAX_TEXT_BYTES = 8 * 1024;
|
|
@@ -1938,6 +2052,8 @@ function parseVoiceHelperEvent(value) {
|
|
|
1938
2052
|
return event;
|
|
1939
2053
|
if (event["type"] === "playback_activity")
|
|
1940
2054
|
return { type: "playback_activity" };
|
|
2055
|
+
if (event["type"] === "audio_diagnostics" && validAudioDiagnostics(event["diagnostics"]))
|
|
2056
|
+
return { type: "audio_diagnostics", diagnostics: event["diagnostics"] };
|
|
1941
2057
|
if (event["type"] === "pcm" && validBase64(event["audio"], MAX_PCM_BYTES) && event["sample_rate"] === 24000 && event["num_channels"] === 1 && validEpoch(event["epoch"]))
|
|
1942
2058
|
return event;
|
|
1943
2059
|
if (event["type"] === "error" && boundedString(event["message"], MAX_TEXT_BYTES))
|
|
@@ -2022,7 +2138,7 @@ class VoiceHelperClient {
|
|
|
2022
2138
|
try {
|
|
2023
2139
|
const event = parseVoiceHelperEvent(JSON.parse(line));
|
|
2024
2140
|
if (event.type === "ready") {
|
|
2025
|
-
if (event.version === 2 || event.version === 3 || event.version === 4 || event.version === 5 || event.version === 6) {
|
|
2141
|
+
if (event.version === 2 || event.version === 3 || event.version === 4 || event.version === 5 || event.version === 6 || event.version === 7 || event.version === 8) {
|
|
2026
2142
|
this.helperProtocolVersion = event.version;
|
|
2027
2143
|
ready.resolve();
|
|
2028
2144
|
} else
|
|
@@ -2196,10 +2312,10 @@ class NativeCodexRealtimePeer {
|
|
|
2196
2312
|
}
|
|
2197
2313
|
async start(config) {
|
|
2198
2314
|
await this.helper.start();
|
|
2199
|
-
if (this.helper.protocolVersion !==
|
|
2315
|
+
if (this.helper.protocolVersion !== 8) {
|
|
2200
2316
|
const actualVersion = this.helper.protocolVersion ?? "unknown";
|
|
2201
2317
|
await this.helper.close();
|
|
2202
|
-
throw new Error(`Incompatible Codex voice helper protocol ${actualVersion}; expected
|
|
2318
|
+
throw new Error(`Incompatible Codex voice helper protocol ${actualVersion}; expected 8`);
|
|
2203
2319
|
}
|
|
2204
2320
|
const offer = deferred();
|
|
2205
2321
|
const removeEvent = this.helper.onEvent((event) => {
|
|
@@ -2245,7 +2361,7 @@ class NativeCodexRealtimePeer {
|
|
|
2245
2361
|
}
|
|
2246
2362
|
}
|
|
2247
2363
|
function toPeerEvent(event) {
|
|
2248
|
-
if (event.type === "state" || event.type === "data" || event.type === "error" || event.type === "playback_activity")
|
|
2364
|
+
if (event.type === "state" || event.type === "data" || event.type === "error" || event.type === "playback_activity" || event.type === "audio_diagnostics")
|
|
2249
2365
|
return event;
|
|
2250
2366
|
return;
|
|
2251
2367
|
}
|
|
@@ -2512,6 +2628,7 @@ function escapeXml(value) {
|
|
|
2512
2628
|
}
|
|
2513
2629
|
|
|
2514
2630
|
// packages/gippity-voice/src/voice/turns.ts
|
|
2631
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
2515
2632
|
var MAX_TRANSCRIPT_DELTA_BYTES = 32 * 1024;
|
|
2516
2633
|
|
|
2517
2634
|
class RealtimeTranscriptBuffer {
|
|
@@ -2569,9 +2686,9 @@ class RealtimeTranscriptBuffer {
|
|
|
2569
2686
|
this.reset();
|
|
2570
2687
|
return transcript || undefined;
|
|
2571
2688
|
}
|
|
2572
|
-
takeInterval(currentUser) {
|
|
2689
|
+
takeInterval(input, currentUser) {
|
|
2573
2690
|
const user = currentUser ?? this.active.get("user");
|
|
2574
|
-
const interval = this.entries.filter((entry) => entry !== user);
|
|
2691
|
+
const interval = this.entries.filter((entry) => entry !== user || entry.text.trim() !== input.trim());
|
|
2575
2692
|
const text = this.render(interval);
|
|
2576
2693
|
this.entries = interval.filter((entry) => !entry.final && entry.role === "assistant");
|
|
2577
2694
|
this.active.clear();
|
|
@@ -2632,6 +2749,7 @@ class RealtimeVoiceTurnTracker {
|
|
|
2632
2749
|
delegatedUserFinishes = 0;
|
|
2633
2750
|
delegatedUserTurns = new Set;
|
|
2634
2751
|
delegationIds = new Set;
|
|
2752
|
+
delegatedTurnInputs = new Set;
|
|
2635
2753
|
outstandingDelegations = new Map;
|
|
2636
2754
|
outstandingInputs = new Set;
|
|
2637
2755
|
get hasPendingInput() {
|
|
@@ -2666,12 +2784,17 @@ class RealtimeVoiceTurnTracker {
|
|
|
2666
2784
|
this.pendingUserInputs.push({ input, transcript });
|
|
2667
2785
|
return true;
|
|
2668
2786
|
}
|
|
2669
|
-
delegated(input, delegationId) {
|
|
2787
|
+
delegated(input, delegationId, userTurnId) {
|
|
2670
2788
|
if (this.delegationIds.has(delegationId))
|
|
2671
2789
|
return;
|
|
2672
2790
|
this.delegationIds.add(delegationId);
|
|
2673
2791
|
if (!this.preserveInterval && this.outstandingInputs.has(input))
|
|
2674
2792
|
return;
|
|
2793
|
+
const turnInput = userTurnId ? createHash2("sha256").update(JSON.stringify([userTurnId, input])).digest("hex") : undefined;
|
|
2794
|
+
if (turnInput && this.delegatedTurnInputs.has(turnInput))
|
|
2795
|
+
return;
|
|
2796
|
+
if (turnInput)
|
|
2797
|
+
this.delegatedTurnInputs.add(turnInput);
|
|
2675
2798
|
this.outstandingDelegations.set(delegationId, input);
|
|
2676
2799
|
this.outstandingInputs.add(input);
|
|
2677
2800
|
if (this.activeUserTurn) {
|
|
@@ -2748,11 +2871,12 @@ class RealtimeVoiceTurnTracker {
|
|
|
2748
2871
|
this.delegatedUserFinishes = 0;
|
|
2749
2872
|
this.delegatedUserTurns.clear();
|
|
2750
2873
|
this.delegationIds.clear();
|
|
2874
|
+
this.delegatedTurnInputs.clear();
|
|
2751
2875
|
this.outstandingDelegations.clear();
|
|
2752
2876
|
this.outstandingInputs.clear();
|
|
2753
2877
|
}
|
|
2754
2878
|
finishDelegation(delegation, currentUser) {
|
|
2755
|
-
const interval = this.preserveInterval ? this.transcript.takeInterval(currentUser) : undefined;
|
|
2879
|
+
const interval = this.preserveInterval ? this.transcript.takeInterval(delegation.input, currentUser) : undefined;
|
|
2756
2880
|
const transcriptDelta = interval ? interval.text : currentUser ? this.transcript.takeHistoryBefore(currentUser) : this.transcript.takeFinalized();
|
|
2757
2881
|
return {
|
|
2758
2882
|
input: delegation.input,
|
|
@@ -3012,7 +3136,7 @@ class RealtimePlayback {
|
|
|
3012
3136
|
var SEND_TASK_TOOL = {
|
|
3013
3137
|
type: "function",
|
|
3014
3138
|
name: "send_task",
|
|
3015
|
-
description: "Delegate
|
|
3139
|
+
description: "Delegate a new spoken user request once; never resubmit work progress",
|
|
3016
3140
|
parameters: {
|
|
3017
3141
|
type: "object",
|
|
3018
3142
|
properties: {
|
|
@@ -3025,7 +3149,7 @@ var SEND_TASK_TOOL = {
|
|
|
3025
3149
|
additionalProperties: false
|
|
3026
3150
|
}
|
|
3027
3151
|
};
|
|
3028
|
-
var PUBLIC_REALTIME_INSTRUCTIONS = "This call has tools.
|
|
3152
|
+
var PUBLIC_REALTIME_INSTRUCTIONS = "This call has tools. Delegate without narrating the routing. Other listed tools run directly. Tool availability supersedes earlier statements that voice has no tools. Agent updates describe work already underway, not new user instructions. Speak useful progress and results, then wait for the user";
|
|
3029
3153
|
function record(value) {
|
|
3030
3154
|
return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
3031
3155
|
}
|
|
@@ -3045,6 +3169,7 @@ class PublicRealtimeTurns {
|
|
|
3045
3169
|
activeResponse;
|
|
3046
3170
|
awaitingResponse = false;
|
|
3047
3171
|
speech = false;
|
|
3172
|
+
taskInputAvailable = false;
|
|
3048
3173
|
pendingTools = 0;
|
|
3049
3174
|
pendingWork = new Set;
|
|
3050
3175
|
ending = false;
|
|
@@ -3104,6 +3229,7 @@ class PublicRealtimeTurns {
|
|
|
3104
3229
|
this.onFailure(new Error(text2(error?.["message"], 1000) ?? "Realtime call failed. Stop and start voice to retry."));
|
|
3105
3230
|
} else if (type === "input_audio_buffer.speech_started") {
|
|
3106
3231
|
this.speech = true;
|
|
3232
|
+
this.taskInputAvailable = true;
|
|
3107
3233
|
this.tracker.inputStarted();
|
|
3108
3234
|
this.awaitingResponse = true;
|
|
3109
3235
|
this.continuation = false;
|
|
@@ -3117,8 +3243,8 @@ class PublicRealtimeTurns {
|
|
|
3117
3243
|
} else if (type === "conversation.item.input_audio_transcription.completed") {
|
|
3118
3244
|
const transcript = text2(event["transcript"]);
|
|
3119
3245
|
if (transcript) {
|
|
3120
|
-
|
|
3121
|
-
|
|
3246
|
+
this.tracker.userFinished(transcript);
|
|
3247
|
+
this.callbacks.onUserTranscript(transcript);
|
|
3122
3248
|
}
|
|
3123
3249
|
} else if (type === "response.created") {
|
|
3124
3250
|
const response = record(event["response"]);
|
|
@@ -3162,6 +3288,8 @@ class PublicRealtimeTurns {
|
|
|
3162
3288
|
}
|
|
3163
3289
|
if (!interrupted && response?.["status"] === "completed" && Array.isArray(response["output"])) {
|
|
3164
3290
|
this.completingResponse = true;
|
|
3291
|
+
if (!response["output"].some((item) => record(item)?.["type"] === "function_call"))
|
|
3292
|
+
this.taskInputAvailable = false;
|
|
3165
3293
|
for (const item of response["output"]) {
|
|
3166
3294
|
const call = record(item);
|
|
3167
3295
|
if (call?.["type"] === "function_call") {
|
|
@@ -3198,6 +3326,7 @@ class PublicRealtimeTurns {
|
|
|
3198
3326
|
this.calls.add(id);
|
|
3199
3327
|
this.pendingTools++;
|
|
3200
3328
|
let output;
|
|
3329
|
+
let continueResponse = true;
|
|
3201
3330
|
try {
|
|
3202
3331
|
const name = text2(call["name"], 64);
|
|
3203
3332
|
const raw = text2(call["arguments"], 32 * 1024);
|
|
@@ -3218,13 +3347,19 @@ class PublicRealtimeTurns {
|
|
|
3218
3347
|
const request = text2(args["request"]);
|
|
3219
3348
|
if (!request || Object.keys(args).some((key) => key !== "request"))
|
|
3220
3349
|
throw new Error("send_task requires only a nonempty request");
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3350
|
+
if (!this.taskInputAvailable) {
|
|
3351
|
+
output = {
|
|
3352
|
+
accepted: false,
|
|
3353
|
+
reason: "No new user speech. Wait for the work already submitted; do not retry."
|
|
3354
|
+
};
|
|
3355
|
+
continueResponse = false;
|
|
3356
|
+
} else {
|
|
3357
|
+
this.taskInputAvailable = false;
|
|
3358
|
+
const delegated = this.tracker.delegated(request, id);
|
|
3359
|
+
if (delegated)
|
|
3360
|
+
this.callbacks.onTurn(delegated.turn);
|
|
3361
|
+
output = { accepted: Boolean(delegated), task_id: id };
|
|
3226
3362
|
}
|
|
3227
|
-
output = { accepted: Boolean(delegated), task_id: id };
|
|
3228
3363
|
} else {
|
|
3229
3364
|
if (!this.access)
|
|
3230
3365
|
throw new Error("Voice tool is unavailable; restart the call");
|
|
@@ -3253,7 +3388,8 @@ class PublicRealtimeTurns {
|
|
|
3253
3388
|
output: JSON.stringify(output)
|
|
3254
3389
|
}
|
|
3255
3390
|
});
|
|
3256
|
-
|
|
3391
|
+
if (continueResponse)
|
|
3392
|
+
this.continuation = true;
|
|
3257
3393
|
this.schedule();
|
|
3258
3394
|
}
|
|
3259
3395
|
send(event) {
|
|
@@ -3437,7 +3573,7 @@ class ConversationSetup {
|
|
|
3437
3573
|
import { randomUUID } from "node:crypto";
|
|
3438
3574
|
|
|
3439
3575
|
// packages/gippity-voice/src/voice/conversation/wire.ts
|
|
3440
|
-
import { createHash as
|
|
3576
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
3441
3577
|
function realtimeEventIdentity(value) {
|
|
3442
3578
|
if (!value || typeof value !== "object")
|
|
3443
3579
|
return;
|
|
@@ -3469,7 +3605,7 @@ function realtimeEventDetails(callId, event, text, accepted) {
|
|
|
3469
3605
|
...typeof offset === "number" && Number.isFinite(offset) ? { offsetMs: offset } : {},
|
|
3470
3606
|
...typeof start === "number" && Number.isFinite(start) ? { startMs: start } : {},
|
|
3471
3607
|
...typeof end === "number" && Number.isFinite(end) ? { endMs: end } : {},
|
|
3472
|
-
...transcript ? { textHash:
|
|
3608
|
+
...transcript ? { textHash: createHash3("sha256").update(transcript).digest("hex") } : {},
|
|
3473
3609
|
accepted
|
|
3474
3610
|
};
|
|
3475
3611
|
}
|
|
@@ -3621,7 +3757,7 @@ class ConversationTurns {
|
|
|
3621
3757
|
this.onFailure(new Error("Codex voice delegation was empty"));
|
|
3622
3758
|
return;
|
|
3623
3759
|
}
|
|
3624
|
-
const delegated = this.turnTracker.delegated(input, delegationId);
|
|
3760
|
+
const delegated = this.turnTracker.delegated(input, delegationId, typeof record["user_bidi_turn_id"] === "string" ? record["user_bidi_turn_id"] : undefined);
|
|
3625
3761
|
this.callbacks.onEvent?.(realtimeEventDetails(this.callId, event, input, Boolean(delegated)));
|
|
3626
3762
|
if (!delegated)
|
|
3627
3763
|
return;
|
|
@@ -3986,6 +4122,8 @@ class CodexRealtimeConversation {
|
|
|
3986
4122
|
this.fail(error);
|
|
3987
4123
|
return;
|
|
3988
4124
|
}
|
|
4125
|
+
if (event.type === "audio_diagnostics")
|
|
4126
|
+
this.callbacks.onAudioDiagnostics?.(event.diagnostics);
|
|
3989
4127
|
if (event.type === "playback_activity") {
|
|
3990
4128
|
if (this.turns instanceof PublicRealtimeTurns)
|
|
3991
4129
|
this.turns.audioActivity();
|
|
@@ -4809,6 +4947,11 @@ function validSdp(value) {
|
|
|
4809
4947
|
}
|
|
4810
4948
|
function readPeerEvent(value) {
|
|
4811
4949
|
if (isRecord(value)) {
|
|
4950
|
+
if (value["type"] === "audio_diagnostics") {
|
|
4951
|
+
const event = parseVoiceHelperEvent(value);
|
|
4952
|
+
if (event.type === "audio_diagnostics")
|
|
4953
|
+
return event;
|
|
4954
|
+
}
|
|
4812
4955
|
if (value["type"] === "playback_activity")
|
|
4813
4956
|
return { type: "playback_activity" };
|
|
4814
4957
|
if (value["type"] === "state" && typeof value["state"] === "string" && value["state"].length <= 128)
|
|
@@ -5279,10 +5422,16 @@ import { WebSocket as WebSocket4 } from "ws";
|
|
|
5279
5422
|
// packages/gippity-voice/src/voice/lan/browser-connections.ts
|
|
5280
5423
|
import { WebSocket as WebSocket2 } from "ws";
|
|
5281
5424
|
var MAX_SOCKET_AUDIO_BACKLOG = 24000 * 2;
|
|
5425
|
+
var AUDIO_REPORT_INTERVAL_MS = 5000;
|
|
5282
5426
|
|
|
5283
5427
|
class LanVoiceBrowserConnections {
|
|
5284
5428
|
eventResponses = new Map;
|
|
5285
5429
|
audioSockets = new Map;
|
|
5430
|
+
audioReports = new WeakMap;
|
|
5431
|
+
onAudioDiagnostics;
|
|
5432
|
+
constructor(onAudioDiagnostics) {
|
|
5433
|
+
this.onAudioDiagnostics = onAudioDiagnostics;
|
|
5434
|
+
}
|
|
5286
5435
|
connectEvents(clientId, response, closed) {
|
|
5287
5436
|
if (closed) {
|
|
5288
5437
|
response.end();
|
|
@@ -5303,12 +5452,15 @@ class LanVoiceBrowserConnections {
|
|
|
5303
5452
|
}
|
|
5304
5453
|
const previous = this.audioSockets.get(clientId);
|
|
5305
5454
|
this.audioSockets.set(clientId, socket);
|
|
5306
|
-
if (previous)
|
|
5455
|
+
if (previous) {
|
|
5456
|
+
this.finishAudioReport(previous, "replaced");
|
|
5307
5457
|
callbacks.onReplaced(previous);
|
|
5458
|
+
}
|
|
5308
5459
|
previous?.close(4001, "replaced");
|
|
5309
5460
|
socket.send(JSON.stringify({ type: "connected" }));
|
|
5310
5461
|
socket.on("message", callbacks.onMessage);
|
|
5311
5462
|
socket.once("close", () => {
|
|
5463
|
+
this.finishAudioReport(socket, "closed");
|
|
5312
5464
|
if (this.audioSockets.get(clientId) === socket)
|
|
5313
5465
|
this.audioSockets.delete(clientId);
|
|
5314
5466
|
callbacks.onClose();
|
|
@@ -5335,8 +5487,69 @@ class LanVoiceBrowserConnections {
|
|
|
5335
5487
|
this.sendControl(clientId, value);
|
|
5336
5488
|
}
|
|
5337
5489
|
sendAudio(socket, pcm) {
|
|
5338
|
-
if (socket.readyState
|
|
5490
|
+
if (socket.readyState !== WebSocket2.OPEN)
|
|
5491
|
+
return;
|
|
5492
|
+
const now = performance.now();
|
|
5493
|
+
const report = this.audioReports.get(socket) ?? {
|
|
5494
|
+
startedAt: now,
|
|
5495
|
+
lastArrivalAt: now,
|
|
5496
|
+
sentFrames: 0,
|
|
5497
|
+
sentBytes: 0,
|
|
5498
|
+
droppedFrames: 0,
|
|
5499
|
+
droppedBytes: 0,
|
|
5500
|
+
maxSocketBacklogBytes: 0,
|
|
5501
|
+
sourcePcmArrivalGapMaxMs: 0
|
|
5502
|
+
};
|
|
5503
|
+
this.audioReports.set(socket, report);
|
|
5504
|
+
report.sourcePcmArrivalGapMaxMs = Math.max(report.sourcePcmArrivalGapMaxMs, now - report.lastArrivalAt);
|
|
5505
|
+
report.lastArrivalAt = now;
|
|
5506
|
+
report.maxSocketBacklogBytes = Math.max(report.maxSocketBacklogBytes, socket.bufferedAmount);
|
|
5507
|
+
if (socket.bufferedAmount < MAX_SOCKET_AUDIO_BACKLOG) {
|
|
5339
5508
|
socket.send(pcm);
|
|
5509
|
+
report.maxSocketBacklogBytes = Math.max(report.maxSocketBacklogBytes, socket.bufferedAmount);
|
|
5510
|
+
report.sentFrames++;
|
|
5511
|
+
report.sentBytes += pcm.byteLength;
|
|
5512
|
+
} else {
|
|
5513
|
+
report.droppedFrames++;
|
|
5514
|
+
report.droppedBytes += pcm.byteLength;
|
|
5515
|
+
}
|
|
5516
|
+
if (!report.timer) {
|
|
5517
|
+
report.startedAt = now;
|
|
5518
|
+
report.timer = setTimeout(() => this.flushAudioReport(socket, "interval"), AUDIO_REPORT_INTERVAL_MS);
|
|
5519
|
+
report.timer.unref();
|
|
5520
|
+
}
|
|
5521
|
+
}
|
|
5522
|
+
flushAudioReport(socket, reason) {
|
|
5523
|
+
const report = this.audioReports.get(socket);
|
|
5524
|
+
if (!report)
|
|
5525
|
+
return;
|
|
5526
|
+
clearTimeout(report.timer);
|
|
5527
|
+
report.timer = undefined;
|
|
5528
|
+
if (report.sentFrames + report.droppedFrames === 0)
|
|
5529
|
+
return;
|
|
5530
|
+
const diagnostics = {
|
|
5531
|
+
source: "lan-playback",
|
|
5532
|
+
reason,
|
|
5533
|
+
windowMs: performance.now() - report.startedAt,
|
|
5534
|
+
sentFrames: report.sentFrames,
|
|
5535
|
+
sentBytes: report.sentBytes,
|
|
5536
|
+
droppedFrames: report.droppedFrames,
|
|
5537
|
+
droppedBytes: report.droppedBytes,
|
|
5538
|
+
maxSocketBacklogBytes: report.maxSocketBacklogBytes,
|
|
5539
|
+
sourcePcmArrivalGapMaxMs: report.sourcePcmArrivalGapMaxMs,
|
|
5540
|
+
sourcePcmArrivalGapIncludesPauses: true
|
|
5541
|
+
};
|
|
5542
|
+
report.sentFrames = report.sentBytes = report.droppedFrames = report.droppedBytes = 0;
|
|
5543
|
+
report.maxSocketBacklogBytes = report.sourcePcmArrivalGapMaxMs = 0;
|
|
5544
|
+
try {
|
|
5545
|
+
this.onAudioDiagnostics?.(diagnostics);
|
|
5546
|
+
} catch {
|
|
5547
|
+
console.warn("[voice audio] LAN playback diagnostics could not be saved");
|
|
5548
|
+
}
|
|
5549
|
+
}
|
|
5550
|
+
finishAudioReport(socket, reason) {
|
|
5551
|
+
this.flushAudioReport(socket, reason);
|
|
5552
|
+
this.audioReports.delete(socket);
|
|
5340
5553
|
}
|
|
5341
5554
|
sendAudioControl(socket, value) {
|
|
5342
5555
|
if (socket.readyState === WebSocket2.OPEN)
|
|
@@ -5351,6 +5564,7 @@ class LanVoiceBrowserConnections {
|
|
|
5351
5564
|
}
|
|
5352
5565
|
close(failures) {
|
|
5353
5566
|
for (const socket of this.audioSockets.values()) {
|
|
5567
|
+
this.finishAudioReport(socket, "server-closed");
|
|
5354
5568
|
try {
|
|
5355
5569
|
socket.terminate();
|
|
5356
5570
|
} catch (error) {
|
|
@@ -5509,6 +5723,7 @@ class LanVoiceBrowserSession {
|
|
|
5509
5723
|
state = { type: "idle" };
|
|
5510
5724
|
operation = Promise.resolve();
|
|
5511
5725
|
conversationOwnerId;
|
|
5726
|
+
stopCompletion;
|
|
5512
5727
|
conversationSpeakerSuppressed = false;
|
|
5513
5728
|
microphoneLevel;
|
|
5514
5729
|
constructor(options, connections) {
|
|
@@ -5540,27 +5755,41 @@ class LanVoiceBrowserSession {
|
|
|
5540
5755
|
}
|
|
5541
5756
|
}
|
|
5542
5757
|
release(clientId, socket, terminateConversation = false) {
|
|
5758
|
+
if (terminateConversation && this.stopCompletion?.clientId === clientId)
|
|
5759
|
+
return this.stopCompletion.promise;
|
|
5760
|
+
const starting = this.state;
|
|
5761
|
+
const ownsConversation = this.conversationOwnerId === clientId && (!socket || (starting.type === "active" || starting.type === "starting") && starting.socket === socket);
|
|
5543
5762
|
this.releaseStarting(clientId, socket);
|
|
5544
|
-
|
|
5545
|
-
|
|
5546
|
-
|
|
5547
|
-
|
|
5548
|
-
|
|
5549
|
-
if (ownsActive && active.mode === "conversation")
|
|
5550
|
-
this.microphoneLevel.reset();
|
|
5551
|
-
if (terminateConversation && this.conversationOwnerId === clientId) {
|
|
5552
|
-
this.conversationOwnerId = undefined;
|
|
5553
|
-
await this.options.onConversationActivity(false);
|
|
5763
|
+
let completion;
|
|
5764
|
+
if (terminateConversation && starting.type === "starting" && starting.mode === "conversation" && starting.clientId === clientId && (!socket || starting.socket === socket)) {
|
|
5765
|
+
this.conversationOwnerId = undefined;
|
|
5766
|
+
completion = Promise.resolve().then(() => this.options.onConversationActivity(false)).then(() => true);
|
|
5767
|
+
this.operation = Promise.all([this.operation, completion]).then(() => {
|
|
5554
5768
|
return;
|
|
5555
|
-
}
|
|
5556
|
-
if (!ownsActive)
|
|
5769
|
+
}, () => {
|
|
5557
5770
|
return;
|
|
5558
|
-
|
|
5559
|
-
|
|
5560
|
-
|
|
5561
|
-
|
|
5562
|
-
|
|
5563
|
-
|
|
5771
|
+
});
|
|
5772
|
+
} else {
|
|
5773
|
+
completion = this.enqueue(async () => {
|
|
5774
|
+
const active = this.state;
|
|
5775
|
+
const ownsActive = active.type === "active" && active.clientId === clientId && (!socket || active.socket === socket);
|
|
5776
|
+
if (ownsActive)
|
|
5777
|
+
this.state = { type: "idle" };
|
|
5778
|
+
if (ownsActive && active.mode === "conversation")
|
|
5779
|
+
this.microphoneLevel.reset();
|
|
5780
|
+
if (terminateConversation && this.conversationOwnerId === clientId && (!socket || ownsActive)) {
|
|
5781
|
+
this.conversationOwnerId = undefined;
|
|
5782
|
+
await this.options.onConversationActivity(false);
|
|
5783
|
+
return true;
|
|
5784
|
+
}
|
|
5785
|
+
if (ownsActive && active.mode === "dictation")
|
|
5786
|
+
await this.options.finishDictation(clientId);
|
|
5787
|
+
return false;
|
|
5788
|
+
});
|
|
5789
|
+
}
|
|
5790
|
+
if (terminateConversation && ownsConversation)
|
|
5791
|
+
this.stopCompletion = { clientId, promise: completion };
|
|
5792
|
+
return completion;
|
|
5564
5793
|
}
|
|
5565
5794
|
releaseStarting(clientId, socket) {
|
|
5566
5795
|
const starting = this.state;
|
|
@@ -5609,8 +5838,10 @@ class LanVoiceBrowserSession {
|
|
|
5609
5838
|
return;
|
|
5610
5839
|
const starting = { type: "starting", clientId, socket, mode };
|
|
5611
5840
|
this.state = starting;
|
|
5612
|
-
if (mode === "conversation")
|
|
5841
|
+
if (mode === "conversation") {
|
|
5842
|
+
this.stopCompletion = undefined;
|
|
5613
5843
|
this.conversationOwnerId = clientId;
|
|
5844
|
+
}
|
|
5614
5845
|
try {
|
|
5615
5846
|
if (mode === "conversation")
|
|
5616
5847
|
await this.options.ensureConversation(takeoverToken);
|
|
@@ -5682,7 +5913,7 @@ class LanVoiceBrowserSession {
|
|
|
5682
5913
|
}
|
|
5683
5914
|
this.state = { type: "closed" };
|
|
5684
5915
|
const failures = [];
|
|
5685
|
-
if (active.type === "active" && active.mode === "conversation") {
|
|
5916
|
+
if ((active.type === "active" || active.type === "starting") && active.mode === "conversation") {
|
|
5686
5917
|
try {
|
|
5687
5918
|
this.conversationOwnerId = undefined;
|
|
5688
5919
|
await this.options.onConversationActivity(false);
|
|
@@ -5721,10 +5952,11 @@ class LanVoiceBrowserSession {
|
|
|
5721
5952
|
|
|
5722
5953
|
// packages/gippity-voice/src/voice/lan/browser-clients.ts
|
|
5723
5954
|
class LanVoiceBrowserClients {
|
|
5724
|
-
connections
|
|
5955
|
+
connections;
|
|
5725
5956
|
session;
|
|
5726
5957
|
takeoverAck;
|
|
5727
5958
|
constructor(options) {
|
|
5959
|
+
this.connections = new LanVoiceBrowserConnections(options.onAudioDiagnostics);
|
|
5728
5960
|
this.session = new LanVoiceBrowserSession(options, this.connections);
|
|
5729
5961
|
}
|
|
5730
5962
|
connectEvents(clientId, response) {
|
|
@@ -5734,7 +5966,12 @@ class LanVoiceBrowserClients {
|
|
|
5734
5966
|
this.connections.connectAudio(clientId, socket, this.session.closed, {
|
|
5735
5967
|
onMessage: (data, isBinary) => this.receive(clientId, socket, data, isBinary),
|
|
5736
5968
|
onReplaced: (previous) => this.session.releaseStarting(clientId, previous),
|
|
5737
|
-
onClose: () =>
|
|
5969
|
+
onClose: () => {
|
|
5970
|
+
this.session.release(clientId, socket).catch((error) => this.connections.sendControl(clientId, {
|
|
5971
|
+
type: "error",
|
|
5972
|
+
message: errorMessage(error)
|
|
5973
|
+
}));
|
|
5974
|
+
}
|
|
5738
5975
|
});
|
|
5739
5976
|
}
|
|
5740
5977
|
sendControl(clientId, value) {
|
|
@@ -5788,7 +6025,7 @@ class LanVoiceBrowserClients {
|
|
|
5788
6025
|
this.session.resetConversationInputLevel();
|
|
5789
6026
|
}
|
|
5790
6027
|
release(clientId, socket, terminateConversation = false) {
|
|
5791
|
-
this.session.release(clientId, socket, terminateConversation);
|
|
6028
|
+
return this.session.release(clientId, socket, terminateConversation);
|
|
5792
6029
|
}
|
|
5793
6030
|
heartbeat() {
|
|
5794
6031
|
this.connections.heartbeat();
|
|
@@ -5817,7 +6054,7 @@ class LanVoiceBrowserClients {
|
|
|
5817
6054
|
} else if (message.type === "finish") {
|
|
5818
6055
|
this.session.finish(clientId, socket, message.draft, message.revision, message.selection).catch((error) => this.sendSocketError(socket, error));
|
|
5819
6056
|
} else if (message.type === "release") {
|
|
5820
|
-
this.session.release(clientId, socket, true);
|
|
6057
|
+
this.session.release(clientId, socket, true).catch((error) => this.sendSocketError(socket, error));
|
|
5821
6058
|
} else if (message.type === "mute") {
|
|
5822
6059
|
this.session.mute(clientId, socket, message.muted);
|
|
5823
6060
|
} else {
|
|
@@ -5870,10 +6107,10 @@ class LanHostRealtimePeer {
|
|
|
5870
6107
|
}
|
|
5871
6108
|
async start(_config) {
|
|
5872
6109
|
await this.helper.start();
|
|
5873
|
-
if (this.helper.protocolVersion !==
|
|
6110
|
+
if (this.helper.protocolVersion !== 8) {
|
|
5874
6111
|
const actualVersion = this.helper.protocolVersion ?? "unknown";
|
|
5875
6112
|
await this.helper.close();
|
|
5876
|
-
throw new Error(`Incompatible Codex voice helper protocol ${actualVersion}; expected
|
|
6113
|
+
throw new Error(`Incompatible Codex voice helper protocol ${actualVersion}; expected 8`);
|
|
5877
6114
|
}
|
|
5878
6115
|
const offer = deferred();
|
|
5879
6116
|
const removeEvent = this.helper.onEvent((event) => {
|
|
@@ -5930,7 +6167,7 @@ class LanHostRealtimePeer {
|
|
|
5930
6167
|
}
|
|
5931
6168
|
}
|
|
5932
6169
|
function toPeerEvent2(event) {
|
|
5933
|
-
if (event.type === "state" || event.type === "data" || event.type === "error" || event.type === "playback_activity")
|
|
6170
|
+
if (event.type === "state" || event.type === "data" || event.type === "error" || event.type === "playback_activity" || event.type === "audio_diagnostics")
|
|
5934
6171
|
return event;
|
|
5935
6172
|
return;
|
|
5936
6173
|
}
|
|
@@ -7306,6 +7543,7 @@ async function startGippityLanServer(options) {
|
|
|
7306
7543
|
}
|
|
7307
7544
|
});
|
|
7308
7545
|
clients = new LanVoiceBrowserClients({
|
|
7546
|
+
...options.onAudioDiagnostics ? { onAudioDiagnostics: options.onAudioDiagnostics } : {},
|
|
7309
7547
|
async ensureConversation(takeoverToken) {
|
|
7310
7548
|
if (closing)
|
|
7311
7549
|
throw new Error("LAN server is closing");
|
|
@@ -7371,7 +7609,7 @@ async function startGippityLanServer(options) {
|
|
|
7371
7609
|
async onConversationActivity(active) {
|
|
7372
7610
|
if (releasing)
|
|
7373
7611
|
return;
|
|
7374
|
-
if (!active && browserVoice) {
|
|
7612
|
+
if (!active && (browserVoice || browserStarting)) {
|
|
7375
7613
|
browserVoice = false;
|
|
7376
7614
|
browserPeer = undefined;
|
|
7377
7615
|
await controls.stopVoice();
|
|
@@ -7606,7 +7844,7 @@ data: {}
|
|
|
7606
7844
|
return send(response, 200, { ok: true });
|
|
7607
7845
|
}
|
|
7608
7846
|
if (path === "/api/stop") {
|
|
7609
|
-
clients.release(client, undefined, body["terminateConversation"] === true);
|
|
7847
|
+
await clients.release(client, undefined, body["terminateConversation"] === true);
|
|
7610
7848
|
return send(response, 200, { ok: true });
|
|
7611
7849
|
}
|
|
7612
7850
|
if (path === "/api/rpc") {
|
|
@@ -7726,28 +7964,21 @@ data: {}
|
|
|
7726
7964
|
else if (event.type === "lifecycle" && !event.active)
|
|
7727
7965
|
clients.broadcastControl({ type: "stop", reason: "voice-ended" });
|
|
7728
7966
|
},
|
|
7729
|
-
|
|
7730
|
-
if (
|
|
7731
|
-
|
|
7732
|
-
|
|
7733
|
-
|
|
7734
|
-
|
|
7735
|
-
|
|
7736
|
-
}
|
|
7967
|
+
agentState(working) {
|
|
7968
|
+
if (working)
|
|
7969
|
+
activity.working();
|
|
7970
|
+
else if (activity.snapshot().state === "working")
|
|
7971
|
+
activity.settled();
|
|
7972
|
+
},
|
|
7973
|
+
agentEntry(entry) {
|
|
7737
7974
|
clients.broadcastControl({
|
|
7738
7975
|
type: "durable.event",
|
|
7739
|
-
event:
|
|
7740
|
-
data: remoteJsonValue(
|
|
7976
|
+
event: "message_end",
|
|
7977
|
+
data: remoteJsonValue({ type: "message_end", entry })
|
|
7741
7978
|
});
|
|
7742
|
-
|
|
7743
|
-
|
|
7744
|
-
|
|
7745
|
-
activity.settled();
|
|
7746
|
-
if (event.type === "message_end") {
|
|
7747
|
-
const text = boundedAssistantText((event.entry.model ?? []).flatMap((message) => message.role === "assistant" && message.stopReason !== "toolUse" ? message.content : []));
|
|
7748
|
-
if (text)
|
|
7749
|
-
activity.settled(text);
|
|
7750
|
-
}
|
|
7979
|
+
const text = boundedAssistantText((entry.model ?? []).flatMap((message) => message.role === "assistant" && message.stopReason !== "toolUse" ? message.content : []));
|
|
7980
|
+
if (text)
|
|
7981
|
+
activity.settled(text);
|
|
7751
7982
|
}
|
|
7752
7983
|
};
|
|
7753
7984
|
}
|
|
@@ -8027,7 +8258,7 @@ import {
|
|
|
8027
8258
|
uuidv7,
|
|
8028
8259
|
validateToolArguments as validateToolArguments2
|
|
8029
8260
|
} from "@earendil-works/pi-ai";
|
|
8030
|
-
var SYSTEM_PROMPT = "Rewrite this conversation as a natural, self-contained request to another assistant. Preserve the user's intent, relevant context and uncertainty without inventing facts or decisions. Don't carry out the request. Prioritise the user's messages over assistant speech or work progress when deciding whether to end the call. Use end_call
|
|
8261
|
+
var SYSTEM_PROMPT = "Rewrite this conversation as a natural, self-contained request to another assistant. Preserve the user's intent, relevant context and uncertainty without inventing facts or decisions. Preserve the user's tone, register and emphasis. Remove transcription noise without formalising, softening or embellishing the request. Do not turn instructions into questions or add conversational next steps the user did not ask for. Don't carry out the request. Prioritise the user's messages over assistant speech or work progress when deciding whether to end the call. Use end_call when the user wants or agrees to end the call, including natural wrap-ups like 'that's all I needed for now'. Thanks alone, assistant completion or finishing one task while continuing the call is not end intent. Otherwise return only the request";
|
|
8031
8262
|
async function rewriteTaskDelegation(models, voice, turn, signal) {
|
|
8032
8263
|
let failure;
|
|
8033
8264
|
for (let attempt = 0;attempt < 4; attempt++) {
|
|
@@ -8329,11 +8560,38 @@ function createGippityVoice(options) {
|
|
|
8329
8560
|
config = normalizeGippityControlConfig(await options.settings.read());
|
|
8330
8561
|
saved = true;
|
|
8331
8562
|
}
|
|
8332
|
-
events = await
|
|
8333
|
-
|
|
8334
|
-
|
|
8335
|
-
|
|
8336
|
-
|
|
8563
|
+
events = await observeVoiceAgent(conversation, context, {
|
|
8564
|
+
state(value) {
|
|
8565
|
+
if (transferred)
|
|
8566
|
+
return;
|
|
8567
|
+
const changed = working !== value;
|
|
8568
|
+
working = value;
|
|
8569
|
+
lan?.agentState(working);
|
|
8570
|
+
if (!changed)
|
|
8571
|
+
return;
|
|
8572
|
+
const type = working ? "run_start" : "run_end";
|
|
8573
|
+
for (const listener of listeners)
|
|
8574
|
+
listener(type, { type });
|
|
8575
|
+
if (working) {
|
|
8576
|
+
session?.agentStarted();
|
|
8577
|
+
session?.resumeAgentWork();
|
|
8578
|
+
} else
|
|
8579
|
+
session?.settleAgentTurn();
|
|
8580
|
+
},
|
|
8581
|
+
entry: handleEntry,
|
|
8582
|
+
partial(delta, active) {
|
|
8583
|
+
if (transferred)
|
|
8584
|
+
return;
|
|
8585
|
+
if (delta)
|
|
8586
|
+
session?.streamAgentDelta(delta);
|
|
8587
|
+
if (active)
|
|
8588
|
+
session?.resumeAgentWork();
|
|
8589
|
+
},
|
|
8590
|
+
compacting: (reason) => session?.announceCompactionStart(reason),
|
|
8591
|
+
failed(error) {
|
|
8592
|
+
report(error);
|
|
8593
|
+
enqueue(stopSession).catch(report);
|
|
8594
|
+
}
|
|
8337
8595
|
});
|
|
8338
8596
|
ready?.resolve();
|
|
8339
8597
|
await new Promise((resolve) => {
|
|
@@ -8517,6 +8775,7 @@ function createGippityVoice(options) {
|
|
|
8517
8775
|
delegations.push(turn, callId);
|
|
8518
8776
|
},
|
|
8519
8777
|
onEvent: (event) => publish({ type: "voice-event", event }),
|
|
8778
|
+
onAudioDiagnostics: (diagnostics) => publish({ type: "audio-diagnostics", diagnostics }),
|
|
8520
8779
|
rewriteDelegation: (turn, signal) => rewriteTaskDelegation(requireRuntime().runtime.models, settings.voice, turn, signal),
|
|
8521
8780
|
onDelegationError: report,
|
|
8522
8781
|
onEndCall: () => {
|
|
@@ -8691,74 +8950,35 @@ ${tail}
|
|
|
8691
8950
|
releaseDelegations();
|
|
8692
8951
|
}
|
|
8693
8952
|
}
|
|
8694
|
-
function
|
|
8953
|
+
function handleEntry(entry) {
|
|
8695
8954
|
if (transferred)
|
|
8696
8955
|
return;
|
|
8697
|
-
const notification = event.type === "snapshot" ? {
|
|
8698
|
-
type: "snapshot",
|
|
8699
|
-
working: Boolean(event.run),
|
|
8700
|
-
model: event.agent.model ?? null
|
|
8701
|
-
} : event;
|
|
8702
8956
|
for (const listener of listeners)
|
|
8703
|
-
listener(
|
|
8704
|
-
lan?.
|
|
8705
|
-
if (
|
|
8706
|
-
|
|
8707
|
-
|
|
8708
|
-
working = true;
|
|
8709
|
-
if (event.type === "run_end")
|
|
8710
|
-
working = false;
|
|
8711
|
-
if (event.type === "run_start") {
|
|
8712
|
-
session?.agentStarted();
|
|
8713
|
-
session?.resumeAgentWork();
|
|
8714
|
-
}
|
|
8715
|
-
if (event.type === "run_end")
|
|
8716
|
-
session?.settleAgentTurn();
|
|
8717
|
-
if (event.type === "message_update") {
|
|
8718
|
-
for (const change of event.changes)
|
|
8719
|
-
if (change.type === "text_delta")
|
|
8720
|
-
session?.streamAgentDelta(change.delta);
|
|
8721
|
-
else if (change.type === "thinking_start" || change.type === "toolcall_start")
|
|
8722
|
-
session?.resumeAgentWork();
|
|
8723
|
-
}
|
|
8724
|
-
if (event.type === "message_end" && event.entry.kind === "gippity.voice.delegation" && isRecord(event.entry.data) && typeof event.entry.data["id"] === "string")
|
|
8725
|
-
if (event.entry.data["callId"] === activeCallId)
|
|
8726
|
-
session?.activateDelegation(event.entry.data["id"]);
|
|
8957
|
+
listener("message_end", { type: "message_end", entry });
|
|
8958
|
+
lan?.agentEntry(entry);
|
|
8959
|
+
if (entry.kind === "gippity.voice.delegation" && isRecord(entry.data) && typeof entry.data["id"] === "string")
|
|
8960
|
+
if (entry.data["callId"] === activeCallId)
|
|
8961
|
+
session?.activateDelegation(entry.data["id"]);
|
|
8727
8962
|
else
|
|
8728
8963
|
session?.agentStarted();
|
|
8729
|
-
|
|
8730
|
-
|
|
8731
|
-
|
|
8732
|
-
const text = typeof message.content === "string" ? message.content : message.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
8964
|
+
for (const message of entry.model ?? []) {
|
|
8965
|
+
if (message.role === "user" && UserEntry.is(entry)) {
|
|
8966
|
+
const text = typeof message.content === "string" ? message.content : message.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
8733
8967
|
`);
|
|
8734
|
-
|
|
8735
|
-
|
|
8736
|
-
}
|
|
8737
|
-
if (message.role !== "assistant")
|
|
8738
|
-
continue;
|
|
8739
|
-
const text = message.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
8740
|
-
`);
|
|
8741
|
-
if (message.stopReason === "toolUse") {
|
|
8742
|
-
session?.agentProgress(text, activeConfig?.voice.forwardReasoningSummaries ? completedVoiceReasoningSummary(message) : undefined);
|
|
8743
|
-
} else
|
|
8744
|
-
session?.agentResult(text, message.stopReason);
|
|
8968
|
+
session?.piInput(text, working ? "steer" : undefined);
|
|
8969
|
+
session?.piUserMessage(message);
|
|
8745
8970
|
}
|
|
8746
|
-
|
|
8747
|
-
|
|
8748
|
-
|
|
8749
|
-
|
|
8750
|
-
|
|
8751
|
-
|
|
8752
|
-
|
|
8753
|
-
|
|
8754
|
-
const submission = await harness.submission(result.submissionId, runtimeContext);
|
|
8755
|
-
if (!submission || (await submission.wait(runtimeContext)).status !== "done")
|
|
8756
|
-
return;
|
|
8757
|
-
} else if (result.entryId === undefined)
|
|
8758
|
-
return;
|
|
8759
|
-
return enqueue(refreshContext);
|
|
8760
|
-
}).catch(report);
|
|
8971
|
+
if (message.role !== "assistant")
|
|
8972
|
+
continue;
|
|
8973
|
+
const text = message.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
8974
|
+
`);
|
|
8975
|
+
if (message.stopReason === "toolUse") {
|
|
8976
|
+
session?.agentProgress(text, activeConfig?.voice.forwardReasoningSummaries ? completedVoiceReasoningSummary(message) : undefined);
|
|
8977
|
+
} else
|
|
8978
|
+
session?.agentResult(text, message.stopReason);
|
|
8761
8979
|
}
|
|
8980
|
+
if (CompactionEntry.is(entry) && activeConfig?.voice.refreshRealtimeAfterCompaction)
|
|
8981
|
+
enqueue(refreshContext).catch(report);
|
|
8762
8982
|
}
|
|
8763
8983
|
async function ensureLan(context, takeover) {
|
|
8764
8984
|
await ensureService(context);
|
|
@@ -8782,11 +9002,12 @@ ${tail}
|
|
|
8782
9002
|
context: runtimeContext,
|
|
8783
9003
|
resolveAuth: () => resolveDictationAuth(runtime.signal),
|
|
8784
9004
|
remoteApps,
|
|
9005
|
+
onAudioDiagnostics: (diagnostics) => publish({ type: "lan-playback-diagnostics", diagnostics }),
|
|
8785
9006
|
...takeover ? { takeoverToken: takeover.token } : {},
|
|
8786
9007
|
startConversation: (factory, token) => startVoice(runtimeContext, factory, true, arrival.consume(token))
|
|
8787
9008
|
});
|
|
8788
9009
|
if (events)
|
|
8789
|
-
lan.
|
|
9010
|
+
lan.agentState(working);
|
|
8790
9011
|
});
|
|
8791
9012
|
return lan.urls;
|
|
8792
9013
|
}
|
|
@@ -9392,7 +9613,7 @@ ${continuity}`
|
|
|
9392
9613
|
event: "binding",
|
|
9393
9614
|
data: {
|
|
9394
9615
|
conversationId: runtime.conversationId,
|
|
9395
|
-
model: events?.
|
|
9616
|
+
model: events?.view.docs[AgentDoc.definition.kind]?.model ?? null
|
|
9396
9617
|
}
|
|
9397
9618
|
});
|
|
9398
9619
|
link.notify({
|