The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // Grok's model and effort wire contract (grok 1.0.41): per-model efforts on `models`, a |
| 2 | // `thought_level` option `reasoning_effort`, and `config_option_update` after set_model. |
| 3 | // T3_GROK_MOCK_PROTOCOL picks the protocol (default 1); T3_GROK_MOCK_REJECT_EFFORT names |
| 4 | // an advertised effort Grok refuses. |
| 5 | import * as NodeFS from "node:fs"; |
| 6 | import * as NodeReadline from "node:readline"; |
| 7 | |
| 8 | const sessionId = "grok-model-selection-session"; |
| 9 | const protocolVersion = Number(process.env.T3_GROK_MOCK_PROTOCOL ?? "1"); |
| 10 | const rejectedEffort = process.env.T3_GROK_MOCK_REJECT_EFFORT; |
| 11 | const models = { |
| 12 | "grok-4.6": { efforts: ["xhigh", "high", "medium", "low"], defaultEffort: "xhigh" }, |
| 13 | "grok-4.6-fast": { efforts: ["high", "low"], defaultEffort: "high" }, |
| 14 | }; |
| 15 | let currentModelId = "grok-4.6"; |
| 16 | let currentEffort = models[currentModelId].defaultEffort; |
| 17 | const write = (message) => process.stdout.write(`${JSON.stringify(message)}\n`); |
| 18 | |
| 19 | const configOptions = () => [ |
| 20 | { |
| 21 | id: "model", |
| 22 | name: "Model", |
| 23 | category: "model", |
| 24 | type: "select", |
| 25 | currentValue: currentModelId, |
| 26 | options: Object.keys(models).map((value) => ({ value, name: value })), |
| 27 | }, |
| 28 | { |
| 29 | id: "reasoning_effort", |
| 30 | name: "Reasoning Effort", |
| 31 | category: "thought_level", |
| 32 | type: "select", |
| 33 | currentValue: currentEffort, |
| 34 | options: models[currentModelId].efforts.map((value) => ({ value, name: value })), |
| 35 | }, |
| 36 | ]; |
| 37 | |
| 38 | // Returns an error message when Grok would refuse the switch. |
| 39 | const switchTo = (modelId, effort) => { |
| 40 | const model = models[modelId]; |
| 41 | if (model === undefined) return "Unknown Grok model"; |
| 42 | if (effort !== undefined && (effort === rejectedEffort || !model.efforts.includes(effort))) { |
| 43 | return `Unsupported reasoning effort ${effort}`; |
| 44 | } |
| 45 | currentEffort = effort ?? (modelId === currentModelId ? currentEffort : model.defaultEffort); |
| 46 | currentModelId = modelId; |
| 47 | return undefined; |
| 48 | }; |
| 49 | |
| 50 | for await (const line of NodeReadline.createInterface({ input: process.stdin })) { |
| 51 | const request = JSON.parse(line); |
| 52 | if (process.env.T3_ACP_REQUEST_LOG_PATH) { |
| 53 | NodeFS.appendFileSync(process.env.T3_ACP_REQUEST_LOG_PATH, `${line}\n`); |
| 54 | } |
| 55 | if (request.id === undefined) continue; |
| 56 | const fail = (message) => |
| 57 | write({ jsonrpc: "2.0", id: request.id, error: { code: -32602, message } }); |
| 58 | let result; |
| 59 | switch (request.method) { |
| 60 | case "initialize": |
| 61 | result = { protocolVersion, agentInfo: { name: "grok", version: "mock" } }; |
| 62 | break; |
| 63 | case "session/new": |
| 64 | result = { |
| 65 | sessionId, |
| 66 | models: { |
| 67 | currentModelId, |
| 68 | availableModels: Object.entries(models).map(([modelId, model]) => ({ |
| 69 | modelId, |
| 70 | name: modelId, |
| 71 | _meta: { |
| 72 | supportsReasoningEffort: true, |
| 73 | reasoningEffort: modelId === currentModelId ? currentEffort : model.defaultEffort, |
| 74 | reasoningEfforts: model.efforts.map((value) => ({ id: value, value, label: value })), |
| 75 | }, |
| 76 | })), |
| 77 | }, |
| 78 | configOptions: configOptions(), |
| 79 | }; |
| 80 | break; |
| 81 | case "session/set_model": { |
| 82 | const error = switchTo(request.params.modelId, request.params._meta?.reasoningEffort); |
| 83 | if (error !== undefined) { |
| 84 | fail(error); |
| 85 | continue; |
| 86 | } |
| 87 | write({ jsonrpc: "2.0", id: request.id, result: {} }); |
| 88 | write({ |
| 89 | jsonrpc: "2.0", |
| 90 | method: "session/update", |
| 91 | params: { |
| 92 | sessionId, |
| 93 | update: { sessionUpdate: "config_option_update", configOptions: configOptions() }, |
| 94 | }, |
| 95 | }); |
| 96 | continue; |
| 97 | } |
| 98 | case "session/set_config_option": { |
| 99 | const { configId, value } = request.params; |
| 100 | const error = |
| 101 | configId === "model" |
| 102 | ? switchTo(value, undefined) |
| 103 | : configId === "reasoning_effort" |
| 104 | ? switchTo(currentModelId, value) |
| 105 | : `Unknown config option ${configId}`; |
| 106 | if (error !== undefined) { |
| 107 | fail(error); |
| 108 | continue; |
| 109 | } |
| 110 | result = { configOptions: configOptions() }; |
| 111 | break; |
| 112 | } |
| 113 | case "session/prompt": |
| 114 | result = { stopReason: "end_turn" }; |
| 115 | break; |
| 116 | default: |
| 117 | write({ |
| 118 | jsonrpc: "2.0", |
| 119 | id: request.id, |
| 120 | error: { code: -32601, message: "Method not found" }, |
| 121 | }); |
| 122 | continue; |
| 123 | } |
| 124 | write({ jsonrpc: "2.0", id: request.id, result }); |
| 125 | } |