Files
sausage/host/js/main.ts
T

71 lines
3.5 KiB
TypeScript

// Durable root conversation and console. SQLite stays outside the world.
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context';
import { proxyModels } from './proxy-models';
import { Harness, AgentDoc, createRegistry } from '@earendil-works/pi-durable';
import { CodingTools } from './environment';
import { openStorage } from './sqlite';
import { environment, conversationChains } from './environment';
import { CredentialRequests, startRequests } from './credentials';
import { serve } from './input';
import { harnessApi } from './harness-api';
import { loadExtensions } from './extensions';
const proxy = Deno.core.ops.op_model_proxy();
if (!proxy?.base_url || !proxy?.api_key) throw new Error('The llm-proxy credential is required');
const models = await proxyModels(proxy);
const model = models.getModel('openai', 'gpt-6.1-sol');
const registry = createRegistry();
registry.install(CodingTools);
registry.install(CredentialRequests);
export const storage = await openStorage(globalThis.storagePath);
const harness = await Harness.open(storage, {
models, registry, settings: { followUpMode: 'all', get extensions() { return registry.snapshot().installed(); } },
env: async (target: any) => environment(target.cwd ?? '/', await chain(target.conversationId)),
}, context);
const chain = conversationChains(harness, context);
const root = await harness.root(context, { agent: { model: { provider: 'openai', modelId: model.id }, thinkingLevel: 'low', cwd: '/' } });
// T32 stored this exact built-in list in the console root. Use registry defaults now.
const storedAgent = await harness.snapshot(AgentDoc, root.id, context);
if (Array.isArray(storedAgent?.extensions) && storedAgent.extensions.length === 2 &&
[CodingTools, CredentialRequests].every(extension => storedAgent.extensions.includes(extension.name))) {
await root.configure({ extensions: null }, context);
}
const watch = await root.watch(context);
const emitted = new Map<number, string>();
let partial = '';
const text = (message: any) => message?.content?.filter((b: any) => b.type === 'text').map((b: any) => b.text).join('') ?? '';
const render = (view: any) => {
for (const entry of view.entries) {
if (entry.kind !== 'pi.assistant') continue;
const value = text(entry.model?.[0]);
if (emitted.has(entry.id)) continue;
if (value.startsWith(partial)) Deno.core.ops.op_output(value.slice(partial.length));
else Deno.core.ops.op_output(value);
partial = '';
emitted.set(entry.id, value);
}
const live = text(view.docs['pi.live']?.generation?.message);
if (live.startsWith(partial)) Deno.core.ops.op_output(live.slice(partial.length));
partial = live;
};
// Do not replay the historical transcript to the console.
for (const entry of watch.value.entries) {
if (entry.kind === 'pi.assistant') emitted.set(entry.id, text(entry.model?.[0]));
}
if (globalThis.consoleEnabled) watch.start((event: any) => { if (event.kind === 'value') render(event.value); });
const stopRequests = await startRequests(harness, context);
const extensions = await loadExtensions(harness, registry, storage, models, context, [CodingTools, CredentialRequests]);
const api = harnessApi(harness, models, extensions, context);
harness.resume();
await serve(harness, context, globalThis.consoleEnabled ? async () => {
await harness.waitForIdle(context);
const final = await root.watch(context);
render(final.value);
await final.stop();
Deno.core.ops.op_output('\n');
} : undefined, api);
await api.close();
await extensions.close();
await stopRequests();
await watch.stop();
await harness.close(context);