豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 0ccc15e

Browse files
chore: add -f argument for count token tool to read from file instead (#1021)
Makes it a little easier to repeatedly run the skill counter on the contents of a file, instead of copy-pasting.
1 parent 59f6477 commit 0ccc15e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

scripts/count_tokens.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
import {readFileSync} from 'node:fs';
78
import {parseArgs} from 'node:util';
89

910
import {GoogleGenAI} from '@google/genai';
@@ -16,18 +17,28 @@ const {values, positionals} = parseArgs({
1617
type: 'string',
1718
default: 'gemini-2.5-flash',
1819
},
20+
file: {
21+
type: 'string',
22+
short: 'f',
23+
},
1924
},
2025
allowPositionals: true,
2126
});
2227

23-
if (!positionals[0]) {
24-
console.error('Usage: npm run count-tokens -- -- <text>');
28+
let contents = positionals[0];
29+
30+
if (values.file) {
31+
contents = readFileSync(values.file, 'utf8');
32+
}
33+
34+
if (!contents) {
35+
console.error('Usage: npm run count-tokens -- [-f <file>] [<text>]');
2536
process.exit(1);
2637
}
2738

2839
const response = await ai.models.countTokens({
2940
model: values.model,
30-
contents: positionals[0],
41+
contents,
3142
});
32-
console.log(`Input: ${positionals[0]}`);
43+
console.log(`Input: ${values.file || positionals[0]}`);
3344
console.log(`Tokens: ${response.totalTokens}`);

0 commit comments

Comments
 (0)