diff --git a/CHANGELOG.md b/CHANGELOG.md index 323448c0e..c90a91efc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -751,6 +751,10 @@ The Go SDK's `Agent.Sync` and the Python SDK's `stream.define_agent` stored the A tool call waiting for the person to allow it went to the channel only in live updates, and the call itself only to the socket of the app that asked. An app killed while the question was up and reopened showed "Thinking…" until the tool's timeout failed the call. The question and the person's answer are now stored on the call's step as soon as they happen, as a connector call's are, and a call still waiting for its answer is sent again, with its command, to a watcher that attaches later. A reopened app shows the question and can run the call when the person allows it, with each SDK's `decide` as it is (AI-3392). +### The JavaScript SDK answers a tool's question from a reopened app + +`AgentChat.decide` reads the call's step under `custom` in stream-chat 10, where it found no call to answer. A call allowed from the conversation before it reached this session, such as one a reopened app shows from history, now runs once the router sends it, instead of failing because the conversation keeps no arguments for it (AI-3392). + ### The Go SDK's `Agent.Sync` works for an agent with skills `Agent.Sync` stored the agent's skills before its config, with no config to name, and the router answered "a skill belongs to an agent config, so one has to be named". It now stores the config first and its skills under the config's id, as the Python SDK does, so a second `Sync` still edits the config and each skill rather than adding copies. An agent read from a directory was not affected (#2074). diff --git a/sdks/js/src/agent-chat.ts b/sdks/js/src/agent-chat.ts index ecc1809c4..a2e99d4cd 100644 --- a/sdks/js/src/agent-chat.ts +++ b/sdks/js/src/agent-chat.ts @@ -525,20 +525,26 @@ function sameActivity(a: AgentActivity | undefined, b: AgentActivity | undefined function callShown(messages: readonly AgentChatMessage[], id: string): Pick { for (const message of messages) { const attachments = messageField(message, "attachments"); - const step = (Array.isArray(attachments) ? (attachments as Partial>[]) : []).find( - (part) => part?.["type"] === "ai_tool_call" && part["id"] === id, + // An attachment's own fields are under its `custom` in stream-chat 10, as a message's are. + const step = (Array.isArray(attachments) ? (attachments as unknown[]) : []).find( + (part): part is object => + typeof part === "object" && + part !== null && + messageField(part, "type") === "ai_tool_call" && + messageField(part, "id") === id, ); - if (!step || typeof step["name"] !== "string" || step["status"] !== "awaiting_approval") { + const name = step && messageField(step, "name"); + if (!step || typeof name !== "string" || messageField(step, "status") !== "awaiting_approval") { continue; } const runtime = messageField(message, "support_runtime") as Record | undefined; const requestId = runtime?.["command_id"]; const turnId = runtime?.["turn_id"]; // Kept as JSON on the step, and left off one too long to show. - const args = step["arguments"]; + const args = messageField(step, "arguments"); return { call: { - name: step["name"], + name, ...(args === undefined ? {} : { arguments: typeof args === "string" ? args : JSON.stringify(args) }), ...(typeof requestId === "string" && requestId ? { requestId } : {}), ...(typeof turnId === "string" && turnId ? { turnId } : {}), diff --git a/sdks/js/src/session.ts b/sdks/js/src/session.ts index 9cd30ec3b..59c8fbe80 100644 --- a/sdks/js/src/session.ts +++ b/sdks/js/src/session.ts @@ -240,6 +240,8 @@ export class Session { private readonly asking = new Map(); /** The calls answered here, so answering one shown from the conversation again sends nothing. */ private readonly answered = new Set(); + /** Calls allowed from the conversation without their arguments, run once the router sends them. */ + private readonly allowedShown = new Set(); private readonly finished: Promise; private ended = false; private stopped = false; @@ -420,8 +422,9 @@ export class Session { /** * Answers a call that never reached this socket, from what the conversation shows of it, once. * The router settles a client call only with its result, so a declined one is answered with - * the same error a waiting one gets, and an allowed one of this session's tools is run here, - * or failed when the conversation left out its arguments, so the model isn't left waiting. + * the same error a waiting one gets, and an allowed one of this session's tools is run here. + * One whose arguments the conversation left out runs when the router sends the call, which it + * does to a session that attaches while the call waits. */ private decideShown(id: string, call: NonNullable, { allowed, summary }: ToolDecision): void { if (this.answered.has(id)) { @@ -437,12 +440,7 @@ export class Session { this.held().send({ type: "tool_result", tool_call_id: id, ...ids, error: "The person declined this call." }); } else if (this.functionsFor(call.name)?.has(call.name)) { if (call.arguments === undefined) { - this.held().send({ - type: "tool_result", - tool_call_id: id, - ...ids, - error: "The call's arguments were too long for the conversation to keep, so it could not run.", - }); + this.allowedShown.add(id); } else { void this.runTool({ type: "tool_call", id, name: call.name, arguments: call.arguments, ...ids }); } @@ -825,8 +823,10 @@ export class Session { if (message["run"] === false || !functions?.has(text(message, "name"))) { continue; } - // A caller who already said yes on a voice call is not asked again. - const approval = message.approved === true ? undefined : functions.approval(text(message, "name")); + // A caller who already said yes, on a voice call or to the call shown in the + // conversation before it reached here, is not asked again. + const allowed = message.approved === true || this.allowedShown.delete(text(message, "id")); + const approval = allowed ? undefined : functions.approval(text(message, "name")); if (approval) { this.hold(message, approval); this.deliver(eventOf(message)); diff --git a/sdks/js/tests/agent-chat.test.ts b/sdks/js/tests/agent-chat.test.ts index e466e22e1..08ed0d7a9 100644 --- a/sdks/js/tests/agent-chat.test.ts +++ b/sdks/js/tests/agent-chat.test.ts @@ -279,10 +279,16 @@ describe("AgentChat", () => { ]); }); - it("reads the request and turn under custom in a stream-chat 10 reply", async () => { + it("reads the step and the request and turn under custom in a stream-chat 10 reply", async () => { const channel = new ChannelV10(); + // stream-chat 10 keeps an attachment's own fields under its custom, as it does a message's. + const { type, ...stepFields } = waitingStep; channel.latest = [ - { id: "r1", attachments: [waitingStep], custom: { support_runtime: { command_id: "q1", turn_id: "t1" } } } as Message, + { + id: "r1", + attachments: [{ type, custom: stepFields }], + custom: { support_runtime: { command_id: "q1", turn_id: "t1" } }, + } as Message, ]; const { open, sessions } = opener(channel); const chat = new AgentChat({ sessionId: "s1", open }); diff --git a/sdks/js/tests/session.test.ts b/sdks/js/tests/session.test.ts index 20d620c45..70341a3df 100644 --- a/sdks/js/tests/session.test.ts +++ b/sdks/js/tests/session.test.ts @@ -950,20 +950,30 @@ describe("Session", () => { await session.close(); }); - it("allowed with its arguments too long for the conversation to show, tells the model it couldn't run", async () => { + it("allowed without its arguments, runs it once the router sends the call, without asking again", async () => { const [session, connection] = await opened(refunds()); session.decide("call_1", { allowed: true, call: { name: "issue_refund", requestId: "m1", turnId: "t1" } }); + assert.deepEqual(await connection.next(), { + type: "tool_approval", + tool_call_id: "call_1", + allowed: true, + request_id: "m1", + turn_id: "t1", + }); + assert.deepEqual(ran, [], "nothing to run it with yet"); + + connection.send(refundCall); - assert.equal((await connection.next()).type, "tool_approval"); assert.deepEqual(await connection.next(), { type: "tool_result", tool_call_id: "call_1", request_id: "m1", turn_id: "t1", - error: "The call's arguments were too long for the conversation to keep, so it could not run.", + output: "Refunded A-1042", }); - assert.deepEqual(ran, []); + assert.deepEqual(ran, ["A-1042"]); + assert.deepEqual(session.approvals, []); await session.close(); });