{"record":{"id":"d740ef11ca2f42d2","repo":"siyuan-note/siyuan","slug":"ai-configuration-is-unavailable","errorCode":null,"errorMessage":"AI configuration is unavailable","messagePattern":"AI configuration is unavailable","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/assets.go","lineNumber":523,"sourceCode":"\t}\n\tprepared, err := util.PrepareModelImage(\n\t\tdata, documentImageMaxBytes, documentImageMaxPixels, documentImageMaxEdge,\n\t)\n\tif err != nil {\n\t\treturn PreparedDocumentImage{}, err\n\t}\n\treturn PreparedDocumentImage{\n\t\tArtifact: ImageArtifactRef{Kind: \"image\", Path: relativePath},\n\t\tData:     prepared.Data,\n\t\tMIMEType: prepared.MIMEType,\n\t\tPrepared: prepared,\n\t}, nil\n}\n\n// GenerateImage 使用全局图片生成配置创建图片字节，可供文档资源、编辑器和其他图片入口复用。\nfunc GenerateImage(ctx context.Context, request GenerateImageRequest) (GenerateImageResult, error) {\n\tif Conf == nil || Conf.AI == nil {\n\t\treturn GenerateImageResult{}, errors.New(\"AI configuration is unavailable\")\n\t}\n\tprovider, generationModel := Conf.AI.GetImageGenerationModel()\n\tif err := validateImageModel(provider, generationModel); err != nil {\n\t\treturn GenerateImageResult{}, err\n\t}\n\tprompt := strings.TrimSpace(request.Prompt)\n\tif prompt == \"\" {\n\t\treturn GenerateImageResult{}, errors.New(\"prompt is required for image generation\")\n\t}\n\tsize := multimodalValueOrDefault(request.Size, Conf.AI.ImageGeneration.Size)\n\tquality := multimodalValueOrDefault(request.Quality, Conf.AI.ImageGeneration.Quality)\n\toutputFormat := strings.ToLower(multimodalValueOrDefault(request.OutputFormat, Conf.AI.ImageGeneration.OutputFormat))\n\tif outputFormat != \"png\" && outputFormat != \"jpeg\" && outputFormat != \"webp\" {\n\t\treturn GenerateImageResult{}, errors.New(\"unsupported image output format\")\n\t}\n\tgenerated, err := util.NewOpenAIImageAdapter(\n\t\tprovider.APIKey, provider.BaseURL, generationModel.Name, Conf.AI.ImageGeneration.RequestTimeout, ResolveAIProviderHeaders(provider),\n\t).Generate(ctx, util.GenerateImageRequest{","sourceCodeStart":505,"sourceCodeEnd":541,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/assets.go#L505-L541","documentation":"GenerateImage reads the global configuration singleton Conf and its AI section to find the image-generation provider. If Conf is nil (configuration not yet loaded/initialized) or Conf.AI is nil (AI settings never created), it cannot proceed and returns \"AI configuration is unavailable\".","triggerScenarios":"Calling GenerateImage (directly or via GenerateDocumentImage) before kernel configuration is loaded — e.g. in tests or early startup — or in an environment where the AI config section was never initialized (Conf.AI == nil).","commonSituations":"A unit test calling GenerateImage without model.InitConf; a headless/embedded use of the model package that skipped configuration loading; a corrupted or manually edited conf.json that prevented the AI section from being populated.","solutions":["Ensure the kernel is fully booted (util.Boot / model configuration load) before calling GenerateImage","Initialize configuration in tests: call the conf init helper (model.InitConf or equivalent) before invoking GenerateImage","Open Settings - AI in the app to create/save the AI configuration section, then retry"],"exampleFix":"// before (test)\nresult, err := model.GenerateImage(ctx, req) // Conf == nil\n// after\nmodel.InitConf()\nconf.Conf.AI.ImageGeneration = ... // set provider/model\nresult, err = model.GenerateImage(ctx, req)","handlingStrategy":"validation","validationCode":"if conf.Conf == nil || conf.Conf.AI == nil {\n    return errors.New(\"AI configuration not loaded; boot the kernel or init conf first\")\n}","typeGuard":"func aiConfigReady() bool { return conf.Conf != nil && conf.Conf.AI != nil }","tryCatchPattern":"res, err := model.GenerateImage(ctx, req)\nif err != nil && err.Error() == \"AI configuration is unavailable\" {\n    // initialize/load configuration, then retry once\n}","preventionTips":["Only call model APIs after kernel boot/configuration loading completes","In tests, call the conf init helper in TestMain","Do not hand-edit conf.json; change AI settings through the app"],"tags":["configuration","ai","go"],"backgroundTag":"missing-required-config","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}