Add ACP support with Cursor provider (#1355)

Co-authored-by: codex <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-04-17 16:21:31 -07:00
1 parent 0f184c28b0
commit 9c64f12ea0
98 files changed
+26746 -862

No files matched your search

+6 -41
View File
@@ -11,10 +11,8 @@ import {
normalizeClaudeModelOptionsWithCapabilities,
normalizeCodexModelOptionsWithCapabilities,
normalizeModelSlug,
resolveApiModelId,
resolveContextWindow,
resolveEffort,
resolveModelSlug,
resolveModelSlugForProvider,
resolveSelectableModel,
trimOrNull,
@@ -61,17 +59,18 @@ describe("normalizeModelSlug", () => {
});
});
describe("resolveModelSlug", () => {
describe("resolveModelSlugForProvider", () => {
it("returns defaults when the model is missing", () => {
expect(resolveModelSlug(undefined, "codex")).toBe(DEFAULT_MODEL_BY_PROVIDER.codex);
expect(resolveModelSlugForProvider("codex", undefined)).toBe(DEFAULT_MODEL_BY_PROVIDER.codex);
expect(resolveModelSlugForProvider("claudeAgent", undefined)).toBe(
DEFAULT_MODEL_BY_PROVIDER.claudeAgent,
);
});
it("preserves normalized unknown models", () => {
expect(resolveModelSlug("custom/internal-model", "codex")).toBe("custom/internal-model");
expect(resolveModelSlugForProvider("codex", "custom/internal-model")).toBe(
"custom/internal-model",
);
});
});
@@ -137,6 +136,7 @@ describe("resolveEffort", () => {
describe("misc helpers", () => {
it("detects ultrathink prompts", () => {
expect(isClaudeUltrathinkPrompt("Please ultrathink about this")).toBe(true);
expect(isClaudeUltrathinkPrompt("Ultrathink:\nInvestigate")).toBe(true);
expect(isClaudeUltrathinkPrompt("Investigate")).toBe(false);
});
@@ -195,41 +195,6 @@ describe("resolveContextWindow", () => {
});
});
describe("resolveApiModelId", () => {
it("appends [1m] suffix for 1m context window", () => {
expect(
resolveApiModelId({
provider: "claudeAgent",
model: "claude-opus-4-6",
options: { contextWindow: "1m" },
}),
).toBe("claude-opus-4-6[1m]");
});
it("returns the model as-is for 200k context window", () => {
expect(
resolveApiModelId({
provider: "claudeAgent",
model: "claude-opus-4-6",
options: { contextWindow: "200k" },
}),
).toBe("claude-opus-4-6");
});
it("returns the model as-is when no context window is set", () => {
expect(resolveApiModelId({ provider: "claudeAgent", model: "claude-opus-4-6" })).toBe(
"claude-opus-4-6",
);
expect(
resolveApiModelId({ provider: "claudeAgent", model: "claude-opus-4-6", options: {} }),
).toBe("claude-opus-4-6");
});
it("returns the model as-is for Codex selections", () => {
expect(resolveApiModelId({ provider: "codex", model: "gpt-5.4" })).toBe("gpt-5.4");
});
});
describe("normalize*ModelOptionsWithCapabilities", () => {
it("preserves explicit false codex fast mode", () => {
expect(
+32 -46
View File
@@ -4,6 +4,7 @@ import {
type ClaudeAgentEffort,
type ClaudeModelOptions,
type CodexModelOptions,
type CursorModelOptions,
type ModelCapabilities,
type ModelSelection,
type OpenCodeModelOptions,
@@ -16,8 +17,6 @@ export interface SelectableModelOption {
name: string;
}
// ── Effort helpers ────────────────────────────────────────────────────
/** Check whether a capabilities object includes a given effort value. */
export function hasEffortLevel(caps: ModelCapabilities, value: string): boolean {
return caps.reasoningEffortLevels.some((l) => l.value === value);
@@ -31,12 +30,9 @@ export function getDefaultEffort(caps: ModelCapabilities): string | null {
/**
* Resolve a raw effort option against capabilities.
*
* Returns the effective effort value — the explicit value if supported and not
* prompt-injected, otherwise the model's default. Returns `undefined` only
* when the model has no effort levels at all.
*
* Prompt-injected efforts (e.g. "ultrathink") are excluded because they are
* applied via prompt text, not the effort API parameter.
* Returns the explicit supported value when present and not prompt-injected,
* otherwise the model default. Returns `undefined` when the model exposes no
* effort levels.
*/
export function resolveEffort(
caps: ModelCapabilities,
@@ -54,8 +50,6 @@ export function resolveEffort(
return defaultValue ?? undefined;
}
// ── Context window helpers ───────────────────────────────────────────
/** Check whether a capabilities object includes a given context window value. */
export function hasContextWindowOption(caps: ModelCapabilities, value: string): boolean {
return caps.contextWindowOptions.some((o) => o.value === value);
@@ -69,14 +63,8 @@ export function getDefaultContextWindow(caps: ModelCapabilities): string | null
/**
* Resolve a raw `contextWindow` option against capabilities.
*
* Returns the effective context window value — the explicit value if supported,
* otherwise the model's default. Returns `undefined` only when the model has
* no context window options at all.
*
* Unlike effort levels (where the API has matching defaults), the context
* window requires an explicit API suffix (e.g. `[1m]`), so we always preserve
* the resolved value to avoid ambiguity between "user chose the default" and
* "not specified".
* Returns the explicit supported value when present, otherwise the model
* default. Returns `undefined` when the model exposes no context window options.
*/
export function resolveContextWindow(
caps: ModelCapabilities,
@@ -119,6 +107,23 @@ export function normalizeClaudeModelOptionsWithCapabilities(
return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
}
export function normalizeCursorModelOptionsWithCapabilities(
caps: ModelCapabilities,
modelOptions: CursorModelOptions | null | undefined,
): CursorModelOptions | undefined {
const reasoning = resolveEffort(caps, modelOptions?.reasoning);
const thinking = caps.supportsThinkingToggle ? modelOptions?.thinking : undefined;
const fastMode = caps.supportsFastMode ? modelOptions?.fastMode : undefined;
const contextWindow = resolveContextWindow(caps, modelOptions?.contextWindow);
const nextOptions: CursorModelOptions = {
...(reasoning ? { reasoning: reasoning as CursorModelOptions["reasoning"] } : {}),
...(fastMode !== undefined ? { fastMode } : {}),
...(thinking !== undefined ? { thinking } : {}),
...(contextWindow !== undefined ? { contextWindow } : {}),
};
return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
}
function resolveLabeledOption(
options: ReadonlyArray<{ value: string; isDefault?: boolean | undefined }> | undefined,
raw: string | null | undefined,
@@ -155,6 +160,8 @@ export function normalizeProviderModelOptionsWithCapabilities(
return normalizeCodexModelOptionsWithCapabilities(caps, modelOptions as CodexModelOptions);
case "claudeAgent":
return normalizeClaudeModelOptionsWithCapabilities(caps, modelOptions as ClaudeModelOptions);
case "cursor":
return normalizeCursorModelOptionsWithCapabilities(caps, modelOptions as CursorModelOptions);
case "opencode":
return normalizeOpenCodeModelOptionsWithCapabilities(
caps,
@@ -220,7 +227,7 @@ export function resolveSelectableModel(
return resolved ? resolved.slug : null;
}
export function resolveModelSlug(model: string | null | undefined, provider: ProviderKind): string {
function resolveModelSlug(model: string | null | undefined, provider: ProviderKind): string {
const normalized = normalizeModelSlug(model, provider);
if (!normalized) {
return DEFAULT_MODEL_BY_PROVIDER[provider];
@@ -260,6 +267,12 @@ export function createModelSelection(
model,
...(options ? { options: options as ClaudeModelOptions } : {}),
};
case "cursor":
return {
provider,
model,
...(options ? { options: options as CursorModelOptions } : {}),
};
case "opencode":
return {
provider,
@@ -269,33 +282,6 @@ export function createModelSelection(
}
}
/**
* Resolve the actual API model identifier from a model selection.
*
* Provider-aware: each provider can map `contextWindow` (or other options)
* to whatever the API requires — a model-id suffix, a separate parameter, etc.
* The canonical slug stored in the selection stays unchanged so the
* capabilities system keeps working.
*
* Expects `contextWindow` to already be resolved (via `resolveContextWindow`)
* to the effective value, not stripped to `undefined` for defaults.
*/
export function resolveApiModelId(modelSelection: ModelSelection): string {
switch (modelSelection.provider) {
case "claudeAgent": {
switch (modelSelection.options?.contextWindow) {
case "1m":
return `${modelSelection.model}[1m]`;
default:
return modelSelection.model;
}
}
default: {
return modelSelection.model;
}
}
}
export function applyClaudePromptEffortPrefix(
text: string,
effort: ClaudeAgentEffort | null | undefined,
+16 -7
View File
@@ -2,6 +2,7 @@ import {
ServerSettings,
type ClaudeModelOptions,
type CodexModelOptions,
type CursorModelOptions,
type OpenCodeModelOptions,
type ServerSettingsPatch,
} from "@t3tools/contracts";
@@ -90,12 +91,20 @@ export function applyServerSettingsPatch(
selectionPatch.options as ClaudeModelOptions | undefined,
),
}
: {
provider,
model,
...withModelSelectionOptions(
selectionPatch.options as OpenCodeModelOptions | undefined,
),
},
: provider === "cursor"
? {
provider,
model,
...withModelSelectionOptions(
selectionPatch.options as CursorModelOptions | undefined,
),
}
: {
provider,
model,
...withModelSelectionOptions(
selectionPatch.options as OpenCodeModelOptions | undefined,
),
},
};
}
+57
View File
@@ -0,0 +1,57 @@
import { describe, expect, it } from "vitest";
import { deriveToolActivityPresentation } from "./toolActivity.ts";
describe("toolActivity", () => {
it("normalizes command tools to a stable ran-command label", () => {
expect(
deriveToolActivityPresentation({
itemType: "command_execution",
title: "Terminal",
detail: "Terminal",
data: {
command: "bun run lint",
},
fallbackSummary: "Terminal",
}),
).toEqual({
summary: "Ran command",
detail: "bun run lint",
});
});
it("uses structured file paths for read-file tools when available", () => {
expect(
deriveToolActivityPresentation({
itemType: "dynamic_tool_call",
title: "Read File",
detail: "Read File",
data: {
kind: "read",
locations: [{ path: "/tmp/app.ts" }],
},
fallbackSummary: "Read File",
}),
).toEqual({
summary: "Read file",
detail: "/tmp/app.ts",
});
});
it("drops duplicated generic read-file detail when no path is available", () => {
expect(
deriveToolActivityPresentation({
itemType: "dynamic_tool_call",
title: "Read File",
detail: "Read File",
data: {
kind: "read",
rawInput: {},
},
fallbackSummary: "Read File",
}),
).toEqual({
summary: "Read file",
});
});
});
+257
View File
@@ -0,0 +1,257 @@
import type { ToolLifecycleItemType } from "@t3tools/contracts";
function asRecord(value: unknown): Record<string, unknown> | undefined {
return value !== null && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: undefined;
}
function asTrimmedString(value: unknown): string | undefined {
if (typeof value !== "string") {
return undefined;
}
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
function normalizeCommandValue(value: unknown): string | undefined {
const direct = asTrimmedString(value);
if (direct) {
return direct;
}
if (!Array.isArray(value)) {
return undefined;
}
const parts = value
.map((entry) => asTrimmedString(entry))
.filter((entry): entry is string => entry !== undefined);
return parts.length > 0 ? parts.join(" ") : undefined;
}
function stripTrailingExitCode(value: string | undefined): string | undefined {
const trimmed = value?.trim();
if (!trimmed) {
return undefined;
}
const match = /^(?<output>[\s\S]*?)(?:\s*<exited with exit code \d+>)\s*$/iu.exec(trimmed);
const output = match?.groups?.output?.trim() ?? trimmed;
return output.length > 0 ? output : undefined;
}
function extractCommandFromTitle(title: string | undefined): string | undefined {
if (!title) {
return undefined;
}
const backtickMatch = /`([^`]+)`/u.exec(title);
return backtickMatch?.[1]?.trim() || undefined;
}
function extractToolCommand(data: Record<string, unknown> | undefined, title: string | undefined) {
const item = asRecord(data?.item);
const itemInput = asRecord(item?.input);
const itemResult = asRecord(item?.result);
const rawInput = asRecord(data?.rawInput);
const candidates = [
normalizeCommandValue(item?.command),
normalizeCommandValue(itemInput?.command),
normalizeCommandValue(itemResult?.command),
normalizeCommandValue(data?.command),
normalizeCommandValue(rawInput?.command),
];
const direct = candidates.find((candidate) => candidate !== undefined);
if (direct) {
return direct;
}
const executable = asTrimmedString(rawInput?.executable);
const args = normalizeCommandValue(rawInput?.args);
if (executable && args) {
return `${executable} ${args}`;
}
if (executable) {
return executable;
}
return extractCommandFromTitle(title);
}
function maybePathLike(value: string | undefined): string | undefined {
if (!value) {
return undefined;
}
if (
value.includes("/") ||
value.includes("\\") ||
value.startsWith(".") ||
/\.(?:[a-z0-9]{1,12})$/iu.test(value)
) {
return value;
}
return undefined;
}
function collectPaths(value: unknown, paths: string[], seen: Set<string>, depth: number): void {
if (depth > 4 || paths.length >= 8) {
return;
}
if (Array.isArray(value)) {
for (const entry of value) {
collectPaths(entry, paths, seen, depth + 1);
if (paths.length >= 8) {
return;
}
}
return;
}
const record = asRecord(value);
if (!record) {
return;
}
for (const key of ["path", "filePath", "relativePath", "filename", "newPath", "oldPath"]) {
const candidate = maybePathLike(asTrimmedString(record[key]));
if (!candidate || seen.has(candidate)) {
continue;
}
seen.add(candidate);
paths.push(candidate);
if (paths.length >= 8) {
return;
}
}
for (const nestedKey of ["locations", "item", "input", "result", "rawInput", "data", "changes"]) {
if (!(nestedKey in record)) {
continue;
}
collectPaths(record[nestedKey], paths, seen, depth + 1);
if (paths.length >= 8) {
return;
}
}
}
function extractPrimaryPath(data: Record<string, unknown> | undefined): string | undefined {
const paths: string[] = [];
collectPaths(data, paths, new Set<string>(), 0);
return paths[0];
}
function normalizeEquivalentValue(value: string | undefined): string | undefined {
const trimmed = asTrimmedString(value);
if (!trimmed) {
return undefined;
}
return trimmed
.replace(/\s+/gu, " ")
.replace(/\s+(?:complete|completed|started)\s*$/iu, "")
.trim();
}
function isEquivalent(left: string | undefined, right: string | undefined): boolean {
const normalizedLeft = normalizeEquivalentValue(left)?.toLowerCase();
const normalizedRight = normalizeEquivalentValue(right)?.toLowerCase();
return normalizedLeft !== undefined && normalizedLeft === normalizedRight;
}
function classifyToolAction(input: {
readonly itemType?: ToolLifecycleItemType | null | undefined;
readonly title?: string | undefined;
readonly data?: Record<string, unknown> | undefined;
}): "command" | "read" | "file_change" | "search" | "other" {
const itemType = input.itemType ?? undefined;
const kind = asTrimmedString(input.data?.kind)?.toLowerCase();
const title = asTrimmedString(input.title)?.toLowerCase();
if (itemType === "command_execution" || kind === "execute" || title === "terminal") {
return "command";
}
if (kind === "read" || title === "read file") {
return "read";
}
if (
itemType === "file_change" ||
kind === "edit" ||
kind === "move" ||
kind === "delete" ||
kind === "write"
) {
return "file_change";
}
if (itemType === "web_search" || kind === "search" || title === "find" || title === "grep") {
return "search";
}
return "other";
}
export interface ToolActivityPresentationInput {
readonly itemType?: ToolLifecycleItemType | null | undefined;
readonly title?: string | null | undefined;
readonly detail?: string | null | undefined;
readonly data?: unknown;
readonly fallbackSummary?: string | null | undefined;
}
export interface ToolActivityPresentation {
readonly summary: string;
readonly detail?: string | undefined;
}
export function deriveToolActivityPresentation(
input: ToolActivityPresentationInput,
): ToolActivityPresentation {
const title = asTrimmedString(input.title);
const detail = stripTrailingExitCode(asTrimmedString(input.detail));
const fallbackSummary = asTrimmedString(input.fallbackSummary) ?? "Tool";
const data = asRecord(input.data);
const command = extractToolCommand(data, title);
const primaryPath = extractPrimaryPath(data);
const action = classifyToolAction({
itemType: input.itemType,
title,
data,
});
if (action === "command") {
return {
summary: "Ran command",
...(command ? { detail: command } : {}),
};
}
if (action === "read") {
if (primaryPath) {
return {
summary: "Read file",
detail: primaryPath,
};
}
return {
summary: "Read file",
};
}
if (action === "file_change") {
return {
summary: "Changed files",
...(primaryPath ? { detail: primaryPath } : {}),
};
}
if (action === "search") {
const query =
asTrimmedString(asRecord(data?.rawInput)?.query) ??
asTrimmedString(asRecord(data?.rawInput)?.pattern) ??
asTrimmedString(asRecord(data?.rawInput)?.searchTerm);
return {
summary: "Searched files",
...(query ? { detail: query } : {}),
};
}
if (detail && !isEquivalent(detail, title) && !isEquivalent(detail, fallbackSummary)) {
return {
summary: title ?? fallbackSummary,
detail,
};
}
return {
summary: title ?? fallbackSummary,
};
}