All files / shared/src/types/ai-providers config.ts

100% Statements 8/8
100% Branches 0/0
100% Functions 0/0
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158                                  1x                                                                                                                                                                                                                                                                     1x 1x 1x 1x 1x 1x 1x      
import type {
  FunctionName,
  SuperAgentsRequestBody,
  SuperAgentsRequestData,
} from '@shared/types/api/request';
import type { SuperAgentsTarget } from '@shared/types/api/request/headers';
import type { Context } from 'hono';
 
// Use generic Context type to avoid circular dependency with API package
// biome-ignore lint/suspicious/noExplicitAny: Using any to avoid circular dep with AppEnv from API
type AppContext = Context<any>;
 
import type {
  ParameterConfig,
  SuperAgentsResponseBody,
} from '@shared/types/api/response/body';
import type { AIProvider } from '@shared/types/constants';
import { z } from 'zod';
import type { ProviderModelCapabilities } from './model-capabilities';
 
/**
 * Configuration for an AI provider.
 */
export interface AIProviderFunctionConfig {
  [key: string]: ParameterConfig | ParameterConfig[];
}
 
/**
 * Configuration for an AI provider's API.
 */
export interface InternalProviderAPIConfig {
  /** A function to generate the headers for the API request. */
  headers: (args: {
    c: AppContext;
    saTarget: SuperAgentsTarget;
    saRequestData: SuperAgentsRequestData;
  }) => Promise<Record<string, string>> | Record<string, string>;
  /** A function to generate the baseURL based on parameters */
  getBaseURL: (args: {
    c: AppContext;
    saTarget: SuperAgentsTarget;
    saRequestData: SuperAgentsRequestData;
  }) => Promise<string> | string;
  /** A function to generate the endpoint based on parameters */
  getEndpoint: (args: {
    c: AppContext;
    saTarget: SuperAgentsTarget;
    saRequestData: SuperAgentsRequestData;
  }) => string;
  /** A function to determine if the request body should be transformed to form data */
  transformToFormData?: (args: {
    saRequestData: SuperAgentsRequestData;
  }) => boolean;
  getProxyEndpoint?: (args: {
    saTarget: SuperAgentsTarget;
    reqPath: string;
    reqQuery: string;
  }) => string;
  customFieldsSchema?: z.ZodType;
  /** Whether an API key is required for this provider. Defaults to true if not specified. */
  isAPIKeyRequired?: boolean;
}
 
/**
 * A collection of API configurations for multiple AI providers.
 */
export interface InternalProviderAPIConfigs {
  /** The API configuration for each provider, indexed by provider name. */
  [key: string]: InternalProviderAPIConfig;
}
 
export type RequestHandlerFunction = (params: {
  c: AppContext;
  saTarget: SuperAgentsTarget;
  saRequestData: SuperAgentsRequestData;
}) => Promise<Response>;
 
export type CustomTransformer<T, U> = (response: T, isError?: boolean) => U;
 
export type FunctionNameToFunctionConfig = {
  [K in FunctionName]?: AIProviderFunctionConfig;
};
 
export type ResponseTransformFunction = (
  aiProviderResponseBody: Record<string, unknown>,
  aiProviderResponseStatus: number,
  aiProviderResponseHeaders: Headers,
  strictOpenAiCompliance: boolean,
  saRequestData: SuperAgentsRequestData,
) => SuperAgentsResponseBody;
 
export type StreamResponseTransformFunction = (
  aiProviderResponseBody: Record<string, unknown>,
  fallbackId: string,
  streamState: Record<string, unknown>,
  strictOpenAiCompliance: boolean,
) => string[];
 
export type ResponseChunkStreamTransformFunction = (
  responseChunk: string,
  fallbackId: string,
  streamState: Record<string, unknown>,
  strictOpenAiCompliance: boolean,
  saRequestData: SuperAgentsRequestData,
) => string | string[];
 
export type JSONToStreamGeneratorTransformFunction = (
  aiProviderResponseBody: Record<string, unknown>,
  provider: AIProvider,
) => Generator<string, void, unknown>;
 
export type ResponseTransformFunctionType =
  | ResponseTransformFunction
  | StreamResponseTransformFunction
  | JSONToStreamGeneratorTransformFunction
  | ResponseChunkStreamTransformFunction;
 
/**
 * Configuration structure for AI providers.
 *
 * If the key is not defined here, it should be a mapping of FunctionName to InternalProviderConfig.
 */
export interface AIProviderConfig extends FunctionNameToFunctionConfig {
  api: InternalProviderAPIConfig;
  getConfig?: (
    saRequestBody?:
      | SuperAgentsRequestBody
      | ReadableStream
      | FormData
      | ArrayBuffer,
  ) => AIProviderConfig;
  requestTransforms?: {
    [K in FunctionName]?: (body: ReadableStream) => ReadableStream;
  };
  requestHandlers?: {
    [key in FunctionName]?: RequestHandlerFunction;
  };
  responseTransforms?: {
    [K in FunctionName]?: ResponseTransformFunctionType;
  };
  forward_headers?: string[];
  /**
   * Model capability configuration for this provider.
   * Defines which parameters are supported by different models.
   */
  modelCapabilities?: ProviderModelCapabilities;
}
 
export const GatewayProvider = z.object({
  id: z.string(),
  name: z.string(),
  object: z.string(),
  description: z.string(),
  base_url: z.string(),
});
 
export type GatewayProvider = z.infer<typeof GatewayProvider>;