-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (132 loc) · 3.87 KB
/
index.js
File metadata and controls
136 lines (132 loc) · 3.87 KB
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
const Workflow = require("@saltcorn/data/models/workflow");
const Form = require("@saltcorn/data/models/form");
const FieldRepeat = require("@saltcorn/data/models/fieldrepeat");
const Trigger = require("@saltcorn/data/models/trigger");
const Table = require("@saltcorn/data/models/table");
const {
get_skills,
getCompletionArguments,
process_interaction,
get_skill_instances,
} = require("./common");
const { applyAsync } = require("@saltcorn/data/utils");
const WorkflowRun = require("@saltcorn/data/models/workflow_run");
const { interpolate } = require("@saltcorn/data/utils");
const { getState } = require("@saltcorn/data/db/state");
module.exports = {
sc_plugin_api_version: 1,
dependencies: ["@saltcorn/large-language-model"],
viewtemplates: [require("./agent-view")],
plugin_name: "agents",
ready_for_mobile: true,
headers: [
{
script: `/plugins/public/agents@${
require("./package.json").version
}/markdown-it.min.js`,
onlyViews: ["Agent Chat", "Saltcorn Agent copilot"],
},
{
script: `/plugins/public/agents@${
require("./package.json").version
}/jquery.autogrow-textarea.js`,
onlyViews: ["Agent Chat", "Saltcorn Agent copilot"],
},
],
actions: {
Agent: require("./action"),
},
functions: {
inspect_agent: {
run: async (agent, user, row) => {
const action = agent.runWithoutRow
? agent
: await Trigger.findOne(
typeof agent == "number" ? { id: agent } : { name: agent },
);
const complArgs = await getCompletionArguments(
action.configuration,
user,
row,
);
const skills = get_skill_instances(action.configuration);
const skill_tools = [];
for (const skill of skills) {
const skillTools = skill.provideTools?.();
const tools = !skillTools
? []
: Array.isArray(skillTools)
? skillTools
: [skillTools];
skill_tools.push(...tools);
}
return {
...complArgs,
action,
skills,
skill_tools,
};
},
isAsync: true,
description: "Return system prompt, tools and action of an agent",
},
agent_generate: {
run: async (agent_name, prompt, opts = {}) => {
const action = await Trigger.findOne({ name: agent_name });
let run;
let context = {
implemented_fcall_ids: [],
interactions: [
...(opts.interactions || []),
{ role: "user", content: prompt },
],
funcalls: {},
};
if (opts.run_id === null || (!opts.run_id && opts.run === null))
run = { context };
else if (opts.run) run = opts.run;
else if (opts.run_id)
run = await WorkflowRun.findOne({ id: +opts.run_id });
else
run = await WorkflowRun.create({
status: "Running",
started_by: opts.user?.id,
trigger_id: action.id,
context,
});
const result = await process_interaction(
run,
action.configuration,
{
user: opts?.user,
body: {},
disable_markdown_render:
typeof opts.disable_markdown_render !== "undefined"
? opts.disable_markdown_render
: !opts?.render_markdown,
},
null,
);
return {
text: result.json.response,
run,
...(run.id ? { run_id: run.id } : {}),
};
},
isAsync: true,
description: "Run an agent on a prompt",
arguments: [
{ name: "agent_name", type: "String" },
{ name: "prompt", type: "String" },
],
},
},
};
/*
TODO
-embedding retrieval list view
-optional user confirm: action, insert
-Preload data
-sql access
-memory
*/