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

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +27 -12
app.js CHANGED
@@ -67,16 +67,15 @@ app.post('/api/generate', async (req, res) => {
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") {
@@ -92,18 +91,25 @@ app.post('/api/stream', async (req, res) => {
92
  });
93
 
94
  const response = await bedrockClient.send(command);
 
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();
 
106
  } else {
 
107
  const stream = await azureOpenAI.chat.completions.create({
108
  model: "gpt-5-mini",
109
  messages: [
@@ -115,8 +121,16 @@ app.post('/api/stream', async (req, res) => {
115
  });
116
 
117
  for await (const chunk of stream) {
118
- const text = chunk.choices[0]?.delta?.content || "";
119
- if (text) res.write(text);
 
 
 
 
 
 
 
 
120
  }
121
  res.end();
122
  }
@@ -126,8 +140,9 @@ app.post('/api/stream', async (req, res) => {
126
  res.end();
127
  }
128
  });
 
129
  app.post('/', async (req, res) => {
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}`));
 
67
  }
68
  });
69
 
70
+ // --- STREAMING ENDPOINT WITH THINKING SUPPORT ---
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; charset=utf-8');
76
  res.setHeader('Transfer-Encoding', 'chunked');
77
  res.setHeader('X-Accel-Buffering', 'no');
78
+ res.flushHeaders();
 
79
 
80
  try {
81
  if (model === "claude") {
 
91
  });
92
 
93
  const response = await bedrockClient.send(command);
94
+
95
  for await (const chunk of response.stream) {
96
  if (chunk.contentBlockDelta) {
97
+ const delta = chunk.contentBlockDelta.delta;
98
+
99
+ // Handle Bedrock "Thinking" (Reasoning Content)
100
+ if (delta.reasoningContent && delta.reasoningContent.text) {
101
+ res.write(`__THINK__${delta.reasoningContent.text}`);
102
+ }
103
+ // Handle Bedrock "Final Output" (Text)
104
+ else if (delta.text) {
105
+ res.write(delta.text);
106
  }
107
  }
108
  }
109
  res.end();
110
+
111
  } else {
112
+ // Azure/OpenAI Streaming
113
  const stream = await azureOpenAI.chat.completions.create({
114
  model: "gpt-5-mini",
115
  messages: [
 
121
  });
122
 
123
  for await (const chunk of stream) {
124
+ const delta = chunk.choices[0]?.delta;
125
+
126
+ // Handle OpenAI "Thinking"
127
+ if (delta?.reasoning_content) {
128
+ res.write(`__THINK__${delta.reasoning_content}`);
129
+ }
130
+ // Handle OpenAI "Final Output"
131
+ else if (delta?.content) {
132
+ res.write(delta.content);
133
+ }
134
  }
135
  res.end();
136
  }
 
140
  res.end();
141
  }
142
  });
143
+
144
  app.post('/', async (req, res) => {
145
+ res.json({ success: true, data: response.choices[0].message.content });
146
  });
147
 
148
+ app.listen(PORT, '0.0.0.0', () => console.log(`Main AI Agent live on port ${PORT}`));