@adminforth/agent 1.43.29 → 1.44.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.
@@ -0,0 +1,261 @@
1
+ import { randomUUID } from "crypto";
2
+
3
+ import type { AgentEvent, AgentEventEmitter } from "../../agentEvents.js";
4
+ import type { ToolCallEvent } from "../../agent/toolCallEvents.js";
5
+
6
+ type AgentEventStreamResponse = {
7
+ writeHead: (statusCode: number, headers: Record<string, string>) => void;
8
+ write: (chunk: string) => unknown;
9
+ end: () => unknown;
10
+ writableEnded: boolean;
11
+ destroyed: boolean;
12
+ };
13
+
14
+ type AgentEventStreamOptions = {
15
+ vercelAiUiMessageStream?: boolean;
16
+ closeActiveBlockOnToolStart?: boolean;
17
+ };
18
+
19
+ function createAgentEventStream(
20
+ res: AgentEventStreamResponse,
21
+ options: AgentEventStreamOptions = {},
22
+ ) {
23
+ let isStreamClosed = false;
24
+ let activeBlock: { type: "text" | "reasoning"; id: string } | null = null;
25
+
26
+ res.writeHead(200, {
27
+ "Content-Type": "text/event-stream",
28
+ "Cache-Control": "no-cache",
29
+ "Connection": "keep-alive",
30
+ ...(options.vercelAiUiMessageStream
31
+ ? { "x-vercel-ai-ui-message-stream": "v1" }
32
+ : {}),
33
+ });
34
+
35
+ const stream = {
36
+ send(obj: unknown) {
37
+ if (isStreamClosed || res.writableEnded || res.destroyed) {
38
+ return;
39
+ }
40
+
41
+ res.write(`data: ${JSON.stringify(obj)}\n\n`);
42
+ },
43
+
44
+ endActiveBlock() {
45
+ if (!activeBlock) {
46
+ return;
47
+ }
48
+
49
+ stream.send({
50
+ type: `${activeBlock.type}-end`,
51
+ id: activeBlock.id,
52
+ });
53
+
54
+ activeBlock = null;
55
+ },
56
+
57
+ startBlock(type: "text" | "reasoning") {
58
+ if (activeBlock?.type === type) {
59
+ return activeBlock.id;
60
+ }
61
+
62
+ stream.endActiveBlock();
63
+
64
+ const id = randomUUID();
65
+ activeBlock = { type, id };
66
+
67
+ stream.send({
68
+ type: `${type}-start`,
69
+ id,
70
+ });
71
+
72
+ return id;
73
+ },
74
+
75
+ start(messageId: string) {
76
+ stream.send({
77
+ type: "start",
78
+ messageId,
79
+ });
80
+ },
81
+
82
+ textDelta(delta: string) {
83
+ const textId = stream.startBlock("text");
84
+ stream.send({
85
+ type: "text-delta",
86
+ id: textId,
87
+ delta,
88
+ });
89
+ },
90
+
91
+ reasoningDelta(delta: string) {
92
+ const reasoningId = stream.startBlock("reasoning");
93
+ stream.send({
94
+ type: "reasoning-delta",
95
+ id: reasoningId,
96
+ delta,
97
+ });
98
+ },
99
+
100
+ toolCall(event: ToolCallEvent) {
101
+ if (options.closeActiveBlockOnToolStart && event.phase === "start") {
102
+ stream.endActiveBlock();
103
+ }
104
+
105
+ stream.send({
106
+ type: "data-tool-call",
107
+ data: event,
108
+ });
109
+ },
110
+
111
+ transcript(text: string, language?: string) {
112
+ stream.send({
113
+ type: "transcript",
114
+ data: {
115
+ text,
116
+ language,
117
+ },
118
+ });
119
+ },
120
+
121
+ response(text: string, sessionId: string, turnId: string) {
122
+ stream.send({
123
+ type: "response",
124
+ data: {
125
+ text,
126
+ sessionId,
127
+ turnId,
128
+ },
129
+ });
130
+ },
131
+
132
+ speechResponse(
133
+ transcript: { text: string; language?: string },
134
+ response: { text: string },
135
+ sessionId: string,
136
+ turnId: string,
137
+ ) {
138
+ stream.send({
139
+ type: "speech-response",
140
+ data: {
141
+ transcript,
142
+ response,
143
+ sessionId,
144
+ turnId,
145
+ },
146
+ });
147
+ },
148
+
149
+ audioStart(
150
+ mimeType: string,
151
+ format: string,
152
+ sampleRate: number,
153
+ channelCount: number,
154
+ bitsPerSample: number,
155
+ ) {
156
+ stream.send({
157
+ type: "audio-start",
158
+ data: {
159
+ mimeType,
160
+ format,
161
+ sampleRate,
162
+ channelCount,
163
+ bitsPerSample,
164
+ },
165
+ });
166
+ },
167
+
168
+ audioDelta(value: Uint8Array) {
169
+ stream.send({
170
+ type: "audio-delta",
171
+ data: {
172
+ base64: Buffer.from(value).toString("base64"),
173
+ },
174
+ });
175
+ },
176
+
177
+ audioDone() {
178
+ stream.send({
179
+ type: "audio-done",
180
+ });
181
+ },
182
+
183
+ error(error: string) {
184
+ stream.send({
185
+ type: "error",
186
+ error,
187
+ });
188
+ },
189
+
190
+ end() {
191
+ if (isStreamClosed || res.writableEnded || res.destroyed) {
192
+ return;
193
+ }
194
+
195
+ stream.endActiveBlock();
196
+ stream.send({
197
+ type: "finish",
198
+ });
199
+
200
+ res.write("data: [DONE]\n\n");
201
+ isStreamClosed = true;
202
+ res.end();
203
+ },
204
+ };
205
+
206
+ return stream;
207
+ }
208
+
209
+ export function createSseEventEmitter(
210
+ res: AgentEventStreamResponse,
211
+ options: AgentEventStreamOptions = {},
212
+ ): AgentEventEmitter {
213
+ const stream = createAgentEventStream(res, options);
214
+
215
+ return async (event: AgentEvent) => {
216
+ switch (event.type) {
217
+ case "turn-started":
218
+ stream.start(event.messageId);
219
+ break;
220
+ case "text-delta":
221
+ stream.textDelta(event.delta);
222
+ break;
223
+ case "reasoning-delta":
224
+ stream.reasoningDelta(event.delta);
225
+ break;
226
+ case "tool-call":
227
+ stream.toolCall(event.data);
228
+ break;
229
+ case "transcript":
230
+ stream.transcript(event.text, event.language);
231
+ break;
232
+ case "response":
233
+ stream.response(event.text, event.sessionId, event.turnId);
234
+ break;
235
+ case "speech-response":
236
+ stream.speechResponse(event.transcript, event.response, event.sessionId, event.turnId);
237
+ break;
238
+ case "audio-start":
239
+ stream.audioStart(
240
+ event.mimeType,
241
+ event.format,
242
+ event.sampleRate,
243
+ event.channelCount,
244
+ event.bitsPerSample,
245
+ );
246
+ break;
247
+ case "audio-delta":
248
+ stream.audioDelta(event.value);
249
+ break;
250
+ case "audio-done":
251
+ stream.audioDone();
252
+ break;
253
+ case "error":
254
+ stream.error(event.error);
255
+ break;
256
+ case "finish":
257
+ stream.end();
258
+ break;
259
+ }
260
+ };
261
+ }
package/tsconfig.json CHANGED
@@ -3,6 +3,7 @@
3
3
  "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include*/
4
4
  "module": "node16", /* Specify what module code is generated. */
5
5
  "outDir": "./dist", /* Specify an output folder for all emitted files. */
6
+ "declaration": true,
6
7
  "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. */
7
8
  "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
8
9
  "strict": true, /* Enable all strict type-checking options. */
package/types.ts CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  type AdminUser,
4
4
  type HttpExtra,
5
5
  type AudioAdapter,
6
+ type ChatSurfaceAdapter,
6
7
  } from "adminforth";
7
8
  import type { AgentModeCompletionAdapter } from "./agent/simpleAgent.js";
8
9
 
@@ -67,6 +68,11 @@ export interface PluginOptions extends PluginsCommonOptions {
67
68
  */
68
69
  audioAdapter?: AudioAdapter;
69
70
 
71
+ /**
72
+ * Optional external chat surfaces, such as Telegram webhooks.
73
+ */
74
+ chatSurfaceAdapters?: ChatSurfaceAdapter[];
75
+
70
76
  /**
71
77
  * Max tokens for the generation.
72
78
  * Default is 1000