feat: search sessions, attach files, read aloud, jump to latest
Reach the first-party mobile client's everyday features: - sessions: a search button opens a field; typing asks the server with its search parameter (350 ms settle) and clearing restores the list - composer: attach photos or files (up to five, 10 MB each) as data URLs with the prompt; attachments show as removable chips and are named under the sent message - turn menu: read the answer aloud with the system voice, or stop it - chat: a "latest" button appears after scrolling up and returns to the end of the conversation - reset a live search when a connection ends Bump version to 0.2.0 (build 14).
This commit is contained in:
1 parent
4c6fcf4cbe
commit
72119b73e8
11 files changed
+445
-56
No files matched your search
+130
-44
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/egoist/asuka/colors"
|
||||
"github.com/egoist/asuka/fonts"
|
||||
"github.com/egoist/asuka/sdk/markdown"
|
||||
"github.com/egoist/asuka/sdk/speech"
|
||||
"github.com/egoist/asuka/sdk/svg"
|
||||
"github.com/egoist/asuka/signal"
|
||||
"github.com/egoist/asuka/ui"
|
||||
@@ -66,6 +67,11 @@ type chat struct {
|
||||
userOpen *signal.Signal[bool]
|
||||
userID *signal.Signal[string]
|
||||
|
||||
attachments *signal.Signal[[]attachment]
|
||||
attaching *signal.Signal[bool]
|
||||
jumpToLatest *signal.Signal[bool]
|
||||
lastScroll float32
|
||||
|
||||
pickerOpen *signal.Signal[bool]
|
||||
pickerKind string
|
||||
pickerItems *signal.Signal[[]pickerItem]
|
||||
@@ -110,6 +116,9 @@ func (c *controller) openChat(s *ocapi.Session, nav *ui.Navigator) *chat {
|
||||
footerID: signal.New(""),
|
||||
userOpen: signal.New(false),
|
||||
userID: signal.New(""),
|
||||
attachments: signal.NewFunc([]attachment{}, nil),
|
||||
attaching: signal.New(false),
|
||||
jumpToLatest: signal.New(false),
|
||||
pickerOpen: signal.New(false),
|
||||
pickerItems: signal.NewFunc([]pickerItem{}, nil),
|
||||
pickerFilter: signal.New(""),
|
||||
@@ -215,7 +224,20 @@ func formatTurnDuration(d time.Duration) string {
|
||||
return fmt.Sprintf("%dm %ds", total/60, total%60)
|
||||
}
|
||||
|
||||
func (ch *chat) copyAnswer(id string) {
|
||||
func (ch *chat) speakAnswer(id string) {
|
||||
text := ch.answerText(id)
|
||||
if strings.TrimSpace(text) == "" {
|
||||
ui.Snackbar(ui.SnackbarProps{Message: "没有可朗读的文字"})
|
||||
return
|
||||
}
|
||||
speech.Speak(text, speech.Options{}, func(err error) {
|
||||
if err != nil && err != speech.ErrStopped {
|
||||
ui.Snackbar(ui.SnackbarProps{Message: "朗读失败:" + err.Error()})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (ch *chat) answerText(id string) string {
|
||||
m := ch.byID.Peek()[id]
|
||||
var b strings.Builder
|
||||
for _, p := range m.Content {
|
||||
@@ -226,7 +248,11 @@ func (ch *chat) copyAnswer(id string) {
|
||||
b.WriteString(p.Text)
|
||||
}
|
||||
}
|
||||
text := strings.TrimSpace(b.String())
|
||||
return strings.TrimSpace(b.String())
|
||||
}
|
||||
|
||||
func (ch *chat) copyAnswer(id string) {
|
||||
text := ch.answerText(id)
|
||||
if text == "" {
|
||||
ui.Snackbar(ui.SnackbarProps{Message: "没有可复制的文字"})
|
||||
return
|
||||
@@ -269,6 +295,8 @@ func (ch *chat) footerSheet() ui.Node {
|
||||
ui.StaticText(stamp, ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
||||
)
|
||||
}),
|
||||
sheetAction("朗读回答", "speaker.wave.2", act(func() { ch.speakAnswer(id) })),
|
||||
sheetAction("停止朗读", "speaker.slash", act(func() { speech.Stop() })),
|
||||
sheetAction("复制回答", "doc.on.doc", act(func() { ch.copyAnswer(id) })),
|
||||
sheetAction("从这里分叉", "", act(func() { ch.forkFrom(id) })),
|
||||
)
|
||||
@@ -440,24 +468,28 @@ func (ch *chat) scheduleReload() {
|
||||
|
||||
func (ch *chat) send() {
|
||||
text := strings.TrimSpace(ch.draft.Peek())
|
||||
if text == "" || ch.client() == nil {
|
||||
files := ch.attachmentFiles()
|
||||
if (text == "" && len(files) == 0) || ch.client() == nil {
|
||||
return
|
||||
}
|
||||
id := newMessageID()
|
||||
sent := ch.optimisticFiles()
|
||||
ch.draft.Set("")
|
||||
ch.attachments.Set(nil)
|
||||
ch.errText.Set("")
|
||||
ch.running.Set(true)
|
||||
ch.addMessage(ocapi.Message{
|
||||
ID: id,
|
||||
Type: "user",
|
||||
Text: text,
|
||||
Time: ocapi.MessageTime{Created: time.Now().UnixMilli()},
|
||||
ID: id,
|
||||
Type: "user",
|
||||
Text: text,
|
||||
Files: sent,
|
||||
Time: ocapi.MessageTime{Created: time.Now().UnixMilli()},
|
||||
})
|
||||
client := ch.client()
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
|
||||
defer cancel()
|
||||
err := client.Prompt(ctx, ch.id, ocapi.PromptOptions{Directory: ch.dir, Text: text, MessageID: id})
|
||||
err := client.Prompt(ctx, ch.id, ocapi.PromptOptions{Directory: ch.dir, Text: text, MessageID: id, Files: files})
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
@@ -470,6 +502,7 @@ func (ch *chat) send() {
|
||||
ch.errText.Set(friendlyError(err))
|
||||
})
|
||||
}()
|
||||
ch.jumpToLatest.Set(false)
|
||||
ui.ScrollToEnd(ch.listNode, true)
|
||||
}
|
||||
|
||||
@@ -743,22 +776,44 @@ func (ch *chat) screen() ui.Node {
|
||||
Appearance: ui.ListBare,
|
||||
Anchor: ui.AnchorBottom,
|
||||
OnStartReached: ch.loadOlder,
|
||||
OnScroll: func(_, y float32) {
|
||||
if y < ch.lastScroll-60 {
|
||||
ch.jumpToLatest.Set(true)
|
||||
}
|
||||
ch.lastScroll = y
|
||||
},
|
||||
}, ch.items(), func(it chatItem) string { return it.Key }, ch.itemRow)
|
||||
|
||||
return ui.SafeArea(ui.Style{Grow: 1},
|
||||
ui.Column(ui.Style{Grow: 1},
|
||||
ui.Show(signal.Func(func() bool { return ch.errText.Get() != "" }), func() ui.Node {
|
||||
return ui.Row(ui.Style{Padding: ui.Symmetric(2, 12), Gap: 8, Align: ui.AlignCenter},
|
||||
ui.Text(ch.errText, ui.Style{Color: colors.Red, Font: fonts.Footnote, Grow: 1}),
|
||||
ui.Button(ui.ButtonProps{Title: ui.Val("重试"), OnPress: ch.load}),
|
||||
)
|
||||
ui.View(ui.ViewProps{Style: ui.Style{Grow: 1}},
|
||||
ui.Column(ui.Style{Grow: 1},
|
||||
ui.Show(signal.Func(func() bool { return ch.errText.Get() != "" }), func() ui.Node {
|
||||
return ui.Row(ui.Style{Padding: ui.Symmetric(2, 12), Gap: 8, Align: ui.AlignCenter},
|
||||
ui.Text(ch.errText, ui.Style{Color: colors.Red, Font: fonts.Footnote, Grow: 1}),
|
||||
ui.Button(ui.ButtonProps{Title: ui.Val("重试"), OnPress: ch.load}),
|
||||
)
|
||||
}),
|
||||
ui.Show(ch.loading, func() ui.Node {
|
||||
return ui.Progress(nil, ui.Style{Padding: ui.Symmetric(2, 16)})
|
||||
}),
|
||||
ch.listNode,
|
||||
ch.permissionSection(),
|
||||
ch.composer(),
|
||||
),
|
||||
ui.Show(ch.jumpToLatest, func() ui.Node {
|
||||
return ui.View(ui.ViewProps{
|
||||
OnPress: func() {
|
||||
ch.jumpToLatest.Set(false)
|
||||
ui.ScrollToEnd(ch.listNode, true)
|
||||
},
|
||||
Label: ui.Val("回到最新"),
|
||||
Style: ui.Style{Absolute: true, Right: ui.Pt(16), Bottom: ui.Pt(96),
|
||||
Padding: ui.Symmetric(8, 12), CornerRadius: 999, Background: colors.SecondaryBackground},
|
||||
}, ui.Row(ui.Style{Gap: 4, Align: ui.AlignCenter},
|
||||
ui.Symbol("arrow.down", ui.Style{Font: fonts.Caption, Color: colors.SecondaryLabel}),
|
||||
ui.StaticText("最新", ui.Style{Font: fonts.Caption, Color: colors.SecondaryLabel}),
|
||||
))
|
||||
}),
|
||||
ui.Show(ch.loading, func() ui.Node {
|
||||
return ui.Progress(nil, ui.Style{Padding: ui.Symmetric(2, 16)})
|
||||
}),
|
||||
ch.listNode,
|
||||
ch.permissionSection(),
|
||||
ch.composer(),
|
||||
),
|
||||
ch.footerSheet(),
|
||||
ch.userSheet(),
|
||||
@@ -1038,30 +1093,41 @@ func (ch *chat) reply(req ocapi.PermissionRequest, decision ocapi.PermissionDeci
|
||||
}
|
||||
|
||||
func (ch *chat) composer() ui.Node {
|
||||
return ui.Row(ui.Style{Gap: 8, Align: ui.AlignEnd, Padding: ui.Symmetric(8, 12)},
|
||||
ui.TextField(ui.TextFieldProps{
|
||||
Value: ch.draft,
|
||||
Placeholder: ui.Val("发消息…"),
|
||||
Multiline: true,
|
||||
Style: ui.Style{Grow: 1, Lines: 5},
|
||||
}),
|
||||
ui.Show(ch.running,
|
||||
func() ui.Node {
|
||||
return ui.Button(ui.ButtonProps{
|
||||
Title: ui.Val("停止"),
|
||||
Variant: ui.ButtonGray,
|
||||
OnPress: ch.stop,
|
||||
})
|
||||
},
|
||||
func() ui.Node {
|
||||
return ui.Button(ui.ButtonProps{
|
||||
Icon: ui.IconSend,
|
||||
Label: ui.Val("发送"),
|
||||
Variant: ui.ButtonFilled,
|
||||
Disabled: signal.Func(func() bool { return strings.TrimSpace(ch.draft.Get()) == "" }),
|
||||
OnPress: ch.send,
|
||||
})
|
||||
return ui.Column(ui.Style{Gap: 6, Padding: ui.Symmetric(8, 0)},
|
||||
ch.attachmentRow(),
|
||||
ui.Row(ui.Style{Gap: 8, Align: ui.AlignEnd, Padding: ui.Symmetric(0, 12)},
|
||||
ui.Show(ch.attaching,
|
||||
func() ui.Node { return ui.Activity() },
|
||||
func() ui.Node {
|
||||
return ui.Button(ui.ButtonProps{Icon: "paperclip", Label: ui.Val("添加附件"),
|
||||
OnPress: ch.attachMenu})
|
||||
}),
|
||||
ui.TextField(ui.TextFieldProps{
|
||||
Value: ch.draft,
|
||||
Placeholder: ui.Val("发消息…"),
|
||||
Multiline: true,
|
||||
Style: ui.Style{Grow: 1, Lines: 5},
|
||||
}),
|
||||
ui.Show(ch.running,
|
||||
func() ui.Node {
|
||||
return ui.Button(ui.ButtonProps{
|
||||
Title: ui.Val("停止"),
|
||||
Variant: ui.ButtonGray,
|
||||
OnPress: ch.stop,
|
||||
})
|
||||
},
|
||||
func() ui.Node {
|
||||
return ui.Button(ui.ButtonProps{
|
||||
Icon: ui.IconSend,
|
||||
Label: ui.Val("发送"),
|
||||
Variant: ui.ButtonFilled,
|
||||
Disabled: signal.Func(func() bool {
|
||||
return strings.TrimSpace(ch.draft.Get()) == "" && len(ch.attachments.Get()) == 0
|
||||
}),
|
||||
OnPress: ch.send,
|
||||
})
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1118,9 +1184,14 @@ func (ch *chat) itemRow(it signal.Accessor[chatItem], _ signal.Accessor[int]) ui
|
||||
switch cur.Kind {
|
||||
case "user":
|
||||
return ui.Row(ui.Style{Justify: ui.JustifyEnd, Padding: ui.Symmetric(4, 12)},
|
||||
ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(8, 12), CornerRadius: 16,
|
||||
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.Show(signal.Func(func() bool { return len(ch.byID.Get()[cur.MsgID].Files) > 0 }),
|
||||
func() ui.Node {
|
||||
return ui.Text(ch.filesText(cur.MsgID),
|
||||
ui.Style{Color: colors.OnAccent, Font: fonts.Caption})
|
||||
}),
|
||||
),
|
||||
)
|
||||
case "thinking":
|
||||
@@ -1379,6 +1450,21 @@ func (ch *chat) toolRow(cur chatItem) ui.Node {
|
||||
|
||||
func (ch *chat) msg(id string) ocapi.Message { return ch.byID.Get()[id] }
|
||||
|
||||
func (ch *chat) filesText(id string) signal.Accessor[string] {
|
||||
return signal.Func(func() string {
|
||||
m := ch.byID.Get()[id]
|
||||
names := make([]string, 0, len(m.Files))
|
||||
for _, f := range m.Files {
|
||||
if f.Name != "" {
|
||||
names = append(names, "📎 "+f.Name)
|
||||
} else {
|
||||
names = append(names, "📎 附件")
|
||||
}
|
||||
}
|
||||
return strings.Join(names, "\n")
|
||||
})
|
||||
}
|
||||
|
||||
func (ch *chat) textOf(id string) signal.Accessor[string] {
|
||||
return signal.Func(func() string { return ch.byID.Get()[id].Text })
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user