@witqq/agent-sdk 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +433 -6
- package/dist/auth/index.cjs +188 -1
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +154 -138
- package/dist/auth/index.d.ts +154 -138
- package/dist/auth/index.js +188 -2
- package/dist/auth/index.js.map +1 -1
- package/dist/backends/claude.cjs +315 -21
- package/dist/backends/claude.cjs.map +1 -1
- package/dist/backends/claude.d.cts +2 -1
- package/dist/backends/claude.d.ts +2 -1
- package/dist/backends/claude.js +315 -21
- package/dist/backends/claude.js.map +1 -1
- package/dist/backends/copilot.cjs +132 -24
- package/dist/backends/copilot.cjs.map +1 -1
- package/dist/backends/copilot.d.cts +2 -1
- package/dist/backends/copilot.d.ts +2 -1
- package/dist/backends/copilot.js +132 -24
- package/dist/backends/copilot.js.map +1 -1
- package/dist/backends/vercel-ai.cjs +56 -17
- package/dist/backends/vercel-ai.cjs.map +1 -1
- package/dist/backends/vercel-ai.d.cts +1 -1
- package/dist/backends/vercel-ai.d.ts +1 -1
- package/dist/backends/vercel-ai.js +56 -17
- package/dist/backends/vercel-ai.js.map +1 -1
- package/dist/chat/accumulator.cjs +147 -0
- package/dist/chat/accumulator.cjs.map +1 -0
- package/dist/chat/accumulator.d.cts +61 -0
- package/dist/chat/accumulator.d.ts +61 -0
- package/dist/chat/accumulator.js +145 -0
- package/dist/chat/accumulator.js.map +1 -0
- package/dist/chat/backends.cjs +3534 -0
- package/dist/chat/backends.cjs.map +1 -0
- package/dist/chat/backends.d.cts +62 -0
- package/dist/chat/backends.d.ts +62 -0
- package/dist/chat/backends.js +3501 -0
- package/dist/chat/backends.js.map +1 -0
- package/dist/chat/context.cjs +230 -0
- package/dist/chat/context.cjs.map +1 -0
- package/dist/chat/context.d.cts +167 -0
- package/dist/chat/context.d.ts +167 -0
- package/dist/chat/context.js +227 -0
- package/dist/chat/context.js.map +1 -0
- package/dist/chat/core.cjs +282 -0
- package/dist/chat/core.cjs.map +1 -0
- package/dist/chat/core.d.cts +435 -0
- package/dist/chat/core.d.ts +435 -0
- package/dist/chat/core.js +261 -0
- package/dist/chat/core.js.map +1 -0
- package/dist/chat/errors.cjs +251 -0
- package/dist/chat/errors.cjs.map +1 -0
- package/dist/chat/errors.d.cts +122 -0
- package/dist/chat/errors.d.ts +122 -0
- package/dist/chat/errors.js +243 -0
- package/dist/chat/errors.js.map +1 -0
- package/dist/chat/events.cjs +203 -0
- package/dist/chat/events.cjs.map +1 -0
- package/dist/chat/events.d.cts +241 -0
- package/dist/chat/events.d.ts +241 -0
- package/dist/chat/events.js +196 -0
- package/dist/chat/events.js.map +1 -0
- package/dist/chat/index.cjs +5359 -0
- package/dist/chat/index.cjs.map +1 -0
- package/dist/chat/index.d.cts +52 -0
- package/dist/chat/index.d.ts +52 -0
- package/dist/chat/index.js +5296 -0
- package/dist/chat/index.js.map +1 -0
- package/dist/chat/react.cjs +2739 -0
- package/dist/chat/react.cjs.map +1 -0
- package/dist/chat/react.d.cts +619 -0
- package/dist/chat/react.d.ts +619 -0
- package/dist/chat/react.js +2714 -0
- package/dist/chat/react.js.map +1 -0
- package/dist/chat/runtime.cjs +1030 -0
- package/dist/chat/runtime.cjs.map +1 -0
- package/dist/chat/runtime.d.cts +118 -0
- package/dist/chat/runtime.d.ts +118 -0
- package/dist/chat/runtime.js +1028 -0
- package/dist/chat/runtime.js.map +1 -0
- package/dist/chat/server.cjs +643 -0
- package/dist/chat/server.cjs.map +1 -0
- package/dist/chat/server.d.cts +287 -0
- package/dist/chat/server.d.ts +287 -0
- package/dist/chat/server.js +617 -0
- package/dist/chat/server.js.map +1 -0
- package/dist/chat/sessions.cjs +398 -0
- package/dist/chat/sessions.cjs.map +1 -0
- package/dist/chat/sessions.d.cts +239 -0
- package/dist/chat/sessions.d.ts +239 -0
- package/dist/chat/sessions.js +394 -0
- package/dist/chat/sessions.js.map +1 -0
- package/dist/chat/state.cjs +177 -0
- package/dist/chat/state.cjs.map +1 -0
- package/dist/chat/state.d.cts +92 -0
- package/dist/chat/state.d.ts +92 -0
- package/dist/chat/state.js +167 -0
- package/dist/chat/state.js.map +1 -0
- package/dist/chat/storage.cjs +240 -0
- package/dist/chat/storage.cjs.map +1 -0
- package/dist/chat/storage.d.cts +191 -0
- package/dist/chat/storage.d.ts +191 -0
- package/dist/chat/storage.js +236 -0
- package/dist/chat/storage.js.map +1 -0
- package/dist/errors-BDLbNu9w.d.cts +13 -0
- package/dist/errors-BDLbNu9w.d.ts +13 -0
- package/dist/in-process-transport-C2oPTYs6.d.ts +223 -0
- package/dist/in-process-transport-DG-w5G6k.d.cts +223 -0
- package/dist/index.cjs +25 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -4
- package/dist/index.d.ts +32 -4
- package/dist/index.js +25 -13
- package/dist/index.js.map +1 -1
- package/dist/transport-D1OaUgRk.d.ts +67 -0
- package/dist/transport-DX1Nhm4N.d.cts +67 -0
- package/dist/types-Bh5AhqD-.d.ts +141 -0
- package/dist/types-CGF7AEX1.d.cts +141 -0
- package/dist/{types-BvwNzZCj.d.cts → types-CqvUAYxt.d.cts} +21 -3
- package/dist/{types-BvwNzZCj.d.ts → types-CqvUAYxt.d.ts} +21 -3
- package/dist/types-DLZzlJxt.d.ts +39 -0
- package/dist/types-tE0CXwBl.d.cts +39 -0
- package/package.json +149 -2
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import { T as ToolDefinition, d as ModelInfo, U as UsageData, A as AgentEvent, M as Message, e as ToolResult } from '../types-CqvUAYxt.js';
|
|
2
|
+
import 'zod';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @witqq/agent-sdk/chat/core
|
|
6
|
+
*
|
|
7
|
+
* Foundational chat types and utilities: ChatMessage, ChatSession, ChatEvent,
|
|
8
|
+
* IChatProvider, type guards, and AgentEvent↔ChatEvent bridge functions.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Branded type for unique identifiers */
|
|
12
|
+
type ChatId = string & {
|
|
13
|
+
readonly __brand: "ChatId";
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Generate a new unique ChatId (crypto.randomUUID-based)
|
|
17
|
+
* @returns Branded ChatId string
|
|
18
|
+
*/
|
|
19
|
+
declare function createChatId(): ChatId;
|
|
20
|
+
/**
|
|
21
|
+
* Cast a string to ChatId with UUID format validation.
|
|
22
|
+
* Use this instead of manual `as ChatId` type assertions.
|
|
23
|
+
*
|
|
24
|
+
* @param value - String to validate and cast
|
|
25
|
+
* @returns Branded ChatId
|
|
26
|
+
* @throws {TypeError} If value is not a valid UUID v4 format
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const id = toChatId("550e8400-e29b-41d4-a716-446655440000");
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare function toChatId(value: string): ChatId;
|
|
34
|
+
/**
|
|
35
|
+
* Accepts either a plain string or branded ChatId for API convenience.
|
|
36
|
+
* Use this in public API signatures so consumers don't need `as ChatId` casts.
|
|
37
|
+
*/
|
|
38
|
+
type ChatIdLike = string | ChatId;
|
|
39
|
+
/** Lifecycle status of a message part (text, reasoning, etc.) */
|
|
40
|
+
type PartStatus = "pending" | "streaming" | "complete" | "error";
|
|
41
|
+
/** Lifecycle status of a tool call within a message */
|
|
42
|
+
type ToolCallStatus = "pending" | "running" | "requires_approval" | "complete" | "error" | "denied";
|
|
43
|
+
/** Lifecycle status of an entire message */
|
|
44
|
+
type MessageStatus = "pending" | "streaming" | "complete" | "error" | "cancelled";
|
|
45
|
+
/** Lifecycle status of a chat session */
|
|
46
|
+
type SessionStatus = "active" | "archived";
|
|
47
|
+
/** Lifecycle status of the chat runtime */
|
|
48
|
+
type RuntimeStatus = "idle" | "streaming" | "error" | "disposed";
|
|
49
|
+
/** Plain text content part */
|
|
50
|
+
interface TextPart {
|
|
51
|
+
type: "text";
|
|
52
|
+
text: string;
|
|
53
|
+
status: PartStatus;
|
|
54
|
+
}
|
|
55
|
+
/** Model reasoning/thinking content part */
|
|
56
|
+
interface ReasoningPart {
|
|
57
|
+
type: "reasoning";
|
|
58
|
+
text: string;
|
|
59
|
+
status: PartStatus;
|
|
60
|
+
}
|
|
61
|
+
/** Tool invocation part with call ID, arguments, optional result */
|
|
62
|
+
interface ToolCallPart {
|
|
63
|
+
type: "tool_call";
|
|
64
|
+
toolCallId: string;
|
|
65
|
+
name: string;
|
|
66
|
+
args: unknown;
|
|
67
|
+
result?: unknown;
|
|
68
|
+
status: ToolCallStatus;
|
|
69
|
+
error?: string;
|
|
70
|
+
}
|
|
71
|
+
/** Source reference part (URL citation) */
|
|
72
|
+
interface SourcePart {
|
|
73
|
+
type: "source";
|
|
74
|
+
url: string;
|
|
75
|
+
title?: string;
|
|
76
|
+
status: PartStatus;
|
|
77
|
+
}
|
|
78
|
+
/** File attachment part (base64-encoded data) */
|
|
79
|
+
interface FilePart {
|
|
80
|
+
type: "file";
|
|
81
|
+
name: string;
|
|
82
|
+
mimeType: string;
|
|
83
|
+
data: string;
|
|
84
|
+
status: PartStatus;
|
|
85
|
+
}
|
|
86
|
+
/** Union of all message part types */
|
|
87
|
+
type MessagePart = TextPart | ReasoningPart | ToolCallPart | SourcePart | FilePart;
|
|
88
|
+
/** Role of message author */
|
|
89
|
+
type ChatRole = "user" | "assistant" | "system";
|
|
90
|
+
/** Metadata attached to messages — useful preset for the TMetadata generic */
|
|
91
|
+
interface ChatMessageMetadata {
|
|
92
|
+
model?: string;
|
|
93
|
+
backend?: string;
|
|
94
|
+
usage?: UsageData;
|
|
95
|
+
isSummary?: boolean;
|
|
96
|
+
isArchived?: boolean;
|
|
97
|
+
estimatedTokens?: number;
|
|
98
|
+
custom?: Record<string, unknown>;
|
|
99
|
+
}
|
|
100
|
+
/** Message status */
|
|
101
|
+
type ChatMessageStatus = MessageStatus;
|
|
102
|
+
/** A single chat message — the fundamental unit of conversation */
|
|
103
|
+
interface ChatMessage<TMetadata = unknown> {
|
|
104
|
+
id: ChatId;
|
|
105
|
+
role: ChatRole;
|
|
106
|
+
parts: MessagePart[];
|
|
107
|
+
metadata?: TMetadata;
|
|
108
|
+
createdAt: string;
|
|
109
|
+
updatedAt?: string;
|
|
110
|
+
status: MessageStatus;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Join all TextPart texts in a message
|
|
114
|
+
* @param message - The chat message to extract text from
|
|
115
|
+
* @returns Concatenated text content
|
|
116
|
+
*/
|
|
117
|
+
declare function getMessageText(message: ChatMessage): string;
|
|
118
|
+
/**
|
|
119
|
+
* Filter all ToolCallParts from a message
|
|
120
|
+
* @param message - The chat message to extract tool calls from
|
|
121
|
+
* @returns Array of ToolCallPart
|
|
122
|
+
*/
|
|
123
|
+
declare function getMessageToolCalls(message: ChatMessage): ToolCallPart[];
|
|
124
|
+
/**
|
|
125
|
+
* Join all ReasoningPart texts in a message
|
|
126
|
+
* @param message - The chat message to extract reasoning from
|
|
127
|
+
* @returns Concatenated reasoning content
|
|
128
|
+
*/
|
|
129
|
+
declare function getMessageReasoning(message: ChatMessage): string;
|
|
130
|
+
/** Options for sending a message */
|
|
131
|
+
interface SendOpts {
|
|
132
|
+
sessionId?: string;
|
|
133
|
+
model?: string;
|
|
134
|
+
signal?: AbortSignal;
|
|
135
|
+
metadata?: Record<string, unknown>;
|
|
136
|
+
}
|
|
137
|
+
/** Options for creating a new session */
|
|
138
|
+
interface CreateSessionOpts {
|
|
139
|
+
id?: string;
|
|
140
|
+
title?: string;
|
|
141
|
+
model?: string;
|
|
142
|
+
metadata?: Record<string, unknown>;
|
|
143
|
+
}
|
|
144
|
+
/** Options for listing sessions with pagination */
|
|
145
|
+
interface ListOpts {
|
|
146
|
+
limit?: number;
|
|
147
|
+
offset?: number;
|
|
148
|
+
status?: SessionStatus;
|
|
149
|
+
}
|
|
150
|
+
/** Options for backend execution (model, tokens, tools) */
|
|
151
|
+
interface BackendOpts {
|
|
152
|
+
model: string;
|
|
153
|
+
signal?: AbortSignal;
|
|
154
|
+
systemPrompt?: string;
|
|
155
|
+
temperature?: number;
|
|
156
|
+
maxTokens?: number;
|
|
157
|
+
tools?: Record<string, unknown>;
|
|
158
|
+
providerOptions?: Record<string, unknown>;
|
|
159
|
+
}
|
|
160
|
+
/** Context passed to tool execute functions */
|
|
161
|
+
interface ToolContext {
|
|
162
|
+
sessionId: string;
|
|
163
|
+
userId?: string;
|
|
164
|
+
signal: AbortSignal;
|
|
165
|
+
}
|
|
166
|
+
/** Configuration for creating a chat runtime */
|
|
167
|
+
interface ChatRuntimeConfig {
|
|
168
|
+
backend: string;
|
|
169
|
+
model?: string;
|
|
170
|
+
apiKey?: string;
|
|
171
|
+
baseUrl?: string;
|
|
172
|
+
context?: {
|
|
173
|
+
maxTokens?: number;
|
|
174
|
+
reserveTokens?: number;
|
|
175
|
+
strategy?: "sliding" | "summarize" | "truncate";
|
|
176
|
+
};
|
|
177
|
+
retry?: {
|
|
178
|
+
maxRetries?: number;
|
|
179
|
+
initialDelay?: number;
|
|
180
|
+
backoffFactor?: number;
|
|
181
|
+
};
|
|
182
|
+
providerOptions?: Record<string, unknown>;
|
|
183
|
+
}
|
|
184
|
+
/** Session configuration snapshot */
|
|
185
|
+
interface ChatSessionConfig {
|
|
186
|
+
model: string;
|
|
187
|
+
backend: string;
|
|
188
|
+
systemPrompt?: string;
|
|
189
|
+
temperature?: number;
|
|
190
|
+
maxTokens?: number;
|
|
191
|
+
}
|
|
192
|
+
/** Session metadata */
|
|
193
|
+
interface ChatSessionMetadata<TCustom extends Record<string, unknown> = Record<string, unknown>> {
|
|
194
|
+
messageCount: number;
|
|
195
|
+
totalTokens: number;
|
|
196
|
+
tags?: string[];
|
|
197
|
+
custom?: TCustom;
|
|
198
|
+
}
|
|
199
|
+
/** Chat session — a conversation with ordered messages */
|
|
200
|
+
interface ChatSession<TCustom extends Record<string, unknown> = Record<string, unknown>> {
|
|
201
|
+
id: ChatId;
|
|
202
|
+
title?: string;
|
|
203
|
+
messages: ChatMessage[];
|
|
204
|
+
config: ChatSessionConfig;
|
|
205
|
+
metadata: ChatSessionMetadata<TCustom>;
|
|
206
|
+
status: SessionStatus;
|
|
207
|
+
createdAt: string;
|
|
208
|
+
updatedAt: string;
|
|
209
|
+
backendSessionId?: string;
|
|
210
|
+
/** Subscribe to session changes (for React useSyncExternalStore) */
|
|
211
|
+
subscribe?(callback: () => void): () => void;
|
|
212
|
+
/** Get immutable snapshot of session state (for React useSyncExternalStore) */
|
|
213
|
+
getSnapshot?(): ChatSession<TCustom>;
|
|
214
|
+
/** Last message in the session */
|
|
215
|
+
readonly lastMessage?: ChatMessage;
|
|
216
|
+
}
|
|
217
|
+
/** Lightweight session info for listing (without full message array) */
|
|
218
|
+
interface SessionInfo {
|
|
219
|
+
id: ChatId;
|
|
220
|
+
title?: string;
|
|
221
|
+
status: SessionStatus;
|
|
222
|
+
messageCount: number;
|
|
223
|
+
lastMessage?: ChatMessage;
|
|
224
|
+
createdAt: string;
|
|
225
|
+
updatedAt: string;
|
|
226
|
+
}
|
|
227
|
+
/** Events emitted during chat operation */
|
|
228
|
+
type ChatEvent = {
|
|
229
|
+
type: "message:start";
|
|
230
|
+
messageId: ChatId;
|
|
231
|
+
role: ChatRole;
|
|
232
|
+
} | {
|
|
233
|
+
type: "message:delta";
|
|
234
|
+
messageId: ChatId;
|
|
235
|
+
text: string;
|
|
236
|
+
} | {
|
|
237
|
+
type: "message:complete";
|
|
238
|
+
messageId: ChatId;
|
|
239
|
+
message: ChatMessage;
|
|
240
|
+
} | {
|
|
241
|
+
type: "tool:start";
|
|
242
|
+
messageId: ChatId;
|
|
243
|
+
toolCallId: string;
|
|
244
|
+
toolName: string;
|
|
245
|
+
args: Record<string, unknown>;
|
|
246
|
+
} | {
|
|
247
|
+
type: "tool:complete";
|
|
248
|
+
messageId: ChatId;
|
|
249
|
+
toolCallId: string;
|
|
250
|
+
toolName: string;
|
|
251
|
+
result: unknown;
|
|
252
|
+
isError?: boolean;
|
|
253
|
+
} | {
|
|
254
|
+
type: "thinking:start";
|
|
255
|
+
messageId: ChatId;
|
|
256
|
+
} | {
|
|
257
|
+
type: "thinking:delta";
|
|
258
|
+
messageId: ChatId;
|
|
259
|
+
text: string;
|
|
260
|
+
} | {
|
|
261
|
+
type: "thinking:end";
|
|
262
|
+
messageId: ChatId;
|
|
263
|
+
} | {
|
|
264
|
+
type: "permission:request";
|
|
265
|
+
messageId: ChatId;
|
|
266
|
+
toolName: string;
|
|
267
|
+
toolArgs: Record<string, unknown>;
|
|
268
|
+
} | {
|
|
269
|
+
type: "permission:response";
|
|
270
|
+
messageId: ChatId;
|
|
271
|
+
toolName: string;
|
|
272
|
+
allowed: boolean;
|
|
273
|
+
} | {
|
|
274
|
+
type: "usage";
|
|
275
|
+
promptTokens: number;
|
|
276
|
+
completionTokens: number;
|
|
277
|
+
model?: string;
|
|
278
|
+
} | {
|
|
279
|
+
type: "session:created";
|
|
280
|
+
sessionId: ChatId;
|
|
281
|
+
} | {
|
|
282
|
+
type: "session:updated";
|
|
283
|
+
sessionId: ChatId;
|
|
284
|
+
} | {
|
|
285
|
+
type: "error";
|
|
286
|
+
error: string;
|
|
287
|
+
recoverable: boolean;
|
|
288
|
+
messageId?: ChatId;
|
|
289
|
+
} | {
|
|
290
|
+
type: "typing:start";
|
|
291
|
+
} | {
|
|
292
|
+
type: "typing:end";
|
|
293
|
+
} | {
|
|
294
|
+
type: "heartbeat";
|
|
295
|
+
} | {
|
|
296
|
+
type: "done";
|
|
297
|
+
finalOutput?: string;
|
|
298
|
+
};
|
|
299
|
+
/** All possible ChatEvent type strings */
|
|
300
|
+
type ChatEventType = ChatEvent["type"];
|
|
301
|
+
/** Context passed to ChatMiddleware hooks */
|
|
302
|
+
interface ChatMiddlewareContext {
|
|
303
|
+
sessionId: ChatId;
|
|
304
|
+
signal: AbortSignal;
|
|
305
|
+
}
|
|
306
|
+
/** Runtime-level middleware for the send/receive lifecycle.
|
|
307
|
+
* Different from EventMiddleware which operates at the event bus level. */
|
|
308
|
+
interface ChatMiddleware {
|
|
309
|
+
/** Transform message before sending to backend */
|
|
310
|
+
onBeforeSend?(message: ChatMessage, context: ChatMiddlewareContext): ChatMessage | Promise<ChatMessage>;
|
|
311
|
+
/** Transform/intercept stream events */
|
|
312
|
+
onEvent?(event: ChatEvent, context: ChatMiddlewareContext): ChatEvent | null | Promise<ChatEvent | null>;
|
|
313
|
+
/** Transform completed message after receiving from backend */
|
|
314
|
+
onAfterReceive?(message: ChatMessage, context: ChatMiddlewareContext): ChatMessage | Promise<ChatMessage>;
|
|
315
|
+
/** Intercept errors — return null to suppress, return error to propagate */
|
|
316
|
+
onError?(error: Error, context: ChatMiddlewareContext): Error | null | Promise<Error | null>;
|
|
317
|
+
}
|
|
318
|
+
/** Options for sending a message to a provider */
|
|
319
|
+
interface SendMessageOptions {
|
|
320
|
+
signal?: AbortSignal;
|
|
321
|
+
model?: string;
|
|
322
|
+
context?: Record<string, unknown>;
|
|
323
|
+
/** Additional tools to include in this request */
|
|
324
|
+
tools?: ToolDefinition[];
|
|
325
|
+
}
|
|
326
|
+
/** Abstract chat provider — wraps an IAgentService for chat use */
|
|
327
|
+
interface IChatProvider {
|
|
328
|
+
readonly name: string;
|
|
329
|
+
sendMessage(session: ChatSession, message: string, options?: SendMessageOptions): Promise<ChatMessage>;
|
|
330
|
+
streamMessage(session: ChatSession, message: string, options?: SendMessageOptions): AsyncIterable<ChatEvent>;
|
|
331
|
+
listModels(): Promise<ModelInfo[]>;
|
|
332
|
+
validate(): Promise<{
|
|
333
|
+
valid: boolean;
|
|
334
|
+
errors: string[];
|
|
335
|
+
}>;
|
|
336
|
+
dispose(): Promise<void>;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Check if a value is a ChatMessage
|
|
340
|
+
* @param value - Value to check
|
|
341
|
+
* @returns True if value has ChatMessage shape
|
|
342
|
+
*/
|
|
343
|
+
declare function isChatMessage(value: unknown): value is ChatMessage;
|
|
344
|
+
/**
|
|
345
|
+
* Check if a value is a ChatSession
|
|
346
|
+
* @param value - Value to check
|
|
347
|
+
* @returns True if value has ChatSession shape
|
|
348
|
+
*/
|
|
349
|
+
declare function isChatSession(value: unknown): value is ChatSession;
|
|
350
|
+
/**
|
|
351
|
+
* Check if a value is a MessagePart
|
|
352
|
+
* @param value - Value to check
|
|
353
|
+
* @returns True if value has MessagePart shape
|
|
354
|
+
*/
|
|
355
|
+
declare function isMessagePart(value: unknown): value is MessagePart;
|
|
356
|
+
/**
|
|
357
|
+
* Check if a value is a TextPart
|
|
358
|
+
* @param value - Value to check
|
|
359
|
+
* @returns True if value is a TextPart
|
|
360
|
+
*/
|
|
361
|
+
declare function isTextPart(value: unknown): value is TextPart;
|
|
362
|
+
/**
|
|
363
|
+
* Check if a value is a ToolCallPart
|
|
364
|
+
* @param value - Value to check
|
|
365
|
+
* @returns True if value is a ToolCallPart
|
|
366
|
+
*/
|
|
367
|
+
declare function isToolCallPart(value: unknown): value is ToolCallPart;
|
|
368
|
+
/**
|
|
369
|
+
* Check if a value is a ReasoningPart
|
|
370
|
+
* @param value - Value to check
|
|
371
|
+
* @returns True if value is a ReasoningPart
|
|
372
|
+
*/
|
|
373
|
+
declare function isReasoningPart(value: unknown): value is ReasoningPart;
|
|
374
|
+
/**
|
|
375
|
+
* Check if a value is a SourcePart
|
|
376
|
+
* @param value - Value to check
|
|
377
|
+
* @returns True if value is a SourcePart
|
|
378
|
+
*/
|
|
379
|
+
declare function isSourcePart(value: unknown): value is SourcePart;
|
|
380
|
+
/**
|
|
381
|
+
* Check if a value is a FilePart
|
|
382
|
+
* @param value - Value to check
|
|
383
|
+
* @returns True if value is a FilePart
|
|
384
|
+
*/
|
|
385
|
+
declare function isFilePart(value: unknown): value is FilePart;
|
|
386
|
+
/**
|
|
387
|
+
* Check if a value is a ChatEvent
|
|
388
|
+
* @param value - Value to check
|
|
389
|
+
* @returns True if value has a valid ChatEvent type
|
|
390
|
+
*/
|
|
391
|
+
declare function isChatEvent(value: unknown): value is ChatEvent;
|
|
392
|
+
/**
|
|
393
|
+
* Map a single AgentEvent to a ChatEvent (or null if no mapping)
|
|
394
|
+
* @param event - The AgentEvent to convert
|
|
395
|
+
* @param messageId - ChatId to associate with the event
|
|
396
|
+
* @returns Corresponding ChatEvent or null if unmappable
|
|
397
|
+
*/
|
|
398
|
+
declare function agentEventToChatEvent(event: AgentEvent, messageId: ChatId): ChatEvent | null;
|
|
399
|
+
/**
|
|
400
|
+
* Convert AgentEvent async iterable to ChatEvent async iterable
|
|
401
|
+
* @param events - Source agent events
|
|
402
|
+
* @param messageId - ChatId to associate with converted events
|
|
403
|
+
* @returns Async iterable of ChatEvent (nulls filtered out)
|
|
404
|
+
*/
|
|
405
|
+
declare function adaptAgentEvents(events: AsyncIterable<AgentEvent>, messageId: ChatId): AsyncIterable<ChatEvent>;
|
|
406
|
+
/**
|
|
407
|
+
* Map a ChatEvent back to an AgentEvent for accumulator consumption.
|
|
408
|
+
* Returns null for events that don't map to accumulator-relevant AgentEvents
|
|
409
|
+
* (e.g. message:start, message:complete, usage, permission:*, heartbeat).
|
|
410
|
+
*
|
|
411
|
+
* @param event - The ChatEvent to convert
|
|
412
|
+
* @returns Corresponding AgentEvent or null if not accumulator-relevant
|
|
413
|
+
*/
|
|
414
|
+
declare function chatEventToAgentEvent(event: ChatEvent): AgentEvent | null;
|
|
415
|
+
/**
|
|
416
|
+
* Convert a ChatMessage to agent-sdk Message format
|
|
417
|
+
* @param message - The ChatMessage to convert
|
|
418
|
+
* @returns agent-sdk Message
|
|
419
|
+
*/
|
|
420
|
+
declare function toAgentMessage(message: ChatMessage): Message;
|
|
421
|
+
/**
|
|
422
|
+
* Convert an agent-sdk Message to ChatMessage
|
|
423
|
+
* @param message - The agent-sdk Message to convert
|
|
424
|
+
* @param id - Optional ChatId (auto-generated if omitted)
|
|
425
|
+
* @returns ChatMessage with status "complete"
|
|
426
|
+
*/
|
|
427
|
+
declare function fromAgentMessage(message: Message, id?: ChatId): ChatMessage;
|
|
428
|
+
/**
|
|
429
|
+
* Extract ToolResults from ToolCallParts that have results
|
|
430
|
+
* @param message - The ChatMessage to extract results from
|
|
431
|
+
* @returns Array of ToolResult for completed tool calls
|
|
432
|
+
*/
|
|
433
|
+
declare function extractToolResults(message: ChatMessage): ToolResult[];
|
|
434
|
+
|
|
435
|
+
export { type BackendOpts, type ChatEvent, type ChatEventType, type ChatId, type ChatIdLike, type ChatMessage, type ChatMessageMetadata, type ChatMessageStatus, type ChatMiddleware, type ChatMiddlewareContext, type ChatRole, type ChatRuntimeConfig, type ChatSession, type ChatSessionConfig, type ChatSessionMetadata, type CreateSessionOpts, type FilePart, type IChatProvider, type ListOpts, type MessagePart, type MessageStatus, type PartStatus, type ReasoningPart, type RuntimeStatus, type SendMessageOptions, type SendOpts, type SessionInfo, type SessionStatus, type SourcePart, type TextPart, type ToolCallPart, type ToolCallStatus, type ToolContext, adaptAgentEvents, agentEventToChatEvent, chatEventToAgentEvent, createChatId, extractToolResults, fromAgentMessage, getMessageReasoning, getMessageText, getMessageToolCalls, isChatEvent, isChatMessage, isChatSession, isFilePart, isMessagePart, isReasoningPart, isSourcePart, isTextPart, isToolCallPart, toAgentMessage, toChatId };
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// src/chat/core.ts
|
|
2
|
+
function createChatId() {
|
|
3
|
+
return crypto.randomUUID();
|
|
4
|
+
}
|
|
5
|
+
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
6
|
+
function toChatId(value) {
|
|
7
|
+
if (!UUID_RE.test(value)) {
|
|
8
|
+
throw new TypeError(`Invalid ChatId: "${value}" is not a valid UUID`);
|
|
9
|
+
}
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
function getMessageText(message) {
|
|
13
|
+
return message.parts.filter((p) => p.type === "text").map((p) => p.text).join("");
|
|
14
|
+
}
|
|
15
|
+
function getMessageToolCalls(message) {
|
|
16
|
+
return message.parts.filter((p) => p.type === "tool_call");
|
|
17
|
+
}
|
|
18
|
+
function getMessageReasoning(message) {
|
|
19
|
+
return message.parts.filter((p) => p.type === "reasoning").map((p) => p.text).join("");
|
|
20
|
+
}
|
|
21
|
+
function isChatMessage(value) {
|
|
22
|
+
if (typeof value !== "object" || value === null) return false;
|
|
23
|
+
const obj = value;
|
|
24
|
+
return typeof obj.id === "string" && typeof obj.role === "string" && (obj.role === "user" || obj.role === "assistant" || obj.role === "system") && Array.isArray(obj.parts) && typeof obj.createdAt === "string" && typeof obj.status === "string";
|
|
25
|
+
}
|
|
26
|
+
function isChatSession(value) {
|
|
27
|
+
if (typeof value !== "object" || value === null) return false;
|
|
28
|
+
const obj = value;
|
|
29
|
+
return typeof obj.id === "string" && Array.isArray(obj.messages) && typeof obj.config === "object" && obj.config !== null && typeof obj.createdAt === "string" && typeof obj.updatedAt === "string" && typeof obj.status === "string";
|
|
30
|
+
}
|
|
31
|
+
function isMessagePart(value) {
|
|
32
|
+
if (typeof value !== "object" || value === null) return false;
|
|
33
|
+
const obj = value;
|
|
34
|
+
return typeof obj.type === "string" && (obj.type === "text" || obj.type === "reasoning" || obj.type === "tool_call" || obj.type === "source" || obj.type === "file");
|
|
35
|
+
}
|
|
36
|
+
function isTextPart(value) {
|
|
37
|
+
if (typeof value !== "object" || value === null) return false;
|
|
38
|
+
const obj = value;
|
|
39
|
+
return obj.type === "text" && typeof obj.text === "string";
|
|
40
|
+
}
|
|
41
|
+
function isToolCallPart(value) {
|
|
42
|
+
if (typeof value !== "object" || value === null) return false;
|
|
43
|
+
const obj = value;
|
|
44
|
+
return obj.type === "tool_call" && typeof obj.toolCallId === "string" && typeof obj.name === "string";
|
|
45
|
+
}
|
|
46
|
+
function isReasoningPart(value) {
|
|
47
|
+
if (typeof value !== "object" || value === null) return false;
|
|
48
|
+
const obj = value;
|
|
49
|
+
return obj.type === "reasoning" && typeof obj.text === "string";
|
|
50
|
+
}
|
|
51
|
+
function isSourcePart(value) {
|
|
52
|
+
if (typeof value !== "object" || value === null) return false;
|
|
53
|
+
const obj = value;
|
|
54
|
+
return obj.type === "source" && typeof obj.url === "string";
|
|
55
|
+
}
|
|
56
|
+
function isFilePart(value) {
|
|
57
|
+
if (typeof value !== "object" || value === null) return false;
|
|
58
|
+
const obj = value;
|
|
59
|
+
return obj.type === "file" && typeof obj.name === "string" && typeof obj.mimeType === "string";
|
|
60
|
+
}
|
|
61
|
+
function isChatEvent(value) {
|
|
62
|
+
if (typeof value !== "object" || value === null) return false;
|
|
63
|
+
const obj = value;
|
|
64
|
+
const validTypes = [
|
|
65
|
+
"message:start",
|
|
66
|
+
"message:delta",
|
|
67
|
+
"message:complete",
|
|
68
|
+
"tool:start",
|
|
69
|
+
"tool:complete",
|
|
70
|
+
"thinking:start",
|
|
71
|
+
"thinking:delta",
|
|
72
|
+
"thinking:end",
|
|
73
|
+
"permission:request",
|
|
74
|
+
"permission:response",
|
|
75
|
+
"usage",
|
|
76
|
+
"session:created",
|
|
77
|
+
"session:updated",
|
|
78
|
+
"error",
|
|
79
|
+
"typing:start",
|
|
80
|
+
"typing:end",
|
|
81
|
+
"heartbeat",
|
|
82
|
+
"done"
|
|
83
|
+
];
|
|
84
|
+
return validTypes.includes(obj.type);
|
|
85
|
+
}
|
|
86
|
+
function agentEventToChatEvent(event, messageId) {
|
|
87
|
+
switch (event.type) {
|
|
88
|
+
case "text_delta":
|
|
89
|
+
return { type: "message:delta", messageId, text: event.text };
|
|
90
|
+
case "thinking_start":
|
|
91
|
+
return { type: "thinking:start", messageId };
|
|
92
|
+
case "thinking_delta":
|
|
93
|
+
return { type: "thinking:delta", messageId, text: event.text };
|
|
94
|
+
case "thinking_end":
|
|
95
|
+
return { type: "thinking:end", messageId };
|
|
96
|
+
case "tool_call_start":
|
|
97
|
+
return {
|
|
98
|
+
type: "tool:start",
|
|
99
|
+
messageId,
|
|
100
|
+
toolCallId: event.toolCallId,
|
|
101
|
+
toolName: event.toolName,
|
|
102
|
+
args: event.args
|
|
103
|
+
};
|
|
104
|
+
case "tool_call_end":
|
|
105
|
+
return {
|
|
106
|
+
type: "tool:complete",
|
|
107
|
+
messageId,
|
|
108
|
+
toolCallId: event.toolCallId,
|
|
109
|
+
toolName: event.toolName,
|
|
110
|
+
result: event.result
|
|
111
|
+
};
|
|
112
|
+
case "permission_request":
|
|
113
|
+
return {
|
|
114
|
+
type: "permission:request",
|
|
115
|
+
messageId,
|
|
116
|
+
toolName: event.request.toolName,
|
|
117
|
+
toolArgs: event.request.toolArgs
|
|
118
|
+
};
|
|
119
|
+
case "permission_response":
|
|
120
|
+
return {
|
|
121
|
+
type: "permission:response",
|
|
122
|
+
messageId,
|
|
123
|
+
toolName: event.toolName,
|
|
124
|
+
allowed: event.decision.allowed
|
|
125
|
+
};
|
|
126
|
+
case "usage_update":
|
|
127
|
+
return {
|
|
128
|
+
type: "usage",
|
|
129
|
+
promptTokens: event.promptTokens,
|
|
130
|
+
completionTokens: event.completionTokens,
|
|
131
|
+
model: event.model
|
|
132
|
+
};
|
|
133
|
+
case "error":
|
|
134
|
+
return {
|
|
135
|
+
type: "error",
|
|
136
|
+
error: event.error,
|
|
137
|
+
recoverable: event.recoverable,
|
|
138
|
+
messageId
|
|
139
|
+
};
|
|
140
|
+
case "heartbeat":
|
|
141
|
+
return { type: "heartbeat" };
|
|
142
|
+
case "ask_user":
|
|
143
|
+
case "ask_user_response":
|
|
144
|
+
case "session_info":
|
|
145
|
+
case "done":
|
|
146
|
+
return null;
|
|
147
|
+
default:
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async function* adaptAgentEvents(events, messageId) {
|
|
152
|
+
for await (const event of events) {
|
|
153
|
+
const chatEvent = agentEventToChatEvent(event, messageId);
|
|
154
|
+
if (chatEvent !== null) {
|
|
155
|
+
yield chatEvent;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function chatEventToAgentEvent(event) {
|
|
160
|
+
switch (event.type) {
|
|
161
|
+
case "message:delta":
|
|
162
|
+
return { type: "text_delta", text: event.text };
|
|
163
|
+
case "thinking:start":
|
|
164
|
+
return { type: "thinking_start" };
|
|
165
|
+
case "thinking:delta":
|
|
166
|
+
return { type: "thinking_delta", text: event.text };
|
|
167
|
+
case "thinking:end":
|
|
168
|
+
return { type: "thinking_end" };
|
|
169
|
+
case "tool:start":
|
|
170
|
+
return {
|
|
171
|
+
type: "tool_call_start",
|
|
172
|
+
toolCallId: event.toolCallId,
|
|
173
|
+
toolName: event.toolName,
|
|
174
|
+
args: event.args
|
|
175
|
+
};
|
|
176
|
+
case "tool:complete":
|
|
177
|
+
return {
|
|
178
|
+
type: "tool_call_end",
|
|
179
|
+
toolCallId: event.toolCallId,
|
|
180
|
+
toolName: event.toolName,
|
|
181
|
+
result: event.result
|
|
182
|
+
};
|
|
183
|
+
case "error":
|
|
184
|
+
return { type: "error", error: event.error, recoverable: event.recoverable };
|
|
185
|
+
default:
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function toAgentMessage(message) {
|
|
190
|
+
const textContent = getMessageText(message);
|
|
191
|
+
const toolCallParts = getMessageToolCalls(message);
|
|
192
|
+
switch (message.role) {
|
|
193
|
+
case "user":
|
|
194
|
+
return { role: "user", content: textContent };
|
|
195
|
+
case "assistant": {
|
|
196
|
+
const toolCalls = toolCallParts.length > 0 ? toolCallParts.map((p) => ({ id: p.toolCallId, name: p.name, args: p.args })) : void 0;
|
|
197
|
+
return {
|
|
198
|
+
role: "assistant",
|
|
199
|
+
content: textContent,
|
|
200
|
+
toolCalls
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
case "system":
|
|
204
|
+
return { role: "system", content: textContent };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function fromAgentMessage(message, id) {
|
|
208
|
+
const chatId = id ?? createChatId();
|
|
209
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
210
|
+
const parts = [];
|
|
211
|
+
const textContent = typeof message.content === "string" ? message.content : Array.isArray(message.content) ? message.content.filter((part) => part.type === "text").map((part) => part.text).join("\n") : message.content ?? "";
|
|
212
|
+
if (textContent) {
|
|
213
|
+
parts.push({ type: "text", text: textContent, status: "complete" });
|
|
214
|
+
}
|
|
215
|
+
if (message.role === "assistant" && message.toolCalls) {
|
|
216
|
+
for (const tc of message.toolCalls) {
|
|
217
|
+
parts.push({
|
|
218
|
+
type: "tool_call",
|
|
219
|
+
toolCallId: tc.id,
|
|
220
|
+
name: tc.name,
|
|
221
|
+
args: tc.args,
|
|
222
|
+
status: "complete"
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (message.role === "tool" && message.toolResults) {
|
|
227
|
+
for (const tr of message.toolResults) {
|
|
228
|
+
parts.push({
|
|
229
|
+
type: "tool_call",
|
|
230
|
+
toolCallId: tr.toolCallId,
|
|
231
|
+
name: tr.name,
|
|
232
|
+
args: {},
|
|
233
|
+
result: tr.result,
|
|
234
|
+
status: "complete"
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (parts.length === 0) {
|
|
239
|
+
parts.push({ type: "text", text: "", status: "complete" });
|
|
240
|
+
}
|
|
241
|
+
const role = message.role === "tool" ? "assistant" : message.role;
|
|
242
|
+
return {
|
|
243
|
+
id: chatId,
|
|
244
|
+
role,
|
|
245
|
+
parts,
|
|
246
|
+
createdAt: now,
|
|
247
|
+
status: "complete"
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function extractToolResults(message) {
|
|
251
|
+
return getMessageToolCalls(message).filter((p) => p.result !== void 0).map((p) => ({
|
|
252
|
+
toolCallId: p.toolCallId,
|
|
253
|
+
name: p.name,
|
|
254
|
+
result: p.result,
|
|
255
|
+
isError: p.status === "error" ? true : void 0
|
|
256
|
+
}));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export { adaptAgentEvents, agentEventToChatEvent, chatEventToAgentEvent, createChatId, extractToolResults, fromAgentMessage, getMessageReasoning, getMessageText, getMessageToolCalls, isChatEvent, isChatMessage, isChatSession, isFilePart, isMessagePart, isReasoningPart, isSourcePart, isTextPart, isToolCallPart, toAgentMessage, toChatId };
|
|
260
|
+
//# sourceMappingURL=core.js.map
|
|
261
|
+
//# sourceMappingURL=core.js.map
|