package appui import ( "context" "strings" "time" "github.com/egoist/asuka/fonts" "github.com/egoist/asuka/signal" "github.com/egoist/asuka/ui" "github.com/openchamber/openchamber-native/internal/ocapi" ) func (c *controller) sessionMenu(s ocapi.Session) { ui.Alert(ui.AlertProps{ Title: "会话", ActionSheet: true, Actions: []ui.Action{ {Title: ui.Val("重命名"), Icon: "pencil", OnPress: func() { c.renameSession(s) }}, {Title: ui.Val("归档"), Icon: "archivebox", OnPress: func() { c.archiveSession(s) }}, {Title: ui.Val("删除"), Icon: ui.IconTrash, Role: ui.ActionDestructive, OnPress: func() { c.confirmDelete(s) }}, }, }) } func (c *controller) renameSession(s ocapi.Session) { name := signal.New(s.Title) ui.Alert(ui.AlertProps{ Title: "重命名会话", Message: "标题留空时,服务端不会自动生成新标题。", Fields: []ui.AlertField{{Text: name, Placeholder: "标题"}}, Actions: []ui.Action{ {Title: ui.Val("取消"), Role: ui.ActionCancel}, {Title: ui.Val("保存"), Role: ui.ActionDefault, Disabled: signal.Func(func() bool { return strings.TrimSpace(name.Get()) == "" }), OnPress: func() { c.patchSessionOnServer(s, name.Get()) }}, }, }) } func (c *controller) patchSessionOnServer(s ocapi.Session, title string) { client := c.client if client == nil { return } dir := sessionDir(s) go func() { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() t := strings.TrimSpace(title) err := client.UpdateSession(ctx, s.ID, dir, ocapi.SessionPatch{Title: &t}) ui.Post(func() { if err != nil { c.status.Set(friendlyError(err)) return } c.sessions.Update(func(list []ocapi.Session) []ocapi.Session { next := make([]ocapi.Session, len(list)) copy(next, list) for i := range next { if next[i].ID == s.ID { next[i].Title = t } } return next }) }) }() } func (c *controller) archiveSession(s ocapi.Session) { client := c.client if client == nil { return } dir := sessionDir(s) go func() { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() archived := true err := client.UpdateSession(ctx, s.ID, dir, ocapi.SessionPatch{Archived: &archived}) ui.Post(func() { if err != nil { c.status.Set(friendlyError(err)) return } c.removeSession(s.ID) }) }() } func (c *controller) confirmDelete(s ocapi.Session) { ui.Alert(ui.AlertProps{ Title: "删除会话?", Message: "会话及其子会话会被永久删除,无法撤销。", Actions: []ui.Action{ {Title: ui.Val("取消"), Role: ui.ActionCancel}, {Title: ui.Val("删除"), Role: ui.ActionDestructive, OnPress: func() { c.deleteSession(s) }}, }, }) } func (c *controller) deleteSession(s ocapi.Session) { client := c.client if client == nil { return } dir := sessionDir(s) go func() { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() err := client.DeleteSession(ctx, s.ID, dir) ui.Post(func() { if err != nil { c.status.Set(friendlyError(err)) return } c.removeSession(s.ID) }) }() } func sessionDir(s ocapi.Session) string { if s.Location != nil { return s.Location.Directory } 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) newSessionSheetNode() ui.Node { return ui.Sheet(ui.SheetProps{ Open: c.newOpen, Detents: ui.DetentLarge, Grabber: true, Content: c.newSessionSheet, OnDismiss: func() { c.creating.Set(false) }, }) } 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)}) } }) }() }