kernl 0.6.2 → 0.6.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.
Files changed (163) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-check-types.log +1 -1
  3. package/CHANGELOG.md +30 -0
  4. package/dist/agent/__tests__/concurrency.test.js +1 -1
  5. package/dist/agent/__tests__/run.test.js +1 -1
  6. package/dist/{types/agent.d.ts → agent/types.d.ts} +2 -2
  7. package/dist/agent/types.d.ts.map +1 -0
  8. package/dist/agent.d.ts +36 -4
  9. package/dist/agent.d.ts.map +1 -1
  10. package/dist/agent.js +58 -0
  11. package/dist/api/models/thread.d.ts +1 -1
  12. package/dist/api/resources/threads/threads.d.ts +1 -1
  13. package/dist/api/resources/threads/threads.d.ts.map +1 -1
  14. package/dist/api/resources/threads/threads.js +1 -1
  15. package/dist/api/resources/threads/types.d.ts +2 -2
  16. package/dist/api/resources/threads/types.d.ts.map +1 -1
  17. package/dist/context.d.ts +4 -4
  18. package/dist/context.d.ts.map +1 -1
  19. package/dist/guardrail.d.ts +2 -2
  20. package/dist/index.d.ts +5 -3
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +4 -3
  23. package/dist/internal.d.ts +2 -2
  24. package/dist/internal.js +1 -1
  25. package/dist/kernl/index.d.ts +1 -1
  26. package/dist/kernl/index.d.ts.map +1 -1
  27. package/dist/kernl/index.js +0 -1
  28. package/dist/kernl/kernl.d.ts +7 -18
  29. package/dist/kernl/kernl.d.ts.map +1 -1
  30. package/dist/kernl/kernl.js +29 -29
  31. package/dist/kernl/types.d.ts +91 -0
  32. package/dist/kernl/types.d.ts.map +1 -0
  33. package/dist/lib/error.d.ts +2 -2
  34. package/dist/lifecycle.d.ts +2 -2
  35. package/dist/memory/codec.d.ts +32 -0
  36. package/dist/memory/codec.d.ts.map +1 -0
  37. package/dist/memory/codec.js +97 -0
  38. package/dist/memory/codecs/domain.d.ts +34 -0
  39. package/dist/memory/codecs/domain.d.ts.map +1 -0
  40. package/dist/memory/codecs/domain.js +99 -0
  41. package/dist/memory/codecs/identity.d.ts +12 -0
  42. package/dist/memory/codecs/identity.d.ts.map +1 -0
  43. package/dist/memory/codecs/identity.js +17 -0
  44. package/dist/memory/codecs/index.d.ts +31 -0
  45. package/dist/memory/codecs/index.d.ts.map +1 -0
  46. package/dist/memory/codecs/index.js +39 -0
  47. package/dist/memory/codecs/tpuf.d.ts +38 -0
  48. package/dist/memory/codecs/tpuf.d.ts.map +1 -0
  49. package/dist/memory/codecs/tpuf.js +90 -0
  50. package/dist/memory/encoder.d.ts +29 -0
  51. package/dist/memory/encoder.d.ts.map +1 -0
  52. package/dist/memory/encoder.js +45 -0
  53. package/dist/memory/handle.d.ts +89 -0
  54. package/dist/memory/handle.d.ts.map +1 -0
  55. package/dist/memory/handle.js +128 -0
  56. package/dist/memory/index.d.ts +12 -0
  57. package/dist/memory/index.d.ts.map +1 -0
  58. package/dist/memory/index.js +7 -0
  59. package/dist/memory/indexes.d.ts +91 -0
  60. package/dist/memory/indexes.d.ts.map +1 -0
  61. package/dist/memory/indexes.js +7 -0
  62. package/dist/memory/memory.d.ts +51 -0
  63. package/dist/memory/memory.d.ts.map +1 -0
  64. package/dist/memory/memory.js +107 -0
  65. package/dist/memory/schema.d.ts +41 -0
  66. package/dist/memory/schema.d.ts.map +1 -0
  67. package/dist/memory/schema.js +112 -0
  68. package/dist/memory/store.d.ts +36 -0
  69. package/dist/memory/store.d.ts.map +1 -0
  70. package/dist/memory/store.js +4 -0
  71. package/dist/memory/types.d.ts +250 -0
  72. package/dist/memory/types.d.ts.map +1 -0
  73. package/dist/memory/types.js +4 -0
  74. package/dist/storage/base.d.ts +6 -1
  75. package/dist/storage/base.d.ts.map +1 -1
  76. package/dist/storage/in-memory.d.ts +24 -2
  77. package/dist/storage/in-memory.d.ts.map +1 -1
  78. package/dist/storage/in-memory.js +131 -0
  79. package/dist/storage/thread.d.ts +1 -1
  80. package/dist/thread/__tests__/integration.test.js +1 -1
  81. package/dist/thread/__tests__/mock.d.ts +1 -1
  82. package/dist/thread/__tests__/namespace.test.js +1 -1
  83. package/dist/thread/__tests__/thread.test.js +1 -1
  84. package/dist/thread/thread.d.ts +2 -2
  85. package/dist/thread/thread.d.ts.map +1 -1
  86. package/dist/{types/thread.d.ts → thread/types.d.ts} +2 -2
  87. package/dist/thread/types.d.ts.map +1 -0
  88. package/dist/thread/utils.d.ts +2 -2
  89. package/dist/thread/utils.d.ts.map +1 -1
  90. package/package.json +4 -2
  91. package/src/{types/agent.ts → agent/types.ts} +1 -1
  92. package/src/agent.ts +78 -2
  93. package/src/api/__tests__/threads.test.ts +2 -2
  94. package/src/api/models/thread.ts +1 -1
  95. package/src/api/resources/threads/events.ts +1 -1
  96. package/src/api/resources/threads/threads.ts +2 -2
  97. package/src/api/resources/threads/types.ts +2 -2
  98. package/src/context.ts +6 -136
  99. package/src/guardrail.ts +2 -2
  100. package/src/index.ts +35 -6
  101. package/src/internal.ts +2 -2
  102. package/src/kernl/index.ts +8 -0
  103. package/src/{kernl.ts → kernl/kernl.ts} +40 -3
  104. package/src/kernl/types.ts +106 -0
  105. package/src/lib/error.ts +2 -2
  106. package/src/lifecycle.ts +2 -2
  107. package/src/memory/codecs/domain.ts +115 -0
  108. package/src/memory/codecs/identity.ts +28 -0
  109. package/src/memory/codecs/index.ts +61 -0
  110. package/src/memory/codecs/tpuf.ts +115 -0
  111. package/src/memory/encoder.ts +56 -0
  112. package/src/memory/handle.ts +189 -0
  113. package/src/memory/index.ts +49 -0
  114. package/src/memory/indexes.ts +108 -0
  115. package/src/memory/memory.ts +143 -0
  116. package/src/memory/schema.ts +142 -0
  117. package/src/memory/store.ts +47 -0
  118. package/src/memory/types.ts +282 -0
  119. package/src/storage/__tests__/in-memory.test.ts +1 -1
  120. package/src/storage/base.ts +7 -1
  121. package/src/storage/in-memory.ts +170 -2
  122. package/src/storage/thread.ts +1 -1
  123. package/src/thread/__tests__/integration.test.ts +1 -1
  124. package/src/thread/__tests__/mock.ts +1 -1
  125. package/src/thread/__tests__/thread.test.ts +1 -1
  126. package/src/thread/thread.ts +2 -2
  127. package/src/{types/thread.ts → thread/types.ts} +1 -1
  128. package/src/thread/utils.ts +2 -2
  129. package/tsconfig.tsbuildinfo +1 -0
  130. package/dist/api/__tests__/cursor-page.test.d.ts +0 -2
  131. package/dist/api/__tests__/cursor-page.test.d.ts.map +0 -1
  132. package/dist/api/__tests__/cursor-page.test.js +0 -414
  133. package/dist/api/__tests__/offset-page.test.d.ts +0 -2
  134. package/dist/api/__tests__/offset-page.test.d.ts.map +0 -1
  135. package/dist/api/__tests__/offset-page.test.js +0 -510
  136. package/dist/api/pagination/base.d.ts +0 -48
  137. package/dist/api/pagination/base.d.ts.map +0 -1
  138. package/dist/api/pagination/base.js +0 -45
  139. package/dist/api/pagination/cursor.d.ts +0 -44
  140. package/dist/api/pagination/cursor.d.ts.map +0 -1
  141. package/dist/api/pagination/cursor.js +0 -52
  142. package/dist/api/pagination/offset.d.ts +0 -42
  143. package/dist/api/pagination/offset.d.ts.map +0 -1
  144. package/dist/api/pagination/offset.js +0 -55
  145. package/dist/kernl/threads.d.ts +0 -110
  146. package/dist/kernl/threads.d.ts.map +0 -1
  147. package/dist/kernl/threads.js +0 -126
  148. package/dist/kernl.d.ts +0 -51
  149. package/dist/kernl.d.ts.map +0 -1
  150. package/dist/kernl.js +0 -91
  151. package/dist/types/agent.d.ts.map +0 -1
  152. package/dist/types/kernl.d.ts +0 -42
  153. package/dist/types/kernl.d.ts.map +0 -1
  154. package/dist/types/thread.d.ts.map +0 -1
  155. package/src/api/__tests__/cursor-page.test.ts +0 -512
  156. package/src/api/__tests__/offset-page.test.ts +0 -624
  157. package/src/api/pagination/base.ts +0 -79
  158. package/src/api/pagination/cursor.ts +0 -86
  159. package/src/api/pagination/offset.ts +0 -89
  160. package/src/types/kernl.ts +0 -51
  161. /package/dist/{types/agent.js → agent/types.js} +0 -0
  162. /package/dist/{types/kernl.js → kernl/types.js} +0 -0
  163. /package/dist/{types/thread.js → thread/types.js} +0 -0
@@ -0,0 +1,91 @@
1
+ import { LanguageModel, EmbeddingModel } from "@kernl-sdk/protocol";
2
+ import { SearchIndex } from "@kernl-sdk/retrieval";
3
+ import { Agent } from "../agent.js";
4
+ import { KernlStorage } from "../storage/index.js";
5
+ /**
6
+ * Storage configuration for Kernl.
7
+ */
8
+ export interface StorageOptions {
9
+ /**
10
+ * Relational database storage (threads, tasks, traces).
11
+ */
12
+ db?: KernlStorage;
13
+ /**
14
+ * Vector search index for semantic memory search.
15
+ * Supports pgvector, Turbopuffer, etc.
16
+ */
17
+ vector?: SearchIndex;
18
+ }
19
+ /**
20
+ * Memory system configuration.
21
+ */
22
+ export interface MemoryOptions {
23
+ /**
24
+ * Embedding model for memory encoding.
25
+ *
26
+ * Can be:
27
+ * - A string like "openai/text-embedding-3-small" (resolved via provider registry)
28
+ * - An EmbeddingModel instance
29
+ *
30
+ * @default "openai/text-embedding-3-small"
31
+ */
32
+ embeddingModel?: string | EmbeddingModel<string>;
33
+ /**
34
+ * Logical index ID used by the search backend.
35
+ * - For pgvector: becomes the table name (with schema from indexProviderOptions)
36
+ * - For Turbopuffer: becomes the namespace
37
+ * @default "kernl_memories_index"
38
+ */
39
+ indexId?: string;
40
+ /**
41
+ * Backend-specific options passed to SearchIndex.createIndex().
42
+ * - For pgvector: { schema?: string } controls schema (default: "kernl")
43
+ * - For Turbopuffer: not used
44
+ */
45
+ indexProviderOptions?: Record<string, unknown>;
46
+ /**
47
+ * Vector dimensions for embeddings.
48
+ * Only needed if embedding model doesn't provide this automatically.
49
+ * @default 1536
50
+ */
51
+ dimensions?: number;
52
+ /**
53
+ * Similarity metric for vector search.
54
+ * @default "cosine"
55
+ */
56
+ similarity?: "cosine" | "euclidean" | "dot_product";
57
+ }
58
+ /**
59
+ * Configuration options for creating a Kernl instance.
60
+ */
61
+ export interface KernlOptions {
62
+ /**
63
+ * Storage configuration for persisting threads, tasks, and traces.
64
+ */
65
+ storage?: StorageOptions;
66
+ /**
67
+ * Memory system configuration.
68
+ */
69
+ memory?: MemoryOptions;
70
+ }
71
+ /**
72
+ * Agent registry interface.
73
+ *
74
+ * Satisfied by Map<string, Agent>.
75
+ */
76
+ export interface AgentRegistry {
77
+ get(id: string): Agent<any> | undefined;
78
+ }
79
+ /**
80
+ * Model registry interface.
81
+ *
82
+ * Satisfied by Map<string, LanguageModel>.
83
+ * Key format: "provider/modelId"
84
+ *
85
+ * TODO: Create an exhaustive model registry in the protocol package
86
+ * with all supported models and their metadata.
87
+ */
88
+ export interface ModelRegistry {
89
+ get(key: string): LanguageModel | undefined;
90
+ }
91
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/kernl/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,EAAE,CAAC,EAAE,YAAY,CAAC;IAElB;;;OAGG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CAKtB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;;;;OAQG;IACH,cAAc,CAAC,EAAE,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;IAEjD;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAE/C;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,UAAU,CAAC,EAAE,QAAQ,GAAG,WAAW,GAAG,aAAa,CAAC;CACrD;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;IAEzB;;OAEG;IACH,MAAM,CAAC,EAAE,aAAa,CAAC;CACxB;AAED;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC5B,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;CACzC;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,aAAa;IAC5B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CAAC;CAC7C"}
@@ -1,7 +1,7 @@
1
1
  import { InputGuardrailResult, OutputGuardrailMetadata, OutputGuardrailResult } from "../guardrail.js";
2
2
  type SerializedThread = any;
3
- import { AgentResponseType } from "../types/agent.js";
4
- import { TextResponse } from "../types/thread.js";
3
+ import { AgentResponseType } from "../agent/types.js";
4
+ import { TextResponse } from "../thread/types.js";
5
5
  /**
6
6
  * Abstract base class for all `kernl` errors
7
7
  * Requires subclasses to implement JSON serialization
@@ -3,8 +3,8 @@ import { Agent } from "./agent.js";
3
3
  import { Context, UnknownContext } from "./context.js";
4
4
  import { Tool } from "./tool/index.js";
5
5
  import type { ToolCall } from "@kernl-sdk/protocol";
6
- import { AgentResponseType } from "./types/agent.js";
7
- import { TextResponse } from "./types/thread.js";
6
+ import { AgentResponseType } from "./agent/types.js";
7
+ import { TextResponse } from "./thread/types.js";
8
8
  export type EventEmitterEvents = Record<string, any[]>;
9
9
  /**
10
10
  * Generic typed event emitter that wraps Node's EventEmitter with type safety
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Memory codecs.
3
+ */
4
+ import type { Codec, AsyncCodec } from "@kernl-sdk/shared/lib";
5
+ import type { Filter as SearchFilter } from "@kernl-sdk/retrieval";
6
+ import type { MemoryFilter, MemoryRecord, MemoryRecordUpdate, IndexMemoryRecord, IndexMemoryRecordPatch, MemoryByteCodec } from "./types.js";
7
+ /**
8
+ * Codec for converting MemoryFilter to retrieval Filter.
9
+ *
10
+ * - scope.namespace → namespace
11
+ * - scope.entityId → entityId
12
+ * - scope.agentId → agentId
13
+ * - collections → collection: { $in: [...] }
14
+ * - after/before → timestamp: { $gt/$lt }
15
+ *
16
+ * Content field filtering (text, metadata, kind) is not currently supported.
17
+ * Text relevance is handled via vector similarity in the query, not filters.
18
+ */
19
+ export declare const MEMORY_FILTER: Codec<MemoryFilter, SearchFilter>;
20
+ /**
21
+ * Create a codec for MemoryRecord -> IndexMemoryRecord.
22
+ */
23
+ export declare function recordCodec(bytecodec: MemoryByteCodec): AsyncCodec<MemoryRecord, IndexMemoryRecord>;
24
+ /**
25
+ * Codec for converting MemoryRecordUpdate to IndexMemoryRecordPatch.
26
+ *
27
+ * Maps patchable fields from domain update to index patch format.
28
+ * wmem/smem are store-only fields and are not included.
29
+ * content changes require full re-index, not a patch.
30
+ */
31
+ export declare const PATCH_CODEC: Codec<MemoryRecordUpdate, IndexMemoryRecordPatch>;
32
+ //# sourceMappingURL=codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/memory/codec.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,KAAK,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEnE,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,EACtB,eAAe,EAChB,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,YAAY,EAAE,YAAY,CA0B3D,CAAC;AAEF;;GAEG;AACH,wBAAgB,WAAW,CACzB,SAAS,EAAE,eAAe,GACzB,UAAU,CAAC,YAAY,EAAE,iBAAiB,CAAC,CAqB7C;AAED;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,EAAE,KAAK,CAAC,kBAAkB,EAAE,sBAAsB,CAqBzE,CAAC"}
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Memory codecs.
3
+ */
4
+ /**
5
+ * Codec for converting MemoryFilter to retrieval Filter.
6
+ *
7
+ * - scope.namespace → namespace
8
+ * - scope.entityId → entityId
9
+ * - scope.agentId → agentId
10
+ * - collections → collection: { $in: [...] }
11
+ * - after/before → timestamp: { $gt/$lt }
12
+ *
13
+ * Content field filtering (text, metadata, kind) is not currently supported.
14
+ * Text relevance is handled via vector similarity in the query, not filters.
15
+ */
16
+ export const MEMORY_FILTER = {
17
+ encode(mf) {
18
+ const sf = {};
19
+ // scope
20
+ if (mf.scope?.namespace !== undefined)
21
+ sf.namespace = mf.scope.namespace;
22
+ if (mf.scope?.entityId !== undefined)
23
+ sf.entityId = mf.scope.entityId;
24
+ if (mf.scope?.agentId !== undefined)
25
+ sf.agentId = mf.scope.agentId;
26
+ if (mf.collections && mf.collections.length > 0) {
27
+ sf.collection = { $in: mf.collections };
28
+ }
29
+ if (mf.after !== undefined || mf.before !== undefined) {
30
+ const ts = {};
31
+ if (mf.after !== undefined)
32
+ ts.$gt = mf.after;
33
+ if (mf.before !== undefined)
34
+ ts.$lt = mf.before;
35
+ sf.timestamp = ts;
36
+ }
37
+ return sf;
38
+ },
39
+ decode(_filter) {
40
+ throw new Error("MEMORY_FILTER.decode not implemented");
41
+ },
42
+ };
43
+ /**
44
+ * Create a codec for MemoryRecord -> IndexMemoryRecord.
45
+ */
46
+ export function recordCodec(bytecodec) {
47
+ return {
48
+ async encode(record) {
49
+ const indexable = await bytecodec.encode(record.content);
50
+ return {
51
+ id: record.id,
52
+ namespace: record.scope.namespace ?? null,
53
+ entityId: record.scope.entityId ?? null,
54
+ agentId: record.scope.agentId ?? null,
55
+ kind: record.kind,
56
+ collection: record.collection,
57
+ timestamp: record.timestamp,
58
+ createdAt: record.createdAt,
59
+ updatedAt: record.updatedAt,
60
+ ...indexable,
61
+ };
62
+ },
63
+ async decode() {
64
+ throw new Error("createRecordCodec.decode not implemented");
65
+ },
66
+ };
67
+ }
68
+ /**
69
+ * Codec for converting MemoryRecordUpdate to IndexMemoryRecordPatch.
70
+ *
71
+ * Maps patchable fields from domain update to index patch format.
72
+ * wmem/smem are store-only fields and are not included.
73
+ * content changes require full re-index, not a patch.
74
+ */
75
+ export const PATCH_CODEC = {
76
+ encode(update) {
77
+ const patch = { id: update.id };
78
+ if (update.scope?.namespace !== undefined)
79
+ patch.namespace = update.scope.namespace;
80
+ if (update.scope?.entityId !== undefined)
81
+ patch.entityId = update.scope.entityId;
82
+ if (update.scope?.agentId !== undefined)
83
+ patch.agentId = update.scope.agentId;
84
+ if (update.collection !== undefined)
85
+ patch.collection = update.collection;
86
+ if (update.timestamp !== undefined)
87
+ patch.timestamp = update.timestamp;
88
+ if (update.updatedAt !== undefined)
89
+ patch.updatedAt = update.updatedAt;
90
+ if (update.metadata !== undefined)
91
+ patch.metadata = update.metadata;
92
+ return patch;
93
+ },
94
+ decode(_patch) {
95
+ throw new Error("PATCH_CODEC.decode not implemented");
96
+ },
97
+ };
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Domain-level memory codecs.
3
+ *
4
+ * Codecs for transforming between memory domain types and search/index formats.
5
+ */
6
+ import type { Codec, AsyncCodec } from "@kernl-sdk/shared/lib";
7
+ import type { Filter as SearchFilter } from "@kernl-sdk/retrieval";
8
+ import type { MemoryFilter, MemoryRecord, MemoryRecordUpdate, IndexMemoryRecord, IndexMemoryRecordPatch, MemoryByteCodec } from "../types.js";
9
+ /**
10
+ * Codec for converting MemoryFilter to retrieval Filter.
11
+ *
12
+ * - scope.namespace → namespace
13
+ * - scope.entityId → entityId
14
+ * - scope.agentId → agentId
15
+ * - collections → collection: { $in: [...] }
16
+ * - after/before → timestamp: { $gt/$lt }
17
+ *
18
+ * Content field filtering (text, metadata, kind) is not currently supported.
19
+ * Text relevance is handled via vector similarity in the query, not filters.
20
+ */
21
+ export declare const MEMORY_FILTER: Codec<MemoryFilter, SearchFilter>;
22
+ /**
23
+ * Create a codec for MemoryRecord -> IndexMemoryRecord.
24
+ */
25
+ export declare function recordCodec(bytecodec: MemoryByteCodec): AsyncCodec<MemoryRecord, IndexMemoryRecord>;
26
+ /**
27
+ * Codec for converting MemoryRecordUpdate to IndexMemoryRecordPatch.
28
+ *
29
+ * Maps patchable fields from domain update to index patch format.
30
+ * wmem/smem are store-only fields and are not included.
31
+ * content changes require full re-index, not a patch.
32
+ */
33
+ export declare const PATCH_CODEC: Codec<MemoryRecordUpdate, IndexMemoryRecordPatch>;
34
+ //# sourceMappingURL=domain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domain.d.ts","sourceRoot":"","sources":["../../../src/memory/codecs/domain.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,KAAK,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEnE,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,EACtB,eAAe,EAChB,MAAM,UAAU,CAAC;AAElB;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,YAAY,EAAE,YAAY,CA0B3D,CAAC;AAEF;;GAEG;AACH,wBAAgB,WAAW,CACzB,SAAS,EAAE,eAAe,GACzB,UAAU,CAAC,YAAY,EAAE,iBAAiB,CAAC,CAqB7C;AAED;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,EAAE,KAAK,CAAC,kBAAkB,EAAE,sBAAsB,CAqBzE,CAAC"}
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Domain-level memory codecs.
3
+ *
4
+ * Codecs for transforming between memory domain types and search/index formats.
5
+ */
6
+ /**
7
+ * Codec for converting MemoryFilter to retrieval Filter.
8
+ *
9
+ * - scope.namespace → namespace
10
+ * - scope.entityId → entityId
11
+ * - scope.agentId → agentId
12
+ * - collections → collection: { $in: [...] }
13
+ * - after/before → timestamp: { $gt/$lt }
14
+ *
15
+ * Content field filtering (text, metadata, kind) is not currently supported.
16
+ * Text relevance is handled via vector similarity in the query, not filters.
17
+ */
18
+ export const MEMORY_FILTER = {
19
+ encode(mf) {
20
+ const sf = {};
21
+ // scope
22
+ if (mf.scope?.namespace !== undefined)
23
+ sf.namespace = mf.scope.namespace;
24
+ if (mf.scope?.entityId !== undefined)
25
+ sf.entityId = mf.scope.entityId;
26
+ if (mf.scope?.agentId !== undefined)
27
+ sf.agentId = mf.scope.agentId;
28
+ if (mf.collections && mf.collections.length > 0) {
29
+ sf.collection = { $in: mf.collections };
30
+ }
31
+ if (mf.after !== undefined || mf.before !== undefined) {
32
+ const ts = {};
33
+ if (mf.after !== undefined)
34
+ ts.$gt = mf.after;
35
+ if (mf.before !== undefined)
36
+ ts.$lt = mf.before;
37
+ sf.timestamp = ts;
38
+ }
39
+ return sf;
40
+ },
41
+ decode(_filter) {
42
+ throw new Error("MEMORY_FILTER.decode not implemented");
43
+ },
44
+ };
45
+ /**
46
+ * Create a codec for MemoryRecord -> IndexMemoryRecord.
47
+ */
48
+ export function recordCodec(bytecodec) {
49
+ return {
50
+ async encode(record) {
51
+ const indexable = await bytecodec.encode(record.content);
52
+ return {
53
+ id: record.id,
54
+ namespace: record.scope.namespace ?? null,
55
+ entityId: record.scope.entityId ?? null,
56
+ agentId: record.scope.agentId ?? null,
57
+ kind: record.kind,
58
+ collection: record.collection,
59
+ timestamp: record.timestamp,
60
+ createdAt: record.createdAt,
61
+ updatedAt: record.updatedAt,
62
+ ...indexable,
63
+ };
64
+ },
65
+ async decode() {
66
+ throw new Error("recordCodec.decode not implemented");
67
+ },
68
+ };
69
+ }
70
+ /**
71
+ * Codec for converting MemoryRecordUpdate to IndexMemoryRecordPatch.
72
+ *
73
+ * Maps patchable fields from domain update to index patch format.
74
+ * wmem/smem are store-only fields and are not included.
75
+ * content changes require full re-index, not a patch.
76
+ */
77
+ export const PATCH_CODEC = {
78
+ encode(update) {
79
+ const patch = { id: update.id };
80
+ if (update.scope?.namespace !== undefined)
81
+ patch.namespace = update.scope.namespace;
82
+ if (update.scope?.entityId !== undefined)
83
+ patch.entityId = update.scope.entityId;
84
+ if (update.scope?.agentId !== undefined)
85
+ patch.agentId = update.scope.agentId;
86
+ if (update.collection !== undefined)
87
+ patch.collection = update.collection;
88
+ if (update.timestamp !== undefined)
89
+ patch.timestamp = update.timestamp;
90
+ if (update.updatedAt !== undefined)
91
+ patch.updatedAt = update.updatedAt;
92
+ if (update.metadata !== undefined)
93
+ patch.metadata = update.metadata;
94
+ return patch;
95
+ },
96
+ decode(_patch) {
97
+ throw new Error("PATCH_CODEC.decode not implemented");
98
+ },
99
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Identity codecs - pass through unchanged.
3
+ *
4
+ * Used for backends that support the full IndexMemoryRecord schema natively.
5
+ */
6
+ import type { Codec } from "@kernl-sdk/shared/lib";
7
+ import type { FieldSchema, SearchQuery, UnknownDocument } from "@kernl-sdk/retrieval";
8
+ import type { IndexMemoryRecord } from "../types.js";
9
+ export declare const IDENTITY_DOC: Codec<IndexMemoryRecord, UnknownDocument>;
10
+ export declare const IDENTITY_SCHEMA: Codec<Record<string, FieldSchema>, Record<string, FieldSchema>>;
11
+ export declare const IDENTITY_QUERY: Codec<SearchQuery, SearchQuery>;
12
+ //# sourceMappingURL=identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../../src/memory/codecs/identity.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEtF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAElD,eAAO,MAAM,YAAY,EAAE,KAAK,CAAC,iBAAiB,EAAE,eAAe,CAGlE,CAAC;AAEF,eAAO,MAAM,eAAe,EAAE,KAAK,CACjC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,EAC3B,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAI5B,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,WAAW,EAAE,WAAW,CAG1D,CAAC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Identity codecs - pass through unchanged.
3
+ *
4
+ * Used for backends that support the full IndexMemoryRecord schema natively.
5
+ */
6
+ export const IDENTITY_DOC = {
7
+ encode: (doc) => doc,
8
+ decode: (row) => row,
9
+ };
10
+ export const IDENTITY_SCHEMA = {
11
+ encode: (schema) => schema,
12
+ decode: (schema) => schema,
13
+ };
14
+ export const IDENTITY_QUERY = {
15
+ encode: (q) => q,
16
+ decode: (q) => q,
17
+ };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Memory codecs.
3
+ *
4
+ * Re-exports all memory codecs:
5
+ * - Domain codecs (MEMORY_FILTER, PATCH_CODEC, recordCodec)
6
+ * - Backend codecs (TPUF_*, IDENTITY_*)
7
+ * - Backend codec registry (getBackendCodecs)
8
+ */
9
+ import type { Codec } from "@kernl-sdk/shared/lib";
10
+ import type { FieldSchema, SearchQuery, UnknownDocument } from "@kernl-sdk/retrieval";
11
+ import type { IndexMemoryRecord } from "../types.js";
12
+ export { MEMORY_FILTER, PATCH_CODEC, recordCodec } from "./domain.js";
13
+ export { IDENTITY_DOC, IDENTITY_SCHEMA, IDENTITY_QUERY } from "./identity.js";
14
+ export { TPUF_DOC, TPUF_SCHEMA, TPUF_QUERY } from "./tpuf.js";
15
+ /**
16
+ * Backend codec set.
17
+ */
18
+ export interface AdapterCodecs {
19
+ doc: Codec<IndexMemoryRecord, UnknownDocument>;
20
+ schema: Codec<Record<string, FieldSchema>, Record<string, FieldSchema>>;
21
+ query: Codec<SearchQuery, SearchQuery>;
22
+ }
23
+ /**
24
+ * Registry of backend codecs.
25
+ */
26
+ export declare const ADAPTER_CODECS: Record<string, AdapterCodecs>;
27
+ /**
28
+ * Get codecs for a backend, falling back to identity.
29
+ */
30
+ export declare function getAdapterCodecs(adapterId: string): AdapterCodecs;
31
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/memory/codecs/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,eAAe,EAChB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAKlD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC3E,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,KAAK,CAAC,iBAAiB,EAAE,eAAe,CAAC,CAAC;IAC/C,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;IACxE,KAAK,EAAE,KAAK,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;CACxC;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAOxD,CAAC;AAWF;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,CAEjE"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Memory codecs.
3
+ *
4
+ * Re-exports all memory codecs:
5
+ * - Domain codecs (MEMORY_FILTER, PATCH_CODEC, recordCodec)
6
+ * - Backend codecs (TPUF_*, IDENTITY_*)
7
+ * - Backend codec registry (getBackendCodecs)
8
+ */
9
+ import { IDENTITY_DOC, IDENTITY_SCHEMA, IDENTITY_QUERY } from "./identity.js";
10
+ import { TPUF_DOC, TPUF_SCHEMA, TPUF_QUERY } from "./tpuf.js";
11
+ // re-exports
12
+ export { MEMORY_FILTER, PATCH_CODEC, recordCodec } from "./domain.js";
13
+ export { IDENTITY_DOC, IDENTITY_SCHEMA, IDENTITY_QUERY } from "./identity.js";
14
+ export { TPUF_DOC, TPUF_SCHEMA, TPUF_QUERY } from "./tpuf.js";
15
+ /**
16
+ * Registry of backend codecs.
17
+ */
18
+ export const ADAPTER_CODECS = {
19
+ turbopuffer: { doc: TPUF_DOC, schema: TPUF_SCHEMA, query: TPUF_QUERY },
20
+ pgvector: {
21
+ doc: IDENTITY_DOC,
22
+ schema: IDENTITY_SCHEMA,
23
+ query: IDENTITY_QUERY,
24
+ },
25
+ };
26
+ /**
27
+ * Default codecs (identity) for unknown backends.
28
+ */
29
+ const DEFAULT_CODECS = {
30
+ doc: IDENTITY_DOC,
31
+ schema: IDENTITY_SCHEMA,
32
+ query: IDENTITY_QUERY,
33
+ };
34
+ /**
35
+ * Get codecs for a backend, falling back to identity.
36
+ */
37
+ export function getAdapterCodecs(adapterId) {
38
+ return ADAPTER_CODECS[adapterId] ?? DEFAULT_CODECS;
39
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Turbopuffer backend codecs.
3
+ *
4
+ * Turbopuffer constraints:
5
+ * - Exactly one ANN vector field named "vector" per namespace.
6
+ *
7
+ * Memory model:
8
+ * - IndexMemoryRecord has modality-specific vectors: tvec, ivec, avec, vvec.
9
+ *
10
+ * Mapping:
11
+ * - tvec (text embedding) → vector
12
+ * - ivec/avec/vvec are dropped (not indexed in Turbopuffer)
13
+ */
14
+ import type { Codec } from "@kernl-sdk/shared/lib";
15
+ import type { FieldSchema, SearchQuery, UnknownDocument } from "@kernl-sdk/retrieval";
16
+ import type { IndexMemoryRecord } from "../types.js";
17
+ /**
18
+ * Turbopuffer document codec.
19
+ *
20
+ * Maps tvec → vector, drops ivec/avec/vvec.
21
+ */
22
+ export declare const TPUF_DOC: Codec<IndexMemoryRecord, UnknownDocument>;
23
+ /**
24
+ * Turbopuffer schema codec.
25
+ *
26
+ * Maps tvec → vector, drops ivec/avec/vvec fields.
27
+ */
28
+ export declare const TPUF_SCHEMA: Codec<Record<string, FieldSchema>, Record<string, FieldSchema>>;
29
+ /**
30
+ * Turbopuffer query codec.
31
+ *
32
+ * Maps tvec → vector in query signals, drops ivec/avec/vvec.
33
+ * Operates on normalized SearchQuery (after planQuery).
34
+ *
35
+ * Defaults include to true so memory searches return all document attributes.
36
+ */
37
+ export declare const TPUF_QUERY: Codec<SearchQuery, SearchQuery>;
38
+ //# sourceMappingURL=tpuf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tpuf.d.ts","sourceRoot":"","sources":["../../../src/memory/codecs/tpuf.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EAEX,eAAe,EAChB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAElD;;;;GAIG;AACH,eAAO,MAAM,QAAQ,EAAE,KAAK,CAAC,iBAAiB,EAAE,eAAe,CAkB9D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,EAAE,KAAK,CAC7B,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,EAC3B,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAmB5B,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,EAAE,KAAK,CAAC,WAAW,EAAE,WAAW,CA6BtD,CAAC"}
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Turbopuffer backend codecs.
3
+ *
4
+ * Turbopuffer constraints:
5
+ * - Exactly one ANN vector field named "vector" per namespace.
6
+ *
7
+ * Memory model:
8
+ * - IndexMemoryRecord has modality-specific vectors: tvec, ivec, avec, vvec.
9
+ *
10
+ * Mapping:
11
+ * - tvec (text embedding) → vector
12
+ * - ivec/avec/vvec are dropped (not indexed in Turbopuffer)
13
+ */
14
+ /**
15
+ * Turbopuffer document codec.
16
+ *
17
+ * Maps tvec → vector, drops ivec/avec/vvec.
18
+ */
19
+ export const TPUF_DOC = {
20
+ encode(doc) {
21
+ const { tvec, ivec, avec, vvec, metadata, ...rest } = doc;
22
+ const row = {
23
+ ...rest,
24
+ metadata: metadata, // metadata is JSONObject | null, cast to FieldValue for UnknownDocument
25
+ };
26
+ if (tvec)
27
+ row.vector = tvec;
28
+ return row;
29
+ },
30
+ decode(row) {
31
+ const { vector, ...rest } = row;
32
+ return {
33
+ ...rest,
34
+ tvec: Array.isArray(vector) ? vector : undefined,
35
+ };
36
+ },
37
+ };
38
+ /**
39
+ * Turbopuffer schema codec.
40
+ *
41
+ * Maps tvec → vector, drops ivec/avec/vvec fields.
42
+ */
43
+ export const TPUF_SCHEMA = {
44
+ encode(schema) {
45
+ const result = {};
46
+ for (const [name, field] of Object.entries(schema)) {
47
+ if (name === "tvec") {
48
+ result.vector = field;
49
+ }
50
+ else if (name === "ivec" || name === "avec" || name === "vvec") {
51
+ continue;
52
+ }
53
+ else {
54
+ result[name] = field;
55
+ }
56
+ }
57
+ return result;
58
+ },
59
+ decode() {
60
+ throw new Error("TPUF_SCHEMA.decode not implemented");
61
+ },
62
+ };
63
+ /**
64
+ * Turbopuffer query codec.
65
+ *
66
+ * Maps tvec → vector in query signals, drops ivec/avec/vvec.
67
+ * Operates on normalized SearchQuery (after planQuery).
68
+ *
69
+ * Defaults include to true so memory searches return all document attributes.
70
+ */
71
+ export const TPUF_QUERY = {
72
+ encode(input) {
73
+ // default include to true for memory search (tpuf only returns id + score without it)
74
+ const include = input.include ?? true;
75
+ if (!input.query) {
76
+ return { ...input, include };
77
+ }
78
+ const signals = input.query.map((signal) => {
79
+ const { tvec, ivec, avec, vvec, ...rest } = signal;
80
+ if (tvec) {
81
+ return { ...rest, vector: tvec };
82
+ }
83
+ return rest;
84
+ });
85
+ return { ...input, query: signals, include };
86
+ },
87
+ decode() {
88
+ throw new Error("TPUF_QUERY.decode not implemented");
89
+ },
90
+ };
@@ -0,0 +1,29 @@
1
+ /**
2
+ * MemoryByte encoder - converts MemoryByte to IndexableByte with embeddings.
3
+ */
4
+ import type { EmbeddingModel } from "@kernl-sdk/protocol";
5
+ import type { MemoryByte, IndexableByte, MemoryByteCodec } from "./types.js";
6
+ /**
7
+ * Encoder that converts MemoryByte to IndexableByte.
8
+ *
9
+ * Extracts canonical text from content and computes embeddings.
10
+ */
11
+ export declare class MemoryByteEncoder implements MemoryByteCodec {
12
+ private readonly embedder;
13
+ constructor(embedder: EmbeddingModel<string>);
14
+ /**
15
+ * Encode a MemoryByte to IndexableByte.
16
+ * Extracts text and computes embeddings for each modality.
17
+ */
18
+ encode(byte: MemoryByte): Promise<IndexableByte>;
19
+ /**
20
+ * Decode is not implemented - IndexableByte cannot be converted back to MemoryByte.
21
+ */
22
+ decode(_indexable: IndexableByte): Promise<MemoryByte>;
23
+ /**
24
+ * Embed a text string.
25
+ * Exposed for query embedding.
26
+ */
27
+ embed(text: string): Promise<number[]>;
28
+ }
29
+ //# sourceMappingURL=encoder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../src/memory/encoder.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1E;;;;GAIG;AACH,qBAAa,iBAAkB,YAAW,eAAe;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAyB;gBAEtC,QAAQ,EAAE,cAAc,CAAC,MAAM,CAAC;IAI5C;;;OAGG;IACG,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC;IAgBtD;;OAEG;IACG,MAAM,CAAC,UAAU,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,CAAC;IAI5D;;;OAGG;IACG,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAI7C"}