-
Notifications
You must be signed in to change notification settings - Fork 701
add a new unified route for count_tokens endpoint #1293
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,57 @@ | ||||||
import { RouterError } from '../errors/RouterError'; | ||||||
import { | ||||||
constructConfigFromRequestHeaders, | ||||||
tryTargetsRecursively, | ||||||
} from './handlerUtils'; | ||||||
import { Context } from 'hono'; | ||||||
|
||||||
/** | ||||||
* Handles the '/messages' API request by selecting the appropriate provider(s) and making the request to them. | ||||||
* | ||||||
* @param {Context} c - The Cloudflare Worker context. | ||||||
* @returns {Promise<Response>} - The response from the provider. | ||||||
* @throws Will throw an error if no provider options can be determined or if the request to the provider(s) fails. | ||||||
* @throws Will throw an 500 error if the handler fails due to some reasons | ||||||
*/ | ||||||
export async function messagesCountTokensHandler( | ||||||
c: Context | ||||||
): Promise<Response> { | ||||||
try { | ||||||
let request = await c.req.json(); | ||||||
let requestHeaders = Object.fromEntries(c.req.raw.headers); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚡️ Performance Improvement Issue: Object.fromEntries creates unnecessary object copy when headers are already iterable
Suggested change
|
||||||
const camelCaseConfig = constructConfigFromRequestHeaders(requestHeaders); | ||||||
const tryTargetsResponse = await tryTargetsRecursively( | ||||||
c, | ||||||
camelCaseConfig ?? {}, | ||||||
request, | ||||||
requestHeaders, | ||||||
'messagesCountTokens', | ||||||
'POST', | ||||||
'config' | ||||||
); | ||||||
|
||||||
return tryTargetsResponse; | ||||||
} catch (err: any) { | ||||||
console.log('messagesCountTokens error', err.message); | ||||||
let statusCode = 500; | ||||||
let errorMessage = 'Something went wrong'; | ||||||
|
||||||
if (err instanceof RouterError) { | ||||||
statusCode = 400; | ||||||
errorMessage = err.message; | ||||||
} | ||||||
|
||||||
return new Response( | ||||||
JSON.stringify({ | ||||||
status: 'failure', | ||||||
message: errorMessage, | ||||||
}), | ||||||
{ | ||||||
status: statusCode, | ||||||
headers: { | ||||||
'content-type': 'application/json', | ||||||
}, | ||||||
} | ||||||
); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { MessageCreateParamsBase } from '../../types/MessagesRequest'; | ||
import { getMessagesConfig } from '../anthropic-base/messages'; | ||
|
||
export const VertexAnthropicMessagesCountTokensConfig = { | ||
...getMessagesConfig({}), | ||
model: { | ||
param: 'model', | ||
required: true, | ||
transform: (params: MessageCreateParamsBase) => { | ||
let model = params.model ?? ''; | ||
return model.replace('anthropic.', ''); | ||
}, | ||
}, | ||
}; |
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.
📝 Documentation Gap
Issue: JSDoc comment describes '/messages' API but function handles '/messages/count_tokens' endpoint
Fix: Update documentation to match actual functionality
Impact: Prevents developer confusion about handler purpose