Skip to content

Commit 0fce590

Browse files
committed
Integrate OpenAI API and update translate endpoint
1 parent daec5eb commit 0fce590

7 files changed

Lines changed: 70 additions & 12 deletions

File tree

apps/api/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ PRIVATE_KEY="0x1d65a3183f35ecef73ce8f7d47920d58abdf3766debc2ff0b4c653b7633707fd"
66
EVER_ACCESS_KEY=""
77
EVER_ACCESS_SECRET=""
88
SHARED_SECRET=""
9+
OPENROUTER_API_KEY=""

apps/api/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ declare namespace NodeJS {
99
EVER_ACCESS_SECRET: string;
1010
SHARED_SECRET: string;
1111
LIVEPEER_KEY: string;
12+
OPENROUTER_API_KEY: string;
1213
}
1314
}

apps/api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"hono-rate-limiter": "^0.4.2",
3232
"jose": "^6.0.11",
3333
"linkedom": "^0.18.10",
34+
"openai": "^5.0.2",
3435
"pg-promise": "^11.13.0",
3536
"redis": "^5.1.1",
3637
"tsx": "^4.19.4",

apps/api/src/routes/ai/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const app = new Hono();
77

88
app.post(
99
"/translate",
10-
zValidator("json", z.object({ text: z.string() })),
10+
zValidator("json", z.object({ post: z.string() })),
1111
translate
1212
);
1313

apps/api/src/routes/ai/translate.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
11
import { Errors } from "@hey/data/errors";
22
import type { Context } from "hono";
3+
import lensPg from "src/utils/lensPg";
4+
import openRouter from "src/utils/openRouter";
5+
import { z } from "zod";
6+
7+
const OpenAIResponseSchema = z.object({
8+
choices: z.array(z.object({ message: z.object({ content: z.string() }) }))
9+
});
310

411
const translate = async (ctx: Context) => {
512
try {
6-
const authHeader = ctx.req.raw.headers.get("authorization");
13+
const { post } = await ctx.req.json();
714

8-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
9-
return ctx.json({ success: false, error: "Unauthorized" }, 401);
10-
}
15+
const metadata = await lensPg.query(
16+
`
17+
SELECT content, language
18+
FROM post.metadata
19+
WHERE post = $1;
20+
`,
21+
[`\\x${post}`]
22+
);
1123

12-
const token = authHeader.split(" ")[1];
24+
const text = metadata[0]?.content;
1325

14-
if (token !== process.env.SHARED_SECRET) {
15-
return ctx.json({ success: false, error: "Invalid shared secret" }, 401);
26+
if (!text) {
27+
return ctx.json({ success: false, error: "No content found" }, 400);
1628
}
1729

18-
return ctx.json({
19-
allowed: true,
20-
sponsored: true,
21-
signingKey: process.env.PRIVATE_KEY
30+
const completion = await openRouter.chat.completions.create({
31+
model: "google/gemma-3n-e4b-it:free",
32+
messages: [
33+
{
34+
role: "user",
35+
content:
36+
"You are a translation assistant. Translate all incoming text to English, and return only the translated output."
37+
},
38+
{ role: "user", content: text }
39+
]
2240
});
41+
42+
const parsed = OpenAIResponseSchema.parse(completion);
43+
const translatedText = parsed.choices[0].message.content;
44+
45+
return ctx.json({ text: translatedText });
2346
} catch {
2447
return ctx.json({ success: false, error: Errors.SomethingWentWrong }, 500);
2548
}

apps/api/src/utils/openRouter.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import OpenAI from "openai";
2+
3+
const openRouter = new OpenAI({
4+
baseURL: "https://openrouter.ai/api/v1",
5+
apiKey: process.env.OPENROUTER_API_KEY,
6+
defaultHeaders: {
7+
"HTTP-Referer": "https://hey.xyz",
8+
"X-Title": "Hey"
9+
}
10+
});
11+
12+
export default openRouter;

pnpm-lock.yaml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)