Download internal/httpapi/openai/chat/handler.go from a3216/ds2api: direct link, hf CLI and curl.
- Browser
- Download file 3.62 kB
-
https://huggingface.co/spaces/a3216/ds2api/resolve/ce5a019598af8a3a8fa70d3880098df3e850ecd7/internal/httpapi/openai/chat/handler.go
- Command line
-
hf download hf://spaces/a3216/ds2api@ce5a019598af8a3a8fa70d3880098df3e850ecd7/internal/httpapi/openai/chat/handler.go
-
curl -L -o handler.go https://huggingface.co/spaces/a3216/ds2api/resolve/ce5a019598af8a3a8fa70d3880098df3e850ecd7/internal/httpapi/openai/chat/handler.go
3.62 kB
| package chat | |
| import ( | |
| "context" | |
| "net/http" | |
| "sync" | |
| "time" | |
| "ds2api/internal/auth" | |
| "ds2api/internal/chathistory" | |
| "ds2api/internal/httpapi/openai/files" | |
| "ds2api/internal/httpapi/openai/history" | |
| "ds2api/internal/httpapi/openai/shared" | |
| "ds2api/internal/promptcompat" | |
| "ds2api/internal/textclean" | |
| "ds2api/internal/toolcall" | |
| "ds2api/internal/toolstream" | |
| ) | |
| const openAIGeneralMaxSize = shared.GeneralMaxSize | |
| var writeJSON = shared.WriteJSON | |
| type Handler struct { | |
| Store shared.ConfigReader | |
| Auth shared.AuthResolver | |
| DS shared.DeepSeekCaller | |
| ChatHistory *chathistory.Store | |
| leaseMu sync.Mutex | |
| streamLeases map[string]streamLease | |
| } | |
| type streamLease struct { | |
| Auth *auth.RequestAuth | |
| Standard promptcompat.StandardRequest | |
| SessionID string | |
| ExpiresAt time.Time | |
| } | |
| func stripReferenceMarkersEnabled() bool { | |
| return textclean.StripReferenceMarkersEnabled() | |
| } | |
| func (h *Handler) applyCurrentInputFile(ctx context.Context, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) (promptcompat.StandardRequest, error) { | |
| if h == nil { | |
| return stdReq, nil | |
| } | |
| stdReq = shared.ApplyThinkingInjection(h.Store, stdReq) | |
| svc := history.Service{Store: h.Store, DS: h.DS} | |
| out, err := svc.ApplyCurrentInputFile(ctx, a, stdReq) | |
| if err != nil || out.CurrentInputFileApplied { | |
| return out, err | |
| } | |
| return out, nil | |
| } | |
| func (h *Handler) preprocessInlineFileInputs(ctx context.Context, a *auth.RequestAuth, req map[string]any) error { | |
| if h == nil { | |
| return nil | |
| } | |
| return (&files.Handler{Store: h.Store, Auth: h.Auth, DS: h.DS, ChatHistory: h.ChatHistory}).PreprocessInlineFileInputs(ctx, a, req) | |
| } | |
| func (h *Handler) toolcallFeatureMatchEnabled() bool { | |
| if h == nil { | |
| return shared.ToolcallFeatureMatchEnabled(nil) | |
| } | |
| return shared.ToolcallFeatureMatchEnabled(h.Store) | |
| } | |
| func (h *Handler) toolcallEarlyEmitHighConfidence() bool { | |
| if h == nil { | |
| return shared.ToolcallEarlyEmitHighConfidence(nil) | |
| } | |
| return shared.ToolcallEarlyEmitHighConfidence(h.Store) | |
| } | |
| func writeOpenAIError(w http.ResponseWriter, status int, message string) { | |
| shared.WriteOpenAIError(w, status, message) | |
| } | |
| func writeOpenAIErrorWithCode(w http.ResponseWriter, status int, message, code string) { | |
| shared.WriteOpenAIErrorWithCode(w, status, message, code) | |
| } | |
| func openAIErrorType(status int) string { | |
| return shared.OpenAIErrorType(status) | |
| } | |
| func writeOpenAIInlineFileError(w http.ResponseWriter, err error) { | |
| files.WriteInlineFileError(w, err) | |
| } | |
| func mapCurrentInputFileError(err error) (int, string) { | |
| return history.MapError(err) | |
| } | |
| func requestTraceID(r *http.Request) string { | |
| return shared.RequestTraceID(r) | |
| } | |
| func asString(v any) string { | |
| return shared.AsString(v) | |
| } | |
| func cleanVisibleOutput(text string, stripReferenceMarkers bool) string { | |
| return shared.CleanVisibleOutput(text, stripReferenceMarkers) | |
| } | |
| func emptyOutputRetryEnabled() bool { | |
| return shared.EmptyOutputRetryEnabled() | |
| } | |
| func emptyOutputRetryMaxAttempts() int { | |
| return shared.EmptyOutputRetryMaxAttempts() | |
| } | |
| func formatIncrementalStreamToolCallDeltas(deltas []toolstream.ToolCallDelta, ids map[int]string) []map[string]any { | |
| return shared.FormatIncrementalStreamToolCallDeltas(deltas, ids) | |
| } | |
| func filterIncrementalToolCallDeltasByAllowed(deltas []toolstream.ToolCallDelta, seenNames map[int]string) []toolstream.ToolCallDelta { | |
| return shared.FilterIncrementalToolCallDeltasByAllowed(deltas, seenNames) | |
| } | |
| func formatFinalStreamToolCallsWithStableIDs(calls []toolcall.ParsedToolCall, ids map[int]string, toolsRaw any) []map[string]any { | |
| return shared.FormatFinalStreamToolCallsWithStableIDs(calls, ids, toolsRaw) | |
| } | |