{"record":{"id":"aac621c24781e15e","repo":"can1357/oh-my-pi","slug":"missing-modelregistry-for-xai-image-generation","errorCode":null,"errorMessage":"Missing modelRegistry for xAI image generation","messagePattern":"Missing modelRegistry for xAI image generation","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/tools/image-gen.ts","lineNumber":1474,"sourceCode":"\t\t\t\t\t\tconst imagePaths = await saveImagesToTemp(parsed.images);\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: buildResponseSummary(provider, model, imagePaths, responseText) }],\n\t\t\t\t\t\t\tdetails: {\n\t\t\t\t\t\t\t\tprovider,\n\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\timageCount: parsed.images.length,\n\t\t\t\t\t\t\t\timagePaths,\n\t\t\t\t\t\t\t\timages: parsed.images,\n\t\t\t\t\t\t\t\tresponseText,\n\t\t\t\t\t\t\t\tusage: parsed.usage,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tif (provider === \"xai\") {\n\t\t\t\t\t\tif (!ctx.modelRegistry) {\n\t\t\t\t\t\t\tthrow new Error(\"Missing modelRegistry for xAI image generation\");\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst xaiCreds = await resolveXAIHttpCredentials(ctx.modelRegistry, resolvedModel);\n\t\t\t\t\t\tif (!xaiCreds) {\n\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t\"No xAI credentials. Run /login → xAI Grok OAuth (SuperGrok or X Premium+) or set XAI_API_KEY.\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst prompt = assemblePrompt(params);\n\t\t\t\t\t\tconst aspectRatio = params.aspect_ratio ?? \"1:1\";\n\t\t\t\t\t\tconst xaiResolution = resolveXAIResolution(params.image_size);\n\n\t\t\t\t\t\tconst isEdit = resolvedImages.length > 0;\n\t\t\t\t\t\tif (isEdit && resolvedImages.length > XAI_MAX_EDIT_IMAGES) {\n\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t`xAI image edits accept up to ${XAI_MAX_EDIT_IMAGES} reference images; got ${resolvedImages.length}.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}","sourceCodeStart":1456,"sourceCodeEnd":1492,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/tools/image-gen.ts#L1456-L1492","documentation":"The image-gen tool's xAI branch requires access to the session's model registry to resolve xAI credentials (OAuth or API key). ctx.modelRegistry is optional on the tool context; when absent the tool cannot call resolveXAIHttpCredentials and throws immediately. This is an internal wiring invariant violation, not a user input problem.","triggerScenarios":"Invoking the image generation tool with provider=xai while the ToolContext was constructed without a modelRegistry (e.g. SDK embedding, tests, or a stripped-down harness that omits it), reaching packages/coding-agent/src/tools/image-gen.ts:1474.","commonSituations":"Embedding the coding agent SDK and building a custom tool context; running image generation in a test fixture; a regression where the TUI/RPC path stopped passing modelRegistry into tool context.","solutions":["Pass a modelRegistry when constructing the tool context that runs image-gen (the same registry the session uses for model resolution).","If embedding the SDK, switch to the standard session/tool-context factory so modelRegistry is populated automatically.","As a workaround, use a non-xAI provider (openai, gemini, openrouter) whose branch does not need the registry, though fixing the context is the real fix."],"exampleFix":"// before\nawait ctx.tools.run('image-gen', { prompt }, partialCtx);\n// after\nawait ctx.tools.run('image-gen', { prompt }, { ...partialCtx, modelRegistry });","handlingStrategy":"validation","validationCode":"if (!ctx.modelRegistry) throw new Error('image-gen requires a modelRegistry in the tool context');\nawait runImageGen(ctx, params);","typeGuard":"function hasModelRegistry(ctx: ToolContext): ctx is ToolContext & { modelRegistry: NonNullable<ToolContext['modelRegistry']> } {\n  return ctx.modelRegistry != null;\n}","tryCatchPattern":"try {\n  await imageGen(params);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('Missing modelRegistry')) {\n    // rebuild context with modelRegistry and retry once\n  } else throw err;\n}","preventionTips":["Always build tool contexts via the shared session factory rather than hand-rolled literals","Add a startup assertion that embedded harnesses supply modelRegistry","Cover the image-gen tool with a test using the real context factory"],"tags":["configuration","image-generation","xai","missing-context"],"backgroundTag":"missing-required-context","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}