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:
@@ -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]*\}/);
|
||||
|
||||
77
packages/server/tests/local/personas-generate.test.ts
Normal file
77
packages/server/tests/local/personas-generate.test.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* UF-045 — generating a persona from a description goes through the built-in
|
||||
* proxy with the session token, and reads the proxy's OpenAI-format reply.
|
||||
* Runs against a listening server with localhost trust off; only the upstream
|
||||
* Anthropic call is faked.
|
||||
*/
|
||||
import { describe, it, expect, beforeAll, afterAll, afterEach, vi } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import { buildLocalServer } from '../../src/local/index.js';
|
||||
import { injectWithAuth } from '../test-utils.js';
|
||||
|
||||
describe('POST /api/personas/generate (UF-045)', () => {
|
||||
let server: FastifyInstance;
|
||||
let tmpDir: string;
|
||||
const realFetch = globalThis.fetch;
|
||||
const priorTrustLocalhost = process.env.WAGGLE_TRUST_LOCALHOST;
|
||||
|
||||
beforeAll(async () => {
|
||||
process.env.WAGGLE_TRUST_LOCALHOST = '0';
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-personas-gen-'));
|
||||
server = await buildLocalServer({ dataDir: tmpDir });
|
||||
await server.listen({ port: 0, host: '127.0.0.1' });
|
||||
server.vault.set('anthropic', 'sk-ant-test-key');
|
||||
// startService sets the provider status; buildLocalServer alone does not.
|
||||
server.agentState.llmProvider = {
|
||||
provider: 'anthropic-proxy',
|
||||
health: 'degraded',
|
||||
detail: 'Built-in provider proxy (verification pending)',
|
||||
checkedAt: new Date().toISOString(),
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => vi.unstubAllGlobals());
|
||||
|
||||
afterAll(async () => {
|
||||
await server.close();
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
if (priorTrustLocalhost === undefined) delete process.env.WAGGLE_TRUST_LOCALHOST;
|
||||
else process.env.WAGGLE_TRUST_LOCALHOST = priorTrustLocalhost;
|
||||
});
|
||||
|
||||
it('returns the persona the model wrote', async () => {
|
||||
const persona = {
|
||||
name: 'Release Scout',
|
||||
description: 'Tracks what changed between releases.',
|
||||
icon: '🔭',
|
||||
systemPrompt: 'You compare release notes and summarise breaking changes.',
|
||||
tools: ['web_search', 'document_processing'],
|
||||
};
|
||||
const upstream = vi.fn(async () => new Response(JSON.stringify({
|
||||
id: 'msg_persona',
|
||||
type: 'message',
|
||||
role: 'assistant',
|
||||
model: 'claude-sonnet-4-6',
|
||||
content: [{ type: 'text', text: JSON.stringify(persona) }],
|
||||
stop_reason: 'end_turn',
|
||||
usage: { input_tokens: 10, output_tokens: 40 },
|
||||
}), { status: 200, headers: { 'content-type': 'application/json' } }));
|
||||
vi.stubGlobal('fetch', (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
|
||||
return url.startsWith('https://api.anthropic.com/') ? upstream() : realFetch(input, init);
|
||||
});
|
||||
|
||||
const res = await injectWithAuth(server, {
|
||||
method: 'POST',
|
||||
url: '/api/personas/generate',
|
||||
payload: { prompt: 'An agent that watches our release notes' },
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(JSON.parse(res.body)).toEqual(persona);
|
||||
expect(upstream).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user