Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Fault injection: a fault replaces or distorts the answer of one route. |
| 3 | * |
| 4 | * A fault is addressed to a route name (`toolRouter.session.execute`) or to a |
| 5 | * `METHOD /path` pattern, and applies to the next `times` matching requests. |
| 6 | */ |
| 7 | |
| 8 | export type Fault = |
| 9 | /** Answer with this status and the standard error envelope (or `body`). */ |
| 10 | | { |
| 11 | readonly kind: 'status'; |
| 12 | readonly status: number; |
| 13 | readonly slug?: string; |
| 14 | readonly message?: string; |
| 15 | readonly code?: number; |
| 16 | readonly headers?: Readonly<Record<string, string>>; |
| 17 | readonly body?: unknown; |
| 18 | } |
| 19 | /** Hold the answer back for `ms` before sending it unchanged. */ |
| 20 | | { readonly kind: 'delay'; readonly ms: number } |
| 21 | /** Send the headers and the first bytes of the body, then drop the connection. */ |
| 22 | | { readonly kind: 'reset-mid-body' } |
| 23 | /** Accept the request and drop the connection without answering. */ |
| 24 | | { readonly kind: 'reset' } |
| 25 | /** 200 with a body that is not JSON. */ |
| 26 | | { readonly kind: 'malformed-json' } |
| 27 | /** 200 with valid JSON whose shape the CLI does not expect. */ |
| 28 | | { readonly kind: 'schema-drift'; readonly transform: (body: unknown) => unknown } |
| 29 | /** An HTML page, as a proxy or captive portal would answer. */ |
| 30 | | { readonly kind: 'html'; readonly status?: number } |
| 31 | /** 200 with an empty body. */ |
| 32 | | { readonly kind: 'empty-body' }; |
| 33 | |
| 34 | export interface FaultTarget { |
| 35 | /** A route name, or a pattern tested against `METHOD /path`. */ |
| 36 | readonly route: string | RegExp; |
| 37 | readonly fault: Fault; |
| 38 | /** How many matching requests the fault applies to. Defaults to every one. */ |
| 39 | readonly times?: number; |
| 40 | } |
| 41 | |
| 42 | interface ArmedFault { |
| 43 | readonly target: FaultTarget; |
| 44 | remaining: number; |
| 45 | } |
| 46 | |
| 47 | export class FaultController { |
| 48 | private armed: ArmedFault[] = []; |
| 49 | |
| 50 | /** Arms a fault and returns a function that disarms it. */ |
| 51 | inject(target: FaultTarget): () => void { |
| 52 | const entry: ArmedFault = { target, remaining: target.times ?? Number.POSITIVE_INFINITY }; |
| 53 | this.armed.push(entry); |
| 54 | return () => { |
| 55 | this.armed = this.armed.filter(candidate => candidate !== entry); |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | clear(): void { |
| 60 | this.armed = []; |
| 61 | } |
| 62 | |
| 63 | /** The fault for this request, consuming one of its uses. */ |
| 64 | take(route: string | null, method: string, path: string): Fault | undefined { |
| 65 | const line = `${method} ${path}`; |
| 66 | const match = this.armed.find(({ target, remaining }) => { |
| 67 | if (remaining <= 0) return false; |
| 68 | return typeof target.route === 'string' ? target.route === route : target.route.test(line); |
| 69 | }); |
| 70 | if (!match) return undefined; |
| 71 | match.remaining -= 1; |
| 72 | return match.target.fault; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | export const describeFault = (fault: Fault): string => |
| 77 | fault.kind === 'status' ? `status:${fault.status}` : fault.kind; |
| 78 | |
| 79 | /** Fixed, so an error path prints the same request id on every run. */ |
| 80 | export const FAULT_REQUEST_ID = 'req_e2e_fault_0001'; |
| 81 | |
| 82 | export const errorEnvelope = (params: { |
| 83 | status: number; |
| 84 | message: string; |
| 85 | slug: string; |
| 86 | code?: number; |
| 87 | suggestedFix?: string; |
| 88 | }) => ({ |
| 89 | error: { |
| 90 | message: params.message, |
| 91 | code: params.code ?? params.status, |
| 92 | slug: params.slug, |
| 93 | status: params.status, |
| 94 | request_id: FAULT_REQUEST_ID, |
| 95 | suggested_fix: params.suggestedFix ?? '', |
| 96 | }, |
| 97 | }); |
| 98 | |
| 99 | export const HTML_ERROR_PAGE = |
| 100 | '<!DOCTYPE html><html><head><title>502 Bad Gateway</title></head>' + |
| 101 | '<body><h1>502 Bad Gateway</h1><p>The upstream server is unreachable.</p></body></html>\n'; |
| 102 | |
| 103 | /** A body no JSON parser accepts: a response cut off inside a string. */ |
| 104 | export const MALFORMED_JSON_BODY = '{"items": [{"slug": "trunc'; |