Tool self-fetches were hairpinning to fam.ginnoir.com and failing, so list_calendars/create_event returned 500 and the model gave up.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
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";
|
||||
import {
|
||||
createApiToolExecutor,
|
||||
resolveInternalApiBase,
|
||||
} from "../../src/modules/agent/tool-executor";
|
||||
|
||||
describe("appendAgentRuntimeContext", () => {
|
||||
it("appends current time and timezone to the prompt", () => {
|
||||
@@ -38,6 +41,34 @@ describe("resolveHouseholdTimezone", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveInternalApiBase", () => {
|
||||
it("uses loopback instead of the public request origin", () => {
|
||||
const original = process.env.INTERNAL_API_BASE_URL;
|
||||
const originalPort = process.env.PORT;
|
||||
delete process.env.INTERNAL_API_BASE_URL;
|
||||
process.env.PORT = "3000";
|
||||
|
||||
const base = resolveInternalApiBase(new Request("https://fam.ginnoir.com/api/agent/chat"));
|
||||
assert.equal(base, "http://127.0.0.1:3000");
|
||||
|
||||
if (original === undefined) delete process.env.INTERNAL_API_BASE_URL;
|
||||
else process.env.INTERNAL_API_BASE_URL = original;
|
||||
if (originalPort === undefined) delete process.env.PORT;
|
||||
else process.env.PORT = originalPort;
|
||||
});
|
||||
|
||||
it("honors INTERNAL_API_BASE_URL when set", () => {
|
||||
const original = process.env.INTERNAL_API_BASE_URL;
|
||||
process.env.INTERNAL_API_BASE_URL = "http://127.0.0.1:3010/";
|
||||
|
||||
const base = resolveInternalApiBase(new Request("https://fam.ginnoir.com/api/agent/chat"));
|
||||
assert.equal(base, "http://127.0.0.1:3010");
|
||||
|
||||
if (original === undefined) delete process.env.INTERNAL_API_BASE_URL;
|
||||
else process.env.INTERNAL_API_BASE_URL = original;
|
||||
});
|
||||
});
|
||||
|
||||
describe("create_event calendar resolution", () => {
|
||||
it("uses the first calendar when calendarId and calendarName are omitted", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
@@ -45,13 +76,13 @@ describe("create_event calendar resolution", () => {
|
||||
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input);
|
||||
if (url.endsWith("/api/v1/calendars") && (!init?.method || init.method === "GET")) {
|
||||
if (url.includes("/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") {
|
||||
if (url.includes("/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 });
|
||||
@@ -59,7 +90,11 @@ describe("create_event calendar resolution", () => {
|
||||
return new Response("not found", { status: 404 });
|
||||
}) as typeof fetch;
|
||||
|
||||
const execute = createApiToolExecutor(new Request("http://localhost:3000/api/agent/chat"));
|
||||
const execute = createApiToolExecutor(
|
||||
new Request("https://fam.ginnoir.com/api/agent/chat", {
|
||||
headers: { cookie: "authjs.session-token=test" },
|
||||
}),
|
||||
);
|
||||
const result = JSON.parse(
|
||||
await execute(
|
||||
"create_event",
|
||||
@@ -74,6 +109,7 @@ describe("create_event calendar resolution", () => {
|
||||
assert.equal(result.status, 201);
|
||||
assert.equal(result.body.calendarId, "cal-1");
|
||||
assert.equal(posts.length, 1);
|
||||
assert.match(posts[0]!.path, /^http:\/\/127\.0\.0\.1:3000\/api\/v1\/events/);
|
||||
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
@@ -83,13 +119,13 @@ describe("create_event calendar resolution", () => {
|
||||
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input);
|
||||
if (url.endsWith("/api/v1/calendars") && (!init?.method || init.method === "GET")) {
|
||||
if (url.includes("/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") {
|
||||
if (url.includes("/api/v1/events") && init?.method === "POST") {
|
||||
const body = JSON.parse(String(init.body));
|
||||
return Response.json({ id: "evt-1", ...body }, { status: 201 });
|
||||
}
|
||||
@@ -114,4 +150,22 @@ describe("create_event calendar resolution", () => {
|
||||
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
|
||||
it("returns a clear error when the internal API is unreachable", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
throw new TypeError("fetch failed");
|
||||
}) as typeof fetch;
|
||||
|
||||
const execute = createApiToolExecutor(new Request("https://fam.ginnoir.com/api/agent/chat"));
|
||||
const result = JSON.parse(await execute("list_calendars", "{}")) as {
|
||||
status: number;
|
||||
body: { error?: string };
|
||||
};
|
||||
|
||||
assert.equal(result.status, 502);
|
||||
assert.match(String(result.body.error), /unreachable/i);
|
||||
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user