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

CodexDeveloperInstructions.ts

66 lines2.5 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;
19​
20const normalizeAvailability = (
21 availability: boolean | T3CodeToolAvailability,
22): T3CodeToolAvailability =>
23 typeof availability === "boolean" ? { browser: availability, device: false } : availability;
24​
25/** Instructions for unavailable tools would steer the model away from its remaining automation. */
26const toolInstructions = (availability: boolean | T3CodeToolAvailability): string => {
27 const tools = normalizeAvailability(availability);
28 return [
29 tools.browser ? T3_CODE_BROWSER_TOOL_INSTRUCTIONS : "",
30 tools.device ? T3_CODE_DEVICE_TOOL_INSTRUCTIONS : "",
31 ]
32 .filter(Boolean)
33 .join("\n\n");
34};
35​
36export interface CodexRuntimeInfo {
37 readonly model: string;
38 readonly modelName?: string | undefined;
39 readonly reasoningEffort?: string | undefined;
41​
42/** Keep this outside collaboration mode; model-catalog mode text overrides client developer_instructions. */
43export function buildCodexAdditionalContext(
44 runtime: CodexRuntimeInfo,
45 toolsAvailable: boolean | T3CodeToolAvailability = true,
46): Record<string, V2TurnStartParams__AdditionalContextEntry> {
47 const tools = toolInstructions(toolsAvailable);
48 // Split values to fit per-entry caps; number keys because Codex emits them in key order.
49 return {
50 ...Object.fromEntries(
51 T3_CODE_ORCHESTRATION_SECTIONS.map((section, index) => [
52 `t3_code_orchestration_${String(index + 1).padStart(2, "0")}`,
53 { kind: "application" as const, value: section },
54 ]),
55 ),
56 t3_code_runtime: {
57 kind: "application",
58 value: buildRuntimeInstructions({
59 harness: "Codex",
60 ...runtime,
61 computerUse: typeof toolsAvailable !== "boolean" && toolsAvailable.computerUse === true,
62 }),
63 },
64 ...(tools ? { t3_code_tools: { kind: "application", value: tools } } : {}),
65 };