@@ -28,8 +28,24 @@ const (
2828 MockModeMaxTokens MockMode = "max_tokens"
2929)
3030
31+ // MockTurn is a single scripted reply for a scripted mock provider. Each call to
32+ // Chat/StreamChat consumes the next turn, letting tests drive multi-turn
33+ // tool-calling conversations without a network.
34+ type MockTurn struct {
35+ Content string // assistant text
36+ ToolCalls []core.ToolCall // optional tool calls
37+ FinishReason string // defaults to end_turn / tool_use
38+ Usage * core.MuxUsage // defaults to a small non-zero usage
39+ Err error // if set, the call fails with it
40+ Delay time.Duration // simulate latency (cancellation aware)
41+ OnBefore func (messages []core.MuxMessage , opts core.ChatOptions ) // inspect inputs, late (outside lock)
42+ }
43+
3144// MockProvider is a core.Provider implementation for testing.
3245// It never makes real HTTP requests.
46+ //
47+ // Modes (Mode field) are used when Script is empty. When Script is populated it
48+ // is consumed turn-by-turn and Mode is ignored.
3349type MockProvider struct {
3450 mu sync.Mutex
3551 Mode MockMode
@@ -38,6 +54,14 @@ type MockProvider struct {
3854 ToolArgs map [string ]interface {}
3955 Delay time.Duration // simulate latency
4056 Calls []MockCall // recorded calls for assertions
57+
58+ // Script drives deterministic multi-turn conversations. When non-empty,
59+ // Chat/StreamChat consume one turn per call instead of Mode.
60+ Script []MockTurn
61+ // Loop replays the script from the start once it is exhausted.
62+ Loop bool
63+ // scriptIndex tracks the next Script entry to consume.
64+ scriptIndex int
4165}
4266
4367// MockCall records a single call to the mock provider.
@@ -64,16 +88,74 @@ func (m *MockProvider) Ping(_ context.Context) error { return nil }
6488func (m * MockProvider ) Chat (ctx context.Context , messages []core.MuxMessage , opts core.ChatOptions ) (* core.MuxResponse , error ) {
6589 m .mu .Lock ()
6690 m .Calls = append (m .Calls , MockCall {Messages : messages , Options : opts })
91+
92+ var turn MockTurn
93+ usedScript := false
94+ if len (m .Script ) > 0 {
95+ usedScript = true
96+ switch {
97+ case m .scriptIndex < len (m .Script ):
98+ turn = m .Script [m .scriptIndex ]
99+ m .scriptIndex ++
100+ case m .Loop && len (m .Script ) > 0 :
101+ turn = m .Script [m .scriptIndex % len (m .Script )]
102+ m .scriptIndex ++
103+ default :
104+ m .mu .Unlock ()
105+ return nil , fmt .Errorf ("mux: mock script exhausted after %d turns" , len (m .Script ))
106+ }
107+ }
67108 m .mu .Unlock ()
68109
69- if m .Delay > 0 {
110+ if usedScript && turn .OnBefore != nil {
111+ turn .OnBefore (messages , opts )
112+ }
113+
114+ delay := m .Delay
115+ if usedScript && turn .Delay > 0 {
116+ delay = turn .Delay
117+ }
118+ if delay > 0 {
70119 select {
71- case <- time .After (m . Delay ):
120+ case <- time .After (delay ):
72121 case <- ctx .Done ():
73122 return nil , ctx .Err ()
74123 }
124+ } else {
125+ select {
126+ case <- ctx .Done ():
127+ return nil , ctx .Err ()
128+ default :
129+ }
75130 }
76131
132+ if usedScript {
133+ if turn .Err != nil {
134+ return nil , turn .Err
135+ }
136+ finish := turn .FinishReason
137+ if finish == "" {
138+ if len (turn .ToolCalls ) > 0 {
139+ finish = "tool_use"
140+ } else {
141+ finish = "end_turn"
142+ }
143+ }
144+ usage := turn .Usage
145+ if usage == nil {
146+ usage = & core.MuxUsage {PromptTokens : 10 , CompletionTokens : 5 , TotalTokens : 15 }
147+ }
148+ return & core.MuxResponse {
149+ Content : turn .Content ,
150+ ToolCalls : turn .ToolCalls ,
151+ FinishReason : finish ,
152+ Usage : usage ,
153+ }, nil
154+ }
155+
156+ m .mu .Lock ()
157+ defer m .mu .Unlock ()
158+ // Re-add the call synchronously after script branch? Already recorded above.
77159 switch m .Mode {
78160 case MockModeError :
79161 return nil , fmt .Errorf ("mux: mock error" )
@@ -158,11 +240,12 @@ func (m *MockProvider) LastCall() *MockCall {
158240 return & c
159241}
160242
161- // Reset clears recorded calls.
243+ // Reset clears recorded calls and resets the scripted turn cursor .
162244func (m * MockProvider ) Reset () {
163245 m .mu .Lock ()
164246 defer m .mu .Unlock ()
165247 m .Calls = nil
248+ m .scriptIndex = 0
166249}
167250
168251// MarshalCalls returns recorded calls as JSON for debugging.
0 commit comments