|
| 1 | +/** |
| 2 | + * Synthetic Autofix Extension |
| 3 | + * |
| 4 | + * Automatically fixes malformed JSON tool calls using the Synthetic fix-json model. |
| 5 | + * This extension works with the hook-based approach (PR #feature/hook-based-tool-parsing). |
| 6 | + * |
| 7 | + * Features: |
| 8 | + * - Intercepts tool call JSON parsing |
| 9 | + * - Fixes malformed JSON using hf:syntheticlab/fix-json |
| 10 | + * - Provides UI notifications during fixing |
| 11 | + * - Graceful fallback when autofix fails |
| 12 | + * |
| 13 | + * Setup: |
| 14 | + * export SYNTHETIC_API_KEY="syn_..." |
| 15 | + * pi -e ./autofix.ts |
| 16 | + * |
| 17 | + * Configuration (optional): |
| 18 | + * export PI_AUTOFIX_MODEL="hf:syntheticlab/fix-json" # Default |
| 19 | + * export PI_AUTOFIX_ENABLED="true" # Default |
| 20 | + */ |
| 21 | + |
| 22 | +import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent"; |
| 23 | +import type { Model, AssistantMessageEventStream, Context, StreamOptions } from "@mariozechner/pi-ai"; |
| 24 | +import { streamSimple } from "@mariozechner/pi-ai"; |
| 25 | + |
| 26 | +interface AutofixConfig { |
| 27 | + enabled: boolean; |
| 28 | + baseUrl: string; |
| 29 | + model: string; |
| 30 | + apiKey?: string; |
| 31 | + temperature: number; |
| 32 | +} |
| 33 | + |
| 34 | +const DEFAULT_CONFIG: AutofixConfig = { |
| 35 | + enabled: true, |
| 36 | + baseUrl: "https://api.synthetic.new/v1", |
| 37 | + model: "hf:syntheticlab/fix-json", |
| 38 | + temperature: 0, |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * Attempts to fix malformed JSON using the Synthetic fix-json model |
| 43 | + */ |
| 44 | +async function autofixJson( |
| 45 | + brokenJson: string, |
| 46 | + config: AutofixConfig, |
| 47 | + signal?: AbortSignal, |
| 48 | +): Promise<{ success: boolean; fixed?: unknown; error?: string }> { |
| 49 | + if (!config.apiKey) { |
| 50 | + return { success: false, error: "No SYNTHETIC_API_KEY available" }; |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + const response = await fetch(`${config.baseUrl}/chat/completions`, { |
| 55 | + method: "POST", |
| 56 | + headers: { |
| 57 | + "Content-Type": "application/json", |
| 58 | + Authorization: `Bearer ${config.apiKey}`, |
| 59 | + }, |
| 60 | + body: JSON.stringify({ |
| 61 | + model: config.model, |
| 62 | + temperature: config.temperature, |
| 63 | + messages: [ |
| 64 | + { |
| 65 | + role: "user", |
| 66 | + content: `Fix this broken JSON and return ONLY valid JSON, no explanation:\n\n${brokenJson}`, |
| 67 | + }, |
| 68 | + ], |
| 69 | + response_format: { type: "json_object" }, |
| 70 | + }), |
| 71 | + signal, |
| 72 | + }); |
| 73 | + |
| 74 | + if (!response.ok) { |
| 75 | + return { success: false, error: `API error: ${response.status}` }; |
| 76 | + } |
| 77 | + |
| 78 | + const data = await response.json(); |
| 79 | + const content = data.choices?.[0]?.message?.content; |
| 80 | + |
| 81 | + if (!content) { |
| 82 | + return { success: false, error: "Empty response" }; |
| 83 | + } |
| 84 | + |
| 85 | + // Try to parse the response |
| 86 | + try { |
| 87 | + return { success: true, fixed: JSON.parse(content) }; |
| 88 | + } catch { |
| 89 | + // Try to extract JSON from the response |
| 90 | + const jsonMatch = content.match(/\{[\s\S]*\}/); |
| 91 | + if (jsonMatch) { |
| 92 | + try { |
| 93 | + return { success: true, fixed: JSON.parse(jsonMatch[0]) }; |
| 94 | + } catch { |
| 95 | + return { success: false, error: "Could not parse fixed JSON" }; |
| 96 | + } |
| 97 | + } |
| 98 | + return { success: false, error: "No JSON found in response" }; |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + return { |
| 102 | + success: false, |
| 103 | + error: error instanceof Error ? error.message : String(error), |
| 104 | + }; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Creates the onToolCallParse hook function |
| 110 | + */ |
| 111 | +function createToolCallParseHook( |
| 112 | + config: AutofixConfig, |
| 113 | + ui: ExtensionContext["ui"], |
| 114 | +): StreamOptions["onToolCallParse"] { |
| 115 | + return async (rawArgs: string, toolName: string) => { |
| 116 | + // First, try standard parsing |
| 117 | + try { |
| 118 | + return JSON.parse(rawArgs); |
| 119 | + } catch { |
| 120 | + // Parsing failed, try autofix |
| 121 | + ui.notify(`🔧 Fixing malformed JSON for ${toolName}...`, "info"); |
| 122 | + |
| 123 | + const result = await autofixJson(rawArgs, config); |
| 124 | + |
| 125 | + if (result.success) { |
| 126 | + ui.notify(`✅ Fixed JSON for ${toolName}`, "info"); |
| 127 | + return result.fixed; |
| 128 | + } else { |
| 129 | + ui.notify(`⚠️ Could not fix JSON for ${toolName}: ${result.error}`, "warning"); |
| 130 | + // Return empty object as fallback |
| 131 | + return {}; |
| 132 | + } |
| 133 | + } |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Extension factory function |
| 139 | + */ |
| 140 | +export default function syntheticAutofixExtension(pi: ExtensionAPI) { |
| 141 | + // Get config from environment |
| 142 | + const config: AutofixConfig = { |
| 143 | + enabled: process.env.PI_AUTOFIX_ENABLED !== "false", |
| 144 | + baseUrl: process.env.PI_AUTOFIX_BASE_URL || DEFAULT_CONFIG.baseUrl, |
| 145 | + model: process.env.PI_AUTOFIX_MODEL || DEFAULT_CONFIG.model, |
| 146 | + apiKey: process.env.SYNTHETIC_API_KEY, |
| 147 | + temperature: DEFAULT_CONFIG.temperature, |
| 148 | + }; |
| 149 | + |
| 150 | + if (!config.enabled) { |
| 151 | + console.log("[Synthetic Autofix] Disabled via PI_AUTOFIX_ENABLED"); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + if (!config.apiKey) { |
| 156 | + console.log("[Synthetic Autofix] No SYNTHETIC_API_KEY, skipping"); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + console.log("[Synthetic Autofix] Extension loaded"); |
| 161 | + console.log(`[Synthetic Autofix] Fix model: ${config.model}`); |
| 162 | + |
| 163 | + // Hook into session start to wrap providers |
| 164 | + pi.on("session_start", async (_event, ctx) => { |
| 165 | + const currentModel = ctx.model; |
| 166 | + if (!currentModel) { |
| 167 | + console.log("[Synthetic Autofix] No current model"); |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + const providerName = currentModel.provider; |
| 172 | + console.log(`[Synthetic Autofix] Active provider: ${providerName}`); |
| 173 | + |
| 174 | + // Create the parse hook |
| 175 | + const onToolCallParse = createToolCallParseHook(config, ctx.ui); |
| 176 | + |
| 177 | + // Note: To actually use the hook, we would need to wrap the provider's |
| 178 | + // streamSimple function. However, pi's extension API doesn't currently |
| 179 | + // expose a way to wrap the stream function. |
| 180 | + // |
| 181 | + // For now, this extension demonstrates the concept and will work once |
| 182 | + // the hook-based PR is merged and extensions can provide stream wrappers. |
| 183 | + // |
| 184 | + // The direct approach (PR #feature/autofix-malformed-tool-calls) works |
| 185 | + // immediately without requiring extension hooks. |
| 186 | + |
| 187 | + ctx.ui.notify("🔧 Synthetic Autofix ready", "info"); |
| 188 | + }); |
| 189 | + |
| 190 | + // Register a command to test autofix |
| 191 | + pi.registerCommand("test-autofix", { |
| 192 | + description: "Test the autofix functionality with sample malformed JSON", |
| 193 | + handler: async (_args, ctx) => { |
| 194 | + if (!config.apiKey) { |
| 195 | + ctx.ui.notify("No SYNTHETIC_API_KEY set", "error"); |
| 196 | + return; |
| 197 | + } |
| 198 | + |
| 199 | + ctx.ui.notify("Testing autofix...", "info"); |
| 200 | + |
| 201 | + // Test cases |
| 202 | + const testCases = [ |
| 203 | + '{"command": "ls -la", "timeout" 30000}', // Missing colon |
| 204 | + '{"command": "ls -la", "timeout": }', // Missing value |
| 205 | + '{"command": "ls -la", "timeout": 30000', // Missing closing brace |
| 206 | + ]; |
| 207 | + |
| 208 | + for (const testCase of testCases) { |
| 209 | + console.log("\n--- Test Case ---"); |
| 210 | + console.log("Input:", testCase); |
| 211 | + |
| 212 | + const result = await autofixJson(testCase, config); |
| 213 | + |
| 214 | + if (result.success) { |
| 215 | + console.log("✅ Fixed:", JSON.stringify(result.fixed)); |
| 216 | + } else { |
| 217 | + console.log("❌ Error:", result.error); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + ctx.ui.notify("Autofix test complete (see logs)", "info"); |
| 222 | + }, |
| 223 | + }); |
| 224 | +} |
0 commit comments