-
Notifications
You must be signed in to change notification settings - Fork 110
Add global starred models to the model picker #279
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
Open
NickTitle
wants to merge
1
commit into
main
Choose a base branch
from
starred-models-block
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.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { useCallback, useSyncExternalStore } from "react"; | ||
| import type { ModelOption } from "../types"; | ||
| import { | ||
| getStarredModels, | ||
| modelStarKey, | ||
| STARRED_MODELS_CHANGED_EVENT, | ||
| STARRED_MODELS_STORAGE_KEY, | ||
| starredModelKey, | ||
| toggleModelStar, | ||
| type StarredModelRecord, | ||
| } from "../lib/starredModels"; | ||
|
|
||
| const EMPTY_RECORDS: StarredModelRecord[] = []; | ||
| let cachedSnapshot: StarredModelRecord[] | null = null; | ||
|
|
||
| export function __resetStarredModelsCacheForTests(): void { | ||
| cachedSnapshot = null; | ||
| } | ||
|
|
||
| function getSnapshot(): StarredModelRecord[] { | ||
| cachedSnapshot ??= getStarredModels(); | ||
| return cachedSnapshot; | ||
| } | ||
|
|
||
| function subscribe(callback: () => void): () => void { | ||
| const update = () => { | ||
| cachedSnapshot = null; | ||
| callback(); | ||
| }; | ||
| const onStorage = (event: StorageEvent) => { | ||
| if (event.key === null || event.key === STARRED_MODELS_STORAGE_KEY) | ||
| update(); | ||
| }; | ||
| window.addEventListener(STARRED_MODELS_CHANGED_EVENT, update); | ||
| window.addEventListener("storage", onStorage); | ||
| return () => { | ||
| window.removeEventListener(STARRED_MODELS_CHANGED_EVENT, update); | ||
| window.removeEventListener("storage", onStorage); | ||
| }; | ||
| } | ||
|
|
||
| export function useStarredModels() { | ||
| const starredModels = useSyncExternalStore( | ||
| subscribe, | ||
| getSnapshot, | ||
| () => EMPTY_RECORDS, | ||
| ); | ||
| const starredKeys = new Set(starredModels.map(starredModelKey)); | ||
| const isStarred = useCallback( | ||
| (agentId: string, model: ModelOption) => | ||
| starredKeys.has(modelStarKey(agentId, model.providerId, model.id)), | ||
| [starredKeys], | ||
| ); | ||
|
|
||
| return { | ||
| starredModels, | ||
| isStarred, | ||
| toggleStar: useCallback((agentId: string, model: ModelOption) => { | ||
| toggleModelStar(agentId, model); | ||
| }, []), | ||
| }; | ||
| } |
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,86 @@ | ||
| import type { ModelOption } from "../types"; | ||
|
|
||
| export const STARRED_MODELS_STORAGE_KEY = "berd:starred-models-v2"; | ||
| export const STARRED_MODELS_CHANGED_EVENT = "berd:starred-models-v2-changed"; | ||
|
|
||
| export interface StarredModelRecord { | ||
| agentId: string; | ||
| model: ModelOption; | ||
| } | ||
|
|
||
| export function modelStarKey( | ||
| agentId: string, | ||
| modelProviderId: string | undefined, | ||
| modelId: string, | ||
| ): string { | ||
| return JSON.stringify([agentId, modelProviderId ?? "", modelId]); | ||
| } | ||
|
|
||
| export function starredModelKey(record: StarredModelRecord): string { | ||
| return modelStarKey(record.agentId, record.model.providerId, record.model.id); | ||
| } | ||
|
|
||
| function isModelOption(value: unknown): value is ModelOption { | ||
| if (!value || typeof value !== "object") return false; | ||
| const model = value as Partial<ModelOption>; | ||
| return typeof model.id === "string" && typeof model.name === "string"; | ||
| } | ||
|
|
||
| function isStarredModelRecord(value: unknown): value is StarredModelRecord { | ||
| if (!value || typeof value !== "object") return false; | ||
| const record = value as Partial<StarredModelRecord>; | ||
| return typeof record.agentId === "string" && isModelOption(record.model); | ||
| } | ||
|
|
||
| export function getStarredModels(): StarredModelRecord[] { | ||
| if (typeof window === "undefined") return []; | ||
|
|
||
| try { | ||
| const parsed: unknown = JSON.parse( | ||
| window.localStorage.getItem(STARRED_MODELS_STORAGE_KEY) ?? "[]", | ||
| ); | ||
| if (!Array.isArray(parsed)) return []; | ||
|
|
||
| const seen = new Set<string>(); | ||
| return parsed.filter((value): value is StarredModelRecord => { | ||
| if (!isStarredModelRecord(value)) return false; | ||
| const key = starredModelKey(value); | ||
| if (seen.has(key)) return false; | ||
| seen.add(key); | ||
| return true; | ||
| }); | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| function persistStarredModels(records: StarredModelRecord[]): void { | ||
| try { | ||
| if (records.length === 0) { | ||
| window.localStorage.removeItem(STARRED_MODELS_STORAGE_KEY); | ||
| } else { | ||
| window.localStorage.setItem( | ||
| STARRED_MODELS_STORAGE_KEY, | ||
| JSON.stringify(records), | ||
| ); | ||
| } | ||
| } catch { | ||
| // localStorage may be unavailable. | ||
| } | ||
| window.dispatchEvent(new CustomEvent(STARRED_MODELS_CHANGED_EVENT)); | ||
| } | ||
|
|
||
| export function toggleModelStar(agentId: string, model: ModelOption): void { | ||
| const records = getStarredModels(); | ||
| const key = modelStarKey(agentId, model.providerId, model.id); | ||
| const existingIndex = records.findIndex( | ||
| (record) => starredModelKey(record) === key, | ||
| ); | ||
|
|
||
| if (existingIndex >= 0) { | ||
| records.splice(existingIndex, 1); | ||
| } else { | ||
| records.push({ agentId, model }); | ||
| } | ||
| persistStarredModels(records); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 P1 · Cross-agent star borrows provider (blocking)
When a starred row belongs to a different agent but has no providerId, nextModelProviderId falls back to the current session's modelProviderId. A Claude star selected from a Goose/OpenAI session can therefore produce a target that combines the Claude harness with the unrelated OpenAI provider. The added component test checks only the callback shape and does not exercise this target derivation.
User effect: Choosing a valid favorite from another agent can fail, roll back, or configure the chat for a provider that does not match the model they chose.
Recommended fix: Reuse the session provider only when the target agent is still the selected agent. Resolve cross-agent selections from that target agent's current inventory; derive non-Goose provider identity from the target agent and require a concrete provider for Goose.
Test: Add a hook-level regression test that selects a providerless claude-acp star from a Goose session using OpenAI and asserts the resulting target uses claude-acp rather than openai.