summaryrefslogtreecommitdiff
path: root/internal/llm
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-20 18:27:35 +0300
committerPaul Buetow <paul@buetow.org>2026-05-20 18:27:35 +0300
commit83caeb9bcdc4a5eafda937807cd42ea8455948a6 (patch)
treef2258d2d77bfb3f9186bbc457cecdfe17877b891 /internal/llm
parent005c262d09314cd43285af33dc6f1afef0621714 (diff)
Add YouSearch (You.com Research API) provider integration
Amp-Thread-ID: https://ampcode.com/threads/T-019e45ff-4976-750c-b2e6-121d0e5991ef Co-authored-by: Amp <amp@ampcode.com>
Diffstat (limited to 'internal/llm')
-rw-r--r--internal/llm/openai_temp_test.go6
-rw-r--r--internal/llm/provider.go13
-rw-r--r--internal/llm/provider_more_test.go2
-rw-r--r--internal/llm/provider_test.go4
-rw-r--r--internal/llm/yousearch.go163
5 files changed, 179 insertions, 9 deletions
diff --git a/internal/llm/openai_temp_test.go b/internal/llm/openai_temp_test.go
index 07abbd5..0b1b795 100644
--- a/internal/llm/openai_temp_test.go
+++ b/internal/llm/openai_temp_test.go
@@ -5,7 +5,7 @@ import "testing"
func TestNewFromConfig_DefaultTemp_ByModel(t *testing.T) {
// OpenAI, gpt-5.* → default temp 1.0 when not provided
cfg := Config{Provider: "openai", OpenAIModel: "gpt-5.0-preview"}
- c, err := NewFromConfig(cfg, "key", "", "", "")
+ c, err := NewFromConfig(cfg, "key", "", "", "", "")
if err != nil {
t.Fatalf("new: %v", err)
}
@@ -18,7 +18,7 @@ func TestNewFromConfig_DefaultTemp_ByModel(t *testing.T) {
}
// OpenAI, gpt-4.* → default temp 0.2 when not provided
cfg2 := Config{Provider: "openai", OpenAIModel: "gpt-4.1"}
- c2, err := NewFromConfig(cfg2, "key", "", "", "")
+ c2, err := NewFromConfig(cfg2, "key", "", "", "", "")
if err != nil {
t.Fatalf("new2: %v", err)
}
@@ -32,7 +32,7 @@ func TestNewFromConfig_DefaultTemp_UpgradeWhenGpt5AndDefault02(t *testing.T) {
// Simulate app-default of 0.2 while selecting a gpt-5 model: should upgrade to 1.0
v := 0.2
cfg := Config{Provider: "openai", OpenAIModel: "gpt-5.0", OpenAITemperature: &v}
- c, err := NewFromConfig(cfg, "key", "", "", "")
+ c, err := NewFromConfig(cfg, "key", "", "", "", "")
if err != nil {
t.Fatalf("new: %v", err)
}
diff --git a/internal/llm/provider.go b/internal/llm/provider.go
index d1ff404..f866c83 100644
--- a/internal/llm/provider.go
+++ b/internal/llm/provider.go
@@ -91,6 +91,8 @@ type Config struct {
AnthropicBaseURL string
AnthropicModel string
AnthropicTemperature *float64
+ // YouSearch options
+ YouSearchResearchEffort string // lite|standard|deep|exhaustive
}
// ProviderKeys contains API credentials used by provider factories.
@@ -101,6 +103,7 @@ type ProviderKeys struct {
OpenRouterAPIKey string
AnthropicAPIKey string
OllamaAPIKey string
+ YouSearchAPIKey string
}
// ProviderFactory builds an LLM client for a named provider.
@@ -134,14 +137,15 @@ func RegisterProvider(name string, factory ProviderFactory) {
}
// RegisterAllProviders registers all built-in LLM providers (anthropic, openai,
-// openrouter, ollama). It is safe to call from multiple entry points because the
-// actual registration runs only once via sync.Once.
+// openrouter, ollama, yousearch). It is safe to call from multiple entry points
+// because the actual registration runs only once via sync.Once.
func RegisterAllProviders() {
registerProvidersOnce.Do(func() {
RegisterProvider("anthropic", anthropicProviderFactory)
RegisterProvider("openai", openAIProviderFactory)
RegisterProvider("openrouter", openRouterProviderFactory)
RegisterProvider("ollama", ollamaProviderFactory)
+ RegisterProvider("yousearch", youSearchProviderFactory)
})
}
@@ -149,7 +153,7 @@ func RegisterAllProviders() {
// API keys are supplied separately and may be read from the environment by the
// caller. ollamaAPIKey is optional and only used when targeting Ollama Cloud;
// a local Ollama server works with an empty value.
-func NewFromConfig(cfg Config, openAIAPIKey, openRouterAPIKey, anthropicAPIKey, ollamaAPIKey string) (Client, error) {
+func NewFromConfig(cfg Config, openAIAPIKey, openRouterAPIKey, anthropicAPIKey, ollamaAPIKey, youSearchAPIKey string) (Client, error) {
provider := normalizeProvider(cfg.Provider)
if provider == "" {
provider = "ollama"
@@ -165,6 +169,7 @@ func NewFromConfig(cfg Config, openAIAPIKey, openRouterAPIKey, anthropicAPIKey,
OpenRouterAPIKey: openRouterAPIKey,
AnthropicAPIKey: anthropicAPIKey,
OllamaAPIKey: ollamaAPIKey,
+ YouSearchAPIKey: youSearchAPIKey,
})
}
@@ -207,6 +212,8 @@ func providerDisplayName(provider string) string {
return "OpenRouter"
case "anthropic":
return "Anthropic"
+ case "yousearch":
+ return "YouSearch"
default:
return provider
}
diff --git a/internal/llm/provider_more_test.go b/internal/llm/provider_more_test.go
index d3be8ef..18cd49a 100644
--- a/internal/llm/provider_more_test.go
+++ b/internal/llm/provider_more_test.go
@@ -16,7 +16,7 @@ func TestWithOptions_Apply(t *testing.T) {
func TestNewFromConfig_Success_OpenAI(t *testing.T) {
// OpenAI success
oc := Config{Provider: "openai", OpenAIBaseURL: "http://x", OpenAIModel: "gpt"}
- c, err := NewFromConfig(oc, "KEY", "", "", "")
+ c, err := NewFromConfig(oc, "KEY", "", "", "", "")
if err != nil || c == nil || c.Name() != "openai" || c.DefaultModel() == "" {
t.Fatalf("openai new: %v %v", c, err)
}
diff --git a/internal/llm/provider_test.go b/internal/llm/provider_test.go
index 2acbc69..4cba691 100644
--- a/internal/llm/provider_test.go
+++ b/internal/llm/provider_test.go
@@ -7,13 +7,13 @@ import (
func TestNewFromConfig_DefaultsAndErrors(t *testing.T) {
// Unknown provider
- if _, err := NewFromConfig(Config{Provider: "bogus"}, "", "", "", ""); err == nil {
+ if _, err := NewFromConfig(Config{Provider: "bogus"}, "", "", "", "", ""); err == nil {
t.Fatalf("expected error for unknown provider")
} else if !strings.Contains(err.Error(), "supported providers:") {
t.Fatalf("expected supported providers hint, got %q", err.Error())
}
// OpenAI missing key
- if _, err := NewFromConfig(Config{Provider: "openai", OpenAIModel: "g"}, "", "", "", ""); err == nil {
+ if _, err := NewFromConfig(Config{Provider: "openai", OpenAIModel: "g"}, "", "", "", "", ""); err == nil {
t.Fatalf("expected key error")
} else if !strings.Contains(err.Error(), "OPENAI_API_KEY") || !strings.Contains(err.Error(), "HEXAI_OPENAI_API_KEY") {
t.Fatalf("expected actionable API key hint, got %q", err.Error())
diff --git a/internal/llm/yousearch.go b/internal/llm/yousearch.go
new file mode 100644
index 0000000..d38a6e2
--- /dev/null
+++ b/internal/llm/yousearch.go
@@ -0,0 +1,163 @@
+// You.com Research API provider. Maps Chat() to a single research request using
+// the last user message as the query. System messages are ignored — the Research
+// API has its own reasoning pipeline. Sources are appended as a markdown section.
+package llm
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "strings"
+ "time"
+
+ "codeberg.org/snonux/hexai/internal/logging"
+)
+
+const youSearchResearchURL = "https://api.you.com/v1/research"
+
+type youSearchClient struct {
+ httpClient *http.Client
+ apiKey string
+ researchEffort string // lite|standard|deep|exhaustive
+ chatLogger logging.ChatLogger
+}
+
+var _ Client = youSearchClient{}
+
+type youSearchRequest struct {
+ Input string `json:"input"`
+ ResearchEffort string `json:"research_effort,omitempty"`
+}
+
+type youSearchSource struct {
+ URL string `json:"url"`
+ Title string `json:"title"`
+ Snippets []string `json:"snippets"`
+}
+
+type youSearchResponse struct {
+ Output struct {
+ Content interface{} `json:"content"`
+ ContentType string `json:"content_type"`
+ Sources []youSearchSource `json:"sources"`
+ } `json:"output"`
+}
+
+func youSearchProviderFactory(cfg Config, keys ProviderKeys) (Client, error) {
+ if strings.TrimSpace(keys.YouSearchAPIKey) == "" {
+ return nil, missingAPIKeyError("yousearch", "HEXAI_YOUSEARCH_API_KEY", "YOU_API_KEY")
+ }
+ timeoutSec := cfg.RequestTimeout
+ if timeoutSec <= 0 {
+ timeoutSec = 120
+ }
+ return youSearchClient{
+ httpClient: &http.Client{Timeout: time.Duration(timeoutSec) * time.Second},
+ apiKey: strings.TrimSpace(keys.YouSearchAPIKey),
+ researchEffort: strings.TrimSpace(cfg.YouSearchResearchEffort),
+ chatLogger: logging.NewChatLogger("yousearch"),
+ }, nil
+}
+
+func (c youSearchClient) Name() string { return "yousearch" }
+func (c youSearchClient) DefaultModel() string { return c.effectiveEffort() }
+
+func (c youSearchClient) effectiveEffort() string {
+ if c.researchEffort != "" {
+ return c.researchEffort
+ }
+ return "standard"
+}
+
+// Chat extracts the last user message and sends it as a research query.
+func (c youSearchClient) Chat(ctx context.Context, messages []Message, opts ...RequestOption) (string, error) {
+ query := lastUserMessage(messages)
+ if query == "" {
+ return "", fmt.Errorf("yousearch: no user message found in conversation")
+ }
+
+ start := time.Now()
+ logStartMessages(c.chatLogger, false, Options{Model: c.effectiveEffort()}, messages)
+
+ payload, err := json.Marshal(youSearchRequest{
+ Input: query,
+ ResearchEffort: c.effectiveEffort(),
+ })
+ if err != nil {
+ return "", err
+ }
+
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, youSearchResearchURL, bytes.NewReader(payload))
+ if err != nil {
+ return "", err
+ }
+ req.Header.Set("X-API-Key", c.apiKey)
+ req.Header.Set("Content-Type", "application/json")
+
+ logging.Logf("llm/yousearch", "POST %s effort=%s", youSearchResearchURL, c.effectiveEffort())
+ resp, err := c.httpClient.Do(req)
+ if err != nil {
+ logging.Logf("llm/yousearch", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase)
+ return "", err
+ }
+ defer func() {
+ if closeErr := resp.Body.Close(); closeErr != nil {
+ logging.Logf("llm/yousearch", "failed to close response body: %v", closeErr)
+ }
+ }()
+
+ if resp.StatusCode != http.StatusOK {
+ logging.Logf("llm/yousearch", "%shttp non-2xx status=%d duration=%s%s", logging.AnsiRed, resp.StatusCode, time.Since(start), logging.AnsiBase)
+ return "", fmt.Errorf("yousearch: API error status %d", resp.StatusCode)
+ }
+
+ var result youSearchResponse
+ if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
+ return "", fmt.Errorf("yousearch: decoding response: %w", err)
+ }
+
+ content := formatYouSearchContent(result)
+ if content == "" {
+ return "", fmt.Errorf("yousearch: empty response")
+ }
+
+ logging.Logf("llm/yousearch", "success size=%d preview=%s%s%s duration=%s",
+ len(content), logging.AnsiGreen, logging.PreviewForLog(content), logging.AnsiBase, time.Since(start))
+ return content, nil
+}
+
+func formatYouSearchContent(result youSearchResponse) string {
+ var sb strings.Builder
+
+ switch v := result.Output.Content.(type) {
+ case string:
+ sb.WriteString(strings.TrimSpace(v))
+ default:
+ out, _ := json.MarshalIndent(v, "", " ")
+ sb.Write(out)
+ }
+
+ if len(result.Output.Sources) > 0 {
+ sb.WriteString("\n\n**Sources:**\n")
+ for i, s := range result.Output.Sources {
+ title := s.Title
+ if title == "" {
+ title = s.URL
+ }
+ sb.WriteString(fmt.Sprintf("%d. [%s](%s)\n", i+1, title, s.URL))
+ }
+ }
+
+ return sb.String()
+}
+
+func lastUserMessage(messages []Message) string {
+ for i := len(messages) - 1; i >= 0; i-- {
+ if strings.ToLower(messages[i].Role) == "user" {
+ return strings.TrimSpace(messages[i].Content)
+ }
+ }
+ return ""
+}