53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
const CORS_HEADERS = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
"Access-Control-Allow-Headers": "*"
|
|
};
|
|
|
|
/**
|
|
* Handle CORS preflight
|
|
*/
|
|
export async function OPTIONS() {
|
|
return new Response(null, { headers: CORS_HEADERS });
|
|
}
|
|
|
|
/**
|
|
* POST /v1/messages/count_tokens - Mock token count response
|
|
*/
|
|
export async function POST(request) {
|
|
let body;
|
|
try {
|
|
body = await request.json();
|
|
} catch {
|
|
return new Response(JSON.stringify({ error: "Invalid JSON body" }), {
|
|
status: 400,
|
|
headers: { "Content-Type": "application/json", ...CORS_HEADERS }
|
|
});
|
|
}
|
|
|
|
// Estimate token count based on content length
|
|
const messages = body.messages || [];
|
|
let totalChars = 0;
|
|
for (const msg of messages) {
|
|
if (typeof msg.content === "string") {
|
|
totalChars += msg.content.length;
|
|
} else if (Array.isArray(msg.content)) {
|
|
for (const part of msg.content) {
|
|
if (part.type === "text" && part.text) {
|
|
totalChars += part.text.length;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Rough estimate: ~4 chars per token
|
|
const inputTokens = Math.ceil(totalChars / 4);
|
|
|
|
return new Response(JSON.stringify({
|
|
input_tokens: inputTokens
|
|
}), {
|
|
headers: { "Content-Type": "application/json", ...CORS_HEADERS }
|
|
});
|
|
}
|
|
|