@alveus-ai/std 0.1.6 → 0.1.12
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/dist/blobs/68b4b4a3d053cd95b88ffc43e60b902db57ef4dc94b2fc43402c3a19a5f0c80a.js +185 -0
- package/dist/blobs/8f3730f82a6aa169634575791cba70d0d17fef6ae67eb182c8f51bfee3250826.js +40 -0
- package/dist/blobs/f3562eafded6e98cec4f63690a7dd2c02a40a7d1288fd0e72a8b5a3eabf5ef1e.js +40 -0
- package/package.json +4 -3
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/agents/virtual-agent.ts
|
|
20
|
+
var virtual_agent_exports = {};
|
|
21
|
+
__export(virtual_agent_exports, {
|
|
22
|
+
default: () => virtual_agent_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(virtual_agent_exports);
|
|
25
|
+
var __alveusHandler = async (_state, event, ctx) => {
|
|
26
|
+
const BUILTIN_EXECUTORS = {
|
|
27
|
+
"builtin:calculator": async ({ expression }) => {
|
|
28
|
+
const sanitized = String(expression).replace(/[^0-9+\-*/().%\s]/g, "");
|
|
29
|
+
if (sanitized !== String(expression)) {
|
|
30
|
+
throw new Error("Invalid characters in expression");
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const result = new Function(`return ${sanitized}`)();
|
|
34
|
+
return String(result);
|
|
35
|
+
} catch {
|
|
36
|
+
throw new Error(`Failed to evaluate: ${expression}`);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"builtin:string": async ({ operation, text }) => {
|
|
40
|
+
switch (operation) {
|
|
41
|
+
case "length":
|
|
42
|
+
return String(text.length);
|
|
43
|
+
case "uppercase":
|
|
44
|
+
return text.toUpperCase();
|
|
45
|
+
case "lowercase":
|
|
46
|
+
return text.toLowerCase();
|
|
47
|
+
case "reverse":
|
|
48
|
+
return text.split("").reverse().join("");
|
|
49
|
+
default:
|
|
50
|
+
throw new Error(`Unknown operation: ${operation}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const buildPrompt = (basePrompt, tools2) => {
|
|
55
|
+
const toolDescriptions = Object.entries(tools2).map(([name, tool]) => {
|
|
56
|
+
const params = tool.parameters || "{ ... }";
|
|
57
|
+
return `- ${name}: ${tool.description}
|
|
58
|
+
Parameters: ${params}`;
|
|
59
|
+
}).join("\n");
|
|
60
|
+
return `${basePrompt}
|
|
61
|
+
|
|
62
|
+
Available tools:
|
|
63
|
+
${toolDescriptions}
|
|
64
|
+
|
|
65
|
+
You MUST respond with a JSON object in this exact format:
|
|
66
|
+
{
|
|
67
|
+
"thought": "Your reasoning about what to do next",
|
|
68
|
+
"action": { "tool": "tool_name", "params": { ... } } OR null if you have the final answer,
|
|
69
|
+
"answer": "Your final answer to the user" OR null if you need to use a tool
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
Rules:
|
|
73
|
+
1. Always start with a thought explaining your reasoning
|
|
74
|
+
2. If you need information or need to calculate something, use a tool by setting "action"
|
|
75
|
+
3. When you have enough information, set "answer" and leave "action" null
|
|
76
|
+
4. Use exactly one tool per response
|
|
77
|
+
5. Never make up information - use tools to get facts`;
|
|
78
|
+
};
|
|
79
|
+
const buildMsgs = (sysPrompt, userInput, steps2) => {
|
|
80
|
+
const messages = [
|
|
81
|
+
{ role: "system", content: sysPrompt },
|
|
82
|
+
{ role: "user", content: userInput }
|
|
83
|
+
];
|
|
84
|
+
for (const step of steps2) {
|
|
85
|
+
if (step.type === "thought") {
|
|
86
|
+
messages.push({
|
|
87
|
+
role: "assistant",
|
|
88
|
+
content: JSON.stringify({
|
|
89
|
+
thought: step.content,
|
|
90
|
+
action: step.tool ? { tool: step.tool, params: step.params } : null,
|
|
91
|
+
answer: null
|
|
92
|
+
})
|
|
93
|
+
});
|
|
94
|
+
} else if (step.type === "observation") {
|
|
95
|
+
messages.push({
|
|
96
|
+
role: "user",
|
|
97
|
+
content: `Tool result: ${step.content}`
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return messages;
|
|
102
|
+
};
|
|
103
|
+
const executeTool = async (toolName, hash, params) => {
|
|
104
|
+
if (hash.startsWith("builtin:")) {
|
|
105
|
+
const executor = BUILTIN_EXECUTORS[hash];
|
|
106
|
+
if (!executor) {
|
|
107
|
+
throw new Error(`Unknown built-in tool: ${hash}`);
|
|
108
|
+
}
|
|
109
|
+
return await executor(params);
|
|
110
|
+
}
|
|
111
|
+
ctx.logger.info(`Calling tool agent: ${toolName}`, { hash: hash.slice(0, 16), params });
|
|
112
|
+
const result = await ctx.call(hash, params);
|
|
113
|
+
return typeof result === "string" ? result : JSON.stringify(result);
|
|
114
|
+
};
|
|
115
|
+
const { systemPrompt, tools, input, context, maxIterations = 10, provider, model } = event;
|
|
116
|
+
const steps = [];
|
|
117
|
+
let iterations = 0;
|
|
118
|
+
const fullPrompt = buildPrompt(systemPrompt, tools);
|
|
119
|
+
const fullInput = context ? `Context: ${context}
|
|
120
|
+
|
|
121
|
+
Question: ${input}` : input;
|
|
122
|
+
ctx.logger.info("Starting ReAct loop", { maxIterations, toolCount: Object.keys(tools).length });
|
|
123
|
+
while (iterations < maxIterations) {
|
|
124
|
+
iterations++;
|
|
125
|
+
const messages = buildMsgs(fullPrompt, fullInput, steps);
|
|
126
|
+
const responseText = await ctx.llm.chat(messages, { jsonMode: true, provider, model });
|
|
127
|
+
let response;
|
|
128
|
+
try {
|
|
129
|
+
response = JSON.parse(responseText);
|
|
130
|
+
} catch {
|
|
131
|
+
steps.push({
|
|
132
|
+
type: "observation",
|
|
133
|
+
content: "Error: Invalid JSON response. Please respond with valid JSON."
|
|
134
|
+
});
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (response.thought) {
|
|
138
|
+
ctx.logger.info(`Thought: ${response.thought.slice(0, 100)}...`);
|
|
139
|
+
steps.push({ type: "thought", content: response.thought });
|
|
140
|
+
}
|
|
141
|
+
if (response.answer && !response.action) {
|
|
142
|
+
ctx.logger.info("ReAct completed with answer", { iterations });
|
|
143
|
+
return { answer: response.answer, steps, iterations };
|
|
144
|
+
}
|
|
145
|
+
if (response.action) {
|
|
146
|
+
const toolName = response.action.tool;
|
|
147
|
+
const toolDef = tools[toolName];
|
|
148
|
+
if (!toolDef) {
|
|
149
|
+
steps.push({
|
|
150
|
+
type: "action",
|
|
151
|
+
content: `Attempted unknown tool: ${toolName}`,
|
|
152
|
+
tool: toolName,
|
|
153
|
+
params: response.action.params
|
|
154
|
+
});
|
|
155
|
+
steps.push({
|
|
156
|
+
type: "observation",
|
|
157
|
+
content: `Error: Tool "${toolName}" not available. Available tools: ${Object.keys(tools).join(", ")}`
|
|
158
|
+
});
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
steps.push({
|
|
162
|
+
type: "action",
|
|
163
|
+
content: `Using ${toolName}`,
|
|
164
|
+
tool: toolName,
|
|
165
|
+
params: response.action.params
|
|
166
|
+
});
|
|
167
|
+
try {
|
|
168
|
+
const result = await executeTool(toolName, toolDef.hash, response.action.params);
|
|
169
|
+
steps.push({ type: "observation", content: result });
|
|
170
|
+
} catch (error) {
|
|
171
|
+
steps.push({
|
|
172
|
+
type: "observation",
|
|
173
|
+
content: `Error: ${error instanceof Error ? error.message : String(error)}`
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
ctx.logger.warn("ReAct hit max iterations", { maxIterations });
|
|
179
|
+
return {
|
|
180
|
+
answer: `Unable to complete within ${maxIterations} iterations.`,
|
|
181
|
+
steps,
|
|
182
|
+
iterations
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
var virtual_agent_default = __alveusHandler;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/agents/virtual-agent.ts
|
|
20
|
+
var virtual_agent_exports = {};
|
|
21
|
+
__export(virtual_agent_exports, {
|
|
22
|
+
default: () => virtual_agent_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(virtual_agent_exports);
|
|
25
|
+
var __alveusHandler = async (_state, event, ctx) => {
|
|
26
|
+
const messages = [
|
|
27
|
+
{ role: "system", content: systemPrompt }
|
|
28
|
+
];
|
|
29
|
+
if (event.history) {
|
|
30
|
+
messages.push(...event.history);
|
|
31
|
+
}
|
|
32
|
+
messages.push({ role: "user", content: event.message });
|
|
33
|
+
const reply = await ctx.llm.chat(messages, {
|
|
34
|
+
...chatOptions,
|
|
35
|
+
jsonMode: true
|
|
36
|
+
});
|
|
37
|
+
const data = JSON.parse(reply);
|
|
38
|
+
return { data, raw: reply };
|
|
39
|
+
};
|
|
40
|
+
var virtual_agent_default = __alveusHandler;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/agents/virtual-agent.ts
|
|
20
|
+
var virtual_agent_exports = {};
|
|
21
|
+
__export(virtual_agent_exports, {
|
|
22
|
+
default: () => virtual_agent_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(virtual_agent_exports);
|
|
25
|
+
var __alveusHandler = async (_state, event, ctx) => {
|
|
26
|
+
const messages = [
|
|
27
|
+
{ role: "system", content: systemPrompt }
|
|
28
|
+
];
|
|
29
|
+
if (event.history) {
|
|
30
|
+
messages.push(...event.history);
|
|
31
|
+
}
|
|
32
|
+
messages.push({ role: "user", content: event.message });
|
|
33
|
+
const reply = await ctx.llm.chat(messages, chatOptions);
|
|
34
|
+
return {
|
|
35
|
+
reply,
|
|
36
|
+
provider: chatOptions.provider,
|
|
37
|
+
model: chatOptions.model
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
var virtual_agent_default = __alveusHandler;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alveus-ai/std",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "Standard library of reusable agents and utilities for Alveus",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -27,9 +27,10 @@
|
|
|
27
27
|
"dist"
|
|
28
28
|
],
|
|
29
29
|
"scripts": {
|
|
30
|
-
"build": "tsc",
|
|
30
|
+
"build": "tsc && node --import tsx scripts/build-blobs.ts",
|
|
31
31
|
"dev": "tsc --watch",
|
|
32
|
-
"clean": "rm -rf dist .alveus"
|
|
32
|
+
"clean": "rm -rf dist .alveus",
|
|
33
|
+
"seed": "node --import tsx scripts/seed.ts"
|
|
33
34
|
},
|
|
34
35
|
"publishConfig": {
|
|
35
36
|
"access": "public"
|