Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E-02 Photo/Video Upload to Twitter, Client Replacement (goat-x) #8

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 45 additions & 7 deletions packages/core/src/clients/discord/actions/generateImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import * as path from 'path';
import { randomUUID } from 'crypto';
import { generateText } from "../../../core/generation.ts";
import { ModelClass } from "../../../core/types";
import { Scraper } from "goat-x";
import { ClientBase } from "../../../clients/twitter/base";

export const discordImageGeneration: Action = {
name: "GENERATE_IMAGE",
Expand Down Expand Up @@ -89,30 +91,66 @@ Enhanced prompt:`;
const tempDir = path.join(process.cwd(), 'temp');
await fs.mkdir(tempDir, { recursive: true });

// For Discord, convert base64 to Buffer
// Convert base64 to Buffer
const imageBuffer = Buffer.from(
images.data[0].replace(/^data:image\/\w+;base64,/, ""),
'base64'
);

// Create temp file path
// Save to temp file
const tempFileName = path.join(tempDir, `${randomUUID()}.png`);

// Save buffer to temp file
await fs.writeFile(tempFileName, imageBuffer);

// Send to Discord
await callback(
{
text: `Here's your generated image based on: "${imagePrompt}"`,
text: `Prompt Used: "${imagePrompt}"`,
files: [{
attachment: tempFileName,
name: 'generated_image.png'
}]
},
[tempFileName] // Pass temp file for cleanup
[tempFileName]
);

// Cleanup temp file after sending

try {
const tweetText = imagePrompt.trim();

elizaLogger.log("Attempting to post to Twitter with text:", tweetText);

// Create a client instance like in post.ts
const client = new ClientBase({ runtime });
const result = await client.requestQueue.add(
async () => await client.twitterClient.sendTweet(
tweetText,
undefined,
[{
data: imageBuffer,
mediaType: 'image/png'
}]
)
);

const body = await result.json();
const tweetResult = body.data.create_tweet.tweet_results.result;

elizaLogger.log("Successfully posted image to Twitter:", tweetResult);

await callback({
text: `Image has been generated and shared on Twitter! 🐦\nhttps://twitter.com/${runtime.getSetting("TWITTER_USERNAME")}/status/${tweetResult.rest_id}`,
files: [{
attachment: tempFileName,
name: 'generated_image.png'
}]
}, [tempFileName]);

} catch (twitterError) {
elizaLogger.error("Error posting to Twitter:", twitterError);
// Continue with Discord post even if Twitter fails
}

// Cleanup temp file
await fs.unlink(tempFileName).catch(err =>
elizaLogger.error("Error cleaning up temp file:", err)
);
Expand Down
71 changes: 63 additions & 8 deletions packages/core/src/clients/discord/actions/generateVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { generateVideo } from "../../../actions/videoGenerationUtils";
import { generateText } from "../../../core/generation";
import { ModelClass } from "../../../core/types";
import { randomUUID } from 'crypto';
import * as fs from 'fs/promises';
import * as path from 'path';
import { ClientBase } from "../../../clients/twitter/base";

export const discordVideoGeneration: Action = {
name: "GENERATE_VIDEO",
Expand Down Expand Up @@ -93,15 +96,67 @@ Enhanced prompt:`;
if (video.success && video.url) {
elizaLogger.log("Video generation successful, URL:", video.url);

await callback(
{
text: `✨ Here's your generated video: ${video.url}`,
try {
// Download video from URL
const response = await fetch(video.url);
const videoBuffer = Buffer.from(await response.arrayBuffer());

// Create temp directory if it doesn't exist
const tempDir = path.join(process.cwd(), 'temp');
await fs.mkdir(tempDir, { recursive: true });

// Save to temp file
const tempFileName = path.join(tempDir, `${randomUUID()}.mp4`);
await fs.writeFile(tempFileName, videoBuffer);

try {
const tweetText = cleanPrompt.trim();

elizaLogger.log("Attempting to post to Twitter with text:", tweetText);

// Create a client instance
const client = new ClientBase({ runtime });
const result = await client.requestQueue.add(
async () => await client.twitterClient.sendTweet(
tweetText,
undefined,
[{
data: videoBuffer,
mediaType: 'video/mp4'
}]
)
);

const body = await result.json();
const tweetResult = body.data.create_tweet.tweet_results.result;

elizaLogger.log("Successfully posted video to Twitter:", tweetResult);

await callback({
text: `✨ Video generated and shared on Twitter! 🐦\n${video.url}\nhttps://twitter.com/${runtime.getSetting("TWITTER_USERNAME")}/status/${tweetResult.rest_id}`,
}, []);

} catch (twitterError) {
elizaLogger.error("Error posting to Twitter:", twitterError);
// Continue with Discord post even if Twitter fails
await callback({
text: `✨ Here's your generated video: ${video.url}`,
action: "GENERATE_VIDEO"
}, []);
}

// Cleanup temp file
await fs.unlink(tempFileName).catch(err =>
elizaLogger.error("Error cleaning up temp file:", err)
);
} catch (fsError) {
elizaLogger.error("File system error:", fsError);
// If file handling fails, still send the video URL
await callback({
text: `${video.url}`,
action: "GENERATE_VIDEO"
},
[]
);

elizaLogger.log("Discord response sent successfully");
}, []);
}
} else {
elizaLogger.error("Video generation failed with response:", video);
await callback(
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/core/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ModelProvider,
Provider,
State,
TwitterAuth,
type Action,
type Evaluator,
type Memory,
Expand Down Expand Up @@ -129,6 +130,8 @@ export class AgentRuntime implements IAgentRuntime {

pdfService: IPdfService;

twitterAuth?: TwitterAuth;

/**
* Fetch function to use
* Some environments may not have access to the global fetch function and need a custom fetch override.
Expand Down Expand Up @@ -200,6 +203,7 @@ export class AgentRuntime implements IAgentRuntime {
databaseAdapter: IDatabaseAdapter; // The database adapter used for interacting with the database
fetch?: typeof fetch | unknown;
speechModelPath?: string;
twitterAuth?: TwitterAuth;
}) {
this.#conversationLength =
opts.conversationLength ?? this.#conversationLength;
Expand Down Expand Up @@ -322,6 +326,8 @@ export class AgentRuntime implements IAgentRuntime {
) {
// this.processCharacterKnowledge(opts.character.knowledge);
}

this.twitterAuth = opts.twitterAuth;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ export interface IAgentRuntime {
character: Character;
providers: Provider[];
actions: Action[];
twitterAuth?: TwitterAuth;

messageManager: IMemoryManager;
descriptionManager: IMemoryManager;
Expand Down Expand Up @@ -629,4 +630,17 @@ export interface ActionResponse {
retweet: boolean;
quote?: boolean;
reply?: boolean;
}

export interface TwitterAuth {
apiKey: string;
apiSecret: string;
accessToken: string;
accessTokenSecret: string;
bearerToken?: string;
guestToken?: string;
cookieJar: () => {
getCookies: (url: string) => Promise<Array<{ key: string; value: string }>>;
getCookieString: (url: string) => Promise<string>;
};
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"checkJs": false,
"noEmitOnError": false,
"moduleDetection": "force",
"allowArbitraryExtensions": true
"allowArbitraryExtensions": true,
}
}
Loading