diff --git a/core/llm/constructMessages.ts b/core/llm/constructMessages.ts
index 49115cb6f..5a3967361 100644
--- a/core/llm/constructMessages.ts
+++ b/core/llm/constructMessages.ts
@@ -4,11 +4,15 @@ import {
ContextItemWithId,
RuleWithSource,
TextMessagePart,
+ Tool,
ToolResultChatMessage,
UserChatMessage,
} from "../";
import { findLast } from "../util/findLast";
-import { normalizeToMessageParts } from "../util/messageContent";
+import {
+ normalizeToMessageParts,
+ renderContextItems,
+} from "../util/messageContent";
import { isUserOrToolMsg } from "./messages";
import { getSystemMessageWithRules } from "./rules/getSystemMessageWithRules";
import { RulePolicies } from "./rules/types";
@@ -109,30 +113,38 @@ export function constructMessages(
history: ChatHistoryItem[],
baseChatOrAgentSystemMessage: string | undefined,
rules: RuleWithSource[],
+ tools: Tool[],
rulePolicies?: RulePolicies,
): ChatMessage[] {
+ const validTools = new Set(tools.map((tool) => tool.function.name));
+
const filteredHistory = history.filter(
(item) => item.message.role !== "system",
);
const msgs: ChatMessage[] = [];
- for (let i = 0; i < filteredHistory.length; i++) {
- const historyItem = filteredHistory[i];
-
- if (messageMode === "chat") {
- const toolMessage: ToolResultChatMessage =
- historyItem.message as ToolResultChatMessage;
- if (historyItem.toolCallState?.toolCallId || toolMessage.toolCallId) {
- // remove all tool calls from the history
- continue;
- }
+ for (const item of filteredHistory) {
+ if (item.message.role === "tool") {
+ continue; // Tool messages will be re-inserted
}
- if (historyItem.message.role === "user") {
- // Gather context items for user messages
- let content = normalizeToMessageParts(historyItem.message);
+ // This was implemented for APIs that require all tool calls to have
+ // A corresponsing tool sent, which means if a user excluded a tool
+ // Then sessions with that tool call in the history would fail
+ // if (messageMode === "chat") {
+ // const toolMessage: ToolResultChatMessage =
+ // historyItem.message as ToolResultChatMessage;
+ // if (historyItem.toolCallState?.toolCallId || toolMessage.toolCallId) {
+ // // remove all tool calls from the history
+ // continue;
+ // }
+ // }
- const ctxItems = historyItem.contextItems
+ if (item.message.role === "user") {
+ // Gather context items for user messages
+ let content = normalizeToMessageParts(item.message);
+
+ const ctxItems = item.contextItems
.map((ctxItem) => {
return {
type: "text",
@@ -143,11 +155,47 @@ export function constructMessages(
content = [...ctxItems, ...content];
msgs.push({
- ...historyItem.message,
+ ...item.message,
content,
});
} else {
- msgs.push(historyItem.message);
+ msgs.push(item.message);
+ }
+
+ if (item.message.role === "assistant") {
+ // First, push a version of the message with any valid tools calls
+ const validToolCalls = item.message.toolCalls?.filter(
+ (toolCall) =>
+ toolCall.function?.name &&
+ toolCall.id &&
+ validTools.has(toolCall.function.name),
+ );
+
+ // Case where no valid tool calls or content are present
+ msgs.push({
+ ...item.message,
+ toolCalls: validToolCalls,
+ });
+
+ // Then push a tool message for each valid tool call
+ if (validToolCalls?.length) {
+ // If the assistant message has tool calls, we need to insert tool messages
+ for (const toolCall of validToolCalls) {
+ let content: string = "No tool output";
+ // TODO toolCallState only supports one tool call per message for now
+ if (
+ item.toolCallState?.toolCallId === toolCall.id &&
+ item.toolCallState?.output
+ ) {
+ content = renderContextItems(item.toolCallState.output);
+ }
+ msgs.push({
+ role: "tool",
+ content,
+ toolCallId: toolCall.id!,
+ });
+ }
+ }
}
}
diff --git a/gui/src/components/mainInput/belowMainInput/ContextItemsPeek.tsx b/gui/src/components/mainInput/belowMainInput/ContextItemsPeek.tsx
index cb3009675..31c392bf1 100644
--- a/gui/src/components/mainInput/belowMainInput/ContextItemsPeek.tsx
+++ b/gui/src/components/mainInput/belowMainInput/ContextItemsPeek.tsx
@@ -143,7 +143,6 @@ export function ContextItemsPeekItem({
{contextItem.uri?.type === "file"
? getUriPathBasename(contextItem.description)
: contextItem.description}
- adsfasdfasdjk; asdfl;kjasdl;fkjasdlk;fja asdfklj;asdl;fkjasdf
{isUrl && (
diff --git a/gui/src/pages/gui/ToolCallDiv/SimpleToolCallUI.tsx b/gui/src/pages/gui/ToolCallDiv/SimpleToolCallUI.tsx
index 9ba9ae60d..51af15c77 100644
--- a/gui/src/pages/gui/ToolCallDiv/SimpleToolCallUI.tsx
+++ b/gui/src/pages/gui/ToolCallDiv/SimpleToolCallUI.tsx
@@ -2,7 +2,6 @@ import { ChevronRightIcon } from "@heroicons/react/24/outline";
import { Tool, ToolCallState } from "core";
import { ComponentType, useMemo, useState } from "react";
import { ContextItemsPeekItem } from "../../../components/mainInput/belowMainInput/ContextItemsPeek";
-import { useAppDispatch, useAppSelector } from "../../../redux/hooks";
import { ArgsItems, ArgsToggleIcon } from "./ToolCallArgs";
import { toolCallStateToContextItems } from "./toolCallStateToContextItem";
import { ToolCallStatusMessage } from "./ToolCallStatusMessage";
@@ -21,8 +20,6 @@ export function SimpleToolCallUI({
tool,
historyIndex,
}: SimpleToolCallUIProps) {
- const history = useAppSelector((state) => state.session.history);
- const dispatch = useAppDispatch();
const shownContextItems = useMemo(() => {
const contextItems = toolCallStateToContextItems(toolCallState);
return contextItems.filter((item) => !item.hidden);
diff --git a/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx b/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx
index cf57a6c08..e4f0a1cc8 100644
--- a/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx
+++ b/gui/src/pages/gui/ToolCallDiv/ToolTruncateHistoryIcon.tsx
@@ -19,7 +19,7 @@ export function ToolTruncateHistoryIcon({
}, [history]);
const dispatch = useAppDispatch();
const { mainEditor } = useMainEditor();
- if (isStreaming || lastMessageIndex) {
+ if (isStreaming || historyIndex === lastMessageIndex) {
return null;
}
return (
@@ -34,8 +34,7 @@ export function ToolTruncateHistoryIcon({
mainEditor?.commands.focus();
}}
>
- {historyIndex}/{history.length - 1}
-
+
);
}
diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts
index 80c88dc54..339369cd9 100644
--- a/gui/src/redux/slices/sessionSlice.ts
+++ b/gui/src/redux/slices/sessionSlice.ts
@@ -157,6 +157,7 @@ export const sessionSlice = createSlice({
const { index } = payload;
if (state.history.length && index < state.history.length) {
+ state.codeBlockApplyStates.curIndex = 0;
state.history = state.history.slice(0, index + 1).concat({
message: {
id: uuidv4(),
diff --git a/gui/src/redux/thunks/streamResponse.ts b/gui/src/redux/thunks/streamResponse.ts
index caeded723..facc987c7 100644
--- a/gui/src/redux/thunks/streamResponse.ts
+++ b/gui/src/redux/thunks/streamResponse.ts
@@ -1,11 +1,13 @@
import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
import { JSONContent } from "@tiptap/core";
import { InputModifiers, ToolResultChatMessage, UserChatMessage } from "core";
+import { modelSupportsTools } from "core/llm/autodetect";
import { constructMessages } from "core/llm/constructMessages";
import { getApplicableRules } from "core/llm/rules/getSystemMessageWithRules";
import posthog from "posthog-js";
import { v4 as uuidv4 } from "uuid";
import { getBaseSystemMessage } from "../../util";
+import { selectActiveTools } from "../selectors/selectActiveTools";
import { selectSelectedChatModel } from "../slices/configSlice";
import {
setAppliedRulesAtIndex,
@@ -119,11 +121,16 @@ export const streamResponseThunk = createAsyncThunk<
messageMode,
);
+ const activeTools = selectActiveTools(state);
+ const toolsSupported = modelSupportsTools(selectedChatModel);
+ const availableTools = toolsSupported ? activeTools : [];
+
const messages = constructMessages(
messageMode,
[...updatedHistory],
baseChatOrAgentSystemMessage,
applicableRules,
+ availableTools,
rulePolicies,
);
diff --git a/gui/src/redux/thunks/streamResponseAfterToolCall.ts b/gui/src/redux/thunks/streamResponseAfterToolCall.ts
index 3a29491c5..75134b951 100644
--- a/gui/src/redux/thunks/streamResponseAfterToolCall.ts
+++ b/gui/src/redux/thunks/streamResponseAfterToolCall.ts
@@ -1,8 +1,10 @@
import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
import { ChatMessage } from "core";
+import { modelSupportsTools } from "core/llm/autodetect";
import { constructMessages } from "core/llm/constructMessages";
import { renderContextItems } from "core/util/messageContent";
import { getBaseSystemMessage } from "../../util";
+import { selectActiveTools } from "../selectors/selectActiveTools";
import { selectSelectedChatModel } from "../slices/configSlice";
import { addContextItemsAtIndex, streamUpdate } from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
@@ -67,11 +69,16 @@ export const streamResponseAfterToolCall = createAsyncThunk<
messageMode,
);
+ const activeTools = selectActiveTools(state);
+ const toolsSupported = modelSupportsTools(selectedChatModel);
+ const availableTools = toolsSupported ? activeTools : [];
+
const messages = constructMessages(
messageMode,
[...updatedHistory],
baseChatOrAgentSystemMessage,
state.config.config.rules,
+ availableTools,
);
unwrapResult(await dispatch(streamNormalInput({ messages })));