bitte-ai-chat
Version:
Bitte AI chat component
275 lines (268 loc) • 8.27 kB
text/typescript
import * as react_jsx_runtime from 'react/jsx-runtime';
import { ReactNode } from 'react';
import { Transaction, FunctionCallAction, TransferAction, Wallet } from '@near-wallet-selector/core';
import { JSONValue, CoreTool, CoreMessage, Message } from 'ai';
import { AssistantTool, FunctionTool } from 'openai/resources/beta/assistants';
import { FunctionDefinition } from 'openai/resources/index';
import { OpenAPIV3 } from 'openapi-types';
import BN from 'bn.js';
import { Account } from 'near-api-js/lib/account';
import { Hex } from 'viem';
import { SafeEncodedSignRequest } from 'near-safe';
declare enum BittePrimitiveName {
TRANSFER_FT = "transfer-ft",
GENERATE_TRANSACTION = "generate-transaction",
SUBMIT_QUERY = "submit-query",
GENERATE_IMAGE = "generate-image",
CREATE_DROP = "create-drop",
GET_SWAP_TRANSACTIONS = "getSwapTransactions",
GET_TOKEN_METADATA = "getTokenMetadata",
GENERATE_EVM_TX = "generate-evm-tx"
}
type TransactionOperation = {
operation: Operation;
sponsorId?: string;
paymasterId?: string;
cost?: string;
fee?: string;
};
declare enum Operation {
NEAR = "near",
RELAY = "relay",
SPONSOR = "sponsor"
}
type BitteMetadata = {
[key: string]: unknown;
};
type BittePrimitiveRef = {
type: string;
};
type BitteOpenAPISpec = OpenAPIV3.Document & {
"x-mb": {
"account-id": string;
assistant?: Pick<BitteAssistantConfig, "name" | "description" | "instructions" | "tools" | "image"> & {
tools?: (AssistantTool | BittePrimitiveRef)[];
};
};
};
type ExecutionDefinition = {
baseUrl: string;
path: string;
httpMethod: string;
};
type PluginToolSpec = {
id: string;
agentId: string;
type: "function";
function: FunctionDefinition;
execution: ExecutionDefinition;
verified: boolean;
};
type BitteToolSpec = PluginToolSpec | FunctionTool;
type BitteToolWarning = {
message: string;
final: boolean;
};
type BitteToolResult<TResult = unknown> = {
data: TResult | null;
warnings: BitteToolWarning[] | null;
error: Error | null;
};
type BitteToolExecutor<TArgs = Record<string, JSONValue>, TResult = unknown> = (args: TArgs, metadata?: BitteMetadata) => Promise<BitteToolResult<TResult>>;
type BitteToolRenderer<TArgs = unknown> = (args: TArgs, metadata?: BitteMetadata) => ReactNode | null;
type BitteTool<TArgs = Record<string, JSONValue>, TResult = unknown> = {
toolSpec: FunctionTool;
execute: BitteToolExecutor<TArgs, TResult>;
render?: BitteToolRenderer;
};
type AnyBitteTool = BitteTool<any>;
type BitteAssistantConfig = {
id: string;
name: string;
accountId: string;
description: string;
instructions: string;
verified: boolean;
tools?: BitteToolSpec[];
image?: string;
};
type BitteAssistant = Omit<BitteAssistantConfig, "tools"> & {
toolSpecs?: FunctionTool[];
tools?: Record<string, CoreTool>;
};
type FunctionDataMessage = {
functionName: BittePrimitiveName | string;
isPrimitive: boolean;
description?: string;
value: JSONValue;
};
type SmartAction = {
id: string;
agentId: string;
message: string;
creator: string;
createdAt: number;
};
type SmartActionMessage = CoreMessage & {
id?: string;
agentId?: string;
};
type SmartActionAiMessage = Message & {
id?: string;
agentId?: string;
};
type SmartActionChat = SmartAction & {
messages: SmartActionMessage[];
};
type SaveSmartAction = {
agentId: string;
creator: string;
message: string;
};
type SaveSmartActionMessages = {
id: string;
agentId: string;
creator: string;
messages: SmartActionMessage[];
};
type TransferTransaction = Omit<Transaction, "actions"> & {
actions: Array<TransferAction>;
};
type AccountTransaction = Omit<Transaction, "actions"> & {
actions: Array<FunctionCallAction | TransferAction>;
};
type SmartActionTransaction = {
args: Record<string, unknown> | string;
contractName: string;
deposit: string;
gas: string;
methodName: string;
} | TransferTransaction;
declare enum AssistantsMode {
DEFAULT = "default",
DEBUG = "debug"
}
declare enum Model {
GPT4o = "gpt4o",
Grok2 = "grok2",
Sonnet = "sonnet"
}
/**
* Props for the BitteAiChat component
* @param agentid - ID of the AI agent to use for chat interactions
* @param apiUrl - Internal API URL for chat communication (e.g. api/chat).
* Used to proxy requests to bitte api to not expose api key.
* @param wallet - Optional wallet configuration for allowing transactions through the component see {@link WalletOptions} for more details
*/
interface BitteAiChatProps {
agentid: string;
apiUrl: string;
historyApiUrl?: string;
messages?: Message[];
wallet?: WalletOptions;
options?: {
agentName?: string;
agentImage?: string;
chatId?: string;
};
theme?: "dark" | "light";
}
/**
* Configuration options for wallet integrations
*
* For NEAR:
* - Uses either near-api-js Account object for direct account access
* - Or Wallet from near-wallet-selector for wallet integrations
*
* For EVM:
* - Typically configured using wagmi hooks with WalletConnect:
* - address: From useAppKitAccount() hook
* - sendTransaction: From useSendTransaction() hook
* - hash: Transaction hash returned after sending
*/
type WalletOptions = {
near?: {
wallet?: Wallet;
account?: Account;
};
evm?: EVMWalletAdapter;
};
type SelectedAgent = {
id?: string;
name?: string;
};
interface AssistantsRequestBody {
threadId: string | null;
message: string;
accountId: string;
kvId: string;
config?: {
mode?: AssistantsMode;
agentId?: string;
};
}
interface ChatRequestBody {
id?: string;
config?: {
mode?: string;
agentId?: string;
model?: string;
};
accountId?: string;
network?: string;
evmAddress?: Hex;
}
type AllowlistedToken = {
name: string;
symbol: string;
contractId: string;
decimals: number;
icon?: string;
};
interface EVMWalletAdapter {
sendTransaction: (params: {
to: string;
value?: bigint;
data?: string;
from: string;
gas?: bigint;
}) => Promise<void>;
address: string | undefined;
hash?: string;
}
type GenerateImageResponse = {
url: string;
hash: string;
};
type TransactionListProps = {
accountId: string;
operation?: TransactionOperation;
transaction: Transaction[];
modifiedUrl: string;
showDetails: boolean;
showTxnDetail: boolean;
setShowTxnDetail: (showTxnDetail: boolean) => void;
costs: Cost[];
gasPrice: string;
};
interface Cost {
deposit: BN;
gas: BN;
}
interface AccountCreationData {
devicePublicKey: string;
accountId: string;
isCreated: boolean;
txnHash?: string;
}
declare const BitteAiChat: ({ wallet, apiUrl, historyApiUrl, agentid, options, theme, }: BitteAiChatProps) => react_jsx_runtime.JSX.Element;
declare const ReviewTransaction: ({ transactions, warnings, walletLoading, chatId, }: {
transactions: Transaction[];
warnings?: BitteToolWarning[] | null;
creator?: string;
evmData?: SafeEncodedSignRequest;
agentId: string;
walletLoading?: boolean;
chatId: string | undefined;
}) => react_jsx_runtime.JSX.Element;
export { type AccountCreationData, type AccountTransaction, type AllowlistedToken, type AnyBitteTool, AssistantsMode, type AssistantsRequestBody, BitteAiChat, type BitteAiChatProps, type BitteAssistant, type BitteAssistantConfig, type BitteMetadata, type BitteOpenAPISpec, type BittePrimitiveRef, type BitteTool, type BitteToolExecutor, type BitteToolRenderer, type BitteToolResult, type BitteToolSpec, type BitteToolWarning, type ChatRequestBody, type Cost, type EVMWalletAdapter, type ExecutionDefinition, type FunctionDataMessage, type GenerateImageResponse, Model, type PluginToolSpec, ReviewTransaction, type SaveSmartAction, type SaveSmartActionMessages, type SelectedAgent, type SmartAction, type SmartActionAiMessage, type SmartActionChat, type SmartActionMessage, type SmartActionTransaction, type TransactionListProps, type WalletOptions };