openai-code
Version:
An unofficial proxy layer that lets you use Anthropic Claude Code with any OpenAI API backend.
47 lines (39 loc) • 1.87 kB
JavaScript
export const commandPatterns = {
// :v or :v<number> even mutliple times (last one wins)
vectorSearch: /^:v(\d+)?/,
// :d or :d<number> ; :d default to 3 (maximum reasoning thought graph depth)
deepThought: /^:d(\d+)?/,
// :p for activating Perplexity agent, <number> is unused
perplexity: /^:p(\d+)?/,
// :so for activating StackOverflow agent, <number> is unused
stackOverflow: /^:so(\d+)?/,
};
export const getCommand = (commands, commandName, defaultValue) =>
commandName in commands ?
commands[commandName] === null ? defaultValue /** command defined but no numeric option */
: commands[commandName] /** command defined with numeric option */
: null /** command was undefined */;
export const parseCommands = (commandLine) => {
const commands = {};
let hasMatch = false; // flag to check if any pattern matches
// iterate over each command pattern in commandPatterns
for (const patternKey in commandPatterns) {
const pattern = commandPatterns[patternKey];
let commandMatch = commandLine.match(pattern);
while (commandMatch !== null) {
hasMatch = true; // set flag to true if a match is found
// use the patternKey as the command name and capture the optional number
const commandValue = commandMatch[1] ? Number.parseInt(commandMatch[1]) : 0;
commands[patternKey] = commandValue;
console.log(`[!] Command: ${patternKey} activated${commandValue !== 0 ? ` with value: ${commandValue}` : ''}`);
// remove the matched command from the command line using the matched string length
commandLine = commandLine.replace(commandMatch[0], '').trim();
commandMatch = commandLine.match(pattern);
if (typeof commands[patternKey] !== "number") {
commands[patternKey] = null;
}
}
}
commands.commandLine = commandLine;
return commands;
}