From 02cc0e3ac6c08f422f11b668c729102fcf0c7f38 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 16 Aug 2025 15:58:40 +0300 Subject: logging: add -log-preview-limit flag with HEXAI_LOG_PREVIEW_LIMIT env; wire through llm.SetLogPreviewLimit; document in README --- internal/llm/provider.go | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'internal/llm/provider.go') diff --git a/internal/llm/provider.go b/internal/llm/provider.go index fd9d4d3..a80b1c1 100644 --- a/internal/llm/provider.go +++ b/internal/llm/provider.go @@ -1,10 +1,10 @@ package llm import ( - "context" - "errors" - "log" - "os" + "context" + "errors" + "log" + "os" ) // Message represents a chat-style prompt message. @@ -16,8 +16,8 @@ type Message struct { // Client is a minimal LLM provider interface. // Future providers (Ollama, etc.) should implement this. type Client interface { - // Chat sends chat messages and returns the assistant text. - Chat(ctx context.Context, messages []Message, opts ...RequestOption) (string, error) + // Chat sends chat messages and returns the assistant text. + Chat(ctx context.Context, messages []Message, opts ...RequestOption) (string, error) } // Options for a request. Providers may ignore unsupported fields. @@ -41,9 +41,23 @@ func WithStop(stop ...string) RequestOption { // NewDefault returns the default provider using environment configuration. // Currently this is the OpenAI provider using OPENAI_API_KEY. func NewDefault(logger *log.Logger) (Client, error) { - apiKey := os.Getenv("OPENAI_API_KEY") - if apiKey == "" { - return nil, errors.New("OPENAI_API_KEY is not set") - } - return newOpenAIFromEnv(apiKey, logger), nil + apiKey := os.Getenv("OPENAI_API_KEY") + if apiKey == "" { + return nil, errors.New("OPENAI_API_KEY is not set") + } + return newOpenAIFromEnv(apiKey, logger), nil +} + +// Logging configuration for previews +var logPreviewLimit int // 0 means unlimited + +// SetLogPreviewLimit sets the maximum number of characters to log for +// request/response previews. Set to 0 for unlimited. +func SetLogPreviewLimit(n int) { logPreviewLimit = n } + +func previewForLog(s string) string { + if logPreviewLimit > 0 { + return trimPreview(s, logPreviewLimit) + } + return s } -- cgit v1.2.3