@genui-a3/core 0.1.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 +1 -0
- package/dist/index.cjs +1085 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +569 -0
- package/dist/index.d.ts +569 -0
- package/dist/index.js +1054 -0
- package/dist/index.js.map +1 -0
- package/package.json +127 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
import * as zod_v4_core from 'zod/v4/core';
|
|
2
|
+
import * as zod from 'zod';
|
|
3
|
+
import { z, ZodType } from 'zod';
|
|
4
|
+
import { Message as Message$1, ConverseStreamOutput, ToolInputSchema } from '@aws-sdk/client-bedrock-runtime';
|
|
5
|
+
|
|
6
|
+
declare enum WidgetType {
|
|
7
|
+
WIDGET_1 = "widget1"
|
|
8
|
+
}
|
|
9
|
+
interface BaseWidget {
|
|
10
|
+
type: string;
|
|
11
|
+
displayText?: string;
|
|
12
|
+
}
|
|
13
|
+
interface Widget1 extends BaseWidget {
|
|
14
|
+
type: WidgetType.WIDGET_1;
|
|
15
|
+
}
|
|
16
|
+
type Widget = Widget1;
|
|
17
|
+
|
|
18
|
+
declare enum ToolName {
|
|
19
|
+
Tool1 = "tool1"
|
|
20
|
+
}
|
|
21
|
+
type MessageMetadata = {
|
|
22
|
+
source: MessageSender;
|
|
23
|
+
timestamp?: number;
|
|
24
|
+
widget?: Widget;
|
|
25
|
+
redirectTo?: string;
|
|
26
|
+
};
|
|
27
|
+
type AgUIEvent = {
|
|
28
|
+
component: string;
|
|
29
|
+
data?: Record<string, unknown>;
|
|
30
|
+
};
|
|
31
|
+
type Message = {
|
|
32
|
+
text: string;
|
|
33
|
+
metadata?: MessageMetadata;
|
|
34
|
+
agUIEvents?: AgUIEvent[];
|
|
35
|
+
isStreaming?: boolean;
|
|
36
|
+
isError?: boolean;
|
|
37
|
+
executingTools?: ToolName[];
|
|
38
|
+
messageId?: string;
|
|
39
|
+
widgets?: object;
|
|
40
|
+
};
|
|
41
|
+
declare enum MessageSender {
|
|
42
|
+
ASSISTANT = "assistant",
|
|
43
|
+
USER = "user"
|
|
44
|
+
}
|
|
45
|
+
type Conversation = Message[];
|
|
46
|
+
type ToolResult<T> = {
|
|
47
|
+
content: T;
|
|
48
|
+
status: 'success' | 'error';
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Interface for session persistence.
|
|
53
|
+
* Implement this to use Redis, DynamoDB, Postgres, etc.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* class RedisSessionStore<TState extends BaseState, TContext extends BaseChatContext>
|
|
58
|
+
* implements SessionStore<TState, TContext> {
|
|
59
|
+
* constructor(private redis: RedisClient) {}
|
|
60
|
+
*
|
|
61
|
+
* async load(sessionId: string): Promise<SessionData<TState, TContext> | null> {
|
|
62
|
+
* const data = await this.redis.get(`session:${sessionId}`)
|
|
63
|
+
* return data ? JSON.parse(data) : null
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* async save(sessionId: string, data: SessionData<TState, TContext>): Promise<void> {
|
|
67
|
+
* await this.redis.set(`session:${sessionId}`, JSON.stringify(data))
|
|
68
|
+
* }
|
|
69
|
+
*
|
|
70
|
+
* async delete(sessionId: string): Promise<void> {
|
|
71
|
+
* await this.redis.del(`session:${sessionId}`)
|
|
72
|
+
* }
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
interface SessionStore<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> {
|
|
77
|
+
/** Load session data, returns null if not found */
|
|
78
|
+
load(sessionId: string): Promise<SessionData<TState, TContext> | null>;
|
|
79
|
+
/** Save session data */
|
|
80
|
+
save(sessionId: string, data: SessionData<TState, TContext>): Promise<void>;
|
|
81
|
+
/** Delete a session (optional) */
|
|
82
|
+
delete?(sessionId: string): Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* BaseState defines the minimum required fields for state.
|
|
87
|
+
* Consumers extend this interface with their own properties.
|
|
88
|
+
*
|
|
89
|
+
* Note: State is GLOBAL across all agents in a session. All agents
|
|
90
|
+
* share the same state object, enabling cross-agent data flow.
|
|
91
|
+
*/
|
|
92
|
+
interface BaseState {
|
|
93
|
+
[key: string]: unknown;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* BaseChatContext defines the minimum required fields for chat context.
|
|
97
|
+
* Consumers extend this interface with their own properties
|
|
98
|
+
*/
|
|
99
|
+
interface BaseChatContext {
|
|
100
|
+
[key: string]: unknown;
|
|
101
|
+
}
|
|
102
|
+
interface SessionData<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> {
|
|
103
|
+
sessionId: string;
|
|
104
|
+
messages: Conversation;
|
|
105
|
+
conversationHistory?: Conversation;
|
|
106
|
+
activeAgentId: AgentId | null;
|
|
107
|
+
state: TState;
|
|
108
|
+
chatContext: TContext;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Response from ChatSession.send()
|
|
112
|
+
*/
|
|
113
|
+
interface ChatResponse<TState extends BaseState = BaseState> {
|
|
114
|
+
responseMessage: string;
|
|
115
|
+
messageMetadata?: MessageMetadata;
|
|
116
|
+
activeAgentId: AgentId | null;
|
|
117
|
+
nextAgentId: AgentId | null;
|
|
118
|
+
state: TState;
|
|
119
|
+
goalAchieved: boolean;
|
|
120
|
+
sessionId: string;
|
|
121
|
+
widgets?: object;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Configuration for creating a ChatSession
|
|
125
|
+
*/
|
|
126
|
+
interface ChatSessionConfig<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> {
|
|
127
|
+
/** Unique session identifier */
|
|
128
|
+
sessionId: string;
|
|
129
|
+
/** Storage adapter for session persistence */
|
|
130
|
+
store?: SessionStore<TState, TContext>;
|
|
131
|
+
/** Initial agent to start the conversation */
|
|
132
|
+
initialAgentId: AgentId;
|
|
133
|
+
/** Initial state (used when creating new sessions) */
|
|
134
|
+
initialState?: TState;
|
|
135
|
+
/** Initial chat context (used when creating new sessions) */
|
|
136
|
+
initialChatContext?: TContext;
|
|
137
|
+
initialMessages?: Message[];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
type StreamEvent<TState extends BaseState = BaseState> = {
|
|
141
|
+
type: 'RunStarted';
|
|
142
|
+
runId?: string;
|
|
143
|
+
threadId?: string;
|
|
144
|
+
input?: unknown;
|
|
145
|
+
} | {
|
|
146
|
+
type: 'TextMessageStart';
|
|
147
|
+
messageId: string;
|
|
148
|
+
role?: string;
|
|
149
|
+
} | {
|
|
150
|
+
type: 'TextMessageContent';
|
|
151
|
+
delta: string;
|
|
152
|
+
agentId: AgentId;
|
|
153
|
+
messageId?: string;
|
|
154
|
+
} | {
|
|
155
|
+
type: 'TextMessageEnd';
|
|
156
|
+
messageId: string;
|
|
157
|
+
} | {
|
|
158
|
+
type: 'ToolCallStart';
|
|
159
|
+
toolCallId: string;
|
|
160
|
+
toolCallName: string;
|
|
161
|
+
parentMessageId?: string;
|
|
162
|
+
} | {
|
|
163
|
+
type: 'ToolCallArgs';
|
|
164
|
+
toolCallId: string;
|
|
165
|
+
delta: string;
|
|
166
|
+
} | {
|
|
167
|
+
type: 'ToolCallEnd';
|
|
168
|
+
toolCallId: string;
|
|
169
|
+
} | {
|
|
170
|
+
type: 'ToolCallResult';
|
|
171
|
+
data: Record<string, unknown>;
|
|
172
|
+
agentId: AgentId;
|
|
173
|
+
toolCallId?: string;
|
|
174
|
+
} | {
|
|
175
|
+
type: 'AgentTransition';
|
|
176
|
+
fromAgentId: AgentId;
|
|
177
|
+
toAgentId: AgentId;
|
|
178
|
+
} | {
|
|
179
|
+
type: 'RunFinished';
|
|
180
|
+
response: ChatResponse<TState>;
|
|
181
|
+
} | {
|
|
182
|
+
type: 'RunError';
|
|
183
|
+
error: Error;
|
|
184
|
+
agentId: AgentId;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Agent IDs are string-based to support dynamic registration.
|
|
189
|
+
* Consumers can define their own agent IDs without modifying the core library.
|
|
190
|
+
*/
|
|
191
|
+
type AgentId = string;
|
|
192
|
+
interface FlowInput<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> {
|
|
193
|
+
agent: Agent<TState, TContext>;
|
|
194
|
+
sessionData: SessionData<TState, TContext>;
|
|
195
|
+
lastAgentUnsentMessage?: string;
|
|
196
|
+
}
|
|
197
|
+
type AgentResponseResult<TState extends BaseState = BaseState> = {
|
|
198
|
+
newState: TState;
|
|
199
|
+
chatbotMessage: string;
|
|
200
|
+
messageMetadata?: MessageMetadata;
|
|
201
|
+
goalAchieved: boolean;
|
|
202
|
+
nextAgentId: AgentId | null;
|
|
203
|
+
redirectToAgent?: AgentId | null;
|
|
204
|
+
widgets?: object;
|
|
205
|
+
};
|
|
206
|
+
type GenerateAgentResponseSpecification<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> = (input: FlowInput<TState, TContext>) => Promise<AgentResponseResult<TState>>;
|
|
207
|
+
type GenerateAgentResponseStreamSpecification<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> = (input: FlowInput<TState, TContext>) => AsyncGenerator<StreamEvent<TState>, AgentResponseResult<TState>>;
|
|
208
|
+
/**
|
|
209
|
+
* Output schema for an agent.
|
|
210
|
+
* Base fields (chatbotMessage, goalAchieved, redirectToAgent) are added automatically.
|
|
211
|
+
*/
|
|
212
|
+
type AgentOutputSchema<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> = z.ZodObject<{
|
|
213
|
+
[key: string]: z.ZodTypeAny;
|
|
214
|
+
}> | ((sessionData: SessionData<TState, TContext>) => z.ZodObject<{
|
|
215
|
+
[key: string]: z.ZodTypeAny;
|
|
216
|
+
}>);
|
|
217
|
+
type Agent<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> = {
|
|
218
|
+
id: AgentId;
|
|
219
|
+
/** Description of the agent's purpose, used for agent pool discovery */
|
|
220
|
+
description: string;
|
|
221
|
+
modelId?: string;
|
|
222
|
+
name: string;
|
|
223
|
+
promptGenerator: (params: FlowInput<TState, TContext>) => Promise<string>;
|
|
224
|
+
outputSchema: AgentOutputSchema<TState, TContext>;
|
|
225
|
+
generateAgentResponse: GenerateAgentResponseSpecification<TState, TContext>;
|
|
226
|
+
/** Optional streaming response generator. Falls back to simpleAgentResponseStream. */
|
|
227
|
+
generateAgentResponseStream?: GenerateAgentResponseStreamSpecification<TState, TContext>;
|
|
228
|
+
fitDataInGeneralFormat: (data: any, state: TState) => TState;
|
|
229
|
+
nextAgentSelector?: (state: TState, agentGoalAchieved: boolean) => AgentId;
|
|
230
|
+
/** Agent IDs this agent can transition to. Used to constrain redirectToAgent in the schema. */
|
|
231
|
+
transitionsTo?: AgentId[];
|
|
232
|
+
filterHistoryStrategy?: (messages: Conversation) => Conversation;
|
|
233
|
+
/** Zod schemas defining the usage of widgets available to the agent */
|
|
234
|
+
widgets?: Record<string, z.ZodObject<any>> | ((sessionData: SessionData<TState, TContext>) => Record<string, z.ZodObject<any>>);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
declare const prepareAgentRequest: <TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>({ agent, sessionData, lastAgentUnsentMessage, }: FlowInput<TState, TContext>) => Promise<{
|
|
238
|
+
systemPrompt: string;
|
|
239
|
+
fullOutputSchema: zod.ZodObject<{
|
|
240
|
+
chatbotMessage: zod.ZodString;
|
|
241
|
+
goalAchieved: zod.ZodBoolean;
|
|
242
|
+
redirectToAgent: zod.ZodNullable<zod.ZodString> | zod.ZodNullable<zod.ZodEnum<{
|
|
243
|
+
[x: string]: string;
|
|
244
|
+
}>>;
|
|
245
|
+
conversationPayload: zod.ZodObject<{
|
|
246
|
+
[key: string]: zod.ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>>;
|
|
247
|
+
}, zod_v4_core.$strip>;
|
|
248
|
+
widgets: zod.ZodOptional<zod.ZodPipe<zod.ZodTransform<unknown, unknown>, zod.ZodUnion<readonly [zod.ZodObject<{}, zod_v4_core.$strip>, zod.ZodLiteral<"">, zod.ZodNull]> | zod.ZodUnion<readonly [zod.ZodObject<{}, zod_v4_core.$strip>, zod.ZodUnion<readonly [zod.ZodObject<{}, zod_v4_core.$strip>, zod.ZodLiteral<"">, zod.ZodNull]>]>>>;
|
|
249
|
+
}, zod_v4_core.$strip>;
|
|
250
|
+
}>;
|
|
251
|
+
declare const getAgentResponse: <TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>({ agent, sessionData, lastAgentUnsentMessage, }: FlowInput<TState, TContext>) => Promise<{
|
|
252
|
+
chatbotMessage: string;
|
|
253
|
+
goalAchieved: boolean;
|
|
254
|
+
redirectToAgent: string | null;
|
|
255
|
+
conversationPayload: Record<string, unknown>;
|
|
256
|
+
widgets?: "" | Record<string, never> | null | undefined;
|
|
257
|
+
}>;
|
|
258
|
+
declare const processAgentResponseData: <TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>(agent: Agent<TState, TContext>, sessionData: SessionData<TState, TContext>, data: Record<string, unknown>) => {
|
|
259
|
+
newState: TState;
|
|
260
|
+
chatbotMessage: string;
|
|
261
|
+
goalAchieved: boolean;
|
|
262
|
+
nextAgentId: string;
|
|
263
|
+
widgets: object | undefined;
|
|
264
|
+
};
|
|
265
|
+
declare const simpleAgentResponse: <TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>({ agent, sessionData, lastAgentUnsentMessage, }: FlowInput<TState, TContext>) => Promise<Awaited<ReturnType<GenerateAgentResponseSpecification<TState, TContext>>>>;
|
|
266
|
+
declare function getAgentResponseStream<TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>({ agent, sessionData, lastAgentUnsentMessage }: FlowInput<TState, TContext>): AsyncGenerator<StreamEvent<TState>>;
|
|
267
|
+
declare function simpleAgentResponseStream<TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>({ agent, sessionData, lastAgentUnsentMessage, }: FlowInput<TState, TContext>): AsyncGenerator<StreamEvent<TState>, AgentResponseResult<TState>>;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* AgentRegistry - A centralized singleton registry for managing agents.
|
|
271
|
+
*
|
|
272
|
+
* This allows consumers of the library to dynamically register their own agents
|
|
273
|
+
* at runtime, enabling modular and extensible agent architectures.
|
|
274
|
+
*
|
|
275
|
+
* The registry is generic over TState - all agents share the same state type.
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```typescript
|
|
279
|
+
* import { AgentRegistry } from '@genui-a3/core'
|
|
280
|
+
*
|
|
281
|
+
* interface State extends BaseState {
|
|
282
|
+
* userName?: string
|
|
283
|
+
* }
|
|
284
|
+
*
|
|
285
|
+
* // Get a typed registry instance
|
|
286
|
+
* const registry = AgentRegistry.getInstance<State>()
|
|
287
|
+
*
|
|
288
|
+
* // Register a single agent
|
|
289
|
+
* registry.register(myAgent)
|
|
290
|
+
*
|
|
291
|
+
* // Register multiple agents at once
|
|
292
|
+
* registry.register([agent1, agent2, agent3])
|
|
293
|
+
*
|
|
294
|
+
* const agent = registry.get('my-agent-id')
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
declare class AgentRegistry<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> {
|
|
298
|
+
private static instance;
|
|
299
|
+
private agents;
|
|
300
|
+
private constructor();
|
|
301
|
+
static getInstance<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext>(): AgentRegistry<TState, TContext>;
|
|
302
|
+
/**
|
|
303
|
+
* Resets the singleton instance. Primarily useful for testing.
|
|
304
|
+
*/
|
|
305
|
+
static resetInstance(): void;
|
|
306
|
+
/**
|
|
307
|
+
* Registers one or more agents with the registry.
|
|
308
|
+
*
|
|
309
|
+
* @param agents - A single agent or array of agents to register
|
|
310
|
+
* @throws Error if any agent ID is already registered
|
|
311
|
+
*/
|
|
312
|
+
register(agents: Agent<TState, TContext> | Agent<TState, TContext>[]): void;
|
|
313
|
+
/**
|
|
314
|
+
* Retrieves an agent by its ID.
|
|
315
|
+
*
|
|
316
|
+
* @param id - The agent ID to look up
|
|
317
|
+
* @returns The agent if found, undefined otherwise
|
|
318
|
+
*/
|
|
319
|
+
get(id: AgentId): Agent<TState, TContext> | undefined;
|
|
320
|
+
/**
|
|
321
|
+
* Returns all registered agents.
|
|
322
|
+
*
|
|
323
|
+
* @returns Array of all registered agents
|
|
324
|
+
*/
|
|
325
|
+
getAll(): Agent<TState, TContext>[];
|
|
326
|
+
/**
|
|
327
|
+
* Checks if an agent with the given ID is registered.
|
|
328
|
+
*
|
|
329
|
+
* @param id - The agent ID to check
|
|
330
|
+
* @returns true if the agent is registered, false otherwise
|
|
331
|
+
*/
|
|
332
|
+
has(id: AgentId): boolean;
|
|
333
|
+
/**
|
|
334
|
+
* Unregisters an agent by its ID.
|
|
335
|
+
*
|
|
336
|
+
* @param id - The agent ID to unregister
|
|
337
|
+
* @returns true if the agent was found and removed, false otherwise
|
|
338
|
+
*/
|
|
339
|
+
unregister(id: AgentId): boolean;
|
|
340
|
+
/**
|
|
341
|
+
* Returns a record of all agent IDs to their descriptions.
|
|
342
|
+
* Useful for generating agent pool documentation.
|
|
343
|
+
*
|
|
344
|
+
* @returns Record mapping agent IDs to descriptions
|
|
345
|
+
*/
|
|
346
|
+
getDescriptions(): Record<AgentId, string>;
|
|
347
|
+
/**
|
|
348
|
+
* Returns the count of registered agents.
|
|
349
|
+
*
|
|
350
|
+
* @returns Number of registered agents
|
|
351
|
+
*/
|
|
352
|
+
get count(): number;
|
|
353
|
+
/**
|
|
354
|
+
* Clears all registered agents from the registry.
|
|
355
|
+
* Primarily useful for testing.
|
|
356
|
+
*/
|
|
357
|
+
clear(): void;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
declare const manageFlow: <TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>({ sessionData, lastAgentUnsentMessage, _depth, }: FlowInput<TState, TContext> & {
|
|
361
|
+
_depth?: number;
|
|
362
|
+
}) => Promise<ChatResponse<TState>>;
|
|
363
|
+
declare function manageFlowStream<TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>({ sessionData, lastAgentUnsentMessage, _depth, }: FlowInput<TState, TContext> & {
|
|
364
|
+
_depth?: number;
|
|
365
|
+
}): AsyncGenerator<StreamEvent<TState>>;
|
|
366
|
+
|
|
367
|
+
declare const baseResponseSchema: z.ZodObject<{
|
|
368
|
+
chatbotMessage: z.ZodString;
|
|
369
|
+
goalAchieved: z.ZodBoolean;
|
|
370
|
+
redirectToAgent: z.ZodNullable<z.ZodString>;
|
|
371
|
+
conversationPayload: z.ZodAny;
|
|
372
|
+
widgets: z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{}, z.core.$strip>, z.ZodLiteral<"">, z.ZodNull]>>;
|
|
373
|
+
}, z.core.$strip>;
|
|
374
|
+
type BaseResponse = z.infer<typeof baseResponseSchema>;
|
|
375
|
+
/**
|
|
376
|
+
* Creates a full output schema by merging base fields with the agent's outputSchema.
|
|
377
|
+
* If transitionsTo is provided, redirectToAgent is constrained to those values.
|
|
378
|
+
*/
|
|
379
|
+
declare function createFullOutputSchema<T extends z.ZodObject<{
|
|
380
|
+
[key: string]: z.ZodTypeAny;
|
|
381
|
+
}>>(outputSchema: T, transitionsTo?: AgentId[], widgets?: Record<string, z.ZodObject>): z.ZodObject<{
|
|
382
|
+
chatbotMessage: z.ZodString;
|
|
383
|
+
goalAchieved: z.ZodBoolean;
|
|
384
|
+
redirectToAgent: z.ZodNullable<z.ZodString> | z.ZodNullable<z.ZodEnum<{
|
|
385
|
+
[x: string]: string;
|
|
386
|
+
}>>;
|
|
387
|
+
conversationPayload: T;
|
|
388
|
+
widgets: z.ZodOptional<z.ZodPipe<z.ZodTransform<unknown, unknown>, z.ZodUnion<readonly [z.ZodObject<{}, z.core.$strip>, z.ZodLiteral<"">, z.ZodNull]> | z.ZodUnion<readonly [z.ZodObject<{}, z.core.$strip>, z.ZodUnion<readonly [z.ZodObject<{}, z.core.$strip>, z.ZodLiteral<"">, z.ZodNull]>]>>>;
|
|
389
|
+
}, z.core.$strip>;
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* ChatSession encapsulates the complete message lifecycle.
|
|
393
|
+
*
|
|
394
|
+
* @example
|
|
395
|
+
* ```typescript
|
|
396
|
+
* const session = new ChatSession({
|
|
397
|
+
* sessionId: 'user-123',
|
|
398
|
+
* store: new RedisSessionStore(redis),
|
|
399
|
+
* initialAgentId: 'greeting',
|
|
400
|
+
* })
|
|
401
|
+
*
|
|
402
|
+
* const result = await session.send('Hello!')
|
|
403
|
+
* ```
|
|
404
|
+
*/
|
|
405
|
+
declare class ChatSession<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> {
|
|
406
|
+
private readonly sessionId;
|
|
407
|
+
private readonly store;
|
|
408
|
+
private readonly initialAgentId;
|
|
409
|
+
private readonly initialState;
|
|
410
|
+
private readonly initialChatContext;
|
|
411
|
+
private readonly initialMessages?;
|
|
412
|
+
constructor(config: ChatSessionConfig<TState, TContext>);
|
|
413
|
+
/**
|
|
414
|
+
* Send a message and get a response.
|
|
415
|
+
*
|
|
416
|
+
* Flow:
|
|
417
|
+
* 1. Load existing session or create new
|
|
418
|
+
* 2. Append user message to history
|
|
419
|
+
* 3. Run manageFlow with current agent
|
|
420
|
+
* 4. Append assistant response to history
|
|
421
|
+
* 5. Save updated session
|
|
422
|
+
* 6. Return response
|
|
423
|
+
*/
|
|
424
|
+
send(message: string): Promise<ChatResponse<TState>>;
|
|
425
|
+
/**
|
|
426
|
+
* Send a message and stream the response as an async generator of StreamEvents.
|
|
427
|
+
*
|
|
428
|
+
* Text deltas are yielded immediately for real-time rendering.
|
|
429
|
+
* Session state is persisted after the stream is fully consumed.
|
|
430
|
+
*/
|
|
431
|
+
sendStream(message: string): AsyncGenerator<StreamEvent<TState>>;
|
|
432
|
+
/**
|
|
433
|
+
* Prepares the session data for sending a message.
|
|
434
|
+
* Loads or creates the session, appends the user message, and returns the active agent.
|
|
435
|
+
*/
|
|
436
|
+
private beforeProcessMessage;
|
|
437
|
+
/**
|
|
438
|
+
* Executes the agent flow to generate a completely buffered response.
|
|
439
|
+
*/
|
|
440
|
+
private processMessage;
|
|
441
|
+
/**
|
|
442
|
+
* Streams the agent flow execution event by event.
|
|
443
|
+
*/
|
|
444
|
+
private processMessageStream;
|
|
445
|
+
/**
|
|
446
|
+
* Finalizes the response by appending the assistant message, updating state,
|
|
447
|
+
* refreshing chat context, and persisting the session.
|
|
448
|
+
*/
|
|
449
|
+
private afterProcessMessage;
|
|
450
|
+
/**
|
|
451
|
+
* Gets or initializes session if it doesn't exist.
|
|
452
|
+
*/
|
|
453
|
+
getOrCreateSessionData(): Promise<SessionData<TState, TContext>>;
|
|
454
|
+
/**
|
|
455
|
+
* Get current session data without sending a message.
|
|
456
|
+
*/
|
|
457
|
+
getSessionData(): Promise<SessionData<TState, TContext> | null>;
|
|
458
|
+
upsertSessionData(updates: Partial<SessionData<TState, TContext>>): Promise<void>;
|
|
459
|
+
/**
|
|
460
|
+
* Get conversation history.
|
|
461
|
+
*/
|
|
462
|
+
getHistory(): Promise<Message[]>;
|
|
463
|
+
/**
|
|
464
|
+
* Clear/delete the session.
|
|
465
|
+
*/
|
|
466
|
+
clear(): Promise<void>;
|
|
467
|
+
private createInitialSession;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* In-memory session store for development and testing.
|
|
472
|
+
* Sessions are lost on process restart.
|
|
473
|
+
*
|
|
474
|
+
* @example
|
|
475
|
+
* ```typescript
|
|
476
|
+
* const store = new MemorySessionStore<MyState, MyContext>()
|
|
477
|
+
* const session = new ChatSession({
|
|
478
|
+
* sessionId: 'test-123',
|
|
479
|
+
* store,
|
|
480
|
+
* initialAgentId: 'greeting',
|
|
481
|
+
* })
|
|
482
|
+
* ```
|
|
483
|
+
*/
|
|
484
|
+
declare class MemorySessionStore<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> implements SessionStore<TState, TContext> {
|
|
485
|
+
private sessions;
|
|
486
|
+
load(sessionId: string): Promise<SessionData<TState, TContext> | null>;
|
|
487
|
+
save(sessionId: string, data: SessionData<TState, TContext>): Promise<void>;
|
|
488
|
+
delete(sessionId: string): Promise<void>;
|
|
489
|
+
/** Clear all sessions (useful for testing) */
|
|
490
|
+
clear(): void;
|
|
491
|
+
/** Get number of active sessions */
|
|
492
|
+
get size(): number;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
declare class AgentCoreMemoryStore<TState extends BaseState = BaseState, TContext extends BaseChatContext = BaseChatContext> implements SessionStore<TState, TContext> {
|
|
496
|
+
private client;
|
|
497
|
+
private memoryId;
|
|
498
|
+
constructor(config: {
|
|
499
|
+
region: string;
|
|
500
|
+
memoryId: string;
|
|
501
|
+
});
|
|
502
|
+
load(sessionId: string): Promise<SessionData<TState, TContext> | null>;
|
|
503
|
+
save(sessionId: string, data: SessionData<TState, TContext>): Promise<void>;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
declare function mergeSequentialMessages(conversation: Conversation): Message$1[];
|
|
507
|
+
|
|
508
|
+
type SendWithModelParams = {
|
|
509
|
+
modelArn: string;
|
|
510
|
+
modelName: string;
|
|
511
|
+
systemPrompt: string;
|
|
512
|
+
mergedMessages: ReturnType<typeof mergeSequentialMessages>;
|
|
513
|
+
inputSchema: ToolInputSchema | undefined;
|
|
514
|
+
};
|
|
515
|
+
declare function sendWithModel(params: SendWithModelParams): Promise<string>;
|
|
516
|
+
declare function sendStreamWithModel(params: SendWithModelParams): Promise<AsyncIterable<ConverseStreamOutput>>;
|
|
517
|
+
type SendChatRequestParams<TState extends BaseState, TContext extends BaseChatContext = BaseChatContext> = {
|
|
518
|
+
agent: Agent<TState, TContext>;
|
|
519
|
+
systemPrompt: string;
|
|
520
|
+
basePrompt: string;
|
|
521
|
+
conversation: Conversation;
|
|
522
|
+
responseFormat: ZodType;
|
|
523
|
+
};
|
|
524
|
+
declare const sendChatRequest: <TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>(params: SendChatRequestParams<TState, TContext>) => Promise<string>;
|
|
525
|
+
declare const sendChatRequestStream: <TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>(params: SendChatRequestParams<TState, TContext>) => Promise<AsyncIterable<ConverseStreamOutput>>;
|
|
526
|
+
|
|
527
|
+
declare const EXAMPLE_CONSTANT = "";
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Generates a formatted string listing the agents based on the provided agent IDs.
|
|
531
|
+
* The format matches the one used in basePrompt.ts:
|
|
532
|
+
* - 'agentId': Description of the agent
|
|
533
|
+
*
|
|
534
|
+
* @param agentIds - Array of AgentId values representing the agents to include
|
|
535
|
+
* @returns A formatted string listing the agents and their descriptions
|
|
536
|
+
*
|
|
537
|
+
* @example
|
|
538
|
+
* ```typescript
|
|
539
|
+
* // Create a formatted string with consent and basic patient info agents
|
|
540
|
+
* const agentsList = generateAgentPool([AgentId.CONSENT, AgentId.BASIC_PATIENT_INFO]);
|
|
541
|
+
* // Result will be:
|
|
542
|
+
* // - 'consent': Collects the user's consent to be assisted by the agent
|
|
543
|
+
* // - 'basicPatientInfo': Collects basic patient information (e.g., user name, user DOB, reason for visit)
|
|
544
|
+
* ```
|
|
545
|
+
*/
|
|
546
|
+
declare function generateAgentPool<TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>(agents: Agent<TState, TContext>[], agentIds: AgentId[]): string;
|
|
547
|
+
/**
|
|
548
|
+
* Generates a formatted string for a single agent item
|
|
549
|
+
*
|
|
550
|
+
* @param agentId - The ID of the agent to format
|
|
551
|
+
* @returns A formatted string for the agent
|
|
552
|
+
* @throws Error if the agent ID is invalid or has no description
|
|
553
|
+
*/
|
|
554
|
+
declare function generateAgentPoolItem(agentId: AgentId): string;
|
|
555
|
+
/**
|
|
556
|
+
* Gets the agent IDs that should be included in the agent pool for a given agent.
|
|
557
|
+
* Returns the current agent's ID plus any agents it can transition to.
|
|
558
|
+
*
|
|
559
|
+
* @param agent - The agent to get the pool IDs for
|
|
560
|
+
* @returns An array of AgentId values
|
|
561
|
+
*/
|
|
562
|
+
declare function getAgentPoolIds<TState extends BaseState, TContext extends BaseChatContext = BaseChatContext>(agent: Agent<TState, TContext>): AgentId[];
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* These instructions help prevent the agent from being overly helpful and making up information
|
|
566
|
+
*/
|
|
567
|
+
declare const getAntiHallucinationPrompt: (subject: string, wrapUpAgentId?: AgentId) => string;
|
|
568
|
+
|
|
569
|
+
export { type AgUIEvent, type Agent, AgentCoreMemoryStore, type AgentId, type AgentOutputSchema, AgentRegistry, type AgentResponseResult, type BaseChatContext, type BaseResponse, type BaseState, type BaseWidget, type ChatResponse, ChatSession, type ChatSessionConfig, type Conversation, EXAMPLE_CONSTANT, type FlowInput, type GenerateAgentResponseSpecification, type GenerateAgentResponseStreamSpecification, MemorySessionStore, type Message, type MessageMetadata, MessageSender, type SendChatRequestParams, type SessionData, type SessionStore, type StreamEvent, ToolName, type ToolResult, type Widget, type Widget1, WidgetType, createFullOutputSchema, generateAgentPool, generateAgentPoolItem, getAgentPoolIds, getAgentResponse, getAgentResponseStream, getAntiHallucinationPrompt, manageFlow, manageFlowStream, mergeSequentialMessages, prepareAgentRequest, processAgentResponseData, sendChatRequest, sendChatRequestStream, sendStreamWithModel, sendWithModel, simpleAgentResponse, simpleAgentResponseStream };
|