144 lines
4.9 KiB
Go
144 lines
4.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"mime"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// shownFile is a resolved file inside the operator's home.
|
|
type shownFile struct {
|
|
path string
|
|
name string
|
|
size int64
|
|
mime string
|
|
}
|
|
|
|
func (f shownFile) item(kind, title string) protocol.ChatShownItem {
|
|
return protocol.ChatShownItem{Kind: kind, Title: title, Path: f.path, Name: f.name, Size: f.size, Mime: f.mime}
|
|
}
|
|
|
|
// resolveShownFile applies the /files endpoint's containment rules to a model
|
|
// supplied path: it must name an existing regular file whose real path (every
|
|
// symlink resolved) is inside the operator's home and not on a special
|
|
// filesystem. A relative path is taken from the chat's working directory.
|
|
func (s *chatToolService) resolveShownFile(ctx context.Context, sessionID, requested string) (shownFile, error) {
|
|
requested = strings.TrimSpace(requested)
|
|
if requested == "" {
|
|
return shownFile{}, errors.New("a path is required")
|
|
}
|
|
home, err := s.home()
|
|
if err != nil {
|
|
return shownFile{}, err
|
|
}
|
|
home = filepath.Clean(home)
|
|
realHome, err := filepath.EvalSymlinks(home)
|
|
if err != nil {
|
|
return shownFile{}, errors.New("the operator's home directory is not available")
|
|
}
|
|
if !filepath.IsAbs(requested) && !strings.HasPrefix(requested, "~") {
|
|
directory := s.chatSessionDirectory(ctx, sessionID)
|
|
if directory == "" {
|
|
return shownFile{}, errors.New("path " + requested + " is relative and this chat has no recorded working directory; pass an absolute path")
|
|
}
|
|
requested = filepath.Join(directory, requested)
|
|
}
|
|
target, err := expandHomePath(home, requested)
|
|
if err != nil {
|
|
return shownFile{}, err
|
|
}
|
|
_, realTarget, err := resolveInsideHome(realHome, target)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "outside the home directory") {
|
|
return shownFile{}, errors.New("path " + requested + " is outside the operator's home directory; only files inside home can be shown")
|
|
}
|
|
return shownFile{}, err
|
|
}
|
|
info, err := os.Stat(realTarget)
|
|
if err != nil {
|
|
return shownFile{}, describePathError(requested, err)
|
|
}
|
|
if info.IsDir() {
|
|
return shownFile{}, errors.New(requested + " is a directory; show one file")
|
|
}
|
|
if !info.Mode().IsRegular() || specialFilesystem(realTarget) {
|
|
return shownFile{}, errors.New(requested + " is not a regular file")
|
|
}
|
|
return shownFile{path: realTarget, name: filepath.Base(realTarget), size: info.Size(), mime: shownFileMime(realTarget)}, nil
|
|
}
|
|
|
|
// shownFileMime is the type the /files endpoint will serve: by extension, else
|
|
// sniffed from the first bytes.
|
|
func shownFileMime(path string) string {
|
|
if byExtension := mime.TypeByExtension(filepath.Ext(path)); byExtension != "" {
|
|
media, _, err := mime.ParseMediaType(byExtension)
|
|
if err == nil {
|
|
return media
|
|
}
|
|
}
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return "application/octet-stream"
|
|
}
|
|
defer file.Close()
|
|
var sample [512]byte
|
|
n, err := file.Read(sample[:])
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
return "application/octet-stream"
|
|
}
|
|
media, _, _ := mime.ParseMediaType(http.DetectContentType(sample[:n]))
|
|
if media == "" {
|
|
return "application/octet-stream"
|
|
}
|
|
return media
|
|
}
|
|
|
|
// shownImageURL accepts only an absolute https URL without credentials.
|
|
func shownImageURL(raw string) (string, error) {
|
|
if len(raw) > 2048 {
|
|
return "", errors.New("the image URL is longer than 2048 characters")
|
|
}
|
|
parsed, err := url.Parse(raw)
|
|
if err != nil || parsed.Host == "" {
|
|
return "", errors.New("the image URL is not a valid absolute URL")
|
|
}
|
|
if parsed.Scheme != "https" {
|
|
return "", errors.New("only https:// image URLs can be shown")
|
|
}
|
|
if parsed.User != nil {
|
|
return "", errors.New("the image URL must not carry credentials")
|
|
}
|
|
return parsed.String(), nil
|
|
}
|
|
|
|
// chatToolNotifier sends one model-requested phone notification for a chat and
|
|
// reports whether ntfy accepted it, or why not.
|
|
type chatToolNotifier func(ctx context.Context, sessionID, title, message string) (bool, string)
|
|
|
|
// publishToolNotification publishes through the operator's existing "system"
|
|
// ntfy destination (the one chat attention uses), honoring their ntfy switch.
|
|
// Tapping it opens the chat (pipelines://chat/<id>).
|
|
func (h *chatHub) publishToolNotification(ctx context.Context, sessionID, title, message string) (bool, string) {
|
|
if !chatAttentionConfigured() {
|
|
return false, "phone notifications (ntfy) are not configured on this Pipelines server"
|
|
}
|
|
if !chatNotificationSettings(ctx, h.settings).NtfyEnabled {
|
|
return false, "the operator has turned phone notifications off"
|
|
}
|
|
click, views := chatAttentionLinks(sessionID)
|
|
if _, err := chatAttentionPublish(ctx, "system", truncateUTF8(title, 256), truncateUTF8(message, 2000), "", nil, click, views...); err != nil {
|
|
log.Printf("chat tool notification session=%s: %v", sessionID, err)
|
|
return false, "the notification service refused it: " + err.Error()
|
|
}
|
|
return true, ""
|
|
}
|