Skip to content

Commit

Permalink
refactor agents project
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrank-summit committed Oct 26, 2024
1 parent 06b5396 commit 9e46c9c
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 448 deletions.
29 changes: 29 additions & 0 deletions auto-content-creator/agents/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import dotenv from 'dotenv';

dotenv.config();

export const config = {
port: process.env.AGENTS_PORT || 3000,
openaiApiKey: process.env.OPENAI_API_KEY,
serpapiApiKey: process.env.SERPAPI_API_KEY,
model: 'gpt-4o-mini',
environment: process.env.NODE_ENV || 'development',
llmConfig: {
generation: {
temperature: 0.7,
maxTokens: 2000
},
reflection: {
temperature: 0.2,
maxTokens: 1000
},
research: {
temperature: 0.2,
maxTokens: 1000
},
feedback: {
temperature: 0.4,
maxTokens: 1500
}
}
};
96 changes: 15 additions & 81 deletions auto-content-creator/agents/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,34 @@
import express, { Request, Response, NextFunction } from 'express';
import { writerAgent } from './writerAgent';
import dotenv from 'dotenv';

dotenv.config();
import { writerRouter } from './routes/writer';
import logger from './logger';
import { config } from './config';

// Create an Express application
const app = express();

// Add JSON parsing middleware
app.use(express.json());

// Specify the port number for the server
const port: number = process.env.AGENTS_PORT ? parseInt(process.env.AGENTS_PORT) : 3000;

// Add this new endpoint for feedback
app.post('/writer/:threadId/feedback', (req: Request, res: Response, next: NextFunction) => {
console.log('Received feedback request:', req.body);
(async () => {
try {
const { feedback } = req.body;
const threadId = req.params.threadId;
const port = config.port;

if (!feedback) {
return res.status(400).json({ error: 'Feedback is required' });
}

console.log('Processing feedback for thread:', threadId);

const result = await writerAgent.continueDraft({
threadId,
feedback,
});

// Only return the latest content
res.json({
threadId,
content: result.finalContent
});
} catch (error) {
console.error('Error in feedback endpoint:', error);
next(error);
}
})();
});

// Modify the original /writer endpoint to return threadId
app.post('/writer', (req: Request, res: Response, next: NextFunction) => {
console.log('Received request body:', req.body);
(async () => {
try {
const { category, topic, contentType, otherInstructions } = req.body;

if (!category || !topic || !contentType) {
console.log('Category, topic, and contentType are required');
return res.status(400).json({ error: 'Category, topic, and contentType are required' });
}
app.use(express.json());

const result = await writerAgent.startDraft({
category,
topic,
contentType,
otherInstructions,
});
// Routes
app.use('/writer', writerRouter);

res.json({
threadId: result.threadId,
finalContent: result.finalContent,
research: result.research,
reflections: result.reflections,
drafts: result.drafts,
feedbackHistory: result.feedbackHistory,
});
} catch (error) {
console.error('Error in /writer endpoint:', error);
next(error);
}
})();
// Health check route
app.get('/', (req, res) => {
logger.info('Health check request received');
res.send('Auto Content Creator Agents Service is running!');
});

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

// Add a simple route for testing
app.get('/', (req, res) => {
console.log('Received request on root route');
res.send('Auto Content Creator Backend is running!');
});

// Start the server and listen on the specified port
// Start server
app.listen(port, () => {
// Log a message when the server is successfully running
console.log(`Server is running on http://localhost:${port}`);
logger.info(`Agents service is running on http://localhost:${port}`);
});
5 changes: 3 additions & 2 deletions auto-content-creator/agents/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import winston from 'winston';
import { config } from './config';

const logger = winston.createLogger({
level: 'info',
Expand All @@ -8,14 +9,14 @@ const logger = winston.createLogger({
winston.format.splat(),
winston.format.json()
),
defaultMeta: { service: 'writer-agent' },
defaultMeta: { service: 'writer-agent', environment: config.environment },
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
});

if (process.env.NODE_ENV !== 'production') {
if (config.environment !== 'production') {
logger.add(
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
Expand Down
68 changes: 68 additions & 0 deletions auto-content-creator/agents/src/routes/writer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import express from 'express';
import { writerAgent } from '../services/writerAgent';
import logger from '../logger';

const router = express.Router();

router.post('/', (req, res, next) => {
logger.info('Received request to start new draft:', req.body);
(async () => {
try {
const { category, topic, contentType, otherInstructions } = req.body;

if (!category || !topic || !contentType) {
logger.warn('Missing required fields');
return res.status(400).json({ error: 'Category, topic, and contentType are required' });
}

const result = await writerAgent.startDraft({
category,
topic,
contentType,
otherInstructions,
});

res.json({
threadId: result.threadId,
finalContent: result.finalContent,
research: result.research,
reflections: result.reflections,
drafts: result.drafts,
feedbackHistory: result.feedbackHistory,
});
} catch (error) {
logger.error('Error in writer endpoint:', error);
next(error);
}
})();
});

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

if (!feedback) {
return res.status(400).json({ error: 'Feedback is required' });
}

logger.info('Processing feedback for thread:', threadId);

const result = await writerAgent.continueDraft({
threadId,
feedback,
});

res.json({
content: result.finalContent
});
} catch (error) {
logger.error('Error in feedback endpoint:', error);
next(error);
}
})();
});

export const writerRouter = router;
Loading

0 comments on commit 9e46c9c

Please sign in to comment.