Local models were burning the 8-round budget on simple event adds. Raise the limit, resolve calendars by name/default, and inject the household clock so relative dates work.
This commit is contained in:
@@ -83,6 +83,36 @@ describe("runAgentChat", () => {
|
||||
assert.equal(result.message.role, "assistant");
|
||||
assert.ok(result.message.content.length > 0);
|
||||
});
|
||||
|
||||
it("appends runtime clock context to the system prompt", async () => {
|
||||
const original = process.env.HOUSEHOLD_TIMEZONE;
|
||||
process.env.HOUSEHOLD_TIMEZONE = "America/Chicago";
|
||||
|
||||
let systemContent = "";
|
||||
const result = await runAgentChat({
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
request: new Request("http://localhost:3000/api/agent/chat"),
|
||||
systemPrompt: "You are a pirate.",
|
||||
llm: {
|
||||
async chatCompletion(request) {
|
||||
const system = request.messages.find((message) => message.role === "system");
|
||||
systemContent = typeof system?.content === "string" ? system.content : "";
|
||||
return {
|
||||
message: { role: "assistant", content: "Ahoy" },
|
||||
finishReason: "stop",
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(result.message.content, "Ahoy");
|
||||
assert.match(systemContent, /^You are a pirate\./);
|
||||
assert.match(systemContent, /Current time:/);
|
||||
assert.match(systemContent, /America\/Chicago/);
|
||||
|
||||
if (original === undefined) delete process.env.HOUSEHOLD_TIMEZONE;
|
||||
else process.env.HOUSEHOLD_TIMEZONE = original;
|
||||
});
|
||||
});
|
||||
|
||||
it("passes a model override to the OpenAI-compatible client", async () => {
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { appendAgentRuntimeContext, resolveHouseholdTimezone } from "../../src/modules/agent/tools";
|
||||
import { createApiToolExecutor } from "../../src/modules/agent/tool-executor";
|
||||
|
||||
describe("appendAgentRuntimeContext", () => {
|
||||
it("appends current time and timezone to the prompt", () => {
|
||||
const original = process.env.HOUSEHOLD_TIMEZONE;
|
||||
process.env.HOUSEHOLD_TIMEZONE = "America/Chicago";
|
||||
|
||||
const now = new Date("2026-07-09T14:30:00.000Z");
|
||||
const result = appendAgentRuntimeContext("Be helpful.", now);
|
||||
|
||||
assert.match(result, /^Be helpful\./);
|
||||
assert.match(result, /Current time:/);
|
||||
assert.match(result, /America\/Chicago/);
|
||||
assert.match(result, /2026-07-09T14:30:00\.000Z/);
|
||||
assert.match(result, /Resolve relative dates from this clock/);
|
||||
|
||||
if (original === undefined) delete process.env.HOUSEHOLD_TIMEZONE;
|
||||
else process.env.HOUSEHOLD_TIMEZONE = original;
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveHouseholdTimezone", () => {
|
||||
it("prefers HOUSEHOLD_TIMEZONE over TZ", () => {
|
||||
const originalHousehold = process.env.HOUSEHOLD_TIMEZONE;
|
||||
const originalTz = process.env.TZ;
|
||||
process.env.HOUSEHOLD_TIMEZONE = "America/New_York";
|
||||
process.env.TZ = "UTC";
|
||||
|
||||
assert.equal(resolveHouseholdTimezone(), "America/New_York");
|
||||
|
||||
if (originalHousehold === undefined) delete process.env.HOUSEHOLD_TIMEZONE;
|
||||
else process.env.HOUSEHOLD_TIMEZONE = originalHousehold;
|
||||
if (originalTz === undefined) delete process.env.TZ;
|
||||
else process.env.TZ = originalTz;
|
||||
});
|
||||
});
|
||||
|
||||
describe("create_event calendar resolution", () => {
|
||||
it("uses the first calendar when calendarId and calendarName are omitted", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
const posts: Array<{ path: string; body: unknown }> = [];
|
||||
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input);
|
||||
if (url.endsWith("/api/v1/calendars") && (!init?.method || init.method === "GET")) {
|
||||
return Response.json([
|
||||
{ id: "cal-1", name: "Family" },
|
||||
{ id: "cal-2", name: "Work" },
|
||||
]);
|
||||
}
|
||||
if (url.endsWith("/api/v1/events") && init?.method === "POST") {
|
||||
const body = JSON.parse(String(init.body));
|
||||
posts.push({ path: url, body });
|
||||
return Response.json({ id: "evt-1", ...body }, { status: 201 });
|
||||
}
|
||||
return new Response("not found", { status: 404 });
|
||||
}) as typeof fetch;
|
||||
|
||||
const execute = createApiToolExecutor(new Request("http://localhost:3000/api/agent/chat"));
|
||||
const result = JSON.parse(
|
||||
await execute(
|
||||
"create_event",
|
||||
JSON.stringify({
|
||||
title: "Dentist",
|
||||
startAt: "2026-07-10T15:00:00.000Z",
|
||||
endAt: "2026-07-10T16:00:00.000Z",
|
||||
}),
|
||||
),
|
||||
) as { status: number; body: { calendarId?: string } };
|
||||
|
||||
assert.equal(result.status, 201);
|
||||
assert.equal(result.body.calendarId, "cal-1");
|
||||
assert.equal(posts.length, 1);
|
||||
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
|
||||
it("matches calendarName case-insensitively", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input);
|
||||
if (url.endsWith("/api/v1/calendars") && (!init?.method || init.method === "GET")) {
|
||||
return Response.json([
|
||||
{ id: "cal-1", name: "Family" },
|
||||
{ id: "cal-2", name: "Work" },
|
||||
]);
|
||||
}
|
||||
if (url.endsWith("/api/v1/events") && init?.method === "POST") {
|
||||
const body = JSON.parse(String(init.body));
|
||||
return Response.json({ id: "evt-1", ...body }, { status: 201 });
|
||||
}
|
||||
return new Response("not found", { status: 404 });
|
||||
}) as typeof fetch;
|
||||
|
||||
const execute = createApiToolExecutor(new Request("http://localhost:3000/api/agent/chat"));
|
||||
const result = JSON.parse(
|
||||
await execute(
|
||||
"create_event",
|
||||
JSON.stringify({
|
||||
calendarName: "work",
|
||||
title: "Standup",
|
||||
startAt: "2026-07-10T15:00:00.000Z",
|
||||
endAt: "2026-07-10T15:30:00.000Z",
|
||||
}),
|
||||
),
|
||||
) as { status: number; body: { calendarId?: string } };
|
||||
|
||||
assert.equal(result.status, 201);
|
||||
assert.equal(result.body.calendarId, "cal-2");
|
||||
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user