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
109 changes: 102 additions & 7 deletions apps/server/src/provider/Layers/ClaudeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import type {
ServerProvider,
ServerProviderModel,
ServerProviderAuth,
ServerProviderSlashCommand,
ServerProviderState,
} from "@t3tools/contracts";
import { Cache, Duration, Effect, Equal, Layer, Option, Result, Schema, Stream } from "effect";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { decodeJsonResult } from "@t3tools/shared/schemaJson";
import { query as claudeQuery } from "@anthropic-ai/claude-agent-sdk";
import {
query as claudeQuery,
type SlashCommand as ClaudeSlashCommand,
} from "@anthropic-ai/claude-agent-sdk";

import {
buildServerProvider,
Expand Down Expand Up @@ -340,6 +344,74 @@ function claudeAuthMetadata(input: {

const CAPABILITIES_PROBE_TIMEOUT_MS = 8_000;

function nonEmptyProbeString(value: string): string | undefined {
const candidate = value.trim();
return candidate ? candidate : undefined;
}

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()];
}

/**
* Probe account information by spawning a lightweight Claude Agent SDK
* session and reading the initialization result.
Expand All @@ -361,13 +433,16 @@ const probeClaudeCapabilities = (binaryPath: string) => {
pathToClaudeCodeExecutable: binaryPath,
abortController: abort,
maxTurns: 0,
settingSources: [],
settingSources: ["user", "project", "local"],
allowedTools: [],
stderr: () => {},
},
});
const init = await q.initializationResult();
return { subscriptionType: init.account?.subscriptionType };
return {
subscriptionType: init.account?.subscriptionType,
slashCommands: parseClaudeInitializationCommands(init.commands),
};
}).pipe(
Effect.ensuring(
Effect.sync(() => {
Expand Down Expand Up @@ -396,6 +471,9 @@ const runClaudeCommand = Effect.fn("runClaudeCommand")(function* (args: Readonly

export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(function* (
resolveSubscriptionType?: (binaryPath: string) => Effect.Effect<string | undefined>,
resolveSlashCommands?: (
binaryPath: string,
) => Effect.Effect<ReadonlyArray<ServerProviderSlashCommand> | undefined>,
): Effect.fn.Return<
ServerProvider,
ServerSettingsError,
Expand Down Expand Up @@ -491,6 +569,14 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
});
}

const slashCommands =
(resolveSlashCommands
? yield* resolveSlashCommands(claudeSettings.binaryPath).pipe(
Effect.orElseSucceed(() => undefined),
)
: undefined) ?? [];
const dedupedSlashCommands = dedupeSlashCommands(slashCommands);

// ── Auth check + subscription detection ────────────────────────────

const authProbe = yield* runClaudeCommand(["auth", "status"]).pipe(
Expand Down Expand Up @@ -525,6 +611,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
enabled: claudeSettings.enabled,
checkedAt,
models,
slashCommands: dedupedSlashCommands,
probe: {
installed: true,
version: parsedVersion,
Expand All @@ -544,6 +631,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
enabled: claudeSettings.enabled,
checkedAt,
models,
slashCommands: dedupedSlashCommands,
probe: {
installed: true,
version: parsedVersion,
Expand All @@ -561,6 +649,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
enabled: claudeSettings.enabled,
checkedAt,
models,
slashCommands: dedupedSlashCommands,
probe: {
installed: true,
version: parsedVersion,
Expand All @@ -583,12 +672,18 @@ export const ClaudeProviderLive = Layer.effect(
const subscriptionProbeCache = yield* Cache.make({
capacity: 1,
timeToLive: Duration.minutes(5),
lookup: (binaryPath: string) =>
probeClaudeCapabilities(binaryPath).pipe(Effect.map((r) => r?.subscriptionType)),
lookup: (binaryPath: string) => probeClaudeCapabilities(binaryPath),
});

const checkProvider = checkClaudeProviderStatus((binaryPath) =>
Cache.get(subscriptionProbeCache, binaryPath),
const checkProvider = checkClaudeProviderStatus(
(binaryPath) =>
Cache.get(subscriptionProbeCache, binaryPath).pipe(
Effect.map((probe) => probe?.subscriptionType),
),
(binaryPath) =>
Cache.get(subscriptionProbeCache, binaryPath).pipe(
Effect.map((probe) => probe?.slashCommands),
),
).pipe(
Effect.provideService(ServerSettingsService, serverSettings),
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
Expand Down
43 changes: 38 additions & 5 deletions apps/server/src/provider/Layers/CodexProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ServerProvider,
ServerProviderModel,
ServerProviderAuth,
ServerProviderSkill,
ServerProviderState,
} from "@t3tools/contracts";
import {
Expand Down Expand Up @@ -44,7 +45,7 @@ import {
codexAuthSubType,
type CodexAccountSnapshot,
} from "../codexAccount";
import { probeCodexAccount } from "../codexAppServer";
import { probeCodexDiscovery } from "../codexAppServer";
import { CodexProvider } from "../Services/CodexProvider";
import { ServerSettingsService } from "../../serverSettings";
import { ServerSettingsError } from "@t3tools/contracts";
Expand Down Expand Up @@ -304,8 +305,9 @@ const CAPABILITIES_PROBE_TIMEOUT_MS = 8_000;
const probeCodexCapabilities = (input: {
readonly binaryPath: string;
readonly homePath?: string;
readonly cwd: string;
}) =>
Effect.tryPromise((signal) => probeCodexAccount({ ...input, signal })).pipe(
Effect.tryPromise((signal) => probeCodexDiscovery({ ...input, signal })).pipe(
Effect.timeoutOption(CAPABILITIES_PROBE_TIMEOUT_MS),
Effect.result,
Effect.map((result) => {
Expand Down Expand Up @@ -334,6 +336,11 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
readonly binaryPath: string;
readonly homePath?: string;
}) => Effect.Effect<CodexAccountSnapshot | undefined>,
resolveSkills?: (input: {
readonly binaryPath: string;
readonly homePath?: string;
readonly cwd: string;
}) => Effect.Effect<ReadonlyArray<ServerProviderSkill> | undefined>,
): Effect.fn.Return<
ServerProvider,
ServerSettingsError,
Expand Down Expand Up @@ -449,12 +456,22 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
});
}

const skills =
(resolveSkills
? yield* resolveSkills({
binaryPath: codexSettings.binaryPath,
homePath: codexSettings.homePath,
cwd: process.cwd(),
}).pipe(Effect.orElseSucceed(() => undefined))
: undefined) ?? [];

if (yield* hasCustomModelProvider) {
return buildServerProvider({
provider: PROVIDER,
enabled: codexSettings.enabled,
checkedAt,
models,
skills,
probe: {
installed: true,
version: parsedVersion,
Expand Down Expand Up @@ -484,6 +501,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
enabled: codexSettings.enabled,
checkedAt,
models: resolvedModels,
skills,
probe: {
installed: true,
version: parsedVersion,
Expand All @@ -500,6 +518,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
enabled: codexSettings.enabled,
checkedAt,
models: resolvedModels,
skills,
probe: {
installed: true,
version: parsedVersion,
Expand All @@ -518,6 +537,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
enabled: codexSettings.enabled,
checkedAt,
models: resolvedModels,
skills,
probe: {
installed: true,
version: parsedVersion,
Expand All @@ -543,16 +563,29 @@ export const CodexProviderLive = Layer.effect(
capacity: 4,
timeToLive: Duration.minutes(5),
lookup: (key: string) => {
const [binaryPath, homePath] = JSON.parse(key) as [string, string | undefined];
const [binaryPath, homePath, cwd] = JSON.parse(key) as [string, string | undefined, string];
return probeCodexCapabilities({
binaryPath,
cwd,
...(homePath ? { homePath } : {}),
});
},
});

const checkProvider = checkCodexProviderStatus((input) =>
Cache.get(accountProbeCache, JSON.stringify([input.binaryPath, input.homePath])),
const getDiscovery = (input: {
readonly binaryPath: string;
readonly homePath?: string;
readonly cwd: string;
}) =>
Cache.get(accountProbeCache, JSON.stringify([input.binaryPath, input.homePath, input.cwd]));

const checkProvider = checkCodexProviderStatus(
(input) =>
getDiscovery({
...input,
cwd: process.cwd(),
}).pipe(Effect.map((discovery) => discovery?.account)),
(input) => getDiscovery(input).pipe(Effect.map((discovery) => discovery?.skills)),
).pipe(
Effect.provideService(ServerSettingsService, serverSettings),
Effect.provideService(FileSystem.FileSystem, fileSystem),
Expand Down
Loading
Loading