返回 DeepSeek-Reasonix
pi_catalog.go
根目录 / internal / provider / pi_catalog.go
1 package provider
2
3 import (
4 "slices"
5 "sort"
6 "strings"
7
8 piAI "github.com/sky-valley/pi/ai"
9 )
10
11 // PiCatalogModelInfo adapts the embedded sky-valley/pi catalog to Reasonix's
12 // provider-neutral model metadata. Only an exact provider/model and matching
13 // official route are accepted; custom endpoints never inherit catalog facts.
14 func PiCatalogModelInfo(kind, baseURL, model string) (ModelInfo, bool) {
15 route, ok := OfficialOpenCodeGoRoute(kind, baseURL)
16 if !ok {
17 return ModelInfo{}, false
18 }
19 for _, candidate := range piCatalogOpenCodeGoModels(route) {
20 if candidate.ID == strings.TrimSpace(model) {
21 return modelInfoFromPi(candidate), true
22 }
23 }
24 return ModelInfo{}, false
25 }
26
27 // PiCatalogModelInfoForProvider resolves the installed catalog using the
28 // configured provider id when it is an exact pi provider id. Endpoint and API
29 // must also match the catalog entry, preventing a custom gateway from
30 // accidentally inheriting another vendor's metadata.
31 func PiCatalogModelInfoForProvider(providerID, kind, baseURL, model string) (ModelInfo, bool) {
32 providerID = strings.TrimSpace(providerID)
33 if providerID == "" {
34 return ModelInfo{}, false
35 }
36 api := expectedCatalogAPI(kind)
37 configuredURL := strings.TrimRight(strings.TrimSpace(baseURL), "/")
38 if route, ok := OfficialOpenCodeGoRoute(kind, configuredURL); ok && piRouteCorrectedAway(route, strings.TrimSpace(model)) {
39 return ModelInfo{}, false
40 }
41 for _, candidate := range piAI.GetModels(providerID) {
42 if candidate == nil || candidate.ID != strings.TrimSpace(model) || strings.ToLower(strings.TrimSpace(string(candidate.Api))) != api {
43 continue
44 }
45 catalogURL := strings.TrimRight(strings.TrimSpace(candidate.BaseURL), "/")
46 if configuredURL != catalogURL {
47 configuredRoute, configuredOfficial := OfficialOpenCodeGoRoute(kind, configuredURL)
48 catalogRoute, catalogOfficial := OfficialOpenCodeGoRoute(kind, catalogURL)
49 if !configuredOfficial || !catalogOfficial || configuredRoute != catalogRoute {
50 continue
51 }
52 }
53 return modelInfoFromPi(candidate), true
54 }
55 return ModelInfo{}, false
56 }
57
58 func expectedCatalogAPI(kind string) string {
59 switch strings.ToLower(strings.TrimSpace(kind)) {
60 case "openai", "chat", "":
61 return "openai-completions"
62 case "responses":
63 return "openai-responses"
64 case "anthropic":
65 return "anthropic-messages"
66 default:
67 return ""
68 }
69 }
70
71 // PiCatalogModelInfos returns the complete embedded catalog for one pi
72 // provider ID. Callers should still verify that the configured endpoint and
73 // protocol match the catalog provider before applying these facts.
74 func PiCatalogModelInfos(providerID string) []ModelInfo {
75 models := piAI.GetModels(strings.TrimSpace(providerID))
76 out := make([]ModelInfo, 0, len(models))
77 for _, model := range models {
78 if model != nil {
79 out = append(out, modelInfoFromPi(model))
80 }
81 }
82 return out
83 }
84
85 // PiCatalogOpenCodeGoModelIDs returns the exact model IDs exposed by one
86 // OpenCode Go wire route in the embedded catalog.
87 func PiCatalogOpenCodeGoModelIDs(route string) []string {
88 models := piCatalogOpenCodeGoModels(route)
89 ids := make([]string, 0, len(models))
90 for _, model := range models {
91 ids = append(ids, model.ID)
92 }
93 sort.Strings(ids)
94 return ids
95 }
96
97 // PiCatalogOpenCodeGoReasoning exposes catalog wire values, not Pi's clamping
98 // policy. Missing off mappings do not invent a disable token for the adapter.
99 func PiCatalogOpenCodeGoReasoning(route, id string) (ReasoningCapability, bool) {
100 for _, model := range piCatalogOpenCodeGoModels(route) {
101 if model.ID != id {
102 continue
103 }
104 if !model.Reasoning {
105 return ReasoningOptions(""), true
106 }
107 var ids []string
108 for _, level := range piAI.GetSupportedThinkingLevels(model) {
109 value, explicit := model.ThinkingLevelMap[level]
110 wire := string(level)
111 if explicit {
112 if value == nil {
113 continue
114 }
115 wire = *value
116 } else if level == "off" {
117 continue
118 }
119 if wire != "" && !slices.Contains(ids, wire) {
120 ids = append(ids, wire)
121 }
122 }
123 def := "high"
124 if !slices.Contains(ids, def) {
125 def = ""
126 if len(ids) > 0 {
127 def = ids[0]
128 }
129 }
130 return ReasoningOptions(def, ids...), true
131 }
132 return ReasoningCapability{}, false
133 }
134
135 // PiCatalogOpenCodeGoVisionModelIDs returns the route's catalog models that
136 // explicitly accept image input.
137 func PiCatalogOpenCodeGoVisionModelIDs(route string) []string {
138 models := piCatalogOpenCodeGoModels(route)
139 ids := make([]string, 0, len(models))
140 for _, model := range models {
141 if modelInfoFromPi(model).SupportsInput(ModalityImage) {
142 ids = append(ids, model.ID)
143 }
144 }
145 sort.Strings(ids)
146 return ids
147 }
148
149 // piOpenCodeGoRouteCorrections pins models whose Pi route annotation disagrees
150 // with both the opencode.ai/docs/go Endpoints table and models.dev.
151 var piOpenCodeGoRouteCorrections = map[string]string{
152 "minimax-m2.7": OpenCodeGoRouteAnthropic,
153 }
154
155 // OpenCodeGoRouteCorrected reports a model whose route differs from the one
156 // the embedded Pi catalog annotates.
157 func OpenCodeGoRouteCorrected(model string) bool {
158 _, ok := piOpenCodeGoRouteCorrections[strings.TrimSpace(model)]
159 return ok
160 }
161
162 func piRouteCorrectedAway(route, id string) bool {
163 corrected, ok := piOpenCodeGoRouteCorrections[id]
164 return ok && corrected != route
165 }
166
167 func piCatalogOpenCodeGoModels(route string) []*piAI.Model {
168 var wantAPI string
169 switch route {
170 case OpenCodeGoRouteChat:
171 wantAPI = "openai-completions"
172 case OpenCodeGoRouteAnthropic:
173 wantAPI = "anthropic-messages"
174 case OpenCodeGoRouteResponses:
175 wantAPI = "openai-responses"
176 default:
177 return nil
178 }
179 models := make([]*piAI.Model, 0)
180 for _, model := range piAI.GetModels("opencode-go") {
181 if model == nil {
182 continue
183 }
184 modelRoute, official := OfficialOpenCodeGoRoute(routeKind(route), model.BaseURL)
185 if strings.EqualFold(string(model.Api), wantAPI) && official && modelRoute == route && !piRouteCorrectedAway(route, model.ID) {
186 models = append(models, model)
187 }
188 }
189 return models
190 }
191
192 func modelInfoFromPi(model *piAI.Model) ModelInfo {
193 modalities := make([]ModelModality, 0, len(model.Input))
194 for _, input := range model.Input {
195 modality := ModelModality(strings.ToLower(strings.TrimSpace(input)))
196 if modality == ModalityText || modality == ModalityImage {
197 modalities = append(modalities, modality)
198 }
199 }
200 if len(modalities) == 0 {
201 modalities = []ModelModality{ModalityText}
202 }
203 return ModelInfo{
204 ID: model.ID,
205 Name: model.Name,
206 API: string(model.Api),
207 BaseURL: model.BaseURL,
208 InputModalities: modalities,
209 ContextWindow: model.ContextWindow,
210 MaxOutputTokens: model.MaxTokens,
211 Reasoning: model.Reasoning,
212 }
213 }
214
214 lines GO