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 | import { Effect, Schema } from 'effect'; |
| 2 | import type { RequestOptions } from '@composio/client/effect'; |
| 3 | import { HttpDecodingError } from './composio-errors'; |
| 4 | |
| 5 | // Maximum concurrent requests per each endpoint |
| 6 | export const MAX_CONCURRENT_REQUESTS_PER_ENDPOINT = 4; |
| 7 | |
| 8 | /** |
| 9 | * The client's default number of retries, which the CLI's clients turn off. |
| 10 | */ |
| 11 | const DEFAULT_MAX_RETRIES = 2; |
| 12 | |
| 13 | /** |
| 14 | * Request options for a call that is safe to repeat: a read, or a call whose |
| 15 | * repeat changes nothing on the server. The client retries such a call after a |
| 16 | * connection failure, a timeout, or a 408, 409, 429 or 5xx response. |
| 17 | * |
| 18 | * Every other call is sent once. A create, an update, a delete, or an execution |
| 19 | * that failed in flight may still have taken effect, and repeating it could |
| 20 | * repeat its side effect. |
| 21 | */ |
| 22 | export const idempotent = { maxRetries: DEFAULT_MAX_RETRIES } satisfies RequestOptions; |
| 23 | |
| 24 | /** |
| 25 | * Request options that make the client validate the response against its own |
| 26 | * schema for the operation, so a response in an unexpected shape fails the |
| 27 | * call with a `DecodeError` instead of surfacing later as a missing field. |
| 28 | * |
| 29 | * Pass it on a read whose fields the command uses through the client's types. |
| 30 | * Leave it off when the CLI decodes the response with a schema it owns, when |
| 31 | * the endpoint is read leniently on purpose, and on every call that is not a |
| 32 | * read. Combine it with {@link idempotent}: `{ ...idempotent, ...validated }`. |
| 33 | */ |
| 34 | export const validated = { decode: true } satisfies RequestOptions; |
| 35 | |
| 36 | /** |
| 37 | * Decodes a payload against a CLI-owned contract. Pass the client's name for |
| 38 | * the request's `operation` to have a mismatch reported by that name. |
| 39 | */ |
| 40 | export const decode = |
| 41 | <S extends Schema.Top>(schema: S, options?: { readonly operation?: string }) => |
| 42 | (input: unknown): Effect.Effect<S['Type'], HttpDecodingError, S['DecodingServices']> => |
| 43 | Schema.decodeUnknownEffect(schema)(input).pipe( |
| 44 | Effect.catchTag( |
| 45 | 'SchemaError', |
| 46 | cause => new HttpDecodingError({ cause, operation: options?.operation }) |
| 47 | ) |
| 48 | ); |