Files
ds2api/internal/util/standard_request.go

87 lines
1.9 KiB
Go

package util
import "ds2api/internal/config"
type StandardRequest struct {
Surface string
RequestedModel string
ResolvedModel string
ResponseModel string
Messages []any
ToolsRaw any
FinalPrompt string
ToolNames []string
ToolChoice ToolChoicePolicy
Stream bool
Thinking bool
Search bool
RefFileIDs []string
PassThrough map[string]any
}
type ToolChoiceMode string
const (
ToolChoiceAuto ToolChoiceMode = "auto"
ToolChoiceNone ToolChoiceMode = "none"
ToolChoiceRequired ToolChoiceMode = "required"
ToolChoiceForced ToolChoiceMode = "forced"
)
type ToolChoicePolicy struct {
Mode ToolChoiceMode
ForcedName string
Allowed map[string]struct{}
}
func DefaultToolChoicePolicy() ToolChoicePolicy {
return ToolChoicePolicy{Mode: ToolChoiceAuto}
}
func (p ToolChoicePolicy) IsNone() bool {
return p.Mode == ToolChoiceNone
}
func (p ToolChoicePolicy) IsRequired() bool {
return p.Mode == ToolChoiceRequired || p.Mode == ToolChoiceForced
}
func (p ToolChoicePolicy) Allows(name string) bool {
if len(p.Allowed) == 0 {
return true
}
_, ok := p.Allowed[name]
return ok
}
func (r StandardRequest) CompletionPayload(sessionID string) map[string]any {
modelID := r.ResolvedModel
if modelID == "" {
modelID = r.RequestedModel
}
modelType := "default"
if resolvedType, ok := config.GetModelType(modelID); ok {
modelType = resolvedType
}
refFileIDs := make([]any, 0, len(r.RefFileIDs))
for _, fileID := range r.RefFileIDs {
if fileID == "" {
continue
}
refFileIDs = append(refFileIDs, fileID)
}
payload := map[string]any{
"chat_session_id": sessionID,
"model_type": modelType,
"parent_message_id": nil,
"prompt": r.FinalPrompt,
"ref_file_ids": refFileIDs,
"thinking_enabled": r.Thinking,
"search_enabled": r.Search,
}
for k, v := range r.PassThrough {
payload[k] = v
}
return payload
}