1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-12-02 20:22:52 +00:00

cleanup parsing and dump endpoint

This commit is contained in:
Laura
2025-10-25 15:09:35 +02:00
parent f7fc793d40
commit b5551ec012
2 changed files with 62 additions and 33 deletions

94
chat.go
View File

@@ -95,6 +95,33 @@ func (t *ToolCall) AsToolMessage() openrouter.ChatCompletionMessage {
} }
} }
func (r *Request) AddToolPrompt(request *openrouter.ChatCompletionRequest, iteration int64) bool {
if len(request.Tools) == 0 {
return false
}
if iteration == r.Iterations-1 {
debug("no more tool calls")
request.Tools = nil
request.ToolChoice = ""
}
// iterations - 1
total := r.Iterations - (iteration + 1)
var tools bytes.Buffer
InternalToolsTmpl.Execute(&tools, map[string]any{
"total": total,
"remaining": total - 1,
})
request.Messages = append(request.Messages, openrouter.SystemMessage(tools.String()))
return true
}
func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) { func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) {
var request openrouter.ChatCompletionRequest var request openrouter.ChatCompletionRequest
@@ -239,20 +266,27 @@ func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) {
return &request, nil return &request, nil
} }
func HandleChat(w http.ResponseWriter, r *http.Request) { func ParseChatRequest(r *http.Request) (*Request, *openrouter.ChatCompletionRequest, error) {
debug("parsing chat")
var raw Request var raw Request
if err := json.NewDecoder(r.Body).Decode(&raw); err != nil { if err := json.NewDecoder(r.Body).Decode(&raw); err != nil {
RespondJson(w, http.StatusBadRequest, map[string]any{ return nil, nil, err
"error": err.Error(),
})
return
} }
request, err := raw.Parse() request, err := raw.Parse()
if err != nil {
return nil, nil, err
}
request.Stream = true
return &raw, request, nil
}
func HandleDump(w http.ResponseWriter, r *http.Request) {
debug("parsing dump")
raw, request, err := ParseChatRequest(r)
if err != nil { if err != nil {
RespondJson(w, http.StatusBadRequest, map[string]any{ RespondJson(w, http.StatusBadRequest, map[string]any{
"error": err.Error(), "error": err.Error(),
@@ -261,7 +295,24 @@ func HandleChat(w http.ResponseWriter, r *http.Request) {
return return
} }
request.Stream = true raw.AddToolPrompt(request, 0)
RespondJson(w, http.StatusOK, map[string]any{
"request": request,
})
}
func HandleChat(w http.ResponseWriter, r *http.Request) {
debug("parsing chat")
raw, request, err := ParseChatRequest(r)
if err != nil {
RespondJson(w, http.StatusBadRequest, map[string]any{
"error": err.Error(),
})
return
}
debug("preparing stream") debug("preparing stream")
@@ -283,30 +334,7 @@ func HandleChat(w http.ResponseWriter, r *http.Request) {
response.WriteChunk(NewChunk(ChunkStart, nil)) response.WriteChunk(NewChunk(ChunkStart, nil))
var hasToolMessage bool hasToolMessage := raw.AddToolPrompt(request, iteration)
if len(request.Tools) > 0 {
if iteration == raw.Iterations-1 {
debug("no more tool calls")
request.Tools = nil
request.ToolChoice = ""
}
// iterations - 1
total := raw.Iterations - (iteration + 1)
var tools bytes.Buffer
InternalToolsTmpl.Execute(&tools, map[string]any{
"total": total,
"remaining": total - 1,
})
request.Messages = append(request.Messages, openrouter.SystemMessage(tools.String()))
hasToolMessage = true
}
dump("chat.json", request) dump("chat.json", request)

View File

@@ -60,6 +60,7 @@ func main() {
gr.Get("/-/stats/{id}", HandleStats) gr.Get("/-/stats/{id}", HandleStats)
gr.Post("/-/title", HandleTitle) gr.Post("/-/title", HandleTitle)
gr.Post("/-/chat", HandleChat) gr.Post("/-/chat", HandleChat)
gr.Post("/-/dump", HandleDump)
gr.Post("/-/tokenize", HandleTokenize(tokenizer)) gr.Post("/-/tokenize", HandleTokenize(tokenizer))
}) })