A plugin's page can now read one day's usage and share an image it
drew. This is for the day card (JungleyYeh on X asked for a "daily AI
statement"); the card itself is an optional community plugin,
@magpie-community/page-day-card.
- A page that asks for the "usage:day" read gets that day as
magpie.read("usage:day", {date, tz}): totals, each agent and its
models, the streak and the average active day (usageapi/day.go,
gui/pageday.go). The Plugins page names the read before it is granted.
- magpie.image.copy(png) and magpie.image.save(png, name) share a PNG
of at most 16 MB. In the window they go through
POST /api/plugin-pages/image (pageimage.go): copy puts the picture on
the clipboard (clipimage.go: osascript PNGf on macOS, PowerShell
PNG + Bitmap on Windows, wl-copy or xclip on Linux), and save writes
it to Downloads under a name made safe. In magpie web, the browser
saves or copies the picture itself.
- The window's side, pageshare.js, is imported the first time a page
shares a picture. The core grows by its one call only, paid for by
comments cut, so the budget is not raised.
Tests:
- New: TestDayOf, TestPluginPageReadsADay, TestPluginPageSharesAPicture,
TestPageImageName and TestClipImageCommand. plugin-page.test.cjs
covers the bridge, both engines and a refused SVG.
What was run (mote):
- gofmt, plus go vet on darwin, linux and windows: clean.
- Full go test ./... at the pre-rebase base: 369 packages ok, 3 FAILs.
- TestEveryRouteHasACommandOrIsNamed and TestCoreScriptBudget were
this change's own reds. The new route now has a parity entry, and
the share code moved into lazy pageshare.js with no raise.
- TestGatewayModeStatsNameTheGatewaySessions is #5690, the red in the
first hour of the day (it ran at 00:32).
- GUI suite (affected and plugin files, both engines): 1160 of 1179
passed. Every red is filed and none touches this change:
- i18n-keys "a plugin's words are its own": #5332, #5426, #5482.
- page-width-steady: #5013.
- plugin-card-update in WebKit: #3079.
- plugin-move-unserved "Try again": #4410.
- At this head, after the rebase on bc8b01d60:
- gofmt clean; go vet ./internal/... on darwin, linux (nogui) and
windows ok.
- internal/usageapi, internal/plugin and internal/cli ok.
- internal/gui: TestCoreScriptBudget, the TestPluginPage* tests and
the TestPageImage/TestClipImage tests pass.
- plugin-page and old-webkit (both engines): 12 of 12.
Real use:
- A sandbox HOME with a copy of real usage, through the real GUI
server, in Chromium and WebKit, light and dark, en/zh/zh-TW/ja/de, at
420 and 1100 px. The card showed 87.9M tokens, $9.85 and 4076 calls
with a 22-day streak.
- Picking an earlier day and WebKit's Copy ("Copied") worked, with no
page errors.
- Not tried at push: Chromium's Copy (its headless clipboard was
denied) and magpie web's Save as a captured download. A second real
run that grants both was still queued on mote's suite lock.
- Clipboard: checked live on macOS (osascript) and Windows
(PowerShell). Not tried live on Linux: WSLg had no display.
103 lines
3.5 KiB
Go
103 lines
3.5 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// copyImageOS puts a PNG on the system's clipboard with the system's own
|
|
// tool (clipImageCommand): Wails' clipboard holds text only.
|
|
func copyImageOS(ctx context.Context, b []byte) error {
|
|
f, err := os.CreateTemp("", "magpie-clip-*.png")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(f.Name())
|
|
if _, err := f.Write(b); err != nil {
|
|
f.Close()
|
|
return err
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
return err
|
|
}
|
|
name, args, env, err := clipImageCommand(runtime.GOOS, os.Getenv, exec.LookPath, f.Name())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cmd := proc.CommandContext(ctx, name, args...)
|
|
cmd.Env = append(os.Environ(), env...)
|
|
if runtime.GOOS == "linux" {
|
|
// xclip and wl-copy leave a process behind that holds the
|
|
// clipboard: one that inherited a pipe of ours would keep the
|
|
// read open until the next copy, so they get no output to write
|
|
if err := cmd.Run(); err != nil {
|
|
return fmt.Errorf("copying the picture: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
if s := strings.TrimSpace(string(out)); s != "" {
|
|
return fmt.Errorf("copying the picture: %s", firstLine(s))
|
|
}
|
|
return fmt.Errorf("copying the picture: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func firstLine(s string) string {
|
|
s, _, _ = strings.Cut(s, "\n")
|
|
return strings.TrimSpace(s)
|
|
}
|
|
|
|
// winClipScript puts the PNG at $env:MAGPIE_CLIP_PNG on Windows' clipboard
|
|
// both as "PNG", which browsers, chat apps and Office paste with its
|
|
// pixels as they are, and as a bitmap for the rest, kept once PowerShell
|
|
// ends (SetDataObject's copy).
|
|
const winClipScript = `$ErrorActionPreference = 'Stop'
|
|
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
|
|
$b = [System.IO.File]::ReadAllBytes($env:MAGPIE_CLIP_PNG)
|
|
$d = New-Object System.Windows.Forms.DataObject
|
|
$d.SetData('PNG', $false, (New-Object System.IO.MemoryStream(,$b)))
|
|
$d.SetImage([System.Drawing.Image]::FromStream((New-Object System.IO.MemoryStream(,$b))))
|
|
[System.Windows.Forms.Clipboard]::SetDataObject($d, $true)`
|
|
|
|
// errNoImageClipboard is a Linux with neither wl-copy nor xclip.
|
|
var errNoImageClipboard = errors.New("no tool to copy a picture with: install wl-clipboard (Wayland) or xclip (X11), or save it instead")
|
|
|
|
// clipImageCommand is the command that puts the PNG at file on goos's
|
|
// clipboard, and what it needs in its environment: osascript on macOS,
|
|
// PowerShell on Windows (as an STA thread, which the clipboard needs),
|
|
// and on Linux wl-copy in a Wayland session, else xclip.
|
|
func clipImageCommand(goos string, getenv func(string) string, look func(string) (string, error), file string) (name string, args, env []string, err error) {
|
|
switch goos {
|
|
case "darwin":
|
|
// the path is an argument, never a part of the script
|
|
return "osascript", []string{
|
|
"-e", "on run argv",
|
|
"-e", "set the clipboard to (read (POSIX file (item 1 of argv)) as «class PNGf»)",
|
|
"-e", "end run",
|
|
file,
|
|
}, nil, nil
|
|
case "windows":
|
|
return "powershell.exe", []string{"-NoProfile", "-NonInteractive", "-STA", "-Command", winClipScript}, []string{"MAGPIE_CLIP_PNG=" + file}, nil
|
|
}
|
|
if getenv("WAYLAND_DISPLAY") != "" {
|
|
if p, err := look("wl-copy"); err == nil {
|
|
// wl-copy reads the file from a shell's < : sh only opens it
|
|
return "sh", []string{"-c", `exec "$0" --type image/png < "$1"`, p, file}, nil, nil
|
|
}
|
|
}
|
|
if p, err := look("xclip"); err == nil {
|
|
return p, []string{"-selection", "clipboard", "-t", "image/png", "-i", file}, nil, nil
|
|
}
|
|
return "", nil, nil, errNoImageClipboard
|
|
}
|