import {z} from 'zod' export type ConnectionsConnectionAuthMode = 'USER' | 'ORG_WIDE'; export type ConnectionsAppProviderConfigurations = {googleads_developer_token?: string | undefined, microsoft_tenant_id?: string | undefined, provider_domain?: string | undefined, slack_connection_type?: ('BOT' | 'USER') | undefined, smart_fhir_aud?: string | undefined, smart_fhir_domain?: string | undefined, trello_app_name?: string | undefined}; export type V1connectionsCreateAppConnection = {auth_mode?: ConnectionsConnectionAuthMode | undefined, connection_name?: string | undefined, provider?: string | undefined, provider_configurations?: ConnectionsAppProviderConfigurations | undefined}; export type CreateApiApiV1AgentkitConnectionArgs = {body: V1connectionsCreateAppConnection}; export type CreateApiApiV1AgentkitConnectionOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const connectionsConnectionAuthMode = z.enum(["USER", "ORG_WIDE"]); export const connectionsGoogleDWDConfig = z.object({required_scopes: z.array(z.string()).optional(), scope_selection_allowed: z.boolean().optional(), scopes: z.array(z.string()).optional(), service_account_json: z.string().optional(), token_uri: z.string().optional()}); export const connectionsOptionalScopes = z.object({field_name: z.string().optional(), scopes: z.array(z.string()).optional()}); export const appOAuthConfigTokenEndpointAuthMethod = z.enum(["NONE", "BASIC_AUTH", "REQUEST_BODY", "DEFAULT"]); export const connectionsAppOAuthConfig = z.object({access_type: z.string().optional(), authorize_uri: z.string().optional(), client_id: z.string().optional(), client_secret: z.string().optional(), extra_authorize_params: z.record(z.string(), z.string()).optional(), optional_scopes: connectionsOptionalScopes.optional(), pkce_enabled: z.boolean().optional(), prompt: z.string().optional(), redirect_uri: z.string().optional(), required_optional_scopes: z.array(z.string()).optional(), required_scopes: z.array(z.string()).optional(), scope_selection_allowed: z.boolean().optional(), scopes: z.array(z.string()).optional(), token_access_type: z.string().optional(), token_endpoint_auth_method: appOAuthConfigTokenEndpointAuthMethod.optional(), token_uri: z.string().optional(), use_scalekit_creds: z.boolean().optional(), user_info_uri: z.string().optional()}); export const connectionsAppProviderConfigurations = z.object({googleads_developer_token: z.string().optional(), microsoft_tenant_id: z.string().optional(), provider_domain: z.string().optional(), slack_connection_type: z.enum(["BOT", "USER"]).optional(), smart_fhir_aud: z.string().optional(), smart_fhir_domain: z.string().optional(), trello_app_name: z.string().optional()}); export const connectionsConnectionType = z.enum(["OIDC", "SAML", "PASSWORD", "OAUTH", "PASSWORDLESS", "BASIC", "BEARER", "API_KEY", "WEBAUTHN", "OAUTH_M2M", "TRELLO_OAUTH1", "GOOGLE_DWD", "TRUSTED_IDP", "SMART_FHIR", "NO_AUTH"]); export const connectionsAppConnection = z.object({auth_mode: connectionsConnectionAuthMode.optional(), connection_mcp_url: z.string().optional(), connection_name: z.string().optional(), create_time: z.string().optional(), google_dwd_config: connectionsGoogleDWDConfig.optional(), id: z.string().optional(), oauth_config: connectionsAppOAuthConfig.optional(), provider: z.string().optional(), provider_configurations: connectionsAppProviderConfigurations.optional(), type: connectionsConnectionType.optional(), update_time: z.string().optional()}); export const connectionsCreateAppConnectionResponse = z.object({connection: connectionsAppConnection.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a connection * * POST /api/v1/agentkit/connection */ export const createApiApiV1AgentkitConnection = async (args: CreateApiApiV1AgentkitConnectionArgs, options?: CreateApiApiV1AgentkitConnectionOptions) => { const {baseUrl = "https://$SCALEKIT_ENVIRONMENT_URL", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/agentkit/connection${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(connectionsCreateAppConnectionResponse, await res.json()) };