fix: ensure account manager initialized for count_tokens

- Add ensureInitialized() call before count_tokens handler
- Use hybrid approach: local estimation for text, API for images/docs
- This prevents "No accounts available" error on first request
This commit is contained in:
minhphuc429
2026-01-14 15:43:25 +07:00
parent df81ba5632
commit 2bdecf6e96
2 changed files with 12 additions and 2 deletions

View File

@@ -278,7 +278,7 @@ export function createCountTokensHandler(accountManager) {
const result = await countTokens(
{ messages, model, system, tools, tool_choice, thinking },
accountManager,
{ useAPI: true } // Use API for accurate token counting
{ useAPI: false } // Use local estimation by default, API for complex content (images/docs)
);
res.json(result);

View File

@@ -601,7 +601,17 @@ app.get('/v1/models', async (req, res) => {
* Count tokens endpoint - Anthropic Messages API compatible
* Uses hybrid approach: local tokenizer for text, API for complex content (images, documents)
*/
app.post('/v1/messages/count_tokens', createCountTokensHandler(accountManager));
app.post('/v1/messages/count_tokens', async (req, res) => {
try {
// Ensure account manager is initialized for API-based counting
await ensureInitialized();
} catch (error) {
// If initialization fails, handler will fall back to local estimation
logger.debug(`[TokenCounter] Account manager not initialized: ${error.message}`);
}
return createCountTokensHandler(accountManager)(req, res);
});
/**
* Main messages endpoint - Anthropic Messages API compatible