Skip to content

Commit

Permalink
Merge pull request elizaOS#985 from jnaulty/jnaulty/run-without-alche…
Browse files Browse the repository at this point in the history
…my-and-aws-env

fix: Goat Plugin + AWS S3 Service error when env vars absent
  • Loading branch information
odilitime authored Dec 11, 2024
2 parents d7e8d9a + 7a37c49 commit 8f1ce72
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
9 changes: 6 additions & 3 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,12 @@ export async function createAgent(
throw new Error("Invalid TEE configuration");
}

const goatPlugin = await createGoatPlugin((secret) =>
getSecret(character, secret)
);
let goatPlugin: any | undefined;
if (getSecret(character, "ALCHEMY_API_KEY")) {
goatPlugin = await createGoatPlugin((secret) =>
getSecret(character, secret)
);
}

return new AgentRuntime({
databaseAdapter: db,
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-node/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ export async function validateNodeConfig(
// VITS settings
VITS_VOICE: voiceSettings?.model || process.env.VITS_VOICE,
VITS_MODEL: process.env.VITS_MODEL,

// AWS settings (only include if present)
...(runtime.getSetting("AWS_ACCESS_KEY_ID") && {
AWS_ACCESS_KEY_ID: runtime.getSetting("AWS_ACCESS_KEY_ID"),
AWS_SECRET_ACCESS_KEY: runtime.getSetting("AWS_SECRET_ACCESS_KEY"),
AWS_REGION: runtime.getSetting("AWS_REGION"),
AWS_S3_BUCKET: runtime.getSetting("AWS_S3_BUCKET"),
AWS_S3_UPLOAD_PATH: runtime.getSetting("AWS_S3_UPLOAD_PATH"),
}),
};

return nodeEnvSchema.parse(config);
Expand Down
63 changes: 37 additions & 26 deletions packages/plugin-node/src/services/awsS3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,28 @@ interface JsonUploadResult extends UploadResult {
export class AwsS3Service extends Service implements IAwsS3Service {
static serviceType: ServiceType = ServiceType.AWS_S3;

private s3Client: S3Client;
private bucket: string;
private fileUploadPath: string;
getInstance(): IAwsS3Service {
return AwsS3Service.getInstance();
}
private s3Client: S3Client | null = null;
private bucket: string = '';
private fileUploadPath: string = '';
private runtime: IAgentRuntime | null = null;

async initialize(runtime: IAgentRuntime): Promise<void> {
console.log("Initializing AwsS3Service");
this.runtime = runtime;
const AWS_ACCESS_KEY_ID = runtime.getSetting("AWS_ACCESS_KEY_ID");
const AWS_SECRET_ACCESS_KEY = runtime.getSetting(
"AWS_SECRET_ACCESS_KEY"
);
const AWS_REGION = runtime.getSetting("AWS_REGION");
const AWS_S3_BUCKET = runtime.getSetting("AWS_S3_BUCKET");
if (
!AWS_ACCESS_KEY_ID ||
!AWS_SECRET_ACCESS_KEY ||
!AWS_REGION ||
!AWS_S3_BUCKET
) {
throw new Error(
"Missing required AWS credentials in environment variables"
);
this.fileUploadPath = runtime.getSetting("AWS_S3_UPLOAD_PATH") ?? "";
}

private async initializeS3Client(): Promise<boolean> {
if (this.s3Client) return true;
if (!this.runtime) return false;

const AWS_ACCESS_KEY_ID = this.runtime.getSetting("AWS_ACCESS_KEY_ID");
const AWS_SECRET_ACCESS_KEY = this.runtime.getSetting("AWS_SECRET_ACCESS_KEY");
const AWS_REGION = this.runtime.getSetting("AWS_REGION");
const AWS_S3_BUCKET = this.runtime.getSetting("AWS_S3_BUCKET");

if (!AWS_ACCESS_KEY_ID || !AWS_SECRET_ACCESS_KEY || !AWS_REGION || !AWS_S3_BUCKET) {
return false;
}

this.s3Client = new S3Client({
Expand All @@ -61,8 +57,8 @@ export class AwsS3Service extends Service implements IAwsS3Service {
secretAccessKey: AWS_SECRET_ACCESS_KEY,
},
});
this.fileUploadPath = runtime.getSetting("AWS_S3_UPLOAD_PATH") ?? "";
this.bucket = AWS_S3_BUCKET;
return true;
}

async uploadFile(
Expand All @@ -71,6 +67,13 @@ export class AwsS3Service extends Service implements IAwsS3Service {
expiresIn: number = 900
): Promise<UploadResult> {
try {
if (!await this.initializeS3Client()) {
return {
success: false,
error: "AWS S3 credentials not configured",
};
}

if (!fs.existsSync(filePath)) {
return {
success: false,
Expand Down Expand Up @@ -120,10 +123,7 @@ export class AwsS3Service extends Service implements IAwsS3Service {
} catch (error) {
return {
success: false,
error:
error instanceof Error
? error.message
: "Unknown error occurred",
error: error instanceof Error ? error.message : "Unknown error occurred",
};
}
}
Expand All @@ -135,6 +135,10 @@ export class AwsS3Service extends Service implements IAwsS3Service {
fileName: string,
expiresIn: number = 900
): Promise<string> {
if (!await this.initializeS3Client()) {
throw new Error("AWS S3 credentials not configured");
}

const command = new GetObjectCommand({
Bucket: this.bucket,
Key: fileName,
Expand Down Expand Up @@ -171,6 +175,13 @@ export class AwsS3Service extends Service implements IAwsS3Service {
expiresIn: number = 900
): Promise<JsonUploadResult> {
try {
if (!await this.initializeS3Client()) {
return {
success: false,
error: "AWS S3 credentials not configured",
};
}

// Validate input
if (!jsonData) {
return {
Expand Down

0 comments on commit 8f1ce72

Please sign in to comment.