Open-source, local-first AI agent for real work.
TypeScript agent-toolsai-agentbyokcoding-agentcross-platformdesktop-appdeveloper-toolselectronharnessknowledge-basellmwikilocal-firstmcpplugin-systemprivacy-firstskillstypescriptvettaworkflow-automation
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { type Api, type AssistantMessage, type AssistantMessageEvent, EventStream, type Model } from "@vetta/ai";
import {
type CodingAgentRuntimeComposition,
createCodingAgentRuntimeHostSessionConfig,
} from "@vetta/coding-agent/composition";
import { CODING_AGENT_ASK_USER_QUESTION_FUNCTION } from "@vetta/coding-agent/function-extensions";
import type { CodingAgentPluginRuntimeSource, CodingAgentRuntimeModelSource } from "@vetta/coding-agent/host-services";
import type {
AgentPluginContinuationInvocation,
AgentPluginSystemPromptInvocation,
AgentPluginToolInvocation,
} from "@vetta/coding-agent/plugin-runtime";
import { RuntimeHost } from "@vetta/runtime-core";
import { SessionExtensionFunctionRegistry } from "@vetta/runtime-core/session-extensions";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
createCodingAgentRuntimeComposition,
createUnsandboxedTestSessionExecutionEnvironment,
} from "./fixtures/runtime-composition.js";
const INTEGRATION_TEST_TIMEOUT_MS = 30_000;
describe("Runtime Host capabilities contract", { timeout: INTEGRATION_TEST_TIMEOUT_MS }, () => {
const directories: string[] = [];
const disposers: Array<() => Promise<void>> = [];
afterEach(async () => {
for (const dispose of disposers.splice(0).reverse()) await dispose();
for (const directory of directories.splice(0).reverse()) {
await rm(directory, { recursive: true, force: true });
}
}, INTEGRATION_TEST_TIMEOUT_MS);
it("maps plugin prompt, tool and continuation invokers and applies live reconfiguration", async () => {
const cwd = await temporaryDirectory("runtime-host-plugin-workspace-");
const conversationDir = await temporaryDirectory("runtime-host-plugin-conversations-");
const modelCalls: Array<{ readonly prompt?: string; readonly tools: readonly string[] }> = [];
const responses = [
assistantToolCall("plugin_artifact", { title: "Report", md_intro: "Finding" }),
assistantText("artifact complete"),
assistantText("plugin continuation complete"),
assistantText("plugins disabled"),
];
let responseIndex = 0;
const toolInvocations: AgentPluginToolInvocation[] = [];
const systemPromptInvocations: AgentPluginSystemPromptInvocation[] = [];
const continuationInvocations: AgentPluginContinuationInvocation[] = [];
let currentPluginConfiguration: ReturnType<CodingAgentPluginRuntimeSource["readAgentPlugins"]> =
pluginConfiguration();
const pluginListeners = new Set<() => void>();
const pluginRuntime: CodingAgentPluginRuntimeSource = {
readAgentPlugins: () => currentPluginConfiguration,
subscribe: (listener) => {
pluginListeners.add(listener);
return () => pluginListeners.delete(listener);
},
invokeTool: async (invocation) => {
toolInvocations.push(invocation);
return { value: { text: "artifact created" }, effects: [] };
},
invokeSystemPrompt: async (invocation) => {
systemPromptInvocations.push(invocation);
return [
{
type: "addBlock",
block: {
id: "plugin.runtime-host",
type: "plugin",
source: { kind: "plugin" },
content: "RuntimeHost plugin instruction",
priority: 700,
enabled: true,
},
},
];
},
invokeContinuation: async (invocation) => {
continuationInvocations.push(invocation);
return continuationInvocations.length === 1
? {
value: { text: "runtime host continuation", idempotencyKey: "runtime-host-once" },
effects: [],
}
: { value: null, effects: [] };
},
};
const composition = await createCodingAgentRuntimeComposition({
conversationDir,
cwd,
createSessionExecutionEnvironment: createUnsandboxedTestSessionExecutionEnvironment,
scenario: "batch",
enableSubagents: false,
activation: { mode: "explicit", toolNames: ["plugin_artifact"] },
modelRegistry: modelRegistry(),
initialModel: MODEL,
initialThinkingLevel: "off",
createPluginRuntime: () => pluginRuntime,
resolveSystemPromptOptions: () => ({ customPrompt: "Base prompt", scenario: "batch" }),
streamFn: (_model, context) => {
modelCalls.push({
prompt: context.systemPrompt,
tools: (context.tools ?? []).map(({ name }) => name),
});
const response = responses[responseIndex++];
if (!response) throw new Error("Missing recorded response");
return new RecordedAssistantStream(response);
},
});
const runtime = new RuntimeHost({ sessionBackend: composition.runtimeHostBackend });
registerDisposal(runtime, composition);
const created = await runtime.createSession(
createCodingAgentRuntimeHostSessionConfig(
composition.agentRuntime,
{ sessionId: "plugin-runtime-host", cwd },
{ sessionDir: conversationDir, scenario: "batch" },
),
);
const activeToolSnapshots: string[][] = [];
const unsubscribe = runtime.subscribe(created.sessionId, (event) => {
if (event.type === "active_tools_update") activeToolSnapshots.push([...event.activeToolNames]);
});
await runtime.prompt(created.sessionId, { text: "create artifact" });
expect(toolInvocations).toHaveLength(1);
expect(continuationInvocations).toHaveLength(2);
expect(systemPromptInvocations).toHaveLength(1);
expect(modelCalls).toHaveLength(3);
expect(modelCalls[0]?.tools).toContain("plugin_artifact");
expect(modelCalls[0]?.prompt).toContain("RuntimeHost plugin instruction");
currentPluginConfiguration = undefined;
for (const listener of pluginListeners) listener();
await runtime.prompt(created.sessionId, { text: "continue without plugins" });
expect(modelCalls).toHaveLength(4);
expect(modelCalls[3]?.tools).not.toContain("plugin_artifact");
expect(modelCalls[3]?.prompt).not.toContain("RuntimeHost plugin instruction");
expect(systemPromptInvocations).toHaveLength(1);
expect(activeToolSnapshots.at(-1)).not.toContain("plugin_artifact");
unsubscribe();
});
it("toggles ask_user_question per model call without rebuilding the session", async () => {
const cwd = await temporaryDirectory("runtime-host-question-workspace-");
const conversationDir = await temporaryDirectory("runtime-host-question-conversations-");
const toolSurfaces: string[][] = [];
const responses = [
assistantToolCall("ask_user_question", {
description: "Choose the implementation",
questions: [
{
question: "Which implementation should be used?",
header: "Implementation",
options: [
{ label: "A", description: "Use implementation A" },
{ label: "B", description: "Use implementation B" },
],
},
],
}),
assistantText("answer received"),
assistantText("question tool disabled"),
];
let responseIndex = 0;
const functions = new SessionExtensionFunctionRegistry();
disposers.push(async () => functions.close());
const composition = await createCodingAgentRuntimeComposition({
conversationDir,
cwd,
createSessionExecutionEnvironment: createUnsandboxedTestSessionExecutionEnvironment,
scenario: "conversation",
enableSubagents: true,
activation: { mode: "explicit", toolNames: [] },
modelRegistry: modelRegistry(),
initialModel: MODEL,
initialThinkingLevel: "off",
sessionExtensionFunctions: functions,
resolveSystemPromptOptions: () => ({ customPrompt: "Base prompt", scenario: "conversation" }),
streamFn: (_model, context) => {
toolSurfaces.push((context.tools ?? []).map(({ name }) => name));
const response = responses[responseIndex++];
if (!response) throw new Error("Missing recorded response");
return new RecordedAssistantStream(response);
},
});
const runtime = new RuntimeHost({ sessionBackend: composition.runtimeHostBackend });
const questionHandler = vi.fn(async () => ({
cancelled: false,
answers: [{ question: "Which implementation should be used?", answers: ["A"] }],
}));
const unregisterQuestion = functions.register(CODING_AGENT_ASK_USER_QUESTION_FUNCTION, questionHandler);
registerDisposal(runtime, composition);
const created = await runtime.createSession(
createCodingAgentRuntimeHostSessionConfig(
composition.agentRuntime,
{ sessionId: "question-runtime-host", cwd },
{
sessionDir: conversationDir,
scenario: "conversation",
},
),
);
await runtime.prompt(created.sessionId, { text: "ask me" });
expect(questionHandler).toHaveBeenCalledOnce();
expect(toolSurfaces[0]).toContain("ask_user_question");
expect(toolSurfaces[1]).toContain("ask_user_question");
expect(runtime.getState(created.sessionId).activeToolNames).toContain("ask_user_question");
unregisterQuestion();
await runtime.prompt(created.sessionId, { text: "continue" });
expect(toolSurfaces[2]).not.toContain("ask_user_question");
expect(runtime.getState(created.sessionId).activeToolNames).not.toContain("ask_user_question");
expect(runtime.getMessages(created.sessionId).find(({ role }) => role === "toolResult")).toMatchObject({
content: [{ type: "text", text: expect.stringContaining('"Which implementation should be used?"="A"') }],
});
});
function registerDisposal(runtime: RuntimeHost, composition: CodingAgentRuntimeComposition): void {
disposers.push(async () => {
await runtime.disposeAllSessions();
await composition.dispose();
});
}
async function temporaryDirectory(prefix: string): Promise<string> {
const directory = await mkdtemp(join(tmpdir(), prefix));
directories.push(directory);
return directory;
}
});
function pluginConfiguration(): NonNullable<ReturnType<CodingAgentPluginRuntimeSource["readAgentPlugins"]>> {
return {
systemPromptProviderContributions: [
{
pluginId: "plugin-a",
id: "runtime-host-prompt",
handlerId: "runtime-host-prompt-handler",
context: { conversation: "messages", systemPrompt: "rendered" },
},
],
toolContributions: [
{
pluginId: "plugin-a",
id: "artifact",
name: "plugin_artifact",
label: "Artifact",
description: "Create an artifact",
parameters: {
type: "object",
properties: { title: { type: "string" } },
required: ["title"],
},
handlerId: "artifact-handler",
context: { conversation: "messages" },
},
],
continuationContributions: [
{
pluginId: "plugin-a",
id: "continue",
handlerId: "continue-handler",
context: { conversation: "messages" },
},
],
};
}
class RecordedAssistantStream extends EventStream<AssistantMessageEvent, AssistantMessage> {
constructor(message: AssistantMessage) {
super(
(event) => event.type === "done" || event.type === "error",
(event) => {
if (event.type === "done") return event.message;
if (event.type === "error") return event.error;
throw new Error("Unexpected assistant event");
},
);
queueMicrotask(() => {
this.push({ type: "done", reason: successfulStopReason(message), message });
});
}
}
function modelRegistry(): CodingAgentRuntimeModelSource {
return {
refresh() {},
getAvailable: () => [MODEL],
find: (provider, modelId) => (provider === MODEL.provider && modelId === MODEL.id ? MODEL : undefined),
getApiKey: async () => "test-key",
setServerToken() {},
loadRemoteModels: async () => undefined,
};
}
function assistantToolCall(name: string, args: Readonly<Record<string, unknown>>): AssistantMessage {
return assistantMessage([{ type: "toolCall", id: `${name}-call`, name, arguments: args }], "toolUse");
}
function assistantText(text: string): AssistantMessage {
return assistantMessage([{ type: "text", text }], "stop");
}
function assistantMessage(
content: AssistantMessage["content"],
stopReason: AssistantMessage["stopReason"],
): AssistantMessage {
return {
role: "assistant",
content,
api: MODEL.api,
provider: MODEL.provider,
model: MODEL.id,
usage: {
input: 1,
output: 1,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 2,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
stopReason,
timestamp: 2,
};
}
function successfulStopReason(message: AssistantMessage): "length" | "stop" | "toolUse" {
if (message.stopReason === "length" || message.stopReason === "stop" || message.stopReason === "toolUse") {
return message.stopReason;
}
throw new Error(`Recorded assistant message did not complete successfully: ${message.stopReason}`);
}
const MODEL: Model<Api> = {
id: "recorded-model",
name: "Recorded Model",
api: "openai-responses",
provider: "test",
baseUrl: "https://example.test",
reasoning: true,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 8_000,
maxTokens: 1_000,
};