File size: 10,485 Bytes
e48a2d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// extensions.go 面板扩展层在网关侧的接线:
//   - 访问密钥鉴权与模型白/黑名单;
//   - 自定义模型映射(每次请求注入提示词);
//   - 免登录免费模型(free:<provider>/<model> 直连上游)。
package server

import (
	"context"
	"encoding/json"
	"net/http"
	"strings"
	"time"

	"github.com/misakano7545/workbuddy2api-panel/internal/extstore"
	"github.com/misakano7545/workbuddy2api-panel/internal/freemodel"
)

// ── 访问密钥上下文 ──────────────────────────────────────────────────────────

type accessKeyCtx struct{}

// withAccessKey 把命中的访问密钥挂到请求上下文(主 api_key 传 nil)。
func withAccessKey(r *http.Request, key *extstore.AccessKey) *http.Request {
	if key == nil {
		return r
	}
	return r.WithContext(context.WithValue(r.Context(), accessKeyCtx{}, *key))
}

// accessKeyFrom 取出本次请求的访问密钥;主 api_key/未鉴权返回 false。
func accessKeyFrom(r *http.Request) (extstore.AccessKey, bool) {
	key, ok := r.Context().Value(accessKeyCtx{}).(extstore.AccessKey)
	return key, ok
}

// keyAllowsModel 判断访问密钥能否使用某个客户端模型名。
//
// 规则:先看黑名单(命中即拒绝),再看白名单(非空时必须命中);两边都空 = 全部允许。
// 模式支持 *(任意长度)与 ?(单字符),大小写不敏感;同时比较完整 id 与去掉
// cn:/global:/free: 前缀后的裸名,方便写 "cn:deepseek-*" 或 "deepseek-*"。
func keyAllowsModel(key *extstore.AccessKey, model string) bool {
	if key == nil {
		return true
	}
	for _, pattern := range key.DenyModels {
		if matchModelPattern(pattern, model) {
			return false
		}
	}
	if len(key.AllowModels) == 0 {
		return true
	}
	for _, pattern := range key.AllowModels {
		if matchModelPattern(pattern, model) {
			return true
		}
	}
	return false
}

// wildcardMatch 复用 handler.go 中的实现(大小写不敏感、* 与 ?)。
// 这里只保留模型名解析,避免两套通配语义漂移。
func matchModelPattern(pattern, model string) bool {
	pattern = strings.TrimSpace(pattern)
	if pattern == "" {
		return false
	}
	candidates := []string{model}
	if _, bare, ok := cutRealm(model); ok {
		candidates = append(candidates, bare)
	}
	if strings.HasPrefix(model, freemodel.Prefix) {
		rest := strings.TrimPrefix(model, freemodel.Prefix)
		if _, id, ok := strings.Cut(rest, "/"); ok {
			candidates = append(candidates, id)
		}
	}
	for _, candidate := range candidates {
		if wildcardMatch(pattern, candidate) {
			return true
		}
	}
	return false
}

func cutRealm(model string) (realm, bare string, ok bool) {
	realm, bare, ok = strings.Cut(model, ":")
	if !ok || (realm != "cn" && realm != "global") {
		return "", model, false
	}
	return realm, bare, true
}

// filterModelEntriesByKey 按访问密钥过滤 /v1/models 条目。
func filterModelEntriesByKey(entries []map[string]any, key *extstore.AccessKey) []map[string]any {
	if key == nil {
		return entries
	}
	out := make([]map[string]any, 0, len(entries))
	for _, entry := range entries {
		id, _ := entry["id"].(string)
		if id != "" && keyAllowsModel(key, id) {
			out = append(out, entry)
		}
	}
	return out
}

// ── 扩展模型路由 ────────────────────────────────────────────────────────────

// extensionChat 在进入账号池之前处理扩展模型。
//
// 返回值:
//   - handled=true:请求已经由免费层处理完(或已写错误),调用方直接返回;
//   - body/model/echo 非空:自定义模型映射改写了请求,调用方继续走原流程
//     (选号/计费),echo 是客户端原始模型名(响应侧回显用);
//   - 三者皆空/false:未命中扩展层,原样继续。
func (h *Handler) extensionChat(w http.ResponseWriter, r *http.Request, body []byte, model string) (handled bool, newBody []byte, newModel, echo string) {
	if h.cfg.Extensions == nil {
		return false, nil, "", ""
	}
	requested := model
	snap := h.cfg.Extensions.Snapshot()
	rewritten := false

	if cm, ok := snap.CustomFor(model); ok {
		body2, err := applyCustomModel(body, cm)
		if err != nil {
			writeOpenAIError(w, http.StatusBadRequest, "invalid_request_error", err.Error())
			return true, nil, "", ""
		}
		body = body2
		model = cm.Target
		rewritten = true
	}

	if strings.HasPrefix(model, freemodel.Prefix) {
		if h.cfg.FreeModels == nil {
			writeOpenAIError(w, http.StatusServiceUnavailable, "model_unavailable", "free model layer is not configured")
			return true, nil, "", ""
		}
		h.cfg.FreeModels.ServeChatEcho(w, r, body, model, requested)
		return true, nil, "", ""
	}
	if !rewritten {
		return false, nil, "", ""
	}
	return false, body, model, requested
}

// applyCustomModel 把客户端模型名改成真实目标模型,并按 Mode 注入提示词。
//
// append(默认):并入第一条 system/developer 消息末尾;
// prepend:拼在第一条 user 消息正文前;
// replace:用提示词替换 system 消息。
func applyCustomModel(body []byte, cm extstore.CustomModel) ([]byte, error) {
	var obj map[string]any
	if err := json.Unmarshal(body, &obj); err != nil {
		return nil, err
	}
	obj["model"] = cm.Target
	if cm.MaxTokens != nil {
		delete(obj, "max_completion_tokens")
		obj["max_tokens"] = *cm.MaxTokens
	}
	if cm.Temperature != nil {
		obj["temperature"] = *cm.Temperature
	}
	prompt := renderCustomPrompt(cm.Prompt, cm)
	if strings.TrimSpace(prompt) != "" {
		switch cm.Mode {
		case "prepend":
			injectUserPrefix(obj, prompt)
		case "replace":
			replaceSystemPrompt(obj, prompt)
		default:
			injectSystemPrompt(obj, prompt)
		}
	}
	return json.Marshal(obj)
}

// renderCustomPrompt 替换自定义提示词里的占位符。
//
// 支持:{{model}} 客户端请求名、{{target}} 目标模型、{{date}}、{{time}}、
// {{datetime}}。未知占位符原样保留,避免误伤用户自己的模板语法。
func renderCustomPrompt(prompt string, cm extstore.CustomModel) string {
	if prompt == "" {
		return ""
	}
	now := time.Now()
	replacer := strings.NewReplacer(
		"{{model}}", cm.Name,
		"{{target}}", cm.Target,
		"{{date}}", now.Format("2006-01-02"),
		"{{time}}", now.Format("15:04:05"),
		"{{datetime}}", now.Format("2006-01-02 15:04:05"),
	)
	return replacer.Replace(prompt)
}

// injectSystemPrompt 把提示词并入现有 system/developer 消息(没有则新建在最前)。
func injectSystemPrompt(body map[string]any, prompt string) {
	messages, _ := body["messages"].([]any)
	for i, raw := range messages {
		msg, _ := raw.(map[string]any)
		if msg == nil {
			continue
		}
		role, _ := msg["role"].(string)
		if role != "system" && role != "developer" {
			continue
		}
		switch content := msg["content"].(type) {
		case string:
			if strings.TrimSpace(content) == "" {
				msg["content"] = prompt
			} else {
				msg["content"] = content + "\n\n" + prompt
			}
		case []any:
			msg["content"] = append(content, map[string]any{"type": "text", "text": prompt})
		default:
			msg["content"] = prompt
		}
		messages[i] = msg
		body["messages"] = messages
		return
	}
	body["messages"] = append([]any{map[string]any{"role": "system", "content": prompt}}, messages...)
}

// replaceSystemPrompt 用提示词替换第一条 system/developer;没有则新建。
func replaceSystemPrompt(body map[string]any, prompt string) {
	messages, _ := body["messages"].([]any)
	for i, raw := range messages {
		msg, _ := raw.(map[string]any)
		if msg == nil {
			continue
		}
		role, _ := msg["role"].(string)
		if role == "system" || role == "developer" {
			msg["content"] = prompt
			messages[i] = msg
			body["messages"] = messages
			return
		}
	}
	body["messages"] = append([]any{map[string]any{"role": "system", "content": prompt}}, messages...)
}

// injectUserPrefix 把提示词拼在第一条 user 消息正文前。
func injectUserPrefix(body map[string]any, prompt string) {
	messages, _ := body["messages"].([]any)
	for i, raw := range messages {
		msg, _ := raw.(map[string]any)
		if msg == nil {
			continue
		}
		if role, _ := msg["role"].(string); role != "user" {
			continue
		}
		switch content := msg["content"].(type) {
		case string:
			msg["content"] = prompt + "\n\n" + content
		case []any:
			msg["content"] = append([]any{map[string]any{"type": "text", "text": prompt}}, content...)
		default:
			msg["content"] = prompt
		}
		messages[i] = msg
		body["messages"] = messages
		return
	}
	// 没有 user 消息时退回 system 注入,至少不丢提示词。
	injectSystemPrompt(body, prompt)
}

// extensionModelEntries /v1/models 里的扩展模型条目(免费层 + 自定义映射)。
func (h *Handler) extensionModelEntries(r *http.Request) []map[string]any {
	if h.cfg.Extensions == nil {
		return nil
	}
	snap := h.cfg.Extensions.Snapshot()
	out := make([]map[string]any, 0)
	if h.cfg.FreeModels != nil {
		for _, m := range h.cfg.FreeModels.Models(r.Context()) {
			entry := map[string]any{
				"id":                freemodel.FreeModelID(m.Provider, m.ID),
				"object":            "model",
				"created":           1753600000,
				"owned_by":          "free:" + m.Provider,
				"channel":           "free",
				"channel_label":     m.ProviderLabel,
				"provider":          m.Provider,
				"free":              true,
				"context_length":    131072,
				"max_output_tokens": 32768,
			}
			if m.Name != "" {
				entry["name"] = m.Name
			} else {
				entry["name"] = m.ID
			}
			out = append(out, entry)
		}
	}
	for _, cm := range snap.CustomModels {
		if !cm.Enabled {
			continue
		}
		entry := map[string]any{
			"id":                cm.Name,
			"object":            "model",
			"created":           1753600000,
			"owned_by":          "custom",
			"channel":           "custom",
			"channel_label":     "自定义映射",
			"base_model":        cm.Target,
			"description":       customDescription(cm),
			"context_length":    131072,
			"max_output_tokens": 32768,
		}
		out = append(out, entry)
	}
	return out
}

func customDescription(cm extstore.CustomModel) string {
	if strings.TrimSpace(cm.Description) != "" {
		return cm.Description
	}
	mode := cm.Mode
	if mode == "" {
		mode = "append"
	}
	return "自定义提示词映射(" + mode + " → " + cm.Target + ")"
}