stitchkit 0.123.5 → 0.123.7

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 (66) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/dist/entrypoints/application/diagnostic-journal.js +1 -1
  3. package/dist/entrypoints/application/directory-inbox.js +3 -3
  4. package/dist/entrypoints/cli/publish.js +2 -2
  5. package/dist/entrypoints/cli.js +2 -1
  6. package/dist/entrypoints/files.js +2 -2
  7. package/dist/entrypoints/node.js +2 -2
  8. package/dist/entrypoints/server.js +5 -5
  9. package/dist/entrypoints/telegram/grammy.d.ts +1 -0
  10. package/dist/entrypoints/telegram/grammy.d.ts.map +1 -1
  11. package/dist/entrypoints/telegram/grammy.js +81 -3
  12. package/dist/entrypoints/telegram/screens.js +1 -1
  13. package/dist/entrypoints/telegram.d.ts +1 -0
  14. package/dist/entrypoints/telegram.d.ts.map +1 -1
  15. package/dist/entrypoints/telegram.js +18 -6
  16. package/dist/entrypoints/tools/contract.js +4 -2
  17. package/dist/entrypoints/tools/toolkit.js +2 -1
  18. package/dist/entrypoints/tools.js +33 -27
  19. package/dist/{index-xjb131sc.js → index-1a741dpf.js} +188 -51
  20. package/dist/{index-yfpt7sxr.js → index-6qrspvxp.js} +5 -3
  21. package/dist/index-81t0bf0f.js +18 -0
  22. package/dist/{index-d9cg0gen.js → index-8yky60kq.js} +1 -1
  23. package/dist/{index-bph35vpy.js → index-9cxxfkhb.js} +21 -1
  24. package/dist/index-bgj9hbm3.js +93 -0
  25. package/dist/index-bknzva6n.js +39 -0
  26. package/dist/{index-73dappvg.js → index-ezwvsqx9.js} +1 -1
  27. package/dist/{index-303qne11.js → index-f56ct5gb.js} +27 -4
  28. package/dist/{index-chz7c830.js → index-mqttte96.js} +39 -6
  29. package/dist/{index-eabx6qjh.js → index-tnnmekgb.js} +1 -6
  30. package/dist/index-zs8cze4j.js +6 -0
  31. package/dist/internal/exclusive-lock.d.ts.map +1 -1
  32. package/dist/internal/media-extension.d.ts +3 -0
  33. package/dist/internal/media-extension.d.ts.map +1 -0
  34. package/dist/telegram/send-failure.d.ts +2 -2
  35. package/dist/telegram/send-failure.d.ts.map +1 -1
  36. package/dist/telegram/widget-api.d.ts +106 -0
  37. package/dist/telegram/widget-api.d.ts.map +1 -0
  38. package/dist/telegram/widget.d.ts +121 -0
  39. package/dist/telegram/widget.d.ts.map +1 -0
  40. package/dist/tools/cli/args.d.ts +1 -1
  41. package/dist/tools/cli/args.d.ts.map +1 -1
  42. package/dist/tools/cli/command.d.ts +7 -1
  43. package/dist/tools/cli/command.d.ts.map +1 -1
  44. package/dist/tools/cli/config.d.ts +5 -2
  45. package/dist/tools/cli/config.d.ts.map +1 -1
  46. package/dist/tools/cli/download.d.ts +25 -4
  47. package/dist/tools/cli/download.d.ts.map +1 -1
  48. package/dist/tools/cli/inline-files.d.ts +70 -0
  49. package/dist/tools/cli/inline-files.d.ts.map +1 -0
  50. package/dist/tools/cli/session.d.ts.map +1 -1
  51. package/dist/tools/transfer/download-core.d.ts +2 -0
  52. package/dist/tools/transfer/download-core.d.ts.map +1 -1
  53. package/dist/tools/transfer/view-file-contract.d.ts +34 -4
  54. package/dist/tools/transfer/view-file-contract.d.ts.map +1 -1
  55. package/dist/tools/transfer/view-file.d.ts.map +1 -1
  56. package/llms/cli.txt +63 -3
  57. package/llms/stitchkit.txt +2 -2
  58. package/llms/telegram-grammy.txt +10 -3
  59. package/llms/telegram.2.txt +5 -0
  60. package/llms/telegram.txt +70 -0
  61. package/llms/tools.2.txt +5 -0
  62. package/llms-full.txt +152 -8
  63. package/llms.txt +6 -6
  64. package/package.json +1 -1
  65. package/dist/index-6j725ez5.js +0 -25
  66. package/dist/index-sba3e6m4.js +0 -52
@@ -27,14 +27,40 @@ export declare const McpAnnotationsSchema: z.ZodObject<{
27
27
  priority: z.ZodOptional<z.ZodNumber>;
28
28
  }, z.core.$strip>;
29
29
  export type McpAnnotations = z.infer<typeof McpAnnotationsSchema>;
30
+ export declare const McpImageContentSchema: z.ZodObject<{
31
+ data: z.ZodString;
32
+ mimeType: z.ZodString;
33
+ name: z.ZodOptional<z.ZodString>;
34
+ annotations: z.ZodOptional<z.ZodObject<{
35
+ audience: z.ZodOptional<z.ZodArray<z.ZodEnum<{
36
+ assistant: "assistant";
37
+ user: "user";
38
+ }>>>;
39
+ priority: z.ZodOptional<z.ZodNumber>;
40
+ }, z.core.$strip>>;
41
+ type: z.ZodLiteral<"image">;
42
+ }, z.core.$strip>;
43
+ export declare const McpAudioContentSchema: z.ZodObject<{
44
+ data: z.ZodString;
45
+ mimeType: z.ZodString;
46
+ name: z.ZodOptional<z.ZodString>;
47
+ annotations: z.ZodOptional<z.ZodObject<{
48
+ audience: z.ZodOptional<z.ZodArray<z.ZodEnum<{
49
+ assistant: "assistant";
50
+ user: "user";
51
+ }>>>;
52
+ priority: z.ZodOptional<z.ZodNumber>;
53
+ }, z.core.$strip>>;
54
+ type: z.ZodLiteral<"audio">;
55
+ }, z.core.$strip>;
30
56
  /** An MCP content block — text / image / audio (video cannot be inlined). */
31
57
  export declare const McpMediaContentSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
32
58
  type: z.ZodLiteral<"text">;
33
59
  text: z.ZodString;
34
60
  }, z.core.$strip>, z.ZodObject<{
35
- type: z.ZodLiteral<"image">;
36
61
  data: z.ZodString;
37
62
  mimeType: z.ZodString;
63
+ name: z.ZodOptional<z.ZodString>;
38
64
  annotations: z.ZodOptional<z.ZodObject<{
39
65
  audience: z.ZodOptional<z.ZodArray<z.ZodEnum<{
40
66
  assistant: "assistant";
@@ -42,10 +68,11 @@ export declare const McpMediaContentSchema: z.ZodDiscriminatedUnion<[z.ZodObject
42
68
  }>>>;
43
69
  priority: z.ZodOptional<z.ZodNumber>;
44
70
  }, z.core.$strip>>;
71
+ type: z.ZodLiteral<"image">;
45
72
  }, z.core.$strip>, z.ZodObject<{
46
- type: z.ZodLiteral<"audio">;
47
73
  data: z.ZodString;
48
74
  mimeType: z.ZodString;
75
+ name: z.ZodOptional<z.ZodString>;
49
76
  annotations: z.ZodOptional<z.ZodObject<{
50
77
  audience: z.ZodOptional<z.ZodArray<z.ZodEnum<{
51
78
  assistant: "assistant";
@@ -53,6 +80,7 @@ export declare const McpMediaContentSchema: z.ZodDiscriminatedUnion<[z.ZodObject
53
80
  }>>>;
54
81
  priority: z.ZodOptional<z.ZodNumber>;
55
82
  }, z.core.$strip>>;
83
+ type: z.ZodLiteral<"audio">;
56
84
  }, z.core.$strip>], "type">;
57
85
  export type McpMediaContent = z.infer<typeof McpMediaContentSchema>;
58
86
  export declare const ViewFileErrorSchema: z.ZodObject<{
@@ -64,9 +92,9 @@ export declare const ViewFileOutputSchema: z.ZodObject<{
64
92
  type: z.ZodLiteral<"text">;
65
93
  text: z.ZodString;
66
94
  }, z.core.$strip>, z.ZodObject<{
67
- type: z.ZodLiteral<"image">;
68
95
  data: z.ZodString;
69
96
  mimeType: z.ZodString;
97
+ name: z.ZodOptional<z.ZodString>;
70
98
  annotations: z.ZodOptional<z.ZodObject<{
71
99
  audience: z.ZodOptional<z.ZodArray<z.ZodEnum<{
72
100
  assistant: "assistant";
@@ -74,10 +102,11 @@ export declare const ViewFileOutputSchema: z.ZodObject<{
74
102
  }>>>;
75
103
  priority: z.ZodOptional<z.ZodNumber>;
76
104
  }, z.core.$strip>>;
105
+ type: z.ZodLiteral<"image">;
77
106
  }, z.core.$strip>, z.ZodObject<{
78
- type: z.ZodLiteral<"audio">;
79
107
  data: z.ZodString;
80
108
  mimeType: z.ZodString;
109
+ name: z.ZodOptional<z.ZodString>;
81
110
  annotations: z.ZodOptional<z.ZodObject<{
82
111
  audience: z.ZodOptional<z.ZodArray<z.ZodEnum<{
83
112
  assistant: "assistant";
@@ -85,6 +114,7 @@ export declare const ViewFileOutputSchema: z.ZodObject<{
85
114
  }>>>;
86
115
  priority: z.ZodOptional<z.ZodNumber>;
87
116
  }, z.core.$strip>>;
117
+ type: z.ZodLiteral<"audio">;
88
118
  }, z.core.$strip>], "type">>;
89
119
  errors: z.ZodArray<z.ZodObject<{
90
120
  path: z.ZodString;
@@ -1 +1 @@
1
- {"version":3,"file":"view-file-contract.d.ts","sourceRoot":"","sources":["../../../src/tools/transfer/view-file-contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,qFAAqF;AACrF,eAAO,MAAM,cAAc,KAAK,CAAC;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB;;;;;;iBAG/B,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAElE,6EAA6E;AAC7E,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;2BAchC,CAAC;AAEH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEpE,eAAO,MAAM,mBAAmB;;;iBAG9B,CAAC;AAEH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAG/B,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAElE,eAAO,MAAM,mBAAmB;;iBAM9B,CAAC"}
1
+ {"version":3,"file":"view-file-contract.d.ts","sourceRoot":"","sources":["../../../src/tools/transfer/view-file-contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,qFAAqF;AACrF,eAAO,MAAM,cAAc,KAAK,CAAC;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB;;;;;;iBAG/B,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAclE,eAAO,MAAM,qBAAqB;;;;;;;;;;;;iBAGhC,CAAC;AACH,eAAO,MAAM,qBAAqB;;;;;;;;;;;;iBAGhC,CAAC;AAEH,6EAA6E;AAC7E,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;2BAIhC,CAAC;AAEH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEpE,eAAO,MAAM,mBAAmB;;;iBAG9B,CAAC;AAEH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAG/B,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAElE,eAAO,MAAM,mBAAmB;;iBAM9B,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"view-file.d.ts","sourceRoot":"","sources":["../../../src/tools/transfer/view-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAAE,mBAAmB,EAAwB,MAAM,sBAAsB,CAAC;AAEjF,OAAO,EACL,KAAK,cAAc,EACnB,oBAAoB,EACpB,KAAK,eAAe,EACpB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,KAAK,cAAc,EACnB,oBAAoB,GACrB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,KAAK,mBAAmB,EAAoB,MAAM,sBAAsB,CAAC;AA0BlF,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,KAAK,CAAC,EAAE,mBAAmB,CAAC;IAC5B;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAwKD;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,eAAe,EAAE,CAAC,CAE5B;AAED;;;;GAIG;AACH,wBAAsB,oBAAoB,CACxC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,mBAAmB,CAAC,CAAC,OAAO,CAAC,EACpD,OAAO,GAAE,eAAoB,EAC7B,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,cAAc,CAAC,CAuBzB;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,GAAE,eAAoB,GAAG,IAAI,CAyBpF"}
1
+ {"version":3,"file":"view-file.d.ts","sourceRoot":"","sources":["../../../src/tools/transfer/view-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAAE,mBAAmB,EAAwB,MAAM,sBAAsB,CAAC;AAEjF,OAAO,EACL,KAAK,cAAc,EACnB,oBAAoB,EACpB,KAAK,eAAe,EACpB,qBAAqB,EACrB,mBAAmB,EACnB,mBAAmB,EACnB,KAAK,cAAc,EACnB,oBAAoB,GACrB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,KAAK,mBAAmB,EAAoB,MAAM,sBAAsB,CAAC;AA2BlF,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,KAAK,CAAC,EAAE,mBAAmB,CAAC;IAC5B;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAmLD;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,eAAe,EAAE,CAAC,CAE5B;AAED;;;;GAIG;AACH,wBAAsB,oBAAoB,CACxC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,mBAAmB,CAAC,CAAC,OAAO,CAAC,EACpD,OAAO,GAAE,eAAoB,EAC7B,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,cAAc,CAAC,CAuBzB;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,GAAE,eAAoB,GAAG,IAAI,CAyBpF"}
package/llms/cli.txt CHANGED
@@ -457,7 +457,7 @@ same Zod schema an HTTP or MCP call does.
457
457
  | `--json` | Compact success/error JSON records for scripts |
458
458
  | `--wait` | Block-poll an async result to a terminal state |
459
459
  | `--wait-timeout <s>` | Override the `--wait` timeout |
460
- | `--output-dir <dir>` | Download result media into a directory |
460
+ | `--output-dir <dir>` | Save the files a result names or carries into a directory |
461
461
  | `--quiet` | Suppress non-essential stderr output |
462
462
  | `--dry-run` | Print the resolved call without executing |
463
463
  | `--schema` | One JSON line: the JSON Schema of a command's input and output |
@@ -679,15 +679,75 @@ myapp generate "a fox" --wait --output-dir ./out
679
679
  myapp generate "a fox" --wait --json > result.json &
680
680
  ```
681
681
 
682
+ ### Saving result media
683
+
684
+ `--output-dir <dir>` saves the files a successful result carries, as one batch in one
685
+ directory: the blocks of the result's top-level `content` array that hold a file inline, and
686
+ the `{ url, name }` files the optional `download(result)` callback names. Inline files need
687
+ no callback — a `defineViewFileTool` result, or a file returned through an MCP client
688
+ connection, is saved as it arrives. URL files keep their SSRF checks, redirect checks and
689
+ fetch deadlines.
690
+
691
+ Three kinds of block hold a file, and the file may be of any type:
692
+
693
+ | Block | Bytes | File name |
694
+ | --- | --- | --- |
695
+ | `image`, `audio` (`McpMediaContentSchema`) | standard base64 in `data` | `name` |
696
+ | `resource` with `blob` — a PDF, a spreadsheet, an archive | standard base64 in `resource.blob` | last path segment of `resource.uri` |
697
+ | `resource` with `text` — CSV, Markdown, JSON | `resource.text` as UTF-8 | last path segment of `resource.uri` |
698
+
699
+ A part of `content` is saved only when it matches one of these shapes. Any other part — a
700
+ rich-text node that also calls itself `image`, a `resource` without content — is the result's
701
+ own data and is left alone.
702
+
703
+ **Names.** A name is reduced to its basename, like a URL download name;
704
+ `defineViewFileTool` sets `name` to the file name of the source. A block that names no file
705
+ is `<type>-<sha256-of-its-bytes><extension>`: known media types supply the extension and
706
+ the rest use `.bin`. A name is written once per batch. The same bytes under the same name
707
+ are one file; different bytes under a name the batch already wrote fail that file, and the
708
+ first one stays.
709
+
710
+ **Bytes.** Padded and unpadded canonical base64 are accepted; anything else fails that file
711
+ alone. `maxDownloadBytes` — a positive safe integer, checked when the CLI is created —
712
+ limits each URL file and each inline file by its decoded or UTF-8 size (default 100 MB). The
713
+ size is checked before the bytes are allocated, and inline files are decoded and written one
714
+ at a time.
715
+
716
+ **Output.** `saved <path> (<size>KB)` goes to stderr; `--quiet` suppresses those lines and
717
+ keeps errors. On stdout each saved inline block is replaced by a link to its file, so the
718
+ bytes are not printed a second time:
719
+
720
+ ```json
721
+ {
722
+ "type": "resource_link",
723
+ "uri": "file:///work/out/shot.png",
724
+ "name": "shot.png",
725
+ "mimeType": "image/png",
726
+ "size": 48213
727
+ }
728
+ ```
729
+
730
+ The link carries `mimeType` when the block declared one. A block that could not be saved
731
+ keeps its bytes, so nothing the operation returned is lost;
732
+ the other files of the batch are still saved and the exit is `1`. A result policy
733
+ (`results`) receives the operation's own result, not the linked one. Without `--output-dir`,
734
+ an inline result is ordinary JSON.
735
+
736
+ **Nothing to save.** A result that carries no files is not an error. The command prints and
737
+ exits exactly as it does without the flag — views and result policies included — and stderr
738
+ says `--output-dir: the result carries no files to save` unless `--quiet` is given. A failed
739
+ operation keeps its own error and exit code. Help, schema inspection and dry-run do not
740
+ execute the operation.
741
+
682
742
  ### Download filesystem boundary
683
743
 
684
- `--output-dir` resolves the explicitly configured root once before fetching and pins that
744
+ `--output-dir` resolves the explicitly configured root once before fetching or decoding and pins that
685
745
  one directory for the whole batch. An initial root alias (including a symbolic link) selects
686
746
  its canonical directory; missing components are created through pinned parents. The root
687
747
  capability continues to name the same directory if another actor renames it or replaces its
688
748
  pathname. The original spelling printed in `saved` is a display path, not a re-open of the root.
689
749
 
690
- Each media name becomes a direct child. Downloads atomically replace an existing file,
750
+ Each media name becomes a direct child. URL and inline files atomically replace an existing file,
691
751
  symbolic link (including a dangling link), or hard link without changing the inode a link
692
752
  points to. An existing directory refuses replacement. Ordinary files retain their permission
693
753
  bits; new files use `0666 & ~umask`. Replacing writes a new file beside the old one, so it needs
@@ -78,8 +78,8 @@ own, recorded as an ADR.
78
78
  | `stitchkit/process/owner-loss` | bundled server entry (Bun, POSIX) | evolving | narrow bootstrap that keeps the process runner lazy while carrying its owner-loss guard in a single-file bundle |
79
79
  | `stitchkit/files/packaging` | build time (Bun or Node) | evolving | installed native asset graph, integrity, Bun packaging plugin and the artifact loader check; a client bundled into one self-contained, hashed and verified file; no runtime install policy |
80
80
  | `stitchkit/files` | server (Bun or Node) | stable | peer-free managed local-file boundary |
81
- | `stitchkit/telegram` | server (Bun or Node) | evolving | peer-free Telegram platform primitives — Mini App `initData` verification, send-failure classification, one outbound budget per bot token with weighted albums and per-chat 429 pauses, resumable broadcasts, the operator channel with dedupe, a Bot API transport that separates a request that never left from an unknown outcome, local Bot API files, the waveform of a voice message read from its PCM, webhook ownership and durable webhook update intake |
82
- | `stitchkit/telegram/grammy` | server (Bun or Node) | evolving | the token's outbound budget as one grammY client transformer — a 429 that comes back as a response pauses its chat, and a cancelled wait is a request that never left |
81
+ | `stitchkit/telegram` | server (Bun or Node) | evolving | peer-free Telegram platform primitives — Mini App `initData` verification, widget limits and the `widget_query` and widget message schemas, send-failure classification, one outbound budget per bot token with weighted albums and per-chat 429 pauses, resumable broadcasts, the operator channel with dedupe, a Bot API transport that separates a request that never left from an unknown outcome, local Bot API files, the waveform of a voice message read from its PCM, webhook ownership and durable webhook update intake |
82
+ | `stitchkit/telegram/grammy` | server (Bun or Node) | evolving | the token's outbound budget as one grammY client transformer — a 429 that comes back as a response pauses its chat, and a cancelled wait is a request that never left; the widget methods over the same client |
83
83
  | `stitchkit/telegram/html` | browser and server | evolving | Telegram's HTML as a tree — clean any markup to what Telegram accepts, cut long text into valid parts by what the reader sees, check markup before it is sent |
84
84
  | `stitchkit/telegram/voice` | browser and server | evolving | the waveform of a voice message as one format — packed for `sendVoice` the way Telegram Desktop scales it, unpacked to draw it, a `getMe` read of whether the server takes it |
85
85
  | `stitchkit/telegram/screens` | server (Bun or Node) | evolving | a bot's menus as declared screens over grammY — typed paths, actions and input, message reconciliation, state in any `StorageAdapter`, a test chat |
@@ -5,18 +5,25 @@
5
5
 
6
6
  Contains: API reference: `stitchkit/telegram/grammy` (docs/api/reference.md)
7
7
 
8
+ Also read:
9
+ - see `llms/telegram.txt` for the Telegram bots guide
10
+
8
11
  ==============================================================================
9
12
  # API reference: `stitchkit/telegram/grammy` (docs/api/reference.md)
10
13
  ==============================================================================
11
14
 
12
15
  ## `stitchkit/telegram/grammy`
13
16
 
14
- The token's outbound budget as one grammY client transformer. Server-only, Bun
15
- or Node; `grammy` appears in the declarations and is never imported at run time —
16
- the bot brings it. → ADR 0274 — [guide](../guide/telegram.md#one-token-one-outbound-budget)
17
+ The token's outbound budget as one grammY client transformer, and the widget
18
+ methods over the same client. Server-only, Bun or Node; `grammy` appears in the
19
+ declarations and is never imported at run time — the bot brings it. → ADR 0274 —
20
+ [guide](../guide/telegram.md#one-token-one-outbound-budget), [widgets](../guide/telegram.md#widgets)
17
21
 
18
22
  | Export | Kind | Summary |
19
23
  |--------|------|---------|
20
24
  | `telegramBudgetTransformer` | function | `bot.api.config.use(telegramBudgetTransformer(budget))`: every Bot API call runs under the token's `telegramOutboundBudget`; a 429 that comes back as a response pauses the token, a cancellation before the request was handed on throws `TelegramNotDispatchedError`, a cancellation in flight stays the client's own error; it never retries |
25
+ | `telegramWidgetApi` / `TelegramWidgetApi` | function / _type_ | `telegramWidgetApi(bot.api)`: `sendWidget`, `editMessageWidgetData` and `answerWidgetQuery` through the client's `api.raw`, so its transformers — the outbound budget among them — run each call; input over a limit throws `ZodError` before any request, an already aborted signal throws `TelegramNotDispatchedError`, a request is handed on once and never repeated, `message is not modified` is success, and an unexpected successful answer is an error whose outcome is unknown |
26
+ | `TelegramSendWidgetParams` / `TelegramEditMessageWidgetDataParams` / `TelegramAnswerWidgetQueryParams` | _type_ | the methods' Bot API parameters by their Bot API names; an edit names `chat_id` with `message_id` or `inline_message_id`, and `chat_data` or `public_data` (`''` removes it, an omitted field keeps it) |
27
+ | `TelegramWidgetCallOptions` | _type_ | `signal` for one call: aborted before the request leaves, the call is not dispatched |
21
28
 
22
29
  ---
@@ -67,5 +67,10 @@ injected grammY bot. → ADR 0143, ADR 0201, ADR 0274 — [guide](../guide/teleg
67
67
  | `postgresTelegramUpdateStore` / `PostgresTelegramUpdateStoreConfig` / `TelegramPostgresQuery` | function / _type_ | the store in Postgres over the client the application holds, as one `query(text, parameters)` returning rows — Bun `sql.unsafe`, Prisma `$queryRawUnsafe`, `pg` `pool.query`; each rule one conditional statement; `table`, `createTable` (default `true`) |
68
68
  | `postgresTelegramUpdateStoreSchema` | function | the table, idempotent `claim_id` upgrade and index as a statement for a migration, safe to run twice; required before fenced methods when `createTable: false`, while legacy base methods remain compatible with the prior table |
69
69
  | `checkTelegramUpdateStore` | function | put any store through every rule, each on a fresh store from `make`, concurrent claims included; resolves with the violations, empty when it keeps them |
70
+ | `TELEGRAM_WIDGET_LIMITS` | constant | Bot API 10.4 widget limits, sizes in UTF-8 bytes: `htmlBytes` 32768, `compressedHtmlBytes` 4096 after zlib level 9 (checked with the runtime's zlib, an estimate of Telegram's), `dataBytes` 1024 for `chat_data`, `public_data` and an answer, `queryDataBytes` 64, `answerWindowMs` 15 000 — [guide](../guide/telegram.md#widgets) |
71
+ | `TelegramWidgetHtmlSchema` / `TelegramWidgetDataSchema` | schema | widget HTML (1–32768 bytes, at most 4096 compressed) and widget data (0–1024 bytes), counted in UTF-8 bytes; NUL and an unpaired surrogate are refused, an empty data string is a value |
72
+ | `TelegramWidgetMessageSchema` / `TelegramWidgetMessage` | schema / _type_ | the message `sendWidget` returns — `message_id`, `chat`, `widget` with `html` and its data; parsed loosely, so fields Telegram adds pass through |
73
+ | `TelegramWidgetQuerySchema` / `TelegramWidgetQuery` | schema / _type_ | `Update.widget_query`: `id`, `from`, `chat` with `message_id` or `inline_message_id`, `chat_instance`, `type` (`data` / `sign_data` / `sent_transaction`), `data` of 1–64 bytes, `widget_id` |
74
+ | `parseTelegramWidgetQuery` / `TelegramWidgetQueryParse` | function / _type_ | read `update.widget_query`: `{ query }`, or `{ id, error }` for a query that does not parse — its id is enough to answer it with a refusal — or `undefined` without an id |
70
75
 
71
76
  ---
package/llms/telegram.txt CHANGED
@@ -29,6 +29,7 @@ way: [ADR 0201](../decisions/0201-a-telegram-bot-is-assembled-from-primitives.md
29
29
  | the bot as resources: command menu, then long polling with updates admitted by batch | `grammyBotResources` — [application kernel](application-kernel.md#optional-grammy-adapter) |
30
30
  | a bot on a webhook: set only where it is this process's, updates recorded before Telegram is answered | `claimTelegramWebhook`, `createTelegramUpdateIntake`, `receiveTelegramWebhook` — [below](#a-bot-on-a-webhook) |
31
31
  | text for Telegram's HTML: cleaned, cut to the limit, checked | `stitchkit/telegram/html` — [below](#message-markup) |
32
+ | widgets: send, change their data, answer their queries, under the token's budget | `telegramWidgetApi` from `stitchkit/telegram/grammy` — [below](#widgets) |
32
33
  | the process journal | `createJsonLogger` from `stitchkit/observability` |
33
34
  | the operator's chat: product events, apart from the journal | `createTelegramOperatorChannel` |
34
35
  | a broadcast that survives crashes, deploys and Ctrl-C | `runTelegramBroadcast` |
@@ -393,6 +394,75 @@ round waits `connectAttemptMs`, default 2 s). The answer must arrive within `res
393
394
  `maxResponseBytes` (default 1 MiB). It speaks HTTP/1.1 over `https:`, and over `http:` for a
394
395
  local Bot API server, and it uses no proxy: where the network needs one, keep `fetch`.
395
396
 
397
+ ## Widgets
398
+
399
+ A widget (Bot API 10.4) is HTML that the Telegram app runs and draws inside a message, with data
400
+ of its own the bot changes and queries it sends to the bot. grammY 1.46 does not type the three
401
+ methods, and a bot that reaches for them tends to build a second client and with it a second
402
+ limiter. `telegramWidgetApi` takes the client the bot already has and calls the methods through
403
+ its `api.raw`, so the client's transformers — the outbound budget among them — run every call:
404
+
405
+ ```ts
406
+ import { telegramOutboundBudget } from 'stitchkit/telegram'
407
+ import { telegramBudgetTransformer, telegramWidgetApi } from 'stitchkit/telegram/grammy'
408
+
409
+ bot.api.config.use(telegramBudgetTransformer(telegramOutboundBudget({ token: env.BOT_TOKEN })))
410
+ const widgets = telegramWidgetApi(bot.api)
411
+
412
+ const message = await widgets.sendWidget(
413
+ { chat_id, html, chat_data: state, allow_queries: true, reply_markup },
414
+ { signal },
415
+ )
416
+ await widgets.editMessageWidgetData(
417
+ { chat_id, message_id: message.message_id, chat_data: next },
418
+ { signal },
419
+ )
420
+ await widgets.answerWidgetQuery({ widget_query_id: query.id, data: answer }, { signal })
421
+ ```
422
+
423
+ The parameters are the Bot API's, by their Bot API names, so the call reads as the grammY call it
424
+ becomes once grammY types the method. A field is sent when it is not `undefined`: `chat_data: ''`
425
+ reaches Telegram and removes the data, an omitted field keeps it. An edit names `chat_id` with
426
+ `message_id`, or `inline_message_id`, and changes `chat_data`, `public_data` or both.
427
+
428
+ - **Limits are checked before the request, in UTF-8 bytes.** `TELEGRAM_WIDGET_LIMITS`: HTML 1–32768
429
+ bytes and at most 4096 after zlib level 9; `chat_data`, `public_data` and an answer 0–1024; query
430
+ data 1–64. One emoji is four bytes, one Cyrillic letter two. NUL and an unpaired surrogate are
431
+ refused too. A refused value throws `ZodError` naming its field and size: nothing was sent, and
432
+ the same input will be refused again, so a durable sender records it as failed rather than
433
+ retrying. `TelegramWidgetHtmlSchema` and `TelegramWidgetDataSchema` are the same checks for a
434
+ sender that wants its own margin first.
435
+ - **The compressed size is an estimate.** It is measured with the runtime's own zlib at level 9,
436
+ and zlib builds differ by tens of bytes near 4096 — Node's output is smaller than Bun's on the
437
+ same HTML — while Telegram does not say which it runs. HTML that compresses close to the limit
438
+ can pass here and be refused by Telegram; keep a margin below 4096.
439
+ - **A send and an edit cost one message; an answer costs none.** Telegram counts a data edit as a
440
+ sent message, so both wait for the token's schedule and a 429 pauses their chat; an inline edit
441
+ has no chat and a 429 on it pauses the token. `answerWidgetQuery` waits for no slot and no pause:
442
+ Telegram expects the answer within `TELEGRAM_WIDGET_LIMITS.answerWindowMs` (15 s).
443
+ - **What happens to a call is said by its error.** An already aborted `signal`, or one aborted
444
+ while the call waits for its slot, throws `TelegramNotDispatchedError` — nothing left, safe to
445
+ send again. A request handed to the client is never repeated: a failure after that point may
446
+ have been delivered, and a send whose outcome is unknown is the caller's to settle.
447
+ `message is not modified` on an edit is success. A successful answer that is not what the method
448
+ returns — `true` where a message was due, as a grammY webhook reply gives — is an error with the
449
+ parse failure as its `cause`: the request was sent and its outcome is unknown.
450
+ - **Queries are answered at intake.** `widget_query` must be in the webhook's or polling's
451
+ `allowed_updates`; grammY's `Update` type does not name it yet, so read
452
+ `update.widget_query` with `parseTelegramWidgetQuery`. It returns `{ query }`, or `{ id, error }`
453
+ for a query that does not parse — an unknown `type` or data outside 1–64 bytes — whose id is
454
+ enough to answer it with a refusal rather than leave the widget waiting, or `undefined` without
455
+ an id. Telegram's message, chat and query objects are parsed loosely: fields it adds later pass
456
+ through.
457
+ - **Shared widgets are not here.** `is_shared`, `sendSharedWidget`, `editSharedWidgetData` and
458
+ `shared_widget_query` are outside this API; in production Telegram sends widgets to private
459
+ chats only.
460
+
461
+ The installed
462
+ [`telegram-widget-api.mjs`](../../packages/core/scripts/consumer-lane/fixtures/minimal/src/telegram-widget-api.mjs)
463
+ fixture runs the packed methods through the packed budget transformer on Bun and Node with no
464
+ grammY installed.
465
+
396
466
  ## Message markup
397
467
 
398
468
  `stitchkit/telegram/html` is Telegram's HTML parse mode as a tree, with no peer
package/llms/tools.2.txt CHANGED
@@ -556,6 +556,11 @@ Ordinary file names containing brackets, such as `[preview].png`, remain valid.
556
556
  A local file with an unsupported extension is refused before reading; its
557
557
  per-item error identifies the caller's path and the extension it saw.
558
558
 
559
+ Each inline `image` and `audio` block carries `name`, the file name of its
560
+ source: a managed file keeps its own name, a URL takes its download name — the
561
+ path's base name with the body's extension. A CLI saves the block under that
562
+ name with [`--output-dir`](cli.md#saving-result-media).
563
+
559
564
  The older `mountWait`, `mountDownload`, `mountUpload` and `mountViewFile`
560
565
  functions remain
561
566
  intentional raw MCP adapters with their text-envelope behavior. Use them under
package/llms-full.txt CHANGED
@@ -69,8 +69,8 @@ own, recorded as an ADR.
69
69
  | `stitchkit/process/owner-loss` | bundled server entry (Bun, POSIX) | evolving | narrow bootstrap that keeps the process runner lazy while carrying its owner-loss guard in a single-file bundle |
70
70
  | `stitchkit/files/packaging` | build time (Bun or Node) | evolving | installed native asset graph, integrity, Bun packaging plugin and the artifact loader check; a client bundled into one self-contained, hashed and verified file; no runtime install policy |
71
71
  | `stitchkit/files` | server (Bun or Node) | stable | peer-free managed local-file boundary |
72
- | `stitchkit/telegram` | server (Bun or Node) | evolving | peer-free Telegram platform primitives — Mini App `initData` verification, send-failure classification, one outbound budget per bot token with weighted albums and per-chat 429 pauses, resumable broadcasts, the operator channel with dedupe, a Bot API transport that separates a request that never left from an unknown outcome, local Bot API files, the waveform of a voice message read from its PCM, webhook ownership and durable webhook update intake |
73
- | `stitchkit/telegram/grammy` | server (Bun or Node) | evolving | the token's outbound budget as one grammY client transformer — a 429 that comes back as a response pauses its chat, and a cancelled wait is a request that never left |
72
+ | `stitchkit/telegram` | server (Bun or Node) | evolving | peer-free Telegram platform primitives — Mini App `initData` verification, widget limits and the `widget_query` and widget message schemas, send-failure classification, one outbound budget per bot token with weighted albums and per-chat 429 pauses, resumable broadcasts, the operator channel with dedupe, a Bot API transport that separates a request that never left from an unknown outcome, local Bot API files, the waveform of a voice message read from its PCM, webhook ownership and durable webhook update intake |
73
+ | `stitchkit/telegram/grammy` | server (Bun or Node) | evolving | the token's outbound budget as one grammY client transformer — a 429 that comes back as a response pauses its chat, and a cancelled wait is a request that never left; the widget methods over the same client |
74
74
  | `stitchkit/telegram/html` | browser and server | evolving | Telegram's HTML as a tree — clean any markup to what Telegram accepts, cut long text into valid parts by what the reader sees, check markup before it is sent |
75
75
  | `stitchkit/telegram/voice` | browser and server | evolving | the waveform of a voice message as one format — packed for `sendVoice` the way Telegram Desktop scales it, unpacked to draw it, a `getMe` read of whether the server takes it |
76
76
  | `stitchkit/telegram/screens` | server (Bun or Node) | evolving | a bot's menus as declared screens over grammY — typed paths, actions and input, message reconciliation, state in any `StorageAdapter`, a test chat |
@@ -5413,6 +5413,11 @@ Ordinary file names containing brackets, such as `[preview].png`, remain valid.
5413
5413
  A local file with an unsupported extension is refused before reading; its
5414
5414
  per-item error identifies the caller's path and the extension it saw.
5415
5415
 
5416
+ Each inline `image` and `audio` block carries `name`, the file name of its
5417
+ source: a managed file keeps its own name, a URL takes its download name — the
5418
+ path's base name with the body's extension. A CLI saves the block under that
5419
+ name with [`--output-dir`](cli.md#saving-result-media).
5420
+
5416
5421
  The older `mountWait`, `mountDownload`, `mountUpload` and `mountViewFile`
5417
5422
  functions remain
5418
5423
  intentional raw MCP adapters with their text-envelope behavior. Use them under
@@ -10864,7 +10869,7 @@ same Zod schema an HTTP or MCP call does.
10864
10869
  | `--json` | Compact success/error JSON records for scripts |
10865
10870
  | `--wait` | Block-poll an async result to a terminal state |
10866
10871
  | `--wait-timeout <s>` | Override the `--wait` timeout |
10867
- | `--output-dir <dir>` | Download result media into a directory |
10872
+ | `--output-dir <dir>` | Save the files a result names or carries into a directory |
10868
10873
  | `--quiet` | Suppress non-essential stderr output |
10869
10874
  | `--dry-run` | Print the resolved call without executing |
10870
10875
  | `--schema` | One JSON line: the JSON Schema of a command's input and output |
@@ -11086,15 +11091,75 @@ myapp generate "a fox" --wait --output-dir ./out
11086
11091
  myapp generate "a fox" --wait --json > result.json &
11087
11092
  ```
11088
11093
 
11094
+ ### Saving result media
11095
+
11096
+ `--output-dir <dir>` saves the files a successful result carries, as one batch in one
11097
+ directory: the blocks of the result's top-level `content` array that hold a file inline, and
11098
+ the `{ url, name }` files the optional `download(result)` callback names. Inline files need
11099
+ no callback — a `defineViewFileTool` result, or a file returned through an MCP client
11100
+ connection, is saved as it arrives. URL files keep their SSRF checks, redirect checks and
11101
+ fetch deadlines.
11102
+
11103
+ Three kinds of block hold a file, and the file may be of any type:
11104
+
11105
+ | Block | Bytes | File name |
11106
+ | --- | --- | --- |
11107
+ | `image`, `audio` (`McpMediaContentSchema`) | standard base64 in `data` | `name` |
11108
+ | `resource` with `blob` — a PDF, a spreadsheet, an archive | standard base64 in `resource.blob` | last path segment of `resource.uri` |
11109
+ | `resource` with `text` — CSV, Markdown, JSON | `resource.text` as UTF-8 | last path segment of `resource.uri` |
11110
+
11111
+ A part of `content` is saved only when it matches one of these shapes. Any other part — a
11112
+ rich-text node that also calls itself `image`, a `resource` without content — is the result's
11113
+ own data and is left alone.
11114
+
11115
+ **Names.** A name is reduced to its basename, like a URL download name;
11116
+ `defineViewFileTool` sets `name` to the file name of the source. A block that names no file
11117
+ is `<type>-<sha256-of-its-bytes><extension>`: known media types supply the extension and
11118
+ the rest use `.bin`. A name is written once per batch. The same bytes under the same name
11119
+ are one file; different bytes under a name the batch already wrote fail that file, and the
11120
+ first one stays.
11121
+
11122
+ **Bytes.** Padded and unpadded canonical base64 are accepted; anything else fails that file
11123
+ alone. `maxDownloadBytes` — a positive safe integer, checked when the CLI is created —
11124
+ limits each URL file and each inline file by its decoded or UTF-8 size (default 100 MB). The
11125
+ size is checked before the bytes are allocated, and inline files are decoded and written one
11126
+ at a time.
11127
+
11128
+ **Output.** `saved <path> (<size>KB)` goes to stderr; `--quiet` suppresses those lines and
11129
+ keeps errors. On stdout each saved inline block is replaced by a link to its file, so the
11130
+ bytes are not printed a second time:
11131
+
11132
+ ```json
11133
+ {
11134
+ "type": "resource_link",
11135
+ "uri": "file:///work/out/shot.png",
11136
+ "name": "shot.png",
11137
+ "mimeType": "image/png",
11138
+ "size": 48213
11139
+ }
11140
+ ```
11141
+
11142
+ The link carries `mimeType` when the block declared one. A block that could not be saved
11143
+ keeps its bytes, so nothing the operation returned is lost;
11144
+ the other files of the batch are still saved and the exit is `1`. A result policy
11145
+ (`results`) receives the operation's own result, not the linked one. Without `--output-dir`,
11146
+ an inline result is ordinary JSON.
11147
+
11148
+ **Nothing to save.** A result that carries no files is not an error. The command prints and
11149
+ exits exactly as it does without the flag — views and result policies included — and stderr
11150
+ says `--output-dir: the result carries no files to save` unless `--quiet` is given. A failed
11151
+ operation keeps its own error and exit code. Help, schema inspection and dry-run do not
11152
+ execute the operation.
11153
+
11089
11154
  ### Download filesystem boundary
11090
11155
 
11091
- `--output-dir` resolves the explicitly configured root once before fetching and pins that
11156
+ `--output-dir` resolves the explicitly configured root once before fetching or decoding and pins that
11092
11157
  one directory for the whole batch. An initial root alias (including a symbolic link) selects
11093
11158
  its canonical directory; missing components are created through pinned parents. The root
11094
11159
  capability continues to name the same directory if another actor renames it or replaces its
11095
11160
  pathname. The original spelling printed in `saved` is a display path, not a re-open of the root.
11096
11161
 
11097
- Each media name becomes a direct child. Downloads atomically replace an existing file,
11162
+ Each media name becomes a direct child. URL and inline files atomically replace an existing file,
11098
11163
  symbolic link (including a dangling link), or hard link without changing the inode a link
11099
11164
  points to. An existing directory refuses replacement. Ordinary files retain their permission
11100
11165
  bits; new files use `0666 & ~umask`. Replacing writes a new file beside the old one, so it needs
@@ -16559,6 +16624,7 @@ way: [ADR 0201](../decisions/0201-a-telegram-bot-is-assembled-from-primitives.md
16559
16624
  | the bot as resources: command menu, then long polling with updates admitted by batch | `grammyBotResources` — [application kernel](application-kernel.md#optional-grammy-adapter) |
16560
16625
  | a bot on a webhook: set only where it is this process's, updates recorded before Telegram is answered | `claimTelegramWebhook`, `createTelegramUpdateIntake`, `receiveTelegramWebhook` — [below](#a-bot-on-a-webhook) |
16561
16626
  | text for Telegram's HTML: cleaned, cut to the limit, checked | `stitchkit/telegram/html` — [below](#message-markup) |
16627
+ | widgets: send, change their data, answer their queries, under the token's budget | `telegramWidgetApi` from `stitchkit/telegram/grammy` — [below](#widgets) |
16562
16628
  | the process journal | `createJsonLogger` from `stitchkit/observability` |
16563
16629
  | the operator's chat: product events, apart from the journal | `createTelegramOperatorChannel` |
16564
16630
  | a broadcast that survives crashes, deploys and Ctrl-C | `runTelegramBroadcast` |
@@ -16923,6 +16989,75 @@ round waits `connectAttemptMs`, default 2 s). The answer must arrive within `res
16923
16989
  `maxResponseBytes` (default 1 MiB). It speaks HTTP/1.1 over `https:`, and over `http:` for a
16924
16990
  local Bot API server, and it uses no proxy: where the network needs one, keep `fetch`.
16925
16991
 
16992
+ ## Widgets
16993
+
16994
+ A widget (Bot API 10.4) is HTML that the Telegram app runs and draws inside a message, with data
16995
+ of its own the bot changes and queries it sends to the bot. grammY 1.46 does not type the three
16996
+ methods, and a bot that reaches for them tends to build a second client and with it a second
16997
+ limiter. `telegramWidgetApi` takes the client the bot already has and calls the methods through
16998
+ its `api.raw`, so the client's transformers — the outbound budget among them — run every call:
16999
+
17000
+ ```ts
17001
+ import { telegramOutboundBudget } from 'stitchkit/telegram'
17002
+ import { telegramBudgetTransformer, telegramWidgetApi } from 'stitchkit/telegram/grammy'
17003
+
17004
+ bot.api.config.use(telegramBudgetTransformer(telegramOutboundBudget({ token: env.BOT_TOKEN })))
17005
+ const widgets = telegramWidgetApi(bot.api)
17006
+
17007
+ const message = await widgets.sendWidget(
17008
+ { chat_id, html, chat_data: state, allow_queries: true, reply_markup },
17009
+ { signal },
17010
+ )
17011
+ await widgets.editMessageWidgetData(
17012
+ { chat_id, message_id: message.message_id, chat_data: next },
17013
+ { signal },
17014
+ )
17015
+ await widgets.answerWidgetQuery({ widget_query_id: query.id, data: answer }, { signal })
17016
+ ```
17017
+
17018
+ The parameters are the Bot API's, by their Bot API names, so the call reads as the grammY call it
17019
+ becomes once grammY types the method. A field is sent when it is not `undefined`: `chat_data: ''`
17020
+ reaches Telegram and removes the data, an omitted field keeps it. An edit names `chat_id` with
17021
+ `message_id`, or `inline_message_id`, and changes `chat_data`, `public_data` or both.
17022
+
17023
+ - **Limits are checked before the request, in UTF-8 bytes.** `TELEGRAM_WIDGET_LIMITS`: HTML 1–32768
17024
+ bytes and at most 4096 after zlib level 9; `chat_data`, `public_data` and an answer 0–1024; query
17025
+ data 1–64. One emoji is four bytes, one Cyrillic letter two. NUL and an unpaired surrogate are
17026
+ refused too. A refused value throws `ZodError` naming its field and size: nothing was sent, and
17027
+ the same input will be refused again, so a durable sender records it as failed rather than
17028
+ retrying. `TelegramWidgetHtmlSchema` and `TelegramWidgetDataSchema` are the same checks for a
17029
+ sender that wants its own margin first.
17030
+ - **The compressed size is an estimate.** It is measured with the runtime's own zlib at level 9,
17031
+ and zlib builds differ by tens of bytes near 4096 — Node's output is smaller than Bun's on the
17032
+ same HTML — while Telegram does not say which it runs. HTML that compresses close to the limit
17033
+ can pass here and be refused by Telegram; keep a margin below 4096.
17034
+ - **A send and an edit cost one message; an answer costs none.** Telegram counts a data edit as a
17035
+ sent message, so both wait for the token's schedule and a 429 pauses their chat; an inline edit
17036
+ has no chat and a 429 on it pauses the token. `answerWidgetQuery` waits for no slot and no pause:
17037
+ Telegram expects the answer within `TELEGRAM_WIDGET_LIMITS.answerWindowMs` (15 s).
17038
+ - **What happens to a call is said by its error.** An already aborted `signal`, or one aborted
17039
+ while the call waits for its slot, throws `TelegramNotDispatchedError` — nothing left, safe to
17040
+ send again. A request handed to the client is never repeated: a failure after that point may
17041
+ have been delivered, and a send whose outcome is unknown is the caller's to settle.
17042
+ `message is not modified` on an edit is success. A successful answer that is not what the method
17043
+ returns — `true` where a message was due, as a grammY webhook reply gives — is an error with the
17044
+ parse failure as its `cause`: the request was sent and its outcome is unknown.
17045
+ - **Queries are answered at intake.** `widget_query` must be in the webhook's or polling's
17046
+ `allowed_updates`; grammY's `Update` type does not name it yet, so read
17047
+ `update.widget_query` with `parseTelegramWidgetQuery`. It returns `{ query }`, or `{ id, error }`
17048
+ for a query that does not parse — an unknown `type` or data outside 1–64 bytes — whose id is
17049
+ enough to answer it with a refusal rather than leave the widget waiting, or `undefined` without
17050
+ an id. Telegram's message, chat and query objects are parsed loosely: fields it adds later pass
17051
+ through.
17052
+ - **Shared widgets are not here.** `is_shared`, `sendSharedWidget`, `editSharedWidgetData` and
17053
+ `shared_widget_query` are outside this API; in production Telegram sends widgets to private
17054
+ chats only.
17055
+
17056
+ The installed
17057
+ [`telegram-widget-api.mjs`](../../packages/core/scripts/consumer-lane/fixtures/minimal/src/telegram-widget-api.mjs)
17058
+ fixture runs the packed methods through the packed budget transformer on Bun and Node with no
17059
+ grammY installed.
17060
+
16926
17061
  ## Message markup
16927
17062
 
16928
17063
  `stitchkit/telegram/html` is Telegram's HTML parse mode as a tree, with no peer
@@ -25285,18 +25420,27 @@ injected grammY bot. → ADR 0143, ADR 0201, ADR 0274 — [guide](../guide/teleg
25285
25420
  | `postgresTelegramUpdateStore` / `PostgresTelegramUpdateStoreConfig` / `TelegramPostgresQuery` | function / _type_ | the store in Postgres over the client the application holds, as one `query(text, parameters)` returning rows — Bun `sql.unsafe`, Prisma `$queryRawUnsafe`, `pg` `pool.query`; each rule one conditional statement; `table`, `createTable` (default `true`) |
25286
25421
  | `postgresTelegramUpdateStoreSchema` | function | the table, idempotent `claim_id` upgrade and index as a statement for a migration, safe to run twice; required before fenced methods when `createTable: false`, while legacy base methods remain compatible with the prior table |
25287
25422
  | `checkTelegramUpdateStore` | function | put any store through every rule, each on a fresh store from `make`, concurrent claims included; resolves with the violations, empty when it keeps them |
25423
+ | `TELEGRAM_WIDGET_LIMITS` | constant | Bot API 10.4 widget limits, sizes in UTF-8 bytes: `htmlBytes` 32768, `compressedHtmlBytes` 4096 after zlib level 9 (checked with the runtime's zlib, an estimate of Telegram's), `dataBytes` 1024 for `chat_data`, `public_data` and an answer, `queryDataBytes` 64, `answerWindowMs` 15 000 — [guide](../guide/telegram.md#widgets) |
25424
+ | `TelegramWidgetHtmlSchema` / `TelegramWidgetDataSchema` | schema | widget HTML (1–32768 bytes, at most 4096 compressed) and widget data (0–1024 bytes), counted in UTF-8 bytes; NUL and an unpaired surrogate are refused, an empty data string is a value |
25425
+ | `TelegramWidgetMessageSchema` / `TelegramWidgetMessage` | schema / _type_ | the message `sendWidget` returns — `message_id`, `chat`, `widget` with `html` and its data; parsed loosely, so fields Telegram adds pass through |
25426
+ | `TelegramWidgetQuerySchema` / `TelegramWidgetQuery` | schema / _type_ | `Update.widget_query`: `id`, `from`, `chat` with `message_id` or `inline_message_id`, `chat_instance`, `type` (`data` / `sign_data` / `sent_transaction`), `data` of 1–64 bytes, `widget_id` |
25427
+ | `parseTelegramWidgetQuery` / `TelegramWidgetQueryParse` | function / _type_ | read `update.widget_query`: `{ query }`, or `{ id, error }` for a query that does not parse — its id is enough to answer it with a refusal — or `undefined` without an id |
25288
25428
 
25289
25429
  ---
25290
25430
 
25291
25431
  ## `stitchkit/telegram/grammy`
25292
25432
 
25293
- The token's outbound budget as one grammY client transformer. Server-only, Bun
25294
- or Node; `grammy` appears in the declarations and is never imported at run time —
25295
- the bot brings it. → ADR 0274 — [guide](../guide/telegram.md#one-token-one-outbound-budget)
25433
+ The token's outbound budget as one grammY client transformer, and the widget
25434
+ methods over the same client. Server-only, Bun or Node; `grammy` appears in the
25435
+ declarations and is never imported at run time — the bot brings it. → ADR 0274 —
25436
+ [guide](../guide/telegram.md#one-token-one-outbound-budget), [widgets](../guide/telegram.md#widgets)
25296
25437
 
25297
25438
  | Export | Kind | Summary |
25298
25439
  |--------|------|---------|
25299
25440
  | `telegramBudgetTransformer` | function | `bot.api.config.use(telegramBudgetTransformer(budget))`: every Bot API call runs under the token's `telegramOutboundBudget`; a 429 that comes back as a response pauses the token, a cancellation before the request was handed on throws `TelegramNotDispatchedError`, a cancellation in flight stays the client's own error; it never retries |
25441
+ | `telegramWidgetApi` / `TelegramWidgetApi` | function / _type_ | `telegramWidgetApi(bot.api)`: `sendWidget`, `editMessageWidgetData` and `answerWidgetQuery` through the client's `api.raw`, so its transformers — the outbound budget among them — run each call; input over a limit throws `ZodError` before any request, an already aborted signal throws `TelegramNotDispatchedError`, a request is handed on once and never repeated, `message is not modified` is success, and an unexpected successful answer is an error whose outcome is unknown |
25442
+ | `TelegramSendWidgetParams` / `TelegramEditMessageWidgetDataParams` / `TelegramAnswerWidgetQueryParams` | _type_ | the methods' Bot API parameters by their Bot API names; an edit names `chat_id` with `message_id` or `inline_message_id`, and `chat_data` or `public_data` (`''` removes it, an omitted field keeps it) |
25443
+ | `TelegramWidgetCallOptions` | _type_ | `signal` for one call: aborted before the request leaves, the call is not dispatched |
25300
25444
 
25301
25445
  ---
25302
25446