// ***************************************************************************** // Copyright (C) 2025 EclipseSource GmbH. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { ChatResponsePartRenderer } from '@theia/ai-chat-ui/lib/browser/chat-response-part-renderer'; import { ResponseNode } from '@theia/ai-chat-ui/lib/browser/chat-tree-view'; import { ChatResponseContent, ToolCallChatResponseContent } from '@theia/ai-chat/lib/common'; import { codicon, LabelProvider } from '@theia/core/lib/browser'; import { URI } from '@theia/core/lib/common/uri'; import { inject, injectable, named } from '@theia/core/shared/inversify'; import * as React from '@theia/core/shared/react'; import { ReactNode } from '@theia/core/shared/react'; import { EditorManager } from '@theia/editor/lib/browser'; import { WorkspaceService } from '@theia/workspace/lib/browser'; import { ClaudeCodeToolCallChatResponseContent } from '../claude-code-tool-call-content'; import { CollapsibleToolRenderer } from './collapsible-tool-renderer'; import { nls, ILogger } from '@theia/core'; interface LSToolInput { path: string; ignore?: string[]; } @injectable() export class LSToolRenderer implements ChatResponsePartRenderer { @inject(WorkspaceService) protected readonly workspaceService: WorkspaceService; @inject(LabelProvider) protected readonly labelProvider: LabelProvider; @inject(EditorManager) protected readonly editorManager: EditorManager; @inject(ILogger) @named('ai-claude-code:LSToolRenderer') protected readonly logger: ILogger; canHandle(response: ChatResponseContent): number { if (ClaudeCodeToolCallChatResponseContent.is(response) && response.name === 'LS') { return 15; // Higher than default ToolCallPartRenderer (10) } return -1; } render(response: ToolCallChatResponseContent, parentNode: ResponseNode): ReactNode { try { const input = JSON.parse(response.arguments || '{}') as LSToolInput; return ; } catch (error) { this.logger.warn('Failed to parse LS tool input:', error); return
{nls.localize('theia/ai/claude-code/failedToParseLSToolData', 'Failed to parse LS tool data')}
; } } } const LSToolComponent: React.FC<{ input: LSToolInput; workspaceService: WorkspaceService; labelProvider: LabelProvider; editorManager: EditorManager; }> = ({ input, workspaceService, labelProvider, editorManager }) => { const getDirectoryName = (dirPath: string): string => dirPath.split('/').pop() || dirPath; const getWorkspaceRelativePath = async (dirPath: string): Promise => { try { const absoluteUri = new URI(dirPath); const workspaceRelativePath = await workspaceService.getWorkspaceRelativePath(absoluteUri); return workspaceRelativePath || ''; } catch { return ''; } }; const handleOpenDirectory = async () => { try { const uri = new URI(input.path); // Note: This might need to be adjusted based on how directories are opened in Theia await editorManager.open(uri); } catch (error) { console.error('Failed to open directory:', error); } }; const [relativePath, setRelativePath] = React.useState(''); React.useEffect(() => { getWorkspaceRelativePath(input.path).then(setRelativePath); }, [input.path]); const compactHeader = ( <>
{nls.localize('theia/ai/claude-code/listing', 'Listing')} {getDirectoryName(input.path)} {relativePath && {relativePath}}
{input.ignore && input.ignore.length > 0 && ( {nls.localize('theia/ai/claude-code/ignoringPatterns', 'Ignoring {0} patterns', input.ignore.length)} )}
); const expandedContent = (
{nls.localizeByDefault('Directory')} {input.path}
{input.ignore && input.ignore.length > 0 && (
{nls.localize('theia/ai/claude-code/ignoredPatterns', 'Ignored Patterns')}
{input.ignore.map((pattern, index) => ( {pattern}{index < input.ignore!.length - 1 ? ', ' : ''} ))}
)}
{nls.localizeByDefault('Description')} {nls.localize('theia/ai/claude-code/listDirectoryContents', 'List directory contents')}{input.ignore && input.ignore.length > 0 ? (input.ignore.length > 1 ? nls.localize('theia/ai/claude-code/excludingPatterns', ' (excluding {0} patterns)', input.ignore.length) : nls.localize('theia/ai/claude-code/excludingOnePattern', ' (exluding 1 pattern)')) : ''}
); return ( ); };