chat: add model switching and preloaded instructions
Expose the OpenCode model list and session model switch over the chat protocol, carry a per-session model provider on chat rows, and apply a preloaded behavior brief as a durable session instruction entry when a chat is created.
This commit is contained in:
@@ -83,6 +83,84 @@ func TestChatV2PromptWithMessageID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatSetInstructionShape(t *testing.T) {
|
||||
var path string
|
||||
var body map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
path = r.URL.Path
|
||||
if r.Method != http.MethodPut {
|
||||
t.Errorf("method = %s", r.Method)
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
if err := chat.SetInstruction(context.Background(), "ses_1", "pipelines.behavior", "be careful"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if path != "/api/experimental/session/ses_1/instructions/entries/pipelines.behavior" {
|
||||
t.Fatalf("path = %q", path)
|
||||
}
|
||||
if body["value"] != "be careful" {
|
||||
t.Fatalf("body = %+v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatModelsListsEnabledModels(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || r.URL.Path != "/api/model" {
|
||||
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[
|
||||
{"modelID":"b-model","providerID":"prov-b","name":"B Model","enabled":true,"status":"active"},
|
||||
{"modelID":"a-model","providerID":"prov-a","name":"A Model","enabled":true,"status":"active"},
|
||||
{"modelID":"off","providerID":"prov-a","name":"Off","enabled":false,"status":"active"},
|
||||
{"modelID":"old","providerID":"prov-a","name":"Old","enabled":true,"status":"deprecated"}
|
||||
]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
models, err := chat.Models(context.Background(), "ses_1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(models) != 2 || models[0].Provider != "prov-a" || models[0].ID != "a-model" || models[1].ID != "b-model" {
|
||||
t.Fatalf("models = %+v", models)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatSwitchModelShape(t *testing.T) {
|
||||
var body struct {
|
||||
Model struct {
|
||||
ID string `json:"id"`
|
||||
ProviderID string `json:"providerID"`
|
||||
} `json:"model"`
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost || r.URL.Path != "/api/session/ses_1/model" {
|
||||
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
|
||||
return
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
if err := chat.SwitchModel(context.Background(), "ses_1", "opencode-go", "deepseek-v4.1-flash"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body.Model.ID != "deepseek-v4.1-flash" || body.Model.ProviderID != "opencode-go" {
|
||||
t.Fatalf("model body = %+v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatV2PromptWithFiles(t *testing.T) {
|
||||
var body struct {
|
||||
Text string `json:"text"`
|
||||
|
||||
Reference in New Issue
Block a user