feat: inline image previews, revert, and the official icon

- sent images keep a local copy and render as rounded thumbnails in the
  message, surviving later page reloads
- a prompt's menu can revert it and everything after it: confirm, then
  stage and commit the server-side revert
- the app icon is OpenChamber's own (packages/mobile/assets/icon-only.png)
- document a terminal assessment (native view vs Go ANSI vs xterm.js in
  a WebView) for the next decision

Bump version to 0.2.1 (build 15).
This commit is contained in:
blumia committed 2026-11-17 23:46:06 +08:00
1 parent 72119b73e8
commit dc3730eba7
9 files changed
+221 -22

No files matched your search

+46 -5
View File
@@ -7,10 +7,12 @@ import (
"os"
"path/filepath"
"strings"
"time"
"github.com/egoist/asuka/colors"
"github.com/egoist/asuka/fonts"
"github.com/egoist/asuka/sdk/device"
"github.com/egoist/asuka/sdk/store"
"github.com/egoist/asuka/signal"
"github.com/egoist/asuka/ui"
"github.com/openchamber/openchamber-native/internal/ocapi"
@@ -23,10 +25,11 @@ const maxAttachmentBytes = 10 << 20
// attachment is a picked file waiting to ride with the next prompt.
type attachment struct {
Name string
MIME string
DataURL string
Size int64
Name string
MIME string
DataURL string
Size int64
LocalPath string // a copy in the app's data dir, for inline preview
}
func (ch *chat) attachMenu() {
@@ -105,11 +108,15 @@ func (ch *chat) readAttachment(path, name, mimeType string) {
return
}
url := "data:" + mimeType + ";base64," + base64.StdEncoding.EncodeToString(data)
kept := ""
if strings.HasPrefix(mimeType, "image/") {
kept = keepImage(path, name)
}
ui.Post(func() {
list := ch.attachments.Peek()
next := make([]attachment, len(list), len(list)+1)
copy(next, list)
next = append(next, attachment{Name: name, MIME: mimeType, DataURL: url, Size: info.Size()})
next = append(next, attachment{Name: name, MIME: mimeType, DataURL: url, Size: info.Size(), LocalPath: kept})
ch.attachments.Set(next)
})
}()
@@ -172,6 +179,40 @@ func (ch *chat) optimisticFiles() []ocapi.FileAttachment {
return out
}
// keepImage copies a picked image into the app's data dir so the sent
// message can show it inline after a reload clears the caches copy.
func keepImage(source, name string) string {
dir := filepath.Join(store.Dir(), "attachments")
_ = os.MkdirAll(dir, 0o700)
dst := filepath.Join(dir, fmt.Sprintf("%d-%s", time.Now().UnixNano(), sanitizeName(name)))
if err := copyFile(source, dst); err != nil {
return ""
}
return dst
}
func sanitizeName(name string) string {
clean := strings.Map(func(r rune) rune {
switch r {
case '/', '\\', ':', '*', '?', '"', '<', '>', '|':
return '_'
}
return r
}, name)
if clean == "" {
return "image"
}
return clean
}
func copyFile(src, dst string) error {
data, err := os.ReadFile(src)
if err != nil {
return err
}
return os.WriteFile(dst, data, 0o600)
}
func humanSize(bytes int64) string {
switch {
case bytes >= 1<<20:
+58
View File
@@ -71,6 +71,7 @@ type chat struct {
attaching *signal.Signal[bool]
jumpToLatest *signal.Signal[bool]
lastScroll float32
imagePaths map[string][]string
pickerOpen *signal.Signal[bool]
pickerKind string
@@ -119,6 +120,7 @@ func (c *controller) openChat(s *ocapi.Session, nav *ui.Navigator) *chat {
attachments: signal.NewFunc([]attachment{}, nil),
attaching: signal.New(false),
jumpToLatest: signal.New(false),
imagePaths: map[string][]string{},
pickerOpen: signal.New(false),
pickerItems: signal.NewFunc([]pickerItem{}, nil),
pickerFilter: signal.New(""),
@@ -474,6 +476,11 @@ func (ch *chat) send() {
}
id := newMessageID()
sent := ch.optimisticFiles()
for _, a := range ch.attachments.Peek() {
if a.LocalPath != "" {
ch.imagePaths[id] = append(ch.imagePaths[id], a.LocalPath)
}
}
ch.draft.Set("")
ch.attachments.Set(nil)
ch.errText.Set("")
@@ -1187,6 +1194,15 @@ func (ch *chat) itemRow(it signal.Accessor[chatItem], _ signal.Accessor[int]) ui
ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(8, 12), CornerRadius: 16, Gap: 4,
MaxWidth: ui.Pct(80), Background: colors.Accent}},
ui.Text(ch.textOf(cur.MsgID), ui.Style{Color: colors.OnAccent}),
ui.For(ch.imagesFor(cur.MsgID), func(p string) string { return p },
func(p signal.Accessor[string], _ signal.Accessor[int]) ui.Node {
return ui.Image(ui.ImageProps{
Source: p,
Mode: ui.ImageFill,
Style: ui.Style{Width: ui.Pt(200), Height: ui.Pt(150),
CornerRadius: 10, Clip: true, MaxWidth: ui.Pct(100)},
})
}),
ui.Show(signal.Func(func() bool { return len(ch.byID.Get()[cur.MsgID].Files) > 0 }),
func() ui.Node {
return ui.Text(ch.filesText(cur.MsgID),
@@ -1333,12 +1349,50 @@ func (ch *chat) userSheet() ui.Node {
)
}),
sheetAction("复制消息", "doc.on.doc", act(func() { ch.copyUserMessage(id) })),
sheetAction("撤回此消息及之后", "", act(func() { ch.revertFrom(id) })),
sheetAction("从这里分叉", "", act(func() { ch.forkBefore(id) })),
)
},
})
}
// revertFrom stages and commits a revert at a prompt, dropping that
// message and everything after it.
func (ch *chat) revertFrom(id string) {
ui.Alert(ui.AlertProps{
Title: "撤回消息?",
Message: "这条消息及其之后的内容会从会话中删除,且无法恢复。",
Actions: []ui.Action{
{Title: ui.Val("取消"), Role: ui.ActionCancel},
{Title: ui.Val("撤回"), Role: ui.ActionDestructive,
OnPress: func() { ch.doRevert(id) }},
},
})
}
func (ch *chat) doRevert(id string) {
client := ch.client()
if client == nil {
return
}
ch.errText.Set("")
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
defer cancel()
if err := client.StageRevert(ctx, ch.id, ch.dir, id); err != nil {
ui.Post(func() { ch.errText.Set(friendlyError(err)) })
return
}
if err := client.CommitRevert(ctx, ch.id, ch.dir); err != nil {
ui.Post(func() { ch.errText.Set(friendlyError(err)) })
return
}
ui.Post(func() {
ch.reloadTail()
})
}()
}
// forkBefore copies the transcript before a message (a prompt) into a new
// session.
func (ch *chat) forkBefore(id string) {
@@ -1450,6 +1504,10 @@ func (ch *chat) toolRow(cur chatItem) ui.Node {
func (ch *chat) msg(id string) ocapi.Message { return ch.byID.Get()[id] }
func (ch *chat) imagesFor(id string) signal.Accessor[[]string] {
return signal.Func(func() []string { return ch.imagePaths[id] })
}
func (ch *chat) filesText(id string) signal.Accessor[string] {
return signal.Func(func() string {
m := ch.byID.Get()[id]
+51 -7
View File
@@ -22,6 +22,7 @@ type chatServer struct {
models chan map[string]any
created chan map[string]any
forked chan map[string]any
reverted chan map[string]any
events chan string
ready chan struct{}
readyOne sync.Once
@@ -38,13 +39,14 @@ func (cs *chatServer) push(ev string) {
func newChatServer(t *testing.T) *chatServer {
t.Helper()
cs := &chatServer{
prompts: make(chan map[string]any, 1),
replies: make(chan map[string]any, 1),
models: make(chan map[string]any, 1),
created: make(chan map[string]any, 1),
forked: make(chan map[string]any, 1),
events: make(chan string, 8),
ready: make(chan struct{}),
prompts: make(chan map[string]any, 1),
replies: make(chan map[string]any, 1),
models: make(chan map[string]any, 1),
created: make(chan map[string]any, 1),
forked: make(chan map[string]any, 1),
reverted: make(chan map[string]any, 2),
events: make(chan string, 8),
ready: make(chan struct{}),
}
mux := http.NewServeMux()
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
@@ -130,6 +132,22 @@ func newChatServer(t *testing.T) *chatServer {
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed",
"input":{"command":"echo hi"},"content":[{"type":"text","text":"tool output here"}]}}]}`)
})
mux.HandleFunc("/api/session/ses_1/revert/stage", func(w http.ResponseWriter, r *http.Request) {
var body map[string]any
_ = json.NewDecoder(r.Body).Decode(&body)
select {
case cs.reverted <- body:
default:
}
fmt.Fprint(w, `{"data":{"messageID":"msg_1"}}`)
})
mux.HandleFunc("/api/session/ses_1/revert/commit", func(w http.ResponseWriter, r *http.Request) {
select {
case cs.reverted <- map[string]any{"commit": true}:
default:
}
w.WriteHeader(http.StatusNoContent)
})
mux.HandleFunc("/api/session/ses_1/fork", func(w http.ResponseWriter, r *http.Request) {
var body map[string]any
_ = json.NewDecoder(r.Body).Decode(&body)
@@ -372,3 +390,29 @@ func TestToolResultLoadsOnExpand(t *testing.T) {
a.Tap("✓ shell echo hi ▸")
a.WaitFor("tool output here")
}
func TestRevertFromUserMessage(t *testing.T) {
srv := newChatServer(t)
a := uitest.Mount(t, func() ui.Node { return rootWithStore(&memStore{}) }, uitest.Platform(ui.PlatformAndroid))
connectViaForm(t, a, srv.URL)
a.WaitFor("First session")
a.Tap("First session")
a.WaitFor("What is this?")
a.Tap("消息操作")
a.Tap("撤回此消息及之后")
a.WaitFor("撤回消息?")
a.Tap("撤回")
stage := <-srv.reverted
if _, ok := stage["commit"]; ok {
t.Fatalf("unexpected first message: %v", stage)
}
if stage["messageID"] != "msg_1" {
t.Errorf("stage messageID = %v", stage["messageID"])
}
commit := <-srv.reverted
if _, ok := commit["commit"]; !ok {
t.Errorf("commit not sent: %v", commit)
}
}