PWA - patched deps
This commit is contained in:
76
src/routes/api/extract/+server.ts
Normal file
76
src/routes/api/extract/+server.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { createLLM } from '$lib/server/llm';
|
||||
import { z } from 'zod';
|
||||
import { zodResponseFormat } from 'openai/helpers/zod';
|
||||
import { chromium } from 'playwright';
|
||||
import fs from 'fs';
|
||||
import { env } from '$env/dynamic/private';
|
||||
|
||||
const RecipeSchema = z.object({
|
||||
name: z.string(),
|
||||
description: z.string(),
|
||||
steps: z.array(z.string()),
|
||||
ingredients: z.array(z.object({
|
||||
item: z.string(),
|
||||
amount: z.string(),
|
||||
unit: z.string()
|
||||
}))
|
||||
});
|
||||
|
||||
export async function POST({ request }) {
|
||||
const { url } = await request.json();
|
||||
|
||||
// 1. Browser Connection
|
||||
// Fallback to localhost if env var not set (e.g. running outside docker)
|
||||
const wsEndpoint = env.PLAYWRIGHT_WS_ENDPOINT || 'ws://127.0.0.1:3000';
|
||||
console.log('Connecting to browser at:', wsEndpoint);
|
||||
|
||||
const browser = await chromium.connect(wsEndpoint);
|
||||
|
||||
// 2. Load Auth if available
|
||||
const authPath = '/app/secrets/auth.json';
|
||||
let context;
|
||||
// We check absolute path (Docker) or relative (Local)
|
||||
if (fs.existsSync(authPath)) {
|
||||
context = await browser.newContext({ storageState: authPath });
|
||||
} else if (fs.existsSync('./secrets/auth.json')) {
|
||||
context = await browser.newContext({ storageState: './secrets/auth.json' });
|
||||
} else {
|
||||
console.warn('No auth.json found. Running as guest.');
|
||||
context = await browser.newContext();
|
||||
}
|
||||
|
||||
const page = await context.newPage();
|
||||
let bodyText = '';
|
||||
|
||||
try {
|
||||
await page.goto(url, { waitUntil: 'domcontentloaded' });
|
||||
// Naive scraper attempt
|
||||
bodyText = await page.evaluate(() => document.body.innerText);
|
||||
} catch (e) {
|
||||
console.error('Scraping error:', e);
|
||||
return json({ error: 'Failed to scrape URL' }, { status: 500 });
|
||||
} finally {
|
||||
await page.close();
|
||||
await context.close();
|
||||
await browser.close();
|
||||
}
|
||||
|
||||
// 3. LLM Processing
|
||||
try {
|
||||
const { client, model } = createLLM();
|
||||
const completion = await client.beta.chat.completions.parse({
|
||||
model,
|
||||
messages: [
|
||||
{ role: "system", content: "Extract a recipe structure from this text. If it is not a recipe, return empty arrays." },
|
||||
{ role: "user", content: bodyText.substring(0, 8000) } // Limit context window
|
||||
],
|
||||
response_format: zodResponseFormat(RecipeSchema, "recipe")
|
||||
});
|
||||
|
||||
return json({ recipe: completion.choices[0].message.parsed });
|
||||
} catch (e) {
|
||||
console.error('LLM error:', e);
|
||||
return json({ error: 'Failed to parse recipe' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user