@alexkroman1/aai 0.9.2 → 0.10.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/_internal-types.d.ts +49 -22
- package/dist/_internal-types.js +43 -1
- package/dist/_mock-ws.d.ts +1 -2
- package/dist/_run-code.d.ts +31 -0
- package/dist/_session-ctx.d.ts +73 -0
- package/dist/_session-otel.d.ts +43 -0
- package/dist/_session-persist.d.ts +30 -0
- package/dist/_ssrf.d.ts +30 -0
- package/dist/_ssrf.js +123 -0
- package/dist/_utils.d.ts +25 -0
- package/dist/_utils.js +54 -1
- package/dist/builtin-tools.d.ts +5 -34
- package/dist/direct-executor-Ca0wt5H0.js +572 -0
- package/dist/direct-executor.d.ts +34 -5
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -2
- package/dist/kv.d.ts +30 -38
- package/dist/kv.js +19 -86
- package/dist/matchers.d.ts +20 -0
- package/dist/matchers.js +41 -0
- package/dist/memory-tools.d.ts +39 -0
- package/dist/middleware-core.d.ts +47 -0
- package/dist/middleware-core.js +107 -0
- package/dist/middleware.d.ts +37 -0
- package/dist/protocol.d.ts +44 -24
- package/dist/protocol.js +34 -14
- package/dist/runtime.d.ts +26 -2
- package/dist/runtime.js +44 -7
- package/dist/s2s.d.ts +19 -29
- package/dist/s2s.js +117 -87
- package/dist/server.d.ts +31 -3
- package/dist/server.js +102 -28
- package/dist/session-BkN9u0ni.js +683 -0
- package/dist/session.d.ts +55 -28
- package/dist/session.js +2 -312
- package/dist/sqlite-kv.d.ts +34 -0
- package/dist/sqlite-kv.js +133 -0
- package/dist/sqlite-vector.d.ts +58 -0
- package/dist/sqlite-vector.js +149 -0
- package/dist/system-prompt.d.ts +21 -0
- package/dist/telemetry.d.ts +49 -0
- package/dist/telemetry.js +95 -0
- package/dist/testing-MRl3SXsI.js +519 -0
- package/dist/testing.d.ts +299 -0
- package/dist/testing.js +2 -0
- package/dist/types.d.ts +324 -39
- package/dist/types.js +62 -9
- package/dist/vector.d.ts +18 -22
- package/dist/vector.js +41 -48
- package/dist/worker-entry.d.ts +11 -3
- package/dist/worker-entry.js +19 -8
- package/dist/ws-handler.d.ts +7 -3
- package/dist/ws-handler.js +64 -12
- package/package.json +55 -8
- package/dist/_mock-ws.js +0 -158
- package/dist/builtin-tools.js +0 -270
- package/dist/direct-executor.js +0 -125
package/dist/protocol.d.ts
CHANGED
|
@@ -21,15 +21,7 @@ export declare const DEFAULT_TTS_SAMPLE_RATE = 24000;
|
|
|
21
21
|
*
|
|
22
22
|
* All audio frames are 16-bit signed PCM, little-endian, mono.
|
|
23
23
|
*/
|
|
24
|
-
export declare const AUDIO_FORMAT
|
|
25
|
-
/** Specification for binary audio frames exchanged over WebSocket. */
|
|
26
|
-
export declare const AudioFrameSpec: {
|
|
27
|
-
readonly format: "pcm16";
|
|
28
|
-
readonly bitsPerSample: 16;
|
|
29
|
-
readonly endianness: "little";
|
|
30
|
-
readonly channels: 1;
|
|
31
|
-
readonly bytesPerSample: 2;
|
|
32
|
-
};
|
|
24
|
+
export declare const AUDIO_FORMAT = "pcm16";
|
|
33
25
|
/** Zod schema for KV operation requests from the worker to the host. */
|
|
34
26
|
export declare const KvRequestSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
35
27
|
op: z.ZodLiteral<"get">;
|
|
@@ -38,7 +30,7 @@ export declare const KvRequestSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
38
30
|
op: z.ZodLiteral<"set">;
|
|
39
31
|
key: z.ZodString;
|
|
40
32
|
value: z.ZodString;
|
|
41
|
-
|
|
33
|
+
expireIn: z.ZodOptional<z.ZodNumber>;
|
|
42
34
|
}, z.core.$strip>, z.ZodObject<{
|
|
43
35
|
op: z.ZodLiteral<"del">;
|
|
44
36
|
key: z.ZodString;
|
|
@@ -57,14 +49,19 @@ export type KvRequest = z.infer<typeof KvRequestSchema>;
|
|
|
57
49
|
export declare const HOOK_TIMEOUT_MS = 5000;
|
|
58
50
|
/** Default timeout for tool execution in the worker. */
|
|
59
51
|
export declare const TOOL_EXECUTION_TIMEOUT_MS = 30000;
|
|
60
|
-
/**
|
|
52
|
+
/** Maximum length for tool result strings sent to clients. */
|
|
53
|
+
export declare const MAX_TOOL_RESULT_CHARS = 4000;
|
|
54
|
+
/**
|
|
55
|
+
* Zod schema for session error codes.
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
61
58
|
export declare const SessionErrorCodeSchema: z.ZodEnum<{
|
|
62
59
|
tool: "tool";
|
|
63
|
-
connection: "connection";
|
|
64
60
|
stt: "stt";
|
|
65
61
|
llm: "llm";
|
|
66
62
|
tts: "tts";
|
|
67
63
|
protocol: "protocol";
|
|
64
|
+
connection: "connection";
|
|
68
65
|
audio: "audio";
|
|
69
66
|
internal: "internal";
|
|
70
67
|
}>;
|
|
@@ -99,6 +96,10 @@ export declare const ClientEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
99
96
|
toolCallId: z.ZodString;
|
|
100
97
|
toolName: z.ZodString;
|
|
101
98
|
args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
99
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
100
|
+
type: z.ZodLiteral<"tool_call_update">;
|
|
101
|
+
toolCallId: z.ZodString;
|
|
102
|
+
data: z.ZodString;
|
|
102
103
|
}, z.core.$strip>, z.ZodObject<{
|
|
103
104
|
type: z.ZodLiteral<"tool_call_done">;
|
|
104
105
|
toolCallId: z.ZodString;
|
|
@@ -109,15 +110,17 @@ export declare const ClientEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
109
110
|
type: z.ZodLiteral<"cancelled">;
|
|
110
111
|
}, z.core.$strip>, z.ZodObject<{
|
|
111
112
|
type: z.ZodLiteral<"reset">;
|
|
113
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
114
|
+
type: z.ZodLiteral<"idle_timeout">;
|
|
112
115
|
}, z.core.$strip>, z.ZodObject<{
|
|
113
116
|
type: z.ZodLiteral<"error">;
|
|
114
117
|
code: z.ZodEnum<{
|
|
115
118
|
tool: "tool";
|
|
116
|
-
connection: "connection";
|
|
117
119
|
stt: "stt";
|
|
118
120
|
llm: "llm";
|
|
119
121
|
tts: "tts";
|
|
120
122
|
protocol: "protocol";
|
|
123
|
+
connection: "connection";
|
|
121
124
|
audio: "audio";
|
|
122
125
|
internal: "internal";
|
|
123
126
|
}>;
|
|
@@ -142,18 +145,23 @@ export interface ClientSink {
|
|
|
142
145
|
}
|
|
143
146
|
/** Supported audio formats for the wire protocol. */
|
|
144
147
|
export type AudioFormatId = "pcm16";
|
|
148
|
+
/** Zod schema for {@link ReadyConfig}. */
|
|
149
|
+
export declare const ReadyConfigSchema: z.ZodObject<{
|
|
150
|
+
audioFormat: z.ZodEnum<{
|
|
151
|
+
pcm16: "pcm16";
|
|
152
|
+
}>;
|
|
153
|
+
sampleRate: z.ZodNumber;
|
|
154
|
+
ttsSampleRate: z.ZodNumber;
|
|
155
|
+
}, z.core.$strip>;
|
|
145
156
|
/** Protocol-level session config returned to the client on connect. */
|
|
146
|
-
export type ReadyConfig =
|
|
147
|
-
audioFormat: AudioFormatId;
|
|
148
|
-
sampleRate: number;
|
|
149
|
-
ttsSampleRate: number;
|
|
150
|
-
};
|
|
157
|
+
export type ReadyConfig = z.infer<typeof ReadyConfigSchema>;
|
|
151
158
|
/** Zod schema for server→client text messages. */
|
|
152
159
|
export declare const ServerMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
153
160
|
type: z.ZodLiteral<"config">;
|
|
154
161
|
audioFormat: z.ZodString;
|
|
155
162
|
sampleRate: z.ZodNumber;
|
|
156
163
|
ttsSampleRate: z.ZodNumber;
|
|
164
|
+
sessionId: z.ZodOptional<z.ZodString>;
|
|
157
165
|
}, z.core.$strip>, z.ZodObject<{
|
|
158
166
|
type: z.ZodLiteral<"audio_done">;
|
|
159
167
|
}, z.core.$strip>, z.ZodObject<{
|
|
@@ -180,6 +188,10 @@ export declare const ServerMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
180
188
|
toolCallId: z.ZodString;
|
|
181
189
|
toolName: z.ZodString;
|
|
182
190
|
args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
191
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
192
|
+
type: z.ZodLiteral<"tool_call_update">;
|
|
193
|
+
toolCallId: z.ZodString;
|
|
194
|
+
data: z.ZodString;
|
|
183
195
|
}, z.core.$strip>, z.ZodObject<{
|
|
184
196
|
type: z.ZodLiteral<"tool_call_done">;
|
|
185
197
|
toolCallId: z.ZodString;
|
|
@@ -190,15 +202,17 @@ export declare const ServerMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
190
202
|
type: z.ZodLiteral<"cancelled">;
|
|
191
203
|
}, z.core.$strip>, z.ZodObject<{
|
|
192
204
|
type: z.ZodLiteral<"reset">;
|
|
205
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
206
|
+
type: z.ZodLiteral<"idle_timeout">;
|
|
193
207
|
}, z.core.$strip>, z.ZodObject<{
|
|
194
208
|
type: z.ZodLiteral<"error">;
|
|
195
209
|
code: z.ZodEnum<{
|
|
196
210
|
tool: "tool";
|
|
197
|
-
connection: "connection";
|
|
198
211
|
stt: "stt";
|
|
199
212
|
llm: "llm";
|
|
200
213
|
tts: "tts";
|
|
201
214
|
protocol: "protocol";
|
|
215
|
+
connection: "connection";
|
|
202
216
|
audio: "audio";
|
|
203
217
|
internal: "internal";
|
|
204
218
|
}>;
|
|
@@ -220,13 +234,19 @@ export declare const ClientMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
220
234
|
user: "user";
|
|
221
235
|
assistant: "assistant";
|
|
222
236
|
}>;
|
|
223
|
-
|
|
237
|
+
content: z.ZodString;
|
|
224
238
|
}, z.core.$strip>>;
|
|
225
239
|
}, z.core.$strip>], "type">;
|
|
226
240
|
/** Client→server text messages (binary frames carry raw PCM16 audio). */
|
|
227
241
|
export type ClientMessage = z.infer<typeof ClientMessageSchema>;
|
|
242
|
+
/** Build the protocol-level session config from S2S sample rates. */
|
|
243
|
+
export declare function buildReadyConfig(s2sConfig: {
|
|
244
|
+
inputSampleRate: number;
|
|
245
|
+
outputSampleRate: number;
|
|
246
|
+
}): ReadyConfig;
|
|
247
|
+
/** Zod schema for {@link TurnConfig}. */
|
|
248
|
+
export declare const TurnConfigSchema: z.ZodObject<{
|
|
249
|
+
maxSteps: z.ZodOptional<z.ZodNumber>;
|
|
250
|
+
}, z.core.$strip>;
|
|
228
251
|
/** Combined turn configuration resolved from the worker before a turn starts. */
|
|
229
|
-
export type TurnConfig =
|
|
230
|
-
maxSteps?: number;
|
|
231
|
-
activeTools?: string[];
|
|
232
|
-
};
|
|
252
|
+
export type TurnConfig = z.infer<typeof TurnConfigSchema>;
|
package/dist/protocol.js
CHANGED
|
@@ -23,14 +23,6 @@ const DEFAULT_TTS_SAMPLE_RATE = 24e3;
|
|
|
23
23
|
* All audio frames are 16-bit signed PCM, little-endian, mono.
|
|
24
24
|
*/
|
|
25
25
|
const AUDIO_FORMAT = "pcm16";
|
|
26
|
-
/** Specification for binary audio frames exchanged over WebSocket. */
|
|
27
|
-
const AudioFrameSpec = {
|
|
28
|
-
format: AUDIO_FORMAT,
|
|
29
|
-
bitsPerSample: 16,
|
|
30
|
-
endianness: "little",
|
|
31
|
-
channels: 1,
|
|
32
|
-
bytesPerSample: 2
|
|
33
|
-
};
|
|
34
26
|
/** Zod schema for KV operation requests from the worker to the host. */
|
|
35
27
|
const KvRequestSchema = z.discriminatedUnion("op", [
|
|
36
28
|
z.object({
|
|
@@ -41,7 +33,7 @@ const KvRequestSchema = z.discriminatedUnion("op", [
|
|
|
41
33
|
op: z.literal("set"),
|
|
42
34
|
key: z.string().min(1),
|
|
43
35
|
value: z.string(),
|
|
44
|
-
|
|
36
|
+
expireIn: z.number().int().positive().optional()
|
|
45
37
|
}),
|
|
46
38
|
z.object({
|
|
47
39
|
op: z.literal("del"),
|
|
@@ -62,7 +54,12 @@ const KvRequestSchema = z.discriminatedUnion("op", [
|
|
|
62
54
|
const HOOK_TIMEOUT_MS = 5e3;
|
|
63
55
|
/** Default timeout for tool execution in the worker. */
|
|
64
56
|
const TOOL_EXECUTION_TIMEOUT_MS = 3e4;
|
|
65
|
-
/**
|
|
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
|
+
*/
|
|
66
63
|
const SessionErrorCodeSchema = z.enum([
|
|
67
64
|
"stt",
|
|
68
65
|
"llm",
|
|
@@ -100,27 +97,40 @@ const ClientEventSchema = z.discriminatedUnion("type", [
|
|
|
100
97
|
toolName: z.string(),
|
|
101
98
|
args: z.record(z.string(), z.unknown())
|
|
102
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
|
+
}),
|
|
103
105
|
z.object({
|
|
104
106
|
type: z.literal("tool_call_done"),
|
|
105
107
|
toolCallId: z.string(),
|
|
106
|
-
result: z.string().max(
|
|
108
|
+
result: z.string().max(MAX_TOOL_RESULT_CHARS)
|
|
107
109
|
}),
|
|
108
110
|
ev("tts_done"),
|
|
109
111
|
ev("cancelled"),
|
|
110
112
|
ev("reset"),
|
|
113
|
+
ev("idle_timeout"),
|
|
111
114
|
z.object({
|
|
112
115
|
type: z.literal("error"),
|
|
113
116
|
code: SessionErrorCodeSchema,
|
|
114
117
|
message: z.string()
|
|
115
118
|
})
|
|
116
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
|
+
});
|
|
117
126
|
/** Zod schema for server→client text messages. */
|
|
118
127
|
const ServerMessageSchema = z.discriminatedUnion("type", [
|
|
119
128
|
z.object({
|
|
120
129
|
type: z.literal("config"),
|
|
121
130
|
audioFormat: z.string(),
|
|
122
131
|
sampleRate: z.number(),
|
|
123
|
-
ttsSampleRate: z.number()
|
|
132
|
+
ttsSampleRate: z.number(),
|
|
133
|
+
sessionId: z.string().optional()
|
|
124
134
|
}),
|
|
125
135
|
ev("audio_done"),
|
|
126
136
|
...ClientEventSchema.options
|
|
@@ -134,9 +144,19 @@ const ClientMessageSchema = z.discriminatedUnion("type", [
|
|
|
134
144
|
type: z.literal("history"),
|
|
135
145
|
messages: z.array(z.object({
|
|
136
146
|
role: z.enum(["user", "assistant"]),
|
|
137
|
-
|
|
147
|
+
content: z.string().max(1e5)
|
|
138
148
|
})).max(200)
|
|
139
149
|
})
|
|
140
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() });
|
|
141
161
|
//#endregion
|
|
142
|
-
export { AUDIO_FORMAT,
|
|
162
|
+
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 };
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,17 +1,41 @@
|
|
|
1
1
|
/** Structured context attached to log messages. */
|
|
2
2
|
export type LogContext = Record<string, unknown>;
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Structured logger interface. Used by tests to suppress output and by
|
|
5
|
+
* consumers to plug in custom logging backends.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const myLogger: Logger = {
|
|
10
|
+
* info: (msg, ctx) => myBackend.log("info", msg, ctx),
|
|
11
|
+
* warn: (msg, ctx) => myBackend.log("warn", msg, ctx),
|
|
12
|
+
* error: (msg, ctx) => myBackend.log("error", msg, ctx),
|
|
13
|
+
* debug: (msg, ctx) => myBackend.log("debug", msg, ctx),
|
|
14
|
+
* };
|
|
15
|
+
* createServer({ agent, logger: myLogger });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
4
18
|
export type Logger = {
|
|
5
19
|
info(msg: string, ctx?: LogContext): void;
|
|
6
20
|
warn(msg: string, ctx?: LogContext): void;
|
|
7
21
|
error(msg: string, ctx?: LogContext): void;
|
|
8
22
|
debug(msg: string, ctx?: LogContext): void;
|
|
9
23
|
};
|
|
24
|
+
/** Default console-backed logger. */
|
|
10
25
|
export declare const consoleLogger: Logger;
|
|
11
|
-
|
|
26
|
+
export declare const jsonLogger: Logger;
|
|
27
|
+
/**
|
|
28
|
+
* Speech-to-Speech (S2S) endpoint configuration.
|
|
29
|
+
*
|
|
30
|
+
* Controls which AssemblyAI real-time WebSocket endpoint to connect to and
|
|
31
|
+
* the audio sample rates for input (microphone → STT) and output (TTS → speaker).
|
|
32
|
+
*/
|
|
12
33
|
export type S2SConfig = {
|
|
34
|
+
/** The WebSocket URL of the S2S real-time endpoint. */
|
|
13
35
|
wssUrl: string;
|
|
36
|
+
/** Sample rate in Hz for audio sent to STT (microphone capture). */
|
|
14
37
|
inputSampleRate: number;
|
|
38
|
+
/** Sample rate in Hz for TTS audio received from the server. */
|
|
15
39
|
outputSampleRate: number;
|
|
16
40
|
};
|
|
17
41
|
/** Default S2S endpoint configuration. */
|
package/dist/runtime.js
CHANGED
|
@@ -1,16 +1,53 @@
|
|
|
1
|
-
import { DEFAULT_TTS_SAMPLE_RATE } from "./protocol.js";
|
|
1
|
+
import { DEFAULT_STT_SAMPLE_RATE, DEFAULT_TTS_SAMPLE_RATE } from "./protocol.js";
|
|
2
|
+
import { context, trace } from "@opentelemetry/api";
|
|
2
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. */
|
|
3
11
|
const consoleLogger = {
|
|
4
|
-
info: (msg, ctx) => console.log(msg,
|
|
5
|
-
warn: (msg, ctx) => console.warn(msg,
|
|
6
|
-
error: (msg, ctx) => console.error(msg,
|
|
7
|
-
debug: (msg, ctx) => console.debug(msg,
|
|
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")
|
|
8
45
|
};
|
|
9
46
|
/** Default S2S endpoint configuration. */
|
|
10
47
|
const DEFAULT_S2S_CONFIG = {
|
|
11
48
|
wssUrl: "wss://speech-to-speech.us.assemblyai.com/v1/realtime",
|
|
12
|
-
inputSampleRate:
|
|
49
|
+
inputSampleRate: DEFAULT_STT_SAMPLE_RATE,
|
|
13
50
|
outputSampleRate: DEFAULT_TTS_SAMPLE_RATE
|
|
14
51
|
};
|
|
15
52
|
//#endregion
|
|
16
|
-
export { DEFAULT_S2S_CONFIG, consoleLogger };
|
|
53
|
+
export { DEFAULT_S2S_CONFIG, consoleLogger, jsonLogger };
|
package/dist/s2s.d.ts
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
import type { JSONSchema7 } from "json-schema";
|
|
5
5
|
import { type Unsubscribe } from "nanoevents";
|
|
6
6
|
import type { Logger, S2SConfig } from "./runtime.ts";
|
|
7
|
-
/** Minimal WebSocket interface for the S2S client. */
|
|
8
7
|
export type S2sWebSocket = {
|
|
9
8
|
readonly readyState: number;
|
|
10
9
|
send(data: string): void;
|
|
@@ -21,14 +20,12 @@ export type S2sWebSocket = {
|
|
|
21
20
|
message?: string;
|
|
22
21
|
}) => void): void;
|
|
23
22
|
};
|
|
24
|
-
/** Factory for creating WebSocket connections (e.g. the `ws` package). */
|
|
25
23
|
export type CreateS2sWebSocket = (url: string, opts: {
|
|
26
24
|
headers: Record<string, string>;
|
|
27
25
|
}) => S2sWebSocket;
|
|
28
|
-
/** Default S2S WebSocket factory using the `ws` package (Node-only). */
|
|
29
26
|
export declare const defaultCreateS2sWebSocket: CreateS2sWebSocket;
|
|
30
27
|
export type S2sSessionConfig = {
|
|
31
|
-
|
|
28
|
+
systemPrompt: string;
|
|
32
29
|
tools: S2sToolSchema[];
|
|
33
30
|
greeting?: string;
|
|
34
31
|
};
|
|
@@ -39,40 +36,42 @@ export type S2sToolSchema = {
|
|
|
39
36
|
parameters: JSONSchema7;
|
|
40
37
|
};
|
|
41
38
|
export type S2sToolCall = {
|
|
42
|
-
|
|
39
|
+
callId: string;
|
|
43
40
|
name: string;
|
|
44
41
|
args: Record<string, unknown>;
|
|
45
42
|
};
|
|
46
|
-
/** Typed event map for S2S handle events. */
|
|
47
43
|
export type S2sEvents = {
|
|
48
44
|
ready: (detail: {
|
|
49
|
-
|
|
45
|
+
sessionId: string;
|
|
50
46
|
}) => void;
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
sessionUpdated: (detail: Record<string, unknown>) => void;
|
|
48
|
+
sessionExpired: (detail: {
|
|
53
49
|
code: string;
|
|
54
50
|
message: string;
|
|
55
51
|
}) => void;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
speechStarted: () => void;
|
|
53
|
+
speechStopped: () => void;
|
|
54
|
+
userTranscriptDelta: (detail: {
|
|
59
55
|
text: string;
|
|
60
56
|
}) => void;
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
userTranscript: (detail: {
|
|
58
|
+
itemId: string;
|
|
63
59
|
text: string;
|
|
64
60
|
}) => void;
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
replyStarted: (detail: {
|
|
62
|
+
replyId: string;
|
|
67
63
|
}) => void;
|
|
68
|
-
|
|
64
|
+
agentTranscriptDelta: (detail: {
|
|
69
65
|
text: string;
|
|
70
66
|
}) => void;
|
|
71
|
-
|
|
67
|
+
agentTranscript: (detail: {
|
|
72
68
|
text: string;
|
|
69
|
+
replyId: string;
|
|
70
|
+
itemId: string;
|
|
71
|
+
interrupted: boolean;
|
|
73
72
|
}) => void;
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
toolCall: (detail: S2sToolCall) => void;
|
|
74
|
+
replyDone: (detail: {
|
|
76
75
|
status?: string;
|
|
77
76
|
}) => void;
|
|
78
77
|
audio: (detail: {
|
|
@@ -98,13 +97,4 @@ export type ConnectS2sOptions = {
|
|
|
98
97
|
createWebSocket: CreateS2sWebSocket;
|
|
99
98
|
logger?: Logger;
|
|
100
99
|
};
|
|
101
|
-
/**
|
|
102
|
-
* Connect to AssemblyAI's Speech-to-Speech WebSocket API.
|
|
103
|
-
*
|
|
104
|
-
* Returns an {@link S2sHandle} with a typed `on()` method.
|
|
105
|
-
* Consumers listen for events: `ready`, `speech_started`, `speech_stopped`,
|
|
106
|
-
* `user_transcript_delta`, `user_transcript`, `reply_started`,
|
|
107
|
-
* `reply_done`, `audio`, `agent_transcript`, `tool_call`,
|
|
108
|
-
* `session_expired`, `error`, `close`.
|
|
109
|
-
*/
|
|
110
100
|
export declare function connectS2s(opts: ConnectS2sOptions): Promise<S2sHandle>;
|