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

21 Commits

Author SHA1 Message Date
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
Laura
225cf59b4e improvements 2025-08-28 16:37:48 +02:00
Laura
f14faa11f2 show tool call cost 2025-08-28 15:00:02 +02:00
Laura
98c6976dfa include exa cost 2025-08-28 14:46:28 +02:00
Laura
b331920634 cleanup 2025-08-28 14:36:52 +02:00
ca5693b08a fix 2025-08-28 01:24:42 +02:00
Laura
26ad8698b7 show retry btn 2025-08-27 17:24:57 +02:00
Laura
5dbb0b0815 small fix 2025-08-27 17:24:08 +02:00
Laura
5479286595 fix 2025-08-27 13:04:55 +02:00
36cc50e90b notes 2025-08-26 03:04:54 +02:00
7d48984703 notes 2025-08-26 02:29:21 +02:00
27 changed files with 455 additions and 180 deletions

View File

@@ -32,6 +32,7 @@ whiskr is a private, self-hosted web chat interface for interacting with AI mode
## TODO
- improved custom prompts
- settings
- auto-retry on edit
- ctrl+enter vs enter for sending

45
chat.go
View File

@@ -14,11 +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"`
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 {
@@ -38,14 +40,24 @@ type Reasoning struct {
Tokens int `json:"tokens"`
}
type Tools struct {
JSON bool `json:"json"`
Search bool `json:"search"`
}
type Metadata struct {
Timezone string `json:"timezone"`
Platform string `json:"platform"`
}
type Request struct {
Prompt string `json:"prompt"`
Model string `json:"model"`
Temperature float64 `json:"temperature"`
Iterations int64 `json:"iterations"`
JSON bool `json:"json"`
Search bool `json:"search"`
Tools Tools `json:"tools"`
Reasoning Reasoning `json:"reasoning"`
Metadata Metadata `json:"metadata"`
Messages []Message `json:"messages"`
}
@@ -118,13 +130,13 @@ func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) {
}
}
if model.JSON && r.JSON {
if model.JSON && r.Tools.JSON {
request.ResponseFormat = &openrouter.ChatCompletionResponseFormat{
Type: openrouter.ChatCompletionResponseFormatTypeJSONObject,
}
}
prompt, err := BuildPrompt(r.Prompt, model)
prompt, err := BuildPrompt(r.Prompt, r.Metadata, model)
if err != nil {
return nil, err
}
@@ -133,7 +145,7 @@ func (r *Request) Parse() (*openrouter.ChatCompletionRequest, error) {
request.Messages = append(request.Messages, openrouter.SystemMessage(prompt))
}
if model.Tools && r.Search && env.Tokens.Exa != "" {
if model.Tools && r.Tools.Search && env.Tokens.Exa != "" {
request.Tools = GetSearchTools()
request.ToolChoice = "auto"
@@ -245,7 +257,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(),
@@ -256,8 +270,6 @@ 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)
@@ -312,7 +324,8 @@ func HandleChat(w http.ResponseWriter, r *http.Request) {
return
}
default:
return
tool.Invalid = true
tool.Result = "error: invalid tool call"
}
tool.Done = true
@@ -409,6 +422,10 @@ func SplitImagePairs(text string) []openrouter.ChatMessagePart {
)
push := func(str, end int) {
if str > end {
return
}
rest := text[str:end]
if rest == "" {

View File

@@ -5,6 +5,7 @@ import "strings"
var cleaner = strings.NewReplacer(
"", "-",
"—", "-",
"", "-",
"“", "\"",
"”", "\"",

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

5
exa.go
View File

@@ -18,9 +18,14 @@ type ExaResult struct {
Summary string `json:"summary"`
}
type ExaCost struct {
Total float64 `json:"total"`
}
type ExaResults struct {
RequestID string `json:"requestId"`
Results []ExaResult `json:"results"`
Cost ExaCost `json:"costDollars"`
}
func (e *ExaResult) String() string {

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=

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

@@ -15,9 +15,10 @@ import (
)
type PromptData struct {
Name string
Slug string
Date string
Name string
Slug string
Date string
Platform string
}
type Prompt struct {
@@ -46,7 +47,7 @@ func init() {
var err error
Prompts, err = LoadPrompts()
log.MustPanic(err)
log.MustFail(err)
}
func NewTemplate(name, text string) *template.Template {
@@ -58,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() {
@@ -79,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
}
@@ -87,7 +88,7 @@ func LoadPrompts() ([]Prompt, error) {
prompt := Prompt{
Key: strings.Replace(filepath.Base(path), ".txt", "", 1),
Name: strings.TrimSpace(string(body[:index])),
Text: strings.TrimSpace(string(body[:index+3])),
Text: strings.TrimSpace(string(body[index+3:])),
}
prompts = append(prompts, prompt)
@@ -105,12 +106,12 @@ 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
}
func BuildPrompt(name string, model *Model) (string, error) {
func BuildPrompt(name string, metadata Metadata, model *Model) (string, error) {
if name == "" {
return "", nil
}
@@ -120,12 +121,26 @@ func BuildPrompt(name string, model *Model) (string, error) {
return "", fmt.Errorf("unknown prompt: %q", name)
}
tz := time.UTC
if metadata.Timezone != "" {
parsed, err := time.LoadLocation(metadata.Timezone)
if err == nil {
tz = parsed
}
}
if metadata.Platform == "" {
metadata.Platform = "Unknown"
}
var buf bytes.Buffer
err := tmpl.Execute(&buf, PromptData{
Name: model.Name,
Slug: model.ID,
Date: time.Now().Format(time.RFC1123),
Name: model.Name,
Slug: model.ID,
Date: time.Now().In(tz).Format(time.RFC1123),
Platform: metadata.Platform,
})
if err != nil {

View File

@@ -1,6 +1,6 @@
Data Analyst
---
You are {{ .Name }} ({{ .Slug }}), an expert data analyst who transforms raw data into clear, actionable insights. Today is {{ .Date }}.
You are {{ .Name }} ({{ .Slug }}), an expert data analyst who transforms raw data into clear, actionable insights. Today is {{ .Date }} (in the user's timezone). The users platform is `{{ .Platform }}`.
## Role & Expertise
- **Primary Role**: Data analyst with expertise in statistical analysis, pattern recognition, and business intelligence

View File

@@ -1,6 +1,6 @@
Prompt Engineer
---
You are {{ .Name }} ({{ .Slug }}), an expert prompt engineering specialist who designs, optimizes, and troubleshoots prompts for maximum AI effectiveness. Today is {{ .Date }}.
You are {{ .Name }} ({{ .Slug }}), an expert prompt engineering specialist who designs, optimizes, and troubleshoots prompts for maximum AI effectiveness. Today is {{ .Date }} (in the user's timezone). The users platform is `{{ .Platform }}`.
## Role & Expertise
- **Primary Role**: Senior prompt engineer with deep knowledge of LLM behavior, cognitive architectures, and optimization techniques

View File

@@ -1,6 +1,6 @@
Assistant
---
You are {{ .Name }} ({{ .Slug }}), a versatile AI assistant designed to help users accomplish diverse tasks efficiently and accurately. Today is {{ .Date }}.
You are {{ .Name }} ({{ .Slug }}), a versatile AI assistant designed to help users accomplish diverse tasks efficiently and accurately. Today is {{ .Date }} (in the user's timezone). The users platform is `{{ .Platform }}`.
## Core Identity & Approach
- **Role**: General-purpose AI assistant with broad knowledge and problem-solving capabilities

View File

@@ -1,6 +1,6 @@
Physics Explainer
---
You are {{ .Name }} ({{ .Slug }}), a physics educator who makes complex concepts accessible without sacrificing accuracy. Today is {{ .Date }}.
You are {{ .Name }} ({{ .Slug }}), a physics educator who makes complex concepts accessible without sacrificing accuracy. Today is {{ .Date }} (in the user's timezone). The users platform is `{{ .Platform }}`.
## Role & Expertise
- **Primary Role**: Physics educator with deep conceptual understanding and exceptional communication skills

View File

@@ -1,6 +1,6 @@
Research Assistant
---
You are {{ .Name }} ({{ .Slug }}), a methodical AI research specialist who conducts systematic information gathering and synthesis to provide comprehensive, evidence-based answers. Today is {{ .Date }}.
You are {{ .Name }} ({{ .Slug }}), a methodical AI research specialist who conducts systematic information gathering and synthesis to provide comprehensive, evidence-based answers. Today is {{ .Date }} (in the user's timezone). The users platform is `{{ .Platform }}`.
## Role & Expertise
- **Primary Role**: Research methodologist skilled in systematic information gathering, source evaluation, and evidence synthesis
@@ -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

@@ -1,6 +1,6 @@
Code Reviewer
---
You are {{ .Name }} ({{ .Slug }}), an expert code security and quality analyst specializing in production-ready code assessment. Today is {{ .Date }}.
You are {{ .Name }} ({{ .Slug }}), an expert code security and quality analyst specializing in production-ready code assessment. Today is {{ .Date }} (in the user's timezone). The users platform is `{{ .Platform }}`.
## Role & Expertise
- **Primary Role**: Senior code reviewer with deep expertise in security vulnerabilities, performance optimization, and maintainable code practices

View File

@@ -1,6 +1,6 @@
Shell Scripter
---
You are {{ .Name }} ({{ .Slug }}), an expert automation engineer specializing in robust shell scripting and system automation. Today is {{ .Date }}.
You are {{ .Name }} ({{ .Slug }}), an expert automation engineer specializing in robust shell scripting and system automation. Today is {{ .Date }} (in the user's timezone). The users platform is `{{ .Platform }}`.
## Role & Expertise
- **Primary Role**: Senior DevOps engineer and automation specialist with deep expertise in Bash, PowerShell, and cross-platform scripting

View File

@@ -116,6 +116,8 @@ func HandleSearchWebTool(ctx context.Context, tool *ToolCall) error {
return nil
}
tool.Cost = results.Cost.Total
if len(results.Results) == 0 {
tool.Result = "error: no search results"
@@ -146,6 +148,8 @@ func HandleFetchContentsTool(ctx context.Context, tool *ToolCall) error {
return nil
}
tool.Cost = results.Cost.Total
if len(results.Results) == 0 {
tool.Result = "error: no search results"

View File

@@ -131,6 +131,7 @@ body:not(.loading) #loading {
position: absolute;
top: 15px;
right: 20px;
z-index: 45;
}
.notification {
@@ -347,36 +348,28 @@ body:not(.loading) #loading {
max-width: 800px;
}
.message .reasoning,
.message .tool,
.message .text {
display: block;
background: transparent;
padding: 10px 12px;
width: 100%;
}
.message .reasoning {
padding-top: 14px;
}
.message:not(.editing) textarea.text,
.message.editing div.text {
display: none;
}
.message .reasoning,
.message .tool,
.message div.text {
background: #24273a;
}
.message textarea.text {
display: block;
background: #181926;
min-width: 480px;
min-height: 100px;
max-width: 100%;
padding: 10px 12px;
width: calc(700px - 24px);
border-radius: 2px;
}
.message.assistant textarea.text {
width: calc(800px - 24px);
}
.message .tool.invalid,
.message .tool .result.error,
.message .text .error {
color: #ed8796;
}
@@ -394,21 +387,7 @@ body:not(.loading) #loading {
background: #1e2030;
border-radius: 6px;
padding: 10px 12px;
}
.message .reasoning-wrapper {
--height: auto;
height: calc(var(--height) + 20px);
overflow: hidden;
transition: 150ms;
}
.message:not(.expanded) .reasoning-wrapper {
height: 0;
}
.message.expanded .reasoning-text {
margin-top: 10px;
margin-top: 16px;
}
.message.has-reasoning:not(.has-text):not(.errored) div.text,
@@ -420,13 +399,34 @@ body:not(.loading) #loading {
}
.message .body {
position: relative;
border-bottom-left-radius: 6px;
border-bottom-right-radius: 6px;
overflow: hidden;
padding: 14px 12px;
display: flex;
flex-direction: column;
gap: 12px;
background: #24273a;
}
.message.has-reasoning .text {
padding-top: 4px;
.message.collapsed .body {
height: 32px;
}
.message.collapsed .body>* {
opacity: 0;
}
.message.collapsed .body::before {
position: absolute;
content: "collapsed...";
font-style: italic;
color: #939ab7;
font-size: 12px;
top: 50%;
left: 12px;
transform: translateY(-50%);
}
.tool .call,
@@ -446,7 +446,7 @@ body:not(.loading) #loading {
background-image: url(icons/reasoning.svg);
position: absolute;
top: -2px;
left: -2px;
left: 0px;
width: 20px;
height: 20px;
}
@@ -459,23 +459,25 @@ body:not(.loading) #loading {
transition: 150ms;
}
.message.expanded .reasoning .toggle::after {
transform: rotate(180deg);
}
.message.has-tool .text {
padding-bottom: 4px;
}
.message .reasoning,
.message .tool {
--height: 0px;
overflow: hidden;
transition: 150ms;
height: calc(90px + var(--height));
height: calc(40px + 16px + var(--height));
position: relative;
}
.message .reasoning {
height: calc(18px + 16px + var(--height));
}
.message .reasoning:not(.expanded) {
height: 18px;
}
.message .tool:not(.expanded) {
height: 62px;
height: 40px;
}
.tool .call {
@@ -505,8 +507,12 @@ body:not(.loading) #loading {
right: -22px;
}
.reasoning.expanded .toggle::after {
transform: scaleY(-100%);
}
.tool.expanded .call .name::after {
transform: translateY(-50%) rotate(180deg);
transform: translateY(-50%) scaleY(-100%);
}
.tool .call::before {
@@ -518,8 +524,19 @@ body:not(.loading) #loading {
width: max-content;
}
.message .tool .result {
margin-top: 16px;
.tool .cost {
position: absolute;
top: 2px;
right: 2px;
font-size: 12px;
font-style: italic;
color: #a5adcb;
transition: 150ms opacity;
opacity: 0;
}
.tool:hover .cost {
opacity: 1;
}
.message .options {
@@ -538,9 +555,31 @@ body:not(.loading) #loading {
pointer-events: all;
}
.message .collapse {
position: relative;
margin-right: 14px;
}
.message .collapse::before {
content: "";
transition: 150ms;
position: absolute;
top: 0;
left: 0;
}
.message.collapsed .collapse::before {
transform: scaleY(-100%);
}
.message .collapse::after {
position: absolute;
top: 4px;
right: -14px;
}
.message.errored .options .copy,
.message.errored .options .edit,
.message.errored .options .retry,
.message.waiting .options,
.message.reasoning .options,
.message.tooling .options,
@@ -675,6 +714,7 @@ body:not(.loading) #loading {
.files {
display: flex;
gap: 6px;
overflow-x: auto;
}
.files:not(.has-files) {
@@ -684,6 +724,7 @@ body:not(.loading) #loading {
.message .files {
background: #181926;
padding: 10px 12px;
border-radius: 2px;
}
.files .file {
@@ -780,6 +821,7 @@ select {
gap: 4px;
}
.message .options .collapse::after,
#chat .option+.option::before {
content: "";
display: block;
@@ -803,6 +845,8 @@ body.loading #version,
.message .role::before,
.message .tag-json,
.message .tag-search,
.message .collapse,
.message .collapse::before,
.message .copy,
.message .edit,
.message .retry,
@@ -850,6 +894,10 @@ input.invalid {
border: 1px solid #ed8796;
}
.message .collapse::before {
background-image: url(icons/collapse.svg);
}
.pre-copy,
.message .copy {
background-image: url(icons/copy.svg);

View File

@@ -0,0 +1,7 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Transformed by: SVG Repo Mixer Tools -->
<svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="SVGRepo_bgCarrier" stroke-width="0"/>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"/>

After

Width:  |  Height:  |  Size: 567 B

View File

@@ -72,6 +72,10 @@
text-decoration-color: rgba(183, 189, 248, 0.6);
}
.markdown p {
white-space: pre-wrap;
}
.markdown img {
max-width: 100%;
border-radius: 6px;

View File

@@ -34,6 +34,16 @@
$password = document.getElementById("password"),
$login = document.getElementById("login");
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
let platform = "";
detectPlatform().then(result => {
platform = result;
console.info(`Detected platform: ${platform}`);
});
const messages = [],
models = {},
modelList = [],
@@ -143,7 +153,6 @@
#error = false;
#editing = false;
#expanded = false;
#state = false;
#_diff;
@@ -159,7 +168,7 @@
#_tool;
#_statistics;
constructor(role, reasoning, text, files = []) {
constructor(role, reasoning, text, files = [], collapsed = false) {
this.#id = uid();
this.#role = role;
this.#reasoning = reasoning || "";
@@ -167,7 +176,7 @@
this.#_diff = document.createElement("div");
this.#build();
this.#build(collapsed);
this.#render();
for (const file of files) {
@@ -181,9 +190,9 @@
}
}
#build() {
#build(collapsed) {
// main message div
this.#_message = make("div", "message", this.#role);
this.#_message = make("div", "message", this.#role, collapsed ? "collapsed" : "");
// message role (wrapper)
const _wrapper = make("div", "role", this.#role);
@@ -224,26 +233,19 @@
_reasoning.appendChild(_toggle);
_toggle.addEventListener("click", () => {
this.#expanded = !this.#expanded;
_reasoning.classList.toggle("expanded");
this.#_message.classList.toggle("expanded", this.#expanded);
if (this.#expanded) {
if (_reasoning.classList.contains("expanded")) {
this.#updateReasoningHeight();
}
updateScrollButton();
});
// message reasoning (height wrapper)
const _height = make("div", "reasoning-wrapper");
_reasoning.appendChild(_height);
// message reasoning (content)
this.#_reasoning = make("div", "reasoning-text", "markdown");
_height.appendChild(this.#_reasoning);
_reasoning.appendChild(this.#_reasoning);
// message content
this.#_text = make("div", "text", "markdown");
@@ -265,6 +267,10 @@
}
});
this.#_edit.addEventListener("input", () => {
this.updateEditHeight();
});
// message tool
this.#_tool = make("div", "tool");
@@ -291,6 +297,13 @@
_call.appendChild(_callArguments);
// tool call cost
const _callCost = make("div", "cost");
_callCost.title = "Cost of this tool call";
this.#_tool.appendChild(_callCost);
// tool call result
const _callResult = make("div", "result", "markdown");
@@ -301,6 +314,19 @@
this.#_message.appendChild(_opts);
// collapse option
const _optCollapse = make("button", "collapse");
_optCollapse.title = "Collapse/Expand message";
_opts.appendChild(_optCollapse);
_optCollapse.addEventListener("click", () => {
this.#_message.classList.toggle("collapsed");
this.#save();
});
// copy option
const _optCopy = make("button", "copy");
@@ -359,7 +385,7 @@
mark(false);
generate(false);
generate(false, true);
});
// edit option
@@ -471,10 +497,11 @@
if (!only || only === "tool") {
if (this.#tool) {
const { name, args, result } = this.#tool;
const { name, args, result, cost, invalid } = this.#tool;
const _name = this.#_tool.querySelector(".name"),
_arguments = this.#_tool.querySelector(".arguments"),
_cost = this.#_tool.querySelector(".cost"),
_result = this.#_tool.querySelector(".result");
_name.title = `Show ${name} call result`;
@@ -483,8 +510,13 @@
_arguments.title = args;
_arguments.textContent = args;
_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");
@@ -615,6 +647,10 @@
data.statistics = this.#statistics;
}
if (this.#_message.classList.contains("collapsed") && full) {
data.collapsed = true;
}
if (!data.files?.length && !data.reasoning && !data.text && !data.tool) {
return false;
}
@@ -651,7 +687,7 @@
console.error(err);
if (!retrying && err.message.includes("not found")) {
setTimeout(this.loadGenerationData.bind(this), 750, generationID, true);
setTimeout(this.loadGenerationData.bind(this), 1500, generationID, true);
}
}
}
@@ -765,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;
@@ -773,11 +814,10 @@
this.#_edit.value = this.#text;
this.#_edit.style.height = `${this.#_text.offsetHeight}px`;
this.#_edit.style.width = `${this.#_text.offsetWidth}px`;
this.setState("editing");
this.updateEditHeight();
this.#_edit.focus();
} else {
activeMessage = null;
@@ -900,11 +940,13 @@
let chatController;
function generate(cancel = false) {
function generate(cancel = false, noPush = false) {
if (chatController) {
chatController.abort();
if (cancel) {
$chat.classList.remove("completing");
return;
}
}
@@ -938,7 +980,9 @@
$reasoningTokens.classList.remove("invalid");
}
pushMessage();
if (!noPush) {
pushMessage();
}
chatController = new AbortController();
@@ -949,12 +993,18 @@
model: $model.value,
temperature: temperature,
iterations: iterations,
tools: {
json: jsonMode,
search: searchTool,
},
reasoning: {
effort: effort,
tokens: tokens || 0,
},
json: jsonMode,
search: searchTool,
metadata: {
timezone: timezone,
platform: platform,
},
messages: messages.map(message => message.getData()).filter(Boolean),
};
@@ -968,7 +1018,7 @@
message.setState(false);
if (!aborted) {
setTimeout(message.loadGenerationData.bind(message), 750, generationID);
setTimeout(message.loadGenerationData.bind(message), 1000, generationID);
}
message = null;
@@ -1003,6 +1053,8 @@
},
chunk => {
if (chunk === "aborted") {
chatController = null;
finish(true);
return;
@@ -1038,6 +1090,8 @@
message.setTool(chunk.text);
if (chunk.text.done) {
totalCost += chunk.text.cost || 0;
finish();
}
@@ -1247,7 +1301,7 @@
}
loadValue("messages", []).forEach(message => {
const obj = new Message(message.role, message.reasoning, message.text, message.files || []);
const obj = new Message(message.role, message.reasoning, message.text, message.files || [], message.collapsed);
if (message.error) {
obj.showError(message.error);
@@ -1508,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) {
@@ -1517,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)");
}
@@ -1555,6 +1609,7 @@
$export.addEventListener("click", () => {
const data = JSON.stringify({
title: chatTitle,
message: $message.value,
attachments: attachments,
role: $role.value,
@@ -1595,6 +1650,7 @@
clearMessages();
storeValue("title", data.title);
storeValue("message", data.message);
storeValue("attachments", data.attachments);
storeValue("role", data.role);

View File

@@ -45,6 +45,8 @@ function uid() {
}
function make(tag, ...classes) {
classes = classes.filter(Boolean);
const el = document.createElement(tag);
if (classes.length) {
@@ -222,3 +224,94 @@ function selectFile(accept, multiple, handler, onError = false) {
input.click();
});
}
async function detectPlatform() {
let os, arch;
let platform = navigator.platform || "";
if (navigator.userAgentData?.getHighEntropyValues) {
try {
const data = await navigator.userAgentData.getHighEntropyValues(["platform", "architecture"]);
platform = data.platform;
arch = data.architecture;
} catch {}
}
const ua = navigator.userAgent || "";
// Windows
if (/Windows NT 10\.0/.test(ua)) os = "Windows 10/11";
else if (/Windows NT 6\.3/.test(ua)) os = "Windows 8.1";
else if (/Windows NT 6\.2/.test(ua)) os = "Windows 8";
else if (/Windows NT 6\.1/.test(ua)) os = "Windows 7";
else if (/Windows NT 6\.0/.test(ua)) os = "Windows Vista";
else if (/Windows NT 5\.1/.test(ua)) os = "Windows XP";
else if (/Windows NT 5\.0/.test(ua)) os = "Windows 2000";
else if (/Windows NT 4\.0/.test(ua)) os = "Windows NT 4.0";
else if (/Win(98|95|16)/.test(ua)) os = "Windows (legacy)";
else if (/Windows/.test(ua)) os = "Windows (unknown version)";
// Mac OS
else if (/Mac OS X/.test(ua)) {
os = "macOS";
const match = ua.match(/Mac OS X ([0-9_]+)/);
if (match) {
os += ` ${match[1].replace(/_/g, ".")}`;
} else {
os += " (unknown version)";
}
}
// Chrome OS
else if (/CrOS/.test(ua)) {
os = "Chrome OS";
const match = ua.match(/CrOS [^ ]+ ([0-9.]+)/);
if (match) {
os += ` ${match[1]}`;
}
}
// Linux (special)
else if (/FreeBSD/.test(ua)) os = "FreeBSD";
else if (/OpenBSD/.test(ua)) os = "OpenBSD";
else if (/NetBSD/.test(ua)) os = "NetBSD";
else if (/SunOS/.test(ua)) os = "Solaris";
// Linux (generic)
else if (/Linux/.test(ua)) {
if (/Ubuntu/i.test(ua)) os = "Ubuntu";
else if (/Debian/i.test(ua)) os = "Debian";
else if (/Fedora/i.test(ua)) os = "Fedora";
else if (/CentOS/i.test(ua)) os = "CentOS";
else if (/Red Hat/i.test(ua)) os = "Red Hat";
else if (/SUSE/i.test(ua)) os = "SUSE";
else if (/Gentoo/i.test(ua)) os = "Gentoo";
else if (/Arch/i.test(ua)) os = "Arch Linux";
else os = "Linux";
}
// Mobile
else if (/Android/.test(ua)) os = "Android";
else if (/iPhone|iPad|iPod/.test(ua)) os = "iOS";
// We still have no OS?
if (!os && platform) {
if (platform.includes("Win")) os = "Windows";
else if (/Mac/.test(platform)) os = "macOS";
else if (/Linux/.test(platform)) os = "Linux";
else os = platform;
}
// Detect architecture
if (!arch) {
if (/WOW64|Win64|x64|amd64/i.test(ua)) arch = "x64";
else if (/arm64|aarch64/i.test(ua)) arch = "arm64";
else if (/i[0-9]86|x86/i.test(ua)) arch = "x86";
else if (/ppc/i.test(ua)) arch = "ppc";
else if (/sparc/i.test(ua)) arch = "sparc";
else if (platform && /arm/i.test(platform)) arch = "arm";
}
return `${os || "Unknown OS"}${arch ? `, ${arch}` : ""}`;
}

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,
})
}