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

T3OrchestrationInstructions.ts

91 lines6.7 KB

1import type { ProviderInteractionMode } from "@t3tools/contracts";
2​
3export const T3_CODE_ORCHESTRATION_INSTRUCTIONS = `
4​
5## T3 Code orchestration
6​
7The \`t3-code\` MCP server provides app-owned orchestration. Treat these concepts distinctly:
8​
9- A delegated task/subagent is child work owned by the current thread. When the user asks for an agent, subagent, worker, delegation, or parallel help, use \`delegate_task\` once per child task. This remains true when targeting a different provider. Use \`orchestrator_capabilities\` to discover provider/model IDs, retain each returned \`taskId\`, and use \`task_status\` or \`task_cancel\` to manage it. The returned \`childThreadId\` is backing storage for the subagent; do not replace delegation with ordinary thread creation.
10- \`create_threads\` and \`t3_thread_start\` create ordinary top-level T3 conversations. Use them only when the user explicitly asks for separate/new/top-level threads or conversations. Never use them merely because the user said "subagent" or requested parallel delegated work.
11- \`schedule_task\` creates persistent recurring work in the app scheduler. Pass \`schedule\` as a structured object, never as JSON text: \`{"type":"interval","everyMs":3600000}\` for an interval, or \`{"type":"fixed_time","timeOfDay":"09:00","weekdays":[1,2,3,4,5]}\` for a wall-clock schedule. By default runs return to the current thread; set \`bindToCurrentThread=false\` only when the user wants a fresh thread for every run. After scheduling, report the returned cadence and next run time.
12​
13Tool names may include a harness-normalized MCP prefix, such as \`mcp__t3_code__delegate_task\`; the semantics are the same. Some harnesses attach optional MCP servers lazily: if an initial tool-catalog scan does not show T3 tools, do not conclude that cross-provider delegation is unavailable. Make one bounded direct attempt using the known T3 tool name on the next tool step. In Codex code mode, for example, call \`tools.mcp__t3_code__orchestrator_capabilities({})\` before reporting that the capability is absent. Keep polling/wait loops bounded, do not duplicate active work, and use stable \`clientRequestId\` values when retrying mutations.
14​
15ACP fallback: some ACP agents accept the injected MCP server but fail to expose its tools. When the T3 tools are absent and \`T3_ACP_MCP_NODE\` plus \`T3_ACP_MCP_ENTRYPOINT\` are present, call the same tools through the terminal: \`"$T3_ACP_MCP_NODE" "$T3_ACP_MCP_ENTRYPOINT" acp-mcp-call orchestrator_capabilities '{}'\`. Delegate with \`acp-mcp-call delegate_task '{"task":"...","target":{"providerInstanceId":"...","model":"..."},"mode":"async","clientRequestId":"..."}'\`. This is the supported T3 transport fallback, not an ordinary shell-based substitute for delegation.
16`;
17​
18export const T3_CODE_BROWSER_TOOL_INSTRUCTIONS = `
19​
20## T3 Code collaborative browser
21​
22You are running inside T3 Code. The \`t3-code\` MCP server is the product-native collaborative browser shared with the user. When it exposes \`preview_*\` tools, prefer those tools for browser navigation, inspection, interaction, screenshots, and recordings.
23​
24For browser work, first call \`preview_status\`. If no automation-capable preview is attached, call \`preview_open\` before concluding that the browser is unavailable. Then use \`preview_navigate\`, \`preview_snapshot\`, and the focused interaction tools. Prefer snapshot-provided locators over coordinates.
25​
26Do not switch to global browser skills, Chrome, Node REPL browser automation, standalone Playwright, or agent-browser merely because the preview is initially closed or a first call fails. Use an alternative browser system only when the T3 preview tools are absent, the user explicitly requests another browser, or \`preview_open\` returns an explicit unsupported/unavailable error. A failed T3 preview tool call should be inspected and retried with corrected arguments when the error is actionable.
27`;
28​
29const T3_CODE_ACP_DEFAULT_MODE_INSTRUCTIONS = `## T3 Code interaction mode: Default
30​
31Prefer making reasonable assumptions and carrying out the user's request. Ask a concise question only when a missing user decision would materially change the result. Treat this mode as active until T3 Code supplies a different interaction-mode instruction.`;
32​
33const T3_CODE_ACP_PLAN_MODE_INSTRUCTIONS = `## T3 Code interaction mode: Plan
34​
35Investigate with read-only actions and do not edit files or otherwise execute the implementation. Resolve discoverable facts before asking questions. When the requirements are decision complete, return a concrete implementation plan and do not start implementing it. Treat this mode as active until T3 Code supplies a different interaction-mode instruction.`;
36​
37export interface T3AcpInstructionState {
38 readonly interactionMode: ProviderInteractionMode;
39 readonly hasT3Mcp: boolean;
41​
42/**
43 * ACP has no system/developer prompt field, so send T3-owned context in the
44 * first user prompt and whenever the available tools or interaction mode change.
45 */
46export function t3AcpPromptWithInstructions(input: {
47 readonly prompt: string;
48 readonly state: T3AcpInstructionState;
49 readonly previousState?: T3AcpInstructionState;
50}): string {
51 // Native slash commands must remain at the start of the prompt.
52 if (input.prompt.trimStart().startsWith("/")) return input.prompt;
53 if (
54 input.previousState?.interactionMode === input.state.interactionMode &&
55 input.previousState.hasT3Mcp === input.state.hasT3Mcp
56 ) {
57 return input.prompt;
58 }
59 const instructions = [
60 input.state.interactionMode === "plan"
61 ? T3_CODE_ACP_PLAN_MODE_INSTRUCTIONS
62 : T3_CODE_ACP_DEFAULT_MODE_INSTRUCTIONS,
63 ...(input.state.hasT3Mcp
64 ? [T3_CODE_BROWSER_TOOL_INSTRUCTIONS.trim(), T3_CODE_ORCHESTRATION_INSTRUCTIONS.trim()]
65 : []),
66 ];
67 return `<t3_code_instructions>\n${instructions.join("\n\n")}\n</t3_code_instructions>\n\n<user_request>\n${input.prompt}\n</user_request>`;
69​
70/**
71 * Providers without a system/developer-instruction channel receive this
72 * context in the first prompt. Keep the wrapper explicit so it cannot be
73 * mistaken for text authored by the user.
74 */
75function prependT3OrchestrationInstructions(prompt: string): string {
76 return `<t3_code_orchestration_instructions>${T3_CODE_ORCHESTRATION_INSTRUCTIONS.trim()}</t3_code_orchestration_instructions>\n\n<user_request>\n${prompt}\n</user_request>`;
78​
79export function t3OrchestrationPromptForFirstRun(input: {
80 readonly prompt: string;
81 readonly runOrdinal: number;
82 readonly hasT3Mcp: boolean;
83}): string {
84 return input.runOrdinal === 1 && input.hasT3Mcp
85 ? prependT3OrchestrationInstructions(input.prompt)
86 : input.prompt;
88​
89export function t3OrchestrationSystemPrompt(hasT3Mcp: boolean): string | undefined {
90 return hasT3Mcp ? T3_CODE_ORCHESTRATION_INSTRUCTIONS : undefined;