Skip to content
Open
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
13 changes: 13 additions & 0 deletions apps/server/src/provider/Layers/CodexAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
type CodexSessionRuntimeOptions,
type CodexSessionRuntimeSendTurnInput,
type CodexSessionRuntimeShape,
type CodexSessionRuntimeSlashCommandInput,
type CodexThreadSnapshot,
} from "./CodexSessionRuntime.ts";
import { makeCodexAdapter } from "./CodexAdapter.ts";
Expand Down Expand Up @@ -83,6 +84,14 @@ class FakeCodexRuntime implements CodexSessionRuntimeShape {
}),
);

public readonly runSlashCommandImpl = vi.fn(
(_input: CodexSessionRuntimeSlashCommandInput): Promise<ProviderTurnStartResult> =>
Promise.resolve({
threadId: this.options.threadId,
turnId: asTurnId("turn-1"),
}),
);

public readonly interruptTurnImpl = vi.fn(
(_turnId?: TurnId): Promise<void> => Promise.resolve(undefined),
);
Expand Down Expand Up @@ -131,6 +140,10 @@ class FakeCodexRuntime implements CodexSessionRuntimeShape {
return Effect.promise(() => this.sendTurnImpl(input));
}

runSlashCommand(input: CodexSessionRuntimeSlashCommandInput) {
return Effect.promise(() => this.runSlashCommandImpl(input));
}

interruptTurn(turnId?: TurnId) {
return Effect.promise(() => this.interruptTurnImpl(turnId));
}
Expand Down
48 changes: 45 additions & 3 deletions apps/server/src/provider/Layers/CodexAdapter.ts
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Comment thread
StiensWout marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
makeCodexSessionRuntime,
type CodexSessionRuntimeError,
type CodexSessionRuntimeOptions,
type CodexSessionRuntimeSlashCommandInput,
type CodexSessionRuntimeShape,
} from "./CodexSessionRuntime.ts";
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
Expand Down Expand Up @@ -92,6 +93,34 @@ interface CodexAdapterSessionContext {
stopped: boolean;
}

function parseStandaloneCodexSlashCommand(
input: ProviderSendTurnInput,
): CodexSessionRuntimeSlashCommandInput | null {
if ((input.attachments?.length ?? 0) > 0) {
return null;
}

const text = input.input?.trim();
if (!text) {
return null;
}

const match = /^\/([a-z][a-z-]*)(?:\s+([\s\S]*))?$/i.exec(text);
if (!match) {
return null;
}

const command = match[1]?.toLowerCase();
const args = match[2]?.trim();
if (command === "compact" && !args) {
return { command: "compact" };
}
if (command === "review") {
return args ? { command: "review", instructions: args } : { command: "review" };
}
return null;
}

function mapCodexRuntimeError(
threadId: ThreadId,
method: string,
Expand Down Expand Up @@ -1531,16 +1560,29 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
input.modelSelection?.instanceId === boundInstanceId
? getModelSelectionStringOptionValue(input.modelSelection, "reasoningEffort")
: undefined;
const selectedModel =
input.modelSelection?.instanceId === boundInstanceId ? input.modelSelection.model : undefined;
const serviceTier =
input.modelSelection?.instanceId === boundInstanceId
? getCodexServiceTierOptionValue(input.modelSelection)
: undefined;
const slashCommand = parseStandaloneCodexSlashCommand(input);
if (slashCommand) {
return yield* session.runtime
.runSlashCommand({
...slashCommand,
...(selectedModel ? { model: selectedModel } : {}),
})
.pipe(
Effect.mapError((cause) =>
mapCodexRuntimeError(input.threadId, `/${slashCommand.command}`, cause),
),
);
}
return yield* session.runtime
.sendTurn({
...(input.input !== undefined ? { input: input.input } : {}),
...(input.modelSelection?.instanceId === boundInstanceId
? { model: input.modelSelection.model }
: {}),
...(selectedModel ? { model: selectedModel } : {}),
...(reasoningEffort
? {
effort: reasoningEffort as EffectCodexSchema.V2TurnStartParams__ReasoningEffort,
Expand Down
19 changes: 19 additions & 0 deletions apps/server/src/provider/Layers/CodexProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
ModelCapabilities,
ProviderOptionDescriptor,
ServerProviderModel,
ServerProviderSlashCommand,
ServerProviderSkill,
} from "@t3tools/contracts";
import { ServerSettingsError } from "@t3tools/contracts";
Expand All @@ -42,6 +43,18 @@ const CODEX_PRESENTATION = {
showInteractionModeToggle: true,
} as const;

const CODEX_APP_SERVER_SLASH_COMMANDS = [
{
name: "review",
description: "Review current changes and find issues",
input: { hint: "optional review instructions" },
},
{
name: "compact",
description: "Summarize conversation history to preserve context",
},
] satisfies ReadonlyArray<ServerProviderSlashCommand>;

export interface CodexAppServerProviderSnapshot {
readonly account: CodexSchema.V2GetAccountResponse;
readonly version: string | undefined;
Expand Down Expand Up @@ -402,6 +415,7 @@ const makePendingCodexProvider = (
enabled: false,
checkedAt,
models,
slashCommands: [],
skills: [],
probe: {
installed: false,
Expand All @@ -418,6 +432,7 @@ const makePendingCodexProvider = (
enabled: true,
checkedAt,
models,
slashCommands: [],
skills: [],
probe: {
installed: false,
Expand Down Expand Up @@ -487,6 +502,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
enabled: false,
checkedAt,
models: emptyModels,
slashCommands: [],
skills: [],
probe: {
installed: false,
Expand Down Expand Up @@ -518,6 +534,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
enabled: codexSettings.enabled,
checkedAt,
models: emptyModels,
slashCommands: [],
skills: [],
probe: {
installed,
Expand All @@ -537,6 +554,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
enabled: codexSettings.enabled,
checkedAt,
models: emptyModels,
slashCommands: [],
skills: [],
probe: {
installed: true,
Expand All @@ -556,6 +574,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
enabled: codexSettings.enabled,
checkedAt,
models: snapshot.models,
slashCommands: CODEX_APP_SERVER_SLASH_COMMANDS,
skills: snapshot.skills,
probe: {
installed: true,
Expand Down
Loading
Loading