Spaces:
Runtime error
Runtime error
File size: 7,938 Bytes
b7c01c9 6968b8e b7c01c9 4b76377 92f8220 b7c01c9 |
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
import express from 'express';
import Claude from 'claude-ai';
import { readFileSync } from 'fs';
import bodyParser from 'body-parser';
const app = express();
app.set('json spaces', 2)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//const version = JSON.parse(readFileSync('package.json', 'utf-8')).version + ` (Claude v${JSON.parse(readFileSync('../package.json', 'utf-8')).version})`;
const version = 1;
// Initialize Claude
//const claude = new Claude({
// sessionKey: ''//process.env.CLAUDE_KEY
//});
let claude;
app.get('/k', (req, res)=>{
let k = req.query.k;
claude = new Claude({sessionKey: k});
res.send();
});
app.get('/', (req, res) => {
res.type('text/plain');
res.send(`
Claude REST API v${version}
Routes:
- GET /conversations - Get conversations
- GET /conversations/:id - Get conversation detail
- POST /conversations/:id - Send message
- POST /conversations/:id/files - Upload file
- GET /conversations/:id/files - Get files
- DELETE /conversations/:id - Delete conversation
- PATCH /conversations/:id - Rename conversation
- POST /ask - Sync ask
`.split('\n').map(i => i.trim()).map(i => i.startsWith('-') ? ' ' + i : i).join('\n'));
});
app.get('/version', (req, res) => {
res.type('text/plain')
res.send(version);
});
app.use(async (req, res, next) => {
if (!claude.ready) {
await claude.init();
}
next();
})
app.get('/organizations', async (req, res) => {
try {
const organizations = await claude.getOrganizations();
res.json(organizations);
} catch (err) {
res.status(500).send({ error: 'Failed to get organizations' });
}
})
// Get conversations
app.get('/conversations', async (req, res) => {
try {
const conversations = await claude.getConversations();
res.json(conversations);
} catch (err) {
console.log(err)
res.status(500).send({ error: 'Failed to get conversations' });
}
});
// Get conversation details
app.get('/conversations/:id', async (req, res) => {
try {
const conversation = await claude.getConversation(req.params.id);
if (!conversation) {
return res.status(404).send({ error: 'Conversation not found' });
}
res.json(await conversation.getInfo());
} catch (err) {
res.status(500).send({ error: 'Failed to get conversation' });
}
});
// Delete conversation
app.delete('/conversations/:id', async (req, res) => {
try {
const conversation = await claude.getConversation(req.params.id);
if (!conversation) {
return res.status(404).send({ error: 'Conversation not found' });
}
await conversation.delete();
res.sendStatus(204);
} catch (err) {
res.status(500).send({ error: 'Failed to delete conversation' });
}
});
app.delete('/conversations', async (req, res) => {
try {
await claude.clearConversations();
res.sendStatus(204);
} catch (err) {
res.status(500).send({ error: 'Failed to delete conversations' });
}
})
// Rename conversation
app.patch('/conversations/:id', async (req, res) => {
try {
const conversation = await claude.getConversation(req.params.id);
if (!conversation) {
return res.status(404).send({ error: 'Conversation not found' });
}
await conversation.rename(req.body.name);
res.sendStatus(204);
} catch (err) {
res.status(500).send({ error: 'Failed to rename conversation' });
}
});
// Send message
app.post('/conversations/:id', async (req, res) => {
try {
const conversation = await claude.getConversation(req.params.id);
if (!conversation) {
return res.status(404).send({ error: 'Conversation not found' });
}
// Stream progress
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Connection', 'keep-alive');
res.flushHeaders();
const stream = conversation.sendMessage(req.body.message, {
progress: (data) => res.write(`data: ${JSON.stringify(data)}\n\n`)
});
stream.then(data => {
res.write(`data: ${JSON.stringify(data)}\n\n`);
res.end();
});
} catch (err) {
res.status(500).send({ error: 'Failed to send message' });
}
});
// Get files for conversation
app.get('/conversations/:id/files', async (req, res) => {
try {
const conversation = await claude.getConversation(req.params.id);
if (!conversation) {
return res.status(404).send({ error: 'Conversation not found' });
}
const files = await conversation.getFiles();
res.json(files);
} catch (err) {
res.status(500).send({ error: 'Failed to get files' });
}
});
// Upload file
app.post('/conversations/:id/files', async (req, res) => {
try {
const conversation = await claude.getConversation(req.params.id);
if (!conversation) {
return res.status(404).send({ error: 'Conversation not found' });
}
const file = req.files.file;
const result = await conversation.uploadFile(file);
res.json(result);
} catch (err) {
res.status(500).send({ error: 'Failed to upload file' });
}
});
// Message feedback
app.post('/conversations/:conversationId/messages/:messageId/feedback', async (req, res) => {
try {
const conversation = await claude.getConversation(req.params.conversationId);
if (!conversation) {
return res.status(404).send({ error: 'Conversation not found' });
}
const message = conversation.getMessage(req.params.messageId);
if (!message) {
return res.status(404).send({ error: 'Message not found' });
}
await message.sendFeedback(req.body.type, req.body.reason);
res.sendStatus(204);
} catch (err) {
res.status(500).send({ error: 'Failed to send feedback' });
}
});
// Get messages
app.get('/conversations/:conversationId/messages', async (req, res) => {
try {
const conversation = await claude.getConversation(req.params.conversationId);
if (!conversation) {
return res.status(404).send({ error: 'Conversation not found' });
}
const messages = await conversation.getMessages();
res.json(messages);
} catch (err) {
res.status(500).send({ error: 'Failed to get messages' });
}
});
// Retry conversation
app.post('/conversations/:conversationId/retry', async (req, res) => {
try {
const conversation = await claude.getConversation(req.params.conversationId);
if (!conversation) {
return res.status(404).send({ error: 'Conversation not found' });
}
await conversation.retry();
res.sendStatus(204);
} catch (err) {
res.status(500).send({ error: 'Failed to retry conversation' });
}
});
// Sync ask route
app.post('/ask', async (req, res) => {
try {
const conversation = await claude.startConversation(req.body.message);
const response = await conversation.sendMessage(req.body.message);
res.json(response);
} catch (err) {
res.status(500).send({ error: 'Failed to get response' });
}
});
app.post('/ask1', async (req, res) => {
try {
const conversation = await claude.startConversation(req.body.message);
res.json(conversation);
} catch (err) {
res.status(500).send({ error: 'Failed to get response' });
}
});
const PORT = process.env.PORT || 7860;
app.listen(PORT, () => {
console.log(`Claude REST API v${version} running on port ${PORT}`);
});
|