Add reusable agent profiles (#3208)

* Search models across all providers from the picker root

* Store agent profiles in daemon config and apply agent config in one RPC

An agent profile is a named bundle of provider, model, mode, thinking
option, feature values and notes, stored host-wide next to terminal
profiles. It carries no system prompt: AgentSessionConfig.systemPrompt is
creation-only, so a profile holding one would apply when starting an agent
and silently do nothing when applied to a running one.

Applying a profile through the four single-field RPCs can fail halfway and
leave an agent matching no profile, so agent.config.apply takes the whole
bundle. Model runs first because a thinking option only means anything
against the model that offers it.

Old daemons parse their persisted config strictly and would drop an
unknown agentProfiles key, making a save appear to succeed and vanish, so
both capabilities are advertised on server_info for the client to gate on.

list_profiles exposes the profiles to orchestrating agents, which read the
notes to pick one and copy its values into create_agent.

* Add the agent profiles settings section

Profiles live behind one module boundary so removing the feature is a
directory plus one mount, which is what favourites never was. The picker
and composer will ask for rows and an apply function rather than learning
what a profile contains.

The edit form follows the form-model pattern instead of the terminal
profile modal's per-field state, because provider selection cascades into
which models, modes, thinking levels and features exist below it.

The section is gated on server_info.features.agentProfiles: an older daemon
drops the key on write, and a save that appears to work and then vanishes
is worse than an unavailable state.

* Document agent profiles and name the four senses of profile

The glossary reserved Profile for HostProfile, terminal profiles owned the
user-facing word, and custom-providers.md used it for provider aliases.
Agent profile is now the term, with all four cross-referenced.

data-model.md never documented terminalProfiles either, so both profile
lists are backfilled together along with the whole-list patch semantics and
why absent and empty differ.

* Pin agent profiles to the model picker and delete favourites

Applying a profile writes its values into the agent controls and is then
forgotten, so rows carry no checkmark and no selected state: after applying,
nothing is selected. Chevron still means navigate deeper, so the absence of
one is what marks a row as an action.

A running agent is its provider's process and agent.config.apply carries no
provider, so the live picker only offers profiles matching the agent's
provider rather than showing rows that cannot do what they say. The draft
path has no such limit and switches provider freely.

Draft feature values go through preferences rather than the feature
callback alone, because the draft feature hook clears local values whenever
the provider changes and a provider-switching profile would lose them.

Favourites are deleted rather than migrated: they were device-local
AsyncStorage keyed by provider and model, while profiles are host-wide
daemon config. ProviderSelectionModelRow.favoriteKey stays as row identity
and moves to its real owner; renaming it is its own commit.

* Cover agent profiles and cross-provider search with E2E specs

Model rows expressed selection only as a checkmark glyph, which assistive
tech cannot see and a spec cannot assert. Rows that draw the indicator now
carry aria-selected; profile rows deliberately carry none, because a
profile is an action rather than a selection. That is what makes "nothing
is marked selected after applying a profile" a real assertion instead of a
tautology, with the materialized model one level down as the control.

The search fixture extends claude rather than mock: provider-config.ts
only allows the six shipped providers as extends targets, so a derived
provider cannot inherit the dev-only mock provider.

* State what applying a config bundle does on partial failure

applyBundle is not a transaction and the earlier commit message implied it
was. A step that rejects aborts the rest and the steps that already applied
stay applied, which a profile omitting modelId can reach: provider setters
validate against the agent's live model, so mode can land while a feature
that model does not support is rejected.

Nothing is hidden by that. Every setter emits agent state before returning,
so what applied is streamed to the client next to the rejected response.
Rolling back would re-invoke the setters that just failed, usually against
a session that is gone, and pre-flight validation would restate each
provider's model-dependent checks where they would drift from the setters
that own them.

What sending the bundle in one request removes is the multi-round-trip
failure class: interruption between hops, and interleaving with another
client. The test already pinned this behaviour; its name now says so.

* feat(agent-profiles): refine profile discovery and appearance

Make profiles discoverable from empty and provider-scoped model pickers, keep global search stable and virtualized, and move profile settings under Agents. Replace emoji identity with a shared Lucide icon and host-color picker, while requiring concrete provider selections so profiles materialize predictably.

* fix(agent-profiles): defer profile swatch styles

Resolving Unistyles proxies at module load can freeze the temporary startup color scheme before the persisted theme is restored. Keep only style-key names at module scope and resolve the selected swatch during render.
This commit is contained in:
Mohamed Boudra
2026-08-11 17:42:35 +02:00
committed by GitHub
parent 07044136c8
commit 308e9af845
72 changed files with 6348 additions and 538 deletions
@@ -0,0 +1,199 @@
import { describe, expect, it } from "vitest";
import type { AgentProfile } from "@getpaseo/protocol/messages";
import { LegacyFavoriteProfileMigration } from ".";
class MemoryStorage {
private readonly values = new Map<string, string>();
constructor(initial: Record<string, unknown> = {}) {
for (const [key, value] of Object.entries(initial)) {
this.values.set(key, JSON.stringify(value));
}
}
async getItem(key: string): Promise<string | null> {
return this.values.get(key) ?? null;
}
async setItem(key: string, value: string): Promise<void> {
this.values.set(key, value);
}
}
class FakeProfileHost {
readonly patches: AgentProfile[][] = [];
config: { agentProfiles?: AgentProfile[] };
shouldFailPatch = false;
shouldFailCatalog = false;
catalogLoadingReads = 0;
constructor(
profiles: AgentProfile[] = [],
readonly supported = true,
) {
this.config = { agentProfiles: profiles };
}
getLastServerInfoMessage() {
return {
features: this.supported
? { agentProfiles: true, agentConfigApply: true }
: { agentProfiles: false, agentConfigApply: false },
};
}
async getDaemonConfig() {
return { requestId: "config", config: this.config };
}
async patchDaemonConfig(patch: { agentProfiles?: AgentProfile[] }) {
if (this.shouldFailPatch) {
throw new Error("host write failed");
}
const profiles = patch.agentProfiles ?? [];
this.patches.push(profiles);
this.config = { ...this.config, agentProfiles: profiles };
return { requestId: "patch", config: this.config };
}
async getProvidersSnapshot() {
if (this.shouldFailCatalog) {
throw new Error("catalog unavailable");
}
if (this.catalogLoadingReads > 0) {
this.catalogLoadingReads -= 1;
return {
entries: [
{
provider: "claude" as const,
status: "loading" as const,
enabled: true,
},
],
generatedAt: new Date(0).toISOString(),
requestId: "providers-loading",
};
}
return {
entries: [
{
provider: "claude" as const,
status: "ready" as const,
enabled: true,
label: "Claude Code",
models: [
{ provider: "claude" as const, id: "opus", label: "Opus" },
{ provider: "claude" as const, id: "sonnet", label: "Sonnet" },
],
},
],
generatedAt: new Date(0).toISOString(),
requestId: "providers",
};
}
}
const PREFERENCES_KEY = "@paseo:create-agent-preferences";
function migrationStorage() {
return new MemoryStorage({
[PREFERENCES_KEY]: {
favoriteModels: [
{ provider: "claude", modelId: "opus" },
{ provider: "claude", modelId: "sonnet" },
{ provider: "claude", modelId: "opus" },
],
},
});
}
describe("legacy favourite profile migration", () => {
it("appends provider-and-model-only profiles with useful catalog names", async () => {
const existing: AgentProfile = {
id: "hand-written",
name: "My Opus",
provider: "claude",
model: "opus",
modeId: "plan",
};
const host = new FakeProfileHost([existing]);
const migration = new LegacyFavoriteProfileMigration(migrationStorage());
await migration.migrateHost("host-a", host);
expect(host.patches).toEqual([
[
existing,
{
id: "legacy_favorite:claude:sonnet",
name: "Sonnet",
provider: "claude",
model: "sonnet",
},
],
]);
});
it("runs once per host, even if the imported profiles are later deleted", async () => {
const storage = migrationStorage();
const migration = new LegacyFavoriteProfileMigration(storage);
const firstHost = new FakeProfileHost();
await migration.migrateHost("host-a", firstHost);
firstHost.config = { agentProfiles: [] };
await migration.migrateHost("host-a", firstHost);
const secondHost = new FakeProfileHost();
await migration.migrateHost("host-b", secondHost);
expect(firstHost.patches).toHaveLength(1);
expect(secondHost.patches).toHaveLength(1);
});
it("waits for profile capabilities without consuming the host migration", async () => {
const storage = migrationStorage();
const migration = new LegacyFavoriteProfileMigration(storage);
const oldHost = new FakeProfileHost([], false);
await migration.migrateHost("host-a", oldHost);
const upgradedHost = new FakeProfileHost();
await migration.migrateHost("host-a", upgradedHost);
expect(oldHost.patches).toHaveLength(0);
expect(upgradedHost.patches).toHaveLength(1);
});
it("retries after a failed host write", async () => {
const host = new FakeProfileHost();
const migration = new LegacyFavoriteProfileMigration(migrationStorage());
host.shouldFailPatch = true;
await expect(migration.migrateHost("host-a", host)).rejects.toThrow("host write failed");
host.shouldFailPatch = false;
await migration.migrateHost("host-a", host);
expect(host.patches).toHaveLength(1);
});
it("retries catalog resolution instead of permanently storing raw ids", async () => {
const host = new FakeProfileHost();
const migration = new LegacyFavoriteProfileMigration(migrationStorage());
host.shouldFailCatalog = true;
await expect(migration.migrateHost("host-a", host)).rejects.toThrow("catalog unavailable");
host.shouldFailCatalog = false;
await migration.migrateHost("host-a", host);
expect(host.patches[0]?.map((profile) => profile.name)).toEqual(["Opus", "Sonnet"]);
});
it("waits for relevant provider models to finish loading", async () => {
const host = new FakeProfileHost();
const migration = new LegacyFavoriteProfileMigration(migrationStorage());
host.catalogLoadingReads = 1;
await migration.migrateHost("host-a", host);
expect(host.patches[0]?.map((profile) => profile.name)).toEqual(["Opus", "Sonnet"]);
});
});
@@ -0,0 +1,231 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
import { z } from "zod";
import type { ProviderSnapshotEntry } from "@getpaseo/protocol/agent-types";
import type { AgentProfile } from "@getpaseo/protocol/messages";
const PREFERENCES_KEY = "@paseo:create-agent-preferences";
const COMPLETION_KEY_PREFIX = "@paseo:legacy-favorites-to-agent-profiles:v1:";
const CATALOG_LOADING_RETRY_DELAYS_MS = [100, 250, 500, 1_000, 2_000, 4_000] as const;
const legacyPreferencesSchema = z.object({
favoriteModels: z
.array(
z.object({
provider: z.string().trim().min(1),
modelId: z.string().trim().min(1),
}),
)
.optional(),
});
type LegacyFavorite = NonNullable<
z.infer<typeof legacyPreferencesSchema>["favoriteModels"]
>[number];
export interface LegacyFavoriteMigrationStorage {
getItem(key: string): Promise<string | null>;
setItem(key: string, value: string): Promise<void>;
}
export interface LegacyFavoriteMigrationHost {
getLastServerInfoMessage(): {
features?: { agentProfiles?: boolean; agentConfigApply?: boolean };
} | null;
getDaemonConfig(): Promise<{ config: { agentProfiles?: AgentProfile[] } }>;
patchDaemonConfig(patch: {
agentProfiles: AgentProfile[];
}): Promise<{ config: { agentProfiles?: AgentProfile[] } }>;
getProvidersSnapshot(): Promise<{ entries: ProviderSnapshotEntry[] }>;
}
function supportsMigration(host: LegacyFavoriteMigrationHost): boolean {
const features = host.getLastServerInfoMessage()?.features;
return features?.agentProfiles === true && features.agentConfigApply === true;
}
function completionKey(serverId: string): string {
return `${COMPLETION_KEY_PREFIX}${serverId}`;
}
function parseStoredPreferences(raw: string | null): LegacyFavorite[] {
if (!raw) {
return [];
}
try {
const result = legacyPreferencesSchema.safeParse(JSON.parse(raw));
return result.success ? (result.data.favoriteModels ?? []) : [];
} catch {
return [];
}
}
function favoriteKey(favorite: Pick<LegacyFavorite, "provider" | "modelId">): string {
return `${favorite.provider}\u0000${favorite.modelId}`;
}
function migratedProfileId(favorite: LegacyFavorite): string {
return `legacy_favorite:${encodeURIComponent(favorite.provider)}:${encodeURIComponent(favorite.modelId)}`;
}
function deduplicateFavorites(favorites: readonly LegacyFavorite[]): LegacyFavorite[] {
const seen = new Set<string>();
return favorites.filter((favorite) => {
const key = favoriteKey(favorite);
if (seen.has(key)) {
return false;
}
seen.add(key);
return true;
});
}
function findCatalogLabels(
favorite: LegacyFavorite,
entries: readonly ProviderSnapshotEntry[],
): { model: string; provider: string } {
const provider = entries.find((entry) => entry.provider === favorite.provider);
const model = provider?.models?.find((entry) => entry.id === favorite.modelId);
return {
model: model?.label?.trim() || favorite.modelId,
provider: provider?.label?.trim() || favorite.provider,
};
}
function uniqueProfileName(input: {
favorite: LegacyFavorite;
entries: readonly ProviderSnapshotEntry[];
usedNames: Set<string>;
}): string {
const labels = findCatalogLabels(input.favorite, input.entries);
const candidates = [
labels.model,
`${labels.provider} · ${labels.model}`,
`${labels.provider} · ${labels.model} (${input.favorite.modelId})`,
];
for (const candidate of candidates) {
if (!input.usedNames.has(candidate)) {
input.usedNames.add(candidate);
return candidate;
}
}
let suffix = 2;
while (input.usedNames.has(`${candidates[2]} ${suffix}`)) {
suffix += 1;
}
const name = `${candidates[2]} ${suffix}`;
input.usedNames.add(name);
return name;
}
function buildMigratedProfiles(input: {
favorites: readonly LegacyFavorite[];
existing: readonly AgentProfile[];
entries: readonly ProviderSnapshotEntry[];
}): AgentProfile[] {
const existingSelections = new Set(
input.existing.map((profile) =>
favoriteKey({ provider: profile.provider, modelId: profile.model?.trim() ?? "" }),
),
);
const existingIds = new Set(input.existing.map((profile) => profile.id));
const usedNames = new Set(input.existing.map((profile) => profile.name));
return deduplicateFavorites(input.favorites).flatMap((favorite) => {
const id = migratedProfileId(favorite);
if (existingSelections.has(favoriteKey(favorite)) || existingIds.has(id)) {
return [];
}
return [
{
id,
name: uniqueProfileName({ favorite, entries: input.entries, usedNames }),
provider: favorite.provider,
model: favorite.modelId,
},
];
});
}
function catalogIsLoading(
entries: readonly ProviderSnapshotEntry[],
favorites: readonly LegacyFavorite[],
): boolean {
const providers = new Set(favorites.map((favorite) => favorite.provider));
return entries.some((entry) => providers.has(entry.provider) && entry.status === "loading");
}
function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function readProviderCatalog(
host: LegacyFavoriteMigrationHost,
favorites: readonly LegacyFavorite[],
): Promise<ProviderSnapshotEntry[]> {
for (const retryDelay of CATALOG_LOADING_RETRY_DELAYS_MS) {
const entries = (await host.getProvidersSnapshot()).entries;
if (!catalogIsLoading(entries, favorites)) {
return entries;
}
await delay(retryDelay);
}
const entries = (await host.getProvidersSnapshot()).entries;
if (catalogIsLoading(entries, favorites)) {
throw new Error("Provider catalog remained loading during legacy favourite migration");
}
return entries;
}
/**
* One-time bridge from device-local model favourites to host-wide profiles.
* The module owns every migration invariant; callers only announce a connected
* host and may safely call again after reconnects or React remounts.
*/
export class LegacyFavoriteProfileMigration {
private readonly inFlight = new Map<string, Promise<void>>();
constructor(private readonly storage: LegacyFavoriteMigrationStorage) {}
migrateHost(serverId: string, host: LegacyFavoriteMigrationHost): Promise<void> {
const active = this.inFlight.get(serverId);
if (active) {
return active;
}
const migration = this.run(serverId, host).finally(() => {
if (this.inFlight.get(serverId) === migration) {
this.inFlight.delete(serverId);
}
});
this.inFlight.set(serverId, migration);
return migration;
}
private async run(serverId: string, host: LegacyFavoriteMigrationHost): Promise<void> {
if (!supportsMigration(host)) {
return;
}
const markerKey = completionKey(serverId);
if ((await this.storage.getItem(markerKey)) === "1") {
return;
}
const favorites = parseStoredPreferences(await this.storage.getItem(PREFERENCES_KEY));
if (favorites.length === 0) {
await this.storage.setItem(markerKey, "1");
return;
}
const [{ config }, entries] = await Promise.all([
host.getDaemonConfig(),
readProviderCatalog(host, favorites),
]);
const existing = config.agentProfiles ?? [];
const migrated = buildMigratedProfiles({ favorites, existing, entries });
if (migrated.length > 0) {
await host.patchDaemonConfig({ agentProfiles: [...existing, ...migrated] });
}
await this.storage.setItem(markerKey, "1");
}
}
export const legacyFavoriteProfileMigration = new LegacyFavoriteProfileMigration(AsyncStorage);