Skip to content

Commit

Permalink
Merge pull request #17 from autonomys/auto-chain-agent
Browse files Browse the repository at this point in the history
Add auto chain agent
  • Loading branch information
jfrank-summit authored Oct 30, 2024
2 parents 6e09eb3 + 241a3e1 commit 833712d
Show file tree
Hide file tree
Showing 18 changed files with 4,456 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ node_modules/
# SQLite database
*.sqlite
*.sqlite-journal

resources/
5 changes: 5 additions & 0 deletions auto-chain-agent/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
AGENTS_PORT=3001
BACKEND_PORT=8001
FRONTEND_PORT=3031
OPENAI_API_KEY=<your_openai_api_key_here>
BACKEND_SERVICE_URL=http://localhost:8001
38 changes: 38 additions & 0 deletions auto-chain-agent/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Dependencies
node_modules
.pnp
.pnp.js

# Build outputs
dist
build
out

# Environment files
.env
.env.local
.env.*.local

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Editor directories
.idea
.vscode
*.swp
*.swo

# OS files
.DS_Store
Thumbs.db

# Database
*.sqlite
*.sqlite3

# Test coverage
coverage
4 changes: 4 additions & 0 deletions auto-chain-agent/agents/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OPENAI_API_KEY=<your-openai-api-key>
ANTHROPIC_API_KEY=<your-anthropic-api-key>
TEST_MNEMONIC=<test-mnemonic>
AGENTS_PORT=3000
6 changes: 6 additions & 0 deletions auto-chain-agent/agents/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"watch": ["src"],
"ext": "ts,json",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/index.ts"
}
36 changes: 36 additions & 0 deletions auto-chain-agent/agents/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@auto-chain/agents",
"version": "1.0.0",
"private": true,
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"dev": "tsc && nodemon",
"start": "node dist/index.js"
},
"dependencies": {
"@autonomys/auto-consensus": "0.7.3",
"@autonomys/auto-drive": "0.7.3",
"@autonomys/auto-utils": "0.7.3",
"@langchain/anthropic": "0.3.7",
"@langchain/community": "0.3.11",
"@langchain/core": "0.3.3",
"@langchain/langgraph": "0.2.8",
"@langchain/openai": "0.3.1",
"axios": "^1.7.7",
"dotenv": "^16.4.1",
"express": "5.0.0",
"sqlite": "^5.1.1",
"sqlite3": "^5.1.7",
"winston": "^3.14.2",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/express": "5.0.0",
"@types/node": "22.6.1",
"nodemon": "^3.0.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}
17 changes: 17 additions & 0 deletions auto-chain-agent/agents/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import dotenv from 'dotenv';

dotenv.config();

export const config = {
port: process.env.AGENTS_PORT || 3000,
anthropicApiKey: process.env.ANTHROPIC_API_KEY,
openaiApiKey: process.env.OPENAI_API_KEY,
environment: process.env.NODE_ENV || 'development',
autoConsensus: {
apiKey: process.env.AUTO_CONSENSUS_API_KEY,
},
llmConfig: {
temperature: 0.4,
maxTokens: 1500
}
};
31 changes: 31 additions & 0 deletions auto-chain-agent/agents/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import express from 'express';
import { chainRouter } from './routes/chainAgentRoutes';
import logger from './logger';
import { config } from './config';

const app = express();
const port = config.port || 3000;

app.use(express.json());

// Routes
app.use('/chainagent', chainRouter);

// Health check route
app.get('/', (req, res) => {
logger.info('Health check request received');
res.send('Blockchain Agent Service is running!');
});

// Error handling middleware
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.error('Unhandled error:', err);
res.status(500).json({
error: 'An error occurred while processing your request',
details: err.message
});
});

app.listen(port, () => {
logger.info(`Blockchain agent service is running on http://localhost:${port}`);
});
38 changes: 38 additions & 0 deletions auto-chain-agent/agents/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import winston from 'winston';
import { config } from './config';

const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.colorize(),
winston.format.printf(({ level, message, timestamp }) => {
return `${timestamp} [${level}]: ${message}`;
})
),
transports: [
new winston.transports.File({
filename: 'error.log',
level: 'error',
format: winston.format.json()
}),
new winston.transports.File({
filename: 'combined.log',
format: winston.format.json()
})
]
});

if (config.environment !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
winston.format.printf(({ level, message, timestamp }) => {
return `${timestamp} [${level}]: ${message}`;
})
)
}));
}

export default logger;
62 changes: 62 additions & 0 deletions auto-chain-agent/agents/src/routes/chainAgentRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import express from 'express';
import { blockchainAgent } from '../services/chainAgent';
import logger from '../logger';

const router = express.Router();

router.post('/', (req, res, next) => {
logger.info('Received blockchain request:', req.body);
(async () => {
try {
const { message } = req.body;

if (!message) {
logger.warn('Missing message field');
return res.status(400).json({ error: 'Message is required' });
}

const result = await blockchainAgent.handleMessage({
message
});

res.json({
threadId: result.threadId,
response: result.response,
toolCalls: result.toolCalls
});
} catch (error) {
logger.error('Error in blockchain endpoint:', error);
next(error);
}
})();
});

router.get('/:threadId/state', (req, res, next) => {
logger.info('Received request to get blockchain thread state:', req.params.threadId);
(async () => {
try {
const threadId = req.params.threadId;
const threadState = await blockchainAgent.getThreadState(threadId);

if (!threadState) {
return res.status(404).json({
error: 'Thread not found'
});
}

res.json({
threadId,
state: {
messages: threadState.state.messages,
toolCalls: threadState.state.toolCalls,
lastOutput: threadState.lastOutput
}
});
} catch (error) {
logger.error('Error getting thread state:', error);
next(error);
}
})();
});

export const chainRouter = router;
Loading

0 comments on commit 833712d

Please sign in to comment.