742 lines
25 KiB
TypeScript
742 lines
25 KiB
TypeScript
import {
|
|
type ClaudeSettings,
|
|
type ModelCapabilities,
|
|
type ServerProvider,
|
|
type ServerProviderSlashCommand,
|
|
type ServerProviderResetCredits,
|
|
} from "@t3tools/contracts";
|
|
import * as DateTime from "effect/DateTime";
|
|
import * as Effect from "effect/Effect";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Option from "effect/Option";
|
|
import * as Path from "effect/Path";
|
|
import * as Ref from "effect/Ref";
|
|
import * as Result from "effect/Result";
|
|
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
import { createModelCapabilities } from "@t3tools/shared/model";
|
|
import { resolveSpawnCommand } from "@t3tools/shared/shell";
|
|
import {
|
|
query as claudeQuery,
|
|
type Options as ClaudeQueryOptions,
|
|
type SlashCommand as ClaudeSlashCommand,
|
|
type SDKControlGetUsageResponse,
|
|
type SDKUserMessage,
|
|
type SettingSource,
|
|
} from "@anthropic-ai/claude-agent-sdk";
|
|
|
|
import {
|
|
buildServerProvider,
|
|
COMPACT_SLASH_COMMAND,
|
|
DEFAULT_TIMEOUT_MS,
|
|
isCommandMissingCause,
|
|
parseGenericCliVersion,
|
|
providerModelsFromSettings,
|
|
spawnAndCollect,
|
|
type ServerProviderDraft,
|
|
} from "@t3tools/provider-core/server/snapshotProbe";
|
|
import { resolveClaudeSdkExecutablePath } from "./Drivers/ClaudeExecutable.ts";
|
|
import { makeClaudeEnvironment } from "./Drivers/ClaudeHome.ts";
|
|
import { discoverClaudeSkills } from "./Drivers/ClaudeSkills.ts";
|
|
import type { ProviderWorkspaceSnapshot } from "@t3tools/provider-core/server/driver";
|
|
import { makeUnavailableUsageLimits } from "@t3tools/provider-core/server/usageLimits";
|
|
import {
|
|
type ClaudeScopedLimitNames,
|
|
claudeUsageResponseToLimits,
|
|
recordClaudeUsageResponse,
|
|
} from "./claudeUsageLimits.ts";
|
|
import {
|
|
BUNDLED_CLAUDE_MODEL_CATALOG,
|
|
type ClaudeModelCatalog,
|
|
formatClaudeVersionUpgradeMessage,
|
|
resolveClaudeModelsForVersion,
|
|
resolveClaudeUpdateRequiredModels,
|
|
} from "./ClaudeModelCatalog.ts";
|
|
|
|
const DEFAULT_CLAUDE_MODEL_CAPABILITIES: ModelCapabilities = createModelCapabilities({
|
|
optionDescriptors: [],
|
|
});
|
|
|
|
const CLAUDE_PRESENTATION = {
|
|
displayName: "Claude",
|
|
showInteractionModeToggle: true,
|
|
reportsContextWindow: true,
|
|
} as const;
|
|
function toTitleCaseWords(value: string): string {
|
|
const parts: Array<string> = [];
|
|
for (const part of value.split(/[\s_-]+/g)) {
|
|
if (part.length > 0) {
|
|
parts.push(part[0]!.toUpperCase() + part.slice(1).toLowerCase());
|
|
}
|
|
}
|
|
return parts.join(" ");
|
|
}
|
|
|
|
function claudeSubscriptionLabel(subscriptionType: string | undefined): string | undefined {
|
|
const normalized = subscriptionType?.toLowerCase().replace(/[\s_-]+/g, "");
|
|
if (!normalized) return undefined;
|
|
|
|
switch (normalized) {
|
|
case "claudemaxsubscription":
|
|
return "Max";
|
|
case "claudemax5xsubscription":
|
|
return "Max 5x";
|
|
case "claudemax20xsubscription":
|
|
return "Max 20x";
|
|
case "claudeenterprisesubscription":
|
|
return "Enterprise";
|
|
case "claudeteamsubscription":
|
|
return "Team";
|
|
case "claudeprosubscription":
|
|
return "Pro";
|
|
case "claudefreesubscription":
|
|
return "Free";
|
|
case "max":
|
|
case "maxplan":
|
|
return "Max";
|
|
case "max5":
|
|
return "Max 5x";
|
|
case "max20":
|
|
return "Max 20x";
|
|
case "enterprise":
|
|
return "Enterprise";
|
|
case "team":
|
|
return "Team";
|
|
case "pro":
|
|
return "Pro";
|
|
case "free":
|
|
return "Free";
|
|
default:
|
|
return toTitleCaseWords(subscriptionType!);
|
|
}
|
|
}
|
|
|
|
function normalizeClaudeAuthMethod(authMethod: string | undefined): string | undefined {
|
|
const normalized = authMethod?.toLowerCase().replace(/[\s_-]+/g, "");
|
|
if (!normalized) return undefined;
|
|
if (
|
|
normalized === "apikey" ||
|
|
normalized === "anthropicapikey" ||
|
|
normalized === "anthropicauthtoken"
|
|
) {
|
|
return "apiKey";
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function formatClaudeSubscriptionAuthLabel(subscriptionType: string): string {
|
|
const subscriptionLabel =
|
|
claudeSubscriptionLabel(subscriptionType) ?? toTitleCaseWords(subscriptionType);
|
|
const normalized = subscriptionLabel.toLowerCase().replace(/[\s_-]+/g, "");
|
|
|
|
if (normalized.startsWith("claude") && normalized.endsWith("subscription")) {
|
|
return subscriptionLabel;
|
|
}
|
|
if (normalized.startsWith("claude")) {
|
|
return `${subscriptionLabel} Subscription`;
|
|
}
|
|
if (normalized.endsWith("subscription")) {
|
|
return `Claude ${subscriptionLabel}`;
|
|
}
|
|
return `Claude ${subscriptionLabel} Subscription`;
|
|
}
|
|
|
|
function claudeAuthMetadata(input: {
|
|
readonly subscriptionType: string | undefined;
|
|
readonly authMethod: string | undefined;
|
|
}): { readonly type: string; readonly label: string } | undefined {
|
|
if (normalizeClaudeAuthMethod(input.authMethod) === "apiKey") {
|
|
return {
|
|
type: "apiKey",
|
|
label: "Claude API Key",
|
|
};
|
|
}
|
|
|
|
if (input.subscriptionType) {
|
|
return {
|
|
type: input.subscriptionType,
|
|
label: formatClaudeSubscriptionAuthLabel(input.subscriptionType),
|
|
};
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
function apiProviderAuthMetadata(
|
|
apiProvider: string | undefined,
|
|
): { readonly type: string; readonly label: string } | undefined {
|
|
return apiProvider === "bedrock" ? { type: "bedrock", label: "Amazon Bedrock" } : undefined;
|
|
}
|
|
|
|
/**
|
|
* Whether the SDK's account payload evidences a credential the CLI can use.
|
|
*
|
|
* The capability probe resolves for a logged-out CLI, so a completed probe only
|
|
* proves Claude Code started. `tokenSource: "none"` is the CLI reporting it
|
|
* found no token at all, and is the one shape that disproves authentication.
|
|
* Everything else either names a credential or, on a third-party backend, omits
|
|
* these fields by design because auth lives with AWS or gcloud instead.
|
|
*
|
|
* Silence is deliberately not disproof. Profile-authenticated installs report no
|
|
* token source, and a CLI too old to send an account payload reports nothing at
|
|
* all; treating either as logged out would sign working setups out of Settings.
|
|
* `apiKeySource: "none"` means no API key is in use, so it is no evidence either.
|
|
*/
|
|
function claudeAuthStatus(
|
|
capabilities: Pick<
|
|
ClaudeCapabilitiesProbe,
|
|
"email" | "subscriptionType" | "tokenSource" | "apiKeySource" | "apiProvider"
|
|
>,
|
|
): "authenticated" | "unauthenticated" {
|
|
if (capabilities.apiProvider !== undefined && capabilities.apiProvider !== "firstParty") {
|
|
return "authenticated";
|
|
}
|
|
if (capabilities.tokenSource !== "none") return "authenticated";
|
|
// An `ANTHROPIC_API_KEY` install reports no token source but is authenticated
|
|
// all the same, so the key and account fields still get a say.
|
|
const hasApiKey = Boolean(capabilities.apiKeySource) && capabilities.apiKeySource !== "none";
|
|
return hasApiKey || capabilities.email || capabilities.subscriptionType
|
|
? "authenticated"
|
|
: "unauthenticated";
|
|
}
|
|
|
|
// ── SDK capability probe ────────────────────────────────────────────
|
|
|
|
// Amazon Bedrock initializes far slower than first-party auth: the SDK boots the
|
|
// Bedrock backend and runs the `awsAuthRefresh` credential hook before returning
|
|
// account info. The previous 8s budget expired mid-init, so the probe returned
|
|
// `undefined` and left the provider unverified and unselectable in the picker.
|
|
const CAPABILITIES_PROBE_TIMEOUT_MS = 25_000;
|
|
|
|
/**
|
|
* Keep workspace-scoped command discovery intact while isolating the periodic
|
|
* health check from configured MCP servers.
|
|
*/
|
|
export const CLAUDE_CAPABILITIES_PROBE_SETTING_SOURCES = [
|
|
"user",
|
|
"project",
|
|
"local",
|
|
] as const satisfies ReadonlyArray<SettingSource>;
|
|
|
|
/** Build the exact SDK options used by the periodic Claude capability probe. */
|
|
export function buildClaudeCapabilitiesProbeQueryOptions(input: {
|
|
readonly executablePath: string;
|
|
readonly abortController: AbortController;
|
|
readonly environment: NodeJS.ProcessEnv;
|
|
readonly cwd: string | undefined;
|
|
}): ClaudeQueryOptions {
|
|
return {
|
|
persistSession: false,
|
|
pathToClaudeCodeExecutable: input.executablePath,
|
|
abortController: input.abortController,
|
|
settingSources: [...CLAUDE_CAPABILITIES_PROBE_SETTING_SOURCES],
|
|
// The probe keeps filesystem setting sources for slash-command discovery,
|
|
// but must not run the user's hooks: it fires every few minutes, so
|
|
// SessionStart hooks would run on every health check.
|
|
settings: { disableAllHooks: true },
|
|
allowedTools: [],
|
|
// Ignore MCP definitions from every filesystem setting source above. The
|
|
// SDK combines this empty explicit map with --strict-mcp-config.
|
|
mcpServers: {},
|
|
strictMcpConfig: true,
|
|
env: {
|
|
...input.environment,
|
|
// Connected claude.ai MCP servers are discovered outside filesystem
|
|
// config; disable them independently for this health check.
|
|
ENABLE_CLAUDEAI_MCP_SERVERS: "false",
|
|
// This is a noninteractive health check, so IDE discovery cannot add any
|
|
// useful capability data. Skipping it also avoids Claude spawning a
|
|
// Windows `tasklist | findstr` process tree on every periodic refresh.
|
|
FORCE_CODE_TERMINAL: undefined,
|
|
CLAUDE_CODE_AUTO_CONNECT_IDE: "0",
|
|
CLAUDE_CODE_IDE_SKIP_AUTO_INSTALL: "1",
|
|
},
|
|
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
stderr: () => {},
|
|
};
|
|
}
|
|
|
|
function nonEmptyProbeString(value: string): string | undefined {
|
|
const candidate = value.trim();
|
|
return candidate ? candidate : undefined;
|
|
}
|
|
|
|
type ClaudeCapabilitiesProbe = {
|
|
readonly email: string | undefined;
|
|
/**
|
|
* The organization UUID from the login's `.claude.json`. The driver reads it
|
|
* in the same cached probe, so it describes the same login as the rest.
|
|
*/
|
|
readonly workspaceId?: string | undefined;
|
|
readonly subscriptionType: string | undefined;
|
|
readonly tokenSource: string | undefined;
|
|
/** Where the CLI found an API key, when it authenticates with one. */
|
|
readonly apiKeySource: string | undefined;
|
|
/**
|
|
* Active API backend reported by the SDK's `AccountInfo`. Anthropic OAuth
|
|
* login only applies when `"firstParty"`; for Amazon Bedrock (`"bedrock"`)
|
|
* the subscription/token fields are absent and auth is external AWS creds.
|
|
*/
|
|
readonly apiProvider: string | undefined;
|
|
readonly slashCommands: ReadonlyArray<ServerProviderSlashCommand>;
|
|
/**
|
|
* Subscription windows from the SDK's `get_usage` control request, or
|
|
* `undefined` when the request itself failed. Absent windows on an
|
|
* otherwise successful response mean the account has none (API key).
|
|
*/
|
|
readonly usage?: Pick<SDKControlGetUsageResponse, "rate_limits_available" | "rate_limits">;
|
|
};
|
|
|
|
function parseClaudeInitializationCommands(
|
|
commands: ReadonlyArray<ClaudeSlashCommand> | undefined,
|
|
): ReadonlyArray<ServerProviderSlashCommand> {
|
|
return dedupeSlashCommands(
|
|
(commands ?? []).flatMap((command) => {
|
|
const name = nonEmptyProbeString(command.name);
|
|
if (!name) {
|
|
return [];
|
|
}
|
|
|
|
const description = nonEmptyProbeString(command.description);
|
|
const argumentHint = nonEmptyProbeString(command.argumentHint);
|
|
|
|
return [
|
|
{
|
|
name,
|
|
...(description ? { description } : {}),
|
|
...(argumentHint ? { input: { hint: argumentHint } } : {}),
|
|
} satisfies ServerProviderSlashCommand,
|
|
];
|
|
}),
|
|
);
|
|
}
|
|
|
|
function dedupeSlashCommands(
|
|
commands: ReadonlyArray<ServerProviderSlashCommand>,
|
|
): ReadonlyArray<ServerProviderSlashCommand> {
|
|
const commandsByName = new Map<string, ServerProviderSlashCommand>();
|
|
|
|
for (const command of commands) {
|
|
const name = nonEmptyProbeString(command.name);
|
|
if (!name) {
|
|
continue;
|
|
}
|
|
|
|
const key = name.toLowerCase();
|
|
const existing = commandsByName.get(key);
|
|
if (!existing) {
|
|
commandsByName.set(key, {
|
|
...command,
|
|
name,
|
|
});
|
|
continue;
|
|
}
|
|
|
|
commandsByName.set(key, {
|
|
...existing,
|
|
...(existing.description
|
|
? {}
|
|
: command.description
|
|
? { description: command.description }
|
|
: {}),
|
|
...(existing.input?.hint
|
|
? {}
|
|
: command.input?.hint
|
|
? { input: { hint: command.input.hint } }
|
|
: {}),
|
|
});
|
|
}
|
|
|
|
return [...commandsByName.values()];
|
|
}
|
|
|
|
function waitForAbortSignal(signal: AbortSignal): Promise<void> {
|
|
if (signal.aborted) {
|
|
return Promise.resolve();
|
|
}
|
|
return new Promise((resolve) => {
|
|
signal.addEventListener("abort", () => resolve(), { once: true });
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Probe account information by spawning a lightweight Claude Agent SDK
|
|
* session and reading the initialization result.
|
|
*
|
|
* We pass a never-yielding AsyncIterable as the prompt so that no user
|
|
* message is ever written to the subprocess stdin. This means the Claude
|
|
* Code subprocess completes its local initialization IPC (returning
|
|
* account info and slash commands) but never starts an API request to
|
|
* Anthropic. We read the init data and then abort the subprocess.
|
|
*
|
|
* This is used as a fallback when `claude auth status` does not include
|
|
* subscription type information.
|
|
*/
|
|
const probeClaudeCapabilities = (
|
|
claudeSettings: ClaudeSettings,
|
|
environment?: NodeJS.ProcessEnv,
|
|
cwd?: string,
|
|
includeUsage = true,
|
|
) => {
|
|
const abort = new AbortController();
|
|
return Effect.gen(function* () {
|
|
const claudeEnvironment = yield* makeClaudeEnvironment(claudeSettings, environment);
|
|
const executablePath = yield* resolveClaudeSdkExecutablePath(
|
|
claudeSettings.binaryPath,
|
|
claudeEnvironment,
|
|
);
|
|
return yield* Effect.tryPromise(async () => {
|
|
const q = claudeQuery({
|
|
// Never yield — we only need initialization data, not a conversation.
|
|
// This prevents any prompt from reaching the Anthropic API.
|
|
// oxlint-disable-next-line require-yield
|
|
prompt: (async function* (): AsyncGenerator<SDKUserMessage> {
|
|
await waitForAbortSignal(abort.signal);
|
|
})(),
|
|
options: buildClaudeCapabilitiesProbeQueryOptions({
|
|
executablePath,
|
|
abortController: abort,
|
|
environment: claudeEnvironment,
|
|
cwd,
|
|
}),
|
|
});
|
|
const init = await q.initializationResult();
|
|
return { q, init };
|
|
});
|
|
}).pipe(
|
|
Effect.timeout(CAPABILITIES_PROBE_TIMEOUT_MS),
|
|
Effect.flatMap(({ q, init }) =>
|
|
Effect.gen(function* () {
|
|
// Usage has its own deadline so a slow optional request cannot discard initialization.
|
|
// Only the rate limits are read, so skip the local transcript scan that fills
|
|
// `behaviors`: with a few GB of transcripts it outlasts the deadline.
|
|
const usageResult = includeUsage
|
|
? yield* Effect.tryPromise(() =>
|
|
q.usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET({ skipBehaviors: true }),
|
|
).pipe(Effect.timeout(DEFAULT_TIMEOUT_MS), Effect.result)
|
|
: undefined;
|
|
const usage =
|
|
usageResult && Result.isSuccess(usageResult)
|
|
? {
|
|
rate_limits_available: usageResult.success.rate_limits_available,
|
|
rate_limits: usageResult.success.rate_limits,
|
|
}
|
|
: undefined;
|
|
const account = init.account as
|
|
| {
|
|
readonly email?: string;
|
|
readonly subscriptionType?: string;
|
|
readonly tokenSource?: string;
|
|
readonly apiKeySource?: string;
|
|
readonly apiProvider?: string;
|
|
}
|
|
| undefined;
|
|
return {
|
|
email: account?.email,
|
|
subscriptionType: account?.subscriptionType,
|
|
tokenSource: account?.tokenSource,
|
|
apiKeySource: account?.apiKeySource,
|
|
apiProvider: account?.apiProvider,
|
|
slashCommands: parseClaudeInitializationCommands(init.commands),
|
|
...(usage ? { usage } : {}),
|
|
} satisfies ClaudeCapabilitiesProbe;
|
|
}),
|
|
),
|
|
Effect.ensuring(
|
|
Effect.sync(() => {
|
|
if (!abort.signal.aborted) abort.abort();
|
|
}),
|
|
),
|
|
Effect.result,
|
|
Effect.map((result) => (Result.isSuccess(result) ? result.success : undefined)),
|
|
);
|
|
};
|
|
|
|
const runClaudeCommand = Effect.fn("runClaudeCommand")(function* (
|
|
claudeSettings: ClaudeSettings,
|
|
args: ReadonlyArray<string>,
|
|
environment?: NodeJS.ProcessEnv,
|
|
) {
|
|
const claudeEnvironment = yield* makeClaudeEnvironment(claudeSettings, environment);
|
|
const spawnCommand = yield* resolveSpawnCommand(claudeSettings.binaryPath, args, {
|
|
env: claudeEnvironment,
|
|
});
|
|
const command = ChildProcess.make(spawnCommand.command, spawnCommand.args, {
|
|
env: claudeEnvironment,
|
|
shell: spawnCommand.shell,
|
|
});
|
|
return yield* spawnAndCollect(claudeSettings.binaryPath, command);
|
|
});
|
|
|
|
/** Read commands from the same cwd Claude uses for a workspace session. */
|
|
export const probeClaudeWorkspaceSnapshot = Effect.fn("probeClaudeWorkspaceSnapshot")(function* (
|
|
claudeSettings: ClaudeSettings,
|
|
machineSnapshot: ServerProvider,
|
|
cwd: string,
|
|
environment?: NodeJS.ProcessEnv,
|
|
): Effect.fn.Return<ProviderWorkspaceSnapshot, never, FileSystem.FileSystem | Path.Path> {
|
|
if (!claudeSettings.enabled) return machineSnapshot;
|
|
const skills = yield* discoverClaudeSkills(claudeSettings, cwd, environment);
|
|
const capabilities = yield* probeClaudeCapabilities(claudeSettings, environment, cwd, false);
|
|
return {
|
|
...machineSnapshot,
|
|
skills,
|
|
slashCommands: dedupeSlashCommands([
|
|
COMPACT_SLASH_COMMAND,
|
|
...(capabilities?.slashCommands ?? []),
|
|
]),
|
|
slashCommandsPending: !capabilities,
|
|
};
|
|
});
|
|
|
|
export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(function* (
|
|
claudeSettings: ClaudeSettings,
|
|
resolveCapabilities?: (
|
|
claudeSettings: ClaudeSettings,
|
|
) => Effect.Effect<ClaudeCapabilitiesProbe | undefined>,
|
|
environment?: NodeJS.ProcessEnv,
|
|
cwd?: string,
|
|
modelCatalog: ClaudeModelCatalog = BUNDLED_CLAUDE_MODEL_CATALOG,
|
|
/** Shared with the adapter so turn events reuse the scoped-bucket names this probe saw. */
|
|
scopedLimitNames?: Ref.Ref<ClaudeScopedLimitNames>,
|
|
/** Banked resets for a subscription login, given the CLI version for the user agent. */
|
|
resolveResetCredits?: (version: string) => Effect.Effect<ServerProviderResetCredits | undefined>,
|
|
): Effect.fn.Return<
|
|
ServerProviderDraft,
|
|
never,
|
|
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
|
|
> {
|
|
const resolvedEnvironment = environment ?? process.env;
|
|
const checkedAt = DateTime.formatIso(yield* DateTime.now);
|
|
const allModels = providerModelsFromSettings(
|
|
modelCatalog.models.map((entry) => entry.model),
|
|
claudeSettings.customModels,
|
|
DEFAULT_CLAUDE_MODEL_CAPABILITIES,
|
|
);
|
|
|
|
if (!claudeSettings.enabled) {
|
|
return buildServerProvider({
|
|
presentation: CLAUDE_PRESENTATION,
|
|
enabled: false,
|
|
checkedAt,
|
|
models: allModels,
|
|
probe: {
|
|
installed: false,
|
|
version: null,
|
|
status: "warning",
|
|
auth: { status: "unknown" },
|
|
message: "Claude is disabled in T3 Code settings.",
|
|
},
|
|
});
|
|
}
|
|
|
|
const versionProbe = yield* runClaudeCommand(
|
|
claudeSettings,
|
|
["--version"],
|
|
resolvedEnvironment,
|
|
).pipe(Effect.timeoutOption(DEFAULT_TIMEOUT_MS), Effect.result);
|
|
|
|
if (Result.isFailure(versionProbe)) {
|
|
const error = versionProbe.failure;
|
|
yield* Effect.logWarning("Claude Agent CLI health check failed.", {
|
|
errorTag: error._tag,
|
|
});
|
|
return buildServerProvider({
|
|
presentation: CLAUDE_PRESENTATION,
|
|
enabled: claudeSettings.enabled,
|
|
checkedAt,
|
|
models: allModels,
|
|
probe: {
|
|
installed: !isCommandMissingCause(error),
|
|
version: null,
|
|
status: "error",
|
|
auth: { status: "unknown" },
|
|
message: isCommandMissingCause(error)
|
|
? "Claude Agent CLI (`claude`) was not found on PATH."
|
|
: "Failed to execute Claude Agent CLI health check.",
|
|
},
|
|
});
|
|
}
|
|
|
|
if (Option.isNone(versionProbe.success)) {
|
|
return buildServerProvider({
|
|
presentation: CLAUDE_PRESENTATION,
|
|
enabled: claudeSettings.enabled,
|
|
checkedAt,
|
|
models: allModels,
|
|
probe: {
|
|
installed: true,
|
|
version: null,
|
|
status: "error",
|
|
auth: { status: "unknown" },
|
|
message:
|
|
"Claude Agent CLI is installed but failed to run. Timed out while running command.",
|
|
},
|
|
});
|
|
}
|
|
|
|
const version = versionProbe.success.value;
|
|
const parsedVersion = parseGenericCliVersion(`${version.stdout}\n${version.stderr}`);
|
|
if (version.code !== 0) {
|
|
yield* Effect.logWarning("Claude Agent CLI version probe exited with a non-zero status.", {
|
|
exitCode: version.code,
|
|
stdoutLength: version.stdout.length,
|
|
stderrLength: version.stderr.length,
|
|
});
|
|
return buildServerProvider({
|
|
presentation: CLAUDE_PRESENTATION,
|
|
enabled: claudeSettings.enabled,
|
|
checkedAt,
|
|
models: allModels,
|
|
probe: {
|
|
installed: true,
|
|
version: parsedVersion,
|
|
status: "error",
|
|
auth: { status: "unknown" },
|
|
message: "Claude Agent CLI is installed but failed to run.",
|
|
},
|
|
});
|
|
}
|
|
|
|
const models = providerModelsFromSettings(
|
|
resolveClaudeModelsForVersion(modelCatalog, parsedVersion),
|
|
claudeSettings.customModels,
|
|
DEFAULT_CLAUDE_MODEL_CAPABILITIES,
|
|
);
|
|
const updateRequiredModels = resolveClaudeUpdateRequiredModels(modelCatalog, parsedVersion);
|
|
const versionUpgradeMessage = formatClaudeVersionUpgradeMessage(modelCatalog, parsedVersion);
|
|
|
|
const capabilities = resolveCapabilities
|
|
? yield* resolveCapabilities(claudeSettings).pipe(Effect.orElseSucceed(() => undefined))
|
|
: undefined;
|
|
const skills = yield* discoverClaudeSkills(claudeSettings, cwd, resolvedEnvironment);
|
|
const slashCommands = [COMPACT_SLASH_COMMAND, ...(capabilities?.slashCommands ?? [])];
|
|
const dedupedSlashCommands = dedupeSlashCommands(slashCommands);
|
|
|
|
if (!capabilities) {
|
|
return buildServerProvider({
|
|
presentation: CLAUDE_PRESENTATION,
|
|
enabled: claudeSettings.enabled,
|
|
checkedAt,
|
|
models,
|
|
updateRequiredModels,
|
|
slashCommands: dedupedSlashCommands,
|
|
skills,
|
|
probe: {
|
|
installed: true,
|
|
version: parsedVersion,
|
|
status: "warning",
|
|
auth: { status: "unknown" },
|
|
message: "Could not verify Claude authentication status from initialization result.",
|
|
},
|
|
});
|
|
}
|
|
|
|
if (claudeAuthStatus(capabilities) === "unauthenticated") {
|
|
return buildServerProvider({
|
|
presentation: CLAUDE_PRESENTATION,
|
|
enabled: claudeSettings.enabled,
|
|
checkedAt,
|
|
models,
|
|
slashCommands: dedupedSlashCommands,
|
|
skills,
|
|
probe: {
|
|
installed: true,
|
|
version: parsedVersion,
|
|
status: "error",
|
|
auth: { status: "unauthenticated" },
|
|
message: "Claude Code is not authenticated. Run `claude auth login` and try again.",
|
|
},
|
|
});
|
|
}
|
|
|
|
const authMetadata =
|
|
claudeAuthMetadata({
|
|
subscriptionType: capabilities.subscriptionType,
|
|
authMethod: capabilities.tokenSource,
|
|
}) ?? apiProviderAuthMetadata(capabilities.apiProvider);
|
|
const usageLimits = !capabilities.usage
|
|
? makeUnavailableUsageLimits({ checkedAt, reason: "probeFailed" })
|
|
: scopedLimitNames
|
|
? yield* recordClaudeUsageResponse(scopedLimitNames, {
|
|
response: capabilities.usage,
|
|
checkedAt,
|
|
})
|
|
: claudeUsageResponseToLimits({ response: capabilities.usage, checkedAt }).limits;
|
|
const resetCredits =
|
|
resolveResetCredits &&
|
|
capabilities.subscriptionType &&
|
|
!usageLimits.unavailable &&
|
|
parsedVersion
|
|
? yield* resolveResetCredits(parsedVersion)
|
|
: undefined;
|
|
return buildServerProvider({
|
|
presentation: CLAUDE_PRESENTATION,
|
|
enabled: claudeSettings.enabled,
|
|
checkedAt,
|
|
models,
|
|
updateRequiredModels,
|
|
slashCommands: dedupedSlashCommands,
|
|
skills,
|
|
probe: {
|
|
installed: true,
|
|
version: parsedVersion,
|
|
status: "ready",
|
|
auth: {
|
|
status: "authenticated",
|
|
...(capabilities.email ? { email: capabilities.email } : {}),
|
|
...(capabilities.workspaceId ? { workspaceId: capabilities.workspaceId } : {}),
|
|
...(authMetadata ? authMetadata : {}),
|
|
},
|
|
...(versionUpgradeMessage ? { message: versionUpgradeMessage } : {}),
|
|
usageLimits: resetCredits ? { ...usageLimits, resetCredits } : usageLimits,
|
|
},
|
|
});
|
|
});
|
|
|
|
const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
|
|
|
|
export const makePendingClaudeProvider = (
|
|
claudeSettings: ClaudeSettings,
|
|
modelCatalog: ClaudeModelCatalog = BUNDLED_CLAUDE_MODEL_CATALOG,
|
|
): Effect.Effect<ServerProviderDraft> =>
|
|
Effect.gen(function* () {
|
|
const checkedAt = yield* nowIso;
|
|
const models = providerModelsFromSettings(
|
|
modelCatalog.models.map((entry) => entry.model),
|
|
claudeSettings.customModels,
|
|
DEFAULT_CLAUDE_MODEL_CAPABILITIES,
|
|
);
|
|
|
|
if (!claudeSettings.enabled) {
|
|
return buildServerProvider({
|
|
presentation: CLAUDE_PRESENTATION,
|
|
enabled: false,
|
|
checkedAt,
|
|
models,
|
|
probe: {
|
|
installed: false,
|
|
version: null,
|
|
status: "warning",
|
|
auth: { status: "unknown" },
|
|
message: "Claude is disabled in T3 Code settings.",
|
|
},
|
|
});
|
|
}
|
|
|
|
return buildServerProvider({
|
|
presentation: CLAUDE_PRESENTATION,
|
|
enabled: true,
|
|
checkedAt,
|
|
models,
|
|
probe: {
|
|
installed: false,
|
|
version: null,
|
|
status: "warning",
|
|
auth: { status: "unknown" },
|
|
message: "Claude provider status has not been checked in this session yet.",
|
|
},
|
|
});
|
|
});
|
|
|
|
export { probeClaudeCapabilities };
|