feat: AI models admin, settings import, Anthropic adapter and the rest of AIService

- golem15_golem_models (encrypted write-only api_key) with its cabana list
  and form under golem15.golem.access_settings, and the Settings lookups
  (Models, DefaultModel with PHP's position-0 quirk, Vision, Image, File,
  ByName), every one Trusted
- golem:import-settings copies the WinterCMS golem_settings repeater once
- AnthropicAdapter; SendStream, SendFile/SendFilePath/SendFileReader,
  SendWithModel, SendToImageModel, SendToVisionModel, GenerateImage, Ask
- PromptFactory; README documents the config keys, the environment
  variable, the command and the admin screen
This commit is contained in:
Jakub Zych
2026-10-03 23:09:02 +02:00
parent da9fcde725
commit c93e2d9142
27 changed files with 2760 additions and 14 deletions

62
models/ai_model.go Normal file
View File

@@ -0,0 +1,62 @@
package models
import (
"time"
"git.golem15.com/golem15/summercms/modules/lagoon"
)
// AiModel is one AI model an operator configured in the admin
// (golem15_golem_models), the Go form of one item of the WinterCMS
// plugin's models repeater. APIKey is encrypted at rest, never serialized
// (json:"-", Hidden) and never echoed by the admin form. BaseURL nil or
// empty means the adapter's default. SortOrder keeps the repeater's order.
type AiModel struct {
ID uint `gorm:"column:id;primaryKey"`
Name string `gorm:"column:name"`
Adapter string `gorm:"column:adapter"`
APIKey lagoon.Encrypted `gorm:"column:api_key" json:"-"`
BaseURL *string `gorm:"column:base_url"`
Model string `gorm:"column:model"`
SystemPrompt *string `gorm:"column:system_prompt"`
IsEnabled bool `gorm:"column:is_enabled"`
IsDefault bool `gorm:"column:is_default"`
GeneratesImages bool `gorm:"column:generates_images"`
AcceptsImages bool `gorm:"column:accepts_images"`
HasFilesEndpoint bool `gorm:"column:has_files_endpoint"`
MaxCompletionTokens *int `gorm:"column:max_completion_tokens"`
SortOrder int `gorm:"column:sort_order"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
}
// TableName is golem15_golem_models.
func (AiModel) TableName() string { return "golem15_golem_models" }
// Fillable is every column the admin form writes.
func (AiModel) Fillable() []string {
return []string{
"name", "adapter", "api_key", "base_url", "model", "system_prompt",
"is_enabled", "is_default", "generates_images", "accepts_images",
"has_files_endpoint", "max_completion_tokens", "sort_order",
}
}
// Hidden keeps the API key out of every serialization.
func (AiModel) Hidden() []string { return []string{"api_key"} }
// Rules are the form's checks: a name, a model id, a known adapter and an
// integer token budget.
func (AiModel) Rules() map[string]string {
return map[string]string{
"name": "required",
"model": "required",
"adapter": "required|in:openai,anthropic",
"max_completion_tokens": "nullable|integer|min:1",
"sort_order": "nullable|integer",
}
}
func init() {
Register(AiModel{})
}

View File

@@ -0,0 +1,21 @@
columns:
name:
label: golem15.golem::lang.settings.model_name
searchable: true
adapter:
label: golem15.golem::lang.settings.adapter
model:
label: golem15.golem::lang.settings.model_id
searchable: true
is_enabled:
label: golem15.golem::lang.settings.is_enabled
type: switch
is_default:
label: golem15.golem::lang.settings.is_default
type: switch
accepts_images:
label: golem15.golem::lang.settings.accepts_images
type: switch
sort_order:
label: golem15.golem::lang.settings.sort_order
sortable: true

View File

@@ -0,0 +1,72 @@
fields:
name:
label: golem15.golem::lang.settings.model_name
type: text
span: left
required: true
adapter:
label: golem15.golem::lang.settings.adapter
type: dropdown
span: right
default: openai
comment: golem15.golem::lang.settings.adapter_comment
options:
openai: OpenAI (compatible)
anthropic: Anthropic
api_key:
label: golem15.golem::lang.settings.api_key
type: text
span: right
comment: golem15.golem::lang.settings.api_key_comment
attributes:
type: password
autocomplete: off
base_url:
label: golem15.golem::lang.settings.base_url
type: text
span: left
default: https://api.openai.com/v1
comment: golem15.golem::lang.settings.base_url_comment
model:
label: golem15.golem::lang.settings.model_id
type: text
span: right
required: true
comment: golem15.golem::lang.settings.model_id_comment
system_prompt:
label: golem15.golem::lang.settings.system_prompt
type: textarea
span: full
size: small
comment: golem15.golem::lang.settings.system_prompt_comment
is_enabled:
label: golem15.golem::lang.settings.is_enabled
type: switch
span: right
is_default:
label: golem15.golem::lang.settings.is_default
type: switch
span: left
generates_images:
label: golem15.golem::lang.settings.generates_images
type: switch
span: auto
accepts_images:
label: golem15.golem::lang.settings.accepts_images
type: switch
span: auto
has_files_endpoint:
label: golem15.golem::lang.settings.has_files_endpoint
type: switch
span: auto
max_completion_tokens:
label: golem15.golem::lang.settings.max_completion_tokens
type: number
span: left
default: 4096
comment: golem15.golem::lang.settings.max_completion_tokens_comment
sort_order:
label: golem15.golem::lang.settings.sort_order
type: number
span: right
default: 0

16
models/registry.go Normal file
View File

@@ -0,0 +1,16 @@
// Package models holds the golem15.golem GORM models. It is a leaf
// package: it imports no other package of the plugin.
package models
var all []any
// Register appends GORM models, so a model file adds itself without
// editing plugin.go.
func Register(models ...any) {
all = append(all, models...)
}
// All returns every model registered by this package.
func All() []any {
return all
}