File size: 10,770 Bytes
8d3471e | 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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | package promptcompat
import (
"fmt"
"strings"
"ds2api/internal/config"
"ds2api/internal/util"
)
type ConfigReader interface {
ModelAliases() map[string]string
}
func NormalizeOpenAIChatRequest(store ConfigReader, req map[string]any, traceID string) (StandardRequest, error) {
model, _ := req["model"].(string)
messagesRaw, _ := req["messages"].([]any)
if strings.TrimSpace(model) == "" || len(messagesRaw) == 0 {
return StandardRequest{}, fmt.Errorf("request must include 'model' and 'messages'")
}
resolvedModel, ok := config.ResolveModel(store, model)
if !ok {
return StandardRequest{}, fmt.Errorf("model %q is not available", model)
}
defaultThinkingEnabled, searchEnabled, _ := config.GetModelConfig(resolvedModel)
thinkingEnabled := util.ResolveThinkingEnabled(req, defaultThinkingEnabled)
if config.IsNoThinkingModel(resolvedModel) {
thinkingEnabled = false
}
responseModel := strings.TrimSpace(model)
if responseModel == "" {
responseModel = resolvedModel
}
toolPolicy := DefaultToolChoicePolicy()
finalPrompt, toolNames := BuildOpenAIPrompt(messagesRaw, req["tools"], traceID, toolPolicy, thinkingEnabled)
toolNames = ensureToolDetectionEnabled(toolNames, req["tools"])
passThrough := collectOpenAIChatPassThrough(req)
refFileIDs := CollectOpenAIRefFileIDs(req)
return StandardRequest{
Surface: "openai_chat",
RequestedModel: strings.TrimSpace(model),
ResolvedModel: resolvedModel,
ResponseModel: responseModel,
Messages: messagesRaw,
PromptTokenText: finalPrompt,
ToolsRaw: req["tools"],
FinalPrompt: finalPrompt,
ToolNames: toolNames,
ToolChoice: toolPolicy,
Stream: util.ToBool(req["stream"]),
Thinking: thinkingEnabled,
Search: searchEnabled,
RefFileIDs: refFileIDs,
RefFileTokens: estimateInlineFileTokens(req),
PassThrough: passThrough,
}, nil
}
func NormalizeOpenAIResponsesRequest(store ConfigReader, req map[string]any, traceID string) (StandardRequest, error) {
model, _ := req["model"].(string)
model = strings.TrimSpace(model)
if model == "" {
return StandardRequest{}, fmt.Errorf("request must include 'model'")
}
resolvedModel, ok := config.ResolveModel(store, model)
if !ok {
return StandardRequest{}, fmt.Errorf("model %q is not available", model)
}
defaultThinkingEnabled, searchEnabled, _ := config.GetModelConfig(resolvedModel)
thinkingEnabled := util.ResolveThinkingEnabled(req, defaultThinkingEnabled)
if config.IsNoThinkingModel(resolvedModel) {
thinkingEnabled = false
}
messagesRaw := ResponsesMessagesFromRequest(req)
if len(messagesRaw) == 0 {
return StandardRequest{}, fmt.Errorf("request must include 'input' or 'messages'")
}
toolPolicy, err := parseToolChoicePolicy(req["tool_choice"], req["tools"])
if err != nil {
return StandardRequest{}, err
}
finalPrompt, toolNames := BuildOpenAIPrompt(messagesRaw, req["tools"], traceID, toolPolicy, thinkingEnabled)
toolNames = ensureToolDetectionEnabled(toolNames, req["tools"])
if !toolPolicy.IsNone() {
toolPolicy.Allowed = namesToSet(toolNames)
}
passThrough := collectOpenAIChatPassThrough(req)
refFileIDs := CollectOpenAIRefFileIDs(req)
return StandardRequest{
Surface: "openai_responses",
RequestedModel: model,
ResolvedModel: resolvedModel,
ResponseModel: model,
Messages: messagesRaw,
PromptTokenText: finalPrompt,
ToolsRaw: req["tools"],
FinalPrompt: finalPrompt,
ToolNames: toolNames,
ToolChoice: toolPolicy,
Stream: util.ToBool(req["stream"]),
Thinking: thinkingEnabled,
Search: searchEnabled,
RefFileIDs: refFileIDs,
RefFileTokens: estimateInlineFileTokens(req),
PassThrough: passThrough,
}, nil
}
func ensureToolDetectionEnabled(toolNames []string, toolsRaw any) []string {
if len(toolNames) > 0 {
return toolNames
}
tools, _ := toolsRaw.([]any)
if len(tools) == 0 {
return toolNames
}
// Keep stream sieve/tool buffering enabled even when client tool schemas
// are malformed or lack explicit names; parsed tool payload names are no
// longer filtered by this list.
return []string{"__any_tool__"}
}
func collectOpenAIChatPassThrough(req map[string]any) map[string]any {
out := map[string]any{}
for _, k := range []string{
"temperature",
"top_p",
"max_tokens",
"max_completion_tokens",
"presence_penalty",
"frequency_penalty",
"stop",
} {
if v, ok := req[k]; ok {
out[k] = v
}
}
return out
}
func parseToolChoicePolicy(toolChoiceRaw any, toolsRaw any) (ToolChoicePolicy, error) {
policy := DefaultToolChoicePolicy()
declaredNames := extractDeclaredToolNames(toolsRaw)
declaredSet := namesToSet(declaredNames)
if len(declaredNames) > 0 {
policy.Allowed = declaredSet
}
if toolChoiceRaw == nil {
return policy, nil
}
switch v := toolChoiceRaw.(type) {
case string:
switch strings.ToLower(strings.TrimSpace(v)) {
case "", "auto":
policy.Mode = ToolChoiceAuto
case "none":
policy.Mode = ToolChoiceNone
policy.Allowed = nil
case "required":
policy.Mode = ToolChoiceRequired
default:
return ToolChoicePolicy{}, fmt.Errorf("unsupported tool_choice: %q", v)
}
case map[string]any:
allowedOverride, hasAllowedOverride, err := parseAllowedToolNames(v["allowed_tools"])
if err != nil {
return ToolChoicePolicy{}, err
}
if hasAllowedOverride {
filtered := make([]string, 0, len(allowedOverride))
for _, name := range allowedOverride {
if _, ok := declaredSet[name]; !ok {
return ToolChoicePolicy{}, fmt.Errorf("tool_choice.allowed_tools contains undeclared tool %q", name)
}
filtered = append(filtered, name)
}
policy.Allowed = namesToSet(filtered)
}
typ := strings.ToLower(strings.TrimSpace(asString(v["type"])))
switch typ {
case "", "auto":
if hasFunctionSelector(v) {
name, err := parseForcedToolName(v)
if err != nil {
return ToolChoicePolicy{}, err
}
policy.Mode = ToolChoiceForced
policy.ForcedName = name
policy.Allowed = namesToSet([]string{name})
} else {
policy.Mode = ToolChoiceAuto
}
case "none":
policy.Mode = ToolChoiceNone
policy.Allowed = nil
case "required":
policy.Mode = ToolChoiceRequired
case "function":
name, err := parseForcedToolName(v)
if err != nil {
return ToolChoicePolicy{}, err
}
policy.Mode = ToolChoiceForced
policy.ForcedName = name
policy.Allowed = namesToSet([]string{name})
default:
return ToolChoicePolicy{}, fmt.Errorf("unsupported tool_choice.type: %q", typ)
}
default:
return ToolChoicePolicy{}, fmt.Errorf("tool_choice must be a string or object")
}
if policy.Mode == ToolChoiceRequired || policy.Mode == ToolChoiceForced {
if len(declaredNames) == 0 {
return ToolChoicePolicy{}, fmt.Errorf("tool_choice=%s requires non-empty tools", policy.Mode)
}
}
if policy.Mode == ToolChoiceForced {
if _, ok := declaredSet[policy.ForcedName]; !ok {
return ToolChoicePolicy{}, fmt.Errorf("tool_choice forced function %q is not declared in tools", policy.ForcedName)
}
}
if len(policy.Allowed) == 0 && (policy.Mode == ToolChoiceRequired || policy.Mode == ToolChoiceForced) {
return ToolChoicePolicy{}, fmt.Errorf("tool_choice policy resolved to empty allowed tool set")
}
return policy, nil
}
func parseForcedToolName(v map[string]any) (string, error) {
if name := strings.TrimSpace(asString(v["name"])); name != "" {
return name, nil
}
if fn, ok := v["function"].(map[string]any); ok {
if name := strings.TrimSpace(asString(fn["name"])); name != "" {
return name, nil
}
}
return "", fmt.Errorf("tool_choice function requires name")
}
func parseAllowedToolNames(raw any) ([]string, bool, error) {
if raw == nil {
return nil, false, nil
}
collectName := func(v any) string {
if name := strings.TrimSpace(asString(v)); name != "" {
return name
}
if m, ok := v.(map[string]any); ok {
if name := strings.TrimSpace(asString(m["name"])); name != "" {
return name
}
if fn, ok := m["function"].(map[string]any); ok {
if name := strings.TrimSpace(asString(fn["name"])); name != "" {
return name
}
}
}
return ""
}
names := []string{}
switch x := raw.(type) {
case []any:
for _, item := range x {
name := collectName(item)
if name == "" {
return nil, true, fmt.Errorf("tool_choice.allowed_tools contains invalid item")
}
names = append(names, name)
}
case []string:
for _, item := range x {
name := strings.TrimSpace(item)
if name == "" {
return nil, true, fmt.Errorf("tool_choice.allowed_tools contains empty name")
}
names = append(names, name)
}
default:
return nil, true, fmt.Errorf("tool_choice.allowed_tools must be an array")
}
if len(names) == 0 {
return nil, true, fmt.Errorf("tool_choice.allowed_tools must not be empty")
}
return names, true, nil
}
func hasFunctionSelector(v map[string]any) bool {
if strings.TrimSpace(asString(v["name"])) != "" {
return true
}
if fn, ok := v["function"].(map[string]any); ok {
return strings.TrimSpace(asString(fn["name"])) != ""
}
return false
}
func extractDeclaredToolNames(toolsRaw any) []string {
tools, ok := toolsRaw.([]any)
if !ok || len(tools) == 0 {
return nil
}
out := make([]string, 0, len(tools))
seen := map[string]struct{}{}
for _, t := range tools {
tool, ok := t.(map[string]any)
if !ok {
continue
}
fn, _ := tool["function"].(map[string]any)
if len(fn) == 0 {
fn = tool
}
name := strings.TrimSpace(asString(fn["name"]))
if name == "" {
continue
}
if _, ok := seen[name]; ok {
continue
}
seen[name] = struct{}{}
out = append(out, name)
}
return out
}
func namesToSet(names []string) map[string]struct{} {
if len(names) == 0 {
return nil
}
out := make(map[string]struct{}, len(names))
for _, name := range names {
trimmed := strings.TrimSpace(name)
if trimmed == "" {
continue
}
out[trimmed] = struct{}{}
}
if len(out) == 0 {
return nil
}
return out
}
// estimateInlineFileTokens extracts the byte count stashed by PreprocessInlineFileInputs
// and converts it to a conservative token estimate. Inline files are typically images or
// documents that the upstream model will process; we use bytes/3 (rather than bytes/4)
// as a slightly pessimistic approximation so the returned context token count stays
// safely above the real value.
func estimateInlineFileTokens(req map[string]any) int {
raw, ok := req["_inline_file_bytes"]
if !ok {
return 0
}
var bytes int
switch v := raw.(type) {
case int:
bytes = v
case int64:
bytes = int(v)
case float64:
bytes = int(v)
default:
return 0
}
if bytes <= 0 {
return 0
}
return bytes / 3
}
|