@agentskit/runtime 0.4.0
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/LICENSE +21 -0
- package/dist/index.cjs +195 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +193 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Emerson Braun
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@agentskit/core');
|
|
4
|
+
|
|
5
|
+
// src/runner.ts
|
|
6
|
+
function safeParseArgs(args) {
|
|
7
|
+
try {
|
|
8
|
+
const parsed = JSON.parse(args);
|
|
9
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
10
|
+
} catch {
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function createRuntime(config) {
|
|
15
|
+
const emitter = core.createEventEmitter();
|
|
16
|
+
for (const observer of config.observers ?? []) {
|
|
17
|
+
emitter.addObserver(observer);
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
async run(task, options) {
|
|
21
|
+
const startTime = Date.now();
|
|
22
|
+
const maxSteps = options?.maxSteps ?? config.maxSteps ?? 10;
|
|
23
|
+
const signal = options?.signal;
|
|
24
|
+
let skillTools = [];
|
|
25
|
+
let systemPrompt = options?.systemPrompt ?? config.systemPrompt ?? "";
|
|
26
|
+
if (options?.skill) {
|
|
27
|
+
systemPrompt = options.skill.systemPrompt;
|
|
28
|
+
if (options.skill.onActivate) {
|
|
29
|
+
const activation = await options.skill.onActivate();
|
|
30
|
+
skillTools = activation.tools ?? [];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const toolMap = /* @__PURE__ */ new Map();
|
|
34
|
+
for (const tool of config.tools ?? []) toolMap.set(tool.name, tool);
|
|
35
|
+
for (const tool of options?.tools ?? []) toolMap.set(tool.name, tool);
|
|
36
|
+
for (const tool of skillTools) toolMap.set(tool.name, tool);
|
|
37
|
+
const tools = [...toolMap.values()];
|
|
38
|
+
const initialized = /* @__PURE__ */ new Set();
|
|
39
|
+
const initTool = async (tool) => {
|
|
40
|
+
if (tool.init && !initialized.has(tool.name)) {
|
|
41
|
+
await tool.init();
|
|
42
|
+
initialized.add(tool.name);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const disposeAll = async () => {
|
|
46
|
+
for (const name of initialized) {
|
|
47
|
+
const tool = toolMap.get(name);
|
|
48
|
+
try {
|
|
49
|
+
await tool?.dispose?.();
|
|
50
|
+
} catch {
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const messages = [];
|
|
55
|
+
if (systemPrompt) {
|
|
56
|
+
messages.push(core.buildMessage({ role: "system", content: systemPrompt }));
|
|
57
|
+
}
|
|
58
|
+
messages.push(core.buildMessage({ role: "user", content: task }));
|
|
59
|
+
const allToolCalls = [];
|
|
60
|
+
let step = 0;
|
|
61
|
+
let finalContent = "";
|
|
62
|
+
try {
|
|
63
|
+
while (step < maxSteps) {
|
|
64
|
+
if (signal?.aborted) {
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
step++;
|
|
68
|
+
emitter.emit({ type: "agent:step", step, action: step === 1 ? "initial" : "tool-result-loop" });
|
|
69
|
+
const request = {
|
|
70
|
+
messages,
|
|
71
|
+
context: {
|
|
72
|
+
systemPrompt,
|
|
73
|
+
temperature: options?.skill?.temperature ?? config.temperature,
|
|
74
|
+
maxTokens: config.maxTokens,
|
|
75
|
+
tools
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const streamStart = Date.now();
|
|
79
|
+
const source = config.adapter.createSource(request);
|
|
80
|
+
emitter.emit({ type: "llm:start", messageCount: messages.length });
|
|
81
|
+
let accumulatedText = "";
|
|
82
|
+
const stepToolCalls = [];
|
|
83
|
+
let streamError = null;
|
|
84
|
+
await core.consumeStream(source, {
|
|
85
|
+
onText(accumulated) {
|
|
86
|
+
accumulatedText = accumulated;
|
|
87
|
+
},
|
|
88
|
+
async onToolCall(chunk) {
|
|
89
|
+
if (chunk.toolCall) {
|
|
90
|
+
stepToolCalls.push({ toolCall: chunk.toolCall });
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
onError(error) {
|
|
94
|
+
streamError = error;
|
|
95
|
+
},
|
|
96
|
+
onDone(accumulated) {
|
|
97
|
+
accumulatedText = accumulated;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
emitter.emit({
|
|
101
|
+
type: "llm:end",
|
|
102
|
+
content: accumulatedText,
|
|
103
|
+
durationMs: Date.now() - streamStart
|
|
104
|
+
});
|
|
105
|
+
if (streamError) {
|
|
106
|
+
emitter.emit({ type: "error", error: streamError });
|
|
107
|
+
throw streamError;
|
|
108
|
+
}
|
|
109
|
+
if (signal?.aborted) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
const assistantToolCalls = stepToolCalls.map(({ toolCall }) => ({
|
|
113
|
+
id: toolCall.id,
|
|
114
|
+
name: toolCall.name,
|
|
115
|
+
args: safeParseArgs(toolCall.args),
|
|
116
|
+
status: "pending"
|
|
117
|
+
}));
|
|
118
|
+
const assistantMessage = core.buildMessage({
|
|
119
|
+
role: "assistant",
|
|
120
|
+
content: accumulatedText,
|
|
121
|
+
status: "complete"
|
|
122
|
+
});
|
|
123
|
+
if (assistantToolCalls.length > 0) {
|
|
124
|
+
assistantMessage.toolCalls = assistantToolCalls;
|
|
125
|
+
}
|
|
126
|
+
messages.push(assistantMessage);
|
|
127
|
+
if (assistantToolCalls.length === 0) {
|
|
128
|
+
finalContent = accumulatedText;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
for (const toolCall of assistantToolCalls) {
|
|
132
|
+
if (signal?.aborted) break;
|
|
133
|
+
const tool = toolMap.get(toolCall.name);
|
|
134
|
+
allToolCalls.push(toolCall);
|
|
135
|
+
if (!tool?.execute) {
|
|
136
|
+
const errorMsg = `Tool "${toolCall.name}" not found or has no execute function`;
|
|
137
|
+
toolCall.status = "error";
|
|
138
|
+
toolCall.error = errorMsg;
|
|
139
|
+
messages.push(core.buildMessage({ role: "tool", content: errorMsg }));
|
|
140
|
+
emitter.emit({ type: "error", error: new Error(errorMsg) });
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
await initTool(tool);
|
|
144
|
+
emitter.emit({ type: "tool:start", name: toolCall.name, args: toolCall.args });
|
|
145
|
+
const toolStart = Date.now();
|
|
146
|
+
try {
|
|
147
|
+
const result = await core.executeToolCall(tool, toolCall.args, {
|
|
148
|
+
messages,
|
|
149
|
+
call: toolCall
|
|
150
|
+
});
|
|
151
|
+
toolCall.status = "complete";
|
|
152
|
+
toolCall.result = result;
|
|
153
|
+
messages.push(core.buildMessage({ role: "tool", content: result }));
|
|
154
|
+
emitter.emit({
|
|
155
|
+
type: "tool:end",
|
|
156
|
+
name: toolCall.name,
|
|
157
|
+
result,
|
|
158
|
+
durationMs: Date.now() - toolStart
|
|
159
|
+
});
|
|
160
|
+
} catch (error) {
|
|
161
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
162
|
+
toolCall.status = "error";
|
|
163
|
+
toolCall.error = errorMsg;
|
|
164
|
+
messages.push(core.buildMessage({ role: "tool", content: `Error: ${errorMsg}` }));
|
|
165
|
+
emitter.emit({
|
|
166
|
+
type: "tool:end",
|
|
167
|
+
name: toolCall.name,
|
|
168
|
+
result: `Error: ${errorMsg}`,
|
|
169
|
+
durationMs: Date.now() - toolStart
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
finalContent = accumulatedText;
|
|
174
|
+
}
|
|
175
|
+
if (config.memory) {
|
|
176
|
+
await config.memory.save(messages);
|
|
177
|
+
emitter.emit({ type: "memory:save", messageCount: messages.length });
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
content: finalContent,
|
|
181
|
+
messages,
|
|
182
|
+
steps: step,
|
|
183
|
+
toolCalls: allToolCalls,
|
|
184
|
+
durationMs: Date.now() - startTime
|
|
185
|
+
};
|
|
186
|
+
} finally {
|
|
187
|
+
await disposeAll();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
exports.createRuntime = createRuntime;
|
|
194
|
+
//# sourceMappingURL=index.cjs.map
|
|
195
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
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EAAY,IAAA,CAAK,GAAA,EAAI,GAAI;AAAA,SAC3B;AAAA,MACF,CAAA,SAAE;AACA,QAAA,MAAM,UAAA,EAAW;AAAA,MACnB;AAAA,IACF;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["import {\n buildMessage,\n consumeStream,\n createEventEmitter,\n executeToolCall,\n generateId,\n} from '@agentskit/core'\nimport type {\n AdapterRequest,\n Message,\n StreamChunk,\n ToolCall,\n ToolDefinition,\n} from '@agentskit/core'\nimport type { RuntimeConfig, RunOptions, RunResult } from './types'\n\nfunction safeParseArgs(args: string): Record<string, unknown> {\n try {\n const parsed = JSON.parse(args)\n return parsed && typeof parsed === 'object' ? (parsed as Record<string, unknown>) : {}\n } catch {\n return {}\n }\n}\n\nexport function createRuntime(config: RuntimeConfig) {\n const emitter = createEventEmitter()\n\n for (const observer of config.observers ?? []) {\n emitter.addObserver(observer)\n }\n\n return {\n async run(task: string, options?: RunOptions): Promise<RunResult> {\n const startTime = Date.now()\n\n // Resolve effective config\n const maxSteps = options?.maxSteps ?? config.maxSteps ?? 10\n const signal = options?.signal\n\n // Activate skill if provided\n let skillTools: ToolDefinition[] = []\n let systemPrompt = options?.systemPrompt ?? config.systemPrompt ?? ''\n\n if (options?.skill) {\n systemPrompt = options.skill.systemPrompt\n if (options.skill.onActivate) {\n const activation = await options.skill.onActivate()\n skillTools = activation.tools ?? []\n }\n }\n\n // Merge tools: config < options < skill (last wins on name collision)\n const toolMap = new Map<string, ToolDefinition>()\n for (const tool of config.tools ?? []) toolMap.set(tool.name, tool)\n for (const tool of options?.tools ?? []) toolMap.set(tool.name, tool)\n for (const tool of skillTools) toolMap.set(tool.name, tool)\n const tools = [...toolMap.values()]\n\n // Lazy init tracking\n const initialized = new Set<string>()\n\n const initTool = async (tool: ToolDefinition) => {\n if (tool.init && !initialized.has(tool.name)) {\n await tool.init()\n initialized.add(tool.name)\n }\n }\n\n const disposeAll = async () => {\n for (const name of initialized) {\n const tool = toolMap.get(name)\n try {\n await tool?.dispose?.()\n } catch {\n // Dispose errors should not propagate\n }\n }\n }\n\n // Build initial messages\n const messages: Message[] = []\n\n if (systemPrompt) {\n messages.push(buildMessage({ role: 'system', content: systemPrompt }))\n }\n\n messages.push(buildMessage({ role: 'user', content: task }))\n\n const allToolCalls: ToolCall[] = []\n let step = 0\n let finalContent = ''\n\n try {\n while (step < maxSteps) {\n if (signal?.aborted) {\n break\n }\n\n step++\n emitter.emit({ type: 'agent:step', step, action: step === 1 ? 'initial' : 'tool-result-loop' })\n\n // Build adapter request\n const request: AdapterRequest = {\n messages,\n context: {\n systemPrompt,\n temperature: options?.skill?.temperature ?? config.temperature,\n maxTokens: config.maxTokens,\n tools,\n },\n }\n\n // Call adapter\n const streamStart = Date.now()\n const source = config.adapter.createSource(request)\n emitter.emit({ type: 'llm:start', messageCount: messages.length })\n\n let accumulatedText = ''\n const stepToolCalls: Array<{ toolCall: StreamChunk['toolCall'] }> = []\n let streamError: Error | null = null\n\n await consumeStream(source, {\n onText(accumulated) {\n accumulatedText = accumulated\n },\n async onToolCall(chunk) {\n if (chunk.toolCall) {\n stepToolCalls.push({ toolCall: chunk.toolCall })\n }\n },\n onError(error) {\n streamError = error\n },\n onDone(accumulated) {\n accumulatedText = accumulated\n },\n })\n\n emitter.emit({\n type: 'llm:end',\n content: accumulatedText,\n durationMs: Date.now() - streamStart,\n })\n\n if (streamError) {\n emitter.emit({ type: 'error', error: streamError })\n throw streamError\n }\n\n if (signal?.aborted) {\n break\n }\n\n // Build assistant message with tool calls\n const assistantToolCalls: ToolCall[] = stepToolCalls.map(({ toolCall }) => ({\n id: toolCall!.id,\n name: toolCall!.name,\n args: safeParseArgs(toolCall!.args),\n status: 'pending' as const,\n }))\n\n const assistantMessage = buildMessage({\n role: 'assistant',\n content: accumulatedText,\n status: 'complete',\n })\n if (assistantToolCalls.length > 0) {\n assistantMessage.toolCalls = assistantToolCalls\n }\n messages.push(assistantMessage)\n\n // No tool calls → agent is done\n if (assistantToolCalls.length === 0) {\n finalContent = accumulatedText\n break\n }\n\n // Execute each tool call and inject results\n for (const toolCall of assistantToolCalls) {\n if (signal?.aborted) break\n\n const tool = toolMap.get(toolCall.name)\n allToolCalls.push(toolCall)\n\n if (!tool?.execute) {\n // No executor — inject error as tool result\n const errorMsg = `Tool \"${toolCall.name}\" not found or has no execute function`\n toolCall.status = 'error'\n toolCall.error = errorMsg\n messages.push(buildMessage({ role: 'tool', content: errorMsg }))\n emitter.emit({ type: 'error', error: new Error(errorMsg) })\n continue\n }\n\n // Lazy init\n await initTool(tool)\n\n emitter.emit({ type: 'tool:start', name: toolCall.name, args: toolCall.args })\n const toolStart = Date.now()\n\n try {\n const result = await executeToolCall(tool, toolCall.args, {\n messages,\n call: toolCall,\n })\n toolCall.status = 'complete'\n toolCall.result = result\n messages.push(buildMessage({ role: 'tool', content: result }))\n emitter.emit({\n type: 'tool:end',\n name: toolCall.name,\n result,\n durationMs: Date.now() - toolStart,\n })\n } catch (error) {\n // Inject error as tool result — let LLM decide what to do\n const errorMsg = error instanceof Error ? error.message : String(error)\n toolCall.status = 'error'\n toolCall.error = errorMsg\n messages.push(buildMessage({ role: 'tool', content: `Error: ${errorMsg}` }))\n emitter.emit({\n type: 'tool:end',\n name: toolCall.name,\n result: `Error: ${errorMsg}`,\n durationMs: Date.now() - toolStart,\n })\n }\n }\n\n // After last iteration with tool calls, if we hit maxSteps next loop will break\n finalContent = accumulatedText\n }\n\n // Save to memory if configured\n if (config.memory) {\n await config.memory.save(messages)\n emitter.emit({ type: 'memory:save', messageCount: messages.length })\n }\n\n return {\n content: finalContent,\n messages,\n steps: step,\n toolCalls: allToolCalls,\n durationMs: Date.now() - startTime,\n }\n } finally {\n await disposeAll()\n }\n },\n }\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ToolDefinition, SkillDefinition, Message, ToolCall, AdapterFactory, ChatMemory, Observer } from '@agentskit/core';
|
|
2
|
+
|
|
3
|
+
interface RuntimeConfig {
|
|
4
|
+
adapter: AdapterFactory;
|
|
5
|
+
tools?: ToolDefinition[];
|
|
6
|
+
systemPrompt?: string;
|
|
7
|
+
memory?: ChatMemory;
|
|
8
|
+
observers?: Observer[];
|
|
9
|
+
maxSteps?: number;
|
|
10
|
+
temperature?: number;
|
|
11
|
+
maxTokens?: number;
|
|
12
|
+
}
|
|
13
|
+
interface RunOptions {
|
|
14
|
+
tools?: ToolDefinition[];
|
|
15
|
+
systemPrompt?: string;
|
|
16
|
+
skill?: SkillDefinition;
|
|
17
|
+
maxSteps?: number;
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
interface RunResult {
|
|
21
|
+
content: string;
|
|
22
|
+
messages: Message[];
|
|
23
|
+
steps: number;
|
|
24
|
+
toolCalls: ToolCall[];
|
|
25
|
+
durationMs: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare function createRuntime(config: RuntimeConfig): {
|
|
29
|
+
run(task: string, options?: RunOptions): Promise<RunResult>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { type RunOptions, type RunResult, type RuntimeConfig, createRuntime };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ToolDefinition, SkillDefinition, Message, ToolCall, AdapterFactory, ChatMemory, Observer } from '@agentskit/core';
|
|
2
|
+
|
|
3
|
+
interface RuntimeConfig {
|
|
4
|
+
adapter: AdapterFactory;
|
|
5
|
+
tools?: ToolDefinition[];
|
|
6
|
+
systemPrompt?: string;
|
|
7
|
+
memory?: ChatMemory;
|
|
8
|
+
observers?: Observer[];
|
|
9
|
+
maxSteps?: number;
|
|
10
|
+
temperature?: number;
|
|
11
|
+
maxTokens?: number;
|
|
12
|
+
}
|
|
13
|
+
interface RunOptions {
|
|
14
|
+
tools?: ToolDefinition[];
|
|
15
|
+
systemPrompt?: string;
|
|
16
|
+
skill?: SkillDefinition;
|
|
17
|
+
maxSteps?: number;
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
interface RunResult {
|
|
21
|
+
content: string;
|
|
22
|
+
messages: Message[];
|
|
23
|
+
steps: number;
|
|
24
|
+
toolCalls: ToolCall[];
|
|
25
|
+
durationMs: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare function createRuntime(config: RuntimeConfig): {
|
|
29
|
+
run(task: string, options?: RunOptions): Promise<RunResult>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { type RunOptions, type RunResult, type RuntimeConfig, createRuntime };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { createEventEmitter, buildMessage, consumeStream, executeToolCall } from '@agentskit/core';
|
|
2
|
+
|
|
3
|
+
// src/runner.ts
|
|
4
|
+
function safeParseArgs(args) {
|
|
5
|
+
try {
|
|
6
|
+
const parsed = JSON.parse(args);
|
|
7
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
8
|
+
} catch {
|
|
9
|
+
return {};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function createRuntime(config) {
|
|
13
|
+
const emitter = createEventEmitter();
|
|
14
|
+
for (const observer of config.observers ?? []) {
|
|
15
|
+
emitter.addObserver(observer);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
async run(task, options) {
|
|
19
|
+
const startTime = Date.now();
|
|
20
|
+
const maxSteps = options?.maxSteps ?? config.maxSteps ?? 10;
|
|
21
|
+
const signal = options?.signal;
|
|
22
|
+
let skillTools = [];
|
|
23
|
+
let systemPrompt = options?.systemPrompt ?? config.systemPrompt ?? "";
|
|
24
|
+
if (options?.skill) {
|
|
25
|
+
systemPrompt = options.skill.systemPrompt;
|
|
26
|
+
if (options.skill.onActivate) {
|
|
27
|
+
const activation = await options.skill.onActivate();
|
|
28
|
+
skillTools = activation.tools ?? [];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const toolMap = /* @__PURE__ */ new Map();
|
|
32
|
+
for (const tool of config.tools ?? []) toolMap.set(tool.name, tool);
|
|
33
|
+
for (const tool of options?.tools ?? []) toolMap.set(tool.name, tool);
|
|
34
|
+
for (const tool of skillTools) toolMap.set(tool.name, tool);
|
|
35
|
+
const tools = [...toolMap.values()];
|
|
36
|
+
const initialized = /* @__PURE__ */ new Set();
|
|
37
|
+
const initTool = async (tool) => {
|
|
38
|
+
if (tool.init && !initialized.has(tool.name)) {
|
|
39
|
+
await tool.init();
|
|
40
|
+
initialized.add(tool.name);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const disposeAll = async () => {
|
|
44
|
+
for (const name of initialized) {
|
|
45
|
+
const tool = toolMap.get(name);
|
|
46
|
+
try {
|
|
47
|
+
await tool?.dispose?.();
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const messages = [];
|
|
53
|
+
if (systemPrompt) {
|
|
54
|
+
messages.push(buildMessage({ role: "system", content: systemPrompt }));
|
|
55
|
+
}
|
|
56
|
+
messages.push(buildMessage({ role: "user", content: task }));
|
|
57
|
+
const allToolCalls = [];
|
|
58
|
+
let step = 0;
|
|
59
|
+
let finalContent = "";
|
|
60
|
+
try {
|
|
61
|
+
while (step < maxSteps) {
|
|
62
|
+
if (signal?.aborted) {
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
step++;
|
|
66
|
+
emitter.emit({ type: "agent:step", step, action: step === 1 ? "initial" : "tool-result-loop" });
|
|
67
|
+
const request = {
|
|
68
|
+
messages,
|
|
69
|
+
context: {
|
|
70
|
+
systemPrompt,
|
|
71
|
+
temperature: options?.skill?.temperature ?? config.temperature,
|
|
72
|
+
maxTokens: config.maxTokens,
|
|
73
|
+
tools
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const streamStart = Date.now();
|
|
77
|
+
const source = config.adapter.createSource(request);
|
|
78
|
+
emitter.emit({ type: "llm:start", messageCount: messages.length });
|
|
79
|
+
let accumulatedText = "";
|
|
80
|
+
const stepToolCalls = [];
|
|
81
|
+
let streamError = null;
|
|
82
|
+
await consumeStream(source, {
|
|
83
|
+
onText(accumulated) {
|
|
84
|
+
accumulatedText = accumulated;
|
|
85
|
+
},
|
|
86
|
+
async onToolCall(chunk) {
|
|
87
|
+
if (chunk.toolCall) {
|
|
88
|
+
stepToolCalls.push({ toolCall: chunk.toolCall });
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
onError(error) {
|
|
92
|
+
streamError = error;
|
|
93
|
+
},
|
|
94
|
+
onDone(accumulated) {
|
|
95
|
+
accumulatedText = accumulated;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
emitter.emit({
|
|
99
|
+
type: "llm:end",
|
|
100
|
+
content: accumulatedText,
|
|
101
|
+
durationMs: Date.now() - streamStart
|
|
102
|
+
});
|
|
103
|
+
if (streamError) {
|
|
104
|
+
emitter.emit({ type: "error", error: streamError });
|
|
105
|
+
throw streamError;
|
|
106
|
+
}
|
|
107
|
+
if (signal?.aborted) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
const assistantToolCalls = stepToolCalls.map(({ toolCall }) => ({
|
|
111
|
+
id: toolCall.id,
|
|
112
|
+
name: toolCall.name,
|
|
113
|
+
args: safeParseArgs(toolCall.args),
|
|
114
|
+
status: "pending"
|
|
115
|
+
}));
|
|
116
|
+
const assistantMessage = buildMessage({
|
|
117
|
+
role: "assistant",
|
|
118
|
+
content: accumulatedText,
|
|
119
|
+
status: "complete"
|
|
120
|
+
});
|
|
121
|
+
if (assistantToolCalls.length > 0) {
|
|
122
|
+
assistantMessage.toolCalls = assistantToolCalls;
|
|
123
|
+
}
|
|
124
|
+
messages.push(assistantMessage);
|
|
125
|
+
if (assistantToolCalls.length === 0) {
|
|
126
|
+
finalContent = accumulatedText;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
for (const toolCall of assistantToolCalls) {
|
|
130
|
+
if (signal?.aborted) break;
|
|
131
|
+
const tool = toolMap.get(toolCall.name);
|
|
132
|
+
allToolCalls.push(toolCall);
|
|
133
|
+
if (!tool?.execute) {
|
|
134
|
+
const errorMsg = `Tool "${toolCall.name}" not found or has no execute function`;
|
|
135
|
+
toolCall.status = "error";
|
|
136
|
+
toolCall.error = errorMsg;
|
|
137
|
+
messages.push(buildMessage({ role: "tool", content: errorMsg }));
|
|
138
|
+
emitter.emit({ type: "error", error: new Error(errorMsg) });
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
await initTool(tool);
|
|
142
|
+
emitter.emit({ type: "tool:start", name: toolCall.name, args: toolCall.args });
|
|
143
|
+
const toolStart = Date.now();
|
|
144
|
+
try {
|
|
145
|
+
const result = await executeToolCall(tool, toolCall.args, {
|
|
146
|
+
messages,
|
|
147
|
+
call: toolCall
|
|
148
|
+
});
|
|
149
|
+
toolCall.status = "complete";
|
|
150
|
+
toolCall.result = result;
|
|
151
|
+
messages.push(buildMessage({ role: "tool", content: result }));
|
|
152
|
+
emitter.emit({
|
|
153
|
+
type: "tool:end",
|
|
154
|
+
name: toolCall.name,
|
|
155
|
+
result,
|
|
156
|
+
durationMs: Date.now() - toolStart
|
|
157
|
+
});
|
|
158
|
+
} catch (error) {
|
|
159
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
160
|
+
toolCall.status = "error";
|
|
161
|
+
toolCall.error = errorMsg;
|
|
162
|
+
messages.push(buildMessage({ role: "tool", content: `Error: ${errorMsg}` }));
|
|
163
|
+
emitter.emit({
|
|
164
|
+
type: "tool:end",
|
|
165
|
+
name: toolCall.name,
|
|
166
|
+
result: `Error: ${errorMsg}`,
|
|
167
|
+
durationMs: Date.now() - toolStart
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
finalContent = accumulatedText;
|
|
172
|
+
}
|
|
173
|
+
if (config.memory) {
|
|
174
|
+
await config.memory.save(messages);
|
|
175
|
+
emitter.emit({ type: "memory:save", messageCount: messages.length });
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
content: finalContent,
|
|
179
|
+
messages,
|
|
180
|
+
steps: step,
|
|
181
|
+
toolCalls: allToolCalls,
|
|
182
|
+
durationMs: Date.now() - startTime
|
|
183
|
+
};
|
|
184
|
+
} finally {
|
|
185
|
+
await disposeAll();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export { createRuntime };
|
|
192
|
+
//# sourceMappingURL=index.js.map
|
|
193
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/runner.ts"],"names":[],"mappings":";;;AAgBA,SAAS,cAAc,IAAA,EAAuC;AAC5D,EAAA,IAAI;AACF,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,KAAA,CAAM,IAAI,CAAA;AAC9B,IAAA,OAAO,MAAA,IAAU,OAAO,MAAA,KAAW,QAAA,GAAY,SAAqC,EAAC;AAAA,EACvF,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,EAAC;AAAA,EACV;AACF;AAEO,SAAS,cAAc,MAAA,EAAuB;AACnD,EAAA,MAAM,UAAU,kBAAA,EAAmB;AAEnC,EAAA,KAAA,MAAW,QAAA,IAAY,MAAA,CAAO,SAAA,IAAa,EAAC,EAAG;AAC7C,IAAA,OAAA,CAAQ,YAAY,QAAQ,CAAA;AAAA,EAC9B;AAEA,EAAA,OAAO;AAAA,IACL,MAAM,GAAA,CAAI,IAAA,EAAc,OAAA,EAA0C;AAChE,MAAA,MAAM,SAAA,GAAY,KAAK,GAAA,EAAI;AAG3B,MAAA,MAAM,QAAA,GAAW,OAAA,EAAS,QAAA,IAAY,MAAA,CAAO,QAAA,IAAY,EAAA;AACzD,MAAA,MAAM,SAAS,OAAA,EAAS,MAAA;AAGxB,MAAA,IAAI,aAA+B,EAAC;AACpC,MAAA,IAAI,YAAA,GAAe,OAAA,EAAS,YAAA,IAAgB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AO,MAAA,EAAQ;AACjB,UAAA,MAAM,MAAA,CAAO,MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA;AACjC,UAAA,OAAA,CAAQ,KAAK,EAAE,IAAA,EAAM,eAAe,YAAA,EAAc,QAAA,CAAS,QAAQ,CAAA;AAAA,QACrE;AAEA,QAAA,OAAO;AAAA,UACL,OAAA,EAAS,YAAA;AAAA,UACT,QAAA;AAAA,UACA,KAAA,EAAO,IAAA;AAAA,UACP,SAAA,EAAW,YAAA;AAAA,UACX,UAAA,EAAY,IAAA,CAAK,GAAA,EAAI,GAAI;AAAA,SAC3B;AAAA,MACF,CAAA,SAAE;AACA,QAAA,MAAM,UAAA,EAAW;AAAA,MACnB;AAAA,IACF;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import {\n buildMessage,\n consumeStream,\n createEventEmitter,\n executeToolCall,\n generateId,\n} from '@agentskit/core'\nimport type {\n AdapterRequest,\n Message,\n StreamChunk,\n ToolCall,\n ToolDefinition,\n} from '@agentskit/core'\nimport type { RuntimeConfig, RunOptions, RunResult } from './types'\n\nfunction safeParseArgs(args: string): Record<string, unknown> {\n try {\n const parsed = JSON.parse(args)\n return parsed && typeof parsed === 'object' ? (parsed as Record<string, unknown>) : {}\n } catch {\n return {}\n }\n}\n\nexport function createRuntime(config: RuntimeConfig) {\n const emitter = createEventEmitter()\n\n for (const observer of config.observers ?? []) {\n emitter.addObserver(observer)\n }\n\n return {\n async run(task: string, options?: RunOptions): Promise<RunResult> {\n const startTime = Date.now()\n\n // Resolve effective config\n const maxSteps = options?.maxSteps ?? config.maxSteps ?? 10\n const signal = options?.signal\n\n // Activate skill if provided\n let skillTools: ToolDefinition[] = []\n let systemPrompt = options?.systemPrompt ?? config.systemPrompt ?? ''\n\n if (options?.skill) {\n systemPrompt = options.skill.systemPrompt\n if (options.skill.onActivate) {\n const activation = await options.skill.onActivate()\n skillTools = activation.tools ?? []\n }\n }\n\n // Merge tools: config < options < skill (last wins on name collision)\n const toolMap = new Map<string, ToolDefinition>()\n for (const tool of config.tools ?? []) toolMap.set(tool.name, tool)\n for (const tool of options?.tools ?? []) toolMap.set(tool.name, tool)\n for (const tool of skillTools) toolMap.set(tool.name, tool)\n const tools = [...toolMap.values()]\n\n // Lazy init tracking\n const initialized = new Set<string>()\n\n const initTool = async (tool: ToolDefinition) => {\n if (tool.init && !initialized.has(tool.name)) {\n await tool.init()\n initialized.add(tool.name)\n }\n }\n\n const disposeAll = async () => {\n for (const name of initialized) {\n const tool = toolMap.get(name)\n try {\n await tool?.dispose?.()\n } catch {\n // Dispose errors should not propagate\n }\n }\n }\n\n // Build initial messages\n const messages: Message[] = []\n\n if (systemPrompt) {\n messages.push(buildMessage({ role: 'system', content: systemPrompt }))\n }\n\n messages.push(buildMessage({ role: 'user', content: task }))\n\n const allToolCalls: ToolCall[] = []\n let step = 0\n let finalContent = ''\n\n try {\n while (step < maxSteps) {\n if (signal?.aborted) {\n break\n }\n\n step++\n emitter.emit({ type: 'agent:step', step, action: step === 1 ? 'initial' : 'tool-result-loop' })\n\n // Build adapter request\n const request: AdapterRequest = {\n messages,\n context: {\n systemPrompt,\n temperature: options?.skill?.temperature ?? config.temperature,\n maxTokens: config.maxTokens,\n tools,\n },\n }\n\n // Call adapter\n const streamStart = Date.now()\n const source = config.adapter.createSource(request)\n emitter.emit({ type: 'llm:start', messageCount: messages.length })\n\n let accumulatedText = ''\n const stepToolCalls: Array<{ toolCall: StreamChunk['toolCall'] }> = []\n let streamError: Error | null = null\n\n await consumeStream(source, {\n onText(accumulated) {\n accumulatedText = accumulated\n },\n async onToolCall(chunk) {\n if (chunk.toolCall) {\n stepToolCalls.push({ toolCall: chunk.toolCall })\n }\n },\n onError(error) {\n streamError = error\n },\n onDone(accumulated) {\n accumulatedText = accumulated\n },\n })\n\n emitter.emit({\n type: 'llm:end',\n content: accumulatedText,\n durationMs: Date.now() - streamStart,\n })\n\n if (streamError) {\n emitter.emit({ type: 'error', error: streamError })\n throw streamError\n }\n\n if (signal?.aborted) {\n break\n }\n\n // Build assistant message with tool calls\n const assistantToolCalls: ToolCall[] = stepToolCalls.map(({ toolCall }) => ({\n id: toolCall!.id,\n name: toolCall!.name,\n args: safeParseArgs(toolCall!.args),\n status: 'pending' as const,\n }))\n\n const assistantMessage = buildMessage({\n role: 'assistant',\n content: accumulatedText,\n status: 'complete',\n })\n if (assistantToolCalls.length > 0) {\n assistantMessage.toolCalls = assistantToolCalls\n }\n messages.push(assistantMessage)\n\n // No tool calls → agent is done\n if (assistantToolCalls.length === 0) {\n finalContent = accumulatedText\n break\n }\n\n // Execute each tool call and inject results\n for (const toolCall of assistantToolCalls) {\n if (signal?.aborted) break\n\n const tool = toolMap.get(toolCall.name)\n allToolCalls.push(toolCall)\n\n if (!tool?.execute) {\n // No executor — inject error as tool result\n const errorMsg = `Tool \"${toolCall.name}\" not found or has no execute function`\n toolCall.status = 'error'\n toolCall.error = errorMsg\n messages.push(buildMessage({ role: 'tool', content: errorMsg }))\n emitter.emit({ type: 'error', error: new Error(errorMsg) })\n continue\n }\n\n // Lazy init\n await initTool(tool)\n\n emitter.emit({ type: 'tool:start', name: toolCall.name, args: toolCall.args })\n const toolStart = Date.now()\n\n try {\n const result = await executeToolCall(tool, toolCall.args, {\n messages,\n call: toolCall,\n })\n toolCall.status = 'complete'\n toolCall.result = result\n messages.push(buildMessage({ role: 'tool', content: result }))\n emitter.emit({\n type: 'tool:end',\n name: toolCall.name,\n result,\n durationMs: Date.now() - toolStart,\n })\n } catch (error) {\n // Inject error as tool result — let LLM decide what to do\n const errorMsg = error instanceof Error ? error.message : String(error)\n toolCall.status = 'error'\n toolCall.error = errorMsg\n messages.push(buildMessage({ role: 'tool', content: `Error: ${errorMsg}` }))\n emitter.emit({\n type: 'tool:end',\n name: toolCall.name,\n result: `Error: ${errorMsg}`,\n durationMs: Date.now() - toolStart,\n })\n }\n }\n\n // After last iteration with tool calls, if we hit maxSteps next loop will break\n finalContent = accumulatedText\n }\n\n // Save to memory if configured\n if (config.memory) {\n await config.memory.save(messages)\n emitter.emit({ type: 'memory:save', messageCount: messages.length })\n }\n\n return {\n content: finalContent,\n messages,\n steps: step,\n toolCalls: allToolCalls,\n durationMs: Date.now() - startTime,\n }\n } finally {\n await disposeAll()\n }\n },\n }\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agentskit/runtime",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Standalone agent runtime with ReAct loop for AgentsKit.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@agentskit/core": "0.4.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^24.0.0",
|
|
24
|
+
"tsup": "^8.5.0",
|
|
25
|
+
"typescript": "^5.9.2",
|
|
26
|
+
"vitest": "^4.1.2"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup",
|
|
33
|
+
"test": "vitest run",
|
|
34
|
+
"lint": "tsc --noEmit",
|
|
35
|
+
"dev": "tsup --watch"
|
|
36
|
+
}
|
|
37
|
+
}
|