@ixo/oracle-runtime-workers 0.16.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +3 -0
  2. package/package.json +5 -3
  3. package/src/core/budgeted-llm.ts +115 -6
  4. package/src/core/context-budget.ts +24 -26
  5. package/src/core/context-window.ts +39 -12
  6. package/src/core/domain-context/document.ts +100 -0
  7. package/src/core/domain-context/findings.ts +25 -0
  8. package/src/core/domain-context/index.ts +28 -0
  9. package/src/core/domain-context/integrity.ts +60 -0
  10. package/src/core/domain-context/lru.ts +63 -0
  11. package/src/core/domain-context/resolver.ts +663 -0
  12. package/src/core/domain-context/transport.ts +382 -0
  13. package/src/core/domain-context/turn-context.ts +443 -0
  14. package/src/core/domain-context/types.ts +119 -0
  15. package/src/core/domain-context/validator.ts +41 -0
  16. package/src/core/env.ts +60 -6
  17. package/src/core/index.ts +2 -0
  18. package/src/core/llm.ts +225 -4
  19. package/src/core/main-agent-types.ts +28 -0
  20. package/src/core/main-agent.ts +61 -4
  21. package/src/core/middlewares/tool-execution.ts +19 -2
  22. package/src/core/registries.ts +14 -3
  23. package/src/core/state.ts +2 -1
  24. package/src/core/sub-agent-fallback.ts +14 -2
  25. package/src/core/turn-budget.ts +19 -22
  26. package/src/do/contracts.ts +23 -2
  27. package/src/do/domain-context-store.ts +123 -0
  28. package/src/do/run-request.ts +6 -0
  29. package/src/do/sse-stream.ts +1 -1
  30. package/src/do/transcript.ts +9 -0
  31. package/src/do/turn-body.ts +2 -1
  32. package/src/do/turn-metadata.ts +13 -6
  33. package/src/do/user-oracle-do.ts +111 -42
  34. package/src/index.ts +16 -0
  35. package/src/llm/byo-adapter.ts +6 -0
  36. package/src/llm/byo-catalog.ts +7 -4
  37. package/src/llm/byo-client.ts +42 -7
  38. package/src/llm/provider-error.ts +21 -3
  39. package/src/llm/stream-liveness.ts +498 -0
  40. package/src/plugins/firecrawl/firecrawl-tools.ts +6 -0
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Byte-level checks every domain document passes before anything reads it:
3
+ * the anchor's CID must name the exact bytes, and the bytes must be strict
4
+ * UTF-8 without a byte-order mark.
5
+ */
6
+ import { CID } from 'multiformats/cid';
7
+ import { sha256 } from 'multiformats/hashes/sha2';
8
+
9
+ /** multicodec `raw`. */
10
+ const RAW_CODEC = 0x55;
11
+ /** multihash `sha2-256`. */
12
+ const SHA2_256 = 0x12;
13
+
14
+ /** Index (`domain.md`) and capsule manifest bound. */
15
+ export const INDEX_MAX_BYTES = 1024 * 1024;
16
+ /** Linked document bound. */
17
+ export const LINKED_DOCUMENT_MAX_BYTES = 2 * 1024 * 1024;
18
+
19
+ /**
20
+ * Throws unless `cid` is a CIDv1 over the raw codec with a sha2-256 digest of
21
+ * exactly `bytes`. Any other CID shape is refused rather than interpreted.
22
+ */
23
+ export async function verifyBytes(
24
+ bytes: Uint8Array,
25
+ cid: string,
26
+ ): Promise<void> {
27
+ let expected: CID;
28
+ try {
29
+ expected = CID.parse(cid);
30
+ } catch {
31
+ throw new Error('invalid-cid');
32
+ }
33
+ if (
34
+ expected.version !== 1 ||
35
+ expected.code !== RAW_CODEC ||
36
+ expected.multihash.code !== SHA2_256
37
+ )
38
+ throw new Error('unsupported-cid-codec');
39
+ const digest = await sha256.digest(bytes);
40
+ if (!CID.createV1(RAW_CODEC, digest).equals(expected))
41
+ throw new Error('cid-mismatch');
42
+ }
43
+
44
+ /** CIDv1 (raw, sha2-256) of `bytes`, as its default base32 string. */
45
+ export async function cidOf(bytes: Uint8Array): Promise<string> {
46
+ return CID.createV1(RAW_CODEC, await sha256.digest(bytes)).toString();
47
+ }
48
+
49
+ /** Strict UTF-8 decode; a byte-order mark or any invalid sequence is refused. */
50
+ export function decode(bytes: Uint8Array): string {
51
+ if (bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf)
52
+ throw new Error('utf8-bom');
53
+ try {
54
+ return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(
55
+ bytes,
56
+ );
57
+ } catch {
58
+ throw new Error('invalid-utf8');
59
+ }
60
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * A small least-recently-used map bounded by entry count and by a total
3
+ * weight (bytes, characters). `Map` keeps insertion order, so re-inserting on
4
+ * every hit makes the first key the least recently used one.
5
+ */
6
+ export class LruMap<K, V> {
7
+ private readonly entries = new Map<K, { value: V; weight: number }>();
8
+ private totalWeight = 0;
9
+
10
+ constructor(
11
+ private readonly limits: {
12
+ maxEntries: number;
13
+ maxWeight?: number;
14
+ weigh?: (value: V) => number;
15
+ },
16
+ ) {}
17
+
18
+ get size(): number {
19
+ return this.entries.size;
20
+ }
21
+
22
+ get weight(): number {
23
+ return this.totalWeight;
24
+ }
25
+
26
+ has(key: K): boolean {
27
+ return this.entries.has(key);
28
+ }
29
+
30
+ get(key: K): V | undefined {
31
+ const entry = this.entries.get(key);
32
+ if (!entry) return undefined;
33
+ this.entries.delete(key);
34
+ this.entries.set(key, entry);
35
+ return entry.value;
36
+ }
37
+
38
+ /** Stores `value` unless it alone exceeds the weight bound; evicts the least recently used entries to fit. */
39
+ set(key: K, value: V): void {
40
+ const weight = this.limits.weigh?.(value) ?? 0;
41
+ const maxWeight = this.limits.maxWeight ?? Infinity;
42
+ this.delete(key);
43
+ if (weight > maxWeight) return;
44
+ while (
45
+ this.entries.size > 0 &&
46
+ (this.entries.size >= this.limits.maxEntries ||
47
+ this.totalWeight + weight > maxWeight)
48
+ ) {
49
+ const oldest = this.entries.keys().next();
50
+ if (oldest.done) break;
51
+ this.delete(oldest.value);
52
+ }
53
+ this.entries.set(key, { value, weight });
54
+ this.totalWeight += weight;
55
+ }
56
+
57
+ delete(key: K): void {
58
+ const entry = this.entries.get(key);
59
+ if (!entry) return;
60
+ this.entries.delete(key);
61
+ this.totalWeight -= entry.weight;
62
+ }
63
+ }