From c7ed2aa62e2b142f5e80448afc264bdc18d44c79 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Fri, 3 Jan 2025 16:41:14 +0100 Subject: [PATCH] gui strict null checks continued --- .../StepContainer/UndoAndRedoButtons.tsx | 13 +++++----- gui/src/components/mainInput/resolveInput.ts | 18 ++++++------- .../StepContainerPreActionButtons.tsx | 2 +- .../StepContainerPreToolbar.tsx | 6 ++--- .../markdown/StyledMarkdownPreview.tsx | 6 ++--- gui/src/components/markdown/utils.ts | 2 +- gui/src/redux/slices/sessionSlice.ts | 26 +++++++++---------- 7 files changed, 37 insertions(+), 36 deletions(-) diff --git a/gui/src/components/StepContainer/UndoAndRedoButtons.tsx b/gui/src/components/StepContainer/UndoAndRedoButtons.tsx index a57b29bd2..e0e57c3e1 100644 --- a/gui/src/components/StepContainer/UndoAndRedoButtons.tsx +++ b/gui/src/components/StepContainer/UndoAndRedoButtons.tsx @@ -12,9 +12,7 @@ export default function UndoAndRedoButtons() { const dispatch = useDispatch(); const ideMessenger = useContext(IdeMessengerContext); - const checkpoints = useAppSelector( - (store) => store.session.history.at(-1).checkpoint, - ); + const history = useAppSelector((store) => store.session.history); const curCheckpointIndex = useAppSelector( (store) => store.session.curCheckpointIndex, @@ -28,10 +26,13 @@ export default function UndoAndRedoButtons() { type === "undo" ? curCheckpointIndex - 1 : curCheckpointIndex + 1, ); - const checkpoint = checkpoints[checkpointIndex]; + const checkpoint = history[checkpointIndex]?.checkpoint ?? {}; - for (const [filepath, prevFileContent] of Object.entries(checkpoint)) { - ideMessenger.post("overwriteFile", { filepath, prevFileContent }); + for (const [filepath, cachedFileContent] of Object.entries(checkpoint)) { + ideMessenger.post("overwriteFile", { + filepath, + prevFileContent: cachedFileContent, + }); } dispatch(setCurCheckpointIndex(checkpointIndex)); diff --git a/gui/src/components/mainInput/resolveInput.ts b/gui/src/components/mainInput/resolveInput.ts index 9c5107d1b..535a3074f 100644 --- a/gui/src/components/mainInput/resolveInput.ts +++ b/gui/src/components/mainInput/resolveInput.ts @@ -122,8 +122,8 @@ async function resolveEditorContent({ let contextItems: ContextItemWithId[] = []; for (const item of contextItemAttrs) { const result = await ideMessenger.request("context/getContextItems", { - name: item.itemType === "contextProvider" ? item.id : item.itemType, - query: item.query, + name: item.itemType === "contextProvider" ? item.id : item.itemType!, + query: item.query ?? "", fullInput: stripImages(parts), selectedCode, selectedModelTitle, @@ -211,22 +211,22 @@ function resolveParagraph( p: JSONContent, ): [string, MentionAttrs[], string | undefined] { let text = ""; - const contextItems = []; + const contextItems: MentionAttrs[] = []; let slashCommand: string | undefined = undefined; for (const child of p.content || []) { if (child.type === "text") { - text += text === "" ? child.text.trimStart() : child.text; + text += text === "" ? child.text?.trimStart() : child.text; } else if (child.type === "mention") { text += - typeof child.attrs.renderInlineAs === "string" + typeof child.attrs?.renderInlineAs === "string" ? child.attrs.renderInlineAs - : child.attrs.label; - contextItems.push(child.attrs); + : child.attrs?.label; + contextItems.push(child.attrs as MentionAttrs); } else if (child.type === "slashcommand") { if (typeof slashCommand === "undefined") { - slashCommand = child.attrs.id; + slashCommand = child.attrs?.id; } else { - text += child.attrs.label; + text += child.attrs?.label; } } else { console.warn("Unexpected child type", child.type); diff --git a/gui/src/components/markdown/StepContainerPreActionButtons.tsx b/gui/src/components/markdown/StepContainerPreActionButtons.tsx index 1afbe2a27..4ecce8315 100644 --- a/gui/src/components/markdown/StepContainerPreActionButtons.tsx +++ b/gui/src/components/markdown/StepContainerPreActionButtons.tsx @@ -50,7 +50,7 @@ const InnerHoverDiv = styled.div<{ isBottomToolbarPosition: boolean }>` `; interface StepContainerPreActionButtonsProps { - language: string; + language: string | null; codeBlockContent: string; codeBlockIndex: number; children: any; diff --git a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx index c6a0d58f0..45b55f10b 100644 --- a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx +++ b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx @@ -44,7 +44,7 @@ const ToolbarDiv = styled.div<{ isExpanded: boolean }>` export interface StepContainerPreToolbarProps { codeBlockContent: string; - language: string; + language: string | null; relativeFilepath: string; isGeneratingCodeBlock: boolean; codeBlockIndex: number; // To track which codeblock we are applying @@ -93,12 +93,12 @@ export default function StepContainerPreToolbar( if (!defaultModel) { return; } - + const fileUri = await inferResolvedUriFromRelativePath( props.relativeFilepath, ideMessenger.ide, ); - + ideMessenger.post("applyToFile", { streamId: streamIdRef.current, filepath: fileUri, diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index a6eb2c569..5ad8c52c2 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -228,7 +228,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( ], rehypeReactOptions: { components: { - a: ({ node, ...aProps }) => { + a: ({ ...aProps }) => { const tooltipId = uuidv4(); return ( @@ -247,7 +247,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( ); }, - pre: ({ node, ...preProps }) => { + pre: ({ ...preProps }) => { const codeBlockIndex = preProps["data-codeblockindex"]; const preChildProps = preProps?.children?.[0]?.props ?? {}; @@ -293,7 +293,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( ); }, - code: ({ node, ...codeProps }) => { + code: ({ ...codeProps }) => { const content = getCodeChildrenContent(codeProps.children); if (content && previousFileContextItemsRef.current) { diff --git a/gui/src/components/markdown/utils.ts b/gui/src/components/markdown/utils.ts index 9b2329303..9435abe47 100644 --- a/gui/src/components/markdown/utils.ts +++ b/gui/src/components/markdown/utils.ts @@ -26,7 +26,7 @@ export function getTerminalCommand(text: string): string { } export function isTerminalCodeBlock( - language: string | undefined, + language: string | undefined | null, text: string, ) { return ( diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 7cd06594b..2ad3ee89e 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -288,9 +288,7 @@ export const sessionSlice = createSlice({ message.role && (lastMessage.role !== message.role || // This is when a tool call comes after assistant text - (lastMessage.content !== "" && - message.role === "assistant" -)) + (lastMessage.content !== "" && message.role === "assistant")) ) { // Create a new message const historyItem: ChatHistoryItemWithMessageId = { @@ -300,17 +298,19 @@ export const sessionSlice = createSlice({ }, contextItems: [], }; - const toolCalls = if (message.role === "assistant" && message.toolCalls?.[0]) { - const [_, parsedArgs] = incrementalParseJson( - message.toolCalls[0].function.arguments, - ); - historyItem.toolCallState = { - status: "generating", - toolCall: message.toolCalls[0] as ToolCall, - toolCallId: message.toolCalls[0].id, - parsedArgs, - }; + const toolCalls = message.toolCalls?.[0]; + if (toolCalls) { + const [_, parsedArgs] = incrementalParseJson( + message.toolCalls[0].function.arguments, + ); + historyItem.toolCallState = { + status: "generating", + toolCall: message.toolCalls[0] as ToolCall, + toolCallId: message.toolCalls[0].id, + parsedArgs, + }; + } } state.history.push(historyItem);