Refactor ClineSay

This commit is contained in:
Saoud Rizwan
2024-10-06 04:29:48 -04:00
parent d5a998a23a
commit 0932361c7d
4 changed files with 27 additions and 27 deletions

View File

@@ -26,8 +26,8 @@ import {
ClaudeApiReqInfo, ClaudeApiReqInfo,
ClineAsk, ClineAsk,
ClineMessage, ClineMessage,
ClaudeSay, ClineSay,
ClaudeSayTool, ClineSayTool,
} from "../shared/ExtensionMessage" } from "../shared/ExtensionMessage"
import { getApiMetrics } from "../shared/getApiMetrics" import { getApiMetrics } from "../shared/getApiMetrics"
import { HistoryItem } from "../shared/HistoryItem" import { HistoryItem } from "../shared/HistoryItem"
@@ -308,7 +308,7 @@ export class Cline {
this.askResponseImages = images this.askResponseImages = images
} }
async say(type: ClaudeSay, text?: string, images?: string[], partial?: boolean): Promise<undefined> { async say(type: ClineSay, text?: string, images?: string[], partial?: boolean): Promise<undefined> {
if (this.abort) { if (this.abort) {
throw new Error("Cline instance aborted") throw new Error("Cline instance aborted")
} }
@@ -973,7 +973,7 @@ export class Cline {
newContent = newContent.split("\n").slice(0, -1).join("\n").trim() newContent = newContent.split("\n").slice(0, -1).join("\n").trim()
} }
const sharedMessageProps: ClaudeSayTool = { const sharedMessageProps: ClineSayTool = {
tool: fileExists ? "editedExistingFile" : "newFileCreated", tool: fileExists ? "editedExistingFile" : "newFileCreated",
path: getReadablePath(cwd, removeClosingTag("path", relPath)), path: getReadablePath(cwd, removeClosingTag("path", relPath)),
} }
@@ -1028,7 +1028,7 @@ export class Cline {
newContent newContent
) )
: undefined, : undefined,
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
const didApprove = await askApproval("tool", completeMessage) const didApprove = await askApproval("tool", completeMessage)
if (!didApprove) { if (!didApprove) {
await this.diffViewProvider.revertChanges() await this.diffViewProvider.revertChanges()
@@ -1043,7 +1043,7 @@ export class Cline {
tool: fileExists ? "editedExistingFile" : "newFileCreated", tool: fileExists ? "editedExistingFile" : "newFileCreated",
path: getReadablePath(cwd, relPath), path: getReadablePath(cwd, relPath),
diff: userEdits, diff: userEdits,
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
) )
pushToolResult( pushToolResult(
`The user made the following updates to your content:\n\n${userEdits}\n\nThe updated content, which includes both your original modifications and the user's additional edits, has been successfully saved to ${relPath.toPosix()}. (Note this does not mean you need to re-write the file with the user's changes, as they have already been applied to the file.)${newProblemsMessage}` `The user made the following updates to your content:\n\n${userEdits}\n\nThe updated content, which includes both your original modifications and the user's additional edits, has been successfully saved to ${relPath.toPosix()}. (Note this does not mean you need to re-write the file with the user's changes, as they have already been applied to the file.)${newProblemsMessage}`
@@ -1064,7 +1064,7 @@ export class Cline {
} }
case "read_file": { case "read_file": {
const relPath: string | undefined = block.params.path const relPath: string | undefined = block.params.path
const sharedMessageProps: ClaudeSayTool = { const sharedMessageProps: ClineSayTool = {
tool: "readFile", tool: "readFile",
path: getReadablePath(cwd, removeClosingTag("path", relPath)), path: getReadablePath(cwd, removeClosingTag("path", relPath)),
} }
@@ -1073,7 +1073,7 @@ export class Cline {
const partialMessage = JSON.stringify({ const partialMessage = JSON.stringify({
...sharedMessageProps, ...sharedMessageProps,
content: undefined, content: undefined,
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
if (this.alwaysAllowReadOnly) { if (this.alwaysAllowReadOnly) {
await this.say("tool", partialMessage, undefined, block.partial) await this.say("tool", partialMessage, undefined, block.partial)
} else { } else {
@@ -1091,7 +1091,7 @@ export class Cline {
const completeMessage = JSON.stringify({ const completeMessage = JSON.stringify({
...sharedMessageProps, ...sharedMessageProps,
content: absolutePath, content: absolutePath,
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
if (this.alwaysAllowReadOnly) { if (this.alwaysAllowReadOnly) {
await this.say("tool", completeMessage, undefined, false) // need to be sending partialValue bool, since undefined has its own purpose in that the message is treated neither as a partial or completion of a partial, but as a single complete message await this.say("tool", completeMessage, undefined, false) // need to be sending partialValue bool, since undefined has its own purpose in that the message is treated neither as a partial or completion of a partial, but as a single complete message
} else { } else {
@@ -1114,7 +1114,7 @@ export class Cline {
const relDirPath: string | undefined = block.params.path const relDirPath: string | undefined = block.params.path
const recursiveRaw: string | undefined = block.params.recursive const recursiveRaw: string | undefined = block.params.recursive
const recursive = recursiveRaw?.toLowerCase() === "true" const recursive = recursiveRaw?.toLowerCase() === "true"
const sharedMessageProps: ClaudeSayTool = { const sharedMessageProps: ClineSayTool = {
tool: !recursive ? "listFilesTopLevel" : "listFilesRecursive", tool: !recursive ? "listFilesTopLevel" : "listFilesRecursive",
path: getReadablePath(cwd, removeClosingTag("path", relDirPath)), path: getReadablePath(cwd, removeClosingTag("path", relDirPath)),
} }
@@ -1123,7 +1123,7 @@ export class Cline {
const partialMessage = JSON.stringify({ const partialMessage = JSON.stringify({
...sharedMessageProps, ...sharedMessageProps,
content: "", content: "",
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
if (this.alwaysAllowReadOnly) { if (this.alwaysAllowReadOnly) {
await this.say("tool", partialMessage, undefined, block.partial) await this.say("tool", partialMessage, undefined, block.partial)
} else { } else {
@@ -1143,7 +1143,7 @@ export class Cline {
const completeMessage = JSON.stringify({ const completeMessage = JSON.stringify({
...sharedMessageProps, ...sharedMessageProps,
content: result, content: result,
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
if (this.alwaysAllowReadOnly) { if (this.alwaysAllowReadOnly) {
await this.say("tool", completeMessage, undefined, false) await this.say("tool", completeMessage, undefined, false)
} else { } else {
@@ -1162,7 +1162,7 @@ export class Cline {
} }
case "list_code_definition_names": { case "list_code_definition_names": {
const relDirPath: string | undefined = block.params.path const relDirPath: string | undefined = block.params.path
const sharedMessageProps: ClaudeSayTool = { const sharedMessageProps: ClineSayTool = {
tool: "listCodeDefinitionNames", tool: "listCodeDefinitionNames",
path: getReadablePath(cwd, removeClosingTag("path", relDirPath)), path: getReadablePath(cwd, removeClosingTag("path", relDirPath)),
} }
@@ -1171,7 +1171,7 @@ export class Cline {
const partialMessage = JSON.stringify({ const partialMessage = JSON.stringify({
...sharedMessageProps, ...sharedMessageProps,
content: "", content: "",
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
if (this.alwaysAllowReadOnly) { if (this.alwaysAllowReadOnly) {
await this.say("tool", partialMessage, undefined, block.partial) await this.say("tool", partialMessage, undefined, block.partial)
} else { } else {
@@ -1192,7 +1192,7 @@ export class Cline {
const completeMessage = JSON.stringify({ const completeMessage = JSON.stringify({
...sharedMessageProps, ...sharedMessageProps,
content: result, content: result,
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
if (this.alwaysAllowReadOnly) { if (this.alwaysAllowReadOnly) {
await this.say("tool", completeMessage, undefined, false) await this.say("tool", completeMessage, undefined, false)
} else { } else {
@@ -1213,7 +1213,7 @@ export class Cline {
const relDirPath: string | undefined = block.params.path const relDirPath: string | undefined = block.params.path
const regex: string | undefined = block.params.regex const regex: string | undefined = block.params.regex
const filePattern: string | undefined = block.params.file_pattern const filePattern: string | undefined = block.params.file_pattern
const sharedMessageProps: ClaudeSayTool = { const sharedMessageProps: ClineSayTool = {
tool: "searchFiles", tool: "searchFiles",
path: getReadablePath(cwd, removeClosingTag("path", relDirPath)), path: getReadablePath(cwd, removeClosingTag("path", relDirPath)),
regex: removeClosingTag("regex", regex), regex: removeClosingTag("regex", regex),
@@ -1224,7 +1224,7 @@ export class Cline {
const partialMessage = JSON.stringify({ const partialMessage = JSON.stringify({
...sharedMessageProps, ...sharedMessageProps,
content: "", content: "",
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
if (this.alwaysAllowReadOnly) { if (this.alwaysAllowReadOnly) {
await this.say("tool", partialMessage, undefined, block.partial) await this.say("tool", partialMessage, undefined, block.partial)
} else { } else {
@@ -1248,7 +1248,7 @@ export class Cline {
const completeMessage = JSON.stringify({ const completeMessage = JSON.stringify({
...sharedMessageProps, ...sharedMessageProps,
content: results, content: results,
} satisfies ClaudeSayTool) } satisfies ClineSayTool)
if (this.alwaysAllowReadOnly) { if (this.alwaysAllowReadOnly) {
await this.say("tool", completeMessage, undefined, false) await this.say("tool", completeMessage, undefined, false)
} else { } else {
@@ -1267,7 +1267,7 @@ export class Cline {
} }
case "inspect_site": { case "inspect_site": {
const url: string | undefined = block.params.url const url: string | undefined = block.params.url
const sharedMessageProps: ClaudeSayTool = { const sharedMessageProps: ClineSayTool = {
tool: "inspectSite", tool: "inspectSite",
path: removeClosingTag("url", url), path: removeClosingTag("url", url),
} }

View File

@@ -41,7 +41,7 @@ export interface ClineMessage {
ts: number ts: number
type: "ask" | "say" type: "ask" | "say"
ask?: ClineAsk ask?: ClineAsk
say?: ClaudeSay say?: ClineSay
text?: string text?: string
images?: string[] images?: string[]
partial?: boolean partial?: boolean
@@ -58,7 +58,7 @@ export type ClineAsk =
| "resume_completed_task" | "resume_completed_task"
| "mistake_limit_reached" | "mistake_limit_reached"
export type ClaudeSay = export type ClineSay =
| "task" | "task"
| "error" | "error"
| "api_req_started" | "api_req_started"
@@ -73,7 +73,7 @@ export type ClaudeSay =
| "shell_integration_warning" | "shell_integration_warning"
| "inspect_site_result" | "inspect_site_result"
export interface ClaudeSayTool { export interface ClineSayTool {
tool: tool:
| "editedExistingFile" | "editedExistingFile"
| "newFileCreated" | "newFileCreated"

View File

@@ -1,7 +1,7 @@
import { VSCodeBadge, VSCodeProgressRing } from "@vscode/webview-ui-toolkit/react" import { VSCodeBadge, VSCodeProgressRing } from "@vscode/webview-ui-toolkit/react"
import deepEqual from "fast-deep-equal" import deepEqual from "fast-deep-equal"
import React, { memo, useEffect, useMemo, useRef } from "react" import React, { memo, useEffect, useMemo, useRef } from "react"
import { ClaudeApiReqInfo, ClineMessage, ClaudeSayTool } from "../../../../src/shared/ExtensionMessage" import { ClaudeApiReqInfo, ClineMessage, ClineSayTool } from "../../../../src/shared/ExtensionMessage"
import { COMMAND_OUTPUT_STRING } from "../../../../src/shared/combineCommandSequences" import { COMMAND_OUTPUT_STRING } from "../../../../src/shared/combineCommandSequences"
import { vscode } from "../../utils/vscode" import { vscode } from "../../utils/vscode"
import CodeAccordian, { removeLeadingNonAlphanumeric } from "../common/CodeAccordian" import CodeAccordian, { removeLeadingNonAlphanumeric } from "../common/CodeAccordian"
@@ -193,7 +193,7 @@ const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifiedMessa
const tool = useMemo(() => { const tool = useMemo(() => {
if (message.ask === "tool" || message.say === "tool") { if (message.ask === "tool" || message.say === "tool") {
return JSON.parse(message.text || "{}") as ClaudeSayTool return JSON.parse(message.text || "{}") as ClineSayTool
} }
return null return null
}, [message.ask, message.say, message.text]) }, [message.ask, message.say, message.text])
@@ -533,7 +533,7 @@ const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifiedMessa
</div> </div>
) )
case "user_feedback_diff": case "user_feedback_diff":
const tool = JSON.parse(message.text || "{}") as ClaudeSayTool const tool = JSON.parse(message.text || "{}") as ClineSayTool
return ( return (
<div <div
style={{ style={{

View File

@@ -4,7 +4,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react"
import { useDeepCompareEffect, useEvent, useMount } from "react-use" import { useDeepCompareEffect, useEvent, useMount } from "react-use"
import { Virtuoso, type VirtuosoHandle } from "react-virtuoso" import { Virtuoso, type VirtuosoHandle } from "react-virtuoso"
import styled from "styled-components" import styled from "styled-components"
import { ClineAsk, ClaudeSayTool, ExtensionMessage } from "../../../../src/shared/ExtensionMessage" import { ClineAsk, ClineSayTool, ExtensionMessage } from "../../../../src/shared/ExtensionMessage"
import { findLast } from "../../../../src/shared/array" import { findLast } from "../../../../src/shared/array"
import { combineApiRequests } from "../../../../src/shared/combineApiRequests" import { combineApiRequests } from "../../../../src/shared/combineApiRequests"
import { combineCommandSequences } from "../../../../src/shared/combineCommandSequences" import { combineCommandSequences } from "../../../../src/shared/combineCommandSequences"
@@ -92,7 +92,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
setTextAreaDisabled(isPartial) setTextAreaDisabled(isPartial)
setClineAsk("tool") setClineAsk("tool")
setEnableButtons(!isPartial) setEnableButtons(!isPartial)
const tool = JSON.parse(lastMessage.text || "{}") as ClaudeSayTool const tool = JSON.parse(lastMessage.text || "{}") as ClineSayTool
switch (tool.tool) { switch (tool.tool) {
case "editedExistingFile": case "editedExistingFile":
case "newFileCreated": case "newFileCreated":