const fs = require('fs');

let runnerCode = fs.readFileSync('src/agent-session-runner.ts', 'utf8');

// Replace the llm logic to match the frontend
const oldLogic = `  const llm = createLLM(config.model, { temperature: 0.2 });
  const oaiTools = toOAITools(config.tools);`;

const newLogic = `  const systemPrompt = resolvePrompt(config.promptId);`;
runnerCode = runnerCode.replace(oldLogic, newLogic);

// Replace the emit logic to remove ingestSessionEvents
const oldEmit = `    await Promise.all([
      patchSession(opts, { outputLine: line }),
      ingestSessionEvents(opts.vibnApiUrl, opts.projectId, opts.sessionId, [
        {
          type: \`output.\${line.type}\`,
          payload: { text: line.text },
          ts: line.ts,
        },
      ]),
    ]);`;
    
const newEmit = `    await patchSession(opts, { outputLine: line });`;
runnerCode = runnerCode.replace(oldEmit, newEmit);

runnerCode = runnerCode.replace(/import \{ createLLM, toOAITools, LLMMessage \} from "\.\/llm";/g, 'import { callVibnChat } from "./llm/vibn-chat-model";\nimport { ChatMessage } from "./llm/gemini-chat";');
runnerCode = runnerCode.replace(/import \{ ingestSessionEvents \} from "\.\/vibn-events-ingest";/g, '');
runnerCode = runnerCode.replace(/const history: LLMMessage\[\]/g, 'const history: ChatMessage[]');

fs.writeFileSync('src/agent-session-runner.ts', runnerCode);
console.log("Patched agent-session-runner.ts");
