Skip to content

Commit

Permalink
disable tools based on provided env variables (#165)
Browse files Browse the repository at this point in the history
* disable tools based on provided tool names in NEXT_PUBLIC_DISABLED_TOOLS env variable

* made env varibale sused by tools public and disable tools if required env variable is not given

* added required env variables in tools and disable tools if required env variable is not given

* removed console statements

---------

Co-authored-by: shubhiscoding <mygitcoint@gmail.com>
  • Loading branch information
shubhiscoding and shubhiscoding authored Jan 28, 2025
1 parent ffbdf99 commit 030e58e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
NEXT_PUBLIC_PRIVY_APP_ID=
NEXT_PUBLIC_EAP_RECEIVE_WALLET_ADDRESS=
NEXT_PUBLIC_HELIUS_RPC_URL=
NEXT_PUBLIC_DISABLED_TOOLS=[] #["getTokenOrders", "holders", "resolveWalletAddressFromDomain"]
OPENAI_API_KEY=
PRIVY_APP_SECRET=
WALLET_ENCRYPTION_KEY=
Expand Down
1 change: 1 addition & 0 deletions src/ai/generic/jina.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const jinaTools = {
.describe('The URL of the web page to read and convert to text'),
}),
isCollapsible: true,
requiredEnvVars: ['JINA_API_KEY'],
execute: async ({ url }: { url: string }) => {
try {
const response = await fetch(`https://r.jina.ai/${url}`, {
Expand Down
5 changes: 5 additions & 0 deletions src/ai/generic/telegram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const telegramTools = {
parameters: z.object({
username: z.string().optional(),
}),
requiredEnvVars: ['TELEGRAM_BOT_USERNAME'],
execute: async function ({ username }: { username?: string }) {
try {
const response = await verifyTelegramSetupAction({
Expand Down Expand Up @@ -150,6 +151,10 @@ export const telegramTools = {
username: z.string().optional(),
message: z.string(),
}),
requiredEnvVars: [
'TELEGRAM_BOT_TOKEN',
'TELEGRAM_BOT_USERNAME',
],
execute: async function ({
username,
message,
Expand Down
28 changes: 27 additions & 1 deletion src/ai/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export interface ToolConfig {
agentKit?: any;
userId?: any;
requiresConfirmation?: boolean;
requiredEnvVars?: string[];
}

export function DefaultToolResultRenderer({ result }: { result: unknown }) {
Expand Down Expand Up @@ -152,6 +153,30 @@ export const defaultTools: Record<string, ToolConfig> = {
...birdeyeTools,
};

export function filterTools(
tools: Record<string, ToolConfig>,
): Record<string, ToolConfig> {
const disabledTools = process.env.NEXT_PUBLIC_DISABLED_TOOLS
? JSON.parse(process.env.NEXT_PUBLIC_DISABLED_TOOLS)
: [];

return Object.fromEntries(
Object.entries(tools).filter(([toolName, toolConfig]) => {
if (disabledTools.includes(toolName)) {
return false;
}
if(toolConfig.requiredEnvVars){
for (const envVar of toolConfig.requiredEnvVars) {
if(!process.env[envVar] || process.env[envVar] == ''){
return false;
}
}
}
return true;
}),
);
}

export const coreTools: Record<string, ToolConfig> = {
...actionTools,
...utilTools,
Expand Down Expand Up @@ -234,8 +259,9 @@ export function getToolConfig(toolName: string): ToolConfig | undefined {
export function getToolsFromRequiredTools(
toolNames: string[],
): Record<string, ToolConfig> {
const enabledTools = filterTools(defaultTools);
return toolNames.reduce((acc: Record<string, ToolConfig>, toolName) => {
const tool = defaultTools[toolName];
const tool = enabledTools[toolName];
if (tool) {
acc[toolName] = tool;
}
Expand Down
1 change: 1 addition & 0 deletions src/ai/solana/birdeye.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const birdeyeTools = {
.nativeEnum(BirdeyeTimeframe)
.describe('The timeframe to search for'),
}),
requiredEnvVars: ['BIRDEYE_API_KEY'],
execute: async ({ timeframe }: { timeframe: BirdeyeTimeframe }) => {
try {
const traders = await getTopTraders({ timeframe });
Expand Down
2 changes: 2 additions & 0 deletions src/ai/solana/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const priceChartTool = {
description:
'Retrieves and displays price history for a Solana token via CoinGecko market data, falling back to DEX if unavailable.',
parameters: chartToolParameters,
requiredEnvVars: ['CG_API_KEY'],
execute: async ({
contractAddress,
timeFrame,
Expand Down Expand Up @@ -104,6 +105,7 @@ export const dexChartTool = {
isExpandedByDefault: true,
description: 'Retrieves and displays price history data from the DEX.',
parameters: chartToolParameters,
requiredEnvVars: ['CG_API_KEY'],
execute: async ({
contractAddress,
timeFrame,
Expand Down

0 comments on commit 030e58e

Please sign in to comment.