Files
sausage/host/js/durable-test.ts
T

48 lines
2.4 KiB
TypeScript

// Faux-model acceptance probe for credential wake-ups, including host restart.
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context';
import { createModels } from '@earendil-works/pi-ai/models';
import { fauxProvider, fauxAssistantMessage, fauxText } from '@earendil-works/pi-ai/providers/faux';
import { Harness, createRegistry } from '@earendil-works/pi-durable';
import { openStorage } from './sqlite';
import { CredentialRequests, startRequests } from './credentials';
const models = createModels();
const faux = fauxProvider({ tokensPerSecond: 100000 });
models.setProvider(faux.provider);
faux.setResponses(Array.from({ length: 8 }, () => fauxAssistantMessage([fauxText('done')])));
const registry = createRegistry();
registry.install(CredentialRequests);
const harness = await Harness.open(await openStorage(globalThis.storagePath), { models, registry }, context);
const root = await harness.root(context, { agent: { model: { provider: 'faux', modelId: 'faux-1' }, extensions: [CredentialRequests] } });
await (await root.submit({ type: 'input', content: 'initial', requestId: 'initial' }, context)).wait(context);
if (globalThis.providerOnly) {
await harness.close(context);
} else {
await Deno.core.ops.op_file('write', '/conversation', Array.from(new TextEncoder().encode(root.id)));
const stop = await startRequests(harness, context);
harness.resume();
const watch = await root.watch(context);
await new Promise<void>((resolve, reject) => {
const check = (event: any) => {
if (event.kind !== 'value') return;
const messages = event.value.entries.filter((e: any) => JSON.stringify(e).includes('Your request for'));
if (messages.length > 1) reject(new Error('Duplicate credential message'));
if (messages.length === 1) {
const message = JSON.stringify(messages);
if (!message.includes('`github`') || !message.includes(globalThis.expectedState)) reject(new Error('Wrong credential decision message'));
else resolve();
}
};
watch.start(check);
check({ kind: 'value', value: watch.value });
});
await harness.waitForIdle(context);
const final = await root.watch(context);
if (final.value.entries.filter((entry: any) => entry.kind === 'pi.assistant').length !== 2) {
throw new Error('The decision must start exactly one model turn');
}
await final.stop();
await stop();
await watch.stop();
await harness.close(context);
}