@voicethere/agent 0.25.0 → 0.27.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.
Files changed (195) hide show
  1. package/README.md +49 -0
  2. package/dist/agent.js +78 -1
  3. package/dist/cli.d.ts +1 -0
  4. package/dist/cli.d.ts.map +1 -1
  5. package/dist/cli.js +11 -0
  6. package/dist/cli.js.map +1 -1
  7. package/dist/flow/classify.d.ts +12 -0
  8. package/dist/flow/classify.d.ts.map +1 -0
  9. package/dist/flow/classify.js +37 -0
  10. package/dist/flow/classify.js.map +1 -0
  11. package/dist/flow/cli-flow.d.ts +19 -0
  12. package/dist/flow/cli-flow.d.ts.map +1 -0
  13. package/dist/flow/cli-flow.js +566 -0
  14. package/dist/flow/cli-flow.js.map +1 -0
  15. package/dist/flow/core/compile.d.ts +131 -0
  16. package/dist/flow/core/compile.d.ts.map +1 -0
  17. package/dist/flow/core/compile.js +197 -0
  18. package/dist/flow/core/compile.js.map +1 -0
  19. package/dist/flow/core/conditions.d.ts +5 -0
  20. package/dist/flow/core/conditions.d.ts.map +1 -0
  21. package/dist/flow/core/conditions.js +70 -0
  22. package/dist/flow/core/conditions.js.map +1 -0
  23. package/dist/flow/core/effects.d.ts +105 -0
  24. package/dist/flow/core/effects.d.ts.map +1 -0
  25. package/dist/flow/core/effects.js +2 -0
  26. package/dist/flow/core/effects.js.map +1 -0
  27. package/dist/flow/core/generate-mock.d.ts +16 -0
  28. package/dist/flow/core/generate-mock.d.ts.map +1 -0
  29. package/dist/flow/core/generate-mock.js +206 -0
  30. package/dist/flow/core/generate-mock.js.map +1 -0
  31. package/dist/flow/core/host.d.ts +9 -0
  32. package/dist/flow/core/host.d.ts.map +1 -0
  33. package/dist/flow/core/host.js +99 -0
  34. package/dist/flow/core/host.js.map +1 -0
  35. package/dist/flow/core/index.d.ts +17 -0
  36. package/dist/flow/core/index.d.ts.map +1 -0
  37. package/dist/flow/core/index.js +17 -0
  38. package/dist/flow/core/index.js.map +1 -0
  39. package/dist/flow/core/interpreter.d.ts +69 -0
  40. package/dist/flow/core/interpreter.d.ts.map +1 -0
  41. package/dist/flow/core/interpreter.js +738 -0
  42. package/dist/flow/core/interpreter.js.map +1 -0
  43. package/dist/flow/core/json-pointer.d.ts +6 -0
  44. package/dist/flow/core/json-pointer.d.ts.map +1 -0
  45. package/dist/flow/core/json-pointer.js +28 -0
  46. package/dist/flow/core/json-pointer.js.map +1 -0
  47. package/dist/flow/core/migrate.d.ts +16 -0
  48. package/dist/flow/core/migrate.d.ts.map +1 -0
  49. package/dist/flow/core/migrate.js +29 -0
  50. package/dist/flow/core/migrate.js.map +1 -0
  51. package/dist/flow/core/mock-effects.d.ts +14 -0
  52. package/dist/flow/core/mock-effects.d.ts.map +1 -0
  53. package/dist/flow/core/mock-effects.js +90 -0
  54. package/dist/flow/core/mock-effects.js.map +1 -0
  55. package/dist/flow/core/mocks.d.ts +55 -0
  56. package/dist/flow/core/mocks.d.ts.map +1 -0
  57. package/dist/flow/core/mocks.js +73 -0
  58. package/dist/flow/core/mocks.js.map +1 -0
  59. package/dist/flow/core/route.d.ts +29 -0
  60. package/dist/flow/core/route.d.ts.map +1 -0
  61. package/dist/flow/core/route.js +61 -0
  62. package/dist/flow/core/route.js.map +1 -0
  63. package/dist/flow/core/schema.d.ts +19 -0
  64. package/dist/flow/core/schema.d.ts.map +1 -0
  65. package/dist/flow/core/schema.js +317 -0
  66. package/dist/flow/core/schema.js.map +1 -0
  67. package/dist/flow/core/template.d.ts +16 -0
  68. package/dist/flow/core/template.d.ts.map +1 -0
  69. package/dist/flow/core/template.js +65 -0
  70. package/dist/flow/core/template.js.map +1 -0
  71. package/dist/flow/core/trace.d.ts +26 -0
  72. package/dist/flow/core/trace.d.ts.map +1 -0
  73. package/dist/flow/core/trace.js +2 -0
  74. package/dist/flow/core/trace.js.map +1 -0
  75. package/dist/flow/core/types.d.ts +159 -0
  76. package/dist/flow/core/types.d.ts.map +1 -0
  77. package/dist/flow/core/types.js +2 -0
  78. package/dist/flow/core/types.js.map +1 -0
  79. package/dist/flow/core/validate.d.ts +3 -0
  80. package/dist/flow/core/validate.d.ts.map +1 -0
  81. package/dist/flow/core/validate.js +550 -0
  82. package/dist/flow/core/validate.js.map +1 -0
  83. package/dist/flow/create-flow-agent.d.ts +36 -0
  84. package/dist/flow/create-flow-agent.d.ts.map +1 -0
  85. package/dist/flow/create-flow-agent.js +175 -0
  86. package/dist/flow/create-flow-agent.js.map +1 -0
  87. package/dist/flow/effects-node.d.ts +21 -0
  88. package/dist/flow/effects-node.d.ts.map +1 -0
  89. package/dist/flow/effects-node.js +189 -0
  90. package/dist/flow/effects-node.js.map +1 -0
  91. package/dist/flow/index.d.ts +12 -0
  92. package/dist/flow/index.d.ts.map +1 -0
  93. package/dist/flow/index.js +7 -0
  94. package/dist/flow/index.js.map +1 -0
  95. package/dist/flow/llm-node.d.ts +24 -0
  96. package/dist/flow/llm-node.d.ts.map +1 -0
  97. package/dist/flow/llm-node.js +251 -0
  98. package/dist/flow/llm-node.js.map +1 -0
  99. package/dist/flow/record.d.ts +22 -0
  100. package/dist/flow/record.d.ts.map +1 -0
  101. package/dist/flow/record.js +126 -0
  102. package/dist/flow/record.js.map +1 -0
  103. package/dist/index.d.ts +2 -2
  104. package/dist/index.d.ts.map +1 -1
  105. package/dist/index.js +1 -1
  106. package/dist/index.js.map +1 -1
  107. package/dist/llm/agent/build-tool-registry.d.ts +26 -0
  108. package/dist/llm/agent/build-tool-registry.d.ts.map +1 -0
  109. package/dist/llm/agent/build-tool-registry.js +106 -0
  110. package/dist/llm/agent/build-tool-registry.js.map +1 -0
  111. package/dist/llm/agent/create-llm-agent.d.ts +5 -0
  112. package/dist/llm/agent/create-llm-agent.d.ts.map +1 -1
  113. package/dist/llm/agent/create-llm-agent.js +38 -103
  114. package/dist/llm/agent/create-llm-agent.js.map +1 -1
  115. package/dist/llm/agent/define-tool.d.ts +2 -0
  116. package/dist/llm/agent/define-tool.d.ts.map +1 -1
  117. package/dist/llm/agent/define-tool.js +1 -0
  118. package/dist/llm/agent/define-tool.js.map +1 -1
  119. package/dist/llm/agent/redact.d.ts.map +1 -1
  120. package/dist/llm/agent/redact.js +1 -0
  121. package/dist/llm/agent/redact.js.map +1 -1
  122. package/dist/llm/anthropic-provider.d.ts.map +1 -1
  123. package/dist/llm/anthropic-provider.js +10 -0
  124. package/dist/llm/anthropic-provider.js.map +1 -1
  125. package/dist/llm/catalog.d.ts +4 -0
  126. package/dist/llm/catalog.d.ts.map +1 -1
  127. package/dist/llm/catalog.js +7 -0
  128. package/dist/llm/catalog.js.map +1 -1
  129. package/dist/llm/end-user/end-user-tools.d.ts +10 -0
  130. package/dist/llm/end-user/end-user-tools.d.ts.map +1 -0
  131. package/dist/llm/end-user/end-user-tools.js +50 -0
  132. package/dist/llm/end-user/end-user-tools.js.map +1 -0
  133. package/dist/llm/gemini-provider.d.ts.map +1 -1
  134. package/dist/llm/gemini-provider.js +12 -0
  135. package/dist/llm/gemini-provider.js.map +1 -1
  136. package/dist/llm/index.d.ts +3 -0
  137. package/dist/llm/index.d.ts.map +1 -1
  138. package/dist/llm/index.js +2 -0
  139. package/dist/llm/index.js.map +1 -1
  140. package/dist/llm/mcp/tool-registry.d.ts +10 -1
  141. package/dist/llm/mcp/tool-registry.d.ts.map +1 -1
  142. package/dist/llm/mcp/tool-registry.js +10 -3
  143. package/dist/llm/mcp/tool-registry.js.map +1 -1
  144. package/dist/llm/openai-provider.d.ts.map +1 -1
  145. package/dist/llm/openai-provider.js +10 -0
  146. package/dist/llm/openai-provider.js.map +1 -1
  147. package/dist/llm/types.d.ts +7 -2
  148. package/dist/llm/types.d.ts.map +1 -1
  149. package/dist/llm/types.js.map +1 -1
  150. package/dist/protocol.d.ts +53 -3
  151. package/dist/protocol.d.ts.map +1 -1
  152. package/dist/protocol.js +2 -0
  153. package/dist/protocol.js.map +1 -1
  154. package/dist/runtime.d.ts +34 -1
  155. package/dist/runtime.d.ts.map +1 -1
  156. package/dist/runtime.js +133 -0
  157. package/dist/runtime.js.map +1 -1
  158. package/dist/templates/echo/agent.js +87 -1
  159. package/dist/templates/echo-dc/agent.js +87 -1
  160. package/dist/templates/flow-agent/agent.js +49176 -0
  161. package/dist/templates/game-sync/agent.js +87 -1
  162. package/dist/templates/language-switch/agent.js +87 -1
  163. package/dist/templates/llm-agent/agent.js +299 -105
  164. package/dist/templates/llm-docs-rag/agent.js +299 -105
  165. package/dist/templates/llm-mcp-service/agent.js +299 -105
  166. package/dist/templates/llm-support-sticky-user/agent.js +60690 -0
  167. package/dist/templates/positional-tts/agent.js +87 -1
  168. package/dist/templates/recording-consent/agent.js +87 -1
  169. package/dist/templates/registry.d.ts.map +1 -1
  170. package/dist/templates/registry.js +49 -0
  171. package/dist/templates/registry.js.map +1 -1
  172. package/dist/templates/spatial-showcase/agent.js +87 -1
  173. package/dist/templates/voice-showcase/agent.js +87 -1
  174. package/dist/templates/voice-starter/agent.js +87 -1
  175. package/dist/templates/webhooks/agent.js +87 -1
  176. package/dist/templates/webhooks-redis/agent.js +87 -1
  177. package/dist/templates/world-sync/agent.js +87 -1
  178. package/dist/templates/world-sync-binary/agent.js +87 -1
  179. package/eject/llm/loop.ts +44 -102
  180. package/package.json +19 -2
  181. package/schema/voicethere.flow.schema.json +681 -0
  182. package/templates/README.md +1 -0
  183. package/templates/flow-agent/README.md +38 -0
  184. package/templates/flow-agent/agent.ts +14 -0
  185. package/templates/flow-agent/tools.ts +5 -0
  186. package/templates/flow-agent/voicethere.flow.json +80 -0
  187. package/templates/flow-agent/voicethere.flow.mocks.json +17 -0
  188. package/templates/llm-support-sticky-user/README.md +68 -0
  189. package/templates/llm-support-sticky-user/agent.ts +29 -0
  190. package/templates/llm-support-sticky-user/hooks.ts +188 -0
  191. package/templates/llm-support-sticky-user/md.d.ts +4 -0
  192. package/templates/llm-support-sticky-user/mock-crm.ts +75 -0
  193. package/templates/llm-support-sticky-user/prompt.md +21 -0
  194. package/templates/llm-support-sticky-user/tools.ts +244 -0
  195. package/templates/llm-support-sticky-user/voicethere.llm.json +52 -0
@@ -0,0 +1,244 @@
1
+ import { agentLog } from "@voicethere/agent";
2
+ import { defineTool } from "@voicethere/agent/llm";
3
+ import type { Crm, Ticket } from "./mock-crm.js";
4
+ import { mockCrm } from "./mock-crm.js";
5
+
6
+ const CRM_TIMEOUT_MS = 3000;
7
+ const MAX_STEP_CHARS = 500;
8
+ const MAX_PENDING_STEPS = 20;
9
+ const TICKET_ID = /^[A-Za-z0-9_-]{1,64}$/;
10
+
11
+ /** HTTP client for your CRM. See the README for the four calls it makes. */
12
+ export function createHttpCrm(baseUrl: string, apiKey: string): Crm {
13
+ const base = baseUrl.replace(/\/+$/, "");
14
+ const call = async (method: string, path: string, body?: unknown) => {
15
+ const res = await fetch(`${base}${path}`, {
16
+ method,
17
+ headers: {
18
+ Authorization: `Bearer ${apiKey}`,
19
+ ...(body === undefined ? {} : { "Content-Type": "application/json" }),
20
+ },
21
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
22
+ signal: AbortSignal.timeout(CRM_TIMEOUT_MS),
23
+ });
24
+ if (res.status === 404) return null;
25
+ if (!res.ok)
26
+ throw new Error(`CRM request failed with status ${res.status}`);
27
+ return (await res.json()) as unknown;
28
+ };
29
+ const user = (id: string) => `/users/${encodeURIComponent(id)}`;
30
+ return {
31
+ async listOpenTickets(userId) {
32
+ const r = await call("GET", `${user(userId)}/tickets?status=open`);
33
+ return Array.isArray(r) ? (r as Ticket[]) : [];
34
+ },
35
+ async getTicket(userId, ticketId) {
36
+ const r = await call(
37
+ "GET",
38
+ `${user(userId)}/tickets/${encodeURIComponent(ticketId)}`,
39
+ );
40
+ return r ? (r as Ticket) : null;
41
+ },
42
+ async addStep(userId, ticketId, step) {
43
+ const r = await call(
44
+ "POST",
45
+ `${user(userId)}/tickets/${encodeURIComponent(ticketId)}/steps`,
46
+ { step },
47
+ );
48
+ return r !== null;
49
+ },
50
+ async createTicket(userId, input) {
51
+ const r = await call("POST", `${user(userId)}/tickets`, input);
52
+ return r as Ticket;
53
+ },
54
+ };
55
+ }
56
+
57
+ /** The HTTP CRM when a CRM URL is set, the in-memory one otherwise. */
58
+ export function defaultCrm(): Crm {
59
+ const url = process.env.AGENT_CRM_API_URL ?? process.env.CRM_API_URL;
60
+ if (!url) return mockCrm;
61
+ const key = process.env.AGENT_CRM_API_KEY ?? process.env.CRM_API_KEY ?? "";
62
+ return createHttpCrm(url, key);
63
+ }
64
+
65
+ export interface PendingStep {
66
+ userId: string;
67
+ ticketId: string;
68
+ step: string;
69
+ }
70
+
71
+ /** Steps the model recorded during a call. `onSessionEnd` writes them to the CRM. */
72
+ export class PendingSteps {
73
+ private readonly bySession = new Map<string, PendingStep[]>();
74
+ /** Start collecting for a session. Steps for a session that is not open are refused. */
75
+ open(sessionId: string): void {
76
+ if (!this.bySession.has(sessionId)) this.bySession.set(sessionId, []);
77
+ }
78
+ add(sessionId: string, s: PendingStep): "ok" | "full" | "closed" {
79
+ const list = this.bySession.get(sessionId);
80
+ if (!list) return "closed";
81
+ if (list.length >= MAX_PENDING_STEPS) return "full";
82
+ list.push({ ...s, step: s.step.slice(0, MAX_STEP_CHARS) });
83
+ return "ok";
84
+ }
85
+ take(sessionId: string): PendingStep[] {
86
+ const list = this.bySession.get(sessionId) ?? [];
87
+ this.bySession.delete(sessionId);
88
+ return list;
89
+ }
90
+ }
91
+
92
+ export const pendingSteps = new PendingSteps();
93
+
94
+ const UNAVAILABLE = "The ticket system is unavailable right now.";
95
+ const LOGIN = "Ask the caller to log in first; account actions need a login.";
96
+
97
+ export function formatTicket(t: Ticket): string {
98
+ const steps = t.stepsTried.length
99
+ ? ` Steps already tried: ${t.stepsTried.join("; ")}.`
100
+ : "";
101
+ return `${t.id}: ${t.title}. ${t.summary}${steps}`;
102
+ }
103
+
104
+ /**
105
+ * The ticket tools. The user comes only from the session (`ctx.user.id`); no tool
106
+ * takes a user id as input, so a spoken "I am user 456" changes nothing.
107
+ */
108
+ export function createTools(deps: { crm?: () => Crm; pending?: PendingSteps }) {
109
+ const crm = deps.crm ?? defaultCrm;
110
+ const pending = deps.pending ?? pendingSteps;
111
+ const failed = (sessionId: string, tool: string, e: unknown) => {
112
+ agentLog(
113
+ "warn",
114
+ "crm tool failed",
115
+ { tool, error: e instanceof Error ? e.name : "Error" },
116
+ sessionId,
117
+ );
118
+ return UNAVAILABLE;
119
+ };
120
+ const ticketIdSchema = { type: "string", maxLength: 64 };
121
+
122
+ return [
123
+ defineTool({
124
+ name: "get_open_tickets",
125
+ description:
126
+ "The caller's open support tickets, with the steps already tried.",
127
+ inputSchema: { type: "object", additionalProperties: false },
128
+ requiresVerifiedUser: true,
129
+ handler: async (_args, ctx) => {
130
+ if (!ctx.user) return LOGIN;
131
+ try {
132
+ const list = await crm().listOpenTickets(ctx.user.id);
133
+ return list.length
134
+ ? list.map(formatTicket).join("\n")
135
+ : "No open tickets.";
136
+ } catch (e) {
137
+ return failed(ctx.sessionId, "get_open_tickets", e);
138
+ }
139
+ },
140
+ }),
141
+ defineTool<{ ticketId?: unknown }>({
142
+ name: "get_ticket",
143
+ description: "One of the caller's tickets by its id.",
144
+ inputSchema: {
145
+ type: "object",
146
+ additionalProperties: false,
147
+ required: ["ticketId"],
148
+ properties: { ticketId: ticketIdSchema },
149
+ },
150
+ requiresVerifiedUser: true,
151
+ handler: async (args, ctx) => {
152
+ if (!ctx.user) return LOGIN;
153
+ if (
154
+ typeof args.ticketId !== "string" ||
155
+ !TICKET_ID.test(args.ticketId)
156
+ ) {
157
+ return "Invalid ticket id.";
158
+ }
159
+ try {
160
+ const t = await crm().getTicket(ctx.user.id, args.ticketId);
161
+ return t ? formatTicket(t) : "No such ticket.";
162
+ } catch (e) {
163
+ return failed(ctx.sessionId, "get_ticket", e);
164
+ }
165
+ },
166
+ }),
167
+ defineTool<{ ticketId?: unknown; step?: unknown }>({
168
+ name: "add_troubleshooting_step",
169
+ description:
170
+ "Record a troubleshooting step the caller just tried on one of their tickets. It is saved when the call ends.",
171
+ inputSchema: {
172
+ type: "object",
173
+ additionalProperties: false,
174
+ required: ["ticketId", "step"],
175
+ properties: {
176
+ ticketId: ticketIdSchema,
177
+ step: { type: "string", maxLength: MAX_STEP_CHARS },
178
+ },
179
+ },
180
+ requiresVerifiedUser: true,
181
+ handler: async (args, ctx) => {
182
+ if (!ctx.user) return LOGIN;
183
+ if (
184
+ typeof args.ticketId !== "string" ||
185
+ !TICKET_ID.test(args.ticketId) ||
186
+ typeof args.step !== "string" ||
187
+ args.step.trim() === ""
188
+ ) {
189
+ return "Invalid ticket id or step.";
190
+ }
191
+ try {
192
+ const t = await crm().getTicket(ctx.user.id, args.ticketId);
193
+ if (!t) return "No such ticket.";
194
+ const added = pending.add(ctx.sessionId, {
195
+ userId: ctx.user.id,
196
+ ticketId: args.ticketId,
197
+ step: args.step.trim(),
198
+ });
199
+ if (added === "closed") return "The call has ended.";
200
+ return added === "ok"
201
+ ? "Step noted."
202
+ : "Too many steps recorded in this call.";
203
+ } catch (e) {
204
+ return failed(ctx.sessionId, "add_troubleshooting_step", e);
205
+ }
206
+ },
207
+ }),
208
+ defineTool<{ title?: unknown; summary?: unknown }>({
209
+ name: "create_ticket",
210
+ description: "Open a new support ticket for the caller.",
211
+ inputSchema: {
212
+ type: "object",
213
+ additionalProperties: false,
214
+ required: ["title", "summary"],
215
+ properties: {
216
+ title: { type: "string", maxLength: 120 },
217
+ summary: { type: "string", maxLength: 1000 },
218
+ },
219
+ },
220
+ requiresVerifiedUser: true,
221
+ handler: async (args, ctx) => {
222
+ if (!ctx.user) return LOGIN;
223
+ if (
224
+ typeof args.title !== "string" ||
225
+ typeof args.summary !== "string" ||
226
+ args.title.trim() === ""
227
+ ) {
228
+ return "A title and a summary are required.";
229
+ }
230
+ try {
231
+ const t = await crm().createTicket(ctx.user.id, {
232
+ title: args.title.trim().slice(0, 120),
233
+ summary: args.summary.trim().slice(0, 1000),
234
+ });
235
+ return `Ticket ${t.id} created.`;
236
+ } catch (e) {
237
+ return failed(ctx.sessionId, "create_ticket", e);
238
+ }
239
+ },
240
+ }),
241
+ ];
242
+ }
243
+
244
+ export const tools = createTools({});
@@ -0,0 +1,52 @@
1
+ {
2
+ "$schema": "./node_modules/@voicethere/agent/schema/voicethere.llm.schema.json",
3
+ "version": 1,
4
+ "provider": "openrouter",
5
+ "model": "anthropic/claude-haiku-5.5",
6
+ "apiKeySecret": "AGENT_OPENROUTER_API_KEY",
7
+ "maxOutputTokens": 1024,
8
+ "greeting": {
9
+ "text": "Hi! This is support. How can I help you today?",
10
+ "interruptible": true
11
+ },
12
+ "bargeIn": {
13
+ "mode": "project"
14
+ },
15
+ "history": {
16
+ "turns": 20,
17
+ "interruptionNote": true
18
+ },
19
+ "fillers": {
20
+ "thinking": {
21
+ "phrases": ["Okay.", "Right.", "Sure.", "Let me see."],
22
+ "afterMs": 1500
23
+ },
24
+ "toolCall": {
25
+ "phrases": [
26
+ "Let me check that for you.",
27
+ "One moment, I'll look that up."
28
+ ]
29
+ },
30
+ "toolWait": {
31
+ "phrases": ["Still checking, one moment.", "Almost there."],
32
+ "afterMs": 4000
33
+ },
34
+ "interruptible": true
35
+ },
36
+ "tools": {
37
+ "maxRounds": 5,
38
+ "timeoutMs": 15000
39
+ },
40
+ "toolSources": [],
41
+ "knowledge": {
42
+ "store": "off",
43
+ "topK": 4,
44
+ "embeddings": {
45
+ "provider": "openai",
46
+ "model": "text-embedding-3-small",
47
+ "dimensions": 512,
48
+ "apiKeySecret": "AGENT_OPENAI_API_KEY"
49
+ }
50
+ },
51
+ "interruptPolicy": "cancel"
52
+ }