@falai/agent 0.4.0 ā 0.5.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 +21 -74
- package/dist/cjs/core/Agent.d.ts +22 -29
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +465 -275
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/Events.d.ts +10 -1
- package/dist/cjs/core/Events.d.ts.map +1 -1
- package/dist/cjs/core/Events.js +3 -2
- package/dist/cjs/core/Events.js.map +1 -1
- package/dist/cjs/core/PersistenceManager.d.ts +19 -0
- package/dist/cjs/core/PersistenceManager.d.ts.map +1 -1
- package/dist/cjs/core/PersistenceManager.js +57 -0
- package/dist/cjs/core/PersistenceManager.js.map +1 -1
- package/dist/cjs/core/PromptComposer.d.ts +24 -0
- package/dist/cjs/core/PromptComposer.d.ts.map +1 -0
- package/dist/cjs/core/PromptComposer.js +127 -0
- package/dist/cjs/core/PromptComposer.js.map +1 -0
- package/dist/cjs/core/ResponseEngine.d.ts +19 -0
- package/dist/cjs/core/ResponseEngine.d.ts.map +1 -0
- package/dist/cjs/core/ResponseEngine.js +51 -0
- package/dist/cjs/core/ResponseEngine.js.map +1 -0
- package/dist/cjs/core/Route.d.ts +18 -12
- package/dist/cjs/core/Route.d.ts.map +1 -1
- package/dist/cjs/core/Route.js +15 -9
- package/dist/cjs/core/Route.js.map +1 -1
- package/dist/cjs/core/RoutingEngine.d.ts +38 -0
- package/dist/cjs/core/RoutingEngine.d.ts.map +1 -0
- package/dist/cjs/core/RoutingEngine.js +110 -0
- package/dist/cjs/core/RoutingEngine.js.map +1 -0
- package/dist/cjs/core/State.d.ts +15 -4
- package/dist/cjs/core/State.d.ts.map +1 -1
- package/dist/cjs/core/State.js +21 -2
- package/dist/cjs/core/State.js.map +1 -1
- package/dist/cjs/core/ToolExecutor.d.ts +29 -0
- package/dist/cjs/core/ToolExecutor.d.ts.map +1 -0
- package/dist/cjs/core/ToolExecutor.js +73 -0
- package/dist/cjs/core/ToolExecutor.js.map +1 -0
- package/dist/cjs/core/Transition.d.ts +5 -5
- package/dist/cjs/core/Transition.d.ts.map +1 -1
- package/dist/cjs/core/Transition.js.map +1 -1
- package/dist/cjs/index.d.ts +6 -8
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +8 -10
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/providers/AnthropicProvider.d.ts.map +1 -1
- package/dist/cjs/providers/AnthropicProvider.js +10 -13
- package/dist/cjs/providers/AnthropicProvider.js.map +1 -1
- package/dist/cjs/providers/GeminiProvider.d.ts.map +1 -1
- package/dist/cjs/providers/GeminiProvider.js +12 -8
- package/dist/cjs/providers/GeminiProvider.js.map +1 -1
- package/dist/cjs/providers/OpenAIProvider.d.ts.map +1 -1
- package/dist/cjs/providers/OpenAIProvider.js +10 -53
- package/dist/cjs/providers/OpenAIProvider.js.map +1 -1
- package/dist/cjs/providers/OpenRouterProvider.d.ts.map +1 -1
- package/dist/cjs/providers/OpenRouterProvider.js +10 -53
- package/dist/cjs/providers/OpenRouterProvider.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +13 -9
- package/dist/cjs/types/agent.d.ts.map +1 -1
- package/dist/cjs/types/ai.d.ts +8 -2
- package/dist/cjs/types/ai.d.ts.map +1 -1
- package/dist/cjs/types/history.d.ts +8 -0
- package/dist/cjs/types/history.d.ts.map +1 -1
- package/dist/cjs/types/index.d.ts +0 -3
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js +1 -3
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/route.d.ts +39 -4
- package/dist/cjs/types/route.d.ts.map +1 -1
- package/dist/cjs/types/routing.d.ts +16 -0
- package/dist/cjs/types/routing.d.ts.map +1 -0
- package/dist/cjs/types/routing.js +3 -0
- package/dist/cjs/types/routing.js.map +1 -0
- package/dist/cjs/types/schema.d.ts +22 -0
- package/dist/cjs/types/schema.d.ts.map +1 -0
- package/dist/cjs/types/schema.js +3 -0
- package/dist/cjs/types/schema.js.map +1 -0
- package/dist/cjs/types/session.d.ts +72 -0
- package/dist/cjs/types/session.d.ts.map +1 -0
- package/dist/cjs/types/session.js +140 -0
- package/dist/cjs/types/session.js.map +1 -0
- package/dist/cjs/types/tool.d.ts +11 -5
- package/dist/cjs/types/tool.d.ts.map +1 -1
- package/dist/cjs/utils/id.d.ts +0 -5
- package/dist/cjs/utils/id.d.ts.map +1 -1
- package/dist/cjs/utils/id.js +0 -10
- package/dist/cjs/utils/id.js.map +1 -1
- package/dist/cjs/utils/schema.d.ts +17 -0
- package/dist/cjs/utils/schema.d.ts.map +1 -0
- package/dist/cjs/utils/schema.js +32 -0
- package/dist/cjs/utils/schema.js.map +1 -0
- package/dist/core/Agent.d.ts +22 -29
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +465 -275
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/Events.d.ts +10 -1
- package/dist/core/Events.d.ts.map +1 -1
- package/dist/core/Events.js +3 -2
- package/dist/core/Events.js.map +1 -1
- package/dist/core/PersistenceManager.d.ts +19 -0
- package/dist/core/PersistenceManager.d.ts.map +1 -1
- package/dist/core/PersistenceManager.js +57 -0
- package/dist/core/PersistenceManager.js.map +1 -1
- package/dist/core/PromptComposer.d.ts +24 -0
- package/dist/core/PromptComposer.d.ts.map +1 -0
- package/dist/core/PromptComposer.js +123 -0
- package/dist/core/PromptComposer.js.map +1 -0
- package/dist/core/ResponseEngine.d.ts +19 -0
- package/dist/core/ResponseEngine.d.ts.map +1 -0
- package/dist/core/ResponseEngine.js +47 -0
- package/dist/core/ResponseEngine.js.map +1 -0
- package/dist/core/Route.d.ts +18 -12
- package/dist/core/Route.d.ts.map +1 -1
- package/dist/core/Route.js +15 -9
- package/dist/core/Route.js.map +1 -1
- package/dist/core/RoutingEngine.d.ts +38 -0
- package/dist/core/RoutingEngine.d.ts.map +1 -0
- package/dist/core/RoutingEngine.js +106 -0
- package/dist/core/RoutingEngine.js.map +1 -0
- package/dist/core/State.d.ts +15 -4
- package/dist/core/State.d.ts.map +1 -1
- package/dist/core/State.js +21 -2
- package/dist/core/State.js.map +1 -1
- package/dist/core/ToolExecutor.d.ts +29 -0
- package/dist/core/ToolExecutor.d.ts.map +1 -0
- package/dist/core/ToolExecutor.js +69 -0
- package/dist/core/ToolExecutor.js.map +1 -0
- package/dist/core/Transition.d.ts +5 -5
- package/dist/core/Transition.d.ts.map +1 -1
- package/dist/core/Transition.js.map +1 -1
- package/dist/index.d.ts +6 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -5
- package/dist/index.js.map +1 -1
- package/dist/providers/AnthropicProvider.d.ts.map +1 -1
- package/dist/providers/AnthropicProvider.js +10 -13
- package/dist/providers/AnthropicProvider.js.map +1 -1
- package/dist/providers/GeminiProvider.d.ts.map +1 -1
- package/dist/providers/GeminiProvider.js +12 -8
- package/dist/providers/GeminiProvider.js.map +1 -1
- package/dist/providers/OpenAIProvider.d.ts.map +1 -1
- package/dist/providers/OpenAIProvider.js +10 -53
- package/dist/providers/OpenAIProvider.js.map +1 -1
- package/dist/providers/OpenRouterProvider.d.ts.map +1 -1
- package/dist/providers/OpenRouterProvider.js +10 -53
- package/dist/providers/OpenRouterProvider.js.map +1 -1
- package/dist/types/agent.d.ts +13 -9
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/ai.d.ts +8 -2
- package/dist/types/ai.d.ts.map +1 -1
- package/dist/types/history.d.ts +8 -0
- package/dist/types/history.d.ts.map +1 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +0 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/route.d.ts +39 -4
- package/dist/types/route.d.ts.map +1 -1
- package/dist/types/routing.d.ts +16 -0
- package/dist/types/routing.d.ts.map +1 -0
- package/dist/types/routing.js +2 -0
- package/dist/types/routing.js.map +1 -0
- package/dist/types/schema.d.ts +22 -0
- package/dist/types/schema.d.ts.map +1 -0
- package/dist/types/schema.js +2 -0
- package/dist/types/schema.js.map +1 -0
- package/dist/types/session.d.ts +72 -0
- package/dist/types/session.d.ts.map +1 -0
- package/dist/types/session.js +132 -0
- package/dist/types/session.js.map +1 -0
- package/dist/types/tool.d.ts +11 -5
- package/dist/types/tool.d.ts.map +1 -1
- package/dist/utils/id.d.ts +0 -5
- package/dist/utils/id.d.ts.map +1 -1
- package/dist/utils/id.js +0 -9
- package/dist/utils/id.js.map +1 -1
- package/dist/utils/schema.d.ts +17 -0
- package/dist/utils/schema.d.ts.map +1 -0
- package/dist/utils/schema.js +27 -0
- package/dist/utils/schema.js.map +1 -0
- package/docs/ADAPTERS.md +83 -3
- package/docs/API_REFERENCE.md +95 -104
- package/docs/ARCHITECTURE.md +284 -286
- package/docs/CONSTRUCTOR_OPTIONS.md +192 -135
- package/docs/CONTEXT_MANAGEMENT.md +311 -28
- package/docs/CONTRIBUTING.md +1 -1
- package/docs/DOMAINS.md +61 -0
- package/docs/GETTING_STARTED.md +177 -88
- package/docs/PERSISTENCE.md +170 -23
- package/docs/README.md +7 -10
- package/examples/business-onboarding.ts +21 -9
- package/examples/company-qna-agent.ts +508 -0
- package/examples/declarative-agent.ts +143 -26
- package/examples/domain-scoping.ts +31 -10
- package/examples/extracted-data-modification.ts +415 -0
- package/examples/healthcare-agent.ts +194 -90
- package/examples/openai-agent.ts +67 -25
- package/examples/opensearch-persistence.ts +455 -151
- package/examples/persistent-onboarding.ts +162 -96
- package/examples/prisma-persistence.ts +371 -125
- package/examples/redis-persistence.ts +393 -23
- package/examples/rules-prohibitions.ts +32 -11
- package/examples/streaming-agent.ts +61 -13
- package/examples/travel-agent.ts +266 -133
- package/package.json +1 -1
- package/src/core/Agent.ts +679 -332
- package/src/core/Events.ts +12 -2
- package/src/core/PersistenceManager.ts +83 -0
- package/src/core/PromptComposer.ts +143 -0
- package/src/core/ResponseEngine.ts +82 -0
- package/src/core/Route.ts +32 -17
- package/src/core/RoutingEngine.ts +165 -0
- package/src/core/State.ts +55 -15
- package/src/core/ToolExecutor.ts +117 -0
- package/src/core/Transition.ts +5 -5
- package/src/index.ts +12 -21
- package/src/providers/AnthropicProvider.ts +10 -13
- package/src/providers/GeminiProvider.ts +12 -8
- package/src/providers/OpenAIProvider.ts +10 -56
- package/src/providers/OpenRouterProvider.ts +10 -56
- package/src/types/agent.ts +16 -10
- package/src/types/ai.ts +6 -2
- package/src/types/history.ts +8 -0
- package/src/types/index.ts +0 -11
- package/src/types/route.ts +41 -5
- package/src/types/routing.ts +18 -0
- package/src/types/schema.ts +23 -0
- package/src/types/session.ts +207 -0
- package/src/types/tool.ts +29 -7
- package/src/utils/id.ts +0 -10
- package/src/utils/schema.ts +32 -0
- package/dist/cjs/core/ConditionEvaluator.d.ts +0 -72
- package/dist/cjs/core/ConditionEvaluator.d.ts.map +0 -1
- package/dist/cjs/core/ConditionEvaluator.js +0 -272
- package/dist/cjs/core/ConditionEvaluator.js.map +0 -1
- package/dist/cjs/core/Observation.d.ts +0 -24
- package/dist/cjs/core/Observation.d.ts.map +0 -1
- package/dist/cjs/core/Observation.js +0 -39
- package/dist/cjs/core/Observation.js.map +0 -1
- package/dist/cjs/core/PreparationEngine.d.ts +0 -105
- package/dist/cjs/core/PreparationEngine.d.ts.map +0 -1
- package/dist/cjs/core/PreparationEngine.js +0 -320
- package/dist/cjs/core/PreparationEngine.js.map +0 -1
- package/dist/cjs/core/PromptBuilder.d.ts +0 -136
- package/dist/cjs/core/PromptBuilder.d.ts.map +0 -1
- package/dist/cjs/core/PromptBuilder.js +0 -421
- package/dist/cjs/core/PromptBuilder.js.map +0 -1
- package/dist/cjs/types/observation.d.ts +0 -27
- package/dist/cjs/types/observation.d.ts.map +0 -1
- package/dist/cjs/types/observation.js +0 -6
- package/dist/cjs/types/observation.js.map +0 -1
- package/dist/cjs/types/prompt.d.ts +0 -46
- package/dist/cjs/types/prompt.d.ts.map +0 -1
- package/dist/cjs/types/prompt.js +0 -19
- package/dist/cjs/types/prompt.js.map +0 -1
- package/dist/core/ConditionEvaluator.d.ts +0 -72
- package/dist/core/ConditionEvaluator.d.ts.map +0 -1
- package/dist/core/ConditionEvaluator.js +0 -268
- package/dist/core/ConditionEvaluator.js.map +0 -1
- package/dist/core/Observation.d.ts +0 -24
- package/dist/core/Observation.d.ts.map +0 -1
- package/dist/core/Observation.js +0 -35
- package/dist/core/Observation.js.map +0 -1
- package/dist/core/PreparationEngine.d.ts +0 -105
- package/dist/core/PreparationEngine.d.ts.map +0 -1
- package/dist/core/PreparationEngine.js +0 -316
- package/dist/core/PreparationEngine.js.map +0 -1
- package/dist/core/PromptBuilder.d.ts +0 -136
- package/dist/core/PromptBuilder.d.ts.map +0 -1
- package/dist/core/PromptBuilder.js +0 -417
- package/dist/core/PromptBuilder.js.map +0 -1
- package/dist/types/observation.d.ts +0 -27
- package/dist/types/observation.d.ts.map +0 -1
- package/dist/types/observation.js +0 -5
- package/dist/types/observation.js.map +0 -1
- package/dist/types/prompt.d.ts +0 -46
- package/dist/types/prompt.d.ts.map +0 -1
- package/dist/types/prompt.js +0 -16
- package/dist/types/prompt.js.map +0 -1
- package/docs/STRUCTURE.md +0 -58
- package/src/core/ConditionEvaluator.ts +0 -381
- package/src/core/Observation.ts +0 -47
- package/src/core/PreparationEngine.ts +0 -500
- package/src/core/PromptBuilder.ts +0 -617
- package/src/types/observation.ts +0 -29
- package/src/types/prompt.ts +0 -49
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Healthcare agent example demonstrating
|
|
2
|
+
* Healthcare agent example demonstrating route-based
|
|
3
|
+
* Updated for v2 architecture with session state management and schema-first data extraction
|
|
3
4
|
*/
|
|
4
5
|
|
|
5
6
|
import {
|
|
@@ -9,6 +10,7 @@ import {
|
|
|
9
10
|
END_ROUTE,
|
|
10
11
|
EventSource,
|
|
11
12
|
createMessageEvent,
|
|
13
|
+
createSession,
|
|
12
14
|
} from "../src/index";
|
|
13
15
|
|
|
14
16
|
// Context type
|
|
@@ -17,6 +19,21 @@ interface HealthcareContext {
|
|
|
17
19
|
patientName: string;
|
|
18
20
|
}
|
|
19
21
|
|
|
22
|
+
// Data extraction types for healthcare scenarios
|
|
23
|
+
interface AppointmentData {
|
|
24
|
+
appointmentReason?: string;
|
|
25
|
+
urgency?: "low" | "medium" | "high";
|
|
26
|
+
preferredTime?: string;
|
|
27
|
+
preferredDate?: string;
|
|
28
|
+
appointmentType?: "checkup" | "consultation" | "followup";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface LabResultsData {
|
|
32
|
+
testType?: string;
|
|
33
|
+
testDate?: string;
|
|
34
|
+
resultsNeeded?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
20
37
|
// Tools
|
|
21
38
|
const getInsuranceProviders = defineTool<HealthcareContext, [], string[]>(
|
|
22
39
|
"get_insurance_providers",
|
|
@@ -46,17 +63,29 @@ const scheduleAppointment = defineTool<
|
|
|
46
63
|
HealthcareContext,
|
|
47
64
|
[datetime: string],
|
|
48
65
|
string
|
|
49
|
-
>("schedule_appointment", async (
|
|
50
|
-
|
|
66
|
+
>("schedule_appointment", async ({ context, extracted }, datetime) => {
|
|
67
|
+
const appointment = extracted as Partial<AppointmentData>;
|
|
68
|
+
if (!appointment?.preferredDate || !appointment?.preferredTime) {
|
|
69
|
+
return {
|
|
70
|
+
data: "Please specify preferred date and time for the appointment",
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
data: `Appointment scheduled for ${appointment.preferredDate} at ${
|
|
75
|
+
appointment.preferredTime
|
|
76
|
+
} for ${appointment.appointmentReason || "consultation"}`,
|
|
77
|
+
};
|
|
51
78
|
});
|
|
52
79
|
|
|
53
80
|
const getLabResults = defineTool<HealthcareContext, [], object>(
|
|
54
81
|
"get_lab_results",
|
|
55
|
-
async ({ context }) => {
|
|
82
|
+
async ({ context, extracted }) => {
|
|
83
|
+
const labData = extracted as Partial<LabResultsData>;
|
|
56
84
|
return {
|
|
57
85
|
data: {
|
|
58
|
-
report:
|
|
59
|
-
prognosis: "
|
|
86
|
+
report: `Lab results for ${labData?.testType || "general"} tests`,
|
|
87
|
+
prognosis: "All tests are within the valid range",
|
|
88
|
+
patientName: context.patientName,
|
|
60
89
|
},
|
|
61
90
|
};
|
|
62
91
|
}
|
|
@@ -96,113 +125,167 @@ async function createHealthcareAgent() {
|
|
|
96
125
|
"The doctor who specializes in neurology and is available on Mondays and Tuesdays.",
|
|
97
126
|
});
|
|
98
127
|
|
|
99
|
-
// Create scheduling route
|
|
100
|
-
const schedulingRoute = agent.createRoute({
|
|
128
|
+
// Create scheduling route with data extraction schema
|
|
129
|
+
const schedulingRoute = agent.createRoute<AppointmentData>({
|
|
101
130
|
title: "Schedule an Appointment",
|
|
102
131
|
description: "Helps the patient find a time for their appointment.",
|
|
103
132
|
conditions: ["The patient wants to schedule an appointment"],
|
|
133
|
+
gatherSchema: {
|
|
134
|
+
type: "object",
|
|
135
|
+
properties: {
|
|
136
|
+
appointmentReason: {
|
|
137
|
+
type: "string",
|
|
138
|
+
description: "Reason for the appointment",
|
|
139
|
+
},
|
|
140
|
+
urgency: {
|
|
141
|
+
type: "string",
|
|
142
|
+
enum: ["low", "medium", "high"],
|
|
143
|
+
default: "medium",
|
|
144
|
+
},
|
|
145
|
+
preferredTime: {
|
|
146
|
+
type: "string",
|
|
147
|
+
description: "Preferred time slot",
|
|
148
|
+
},
|
|
149
|
+
preferredDate: {
|
|
150
|
+
type: "string",
|
|
151
|
+
description: "Preferred date",
|
|
152
|
+
},
|
|
153
|
+
appointmentType: {
|
|
154
|
+
type: "string",
|
|
155
|
+
enum: ["checkup", "consultation", "followup"],
|
|
156
|
+
default: "consultation",
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
required: ["appointmentReason"],
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// State 1: Gather appointment reason
|
|
164
|
+
const gatherReason = schedulingRoute.initialState.transitionTo({
|
|
165
|
+
chatState: "Ask what the patient needs an appointment for",
|
|
166
|
+
gather: ["appointmentReason"],
|
|
167
|
+
skipIf: (extracted) => !!extracted.appointmentReason,
|
|
104
168
|
});
|
|
105
169
|
|
|
106
|
-
|
|
107
|
-
|
|
170
|
+
// State 2: Check urgency and show available slots
|
|
171
|
+
const checkUrgency = gatherReason.transitionTo({
|
|
172
|
+
chatState: "Check if this is urgent and show available slots",
|
|
173
|
+
gather: ["urgency"],
|
|
174
|
+
skipIf: (extracted) => !!extracted.urgency,
|
|
175
|
+
requiredData: ["appointmentReason"],
|
|
108
176
|
});
|
|
109
177
|
|
|
110
|
-
const
|
|
178
|
+
const showSlots = checkUrgency.transitionTo({
|
|
111
179
|
toolState: getUpcomingSlots,
|
|
112
180
|
});
|
|
113
181
|
|
|
114
|
-
|
|
115
|
-
|
|
182
|
+
// State 3: Present available times
|
|
183
|
+
const presentTimes = showSlots.transitionTo({
|
|
184
|
+
chatState: "List available times and ask which one works for them",
|
|
116
185
|
});
|
|
117
186
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
187
|
+
// State 4: Gather preferred time and date
|
|
188
|
+
const gatherPreferences = presentTimes.transitionTo({
|
|
189
|
+
chatState: "Collect preferred time and date",
|
|
190
|
+
gather: ["preferredTime", "preferredDate"],
|
|
191
|
+
skipIf: (extracted) =>
|
|
192
|
+
!!extracted.preferredTime && !!extracted.preferredDate,
|
|
193
|
+
});
|
|
124
194
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
195
|
+
// State 5: Confirm details and schedule
|
|
196
|
+
const confirmDetails = gatherPreferences.transitionTo({
|
|
197
|
+
chatState: "Confirm the details with the patient before scheduling",
|
|
198
|
+
gather: ["appointmentType"],
|
|
199
|
+
skipIf: (extracted) => !!extracted.appointmentType,
|
|
200
|
+
requiredData: ["appointmentReason", "preferredTime", "preferredDate"],
|
|
201
|
+
});
|
|
131
202
|
|
|
132
|
-
const
|
|
203
|
+
const schedule = confirmDetails.transitionTo({
|
|
204
|
+
toolState: scheduleAppointment,
|
|
205
|
+
requiredData: ["appointmentReason", "preferredTime", "preferredDate"],
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const confirmation = schedule.transitionTo({
|
|
133
209
|
chatState: "Confirm the appointment has been scheduled",
|
|
134
210
|
});
|
|
135
211
|
|
|
136
|
-
|
|
212
|
+
confirmation.transitionTo({ state: END_ROUTE });
|
|
137
213
|
|
|
138
|
-
// Alternative path: no times work
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
},
|
|
143
|
-
"None of those times work for the patient"
|
|
144
|
-
);
|
|
214
|
+
// Alternative path: no times work - show later slots
|
|
215
|
+
const laterSlots = presentTimes.transitionTo({
|
|
216
|
+
toolState: getLaterSlots,
|
|
217
|
+
});
|
|
145
218
|
|
|
146
|
-
|
|
219
|
+
laterSlots.transitionTo({
|
|
147
220
|
chatState: "List later times and ask if any of them works",
|
|
148
221
|
});
|
|
149
222
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
},
|
|
154
|
-
"The patient picks a time"
|
|
155
|
-
);
|
|
156
|
-
|
|
157
|
-
const t8 = t7.transitionTo(
|
|
158
|
-
{
|
|
223
|
+
// If no times work at all, end route
|
|
224
|
+
laterSlots
|
|
225
|
+
.transitionTo({
|
|
159
226
|
chatState:
|
|
160
227
|
"Ask the patient to call the office to schedule an appointment",
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
t8.transitionTo({ state: END_ROUTE });
|
|
228
|
+
})
|
|
229
|
+
.transitionTo({ state: END_ROUTE });
|
|
166
230
|
|
|
167
231
|
schedulingRoute.createGuideline({
|
|
168
232
|
condition: "The patient says their visit is urgent",
|
|
169
233
|
action: "Tell them to call the office immediately",
|
|
170
234
|
});
|
|
171
235
|
|
|
172
|
-
// Create lab results route
|
|
173
|
-
const labResultsRoute = agent.createRoute({
|
|
236
|
+
// Create lab results route with data extraction schema
|
|
237
|
+
const labResultsRoute = agent.createRoute<LabResultsData>({
|
|
174
238
|
title: "Lab Results",
|
|
175
239
|
description: "Retrieves the patient's lab results and explains them.",
|
|
176
240
|
conditions: ["The patient wants to see their lab results"],
|
|
241
|
+
gatherSchema: {
|
|
242
|
+
type: "object",
|
|
243
|
+
properties: {
|
|
244
|
+
testType: {
|
|
245
|
+
type: "string",
|
|
246
|
+
description: "Type of lab test",
|
|
247
|
+
},
|
|
248
|
+
testDate: {
|
|
249
|
+
type: "string",
|
|
250
|
+
description: "Date of the lab test",
|
|
251
|
+
},
|
|
252
|
+
resultsNeeded: {
|
|
253
|
+
type: "boolean",
|
|
254
|
+
default: true,
|
|
255
|
+
description: "Whether detailed results are needed",
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
required: ["testType"],
|
|
259
|
+
},
|
|
177
260
|
});
|
|
178
261
|
|
|
179
|
-
|
|
180
|
-
|
|
262
|
+
// State 1: Gather test information
|
|
263
|
+
const gatherTestInfo = labResultsRoute.initialState.transitionTo({
|
|
264
|
+
chatState: "Ask what type of test results they want to see",
|
|
265
|
+
gather: ["testType"],
|
|
266
|
+
skipIf: (extracted) => !!extracted.testType,
|
|
181
267
|
});
|
|
182
268
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
);
|
|
269
|
+
// State 2: Optional: gather test date
|
|
270
|
+
const gatherTestDate = gatherTestInfo.transitionTo({
|
|
271
|
+
chatState: "Ask for the test date if they remember it",
|
|
272
|
+
gather: ["testDate"],
|
|
273
|
+
skipIf: (extracted) => !!extracted.testDate,
|
|
274
|
+
requiredData: ["testType"],
|
|
275
|
+
});
|
|
190
276
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
"The lab results are good - i.e., nothing to worry about"
|
|
197
|
-
);
|
|
277
|
+
// State 3: Get lab results
|
|
278
|
+
const getResults = gatherTestDate.transitionTo({
|
|
279
|
+
toolState: getLabResults,
|
|
280
|
+
requiredData: ["testType"],
|
|
281
|
+
});
|
|
198
282
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
);
|
|
283
|
+
// State 4: Present results based on status
|
|
284
|
+
const presentResults = getResults.transitionTo({
|
|
285
|
+
chatState: "Present the lab results and explain what they mean",
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
presentResults.transitionTo({ state: END_ROUTE });
|
|
206
289
|
|
|
207
290
|
labResultsRoute.createGuideline({
|
|
208
291
|
condition:
|
|
@@ -211,14 +294,6 @@ async function createHealthcareAgent() {
|
|
|
211
294
|
"Assertively tell them that you cannot help and they should call the office",
|
|
212
295
|
});
|
|
213
296
|
|
|
214
|
-
// Create observation for disambiguation
|
|
215
|
-
const statusInquiry = agent.createObservation(
|
|
216
|
-
"The patient asks to follow up on their visit, but it's not clear in which way"
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
// Use observation to disambiguate between the two routes
|
|
220
|
-
statusInquiry.disambiguate([schedulingRoute, labResultsRoute]);
|
|
221
|
-
|
|
222
297
|
// Global guidelines
|
|
223
298
|
agent.createGuideline({
|
|
224
299
|
condition: "The patient asks about insurance",
|
|
@@ -242,10 +317,13 @@ async function createHealthcareAgent() {
|
|
|
242
317
|
return agent;
|
|
243
318
|
}
|
|
244
319
|
|
|
245
|
-
// Example usage
|
|
320
|
+
// Example usage with session state management
|
|
246
321
|
async function main() {
|
|
247
322
|
const agent = await createHealthcareAgent();
|
|
248
323
|
|
|
324
|
+
// Initialize session state for multi-turn conversation
|
|
325
|
+
let session = createSession<AppointmentData | LabResultsData>();
|
|
326
|
+
|
|
249
327
|
const history = [
|
|
250
328
|
createMessageEvent(
|
|
251
329
|
EventSource.CUSTOMER,
|
|
@@ -259,7 +337,6 @@ async function main() {
|
|
|
259
337
|
console.log("\nRoutes:", agent.getRoutes().length);
|
|
260
338
|
console.log("Terms:", agent.getTerms().length);
|
|
261
339
|
console.log("Guidelines:", agent.getGuidelines().length);
|
|
262
|
-
console.log("Observations:", agent.getObservations().length);
|
|
263
340
|
|
|
264
341
|
// Print routes
|
|
265
342
|
const routes = agent.getRoutes();
|
|
@@ -267,12 +344,39 @@ async function main() {
|
|
|
267
344
|
console.log("\n" + route.describe());
|
|
268
345
|
}
|
|
269
346
|
|
|
270
|
-
//
|
|
271
|
-
console.log("\
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
347
|
+
// Example conversation with session state
|
|
348
|
+
console.log("\n=== EXAMPLE CONVERSATION ===");
|
|
349
|
+
|
|
350
|
+
// Turn 1: Patient wants to follow up
|
|
351
|
+
const response1 = await agent.respond({ history, session });
|
|
352
|
+
console.log("Patient: Hi, I need to follow up on my visit");
|
|
353
|
+
console.log("Agent:", response1.message);
|
|
354
|
+
console.log("Route:", response1.session?.currentRoute?.title);
|
|
355
|
+
console.log("Extracted:", response1.session?.extracted);
|
|
356
|
+
|
|
357
|
+
// Update session with progress
|
|
358
|
+
session = response1.session!;
|
|
359
|
+
|
|
360
|
+
// Turn 2: Patient specifies they want to schedule an appointment
|
|
361
|
+
if (response1.session?.currentRoute?.title === "Schedule an Appointment") {
|
|
362
|
+
const history2 = [
|
|
363
|
+
...history,
|
|
364
|
+
createMessageEvent(EventSource.AI_AGENT, "Agent", response1.message),
|
|
365
|
+
createMessageEvent(
|
|
366
|
+
EventSource.CUSTOMER,
|
|
367
|
+
"Patient",
|
|
368
|
+
"I need to schedule a checkup for next week"
|
|
369
|
+
),
|
|
370
|
+
];
|
|
371
|
+
|
|
372
|
+
const response2 = await agent.respond({ history: history2, session });
|
|
373
|
+
console.log("\nPatient: I need to schedule a checkup for next week");
|
|
374
|
+
console.log("Agent:", response2.message);
|
|
375
|
+
console.log("Updated extracted:", response2.session?.extracted);
|
|
376
|
+
console.log("Current state:", response2.session?.currentState?.id);
|
|
377
|
+
|
|
378
|
+
// Update session again
|
|
379
|
+
session = response2.session!;
|
|
276
380
|
}
|
|
277
381
|
}
|
|
278
382
|
|
package/examples/openai-agent.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Example: OpenAI Agent with multiple providers
|
|
3
|
-
*
|
|
4
|
-
* This example demonstrates how to use the OpenAI provider
|
|
5
|
-
* and how to switch between different AI providers
|
|
3
|
+
* Updated for v2 architecture with session state management and schema-first data extraction
|
|
6
4
|
*/
|
|
7
5
|
|
|
8
6
|
import {
|
|
@@ -11,6 +9,8 @@ import {
|
|
|
11
9
|
defineTool,
|
|
12
10
|
createMessageEvent,
|
|
13
11
|
EventSource,
|
|
12
|
+
createSession,
|
|
13
|
+
END_ROUTE,
|
|
14
14
|
} from "../src/index";
|
|
15
15
|
|
|
16
16
|
// Custom context type
|
|
@@ -20,18 +20,29 @@ interface CustomerContext {
|
|
|
20
20
|
preferences: string[];
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
//
|
|
23
|
+
// Data extraction type for weather queries
|
|
24
|
+
interface WeatherData {
|
|
25
|
+
location?: string;
|
|
26
|
+
temperature?: number;
|
|
27
|
+
condition?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Define a tool that can access extracted data
|
|
24
31
|
const getWeather = defineTool<
|
|
25
32
|
CustomerContext,
|
|
26
33
|
[{ location: string }],
|
|
27
34
|
{ location: string; temperature: number; condition: string }
|
|
28
35
|
>(
|
|
29
36
|
"get_weather",
|
|
30
|
-
async (
|
|
37
|
+
async ({ context, extracted }, args) => {
|
|
38
|
+
// Use extracted location if available, otherwise use args
|
|
39
|
+
const location =
|
|
40
|
+
(extracted as Partial<WeatherData>)?.location || args.location;
|
|
41
|
+
|
|
31
42
|
// Simulate API call
|
|
32
43
|
return {
|
|
33
44
|
data: {
|
|
34
|
-
location
|
|
45
|
+
location,
|
|
35
46
|
temperature: 72,
|
|
36
47
|
condition: "Sunny",
|
|
37
48
|
},
|
|
@@ -96,33 +107,58 @@ async function main() {
|
|
|
96
107
|
enabled: true,
|
|
97
108
|
});
|
|
98
109
|
|
|
99
|
-
// Create
|
|
100
|
-
const weatherRoute = agent.createRoute({
|
|
110
|
+
// Create weather route with data extraction schema
|
|
111
|
+
const weatherRoute = agent.createRoute<WeatherData>({
|
|
101
112
|
title: "Check Weather",
|
|
102
113
|
description: "Help user check weather for a location",
|
|
103
114
|
conditions: ["User wants to know the weather"],
|
|
115
|
+
gatherSchema: {
|
|
116
|
+
type: "object",
|
|
117
|
+
properties: {
|
|
118
|
+
location: {
|
|
119
|
+
type: "string",
|
|
120
|
+
description: "City or location for weather check",
|
|
121
|
+
},
|
|
122
|
+
temperature: {
|
|
123
|
+
type: "number",
|
|
124
|
+
description: "Temperature in Fahrenheit",
|
|
125
|
+
},
|
|
126
|
+
condition: {
|
|
127
|
+
type: "string",
|
|
128
|
+
description: "Weather condition (sunny, cloudy, rainy, etc.)",
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
required: ["location"],
|
|
132
|
+
},
|
|
104
133
|
});
|
|
105
134
|
|
|
106
|
-
//
|
|
135
|
+
// State 1: Gather location
|
|
107
136
|
const askLocation = weatherRoute.initialState.transitionTo({
|
|
108
137
|
chatState: "Ask which city they want weather for",
|
|
138
|
+
gather: ["location"],
|
|
139
|
+
skipIf: (extracted) => !!extracted.location,
|
|
109
140
|
});
|
|
110
141
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
);
|
|
142
|
+
// State 2: Get weather data
|
|
143
|
+
const fetchWeather = askLocation.transitionTo({
|
|
144
|
+
toolState: getWeather,
|
|
145
|
+
requiredData: ["location"],
|
|
146
|
+
});
|
|
117
147
|
|
|
148
|
+
// State 3: Present weather information
|
|
118
149
|
const showWeather = fetchWeather.transitionTo({
|
|
119
150
|
chatState:
|
|
120
151
|
"Present the weather information in a friendly way with temperature and condition",
|
|
121
152
|
});
|
|
122
153
|
|
|
123
|
-
|
|
154
|
+
showWeather.transitionTo({ state: END_ROUTE });
|
|
155
|
+
|
|
156
|
+
// Example conversation with session state management
|
|
124
157
|
console.log("š¤ Starting OpenAI Agent Example\n");
|
|
125
158
|
|
|
159
|
+
// Initialize session state for multi-turn conversation
|
|
160
|
+
let session = createSession<WeatherData>();
|
|
161
|
+
|
|
126
162
|
// Build history
|
|
127
163
|
const history = [
|
|
128
164
|
createMessageEvent(
|
|
@@ -133,11 +169,10 @@ async function main() {
|
|
|
133
169
|
];
|
|
134
170
|
|
|
135
171
|
try {
|
|
136
|
-
//
|
|
137
|
-
console.log("š¤
|
|
172
|
+
// Turn 1: Process weather query with session state
|
|
173
|
+
console.log("š¤ Processing with session state...");
|
|
174
|
+
const response = await agent.respond({ history, session });
|
|
138
175
|
|
|
139
|
-
// In a real scenario, you would call agent.generateMessage() or similar
|
|
140
|
-
// For now, let's just show the structure:
|
|
141
176
|
console.log("\nā
Agent Configuration:");
|
|
142
177
|
console.log(` AI Provider: ${openaiProvider.name}`);
|
|
143
178
|
|
|
@@ -147,12 +182,19 @@ async function main() {
|
|
|
147
182
|
` States: Initial ā Ask Location ā Fetch Weather ā Show Weather`
|
|
148
183
|
);
|
|
149
184
|
|
|
150
|
-
console.log("\nš¬ Conversation
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
});
|
|
185
|
+
console.log("\nš¬ Conversation:");
|
|
186
|
+
console.log(` Customer: ${history[0].data.message}`);
|
|
187
|
+
console.log(` Agent: ${response.message}`);
|
|
188
|
+
console.log(` Route: ${response.session?.currentRoute?.title}`);
|
|
189
|
+
console.log(` Extracted:`, response.session?.extracted);
|
|
190
|
+
|
|
191
|
+
// Update session with progress
|
|
192
|
+
session = response.session!;
|
|
154
193
|
|
|
155
|
-
console.log("\nāØ
|
|
194
|
+
console.log("\n⨠Session state benefits:");
|
|
195
|
+
console.log(" ā
Data extraction tracked across turns");
|
|
196
|
+
console.log(" ā
State progression managed automatically");
|
|
197
|
+
console.log(" ā
Always-on routing respects intent changes");
|
|
156
198
|
console.log(
|
|
157
199
|
" (Set OPENAI_API_KEY environment variable to make actual API calls)"
|
|
158
200
|
);
|