Skip to content

Commit b75b19e

Browse files
Update toolMentionParser.ts
1 parent 2c52dec commit b75b19e

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

app/utils/toolMentionParser.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export interface AutocompleteState {
44
atPosition: number;
55
}
66

7+
export type ReferenceType = 'file' | 'tool';
8+
79
export function shouldShowAutocomplete(text: string, cursorPos: number): AutocompleteState {
810
const textBeforeCursor = text.slice(0, cursorPos);
911
const lastAtIndex = textBeforeCursor.lastIndexOf('@');
@@ -34,6 +36,10 @@ export function extractSearchQuery(text: string, cursorPos: number): string {
3436
return state.searchQuery;
3537
}
3638

39+
export function detectReferenceType(searchQuery: string): ReferenceType {
40+
return /[\/\.]/.test(searchQuery) ? 'file' : 'tool';
41+
}
42+
3743
function getTextWidth(text: string, font: string): number {
3844
const canvas = document.createElement('canvas');
3945
const context = canvas.getContext('2d');
@@ -94,3 +100,22 @@ export function insertToolMention(
94100

95101
return { newText, newCursorPos };
96102
}
103+
104+
export function insertFileReference(
105+
text: string,
106+
cursorPos: number,
107+
filePath: string,
108+
): { newText: string; newCursorPos: number } {
109+
const state = shouldShowAutocomplete(text, cursorPos);
110+
111+
if (!state.isOpen) {
112+
return { newText: text, newCursorPos: cursorPos };
113+
}
114+
115+
const beforeAt = text.slice(0, state.atPosition);
116+
const afterCursor = text.slice(cursorPos);
117+
const newText = `${beforeAt}@${filePath} ${afterCursor}`;
118+
const newCursorPos = state.atPosition + filePath.length + 2;
119+
120+
return { newText, newCursorPos };
121+
}

0 commit comments

Comments
 (0)