-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06b5396
commit 9e46c9c
Showing
7 changed files
with
476 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.