openai-code
Version:
An unofficial proxy layer that lets you use Anthropic Claude Code with any OpenAI API backend.
92 lines (78 loc) • 2.64 kB
JavaScript
import { getRelevantCodeForPrompt } from "../vectorindex.mjs";
// unused atm
export const useCodeSearch = async ({ query, formattedMessages, topK = 5 }) => {
// add a message indicating that the ProjectSearch tool has been requested
formattedMessages.push({
role: "user",
content: [{
type: 'text',
text: `Tool ProjectSearch with query: ${query} and top_k: ${topK} was planned to be used.`,
}]
});
// add a dummy result message for the ProjectSearch tool
formattedMessages.push({
role: "assistant",
content: [{
type: "text",
text: `Tool ProjectSearch was executed. Vector database search results for "${query}": ${
await getRelevantCodeForPrompt(query, topK)
}`
}]
});
formattedMessages.push({
role: "user",
content: [{
type: 'text',
text: 'Consider the Vector database results too, if relevant.',
}]
})
return {
formattedMessages
}
}
export const runProjectSearchAgent = async({ useTools, formattedMessages }) => {
// check if useTools contains GrepTool or GlobTool
const grepOrGlobTool = useTools.find(tool => tool.name === "GrepTool" || tool.name === "GlobTool");
// derive a boolean to check if ProjectSearch tool is part of useTools
const projectSearchToolIndex = useTools.findIndex(tool => tool.name === "ProjectSearch");
let projectSearchQuery = null;
if (projectSearchToolIndex !== -1) {
const projectSearchTool = useTools[projectSearchToolIndex];
projectSearchQuery = projectSearchTool.parameters.query;
// remove the ProjectSearch tool from useTools
useTools.splice(projectSearchToolIndex, 1);
}
// make sure to add the ProjectSearch tool if GrepTool or GlobTool is present and ProjectSearch is not already in useTools
if (grepOrGlobTool && projectSearchToolIndex === -1) {
projectSearchQuery = grepOrGlobTool.parameters.pattern;
}
if (projectSearchQuery) {
return {
useTools,
reReason: true,
...await useCodeSearch({ query: projectSearchQuery, formattedMessages, topK: 5 })
}
}
return {
useTools,
reReason: false,
formattedMessages,
}
}
export const codesearchAgent = async({ useTools, formattedMessages, decisions, reasoningAttempts }) => {
if (decisions?.codeSearchQuery && reasoningAttempts === 0) {
return {
useTools,
reReason: true,
...await useCodeSearch({ query: decisions.codeSearchQuery, formattedMessages, topK: 5 })
}
}
if (useTools && reasoningAttempts === 0) {
return runProjectSearchAgent()
}
return {
useTools,
formattedMessages,
reReason: false
}
}