refactor: tool calls sent when pending and after completion

This commit is contained in:
Siddhant Rai
2025-06-11 12:40:32 +05:30
parent dd9d18208d
commit 3351f71813
6 changed files with 94 additions and 72 deletions

View File

@@ -14,6 +14,7 @@ import {
ConversationState,
Attachment,
} from './conversationModels';
import { ToolCallsType } from './types';
const initialState: ConversationState = {
queries: [],
@@ -110,11 +111,11 @@ export const fetchAnswer = createAsyncThunk<
query: { sources: data.source ?? [] },
}),
);
} else if (data.type === 'tool_calls') {
} else if (data.type === 'tool_call') {
dispatch(
updateToolCalls({
updateToolCall({
index: targetIndex,
query: { tool_calls: data.tool_calls },
tool_call: data.data as ToolCallsType,
}),
);
} else if (data.type === 'error') {
@@ -280,12 +281,23 @@ export const conversationSlice = createSlice({
state.queries[index].sources!.push(query.sources![0]);
}
},
updateToolCalls(
state,
action: PayloadAction<{ index: number; query: Partial<Query> }>,
) {
const { index, query } = action.payload;
state.queries[index].tool_calls = query?.tool_calls ?? [];
updateToolCall(state, action) {
const { index, tool_call } = action.payload;
if (!state.queries[index].tool_calls) {
state.queries[index].tool_calls = [];
}
const existingIndex = state.queries[index].tool_calls.findIndex(
(call) => call.call_id === tool_call.call_id,
);
if (existingIndex !== -1) {
Object.assign(
state.queries[index].tool_calls[existingIndex],
tool_call,
);
} else state.queries[index].tool_calls.push(tool_call);
},
updateQuery(
state,
@@ -378,7 +390,7 @@ export const {
updateConversationId,
updateThought,
updateStreamingSource,
updateToolCalls,
updateToolCall,
setConversation,
setAttachments,
addAttachment,

View File

@@ -3,5 +3,6 @@ export type ToolCallsType = {
action_name: string;
call_id: string;
arguments: Record<string, any>;
result: Record<string, any>;
result?: Record<string, any>;
status?: 'pending' | 'completed';
};