feat(agent): 支持持久化 /btw 旁路提问
This commit is contained in:
1 parent
8dae851b9b
commit
555539bc8e
34 files changed
+3744
-314
No files matched your search
@@ -130,7 +130,12 @@ func (r *Recorder) Stream(ctx context.Context, req llm.CompletionRequest) iter.S
|
||||
streamErr error
|
||||
)
|
||||
|
||||
finished := false
|
||||
finish := func(err error) {
|
||||
if finished {
|
||||
return
|
||||
}
|
||||
finished = true
|
||||
status := "ok"
|
||||
if err != nil {
|
||||
status = "error"
|
||||
@@ -142,6 +147,7 @@ func (r *Recorder) Stream(ctx context.Context, req llm.CompletionRequest) iter.S
|
||||
r.record(req, session, taskID, worker, reqBody, capt, start, textBuf.String(), thinkingBuf.String(), usage, stopReason, err)
|
||||
}
|
||||
}
|
||||
defer func() { finish(ctx.Err()) }()
|
||||
|
||||
for ev, err := range r.inner.Stream(ctx, req) {
|
||||
if err != nil {
|
||||
|
||||
@@ -3,9 +3,13 @@ package llmrec
|
||||
import (
|
||||
"context"
|
||||
"iter"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/Autumn-27/artex/db"
|
||||
"github.com/Autumn-27/norma/llm"
|
||||
"github.com/Autumn-27/norma/transcript"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type completeProvider struct{}
|
||||
@@ -63,3 +67,59 @@ func TestCompleteForwardsAtomicResponse(t *testing.T) {
|
||||
t.Fatalf("Complete() usage=%+v", usage)
|
||||
}
|
||||
}
|
||||
|
||||
type meteredStreamProvider struct{ completeProvider }
|
||||
|
||||
func (meteredStreamProvider) Stream(context.Context, llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
|
||||
return func(yield func(llm.StreamEvent, error) bool) {
|
||||
if !yield(llm.StreamEvent{Type: llm.SEMessageStart, Usage: llm.Usage{InputTokens: 23}}, nil) {
|
||||
return
|
||||
}
|
||||
if !yield(llm.StreamEvent{Type: llm.SEMessageDelta, Usage: llm.Usage{OutputTokens: 5}}, nil) {
|
||||
return
|
||||
}
|
||||
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSideUsageRecordedOnceOnConsumerCancellation(t *testing.T) {
|
||||
dsn := os.Getenv("ARTEX_PG_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("requires isolated ARTEX_PG_DSN")
|
||||
}
|
||||
pg, err := db.Open(dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pg.Close()
|
||||
for _, early := range []bool{false, true} {
|
||||
profile := "btw-metering-" + uuid.NewString()
|
||||
ctx, cancel := context.WithCancel(transcript.WithSessionID(t.Context(), "exp0-btw-test"))
|
||||
recorder := Wrap(meteredStreamProvider{}, pg, "fixture", profile, "", "", func() bool { return false })
|
||||
for event, err := range recorder.Stream(ctx, llm.CompletionRequest{}) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if early && event.Type == llm.SEMessageDelta {
|
||||
cancel()
|
||||
break // Consumer exits before the provider can return a cancellation event.
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
var count, input, output int
|
||||
var worker, status string
|
||||
err = pg.QueryRow(`SELECT count(*),max(input_tokens),max(output_tokens),max(worker),max(status)
|
||||
FROM llm_usage WHERE profile_name=$1`, profile).Scan(&count, &input, &output, &worker, &status)
|
||||
_, _ = pg.Exec(`DELETE FROM llm_usage WHERE profile_name=$1`, profile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantStatus := "ok"
|
||||
if early {
|
||||
wantStatus = "error"
|
||||
}
|
||||
if count != 1 || input != 23 || output != 5 || worker != "btw" || status != wantStatus {
|
||||
t.Fatalf("early=%v: count=%d usage=%d/%d worker=%q status=%q", early, count, input, output, worker, status)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user