Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
1.4 kB · 46 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647export type SlashCommandContext = | { kind: "name"; prefix: string; isAtPromptStart: boolean } | { kind: "argument"; commandName: string; prefix: string; isAtPromptStart: true };
export function getSlashCommandContext( lines: string[], cursorLine: number, cursorCol: number,): SlashCommandContext | null { const currentLine = lines[cursorLine] ?? ""; const textBeforeCursor = currentLine.slice(0, cursorCol); const trimmedStart = textBeforeCursor.trimStart();
if (cursorLine === 0 && trimmedStart.startsWith("/")) { const separatorIndex = trimmedStart.search(/[ \t]/); const commandToken = separatorIndex === -1 ? trimmedStart : trimmedStart.slice(0, separatorIndex);
if (commandToken.slice(1).includes("/")) { return null; }
if (separatorIndex === -1) { return { kind: "name", prefix: commandToken, isAtPromptStart: true }; }
const commandName = commandToken.slice(1); if (!commandName) { return null; }
return { kind: "argument", commandName, prefix: trimmedStart.slice(separatorIndex + 1), isAtPromptStart: true, }; }
const tokenStart = Math.max(textBeforeCursor.lastIndexOf(" "), textBeforeCursor.lastIndexOf("\t")) + 1; const prefix = textBeforeCursor.slice(tokenStart); if (!prefix.startsWith("/") || prefix.slice(1).includes("/")) { return null; }
return { kind: "name", prefix, isAtPromptStart: false };}