UNPKG

openai-code

Version:

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

62 lines (53 loc) 1.91 kB
import { safeReadFileSync } from "./fs.mjs"; import { writeFileSync } from 'node:fs'; import { join } from 'node:path'; // define the default state const defaultState = { nextTask: null, currentGoal: null, agentState: {} }; const stateFileName = 'CLAUDE_STATE.json' /** reads project related state (to bridge state persistence over turns) */ export const readState = (workingDirectory) => { if (!workingDirectory) { return defaultState; } const stateFilePath = join(workingDirectory, stateFileName); try { const data = safeReadFileSync(stateFilePath, 'utf-8'); if (data === null) { return defaultState // state file not found, return default state } return { ...defaultState, ...JSON.parse(data) } } catch (error) { console.error(`State could not be read from: ${stateFilePath}. Using default state: `, defaultState); return defaultState // return default state if file doesn't exist or error occurs } } /** writes project related state (to bridge state persistence over turns) */ export const writeState = (workingDirectory, state) => { const stateFilePath = join(workingDirectory, stateFileName); try { const data = JSON.stringify(state, null, 2); // format JSON with 2 spaces for readability writeFileSync(stateFilePath, data, 'utf-8'); // write the state to the file } catch (error) { console.error(`Error saving state to ${stateFilePath}:`, error); } } export const setState = (workingDirectory, key, value) => { const state = readState(workingDirectory); if (key) { state[key] = value; } writeState(workingDirectory, state); } export const getState = (workingDirectory, key) => { const state = readState(workingDirectory); if (key && typeof state[key] !== "undefined") { return state[key]; } if (key && typeof state[key] !== "undefined") { return defaultState[key] } return null; }