@google/gemini-cli-core 0.7.0-nightly.20250912.68035591 → 0.7.0-preview.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. package/dist/index.d.ts +5 -4
  2. package/dist/index.js +5 -4
  3. package/dist/index.js.map +1 -1
  4. package/dist/src/code_assist/converter.d.ts +1 -0
  5. package/dist/src/code_assist/converter.js +1 -0
  6. package/dist/src/code_assist/converter.js.map +1 -1
  7. package/dist/src/code_assist/converter.test.js +10 -0
  8. package/dist/src/code_assist/converter.test.js.map +1 -1
  9. package/dist/src/code_assist/oauth-credential-storage.d.ts +5 -7
  10. package/dist/src/code_assist/oauth-credential-storage.js +5 -8
  11. package/dist/src/code_assist/oauth-credential-storage.js.map +1 -1
  12. package/dist/src/code_assist/oauth-credential-storage.test.js +35 -33
  13. package/dist/src/code_assist/oauth-credential-storage.test.js.map +1 -1
  14. package/dist/src/code_assist/oauth2.js +28 -2
  15. package/dist/src/code_assist/oauth2.js.map +1 -1
  16. package/dist/src/code_assist/oauth2.test.js +674 -536
  17. package/dist/src/code_assist/oauth2.test.js.map +1 -1
  18. package/dist/src/config/config.d.ts +19 -0
  19. package/dist/src/config/config.js +48 -6
  20. package/dist/src/config/config.js.map +1 -1
  21. package/dist/src/config/config.test.js +93 -1
  22. package/dist/src/config/config.test.js.map +1 -1
  23. package/dist/src/config/models.d.ts +1 -0
  24. package/dist/src/config/models.js +1 -0
  25. package/dist/src/config/models.js.map +1 -1
  26. package/dist/src/core/baseLlmClient.d.ts +1 -0
  27. package/dist/src/core/baseLlmClient.js +24 -0
  28. package/dist/src/core/baseLlmClient.js.map +1 -1
  29. package/dist/src/core/baseLlmClient.test.js +63 -0
  30. package/dist/src/core/baseLlmClient.test.js.map +1 -1
  31. package/dist/src/core/client.d.ts +3 -4
  32. package/dist/src/core/client.js +62 -145
  33. package/dist/src/core/client.js.map +1 -1
  34. package/dist/src/core/client.test.js +119 -202
  35. package/dist/src/core/client.test.js.map +1 -1
  36. package/dist/src/core/coreToolScheduler.test.js +9 -0
  37. package/dist/src/core/coreToolScheduler.test.js.map +1 -1
  38. package/dist/src/core/geminiChat.d.ts +16 -11
  39. package/dist/src/core/geminiChat.js +124 -150
  40. package/dist/src/core/geminiChat.js.map +1 -1
  41. package/dist/src/core/geminiChat.test.js +342 -204
  42. package/dist/src/core/geminiChat.test.js.map +1 -1
  43. package/dist/src/core/loggingContentGenerator.js +5 -5
  44. package/dist/src/core/loggingContentGenerator.js.map +1 -1
  45. package/dist/src/core/nonInteractiveToolExecutor.test.js +1 -0
  46. package/dist/src/core/nonInteractiveToolExecutor.test.js.map +1 -1
  47. package/dist/src/generated/git-commit.d.ts +2 -2
  48. package/dist/src/generated/git-commit.js +2 -2
  49. package/dist/src/generated/git-commit.js.map +1 -1
  50. package/dist/src/ide/constants.d.ts +1 -0
  51. package/dist/src/ide/constants.js +1 -0
  52. package/dist/src/ide/constants.js.map +1 -1
  53. package/dist/src/ide/detect-ide.d.ts +44 -14
  54. package/dist/src/ide/detect-ide.js +29 -69
  55. package/dist/src/ide/detect-ide.js.map +1 -1
  56. package/dist/src/ide/detect-ide.test.js +29 -46
  57. package/dist/src/ide/detect-ide.test.js.map +1 -1
  58. package/dist/src/ide/ide-client.d.ts +28 -17
  59. package/dist/src/ide/ide-client.js +125 -57
  60. package/dist/src/ide/ide-client.js.map +1 -1
  61. package/dist/src/ide/ide-client.test.js +44 -10
  62. package/dist/src/ide/ide-client.test.js.map +1 -1
  63. package/dist/src/ide/ide-installer.d.ts +2 -2
  64. package/dist/src/ide/ide-installer.js +15 -11
  65. package/dist/src/ide/ide-installer.js.map +1 -1
  66. package/dist/src/ide/ide-installer.test.js +30 -12
  67. package/dist/src/ide/ide-installer.test.js.map +1 -1
  68. package/dist/src/ide/ideContext.d.ts +0 -93
  69. package/dist/src/ide/ideContext.js +0 -45
  70. package/dist/src/ide/ideContext.js.map +1 -1
  71. package/dist/src/ide/types.d.ts +141 -0
  72. package/dist/src/ide/types.js +73 -0
  73. package/dist/src/ide/types.js.map +1 -1
  74. package/dist/src/index.d.ts +4 -2
  75. package/dist/src/index.js +4 -2
  76. package/dist/src/index.js.map +1 -1
  77. package/dist/src/mcp/oauth-provider.d.ts +4 -1
  78. package/dist/src/mcp/oauth-provider.js +32 -26
  79. package/dist/src/mcp/oauth-provider.js.map +1 -1
  80. package/dist/src/mcp/oauth-token-storage.d.ts +2 -0
  81. package/dist/src/mcp/oauth-token-storage.js +25 -0
  82. package/dist/src/mcp/oauth-token-storage.js.map +1 -1
  83. package/dist/src/mcp/oauth-token-storage.test.js +251 -160
  84. package/dist/src/mcp/oauth-token-storage.test.js.map +1 -1
  85. package/dist/src/mcp/token-storage/index.d.ts +11 -0
  86. package/dist/src/mcp/token-storage/index.js +12 -0
  87. package/dist/src/mcp/token-storage/index.js.map +1 -0
  88. package/dist/src/policy/policy-engine.js +11 -2
  89. package/dist/src/policy/policy-engine.js.map +1 -1
  90. package/dist/src/policy/policy-engine.test.js +45 -0
  91. package/dist/src/policy/policy-engine.test.js.map +1 -1
  92. package/dist/src/routing/modelRouterService.js +37 -3
  93. package/dist/src/routing/modelRouterService.js.map +1 -1
  94. package/dist/src/routing/modelRouterService.test.js +37 -11
  95. package/dist/src/routing/modelRouterService.test.js.map +1 -1
  96. package/dist/src/routing/strategies/classifierStrategy.d.ts +12 -0
  97. package/dist/src/routing/strategies/classifierStrategy.js +173 -0
  98. package/dist/src/routing/strategies/classifierStrategy.js.map +1 -0
  99. package/dist/src/routing/strategies/classifierStrategy.test.d.ts +6 -0
  100. package/dist/src/routing/strategies/classifierStrategy.test.js +192 -0
  101. package/dist/src/routing/strategies/classifierStrategy.test.js.map +1 -0
  102. package/dist/src/routing/strategies/compositeStrategy.js +4 -3
  103. package/dist/src/routing/strategies/compositeStrategy.js.map +1 -1
  104. package/dist/src/routing/strategies/overrideStrategy.js +13 -12
  105. package/dist/src/routing/strategies/overrideStrategy.js.map +1 -1
  106. package/dist/src/routing/strategies/overrideStrategy.test.js +3 -2
  107. package/dist/src/routing/strategies/overrideStrategy.test.js.map +1 -1
  108. package/dist/src/services/chatRecordingService.d.ts +2 -1
  109. package/dist/src/services/chatRecordingService.js +3 -3
  110. package/dist/src/services/chatRecordingService.js.map +1 -1
  111. package/dist/src/services/chatRecordingService.test.js +8 -3
  112. package/dist/src/services/chatRecordingService.test.js.map +1 -1
  113. package/dist/src/services/gitService.js +9 -12
  114. package/dist/src/services/gitService.js.map +1 -1
  115. package/dist/src/services/gitService.test.js +10 -20
  116. package/dist/src/services/gitService.test.js.map +1 -1
  117. package/dist/src/services/loopDetectionService.js +23 -18
  118. package/dist/src/services/loopDetectionService.js.map +1 -1
  119. package/dist/src/services/loopDetectionService.test.js +27 -13
  120. package/dist/src/services/loopDetectionService.test.js.map +1 -1
  121. package/dist/src/services/shellExecutionService.js +30 -15
  122. package/dist/src/services/shellExecutionService.js.map +1 -1
  123. package/dist/src/services/shellExecutionService.test.js +43 -11
  124. package/dist/src/services/shellExecutionService.test.js.map +1 -1
  125. package/dist/src/telemetry/activity-detector.d.ts +41 -0
  126. package/dist/src/telemetry/activity-detector.js +61 -0
  127. package/dist/src/telemetry/activity-detector.js.map +1 -0
  128. package/dist/src/telemetry/activity-detector.test.d.ts +6 -0
  129. package/dist/src/telemetry/activity-detector.test.js +136 -0
  130. package/dist/src/telemetry/activity-detector.test.js.map +1 -0
  131. package/dist/src/telemetry/activity-types.d.ts +19 -0
  132. package/dist/src/telemetry/activity-types.js +21 -0
  133. package/dist/src/telemetry/activity-types.js.map +1 -0
  134. package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +14 -2
  135. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +109 -3
  136. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
  137. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js +63 -5
  138. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js.map +1 -1
  139. package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +12 -1
  140. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +27 -0
  141. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -1
  142. package/dist/src/telemetry/config.d.ts +31 -0
  143. package/dist/src/telemetry/config.js +76 -0
  144. package/dist/src/telemetry/config.js.map +1 -0
  145. package/dist/src/telemetry/config.test.d.ts +6 -0
  146. package/dist/src/telemetry/config.test.js +124 -0
  147. package/dist/src/telemetry/config.test.js.map +1 -0
  148. package/dist/src/telemetry/constants.d.ts +9 -0
  149. package/dist/src/telemetry/constants.js +9 -0
  150. package/dist/src/telemetry/constants.js.map +1 -1
  151. package/dist/src/telemetry/gcp-exporters.d.ts +34 -0
  152. package/dist/src/telemetry/gcp-exporters.js +117 -0
  153. package/dist/src/telemetry/gcp-exporters.js.map +1 -0
  154. package/dist/src/telemetry/gcp-exporters.test.d.ts +6 -0
  155. package/dist/src/telemetry/gcp-exporters.test.js +318 -0
  156. package/dist/src/telemetry/gcp-exporters.test.js.map +1 -0
  157. package/dist/src/telemetry/index.d.ts +5 -1
  158. package/dist/src/telemetry/index.js +5 -1
  159. package/dist/src/telemetry/index.js.map +1 -1
  160. package/dist/src/telemetry/loggers.d.ts +8 -1
  161. package/dist/src/telemetry/loggers.js +111 -2
  162. package/dist/src/telemetry/loggers.js.map +1 -1
  163. package/dist/src/telemetry/loggers.test.js +207 -4
  164. package/dist/src/telemetry/loggers.test.js.map +1 -1
  165. package/dist/src/telemetry/metrics.d.ts +3 -0
  166. package/dist/src/telemetry/metrics.js +43 -1
  167. package/dist/src/telemetry/metrics.js.map +1 -1
  168. package/dist/src/telemetry/metrics.test.js +42 -0
  169. package/dist/src/telemetry/metrics.test.js.map +1 -1
  170. package/dist/src/telemetry/sdk.js +20 -2
  171. package/dist/src/telemetry/sdk.js.map +1 -1
  172. package/dist/src/telemetry/sdk.test.js +108 -0
  173. package/dist/src/telemetry/sdk.test.js.map +1 -1
  174. package/dist/src/telemetry/types.d.ts +50 -3
  175. package/dist/src/telemetry/types.js +91 -6
  176. package/dist/src/telemetry/types.js.map +1 -1
  177. package/dist/src/telemetry/uiTelemetry.d.ts +1 -1
  178. package/dist/src/telemetry/uiTelemetry.js +2 -3
  179. package/dist/src/telemetry/uiTelemetry.js.map +1 -1
  180. package/dist/src/telemetry/uiTelemetry.test.js +11 -11
  181. package/dist/src/telemetry/uiTelemetry.test.js.map +1 -1
  182. package/dist/src/tools/edit.js +4 -2
  183. package/dist/src/tools/edit.js.map +1 -1
  184. package/dist/src/tools/edit.test.js +77 -1
  185. package/dist/src/tools/edit.test.js.map +1 -1
  186. package/dist/src/tools/message-bus-integration.test.d.ts +6 -0
  187. package/dist/src/tools/message-bus-integration.test.js +183 -0
  188. package/dist/src/tools/message-bus-integration.test.js.map +1 -0
  189. package/dist/src/tools/shell.js +8 -11
  190. package/dist/src/tools/shell.js.map +1 -1
  191. package/dist/src/tools/shell.test.js +33 -37
  192. package/dist/src/tools/shell.test.js.map +1 -1
  193. package/dist/src/tools/smart-edit.d.ts +0 -1
  194. package/dist/src/tools/smart-edit.js +3 -15
  195. package/dist/src/tools/smart-edit.js.map +1 -1
  196. package/dist/src/tools/smart-edit.test.js +16 -1
  197. package/dist/src/tools/smart-edit.test.js.map +1 -1
  198. package/dist/src/tools/tool-registry.js +1 -0
  199. package/dist/src/tools/tool-registry.js.map +1 -1
  200. package/dist/src/tools/tools.d.ts +13 -4
  201. package/dist/src/tools/tools.js +101 -3
  202. package/dist/src/tools/tools.js.map +1 -1
  203. package/dist/src/tools/write-file.js +2 -2
  204. package/dist/src/tools/write-file.js.map +1 -1
  205. package/dist/src/tools/write-file.test.js +16 -10
  206. package/dist/src/tools/write-file.test.js.map +1 -1
  207. package/dist/src/tools/write-todos.d.ts +25 -0
  208. package/dist/src/tools/write-todos.js +150 -0
  209. package/dist/src/tools/write-todos.js.map +1 -0
  210. package/dist/src/tools/write-todos.test.d.ts +6 -0
  211. package/dist/src/tools/write-todos.test.js +89 -0
  212. package/dist/src/tools/write-todos.test.js.map +1 -0
  213. package/dist/src/utils/editCorrector.d.ts +7 -6
  214. package/dist/src/utils/editCorrector.js +61 -18
  215. package/dist/src/utils/editCorrector.js.map +1 -1
  216. package/dist/src/utils/editCorrector.test.js +30 -79
  217. package/dist/src/utils/editCorrector.test.js.map +1 -1
  218. package/dist/src/utils/editor.js +31 -44
  219. package/dist/src/utils/editor.js.map +1 -1
  220. package/dist/src/utils/editor.test.js +61 -75
  221. package/dist/src/utils/editor.test.js.map +1 -1
  222. package/dist/src/utils/errorParsing.js +2 -2
  223. package/dist/src/utils/errorParsing.js.map +1 -1
  224. package/dist/src/utils/errorParsing.test.js +7 -7
  225. package/dist/src/utils/errorParsing.test.js.map +1 -1
  226. package/dist/src/utils/fileUtils.test.js +17 -8
  227. package/dist/src/utils/fileUtils.test.js.map +1 -1
  228. package/dist/src/utils/memoryDiscovery.test.js +12 -6
  229. package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
  230. package/dist/src/utils/nextSpeakerChecker.d.ts +2 -2
  231. package/dist/src/utils/nextSpeakerChecker.js +8 -2
  232. package/dist/src/utils/nextSpeakerChecker.js.map +1 -1
  233. package/dist/src/utils/nextSpeakerChecker.test.js +40 -33
  234. package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -1
  235. package/dist/src/utils/shell-utils.d.ts +5 -0
  236. package/dist/src/utils/shell-utils.js +23 -0
  237. package/dist/src/utils/shell-utils.js.map +1 -1
  238. package/dist/src/utils/textUtils.d.ts +5 -0
  239. package/dist/src/utils/textUtils.js +14 -0
  240. package/dist/src/utils/textUtils.js.map +1 -1
  241. package/dist/src/utils/textUtils.test.d.ts +6 -0
  242. package/dist/src/utils/textUtils.test.js +59 -0
  243. package/dist/src/utils/textUtils.test.js.map +1 -0
  244. package/dist/tsconfig.tsbuildinfo +1 -1
  245. package/package.json +5 -1
  246. package/dist/google-gemini-cli-core-0.6.0-nightly.tgz +0 -0
@@ -4,11 +4,13 @@
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
  import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
7
- import { GeminiChat, EmptyStreamError, StreamEventType, } from './geminiChat.js';
7
+ import { GeminiChat, InvalidStreamError, StreamEventType, } from './geminiChat.js';
8
8
  import { setSimulate429 } from '../utils/testUtils.js';
9
9
  import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
10
10
  import { AuthType } from './contentGenerator.js';
11
11
  import {} from '../utils/retry.js';
12
+ import { Kind } from '../tools/tools.js';
13
+ import { uiTelemetryService } from '../telemetry/uiTelemetry.js';
12
14
  // Mock fs module to prevent actual file system operations during tests
13
15
  const mockFileSystem = new Map();
14
16
  vi.mock('node:fs', () => {
@@ -45,16 +47,19 @@ vi.mock('../utils/retry.js', () => ({
45
47
  vi.mock('../fallback/handler.js', () => ({
46
48
  handleFallback: mockHandleFallback,
47
49
  }));
48
- const { mockLogInvalidChunk, mockLogContentRetry, mockLogContentRetryFailure } = vi.hoisted(() => ({
49
- mockLogInvalidChunk: vi.fn(),
50
+ const { mockLogContentRetry, mockLogContentRetryFailure } = vi.hoisted(() => ({
50
51
  mockLogContentRetry: vi.fn(),
51
52
  mockLogContentRetryFailure: vi.fn(),
52
53
  }));
53
54
  vi.mock('../telemetry/loggers.js', () => ({
54
- logInvalidChunk: mockLogInvalidChunk,
55
55
  logContentRetry: mockLogContentRetry,
56
56
  logContentRetryFailure: mockLogContentRetryFailure,
57
57
  }));
58
+ vi.mock('../telemetry/uiTelemetry.js', () => ({
59
+ uiTelemetryService: {
60
+ setLastPromptTokenCount: vi.fn(),
61
+ },
62
+ }));
58
63
  describe('GeminiChat', () => {
59
64
  let mockContentGenerator;
60
65
  let chat;
@@ -62,6 +67,7 @@ describe('GeminiChat', () => {
62
67
  const config = {};
63
68
  beforeEach(() => {
64
69
  vi.clearAllMocks();
70
+ vi.mocked(uiTelemetryService.setLastPromptTokenCount).mockClear();
65
71
  mockContentGenerator = {
66
72
  generateContent: vi.fn(),
67
73
  generateContentStream: vi.fn(),
@@ -179,7 +185,7 @@ describe('GeminiChat', () => {
179
185
  for await (const _ of stream) {
180
186
  /* consume stream */
181
187
  }
182
- })()).rejects.toThrow(EmptyStreamError);
188
+ })()).rejects.toThrow(InvalidStreamError);
183
189
  });
184
190
  it('should succeed if the stream ends with an invalid part but has a finishReason and contained a valid part', async () => {
185
191
  // 1. Mock a stream that sends a valid chunk, then an invalid one, but has a finish reason.
@@ -222,55 +228,6 @@ describe('GeminiChat', () => {
222
228
  expect(modelTurn?.parts?.length).toBe(1);
223
229
  expect(modelTurn?.parts[0].text).toBe('Initial valid content...');
224
230
  });
225
- it('should not consolidate text into a part that also contains a functionCall', async () => {
226
- // 1. Mock the API to stream a malformed part followed by a valid text part.
227
- const multiChunkStream = (async function* () {
228
- // This malformed part has both text and a functionCall.
229
- yield {
230
- candidates: [
231
- {
232
- content: {
233
- role: 'model',
234
- parts: [
235
- {
236
- text: 'Some text',
237
- functionCall: { name: 'do_stuff', args: {} },
238
- },
239
- ],
240
- },
241
- },
242
- ],
243
- };
244
- // This valid text part should NOT be merged into the malformed one.
245
- yield {
246
- candidates: [
247
- {
248
- content: {
249
- role: 'model',
250
- parts: [{ text: ' that should not be merged.' }],
251
- },
252
- },
253
- ],
254
- };
255
- })();
256
- vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
257
- // 2. Action: Send a message and consume the stream.
258
- const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-malformed-chunk');
259
- for await (const _ of stream) {
260
- // Consume the stream to trigger history recording.
261
- }
262
- // 3. Assert: Check that the final history was not incorrectly consolidated.
263
- const history = chat.getHistory();
264
- expect(history.length).toBe(2);
265
- const modelTurn = history[1];
266
- // CRUCIAL ASSERTION: There should be two separate parts.
267
- // The old, non-strict logic would incorrectly merge them, resulting in one part.
268
- expect(modelTurn?.parts?.length).toBe(2);
269
- // Verify the contents of each part.
270
- expect(modelTurn?.parts[0].text).toBe('Some text');
271
- expect(modelTurn?.parts[0].functionCall).toBeDefined();
272
- expect(modelTurn?.parts[1].text).toBe(' that should not be merged.');
273
- });
274
231
  it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
275
232
  // 1. Mock the API to return a stream where one chunk is just an empty text part.
276
233
  const multiChunkStream = (async function* () {
@@ -393,7 +350,7 @@ describe('GeminiChat', () => {
393
350
  expect(modelTurn?.parts?.length).toBe(1);
394
351
  expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
395
352
  });
396
- it('should add a placeholder model turn when a tool call is followed by an empty stream response', async () => {
353
+ it('should throw an error when a tool call is followed by an empty stream response', async () => {
397
354
  // 1. Setup: A history where the model has just made a function call.
398
355
  const initialHistory = [
399
356
  {
@@ -434,20 +391,113 @@ describe('GeminiChat', () => {
434
391
  },
435
392
  },
436
393
  }, 'prompt-id-stream-1');
437
- for await (const _ of stream) {
438
- // This loop consumes the stream to trigger the internal logic.
439
- }
440
- // 4. Assert: The history should now have four valid, alternating turns.
441
- const history = chat.getHistory();
442
- expect(history.length).toBe(4);
443
- // The final turn must be the empty model placeholder.
444
- const lastTurn = history[3];
445
- expect(lastTurn.role).toBe('model');
446
- expect(lastTurn?.parts?.length).toBe(0);
447
- // The second-to-last turn must be the function response we sent.
448
- const secondToLastTurn = history[2];
449
- expect(secondToLastTurn.role).toBe('user');
450
- expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
394
+ // 4. Assert: The stream processing should throw an InvalidStreamError.
395
+ await expect((async () => {
396
+ for await (const _ of stream) {
397
+ // This loop consumes the stream to trigger the internal logic.
398
+ }
399
+ })()).rejects.toThrow(InvalidStreamError);
400
+ });
401
+ it('should succeed when there is a tool call without finish reason', async () => {
402
+ // Setup: Stream with tool call but no finish reason
403
+ const streamWithToolCall = (async function* () {
404
+ yield {
405
+ candidates: [
406
+ {
407
+ content: {
408
+ role: 'model',
409
+ parts: [
410
+ {
411
+ functionCall: {
412
+ name: 'test_function',
413
+ args: { param: 'value' },
414
+ },
415
+ },
416
+ ],
417
+ },
418
+ // No finishReason
419
+ },
420
+ ],
421
+ };
422
+ })();
423
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithToolCall);
424
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-1');
425
+ // Should not throw an error
426
+ await expect((async () => {
427
+ for await (const _ of stream) {
428
+ // consume stream
429
+ }
430
+ })()).resolves.not.toThrow();
431
+ });
432
+ it('should throw InvalidStreamError when no tool call and no finish reason', async () => {
433
+ // Setup: Stream with text but no finish reason and no tool call
434
+ const streamWithoutFinishReason = (async function* () {
435
+ yield {
436
+ candidates: [
437
+ {
438
+ content: {
439
+ role: 'model',
440
+ parts: [{ text: 'some response' }],
441
+ },
442
+ // No finishReason
443
+ },
444
+ ],
445
+ };
446
+ })();
447
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithoutFinishReason);
448
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-1');
449
+ await expect((async () => {
450
+ for await (const _ of stream) {
451
+ // consume stream
452
+ }
453
+ })()).rejects.toThrow(InvalidStreamError);
454
+ });
455
+ it('should throw InvalidStreamError when no tool call and empty response text', async () => {
456
+ // Setup: Stream with finish reason but empty response (only thoughts)
457
+ const streamWithEmptyResponse = (async function* () {
458
+ yield {
459
+ candidates: [
460
+ {
461
+ content: {
462
+ role: 'model',
463
+ parts: [{ thought: 'thinking...' }],
464
+ },
465
+ finishReason: 'STOP',
466
+ },
467
+ ],
468
+ };
469
+ })();
470
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithEmptyResponse);
471
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-1');
472
+ await expect((async () => {
473
+ for await (const _ of stream) {
474
+ // consume stream
475
+ }
476
+ })()).rejects.toThrow(InvalidStreamError);
477
+ });
478
+ it('should succeed when there is finish reason and response text', async () => {
479
+ // Setup: Stream with both finish reason and text content
480
+ const validStream = (async function* () {
481
+ yield {
482
+ candidates: [
483
+ {
484
+ content: {
485
+ role: 'model',
486
+ parts: [{ text: 'valid response' }],
487
+ },
488
+ finishReason: 'STOP',
489
+ },
490
+ ],
491
+ };
492
+ })();
493
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(validStream);
494
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-1');
495
+ // Should not throw an error
496
+ await expect((async () => {
497
+ for await (const _ of stream) {
498
+ // consume stream
499
+ }
500
+ })()).resolves.not.toThrow();
451
501
  });
452
502
  it('should call generateContentStream with the correct parameters', async () => {
453
503
  const response = (async function* () {
@@ -464,6 +514,11 @@ describe('GeminiChat', () => {
464
514
  },
465
515
  ],
466
516
  text: () => 'response',
517
+ usageMetadata: {
518
+ promptTokenCount: 42,
519
+ candidatesTokenCount: 15,
520
+ totalTokenCount: 57,
521
+ },
467
522
  };
468
523
  })();
469
524
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(response);
@@ -481,135 +536,9 @@ describe('GeminiChat', () => {
481
536
  ],
482
537
  config: {},
483
538
  }, 'prompt-id-1');
484
- });
485
- });
486
- describe('recordHistory', () => {
487
- const userInput = {
488
- role: 'user',
489
- parts: [{ text: 'User input' }],
490
- };
491
- it('should consolidate all consecutive model turns into a single turn', () => {
492
- const userInput = {
493
- role: 'user',
494
- parts: [{ text: 'User input' }],
495
- };
496
- // This simulates a multi-part model response with different part types.
497
- const modelOutput = [
498
- { role: 'model', parts: [{ text: 'Thinking...' }] },
499
- {
500
- role: 'model',
501
- parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
502
- },
503
- ];
504
- // @ts-expect-error Accessing private method for testing
505
- chat.recordHistory(userInput, modelOutput);
506
- const history = chat.getHistory();
507
- // The history should contain the user's turn and ONE consolidated model turn.
508
- // The old code would fail here, resulting in a length of 3.
509
- //expect(history).toBe([]);
510
- expect(history.length).toBe(2);
511
- const modelTurn = history[1];
512
- expect(modelTurn.role).toBe('model');
513
- // The consolidated turn should contain both the text part and the functionCall part.
514
- expect(modelTurn?.parts?.length).toBe(2);
515
- expect(modelTurn?.parts[0].text).toBe('Thinking...');
516
- expect(modelTurn?.parts[1].functionCall).toBeDefined();
517
- });
518
- it('should add a placeholder model turn when a tool call is followed by an empty response', () => {
519
- // 1. Setup: A history where the model has just made a function call.
520
- const initialHistory = [
521
- { role: 'user', parts: [{ text: 'Initial prompt' }] },
522
- {
523
- role: 'model',
524
- parts: [{ functionCall: { name: 'test_tool', args: {} } }],
525
- },
526
- ];
527
- chat.setHistory(initialHistory);
528
- // 2. Action: The user provides the tool's response, and the model's
529
- // final output is empty (e.g., just a thought, which gets filtered out).
530
- const functionResponse = {
531
- role: 'user',
532
- parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
533
- };
534
- const emptyModelOutput = [];
535
- // @ts-expect-error Accessing private method for testing
536
- chat.recordHistory(functionResponse, emptyModelOutput, [
537
- functionResponse,
538
- ]);
539
- // 3. Assert: The history should now have four valid, alternating turns.
540
- const history = chat.getHistory();
541
- expect(history.length).toBe(4);
542
- // The final turn must be the empty model placeholder.
543
- const lastTurn = history[3];
544
- expect(lastTurn.role).toBe('model');
545
- expect(lastTurn?.parts?.length).toBe(0);
546
- // The second-to-last turn must be the function response we provided.
547
- const secondToLastTurn = history[2];
548
- expect(secondToLastTurn.role).toBe('user');
549
- expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
550
- });
551
- it('should add user input and a single model output to history', () => {
552
- const modelOutput = [
553
- { role: 'model', parts: [{ text: 'Model output' }] },
554
- ];
555
- // @ts-expect-error Accessing private method for testing
556
- chat.recordHistory(userInput, modelOutput);
557
- const history = chat.getHistory();
558
- expect(history.length).toBe(2);
559
- expect(history[0]).toEqual(userInput);
560
- expect(history[1]).toEqual(modelOutput[0]);
561
- });
562
- it('should consolidate adjacent text parts from multiple content objects', () => {
563
- const modelOutput = [
564
- { role: 'model', parts: [{ text: 'Part 1.' }] },
565
- { role: 'model', parts: [{ text: ' Part 2.' }] },
566
- { role: 'model', parts: [{ text: ' Part 3.' }] },
567
- ];
568
- // @ts-expect-error Accessing private method for testing
569
- chat.recordHistory(userInput, modelOutput);
570
- const history = chat.getHistory();
571
- expect(history.length).toBe(2);
572
- expect(history[1].role).toBe('model');
573
- expect(history[1].parts).toEqual([{ text: 'Part 1. Part 2. Part 3.' }]);
574
- });
575
- it('should add an empty placeholder turn if modelOutput is empty', () => {
576
- // This simulates receiving a pre-filtered, thought-only response.
577
- const emptyModelOutput = [];
578
- // @ts-expect-error Accessing private method for testing
579
- chat.recordHistory(userInput, emptyModelOutput);
580
- const history = chat.getHistory();
581
- expect(history.length).toBe(2);
582
- expect(history[0]).toEqual(userInput);
583
- expect(history[1].role).toBe('model');
584
- expect(history[1].parts).toEqual([]);
585
- });
586
- it('should preserve model outputs with undefined or empty parts arrays', () => {
587
- const malformedOutput = [
588
- { role: 'model', parts: [{ text: 'Text part' }] },
589
- { role: 'model', parts: undefined },
590
- { role: 'model', parts: [] },
591
- ];
592
- // @ts-expect-error Accessing private method for testing
593
- chat.recordHistory(userInput, malformedOutput);
594
- const history = chat.getHistory();
595
- expect(history.length).toBe(4); // userInput + 3 model turns
596
- expect(history[1].parts).toEqual([{ text: 'Text part' }]);
597
- expect(history[2].parts).toBeUndefined();
598
- expect(history[3].parts).toEqual([]);
599
- });
600
- it('should not consolidate content with different roles', () => {
601
- const mixedOutput = [
602
- { role: 'model', parts: [{ text: 'Model 1' }] },
603
- { role: 'user', parts: [{ text: 'Unexpected User' }] },
604
- { role: 'model', parts: [{ text: 'Model 2' }] },
605
- ];
606
- // @ts-expect-error Accessing private method for testing
607
- chat.recordHistory(userInput, mixedOutput);
608
- const history = chat.getHistory();
609
- expect(history.length).toBe(4); // userInput, model1, unexpected_user, model2
610
- expect(history[1]).toEqual(mixedOutput[0]);
611
- expect(history[2]).toEqual(mixedOutput[1]);
612
- expect(history[3]).toEqual(mixedOutput[2]);
539
+ // Verify that token counting is called when usageMetadata is present
540
+ expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(42);
541
+ expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
613
542
  });
614
543
  });
615
544
  describe('addHistory', () => {
@@ -702,7 +631,6 @@ describe('GeminiChat', () => {
702
631
  chunks.push(chunk);
703
632
  }
704
633
  // Assertions
705
- expect(mockLogInvalidChunk).toHaveBeenCalledTimes(1);
706
634
  expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
707
635
  expect(mockLogContentRetryFailure).not.toHaveBeenCalled();
708
636
  expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
@@ -723,6 +651,8 @@ describe('GeminiChat', () => {
723
651
  role: 'model',
724
652
  parts: [{ text: 'Successful response' }],
725
653
  });
654
+ // Verify that token counting is not called when usageMetadata is missing
655
+ expect(uiTelemetryService.setLastPromptTokenCount).not.toHaveBeenCalled();
726
656
  });
727
657
  it('should fail after all retries on persistent invalid content and report metrics', async () => {
728
658
  vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
@@ -742,11 +672,10 @@ describe('GeminiChat', () => {
742
672
  for await (const _ of stream) {
743
673
  // Must loop to trigger the internal logic that throws.
744
674
  }
745
- }).rejects.toThrow(EmptyStreamError);
746
- // Should be called 3 times (initial + 2 retries)
747
- expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(3);
748
- expect(mockLogInvalidChunk).toHaveBeenCalledTimes(3);
749
- expect(mockLogContentRetry).toHaveBeenCalledTimes(2);
675
+ }).rejects.toThrow(InvalidStreamError);
676
+ // Should be called 2 times (initial + 1 retry)
677
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
678
+ expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
750
679
  expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1);
751
680
  // History should be clean, as if the failed turn never happened.
752
681
  const history = chat.getHistory();
@@ -922,6 +851,215 @@ describe('GeminiChat', () => {
922
851
  }
923
852
  expect(turn4.parts[0].text).toBe('second response');
924
853
  });
854
+ describe('stopBeforeSecondMutator', () => {
855
+ beforeEach(() => {
856
+ // Common setup for these tests: mock the tool registry.
857
+ const mockToolRegistry = {
858
+ getTool: vi.fn((toolName) => {
859
+ if (toolName === 'edit') {
860
+ return { kind: Kind.Edit };
861
+ }
862
+ return { kind: Kind.Other };
863
+ }),
864
+ };
865
+ vi.mocked(mockConfig.getToolRegistry).mockReturnValue(mockToolRegistry);
866
+ });
867
+ it('should stop streaming before a second mutator tool call', async () => {
868
+ const responses = [
869
+ {
870
+ candidates: [
871
+ { content: { role: 'model', parts: [{ text: 'First part. ' }] } },
872
+ ],
873
+ },
874
+ {
875
+ candidates: [
876
+ {
877
+ content: {
878
+ role: 'model',
879
+ parts: [{ functionCall: { name: 'edit', args: {} } }],
880
+ },
881
+ },
882
+ ],
883
+ },
884
+ {
885
+ candidates: [
886
+ {
887
+ content: {
888
+ role: 'model',
889
+ parts: [{ functionCall: { name: 'fetch', args: {} } }],
890
+ },
891
+ },
892
+ ],
893
+ },
894
+ // This chunk contains the second mutator and should be clipped.
895
+ {
896
+ candidates: [
897
+ {
898
+ content: {
899
+ role: 'model',
900
+ parts: [
901
+ { functionCall: { name: 'edit', args: {} } },
902
+ { text: 'some trailing text' },
903
+ ],
904
+ },
905
+ },
906
+ ],
907
+ },
908
+ // This chunk should never be reached.
909
+ {
910
+ candidates: [
911
+ {
912
+ content: {
913
+ role: 'model',
914
+ parts: [{ text: 'This should not appear.' }],
915
+ },
916
+ },
917
+ ],
918
+ },
919
+ ];
920
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
921
+ for (const response of responses) {
922
+ yield response;
923
+ }
924
+ })());
925
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-mutator-test');
926
+ for await (const _ of stream) {
927
+ // Consume the stream to trigger history recording.
928
+ }
929
+ const history = chat.getHistory();
930
+ expect(history.length).toBe(2);
931
+ const modelTurn = history[1];
932
+ expect(modelTurn.role).toBe('model');
933
+ expect(modelTurn?.parts?.length).toBe(3);
934
+ expect(modelTurn?.parts[0].text).toBe('First part. ');
935
+ expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
936
+ expect(modelTurn.parts[2].functionCall?.name).toBe('fetch');
937
+ });
938
+ it('should not stop streaming if only one mutator is present', async () => {
939
+ const responses = [
940
+ {
941
+ candidates: [
942
+ { content: { role: 'model', parts: [{ text: 'Part 1. ' }] } },
943
+ ],
944
+ },
945
+ {
946
+ candidates: [
947
+ {
948
+ content: {
949
+ role: 'model',
950
+ parts: [{ functionCall: { name: 'edit', args: {} } }],
951
+ },
952
+ },
953
+ ],
954
+ },
955
+ {
956
+ candidates: [
957
+ {
958
+ content: {
959
+ role: 'model',
960
+ parts: [{ text: 'Part 2.' }],
961
+ },
962
+ finishReason: 'STOP',
963
+ },
964
+ ],
965
+ },
966
+ ];
967
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
968
+ for (const response of responses) {
969
+ yield response;
970
+ }
971
+ })());
972
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-one-mutator');
973
+ for await (const _ of stream) {
974
+ /* consume */
975
+ }
976
+ const history = chat.getHistory();
977
+ const modelTurn = history[1];
978
+ expect(modelTurn?.parts?.length).toBe(3);
979
+ expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
980
+ expect(modelTurn.parts[2].text).toBe('Part 2.');
981
+ });
982
+ it('should clip the chunk containing the second mutator, preserving prior parts', async () => {
983
+ const responses = [
984
+ {
985
+ candidates: [
986
+ {
987
+ content: {
988
+ role: 'model',
989
+ parts: [{ functionCall: { name: 'edit', args: {} } }],
990
+ },
991
+ },
992
+ ],
993
+ },
994
+ // This chunk has a valid part before the second mutator.
995
+ // The valid part should be kept, the rest of the chunk discarded.
996
+ {
997
+ candidates: [
998
+ {
999
+ content: {
1000
+ role: 'model',
1001
+ parts: [
1002
+ { text: 'Keep this text. ' },
1003
+ { functionCall: { name: 'edit', args: {} } },
1004
+ { text: 'Discard this text.' },
1005
+ ],
1006
+ },
1007
+ finishReason: 'STOP',
1008
+ },
1009
+ ],
1010
+ },
1011
+ ];
1012
+ const stream = (async function* () {
1013
+ for (const response of responses) {
1014
+ yield response;
1015
+ }
1016
+ })();
1017
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
1018
+ const resultStream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-clip-chunk');
1019
+ for await (const _ of resultStream) {
1020
+ /* consume */
1021
+ }
1022
+ const history = chat.getHistory();
1023
+ const modelTurn = history[1];
1024
+ expect(modelTurn?.parts?.length).toBe(2);
1025
+ expect(modelTurn.parts[0].functionCall?.name).toBe('edit');
1026
+ expect(modelTurn.parts[1].text).toBe('Keep this text. ');
1027
+ });
1028
+ it('should handle two mutators in the same chunk (parallel call scenario)', async () => {
1029
+ const responses = [
1030
+ {
1031
+ candidates: [
1032
+ {
1033
+ content: {
1034
+ role: 'model',
1035
+ parts: [
1036
+ { text: 'Some text. ' },
1037
+ { functionCall: { name: 'edit', args: {} } },
1038
+ { functionCall: { name: 'edit', args: {} } },
1039
+ ],
1040
+ },
1041
+ finishReason: 'STOP',
1042
+ },
1043
+ ],
1044
+ },
1045
+ ];
1046
+ const stream = (async function* () {
1047
+ for (const response of responses) {
1048
+ yield response;
1049
+ }
1050
+ })();
1051
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
1052
+ const resultStream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-parallel-mutators');
1053
+ for await (const _ of resultStream) {
1054
+ /* consume */
1055
+ }
1056
+ const history = chat.getHistory();
1057
+ const modelTurn = history[1];
1058
+ expect(modelTurn?.parts?.length).toBe(2);
1059
+ expect(modelTurn.parts[0].text).toBe('Some text. ');
1060
+ expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
1061
+ });
1062
+ });
925
1063
  describe('Model Resolution', () => {
926
1064
  const mockResponse = {
927
1065
  candidates: [