@andreprado/agentkit 0.1.0-alpha.14 → 0.1.0-alpha.16

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 (56) hide show
  1. package/README.md +2 -0
  2. package/docs/guides/add-channel.md +63 -0
  3. package/docs/guides/channel-security.md +32 -0
  4. package/docs/guides/connect-telegram.md +58 -0
  5. package/docs/guides/connect-whatsapp-zapster.md +65 -0
  6. package/docs/guides/run-evals.md +73 -25
  7. package/docs/llms-full.txt +24 -9
  8. package/docs/llms.txt +2 -0
  9. package/package.json +1 -1
  10. package/src/cli/cloud-client.ts +30 -10
  11. package/src/cli/commands/channels.ts +2 -0
  12. package/src/cli/deploy-readiness.ts +32 -11
  13. package/src/cli/index.ts +20 -6
  14. package/src/cloud/client.ts +4 -3
  15. package/src/cloud/contracts.ts +1 -1
  16. package/src/create-project.ts +1 -1
  17. package/src/index.ts +110 -1
  18. package/src/providers/pi.ts +14 -1
  19. package/src/providers/test.ts +36 -0
  20. package/src/runtime/channel-test-harness.ts +2 -0
  21. package/src/runtime/channels/telegram.ts +326 -10
  22. package/src/runtime/channels/whatsapp-zapster.ts +319 -0
  23. package/src/runtime/channels.ts +47 -1
  24. package/src/runtime/chat.ts +59 -42
  25. package/src/runtime/config.ts +96 -4
  26. package/src/runtime/core/manifest.ts +35 -3
  27. package/src/runtime/deploy-readiness.ts +3 -3
  28. package/src/runtime/dev-server.ts +243 -17
  29. package/src/runtime/env.ts +8 -3
  30. package/src/runtime/evals.ts +404 -69
  31. package/src/runtime/inspect.ts +46 -0
  32. package/src/runtime/prompt-context.ts +141 -0
  33. package/src/runtime/runtime-contract.ts +17 -7
  34. package/src/runtime/targets/cloudflare/build.ts +25 -3
  35. package/src/runtime/targets/container/server.ts +1 -1
  36. package/src/runtime/targets/vps/deploy.ts +25 -8
  37. package/src/runtime/tool-runner.ts +7 -0
  38. package/src/runtime/tools.ts +8 -2
  39. package/src/runtime/transcription.ts +483 -0
  40. package/src/templates/blank.ts +8 -3
  41. package/src/templates/dentista.ts +18 -10
  42. package/src/templates/skills/agentkit-build-agent/SKILL.md +6 -5
  43. package/src/templates/skills/agentkit-build-agent/templates/appointment-intake.instructions.md +2 -1
  44. package/src/templates/skills/agentkit-capsule/SKILL.md +1 -1
  45. package/src/templates/skills/agentkit-channels/SKILL.md +34 -1
  46. package/src/templates/skills/agentkit-channels/references/channel-debugging.md +13 -0
  47. package/src/templates/skills/agentkit-channels/references/telegram.md +32 -0
  48. package/src/templates/skills/agentkit-channels/references/whatsapp-zapster.md +29 -0
  49. package/src/templates/skills/agentkit-evals/SKILL.md +53 -13
  50. package/src/templates/skills/agentkit-evals/templates/multi-turn.eval.md +13 -6
  51. package/src/templates/skills/agentkit-evals/templates/no-leak.eval.md +8 -4
  52. package/src/templates/skills/agentkit-evals/templates/smoke.eval.md +8 -4
  53. package/src/templates/skills/agentkit-evals/templates/tool-call.eval.md +16 -7
  54. package/src/templates/skills/agentkit-prompts/SKILL.md +3 -1
  55. package/src/templates/skills/agentkit-tools/SKILL.md +2 -1
  56. package/src/templates/support.ts +8 -3
@@ -1,5 +1,7 @@
1
1
  import type {
2
2
  ChannelAdapter,
3
+ ChannelMediaDownloadInput,
4
+ ChannelMediaDownloadResult,
3
5
  ChannelSendInput,
4
6
  ChannelStatusResult,
5
7
  RawWebhookEvent,
@@ -86,13 +88,37 @@ export const telegramChannelAdapter: ChannelAdapter = {
86
88
 
87
89
  const text = readString(message, "text");
88
90
 
89
- if (!text) {
91
+ if (text) {
90
92
  return [
91
93
  {
92
94
  ...base,
93
- kind: "unsupported",
95
+ kind: "message",
96
+ externalIdentity: identity,
97
+ message: {
98
+ role: "user",
99
+ contentType: "text",
100
+ content: text,
101
+ providerMessageId: readNumberishString(message, "message_id"),
102
+ },
103
+ },
104
+ ];
105
+ }
106
+
107
+ const audio = normalizeTelegramAudio(message);
108
+
109
+ if (audio) {
110
+ return [
111
+ {
112
+ ...base,
113
+ kind: "message",
94
114
  externalIdentity: identity,
95
- unsupportedReason: "unsupported_message_type:non_text",
115
+ message: {
116
+ role: "user",
117
+ contentType: "audio",
118
+ content: "",
119
+ providerMessageId: readNumberishString(message, "message_id"),
120
+ audio,
121
+ },
96
122
  },
97
123
  ];
98
124
  }
@@ -100,17 +126,15 @@ export const telegramChannelAdapter: ChannelAdapter = {
100
126
  return [
101
127
  {
102
128
  ...base,
103
- kind: "message",
129
+ kind: "unsupported",
104
130
  externalIdentity: identity,
105
- message: {
106
- role: "user",
107
- contentType: "text",
108
- content: text,
109
- providerMessageId: readNumberishString(message, "message_id"),
110
- },
131
+ unsupportedReason: "unsupported_message_type:non_text",
111
132
  },
112
133
  ];
113
134
  },
135
+ async downloadMedia(input) {
136
+ return downloadTelegramMedia(input);
137
+ },
114
138
  async sendMessage(input) {
115
139
  return sendTelegramMessage(input);
116
140
  },
@@ -142,6 +166,7 @@ export type TelegramSendMessageRequest = {
142
166
  };
143
167
 
144
168
  type TelegramFetch = (url: string, init: RequestInit) => Promise<Response>;
169
+ const TELEGRAM_MAX_DOWNLOAD_BYTES = 20_000_000;
145
170
 
146
171
  export function buildTelegramSendMessageRequest(input: ChannelSendInput): TelegramSendMessageRequest {
147
172
  return buildTelegramSendMessageRequestInternal(input, "<redacted>");
@@ -275,6 +300,149 @@ export async function sendTelegramMessage(
275
300
  };
276
301
  }
277
302
 
303
+ export async function downloadTelegramMedia(
304
+ input: ChannelMediaDownloadInput,
305
+ fetcher: TelegramFetch = fetch,
306
+ ): Promise<ChannelMediaDownloadResult> {
307
+ const token = input.secrets.TELEGRAM_BOT_TOKEN;
308
+
309
+ if (!token) {
310
+ return {
311
+ ok: false,
312
+ retryable: false,
313
+ code: "channel_secret_missing",
314
+ message: "Secret TELEGRAM_BOT_TOKEN is not set for this channel.",
315
+ };
316
+ }
317
+
318
+ const audio = input.message.audio;
319
+
320
+ if (!audio?.providerMediaId) {
321
+ return {
322
+ ok: false,
323
+ retryable: false,
324
+ code: "channel_audio_download_unavailable",
325
+ message: "Telegram audio message is missing file_id.",
326
+ };
327
+ }
328
+
329
+ let fileResponse: Response;
330
+
331
+ try {
332
+ const url = new URL(`/bot${token}/getFile`, telegramApiBaseUrl(input.secrets));
333
+ url.searchParams.set("file_id", audio.providerMediaId);
334
+ fileResponse = await fetcher(url.href, { method: "GET" });
335
+ } catch (error) {
336
+ return {
337
+ ok: false,
338
+ retryable: true,
339
+ code: "channel_provider_unavailable",
340
+ message: `Telegram getFile request failed before a response: ${redactToken(
341
+ error instanceof Error ? error.message : String(error),
342
+ token,
343
+ )}`,
344
+ };
345
+ }
346
+
347
+ const filePayload = await fileResponse.json().catch(() => null);
348
+
349
+ if (!fileResponse.ok || !isRecord(filePayload) || filePayload.ok !== true || !isRecord(filePayload.result)) {
350
+ const description =
351
+ isRecord(filePayload) && typeof filePayload.description === "string"
352
+ ? filePayload.description
353
+ : `Telegram getFile returned HTTP ${fileResponse.status}.`;
354
+
355
+ return {
356
+ ok: false,
357
+ retryable: fileResponse.status === 429 || fileResponse.status >= 500,
358
+ code: fileResponse.status === 429 || fileResponse.status >= 500
359
+ ? "channel_provider_unavailable"
360
+ : "channel_media_download_failed",
361
+ message: redactToken(description, token),
362
+ providerMetadata: {
363
+ status: fileResponse.status,
364
+ },
365
+ };
366
+ }
367
+
368
+ const filePath = readString(filePayload.result, "file_path");
369
+
370
+ if (!filePath) {
371
+ return {
372
+ ok: false,
373
+ retryable: false,
374
+ code: "channel_media_download_failed",
375
+ message: "Telegram getFile did not return result.file_path.",
376
+ providerMetadata: {
377
+ status: fileResponse.status,
378
+ },
379
+ };
380
+ }
381
+
382
+ const maxDownloadBytes = Math.min(input.maxBytes ?? TELEGRAM_MAX_DOWNLOAD_BYTES, TELEGRAM_MAX_DOWNLOAD_BYTES);
383
+ const fileSize = readNumber(filePayload.result, "file_size") ?? audio.sizeBytes;
384
+
385
+ if (fileSize !== undefined && fileSize > maxDownloadBytes) {
386
+ return {
387
+ ok: false,
388
+ retryable: false,
389
+ code: "channel_audio_too_large",
390
+ message: `Telegram audio is ${fileSize} bytes, which exceeds the configured ${maxDownloadBytes} byte limit.`,
391
+ providerMetadata: {
392
+ sizeBytes: fileSize,
393
+ },
394
+ };
395
+ }
396
+
397
+ let mediaResponse: Response;
398
+
399
+ try {
400
+ mediaResponse = await fetcher(`${telegramApiBaseUrl(input.secrets)}/file/bot${token}/${filePath}`, { method: "GET" });
401
+ } catch (error) {
402
+ return {
403
+ ok: false,
404
+ retryable: true,
405
+ code: "channel_provider_unavailable",
406
+ message: `Telegram file download failed before a response: ${redactToken(
407
+ error instanceof Error ? error.message : String(error),
408
+ token,
409
+ )}`,
410
+ };
411
+ }
412
+
413
+ if (!mediaResponse.ok) {
414
+ return {
415
+ ok: false,
416
+ retryable: mediaResponse.status === 429 || mediaResponse.status >= 500,
417
+ code: mediaResponse.status === 429 || mediaResponse.status >= 500
418
+ ? "channel_provider_unavailable"
419
+ : "channel_media_download_failed",
420
+ message: `Telegram file download returned HTTP ${mediaResponse.status}.`,
421
+ providerMetadata: {
422
+ status: mediaResponse.status,
423
+ },
424
+ };
425
+ }
426
+
427
+ const bytes = await readResponseBytesWithLimit(mediaResponse, maxDownloadBytes);
428
+
429
+ if (!bytes.ok) {
430
+ return bytes;
431
+ }
432
+
433
+ return {
434
+ ok: true,
435
+ audio: bytes.audio,
436
+ filename: audio.filename ?? basename(filePath) ?? `${audio.providerMediaId}.ogg`,
437
+ ...(audio.mimeType ? { mimeType: audio.mimeType } : {}),
438
+ ...(audio.durationSeconds !== undefined ? { durationSeconds: audio.durationSeconds } : {}),
439
+ sizeBytes: bytes.audio.byteLength,
440
+ providerMetadata: {
441
+ fileSize: fileSize ?? bytes.audio.byteLength,
442
+ },
443
+ };
444
+ }
445
+
278
446
  function isSyntheticTelegramChatId(chatId: string): boolean {
279
447
  return chatId === "456" || chatId.startsWith("agentkit-synthetic-");
280
448
  }
@@ -297,6 +465,48 @@ function buildTelegramSendMessageRequestInternal(input: ChannelSendInput, token:
297
465
  };
298
466
  }
299
467
 
468
+ function normalizeTelegramAudio(message: Record<string, unknown>) {
469
+ const voice = readRecord(message, "voice");
470
+
471
+ if (Object.keys(voice).length > 0) {
472
+ const fileId = readString(voice, "file_id");
473
+
474
+ if (!fileId) {
475
+ return null;
476
+ }
477
+
478
+ return {
479
+ providerMediaId: fileId,
480
+ kind: "voice" as const,
481
+ mimeType: readString(voice, "mime_type") ?? "audio/ogg",
482
+ filename: `${fileId}.ogg`,
483
+ ...(readNumber(voice, "duration") !== undefined ? { durationSeconds: readNumber(voice, "duration") } : {}),
484
+ ...(readNumber(voice, "file_size") !== undefined ? { sizeBytes: readNumber(voice, "file_size") } : {}),
485
+ };
486
+ }
487
+
488
+ const audio = readRecord(message, "audio");
489
+
490
+ if (Object.keys(audio).length > 0) {
491
+ const fileId = readString(audio, "file_id");
492
+
493
+ if (!fileId) {
494
+ return null;
495
+ }
496
+
497
+ return {
498
+ providerMediaId: fileId,
499
+ kind: "audio" as const,
500
+ ...(readString(audio, "mime_type") ? { mimeType: readString(audio, "mime_type") } : {}),
501
+ filename: readString(audio, "file_name") ?? `${fileId}.mp3`,
502
+ ...(readNumber(audio, "duration") !== undefined ? { durationSeconds: readNumber(audio, "duration") } : {}),
503
+ ...(readNumber(audio, "file_size") !== undefined ? { sizeBytes: readNumber(audio, "file_size") } : {}),
504
+ };
505
+ }
506
+
507
+ return null;
508
+ }
509
+
300
510
  export function parseTelegramWebhookInfo(input: unknown, checkedAt: string): ChannelStatusResult {
301
511
  if (!isRecord(input) || input.ok !== true || !isRecord(input.result)) {
302
512
  return {
@@ -362,6 +572,112 @@ function readNumberishString(value: Record<string, unknown>, key: string): strin
362
572
  return undefined;
363
573
  }
364
574
 
575
+ function telegramApiBaseUrl(env: Record<string, string | undefined>): string {
576
+ return env.AGENTKIT_TELEGRAM_API_BASE_URL ?? "https://api.telegram.org";
577
+ }
578
+
579
+ function basename(path: string): string | null {
580
+ const name = path.split("/").filter(Boolean).at(-1);
581
+ return name && name.length > 0 ? name : null;
582
+ }
583
+
584
+ async function readResponseBytesWithLimit(
585
+ response: Response,
586
+ maxBytes: number,
587
+ ): Promise<
588
+ | {
589
+ ok: true;
590
+ audio: Uint8Array;
591
+ }
592
+ | {
593
+ ok: false;
594
+ retryable: false;
595
+ code: "channel_audio_too_large";
596
+ message: string;
597
+ providerMetadata?: Record<string, unknown>;
598
+ }
599
+ > {
600
+ const contentLength = response.headers.get("content-length");
601
+ const declaredBytes = contentLength ? Number(contentLength) : undefined;
602
+
603
+ if (declaredBytes !== undefined && Number.isFinite(declaredBytes) && declaredBytes > maxBytes) {
604
+ return {
605
+ ok: false,
606
+ retryable: false,
607
+ code: "channel_audio_too_large",
608
+ message: `Audio response declares ${declaredBytes} bytes, which exceeds the configured ${maxBytes} byte limit.`,
609
+ providerMetadata: {
610
+ sizeBytes: declaredBytes,
611
+ },
612
+ };
613
+ }
614
+
615
+ if (!response.body) {
616
+ const audio = new Uint8Array(await response.arrayBuffer());
617
+
618
+ if (audio.byteLength > maxBytes) {
619
+ return {
620
+ ok: false,
621
+ retryable: false,
622
+ code: "channel_audio_too_large",
623
+ message: `Audio response is ${audio.byteLength} bytes, which exceeds the configured ${maxBytes} byte limit.`,
624
+ providerMetadata: {
625
+ sizeBytes: audio.byteLength,
626
+ },
627
+ };
628
+ }
629
+
630
+ return { ok: true, audio };
631
+ }
632
+
633
+ const reader = response.body.getReader();
634
+ const chunks: Uint8Array[] = [];
635
+ let totalBytes = 0;
636
+
637
+ try {
638
+ while (true) {
639
+ const chunk = await reader.read();
640
+
641
+ if (chunk.done) {
642
+ break;
643
+ }
644
+
645
+ totalBytes += chunk.value.byteLength;
646
+
647
+ if (totalBytes > maxBytes) {
648
+ await reader.cancel().catch(() => undefined);
649
+ return {
650
+ ok: false,
651
+ retryable: false,
652
+ code: "channel_audio_too_large",
653
+ message: `Audio response exceeded the configured ${maxBytes} byte limit while downloading.`,
654
+ providerMetadata: {
655
+ sizeBytes: totalBytes,
656
+ },
657
+ };
658
+ }
659
+
660
+ chunks.push(chunk.value);
661
+ }
662
+ } finally {
663
+ reader.releaseLock();
664
+ }
665
+
666
+ const audio = new Uint8Array(totalBytes);
667
+ let offset = 0;
668
+
669
+ for (const chunk of chunks) {
670
+ audio.set(chunk, offset);
671
+ offset += chunk.byteLength;
672
+ }
673
+
674
+ return { ok: true, audio };
675
+ }
676
+
677
+ function redactToken(value: string, token: string): string {
678
+ return token ? value.replaceAll(token, "<redacted>") : value;
679
+ }
680
+
365
681
  function redactId(value: string): string {
366
682
  if (value.length <= 2) {
367
683
  return "*".repeat(value.length);