Initial commit

This commit is contained in:
decolua
2026-01-05 09:58:59 +07:00
commit 3857598de4
159 changed files with 14537 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import { handleChat } from "@/sse/handlers/chat.js";
import { initTranslators } from "open-sse/translator/index.js";
import { transformToOllama } from "open-sse/utils/ollamaTransform.js";
let initialized = false;
async function ensureInitialized() {
if (!initialized) {
await initTranslators();
initialized = true;
console.log("[SSE] Translators initialized");
}
}
export async function OPTIONS() {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "*"
}
});
}
export async function POST(request) {
await ensureInitialized();
const clonedReq = request.clone();
let modelName = "llama3.2";
try {
const body = await clonedReq.json();
modelName = body.model || "llama3.2";
} catch {}
const response = await handleChat(request);
return transformToOllama(response, modelName);
}

View File

@@ -0,0 +1,37 @@
import { callCloudWithMachineId } from "@/shared/utils/cloud.js";
import { handleChat } from "@/sse/handlers/chat.js";
import { initTranslators } from "open-sse/translator/index.js";
let initialized = false;
/**
* Initialize translators once
*/
async function ensureInitialized() {
if (!initialized) {
await initTranslators();
initialized = true;
console.log("[SSE] Translators initialized");
}
}
/**
* Handle CORS preflight
*/
export async function OPTIONS() {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "*"
}
});
}
export async function POST(request) {
// Fallback to local handling
await ensureInitialized();
return await handleChat(request);
}

View File

@@ -0,0 +1,52 @@
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 }
});
}

View File

@@ -0,0 +1,37 @@
import { handleChat } from "@/sse/handlers/chat.js";
import { initTranslators } from "open-sse/translator/index.js";
let initialized = false;
/**
* Initialize translators once
*/
async function ensureInitialized() {
if (!initialized) {
await initTranslators();
initialized = true;
console.log("[SSE] Translators initialized for /v1/messages");
}
}
/**
* Handle CORS preflight
*/
export async function OPTIONS() {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "*"
}
});
}
/**
* POST /v1/messages - Claude format (auto convert via handleChat)
*/
export async function POST(request) {
await ensureInitialized();
return await handleChat(request);
}

View File

@@ -0,0 +1,31 @@
import { handleChat } from "@/sse/handlers/chat.js";
import { initTranslators } from "open-sse/translator/index.js";
let initialized = false;
async function ensureInitialized() {
if (!initialized) {
await initTranslators();
initialized = true;
console.log("[SSE] Translators initialized for /v1/responses");
}
}
export async function OPTIONS() {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "*"
}
});
}
/**
* POST /v1/responses - OpenAI Responses API format
* Now handled by translator pattern (openai-responses format auto-detected)
*/
export async function POST(request) {
await ensureInitialized();
return await handleChat(request);
}

32
src/app/api/v1/route.js Normal file
View File

@@ -0,0 +1,32 @@
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers": "*"
};
/**
* Handle CORS preflight
*/
export async function OPTIONS() {
return new Response(null, { headers: CORS_HEADERS });
}
/**
* GET /v1 - Return models list (OpenAI compatible)
*/
export async function GET() {
const models = [
{ id: "claude-sonnet-4-20250514", object: "model", owned_by: "anthropic" },
{ id: "claude-3-5-sonnet-20241022", object: "model", owned_by: "anthropic" },
{ id: "gpt-4o", object: "model", owned_by: "openai" },
{ id: "gemini-2.5-pro", object: "model", owned_by: "google" }
];
return new Response(JSON.stringify({
object: "list",
data: models
}), {
headers: { "Content-Type": "application/json", ...CORS_HEADERS }
});
}