@saltcorn/agents 0.6.8 → 0.6.10

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/agent-view.js CHANGED
@@ -32,6 +32,7 @@ const {
32
32
  a,
33
33
  br,
34
34
  img,
35
+ text,
35
36
  } = require("@saltcorn/markup/tags");
36
37
  const { getState } = require("@saltcorn/data/db/state");
37
38
  const {
@@ -50,7 +51,7 @@ const {
50
51
  } = require("./common");
51
52
  const MarkdownIt = require("markdown-it"),
52
53
  md = new MarkdownIt();
53
- const { isWeb } = require("@saltcorn/data/utils");
54
+ const { isWeb, escapeHtml } = require("@saltcorn/data/utils");
54
55
  const path = require("path");
55
56
 
56
57
  const configuration_workflow = (req) =>
@@ -240,7 +241,7 @@ const run = async (
240
241
  state,
241
242
  { res, req },
242
243
  ) => {
243
- const action = agent_action || await Trigger.findOne({ id: action_id });
244
+ const action = agent_action || (await Trigger.findOne({ id: action_id }));
244
245
  if (!action) throw new Error(`Action not found: ${action_id}`);
245
246
  const prevRuns = show_prev_runs
246
247
  ? (
@@ -532,9 +533,11 @@ const run = async (
532
533
 
533
534
  p(
534
535
  { class: "prevrun_content" },
535
- run.context.interactions
536
- .find((i) => typeof i?.content === "string")
537
- ?.content?.substring?.(0, 80),
536
+ escapeHtml(
537
+ run.context.interactions
538
+ .find((i) => typeof i?.content === "string")
539
+ ?.content?.substring?.(0, 80),
540
+ ),
538
541
  ),
539
542
  ),
540
543
  ),
@@ -820,7 +823,8 @@ const run = async (
820
823
 
821
824
  const interact = async (table_id, viewname, config, body, { req, res }) => {
822
825
  const { userinput, run_id, triggering_row_id } = body;
823
- const action = config.agent_action || await Trigger.findOne({ id: config.action_id });
826
+ const action =
827
+ config.agent_action || (await Trigger.findOne({ id: config.action_id }));
824
828
 
825
829
  let run;
826
830
  let triggering_row;
@@ -892,7 +896,11 @@ const interact = async (table_id, viewname, config, body, { req, res }) => {
892
896
  await saveInteractions(run);
893
897
  fileBadges = div({ class: "d-flex" }, badges);
894
898
  }
895
- const userInteractions = wrapSegment(p(userinput) + fileBadges, "You", true);
899
+ const userInteractions = wrapSegment(
900
+ p(escapeHtml(userinput)) + fileBadges,
901
+ "You",
902
+ true,
903
+ );
896
904
 
897
905
  await addToContext(run, {
898
906
  interactions: [
@@ -951,7 +959,8 @@ const delprevrun = async (table_id, viewname, config, body, { req, res }) => {
951
959
 
952
960
  const debug_info = async (table_id, viewname, config, body, { req, res }) => {
953
961
  const { run_id, triggering_row_id } = body;
954
- const action = config.agent_action || await Trigger.findOne({ id: config.action_id });
962
+ const action =
963
+ config.agent_action || (await Trigger.findOne({ id: config.action_id }));
955
964
  let triggering_row;
956
965
  if (table_id && triggering_row_id) {
957
966
  const table = Table.findOne(table_id);
@@ -974,10 +983,12 @@ const debug_info = async (table_id, viewname, config, body, { req, res }) => {
974
983
  sysPrompt = complArgs.systemPrompt;
975
984
  }
976
985
  const debug_html = div(
977
- div(h4("System prompt"), pre(sysPrompt)),
986
+ div(h4("System prompt"), pre(text(escapeHtml(sysPrompt)))),
978
987
  div(
979
988
  h4("API interactions"),
980
- pre(JSON.stringify(run.context.api_interactions, null, 2)),
989
+ pre(
990
+ text(escapeHtml(JSON.stringify(run.context.api_interactions, null, 2))),
991
+ ),
981
992
  ),
982
993
  );
983
994
  if (run && req.user?.role_id === 1)
@@ -993,7 +1004,8 @@ const debug_info = async (table_id, viewname, config, body, { req, res }) => {
993
1004
 
994
1005
  const skillroute = async (table_id, viewname, config, body, { req, res }) => {
995
1006
  const { run_id, triggering_row_id, skillid } = body;
996
- const action = config.agent_action || await Trigger.findOne({ id: config.action_id });
1007
+ const action =
1008
+ config.agent_action || (await Trigger.findOne({ id: config.action_id }));
997
1009
  let triggering_row;
998
1010
  if (table_id && triggering_row_id) {
999
1011
  const table = Table.findOne(table_id);
@@ -1030,7 +1042,8 @@ const execute_user_action = async (
1030
1042
  ) => {
1031
1043
  const { run_id, rndid, uaname } = body;
1032
1044
 
1033
- const action = config.agent_action || await Trigger.findOne({ id: config.action_id });
1045
+ const action =
1046
+ config.agent_action || (await Trigger.findOne({ id: config.action_id }));
1034
1047
  const run = await WorkflowRun.findOne({ id: +run_id });
1035
1048
  //console.log("run uas",run.context.user_actions );
1036
1049
 
package/common.js CHANGED
@@ -418,6 +418,17 @@ const process_interaction = async (
418
418
  ...opts,
419
419
  });
420
420
  },
421
+ emit_update(s) {
422
+ if (!stream || !viewname) return;
423
+ const view = View.findOne({ name: viewname });
424
+ const pageLoadTag = req.body.page_load_tag;
425
+ view.emitRealTimeEvent(
426
+ `STREAM_CHUNK?page_load_tag=${pageLoadTag}`,
427
+ {
428
+ content: s,
429
+ },
430
+ );
431
+ },
421
432
  });
422
433
  if (generateUsed)
423
434
  await addToContext(run, {
@@ -431,7 +442,7 @@ const process_interaction = async (
431
442
  { role: "system", content: postprocres.add_system_prompt },
432
443
  ],
433
444
  });
434
- if (postprocres.add_response)
445
+ if (postprocres.add_response) {
435
446
  add_response(
436
447
  wrapSegment(
437
448
  wrapCard(
@@ -441,6 +452,26 @@ const process_interaction = async (
441
452
  agent_label,
442
453
  ),
443
454
  );
455
+ //replace tool response with this
456
+ // run.context.interactions.forEach((ic) => {});
457
+ const result = postprocres.add_response;
458
+ await sysState.functions.llm_add_message.run(
459
+ "tool_response",
460
+ !result || typeof result === "string"
461
+ ? {
462
+ type: "text",
463
+ value: result || "Action run",
464
+ }
465
+ : {
466
+ type: "json",
467
+ value: JSON.parse(JSON.stringify(result)),
468
+ },
469
+ {
470
+ chat: run.context.interactions,
471
+ tool_call,
472
+ },
473
+ );
474
+ }
444
475
  if (postprocres.add_user_action && viewname) {
445
476
  const user_actions = Array.isArray()
446
477
  ? postprocres.add_user_action
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/agents",
3
- "version": "0.6.8",
3
+ "version": "0.6.10",
4
4
  "description": "AI agents for Saltcorn",
5
5
  "main": "index.js",
6
6
  "dependencies": {
@@ -27,7 +27,7 @@ class GenerateAndRunJsCodeSkill {
27
27
  }
28
28
 
29
29
  constructor(cfg) {
30
- Object.assign(this, cfg);
30
+ Object.assign(this, cfg);
31
31
  }
32
32
 
33
33
  async runCode(code, { user, req }) {
@@ -42,6 +42,7 @@ class GenerateAndRunJsCodeSkill {
42
42
  }
43
43
  : {}),
44
44
  ...(this.allow_fetch ? { fetch } : {}),
45
+ ...(this.allow_functions ? sysState.eval_context : {}),
45
46
  user,
46
47
  console,
47
48
  sleep,
@@ -94,6 +95,11 @@ class GenerateAndRunJsCodeSkill {
94
95
  label: "Allow access to tables",
95
96
  type: "Bool",
96
97
  },
98
+ {
99
+ name: "allow_functions",
100
+ label: "Allow calls to functions from codepages and modules",
101
+ type: "Bool",
102
+ },
97
103
  ...(Table.subClass
98
104
  ? [
99
105
  {
@@ -117,9 +123,15 @@ class GenerateAndRunJsCodeSkill {
117
123
  /*renderToolCall({ phrase }, { req }) {
118
124
  return div({ class: "border border-primary p-2 m-2" }, phrase);
119
125
  },*/
120
- postProcess: async ({ tool_call, req, generate, ...rest }) => {
126
+ postProcess: async ({
127
+ tool_call,
128
+ req,
129
+ generate,
130
+ emit_update,
131
+ ...rest
132
+ }) => {
121
133
  //console.log("postprocess args", { tool_call, ...rest });
122
-
134
+ emit_update("Generating code");
123
135
  const str = await generate(
124
136
  `You will now be asked to write JavaScript code.
125
137
  ${this.code_description ? "\nSome more information: " + this.code_description : ""}
@@ -131,17 +143,16 @@ The code you write can use await at the top level, and should return
131
143
 
132
144
  Now generate the JavaScript code required by the user.`,
133
145
  );
134
- //console.log("gen answer", str);
135
-
146
+ getState().log(6, "Generated code: \n" + str);
136
147
  const js_code = str.includes("```javascript")
137
148
  ? str.split("```javascript")[1].split("```")[0]
138
149
  : str;
139
-
150
+ emit_update("Running code");
140
151
  const res = await this.runCode(js_code, { user: req.user });
141
152
  //console.log("code response", res);
142
-
153
+ getState().log(6, "Code answer: " + JSON.stringify(res));
143
154
  return {
144
- stop: true,
155
+ //stop: true,
145
156
  add_response: res,
146
157
  };
147
158
  },