1package openrouter
2
3import (
4 "github.com/charmbracelet/fantasy/ai"
5)
6
7const Name = "openrouter"
8
9type ReasoningEffort string
10
11const (
12 ReasoningEffortLow ReasoningEffort = "low"
13 ReasoningEffortMedium ReasoningEffort = "medium"
14 ReasoningEffortHigh ReasoningEffort = "high"
15)
16
17type ProviderMetadata struct{}
18
19func (*ProviderMetadata) Options() {}
20
21type ReasoningOptions struct {
22 // Whether reasoning is enabled
23 Enabled *bool `json:"enabled"`
24 // Whether to exclude reasoning from the response
25 Exclude *bool `json:"exclude"`
26 // Maximum number of tokens to use for reasoning
27 MaxTokens *int64 `json:"max_tokens"`
28 // Reasoning effort level: "low" | "medium" | "high"
29 Effort *ReasoningEffort `json:"effort"`
30}
31
32type Provider struct {
33 // List of provider slugs to try in order (e.g. ["anthropic", "openai"])
34 Order []string `json:"order"`
35 // Whether to allow backup providers when primary is unavailable (default: true)
36 AllowFallbacks *bool `json:"allow_fallbacks"`
37 // Only use providers that support all parameters in your request (default: false)
38 RequireParameters *bool `json:"require_parameters"`
39 // Control whether to use providers that may store data: "allow" | "deny"
40 DataCollection *string `json:"data_collection"`
41 // List of provider slugs to allow for this request
42 Only []string `json:"only"`
43 // List of provider slugs to skip for this request
44 Ignore []string `json:"ignore"`
45 // List of quantization levels to filter by (e.g. ["int4", "int8"])
46 Quantizations []string `json:"quantizations"`
47 // Sort providers by "price" | "throughput" | "latency"
48 Sort *string `json:"sort"`
49}
50
51type ProviderOptions struct {
52 Reasoning *ReasoningOptions `json:"reasoning"`
53 ExtraBody map[string]any `json:"extra_body"`
54 IncludeUsage *bool `json:"include_usage"`
55 // Modify the likelihood of specified tokens appearing in the completion.
56 // Accepts a map that maps tokens (specified by their token ID) to an associated bias value from -100 to 100.
57 // The bias is added to the logits generated by the model prior to sampling.
58 LogitBias map[string]int64 `json:"logit_bias"`
59 // Return the log probabilities of the tokens. Including logprobs will increase the response size.
60 // Setting to true will return the log probabilities of the tokens that were generated.
61 LogProbs *bool `json:"log_probs"`
62 // Whether to enable parallel function calling during tool use. Default to true.
63 ParallelToolCalls *bool `json:"parallel_tool_calls"`
64 // A unique identifier representing your end-user, which can help OpenRouter to monitor and detect abuse.
65 User *string `json:"user"`
66 // Provider routing preferences to control request routing behavior
67 Provider *Provider `json:"provider"`
68 // TODO: add the web search plugin config
69}
70
71func (*ProviderOptions) Options() {}
72
73func ReasoningEffortOption(e ReasoningEffort) *ReasoningEffort {
74 return &e
75}
76
77func NewProviderOptions(opts *ProviderOptions) ai.ProviderOptions {
78 return ai.ProviderOptions{
79 Name: opts,
80 }
81}