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