-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(core): add ConfigTool for programmatic config read/write #2911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
wenshao
wants to merge
6
commits into
main
Choose a base branch
from
feat/config-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c9d9974
feat(core): add ConfigTool for programmatic config read/write
wenshao d30de21
fix: address review feedback on ConfigTool error handling
wenshao af49c95
fix: address audit findings — prototype pollution, empty value, non-n…
wenshao aafb847
fix: remove permissionRules from ConfigTool confirmation
wenshao 9be4240
fix: harden getDescriptor with Object.hasOwn, add config to CORE_TOOLS
wenshao 7ba3d51
fix: set ToolResult.error on failure, restrict type to string-only
wenshao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -416,6 +416,7 @@ export class PermissionManager { | |
| 'cron_create', | ||
| 'cron_list', | ||
| 'cron_delete', | ||
| 'config', | ||
| ]); | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
| import { ConfigTool } from './config-tool.js'; | ||
| import type { Config } from '../config/config.js'; | ||
|
|
||
| function makeConfig(currentModel = 'qwen-coder-plus') { | ||
| let model = currentModel; | ||
| return { | ||
| getModel: vi.fn(() => model), | ||
| setModel: vi.fn(async (newModel: string) => { | ||
| model = newModel; | ||
| }), | ||
| getAvailableModels: vi.fn(() => [ | ||
| { id: 'qwen-coder-plus', label: 'Qwen Coder Plus', authType: 'api-key' }, | ||
| { id: 'qwen3-coder', label: 'Qwen3 Coder', authType: 'api-key' }, | ||
| ]), | ||
| } as unknown as Config; | ||
| } | ||
|
|
||
| describe('ConfigTool', () => { | ||
| let config: ReturnType<typeof makeConfig>; | ||
| let tool: ConfigTool; | ||
|
|
||
| beforeEach(() => { | ||
| config = makeConfig(); | ||
| tool = new ConfigTool(config); | ||
| }); | ||
|
|
||
| it('has the correct name and display name', () => { | ||
| expect(tool.name).toBe('config'); | ||
| expect(tool.displayName).toBe('Config'); | ||
| }); | ||
|
|
||
| describe('validation', () => { | ||
| it('rejects unknown setting', () => { | ||
| expect(() => | ||
| tool.build({ action: 'get', setting: 'nonexistent' }), | ||
| ).toThrow(/Unknown setting.*nonexistent/); | ||
| }); | ||
|
|
||
| it('rejects SET without value', () => { | ||
| expect(() => tool.build({ action: 'set', setting: 'model' })).toThrow( | ||
| /Value is required/, | ||
| ); | ||
| }); | ||
|
|
||
| it('rejects SET with empty string value', () => { | ||
| expect(() => | ||
| tool.build({ action: 'set', setting: 'model', value: '' }), | ||
| ).toThrow(/Value is required/); | ||
| }); | ||
|
|
||
| it('rejects SET with whitespace-only value', () => { | ||
| expect(() => | ||
| tool.build({ action: 'set', setting: 'model', value: ' ' }), | ||
| ).toThrow(/Value is required/); | ||
| }); | ||
|
|
||
| it('rejects prototype chain keys like toString', () => { | ||
| expect(() => tool.build({ action: 'get', setting: 'toString' })).toThrow( | ||
| /Unknown setting.*toString/, | ||
| ); | ||
| }); | ||
|
|
||
| it('rejects __proto__ as setting name', () => { | ||
| expect(() => tool.build({ action: 'get', setting: '__proto__' })).toThrow( | ||
| /Unknown setting/, | ||
| ); | ||
| }); | ||
|
|
||
| it('accepts valid GET params', () => { | ||
| expect(() => | ||
| tool.build({ action: 'get', setting: 'model' }), | ||
| ).not.toThrow(); | ||
| }); | ||
|
|
||
| it('accepts valid SET params', () => { | ||
| expect(() => | ||
| tool.build({ action: 'set', setting: 'model', value: 'qwen3-coder' }), | ||
| ).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('GET', () => { | ||
| it('returns current model value and available models', async () => { | ||
| const invocation = tool.build({ action: 'get', setting: 'model' }); | ||
| const result = await invocation.execute(new AbortController().signal); | ||
|
|
||
| expect(result.llmContent).toContain('model = qwen-coder-plus'); | ||
| expect(result.llmContent).toContain('Available models:'); | ||
| expect(result.llmContent).toContain('qwen-coder-plus'); | ||
| expect(result.llmContent).toContain('qwen3-coder'); | ||
| }); | ||
|
|
||
| it('permission is allow for GET', async () => { | ||
| const invocation = tool.build({ action: 'get', setting: 'model' }); | ||
| const permission = await invocation.getDefaultPermission(); | ||
| expect(permission).toBe('allow'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('SET', () => { | ||
| it('permission is ask for SET', async () => { | ||
| const invocation = tool.build({ | ||
| action: 'set', | ||
| setting: 'model', | ||
| value: 'qwen3-coder', | ||
| }); | ||
| const permission = await invocation.getDefaultPermission(); | ||
| expect(permission).toBe('ask'); | ||
| }); | ||
|
|
||
| it('changes model on success', async () => { | ||
| const invocation = tool.build({ | ||
| action: 'set', | ||
| setting: 'model', | ||
| value: 'qwen3-coder', | ||
| }); | ||
| const result = await invocation.execute(new AbortController().signal); | ||
|
|
||
| expect(result.llmContent).toContain("changed from 'qwen-coder-plus'"); | ||
| expect(result.llmContent).toContain("to 'qwen3-coder'"); | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| expect((config as any).setModel).toHaveBeenCalledWith('qwen3-coder', { | ||
| reason: 'agent-config-tool', | ||
| context: 'ConfigTool SET', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns error when setModel throws', async () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| (config as any).setModel = vi.fn(async () => { | ||
| throw new Error('Invalid model ID'); | ||
| }); | ||
| tool = new ConfigTool(config); | ||
|
|
||
| const invocation = tool.build({ | ||
| action: 'set', | ||
| setting: 'model', | ||
| value: 'nonexistent-model', | ||
| }); | ||
| const result = await invocation.execute(new AbortController().signal); | ||
|
|
||
wenshao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expect(result.llmContent).toContain('Failed to set model'); | ||
| expect(result.llmContent).toContain('Invalid model ID'); | ||
| expect(result.error).toBeDefined(); | ||
| expect(result.error?.type).toBe('execution_failed'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('confirmation details', () => { | ||
| it('shows from/to for SET', async () => { | ||
| const invocation = tool.build({ | ||
| action: 'set', | ||
| setting: 'model', | ||
| value: 'qwen3-coder', | ||
| }); | ||
| const details = await invocation.getConfirmationDetails( | ||
| new AbortController().signal, | ||
| ); | ||
|
|
||
| expect(details.type).toBe('info'); | ||
| if (details.type === 'info') { | ||
| expect(details.prompt).toContain('qwen-coder-plus'); | ||
| expect(details.prompt).toContain('qwen3-coder'); | ||
| expect(details.hideAlwaysAllow).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| it('shows read description for GET', async () => { | ||
| const invocation = tool.build({ action: 'get', setting: 'model' }); | ||
| const details = await invocation.getConfirmationDetails( | ||
| new AbortController().signal, | ||
| ); | ||
|
|
||
| expect(details.type).toBe('info'); | ||
| if (details.type === 'info') { | ||
| expect(details.prompt).toContain('Read model'); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.