general-agent-runtime 0.0.1
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/README.md +311 -0
- package/dist/agent/agent.d.ts +14 -0
- package/dist/agent/agent.js +25 -0
- package/dist/agent/agent.js.map +1 -0
- package/dist/agent/builder.d.ts +28 -0
- package/dist/agent/builder.js +178 -0
- package/dist/agent/builder.js.map +1 -0
- package/dist/agent/create-agent.d.ts +2 -0
- package/dist/agent/create-agent.js +29 -0
- package/dist/agent/create-agent.js.map +1 -0
- package/dist/agent/index.d.ts +8 -0
- package/dist/agent/index.js +8 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/integrations/session-context-source.d.ts +12 -0
- package/dist/agent/integrations/session-context-source.js +84 -0
- package/dist/agent/integrations/session-context-source.js.map +1 -0
- package/dist/agent/integrations/session-run-recorder.d.ts +17 -0
- package/dist/agent/integrations/session-run-recorder.js +60 -0
- package/dist/agent/integrations/session-run-recorder.js.map +1 -0
- package/dist/agent/loop-factory.d.ts +11 -0
- package/dist/agent/loop-factory.js +16 -0
- package/dist/agent/loop-factory.js.map +1 -0
- package/dist/agent/run-manager.d.ts +38 -0
- package/dist/agent/run-manager.js +96 -0
- package/dist/agent/run-manager.js.map +1 -0
- package/dist/agent/types.d.ts +38 -0
- package/dist/agent/types.js +2 -0
- package/dist/agent/types.js.map +1 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +88 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/cli/renderer.d.ts +2 -0
- package/dist/cli/renderer.js +36 -0
- package/dist/cli/renderer.js.map +1 -0
- package/dist/compaction/budget.d.ts +14 -0
- package/dist/compaction/budget.js +28 -0
- package/dist/compaction/budget.js.map +1 -0
- package/dist/compaction/deterministic.d.ts +9 -0
- package/dist/compaction/deterministic.js +106 -0
- package/dist/compaction/deterministic.js.map +1 -0
- package/dist/compaction/index.d.ts +5 -0
- package/dist/compaction/index.js +5 -0
- package/dist/compaction/index.js.map +1 -0
- package/dist/compaction/manager.d.ts +22 -0
- package/dist/compaction/manager.js +111 -0
- package/dist/compaction/manager.js.map +1 -0
- package/dist/compaction/semantic.d.ts +13 -0
- package/dist/compaction/semantic.js +234 -0
- package/dist/compaction/semantic.js.map +1 -0
- package/dist/compaction/strategy.d.ts +1 -0
- package/dist/compaction/strategy.js +2 -0
- package/dist/compaction/strategy.js.map +1 -0
- package/dist/compaction/types.d.ts +40 -0
- package/dist/compaction/types.js +2 -0
- package/dist/compaction/types.js.map +1 -0
- package/dist/context/budget.d.ts +12 -0
- package/dist/context/budget.js +13 -0
- package/dist/context/budget.js.map +1 -0
- package/dist/context/controller.d.ts +17 -0
- package/dist/context/controller.js +78 -0
- package/dist/context/controller.js.map +1 -0
- package/dist/context/index.d.ts +14 -0
- package/dist/context/index.js +13 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/manager.d.ts +10 -0
- package/dist/context/manager.js +65 -0
- package/dist/context/manager.js.map +1 -0
- package/dist/context/pipeline.d.ts +16 -0
- package/dist/context/pipeline.js +45 -0
- package/dist/context/pipeline.js.map +1 -0
- package/dist/context/projection.d.ts +8 -0
- package/dist/context/projection.js +10 -0
- package/dist/context/projection.js.map +1 -0
- package/dist/context/source.d.ts +6 -0
- package/dist/context/source.js +2 -0
- package/dist/context/source.js.map +1 -0
- package/dist/context/sources/dynamic.d.ts +10 -0
- package/dist/context/sources/dynamic.js +13 -0
- package/dist/context/sources/dynamic.js.map +1 -0
- package/dist/context/sources/project.d.ts +19 -0
- package/dist/context/sources/project.js +75 -0
- package/dist/context/sources/project.js.map +1 -0
- package/dist/context/sources/system.d.ts +14 -0
- package/dist/context/sources/system.js +23 -0
- package/dist/context/sources/system.js.map +1 -0
- package/dist/context/sources/user.d.ts +14 -0
- package/dist/context/sources/user.js +23 -0
- package/dist/context/sources/user.js.map +1 -0
- package/dist/context/stages/deduplicate.d.ts +7 -0
- package/dist/context/stages/deduplicate.js +19 -0
- package/dist/context/stages/deduplicate.js.map +1 -0
- package/dist/context/stages/expiration.d.ts +5 -0
- package/dist/context/stages/expiration.js +14 -0
- package/dist/context/stages/expiration.js.map +1 -0
- package/dist/context/stages/visibility.d.ts +5 -0
- package/dist/context/stages/visibility.js +7 -0
- package/dist/context/stages/visibility.js.map +1 -0
- package/dist/context/types.d.ts +72 -0
- package/dist/context/types.js +2 -0
- package/dist/context/types.js.map +1 -0
- package/dist/errors/codes.d.ts +1 -0
- package/dist/errors/codes.js +2 -0
- package/dist/errors/codes.js.map +1 -0
- package/dist/errors/index.d.ts +4 -0
- package/dist/errors/index.js +4 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/errors/normalize.d.ts +2 -0
- package/dist/errors/normalize.js +32 -0
- package/dist/errors/normalize.js.map +1 -0
- package/dist/errors/runtime-error.d.ts +18 -0
- package/dist/errors/runtime-error.js +21 -0
- package/dist/errors/runtime-error.js.map +1 -0
- package/dist/errors/serialize.d.ts +2 -0
- package/dist/errors/serialize.js +12 -0
- package/dist/errors/serialize.js.map +1 -0
- package/dist/events/bus.d.ts +17 -0
- package/dist/events/bus.js +52 -0
- package/dist/events/bus.js.map +1 -0
- package/dist/events/index.d.ts +6 -0
- package/dist/events/index.js +5 -0
- package/dist/events/index.js.map +1 -0
- package/dist/events/publisher.d.ts +12 -0
- package/dist/events/publisher.js +22 -0
- package/dist/events/publisher.js.map +1 -0
- package/dist/events/sequence.d.ts +6 -0
- package/dist/events/sequence.js +14 -0
- package/dist/events/sequence.js.map +1 -0
- package/dist/events/sink.d.ts +5 -0
- package/dist/events/sink.js +2 -0
- package/dist/events/sink.js.map +1 -0
- package/dist/events/stream.d.ts +12 -0
- package/dist/events/stream.js +46 -0
- package/dist/events/stream.js.map +1 -0
- package/dist/events/types.d.ts +42 -0
- package/dist/events/types.js +2 -0
- package/dist/events/types.js.map +1 -0
- package/dist/hooks/bus.d.ts +14 -0
- package/dist/hooks/bus.js +104 -0
- package/dist/hooks/bus.js.map +1 -0
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/policy.d.ts +3 -0
- package/dist/hooks/policy.js +33 -0
- package/dist/hooks/policy.js.map +1 -0
- package/dist/hooks/registry.d.ts +13 -0
- package/dist/hooks/registry.js +31 -0
- package/dist/hooks/registry.js.map +1 -0
- package/dist/hooks/timeout.d.ts +2 -0
- package/dist/hooks/timeout.js +63 -0
- package/dist/hooks/timeout.js.map +1 -0
- package/dist/hooks/types.d.ts +40 -0
- package/dist/hooks/types.js +2 -0
- package/dist/hooks/types.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/loop/index.d.ts +7 -0
- package/dist/loop/index.js +6 -0
- package/dist/loop/index.js.map +1 -0
- package/dist/loop/loop.d.ts +22 -0
- package/dist/loop/loop.js +623 -0
- package/dist/loop/loop.js.map +1 -0
- package/dist/loop/policy.d.ts +21 -0
- package/dist/loop/policy.js +54 -0
- package/dist/loop/policy.js.map +1 -0
- package/dist/loop/recorder.d.ts +9 -0
- package/dist/loop/recorder.js +2 -0
- package/dist/loop/recorder.js.map +1 -0
- package/dist/loop/state.d.ts +18 -0
- package/dist/loop/state.js +81 -0
- package/dist/loop/state.js.map +1 -0
- package/dist/loop/stream-assembler.d.ts +12 -0
- package/dist/loop/stream-assembler.js +76 -0
- package/dist/loop/stream-assembler.js.map +1 -0
- package/dist/loop/transition.d.ts +2 -0
- package/dist/loop/transition.js +27 -0
- package/dist/loop/transition.js.map +1 -0
- package/dist/loop/types.d.ts +53 -0
- package/dist/loop/types.js +2 -0
- package/dist/loop/types.js.map +1 -0
- package/dist/model/index.d.ts +6 -0
- package/dist/model/index.js +5 -0
- package/dist/model/index.js.map +1 -0
- package/dist/model/provider.d.ts +6 -0
- package/dist/model/provider.js +2 -0
- package/dist/model/provider.js.map +1 -0
- package/dist/model/providers/openai-compatible/adapter.d.ts +3 -0
- package/dist/model/providers/openai-compatible/adapter.js +92 -0
- package/dist/model/providers/openai-compatible/adapter.js.map +1 -0
- package/dist/model/providers/openai-compatible/errors.d.ts +3 -0
- package/dist/model/providers/openai-compatible/errors.js +68 -0
- package/dist/model/providers/openai-compatible/errors.js.map +1 -0
- package/dist/model/providers/openai-compatible/provider.d.ts +18 -0
- package/dist/model/providers/openai-compatible/provider.js +182 -0
- package/dist/model/providers/openai-compatible/provider.js.map +1 -0
- package/dist/model/providers/openai-compatible/stream-parser.d.ts +1 -0
- package/dist/model/providers/openai-compatible/stream-parser.js +45 -0
- package/dist/model/providers/openai-compatible/stream-parser.js.map +1 -0
- package/dist/model/registry.d.ts +9 -0
- package/dist/model/registry.js +35 -0
- package/dist/model/registry.js.map +1 -0
- package/dist/model/types.d.ts +49 -0
- package/dist/model/types.js +2 -0
- package/dist/model/types.js.map +1 -0
- package/dist/session/factory.d.ts +13 -0
- package/dist/session/factory.js +14 -0
- package/dist/session/factory.js.map +1 -0
- package/dist/session/index.d.ts +8 -0
- package/dist/session/index.js +7 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/jsonl-store.d.ts +20 -0
- package/dist/session/jsonl-store.js +264 -0
- package/dist/session/jsonl-store.js.map +1 -0
- package/dist/session/projector.d.ts +4 -0
- package/dist/session/projector.js +104 -0
- package/dist/session/projector.js.map +1 -0
- package/dist/session/serializer.d.ts +5 -0
- package/dist/session/serializer.js +126 -0
- package/dist/session/serializer.js.map +1 -0
- package/dist/session/session.d.ts +32 -0
- package/dist/session/session.js +129 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/store.d.ts +9 -0
- package/dist/session/store.js +2 -0
- package/dist/session/store.js.map +1 -0
- package/dist/session/types.d.ts +29 -0
- package/dist/session/types.js +2 -0
- package/dist/session/types.js.map +1 -0
- package/dist/settings/defaults.d.ts +7 -0
- package/dist/settings/defaults.js +40 -0
- package/dist/settings/defaults.js.map +1 -0
- package/dist/settings/env-source.d.ts +8 -0
- package/dist/settings/env-source.js +26 -0
- package/dist/settings/env-source.js.map +1 -0
- package/dist/settings/file-source.d.ts +9 -0
- package/dist/settings/file-source.js +32 -0
- package/dist/settings/file-source.js.map +1 -0
- package/dist/settings/index.d.ts +8 -0
- package/dist/settings/index.js +7 -0
- package/dist/settings/index.js.map +1 -0
- package/dist/settings/resolver.d.ts +5 -0
- package/dist/settings/resolver.js +29 -0
- package/dist/settings/resolver.js.map +1 -0
- package/dist/settings/runtime-source.d.ts +8 -0
- package/dist/settings/runtime-source.js +11 -0
- package/dist/settings/runtime-source.js.map +1 -0
- package/dist/settings/schema.d.ts +37 -0
- package/dist/settings/schema.js +47 -0
- package/dist/settings/schema.js.map +1 -0
- package/dist/settings/source.d.ts +5 -0
- package/dist/settings/source.js +2 -0
- package/dist/settings/source.js.map +1 -0
- package/dist/settings/types.d.ts +46 -0
- package/dist/settings/types.js +2 -0
- package/dist/settings/types.js.map +1 -0
- package/dist/tools/builtin/list-directory.d.ts +17 -0
- package/dist/tools/builtin/list-directory.js +31 -0
- package/dist/tools/builtin/list-directory.js.map +1 -0
- package/dist/tools/builtin/read-file.d.ts +14 -0
- package/dist/tools/builtin/read-file.js +23 -0
- package/dist/tools/builtin/read-file.js.map +1 -0
- package/dist/tools/builtin/run-command.d.ts +19 -0
- package/dist/tools/builtin/run-command.js +46 -0
- package/dist/tools/builtin/run-command.js.map +1 -0
- package/dist/tools/builtin/write-file.d.ts +20 -0
- package/dist/tools/builtin/write-file.js +29 -0
- package/dist/tools/builtin/write-file.js.map +1 -0
- package/dist/tools/executor.d.ts +29 -0
- package/dist/tools/executor.js +212 -0
- package/dist/tools/executor.js.map +1 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +9 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +19 -0
- package/dist/tools/registry.js +42 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/result-processor.d.ts +4 -0
- package/dist/tools/result-processor.js +122 -0
- package/dist/tools/result-processor.js.map +1 -0
- package/dist/tools/tool.d.ts +8 -0
- package/dist/tools/tool.js +2 -0
- package/dist/tools/tool.js.map +1 -0
- package/dist/tools/types.d.ts +44 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/tools/validator.d.ts +5 -0
- package/dist/tools/validator.js +112 -0
- package/dist/tools/validator.js.map +1 -0
- package/dist/types/compaction.d.ts +22 -0
- package/dist/types/compaction.js +2 -0
- package/dist/types/compaction.js.map +1 -0
- package/dist/types/error.d.ts +10 -0
- package/dist/types/error.js +2 -0
- package/dist/types/error.js.map +1 -0
- package/dist/types/ids.d.ts +12 -0
- package/dist/types/ids.js +2 -0
- package/dist/types/ids.js.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/json.d.ts +7 -0
- package/dist/types/json.js +2 -0
- package/dist/types/json.js.map +1 -0
- package/dist/types/message.d.ts +39 -0
- package/dist/types/message.js +2 -0
- package/dist/types/message.js.map +1 -0
- package/dist/types/metadata.d.ts +2 -0
- package/dist/types/metadata.js +2 -0
- package/dist/types/metadata.js.map +1 -0
- package/dist/types/runtime.d.ts +20 -0
- package/dist/types/runtime.js +2 -0
- package/dist/types/runtime.js.map +1 -0
- package/dist/types/tool.d.ts +12 -0
- package/dist/types/tool.js +2 -0
- package/dist/types/tool.js.map +1 -0
- package/dist/types/usage.d.ts +17 -0
- package/dist/types/usage.js +2 -0
- package/dist/types/usage.js.map +1 -0
- package/dist/utils/deep.d.ts +2 -0
- package/dist/utils/deep.js +34 -0
- package/dist/utils/deep.js.map +1 -0
- package/dist/utils/id.d.ts +10 -0
- package/dist/utils/id.js +14 -0
- package/dist/utils/id.js.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/message.d.ts +11 -0
- package/dist/utils/message.js +31 -0
- package/dist/utils/message.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { JsonObject } from "../../types/index.js";
|
|
2
|
+
import type { Tool } from "../tool.js";
|
|
3
|
+
import type { ToolContext } from "../types.js";
|
|
4
|
+
type ReadFileInput = JsonObject & {
|
|
5
|
+
path: string;
|
|
6
|
+
encoding?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare class ReadFileTool implements Tool<ReadFileInput, string> {
|
|
9
|
+
readonly name = "read_file";
|
|
10
|
+
readonly description = "Read a UTF-8 text file from the filesystem.";
|
|
11
|
+
readonly inputSchema: JsonObject;
|
|
12
|
+
execute(input: ReadFileInput, context: ToolContext, signal?: AbortSignal): Promise<string>;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export class ReadFileTool {
|
|
4
|
+
name = "read_file";
|
|
5
|
+
description = "Read a UTF-8 text file from the filesystem.";
|
|
6
|
+
inputSchema = {
|
|
7
|
+
type: "object",
|
|
8
|
+
properties: {
|
|
9
|
+
path: { type: "string", minLength: 1 },
|
|
10
|
+
encoding: { type: "string", enum: ["utf8", "utf-8"] },
|
|
11
|
+
},
|
|
12
|
+
required: ["path"],
|
|
13
|
+
additionalProperties: false,
|
|
14
|
+
};
|
|
15
|
+
async execute(input, context, signal) {
|
|
16
|
+
const target = path.resolve(context.cwd ?? process.cwd(), input.path);
|
|
17
|
+
return readFile(target, {
|
|
18
|
+
encoding: "utf8",
|
|
19
|
+
...(signal === undefined ? {} : { signal }),
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=read-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-file.js","sourceRoot":"","sources":["../../../src/tools/builtin/read-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAU7B,MAAM,OAAO,YAAY;IACd,IAAI,GAAG,WAAW,CAAC;IACnB,WAAW,GAAG,6CAA6C,CAAC;IAC5D,WAAW,GAAe;QACjC,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE;YACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,EAAE;YACtC,QAAQ,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE;SACtD;QACD,QAAQ,EAAE,CAAC,MAAM,CAAC;QAClB,oBAAoB,EAAE,KAAK;KAC5B,CAAC;IAEF,KAAK,CAAC,OAAO,CACX,KAAoB,EACpB,OAAoB,EACpB,MAAoB;QAEpB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,EAAE,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;QACtE,OAAO,QAAQ,CAAC,MAAM,EAAE;YACtB,QAAQ,EAAE,MAAM;YAChB,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;SAC5C,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { JsonObject } from "../../types/index.js";
|
|
2
|
+
import type { Tool } from "../tool.js";
|
|
3
|
+
import type { ToolContext } from "../types.js";
|
|
4
|
+
type RunCommandInput = JsonObject & {
|
|
5
|
+
command: string;
|
|
6
|
+
cwd?: string;
|
|
7
|
+
};
|
|
8
|
+
interface RunCommandResult extends JsonObject {
|
|
9
|
+
stdout: string;
|
|
10
|
+
stderr: string;
|
|
11
|
+
exitCode: number;
|
|
12
|
+
}
|
|
13
|
+
export declare class RunCommandTool implements Tool<RunCommandInput, RunCommandResult> {
|
|
14
|
+
readonly name = "run_command";
|
|
15
|
+
readonly description = "Run a shell command and return stdout, stderr, and exit code.";
|
|
16
|
+
readonly inputSchema: JsonObject;
|
|
17
|
+
execute(input: RunCommandInput, context: ToolContext, signal?: AbortSignal): Promise<RunCommandResult>;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export class RunCommandTool {
|
|
4
|
+
name = "run_command";
|
|
5
|
+
description = "Run a shell command and return stdout, stderr, and exit code.";
|
|
6
|
+
inputSchema = {
|
|
7
|
+
type: "object",
|
|
8
|
+
properties: {
|
|
9
|
+
command: { type: "string", minLength: 1 },
|
|
10
|
+
cwd: { type: "string", minLength: 1 },
|
|
11
|
+
},
|
|
12
|
+
required: ["command"],
|
|
13
|
+
additionalProperties: false,
|
|
14
|
+
};
|
|
15
|
+
async execute(input, context, signal) {
|
|
16
|
+
signal?.throwIfAborted();
|
|
17
|
+
const cwd = path.resolve(context.cwd ?? process.cwd(), input.cwd ?? ".");
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
let stdout = "";
|
|
20
|
+
let stderr = "";
|
|
21
|
+
const child = spawn(input.command, {
|
|
22
|
+
cwd,
|
|
23
|
+
shell: true,
|
|
24
|
+
windowsHide: true,
|
|
25
|
+
...(signal === undefined ? {} : { signal }),
|
|
26
|
+
});
|
|
27
|
+
child.stdout?.setEncoding("utf8");
|
|
28
|
+
child.stderr?.setEncoding("utf8");
|
|
29
|
+
child.stdout?.on("data", (chunk) => {
|
|
30
|
+
stdout += chunk;
|
|
31
|
+
});
|
|
32
|
+
child.stderr?.on("data", (chunk) => {
|
|
33
|
+
stderr += chunk;
|
|
34
|
+
});
|
|
35
|
+
child.once("error", reject);
|
|
36
|
+
child.once("close", (code) => {
|
|
37
|
+
resolve({
|
|
38
|
+
stdout,
|
|
39
|
+
stderr,
|
|
40
|
+
exitCode: code ?? -1,
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=run-command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-command.js","sourceRoot":"","sources":["../../../src/tools/builtin/run-command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,IAAI,MAAM,WAAW,CAAC;AAgB7B,MAAM,OAAO,cAAc;IAChB,IAAI,GAAG,aAAa,CAAC;IACrB,WAAW,GAAG,+DAA+D,CAAC;IAC9E,WAAW,GAAe;QACjC,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE;YACV,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,EAAE;YACzC,GAAG,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,EAAE;SACtC;QACD,QAAQ,EAAE,CAAC,SAAS,CAAC;QACrB,oBAAoB,EAAE,KAAK;KAC5B,CAAC;IAEF,KAAK,CAAC,OAAO,CACX,KAAsB,EACtB,OAAoB,EACpB,MAAoB;QAEpB,MAAM,EAAE,cAAc,EAAE,CAAC;QAEzB,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CACtB,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,EAAE,EAC5B,KAAK,CAAC,GAAG,IAAI,GAAG,CACjB,CAAC;QAEF,OAAO,IAAI,OAAO,CAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACvD,IAAI,MAAM,GAAG,EAAE,CAAC;YAChB,IAAI,MAAM,GAAG,EAAE,CAAC;YAEhB,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE;gBACjC,GAAG;gBACH,KAAK,EAAE,IAAI;gBACX,WAAW,EAAE,IAAI;gBACjB,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;aAC5C,CAAC,CAAC;YAEH,KAAK,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;YAClC,KAAK,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;YAClC,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,KAAa,EAAE,EAAE;gBACzC,MAAM,IAAI,KAAK,CAAC;YAClB,CAAC,CAAC,CAAC;YACH,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,KAAa,EAAE,EAAE;gBACzC,MAAM,IAAI,KAAK,CAAC;YAClB,CAAC,CAAC,CAAC;YAEH,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YAC5B,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE;gBAC3B,OAAO,CAAC;oBACN,MAAM;oBACN,MAAM;oBACN,QAAQ,EAAE,IAAI,IAAI,CAAC,CAAC;iBACrB,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { JsonObject } from "../../types/index.js";
|
|
2
|
+
import type { Tool } from "../tool.js";
|
|
3
|
+
import type { ToolContext } from "../types.js";
|
|
4
|
+
interface WriteFileInput extends JsonObject {
|
|
5
|
+
path: string;
|
|
6
|
+
content: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class WriteFileTool implements Tool<WriteFileInput, {
|
|
9
|
+
path: string;
|
|
10
|
+
bytes: number;
|
|
11
|
+
}> {
|
|
12
|
+
readonly name = "write_file";
|
|
13
|
+
readonly description = "Create or overwrite a UTF-8 text file.";
|
|
14
|
+
readonly inputSchema: JsonObject;
|
|
15
|
+
execute(input: WriteFileInput, context: ToolContext, signal?: AbortSignal): Promise<{
|
|
16
|
+
path: string;
|
|
17
|
+
bytes: number;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export class WriteFileTool {
|
|
4
|
+
name = "write_file";
|
|
5
|
+
description = "Create or overwrite a UTF-8 text file.";
|
|
6
|
+
inputSchema = {
|
|
7
|
+
type: "object",
|
|
8
|
+
properties: {
|
|
9
|
+
path: { type: "string", minLength: 1 },
|
|
10
|
+
content: { type: "string" },
|
|
11
|
+
},
|
|
12
|
+
required: ["path", "content"],
|
|
13
|
+
additionalProperties: false,
|
|
14
|
+
};
|
|
15
|
+
async execute(input, context, signal) {
|
|
16
|
+
signal?.throwIfAborted();
|
|
17
|
+
const target = path.resolve(context.cwd ?? process.cwd(), input.path);
|
|
18
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
19
|
+
await writeFile(target, input.content, {
|
|
20
|
+
encoding: "utf8",
|
|
21
|
+
...(signal === undefined ? {} : { signal }),
|
|
22
|
+
});
|
|
23
|
+
return {
|
|
24
|
+
path: target,
|
|
25
|
+
bytes: Buffer.byteLength(input.content, "utf8"),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=write-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-file.js","sourceRoot":"","sources":["../../../src/tools/builtin/write-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,IAAI,MAAM,WAAW,CAAC;AAU7B,MAAM,OAAO,aAAa;IAGf,IAAI,GAAG,YAAY,CAAC;IACpB,WAAW,GAAG,wCAAwC,CAAC;IACvD,WAAW,GAAe;QACjC,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE;YACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,EAAE;YACtC,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;SAC5B;QACD,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;QAC7B,oBAAoB,EAAE,KAAK;KAC5B,CAAC;IAEF,KAAK,CAAC,OAAO,CACX,KAAqB,EACrB,OAAoB,EACpB,MAAoB;QAEpB,MAAM,EAAE,cAAc,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,EAAE,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;QACtE,MAAM,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACvD,MAAM,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,EAAE;YACrC,QAAQ,EAAE,MAAM;YAChB,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;SAC5C,CAAC,CAAC;QACH,OAAO;YACL,IAAI,EAAE,MAAM;YACZ,KAAK,EAAE,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC;SAChD,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { HookBus } from "../hooks/bus.js";
|
|
2
|
+
import type { ToolCall } from "../types/index.js";
|
|
3
|
+
import type { ToolRegistry } from "./registry.js";
|
|
4
|
+
import type { ToolContext, ToolExecutionResult, ToolInputValidator, ToolResultProcessor } from "./types.js";
|
|
5
|
+
export interface ToolExecutor {
|
|
6
|
+
execute(call: ToolCall, context: ToolContext, signal?: AbortSignal): Promise<ToolExecutionResult>;
|
|
7
|
+
executeMany(calls: readonly ToolCall[], context: ToolContext, signal?: AbortSignal): Promise<readonly ToolExecutionResult[]>;
|
|
8
|
+
}
|
|
9
|
+
export interface DefaultToolExecutorOptions {
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
maxParallel?: number;
|
|
12
|
+
maxResultChars?: number;
|
|
13
|
+
maxResultLines?: number;
|
|
14
|
+
maxRetries?: number;
|
|
15
|
+
}
|
|
16
|
+
export declare class DefaultToolExecutor implements ToolExecutor {
|
|
17
|
+
private readonly registry;
|
|
18
|
+
private readonly validator;
|
|
19
|
+
private readonly processor;
|
|
20
|
+
private readonly hookBus;
|
|
21
|
+
private readonly options;
|
|
22
|
+
constructor(registry: ToolRegistry, validator: ToolInputValidator, processor: ToolResultProcessor, hookBus: HookBus, options?: DefaultToolExecutorOptions);
|
|
23
|
+
execute(call: ToolCall, context: ToolContext, signal?: AbortSignal): Promise<ToolExecutionResult>;
|
|
24
|
+
executeMany(calls: readonly ToolCall[], context: ToolContext, signal?: AbortSignal): Promise<readonly ToolExecutionResult[]>;
|
|
25
|
+
private hookContext;
|
|
26
|
+
private retryExhausted;
|
|
27
|
+
private runWithTimeout;
|
|
28
|
+
private processError;
|
|
29
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { RuntimeError } from "../errors/runtime-error.js";
|
|
2
|
+
import { normalizeUnknownError } from "../errors/normalize.js";
|
|
3
|
+
const DEFAULT_OPTIONS = {
|
|
4
|
+
timeoutMs: 30_000,
|
|
5
|
+
maxParallel: 4,
|
|
6
|
+
maxResultChars: 8_000,
|
|
7
|
+
maxResultLines: 200,
|
|
8
|
+
maxRetries: 1,
|
|
9
|
+
};
|
|
10
|
+
export class DefaultToolExecutor {
|
|
11
|
+
registry;
|
|
12
|
+
validator;
|
|
13
|
+
processor;
|
|
14
|
+
hookBus;
|
|
15
|
+
options;
|
|
16
|
+
constructor(registry, validator, processor, hookBus, options = {}) {
|
|
17
|
+
this.registry = registry;
|
|
18
|
+
this.validator = validator;
|
|
19
|
+
this.processor = processor;
|
|
20
|
+
this.hookBus = hookBus;
|
|
21
|
+
this.options = { ...DEFAULT_OPTIONS, ...options };
|
|
22
|
+
}
|
|
23
|
+
async execute(call, context, signal) {
|
|
24
|
+
signal?.throwIfAborted();
|
|
25
|
+
const startedAt = Date.now();
|
|
26
|
+
try {
|
|
27
|
+
let currentCall = structuredClone(call);
|
|
28
|
+
for (let attempt = 0; attempt <= this.options.maxRetries; attempt += 1) {
|
|
29
|
+
signal?.throwIfAborted();
|
|
30
|
+
const initialTool = this.registry.require(currentCall.toolName);
|
|
31
|
+
this.validator.validate(initialTool.inputSchema, currentCall.input);
|
|
32
|
+
const before = await this.hookBus.run("before_tool", currentCall, this.hookContext(context, signal));
|
|
33
|
+
if (before.blocked) {
|
|
34
|
+
return this.processError(before.value, new RuntimeError({
|
|
35
|
+
code: "TOOL_PERMISSION",
|
|
36
|
+
category: "tool",
|
|
37
|
+
message: before.reason ?? `Tool blocked: ${currentCall.toolName}`,
|
|
38
|
+
}), startedAt, signal);
|
|
39
|
+
}
|
|
40
|
+
if (before.retryRequested) {
|
|
41
|
+
if (attempt < this.options.maxRetries) {
|
|
42
|
+
currentCall = before.value;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
return this.retryExhausted(before.value, before.reason, startedAt, signal);
|
|
46
|
+
}
|
|
47
|
+
currentCall = before.value;
|
|
48
|
+
const tool = this.registry.require(currentCall.toolName);
|
|
49
|
+
this.validator.validate(tool.inputSchema, currentCall.input);
|
|
50
|
+
const value = await this.runWithTimeout((toolSignal) => tool.execute(currentCall.input, context, toolSignal), currentCall.toolName, signal);
|
|
51
|
+
const processed = await this.processor.process({
|
|
52
|
+
call: currentCall,
|
|
53
|
+
raw: {
|
|
54
|
+
value,
|
|
55
|
+
durationMs: Date.now() - startedAt,
|
|
56
|
+
},
|
|
57
|
+
limits: {
|
|
58
|
+
maxChars: this.options.maxResultChars,
|
|
59
|
+
maxLines: this.options.maxResultLines,
|
|
60
|
+
},
|
|
61
|
+
}, signal);
|
|
62
|
+
const after = await this.hookBus.run("after_tool", processed, this.hookContext(context, signal));
|
|
63
|
+
if (after.blocked) {
|
|
64
|
+
return this.processError(processed.call, new RuntimeError({
|
|
65
|
+
code: "TOOL_EXECUTION_FAILED",
|
|
66
|
+
category: "tool",
|
|
67
|
+
message: after.reason ??
|
|
68
|
+
`Tool result blocked: ${processed.call.toolName}`,
|
|
69
|
+
}), startedAt, signal);
|
|
70
|
+
}
|
|
71
|
+
if (after.retryRequested) {
|
|
72
|
+
if (attempt < this.options.maxRetries) {
|
|
73
|
+
currentCall = processed.call;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
return this.retryExhausted(processed.call, after.reason, startedAt, signal);
|
|
77
|
+
}
|
|
78
|
+
return after.value;
|
|
79
|
+
}
|
|
80
|
+
return this.retryExhausted(currentCall, undefined, startedAt, signal);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
const normalized = normalizeUnknownError(error);
|
|
84
|
+
if (normalized.code === "ABORTED") {
|
|
85
|
+
throw normalized;
|
|
86
|
+
}
|
|
87
|
+
const runtimeError = normalized.category === "tool"
|
|
88
|
+
? normalized
|
|
89
|
+
: new RuntimeError({
|
|
90
|
+
code: "TOOL_EXECUTION_FAILED",
|
|
91
|
+
category: "tool",
|
|
92
|
+
message: normalized.message,
|
|
93
|
+
cause: normalized,
|
|
94
|
+
});
|
|
95
|
+
return this.processError(call, runtimeError, startedAt, signal);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async executeMany(calls, context, signal) {
|
|
99
|
+
signal?.throwIfAborted();
|
|
100
|
+
if (calls.length === 0) {
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
const results = new Array(calls.length);
|
|
104
|
+
let nextIndex = 0;
|
|
105
|
+
const workerCount = Math.max(1, Math.min(this.options.maxParallel, calls.length));
|
|
106
|
+
const worker = async () => {
|
|
107
|
+
while (true) {
|
|
108
|
+
signal?.throwIfAborted();
|
|
109
|
+
const index = nextIndex;
|
|
110
|
+
nextIndex += 1;
|
|
111
|
+
if (index >= calls.length) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const call = calls[index];
|
|
115
|
+
if (call !== undefined) {
|
|
116
|
+
results[index] = await this.execute(call, context, signal);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
await Promise.all(Array.from({ length: workerCount }, () => worker()));
|
|
121
|
+
return results;
|
|
122
|
+
}
|
|
123
|
+
hookContext(context, signal) {
|
|
124
|
+
return {
|
|
125
|
+
sessionId: context.sessionId,
|
|
126
|
+
runId: context.runId,
|
|
127
|
+
turnId: context.turnId,
|
|
128
|
+
...(signal === undefined ? {} : { signal }),
|
|
129
|
+
...(context.metadata === undefined
|
|
130
|
+
? {}
|
|
131
|
+
: { metadata: context.metadata }),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
retryExhausted(call, reason, startedAt, signal) {
|
|
135
|
+
return this.processError(call, new RuntimeError({
|
|
136
|
+
code: "TOOL_EXECUTION_FAILED",
|
|
137
|
+
category: "tool",
|
|
138
|
+
message: reason ??
|
|
139
|
+
`Tool retry limit exceeded after ${this.options.maxRetries} retries: ${call.toolName}`,
|
|
140
|
+
retryable: true,
|
|
141
|
+
}), startedAt, signal);
|
|
142
|
+
}
|
|
143
|
+
async runWithTimeout(execute, toolName, signal) {
|
|
144
|
+
if (signal?.aborted) {
|
|
145
|
+
throw new RuntimeError({
|
|
146
|
+
code: "ABORTED",
|
|
147
|
+
category: "abort",
|
|
148
|
+
message: "Operation aborted",
|
|
149
|
+
cause: signal.reason,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
const controller = new AbortController();
|
|
153
|
+
let timeoutId;
|
|
154
|
+
let externalAbort;
|
|
155
|
+
const guard = new Promise((_, reject) => {
|
|
156
|
+
timeoutId = setTimeout(() => {
|
|
157
|
+
controller.abort("timeout");
|
|
158
|
+
reject(new RuntimeError({
|
|
159
|
+
code: "TOOL_TIMEOUT",
|
|
160
|
+
category: "tool",
|
|
161
|
+
message: `Tool timed out after ${this.options.timeoutMs}ms: ${toolName}`,
|
|
162
|
+
retryable: true,
|
|
163
|
+
}));
|
|
164
|
+
}, this.options.timeoutMs);
|
|
165
|
+
if (signal !== undefined) {
|
|
166
|
+
externalAbort = () => {
|
|
167
|
+
controller.abort(signal.reason);
|
|
168
|
+
reject(new RuntimeError({
|
|
169
|
+
code: "ABORTED",
|
|
170
|
+
category: "abort",
|
|
171
|
+
message: "Operation aborted",
|
|
172
|
+
cause: signal.reason,
|
|
173
|
+
}));
|
|
174
|
+
};
|
|
175
|
+
signal.addEventListener("abort", externalAbort, { once: true });
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
try {
|
|
179
|
+
return await Promise.race([execute(controller.signal), guard]);
|
|
180
|
+
}
|
|
181
|
+
finally {
|
|
182
|
+
if (timeoutId !== undefined) {
|
|
183
|
+
clearTimeout(timeoutId);
|
|
184
|
+
}
|
|
185
|
+
if (signal !== undefined && externalAbort !== undefined) {
|
|
186
|
+
signal.removeEventListener("abort", externalAbort);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
processError(call, error, startedAt, signal) {
|
|
191
|
+
const content = {
|
|
192
|
+
error: {
|
|
193
|
+
code: error.code,
|
|
194
|
+
message: error.message,
|
|
195
|
+
retryable: error.retryable,
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
return this.processor.process({
|
|
199
|
+
call,
|
|
200
|
+
raw: {
|
|
201
|
+
value: content,
|
|
202
|
+
isError: true,
|
|
203
|
+
durationMs: Date.now() - startedAt,
|
|
204
|
+
},
|
|
205
|
+
limits: {
|
|
206
|
+
maxChars: this.options.maxResultChars,
|
|
207
|
+
maxLines: this.options.maxResultLines,
|
|
208
|
+
},
|
|
209
|
+
}, signal);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.js","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olRegistry, } from "./registry.js";
|
|
4
|
+
export { JsonSchemaToolInputValidator } from "./validator.js";
|
|
5
|
+
export { DefaultToolResultProcessor } from "./result-processor.js";
|
|
6
|
+
export { DefaultToolExecutor, type ToolExecutor, type DefaultToolExecutorOptions, } from "./executor.js";
|
|
7
|
+
export { ReadFileTool } from "./builtin/read-file.js";
|
|
8
|
+
export { WriteFileTool } from "./builtin/write-file.js";
|
|
9
|
+
export { ListDirectoryTool } from "./builtin/list-directory.js";
|
|
10
|
+
export { RunCommandTool } from "./builtin/run-command.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { DefaultToolRegistry, } from "./registry.js";
|
|
2
|
+
export { JsonSchemaToolInputValidator } from "./validator.js";
|
|
3
|
+
export { DefaultToolResultProcessor } from "./result-processor.js";
|
|
4
|
+
export { DefaultToolExecutor, } from "./executor.js";
|
|
5
|
+
export { ReadFileTool } from "./builtin/read-file.js";
|
|
6
|
+
export { WriteFileTool } from "./builtin/write-file.js";
|
|
7
|
+
export { ListDirectoryTool } from "./builtin/list-directory.js";
|
|
8
|
+
export { RunCommandTool } from "./builtin/run-command.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,mBAAmB,GAEpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,4BAA4B,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EACL,mBAAmB,GAGpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ToolSpec } from "../types/tool.js";
|
|
2
|
+
import type { Tool } from "./tool.js";
|
|
3
|
+
export interface ToolRegistry {
|
|
4
|
+
register(tool: Tool): void;
|
|
5
|
+
unregister(name: string): boolean;
|
|
6
|
+
get(name: string): Tool | undefined;
|
|
7
|
+
require(name: string): Tool;
|
|
8
|
+
list(): readonly Tool[];
|
|
9
|
+
specs(): readonly ToolSpec[];
|
|
10
|
+
}
|
|
11
|
+
export declare class DefaultToolRegistry implements ToolRegistry {
|
|
12
|
+
private readonly tools;
|
|
13
|
+
register(tool: Tool): void;
|
|
14
|
+
unregister(name: string): boolean;
|
|
15
|
+
get(name: string): Tool | undefined;
|
|
16
|
+
require(name: string): Tool;
|
|
17
|
+
list(): readonly Tool[];
|
|
18
|
+
specs(): readonly ToolSpec[];
|
|
19
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { RuntimeError } from "../errors/runtime-error.js";
|
|
2
|
+
export class DefaultToolRegistry {
|
|
3
|
+
tools = new Map();
|
|
4
|
+
register(tool) {
|
|
5
|
+
if (this.tools.has(tool.name)) {
|
|
6
|
+
throw new RuntimeError({
|
|
7
|
+
code: "TOOL_EXECUTION_FAILED",
|
|
8
|
+
category: "tool",
|
|
9
|
+
message: `Tool already registered: ${tool.name}`,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
this.tools.set(tool.name, tool);
|
|
13
|
+
}
|
|
14
|
+
unregister(name) {
|
|
15
|
+
return this.tools.delete(name);
|
|
16
|
+
}
|
|
17
|
+
get(name) {
|
|
18
|
+
return this.tools.get(name);
|
|
19
|
+
}
|
|
20
|
+
require(name) {
|
|
21
|
+
const tool = this.tools.get(name);
|
|
22
|
+
if (tool === undefined) {
|
|
23
|
+
throw new RuntimeError({
|
|
24
|
+
code: "TOOL_NOT_FOUND",
|
|
25
|
+
category: "tool",
|
|
26
|
+
message: `Tool not found: ${name}`,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return tool;
|
|
30
|
+
}
|
|
31
|
+
list() {
|
|
32
|
+
return [...this.tools.values()];
|
|
33
|
+
}
|
|
34
|
+
specs() {
|
|
35
|
+
return this.list().map((tool) => ({
|
|
36
|
+
name: tool.name,
|
|
37
|
+
description: tool.description,
|
|
38
|
+
inputSchema: structuredClone(tool.inputSchema),
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/tools/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAa1D,MAAM,OAAO,mBAAmB;IACb,KAAK,GAAG,IAAI,GAAG,EAAgB,CAAC;IAEjD,QAAQ,CAAC,IAAU;QACjB,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YAC9B,MAAM,IAAI,YAAY,CAAC;gBACrB,IAAI,EAAE,uBAAuB;gBAC7B,QAAQ,EAAE,MAAM;gBAChB,OAAO,EAAE,4BAA4B,IAAI,CAAC,IAAI,EAAE;aACjD,CAAC,CAAC;QACL,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAClC,CAAC;IAED,UAAU,CAAC,IAAY;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED,OAAO,CAAC,IAAY;QAClB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAClC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,MAAM,IAAI,YAAY,CAAC;gBACrB,IAAI,EAAE,gBAAgB;gBACtB,QAAQ,EAAE,MAAM;gBAChB,OAAO,EAAE,mBAAmB,IAAI,EAAE;aACnC,CAAC,CAAC;QACL,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI;QACF,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;IAClC,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAChC,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,WAAW,EAAE,eAAe,CAAC,IAAI,CAAC,WAAW,CAAC;SAC/C,CAAC,CAAC,CAAC;IACN,CAAC;CACF"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ToolExecutionResult, ToolResultProcessRequest, ToolResultProcessor } from "./types.js";
|
|
2
|
+
export declare class DefaultToolResultProcessor implements ToolResultProcessor {
|
|
3
|
+
process(request: ToolResultProcessRequest, signal?: AbortSignal): Promise<ToolExecutionResult>;
|
|
4
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
const TRUNCATION_MARKER = "\n... [truncated] ...\n";
|
|
2
|
+
function jsonSafe(value, seen = new Set()) {
|
|
3
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
4
|
+
return value;
|
|
5
|
+
}
|
|
6
|
+
if (typeof value === "number") {
|
|
7
|
+
return Number.isFinite(value) ? value : String(value);
|
|
8
|
+
}
|
|
9
|
+
if (typeof value === "bigint") {
|
|
10
|
+
return value.toString();
|
|
11
|
+
}
|
|
12
|
+
if (Buffer.isBuffer(value)) {
|
|
13
|
+
return value.toString("utf8");
|
|
14
|
+
}
|
|
15
|
+
if (value instanceof Error) {
|
|
16
|
+
const result = {
|
|
17
|
+
name: value.name,
|
|
18
|
+
message: value.message,
|
|
19
|
+
};
|
|
20
|
+
if (value.stack) {
|
|
21
|
+
result.stack = value.stack;
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
if (value instanceof Date) {
|
|
26
|
+
return value.toISOString();
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(value)) {
|
|
29
|
+
if (seen.has(value)) {
|
|
30
|
+
return "[Circular]";
|
|
31
|
+
}
|
|
32
|
+
seen.add(value);
|
|
33
|
+
try {
|
|
34
|
+
return value.map((item) => jsonSafe(item, seen));
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
seen.delete(value);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (typeof value === "object") {
|
|
41
|
+
if (seen.has(value)) {
|
|
42
|
+
return "[Circular]";
|
|
43
|
+
}
|
|
44
|
+
seen.add(value);
|
|
45
|
+
try {
|
|
46
|
+
const result = {};
|
|
47
|
+
for (const [key, child] of Object.entries(value)) {
|
|
48
|
+
result[key] = jsonSafe(child, seen);
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
seen.delete(value);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return String(value);
|
|
57
|
+
}
|
|
58
|
+
function truncateByLines(text, maxLines) {
|
|
59
|
+
if (maxLines <= 0) {
|
|
60
|
+
return { text: "", truncated: text.length > 0 };
|
|
61
|
+
}
|
|
62
|
+
const lines = text.split(/\r?\n/);
|
|
63
|
+
if (lines.length <= maxLines) {
|
|
64
|
+
return { text, truncated: false };
|
|
65
|
+
}
|
|
66
|
+
const headCount = Math.ceil(maxLines / 2);
|
|
67
|
+
const tailCount = Math.floor(maxLines / 2);
|
|
68
|
+
const head = lines.slice(0, headCount).join("\n");
|
|
69
|
+
const tail = tailCount === 0 ? "" : lines.slice(-tailCount).join("\n");
|
|
70
|
+
return {
|
|
71
|
+
text: tail ? `${head}${TRUNCATION_MARKER}${tail}` : head,
|
|
72
|
+
truncated: true,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function truncateByChars(text, maxChars) {
|
|
76
|
+
if (text.length <= maxChars) {
|
|
77
|
+
return { text, truncated: false };
|
|
78
|
+
}
|
|
79
|
+
if (maxChars <= 0) {
|
|
80
|
+
return { text: "", truncated: true };
|
|
81
|
+
}
|
|
82
|
+
if (maxChars <= TRUNCATION_MARKER.length + 2) {
|
|
83
|
+
return { text: text.slice(0, maxChars), truncated: true };
|
|
84
|
+
}
|
|
85
|
+
const remaining = maxChars - TRUNCATION_MARKER.length;
|
|
86
|
+
const headCount = Math.ceil(remaining / 2);
|
|
87
|
+
const tailCount = Math.floor(remaining / 2);
|
|
88
|
+
return {
|
|
89
|
+
text: text.slice(0, headCount) +
|
|
90
|
+
TRUNCATION_MARKER +
|
|
91
|
+
text.slice(text.length - tailCount),
|
|
92
|
+
truncated: true,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function enforceLimits(content, maxChars, maxLines) {
|
|
96
|
+
const rendered = typeof content === "string" ? content : JSON.stringify(content, null, 2);
|
|
97
|
+
const lineLimited = truncateByLines(rendered, maxLines);
|
|
98
|
+
const charLimited = truncateByChars(lineLimited.text, maxChars);
|
|
99
|
+
const truncated = lineLimited.truncated || charLimited.truncated;
|
|
100
|
+
if (!truncated) {
|
|
101
|
+
return { content, truncated: false };
|
|
102
|
+
}
|
|
103
|
+
return { content: charLimited.text, truncated: true };
|
|
104
|
+
}
|
|
105
|
+
export class DefaultToolResultProcessor {
|
|
106
|
+
async process(request, signal) {
|
|
107
|
+
signal?.throwIfAborted();
|
|
108
|
+
const normalized = jsonSafe(request.raw.value);
|
|
109
|
+
const limited = enforceLimits(normalized, request.limits.maxChars, request.limits.maxLines);
|
|
110
|
+
return {
|
|
111
|
+
call: structuredClone(request.call),
|
|
112
|
+
content: limited.content,
|
|
113
|
+
isError: request.raw.isError ?? false,
|
|
114
|
+
truncated: limited.truncated,
|
|
115
|
+
durationMs: request.raw.durationMs ?? 0,
|
|
116
|
+
...(request.raw.metadata === undefined
|
|
117
|
+
? {}
|
|
118
|
+
: { metadata: structuredClone(request.raw.metadata) }),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=result-processor.js.map
|