v0.38.1 969e52a

model_router.go RawHistoryFolder

1package provider
2
3import (
4	"context"
5	"fmt"
6	"strings"
7	"sync"
8)
9
10const (
11	// APICompletions identifies the OpenAI Chat Completions wire API.
12	APICompletions = "openai-completions"
13	// APIResponses identifies the OpenAI Responses wire API.
14	APIResponses = "openai-responses"
15	// APIAnthropicMessages identifies the Anthropic Messages wire API.
16	APIAnthropicMessages = "anthropic-messages"
17)
18
19// modelRouter dispatches each request according to the selected model's API.
20// This lets one provider expose models that use different wire protocols while
21// remaining reusable across model switches.
22type modelRouter struct {
23	name             string
24	fallback         Client
25	byAPI            map[string]Client
26	modelOverridesMu sync.RWMutex
27	modelOverrides   map[string]Model
28	dynamicCatalog   bool
29	scopedCatalog    bool
30}
31
32// ModelMetadataSetter lets a runtime-owned client retain model metadata
33// without consulting the process-global catalog on every request.
34type ModelMetadataSetter interface {
35	SetModelMetadata(Model)
36}
37
38// NewModelRouter creates a client that dispatches requests using Model.API.
39// Models with no API override, and models absent from the catalog, use fallback;
40// an explicit API without a registered client returns an error.
41func NewModelRouter(name string, fallback Client, byAPI map[string]Client) Client {
42	return &modelRouter{name: name, fallback: fallback, byAPI: byAPI}
43}
44
45func (c *modelRouter) Name() string { return c.name }
46
47func (c *modelRouter) SetModelMetadata(model Model) {
48	model = cloneModel(model)
49	c.modelOverridesMu.Lock()
50	if c.modelOverrides == nil {
51		c.modelOverrides = make(map[string]Model)
52	}
53	c.modelOverrides[model.ID] = model
54	c.modelOverridesMu.Unlock()
55
56	setModelMetadata(c.fallback, model)
57	for _, client := range c.byAPI {
58		setModelMetadata(client, model)
59	}
60}
61
62func (c *modelRouter) modelOverride(id string) (Model, bool) {
63	c.modelOverridesMu.RLock()
64	model, ok := c.modelOverrides[id]
65	if ok {
66		model = cloneModel(model)
67	}
68	c.modelOverridesMu.RUnlock()
69	return model, ok
70}
71
72func setModelMetadata(client Client, model Model) {
73	if setter, ok := client.(ModelMetadataSetter); ok {
74		setter.SetModelMetadata(model)
75	}
76}
77
78func (c *modelRouter) Stream(ctx context.Context, req Request) (<-chan Event, error) {
79	req.Model = strings.TrimSpace(req.Model)
80	if err := ValidateFastMode(c.Name(), req.FastMode); err != nil {
81		return nil, err
82	}
83	client := c.fallback
84	api := ""
85	found := false
86	if c.dynamicCatalog {
87		// Normal CLI/RPC runtimes follow the process-global catalog so a
88		// background refresh can replace stale request metadata. Keep a
89		// bootstrap override only until an authoritative live entry exists.
90		if model, err := FindModel(c.name, req.Model); err == nil {
91			api = model.API
92			found = true
93			c.SetModelMetadata(model)
94		} else if !IsProviderCatalogAuthoritative(c.name) {
95			if model, ok := c.modelOverride(req.Model); ok {
96				api = model.API
97				found = true
98			}
99		}
100	} else if model, ok := c.modelOverride(req.Model); ok {
101		api = model.API
102		found = true
103	} else if !c.scopedCatalog {
104		if model, err := FindModel(c.name, req.Model); err == nil {
105			api = model.API
106			found = true
107		}
108	}
109	if !found && c.dynamicCatalog && IsProviderCatalogAuthoritative(c.name) {
110		return nil, fmt.Errorf("unknown model %q (provider=%q)", req.Model, c.name)
111	}
112	if api == "" && !c.scopedCatalog && AcceptsUnlistedModels(c.name) {
113		// The OpenCode Go catalog is intentionally live-only. Preserve the
114		// family route for an explicit model while the first discovery is
115		// still in flight or unavailable.
116		api = openCodeGoAPIForModel(req.Model)
117	}
118	if api != "" {
119		routed := c.byAPI[api]
120		if routed == nil && api == APICompletions {
121			routed = c.fallback
122		}
123		if routed == nil {
124			return nil, fmt.Errorf("provider %q model %q requests unsupported wire API %q", c.name, req.Model, api)
125		}
126		client = routed
127	}
128	return client.Stream(ctx, req)
129}

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