Skip to content

feat: add next commands to paged reads and an empty chat cache - #176

Merged
neoromantic merged 1 commit into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/next-commands
Nov 18, 2026
Merged

neoromantic merged 1 commit into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/next-commands

Conversation

@ertembiyik

Copy link
Copy Markdown
Contributor

What and why

Point 1 of #160. A success answer can now carry next: complete tg command lines, each with a reason, so an agent runs the next page or the cache refresh as written instead of rebuilding it from cursors or from advice inside a message string. As you asked, each command keeps the account and every filter the original was given. Refs #160

Before (tg chats search --query 'nothing like it', nothing cached matches):

{
  "success": true,
  "data": {
    "query": "nothing like it",
    "results": [],
    "total": 0,
    "source": "cache",
    "stale": false,
    "message": "No results in cache. Run \"tg chats list --fresh\" to populate cache first."
  }
}

After: the same data, plus

"next": [
  {
    "command": "tg chats list --fresh",
    "why": "No cached chat matches: this refreshes the chat list from Telegram."
  },
  {
    "command": "tg chats search --query='nothing like it'",
    "why": "Then search the refreshed cache again."
  }
]

Paging. tg messages list --chat -1001234567890 --limit 3 --account 'Work Phone' now adds:

"next": [
  {
    "command": "tg messages list --chat=-1001234567890 --limit=3 --before=5 --account='Work Phone'",
    "why": "The next page of older messages."
  }
]

All of the above is real output from the e2e fixtures.

What gets next:

Command When Suggests
messages list after a full page, in the direction it read the next page (--before or --after)
messages search when another match exists the next page (--offset)
chats list, contacts list when hasMore the next page (--offset)
messages wait --chat when it returned messages the wait after the last one, so none is missed
chats search when it finds nothing chats list --fresh, then the same search

How it is built:

  • Placement: next sits beside data, not inside it, so data is exactly what it was. Every existing field (message, nextBefore, nextAfter, hasMore, offset) is unchanged.
  • From parsed arguments, not argv: tgCommand in src/commands/next.ts walks the command's own declared arguments with their defaults, so a flag added to one of these commands later is carried without touching the builder. Flags at their default are left out.
  • Shell-safe: every value goes as --name=value, so a negative chat ID or a query that starts with - stays a value, and anything the shell would change is single-quoted.
  • Formats: json adds next; pretty keeps stdout the bare data and prints each suggestion on stderr; quiet prints nothing. The global -f, -v and -q are not carried.
  • Read-only mode and allowedChats: the builder refuses any command that isn't a read in COMMAND_ACCESS, and a suggestion reads the same chat the original already passed the check for.

Most of the line count in the six command files is their args moving into a named constant, one indent level out, so the builder can read the defaults; git diff -w shows the real change.

Decisions you may want to change:

  • The account is carried as typed. With no --account the suggestion has none and follows the active account. Pinning the resolved ID would survive an account switch between pages, but adds --account=1 to every single-account setup.
  • --fresh is carried on chats list and contacts list, so the next page slices the same list the first page came from. The cost is one dialog fetch per page.
  • messages wait --chat is included, although it isn't a list, because it has a real cursor.
  • Left without next: messages unread (its continuation is a different read), messages wait without --chat (no single cursor), messages watch (a stream), and the "not signed in" answers (tg auth login is a write and needs a person).

How I checked it

  • E2E in src/__e2e__/next-commands.e2e.test.ts: for each paged command the test takes next[0].command from the answer, runs that exact string through sh -c, and gets the following page. One case uses a phrase query with quotes, $ and a leading --, a negative chat ID and an account label with a space. The empty-cache cases and the pretty and quiet formats are covered too.

  • Unit tests in src/__tests__/next-command.test.ts: each quoted value goes through a real sh and comes back unchanged; defaults are left out; a write is refused.

  • Not checked against Telegram: suggestions that need a connection (chats list --fresh, a forward page past the cache) are only checked to be well-formed, since the fixtures have no credentials.

  • bun run check: clean. bun run test: 2319 pass, 0 fail. bun run test:e2e: 443 pass, 0 fail.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard (docs/api-design.md, docs/cli-commands.md, README, CHANGELOG)

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

🤖 Generated with Claude Code

A success answer can carry next: complete tg command lines, each with a
reason, that continue the read. Paged lists (messages list and search,
chats list, contacts list, messages wait on one chat) suggest the next
page, and a chat search that finds nothing suggests the refresh. Each
command is built from the parsed arguments, so it keeps the account and
every filter, quoted for a POSIX shell. Existing fields are unchanged;
pretty output prints the suggestions on stderr.

Refs neoromantic#160

Co-Authored-By: Claude Fable 5.2 <[email protected]>
neoromantic added a commit that referenced this pull request Nov 18, 2026
Lands #176 on current main. Refs #160

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit 7206ad0 into neoromantic:main Nov 18, 2026
ertembiyik pushed a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
Resolves the conflicts with neoromantic#176/neoromantic#177 in CHANGELOG.md (both sections
kept) and src/commands/messages/search.ts (success(data, next) from main,
the snippet from the rendered item text from neoromantic#178).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants