Skip to content

Commit cc28c54

Browse files
committed
fix for vercel
1 parent 5fdcf87 commit cc28c54

File tree

3 files changed

+64
-47
lines changed

3 files changed

+64
-47
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const Archetype = require('archetype');
4+
const authorize = require('../../authorize');
5+
const callLLM = require('../../integrations/callLLM');
6+
const getModelDescriptions = require('../../helpers/getModelDescriptions');
7+
8+
const CreateChatMessageParams = new Archetype({
9+
model: {
10+
$type: 'string',
11+
$required: true
12+
},
13+
content: {
14+
$type: 'string',
15+
$required: true
16+
},
17+
documentData: {
18+
$type: 'string'
19+
},
20+
roles: {
21+
$type: ['string']
22+
}
23+
}).compile('CreateChatMessageParams');
24+
25+
module.exports = ({ db, options }) => async function createChatMessage(params) {
26+
const { model, content, documentData, roles } = new CreateChatMessageParams(params);
27+
28+
await authorize('Model.createChatMessage', roles);
29+
30+
const Model = db.models[model];
31+
if (Model == null) {
32+
throw new Error(`Model ${model} not found`);
33+
}
34+
35+
const modelDescriptions = getModelDescriptions({ models: { [Model.modelName]: Model } });
36+
const context = [
37+
modelDescriptions,
38+
'Current draft document:\n' + (documentData || '')
39+
].join('\n\n');
40+
const system = systemPrompt + '\n\n' + context + (options?.context ? '\n\n' + options.context : '');
41+
42+
const llmMessages = [{ role: 'user', content: [{ type: 'text', text: content }] }];
43+
const res = await callLLM(llmMessages, system, options);
44+
45+
return { text: res.text };
46+
};
47+
48+
const systemPrompt = `
49+
You are a helpful assistant that drafts MongoDB documents for the user.
50+
51+
Use the model description and the current draft document to refine the user's intent.
52+
53+
Return only the updated document body as a JavaScript object literal. Do not use Markdown or code fences.
54+
`.trim();

backend/actions/Model/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
exports.addField = require('./addField');
4+
exports.createChatMessage = require('./createChatMessage');
45
exports.createDocument = require('./createDocument');
56
exports.deleteDocument = require('./deleteDocument');
67
exports.deleteDocuments = require('./deleteDocuments');

frontend/src/api.js

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -147,54 +147,13 @@ if (window.MONGOOSE_STUDIO_CONFIG.isLambda) {
147147
updateDocument: function updateDocument(params) {
148148
return client.post('', { action: 'Model.updateDocument', ...params }).then(res => res.data);
149149
},
150+
createChatMessage(params) {
151+
return client.post('', { action: 'Model.createChatMessage', ...params }).then(res => res.data);
152+
},
150153
streamChatMessage: async function* streamChatMessage(params) {
151-
const accessToken = window.localStorage.getItem('_mongooseStudioAccessToken') || null;
152-
const url = window.MONGOOSE_STUDIO_CONFIG.baseURL + '?' + new URLSearchParams({ action: 'Model.streamChatMessage', ...params }).toString();
153-
154-
const response = await fetch(url, {
155-
method: 'GET',
156-
headers: {
157-
Authorization: `${accessToken}`,
158-
Accept: 'text/event-stream'
159-
}
160-
});
161-
162-
if (!response.ok) {
163-
throw new Error(`HTTP error! Status: ${response.status}`);
164-
}
165-
166-
const reader = response.body.getReader();
167-
const decoder = new TextDecoder('utf-8');
168-
let buffer = '';
169-
170-
while (true) {
171-
const { done, value } = await reader.read();
172-
if (done) break;
173-
buffer += decoder.decode(value, { stream: true });
174-
175-
let eventEnd;
176-
while ((eventEnd = buffer.indexOf('\n\n')) !== -1) {
177-
const eventStr = buffer.slice(0, eventEnd);
178-
buffer = buffer.slice(eventEnd + 2);
179-
180-
// Parse SSE event
181-
const lines = eventStr.split('\n');
182-
let data = '';
183-
for (const line of lines) {
184-
if (line.startsWith('data:')) {
185-
data += line.slice(5).trim();
186-
}
187-
}
188-
if (data) {
189-
try {
190-
const res = JSON.parse(data);
191-
yield res;
192-
} catch (err) {
193-
yield data;
194-
}
195-
}
196-
}
197-
}
154+
// Don't stream on Next.js or Netlify for now.
155+
const data = await client.post('', { action: 'Model.createChatMessage', ...params }).then(res => res.data);
156+
yield { textPart: data.text };
198157
},
199158
updateDocuments: function updateDocuments(params) {
200159
return client.post('', { action: 'Model.updateDocuments', ...params }).then(res => res.data);
@@ -299,6 +258,9 @@ if (window.MONGOOSE_STUDIO_CONFIG.isLambda) {
299258
createChart: function(params) {
300259
return client.post('/Model/createChart', params).then(res => res.data);
301260
},
261+
createChatMessage: function(params) {
262+
return client.post('/Model/createChatMessage', params).then(res => res.data);
263+
},
302264
createDocument: function(params) {
303265
return client.post('/Model/createDocument', params).then(res => res.data);
304266
},

0 commit comments

Comments
 (0)