Skip to content

Commit 0b6eacd

Browse files
committed
wip
1 parent 601ef28 commit 0b6eacd

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

typescript-sdk/packages/client/src/agent/__tests__/subscriber.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,11 @@ describe("RunAgentSubscriber", () => {
629629
expect.objectContaining({
630630
event: textContentEvent,
631631
textMessageBuffer: "Hello",
632-
messages: agent.messages, // Can check final state for content events
633-
state: agent.state, // Can check final state for content events
632+
messages: [
633+
{ content: "Hello", id: "msg-1", role: "user" },
634+
{ content: "", id: "test-msg", role: "assistant" },
635+
], // Pre-mutation state (before current delta is applied to messages)
636+
state: { counter: 0 }, // Pre-mutation state
634637
agent,
635638
}),
636639
);
@@ -780,7 +783,7 @@ describe("RunAgentSubscriber", () => {
780783
expect.objectContaining({
781784
toolCallBuffer: '{"query": "te',
782785
toolCallName: "search",
783-
partialToolCallArgs: {}, // Partial JSON parsing failed
786+
partialToolCallArgs: '{"query": "te"}', // Partial JSON from untruncateJson
784787
}),
785788
);
786789

@@ -790,7 +793,7 @@ describe("RunAgentSubscriber", () => {
790793
expect.objectContaining({
791794
toolCallBuffer: '{"query": "test"}',
792795
toolCallName: "search",
793-
partialToolCallArgs: { query: "test" },
796+
partialToolCallArgs: '{"query": "test"}', // Complete JSON from untruncateJson
794797
}),
795798
);
796799

typescript-sdk/packages/client/src/apply/default.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ export const defaultApplyEvents = (
116116
}
117117

118118
case EventType.TEXT_MESSAGE_CONTENT: {
119+
const { delta } = event as TextMessageContentEvent;
120+
const currentContent = messages[messages.length - 1].content ?? "";
121+
const newTextMessageBuffer = currentContent + delta;
122+
119123
const mutation = await runSubscribersWithMutation(
120124
subscribers,
121125
messages,
@@ -127,17 +131,15 @@ export const defaultApplyEvents = (
127131
state,
128132
agent,
129133
input,
130-
textMessageBuffer: messages[messages.length - 1].content ?? "",
134+
textMessageBuffer: newTextMessageBuffer,
131135
}),
132136
);
133137
applyMutation(mutation);
134138

135139
if (mutation.stopPropagation !== true) {
136-
const { delta } = event as TextMessageContentEvent;
137-
138140
// Get the last message and append the content
139141
const lastMessage = messages[messages.length - 1];
140-
lastMessage.content = lastMessage.content! + delta;
142+
lastMessage.content = newTextMessageBuffer;
141143
applyMutation({ messages });
142144
}
143145

@@ -233,31 +235,31 @@ export const defaultApplyEvents = (
233235
}
234236

235237
case EventType.TOOL_CALL_ARGS: {
238+
const { delta } = event as ToolCallArgsEvent;
239+
const toolCalls = (messages[messages.length - 1] as AssistantMessage)?.toolCalls ?? [];
240+
const currentToolCallBuffer =
241+
toolCalls.length > 0 ? toolCalls[toolCalls.length - 1].function.arguments : "";
242+
const newToolCallBuffer = currentToolCallBuffer + delta;
243+
const toolCallName =
244+
toolCalls.length > 0 ? toolCalls[toolCalls.length - 1].function.name : "";
245+
246+
let partialToolCallArgs = {};
247+
try {
248+
partialToolCallArgs = untruncateJson(newToolCallBuffer);
249+
} catch (error) {}
250+
236251
const mutation = await runSubscribersWithMutation(
237252
subscribers,
238253
messages,
239254
state,
240255
(subscriber, messages, state) => {
241-
const toolCalls =
242-
(messages[messages.length - 1] as AssistantMessage)?.toolCalls ?? [];
243-
const toolCallBuffer =
244-
toolCalls.length > 0 ? toolCalls[toolCalls.length - 1].function.arguments : "";
245-
const toolCallName =
246-
toolCalls.length > 0 ? toolCalls[toolCalls.length - 1].function.name : "";
247-
let partialToolCallArgs = {};
248-
try {
249-
partialToolCallArgs = untruncateJson(
250-
toolCallBuffer + (event as ToolCallArgsEvent).delta,
251-
);
252-
} catch (error) {}
253-
254256
return subscriber.onToolCallArgsEvent?.({
255257
event: event as ToolCallArgsEvent,
256258
messages,
257259
state,
258260
agent,
259261
input,
260-
toolCallBuffer,
262+
toolCallBuffer: newToolCallBuffer,
261263
toolCallName,
262264
partialToolCallArgs,
263265
});
@@ -266,16 +268,14 @@ export const defaultApplyEvents = (
266268
applyMutation(mutation);
267269

268270
if (mutation.stopPropagation !== true) {
269-
const { delta } = event as ToolCallArgsEvent;
270-
271271
// Get the last message
272272
const lastMessage = messages[messages.length - 1];
273273

274274
// Get the last tool call
275275
const lastToolCall = lastMessage.toolCalls[lastMessage.toolCalls.length - 1];
276276

277-
// Append the arguments
278-
lastToolCall.function.arguments += delta;
277+
// Set the complete new arguments
278+
lastToolCall.function.arguments = newToolCallBuffer;
279279

280280
applyMutation({ messages });
281281
}

0 commit comments

Comments
 (0)