@howaboua/pi-durable-gippity-voice 0.9.1 → 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 +9707 -0
- package/dist/index.js +369 -148
- package/dist/packages/execution/src/tool-contract.d.ts +2 -2
- package/dist/packages/gippity-voice/src/activation.d.ts +8 -0
- package/dist/packages/gippity-voice/src/agent-observation.d.ts +13 -0
- package/dist/packages/gippity-voice/src/component-settings.d.ts +3 -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 +35 -6
- 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/index.js
CHANGED
|
@@ -642,10 +642,11 @@ import {
|
|
|
642
642
|
withAbortSignal
|
|
643
643
|
} from "@earendil-works/chord/context";
|
|
644
644
|
import {
|
|
645
|
+
AgentDoc,
|
|
646
|
+
CompactionEntry,
|
|
645
647
|
defineExtension,
|
|
646
648
|
defineTask as defineTask2,
|
|
647
|
-
UserEntry
|
|
648
|
-
watchEvents
|
|
649
|
+
UserEntry
|
|
649
650
|
} from "@earendil-works/pi-durable";
|
|
650
651
|
|
|
651
652
|
// internal/settings/index.ts
|
|
@@ -755,7 +756,7 @@ function createSettingsContribution(options) {
|
|
|
755
756
|
import {
|
|
756
757
|
defineTask
|
|
757
758
|
} from "@earendil-works/pi-durable";
|
|
758
|
-
import {
|
|
759
|
+
import { compile as compile2 } from "@earendil-works/pi-schema";
|
|
759
760
|
|
|
760
761
|
// packages/execution/src/forwarding.ts
|
|
761
762
|
import { copyJson as copyJson2 } from "@earendil-works/chord";
|
|
@@ -1609,7 +1610,7 @@ import { validateToolArguments } from "@earendil-works/pi-ai/utils/validation";
|
|
|
1609
1610
|
import {
|
|
1610
1611
|
ToolTask as ToolTask3
|
|
1611
1612
|
} from "@earendil-works/pi-durable";
|
|
1612
|
-
import {
|
|
1613
|
+
import { compile } from "@earendil-works/pi-schema";
|
|
1613
1614
|
|
|
1614
1615
|
// packages/execution/src/tool-contract.ts
|
|
1615
1616
|
import { copyJson as copyJson5 } from "@earendil-works/chord";
|
|
@@ -1698,7 +1699,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1698
1699
|
if (typeof input !== "string")
|
|
1699
1700
|
return input;
|
|
1700
1701
|
const contract = readToolContract(tool);
|
|
1701
|
-
if (Boolean(
|
|
1702
|
+
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)))
|
|
1702
1703
|
return input;
|
|
1703
1704
|
const text = input.trim();
|
|
1704
1705
|
if (contract.discovery === "help" && text === "help")
|
|
@@ -1861,9 +1862,9 @@ function createNestedToolTask(name) {
|
|
|
1861
1862
|
return reject(runtime, call, executionError("tool_unavailable", `Tool ${call.name} is not available`), agent, tool, context);
|
|
1862
1863
|
let args;
|
|
1863
1864
|
try {
|
|
1864
|
-
const supplied = task.input.arguments === null && !
|
|
1865
|
+
const supplied = task.input.arguments === null && !compile2(tool.parameters).check(null) && compile2(tool.parameters).check({}) ? undefined : task.input.arguments;
|
|
1865
1866
|
const prepared = tool.prepareArguments === undefined ? prepareNestedInput(tool, supplied) : tool.prepareArguments(prepareNestedInput(tool, supplied));
|
|
1866
|
-
args = checked(tool, call, prepared === undefined &&
|
|
1867
|
+
args = checked(tool, call, prepared === undefined && compile2(tool.parameters).check({}) ? {} : prepared);
|
|
1867
1868
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1868
1869
|
call.arguments = args;
|
|
1869
1870
|
} catch (error) {
|
|
@@ -2003,7 +2004,7 @@ function finishResult(tool, name, result) {
|
|
|
2003
2004
|
} else if (final.structuredOutput === undefined) {
|
|
2004
2005
|
if (!final.isError)
|
|
2005
2006
|
throw new Error(`Tool ${name} returned no structuredOutput`);
|
|
2006
|
-
} else if (!
|
|
2007
|
+
} else if (!compile2(tool.structuredOutputSchema).check(final.structuredOutput)) {
|
|
2007
2008
|
throw new Error(`Tool ${name} returned structuredOutput that does not match its schema`);
|
|
2008
2009
|
}
|
|
2009
2010
|
} catch (error) {
|
|
@@ -2024,6 +2025,83 @@ function finishResult(tool, name, result) {
|
|
|
2024
2025
|
return final;
|
|
2025
2026
|
}
|
|
2026
2027
|
|
|
2028
|
+
// packages/gippity-voice/src/agent-observation.ts
|
|
2029
|
+
import {
|
|
2030
|
+
LiveDoc as LiveDoc3
|
|
2031
|
+
} from "@earendil-works/pi-durable";
|
|
2032
|
+
async function observeVoiceAgent(conversation, context, callbacks) {
|
|
2033
|
+
const watch = await conversation.watch(context);
|
|
2034
|
+
let newest = watch.value.entries.at(-1)?.id ?? watch.value.before;
|
|
2035
|
+
let previous = liveState(watch.value);
|
|
2036
|
+
let text = "";
|
|
2037
|
+
let stopped = false;
|
|
2038
|
+
let work = Promise.resolve();
|
|
2039
|
+
callbacks.state(Boolean(previous.run));
|
|
2040
|
+
async function advance(view) {
|
|
2041
|
+
if (stopped)
|
|
2042
|
+
return;
|
|
2043
|
+
const live = liveState(view);
|
|
2044
|
+
if (live.run)
|
|
2045
|
+
callbacks.state(true);
|
|
2046
|
+
const upTo = view.entries.at(-1)?.id;
|
|
2047
|
+
if (upTo !== undefined && (newest === undefined || upTo > newest)) {
|
|
2048
|
+
const from = newest;
|
|
2049
|
+
let cursor;
|
|
2050
|
+
do {
|
|
2051
|
+
const page = await conversation.entries({
|
|
2052
|
+
...from === undefined ? {} : { minEntryId: from },
|
|
2053
|
+
maxEntryId: upTo,
|
|
2054
|
+
order: "ascending"
|
|
2055
|
+
}, 128, cursor, context);
|
|
2056
|
+
if (stopped)
|
|
2057
|
+
return;
|
|
2058
|
+
for (const entry of page.items) {
|
|
2059
|
+
if (newest !== undefined && entry.id <= newest)
|
|
2060
|
+
continue;
|
|
2061
|
+
callbacks.entry(entry);
|
|
2062
|
+
newest = entry.id;
|
|
2063
|
+
}
|
|
2064
|
+
cursor = page.next;
|
|
2065
|
+
} while (cursor);
|
|
2066
|
+
}
|
|
2067
|
+
const partial = live.generation?.message;
|
|
2068
|
+
const nextText = partial?.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
2069
|
+
`) ?? "";
|
|
2070
|
+
if (previous.run?.taskId !== live.run?.taskId || previous.generation?.attempt !== live.generation?.attempt || !nextText.startsWith(text))
|
|
2071
|
+
text = "";
|
|
2072
|
+
callbacks.partial(nextText.slice(text.length), Boolean(partial?.content.some((part) => part.type === "thinking" || part.type === "toolCall")));
|
|
2073
|
+
text = nextText;
|
|
2074
|
+
for (const compact of live.compactions ?? []) {
|
|
2075
|
+
if (compact.trigger !== "manual" && !previous.compactions?.some((old) => old.taskId === compact.taskId))
|
|
2076
|
+
callbacks.compacting(compact.trigger === "overflow" ? "overflow" : "threshold");
|
|
2077
|
+
}
|
|
2078
|
+
if (!live.run)
|
|
2079
|
+
callbacks.state(false);
|
|
2080
|
+
previous = live;
|
|
2081
|
+
}
|
|
2082
|
+
watch.start((call) => {
|
|
2083
|
+
work = call.kind === "failed" ? Promise.reject(call.error) : advance(call.value);
|
|
2084
|
+
return work;
|
|
2085
|
+
});
|
|
2086
|
+
watch.closed.then((end) => {
|
|
2087
|
+
if (end.kind === "failed" || end.kind === "listener_error")
|
|
2088
|
+
callbacks.failed(end.error);
|
|
2089
|
+
});
|
|
2090
|
+
return {
|
|
2091
|
+
get view() {
|
|
2092
|
+
return watch.value;
|
|
2093
|
+
},
|
|
2094
|
+
async stop() {
|
|
2095
|
+
stopped = true;
|
|
2096
|
+
await watch.stop();
|
|
2097
|
+
await work;
|
|
2098
|
+
}
|
|
2099
|
+
};
|
|
2100
|
+
}
|
|
2101
|
+
function liveState(view) {
|
|
2102
|
+
return view.docs[LiveDoc3.definition.kind] ?? {};
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2027
2105
|
// packages/gippity-voice/src/preferences.ts
|
|
2028
2106
|
function object(value, path) {
|
|
2029
2107
|
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
@@ -2248,6 +2326,42 @@ function voiceTranscriptSince(view, before) {
|
|
|
2248
2326
|
var MAX_REALTIME_SDP_BYTES = 256 * 1024;
|
|
2249
2327
|
|
|
2250
2328
|
// packages/gippity-voice/src/voice/helper-protocol.ts
|
|
2329
|
+
var AUDIO_COUNTERS = [
|
|
2330
|
+
"elapsed_ms",
|
|
2331
|
+
"transitions",
|
|
2332
|
+
"drained_packets",
|
|
2333
|
+
"release_drained_packets",
|
|
2334
|
+
"rejected_packets",
|
|
2335
|
+
"buffering_entries",
|
|
2336
|
+
"tail_flushes",
|
|
2337
|
+
"clock_rebases",
|
|
2338
|
+
"decode_failures",
|
|
2339
|
+
"received_packets",
|
|
2340
|
+
"missing_packets",
|
|
2341
|
+
"decoded_packets",
|
|
2342
|
+
"decoded_packet_samples",
|
|
2343
|
+
"min_decoded_packet_samples",
|
|
2344
|
+
"max_decoded_packet_samples",
|
|
2345
|
+
"late_packets",
|
|
2346
|
+
"duplicate_packets",
|
|
2347
|
+
"reordered_packets",
|
|
2348
|
+
"resyncs",
|
|
2349
|
+
"resync_discarded_packets",
|
|
2350
|
+
"read_gaps_over_40ms",
|
|
2351
|
+
"max_read_gap_ms",
|
|
2352
|
+
"max_playout_lateness_us",
|
|
2353
|
+
"bridge_frames",
|
|
2354
|
+
"bridge_channel_full",
|
|
2355
|
+
"bridge_sends_over_1ms",
|
|
2356
|
+
"max_bridge_send_us"
|
|
2357
|
+
];
|
|
2358
|
+
function validAudioDiagnostics(value) {
|
|
2359
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
2360
|
+
return false;
|
|
2361
|
+
const data = value;
|
|
2362
|
+
const counter = (item) => typeof item === "number" && Number.isInteger(item) && item >= 0 && item <= 4294967295;
|
|
2363
|
+
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));
|
|
2364
|
+
}
|
|
2251
2365
|
var MAX_PCM_BYTES = 64 * 1024;
|
|
2252
2366
|
var MAX_DATA_MESSAGE_BYTES = 64 * 1024;
|
|
2253
2367
|
var MAX_TEXT_BYTES = 8 * 1024;
|
|
@@ -2324,6 +2438,8 @@ function parseVoiceHelperEvent(value) {
|
|
|
2324
2438
|
return event;
|
|
2325
2439
|
if (event["type"] === "playback_activity")
|
|
2326
2440
|
return { type: "playback_activity" };
|
|
2441
|
+
if (event["type"] === "audio_diagnostics" && validAudioDiagnostics(event["diagnostics"]))
|
|
2442
|
+
return { type: "audio_diagnostics", diagnostics: event["diagnostics"] };
|
|
2327
2443
|
if (event["type"] === "pcm" && validBase64(event["audio"], MAX_PCM_BYTES) && event["sample_rate"] === 24000 && event["num_channels"] === 1 && validEpoch(event["epoch"]))
|
|
2328
2444
|
return event;
|
|
2329
2445
|
if (event["type"] === "error" && boundedString(event["message"], MAX_TEXT_BYTES))
|
|
@@ -2408,7 +2524,7 @@ class VoiceHelperClient {
|
|
|
2408
2524
|
try {
|
|
2409
2525
|
const event = parseVoiceHelperEvent(JSON.parse(line));
|
|
2410
2526
|
if (event.type === "ready") {
|
|
2411
|
-
if (event.version === 2 || event.version === 3 || event.version === 4 || event.version === 5 || event.version === 6) {
|
|
2527
|
+
if (event.version === 2 || event.version === 3 || event.version === 4 || event.version === 5 || event.version === 6 || event.version === 7 || event.version === 8) {
|
|
2412
2528
|
this.helperProtocolVersion = event.version;
|
|
2413
2529
|
ready.resolve();
|
|
2414
2530
|
} else
|
|
@@ -2582,10 +2698,10 @@ class NativeCodexRealtimePeer {
|
|
|
2582
2698
|
}
|
|
2583
2699
|
async start(config) {
|
|
2584
2700
|
await this.helper.start();
|
|
2585
|
-
if (this.helper.protocolVersion !==
|
|
2701
|
+
if (this.helper.protocolVersion !== 8) {
|
|
2586
2702
|
const actualVersion = this.helper.protocolVersion ?? "unknown";
|
|
2587
2703
|
await this.helper.close();
|
|
2588
|
-
throw new Error(`Incompatible Codex voice helper protocol ${actualVersion}; expected
|
|
2704
|
+
throw new Error(`Incompatible Codex voice helper protocol ${actualVersion}; expected 8`);
|
|
2589
2705
|
}
|
|
2590
2706
|
const offer = deferred();
|
|
2591
2707
|
const removeEvent = this.helper.onEvent((event) => {
|
|
@@ -2631,7 +2747,7 @@ class NativeCodexRealtimePeer {
|
|
|
2631
2747
|
}
|
|
2632
2748
|
}
|
|
2633
2749
|
function toPeerEvent(event) {
|
|
2634
|
-
if (event.type === "state" || event.type === "data" || event.type === "error" || event.type === "playback_activity")
|
|
2750
|
+
if (event.type === "state" || event.type === "data" || event.type === "error" || event.type === "playback_activity" || event.type === "audio_diagnostics")
|
|
2635
2751
|
return event;
|
|
2636
2752
|
return;
|
|
2637
2753
|
}
|
|
@@ -2775,6 +2891,7 @@ function escapeXml(value) {
|
|
|
2775
2891
|
}
|
|
2776
2892
|
|
|
2777
2893
|
// packages/gippity-voice/src/voice/turns.ts
|
|
2894
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
2778
2895
|
var MAX_TRANSCRIPT_DELTA_BYTES = 32 * 1024;
|
|
2779
2896
|
|
|
2780
2897
|
class RealtimeTranscriptBuffer {
|
|
@@ -2832,9 +2949,9 @@ class RealtimeTranscriptBuffer {
|
|
|
2832
2949
|
this.reset();
|
|
2833
2950
|
return transcript || undefined;
|
|
2834
2951
|
}
|
|
2835
|
-
takeInterval(currentUser) {
|
|
2952
|
+
takeInterval(input, currentUser) {
|
|
2836
2953
|
const user = currentUser ?? this.active.get("user");
|
|
2837
|
-
const interval = this.entries.filter((entry) => entry !== user);
|
|
2954
|
+
const interval = this.entries.filter((entry) => entry !== user || entry.text.trim() !== input.trim());
|
|
2838
2955
|
const text = this.render(interval);
|
|
2839
2956
|
this.entries = interval.filter((entry) => !entry.final && entry.role === "assistant");
|
|
2840
2957
|
this.active.clear();
|
|
@@ -2895,6 +3012,7 @@ class RealtimeVoiceTurnTracker {
|
|
|
2895
3012
|
delegatedUserFinishes = 0;
|
|
2896
3013
|
delegatedUserTurns = new Set;
|
|
2897
3014
|
delegationIds = new Set;
|
|
3015
|
+
delegatedTurnInputs = new Set;
|
|
2898
3016
|
outstandingDelegations = new Map;
|
|
2899
3017
|
outstandingInputs = new Set;
|
|
2900
3018
|
get hasPendingInput() {
|
|
@@ -2929,12 +3047,17 @@ class RealtimeVoiceTurnTracker {
|
|
|
2929
3047
|
this.pendingUserInputs.push({ input, transcript });
|
|
2930
3048
|
return true;
|
|
2931
3049
|
}
|
|
2932
|
-
delegated(input, delegationId) {
|
|
3050
|
+
delegated(input, delegationId, userTurnId) {
|
|
2933
3051
|
if (this.delegationIds.has(delegationId))
|
|
2934
3052
|
return;
|
|
2935
3053
|
this.delegationIds.add(delegationId);
|
|
2936
3054
|
if (!this.preserveInterval && this.outstandingInputs.has(input))
|
|
2937
3055
|
return;
|
|
3056
|
+
const turnInput = userTurnId ? createHash2("sha256").update(JSON.stringify([userTurnId, input])).digest("hex") : undefined;
|
|
3057
|
+
if (turnInput && this.delegatedTurnInputs.has(turnInput))
|
|
3058
|
+
return;
|
|
3059
|
+
if (turnInput)
|
|
3060
|
+
this.delegatedTurnInputs.add(turnInput);
|
|
2938
3061
|
this.outstandingDelegations.set(delegationId, input);
|
|
2939
3062
|
this.outstandingInputs.add(input);
|
|
2940
3063
|
if (this.activeUserTurn) {
|
|
@@ -3011,11 +3134,12 @@ class RealtimeVoiceTurnTracker {
|
|
|
3011
3134
|
this.delegatedUserFinishes = 0;
|
|
3012
3135
|
this.delegatedUserTurns.clear();
|
|
3013
3136
|
this.delegationIds.clear();
|
|
3137
|
+
this.delegatedTurnInputs.clear();
|
|
3014
3138
|
this.outstandingDelegations.clear();
|
|
3015
3139
|
this.outstandingInputs.clear();
|
|
3016
3140
|
}
|
|
3017
3141
|
finishDelegation(delegation, currentUser) {
|
|
3018
|
-
const interval = this.preserveInterval ? this.transcript.takeInterval(currentUser) : undefined;
|
|
3142
|
+
const interval = this.preserveInterval ? this.transcript.takeInterval(delegation.input, currentUser) : undefined;
|
|
3019
3143
|
const transcriptDelta = interval ? interval.text : currentUser ? this.transcript.takeHistoryBefore(currentUser) : this.transcript.takeFinalized();
|
|
3020
3144
|
return {
|
|
3021
3145
|
input: delegation.input,
|
|
@@ -3275,7 +3399,7 @@ class RealtimePlayback {
|
|
|
3275
3399
|
var SEND_TASK_TOOL = {
|
|
3276
3400
|
type: "function",
|
|
3277
3401
|
name: "send_task",
|
|
3278
|
-
description: "Delegate
|
|
3402
|
+
description: "Delegate a new spoken user request once; never resubmit work progress",
|
|
3279
3403
|
parameters: {
|
|
3280
3404
|
type: "object",
|
|
3281
3405
|
properties: {
|
|
@@ -3288,7 +3412,7 @@ var SEND_TASK_TOOL = {
|
|
|
3288
3412
|
additionalProperties: false
|
|
3289
3413
|
}
|
|
3290
3414
|
};
|
|
3291
|
-
var PUBLIC_REALTIME_INSTRUCTIONS = "This call has tools.
|
|
3415
|
+
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";
|
|
3292
3416
|
function record(value) {
|
|
3293
3417
|
return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
3294
3418
|
}
|
|
@@ -3308,6 +3432,7 @@ class PublicRealtimeTurns {
|
|
|
3308
3432
|
activeResponse;
|
|
3309
3433
|
awaitingResponse = false;
|
|
3310
3434
|
speech = false;
|
|
3435
|
+
taskInputAvailable = false;
|
|
3311
3436
|
pendingTools = 0;
|
|
3312
3437
|
pendingWork = new Set;
|
|
3313
3438
|
ending = false;
|
|
@@ -3367,6 +3492,7 @@ class PublicRealtimeTurns {
|
|
|
3367
3492
|
this.onFailure(new Error(text2(error?.["message"], 1000) ?? "Realtime call failed. Stop and start voice to retry."));
|
|
3368
3493
|
} else if (type === "input_audio_buffer.speech_started") {
|
|
3369
3494
|
this.speech = true;
|
|
3495
|
+
this.taskInputAvailable = true;
|
|
3370
3496
|
this.tracker.inputStarted();
|
|
3371
3497
|
this.awaitingResponse = true;
|
|
3372
3498
|
this.continuation = false;
|
|
@@ -3380,8 +3506,8 @@ class PublicRealtimeTurns {
|
|
|
3380
3506
|
} else if (type === "conversation.item.input_audio_transcription.completed") {
|
|
3381
3507
|
const transcript = text2(event["transcript"]);
|
|
3382
3508
|
if (transcript) {
|
|
3383
|
-
|
|
3384
|
-
|
|
3509
|
+
this.tracker.userFinished(transcript);
|
|
3510
|
+
this.callbacks.onUserTranscript(transcript);
|
|
3385
3511
|
}
|
|
3386
3512
|
} else if (type === "response.created") {
|
|
3387
3513
|
const response = record(event["response"]);
|
|
@@ -3425,6 +3551,8 @@ class PublicRealtimeTurns {
|
|
|
3425
3551
|
}
|
|
3426
3552
|
if (!interrupted && response?.["status"] === "completed" && Array.isArray(response["output"])) {
|
|
3427
3553
|
this.completingResponse = true;
|
|
3554
|
+
if (!response["output"].some((item) => record(item)?.["type"] === "function_call"))
|
|
3555
|
+
this.taskInputAvailable = false;
|
|
3428
3556
|
for (const item of response["output"]) {
|
|
3429
3557
|
const call = record(item);
|
|
3430
3558
|
if (call?.["type"] === "function_call") {
|
|
@@ -3461,6 +3589,7 @@ class PublicRealtimeTurns {
|
|
|
3461
3589
|
this.calls.add(id);
|
|
3462
3590
|
this.pendingTools++;
|
|
3463
3591
|
let output;
|
|
3592
|
+
let continueResponse = true;
|
|
3464
3593
|
try {
|
|
3465
3594
|
const name = text2(call["name"], 64);
|
|
3466
3595
|
const raw = text2(call["arguments"], 32 * 1024);
|
|
@@ -3481,13 +3610,19 @@ class PublicRealtimeTurns {
|
|
|
3481
3610
|
const request = text2(args["request"]);
|
|
3482
3611
|
if (!request || Object.keys(args).some((key) => key !== "request"))
|
|
3483
3612
|
throw new Error("send_task requires only a nonempty request");
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3613
|
+
if (!this.taskInputAvailable) {
|
|
3614
|
+
output = {
|
|
3615
|
+
accepted: false,
|
|
3616
|
+
reason: "No new user speech. Wait for the work already submitted; do not retry."
|
|
3617
|
+
};
|
|
3618
|
+
continueResponse = false;
|
|
3619
|
+
} else {
|
|
3620
|
+
this.taskInputAvailable = false;
|
|
3621
|
+
const delegated = this.tracker.delegated(request, id);
|
|
3622
|
+
if (delegated)
|
|
3623
|
+
this.callbacks.onTurn(delegated.turn);
|
|
3624
|
+
output = { accepted: Boolean(delegated), task_id: id };
|
|
3489
3625
|
}
|
|
3490
|
-
output = { accepted: Boolean(delegated), task_id: id };
|
|
3491
3626
|
} else {
|
|
3492
3627
|
if (!this.access)
|
|
3493
3628
|
throw new Error("Voice tool is unavailable; restart the call");
|
|
@@ -3516,7 +3651,8 @@ class PublicRealtimeTurns {
|
|
|
3516
3651
|
output: JSON.stringify(output)
|
|
3517
3652
|
}
|
|
3518
3653
|
});
|
|
3519
|
-
|
|
3654
|
+
if (continueResponse)
|
|
3655
|
+
this.continuation = true;
|
|
3520
3656
|
this.schedule();
|
|
3521
3657
|
}
|
|
3522
3658
|
send(event) {
|
|
@@ -3700,7 +3836,7 @@ class ConversationSetup {
|
|
|
3700
3836
|
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
3701
3837
|
|
|
3702
3838
|
// packages/gippity-voice/src/voice/conversation/wire.ts
|
|
3703
|
-
import { createHash as
|
|
3839
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
3704
3840
|
function realtimeEventIdentity(value) {
|
|
3705
3841
|
if (!value || typeof value !== "object")
|
|
3706
3842
|
return;
|
|
@@ -3732,7 +3868,7 @@ function realtimeEventDetails(callId, event, text, accepted) {
|
|
|
3732
3868
|
...typeof offset === "number" && Number.isFinite(offset) ? { offsetMs: offset } : {},
|
|
3733
3869
|
...typeof start === "number" && Number.isFinite(start) ? { startMs: start } : {},
|
|
3734
3870
|
...typeof end === "number" && Number.isFinite(end) ? { endMs: end } : {},
|
|
3735
|
-
...transcript ? { textHash:
|
|
3871
|
+
...transcript ? { textHash: createHash3("sha256").update(transcript).digest("hex") } : {},
|
|
3736
3872
|
accepted
|
|
3737
3873
|
};
|
|
3738
3874
|
}
|
|
@@ -3884,7 +4020,7 @@ class ConversationTurns {
|
|
|
3884
4020
|
this.onFailure(new Error("Codex voice delegation was empty"));
|
|
3885
4021
|
return;
|
|
3886
4022
|
}
|
|
3887
|
-
const delegated = this.turnTracker.delegated(input, delegationId);
|
|
4023
|
+
const delegated = this.turnTracker.delegated(input, delegationId, typeof record["user_bidi_turn_id"] === "string" ? record["user_bidi_turn_id"] : undefined);
|
|
3888
4024
|
this.callbacks.onEvent?.(realtimeEventDetails(this.callId, event, input, Boolean(delegated)));
|
|
3889
4025
|
if (!delegated)
|
|
3890
4026
|
return;
|
|
@@ -4249,6 +4385,8 @@ class CodexRealtimeConversation {
|
|
|
4249
4385
|
this.fail(error);
|
|
4250
4386
|
return;
|
|
4251
4387
|
}
|
|
4388
|
+
if (event.type === "audio_diagnostics")
|
|
4389
|
+
this.callbacks.onAudioDiagnostics?.(event.diagnostics);
|
|
4252
4390
|
if (event.type === "playback_activity") {
|
|
4253
4391
|
if (this.turns instanceof PublicRealtimeTurns)
|
|
4254
4392
|
this.turns.audioActivity();
|
|
@@ -4868,6 +5006,11 @@ function validSdp(value) {
|
|
|
4868
5006
|
}
|
|
4869
5007
|
function readPeerEvent(value) {
|
|
4870
5008
|
if (isRecord(value)) {
|
|
5009
|
+
if (value["type"] === "audio_diagnostics") {
|
|
5010
|
+
const event = parseVoiceHelperEvent(value);
|
|
5011
|
+
if (event.type === "audio_diagnostics")
|
|
5012
|
+
return event;
|
|
5013
|
+
}
|
|
4871
5014
|
if (value["type"] === "playback_activity")
|
|
4872
5015
|
return { type: "playback_activity" };
|
|
4873
5016
|
if (value["type"] === "state" && typeof value["state"] === "string" && value["state"].length <= 128)
|
|
@@ -5338,10 +5481,16 @@ import { WebSocket as WebSocket4 } from "ws";
|
|
|
5338
5481
|
// packages/gippity-voice/src/voice/lan/browser-connections.ts
|
|
5339
5482
|
import { WebSocket as WebSocket2 } from "ws";
|
|
5340
5483
|
var MAX_SOCKET_AUDIO_BACKLOG = 24000 * 2;
|
|
5484
|
+
var AUDIO_REPORT_INTERVAL_MS = 5000;
|
|
5341
5485
|
|
|
5342
5486
|
class LanVoiceBrowserConnections {
|
|
5343
5487
|
eventResponses = new Map;
|
|
5344
5488
|
audioSockets = new Map;
|
|
5489
|
+
audioReports = new WeakMap;
|
|
5490
|
+
onAudioDiagnostics;
|
|
5491
|
+
constructor(onAudioDiagnostics) {
|
|
5492
|
+
this.onAudioDiagnostics = onAudioDiagnostics;
|
|
5493
|
+
}
|
|
5345
5494
|
connectEvents(clientId, response, closed) {
|
|
5346
5495
|
if (closed) {
|
|
5347
5496
|
response.end();
|
|
@@ -5362,12 +5511,15 @@ class LanVoiceBrowserConnections {
|
|
|
5362
5511
|
}
|
|
5363
5512
|
const previous = this.audioSockets.get(clientId);
|
|
5364
5513
|
this.audioSockets.set(clientId, socket);
|
|
5365
|
-
if (previous)
|
|
5514
|
+
if (previous) {
|
|
5515
|
+
this.finishAudioReport(previous, "replaced");
|
|
5366
5516
|
callbacks.onReplaced(previous);
|
|
5517
|
+
}
|
|
5367
5518
|
previous?.close(4001, "replaced");
|
|
5368
5519
|
socket.send(JSON.stringify({ type: "connected" }));
|
|
5369
5520
|
socket.on("message", callbacks.onMessage);
|
|
5370
5521
|
socket.once("close", () => {
|
|
5522
|
+
this.finishAudioReport(socket, "closed");
|
|
5371
5523
|
if (this.audioSockets.get(clientId) === socket)
|
|
5372
5524
|
this.audioSockets.delete(clientId);
|
|
5373
5525
|
callbacks.onClose();
|
|
@@ -5394,8 +5546,69 @@ class LanVoiceBrowserConnections {
|
|
|
5394
5546
|
this.sendControl(clientId, value);
|
|
5395
5547
|
}
|
|
5396
5548
|
sendAudio(socket, pcm) {
|
|
5397
|
-
if (socket.readyState
|
|
5549
|
+
if (socket.readyState !== WebSocket2.OPEN)
|
|
5550
|
+
return;
|
|
5551
|
+
const now = performance.now();
|
|
5552
|
+
const report = this.audioReports.get(socket) ?? {
|
|
5553
|
+
startedAt: now,
|
|
5554
|
+
lastArrivalAt: now,
|
|
5555
|
+
sentFrames: 0,
|
|
5556
|
+
sentBytes: 0,
|
|
5557
|
+
droppedFrames: 0,
|
|
5558
|
+
droppedBytes: 0,
|
|
5559
|
+
maxSocketBacklogBytes: 0,
|
|
5560
|
+
sourcePcmArrivalGapMaxMs: 0
|
|
5561
|
+
};
|
|
5562
|
+
this.audioReports.set(socket, report);
|
|
5563
|
+
report.sourcePcmArrivalGapMaxMs = Math.max(report.sourcePcmArrivalGapMaxMs, now - report.lastArrivalAt);
|
|
5564
|
+
report.lastArrivalAt = now;
|
|
5565
|
+
report.maxSocketBacklogBytes = Math.max(report.maxSocketBacklogBytes, socket.bufferedAmount);
|
|
5566
|
+
if (socket.bufferedAmount < MAX_SOCKET_AUDIO_BACKLOG) {
|
|
5398
5567
|
socket.send(pcm);
|
|
5568
|
+
report.maxSocketBacklogBytes = Math.max(report.maxSocketBacklogBytes, socket.bufferedAmount);
|
|
5569
|
+
report.sentFrames++;
|
|
5570
|
+
report.sentBytes += pcm.byteLength;
|
|
5571
|
+
} else {
|
|
5572
|
+
report.droppedFrames++;
|
|
5573
|
+
report.droppedBytes += pcm.byteLength;
|
|
5574
|
+
}
|
|
5575
|
+
if (!report.timer) {
|
|
5576
|
+
report.startedAt = now;
|
|
5577
|
+
report.timer = setTimeout(() => this.flushAudioReport(socket, "interval"), AUDIO_REPORT_INTERVAL_MS);
|
|
5578
|
+
report.timer.unref();
|
|
5579
|
+
}
|
|
5580
|
+
}
|
|
5581
|
+
flushAudioReport(socket, reason) {
|
|
5582
|
+
const report = this.audioReports.get(socket);
|
|
5583
|
+
if (!report)
|
|
5584
|
+
return;
|
|
5585
|
+
clearTimeout(report.timer);
|
|
5586
|
+
report.timer = undefined;
|
|
5587
|
+
if (report.sentFrames + report.droppedFrames === 0)
|
|
5588
|
+
return;
|
|
5589
|
+
const diagnostics = {
|
|
5590
|
+
source: "lan-playback",
|
|
5591
|
+
reason,
|
|
5592
|
+
windowMs: performance.now() - report.startedAt,
|
|
5593
|
+
sentFrames: report.sentFrames,
|
|
5594
|
+
sentBytes: report.sentBytes,
|
|
5595
|
+
droppedFrames: report.droppedFrames,
|
|
5596
|
+
droppedBytes: report.droppedBytes,
|
|
5597
|
+
maxSocketBacklogBytes: report.maxSocketBacklogBytes,
|
|
5598
|
+
sourcePcmArrivalGapMaxMs: report.sourcePcmArrivalGapMaxMs,
|
|
5599
|
+
sourcePcmArrivalGapIncludesPauses: true
|
|
5600
|
+
};
|
|
5601
|
+
report.sentFrames = report.sentBytes = report.droppedFrames = report.droppedBytes = 0;
|
|
5602
|
+
report.maxSocketBacklogBytes = report.sourcePcmArrivalGapMaxMs = 0;
|
|
5603
|
+
try {
|
|
5604
|
+
this.onAudioDiagnostics?.(diagnostics);
|
|
5605
|
+
} catch {
|
|
5606
|
+
console.warn("[voice audio] LAN playback diagnostics could not be saved");
|
|
5607
|
+
}
|
|
5608
|
+
}
|
|
5609
|
+
finishAudioReport(socket, reason) {
|
|
5610
|
+
this.flushAudioReport(socket, reason);
|
|
5611
|
+
this.audioReports.delete(socket);
|
|
5399
5612
|
}
|
|
5400
5613
|
sendAudioControl(socket, value) {
|
|
5401
5614
|
if (socket.readyState === WebSocket2.OPEN)
|
|
@@ -5410,6 +5623,7 @@ class LanVoiceBrowserConnections {
|
|
|
5410
5623
|
}
|
|
5411
5624
|
close(failures) {
|
|
5412
5625
|
for (const socket of this.audioSockets.values()) {
|
|
5626
|
+
this.finishAudioReport(socket, "server-closed");
|
|
5413
5627
|
try {
|
|
5414
5628
|
socket.terminate();
|
|
5415
5629
|
} catch (error) {
|
|
@@ -5568,6 +5782,7 @@ class LanVoiceBrowserSession {
|
|
|
5568
5782
|
state = { type: "idle" };
|
|
5569
5783
|
operation = Promise.resolve();
|
|
5570
5784
|
conversationOwnerId;
|
|
5785
|
+
stopCompletion;
|
|
5571
5786
|
conversationSpeakerSuppressed = false;
|
|
5572
5787
|
microphoneLevel;
|
|
5573
5788
|
constructor(options, connections) {
|
|
@@ -5599,27 +5814,41 @@ class LanVoiceBrowserSession {
|
|
|
5599
5814
|
}
|
|
5600
5815
|
}
|
|
5601
5816
|
release(clientId, socket, terminateConversation = false) {
|
|
5817
|
+
if (terminateConversation && this.stopCompletion?.clientId === clientId)
|
|
5818
|
+
return this.stopCompletion.promise;
|
|
5819
|
+
const starting = this.state;
|
|
5820
|
+
const ownsConversation = this.conversationOwnerId === clientId && (!socket || (starting.type === "active" || starting.type === "starting") && starting.socket === socket);
|
|
5602
5821
|
this.releaseStarting(clientId, socket);
|
|
5603
|
-
|
|
5604
|
-
|
|
5605
|
-
|
|
5606
|
-
|
|
5607
|
-
|
|
5608
|
-
if (ownsActive && active.mode === "conversation")
|
|
5609
|
-
this.microphoneLevel.reset();
|
|
5610
|
-
if (terminateConversation && this.conversationOwnerId === clientId) {
|
|
5611
|
-
this.conversationOwnerId = undefined;
|
|
5612
|
-
await this.options.onConversationActivity(false);
|
|
5822
|
+
let completion;
|
|
5823
|
+
if (terminateConversation && starting.type === "starting" && starting.mode === "conversation" && starting.clientId === clientId && (!socket || starting.socket === socket)) {
|
|
5824
|
+
this.conversationOwnerId = undefined;
|
|
5825
|
+
completion = Promise.resolve().then(() => this.options.onConversationActivity(false)).then(() => true);
|
|
5826
|
+
this.operation = Promise.all([this.operation, completion]).then(() => {
|
|
5613
5827
|
return;
|
|
5614
|
-
}
|
|
5615
|
-
if (!ownsActive)
|
|
5828
|
+
}, () => {
|
|
5616
5829
|
return;
|
|
5617
|
-
|
|
5618
|
-
|
|
5619
|
-
|
|
5620
|
-
|
|
5621
|
-
|
|
5622
|
-
|
|
5830
|
+
});
|
|
5831
|
+
} else {
|
|
5832
|
+
completion = this.enqueue(async () => {
|
|
5833
|
+
const active = this.state;
|
|
5834
|
+
const ownsActive = active.type === "active" && active.clientId === clientId && (!socket || active.socket === socket);
|
|
5835
|
+
if (ownsActive)
|
|
5836
|
+
this.state = { type: "idle" };
|
|
5837
|
+
if (ownsActive && active.mode === "conversation")
|
|
5838
|
+
this.microphoneLevel.reset();
|
|
5839
|
+
if (terminateConversation && this.conversationOwnerId === clientId && (!socket || ownsActive)) {
|
|
5840
|
+
this.conversationOwnerId = undefined;
|
|
5841
|
+
await this.options.onConversationActivity(false);
|
|
5842
|
+
return true;
|
|
5843
|
+
}
|
|
5844
|
+
if (ownsActive && active.mode === "dictation")
|
|
5845
|
+
await this.options.finishDictation(clientId);
|
|
5846
|
+
return false;
|
|
5847
|
+
});
|
|
5848
|
+
}
|
|
5849
|
+
if (terminateConversation && ownsConversation)
|
|
5850
|
+
this.stopCompletion = { clientId, promise: completion };
|
|
5851
|
+
return completion;
|
|
5623
5852
|
}
|
|
5624
5853
|
releaseStarting(clientId, socket) {
|
|
5625
5854
|
const starting = this.state;
|
|
@@ -5668,8 +5897,10 @@ class LanVoiceBrowserSession {
|
|
|
5668
5897
|
return;
|
|
5669
5898
|
const starting = { type: "starting", clientId, socket, mode };
|
|
5670
5899
|
this.state = starting;
|
|
5671
|
-
if (mode === "conversation")
|
|
5900
|
+
if (mode === "conversation") {
|
|
5901
|
+
this.stopCompletion = undefined;
|
|
5672
5902
|
this.conversationOwnerId = clientId;
|
|
5903
|
+
}
|
|
5673
5904
|
try {
|
|
5674
5905
|
if (mode === "conversation")
|
|
5675
5906
|
await this.options.ensureConversation(takeoverToken);
|
|
@@ -5741,7 +5972,7 @@ class LanVoiceBrowserSession {
|
|
|
5741
5972
|
}
|
|
5742
5973
|
this.state = { type: "closed" };
|
|
5743
5974
|
const failures = [];
|
|
5744
|
-
if (active.type === "active" && active.mode === "conversation") {
|
|
5975
|
+
if ((active.type === "active" || active.type === "starting") && active.mode === "conversation") {
|
|
5745
5976
|
try {
|
|
5746
5977
|
this.conversationOwnerId = undefined;
|
|
5747
5978
|
await this.options.onConversationActivity(false);
|
|
@@ -5780,10 +6011,11 @@ class LanVoiceBrowserSession {
|
|
|
5780
6011
|
|
|
5781
6012
|
// packages/gippity-voice/src/voice/lan/browser-clients.ts
|
|
5782
6013
|
class LanVoiceBrowserClients {
|
|
5783
|
-
connections
|
|
6014
|
+
connections;
|
|
5784
6015
|
session;
|
|
5785
6016
|
takeoverAck;
|
|
5786
6017
|
constructor(options) {
|
|
6018
|
+
this.connections = new LanVoiceBrowserConnections(options.onAudioDiagnostics);
|
|
5787
6019
|
this.session = new LanVoiceBrowserSession(options, this.connections);
|
|
5788
6020
|
}
|
|
5789
6021
|
connectEvents(clientId, response) {
|
|
@@ -5793,7 +6025,12 @@ class LanVoiceBrowserClients {
|
|
|
5793
6025
|
this.connections.connectAudio(clientId, socket, this.session.closed, {
|
|
5794
6026
|
onMessage: (data, isBinary) => this.receive(clientId, socket, data, isBinary),
|
|
5795
6027
|
onReplaced: (previous) => this.session.releaseStarting(clientId, previous),
|
|
5796
|
-
onClose: () =>
|
|
6028
|
+
onClose: () => {
|
|
6029
|
+
this.session.release(clientId, socket).catch((error) => this.connections.sendControl(clientId, {
|
|
6030
|
+
type: "error",
|
|
6031
|
+
message: errorMessage(error)
|
|
6032
|
+
}));
|
|
6033
|
+
}
|
|
5797
6034
|
});
|
|
5798
6035
|
}
|
|
5799
6036
|
sendControl(clientId, value) {
|
|
@@ -5847,7 +6084,7 @@ class LanVoiceBrowserClients {
|
|
|
5847
6084
|
this.session.resetConversationInputLevel();
|
|
5848
6085
|
}
|
|
5849
6086
|
release(clientId, socket, terminateConversation = false) {
|
|
5850
|
-
this.session.release(clientId, socket, terminateConversation);
|
|
6087
|
+
return this.session.release(clientId, socket, terminateConversation);
|
|
5851
6088
|
}
|
|
5852
6089
|
heartbeat() {
|
|
5853
6090
|
this.connections.heartbeat();
|
|
@@ -5876,7 +6113,7 @@ class LanVoiceBrowserClients {
|
|
|
5876
6113
|
} else if (message.type === "finish") {
|
|
5877
6114
|
this.session.finish(clientId, socket, message.draft, message.revision, message.selection).catch((error) => this.sendSocketError(socket, error));
|
|
5878
6115
|
} else if (message.type === "release") {
|
|
5879
|
-
this.session.release(clientId, socket, true);
|
|
6116
|
+
this.session.release(clientId, socket, true).catch((error) => this.sendSocketError(socket, error));
|
|
5880
6117
|
} else if (message.type === "mute") {
|
|
5881
6118
|
this.session.mute(clientId, socket, message.muted);
|
|
5882
6119
|
} else {
|
|
@@ -5929,10 +6166,10 @@ class LanHostRealtimePeer {
|
|
|
5929
6166
|
}
|
|
5930
6167
|
async start(_config) {
|
|
5931
6168
|
await this.helper.start();
|
|
5932
|
-
if (this.helper.protocolVersion !==
|
|
6169
|
+
if (this.helper.protocolVersion !== 8) {
|
|
5933
6170
|
const actualVersion = this.helper.protocolVersion ?? "unknown";
|
|
5934
6171
|
await this.helper.close();
|
|
5935
|
-
throw new Error(`Incompatible Codex voice helper protocol ${actualVersion}; expected
|
|
6172
|
+
throw new Error(`Incompatible Codex voice helper protocol ${actualVersion}; expected 8`);
|
|
5936
6173
|
}
|
|
5937
6174
|
const offer = deferred();
|
|
5938
6175
|
const removeEvent = this.helper.onEvent((event) => {
|
|
@@ -5989,7 +6226,7 @@ class LanHostRealtimePeer {
|
|
|
5989
6226
|
}
|
|
5990
6227
|
}
|
|
5991
6228
|
function toPeerEvent2(event) {
|
|
5992
|
-
if (event.type === "state" || event.type === "data" || event.type === "error" || event.type === "playback_activity")
|
|
6229
|
+
if (event.type === "state" || event.type === "data" || event.type === "error" || event.type === "playback_activity" || event.type === "audio_diagnostics")
|
|
5993
6230
|
return event;
|
|
5994
6231
|
return;
|
|
5995
6232
|
}
|
|
@@ -7365,6 +7602,7 @@ async function startGippityLanServer(options) {
|
|
|
7365
7602
|
}
|
|
7366
7603
|
});
|
|
7367
7604
|
clients = new LanVoiceBrowserClients({
|
|
7605
|
+
...options.onAudioDiagnostics ? { onAudioDiagnostics: options.onAudioDiagnostics } : {},
|
|
7368
7606
|
async ensureConversation(takeoverToken) {
|
|
7369
7607
|
if (closing)
|
|
7370
7608
|
throw new Error("LAN server is closing");
|
|
@@ -7430,7 +7668,7 @@ async function startGippityLanServer(options) {
|
|
|
7430
7668
|
async onConversationActivity(active) {
|
|
7431
7669
|
if (releasing)
|
|
7432
7670
|
return;
|
|
7433
|
-
if (!active && browserVoice) {
|
|
7671
|
+
if (!active && (browserVoice || browserStarting)) {
|
|
7434
7672
|
browserVoice = false;
|
|
7435
7673
|
browserPeer = undefined;
|
|
7436
7674
|
await controls.stopVoice();
|
|
@@ -7665,7 +7903,7 @@ data: {}
|
|
|
7665
7903
|
return send(response, 200, { ok: true });
|
|
7666
7904
|
}
|
|
7667
7905
|
if (path === "/api/stop") {
|
|
7668
|
-
clients.release(client, undefined, body["terminateConversation"] === true);
|
|
7906
|
+
await clients.release(client, undefined, body["terminateConversation"] === true);
|
|
7669
7907
|
return send(response, 200, { ok: true });
|
|
7670
7908
|
}
|
|
7671
7909
|
if (path === "/api/rpc") {
|
|
@@ -7785,28 +8023,21 @@ data: {}
|
|
|
7785
8023
|
else if (event.type === "lifecycle" && !event.active)
|
|
7786
8024
|
clients.broadcastControl({ type: "stop", reason: "voice-ended" });
|
|
7787
8025
|
},
|
|
7788
|
-
|
|
7789
|
-
if (
|
|
7790
|
-
|
|
7791
|
-
|
|
7792
|
-
|
|
7793
|
-
|
|
7794
|
-
|
|
7795
|
-
}
|
|
8026
|
+
agentState(working) {
|
|
8027
|
+
if (working)
|
|
8028
|
+
activity.working();
|
|
8029
|
+
else if (activity.snapshot().state === "working")
|
|
8030
|
+
activity.settled();
|
|
8031
|
+
},
|
|
8032
|
+
agentEntry(entry) {
|
|
7796
8033
|
clients.broadcastControl({
|
|
7797
8034
|
type: "durable.event",
|
|
7798
|
-
event:
|
|
7799
|
-
data: remoteJsonValue(
|
|
8035
|
+
event: "message_end",
|
|
8036
|
+
data: remoteJsonValue({ type: "message_end", entry })
|
|
7800
8037
|
});
|
|
7801
|
-
|
|
7802
|
-
|
|
7803
|
-
|
|
7804
|
-
activity.settled();
|
|
7805
|
-
if (event.type === "message_end") {
|
|
7806
|
-
const text = boundedAssistantText((event.entry.model ?? []).flatMap((message) => message.role === "assistant" && message.stopReason !== "toolUse" ? message.content : []));
|
|
7807
|
-
if (text)
|
|
7808
|
-
activity.settled(text);
|
|
7809
|
-
}
|
|
8038
|
+
const text = boundedAssistantText((entry.model ?? []).flatMap((message) => message.role === "assistant" && message.stopReason !== "toolUse" ? message.content : []));
|
|
8039
|
+
if (text)
|
|
8040
|
+
activity.settled(text);
|
|
7810
8041
|
}
|
|
7811
8042
|
};
|
|
7812
8043
|
}
|
|
@@ -8086,7 +8317,7 @@ import {
|
|
|
8086
8317
|
uuidv7,
|
|
8087
8318
|
validateToolArguments as validateToolArguments2
|
|
8088
8319
|
} from "@earendil-works/pi-ai";
|
|
8089
|
-
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
|
|
8320
|
+
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";
|
|
8090
8321
|
async function rewriteTaskDelegation(models, voice, turn, signal) {
|
|
8091
8322
|
let failure;
|
|
8092
8323
|
for (let attempt = 0;attempt < 4; attempt++) {
|
|
@@ -8388,11 +8619,38 @@ function createGippityVoice(options) {
|
|
|
8388
8619
|
config = normalizeGippityControlConfig(await options.settings.read());
|
|
8389
8620
|
saved = true;
|
|
8390
8621
|
}
|
|
8391
|
-
events = await
|
|
8392
|
-
|
|
8393
|
-
|
|
8394
|
-
|
|
8395
|
-
|
|
8622
|
+
events = await observeVoiceAgent(conversation, context, {
|
|
8623
|
+
state(value) {
|
|
8624
|
+
if (transferred)
|
|
8625
|
+
return;
|
|
8626
|
+
const changed = working !== value;
|
|
8627
|
+
working = value;
|
|
8628
|
+
lan?.agentState(working);
|
|
8629
|
+
if (!changed)
|
|
8630
|
+
return;
|
|
8631
|
+
const type = working ? "run_start" : "run_end";
|
|
8632
|
+
for (const listener of listeners)
|
|
8633
|
+
listener(type, { type });
|
|
8634
|
+
if (working) {
|
|
8635
|
+
session?.agentStarted();
|
|
8636
|
+
session?.resumeAgentWork();
|
|
8637
|
+
} else
|
|
8638
|
+
session?.settleAgentTurn();
|
|
8639
|
+
},
|
|
8640
|
+
entry: handleEntry,
|
|
8641
|
+
partial(delta, active) {
|
|
8642
|
+
if (transferred)
|
|
8643
|
+
return;
|
|
8644
|
+
if (delta)
|
|
8645
|
+
session?.streamAgentDelta(delta);
|
|
8646
|
+
if (active)
|
|
8647
|
+
session?.resumeAgentWork();
|
|
8648
|
+
},
|
|
8649
|
+
compacting: (reason) => session?.announceCompactionStart(reason),
|
|
8650
|
+
failed(error) {
|
|
8651
|
+
report(error);
|
|
8652
|
+
enqueue(stopSession).catch(report);
|
|
8653
|
+
}
|
|
8396
8654
|
});
|
|
8397
8655
|
ready?.resolve();
|
|
8398
8656
|
await new Promise((resolve) => {
|
|
@@ -8576,6 +8834,7 @@ function createGippityVoice(options) {
|
|
|
8576
8834
|
delegations.push(turn, callId);
|
|
8577
8835
|
},
|
|
8578
8836
|
onEvent: (event) => publish({ type: "voice-event", event }),
|
|
8837
|
+
onAudioDiagnostics: (diagnostics) => publish({ type: "audio-diagnostics", diagnostics }),
|
|
8579
8838
|
rewriteDelegation: (turn, signal) => rewriteTaskDelegation(requireRuntime().runtime.models, settings.voice, turn, signal),
|
|
8580
8839
|
onDelegationError: report,
|
|
8581
8840
|
onEndCall: () => {
|
|
@@ -8750,74 +9009,35 @@ ${tail}
|
|
|
8750
9009
|
releaseDelegations();
|
|
8751
9010
|
}
|
|
8752
9011
|
}
|
|
8753
|
-
function
|
|
9012
|
+
function handleEntry(entry) {
|
|
8754
9013
|
if (transferred)
|
|
8755
9014
|
return;
|
|
8756
|
-
const notification = event.type === "snapshot" ? {
|
|
8757
|
-
type: "snapshot",
|
|
8758
|
-
working: Boolean(event.run),
|
|
8759
|
-
model: event.agent.model ?? null
|
|
8760
|
-
} : event;
|
|
8761
9015
|
for (const listener of listeners)
|
|
8762
|
-
listener(
|
|
8763
|
-
lan?.
|
|
8764
|
-
if (
|
|
8765
|
-
|
|
8766
|
-
|
|
8767
|
-
working = true;
|
|
8768
|
-
if (event.type === "run_end")
|
|
8769
|
-
working = false;
|
|
8770
|
-
if (event.type === "run_start") {
|
|
8771
|
-
session?.agentStarted();
|
|
8772
|
-
session?.resumeAgentWork();
|
|
8773
|
-
}
|
|
8774
|
-
if (event.type === "run_end")
|
|
8775
|
-
session?.settleAgentTurn();
|
|
8776
|
-
if (event.type === "message_update") {
|
|
8777
|
-
for (const change of event.changes)
|
|
8778
|
-
if (change.type === "text_delta")
|
|
8779
|
-
session?.streamAgentDelta(change.delta);
|
|
8780
|
-
else if (change.type === "thinking_start" || change.type === "toolcall_start")
|
|
8781
|
-
session?.resumeAgentWork();
|
|
8782
|
-
}
|
|
8783
|
-
if (event.type === "message_end" && event.entry.kind === "gippity.voice.delegation" && isRecord(event.entry.data) && typeof event.entry.data["id"] === "string")
|
|
8784
|
-
if (event.entry.data["callId"] === activeCallId)
|
|
8785
|
-
session?.activateDelegation(event.entry.data["id"]);
|
|
9016
|
+
listener("message_end", { type: "message_end", entry });
|
|
9017
|
+
lan?.agentEntry(entry);
|
|
9018
|
+
if (entry.kind === "gippity.voice.delegation" && isRecord(entry.data) && typeof entry.data["id"] === "string")
|
|
9019
|
+
if (entry.data["callId"] === activeCallId)
|
|
9020
|
+
session?.activateDelegation(entry.data["id"]);
|
|
8786
9021
|
else
|
|
8787
9022
|
session?.agentStarted();
|
|
8788
|
-
|
|
8789
|
-
|
|
8790
|
-
|
|
8791
|
-
const text = typeof message.content === "string" ? message.content : message.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
9023
|
+
for (const message of entry.model ?? []) {
|
|
9024
|
+
if (message.role === "user" && UserEntry.is(entry)) {
|
|
9025
|
+
const text = typeof message.content === "string" ? message.content : message.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
8792
9026
|
`);
|
|
8793
|
-
|
|
8794
|
-
|
|
8795
|
-
}
|
|
8796
|
-
if (message.role !== "assistant")
|
|
8797
|
-
continue;
|
|
8798
|
-
const text = message.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
8799
|
-
`);
|
|
8800
|
-
if (message.stopReason === "toolUse") {
|
|
8801
|
-
session?.agentProgress(text, activeConfig?.voice.forwardReasoningSummaries ? completedVoiceReasoningSummary(message) : undefined);
|
|
8802
|
-
} else
|
|
8803
|
-
session?.agentResult(text, message.stopReason);
|
|
9027
|
+
session?.piInput(text, working ? "steer" : undefined);
|
|
9028
|
+
session?.piUserMessage(message);
|
|
8804
9029
|
}
|
|
8805
|
-
|
|
8806
|
-
|
|
8807
|
-
|
|
8808
|
-
|
|
8809
|
-
|
|
8810
|
-
|
|
8811
|
-
|
|
8812
|
-
|
|
8813
|
-
const submission = await harness.submission(result.submissionId, runtimeContext);
|
|
8814
|
-
if (!submission || (await submission.wait(runtimeContext)).status !== "done")
|
|
8815
|
-
return;
|
|
8816
|
-
} else if (result.entryId === undefined)
|
|
8817
|
-
return;
|
|
8818
|
-
return enqueue(refreshContext);
|
|
8819
|
-
}).catch(report);
|
|
9030
|
+
if (message.role !== "assistant")
|
|
9031
|
+
continue;
|
|
9032
|
+
const text = message.content.flatMap((part) => part.type === "text" ? [part.text] : []).join(`
|
|
9033
|
+
`);
|
|
9034
|
+
if (message.stopReason === "toolUse") {
|
|
9035
|
+
session?.agentProgress(text, activeConfig?.voice.forwardReasoningSummaries ? completedVoiceReasoningSummary(message) : undefined);
|
|
9036
|
+
} else
|
|
9037
|
+
session?.agentResult(text, message.stopReason);
|
|
8820
9038
|
}
|
|
9039
|
+
if (CompactionEntry.is(entry) && activeConfig?.voice.refreshRealtimeAfterCompaction)
|
|
9040
|
+
enqueue(refreshContext).catch(report);
|
|
8821
9041
|
}
|
|
8822
9042
|
async function ensureLan(context, takeover) {
|
|
8823
9043
|
await ensureService(context);
|
|
@@ -8841,11 +9061,12 @@ ${tail}
|
|
|
8841
9061
|
context: runtimeContext,
|
|
8842
9062
|
resolveAuth: () => resolveDictationAuth(runtime.signal),
|
|
8843
9063
|
remoteApps,
|
|
9064
|
+
onAudioDiagnostics: (diagnostics) => publish({ type: "lan-playback-diagnostics", diagnostics }),
|
|
8844
9065
|
...takeover ? { takeoverToken: takeover.token } : {},
|
|
8845
9066
|
startConversation: (factory, token) => startVoice(runtimeContext, factory, true, arrival.consume(token))
|
|
8846
9067
|
});
|
|
8847
9068
|
if (events)
|
|
8848
|
-
lan.
|
|
9069
|
+
lan.agentState(working);
|
|
8849
9070
|
});
|
|
8850
9071
|
return lan.urls;
|
|
8851
9072
|
}
|
|
@@ -9451,7 +9672,7 @@ ${continuity}`
|
|
|
9451
9672
|
event: "binding",
|
|
9452
9673
|
data: {
|
|
9453
9674
|
conversationId: runtime.conversationId,
|
|
9454
|
-
model: events?.
|
|
9675
|
+
model: events?.view.docs[AgentDoc.definition.kind]?.model ?? null
|
|
9455
9676
|
}
|
|
9456
9677
|
});
|
|
9457
9678
|
link.notify({
|