@alexkroman1/aai 0.10.2 → 0.10.3
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/_embeddings.d.ts +31 -0
- package/dist/_internal-types-IfPcaJd5.js +61 -0
- package/dist/_internal-types.js +1 -60
- package/dist/_ssrf-DCp_27V4.js +123 -0
- package/dist/_ssrf.js +1 -122
- package/dist/_utils-DgzpOMSV.js +61 -0
- package/dist/_utils.js +1 -60
- package/dist/{direct-executor-Ca0wt5H0.js → direct-executor-B-5mq3cu.js} +15 -17
- package/dist/index.js +1 -1
- package/dist/kv-iXtikQmR.js +32 -0
- package/dist/kv.js +1 -31
- package/dist/matchers.js +1 -1
- package/dist/middleware-core-BwyBIPed.js +107 -0
- package/dist/middleware-core.js +1 -106
- package/dist/protocol-B-H2Q4ox.js +162 -0
- package/dist/protocol.js +1 -161
- package/dist/runtime-CxcwaK68.js +58 -0
- package/dist/runtime.js +1 -52
- package/dist/s2s-M7JqtgFw.js +272 -0
- package/dist/s2s.js +1 -271
- package/dist/server.d.ts +6 -6
- package/dist/server.js +47 -43
- package/dist/{session-BkN9u0ni.js → session-BYlwcrya.js} +6 -6
- package/dist/session.js +1 -1
- package/dist/telemetry-CJlaDFNc.js +95 -0
- package/dist/telemetry.js +1 -94
- package/dist/{testing-MRl3SXsI.js → testing-BbitshLb.js} +7 -9
- package/dist/testing.js +1 -1
- package/dist/types-D8ZBxTL_.js +192 -0
- package/dist/types.js +1 -191
- package/dist/unstorage-kv-CDgP-frt.js +64 -0
- package/dist/unstorage-kv.d.ts +33 -0
- package/dist/unstorage-kv.js +2 -0
- package/dist/unstorage-vector-Cj5llNhg.js +172 -0
- package/dist/unstorage-vector.d.ts +47 -0
- package/dist/unstorage-vector.js +2 -0
- package/dist/vector.d.ts +3 -2
- package/dist/worker-entry-2jaiqIj0.js +70 -0
- package/dist/worker-entry.js +1 -69
- package/dist/ws-handler-C0Q6eSay.js +207 -0
- package/dist/ws-handler.js +1 -206
- package/package.json +14 -9
- package/dist/sqlite-kv.d.ts +0 -34
- package/dist/sqlite-kv.js +0 -133
- package/dist/sqlite-vector.d.ts +0 -58
- package/dist/sqlite-vector.js +0 -149
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
//#region middleware-core.ts
|
|
2
|
+
/**
|
|
3
|
+
* Run all `beforeInput` middleware in order, piping the text through each.
|
|
4
|
+
* Symmetric to {@link runOutputFilters} but for user input.
|
|
5
|
+
*/
|
|
6
|
+
async function runInputFilters(middleware, text, ctx) {
|
|
7
|
+
let filtered = text;
|
|
8
|
+
for (const mw of middleware) {
|
|
9
|
+
if (!mw.beforeInput) continue;
|
|
10
|
+
try {
|
|
11
|
+
filtered = await mw.beforeInput(filtered, ctx);
|
|
12
|
+
} catch (err) {
|
|
13
|
+
console.warn("Middleware beforeInput failed:", err);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return filtered;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Run all `beforeTurn` middleware in order. Returns a block result if any
|
|
20
|
+
* middleware blocks the turn, or `undefined` to proceed.
|
|
21
|
+
*/
|
|
22
|
+
async function runBeforeTurnMiddleware(middleware, text, ctx) {
|
|
23
|
+
for (const mw of middleware) {
|
|
24
|
+
if (!mw.beforeTurn) continue;
|
|
25
|
+
try {
|
|
26
|
+
const result = await mw.beforeTurn(text, ctx);
|
|
27
|
+
if (result && "block" in result && result.block) return result;
|
|
28
|
+
} catch (err) {
|
|
29
|
+
console.warn("Middleware beforeTurn failed:", err);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Run all `afterTurn` middleware in reverse order.
|
|
35
|
+
*/
|
|
36
|
+
async function runAfterTurnMiddleware(middleware, text, ctx) {
|
|
37
|
+
for (let i = middleware.length - 1; i >= 0; i--) {
|
|
38
|
+
const mw = middleware[i];
|
|
39
|
+
if (!mw?.afterTurn) continue;
|
|
40
|
+
try {
|
|
41
|
+
await mw.afterTurn(text, ctx);
|
|
42
|
+
} catch (err) {
|
|
43
|
+
console.warn("Middleware afterTurn failed:", err);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Run all `beforeToolCall` middleware in order. Returns a result that
|
|
49
|
+
* may block execution, provide a cached result, or transform args.
|
|
50
|
+
* Returns `undefined` to proceed with normal execution.
|
|
51
|
+
*/
|
|
52
|
+
async function runToolCallInterceptors(middleware, toolName, args, ctx) {
|
|
53
|
+
let currentArgs = args;
|
|
54
|
+
for (const mw of middleware) {
|
|
55
|
+
if (!mw.beforeToolCall) continue;
|
|
56
|
+
try {
|
|
57
|
+
const result = await mw.beforeToolCall(toolName, currentArgs, ctx);
|
|
58
|
+
if (!result) continue;
|
|
59
|
+
if ("block" in result && result.block) return {
|
|
60
|
+
type: "block",
|
|
61
|
+
reason: result.reason
|
|
62
|
+
};
|
|
63
|
+
if ("result" in result) return {
|
|
64
|
+
type: "result",
|
|
65
|
+
result: result.result
|
|
66
|
+
};
|
|
67
|
+
if ("args" in result) currentArgs = result.args;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
console.warn("Middleware beforeToolCall failed:", err);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (currentArgs !== args) return {
|
|
73
|
+
type: "args",
|
|
74
|
+
args: currentArgs
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Run all `afterToolCall` middleware in reverse order.
|
|
79
|
+
*/
|
|
80
|
+
async function runAfterToolCallMiddleware(middleware, toolName, args, result, ctx) {
|
|
81
|
+
for (let i = middleware.length - 1; i >= 0; i--) {
|
|
82
|
+
const mw = middleware[i];
|
|
83
|
+
if (!mw?.afterToolCall) continue;
|
|
84
|
+
try {
|
|
85
|
+
await mw.afterToolCall(toolName, args, result, ctx);
|
|
86
|
+
} catch (err) {
|
|
87
|
+
console.warn("Middleware afterToolCall failed:", err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Run all `beforeOutput` middleware in order, piping the text through each.
|
|
93
|
+
*/
|
|
94
|
+
async function runOutputFilters(middleware, text, ctx) {
|
|
95
|
+
let filtered = text;
|
|
96
|
+
for (const mw of middleware) {
|
|
97
|
+
if (!mw.beforeOutput) continue;
|
|
98
|
+
try {
|
|
99
|
+
filtered = await mw.beforeOutput(filtered, ctx);
|
|
100
|
+
} catch (err) {
|
|
101
|
+
console.warn("Middleware beforeOutput failed:", err);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return filtered;
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
export { runOutputFilters as a, runInputFilters as i, runAfterTurnMiddleware as n, runToolCallInterceptors as o, runBeforeTurnMiddleware as r, runAfterToolCallMiddleware as t };
|
package/dist/middleware-core.js
CHANGED
|
@@ -1,107 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Run all `beforeInput` middleware in order, piping the text through each.
|
|
4
|
-
* Symmetric to {@link runOutputFilters} but for user input.
|
|
5
|
-
*/
|
|
6
|
-
async function runInputFilters(middleware, text, ctx) {
|
|
7
|
-
let filtered = text;
|
|
8
|
-
for (const mw of middleware) {
|
|
9
|
-
if (!mw.beforeInput) continue;
|
|
10
|
-
try {
|
|
11
|
-
filtered = await mw.beforeInput(filtered, ctx);
|
|
12
|
-
} catch (err) {
|
|
13
|
-
console.warn("Middleware beforeInput failed:", err);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return filtered;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Run all `beforeTurn` middleware in order. Returns a block result if any
|
|
20
|
-
* middleware blocks the turn, or `undefined` to proceed.
|
|
21
|
-
*/
|
|
22
|
-
async function runBeforeTurnMiddleware(middleware, text, ctx) {
|
|
23
|
-
for (const mw of middleware) {
|
|
24
|
-
if (!mw.beforeTurn) continue;
|
|
25
|
-
try {
|
|
26
|
-
const result = await mw.beforeTurn(text, ctx);
|
|
27
|
-
if (result && "block" in result && result.block) return result;
|
|
28
|
-
} catch (err) {
|
|
29
|
-
console.warn("Middleware beforeTurn failed:", err);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Run all `afterTurn` middleware in reverse order.
|
|
35
|
-
*/
|
|
36
|
-
async function runAfterTurnMiddleware(middleware, text, ctx) {
|
|
37
|
-
for (let i = middleware.length - 1; i >= 0; i--) {
|
|
38
|
-
const mw = middleware[i];
|
|
39
|
-
if (!mw?.afterTurn) continue;
|
|
40
|
-
try {
|
|
41
|
-
await mw.afterTurn(text, ctx);
|
|
42
|
-
} catch (err) {
|
|
43
|
-
console.warn("Middleware afterTurn failed:", err);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Run all `beforeToolCall` middleware in order. Returns a result that
|
|
49
|
-
* may block execution, provide a cached result, or transform args.
|
|
50
|
-
* Returns `undefined` to proceed with normal execution.
|
|
51
|
-
*/
|
|
52
|
-
async function runToolCallInterceptors(middleware, toolName, args, ctx) {
|
|
53
|
-
let currentArgs = args;
|
|
54
|
-
for (const mw of middleware) {
|
|
55
|
-
if (!mw.beforeToolCall) continue;
|
|
56
|
-
try {
|
|
57
|
-
const result = await mw.beforeToolCall(toolName, currentArgs, ctx);
|
|
58
|
-
if (!result) continue;
|
|
59
|
-
if ("block" in result && result.block) return {
|
|
60
|
-
type: "block",
|
|
61
|
-
reason: result.reason
|
|
62
|
-
};
|
|
63
|
-
if ("result" in result) return {
|
|
64
|
-
type: "result",
|
|
65
|
-
result: result.result
|
|
66
|
-
};
|
|
67
|
-
if ("args" in result) currentArgs = result.args;
|
|
68
|
-
} catch (err) {
|
|
69
|
-
console.warn("Middleware beforeToolCall failed:", err);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (currentArgs !== args) return {
|
|
73
|
-
type: "args",
|
|
74
|
-
args: currentArgs
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Run all `afterToolCall` middleware in reverse order.
|
|
79
|
-
*/
|
|
80
|
-
async function runAfterToolCallMiddleware(middleware, toolName, args, result, ctx) {
|
|
81
|
-
for (let i = middleware.length - 1; i >= 0; i--) {
|
|
82
|
-
const mw = middleware[i];
|
|
83
|
-
if (!mw?.afterToolCall) continue;
|
|
84
|
-
try {
|
|
85
|
-
await mw.afterToolCall(toolName, args, result, ctx);
|
|
86
|
-
} catch (err) {
|
|
87
|
-
console.warn("Middleware afterToolCall failed:", err);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Run all `beforeOutput` middleware in order, piping the text through each.
|
|
93
|
-
*/
|
|
94
|
-
async function runOutputFilters(middleware, text, ctx) {
|
|
95
|
-
let filtered = text;
|
|
96
|
-
for (const mw of middleware) {
|
|
97
|
-
if (!mw.beforeOutput) continue;
|
|
98
|
-
try {
|
|
99
|
-
filtered = await mw.beforeOutput(filtered, ctx);
|
|
100
|
-
} catch (err) {
|
|
101
|
-
console.warn("Middleware beforeOutput failed:", err);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return filtered;
|
|
105
|
-
}
|
|
106
|
-
//#endregion
|
|
1
|
+
import { a as runOutputFilters, i as runInputFilters, n as runAfterTurnMiddleware, o as runToolCallInterceptors, r as runBeforeTurnMiddleware, t as runAfterToolCallMiddleware } from "./middleware-core-BwyBIPed.js";
|
|
107
2
|
export { runAfterToolCallMiddleware, runAfterTurnMiddleware, runBeforeTurnMiddleware, runInputFilters, runOutputFilters, runToolCallInterceptors };
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
//#region protocol.ts
|
|
3
|
+
/**
|
|
4
|
+
* WebSocket wire-format types shared by server and client.
|
|
5
|
+
*
|
|
6
|
+
* Note: this module is for internal use only and should not be used directly.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Default sample rate for speech-to-text audio in Hz.
|
|
10
|
+
*
|
|
11
|
+
* This is the sample rate expected by the STT provider (AssemblyAI).
|
|
12
|
+
*/
|
|
13
|
+
const DEFAULT_STT_SAMPLE_RATE = 16e3;
|
|
14
|
+
/**
|
|
15
|
+
* Default sample rate for text-to-speech audio in Hz.
|
|
16
|
+
*
|
|
17
|
+
* This is the sample rate produced by the TTS provider.
|
|
18
|
+
*/
|
|
19
|
+
const DEFAULT_TTS_SAMPLE_RATE = 24e3;
|
|
20
|
+
/**
|
|
21
|
+
* Audio codec identifier used in the wire protocol.
|
|
22
|
+
*
|
|
23
|
+
* All audio frames are 16-bit signed PCM, little-endian, mono.
|
|
24
|
+
*/
|
|
25
|
+
const AUDIO_FORMAT = "pcm16";
|
|
26
|
+
/** Zod schema for KV operation requests from the worker to the host. */
|
|
27
|
+
const KvRequestSchema = z.discriminatedUnion("op", [
|
|
28
|
+
z.object({
|
|
29
|
+
op: z.literal("get"),
|
|
30
|
+
key: z.string().min(1)
|
|
31
|
+
}),
|
|
32
|
+
z.object({
|
|
33
|
+
op: z.literal("set"),
|
|
34
|
+
key: z.string().min(1),
|
|
35
|
+
value: z.string(),
|
|
36
|
+
expireIn: z.number().int().positive().optional()
|
|
37
|
+
}),
|
|
38
|
+
z.object({
|
|
39
|
+
op: z.literal("del"),
|
|
40
|
+
key: z.string().min(1)
|
|
41
|
+
}),
|
|
42
|
+
z.object({
|
|
43
|
+
op: z.literal("list"),
|
|
44
|
+
prefix: z.string(),
|
|
45
|
+
limit: z.number().int().positive().optional(),
|
|
46
|
+
reverse: z.boolean().optional()
|
|
47
|
+
}),
|
|
48
|
+
z.object({
|
|
49
|
+
op: z.literal("keys"),
|
|
50
|
+
pattern: z.string().optional()
|
|
51
|
+
})
|
|
52
|
+
]);
|
|
53
|
+
/** Default timeout for agent lifecycle hooks (onConnect, onTurn, etc). */
|
|
54
|
+
const HOOK_TIMEOUT_MS = 5e3;
|
|
55
|
+
/** Default timeout for tool execution in the worker. */
|
|
56
|
+
const TOOL_EXECUTION_TIMEOUT_MS = 3e4;
|
|
57
|
+
/** Maximum length for tool result strings sent to clients. */
|
|
58
|
+
const MAX_TOOL_RESULT_CHARS = 4e3;
|
|
59
|
+
/**
|
|
60
|
+
* Zod schema for session error codes.
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
const SessionErrorCodeSchema = z.enum([
|
|
64
|
+
"stt",
|
|
65
|
+
"llm",
|
|
66
|
+
"tts",
|
|
67
|
+
"tool",
|
|
68
|
+
"protocol",
|
|
69
|
+
"connection",
|
|
70
|
+
"audio",
|
|
71
|
+
"internal"
|
|
72
|
+
]);
|
|
73
|
+
/** Helper: simple event with only a type field. */
|
|
74
|
+
const ev = (t) => z.object({ type: z.literal(t) });
|
|
75
|
+
/** Helper: event with type + text. */
|
|
76
|
+
const textEv = (t) => z.object({
|
|
77
|
+
type: z.literal(t),
|
|
78
|
+
text: z.string()
|
|
79
|
+
});
|
|
80
|
+
const turnOrder = z.number().int().nonnegative().optional();
|
|
81
|
+
/** Zod schema for {@link ClientEvent}. */
|
|
82
|
+
const ClientEventSchema = z.discriminatedUnion("type", [
|
|
83
|
+
ev("speech_started"),
|
|
84
|
+
ev("speech_stopped"),
|
|
85
|
+
z.object({
|
|
86
|
+
type: z.literal("transcript"),
|
|
87
|
+
text: z.string(),
|
|
88
|
+
isFinal: z.boolean(),
|
|
89
|
+
turnOrder
|
|
90
|
+
}),
|
|
91
|
+
textEv("turn").extend({ turnOrder }),
|
|
92
|
+
textEv("chat"),
|
|
93
|
+
textEv("chat_delta"),
|
|
94
|
+
z.object({
|
|
95
|
+
type: z.literal("tool_call_start"),
|
|
96
|
+
toolCallId: z.string(),
|
|
97
|
+
toolName: z.string(),
|
|
98
|
+
args: z.record(z.string(), z.unknown())
|
|
99
|
+
}),
|
|
100
|
+
z.object({
|
|
101
|
+
type: z.literal("tool_call_update"),
|
|
102
|
+
toolCallId: z.string(),
|
|
103
|
+
data: z.string().max(MAX_TOOL_RESULT_CHARS)
|
|
104
|
+
}),
|
|
105
|
+
z.object({
|
|
106
|
+
type: z.literal("tool_call_done"),
|
|
107
|
+
toolCallId: z.string(),
|
|
108
|
+
result: z.string().max(MAX_TOOL_RESULT_CHARS)
|
|
109
|
+
}),
|
|
110
|
+
ev("tts_done"),
|
|
111
|
+
ev("cancelled"),
|
|
112
|
+
ev("reset"),
|
|
113
|
+
ev("idle_timeout"),
|
|
114
|
+
z.object({
|
|
115
|
+
type: z.literal("error"),
|
|
116
|
+
code: SessionErrorCodeSchema,
|
|
117
|
+
message: z.string()
|
|
118
|
+
})
|
|
119
|
+
]);
|
|
120
|
+
/** Zod schema for {@link ReadyConfig}. */
|
|
121
|
+
const ReadyConfigSchema = z.object({
|
|
122
|
+
audioFormat: z.enum(["pcm16"]),
|
|
123
|
+
sampleRate: z.number().int().positive(),
|
|
124
|
+
ttsSampleRate: z.number().int().positive()
|
|
125
|
+
});
|
|
126
|
+
/** Zod schema for server→client text messages. */
|
|
127
|
+
const ServerMessageSchema = z.discriminatedUnion("type", [
|
|
128
|
+
z.object({
|
|
129
|
+
type: z.literal("config"),
|
|
130
|
+
audioFormat: z.string(),
|
|
131
|
+
sampleRate: z.number(),
|
|
132
|
+
ttsSampleRate: z.number(),
|
|
133
|
+
sessionId: z.string().optional()
|
|
134
|
+
}),
|
|
135
|
+
ev("audio_done"),
|
|
136
|
+
...ClientEventSchema.options
|
|
137
|
+
]);
|
|
138
|
+
/** Zod schema for client→server text messages. */
|
|
139
|
+
const ClientMessageSchema = z.discriminatedUnion("type", [
|
|
140
|
+
ev("audio_ready"),
|
|
141
|
+
ev("cancel"),
|
|
142
|
+
ev("reset"),
|
|
143
|
+
z.object({
|
|
144
|
+
type: z.literal("history"),
|
|
145
|
+
messages: z.array(z.object({
|
|
146
|
+
role: z.enum(["user", "assistant"]),
|
|
147
|
+
content: z.string().max(1e5)
|
|
148
|
+
})).max(200)
|
|
149
|
+
})
|
|
150
|
+
]);
|
|
151
|
+
/** Build the protocol-level session config from S2S sample rates. */
|
|
152
|
+
function buildReadyConfig(s2sConfig) {
|
|
153
|
+
return {
|
|
154
|
+
audioFormat: AUDIO_FORMAT,
|
|
155
|
+
sampleRate: s2sConfig.inputSampleRate,
|
|
156
|
+
ttsSampleRate: s2sConfig.outputSampleRate
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/** Zod schema for {@link TurnConfig}. */
|
|
160
|
+
const TurnConfigSchema = z.object({ maxSteps: z.number().int().positive().optional() });
|
|
161
|
+
//#endregion
|
|
162
|
+
export { DEFAULT_TTS_SAMPLE_RATE as a, MAX_TOOL_RESULT_CHARS as c, SessionErrorCodeSchema as d, TOOL_EXECUTION_TIMEOUT_MS as f, DEFAULT_STT_SAMPLE_RATE as i, ReadyConfigSchema as l, buildReadyConfig as m, ClientEventSchema as n, HOOK_TIMEOUT_MS as o, TurnConfigSchema as p, ClientMessageSchema as r, KvRequestSchema as s, AUDIO_FORMAT as t, ServerMessageSchema as u };
|
package/dist/protocol.js
CHANGED
|
@@ -1,162 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region protocol.ts
|
|
3
|
-
/**
|
|
4
|
-
* WebSocket wire-format types shared by server and client.
|
|
5
|
-
*
|
|
6
|
-
* Note: this module is for internal use only and should not be used directly.
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Default sample rate for speech-to-text audio in Hz.
|
|
10
|
-
*
|
|
11
|
-
* This is the sample rate expected by the STT provider (AssemblyAI).
|
|
12
|
-
*/
|
|
13
|
-
const DEFAULT_STT_SAMPLE_RATE = 16e3;
|
|
14
|
-
/**
|
|
15
|
-
* Default sample rate for text-to-speech audio in Hz.
|
|
16
|
-
*
|
|
17
|
-
* This is the sample rate produced by the TTS provider.
|
|
18
|
-
*/
|
|
19
|
-
const DEFAULT_TTS_SAMPLE_RATE = 24e3;
|
|
20
|
-
/**
|
|
21
|
-
* Audio codec identifier used in the wire protocol.
|
|
22
|
-
*
|
|
23
|
-
* All audio frames are 16-bit signed PCM, little-endian, mono.
|
|
24
|
-
*/
|
|
25
|
-
const AUDIO_FORMAT = "pcm16";
|
|
26
|
-
/** Zod schema for KV operation requests from the worker to the host. */
|
|
27
|
-
const KvRequestSchema = z.discriminatedUnion("op", [
|
|
28
|
-
z.object({
|
|
29
|
-
op: z.literal("get"),
|
|
30
|
-
key: z.string().min(1)
|
|
31
|
-
}),
|
|
32
|
-
z.object({
|
|
33
|
-
op: z.literal("set"),
|
|
34
|
-
key: z.string().min(1),
|
|
35
|
-
value: z.string(),
|
|
36
|
-
expireIn: z.number().int().positive().optional()
|
|
37
|
-
}),
|
|
38
|
-
z.object({
|
|
39
|
-
op: z.literal("del"),
|
|
40
|
-
key: z.string().min(1)
|
|
41
|
-
}),
|
|
42
|
-
z.object({
|
|
43
|
-
op: z.literal("list"),
|
|
44
|
-
prefix: z.string(),
|
|
45
|
-
limit: z.number().int().positive().optional(),
|
|
46
|
-
reverse: z.boolean().optional()
|
|
47
|
-
}),
|
|
48
|
-
z.object({
|
|
49
|
-
op: z.literal("keys"),
|
|
50
|
-
pattern: z.string().optional()
|
|
51
|
-
})
|
|
52
|
-
]);
|
|
53
|
-
/** Default timeout for agent lifecycle hooks (onConnect, onTurn, etc). */
|
|
54
|
-
const HOOK_TIMEOUT_MS = 5e3;
|
|
55
|
-
/** Default timeout for tool execution in the worker. */
|
|
56
|
-
const TOOL_EXECUTION_TIMEOUT_MS = 3e4;
|
|
57
|
-
/** Maximum length for tool result strings sent to clients. */
|
|
58
|
-
const MAX_TOOL_RESULT_CHARS = 4e3;
|
|
59
|
-
/**
|
|
60
|
-
* Zod schema for session error codes.
|
|
61
|
-
* @public
|
|
62
|
-
*/
|
|
63
|
-
const SessionErrorCodeSchema = z.enum([
|
|
64
|
-
"stt",
|
|
65
|
-
"llm",
|
|
66
|
-
"tts",
|
|
67
|
-
"tool",
|
|
68
|
-
"protocol",
|
|
69
|
-
"connection",
|
|
70
|
-
"audio",
|
|
71
|
-
"internal"
|
|
72
|
-
]);
|
|
73
|
-
/** Helper: simple event with only a type field. */
|
|
74
|
-
const ev = (t) => z.object({ type: z.literal(t) });
|
|
75
|
-
/** Helper: event with type + text. */
|
|
76
|
-
const textEv = (t) => z.object({
|
|
77
|
-
type: z.literal(t),
|
|
78
|
-
text: z.string()
|
|
79
|
-
});
|
|
80
|
-
const turnOrder = z.number().int().nonnegative().optional();
|
|
81
|
-
/** Zod schema for {@link ClientEvent}. */
|
|
82
|
-
const ClientEventSchema = z.discriminatedUnion("type", [
|
|
83
|
-
ev("speech_started"),
|
|
84
|
-
ev("speech_stopped"),
|
|
85
|
-
z.object({
|
|
86
|
-
type: z.literal("transcript"),
|
|
87
|
-
text: z.string(),
|
|
88
|
-
isFinal: z.boolean(),
|
|
89
|
-
turnOrder
|
|
90
|
-
}),
|
|
91
|
-
textEv("turn").extend({ turnOrder }),
|
|
92
|
-
textEv("chat"),
|
|
93
|
-
textEv("chat_delta"),
|
|
94
|
-
z.object({
|
|
95
|
-
type: z.literal("tool_call_start"),
|
|
96
|
-
toolCallId: z.string(),
|
|
97
|
-
toolName: z.string(),
|
|
98
|
-
args: z.record(z.string(), z.unknown())
|
|
99
|
-
}),
|
|
100
|
-
z.object({
|
|
101
|
-
type: z.literal("tool_call_update"),
|
|
102
|
-
toolCallId: z.string(),
|
|
103
|
-
data: z.string().max(MAX_TOOL_RESULT_CHARS)
|
|
104
|
-
}),
|
|
105
|
-
z.object({
|
|
106
|
-
type: z.literal("tool_call_done"),
|
|
107
|
-
toolCallId: z.string(),
|
|
108
|
-
result: z.string().max(MAX_TOOL_RESULT_CHARS)
|
|
109
|
-
}),
|
|
110
|
-
ev("tts_done"),
|
|
111
|
-
ev("cancelled"),
|
|
112
|
-
ev("reset"),
|
|
113
|
-
ev("idle_timeout"),
|
|
114
|
-
z.object({
|
|
115
|
-
type: z.literal("error"),
|
|
116
|
-
code: SessionErrorCodeSchema,
|
|
117
|
-
message: z.string()
|
|
118
|
-
})
|
|
119
|
-
]);
|
|
120
|
-
/** Zod schema for {@link ReadyConfig}. */
|
|
121
|
-
const ReadyConfigSchema = z.object({
|
|
122
|
-
audioFormat: z.enum(["pcm16"]),
|
|
123
|
-
sampleRate: z.number().int().positive(),
|
|
124
|
-
ttsSampleRate: z.number().int().positive()
|
|
125
|
-
});
|
|
126
|
-
/** Zod schema for server→client text messages. */
|
|
127
|
-
const ServerMessageSchema = z.discriminatedUnion("type", [
|
|
128
|
-
z.object({
|
|
129
|
-
type: z.literal("config"),
|
|
130
|
-
audioFormat: z.string(),
|
|
131
|
-
sampleRate: z.number(),
|
|
132
|
-
ttsSampleRate: z.number(),
|
|
133
|
-
sessionId: z.string().optional()
|
|
134
|
-
}),
|
|
135
|
-
ev("audio_done"),
|
|
136
|
-
...ClientEventSchema.options
|
|
137
|
-
]);
|
|
138
|
-
/** Zod schema for client→server text messages. */
|
|
139
|
-
const ClientMessageSchema = z.discriminatedUnion("type", [
|
|
140
|
-
ev("audio_ready"),
|
|
141
|
-
ev("cancel"),
|
|
142
|
-
ev("reset"),
|
|
143
|
-
z.object({
|
|
144
|
-
type: z.literal("history"),
|
|
145
|
-
messages: z.array(z.object({
|
|
146
|
-
role: z.enum(["user", "assistant"]),
|
|
147
|
-
content: z.string().max(1e5)
|
|
148
|
-
})).max(200)
|
|
149
|
-
})
|
|
150
|
-
]);
|
|
151
|
-
/** Build the protocol-level session config from S2S sample rates. */
|
|
152
|
-
function buildReadyConfig(s2sConfig) {
|
|
153
|
-
return {
|
|
154
|
-
audioFormat: AUDIO_FORMAT,
|
|
155
|
-
sampleRate: s2sConfig.inputSampleRate,
|
|
156
|
-
ttsSampleRate: s2sConfig.outputSampleRate
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
/** Zod schema for {@link TurnConfig}. */
|
|
160
|
-
const TurnConfigSchema = z.object({ maxSteps: z.number().int().positive().optional() });
|
|
161
|
-
//#endregion
|
|
1
|
+
import { a as DEFAULT_TTS_SAMPLE_RATE, c as MAX_TOOL_RESULT_CHARS, d as SessionErrorCodeSchema, f as TOOL_EXECUTION_TIMEOUT_MS, i as DEFAULT_STT_SAMPLE_RATE, l as ReadyConfigSchema, m as buildReadyConfig, n as ClientEventSchema, o as HOOK_TIMEOUT_MS, p as TurnConfigSchema, r as ClientMessageSchema, s as KvRequestSchema, t as AUDIO_FORMAT, u as ServerMessageSchema } from "./protocol-B-H2Q4ox.js";
|
|
162
2
|
export { AUDIO_FORMAT, ClientEventSchema, ClientMessageSchema, DEFAULT_STT_SAMPLE_RATE, DEFAULT_TTS_SAMPLE_RATE, HOOK_TIMEOUT_MS, KvRequestSchema, MAX_TOOL_RESULT_CHARS, ReadyConfigSchema, ServerMessageSchema, SessionErrorCodeSchema, TOOL_EXECUTION_TIMEOUT_MS, TurnConfigSchema, buildReadyConfig };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { a as DEFAULT_TTS_SAMPLE_RATE, i as DEFAULT_STT_SAMPLE_RATE } from "./protocol-B-H2Q4ox.js";
|
|
2
|
+
//#region runtime.ts
|
|
3
|
+
/**
|
|
4
|
+
* Runtime dependencies injected into the session pipeline.
|
|
5
|
+
*
|
|
6
|
+
* Defines the {@link Logger} interface, a default {@link consoleLogger},
|
|
7
|
+
* and the {@link S2SConfig} for Speech-to-Speech endpoint configuration.
|
|
8
|
+
*/
|
|
9
|
+
let _otel;
|
|
10
|
+
try {
|
|
11
|
+
_otel = await import("@opentelemetry/api");
|
|
12
|
+
} catch {}
|
|
13
|
+
/** Default console-backed logger. */
|
|
14
|
+
const consoleLogger = {
|
|
15
|
+
info: (msg, ctx) => ctx ? console.log(msg, ctx) : console.log(msg),
|
|
16
|
+
warn: (msg, ctx) => ctx ? console.warn(msg, ctx) : console.warn(msg),
|
|
17
|
+
error: (msg, ctx) => ctx ? console.error(msg, ctx) : console.error(msg),
|
|
18
|
+
debug: (msg, ctx) => ctx ? console.debug(msg, ctx) : console.debug(msg)
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Structured JSON logger for production diagnostics. Each log entry is a
|
|
22
|
+
* single-line JSON object with `timestamp`, `level`, `msg`, and any
|
|
23
|
+
* caller-provided context fields. When an active OpenTelemetry span exists,
|
|
24
|
+
* `trace_id` and `span_id` are included automatically.
|
|
25
|
+
*/
|
|
26
|
+
function jsonLog(level) {
|
|
27
|
+
return (msg, ctx) => {
|
|
28
|
+
const entry = {
|
|
29
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
30
|
+
level,
|
|
31
|
+
msg
|
|
32
|
+
};
|
|
33
|
+
if (_otel) {
|
|
34
|
+
const span = _otel.trace.getSpan(_otel.context.active());
|
|
35
|
+
if (span) {
|
|
36
|
+
const sc = span.spanContext();
|
|
37
|
+
entry.trace_id = sc.traceId;
|
|
38
|
+
entry.span_id = sc.spanId;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (ctx) Object.assign(entry, ctx);
|
|
42
|
+
(level === "error" || level === "warn" ? process.stderr : process.stdout).write(`${JSON.stringify(entry)}\n`);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const jsonLogger = {
|
|
46
|
+
info: jsonLog("info"),
|
|
47
|
+
warn: jsonLog("warn"),
|
|
48
|
+
error: jsonLog("error"),
|
|
49
|
+
debug: jsonLog("debug")
|
|
50
|
+
};
|
|
51
|
+
/** Default S2S endpoint configuration. */
|
|
52
|
+
const DEFAULT_S2S_CONFIG = {
|
|
53
|
+
wssUrl: "wss://speech-to-speech.us.assemblyai.com/v1/realtime",
|
|
54
|
+
inputSampleRate: DEFAULT_STT_SAMPLE_RATE,
|
|
55
|
+
outputSampleRate: DEFAULT_TTS_SAMPLE_RATE
|
|
56
|
+
};
|
|
57
|
+
//#endregion
|
|
58
|
+
export { consoleLogger as n, jsonLogger as r, DEFAULT_S2S_CONFIG as t };
|
package/dist/runtime.js
CHANGED
|
@@ -1,53 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { context, trace } from "@opentelemetry/api";
|
|
3
|
-
//#region runtime.ts
|
|
4
|
-
/**
|
|
5
|
-
* Runtime dependencies injected into the session pipeline.
|
|
6
|
-
*
|
|
7
|
-
* Defines the {@link Logger} interface, a default {@link consoleLogger},
|
|
8
|
-
* and the {@link S2SConfig} for Speech-to-Speech endpoint configuration.
|
|
9
|
-
*/
|
|
10
|
-
/** Default console-backed logger. */
|
|
11
|
-
const consoleLogger = {
|
|
12
|
-
info: (msg, ctx) => ctx ? console.log(msg, ctx) : console.log(msg),
|
|
13
|
-
warn: (msg, ctx) => ctx ? console.warn(msg, ctx) : console.warn(msg),
|
|
14
|
-
error: (msg, ctx) => ctx ? console.error(msg, ctx) : console.error(msg),
|
|
15
|
-
debug: (msg, ctx) => ctx ? console.debug(msg, ctx) : console.debug(msg)
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Structured JSON logger for production diagnostics. Each log entry is a
|
|
19
|
-
* single-line JSON object with `timestamp`, `level`, `msg`, and any
|
|
20
|
-
* caller-provided context fields. When an active OpenTelemetry span exists,
|
|
21
|
-
* `trace_id` and `span_id` are included automatically.
|
|
22
|
-
*/
|
|
23
|
-
function jsonLog(level) {
|
|
24
|
-
return (msg, ctx) => {
|
|
25
|
-
const entry = {
|
|
26
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
27
|
-
level,
|
|
28
|
-
msg
|
|
29
|
-
};
|
|
30
|
-
const span = trace.getSpan(context.active());
|
|
31
|
-
if (span) {
|
|
32
|
-
const sc = span.spanContext();
|
|
33
|
-
entry.trace_id = sc.traceId;
|
|
34
|
-
entry.span_id = sc.spanId;
|
|
35
|
-
}
|
|
36
|
-
if (ctx) Object.assign(entry, ctx);
|
|
37
|
-
(level === "error" || level === "warn" ? process.stderr : process.stdout).write(`${JSON.stringify(entry)}\n`);
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
const jsonLogger = {
|
|
41
|
-
info: jsonLog("info"),
|
|
42
|
-
warn: jsonLog("warn"),
|
|
43
|
-
error: jsonLog("error"),
|
|
44
|
-
debug: jsonLog("debug")
|
|
45
|
-
};
|
|
46
|
-
/** Default S2S endpoint configuration. */
|
|
47
|
-
const DEFAULT_S2S_CONFIG = {
|
|
48
|
-
wssUrl: "wss://speech-to-speech.us.assemblyai.com/v1/realtime",
|
|
49
|
-
inputSampleRate: DEFAULT_STT_SAMPLE_RATE,
|
|
50
|
-
outputSampleRate: DEFAULT_TTS_SAMPLE_RATE
|
|
51
|
-
};
|
|
52
|
-
//#endregion
|
|
1
|
+
import { n as consoleLogger, r as jsonLogger, t as DEFAULT_S2S_CONFIG } from "./runtime-CxcwaK68.js";
|
|
53
2
|
export { DEFAULT_S2S_CONFIG, consoleLogger, jsonLogger };
|