@mongoosejs/studio 0.0.76 → 0.0.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,7 +27,7 @@ module.exports = ({ db }) => async function executeScript(params) {
27
27
 
28
28
  // Create a sandbox with the db object
29
29
  const logs = [];
30
- const sandbox = { db, console: {} };
30
+ const sandbox = { db, console: {}, ObjectId: mongoose.Types.ObjectId };
31
31
 
32
32
  // Capture console logs
33
33
  sandbox.console.log = function() {
@@ -13,6 +13,10 @@ const CreateChatMessageParams = new Archetype({
13
13
  },
14
14
  content: {
15
15
  $type: String
16
+ },
17
+ authorization: {
18
+ $type: 'string',
19
+ $required: true
16
20
  }
17
21
  }).compile('CreateChatMessageParams');
18
22
 
@@ -23,6 +27,8 @@ Keep scripts concise. Avoid unnecessary comments, error handling, and temporary
23
27
 
24
28
  Do not write any imports or require() statements, that will cause the script to break.
25
29
 
30
+ If the user approves the script, the script will run in the Node.js server and 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.
31
+
26
32
  Assume the user has pre-defined schemas and models. Do not define any new schemas or models for the user.
27
33
 
28
34
  Use async/await where possible. Assume top-level await is allowed.
@@ -33,7 +39,7 @@ Format output as Markdown, including code fences for any scripts the user reques
33
39
 
34
40
  Add a brief text description of what the script does.
35
41
 
36
- 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.
42
+ 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.
37
43
 
38
44
  Example output:
39
45
 
@@ -50,7 +56,7 @@ Here is a description of the user's models. Assume these are the only models ava
50
56
  `.trim();
51
57
 
52
58
  module.exports = ({ db }) => async function createChatMessage(params) {
53
- const { chatThreadId, userId, content, script } = new CreateChatMessageParams(params);
59
+ const { chatThreadId, userId, content, script, authorization } = new CreateChatMessageParams(params);
54
60
  const ChatThread = db.model('__Studio_ChatThread');
55
61
  const ChatMessage = db.model('__Studio_ChatMessage');
56
62
 
@@ -74,8 +80,8 @@ module.exports = ({ db }) => async function createChatMessage(params) {
74
80
  getChatCompletion([
75
81
  { role: 'system', content: 'Summarize the following chat thread in 6 words or less, as a helpful thread title' },
76
82
  ...llmMessages
77
- ]).then(res => {
78
- const title = res.choices?.[0]?.message?.content;
83
+ ], authorization).then(res => {
84
+ const title = res.response;
79
85
  chatThread.title = title;
80
86
  return chatThread.save();
81
87
  }).catch(() => {});
@@ -95,9 +101,8 @@ module.exports = ({ db }) => async function createChatMessage(params) {
95
101
  script,
96
102
  executionResult: null
97
103
  }),
98
- getChatCompletion(llmMessages).then(res => {
99
- const content = res.choices?.[0]?.message?.content || '';
100
- console.log('Content', content, res);
104
+ getChatCompletion(llmMessages, authorization).then(res => {
105
+ const content = res.response;
101
106
  return ChatMessage.create({
102
107
  chatThreadId,
103
108
  role: 'assistant',
@@ -109,20 +114,27 @@ module.exports = ({ db }) => async function createChatMessage(params) {
109
114
  return { chatMessages };
110
115
  };
111
116
 
112
- async function getChatCompletion(messages, options = {}) {
113
- const response = await fetch('https://api.openai.com/v1/chat/completions', {
117
+ async function getChatCompletion(messages, authorization) {
118
+ const response = await fetch('https://mongoose-js.netlify.app/.netlify/functions/createChatMessage', {
114
119
  method: 'POST',
115
120
  headers: {
116
- Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
121
+ Authorization: authorization,
117
122
  'Content-Type': 'application/json'
118
123
  },
119
124
  body: JSON.stringify({
120
- model: 'gpt-4o',
121
- max_tokens: 2500,
122
- ...options,
123
125
  messages
124
126
  })
127
+ }).then(response => {
128
+ if (response.status < 200 || response.status >= 400) {
129
+ return response.json().then(data => {
130
+ throw new Error(`Mongoose Studio chat completion error: ${data.message}`);
131
+ });
132
+ }
133
+ return response;
125
134
  });
126
135
 
127
- return await response.json();
128
- };
136
+ return await response.json().then(res => {
137
+ console.log('Response', res);
138
+ return res;
139
+ });
140
+ }
package/express.js CHANGED
@@ -63,6 +63,7 @@ module.exports = async function(apiUrl, conn, options) {
63
63
  if (!user || !roles) {
64
64
  throw new Error('Not authorized');
65
65
  }
66
+ req.params.authorization = authorization;
66
67
  req.params.initiatedById = user._id;
67
68
  req.params.roles = roles;
68
69
 
@@ -3767,7 +3767,7 @@ module.exports = "<div>\n <div class=\"mb-2\">\n <textarea class=\"border bo
3767
3767
  /***/ ((module) => {
3768
3768
 
3769
3769
  "use strict";
3770
- module.exports = "<div class=\"py-2\">\n <div v-if=\"header\" class=\"border-b border-gray-100 px-2 pb-2 text-xl font-bold\">\n {{header}}\n </div>\n <div class=\"text-xl py-2\">\n <canvas ref=\"chart\"></canvas>\n </div>\n</div>";
3770
+ module.exports = "<div class=\"py-2 h-full\">\n <div v-if=\"header\" class=\"border-b border-gray-100 px-2 pb-2 text-xl font-bold\">\n {{header}}\n </div>\n <div class=\"text-xl py-2 h-full\">\n <canvas ref=\"chart\"></canvas>\n </div>\n</div>\n";
3771
3771
 
3772
3772
  /***/ }),
3773
3773
 
@@ -1,8 +1,8 @@
1
- <div class="py-2">
1
+ <div class="py-2 h-full">
2
2
  <div v-if="header" class="border-b border-gray-100 px-2 pb-2 text-xl font-bold">
3
3
  {{header}}
4
4
  </div>
5
- <div class="text-xl py-2">
5
+ <div class="text-xl py-2 h-full">
6
6
  <canvas ref="chart"></canvas>
7
7
  </div>
8
- </div>
8
+ </div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mongoosejs/studio",
3
- "version": "0.0.76",
3
+ "version": "0.0.78",
4
4
  "dependencies": {
5
5
  "archetype": "0.13.1",
6
6
  "csv-stringify": "6.3.0",