Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CodexDeveloperInstructions.ts

69 lines2.7 KB

1import type { V2TurnStartParams__AdditionalContextEntry } from "effect-codex-app-server/schema";
2import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeInstructions";
3​
4import {
5 T3_CODE_BROWSER_TOOL_INSTRUCTIONS,
6 T3_CODE_DEVICE_TOOL_INSTRUCTIONS,
7 T3_CODE_ORCHESTRATION_SECTIONS,
8} from "@t3tools/provider-core/server/orchestrationInstructions";
9​
10/** Codex silently truncates additionalContext values over 1,000 tokens, counted as UTF-8 bytes / 4. */
11export const CODEX_ADDITIONAL_CONTEXT_VALUE_MAX_BYTES = 4_000;
12​
13export interface T3CodeToolAvailability {
14 readonly browser: boolean;
15 readonly device: boolean;
16 /** The managed `cua-driver` MCP server is attached to this turn. */
17 readonly computerUse?: boolean;
18 /** The browser `chrome-devtools` drives, when browser tabs are attached. */
19 readonly browserTabs?: string;
21​
22const normalizeAvailability = (
23 availability: boolean | T3CodeToolAvailability,
24): T3CodeToolAvailability =>
25 typeof availability === "boolean" ? { browser: availability, device: false } : availability;
26​
27/** Instructions for unavailable tools would steer the model away from its remaining automation. */
28const toolInstructions = (availability: boolean | T3CodeToolAvailability): string => {
29 const tools = normalizeAvailability(availability);
30 return [
31 tools.browser ? T3_CODE_BROWSER_TOOL_INSTRUCTIONS : "",
32 tools.device ? T3_CODE_DEVICE_TOOL_INSTRUCTIONS : "",
33 ]
34 .filter(Boolean)
35 .join("\n\n");
36};
37​
38export interface CodexRuntimeInfo {
39 readonly model: string;
40 readonly modelName?: string | undefined;
41 readonly reasoningEffort?: string | undefined;
43​
44/** Keep this outside collaboration mode; model-catalog mode text overrides client developer_instructions. */
45export function buildCodexAdditionalContext(
46 runtime: CodexRuntimeInfo,
47 toolsAvailable: boolean | T3CodeToolAvailability = true,
48): Record<string, V2TurnStartParams__AdditionalContextEntry> {
49 const tools = toolInstructions(toolsAvailable);
50 // Split values to fit per-entry caps; number keys because Codex emits them in key order.
51 return {
52 ...Object.fromEntries(
53 T3_CODE_ORCHESTRATION_SECTIONS.map((section, index) => [
54 `t3_code_orchestration_${String(index + 1).padStart(2, "0")}`,
55 { kind: "application" as const, value: section },
56 ]),
57 ),
58 t3_code_runtime: {
59 kind: "application",
60 value: buildRuntimeInstructions({
61 harness: "Codex",
62 ...runtime,
63 computerUse: typeof toolsAvailable !== "boolean" && toolsAvailable.computerUse === true,
64 browserTabs: typeof toolsAvailable === "boolean" ? undefined : toolsAvailable.browserTabs,
65 }),
66 },
67 ...(tools ? { t3_code_tools: { kind: "application", value: tools } } : {}),
68 };