Skip to content

Commit

Permalink
Merge pull request #106 from autonomys/ref/agent-frame-types
Browse files Browse the repository at this point in the history
Ref/agent frame types
  • Loading branch information
Xm0onh authored Jan 4, 2025
2 parents 53bd452 + e607f53 commit 14f66a5
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 47 deletions.
5 changes: 4 additions & 1 deletion auto-agents-framework/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ SERPAPI_API_KEY=<serpapi_api_key>
NODE_ENV=<node_env>

# Retry Limit
RETRY_LIMIT=<retry_limit>
RETRY_LIMIT=<retry_limit>

# Agent Version
AGENT_VERSION=<agent_version>
1 change: 1 addition & 0 deletions auto-agents-framework/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
characters/
!characters/character.example.ts
.cookies/
dsn-kol-schemas.json
10 changes: 6 additions & 4 deletions auto-agents-framework/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"dev": "tsx watch src/index.ts",
"format": "prettier --write \"src/**/*.ts\"",
"format:check": "prettier --check \"src/**/*.ts\"",
"example:twitter": "tsx examples/twitter.ts"
"example:twitter": "tsx examples/twitter.ts",
"extract-kol-dsn-schemas": "tsx src/agents/workflows/kol/cli/extractDsnSchemas.ts"
},
"dependencies": {
"@autonomys/auto-dag-data": "1.2.1",
Expand All @@ -21,9 +22,10 @@
"@langchain/openai": "0.3.16",
"agent-twitter-client": "0.0.18",
"dotenv": "^16.3.1",
"ethers": "^6.13.4",
"ethers": "^6.13.4",
"winston": "^3.11.0",
"zod": "^3.22.4"
"zod": "^3.22.4",
"zod-to-json-schema": "^3.24.1"
},
"devDependencies": {
"@tsconfig/node20": "^20.1.4",
Expand All @@ -33,4 +35,4 @@
"tsx": "^4.7.1",
"typescript": "^5.3.3"
}
}
}
5 changes: 4 additions & 1 deletion auto-agents-framework/src/agents/tools/utils/dsnUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createLogger } from '../../../utils/logger.js';
import { hexlify } from 'ethers';
import { createAutoDriveApi, uploadFile } from '@autonomys/auto-drive';
import { stringToCid, blake3HashFromCid } from '@autonomys/auto-dag-data';
import { config } from '../../../config/index.js';
import { config, agentVersion } from '../../../config/index.js';
import { wallet, signMessage } from './agentWallet.js';
import { setLastMemoryHash, getLastMemoryCid } from './agentMemoryContract.js';

Expand Down Expand Up @@ -68,13 +68,15 @@ export async function uploadToDsn(data: object) {
data: data,
previousCid: previousCid,
timestamp: timestamp,
agentVersion: agentVersion,
});

const dsnData = {
...data,
previousCid: previousCid,
signature: signature,
timestamp: timestamp,
agentVersion: agentVersion,
};

logger.info('Upload to Dsn - DSN Data', { dsnData });
Expand All @@ -93,6 +95,7 @@ export async function uploadToDsn(data: object) {
compression: true,
password: config.autoDriveConfig.AUTO_DRIVE_ENCRYPTION_PASSWORD || undefined,
});
logger.info('Upload to Dsn - Uploaded CID', { uploadedCid }, stringToCid(uploadedCid));

const blake3hash = blake3HashFromCid(stringToCid(uploadedCid));
logger.info('Setting last memory hash', {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import { DsnDataType } from '../types.js';
import { engagementSchema, responseSchema, skippedEngagementSchema, dsnTweet } from '../schemas.js';
import { writeFileSync } from 'fs';
import { join } from 'path';

const dsnCommonFields = z.object({
previousCid: z.string().optional(),
signature: z.string(),
timestamp: z.string(),
agentVersion: z.string(),
});

const extractDsnResponseSchema = () => {
const schema = z
.object({
type: z.literal(DsnDataType.RESPONSE),
tweet: dsnTweet,
decision: engagementSchema,
})
.merge(responseSchema)
.merge(dsnCommonFields);

return zodToJsonSchema(schema);
};

const extractDsnSkippedEngagementSchema = () => {
const schema = z
.object({
type: z.literal(DsnDataType.SKIPPED_ENGAGEMENT),
tweet: dsnTweet,
})
.merge(skippedEngagementSchema)
.merge(dsnCommonFields);

return zodToJsonSchema(schema);
};

const extractDsnGeneratedTweetSchema = () => {
const schema = z
.object({
type: z.literal(DsnDataType.GENERATED_TWEET),
content: z.string(),
tweetId: z.string().nullable(),
})
.merge(dsnCommonFields);

return zodToJsonSchema(schema);
};

const main = () => {
const schemas = {
response: extractDsnResponseSchema(),
skipped_engagement: extractDsnSkippedEngagementSchema(),
generated_tweet: extractDsnGeneratedTweetSchema(),
};

const outputPath = join(process.cwd(), 'dsn-kol-schemas.json');
writeFileSync(outputPath, JSON.stringify(schemas, null, 2));
console.log(`Schemas written to ${outputPath}`);
};

main();
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ export const createEngagementNode = (config: WorkflowConfig) => {
text: tweet.text!,
username: tweet.username!,
timeParsed: tweet.timeParsed!,
thread:
tweet.thread && tweet.thread.length > 0
? Array.from(
tweet.thread.map(t => ({
id: t.id,
text: t.text,
username: t.username,
timeParsed: t.timeParsed,
})),
)
: 'No thread',
},
decision,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { EngagementDecision, WorkflowConfig } from '../types.js';
import {
DsnData,
DsnGeneratedTweetData,
DsnResponseData,
DsnSkippedEngagementData,
EngagementDecision,
WorkflowConfig,
DsnDataType,
} from '../types.js';
import { createLogger } from '../../../../utils/logger.js';
import { State } from '../workflow.js';
import { invokePostTweetTool } from '../../../tools/postTweetTool.js';
Expand All @@ -13,26 +21,30 @@ const postResponse = async (
decision: EngagementDecision,
summary: z.infer<typeof summarySchema>,
) => {
const thread =
decision.tweet.thread && decision.tweet.thread.length > 0
? decision.tweet.thread.map(t => ({ text: t.text, username: t.username }))
: 'No thread';
const decisionInfo = { tweet: decision.tweet.text, reason: decision.decision.reason };
const engagementDecision = {
tweetText: decision.tweet.text,
reason: decision.decision.reason,
};
const response = await config.prompts.responsePrompt
.pipe(config.llms.generation)
.pipe(responseParser)
.invoke({
decision: decisionInfo,
thread,
decision: engagementDecision,
thread: decision.tweet.thread,
patterns: summary.patterns,
commonWords: summary.commonWords,
});
//TODO: After sending the tweet, we need to get the latest tweet, ensure it is the same as we sent and return it
//This has not been working as expected, so we need to investigate this later
const tweet = await invokePostTweetTool(config.toolNode, response.content, decision.tweet.id);
const postedResponse = await invokePostTweetTool(
config.toolNode,
response.content,
decision.tweet.id,
);
return {
...response,
decisionInfo: decisionInfo,
tweet: decision.tweet,
engagementDecision,
//tweetId: tweet ? tweet.id : null
};
};
Expand Down Expand Up @@ -63,9 +75,7 @@ export const createGenerateTweetNode =
id: d.tweet.id,
text: d.tweet.text,
username: d.tweet.username,
thread: d.tweet.thread
? d.tweet.thread.map(t => ({ id: t.id, text: t.text, username: t.username }))
: 'No thread',
thread: d.tweet.thread,
},
}));
logger.info('Engagement Decisions', {
Expand All @@ -90,24 +100,33 @@ export const createGenerateTweetNode =
const postedTweet = await invokePostTweetTool(config.toolNode, generatedTweet.tweet);

// Transform the data into an array format expected by DSN
const formattedDsnData = [
...postedResponses.map(response => ({
type: 'response',
content: response.content,
decisionInfo: response.decisionInfo,
//tweetId: response.tweetId,
strategy: response.strategy,
})),
...shouldNotEngage.map(item => ({
type: 'skipped_engagement',
decision: item.decision,
tweet: item.tweet,
})),
const formattedDsnData: DsnData[] = [
...postedResponses.map(
response =>
({
type: DsnDataType.RESPONSE,
tweet: response.tweet,
content: response.content,
strategy: response.strategy,
decision: {
shouldEngage: true,
reason: response.engagementDecision.reason,
},
}) as DsnResponseData,
),
...shouldNotEngage.map(
item =>
({
type: DsnDataType.SKIPPED_ENGAGEMENT,
decision: item.decision,
tweet: item.tweet,
}) as DsnSkippedEngagementData,
),
{
type: 'generated_tweet',
type: DsnDataType.GENERATED_TWEET,
content: generatedTweet.tweet,
tweetId: postedTweet ? postedTweet.postedTweetId : null,
},
} as DsnGeneratedTweetData,
];

return {
Expand Down
26 changes: 15 additions & 11 deletions auto-agents-framework/src/agents/workflows/kol/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import { z } from 'zod';

export const dsnTweet: z.ZodType = z.object({
id: z.string(),
text: z.string(),
username: z.string(),
timeParsed: z.string(),
thread: z.array(z.lazy(() => dsnTweet)).optional(),
});

export const engagementSchema = z.object({
shouldEngage: z.boolean(),
reason: z.string(),
reason: z.string().optional(),
});

export const responseSchema = z.object({
content: z.string().describe('The response to the tweet'),
strategy: z.string().describe('The strategy used to generate the response'),
thread: z
.array(
z.object({
id: z.string(),
text: z.string(),
username: z.string(),
}),
)
.optional(),
});

export const skippedEngagementSchema = z.object({
decision: engagementSchema,
});

export const dsnUploadSchema = z.object({
previousCid: z.string().optional(),
data: z.any(),
signature: z.string(),
previousCid: z.string().optional(),
});

export const trendSchema = z.object({
Expand Down
26 changes: 25 additions & 1 deletion auto-agents-framework/src/agents/workflows/kol/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import { Tweet, TwitterApi } from '../../../services/twitter/types.js';
import { ToolNode } from '@langchain/langgraph/prebuilt';
import { ChatOpenAI } from '@langchain/openai';
import { Runnable } from '@langchain/core/runnables';
import { engagementSchema } from './schemas.js';
import { engagementSchema, responseSchema, skippedEngagementSchema, dsnTweet } from './schemas.js';
import { ChatPromptTemplate } from '@langchain/core/prompts';

export enum DsnDataType {
RESPONSE = 'response',
SKIPPED_ENGAGEMENT = 'skipped',
GENERATED_TWEET = 'posted',
}
export type WorkflowConfig = Readonly<{
twitterApi: TwitterApi;
toolNode: ToolNode;
Expand All @@ -30,3 +35,22 @@ export type EngagementDecision = {
decision: z.infer<typeof engagementSchema>;
tweet: Tweet;
};

export type DsnResponseData = {
type: DsnDataType.RESPONSE;
tweet: z.infer<typeof dsnTweet>;
decision: z.infer<typeof engagementSchema>;
} & z.infer<typeof responseSchema>;

export type DsnSkippedEngagementData = {
type: DsnDataType.SKIPPED_ENGAGEMENT;
tweet: z.infer<typeof dsnTweet>;
} & z.infer<typeof skippedEngagementSchema>;

export type DsnGeneratedTweetData = {
type: DsnDataType.GENERATED_TWEET;
content: string;
tweetId: string | null;
};

export type DsnData = DsnResponseData | DsnSkippedEngagementData | DsnGeneratedTweetData;
2 changes: 2 additions & 0 deletions auto-agents-framework/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function formatZodError(error: z.ZodError) {
\nPlease check your .env file and ensure all required variables are set correctly.`;
}

export const agentVersion = process.env.AGENT_VERSION || '1.0.0';

export const config = (() => {
try {
const username = process.env.TWITTER_USERNAME || '';
Expand Down
2 changes: 1 addition & 1 deletion auto-agents-framework/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,7 @@ yaml@^2.2.1:
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.6.1.tgz#42f2b1ba89203f374609572d5349fb8686500773"
integrity sha512-7r0XPzioN/Q9kXBro/XPnA6kznR73DHq+GXh5ON7ZozRO6aMjbmiBuKste2wslTFkC5d1dw0GooOCepZXJ2SAg==

zod-to-json-schema@^3.22.3, zod-to-json-schema@^3.22.5:
zod-to-json-schema@^3.22.3, zod-to-json-schema@^3.22.5, zod-to-json-schema@^3.24.1:
version "3.24.1"
resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.24.1.tgz#f08c6725091aadabffa820ba8d50c7ab527f227a"
integrity sha512-3h08nf3Vw3Wl3PK+q3ow/lIil81IT2Oa7YpQyUUDsEWbXveMesdfK1xBd2RhCkynwZndAxixji/7SYJJowr62w==
Expand Down

0 comments on commit 14f66a5

Please sign in to comment.