Show recorded OpenCode models in the chat list
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
type chatSessionModelResolver interface {
|
||||
ResolveSessionModel(context.Context, protocol.ChatSession) (protocol.ChatSession, error)
|
||||
}
|
||||
|
||||
func (m *chatManager) ResolveSessionModel(ctx context.Context, session protocol.ChatSession) (protocol.ChatSession, error) {
|
||||
if session.Model != "" || (session.Backend != "" && session.Backend != "opencode") {
|
||||
return session, nil
|
||||
}
|
||||
if resolver, ok := m.opencode.(chatSessionModelResolver); ok {
|
||||
return resolver.ResolveSessionModel(ctx, session)
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestChatListResolvesOnlyListedOperatorModels(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := newOperatorStore(t)
|
||||
if err := store.RecordOperatorSession(ctx, "ses_operator", "/operator", "Operator", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var detailReads, historyReads int
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/api/session":
|
||||
fmt.Fprint(w, `{"data":[{"id":"ses_operator","time":{"updated":12}},{"id":"ses_pipeline"},{"id":"ses_foreign"}]}`)
|
||||
case "/api/session/ses_operator":
|
||||
detailReads++
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_operator"}}`)
|
||||
case "/api/session/ses_operator/message":
|
||||
historyReads++
|
||||
fmt.Fprint(w, `{"data":[{"type":"assistant","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"}}]}`)
|
||||
default:
|
||||
t.Errorf("read unlisted history: %s", r.URL)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
broker := NewChatManager(ctx, opencode.NewChat(&opencode.Connection{Backend: "v2", URL: srv.URL}), store)
|
||||
for range 2 {
|
||||
var reply protocol.Message
|
||||
handleChatSessions(ctx, protocol.Message{ID: "list"}, broker, nil, store, func(msg protocol.Message) error { reply = msg; return nil })
|
||||
if reply.Type != protocol.ChatSessions || len(reply.ChatSessions) != 1 {
|
||||
t.Fatalf("list: %+v", reply)
|
||||
}
|
||||
row := reply.ChatSessions[0]
|
||||
if row.ID != "ses_operator" || row.Model != "deepseek-v4.1-flash" || row.ModelProvider != "opencode-go" {
|
||||
t.Fatalf("model: %+v", row)
|
||||
}
|
||||
}
|
||||
if detailReads != 1 || historyReads != 1 {
|
||||
t.Fatalf("metadata was not cached: detail=%d history=%d", detailReads, historyReads)
|
||||
}
|
||||
}
|
||||
|
||||
type unavailableSessionModel struct{ *fakeChat }
|
||||
|
||||
func (b unavailableSessionModel) ResolveSessionModel(_ context.Context, row protocol.ChatSession) (protocol.ChatSession, error) {
|
||||
return row, errors.New("metadata unavailable")
|
||||
}
|
||||
|
||||
func TestChatListSurvivesUnavailableModelMetadata(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := newOperatorStore(t)
|
||||
if err := store.RecordOperatorSession(ctx, "ses_operator", "/operator", "Operator", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fake := scriptedChat()
|
||||
fake.sessions = []protocol.ChatSession{{ID: "ses_operator"}}
|
||||
var reply protocol.Message
|
||||
handleChatSessions(ctx, protocol.Message{ID: "list"}, unavailableSessionModel{fake}, nil, store, func(msg protocol.Message) error { reply = msg; return nil })
|
||||
if reply.Type != protocol.ChatSessions || len(reply.ChatSessions) != 1 || reply.ChatSessions[0].Model != "" {
|
||||
t.Fatalf("unavailable metadata hid or invented a model: %+v", reply)
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,11 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
@@ -61,6 +63,10 @@ func handleChatSessions(ctx context.Context, request protocol.Message, broker ch
|
||||
present[session.ID] = session
|
||||
}
|
||||
}
|
||||
// All optional model lookups share one budget, so a slow metadata endpoint
|
||||
// cannot add one full timeout per row to the ordinary list request.
|
||||
modelCtx, cancelModels := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancelModels()
|
||||
for _, operator := range operatorSessions {
|
||||
session, ok := present[operator.ID]
|
||||
if !ok {
|
||||
@@ -72,6 +78,15 @@ func handleChatSessions(ctx context.Context, request protocol.Message, broker ch
|
||||
session.Directory = operator.Directory
|
||||
session.Tag = operator.Tag
|
||||
session.Backend = operator.Backend
|
||||
if resolver, ok := broker.(chatSessionModelResolver); ok && session.Model == "" && modelCtx.Err() == nil {
|
||||
resolved, err := resolver.ResolveSessionModel(modelCtx, session)
|
||||
if err != nil {
|
||||
// Metadata failure must not hide an otherwise usable chat.
|
||||
log.Printf("chat model metadata %s: %v", session.ID, err)
|
||||
} else {
|
||||
session = resolved
|
||||
}
|
||||
}
|
||||
session.CreatedAt = operator.CreatedAt
|
||||
session.UpdatedAt = operator.UpdatedAt
|
||||
// The closed state comes from the registry, so every client sees the same
|
||||
|
||||
Reference in New Issue
Block a user