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 { createOpenAI } from '@ai-sdk/openai'; |
| 2 | import { defineAgent } from 'eve'; |
| 3 | import type { AgentModelDefinition, AgentModelOptionsDefinition } from 'eve'; |
| 4 | import { z } from 'zod'; |
| 5 | |
| 6 | const INCEPTION_BASE_URL = process.env.INCEPTION_BASE_URL ?? 'https://api.inceptionlabs.ai/v1'; |
| 7 | const INCEPTION_MODEL = process.env.INCEPTION_MODEL ?? 'mercury-2'; |
| 8 | const DOCS_AGENT_GATEWAY_MODEL = process.env.DOCS_AGENT_GATEWAY_MODEL ?? 'openai/gpt-5.4-mini'; |
| 9 | const DOCS_AGENT_MODEL_FLOW = process.env.DOCS_AGENT_MODEL_FLOW ?? 'mercury'; |
| 10 | |
| 11 | type DocsAgentModelConfig = { |
| 12 | model: AgentModelDefinition; |
| 13 | modelContextWindowTokens?: number; |
| 14 | modelOptions?: AgentModelOptionsDefinition; |
| 15 | }; |
| 16 | |
| 17 | const resolveInceptionApiKey = () => { |
| 18 | const apiKey = process.env.INCEPTION_API_KEY; |
| 19 | |
| 20 | if (!apiKey) { |
| 21 | throw new Error('INCEPTION_API_KEY is required to run the docs agent with Inception Mercury.'); |
| 22 | } |
| 23 | |
| 24 | return apiKey; |
| 25 | }; |
| 26 | |
| 27 | const INCEPTION_SAFE_ERROR_MESSAGE = 'Docs agent model request failed.'; |
| 28 | const INCEPTION_ERROR_RESPONSE_SCHEMA = z |
| 29 | .object({ |
| 30 | error: z.object({}).passthrough(), |
| 31 | }) |
| 32 | .passthrough(); |
| 33 | |
| 34 | const ABORT_LIKE_ERROR_SCHEMA = z |
| 35 | .object({ |
| 36 | name: z.enum(['AbortError', 'TimeoutError']), |
| 37 | }) |
| 38 | .passthrough(); |
| 39 | |
| 40 | const isAbortLikeError = (error: unknown) => ABORT_LIKE_ERROR_SCHEMA.safeParse(error).success; |
| 41 | |
| 42 | async function throwIfInceptionErrorPayload(response: Response): Promise<void> { |
| 43 | const contentType = response.headers.get('content-type')?.toLowerCase() ?? ''; |
| 44 | |
| 45 | if (!contentType.includes('application/json')) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | const body = await response |
| 50 | .clone() |
| 51 | .json() |
| 52 | .catch(() => null); |
| 53 | |
| 54 | if (INCEPTION_ERROR_RESPONSE_SCHEMA.safeParse(body).success) { |
| 55 | throw new Error(`${INCEPTION_SAFE_ERROR_MESSAGE} Upstream returned an error payload.`); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | export async function safeInceptionFetch( |
| 60 | input: RequestInfo | URL, |
| 61 | init?: RequestInit |
| 62 | ): Promise<Response> { |
| 63 | const headers = new Headers(init?.headers); |
| 64 | headers.set('Authorization', `Bearer ${resolveInceptionApiKey()}`); |
| 65 | |
| 66 | let response: Response; |
| 67 | try { |
| 68 | response = await fetch(input, { ...init, headers }); |
| 69 | } catch (error) { |
| 70 | if (isAbortLikeError(error)) { |
| 71 | throw error; |
| 72 | } |
| 73 | |
| 74 | throw new Error(INCEPTION_SAFE_ERROR_MESSAGE); |
| 75 | } |
| 76 | |
| 77 | if (!response.ok) { |
| 78 | throw new Error(`${INCEPTION_SAFE_ERROR_MESSAGE} Upstream status: ${response.status}.`); |
| 79 | } |
| 80 | |
| 81 | await throwIfInceptionErrorPayload(response); |
| 82 | |
| 83 | return response; |
| 84 | } |
| 85 | |
| 86 | const inception = createOpenAI({ |
| 87 | name: 'inception', |
| 88 | baseURL: INCEPTION_BASE_URL, |
| 89 | // The OpenAI-compatible AI SDK provider otherwise falls back to OPENAI_API_KEY |
| 90 | // when apiKey is undefined. Keep the actual Mercury key runtime-resolved so we |
| 91 | // never accidentally send an OpenAI key to Inception's endpoint. |
| 92 | apiKey: 'runtime-resolved-by-inception-fetch', |
| 93 | fetch: safeInceptionFetch, |
| 94 | }); |
| 95 | |
| 96 | type InceptionChatModel = ReturnType<typeof inception.chat>; |
| 97 | type InceptionStreamResult = Awaited<ReturnType<InceptionChatModel['doStream']>>; |
| 98 | type InceptionStreamPart = |
| 99 | InceptionStreamResult['stream'] extends ReadableStream<infer Part> ? Part : never; |
| 100 | |
| 101 | // AI SDK provider errors carry `requestBodyValues`, which holds the chat payload |
| 102 | // and therefore the system prompt. safeInceptionFetch cannot stop the ones the |
| 103 | // provider builds itself (e.g. an error frame inside a 200 SSE stream). |
| 104 | const sanitizeInceptionModelError = (error: unknown): unknown => |
| 105 | isAbortLikeError(error) ? error : new Error(INCEPTION_SAFE_ERROR_MESSAGE); |
| 106 | |
| 107 | export function withSanitizedModelErrors(model: InceptionChatModel): InceptionChatModel { |
| 108 | return { |
| 109 | specificationVersion: model.specificationVersion, |
| 110 | provider: model.provider, |
| 111 | modelId: model.modelId, |
| 112 | supportedUrls: model.supportedUrls, |
| 113 | async doGenerate(options) { |
| 114 | try { |
| 115 | return await model.doGenerate(options); |
| 116 | } catch (error) { |
| 117 | throw sanitizeInceptionModelError(error); |
| 118 | } |
| 119 | }, |
| 120 | async doStream(options) { |
| 121 | let result: InceptionStreamResult; |
| 122 | try { |
| 123 | result = await model.doStream(options); |
| 124 | } catch (error) { |
| 125 | throw sanitizeInceptionModelError(error); |
| 126 | } |
| 127 | |
| 128 | // Error parts that arrive after the first output chunk never pass through |
| 129 | // the provider's pre-output check, so sanitize the stream as well. |
| 130 | return { |
| 131 | ...result, |
| 132 | stream: result.stream.pipeThrough( |
| 133 | new TransformStream<InceptionStreamPart, InceptionStreamPart>({ |
| 134 | transform(part, controller) { |
| 135 | controller.enqueue( |
| 136 | part.type === 'error' |
| 137 | ? { type: 'error', error: sanitizeInceptionModelError(part.error) } |
| 138 | : part |
| 139 | ); |
| 140 | }, |
| 141 | }) |
| 142 | ), |
| 143 | }; |
| 144 | }, |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | export const createInceptionChatModel = (modelId: string = INCEPTION_MODEL): InceptionChatModel => |
| 149 | withSanitizedModelErrors(inception.chat(modelId)); |
| 150 | |
| 151 | const gatewayModel = (model: string): AgentModelDefinition => { |
| 152 | // Eve supports AI Gateway model id strings here. Cast until the published |
| 153 | // type catches up with the documented `defineAgent({ model: "provider/model" })` |
| 154 | // gateway path. |
| 155 | return model as unknown as AgentModelDefinition; |
| 156 | }; |
| 157 | |
| 158 | const resolveDocsAgentModel = (): DocsAgentModelConfig => { |
| 159 | switch (DOCS_AGENT_MODEL_FLOW) { |
| 160 | case 'gateway': |
| 161 | return { |
| 162 | model: gatewayModel(DOCS_AGENT_GATEWAY_MODEL), |
| 163 | }; |
| 164 | case 'mercury': |
| 165 | return { |
| 166 | // Use the chat-completions path because Mercury exposes tool calling there. |
| 167 | model: createInceptionChatModel(), |
| 168 | // Mercury 2's chat context window is 128K tokens. |
| 169 | modelContextWindowTokens: 128_000, |
| 170 | modelOptions: { |
| 171 | providerOptions: { |
| 172 | openai: { |
| 173 | // Mercury supports tool calling and the OpenAI-compatible adapter |
| 174 | // maps this to `reasoning_effort`. `medium` is Inception's |
| 175 | // recommended default and is accepted by the AI SDK OpenAI provider |
| 176 | // schema. |
| 177 | reasoningEffort: 'medium', |
| 178 | }, |
| 179 | }, |
| 180 | }, |
| 181 | }; |
| 182 | default: |
| 183 | throw new Error( |
| 184 | `Unsupported DOCS_AGENT_MODEL_FLOW "${DOCS_AGENT_MODEL_FLOW}". Expected "mercury" or "gateway".` |
| 185 | ); |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | const docsAgentModel = resolveDocsAgentModel(); |
| 190 | |
| 191 | /** |
| 192 | * Eve — the Composio docs assistant. |
| 193 | * |
| 194 | * Runs inside the docs Next.js app (mounted via `withEve` in next.config.mjs) |
| 195 | * and answers questions grounded in the docs. The `search_docs` tool returns |
| 196 | * doc snippets and their URLs so Eve can cite and link specific pages. |
| 197 | * |
| 198 | * Model: defaults to Inception Labs Mercury 2 diffusion model through the |
| 199 | * OpenAI-compatible chat endpoint. Set `INCEPTION_API_KEY` locally and in the |
| 200 | * preview environment. |
| 201 | * |
| 202 | * Evaluation / A-B knobs: |
| 203 | * |
| 204 | * - `DOCS_AGENT_MODEL_FLOW=mercury` (default) uses Inception Mercury. |
| 205 | * - `DOCS_AGENT_MODEL_FLOW=gateway` uses the Vercel AI Gateway fallback. |
| 206 | * - `INCEPTION_MODEL` (default: `mercury-2`) |
| 207 | * - `INCEPTION_BASE_URL` (default: `https://api.inceptionlabs.ai/v1`) |
| 208 | * - `DOCS_AGENT_GATEWAY_MODEL` (default: `openai/gpt-5.4-mini`) |
| 209 | */ |
| 210 | export default defineAgent({ |
| 211 | model: docsAgentModel.model, |
| 212 | modelContextWindowTokens: docsAgentModel.modelContextWindowTokens, |
| 213 | modelOptions: docsAgentModel.modelOptions, |
| 214 | }); |