Something went wrong. Try again.
presents omp as codex for use with chatgpt remote
Something went wrong. Try again.
4.0 kB · 110 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import { abortError, id, sleep, throwIfAborted } from "../util.mjs";
export class MockBackendFactory { constructor({ chunkDelayMs = 0 } = {}) { this.chunkDelayMs = chunkDelayMs; }
async listModels() { return [{ id: "mock/omp", model: "mock/omp", provider: "mock", displayName: "Mock OMP", isDefault: true }]; }
async openSession(options = {}) { return new MockSession({ ...options, chunkDelayMs: this.chunkDelayMs }); }
async forkSession(_source, options = {}) { return this.openSession(options); }}
class MockSession { constructor(options) { this.options = options; this.controller = null; this.steered = []; this.injectedItems = []; }
async runTurn(prompt, context) { const ownController = new AbortController(); this.controller = ownController; const signal = context.signal; const onAbort = () => ownController.abort(signal.reason || abortError()); signal?.addEventListener("abort", onAbort, { once: true }); try { const text = String(prompt || ""); if (text.includes("/reason")) { await context.emit({ type: "reasoningDelta", delta: "checking the bridge" }); } if (text.includes("/approval") || text.includes("/tool")) { const toolCallId = id("mock_tool"); const command = "printf 'bridge tool output\\n'"; await context.emit({ type: "toolStart", toolCallId, toolName: "bash", command, cwd: this.options.cwd }); const decision = await context.requestApproval({ kind: "command", toolCallId, command, cwd: this.options.cwd, reason: "Mock backend approval test", }); if (decision === "accept" || decision === "acceptForSession") { await context.emit({ type: "toolDelta", toolCallId, delta: "bridge tool output\n" }); await context.emit({ type: "toolEnd", toolCallId, status: "completed", exitCode: 0 }); } else { await context.emit({ type: "toolEnd", toolCallId, status: "declined", exitCode: null }); } } if (text.includes("/file")) { const toolCallId = id("mock_file"); const file = `${this.options.cwd || process.cwd()}/bridge-test.txt`; const diff = "--- a/bridge-test.txt\n+++ b/bridge-test.txt\n@@ -0,0 +1 @@\n+hello from OMP\n"; await context.emit({ type: "fileStart", toolCallId, changes: [{ path: file, kind: "add", diff }] }); const decision = await context.requestApproval({ kind: "file", toolCallId, changes: [{ path: file, kind: "add", diff }] }); await context.emit({ type: "fileEnd", toolCallId, status: decision.startsWith("accept") ? "completed" : "declined" }); } const response = text.includes("/slow") ? `Mock OMP slow response for: ${text} `.repeat(20) : `Mock OMP response: ${text}`; const words = response.match(/\S+\s*/g) || [response]; for (const word of words) { throwIfAborted(ownController.signal); await sleep(text.includes("/slow") ? Math.max(10, this.options.chunkDelayMs) : this.options.chunkDelayMs, ownController.signal); await context.emit({ type: "textDelta", delta: word }); } await context.emit({ type: "tokenUsage", usage: { input: 120, output: 40, totalTokens: 160 }, contextUsage: { tokens: 160, contextWindow: 1_000_000, percent: 0.016 }, }); return { status: "completed" }; } finally { signal?.removeEventListener("abort", onAbort); if (this.controller === ownController) this.controller = null; } }
async injectItems(items) { this.injectedItems.push(...items); }
async steer(text) { this.steered.push(String(text)); }
async interrupt(reason) { this.controller?.abort(reason || abortError()); }
async compact() { return { compacted: true }; }
async dispose() { await this.interrupt(); }
getPersistentState() { return { kind: "mock" }; }}