Pepguy commited on
Commit
7928c15
·
verified ·
1 Parent(s): ae32deb

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +11 -4
app.js CHANGED
@@ -67,13 +67,16 @@ app.post('/api/generate', async (req, res) => {
67
  }
68
  });
69
 
70
- // --- NEW STREAMING ENDPOINT ---
71
  app.post('/api/stream', async (req, res) => {
72
  const { model, prompt, system_prompt } = req.body;
73
  console.log(`[STREAM] Request for ${model}`);
74
 
75
- res.setHeader('Content-Type', 'text/plain');
 
76
  res.setHeader('Transfer-Encoding', 'chunked');
 
 
 
77
 
78
  try {
79
  if (model === "claude") {
@@ -92,7 +95,11 @@ app.post('/api/stream', async (req, res) => {
92
  for await (const chunk of response.stream) {
93
  if (chunk.contentBlockDelta) {
94
  const text = chunk.contentBlockDelta.delta?.text;
95
- if (text) res.write(text);
 
 
 
 
96
  }
97
  }
98
  res.end();
@@ -123,4 +130,4 @@ app.post('/', async (req, res) => {
123
  res.json({ success: true, status: 200 });
124
  });
125
 
126
- app.listen(PORT, '0.0.0.0', () => console.log(`Main AI Agent live on port ${PORT}`));
 
67
  }
68
  });
69
 
 
70
  app.post('/api/stream', async (req, res) => {
71
  const { model, prompt, system_prompt } = req.body;
72
  console.log(`[STREAM] Request for ${model}`);
73
 
74
+ // Headers to force immediate flushing and disable buffering on Nginx/Cloudflare
75
+ res.setHeader('Content-Type', 'text/plain; charset=utf-8');
76
  res.setHeader('Transfer-Encoding', 'chunked');
77
+ res.setHeader('X-Accel-Buffering', 'no');
78
+ res.setHeader('Cache-Control', 'no-cache, no-transform');
79
+ res.flushHeaders(); // Ensure headers are sent immediately
80
 
81
  try {
82
  if (model === "claude") {
 
95
  for await (const chunk of response.stream) {
96
  if (chunk.contentBlockDelta) {
97
  const text = chunk.contentBlockDelta.delta?.text;
98
+ if (text) {
99
+ res.write(text);
100
+ // In some Node setups with compression, we might need manual flushing,
101
+ // but usually res.write works with disabled buffering headers.
102
+ }
103
  }
104
  }
105
  res.end();
 
130
  res.json({ success: true, status: 200 });
131
  });
132
 
133
+ app.listen(PORT, '0.0.0.0', () => console.log(`Main AI Agent live on port ${PORT}`));