Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
"dependencies": {
"read-cmd-shim": "^5.0.0",
"cross-spawn": "^7.0.6",
"@anthropic-ai/claude-agent-sdk": "^0.3.170",
"@anthropic-ai/claude-agent-sdk": "^0.3.291",
"@modelcontextprotocol/sdk": "^1.29.0",
"@openai/codex-sdk": "^0.139.0",
"@openai/codex-sdk": "^0.160.1",
"@solana/spl-token": "^0.4.14",
"@solana/spl-token-group": "^0.0.7",
"@solana/spl-token-metadata": "^0.1.6",
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/chat/contextNotice.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { expect, it } from "vitest";
import { contextNotice } from "./contextNotice.js";

it("retains the standard engine breakdown and supplied window", () => {
it("uses supplied capacity without inventing a model limit or compaction policy", () => {
expect(contextNotice("claude", 10000, 100000)).toContain("10,000 / 100,000 (10%)");
expect(contextNotice("codex")).toContain("256,000");
expect(contextNotice("codex")).toBe("Context (codex): usage not reported yet. Model context limit unknown.");
expect(contextNotice("claude", 10000)).toContain("10,000 tokens last reported");
expect(contextNotice("claude", 10000, 100000)).not.toContain("auto-compact");
});
it("Custom never presents a Codex fallback as provider capacity", () => {
expect(contextNotice("custom", 12000, 256000)).toBe("Context (custom): 12,000 tokens used. Provider context limit unknown.");
expect(contextNotice("custom", 12000, 256000)).toBe("Context (custom): 12,000 tokens last reported. Model context limit unknown.");
expect(contextNotice("custom")).toContain("usage not reported yet");
});
26 changes: 8 additions & 18 deletions packages/core/src/chat/contextNotice.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import type { EngineKey } from "../runtime/engineRegistry.js";

// Shared by the terminal and host-dispatched /context command.
// Shared by the terminal and host-dispatched /context command. These are the
// engine's last reported snapshot, not cumulative billing or a compaction forecast.
export function contextNotice(cli: EngineKey, used?: number, reportedWindow?: number): string {
const fmt = (n: number) => Math.round(n).toLocaleString("en-US");
// Custom runs through Codex, whose reported window may be its own fallback.
// It is not evidence of the configured provider's capacity or compaction policy.
if (cli === "custom") {
return used === undefined
? "Context: usage not reported yet. Provider context limit unknown."
: `Context (custom): ${fmt(used)} tokens used. Provider context limit unknown.`;
}
const window = reportedWindow ?? (cli === "claude" ? 200_000 : 256_000);
if (used === undefined) return `Context: 0 / ${fmt(window)} tokens. Send a message to measure usage.`;
const free = Math.max(0, window - used);
const pct = Math.round((used / window) * 100);
const threshold = Math.max(0, window - 33_000);
const tpct = Math.round((threshold / window) * 100);
return `Context window (${cli})\n`
+ ` used ${fmt(used)} / ${fmt(window)} (${pct}%)\n`
+ ` free ${fmt(free)}\n`
+ ` auto-compact at ~${fmt(threshold)} (${tpct}%)`;
const window = cli !== "custom" && reportedWindow && Number.isFinite(reportedWindow) && reportedWindow > 0 ? reportedWindow : undefined;
if (used === undefined) return `Context (${cli}): usage not reported yet. ${window ? `Model context limit ${fmt(window)}.` : "Model context limit unknown."}`;
if (!window) return `Context (${cli}): ${fmt(used)} tokens last reported. Model context limit unknown.`;
return `Context snapshot (${cli})\n`
+ ` used ${fmt(used)} / ${fmt(window)} (${Math.round((used / window) * 100)}%)\n`
+ ` free ${fmt(Math.max(0, window - used))}`;
}
23 changes: 23 additions & 0 deletions packages/core/src/chat/modelOptions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from "vitest";
import { findChatModelOption, modelEffortOptions, normalizeModelEffort, type ChatModelOption } from "./modelOptions.js";

const catalog: ChatModelOption[] = [
{ value: "latest", resolvedModel: "canonical-latest", chipLabel: "Latest", label: "Latest", description: "", supportedEfforts: ["low", "ultra"] },
{ value: "fast", chipLabel: "Fast", label: "Fast", description: "", supportedEfforts: [] },
];
describe("model capabilities", () => {
it("uses the recommendation for default, while preserving unknown explicit IDs", () => {
expect(findChatModelOption(catalog)).toBe(catalog[0]);
expect(findChatModelOption(catalog, "default")).toBe(catalog[0]);
expect(findChatModelOption(catalog, "fast")).toBe(catalog[1]);
expect(findChatModelOption(catalog, "canonical-latest")).toBe(catalog[0]);
expect(findChatModelOption(catalog, "unlisted")).toBeUndefined();
});
it("accepts new advertised effort levels and clears unsupported selections", () => {
expect(modelEffortOptions(catalog[0]).map(option => option.value)).toEqual(["default", "low", "ultra"]);
expect(normalizeModelEffort(catalog[0], "ultra")).toBe("ultra");
expect(normalizeModelEffort(catalog[1], "ultra")).toBeUndefined();
expect(modelEffortOptions(undefined).map(option => option.value)).toEqual(["default"]);
expect(normalizeModelEffort(undefined, "high")).toBeUndefined();
});
});
46 changes: 21 additions & 25 deletions packages/core/src/chat/modelOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,36 @@ export type ChatModelOption = {
chipLabel: string;
label: string;
description: string;
resolvedModel?: string;
supportedEfforts?: string[];
};

// One shared model catalog for every surface. The runtime only cares about the raw
// `value` (passed as the CLI/app-server model override); surfaces use the richer
// labels/descriptions so the picker is understandable instead of exposing bare aliases.
// No bare "default" pseudo-entry: the first real model is the sensible default, and the
// picker shows its actual name (e.g. "Opus 5") instead of an opaque "default" chip.
// This is only the offline/fallback baseline — surfaces upgrade to the CLI's live list.
export const CHAT_MODEL_OPTIONS: Record<EngineKey, ChatModelOption[]> = {
claude: [
{
value: "opus",
chipLabel: "Opus 5",
label: "Opus 5",
chipLabel: "Opus",
label: "Opus",
description: "Most capable · Claude alias: opus",
},
{
value: "sonnet",
chipLabel: "Sonnet 5",
label: "Sonnet 5",
chipLabel: "Sonnet",
label: "Sonnet",
description: "Balanced · Claude alias: sonnet",
},
{
value: "haiku",
chipLabel: "Haiku 4.5",
label: "Haiku 4.5",
chipLabel: "Haiku",
label: "Haiku",
description: "Fastest · Claude alias: haiku",
},
],
codex: [
{
value: "gpt-5.5-codex",
chipLabel: "GPT-5.5 Codex",
label: "GPT-5.5 Codex",
description: "Coding-tuned · exact value: gpt-5.5-codex",
},
{
value: "gpt-5.5",
chipLabel: "GPT-5.5",
label: "GPT-5.5",
description: "General GPT model · exact value: gpt-5.5",
},
],
codex: [{ chipLabel: "Default", label: "Default", description: "CLI default · live model catalog unavailable" }],
// The custom engine has no catalog: its one model is whatever the saved endpoint
// config names, which lives on the host side. Surfaces build the entry with
// customModelOption from the stored config and push it over their live channel.
Expand All @@ -69,7 +56,16 @@ export function customModelOption(model: string, label?: string): ChatModelOptio
}];
}

export function findChatModelOption(cli: EngineKey, model?: string): ChatModelOption | undefined {
const opts = CHAT_MODEL_OPTIONS[cli];
return opts.find((opt) => (opt.value ?? "default") === (model ?? "default"));
export function findChatModelOption(catalog: EngineKey | ChatModelOption[], model?: string): ChatModelOption | undefined {
const options = typeof catalog === "string" ? CHAT_MODEL_OPTIONS[catalog] : catalog;
return !model || model === "default" ? options[0] : options.find(option => option.value === model || option.resolvedModel === model);
}

export function normalizeModelEffort(model: ChatModelOption | undefined, effort?: string): string | undefined {
return effort && model?.supportedEfforts?.includes(effort) ? effort : undefined;
}

// Omitted capability metadata means unknown, not support for every engine's levels.
export function modelEffortOptions(model?: ChatModelOption): { value: string; label: string }[] {
return [{ value: "default", label: "default" }, ...(model?.supportedEfforts ?? []).map(value => ({ value, label: value === "xhigh" ? "x-high" : value }))];
}
85 changes: 83 additions & 2 deletions packages/core/src/chat/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,13 @@ describe("Custom context reporting", () => {
fromUI({ type: "platform", cli: "custom" });
fromUI({ type: "slashCommand", command: "context" });
await flush();
expect(transport.send).toHaveBeenCalledWith({ type: "notice", text: "Context: usage not reported yet. Provider context limit unknown." });
expect(transport.send).toHaveBeenCalledWith({ type: "notice", text: "Context (custom): usage not reported yet. Model context limit unknown." });
fromUI({ type: "send", text: "fixture" });
await flush();
handles[0].emitUsage(12000, 256000);
fromUI({ type: "slashCommand", command: "context" });
await flush();
expect(transport.send).toHaveBeenCalledWith({ type: "notice", text: "Context (custom): 12,000 tokens used. Provider context limit unknown." });
expect(transport.send).toHaveBeenCalledWith({ type: "notice", text: "Context (custom): 12,000 tokens last reported. Model context limit unknown." });
} finally { chat.stop(); }
});
});
Expand Down Expand Up @@ -772,3 +772,84 @@ it("adopts merged scrollback even when the newest 30 messages are identical loca
expect(older?.messages.map((m: any) => m.text)).toEqual(["기기 B 0", "기기 B 1", "기기 B 2"]);
expect(older?.hasMore).toBe(false);
});

describe("chat model settings", () => {
const catalog = [
{ value: "opus", chipLabel: "Opus", label: "Opus", description: "", supportedEfforts: ["high", "ultra"] },
{ value: "haiku", chipLabel: "Haiku", label: "Haiku", description: "", supportedEfforts: [] },
];
it("acknowledges actual settings and removes unsupported effort on a model switch", async () => {
const { fromUI, transport, handles } = harness({ env: { modelOptions: async () => catalog } });
fromUI({ type: "effort", effort: "ultra" });
await flush();
fromUI({ type: "model", model: "haiku" });
fromUI({ type: "getSettings", requestId: "restore-1" });
fromUI({ type: "send", text: "test" });
await flush();
expect(transport.send).toHaveBeenCalledWith(expect.objectContaining({ type: "settings", requestId: "restore-1", cli: "claude", model: "haiku", effort: undefined }));
expect((handles[0] as any).opts.effort).toBeUndefined();
});
it("rejects a typed effort for an unlisted model rather than using another model's capabilities", async () => {
const { fromUI, transport, handles } = harness({ env: { modelOptions: async () => catalog } });
fromUI({ type: "model", model: "unlisted" });
fromUI({ type: "effort", effort: "high" });
fromUI({ type: "send", text: "test" });
await flush();
expect(transport.send).toHaveBeenCalledWith(expect.objectContaining({ type: "toast" }));
expect((handles[0] as any).opts.effort).toBeUndefined();
});
it("coalesces simultaneous probes and retries after a failed probe", async () => {
let complete!: (value: null) => void;
const query = vi.fn().mockImplementationOnce(() => new Promise(resolve => { complete = resolve; })).mockResolvedValueOnce(catalog);
const { fromUI, transport } = harness({ env: { modelOptions: query } });
fromUI({ type: "getModelOptions" });
fromUI({ type: "getModelOptions" });
await flush();
expect(query).toHaveBeenCalledTimes(1);
complete(null);
await flush();
fromUI({ type: "getModelOptions" });
await flush();
expect(query).toHaveBeenCalledTimes(2);
expect(transport.send).toHaveBeenCalledWith(expect.objectContaining({ type: "modelOptions", options: catalog }));
});
it("drops a previous context denominator when the engine reports none", async () => {
const { fromUI, handles, transport } = harness();
fromUI({ type: "send", text: "test" });
await flush();
handles[0].emitUsage(10, 100);
handles[0].emitUsage(20);
expect(transport.send).toHaveBeenLastCalledWith({ type: "usage", contextTokens: 20, contextWindow: undefined });
});
});

describe("model-change usage boundaries", () => {
it("does not report an old in-flight model's window under the next model", async () => {
const { fromUI, handles, transport } = harness();
fromUI({ type: "send", text: "first" });
await flush();
handles[0].emitUsage(10, 100);
fromUI({ type: "model", model: "next-model" });
await flush();
transport.send.mockClear();
handles[0].emitUsage(20, 100);
expect(transport.send).not.toHaveBeenCalled();
expect(handles[0].stop).not.toHaveBeenCalled();
});
});

it("carries a newly-created engine session when switching before any explicit open", async () => {
const loadSessionLocal = vi.fn(async () => ({ messages: [{ role: "assistant", text: "kept history", ts: 1 }], hasMore: false, cursor: 0 }));
const { fromUI, handles, transport, startSession } = harness({ rt: { loadSessionLocal } });
fromUI({ type: "send", text: "first" });
await flush();
handles[0].emitTurnEnd();
await flush();
fromUI({ type: "platform", cli: "codex" });
await flush();
expect(loadSessionLocal).toHaveBeenCalledWith("sess-0");
expect(transport.send).toHaveBeenCalledWith(expect.objectContaining({ type: "message", msg: expect.objectContaining({ text: "kept history" }) }));
fromUI({ type: "send", text: "next" });
await flush();
expect(startSession).toHaveBeenLastCalledWith(expect.objectContaining({ cli: "codex", sessionId: "sess-0" }));
});
Loading
Loading