@andreprado/agentkit 0.1.0-alpha.2 → 0.1.0-alpha.21

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 (142) hide show
  1. package/README.md +68 -6
  2. package/docs/guides/add-channel.md +189 -7
  3. package/docs/guides/add-knowledge.md +144 -0
  4. package/docs/guides/add-managed-composio.md +163 -0
  5. package/docs/guides/add-tool.md +1 -1
  6. package/docs/guides/channel-security.md +128 -32
  7. package/docs/guides/connect-discord.md +178 -0
  8. package/docs/guides/connect-slack.md +126 -0
  9. package/docs/guides/connect-telegram.md +78 -1
  10. package/docs/guides/connect-whatsapp-evolution.md +121 -0
  11. package/docs/guides/connect-whatsapp-uazapi.md +126 -0
  12. package/docs/guides/connect-whatsapp-zapster.md +112 -8
  13. package/docs/guides/create-agent.md +45 -4
  14. package/docs/guides/debug-channel.md +147 -0
  15. package/docs/guides/improve-from-production.md +151 -0
  16. package/docs/guides/prepare-deploy.md +47 -17
  17. package/docs/guides/replay-production-traces.md +72 -0
  18. package/docs/guides/run-evals.md +147 -20
  19. package/docs/guides/security-rules.md +7 -6
  20. package/docs/guides/send-feedback.md +135 -0
  21. package/docs/guides/use-provider.md +27 -3
  22. package/docs/llms-full.txt +348 -55
  23. package/docs/llms.txt +62 -7
  24. package/package.json +2 -5
  25. package/src/cli/args.ts +57 -0
  26. package/src/cli/cloud-client.ts +377 -0
  27. package/src/cli/commands/channels.ts +1586 -0
  28. package/src/cli/commands/feedback.ts +438 -0
  29. package/src/cli/commands/knowledge.ts +136 -0
  30. package/src/cli/commands/transcribe.ts +171 -0
  31. package/src/cli/constants.ts +4 -0
  32. package/src/cli/deploy-chat-ui.ts +535 -0
  33. package/src/cli/deploy-readiness.ts +481 -0
  34. package/src/cli/flags.ts +162 -0
  35. package/src/cli/help.ts +236 -0
  36. package/src/cli/index.ts +1167 -1005
  37. package/src/cli/process.ts +31 -0
  38. package/src/cloud/artifact.ts +139 -0
  39. package/src/cloud/client.ts +80 -0
  40. package/src/cloud/contracts.ts +63 -0
  41. package/src/cloud/index.ts +3 -0
  42. package/src/create-project.ts +21 -6
  43. package/src/index.ts +517 -8
  44. package/src/providers/pi.ts +70 -16
  45. package/src/providers/test.ts +88 -1
  46. package/src/providers/types.ts +7 -0
  47. package/src/runtime/channel-buffer.ts +30 -0
  48. package/src/runtime/channel-test-harness.ts +21 -1
  49. package/src/runtime/channels/discord.ts +896 -0
  50. package/src/runtime/channels/generic-webhook.ts +225 -0
  51. package/src/runtime/channels/slack.ts +646 -0
  52. package/src/runtime/channels/telegram.ts +466 -23
  53. package/src/runtime/channels/whatsapp-evolution.ts +1357 -0
  54. package/src/runtime/channels/whatsapp-meta.ts +9 -0
  55. package/src/runtime/channels/whatsapp-uazapi.ts +1327 -0
  56. package/src/runtime/channels/whatsapp-zapster.ts +677 -40
  57. package/src/runtime/channels.ts +87 -4
  58. package/src/runtime/chat.ts +130 -38
  59. package/src/runtime/config.ts +519 -19
  60. package/src/runtime/core/manifest.ts +103 -5
  61. package/src/runtime/core/targets.ts +5 -5
  62. package/src/runtime/database.ts +93 -2
  63. package/src/runtime/db-commands.ts +9 -0
  64. package/src/runtime/deploy-readiness.ts +46 -4
  65. package/src/runtime/deploy.ts +1 -1
  66. package/src/runtime/dev-server.ts +779 -45
  67. package/src/runtime/env.ts +8 -3
  68. package/src/runtime/evals.ts +589 -43
  69. package/src/runtime/improve.ts +868 -0
  70. package/src/runtime/inspect.ts +194 -4
  71. package/src/runtime/integrations/composio.ts +423 -0
  72. package/src/runtime/knowledge/chunk.ts +333 -0
  73. package/src/runtime/knowledge/config.ts +135 -0
  74. package/src/runtime/knowledge/embeddings.ts +133 -0
  75. package/src/runtime/knowledge/ingest.ts +521 -0
  76. package/src/runtime/knowledge/prompt-policy.ts +30 -0
  77. package/src/runtime/knowledge/retrieve.ts +303 -0
  78. package/src/runtime/knowledge/schema.ts +100 -0
  79. package/src/runtime/knowledge/tool.ts +64 -0
  80. package/src/runtime/knowledge/vector.ts +258 -0
  81. package/src/runtime/prompt-context.ts +141 -0
  82. package/src/runtime/runtime-contract.ts +86 -8
  83. package/src/runtime/skills.ts +95 -0
  84. package/src/runtime/spec.ts +152 -0
  85. package/src/runtime/sync.ts +144 -0
  86. package/src/runtime/targets/cloudflare/build.ts +1468 -203
  87. package/src/runtime/targets/container/server.ts +1 -1
  88. package/src/runtime/targets/vps/deploy.ts +26 -9
  89. package/src/runtime/tool-runner.ts +9 -1
  90. package/src/runtime/tools.ts +128 -2
  91. package/src/runtime/traces.ts +41 -0
  92. package/src/runtime/transcription.ts +483 -0
  93. package/src/storage/sqlite.ts +149 -3
  94. package/src/templates/blank.ts +76 -17
  95. package/src/templates/dentista.ts +1011 -0
  96. package/src/templates/index.ts +2 -0
  97. package/src/templates/skills/agentkit-build-agent/SKILL.md +52 -0
  98. package/src/templates/skills/agentkit-build-agent/templates/appointment-intake.instructions.md +21 -0
  99. package/src/templates/skills/agentkit-build-agent/templates/sales-qualifier.instructions.md +17 -0
  100. package/src/templates/skills/agentkit-build-agent/templates/support-agent.instructions.md +16 -0
  101. package/src/templates/skills/agentkit-capsule/SKILL.md +70 -0
  102. package/src/templates/skills/agentkit-capsule/references/docs-router.md +15 -0
  103. package/src/templates/skills/agentkit-channels/SKILL.md +127 -0
  104. package/src/templates/skills/agentkit-channels/references/channel-buffering.md +65 -0
  105. package/src/templates/skills/agentkit-channels/references/channel-debugging.md +66 -0
  106. package/src/templates/skills/agentkit-channels/references/discord.md +93 -0
  107. package/src/templates/skills/agentkit-channels/references/slack.md +56 -0
  108. package/src/templates/skills/agentkit-channels/references/telegram.md +72 -0
  109. package/src/templates/skills/agentkit-channels/references/whatsapp-evolution.md +57 -0
  110. package/src/templates/skills/agentkit-channels/references/whatsapp-uazapi.md +61 -0
  111. package/src/templates/skills/agentkit-channels/references/whatsapp-zapster.md +77 -0
  112. package/src/templates/skills/agentkit-database/SKILL.md +45 -0
  113. package/src/templates/skills/agentkit-database/templates/appointments.schema.sql +15 -0
  114. package/src/templates/skills/agentkit-database/templates/leads.schema.sql +17 -0
  115. package/src/templates/skills/agentkit-deploy/SKILL.md +50 -0
  116. package/src/templates/skills/agentkit-evals/SKILL.md +109 -0
  117. package/src/templates/skills/agentkit-evals/templates/multi-turn.eval.md +29 -0
  118. package/src/templates/skills/agentkit-evals/templates/no-leak.eval.md +18 -0
  119. package/src/templates/skills/agentkit-evals/templates/smoke.eval.md +18 -0
  120. package/src/templates/skills/agentkit-evals/templates/tool-call.eval.md +27 -0
  121. package/src/templates/skills/agentkit-improve/SKILL.md +86 -0
  122. package/src/templates/skills/agentkit-improve/references/replay-side-effects.md +18 -0
  123. package/src/templates/skills/agentkit-improve/references/trace-packets.md +22 -0
  124. package/src/templates/skills/agentkit-improve/templates/regression.eval.md +18 -0
  125. package/src/templates/skills/agentkit-integrations/SKILL.md +76 -0
  126. package/src/templates/skills/agentkit-knowledge/SKILL.md +43 -0
  127. package/src/templates/skills/agentkit-knowledge/templates/faq.md +14 -0
  128. package/src/templates/skills/agentkit-knowledge/templates/policies.md +14 -0
  129. package/src/templates/skills/agentkit-knowledge/templates/prices.csv +3 -0
  130. package/src/templates/skills/agentkit-prompts/SKILL.md +47 -0
  131. package/src/templates/skills/agentkit-prompts/templates/knowledge-grounded-faq.instructions.md +11 -0
  132. package/src/templates/skills/agentkit-provider/SKILL.md +60 -0
  133. package/src/templates/skills/agentkit-security/SKILL.md +56 -0
  134. package/src/templates/skills/agentkit-tools/SKILL.md +37 -0
  135. package/src/templates/skills/agentkit-tools/examples/database-write.tool.md +35 -0
  136. package/src/templates/skills/agentkit-tools/examples/eval-safe-external-action.tool.md +37 -0
  137. package/src/templates/skills/agentkit-tools/examples/lookup-order.tool.md +46 -0
  138. package/src/templates/skills/agentkit-troubleshooting/SKILL.md +76 -0
  139. package/src/templates/support.ts +77 -18
  140. package/docs/guides/channels-production-handoff.md +0 -99
  141. package/docs/portable-deploy-release-checklist.md +0 -41
  142. package/src/runtime/targets/cloudflare/deploy.ts +0 -5475
@@ -13,21 +13,38 @@ import type {
13
13
  AgentChannel,
14
14
  ChannelProvider,
15
15
  ChannelType,
16
+ DiscordChannelMode,
16
17
  TelegramAllowedUpdate,
18
+ AgentKnowledgeEmbeddingProvider,
19
+ ChannelBufferMode,
20
+ AgentIntegration,
21
+ ChannelAudioMode,
22
+ TranscriptionProviderName,
17
23
  } from "../index";
24
+ import { MANAGED_COMPOSIO_TOOL_NAME } from "./integrations/composio";
18
25
  import { AgentKitError } from "./errors";
26
+ import { isValidTimeZone } from "./prompt-context";
19
27
 
20
28
  const CONFIG_FILE = "agentkit.config.ts";
21
29
  const AGENT_RUNTIMES = new Set<AgentRuntime>(["local", "edge"]);
22
30
  const PROVIDERS = new Set<AgentProviderName>(["test", "openai", "anthropic", "openrouter", "custom"]);
23
31
  const ACCESS_MODES = new Set<AccessMode>(["private", "token", "public"]);
24
32
  const TOOL_VISIBILITIES = new Set<ToolVisibility>(["user", "internal"]);
25
- const CHANNEL_TYPES = new Set<ChannelType>(["website", "telegram", "whatsapp"]);
26
- const CHANNEL_PROVIDERS = new Set<ChannelProvider>(["agentkit", "telegram", "zapster", "meta"]);
33
+ const CHANNEL_TYPES = new Set<ChannelType>(["website", "telegram", "whatsapp", "discord", "slack", "webhook"]);
34
+ const CHANNEL_PROVIDERS = new Set<ChannelProvider>(["agentkit", "telegram", "zapster", "meta", "uazapi", "evolution", "discord", "slack", "generic"]);
35
+ const CHANNEL_BUFFER_MODES = new Set<ChannelBufferMode>(["off", "debounce"]);
36
+ const CHANNEL_AUDIO_MODES = new Set<ChannelAudioMode>(["off", "reject", "transcribe"]);
37
+ const DISCORD_CHANNEL_MODES = new Set<DiscordChannelMode>(["interactions", "bot"]);
27
38
  const TELEGRAM_ALLOWED_UPDATES = new Set<TelegramAllowedUpdate>(["message"]);
39
+ const KNOWLEDGE_EMBEDDING_PROVIDERS = new Set<AgentKnowledgeEmbeddingProvider>(["none", "test", "openai"]);
40
+ const TRANSCRIPTION_PROVIDERS = new Set<TranscriptionProviderName>(["test", "openai", "groq"]);
28
41
  const CHANNEL_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
42
+ const INTEGRATION_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
43
+ const COMPOSIO_TOOLKIT_PATTERN = /^[a-z][a-z0-9_-]*$/;
44
+ const COMPOSIO_TOOL_SLUG_PATTERN = /^[A-Z][A-Z0-9_]*$/;
29
45
  const SECRET_NAME_PATTERN = /^[A-Z_][A-Z0-9_]*$/;
30
46
  const moduleDir = dirname(fileURLToPath(import.meta.url));
47
+ let configImportNonce = 0;
31
48
 
32
49
  export type LoadedAgentCapsule = {
33
50
  root: string;
@@ -104,15 +121,13 @@ function resolveLocalStoragePath(root: string, config: AgentConfig): string | un
104
121
  }
105
122
 
106
123
  async function loadConfigModule(configPath: string): Promise<unknown> {
107
- const configUrl = pathToFileURL(configPath);
108
- configUrl.searchParams.set("t", String(Date.now()));
124
+ const source = await readFile(configPath, "utf8");
109
125
 
110
126
  try {
111
- const module = (await import(configUrl.href)) as { default?: unknown };
112
- return module.default;
127
+ return await importConfigSource(configPath, source);
113
128
  } catch (error) {
114
129
  if (isMissingAgentKitSelfImport(error)) {
115
- return loadConfigModuleWithLocalSelfImport(configPath, error);
130
+ return loadConfigModuleWithLocalSelfImport(configPath, source, error);
116
131
  }
117
132
 
118
133
  throw new AgentKitError(
@@ -123,8 +138,11 @@ async function loadConfigModule(configPath: string): Promise<unknown> {
123
138
  }
124
139
  }
125
140
 
126
- async function loadConfigModuleWithLocalSelfImport(configPath: string, originalError: unknown): Promise<unknown> {
127
- const source = await readFile(configPath, "utf8");
141
+ async function loadConfigModuleWithLocalSelfImport(
142
+ configPath: string,
143
+ source: string,
144
+ originalError: unknown,
145
+ ): Promise<unknown> {
128
146
  const localAgentKitUrl = pathToFileURL(resolve(moduleDir, "../index.ts")).href;
129
147
  const patchedSource = source.replace(
130
148
  /from\s+["']@andreprado\/agentkit["']/g,
@@ -139,15 +157,20 @@ async function loadConfigModuleWithLocalSelfImport(configPath: string, originalE
139
157
  );
140
158
  }
141
159
 
160
+ return importConfigSource(configPath, patchedSource);
161
+ }
162
+
163
+ async function importConfigSource(configPath: string, source: string): Promise<unknown> {
164
+ const nonce = nextConfigImportNonce();
142
165
  const tempConfigPath = join(
143
166
  dirname(configPath),
144
- `.agentkit.config.loader-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}.ts`,
167
+ `.agentkit.config.loader-${process.pid}-${nonce}-${Math.random().toString(16).slice(2)}.ts`,
145
168
  );
146
169
 
147
170
  try {
148
- await writeFile(tempConfigPath, patchedSource);
171
+ await writeFile(tempConfigPath, source);
149
172
  const configUrl = pathToFileURL(tempConfigPath);
150
- configUrl.searchParams.set("t", String(Date.now()));
173
+ configUrl.searchParams.set("t", nonce);
151
174
  const module = (await import(configUrl.href)) as { default?: unknown };
152
175
  return module.default;
153
176
  } catch (error) {
@@ -161,6 +184,11 @@ async function loadConfigModuleWithLocalSelfImport(configPath: string, originalE
161
184
  }
162
185
  }
163
186
 
187
+ function nextConfigImportNonce(): string {
188
+ configImportNonce += 1;
189
+ return `${Date.now()}-${configImportNonce}`;
190
+ }
191
+
164
192
  function isMissingAgentKitSelfImport(error: unknown): boolean {
165
193
  const message = error instanceof Error ? error.message : String(error);
166
194
  return message.includes("@andreprado/agentkit");
@@ -191,8 +219,17 @@ function validateAgentConfig(value: unknown): asserts value is AgentConfig {
191
219
  throw invalidConfig('provider.model must be "fake" when provider.name is "test".');
192
220
  }
193
221
 
222
+ if (value.timeZone !== undefined) {
223
+ expectString(value.timeZone, "timeZone");
224
+
225
+ if (!isValidTimeZone(value.timeZone)) {
226
+ throw invalidConfig('timeZone must be a valid IANA time zone such as "UTC" or "America/New_York".');
227
+ }
228
+ }
229
+
194
230
  expectString(value.instructions, "instructions");
195
231
  expectStringArray(value.secrets, "secrets");
232
+ validateSecretNames(value.secrets, "secrets");
196
233
 
197
234
  if (value.tools !== undefined) {
198
235
  if (!Array.isArray(value.tools)) {
@@ -204,7 +241,10 @@ function validateAgentConfig(value: unknown): asserts value is AgentConfig {
204
241
  }
205
242
  }
206
243
 
207
- validateChannels(value.channels, value.runtime as AgentRuntime);
244
+ validateIntegrations(value.integrations, value.runtime as AgentRuntime, value.tools);
245
+ validateTranscriptionConfig(value.transcription);
246
+ validateChannels(value.channels, value.runtime as AgentRuntime, Boolean(value.transcription));
247
+ validateKnowledgeConfig(value.knowledge);
208
248
 
209
249
  if (!isRecord(value.access) || !ACCESS_MODES.has(value.access.mode as AccessMode)) {
210
250
  throw invalidConfig('access.mode must be "private", "token", or "public".');
@@ -222,11 +262,310 @@ function validateAgentConfig(value: unknown): asserts value is AgentConfig {
222
262
  throw invalidConfig("storage.path must be a string when provided.");
223
263
  }
224
264
 
265
+ if (value.storage.driver === "sqlite" && typeof value.storage.path !== "string") {
266
+ throw invalidConfig('storage.path is required when storage.driver is "sqlite".');
267
+ }
268
+
225
269
  validateDatabaseConfig(value.storage.database);
226
270
  validateFilesConfig(value.storage.files);
227
271
  }
228
272
 
229
- function validateChannels(value: unknown, runtime: AgentRuntime): void {
273
+ function validateIntegrations(
274
+ value: unknown,
275
+ runtime: AgentRuntime,
276
+ tools: AgentConfig["tools"] = [],
277
+ ): void {
278
+ if (value === undefined) {
279
+ return;
280
+ }
281
+
282
+ if (!Array.isArray(value)) {
283
+ throw invalidConfig("integrations must be an array when provided.");
284
+ }
285
+
286
+ if (value.length > 0 && runtime !== "edge") {
287
+ throw invalidConfig('managed integrations require runtime: "edge". Use BYO defineTool wrappers for local-only integrations.');
288
+ }
289
+
290
+ let composioCount = 0;
291
+
292
+ for (const [index, integration] of value.entries()) {
293
+ if (!isRecord(integration)) {
294
+ throw invalidConfig(`integrations[${index}] must be an object.`);
295
+ }
296
+
297
+ if (integration.provider !== "composio" || integration.type !== "composio" || integration.mode !== "managed") {
298
+ throw invalidConfig(`integrations[${index}] must be composioManaged({...}) or { type: "composio", provider: "composio", mode: "managed", ... }.`);
299
+ }
300
+
301
+ composioCount += 1;
302
+ validateComposioManagedIntegration(integration as unknown as AgentIntegration, index);
303
+ }
304
+
305
+ if (composioCount > 1) {
306
+ throw invalidConfig("Only one managed Composio integration is supported per agent.");
307
+ }
308
+
309
+ const hasExplicitComposioTools = value.some(
310
+ (integration) => isRecord(integration) && integration.provider === "composio" && isRecord(integration.tools),
311
+ );
312
+
313
+ if (hasExplicitComposioTools && tools.some((tool) => isRecord(tool) && tool.name === MANAGED_COMPOSIO_TOOL_NAME)) {
314
+ throw invalidConfig(`tools must not declare reserved managed Composio tool name "${MANAGED_COMPOSIO_TOOL_NAME}".`);
315
+ }
316
+ }
317
+
318
+ function validateComposioManagedIntegration(value: AgentIntegration, index: number): void {
319
+ expectString(value.name, `integrations[${index}].name`);
320
+
321
+ if (!INTEGRATION_NAME_PATTERN.test(value.name)) {
322
+ throw invalidConfig(`integrations[${index}].name must start with a lowercase letter and contain only lowercase letters, numbers, and hyphens.`);
323
+ }
324
+
325
+ if (!Array.isArray(value.toolkits) || value.toolkits.length === 0) {
326
+ throw invalidConfig(`integrations[${index}].toolkits must include at least one Composio toolkit slug.`);
327
+ }
328
+
329
+ const toolkits = new Set<string>();
330
+
331
+ for (const [toolkitIndex, toolkit] of value.toolkits.entries()) {
332
+ expectString(toolkit, `integrations[${index}].toolkits[${toolkitIndex}]`);
333
+
334
+ if (!COMPOSIO_TOOLKIT_PATTERN.test(toolkit)) {
335
+ throw invalidConfig(`integrations[${index}].toolkits[${toolkitIndex}] must be a lowercase Composio toolkit slug.`);
336
+ }
337
+
338
+ if (toolkits.has(toolkit)) {
339
+ throw invalidConfig(`integrations[${index}].toolkits must not contain duplicates.`);
340
+ }
341
+
342
+ toolkits.add(toolkit);
343
+ }
344
+
345
+ if (value.tools !== undefined) {
346
+ if (!isRecord(value.tools)) {
347
+ throw invalidConfig(`integrations[${index}].tools must be an object when provided.`);
348
+ }
349
+
350
+ for (const [toolkit, tools] of Object.entries(value.tools)) {
351
+ if (!toolkits.has(toolkit)) {
352
+ throw invalidConfig(`integrations[${index}].tools.${toolkit} must reference a toolkit listed in integrations[${index}].toolkits.`);
353
+ }
354
+
355
+ if (!Array.isArray(tools) || tools.length === 0) {
356
+ throw invalidConfig(`integrations[${index}].tools.${toolkit} must include at least one Composio action slug.`);
357
+ }
358
+
359
+ const seenTools = new Set<string>();
360
+
361
+ for (const [toolIndex, tool] of tools.entries()) {
362
+ expectString(tool, `integrations[${index}].tools.${toolkit}[${toolIndex}]`);
363
+
364
+ if (!COMPOSIO_TOOL_SLUG_PATTERN.test(tool)) {
365
+ throw invalidConfig(`integrations[${index}].tools.${toolkit}[${toolIndex}] must be an uppercase Composio action slug.`);
366
+ }
367
+
368
+ if (seenTools.has(tool)) {
369
+ throw invalidConfig(`integrations[${index}].tools.${toolkit} must not contain duplicates.`);
370
+ }
371
+
372
+ seenTools.add(tool);
373
+ }
374
+ }
375
+ }
376
+
377
+ if (value.tags !== undefined) {
378
+ validateIntegrationTags(value.tags, `integrations[${index}].tags`);
379
+ }
380
+
381
+ if (value.confirmExternalWrites !== undefined && typeof value.confirmExternalWrites !== "boolean") {
382
+ throw invalidConfig(`integrations[${index}].confirmExternalWrites must be a boolean when provided.`);
383
+ }
384
+ }
385
+
386
+ function validateIntegrationTags(value: unknown, label: string): void {
387
+ if (!isRecord(value)) {
388
+ throw invalidConfig(`${label} must be an object when provided.`);
389
+ }
390
+
391
+ if (value.enable !== undefined) {
392
+ expectStringArray(value.enable, `${label}.enable`);
393
+ }
394
+
395
+ if (value.disable !== undefined) {
396
+ expectStringArray(value.disable, `${label}.disable`);
397
+ }
398
+ }
399
+
400
+ function validateTranscriptionConfig(value: unknown): void {
401
+ if (value === undefined) {
402
+ return;
403
+ }
404
+
405
+ if (!isRecord(value)) {
406
+ throw invalidConfig("transcription must be an object when provided.");
407
+ }
408
+
409
+ if (!TRANSCRIPTION_PROVIDERS.has(value.provider as TranscriptionProviderName)) {
410
+ throw invalidConfig(`transcription.provider must be one of: ${Array.from(TRANSCRIPTION_PROVIDERS).join(", ")}.`);
411
+ }
412
+
413
+ expectString(value.model, "transcription.model");
414
+
415
+ if (value.secret !== undefined) {
416
+ expectString(value.secret, "transcription.secret");
417
+ validateSecretNames([value.secret], "transcription.secret");
418
+ }
419
+
420
+ if (value.language !== undefined) {
421
+ expectString(value.language, "transcription.language");
422
+ }
423
+
424
+ if (value.prompt !== undefined) {
425
+ expectString(value.prompt, "transcription.prompt");
426
+ }
427
+
428
+ if (value.limits !== undefined) {
429
+ if (!isRecord(value.limits)) {
430
+ throw invalidConfig("transcription.limits must be an object when provided.");
431
+ }
432
+
433
+ validateOptionalPositiveInteger(value.limits.maxDurationSeconds, "transcription.limits.maxDurationSeconds");
434
+ validateOptionalPositiveInteger(value.limits.maxBytes, "transcription.limits.maxBytes");
435
+ }
436
+
437
+ validateOptionalPositiveInteger(value.rawAudioTtlSeconds, "transcription.rawAudioTtlSeconds");
438
+ }
439
+
440
+ function validateKnowledgeConfig(value: unknown): void {
441
+ if (value === undefined) {
442
+ return;
443
+ }
444
+
445
+ if (!isRecord(value)) {
446
+ throw invalidConfig("knowledge must be an object when provided.");
447
+ }
448
+
449
+ if (value.sources !== undefined) {
450
+ if (!Array.isArray(value.sources)) {
451
+ throw invalidConfig("knowledge.sources must be an array when provided.");
452
+ }
453
+
454
+ for (const [index, source] of value.sources.entries()) {
455
+ validateKnowledgeSource(source, index);
456
+ }
457
+ }
458
+
459
+ if (value.embedding !== undefined) {
460
+ validateKnowledgeEmbedding(value.embedding);
461
+ }
462
+
463
+ if (value.retrieval !== undefined) {
464
+ validateKnowledgeRetrieval(value.retrieval);
465
+ }
466
+ }
467
+
468
+ function validateKnowledgeSource(value: unknown, index: number): void {
469
+ if (typeof value === "string") {
470
+ expectString(value, `knowledge.sources[${index}]`);
471
+ validateKnowledgeSourceReference(value, `knowledge.sources[${index}]`);
472
+ return;
473
+ }
474
+
475
+ if (!isRecord(value)) {
476
+ throw invalidConfig(`knowledge.sources[${index}] must be a path string or an object with path or url.`);
477
+ }
478
+
479
+ const hasPath = "path" in value;
480
+ const hasUrl = "url" in value;
481
+
482
+ if (hasPath === hasUrl) {
483
+ throw invalidConfig(`knowledge.sources[${index}] must set exactly one of path or url.`);
484
+ }
485
+
486
+ if (hasPath) {
487
+ expectString(value.path, `knowledge.sources[${index}].path`);
488
+ validateKnowledgeSourceReference(value.path, `knowledge.sources[${index}].path`);
489
+ }
490
+
491
+ if (hasUrl) {
492
+ expectString(value.url, `knowledge.sources[${index}].url`);
493
+ validateKnowledgeUrl(value.url, `knowledge.sources[${index}].url`);
494
+ }
495
+
496
+ if (value.title !== undefined) {
497
+ expectString(value.title, `knowledge.sources[${index}].title`);
498
+ }
499
+ }
500
+
501
+ function validateKnowledgeSourceReference(value: string, label: string): void {
502
+ if (value.includes("\0")) {
503
+ throw invalidConfig(`${label} must not contain null bytes.`);
504
+ }
505
+
506
+ if (/^https?:\/\//i.test(value)) {
507
+ validateKnowledgeUrl(value, label);
508
+ }
509
+ }
510
+
511
+ function validateKnowledgeUrl(value: string, label: string): void {
512
+ let url: URL;
513
+
514
+ try {
515
+ url = new URL(value);
516
+ } catch {
517
+ throw invalidConfig(`${label} must be a valid http or https URL.`);
518
+ }
519
+
520
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
521
+ throw invalidConfig(`${label} must be a valid http or https URL.`);
522
+ }
523
+ }
524
+
525
+ function validateKnowledgeEmbedding(value: unknown): void {
526
+ if (!isRecord(value)) {
527
+ throw invalidConfig("knowledge.embedding must be an object when provided.");
528
+ }
529
+
530
+ if (value.provider !== undefined && !KNOWLEDGE_EMBEDDING_PROVIDERS.has(value.provider as AgentKnowledgeEmbeddingProvider)) {
531
+ throw invalidConfig(
532
+ `knowledge.embedding.provider must be one of: ${Array.from(KNOWLEDGE_EMBEDDING_PROVIDERS).join(", ")}.`,
533
+ );
534
+ }
535
+
536
+ if (value.model !== undefined) {
537
+ expectString(value.model, "knowledge.embedding.model");
538
+ }
539
+
540
+ if (value.secret !== undefined) {
541
+ expectString(value.secret, "knowledge.embedding.secret");
542
+ validateSecretNames([value.secret], "knowledge.embedding.secret");
543
+ }
544
+
545
+ const dimensions = value.dimensions;
546
+ if (dimensions !== undefined) {
547
+ if (typeof dimensions !== "number" || !Number.isInteger(dimensions) || dimensions <= 0 || dimensions > 65_536) {
548
+ throw invalidConfig("knowledge.embedding.dimensions must be a positive integer no larger than 65536.");
549
+ }
550
+ }
551
+ }
552
+
553
+ function validateKnowledgeRetrieval(value: unknown): void {
554
+ if (!isRecord(value)) {
555
+ throw invalidConfig("knowledge.retrieval must be an object when provided.");
556
+ }
557
+
558
+ const topK = value.topK;
559
+ if (topK !== undefined && (typeof topK !== "number" || !Number.isInteger(topK) || topK <= 0 || topK > 50)) {
560
+ throw invalidConfig("knowledge.retrieval.topK must be a positive integer no larger than 50.");
561
+ }
562
+
563
+ if (value.hybrid !== undefined && typeof value.hybrid !== "boolean") {
564
+ throw invalidConfig("knowledge.retrieval.hybrid must be a boolean when provided.");
565
+ }
566
+ }
567
+
568
+ function validateChannels(value: unknown, runtime: AgentRuntime, hasTranscriptionConfig: boolean): void {
230
569
  if (value === undefined) {
231
570
  return;
232
571
  }
@@ -242,7 +581,7 @@ function validateChannels(value: unknown, runtime: AgentRuntime): void {
242
581
  const names = new Set<string>();
243
582
 
244
583
  for (const [index, channel] of value.entries()) {
245
- validateChannel(channel, index);
584
+ validateChannel(channel, index, hasTranscriptionConfig);
246
585
 
247
586
  if (names.has(channel.name)) {
248
587
  throw invalidConfig(`channels[${index}].name must be unique within the Agent Capsule.`);
@@ -252,7 +591,7 @@ function validateChannels(value: unknown, runtime: AgentRuntime): void {
252
591
  }
253
592
  }
254
593
 
255
- function validateChannel(value: unknown, index: number): asserts value is AgentChannel {
594
+ function validateChannel(value: unknown, index: number, hasTranscriptionConfig: boolean): asserts value is AgentChannel {
256
595
  if (!isRecord(value)) {
257
596
  throw invalidConfig(`channels[${index}] must be an object.`);
258
597
  }
@@ -276,6 +615,24 @@ function validateChannel(value: unknown, index: number): asserts value is AgentC
276
615
  expectStringArray(value.secrets, `channels[${index}].secrets`);
277
616
  validateSecretNames(value.secrets, `channels[${index}].secrets`);
278
617
  validateChannelLimits(value.limits, index);
618
+ validateChannelBuffer(value.buffer, index);
619
+
620
+ if (value.type === "discord") {
621
+ validateDiscordChannel(value, index);
622
+ return;
623
+ }
624
+
625
+ if (value.type === "slack") {
626
+ validateSlackChannel(value, index);
627
+ return;
628
+ }
629
+
630
+ if (value.type === "webhook") {
631
+ validateGenericWebhookChannel(value, index);
632
+ return;
633
+ }
634
+
635
+ validateChannelAudio(value.audio, index, hasTranscriptionConfig);
279
636
 
280
637
  if (value.type === "website") {
281
638
  validateWebsiteChannel(value, index);
@@ -290,6 +647,31 @@ function validateChannel(value: unknown, index: number): asserts value is AgentC
290
647
  validateWhatsappChannel(value, index);
291
648
  }
292
649
 
650
+ function validateChannelAudio(value: unknown, index: number, hasTranscriptionConfig: boolean): void {
651
+ if (value === undefined) {
652
+ return;
653
+ }
654
+
655
+ if (!isRecord(value)) {
656
+ throw invalidConfig(`channels[${index}].audio must be an object when provided.`);
657
+ }
658
+
659
+ if (!CHANNEL_AUDIO_MODES.has(value.mode as ChannelAudioMode)) {
660
+ throw invalidConfig(`channels[${index}].audio.mode must be "off", "reject", or "transcribe".`);
661
+ }
662
+
663
+ if (value.mode === "transcribe" && !hasTranscriptionConfig) {
664
+ throw invalidConfig(`channels[${index}].audio.mode "transcribe" requires top-level transcription config.`);
665
+ }
666
+
667
+ if (value.mode !== "off") {
668
+ expectString(value.fallbackMessage, `channels[${index}].audio.fallbackMessage`);
669
+ }
670
+
671
+ validateOptionalPositiveInteger(value.maxDurationSeconds, `channels[${index}].audio.maxDurationSeconds`);
672
+ validateOptionalPositiveInteger(value.maxBytes, `channels[${index}].audio.maxBytes`);
673
+ }
674
+
293
675
  function validateWebsiteChannel(value: Record<string, unknown>, index: number): void {
294
676
  if (value.provider !== "agentkit") {
295
677
  throw invalidConfig(`channels[${index}].provider must be "agentkit" when type is "website".`);
@@ -322,11 +704,15 @@ function validateTelegramChannel(value: Record<string, unknown>, index: number):
322
704
 
323
705
  function validateWhatsappChannel(value: Record<string, unknown>, index: number): void {
324
706
  if (value.type !== "whatsapp") {
325
- throw invalidConfig(`channels[${index}].type must be "website", "telegram", or "whatsapp".`);
707
+ throw invalidConfig(`channels[${index}].type must be "website", "telegram", "whatsapp", "discord", "slack", or "webhook".`);
326
708
  }
327
709
 
328
710
  if (value.provider === "zapster") {
329
- expectRequiredSecrets(value.secrets, ["ZAPSTER_API_KEY", "ZAPSTER_WEBHOOK_SECRET"], `channels[${index}]`);
711
+ expectRequiredSecrets(
712
+ value.secrets,
713
+ ["ZAPSTER_API_KEY", "ZAPSTER_INSTANCE_ID", "ZAPSTER_WEBHOOK_ID"],
714
+ `channels[${index}]`,
715
+ );
330
716
  return;
331
717
  }
332
718
 
@@ -339,7 +725,69 @@ function validateWhatsappChannel(value: Record<string, unknown>, index: number):
339
725
  return;
340
726
  }
341
727
 
342
- throw invalidConfig(`channels[${index}].provider must be "zapster" or "meta" when type is "whatsapp".`);
728
+ if (value.provider === "uazapi") {
729
+ expectRequiredSecrets(
730
+ value.secrets,
731
+ ["UAZAPI_BASE_URL", "UAZAPI_TOKEN"],
732
+ `channels[${index}]`,
733
+ );
734
+ return;
735
+ }
736
+
737
+ if (value.provider === "evolution") {
738
+ expectRequiredSecrets(
739
+ value.secrets,
740
+ ["EVOLUTION_API_BASE_URL", "EVOLUTION_API_KEY", "EVOLUTION_INSTANCE_NAME", "EVOLUTION_WEBHOOK_TOKEN"],
741
+ `channels[${index}]`,
742
+ );
743
+ return;
744
+ }
745
+
746
+ throw invalidConfig(`channels[${index}].provider must be "zapster", "meta", "uazapi", or "evolution" when type is "whatsapp".`);
747
+ }
748
+
749
+ function validateDiscordChannel(value: Record<string, unknown>, index: number): void {
750
+ if (value.provider !== "discord") {
751
+ throw invalidConfig(`channels[${index}].provider must be "discord" when type is "discord".`);
752
+ }
753
+
754
+ if (value.mode !== undefined && !DISCORD_CHANNEL_MODES.has(value.mode as DiscordChannelMode)) {
755
+ throw invalidConfig(`channels[${index}].mode must be "interactions" or "bot" when type is "discord".`);
756
+ }
757
+
758
+ if (value.audio !== undefined) {
759
+ throw invalidConfig(`channels[${index}].audio is not supported when type is "discord".`);
760
+ }
761
+
762
+ expectRequiredSecrets(
763
+ value.secrets,
764
+ value.mode === "bot" ? ["DISCORD_BOT_TOKEN"] : ["DISCORD_PUBLIC_KEY"],
765
+ `channels[${index}]`,
766
+ );
767
+ }
768
+
769
+ function validateSlackChannel(value: Record<string, unknown>, index: number): void {
770
+ if (value.provider !== "slack") {
771
+ throw invalidConfig(`channels[${index}].provider must be "slack" when type is "slack".`);
772
+ }
773
+
774
+ if (value.audio !== undefined) {
775
+ throw invalidConfig(`channels[${index}].audio is not supported when type is "slack".`);
776
+ }
777
+
778
+ expectRequiredSecrets(value.secrets, ["SLACK_BOT_TOKEN", "SLACK_SIGNING_SECRET"], `channels[${index}]`);
779
+ }
780
+
781
+ function validateGenericWebhookChannel(value: Record<string, unknown>, index: number): void {
782
+ if (value.provider !== "generic") {
783
+ throw invalidConfig(`channels[${index}].provider must be "generic" when type is "webhook".`);
784
+ }
785
+
786
+ if (value.audio !== undefined) {
787
+ throw invalidConfig(`channels[${index}].audio is not supported when type is "webhook".`);
788
+ }
789
+
790
+ expectRequiredSecrets(value.secrets, ["AGENTKIT_WEBHOOK_SECRET"], `channels[${index}]`);
343
791
  }
344
792
 
345
793
  function validateChannelLimits(value: unknown, index: number): void {
@@ -361,6 +809,39 @@ function validateChannelLimits(value: unknown, index: number): void {
361
809
  }
362
810
  }
363
811
 
812
+ function validateChannelBuffer(value: unknown, index: number): void {
813
+ if (value === undefined) {
814
+ return;
815
+ }
816
+
817
+ if (!isRecord(value)) {
818
+ throw invalidConfig(`channels[${index}].buffer must be an object when provided.`);
819
+ }
820
+
821
+ if (!CHANNEL_BUFFER_MODES.has(value.mode as ChannelBufferMode)) {
822
+ throw invalidConfig(`channels[${index}].buffer.mode must be "off" or "debounce".`);
823
+ }
824
+
825
+ if (value.mode === "off") {
826
+ return;
827
+ }
828
+
829
+ for (const field of ["quietWindowMs", "maxWaitMs", "maxMessages", "maxChars"] as const) {
830
+ if (
831
+ value[field] === undefined ||
832
+ typeof value[field] !== "number" ||
833
+ !Number.isInteger(value[field]) ||
834
+ value[field] <= 0
835
+ ) {
836
+ throw invalidConfig(`channels[${index}].buffer.${field} must be a positive integer.`);
837
+ }
838
+ }
839
+
840
+ if ((value.maxWaitMs as number) < (value.quietWindowMs as number)) {
841
+ throw invalidConfig(`channels[${index}].buffer.maxWaitMs must be greater than or equal to quietWindowMs.`);
842
+ }
843
+ }
844
+
364
845
  function validateSecretNames(secrets: string[], field: string): void {
365
846
  for (const secret of secrets) {
366
847
  if (!SECRET_NAME_PATTERN.test(secret)) {
@@ -369,6 +850,16 @@ function validateSecretNames(secrets: string[], field: string): void {
369
850
  }
370
851
  }
371
852
 
853
+ function validateOptionalPositiveInteger(value: unknown, field: string): void {
854
+ if (value === undefined) {
855
+ return;
856
+ }
857
+
858
+ if (typeof value !== "number" || !Number.isInteger(value) || value <= 0) {
859
+ throw invalidConfig(`${field} must be a positive integer when provided.`);
860
+ }
861
+ }
862
+
372
863
  function expectRequiredSecrets(value: unknown, requiredSecrets: string[], field: string): void {
373
864
  const declaredSecrets = new Set(Array.isArray(value) ? value : []);
374
865
  const missingSecrets = requiredSecrets.filter((secret) => !declaredSecrets.has(secret));
@@ -394,6 +885,10 @@ function validateDatabaseConfig(value: unknown): void {
394
885
  if (value.schema !== undefined && typeof value.schema !== "string") {
395
886
  throw invalidConfig("storage.database.schema must be a string when provided.");
396
887
  }
888
+
889
+ if (value.migrations !== undefined && typeof value.migrations !== "string") {
890
+ throw invalidConfig("storage.database.migrations must be a string when provided.");
891
+ }
397
892
  }
398
893
 
399
894
  function validateFilesConfig(value: unknown): void {
@@ -449,6 +944,7 @@ function validateTool(value: unknown, index: number): asserts value is AgentTool
449
944
 
450
945
  if (value.secrets !== undefined) {
451
946
  expectStringArray(value.secrets, `tools[${index}].secrets`);
947
+ validateSecretNames(value.secrets, `tools[${index}].secrets`);
452
948
  }
453
949
 
454
950
  if (value.permissions !== undefined) {
@@ -464,6 +960,10 @@ function validateTool(value: unknown, index: number): asserts value is AgentTool
464
960
  if (typeof value.execute !== "function") {
465
961
  throw invalidConfig(`tools[${index}].execute must be a function.`);
466
962
  }
963
+
964
+ if (value.render !== undefined && typeof value.render !== "function") {
965
+ throw invalidConfig(`tools[${index}].render must be a function when provided.`);
966
+ }
467
967
  }
468
968
 
469
969
  function resolvePath(root: string, path: string): string {