feat: switch model or agent and start new sessions

- chat: a searchable sheet picks the model or agent for the session; the
  choice is applied through the server and mirrored in the session list
- sessions: a new-session sheet with a title and a working directory
  (the server's default plus the directories already listed); creating
  one opens its chat
- ocapi: list models with their display names and read the server's
  default location
- bump version to 0.1.1 (build 2)
This commit is contained in:
blumia committed 2026-11-16 10:31:20 +08:00
1 parent 254d08b1bd
commit 23f72fc46d
9 files changed
+461 -42

No files matched your search

+102
View File
@@ -5,6 +5,7 @@ import (
"strings"
"time"
"github.com/egoist/asuka/fonts"
"github.com/egoist/asuka/signal"
"github.com/egoist/asuka/ui"
"github.com/openchamber/openchamber-native/internal/ocapi"
@@ -127,3 +128,104 @@ func sessionDir(s ocapi.Session) string {
}
return ""
}
// directories lists the working directories a new session can use: the
// server's default, then every directory the loaded sessions live in.
func (c *controller) directories() signal.Accessor[[]string] {
return signal.Func(func() []string {
seen := map[string]bool{}
out := make([]string, 0, 4)
add := func(d string) {
if d == "" || seen[d] {
return
}
seen[d] = true
out = append(out, d)
}
add(c.defaultDir.Get())
for _, s := range c.sessions.Get() {
if s.Location != nil {
add(s.Location.Directory)
}
}
return out
})
}
func chatActions(ch *chat) []ui.Action {
return []ui.Action{
{Title: ui.Val("模型"), OnPress: ch.openModelPicker},
{Title: ui.Val("Agent"), OnPress: ch.openAgentPicker},
}
}
func (c *controller) openNewSession() {
if c.client == nil {
return
}
c.newTitle.Set("")
c.newDir.Set(0)
c.creating.Set(false)
c.newOpen.Set(true)
}
func (c *controller) newSessionSheet() ui.Node {
return ui.SafeArea(ui.Style{Padding: ui.All(20), Gap: 14, Grow: 1},
ui.StaticText("新建会话", ui.Style{Font: fonts.Title2.Weighted(fonts.Bold)}),
ui.TextField(ui.TextFieldProps{
Value: c.newTitle,
Placeholder: ui.Val("标题(可选,留空由服务端生成)"),
ReturnKey: ui.ReturnGo,
OnSubmit: c.createSession,
}),
ui.Picker(ui.PickerProps{
Items: c.directories(),
Selected: c.newDir,
Label: ui.Val("工作目录"),
}),
ui.Button(ui.ButtonProps{
Title: ui.Val("创建"),
Variant: ui.ButtonFilled,
Disabled: c.creating,
OnPress: c.createSession,
}),
ui.Show(c.creating, func() ui.Node { return ui.Activity() }),
)
}
func (c *controller) createSession() {
client := c.client
nav := c.nav
if client == nil || c.creating.Peek() {
return
}
dirs := signal.Untrack(func() []string { return c.directories().Get() })
dir := ""
if idx := c.newDir.Peek(); idx >= 0 && idx < len(dirs) {
dir = dirs[idx]
}
title := strings.TrimSpace(c.newTitle.Peek())
c.creating.Set(true)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
defer cancel()
s, err := client.CreateSession(ctx, ocapi.CreateSessionOptions{Directory: dir, Title: title})
ui.Post(func() {
c.creating.Set(false)
if err != nil {
c.status.Set(friendlyError(err))
return
}
c.newOpen.Set(false)
c.sessions.Update(func(list []ocapi.Session) []ocapi.Session {
next := make([]ocapi.Session, 0, len(list)+1)
next = append(next, s)
return append(next, list...)
})
if nav != nil {
ch := c.openChat(&s, nav)
nav.Push(ch.screen, ui.ScreenOptions{Title: ch.title, Actions: chatActions(ch)})
}
})
}()
}