(result: unknown, callerContext: unknown)
| 150 | // ── Response shaping ───────────────────────────────────────────── |
| 151 | |
| 152 | function toAdaptedResponse(result: unknown, callerContext: unknown): AdaptedResponse { |
| 153 | const errsField = (result as { errors?: unknown[] } | null | undefined)?.errors; |
| 154 | if (Array.isArray(errsField) && errsField.length > 0) { |
| 155 | const first = errsField[0] as InlineError; |
| 156 | const errorObj: Record<string, unknown> = { code: first.code, message: first.message }; |
| 157 | if (first.field) errorObj.field = first.field; |
| 158 | if (first.details !== undefined) errorObj.details = first.details; |
| 159 | if (first.recovery) errorObj.recovery = first.recovery; |
| 160 | const body = wrapEnvelope({ adcp_error: errorObj }, { context: callerContext }); |
| 161 | return { |
| 162 | isError: true, |
| 163 | content: [{ type: 'text', text: JSON.stringify(body) }], |
| 164 | structuredContent: body, |
| 165 | }; |
| 166 | } |
| 167 | const inner = (result ?? {}) as Record<string, unknown>; |
| 168 | // wrapEnvelope stamps the AdCP context-echo envelope. `replayed` is |
| 169 | // intentionally NOT set here — per protocol-envelope.json and the |
| 170 | // SDK's injectReplayed helper, fresh executions MUST omit the field |
| 171 | // (the framework stamps `replayed: true` only on idempotency replays). |
| 172 | const withEnvelope = wrapEnvelope(inner, { |
| 173 | ...(callerContext !== undefined && typeof callerContext === 'object' && callerContext !== null |
| 174 | ? { context: callerContext } |
| 175 | : {}), |
| 176 | }); |
| 177 | const response = withEnvelope as Record<string, unknown>; |
| 178 | return { |
| 179 | content: [{ type: 'text', text: JSON.stringify(response) }], |
| 180 | structuredContent: response, |
| 181 | }; |
| 182 | } |
| 183 | |
| 184 | function serviceUnavailable(err: unknown, callerContext: unknown): AdaptedResponse { |
| 185 | const errorObj: Record<string, unknown> = { |
no outgoing calls
no test coverage detected