contracts.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /** Product-level Plugin Hook events shared by Desktop and Cloud runtimes. */
  2. export const PLUGIN_HOOK_EVENTS = [
  3. 'SessionStart',
  4. 'SessionEnd',
  5. 'UserPromptSubmit',
  6. 'PreToolUse',
  7. 'PermissionRequest',
  8. 'PostToolUse',
  9. 'SubagentStart',
  10. 'SubagentStop',
  11. 'Stop',
  12. 'PreCompact',
  13. 'PostCompact',
  14. ] as const;
  15. export type PluginHookEventName = (typeof PLUGIN_HOOK_EVENTS)[number];
  16. export type PluginHookSourceFormat = 'MINIMAX' | 'CLAUDE' | 'CODEX';
  17. export interface PluginHookEffort {
  18. readonly level: 'low' | 'medium' | 'high' | 'xhigh' | 'max';
  19. }
  20. export type PluginHookSessionStartSource =
  21. | 'startup'
  22. | 'resume'
  23. | 'clear'
  24. | 'compact'
  25. | 'fork'
  26. | 'plugin_activation';
  27. export interface PluginHookCommandHandler {
  28. readonly kind: 'command';
  29. readonly sourceFormat: PluginHookSourceFormat;
  30. readonly pluginName: string;
  31. readonly pluginRoot: string;
  32. /** Host-owned writable state directory; the Plugin package root is immutable. */
  33. readonly pluginDataDir?: string;
  34. /** Changes whenever this Plugin capability is updated or re-activated. */
  35. readonly activationKey?: string;
  36. readonly sourcePath: string;
  37. readonly event: PluginHookEventName;
  38. readonly matcher?: string;
  39. readonly command: string;
  40. /** Compatible exec-form arguments. Omitted means shell-form command execution. */
  41. readonly args?: readonly string[];
  42. /** Compatible shell-form override. Ignored when `args` selects exec form. */
  43. readonly shell?: 'bash' | 'powershell';
  44. /** Compatible tool-input predicate such as `Bash(rm *)`. */
  45. readonly condition?: string;
  46. readonly timeoutMs: number;
  47. /** Codex-only per-handler model-visible context limit; 0 disables spilling. */
  48. readonly additionalContextLimit?: number;
  49. readonly declarationOrder: number;
  50. }
  51. export interface PluginHookDiagnostic {
  52. readonly code: string;
  53. readonly pluginName: string;
  54. readonly sourcePath: string;
  55. readonly event?: PluginHookEventName;
  56. readonly declarationOrder?: number;
  57. }
  58. export interface PluginHookSet {
  59. readonly handlers: readonly PluginHookCommandHandler[];
  60. readonly diagnostics: readonly PluginHookDiagnostic[];
  61. }
  62. export interface PluginHookEventInput {
  63. readonly event: PluginHookEventName;
  64. readonly sessionId: string;
  65. readonly turnId?: string;
  66. /** Path to the active root transcript. `null` is serialized when no transcript exists yet. */
  67. readonly transcriptPath?: string | null;
  68. /** Codex rollout projection; Compatible continues to use `transcriptPath`. */
  69. readonly codexTranscriptPath?: string | null;
  70. readonly cwd: string;
  71. readonly model?: string;
  72. readonly permissionMode?: string;
  73. /** Compatible-only effective effort for events inside a tool-use context. */
  74. readonly effort?: PluginHookEffort;
  75. /** Compatible's prompt-scoped identifier. Codex uses `turnId` instead. */
  76. readonly promptId?: string;
  77. /** Internal parent/child identities used to project vendor-specific subagent semantics. */
  78. readonly subagentContext?: {
  79. readonly parentSessionId: string;
  80. readonly parentTurnId: string;
  81. readonly parentTranscriptPath?: string | null;
  82. readonly parentCodexTranscriptPath?: string | null;
  83. readonly childSessionId: string;
  84. readonly childTranscriptPath?: string | null;
  85. readonly childCodexTranscriptPath?: string | null;
  86. };
  87. /**
  88. * Provenance for a tool whose vendor-visible identity cannot be derived from
  89. * its runtime name alone. In particular, plugin MCP names are scoped only
  90. * when the host proves which plugin owns the server.
  91. */
  92. readonly toolProvenance?: {
  93. readonly kind: 'plugin_mcp' | 'plugin_host';
  94. readonly pluginName: string;
  95. };
  96. readonly matcherValue?: string;
  97. readonly payload?: Readonly<Record<string, unknown>>;
  98. }
  99. export type PluginHookPermissionDecision = 'allow' | 'deny' | 'ask' | 'defer';
  100. export type PluginHookPermissionResolution = 'allow' | 'deny' | 'abstain';
  101. export type PluginHookPermissionAutoApproval = 'ordinary_only' | 'any_prompt';
  102. export type PluginHookToolPermissionResolution = 'allow' | 'deny' | 'ask' | 'defer' | 'abstain';
  103. /** Compatible-compatible destination. `cliArg` is intentionally not host-persistable. */
  104. export type PluginHookPermissionUpdateDestination =
  105. | 'session'
  106. | 'localSettings'
  107. | 'projectSettings'
  108. | 'userSettings';
  109. export type PluginHookPermissionUpdateMode =
  110. | 'default'
  111. | 'auto'
  112. | 'acceptEdits'
  113. | 'dontAsk'
  114. | 'bypassPermissions'
  115. | 'plan';
  116. export interface PluginHookPermissionRuleValue {
  117. readonly toolName: string;
  118. readonly ruleContent?: string;
  119. }
  120. /**
  121. * Host-neutral, fully validated form of Compatible PermissionRequest
  122. * `updatedPermissions`. Hosts must apply the complete array atomically.
  123. */
  124. export type PluginHookPermissionUpdate =
  125. | {
  126. readonly type: 'addRules' | 'replaceRules' | 'removeRules';
  127. readonly rules: readonly PluginHookPermissionRuleValue[];
  128. readonly behavior: 'allow' | 'deny' | 'ask';
  129. readonly destination: PluginHookPermissionUpdateDestination;
  130. }
  131. | {
  132. readonly type: 'setMode';
  133. readonly mode: PluginHookPermissionUpdateMode;
  134. readonly destination: PluginHookPermissionUpdateDestination;
  135. }
  136. | {
  137. readonly type: 'addDirectories' | 'removeDirectories';
  138. readonly directories: readonly string[];
  139. readonly destination: PluginHookPermissionUpdateDestination;
  140. };
  141. export interface PluginHookDecision {
  142. readonly decision: PluginHookPermissionDecision;
  143. /**
  144. * PermissionRequest-specific resolution. An omitted Hook output is `abstain`;
  145. * an explicit `allow` authorizes the tool without showing the product UI.
  146. */
  147. readonly permissionDecision?: PluginHookPermissionResolution;
  148. /**
  149. * Scope of a PermissionRequest allow. Compatible still evaluates deny/ask rules;
  150. * Codex treats the Hook verdict as the final approval.
  151. */
  152. readonly permissionAutoApproval?: PluginHookPermissionAutoApproval;
  153. /** PreToolUse-specific permission control, distinct from an absent verdict. */
  154. readonly toolPermissionDecision?: PluginHookToolPermissionResolution;
  155. readonly reason?: string;
  156. readonly additionalContext?: string;
  157. readonly updatedInput?: Readonly<Record<string, unknown>>;
  158. /** Compatible-only permission mutations; the Desktop host owns atomic application. */
  159. readonly updatedPermissions?: readonly PluginHookPermissionUpdate[];
  160. /** Compatible PermissionRequest deny-only request to stop the active agent run. */
  161. readonly interrupt?: boolean;
  162. readonly continuePrompt?: string;
  163. readonly defer?: boolean;
  164. /** Universal Compatible/Codex control output. `false` stops the active agent run. */
  165. readonly continue?: boolean;
  166. readonly stopReason?: string;
  167. readonly suppressOutput?: boolean;
  168. readonly systemMessage?: string;
  169. /** Compatible-only, allowlisted terminal notification bytes for a real terminal surface. */
  170. readonly terminalSequence?: string;
  171. /** Codex PostToolUse error feedback for the next model step; never an agent hard stop. */
  172. readonly postToolFeedback?: string;
  173. /** Replacement for the model-visible tool result; audit/persistence keeps the original result. */
  174. readonly updatedResult?: unknown;
  175. /** Vendor contract that owns `updatedResult`; hosts use its native serializer. */
  176. readonly updatedResultFormat?: 'CLAUDE' | 'MINIMAX';
  177. }
  178. export interface PluginHookRunDiagnostic extends PluginHookDiagnostic {
  179. readonly code:
  180. | 'HOOK_ABORTED'
  181. | 'HOOK_INVALID_INPUT'
  182. | 'HOOK_INVALID_OUTPUT'
  183. | 'HOOK_PROCESS_ERROR'
  184. | 'HOOK_PROCESS_EXITED'
  185. | 'HOOK_TIMEOUT';
  186. }
  187. export interface PluginHookRunResult {
  188. readonly decision: PluginHookDecision;
  189. readonly diagnostics: readonly PluginHookRunDiagnostic[];
  190. }
  191. export interface PluginSubagentRegistration {
  192. readonly handlers: readonly PluginHookCommandHandler[];
  193. readonly parentSessionId: string;
  194. readonly parentTurnId: string;
  195. /** Compatible prompt that triggered the subagent; distinct from the child turn id. */
  196. readonly parentPromptId?: string;
  197. readonly parentTranscriptPath?: string | null;
  198. readonly parentCodexTranscriptPath?: string | null;
  199. readonly childSessionId: string;
  200. readonly childTurnId: string;
  201. readonly agentId: string;
  202. readonly agentType: string;
  203. readonly agentTranscriptPath?: string | null;
  204. readonly agentCodexTranscriptPath?: string | null;
  205. readonly cwd: string;
  206. readonly model?: string;
  207. readonly permissionMode?: string;
  208. readonly effort?: PluginHookEffort;
  209. }
  210. export interface PluginHookLogger {
  211. warn(fields: Readonly<Record<string, unknown>>, message: string): void;
  212. }
  213. export interface PluginHookObserver {
  214. onHandler(input: {
  215. readonly event: PluginHookEventName;
  216. readonly format: PluginHookSourceFormat;
  217. readonly outcome: 'success' | 'deny' | 'ask' | 'error' | 'timeout' | 'aborted';
  218. readonly durationMs: number;
  219. readonly processKilled: boolean;
  220. }): void;
  221. onEvent(input: {
  222. readonly event: PluginHookEventName;
  223. readonly outcome: 'success' | 'deny' | 'ask' | 'degraded' | 'timeout' | 'aborted';
  224. readonly durationMs: number;
  225. }): void;
  226. }