-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstream_test.go
More file actions
333 lines (307 loc) · 10.5 KB
/
Copy pathstream_test.go
File metadata and controls
333 lines (307 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2025 Xavier Portilla Edo
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package bedrock
import (
"context"
"errors"
"strings"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types"
"github.com/firebase/genkit/go/ai"
)
func TestConsumeStreamEvents_TextOnly(t *testing.T) {
events := streamEvents(
textDelta(0, "hel"),
textDelta(0, "lo"),
&types.ConverseStreamOutputMemberMetadata{Value: types.ConverseStreamMetadataEvent{
Usage: &types.TokenUsage{
InputTokens: aws.Int32(3),
OutputTokens: aws.Int32(2),
TotalTokens: aws.Int32(5),
},
}},
&types.ConverseStreamOutputMemberMessageStop{Value: types.MessageStopEvent{StopReason: types.StopReasonEndTurn}},
)
var chunks []string
req := &ai.ModelRequest{}
resp, err := (&Bedrock{}).consumeStreamEvents(context.Background(), events, req, func(ctx context.Context, chunk *ai.ModelResponseChunk) error {
if len(chunk.Content) != 1 || !chunk.Content[0].IsText() {
t.Fatalf("chunk content = %+v, want one text part", chunk.Content)
}
chunks = append(chunks, chunk.Content[0].Text)
return nil
})
if err != nil {
t.Fatal(err)
}
if strings.Join(chunks, "") != "hello" {
t.Fatalf("streamed chunks = %q, want hello", strings.Join(chunks, ""))
}
if resp.Text() != "hello" {
t.Fatalf("final text = %q, want hello", resp.Text())
}
if resp.FinishReason != ai.FinishReasonStop {
t.Fatalf("FinishReason = %q, want stop", resp.FinishReason)
}
if resp.Usage == nil || resp.Usage.TotalTokens != 5 {
t.Fatalf("Usage = %+v, want total tokens 5", resp.Usage)
}
if resp.Request != req {
t.Fatalf("Request = %p, want %p", resp.Request, req)
}
}
func TestBlocksToParts_StreamReassembly(t *testing.T) {
blocks := map[int32]*streamBlock{
1: {isTool: true, toolID: "call_1", toolName: "get_weather"},
0: {},
}
blocks[1].toolInput.WriteString(`{"location":"NYC"}`)
blocks[0].text.WriteString("Looking up the weather...")
parts, err := (&Bedrock{}).blocksToParts(blocks, nil)
if err != nil {
t.Fatal(err)
}
if len(parts) != 2 {
t.Fatalf("len(parts) = %d, want 2", len(parts))
}
if parts[0].Text != "Looking up the weather..." {
t.Fatalf("parts[0] = %q, want text", parts[0].Text)
}
if !parts[1].IsToolRequest() {
t.Fatalf("parts[1] = %+v, want tool request", parts[1])
}
if parts[1].ToolRequest.Name != "get_weather" {
t.Fatalf("tool name = %q, want get_weather", parts[1].ToolRequest.Name)
}
input := parts[1].ToolRequest.Input.(map[string]any)
if input["location"] != "NYC" {
t.Fatalf("location = %v, want NYC", input["location"])
}
}
func TestConsumeStreamEvents_ToolUseFragmentsAndSchemaConversion(t *testing.T) {
req := &ai.ModelRequest{Tools: []*ai.ToolDefinition{
{
Name: "get_weather",
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"location": map[string]any{"type": "string"},
"count": map[string]any{"type": "string"},
},
},
},
}}
events := streamEvents(
toolStart(0, "call_1", "get_weather"),
toolDelta(0, `{"location":"NYC",`),
toolDelta(0, `"count":42}`),
toolStop(0),
&types.ConverseStreamOutputMemberMessageStop{Value: types.MessageStopEvent{StopReason: types.StopReasonToolUse}},
)
var toolChunks int
resp, err := (&Bedrock{}).consumeStreamEvents(context.Background(), events, req, func(ctx context.Context, chunk *ai.ModelResponseChunk) error {
toolChunks++
if len(chunk.Content) != 1 || !chunk.Content[0].IsToolRequest() {
t.Fatalf("chunk content = %+v, want one tool request", chunk.Content)
}
return nil
})
if err != nil {
t.Fatal(err)
}
if toolChunks != 1 {
t.Fatalf("tool callback count = %d, want 1", toolChunks)
}
if resp.FinishReason != ai.FinishReasonStop {
t.Fatalf("FinishReason = %q, want stop", resp.FinishReason)
}
if len(resp.Message.Content) != 1 || !resp.Message.Content[0].IsToolRequest() {
t.Fatalf("final content = %+v, want one tool request", resp.Message.Content)
}
input := resp.Message.Content[0].ToolRequest.Input.(map[string]any)
if input["count"] != "42" {
t.Fatalf("count = %#v (%T), want string 42", input["count"], input["count"])
}
}
func TestConsumeStreamEvents_CallbackErrors(t *testing.T) {
callbackErr := errors.New("callback failed")
tests := []struct {
name string
events <-chan types.ConverseStreamOutput
}{
{
name: "text",
events: streamEvents(textDelta(0, "hello")),
},
{
name: "reasoning",
events: streamEvents(&types.ConverseStreamOutputMemberContentBlockDelta{Value: types.ContentBlockDeltaEvent{
ContentBlockIndex: aws.Int32(0),
Delta: &types.ContentBlockDeltaMemberReasoningContent{
Value: &types.ReasoningContentBlockDeltaMemberText{Value: "thinking"},
},
}}),
},
{
name: "tool stop",
events: streamEvents(
toolStart(0, "call_1", "get_weather"),
toolDelta(0, `{}`),
toolStop(0),
),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := (&Bedrock{}).consumeStreamEvents(context.Background(), tt.events, nil, func(ctx context.Context, chunk *ai.ModelResponseChunk) error {
return callbackErr
})
if !errors.Is(err, callbackErr) {
t.Fatalf("error = %v, want callback error", err)
}
})
}
}
func TestConsumeStreamEvents_MalformedToolInputErrors(t *testing.T) {
var callbacks int
_, err := (&Bedrock{}).consumeStreamEvents(context.Background(), streamEvents(
toolStart(0, "call_1", "get_weather"),
toolDelta(0, `{not valid`),
toolStop(0),
), nil, func(ctx context.Context, chunk *ai.ModelResponseChunk) error {
callbacks++
return nil
})
if err == nil || !strings.Contains(err.Error(), "stream tool block 0") {
t.Fatalf("error = %v, want malformed tool input error", err)
}
if callbacks != 0 {
t.Fatalf("callbacks = %d, want 0", callbacks)
}
}
func TestConsumeStreamEvents_UnsupportedDeltaErrors(t *testing.T) {
_, err := (&Bedrock{}).consumeStreamEvents(context.Background(), streamEvents(
&types.ConverseStreamOutputMemberContentBlockDelta{Value: types.ContentBlockDeltaEvent{
ContentBlockIndex: aws.Int32(0),
Delta: &types.ContentBlockDeltaMemberCitation{},
}},
), nil, nil)
if err == nil || !strings.Contains(err.Error(), "unhandled stream content delta") {
t.Fatalf("error = %v, want unsupported delta error", err)
}
}
func TestAppendContentBlockDelta_NilBlockErrors(t *testing.T) {
err := appendContentBlockDelta(context.Background(), nil, &types.ContentBlockDeltaMemberText{Value: "hello"}, nil)
if !errors.Is(err, errStreamBlockRequired) {
t.Fatalf("error = %v, want errStreamBlockRequired", err)
}
}
func TestAppendReasoningDelta_NilBlockErrors(t *testing.T) {
_, err := appendReasoningDelta(nil, &types.ReasoningContentBlockDeltaMemberText{Value: "thinking"})
if !errors.Is(err, errStreamBlockRequired) {
t.Fatalf("error = %v, want errStreamBlockRequired", err)
}
}
func TestEmitToolBlockStopNoopCases(t *testing.T) {
ctx := context.Background()
b := &Bedrock{}
called := false
cb := func(context.Context, *ai.ModelResponseChunk) error {
called = true
return nil
}
if err := b.emitToolBlockStop(ctx, 0, nil, nil, cb); err != nil {
t.Fatalf("nil block error = %v", err)
}
if err := b.emitToolBlockStop(ctx, 0, &streamBlock{}, nil, cb); err != nil {
t.Fatalf("non-tool block error = %v", err)
}
if err := b.emitToolBlockStop(ctx, 0, &streamBlock{isTool: true}, nil, nil); err != nil {
t.Fatalf("nil callback error = %v", err)
}
if called {
t.Fatal("callback was called for no-op tool block stop cases")
}
}
func TestConsumeStreamEvents_EmptyContentReturnsPlaceholder(t *testing.T) {
resp, err := (&Bedrock{}).consumeStreamEvents(context.Background(), streamEvents(), nil, nil)
if err != nil {
t.Fatal(err)
}
if len(resp.Message.Content) != 1 || !resp.Message.Content[0].IsText() || resp.Message.Content[0].Text != "" {
t.Fatalf("content = %+v, want empty text placeholder", resp.Message.Content)
}
}
func TestDecodeToolInput_EmptyAndMalformed(t *testing.T) {
v, err := decodeToolInput(" \n\t")
if err != nil || v != nil {
t.Fatalf("decodeToolInput(empty) = (%v, %v), want (nil, nil)", v, err)
}
if _, err := decodeToolInput("{not valid"); err == nil {
t.Fatal("expected malformed JSON error")
}
}
func TestDecodeToolInput_TrailingJSONErrors(t *testing.T) {
_, err := decodeToolInput(`{"ok":true} {"extra":true}`)
if err == nil || !strings.Contains(err.Error(), "trailing data") {
t.Fatalf("decodeToolInput trailing JSON error = %v, want trailing data error", err)
}
}
func TestIndexOf(t *testing.T) {
if got := indexOf(nil); got != 0 {
t.Fatalf("indexOf(nil) = %d, want 0", got)
}
idx := int32(7)
if got := indexOf(&idx); got != 7 {
t.Fatalf("indexOf(&7) = %d, want 7", got)
}
}
func streamEvents(events ...types.ConverseStreamOutput) <-chan types.ConverseStreamOutput {
ch := make(chan types.ConverseStreamOutput, len(events))
for _, event := range events {
ch <- event
}
close(ch)
return ch
}
func textDelta(idx int32, text string) types.ConverseStreamOutput {
return &types.ConverseStreamOutputMemberContentBlockDelta{Value: types.ContentBlockDeltaEvent{
ContentBlockIndex: aws.Int32(idx),
Delta: &types.ContentBlockDeltaMemberText{Value: text},
}}
}
func toolStart(idx int32, id, name string) types.ConverseStreamOutput {
return &types.ConverseStreamOutputMemberContentBlockStart{Value: types.ContentBlockStartEvent{
ContentBlockIndex: aws.Int32(idx),
Start: &types.ContentBlockStartMemberToolUse{Value: types.ToolUseBlockStart{
ToolUseId: aws.String(id),
Name: aws.String(name),
}},
}}
}
func toolDelta(idx int32, input string) types.ConverseStreamOutput {
return &types.ConverseStreamOutputMemberContentBlockDelta{Value: types.ContentBlockDeltaEvent{
ContentBlockIndex: aws.Int32(idx),
Delta: &types.ContentBlockDeltaMemberToolUse{Value: types.ToolUseBlockDelta{Input: aws.String(input)}},
}}
}
func toolStop(idx int32) types.ConverseStreamOutput {
return &types.ConverseStreamOutputMemberContentBlockStop{Value: types.ContentBlockStopEvent{
ContentBlockIndex: aws.Int32(idx),
}}
}