Skip to content

Commit 9268dff

Browse files
committed
refactor: fix DRY violations and code quality issues
- Extract parseContextualReferences to shared utils to avoid duplication - Remove duplicate ContextualItem import - Clean up unused comments and dead code - Improve code organization and maintainability
1 parent 8bf3ea0 commit 9268dff

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

multimodal/tarko/agent-web-ui/src/standalone/chat/MessageInput/ChatInput.tsx

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import {
1010
addContextualItemAction,
1111
updateSelectorStateAction,
1212
clearContextualStateAction,
13+
ContextualItem,
1314
} from '@/common/state/atoms/contextualSelector';
14-
import { ContextualSelector, ContextualItem } from '../ContextualSelector';
15+
import { ContextualSelector } from '../ContextualSelector';
1516
import { MessageAttachments } from './MessageAttachments';
1617
import { ImagePreviewInline } from './ImagePreviewInline';
1718
import { getAgentTitle, isContextualSelectorEnabled } from '@/config/web-ui-config';
18-
import { composeMessageContent, isMessageEmpty } from './utils';
19+
import { composeMessageContent, isMessageEmpty, parseContextualReferences } from './utils';
1920

2021
interface ChatInputProps {
2122
onSubmit: (content: string | ChatCompletionContentPart[]) => Promise<void>;
@@ -85,9 +86,6 @@ export const ChatInput: React.FC<ChatInputProps> = ({
8586
}
8687
}, [initialValue, contextualState.input, setContextualState]);
8788

88-
// Remove the effect that keeps images during processing
89-
// Images should be cleared immediately after sending
90-
9189
useEffect(() => {
9290
if (!isDisabled && autoFocus && inputRef.current) {
9391
inputRef.current.focus();
@@ -146,35 +144,7 @@ export const ChatInput: React.FC<ChatInputProps> = ({
146144
};
147145

148146
// Parse contextual references from input text
149-
const parseContextualReferences = (text: string): ContextualItem[] => {
150-
const contextualReferencePattern = /@(file|dir):([^\s]+)/g;
151-
const workspacePattern = /@workspace/g;
152-
153-
const contextualRefs = Array.from(text.matchAll(contextualReferencePattern)).map(
154-
(match, index) => {
155-
const [fullMatch, type, relativePath] = match;
156-
const name = relativePath.split(/[/\\]/).pop() || relativePath;
157-
158-
return {
159-
id: `${type}-${relativePath}-${index}`,
160-
type: type as 'file' | 'directory',
161-
name,
162-
path: relativePath,
163-
relativePath,
164-
};
165-
},
166-
);
167-
168-
const workspaceRefs = Array.from(text.matchAll(workspacePattern)).map((match, index) => ({
169-
id: `workspace-${index}`,
170-
type: 'workspace' as const,
171-
name: 'workspace',
172-
path: '/',
173-
relativePath: '.',
174-
}));
175147

176-
return [...contextualRefs, ...workspaceRefs];
177-
};
178148

179149
const handleContextualSelect = (item: ContextualItem) => {
180150
addContextualItem(item);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { MessageAttachments } from './MessageAttachments';
22
export { ChatInput } from './ChatInput';
33
export { ImagePreviewInline } from './ImagePreviewInline';
4-
export { composeMessageContent, isMessageEmpty } from './utils';
4+
export { composeMessageContent, isMessageEmpty, parseContextualReferences } from './utils';

multimodal/tarko/agent-web-ui/src/standalone/chat/MessageInput/utils.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ChatCompletionContentPart } from '@tarko/agent-interface';
2+
import { ContextualItem } from '@/common/state/atoms/contextualSelector';
23

34
/**
45
* Compose multimodal message content from text and images
@@ -33,3 +34,39 @@ export const composeMessageContent = (
3334
export const isMessageEmpty = (text: string, images: ChatCompletionContentPart[] = []): boolean => {
3435
return !text.trim() && images.length === 0;
3536
};
37+
38+
/**
39+
* Parse contextual references from text - shared utility
40+
*
41+
* @param text - The text to parse
42+
* @returns Array of contextual items found in the text
43+
*/
44+
export const parseContextualReferences = (text: string): ContextualItem[] => {
45+
const contextualReferencePattern = /@(file|dir):([^\s]+)/g;
46+
const workspacePattern = /@workspace/g;
47+
48+
const contextualRefs = Array.from(text.matchAll(contextualReferencePattern)).map(
49+
(match, index) => {
50+
const [fullMatch, type, relativePath] = match;
51+
const name = relativePath.split(/[/\\]/).pop() || relativePath;
52+
53+
return {
54+
id: `${type}-${relativePath}-${index}`,
55+
type: type as 'file' | 'directory',
56+
name,
57+
path: relativePath,
58+
relativePath,
59+
};
60+
},
61+
);
62+
63+
const workspaceRefs = Array.from(text.matchAll(workspacePattern)).map((match, index) => ({
64+
id: `workspace-${index}`,
65+
type: 'workspace' as const,
66+
name: 'workspace',
67+
path: '/',
68+
relativePath: '.',
69+
}));
70+
71+
return [...contextualRefs, ...workspaceRefs];
72+
};

0 commit comments

Comments
 (0)