Repository navigation
Add test for streaming text concatenation in gemini_agent - #1279
Conversation
📝 WalkthroughWalkthroughA new test case named "test gemini stream concatenation" was added to verify that streamed chunks from dataStream are correctly concatenated. The test collects chunk texts via streamTokenCallback, concatenates them, and validates that the concatenated result matches the final response text. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@llm_agents/gemini_agent/tests/run_gemini.ts`:
- Around line 173-191: The test dereferences res.text without checking that
geminiAgent returned a value; update the assertion to guard against undefined by
checking the result (res) and res.text before using them (e.g., if (!res ||
typeof res.text !== "string") handle/log/throw a clear error), and then compare
concatenatedFromChunks to res.text; reference the geminiAgent call, the res
variable, res.text, concatenatedFromChunks and collectedChunks when making this
defensive check so failures report a clear message instead of throwing on
undefined.
| const res = (await geminiAgent({ | ||
| ...defaultTestContext, | ||
| namedInputs, | ||
| params, | ||
| filterParams: { | ||
| streamTokenCallback: (data: any) => { | ||
| const text = data?.response?.output?.[0]?.text; | ||
| if (text) { | ||
| collectedChunks.push(text); | ||
| } | ||
| }, | ||
| }, | ||
| })) as any; | ||
|
|
||
| const concatenatedFromChunks = collectedChunks.join(""); | ||
|
|
||
| console.log("Collected chunks:", collectedChunks); | ||
| console.log("Concatenated from chunks:", concatenatedFromChunks); | ||
| console.log("Result text:", res.text); |
There was a problem hiding this comment.
Guard against undefined response before dereferencing res.text.
res.text is accessed unconditionally; if geminiAgent returns undefined on error, this will throw before the assertion explains why. Add a quick guard so failures are clearer.
Suggested fix
- const res = (await geminiAgent({
+ const res = (await geminiAgent({
...defaultTestContext,
namedInputs,
params,
filterParams: {
streamTokenCallback: (data: any) => {
const text = data?.response?.output?.[0]?.text;
if (text) {
collectedChunks.push(text);
}
},
},
})) as any;
+ assert.ok(res, "Expected a response from geminiAgent");
const concatenatedFromChunks = collectedChunks.join("");📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const res = (await geminiAgent({ | |
| ...defaultTestContext, | |
| namedInputs, | |
| params, | |
| filterParams: { | |
| streamTokenCallback: (data: any) => { | |
| const text = data?.response?.output?.[0]?.text; | |
| if (text) { | |
| collectedChunks.push(text); | |
| } | |
| }, | |
| }, | |
| })) as any; | |
| const concatenatedFromChunks = collectedChunks.join(""); | |
| console.log("Collected chunks:", collectedChunks); | |
| console.log("Concatenated from chunks:", concatenatedFromChunks); | |
| console.log("Result text:", res.text); | |
| const res = (await geminiAgent({ | |
| ...defaultTestContext, | |
| namedInputs, | |
| params, | |
| filterParams: { | |
| streamTokenCallback: (data: any) => { | |
| const text = data?.response?.output?.[0]?.text; | |
| if (text) { | |
| collectedChunks.push(text); | |
| } | |
| }, | |
| }, | |
| })) as any; | |
| assert.ok(res, "Expected a response from geminiAgent"); | |
| const concatenatedFromChunks = collectedChunks.join(""); | |
| console.log("Collected chunks:", collectedChunks); | |
| console.log("Concatenated from chunks:", concatenatedFromChunks); | |
| console.log("Result text:", res.text); |
🤖 Prompt for AI Agents
In `@llm_agents/gemini_agent/tests/run_gemini.ts` around lines 173 - 191, The test
dereferences res.text without checking that geminiAgent returned a value; update
the assertion to guard against undefined by checking the result (res) and
res.text before using them (e.g., if (!res || typeof res.text !== "string")
handle/log/throw a clear error), and then compare concatenatedFromChunks to
res.text; reference the geminiAgent call, the res variable, res.text,
concatenatedFromChunks and collectedChunks when making this defensive check so
failures report a clear message instead of throwing on undefined.
概要
PR #1276 で修正されたストリーミング時のテキスト連結機能に対するテストを追加しました。
変更内容
tests/run_gemini.ts にストリーミングの連結テスト test gemini stream concatenation を追加
ストリーミング中に収集したチャンクと、最終結果の res.text が一致することを検証
テスト内容
ストリーミングモードで Gemini API を呼び出し
streamTokenCallback で各チャンクのテキストを収集
収集したチャンクを連結した結果と res.text が一致することを確認
関連
Resolves: なし(テスト追加のみ)
Related: #1276
Summary by CodeRabbit
Tests
✏️ Tip: You can customize this high-level summary in your review settings.