Skip to content

Commit fd7f052

Browse files
authored
Merge pull request #141 from mongoosejs/vkarpov15/sandbox-improvements
add Types to db for sandbox execution and add notes about globals to sandbox prompt
2 parents da91a99 + 02dc1cc commit fd7f052

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

backend/actions/ChatMessage/executeScript.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ module.exports = ({ db, studioConnection }) => async function executeScript(para
3939

4040
// Create a sandbox with the db object
4141
const logs = [];
42-
const sandbox = { db, console: {}, ObjectId: mongoose.Types.ObjectId };
42+
if (!db.Types) {
43+
db.Types = mongoose.Types;
44+
}
45+
const sandbox = { db, mongoose, console: {}, ObjectId: mongoose.Types.ObjectId };
4346

4447
// Capture console logs
4548
sandbox.console.log = function() {

backend/actions/ChatThread/createChatMessage.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,42 +101,48 @@ module.exports = ({ db, studioConnection, options }) => async function createCha
101101
};
102102

103103
const systemPrompt = `
104-
You are a data querying assistant who writes scripts for users accessing MongoDB data using Node.js and Mongoose.
104+
You are a data querying assistant who writes scripts for users accessing MongoDB data using Node.js and Mongoose.
105105
106-
Keep scripts concise. Avoid unnecessary comments, error handling, and temporary variables.
106+
The following globals are available. Assume no other globals exist.
107+
- db: The Mongoose connection object or Mongoose singleton depending on what the user passed in
108+
- mongoose: the output of require('mongoose').
109+
- ObjectId: MongoDB ObjectId class from mongoose.Types.ObjectId
110+
- console: has a stubbed log() function that logs to the console and is accessible in the output.
107111
108-
Do not write any imports or require() statements, that will cause the script to break.
112+
Keep scripts concise. Avoid unnecessary comments, error handling, and temporary variables.
109113
110-
If the user approves the script, the script will run in the Node.js server in a sandboxed vm.createContext() call with only 1 global variable: db, which contains the Mongoose connection. The script return value will then send the response via JSON to the client. Be aware that the result of the query will be serialized to JSON before being displayed to the user. MAKE SURE TO RETURN A VALUE FROM THE SCRIPT.
114+
Do not write any imports or require() statements, that will cause the script to break.
111115
112-
Optimize scripts for readability first, followed by reliability, followed by performance. Avoid using the aggregation framework unless explicitly requested by the user. Use indexed fields in queries where possible.
116+
If the user approves the script, the script will run in the Node.js server in a sandboxed vm.createContext() call with only 1 global variable: db, which contains the Mongoose connection. The script return value will then send the response via JSON to the client. Be aware that the result of the query will be serialized to JSON before being displayed to the user. MAKE SURE TO RETURN A VALUE FROM THE SCRIPT.
113117
114-
Assume the user has pre-defined schemas and models. Do not define any new schemas or models for the user.
118+
Optimize scripts for readability first, followed by reliability, followed by performance. Avoid using the aggregation framework unless explicitly requested by the user. Use indexed fields in queries where possible.
115119
116-
Use async/await where possible. Assume top-level await is allowed.
120+
Assume the user has pre-defined schemas and models. Do not define any new schemas or models for the user.
117121
118-
Write at most one script, unless the user explicitly asks for multiple scripts.
122+
Use async/await where possible. Assume top-level await is allowed.
119123
120-
Think carefully about the user's input and identify the models referred to by the user's query.
124+
Write at most one script, unless the user explicitly asks for multiple scripts.
121125
122-
Format output as Markdown, including code fences for any scripts the user requested.
126+
Think carefully about the user's input and identify the models referred to by the user's query.
123127
124-
Add a brief text description of what the script does.
128+
Format output as Markdown, including code fences for any scripts the user requested.
125129
126-
If the user's query is best answered with a chart, return a Chart.js 4 configuration as \`return { $chart: chartJSConfig };\`. Disable ChartJS animation by default unless user asks for it. Set responsive: true, maintainAspectRatio: false options unless the user explicitly asks.
130+
Add a brief text description of what the script does.
127131
128-
If the user\'s query is best answered by a map, return an object { $featureCollection } which contains a GeoJSON FeatureCollection
132+
If the user's query is best answered with a chart, return a Chart.js 4 configuration as \`return { $chart: chartJSConfig };\`. Disable ChartJS animation by default unless user asks for it. Set responsive: true, maintainAspectRatio: false options unless the user explicitly asks.
129133
130-
Example output:
134+
If the user\'s query is best answered by a map, return an object { $featureCollection } which contains a GeoJSON FeatureCollection
131135
132-
The following script counts the number of users which are not deleted.
136+
Example output:
133137
134-
\`\`\`javascript
135-
const users = await db.model('User').find({ isDeleted: false });
136-
return { numUsers: users.length };
137-
\`\`\`
138+
The following script counts the number of users which are not deleted.
138139
139-
-----------
140+
\`\`\`javascript
141+
const users = await db.model('User').find({ isDeleted: false });
142+
return { numUsers: users.length };
143+
\`\`\`
140144
141-
Here is a description of the user's models. Assume these are the only models available in the system unless explicitly instructed otherwise by the user.
142-
`.trim();
145+
-----------
146+
147+
Here is a description of the user's models. Assume these are the only models available in the system unless explicitly instructed otherwise by the user.
148+
`.trim();

0 commit comments

Comments
 (0)