Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(api): parse agents.options in GET /v2/agent (#4746)

claude[bot]71335583541e
diff --git a/apps/api/src/controllers/v2/__tests__/agent-list.test.ts b/apps/api/src/controllers/v2/__tests__/agent-list.test.ts
new file mode 100644
index 000000000..44ac36cf5
--- /dev/null
+++ b/apps/api/src/controllers/v2/__tests__/agent-list.test.ts
@@ -0,0 +1,143 @@
+import type { Response } from "express";
+import type { RequestWithAuth } from "../types";
+
+const mocks = vi.hoisted(() => ({
+ query: vi.fn(),
+}));
+
+vi.mock("../../../config", () => ({
+ config: {
+ USE_DB_AUTHENTICATION: true,
+ EXTRACT_V3_BETA_URL: "https://agent.example",
+ AGENT_INTEROP_SECRET: "test",
+ },
+}));
+
+vi.mock("../../../lib/clickhouse-client", () => ({
+ clickhouseClient: { query: mocks.query },
+}));
+
+vi.mock("../../../db/connection", () => ({
+ db: {
+ select: () => ({ from: () => ({ where: async () => [] }) }),
+ },
+}));
+
+import { agentListController } from "../agent-list";
+
+const TEAM_ID = "11111111-1111-1111-1111-111111111111";
+const AGENT_ID = "22222222-2222-2222-2222-222222222222";
+
+function makeReq() {
+ return {
+ query: {},
+ auth: { team_id: TEAM_ID },
+ protocol: "https",
+ host: "api.example",
+ } as unknown as RequestWithAuth<{}, any>;
+}
+
+function makeRes() {
+ return {
+ status: vi.fn().mockReturnThis(),
+ json: vi.fn(),
+ } as unknown as Response;
+}
+
+// The first query reads `requests`, the second reads `agents`.
+function mockAgentRow(options: unknown) {
+ mocks.query.mockImplementation(async ({ query }: { query: string }) => ({
+ json: async () =>
+ query.includes("FROM requests")
+ ? [
+ {
+ id: AGENT_ID,
+ created_at: "2026-09-23 12:00:00.000",
+ target_hint: "https://example.com",
+ origin: "api",
+ integration: null,
+ },
+ ]
+ : [{ id: AGENT_ID, options, is_successful: true, error: null }],
+ }));
+}
+
+async function listOptions() {
+ const res = makeRes();
+ await agentListController(makeReq(), res);
+ const body = vi.mocked(res.json).mock.calls[0][0];
+ expect(body.success).toBe(true);
+ expect(body.agents).toHaveLength(1);
+ return body.agents[0].options;
+}
+
+describe("agentListController options", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({ ok: true, json: async () => [] }),
+ );
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it("parses options that ClickHouse returns as a JSON string", async () => {
+ mockAgentRow(
+ JSON.stringify({
+ prompt: "find the price",
+ urls: ["https://example.com"],
+ schema: { type: "object" },
+ model: "spark-2",
+ threadId: "thread-1",
+ threadTurn: 2,
+ }),
+ );
+
+ expect(await listOptions()).toEqual({
+ urls: ["https://example.com"],
+ prompt: "find the price",
+ schema: { type: "object" },
+ model: "spark-2",
+ effort: "medium",
+ threadId: "thread-1",
+ threadTurn: 2,
+ });
+ });
+
+ it("reads a quoted threadTurn as a number", async () => {
+ mockAgentRow(
+ JSON.stringify({ prompt: "p", threadId: "thread-1", threadTurn: "3" }),
+ );
+
+ expect(await listOptions()).toMatchObject({
+ threadId: "thread-1",
+ threadTurn: 3,
+ });
+ });
+
+ it("drops a quoted threadTurn that a number cannot hold exactly", async () => {
+ mockAgentRow(
+ JSON.stringify({
+ prompt: "p",
+ threadId: "thread-1",
+ threadTurn: "9223372036854775807",
+ }),
+ );
+
+ const options = await listOptions();
+ expect(options).toMatchObject({ threadId: "thread-1" });
+ expect(options).not.toHaveProperty("threadTurn");
+ });
+
+ it("falls back to defaults when options do not parse", async () => {
+ mockAgentRow("{not json");
+
+ const options = await listOptions();
+ expect(options).toMatchObject({ prompt: "", model: "spark-1-pro" });
+ expect(options).not.toHaveProperty("threadId");
+ expect(options).not.toHaveProperty("threadTurn");
+ });
+});
diff --git a/apps/api/src/controllers/v2/agent-list.ts b/apps/api/src/controllers/v2/agent-list.ts
index 8f60873ca..02d8bf423 100644
--- a/apps/api/src/controllers/v2/agent-list.ts
+++ b/apps/api/src/controllers/v2/agent-list.ts
@@ -19,19 +19,45 @@ type RecentAgent = {
| "credit_limit_reached";
};
+// ClickHouse returns the `agents.options` column as a JSON string, not an
+// object. Parse it so the option fields can be read. A value that does not
+// parse to an object reads as no options.
+function parseAgentOptions(options: unknown): Record<string, any> {
+ let parsed = options;
+ if (typeof parsed === "string") {
+ try {
+ parsed = JSON.parse(parsed);
+ } catch {
+ return {};
+ }
+ }
+ return parsed !== null && typeof parsed === "object" ? parsed : {};
+}
+
+// JSONEachRow can quote 64-bit integers, so a turn can arrive as "2". A turn
+// that a number cannot hold exactly is dropped, not rounded.
+function threadTurnOf(value: unknown): number | undefined {
+ const turn =
+ typeof value === "number"
+ ? value
+ : typeof value === "string" && value.trim() !== ""
+ ? Number(value)
+ : NaN;
+ return Number.isSafeInteger(turn) ? turn : undefined;
+}
+
// Each turn of a thread is its own run, so clients need the thread to list
// a conversation once.
function threadOptions(options: any): {
threadId?: string;
threadTurn?: number;
} {
+ const threadTurn = threadTurnOf(options?.threadTurn);
return {
...(typeof options?.threadId === "string" && {
threadId: options.threadId,
}),
- ...(typeof options?.threadTurn === "number" && {
- threadTurn: options.threadTurn,
- }),
+ ...(threadTurn !== undefined && { threadTurn }),
};
}
@@ -196,7 +222,7 @@ export async function agentListController(
error: string | null;
}) => ({
id: x.id,
- options: x.options,
+ options: parseAgentOptions(x.options),
isSuccessful: x.is_successful,
error: x.error,
}),