@saltcorn/copilot 0.7.3 → 0.7.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/copilot",
3
- "version": "0.7.3",
3
+ "version": "0.7.5",
4
4
  "description": "AI assistant for building Saltcorn applications",
5
5
  "main": "index.js",
6
6
  "dependencies": {
@@ -0,0 +1,83 @@
1
+ const { getState } = require("@saltcorn/data/db/state");
2
+ const WorkflowStep = require("@saltcorn/data/models/workflow_step");
3
+ const Trigger = require("@saltcorn/data/models/trigger");
4
+ const Table = require("@saltcorn/data/models/table");
5
+ const { getActionConfigFields } = require("@saltcorn/data/plugin-helper");
6
+ const { getPromptFromTemplate } = require("./common");
7
+
8
+ const scTypeToTsType = (type, field) => {
9
+ if (field?.is_fkey) {
10
+ if (field.reftype) return scTypeToTsType(field.reftype);
11
+ }
12
+ return (
13
+ {
14
+ String: "string",
15
+ Integer: "number",
16
+ Float: "number",
17
+ Bool: "boolean",
18
+ Date: "Date",
19
+ HTML: "string",
20
+ }[type?.name || type] || "any"
21
+ );
22
+ };
23
+
24
+ module.exports = {
25
+ run: async ({ table, language, has_table, has_functions }) => {
26
+ if (language === "javascript") {
27
+ let prompts = [``];
28
+ if (has_table)
29
+ prompts.push(await getPromptFromTemplate("action-builder.txt", ""));
30
+ if (!has_table)
31
+ prompts.push(`Your code can can manipulate rows in the database, manipulate files, interact
32
+ with remote APIs, or issue directives for the user's display.
33
+
34
+ Your code can use await at the top level, and should do so whenever calling
35
+ database queries or other aynchronous code (see examples below)
36
+ `);
37
+ if (has_functions) {
38
+ const ds = [];
39
+ for (const [nm, f] of Object.entries(getState().functions)) {
40
+ const comment = f.description ? " // " + f.description : "";
41
+ const returns =
42
+ f.returns || f.tsreturns
43
+ ? ": " + (f.tsreturns || scTypeToTsType(f.returns))
44
+ : "";
45
+ if (nm === "today") {
46
+ ds.push(
47
+ `function today(offset_days?: number | {startOf: "year" | "quarter" | "month" | "week" | "day" | "hour"} | {endOf: "year" | "quarter" | "month" | "week" | "day" | "hour"}): Date`,
48
+ );
49
+ }
50
+ if (nm === "slugify") {
51
+ ds.push(`function slugify(s: string): string`);
52
+ } else if (f.run) {
53
+ if (f["arguments"]) {
54
+ const args = (f["arguments"] || []).map(
55
+ ({ name, type, tstype, required }) =>
56
+ `${name}${required ? "" : "?"}: ${tstype || scTypeToTsType(type)}`,
57
+ );
58
+ ds.push(
59
+ `${f.isAsync ? "async " : ""}function ${nm}(${args.join(", ")})${returns}${comment}`,
60
+ );
61
+ } else
62
+ ds.push(
63
+ `declare var ${nm}: ${f.isAsync ? "AsyncFunction" : "Function"}${comment}`,
64
+ );
65
+ } else ds.push(`declare const ${nm}: Function;${comment}`);
66
+ }
67
+ prompts.push(`You can also call some functions, here with TypeScript declarations (although you are writing JavaScript):
68
+
69
+ ${ds.join("\n")}`);
70
+ }
71
+ return prompts.join("\n");
72
+ }
73
+ },
74
+ isAsync: true,
75
+ description: "Return a standard prompt for writing code",
76
+ arguments: [
77
+ {
78
+ name: "options",
79
+ type: "JSON",
80
+ tstype: `{language: "javascript", table?:string, has_table?: boolean, has_functions?:boolean}`,
81
+ },
82
+ ],
83
+ };