@diffra-ai/durable 0.0.0-stage → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +440 -0
- package/README.md +1153 -2
- package/dist/codemode/codemode.d.ts +174 -0
- package/dist/codemode/codemode.d.ts.map +1 -0
- package/dist/codemode/codemode.js +884 -0
- package/dist/codemode/codemode.js.map +1 -0
- package/dist/codemode/describe.d.ts +38 -0
- package/dist/codemode/describe.d.ts.map +1 -0
- package/dist/codemode/describe.js +127 -0
- package/dist/codemode/describe.js.map +1 -0
- package/dist/codemode/index.d.ts +3 -0
- package/dist/codemode/index.d.ts.map +1 -0
- package/dist/codemode/index.js +3 -0
- package/dist/codemode/index.js.map +1 -0
- package/dist/codemode/models.d.ts +29 -0
- package/dist/codemode/models.d.ts.map +1 -0
- package/dist/codemode/models.js +242 -0
- package/dist/codemode/models.js.map +1 -0
- package/dist/codemode/output.d.ts +43 -0
- package/dist/codemode/output.d.ts.map +1 -0
- package/dist/codemode/output.js +168 -0
- package/dist/codemode/output.js.map +1 -0
- package/dist/documents.d.ts +66 -0
- package/dist/documents.d.ts.map +1 -0
- package/dist/documents.js +98 -0
- package/dist/documents.js.map +1 -0
- package/dist/entries.d.ts +31 -0
- package/dist/entries.d.ts.map +1 -0
- package/dist/entries.js +31 -0
- package/dist/entries.js.map +1 -0
- package/dist/env/decode.d.ts +16 -0
- package/dist/env/decode.d.ts.map +1 -0
- package/dist/env/decode.js +28 -0
- package/dist/env/decode.js.map +1 -0
- package/dist/env/index.d.ts +308 -0
- package/dist/env/index.d.ts.map +1 -0
- package/dist/env/index.js +49 -0
- package/dist/env/index.js.map +1 -0
- package/dist/env/line-scan.d.ts +13 -0
- package/dist/env/line-scan.d.ts.map +1 -0
- package/dist/env/line-scan.js +147 -0
- package/dist/env/line-scan.js.map +1 -0
- package/dist/env/node-watch.d.ts +29 -0
- package/dist/env/node-watch.d.ts.map +1 -0
- package/dist/env/node-watch.js +487 -0
- package/dist/env/node-watch.js.map +1 -0
- package/dist/env/node.d.ts +56 -0
- package/dist/env/node.d.ts.map +1 -0
- package/dist/env/node.js +1185 -0
- package/dist/env/node.js.map +1 -0
- package/dist/errors.d.ts +48 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +76 -0
- package/dist/errors.js.map +1 -0
- package/dist/harness/agent.d.ts +46 -0
- package/dist/harness/agent.d.ts.map +1 -0
- package/dist/harness/agent.js +334 -0
- package/dist/harness/agent.js.map +1 -0
- package/dist/harness/compaction.d.ts +68 -0
- package/dist/harness/compaction.d.ts.map +1 -0
- package/dist/harness/compaction.js +431 -0
- package/dist/harness/compaction.js.map +1 -0
- package/dist/harness/context.d.ts +84 -0
- package/dist/harness/context.d.ts.map +1 -0
- package/dist/harness/context.js +301 -0
- package/dist/harness/context.js.map +1 -0
- package/dist/harness/define.d.ts +25 -0
- package/dist/harness/define.d.ts.map +1 -0
- package/dist/harness/define.js +32 -0
- package/dist/harness/define.js.map +1 -0
- package/dist/harness/generation.d.ts +117 -0
- package/dist/harness/generation.d.ts.map +1 -0
- package/dist/harness/generation.js +1098 -0
- package/dist/harness/generation.js.map +1 -0
- package/dist/harness/harness.d.ts +22 -0
- package/dist/harness/harness.d.ts.map +1 -0
- package/dist/harness/harness.js +438 -0
- package/dist/harness/harness.js.map +1 -0
- package/dist/harness/inbox.d.ts +84 -0
- package/dist/harness/inbox.d.ts.map +1 -0
- package/dist/harness/inbox.js +135 -0
- package/dist/harness/inbox.js.map +1 -0
- package/dist/harness/json.d.ts +26 -0
- package/dist/harness/json.d.ts.map +1 -0
- package/dist/harness/json.js +51 -0
- package/dist/harness/json.js.map +1 -0
- package/dist/harness/live.d.ts +285 -0
- package/dist/harness/live.d.ts.map +1 -0
- package/dist/harness/live.js +257 -0
- package/dist/harness/live.js.map +1 -0
- package/dist/harness/parks.d.ts +23 -0
- package/dist/harness/parks.d.ts.map +1 -0
- package/dist/harness/parks.js +52 -0
- package/dist/harness/parks.js.map +1 -0
- package/dist/harness/partial-changes.d.ts +54 -0
- package/dist/harness/partial-changes.d.ts.map +1 -0
- package/dist/harness/partial-changes.js +163 -0
- package/dist/harness/partial-changes.js.map +1 -0
- package/dist/harness/prompt.d.ts +27 -0
- package/dist/harness/prompt.d.ts.map +1 -0
- package/dist/harness/prompt.js +131 -0
- package/dist/harness/prompt.js.map +1 -0
- package/dist/harness/provider.d.ts +16 -0
- package/dist/harness/provider.d.ts.map +1 -0
- package/dist/harness/provider.js +33 -0
- package/dist/harness/provider.js.map +1 -0
- package/dist/harness/registry-reader.d.ts +11 -0
- package/dist/harness/registry-reader.d.ts.map +1 -0
- package/dist/harness/registry-reader.js +25 -0
- package/dist/harness/registry-reader.js.map +1 -0
- package/dist/harness/registry.d.ts +6 -0
- package/dist/harness/registry.d.ts.map +1 -0
- package/dist/harness/registry.js +101 -0
- package/dist/harness/registry.js.map +1 -0
- package/dist/harness/scheduler.d.ts +195 -0
- package/dist/harness/scheduler.d.ts.map +1 -0
- package/dist/harness/scheduler.js +3073 -0
- package/dist/harness/scheduler.js.map +1 -0
- package/dist/harness/submissions.d.ts +44 -0
- package/dist/harness/submissions.d.ts.map +1 -0
- package/dist/harness/submissions.js +265 -0
- package/dist/harness/submissions.js.map +1 -0
- package/dist/harness/task-graph.d.ts +75 -0
- package/dist/harness/task-graph.d.ts.map +1 -0
- package/dist/harness/task-graph.js +224 -0
- package/dist/harness/task-graph.js.map +1 -0
- package/dist/harness/telemetry.d.ts +307 -0
- package/dist/harness/telemetry.d.ts.map +1 -0
- package/dist/harness/telemetry.js +140 -0
- package/dist/harness/telemetry.js.map +1 -0
- package/dist/harness/tool.d.ts +106 -0
- package/dist/harness/tool.d.ts.map +1 -0
- package/dist/harness/tool.js +1182 -0
- package/dist/harness/tool.js.map +1 -0
- package/dist/harness/transcript.d.ts +72 -0
- package/dist/harness/transcript.d.ts.map +1 -0
- package/dist/harness/transcript.js +264 -0
- package/dist/harness/transcript.js.map +1 -0
- package/dist/harness/types.d.ts +1146 -0
- package/dist/harness/types.d.ts.map +1 -0
- package/dist/harness/types.js +2 -0
- package/dist/harness/types.js.map +1 -0
- package/dist/harness/usage.d.ts +19 -0
- package/dist/harness/usage.d.ts.map +1 -0
- package/dist/harness/usage.js +57 -0
- package/dist/harness/usage.js.map +1 -0
- package/dist/harness/util.d.ts +25 -0
- package/dist/harness/util.d.ts.map +1 -0
- package/dist/harness/util.js +76 -0
- package/dist/harness/util.js.map +1 -0
- package/dist/harness/view.d.ts +69 -0
- package/dist/harness/view.d.ts.map +1 -0
- package/dist/harness/view.js +265 -0
- package/dist/harness/view.js.map +1 -0
- package/dist/ids.d.ts +6 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +9 -0
- package/dist/ids.js.map +1 -0
- package/dist/images/cloudflare.d.ts +4 -0
- package/dist/images/cloudflare.d.ts.map +1 -0
- package/dist/images/cloudflare.js +9 -0
- package/dist/images/cloudflare.js.map +1 -0
- package/dist/images/exif.d.ts +3 -0
- package/dist/images/exif.d.ts.map +1 -0
- package/dist/images/exif.js +71 -0
- package/dist/images/exif.js.map +1 -0
- package/dist/images/fit.d.ts +41 -0
- package/dist/images/fit.d.ts.map +1 -0
- package/dist/images/fit.js +108 -0
- package/dist/images/fit.js.map +1 -0
- package/dist/images/index.d.ts +18 -0
- package/dist/images/index.d.ts.map +1 -0
- package/dist/images/index.js +125 -0
- package/dist/images/index.js.map +1 -0
- package/dist/images/node-worker.d.ts +19 -0
- package/dist/images/node-worker.d.ts.map +1 -0
- package/dist/images/node-worker.js +17 -0
- package/dist/images/node-worker.js.map +1 -0
- package/dist/images/node.d.ts +21 -0
- package/dist/images/node.d.ts.map +1 -0
- package/dist/images/node.js +121 -0
- package/dist/images/node.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/search.d.ts +29 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +148 -0
- package/dist/search.js.map +1 -0
- package/dist/session/cached-storage.d.ts +72 -0
- package/dist/session/cached-storage.d.ts.map +1 -0
- package/dist/session/cached-storage.js +269 -0
- package/dist/session/cached-storage.js.map +1 -0
- package/dist/session/conversation-tails.d.ts +33 -0
- package/dist/session/conversation-tails.d.ts.map +1 -0
- package/dist/session/conversation-tails.js +101 -0
- package/dist/session/conversation-tails.js.map +1 -0
- package/dist/session/forks.d.ts +10 -0
- package/dist/session/forks.d.ts.map +1 -0
- package/dist/session/forks.js +42 -0
- package/dist/session/forks.js.map +1 -0
- package/dist/session/keys.d.ts +69 -0
- package/dist/session/keys.d.ts.map +1 -0
- package/dist/session/keys.js +161 -0
- package/dist/session/keys.js.map +1 -0
- package/dist/session/session.d.ts +287 -0
- package/dist/session/session.d.ts.map +1 -0
- package/dist/session/session.js +1701 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/transaction.d.ts +205 -0
- package/dist/session/transaction.d.ts.map +1 -0
- package/dist/session/transaction.js +1059 -0
- package/dist/session/transaction.js.map +1 -0
- package/dist/session/writer.d.ts +74 -0
- package/dist/session/writer.d.ts.map +1 -0
- package/dist/session/writer.js +294 -0
- package/dist/session/writer.js.map +1 -0
- package/dist/signals.d.ts +180 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +236 -0
- package/dist/signals.js.map +1 -0
- package/dist/storage/indexeddb.d.ts +69 -0
- package/dist/storage/indexeddb.d.ts.map +1 -0
- package/dist/storage/indexeddb.js +883 -0
- package/dist/storage/indexeddb.js.map +1 -0
- package/dist/storage/jsonl/index.d.ts +2 -0
- package/dist/storage/jsonl/index.d.ts.map +1 -0
- package/dist/storage/jsonl/index.js +2 -0
- package/dist/storage/jsonl/index.js.map +1 -0
- package/dist/storage/jsonl/node.d.ts +6 -0
- package/dist/storage/jsonl/node.d.ts.map +1 -0
- package/dist/storage/jsonl/node.js +8 -0
- package/dist/storage/jsonl/node.js.map +1 -0
- package/dist/storage/jsonl/storage.d.ts +206 -0
- package/dist/storage/jsonl/storage.d.ts.map +1 -0
- package/dist/storage/jsonl/storage.js +799 -0
- package/dist/storage/jsonl/storage.js.map +1 -0
- package/dist/storage/memory.d.ts +68 -0
- package/dist/storage/memory.d.ts.map +1 -0
- package/dist/storage/memory.js +761 -0
- package/dist/storage/memory.js.map +1 -0
- package/dist/storage/scan.d.ts +18 -0
- package/dist/storage/scan.d.ts.map +1 -0
- package/dist/storage/scan.js +29 -0
- package/dist/storage/scan.js.map +1 -0
- package/dist/storage/sqlite/cloudflare.d.ts +9 -0
- package/dist/storage/sqlite/cloudflare.d.ts.map +1 -0
- package/dist/storage/sqlite/cloudflare.js +11 -0
- package/dist/storage/sqlite/cloudflare.js.map +1 -0
- package/dist/storage/sqlite/commit.d.ts +52 -0
- package/dist/storage/sqlite/commit.d.ts.map +1 -0
- package/dist/storage/sqlite/commit.js +371 -0
- package/dist/storage/sqlite/commit.js.map +1 -0
- package/dist/storage/sqlite/database.d.ts +2 -0
- package/dist/storage/sqlite/database.d.ts.map +1 -0
- package/dist/storage/sqlite/database.js +2 -0
- package/dist/storage/sqlite/database.js.map +1 -0
- package/dist/storage/sqlite/index.d.ts +4 -0
- package/dist/storage/sqlite/index.d.ts.map +1 -0
- package/dist/storage/sqlite/index.js +3 -0
- package/dist/storage/sqlite/index.js.map +1 -0
- package/dist/storage/sqlite/migrations.d.ts +12 -0
- package/dist/storage/sqlite/migrations.d.ts.map +1 -0
- package/dist/storage/sqlite/migrations.js +121 -0
- package/dist/storage/sqlite/migrations.js.map +1 -0
- package/dist/storage/sqlite/node.d.ts +8 -0
- package/dist/storage/sqlite/node.d.ts.map +1 -0
- package/dist/storage/sqlite/node.js +12 -0
- package/dist/storage/sqlite/node.js.map +1 -0
- package/dist/storage/sqlite/rows.d.ts +33 -0
- package/dist/storage/sqlite/rows.d.ts.map +1 -0
- package/dist/storage/sqlite/rows.js +53 -0
- package/dist/storage/sqlite/rows.js.map +1 -0
- package/dist/storage/sqlite/schema.d.ts +45 -0
- package/dist/storage/sqlite/schema.d.ts.map +1 -0
- package/dist/storage/sqlite/schema.js +50 -0
- package/dist/storage/sqlite/schema.js.map +1 -0
- package/dist/storage/sqlite/statements.d.ts +13 -0
- package/dist/storage/sqlite/statements.d.ts.map +1 -0
- package/dist/storage/sqlite/statements.js +88 -0
- package/dist/storage/sqlite/statements.js.map +1 -0
- package/dist/storage/sqlite/storage.d.ts +86 -0
- package/dist/storage/sqlite/storage.d.ts.map +1 -0
- package/dist/storage/sqlite/storage.js +534 -0
- package/dist/storage/sqlite/storage.js.map +1 -0
- package/dist/storage/store/commit.d.ts +33 -0
- package/dist/storage/store/commit.d.ts.map +1 -0
- package/dist/storage/store/commit.js +286 -0
- package/dist/storage/store/commit.js.map +1 -0
- package/dist/storage/store/documents.d.ts +43 -0
- package/dist/storage/store/documents.d.ts.map +1 -0
- package/dist/storage/store/documents.js +102 -0
- package/dist/storage/store/documents.js.map +1 -0
- package/dist/storage/store/index.d.ts +2 -0
- package/dist/storage/store/index.d.ts.map +1 -0
- package/dist/storage/store/index.js +2 -0
- package/dist/storage/store/index.js.map +1 -0
- package/dist/storage/store/queries.d.ts +49 -0
- package/dist/storage/store/queries.d.ts.map +1 -0
- package/dist/storage/store/queries.js +93 -0
- package/dist/storage/store/queries.js.map +1 -0
- package/dist/storage/store/storage.d.ts +60 -0
- package/dist/storage/store/storage.d.ts.map +1 -0
- package/dist/storage/store/storage.js +438 -0
- package/dist/storage/store/storage.js.map +1 -0
- package/dist/storage/store/tables.d.ts +54 -0
- package/dist/storage/store/tables.d.ts.map +1 -0
- package/dist/storage/store/tables.js +108 -0
- package/dist/storage/store/tables.js.map +1 -0
- package/dist/tasks.d.ts +6 -0
- package/dist/tasks.d.ts.map +1 -0
- package/dist/tasks.js +5 -0
- package/dist/tasks.js.map +1 -0
- package/dist/testing/assertions.d.ts +5 -0
- package/dist/testing/assertions.d.ts.map +1 -0
- package/dist/testing/assertions.js +25 -0
- package/dist/testing/assertions.js.map +1 -0
- package/dist/testing/commit-writes.d.ts +5 -0
- package/dist/testing/commit-writes.d.ts.map +1 -0
- package/dist/testing/commit-writes.js +12 -0
- package/dist/testing/commit-writes.js.map +1 -0
- package/dist/testing/env-conformance.d.ts +7 -0
- package/dist/testing/env-conformance.d.ts.map +1 -0
- package/dist/testing/env-conformance.js +531 -0
- package/dist/testing/env-conformance.js.map +1 -0
- package/dist/testing/index.d.ts +8 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +7 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/runner.d.ts +12 -0
- package/dist/testing/runner.d.ts.map +1 -0
- package/dist/testing/runner.js +23 -0
- package/dist/testing/runner.js.map +1 -0
- package/dist/testing/storage-benchmark.d.ts +41 -0
- package/dist/testing/storage-benchmark.d.ts.map +1 -0
- package/dist/testing/storage-benchmark.js +351 -0
- package/dist/testing/storage-benchmark.js.map +1 -0
- package/dist/testing/storage-conformance.d.ts +4 -0
- package/dist/testing/storage-conformance.d.ts.map +1 -0
- package/dist/testing/storage-conformance.js +1554 -0
- package/dist/testing/storage-conformance.js.map +1 -0
- package/dist/testing/types.d.ts +39 -0
- package/dist/testing/types.d.ts.map +1 -0
- package/dist/testing/types.js +2 -0
- package/dist/testing/types.js.map +1 -0
- package/dist/timers.d.ts +21 -0
- package/dist/timers.d.ts.map +1 -0
- package/dist/timers.js +72 -0
- package/dist/timers.js.map +1 -0
- package/dist/tool-search/index.d.ts +2 -0
- package/dist/tool-search/index.d.ts.map +1 -0
- package/dist/tool-search/index.js +2 -0
- package/dist/tool-search/index.js.map +1 -0
- package/dist/tool-search/tool-search.d.ts +17 -0
- package/dist/tool-search/tool-search.d.ts.map +1 -0
- package/dist/tool-search/tool-search.js +96 -0
- package/dist/tool-search/tool-search.js.map +1 -0
- package/dist/tools/bash.d.ts +55 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/bash.js +139 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/edit-diff.d.ts +89 -0
- package/dist/tools/edit-diff.d.ts.map +1 -0
- package/dist/tools/edit-diff.js +386 -0
- package/dist/tools/edit-diff.js.map +1 -0
- package/dist/tools/edit.d.ts +18 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/edit.js +117 -0
- package/dist/tools/edit.js.map +1 -0
- package/dist/tools/env.d.ts +5 -0
- package/dist/tools/env.d.ts.map +1 -0
- package/dist/tools/env.js +7 -0
- package/dist/tools/env.js.map +1 -0
- package/dist/tools/file-mutation-queue.d.ts +9 -0
- package/dist/tools/file-mutation-queue.d.ts.map +1 -0
- package/dist/tools/file-mutation-queue.js +52 -0
- package/dist/tools/file-mutation-queue.js.map +1 -0
- package/dist/tools/image-processor.d.ts +57 -0
- package/dist/tools/image-processor.d.ts.map +1 -0
- package/dist/tools/image-processor.js +49 -0
- package/dist/tools/image-processor.js.map +1 -0
- package/dist/tools/image.d.ts +20 -0
- package/dist/tools/image.d.ts.map +1 -0
- package/dist/tools/image.js +178 -0
- package/dist/tools/image.js.map +1 -0
- package/dist/tools/index.d.ts +46 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +23 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/path-utils.d.ts +5 -0
- package/dist/tools/path-utils.d.ts.map +1 -0
- package/dist/tools/path-utils.js +26 -0
- package/dist/tools/path-utils.js.map +1 -0
- package/dist/tools/read.d.ts +30 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/read.js +239 -0
- package/dist/tools/read.js.map +1 -0
- package/dist/tools/write.d.ts +10 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/tools/write.js +31 -0
- package/dist/tools/write.js.map +1 -0
- package/dist/truncate.d.ts +65 -0
- package/dist/truncate.d.ts.map +1 -0
- package/dist/truncate.js +124 -0
- package/dist/truncate.js.map +1 -0
- package/dist/types.d.ts +1244 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +179 -5
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Context } from "@diffra-ai/chord/context";
|
|
2
|
+
import { type ExecutionEnv } from "../env/index.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Serialize `edit` and `write` mutations of one file within this process: same file system id and canonical path,
|
|
5
|
+
* whichever environment object the call got. Other files, and other file systems, never wait. Concurrent calls on one
|
|
6
|
+
* file run in the order their keys resolve. Not a lock against `bash` or other processes.
|
|
7
|
+
*/
|
|
8
|
+
export declare function withFileMutationQueue<T>(env: ExecutionEnv, path: string, fn: () => Promise<T>, context: Context): Promise<T>;
|
|
9
|
+
//# sourceMappingURL=file-mutation-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-mutation-queue.d.ts","sourceRoot":"","sources":["../../src/tools/file-mutation-queue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,KAAK,YAAY,EAAc,MAAM,iBAAiB,CAAC;AA0BhE;;;;GAIG;AACH,wBAAsB,qBAAqB,CAAC,CAAC,EAC5C,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,OAAO,GACd,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { getOrThrow } from "../env/index.js";
|
|
2
|
+
/** Tail of the mutation chain of each file, keyed by file system id and canonical path. */
|
|
3
|
+
const queues = new Map();
|
|
4
|
+
async function mutationKey(env, path, context) {
|
|
5
|
+
const absolutePath = getOrThrow(await env.absolutePath(path, context));
|
|
6
|
+
return `${env.id}\0${await canonical(env, absolutePath, context)}`;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The canonical path; for a file that does not exist yet, its canonical parent joined with its name, so a `write` that
|
|
10
|
+
* creates a file and a later mutation of it share one key even under a symlinked directory.
|
|
11
|
+
*/
|
|
12
|
+
async function canonical(env, absolutePath, context) {
|
|
13
|
+
const result = await env.canonicalPath(absolutePath, context);
|
|
14
|
+
if (result.ok)
|
|
15
|
+
return result.value;
|
|
16
|
+
if (result.error.code === "not_supported")
|
|
17
|
+
return absolutePath;
|
|
18
|
+
if (result.error.code !== "not_found")
|
|
19
|
+
throw result.error;
|
|
20
|
+
// The file system splits the path, so a name may contain characters that are separators elsewhere.
|
|
21
|
+
const parent = getOrThrow(await env.joinPath([absolutePath, ".."], context));
|
|
22
|
+
if (parent === absolutePath || !absolutePath.startsWith(parent))
|
|
23
|
+
return absolutePath;
|
|
24
|
+
const name = absolutePath.slice(parent.length + (/[/\\]$/.test(parent) ? 0 : 1));
|
|
25
|
+
return getOrThrow(await env.joinPath([await canonical(env, parent, context), name], context));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Serialize `edit` and `write` mutations of one file within this process: same file system id and canonical path,
|
|
29
|
+
* whichever environment object the call got. Other files, and other file systems, never wait. Concurrent calls on one
|
|
30
|
+
* file run in the order their keys resolve. Not a lock against `bash` or other processes.
|
|
31
|
+
*/
|
|
32
|
+
export async function withFileMutationQueue(env, path, fn, context) {
|
|
33
|
+
const key = await mutationKey(env, path, context);
|
|
34
|
+
// Take the slot without awaiting, so no other call can take it in between.
|
|
35
|
+
const previous = queues.get(key) ?? Promise.resolve();
|
|
36
|
+
let release = () => { };
|
|
37
|
+
const done = new Promise((resolve) => {
|
|
38
|
+
release = resolve;
|
|
39
|
+
});
|
|
40
|
+
const tail = previous.then(() => done);
|
|
41
|
+
queues.set(key, tail);
|
|
42
|
+
await previous;
|
|
43
|
+
try {
|
|
44
|
+
return await fn();
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
release();
|
|
48
|
+
if (queues.get(key) === tail)
|
|
49
|
+
queues.delete(key);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# source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port { type ExecutionEnv, getOrThrow } from \"../env/index.ts\";\n\n/** Tail of the mutation chain of each file, keyed by file system id and canonical path. */\nconst queues = new Map<string, Promise<void>>();\n\nasync function mutationKey(env: ExecutionEnv, path: string, context: Context): Promise<string> {\n\tconst absolutePath = getOrThrow(await env.absolutePath(path, context));\n\treturn `${env.id}\\0${await canonical(env, absolutePath, context)}`;\n}\n\n/**\n * The canonical path; for a file that does not exist yet, its canonical parent joined with its name, so a `write` that\n * creates a file and a later mutation of it share one key even under a symlinked directory.\n */\nasync function canonical(env: ExecutionEnv, absolutePath: string, context: Context): Promise<string> {\n\tconst result = await env.canonicalPath(absolutePath, context);\n\tif (result.ok) return result.value;\n\tif (result.error.code === \"not_supported\") return absolutePath;\n\tif (result.error.code !== \"not_found\") throw result.error;\n\t// The file system splits the path, so a name may contain characters that are separators elsewhere.\n\tconst parent = getOrThrow(await env.joinPath([absolutePath, \"..\"], context));\n\tif (parent === absolutePath || !absolutePath.startsWith(parent)) return absolutePath;\n\tconst name = absolutePath.slice(parent.length + (/[/\\\\]$/.test(parent) ? 0 : 1));\n\treturn getOrThrow(await env.joinPath([await canonical(env, parent, context), name], context));\n}\n\n/**\n * Serialize `edit` and `write` mutations of one file within this process: same file system id and canonical path,\n * whichever environment object the call got. Other files, and other file systems, never wait. Concurrent calls on one\n * file run in the order their keys resolve. Not a lock against `bash` or other processes.\n */\nexport async function withFileMutationQueue<T>(\n\tenv: ExecutionEnv,\n\tpath: string,\n\tfn: () => Promise<T>,\n\tcontext: Context,\n): Promise<T> {\n\tconst key = await mutationKey(env, path, context);\n\t// Take the slot without awaiting, so no other call can take it in between.\n\tconst previous = queues.get(key) ?? Promise.resolve();\n\tlet release = (): void => {};\n\tconst done = new Promise<void>((resolve) => {\n\t\trelease = resolve;\n\t});\n\tconst tail = previous.then(() => done);\n\tqueues.set(key, tail);\n\tawait previous;\n\ttry {\n\t\treturn await fn();\n\t} finally {\n\t\trelease();\n\t\tif (queues.get(key) === tail) queues.delete(key);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Api, Model } from "@diffra-ai/ai";
|
|
2
|
+
/** Limits an image the model sees must keep; the defaults below are the coding agent's. */
|
|
3
|
+
export type ImageLimits = {
|
|
4
|
+
/** Pixels. Default 2000. */
|
|
5
|
+
readonly maxWidth: number;
|
|
6
|
+
/** Pixels. Default 2000. */
|
|
7
|
+
readonly maxHeight: number;
|
|
8
|
+
/** Bytes of base64. Default 4.5 MiB, below Anthropic's 5 MB. */
|
|
9
|
+
readonly maxBytes: number;
|
|
10
|
+
};
|
|
11
|
+
export declare const DEFAULT_IMAGE_LIMITS: ImageLimits;
|
|
12
|
+
/** The limits of images sent to `model`: its `inputLimits.images.resize`, each absent one `DEFAULT_IMAGE_LIMITS`'s. */
|
|
13
|
+
export declare function imageLimits(model: Model<Api> | undefined): ImageLimits;
|
|
14
|
+
/** An image the model can take: a supported format, within the limits, as base64. */
|
|
15
|
+
export type PreparedImage = {
|
|
16
|
+
readonly data: string;
|
|
17
|
+
readonly mimeType: string;
|
|
18
|
+
/** Set when the image was resized: its size before and after, for the model to map coordinates back. */
|
|
19
|
+
readonly resized?: {
|
|
20
|
+
readonly from: {
|
|
21
|
+
readonly width: number;
|
|
22
|
+
readonly height: number;
|
|
23
|
+
};
|
|
24
|
+
readonly to: {
|
|
25
|
+
readonly width: number;
|
|
26
|
+
readonly height: number;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
/** The original format, set when it differs from `mimeType`: BMP becomes PNG, a large PNG may become JPEG. */
|
|
30
|
+
readonly convertedFrom?: string;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Decodes, orients, resizes, and re-encodes images so the model can take them. `read` uses one when given; without
|
|
34
|
+
* one, it passes supported images within the byte limit through as they are. `@diffra-ai/durable/images`
|
|
35
|
+
* provides a Photon-based one.
|
|
36
|
+
*/
|
|
37
|
+
export type ImageProcessor = {
|
|
38
|
+
/**
|
|
39
|
+
* The image, prepared to fit `limits`, or `undefined` when it cannot be decoded or made to fit. A promise, so a
|
|
40
|
+
* processor can do the work off the event loop.
|
|
41
|
+
*/
|
|
42
|
+
prepare(bytes: Uint8Array, mimeType: string, limits: ImageLimits): Promise<PreparedImage | undefined>;
|
|
43
|
+
};
|
|
44
|
+
/** What preparing changed, for the model: the format, and the size with the factor that maps coordinates back. */
|
|
45
|
+
export declare function preparedNotes(image: PreparedImage): string[];
|
|
46
|
+
/** Formats every provider takes inline; others, such as BMP, need converting. */
|
|
47
|
+
export declare const INLINE_IMAGE_TYPES: ReadonlySet<string>;
|
|
48
|
+
/**
|
|
49
|
+
* Standard base64 of `bytes`. `btoa` takes a binary string, built 8 KiB at a time: a single `String.fromCharCode`
|
|
50
|
+
* call with megabytes of arguments overflows the stack.
|
|
51
|
+
*/
|
|
52
|
+
export declare function toBase64(bytes: Uint8Array): string;
|
|
53
|
+
/** The bytes of standard base64 `data`. */
|
|
54
|
+
export declare function fromBase64(data: string): Uint8Array;
|
|
55
|
+
/** The length of the base64 of `byteLength` bytes. */
|
|
56
|
+
export declare function base64Length(byteLength: number): number;
|
|
57
|
+
//# sourceMappingURL=image-processor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-processor.d.ts","sourceRoot":"","sources":["../../src/tools/image-processor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEhD,2FAA2F;AAC3F,MAAM,MAAM,WAAW,GAAG;IACzB,4BAA4B;IAC5B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,4BAA4B;IAC5B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,gEAAgE;IAChE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,eAAO,MAAM,oBAAoB,EAAE,WAA8E,CAAC;AAElH,uHAAuH;AACvH,wBAAgB,WAAW,CA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
|
|
1
|
+
export const DEFAULT_IMAGE_LIMITS = { maxWidth: 2000, maxHeight: 2000, maxBytes: 4.5 * 1024 * 1024 };
|
|
2
|
+
/** The limits of images sent to `model`: its `inputLimits.images.resize`, each absent one `DEFAULT_IMAGE_LIMITS`'s. */
|
|
3
|
+
export function imageLimits(model) {
|
|
4
|
+
const resize = model?.inputLimits?.images?.resize;
|
|
5
|
+
return {
|
|
6
|
+
maxWidth: resize?.maxWidth ?? DEFAULT_IMAGE_LIMITS.maxWidth,
|
|
7
|
+
maxHeight: resize?.maxHeight ?? DEFAULT_IMAGE_LIMITS.maxHeight,
|
|
8
|
+
maxBytes: resize?.maxBytes ?? DEFAULT_IMAGE_LIMITS.maxBytes,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/** What preparing changed, for the model: the format, and the size with the factor that maps coordinates back. */
|
|
12
|
+
export function preparedNotes(image) {
|
|
13
|
+
const notes = [];
|
|
14
|
+
if (image.convertedFrom !== undefined)
|
|
15
|
+
notes.push(`Converted from ${image.convertedFrom} to ${image.mimeType}.`);
|
|
16
|
+
if (image.resized !== undefined) {
|
|
17
|
+
const { from, to } = image.resized;
|
|
18
|
+
const scale = (from.width / to.width).toFixed(2);
|
|
19
|
+
notes.push(`Resized from ${from.width}x${from.height} to ${to.width}x${to.height}. Multiply coordinates by ${scale} to map them to the original.`);
|
|
20
|
+
}
|
|
21
|
+
return notes;
|
|
22
|
+
}
|
|
23
|
+
/** Formats every provider takes inline; others, such as BMP, need converting. */
|
|
24
|
+
export const INLINE_IMAGE_TYPES = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"]);
|
|
25
|
+
/**
|
|
26
|
+
* Standard base64 of `bytes`. `btoa` takes a binary string, built 8 KiB at a time: a single `String.fromCharCode`
|
|
27
|
+
* call with megabytes of arguments overflows the stack.
|
|
28
|
+
*/
|
|
29
|
+
export function toBase64(bytes) {
|
|
30
|
+
const parts = [];
|
|
31
|
+
for (let i = 0; i < bytes.length; i += 8192) {
|
|
32
|
+
// `apply` takes any array-like, though its type asks for an array.
|
|
33
|
+
parts.push(String.fromCharCode.apply(null, bytes.subarray(i, i + 8192)));
|
|
34
|
+
}
|
|
35
|
+
return btoa(parts.join(""));
|
|
36
|
+
}
|
|
37
|
+
/** The bytes of standard base64 `data`. */
|
|
38
|
+
export function fromBase64(data) {
|
|
39
|
+
const binary = atob(data);
|
|
40
|
+
const bytes = new Uint8Array(binary.length);
|
|
41
|
+
for (let index = 0; index < binary.length; index++)
|
|
42
|
+
bytes[index] = binary.charCodeAt(index);
|
|
43
|
+
return bytes;
|
|
44
|
+
}
|
|
45
|
+
/** The length of the base64 of `byteLength` bytes. */
|
|
46
|
+
export function base64Length(byteLength) {
|
|
47
|
+
return Math.ceil(byteLength / 3) * 4;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=image-processor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-processor.js","sourceRoot":"","sources":["../../src/tools/image-processor.ts"],"names":[],"mappings":"AAYA,MAAM,CAAC,MAAM,oBAAoB,GAAgB,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI,EAAE,CAAC;AAElH,uHAAuH;AACvH,MAAM,UAAU,WAAW,CAAC,KAA6B;IACxD,MAAM,MAAM,GAAG,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,CAAC;IAClD,OAAO;QACN,QAAQ,EAAE,MAAM,EAAE,QAAQ,IAAI,oBAAoB,CAAC,QAAQ;QAC3D,SAAS,EAAE,MAAM,EAAE,SAAS,IAAI,oBAAoB,CAAC,SAAS;QAC9D,QAAQ,EAAE,MAAM,EAAE,QAAQ,IAAI,oBAAoB,CAAC,QAAQ;KAC3D,CAAC;AACH,CAAC;AA4BD,kHAAkH;AAClH,MAAM,UAAU,aAAa,CAAC,KAAoB;IACjD,MAAM,KAAK,GAAa,EAAE,CAAC;I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U,CAAC,IAAY;IACtC,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5C,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE;QAAE,KAAK,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IAC5F,OAAO,KAAK,CAAC;AACd,CAAC;AAED,sDAAsD;AACtD,MAAM,UAAU,YAAY,CAAC,UAAkB;IAC9C,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AACtC,CAAC","sourcesContent":["import type { Api, Model } from \"@diffra-ai/ai\";\n\n/** Limits an image the model sees must keep; the defaults below are the coding agent's. */\nexport type ImageLimits = {\n\t/** Pixels. Default 2000. */\n\treadonly maxWidth: number;\n\t/** Pixels. Default 2000. */\n\treadonly maxHeight: number;\n\t/** Bytes of base64. Default 4.5 MiB, below Anthropic's 5 MB. */\n\treadonly maxBytes: number;\n};\n\nexport const DEFAULT_IMAGE_LIMITS: ImageLimits = { maxWidth: 2000, maxHeight: 2000, maxBytes: 4.5 * 1024 * 1024 };\n\n/** The limits of images sent to `model`: its `inputLimits.images.resize`, each absent one `DEFAULT_IMAGE_LIMITS`'s. */\nexport function imageLimits(model: Model<Api> | undefined): ImageLimits {\n\tconst resize = model?.inputLimits?.images?.resize;\n\treturn {\n\t\tmaxWidth: resize?.maxWidth ?? DEFAULT_IMAGE_LIMITS.maxWidth,\n\t\tmaxHeight: resize?.maxHeight ?? DEFAULT_IMAGE_LIMITS.maxHeight,\n\t\tmaxBytes: resize?.maxBytes ?? DEFAULT_IMAGE_LIMITS.maxBytes,\n\t};\n}\n\n/** An image the model can take: a supported format, within the limits, as base64. */\nexport type PreparedImage = {\n\treadonly data: string;\n\treadonly mimeType: string;\n\t/** Set when the image was resized: its size before and after, for the model to map coordinates back. */\n\treadonly resized?: {\n\t\treadonly from: { readonly width: number; readonly height: number };\n\t\treadonly to: { readonly width: number; readonly height: number };\n\t};\n\t/** The original format, set when it differs from `mimeType`: BMP becomes PNG, a large PNG may become JPEG. */\n\treadonly convertedFrom?: string;\n};\n\n/**\n * Decodes, orients, resizes, and re-encodes images so the model can take them. `read` uses one when given; without\n * one, it passes supported images within the byte limit through as they are. `@diffra-ai/durable/images`\n * provides a Photon-based one.\n */\nexport type ImageProcessor = {\n\t/**\n\t * The image, prepared to fit `limits`, or `undefined` when it cannot be decoded or made to fit. A promise, so a\n\t * processor can do the work off the event loop.\n\t */\n\tprepare(bytes: Uint8Array, mimeType: string, limits: ImageLimits): Promise<PreparedImage | undefined>;\n};\n\n/** What preparing changed, for the model: the format, and the size with the factor that maps coordinates back. */\nexport function preparedNotes(image: PreparedImage): string[] {\n\tconst notes: string[] = [];\n\tif (image.convertedFrom !== undefined) notes.push(`Converted from ${image.convertedFrom} to ${image.mimeType}.`);\n\tif (image.resized !== undefined) {\n\t\tconst { from, to } = image.resized;\n\t\tconst scale = (from.width / to.width).toFixed(2);\n\t\tnotes.push(\n\t\t\t`Resized from ${from.width}x${from.height} to ${to.width}x${to.height}. Multiply coordinates by ${scale} to map them to the original.`,\n\t\t);\n\t}\n\treturn notes;\n}\n\n/** Formats every provider takes inline; others, such as BMP, need converting. */\nexport const INLINE_IMAGE_TYPES: ReadonlySet<string> = new Set([\"image/png\", \"image/jpeg\", \"image/gif\", \"image/webp\"]);\n\n/**\n * Standard base64 of `bytes`. `btoa` takes a binary string, built 8 KiB at a time: a single `String.fromCharCode`\n * call with megabytes of arguments overflows the stack.\n */\nexport function toBase64(bytes: Uint8Array): string {\n\tconst parts: string[] = [];\n\tfor (let i = 0; i < bytes.length; i += 8192) {\n\t\t// `apply` takes any array-like, though its type asks for an array.\n\t\tparts.push(String.fromCharCode.apply(null, bytes.subarray(i, i + 8192) as unknown as number[]));\n\t}\n\treturn btoa(parts.join(\"\"));\n}\n\n/** The bytes of standard base64 `data`. */\nexport function fromBase64(data: string): Uint8Array {\n\tconst binary = atob(data);\n\tconst bytes = new Uint8Array(binary.length);\n\tfor (let index = 0; index < binary.length; index++) bytes[index] = binary.charCodeAt(index);\n\treturn bytes;\n}\n\n/** The length of the base64 of `byteLength` bytes. */\nexport function base64Length(byteLength: number): number {\n\treturn Math.ceil(byteLength / 3) * 4;\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Positional reads of a file of `size` bytes. */
|
|
2
|
+
export type ByteSource = {
|
|
3
|
+
readonly size: number;
|
|
4
|
+
read(offset: number, length: number): Promise<Uint8Array>;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* `detectSupportedImageMimeType` of a whole file, reading only its header and, for PNG, the chunk headers up to the
|
|
8
|
+
* first `acTL` or `IDAT`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function detectSupportedImageMimeTypeOf(source: ByteSource): Promise<string | undefined>;
|
|
11
|
+
export declare function detectSupportedImageMimeType(buffer: Uint8Array): string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* The width and height an image of `mimeType` declares in its header, without decoding it: PNG's IHDR, GIF's screen,
|
|
14
|
+
* WebP's VP8, VP8L, or VP8X chunk, JPEG's first SOF segment. `undefined` when the header cannot be read.
|
|
15
|
+
*/
|
|
16
|
+
export declare function imageDimensions(bytes: Uint8Array, mimeType: string): {
|
|
17
|
+
readonly width: number;
|
|
18
|
+
readonly height: number;
|
|
19
|
+
} | undefined;
|
|
20
|
+
//# sourceMappingURL=image.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../src/tools/image.ts"],"names":[],"mappings":"AAKA,kDAAkD;AAClD,MAAM,MAAM,UAAU,GAAG;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC1D,CAAC;AAEF;;;GAGG;AACH,wBAAsB,8BAA8B,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAIpG;AA0BD,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,GAAG,SAAS,CAOnF;AAsBD;;;GAGG;AACH,wBAAgB,eAAe,CAC9B,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,MAAM,GACd;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAqCjE"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
|
2
|
+
/** Bytes every check except the APNG chunk walk needs: BMP reads up to offset 29. */
|
|
3
|
+
const HEADER_BYTES = 32;
|
|
4
|
+
const BLOCK_BYTES = 64 * 1024;
|
|
5
|
+
/**
|
|
6
|
+
* `detectSupportedImageMimeType` of a whole file, reading only its header and, for PNG, the chunk headers up to the
|
|
7
|
+
* first `acTL` or `IDAT`.
|
|
8
|
+
*/
|
|
9
|
+
export async function detectSupportedImageMimeTypeOf(source) {
|
|
10
|
+
const header = await source.read(0, HEADER_BYTES);
|
|
11
|
+
if (!startsWith(header, PNG_SIGNATURE))
|
|
12
|
+
return detectSupportedImageMimeType(header);
|
|
13
|
+
return isPng(header) && !(await isAnimatedPngOf(source)) ? "image/png" : undefined;
|
|
14
|
+
}
|
|
15
|
+
/** `isAnimatedPng` over a file read in blocks. */
|
|
16
|
+
async function isAnimatedPngOf(source) {
|
|
17
|
+
let block = new Uint8Array(0);
|
|
18
|
+
let blockStart = 0;
|
|
19
|
+
const bytesAt = async (offset, length) => {
|
|
20
|
+
if (offset < blockStart || offset + length > blockStart + block.length) {
|
|
21
|
+
blockStart = offset;
|
|
22
|
+
block = await source.read(offset, BLOCK_BYTES);
|
|
23
|
+
}
|
|
24
|
+
return block.subarray(offset - blockStart, offset - blockStart + length);
|
|
25
|
+
};
|
|
26
|
+
let offset = PNG_SIGNATURE.length;
|
|
27
|
+
while (offset + 8 <= source.size) {
|
|
28
|
+
const chunkHeader = await bytesAt(offset, 8);
|
|
29
|
+
const chunkLength = readUint32BE(chunkHeader, 0);
|
|
30
|
+
if (startsWithAscii(chunkHeader, 4, "acTL"))
|
|
31
|
+
return true;
|
|
32
|
+
if (startsWithAscii(chunkHeader, 4, "IDAT"))
|
|
33
|
+
return false;
|
|
34
|
+
const nextOffset = offset + 8 + chunkLength + 4;
|
|
35
|
+
if (nextOffset <= offset || nextOffset > source.size)
|
|
36
|
+
return false;
|
|
37
|
+
offset = nextOffset;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
export function detectSupportedImageMimeType(buffer) {
|
|
42
|
+
if (startsWith(buffer, [0xff, 0xd8, 0xff]))
|
|
43
|
+
return buffer[3] === 0xf7 ? undefined : "image/jpeg";
|
|
44
|
+
if (startsWith(buffer, PNG_SIGNATURE))
|
|
45
|
+
return isPng(buffer) && !isAnimatedPng(buffer) ? "image/png" : undefined;
|
|
46
|
+
if (startsWithAscii(buffer, 0, "GIF87a") || startsWithAscii(buffer, 0, "GIF89a"))
|
|
47
|
+
return "image/gif";
|
|
48
|
+
if (startsWithAscii(buffer, 0, "RIFF") && startsWithAscii(buffer, 8, "WEBP"))
|
|
49
|
+
return "image/webp";
|
|
50
|
+
if (startsWithAscii(buffer, 0, "BM") && isBmp(buffer))
|
|
51
|
+
return "image/bmp";
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
function isPng(buffer) {
|
|
55
|
+
return (buffer.length >= 16 && readUint32BE(buffer, PNG_SIGNATURE.length) === 13 && startsWithAscii(buffer, 12, "IHDR"));
|
|
56
|
+
}
|
|
57
|
+
function isAnimatedPng(buffer) {
|
|
58
|
+
let offset = PNG_SIGNATURE.length;
|
|
59
|
+
while (offset + 8 <= buffer.length) {
|
|
60
|
+
const chunkLength = readUint32BE(buffer, offset);
|
|
61
|
+
const chunkTypeOffset = offset + 4;
|
|
62
|
+
if (startsWithAscii(buffer, chunkTypeOffset, "acTL"))
|
|
63
|
+
return true;
|
|
64
|
+
if (startsWithAscii(buffer, chunkTypeOffset, "IDAT"))
|
|
65
|
+
return false;
|
|
66
|
+
const nextOffset = offset + 8 + chunkLength + 4;
|
|
67
|
+
if (nextOffset <= offset || nextOffset > buffer.length)
|
|
68
|
+
return false;
|
|
69
|
+
offset = nextOffset;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The width and height an image of `mimeType` declares in its header, without decoding it: PNG's IHDR, GIF's screen,
|
|
75
|
+
* WebP's VP8, VP8L, or VP8X chunk, JPEG's first SOF segment. `undefined` when the header cannot be read.
|
|
76
|
+
*/
|
|
77
|
+
export function imageDimensions(bytes, mimeType) {
|
|
78
|
+
switch (mimeType) {
|
|
79
|
+
case "image/png":
|
|
80
|
+
return bytes.length < 24 ? undefined : { width: readUint32BE(bytes, 16), height: readUint32BE(bytes, 20) };
|
|
81
|
+
case "image/gif":
|
|
82
|
+
return bytes.length < 10 ? undefined : { width: readUint16LE(bytes, 6), height: readUint16LE(bytes, 8) };
|
|
83
|
+
case "image/webp":
|
|
84
|
+
if (bytes.length < 30)
|
|
85
|
+
return undefined;
|
|
86
|
+
if (startsWithAscii(bytes, 12, "VP8 "))
|
|
87
|
+
return { width: readUint16LE(bytes, 26) & 0x3fff, height: readUint16LE(bytes, 28) & 0x3fff };
|
|
88
|
+
if (startsWithAscii(bytes, 12, "VP8L")) {
|
|
89
|
+
const bits = readUint32LE(bytes, 21);
|
|
90
|
+
return { width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1 };
|
|
91
|
+
}
|
|
92
|
+
if (startsWithAscii(bytes, 12, "VP8X")) {
|
|
93
|
+
const uint24 = (offset) => readUint16LE(bytes, offset) + ((bytes[offset + 2] ?? 0) << 16);
|
|
94
|
+
return { width: uint24(24) + 1, height: uint24(27) + 1 };
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
case "image/jpeg":
|
|
98
|
+
for (let offset = 2; offset + 9 <= bytes.length;) {
|
|
99
|
+
if (bytes[offset] !== 0xff)
|
|
100
|
+
return undefined;
|
|
101
|
+
const marker = bytes[offset + 1];
|
|
102
|
+
if (marker === 0xff) {
|
|
103
|
+
offset++;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
// SOF0 to SOF15, except DHT (C4), JPG (C8), and DAC (CC), which share the range.
|
|
107
|
+
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
|
|
108
|
+
return { width: readUint16BE(bytes, offset + 7), height: readUint16BE(bytes, offset + 5) };
|
|
109
|
+
}
|
|
110
|
+
offset += 2 + readUint16BE(bytes, offset + 2);
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
default:
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function isBmp(buffer) {
|
|
118
|
+
if (buffer.length < 26)
|
|
119
|
+
return false;
|
|
120
|
+
const declaredFileSize = readUint32LE(buffer, 2);
|
|
121
|
+
const pixelDataOffset = readUint32LE(buffer, 10);
|
|
122
|
+
const dibHeaderSize = readUint32LE(buffer, 14);
|
|
123
|
+
if (declaredFileSize !== 0 && declaredFileSize < 26)
|
|
124
|
+
return false;
|
|
125
|
+
if (pixelDataOffset < 14 + dibHeaderSize)
|
|
126
|
+
return false;
|
|
127
|
+
if (declaredFileSize !== 0 && pixelDataOffset >= declaredFileSize)
|
|
128
|
+
return false;
|
|
129
|
+
let colorPlanes;
|
|
130
|
+
let bitsPerPixel;
|
|
131
|
+
if (dibHeaderSize === 12) {
|
|
132
|
+
colorPlanes = readUint16LE(buffer, 22);
|
|
133
|
+
bitsPerPixel = readUint16LE(buffer, 24);
|
|
134
|
+
}
|
|
135
|
+
else if (dibHeaderSize >= 40 && dibHeaderSize <= 124) {
|
|
136
|
+
if (buffer.length < 30)
|
|
137
|
+
return false;
|
|
138
|
+
colorPlanes = readUint16LE(buffer, 26);
|
|
139
|
+
bitsPerPixel = readUint16LE(buffer, 28);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return colorPlanes === 1 && [1, 4, 8, 16, 24, 32].includes(bitsPerPixel);
|
|
145
|
+
}
|
|
146
|
+
function readUint16LE(buffer, offset) {
|
|
147
|
+
return (buffer[offset] ?? 0) + ((buffer[offset + 1] ?? 0) << 8);
|
|
148
|
+
}
|
|
149
|
+
function readUint16BE(buffer, offset) {
|
|
150
|
+
return ((buffer[offset] ?? 0) << 8) + (buffer[offset + 1] ?? 0);
|
|
151
|
+
}
|
|
152
|
+
function readUint32BE(buffer, offset) {
|
|
153
|
+
return ((buffer[offset] ?? 0) * 0x1000000 +
|
|
154
|
+
((buffer[offset + 1] ?? 0) << 16) +
|
|
155
|
+
((buffer[offset + 2] ?? 0) << 8) +
|
|
156
|
+
(buffer[offset + 3] ?? 0));
|
|
157
|
+
}
|
|
158
|
+
function readUint32LE(buffer, offset) {
|
|
159
|
+
return ((buffer[offset] ?? 0) +
|
|
160
|
+
((buffer[offset + 1] ?? 0) << 8) +
|
|
161
|
+
((buffer[offset + 2] ?? 0) << 16) +
|
|
162
|
+
(buffer[offset + 3] ?? 0) * 0x1000000);
|
|
163
|
+
}
|
|
164
|
+
function startsWith(buffer, bytes) {
|
|
165
|
+
if (buffer.length < bytes.length)
|
|
166
|
+
return false;
|
|
167
|
+
return bytes.every((byte, index) => buffer[index] === byte);
|
|
168
|
+
}
|
|
169
|
+
function startsWithAscii(buffer, offset, text) {
|
|
170
|
+
if (buffer.length < offset + text.length)
|
|
171
|
+
return false;
|
|
172
|
+
for (let index = 0; index < text.length; index++) {
|
|
173
|
+
if (buffer[offset + index] !== text.charCodeAt(index))
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=image.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.js","sourceRoot":"","sources":["../../src/tools/image.ts"],"names":[],"mappings":"AAAA,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AACvE,qFAAqF;AACrF,MAAM,YAAY,GAAG,EAAE,CAAC;AACxB,MAAM,WAAW,GAAG,EAAE,GAAG,IAAI,CAAC;AAQ9B;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,8BAA8B,CAAC,MAAkB;IACtE,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC;IAClD,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC;QAAE,OAAO,4BAA4B,CAAC,MAAM,CAAC,CAAC;IACpF,OAAO,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC;AACpF,CAAC;AAED,kDAAkD;AAClD,KAAK,UAAU,eAAe,CAAC,MAAkB;IAChD,IAAI,KAAK,GAAe,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IAC1C,IAAI,UAAU,GAAG,CAAC,CAAC;IACnB,MAAM,OAAO,GAAG,KAAK,EAAE,MAAc,EAAE,MAAc,EAAuB,EAAE;QAC7E,IAAI,MAAM,GAAG,UAAU,IAAI,MAAM,GAAG,MAAM,GAAG,UAAU,G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AAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;QACjC,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS,CACrC,CAAC;AACH,CAAC;AAED,SAAS,UAAU,CAAC,MAAkB,EAAE,KAAe;IACtD,IAAI,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAC/C,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC;AAC7D,CAAC;AAED,SAAS,eAAe,CAAC,MAAkB,EAAE,MAAc,EAAE,IAAY;IACxE,IAAI,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACvD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAClD,IAAI,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC,KAAK,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC;YAAE,OAAO,KAAK,CAAC;IACrE,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC","sourcesContent":["const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];\n/** Bytes every check except the APNG chunk walk needs: BMP reads up to offset 29. */\nconst HEADER_BYTES = 32;\nconst BLOCK_BYTES = 64 * 1024;\n\n/** Positional reads of a file of `size` bytes. */\nexport type ByteSource = {\n\treadonly size: number;\n\tread(offset: number, length: number): Promise<Uint8Array>;\n};\n\n/**\n * `detectSupportedImageMimeType` of a whole file, reading only its header and, for PNG, the chunk headers up to the\n * first `acTL` or `IDAT`.\n */\nexport async function detectSupportedImageMimeTypeOf(source: ByteSource): Promise<string | undefined> {\n\tconst header = await source.read(0, HEADER_BYTES);\n\tif (!startsWith(header, PNG_SIGNATURE)) return detectSupportedImageMimeType(header);\n\treturn isPng(header) && !(await isAnimatedPngOf(source)) ? \"image/png\" : undefined;\n}\n\n/** `isAnimatedPng` over a file read in blocks. */\nasync function isAnimatedPngOf(source: ByteSource): Promise<boolean> {\n\tlet block: Uint8Array = new Uint8Array(0);\n\tlet blockStart = 0;\n\tconst bytesAt = async (offset: number, length: number): Promise<Uint8Array> => {\n\t\tif (offset < blockStart || offset + length > blockStart + block.length) {\n\t\t\tblockStart = offset;\n\t\t\tblock = await source.read(offset, BLOCK_BYTES);\n\t\t}\n\t\treturn block.subarray(offset - blockStart, offset - blockStart + length);\n\t};\n\tlet offset = PNG_SIGNATURE.length;\n\twhile (offset + 8 <= source.size) {\n\t\tconst chunkHeader = await bytesAt(offset, 8);\n\t\tconst chunkLength = readUint32BE(chunkHeader, 0);\n\t\tif (startsWithAscii(chunkHeader, 4, \"acTL\")) return true;\n\t\tif (startsWithAscii(chunkHeader, 4, \"IDAT\")) return false;\n\t\tconst nextOffset = offset + 8 + chunkLength + 4;\n\t\tif (nextOffset <= offset || nextOffset > source.size) return false;\n\t\toffset = nextOffset;\n\t}\n\treturn false;\n}\n\nexport function detectSupportedImageMimeType(buffer: Uint8Array): string | undefined {\n\tif (startsWith(buffer, [0xff, 0xd8, 0xff])) return buffer[3] === 0xf7 ? undefined : \"image/jpeg\";\n\tif (startsWith(buffer, PNG_SIGNATURE)) return isPng(buffer) && !isAnimatedPng(buffer) ? \"image/png\" : undefined;\n\tif (startsWithAscii(buffer, 0, \"GIF87a\") || startsWithAscii(buffer, 0, \"GIF89a\")) return \"image/gif\";\n\tif (startsWithAscii(buffer, 0, \"RIFF\") && startsWithAscii(buffer, 8, \"WEBP\")) return \"image/webp\";\n\tif (startsWithAscii(buffer, 0, \"BM\") && isBmp(buffer)) return \"image/bmp\";\n\treturn undefined;\n}\n\nfunction isPng(buffer: Uint8Array): boolean {\n\treturn (\n\t\tbuffer.length >= 16 && readUint32BE(buffer, PNG_SIGNATURE.length) === 13 && startsWithAscii(buffer, 12, \"IHDR\")\n\t);\n}\n\nfunction isAnimatedPng(buffer: Uint8Array): boolean {\n\tlet offset = PNG_SIGNATURE.length;\n\twhile (offset + 8 <= buffer.length) {\n\t\tconst chunkLength = readUint32BE(buffer, offset);\n\t\tconst chunkTypeOffset = offset + 4;\n\t\tif (startsWithAscii(buffer, chunkTypeOffset, \"acTL\")) return true;\n\t\tif (startsWithAscii(buffer, chunkTypeOffset, \"IDAT\")) return false;\n\t\tconst nextOffset = offset + 8 + chunkLength + 4;\n\t\tif (nextOffset <= offset || nextOffset > buffer.length) return false;\n\t\toffset = nextOffset;\n\t}\n\treturn false;\n}\n\n/**\n * The width and height an image of `mimeType` declares in its header, without decoding it: PNG's IHDR, GIF's screen,\n * WebP's VP8, VP8L, or VP8X chunk, JPEG's first SOF segment. `undefined` when the header cannot be read.\n */\nexport function imageDimensions(\n\tbytes: Uint8Array,\n\tmimeType: string,\n): { readonly width: number; readonly height: number } | undefined {\n\tswitch (mimeType) {\n\t\tcase \"image/png\":\n\t\t\treturn bytes.length < 24 ? undefined : { width: readUint32BE(bytes, 16), height: readUint32BE(bytes, 20) };\n\t\tcase \"image/gif\":\n\t\t\treturn bytes.length < 10 ? undefined : { width: readUint16LE(bytes, 6), height: readUint16LE(bytes, 8) };\n\t\tcase \"image/webp\":\n\t\t\tif (bytes.length < 30) return undefined;\n\t\t\tif (startsWithAscii(bytes, 12, \"VP8 \"))\n\t\t\t\treturn { width: readUint16LE(bytes, 26) & 0x3fff, height: readUint16LE(bytes, 28) & 0x3fff };\n\t\t\tif (startsWithAscii(bytes, 12, \"VP8L\")) {\n\t\t\t\tconst bits = readUint32LE(bytes, 21);\n\t\t\t\treturn { width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1 };\n\t\t\t}\n\t\t\tif (startsWithAscii(bytes, 12, \"VP8X\")) {\n\t\t\t\tconst uint24 = (offset: number): number => readUint16LE(bytes, offset) + ((bytes[offset + 2] ?? 0) << 16);\n\t\t\t\treturn { width: uint24(24) + 1, height: uint24(27) + 1 };\n\t\t\t}\n\t\t\treturn undefined;\n\t\tcase \"image/jpeg\":\n\t\t\tfor (let offset = 2; offset + 9 <= bytes.length; ) {\n\t\t\t\tif (bytes[offset] !== 0xff) return undefined;\n\t\t\t\tconst marker = bytes[offset + 1]!;\n\t\t\t\tif (marker === 0xff) {\n\t\t\t\t\toffset++;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// SOF0 to SOF15, except DHT (C4), JPG (C8), and DAC (CC), which share the range.\n\t\t\t\tif (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {\n\t\t\t\t\treturn { width: readUint16BE(bytes, offset + 7), height: readUint16BE(bytes, offset + 5) };\n\t\t\t\t}\n\t\t\t\toffset += 2 + readUint16BE(bytes, offset + 2);\n\t\t\t}\n\t\t\treturn undefined;\n\t\tdefault:\n\t\t\treturn undefined;\n\t}\n}\n\nfunction isBmp(buffer: Uint8Array): boolean {\n\tif (buffer.length < 26) return false;\n\tconst declaredFileSize = readUint32LE(buffer, 2);\n\tconst pixelDataOffset = readUint32LE(buffer, 10);\n\tconst dibHeaderSize = readUint32LE(buffer, 14);\n\tif (declaredFileSize !== 0 && declaredFileSize < 26) return false;\n\tif (pixelDataOffset < 14 + dibHeaderSize) return false;\n\tif (declaredFileSize !== 0 && pixelDataOffset >= declaredFileSize) return false;\n\n\tlet colorPlanes: number;\n\tlet bitsPerPixel: number;\n\tif (dibHeaderSize === 12) {\n\t\tcolorPlanes = readUint16LE(buffer, 22);\n\t\tbitsPerPixel = readUint16LE(buffer, 24);\n\t} else if (dibHeaderSize >= 40 && dibHeaderSize <= 124) {\n\t\tif (buffer.length < 30) return false;\n\t\tcolorPlanes = readUint16LE(buffer, 26);\n\t\tbitsPerPixel = readUint16LE(buffer, 28);\n\t} else {\n\t\treturn false;\n\t}\n\treturn colorPlanes === 1 && [1, 4, 8, 16, 24, 32].includes(bitsPerPixel);\n}\n\nfunction readUint16LE(buffer: Uint8Array, offset: number): number {\n\treturn (buffer[offset] ?? 0) + ((buffer[offset + 1] ?? 0) << 8);\n}\n\nfunction readUint16BE(buffer: Uint8Array, offset: number): number {\n\treturn ((buffer[offset] ?? 0) << 8) + (buffer[offset + 1] ?? 0);\n}\n\nfunction readUint32BE(buffer: Uint8Array, offset: number): number {\n\treturn (\n\t\t(buffer[offset] ?? 0) * 0x1000000 +\n\t\t((buffer[offset + 1] ?? 0) << 16) +\n\t\t((buffer[offset + 2] ?? 0) << 8) +\n\t\t(buffer[offset + 3] ?? 0)\n\t);\n}\n\nfunction readUint32LE(buffer: Uint8Array, offset: number): number {\n\treturn (\n\t\t(buffer[offset] ?? 0) +\n\t\t((buffer[offset + 1] ?? 0) << 8) +\n\t\t((buffer[offset + 2] ?? 0) << 16) +\n\t\t(buffer[offset + 3] ?? 0) * 0x1000000\n\t);\n}\n\nfunction startsWith(buffer: Uint8Array, bytes: number[]): boolean {\n\tif (buffer.length < bytes.length) return false;\n\treturn bytes.every((byte, index) => buffer[index] === byte);\n}\n\nfunction startsWithAscii(buffer: Uint8Array, offset: number, text: string): boolean {\n\tif (buffer.length < offset + text.length) return false;\n\tfor (let index = 0; index < text.length; index++) {\n\t\tif (buffer[offset + index] !== text.charCodeAt(index)) return false;\n\t}\n\treturn true;\n}\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type ReadToolOptions } from "./read.ts";
|
|
2
|
+
export { type BashExecution, type BashPrepare, type BashToolInput, type BashToolOptions, createBashTool, createPowerShellTool, type PowerShellToolInput, type PowerShellToolOptions, } from "./bash.ts";
|
|
3
|
+
export { createEditTool, type EditToolDetails, type EditToolInput } from "./edit.ts";
|
|
4
|
+
export { DEFAULT_IMAGE_LIMITS, type ImageLimits, type ImageProcessor, imageLimits, type PreparedImage, } from "./image-processor.ts";
|
|
5
|
+
export { createReadTool, type ReadToolDetails, type ReadToolInput, type ReadToolOptions } from "./read.ts";
|
|
6
|
+
export { createWriteTool, type WriteToolInput } from "./write.ts";
|
|
7
|
+
/**
|
|
8
|
+
* The `coding-tools` extension: `read`, `write`, `edit`, and `bash`; nothing installs it automatically.
|
|
9
|
+
* `createPowerShellTool()` adds `powershell`. `images` prepares the images `read` returns (see `ReadToolOptions`).
|
|
10
|
+
*/
|
|
11
|
+
export declare function createCodingTools(options?: ReadToolOptions): import("../index.ts").Extension<import("../index.ts").ToolRegistration<import("@diffra-ai/schema").Schema<{
|
|
12
|
+
path: string;
|
|
13
|
+
content: string;
|
|
14
|
+
}>> | import("../index.ts").ToolRegistration<import("@diffra-ai/schema").Schema<{
|
|
15
|
+
command: string;
|
|
16
|
+
timeout?: number | undefined;
|
|
17
|
+
}>> | import("../index.ts").ToolRegistration<import("@diffra-ai/schema").Schema<{
|
|
18
|
+
path: string;
|
|
19
|
+
offset?: number | undefined;
|
|
20
|
+
limit?: number | undefined;
|
|
21
|
+
}>, import("./read.ts").ReadToolDetails> | import("../index.ts").ToolRegistration<import("@diffra-ai/schema").Schema<{
|
|
22
|
+
path: string;
|
|
23
|
+
edits: {
|
|
24
|
+
oldText: string;
|
|
25
|
+
newText: string;
|
|
26
|
+
}[];
|
|
27
|
+
}>, import("./edit.ts").EditToolDetails>>;
|
|
28
|
+
/** `createCodingTools()` without an image processor. */
|
|
29
|
+
export declare const CodingTools: import("../index.ts").Extension<import("../index.ts").ToolRegistration<import("@diffra-ai/schema").Schema<{
|
|
30
|
+
path: string;
|
|
31
|
+
content: string;
|
|
32
|
+
}>> | import("../index.ts").ToolRegistration<import("@diffra-ai/schema").Schema<{
|
|
33
|
+
command: string;
|
|
34
|
+
timeout?: number | undefined;
|
|
35
|
+
}>> | import("../index.ts").ToolRegistration<import("@diffra-ai/schema").Schema<{
|
|
36
|
+
path: string;
|
|
37
|
+
offset?: number | undefined;
|
|
38
|
+
limit?: number | undefined;
|
|
39
|
+
}>, import("./read.ts").ReadToolDetails> | import("../index.ts").ToolRegistration<import("@diffra-ai/schema").Schema<{
|
|
40
|
+
path: string;
|
|
41
|
+
edits: {
|
|
42
|
+
oldText: string;
|
|
43
|
+
newText: string;
|
|
44
|
+
}[];
|
|
45
|
+
}>, import("./edit.ts").EditToolDetails>>;
|
|
46
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAkB,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAGjE,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,eAAe,EACpB,cAAc,EACd,oBAAoB,EACpB,KAAK,mBAAmB,EACxB,KAAK,qBAAqB,GAC1B,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,cAAc,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE,MAAM,WAAW,CAAC;AACrF,OAAO,EACN,oBAAoB,EACpB,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,WAAW,EACX,KAAK,aAAa,GAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,cAAc,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAC3G,OAAO,EAAE,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAElE;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,eAAoB;;;;;;;;;;;;;;;;0CAK9D;AAED,wDAAwD;AACxD,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;yCAAsB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { defineExtension } from "../harness/define.js";
|
|
2
|
+
import { createBashTool } from "./bash.js";
|
|
3
|
+
import { createEditTool } from "./edit.js";
|
|
4
|
+
import { createReadTool } from "./read.js";
|
|
5
|
+
import { createWriteTool } from "./write.js";
|
|
6
|
+
export { createBashTool, createPowerShellTool, } from "./bash.js";
|
|
7
|
+
export { createEditTool } from "./edit.js";
|
|
8
|
+
export { DEFAULT_IMAGE_LIMITS, imageLimits, } from "./image-processor.js";
|
|
9
|
+
export { createReadTool } from "./read.js";
|
|
10
|
+
export { createWriteTool } from "./write.js";
|
|
11
|
+
/**
|
|
12
|
+
* The `coding-tools` extension: `read`, `write`, `edit`, and `bash`; nothing installs it automatically.
|
|
13
|
+
* `createPowerShellTool()` adds `powershell`. `images` prepares the images `read` returns (see `ReadToolOptions`).
|
|
14
|
+
*/
|
|
15
|
+
export function createCodingTools(options = {}) {
|
|
16
|
+
return defineExtension({
|
|
17
|
+
name: "coding-tools",
|
|
18
|
+
tools: [createReadTool(options), createWriteTool(), createEditTool(), createBashTool()],
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/** `createCodingTools()` without an image processor. */
|
|
22
|
+
export const CodingTools = createCodingTools();
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAwB,MAAM,WAAW,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,OAAO,EAKN,cAAc,EACd,oBAAoB,GAGpB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,cAAc,EAA4C,MAAM,WAAW,CAAC;AACrF,OAAO,EACN,oBAAoB,EAGpB,WAAW,GAEX,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,cAAc,EAAkE,MAAM,WAAW,CAAC;AAC3G,OAAO,EAAE,eAAe,EAAuB,MAAM,YAAY,CAAC;AAElE;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAO,GAAoB,EAAE;IAC9D,OAAO,eAAe,CAAC;QACtB,IAAI,EAAE,cAAc;QACpB,KAAK,EAAE,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,eAAe,EAAE,EAAE,cAAc,EAAE,EAAE,cAAc,EAAE,CAAC;KACvF,CAAC,CAAC;AACJ,CAAC;AAED,wDAAwD;AACxD,MAAM,CAAC,MAAM,WAAW,GAAG,iBAAiB,EAAE,CAAC","sourcesContent":["import { defineExtension } from \"../harness/define.ts\";\nimport { createBashTool } from \"./bash.ts\";\nimport { createEditTool } from \"./edit.ts\";\nimport { createReadTool, type ReadToolOptions } from \"./read.ts\";\nimport { createWriteTool } from \"./write.ts\";\n\nexport {\n\ttype BashExecution,\n\ttype BashPrepare,\n\ttype BashToolInput,\n\ttype BashToolOptions,\n\tcreateBashTool,\n\tcreatePowerShellTool,\n\ttype PowerShellToolInput,\n\ttype PowerShellToolOptions,\n} from \"./bash.ts\";\nexport { createEditTool, type EditToolDetails, type EditToolInput } from \"./edit.ts\";\nexport {\n\tDEFAULT_IMAGE_LIMITS,\n\ttype ImageLimits,\n\ttype ImageProcessor,\n\timageLimits,\n\ttype PreparedImage,\n} from \"./image-processor.ts\";\nexport { createReadTool, type ReadToolDetails, type ReadToolInput, type ReadToolOptions } from \"./read.ts\";\nexport { createWriteTool, type WriteToolInput } from \"./write.ts\";\n\n/**\n * The `coding-tools` extension: `read`, `write`, `edit`, and `bash`; nothing installs it automatically.\n * `createPowerShellTool()` adds `powershell`. `images` prepares the images `read` returns (see `ReadToolOptions`).\n */\nexport function createCodingTools(options: ReadToolOptions = {}) {\n\treturn defineExtension({\n\t\tname: \"coding-tools\",\n\t\ttools: [createReadTool(options), createWriteTool(), createEditTool(), createBashTool()],\n\t});\n}\n\n/** `createCodingTools()` without an image processor. */\nexport const CodingTools = createCodingTools();\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Context } from "@diffra-ai/chord/context";
|
|
2
|
+
import { type ExecutionEnv } from "../env/index.ts";
|
|
3
|
+
export declare function resolveToolPath(env: ExecutionEnv, path: string, context: Context): Promise<string>;
|
|
4
|
+
export declare function resolveReadToolPath(env: ExecutionEnv, path: string, context: Context): Promise<string>;
|
|
5
|
+
//# sourceMappingURL=path-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-utils.d.ts","sourceRoot":"","sources":["../../src/tools/path-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,KAAK,YAAY,EAAc,MAAM,iBAAiB,CAAC;AAUhE,wBAAsB,eAAe,CAAC,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAExG;AAED,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAc5G"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { getOrThrow } from "../env/index.js";
|
|
2
|
+
const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
|
|
3
|
+
const NARROW_NO_BREAK_SPACE = "\u202F";
|
|
4
|
+
function normalizeToolPath(path) {
|
|
5
|
+
const normalized = path.replace(UNICODE_SPACES, " ");
|
|
6
|
+
return normalized.startsWith("@") ? normalized.slice(1) : normalized;
|
|
7
|
+
}
|
|
8
|
+
export async function resolveToolPath(env, path, context) {
|
|
9
|
+
return getOrThrow(await env.absolutePath(normalizeToolPath(path), context));
|
|
10
|
+
}
|
|
11
|
+
export async function resolveReadToolPath(env, path, context) {
|
|
12
|
+
const resolved = await resolveToolPath(env, path, context);
|
|
13
|
+
const variants = [
|
|
14
|
+
resolved,
|
|
15
|
+
resolved.replace(/ (AM|PM)\./gi, `${NARROW_NO_BREAK_SPACE}$1.`),
|
|
16
|
+
resolved.normalize("NFD"),
|
|
17
|
+
resolved.replace(/'/g, "\u2019"),
|
|
18
|
+
resolved.normalize("NFD").replace(/'/g, "\u2019"),
|
|
19
|
+
];
|
|
20
|
+
for (const variant of new Set(variants)) {
|
|
21
|
+
if (getOrThrow(await env.exists(variant, context)))
|
|
22
|
+
return variant;
|
|
23
|
+
}
|
|
24
|
+
return resolved;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=path-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-utils.js","sourceRoot":"","sources":["../../src/tools/path-utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAEhE,MAAM,cAAc,GAAG,0CAA0C,CAAC;AAClE,MAAM,qBAAqB,GAAG,QAAQ,CAAC;AAEvC,SAAS,iBAAiB,CAAC,IAAY;IACtC,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;IACrD,OAAO,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;AACtE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAiB,EAAE,IAAY,EAAE,OAAgB;IACtF,OAAO,UAAU,CAAC,MAAM,GAAG,CAAC,YAAY,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;AAC7E,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,GAAiB,EAAE,IAAY,EAAE,OAAgB;IAC1F,MAAM,QAAQ,GAAG,MAAM,eAAe,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IAC3D,MAAM,QAAQ,GAAG;QAChB,QAAQ;QACR,QAAQ,CAAC,OAAO,CAAC,cAAc,EAAE,GAAG,qBAAqB,KAAK,CAAC;QAC/D,QAAQ,CAAC,SAAS,CAAC,KAAK,CAAC;QACzB,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;QAChC,QAAQ,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;KACjD,CAAC;IAEF,KAAK,MAAM,OAAO,IAAI,IAAI,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QACzC,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAAE,OAAO,OAAO,CAAC;IACpE,CAAC;IACD,OAAO,QAAQ,CAAC;AACjB,CAAC","sourcesContent":["import type { Context } from \"@diffra-ai/chord/context\";\nimport { type ExecutionEnv, getOrThrow } from \"../env/index.ts\";\n\nconst UNICODE_SPACES = /[\\u00A0\\u2000-\\u200A\\u202F\\u205F\\u3000]/g;\nconst NARROW_NO_BREAK_SPACE = \"\\u202F\";\n\nfunction normalizeToolPath(path: string): string {\n\tconst normalized = path.replace(UNICODE_SPACES, \" \");\n\treturn normalized.startsWith(\"@\") ? normalized.slice(1) : normalized;\n}\n\nexport async function resolveToolPath(env: ExecutionEnv, path: string, context: Context): Promise<string> {\n\treturn getOrThrow(await env.absolutePath(normalizeToolPath(path), context));\n}\n\nexport async function resolveReadToolPath(env: ExecutionEnv, path: string, context: Context): Promise<string> {\n\tconst resolved = await resolveToolPath(env, path, context);\n\tconst variants = [\n\t\tresolved,\n\t\tresolved.replace(/ (AM|PM)\\./gi, `${NARROW_NO_BREAK_SPACE}$1.`),\n\t\tresolved.normalize(\"NFD\"),\n\t\tresolved.replace(/'/g, \"\\u2019\"),\n\t\tresolved.normalize(\"NFD\").replace(/'/g, \"\\u2019\"),\n\t];\n\n\tfor (const variant of new Set(variants)) {\n\t\tif (getOrThrow(await env.exists(variant, context))) return variant;\n\t}\n\treturn resolved;\n}\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Static } from "@diffra-ai/schema";
|
|
2
|
+
import type { ToolRegistration } from "../harness/types.ts";
|
|
3
|
+
import { type TruncationResult } from "../truncate.ts";
|
|
4
|
+
import { type ImageProcessor } from "./image-processor.ts";
|
|
5
|
+
declare const readSchema: import("@diffra-ai/schema").Schema<{
|
|
6
|
+
path: string;
|
|
7
|
+
offset?: number | undefined;
|
|
8
|
+
limit?: number | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
export type ReadToolInput = Static<typeof readSchema>;
|
|
11
|
+
export type ReadToolDetails = {
|
|
12
|
+
/** How the shown text was cut; the text itself is the result content. */
|
|
13
|
+
truncation?: Omit<TruncationResult, "content">;
|
|
14
|
+
/**
|
|
15
|
+
* Id of the conversation's model when the image was read, if it has no image input. For UIs only: the model gets
|
|
16
|
+
* diffra-ai's placeholder per request, so the stored result stays right after a switch to a vision model.
|
|
17
|
+
*/
|
|
18
|
+
imageUnsupportedByModel?: string;
|
|
19
|
+
};
|
|
20
|
+
export type ReadToolOptions = {
|
|
21
|
+
/** Resizes and converts images; without one, images within `DEFAULT_IMAGE_LIMITS.maxBytes` pass through as they are. */
|
|
22
|
+
readonly images?: ImageProcessor;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Reads text files and images. Remarks about truncation, continuation, and image processing are diagnostics; the content
|
|
26
|
+
* is only file text, or one image. Programs get the text, or the image's `ImageContent`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createReadTool(options?: ReadToolOptions): ToolRegistration<typeof readSchema, ReadToolDetails>;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=read.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read.d.ts","sourceRoot":"","sources":["../../src/tools/read.ts"],"names":[],"mappings":"AAEA,OAAO,EAAsB,KAAK,MAAM,EAAc,MAAM,mBAAmB,CAAC;AAUhF,OAAO,KAAK,EAAyD,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACnH,OAAO,EAIN,KAAK,gBAAgB,EAGrB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAGN,KAAK,cAAc,EAKnB,MAAM,sBAAsB,CAAC;AAG9B,QAAA,MAAM,UAAU;;;;EAOf,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC;AAWtD,MAAM,MAAM,eAAe,GAAG;IAC7B,yEAAyE;IACzE,UAAU,CAAC,EAAE,IAAI,CAAC,gBAAgB,EAAE,SAAS,CAAC,CAAC;IAC/C;;;OAGG;IACH,uBAAuB,CAAC,EAAE,MAAM,CAAC;CACjC,CAAC;AAmCF,MAAM,MAAM,eAAe,GAAG;IAC7B,wHAAwH;IACxH,QAAQ,CAAC,MAAM,CAAC,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,cAAc,CAAC,OAAO,GAAE,eAAoB,GAAG,gBAAgB,CAAC,OAAO,UAAU,EAAE,eAAe,CAAC,CA2ClH"}
|