UNPKG

openai-code

Version:

An unofficial proxy layer that lets you use Anthropic Claude Code with any OpenAI API backend.

76 lines (67 loc) 2.69 kB
import { tmpdir } from 'node:os'; import { join, isAbsolute } from 'node:path' import { writeFileSync, accessSync, constants, lstatSync, readdirSync, readFileSync } from 'node:fs' import { nonEmbeddableFileEndings, skipDirectoryNames, isEmbeddableTextFile } from "./ignore.mjs" export const ensureAbsolutePath = (root, path) => isAbsolute(path) ? path : join(root, path) export const safeReadFileSync = (...opts) => { try { return readFileSync(...opts) } catch (error) { return null } } export function storeTempFile(uniqueId, content) { const tempFilePath = join(tmpdir(), `${uniqueId}.tmp`); writeFileSync(tempFilePath, content, { flag: 'w' }); // 'w' flag ensures the file is overwritten if it exists return uniqueId; } export function readTempFile(uniqueId) { const filePath = join(tmpdir(), `${uniqueId}.tmp`); try { return readFileSync(filePath, 'utf-8'); } catch (error) { console.error(`Error reading file with ID ${uniqueId}:`, error); return null; } } // get a directory listing of all files in workingDirectory export const getAllSourceFilePaths = (workingDirectory) => { const entries = readdirSync(workingDirectory, { withFileTypes: true }); const filePaths = []; for (const entry of entries) { const fullPath = join(workingDirectory, entry.name); if (entry.isDirectory()) { // skip directories that start with a skipped name if (!skipDirectoryNames.some(prefix => entry.name.startsWith(prefix))) { // recursively get file paths from subdirectories filePaths.push(...getAllSourceFilePaths(fullPath, skipDirectoryNames)); } } else { // check for non-embeddable file endings by checking the file extension if ending starts with a dot, otherwise full match const hasNonEmbeddableEnding = nonEmbeddableFileEndings.some(ending => { if (ending.startsWith('.')) { const dotIndex = entry.name.lastIndexOf('.'); if (dotIndex === -1) return false; const ext = entry.name.slice(dotIndex); return ext.startsWith(ending); } return entry.name === ending; }); if (!hasNonEmbeddableEnding) { try { // check if the file is accessible, is not a directory, and is embeddable if ( accessSync(fullPath, constants.R_OK) === undefined && !lstatSync(fullPath).isDirectory() && isEmbeddableTextFile(fullPath) ) { filePaths.push(fullPath); } } catch (error) { console.warn(`File ${fullPath} is not accessible, is a directory, or not embeddable:`, error); } } } } return filePaths; }