mirror of
https://github.com/nuxt/ui.git
synced 2026-09-14 19:51:10 +08:00
69 lines
2.8 KiB
TypeScript
69 lines
2.8 KiB
TypeScript
import { streamText, createTextStreamResponse } from 'ai'
|
|
import type { GatewayProviderOptions } from '@ai-sdk/gateway'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const { prompt, mode, language } = await readBody(event)
|
|
if (!prompt) {
|
|
throw createError({ statusCode: 400, message: 'Prompt is required' })
|
|
}
|
|
|
|
let instructions: string
|
|
let maxOutputTokens: number
|
|
|
|
const preserveMarkdown = 'IMPORTANT: Preserve all markdown formatting (bold, italic, links, etc.) exactly as in the original.'
|
|
|
|
switch (mode) {
|
|
case 'fix':
|
|
instructions = `You are a writing assistant. Fix all spelling and grammar errors in the given text. ${preserveMarkdown} Only output the corrected text, nothing else.`
|
|
maxOutputTokens = 500
|
|
break
|
|
case 'extend':
|
|
instructions = `You are a writing assistant. Extend the given text with more details, examples, and explanations while maintaining the same style. ${preserveMarkdown} Only output the extended text, nothing else.`
|
|
maxOutputTokens = 500
|
|
break
|
|
case 'reduce':
|
|
instructions = `You are a writing assistant. Make the given text more concise by removing unnecessary words while keeping the meaning. ${preserveMarkdown} Only output the reduced text, nothing else.`
|
|
maxOutputTokens = 300
|
|
break
|
|
case 'simplify':
|
|
instructions = `You are a writing assistant. Simplify the given text to make it easier to understand, using simpler words and shorter sentences. ${preserveMarkdown} Only output the simplified text, nothing else.`
|
|
maxOutputTokens = 400
|
|
break
|
|
case 'summarize':
|
|
instructions = 'You are a writing assistant. Summarize the given text concisely while keeping the key points. Only output the summary, nothing else.'
|
|
maxOutputTokens = 200
|
|
break
|
|
case 'translate':
|
|
instructions = `You are a writing assistant. Translate the given text to ${language || 'English'}. ${preserveMarkdown} Only output the translated text, nothing else.`
|
|
maxOutputTokens = 500
|
|
break
|
|
case 'continue':
|
|
default:
|
|
instructions = `You are a writing assistant providing inline autocompletions.
|
|
CRITICAL RULES:
|
|
- Output ONLY the NEW text that comes AFTER the user's input
|
|
- NEVER repeat any words from the end of the user's text
|
|
- Keep completions short (1 sentence max)
|
|
- Match the tone and style of the existing text
|
|
- ${preserveMarkdown}`
|
|
maxOutputTokens = 25
|
|
break
|
|
}
|
|
|
|
const result = streamText({
|
|
model: 'anthropic/claude-haiku-4.5',
|
|
instructions,
|
|
prompt,
|
|
maxOutputTokens,
|
|
providerOptions: {
|
|
gateway: {
|
|
caching: 'auto',
|
|
user: getChatUser(event),
|
|
tags: ['docs-completion']
|
|
} satisfies GatewayProviderOptions
|
|
}
|
|
})
|
|
|
|
return createTextStreamResponse({ stream: result.textStream })
|
|
})
|