@ank1015/agents-runtime 0.1.11
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/CHANGELOG.md +21 -0
- package/README.md +92 -0
- package/dist/agent.d.ts +42 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +102 -0
- package/dist/agent.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/kernel/context.d.ts +28 -0
- package/dist/kernel/context.d.ts.map +1 -0
- package/dist/kernel/context.js +31 -0
- package/dist/kernel/context.js.map +1 -0
- package/dist/kernel/driver.d.ts +27 -0
- package/dist/kernel/driver.d.ts.map +1 -0
- package/dist/kernel/driver.js +479 -0
- package/dist/kernel/driver.js.map +1 -0
- package/dist/plugins/registry.d.ts +14 -0
- package/dist/plugins/registry.d.ts.map +1 -0
- package/dist/plugins/registry.js +53 -0
- package/dist/plugins/registry.js.map +1 -0
- package/dist/stream/consume.d.ts +10 -0
- package/dist/stream/consume.d.ts.map +1 -0
- package/dist/stream/consume.js +25 -0
- package/dist/stream/consume.js.map +1 -0
- package/dist/tools/build-result.d.ts +14 -0
- package/dist/tools/build-result.d.ts.map +1 -0
- package/dist/tools/build-result.js +32 -0
- package/dist/tools/build-result.js.map +1 -0
- package/dist/tools/define-tool.d.ts +16 -0
- package/dist/tools/define-tool.d.ts.map +1 -0
- package/dist/tools/define-tool.js +18 -0
- package/dist/tools/define-tool.js.map +1 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +5 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +3 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +28 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/validate.d.ts +15 -0
- package/dist/tools/validate.d.ts.map +1 -0
- package/dist/tools/validate.js +35 -0
- package/dist/tools/validate.js.map +1 -0
- package/dist/utils/abort.d.ts +5 -0
- package/dist/utils/abort.d.ts.map +1 -0
- package/dist/utils/abort.js +13 -0
- package/dist/utils/abort.js.map +1 -0
- package/dist/utils/content.d.ts +8 -0
- package/dist/utils/content.d.ts.map +1 -0
- package/dist/utils/content.js +19 -0
- package/dist/utils/content.js.map +1 -0
- package/dist/utils/errors.d.ts +16 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +43 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/ids.d.ts +6 -0
- package/dist/utils/ids.d.ts.map +1 -0
- package/dist/utils/ids.js +12 -0
- package/dist/utils/ids.js.map +1 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/time.d.ts +5 -0
- package/dist/utils/time.d.ts.map +1 -0
- package/dist/utils/time.js +5 -0
- package/dist/utils/time.js.map +1 -0
- package/dist/utils/usage.d.ts +9 -0
- package/dist/utils/usage.d.ts.map +1 -0
- package/dist/utils/usage.js +15 -0
- package/dist/utils/usage.js.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createId } from '../utils/ids.js';
|
|
2
|
+
import { now } from '../utils/time.js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts a {@link ToolExecutionResult} into a persisted `ToolResultMessage`
|
|
5
|
+
* (successful or failed variant). For failures, `errorDetails` is used or a
|
|
6
|
+
* generic error is synthesized.
|
|
7
|
+
*/
|
|
8
|
+
export function buildToolResultMessage(toolCall, result, isError, errorDetails, options = {}) {
|
|
9
|
+
const base = {
|
|
10
|
+
role: 'toolResult',
|
|
11
|
+
id: options.id ?? createId('tr'),
|
|
12
|
+
toolName: toolCall.name,
|
|
13
|
+
toolCallId: toolCall.toolCallId,
|
|
14
|
+
content: result.content,
|
|
15
|
+
timestamp: options.timestamp ?? now(),
|
|
16
|
+
...(result.details !== undefined ? { details: result.details } : {}),
|
|
17
|
+
};
|
|
18
|
+
if (isError) {
|
|
19
|
+
const failed = {
|
|
20
|
+
...base,
|
|
21
|
+
isError: true,
|
|
22
|
+
error: errorDetails ?? { message: 'Tool execution failed' },
|
|
23
|
+
};
|
|
24
|
+
return failed;
|
|
25
|
+
}
|
|
26
|
+
const success = {
|
|
27
|
+
...base,
|
|
28
|
+
isError: false,
|
|
29
|
+
};
|
|
30
|
+
return success;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=build-result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-result.js","sourceRoot":"","sources":["../../src/tools/build-result.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AASvC;;;;GAIG;AACH,MAAM,UAAU,sBAAsB,CACpC,QAA2B,EAC3B,MAA2B,EAC3B,OAAgB,EAChB,YAA8B,EAC9B,UAAkC,EAAE;IAEpC,MAAM,IAAI,GAAG;QACX,IAAI,EAAE,YAAqB;QAC3B,EAAE,EAAE,OAAO,CAAC,EAAE,IAAI,QAAQ,CAAC,IAAI,CAAC;QAChC,QAAQ,EAAE,QAAQ,CAAC,IAAI;QACvB,UAAU,EAAE,QAAQ,CAAC,UAAU;QAC/B,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,GAAG,EAAE;QACrC,GAAG,CAAC,MAAM,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACrE,CAAC;IAEF,IAAI,OAAO,EAAE,CAAC;QACZ,MAAM,MAAM,GAA4B;YACtC,GAAG,IAAI;YACP,OAAO,EAAE,IAAI;YACb,KAAK,EAAE,YAAY,IAAI,EAAE,OAAO,EAAE,uBAAuB,EAAE;SAC5D,CAAC;QACF,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,OAAO,GAAgC;QAC3C,GAAG,IAAI;QACP,OAAO,EAAE,KAAK;KACf,CAAC;IACF,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ExecutableTool, ToolExecutionInput, ToolExecutionResult } from '@ank1015/agents-contracts';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
export interface DefineToolOptions<TParams extends z.ZodType, TDetails> {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
/** Zod schema for the tool's arguments; drives both validation and `args` typing. */
|
|
7
|
+
parameters: TParams;
|
|
8
|
+
strict?: boolean | null;
|
|
9
|
+
execute: (input: ToolExecutionInput<z.infer<TParams>, TDetails>) => Promise<ToolExecutionResult<TDetails>>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates an {@link ExecutableTool} from a Zod schema, inferring the `execute`
|
|
13
|
+
* `args` type from `parameters` so handlers are fully typed.
|
|
14
|
+
*/
|
|
15
|
+
export declare function defineTool<TParams extends z.ZodType, TDetails = unknown>(options: DefineToolOptions<TParams, TDetails>): ExecutableTool<z.infer<TParams>, TDetails>;
|
|
16
|
+
//# sourceMappingURL=define-tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define-tool.d.ts","sourceRoot":"","sources":["../../src/tools/define-tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EAEd,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,MAAM,WAAW,iBAAiB,CAAC,OAAO,SAAS,CAAC,CAAC,OAAO,EAAE,QAAQ;IACpE,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,qFAAqF;IACrF,UAAU,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IACxB,OAAO,EAAE,CACP,KAAK,EAAE,kBAAkB,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,KAClD,OAAO,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC,CAAC;CAC7C;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,EACtE,OAAO,EAAE,iBAAiB,CAAC,OAAO,EAAE,QAAQ,CAAC,GAC5C,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,CAa5C"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an {@link ExecutableTool} from a Zod schema, inferring the `execute`
|
|
3
|
+
* `args` type from `parameters` so handlers are fully typed.
|
|
4
|
+
*/
|
|
5
|
+
export function defineTool(options) {
|
|
6
|
+
const definition = {
|
|
7
|
+
type: 'function',
|
|
8
|
+
name: options.name,
|
|
9
|
+
description: options.description,
|
|
10
|
+
parameters: options.parameters,
|
|
11
|
+
...(options.strict !== undefined ? { strict: options.strict } : {}),
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
definition,
|
|
15
|
+
execute: options.execute,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=define-tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define-tool.js","sourceRoot":"","sources":["../../src/tools/define-tool.ts"],"names":[],"mappings":"AAmBA;;;GAGG;AACH,MAAM,UAAU,UAAU,CACxB,OAA6C;IAE7C,MAAM,UAAU,GAA0B;QACxC,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACpE,CAAC;IAEF,OAAO;QACL,UAAU;QACV,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/tools/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAE9E,wBAAgB,kBAAkB,CAAC,KAAK,GAAE,SAAS,cAAc,EAAO,GAAG,YAAY,CAEtF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function createToolRegistry(tools = []) {
|
|
2
|
+
return new DefaultToolRegistry(tools);
|
|
3
|
+
}
|
|
4
|
+
class DefaultToolRegistry {
|
|
5
|
+
#tools = new Map();
|
|
6
|
+
constructor(tools) {
|
|
7
|
+
for (const tool of tools) {
|
|
8
|
+
this.register(tool);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
register(tool) {
|
|
12
|
+
const { name } = tool.definition;
|
|
13
|
+
if (this.#tools.has(name)) {
|
|
14
|
+
throw new Error(`Duplicate tool registered: "${name}".`);
|
|
15
|
+
}
|
|
16
|
+
this.#tools.set(name, tool);
|
|
17
|
+
}
|
|
18
|
+
get(name) {
|
|
19
|
+
return this.#tools.get(name);
|
|
20
|
+
}
|
|
21
|
+
has(name) {
|
|
22
|
+
return this.#tools.has(name);
|
|
23
|
+
}
|
|
24
|
+
list() {
|
|
25
|
+
return [...this.#tools.values()];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/tools/registry.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,kBAAkB,CAAC,QAAmC,EAAE;IACtE,OAAO,IAAI,mBAAmB,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,mBAAmB;IACd,MAAM,GAAG,IAAI,GAAG,EAA0B,CAAC;IAEpD,YAAY,KAAgC;QAC1C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IAED,QAAQ,CAAC,IAAoB;QAC3B,MAAM,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC;QACjC,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CAAC,+BAA+B,IAAI,IAAI,CAAC,CAAC;QAC3D,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IAED,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IAED,IAAI;QACF,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AssistantToolCall, ExecutableTool } from '@ank1015/agents-contracts';
|
|
2
|
+
/** Thrown when a tool call's arguments fail schema validation. */
|
|
3
|
+
export declare class ToolArgumentValidationError extends Error {
|
|
4
|
+
readonly toolName: string;
|
|
5
|
+
readonly issues: unknown;
|
|
6
|
+
constructor(toolName: string, issues: unknown, message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Validates `toolCall.arguments` against the tool's `definition.parameters`.
|
|
10
|
+
* Returns the parsed arguments, or throws {@link ToolArgumentValidationError}.
|
|
11
|
+
* Custom (grammar) tools and JSON-schema parameter sets carry no Zod validator,
|
|
12
|
+
* so their arguments pass through unchanged.
|
|
13
|
+
*/
|
|
14
|
+
export declare function validateToolArguments(tool: ExecutableTool, toolCall: AssistantToolCall): unknown;
|
|
15
|
+
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../src/tools/validate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAkB,MAAM,2BAA2B,CAAC;AAGnG,kEAAkE;AAClE,qBAAa,2BAA4B,SAAQ,KAAK;IACpD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;gBAEb,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM;CAM/D;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,cAAc,EAAE,QAAQ,EAAE,iBAAiB,GAAG,OAAO,CAgBhG"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** Thrown when a tool call's arguments fail schema validation. */
|
|
2
|
+
export class ToolArgumentValidationError extends Error {
|
|
3
|
+
toolName;
|
|
4
|
+
issues;
|
|
5
|
+
constructor(toolName, issues, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'ToolArgumentValidationError';
|
|
8
|
+
this.toolName = toolName;
|
|
9
|
+
this.issues = issues;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Validates `toolCall.arguments` against the tool's `definition.parameters`.
|
|
14
|
+
* Returns the parsed arguments, or throws {@link ToolArgumentValidationError}.
|
|
15
|
+
* Custom (grammar) tools and JSON-schema parameter sets carry no Zod validator,
|
|
16
|
+
* so their arguments pass through unchanged.
|
|
17
|
+
*/
|
|
18
|
+
export function validateToolArguments(tool, toolCall) {
|
|
19
|
+
const { definition } = tool;
|
|
20
|
+
if (definition.type !== 'function' || !isZodSchema(definition.parameters)) {
|
|
21
|
+
return toolCall.arguments;
|
|
22
|
+
}
|
|
23
|
+
const parsed = definition.parameters.safeParse(toolCall.arguments);
|
|
24
|
+
if (!parsed.success) {
|
|
25
|
+
throw new ToolArgumentValidationError(toolCall.name, parsed.error.issues, `Invalid arguments for tool "${toolCall.name}": ${parsed.error.message}`);
|
|
26
|
+
}
|
|
27
|
+
return parsed.data;
|
|
28
|
+
}
|
|
29
|
+
function isZodSchema(parameters) {
|
|
30
|
+
return (typeof parameters === 'object' &&
|
|
31
|
+
parameters !== null &&
|
|
32
|
+
'safeParse' in parameters &&
|
|
33
|
+
typeof parameters.safeParse === 'function');
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../../src/tools/validate.ts"],"names":[],"mappings":"AAGA,kEAAkE;AAClE,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IAC3C,QAAQ,CAAS;IACjB,MAAM,CAAU;IAEzB,YAAY,QAAgB,EAAE,MAAe,EAAE,OAAe;QAC5D,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;QAC1C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAoB,EAAE,QAA2B;IACrF,MAAM,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC;IAC5B,IAAI,UAAU,CAAC,IAAI,KAAK,UAAU,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QAC1E,OAAO,QAAQ,CAAC,SAAS,CAAC;IAC5B,CAAC;IAED,MAAM,MAAM,GAAG,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;IACnE,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,IAAI,2BAA2B,CACnC,QAAQ,CAAC,IAAI,EACb,MAAM,CAAC,KAAK,CAAC,MAAM,EACnB,+BAA+B,QAAQ,CAAC,IAAI,MAAM,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,CACzE,CAAC;IACJ,CAAC;IAED,OAAO,MAAM,CAAC,IAAI,CAAC;AACrB,CAAC;AAED,SAAS,WAAW,CAAC,UAA0B;IAC7C,OAAO,CACL,OAAO,UAAU,KAAK,QAAQ;QAC9B,UAAU,KAAK,IAAI;QACnB,WAAW,IAAI,UAAU;QACzB,OAAQ,UAAqC,CAAC,SAAS,KAAK,UAAU,CACvE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abort.d.ts","sourceRoot":"","sources":["../../src/utils/abort.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAQ1E"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detects abort-style errors raised by provider SDKs / `fetch`, or an already-aborted signal.
|
|
3
|
+
*/
|
|
4
|
+
export function isAbortError(error, signal) {
|
|
5
|
+
if (signal?.aborted) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
if (!(error instanceof Error)) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
return error.name === 'AbortError' || error.name === 'APIUserAbortError';
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=abort.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abort.js","sourceRoot":"","sources":["../../src/utils/abort.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc,EAAE,MAAoB;IAC/D,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,KAAK,YAAY,IAAI,KAAK,CAAC,IAAI,KAAK,mBAAmB,CAAC;AAC3E,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Content, TextContent } from '@ank1015/agents-contracts';
|
|
2
|
+
/** Builds a single text content part. */
|
|
3
|
+
export declare function text(content: string): TextContent;
|
|
4
|
+
/** Builds a content array with a single text part. */
|
|
5
|
+
export declare function textContent(content: string): Content;
|
|
6
|
+
/** Concatenates the text of all text parts, ignoring non-text (e.g. image) parts. */
|
|
7
|
+
export declare function contentToText(content: Content): string;
|
|
8
|
+
//# sourceMappingURL=content.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../../src/utils/content.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAEtE,yCAAyC;AACzC,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,WAAW,CAEjD;AAED,sDAAsD;AACtD,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED,qFAAqF;AACrF,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAQtD"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Builds a single text content part. */
|
|
2
|
+
export function text(content) {
|
|
3
|
+
return { type: 'text', content };
|
|
4
|
+
}
|
|
5
|
+
/** Builds a content array with a single text part. */
|
|
6
|
+
export function textContent(content) {
|
|
7
|
+
return [text(content)];
|
|
8
|
+
}
|
|
9
|
+
/** Concatenates the text of all text parts, ignoring non-text (e.g. image) parts. */
|
|
10
|
+
export function contentToText(content) {
|
|
11
|
+
let result = '';
|
|
12
|
+
for (const part of content) {
|
|
13
|
+
if (part.type === 'text') {
|
|
14
|
+
result += part.content;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=content.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content.js","sourceRoot":"","sources":["../../src/utils/content.ts"],"names":[],"mappings":"AAEA,yCAAyC;AACzC,MAAM,UAAU,IAAI,CAAC,OAAe;IAClC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;AACnC,CAAC;AAED,sDAAsD;AACtD,MAAM,UAAU,WAAW,CAAC,OAAe;IACzC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;AACzB,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,aAAa,CAAC,OAAgB;IAC5C,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,MAAM,IAAI,IAAI,OAAO,EAAE,CAAC;QAC3B,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YACzB,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC;QACzB,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AgentError, AgentErrorPhase, AssistantMessage, ToolResultError } from '@ank1015/agents-contracts';
|
|
2
|
+
/** Best-effort string extraction from an unknown thrown value. */
|
|
3
|
+
export declare function getErrorMessage(error: unknown): string;
|
|
4
|
+
/** Normalizes a thrown value into the `ToolResultError` shape stored on failed tool results. */
|
|
5
|
+
export declare function toToolResultError(cause: unknown): ToolResultError;
|
|
6
|
+
export interface CreateAgentErrorInput {
|
|
7
|
+
phase: AgentErrorPhase;
|
|
8
|
+
message?: string;
|
|
9
|
+
canRetry?: boolean;
|
|
10
|
+
attempts?: number;
|
|
11
|
+
cause?: unknown;
|
|
12
|
+
assistantMessage?: AssistantMessage;
|
|
13
|
+
}
|
|
14
|
+
/** Builds an `AgentError`, defaulting `message` from `cause`, `canRetry` false, `attempts` 1. */
|
|
15
|
+
export declare function createAgentError(input: CreateAgentErrorInput): AgentError;
|
|
16
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,eAAe,EACf,gBAAgB,EAChB,eAAe,EAChB,MAAM,2BAA2B,CAAC;AAEnC,kEAAkE;AAClE,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAYtD;AAED,gGAAgG;AAChG,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,eAAe,CASjE;AAED,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,eAAe,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;CACrC;AAED,iGAAiG;AACjG,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,UAAU,CAczE"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Best-effort string extraction from an unknown thrown value. */
|
|
2
|
+
export function getErrorMessage(error) {
|
|
3
|
+
if (error instanceof Error) {
|
|
4
|
+
return error.message;
|
|
5
|
+
}
|
|
6
|
+
if (typeof error === 'string') {
|
|
7
|
+
return error;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
return JSON.stringify(error) ?? String(error);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return String(error);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Normalizes a thrown value into the `ToolResultError` shape stored on failed tool results. */
|
|
17
|
+
export function toToolResultError(cause) {
|
|
18
|
+
if (cause instanceof Error) {
|
|
19
|
+
const error = { message: cause.message, name: cause.name };
|
|
20
|
+
if (cause.stack !== undefined) {
|
|
21
|
+
error.stack = cause.stack;
|
|
22
|
+
}
|
|
23
|
+
return error;
|
|
24
|
+
}
|
|
25
|
+
return { message: getErrorMessage(cause) };
|
|
26
|
+
}
|
|
27
|
+
/** Builds an `AgentError`, defaulting `message` from `cause`, `canRetry` false, `attempts` 1. */
|
|
28
|
+
export function createAgentError(input) {
|
|
29
|
+
const error = {
|
|
30
|
+
phase: input.phase,
|
|
31
|
+
message: input.message ?? getErrorMessage(input.cause),
|
|
32
|
+
canRetry: input.canRetry ?? false,
|
|
33
|
+
attempts: input.attempts ?? 1,
|
|
34
|
+
};
|
|
35
|
+
if (input.cause !== undefined) {
|
|
36
|
+
error.cause = input.cause;
|
|
37
|
+
}
|
|
38
|
+
if (input.assistantMessage !== undefined) {
|
|
39
|
+
error.assistantMessage = input.assistantMessage;
|
|
40
|
+
}
|
|
41
|
+
return error;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAOA,kEAAkE;AAClE,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC,OAAO,CAAC;IACvB,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;IAChD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;AACH,CAAC;AAED,gGAAgG;AAChG,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,MAAM,KAAK,GAAoB,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC;QAC5E,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YAC9B,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;QAC5B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;AAC7C,CAAC;AAWD,iGAAiG;AACjG,MAAM,UAAU,gBAAgB,CAAC,KAA4B;IAC3D,MAAM,KAAK,GAAe;QACxB,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,OAAO,EAAE,KAAK,CAAC,OAAO,IAAI,eAAe,CAAC,KAAK,CAAC,KAAK,CAAC;QACtD,QAAQ,EAAE,KAAK,CAAC,QAAQ,IAAI,KAAK;QACjC,QAAQ,EAAE,KAAK,CAAC,QAAQ,IAAI,CAAC;KAC9B,CAAC;IACF,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;QAC9B,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;IAC5B,CAAC;IACD,IAAI,KAAK,CAAC,gBAAgB,KAAK,SAAS,EAAE,CAAC;QACzC,KAAK,CAAC,gBAAgB,GAAG,KAAK,CAAC,gBAAgB,CAAC;IAClD,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../../src/utils/ids.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAGhD"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a unique id, optionally namespaced with a prefix
|
|
3
|
+
* (e.g. `createId('msg')` → `"msg_018f…"`).
|
|
4
|
+
*/
|
|
5
|
+
export function createId(prefix) {
|
|
6
|
+
const uuid = globalThis.crypto?.randomUUID?.() ?? fallbackId();
|
|
7
|
+
return prefix ? `${prefix}_${uuid}` : uuid;
|
|
8
|
+
}
|
|
9
|
+
function fallbackId() {
|
|
10
|
+
return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=ids.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ids.js","sourceRoot":"","sources":["../../src/utils/ids.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAe;IACtC,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,IAAI,UAAU,EAAE,CAAC;IAC/D,OAAO,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AAC7C,CAAC;AAED,SAAS,UAAU;IACjB,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;AAChF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../src/utils/time.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,MAAM,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC;AAEjC,0EAA0E;AAC1E,wBAAgB,GAAG,IAAI,MAAM,CAE5B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time.js","sourceRoot":"","sources":["../../src/utils/time.ts"],"names":[],"mappings":"AAGA,0EAA0E;AAC1E,MAAM,UAAU,GAAG;IACjB,OAAO,IAAI,CAAC,GAAG,EAAE,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Usage } from '@ank1015/agents-contracts';
|
|
2
|
+
/**
|
|
3
|
+
* Sum of all token categories (input + output + cacheRead + cacheWrite). We
|
|
4
|
+
* intentionally don't store an aggregate on `Usage`, so callers compute it here.
|
|
5
|
+
*/
|
|
6
|
+
export declare function tokenTotal(usage: Usage | undefined): number;
|
|
7
|
+
/** Total cost in USD for a usage record, or 0 when absent. */
|
|
8
|
+
export declare function costTotal(usage: Usage | undefined): number;
|
|
9
|
+
//# sourceMappingURL=usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.d.ts","sourceRoot":"","sources":["../../src/utils/usage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAEvD;;;GAGG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,GAAG,MAAM,CAO3D;AAED,8DAA8D;AAC9D,wBAAgB,SAAS,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,GAAG,MAAM,CAE1D"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sum of all token categories (input + output + cacheRead + cacheWrite). We
|
|
3
|
+
* intentionally don't store an aggregate on `Usage`, so callers compute it here.
|
|
4
|
+
*/
|
|
5
|
+
export function tokenTotal(usage) {
|
|
6
|
+
if (!usage) {
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
return ((usage.input ?? 0) + (usage.output ?? 0) + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0));
|
|
10
|
+
}
|
|
11
|
+
/** Total cost in USD for a usage record, or 0 when absent. */
|
|
12
|
+
export function costTotal(usage) {
|
|
13
|
+
return usage?.cost?.total ?? 0;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.js","sourceRoot":"","sources":["../../src/utils/usage.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,KAAwB;IACjD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO,CAAC,CAAC;IACX,CAAC;IACD,OAAO,CACL,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,UAAU,IAAI,CAAC,CAAC,CAC5F,CAAC;AACJ,CAAC;AAED,8DAA8D;AAC9D,MAAM,UAAU,SAAS,CAAC,KAAwB;IAChD,OAAO,KAAK,EAAE,IAAI,EAAE,KAAK,IAAI,CAAC,CAAC;AACjC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ank1015/agents-runtime",
|
|
3
|
+
"version": "0.1.11",
|
|
4
|
+
"description": "Generic agent loop runtime for @ank1015/agents.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agents",
|
|
7
|
+
"llm",
|
|
8
|
+
"runtime",
|
|
9
|
+
"agent-loop",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"license": "UNLICENSED",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/ank1015/agent.git",
|
|
20
|
+
"directory": "packages/runtime"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/ank1015/agent/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/ank1015/agent#readme",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./tools": {
|
|
32
|
+
"types": "./dist/tools/index.d.ts",
|
|
33
|
+
"import": "./dist/tools/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./utils": {
|
|
36
|
+
"types": "./dist/utils/index.d.ts",
|
|
37
|
+
"import": "./dist/utils/index.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"README.md",
|
|
46
|
+
"CHANGELOG.md"
|
|
47
|
+
],
|
|
48
|
+
"scripts": {
|
|
49
|
+
"prepack": "tsc -b",
|
|
50
|
+
"build": "tsc -b",
|
|
51
|
+
"check": "tsc -b --pretty false",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"clean": "rm -rf dist ../../.tsbuildinfo/runtime.tsbuildinfo"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@ank1015/agents-contracts": "^0.1.11",
|
|
57
|
+
"zod": "^4.4.3"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"vitest": "^4.1.9"
|
|
61
|
+
}
|
|
62
|
+
}
|