openai-code
Version:
An unofficial proxy layer that lets you use Anthropic Claude Code with any OpenAI API backend.
49 lines (38 loc) • 1.66 kB
JavaScript
import { getEnv } from '../env.mjs';
import { getCommand } from '../command.mjs';
import { scrape } from '../scrape.mjs';
export const fetchStackOverflowAnswers = async (searchTerm, topK = 3) => {
try {
const apiUrl = `https://api.stackexchange.com/2.3/search/advanced?q=${encodeURIComponent(searchTerm)}&site=stackoverflow`;
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`Error fetching data: ${response.statusText}`);
}
const data = await response.json();
if (data.items && data.items.length > 0) {
const resultPageMarkdown = []
const answeredLinks = data.items
.filter(item => item.is_answered) // filter for answered questions
.map(item => item.link); // map to get the link of each answered question
const topAnsweredLinks = answeredLinks.slice(0, topK); // limit to top 3 answered questions
for (const link of topAnsweredLinks) {
const scrapeResult = await scrape(link, '#answers');
if (scrapeResult?.markdown) {
resultPageMarkdown.push(scrapeResult?.markdown); // scrape the content of each link
}
}
return answeredLinks.map(link => {
return {
link,
markdown: resultPageMarkdown[answeredLinks.indexOf(link)]
};
});
}
return [];
} catch (error) {
console.error('Error fetching data:', error);
throw error; // rethrow the error after logging
}
};
export const getStackOverflowN = (commands) => getCommand(commands, 'stackOverflow', 0);
export const isStackOverflowActivated = (commands) => getStackOverflowN(commands) !== null;