@falai/agent 0.9.0-alpha-2 → 0.9.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/cjs/src/core/Agent.d.ts +31 -41
- package/dist/cjs/src/core/Agent.d.ts.map +1 -1
- package/dist/cjs/src/core/Agent.js +72 -1075
- package/dist/cjs/src/core/Agent.js.map +1 -1
- package/dist/cjs/src/core/ResponseModal.d.ts +205 -0
- package/dist/cjs/src/core/ResponseModal.d.ts.map +1 -0
- package/dist/cjs/src/core/ResponseModal.js +1328 -0
- package/dist/cjs/src/core/ResponseModal.js.map +1 -0
- package/dist/cjs/src/core/ResponsePipeline.js +1 -1
- package/dist/cjs/src/core/ResponsePipeline.js.map +1 -1
- package/dist/cjs/src/index.d.ts +3 -1
- package/dist/cjs/src/index.d.ts.map +1 -1
- package/dist/cjs/src/index.js +7 -1
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/types/agent.d.ts +1 -0
- package/dist/cjs/src/types/agent.d.ts.map +1 -1
- package/dist/cjs/src/types/ai.d.ts +1 -1
- package/dist/cjs/src/types/ai.d.ts.map +1 -1
- package/dist/cjs/src/utils/clone.d.ts.map +1 -1
- package/dist/cjs/src/utils/clone.js +0 -4
- package/dist/cjs/src/utils/clone.js.map +1 -1
- package/dist/cjs/src/utils/history.d.ts +30 -1
- package/dist/cjs/src/utils/history.d.ts.map +1 -1
- package/dist/cjs/src/utils/history.js +169 -23
- package/dist/cjs/src/utils/history.js.map +1 -1
- package/dist/cjs/src/utils/index.d.ts +1 -1
- package/dist/cjs/src/utils/index.d.ts.map +1 -1
- package/dist/cjs/src/utils/index.js +5 -1
- package/dist/cjs/src/utils/index.js.map +1 -1
- package/dist/src/core/Agent.d.ts +31 -41
- package/dist/src/core/Agent.d.ts.map +1 -1
- package/dist/src/core/Agent.js +73 -1076
- package/dist/src/core/Agent.js.map +1 -1
- package/dist/src/core/ResponseModal.d.ts +205 -0
- package/dist/src/core/ResponseModal.d.ts.map +1 -0
- package/dist/src/core/ResponseModal.js +1323 -0
- package/dist/src/core/ResponseModal.js.map +1 -0
- package/dist/src/core/ResponsePipeline.js +1 -1
- package/dist/src/core/ResponsePipeline.js.map +1 -1
- package/dist/src/index.d.ts +3 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/types/agent.d.ts +1 -0
- package/dist/src/types/agent.d.ts.map +1 -1
- package/dist/src/types/ai.d.ts +1 -1
- package/dist/src/types/ai.d.ts.map +1 -1
- package/dist/src/utils/clone.d.ts.map +1 -1
- package/dist/src/utils/clone.js +0 -4
- package/dist/src/utils/clone.js.map +1 -1
- package/dist/src/utils/history.d.ts +30 -1
- package/dist/src/utils/history.d.ts.map +1 -1
- package/dist/src/utils/history.js +165 -23
- package/dist/src/utils/history.js.map +1 -1
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.d.ts.map +1 -1
- package/dist/src/utils/index.js +1 -1
- package/dist/src/utils/index.js.map +1 -1
- package/docs/README.md +2 -1
- package/docs/api/README.md +160 -0
- package/docs/api/overview.md +66 -1
- package/docs/guides/migration/README.md +72 -0
- package/docs/guides/migration/response-modal-refactor.md +518 -0
- package/examples/advanced-patterns/streaming-responses.ts +169 -96
- package/examples/core-concepts/modern-streaming-api.ts +309 -0
- package/package.json +1 -1
- package/src/core/Agent.ts +95 -1488
- package/src/core/ResponseModal.ts +1722 -0
- package/src/core/ResponsePipeline.ts +1 -1
- package/src/index.ts +11 -0
- package/src/types/agent.ts +1 -0
- package/src/types/ai.ts +1 -1
- package/src/utils/clone.ts +6 -8
- package/src/utils/history.ts +190 -27
- package/src/utils/index.ts +4 -0
|
@@ -670,7 +670,7 @@ export class ResponsePipeline<TContext = unknown, TData = unknown> {
|
|
|
670
670
|
string,
|
|
671
671
|
Tool<TContext, TData, unknown[], unknown>
|
|
672
672
|
>();
|
|
673
|
-
for (const toolId of allowedToolIds) {
|
|
673
|
+
for (const toolId of Array.from(allowedToolIds)) {
|
|
674
674
|
const tool = availableTools.get(toolId);
|
|
675
675
|
if (tool) {
|
|
676
676
|
filteredTools.set(toolId, tool);
|
package/src/index.ts
CHANGED
|
@@ -8,6 +8,13 @@
|
|
|
8
8
|
export { Agent } from "./core/Agent";
|
|
9
9
|
export { Route } from "./core/Route";
|
|
10
10
|
export { Step } from "./core/Step";
|
|
11
|
+
export { ResponseModal } from "./core/ResponseModal";
|
|
12
|
+
export type {
|
|
13
|
+
ResponseModalOptions,
|
|
14
|
+
RespondParams,
|
|
15
|
+
StreamOptions,
|
|
16
|
+
GenerateOptions
|
|
17
|
+
} from "./core/ResponseModal";
|
|
11
18
|
export { adaptEvent, convertHistoryToEvents } from "./core/Events";
|
|
12
19
|
export { PersistenceManager } from "./core/PersistenceManager";
|
|
13
20
|
export { SessionManager } from "./core/SessionManager";
|
|
@@ -67,6 +74,10 @@ export { generateRouteId, generateStepId, generateToolId } from "./utils/id";
|
|
|
67
74
|
export { formatKnowledgeBase } from "./utils/template";
|
|
68
75
|
export {
|
|
69
76
|
normalizeHistory,
|
|
77
|
+
historyItemToEvent,
|
|
78
|
+
historyToEvents,
|
|
79
|
+
eventToHistoryItem,
|
|
80
|
+
eventsToHistory,
|
|
70
81
|
userMessage,
|
|
71
82
|
assistantMessage,
|
|
72
83
|
toolMessage,
|
package/src/types/agent.ts
CHANGED
package/src/types/ai.ts
CHANGED
|
@@ -66,7 +66,7 @@ export interface GenerateMessageInput<TContext = unknown> {
|
|
|
66
66
|
/**
|
|
67
67
|
* Structured response from AI containing message and metadata
|
|
68
68
|
*/
|
|
69
|
-
export interface AgentStructuredResponse {
|
|
69
|
+
export interface AgentStructuredResponse extends Record<string, unknown> {
|
|
70
70
|
/** The actual message to send to the user */
|
|
71
71
|
message: string;
|
|
72
72
|
/** Route chosen by the agent (route title or null if no route) */
|
package/src/utils/clone.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
2
|
/**
|
|
3
3
|
* Deep clone utility to avoid adding large dependencies like lodash.
|
|
4
4
|
* Handles objects, arrays, dates, and primitives.
|
|
@@ -11,21 +11,19 @@ export function cloneDeep<T>(obj: T): T {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
if (obj instanceof Date) {
|
|
14
|
-
|
|
15
|
-
return new Date(obj.getTime()) as any;
|
|
14
|
+
return new Date(obj.getTime()) as T;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
if (Array.isArray(obj)) {
|
|
19
|
-
const arrCopy
|
|
18
|
+
const arrCopy: unknown[] = [];
|
|
20
19
|
for (let i = 0; i < obj.length; i++) {
|
|
21
|
-
|
|
20
|
+
|
|
22
21
|
arrCopy[i] = cloneDeep(obj[i]);
|
|
23
22
|
}
|
|
24
|
-
|
|
25
|
-
return arrCopy as any;
|
|
23
|
+
return arrCopy as T;
|
|
26
24
|
}
|
|
27
25
|
|
|
28
|
-
const objCopy
|
|
26
|
+
const objCopy: Record<string, unknown> = {};
|
|
29
27
|
for (const key in obj) {
|
|
30
28
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
31
29
|
objCopy[key] = cloneDeep((obj as Record<string, unknown>)[key]);
|
package/src/utils/history.ts
CHANGED
|
@@ -3,33 +3,53 @@
|
|
|
3
3
|
* Convert simplified history format to internal Event format
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import type { Event, MessageEventData } from "../types";
|
|
6
|
+
import type { Event, MessageEventData, ToolEventData, StatusEventData } from "../types";
|
|
7
7
|
import { EventKind, MessageRole } from "../types";
|
|
8
|
-
import type { History, HistoryItem } from "../types/history";
|
|
8
|
+
import type { History, HistoryItem, UserHistoryItem, AssistantHistoryItem, ToolHistoryItem } from "../types/history";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Convert a simplified history item to an internal Event
|
|
12
|
+
* @param item - The HistoryItem to convert
|
|
13
|
+
* @returns Event with proper type-safe structure
|
|
14
|
+
* @throws Error if the history item is malformed or has invalid data
|
|
12
15
|
*/
|
|
13
|
-
function
|
|
16
|
+
export function historyItemToEvent(item: HistoryItem): Event<MessageEventData | ToolEventData | StatusEventData> {
|
|
17
|
+
if (!item || typeof item !== 'object') {
|
|
18
|
+
throw new Error('Invalid history item: must be a non-null object');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!item.role || typeof item.role !== 'string') {
|
|
22
|
+
throw new Error('Invalid history item: role is required and must be a string');
|
|
23
|
+
}
|
|
24
|
+
|
|
14
25
|
const timestamp = new Date().toISOString();
|
|
15
26
|
|
|
16
27
|
switch (item.role) {
|
|
17
28
|
case "user": {
|
|
29
|
+
const userItem = item;
|
|
30
|
+
if (typeof userItem.content !== 'string') {
|
|
31
|
+
throw new Error('Invalid user history item: content must be a string');
|
|
32
|
+
}
|
|
33
|
+
|
|
18
34
|
return {
|
|
19
35
|
kind: EventKind.MESSAGE,
|
|
20
36
|
source: MessageRole.USER,
|
|
21
37
|
timestamp,
|
|
22
38
|
data: {
|
|
23
39
|
participant: {
|
|
24
|
-
display_name:
|
|
40
|
+
display_name: userItem.name || "User",
|
|
25
41
|
},
|
|
26
|
-
message:
|
|
42
|
+
message: userItem.content,
|
|
27
43
|
},
|
|
28
44
|
};
|
|
29
45
|
}
|
|
30
46
|
case "assistant": {
|
|
31
|
-
|
|
32
|
-
|
|
47
|
+
const assistantItem = item;
|
|
48
|
+
if (assistantItem.content !== null && typeof assistantItem.content !== 'string') {
|
|
49
|
+
throw new Error('Invalid assistant history item: content must be a string or null');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const event: Event<MessageEventData> = {
|
|
33
53
|
kind: EventKind.MESSAGE,
|
|
34
54
|
source: MessageRole.ASSISTANT,
|
|
35
55
|
timestamp,
|
|
@@ -37,19 +57,36 @@ function convertHistoryItemToEvent(item: HistoryItem): Event {
|
|
|
37
57
|
participant: {
|
|
38
58
|
display_name: "Assistant",
|
|
39
59
|
},
|
|
40
|
-
message:
|
|
60
|
+
message: assistantItem.content || "",
|
|
41
61
|
},
|
|
42
62
|
};
|
|
43
63
|
|
|
44
|
-
// If there are tool calls,
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
64
|
+
// If there are tool calls, validate and add them
|
|
65
|
+
if (assistantItem.tool_calls && assistantItem.tool_calls.length > 0) {
|
|
66
|
+
if (!Array.isArray(assistantItem.tool_calls)) {
|
|
67
|
+
throw new Error('Invalid assistant history item: tool_calls must be an array');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (const toolCall of assistantItem.tool_calls) {
|
|
71
|
+
if (!toolCall.id || !toolCall.name || typeof toolCall.arguments !== 'object') {
|
|
72
|
+
throw new Error('Invalid tool call: id, name, and arguments are required');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
event.data.toolCalls = assistantItem.tool_calls;
|
|
48
77
|
}
|
|
49
78
|
|
|
50
79
|
return event;
|
|
51
80
|
}
|
|
52
81
|
case "tool": {
|
|
82
|
+
const toolItem = item;
|
|
83
|
+
if (!toolItem.tool_call_id || typeof toolItem.tool_call_id !== 'string') {
|
|
84
|
+
throw new Error('Invalid tool history item: tool_call_id is required and must be a string');
|
|
85
|
+
}
|
|
86
|
+
if (!toolItem.name || typeof toolItem.name !== 'string') {
|
|
87
|
+
throw new Error('Invalid tool history item: name is required and must be a string');
|
|
88
|
+
}
|
|
89
|
+
|
|
53
90
|
return {
|
|
54
91
|
kind: EventKind.TOOL,
|
|
55
92
|
source: MessageRole.AGENT,
|
|
@@ -57,10 +94,10 @@ function convertHistoryItemToEvent(item: HistoryItem): Event {
|
|
|
57
94
|
data: {
|
|
58
95
|
tool_calls: [
|
|
59
96
|
{
|
|
60
|
-
tool_id:
|
|
97
|
+
tool_id: toolItem.tool_call_id,
|
|
61
98
|
arguments: {}, // Tool results don't have arguments
|
|
62
99
|
result: {
|
|
63
|
-
data:
|
|
100
|
+
data: toolItem.content,
|
|
64
101
|
},
|
|
65
102
|
},
|
|
66
103
|
],
|
|
@@ -68,6 +105,11 @@ function convertHistoryItemToEvent(item: HistoryItem): Event {
|
|
|
68
105
|
};
|
|
69
106
|
}
|
|
70
107
|
case "system": {
|
|
108
|
+
const systemItem = item;
|
|
109
|
+
if (typeof systemItem.content !== 'string') {
|
|
110
|
+
throw new Error('Invalid system history item: content must be a string');
|
|
111
|
+
}
|
|
112
|
+
|
|
71
113
|
return {
|
|
72
114
|
kind: EventKind.MESSAGE,
|
|
73
115
|
source: MessageRole.SYSTEM,
|
|
@@ -76,31 +118,152 @@ function convertHistoryItemToEvent(item: HistoryItem): Event {
|
|
|
76
118
|
participant: {
|
|
77
119
|
display_name: "System",
|
|
78
120
|
},
|
|
79
|
-
message:
|
|
121
|
+
message: systemItem.content,
|
|
80
122
|
},
|
|
81
123
|
};
|
|
82
124
|
}
|
|
83
125
|
default:
|
|
84
|
-
|
|
126
|
+
throw new Error(`Invalid history item role: ${String((item as { role?: unknown }).role)}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Convert an array of HistoryItems to Events
|
|
132
|
+
* @param history - Array of HistoryItems to convert
|
|
133
|
+
* @returns Array of Events with proper type-safe structure
|
|
134
|
+
* @throws Error if any history item is malformed
|
|
135
|
+
*/
|
|
136
|
+
export function historyToEvents(history: History): Event<MessageEventData | ToolEventData | StatusEventData>[] {
|
|
137
|
+
if (!Array.isArray(history)) {
|
|
138
|
+
throw new Error('Invalid history: must be an array');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return history.map((item, index) => {
|
|
142
|
+
try {
|
|
143
|
+
return historyItemToEvent(item);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
throw new Error(`Error converting history item at index ${index}: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Convert an Event back to a HistoryItem
|
|
152
|
+
* @param event - The Event to convert
|
|
153
|
+
* @returns HistoryItem with simplified structure
|
|
154
|
+
* @throws Error if the event is malformed or has invalid data
|
|
155
|
+
*/
|
|
156
|
+
export function eventToHistoryItem(event: Event): HistoryItem {
|
|
157
|
+
if (!event || typeof event !== 'object') {
|
|
158
|
+
throw new Error('Invalid event: must be a non-null object');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!event.kind || !event.source || !event.data) {
|
|
162
|
+
throw new Error('Invalid event: kind, source, and data are required');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
switch (event.kind) {
|
|
166
|
+
case EventKind.MESSAGE: {
|
|
167
|
+
const messageData = event.data as MessageEventData;
|
|
168
|
+
if (!messageData.message && messageData.message !== '') {
|
|
169
|
+
throw new Error('Invalid message event: message is required');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
switch (event.source) {
|
|
173
|
+
case MessageRole.USER: {
|
|
174
|
+
const userItem: UserHistoryItem = {
|
|
175
|
+
role: "user",
|
|
176
|
+
content: messageData.message,
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
if (messageData.participant?.display_name && messageData.participant.display_name !== "User") {
|
|
180
|
+
userItem.name = messageData.participant.display_name;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return userItem;
|
|
184
|
+
}
|
|
185
|
+
case MessageRole.ASSISTANT: {
|
|
186
|
+
const assistantItem: AssistantHistoryItem = {
|
|
187
|
+
role: "assistant",
|
|
188
|
+
content: messageData.message || null,
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
if (messageData.toolCalls && messageData.toolCalls.length > 0) {
|
|
192
|
+
assistantItem.tool_calls = messageData.toolCalls;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return assistantItem;
|
|
196
|
+
}
|
|
197
|
+
case MessageRole.SYSTEM: {
|
|
198
|
+
return {
|
|
199
|
+
role: "system",
|
|
200
|
+
content: messageData.message,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
default:
|
|
204
|
+
throw new Error(`Unsupported message source for conversion: ${event.source}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
case EventKind.TOOL: {
|
|
208
|
+
const toolData = event.data as ToolEventData;
|
|
209
|
+
if (!toolData.tool_calls || !Array.isArray(toolData.tool_calls) || toolData.tool_calls.length === 0) {
|
|
210
|
+
throw new Error('Invalid tool event: tool_calls array is required and must not be empty');
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const firstToolCall = toolData.tool_calls[0];
|
|
214
|
+
if (!firstToolCall.tool_id) {
|
|
215
|
+
throw new Error('Invalid tool call: tool_id is required');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const toolItem: ToolHistoryItem = {
|
|
219
|
+
role: "tool",
|
|
220
|
+
tool_call_id: firstToolCall.tool_id,
|
|
221
|
+
name: firstToolCall.tool_id, // Use tool_id as name for simplicity
|
|
222
|
+
content: firstToolCall.result?.data,
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
return toolItem;
|
|
226
|
+
}
|
|
227
|
+
case EventKind.STATUS: {
|
|
228
|
+
// Status events don't have a direct HistoryItem equivalent
|
|
229
|
+
// Convert to system message for compatibility
|
|
230
|
+
const statusData = event.data as StatusEventData;
|
|
85
231
|
return {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
timestamp,
|
|
89
|
-
data: {
|
|
90
|
-
participant: {
|
|
91
|
-
display_name: "Unknown",
|
|
92
|
-
},
|
|
93
|
-
message: "Unknown message type",
|
|
94
|
-
},
|
|
232
|
+
role: "system",
|
|
233
|
+
content: statusData.status || "Status update",
|
|
95
234
|
};
|
|
235
|
+
}
|
|
236
|
+
default:
|
|
237
|
+
throw new Error(`Unsupported event kind for conversion: ${String(event.kind)}`);
|
|
96
238
|
}
|
|
97
239
|
}
|
|
98
240
|
|
|
241
|
+
/**
|
|
242
|
+
* Convert an array of Events back to HistoryItems
|
|
243
|
+
* @param events - Array of Events to convert
|
|
244
|
+
* @returns Array of HistoryItems with simplified structure
|
|
245
|
+
* @throws Error if any event is malformed
|
|
246
|
+
*/
|
|
247
|
+
export function eventsToHistory(events: Event[]): History {
|
|
248
|
+
if (!Array.isArray(events)) {
|
|
249
|
+
throw new Error('Invalid events: must be an array');
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return events.map((event, index) => {
|
|
253
|
+
try {
|
|
254
|
+
return eventToHistoryItem(event);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
throw new Error(`Error converting event at index ${index}: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
99
261
|
/**
|
|
100
262
|
* Normalize a simplified history array to internal Event array
|
|
263
|
+
* @deprecated Use historyToEvents instead
|
|
101
264
|
*/
|
|
102
265
|
export function normalizeHistory(history: History): Event[] {
|
|
103
|
-
return history
|
|
266
|
+
return historyToEvents(history);
|
|
104
267
|
}
|
|
105
268
|
|
|
106
269
|
/**
|
|
@@ -140,4 +303,4 @@ export function toolMessage(
|
|
|
140
303
|
*/
|
|
141
304
|
export function systemMessage(content: string): HistoryItem {
|
|
142
305
|
return { role: "system", content };
|
|
143
|
-
}
|
|
306
|
+
}
|
package/src/utils/index.ts
CHANGED