v0.2.54 5c27086

extra_providers.go RawHistoryFolder

1package provider
2
3// Extra third-party providers.
4//
5// Most are OpenAI Chat Completions–compatible, so they reuse `openaiClient`
6// with a different name + base URL. A handful speak the Anthropic Messages
7// API and reuse `anthropicClient` via NewAnthropicCompat below.
8//
9// Providers with a non-trivial protocol (Bedrock Converse, Vertex SSE, Azure
10// Responses, Mistral Conversations) are stubbed so the host wiring compiles.
11// Filling those in is its own work — Bedrock needs SigV4 + Converse stream
12// parsing, Vertex needs ADC, Azure needs the Responses API shape, Mistral
13// needs its bespoke Conversations API.
14
15import (
16	"context"
17	"fmt"
18	"net/http"
19	"os"
20	"strings"
21)
22
23// ----------------------------------------------------------------------
24// OpenAI Chat Completions–compatible providers. All of these just route
25// Chat Completions to a different base URL with a different name. The
26// default Authorization: Bearer header from openaiClient is correct for
27// every one of them.
28// ----------------------------------------------------------------------
29
30// NewOpenAICompat is the shared constructor for OpenAI-completions clones.
31// Exported so build.go can instantiate clients for user-defined providers.
32func NewOpenAICompat(name, apiKey, baseURL, fallbackBaseURL string) Client {
33	if baseURL == "" {
34		baseURL = fallbackBaseURL
35	}
36	return &openaiClient{
37		apiKey:  apiKey,
38		baseURL: strings.TrimRight(baseURL, "/"),
39		name:    name,
40		http:    &http.Client{Timeout: 0},
41	}
42}
43
44// NewMoonshot is the global Moonshot AI endpoint (Kimi-K2 family by id).
45// Provider id is `moonshotai`.
46func NewMoonshot(apiKey, baseURL string) Client {
47	return NewOpenAICompat("moonshotai", apiKey, baseURL, "https://api.moonshot.ai/v1")
48}
49
50// NewMoonshotCN is the China-region Moonshot endpoint. Same model ids as
51// the global flavor, different base URL.
52func NewMoonshotCN(apiKey, baseURL string) Client {
53	return NewOpenAICompat("moonshotai-cn", apiKey, baseURL, "https://api.moonshot.cn/v1")
54}
55
56// NewCerebras: ultra-fast inference (Llama/Qwen/GPT-OSS/GLM).
57func NewCerebras(apiKey, baseURL string) Client {
58	return NewOpenAICompat("cerebras", apiKey, baseURL, "https://api.cerebras.ai/v1")
59}
60
61// NewGroq: LPU inference (Llama/Kimi/Qwen/GPT-OSS).
62func NewGroq(apiKey, baseURL string) Client {
63	return NewOpenAICompat("groq", apiKey, baseURL, "https://api.groq.com/openai/v1")
64}
65
66// NewXAI: xAI Grok.
67func NewXAI(apiKey, baseURL string) Client {
68	return NewOpenAICompat("xai", apiKey, baseURL, "https://api.x.ai/v1")
69}
70
71// NewTogether: Together.ai aggregator.
72func NewTogether(apiKey, baseURL string) Client {
73	return NewOpenAICompat("together", apiKey, baseURL, "https://api.together.ai/v1")
74}
75
76// NewHuggingFace: HF inference router.
77func NewHuggingFace(apiKey, baseURL string) Client {
78	return NewOpenAICompat("huggingface", apiKey, baseURL, "https://router.huggingface.co/v1")
79}
80
81// NewZAI: Z.AI GLM family.
82func NewZAI(apiKey, baseURL string) Client {
83	return NewOpenAICompat("zai", apiKey, baseURL, "https://api.z.ai/api/coding/paas/v4")
84}
85
86// NewXiaomi: Xiaomi MiMo family (default endpoint).
87func NewXiaomi(apiKey, baseURL string) Client {
88	return NewOpenAICompat("xiaomi", apiKey, baseURL, "https://api.xiaomimimo.com/v1")
89}
90
91// NewXiaomiTokenPlan creates a regional Xiaomi token-plan client.
92// region must be "ams", "cn", or "sgp", matching the three
93// `xiaomi-token-plan-*` provider ids.
94func NewXiaomiTokenPlan(region, apiKey, baseURL string) Client {
95	var fallback string
96	var name string
97	switch region {
98	case "ams":
99		fallback = "https://token-plan-ams.xiaomimimo.com/v1"
100		name = "xiaomi-token-plan-ams"
101	case "cn":
102		fallback = "https://token-plan-cn.xiaomimimo.com/v1"
103		name = "xiaomi-token-plan-cn"
104	case "sgp":
105		fallback = "https://token-plan-sgp.xiaomimimo.com/v1"
106		name = "xiaomi-token-plan-sgp"
107	default:
108		panic(fmt.Sprintf("xiaomi token-plan: unknown region %q", region))
109	}
110	return NewOpenAICompat(name, apiKey, baseURL, fallback)
111}
112
113// NewOpenRouter: OpenRouter aggregator. Unlocks dozens of upstream
114// models with one key.
115func NewOpenRouter(apiKey, baseURL string) Client {
116	return NewOpenAICompat("openrouter", apiKey, baseURL, openrouterDefaultBaseURL)
117}
118
119// NewOpenCode is the opencode.ai Zen endpoint. Mixed APIs upstream; this
120// constructor wires the openai-completions flavor only. Models that need
121// the anthropic-messages flavor under the same provider should be built
122// with NewAnthropicCompat against the same base URL.
123func NewOpenCode(apiKey, baseURL string) Client {
124	return NewOpenAICompat("opencode", apiKey, baseURL, "https://opencode.ai/zen/v1")
125}
126
127// NewOpenCodeGo is the opencode-go variant.
128func NewOpenCodeGo(apiKey, baseURL string) Client {
129	return NewOpenAICompat("opencode-go", apiKey, baseURL, "https://opencode.ai/zen/go/v1")
130}
131
132// NewMinimaxOpenAI is the OpenAI-completions flavor of MiniMax, in case
133// downstream models switch from anthropic-messages. The main MiniMax route
134// uses anthropic-messages; see NewMinimaxAnthropic below.
135func NewMinimaxOpenAI(apiKey, baseURL string) Client {
136	return NewOpenAICompat("minimax", apiKey, baseURL, "https://api.minimax.io/v1")
137}
138
139// NewMinimaxCNOpenAI is the CN-region MiniMax (openai-completions).
140func NewMinimaxCNOpenAI(apiKey, baseURL string) Client {
141	return NewOpenAICompat("minimax-cn", apiKey, baseURL, "https://api.minimaxi.com/v1")
142}
143
144// NewFireworksOpenAI is the OpenAI-completions flavor of Fireworks.
145// The main route uses the anthropic-messages variant on
146// api.fireworks.ai/inference; see NewFireworksAnthropic.
147func NewFireworksOpenAI(apiKey, baseURL string) Client {
148	return NewOpenAICompat("fireworks", apiKey, baseURL, "https://api.fireworks.ai/inference/v1")
149}
150
151// NewVercelGatewayOpenAI is Vercel AI Gateway's OpenAI-compat shim. The
152// main route uses anthropic-messages; see NewVercelGatewayAnthropic.
153func NewVercelGatewayOpenAI(apiKey, baseURL string) Client {
154	return NewOpenAICompat("vercel-ai-gateway", apiKey, baseURL, "https://ai-gateway.vercel.sh/v1")
155}
156
157// ----------------------------------------------------------------------
158// Anthropic Messages–compatible providers. These speak Anthropic's wire
159// format but live behind a third party's base URL. They reuse
160// anthropicClient with a custom name.
161// ----------------------------------------------------------------------
162
163// NewAnthropicCompat returns an anthropicClient pinned to a non-default
164// base URL and identifying as `name` for cost / logging purposes. Auth is
165// API key (x-api-key header). For OAuth-fronted compatibles (rare) use
166// NewAnthropicOAuth and rename via NameClient.
167func NewAnthropicCompat(name, apiKey, baseURL string) Client {
168	if baseURL == "" {
169		baseURL = anthropicDefaultBaseURL
170	}
171	return &anthropicClient{
172		apiKey:  apiKey,
173		baseURL: strings.TrimRight(baseURL, "/"),
174		name:    name,
175		http:    &http.Client{Timeout: 0},
176	}
177}
178
179// NewKimiCoding is the Kimi Code client: Kimi behind the Anthropic
180// Messages API at https://api.kimi.com/coding. Replaces the older
181// OpenAI-completions-on-/coding/v1 wiring.
182func NewKimiCoding(apiKey, baseURL string) Client {
183	return NewKimiCodingWithHeaders(apiKey, baseURL, nil)
184}
185
186// NewKimiCodingWithHeaders is the headered variant used by OAuth.
187func NewKimiCodingWithHeaders(apiKey, baseURL string, headers map[string]string) Client {
188	if baseURL == "" {
189		baseURL = "https://api.kimi.com/coding"
190	}
191	return &anthropicClient{
192		apiKey:  apiKey,
193		baseURL: strings.TrimRight(baseURL, "/"),
194		name:    "kimi",
195		headers: headers,
196		http:    &http.Client{Timeout: 0},
197	}
198}
199
200// NewMinimaxAnthropic is the anthropic-messages flavor on
201// api.minimax.io/anthropic, catalogued under provider=minimax.
202func NewMinimaxAnthropic(apiKey, baseURL string) Client {
203	return NewAnthropicCompat("minimax", apiKey, firstNonEmptyString(baseURL, "https://api.minimax.io/anthropic"))
204}
205
206// NewMinimaxCNAnthropic is the CN-region MiniMax (anthropic-messages).
207func NewMinimaxCNAnthropic(apiKey, baseURL string) Client {
208	return NewAnthropicCompat("minimax-cn", apiKey, firstNonEmptyString(baseURL, "https://api.minimaxi.com/anthropic"))
209}
210
211// NewFireworksAnthropic is the main Fireworks route. The
212// anthropic-messages-compatible endpoint at api.fireworks.ai/inference
213// expects Anthropic-style request bodies; use this rather than the
214// OpenAI flavor.
215func NewFireworksAnthropic(apiKey, baseURL string) Client {
216	return NewAnthropicCompat("fireworks", apiKey, firstNonEmptyString(baseURL, "https://api.fireworks.ai/inference"))
217}
218
219// NewVercelGatewayAnthropic — Vercel AI Gateway anthropic-messages route.
220func NewVercelGatewayAnthropic(apiKey, baseURL string) Client {
221	return NewAnthropicCompat("vercel-ai-gateway", apiKey, firstNonEmptyString(baseURL, "https://ai-gateway.vercel.sh"))
222}
223
224// ----------------------------------------------------------------------
225// Stubbed providers — bigger protocol surface, follow-up work.
226// ----------------------------------------------------------------------
227
228type unimplementedClient struct {
229	name string
230	hint string
231}
232
233func (c *unimplementedClient) Name() string { return c.name }
234
235func (c *unimplementedClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
236	return nil, fmt.Errorf("provider %q not yet implemented: %s", c.name, c.hint)
237}
238
239// NewBedrock returns an AWS Bedrock client. See amazon_bedrock.go for the
240// hand-rolled Converse-Stream wire-format parser. Auth is via
241// AWS_BEARER_TOKEN_BEDROCK (the modern Bedrock API key flow); SigV4
242// signing for IAM access-key + secret credentials is not yet wired.
243func NewBedrock(apiKey, baseURL string) Client {
244	return NewBedrockClient(apiKey, baseURL)
245}
246
247// NewGoogleVertex returns a Vertex AI client. See google_vertex.go for
248// the full auth + URL-rewrite implementation. Requires GOOGLE_CLOUD_PROJECT,
249// GOOGLE_CLOUD_LOCATION, and either GOOGLE_CLOUD_API_KEY or a service-
250// account JSON file pointed to by GOOGLE_APPLICATION_CREDENTIALS (or the
251// default ADC location ~/.config/gcloud/application_default_credentials.json).
252func NewGoogleVertex(apiKey, baseURL string) Client {
253	return NewVertex(apiKey, baseURL)
254}
255
256// NewAzureOpenAIResponses delegates to the real Azure OpenAI client.
257// Despite the provider id mentioning "responses", zot uses Azure's
258// Chat Completions endpoint (older but functionally complete for our
259// agent loop) to avoid duplicating the full openai-responses wire
260// client. Models register under provider id `azure-openai-responses`
261// so user catalogs keep working unchanged.
262func NewAzureOpenAIResponses(apiKey, baseURL string) Client {
263	return NewAzureOpenAI(apiKey, baseURL)
264}
265
266// NewMistral returns a Mistral client using their OpenAI-compatible Chat
267// Completions endpoint at https://api.mistral.ai/v1. Mistral also offers a
268// bespoke "Conversations" API, but the OpenAI-compat endpoint supports
269// the same models with tool calling and streaming, so we use that for
270// simplicity (no extra wire format to maintain).
271func NewMistral(apiKey, baseURL string) Client {
272	return NewOpenAICompat("mistral", apiKey, baseURL, "https://api.mistral.ai/v1")
273}
274
275// Cloudflare endpoints carry `{CLOUDFLARE_ACCOUNT_ID}` and (for the AI
276// Gateway) `{CLOUDFLARE_GATEWAY_ID}` placeholders that we substitute
277// from env vars at client-construction time. Workers AI uses standard
278// `Authorization: Bearer <key>`; AI Gateway uses `cf-aig-authorization`
279// (the upstream Authorization header is passed through to whichever
280// downstream provider the gateway forwards to).
281
282func resolveCloudflareURL(template string) (string, error) {
283	out := template
284	for _, key := range []string{"CLOUDFLARE_ACCOUNT_ID", "CLOUDFLARE_GATEWAY_ID"} {
285		placeholder := "{" + key + "}"
286		if !strings.Contains(out, placeholder) {
287			continue
288		}
289		v := os.Getenv(key)
290		if v == "" {
291			return "", fmt.Errorf("%s is required but not set in env", key)
292		}
293		out = strings.ReplaceAll(out, placeholder, v)
294	}
295	return out, nil
296}
297
298// NewCloudflareWorkersAI returns an OpenAI-compatible client pinned to
299// the Workers AI base URL with {CLOUDFLARE_ACCOUNT_ID} substituted.
300// Returns an erroring client (deferred error on first Stream call) if
301// the env var is missing, so the constructor signature stays the same.
302func NewCloudflareWorkersAI(apiKey, baseURL string) Client {
303	if baseURL == "" {
304		baseURL = "https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/v1"
305	}
306	resolved, err := resolveCloudflareURL(baseURL)
307	if err != nil {
308		return &unimplementedClient{name: "cloudflare-workers-ai", hint: err.Error()}
309	}
310	return NewOpenAICompat("cloudflare-workers-ai", apiKey, resolved, "")
311}
312
313// NewCloudflareAIGateway returns the AI Gateway client (OpenAI compat
314// route). Sends `cf-aig-authorization` instead of `Authorization` so
315// the gateway authenticates the caller (downstream-provider auth is
316// configured per-gateway in the Cloudflare dashboard).
317func NewCloudflareAIGateway(apiKey, baseURL string) Client {
318	if baseURL == "" {
319		baseURL = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/compat"
320	}
321	resolved, err := resolveCloudflareURL(baseURL)
322	if err != nil {
323		return &unimplementedClient{name: "cloudflare-ai-gateway", hint: err.Error()}
324	}
325	return &openaiClient{
326		apiKey:  apiKey,
327		baseURL: strings.TrimRight(resolved, "/"),
328		name:    "cloudflare-ai-gateway",
329		headers: map[string]string{"cf-aig-authorization": "Bearer " + apiKey},
330		http:    &http.Client{Timeout: 0},
331	}
332}
333
334// NewGithubCopilot returns a GitHub Copilot client. The provided
335// credential must be a GitHub Personal Access Token (PAT) with Copilot
336// access enabled; zot trades it for a short-lived Copilot token on
337// every inference request (cached in memory until ~5min before expiry).
338//
339// Wire format: OpenAI Chat Completions. Copilot-specific headers
340// (X-Initiator, Openai-Intent, Editor-Version, Editor-Plugin-Version,
341// Copilot-Integration-Id, User-Agent) are added by the refresh
342// transport. The model id passes through unchanged.
343//
344// baseURL is ignored: the canonical host is read from `proxy-ep=...` in
345// the short-lived token's value.
346func NewGithubCopilot(apiKey, _ string) Client {
347	if apiKey == "" {
348		return &unimplementedClient{name: "github-copilot", hint: "set COPILOT_GITHUB_TOKEN"}
349	}
350	return NewGithubCopilotClient(apiKey)
351}
352
353// ----------------------------------------------------------------------
354// helpers
355// ----------------------------------------------------------------------
356
357func firstNonEmptyString(vals ...string) string {
358	for _, v := range vals {
359		if v != "" {
360			return v
361		}
362	}
363	return ""
364}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close