GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — t3code/scripts/mobile-behavior/fake-fx.ts at main · VitaCodez/t3code Expand file tree/ Copy pathfake-fx.tsMore file actions 262 lines (246 loc) · 9.56 KB / Copy pathfake-fx.tsFile metadata and controls262 lines (246 loc) · 9.56 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 // @effect-diagnostics nodeBuiltinImport:off globalTimers:off globalJsonInEffect:off globalConsole:off - a tiny stdio ACP agent spawned by the server; plain Node keeps it dependency-free. /** * A scripted stand-in for the `fx` CLI, registered as an Fx provider instance so the * server streams deterministic replies with no model. A `[[eval:<script>]]` marker in * the prompt picks the reply; see `scriptOf`. `hold` scripts wait for a release file * named after the marker's key in `T3_EVAL_FX_CONTROL_DIR`. */ import * as NodeFS from "node:fs"; import * as NodePath from "node:path"; import * as NodeReadline from "node:readline"; const MODEL = "eval/scripted"; type Json = null | boolean | number | string | Json[] | { [key: string]: Json }; interface Message { readonly id?: number | string; readonly method?: string; readonly params?: { [key: string]: Json }; readonly result?: Json; readonly error?: Json; } const controlDir = process.env.T3_EVAL_FX_CONTROL_DIR ?? ""; const send = (message: object) => process.stdout.write(`${JSON.stringify(message)}\n`); const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)); const update = (sessionId: string, value: object) => send({ jsonrpc: "2.0", method: "session/update", params: { sessionId, update: value } }); const select = (id: string, category: string, currentValue: string, values: string[]) => ({ id, name: id, category, type: "select", currentValue, options: values.map((value) => ({ value, name: value })), }); // The Fx adapter needs a mode option and the modes list; it sets mode and effort per session. const configOptions = [ select("model", "model", MODEL, [MODEL]), select("mode", "mode", "full-access", ["auto", "ask", "full-access"]), select("effort", "thought_level", "auto", ["auto", "low", "medium", "high"]), ]; const modes = { currentModeId: "full-access", availableModes: ["auto", "ask", "full-access"].map((id) => ({ id, name: id })), }; let nextOutgoingId = 1_000_000; const pendingReplies = new Map<number, (message: Message) => void>(); const cancelled = new Set<string>(); let sessionCount = 0; function request(method: string, params: object): Promise<Message> { const id = nextOutgoingId++; send({ jsonrpc: "2.0", id, method, params }); return new Promise((resolve) => pendingReplies.set(id, resolve)); } /** Paragraphs of plain filler with a heading every fifth paragraph, so the feed lays out like a real reply. */ export function fillerText(words: number, seed = 0): string { const vocabulary = "the agent reads the file then edits the module and runs the focused test before it writes a short summary of what changed and why the change is safe".split( " ", ); const out: string[] = []; let paragraph: string[] = []; for (let index = 0; index < words; index++) { paragraph.push(vocabulary[(index * 7 + seed) % vocabulary.length]!); if (paragraph.length === 60 || index === words - 1) { const count = out.length; out.push(`${count % 5 === 0 ? `## Part ${count / 5 + 1}\n\n` : ""}${paragraph.join(" ")}.`); paragraph = []; } } return out.join("\n\n"); } interface Script { readonly kind: "reply" | "stream" | "hold" | "question" | "subagents" | "fail"; readonly words: number; readonly chunkMs: number; readonly key: string; } /** `[[eval:stream:3000:40]]` streams 3000 words in 40 ms chunks; `[[eval:hold:k]]` replies when `k` is released. */ export function scriptOf(prompt: string): Script { const marker = /\[\[eval:([a-z]+)(?::([^\]:]+))?(?::([^\]:]+))?\]\]/.exec(prompt); const kind = marker?.[1]; if (kind === "stream") return { kind, words: Number(marker?.[2] ?? 3000), chunkMs: Number(marker?.[3] ?? 40), key: "", }; if (kind === "hold" || kind === "subagents" || kind === "question") return { kind, words: 40, chunkMs: 0, key: marker?.[2] ?? kind }; if (kind === "fail") return { kind, words: 0, chunkMs: 0, key: "" }; return { kind: "reply", words: 40, chunkMs: 0, key: "" }; } async function waitForRelease(sessionId: string, key: string) { const file = NodePath.join(controlDir, `release-${key}`); while (!cancelled.has(sessionId) && !NodeFS.existsSync(file)) await sleep(100); } async function streamText(sessionId: string, text: string, chunkMs: number, messageId: string) { const tokens = text.split(/(?<= )/); for (let index = 0; index < tokens.length; index += 4) { if (cancelled.has(sessionId)) return; update(sessionId, { sessionUpdate: "agent_message_chunk", messageId, content: { type: "text", text: tokens.slice(index, index + 4).join("") }, }); if (chunkMs > 0) await sleep(chunkMs); } } async function runPrompt(sessionId: string, prompt: string, turn: number): Promise<object> { const script = scriptOf(prompt); const messageId = `${sessionId}-m${turn}`; if (script.kind === "fail") throw new Error("Scripted provider failure."); if (script.kind === "hold") await waitForRelease(sessionId, script.key); if (script.kind === "question") { const reply = await request("elicitation/create", { sessionId, mode: "form", message: "Which option should I use?", requestedSchema: { type: "object", properties: { choice: { type: "string", title: "Option", description: "Which option should I use?", enum: ["First", "Second"], }, }, required: ["choice"], }, }); if (reply.error) return { stopReason: "cancelled" }; } if (script.kind === "subagents") { for (const name of ["one", "two"]) { const toolCallId = `${sessionId}-sub-${name}-${turn}`; update(sessionId, { sessionUpdate: "tool_call", toolCallId, name: "subagent", title: `Subagent · read ${name}`, kind: "other", status: "pending", rawInput: { request: { action: "run", task: `Read file ${name}` } }, }); update(sessionId, { sessionUpdate: "tool_call_update", toolCallId, status: "in_progress" }); } await waitForRelease(sessionId, script.key); for (const name of ["one", "two"]) update(sessionId, { sessionUpdate: "tool_call_update", toolCallId: `${sessionId}-sub-${name}-${turn}`, status: "completed", content: [ { type: "content", // A long reply gives the child thread something to scroll. content: { type: "text", text: JSON.stringify({ ok: true, result: fillerText(1500, turn) }), }, }, ], }); } if (cancelled.has(sessionId)) return { stopReason: "cancelled" }; await streamText(sessionId, fillerText(script.words, turn), script.chunkMs, messageId); return { stopReason: cancelled.has(sessionId) ? "cancelled" : "end_turn" }; } const turns = new Map<string, number>(); async function handle(message: Message) { if (message.method === undefined) { if (typeof message.id === "number") pendingReplies.get(message.id)?.(message); return; } const reply = (result: object) => send({ jsonrpc: "2.0", id: message.id, result }); const params = message.params ?? {}; const sessionId = typeof params.sessionId === "string" ? params.sessionId : ""; switch (message.method) { case "initialize": return reply({ protocolVersion: 1, agentCapabilities: { loadSession: true, promptCapabilities: { image: true, audio: false, embeddedContext: true }, mcpCapabilities: { http: true, sse: true, acp: true }, sessionCapabilities: { list: {}, resume: {}, close: {}, systemPrompt: {} }, }, agentInfo: { name: "fx", title: "fx", version: "0.0.14" }, authMethods: [], }); case "session/new": { sessionCount += 1; const sessionId = `eval-session-${process.pid}-${sessionCount}`; return reply({ sessionId, configOptions, modes }); } case "session/load": case "session/resume": return reply({ configOptions, modes }); case "session/set_config_option": return reply({ configOptions }); case "session/cancel": cancelled.add(sessionId); return; case "session/prompt": { cancelled.delete(sessionId); const turn = (turns.get(sessionId) ?? 0) + 1; turns.set(sessionId, turn); const blocks = Array.isArray(params.prompt) ? params.prompt : []; const text = blocks .map((block) => block && typeof block === "object" && !Array.isArray(block) ? block.text : "", ) .filter((value) => typeof value === "string") .join("\n"); try { return reply(await runPrompt(sessionId, text, turn)); } catch (error) { return send({ jsonrpc: "2.0", id: message.id, error: { code: -32603, message: error instanceof Error ? error.message : String(error) }, }); } } default: if (message.id !== undefined) return reply({}); } } function main(args: ReadonlyArray<string>) { const probes: Record<string, string> = { "--version": "fx 0.0.14", status: JSON.stringify({ auth: "eval", model: MODEL }), models: JSON.stringify({ ids: [MODEL] }), }; const probe = probes[args[0] ?? ""]; if (probe !== undefined) return console.log(probe); if (args[0] !== "acp") { console.error(`fake-fx: unsupported command ${args.join(" ")}`); process.exitCode = 2; return; } NodeReadline.createInterface({ input: process.stdin }).on("line", (line) => { if (line.trim()) void handle(JSON.parse(line) as Message); }); } if (import.meta.main) main(process.argv.slice(2)); |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |