@falai/agent 0.6.8 β 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 +62 -59
- package/dist/adapters/MemoryAdapter.js +2 -2
- package/dist/adapters/MemoryAdapter.js.map +1 -1
- package/dist/adapters/MongoAdapter.js +2 -2
- package/dist/adapters/MongoAdapter.js.map +1 -1
- package/dist/adapters/OpenSearchAdapter.js +7 -7
- package/dist/adapters/OpenSearchAdapter.js.map +1 -1
- package/dist/adapters/PostgreSQLAdapter.js +9 -9
- package/dist/adapters/PostgreSQLAdapter.js.map +1 -1
- package/dist/adapters/PrismaAdapter.js +3 -3
- package/dist/adapters/PrismaAdapter.js.map +1 -1
- package/dist/adapters/RedisAdapter.js +2 -2
- package/dist/adapters/RedisAdapter.js.map +1 -1
- package/dist/adapters/SQLiteAdapter.d.ts +3 -3
- package/dist/adapters/SQLiteAdapter.d.ts.map +1 -1
- package/dist/adapters/SQLiteAdapter.js +11 -11
- package/dist/adapters/SQLiteAdapter.js.map +1 -1
- package/dist/adapters/index.d.ts +1 -1
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/cjs/adapters/MemoryAdapter.js +2 -2
- package/dist/cjs/adapters/MemoryAdapter.js.map +1 -1
- package/dist/cjs/adapters/MongoAdapter.js +2 -2
- package/dist/cjs/adapters/MongoAdapter.js.map +1 -1
- package/dist/cjs/adapters/OpenSearchAdapter.js +7 -7
- package/dist/cjs/adapters/OpenSearchAdapter.js.map +1 -1
- package/dist/cjs/adapters/PostgreSQLAdapter.js +9 -9
- package/dist/cjs/adapters/PostgreSQLAdapter.js.map +1 -1
- package/dist/cjs/adapters/PrismaAdapter.js +3 -3
- package/dist/cjs/adapters/PrismaAdapter.js.map +1 -1
- package/dist/cjs/adapters/RedisAdapter.js +2 -2
- package/dist/cjs/adapters/RedisAdapter.js.map +1 -1
- package/dist/cjs/adapters/SQLiteAdapter.d.ts +3 -3
- package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +1 -1
- package/dist/cjs/adapters/SQLiteAdapter.js +11 -11
- package/dist/cjs/adapters/SQLiteAdapter.js.map +1 -1
- package/dist/cjs/adapters/index.d.ts +1 -1
- package/dist/cjs/adapters/index.d.ts.map +1 -1
- package/dist/cjs/constants/index.d.ts +4 -4
- package/dist/cjs/constants/index.js +5 -5
- package/dist/cjs/core/Agent.d.ts +22 -22
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +160 -152
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/Events.d.ts +6 -6
- package/dist/cjs/core/Events.d.ts.map +1 -1
- package/dist/cjs/core/PersistenceManager.d.ts +13 -13
- package/dist/cjs/core/PersistenceManager.d.ts.map +1 -1
- package/dist/cjs/core/PersistenceManager.js +24 -24
- package/dist/cjs/core/PersistenceManager.js.map +1 -1
- package/dist/cjs/core/ResponseEngine.d.ts +3 -8
- package/dist/cjs/core/ResponseEngine.d.ts.map +1 -1
- package/dist/cjs/core/ResponseEngine.js +8 -8
- package/dist/cjs/core/ResponseEngine.js.map +1 -1
- package/dist/cjs/core/Route.d.ts +17 -17
- package/dist/cjs/core/Route.d.ts.map +1 -1
- package/dist/cjs/core/Route.js +33 -33
- package/dist/cjs/core/Route.js.map +1 -1
- package/dist/cjs/core/RoutingEngine.d.ts +30 -30
- package/dist/cjs/core/RoutingEngine.d.ts.map +1 -1
- package/dist/cjs/core/RoutingEngine.js +192 -192
- package/dist/cjs/core/RoutingEngine.js.map +1 -1
- package/dist/cjs/core/Step.d.ts +72 -0
- package/dist/cjs/core/Step.d.ts.map +1 -0
- package/dist/cjs/core/Step.js +150 -0
- package/dist/cjs/core/Step.js.map +1 -0
- package/dist/cjs/core/ToolExecutor.d.ts +5 -5
- package/dist/cjs/core/ToolExecutor.d.ts.map +1 -1
- package/dist/cjs/core/ToolExecutor.js +8 -8
- package/dist/cjs/core/ToolExecutor.js.map +1 -1
- package/dist/cjs/core/Transition.d.ts +14 -14
- package/dist/cjs/core/Transition.d.ts.map +1 -1
- package/dist/cjs/core/Transition.js +48 -19
- package/dist/cjs/core/Transition.js.map +1 -1
- package/dist/cjs/index.d.ts +7 -7
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +8 -8
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +8 -8
- package/dist/cjs/types/agent.d.ts.map +1 -1
- package/dist/cjs/types/ai.d.ts +2 -2
- package/dist/cjs/types/ai.d.ts.map +1 -1
- package/dist/cjs/types/history.d.ts +3 -3
- package/dist/cjs/types/history.d.ts.map +1 -1
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/persistence.d.ts +5 -5
- package/dist/cjs/types/persistence.d.ts.map +1 -1
- package/dist/cjs/types/route.d.ts +57 -52
- package/dist/cjs/types/route.d.ts.map +1 -1
- package/dist/cjs/types/session.d.ts +27 -27
- package/dist/cjs/types/session.d.ts.map +1 -1
- package/dist/cjs/types/session.js +48 -50
- package/dist/cjs/types/session.js.map +1 -1
- package/dist/cjs/types/tool.d.ts +13 -13
- package/dist/cjs/types/tool.d.ts.map +1 -1
- package/dist/cjs/utils/id.d.ts +8 -3
- package/dist/cjs/utils/id.d.ts.map +1 -1
- package/dist/cjs/utils/id.js +16 -7
- package/dist/cjs/utils/id.js.map +1 -1
- package/dist/constants/index.d.ts +4 -4
- package/dist/constants/index.js +4 -4
- package/dist/core/Agent.d.ts +22 -22
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +162 -154
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/Events.d.ts +6 -6
- package/dist/core/Events.d.ts.map +1 -1
- package/dist/core/PersistenceManager.d.ts +13 -13
- package/dist/core/PersistenceManager.d.ts.map +1 -1
- package/dist/core/PersistenceManager.js +25 -25
- package/dist/core/PersistenceManager.js.map +1 -1
- package/dist/core/ResponseEngine.d.ts +3 -8
- package/dist/core/ResponseEngine.d.ts.map +1 -1
- package/dist/core/ResponseEngine.js +8 -8
- package/dist/core/ResponseEngine.js.map +1 -1
- package/dist/core/Route.d.ts +17 -17
- package/dist/core/Route.d.ts.map +1 -1
- package/dist/core/Route.js +33 -33
- package/dist/core/Route.js.map +1 -1
- package/dist/core/RoutingEngine.d.ts +30 -30
- package/dist/core/RoutingEngine.d.ts.map +1 -1
- package/dist/core/RoutingEngine.js +193 -193
- package/dist/core/RoutingEngine.js.map +1 -1
- package/dist/core/Step.d.ts +72 -0
- package/dist/core/Step.d.ts.map +1 -0
- package/dist/core/Step.js +146 -0
- package/dist/core/Step.js.map +1 -0
- package/dist/core/ToolExecutor.d.ts +5 -5
- package/dist/core/ToolExecutor.d.ts.map +1 -1
- package/dist/core/ToolExecutor.js +8 -8
- package/dist/core/ToolExecutor.js.map +1 -1
- package/dist/core/Transition.d.ts +14 -14
- package/dist/core/Transition.d.ts.map +1 -1
- package/dist/core/Transition.js +48 -19
- package/dist/core/Transition.js.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/types/agent.d.ts +8 -8
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/ai.d.ts +2 -2
- package/dist/types/ai.d.ts.map +1 -1
- package/dist/types/history.d.ts +3 -3
- package/dist/types/history.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/persistence.d.ts +5 -5
- package/dist/types/persistence.d.ts.map +1 -1
- package/dist/types/route.d.ts +57 -52
- package/dist/types/route.d.ts.map +1 -1
- package/dist/types/session.d.ts +27 -27
- package/dist/types/session.d.ts.map +1 -1
- package/dist/types/session.js +44 -46
- package/dist/types/session.js.map +1 -1
- package/dist/types/tool.d.ts +13 -13
- package/dist/types/tool.d.ts.map +1 -1
- package/dist/utils/id.d.ts +8 -3
- package/dist/utils/id.d.ts.map +1 -1
- package/dist/utils/id.js +14 -6
- package/dist/utils/id.js.map +1 -1
- package/docs/ADAPTERS.md +21 -21
- package/docs/AGENT.md +57 -55
- package/docs/API_REFERENCE.md +218 -220
- package/docs/ARCHITECTURE.md +99 -104
- package/docs/CONTEXT_MANAGEMENT.md +81 -88
- package/docs/DOCS.md +18 -18
- package/docs/DOMAINS.md +16 -16
- package/docs/EXAMPLES.md +43 -43
- package/docs/GETTING_STARTED.md +60 -63
- package/docs/PERSISTENCE.md +66 -70
- package/docs/PROVIDERS.md +2 -2
- package/docs/README.md +6 -6
- package/docs/ROUTES.md +218 -220
- package/docs/STEPS.md +883 -0
- package/examples/business-onboarding.ts +84 -81
- package/examples/company-qna-agent.ts +68 -67
- package/examples/custom-database-persistence.ts +87 -89
- package/examples/declarative-agent.ts +32 -32
- package/examples/domain-scoping.ts +18 -18
- package/examples/extracted-data-modification.ts +92 -97
- package/examples/healthcare-agent.ts +89 -91
- package/examples/openai-agent.ts +29 -32
- package/examples/opensearch-persistence.ts +43 -45
- package/examples/persistent-onboarding.ts +65 -66
- package/examples/prisma-persistence.ts +108 -112
- package/examples/prisma-schema.example.prisma +3 -3
- package/examples/redis-persistence.ts +67 -73
- package/examples/route-transitions.ts +71 -47
- package/examples/rules-prohibitions.ts +28 -28
- package/examples/streaming-agent.ts +24 -24
- package/examples/travel-agent.ts +94 -109
- package/package.json +1 -1
- package/src/adapters/MemoryAdapter.ts +3 -3
- package/src/adapters/MongoAdapter.ts +3 -3
- package/src/adapters/OpenSearchAdapter.ts +8 -8
- package/src/adapters/PostgreSQLAdapter.ts +10 -10
- package/src/adapters/PrismaAdapter.ts +4 -4
- package/src/adapters/RedisAdapter.ts +3 -3
- package/src/adapters/SQLiteAdapter.ts +15 -15
- package/src/adapters/index.ts +1 -1
- package/src/constants/index.ts +4 -4
- package/src/core/Agent.ts +210 -206
- package/src/core/Events.ts +12 -12
- package/src/core/PersistenceManager.ts +32 -36
- package/src/core/ResponseEngine.ts +11 -17
- package/src/core/Route.ts +55 -49
- package/src/core/RoutingEngine.ts +244 -252
- package/src/core/Step.ts +197 -0
- package/src/core/ToolExecutor.ts +11 -11
- package/src/core/Transition.ts +72 -26
- package/src/index.ts +8 -8
- package/src/types/agent.ts +8 -8
- package/src/types/ai.ts +2 -2
- package/src/types/history.ts +3 -3
- package/src/types/index.ts +1 -1
- package/src/types/persistence.ts +6 -6
- package/src/types/route.ts +77 -61
- package/src/types/session.ts +75 -78
- package/src/types/tool.ts +17 -17
- package/src/utils/id.ts +15 -6
- package/dist/cjs/core/State.d.ts +0 -72
- package/dist/cjs/core/State.d.ts.map +0 -1
- package/dist/cjs/core/State.js +0 -148
- package/dist/cjs/core/State.js.map +0 -1
- package/dist/core/State.d.ts +0 -72
- package/dist/core/State.d.ts.map +0 -1
- package/dist/core/State.js +0 -144
- package/dist/core/State.js.map +0 -1
- package/docs/STATES.md +0 -888
- package/src/core/State.ts +0 -212
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Healthcare agent example demonstrating route-based
|
|
3
|
-
* Updated for v2 architecture with session
|
|
3
|
+
* Updated for v2 architecture with session step management and schema-first data extraction
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
7
|
Agent,
|
|
8
8
|
defineTool,
|
|
9
9
|
AnthropicProvider,
|
|
10
|
-
|
|
10
|
+
END_ROUTE,
|
|
11
11
|
EventSource,
|
|
12
12
|
createMessageEvent,
|
|
13
13
|
createSession,
|
|
@@ -69,8 +69,8 @@ const scheduleAppointment = defineTool<
|
|
|
69
69
|
HealthcareContext,
|
|
70
70
|
[datetime: string],
|
|
71
71
|
string
|
|
72
|
-
>("schedule_appointment", async ({ context,
|
|
73
|
-
const appointment =
|
|
72
|
+
>("schedule_appointment", async ({ context, data }, datetime) => {
|
|
73
|
+
const appointment = data as Partial<AppointmentData>;
|
|
74
74
|
if (!appointment?.preferredDate || !appointment?.preferredTime) {
|
|
75
75
|
return {
|
|
76
76
|
data: "Please specify preferred date and time for the appointment",
|
|
@@ -85,8 +85,8 @@ const scheduleAppointment = defineTool<
|
|
|
85
85
|
|
|
86
86
|
const getLabResults = defineTool<HealthcareContext, [], object>(
|
|
87
87
|
"get_lab_results",
|
|
88
|
-
async ({ context,
|
|
89
|
-
const labData =
|
|
88
|
+
async ({ context, data }) => {
|
|
89
|
+
const labData = data as Partial<LabResultsData>;
|
|
90
90
|
return {
|
|
91
91
|
data: {
|
|
92
92
|
report: `Lab results for ${labData?.testType || "general"} tests`,
|
|
@@ -137,7 +137,7 @@ async function createHealthcareAgent() {
|
|
|
137
137
|
title: "Schedule an Appointment",
|
|
138
138
|
description: "Helps the patient find a time for their appointment.",
|
|
139
139
|
conditions: ["The patient wants to schedule an appointment"],
|
|
140
|
-
|
|
140
|
+
schema: {
|
|
141
141
|
type: "object",
|
|
142
142
|
properties: {
|
|
143
143
|
appointmentReason: {
|
|
@@ -169,79 +169,78 @@ async function createHealthcareAgent() {
|
|
|
169
169
|
onComplete: "Satisfaction Survey",
|
|
170
170
|
});
|
|
171
171
|
|
|
172
|
-
//
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
skipIf: (
|
|
172
|
+
// Step 1: Collect appointment reason
|
|
173
|
+
const collectReason = schedulingRoute.initialStep.nextStep({
|
|
174
|
+
instructions: "Ask what the patient needs an appointment for",
|
|
175
|
+
collect: ["appointmentReason"],
|
|
176
|
+
skipIf: (data) => !!data.appointmentReason,
|
|
177
177
|
condition: "Patient hasn't specified reason for appointment yet",
|
|
178
178
|
});
|
|
179
179
|
|
|
180
|
-
//
|
|
181
|
-
const checkUrgency =
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
skipIf: (
|
|
185
|
-
|
|
180
|
+
// Step 2: Check urgency and show available slots
|
|
181
|
+
const checkUrgency = collectReason.nextStep({
|
|
182
|
+
instructions: "Check if this is urgent and show available slots",
|
|
183
|
+
collect: ["urgency"],
|
|
184
|
+
skipIf: (data) => !!data.urgency,
|
|
185
|
+
requires: ["appointmentReason"],
|
|
186
186
|
condition: "Reason provided, now assess urgency level",
|
|
187
187
|
});
|
|
188
188
|
|
|
189
|
-
const showSlots = checkUrgency.
|
|
190
|
-
|
|
189
|
+
const showSlots = checkUrgency.nextStep({
|
|
190
|
+
tool: getUpcomingSlots,
|
|
191
191
|
});
|
|
192
192
|
|
|
193
|
-
//
|
|
194
|
-
const presentTimes = showSlots.
|
|
195
|
-
|
|
193
|
+
// Step 3: Present available times
|
|
194
|
+
const presentTimes = showSlots.nextStep({
|
|
195
|
+
instructions: "List available times and ask which one works for them",
|
|
196
196
|
});
|
|
197
197
|
|
|
198
|
-
//
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
skipIf: (
|
|
203
|
-
!!extracted.preferredTime && !!extracted.preferredDate,
|
|
198
|
+
// Step 4: Collect preferred time and date
|
|
199
|
+
const collectPreferences = presentTimes.nextStep({
|
|
200
|
+
instructions: "Collect preferred time and date",
|
|
201
|
+
collect: ["preferredTime", "preferredDate"],
|
|
202
|
+
skipIf: (data) => !!data.preferredTime && !!data.preferredDate,
|
|
204
203
|
});
|
|
205
204
|
|
|
206
|
-
//
|
|
207
|
-
const confirmDetails =
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
skipIf: (
|
|
211
|
-
|
|
205
|
+
// Step 5: Confirm details and schedule
|
|
206
|
+
const confirmDetails = collectPreferences.nextStep({
|
|
207
|
+
instructions: "Confirm the details with the patient before scheduling",
|
|
208
|
+
collect: ["appointmentType"],
|
|
209
|
+
skipIf: (data) => !!data.appointmentType,
|
|
210
|
+
requires: ["appointmentReason", "preferredTime", "preferredDate"],
|
|
212
211
|
});
|
|
213
212
|
|
|
214
|
-
const schedule = confirmDetails.
|
|
215
|
-
|
|
216
|
-
|
|
213
|
+
const schedule = confirmDetails.nextStep({
|
|
214
|
+
tool: scheduleAppointment,
|
|
215
|
+
requires: ["appointmentReason", "preferredTime", "preferredDate"],
|
|
217
216
|
condition: "All details confirmed, book the appointment",
|
|
218
217
|
});
|
|
219
218
|
|
|
220
|
-
const confirmation = schedule.
|
|
221
|
-
|
|
219
|
+
const confirmation = schedule.nextStep({
|
|
220
|
+
instructions: "Confirm the appointment has been scheduled",
|
|
222
221
|
});
|
|
223
222
|
|
|
224
|
-
confirmation.
|
|
225
|
-
|
|
223
|
+
confirmation.nextStep({
|
|
224
|
+
step: END_ROUTE,
|
|
226
225
|
condition: "Appointment booked successfully",
|
|
227
226
|
});
|
|
228
227
|
|
|
229
228
|
// Alternative path: no times work - show later slots
|
|
230
|
-
const laterSlots = presentTimes.
|
|
231
|
-
|
|
229
|
+
const laterSlots = presentTimes.nextStep({
|
|
230
|
+
tool: getLaterSlots,
|
|
232
231
|
});
|
|
233
232
|
|
|
234
|
-
laterSlots.
|
|
235
|
-
|
|
233
|
+
laterSlots.nextStep({
|
|
234
|
+
instructions: "List later times and ask if any of them works",
|
|
236
235
|
});
|
|
237
236
|
|
|
238
237
|
// If no times work at all, end route
|
|
239
238
|
laterSlots
|
|
240
|
-
.
|
|
241
|
-
|
|
239
|
+
.nextStep({
|
|
240
|
+
instructions:
|
|
242
241
|
"Ask the patient to call the office to schedule an appointment",
|
|
243
242
|
})
|
|
244
|
-
.
|
|
243
|
+
.nextStep({ step: END_ROUTE });
|
|
245
244
|
|
|
246
245
|
schedulingRoute.createGuideline({
|
|
247
246
|
condition: "The patient says their visit is urgent",
|
|
@@ -253,7 +252,7 @@ async function createHealthcareAgent() {
|
|
|
253
252
|
title: "Lab Results",
|
|
254
253
|
description: "Retrieves the patient's lab results and explains them.",
|
|
255
254
|
conditions: ["The patient wants to see their lab results"],
|
|
256
|
-
|
|
255
|
+
schema: {
|
|
257
256
|
type: "object",
|
|
258
257
|
properties: {
|
|
259
258
|
testType: {
|
|
@@ -274,33 +273,33 @@ async function createHealthcareAgent() {
|
|
|
274
273
|
},
|
|
275
274
|
});
|
|
276
275
|
|
|
277
|
-
//
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
skipIf: (
|
|
276
|
+
// Step 1: Collect test information
|
|
277
|
+
const collectTestInfo = labResultsRoute.initialStep.nextStep({
|
|
278
|
+
instructions: "Ask what type of test results they want to see",
|
|
279
|
+
collect: ["testType"],
|
|
280
|
+
skipIf: (data) => !!data.testType,
|
|
282
281
|
});
|
|
283
282
|
|
|
284
|
-
//
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
skipIf: (
|
|
289
|
-
|
|
283
|
+
// Step 2: Optional: collect test date
|
|
284
|
+
const collectTestDate = collectTestInfo.nextStep({
|
|
285
|
+
instructions: "Ask for the test date if they remember it",
|
|
286
|
+
collect: ["testDate"],
|
|
287
|
+
skipIf: (data) => !!data.testDate,
|
|
288
|
+
requires: ["testType"],
|
|
290
289
|
});
|
|
291
290
|
|
|
292
|
-
//
|
|
293
|
-
const getResults =
|
|
294
|
-
|
|
295
|
-
|
|
291
|
+
// Step 3: Get lab results
|
|
292
|
+
const getResults = collectTestDate.nextStep({
|
|
293
|
+
tool: getLabResults,
|
|
294
|
+
requires: ["testType"],
|
|
296
295
|
});
|
|
297
296
|
|
|
298
|
-
//
|
|
299
|
-
const presentResults = getResults.
|
|
300
|
-
|
|
297
|
+
// Step 4: Present results based on status
|
|
298
|
+
const presentResults = getResults.nextStep({
|
|
299
|
+
instructions: "Present the lab results and explain what they mean",
|
|
301
300
|
});
|
|
302
301
|
|
|
303
|
-
presentResults.
|
|
302
|
+
presentResults.nextStep({ step: END_ROUTE });
|
|
304
303
|
|
|
305
304
|
labResultsRoute.createGuideline({
|
|
306
305
|
condition:
|
|
@@ -314,7 +313,7 @@ async function createHealthcareAgent() {
|
|
|
314
313
|
title: "Satisfaction Survey",
|
|
315
314
|
description: "Quick satisfaction survey after scheduling",
|
|
316
315
|
conditions: ["Collect patient satisfaction feedback"],
|
|
317
|
-
|
|
316
|
+
schema: {
|
|
318
317
|
type: "object",
|
|
319
318
|
properties: {
|
|
320
319
|
rating: {
|
|
@@ -334,24 +333,25 @@ async function createHealthcareAgent() {
|
|
|
334
333
|
},
|
|
335
334
|
});
|
|
336
335
|
|
|
337
|
-
const askRating = satisfactionRoute.
|
|
338
|
-
|
|
336
|
+
const askRating = satisfactionRoute.initialStep.nextStep({
|
|
337
|
+
instructions:
|
|
339
338
|
"Ask for overall satisfaction rating from 1 to 5 with the scheduling experience",
|
|
340
|
-
|
|
341
|
-
skipIf: (
|
|
339
|
+
collect: ["rating"],
|
|
340
|
+
skipIf: (data) => !!data.rating,
|
|
342
341
|
});
|
|
343
342
|
|
|
344
|
-
const askComments = askRating.
|
|
345
|
-
|
|
346
|
-
|
|
343
|
+
const askComments = askRating.nextStep({
|
|
344
|
+
instructions:
|
|
345
|
+
"Ask if they have any additional comments or feedback (optional)",
|
|
346
|
+
collect: ["comments"],
|
|
347
347
|
});
|
|
348
348
|
|
|
349
|
-
const thankYou = askComments.
|
|
350
|
-
|
|
349
|
+
const thankYou = askComments.nextStep({
|
|
350
|
+
instructions:
|
|
351
351
|
"Thank them for their feedback and confirm their appointment details one more time",
|
|
352
352
|
});
|
|
353
353
|
|
|
354
|
-
thankYou.
|
|
354
|
+
thankYou.nextStep({ step: END_ROUTE });
|
|
355
355
|
|
|
356
356
|
// Global guidelines
|
|
357
357
|
agent.createGuideline({
|
|
@@ -376,11 +376,11 @@ async function createHealthcareAgent() {
|
|
|
376
376
|
return agent;
|
|
377
377
|
}
|
|
378
378
|
|
|
379
|
-
// Example usage with session
|
|
379
|
+
// Example usage with session step management
|
|
380
380
|
async function main() {
|
|
381
381
|
const agent = await createHealthcareAgent();
|
|
382
382
|
|
|
383
|
-
// Initialize session
|
|
383
|
+
// Initialize session step for multi-turn conversation
|
|
384
384
|
let session = createSession<AppointmentData | LabResultsData>();
|
|
385
385
|
|
|
386
386
|
const history = [
|
|
@@ -403,7 +403,7 @@ async function main() {
|
|
|
403
403
|
console.log("\n" + route.describe());
|
|
404
404
|
}
|
|
405
405
|
|
|
406
|
-
// Example conversation with session
|
|
406
|
+
// Example conversation with session step
|
|
407
407
|
console.log("\n=== EXAMPLE CONVERSATION ===");
|
|
408
408
|
|
|
409
409
|
// Turn 1: Patient wants to follow up
|
|
@@ -411,7 +411,7 @@ async function main() {
|
|
|
411
411
|
console.log("Patient: Hi, I need to follow up on my visit");
|
|
412
412
|
console.log("Agent:", response1.message);
|
|
413
413
|
console.log("Route:", response1.session?.currentRoute?.title);
|
|
414
|
-
console.log("
|
|
414
|
+
console.log("Data:", response1.session?.data);
|
|
415
415
|
|
|
416
416
|
// Update session with progress
|
|
417
417
|
session = response1.session!;
|
|
@@ -431,8 +431,8 @@ async function main() {
|
|
|
431
431
|
const response2 = await agent.respond({ history: history2, session });
|
|
432
432
|
console.log("\nPatient: I need to schedule a checkup for next week");
|
|
433
433
|
console.log("Agent:", response2.message);
|
|
434
|
-
console.log("Updated
|
|
435
|
-
console.log("Current
|
|
434
|
+
console.log("Updated data:", response2.session?.data);
|
|
435
|
+
console.log("Current step:", response2.session?.currentStep?.id);
|
|
436
436
|
|
|
437
437
|
// Update session again
|
|
438
438
|
session = response2.session!;
|
|
@@ -461,16 +461,14 @@ async function main() {
|
|
|
461
461
|
const response3 = await agent.respond({ history: history3, session });
|
|
462
462
|
console.log("\nPatient: Tuesday at 2 PM works for me.");
|
|
463
463
|
console.log("Agent:", response3.message);
|
|
464
|
-
console.log("Updated
|
|
465
|
-
console.log("Current
|
|
464
|
+
console.log("Updated data:", response3.session?.data);
|
|
465
|
+
console.log("Current step:", response3.session?.currentStep?.id);
|
|
466
466
|
|
|
467
467
|
// Check for route completion
|
|
468
468
|
if (response3.isRouteComplete) {
|
|
469
469
|
console.log("\nβ
Appointment scheduling complete!");
|
|
470
470
|
await sendAppointmentReminder(
|
|
471
|
-
agent.
|
|
472
|
-
response3.session?.id
|
|
473
|
-
) as unknown as AppointmentData
|
|
471
|
+
agent.getData(response3.session?.id) as unknown as AppointmentData
|
|
474
472
|
);
|
|
475
473
|
}
|
|
476
474
|
}
|
package/examples/openai-agent.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Example: OpenAI Agent with multiple providers
|
|
3
|
-
* Updated for v2 architecture with session
|
|
3
|
+
* Updated for v2 architecture with session step management and schema-first data extraction
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import {
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
createMessageEvent,
|
|
11
11
|
EventSource,
|
|
12
12
|
createSession,
|
|
13
|
-
|
|
13
|
+
END_ROUTE,
|
|
14
14
|
} from "../src/index";
|
|
15
15
|
|
|
16
16
|
// Custom context type
|
|
@@ -27,17 +27,16 @@ interface WeatherData {
|
|
|
27
27
|
condition?: string;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
// Define a tool that can access
|
|
30
|
+
// Define a tool that can access collected data
|
|
31
31
|
const getWeather = defineTool<
|
|
32
32
|
CustomerContext,
|
|
33
33
|
[{ location: string }],
|
|
34
34
|
{ location: string; temperature: number; condition: string }
|
|
35
35
|
>(
|
|
36
36
|
"get_weather",
|
|
37
|
-
async ({ context,
|
|
38
|
-
// Use
|
|
39
|
-
const location =
|
|
40
|
-
(extracted as Partial<WeatherData>)?.location || args.location;
|
|
37
|
+
async ({ context, data }, args) => {
|
|
38
|
+
// Use data location if available, otherwise use args
|
|
39
|
+
const location = (data as Partial<WeatherData>)?.location || args.location;
|
|
41
40
|
|
|
42
41
|
// Simulate API call
|
|
43
42
|
return {
|
|
@@ -112,7 +111,7 @@ async function main() {
|
|
|
112
111
|
title: "Check Weather",
|
|
113
112
|
description: "Help user check weather for a location",
|
|
114
113
|
conditions: ["User wants to know the weather"],
|
|
115
|
-
|
|
114
|
+
schema: {
|
|
116
115
|
type: "object",
|
|
117
116
|
properties: {
|
|
118
117
|
location: {
|
|
@@ -132,31 +131,31 @@ async function main() {
|
|
|
132
131
|
},
|
|
133
132
|
});
|
|
134
133
|
|
|
135
|
-
//
|
|
136
|
-
const askLocation = weatherRoute.
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
skipIf: (
|
|
134
|
+
// Step 1: Collect location
|
|
135
|
+
const askLocation = weatherRoute.initialStep.nextStep({
|
|
136
|
+
instructions: "Ask which city they want weather for",
|
|
137
|
+
collect: ["location"],
|
|
138
|
+
skipIf: (data) => !!data.location,
|
|
140
139
|
});
|
|
141
140
|
|
|
142
|
-
//
|
|
143
|
-
const fetchWeather = askLocation.
|
|
144
|
-
|
|
145
|
-
|
|
141
|
+
// Step 2: Get weather data
|
|
142
|
+
const fetchWeather = askLocation.nextStep({
|
|
143
|
+
tool: getWeather,
|
|
144
|
+
requires: ["location"],
|
|
146
145
|
});
|
|
147
146
|
|
|
148
|
-
//
|
|
149
|
-
const showWeather = fetchWeather.
|
|
150
|
-
|
|
147
|
+
// Step 3: Present weather information
|
|
148
|
+
const showWeather = fetchWeather.nextStep({
|
|
149
|
+
instructions:
|
|
151
150
|
"Present the weather information in a friendly way with temperature and condition",
|
|
152
151
|
});
|
|
153
152
|
|
|
154
|
-
showWeather.
|
|
153
|
+
showWeather.nextStep({ step: END_ROUTE });
|
|
155
154
|
|
|
156
|
-
// Example conversation with session
|
|
155
|
+
// Example conversation with session step management
|
|
157
156
|
console.log("π€ Starting OpenAI Agent Example\n");
|
|
158
157
|
|
|
159
|
-
// Initialize session
|
|
158
|
+
// Initialize session step for multi-turn conversation
|
|
160
159
|
let session = createSession<WeatherData>();
|
|
161
160
|
|
|
162
161
|
// Build history
|
|
@@ -169,8 +168,8 @@ async function main() {
|
|
|
169
168
|
];
|
|
170
169
|
|
|
171
170
|
try {
|
|
172
|
-
// Turn 1: Process weather query with session
|
|
173
|
-
console.log("π€ Processing with session
|
|
171
|
+
// Turn 1: Process weather query with session step
|
|
172
|
+
console.log("π€ Processing with session step...");
|
|
174
173
|
const response = await agent.respond({ history, session });
|
|
175
174
|
|
|
176
175
|
console.log("\nβ
Agent Configuration:");
|
|
@@ -179,14 +178,14 @@ async function main() {
|
|
|
179
178
|
console.log("\nπΊοΈ Route Configuration:");
|
|
180
179
|
console.log(` Title: ${weatherRoute.title}`);
|
|
181
180
|
console.log(
|
|
182
|
-
`
|
|
181
|
+
` Steps: Initial β Ask Location β Fetch Weather β Show Weather`
|
|
183
182
|
);
|
|
184
183
|
|
|
185
184
|
console.log("\n㪠Conversation:");
|
|
186
185
|
console.log(` Customer: ${history[0].data.message}`);
|
|
187
186
|
console.log(` Agent: ${response.message}`);
|
|
188
187
|
console.log(` Route: ${response.session?.currentRoute?.title}`);
|
|
189
|
-
console.log(`
|
|
188
|
+
console.log(` Data:`, response.session?.data);
|
|
190
189
|
|
|
191
190
|
// Update session with progress
|
|
192
191
|
session = response.session!;
|
|
@@ -194,14 +193,12 @@ async function main() {
|
|
|
194
193
|
// Check for route completion
|
|
195
194
|
if (response.isRouteComplete) {
|
|
196
195
|
console.log("\nβ
Weather route complete!");
|
|
197
|
-
await logWeatherRequest(
|
|
198
|
-
agent.getExtractedData(session.id) as WeatherData
|
|
199
|
-
);
|
|
196
|
+
await logWeatherRequest(agent.getData(session.id) as WeatherData);
|
|
200
197
|
}
|
|
201
198
|
|
|
202
|
-
console.log("\n⨠Session
|
|
199
|
+
console.log("\n⨠Session step benefits:");
|
|
203
200
|
console.log(" β
Data extraction tracked across turns");
|
|
204
|
-
console.log(" β
|
|
201
|
+
console.log(" β
Step progression managed automatically");
|
|
205
202
|
console.log(" β
Always-on routing respects intent changes");
|
|
206
203
|
console.log(
|
|
207
204
|
" (Set OPENAI_API_KEY environment variable to make actual API calls)"
|