Repository navigation
feat(cli): run the agents on a chosen provider's models #1119
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
alexsoyes
wants to merge
1
commit into
feat/global-install
Choose a base branch
from
feat/agent-models
base: feat/global-install
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.
+915
−36
Draft
Changes from all commits
Commits
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
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
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
104 changes: 104 additions & 0 deletions
104
cli/src/contexts/framework/application/agents/set-agent-model-use-case.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,104 @@ | ||
| import type { MarketplaceScope } from "../../../../kernel/scope.js"; | ||
| import { | ||
| type AgentHost, | ||
| type AgentProvider, | ||
| supportedProvider, | ||
| } from "../../../tools/domain/formats/agent-models.js"; | ||
| import type { AgentModelStore } from "../../domain/ports/agent-model-store.js"; | ||
| import type { ManifestRepository } from "../../domain/ports/manifest-repository.js"; | ||
| import type { MarketplaceSyncSettings } from "../flows/marketplace-sync-settings-use-case.js"; | ||
| import type { PluginUpdateUseCase } from "../plugin/plugin-update-use-case.js"; | ||
|
|
||
| export interface SetAgentModelOptions { | ||
| readonly projectRoot: string; | ||
| readonly hosts: readonly AgentHost[]; | ||
| /** Absent, the hosts' agents go back to the session's model. */ | ||
| readonly provider?: string; | ||
| /** `"user"` also refreshes what `setup --global` installed for the machine. */ | ||
| readonly scope: MarketplaceScope; | ||
| } | ||
|
|
||
| export interface SetAgentModelResult { | ||
| /** Plugins re-installed with the new models. */ | ||
| readonly reinstalled: readonly string[]; | ||
| } | ||
|
|
||
| /** | ||
| * The setter for agent models. Records the provider each host's agents run on, which every | ||
| * build of that host then applies, and re-installs the plugins already installed so they | ||
| * carry it. The session's own model stays the IDE's choice. | ||
| */ | ||
| export class SetAgentModelUseCase { | ||
| constructor( | ||
| private readonly store: AgentModelStore, | ||
| private readonly manifestRepo: ManifestRepository, | ||
| private readonly userManifestRepo: ManifestRepository, | ||
| private readonly pluginUpdate: Pick<PluginUpdateUseCase, "execute">, | ||
| private readonly userPluginUpdate: Pick<PluginUpdateUseCase, "execute">, | ||
| private readonly activation: MarketplaceSyncSettings | ||
| ) {} | ||
|
|
||
| async execute(options: SetAgentModelOptions): Promise<SetAgentModelResult> { | ||
| const chosen = new Map<AgentHost, AgentProvider>(); | ||
| if (options.provider !== undefined) { | ||
| for (const host of options.hosts) chosen.set(host, supportedProvider(host, options.provider)); | ||
| } | ||
| const providers = await this.store.loadProviders(); | ||
| for (const host of options.hosts) { | ||
| const provider = chosen.get(host); | ||
| if (provider === undefined) delete providers[host]; | ||
| else providers[host] = provider; | ||
| } | ||
| await this.store.saveProviders(providers); | ||
| return { reinstalled: await this.reinstall(options) }; | ||
| } | ||
|
|
||
| /** Files a plugin wrote are rewritten from the rebuilt tree, and a host that installs plugins | ||
| * itself is driven again, which hands it that tree. The provider is machine-wide, so this | ||
| * project is always refreshed, and a plugin installed in the user's home, as Cursor's always | ||
| * is, too; `--global` adds the machine-scope registrations. */ | ||
| private async reinstall(options: SetAgentModelOptions): Promise<readonly string[]> { | ||
| const rewritten = new Set<string>(); | ||
| const userManifest = await this.userManifestRepo.load(); | ||
| const userPlugins = options.hosts.filter((host) => | ||
| userManifest?.getPlugins(host).some((plugin) => plugin.scope === "user") | ||
| ); | ||
| if (userPlugins.length > 0) { | ||
| const names = await this.userPluginUpdate.execute({ | ||
| toolIds: userPlugins, | ||
| projectRoot: options.projectRoot, | ||
| scope: "user", | ||
| rebuild: true, | ||
| }); | ||
| for (const name of names) rewritten.add(name); | ||
| } | ||
| const manifest = await this.manifestRepo.load(); | ||
| const installed = options.hosts.filter((host) => manifest?.hasTool(host) === true); | ||
| const projectPlugins = installed.filter((host) => | ||
| manifest?.getPlugins(host).some((plugin) => plugin.scope !== "user") | ||
| ); | ||
| if (projectPlugins.length > 0) { | ||
| const names = await this.pluginUpdate.execute({ | ||
| toolIds: projectPlugins, | ||
| projectRoot: options.projectRoot, | ||
| rebuild: true, | ||
| }); | ||
| for (const name of names) rewritten.add(name); | ||
| } | ||
| if (installed.length > 0) await this.activate(options.projectRoot, installed); | ||
| if (options.scope === "user") { | ||
| const machine = options.hosts.filter((host) => userManifest?.hasTool(host) === true); | ||
| if (machine.length > 0) await this.activate(options.projectRoot, machine, true); | ||
| } | ||
| return [...rewritten]; | ||
| } | ||
|
|
||
| private async activate(projectRoot: string, toolIds: AgentHost[], user = false): Promise<void> { | ||
| const { errors } = await this.activation.execute({ | ||
| projectRoot, | ||
| toolIds, | ||
| ...(user ? { scope: "user" as const, manifestRepo: this.userManifestRepo } : {}), | ||
| }); | ||
| if (errors.length > 0) throw new Error(errors.map((error) => error.message).join("\n")); | ||
| } | ||
| } |
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
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.