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:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user