-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex-https.js
More file actions
152 lines (139 loc) · 4.33 KB
/
index-https.js
File metadata and controls
152 lines (139 loc) · 4.33 KB
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
import { config } from "dotenv";
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import express from "express";
import https from "https";
import http from "http";
import fs from "fs";
import { init } from "@heyputer/puter.js/src/init.cjs";
import { pickModel } from "./router.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
config({ path: join(__dirname, '.env') });
const puter = init(process.env.PUTER_AUTH_TOKEN);
const app = express();
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
function extractContent(content) {
if (typeof content === "string") {
return content;
} else if (Array.isArray(content)) {
return content.map(c => c.text || c).join("");
}
return "";
}
app.post("/v1/chat/completions", async (req, res) => {
try {
let { messages, model, stream } = req.body;
if (!model || model === "auto" || model === "Auto") {
model = pickModel(messages);
}
if (!messages || !Array.isArray(messages)) {
messages = [{ role: "user", content: "" }];
}
const response = await puter.ai.chat(messages, {
model: model,
stream: false
});
const contentText = extractContent(response.message?.content);
res.json({
id: "chatcmpl-" + Date.now(),
object: "chat.completion",
created: Date.now(),
model: model,
choices: [{
index: 0,
message: { role: "assistant", content: contentText },
finish_reason: "stop"
}],
usage: response.usage || {}
});
} catch (error) {
console.error("Error:", error.message);
res.status(500).json({
error: error.message,
type: "internal_error"
});
}
});
app.post("/v1/messages", async (req, res) => {
try {
let { messages, model, max_tokens, stream, system, prompt } = req.body;
if (!model || model === "auto" || model === "Auto") {
model = "claude-opus-4-5-latest";
}
let allMessages = [];
if (system) {
allMessages.push({ role: "system", content: system });
}
if (messages && Array.isArray(messages)) {
allMessages.push(...messages);
} else if (prompt) {
allMessages.push({ role: "user", content: prompt });
}
if (allMessages.length === 0) {
allMessages.push({ role: "user", content: "" });
}
const response = await puter.ai.chat(allMessages, {
model: model,
stream: false,
max_tokens: max_tokens || 4096
});
const contentText = extractContent(response.message?.content);
const contentBlocks = contentText
? [{ type: "text", text: contentText }]
: [];
res.json({
id: response.message?.id || "msg_" + Date.now(),
type: "message",
role: "assistant",
content: contentBlocks,
model: model,
stop_reason: response.message?.stop_reason || "end_turn",
usage: response.usage || { input_tokens: 0, output_tokens: 0 }
});
} catch (error) {
console.error("Error:", error.message);
res.status(500).json({
error: error.message,
type: "error"
});
}
});
app.post("/chat", async (req, res) => {
try {
let { messages, model, stream } = req.body;
if (!model || model === "auto" || model === "Auto") {
model = pickModel(messages);
}
if (!messages || !Array.isArray(messages)) {
messages = [{ role: "user", content: "" }];
}
const response = await puter.ai.chat(messages, {
model: model,
stream: false
});
res.json(response);
} catch (error) {
console.error("Error:", error.message);
res.status(500).json({ error: error.message });
}
});
// Generate self-signed cert
const certs = {
key: fs.readFileSync('/tmp/key.pem'),
cert: fs.readFileSync('/tmp/cert.pem')
};
// Generate certs if not exist
if (!fs.existsSync('/tmp/key.pem')) {
const { execSync } = require('child_process');
execSync('openssl req -x509 -newkey rsa:2048 -keyout /tmp/key.pem -out /tmp/cert.pem -days 365 -nodes -subj "/CN=localhost"', { stdio: 'inherit' });
}
const httpServer = http.createServer(app);
const httpsServer = https.createServer(certs, app);
httpServer.listen(3333, () => {
console.log("HTTP server running on http://localhost:3333");
});
httpsServer.listen(443, () => {
console.log("HTTPS server running on https://localhost:443");
});