@saltcorn/agents 0.7.7 → 0.7.8

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/action.js CHANGED
@@ -92,6 +92,7 @@ module.exports = {
92
92
  is_sub_agent,
93
93
  agent_view_config,
94
94
  dyn_updates,
95
+ agent_label,
95
96
  ...rest
96
97
  }) => {
97
98
  const userinput = interpolate(configuration.prompt, row, user);
@@ -116,7 +117,7 @@ module.exports = {
116
117
  run,
117
118
  configuration,
118
119
  req,
119
- undefined,
120
+ agent_label || undefined,
120
121
  [],
121
122
  row,
122
123
  agent_view_config || { stream: false },
package/common.js CHANGED
@@ -234,7 +234,7 @@ const process_interaction = async (
234
234
  run,
235
235
  config,
236
236
  req,
237
- agent_label = "Copilot",
237
+ agent_label = "Agent",
238
238
  prevResponses = [],
239
239
  triggering_row = {},
240
240
  agentsViewCfg = { stream: false },
@@ -391,6 +391,9 @@ const process_interaction = async (
391
391
  },
392
392
  );
393
393
  }
394
+ const response_label = is_sub_agent
395
+ ? agent_label
396
+ : tool.skill.skill_label || tool.skill.constructor.skill_name;
394
397
  if (tool.tool.renderToolCall) {
395
398
  const row = tool_call.input;
396
399
 
@@ -400,10 +403,7 @@ const process_interaction = async (
400
403
  if (rendered)
401
404
  add_response(
402
405
  wrapSegment(
403
- wrapCard(
404
- tool.skill.skill_label || tool.skill.constructor.skill_name,
405
- rendered,
406
- ),
406
+ wrapCard(response_label, rendered),
407
407
  agent_label,
408
408
  false,
409
409
  layout,
@@ -427,11 +427,7 @@ const process_interaction = async (
427
427
  if (rendered)
428
428
  add_response(
429
429
  wrapSegment(
430
- wrapCard(
431
- tool.skill.skill_label ||
432
- tool.skill.constructor.skill_name,
433
- rendered,
434
- ),
430
+ wrapCard(response_label, rendered),
435
431
  agent_label,
436
432
  false,
437
433
  layout,
@@ -473,8 +469,10 @@ const process_interaction = async (
473
469
  let stop = false,
474
470
  myHasResult = false;
475
471
  if (tool.tool.postProcess && !stop) {
476
- let result = toolResults[tool_call.tool_call_id];
477
-
472
+ let result = toolResults[tool_call.tool_call_id];
473
+ const response_label = is_sub_agent
474
+ ? agent_label
475
+ : tool.skill.skill_label || tool.skill.constructor.skill_name;
478
476
  const chat = run.context.interactions;
479
477
  let generateUsed = false;
480
478
  const systemPrompt = await getSystemPrompt(
@@ -532,48 +530,75 @@ const process_interaction = async (
532
530
  }
533
531
 
534
532
  for (const add_resp of postprocres.add_responses || []) {
535
- raw_responses.push(add_resp);
536
- const renderedAddResponse =
537
- typeof add_resp === "string" ? md.render(add_resp) : add_resp;
538
- add_response(
539
- wrapSegment(
540
- wrapCard(
541
- tool.skill.skill_label || tool.skill.constructor.skill_name,
542
- renderedAddResponse,
533
+ const content =
534
+ add_resp.role && add_resp.content ? add_resp.content : add_resp;
535
+ raw_responses.push(content);
536
+ if (add_resp.md_response !== null) {
537
+ const renderedAddResponse = add_resp.md_response
538
+ ? md.render(add_resp.md_response)
539
+ : typeof content === "string"
540
+ ? md.render(content)
541
+ : content;
542
+ add_response(
543
+ wrapSegment(
544
+ wrapCard(response_label, renderedAddResponse),
545
+ agent_label,
546
+ false,
547
+ layout,
543
548
  ),
544
- agent_label,
545
- false,
546
- layout,
547
- ),
548
- );
549
+ );
550
+ }
551
+ if (typeof add_resp.md_response !== "undefined")
552
+ delete add_resp.md_response;
549
553
 
550
- const result = add_resp;
551
- await sysState.functions.llm_add_message.run(
552
- "assistant",
554
+ const result = content;
553
555
 
554
- !result || typeof result === "string"
555
- ? result || "Action run"
556
- : JSON.stringify(result),
556
+ if (add_resp.role && add_resp.content) {
557
+ await sysState.functions.llm_add_message.run(
558
+ add_resp.role,
559
+ add_resp.content,
560
+ {
561
+ chat: run.context.interactions,
562
+ },
563
+ );
564
+ } else
565
+ await sysState.functions.llm_add_message.run(
566
+ "assistant",
567
+
568
+ !result || typeof result === "string"
569
+ ? result || "Action run"
570
+ : JSON.stringify(result),
571
+
572
+ {
573
+ chat: run.context.interactions,
574
+ },
575
+ );
557
576
 
558
- {
559
- chat: run.context.interactions,
560
- },
561
- );
562
577
  await addToContext(run, {
563
578
  interactions: run.context.interactions,
564
579
  });
565
580
  }
566
581
  if (!postprocres.stop) {
567
- await sysState.functions.llm_add_message.run(
568
- "user",
569
- postprocres.follow_up_prompt || "Continue with the query",
570
- {
571
- chat: run.context.interactions,
572
- },
573
- );
574
- await addToContext(run, {
575
- interactions: run.context.interactions,
576
- });
582
+ const lastInteract =
583
+ run.context.interactions[run.context.interactions.length - 1];
584
+
585
+ if (
586
+ postprocres.follow_up_prompt ||
587
+ !(
588
+ lastInteract?.role === "user" || lastInteract?.role === "tool"
589
+ )
590
+ ) {
591
+ await sysState.functions.llm_add_message.run(
592
+ "user",
593
+ postprocres.follow_up_prompt || "Continue with the query",
594
+ {
595
+ chat: run.context.interactions,
596
+ },
597
+ );
598
+ await addToContext(run, {
599
+ interactions: run.context.interactions,
600
+ });
601
+ }
577
602
  myHasResult = true;
578
603
  }
579
604
  if (postprocres.add_user_action && viewname) {
@@ -624,7 +649,7 @@ const process_interaction = async (
624
649
  ? answer
625
650
  : wrapSegment(md.render(answer), agent_label, false, layout),
626
651
  );
627
- if (dyn_updates)
652
+ if (dyn_updates && !is_sub_agent)
628
653
  getState().emitDynamicUpdate(
629
654
  db.getTenantSchema(),
630
655
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/agents",
3
- "version": "0.7.7",
3
+ "version": "0.7.8",
4
4
  "description": "AI agents for Saltcorn",
5
5
  "main": "index.js",
6
6
  "dependencies": {
@@ -215,13 +215,32 @@ Now generate the JavaScript code required by the user.`,
215
215
  if (res !== undefined && res !== null && res !== "") return res;
216
216
  return "Code executed successfully but returned no output.";
217
217
  };
218
+ const mkMdResponse = (result, code) =>
219
+ req?.user?.role_id === 1
220
+ ? `<details>
221
+
222
+ <summary>Show code</summary>
223
+
224
+ \`\`\`javascript
225
+ ${code}
226
+ \`\`\`
227
+
228
+
229
+ </details>
230
+
231
+ ${result}`
232
+ : result;
218
233
  try {
219
234
  const res = await this.runCode(js_code, { user: req.user });
220
235
  getState().log(6, "Code answer: " + JSON.stringify(res));
221
236
  const effectiveRes = ensureResult(res);
222
237
  return {
223
238
  stop: typeof res === "string" && !this.follow_up_prompt,
224
- add_response: effectiveRes,
239
+ add_response: {
240
+ role: "user",
241
+ content: `The result of running the code is: ${effectiveRes}`,
242
+ md_response: mkMdResponse(effectiveRes, js_code),
243
+ },
225
244
  ...(this.follow_up_prompt
226
245
  ? { follow_up_prompt: this.follow_up_prompt }
227
246
  : {}),
@@ -240,7 +259,7 @@ this code produced the following error:
240
259
  ${err.message}
241
260
  \`\`\`
242
261
 
243
- Correct this error.
262
+ Correct this error and generate the new Javascript code to run
244
263
  `);
245
264
  try {
246
265
  const res = await this.runCode(retry_js_code, {
@@ -250,7 +269,11 @@ Correct this error.
250
269
  const effectiveRes = ensureResult(res);
251
270
  return {
252
271
  stop: typeof res === "string" && !this.follow_up_prompt,
253
- add_response: effectiveRes,
272
+ add_response: {
273
+ role: "user",
274
+ content: `The result of running the code is: ${effectiveRes}`,
275
+ md_response: mkMdResponse(effectiveRes, retry_js_code),
276
+ },
254
277
  ...(this.follow_up_prompt
255
278
  ? { follow_up_prompt: this.follow_up_prompt }
256
279
  : {}),
@@ -102,6 +102,7 @@ class SubagentToSkill {
102
102
  agent_view_config,
103
103
  dyn_updates,
104
104
  req,
105
+ agent_label: this.agent_name,
105
106
  });
106
107
  getState().log(6, "Subagent response", JSON.stringify(subres, null, 2));
107
108
  //if (subres.json.raw_responses)