Skip to content

Commit 731ab6b

Browse files
committed
wip
1 parent 0b6eacd commit 731ab6b

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,12 @@ describe("RunAgentSubscriber", () => {
628628
expect(specificSubscriber.onTextMessageContentEvent).toHaveBeenCalledWith(
629629
expect.objectContaining({
630630
event: textContentEvent,
631-
textMessageBuffer: "Hello",
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
631+
textMessageBuffer: "", // Empty - buffer before current delta is applied
632+
messages: expect.arrayContaining([
633+
expect.objectContaining({ content: "Hello", id: "msg-1", role: "user" }),
634+
expect.objectContaining({ content: "", id: "test-msg", role: "assistant" }), // Message before delta applied
635+
]),
636+
state: { counter: 0 },
637637
agent,
638638
}),
639639
);
@@ -777,23 +777,23 @@ describe("RunAgentSubscriber", () => {
777777
// Check buffer accumulation
778778
expect(mockSubscriber.onToolCallArgsEvent).toHaveBeenCalledTimes(2);
779779

780-
// First call should have partial buffer
780+
// First call should have empty buffer (before first delta applied)
781781
expect(mockSubscriber.onToolCallArgsEvent).toHaveBeenNthCalledWith(
782782
1,
783783
expect.objectContaining({
784-
toolCallBuffer: '{"query": "te',
784+
toolCallBuffer: "",
785785
toolCallName: "search",
786-
partialToolCallArgs: '{"query": "te"}', // Partial JSON from untruncateJson
786+
partialToolCallArgs: "", // Empty string when buffer is empty
787787
}),
788788
);
789789

790-
// Second call should have complete buffer
790+
// Second call should have partial buffer (before second delta applied)
791791
expect(mockSubscriber.onToolCallArgsEvent).toHaveBeenNthCalledWith(
792792
2,
793793
expect.objectContaining({
794-
toolCallBuffer: '{"query": "test"}',
794+
toolCallBuffer: '{"query": "te',
795795
toolCallName: "search",
796-
partialToolCallArgs: '{"query": "test"}', // Complete JSON from untruncateJson
796+
partialToolCallArgs: '{"query": "te"}', // untruncateJson returns truncated JSON string
797797
}),
798798
);
799799

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ 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-
123119
const mutation = await runSubscribersWithMutation(
124120
subscribers,
125121
messages,
@@ -131,15 +127,17 @@ export const defaultApplyEvents = (
131127
state,
132128
agent,
133129
input,
134-
textMessageBuffer: newTextMessageBuffer,
130+
textMessageBuffer: messages[messages.length - 1].content ?? "",
135131
}),
136132
);
137133
applyMutation(mutation);
138134

139135
if (mutation.stopPropagation !== true) {
136+
const { delta } = event as TextMessageContentEvent;
137+
140138
// Get the last message and append the content
141139
const lastMessage = messages[messages.length - 1];
142-
lastMessage.content = newTextMessageBuffer;
140+
lastMessage.content = lastMessage.content! + delta;
143141
applyMutation({ messages });
144142
}
145143

@@ -235,31 +233,30 @@ export const defaultApplyEvents = (
235233
}
236234

237235
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-
251236
const mutation = await runSubscribersWithMutation(
252237
subscribers,
253238
messages,
254239
state,
255240
(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+
// Parse from toolCallBuffer only (before current delta is applied)
250+
partialToolCallArgs = untruncateJson(toolCallBuffer);
251+
} catch (error) {}
252+
256253
return subscriber.onToolCallArgsEvent?.({
257254
event: event as ToolCallArgsEvent,
258255
messages,
259256
state,
260257
agent,
261258
input,
262-
toolCallBuffer: newToolCallBuffer,
259+
toolCallBuffer,
263260
toolCallName,
264261
partialToolCallArgs,
265262
});
@@ -268,14 +265,16 @@ export const defaultApplyEvents = (
268265
applyMutation(mutation);
269266

270267
if (mutation.stopPropagation !== true) {
268+
const { delta } = event as ToolCallArgsEvent;
269+
271270
// Get the last message
272271
const lastMessage = messages[messages.length - 1];
273272

274273
// Get the last tool call
275274
const lastToolCall = lastMessage.toolCalls[lastMessage.toolCalls.length - 1];
276275

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

280279
applyMutation({ messages });
281280
}

0 commit comments

Comments
 (0)