fix(server): generate personas through the built-in proxy (UF-045)

POST /api/personas/generate posted to /api/anthropic/v1/messages, a route
that does not exist, with no session token and a port built from
address().toString(), so the URL did not even parse. It also pinned
claude-sonnet-4-6, which needs an Anthropic key.

The route now calls the built-in proxy through self-request.ts, reads the
OpenAI-format reply, and picks the model from the user's default with the
same fallback chat uses, so a local Ollama model works too. An end-to-end
test with localhost trust off checks the generated persona comes back.
This commit is contained in:
shumeiko.it
2026-10-26 01:50:47 +01:00
parent d6217ad7c2
commit 48bdab8087
3 changed files with 96 additions and 12 deletions

View File

@@ -1,14 +1,18 @@
import type { FastifyPluginAsync } from 'fastify';
import { z } from 'zod';
import { WaggleConfig } from '@waggle/core';
import {
listPersonas,
getPersona,
saveCustomPersona,
deleteCustomPersona,
isValidCustomPersonaId,
parseOpenAiTextCompletion,
type AgentPersona,
} from '@waggle/agent';
import { validateBody } from '../../validate-body.js';
import { resolveUsableModel } from '../model-availability.js';
import { selfBaseUrl, selfRequestHeaders } from '../self-request.js';
const customPersonaIdSchema = z.string().max(200).refine(isValidCustomPersonaId, {
message: 'Invalid custom persona ID',
@@ -156,13 +160,17 @@ User's description: "${body.prompt}"
Respond with ONLY valid JSON, no markdown or explanation.`;
// Use the built-in Anthropic proxy or LiteLLM
const chatUrl = `http://localhost:${fastify.server.address()?.toString().split(':').pop() ?? '3333'}/api/anthropic/v1/messages`;
const res = await fetch(chatUrl, {
// The built-in proxy routes every configured provider, Ollama included.
// Start from the user's default model and fall back the way chat does.
const model = await resolveUsableModel(
fastify,
new WaggleConfig(fastify.localConfig.dataDir).getDefaultModel() || 'claude-sonnet-4-6',
);
const res = await fetch(`${selfBaseUrl(fastify)}/v1/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: selfRequestHeaders(fastify),
body: JSON.stringify({
model: 'claude-sonnet-4-6',
model,
max_tokens: 2000,
messages: [{ role: 'user', content: metaPrompt }],
}),
@@ -172,8 +180,7 @@ Respond with ONLY valid JSON, no markdown or explanation.`;
return reply.code(502).send({ error: 'LLM request failed' });
}
const result = await res.json() as { content?: Array<{ text?: string }> };
const text = result.content?.[0]?.text ?? '';
const text = parseOpenAiTextCompletion(await res.json()).content;
// Extract JSON from response
const jsonMatch = text.match(/\{[\s\S]*\}/);