Files
openchamber-android-native/internal/appui/previews.go
T

264 lines
7.0 KiB
Go
Raw Normal View History

package appui
import (
"context"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/egoist/asuka/colors"
"github.com/egoist/asuka/fonts"
"github.com/egoist/asuka/sdk/store"
"github.com/egoist/asuka/signal"
"github.com/egoist/asuka/ui"
"github.com/openchamber/openchamber-native/internal/ocapi"
)
var imagePathPattern = regexp.MustCompile(`(/[^\s"'` + "`" + `()\[\]{}<>]+\.(?:png|jpg|jpeg|gif|webp|bmp|avif))`)
var imageExtensions = map[string]bool{
".png": true, ".jpg": true, ".jpeg": true, ".gif": true,
".webp": true, ".bmp": true, ".avif": true,
}
// imageRef is one previewable image a tool call names: the Source is an
// absolute path or a data URL, the Label what its row shows.
type imageRef struct {
Source string
Label string
}
func isImagePath(path string) bool {
return imageExtensions[strings.ToLower(filepath.Ext(path))]
}
// toolImages lists the images a tool call names: its file input, file parts
// in its output (data URLs from a read, for example), and paths in its text
// output. Relative paths resolve against the session's directory.
func (ch *chat) toolImages(p ocapi.ContentPart) []imageRef {
seen := map[string]bool{}
out := make([]imageRef, 0, 2)
hasLabel := func(label string) bool {
for _, ref := range out {
if ref.Label == label {
return true
}
}
return false
}
add := func(source, label string) {
source = strings.TrimSpace(source)
if strings.HasPrefix(source, "file://") {
source = strings.TrimPrefix(source, "file://")
}
if source == "" || seen[source] {
return
}
if !strings.HasPrefix(source, "data:") {
if strings.Contains(source, "://") {
return
}
if !strings.HasPrefix(source, "/") {
if ch.dir == "" {
return
}
source = filepath.Join(ch.dir, source)
}
if !isImagePath(source) {
return
}
if label == "" {
label = filepath.Base(source)
}
}
if label == "" {
label = "图片"
}
seen[source] = true
out = append(out, imageRef{Source: source, Label: label})
}
addPath := func(path string) {
path = strings.TrimSpace(path)
if strings.HasPrefix(path, "file://") {
path = strings.TrimPrefix(path, "file://")
} else if strings.Contains(path, "://") {
return
}
if !strings.HasPrefix(path, "/") {
if ch.dir == "" {
return
}
path = filepath.Join(ch.dir, path)
}
if !isImagePath(path) || hasLabel(filepath.Base(path)) {
return
}
add(path, "")
}
attachments := 0
if p.State != nil {
// Server-projected tool attachments carry the image itself, so they
// come first: a read of an image needs no file request.
for _, group := range [][]ocapi.ContentPart{p.State.Attachments, p.State.Content} {
for _, c := range group {
switch c.Type {
case "file":
source := c.URL
if source == "" {
source = c.URI
}
if source != "" && strings.HasPrefix(c.MIME, "image/") {
attachments++
add(source, c.Filename)
}
case "text":
for _, m := range imagePathPattern.FindAllString(c.Text, -1) {
addPath(m)
}
}
}
}
// The attachment already is the image the call named.
if attachments == 0 {
for _, key := range []string{"filePath", "path", "file", "file_path", "sourcePath"} {
if v, ok := p.State.Input[key].(string); ok {
addPath(v)
}
}
}
}
return out
}
func (ch *chat) toolImageAccessor(part signal.Accessor[ocapi.ContentPart]) signal.Accessor[[]imageRef] {
return signal.Func(func() []imageRef { return ch.toolImages(part.Get()) })
}
// previewKey names a cached preview: a hash keeps data URLs out of the map.
func previewKey(source string) string {
sum := sha1.Sum([]byte(source))
return hex.EncodeToString(sum[:])
}
func (ch *chat) previewImage(ref imageRef) {
ch.previewSeq++
seq := ch.previewSeq
ch.previewTitle.Set(ref.Label)
ch.previewPath.Set("")
ch.previewLoading.Set(true)
ch.previewOpen.Set(true)
key := previewKey(ref.Source)
if local, ok := ch.previewCache[key]; ok {
ch.previewPath.Set(local)
ch.previewLoading.Set(false)
return
}
go func() {
data, err := ch.loadPreview(ref.Source)
if err != nil {
ui.Post(func() {
if seq != ch.previewSeq {
return
}
ch.previewLoading.Set(false)
ch.previewOpen.Set(false)
ui.Snackbar(ui.SnackbarProps{Message: "无法预览:" + friendlyError(err)})
})
return
}
dir := filepath.Join(store.Dir(), "previews")
_ = os.MkdirAll(dir, 0o700)
name := strings.Map(func(r rune) rune {
if r == '/' || r == '\\' || r == ':' || r < ' ' {
return '_'
}
return r
}, ref.Label)
local := filepath.Join(dir, fmt.Sprintf("%d-%s", time.Now().UnixNano(), name))
if err := os.WriteFile(local, data, 0o600); err != nil {
ui.Post(func() {
if seq != ch.previewSeq {
return
}
ch.previewLoading.Set(false)
ch.previewOpen.Set(false)
ui.Snackbar(ui.SnackbarProps{Message: "无法保存预览"})
})
return
}
ui.Post(func() {
if seq != ch.previewSeq {
return
}
ch.previewCache[key] = local
ch.previewPath.Set(local)
ch.previewLoading.Set(false)
})
}()
}
// loadPreview reads a data URL itself or asks the server for the file.
func (ch *chat) loadPreview(source string) ([]byte, error) {
if strings.HasPrefix(source, "data:") {
comma := strings.IndexByte(source, ',')
if comma < 0 {
return nil, fmt.Errorf("malformed data URL")
}
meta := source[len("data:"):comma]
if !strings.Contains(meta, ";base64") {
return nil, fmt.Errorf("unsupported data URL")
}
return base64.StdEncoding.DecodeString(source[comma+1:])
}
client := ch.client()
if client == nil {
return nil, fmt.Errorf("not connected")
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
data, _, err := client.RawFile(ctx, source, ch.dir)
return data, err
}
func (ch *chat) previewSheet() ui.Node {
return ui.Sheet(ui.SheetProps{
Open: ch.previewOpen,
Detents: ui.DetentLarge,
Grabber: true,
OnDismiss: func() { ch.previewOpen.Set(false) },
Content: func() ui.Node {
return ui.View(ui.ViewProps{Label: ui.Val("图片预览"), Style: ui.Style{Grow: 1}},
ui.SafeArea(ui.Style{Padding: ui.All(16), Gap: 10},
ui.Text(ch.previewTitle, ui.Style{Font: fonts.Headline, Lines: 2}),
ui.Show(ch.previewLoading,
func() ui.Node { return ui.Activity() },
func() ui.Node {
return ui.Image(ui.ImageProps{
Source: ch.previewPath,
Mode: ui.ImageFit,
Style: ui.Style{Width: ui.Pct(100), Height: ui.Pt(420), CornerRadius: 10},
})
}),
),
)
},
})
}
// imageLinkRow is one previewable image under a tool call.
func (ch *chat) imageLinkRow(ref imageRef) ui.Node {
return ui.View(ui.ViewProps{
OnPress: func() { ch.previewImage(ref) },
Label: ui.Val("预览 " + ref.Label),
Style: ui.Style{Padding: ui.Symmetric(3, 0)},
}, ui.Row(ui.Style{Gap: 6, Align: ui.AlignCenter},
ui.Symbol("photo", ui.Style{Font: fonts.Caption, Color: colors.Accent}),
ui.StaticText(ref.Label, ui.Style{Font: fonts.Caption, Color: colors.Accent, Lines: 1}),
))
}