-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat-copilot.js
558 lines (522 loc) · 16.4 KB
/
chat-copilot.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
const Field = require("@saltcorn/data/models/field");
const Table = require("@saltcorn/data/models/table");
const Form = require("@saltcorn/data/models/form");
const View = require("@saltcorn/data/models/view");
const Trigger = require("@saltcorn/data/models/trigger");
const { findType } = require("@saltcorn/data/models/discovery");
const { save_menu_items } = require("@saltcorn/data/models/config");
const db = require("@saltcorn/data/db");
const WorkflowRun = require("@saltcorn/data/models/workflow_run");
const { localeDateTime } = require("@saltcorn/markup");
const {
div,
script,
domReady,
pre,
code,
input,
h4,
style,
h5,
button,
text_attr,
i,
p,
span,
small,
form,
textarea,
} = require("@saltcorn/markup/tags");
const { getState } = require("@saltcorn/data/db/state");
const {
getCompletion,
getPromptFromTemplate,
incompleteCfgMsg,
} = require("./common");
const MarkdownIt = require("markdown-it"),
md = new MarkdownIt();
const get_state_fields = () => [];
const run = async (table_id, viewname, cfg, state, { res, req }) => {
const prevRuns = (
await WorkflowRun.find(
{ trigger_id: null /*started_by: req.user?.id*/ }, //todo uncomment
{ orderBy: "started_at", orderDesc: true, limit: 30 }
)
).filter(
(r) =>
r.context.interactions &&
(r.context.copilot === "_system" || !r.context.copilot)
);
const cfgMsg = incompleteCfgMsg();
if (cfgMsg) return cfgMsg;
let runInteractions = "";
if (state.run_id) {
const run = prevRuns.find((r) => r.id == state.run_id);
const interactMarkups = [];
for (const interact of run.context.interactions) {
switch (interact.role) {
case "user":
interactMarkups.push(
div(
{ class: "interaction-segment" },
span({ class: "badge bg-secondary" }, "You"),
p(interact.content)
)
);
break;
case "assistant":
case "system":
if (interact.tool_calls) {
for (const tool_call of interact.tool_calls) {
const markup = await renderToolcall(
tool_call,
viewname,
(run.context.implemented_fcall_ids || []).includes(
tool_call.id
),
run
);
interactMarkups.push(
div(
{ class: "interaction-segment" },
span({ class: "badge bg-secondary" }, "Copilot"),
markup
)
);
}
} else
interactMarkups.push(
div(
{ class: "interaction-segment" },
span({ class: "badge bg-secondary" }, "Copilot"),
typeof interact.content === "string"
? md.render(interact.content)
: interact.content
)
);
break;
case "tool":
//ignore
break;
}
}
runInteractions = interactMarkups.join("");
}
const input_form = form(
{
onsubmit:
"event.preventDefault();spin_send_button();view_post('Saltcorn Copilot', 'interact', $(this).serialize(), processCopilotResponse);return false;",
class: "form-namespace copilot mt-2",
method: "post",
},
input({
type: "hidden",
name: "_csrf",
value: req.csrfToken(),
}),
input({
type: "hidden",
class: "form-control ",
name: "run_id",
value: state.run_id ? +state.run_id : undefined,
}),
div(
{ class: "copilot-entry" },
textarea({
class: "form-control",
name: "userinput",
"data-fieldname": "userinput",
placeholder: "How can I help you?",
id: "inputuserinput",
rows: "3",
autofocus: true,
}),
span(
{ class: "submit-button p-2", onclick: "$('form.copilot').submit()" },
i({ id: "sendbuttonicon", class: "far fa-paper-plane" })
)
),
i(
small(
"Skills you can request: " +
actionClasses.map((ac) => ac.title).join(", ")
)
)
);
return {
widths: [3, 9],
gx: 3,
besides: [
{
type: "container",
contents: div(
div(
{
class: "d-flex justify-content-between align-middle mb-2",
},
h5("Sessions"),
button(
{
type: "button",
class: "btn btn-secondary btn-sm py-0",
style: "font-size: 0.9em;height:1.5em",
onclick: "unset_state_field('run_id')",
title: "New session",
},
i({ class: "fas fa-redo fa-sm" })
)
),
prevRuns.map((run) =>
div(
{
onclick: `set_state_field('run_id',${run.id})`,
class: "prevcopilotrun border p-2",
},
localeDateTime(run.started_at),
p(
{ class: "prevrun_content" },
run.context.interactions[0]?.content
)
)
)
),
},
{
type: "container",
contents: div(
{ class: "card" },
div(
{ class: "card-body" },
script({
src: `/static_assets/${db.connectObj.version_tag}/mermaid.min.js`,
}),
script(
{ type: "module" },
`mermaid.initialize({securityLevel: 'loose'${
getState().getLightDarkMode(req.user) === "dark"
? ",theme: 'dark',"
: ""
}});`
),
div({ id: "copilotinteractions" }, runInteractions),
input_form,
style(
`div.interaction-segment:not(:first-child) {border-top: 1px solid #e7e7e7; }
div.interaction-segment {padding-top: 5px;padding-bottom: 5px;}
div.interaction-segment p {margin-bottom: 0px;}
div.interaction-segment div.card {margin-top: 0.5rem;}
div.prevcopilotrun:hover {cursor: pointer; background-color: var(--tblr-secondary-bg-subtle, var(--bs-secondary-bg-subtle, gray));}
.copilot-entry .submit-button:hover { cursor: pointer}
.copilot-entry .submit-button {
position: relative;
top: -1.8rem;
left: 0.1rem;
}
.copilot-entry {margin-bottom: -1.25rem; margin-top: 1rem;}
p.prevrun_content {
white-space: nowrap;
overflow: hidden;
margin-bottom: 0px;
display: block;
text-overflow: ellipsis;}`
),
script(`function processCopilotResponse(res) {
$("#sendbuttonicon").attr("class","far fa-paper-plane");
const $runidin= $("input[name=run_id")
if(res.run_id && (!$runidin.val() || $runidin.val()=="undefined"))
$runidin.val(res.run_id);
const wrapSegment = (html, who) => '<div class="interaction-segment"><span class="badge bg-secondary">'+who+'</span>'+html+'</div>'
$("#copilotinteractions").append(wrapSegment('<p>'+$("textarea[name=userinput]").val()+'</p>', "You"))
$("textarea[name=userinput]").val("")
for(const action of res.actions||[]) {
$("#copilotinteractions").append(wrapSegment(action, "Copilot"))
}
if(res.response)
$("#copilotinteractions").append(wrapSegment('<p>'+res.response+'</p>', "Copilot"))
}
function restore_old_button_elem(btn) {
const oldText = $(btn).data("old-text");
btn.html(oldText);
btn.css({ width: "" }).prop("disabled", false);
btn.removeData("old-text");
}
function processExecuteResponse(res) {
const btn = $("#exec-"+res.fcall_id)
restore_old_button_elem($("#exec-"+res.fcall_id))
btn.prop('disabled', true);
btn.html('<i class="fas fa-check me-1"></i>Applied')
btn.removeClass("btn-primary")
btn.addClass("btn-secondary")
if(res.postExec) {
$('#postexec-'+res.fcall_id).html(res.postExec)
}
}
function submitOnEnter(event) {
if (event.which === 13) {
if (!event.repeat) {
const newEvent = new Event("submit", {cancelable: true});
event.target.form.dispatchEvent(newEvent);
}
event.preventDefault(); // Prevents the addition of a new line in the text field
}
}
document.getElementById("inputuserinput").addEventListener("keydown", submitOnEnter);
function spin_send_button() {
$("#sendbuttonicon").attr("class","fas fa-spinner fa-spin");
}
`)
)
),
},
],
};
};
const ellipsize = (s, nchars) => {
if (!s || !s.length) return "";
if (s.length <= (nchars || 20)) return text_attr(s);
return text_attr(s.substr(0, (nchars || 20) - 3)) + "...";
};
const actionClasses = [
require("./actions/generate-workflow"),
require("./actions/generate-tables"),
require("./actions/generate-js-action"),
require("./actions/generate-page"),
require("./actions/generate-view"),
];
const getCompletionArguments = async () => {
const tools = [];
const sysPrompts = [];
for (const actionClass of actionClasses) {
tools.push({
type: "function",
function: {
name: actionClass.function_name,
description: actionClass.description,
parameters: await actionClass.json_schema(),
},
});
sysPrompts.push(await actionClass.system_prompt());
}
const systemPrompt =
"You are building application components in a database application builder called Saltcorn.\n\n" +
sysPrompts.join("\n\n");
return { tools, systemPrompt };
};
/*
build a workflow that asks the user for their name and age
*/
const execute = async (table_id, viewname, config, body, { req }) => {
const { fcall_id, run_id } = body;
const run = await WorkflowRun.findOne({ id: +run_id });
const fcall = run.context.funcalls[fcall_id];
const actionClass = actionClasses.find(
(ac) => ac.function_name === fcall.name
);
let result;
if (actionClass.follow_on_generate) {
const toolCallIndex = run.context.interactions.findIndex(
(i) => i.tool_call_id === fcall_id
);
const follow_on_gen = run.context.interactions.find(
(i, ix) => i.role === "assistant" && ix > toolCallIndex
);
result = await actionClass.execute(
JSON.parse(fcall.arguments),
req,
follow_on_gen.content
);
} else {
result = await actionClass.execute(JSON.parse(fcall.arguments), req);
}
await addToContext(run, { implemented_fcall_ids: [fcall_id] });
return { json: { success: "ok", fcall_id, ...(result || {}) } };
};
const interact = async (table_id, viewname, config, body, { req }) => {
const { userinput, run_id } = body;
let run;
if (!run_id || run_id === "undefined")
run = await WorkflowRun.create({
status: "Running",
started_by: req.user?.id,
context: {
copilot: "_system",
implemented_fcall_ids: [],
interactions: [{ role: "user", content: userinput }],
funcalls: {},
},
});
else {
run = await WorkflowRun.findOne({ id: +run_id });
await addToContext(run, {
interactions: [{ role: "user", content: userinput }],
});
}
const complArgs = await getCompletionArguments();
complArgs.chat = run.context.interactions;
//console.log(complArgs);
//build a database for a bicycle rental company
//add a boolean field called "paid" to the payments table
const answer = await getState().functions.llm_generate.run(
userinput,
complArgs
);
await addToContext(run, {
interactions:
typeof answer === "object" && answer.tool_calls
? [
{ role: "assistant", tool_calls: answer.tool_calls },
...answer.tool_calls.map((tc) => ({
role: "tool",
tool_call_id: tc.id,
name: tc.function.name,
content: "Action suggested to user.",
})),
]
: [{ role: "assistant", content: answer }],
});
console.log("answer", answer);
if (typeof answer === "object" && answer.tool_calls) {
const actions = [];
for (const tool_call of answer.tool_calls) {
await addToContext(run, {
funcalls: { [tool_call.id]: tool_call.function },
});
const followOnGen = await getFollowOnGeneration(tool_call);
if (followOnGen) {
const { response_schema, prompt } = followOnGen;
const follow_on_answer = await getState().functions.llm_generate.run(
prompt,
{
debugResult: true,
chat: run.context.interactions,
response_format: response_schema
? {
type: "json_schema",
json_schema: {
name: "generate_page",
schema: response_schema,
},
}
: undefined,
}
);
console.log("follow on answer", follow_on_answer);
await addToContext(run, {
interactions: [
{ role: "user", content: prompt },
{ role: "assistant", content: follow_on_answer },
],
});
const markup = await renderToolcall(
tool_call,
viewname,
false,
run,
follow_on_answer
);
actions.push(markup);
} else {
const markup = await renderToolcall(tool_call, viewname, false, run);
actions.push(markup);
}
}
return { json: { success: "ok", actions, run_id: run.id } };
} else
return {
json: { success: "ok", response: md.render(answer), run_id: run.id },
};
};
const getFollowOnGeneration = async (tool_call) => {
const fname = tool_call.function.name;
const actionClass = actionClasses.find((ac) => ac.function_name === fname);
const args = JSON.parse(tool_call.function.arguments);
if (actionClass.follow_on_generate) {
return await actionClass.follow_on_generate(args);
} else return null;
};
const renderToolcall = async (
tool_call,
viewname,
implemented,
run,
follow_on_answer
) => {
const fname = tool_call.function.name;
const actionClass = actionClasses.find((ac) => ac.function_name === fname);
const args = JSON.parse(tool_call.function.arguments);
const inner_markup = await actionClass.render_html(args, follow_on_answer);
return wrapAction(
inner_markup,
viewname,
tool_call,
actionClass,
implemented,
run
);
};
const wrapAction = (
inner_markup,
viewname,
tool_call,
actionClass,
implemented,
run
) =>
span({ class: "badge bg-info ms-1" }, actionClass.title) +
div(
{ class: "card mb-3 bg-secondary-subtle" },
div(
{ class: "card-body" },
inner_markup,
implemented
? button(
{
type: "button",
class: "btn btn-secondary d-block mt-3 float-end",
disabled: true,
},
i({ class: "fas fa-check me-1" }),
"Applied"
)
: button(
{
type: "button",
id: "exec-" + tool_call.id,
class: "btn btn-primary d-block mt-3 float-end",
onclick: `press_store_button(this, true);view_post('${viewname}', 'execute', {fcall_id: '${tool_call.id}', run_id: ${run.id}}, processExecuteResponse)`,
},
"Apply"
),
div({ id: "postexec-" + tool_call.id })
)
);
const addToContext = async (run, newCtx) => {
if (run.addToContext) return await run.addToContext(newCtx);
let changed = true;
Object.keys(newCtx).forEach((k) => {
if (Array.isArray(run.context[k])) {
if (!Array.isArray(newCtx[k]))
throw new Error("Must be array to append to array");
run.context[k].push(...newCtx[k]);
changed = true;
} else if (typeof run.context[k] === "object") {
if (typeof newCtx[k] !== "object")
throw new Error("Must be object to append to object");
Object.assign(run.context[k], newCtx[k]);
changed = true;
} else {
run.context[k] = newCtx[k];
changed = true;
}
});
if (changed) await run.update({ context: run.context });
};
module.exports = {
name: "Saltcorn Copilot",
display_state_form: false,
get_state_fields,
tableless: true,
singleton: true,
run,
routes: { interact, execute },
};