|
1 |
| -import { createReducer } from "@reduxjs/toolkit"; |
| 1 | +import { createReducer, Draft } from "@reduxjs/toolkit"; |
2 | 2 | import {
|
3 | 3 | Chat,
|
4 | 4 | ChatThread,
|
@@ -38,8 +38,14 @@ import {
|
38 | 38 | } from "./actions";
|
39 | 39 | import { formatChatResponse } from "./utils";
|
40 | 40 | import {
|
| 41 | + ChatMessages, |
41 | 42 | DEFAULT_MAX_NEW_TOKENS,
|
| 43 | + isAssistantMessage, |
| 44 | + isDiffMessage, |
| 45 | + isMultiModalToolResult, |
42 | 46 | isToolCallMessage,
|
| 47 | + isToolMessage, |
| 48 | + ToolMessage, |
43 | 49 | validateToolCall,
|
44 | 50 | } from "../../../services/refact";
|
45 | 51 |
|
@@ -335,76 +341,76 @@ export const chatReducer = createReducer(initialState, (builder) => {
|
335 | 341 | state.thread.messages = [...messages, newMessage];
|
336 | 342 | });
|
337 | 343 |
|
338 |
| - // builder.addCase(upsertToolCall, (state, action) => { |
339 |
| - // // if (action.payload.toolCallId !== state.thread.id && !(action.payload.chatId in state.cache)) return state; |
340 |
| - // if (action.payload.chatId === state.thread.id) { |
341 |
| - // maybeAppendToolCallResultFromIdeToMessages( |
342 |
| - // state.thread.messages, |
343 |
| - // action.payload.toolCallId, |
344 |
| - // action.payload.accepted, |
345 |
| - // ); |
346 |
| - // } else if (action.payload.chatId in state.cache) { |
347 |
| - // const thread = state.cache[action.payload.chatId]; |
348 |
| - // maybeAppendToolCallResultFromIdeToMessages( |
349 |
| - // thread.messages, |
350 |
| - // action.payload.toolCallId, |
351 |
| - // action.payload.accepted, |
352 |
| - // ); |
353 |
| - // } |
354 |
| - // }); |
| 344 | + builder.addCase(upsertToolCall, (state, action) => { |
| 345 | + // if (action.payload.toolCallId !== state.thread.id && !(action.payload.chatId in state.cache)) return state; |
| 346 | + if (action.payload.chatId === state.thread.id) { |
| 347 | + maybeAppendToolCallResultFromIdeToMessages( |
| 348 | + state.thread.messages, |
| 349 | + action.payload.toolCallId, |
| 350 | + action.payload.accepted, |
| 351 | + ); |
| 352 | + } else if (action.payload.chatId in state.cache) { |
| 353 | + const thread = state.cache[action.payload.chatId]; |
| 354 | + maybeAppendToolCallResultFromIdeToMessages( |
| 355 | + thread.messages, |
| 356 | + action.payload.toolCallId, |
| 357 | + action.payload.accepted, |
| 358 | + ); |
| 359 | + } |
| 360 | + }); |
355 | 361 | });
|
356 | 362 |
|
357 |
| -// export function maybeAppendToolCallResultFromIdeToMessages( |
358 |
| -// messages: Draft<ChatMessages>, |
359 |
| -// toolCallId: string, |
360 |
| -// accepted: boolean | "indeterminate", |
361 |
| -// ) { |
362 |
| -// const hasDiff = messages.find( |
363 |
| -// (d) => isDiffMessage(d) && d.tool_call_id === toolCallId, |
364 |
| -// ); |
365 |
| -// if (hasDiff) return; |
366 |
| - |
367 |
| -// const message = messageForToolCall(accepted); |
368 |
| - |
369 |
| -// const hasToolCall = messages.find( |
370 |
| -// (d) => isToolMessage(d) && d.content.tool_call_id === toolCallId, |
371 |
| -// ); |
372 |
| - |
373 |
| -// if ( |
374 |
| -// hasToolCall && |
375 |
| -// isToolMessage(hasToolCall) && |
376 |
| -// typeof hasToolCall.content.content === "string" |
377 |
| -// ) { |
378 |
| -// hasToolCall.content.content = message; |
379 |
| -// return; |
380 |
| -// } else if ( |
381 |
| -// hasToolCall && |
382 |
| -// isToolMessage(hasToolCall) && |
383 |
| -// isMultiModalToolResult(hasToolCall.content) |
384 |
| -// ) { |
385 |
| -// hasToolCall.content.content.push({ m_type: "text", m_content: message }); |
386 |
| -// return; |
387 |
| -// } |
388 |
| - |
389 |
| -// const assistantMessageIndex = messages.findIndex((message) => { |
390 |
| -// if (!isAssistantMessage(message)) return false; |
391 |
| -// return message.tool_calls?.find((toolCall) => toolCall.id === toolCallId); |
392 |
| -// }); |
393 |
| - |
394 |
| -// if (assistantMessageIndex === -1) return; |
395 |
| -// const toolMessage: ToolMessage = { |
396 |
| -// role: "tool", |
397 |
| -// content: { |
398 |
| -// content: message, |
399 |
| -// tool_call_id: toolCallId, |
400 |
| -// }, |
401 |
| -// }; |
402 |
| - |
403 |
| -// messages.splice(assistantMessageIndex + 1, 0, toolMessage); |
404 |
| -// } |
405 |
| - |
406 |
| -// function messageForToolCall(accepted: boolean | "indeterminate") { |
407 |
| -// if (accepted === false) return "The user rejected the changes."; |
408 |
| -// if (accepted === true) return "The user accepted the changes."; |
409 |
| -// return "The user may have made modifications to changes."; |
410 |
| -// } |
| 363 | +export function maybeAppendToolCallResultFromIdeToMessages( |
| 364 | + messages: Draft<ChatMessages>, |
| 365 | + toolCallId: string, |
| 366 | + accepted: boolean | "indeterminate", |
| 367 | +) { |
| 368 | + const hasDiff = messages.find( |
| 369 | + (d) => isDiffMessage(d) && d.tool_call_id === toolCallId, |
| 370 | + ); |
| 371 | + if (hasDiff) return; |
| 372 | + |
| 373 | + const message = messageForToolCall(accepted); |
| 374 | + |
| 375 | + const hasToolCall = messages.find( |
| 376 | + (d) => isToolMessage(d) && d.content.tool_call_id === toolCallId, |
| 377 | + ); |
| 378 | + |
| 379 | + if ( |
| 380 | + hasToolCall && |
| 381 | + isToolMessage(hasToolCall) && |
| 382 | + typeof hasToolCall.content.content === "string" |
| 383 | + ) { |
| 384 | + hasToolCall.content.content = message; |
| 385 | + return; |
| 386 | + } else if ( |
| 387 | + hasToolCall && |
| 388 | + isToolMessage(hasToolCall) && |
| 389 | + isMultiModalToolResult(hasToolCall.content) |
| 390 | + ) { |
| 391 | + hasToolCall.content.content.push({ m_type: "text", m_content: message }); |
| 392 | + return; |
| 393 | + } |
| 394 | + |
| 395 | + const assistantMessageIndex = messages.findIndex((message) => { |
| 396 | + if (!isAssistantMessage(message)) return false; |
| 397 | + return message.tool_calls?.find((toolCall) => toolCall.id === toolCallId); |
| 398 | + }); |
| 399 | + |
| 400 | + if (assistantMessageIndex === -1) return; |
| 401 | + const toolMessage: ToolMessage = { |
| 402 | + role: "tool", |
| 403 | + content: { |
| 404 | + content: message, |
| 405 | + tool_call_id: toolCallId, |
| 406 | + }, |
| 407 | + }; |
| 408 | + |
| 409 | + messages.splice(assistantMessageIndex + 1, 0, toolMessage); |
| 410 | +} |
| 411 | + |
| 412 | +function messageForToolCall(accepted: boolean | "indeterminate") { |
| 413 | + if (accepted === false) return "The user rejected the changes."; |
| 414 | + if (accepted === true) return "The user accepted the changes."; |
| 415 | + return "The user may have made modifications to changes."; |
| 416 | +} |
0 commit comments