-
Notifications
You must be signed in to change notification settings - Fork 7
Extract ActivityCard model colors to lib/constants/model-colors.ts #143
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6c27bf1
Extract ActivityCard model colors to lib/constants/model-colors.ts
claude d98e6eb
Merge main into model-colors extraction, fix negative-hash palette index
ohong a360d03
Merge remote-tracking branch 'origin/main' into oh-nightly/model-colo…
ohong e0f4d63
Merge remote-tracking branch 'origin/main' into oh-nightly/model-colo…
ohong 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,60 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { prettifyModel } from "@straude/shared/models"; | ||
| import { | ||
| MODEL_COLOR_FALLBACK_PALETTE, | ||
| MODEL_COLOR_PATTERNS, | ||
| modelColor, | ||
| } from "@/lib/constants/model-colors"; | ||
|
|
||
| describe("modelColor", () => { | ||
| it("maps every known family to its chip colour", () => { | ||
| // Keyed off prettifyModel output, which is what ActivityCard passes in — | ||
| // a pattern that doesn't match the display names is dead code. | ||
| expect(modelColor(prettifyModel("claude-fable-5"))).toBe("#C2410C"); | ||
| expect(modelColor(prettifyModel("claude-opus-5"))).toBe("#DF561F"); | ||
| expect(modelColor(prettifyModel("claude-sonnet-5"))).toBe("#F08A5D"); | ||
| expect(modelColor(prettifyModel("claude-haiku-4-5-20251001"))).toBe("#F7B267"); | ||
| expect(modelColor(prettifyModel("gpt-5.6-codex"))).toBe("#2A9D8F"); | ||
| expect(modelColor(prettifyModel("gpt-4o"))).toBe("#4C78A8"); | ||
| expect(modelColor(prettifyModel("o3-mini"))).toBe("#3B82F6"); | ||
| expect(modelColor(prettifyModel("o4-mini"))).toBe("#6366F1"); | ||
| }); | ||
|
|
||
| it("keeps Fable ahead of the broader Claude patterns", () => { | ||
| // First match wins, so reordering MODEL_COLOR_PATTERNS silently repaints | ||
| // chips. Fable and Opus share the accent family and are easy to swap. | ||
| const order = MODEL_COLOR_PATTERNS.map(([pattern]) => pattern.source); | ||
| expect(order.indexOf("Claude Fable")).toBeLessThan(order.indexOf("Claude Opus")); | ||
| }); | ||
|
|
||
| it("always returns a colour for an unknown model", () => { | ||
| // hashString is a signed 32-bit value, so roughly half of these names | ||
| // hashed negative and indexed off the front of the palette, handing the | ||
| // chip an undefined backgroundColor. Now that collection accepts every | ||
| // ccusage source, these names reach the feed for real. | ||
| const unknown = [ | ||
| "kimi-k2", | ||
| "glm-4.6", | ||
| "deepseek-v3", | ||
| "grok-code-fast-1", | ||
| "qwen3-coder", | ||
| "mistral-large", | ||
| "gemini-2.5-pro", | ||
| "llama-4", | ||
| ]; | ||
|
|
||
| for (const name of unknown) { | ||
| expect(MODEL_COLOR_FALLBACK_PALETTE).toContain( | ||
| modelColor(prettifyModel(name)), | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it("is stable for the same name", () => { | ||
| expect(modelColor("kimi-k2")).toBe(modelColor("kimi-k2")); | ||
| }); | ||
|
|
||
| it("handles the empty name without throwing", () => { | ||
| expect(MODEL_COLOR_FALLBACK_PALETTE).toContain(modelColor("")); | ||
| }); | ||
| }); |
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,47 @@ | ||
| /** Fallback palette for models that don't match a known name pattern. */ | ||
| export const MODEL_COLOR_FALLBACK_PALETTE = [ | ||
| "#EF4444", | ||
| "#F59E0B", | ||
| "#10B981", | ||
| "#06B6D4", | ||
| "#8B5CF6", | ||
| "#EC4899", | ||
| ] as const; | ||
|
|
||
| /** Ordered [pattern, color] pairs for known model families. First match wins, | ||
| * so a more specific pattern must come before a broader one. Inputs are the | ||
| * display names produced by `prettifyModel`, not raw model IDs. */ | ||
| export const MODEL_COLOR_PATTERNS: readonly (readonly [RegExp, string])[] = [ | ||
| [/Claude Fable/, "#C2410C"], | ||
| [/Claude Opus/, "#DF561F"], | ||
| [/Claude Sonnet/, "#F08A5D"], | ||
| [/Claude Haiku/, "#F7B267"], | ||
| [/GPT-5/, "#2A9D8F"], | ||
| [/GPT-4o/, "#4C78A8"], | ||
| [/^o3/i, "#3B82F6"], | ||
| [/^o4/i, "#6366F1"], | ||
| ] as const; | ||
|
|
||
| /** Stable chip colour for a model display name. Never returns undefined: every | ||
| * name that misses the known-family patterns still gets a deterministic | ||
| * palette entry, which matters now that collection accepts every ccusage | ||
| * source (Gemini, Kimi, DeepSeek, Qwen, …) rather than just Claude/Codex. */ | ||
| export function modelColor(name: string): string { | ||
| for (const [pattern, color] of MODEL_COLOR_PATTERNS) { | ||
| if (pattern.test(name)) return color; | ||
| } | ||
|
|
||
| const index = hashString(name) % MODEL_COLOR_FALLBACK_PALETTE.length; | ||
| return MODEL_COLOR_FALLBACK_PALETTE[index]!; | ||
| } | ||
|
|
||
| function hashString(input: string): number { | ||
| let hash = 0; | ||
| for (let i = 0; i < input.length; i++) { | ||
| hash = (hash << 5) - hash + input.charCodeAt(i); | ||
| hash |= 0; | ||
| } | ||
| // `|= 0` yields a signed 32-bit int, so the hash is negative about half the | ||
| // time and `hash % length` would index off the front of the palette. | ||
| return Math.abs(hash); | ||
| } |
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.
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Do not document the fallback-index fix until the implementation normalizes negative indexes.
apps/web/lib/constants/model-colors.tsstill useshashString(name) % MODEL_COLOR_FALLBACK_PALETTE.length. A negative signed hash produces a negative array property, somodelColor()can still returnundefined.ActivityCardthen receives an invalidbackgroundColor.Normalize the modulo result before updating this changelog entry.
Proposed fix
🤖 Prompt for AI Agents