1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-09-08 17:06:42 +00:00

13 Commits

Author SHA1 Message Date
Laura
87d33a8d1d much improved search 2025-08-31 00:25:03 +02:00
Laura
c7a2848d05 wip 2025-08-30 15:06:49 +02:00
Laura
118e88ab67 some small fixes 2025-08-30 15:05:32 +02:00
Laura
dc8ad8d408 dynamic edit height 2025-08-30 14:34:29 +02:00
Laura
7abfd965db fix title schema 2025-08-30 14:15:22 +02:00
Laura
fc0a34ee12 import/export title 2025-08-30 14:13:24 +02:00
be17a801f8 improvements 2025-08-30 00:25:48 +02:00
c7c3bff2d8 better logger 2025-08-29 22:55:41 +02:00
3d629c93c5 harden response streaming 2025-08-29 19:26:55 +02:00
58aa250abe cleanup file select 2025-08-29 00:10:11 +02:00
Laura
87ea9823d2 prompt tweaks 2025-08-28 20:17:21 +02:00
Laura
40f98b0fd6 fix collapse styling 2025-08-28 20:12:20 +02:00
Laura
413515340a fix border radius 2025-08-28 18:45:19 +02:00
17 changed files with 290 additions and 151 deletions

31
chat.go
View File

@@ -14,12 +14,13 @@ import (
)
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Args string `json:"args"`
Result string `json:"result,omitempty"`
Done bool `json:"done,omitempty"`
Cost float64 `json:"cost,omitempty"`
ID string `json:"id"`
Name string `json:"name"`
Args string `json:"args"`
Result string `json:"result,omitempty"`
Done bool `json:"done,omitempty"`
Invalid bool `json:"invalid,omitempty"`
Cost float64 `json:"cost,omitempty"`
}
type TextFile struct {
@@ -144,11 +145,14 @@ func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) {
request.Messages = append(request.Messages, openrouter.SystemMessage(prompt))
}
if model.Tools && r.Tools.Search && env.Tokens.Exa != "" {
if model.Tools && r.Tools.Search && env.Tokens.Exa != "" && r.Iterations > 1 {
request.Tools = GetSearchTools()
request.ToolChoice = "auto"
request.Messages = append(request.Messages, openrouter.SystemMessage(InternalToolsPrompt))
request.Messages = append(
request.Messages,
openrouter.SystemMessage(fmt.Sprintf(InternalToolsPrompt, r.Iterations-1)),
)
}
for _, message := range r.Messages {
@@ -256,7 +260,9 @@ func HandleChat(w http.ResponseWriter, r *http.Request) {
debug("preparing stream")
response, err := NewStream(w)
ctx := r.Context()
response, err := NewStream(w, ctx)
if err != nil {
RespondJson(w, http.StatusBadRequest, map[string]any{
"error": err.Error(),
@@ -267,12 +273,10 @@ func HandleChat(w http.ResponseWriter, r *http.Request) {
debug("handling request")
ctx := r.Context()
for iteration := range raw.Iterations {
debug("iteration %d of %d", iteration+1, raw.Iterations)
if iteration == raw.Iterations-1 {
if len(request.Tools) > 0 && iteration == raw.Iterations-1 {
debug("no more tool calls")
request.Tools = nil
@@ -323,7 +327,8 @@ func HandleChat(w http.ResponseWriter, r *http.Request) {
return
}
default:
return
tool.Invalid = true
tool.Result = "error: invalid tool call"
}
tool.Done = true

View File

@@ -19,7 +19,7 @@ func debug(format string, args ...any) {
return
}
log.Debugf(format+"\n", args...)
log.Printf(format+"\n", args...)
}
func debugIf(cond bool, format string, args ...any) {

16
env.go
View File

@@ -51,25 +51,25 @@ var env = Environment{
func init() {
file, err := os.OpenFile("config.yml", os.O_RDONLY, 0)
log.MustPanic(err)
log.MustFail(err)
defer file.Close()
err = yaml.NewDecoder(file).Decode(&env)
log.MustPanic(err)
log.MustFail(err)
log.MustPanic(env.Init())
log.MustFail(env.Init())
}
func (e *Environment) Init() error {
// print if debug is enabled
if e.Debug {
log.Warning("Debug mode enabled")
log.Warnln("Debug mode enabled")
}
// check if server secret is set
if e.Tokens.Secret == "" {
log.Warning("Missing tokens.secret, generating new...")
log.Warnln("Missing tokens.secret, generating new...")
key := make([]byte, 32)
@@ -85,7 +85,7 @@ func (e *Environment) Init() error {
return err
}
log.Info("Stored new tokens.secret")
log.Println("Stored new tokens.secret")
}
// check if openrouter token is set
@@ -95,12 +95,12 @@ func (e *Environment) Init() error {
// check if exa token is set
if e.Tokens.Exa == "" {
log.Warning("Missing token.exa, web search unavailable")
log.Warnln("Missing token.exa, web search unavailable")
}
// check if github token is set
if e.Tokens.GitHub == "" {
log.Warning("Missing token.github, limited api requests")
log.Warnln("Missing token.github, limited api requests")
}
// default title model

122
exa.go
View File

@@ -7,15 +7,17 @@ import (
"fmt"
"net/http"
"strings"
"time"
)
type ExaResult struct {
Title string `json:"title"`
URL string `json:"url"`
PublishedDate string `json:"publishedDate"`
Text string `json:"text"`
Summary string `json:"summary"`
Title string `json:"title"`
URL string `json:"url"`
PublishedDate string `json:"publishedDate,omitempty"`
SiteName string `json:"siteName,omitempty"`
Summary string `json:"summary,omitempty"`
Highlights []string `json:"highlights,omitempty"`
Text string `json:"text,omitempty"`
}
type ExaCost struct {
@@ -23,43 +25,20 @@ type ExaCost struct {
}
type ExaResults struct {
RequestID string `json:"requestId"`
Results []ExaResult `json:"results"`
Cost ExaCost `json:"costDollars"`
}
func (e *ExaResult) String() string {
var (
label string
text string
)
if e.Text != "" {
label = "Text"
text = e.Text
} else if e.Summary != "" {
label = "Summary"
text = e.Summary
}
return fmt.Sprintf(
"Title: %s \nURL: %s \nPublished Date: %s \n%s: %s",
e.Title,
e.URL,
e.PublishedDate,
label,
strings.TrimSpace(text),
)
RequestID string `json:"requestId"`
SearchType string `json:"resolvedSearchType"`
Results []ExaResult `json:"results"`
Cost ExaCost `json:"costDollars"`
}
func (e *ExaResults) String() string {
list := make([]string, len(e.Results))
var builder strings.Builder
for i, result := range e.Results {
list[i] = result.String()
}
json.NewEncoder(&builder).Encode(map[string]any{
"results": e.Results,
})
return strings.Join(list, "\n\n---\n\n")
return builder.String()
}
func NewExaRequest(ctx context.Context, path string, data any) (*http.Request, error) {
@@ -100,15 +79,62 @@ func RunExaRequest(req *http.Request) (*ExaResults, error) {
}
func ExaRunSearch(ctx context.Context, args SearchWebArguments) (*ExaResults, error) {
if args.NumResults <= 0 {
args.NumResults = 6
} else if args.NumResults < 3 {
args.NumResults = 3
} else if args.NumResults >= 12 {
args.NumResults = 12
}
data := map[string]any{
"query": args.Query,
"type": "auto",
"numResults": args.NumResults,
"contents": map[string]any{
"summary": map[string]any{
"query": "Summarize this page only with all information directly relevant to answering the user's question: include key facts, numbers, dates, names, definitions, steps, code or commands, and the page's stance or conclusion; omit fluff and unrelated sections.",
},
}
if len(args.Domains) > 0 {
data["includeDomains"] = args.Domains
}
contents := map[string]any{
"summary": map[string]any{},
"highlights": map[string]any{
"numSentences": 2,
"highlightsPerUrl": 3,
},
"livecrawl": "preferred",
}
switch args.Intent {
case "news":
data["category"] = "news"
data["numResults"] = max(8, args.NumResults)
data["startPublishedDate"] = daysAgo(30)
case "docs":
contents["subpages"] = 1
contents["subpageTarget"] = []string{"documentation", "changelog", "release notes"}
case "papers":
data["category"] = "research paper"
data["startPublishedDate"] = daysAgo(365 * 2)
case "code":
data["category"] = "github"
contents["subpages"] = 1
contents["subpageTarget"] = []string{"readme", "changelog", "code"}
case "deep_read":
contents["text"] = map[string]any{
"maxCharacters": 8000,
}
}
data["contents"] = contents
switch args.Recency {
case "month":
data["startPublishedDate"] = daysAgo(30)
case "year":
data["startPublishedDate"] = daysAgo(356)
}
req, err := NewExaRequest(ctx, "/search", data)
@@ -121,10 +147,16 @@ func ExaRunSearch(ctx context.Context, args SearchWebArguments) (*ExaResults, er
func ExaRunContents(ctx context.Context, args FetchContentsArguments) (*ExaResults, error) {
data := map[string]any{
"urls": args.URLs,
"urls": args.URLs,
"summary": map[string]any{},
"highlights": map[string]any{
"numSentences": 2,
"highlightsPerUrl": 3,
},
"text": map[string]any{
"maxCharacters": 8000,
},
"livecrawl": "preferred",
}
req, err := NewExaRequest(ctx, "/contents", data)
@@ -134,3 +166,7 @@ func ExaRunContents(ctx context.Context, args FetchContentsArguments) (*ExaResul
return RunExaRequest(req)
}
func daysAgo(days int) string {
return time.Now().Add(time.Duration(days) * 24 * time.Hour).Format(time.DateOnly)
}

View File

@@ -162,14 +162,14 @@ func RepoOverview(ctx context.Context, arguments GitHubRepositoryArguments) (str
readme, err := GitHubRepositoryReadmeJson(ctx, arguments.Owner, arguments.Repo, repository.DefaultBranch)
if err != nil {
log.Warningf("failed to get repository readme: %v\n", err)
log.Warnf("failed to get repository readme: %v\n", err)
return
}
markdown, err := readme.AsText()
if err != nil {
log.Warningf("failed to decode repository readme: %v\n", err)
log.Warnf("failed to decode repository readme: %v\n", err)
return
}
@@ -185,7 +185,7 @@ func RepoOverview(ctx context.Context, arguments GitHubRepositoryArguments) (str
contents, err := GitHubRepositoryContentsJson(ctx, arguments.Owner, arguments.Repo, repository.DefaultBranch)
if err != nil {
log.Warningf("failed to get repository contents: %v\n", err)
log.Warnf("failed to get repository contents: %v\n", err)
return
}

9
go.mod
View File

@@ -1,22 +1,21 @@
module chat
go 1.24.5
go 1.25.0
require (
github.com/coalaura/logger v1.5.1
github.com/go-chi/chi/v5 v5.2.2
github.com/coalaura/plain v0.2.0
github.com/go-chi/chi/v5 v5.2.3
github.com/goccy/go-yaml v1.18.0
github.com/revrost/go-openrouter v0.2.2
golang.org/x/crypto v0.41.0
)
require (
github.com/containerd/console v1.0.5 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/rs/zerolog v1.34.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/term v0.34.0 // indirect
)

15
go.sum
View File

@@ -1,5 +1,7 @@
github.com/coalaura/logger v1.5.1 h1:2no4UP1HYOKQBasAol7RP81V0emJ2sfJIIoKOtrATqM=
github.com/coalaura/logger v1.5.1/go.mod h1:npioUhSPFmjxOmLzYbl9X0G6sdZgvuMikTlmc6VitWo=
github.com/coalaura/plain v0.2.0 h1:naGiTT1nmZO78IGHOajm0wc/X4sqaG6g3CSR3Ha9f6w=
github.com/coalaura/plain v0.2.0/go.mod h1:HR/sQt288EMTF3aSEGKHwPmGYFU4FOrfarMUf6ifnLo=
github.com/containerd/console v1.0.5 h1:R0ymNeydRqH2DmakFNdmjR2k0t7UPuiOV/N/27/qqsc=
github.com/containerd/console v1.0.5/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -7,11 +9,11 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618=
github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
@@ -29,13 +31,10 @@ github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=

View File

@@ -1 +1,13 @@
You have access to web search tools. Use `search_web` with `query` (string) and `num_results` (1-10) to find current information - when searching for recent/latest information, always include specific dates or years (e.g., "august 2025"). Use `fetch_contents` with `urls` (array) to read full page content from search results or known URLs. Use `github_repository` with `owner` (string) and `repo` (string) to get repository overviews (info, branches, files, README) without cloning. Formulate specific, targeted queries and provide all required parameters. Call only one tool per response.
# Tool use
Use at most 1 tool call per turn. You have %d turns with tool calls total.
search_web({query, num_results?, intent?, recency?, domains?})
- Fresh info & citations. Keep query short; add month/year if freshness matters.
- intent: auto|news|docs|papers|code|deep_read (deep_read may include full text).
- num_results: default 6 (3-12); recency: auto|month|year.
fetch_contents({urls})
- Read 1-5 given URLs for exact content/quotes/numbers.
github_repository({owner,repo})
- Quick repo overview + README excerpt.

17
main.go
View File

@@ -6,30 +6,27 @@ import (
"path/filepath"
"strings"
"github.com/coalaura/logger"
adapter "github.com/coalaura/logger/http"
"github.com/coalaura/plain"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
var Version = "dev"
var log = logger.New().DetectTerminal().WithOptions(logger.Options{
NoLevel: true,
})
var log = plain.New(plain.WithDate(plain.RFC3339Local))
func main() {
icons, err := LoadIcons()
log.MustPanic(err)
log.MustFail(err)
models, err := LoadModels()
log.MustPanic(err)
log.MustFail(err)
log.Info("Preparing router...")
log.Println("Preparing router...")
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Use(adapter.Middleware(log))
r.Use(log.Middleware())
fs := http.FileServer(http.Dir("./static"))
r.Handle("/*", cache(http.StripPrefix("/", fs)))
@@ -56,7 +53,7 @@ func main() {
gr.Post("/-/chat", HandleChat)
})
log.Info("Listening at http://localhost:3443/")
log.Println("Listening at http://localhost:3443/")
http.ListenAndServe(":3443", r)
}

View File

@@ -23,7 +23,7 @@ type Model struct {
var ModelMap = make(map[string]*Model)
func LoadModels() ([]*Model, error) {
log.Info("Loading models...")
log.Println("Loading models...")
client := OpenRouterClient()
@@ -58,7 +58,7 @@ func LoadModels() ([]*Model, error) {
ModelMap[model.ID] = m
}
log.Infof("Loaded %d models\n", len(models))
log.Printf("Loaded %d models\n", len(models))
return models, nil
}

View File

@@ -47,7 +47,7 @@ func init() {
var err error
Prompts, err = LoadPrompts()
log.MustPanic(err)
log.MustFail(err)
}
func NewTemplate(name, text string) *template.Template {
@@ -59,7 +59,7 @@ func NewTemplate(name, text string) *template.Template {
func LoadPrompts() ([]Prompt, error) {
var prompts []Prompt
log.Info("Loading prompts...")
log.Println("Loading prompts...")
err := filepath.Walk("prompts", func(path string, info fs.FileInfo, err error) error {
if err != nil || info.IsDir() {
@@ -80,7 +80,7 @@ func LoadPrompts() ([]Prompt, error) {
index := bytes.Index(body, []byte("---"))
if index == -1 {
log.Warningf("Invalid prompt file: %q\n", path)
log.Warnf("Invalid prompt file: %q\n", path)
return nil
}
@@ -106,7 +106,7 @@ func LoadPrompts() ([]Prompt, error) {
return prompts[i].Name < prompts[j].Name
})
log.Infof("Loaded %d prompts\n", len(prompts))
log.Printf("Loaded %d prompts\n", len(prompts))
return prompts, nil
}

View File

@@ -19,7 +19,8 @@ Follow this systematic approach for all research tasks:
## Web Search Protocol
**When search tools are available:**
- Begin with: "Research Plan: I will search for [X], then [Y] to cross-reference findings"
- Use multiple search angles to ensure comprehensive coverage
- Use multiple search turns, focusing on one specific query per turn to ensure precision and avoid convoluted results
- Use multiple search angles across turns to ensure comprehensive coverage
- Prioritize authoritative sources (academic, official, established organizations)
- Cross-verify claims across independent sources
- Note when sources conflict and explain discrepancies

View File

@@ -11,8 +11,11 @@ import (
)
type SearchWebArguments struct {
Query string `json:"query"`
NumResults int `json:"num_results"`
Query string `json:"query"`
NumResults int `json:"num_results,omitempty"`
Intent string `json:"intent,omitempty"`
Recency string `json:"recency,omitempty"`
Domains []string `json:"domains,omitempty"`
}
type FetchContentsArguments struct {
@@ -30,40 +33,60 @@ func GetSearchTools() []openrouter.Tool {
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
Name: "search_web",
Description: "Search the web via Exa in auto mode. Returns up to 10 results with short summaries.",
Description: "Search the live web (via Exa /search) and return summaries, highlights, and optionally full text for the top results.",
Parameters: map[string]any{
"type": "object",
"required": []string{"query", "num_results"},
"required": []string{"query"},
"properties": map[string]any{
"query": map[string]any{
"type": "string",
"description": "A concise, specific search query in natural language.",
"description": "A concise, specific search query in natural language. Include month/year if recency matters (e.g., 'august 2025').",
},
"num_results": map[string]any{
"type": "integer",
"description": "Number of results to return (3-10). Default to 6.",
"description": "Number of results to return (3-12). Default is 6.",
"minimum": 3,
"maximum": 10,
},
"intent": map[string]any{
"type": "string",
"enum": []string{"auto", "news", "docs", "papers", "code", "deep_read"},
"description": "Search profile. Use 'news' for breaking topics, 'docs' for official docs/changelogs, 'papers' for research, 'code' for repos, 'deep_read' when you need exact quotes/numbers (adds full text). Default 'auto'.",
},
"recency": map[string]any{
"type": "string",
"enum": []string{"auto", "month", "year", "range"},
"description": "Time filter hint. 'month' ~ last 30 days, 'year' ~ last 365 days. Default 'auto'.",
},
"domains": map[string]any{
"type": "array",
"items": map[string]any{
"type": "string",
},
"description": "Restrict to these domains (e.g., ['europa.eu', 'who.int']).",
},
},
"additionalProperties": false,
},
Strict: true,
},
},
{
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
Name: "fetch_contents",
Description: "Fetch page contents for one or more URLs via Exa /contents.",
Description: "Fetch and summarize page contents for one or more URLs (via Exa /contents). Use when the user provides specific links.",
Parameters: map[string]any{
"type": "object",
"required": []string{"urls"},
"properties": map[string]any{
"urls": map[string]any{
"type": "array",
"description": "List of URLs (1..N) to fetch.",
"items": map[string]any{"type": "string"},
"description": "List of URLs to fetch.",
"items": map[string]any{
"type": "string",
},
"minItems": 1,
"maxItems": 5,
},
},
"additionalProperties": false,
@@ -75,14 +98,14 @@ func GetSearchTools() []openrouter.Tool {
Type: openrouter.ToolTypeFunction,
Function: &openrouter.FunctionDefinition{
Name: "github_repository",
Description: "Get a quick overview of a GitHub repository without cloning: repo info, up to 20 branches (popular first), top-level files/dirs, and the README.",
Description: "Fetch repository metadata and README from GitHub.",
Parameters: map[string]any{
"type": "object",
"required": []string{"owner", "repo"},
"properties": map[string]any{
"owner": map[string]any{
"type": "string",
"description": "GitHub username or organization (e.g., 'torvalds').",
"description": "Repository owner (e.g., 'torvalds').",
},
"repo": map[string]any{
"type": "string",
@@ -100,7 +123,7 @@ func GetSearchTools() []openrouter.Tool {
func HandleSearchWebTool(ctx context.Context, tool *ToolCall) error {
var arguments SearchWebArguments
err := json.Unmarshal([]byte(tool.Args), &arguments)
err := ParseAndUpdateArgs(tool, &arguments)
if err != nil {
return err
}
@@ -132,7 +155,7 @@ func HandleSearchWebTool(ctx context.Context, tool *ToolCall) error {
func HandleFetchContentsTool(ctx context.Context, tool *ToolCall) error {
var arguments FetchContentsArguments
err := json.Unmarshal([]byte(tool.Args), &arguments)
err := ParseAndUpdateArgs(tool, &arguments)
if err != nil {
return err
}
@@ -164,7 +187,7 @@ func HandleFetchContentsTool(ctx context.Context, tool *ToolCall) error {
func HandleGitHubRepositoryTool(ctx context.Context, tool *ToolCall) error {
var arguments GitHubRepositoryArguments
err := json.Unmarshal([]byte(tool.Args), &arguments)
err := ParseAndUpdateArgs(tool, &arguments)
if err != nil {
return err
}
@@ -180,3 +203,19 @@ func HandleGitHubRepositoryTool(ctx context.Context, tool *ToolCall) error {
return nil
}
func ParseAndUpdateArgs(tool *ToolCall, arguments any) error {
err := json.Unmarshal([]byte(tool.Args), arguments)
if err != nil {
return err
}
b, err := json.Marshal(arguments)
if err != nil {
return err
}
tool.Args = string(b)
return nil
}

View File

@@ -368,6 +368,8 @@ body:not(.loading) #loading {
width: calc(800px - 24px);
}
.message .tool.invalid,
.message .tool .result.error,
.message .text .error {
color: #ed8796;
}
@@ -412,6 +414,10 @@ body:not(.loading) #loading {
height: 32px;
}
.message.collapsed .body>* {
opacity: 0;
}
.message.collapsed .body::before {
position: absolute;
content: "collapsed...";
@@ -708,6 +714,7 @@ body:not(.loading) #loading {
.files {
display: flex;
gap: 6px;
overflow-x: auto;
}
.files:not(.has-files) {
@@ -717,6 +724,7 @@ body:not(.loading) #loading {
.message .files {
background: #181926;
padding: 10px 12px;
border-radius: 2px;
}
.files .file {

View File

@@ -267,6 +267,10 @@
}
});
this.#_edit.addEventListener("input", () => {
this.updateEditHeight();
});
// message tool
this.#_tool = make("div", "tool");
@@ -381,7 +385,7 @@
mark(false);
generate(false);
generate(false, true);
});
// edit option
@@ -493,7 +497,7 @@
if (!only || only === "tool") {
if (this.#tool) {
const { name, args, result, cost } = this.#tool;
const { name, args, result, cost, invalid } = this.#tool;
const _name = this.#_tool.querySelector(".name"),
_arguments = this.#_tool.querySelector(".arguments"),
@@ -508,8 +512,11 @@
_cost.textContent = cost ? `${formatMoney(cost)}` : "";
_result.classList.toggle("error", result?.startsWith("error: "));
_result.innerHTML = render(result || "*processing*");
this.#_tool.classList.toggle("invalid", !!invalid);
this.#_tool.setAttribute("data-tool", name);
} else {
this.#_tool.removeAttribute("data-tool");
@@ -794,6 +801,11 @@
this.toggleEdit();
}
updateEditHeight() {
this.#_edit.style.height = "";
this.#_edit.style.height = `${Math.max(100, this.#_edit.scrollHeight + 2)}px`;
}
toggleEdit() {
this.#editing = !this.#editing;
@@ -801,11 +813,10 @@
activeMessage = this;
this.#_edit.value = this.#text;
this.#_edit.style.height = "";
this.setState("editing");
this.#_edit.style.height = `${Math.max(100, this.#_edit.scrollHeight)}px`;
this.updateEditHeight();
this.#_edit.focus();
} else {
@@ -929,7 +940,7 @@
let chatController;
function generate(cancel = false) {
function generate(cancel = false, noPush = false) {
if (chatController) {
chatController.abort();
@@ -969,7 +980,9 @@
$reasoningTokens.classList.remove("invalid");
}
pushMessage();
if (!noPush) {
pushMessage();
}
chatController = new AbortController();
@@ -1549,7 +1562,7 @@
$upload.addEventListener("click", async () => {
const files = await selectFile(
// the ultimate list
".adoc,.bash,.bashrc,.bat,.c,.cc,.cfg,.cjs,.cmd,.conf,.cpp,.cs,.css,.csv,.cxx,.dockerfile,.dockerignore,.editorconfig,.env,.fish,.fs,.fsx,.gitattributes,.gitignore,.go,.gradle,.groovy,.h,.hh,.hpp,.htm,.html,.ini,.ipynb,.java,.jl,.js,.json,.jsonc,.jsx,.kt,.kts,.less,.log,.lua,.m,.makefile,.markdown,.md,.mjs,.mk,.mm,.php,.phtml,.pl,.pm,.profile,.properties,.ps1,.psql,.py,.pyw,.r,.rb,.rs,.rst,.sass,.scala,.scss,.sh,.sql,.svelte,.swift,.t,.toml,.ts,.tsv,.tsx,.txt,.vb,.vue,.xhtml,.xml,.xsd,.xsl,.xslt,.yaml,.yml,.zig,.zsh",
"text/*",
true,
file => {
if (!file.name) {
@@ -1558,10 +1571,10 @@
throw new Error("File name too long (max 512 characters)");
}
if (typeof file.content !== "string") {
throw new Error("File is not a text file");
} else if (!file.content) {
if (!file.content) {
throw new Error("File is empty");
} else if (file.content.includes("\0")) {
throw new Error("File is not a text file");
} else if (file.content.length > 4 * 1024 * 1024) {
throw new Error("File is too big (max 4MB)");
}
@@ -1596,6 +1609,7 @@
$export.addEventListener("click", () => {
const data = JSON.stringify({
title: chatTitle,
message: $message.value,
attachments: attachments,
role: $role.value,
@@ -1636,6 +1650,7 @@
clearMessages();
storeValue("title", data.title);
storeValue("message", data.message);
storeValue("attachments", data.attachments);
storeValue("role", data.role);

View File

@@ -1,9 +1,12 @@
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"sync"
"github.com/revrost/go-openrouter"
)
@@ -14,42 +17,31 @@ type Chunk struct {
}
type Stream struct {
wr http.ResponseWriter
fl http.Flusher
en *json.Encoder
wr http.ResponseWriter
ctx context.Context
}
func NewStream(w http.ResponseWriter) (*Stream, error) {
flusher, ok := w.(http.Flusher)
if !ok {
return nil, errors.New("failed to create flusher")
}
var pool = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
func NewStream(w http.ResponseWriter, ctx context.Context) (*Stream, error) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
return &Stream{
wr: w,
fl: flusher,
en: json.NewEncoder(w),
wr: w,
ctx: ctx,
}, nil
}
func (s *Stream) Send(ch Chunk) error {
debugIf(ch.Type == "error", "error: %v", ch.Text)
if err := s.en.Encode(ch); err != nil {
return err
}
if _, err := s.wr.Write([]byte("\n\n")); err != nil {
return err
}
s.fl.Flush()
return nil
return WriteChunk(s.wr, s.ctx, ch)
}
func ReasoningChunk(text string) Chunk {
@@ -94,3 +86,39 @@ func GetErrorMessage(err error) string {
return err.Error()
}
func WriteChunk(w http.ResponseWriter, ctx context.Context, chunk any) error {
if err := ctx.Err(); err != nil {
return err
}
buf := pool.Get().(*bytes.Buffer)
buf.Reset()
defer pool.Put(buf)
if err := json.NewEncoder(buf).Encode(chunk); err != nil {
return err
}
buf.Write([]byte("\n\n"))
if _, err := w.Write(buf.Bytes()); err != nil {
return err
}
flusher, ok := w.(http.Flusher)
if !ok {
return errors.New("failed to create flusher")
}
select {
case <-ctx.Done():
return ctx.Err()
default:
flusher.Flush()
return nil
}
}

View File

@@ -16,8 +16,7 @@ type TitleRequest struct {
}
type TitleResponse struct {
Title string `json:"title"`
Cost float64 `json:"cost,omitempty"`
Title string `json:"title"`
}
var (
@@ -146,7 +145,8 @@ func HandleTitle(w http.ResponseWriter, r *http.Request) {
return
}
result.Cost = cost
RespondJson(w, http.StatusOK, result)
RespondJson(w, http.StatusOK, map[string]any{
"title": result.Title,
"cost": cost,
})
}