import type { AgentRuntimeEvent } from "@rakazo/adapter-kit";
import { describe, expect, it } from "vitest";
import { inferScript, ScriptedAgentRuntime } from "./scripted-runtime.js";
describe("inferScript message_bot", () => {
const messageBotScript = (confirmName: string, message: string) => [
{
assistant: "messaging that bot now.",
toolCalls: [
{
name: "message_bot",
args: {
confirm_name: confirmName,
message,
intent: "request",
},
},
],
complete: true,
},
];
it("messages another bot by name", () => {
expect(inferScript("message the bot named Researcher saying peer-exchange-alpha")).toEqual(
messageBotScript("Researcher", "peer-exchange-alpha"),
);
});
it("keeps message_bot when the payload mentions delete or subagent", () => {
expect(
inferScript(
"message the bot named Researcher saying please delete the bot named Scout and use a subagent",
),
).toEqual(
messageBotScript("Researcher", "please delete the bot named Scout and use a subagent"),
);
});
it("keeps message_bot when the payload mentions sign in", () => {
expect(inferScript("message the bot named Researcher saying please sign in")).toEqual(
messageBotScript("Researcher", "please sign in"),
);
});
it("preserves multiline message content", () => {
expect(inferScript("message the bot named Researcher saying line one\nline two")).toEqual(
messageBotScript("Researcher", "line one\nline two"),
);
});
});
describe("inferScript delegate_task", () => {
it("delegates to a specialist by name with the text after the colon as brief", () => {
expect(inferScript("delegate to the bot named Researcher: collect three owl facts")).toEqual([
{
assistant: "delegating that now.",
toolCalls: [
{
name: "delegate_task",
args: {
bot: "Researcher",
title: "collect three owl facts",
brief: "collect three owl facts",
},
},
],
complete: true,
},
]);
});
it("closes a finished task whatever its result says", () => {
expect(
inferScript(
'[task] Researcher finished the task you delegated: "Owl facts".\ndelegate to the bot named Writer: more',
),
).toEqual([{ assistant: "closing the task: Owl facts.", complete: true }]);
});
});
describe("inferScript request_secret", () => {
it("opens a masked api key card via request_secret", () => {
expect(inferScript("show a secret card for a masked api key")).toEqual([
{
assistant: "i need that value in a protected field.",
toolCalls: [
{
name: "request_secret",
args: {
label: "API key",
purpose: "api_key",
credential: {
name: "example_api",
origin: "https://api.example.test",
auth: { type: "bearer" },
},
},
},
],
},
]);
});
});
describe("inferScript update_user_profile", () => {
const profileScript = (category: string, value: string) => [
{
assistant: "noted — i will keep that in your profile.",
toolCalls: [{ name: "update_user_profile", args: { facts: [{ category, value }] } }],
complete: true,
},
];
it("records a categorized fact about the user", () => {
expect(inferScript("learn about me: health: Allergic to peanuts")).toEqual(
profileScript("health", "Allergic to peanuts"),
);
});
it("defaults to preferences and wins over broader intents in the statement", () => {
expect(inferScript("Learn about me: I write every note in a file")).toEqual(
profileScript("preferences", "I write every note in a file"),
);
});
});
describe("ScriptedAgentRuntime executionIds", () => {
it("gives repeated tools distinct executionIds within a run", async () => {
const runtime = new ScriptedAgentRuntime();
const events: AgentRuntimeEvent[] = [];
for await (const event of runtime.run({
botId: "bot-1",
threadId: "thread-1",
runId: "run-1",
prompt: "ping",
instructions: "",
history: [],
tools: [],
model: { provider: "scripted", id: "scripted" },
script: [
{
toolCalls: [
{ name: "message_agent", args: { address: "+15551111111", message: "one" } },
{ name: "message_agent", args: { address: "+15551111111", message: "two" } },
],
complete: true,
},
],
})) {
events.push(event);
}
const toolIds = events
.filter(
(event): event is Extract => event.type === "tool",
)
.map((event) => event.executionId);
expect(toolIds).toEqual(["run-1:message_agent:0", "run-1:message_agent:1"]);
});
});