feat(companion): Dross persona (Cradle) + migration 008 rename; remove dead API-key path

- system prompt = Dross (Ozriel's construct fragment, per Void 1.0), with tool guidance
- migration 008 renames the seeded agent 'companion' → display name 'Dross'
- removed lib/ai/anthropic.js + lib/ai/agent/runtime.js + tests + @anthropic-ai/sdk dep (companion now runs via the claude CLI; kept lib/ai/secret.js for the Vaultwarden roadmap)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
root
2026-06-01 22:58:43 +10:00
parent d35e43a8ad
commit 8ce97bbacc
8 changed files with 29 additions and 500 deletions

View File

@@ -1,68 +0,0 @@
import { describe, it, expect } from 'vitest';
import { createRegistry } from '../../../lib/ai/agent/registry.js';
import { runTurn } from '../../../lib/ai/agent/runtime.js';
function scriptedCallModel(steps) {
let i = 0;
return async ({ onTextDelta }) => {
const step = steps[i++];
if (step.text) for (const ch of step.text) onTextDelta?.(ch);
return { text: step.text || '', toolUses: step.toolUses || [], stopReason: step.toolUses ? 'tool_use' : 'end_turn', usage: { output_tokens: 1 } };
};
}
describe('runTurn', () => {
it('runs a tool then produces a final answer', async () => {
const reg = createRegistry();
reg.registerTool({ name: 'search', description: 's', input_schema: { type: 'object', properties: {} },
handler: async () => ({ results: [{ title: 'Hit' }] }) });
const callModel = scriptedCallModel([
{ toolUses: [{ id: 'tu1', name: 'search', input: { q: 'x' } }] },
{ text: 'Found it.' }
]);
const events = [];
const out = await runTurn({
callModel, registry: reg, system: 'sys',
messages: [{ role: 'user', content: 'find x' }],
ctx: { agent: { id: 'a' }, space_id: 's' },
onEvent: e => events.push(e)
});
expect(out.text).toBe('Found it.');
expect(events.filter(e => e.type === 'tool').map(e => e.tool)).toEqual(['search']);
expect(events.some(e => e.type === 'delta' && e.text)).toBe(true);
expect(out.toolTrace[0]).toMatchObject({ tool: 'search' });
});
it('emits a draft event when propose_change runs', async () => {
const reg = createRegistry();
reg.registerTool({ name: 'propose_change', description: 'p', input_schema: { type: 'object', properties: {} },
handler: async () => ({ pending_change_id: 'pc1', applied: false, summary: 'create task "X"' }) });
const callModel = scriptedCallModel([
{ toolUses: [{ id: 'tu1', name: 'propose_change', input: {} }] },
{ text: 'Drafted.' }
]);
const events = [];
const out = await runTurn({ callModel, registry: reg, system: 's',
messages: [{ role: 'user', content: 'make a task' }],
ctx: { agent: { id: 'a' } }, onEvent: e => events.push(e) });
expect(out.draftIds).toEqual(['pc1']);
expect(events.find(e => e.type === 'draft')).toMatchObject({ pending_change_id: 'pc1' });
});
it('stops at the iteration guard', async () => {
const reg = createRegistry();
reg.registerTool({ name: 'search', description: 's', input_schema: { type: 'object', properties: {} },
handler: async () => ({ results: [] }) });
const always = async () => ({ text: '', toolUses: [{ id: 't', name: 'search', input: {} }], stopReason: 'tool_use', usage: {} });
const out = await runTurn({ callModel: always, registry: reg, system: 's',
messages: [{ role: 'user', content: 'loop' }], ctx: { agent: { id: 'a' } }, onEvent: () => {}, maxIterations: 3 });
expect(out.toolTrace.length).toBe(3);
expect(out.stoppedOnGuard).toBe(true);
});
});

View File

@@ -1,116 +0,0 @@
import { describe, it, expect } from 'vitest';
import { makeCallModel } from '../../lib/ai/anthropic.js';
// ---------------------------------------------------------------------------
// Fake client whose messages.stream() returns an async-iterable of real-SDK-
// shaped RawMessageStreamEvent objects, plus a finalMessage() method that
// resolves to the assembled Message. Shape matches @anthropic-ai/sdk@0.40.1.
// ---------------------------------------------------------------------------
function makeFakeStream({ events, finalMsg }) {
// The stream object is both async-iterable and has a finalMessage() method,
// exactly as MessageStream from the real SDK.
return {
[Symbol.asyncIterator]() {
let idx = 0;
return {
async next() {
if (idx < events.length) return { value: events[idx++], done: false };
return { value: undefined, done: true };
},
};
},
async finalMessage() {
return finalMsg;
},
};
}
const FAKE_FINAL_MESSAGE = {
content: [
{ type: 'text', text: 'Hello', citations: null },
{ type: 'tool_use', id: 'tu_1', name: 'search', input: { q: 'x' } },
],
stop_reason: 'tool_use',
usage: { input_tokens: 10, output_tokens: 5 },
};
// Events emitted during streaming: two text deltas that together form 'Hello'
const FAKE_EVENTS = [
// message_start — no text
{ type: 'message_start', message: {} },
// content_block_start for text block
{ type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } },
// text deltas
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'Hel' } },
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'lo' } },
// content_block_stop for text block
{ type: 'content_block_stop', index: 0 },
// content_block_start for tool_use block
{ type: 'content_block_start', index: 1, content_block: { type: 'tool_use', id: 'tu_1', name: 'search', input: {} } },
// input_json delta (not text — should NOT trigger onTextDelta)
{ type: 'content_block_delta', index: 1, delta: { type: 'input_json_delta', partial_json: '{"q":"x"}' } },
{ type: 'content_block_stop', index: 1 },
// message_stop
{ type: 'message_stop' },
];
const fakeClient = {
messages: {
stream(_params) {
return makeFakeStream({ events: FAKE_EVENTS, finalMsg: FAKE_FINAL_MESSAGE });
},
},
};
describe('makeCallModel', () => {
it('streams text deltas and returns stable shape', async () => {
const callModel = makeCallModel({ client: fakeClient, model: 'm', maxTokens: 512 });
const deltas = [];
const out = await callModel({
system: 'You are helpful.',
messages: [{ role: 'user', content: 'Hi' }],
tools: [],
onTextDelta: (chunk) => deltas.push(chunk),
});
// Streamed deltas must concatenate to 'Hello'
expect(deltas.join('')).toBe('Hello');
// Stable return shape
expect(out.text).toBe('Hello');
expect(out.toolUses).toHaveLength(1);
expect(out.toolUses[0]).toMatchObject({ id: 'tu_1', name: 'search' });
expect(out.toolUses[0].input).toEqual({ q: 'x' });
expect(out.stopReason).toBe('tool_use');
expect(out.usage).toMatchObject({ input_tokens: 10, output_tokens: 5 });
});
it('works without onTextDelta callback', async () => {
const callModel = makeCallModel({ client: fakeClient, model: 'm' });
const out = await callModel({
messages: [{ role: 'user', content: 'Hi' }],
});
expect(out.text).toBe('Hello');
expect(out.stopReason).toBe('tool_use');
});
it('returns empty toolUses when no tool_use blocks', async () => {
const textOnlyMsg = {
content: [{ type: 'text', text: 'Just text', citations: null }],
stop_reason: 'end_turn',
usage: { input_tokens: 5, output_tokens: 3 },
};
const textOnlyClient = {
messages: {
stream: () => makeFakeStream({ events: [], finalMsg: textOnlyMsg }),
},
};
const callModel = makeCallModel({ client: textOnlyClient, model: 'm' });
const out = await callModel({ messages: [{ role: 'user', content: 'Hi' }] });
expect(out.text).toBe('Just text');
expect(out.toolUses).toEqual([]);
expect(out.stopReason).toBe('end_turn');
});
});