| 1 | import type * as AcpSchema from "effect-acp/compat"; |
| 2 | import type { AcpToolCallState } from "@t3tools/provider-acp/server/runtimeModel"; |
| 3 | import type { AcpAdapterV2SubagentUpdate } from "@t3tools/provider-acp/server/adapter"; |
| 4 | |
| 5 | function record(value: unknown): Record<string, unknown> | undefined { |
| 6 | return typeof value === "object" && value !== null && !Array.isArray(value) |
| 7 | ? (value as Record<string, unknown>) |
| 8 | : undefined; |
| 9 | } |
| 10 | |
| 11 | /** Devin's typed extension metadata, captured from its native ACP stream. */ |
| 12 | export function normalizeDevinSessionUpdate( |
| 13 | notification: AcpSchema.SessionNotification, |
| 14 | ): AcpSchema.SessionNotification { |
| 15 | const update = notification.update; |
| 16 | const meta = record("_meta" in update ? update._meta : undefined); |
| 17 | const parentAgentId = record(meta?.["cognition.ai/subagent_context"])?.parentAgentId; |
| 18 | const routed = |
| 19 | typeof parentAgentId === "string" && parentAgentId !== "root" |
| 20 | ? { ...notification, sessionId: parentAgentId } |
| 21 | : notification; |
| 22 | const messageId = meta?.["cognition.ai/streamingMessageId"]; |
| 23 | if ( |
| 24 | (update.sessionUpdate === "agent_message_chunk" || |
| 25 | update.sessionUpdate === "agent_thought_chunk") && |
| 26 | typeof messageId === "string" |
| 27 | ) { |
| 28 | return { ...routed, update: { ...update, messageId } }; |
| 29 | } |
| 30 | return routed; |
| 31 | } |
| 32 | |
| 33 | export function normalizeDevinToolCall(tool: AcpToolCallState): AcpToolCallState { |
| 34 | const name = record(tool.data.meta)?.["cognition.ai/inferenceToolName"]; |
| 35 | return typeof name === "string" && (!tool.title || tool.title === "Tool") |
| 36 | ? { ...tool, title: name } |
| 37 | : tool; |
| 38 | } |
| 39 | |
| 40 | export function extractDevinSubagentUpdate( |
| 41 | tool: AcpToolCallState, |
| 42 | ): AcpAdapterV2SubagentUpdate | undefined { |
| 43 | const meta = record(tool.data.meta); |
| 44 | const completed = record(meta?.["cognition.ai/subagent_completed"]); |
| 45 | const started = record(meta?.["cognition.ai/subagent_started"]); |
| 46 | const data = completed ?? started; |
| 47 | if (typeof data?.agentId !== "string") return undefined; |
| 48 | return { |
| 49 | nativeTaskId: data.agentId, |
| 50 | childSessionId: data.agentId, |
| 51 | prompt: typeof started?.task === "string" ? started.task : "", |
| 52 | title: typeof started?.title === "string" ? started.title : null, |
| 53 | model: typeof started?.model === "string" ? started.model : null, |
| 54 | status: completed ? (completed.success === false ? "failed" : "completed") : "running", |
| 55 | result: typeof completed?.summary === "string" ? completed.summary : null, |
| 56 | }; |
| 57 | } |