@jambonz/sdk 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/index.d.cts +2 -2
- package/dist/client/index.d.ts +2 -2
- package/dist/harness/index.cjs +843 -0
- package/dist/harness/index.cjs.map +1 -0
- package/dist/harness/index.d.cts +503 -0
- package/dist/harness/index.d.ts +503 -0
- package/dist/harness/index.js +832 -0
- package/dist/harness/index.js.map +1 -0
- package/dist/index.cjs +14 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -1
- package/dist/{rest-0m9Hgjmn.d.cts → rest-B-7QX4TM.d.cts} +1 -1
- package/dist/{rest-CqS2POQv.d.ts → rest-r1vLsPCg.d.ts} +1 -1
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/{verb-builder-C1id83_f.d.cts → verb-builder-UVx6Exh1.d.ts} +3 -1
- package/dist/{verb-builder-YFevoXCs.d.ts → verb-builder-tc7P_MTy.d.cts} +3 -1
- package/dist/{verbs-DQ6zJ0bn.d.cts → verbs-D461pHTq.d.cts} +52 -6
- package/dist/{verbs-DQ6zJ0bn.d.ts → verbs-D461pHTq.d.ts} +52 -6
- package/dist/webhook/index.cjs +6 -0
- package/dist/webhook/index.cjs.map +1 -1
- package/dist/webhook/index.d.cts +2 -2
- package/dist/webhook/index.d.ts +2 -2
- package/dist/webhook/index.js +6 -0
- package/dist/webhook/index.js.map +1 -1
- package/dist/websocket/index.cjs +14 -0
- package/dist/websocket/index.cjs.map +1 -1
- package/dist/websocket/index.d.cts +5 -3
- package/dist/websocket/index.d.ts +5 -3
- package/dist/websocket/index.js +14 -0
- package/dist/websocket/index.js.map +1 -1
- package/examples/harness/README.md +73 -0
- package/examples/harness/harness.ts +99 -0
- package/examples/harness/package.json +21 -0
- package/examples/harness/tsconfig.json +12 -0
- package/examples/openai-realtime/ws-app.ts +18 -14
- package/package.json +13 -2
|
@@ -0,0 +1,843 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var http = require('http');
|
|
4
|
+
var crypto = require('crypto');
|
|
5
|
+
var ws = require('ws');
|
|
6
|
+
var pino = require('pino');
|
|
7
|
+
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
|
|
10
|
+
var http__default = /*#__PURE__*/_interopDefault(http);
|
|
11
|
+
|
|
12
|
+
// src/harness/server.ts
|
|
13
|
+
|
|
14
|
+
// src/harness/sentences.ts
|
|
15
|
+
var ABBREVIATIONS = /* @__PURE__ */ new Set(["mr", "mrs", "ms", "dr", "st", "vs", "jr", "sr", "prof", "inc", "ltd", "e.g", "i.e", "etc", "no"]);
|
|
16
|
+
var SentenceSplitter = class {
|
|
17
|
+
buffer = "";
|
|
18
|
+
/** Add streamed text; returns any sentences it completed (each with its trailing whitespace). */
|
|
19
|
+
push(text) {
|
|
20
|
+
this.buffer += text;
|
|
21
|
+
const out = [];
|
|
22
|
+
let start = 0;
|
|
23
|
+
const re = /([.!?]+["')\]]*)(\s+)|(\n+)/g;
|
|
24
|
+
let m;
|
|
25
|
+
while (m = re.exec(this.buffer)) {
|
|
26
|
+
const end = m.index + m[0].length;
|
|
27
|
+
if (m[1]) {
|
|
28
|
+
const words = this.buffer.slice(start, m.index).trim().split(/\s+/);
|
|
29
|
+
const last = (words[words.length - 1] ?? "").toLowerCase();
|
|
30
|
+
if (m[1] === "." && ABBREVIATIONS.has(last)) continue;
|
|
31
|
+
}
|
|
32
|
+
const sentence = this.buffer.slice(start, end);
|
|
33
|
+
if (sentence.trim()) out.push(sentence);
|
|
34
|
+
start = end;
|
|
35
|
+
}
|
|
36
|
+
this.buffer = this.buffer.slice(start);
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/** Returns whatever is left (the final, possibly unterminated, sentence) and clears it. */
|
|
40
|
+
flush() {
|
|
41
|
+
const rest = this.buffer;
|
|
42
|
+
this.buffer = "";
|
|
43
|
+
return rest;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/harness/call.ts
|
|
48
|
+
var COMMAND_TIMEOUT_MS = 3e4;
|
|
49
|
+
var TOOL_TIMEOUT_MS = 6e4;
|
|
50
|
+
var SPOKEN_WAIT_MS = 15e3;
|
|
51
|
+
var MAX_TRACKED_REPLIES = 20;
|
|
52
|
+
var MAX_TURN_RECORD_BYTES = 16 * 1024;
|
|
53
|
+
var SILENCE_REPROMPT = "Are you still there?";
|
|
54
|
+
var SILENCE_GOODBYE = "I haven't heard from you, so I'll end the call now. Goodbye.";
|
|
55
|
+
var DEFAULT_ERROR_REPLY = "Sorry, I'm having trouble right now. Could you say that again?";
|
|
56
|
+
var GREETING_PROMPT = "(The call has just connected. Greet the caller.)";
|
|
57
|
+
var now = () => performance.now();
|
|
58
|
+
var ms = (since) => Math.round(performance.now() - since);
|
|
59
|
+
var TurnRecorder = class {
|
|
60
|
+
record = {};
|
|
61
|
+
step(name, took, result, detail) {
|
|
62
|
+
(this.record.steps ??= []).push({ name, ms: took, ...result ? { result } : {}, ...detail ? { detail } : {} });
|
|
63
|
+
}
|
|
64
|
+
modelCall(call) {
|
|
65
|
+
(this.record.model_calls ??= []).push(call);
|
|
66
|
+
}
|
|
67
|
+
ownTool(name, ok, took) {
|
|
68
|
+
(this.record.own_tools ??= []).push({ name, ok, ms: took });
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var ReplyWriter = class {
|
|
72
|
+
constructor(call, replyId, turnId, signal, first, check) {
|
|
73
|
+
this.call = call;
|
|
74
|
+
this.replyId = replyId;
|
|
75
|
+
this.turnId = turnId;
|
|
76
|
+
this.signal = signal;
|
|
77
|
+
this.first = first;
|
|
78
|
+
this.check = check;
|
|
79
|
+
}
|
|
80
|
+
call;
|
|
81
|
+
replyId;
|
|
82
|
+
turnId;
|
|
83
|
+
signal;
|
|
84
|
+
first;
|
|
85
|
+
check;
|
|
86
|
+
started = false;
|
|
87
|
+
stopped = false;
|
|
88
|
+
/** Everything released to TTS. */
|
|
89
|
+
text = "";
|
|
90
|
+
/** The history message for the current model round, created when its first text is released. */
|
|
91
|
+
segmentMsg;
|
|
92
|
+
splitter = new SentenceSplitter();
|
|
93
|
+
meta;
|
|
94
|
+
checks = { count: 0, ms: 0, replaced: 0 };
|
|
95
|
+
/** Streamed text from the model. Returns false when the reply must stop. */
|
|
96
|
+
async content(text, chunk) {
|
|
97
|
+
if (this.stopped) return false;
|
|
98
|
+
if (chunk) this.meta = chunk;
|
|
99
|
+
if (!this.check) {
|
|
100
|
+
this.release(text, chunk);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
for (const sentence of this.splitter.push(text)) {
|
|
104
|
+
if (!await this.checkAndRelease(sentence)) return false;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
/** End of a model round: check and release the unterminated tail; returns the round's history message, if it said anything. */
|
|
109
|
+
async endSegment() {
|
|
110
|
+
const rest = this.splitter.flush();
|
|
111
|
+
if (rest.trim() && !this.stopped) await this.checkAndRelease(rest);
|
|
112
|
+
const msg = this.segmentMsg;
|
|
113
|
+
this.segmentMsg = void 0;
|
|
114
|
+
return msg;
|
|
115
|
+
}
|
|
116
|
+
/** Speak fixed text (a scripted reply), as one segment. */
|
|
117
|
+
async write(text) {
|
|
118
|
+
await this.content(text);
|
|
119
|
+
await this.endSegment();
|
|
120
|
+
}
|
|
121
|
+
recordChecks(rec) {
|
|
122
|
+
if (!this.checks.count) return;
|
|
123
|
+
const result = this.stopped ? "fail" : this.checks.replaced ? "replaced" : "pass";
|
|
124
|
+
rec.step("output_check", Math.round(this.checks.ms), result, { sentences: this.checks.count });
|
|
125
|
+
}
|
|
126
|
+
async checkAndRelease(sentence) {
|
|
127
|
+
if (this.signal?.aborted) return false;
|
|
128
|
+
const t = now();
|
|
129
|
+
const verdict = await this.check(sentence.trim(), this.call.ctx);
|
|
130
|
+
this.checks.ms += now() - t;
|
|
131
|
+
this.checks.count++;
|
|
132
|
+
if (this.signal?.aborted) return false;
|
|
133
|
+
if (verdict === false) {
|
|
134
|
+
this.stopped = true;
|
|
135
|
+
this.call.log.info({ turn_id: this.turnId, reply_id: this.replyId }, "checkSentence stopped the reply");
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
let out = sentence;
|
|
139
|
+
if (typeof verdict === "string") {
|
|
140
|
+
this.checks.replaced++;
|
|
141
|
+
out = verdict + (sentence.match(/\s*$/)?.[0] ?? "");
|
|
142
|
+
}
|
|
143
|
+
this.release(out);
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
release(text, original) {
|
|
147
|
+
if (!text || this.signal?.aborted) return;
|
|
148
|
+
const chunk = original ?? {
|
|
149
|
+
id: this.meta?.id ?? this.replyId,
|
|
150
|
+
object: "chat.completion.chunk",
|
|
151
|
+
created: this.meta?.created ?? Math.floor(Date.now() / 1e3),
|
|
152
|
+
model: this.meta?.model ?? "harness",
|
|
153
|
+
choices: [{ index: 0, delta: { content: text }, finish_reason: null }]
|
|
154
|
+
};
|
|
155
|
+
this.call.send({
|
|
156
|
+
type: "reply:chunk",
|
|
157
|
+
reply_id: this.replyId,
|
|
158
|
+
...this.turnId !== void 0 ? { turn_id: this.turnId } : {},
|
|
159
|
+
...!this.started ? stripUndefined(this.first) : {},
|
|
160
|
+
chunk
|
|
161
|
+
});
|
|
162
|
+
this.started = true;
|
|
163
|
+
this.text += text;
|
|
164
|
+
if (!this.segmentMsg) {
|
|
165
|
+
this.segmentMsg = { role: "assistant", content: "" };
|
|
166
|
+
this.call.addAssistant(this.replyId, this.segmentMsg);
|
|
167
|
+
}
|
|
168
|
+
this.segmentMsg.content += text;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
function stripUndefined(o) {
|
|
172
|
+
return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== void 0));
|
|
173
|
+
}
|
|
174
|
+
function errMessage(err) {
|
|
175
|
+
return err instanceof Error ? err.message : String(err);
|
|
176
|
+
}
|
|
177
|
+
var HarnessCall = class {
|
|
178
|
+
constructor(session, opts, model, baseLogger) {
|
|
179
|
+
this.session = session;
|
|
180
|
+
this.opts = opts;
|
|
181
|
+
this.model = model;
|
|
182
|
+
this.callSid = session.call_sid;
|
|
183
|
+
this.log = baseLogger.child({ call_sid: this.callSid });
|
|
184
|
+
const call = this;
|
|
185
|
+
this.ctx = {
|
|
186
|
+
callSid: this.callSid,
|
|
187
|
+
session,
|
|
188
|
+
state: this.state,
|
|
189
|
+
history: this.history,
|
|
190
|
+
log: this.log,
|
|
191
|
+
say: (text, o) => call.say(text, o),
|
|
192
|
+
command: (name, args) => call.command(name, args),
|
|
193
|
+
callTool: (name, args) => call.callTool(name, args ?? {}),
|
|
194
|
+
record: (field, value) => {
|
|
195
|
+
if (call.activeRecorder) call.activeRecorder.record[field] = value;
|
|
196
|
+
else call.pendingRecord[field] = value;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
session;
|
|
201
|
+
opts;
|
|
202
|
+
model;
|
|
203
|
+
callSid;
|
|
204
|
+
log;
|
|
205
|
+
ctx;
|
|
206
|
+
history = [];
|
|
207
|
+
state = /* @__PURE__ */ new Map();
|
|
208
|
+
ended = false;
|
|
209
|
+
ws;
|
|
210
|
+
current;
|
|
211
|
+
seq = 0;
|
|
212
|
+
silenceStreak = 0;
|
|
213
|
+
pendingRecord = {};
|
|
214
|
+
activeRecorder;
|
|
215
|
+
replies = /* @__PURE__ */ new Map();
|
|
216
|
+
commands = /* @__PURE__ */ new Map();
|
|
217
|
+
toolCalls = /* @__PURE__ */ new Map();
|
|
218
|
+
spokenWaiters = /* @__PURE__ */ new Map();
|
|
219
|
+
attach(ws) {
|
|
220
|
+
this.ws = ws;
|
|
221
|
+
}
|
|
222
|
+
/** The socket dropped without `session:end`: stop the turn in flight; state is kept for a resume. */
|
|
223
|
+
detach() {
|
|
224
|
+
this.ws = void 0;
|
|
225
|
+
this.abortTurn("socket_closed");
|
|
226
|
+
}
|
|
227
|
+
send(msg) {
|
|
228
|
+
const ws = this.ws;
|
|
229
|
+
if (!ws || ws.readyState !== 1) {
|
|
230
|
+
this.log.warn({ type: msg.type }, "harness socket not open; message dropped");
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
ws.send(JSON.stringify(msg));
|
|
234
|
+
}
|
|
235
|
+
async start() {
|
|
236
|
+
if (this.opts.onSessionStart) await this.opts.onSessionStart(this.session, this.ctx);
|
|
237
|
+
}
|
|
238
|
+
handle(msg) {
|
|
239
|
+
switch (msg.type) {
|
|
240
|
+
case "turn:start":
|
|
241
|
+
void this.runTurn(msg);
|
|
242
|
+
break;
|
|
243
|
+
case "turn:cancel":
|
|
244
|
+
if (this.current?.turnId === msg.turn_id) {
|
|
245
|
+
this.log.debug({ turn_id: msg.turn_id, reason: msg.reason }, "turn cancelled");
|
|
246
|
+
this.abortTurn(msg.reason);
|
|
247
|
+
}
|
|
248
|
+
break;
|
|
249
|
+
case "event":
|
|
250
|
+
void this.handleEvent(msg);
|
|
251
|
+
break;
|
|
252
|
+
case "command:result": {
|
|
253
|
+
const resolve = this.commands.get(msg.command_id);
|
|
254
|
+
if (!resolve) {
|
|
255
|
+
this.log.warn({ command_id: msg.command_id }, "command:result for an unknown command");
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
this.commands.delete(msg.command_id);
|
|
259
|
+
const { type: _t, call_sid: _c, command_id: _id, ...result } = msg;
|
|
260
|
+
if (!msg.ok) this.log.warn({ command_id: msg.command_id, error: msg.error }, "command failed");
|
|
261
|
+
resolve(result);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
case "tool:result": {
|
|
265
|
+
const pending = this.toolCalls.get(msg.call_id);
|
|
266
|
+
if (!pending) {
|
|
267
|
+
this.log.warn({ call_id: msg.call_id }, "tool:result for an unknown tool call");
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
this.toolCalls.delete(msg.call_id);
|
|
271
|
+
if (msg.error !== void 0) pending.reject(new Error(msg.error));
|
|
272
|
+
else pending.resolve(msg.result);
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
case "session:end":
|
|
276
|
+
void this.end(msg);
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
async end(msg) {
|
|
281
|
+
if (this.ended) return;
|
|
282
|
+
this.ended = true;
|
|
283
|
+
this.log.info({ reason: msg.reason, duration_ms: msg.duration_ms }, "session ended");
|
|
284
|
+
this.abortTurn("session_end");
|
|
285
|
+
for (const resolve of this.commands.values()) resolve({ ok: false, error: "session_ended" });
|
|
286
|
+
this.commands.clear();
|
|
287
|
+
for (const p of this.toolCalls.values()) p.reject(new Error("session_ended"));
|
|
288
|
+
this.toolCalls.clear();
|
|
289
|
+
for (const done of this.spokenWaiters.values()) done();
|
|
290
|
+
this.spokenWaiters.clear();
|
|
291
|
+
try {
|
|
292
|
+
if (this.opts.onSessionEnd) await this.opts.onSessionEnd(msg, this.ctx);
|
|
293
|
+
} catch (err) {
|
|
294
|
+
this.log.error({ err }, "onSessionEnd hook threw");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/* ---------------- ctx operations ---------------- */
|
|
298
|
+
say(text, o = {}) {
|
|
299
|
+
const replyId = `u${++this.seq}`;
|
|
300
|
+
const writer = new ReplyWriter(this, replyId, void 0, void 0, { interrupt: o.interrupt, interruptible: o.interruptible }, void 0);
|
|
301
|
+
void writer.write(text);
|
|
302
|
+
this.send({ type: "reply:done", reply_id: replyId });
|
|
303
|
+
return replyId;
|
|
304
|
+
}
|
|
305
|
+
command(name, args = {}) {
|
|
306
|
+
const commandId = `c${++this.seq}`;
|
|
307
|
+
if (this.ended) return Promise.resolve({ ok: false, error: "session_ended" });
|
|
308
|
+
return new Promise((resolve) => {
|
|
309
|
+
const timer = setTimeout(() => {
|
|
310
|
+
if (this.commands.delete(commandId)) {
|
|
311
|
+
this.log.warn({ command_id: commandId, name }, "no command:result within timeout");
|
|
312
|
+
resolve({ ok: false, error: "timeout" });
|
|
313
|
+
}
|
|
314
|
+
}, COMMAND_TIMEOUT_MS);
|
|
315
|
+
this.commands.set(commandId, (r) => {
|
|
316
|
+
clearTimeout(timer);
|
|
317
|
+
resolve(r);
|
|
318
|
+
});
|
|
319
|
+
this.log.info({ command_id: commandId, name }, "sending command");
|
|
320
|
+
this.send({ type: "command", command_id: commandId, name, args });
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
callTool(name, args, turnId) {
|
|
324
|
+
const callId = `t${++this.seq}`;
|
|
325
|
+
if (this.ended) return Promise.reject(new Error("session_ended"));
|
|
326
|
+
return new Promise((resolve, reject2) => {
|
|
327
|
+
const timer = setTimeout(() => {
|
|
328
|
+
if (this.toolCalls.delete(callId)) reject2(new Error("timeout waiting for tool:result"));
|
|
329
|
+
}, TOOL_TIMEOUT_MS);
|
|
330
|
+
this.toolCalls.set(callId, {
|
|
331
|
+
resolve: (v) => {
|
|
332
|
+
clearTimeout(timer);
|
|
333
|
+
resolve(v);
|
|
334
|
+
},
|
|
335
|
+
reject: (e) => {
|
|
336
|
+
clearTimeout(timer);
|
|
337
|
+
reject2(e);
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
this.send({ type: "tool:call", call_id: callId, ...turnId !== void 0 ? { turn_id: turnId } : {}, name, args });
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
/* ---------------- events ---------------- */
|
|
344
|
+
async handleEvent(e) {
|
|
345
|
+
if (e.name === "user_interruption") this.trimToSpoken(e);
|
|
346
|
+
if (e.name === "reply_spoken" || e.name === "reply_dropped") {
|
|
347
|
+
const id = e.reply_id;
|
|
348
|
+
this.spokenWaiters.get(id)?.();
|
|
349
|
+
this.spokenWaiters.delete(id);
|
|
350
|
+
}
|
|
351
|
+
if (e.name === "error") this.log.warn({ event: e }, "jambonz refused a message from the harness");
|
|
352
|
+
const handler = this.opts.onEvent?.[e.name];
|
|
353
|
+
try {
|
|
354
|
+
if (handler) await handler(e, this.ctx);
|
|
355
|
+
else if (e.name === "silence") await this.defaultSilence(e.streak ?? 1);
|
|
356
|
+
} catch (err) {
|
|
357
|
+
this.log.error({ err, event: e.name }, "onEvent handler threw");
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
/** Keep only what the caller heard of an interrupted reply. */
|
|
361
|
+
trimToSpoken(e) {
|
|
362
|
+
const msgs = this.replies.get(e.reply_id);
|
|
363
|
+
if (!msgs) return;
|
|
364
|
+
let rest = e.spoken ?? "";
|
|
365
|
+
for (const m of msgs) {
|
|
366
|
+
if (m.role !== "assistant" || !m.content) continue;
|
|
367
|
+
const take = rest.slice(0, m.content.length);
|
|
368
|
+
rest = rest.slice(m.content.length);
|
|
369
|
+
m.content = take;
|
|
370
|
+
}
|
|
371
|
+
for (const m of msgs) {
|
|
372
|
+
if (m.role === "assistant" && !m.content && !m.tool_calls?.length) {
|
|
373
|
+
const i = this.history.indexOf(m);
|
|
374
|
+
if (i >= 0) this.history.splice(i, 1);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
this.log.debug({ reply_id: e.reply_id, spoken: e.spoken?.length }, "history trimmed to what the caller heard");
|
|
378
|
+
}
|
|
379
|
+
async defaultSilence(streak) {
|
|
380
|
+
if (streak < 2) {
|
|
381
|
+
this.say(SILENCE_REPROMPT);
|
|
382
|
+
} else {
|
|
383
|
+
const replyId = this.say(SILENCE_GOODBYE);
|
|
384
|
+
await this.hangupAfterSpoken(replyId);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async hangupAfterSpoken(replyId) {
|
|
388
|
+
await new Promise((resolve) => {
|
|
389
|
+
const timer = setTimeout(resolve, SPOKEN_WAIT_MS);
|
|
390
|
+
this.spokenWaiters.set(replyId, () => {
|
|
391
|
+
clearTimeout(timer);
|
|
392
|
+
resolve();
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
if (!this.ended) await this.command("hangup", { reason: "no_input" });
|
|
396
|
+
}
|
|
397
|
+
addAssistant(replyId, msg) {
|
|
398
|
+
this.history.push(msg);
|
|
399
|
+
const list = this.replies.get(replyId);
|
|
400
|
+
if (list) list.push(msg);
|
|
401
|
+
else {
|
|
402
|
+
this.replies.set(replyId, [msg]);
|
|
403
|
+
if (this.replies.size > MAX_TRACKED_REPLIES) this.replies.delete(this.replies.keys().next().value);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
abortTurn(reason) {
|
|
407
|
+
if (!this.current) return;
|
|
408
|
+
this.current.controller.abort(reason);
|
|
409
|
+
this.current = void 0;
|
|
410
|
+
}
|
|
411
|
+
/* ---------------- turns ---------------- */
|
|
412
|
+
async runTurn(msg) {
|
|
413
|
+
this.abortTurn("superseded");
|
|
414
|
+
const run = { turnId: msg.turn_id, controller: new AbortController() };
|
|
415
|
+
this.current = run;
|
|
416
|
+
const { signal } = run.controller;
|
|
417
|
+
const input = msg.input ?? { type: "speech", text: "" };
|
|
418
|
+
const turn = { turnId: msg.turn_id, input, text: input.type === "speech" ? input.text ?? "" : "" };
|
|
419
|
+
const rec = new TurnRecorder();
|
|
420
|
+
Object.assign(rec.record, this.pendingRecord);
|
|
421
|
+
this.pendingRecord = {};
|
|
422
|
+
this.activeRecorder = rec;
|
|
423
|
+
const replyId = `r${msg.turn_id}`;
|
|
424
|
+
const first = {};
|
|
425
|
+
const writer = new ReplyWriter(this, replyId, msg.turn_id, signal, first, this.opts.checkSentence);
|
|
426
|
+
let hangupAfter = false;
|
|
427
|
+
try {
|
|
428
|
+
if (input.type === "no_input") {
|
|
429
|
+
hangupAfter = await this.noInputTurn(turn, writer);
|
|
430
|
+
} else {
|
|
431
|
+
if (input.type === "speech") {
|
|
432
|
+
this.history.push({ role: "user", content: turn.text });
|
|
433
|
+
this.silenceStreak = 0;
|
|
434
|
+
}
|
|
435
|
+
let plan = {};
|
|
436
|
+
if (this.opts.beforeModel) {
|
|
437
|
+
const t = now();
|
|
438
|
+
plan = await this.opts.beforeModel(turn, this.ctx) ?? {};
|
|
439
|
+
rec.step("beforeModel", ms(t));
|
|
440
|
+
if (signal.aborted) return;
|
|
441
|
+
}
|
|
442
|
+
if (plan.route) rec.record.route = plan.route;
|
|
443
|
+
if (plan.interruptible === false) first.interruptible = false;
|
|
444
|
+
const scripted = plan.reply ?? (input.type === "greeting" && !plan.messages ? this.opts.greeting : void 0);
|
|
445
|
+
if (scripted !== void 0) {
|
|
446
|
+
await writer.write(scripted);
|
|
447
|
+
} else {
|
|
448
|
+
await this.modelLoop(turn, plan, writer, rec, signal);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
} catch (err) {
|
|
452
|
+
if (signal.aborted) return;
|
|
453
|
+
this.log.error({ err, turn_id: msg.turn_id }, "turn failed");
|
|
454
|
+
rec.record.error = errMessage(err);
|
|
455
|
+
if (!writer.started) {
|
|
456
|
+
await writer.write(this.opts.errorReply ?? DEFAULT_ERROR_REPLY).catch(() => void 0);
|
|
457
|
+
}
|
|
458
|
+
} finally {
|
|
459
|
+
if (this.activeRecorder === rec) this.activeRecorder = void 0;
|
|
460
|
+
}
|
|
461
|
+
if (signal.aborted) return;
|
|
462
|
+
writer.recordChecks(rec);
|
|
463
|
+
if (this.opts.afterTurn) {
|
|
464
|
+
this.activeRecorder = rec;
|
|
465
|
+
try {
|
|
466
|
+
await this.opts.afterTurn({ ...turn, reply: writer.text, record: rec.record }, this.ctx);
|
|
467
|
+
} catch (err) {
|
|
468
|
+
this.log.error({ err, turn_id: msg.turn_id }, "afterTurn hook threw");
|
|
469
|
+
} finally {
|
|
470
|
+
this.activeRecorder = void 0;
|
|
471
|
+
}
|
|
472
|
+
if (signal.aborted) return;
|
|
473
|
+
}
|
|
474
|
+
if (this.current === run) this.current = void 0;
|
|
475
|
+
let record = rec.record;
|
|
476
|
+
const size = Buffer.byteLength(JSON.stringify(record));
|
|
477
|
+
if (size > MAX_TURN_RECORD_BYTES) {
|
|
478
|
+
this.log.warn({ turn_id: msg.turn_id, bytes: size }, "turn_record over 16 KB; sending a truncated record");
|
|
479
|
+
record = { route: record.route, note: `turn_record truncated (${size} bytes)` };
|
|
480
|
+
}
|
|
481
|
+
let spoken;
|
|
482
|
+
if (hangupAfter) spoken = this.hangupAfterSpoken(replyId);
|
|
483
|
+
this.send({ type: "reply:done", reply_id: replyId, turn_id: msg.turn_id, turn_record: record });
|
|
484
|
+
await spoken;
|
|
485
|
+
}
|
|
486
|
+
/** A `no_input` turn: `onEvent.silence` if set, else reprompt once and hang up on the next. Returns true to hang up. */
|
|
487
|
+
async noInputTurn(turn, writer) {
|
|
488
|
+
const streak = ++this.silenceStreak;
|
|
489
|
+
const handler = this.opts.onEvent?.silence;
|
|
490
|
+
if (handler) {
|
|
491
|
+
await handler({ name: "silence", streak, turn_id: turn.turnId }, this.ctx);
|
|
492
|
+
return false;
|
|
493
|
+
}
|
|
494
|
+
const text = streak < 2 ? SILENCE_REPROMPT : SILENCE_GOODBYE;
|
|
495
|
+
await writer.write(text);
|
|
496
|
+
return streak >= 2;
|
|
497
|
+
}
|
|
498
|
+
toolsFor(plan) {
|
|
499
|
+
const own = Object.entries(this.opts.tools ?? {}).map(([name, t]) => ({
|
|
500
|
+
type: "function",
|
|
501
|
+
function: { name, description: t.description, parameters: t.parameters ?? { type: "object", properties: {} } }
|
|
502
|
+
}));
|
|
503
|
+
const ownNames = new Set(own.map((t) => t.function.name));
|
|
504
|
+
const all = [...own, ...(this.session.tools ?? []).filter((t) => !ownNames.has(t.function?.name))];
|
|
505
|
+
if (!plan.tools) return all;
|
|
506
|
+
const wanted = new Set(plan.tools);
|
|
507
|
+
return all.filter((t) => wanted.has(t.function.name));
|
|
508
|
+
}
|
|
509
|
+
async modelLoop(turn, plan, writer, rec, signal) {
|
|
510
|
+
const model = plan.model ?? this.opts.model ?? "gpt-4.1-mini";
|
|
511
|
+
const instructions = plan.instructions ?? this.opts.instructions;
|
|
512
|
+
const tools = this.toolsFor(plan);
|
|
513
|
+
const base = plan.messages ? [...plan.messages] : [
|
|
514
|
+
...instructions ? [{ role: "system", content: instructions }] : [],
|
|
515
|
+
...this.history,
|
|
516
|
+
...turn.input.type === "greeting" ? [{ role: "user", content: GREETING_PROMPT }] : []
|
|
517
|
+
];
|
|
518
|
+
const added = [];
|
|
519
|
+
const maxRounds = this.opts.maxToolRounds ?? 5;
|
|
520
|
+
for (let round = 0; ; round++) {
|
|
521
|
+
const t0 = now();
|
|
522
|
+
let firstTokenMs;
|
|
523
|
+
let usage;
|
|
524
|
+
const calls = [];
|
|
525
|
+
let stopped = false;
|
|
526
|
+
const offer = round < maxRounds ? tools : [];
|
|
527
|
+
for await (const chunk of this.model.stream({
|
|
528
|
+
model,
|
|
529
|
+
messages: [...base, ...added],
|
|
530
|
+
tools: offer,
|
|
531
|
+
params: this.opts.modelParams,
|
|
532
|
+
signal
|
|
533
|
+
})) {
|
|
534
|
+
if (chunk.usage) usage = chunk.usage;
|
|
535
|
+
const delta = chunk.choices[0]?.delta;
|
|
536
|
+
if (!delta) continue;
|
|
537
|
+
if (delta.content) {
|
|
538
|
+
firstTokenMs ??= ms(t0);
|
|
539
|
+
if (!await writer.content(delta.content, chunk)) {
|
|
540
|
+
stopped = true;
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
for (const tc of delta.tool_calls ?? []) {
|
|
545
|
+
const slot = calls[tc.index] ??= { id: "", name: "", arguments: "" };
|
|
546
|
+
if (tc.id) slot.id = tc.id;
|
|
547
|
+
if (tc.function?.name) slot.name += tc.function.name;
|
|
548
|
+
if (tc.function?.arguments) slot.arguments += tc.function.arguments;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (signal.aborted) return;
|
|
552
|
+
const took = ms(t0);
|
|
553
|
+
rec.step("model", took);
|
|
554
|
+
rec.modelCall({
|
|
555
|
+
model,
|
|
556
|
+
purpose: "reply",
|
|
557
|
+
...usage?.prompt_tokens !== void 0 ? { input_tokens: usage.prompt_tokens } : {},
|
|
558
|
+
...usage?.completion_tokens !== void 0 ? { output_tokens: usage.completion_tokens } : {},
|
|
559
|
+
...firstTokenMs !== void 0 ? { first_token_ms: firstTokenMs } : {},
|
|
560
|
+
ms: took
|
|
561
|
+
});
|
|
562
|
+
const said = await writer.endSegment();
|
|
563
|
+
if (signal.aborted) return;
|
|
564
|
+
const toolCalls = stopped || writer.stopped ? [] : calls.filter(Boolean).map((c, i) => ({
|
|
565
|
+
id: c.id || `call_${turn.turnId}_${round}_${i}`,
|
|
566
|
+
type: "function",
|
|
567
|
+
function: { name: c.name, arguments: c.arguments }
|
|
568
|
+
}));
|
|
569
|
+
if (!toolCalls.length) return;
|
|
570
|
+
let assistant;
|
|
571
|
+
if (said) {
|
|
572
|
+
assistant = Object.assign(said, { tool_calls: toolCalls });
|
|
573
|
+
} else {
|
|
574
|
+
assistant = { role: "assistant", content: null, tool_calls: toolCalls };
|
|
575
|
+
this.addAssistant(writer.replyId, assistant);
|
|
576
|
+
}
|
|
577
|
+
added.push(assistant);
|
|
578
|
+
const onAbort = () => {
|
|
579
|
+
for (const tc of toolCalls) this.history.push({ role: "tool", tool_call_id: tc.id, content: '{"error":"cancelled"}' });
|
|
580
|
+
};
|
|
581
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
582
|
+
const results = await Promise.all(toolCalls.map((tc) => this.runTool(tc, turn.turnId, rec)));
|
|
583
|
+
signal.removeEventListener("abort", onAbort);
|
|
584
|
+
if (signal.aborted) return;
|
|
585
|
+
for (const r of results) {
|
|
586
|
+
added.push(r);
|
|
587
|
+
this.history.push(r);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
async runTool(tc, turnId, rec) {
|
|
592
|
+
const name = tc.function.name;
|
|
593
|
+
const t = now();
|
|
594
|
+
let content;
|
|
595
|
+
let ok = true;
|
|
596
|
+
let args = {};
|
|
597
|
+
try {
|
|
598
|
+
args = tc.function.arguments ? JSON.parse(tc.function.arguments) : {};
|
|
599
|
+
} catch {
|
|
600
|
+
ok = false;
|
|
601
|
+
}
|
|
602
|
+
const own = this.opts.tools?.[name];
|
|
603
|
+
const studio = this.session.tools?.some((x) => x.function?.name === name);
|
|
604
|
+
try {
|
|
605
|
+
if (!ok) throw new Error(`invalid JSON arguments for ${name}`);
|
|
606
|
+
let result;
|
|
607
|
+
if (own) result = await own.run(args, this.ctx);
|
|
608
|
+
else if (studio) result = await this.callTool(name, args, turnId);
|
|
609
|
+
else throw new Error(`unknown tool ${name}`);
|
|
610
|
+
content = typeof result === "string" ? result : JSON.stringify(result ?? null);
|
|
611
|
+
} catch (err) {
|
|
612
|
+
ok = false;
|
|
613
|
+
this.log.warn({ err, tool: name, turn_id: turnId }, "tool failed");
|
|
614
|
+
content = JSON.stringify({ error: errMessage(err) });
|
|
615
|
+
}
|
|
616
|
+
const took = ms(t);
|
|
617
|
+
rec.step(`tool:${name}`, took, ok ? "ok" : "error");
|
|
618
|
+
if (own) rec.ownTool(name, ok, took);
|
|
619
|
+
return { role: "tool", tool_call_id: tc.id, content };
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
// src/harness/model.ts
|
|
624
|
+
var ModelError = class extends Error {
|
|
625
|
+
constructor(message, status) {
|
|
626
|
+
super(message);
|
|
627
|
+
this.status = status;
|
|
628
|
+
this.name = "ModelError";
|
|
629
|
+
}
|
|
630
|
+
status;
|
|
631
|
+
};
|
|
632
|
+
function createModelClient(opts = {}) {
|
|
633
|
+
const apiKey = opts.apiKey ?? process.env.OPENAI_API_KEY;
|
|
634
|
+
const baseURL = (opts.baseURL ?? process.env.OPENAI_BASE_URL ?? "https://api.openai.com/v1").replace(/\/+$/, "");
|
|
635
|
+
return {
|
|
636
|
+
baseURL,
|
|
637
|
+
async *stream(req) {
|
|
638
|
+
const body = {
|
|
639
|
+
stream_options: { include_usage: true },
|
|
640
|
+
...req.params,
|
|
641
|
+
model: req.model,
|
|
642
|
+
messages: req.messages,
|
|
643
|
+
stream: true
|
|
644
|
+
};
|
|
645
|
+
if (req.tools?.length) body.tools = req.tools;
|
|
646
|
+
const res = await fetch(`${baseURL}/chat/completions`, {
|
|
647
|
+
method: "POST",
|
|
648
|
+
headers: {
|
|
649
|
+
"Content-Type": "application/json",
|
|
650
|
+
Accept: "text/event-stream",
|
|
651
|
+
...apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
|
|
652
|
+
...opts.headers
|
|
653
|
+
},
|
|
654
|
+
body: JSON.stringify(body),
|
|
655
|
+
signal: req.signal
|
|
656
|
+
});
|
|
657
|
+
if (!res.ok || !res.body) {
|
|
658
|
+
const text = await res.text().catch(() => "");
|
|
659
|
+
throw new ModelError(`model request failed: ${res.status} ${text.slice(0, 500)}`, res.status);
|
|
660
|
+
}
|
|
661
|
+
const decoder = new TextDecoder();
|
|
662
|
+
let buffer = "";
|
|
663
|
+
for await (const bytes of res.body) {
|
|
664
|
+
buffer += decoder.decode(bytes, { stream: true });
|
|
665
|
+
let nl;
|
|
666
|
+
while ((nl = buffer.indexOf("\n")) >= 0) {
|
|
667
|
+
const line = buffer.slice(0, nl).trim();
|
|
668
|
+
buffer = buffer.slice(nl + 1);
|
|
669
|
+
if (!line.startsWith("data:")) continue;
|
|
670
|
+
const data = line.slice(5).trim();
|
|
671
|
+
if (data === "[DONE]") return;
|
|
672
|
+
let chunk;
|
|
673
|
+
try {
|
|
674
|
+
chunk = JSON.parse(data);
|
|
675
|
+
} catch {
|
|
676
|
+
throw new ModelError(`model sent an unparseable stream event: ${data.slice(0, 200)}`);
|
|
677
|
+
}
|
|
678
|
+
if (chunk.error) throw new ModelError(`model stream error: ${chunk.error.message ?? JSON.stringify(chunk.error)}`);
|
|
679
|
+
if (!Array.isArray(chunk.choices)) chunk.choices = [];
|
|
680
|
+
yield chunk;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// src/harness/protocol.ts
|
|
688
|
+
var HARNESS_SUBPROTOCOL = "harness.jambonz.org";
|
|
689
|
+
var HARNESS_PROTOCOL_VERSION = 1;
|
|
690
|
+
|
|
691
|
+
// src/harness/server.ts
|
|
692
|
+
var RESUME_WINDOW_MS = 1e4;
|
|
693
|
+
function keyMatches(header, apiKey) {
|
|
694
|
+
const expected = Buffer.from(`Bearer ${apiKey}`);
|
|
695
|
+
const got = Buffer.from(header ?? "");
|
|
696
|
+
return got.length === expected.length && crypto.timingSafeEqual(got, expected);
|
|
697
|
+
}
|
|
698
|
+
function reject(socket, status, text) {
|
|
699
|
+
socket.write(`HTTP/1.1 ${status} ${text}\r
|
|
700
|
+
Connection: close\r
|
|
701
|
+
Content-Length: 0\r
|
|
702
|
+
\r
|
|
703
|
+
`);
|
|
704
|
+
socket.destroy();
|
|
705
|
+
}
|
|
706
|
+
function createHarness(options = {}) {
|
|
707
|
+
const log = options.logger ?? pino.pino({ level: process.env.LOG_LEVEL ?? "info" });
|
|
708
|
+
const model = createModelClient(options.client);
|
|
709
|
+
const calls = /* @__PURE__ */ new Map();
|
|
710
|
+
const detached = /* @__PURE__ */ new Map();
|
|
711
|
+
const server = http__default.default.createServer((req, res) => {
|
|
712
|
+
if (req.url === "/health") {
|
|
713
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
714
|
+
res.end(JSON.stringify({ ok: true, calls: calls.size }));
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
res.writeHead(426, { "Content-Type": "text/plain" });
|
|
718
|
+
res.end(`WebSocket (${HARNESS_SUBPROTOCOL}) only
|
|
719
|
+
`);
|
|
720
|
+
});
|
|
721
|
+
const wss = new ws.WebSocketServer({
|
|
722
|
+
noServer: true,
|
|
723
|
+
handleProtocols: (protocols) => protocols.has(HARNESS_SUBPROTOCOL) ? HARNESS_SUBPROTOCOL : false
|
|
724
|
+
});
|
|
725
|
+
server.on("upgrade", (req, socket, head) => {
|
|
726
|
+
const remote = req.socket.remoteAddress;
|
|
727
|
+
if (options.apiKey && !keyMatches(req.headers.authorization, options.apiKey)) {
|
|
728
|
+
log.warn({ remote, has_auth: !!req.headers.authorization }, "harness connection rejected: bad or missing api key");
|
|
729
|
+
return reject(socket, 401, "Unauthorized");
|
|
730
|
+
}
|
|
731
|
+
const offered = (req.headers["sec-websocket-protocol"] ?? "").split(",").map((s) => s.trim());
|
|
732
|
+
if (!offered.includes(HARNESS_SUBPROTOCOL)) {
|
|
733
|
+
log.warn({ remote, offered }, `harness connection rejected: subprotocol ${HARNESS_SUBPROTOCOL} not offered`);
|
|
734
|
+
return reject(socket, 400, "Bad Request");
|
|
735
|
+
}
|
|
736
|
+
wss.handleUpgrade(req, socket, head, (ws) => onConnection(ws, remote));
|
|
737
|
+
});
|
|
738
|
+
function onConnection(ws, remote) {
|
|
739
|
+
let call;
|
|
740
|
+
let ready = Promise.resolve();
|
|
741
|
+
let ended = false;
|
|
742
|
+
log.debug({ remote }, "harness connection opened");
|
|
743
|
+
ws.on("message", async (raw, isBinary) => {
|
|
744
|
+
if (isBinary) {
|
|
745
|
+
log.warn({ call_sid: call?.callSid }, "binary frame ignored");
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
let msg;
|
|
749
|
+
try {
|
|
750
|
+
msg = JSON.parse(raw.toString());
|
|
751
|
+
} catch {
|
|
752
|
+
log.warn({ call_sid: call?.callSid }, "unparseable message ignored");
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
if (!msg || typeof msg.type !== "string") return;
|
|
756
|
+
if (msg.type === "session:start") {
|
|
757
|
+
if (msg.protocol !== void 0 && msg.protocol !== HARNESS_PROTOCOL_VERSION) {
|
|
758
|
+
log.error({ call_sid: msg.call_sid, protocol: msg.protocol }, "unsupported harness protocol version");
|
|
759
|
+
ws.close(4e3, `unsupported protocol ${msg.protocol}; this harness speaks ${HARNESS_PROTOCOL_VERSION}`);
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
const existing = calls.get(msg.call_sid);
|
|
763
|
+
if (msg.resumed && existing && !existing.ended) {
|
|
764
|
+
clearTimeout(detached.get(msg.call_sid));
|
|
765
|
+
detached.delete(msg.call_sid);
|
|
766
|
+
call = existing;
|
|
767
|
+
call.attach(ws);
|
|
768
|
+
call.log.info({ turn_id: msg.turn_id }, "session resumed on a new socket");
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
call = new HarnessCall(msg, options, model, log);
|
|
772
|
+
call.attach(ws);
|
|
773
|
+
calls.set(msg.call_sid, call);
|
|
774
|
+
call.log.info(
|
|
775
|
+
{ from: msg.from, to: msg.to, direction: msg.direction, tools: msg.tools?.length ?? 0, resumed: !!msg.resumed },
|
|
776
|
+
msg.resumed ? "session resumed, but its state was lost; starting fresh" : "session started"
|
|
777
|
+
);
|
|
778
|
+
const c2 = call;
|
|
779
|
+
ready = c2.start().catch((err) => c2.log.error({ err }, "onSessionStart hook threw"));
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
if (!call) {
|
|
783
|
+
log.warn({ type: msg.type, call_sid: msg.call_sid }, "message before session:start ignored");
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
if (msg.type === "session:end") ended = true;
|
|
787
|
+
const c = call;
|
|
788
|
+
await ready;
|
|
789
|
+
if (msg.type === "session:end" && calls.get(c.callSid) === c) calls.delete(c.callSid);
|
|
790
|
+
c.handle(msg);
|
|
791
|
+
});
|
|
792
|
+
ws.on("close", (code, reason) => {
|
|
793
|
+
if (!call) return;
|
|
794
|
+
const c = call;
|
|
795
|
+
if (ended || c.ended) {
|
|
796
|
+
c.log.debug({ code }, "harness socket closed");
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
c.log.warn({ code, reason: reason.toString() }, "harness socket closed without session:end; holding state for a resume");
|
|
800
|
+
c.detach();
|
|
801
|
+
detached.set(
|
|
802
|
+
c.callSid,
|
|
803
|
+
setTimeout(() => {
|
|
804
|
+
detached.delete(c.callSid);
|
|
805
|
+
if (calls.get(c.callSid) === c) calls.delete(c.callSid);
|
|
806
|
+
void c.end({ type: "session:end", call_sid: c.callSid, reason: "socket_closed" });
|
|
807
|
+
}, RESUME_WINDOW_MS)
|
|
808
|
+
);
|
|
809
|
+
});
|
|
810
|
+
ws.on("error", (err) => log.error({ err, call_sid: call?.callSid }, "harness socket error"));
|
|
811
|
+
}
|
|
812
|
+
return {
|
|
813
|
+
listen(opts) {
|
|
814
|
+
const { port, host } = typeof opts === "number" ? { port: opts, host: void 0 } : opts;
|
|
815
|
+
return new Promise((resolve, rejectListen) => {
|
|
816
|
+
server.once("error", rejectListen);
|
|
817
|
+
server.listen(port, host, () => {
|
|
818
|
+
server.off("error", rejectListen);
|
|
819
|
+
const addr = server.address();
|
|
820
|
+
log.info({ port: addr.port, host: addr.address, auth: !!options.apiKey }, "jambonz harness listening");
|
|
821
|
+
resolve({ port: addr.port, host: addr.address });
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
},
|
|
825
|
+
async close() {
|
|
826
|
+
for (const t of detached.values()) clearTimeout(t);
|
|
827
|
+
detached.clear();
|
|
828
|
+
for (const ws of wss.clients) ws.terminate();
|
|
829
|
+
calls.clear();
|
|
830
|
+
await new Promise((resolve) => wss.close(() => resolve()));
|
|
831
|
+
if (server.listening) await new Promise((resolve) => server.close(() => resolve()));
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
exports.HARNESS_PROTOCOL_VERSION = HARNESS_PROTOCOL_VERSION;
|
|
837
|
+
exports.HARNESS_SUBPROTOCOL = HARNESS_SUBPROTOCOL;
|
|
838
|
+
exports.ModelError = ModelError;
|
|
839
|
+
exports.SentenceSplitter = SentenceSplitter;
|
|
840
|
+
exports.createHarness = createHarness;
|
|
841
|
+
exports.createModelClient = createModelClient;
|
|
842
|
+
//# sourceMappingURL=index.cjs.map
|
|
843
|
+
//# sourceMappingURL=index.cjs.map
|