@saltcorn/sql 0.5.1 → 0.5.2

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 +93 -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,64 @@ 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
+ },
99
+ {
100
+ name: "tool_description",
101
+ label: "Tool description",
102
+ type: "String",
103
+ showIf: { mode: "Tool" },
81
104
  },
105
+
82
106
  {
83
107
  name: "sql",
84
108
  label: "SQL",
85
109
  input_type: "code",
86
110
  attributes: { mode: "text/x-sql" },
87
111
  sublabel:
88
- "Refer to row parameters in the order below with <code>$1</code>, <code>$2</code> etc",
112
+ "Refer to query parameters with <code>$1</code>, <code>$2</code> etc",
89
113
  },
114
+ { input_type: "section_header", label: "Query parameters" },
115
+ new FieldRepeat({
116
+ name: "toolargs",
117
+ showIf: { mode: "Tool" },
118
+ fields: [
119
+ {
120
+ name: "name",
121
+ label: "Name",
122
+ type: "String",
123
+ },
124
+ {
125
+ name: "description",
126
+ label: "Description",
127
+ type: "String",
128
+ },
129
+ {
130
+ name: "argtype",
131
+ label: "Type",
132
+ type: "String",
133
+ required: true,
134
+ attributes: { options: ["string", "number", "integer", "boolean"] },
135
+ },
136
+ ],
137
+ }),
90
138
  {
91
139
  name: "query_parameters",
92
140
  label: "Query parameters",
93
141
  sublabel:
94
142
  "Comma separated list of variables to use as SQL query parameters. User variables can be used as <code>user.id</code> etc",
95
143
  type: "String",
96
- //showIf: { mode: "Tool" },
144
+ showIf: { mode: "Preload into system prompt" },
97
145
  },
98
146
  {
99
147
  name: "add_sys_prompt",
@@ -111,6 +159,38 @@ class SQLQuerySkill {
111
159
  },
112
160
  ];
113
161
  }
162
+
163
+ provideTools = () => {
164
+ if (this.mode === "Preload into system prompt") return null;
165
+ let properties = {};
166
+ (this.toolargs || []).forEach((arg) => {
167
+ properties[arg.name] = {
168
+ description: arg.description,
169
+ type: arg.argtype,
170
+ };
171
+ });
172
+ return {
173
+ type: "function",
174
+ process: async (row, { req }) => {
175
+ return await this.runQuery({ triggering_row: row });
176
+ },
177
+ /*renderToolCall({ phrase }, { req }) {
178
+ return div({ class: "border border-primary p-2 m-2" }, phrase);
179
+ },*/
180
+ renderToolResponse: async (response, { req }) => {
181
+ return div({ class: "border border-success p-2 m-2" }, response);
182
+ },
183
+ function: {
184
+ name: this.tool_name,
185
+ description: this.tool_description,
186
+ parameters: {
187
+ type: "object",
188
+ required: (this.toolargs || []).map((a) => a.name),
189
+ properties,
190
+ },
191
+ },
192
+ };
193
+ };
114
194
  }
115
195
 
116
196
  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.2",
4
4
  "description": "Actions and views based on SQL",
5
5
  "main": "index.js",
6
6
  "dependencies": {