UNPKG

@brian-ai/sdk

Version:

Official Typescript SDK for Brian AI.

591 lines (586 loc) 21 kB
/** * @dev Extracted action. */ type Action = "swap" | "transfer" | "bridge" | "balance" | "wrap native" | "unwrap native" | "totalsupply" | "approve" | "deposit" | "stake on Lido" | "withdraw" | "ENS Forward Resolution" | "ENS Reverse Resolution" | "ENS Availability" | "ENS Expiration" | "ENS Registration Cost" | "ENS Renewal Cost" | "ENS Registration" | "ENS Renewal" | "AAVE Borrow" | "AAVE Repay" | "Aave User Data" | "Find protocol" | "deploytoken" | "claim" | "vote"; /** * @dev Brian chat message type. * @property {"user" | "brian"} sender - The sender of the message. * @property {string} content - The content of the message. */ type Message = { sender: "user" | "brian"; content: string; }; /** * @dev Token type. * @property {string} address - The address of the token. * @property {number} chainId - The chain ID of the token. * @property {string} symbol - The symbol of the token. * @property {number} decimals - The decimals of the token. * @property {string} name - The name of the token. * @property {string} coinKey - The coin key of the token. * @property {string} logoURI - The logo URI of the token. * @property {string} priceUSD - The price of the token in USD. */ type Token = { address: `0x${string}`; chainId: number; symbol: string; decimals: number; name: string; coinKey?: string; logoURI?: string; priceUSD?: string; }; /** * @dev Brian SDK options type. * @property {string} apiUrl - The API URL for the Brian API. * @property {string} apiKey - The API Key for the Brian API. * @property {string} apiVersion - The API Version for the Brian API. */ type BrianSDKOptions = { apiUrl?: string; apiKey: string; apiVersion?: "v0"; }; type PromptRequestBody = { prompt: string; }; /** * @dev Request body sent to /agent/knowledge. * @property {string} prompt - The prompt to send to the Brian API. * @property {string} kb - The knowledge base to use for the prompt. */ type AskRequestBody = PromptRequestBody & { kb: string; }; /** * @dev Response body received from /agent/knowledge. * @property {AskResult} result - The result from the Brian API. */ type AskResponse = { result: AskResult; }; /** * @dev The result from the Brian API. * @property {string} answer - AI-generated text. * @property {string} input - The input prompt. * @property {SourceDocument[]} context - The source documents used to generate the text. */ type AskResult = { answer: string; input: string; context: SourceDocument[]; }; /** * @dev The source document used to generate the text. * @property {string} pageContent - The page content of the source document. * @property {SourceDocumentMetadata} metadata - The metadata of the source document. */ type SourceDocument = { pageContent: string; metadata: SourceDocumentMetadata; }; /** * @dev The metadata of the source document. * @property {string} description - The description of the document. * @property {string} language - The language of the document. * @property {string} source - The source of the document. * @property {string} title - The title of the document. */ type SourceDocumentMetadata = { description: string; language: string; source: string; title: string; }; /** * @dev Request body sent to /agent/parameters-extraction. * @property {string} prompt - The prompt to send to the Brian API. */ type ExtractParametersRequestBody = PromptRequestBody; /** * @dev Response body received from /agent/parameters-extraction. * @property {ExtractParametersResult} result - The result from the Brian API. */ type ExtractParametersResponse = { result: ExtractParametersResult; }; /** * @dev The result from the Brian API. * @property {string} prompt - The prompt that was send to the API. * @property {Completion[]} completion - The completion or array of completions from the Brian API. */ type ExtractParametersResult = { prompt: string; completion: Completion[]; }; /** * @dev Completion from the Brian API. * @property {Action} action - The action extracted from the prompt. * @property {string} token1 - The first token extracted from the prompt. * @property {string} token2 - The second token extracted from the prompt. * @property {string} chain - The chain extracted from the prompt. * @property {string} address - The address extracted from the prompt. * @property {string} amount - The amount extracted from the prompt. * @property {string} dest_chain - The destination chain extracted from the prompt. */ type Completion = { action: Action; token1?: string; token2?: string; chain?: string; address?: string; amount?: string; dest_chain?: string; primaryAddress?: string; owner?: string; supply?: string; withdrawpercentage?: string; repaypercentage?: string; protocol?: string; duration?: string; interest?: string; apy?: string; liquidity?: string; name?: string; symbol?: string; uri?: string; tag?: string; prompt?: string; }; /** * @dev Request body sent to /agent/smart-contracts. */ type GenerateCodeRequestBody = PromptRequestBody & { messages?: ContextMessage[]; compile?: boolean; }; /** * @dev Response body received from /agent/smart-contracts. * @property {string} result - The result from the Brian API. */ type GenerateCodeResponse = { result: { contract: string; contractName: string; abi: any; bytecode: `0x${string}`; standardJsonInput: string; version: string; }; }; type GenerateCodeResult = { contract: string; contractName: string; abi: any; bytecode: `0x${string}`; standardJsonInput: string; version: string; }; /** * @dev Request body sent to /agent/transaction. * @property {string} prompt - The prompt to send to the Brian API. * @property {string} address - The address to send the transaction from. * @property {`${number}`} chainId - The chain ID to send the transaction from (optional). */ type TransactionRequestBody = PromptRequestBody & { address: string; chainId?: `${number}`; }; /** * @dev Response body received from /agent/transaction. * @property {TransactionResult[]} result - The results from the Brian API. */ type TransactionResponse = { result: TransactionResult[]; }; type TransactionResult = { type: "read" | "write"; action: Action; data: TransactionData; solver: string; }; /** * @dev Transaction data. * @property {TransactionStep[]} steps - The steps of the transaction. * @property {string} description - The description of the transaction. * @property {number} fromChainId - The chain ID to send the transaction from. * @property {string} fromAmount - The amount to send from the transaction. * @property {Token} fromToken - The token to send from the transaction. * @property {string} fromAddress - The address to send from the transaction. * @property {number} toChainId - The chain ID to send the transaction to. * @property {string} toAmount - The amount to send to the transaction. * @property {string} toAmountMin - The minimum amount to send to the transaction. * @property {Token} toToken - The token to send to the transaction. * @property {string} toAddress - The address to send to the transaction. */ type TransactionData = { description: string; steps?: TransactionStep[]; fromChainId?: number; fromAmount?: `${number}`; fromToken?: Token; fromAddress?: `0x${string}`; toChainId?: number; toAmount?: `${number}`; toAmountMin?: `${number}`; toToken?: Token; toAddress?: `0x${string}`; }; /** * @dev Transaction step. * @property {number} chainId - The chain ID of the transaction step. * @property {number} blockNumber - The block number of the transaction step. * @property {string} from - The from address of the transaction step. * @property {string} to - The to address of the transaction step. * @property {string} value - The value of the transaction step. * @property {string} data - The data of the transaction step. */ type TransactionStep = { chainId: number; blockNumber?: number; from: `0x${string}`; to: `0x${string}`; value: `${number}`; data: `0x${string}`; }; /** * @dev Request body sent to /utils/explain. * @property {string} prompt - The prompt to send to the Brian API. */ type ExplainRequestBody = PromptRequestBody; /** * @dev Response body received from /utils/explain. * @property {string | null} result - The result from the Brian API. */ type ExplainResponse = { result: string | null; }; /** * @dev Request body sent to /utils/compile. * @property {string} prompt - The prompt to send to the Brian API. * @property {string} contractName - The name of the contract to compile. */ type CompileRequestBody = PromptRequestBody & { contractName: string; }; /** * @dev Response body received from /utils/compile. * @property {any} abi - The ABI of the contract. * @property {string} bytecode - The bytecode of the contract. * @property {string} fileName - The file name of the contract. */ type CompileResponse = { abi: any; bytecode: `0x${string}`; fileName: string; }; /** * @dev Context message type. * @property {"user" | "agent" | "system"} role - The role of the message. * @property {string} content - The content of the message. */ type ContextMessage = { role: "user" | "agent" | "system"; content: string; }; /** * @dev Knowledge base type. * @property {number} id - The ID of the knowledge base. * @property {string} name - The name of the knowledge base. * @property {string} description - The description of the knowledge base. * @property {string} slug - The slug of the knowledge base. * @property {string} createdAt - The creation date of the knowledge base. * @property {KnowledgeBaseResource[]} resources - The resources of the knowledge base. */ type KnowledgeBase = { id: number; name: string; description: string; slug: string; createdAt: string; resources: KnowledgeBaseResource[]; }; /** * @dev Request body sent to /knowledge-bases. * @property {string} name - The name of the knowledge base. * @property {string} description - The description of the knowledge base. */ type CreateKnowledgeBaseBody = { name: string; description: string; }; /** * @dev Response body received from /knowledge-bases. * @property {KnowledgeBase} result - The result from the Brian API. */ type CreateKnowledgeBaseResponse = { result: KnowledgeBase; }; /** * @dev Knowledge base resource type. * @property {number} id - The ID of the knowledge base resource. * @property {string} name - The name of the knowledge base resource. * @property {number} size - The size of the knowledge base resource. * @property {number} knowledgeBaseId - The ID of the knowledge base. * @property {string} createdAt - The creation date of the knowledge base resource. */ type KnowledgeBaseResource = { id: number; name: string; size: number; knowledgeBaseId: number; createdAt: string; }; /** * @dev Response body received from /knowledge-bases. * @property {KnowledgeBase[]} result - The result from the Brian API. */ type GetKnowledgeBasesResponse = { result: KnowledgeBase[]; }; /** * @dev Response body received from /knowledge-bases/:id. * @property {KnowledgeBase} result - The result from the Brian API. */ type GetKnowledgeBaseResponse = { result: KnowledgeBase; }; /** * @dev Request body sent to /knowledge-bases/:id/resources. * @property {"ok"} result - Result of the deletion operation. */ type DeleteKnowledgeBoxResponse = { result: "ok"; }; /** * @dev Request body sent to /knowledge-bases/:id/resources. * @property {string} name - The name of the knowledge base resource. * @property {string} content - The content of the knowledge base resource. * @property {string} source - The source of the knowledge base resource. */ type CreateKnowledgeBaseResourceBody = { name: string; content: string; source: string; }; /** * @dev Network currency type. * @property {string} name - The name of the network currency. * @property {string} symbol - The symbol of the network currency. * @property {number} decimals - The decimals of the network currency. */ type NetworkCurrency = { name: string; symbol: string; decimals: number; }; /** * @dev Network object. * @property {number} id - The ID of the network. * @property {string} name - The name of the network. * @property {NetworkCurrency} nativeCurrency - The native currency of the network. */ type Network = { id: number; name: string; nativeCurrency: NetworkCurrency; }; /** * @dev Response body received from /utils/networks. * @property {Network[]} result - The result from the Brian API. */ type NetworksResponse = { result: Network[]; }; /** * @dev Request body sent to /agent. * @property {string} prompt - The prompt to send to the Brian API. * @property {string} address - The address to send the transaction from. * @property {string} chainId - The chain ID to send the transaction from. * @property {Message[]} messages - The chat context. */ type ChatRequestBody = TransactionRequestBody & { messages: Message[]; }; /** * @dev Response body received from /agent in case of error. * @property {string} error - The error message. */ type ChatMissingParameterResponse = { error: string; }; /** @dev Chat response type. */ type ChatResponse = TransactionResponse | AskResponse; /** * @dev BrianKnowledgeBaseSDK is the class for interacting with the Brian Knowledge Base API. */ declare class BrianKnowledgeBaseSDK { private apiUrl; private apiVersion; private options; constructor({ apiUrl, apiKey, apiVersion, }: BrianSDKOptions); /** * @dev Creates a new knowledge base. * @param {CreateKnowledgeBaseBody} body - The body for creating a new knowledge base. * @returns {Promise<KnowledgeBase>} The created knowledge base. */ createKnowledgeBase({ name, description, }: CreateKnowledgeBaseBody): Promise<KnowledgeBase>; /** * @dev Gets all knowledge bases associated with the given API key. * @returns {Promise<KnowledgeBase[]>} The knowledge bases associated with the given API key. */ getKnowledgeBoxes(): Promise<KnowledgeBase[]>; /** * @dev Gets a knowledge base by its ID. * @param {number} kbId - The ID of the knowledge base. * @returns {Promise<KnowledgeBase>} The knowledge base with the given ID. */ getKnowledgeBox(kbId: number): Promise<KnowledgeBase>; /** * @dev Deletes a knowledge box by its ID. * @param {number} kbId - The ID of the knowledge box. * @returns {Promise<string>} "ok" if everything went smoothly. */ deleteKnowledgeBox(kbId: number): Promise<string>; } /** * @dev BrianSDK is the main class for interacting with the Brian API. */ declare class BrianSDK { private apiUrl; private apiVersion; private options; kb: BrianKnowledgeBaseSDK; /** * @dev The constructor for the BrianSDK class. * @param {BrianSDKOptions} options - The options for initializing SDK. */ constructor({ apiUrl, apiKey, apiVersion, }: BrianSDKOptions); /** * @dev Chat with the Brian agent via API. * @param {ChatRequestBody} body - The request body sent to the Brian API. * @returns {Promise<TransactionResult[] | AskResult | ChatMissingParameterResponse>} - result of the chat with Brian. */ chat(body: ChatRequestBody): Promise<TransactionResult[] | AskResult | ChatMissingParameterResponse>; /** * @dev Asks the Brian API a question about documents or links. * @param {AskRequestBody} body - The request body sent to the Brian API. * @returns {Promise<AskResult>} The result from the Brian API. */ ask(body: AskRequestBody): Promise<AskResult>; /** * @dev Extracts parameters from a given text. * @param {ExtractParametersRequestBody} body - The request body sent to the Brian API. * @returns {Promise<ExtractParametersResult>} The result from the Brian API. */ extract(body: ExtractParametersRequestBody): Promise<ExtractParametersResult>; /** * @dev Generates code from a given text. * @param {GenerateCodeRequestBody} body - The request body sent to the Brian API. * @param {boolean} removeMarkdown - Whether to remove markdown from the generated code. * @returns {Promise<string>} The result from the Brian API. */ generateCode(body: GenerateCodeRequestBody, removeMarkdown?: boolean): Promise<GenerateCodeResult>; /** * @dev Asks Brian to build one or more transactions. * @param {TransactionRequestBody} body - The request body sent to the Brian API. * @returns {TransactionResult[]} The result from the Brian API. */ transact(body: TransactionRequestBody): Promise<TransactionResult[]>; /** * @dev Compiles a given code. * @param {CompileRequestBody} body - The request body sent to the Brian API. * @returns {CompileResponse} The result from the Brian API. */ compile(body: CompileRequestBody): Promise<CompileResponse>; /** * @dev Explains a given code. * @param {ExplainRequestBody} body - The request body sent to the Brian API. * @returns {string | null} The result from the Brian API. */ explain(body: ExplainRequestBody): Promise<string | null>; /** * @dev Returns all the networks supported by the Brian API. * @returns {Network[]} array of networks supported by the Brian API. */ getNetworks(): Promise<Network[]>; } type ErrorName = "SDKInitializationError" | "BadRequestError" | "RateLimitError" | "InternalServerError" | "NotFoundError"; /** * @dev Generic class for Brian SDK errors. * @property {string} name - The name of the error. * @property {string} message - The message of the error. * @property {any} cause - The cause of the error. */ declare class BrianSDKError extends Error { name: ErrorName; message: string; cause: any; /** * @dev The constructor for the SDKInitializationError class. */ constructor({ name, message, cause, }: { name: ErrorName; message: string; cause?: any; }); } /** * @dev SDKInitializationError is the error thrown when the SDK is not initialized correctly. */ declare class SDKInitializationError extends BrianSDKError { /** * @dev The constructor for the RateLimitError class. */ constructor({ cause, message }: { message: string; cause?: any; }); } /** * @dev BadRequestError is the error thrown when the API receives a bad request. */ declare class BadRequestError extends BrianSDKError { /** * @dev The constructor for the BadRequestError class. */ constructor({ cause }: { cause?: any; }); } /** * @dev NotFoundError is the error thrown when the API receives a request for a non-existing entity. */ declare class NotFoundError extends BrianSDKError { /** * @dev The constructor for the NotFoundError class. */ constructor({ cause }: { cause?: any; }); } /** * @dev RateLimitError is the error thrown when the API is rate limited. */ declare class RateLimitError extends BrianSDKError { /** * @dev The constructor for the RateLimitError class. */ constructor({ cause }: { cause?: any; }); } /** * @dev InternalServerError is the error thrown when the API encounters an internal server error. */ declare class InternalServerError extends BrianSDKError { /** * @dev The constructor for the InternalServerError class. */ constructor({ cause }: { cause?: any; }); } export { type AskRequestBody, type AskResponse, type AskResult, BadRequestError, BrianSDK, BrianSDKError, type BrianSDKOptions, type ChatMissingParameterResponse, type ChatRequestBody, type ChatResponse, type CompileRequestBody, type CompileResponse, type Completion, type ContextMessage, type CreateKnowledgeBaseBody, type CreateKnowledgeBaseResourceBody, type CreateKnowledgeBaseResponse, type DeleteKnowledgeBoxResponse, type ExplainRequestBody, type ExplainResponse, type ExtractParametersRequestBody, type ExtractParametersResponse, type ExtractParametersResult, type GenerateCodeRequestBody, type GenerateCodeResponse, type GenerateCodeResult, type GetKnowledgeBaseResponse, type GetKnowledgeBasesResponse, InternalServerError, type KnowledgeBase, type KnowledgeBaseResource, type Message, type Network, type NetworkCurrency, type NetworksResponse, NotFoundError, RateLimitError, SDKInitializationError, type SourceDocument, type SourceDocumentMetadata, type Token, type TransactionData, type TransactionRequestBody, type TransactionResponse, type TransactionResult, type TransactionStep };