Skip to content

Commit

Permalink
env
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Nov 21, 2024
1 parent 6d2c658 commit 365ecb6
Show file tree
Hide file tree
Showing 21 changed files with 347 additions and 47 deletions.
15 changes: 13 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Required environment variables
# Discord Configuration
DISCORD_APPLICATION_ID=
DISCORD_API_TOKEN= # Bot token

# AI Model API Keys
OPENAI_API_KEY= # OpenAI API key, starting with sk-
REDPILL_API_KEY= # REDPILL API Key
GROK_API_KEY= # GROK API Key
GROQ_API_KEY= # Starts with gsk_
OPENROUTER_API_KEY=
GOOGLE_GENERATIVE_AI_API_KEY= # Gemini API key

# Speech Synthesis
ELEVENLABS_XI_API_KEY= # API key from elevenlabs

# ElevenLabs Settings
Expand Down Expand Up @@ -73,11 +77,18 @@ BASE_MINT=So11111111111111111111111111111111111111112
RPC_URL=https://api.mainnet-beta.solana.com
HELIUS_API_KEY=

# Other Services
# Telegram Configuration
TELEGRAM_BOT_TOKEN=

# Together Configuration
TOGETHER_API_KEY=

# Server Configuration
SERVER_PORT=3000

# Starknet Configuration
STARKNET_ADDRESS=
STARKNET_PRIVATE_KEY=
STARKNET_RPC_URL=


6 changes: 4 additions & 2 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PostgresDatabaseAdapter } from "@ai16z/adapter-postgres";
import { SqliteDatabaseAdapter } from "@ai16z/adapter-sqlite";
import { DirectClient, DirectClientInterface } from "@ai16z/client-direct";
import { DirectClientInterface } from "@ai16z/client-direct";
import { DiscordClientInterface } from "@ai16z/client-discord";
import { AutoClientInterface } from "@ai16z/client-auto";
import { TelegramClientInterface } from "@ai16z/client-telegram";
Expand All @@ -20,6 +20,7 @@ import {
elizaLogger,
settings,
IDatabaseAdapter,
validateCharacterConfig,
} from "@ai16z/eliza";
import { bootstrapPlugin } from "@ai16z/plugin-bootstrap";
import { solanaPlugin } from "@ai16z/plugin-solana";
Expand All @@ -30,7 +31,8 @@ import readline from "readline";
import yargs from "yargs";
import path from "path";
import { fileURLToPath } from "url";
import { character } from "./character.ts";
import blobert from "./blobert.ts";
import { DirectClient } from "@ai16z/client-direct";

const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
Expand Down
15 changes: 5 additions & 10 deletions packages/client-discord/src/enviroment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@ export const discordEnvSchema = z.object({

export type DiscordConfig = z.infer<typeof discordEnvSchema>;

// Validate and export the config
export const discordConfig = discordEnvSchema.parse({
DISCORD_APPLICATION_ID: process.env.DISCORD_APPLICATION_ID,
DISCORD_API_TOKEN: process.env.DISCORD_API_TOKEN,
});

export function validateDiscordConfig(runtime: IAgentRuntime): DiscordConfig {
console.log(runtime?.character.settings.secrets.DISCORD_APPLICATION_ID);
export async function validateDiscordConfig(
runtime: IAgentRuntime
): Promise<DiscordConfig> {
try {
const config = {
DISCORD_APPLICATION_ID:
runtime?.character.settings.secrets.DISCORD_APPLICATION_ID ||
runtime.getSetting("DISCORD_APPLICATION_ID") ||
process.env.DISCORD_APPLICATION_ID,
DISCORD_API_TOKEN:
runtime?.character.settings.secrets.DISCORD_API_TOKEN ||
runtime.getSetting("DISCORD_API_TOKEN") ||
process.env.DISCORD_API_TOKEN,
};

Expand Down
3 changes: 2 additions & 1 deletion packages/client-discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ export function startDiscord(runtime: IAgentRuntime) {

export const DiscordClientInterface: ElizaClient = {
start: async (runtime: IAgentRuntime) => {
// validateDiscordConfig(runtime);
await validateDiscordConfig(runtime);

return new DiscordClient(runtime);
},
stop: async (runtime: IAgentRuntime) => {
Expand Down
38 changes: 38 additions & 0 deletions packages/client-github/src/enviroment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { IAgentRuntime } from "@ai16z/eliza";
import { z } from "zod";

export const githubEnvSchema = z.object({
GITHUB_OWNER: z.string().min(1, "GitHub owner is required"),
GITHUB_REPO: z.string().min(1, "GitHub repo is required"),
GITHUB_BRANCH: z.string().min(1, "GitHub branch is required"),
GITHUB_PATH: z.string().min(1, "GitHub path is required"),
GITHUB_API_TOKEN: z.string().min(1, "GitHub API token is required"),
});

export type GithubConfig = z.infer<typeof githubEnvSchema>;

export async function validateGithubConfig(
runtime: IAgentRuntime
): Promise<GithubConfig> {
try {
const config = {
GITHUB_OWNER: runtime.getSetting("GITHUB_OWNER"),
GITHUB_REPO: runtime.getSetting("GITHUB_REPO"),
GITHUB_BRANCH: runtime.getSetting("GITHUB_BRANCH"),
GITHUB_PATH: runtime.getSetting("GITHUB_PATH"),
GITHUB_API_TOKEN: runtime.getSetting("GITHUB_API_TOKEN"),
};

return githubEnvSchema.parse(config);
} catch (error) {
if (error instanceof z.ZodError) {
const errorMessages = error.errors
.map((err) => `${err.path.join(".")}: ${err.message}`)
.join("\n");
throw new Error(
`GitHub configuration validation failed:\n${errorMessages}`
);
}
throw error;
}
}
2 changes: 2 additions & 0 deletions packages/client-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
splitChunks,
embed,
} from "@ai16z/eliza";
import { validateGithubConfig } from "./enviroment";

export interface GitHubConfig {
owner: string;
Expand Down Expand Up @@ -220,6 +221,7 @@ export class GitHubClient {

export const GitHubClientInterface: Client = {
start: async (runtime: IAgentRuntime) => {
await validateGithubConfig(runtime);
elizaLogger.log("GitHubClientInterface start");

const client = new GitHubClient(runtime as AgentRuntime);
Expand Down
18 changes: 11 additions & 7 deletions packages/client-telegram/src/enviroment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IAgentRuntime } from "@ai16z/eliza";
import { z } from "zod";

export const telegramEnvSchema = z.object({
Expand All @@ -6,14 +7,17 @@ export const telegramEnvSchema = z.object({

export type TelegramConfig = z.infer<typeof telegramEnvSchema>;

// Validate and export the config
export const telegramConfig = telegramEnvSchema.parse({
TELEGRAM_BOT_TOKEN: process.env.TELEGRAM_BOT_TOKEN,
});

export function validateTelegramConfig(): TelegramConfig {
export async function validateTelegramConfig(
runtime: IAgentRuntime
): Promise<TelegramConfig> {
try {
return telegramEnvSchema.parse(process.env);
const config = {
TELEGRAM_BOT_TOKEN:
runtime.getSetting("TELEGRAM_BOT_TOKEN") ||
process.env.TELEGRAM_BOT_TOKEN,
};

return telegramEnvSchema.parse(config);
} catch (error) {
if (error instanceof z.ZodError) {
const errorMessages = error.errors
Expand Down
2 changes: 1 addition & 1 deletion packages/client-telegram/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { validateTelegramConfig } from "./enviroment.ts";

export const TelegramClientInterface: Client = {
start: async (runtime: IAgentRuntime) => {
validateTelegramConfig();
await validateTelegramConfig(runtime);

const tg = new TelegramClient(
runtime,
Expand Down
40 changes: 26 additions & 14 deletions packages/client-twitter/src/enviroment.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import { IAgentRuntime } from "@ai16z/eliza";
import { z } from "zod";

export const twitterEnvSchema = z.object({
TWITTER_DRY_RUN: z
.string()
.transform((val) => val.toLowerCase() === "true"),
TWITTER_USERNAME: z.string().min(1),
TWITTER_PASSWORD: z.string().min(1),
TWITTER_EMAIL: z.string().email(),
TWITTER_USERNAME: z.string().min(1, "Twitter username is required"),
TWITTER_PASSWORD: z.string().min(1, "Twitter password is required"),
TWITTER_EMAIL: z.string().email("Valid Twitter email is required"),
TWITTER_COOKIES: z.string().optional(),
});

export type TwitterConfig = z.infer<typeof twitterEnvSchema>;

// Validate and export the config
export const twitterConfig = twitterEnvSchema.parse({
TWITTER_DRY_RUN: process.env.TWITTER_DRY_RUN,
TWITTER_USERNAME: process.env.TWITTER_USERNAME,
TWITTER_PASSWORD: process.env.TWITTER_PASSWORD,
TWITTER_EMAIL: process.env.TWITTER_EMAIL,
TWITTER_COOKIES: process.env.TWITTER_COOKIES,
});

export function validateTwitterConfig(): TwitterConfig {
export async function validateTwitterConfig(
runtime: IAgentRuntime
): Promise<TwitterConfig> {
try {
return twitterEnvSchema.parse(process.env);
const config = {
TWITTER_DRY_RUN:
runtime.getSetting("TWITTER_DRY_RUN") ||
process.env.TWITTER_DRY_RUN,
TWITTER_USERNAME:
runtime.getSetting("TWITTER_USERNAME") ||
process.env.TWITTER_USERNAME,
TWITTER_PASSWORD:
runtime.getSetting("TWITTER_PASSWORD") ||
process.env.TWITTER_PASSWORD,
TWITTER_EMAIL:
runtime.getSetting("TWITTER_EMAIL") ||
process.env.TWITTER_EMAIL,
TWITTER_COOKIES:
runtime.getSetting("TWITTER_COOKIES") ||
process.env.TWITTER_COOKIES,
};

return twitterEnvSchema.parse(config);
} catch (error) {
if (error instanceof z.ZodError) {
const errorMessages = error.errors
Expand Down
2 changes: 1 addition & 1 deletion packages/client-twitter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TwitterManager {

export const TwitterClientInterface: Client = {
async start(runtime: IAgentRuntime) {
validateTwitterConfig();
await validateTwitterConfig(runtime);

elizaLogger.log("Twitter client started");

Expand Down
54 changes: 54 additions & 0 deletions packages/plugin-image-generation/src/enviroment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { IAgentRuntime } from "@ai16z/eliza";
import { z } from "zod";

export const imageGenEnvSchema = z
.object({
ANTHROPIC_API_KEY: z.string().optional(),
TOGETHER_API_KEY: z.string().optional(),
HEURIST_API_KEY: z.string().optional(),
})
.refine(
(data) => {
return !!(
data.ANTHROPIC_API_KEY ||
data.TOGETHER_API_KEY ||
data.HEURIST_API_KEY
);
},
{
message:
"At least one of ANTHROPIC_API_KEY, TOGETHER_API_KEY, or HEURIST_API_KEY is required",
}
);

export type ImageGenConfig = z.infer<typeof imageGenEnvSchema>;

export async function validateImageGenConfig(
runtime: IAgentRuntime
): Promise<ImageGenConfig> {
try {
const config = {
ANTHROPIC_API_KEY:
runtime.getSetting("ANTHROPIC_API_KEY") ||
process.env.ANTHROPIC_API_KEY,
TOGETHER_API_KEY:
runtime.getSetting("TOGETHER_API_KEY") ||
process.env.TOGETHER_API_KEY,
HEURIST_API_KEY:
runtime.getSetting("HEURIST_API_KEY") ||
process.env.HEURIST_API_KEY,
};

return imageGenEnvSchema.parse(config);
} catch (error) {
if (error instanceof z.ZodError) {
const errorMessages = error.errors
.map((err) => `${err.path.join(".")}: ${err.message}`)
.join("\n");
throw new Error(
`Image generation configuration validation failed:\n${errorMessages}`
);
}
throw error;
}
}
3 changes: 3 additions & 0 deletions packages/plugin-image-generation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { generateCaption, generateImage } from "@ai16z/eliza";

import fs from "fs";
import path from "path";
import { validateImageGenConfig } from "./enviroment";

export function saveBase64Image(base64Data: string, filename: string): string {
// Create generatedImages directory if it doesn't exist
Expand Down Expand Up @@ -76,6 +77,8 @@ const imageGeneration: Action = {
],
description: "Generate an image to go along with the message.",
validate: async (runtime: IAgentRuntime, message: Memory) => {
await validateImageGenConfig(runtime);

const anthropicApiKeyOk = !!runtime.getSetting("ANTHROPIC_API_KEY");
const togetherApiKeyOk = !!runtime.getSetting("TOGETHER_API_KEY");
const heuristApiKeyOk = !!runtime.getSetting("HEURIST_API_KEY");
Expand Down
Loading

0 comments on commit 365ecb6

Please sign in to comment.