@diffra-ai/durable 0.0.0-stage → 0.2.3
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 +442 -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,301 @@
|
|
|
1
|
+
import { SystemEntry } from "../entries.js";
|
|
2
|
+
import { idFromNumber } from "../ids.js";
|
|
3
|
+
const SCAN_PAGE_SIZE = 256;
|
|
4
|
+
const EXCLUDED_STOP_REASONS = new Set(["aborted", "error", "deferred"]);
|
|
5
|
+
const MISSING_RESULT_TEXT = "Tool result unavailable: history ends before this call completed.";
|
|
6
|
+
/**
|
|
7
|
+
* Capture the bounds of the current context, or of the context cut off at the visible entry `at`. The newest entry
|
|
8
|
+
* and head marker are known from the Session's commits, so the current bounds usually cost no Storage read. Run this
|
|
9
|
+
* on the Session line; entries at or below the tail are immutable, so `deriveContext()` can then scan them off the
|
|
10
|
+
* line.
|
|
11
|
+
*/
|
|
12
|
+
export async function captureContextBounds(session, storage, conversationId, context, at,
|
|
13
|
+
/** Set when `at` is known to be visible, as an entry of a range of the conversation is. */
|
|
14
|
+
visible = false) {
|
|
15
|
+
let tail;
|
|
16
|
+
if (at === undefined) {
|
|
17
|
+
tail = await session.latestEntryId(conversationId, context);
|
|
18
|
+
if (tail === undefined)
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
if (!visible && (await storage.entry(conversationId, at, context)) === undefined) {
|
|
23
|
+
throw new Error(`Entry ${at} is not visible from conversation ${conversationId}`);
|
|
24
|
+
}
|
|
25
|
+
tail = at;
|
|
26
|
+
}
|
|
27
|
+
return { head: await session.latestHeadMarker(conversationId, tail, context), tail };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* `readContext()` that reuses `previous`, an earlier range of the same conversation: with the same head marker, only
|
|
31
|
+
* entries after its tail are scanned. Returns the view and the range to pass to the next read. A range outlives the
|
|
32
|
+
* read, so its entries are frozen like `MemoryStorage` records, and the returned view has its own arrays. A caller that
|
|
33
|
+
* keeps the range for `advanceRange()` checks `current` again, synchronously where it installs it: a commit can be
|
|
34
|
+
* published between this function's return and the caller's continuation.
|
|
35
|
+
*/
|
|
36
|
+
export async function readContextFrom(session, storage, conversationId, context, at, previous) {
|
|
37
|
+
const visible = at !== undefined && previous !== undefined && rangeEntry(previous, at) !== undefined;
|
|
38
|
+
const bounds = await session.readOnLine((line) => captureContextBounds(session, line, conversationId, context, at, visible));
|
|
39
|
+
if (bounds === undefined)
|
|
40
|
+
return { view: emptyView(), range: undefined };
|
|
41
|
+
freezeJson(bounds.head);
|
|
42
|
+
const current = at === undefined;
|
|
43
|
+
let range;
|
|
44
|
+
if (previous === undefined || previous.bounds.head?.id !== bounds.head?.id) {
|
|
45
|
+
const entries = freezeJson(await scanRange(storage, conversationId, rangeQuery(bounds), context));
|
|
46
|
+
range = deriveRange(bounds, entries, current);
|
|
47
|
+
}
|
|
48
|
+
else if (bounds.tail === previous.bounds.tail) {
|
|
49
|
+
range = previous;
|
|
50
|
+
}
|
|
51
|
+
else if (bounds.tail < previous.bounds.tail) {
|
|
52
|
+
range = deriveRange(bounds, previous.entries.filter((entry) => entry.id <= bounds.tail), false);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const minEntryId = idFromNumber(previous.bounds.tail + 1);
|
|
56
|
+
const added = await scanRange(storage, conversationId, { minEntryId, maxEntryId: bounds.tail }, context);
|
|
57
|
+
range = extendRange(previous, bounds, freezeJson(added), current);
|
|
58
|
+
}
|
|
59
|
+
// Entries committed while the scan ran off the line are not in the range: it is current only if none was.
|
|
60
|
+
if (range.current && session.knownLatestEntryId(conversationId) !== range.bounds.tail) {
|
|
61
|
+
range = { ...range, current: false };
|
|
62
|
+
}
|
|
63
|
+
const { view } = range;
|
|
64
|
+
return {
|
|
65
|
+
view: {
|
|
66
|
+
...view,
|
|
67
|
+
entries: [...view.entries],
|
|
68
|
+
contributions: [...view.contributions],
|
|
69
|
+
messages: [...view.messages],
|
|
70
|
+
},
|
|
71
|
+
range,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** Committed context of one conversation: bounds captured on the Session line, entries derived off it. */
|
|
75
|
+
export async function readContext(session, storage, conversationId, context, at) {
|
|
76
|
+
const bounds = await session.readOnLine((line) => captureContextBounds(session, line, conversationId, context, at));
|
|
77
|
+
return deriveContext(storage, conversationId, bounds, context);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Derive the active transcript and model context of one conversation within captured bounds.
|
|
81
|
+
*
|
|
82
|
+
* H = newest visible head marker; the range runs from `H.head` (or transcript start) through the tail. Per target,
|
|
83
|
+
* the newest edit in the range wins. Context entries are H followed by the range's non-head entries.
|
|
84
|
+
*/
|
|
85
|
+
export async function deriveContext(storage, conversationId, bounds, context) {
|
|
86
|
+
if (bounds === undefined)
|
|
87
|
+
return emptyView();
|
|
88
|
+
return deriveRange(bounds, await scanRange(storage, conversationId, rangeQuery(bounds), context), false).view;
|
|
89
|
+
}
|
|
90
|
+
function emptyView() {
|
|
91
|
+
return { head: undefined, entries: [], contributions: [], messages: [] };
|
|
92
|
+
}
|
|
93
|
+
/** Entry `id` of `range`, by binary search: a range holds its entries in ID order, every one visible from its conversation. */
|
|
94
|
+
export function rangeEntry(range, id) {
|
|
95
|
+
const entries = range.entries;
|
|
96
|
+
let low = 0;
|
|
97
|
+
let high = entries.length - 1;
|
|
98
|
+
while (low <= high) {
|
|
99
|
+
const middle = (low + high) >>> 1;
|
|
100
|
+
const entry = entries[middle];
|
|
101
|
+
if (entry.id === id)
|
|
102
|
+
return entry;
|
|
103
|
+
if (entry.id < id)
|
|
104
|
+
low = middle + 1;
|
|
105
|
+
else
|
|
106
|
+
high = middle - 1;
|
|
107
|
+
}
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* `range` with the entries one commit appended to its conversation, in ID order, without reading Storage: the
|
|
112
|
+
* conversation's own new entries are its newest, and a new head marker's head never lies before the range's start.
|
|
113
|
+
* A range that is not `current` stays as it is; the next read extends it from Storage. `undefined` drops the range.
|
|
114
|
+
*/
|
|
115
|
+
export function advanceRange(range, appended) {
|
|
116
|
+
if (!range.current || appended.length === 0)
|
|
117
|
+
return range;
|
|
118
|
+
const added = freezeJson(appended);
|
|
119
|
+
let head = range.bounds.head;
|
|
120
|
+
for (const entry of added)
|
|
121
|
+
if (entry.head !== undefined)
|
|
122
|
+
head = entry;
|
|
123
|
+
const bounds = { head, tail: added.at(-1).id };
|
|
124
|
+
if (head?.id === range.bounds.head?.id)
|
|
125
|
+
return extendRange(range, bounds, added, true);
|
|
126
|
+
const start = head.head;
|
|
127
|
+
// Heads only move forward: compaction keeps a later entry, a reset starts at itself, an older head is stale.
|
|
128
|
+
if (range.bounds.head !== undefined && start < range.bounds.head.head)
|
|
129
|
+
return undefined;
|
|
130
|
+
const entries = [...range.entries, ...added].filter((entry) => entry.id >= start);
|
|
131
|
+
return deriveRange(bounds, entries, true);
|
|
132
|
+
}
|
|
133
|
+
/** Derive the context view from the entries scanned within `bounds`. */
|
|
134
|
+
function deriveRange(bounds, entries, current) {
|
|
135
|
+
const edits = new Map();
|
|
136
|
+
// Edits of every entry in the range count, including older head markers that `selectActive()` drops.
|
|
137
|
+
for (const entry of entries)
|
|
138
|
+
for (const edit of entry.edits ?? [])
|
|
139
|
+
edits.set(edit.target, edit);
|
|
140
|
+
const active = selectActive(bounds.head, entries);
|
|
141
|
+
const contributions = active.map((entry) => contribute(entry, edits.get(entry.id)));
|
|
142
|
+
const { settled, open } = settle([], contributions.flat());
|
|
143
|
+
const baseline = findBaseline(bounds.head, active);
|
|
144
|
+
const messages = leadWithSystem([...settled, ...orderToolResults(open)], baseline);
|
|
145
|
+
const view = { head: bounds.head, entries: active, contributions, messages };
|
|
146
|
+
return { bounds, entries, view, edited: new Set(edits.keys()), settled, open, baseline, current };
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* `previous` extended by the entries `added` after its tail under the same head marker: only their contributions and
|
|
150
|
+
* the open messages are derived. An added edit can change an earlier entry, so it derives the whole range again.
|
|
151
|
+
*/
|
|
152
|
+
function extendRange(previous, bounds, added, current) {
|
|
153
|
+
const entries = [...previous.entries, ...added];
|
|
154
|
+
if (added.some((entry) => entry.edits !== undefined || entry.head !== undefined || previous.edited.has(entry.id))) {
|
|
155
|
+
return deriveRange(bounds, entries, current);
|
|
156
|
+
}
|
|
157
|
+
const contributions = added.map((entry) => contribute(entry, undefined));
|
|
158
|
+
const { settled, open } = settle(previous.settled, [...previous.open, ...contributions.flat()]);
|
|
159
|
+
const baseline = previous.baseline !== undefined ? previous.baseline : findBaseline(bounds.head, added);
|
|
160
|
+
const view = {
|
|
161
|
+
head: bounds.head,
|
|
162
|
+
entries: [...previous.view.entries, ...added],
|
|
163
|
+
contributions: [...previous.view.contributions, ...contributions],
|
|
164
|
+
messages: leadWithSystem([...settled, ...orderToolResults(open)], baseline),
|
|
165
|
+
};
|
|
166
|
+
return { bounds, entries, view, edited: previous.edited, settled, open, baseline, current };
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The baseline among `entries`, oldest first: the system message of the first `diffra.system` entry after the head
|
|
170
|
+
* marker, or of the transcript without one, when generation wrote it with every section and tool in effect. After a
|
|
171
|
+
* head marker it always does, and omits the retained `diffra.system` entries. Without one it does when it saw no system
|
|
172
|
+
* message before, which holds unless an earlier entry carries one, even if an edit omits it later. An edit that targets
|
|
173
|
+
* the baseline contributes other messages, or none, so none leads; a later entry does not take its place.
|
|
174
|
+
*/
|
|
175
|
+
function findBaseline(head, entries) {
|
|
176
|
+
for (const entry of entries) {
|
|
177
|
+
if (head !== undefined && entry.id <= head.id)
|
|
178
|
+
continue;
|
|
179
|
+
if (SystemEntry.is(entry))
|
|
180
|
+
return entry.model?.find((message) => message.role === "system") ?? null;
|
|
181
|
+
if (head === undefined && carriesSystemMessage(entry))
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
/** Whether an entry's own messages, or the replacement messages of its edits, include a system message. */
|
|
187
|
+
function carriesSystemMessage(entry) {
|
|
188
|
+
const isSystem = (message) => message.role === "system";
|
|
189
|
+
return ((entry.model?.some(isSystem) ?? false) ||
|
|
190
|
+
(entry.edits?.some((edit) => edit.action === "replace" && edit.messages.some(isSystem)) ?? false));
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Move the first system message to the front when only user messages precede it, or when it is the baseline's. A run's
|
|
194
|
+
* input is committed before generation renders the system prompt, so a transcript starts with the input followed by
|
|
195
|
+
* the baseline, and the range after a compaction with the kept history, assistant messages included. Providers treat
|
|
196
|
+
* only a leading system message as the initial prompt and tool set; without it, a later tool change rewrites the
|
|
197
|
+
* request's tool list and invalidates the whole prompt cache. Only the first system message moves, so replaying
|
|
198
|
+
* system messages in order gives the same sections and tools. Any other system message, before an assistant message,
|
|
199
|
+
* stays where it was committed: moving it would change what it means to the model.
|
|
200
|
+
*/
|
|
201
|
+
function leadWithSystem(messages, baseline) {
|
|
202
|
+
const index = messages.findIndex((message) => message.role === "system");
|
|
203
|
+
if (index <= 0)
|
|
204
|
+
return messages;
|
|
205
|
+
const system = messages[index];
|
|
206
|
+
if (system !== baseline && messages.slice(0, index).some((message) => message.role !== "user")) {
|
|
207
|
+
return messages;
|
|
208
|
+
}
|
|
209
|
+
return [system, ...messages.slice(0, index), ...messages.slice(index + 1)];
|
|
210
|
+
}
|
|
211
|
+
/** One active entry's model messages after its edit and excluded stop reasons, before tool result ordering. */
|
|
212
|
+
function contribute(entry, edit) {
|
|
213
|
+
if (edit?.action === "omit")
|
|
214
|
+
return Object.freeze([]);
|
|
215
|
+
const contributed = edit?.action === "replace" ? edit.messages : (entry.model ?? []);
|
|
216
|
+
return Object.freeze(contributed.filter((message) => message.role !== "assistant" || !EXCLUDED_STOP_REASONS.has(message.stopReason)));
|
|
217
|
+
}
|
|
218
|
+
/** Objects `freezeJson()` froze with everything in them. `Object.isFrozen()` holds for shallow freezes too. */
|
|
219
|
+
const deeplyFrozen = new WeakSet();
|
|
220
|
+
/** Freeze a JSON value and everything in it. */
|
|
221
|
+
function freezeJson(value) {
|
|
222
|
+
if (value === null || typeof value !== "object" || deeplyFrozen.has(value))
|
|
223
|
+
return value;
|
|
224
|
+
for (const child of Object.values(value))
|
|
225
|
+
freezeJson(child);
|
|
226
|
+
Object.freeze(value);
|
|
227
|
+
deeplyFrozen.add(value);
|
|
228
|
+
return value;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Move the ordered messages before the last assistant message of `open` to `settled`. `orderToolResults()` of a
|
|
232
|
+
* sequence equals the concatenation over its parts when each later part starts with an assistant message.
|
|
233
|
+
*/
|
|
234
|
+
function settle(settled, open) {
|
|
235
|
+
const last = open.findLastIndex((message) => message.role === "assistant");
|
|
236
|
+
if (last <= 0)
|
|
237
|
+
return { settled, open };
|
|
238
|
+
return { settled: [...settled, ...orderToolResults(open.slice(0, last))], open: open.slice(last) };
|
|
239
|
+
}
|
|
240
|
+
/** The visible range of `bounds`: from the head marker's head, or transcript start, through the tail. */
|
|
241
|
+
function rangeQuery(bounds) {
|
|
242
|
+
const head = bounds.head;
|
|
243
|
+
return head === undefined ? { maxEntryId: bounds.tail } : { minEntryId: head.head, maxEntryId: bounds.tail };
|
|
244
|
+
}
|
|
245
|
+
/** Visible entries within `range`, oldest first. */
|
|
246
|
+
async function scanRange(storage, conversationId, range, context) {
|
|
247
|
+
const entries = [];
|
|
248
|
+
let cursor;
|
|
249
|
+
do {
|
|
250
|
+
const page = await storage.scanEntries({ conversationId, ...range }, SCAN_PAGE_SIZE, cursor, context);
|
|
251
|
+
entries.push(...page.items);
|
|
252
|
+
cursor = page.next;
|
|
253
|
+
} while (cursor !== undefined);
|
|
254
|
+
return entries.reverse();
|
|
255
|
+
}
|
|
256
|
+
/** The head marker followed by the range's non-head entries, or the whole range without a marker. */
|
|
257
|
+
function selectActive(head, range) {
|
|
258
|
+
return head === undefined ? range : [head, ...range.filter((entry) => entry.head === undefined)];
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Place each assistant's tool results directly after it in call order. Results are taken from the messages before
|
|
262
|
+
* the next assistant; a missing result is synthesized and unmatched results are dropped.
|
|
263
|
+
*/
|
|
264
|
+
export function orderToolResults(messages) {
|
|
265
|
+
const ordered = [];
|
|
266
|
+
for (let index = 0; index < messages.length; index++) {
|
|
267
|
+
const message = messages[index];
|
|
268
|
+
if (message.role === "toolResult")
|
|
269
|
+
continue;
|
|
270
|
+
ordered.push(message);
|
|
271
|
+
if (message.role !== "assistant")
|
|
272
|
+
continue;
|
|
273
|
+
const calls = message.content.filter((content) => content.type === "toolCall");
|
|
274
|
+
if (calls.length === 0)
|
|
275
|
+
continue;
|
|
276
|
+
const results = new Map();
|
|
277
|
+
for (let next = index + 1; next < messages.length && messages[next].role !== "assistant"; next++) {
|
|
278
|
+
const candidate = messages[next];
|
|
279
|
+
if (candidate.role === "toolResult" && !results.has(candidate.toolCallId)) {
|
|
280
|
+
results.set(candidate.toolCallId, next);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
for (const call of calls) {
|
|
284
|
+
const resultIndex = results.get(call.id);
|
|
285
|
+
ordered.push(resultIndex === undefined ? missingResult(call, message.timestamp) : messages[resultIndex]);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return ordered;
|
|
289
|
+
}
|
|
290
|
+
function missingResult(call, timestamp) {
|
|
291
|
+
return freezeJson({
|
|
292
|
+
role: "toolResult",
|
|
293
|
+
toolCallId: call.id,
|
|
294
|
+
toolName: call.name,
|
|
295
|
+
content: [{ type: "text", text: MISSING_RESULT_TEXT }],
|
|
296
|
+
isError: true,
|
|
297
|
+
details: { reason: "missing_result" },
|
|
298
|
+
timestamp,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/harness/context.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,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,SAAS,GAAG,QAAQ,CAAC,IAAI,CAAE,CAAC;YAClC,IAAI,SAAS,CAAC,IAAI,KAAK,YAAY,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC3E,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;YACzC,CAAC;QACF,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YAC1B,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACzC,OAAO,CAAC,IAAI,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAE,CAAC,CAAC;QAC3G,CAAC;IACF,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC;AAED,SAAS,aAAa,CAAC,IAAc,EAAE,SAAiB;IACvD,OAAO,UAAU,CAAC;QACjB,IAAI,EAAE,YAAY;QAClB,UAAU,EAAE,IAAI,CAAC,EAAE;QACnB,QAAQ,EAAE,IAAI,CAAC,IAAI;QACnB,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,mBAAmB,EAAE,CAAC;QACtD,OAAO,EAAE,IAAI;QACb,OAAO,EAAE,EAAE,MAAM,EAAE,gBAAgB,EAAE;QACrC,SAAS;KACT,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { AssistantMessage, Message, ToolCall, ToolResultMessage } from \"@diffra-ai/ai\";\nimport type { Context } from \"@diffra-ai/chord/context\";\nimport { SystemEntry } from \"../entries.ts\";\nimport { idFromNumber } from \"../ids.ts\";\nimport type { SessionImpl } from \"../session/session.ts\";\nimport type { ContextEdit, ConversationId, Cursor, EntryId, EntryQuery, EntryRecord, Storage } from \"../types.ts\";\nimport type { ContextView } from \"./types.ts\";\n\nconst SCAN_PAGE_SIZE = 256;\nconst EXCLUDED_STOP_REASONS: ReadonlySet<AssistantMessage[\"stopReason\"]> = new Set([\"aborted\", \"error\", \"deferred\"]);\nconst MISSING_RESULT_TEXT = \"Tool result unavailable: history ends before this call completed.\";\n\n/** Head marker and newest visible entry that fix one committed context range. */\nexport type ContextBounds = {\n\treadonly head: (EntryRecord & { readonly head: EntryId }) | undefined;\n\treadonly tail: EntryId;\n};\n\n/**\n * Capture the bounds of the current context, or of the context cut off at the visible entry `at`. The newest entry\n * and head marker are known from the Session's commits, so the current bounds usually cost no Storage read. Run this\n * on the Session line; entries at or below the tail are immutable, so `deriveContext()` can then scan them off the\n * line.\n */\nexport async function captureContextBounds(\n\tsession: SessionImpl,\n\tstorage: Storage,\n\tconversationId: ConversationId,\n\tcontext: Context,\n\tat?: EntryId,\n\t/** Set when `at` is known to be visible, as an entry of a range of the conversation is. */\n\tvisible = false,\n): Promise<ContextBounds | undefined> {\n\tlet tail: EntryId | undefined;\n\tif (at === undefined) {\n\t\ttail = await session.latestEntryId(conversationId, context);\n\t\tif (tail === undefined) return undefined;\n\t} else {\n\t\tif (!visible && (await storage.entry(conversationId, at, context)) === undefined) {\n\t\t\tthrow new Error(`Entry ${at} is not visible from conversation ${conversationId}`);\n\t\t}\n\t\ttail = at;\n\t}\n\treturn { head: await session.latestHeadMarker(conversationId, tail, context), tail };\n}\n\n/**\n * One context read: the visible entries it scanned, from the head marker's head, or transcript start, through\n * `bounds.tail`, oldest first, and the view derived from them. Entries at or below the tail never change, so a later\n * read with the same head marker extends both.\n */\nexport type ContextRange = {\n\treadonly bounds: ContextBounds;\n\treadonly entries: readonly EntryRecord[];\n\treadonly view: ContextView;\n\t/** Targets of the edits in `entries`. */\n\treadonly edited: ReadonlySet<EntryId>;\n\t/**\n\t * `view.messages` from the contributed messages before the last assistant message. Tool results are ordered within\n\t * the messages up to the next assistant message, so later entries cannot change these.\n\t */\n\treadonly settled: readonly Message[];\n\t/** Contributed messages from the last assistant message on, before tool result ordering. */\n\treadonly open: readonly Message[];\n\t/** The baseline's system message; see `findBaseline()`. */\n\treadonly baseline: Baseline;\n\t/**\n\t * Whether the range reached the conversation's newest entry when it was read, so the entries of later commits extend\n\t * it (`advanceRange()`); a range cut off at an earlier entry is not.\n\t */\n\treadonly current: boolean;\n};\n\n/** A baseline's system message, `null` when the range cannot have one, or `undefined` while a later entry may be one. */\ntype Baseline = Message | null | undefined;\n\n/**\n * `readContext()` that reuses `previous`, an earlier range of the same conversation: with the same head marker, only\n * entries after its tail are scanned. Returns the view and the range to pass to the next read. A range outlives the\n * read, so its entries are frozen like `MemoryStorage` records, and the returned view has its own arrays. A caller that\n * keeps the range for `advanceRange()` checks `current` again, synchronously where it installs it: a commit can be\n * published between this function's return and the caller's continuation.\n */\nexport async function readContextFrom(\n\tsession: SessionImpl,\n\tstorage: Storage,\n\tconversationId: ConversationId,\n\tcontext: Context,\n\tat: EntryId | undefined,\n\tprevious: ContextRange | undefined,\n): Promise<{ readonly view: ContextView; readonly range: ContextRange | undefined }> {\n\tconst visible = at !== undefined && previous !== undefined && rangeEntry(previous, at) !== undefined;\n\tconst bounds = await session.readOnLine((line) =>\n\t\tcaptureContextBounds(session, line, conversationId, context, at, visible),\n\t);\n\tif (bounds === undefined) return { view: emptyView(), range: undefined };\n\tfreezeJson(bounds.head);\n\tconst current = at === undefined;\n\tlet range: ContextRange;\n\tif (previous === undefined || previous.bounds.head?.id !== bounds.head?.id) {\n\t\tconst entries = freezeJson(await scanRange(storage, conversationId, rangeQuery(bounds), context));\n\t\trange = deriveRange(bounds, entries, current);\n\t} else if (bounds.tail === previous.bounds.tail) {\n\t\trange = previous;\n\t} else if (bounds.tail < previous.bounds.tail) {\n\t\trange = deriveRange(\n\t\t\tbounds,\n\t\t\tprevious.entries.filter((entry) => entry.id <= bounds.tail),\n\t\t\tfalse,\n\t\t);\n\t} else {\n\t\tconst minEntryId = idFromNumber<EntryId>(previous.bounds.tail + 1);\n\t\tconst added = await scanRange(storage, conversationId, { minEntryId, maxEntryId: bounds.tail }, context);\n\t\trange = extendRange(previous, bounds, freezeJson(added), current);\n\t}\n\t// Entries committed while the scan ran off the line are not in the range: it is current only if none was.\n\tif (range.current && session.knownLatestEntryId(conversationId) !== range.bounds.tail) {\n\t\trange = { ...range, current: false };\n\t}\n\tconst { view } = range;\n\treturn {\n\t\tview: {\n\t\t\t...view,\n\t\t\tentries: [...view.entries],\n\t\t\tcontributions: [...view.contributions],\n\t\t\tmessages: [...view.messages],\n\t\t},\n\t\trange,\n\t};\n}\n\n/** Committed context of one conversation: bounds captured on the Session line, entries derived off it. */\nexport async function readContext(\n\tsession: SessionImpl,\n\tstorage: Storage,\n\tconversationId: ConversationId,\n\tcontext: Context,\n\tat?: EntryId,\n): Promise<ContextView> {\n\tconst bounds = await session.readOnLine((line) => captureContextBounds(session, line, conversationId, context, at));\n\treturn deriveContext(storage, conversationId, bounds, context);\n}\n\n/**\n * Derive the active transcript and model context of one conversation within captured bounds.\n *\n * H = newest visible head marker; the range runs from `H.head` (or transcript start) through the tail. Per target,\n * the newest edit in the range wins. Context entries are H followed by the range's non-head entries.\n */\nexport async function deriveContext(\n\tstorage: Storage,\n\tconversationId: ConversationId,\n\tbounds: ContextBounds | undefined,\n\tcontext: Context,\n): Promise<ContextView> {\n\tif (bounds === undefined) return emptyView();\n\treturn deriveRange(bounds, await scanRange(storage, conversationId, rangeQuery(bounds), context), false).view;\n}\n\nfunction emptyView(): ContextView {\n\treturn { head: undefined, entries: [], contributions: [], messages: [] };\n}\n\n/** Entry `id` of `range`, by binary search: a range holds its entries in ID order, every one visible from its conversation. */\nexport function rangeEntry(range: ContextRange, id: EntryId): EntryRecord | undefined {\n\tconst entries = range.entries;\n\tlet low = 0;\n\tlet high = entries.length - 1;\n\twhile (low <= high) {\n\t\tconst middle = (low + high) >>> 1;\n\t\tconst entry = entries[middle]!;\n\t\tif (entry.id === id) return entry;\n\t\tif (entry.id < id) low = middle + 1;\n\t\telse high = middle - 1;\n\t}\n\treturn undefined;\n}\n\n/**\n * `range` with the entries one commit appended to its conversation, in ID order, without reading Storage: the\n * conversation's own new entries are its newest, and a new head marker's head never lies before the range's start.\n * A range that is not `current` stays as it is; the next read extends it from Storage. `undefined` drops the range.\n */\nexport function advanceRange(range: ContextRange, appended: readonly EntryRecord[]): ContextRange | undefined {\n\tif (!range.current || appended.length === 0) return range;\n\tconst added = freezeJson(appended);\n\tlet head = range.bounds.head;\n\tfor (const entry of added) if (entry.head !== undefined) head = entry as typeof head;\n\tconst bounds = { head, tail: added.at(-1)!.id };\n\tif (head?.id === range.bounds.head?.id) return extendRange(range, bounds, added, true);\n\tconst start = head!.head;\n\t// Heads only move forward: compaction keeps a later entry, a reset starts at itself, an older head is stale.\n\tif (range.bounds.head !== undefined && start < range.bounds.head.head) return undefined;\n\tconst entries = [...range.entries, ...added].filter((entry) => entry.id >= start);\n\treturn deriveRange(bounds, entries, true);\n}\n\n/** Derive the context view from the entries scanned within `bounds`. */\nfunction deriveRange(bounds: ContextBounds, entries: readonly EntryRecord[], current: boolean): ContextRange {\n\tconst edits = new Map<EntryId, ContextEdit>();\n\t// Edits of every entry in the range count, including older head markers that `selectActive()` drops.\n\tfor (const entry of entries) for (const edit of entry.edits ?? []) edits.set(edit.target, edit);\n\tconst active = selectActive(bounds.head, entries);\n\tconst contributions = active.map((entry) => contribute(entry, edits.get(entry.id)));\n\tconst { settled, open } = settle([], contributions.flat());\n\tconst baseline = findBaseline(bounds.head, active);\n\tconst messages = leadWithSystem([...settled, ...orderToolResults(open)], baseline);\n\tconst view = { head: bounds.head, entries: active, contributions, messages };\n\treturn { bounds, entries, view, edited: new Set(edits.keys()), settled, open, baseline, current };\n}\n\n/**\n * `previous` extended by the entries `added` after its tail under the same head marker: only their contributions and\n * the open messages are derived. An added edit can change an earlier entry, so it derives the whole range again.\n */\nfunction extendRange(\n\tprevious: ContextRange,\n\tbounds: ContextBounds,\n\tadded: readonly EntryRecord[],\n\tcurrent: boolean,\n): ContextRange {\n\tconst entries = [...previous.entries, ...added];\n\tif (added.some((entry) => entry.edits !== undefined || entry.head !== undefined || previous.edited.has(entry.id))) {\n\t\treturn deriveRange(bounds, entries, current);\n\t}\n\tconst contributions = added.map((entry) => contribute(entry, undefined));\n\tconst { settled, open } = settle(previous.settled, [...previous.open, ...contributions.flat()]);\n\tconst baseline = previous.baseline !== undefined ? previous.baseline : findBaseline(bounds.head, added);\n\tconst view = {\n\t\thead: bounds.head,\n\t\tentries: [...previous.view.entries, ...added],\n\t\tcontributions: [...previous.view.contributions, ...contributions],\n\t\tmessages: leadWithSystem([...settled, ...orderToolResults(open)], baseline),\n\t};\n\treturn { bounds, entries, view, edited: previous.edited, settled, open, baseline, current };\n}\n\n/**\n * The baseline among `entries`, oldest first: the system message of the first `diffra.system` entry after the head\n * marker, or of the transcript without one, when generation wrote it with every section and tool in effect. After a\n * head marker it always does, and omits the retained `diffra.system` entries. Without one it does when it saw no system\n * message before, which holds unless an earlier entry carries one, even if an edit omits it later. An edit that targets\n * the baseline contributes other messages, or none, so none leads; a later entry does not take its place.\n */\nfunction findBaseline(head: ContextBounds[\"head\"], entries: readonly EntryRecord[]): Baseline {\n\tfor (const entry of entries) {\n\t\tif (head !== undefined && entry.id <= head.id) continue;\n\t\tif (SystemEntry.is(entry)) return entry.model?.find((message) => message.role === \"system\") ?? null;\n\t\tif (head === undefined && carriesSystemMessage(entry)) return null;\n\t}\n\treturn undefined;\n}\n\n/** Whether an entry's own messages, or the replacement messages of its edits, include a system message. */\nfunction carriesSystemMessage(entry: EntryRecord): boolean {\n\tconst isSystem = (message: Message) => message.role === \"system\";\n\treturn (\n\t\t(entry.model?.some(isSystem) ?? false) ||\n\t\t(entry.edits?.some((edit) => edit.action === \"replace\" && edit.messages.some(isSystem)) ?? false)\n\t);\n}\n\n/**\n * Move the first system message to the front when only user messages precede it, or when it is the baseline's. A run's\n * input is committed before generation renders the system prompt, so a transcript starts with the input followed by\n * the baseline, and the range after a compaction with the kept history, assistant messages included. Providers treat\n * only a leading system message as the initial prompt and tool set; without it, a later tool change rewrites the\n * request's tool list and invalidates the whole prompt cache. Only the first system message moves, so replaying\n * system messages in order gives the same sections and tools. Any other system message, before an assistant message,\n * stays where it was committed: moving it would change what it means to the model.\n */\nfunction leadWithSystem(messages: Message[], baseline: Baseline): Message[] {\n\tconst index = messages.findIndex((message) => message.role === \"system\");\n\tif (index <= 0) return messages;\n\tconst system = messages[index]!;\n\tif (system !== baseline && messages.slice(0, index).some((message) => message.role !== \"user\")) {\n\t\treturn messages;\n\t}\n\treturn [system, ...messages.slice(0, index), ...messages.slice(index + 1)];\n}\n\n/** One active entry's model messages after its edit and excluded stop reasons, before tool result ordering. */\nfunction contribute(entry: EntryRecord, edit: ContextEdit | undefined): readonly Message[] {\n\tif (edit?.action === \"omit\") return Object.freeze([]);\n\tconst contributed = edit?.action === \"replace\" ? edit.messages : (entry.model ?? []);\n\treturn Object.freeze(\n\t\tcontributed.filter((message) => message.role !== \"assistant\" || !EXCLUDED_STOP_REASONS.has(message.stopReason)),\n\t);\n}\n\n/** Objects `freezeJson()` froze with everything in them. `Object.isFrozen()` holds for shallow freezes too. */\nconst deeplyFrozen = new WeakSet<object>();\n\n/** Freeze a JSON value and everything in it. */\nfunction freezeJson<T>(value: T): T {\n\tif (value === null || typeof value !== \"object\" || deeplyFrozen.has(value)) return value;\n\tfor (const child of Object.values(value)) freezeJson(child);\n\tObject.freeze(value);\n\tdeeplyFrozen.add(value);\n\treturn value;\n}\n\n/**\n * Move the ordered messages before the last assistant message of `open` to `settled`. `orderToolResults()` of a\n * sequence equals the concatenation over its parts when each later part starts with an assistant message.\n */\nfunction settle(\n\tsettled: readonly Message[],\n\topen: readonly Message[],\n): { readonly settled: readonly Message[]; readonly open: readonly Message[] } {\n\tconst last = open.findLastIndex((message) => message.role === \"assistant\");\n\tif (last <= 0) return { settled, open };\n\treturn { settled: [...settled, ...orderToolResults(open.slice(0, last))], open: open.slice(last) };\n}\n\n/** The visible range of `bounds`: from the head marker's head, or transcript start, through the tail. */\nfunction rangeQuery(bounds: ContextBounds): Omit<EntryQuery, \"conversationId\"> {\n\tconst head = bounds.head;\n\treturn head === undefined ? { maxEntryId: bounds.tail } : { minEntryId: head.head, maxEntryId: bounds.tail };\n}\n\n/** Visible entries within `range`, oldest first. */\nasync function scanRange(\n\tstorage: Storage,\n\tconversationId: ConversationId,\n\trange: Omit<EntryQuery, \"conversationId\">,\n\tcontext: Context,\n): Promise<EntryRecord[]> {\n\tconst entries: EntryRecord[] = [];\n\tlet cursor: Cursor | undefined;\n\tdo {\n\t\tconst page = await storage.scanEntries({ conversationId, ...range }, SCAN_PAGE_SIZE, cursor, context);\n\t\tentries.push(...page.items);\n\t\tcursor = page.next;\n\t} while (cursor !== undefined);\n\treturn entries.reverse();\n}\n\n/** The head marker followed by the range's non-head entries, or the whole range without a marker. */\nfunction selectActive(head: ContextBounds[\"head\"], range: readonly EntryRecord[]): readonly EntryRecord[] {\n\treturn head === undefined ? range : [head, ...range.filter((entry) => entry.head === undefined)];\n}\n\n/**\n * Place each assistant's tool results directly after it in call order. Results are taken from the messages before\n * the next assistant; a missing result is synthesized and unmatched results are dropped.\n */\nexport function orderToolResults(messages: readonly Message[]): Message[] {\n\tconst ordered: Message[] = [];\n\tfor (let index = 0; index < messages.length; index++) {\n\t\tconst message = messages[index]!;\n\t\tif (message.role === \"toolResult\") continue;\n\t\tordered.push(message);\n\t\tif (message.role !== \"assistant\") continue;\n\t\tconst calls = message.content.filter((content): content is ToolCall => content.type === \"toolCall\");\n\t\tif (calls.length === 0) continue;\n\t\tconst results = new Map<string, number>();\n\t\tfor (let next = index + 1; next < messages.length && messages[next]!.role !== \"assistant\"; next++) {\n\t\t\tconst candidate = messages[next]!;\n\t\t\tif (candidate.role === \"toolResult\" && !results.has(candidate.toolCallId)) {\n\t\t\t\tresults.set(candidate.toolCallId, next);\n\t\t\t}\n\t\t}\n\t\tfor (const call of calls) {\n\t\t\tconst resultIndex = results.get(call.id);\n\t\t\tordered.push(resultIndex === undefined ? missingResult(call, message.timestamp) : messages[resultIndex]!);\n\t\t}\n\t}\n\treturn ordered;\n}\n\nfunction missingResult(call: ToolCall, timestamp: number): ToolResultMessage {\n\treturn freezeJson({\n\t\trole: \"toolResult\",\n\t\ttoolCallId: call.id,\n\t\ttoolName: call.name,\n\t\tcontent: [{ type: \"text\", text: MISSING_RESULT_TEXT }],\n\t\tisError: true,\n\t\tdetails: { reason: \"missing_result\" },\n\t\ttimestamp,\n\t});\n}\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { JsonValue } from "@diffra-ai/chord/json";
|
|
2
|
+
import type { Task } from "../types.ts";
|
|
3
|
+
import type { AnyTask, Extension, HookRegistration, HooksOf, HookTask, PromptSection, ToolRegistration, Wrap } from "./types.ts";
|
|
4
|
+
/** Identity function that types an extension. */
|
|
5
|
+
export declare function defineExtension<Tool extends ToolRegistration = ToolRegistration>(extension: Extension<Tool>): Extension<Tool>;
|
|
6
|
+
/** Identity function that types a tool: `args` from `parameters`, details from what it reports, `C` its park checkpoint. */
|
|
7
|
+
export declare function defineTool<TParameters extends object, TDetails extends JsonValue = JsonValue, C extends JsonValue = JsonValue>(tool: ToolRegistration<TParameters, TDetails, C>): ToolRegistration<TParameters, TDetails, C>;
|
|
8
|
+
/** A prompt section; tagged unless `tag` is false. */
|
|
9
|
+
export declare function section<Tool extends ToolRegistration = ToolRegistration>(key: string, render: PromptSection<Tool>["render"], options?: {
|
|
10
|
+
readonly tag?: boolean;
|
|
11
|
+
}): PromptSection<Tool>;
|
|
12
|
+
/** Hook handlers for tasks with `task`'s name. */
|
|
13
|
+
export declare function hook<K extends AnyTask>(task: K, handlers: Partial<HooksOf<K>>): HookRegistration;
|
|
14
|
+
/** A task a hook (`afterTools`, `onYield`, `afterRun`) asks for, its input typed by the task. */
|
|
15
|
+
export declare function hookTask<I extends JsonValue, S extends {
|
|
16
|
+
phase: string;
|
|
17
|
+
}, R, H extends object>(task: Task<I, S, R, H>, input: I): HookTask;
|
|
18
|
+
/**
|
|
19
|
+
* Wrap the tool named like `tool` wherever the wrapping extension is selected. Wrappers never park: one passes `api` to
|
|
20
|
+
* its base unchanged or spread and returns the base's park unchanged; a park of its own ends the call `invalid_park`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function wrapTool<Tool extends ToolRegistration>(tool: Tool, wrapper: (tool: Tool) => Tool): Wrap<Tool>;
|
|
23
|
+
/** Wrap the section `key` wherever the wrapping extension is selected. */
|
|
24
|
+
export declare function wrapSection<Tool extends ToolRegistration = ToolRegistration>(key: string, wrapper: (section: PromptSection<Tool>) => PromptSection<Tool>): Wrap<Tool>;
|
|
25
|
+
//# sourceMappingURL=define.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../../src/harness/define.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,KAAK,EACX,OAAO,EACP,SAAS,EACT,gBAAgB,EAChB,OAAO,EACP,QAAQ,EACR,aAAa,EACb,gBAAgB,EAChB,IAAI,EACJ,MAAM,YAAY,CAAC;AAEpB,iDAAiD;AACjD,wBAAgB,eAAe,CAAC,IAAI,SAAS,gBAAgB,GAAG,gBAAgB,EAC/E,SAAS,EAAE,SAAS,CAAC,IAAI,CAAC,GACxB,SAAS,CAAC,IAAI,CAAC,CAEjB;AAED,4HAA4H;AAC5H,wBAAgB,UAAU,CACzB,WAAW,SAAS,MAAM,EAC1B,QAAQ,SAAS,SAAS,GAAG,SAAS,EACtC,CAAC,SAAS,SAAS,GAAG,SAAS,EAC9B,IAAI,EAAE,gBAAgB,CAAC,WAAW,EAAE,QAAQ,EAAE,CAAC,CAAC,GAAG,gBAAgB,CAAC,WAAW,EAAE,QAAQ,EAAE,CAAC,CAAC,CA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+
}
|
|
5
|
+
/** Identity function that types a tool: `args` from `parameters`, details from what it reports, `C` its park checkpoint. */
|
|
6
|
+
export function defineTool(tool) {
|
|
7
|
+
return tool;
|
|
8
|
+
}
|
|
9
|
+
/** A prompt section; tagged unless `tag` is false. */
|
|
10
|
+
export function section(key, render, options) {
|
|
11
|
+
return options?.tag === undefined ? { key, render } : { key, render, tag: options.tag };
|
|
12
|
+
}
|
|
13
|
+
/** Hook handlers for tasks with `task`'s name. */
|
|
14
|
+
export function hook(task, handlers) {
|
|
15
|
+
return { task: task.definition.name, handlers };
|
|
16
|
+
}
|
|
17
|
+
/** A task a hook (`afterTools`, `onYield`, `afterRun`) asks for, its input typed by the task. */
|
|
18
|
+
export function hookTask(task, input) {
|
|
19
|
+
return { task, input };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Wrap the tool named like `tool` wherever the wrapping extension is selected. Wrappers never park: one passes `api` to
|
|
23
|
+
* its base unchanged or spread and returns the base's park unchanged; a park of its own ends the call `invalid_park`.
|
|
24
|
+
*/
|
|
25
|
+
export function wrapTool(tool, wrapper) {
|
|
26
|
+
return { kind: "tool", tool: tool.name, wrap: wrapper };
|
|
27
|
+
}
|
|
28
|
+
/** Wrap the section `key` wherever the wrapping extension is selected. */
|
|
29
|
+
export function wrapSection(key, wrapper) {
|
|
30
|
+
return { kind: "section", section: key, wrap: wrapper };
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=define.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.js","sourceRoot":"","sources":["../../src/harness/define.ts"],"names":[],"mappings":"AAaA,iDAAiD;AACjD,MAAM,UAAU,eAAe,CAC9B,SAA0B;IAE1B,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,4HAA4H;AAC5H,MAAM,UAAU,UAAU,CAIxB,IAAgD;IACjD,OAAO,IAAI,CAAC;AACb,CAAC;AAED,sDAAsD;AACtD,MAAM,UAAU,OAAO,CACtB,GAAW,EACX,MAAqC,EACrC,OAAoC;IAEpC,OAAO,OAAO,EAAE,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC;AACzF,CAAC;AAED,kDAAkD;AAClD,MAAM,UAAU,IAAI,CAAoB,IAAO,EAAE,QAA6B;IAC7E,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,QAAQ,EAAE,CAAC;AACjD,CAAC;AAED,iGAAiG;AACjG,MAAM,UAAU,QAAQ,CACvB,IAAsB,EACtB,KAAQ;IAER,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AACxB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,QAAQ,CAAgC,IAAU,EAAE,OAA6B;IAChG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AACzD,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,WAAW,CAC1B,GAAW,EACX,OAA8D;IAE9D,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AACzD,CAAC","sourcesContent":["import type { JsonValue } from \"@diffra-ai/chord/json\";\nimport type { Task } from \"../types.ts\";\nimport type {\n\tAnyTask,\n\tExtension,\n\tHookRegistration,\n\tHooksOf,\n\tHookTask,\n\tPromptSection,\n\tToolRegistration,\n\tWrap,\n} from \"./types.ts\";\n\n/** Identity function that types an extension. */\nexport function defineExtension<Tool extends ToolRegistration = ToolRegistration>(\n\textension: Extension<Tool>,\n): Extension<Tool> {\n\treturn extension;\n}\n\n/** Identity function that types a tool: `args` from `parameters`, details from what it reports, `C` its park checkpoint. */\nexport function defineTool<\n\tTParameters extends object,\n\tTDetails extends JsonValue = JsonValue,\n\tC extends JsonValue = JsonValue,\n>(tool: ToolRegistration<TParameters, TDetails, C>): ToolRegistration<TParameters, TDetails, C> {\n\treturn tool;\n}\n\n/** A prompt section; tagged unless `tag` is false. */\nexport function section<Tool extends ToolRegistration = ToolRegistration>(\n\tkey: string,\n\trender: PromptSection<Tool>[\"render\"],\n\toptions?: { readonly tag?: boolean },\n): PromptSection<Tool> {\n\treturn options?.tag === undefined ? { key, render } : { key, render, tag: options.tag };\n}\n\n/** Hook handlers for tasks with `task`'s name. */\nexport function hook<K extends AnyTask>(task: K, handlers: Partial<HooksOf<K>>): HookRegistration {\n\treturn { task: task.definition.name, handlers };\n}\n\n/** A task a hook (`afterTools`, `onYield`, `afterRun`) asks for, its input typed by the task. */\nexport function hookTask<I extends JsonValue, S extends { phase: string }, R, H extends object>(\n\ttask: Task<I, S, R, H>,\n\tinput: I,\n): HookTask {\n\treturn { task, input };\n}\n\n/**\n * Wrap the tool named like `tool` wherever the wrapping extension is selected. Wrappers never park: one passes `api` to\n * its base unchanged or spread and returns the base's park unchanged; a park of its own ends the call `invalid_park`.\n */\nexport function wrapTool<Tool extends ToolRegistration>(tool: Tool, wrapper: (tool: Tool) => Tool): Wrap<Tool> {\n\treturn { kind: \"tool\", tool: tool.name, wrap: wrapper };\n}\n\n/** Wrap the section `key` wherever the wrapping extension is selected. */\nexport function wrapSection<Tool extends ToolRegistration = ToolRegistration>(\n\tkey: string,\n\twrapper: (section: PromptSection<Tool>) => PromptSection<Tool>,\n): Wrap<Tool> {\n\treturn { kind: \"section\", section: key, wrap: wrapper };\n}\n"]}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { DeferredHandle, ModelThinkingLevel } from "@diffra-ai/ai";
|
|
2
|
+
import type { Draft } from "@diffra-ai/chord/delta";
|
|
3
|
+
import type { ConversationId, EntryId, SubmissionId, Task, TaskId, Tx } from "../types.ts";
|
|
4
|
+
import { type LiveState } from "./live.ts";
|
|
5
|
+
import { type ToolTaskResult } from "./tool.ts";
|
|
6
|
+
import type { CompactionResult, ConversationStreamOptions, GenerationHooks, ModelRef, RunSummary } from "./types.ts";
|
|
7
|
+
export type GenerationInput = Record<string, never>;
|
|
8
|
+
export type GenerationCheckpoint = {
|
|
9
|
+
phase: "prepare";
|
|
10
|
+
attempt: number;
|
|
11
|
+
/** The blocking compaction this generation waited for; it starts no other compaction (spec §8.3). */
|
|
12
|
+
compacted?: TaskId<CompactionResult>;
|
|
13
|
+
/** Error text of the overflow that started `compacted`; checked once when `prepare` resumes. */
|
|
14
|
+
overflow?: string;
|
|
15
|
+
/** Set when the blocking threshold started `compacted`; its outcome is checked once when `prepare` resumes. */
|
|
16
|
+
threshold?: true;
|
|
17
|
+
} | {
|
|
18
|
+
phase: "request";
|
|
19
|
+
attempt: number;
|
|
20
|
+
compacted?: TaskId<CompactionResult>;
|
|
21
|
+
model: ModelRef;
|
|
22
|
+
thinkingLevel: ModelThinkingLevel;
|
|
23
|
+
/** The settings' request options when preparation committed; a resend after recovery uses them unchanged. */
|
|
24
|
+
streamOptions: ConversationStreamOptions;
|
|
25
|
+
/** Newest entry included in the request. */
|
|
26
|
+
cutoff: EntryId;
|
|
27
|
+
} | {
|
|
28
|
+
phase: "retry";
|
|
29
|
+
attempt: number;
|
|
30
|
+
compacted?: TaskId<CompactionResult>;
|
|
31
|
+
until: number;
|
|
32
|
+
/**
|
|
33
|
+
* The deferred response a poll was fetching when the host died: the next attempt polls it again instead of
|
|
34
|
+
* sending another request, so the provider's job is neither lost nor paid twice. Absent otherwise.
|
|
35
|
+
*/
|
|
36
|
+
poll?: {
|
|
37
|
+
model: ModelRef;
|
|
38
|
+
thinkingLevel?: ModelThinkingLevel;
|
|
39
|
+
cutoff: EntryId;
|
|
40
|
+
handle: DeferredHandle;
|
|
41
|
+
};
|
|
42
|
+
} | {
|
|
43
|
+
phase: "poll";
|
|
44
|
+
attempt: number;
|
|
45
|
+
compacted?: TaskId<CompactionResult>;
|
|
46
|
+
model: ModelRef;
|
|
47
|
+
/** Absent in checkpoints of earlier versions. */
|
|
48
|
+
thinkingLevel?: ModelThinkingLevel;
|
|
49
|
+
cutoff: EntryId;
|
|
50
|
+
handle: DeferredHandle;
|
|
51
|
+
pollAt: number;
|
|
52
|
+
} | {
|
|
53
|
+
/** Waiting on the round's tool tasks, which the generation owns (spec §8.5). */
|
|
54
|
+
phase: "tools";
|
|
55
|
+
/** The tool-calling answer. */
|
|
56
|
+
assistant: EntryId;
|
|
57
|
+
/** Tool tasks created so far, in call order; grows by one per started call of a sequential round. */
|
|
58
|
+
tools: TaskId<ToolTaskResult>[];
|
|
59
|
+
/** Calls of a sequential round not started yet, in call order. */
|
|
60
|
+
pending: string[];
|
|
61
|
+
} | {
|
|
62
|
+
/**
|
|
63
|
+
* The run answered: its answer is committed and its inputs settled, and it keeps `diffra.live.run` while `afterRun`
|
|
64
|
+
* hooks are asked for tasks (spec §8.3). A crash here asks them again: nothing was created yet.
|
|
65
|
+
*/
|
|
66
|
+
phase: "finish";
|
|
67
|
+
run: RunSummary;
|
|
68
|
+
} | {
|
|
69
|
+
/** Waiting on the tasks `afterRun` hooks asked for, which the generation owns; then the run ends. */
|
|
70
|
+
phase: "drain";
|
|
71
|
+
answer: EntryId;
|
|
72
|
+
tasks: TaskId[];
|
|
73
|
+
} | {
|
|
74
|
+
/**
|
|
75
|
+
* The round is complete and waits on the tasks `afterTools` hooks asked for, which the generation owns; then it
|
|
76
|
+
* ends as the `tools` phase ends it (spec suspendable §6). A crash before they exist asks the hooks again.
|
|
77
|
+
*/
|
|
78
|
+
phase: "afterTools";
|
|
79
|
+
assistant: EntryId;
|
|
80
|
+
tools: TaskId<ToolTaskResult>[];
|
|
81
|
+
tasks: TaskId[];
|
|
82
|
+
} | {
|
|
83
|
+
/**
|
|
84
|
+
* The answer is appended, and the run's inputs stay open while the tasks an `onYield` hook asked for, which the
|
|
85
|
+
* generation owns, decide whether it stands (`LiveRun.yield`).
|
|
86
|
+
*/
|
|
87
|
+
phase: "yield";
|
|
88
|
+
answer: EntryId;
|
|
89
|
+
tasks: TaskId[];
|
|
90
|
+
};
|
|
91
|
+
export type GenerationResult = {
|
|
92
|
+
entryId: EntryId;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Built-in generation task: prepares the positional system prompt and tool loadout, requests or polls the model,
|
|
96
|
+
* retries, and classifies the response. The run's inputs live in `diffra.live.run`.
|
|
97
|
+
*/
|
|
98
|
+
export declare const GenerationTask: Task<GenerationInput, GenerationCheckpoint, GenerationResult, GenerationHooks>;
|
|
99
|
+
/**
|
|
100
|
+
* Append a committed partial left by an interrupted, aborted, faulted, or orphaned attempt as an aborted assistant
|
|
101
|
+
* entry; the caller replaces or removes `generation`.
|
|
102
|
+
*/
|
|
103
|
+
export declare function convertPartial(tx: Tx, live: Draft<LiveState>, conversationId: ConversationId): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Start a run for `inputs`, placed input submissions, whose first entry is `first` (`LiveRun.first`): a new generation
|
|
106
|
+
* takes `diffra.live.run`. Removes `runFailure`, unless `keepFailure`: a run started in the commit that ended the failed one
|
|
107
|
+
* keeps its failure.
|
|
108
|
+
*/
|
|
109
|
+
export declare function startRun(tx: Tx, conversationId: ConversationId, live: Draft<LiveState>, run: {
|
|
110
|
+
readonly inputs: SubmissionId[];
|
|
111
|
+
readonly first: EntryId | undefined;
|
|
112
|
+
}, options?: {
|
|
113
|
+
readonly keepFailure?: boolean;
|
|
114
|
+
}): Promise<void>;
|
|
115
|
+
/** Hand run control from `from` to `to`; the run's inputs move with it. */
|
|
116
|
+
export declare function handOver(live: Draft<LiveState>, from: TaskId, to: TaskId): void;
|
|
117
|
+
//# sourceMappingURL=generation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generation.d.ts","sourceRoot":"","sources":["../../src/harness/generation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGX,cAAc,EAGd,kBAAkB,EAGlB,MAAM,eA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