Skip to content

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

feat(api): return threadId and threadTurn in GET /v2/agent options (#4744)

claude[bot]c30dd25d6311
diff --git a/apps/api/src/__tests__/snips/v2/agent-thread.test.ts b/apps/api/src/__tests__/snips/v2/agent-thread.test.ts
index 775315f1d..b19bbf288 100644
--- a/apps/api/src/__tests__/snips/v2/agent-thread.test.ts
+++ b/apps/api/src/__tests__/snips/v2/agent-thread.test.ts
@@ -67,15 +67,22 @@ const cancel = (jobId: string) =>
.delete(`/v2/agent/${jobId}`)
.set("Authorization", `Bearer ${identity.apiKey}`);
-const listAgentIds = async () => {
+type ListedAgent = {
+ id: string;
+ options?: { threadId?: string; threadTurn?: number };
+};
+
+const listAgents = async () => {
const response = await request(TEST_API_URL)
.get("/v2/agent")
.set("Authorization", `Bearer ${identity.apiKey}`);
expect(response.statusCode).toBe(200);
- return ((response.body.agents ?? []) as { id: string }[]).map(a => a.id);
+ return (response.body.agents ?? []) as ListedAgent[];
};
+const listAgentIds = async () => (await listAgents()).map(a => a.id);
+
async function waitForTerminal(jobId: string) {
const deadline = Date.now() + TURN_TIMEOUT - scrapeTimeout;
@@ -236,6 +243,56 @@ describeIf(REQUIRES_FIRE_ENGINE && REQUIRES_AI && HAS_AGENT_BETA)(
TURN_TIMEOUT * 2,
);
+ it(
+ "lists each turn with its thread so clients can group them",
+ async () => {
+ const first = await startTurn({
+ urls: [TEST_SUITE_WEBSITE],
+ prompt: "Extract the page title.",
+ effort: "low",
+ });
+
+ await cancel(first.id);
+ await waitForTerminal(first.id);
+
+ const second = await startTurn({
+ threadId: first.threadId,
+ prompt: "And the heading?",
+ effort: "low",
+ });
+
+ await cancel(second.id);
+ await waitForTerminal(second.id);
+
+ // A plain run starts its own thread and must not join the one above.
+ const other = await startTurn({
+ urls: [TEST_SUITE_WEBSITE],
+ prompt: "What does this page offer?",
+ effort: "low",
+ });
+
+ await cancel(other.id);
+ await waitForTerminal(other.id);
+
+ const agents = await listAgents();
+ const byId = new Map(agents.map(agent => [agent.id, agent]));
+
+ expect(byId.get(first.id)?.options?.threadId).toBe(first.threadId);
+ expect(byId.get(first.id)?.options?.threadTurn).toBe(1);
+ expect(byId.get(second.id)?.options?.threadId).toBe(first.threadId);
+ expect(byId.get(second.id)?.options?.threadTurn).toBe(2);
+
+ expect(byId.get(other.id)?.options?.threadId).toBe(other.threadId);
+ expect(byId.get(other.id)?.options?.threadId).not.toBe(first.threadId);
+ expect(byId.get(other.id)?.options?.threadTurn).toBe(1);
+
+ expect(
+ agents.filter(agent => agent.options?.threadId === first.threadId),
+ ).toHaveLength(2);
+ },
+ TURN_TIMEOUT * 3,
+ );
+
it(
"answers a chat-mode follow-up with a message instead of data",
async () => {
diff --git a/apps/api/src/controllers/v2/agent-list.ts b/apps/api/src/controllers/v2/agent-list.ts
index e3abaf940..8f60873ca 100644
--- a/apps/api/src/controllers/v2/agent-list.ts
+++ b/apps/api/src/controllers/v2/agent-list.ts
@@ -19,6 +19,22 @@ type RecentAgent = {
| "credit_limit_reached";
};
+// 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;
+} {
+ return {
+ ...(typeof options?.threadId === "string" && {
+ threadId: options.threadId,
+ }),
+ ...(typeof options?.threadTurn === "number" && {
+ threadTurn: options.threadTurn,
+ }),
+ };
+}
+
export async function agentListController(
req: RequestWithAuth<{}, AgentListResponse>,
res: Response<AgentListResponse>,
@@ -271,6 +287,7 @@ export async function agentListController(
effort:
db.agent.options.effort ??
(db.agent.options.model === "spark-2" ? "medium" : undefined),
+ ...threadOptions(db.agent.options),
}
: recent?.options
? {
@@ -283,6 +300,7 @@ export async function agentListController(
(recent.options.modelPreset === "spark-2"
? "medium"
: undefined),
+ ...threadOptions(recent.options),
}
: undefined,
});
diff --git a/apps/api/src/controllers/v2/types.ts b/apps/api/src/controllers/v2/types.ts
index 29e1bafff..aa671ad2e 100644
--- a/apps/api/src/controllers/v2/types.ts
+++ b/apps/api/src/controllers/v2/types.ts
@@ -1621,6 +1621,8 @@ export type AgentListResponse =
// type release.
model: "spark-1-pro" | "spark-1-mini" | "spark-2" | (string & {});
effort?: "low" | "medium" | "high";
+ threadId?: string;
+ threadTurn?: number;
};
}[];
next?: string;