Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56d0c21b79 | ||
|
|
37e81e773e | ||
|
|
534da11217 | ||
|
|
2212755772 | ||
|
|
f2e1390507 | ||
|
|
31496876a2 |
@@ -10,7 +10,7 @@ app.use(express.json());
|
||||
app.use('/v1/notes', router);
|
||||
|
||||
app.use((req, res) => {
|
||||
res.status(404).json({ error: `Requested path is invalid or does not exist: ${req.method} ${req.originalUrl}` });
|
||||
});
|
||||
res.status(404).json({ error: `Requested path is invalid or does not exist: ${req.method} ${req.originalUrl}` });
|
||||
});
|
||||
|
||||
export default app;
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
import { Router } from 'express';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname, join } from 'path';
|
||||
import { clusterNotes } from '../services/clustering.service.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
const DATA_PATH = join(__dirname, '..', 'data', 'notes.json');
|
||||
const DATA_PATH = '../data/notes.json';
|
||||
|
||||
const loadNotes = async () => {
|
||||
const raw = await readFile(DATA_PATH, 'utf-8');
|
||||
@@ -20,7 +15,7 @@ router.get('/', async (req, res) => {
|
||||
const notes = await loadNotes();
|
||||
res.json(notes);
|
||||
} catch (err) {
|
||||
console.error(`Error loading notes: ${err}`)
|
||||
console.error(`Error loading notes: ${err}`);
|
||||
res.status(500).json({ error: 'Failed to load notes' });
|
||||
}
|
||||
});
|
||||
@@ -31,7 +26,7 @@ router.post('/cluster', async (req, res) => {
|
||||
const clusters = await clusterNotes(notes);
|
||||
res.json(clusters);
|
||||
} catch (err) {
|
||||
console.error(`Clustering failed: ${err}` )
|
||||
console.error(`Clustering failed: ${err}`);
|
||||
res.status(500).json({ error: `Clustering failed: ${err}` });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -40,6 +40,15 @@ export const clusterNotes = async (notes) => {
|
||||
],
|
||||
});
|
||||
|
||||
const raw = response.content[0].text;
|
||||
return JSON.parse(raw);
|
||||
const textBlock = response?.content?.[0];
|
||||
|
||||
if (!textBlock || textBlock.type !== 'text' || typeof textBlock.text !== 'string') {
|
||||
throw new Error('Unexpected response from LLM API: no text content returned');
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(textBlock.text);
|
||||
} catch {
|
||||
throw new Error('LLM API returned non-JSON response');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -35,7 +35,7 @@ describe('clusterNotes service', () => {
|
||||
|
||||
it('should call Anthropic messages.create with the correct model', async () => {
|
||||
createMock.mockResolvedValue({
|
||||
content: [{ text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||
content: [{ type: 'text', text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||
});
|
||||
|
||||
await clusterNotes(MOCK_NOTES);
|
||||
@@ -48,7 +48,7 @@ describe('clusterNotes service', () => {
|
||||
|
||||
it('should include all note texts in prompt sent to the LLM API', async () => {
|
||||
createMock.mockResolvedValue({
|
||||
content: [{ text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||
content: [{ type: 'text', text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||
});
|
||||
|
||||
await clusterNotes(MOCK_NOTES);
|
||||
@@ -62,7 +62,7 @@ describe('clusterNotes service', () => {
|
||||
|
||||
it('should parse and return the clustered JSON from the API response', async () => {
|
||||
createMock.mockResolvedValue({
|
||||
content: [{ text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||
content: [{ type: 'text', text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||
});
|
||||
|
||||
const result = await clusterNotes(MOCK_NOTES);
|
||||
@@ -72,10 +72,16 @@ describe('clusterNotes service', () => {
|
||||
|
||||
it('should throw error when the API returns non-JSON', async () => {
|
||||
createMock.mockResolvedValue({
|
||||
content: [{ text: 'An unknown error occured when generting structured response.' }],
|
||||
content: [{ type: 'text', text: 'An unknown error occured when generting structured response.' }],
|
||||
});
|
||||
|
||||
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow();
|
||||
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('non-JSON response');
|
||||
});
|
||||
|
||||
it('should throw error when the API response has no text content', async () => {
|
||||
createMock.mockResolvedValue({ content: [] });
|
||||
|
||||
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('no text content returned');
|
||||
});
|
||||
|
||||
it('should throw error when Anthropic API authentication fails', async () => {
|
||||
|
||||
Reference in New Issue
Block a user