Skip to content

Commit 3f2baae

Browse files
committed
--tokenUsage
1 parent 683619d commit 3f2baae

File tree

8 files changed

+37
-28
lines changed

8 files changed

+37
-28
lines changed

packages/agent/src/core/toolAgent.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export const toolAgent = async (
238238
config = CONFIG,
239239
context: ToolContext,
240240
): Promise<ToolAgentResult> => {
241-
const { logger, tokenLevel } = context;
241+
const { logger, tokenLevel, tokenUsage } = context;
242242

243243
logger.verbose('Starting agent execution');
244244
logger.verbose('Initial prompt:', initialPrompt);
@@ -335,7 +335,9 @@ export const toolAgent = async (
335335
logger.info(assistantMessage);
336336
}
337337

338-
logger[tokenLevel](
338+
// Use the appropriate log level based on tokenUsage flag
339+
const logLevel = tokenUsage ? 'info' : tokenLevel;
340+
logger[logLevel](
339341
chalk.blue(`[Token Usage/Message] ${getTokenString(tokenPerMessage)}`),
340342
);
341343

@@ -365,7 +367,9 @@ export const toolAgent = async (
365367
tokens,
366368
interactions,
367369
};
368-
logger[tokenLevel](
370+
// Use the appropriate log level based on tokenUsage flag
371+
const logLevel = tokenUsage ? 'info' : tokenLevel;
372+
logger[logLevel](
369373
chalk.blueBright(`[Token Usage/Agent] ${getTokenString(tokens)}`),
370374
);
371375
return result;
@@ -378,7 +382,9 @@ export const toolAgent = async (
378382
tokens,
379383
interactions,
380384
};
381-
logger[tokenLevel](
385+
// Use the appropriate log level based on tokenUsage flag
386+
const logLevel = tokenUsage ? 'info' : tokenLevel;
387+
logger[logLevel](
382388
chalk.blueBright(`[Token Usage/Agent] ${getTokenString(tokens)}`),
383389
);
384390
return result;

packages/agent/src/core/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type ToolContext = {
99
workingDirectory: string;
1010
headless: boolean;
1111
tokenLevel: TokenLevel;
12+
tokenUsage?: boolean;
1213
};
1314

1415
export type Tool<TParams = Record<string, any>, TReturn = any> = {

packages/cli/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# mycoder
22

3+
## 0.2.2
4+
5+
### Patch Changes
6+
7+
- Replaced `--tokenLog` with `--tokenUsage` boolean flag that outputs token usage at info log level when enabled
8+
39
## 0.2.1
410

511
### Patch Changes

packages/cli/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mycoder --promptFile=your-prompt.txt
5353
- `-i, --interactive`: Run in interactive mode, asking for prompts
5454
- `-f, --file`: Read prompt from a specified file
5555
- `--log`: Set log level (info, verbose, warn, error)
56-
- `--tokenLog`: Output token usage at specified log level (defaults to debug)
56+
- `--tokenUsage`: Output token usage at info log level
5757
- `-h, --help`: Show help
5858
- `-V, --version`: Show version
5959

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mycoder",
33
"description": "A command line tool using agent that can do arbitrary tasks, including coding tasks",
4-
"version": "0.2.1",
4+
"version": "0.2.2",
55
"type": "module",
66
"bin": "./bin/cli.js",
77
"main": "./dist/index.js",

packages/cli/src/commands/$default.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export const command: CommandModule<object, DefaultArgs> = {
3333
const logger = new Logger({ name: 'Default' });
3434
const packageInfo = getPackageInfo();
3535

36-
const tokenLevel = (argv.tokenLog as TokenLevel) ?? 'debug';
36+
// Use 'info' log level for token logging when tokenUsage is enabled, otherwise use 'debug'
37+
const tokenLevel: TokenLevel = argv.tokenUsage ? 'info' : 'debug';
3738

3839
logger.info(
3940
`MyCoder v${packageInfo.version} - AI-powered coding assistant`,
@@ -125,6 +126,7 @@ export const command: CommandModule<object, DefaultArgs> = {
125126
headless: true,
126127
workingDirectory: '.',
127128
tokenLevel,
129+
tokenUsage: argv.tokenUsage,
128130
});
129131
const output =
130132
typeof result.result === 'string'

packages/cli/src/options.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type SharedOptions = {
44
readonly log: LogLevel;
55
readonly interactive: boolean;
66
readonly file?: string;
7-
readonly tokenLog?: string;
7+
readonly tokenUsage?: boolean;
88
};
99

1010
export const sharedOptions = {
@@ -26,9 +26,9 @@ export const sharedOptions = {
2626
alias: 'f',
2727
description: 'Read prompt from a file',
2828
} as const,
29-
tokenLog: {
30-
type: 'string',
31-
description: 'Output token usage at specified log level (defaults to debug)',
32-
choices: ['debug', 'verbose', 'info', 'warn', 'error'],
29+
tokenUsage: {
30+
type: 'boolean',
31+
description: 'Output token usage at info log level',
32+
default: false,
3333
} as const,
3434
};

packages/cli/tests/manual/tokenLog.test.js renamed to packages/cli/tests/manual/tokenUsage.test.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
#!/usr/bin/env node
22

3-
// This is a simple test script to verify the --tokenLog option works
3+
// This is a simple test script to verify the --tokenUsage option works
44

55
import { execSync } from 'child_process';
66

77
// Set up test environment
88
const testPrompt = 'Hello, can you tell me what day it is today?';
99

10-
// Test with different log levels
10+
// Test with different configurations
1111
const testCases = [
12-
{ name: 'Default (debug)', args: '--tokenLog=debug --log=debug' },
13-
{ name: 'Verbose level', args: '--tokenLog=verbose --log=verbose' },
14-
{ name: 'Info level', args: '--tokenLog=info --log=info' },
12+
{ name: 'Without tokenUsage (debug level)', args: '--log=debug' },
13+
{ name: 'With tokenUsage flag', args: '--tokenUsage --log=info' },
1514
];
1615

17-
console.log('Testing --tokenLog option:\n');
16+
console.log('Testing --tokenUsage option:\n');
1817

1918
for (const test of testCases) {
2019
console.log(`\n=== ${test.name} ===`);
2120
try {
22-
// Run the CLI with tokenLog option
21+
// Run the CLI with tokenUsage option
2322
const cmd = `node --no-deprecation bin/cli.js ${test.args} "${testPrompt}"`;
2423
console.log(`Running: ${cmd}\n`);
2524

@@ -34,17 +33,12 @@ for (const test of testCases) {
3433
console.log('\n');
3534

3635
// Check if token usage info appears in the output
37-
const hasTokenUsage = output.includes('Token usage:');
36+
const hasTokenUsage = output.includes('[Token Usage');
3837
console.log(`Token usage info present: ${hasTokenUsage ? '✅ Yes' : '❌ No'}`);
3938

40-
// Check if cached tokens info appears in the output
41-
const hasCachedTokens = output.includes('cached');
42-
console.log(`Cached tokens info present: ${hasCachedTokens ? '✅ Yes' : '❌ No'}`);
43-
44-
// Check if cache hit rate appears in the output
45-
const hasCacheHitRate = output.includes('cache hit rate');
46-
console.log(`Cache hit rate info present: ${hasCacheHitRate ? '✅ Yes' : '❌ No'}`);
47-
39+
// Check if token cost appears in the output
40+
const hasTokenCost = output.includes('COST:');
41+
console.log(`Token cost info present: ${hasTokenCost ? '✅ Yes' : '❌ No'}`);
4842
} catch (error) {
4943
console.error(`Error running test: ${error.message}`);
5044
}

0 commit comments

Comments
 (0)