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

+170
View File
@@ -53,6 +53,21 @@ type chat struct {
reasoningSlots map[string]map[int]int
reloadScheduled bool
listNode ui.Node
pickerOpen *signal.Signal[bool]
pickerKind string
pickerItems *signal.Signal[[]pickerItem]
pickerFilter *signal.Signal[string]
pickerLoad *signal.Signal[bool]
}
// pickerItem is one row of the model/agent sheet.
type pickerItem struct {
Key string
Title string
Subtitle string
Model *ocapi.ModelRef
Agent string
}
func (c *controller) openChat(s *ocapi.Session, nav *ui.Navigator) *chat {
@@ -76,6 +91,10 @@ func (c *controller) openChat(s *ocapi.Session, nav *ui.Navigator) *chat {
errText: signal.New(""),
textSlots: map[string]map[int]int{},
reasoningSlots: map[string]map[int]int{},
pickerOpen: signal.New(false),
pickerItems: signal.NewFunc([]pickerItem{}, nil),
pickerFilter: signal.New(""),
pickerLoad: signal.New(false),
}
c.chat = ch
ch.load()
@@ -417,9 +436,160 @@ func (ch *chat) screen() ui.Node {
ch.permissionSection(),
ch.composer(),
),
ui.Sheet(ui.SheetProps{
Open: ch.pickerOpen,
Detents: ui.DetentLarge,
Grabber: true,
Content: ch.pickerSheet,
OnDismiss: func() { ch.pickerOpen.Set(false) },
}),
)
}
func (ch *chat) openModelPicker() {
ch.openPicker("model")
}
func (ch *chat) openAgentPicker() {
ch.openPicker("agent")
}
func (ch *chat) openPicker(kind string) {
client := ch.client()
if client == nil {
return
}
ch.pickerKind = kind
ch.pickerFilter.Set("")
ch.pickerItems.Set(nil)
ch.pickerLoad.Set(true)
ch.pickerOpen.Set(true)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
var items []pickerItem
var err error
switch kind {
case "model":
var models []ocapi.ModelInfo
models, err = client.Models(ctx, ch.dir)
for _, m := range models {
name := m.Name
if name == "" {
name = m.ID
}
items = append(items, pickerItem{
Key: m.ProviderID + "/" + m.ID,
Title: name,
Subtitle: m.ProviderID + " · " + m.ID,
Model: &ocapi.ModelRef{ID: m.ID, ProviderID: m.ProviderID, Variant: m.Variant},
})
}
case "agent":
var agents []ocapi.AgentInfo
agents, err = client.Agents(ctx, ch.dir)
for _, a := range agents {
if a.Hidden {
continue
}
items = append(items, pickerItem{
Key: a.ID,
Title: a.Name,
Subtitle: a.Description,
Agent: a.ID,
})
}
}
ui.Post(func() {
ch.pickerLoad.Set(false)
if err != nil {
ch.errText.Set(friendlyError(err))
return
}
ch.pickerItems.Set(items)
})
}()
}
func (ch *chat) filteredPicks() signal.Accessor[[]pickerItem] {
return signal.Func(func() []pickerItem {
q := strings.ToLower(strings.TrimSpace(ch.pickerFilter.Get()))
items := ch.pickerItems.Get()
if q == "" {
return items
}
out := make([]pickerItem, 0, len(items))
for _, it := range items {
if strings.Contains(strings.ToLower(it.Title), q) ||
strings.Contains(strings.ToLower(it.Subtitle), q) {
out = append(out, it)
}
}
return out
})
}
func (ch *chat) pickerSheet() ui.Node {
title := "选择模型"
if ch.pickerKind == "agent" {
title = "选择 Agent"
}
return ui.SafeArea(ui.Style{Padding: ui.All(20), Gap: 12, Grow: 1},
ui.StaticText(title, ui.Style{Font: fonts.Title2.Weighted(fonts.Bold)}),
ui.TextField(ui.TextFieldProps{
Value: ch.pickerFilter,
Placeholder: ui.Val("搜索"),
Style: ui.Style{Grow: 0},
}),
ui.Show(ch.pickerLoad, func() ui.Node {
return ui.Activity()
}),
ui.List(ui.ListProps{Appearance: ui.ListPlain, Style: ui.Style{Grow: 1}},
ch.filteredPicks(), func(it pickerItem) string { return it.Key },
func(it signal.Accessor[pickerItem], _ signal.Accessor[int]) ui.Node {
return ui.View(ui.ViewProps{OnPress: func() { ch.applyPick(it.Get()) },
Style: ui.Style{Padding: ui.Symmetric(10, 4), Gap: 2}},
ui.Text(signal.Func(func() string { return it.Get().Title }), ui.Style{Font: fonts.Headline, Lines: 1}),
ui.Show(signal.Func(func() bool { return it.Get().Subtitle != "" }),
func() ui.Node {
return ui.Text(signal.Func(func() string { return it.Get().Subtitle }),
ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel, Lines: 2})
}),
)
}),
)
}
func (ch *chat) applyPick(it pickerItem) {
ch.pickerOpen.Set(false)
client := ch.client()
if client == nil {
return
}
kind := ch.pickerKind
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
var err error
if kind == "model" && it.Model != nil {
err = client.SwitchModel(ctx, ch.id, ch.dir, *it.Model)
} else if it.Agent != "" {
err = client.SwitchAgent(ctx, ch.id, ch.dir, it.Agent)
}
ui.Post(func() {
if err != nil {
ch.errText.Set(friendlyError(err))
return
}
if kind == "model" && it.Model != nil {
ch.c.patchSession(ch.id, func(s *ocapi.Session) { s.Model = it.Model })
} else if it.Agent != "" {
ch.c.patchSession(ch.id, func(s *ocapi.Session) { s.Agent = it.Agent })
}
})
}()
}
// perms are the pending permission requests for this session.
func (ch *chat) perms() signal.Accessor[[]ocapi.PermissionRequest] {
return signal.Func(func() []ocapi.PermissionRequest {