Skip to content

Canvix — Free & open-source graphic design tool. A community-built Canva alternative with a drag-and-drop editor, templates, shapes, text, images & one-click export. Works on desktop, tablet & mobile.

3.4 KB
Raw Blame
import { NextRequest, NextResponse } from 'next/server'
import { withProvider } from '@/lib/ai/provider'
import { withLogging } from '@/lib/observability'

export const runtime = 'nodejs'
export const maxDuration = 60

interface TranslateBody {
  texts?: string[]
  language?: string
}

/** Robustly extract the first JSON array of strings from an LLM reply
 *  (walks closing brackets backwards to tolerate trailing junk). */
function parseTranslations(raw: string, expected: number): string[] | null {
  // strip code fences if present
  const cleaned = raw.replace(/```(?:json)?/gi, '').trim()
  const start = cleaned.indexOf('[')
  if (start === -1) return null
  let end = cleaned.lastIndexOf(']')
  while (end > start) {
    try {
      const arr = JSON.parse(cleaned.slice(start, end + 1))
      if (Array.isArray(arr) && arr.every((x) => typeof x === 'string') && arr.length === expected) {
        return arr
      }
    } catch {
      /* try an earlier closing bracket */
    }
    end = cleaned.lastIndexOf(']', end - 1)
  }
  return null
}

/** POST /api/ai/translate — Canva "Translate": translate an array of design texts. */
async function __canvix_post(req: NextRequest) {
  const body = (await req.json().catch(() => ({}))) as TranslateBody
  const language = (body.language ?? '').trim()
  const texts = Array.isArray(body.texts) ? body.texts.filter((t) => typeof t === 'string') : []

  if (!language) return NextResponse.json({ error: 'A target language is required.' }, { status: 400 })
  if (!texts.length) return NextResponse.json({ error: 'No texts to translate.' }, { status: 400 })
  if (texts.length > 300) return NextResponse.json({ error: 'Too many texts (max 300).' }, { status: 400 })
  if (texts.some((t) => t.length > 2000)) {
    return NextResponse.json({ error: 'One of the texts is too long (max 2000 chars).' }, { status: 400 })
  }

  const numbered = texts.map((t, i) => `${i + 1}. ${JSON.stringify(t)}`).join('\n')
  const system =
    'You are a professional design translator for a graphic-design tool. ' +
    'Translate each numbered text into the requested language, keeping the meaning, tone and style. ' +
    'Preserve line breaks, emoji and placeholders. Keep brand names, URLs and proper nouns untranslated. ' +
    'Adapt naturally (idiomatic), do not transliterate unless the language requires it. ' +
    'Respond ONLY with a JSON array of strings in the same order and same length as the input. No markdown, no numbering.'

  const user = `Target language: ${language}\n\nTexts:\n${numbered}\n\nRespond with a JSON array of ${texts.length} translated strings, same order.`

  return withProvider('translate', req, { limit: 15, windowSec: 60 }, async (ctx) => {
    const completion = await ctx.zai.chat.completions.create({
      model: ctx.model,
      messages: [
        { role: 'assistant', content: system },
        { role: 'user', content: user },
      ],
      thinking: { type: 'disabled' },
    })

    const reply = completion.choices[0]?.message?.content ?? ''
    const translations = parseTranslations(reply, texts.length)
    if (!translations) {
      return NextResponse.json({ error: 'Translation failed — please try again.' }, { status: 502 })
    }
    return NextResponse.json({ language, translations })
  })
}

// v0.12.5 (H7): opt-in request logging wrapper — passthrough
// unless CANVIX_LOG=requests (see src/lib/observability.ts)
export const POST = withLogging('POST /api/ai/translate', __canvix_post)