@saltcorn/agents 0.7.1 → 0.7.3

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
@@ -50,7 +50,7 @@ const {
50
50
  saveInteractions,
51
51
  } = require("./common");
52
52
  const MarkdownIt = require("markdown-it"),
53
- md = new MarkdownIt();
53
+ md = new MarkdownIt({ html: true, breaks: true, linkify: true });
54
54
  const { isWeb, escapeHtml } = require("@saltcorn/data/utils");
55
55
  const path = require("path");
56
56
 
@@ -119,7 +119,7 @@ const configuration_workflow = (req) =>
119
119
  type: "String",
120
120
  required: true,
121
121
  attributes: {
122
- options: ["Standard", "No card"],
122
+ options: ["Standard", "No card", "Modern chat", "Modern chat - no card"],
123
123
  },
124
124
  },
125
125
  {
@@ -180,11 +180,11 @@ const uploadForm = (viewname, req) =>
180
180
  span({ class: "ms-2 filename-label" }),
181
181
  );
182
182
 
183
- const realTimeCollabScript = (viewname, rndid) => {
183
+ const realTimeCollabScript = (viewname, rndid, layout) => {
184
184
  const view = View.findOne({ name: viewname });
185
185
  return script(
186
186
  domReady(`
187
- const md = markdownit()
187
+ const md = markdownit({html: true, breaks: true, linkify: true})
188
188
  window['stream scratch ${viewname} ${rndid}'] = []
189
189
  const callback = () => {
190
190
  const collabCfg = {
@@ -193,8 +193,11 @@ const realTimeCollabScript = (viewname, rndid) => {
193
193
  "STREAM_CHUNK",
194
194
  )}' + \`?page_load_tag=\${_sc_pageloadtag}\`]: async (data) => {
195
195
  window['stream scratch ${viewname} ${rndid}'].push(data.content)
196
+ const rendered = md.render(window['stream scratch ${viewname} ${rndid}'].join(""));
196
197
  $('form.agent-view div.next_response_scratch').html(
197
- md.render(window['stream scratch ${viewname} ${rndid}'].join(""))
198
+ (${JSON.stringify(layout || "")} || "").startsWith("Modern chat")
199
+ ? '<div class="chat-message chat-assistant"><div class="chat-avatar"><i class="fas fa-robot"></i></div><div class="chat-bubble">' + rendered + '</div></div>'
200
+ : rendered
198
201
  );
199
202
  }
200
203
  }
@@ -284,26 +287,24 @@ const run = async (
284
287
  const image_url = interact.content[0].image_url.url;
285
288
  if (image_url.startsWith("data"))
286
289
  interactMarkups.push(
287
- div(
288
- { class: "interaction-segment" },
289
- span({ class: "badge bg-secondary" }, "You"),
290
- "File",
291
- ),
290
+ wrapSegment("File", "You", true, layout),
292
291
  );
293
292
  else
294
293
  interactMarkups.push(
295
- div(
296
- { class: "interaction-segment" },
297
- span({ class: "badge bg-secondary" }, "You"),
294
+ wrapSegment(
298
295
  a({ href: image_url, target: "_blank" }, "File"),
296
+ "You",
297
+ true,
298
+ layout,
299
299
  ),
300
300
  );
301
301
  } else
302
302
  interactMarkups.push(
303
- div(
304
- { class: "interaction-segment" },
305
- span({ class: "badge bg-secondary" }, "You"),
303
+ wrapSegment(
306
304
  md.render(interact.content),
305
+ "You",
306
+ true,
307
+ layout,
307
308
  ),
308
309
  );
309
310
  break;
@@ -329,6 +330,8 @@ const run = async (
329
330
  rendered,
330
331
  ),
331
332
  action.name,
333
+ false,
334
+ layout,
332
335
  ),
333
336
  );
334
337
  }
@@ -354,6 +357,8 @@ const run = async (
354
357
  rendered,
355
358
  ),
356
359
  action.name,
360
+ false,
361
+ layout,
357
362
  ),
358
363
  );
359
364
  }
@@ -364,14 +369,15 @@ const run = async (
364
369
  typeof interact.content?.content === "string"
365
370
  )
366
371
  interactMarkups.push(
367
- div(
368
- { class: "interaction-segment" },
369
- span({ class: "badge bg-secondary" }, action.name),
372
+ wrapSegment(
370
373
  typeof interact.content === "string"
371
374
  ? md.render(interact.content)
372
375
  : typeof interact.content?.content === "string"
373
376
  ? md.render(interact.content.content)
374
377
  : interact.content,
378
+ action.name,
379
+ false,
380
+ layout,
375
381
  ),
376
382
  );
377
383
  break;
@@ -400,6 +406,8 @@ const run = async (
400
406
  markupContent,
401
407
  ),
402
408
  action.name,
409
+ false,
410
+ layout,
403
411
  ),
404
412
  );
405
413
  }
@@ -484,63 +492,110 @@ const run = async (
484
492
  explainer && small({ class: "explainer" }, i(explainer)),
485
493
  ),
486
494
  stream &&
487
- realTimeCollabScript(viewname, rndid) +
495
+ realTimeCollabScript(viewname, rndid, layout) +
488
496
  div({ class: "next_response_scratch" }),
489
497
  );
490
498
 
499
+ const isModernSidebar = layout && layout.startsWith("Modern chat");
491
500
  const prev_runs_side_bar = show_prev_runs
492
501
  ? div(
493
- div(
494
- {
495
- class: "d-flex flex-wrap justify-content-between align-middle mb-2",
496
- },
497
- div(
498
- { class: "d-flex" },
499
- i({
500
- class: "fas fa-caret-down me-1 session-open-sessions",
501
- onclick: "close_session_list()",
502
- }),
503
- h5(req.__("Sessions")),
504
- ),
505
- button(
506
- {
507
- type: "button",
508
- class: "btn btn-secondary btn-sm pt-0 pb-1",
509
- style: "font-size: 0.9em;height:1.5em",
510
- onclick: "unset_state_field('run_id')",
511
- title: "New session",
512
- },
513
- i({ class: "fas fa-redo fa-sm" }),
514
- ),
515
- ),
516
- prevRuns.map((run) =>
517
- div(
518
- {
519
- onclick: `set_state_field('run_id',${run.id})`,
520
- class: "prevcopilotrun border p-2",
521
- },
522
- div(
523
- { class: "d-flex justify-content-between" },
524
- span(
525
- { class: "text-truncate", style: "min-width:0" },
526
- localeDateTime(run.started_at),
502
+ { class: isModernSidebar ? "modern-sessions" : "" },
503
+ isModernSidebar
504
+ ? div(
505
+ { class: "modern-sessions-header" },
506
+ div(
507
+ { class: "d-flex align-items-center" },
508
+ i({
509
+ class: "fas fa-caret-down me-2 session-open-sessions",
510
+ onclick: "close_session_list()",
511
+ }),
512
+ i({ class: "fas fa-comments me-2 text-primary" }),
513
+ span({ class: "fw-semibold" }, req.__("Sessions")),
527
514
  ),
528
- i({
529
- class: "far fa-trash-alt",
530
- onclick: `delprevrun(event, ${run.id})`,
531
- }),
532
- ),
533
-
534
- p(
535
- { class: "prevrun_content" },
536
- escapeHtml(
537
- run.context.interactions
538
- .find((i) => typeof i?.content === "string")
539
- ?.content?.substring?.(0, 80),
515
+ button(
516
+ {
517
+ type: "button",
518
+ class: "btn btn-primary btn-sm rounded-pill px-3",
519
+ onclick: "unset_state_field('run_id')",
520
+ title: "New chat",
521
+ },
522
+ i({ class: "fas fa-plus me-1" }),
523
+ "New",
524
+ ),
525
+ )
526
+ : div(
527
+ {
528
+ class: "d-flex flex-wrap justify-content-between align-middle mb-2",
529
+ },
530
+ div(
531
+ { class: "d-flex" },
532
+ i({
533
+ class: "fas fa-caret-down me-1 session-open-sessions",
534
+ onclick: "close_session_list()",
535
+ }),
536
+ h5(req.__("Sessions")),
537
+ ),
538
+ button(
539
+ {
540
+ type: "button",
541
+ class: "btn btn-secondary btn-sm pt-0 pb-1",
542
+ style: "font-size: 0.9em;height:1.5em",
543
+ onclick: "unset_state_field('run_id')",
544
+ title: "New session",
545
+ },
546
+ i({ class: "fas fa-redo fa-sm" }),
540
547
  ),
541
548
  ),
542
- ),
543
- ),
549
+ prevRuns.map((run) => {
550
+ const isActive = state.run_id && +state.run_id === run.id;
551
+ const preview = escapeHtml(
552
+ run.context.interactions
553
+ .find((ix) => typeof ix?.content === "string")
554
+ ?.content?.substring?.(0, 80),
555
+ );
556
+ return isModernSidebar
557
+ ? div(
558
+ {
559
+ onclick: `set_state_field('run_id',${run.id})`,
560
+ class:
561
+ "prevcopilotrun modern-session-item" +
562
+ (isActive ? " active-session" : ""),
563
+ },
564
+ div(
565
+ { class: "d-flex justify-content-between align-items-center mb-1" },
566
+ small(
567
+ { class: "text-muted text-truncate", style: "min-width:0" },
568
+ localeDateTime(run.started_at),
569
+ ),
570
+ i({
571
+ class: "far fa-trash-alt text-muted",
572
+ onclick: `delprevrun(event, ${run.id})`,
573
+ }),
574
+ ),
575
+ p({ class: "prevrun_content mb-0" }, preview),
576
+ )
577
+ : div(
578
+ {
579
+ onclick: `set_state_field('run_id',${run.id})`,
580
+ class: "prevcopilotrun border p-2",
581
+ },
582
+ div(
583
+ { class: "d-flex justify-content-between" },
584
+ span(
585
+ { class: "text-truncate", style: "min-width:0" },
586
+ localeDateTime(run.started_at),
587
+ ),
588
+ i({
589
+ class: "far fa-trash-alt",
590
+ onclick: `delprevrun(event, ${run.id})`,
591
+ }),
592
+ ),
593
+ p(
594
+ { class: "prevrun_content" },
595
+ preview,
596
+ ),
597
+ );
598
+ }),
544
599
  )
545
600
  : "";
546
601
 
@@ -629,7 +684,199 @@ const run = async (
629
684
  overflow: hidden;
630
685
  margin-bottom: 0px;
631
686
  display: block;
632
- text-overflow: ellipsis;}`,
687
+ text-overflow: ellipsis;}
688
+ /* Modern Chat Layout */
689
+ .modern-chat-layout {
690
+ display: flex;
691
+ flex-direction: column;
692
+ height: 100%;
693
+ }
694
+ .modern-chat-layout #copilotinteractions {
695
+ max-height: 70vh;
696
+ overflow-y: auto;
697
+ padding: 1rem;
698
+ display: flex;
699
+ flex-direction: column;
700
+ gap: 0.75rem;
701
+ }
702
+ .modern-chat-layout .chat-message {
703
+ display: flex;
704
+ gap: 0.5rem;
705
+ max-width: 85%;
706
+ align-items: flex-start;
707
+ }
708
+ .modern-chat-layout .chat-message.chat-user {
709
+ align-self: flex-end;
710
+ flex-direction: row-reverse;
711
+ }
712
+ .modern-chat-layout .chat-message.chat-assistant {
713
+ align-self: flex-start;
714
+ }
715
+ .modern-chat-layout .chat-avatar {
716
+ width: 2rem;
717
+ height: 2rem;
718
+ border-radius: 50%;
719
+ display: flex;
720
+ align-items: center;
721
+ justify-content: center;
722
+ flex-shrink: 0;
723
+ font-size: 0.85rem;
724
+ background: var(--tblr-secondary-bg-subtle, var(--bs-secondary-bg-subtle, #e9ecef));
725
+ color: var(--tblr-secondary-color, var(--bs-secondary-color, #6c757d));
726
+ }
727
+ .modern-chat-layout .chat-user .chat-avatar {
728
+ background: #0d6efd;
729
+ color: #fff;
730
+ }
731
+ .modern-chat-layout .chat-bubble {
732
+ padding: 0.6rem 1rem;
733
+ border-radius: 1rem;
734
+ line-height: 1.5;
735
+ word-wrap: break-word;
736
+ overflow-wrap: break-word;
737
+ }
738
+ .modern-chat-layout .chat-user .chat-bubble {
739
+ background: #0d6efd;
740
+ color: #fff;
741
+ border-bottom-right-radius: 0.25rem;
742
+ }
743
+ .modern-chat-layout .chat-assistant .chat-bubble {
744
+ background: var(--tblr-secondary-bg-subtle, var(--bs-secondary-bg-subtle, #f0f2f5));
745
+ color: var(--tblr-body-color, var(--bs-body-color, #212529));
746
+ border-bottom-left-radius: 0.25rem;
747
+ }
748
+ /* Markdown content inside bubbles */
749
+ .modern-chat-layout .chat-bubble h1,
750
+ .modern-chat-layout .chat-bubble h2,
751
+ .modern-chat-layout .chat-bubble h3,
752
+ .modern-chat-layout .chat-bubble h4 {
753
+ margin-top: 0.5rem;
754
+ margin-bottom: 0.25rem;
755
+ }
756
+ .modern-chat-layout .chat-bubble h1 { font-size: 1.3rem; }
757
+ .modern-chat-layout .chat-bubble h2 { font-size: 1.15rem; }
758
+ .modern-chat-layout .chat-bubble h3 { font-size: 1.05rem; }
759
+ .modern-chat-layout .chat-bubble h4 { font-size: 1rem; }
760
+ .modern-chat-layout .chat-bubble p {
761
+ margin-bottom: 0.4rem;
762
+ }
763
+ .modern-chat-layout .chat-bubble p:last-child {
764
+ margin-bottom: 0;
765
+ }
766
+ .modern-chat-layout .chat-bubble ul,
767
+ .modern-chat-layout .chat-bubble ol {
768
+ padding-left: 1.5rem;
769
+ margin-bottom: 0.4rem;
770
+ }
771
+ .modern-chat-layout .chat-bubble table {
772
+ width: 100%;
773
+ border-collapse: collapse;
774
+ margin: 0.5rem 0;
775
+ font-size: 0.9em;
776
+ }
777
+ .modern-chat-layout .chat-bubble table th,
778
+ .modern-chat-layout .chat-bubble table td {
779
+ border: 1px solid rgba(0,0,0,0.15);
780
+ padding: 0.3rem 0.5rem;
781
+ }
782
+ .modern-chat-layout .chat-bubble table th {
783
+ background: rgba(0,0,0,0.05);
784
+ font-weight: 600;
785
+ }
786
+ .modern-chat-layout .chat-bubble pre {
787
+ background: rgba(0,0,0,0.06);
788
+ padding: 0.5rem;
789
+ border-radius: 0.5rem;
790
+ overflow-x: auto;
791
+ margin: 0.4rem 0;
792
+ }
793
+ .modern-chat-layout .chat-bubble code {
794
+ font-size: 0.88em;
795
+ }
796
+ .modern-chat-layout .chat-bubble p > code {
797
+ background: rgba(0,0,0,0.06);
798
+ padding: 0.1rem 0.3rem;
799
+ border-radius: 0.25rem;
800
+ }
801
+ .modern-chat-layout .chat-user .chat-bubble pre {
802
+ background: rgba(255,255,255,0.15);
803
+ }
804
+ .modern-chat-layout .chat-user .chat-bubble p > code {
805
+ background: rgba(255,255,255,0.15);
806
+ }
807
+ .modern-chat-layout .chat-user .chat-bubble table th,
808
+ .modern-chat-layout .chat-user .chat-bubble table td {
809
+ border-color: rgba(255,255,255,0.25);
810
+ }
811
+ .modern-chat-layout .chat-user .chat-bubble table th {
812
+ background: rgba(255,255,255,0.1);
813
+ }
814
+ /* Input area for modern chat */
815
+ .modern-chat-layout .copilot-entry {
816
+ border-top: 1px solid var(--tblr-border-color, var(--bs-border-color, #dee2e6));
817
+ padding-top: 0.75rem;
818
+ margin-top: 0.5rem;
819
+ }
820
+ .modern-chat-layout .copilot-entry textarea {
821
+ border-radius: 1.5rem;
822
+ padding: 0.6rem 1rem;
823
+ resize: none;
824
+ }
825
+ /* Streaming scratch in modern chat */
826
+ .modern-chat-layout .next_response_scratch {
827
+ padding: 0 1rem;
828
+ }
829
+ .modern-chat-layout .next_response_scratch:not(:empty) {
830
+ margin-bottom: 0.5rem;
831
+ }
832
+ /* Interaction segment (tool cards) inside modern chat */
833
+ .modern-chat-layout .interaction-segment {
834
+ border-top: none;
835
+ }
836
+ /* Modern Sessions Sidebar */
837
+ .modern-sessions-header {
838
+ display: flex;
839
+ align-items: center;
840
+ justify-content: space-between;
841
+ padding: 0.6rem 0.75rem;
842
+ margin-bottom: 0.75rem;
843
+ background: var(--tblr-secondary-bg-subtle, var(--bs-secondary-bg-subtle, #f8f9fa));
844
+ border-radius: 0.75rem;
845
+ border-bottom: 1px solid var(--tblr-border-color, var(--bs-border-color, #dee2e6));
846
+ position: sticky;
847
+ top: 0;
848
+ z-index: 1;
849
+ }
850
+ .modern-sessions .modern-session-item {
851
+ border-radius: 0.75rem;
852
+ padding: 0.65rem 0.75rem;
853
+ margin-bottom: 0.4rem;
854
+ border: 1px solid var(--tblr-border-color, var(--bs-border-color, #dee2e6));
855
+ cursor: pointer;
856
+ transition: all 0.15s ease;
857
+ }
858
+ .modern-sessions .modern-session-item:hover {
859
+ box-shadow: 0 2px 8px rgba(0,0,0,0.07);
860
+ background-color: var(--tblr-secondary-bg-subtle, var(--bs-secondary-bg-subtle, #f8f9fa));
861
+ }
862
+ .modern-sessions .modern-session-item.active-session {
863
+ border-left: 3px solid #0d6efd;
864
+ background-color: rgba(13, 110, 253, 0.05);
865
+ }
866
+ .modern-sessions .modern-session-item i.fa-trash-alt {
867
+ display: none;
868
+ font-size: 0.8em;
869
+ }
870
+ .modern-sessions .modern-session-item:hover i.fa-trash-alt {
871
+ display: inline;
872
+ }
873
+ .modern-sessions .modern-session-item .prevrun_content {
874
+ font-size: 0.85em;
875
+ color: var(--tblr-secondary-color, var(--bs-secondary-color, #6c757d));
876
+ white-space: nowrap;
877
+ overflow: hidden;
878
+ text-overflow: ellipsis;
879
+ }`,
633
880
  ),
634
881
  script(domReady(`$( "#inputuserinput" ).autogrow({paddingBottom: 20});`)),
635
882
  script(
@@ -661,10 +908,13 @@ const run = async (
661
908
  const $runidin= $("input[name=run_id")
662
909
  if(res.run_id && (!$runidin.val() || $runidin.val()=="undefined"))
663
910
  $runidin.val(res.run_id);
664
- const wrapSegment = (html, who) => '<div class="interaction-segment"><span class="badge bg-secondary">'+who+'</span>'+html+'</div>'
911
+ const currentLayout = ${JSON.stringify(layout || "")};
912
+ const wrapSegment = (html, who, toRight) => currentLayout.startsWith("Modern chat")
913
+ ? '<div class="chat-message '+(toRight ? 'chat-user' : 'chat-assistant')+'"><div class="chat-avatar"><i class="fas '+(toRight ? 'fa-user' : 'fa-robot')+'"></i></div><div class="chat-bubble">'+html+'</div></div>'
914
+ : '<div class="interaction-segment '+(toRight ? 'to-right' : '')+'"><div><div class="badgewrap"><span class="badge bg-secondary">'+who+'</span></div>'+html+'</div></div>'
665
915
  const user_input = $("textarea[name=userinput]").val()
666
916
  if(user_input && (!${JSON.stringify(dyn_updates)}))
667
- $("#copilotinteractions").append(wrapSegment('<p>'+user_input+'</p>'+fileBadge, "You"))
917
+ $("#copilotinteractions").append(wrapSegment('<p>'+user_input+'</p>'+fileBadge, "You", true))
668
918
  $("textarea[name=userinput]").val("")
669
919
  $('form.agent-view div.next_response_scratch').html("")
670
920
  window['stream scratch ${viewname} ${rndid}'] = []
@@ -804,10 +1054,15 @@ const run = async (
804
1054
  initial_q && domReady("$('form.copilot').submit()"),
805
1055
  ),
806
1056
  );
1057
+ const isModern = layout && layout.startsWith("Modern chat");
807
1058
  const main_chat =
808
- layout === "No card"
809
- ? div({ class: "mx-1" }, main_inner)
810
- : div({ class: "card" }, div({ class: "card-body" }, main_inner));
1059
+ layout === "Modern chat"
1060
+ ? div({ class: "card" }, div({ class: "card-body modern-chat-layout" }, main_inner))
1061
+ : layout === "Modern chat - no card"
1062
+ ? div({ class: "modern-chat-layout" }, main_inner)
1063
+ : layout === "No card"
1064
+ ? div({ class: "mx-1" }, main_inner)
1065
+ : div({ class: "card" }, div({ class: "card-body" }, main_inner));
811
1066
 
812
1067
  return show_prev_runs
813
1068
  ? div(
@@ -900,6 +1155,7 @@ const interact = async (table_id, viewname, config, body, { req, res }) => {
900
1155
  p(escapeHtml(userinput)) + fileBadges,
901
1156
  "You",
902
1157
  true,
1158
+ config.layout,
903
1159
  );
904
1160
 
905
1161
  await addToContext(run, {
package/common.js CHANGED
@@ -6,7 +6,7 @@ const { interpolate } = require("@saltcorn/data/utils");
6
6
  const db = require("@saltcorn/data/db");
7
7
 
8
8
  const MarkdownIt = require("markdown-it"),
9
- md = new MarkdownIt();
9
+ md = new MarkdownIt({ html: true, breaks: true, linkify: true });
10
10
 
11
11
  const nubBy = (f, xs) => {
12
12
  const vs = new Set();
@@ -34,9 +34,11 @@ const get_skills = () => {
34
34
  require("./skills/GenerateImage"),
35
35
  require("./skills/ModelContextProtocol"),
36
36
  require("./skills/PromptPicker"),
37
+ require("./skills/ModelPicker"),
37
38
  require("./skills/RunJsCode"),
38
39
  require("./skills/GenerateAndRunJsCode"),
39
40
  require("./skills/Fetch"),
41
+ require("./skills/WebSearch"),
40
42
  require("./skills/Subagent"),
41
43
  //require("./skills/AdaptiveFeedback"),
42
44
  ...exchange_skills,
@@ -127,12 +129,21 @@ const getCompletionArguments = async (
127
129
  ];
128
130
 
129
131
  const skills = get_skill_instances(config);
132
+ const overrides = {};
130
133
  for (const skill of skills) {
131
134
  const sysPr = await skill.systemPrompt?.({
132
135
  ...(formbody || {}),
133
136
  user,
134
137
  triggering_row,
135
138
  });
139
+ const overide =
140
+ (await skill.settingsOverride?.({
141
+ ...(formbody || {}),
142
+ user,
143
+ triggering_row,
144
+ })) || {};
145
+ Object.assign(overrides, overide);
146
+
136
147
  if (sysPr) sysPrompts.push(sysPr);
137
148
  const skillTools = skill.provideTools?.();
138
149
  if (skillTools && Array.isArray(skillTools)) tools.push(...skillTools);
@@ -141,7 +152,9 @@ const getCompletionArguments = async (
141
152
  if (tools.length === 0) tools = undefined;
142
153
  const complArgs = { tools, systemPrompt: sysPrompts.join("\n\n") };
143
154
  if (config.model) complArgs.model = config.model;
144
- if (config.alt_config) complArgs.alt_config = config.alt_config;
155
+
156
+ if (overrides.alt_config || config.alt_config)
157
+ complArgs.alt_config = overrides.alt_config || config.alt_config;
145
158
  return complArgs;
146
159
  };
147
160
 
@@ -194,14 +207,19 @@ const saveInteractions = async (run) => {
194
207
  });
195
208
  };
196
209
 
197
- const wrapSegment = (html, who, to_right) =>
210
+ const wrapSegment = (html, who, to_right, layout) =>
198
211
  who === null
199
212
  ? html
200
- : `<div class="interaction-segment ${to_right ? "to-right" : ""}"><div><div class="badgewrap"><span class="badge bg-secondary">` +
201
- who +
202
- "</span></div>" +
203
- html +
204
- "</div></div>";
213
+ : layout && layout.startsWith("Modern chat")
214
+ ? `<div class="chat-message ${to_right ? "chat-user" : "chat-assistant"}">` +
215
+ `<div class="chat-avatar"><i class="fas ${to_right ? "fa-user" : "fa-robot"}"></i></div>` +
216
+ `<div class="chat-bubble">${html}</div>` +
217
+ `</div>`
218
+ : `<div class="interaction-segment ${to_right ? "to-right" : ""}"><div><div class="badgewrap"><span class="badge bg-secondary">` +
219
+ who +
220
+ "</span></div>" +
221
+ html +
222
+ "</div></div>";
205
223
 
206
224
  const wrapCard = (title, ...inners) =>
207
225
  span({ class: "badge bg-info ms-1" }, title) +
@@ -222,7 +240,7 @@ const process_interaction = async (
222
240
  agentsViewCfg = { stream: false },
223
241
  dyn_updates = false,
224
242
  ) => {
225
- const { stream, viewname } = agentsViewCfg;
243
+ const { stream, viewname, layout } = agentsViewCfg;
226
244
  const sysState = getState();
227
245
  const complArgs = await getCompletionArguments(
228
246
  config,
@@ -232,6 +250,7 @@ const process_interaction = async (
232
250
  );
233
251
  complArgs.appendToChat = true;
234
252
  complArgs.chat = run.context.interactions;
253
+ const use_alt_config = complArgs.alt_config;
235
254
  //complArgs.debugResult = true;
236
255
  //console.log("complArgs", JSON.stringify(complArgs, null, 2));
237
256
  const debugMode = is_debug_mode(config, req.user);
@@ -311,6 +330,8 @@ const process_interaction = async (
311
330
  rendered,
312
331
  ),
313
332
  agent_label,
333
+ false,
334
+ layout,
314
335
  ),
315
336
  );
316
337
  }
@@ -319,7 +340,7 @@ const process_interaction = async (
319
340
  add_response(
320
341
  req.disable_markdown_render
321
342
  ? answer
322
- : wrapSegment(md.render(answer.content), agent_label),
343
+ : wrapSegment(md.render(answer.content), agent_label, false, layout),
323
344
  );
324
345
  }
325
346
 
@@ -332,11 +353,12 @@ const process_interaction = async (
332
353
  add_response(
333
354
  req.disable_markdown_render
334
355
  ? answer
335
- : wrapSegment(md.render(answer.content), agent_label),
356
+ : wrapSegment(md.render(answer.content), agent_label, false, layout),
336
357
  );
337
358
  //const actions = [];
338
359
  let hasResult = false;
339
360
  if ((answer.mcp_calls || []).length && !answer.content) hasResult = true;
361
+ const toolResults = {};
340
362
  if (answer.hasToolCalls)
341
363
  for (const tool_call of answer.getToolCalls()) {
342
364
  getState().log(6, "call function " + tool_call.tool_name);
@@ -379,6 +401,8 @@ const process_interaction = async (
379
401
  rendered,
380
402
  ),
381
403
  agent_label,
404
+ false,
405
+ layout,
382
406
  ),
383
407
  );
384
408
  }
@@ -386,6 +410,7 @@ const process_interaction = async (
386
410
  const result = await tool.tool.process(tool_call.input, {
387
411
  req,
388
412
  });
413
+ toolResults[tool_call.tool_call_id] = result;
389
414
  if (result.stop) stop = true;
390
415
  if (
391
416
  (typeof result === "object" && Object.keys(result || {}).length) ||
@@ -404,6 +429,8 @@ const process_interaction = async (
404
429
  rendered,
405
430
  ),
406
431
  agent_label,
432
+ false,
433
+ layout,
407
434
  ),
408
435
  );
409
436
  }
@@ -429,7 +456,21 @@ const process_interaction = async (
429
456
  await addToContext(run, {
430
457
  interactions: run.context.interactions,
431
458
  });
459
+
460
+ if (myHasResult && !stop) hasResult = true;
461
+ }
462
+ }
463
+
464
+ // postprocess - now all tool calls have responses
465
+ if (answer.hasToolCalls)
466
+ for (const tool_call of answer.getToolCalls()) {
467
+ const tool = find_tool(tool_call.tool_name, config);
468
+ if (tool) {
469
+ let stop = false,
470
+ myHasResult = false;
432
471
  if (tool.tool.postProcess && !stop) {
472
+ let result = toolResults[tool_call.tool_call_id];
473
+
433
474
  const chat = run.context.interactions;
434
475
  let generateUsed = false;
435
476
  const systemPrompt = await getSystemPrompt(
@@ -450,7 +491,7 @@ const process_interaction = async (
450
491
  chat,
451
492
  appendToChat: true,
452
493
  systemPrompt,
453
- alt_config: config.alt_config,
494
+ alt_config: use_alt_config,
454
495
  ...opts,
455
496
  });
456
497
  },
@@ -479,13 +520,19 @@ const process_interaction = async (
479
520
  ],
480
521
  });
481
522
  if (postprocres.add_response) {
523
+ const renderedAddResponse =
524
+ typeof postprocres.add_response === "string"
525
+ ? md.render(postprocres.add_response)
526
+ : postprocres.add_response;
482
527
  add_response(
483
528
  wrapSegment(
484
529
  wrapCard(
485
530
  tool.skill.skill_label || tool.skill.constructor.skill_name,
486
- postprocres.add_response,
531
+ renderedAddResponse,
487
532
  ),
488
533
  agent_label,
534
+ false,
535
+ layout,
489
536
  ),
490
537
  );
491
538
  //replace tool response with this
@@ -563,7 +610,7 @@ const process_interaction = async (
563
610
  add_response(
564
611
  req.disable_markdown_render
565
612
  ? answer
566
- : wrapSegment(md.render(answer), agent_label),
613
+ : wrapSegment(md.render(answer), agent_label, false, layout),
567
614
  );
568
615
  if (dyn_updates)
569
616
  getState().emitDynamicUpdate(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/agents",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "description": "AI agents for Saltcorn",
5
5
  "main": "index.js",
6
6
  "dependencies": {
@@ -0,0 +1,47 @@
1
+ const Workflow = require("@saltcorn/data/models/workflow");
2
+ const Form = require("@saltcorn/data/models/form");
3
+ const FieldRepeat = require("@saltcorn/data/models/fieldrepeat");
4
+ const { select, option } = require("@saltcorn/markup/tags");
5
+ const { eval_expression } = require("@saltcorn/data/models/expression");
6
+ const { validID } = require("@saltcorn/markup/layout_utils");
7
+ const { getState } = require("@saltcorn/data/db/state");
8
+
9
+ class ModelPicker {
10
+ static skill_name = "Model picker";
11
+ get skill_label() {
12
+ return `Model picker`;
13
+ }
14
+ constructor(cfg) {
15
+ Object.assign(this, cfg);
16
+ }
17
+ static async configFields() {
18
+ return [
19
+ { name: "placeholder", label: "Placeholder", type: "String" },
20
+ {
21
+ name: "default_label",
22
+ label: "Default configuration label",
23
+ type: "String",
24
+ },
25
+ ];
26
+ }
27
+ async formWidget({ user, klass }) {
28
+ const llm_cfg_fun = getState().functions.llm_get_configuration;
29
+ const alt_config_options = llm_cfg_fun
30
+ ? llm_cfg_fun.run().alt_config_names || []
31
+ : [];
32
+ return select(
33
+ {
34
+ class: ["form-select form-select-sm w-unset", klass],
35
+ name: "modelpicker",
36
+ },
37
+ this.placeholder && option({ disabled: true }, this.placeholder),
38
+ option({ value: "" }, this.default_label),
39
+ alt_config_options.map((o) => option(o)),
40
+ );
41
+ }
42
+ settingsOverride(body) {
43
+ if (body.modelpicker) return { alt_config: body.modelpicker };
44
+ }
45
+ }
46
+
47
+ module.exports = ModelPicker;
@@ -0,0 +1,96 @@
1
+ const { div, pre, a } = require("@saltcorn/markup/tags");
2
+ const Workflow = require("@saltcorn/data/models/workflow");
3
+ const Form = require("@saltcorn/data/models/form");
4
+ const Table = require("@saltcorn/data/models/table");
5
+ const User = require("@saltcorn/data/models/user");
6
+ const File = require("@saltcorn/data/models/file");
7
+ const View = require("@saltcorn/data/models/view");
8
+ const Trigger = require("@saltcorn/data/models/trigger");
9
+ const FieldRepeat = require("@saltcorn/data/models/fieldrepeat");
10
+ const { getState } = require("@saltcorn/data/db/state");
11
+ const db = require("@saltcorn/data/db");
12
+ const { eval_expression } = require("@saltcorn/data/models/expression");
13
+ const { interpolate, sleep } = require("@saltcorn/data/utils");
14
+ const { features } = require("@saltcorn/data/db/state");
15
+ const { button } = require("@saltcorn/markup/tags");
16
+ const { validID } = require("@saltcorn/markup/layout_utils");
17
+
18
+ const vm = require("vm");
19
+
20
+ //const { fieldProperties } = require("./helpers");
21
+
22
+ class WebSearchSkill {
23
+ static skill_name = "Web search";
24
+
25
+ get skill_label() {
26
+ return "Web search";
27
+ }
28
+
29
+ constructor(cfg) {
30
+ Object.assign(this, cfg);
31
+ }
32
+
33
+ static async configFields() {
34
+ return [
35
+ {
36
+ name: "search_provider",
37
+ label: "Search provider",
38
+ type: "String",
39
+ required: true,
40
+ attributes: { options: ["By URL template"] },
41
+ },
42
+ {
43
+ name: "url_template",
44
+ label: "URL template",
45
+ sublabel: "Use <code>{{q}}</code> for the URL-encoded search phrase",
46
+ type: "String",
47
+ required: true,
48
+ showIf: { search_provider: "By URL template" },
49
+ },
50
+ {
51
+ name: "header",
52
+ label: "Header",
53
+ sublabel: "For example <code>X-Subscription-Token: YOUR_API_KEY</code>",
54
+ type: "String",
55
+ showIf: { search_provider: "By URL template" },
56
+ },
57
+ ];
58
+ }
59
+ systemPrompt() {
60
+ return "If you need to search the web with a search phrase, use the web_search to get search engine results";
61
+ }
62
+
63
+ provideTools = () => {
64
+ return {
65
+ type: "function",
66
+ process: async (row) => {
67
+ const fOpts = { method: "GET" };
68
+ if (this.header) {
69
+ const [key, val] = this.header.split(":");
70
+ const myHeaders = new Headers();
71
+ myHeaders.append(key, val.trim());
72
+ fOpts.headers = myHeaders;
73
+ }
74
+ const url = interpolate(this.url_template, { q: row.search_phrase });
75
+ const resp = await fetch(url);
76
+ return await resp.text();
77
+ },
78
+ function: {
79
+ name: "web_search",
80
+ description: "Search the web with a search engine by a search phrase",
81
+ parameters: {
82
+ type: "object",
83
+ required: ["search_phrase"],
84
+ properties: {
85
+ search_phrase: {
86
+ description: "The phrase to search for",
87
+ type: "string",
88
+ },
89
+ },
90
+ },
91
+ },
92
+ };
93
+ };
94
+ }
95
+
96
+ module.exports = WebSearchSkill;
@@ -29,6 +29,13 @@ beforeAll(async () => {
29
29
  when_trigger: "Never",
30
30
  configuration: require("./agentcfg").maths_agent_cfg,
31
31
  });
32
+ await Trigger.create({
33
+ name: "OracleAgent",
34
+ description: "The all-knowing oracle answers any question",
35
+ action: "Agent",
36
+ when_trigger: "Never",
37
+ configuration: require("./agentcfg").oracle_agent_cfg,
38
+ });
32
39
 
33
40
  await getState().refresh_triggers(false);
34
41
  //await getState().setConfig("log_level", 6);
@@ -109,6 +116,27 @@ for (const nameconfig of require("./configs")) {
109
116
  });
110
117
  expect(result.json.response).toContain("987");
111
118
  });
119
+ it("run multiple subagents concurrenty", async () => {
120
+ const configuration = { ...require("./agentcfg").agent1 };
121
+ configuration.skills = [
122
+ ...configuration.skills,
123
+ {
124
+ agent_name: "OracleAgent",
125
+ skill_type: "Subagent",
126
+ },
127
+ ];
128
+ const result = await action.run({
129
+ row: {
130
+ theprompt:
131
+ "What is the 16th Fibonacci number (when F1=1 and F2=1)? Consult both the math agent and the oracle and see if they agree",
132
+ },
133
+ configuration,
134
+ user,
135
+ req: { user },
136
+ });
137
+ expect(result.json.response).toContain("987");
138
+ });
112
139
  });
140
+
113
141
  //break;
114
142
  }
package/tests/agentcfg.js CHANGED
@@ -65,4 +65,26 @@ const maths_agent_cfg = {
65
65
  "If the user asks an arithmetic question, generate javascript code to solve it with the generate_arithmetic_code tool",
66
66
  };
67
67
 
68
- module.exports = { agent1, maths_agent_cfg };
68
+ const oracle_agent_cfg = {
69
+ model: "",
70
+ prompt: "{{theprompt}}",
71
+ skills: [
72
+ {
73
+ tool_name: "ask_the_oracle",
74
+ tool_description: "Ask the all-knowing oracle a question",
75
+ skill_type: "Run JavaScript code",
76
+ js_code: "return '987';",
77
+ toolargs: [
78
+ {
79
+ name: "question",
80
+ description: "The question you would like to ask",
81
+ argtype: "string",
82
+ },
83
+ ],
84
+ },
85
+ ],
86
+ sys_prompt:
87
+ "You can ask a question of the all-knowing oracle by calling the ask_the_oracle tool ",
88
+ };
89
+
90
+ module.exports = { agent1, maths_agent_cfg, oracle_agent_cfg };