@ainative/cody-sdk 0.1.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/.github/logo.svg +3 -0
- package/CHANGELOG.md +2345 -0
- package/LICENSE +8 -0
- package/README.md +45 -0
- package/_vendor/partial-json-parser/parser.d.mts +3 -0
- package/_vendor/partial-json-parser/parser.d.mts.map +1 -0
- package/_vendor/partial-json-parser/parser.d.ts +3 -0
- package/_vendor/partial-json-parser/parser.d.ts.map +1 -0
- package/_vendor/partial-json-parser/parser.js +226 -0
- package/_vendor/partial-json-parser/parser.js.map +1 -0
- package/_vendor/partial-json-parser/parser.mjs +223 -0
- package/_vendor/partial-json-parser/parser.mjs.map +1 -0
- package/ainative.d.mts +32 -0
- package/ainative.d.mts.map +1 -0
- package/ainative.d.ts +32 -0
- package/ainative.d.ts.map +1 -0
- package/ainative.js +78 -0
- package/ainative.js.map +1 -0
- package/ainative.mjs +71 -0
- package/ainative.mjs.map +1 -0
- package/api-promise.d.mts +2 -0
- package/api-promise.d.mts.map +1 -0
- package/api-promise.d.ts +2 -0
- package/api-promise.d.ts.map +1 -0
- package/api-promise.js +6 -0
- package/api-promise.js.map +1 -0
- package/api-promise.mjs +2 -0
- package/api-promise.mjs.map +1 -0
- package/bin/cli +53 -0
- package/bin/migration-config.json +82 -0
- package/client.d.mts +247 -0
- package/client.d.mts.map +1 -0
- package/client.d.ts +247 -0
- package/client.d.ts.map +1 -0
- package/client.js +567 -0
- package/client.js.map +1 -0
- package/client.mjs +562 -0
- package/client.mjs.map +1 -0
- package/core/api-promise.d.mts +49 -0
- package/core/api-promise.d.mts.map +1 -0
- package/core/api-promise.d.ts +49 -0
- package/core/api-promise.d.ts.map +1 -0
- package/core/api-promise.js +76 -0
- package/core/api-promise.js.map +1 -0
- package/core/api-promise.mjs +72 -0
- package/core/api-promise.mjs.map +1 -0
- package/core/error.d.mts +50 -0
- package/core/error.d.mts.map +1 -0
- package/core/error.d.ts +50 -0
- package/core/error.d.ts.map +1 -0
- package/core/error.js +116 -0
- package/core/error.js.map +1 -0
- package/core/error.mjs +100 -0
- package/core/error.mjs.map +1 -0
- package/core/pagination.d.mts +105 -0
- package/core/pagination.d.mts.map +1 -0
- package/core/pagination.d.ts +105 -0
- package/core/pagination.d.ts.map +1 -0
- package/core/pagination.js +185 -0
- package/core/pagination.js.map +1 -0
- package/core/pagination.mjs +177 -0
- package/core/pagination.mjs.map +1 -0
- package/core/resource.d.mts +6 -0
- package/core/resource.d.mts.map +1 -0
- package/core/resource.d.ts +6 -0
- package/core/resource.d.ts.map +1 -0
- package/core/resource.js +11 -0
- package/core/resource.js.map +1 -0
- package/core/resource.mjs +7 -0
- package/core/resource.mjs.map +1 -0
- package/core/streaming.d.mts +33 -0
- package/core/streaming.d.mts.map +1 -0
- package/core/streaming.d.ts +33 -0
- package/core/streaming.d.ts.map +1 -0
- package/core/streaming.js +290 -0
- package/core/streaming.js.map +1 -0
- package/core/streaming.mjs +285 -0
- package/core/streaming.mjs.map +1 -0
- package/core/uploads.d.mts +3 -0
- package/core/uploads.d.mts.map +1 -0
- package/core/uploads.d.ts +3 -0
- package/core/uploads.d.ts.map +1 -0
- package/core/uploads.js +6 -0
- package/core/uploads.js.map +1 -0
- package/core/uploads.mjs +2 -0
- package/core/uploads.mjs.map +1 -0
- package/error.d.mts +2 -0
- package/error.d.mts.map +1 -0
- package/error.d.ts +2 -0
- package/error.d.ts.map +1 -0
- package/error.js +6 -0
- package/error.js.map +1 -0
- package/error.mjs +2 -0
- package/error.mjs.map +1 -0
- package/helpers/beta/json-schema.d.mts +31 -0
- package/helpers/beta/json-schema.d.mts.map +1 -0
- package/helpers/beta/json-schema.d.ts +31 -0
- package/helpers/beta/json-schema.d.ts.map +1 -0
- package/helpers/beta/json-schema.js +56 -0
- package/helpers/beta/json-schema.js.map +1 -0
- package/helpers/beta/json-schema.mjs +52 -0
- package/helpers/beta/json-schema.mjs.map +1 -0
- package/helpers/beta/mcp.d.mts +303 -0
- package/helpers/beta/mcp.d.mts.map +1 -0
- package/helpers/beta/mcp.d.ts +303 -0
- package/helpers/beta/mcp.d.ts.map +1 -0
- package/helpers/beta/mcp.js +419 -0
- package/helpers/beta/mcp.js.map +1 -0
- package/helpers/beta/mcp.mjs +406 -0
- package/helpers/beta/mcp.mjs.map +1 -0
- package/helpers/beta/memory.d.mts +11 -0
- package/helpers/beta/memory.d.mts.map +1 -0
- package/helpers/beta/memory.d.ts +11 -0
- package/helpers/beta/memory.d.ts.map +1 -0
- package/helpers/beta/memory.js +18 -0
- package/helpers/beta/memory.js.map +1 -0
- package/helpers/beta/memory.mjs +15 -0
- package/helpers/beta/memory.mjs.map +1 -0
- package/helpers/beta/zod.d.mts +27 -0
- package/helpers/beta/zod.d.mts.map +1 -0
- package/helpers/beta/zod.d.ts +27 -0
- package/helpers/beta/zod.d.ts.map +1 -0
- package/helpers/beta/zod.js +57 -0
- package/helpers/beta/zod.js.map +1 -0
- package/helpers/beta/zod.mjs +52 -0
- package/helpers/beta/zod.mjs.map +1 -0
- package/helpers/index.d.mts +3 -0
- package/helpers/index.d.mts.map +1 -0
- package/helpers/index.d.ts +3 -0
- package/helpers/index.d.ts.map +1 -0
- package/helpers/index.js +8 -0
- package/helpers/index.js.map +1 -0
- package/helpers/index.mjs +3 -0
- package/helpers/index.mjs.map +1 -0
- package/helpers/json-schema.d.mts +18 -0
- package/helpers/json-schema.d.mts.map +1 -0
- package/helpers/json-schema.d.ts +18 -0
- package/helpers/json-schema.d.ts.map +1 -0
- package/helpers/json-schema.js +37 -0
- package/helpers/json-schema.js.map +1 -0
- package/helpers/json-schema.mjs +34 -0
- package/helpers/json-schema.mjs.map +1 -0
- package/helpers/zod.d.mts +13 -0
- package/helpers/zod.d.mts.map +1 -0
- package/helpers/zod.d.ts +13 -0
- package/helpers/zod.d.ts.map +1 -0
- package/helpers/zod.js +47 -0
- package/helpers/zod.js.map +1 -0
- package/helpers/zod.mjs +43 -0
- package/helpers/zod.mjs.map +1 -0
- package/index.d.mts +9 -0
- package/index.d.mts.map +1 -0
- package/index.d.ts +9 -0
- package/index.d.ts.map +1 -0
- package/index.js +42 -0
- package/index.js.map +1 -0
- package/index.mjs +9 -0
- package/index.mjs.map +1 -0
- package/internal/builtin-types.d.mts +73 -0
- package/internal/builtin-types.d.mts.map +1 -0
- package/internal/builtin-types.d.ts +73 -0
- package/internal/builtin-types.d.ts.map +1 -0
- package/internal/builtin-types.js +4 -0
- package/internal/builtin-types.js.map +1 -0
- package/internal/builtin-types.mjs +3 -0
- package/internal/builtin-types.mjs.map +1 -0
- package/internal/constants.d.mts +5 -0
- package/internal/constants.d.mts.map +1 -0
- package/internal/constants.d.ts +5 -0
- package/internal/constants.d.ts.map +1 -0
- package/internal/constants.js +18 -0
- package/internal/constants.js.map +1 -0
- package/internal/constants.mjs +15 -0
- package/internal/constants.mjs.map +1 -0
- package/internal/decoders/jsonl.d.mts +10 -0
- package/internal/decoders/jsonl.d.mts.map +1 -0
- package/internal/decoders/jsonl.d.ts +10 -0
- package/internal/decoders/jsonl.d.ts.map +1 -0
- package/internal/decoders/jsonl.js +39 -0
- package/internal/decoders/jsonl.js.map +1 -0
- package/internal/decoders/jsonl.mjs +35 -0
- package/internal/decoders/jsonl.mjs.map +1 -0
- package/internal/decoders/line.d.mts +17 -0
- package/internal/decoders/line.d.mts.map +1 -0
- package/internal/decoders/line.d.ts +17 -0
- package/internal/decoders/line.d.ts.map +1 -0
- package/internal/decoders/line.js +113 -0
- package/internal/decoders/line.js.map +1 -0
- package/internal/decoders/line.mjs +108 -0
- package/internal/decoders/line.mjs.map +1 -0
- package/internal/detect-platform.d.mts +15 -0
- package/internal/detect-platform.d.mts.map +1 -0
- package/internal/detect-platform.d.ts +15 -0
- package/internal/detect-platform.d.ts.map +1 -0
- package/internal/detect-platform.js +162 -0
- package/internal/detect-platform.js.map +1 -0
- package/internal/detect-platform.mjs +157 -0
- package/internal/detect-platform.mjs.map +1 -0
- package/internal/errors.d.mts +3 -0
- package/internal/errors.d.mts.map +1 -0
- package/internal/errors.d.ts +3 -0
- package/internal/errors.d.ts.map +1 -0
- package/internal/errors.js +41 -0
- package/internal/errors.js.map +1 -0
- package/internal/errors.mjs +36 -0
- package/internal/errors.mjs.map +1 -0
- package/internal/headers.d.mts +22 -0
- package/internal/headers.d.mts.map +1 -0
- package/internal/headers.d.ts +22 -0
- package/internal/headers.d.ts.map +1 -0
- package/internal/headers.js +79 -0
- package/internal/headers.js.map +1 -0
- package/internal/headers.mjs +74 -0
- package/internal/headers.mjs.map +1 -0
- package/internal/parse.d.mts +17 -0
- package/internal/parse.d.mts.map +1 -0
- package/internal/parse.d.ts +17 -0
- package/internal/parse.d.ts.map +1 -0
- package/internal/parse.js +60 -0
- package/internal/parse.js.map +1 -0
- package/internal/parse.mjs +56 -0
- package/internal/parse.mjs.map +1 -0
- package/internal/request-options.d.mts +77 -0
- package/internal/request-options.d.mts.map +1 -0
- package/internal/request-options.d.ts +77 -0
- package/internal/request-options.d.ts.map +1 -0
- package/internal/request-options.js +14 -0
- package/internal/request-options.js.map +1 -0
- package/internal/request-options.mjs +10 -0
- package/internal/request-options.mjs.map +1 -0
- package/internal/shim-types.d.mts +17 -0
- package/internal/shim-types.d.mts.map +1 -0
- package/internal/shim-types.d.ts +17 -0
- package/internal/shim-types.d.ts.map +1 -0
- package/internal/shim-types.js +4 -0
- package/internal/shim-types.js.map +1 -0
- package/internal/shim-types.mjs +3 -0
- package/internal/shim-types.mjs.map +1 -0
- package/internal/shims.d.mts +20 -0
- package/internal/shims.d.mts.map +1 -0
- package/internal/shims.d.ts +20 -0
- package/internal/shims.d.ts.map +1 -0
- package/internal/shims.js +92 -0
- package/internal/shims.js.map +1 -0
- package/internal/shims.mjs +85 -0
- package/internal/shims.mjs.map +1 -0
- package/internal/stream-utils.d.mts +8 -0
- package/internal/stream-utils.d.mts.map +1 -0
- package/internal/stream-utils.d.ts +8 -0
- package/internal/stream-utils.d.ts.map +1 -0
- package/internal/stream-utils.js +38 -0
- package/internal/stream-utils.js.map +1 -0
- package/internal/stream-utils.mjs +35 -0
- package/internal/stream-utils.mjs.map +1 -0
- package/internal/to-file.d.mts +45 -0
- package/internal/to-file.d.mts.map +1 -0
- package/internal/to-file.d.ts +45 -0
- package/internal/to-file.d.ts.map +1 -0
- package/internal/to-file.js +96 -0
- package/internal/to-file.js.map +1 -0
- package/internal/to-file.mjs +93 -0
- package/internal/to-file.mjs.map +1 -0
- package/internal/tslib.js +81 -0
- package/internal/tslib.mjs +17 -0
- package/internal/types.d.mts +69 -0
- package/internal/types.d.mts.map +1 -0
- package/internal/types.d.ts +69 -0
- package/internal/types.d.ts.map +1 -0
- package/internal/types.js +4 -0
- package/internal/types.js.map +1 -0
- package/internal/types.mjs +3 -0
- package/internal/types.mjs.map +1 -0
- package/internal/uploads.d.mts +42 -0
- package/internal/uploads.d.mts.map +1 -0
- package/internal/uploads.d.ts +42 -0
- package/internal/uploads.d.ts.map +1 -0
- package/internal/uploads.js +145 -0
- package/internal/uploads.js.map +1 -0
- package/internal/uploads.mjs +135 -0
- package/internal/uploads.mjs.map +1 -0
- package/internal/utils/base64.d.mts +3 -0
- package/internal/utils/base64.d.mts.map +1 -0
- package/internal/utils/base64.d.ts +3 -0
- package/internal/utils/base64.d.ts.map +1 -0
- package/internal/utils/base64.js +38 -0
- package/internal/utils/base64.js.map +1 -0
- package/internal/utils/base64.mjs +33 -0
- package/internal/utils/base64.mjs.map +1 -0
- package/internal/utils/bytes.d.mts +4 -0
- package/internal/utils/bytes.d.mts.map +1 -0
- package/internal/utils/bytes.d.ts +4 -0
- package/internal/utils/bytes.d.ts.map +1 -0
- package/internal/utils/bytes.js +31 -0
- package/internal/utils/bytes.js.map +1 -0
- package/internal/utils/bytes.mjs +26 -0
- package/internal/utils/bytes.mjs.map +1 -0
- package/internal/utils/env.d.mts +9 -0
- package/internal/utils/env.d.mts.map +1 -0
- package/internal/utils/env.d.ts +9 -0
- package/internal/utils/env.d.ts.map +1 -0
- package/internal/utils/env.js +22 -0
- package/internal/utils/env.js.map +1 -0
- package/internal/utils/env.mjs +18 -0
- package/internal/utils/env.mjs.map +1 -0
- package/internal/utils/log.d.mts +37 -0
- package/internal/utils/log.d.mts.map +1 -0
- package/internal/utils/log.d.ts +37 -0
- package/internal/utils/log.d.ts.map +1 -0
- package/internal/utils/log.js +86 -0
- package/internal/utils/log.js.map +1 -0
- package/internal/utils/log.mjs +80 -0
- package/internal/utils/log.mjs.map +1 -0
- package/internal/utils/path.d.mts +15 -0
- package/internal/utils/path.d.mts.map +1 -0
- package/internal/utils/path.d.ts +15 -0
- package/internal/utils/path.d.ts.map +1 -0
- package/internal/utils/path.js +79 -0
- package/internal/utils/path.js.map +1 -0
- package/internal/utils/path.mjs +74 -0
- package/internal/utils/path.mjs.map +1 -0
- package/internal/utils/query.d.mts +5 -0
- package/internal/utils/query.d.mts.map +1 -0
- package/internal/utils/query.d.ts +5 -0
- package/internal/utils/query.d.ts.map +1 -0
- package/internal/utils/query.js +23 -0
- package/internal/utils/query.js.map +1 -0
- package/internal/utils/query.mjs +20 -0
- package/internal/utils/query.mjs.map +1 -0
- package/internal/utils/sleep.d.mts +2 -0
- package/internal/utils/sleep.d.mts.map +1 -0
- package/internal/utils/sleep.d.ts +2 -0
- package/internal/utils/sleep.d.ts.map +1 -0
- package/internal/utils/sleep.js +7 -0
- package/internal/utils/sleep.js.map +1 -0
- package/internal/utils/sleep.mjs +3 -0
- package/internal/utils/sleep.mjs.map +1 -0
- package/internal/utils/uuid.d.mts +5 -0
- package/internal/utils/uuid.d.mts.map +1 -0
- package/internal/utils/uuid.d.ts +5 -0
- package/internal/utils/uuid.d.ts.map +1 -0
- package/internal/utils/uuid.js +19 -0
- package/internal/utils/uuid.js.map +1 -0
- package/internal/utils/uuid.mjs +15 -0
- package/internal/utils/uuid.mjs.map +1 -0
- package/internal/utils/values.d.mts +19 -0
- package/internal/utils/values.d.mts.map +1 -0
- package/internal/utils/values.d.ts +19 -0
- package/internal/utils/values.d.ts.map +1 -0
- package/internal/utils/values.js +119 -0
- package/internal/utils/values.js.map +1 -0
- package/internal/utils/values.mjs +100 -0
- package/internal/utils/values.mjs.map +1 -0
- package/internal/utils.d.mts +8 -0
- package/internal/utils.d.mts.map +1 -0
- package/internal/utils.d.ts +8 -0
- package/internal/utils.d.ts.map +1 -0
- package/internal/utils.js +12 -0
- package/internal/utils.js.map +1 -0
- package/internal/utils.mjs +9 -0
- package/internal/utils.mjs.map +1 -0
- package/lib/BetaMessageStream.d.mts +122 -0
- package/lib/BetaMessageStream.d.mts.map +1 -0
- package/lib/BetaMessageStream.d.ts +122 -0
- package/lib/BetaMessageStream.d.ts.map +1 -0
- package/lib/BetaMessageStream.js +620 -0
- package/lib/BetaMessageStream.js.map +1 -0
- package/lib/BetaMessageStream.mjs +616 -0
- package/lib/BetaMessageStream.mjs.map +1 -0
- package/lib/MessageStream.d.mts +121 -0
- package/lib/MessageStream.d.mts.map +1 -0
- package/lib/MessageStream.d.ts +121 -0
- package/lib/MessageStream.d.ts.map +1 -0
- package/lib/MessageStream.js +595 -0
- package/lib/MessageStream.js.map +1 -0
- package/lib/MessageStream.mjs +591 -0
- package/lib/MessageStream.mjs.map +1 -0
- package/lib/beta-parser.d.mts +37 -0
- package/lib/beta-parser.d.mts.map +1 -0
- package/lib/beta-parser.d.ts +37 -0
- package/lib/beta-parser.d.ts.map +1 -0
- package/lib/beta-parser.js +79 -0
- package/lib/beta-parser.js.map +1 -0
- package/lib/beta-parser.mjs +75 -0
- package/lib/beta-parser.mjs.map +1 -0
- package/lib/parser.d.mts +32 -0
- package/lib/parser.d.mts.map +1 -0
- package/lib/parser.d.ts +32 -0
- package/lib/parser.d.ts.map +1 -0
- package/lib/parser.js +66 -0
- package/lib/parser.js.map +1 -0
- package/lib/parser.mjs +62 -0
- package/lib/parser.mjs.map +1 -0
- package/lib/stainless-helper-header.d.mts +34 -0
- package/lib/stainless-helper-header.d.mts.map +1 -0
- package/lib/stainless-helper-header.d.ts +34 -0
- package/lib/stainless-helper-header.d.ts.map +1 -0
- package/lib/stainless-helper-header.js +70 -0
- package/lib/stainless-helper-header.js.map +1 -0
- package/lib/stainless-helper-header.mjs +63 -0
- package/lib/stainless-helper-header.mjs.map +1 -0
- package/lib/tools/BetaRunnableTool.d.mts +12 -0
- package/lib/tools/BetaRunnableTool.d.mts.map +1 -0
- package/lib/tools/BetaRunnableTool.d.ts +12 -0
- package/lib/tools/BetaRunnableTool.d.ts.map +1 -0
- package/lib/tools/BetaRunnableTool.js +3 -0
- package/lib/tools/BetaRunnableTool.js.map +1 -0
- package/lib/tools/BetaRunnableTool.mjs +2 -0
- package/lib/tools/BetaRunnableTool.mjs.map +1 -0
- package/lib/tools/BetaToolRunner.d.mts +141 -0
- package/lib/tools/BetaToolRunner.d.mts.map +1 -0
- package/lib/tools/BetaToolRunner.d.ts +141 -0
- package/lib/tools/BetaToolRunner.d.ts.map +1 -0
- package/lib/tools/BetaToolRunner.js +381 -0
- package/lib/tools/BetaToolRunner.js.map +1 -0
- package/lib/tools/BetaToolRunner.mjs +377 -0
- package/lib/tools/BetaToolRunner.mjs.map +1 -0
- package/lib/tools/CompactionControl.d.mts +30 -0
- package/lib/tools/CompactionControl.d.mts.map +1 -0
- package/lib/tools/CompactionControl.d.ts +30 -0
- package/lib/tools/CompactionControl.d.ts.map +1 -0
- package/lib/tools/CompactionControl.js +28 -0
- package/lib/tools/CompactionControl.js.map +1 -0
- package/lib/tools/CompactionControl.mjs +25 -0
- package/lib/tools/CompactionControl.mjs.map +1 -0
- package/lib/tools/ToolError.d.mts +33 -0
- package/lib/tools/ToolError.d.mts.map +1 -0
- package/lib/tools/ToolError.d.ts +33 -0
- package/lib/tools/ToolError.d.ts.map +1 -0
- package/lib/tools/ToolError.js +42 -0
- package/lib/tools/ToolError.js.map +1 -0
- package/lib/tools/ToolError.mjs +38 -0
- package/lib/tools/ToolError.mjs.map +1 -0
- package/lib/tools/ToolRunner.d.mts +132 -0
- package/lib/tools/ToolRunner.d.mts.map +1 -0
- package/lib/tools/ToolRunner.d.ts +132 -0
- package/lib/tools/ToolRunner.d.ts.map +1 -0
- package/lib/tools/ToolRunner.js +294 -0
- package/lib/tools/ToolRunner.js.map +1 -0
- package/lib/tools/ToolRunner.mjs +290 -0
- package/lib/tools/ToolRunner.mjs.map +1 -0
- package/lib/transform-json-schema.d.mts +3 -0
- package/lib/transform-json-schema.d.mts.map +1 -0
- package/lib/transform-json-schema.d.ts +3 -0
- package/lib/transform-json-schema.d.ts.map +1 -0
- package/lib/transform-json-schema.js +114 -0
- package/lib/transform-json-schema.js.map +1 -0
- package/lib/transform-json-schema.mjs +111 -0
- package/lib/transform-json-schema.mjs.map +1 -0
- package/package.json +246 -0
- package/pagination.d.mts +2 -0
- package/pagination.d.mts.map +1 -0
- package/pagination.d.ts +2 -0
- package/pagination.d.ts.map +1 -0
- package/pagination.js +6 -0
- package/pagination.js.map +1 -0
- package/pagination.mjs +2 -0
- package/pagination.mjs.map +1 -0
- package/resource.d.mts +2 -0
- package/resource.d.mts.map +1 -0
- package/resource.d.ts +2 -0
- package/resource.d.ts.map +1 -0
- package/resource.js +6 -0
- package/resource.js.map +1 -0
- package/resource.mjs +2 -0
- package/resource.mjs.map +1 -0
- package/resources/beta/beta.d.mts +66 -0
- package/resources/beta/beta.d.mts.map +1 -0
- package/resources/beta/beta.d.ts +66 -0
- package/resources/beta/beta.d.ts.map +1 -0
- package/resources/beta/beta.js +29 -0
- package/resources/beta/beta.js.map +1 -0
- package/resources/beta/beta.mjs +24 -0
- package/resources/beta/beta.mjs.map +1 -0
- package/resources/beta/files.d.mts +151 -0
- package/resources/beta/files.d.mts.map +1 -0
- package/resources/beta/files.d.ts +151 -0
- package/resources/beta/files.d.ts.map +1 -0
- package/resources/beta/files.js +124 -0
- package/resources/beta/files.js.map +1 -0
- package/resources/beta/files.mjs +120 -0
- package/resources/beta/files.mjs.map +1 -0
- package/resources/beta/index.d.mts +6 -0
- package/resources/beta/index.d.mts.map +1 -0
- package/resources/beta/index.d.ts +6 -0
- package/resources/beta/index.d.ts.map +1 -0
- package/resources/beta/index.js +15 -0
- package/resources/beta/index.js.map +1 -0
- package/resources/beta/index.mjs +7 -0
- package/resources/beta/index.mjs.map +1 -0
- package/resources/beta/messages/batches.d.mts +646 -0
- package/resources/beta/messages/batches.d.mts.map +1 -0
- package/resources/beta/messages/batches.d.ts +646 -0
- package/resources/beta/messages/batches.d.ts.map +1 -0
- package/resources/beta/messages/batches.js +204 -0
- package/resources/beta/messages/batches.js.map +1 -0
- package/resources/beta/messages/batches.mjs +200 -0
- package/resources/beta/messages/batches.mjs.map +1 -0
- package/resources/beta/messages/index.d.mts +4 -0
- package/resources/beta/messages/index.d.mts.map +1 -0
- package/resources/beta/messages/index.d.ts +4 -0
- package/resources/beta/messages/index.d.ts.map +1 -0
- package/resources/beta/messages/index.js +12 -0
- package/resources/beta/messages/index.js.map +1 -0
- package/resources/beta/messages/index.mjs +5 -0
- package/resources/beta/messages/index.mjs.map +1 -0
- package/resources/beta/messages/messages.d.mts +2993 -0
- package/resources/beta/messages/messages.d.mts.map +1 -0
- package/resources/beta/messages/messages.d.ts +2993 -0
- package/resources/beta/messages/messages.d.ts.map +1 -0
- package/resources/beta/messages/messages.js +163 -0
- package/resources/beta/messages/messages.js.map +1 -0
- package/resources/beta/messages/messages.mjs +156 -0
- package/resources/beta/messages/messages.mjs.map +1 -0
- package/resources/beta/messages.d.mts +2 -0
- package/resources/beta/messages.d.mts.map +1 -0
- package/resources/beta/messages.d.ts +2 -0
- package/resources/beta/messages.d.ts.map +1 -0
- package/resources/beta/messages.js +6 -0
- package/resources/beta/messages.js.map +1 -0
- package/resources/beta/messages.mjs +3 -0
- package/resources/beta/messages.mjs.map +1 -0
- package/resources/beta/models.d.mts +208 -0
- package/resources/beta/models.d.mts.map +1 -0
- package/resources/beta/models.d.ts +208 -0
- package/resources/beta/models.d.ts.map +1 -0
- package/resources/beta/models.js +60 -0
- package/resources/beta/models.js.map +1 -0
- package/resources/beta/models.mjs +56 -0
- package/resources/beta/models.mjs.map +1 -0
- package/resources/beta/skills/index.d.mts +3 -0
- package/resources/beta/skills/index.d.mts.map +1 -0
- package/resources/beta/skills/index.d.ts +3 -0
- package/resources/beta/skills/index.d.ts.map +1 -0
- package/resources/beta/skills/index.js +9 -0
- package/resources/beta/skills/index.js.map +1 -0
- package/resources/beta/skills/index.mjs +4 -0
- package/resources/beta/skills/index.mjs.map +1 -0
- package/resources/beta/skills/skills.d.mts +249 -0
- package/resources/beta/skills/skills.d.mts.map +1 -0
- package/resources/beta/skills/skills.d.ts +249 -0
- package/resources/beta/skills/skills.d.ts.map +1 -0
- package/resources/beta/skills/skills.js +98 -0
- package/resources/beta/skills/skills.js.map +1 -0
- package/resources/beta/skills/skills.mjs +93 -0
- package/resources/beta/skills/skills.mjs.map +1 -0
- package/resources/beta/skills/versions.d.mts +257 -0
- package/resources/beta/skills/versions.d.mts.map +1 -0
- package/resources/beta/skills/versions.d.ts +257 -0
- package/resources/beta/skills/versions.d.ts.map +1 -0
- package/resources/beta/skills/versions.js +100 -0
- package/resources/beta/skills/versions.js.map +1 -0
- package/resources/beta/skills/versions.mjs +96 -0
- package/resources/beta/skills/versions.mjs.map +1 -0
- package/resources/beta/skills.d.mts +2 -0
- package/resources/beta/skills.d.mts.map +1 -0
- package/resources/beta/skills.d.ts +2 -0
- package/resources/beta/skills.d.ts.map +1 -0
- package/resources/beta/skills.js +6 -0
- package/resources/beta/skills.js.map +1 -0
- package/resources/beta/skills.mjs +3 -0
- package/resources/beta/skills.mjs.map +1 -0
- package/resources/beta.d.mts +2 -0
- package/resources/beta.d.mts.map +1 -0
- package/resources/beta.d.ts +2 -0
- package/resources/beta.d.ts.map +1 -0
- package/resources/beta.js +6 -0
- package/resources/beta.js.map +1 -0
- package/resources/beta.mjs +3 -0
- package/resources/beta.mjs.map +1 -0
- package/resources/completions.d.mts +182 -0
- package/resources/completions.d.mts.map +1 -0
- package/resources/completions.d.ts +182 -0
- package/resources/completions.d.ts.map +1 -0
- package/resources/completions.js +23 -0
- package/resources/completions.js.map +1 -0
- package/resources/completions.mjs +19 -0
- package/resources/completions.mjs.map +1 -0
- package/resources/index.d.mts +6 -0
- package/resources/index.d.mts.map +1 -0
- package/resources/index.d.ts +6 -0
- package/resources/index.d.ts.map +1 -0
- package/resources/index.js +15 -0
- package/resources/index.js.map +1 -0
- package/resources/index.mjs +7 -0
- package/resources/index.mjs.map +1 -0
- package/resources/messages/batches.d.mts +304 -0
- package/resources/messages/batches.d.mts.map +1 -0
- package/resources/messages/batches.d.ts +304 -0
- package/resources/messages/batches.d.ts.map +1 -0
- package/resources/messages/batches.js +153 -0
- package/resources/messages/batches.js.map +1 -0
- package/resources/messages/batches.mjs +149 -0
- package/resources/messages/batches.mjs.map +1 -0
- package/resources/messages/index.d.mts +3 -0
- package/resources/messages/index.d.mts.map +1 -0
- package/resources/messages/index.d.ts +3 -0
- package/resources/messages/index.d.ts.map +1 -0
- package/resources/messages/index.js +9 -0
- package/resources/messages/index.js.map +1 -0
- package/resources/messages/index.mjs +4 -0
- package/resources/messages/index.mjs.map +1 -0
- package/resources/messages/messages.d.mts +2285 -0
- package/resources/messages/messages.d.mts.map +1 -0
- package/resources/messages/messages.d.ts +2285 -0
- package/resources/messages/messages.d.ts.map +1 -0
- package/resources/messages/messages.js +128 -0
- package/resources/messages/messages.js.map +1 -0
- package/resources/messages/messages.mjs +123 -0
- package/resources/messages/messages.mjs.map +1 -0
- package/resources/messages.d.mts +2 -0
- package/resources/messages.d.mts.map +1 -0
- package/resources/messages.d.ts +2 -0
- package/resources/messages.d.ts.map +1 -0
- package/resources/messages.js +6 -0
- package/resources/messages.js.map +1 -0
- package/resources/messages.mjs +3 -0
- package/resources/messages.mjs.map +1 -0
- package/resources/models.d.mts +193 -0
- package/resources/models.d.mts.map +1 -0
- package/resources/models.d.ts +193 -0
- package/resources/models.d.ts.map +1 -0
- package/resources/models.js +45 -0
- package/resources/models.js.map +1 -0
- package/resources/models.mjs +41 -0
- package/resources/models.mjs.map +1 -0
- package/resources/shared.d.mts +44 -0
- package/resources/shared.d.mts.map +1 -0
- package/resources/shared.d.ts +44 -0
- package/resources/shared.d.ts.map +1 -0
- package/resources/shared.js +4 -0
- package/resources/shared.js.map +1 -0
- package/resources/shared.mjs +3 -0
- package/resources/shared.mjs.map +1 -0
- package/resources/top-level.d.mts +2 -0
- package/resources/top-level.d.mts.map +1 -0
- package/resources/top-level.d.ts +2 -0
- package/resources/top-level.d.ts.map +1 -0
- package/resources/top-level.js +4 -0
- package/resources/top-level.js.map +1 -0
- package/resources/top-level.mjs +3 -0
- package/resources/top-level.mjs.map +1 -0
- package/resources.d.mts +2 -0
- package/resources.d.mts.map +1 -0
- package/resources.d.ts +2 -0
- package/resources.d.ts.map +1 -0
- package/resources.js +5 -0
- package/resources.js.map +1 -0
- package/resources.mjs +2 -0
- package/resources.mjs.map +1 -0
- package/src/_vendor/partial-json-parser/README.md +3 -0
- package/src/_vendor/partial-json-parser/parser.ts +264 -0
- package/src/ainative.ts +85 -0
- package/src/api-promise.ts +2 -0
- package/src/client.ts +1326 -0
- package/src/core/README.md +3 -0
- package/src/core/api-promise.ts +101 -0
- package/src/core/error.ts +145 -0
- package/src/core/pagination.ts +331 -0
- package/src/core/resource.ts +11 -0
- package/src/core/streaming.ts +348 -0
- package/src/core/uploads.ts +2 -0
- package/src/error.ts +2 -0
- package/src/helpers/beta/json-schema.ts +75 -0
- package/src/helpers/beta/mcp.ts +624 -0
- package/src/helpers/beta/memory.ts +28 -0
- package/src/helpers/beta/zod.ts +72 -0
- package/src/helpers/index.ts +2 -0
- package/src/helpers/json-schema.ts +48 -0
- package/src/helpers/zod.ts +58 -0
- package/src/index.ts +40 -0
- package/src/internal/README.md +3 -0
- package/src/internal/builtin-types.ts +93 -0
- package/src/internal/constants.ts +15 -0
- package/src/internal/decoders/jsonl.ts +48 -0
- package/src/internal/decoders/line.ts +135 -0
- package/src/internal/detect-platform.ts +196 -0
- package/src/internal/errors.ts +33 -0
- package/src/internal/headers.ts +99 -0
- package/src/internal/parse.ts +90 -0
- package/src/internal/request-options.ts +93 -0
- package/src/internal/shim-types.ts +26 -0
- package/src/internal/shims.ts +107 -0
- package/src/internal/stream-utils.ts +32 -0
- package/src/internal/to-file.ts +159 -0
- package/src/internal/types.ts +95 -0
- package/src/internal/uploads.ts +204 -0
- package/src/internal/utils/base64.ts +40 -0
- package/src/internal/utils/bytes.ts +32 -0
- package/src/internal/utils/env.ts +18 -0
- package/src/internal/utils/log.ts +127 -0
- package/src/internal/utils/path.ts +88 -0
- package/src/internal/utils/query.ts +23 -0
- package/src/internal/utils/sleep.ts +3 -0
- package/src/internal/utils/uuid.ts +17 -0
- package/src/internal/utils/values.ts +112 -0
- package/src/internal/utils.ts +9 -0
- package/src/lib/.keep +4 -0
- package/src/lib/BetaMessageStream.ts +764 -0
- package/src/lib/MessageStream.ts +743 -0
- package/src/lib/beta-parser.ts +148 -0
- package/src/lib/parser.ts +125 -0
- package/src/lib/stainless-helper-header.ts +80 -0
- package/src/lib/tools/BetaRunnableTool.ts +40 -0
- package/src/lib/tools/BetaToolRunner.ts +494 -0
- package/src/lib/tools/CompactionControl.ts +57 -0
- package/src/lib/tools/ToolError.ts +47 -0
- package/src/lib/tools/ToolRunner.ts +381 -0
- package/src/lib/transform-json-schema.ts +124 -0
- package/src/pagination.ts +2 -0
- package/src/resource.ts +2 -0
- package/src/resources/beta/beta.ts +603 -0
- package/src/resources/beta/files.ts +261 -0
- package/src/resources/beta/index.ts +259 -0
- package/src/resources/beta/messages/batches.ts +827 -0
- package/src/resources/beta/messages/index.ts +229 -0
- package/src/resources/beta/messages/messages.ts +4372 -0
- package/src/resources/beta/messages.ts +3 -0
- package/src/resources/beta/models.ts +284 -0
- package/src/resources/beta/skills/index.ts +26 -0
- package/src/resources/beta/skills/skills.ts +384 -0
- package/src/resources/beta/skills/versions.ts +374 -0
- package/src/resources/beta/skills.ts +3 -0
- package/src/resources/beta.ts +3 -0
- package/src/resources/completions.ts +230 -0
- package/src/resources/index.ts +206 -0
- package/src/resources/messages/batches.ts +396 -0
- package/src/resources/messages/index.ts +189 -0
- package/src/resources/messages/messages.ts +3361 -0
- package/src/resources/messages.ts +3 -0
- package/src/resources/models.ts +269 -0
- package/src/resources/shared.ts +85 -0
- package/src/resources/top-level.ts +3 -0
- package/src/resources.ts +1 -0
- package/src/streaming.ts +2 -0
- package/src/tools/memory/node.ts +392 -0
- package/src/tsconfig.json +11 -0
- package/src/uploads.ts +2 -0
- package/src/version.ts +1 -0
- package/streaming.d.mts +2 -0
- package/streaming.d.mts.map +1 -0
- package/streaming.d.ts +2 -0
- package/streaming.d.ts.map +1 -0
- package/streaming.js +6 -0
- package/streaming.js.map +1 -0
- package/streaming.mjs +2 -0
- package/streaming.mjs.map +1 -0
- package/tools/memory/node.d.mts +18 -0
- package/tools/memory/node.d.mts.map +1 -0
- package/tools/memory/node.d.ts +18 -0
- package/tools/memory/node.d.ts.map +1 -0
- package/tools/memory/node.js +324 -0
- package/tools/memory/node.js.map +1 -0
- package/tools/memory/node.mjs +318 -0
- package/tools/memory/node.mjs.map +1 -0
- package/uploads.d.mts +2 -0
- package/uploads.d.mts.map +1 -0
- package/uploads.d.ts +2 -0
- package/uploads.d.ts.map +1 -0
- package/uploads.js +6 -0
- package/uploads.js.map +1 -0
- package/uploads.mjs +2 -0
- package/uploads.mjs.map +1 -0
- package/version.d.mts +2 -0
- package/version.d.mts.map +1 -0
- package/version.d.ts +2 -0
- package/version.d.ts.map +1 -0
- package/version.js +5 -0
- package/version.js.map +1 -0
- package/version.mjs +2 -0
- package/version.mjs.map +1 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BetaLocalFilesystemMemoryTool = exports.betaMemoryTool = void 0;
|
|
4
|
+
const tslib_1 = require("../../internal/tslib.js");
|
|
5
|
+
var memory_1 = require("../../helpers/beta/memory.js");
|
|
6
|
+
Object.defineProperty(exports, "betaMemoryTool", { enumerable: true, get: function () { return memory_1.betaMemoryTool; } });
|
|
7
|
+
const fs = tslib_1.__importStar(require("fs/promises"));
|
|
8
|
+
const path = tslib_1.__importStar(require("path"));
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
10
|
+
async function exists(path) {
|
|
11
|
+
return await fs
|
|
12
|
+
.access(path)
|
|
13
|
+
.then(() => true)
|
|
14
|
+
.catch((err) => {
|
|
15
|
+
if (err.code === 'ENOENT')
|
|
16
|
+
return false;
|
|
17
|
+
throw err;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Atomically writes content to a file by writing to a temporary file first and then renaming it.
|
|
22
|
+
* This ensures the target file is never in a partially written state, preventing data corruption
|
|
23
|
+
* if the process crashes or is interrupted during the write operation. The rename operation is
|
|
24
|
+
* atomic on most file systems, guaranteeing that readers will only ever see the complete old
|
|
25
|
+
* content or the complete new content, never a mix or partial state.
|
|
26
|
+
*
|
|
27
|
+
* @param targetPath - The path where the file should be written
|
|
28
|
+
* @param content - The content to write to the file
|
|
29
|
+
*/
|
|
30
|
+
async function atomicWriteFile(targetPath, content) {
|
|
31
|
+
const dir = path.dirname(targetPath);
|
|
32
|
+
const tempPath = path.join(dir, `.tmp-${process.pid}-${(0, crypto_1.randomUUID)()}`);
|
|
33
|
+
let handle;
|
|
34
|
+
try {
|
|
35
|
+
handle = await fs.open(tempPath, 'wx');
|
|
36
|
+
await handle.writeFile(content, 'utf-8');
|
|
37
|
+
await handle.sync();
|
|
38
|
+
await handle.close();
|
|
39
|
+
handle = undefined;
|
|
40
|
+
await fs.rename(tempPath, targetPath);
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
if (handle) {
|
|
44
|
+
await handle.close().catch(() => { });
|
|
45
|
+
}
|
|
46
|
+
await fs.unlink(tempPath).catch(() => { });
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Validates that a target path doesn't escape the memory root via symlinks.
|
|
52
|
+
*
|
|
53
|
+
* Prevents symlink attacks where a malicious symlink inside /memories points
|
|
54
|
+
* outside (e.g., /memories/foo -> /etc), which would allow operations like
|
|
55
|
+
* creating /memories/foo/passwd to actually write to /etc/passwd.
|
|
56
|
+
*
|
|
57
|
+
* Walks up from the target path to find the deepest existing ancestor,
|
|
58
|
+
* then resolves it to ensure the real path stays within memoryRoot.
|
|
59
|
+
*/
|
|
60
|
+
async function validateNoSymlinkEscape(targetPath, memoryRoot) {
|
|
61
|
+
const resolvedRoot = await fs.realpath(memoryRoot);
|
|
62
|
+
let current = targetPath;
|
|
63
|
+
while (true) {
|
|
64
|
+
try {
|
|
65
|
+
const resolved = await fs.realpath(current);
|
|
66
|
+
if (resolved !== resolvedRoot && !resolved.startsWith(resolvedRoot + path.sep)) {
|
|
67
|
+
throw new Error(`Path would escape /memories directory via symlink`);
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
if (err.code !== 'ENOENT')
|
|
73
|
+
throw err;
|
|
74
|
+
const parent = path.dirname(current);
|
|
75
|
+
if (parent === current || current === memoryRoot) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
current = parent;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function readFileContent(fullPath, memoryPath) {
|
|
83
|
+
try {
|
|
84
|
+
return await fs.readFile(fullPath, 'utf-8');
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
if (err.code === 'ENOENT') {
|
|
88
|
+
throw new Error(`The file ${memoryPath} no longer exists (may have been deleted or renamed concurrently).`);
|
|
89
|
+
}
|
|
90
|
+
throw err;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function formatFileSize(bytes) {
|
|
94
|
+
if (bytes === 0)
|
|
95
|
+
return '0B';
|
|
96
|
+
const k = 1024;
|
|
97
|
+
const sizes = ['B', 'K', 'M', 'G'];
|
|
98
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
99
|
+
const size = bytes / Math.pow(k, i);
|
|
100
|
+
return (size % 1 === 0 ? size.toString() : size.toFixed(1)) + sizes[i];
|
|
101
|
+
}
|
|
102
|
+
const MAX_LINES = 999999;
|
|
103
|
+
const LINE_NUMBER_WIDTH = String(MAX_LINES).length;
|
|
104
|
+
class BetaLocalFilesystemMemoryTool {
|
|
105
|
+
constructor(basePath = './memory') {
|
|
106
|
+
this.basePath = basePath;
|
|
107
|
+
this.memoryRoot = path.join(this.basePath, 'memories');
|
|
108
|
+
}
|
|
109
|
+
static async init(basePath = './memory') {
|
|
110
|
+
const memory = new BetaLocalFilesystemMemoryTool(basePath);
|
|
111
|
+
await fs.mkdir(memory.memoryRoot, { recursive: true });
|
|
112
|
+
return memory;
|
|
113
|
+
}
|
|
114
|
+
async validatePath(memoryPath) {
|
|
115
|
+
if (!memoryPath.startsWith('/memories')) {
|
|
116
|
+
throw new Error(`Path must start with /memories, got: ${memoryPath}`);
|
|
117
|
+
}
|
|
118
|
+
const relativePath = memoryPath.slice('/memories'.length).replace(/^\//, '');
|
|
119
|
+
const fullPath = relativePath ? path.join(this.memoryRoot, relativePath) : this.memoryRoot;
|
|
120
|
+
const resolvedPath = path.resolve(fullPath);
|
|
121
|
+
const resolvedRoot = path.resolve(this.memoryRoot);
|
|
122
|
+
if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(resolvedRoot + path.sep)) {
|
|
123
|
+
throw new Error(`Path ${memoryPath} would escape /memories directory`);
|
|
124
|
+
}
|
|
125
|
+
await validateNoSymlinkEscape(resolvedPath, this.memoryRoot);
|
|
126
|
+
return resolvedPath;
|
|
127
|
+
}
|
|
128
|
+
async view(command) {
|
|
129
|
+
const fullPath = await this.validatePath(command.path);
|
|
130
|
+
let stat;
|
|
131
|
+
try {
|
|
132
|
+
stat = await fs.stat(fullPath);
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
if (err.code === 'ENOENT') {
|
|
136
|
+
throw new Error(`The path ${command.path} does not exist. Please provide a valid path.`);
|
|
137
|
+
}
|
|
138
|
+
throw err;
|
|
139
|
+
}
|
|
140
|
+
if (stat.isDirectory()) {
|
|
141
|
+
const items = [];
|
|
142
|
+
const collectItems = async (dirPath, relativePath, depth) => {
|
|
143
|
+
if (depth > 2)
|
|
144
|
+
return;
|
|
145
|
+
const dirContents = await fs.readdir(dirPath);
|
|
146
|
+
for (const item of dirContents.sort()) {
|
|
147
|
+
if (item.startsWith('.') || item === 'node_modules') {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const itemPath = path.join(dirPath, item);
|
|
151
|
+
const itemRelativePath = relativePath ? `${relativePath}/${item}` : item;
|
|
152
|
+
let itemStat;
|
|
153
|
+
try {
|
|
154
|
+
itemStat = await fs.stat(itemPath);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (itemStat.isDirectory()) {
|
|
160
|
+
items.push({ size: formatFileSize(itemStat.size), path: `${itemRelativePath}/` });
|
|
161
|
+
if (depth < 2) {
|
|
162
|
+
await collectItems(itemPath, itemRelativePath, depth + 1);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else if (itemStat.isFile()) {
|
|
166
|
+
items.push({ size: formatFileSize(itemStat.size), path: itemRelativePath });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
await collectItems(fullPath, '', 1);
|
|
171
|
+
const header = `Here're the files and directories up to 2 levels deep in ${command.path}, excluding hidden items and node_modules:`;
|
|
172
|
+
const dirSize = formatFileSize(stat.size);
|
|
173
|
+
const lines = [
|
|
174
|
+
`${dirSize}\t${command.path}`,
|
|
175
|
+
...items.map((item) => `${item.size}\t${command.path}/${item.path}`),
|
|
176
|
+
];
|
|
177
|
+
return `${header}\n${lines.join('\n')}`;
|
|
178
|
+
}
|
|
179
|
+
else if (stat.isFile()) {
|
|
180
|
+
const content = await readFileContent(fullPath, command.path);
|
|
181
|
+
const lines = content.split('\n');
|
|
182
|
+
if (lines.length > MAX_LINES) {
|
|
183
|
+
throw new Error(`File ${command.path} has too many lines (${lines.length}). Maximum is ${MAX_LINES.toLocaleString()} lines.`);
|
|
184
|
+
}
|
|
185
|
+
let displayLines = lines;
|
|
186
|
+
let startNum = 1;
|
|
187
|
+
if (command.view_range && command.view_range.length === 2) {
|
|
188
|
+
const startLine = Math.max(1, command.view_range[0]) - 1;
|
|
189
|
+
const endLine = command.view_range[1] === -1 ? lines.length : command.view_range[1];
|
|
190
|
+
displayLines = lines.slice(startLine, endLine);
|
|
191
|
+
startNum = startLine + 1;
|
|
192
|
+
}
|
|
193
|
+
const numberedLines = displayLines.map((line, i) => `${String(i + startNum).padStart(LINE_NUMBER_WIDTH, ' ')}\t${line}`);
|
|
194
|
+
return `Here's the content of ${command.path} with line numbers:\n${numberedLines.join('\n')}`;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
throw new Error(`Unsupported file type for ${command.path}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
async create(command) {
|
|
201
|
+
const fullPath = await this.validatePath(command.path);
|
|
202
|
+
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
|
203
|
+
let handle;
|
|
204
|
+
try {
|
|
205
|
+
handle = await fs.open(fullPath, 'wx');
|
|
206
|
+
await handle.writeFile(command.file_text, 'utf-8');
|
|
207
|
+
await handle.sync();
|
|
208
|
+
}
|
|
209
|
+
catch (err) {
|
|
210
|
+
if (err?.code === 'EEXIST') {
|
|
211
|
+
throw new Error(`File ${command.path} already exists`);
|
|
212
|
+
}
|
|
213
|
+
throw err;
|
|
214
|
+
}
|
|
215
|
+
finally {
|
|
216
|
+
await handle?.close().catch(() => { });
|
|
217
|
+
}
|
|
218
|
+
return `File created successfully at: ${command.path}`;
|
|
219
|
+
}
|
|
220
|
+
async str_replace(command) {
|
|
221
|
+
const fullPath = await this.validatePath(command.path);
|
|
222
|
+
let stat;
|
|
223
|
+
try {
|
|
224
|
+
stat = await fs.stat(fullPath);
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
if (err.code === 'ENOENT') {
|
|
228
|
+
throw new Error(`The path ${command.path} does not exist. Please provide a valid path.`);
|
|
229
|
+
}
|
|
230
|
+
throw err;
|
|
231
|
+
}
|
|
232
|
+
if (!stat.isFile()) {
|
|
233
|
+
throw new Error(`The path ${command.path} is not a file.`);
|
|
234
|
+
}
|
|
235
|
+
const content = await readFileContent(fullPath, command.path);
|
|
236
|
+
const lines = content.split('\n');
|
|
237
|
+
const matchingLines = [];
|
|
238
|
+
lines.forEach((line, index) => {
|
|
239
|
+
if (line.includes(command.old_str)) {
|
|
240
|
+
matchingLines.push(index + 1);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
if (matchingLines.length === 0) {
|
|
244
|
+
throw new Error(`No replacement was performed, old_str \`${command.old_str}\` did not appear verbatim in ${command.path}.`);
|
|
245
|
+
}
|
|
246
|
+
else if (matchingLines.length > 1) {
|
|
247
|
+
throw new Error(`No replacement was performed. Multiple occurrences of old_str \`${command.old_str}\` in lines: ${matchingLines.join(', ')}. Please ensure it is unique`);
|
|
248
|
+
}
|
|
249
|
+
const newContent = content.replace(command.old_str, command.new_str);
|
|
250
|
+
await atomicWriteFile(fullPath, newContent);
|
|
251
|
+
const newLines = newContent.split('\n');
|
|
252
|
+
const changedLineIndex = matchingLines[0] - 1;
|
|
253
|
+
const contextStart = Math.max(0, changedLineIndex - 2);
|
|
254
|
+
const contextEnd = Math.min(newLines.length, changedLineIndex + 3);
|
|
255
|
+
const snippet = newLines.slice(contextStart, contextEnd).map((line, i) => {
|
|
256
|
+
const lineNum = contextStart + i + 1;
|
|
257
|
+
return `${String(lineNum).padStart(LINE_NUMBER_WIDTH, ' ')}\t${line}`;
|
|
258
|
+
});
|
|
259
|
+
return `The memory file has been edited. Here is the snippet showing the change (with line numbers):\n${snippet.join('\n')}`;
|
|
260
|
+
}
|
|
261
|
+
async insert(command) {
|
|
262
|
+
const fullPath = await this.validatePath(command.path);
|
|
263
|
+
let stat;
|
|
264
|
+
try {
|
|
265
|
+
stat = await fs.stat(fullPath);
|
|
266
|
+
}
|
|
267
|
+
catch (err) {
|
|
268
|
+
if (err.code === 'ENOENT') {
|
|
269
|
+
throw new Error(`The path ${command.path} does not exist. Please provide a valid path.`);
|
|
270
|
+
}
|
|
271
|
+
throw err;
|
|
272
|
+
}
|
|
273
|
+
if (!stat.isFile()) {
|
|
274
|
+
throw new Error(`The path ${command.path} is not a file.`);
|
|
275
|
+
}
|
|
276
|
+
const content = await readFileContent(fullPath, command.path);
|
|
277
|
+
const lines = content.split('\n');
|
|
278
|
+
if (command.insert_line < 0 || command.insert_line > lines.length) {
|
|
279
|
+
throw new Error(`Invalid \`insert_line\` parameter: ${command.insert_line}. It should be within the range of lines of the file: [0, ${lines.length}]`);
|
|
280
|
+
}
|
|
281
|
+
lines.splice(command.insert_line, 0, command.insert_text.replace(/\n$/, ''));
|
|
282
|
+
await atomicWriteFile(fullPath, lines.join('\n'));
|
|
283
|
+
return `The file ${command.path} has been edited.`;
|
|
284
|
+
}
|
|
285
|
+
async delete(command) {
|
|
286
|
+
const fullPath = await this.validatePath(command.path);
|
|
287
|
+
if (command.path === '/memories') {
|
|
288
|
+
throw new Error('Cannot delete the /memories directory itself');
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
await fs.rm(fullPath, { recursive: true, force: false });
|
|
292
|
+
}
|
|
293
|
+
catch (err) {
|
|
294
|
+
if (err.code === 'ENOENT') {
|
|
295
|
+
throw new Error(`The path ${command.path} does not exist`);
|
|
296
|
+
}
|
|
297
|
+
throw err;
|
|
298
|
+
}
|
|
299
|
+
return `Successfully deleted ${command.path}`;
|
|
300
|
+
}
|
|
301
|
+
async rename(command) {
|
|
302
|
+
const oldFullPath = await this.validatePath(command.old_path);
|
|
303
|
+
const newFullPath = await this.validatePath(command.new_path);
|
|
304
|
+
// POSIX rename() silently overwrites existing files without error,
|
|
305
|
+
// so we can't catch this atomically. Best-effort check to warn user.
|
|
306
|
+
if (await exists(newFullPath)) {
|
|
307
|
+
throw new Error(`The destination ${command.new_path} already exists`);
|
|
308
|
+
}
|
|
309
|
+
const newDir = path.dirname(newFullPath);
|
|
310
|
+
await fs.mkdir(newDir, { recursive: true });
|
|
311
|
+
try {
|
|
312
|
+
await fs.rename(oldFullPath, newFullPath);
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
if (err.code === 'ENOENT') {
|
|
316
|
+
throw new Error(`The path ${command.old_path} does not exist`);
|
|
317
|
+
}
|
|
318
|
+
throw err;
|
|
319
|
+
}
|
|
320
|
+
return `Successfully renamed ${command.old_path} to ${command.new_path}`;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
exports.BetaLocalFilesystemMemoryTool = BetaLocalFilesystemMemoryTool;
|
|
324
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../../src/tools/memory/node.ts"],"names":[],"mappings":";;;;AAAA,uDAA2D;AAAlD,wGAAA,cAAc,OAAA;AAGvB,wDAAkC;AAClC,mDAA6B;AAC7B,mCAAoC;AAWpC,KAAK,UAAU,MAAM,CAAC,IAAY;IAChC,OAAO,MAAM,EAAE;SACZ,MAAM,CAAC,IAAI,CAAC;SACZ,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC;SAChB,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QACb,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO,KAAK,CAAC;QACxC,MAAM,GAAG,CAAC;IACZ,CAAC,CAAC,CAAC;AACP,CAAC;AAED;;;;;;;;;GASG;AACH,KAAK,UAAU,eAAe,CAAC,UAAkB,EAAE,OAAe;IAChE,MAAM,GAAG,GAAG,IAAI,CAAC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ACH,MAAM,EAAE,CAAC,MAAM,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;QAC5C,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC1B,MAAM,IAAI,KAAK,CAAC,YAAY,OAAO,CAAC,QAAQ,iBAAiB,CAAC,CAAC;YACjE,CAAC;YACD,MAAM,GAAG,CAAC;QACZ,CAAC;QAED,OAAO,wBAAwB,OAAO,CAAC,QAAQ,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAC;IAC3E,CAAC;CACF;AAnRD,sEAmRC"}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
export { betaMemoryTool } from "../../helpers/beta/memory.mjs";
|
|
2
|
+
import * as fs from 'fs/promises';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { randomUUID } from 'crypto';
|
|
5
|
+
async function exists(path) {
|
|
6
|
+
return await fs
|
|
7
|
+
.access(path)
|
|
8
|
+
.then(() => true)
|
|
9
|
+
.catch((err) => {
|
|
10
|
+
if (err.code === 'ENOENT')
|
|
11
|
+
return false;
|
|
12
|
+
throw err;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Atomically writes content to a file by writing to a temporary file first and then renaming it.
|
|
17
|
+
* This ensures the target file is never in a partially written state, preventing data corruption
|
|
18
|
+
* if the process crashes or is interrupted during the write operation. The rename operation is
|
|
19
|
+
* atomic on most file systems, guaranteeing that readers will only ever see the complete old
|
|
20
|
+
* content or the complete new content, never a mix or partial state.
|
|
21
|
+
*
|
|
22
|
+
* @param targetPath - The path where the file should be written
|
|
23
|
+
* @param content - The content to write to the file
|
|
24
|
+
*/
|
|
25
|
+
async function atomicWriteFile(targetPath, content) {
|
|
26
|
+
const dir = path.dirname(targetPath);
|
|
27
|
+
const tempPath = path.join(dir, `.tmp-${process.pid}-${randomUUID()}`);
|
|
28
|
+
let handle;
|
|
29
|
+
try {
|
|
30
|
+
handle = await fs.open(tempPath, 'wx');
|
|
31
|
+
await handle.writeFile(content, 'utf-8');
|
|
32
|
+
await handle.sync();
|
|
33
|
+
await handle.close();
|
|
34
|
+
handle = undefined;
|
|
35
|
+
await fs.rename(tempPath, targetPath);
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
if (handle) {
|
|
39
|
+
await handle.close().catch(() => { });
|
|
40
|
+
}
|
|
41
|
+
await fs.unlink(tempPath).catch(() => { });
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Validates that a target path doesn't escape the memory root via symlinks.
|
|
47
|
+
*
|
|
48
|
+
* Prevents symlink attacks where a malicious symlink inside /memories points
|
|
49
|
+
* outside (e.g., /memories/foo -> /etc), which would allow operations like
|
|
50
|
+
* creating /memories/foo/passwd to actually write to /etc/passwd.
|
|
51
|
+
*
|
|
52
|
+
* Walks up from the target path to find the deepest existing ancestor,
|
|
53
|
+
* then resolves it to ensure the real path stays within memoryRoot.
|
|
54
|
+
*/
|
|
55
|
+
async function validateNoSymlinkEscape(targetPath, memoryRoot) {
|
|
56
|
+
const resolvedRoot = await fs.realpath(memoryRoot);
|
|
57
|
+
let current = targetPath;
|
|
58
|
+
while (true) {
|
|
59
|
+
try {
|
|
60
|
+
const resolved = await fs.realpath(current);
|
|
61
|
+
if (resolved !== resolvedRoot && !resolved.startsWith(resolvedRoot + path.sep)) {
|
|
62
|
+
throw new Error(`Path would escape /memories directory via symlink`);
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
if (err.code !== 'ENOENT')
|
|
68
|
+
throw err;
|
|
69
|
+
const parent = path.dirname(current);
|
|
70
|
+
if (parent === current || current === memoryRoot) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
current = parent;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async function readFileContent(fullPath, memoryPath) {
|
|
78
|
+
try {
|
|
79
|
+
return await fs.readFile(fullPath, 'utf-8');
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
if (err.code === 'ENOENT') {
|
|
83
|
+
throw new Error(`The file ${memoryPath} no longer exists (may have been deleted or renamed concurrently).`);
|
|
84
|
+
}
|
|
85
|
+
throw err;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function formatFileSize(bytes) {
|
|
89
|
+
if (bytes === 0)
|
|
90
|
+
return '0B';
|
|
91
|
+
const k = 1024;
|
|
92
|
+
const sizes = ['B', 'K', 'M', 'G'];
|
|
93
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
94
|
+
const size = bytes / Math.pow(k, i);
|
|
95
|
+
return (size % 1 === 0 ? size.toString() : size.toFixed(1)) + sizes[i];
|
|
96
|
+
}
|
|
97
|
+
const MAX_LINES = 999999;
|
|
98
|
+
const LINE_NUMBER_WIDTH = String(MAX_LINES).length;
|
|
99
|
+
export class BetaLocalFilesystemMemoryTool {
|
|
100
|
+
constructor(basePath = './memory') {
|
|
101
|
+
this.basePath = basePath;
|
|
102
|
+
this.memoryRoot = path.join(this.basePath, 'memories');
|
|
103
|
+
}
|
|
104
|
+
static async init(basePath = './memory') {
|
|
105
|
+
const memory = new BetaLocalFilesystemMemoryTool(basePath);
|
|
106
|
+
await fs.mkdir(memory.memoryRoot, { recursive: true });
|
|
107
|
+
return memory;
|
|
108
|
+
}
|
|
109
|
+
async validatePath(memoryPath) {
|
|
110
|
+
if (!memoryPath.startsWith('/memories')) {
|
|
111
|
+
throw new Error(`Path must start with /memories, got: ${memoryPath}`);
|
|
112
|
+
}
|
|
113
|
+
const relativePath = memoryPath.slice('/memories'.length).replace(/^\//, '');
|
|
114
|
+
const fullPath = relativePath ? path.join(this.memoryRoot, relativePath) : this.memoryRoot;
|
|
115
|
+
const resolvedPath = path.resolve(fullPath);
|
|
116
|
+
const resolvedRoot = path.resolve(this.memoryRoot);
|
|
117
|
+
if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(resolvedRoot + path.sep)) {
|
|
118
|
+
throw new Error(`Path ${memoryPath} would escape /memories directory`);
|
|
119
|
+
}
|
|
120
|
+
await validateNoSymlinkEscape(resolvedPath, this.memoryRoot);
|
|
121
|
+
return resolvedPath;
|
|
122
|
+
}
|
|
123
|
+
async view(command) {
|
|
124
|
+
const fullPath = await this.validatePath(command.path);
|
|
125
|
+
let stat;
|
|
126
|
+
try {
|
|
127
|
+
stat = await fs.stat(fullPath);
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
if (err.code === 'ENOENT') {
|
|
131
|
+
throw new Error(`The path ${command.path} does not exist. Please provide a valid path.`);
|
|
132
|
+
}
|
|
133
|
+
throw err;
|
|
134
|
+
}
|
|
135
|
+
if (stat.isDirectory()) {
|
|
136
|
+
const items = [];
|
|
137
|
+
const collectItems = async (dirPath, relativePath, depth) => {
|
|
138
|
+
if (depth > 2)
|
|
139
|
+
return;
|
|
140
|
+
const dirContents = await fs.readdir(dirPath);
|
|
141
|
+
for (const item of dirContents.sort()) {
|
|
142
|
+
if (item.startsWith('.') || item === 'node_modules') {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const itemPath = path.join(dirPath, item);
|
|
146
|
+
const itemRelativePath = relativePath ? `${relativePath}/${item}` : item;
|
|
147
|
+
let itemStat;
|
|
148
|
+
try {
|
|
149
|
+
itemStat = await fs.stat(itemPath);
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (itemStat.isDirectory()) {
|
|
155
|
+
items.push({ size: formatFileSize(itemStat.size), path: `${itemRelativePath}/` });
|
|
156
|
+
if (depth < 2) {
|
|
157
|
+
await collectItems(itemPath, itemRelativePath, depth + 1);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else if (itemStat.isFile()) {
|
|
161
|
+
items.push({ size: formatFileSize(itemStat.size), path: itemRelativePath });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
await collectItems(fullPath, '', 1);
|
|
166
|
+
const header = `Here're the files and directories up to 2 levels deep in ${command.path}, excluding hidden items and node_modules:`;
|
|
167
|
+
const dirSize = formatFileSize(stat.size);
|
|
168
|
+
const lines = [
|
|
169
|
+
`${dirSize}\t${command.path}`,
|
|
170
|
+
...items.map((item) => `${item.size}\t${command.path}/${item.path}`),
|
|
171
|
+
];
|
|
172
|
+
return `${header}\n${lines.join('\n')}`;
|
|
173
|
+
}
|
|
174
|
+
else if (stat.isFile()) {
|
|
175
|
+
const content = await readFileContent(fullPath, command.path);
|
|
176
|
+
const lines = content.split('\n');
|
|
177
|
+
if (lines.length > MAX_LINES) {
|
|
178
|
+
throw new Error(`File ${command.path} has too many lines (${lines.length}). Maximum is ${MAX_LINES.toLocaleString()} lines.`);
|
|
179
|
+
}
|
|
180
|
+
let displayLines = lines;
|
|
181
|
+
let startNum = 1;
|
|
182
|
+
if (command.view_range && command.view_range.length === 2) {
|
|
183
|
+
const startLine = Math.max(1, command.view_range[0]) - 1;
|
|
184
|
+
const endLine = command.view_range[1] === -1 ? lines.length : command.view_range[1];
|
|
185
|
+
displayLines = lines.slice(startLine, endLine);
|
|
186
|
+
startNum = startLine + 1;
|
|
187
|
+
}
|
|
188
|
+
const numberedLines = displayLines.map((line, i) => `${String(i + startNum).padStart(LINE_NUMBER_WIDTH, ' ')}\t${line}`);
|
|
189
|
+
return `Here's the content of ${command.path} with line numbers:\n${numberedLines.join('\n')}`;
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
throw new Error(`Unsupported file type for ${command.path}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async create(command) {
|
|
196
|
+
const fullPath = await this.validatePath(command.path);
|
|
197
|
+
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
|
198
|
+
let handle;
|
|
199
|
+
try {
|
|
200
|
+
handle = await fs.open(fullPath, 'wx');
|
|
201
|
+
await handle.writeFile(command.file_text, 'utf-8');
|
|
202
|
+
await handle.sync();
|
|
203
|
+
}
|
|
204
|
+
catch (err) {
|
|
205
|
+
if (err?.code === 'EEXIST') {
|
|
206
|
+
throw new Error(`File ${command.path} already exists`);
|
|
207
|
+
}
|
|
208
|
+
throw err;
|
|
209
|
+
}
|
|
210
|
+
finally {
|
|
211
|
+
await handle?.close().catch(() => { });
|
|
212
|
+
}
|
|
213
|
+
return `File created successfully at: ${command.path}`;
|
|
214
|
+
}
|
|
215
|
+
async str_replace(command) {
|
|
216
|
+
const fullPath = await this.validatePath(command.path);
|
|
217
|
+
let stat;
|
|
218
|
+
try {
|
|
219
|
+
stat = await fs.stat(fullPath);
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
if (err.code === 'ENOENT') {
|
|
223
|
+
throw new Error(`The path ${command.path} does not exist. Please provide a valid path.`);
|
|
224
|
+
}
|
|
225
|
+
throw err;
|
|
226
|
+
}
|
|
227
|
+
if (!stat.isFile()) {
|
|
228
|
+
throw new Error(`The path ${command.path} is not a file.`);
|
|
229
|
+
}
|
|
230
|
+
const content = await readFileContent(fullPath, command.path);
|
|
231
|
+
const lines = content.split('\n');
|
|
232
|
+
const matchingLines = [];
|
|
233
|
+
lines.forEach((line, index) => {
|
|
234
|
+
if (line.includes(command.old_str)) {
|
|
235
|
+
matchingLines.push(index + 1);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
if (matchingLines.length === 0) {
|
|
239
|
+
throw new Error(`No replacement was performed, old_str \`${command.old_str}\` did not appear verbatim in ${command.path}.`);
|
|
240
|
+
}
|
|
241
|
+
else if (matchingLines.length > 1) {
|
|
242
|
+
throw new Error(`No replacement was performed. Multiple occurrences of old_str \`${command.old_str}\` in lines: ${matchingLines.join(', ')}. Please ensure it is unique`);
|
|
243
|
+
}
|
|
244
|
+
const newContent = content.replace(command.old_str, command.new_str);
|
|
245
|
+
await atomicWriteFile(fullPath, newContent);
|
|
246
|
+
const newLines = newContent.split('\n');
|
|
247
|
+
const changedLineIndex = matchingLines[0] - 1;
|
|
248
|
+
const contextStart = Math.max(0, changedLineIndex - 2);
|
|
249
|
+
const contextEnd = Math.min(newLines.length, changedLineIndex + 3);
|
|
250
|
+
const snippet = newLines.slice(contextStart, contextEnd).map((line, i) => {
|
|
251
|
+
const lineNum = contextStart + i + 1;
|
|
252
|
+
return `${String(lineNum).padStart(LINE_NUMBER_WIDTH, ' ')}\t${line}`;
|
|
253
|
+
});
|
|
254
|
+
return `The memory file has been edited. Here is the snippet showing the change (with line numbers):\n${snippet.join('\n')}`;
|
|
255
|
+
}
|
|
256
|
+
async insert(command) {
|
|
257
|
+
const fullPath = await this.validatePath(command.path);
|
|
258
|
+
let stat;
|
|
259
|
+
try {
|
|
260
|
+
stat = await fs.stat(fullPath);
|
|
261
|
+
}
|
|
262
|
+
catch (err) {
|
|
263
|
+
if (err.code === 'ENOENT') {
|
|
264
|
+
throw new Error(`The path ${command.path} does not exist. Please provide a valid path.`);
|
|
265
|
+
}
|
|
266
|
+
throw err;
|
|
267
|
+
}
|
|
268
|
+
if (!stat.isFile()) {
|
|
269
|
+
throw new Error(`The path ${command.path} is not a file.`);
|
|
270
|
+
}
|
|
271
|
+
const content = await readFileContent(fullPath, command.path);
|
|
272
|
+
const lines = content.split('\n');
|
|
273
|
+
if (command.insert_line < 0 || command.insert_line > lines.length) {
|
|
274
|
+
throw new Error(`Invalid \`insert_line\` parameter: ${command.insert_line}. It should be within the range of lines of the file: [0, ${lines.length}]`);
|
|
275
|
+
}
|
|
276
|
+
lines.splice(command.insert_line, 0, command.insert_text.replace(/\n$/, ''));
|
|
277
|
+
await atomicWriteFile(fullPath, lines.join('\n'));
|
|
278
|
+
return `The file ${command.path} has been edited.`;
|
|
279
|
+
}
|
|
280
|
+
async delete(command) {
|
|
281
|
+
const fullPath = await this.validatePath(command.path);
|
|
282
|
+
if (command.path === '/memories') {
|
|
283
|
+
throw new Error('Cannot delete the /memories directory itself');
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
await fs.rm(fullPath, { recursive: true, force: false });
|
|
287
|
+
}
|
|
288
|
+
catch (err) {
|
|
289
|
+
if (err.code === 'ENOENT') {
|
|
290
|
+
throw new Error(`The path ${command.path} does not exist`);
|
|
291
|
+
}
|
|
292
|
+
throw err;
|
|
293
|
+
}
|
|
294
|
+
return `Successfully deleted ${command.path}`;
|
|
295
|
+
}
|
|
296
|
+
async rename(command) {
|
|
297
|
+
const oldFullPath = await this.validatePath(command.old_path);
|
|
298
|
+
const newFullPath = await this.validatePath(command.new_path);
|
|
299
|
+
// POSIX rename() silently overwrites existing files without error,
|
|
300
|
+
// so we can't catch this atomically. Best-effort check to warn user.
|
|
301
|
+
if (await exists(newFullPath)) {
|
|
302
|
+
throw new Error(`The destination ${command.new_path} already exists`);
|
|
303
|
+
}
|
|
304
|
+
const newDir = path.dirname(newFullPath);
|
|
305
|
+
await fs.mkdir(newDir, { recursive: true });
|
|
306
|
+
try {
|
|
307
|
+
await fs.rename(oldFullPath, newFullPath);
|
|
308
|
+
}
|
|
309
|
+
catch (err) {
|
|
310
|
+
if (err.code === 'ENOENT') {
|
|
311
|
+
throw new Error(`The path ${command.old_path} does not exist`);
|
|
312
|
+
}
|
|
313
|
+
throw err;
|
|
314
|
+
}
|
|
315
|
+
return `Successfully renamed ${command.old_path} to ${command.new_path}`;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
//# sourceMappingURL=node.mjs.map
|