From 30f2b6656e537c6f5958cd02e2815c6bd64fb166 Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 16 Aug 2025 13:53:55 +0200 Subject: [PATCH] some fixes --- chat.go | 54 ++++++++++++++++++++++++++------------------- search.go | 1 - static/css/chat.css | 2 +- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/chat.go b/chat.go index 5bf60aa..1f416b1 100644 --- a/chat.go +++ b/chat.go @@ -42,13 +42,26 @@ type Request struct { Messages []Message `json:"messages"` } -func (t *ToolCall) AsToolCall() openrouter.ToolCall { - return openrouter.ToolCall{ - ID: t.ID, - Type: openrouter.ToolTypeFunction, - Function: openrouter.FunctionCall{ - Name: t.Name, - Arguments: t.Args, +func (t *ToolCall) AsAssistantToolCall(content string) openrouter.ChatCompletionMessage { + // Some models require there to be content + if content == "" { + content = " " + } + + return openrouter.ChatCompletionMessage{ + Role: openrouter.ChatMessageRoleAssistant, + Content: openrouter.Content{ + Text: content, + }, + ToolCalls: []openrouter.ToolCall{ + { + ID: t.ID, + Type: openrouter.ToolTypeFunction, + Function: openrouter.FunctionCall{ + Name: t.Name, + Arguments: t.Args, + }, + }, }, } } @@ -100,13 +113,6 @@ func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) { } } - if model.Tools && r.Search && ExaToken != "" { - request.Tools = GetSearchTools() - request.ToolChoice = "auto" - - request.Messages = append(request.Messages, openrouter.SystemMessage("You have access to web search tools. Use `search_web` with `query` (string) and `num_results` (1-10) to find current information and get result summaries. Use `fetch_contents` with `urls` (array) to read full page content. Always specify all parameters for each tool call. Call only one tool per response.")) - } - prompt, err := BuildPrompt(r.Prompt, model) if err != nil { return nil, err @@ -116,6 +122,13 @@ func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) { request.Messages = append(request.Messages, openrouter.SystemMessage(prompt)) } + if model.Tools && r.Search && ExaToken != "" { + request.Tools = GetSearchTools() + request.ToolChoice = "auto" + + request.Messages = append(request.Messages, openrouter.SystemMessage("You have access to web search tools. Use `search_web` with `query` (string) and `num_results` (1-10) to find current information and get result summaries. Use `fetch_contents` with `urls` (array) to read full page content. Always specify all parameters for each tool call. Call only one tool per response.")) + } + for index, message := range r.Messages { switch message.Role { case "system": @@ -148,7 +161,7 @@ func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) { tool := message.Tool if tool != nil { - msg.ToolCalls = []openrouter.ToolCall{tool.AsToolCall()} + msg = tool.AsAssistantToolCall(message.Text) request.Messages = append(request.Messages, msg) @@ -188,7 +201,6 @@ func HandleChat(w http.ResponseWriter, r *http.Request) { request.Stream = true - dump("debug.json", request) debug("preparing stream") response, err := NewStream(w) @@ -216,6 +228,8 @@ func HandleChat(w http.ResponseWriter, r *http.Request) { request.Messages = append(request.Messages, openrouter.SystemMessage("You have reached the maximum number of tool calls for this conversation. Provide your final response based on the information you have gathered.")) } + dump("debug.json", request) + tool, message, err := RunCompletion(ctx, response, request) if err != nil { response.Send(ErrorChunk(err)) @@ -259,13 +273,7 @@ func HandleChat(w http.ResponseWriter, r *http.Request) { response.Send(ToolChunk(tool)) request.Messages = append(request.Messages, - openrouter.ChatCompletionMessage{ - Role: openrouter.ChatMessageRoleAssistant, - Content: openrouter.Content{ - Text: message, - }, - ToolCalls: []openrouter.ToolCall{tool.AsToolCall()}, - }, + tool.AsAssistantToolCall(message), tool.AsToolMessage(), ) } diff --git a/search.go b/search.go index e8169ca..3c1bd99 100644 --- a/search.go +++ b/search.go @@ -59,7 +59,6 @@ func GetSearchTools() []openrouter.Tool { "type": "array", "description": "List of URLs (1..N) to fetch.", "items": map[string]any{"type": "string"}, - "minItems": 1, }, }, "additionalProperties": false, diff --git a/static/css/chat.css b/static/css/chat.css index f60f32f..bbe9646 100644 --- a/static/css/chat.css +++ b/static/css/chat.css @@ -244,7 +244,7 @@ body.loading #version { .message textarea.text { background: #181926; - min-width: 100%; + min-width: 480px; min-height: 100px; }