-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add model roles for small, implementer, and advisor #56
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
Merged
+587
−18
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d48da1
feat: add model roles for small, implementer, and advisor
elkaix a86a9ed
fix: address review feedback on model roles
elkaix c1d75e8
fix: persist model role assignments in the config write path
elkaix b2c092a
fix: preserve role assignment across model picker refresh
elkaix 1e1559e
refactor: move the built-in model role list to the TUI constants
elkaix 4243f03
fix: drop the model roles table when the role map is cleared
elkaix 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@pythoughts/pythinker-code": minor | ||
| --- | ||
|
|
||
| Add model roles: lock a model alias to the small, implementer, or advisor slot with `/model <role>`, list assignments with `/model roles`, and reference roles as `@small`, `@implementer`, or `@advisor` wherever a subagent model can be set; an assigned implementer role becomes the default model for subagents. |
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
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
161 changes: 161 additions & 0 deletions
161
apps/pythinker-code/test/tui/commands/model-roles.test.ts
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,161 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { handleModelCommand } from '#/tui/commands/index'; | ||
| import type { SlashCommandHost } from '#/tui/commands/dispatch'; | ||
|
|
||
| const ENTER = '\r'; | ||
|
|
||
| interface TestPicker { | ||
| handleInput(data: string): void; | ||
| } | ||
|
|
||
| function model(name: string) { | ||
| return { | ||
| provider: 'test', | ||
| model: name, | ||
| maxContextSize: 200_000, | ||
| displayName: name, | ||
| capabilities: [], | ||
| }; | ||
| } | ||
|
|
||
| function makeHost(options: { | ||
| currentModel?: string; | ||
| availableModels?: Record<string, ReturnType<typeof model>>; | ||
| modelRoles?: Record<string, string>; | ||
| setConfig?: ReturnType<typeof vi.fn>; | ||
| } = {}) { | ||
| const session = { | ||
| setModel: vi.fn(async () => {}), | ||
| setThinking: vi.fn(async () => {}), | ||
| }; | ||
| const getConfig = vi.fn(async () => ({ | ||
| providers: {}, | ||
| modelRoles: options.modelRoles, | ||
| })); | ||
| const setConfig = options.setConfig ?? vi.fn(async () => {}); | ||
| const host = { | ||
| state: { | ||
| appState: { | ||
| model: options.currentModel ?? 'worker', | ||
| thinkingLevel: 'off', | ||
| streamingPhase: 'idle', | ||
| availableModels: options.availableModels ?? { worker: model('worker') }, | ||
| }, | ||
| editorContainer: { children: [] }, | ||
| }, | ||
| session, | ||
| harness: { getConfig, setConfig }, | ||
| authFlow: { | ||
| refreshProviderModels: vi.fn(async () => ({ failed: [] })), | ||
| }, | ||
| setAppState: vi.fn((patch: Record<string, unknown>) => Object.assign(host.state.appState, patch)), | ||
| showError: vi.fn(), | ||
| showStatus: vi.fn(), | ||
| showNotice: vi.fn(), | ||
| mountEditorReplacement: vi.fn(), | ||
| restoreEditor: vi.fn(), | ||
| track: vi.fn(), | ||
| } as unknown as SlashCommandHost; | ||
| return { host, session, setConfig }; | ||
| } | ||
|
|
||
| function mountedPicker(host: SlashCommandHost, index = 0): TestPicker { | ||
| const mount = host.mountEditorReplacement as ReturnType<typeof vi.fn>; | ||
| return mount.mock.calls[index]?.[0] as TestPicker; | ||
| } | ||
|
|
||
| describe('/model roles', () => { | ||
| it('lists every built-in role as not set when no assignments exist', async () => { | ||
| const { host } = makeHost(); | ||
|
|
||
| await handleModelCommand(host, 'roles'); | ||
|
|
||
| expect(host.showNotice).toHaveBeenCalledWith( | ||
| 'Model roles', | ||
| 'small: (not set)\nimplementer: (not set)\nadvisor: (not set)', | ||
| ); | ||
| }); | ||
|
|
||
| it('locks a selected alias to a role without switching the session model', async () => { | ||
| const { host, session, setConfig } = makeHost(); | ||
|
|
||
| await handleModelCommand(host, 'small'); | ||
| expect(host.authFlow.refreshProviderModels).toHaveBeenCalledOnce(); | ||
| mountedPicker(host).handleInput(ENTER); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(setConfig).toHaveBeenCalledWith({ modelRoles: { small: 'worker' } }); | ||
| }); | ||
| expect(session.setModel).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('keeps role assignment active after the picker refreshes', async () => { | ||
| const { host, session, setConfig } = makeHost({ | ||
| currentModel: 'parent', | ||
| availableModels: { | ||
| parent: model('parent'), | ||
| worker: model('worker'), | ||
| }, | ||
| modelRoles: { small: 'worker' }, | ||
| }); | ||
| vi.mocked(host.mountEditorReplacement).mockImplementation((picker) => { | ||
| host.state.editorContainer.children[0] = picker; | ||
| }); | ||
| vi.mocked(host.authFlow.refreshProviderModels).mockImplementation(async () => { | ||
| host.state.appState.availableModels['reviewer'] = model('reviewer'); | ||
| return { changed: [], unchanged: [], failed: [] }; | ||
| }); | ||
|
|
||
| await handleModelCommand(host, 'small'); | ||
| await vi.waitFor(() => { | ||
| expect(host.mountEditorReplacement).toHaveBeenCalledTimes(2); | ||
| }); | ||
| mountedPicker(host, 1).handleInput(ENTER); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(setConfig).toHaveBeenCalledWith({ modelRoles: { small: 'worker' } }); | ||
| }); | ||
| expect(session.setModel).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('reports a role persistence failure without showing success', async () => { | ||
| const setConfig = vi.fn(async () => { | ||
| throw new Error('disk full'); | ||
| }); | ||
| const { host } = makeHost({ setConfig }); | ||
|
|
||
| await handleModelCommand(host, 'small'); | ||
| mountedPicker(host).handleInput(ENTER); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(host.showError).toHaveBeenCalledWith(expect.stringContaining('disk full')); | ||
| }); | ||
| expect(host.showStatus).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('clears a role with an empty-string tombstone', async () => { | ||
| const { host, setConfig } = makeHost({ modelRoles: { small: 'worker' } }); | ||
|
|
||
| await handleModelCommand(host, 'small clear'); | ||
|
|
||
| expect(setConfig).toHaveBeenCalledWith({ modelRoles: { small: '' } }); | ||
| }); | ||
|
|
||
| it('keeps an existing model alias on the default switch path', async () => { | ||
| const { host, session } = makeHost({ | ||
| currentModel: 'parent', | ||
| availableModels: { | ||
| parent: model('parent'), | ||
| worker: model('worker'), | ||
| }, | ||
| }); | ||
|
|
||
| await handleModelCommand(host, 'worker'); | ||
| mountedPicker(host).handleInput(ENTER); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(session.setModel).toHaveBeenCalledWith('worker'); | ||
| }); | ||
| }); | ||
| }); |
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
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** Built-in model roles a user can lock a model alias to. */ | ||
| export const BUILT_IN_MODEL_ROLES = ['small', 'implementer', 'advisor'] as const; | ||
| export type BuiltInModelRole = (typeof BUILT_IN_MODEL_ROLES)[number]; | ||
|
|
||
| interface ModelRoleSource { | ||
| modelRoles?: Record<string, string>; | ||
| defaultModel?: string; | ||
| } | ||
|
|
||
| /** Resolve a role name to its locked model alias. Empty string means cleared. */ | ||
| export function resolveModelRoleAlias( | ||
| config: ModelRoleSource | undefined, | ||
| role: string, | ||
| ): string | undefined { | ||
| if (role === 'default') return config?.defaultModel; | ||
| const alias = config?.modelRoles?.[role]?.trim(); | ||
| return alias === '' ? undefined : alias; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** Expand a "@role" model reference; non-@ strings pass through unchanged. */ | ||
| export function expandModelRef( | ||
| config: ModelRoleSource | undefined, | ||
| ref: string, | ||
| ): string | undefined { | ||
| return ref.startsWith('@') ? resolveModelRoleAlias(config, ref.slice(1)) : ref; | ||
| } | ||
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.