@saltcorn/sql 0.5.1 → 0.5.3

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.
Files changed (2) hide show
  1. package/agent-skill.js +103 -13
  2. package/package.json +1 -1
package/agent-skill.js CHANGED
@@ -4,10 +4,12 @@ const Form = require("@saltcorn/data/models/form");
4
4
  const Table = require("@saltcorn/data/models/table");
5
5
  const View = require("@saltcorn/data/models/view");
6
6
  const Trigger = require("@saltcorn/data/models/trigger");
7
+ const FieldRepeat = require("@saltcorn/data/models/fieldrepeat");
7
8
  const { getState } = require("@saltcorn/data/db/state");
8
9
  const db = require("@saltcorn/data/db");
9
10
  const { eval_expression } = require("@saltcorn/data/models/expression");
10
11
  const { interpolate } = require("@saltcorn/data/utils");
12
+ const { features } = require("@saltcorn/data/db/state");
11
13
 
12
14
  //const { fieldProperties } = require("./helpers");
13
15
 
@@ -28,17 +30,22 @@ class SQLQuerySkill {
28
30
  const is_sqlite = db.isSQLite;
29
31
 
30
32
  const phValues = [];
31
- (this.query_parameters || "")
32
- .split(",")
33
- .filter((s) => s)
34
- .forEach((sp0) => {
35
- const sp = sp0.trim();
36
- if (sp.startsWith("user.")) {
37
- phValues.push(eval_expression(sp, {}, user));
38
- } else if (typeof row[sp] === "undefined") phValues.push(null);
39
- else phValues.push(row[sp]);
33
+ if (this.mode === "Preload into system prompt") {
34
+ (this.query_parameters || "")
35
+ .split(",")
36
+ .filter((s) => s)
37
+ .forEach((sp0) => {
38
+ const sp = sp0.trim();
39
+ if (sp.startsWith("user.")) {
40
+ phValues.push(eval_expression(sp, {}, user));
41
+ } else if (typeof row[sp] === "undefined") phValues.push(null);
42
+ else phValues.push(row[sp]);
43
+ });
44
+ } else {
45
+ (this.toolargs || []).forEach((arg) => {
46
+ phValues.push(row[arg.name]);
40
47
  });
41
-
48
+ }
42
49
  const client = is_sqlite ? db : await db.getClient();
43
50
  await client.query(`BEGIN;`);
44
51
  if (!is_sqlite) {
@@ -77,23 +84,72 @@ class SQLQuerySkill {
77
84
  label: "Mode",
78
85
  type: "String",
79
86
  required: true,
80
- attributes: { options: [/*"Tool",*/ "Preload into system prompt"] },
87
+ attributes: {
88
+ options: features.nested_fieldrepeats
89
+ ? ["Preload into system prompt", "Tool"]
90
+ : ["Preload into system prompt"],
91
+ },
92
+ },
93
+ {
94
+ name: "tool_name",
95
+ label: "Tool name",
96
+ type: "String",
97
+ showIf: { mode: "Tool" },
98
+ class: "validate-identifier",
99
+ },
100
+ {
101
+ name: "tool_description",
102
+ label: "Tool description",
103
+ type: "String",
104
+ showIf: { mode: "Tool" },
81
105
  },
106
+
82
107
  {
83
108
  name: "sql",
84
109
  label: "SQL",
85
110
  input_type: "code",
86
111
  attributes: { mode: "text/x-sql" },
87
112
  sublabel:
88
- "Refer to row parameters in the order below with <code>$1</code>, <code>$2</code> etc",
113
+ "Refer to query parameters with <code>$1</code>, <code>$2</code> etc",
89
114
  },
115
+ { input_type: "section_header", label: "Query parameters" },
116
+ new FieldRepeat({
117
+ name: "toolargs",
118
+ showIf: { mode: "Tool" },
119
+ fields: [
120
+ {
121
+ name: "name",
122
+ label: "Name",
123
+ type: "String",
124
+ },
125
+ {
126
+ name: "description",
127
+ label: "Description",
128
+ type: "String",
129
+ },
130
+ {
131
+ name: "argtype",
132
+ label: "Type",
133
+ type: "String",
134
+ required: true,
135
+ attributes: { options: ["string", "number", "integer", "boolean"] },
136
+ },
137
+ {
138
+ name: "options",
139
+ label: "Options",
140
+ type: "String",
141
+ sublabel: "Optional. Comma-separated list of values",
142
+ showIf: { argtype: "string" },
143
+ },
144
+ ],
145
+ }),
90
146
  {
91
147
  name: "query_parameters",
92
148
  label: "Query parameters",
93
149
  sublabel:
94
150
  "Comma separated list of variables to use as SQL query parameters. User variables can be used as <code>user.id</code> etc",
95
151
  type: "String",
96
- //showIf: { mode: "Tool" },
152
+ showIf: { mode: "Preload into system prompt" },
97
153
  },
98
154
  {
99
155
  name: "add_sys_prompt",
@@ -111,6 +167,40 @@ class SQLQuerySkill {
111
167
  },
112
168
  ];
113
169
  }
170
+
171
+ provideTools = () => {
172
+ if (this.mode === "Preload into system prompt") return null;
173
+ let properties = {};
174
+ (this.toolargs || []).forEach((arg) => {
175
+ properties[arg.name] = {
176
+ description: arg.description,
177
+ type: arg.argtype,
178
+ };
179
+ if (arg.options && arg.argtype === "string")
180
+ properties[arg.name].enum = arg.options.split(",").map((s) => s.trim());
181
+ });
182
+ return {
183
+ type: "function",
184
+ process: async (row, { req }) => {
185
+ return await this.runQuery({ triggering_row: row });
186
+ },
187
+ /*renderToolCall({ phrase }, { req }) {
188
+ return div({ class: "border border-primary p-2 m-2" }, phrase);
189
+ },*/
190
+ renderToolResponse: async (response, { req }) => {
191
+ return div({ class: "border border-success p-2 m-2" }, response);
192
+ },
193
+ function: {
194
+ name: this.tool_name,
195
+ description: this.tool_description,
196
+ parameters: {
197
+ type: "object",
198
+ required: (this.toolargs || []).map((a) => a.name),
199
+ properties,
200
+ },
201
+ },
202
+ };
203
+ };
114
204
  }
115
205
 
116
206
  module.exports = SQLQuerySkill;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/sql",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "Actions and views based on SQL",
5
5
  "main": "index.js",
6
6
  "dependencies": {