|
1 | 1 | import { Errors } from "@hey/data/errors"; |
2 | 2 | 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 | +}); |
3 | 10 |
|
4 | 11 | const translate = async (ctx: Context) => { |
5 | 12 | try { |
6 | | - const authHeader = ctx.req.raw.headers.get("authorization"); |
| 13 | + const { post } = await ctx.req.json(); |
7 | 14 |
|
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 | + ); |
11 | 23 |
|
12 | | - const token = authHeader.split(" ")[1]; |
| 24 | + const text = metadata[0]?.content; |
13 | 25 |
|
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); |
16 | 28 | } |
17 | 29 |
|
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 | + ] |
22 | 40 | }); |
| 41 | + |
| 42 | + const parsed = OpenAIResponseSchema.parse(completion); |
| 43 | + const translatedText = parsed.choices[0].message.content; |
| 44 | + |
| 45 | + return ctx.json({ text: translatedText }); |
23 | 46 | } catch { |
24 | 47 | return ctx.json({ success: false, error: Errors.SomethingWentWrong }, 500); |
25 | 48 | } |
|
0 commit comments