Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/services/command/__tests__/built-in-commands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ describe("Built-in Commands", () => {
it("should return all built-in commands", async () => {
const commands = await getBuiltInCommands()

expect(commands).toHaveLength(1)
expect(commands.map((cmd) => cmd.name)).toEqual(expect.arrayContaining(["init"]))
expect(commands).toHaveLength(3)
expect(commands.map((cmd) => cmd.name)).toEqual(expect.arrayContaining(["init", "compact", "compact-and"]))

// Verify all commands have required properties
commands.forEach((command) => {
Expand Down Expand Up @@ -63,10 +63,10 @@ describe("Built-in Commands", () => {
it("should return all built-in command names", async () => {
const names = await getBuiltInCommandNames()

expect(names).toHaveLength(1)
expect(names).toEqual(expect.arrayContaining(["init"]))
expect(names).toHaveLength(3)
expect(names).toEqual(expect.arrayContaining(["init", "compact", "compact-and"]))
// Order doesn't matter since it's based on filesystem order
expect(names.sort()).toEqual(["init"])
expect(names.sort()).toEqual(["compact", "compact-and", "init"])
})

it("should return array of strings", async () => {
Expand Down
11 changes: 11 additions & 0 deletions src/services/command/built-in-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ interface BuiltInCommandDefinition {
}

const BUILT_IN_COMMANDS: Record<string, BuiltInCommandDefinition> = {
compact: {
name: "compact",
description: "Manually condense the current task context to free up tokens",
content: "",
},
"compact-and": {
name: "compact-and",
description: "Condense context, then send the following message",
argumentHint: "<message to send after condensing>",
content: "",
},
init: {
name: "init",
description: "Analyze codebase and create concise AGENTS.md files for AI assistants",
Expand Down
66 changes: 57 additions & 9 deletions webview-ui/src/components/chat/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
const [playProgressLoop] = useSound(`${audioBaseUri}/progress_loop.wav`, { volume, soundEnabled, interrupt: true })

const lastPlayedRef = useRef<Record<string, number>>({})
const pendingPostCompactRef = useRef<{ text: string; images: string[] } | null>(null)

const playSound = useCallback(
(audioType: AudioType) => {
Expand Down Expand Up @@ -575,6 +576,18 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
// setSecondaryButtonText(undefined)
}, [])

const handleCondenseContext = useCallback(
(taskId: string) => {
if (isCondensing || sendingDisabled) {
return
}
setIsCondensing(true)
setSendingDisabled(true)
vscode.postMessage({ type: "condenseTaskContextRequest", text: taskId })
},
[isCondensing, sendingDisabled],
)

/**
* Handles sending messages to the extension
* @param text - The message text to send
Expand All @@ -585,6 +598,32 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
text = text.trim()

if (text || images.length > 0) {
// Intercept /compact and /compact-and before any other processing.
// /compact triggers the same context-condensing used by auto-condense.
// /compact-and <msg> condenses, then sends <msg> once condensing completes.
const compactMatch = /^\/compact(-and)?(?:\s+([\s\S]+))?$/.exec(text)
if (compactMatch) {
const taskId = currentTaskItem?.id
if (!taskId || messagesRef.current.length === 0 || isCondensing || sendingDisabled) {
// Nothing to condense, or a condense is already in flight.
setInputValue("")
setSelectedImages([])
return
}

const followUp = (compactMatch[2] ?? "").trim()
if (compactMatch[1] && followUp) {
pendingPostCompactRef.current = { text: followUp, images }
}

setIsCondensing(true)
setSendingDisabled(true)
vscode.postMessage({ type: "condenseTaskContextRequest", text: taskId })
setInputValue("")
setSelectedImages([])
return
}

// Intercept when the active provider is retired — show a
// WarningRow instead of sending anything to the backend.
if (apiConfiguration?.apiProvider && isRetiredProvider(apiConfiguration.apiProvider)) {
Expand Down Expand Up @@ -663,9 +702,27 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
isStreaming,
messageQueue.length,
apiConfiguration?.apiProvider,
currentTaskItem?.id,
isCondensing,
], // messagesRef and clineAskRef are stable
)

// After /compact-and completes, dispatch the follow-up message stashed
// when the user submitted the command.
useEffect(() => {
if (isCondensing || sendingDisabled) {
return
}

const pending = pendingPostCompactRef.current
if (!pending) {
return
}

pendingPostCompactRef.current = null
handleSendMessage(pending.text, pending.images)
}, [isCondensing, sendingDisabled, handleSendMessage])

const handleSetChatBoxMessage = useCallback(
(text: string, images: string[]) => {
// Avoid nested template literals by breaking down the logic
Expand Down Expand Up @@ -1539,15 +1596,6 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
},
}))

const handleCondenseContext = (taskId: string) => {
if (isCondensing || sendingDisabled) {
return
}
setIsCondensing(true)
setSendingDisabled(true)
vscode.postMessage({ type: "condenseTaskContextRequest", text: taskId })
}

const areButtonsVisible = showScrollToBottom || primaryButtonText || secondaryButtonText

return (
Expand Down
Loading