-
Notifications
You must be signed in to change notification settings - Fork 19
feat: add --toon output format for token-optimized CLI output #215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
akb4q
wants to merge
4
commits into
InsForge:main
Choose a base branch
from
akb4q:feat/toon-output-format
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ee4a99c
feat: add --toon output format for token-optimized CLI output
jiangzhuyun 1950776
fix: resolve ESLint errors in toonFormat functions
jiangzhuyun 0a96fe2
fix: address all PR review comments
jiangzhuyun f388046
fix: suppress outputSuccess/Info in toonMode, fix quoting regex, add …
jiangzhuyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { setToonMode, outputTable, outputSuccess, outputInfo, outputJson } from './output.js'; | ||
|
|
||
| describe('toonEscapeValue (indirectly via outputTable)', () => { | ||
| it('quotes values containing structural chars (comma, colon, bracket, etc.)', () => { | ||
| // outputTable with toon mode writes TOON to stdout | ||
| // We capture console.log to test the output | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputTable(['Name'], [['Smith, John']]); | ||
| expect(logs[0]).toContain('"Smith, John"'); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
|
|
||
| it('outputTable renders colon-containing values quoted', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputTable(['Key'], [['a:b']]); | ||
| expect(logs[0]).toContain('"a:b"'); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
|
|
||
| it('does not quote simple alphanumeric values', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputTable(['Name'], [['Alice']]); | ||
| // Tabular TOON: header on first line, values on second line starting with 2 spaces | ||
| expect(logs[0]).toMatch(/\n Alice\n?$/); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
|
|
||
| it('quotes empty string values', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputTable(['Name'], [['']]); | ||
| expect(logs[0]).toContain('""'); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
|
|
||
| it('quotes boolean-looking values', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputTable(['Val'], [['true'], ['false']]); | ||
| expect(logs[0]).toContain('"true"'); | ||
| expect(logs[0]).toContain('"false"'); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('outputJson with TOON mode', () => { | ||
| it('single object output is single TOON document', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputJson({ name: 'Alice', role: 'admin' }); | ||
| expect(logs).toHaveLength(1); | ||
| expect(logs[0]).toContain('name:'); | ||
| expect(logs[0]).toContain('Alice'); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
|
|
||
| it('null values are preserved in TOON output', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputJson({ name: 'Alice', deletedAt: null }); | ||
| expect(logs).toHaveLength(1); | ||
| expect(logs[0]).toContain('null'); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
|
|
||
| it('outputs a single doc (not multiple) in TOON mode', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputJson({ users: [{ id: 1 }, { id: 2 }] }); | ||
| expect(logs).toHaveLength(1); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('outputSuccess/outputInfo in TOON mode', () => { | ||
| it('outputSuccess suppresses in TOON mode', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputSuccess('done'); | ||
| expect(logs).toHaveLength(0); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
|
|
||
| it('outputInfo suppresses in TOON mode', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputInfo('some info'); | ||
| expect(logs).toHaveLength(0); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
|
|
||
| it('outputSuccess works normally outside TOON mode', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| outputSuccess('done'); | ||
| expect(logs).toHaveLength(1); | ||
| expect(logs[0]).toContain('done'); | ||
| } finally { | ||
| console.log = origLog; | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('outputTable renders correct TOON format', () => { | ||
| it('produces tabular TOON with headers and rows', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputTable(['Slug', 'Status'], [['my-func', 'active'], ['other-func', 'inactive']]); | ||
| expect(logs[0]).toMatch(/^\[2\]\{/); | ||
| expect(logs[0]).toContain('my-func'); | ||
| expect(logs[0]).toContain('active'); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
|
|
||
| it('handles empty table showing header-only line', () => { | ||
| const logs: string[] = []; | ||
| const origLog = console.log; | ||
| console.log = (msg: string) => logs.push(msg); | ||
|
|
||
| try { | ||
| setToonMode(true); | ||
| outputTable(['Name'], []); | ||
| expect(logs).toHaveLength(1); | ||
| expect(logs[0]).toBe('[0]{Name}:'); | ||
| } finally { | ||
| console.log = origLog; | ||
| setToonMode(false); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Using
--toonalone can produce an empty, human-readable, or raw response instead of TOON. The new flag is parsed by the preAction hook, but command handlers still branch only ongetRootOpts(cmd).json:whoami --toontakes itsoutputInfobranch and both messages are then suppressed, while commands such asfunctions invokeandmetadatawrite directly withconsole.logand bypass the TOON renderer. This makes the advertised global format inconsistent unless callers already know to add--json; treating TOON as an effective structured-output mode (or explicitly rejecting/documenting it without--json) would avoid these silent/mixed responses.Prompt for AI agents