Files
Lukas Hirt 14ca73fa83 feat: [OCISDEV-1061] add llm proxy service
Add a new llm service that proxies chat-completion requests from AI web extensions to a configured OpenAI-compatible endpoint, replacing the standalone Node.js sidecar. It is part of the oCIS single binary, opt-in via OCIS_ADD_RUN_SERVICES=llm, and exposed through the proxy at POST /graph/v1beta1/extensions/org.libregraph/llm/chat/completions.

Authentication is delegated to oCIS. Requests are rate-limited per user with a sliding window backed by the shared store, and the request body is sanitized before forwarding. Streaming requests are rejected.

Signed-off-by: Lukas Hirt <info@hirt.cz>
2026-10-15 14:31:07 +02:00

62 lines
1.9 KiB
Go

package parser
import (
"errors"
"fmt"
"net/url"
ociscfg "github.com/owncloud/ocis/v2/ocis-pkg/config"
"github.com/owncloud/ocis/v2/ocis-pkg/config/envdecode"
"github.com/owncloud/ocis/v2/services/llm/pkg/config"
"github.com/owncloud/ocis/v2/services/llm/pkg/config/defaults"
)
// ParseConfig loads configuration from known paths.
func ParseConfig(cfg *config.Config) error {
err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg)
if err != nil {
return err
}
defaults.EnsureDefaults(cfg)
// load all env variables relevant to the config in the current context.
if err := envdecode.Decode(cfg); err != nil {
// no environment variable set for this config is an expected "error"
if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) {
return err
}
}
defaults.Sanitize(cfg)
return Validate(cfg)
}
// Validate validates the config.
func Validate(cfg *config.Config) error {
if cfg.LLM.Endpoint == "" {
return errors.New("llm: LLM_ENDPOINT is required")
}
u, err := url.Parse(cfg.LLM.Endpoint)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
return fmt.Errorf("llm: LLM_ENDPOINT must be an http(s) URL, got %q", cfg.LLM.Endpoint)
}
if cfg.LLM.MaxTokensLimit <= 0 {
return errors.New("llm: LLM_MAX_TOKENS_LIMIT must be greater than 0")
}
if cfg.LLM.Timeout < 0 {
return errors.New("llm: LLM_TIMEOUT must not be negative")
}
if cfg.RateLimit.Window < 0 || cfg.RateLimit.MaxRequests < 0 {
return errors.New("llm: LLM_RATE_LIMIT_WINDOW and LLM_RATE_LIMIT_MAX_REQUESTS must not be negative")
}
// Rate-limit records are expired by the store; a store TTL shorter than
// the window would silently shrink the effective window.
if cfg.RateLimit.Window > 0 && cfg.RateLimit.MaxRequests > 0 &&
cfg.Store.TTL > 0 && cfg.Store.TTL < cfg.RateLimit.Window {
return fmt.Errorf("llm: LLM_STORE_TTL (%s) must not be shorter than LLM_RATE_LIMIT_WINDOW (%s)", cfg.Store.TTL, cfg.RateLimit.Window)
}
return nil
}