| 1 | package slides |
| 2 | |
| 3 | // config_ai.go is the editor assistant's configuration: memoria's |
| 4 | // posture (oficina/memoria config.go), adopted. |
| 5 | // |
| 6 | // - SLIDES_AI is "on" or unset. Anything else refuses boot: an owner |
| 7 | // who meant to switch it on and typed "On", "yes" or "true" should |
| 8 | // find out at deploy, not by wondering why the button is missing. |
| 9 | // - SLIDES_TENSORX_API_KEY is required when it is on, and a key alone |
| 10 | // does NOT turn it on. Presence of a credential is not consent. |
| 11 | // - SLIDES_AI_MODELS is the comma-separated model ids the editor |
| 12 | // offers, first one preselected; unset means assist.DefaultModel. |
| 13 | // Configurable rather than hard-coded because which ids TensorX |
| 14 | // serves (a GLM flash tier, a newer DeepSeek) changes faster than |
| 15 | // a release. |
| 16 | // - SLIDES_AI_DEV_BASE_URL points the client at another |
| 17 | // OpenAI-compatible server — a fake one, for a local run. Honoured |
| 18 | // in development only; set anywhere else it refuses boot, because a |
| 19 | // configurable base URL in production is a way to put a second |
| 20 | // party on the path of every deck (memoria decision 13). |
| 21 | // |
| 22 | // With AI off nothing is mounted: no route, no button, no strings. |
| 23 | |
| 24 | import ( |
| 25 | "errors" |
| 26 | "fmt" |
| 27 | "strings" |
| 28 | |
| 29 | "amadan.net/oficina/slides/internal/slides/assist" |
| 30 | ) |
| 31 | |
| 32 | // AIConfig is the assistant's part of Config. |
| 33 | type AIConfig struct { |
| 34 | On bool |
| 35 | Key string |
| 36 | Models []string |
| 37 | DevBaseURL string |
| 38 | } |
| 39 | |
| 40 | // maxModelID bounds one configured model id; a real one is a few dozen |
| 41 | // characters. |
| 42 | const maxModelID = 128 |
| 43 | |
| 44 | // AIConfigFromEnv reads the four SLIDES_AI* variables through getenv |
| 45 | // (os.Getenv in main). A typo is an error, never a silent off. |
| 46 | func AIConfigFromEnv(getenv func(string) string) (AIConfig, error) { |
| 47 | var c AIConfig |
| 48 | c.DevBaseURL = strings.TrimSpace(getenv("SLIDES_AI_DEV_BASE_URL")) |
| 49 | switch v := getenv("SLIDES_AI"); v { |
| 50 | case "": |
| 51 | return c, nil |
| 52 | case "on": |
| 53 | c.On = true |
| 54 | default: |
| 55 | return c, fmt.Errorf("slides: SLIDES_AI must be \"on\" or unset; refusing to guess what %q means", v) |
| 56 | } |
| 57 | c.Key = getenv("SLIDES_TENSORX_API_KEY") |
| 58 | for _, m := range strings.Split(getenv("SLIDES_AI_MODELS"), ",") { |
| 59 | if m = strings.TrimSpace(m); m != "" { |
| 60 | c.Models = append(c.Models, m) |
| 61 | } |
| 62 | } |
| 63 | return c, nil |
| 64 | } |
| 65 | |
| 66 | var errAIConfig = errors.New("slides: assistant configuration") |
| 67 | |
| 68 | // resolveAI applies AIConfig's rules to what a caller supplied, for |
| 69 | // main and the test harness alike: the key required when on, the model |
| 70 | // ids checked and deduplicated, the default model filled in, and the |
| 71 | // development override refused outside development. |
| 72 | func resolveAI(cfg Config) (AIConfig, error) { |
| 73 | ai := cfg.AI |
| 74 | if ai.DevBaseURL != "" && !cfg.development() { |
| 75 | return AIConfig{}, fmt.Errorf("%w: SLIDES_AI_DEV_BASE_URL is honoured only in development", errAIConfig) |
| 76 | } |
| 77 | if !ai.On { |
| 78 | return AIConfig{}, nil |
| 79 | } |
| 80 | if ai.Key == "" { |
| 81 | return AIConfig{}, fmt.Errorf("%w: SLIDES_AI=on names a provider but SLIDES_TENSORX_API_KEY gives no credential for it", errAIConfig) |
| 82 | } |
| 83 | seen := map[string]bool{} |
| 84 | var models []string |
| 85 | for _, m := range ai.Models { |
| 86 | if !validModelID(m) { |
| 87 | return AIConfig{}, fmt.Errorf("%w: SLIDES_AI_MODELS: %q is not a model id", errAIConfig, m) |
| 88 | } |
| 89 | if !seen[m] { |
| 90 | seen[m] = true |
| 91 | models = append(models, m) |
| 92 | } |
| 93 | } |
| 94 | if len(models) == 0 { |
| 95 | models = []string{assist.DefaultModel} |
| 96 | } |
| 97 | ai.Models = models |
| 98 | return ai, nil |
| 99 | } |
| 100 | |
| 101 | // validModelID is the shape of an OpenAI-compatible model id: letters, |
| 102 | // digits and . _ - / : — "deepseek/deepseek-v4-flash-0731", "zai/glm-5.3-flash". |
| 103 | func validModelID(m string) bool { |
| 104 | if m == "" || len(m) > maxModelID { |
| 105 | return false |
| 106 | } |
| 107 | for _, r := range m { |
| 108 | switch { |
| 109 | case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9': |
| 110 | case r == '.', r == '_', r == '-', r == '/', r == ':': |
| 111 | default: |
| 112 | return false |
| 113 | } |
| 114 | } |
| 115 | return true |
| 116 | } |
| 117 | |
| 118 | // newAssistClient is the provider client the resolved config asks for: |
| 119 | // TensorX, or the development base. |
| 120 | func newAssistClient(ai AIConfig) (*assist.Client, error) { |
| 121 | if ai.DevBaseURL != "" { |
| 122 | return assist.NewAt(ai.Key, ai.DevBaseURL) |
| 123 | } |
| 124 | return assist.New(ai.Key) |
| 125 | } |
| 126 | |