package server import ( "context" "encoding/json" "fmt" "strings" "unicode/utf8" "git.a-wan.net/alan/pipelines/internal/chattools" "git.a-wan.net/alan/pipelines/internal/protocol" ) // run executes one tool for sessionID. A non-nil item is recorded and shown; // the result is the short text the model reads. func (s *chatToolService) run(ctx context.Context, sessionID, tool string, arguments json.RawMessage) (*protocol.ChatShownItem, chattools.Result) { fail := func(format string, args ...any) (*protocol.ChatShownItem, chattools.Result) { return nil, chattools.Result{Text: fmt.Sprintf(format, args...), IsError: true} } var in struct { Path string `json:"path"` URLOrPath string `json:"url_or_path"` Title string `json:"title"` Alt string `json:"alt"` HTML string `json:"html"` HeightHint int `json:"height_hint"` Message string `json:"message"` Priority string `json:"priority"` } if err := json.Unmarshal(arguments, &in); err != nil { return fail("The arguments must be a JSON object matching the %s schema: %v", tool, err) } title := boundedLine(in.Title, 200) switch tool { case chattools.ToolShowFile: file, err := s.resolveShownFile(ctx, sessionID, in.Path) if err != nil { return fail("Nothing was shown: %v", err) } item := file.item(protocol.ChatShownFile, title) return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat: %s (%s).", file.name, humanBytes(file.size))} case chattools.ToolShowImage: source := strings.TrimSpace(in.URLOrPath) alt := boundedLine(in.Alt, 500) if strings.Contains(source, "://") { address, err := shownImageURL(source) if err != nil { return fail("Nothing was shown: %v", err) } item := protocol.ChatShownItem{Kind: protocol.ChatShownImage, Title: title, URL: address, Alt: alt} return &item, chattools.Result{Text: "Shown in the operator's Pipelines chat: the image at " + address + "."} } file, err := s.resolveShownFile(ctx, sessionID, source) if err != nil { return fail("Nothing was shown: %v", err) } if !strings.HasPrefix(file.mime, "image/") { return fail("Nothing was shown: %s is %s, not an image; use show_file for documents.", file.name, file.mime) } item := file.item(protocol.ChatShownImage, title) item.Alt = alt return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat: the image %s (%s).", file.name, humanBytes(file.size))} case chattools.ToolShowHTML: if strings.TrimSpace(in.HTML) == "" { return fail("Nothing was shown: html is empty.") } if len(in.HTML) > chattools.MaxHTMLBytes { return fail("Nothing was shown: the document is %s; the limit is 512 KiB. Simplify it (inline SVG, fewer data points) and call show_html again.", humanBytes(int64(len(in.HTML)))) } if !utf8.ValidString(in.HTML) { return fail("Nothing was shown: html is not valid UTF-8.") } item := protocol.ChatShownItem{Kind: protocol.ChatShownHTML, Title: title, HTML: in.HTML, HTMLBytes: len(in.HTML), HeightHint: clampHeightHint(in.HeightHint)} return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat (%s, sandboxed: scripts and network are disabled).", humanBytes(int64(len(in.HTML))))} case chattools.ToolNotify: return s.runNotify(ctx, sessionID, title, in.Message, in.Priority) } return fail("Unknown tool %s.", tool) } func (s *chatToolService) runNotify(ctx context.Context, sessionID, title, message, priority string) (*protocol.ChatShownItem, chattools.Result) { message = boundedLine(message, 2000) if title == "" || message == "" { return nil, chattools.Result{Text: "Not sent: title and message are both required.", IsError: true} } switch priority { case "": priority = "default" case "low", "default", "high": default: return nil, chattools.Result{Text: "Not sent: priority must be low, default or high.", IsError: true} } if !s.limiter.allow(sessionID, s.now()) { return nil, chattools.Result{Text: "Not sent: this chat already sent 5 notifications in the last minute. Wait before notifying again.", IsError: true} } delivered, detail := s.notify(ctx, sessionID, title, message) item := protocol.ChatShownItem{Kind: protocol.ChatShownNotify, Title: title, Message: message, Priority: priority, Delivered: delivered, Detail: detail} if !delivered { return &item, chattools.Result{Text: "Not delivered: " + detail + ".", IsError: true} } return &item, chattools.Result{Text: "Delivery accepted: the notification was handed to the operator's phone notification service (ntfy)."} } // boundedLine folds text to one line of at most limit bytes. func boundedLine(text string, limit int) string { return truncateUTF8(singleLine(text), limit) } func clampHeightHint(hint int) int { switch { case hint <= 0: return 0 case hint < 120: return 120 case hint > 600: return 600 } return hint } func humanBytes(n int64) string { switch { case n >= 1<<20: return fmt.Sprintf("%.1f MiB", float64(n)/(1<<20)) case n >= 1<<10: return fmt.Sprintf("%.1f KiB", float64(n)/(1<<10)) } return fmt.Sprintf("%d bytes", n) }