-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.mjs
More file actions
68 lines (59 loc) · 1.89 KB
/
main.mjs
File metadata and controls
68 lines (59 loc) · 1.89 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
import express from "express";
import { PrismaClient } from "./generated/prisma/index.js";
const app = express();
const client = new PrismaClient();
app.use(express.json());
app.use(express.static("./public"));
app.get("/todos", async (request, response) => {
const todos = await client.todo.findMany({ orderBy: { id: "asc" } });
response.json(todos);
});
app.post("/todos", async (request, response) => {
const todo = await client.todo.create({
data: {
title: request.body.title,
dueAt: new Date(request.body.dueAt),
},
});
response.json(todo);
});
app.delete("/todos/:id", async (request, response) => {
await client.todo.delete({
where: { id: parseInt(request.params.id) },
});
response.json({ success: true });
});
app.post("/todos/ai", async (request, response) => {
const systemPrompt = `
ユーザーの入力から、ToDoのタイトルと期限を抽出してください。
1行目にタイトル、2行目に期限(ISO8601形式、タイムゾーンは東京)を出力してください。
現在日時: ${new Date().toISOString()}
例
現在日時: 2026-01-20T12:00:00+09:00
入力: 明日の10時に会議
出力: 会議
2026-01-21T10:00:00+09:00
`;
const result = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "openrouter/free",
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: request.body.instruction },
],
}),
});
const data = await result.json();
const content = data.choices[0].message.content;
const lines = content.split("\n");
const todo = await client.todo.create({
data: { title: lines[0], dueAt: new Date(lines[1]) },
});
response.json(todo);
});
app.listen(3000);