@saltcorn/copilot 0.7.4 → 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.
@@ -1,6 +1,7 @@
1
1
  const Table = require("@saltcorn/data/models/table");
2
2
  const View = require("@saltcorn/data/models/view");
3
3
  const { fieldProperties } = require("../common");
4
+ const { initial_config_all_fields } = require("@saltcorn/data/plugin-helper");
4
5
  const { getState } = require("@saltcorn/data/db/state");
5
6
  const {
6
7
  div,
@@ -12,6 +13,7 @@ const {
12
13
  iframe,
13
14
  text_attr,
14
15
  } = require("@saltcorn/markup/tags");
16
+ const builderGen = require("../builder-gen");
15
17
 
16
18
  class GenerateViewSkill {
17
19
  static skill_name = "Generate View";
@@ -38,11 +40,14 @@ a view generation mode. The tool call only requires high-level details to start
38
40
  table,
39
41
  min_role,
40
42
  }) {
43
+ const normalizedRole = min_role || "public";
44
+ const tableRow = table ? Table.findOne({ name: table }) : null;
41
45
  await View.create({
42
46
  name,
43
47
  viewtemplate: viewpattern,
44
- table: Table.findOne({ name: table }),
45
- min_role: { admin: 1, public: 100, user: 80 }[min_role],
48
+ table_id: tableRow?.id,
49
+ table: tableRow,
50
+ min_role: { admin: 1, public: 100, user: 80 }[normalizedRole],
46
51
  configuration: wfctx,
47
52
  });
48
53
  setTimeout(() => getState().refresh_views(), 200);
@@ -63,6 +68,7 @@ a view generation mode. The tool call only requires high-level details to start
63
68
  vts[vtnm].enable_copilot_viewgen ||
64
69
  vts[vtnm].copilot_generate_view_prompt,
65
70
  );
71
+ if (!enabled_vt_names.includes("Show")) enabled_vt_names.push("Show");
66
72
  //const roles = await User.get_roles();
67
73
  const tableless = enabled_vt_names.filter(
68
74
  (vtnm) => vts[vtnm].tableless === true,
@@ -107,70 +113,95 @@ a view generation mode. The tool call only requires high-level details to start
107
113
  process: async (input) => {
108
114
  return "Metadata received";
109
115
  },
110
- postProcess: async ({ tool_call, req, generate }) => {
116
+ postProcess: async ({ tool_call, req, generate, chat }) => {
111
117
  const state = getState();
112
118
  const vt = state.viewtemplates[tool_call.input.viewpattern];
113
119
  const table =
114
120
  vt.tableless === true
115
121
  ? null
116
122
  : Table.findOne({ name: tool_call.input.table });
117
- const flow = vt.configuration_workflow(req);
118
- const wfctx = { viewname: tool_call.input.name, table_id: table?.id };
119
- let vt_prompt = "";
120
- if (vt.copilot_generate_view_prompt) {
121
- if (typeof vt.copilot_generate_view_prompt === "string")
122
- vt_prompt = vt.copilot_generate_view_prompt;
123
- else if (typeof vt.copilot_generate_view_prompt === "function")
124
- vt_prompt = await vt.copilot_generate_view_prompt(tool_call.input);
125
- }
126
123
 
127
- for (const step of flow.steps) {
128
- const form = await step.form(wfctx);
129
- const properties = {};
130
- //TODO onlyWhen
131
- for (const field of form.fields) {
132
- //TODO showIf
133
- properties[field.name] = {
134
- description:
135
- field.copilot_description ||
136
- `${field.label}.${field.sublabel ? ` ${field.sublabel}` : ""}`,
137
- ...fieldProperties(field),
138
- };
124
+ const wfctx = { viewname: tool_call.input.name, table_id: table?.id };
125
+ if (tool_call.input.viewpattern === "Show") {
126
+ const promptFromChat = Array.isArray(chat)
127
+ ? [...chat]
128
+ .reverse()
129
+ .find((item) => item?.role === "user" && item?.content)?.content
130
+ : "";
131
+ const layoutPrompt = promptFromChat || tool_call.input.name || "";
132
+ wfctx.layout = await builderGen.run(
133
+ layoutPrompt,
134
+ "show",
135
+ table?.name,
136
+ chat,
137
+ );
138
+ if (table) {
139
+ const baseCfg = await initial_config_all_fields(false)({
140
+ table_id: table.id,
141
+ });
142
+ if (baseCfg?.columns) wfctx.columns = baseCfg.columns;
143
+ }
144
+ } else {
145
+ const flow = vt.configuration_workflow(req);
146
+ let vt_prompt = "";
147
+ if (vt.copilot_generate_view_prompt) {
148
+ if (typeof vt.copilot_generate_view_prompt === "string")
149
+ vt_prompt = vt.copilot_generate_view_prompt;
150
+ else if (typeof vt.copilot_generate_view_prompt === "function")
151
+ vt_prompt = await vt.copilot_generate_view_prompt(
152
+ tool_call.input,
153
+ );
139
154
  }
140
155
 
141
- const answer = await generate(
142
- `${vt_prompt ? vt_prompt + "\n\n" : ""}Now generate the ${step.name} details of the view by calling the generate_view_details tool`,
143
- {
144
- tools: [
145
- {
156
+ for (const step of flow.steps) {
157
+ const form = await step.form(wfctx);
158
+ const properties = {};
159
+ //TODO onlyWhen
160
+ for (const field of form.fields) {
161
+ //TODO showIf
162
+ properties[field.name] = {
163
+ description:
164
+ field.copilot_description ||
165
+ `${field.label}.${field.sublabel ? ` ${field.sublabel}` : ""}`,
166
+ ...fieldProperties(field),
167
+ };
168
+ }
169
+
170
+ const answer = await generate(
171
+ `${vt_prompt ? vt_prompt + "\n\n" : ""}Now generate the ${step.name} details of the view by calling the generate_view_details tool`,
172
+ {
173
+ tools: [
174
+ {
175
+ type: "function",
176
+ function: {
177
+ name: "generate_view_details",
178
+ description: "Provide view details",
179
+ parameters: {
180
+ type: "object",
181
+ properties,
182
+ },
183
+ },
184
+ },
185
+ ],
186
+ tool_choice: {
146
187
  type: "function",
147
188
  function: {
148
189
  name: "generate_view_details",
149
- description: "Provide view details",
150
- parameters: {
151
- type: "object",
152
- properties,
153
- },
154
190
  },
155
191
  },
156
- ],
157
- tool_choice: {
158
- type: "function",
159
- function: {
160
- name: "generate_view_details",
161
- },
162
192
  },
163
- },
164
- );
165
- const tc = answer.getToolCalls()[0];
166
- Object.assign(wfctx, tc.input);
193
+ );
194
+ const tc = answer.getToolCalls()[0];
195
+ Object.assign(wfctx, tc.input);
196
+ }
167
197
  }
168
198
  const view = new View({
169
199
  name: tool_call.input.name,
170
200
  viewtemplate: tool_call.input.viewpattern,
171
201
  table,
202
+ table_id: table?.id,
172
203
  min_role: { admin: 1, public: 100, user: 80 }[
173
- tool_call.input.min_role
204
+ tool_call.input.min_role || "public"
174
205
  ],
175
206
  configuration: wfctx,
176
207
  });