Spaces:
Running
Running
File size: 6,474 Bytes
bd26047 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | import { createServer } from "node:http";
import { randomUUID } from "node:crypto";
import { pathToFileURL } from "node:url";
import { getAuthToken, init } from "@heyputer/puter.js/src/init.cjs";
const MAX_BODY_BYTES = 1_000_000;
export function toPuterOptions(body, testMode) {
const options = {
model: body.model,
fallback: false,
normalize: true,
stream: false,
};
if (typeof body.max_tokens === "number") options.max_tokens = body.max_tokens;
if (typeof body.temperature === "number") options.temperature = body.temperature;
if (typeof body.top_p === "number") options.top_p = body.top_p;
if (Array.isArray(body.tools)) options.tools = body.tools;
if (body.tool_choice !== undefined) options.toolChoice = body.tool_choice;
return { messages: body.messages, testMode, options };
}
export function toOpenAICompletion(result, requestedModel) {
const message = result?.message;
if (!message || typeof message !== "object") {
throw new TypeError("Puter returned a response without a message");
}
const rawContent = message.content;
const content = typeof rawContent === "string" || rawContent === null
? rawContent
: Array.isArray(rawContent)
? rawContent.filter((part) => part?.type === "text").map((part) => part.text).join("")
: null;
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : undefined;
const usage = result.usage ?? {};
const details = result.usageDetails ?? {};
const promptTokens = usage.prompt_tokens ?? usage.input_tokens ?? details.inputTokens ?? 0;
const completionTokens = usage.completion_tokens ?? usage.output_tokens ?? details.outputTokens ?? 0;
return {
id: result.id ?? `puter-${randomUUID()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model: result.model ?? requestedModel,
choices: [{
index: 0,
message: {
role: message.role ?? "assistant",
content,
...(toolCalls ? { tool_calls: toolCalls } : {}),
},
finish_reason: result.finish_reason ?? (toolCalls?.length ? "tool_calls" : "stop"),
}],
usage: {
prompt_tokens: promptTokens,
completion_tokens: completionTokens,
total_tokens: usage.total_tokens ?? promptTokens + completionTokens,
},
};
}
async function readJsonBody(request) {
const chunks = [];
let size = 0;
for await (const chunk of request) {
size += chunk.length;
if (size > MAX_BODY_BYTES) {
const error = new Error("Request body is too large");
error.statusCode = 413;
throw error;
}
chunks.push(chunk);
}
try {
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
} catch {
const error = new Error("Invalid JSON request body");
error.statusCode = 400;
throw error;
}
}
function sendJson(response, statusCode, body) {
response.writeHead(statusCode, {
"content-type": "application/json",
"cache-control": "no-store",
});
response.end(JSON.stringify(body));
}
function statusCodeFor(error) {
const status = error?.statusCode ?? error?.response?.status;
return Number.isInteger(status) && status >= 400 && status <= 599 ? status : 502;
}
export async function startGateway({
port = Number(process.env.PUTER_PORT ?? 3800),
testMode = process.env.PUTER_TEST_MODE !== "0",
sdk = { getAuthToken, init },
} = {}) {
if (!testMode && process.env.PUTER_ALLOW_BILLABLE_USAGE !== "1") {
throw new Error("Set PUTER_ALLOW_BILLABLE_USAGE=1 to enable non-test Puter requests");
}
const token = process.env.PUTER_AUTH_TOKEN || await sdk.getAuthToken();
if (typeof token !== "string" || !token) {
throw new Error("Puter browser authentication did not return an auth token");
}
const puter = sdk.init(token);
const server = createServer(async (request, response) => {
if (request.method === "GET" && request.url === "/health") {
return sendJson(response, 200, { status: "ready", test_mode: testMode });
}
if (request.method !== "POST" || request.url !== "/v1/chat/completions") {
return sendJson(response, 404, { error: { message: "Not found", type: "not_found" } });
}
if (request.headers.authorization !== "Bearer puter-local") {
return sendJson(response, 401, { error: { message: "Gateway authorization required", type: "authentication_error" } });
}
if (!request.headers["content-type"]?.toLowerCase().startsWith("application/json")) {
return sendJson(response, 415, { error: { message: "JSON content type required", type: "invalid_request_error" } });
}
const origin = request.headers.origin;
if (origin && !/^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/i.test(origin)) {
return sendJson(response, 403, { error: { message: "Local origin required", type: "forbidden" } });
}
try {
const body = await readJsonBody(request);
if (!Array.isArray(body.messages) || !body.messages.length || typeof body.model !== "string") {
return sendJson(response, 400, { error: { message: "model and messages are required", type: "invalid_request_error" } });
}
if (body.stream) {
return sendJson(response, 400, { error: { message: "Streaming is not supported by this local bridge", type: "invalid_request_error" } });
}
const { messages, options } = toPuterOptions(body, testMode);
const result = await puter.ai.chat(messages, testMode, options);
return sendJson(response, 200, toOpenAICompletion(result, body.model));
} catch (error) {
const status = statusCodeFor(error);
console.error(`Puter request failed (${status}, ${error?.name ?? "Error"})`);
return sendJson(response, status, {
error: {
message: status === 429 ? "Puter rate limit reached" : "Puter request failed",
type: status === 429 ? "rate_limit_error" : "upstream_error",
},
});
}
});
await new Promise((resolve, reject) => {
server.once("error", reject);
server.listen(port, "127.0.0.1", resolve);
});
const boundPort = server.address().port;
console.log(`Puter local gateway ready on http://127.0.0.1:${boundPort}/v1 (${testMode ? "test mode" : "billable mode"})`);
return server;
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
startGateway().catch((error) => {
console.error(`Puter gateway did not start (${error?.name ?? "Error"})`);
process.exitCode = 1;
});
}
|