import { pathToFileURL } from "node:url";
import { openai, type ModelProvider } from "@agentium/core";
import { agentDriver, base, HarnessRuntime } from "@agentium/harness";
export async function runConversation(model: ModelProvider) {
const runtime = new HarnessRuntime({
definition: base({ id: "project-assistant" }),
driver: agentDriver({ name: "project-assistant", model, instructions: "Answer briefly from this conversation." }),
grants: { toolIds: [], modelRoles: ["main"] },
budgets: { maxModelCalls: 2, maxToolCalls: 0, maxTokens: 4000 },
});
const identity = { tenantId: "demo", userId: "alice" };
const sessionId = "project-42";
const options = { identity, sessionId };
try {
for (const input of ["Our project is called Atlas. Remember that.", "What is our project called?"]) {
const result = await runtime.run(input, options);
if (result.status !== "completed") throw new Error(`Conversation ended: ${result.status}`);
console.log(result.text);
}
// Inspect only after the run settles; acquisition is an exclusive lease.
const lease = await runtime.sessions.acquire(identity, sessionId);
let historyLength: number;
try {
const snapshot = lease.read();
historyLength = snapshot.history.length;
console.log("messages:", historyLength, "replayable:", snapshot.replayable);
} finally {
lease.release();
}
// The same session ID for another user has separate state.
const other = await runtime.sessions.acquire({ tenantId: "demo", userId: "bob" }, sessionId);
try {
if (other.read().history.length !== 0) throw new Error("Unexpected cross-user history");
} finally {
other.release();
}
return { historyLength };
} finally {
const diagnostics = await runtime.resources.closeSession(identity, sessionId);
if (diagnostics.length) throw new Error(`Session cleanup failed: ${diagnostics.join(", ")}`);
}
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
await runConversation(openai(process.env.OPENAI_MODEL ?? "gpt-6-luna"));
}