Skip to content

Commit ab65b99

Browse files
mmetylerslaton
authored andcommitted
fix message content
1 parent e06e6eb commit ab65b99

File tree

3 files changed

+908
-35
lines changed

3 files changed

+908
-35
lines changed

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

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

118118
case EventType.TEXT_MESSAGE_CONTENT: {
119+
const { messageId, delta } = event as TextMessageContentEvent;
120+
121+
// Find the target message by ID
122+
const targetMessage = messages.find((m) => m.id === messageId);
123+
if (!targetMessage) {
124+
console.warn(`TEXT_MESSAGE_CONTENT: No message found with ID '${messageId}'`);
125+
return emitUpdates();
126+
}
127+
119128
const mutation = await runSubscribersWithMutation(
120129
subscribers,
121130
messages,
@@ -127,24 +136,30 @@ export const defaultApplyEvents = (
127136
state,
128137
agent,
129138
input,
130-
textMessageBuffer: messages[messages.length - 1].content ?? "",
139+
textMessageBuffer: targetMessage.content ?? "",
131140
}),
132141
);
133142
applyMutation(mutation);
134143

135144
if (mutation.stopPropagation !== true) {
136-
const { delta } = event as TextMessageContentEvent;
137-
138-
// Get the last message and append the content
139-
const lastMessage = messages[messages.length - 1];
140-
lastMessage.content = lastMessage.content! + delta;
145+
// Append content to the correct message by ID
146+
targetMessage.content = (targetMessage.content || "") + delta;
141147
applyMutation({ messages });
142148
}
143149

144150
return emitUpdates();
145151
}
146152

147153
case EventType.TEXT_MESSAGE_END: {
154+
const { messageId } = event as TextMessageEndEvent;
155+
156+
// Find the target message by ID
157+
const targetMessage = messages.find((m) => m.id === messageId);
158+
if (!targetMessage) {
159+
console.warn(`TEXT_MESSAGE_END: No message found with ID '${messageId}'`);
160+
return emitUpdates();
161+
}
162+
148163
const mutation = await runSubscribersWithMutation(
149164
subscribers,
150165
messages,
@@ -156,15 +171,15 @@ export const defaultApplyEvents = (
156171
state,
157172
agent,
158173
input,
159-
textMessageBuffer: messages[messages.length - 1].content ?? "",
174+
textMessageBuffer: targetMessage.content ?? "",
160175
}),
161176
);
162177
applyMutation(mutation);
163178

164179
await Promise.all(
165180
subscribers.map((subscriber) => {
166181
subscriber.onNewMessage?.({
167-
message: messages[messages.length - 1],
182+
message: targetMessage,
168183
messages,
169184
state,
170185
agent,
@@ -233,17 +248,34 @@ export const defaultApplyEvents = (
233248
}
234249

235250
case EventType.TOOL_CALL_ARGS: {
251+
const { toolCallId, delta } = event as ToolCallArgsEvent;
252+
253+
// Find the message containing this tool call
254+
const targetMessage = messages.find((m) =>
255+
(m as AssistantMessage).toolCalls?.some((tc) => tc.id === toolCallId),
256+
) as AssistantMessage;
257+
258+
if (!targetMessage) {
259+
console.warn(
260+
`TOOL_CALL_ARGS: No message found containing tool call with ID '${toolCallId}'`,
261+
);
262+
return emitUpdates();
263+
}
264+
265+
// Find the specific tool call
266+
const targetToolCall = targetMessage.toolCalls!.find((tc) => tc.id === toolCallId);
267+
if (!targetToolCall) {
268+
console.warn(`TOOL_CALL_ARGS: No tool call found with ID '${toolCallId}'`);
269+
return emitUpdates();
270+
}
271+
236272
const mutation = await runSubscribersWithMutation(
237273
subscribers,
238274
messages,
239275
state,
240276
(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 : "";
277+
const toolCallBuffer = targetToolCall.function.arguments;
278+
const toolCallName = targetToolCall.function.name;
247279
let partialToolCallArgs = {};
248280
try {
249281
// Parse from toolCallBuffer only (before current delta is applied)
@@ -265,35 +297,43 @@ export const defaultApplyEvents = (
265297
applyMutation(mutation);
266298

267299
if (mutation.stopPropagation !== true) {
268-
const { delta } = event as ToolCallArgsEvent;
269-
270-
// Get the last message
271-
const lastMessage = messages[messages.length - 1] as AssistantMessage;
272-
273-
// Get the last tool call
274-
const lastToolCall = lastMessage.toolCalls![lastMessage.toolCalls!.length - 1];
275-
276-
// Append the arguments
277-
lastToolCall.function.arguments += delta;
278-
300+
// Append the arguments to the correct tool call by ID
301+
targetToolCall.function.arguments += delta;
279302
applyMutation({ messages });
280303
}
281304

282305
return emitUpdates();
283306
}
284307

285308
case EventType.TOOL_CALL_END: {
309+
const { toolCallId } = event as ToolCallEndEvent;
310+
311+
// Find the message containing this tool call
312+
const targetMessage = messages.find((m) =>
313+
(m as AssistantMessage).toolCalls?.some((tc) => tc.id === toolCallId),
314+
) as AssistantMessage;
315+
316+
if (!targetMessage) {
317+
console.warn(
318+
`TOOL_CALL_END: No message found containing tool call with ID '${toolCallId}'`,
319+
);
320+
return emitUpdates();
321+
}
322+
323+
// Find the specific tool call
324+
const targetToolCall = targetMessage.toolCalls!.find((tc) => tc.id === toolCallId);
325+
if (!targetToolCall) {
326+
console.warn(`TOOL_CALL_END: No tool call found with ID '${toolCallId}'`);
327+
return emitUpdates();
328+
}
329+
286330
const mutation = await runSubscribersWithMutation(
287331
subscribers,
288332
messages,
289333
state,
290334
(subscriber, messages, state) => {
291-
const toolCalls =
292-
(messages[messages.length - 1] as AssistantMessage)?.toolCalls ?? [];
293-
const toolCallArgsString =
294-
toolCalls.length > 0 ? toolCalls[toolCalls.length - 1].function.arguments : "";
295-
const toolCallName =
296-
toolCalls.length > 0 ? toolCalls[toolCalls.length - 1].function.name : "";
335+
const toolCallArgsString = targetToolCall.function.arguments;
336+
const toolCallName = targetToolCall.function.name;
297337
let toolCallArgs = {};
298338
try {
299339
toolCallArgs = JSON.parse(toolCallArgsString);
@@ -314,9 +354,7 @@ export const defaultApplyEvents = (
314354
await Promise.all(
315355
subscribers.map((subscriber) => {
316356
subscriber.onNewToolCall?.({
317-
toolCall: (messages[messages.length - 1] as AssistantMessage).toolCalls![
318-
(messages[messages.length - 1] as AssistantMessage).toolCalls!.length - 1
319-
],
357+
toolCall: targetToolCall,
320358
messages,
321359
state,
322360
agent,

0 commit comments

Comments
 (0)