feat(server): provider switching API, WS/REST cancel, summarize endpoints
This commit is contained in:
parent
f1b2093b22
commit
49fdc5a189
7 changed files with 463 additions and 28 deletions
39
internal/llm/cancel_test.go
Normal file
39
internal/llm/cancel_test.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Cancelling the context must abort the in-flight provider HTTP request.
|
||||
func TestOpenAICancelAbortsRequest(t *testing.T) {
|
||||
aborted := make(chan struct{})
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fmt.Fprint(w, "data: {\"choices\":[{\"delta\":{\"content\":\"Hal\"}}]}\n\n")
|
||||
w.(http.Flusher).Flush()
|
||||
<-r.Context().Done()
|
||||
close(aborted)
|
||||
}))
|
||||
defer srv.Close()
|
||||
p := NewOpenAI(srv.URL, "k", "m", 0, 30*time.Second)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
got := ""
|
||||
_, err := p.Chat(ctx, Request{Messages: []Message{{Role: RoleUser, Content: "x"}}}, func(s string) {
|
||||
got += s
|
||||
cancel()
|
||||
})
|
||||
if err == nil || !errors.Is(ctx.Err(), context.Canceled) || got != "Hal" {
|
||||
t.Fatalf("err=%v got=%q", err, got)
|
||||
}
|
||||
select {
|
||||
case <-aborted:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("provider request was not aborted")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue