diff --git a/docs/oss/upstream-fixes.md b/docs/oss/upstream-fixes.md index ebc5197..a562928 100644 --- a/docs/oss/upstream-fixes.md +++ b/docs/oss/upstream-fixes.md @@ -73,7 +73,7 @@ Baseline: commit `b20b138` of `git.os-dev.tech/oleg.m/waggle-os`. | UF-042 | Sidebar hints repeat the visible labels and are partly hidden behind the main area | Fixed | Web UI | | UF-043 | The chat composer draws two focus outlines | Fixed | Web UI | | UF-044 | A working API key shows as not responding, and other internal model calls fail | Fixed | Sidecar, models, auth | -| UF-045 | Generating a persona from a description calls a route that does not exist | Open | Sidecar, agents | +| UF-045 | Generating a persona from a description calls a route that does not exist | Fixed | Sidecar, agents | ## Entries @@ -448,10 +448,10 @@ Entries UF-010 to UF-019 come from the UI audit `anti-slop/audit-001-2026-10-14. - Recommendation: apply the same change in the main project, and run the auth-sensitive suites with localhost trust off. ### UF-045. Generating a persona from a description calls a route that does not exist -- Status: Open +- Status: Fixed - Found: 2026-10-26 - Where: at commit `b20b138`, `packages/server/src/local/routes/personas.ts:160` (`POST /api/personas/generate`, used by the agent templates view) - Symptom: generating an agent persona from a text description cannot get an answer from the model. -- Cause: the route posts to `/api/anthropic/v1/messages`, which no route serves. The request also has no session token, and its port comes from `address().toString()`, which is `[object Object]`. -- What we did: nothing yet. The fix means moving the call to the built-in proxy (`/v1/chat/completions` through `self-request.ts`) and reading its OpenAI-format reply. -- Recommendation: move the call to the built-in proxy in the main project as well. +- Cause: the route posts to `/api/anthropic/v1/messages`, which no route serves. The request also has no session token, and its port comes from `address().toString()`, which is `[object Object]`, so the URL does not even parse. The model was fixed to `claude-sonnet-4-6`, so without an Anthropic key the call could not work either. +- What we did: the route calls the built-in proxy (`/v1/chat/completions` through `self-request.ts`, UF-044) and reads its OpenAI-format reply with `parseOpenAiTextCompletion`. The model starts from the user's default and falls back through `resolveUsableModel`, the way chat does, so a local Ollama model works too. An end-to-end test with localhost trust off checks that the persona the model wrote comes back; before the fix it got HTTP 500. +- Recommendation: apply the same change in the main project. diff --git a/packages/server/src/local/routes/personas.ts b/packages/server/src/local/routes/personas.ts index 08b7261..e10f46f 100644 --- a/packages/server/src/local/routes/personas.ts +++ b/packages/server/src/local/routes/personas.ts @@ -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]*\}/); diff --git a/packages/server/tests/local/personas-generate.test.ts b/packages/server/tests/local/personas-generate.test.ts new file mode 100644 index 0000000..c0494d7 --- /dev/null +++ b/packages/server/tests/local/personas-generate.test.ts @@ -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); + }); +});