The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Option from "effect/Option"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | import type * as AcpSchema from "effect-acp/compat"; |
| 4 | import type { AcpToolCallState } from "@t3tools/provider-acp/server/runtimeModel"; |
| 5 | import { |
| 6 | acpUnfinishedStopMessage, |
| 7 | type AcpAdapterV2SubagentUpdate, |
| 8 | type AcpSubagentSessionEvent, |
| 9 | } from "@t3tools/provider-acp/server/adapter"; |
| 10 | |
| 11 | const decodeAgentInput = Schema.decodeUnknownOption( |
| 12 | Schema.Struct({ |
| 13 | prompt: Schema.String, |
| 14 | description: Schema.optionalKey(Schema.String), |
| 15 | model: Schema.optionalKey(Schema.String), |
| 16 | run_in_background: Schema.optionalKey(Schema.Boolean), |
| 17 | }), |
| 18 | ); |
| 19 | const decodeAsyncResponse = Schema.decodeUnknownOption( |
| 20 | Schema.Struct({ |
| 21 | claudeCode: Schema.Struct({ toolResponse: Schema.Struct({ isAsync: Schema.Boolean }) }), |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | export function isClaudeProvisionalSubagentTool(tool: AcpToolCallState): boolean { |
| 26 | return ( |
| 27 | (tool.data.name === "Agent" || tool.data.name === "Task") && |
| 28 | tool.status !== "failed" && |
| 29 | tool.status !== "completed" && |
| 30 | Option.isNone(decodeAgentInput(tool.data.rawInput)) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | export function extractClaudeSubagentUpdate( |
| 35 | tool: AcpToolCallState, |
| 36 | ): AcpAdapterV2SubagentUpdate | undefined { |
| 37 | if (tool.data.name !== "Agent" && tool.data.name !== "Task") return; |
| 38 | const input = decodeAgentInput(tool.data.rawInput); |
| 39 | if (Option.isNone(input)) return; |
| 40 | const response = decodeAsyncResponse(tool.data.meta); |
| 41 | const background = |
| 42 | input.value.run_in_background === true || |
| 43 | (Option.isSome(response) && response.value.claudeCode.toolResponse.isAsync); |
| 44 | return { |
| 45 | nativeTaskId: tool.toolCallId, |
| 46 | childSessionId: null, |
| 47 | prompt: input.value.prompt, |
| 48 | title: input.value.description ?? null, |
| 49 | model: input.value.model ?? null, |
| 50 | status: |
| 51 | tool.status === "failed" |
| 52 | ? "failed" |
| 53 | : tool.status === "completed" && !background |
| 54 | ? "completed" |
| 55 | : "running", |
| 56 | result: null, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | const decodeChildState = Schema.decodeUnknownOption( |
| 61 | Schema.Struct({ |
| 62 | sessionId: Schema.String, |
| 63 | state: Schema.Struct({ |
| 64 | state: Schema.String, |
| 65 | stopReason: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 66 | error: Schema.optionalKey(Schema.NullOr(Schema.Struct({ message: Schema.String }))), |
| 67 | }), |
| 68 | }), |
| 69 | ); |
| 70 | const decodeDelegation = Schema.decodeUnknownOption( |
| 71 | Schema.Struct({ |
| 72 | messageId: Schema.String, |
| 73 | senderSessionId: Schema.optionalKey(Schema.String), |
| 74 | content: Schema.Array( |
| 75 | Schema.Struct({ type: Schema.String, text: Schema.optionalKey(Schema.String) }), |
| 76 | ), |
| 77 | }), |
| 78 | ); |
| 79 | |
| 80 | // Claude's session_message id is the Agent/Task or SendMessage tool-use id. |
| 81 | export function extractClaudeSubagentSessionEvent( |
| 82 | notification: AcpSchema.SessionNotification, |
| 83 | ): AcpSubagentSessionEvent | undefined { |
| 84 | const update = notification.update; |
| 85 | if (update.sessionUpdate !== "_t3_unknown") return; |
| 86 | if (update.originalSessionUpdate === "subagent_update") { |
| 87 | const child = decodeChildState(update.raw); |
| 88 | if (Option.isNone(child)) return; |
| 89 | const { sessionId, state } = child.value; |
| 90 | const unfinishedMessage = acpUnfinishedStopMessage(state.stopReason); |
| 91 | return { |
| 92 | type: "state", |
| 93 | childSessionId: sessionId, |
| 94 | parentSessionId: notification.sessionId, |
| 95 | status: |
| 96 | state.state === "running" || state.state === "requires_action" |
| 97 | ? "running" |
| 98 | : state.stopReason === "error" || unfinishedMessage !== undefined |
| 99 | ? "failed" |
| 100 | : state.stopReason === "cancelled" || state.state === "unknown" |
| 101 | ? "cancelled" |
| 102 | : "completed", |
| 103 | result: state.error?.message ?? unfinishedMessage ?? null, |
| 104 | }; |
| 105 | } |
| 106 | if (update.originalSessionUpdate === "session_message") { |
| 107 | const delegation = decodeDelegation(update.raw); |
| 108 | if (Option.isNone(delegation)) return; |
| 109 | return { |
| 110 | type: "delegation", |
| 111 | childSessionId: notification.sessionId, |
| 112 | parentSessionId: delegation.value.senderSessionId ?? null, |
| 113 | delegationId: delegation.value.messageId, |
| 114 | prompt: delegation.value.content |
| 115 | .flatMap((block) => (block.type === "text" && block.text !== undefined ? [block.text] : [])) |
| 116 | .join("\n"), |
| 117 | }; |
| 118 | } |
| 119 | } |