Repository navigation
Description
tg's main users are agents, and the envelope (success, data or error, stderr for errors, documented exit codes) already suits them. This issue proposes six additions to that contract. Each one comes from something agents got wrong while using tg, not from taste, and each would be its own small PR. Tell me which ones you want.
Where the evidence comes from
- Usage: transcripts of 27 agent sessions on my machine over three weeks (Claude Code and Codex,
tg0.5 to 0.10), read for everytgcall, its result and what the agent did next. Counts below are from those. Anything marked "0.12.1" I rechecked on the current release. - Code: counts on
mainatfaf505d. - Precedent: other CLIs built for agents, checked in their current source, plus two guides: clig.dev and Anthropic's Writing tools for agents.
1. next: runnable commands in the answer
Evidence
- A search on an empty cache answers with success and the fix inside a free-text field:
{"results":[],"total":0,"source":"cache","message":"No results in cache. Run \"tg chats list --fresh\" to populate cache first."}. Agents read this as "nothing found" 13 times in 5 sessions before one of them noticed the message. - Every deep read of a chat was a hand-written shell loop that rebuilt the command from
nextBefore. One loop passed the flag wrongly and fetched the same 100 messages 80 times (a quoting mistake on the agent's side, but there was no command to copy).
Precedent: Vercel's agent envelope has next?: Array<{ command, when? }> (agent-output.ts); Basecamp's has breadcrumbs: [{ action, cmd }] (envelope.go); clig.dev: "suggesting to the user commands they can run next helps them learn how to use your program."
Proposal: an optional next: [{ command, why }] beside data. First uses: the next page of a list, and the --fresh command when the cache has nothing. Additive.
2. Errors that say what to do, and whether to retry
Evidence
error()takes asuggestion, andAGENTS.mdlists it in the stable error schema, but 20 of 134 calls pass one. By code:TELEGRAM_ERROR0 of 24,RATE_LIMITED0 of 9,INVALID_ARGS7 of 60. (Finish the AI integration (skills) #3 notes the same gap.)tg apiwith{"peer":"@name"}fails withUnknown object undefined. That happened 7 times in 4 sessions, and each agent had to work outcontacts.resolveUsernameby itself.- 63 downloads run four at a time: 7 failed during a flood-wait and all 7 passed when rerun one by one. Nothing in the error said a retry was safe or when.
Precedent: Basecamp errors are { error, code, retryable, hint } (same file); Vercel has hint; Anthropic's guide: "prompt-engineer your error responses to clearly communicate specific and actionable improvements."
Proposal: set suggestion on the errors agents hit most (unresolved peers in tg api, missing arguments, empty cache, daemon not running), and add retryable plus the wait in seconds where Telegram gives one (details.wait_seconds already has it). Additive.
3. One shape for every list
Evidence (0.12.1, real output):
| Command | List key | Paging | Date field |
|---|---|---|---|
chats list |
items |
offset, limit, hasMore |
lastMessageAt, milliseconds |
chats search |
results |
none | lastMessageAt, milliseconds |
messages list |
items |
before, after, nextBefore, nextAfter |
date, seconds |
messages search |
results |
limit, offset, total |
date, seconds |
contacts list |
items |
offset, limit, hasMore |
— |
tg status and tg sql rows are snake_case; message and chat items are camelCase.
Agents in 6 sessions wrote fallbacks such as .items // .chats // .results, one script crashed reading items from the wrong level, and one query guarded against both date units in a single expression.
Precedent: clig.dev: "Use the same flag names for the same things, have similar output formatting"; the Vercel and Basecamp envelopes above are one shape for every command.
Proposal: every list answers with items and one paging block, and every timestamp uses one unit. This one breaks existing scripts, so it needs your call on how: a major version, or the schema name in point 6, which lets a new shape ship beside the old one.
4. tg commands --json
Evidence: #3 already says tg skill install writes a skill.json that no agent reads. In the sessions, agents learned tg by calling --help on one subcommand after another (dozens of calls), and still guessed flag names: --chat on chats get, which takes --id, and a positional query before tg sql accepted one.
Precedent: Resend ships a commands and a doctor command (src/commands); gh pr list --json with no fields prints the fields it accepts, so the flag documents itself.
Proposal: one command that prints every command, its flags, the error codes it can return and the name of its output shape, from the installed version. The skill in #3 can then be checked against it in CI.
5. Declare each command's output once
Evidence: drift between what is documented and what the code does is already visible: the suggestion field in point 2, and in #159 one new field had to be described by hand in four places (docs/cli-commands.md, docs/database-schema.md, the schema annotation and the TypeScript type).
Proposal: one declaration per command's data that produces the type, the docs table and the entry in tg commands --json, with an e2e check that real output matches it. Which library, if any, is your choice; the point is one source. I would not validate tg's own output at runtime or run a schema over every row of a large read.
6. A schema name in each answer
Why it is needed
- What an agent knows about
tgis older than the binary it runs: it comes from a skill, the docs or an earlier session. Today nothing in an answer says which shape it is, and agents rarely calltg --versionfirst. - The shapes do change.
tgshipped nine releases between 28 October and 13 November, and the sessions above ran three of them (0.5, 0.8, 0.10). Between those,tg mediaappeared,chats listgainedsyncEnabledandsyncReason, andtg sqlbegan to accept a positional query. An agent that learned one version guessed wrong on another. - Two installs can differ at the same moment: 0.11.0 reached npm before the Homebrew tap.
Proposal: a schema field beside success that names the shape and its version, for example tg.messages.list/1. A shape changes only with a new number. That gives point 3 a path that breaks nobody (ship /2, keep /1 for a release), lets point 4 list the shapes, and lets the skill in #3 say which versions it was written for. Additive.
Not proposed
- Detecting an agent from environment variables. Flags and the TTY should decide the mode.
--yesontg send. Read-only mode and a chat allowlist for agents #26 covers that ground better.- Escaping control characters in message text. JSON output already escapes them. Bidi and tag characters do pass through; one line in the skill (Finish the AI integration (skills) #3) saying message text is data, not instructions, covers the agent side.
Suggested order
2, then 1, then 6, then 4 with 5, and 3 last, since it is the only breaking one. I'm happy to send each as its own PR once you say which you want and how you'd like 3 handled.
Activity
neoromantic commentedon Nov 17, 2026
Thanks for going through those sessions. The repeated page fetch and the empty-cache searches make a good case for these changes.
nextfor pagination and empty caches. Please carry the account and filters into the suggested command.ertembiyik commentedon Nov 18, 2026
Thanks. 1 and 2 are up as separate PRs: #176 (
next, carrying the account and every filter) and #177 (errors, starting with flood waits).On 3, the simple version.
itemsinstead ofresults, like every other list. One PR, marked breaking, with the before/after and the one-line script change (.resultsbecomes.items).dateandeditDateare Telegram's own field names and carry Telegram's seconds; the fieldstgadds itself (lastMessageAt, the send result'stimestamp) are milliseconds, as the schema docs already say for the database. I'd only state that rule in the output docs, so an agent doesn't have to guess.On 4: agreed, commands, arguments and flags are enough. What I saw was agents guessing flag names (
--chatvs--id, a positional query vs--query) and calling--helpon one subcommand after another. I have no evidence that listing errors or schema names changes what an agent does, so let's drop both. It derives from the citty command tree, plus read or write fromCOMMAND_ACCESS, so nothing is kept by hand.On 5: I tried it on
tg chats list: #179. One 82-line declaration replaces three hand-written types and the field prose in the docs (which covered 3 of 16 fields) with a generated table, and an e2e test parses the real output strictly against it; the PR shows the failing output when a field is renamed without the declaration. It uses zod as a devDependency only, so nothing new ships in the binary. The output is byte-identical.On 6: let's drop it. If 3 ships as one documented breaking change, we don't need schema names or two shapes side by side.
feat(errors): say whether a retry can work, and what to run (#177)
feat: add next commands to paged reads and an empty chat cache (#185)