@google/gemini-cli-core 0.1.12 → 0.1.13
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.
- package/README.md +8 -2
- package/dist/google-gemini-cli-core-0.1.12.tgz +0 -0
- package/dist/src/code_assist/oauth2.js +39 -5
- package/dist/src/code_assist/oauth2.js.map +1 -1
- package/dist/src/code_assist/oauth2.test.js +8 -3
- package/dist/src/code_assist/oauth2.test.js.map +1 -1
- package/dist/src/code_assist/server.test.js +10 -2
- package/dist/src/code_assist/server.test.js.map +1 -1
- package/dist/src/config/config.d.ts +28 -5
- package/dist/src/config/config.js +34 -18
- package/dist/src/config/config.js.map +1 -1
- package/dist/src/config/config.test.js +28 -3
- package/dist/src/config/config.test.js.map +1 -1
- package/dist/src/core/client.d.ts +3 -1
- package/dist/src/core/client.js +38 -1
- package/dist/src/core/client.js.map +1 -1
- package/dist/src/core/client.test.js +94 -37
- package/dist/src/core/client.test.js.map +1 -1
- package/dist/src/core/contentGenerator.d.ts +2 -1
- package/dist/src/core/contentGenerator.js +4 -3
- package/dist/src/core/contentGenerator.js.map +1 -1
- package/dist/src/core/contentGenerator.test.js +12 -5
- package/dist/src/core/contentGenerator.test.js.map +1 -1
- package/dist/src/core/coreToolScheduler.js +14 -1
- package/dist/src/core/coreToolScheduler.js.map +1 -1
- package/dist/src/core/coreToolScheduler.test.js +84 -2
- package/dist/src/core/coreToolScheduler.test.js.map +1 -1
- package/dist/src/core/modelCheck.d.ts +1 -1
- package/dist/src/core/modelCheck.js +10 -3
- package/dist/src/core/modelCheck.js.map +1 -1
- package/dist/src/core/nonInteractiveToolExecutor.test.js +3 -0
- package/dist/src/core/nonInteractiveToolExecutor.test.js.map +1 -1
- package/dist/src/core/turn.d.ts +6 -2
- package/dist/src/core/turn.js +1 -0
- package/dist/src/core/turn.js.map +1 -1
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +8 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/oauth-provider.d.ts +142 -0
- package/dist/src/mcp/oauth-provider.js +446 -0
- package/dist/src/mcp/oauth-provider.js.map +1 -0
- package/dist/src/mcp/oauth-provider.test.d.ts +6 -0
- package/dist/src/mcp/oauth-provider.test.js +520 -0
- package/dist/src/mcp/oauth-provider.test.js.map +1 -0
- package/dist/src/mcp/oauth-token-storage.d.ts +81 -0
- package/dist/src/mcp/oauth-token-storage.js +149 -0
- package/dist/src/mcp/oauth-token-storage.js.map +1 -0
- package/dist/src/mcp/oauth-token-storage.test.d.ts +6 -0
- package/dist/src/mcp/oauth-token-storage.test.js +205 -0
- package/dist/src/mcp/oauth-token-storage.test.js.map +1 -0
- package/dist/src/mcp/oauth-utils.d.ts +109 -0
- package/dist/src/mcp/oauth-utils.js +183 -0
- package/dist/src/mcp/oauth-utils.js.map +1 -0
- package/dist/src/mcp/oauth-utils.test.d.ts +6 -0
- package/dist/src/mcp/oauth-utils.test.js +144 -0
- package/dist/src/mcp/oauth-utils.test.js.map +1 -0
- package/dist/src/services/gitService.js +1 -5
- package/dist/src/services/gitService.js.map +1 -1
- package/dist/src/services/gitService.test.js +1 -6
- package/dist/src/services/gitService.test.js.map +1 -1
- package/dist/src/services/ideContext.d.ts +126 -0
- package/dist/src/services/ideContext.js +98 -0
- package/dist/src/services/ideContext.js.map +1 -0
- package/dist/src/services/ideContext.test.d.ts +6 -0
- package/dist/src/services/ideContext.test.js +111 -0
- package/dist/src/services/ideContext.test.js.map +1 -0
- package/dist/src/services/loopDetectionService.d.ts +51 -0
- package/dist/src/services/loopDetectionService.js +232 -0
- package/dist/src/services/loopDetectionService.js.map +1 -0
- package/dist/src/services/loopDetectionService.test.d.ts +6 -0
- package/dist/src/services/loopDetectionService.test.js +339 -0
- package/dist/src/services/loopDetectionService.test.js.map +1 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +4 -1
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +54 -4
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +4 -1
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +9 -0
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -1
- package/dist/src/telemetry/integration.test.circular.d.ts +6 -0
- package/dist/src/telemetry/integration.test.circular.js +53 -0
- package/dist/src/telemetry/integration.test.circular.js.map +1 -0
- package/dist/src/telemetry/loggers.d.ts +2 -1
- package/dist/src/telemetry/loggers.js +17 -1
- package/dist/src/telemetry/loggers.js.map +1 -1
- package/dist/src/telemetry/loggers.test.circular.d.ts +6 -0
- package/dist/src/telemetry/loggers.test.circular.js +100 -0
- package/dist/src/telemetry/loggers.test.circular.js.map +1 -0
- package/dist/src/telemetry/types.d.ts +12 -1
- package/dist/src/telemetry/types.js +16 -0
- package/dist/src/telemetry/types.js.map +1 -1
- package/dist/src/telemetry/uiTelemetry.d.ts +1 -0
- package/dist/src/telemetry/uiTelemetry.js +7 -0
- package/dist/src/telemetry/uiTelemetry.js.map +1 -1
- package/dist/src/telemetry/uiTelemetry.test.js +92 -0
- package/dist/src/telemetry/uiTelemetry.test.js.map +1 -1
- package/dist/src/tools/edit.d.ts +7 -12
- package/dist/src/tools/edit.js +24 -28
- package/dist/src/tools/edit.js.map +1 -1
- package/dist/src/tools/glob.d.ts +1 -14
- package/dist/src/tools/glob.js +13 -36
- package/dist/src/tools/glob.js.map +1 -1
- package/dist/src/tools/glob.test.js +4 -3
- package/dist/src/tools/glob.test.js.map +1 -1
- package/dist/src/tools/grep.d.ts +3 -6
- package/dist/src/tools/grep.js +12 -18
- package/dist/src/tools/grep.js.map +1 -1
- package/dist/src/tools/grep.test.js +4 -1
- package/dist/src/tools/grep.test.js.map +1 -1
- package/dist/src/tools/ls.d.ts +1 -12
- package/dist/src/tools/ls.js +8 -30
- package/dist/src/tools/ls.js.map +1 -1
- package/dist/src/tools/mcp-client.d.ts +55 -1
- package/dist/src/tools/mcp-client.js +165 -140
- package/dist/src/tools/mcp-client.js.map +1 -1
- package/dist/src/tools/mcp-client.test.js +127 -624
- package/dist/src/tools/mcp-client.test.js.map +1 -1
- package/dist/src/tools/mcp-tool.d.ts +11 -5
- package/dist/src/tools/mcp-tool.js +33 -9
- package/dist/src/tools/mcp-tool.js.map +1 -1
- package/dist/src/tools/mcp-tool.test.js +40 -24
- package/dist/src/tools/mcp-tool.test.js.map +1 -1
- package/dist/src/tools/memoryTool.js +2 -2
- package/dist/src/tools/memoryTool.js.map +1 -1
- package/dist/src/tools/read-file.d.ts +3 -3
- package/dist/src/tools/read-file.js +10 -10
- package/dist/src/tools/read-file.js.map +1 -1
- package/dist/src/tools/read-file.test.js +2 -1
- package/dist/src/tools/read-file.test.js.map +1 -1
- package/dist/src/tools/read-many-files.d.ts +1 -7
- package/dist/src/tools/read-many-files.js +12 -21
- package/dist/src/tools/read-many-files.js.map +1 -1
- package/dist/src/tools/read-many-files.test.js +2 -1
- package/dist/src/tools/read-many-files.test.js.map +1 -1
- package/dist/src/tools/shell.js +11 -4
- package/dist/src/tools/shell.js.map +1 -1
- package/dist/src/tools/shell.test.js +60 -0
- package/dist/src/tools/shell.test.js.map +1 -1
- package/dist/src/tools/tool-registry.d.ts +0 -1
- package/dist/src/tools/tool-registry.js +12 -9
- package/dist/src/tools/tool-registry.js.map +1 -1
- package/dist/src/tools/tool-registry.test.js +109 -38
- package/dist/src/tools/tool-registry.test.js.map +1 -1
- package/dist/src/tools/tools.d.ts +37 -2
- package/dist/src/tools/tools.js +25 -2
- package/dist/src/tools/tools.js.map +1 -1
- package/dist/src/tools/web-fetch.js +7 -2
- package/dist/src/tools/web-fetch.js.map +1 -1
- package/dist/src/tools/web-fetch.test.js +1 -0
- package/dist/src/tools/web-fetch.test.js.map +1 -1
- package/dist/src/tools/web-search.js +2 -2
- package/dist/src/tools/web-search.js.map +1 -1
- package/dist/src/tools/write-file.d.ts +0 -8
- package/dist/src/tools/write-file.js +14 -23
- package/dist/src/tools/write-file.js.map +1 -1
- package/dist/src/utils/browser.d.ts +13 -0
- package/dist/src/utils/browser.js +49 -0
- package/dist/src/utils/browser.js.map +1 -0
- package/dist/src/utils/errors.js +4 -4
- package/dist/src/utils/errors.js.map +1 -1
- package/dist/src/utils/fileUtils.js +2 -2
- package/dist/src/utils/fileUtils.js.map +1 -1
- package/dist/src/utils/quotaErrorDetection.js +2 -9
- package/dist/src/utils/quotaErrorDetection.js.map +1 -1
- package/dist/src/utils/retry.d.ts +6 -0
- package/dist/src/utils/retry.js +1 -1
- package/dist/src/utils/retry.js.map +1 -1
- package/dist/src/utils/safeJsonStringify.d.ts +13 -0
- package/dist/src/utils/safeJsonStringify.js +25 -0
- package/dist/src/utils/safeJsonStringify.js.map +1 -0
- package/dist/src/utils/safeJsonStringify.test.d.ts +6 -0
- package/dist/src/utils/safeJsonStringify.test.js +61 -0
- package/dist/src/utils/safeJsonStringify.test.js.map +1 -0
- package/dist/src/utils/summarizer.d.ts +1 -1
- package/dist/src/utils/summarizer.js +10 -9
- package/dist/src/utils/summarizer.js.map +1 -1
- package/dist/src/utils/summarizer.test.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/dist/google-gemini-cli-core-0.1.11.tgz +0 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
/**
|
|
8
|
+
* The reserved server name for the IDE's MCP server.
|
|
9
|
+
*/
|
|
10
|
+
export const IDE_SERVER_NAME = '_ide_server';
|
|
11
|
+
/**
|
|
12
|
+
* Zod schema for validating a cursor position.
|
|
13
|
+
*/
|
|
14
|
+
export const CursorSchema = z.object({
|
|
15
|
+
line: z.number(),
|
|
16
|
+
character: z.number(),
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* Zod schema for validating an active file context from the IDE.
|
|
20
|
+
*/
|
|
21
|
+
export const ActiveFileSchema = z.object({
|
|
22
|
+
filePath: z.string(),
|
|
23
|
+
cursor: CursorSchema.optional(),
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Zod schema for validating the 'ide/activeFileChanged' notification from the IDE.
|
|
27
|
+
*/
|
|
28
|
+
export const ActiveFileNotificationSchema = z.object({
|
|
29
|
+
method: z.literal('ide/activeFileChanged'),
|
|
30
|
+
params: ActiveFileSchema,
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new store for managing the IDE's active file context.
|
|
34
|
+
* This factory function encapsulates the state and logic, allowing for the creation
|
|
35
|
+
* of isolated instances, which is particularly useful for testing.
|
|
36
|
+
*
|
|
37
|
+
* @returns An object with methods to interact with the active file context.
|
|
38
|
+
*/
|
|
39
|
+
export function createIdeContextStore() {
|
|
40
|
+
let activeFileContext = undefined;
|
|
41
|
+
const subscribers = new Set();
|
|
42
|
+
/**
|
|
43
|
+
* Notifies all registered subscribers about the current active file context.
|
|
44
|
+
*/
|
|
45
|
+
function notifySubscribers() {
|
|
46
|
+
for (const subscriber of subscribers) {
|
|
47
|
+
subscriber(activeFileContext);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Sets the active file context and notifies all registered subscribers of the change.
|
|
52
|
+
* @param newActiveFile The new active file context from the IDE.
|
|
53
|
+
*/
|
|
54
|
+
function setActiveFileContext(newActiveFile) {
|
|
55
|
+
activeFileContext = newActiveFile;
|
|
56
|
+
notifySubscribers();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Clears the active file context and notifies all registered subscribers of the change.
|
|
60
|
+
*/
|
|
61
|
+
function clearActiveFileContext() {
|
|
62
|
+
activeFileContext = undefined;
|
|
63
|
+
notifySubscribers();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Retrieves the current active file context.
|
|
67
|
+
* @returns The `ActiveFile` object if a file is active, otherwise `undefined`.
|
|
68
|
+
*/
|
|
69
|
+
function getActiveFileContext() {
|
|
70
|
+
return activeFileContext;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Subscribes to changes in the active file context.
|
|
74
|
+
*
|
|
75
|
+
* When the active file context changes, the provided `subscriber` function will be called.
|
|
76
|
+
* Note: The subscriber is not called with the current value upon subscription.
|
|
77
|
+
*
|
|
78
|
+
* @param subscriber The function to be called when the active file context changes.
|
|
79
|
+
* @returns A function that, when called, will unsubscribe the provided subscriber.
|
|
80
|
+
*/
|
|
81
|
+
function subscribeToActiveFile(subscriber) {
|
|
82
|
+
subscribers.add(subscriber);
|
|
83
|
+
return () => {
|
|
84
|
+
subscribers.delete(subscriber);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
setActiveFileContext,
|
|
89
|
+
getActiveFileContext,
|
|
90
|
+
subscribeToActiveFile,
|
|
91
|
+
clearActiveFileContext,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The default, shared instance of the IDE context store for the application.
|
|
96
|
+
*/
|
|
97
|
+
export const ideContext = createIdeContextStore();
|
|
98
|
+
//# sourceMappingURL=ideContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ideContext.js","sourceRoot":"","sources":["../../../src/services/ideContext.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,aAAa,CAAC;AAE7C;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC;IACnC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;IAChB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC,CAAC;AAGH;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,C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}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
7
|
+
import { createIdeContextStore } from './ideContext.js';
|
|
8
|
+
describe('ideContext - Active File', () => {
|
|
9
|
+
let ideContext;
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
// Create a fresh, isolated instance for each test
|
|
12
|
+
ideContext = createIdeContextStore();
|
|
13
|
+
});
|
|
14
|
+
it('should return undefined initially for active file context', () => {
|
|
15
|
+
expect(ideContext.getActiveFileContext()).toBeUndefined();
|
|
16
|
+
});
|
|
17
|
+
it('should set and retrieve the active file context', () => {
|
|
18
|
+
const testFile = {
|
|
19
|
+
filePath: '/path/to/test/file.ts',
|
|
20
|
+
cursor: { line: 5, character: 10 },
|
|
21
|
+
};
|
|
22
|
+
ideContext.setActiveFileContext(testFile);
|
|
23
|
+
const activeFile = ideContext.getActiveFileContext();
|
|
24
|
+
expect(activeFile).toEqual(testFile);
|
|
25
|
+
});
|
|
26
|
+
it('should update the active file context when called multiple times', () => {
|
|
27
|
+
const firstFile = {
|
|
28
|
+
filePath: '/path/to/first.js',
|
|
29
|
+
cursor: { line: 1, character: 1 },
|
|
30
|
+
};
|
|
31
|
+
ideContext.setActiveFileContext(firstFile);
|
|
32
|
+
const secondFile = {
|
|
33
|
+
filePath: '/path/to/second.py',
|
|
34
|
+
cursor: { line: 20, character: 30 },
|
|
35
|
+
};
|
|
36
|
+
ideContext.setActiveFileContext(secondFile);
|
|
37
|
+
const activeFile = ideContext.getActiveFileContext();
|
|
38
|
+
expect(activeFile).toEqual(secondFile);
|
|
39
|
+
});
|
|
40
|
+
it('should handle empty string for file path', () => {
|
|
41
|
+
const testFile = {
|
|
42
|
+
filePath: '',
|
|
43
|
+
cursor: { line: 0, character: 0 },
|
|
44
|
+
};
|
|
45
|
+
ideContext.setActiveFileContext(testFile);
|
|
46
|
+
expect(ideContext.getActiveFileContext()).toEqual(testFile);
|
|
47
|
+
});
|
|
48
|
+
it('should notify subscribers when active file context changes', () => {
|
|
49
|
+
const subscriber1 = vi.fn();
|
|
50
|
+
const subscriber2 = vi.fn();
|
|
51
|
+
ideContext.subscribeToActiveFile(subscriber1);
|
|
52
|
+
ideContext.subscribeToActiveFile(subscriber2);
|
|
53
|
+
const testFile = {
|
|
54
|
+
filePath: '/path/to/subscribed.ts',
|
|
55
|
+
cursor: { line: 15, character: 25 },
|
|
56
|
+
};
|
|
57
|
+
ideContext.setActiveFileContext(testFile);
|
|
58
|
+
expect(subscriber1).toHaveBeenCalledTimes(1);
|
|
59
|
+
expect(subscriber1).toHaveBeenCalledWith(testFile);
|
|
60
|
+
expect(subscriber2).toHaveBeenCalledTimes(1);
|
|
61
|
+
expect(subscriber2).toHaveBeenCalledWith(testFile);
|
|
62
|
+
// Test with another update
|
|
63
|
+
const newFile = {
|
|
64
|
+
filePath: '/path/to/new.js',
|
|
65
|
+
cursor: { line: 1, character: 1 },
|
|
66
|
+
};
|
|
67
|
+
ideContext.setActiveFileContext(newFile);
|
|
68
|
+
expect(subscriber1).toHaveBeenCalledTimes(2);
|
|
69
|
+
expect(subscriber1).toHaveBeenCalledWith(newFile);
|
|
70
|
+
expect(subscriber2).toHaveBeenCalledTimes(2);
|
|
71
|
+
expect(subscriber2).toHaveBeenCalledWith(newFile);
|
|
72
|
+
});
|
|
73
|
+
it('should stop notifying a subscriber after unsubscribe', () => {
|
|
74
|
+
const subscriber1 = vi.fn();
|
|
75
|
+
const subscriber2 = vi.fn();
|
|
76
|
+
const unsubscribe1 = ideContext.subscribeToActiveFile(subscriber1);
|
|
77
|
+
ideContext.subscribeToActiveFile(subscriber2);
|
|
78
|
+
ideContext.setActiveFileContext({
|
|
79
|
+
filePath: '/path/to/file1.txt',
|
|
80
|
+
cursor: { line: 1, character: 1 },
|
|
81
|
+
});
|
|
82
|
+
expect(subscriber1).toHaveBeenCalledTimes(1);
|
|
83
|
+
expect(subscriber2).toHaveBeenCalledTimes(1);
|
|
84
|
+
unsubscribe1();
|
|
85
|
+
ideContext.setActiveFileContext({
|
|
86
|
+
filePath: '/path/to/file2.txt',
|
|
87
|
+
cursor: { line: 2, character: 2 },
|
|
88
|
+
});
|
|
89
|
+
expect(subscriber1).toHaveBeenCalledTimes(1); // Should not be called again
|
|
90
|
+
expect(subscriber2).toHaveBeenCalledTimes(2);
|
|
91
|
+
});
|
|
92
|
+
it('should allow the cursor to be optional', () => {
|
|
93
|
+
const testFile = {
|
|
94
|
+
filePath: '/path/to/test/file.ts',
|
|
95
|
+
};
|
|
96
|
+
ideContext.setActiveFileContext(testFile);
|
|
97
|
+
const activeFile = ideContext.getActiveFileContext();
|
|
98
|
+
expect(activeFile).toEqual(testFile);
|
|
99
|
+
});
|
|
100
|
+
it('should clear the active file context', () => {
|
|
101
|
+
const testFile = {
|
|
102
|
+
filePath: '/path/to/test/file.ts',
|
|
103
|
+
cursor: { line: 5, character: 10 },
|
|
104
|
+
};
|
|
105
|
+
ideContext.setActiveFileContext(testFile);
|
|
106
|
+
expect(ideContext.getActiveFileContext()).toEqual(testFile);
|
|
107
|
+
ideContext.clearActiveFileContext();
|
|
108
|
+
expect(ideContext.getActiveFileContext()).toBeUndefined();
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
//# sourceMappingURL=ideContext.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ideContext.test.js","sourceRoot":"","sources":["../../../src/services/ideContext.test.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAExD,QAAQ,CAAC,0BAA0B,EAAE,GAAG,EAAE;I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C/C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wCAAwC,EAAE,GAAG,EAAE;QAChD,MAAM,QAAQ,GAAG;YACf,QAAQ,EAAE,uBAAuB;SAClC,CAAC;QAEF,UAAU,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC;QAE1C,MAAM,UAAU,GAAG,UAAU,CAAC,oBAAoB,EAAE,CAAC;QACrD,MAAM,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,sCAAsC,EAAE,GAAG,EAAE;QAC9C,MAAM,QAAQ,GAAG;YACf,QAAQ,EAAE,uBAAuB;YACjC,MAAM,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,SAAS,EAAE,EAAE,EAAE;SACnC,CAAC;QAEF,UAAU,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC;QAE1C,MAAM,CAAC,UAAU,CAAC,oBAAoB,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAE5D,UAAU,CAAC,sBAAsB,EAAE,CAAC;QAEpC,MAAM,CAAC,UAAU,CAAC,oBAAoB,EAAE,CAAC,CAAC,aAAa,EAAE,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { ServerGeminiStreamEvent } from '../core/turn.js';
|
|
7
|
+
import { Config } from '../config/config.js';
|
|
8
|
+
/**
|
|
9
|
+
* Service for detecting and preventing infinite loops in AI responses.
|
|
10
|
+
* Monitors tool call repetitions and content sentence repetitions.
|
|
11
|
+
*/
|
|
12
|
+
export declare class LoopDetectionService {
|
|
13
|
+
private readonly config;
|
|
14
|
+
private lastToolCallKey;
|
|
15
|
+
private toolCallRepetitionCount;
|
|
16
|
+
private lastRepeatedSentence;
|
|
17
|
+
private sentenceRepetitionCount;
|
|
18
|
+
private partialContent;
|
|
19
|
+
private turnsInCurrentPrompt;
|
|
20
|
+
private llmCheckInterval;
|
|
21
|
+
private lastCheckTurn;
|
|
22
|
+
constructor(config: Config);
|
|
23
|
+
private getToolCallKey;
|
|
24
|
+
/**
|
|
25
|
+
* Processes a stream event and checks for loop conditions.
|
|
26
|
+
* @param event - The stream event to process
|
|
27
|
+
* @returns true if a loop is detected, false otherwise
|
|
28
|
+
*/
|
|
29
|
+
addAndCheck(event: ServerGeminiStreamEvent): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Signals the start of a new turn in the conversation.
|
|
32
|
+
*
|
|
33
|
+
* This method increments the turn counter and, if specific conditions are met,
|
|
34
|
+
* triggers an LLM-based check to detect potential conversation loops. The check
|
|
35
|
+
* is performed periodically based on the `llmCheckInterval`.
|
|
36
|
+
*
|
|
37
|
+
* @param signal - An AbortSignal to allow for cancellation of the asynchronous LLM check.
|
|
38
|
+
* @returns A promise that resolves to `true` if a loop is detected, and `false` otherwise.
|
|
39
|
+
*/
|
|
40
|
+
turnStarted(signal: AbortSignal): Promise<boolean>;
|
|
41
|
+
private checkToolCallLoop;
|
|
42
|
+
private checkContentLoop;
|
|
43
|
+
private checkForLoopWithLLM;
|
|
44
|
+
/**
|
|
45
|
+
* Resets all loop detection state.
|
|
46
|
+
*/
|
|
47
|
+
reset(): void;
|
|
48
|
+
private resetToolCallCount;
|
|
49
|
+
private resetSentenceCount;
|
|
50
|
+
private resetLlmCheckTracking;
|
|
51
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { createHash } from 'crypto';
|
|
7
|
+
import { GeminiEventType } from '../core/turn.js';
|
|
8
|
+
import { logLoopDetected } from '../telemetry/loggers.js';
|
|
9
|
+
import { LoopDetectedEvent, LoopType } from '../telemetry/types.js';
|
|
10
|
+
import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/config.js';
|
|
11
|
+
import { Type } from '@google/genai';
|
|
12
|
+
const TOOL_CALL_LOOP_THRESHOLD = 5;
|
|
13
|
+
const CONTENT_LOOP_THRESHOLD = 10;
|
|
14
|
+
/**
|
|
15
|
+
* The number of recent conversation turns to include in the history when asking the LLM to check for a loop.
|
|
16
|
+
*/
|
|
17
|
+
const LLM_LOOP_CHECK_HISTORY_COUNT = 20;
|
|
18
|
+
/**
|
|
19
|
+
* The number of turns that must pass in a single prompt before the LLM-based loop check is activated.
|
|
20
|
+
*/
|
|
21
|
+
const LLM_CHECK_AFTER_TURNS = 30;
|
|
22
|
+
/**
|
|
23
|
+
* The default interval, in number of turns, at which the LLM-based loop check is performed.
|
|
24
|
+
* This value is adjusted dynamically based on the LLM's confidence.
|
|
25
|
+
*/
|
|
26
|
+
const DEFAULT_LLM_CHECK_INTERVAL = 3;
|
|
27
|
+
/**
|
|
28
|
+
* The minimum interval for LLM-based loop checks.
|
|
29
|
+
* This is used when the confidence of a loop is high, to check more frequently.
|
|
30
|
+
*/
|
|
31
|
+
const MIN_LLM_CHECK_INTERVAL = 5;
|
|
32
|
+
/**
|
|
33
|
+
* The maximum interval for LLM-based loop checks.
|
|
34
|
+
* This is used when the confidence of a loop is low, to check less frequently.
|
|
35
|
+
*/
|
|
36
|
+
const MAX_LLM_CHECK_INTERVAL = 15;
|
|
37
|
+
const SENTENCE_ENDING_PUNCTUATION_REGEX = /[.!?]+(?=\s|$)/;
|
|
38
|
+
/**
|
|
39
|
+
* Service for detecting and preventing infinite loops in AI responses.
|
|
40
|
+
* Monitors tool call repetitions and content sentence repetitions.
|
|
41
|
+
*/
|
|
42
|
+
export class LoopDetectionService {
|
|
43
|
+
config;
|
|
44
|
+
// Tool call tracking
|
|
45
|
+
lastToolCallKey = null;
|
|
46
|
+
toolCallRepetitionCount = 0;
|
|
47
|
+
// Content streaming tracking
|
|
48
|
+
lastRepeatedSentence = '';
|
|
49
|
+
sentenceRepetitionCount = 0;
|
|
50
|
+
partialContent = '';
|
|
51
|
+
// LLM loop track tracking
|
|
52
|
+
turnsInCurrentPrompt = 0;
|
|
53
|
+
llmCheckInterval = DEFAULT_LLM_CHECK_INTERVAL;
|
|
54
|
+
lastCheckTurn = 0;
|
|
55
|
+
constructor(config) {
|
|
56
|
+
this.config = config;
|
|
57
|
+
}
|
|
58
|
+
getToolCallKey(toolCall) {
|
|
59
|
+
const argsString = JSON.stringify(toolCall.args);
|
|
60
|
+
const keyString = `${toolCall.name}:${argsString}`;
|
|
61
|
+
return createHash('sha256').update(keyString).digest('hex');
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Processes a stream event and checks for loop conditions.
|
|
65
|
+
* @param event - The stream event to process
|
|
66
|
+
* @returns true if a loop is detected, false otherwise
|
|
67
|
+
*/
|
|
68
|
+
addAndCheck(event) {
|
|
69
|
+
switch (event.type) {
|
|
70
|
+
case GeminiEventType.ToolCallRequest:
|
|
71
|
+
// content chanting only happens in one single stream, reset if there
|
|
72
|
+
// is a tool call in between
|
|
73
|
+
this.resetSentenceCount();
|
|
74
|
+
return this.checkToolCallLoop(event.value);
|
|
75
|
+
case GeminiEventType.Content:
|
|
76
|
+
return this.checkContentLoop(event.value);
|
|
77
|
+
default:
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Signals the start of a new turn in the conversation.
|
|
83
|
+
*
|
|
84
|
+
* This method increments the turn counter and, if specific conditions are met,
|
|
85
|
+
* triggers an LLM-based check to detect potential conversation loops. The check
|
|
86
|
+
* is performed periodically based on the `llmCheckInterval`.
|
|
87
|
+
*
|
|
88
|
+
* @param signal - An AbortSignal to allow for cancellation of the asynchronous LLM check.
|
|
89
|
+
* @returns A promise that resolves to `true` if a loop is detected, and `false` otherwise.
|
|
90
|
+
*/
|
|
91
|
+
async turnStarted(signal) {
|
|
92
|
+
this.turnsInCurrentPrompt++;
|
|
93
|
+
if (this.turnsInCurrentPrompt >= LLM_CHECK_AFTER_TURNS &&
|
|
94
|
+
this.turnsInCurrentPrompt - this.lastCheckTurn >= this.llmCheckInterval) {
|
|
95
|
+
this.lastCheckTurn = this.turnsInCurrentPrompt;
|
|
96
|
+
return await this.checkForLoopWithLLM(signal);
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
checkToolCallLoop(toolCall) {
|
|
101
|
+
const key = this.getToolCallKey(toolCall);
|
|
102
|
+
if (this.lastToolCallKey === key) {
|
|
103
|
+
this.toolCallRepetitionCount++;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
this.lastToolCallKey = key;
|
|
107
|
+
this.toolCallRepetitionCount = 1;
|
|
108
|
+
}
|
|
109
|
+
if (this.toolCallRepetitionCount >= TOOL_CALL_LOOP_THRESHOLD) {
|
|
110
|
+
logLoopDetected(this.config, new LoopDetectedEvent(LoopType.CONSECUTIVE_IDENTICAL_TOOL_CALLS));
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
checkContentLoop(content) {
|
|
116
|
+
this.partialContent += content;
|
|
117
|
+
if (!SENTENCE_ENDING_PUNCTUATION_REGEX.test(this.partialContent)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
const completeSentences = this.partialContent.match(/[^.!?]+[.!?]+(?=\s|$)/g) || [];
|
|
121
|
+
if (completeSentences.length === 0) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const lastSentence = completeSentences[completeSentences.length - 1];
|
|
125
|
+
const lastCompleteIndex = this.partialContent.lastIndexOf(lastSentence);
|
|
126
|
+
const endOfLastSentence = lastCompleteIndex + lastSentence.length;
|
|
127
|
+
this.partialContent = this.partialContent.slice(endOfLastSentence);
|
|
128
|
+
for (const sentence of completeSentences) {
|
|
129
|
+
const trimmedSentence = sentence.trim();
|
|
130
|
+
if (trimmedSentence === '') {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (this.lastRepeatedSentence === trimmedSentence) {
|
|
134
|
+
this.sentenceRepetitionCount++;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
this.lastRepeatedSentence = trimmedSentence;
|
|
138
|
+
this.sentenceRepetitionCount = 1;
|
|
139
|
+
}
|
|
140
|
+
if (this.sentenceRepetitionCount >= CONTENT_LOOP_THRESHOLD) {
|
|
141
|
+
logLoopDetected(this.config, new LoopDetectedEvent(LoopType.CHANTING_IDENTICAL_SENTENCES));
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
async checkForLoopWithLLM(signal) {
|
|
148
|
+
const recentHistory = this.config
|
|
149
|
+
.getGeminiClient()
|
|
150
|
+
.getHistory()
|
|
151
|
+
.slice(-LLM_LOOP_CHECK_HISTORY_COUNT);
|
|
152
|
+
const prompt = `You are a sophisticated AI diagnostic agent specializing in identifying when a conversational AI is stuck in an unproductive state. Your task is to analyze the provided conversation history and determine if the assistant has ceased to make meaningful progress.
|
|
153
|
+
|
|
154
|
+
An unproductive state is characterized by one or more of the following patterns over the last 5 or more assistant turns:
|
|
155
|
+
|
|
156
|
+
Repetitive Actions: The assistant repeats the same tool calls or conversational responses a decent number of times. This includes simple loops (e.g., tool_A, tool_A, tool_A) and alternating patterns (e.g., tool_A, tool_B, tool_A, tool_B, ...).
|
|
157
|
+
|
|
158
|
+
Cognitive Loop: The assistant seems unable to determine the next logical step. It might express confusion, repeatedly ask the same questions, or generate responses that don't logically follow from the previous turns, indicating it's stuck and not advancing the task.
|
|
159
|
+
|
|
160
|
+
Crucially, differentiate between a true unproductive state and legitimate, incremental progress.
|
|
161
|
+
For example, a series of 'tool_A' or 'tool_B' tool calls that make small, distinct changes to the same file (like adding docstrings to functions one by one) is considered forward progress and is NOT a loop. A loop would be repeatedly replacing the same text with the same content, or cycling between a small set of files with no net change.
|
|
162
|
+
|
|
163
|
+
Please analyze the conversation history to determine the possibility that the conversation is stuck in a repetitive, non-productive state.`;
|
|
164
|
+
const contents = [
|
|
165
|
+
...recentHistory,
|
|
166
|
+
{ role: 'user', parts: [{ text: prompt }] },
|
|
167
|
+
];
|
|
168
|
+
const schema = {
|
|
169
|
+
type: Type.OBJECT,
|
|
170
|
+
properties: {
|
|
171
|
+
reasoning: {
|
|
172
|
+
type: Type.STRING,
|
|
173
|
+
description: 'Your reasoning on if the conversation is looping without forward progress.',
|
|
174
|
+
},
|
|
175
|
+
confidence: {
|
|
176
|
+
type: Type.NUMBER,
|
|
177
|
+
description: 'A number between 0.0 and 1.0 representing your confidence that the conversation is in an unproductive state.',
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
required: ['reasoning', 'confidence'],
|
|
181
|
+
};
|
|
182
|
+
let result;
|
|
183
|
+
try {
|
|
184
|
+
result = await this.config
|
|
185
|
+
.getGeminiClient()
|
|
186
|
+
.generateJson(contents, schema, signal, DEFAULT_GEMINI_FLASH_MODEL);
|
|
187
|
+
}
|
|
188
|
+
catch (e) {
|
|
189
|
+
// Do nothing, treat it as a non-loop.
|
|
190
|
+
this.config.getDebugMode() ? console.error(e) : console.debug(e);
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
if (typeof result.confidence === 'number') {
|
|
194
|
+
if (result.confidence > 0.9) {
|
|
195
|
+
if (typeof result.reasoning === 'string' && result.reasoning) {
|
|
196
|
+
console.warn(result.reasoning);
|
|
197
|
+
}
|
|
198
|
+
logLoopDetected(this.config, new LoopDetectedEvent(LoopType.LLM_DETECTED_LOOP));
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
this.llmCheckInterval = Math.round(MIN_LLM_CHECK_INTERVAL +
|
|
203
|
+
(MAX_LLM_CHECK_INTERVAL - MIN_LLM_CHECK_INTERVAL) *
|
|
204
|
+
(1 - result.confidence));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Resets all loop detection state.
|
|
211
|
+
*/
|
|
212
|
+
reset() {
|
|
213
|
+
this.resetToolCallCount();
|
|
214
|
+
this.resetSentenceCount();
|
|
215
|
+
this.resetLlmCheckTracking();
|
|
216
|
+
}
|
|
217
|
+
resetToolCallCount() {
|
|
218
|
+
this.lastToolCallKey = null;
|
|
219
|
+
this.toolCallRepetitionCount = 0;
|
|
220
|
+
}
|
|
221
|
+
resetSentenceCount() {
|
|
222
|
+
this.lastRepeatedSentence = '';
|
|
223
|
+
this.sentenceRepetitionCount = 0;
|
|
224
|
+
this.partialContent = '';
|
|
225
|
+
}
|
|
226
|
+
resetLlmCheckTracking() {
|
|
227
|
+
this.turnsInCurrentPrompt = 0;
|
|
228
|
+
this.llmCheckInterval = DEFAULT_LLM_CHECK_INTERVAL;
|
|
229
|
+
this.lastCheckTurn = 0;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
//# sourceMappingURL=loopDetectionService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loopDetectionService.js","sourceRoot":"","sources":["../../../src/services/loopDetectionService.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,eAAe,EAA2B,MAAM,iBAAiB,CAAC;AAC3E,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAU,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAe,IAAI,EAAE,MAAM,eAAe,CAAC;AAElD,MAAM,wBAAwB,GAAG,CAAC,CAAC;AACnC,MAA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