@functional-systems/memdrive 0.1.1-dev.1 → 0.1.1-dev.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 (117) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +20 -250
  3. package/dist/commonjs/chunking.d.ts +1 -2
  4. package/dist/commonjs/chunking.d.ts.map +1 -1
  5. package/dist/commonjs/chunking.js +2 -5
  6. package/dist/commonjs/chunking.js.map +1 -1
  7. package/dist/commonjs/config.d.ts +17 -0
  8. package/dist/commonjs/config.d.ts.map +1 -0
  9. package/dist/commonjs/config.js +116 -0
  10. package/dist/commonjs/config.js.map +1 -0
  11. package/dist/commonjs/errors.d.ts +10 -6
  12. package/dist/commonjs/errors.d.ts.map +1 -1
  13. package/dist/commonjs/errors.js +25 -5
  14. package/dist/commonjs/errors.js.map +1 -1
  15. package/dist/commonjs/index.d.ts +15 -5
  16. package/dist/commonjs/index.d.ts.map +1 -1
  17. package/dist/commonjs/index.js +331 -307
  18. package/dist/commonjs/index.js.map +1 -1
  19. package/dist/commonjs/records.d.ts +44 -0
  20. package/dist/commonjs/records.d.ts.map +1 -0
  21. package/dist/commonjs/records.js +88 -0
  22. package/dist/commonjs/records.js.map +1 -0
  23. package/dist/commonjs/search.d.ts +8 -0
  24. package/dist/commonjs/search.d.ts.map +1 -0
  25. package/dist/commonjs/search.js +56 -0
  26. package/dist/commonjs/search.js.map +1 -0
  27. package/dist/commonjs/types.d.ts +84 -252
  28. package/dist/commonjs/types.d.ts.map +1 -1
  29. package/dist/esm/chunking.d.ts +1 -2
  30. package/dist/esm/chunking.d.ts.map +1 -1
  31. package/dist/esm/chunking.js +2 -4
  32. package/dist/esm/chunking.js.map +1 -1
  33. package/dist/esm/config.d.ts +17 -0
  34. package/dist/esm/config.d.ts.map +1 -0
  35. package/dist/esm/config.js +107 -0
  36. package/dist/esm/config.js.map +1 -0
  37. package/dist/esm/errors.d.ts +10 -6
  38. package/dist/esm/errors.d.ts.map +1 -1
  39. package/dist/esm/errors.js +22 -4
  40. package/dist/esm/errors.js.map +1 -1
  41. package/dist/esm/index.d.ts +15 -5
  42. package/dist/esm/index.d.ts.map +1 -1
  43. package/dist/esm/index.js +331 -305
  44. package/dist/esm/index.js.map +1 -1
  45. package/dist/esm/records.d.ts +44 -0
  46. package/dist/esm/records.d.ts.map +1 -0
  47. package/dist/esm/records.js +77 -0
  48. package/dist/esm/records.js.map +1 -0
  49. package/dist/esm/search.d.ts +8 -0
  50. package/dist/esm/search.d.ts.map +1 -0
  51. package/dist/esm/search.js +52 -0
  52. package/dist/esm/search.js.map +1 -0
  53. package/dist/esm/types.d.ts +84 -252
  54. package/dist/esm/types.d.ts.map +1 -1
  55. package/package.json +10 -32
  56. package/src/chunking.ts +2 -3
  57. package/src/config.ts +121 -0
  58. package/src/errors.ts +20 -6
  59. package/src/index.ts +351 -289
  60. package/src/records.ts +83 -0
  61. package/src/search.ts +57 -0
  62. package/src/types.ts +74 -253
  63. package/dist/commonjs/filter.d.ts +0 -30
  64. package/dist/commonjs/filter.d.ts.map +0 -1
  65. package/dist/commonjs/filter.js +0 -224
  66. package/dist/commonjs/filter.js.map +0 -1
  67. package/dist/commonjs/storage.d.ts +0 -274
  68. package/dist/commonjs/storage.d.ts.map +0 -1
  69. package/dist/commonjs/storage.js +0 -861
  70. package/dist/commonjs/storage.js.map +0 -1
  71. package/dist/commonjs/tools/definitions.d.ts +0 -5
  72. package/dist/commonjs/tools/definitions.d.ts.map +0 -1
  73. package/dist/commonjs/tools/definitions.js +0 -129
  74. package/dist/commonjs/tools/definitions.js.map +0 -1
  75. package/dist/commonjs/tools/index.d.ts +0 -6
  76. package/dist/commonjs/tools/index.d.ts.map +0 -1
  77. package/dist/commonjs/tools/index.js +0 -104
  78. package/dist/commonjs/tools/index.js.map +0 -1
  79. package/dist/commonjs/tools/types.d.ts +0 -50
  80. package/dist/commonjs/tools/types.d.ts.map +0 -1
  81. package/dist/commonjs/tools/types.js +0 -3
  82. package/dist/commonjs/tools/types.js.map +0 -1
  83. package/dist/commonjs/values.d.ts +0 -17
  84. package/dist/commonjs/values.d.ts.map +0 -1
  85. package/dist/commonjs/values.js +0 -90
  86. package/dist/commonjs/values.js.map +0 -1
  87. package/dist/esm/filter.d.ts +0 -30
  88. package/dist/esm/filter.d.ts.map +0 -1
  89. package/dist/esm/filter.js +0 -209
  90. package/dist/esm/filter.js.map +0 -1
  91. package/dist/esm/storage.d.ts +0 -274
  92. package/dist/esm/storage.d.ts.map +0 -1
  93. package/dist/esm/storage.js +0 -845
  94. package/dist/esm/storage.js.map +0 -1
  95. package/dist/esm/tools/definitions.d.ts +0 -5
  96. package/dist/esm/tools/definitions.d.ts.map +0 -1
  97. package/dist/esm/tools/definitions.js +0 -126
  98. package/dist/esm/tools/definitions.js.map +0 -1
  99. package/dist/esm/tools/index.d.ts +0 -6
  100. package/dist/esm/tools/index.d.ts.map +0 -1
  101. package/dist/esm/tools/index.js +0 -101
  102. package/dist/esm/tools/index.js.map +0 -1
  103. package/dist/esm/tools/types.d.ts +0 -50
  104. package/dist/esm/tools/types.d.ts.map +0 -1
  105. package/dist/esm/tools/types.js +0 -2
  106. package/dist/esm/tools/types.js.map +0 -1
  107. package/dist/esm/values.d.ts +0 -17
  108. package/dist/esm/values.d.ts.map +0 -1
  109. package/dist/esm/values.js +0 -87
  110. package/dist/esm/values.js.map +0 -1
  111. package/docs/tools.md +0 -163
  112. package/src/filter.ts +0 -189
  113. package/src/storage.ts +0 -765
  114. package/src/tools/definitions.ts +0 -156
  115. package/src/tools/index.ts +0 -97
  116. package/src/tools/types.ts +0 -45
  117. package/src/values.ts +0 -103
package/docs/tools.md DELETED
@@ -1,163 +0,0 @@
1
- # Common memory tools
2
-
3
- `@functional-systems/memdrive/tools` supplies provider-independent tool definitions
4
- and execution functions. Calling agents plan queries, choose evidence and generate
5
- answers. A separate hosted MCP service owns authentication, authorization,
6
- protocol handling, deployment and operational limits. No MCP or model client is
7
- bundled with MemDrive.
8
-
9
- ## Bind tools
10
-
11
- ```ts
12
- import { createTools } from '@functional-systems/memdrive/tools';
13
-
14
- // The application has already opened and authorized this drive.
15
- const tools = await createTools({ drive });
16
- const result = await tools.execute('memdrive_recall', {
17
- requests: [{ query: 'What was agreed about the meeting?', limit: 10 }],
18
- }, { signal });
19
- ```
20
-
21
- Creation reads `drive.describe()` once to build schemas from the actual embedding
22
- configuration and metadata declarations, including field descriptions. Pass an
23
- optional `branch` to bind data operations to that branch; otherwise the supplied
24
- handle's branch is used. Model arguments cannot change the drive, connection or
25
- branch. `describe` remains collection-scoped, as it is in the SDK.
26
-
27
- The default tools are read-only. `createTools({ drive, writable: true })` also
28
- exposes `remember` and `forget`. This switch and tool annotations are not access
29
- control: the service must authorize each call and select the correct handle.
30
- Collection creation/destruction, configuration and branch administration are not
31
- part of this memory-tool surface.
32
-
33
- `definitions` returns detached JSON data. Recreate the registry after changing
34
- the metadata schema; existing input schemas describe the creation-time fields.
35
- A `describe` call reads fresh metadata but does not silently change the registered
36
- tool definitions. Hosted services own any tool-list refresh notification.
37
-
38
- ## Tool catalog
39
-
40
- | Tool | Purpose | Input and result |
41
- | --- | --- | --- |
42
- | `memdrive_recall` | Retrieve evidence for questions or missing facts | `{requests: [...]}` with 1–5 SDK recall requests; `value.results` follows input order, including per-request failures. |
43
- | `memdrive_read` | Expand excerpts to complete originals | `{ids: [...]}` using canonical `memId`; returns `value.mems`, omitting missing IDs. |
44
- | `memdrive_describe` | Discover fields, meanings and search configuration | `{}`; returns the existing SDK drive descriptor. |
45
- | `memdrive_count_values` | Inspect keyword value distributions | `{fields, filter?, excludeIds?, limit?}`; returns buckets, per-field `hasMore` and `asOf`. |
46
- | `memdrive_list` | Browse inventory without a relevance query | `{limit?, cursor?}`; returns original mems and optional `nextCursor`. Continue even after an empty page when a cursor exists. |
47
- | `memdrive_remember` | Add memories or fully replace supplied IDs | `{mems: [...]}`; returns IDs in input order. Available only when writable. |
48
- | `memdrive_forget` | Delete whole memories and their chunks | Exactly `{ids: [...]}` or `{filter: ...}`; success returns `{acknowledged: true}`, not a deletion count. Available only when writable. |
49
-
50
- Descriptions state when to use each operation, parameter meanings and important
51
- limitations. Field schemas carry the actual metadata types and descriptions.
52
- Closed objects reject extra top-level arguments, filters use explicit Boolean
53
- expressions, and SDK validation remains authoritative for dates, byte limits,
54
- vector values and other semantic constraints. No silent coercion or null-to-omit
55
- conversion is performed. Input is detached before asynchronous execution.
56
-
57
- Recall uses the SDK's existing vector or hybrid pipeline and native reranking
58
- settings. It does not add a query-rewriting model, select evidence or execute a
59
- second search. The tool's five-request bound is separate from the SDK's uncapped
60
- batch length; SDK concurrency remains shared across branch views.
61
-
62
- External-vector drives have vector-input schemas. They return references and
63
- metadata without vectors or source text. A model must not invent embeddings;
64
- the caller provides values from the configured vector space.
65
-
66
- Counts accept up to five keyword fields and 100 values per field, matching native
67
- facet limits. They count original memories once rather than counting chunks.
68
- An array may contribute to several buckets. Counts can be cached for 60 seconds;
69
- `asOf` is the fetch time, and `hasMore: null` means completeness is unknown.
70
-
71
- ## Evidence and writes
72
-
73
- A match with `kind: 'mem'` is already complete; `kind: 'chunk'` contains an excerpt
74
- with UTF-16 offsets. Pass its canonical `memId` to `memdrive_read` for surrounding
75
- context. There are no short aliases, session-specific evidence tables, automatic
76
- deduplication or finish/report tools. Callers retain earlier results and decide
77
- whether more retrieval is useful. `excludeIds` excludes whole memories and all
78
- their chunks, so it is not equivalent to hiding only previously seen excerpts.
79
-
80
- Remember without an ID creates a new memory, including when repeated. Supplying
81
- an existing ID fully replaces content and metadata; it is not a patch. Errors may
82
- contain `completedIds`, `uncertainIds` and `notAttemptedIds`. Preserve these outcomes
83
- and reconcile them before retrying; neither tools nor SDK automatically retry a
84
- multi-request write. Deletion success acknowledges the request and does not prove
85
- that any specified ID existed. Cancellation propagates through `AbortSignal` but
86
- does not undo accepted writes.
87
-
88
- ## One execution contract, different transports
89
-
90
- Every execution returns JSON data in one of these envelopes:
91
-
92
- ```ts
93
- { ok: true, value: /* SDK result, or forget acknowledgement */ }
94
- { ok: false, error: { code, message, status?, details? } }
95
- ```
96
-
97
- The tool's `outputSchema` describes both. Errors expose actionable SDK codes and
98
- safe write outcomes, without causes, stack traces or raw transport bodies.
99
- Unexpected exceptions use `TOOL_EXECUTION_FAILED` with a generic message. A batch
100
- recall that ran has `ok: true` even when some `value.results` items contain errors;
101
- inspect each item. Unknown or disabled tools return `UNKNOWN_TOOL` before dispatch.
102
-
103
- Direct function calling can register the same definitions. For OpenAI Responses,
104
- the registration shape is:
105
-
106
- ```ts
107
- const functions = tools.definitions.map(tool => ({
108
- type: 'function', name: tool.name, description: tool.description,
109
- parameters: tool.inputSchema, strict: false,
110
- }));
111
- // On a function call, parse its arguments and pass the same object to execute.
112
- const result = await tools.execute(call.name, JSON.parse(call.arguments), { signal });
113
- const output = JSON.stringify(result);
114
- ```
115
-
116
- These portable schemas preserve optional fields and SDK omission semantics.
117
- They are not advertised as OpenAI strict schemas: that mode requires every
118
- property to be required and uses nullable fields for optionality. Explicitly use
119
- `strict: false`; a future provider-specific strict conversion must also define
120
- and test its argument normalization. Claude registration uses the same `name`
121
- and `description` with `input_schema: tool.inputSchema`.
122
-
123
- A hosted MCP service lists the same definitions and packages the execution data:
124
-
125
- ```ts
126
- const result = await tools.execute(name, argumentsObject, { signal });
127
- const mcpResult = {
128
- isError: !result.ok,
129
- structuredContent: result,
130
- content: [{ type: 'text', text: JSON.stringify(result) }],
131
- };
132
- ```
133
-
134
- This is a mapping example, not an MCP server implementation. The MCP service
135
- handles protocol errors, unknown-tool routing, sessions, cancellation messages
136
- and authentication. In particular, it maps unknown names to the protocol's
137
- unknown-tool error instead of treating them as successful tool dispatches.
138
- It must not change defaults, truncate results silently or implement separate
139
- search logic. Direct function-call output and MCP `structuredContent`/JSON text
140
- carry the same canonical execution result. Provider context limits and differing
141
- agent prompts can still change the model's choices and final answer.
142
-
143
- ## Standards and validation
144
-
145
- Definitions use JSON Schema 2020-12, structured output schemas and tool annotations
146
- following the [MCP tools specification](https://modelcontextprotocol.io/specification/2025-11-25/server/tools).
147
- The [MCP annotation reference](https://modelcontextprotocol.io/specification/2025-11-25/schema#toolannotations)
148
- defines effect hints, not authorization. Reads are marked read-only; remember and
149
- forget can destroy prior data, and remember is not generally idempotent because
150
- IDs may be generated. The tools operate on a bound memory database rather than
151
- arbitrary external destinations.
152
-
153
- The [OpenAI function-calling guide](https://developers.openai.com/api/docs/guides/function-calling)
154
- documents strict-mode requirements and explicit non-strict registration.
155
- The [Claude tool-definition guide](https://platform.claude.com/docs/en/agents-and-tools/tool-use/define-tools)
156
- emphasizes clear purposes, parameter meanings and limitations. These sources
157
- inform the shared contract; they do not imply live certification by a provider.
158
-
159
- Maintained tests compile the schemas with Ajv 2020, validate real SDK fixture
160
- results, exercise branch binding, writable selection, batch correspondence,
161
- partial failures and cancellation, and compare direct/MCP result packaging.
162
- Package checks exercise ESM, CommonJS, public TypeScript types and browser bundles.
163
- Simulated envelope equality is not a live MCP transport or model-quality test.
package/src/filter.ts DELETED
@@ -1,189 +0,0 @@
1
- import type { Filter, FilterScalar, Metadata, MetadataSchema, MetadataType } from './types.js';
2
-
3
- import { requireInput } from './errors.js';
4
-
5
- export const isObject = (value: unknown): value is Record<string, unknown> => value !== null && typeof value === 'object'
6
- && !Array.isArray(value) && [Object.prototype, null].includes(Object.getPrototypeOf(value));
7
- const own = (value: object, key: PropertyKey) => Object.hasOwn(value, key);
8
- const kinds = new Set(['string', 'number', 'boolean', 'datetime', 'string[]']);
9
- export const matchAll = () => ({ queryString: { query: '*:*' } });
10
-
11
- // LambdaDB datetime indexes store epoch milliseconds. Reject finer precision
12
- // instead of silently changing equality/range semantics; trailing zeroes are exact.
13
- export function dateValue(value: unknown) {
14
- if (typeof value !== 'string') return undefined;
15
- const m = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?(Z|[+-]\d{2}:\d{2})$/.exec(value);
16
- if (!m) return undefined;
17
- const year = m[1]!, month = m[2]!, day = m[3]!, hour = m[4]!, minute = m[5]!, second = m[6]!, fraction = m[7] ?? '', zone = m[8]!;
18
- if (/[1-9]/.test(fraction.slice(3))) return undefined;
19
- const y = Number(year), mo = Number(month), d = Number(day);
20
- const days = [31, y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
21
- if (mo < 1 || mo > 12 || d < 1 || d > days[mo - 1]! || Number(hour) > 23
22
- || Number(minute) > 59 || Number(second) > 59
23
- || (zone !== 'Z' && (Number(zone.slice(1, 3)) > 23 || Number(zone.slice(4)) > 59))) return undefined;
24
- const millis = Date.parse(`${year}-${month}-${day}T${hour}:${minute}:${second}${zone}`);
25
- return Number.isFinite(millis) ? BigInt(millis) + BigInt(fraction.padEnd(3, '0').slice(0, 3)) : undefined;
26
- }
27
-
28
- export function validateDescription(description: unknown): string | undefined {
29
- requireInput(description === undefined || (typeof description === 'string' && description.length <= 255),
30
- 'Description must be text of at most 255 characters; use an empty string to clear it.');
31
- return description;
32
- }
33
-
34
- export function validateMetadataSchema(schema: unknown): MetadataSchema {
35
- requireInput(isObject(schema), 'metadataSchema must be an object.');
36
- for (const [name, declaration] of Object.entries(schema)) {
37
- requireInput(/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)
38
- && !['id', '__proto__', 'prototype', 'constructor'].includes(name), 'Metadata field names must be unambiguous identifiers and cannot be reserved.');
39
- requireInput(isObject(declaration) && Object.keys(declaration).every(key => ['type', 'description'].includes(key))
40
- && own(declaration, 'type') && typeof declaration.type === 'string' && kinds.has(declaration.type), 'Declare each metadata field with a supported type and optional description.');
41
- validateDescription(declaration.description);
42
- }
43
- return structuredClone(schema) as MetadataSchema;
44
- }
45
-
46
- export function fieldType(field: unknown, schema: MetadataSchema): MetadataType | undefined {
47
- if (field === 'id') return 'string';
48
- if (field === 'timestamp') return 'datetime';
49
- if (typeof field === 'string' && field.startsWith('metadata.')) {
50
- const name = field.slice(9);
51
- if (own(schema, name)) return schema[name]!.type;
52
- }
53
- return undefined;
54
- }
55
-
56
- export function validScalar(value: unknown, type: MetadataType | undefined) {
57
- if (type === 'string' || type === 'string[]') return typeof value === 'string' && value.length <= 4096;
58
- if (type === 'number') return typeof value === 'number' && Number.isFinite(value);
59
- if (type === 'boolean') return typeof value === 'boolean';
60
- return type === 'datetime' && dateValue(value) !== undefined;
61
- }
62
-
63
- export function validateFilter(filter: unknown, schema: MetadataSchema): Filter | undefined {
64
- if (filter === undefined) return undefined;
65
- const seen = new Set<object>();
66
- const visit = (value: unknown): void => {
67
- requireInput(isObject(value) && !seen.has(value), 'Filter must be an acyclic explicit Boolean expression.');
68
- seen.add(value);
69
- const keys = Object.keys(value);
70
- if (keys.length === 1 && ['and', 'or'].includes(keys[0]!)) {
71
- const children = value[keys[0]!];
72
- requireInput(Array.isArray(children) && children.length > 0, 'and/or require nonempty arrays.');
73
- children.forEach(visit);
74
- } else {
75
- requireInput(keys.length === 3 && ['field', 'op', 'value'].every(key => own(value, key)), 'Filter leaves require exactly field, op and value.');
76
- const type = fieldType(value.field, schema);
77
- requireInput(type !== undefined, 'Filter field must be id, timestamp, or declared metadata.');
78
- if (value.op === 'exists') requireInput(typeof value.value === 'boolean', 'exists requires a boolean.');
79
- else if (value.op === 'in') requireInput(Array.isArray(value.value) && value.value.length > 0
80
- && value.value.every(item => validScalar(item, type)), 'in requires a nonempty list of correctly typed scalar values.');
81
- else {
82
- requireInput(typeof value.op === 'string' && ['eq', 'neq', 'gt', 'gte', 'lt', 'lte'].includes(value.op), 'Unsupported filter operator.');
83
- requireInput(validScalar(value.value, type), 'Filter value does not match the declared field type.');
84
- requireInput(['eq', 'neq'].includes(value.op) || ['number', 'datetime'].includes(type), 'Ordering is supported only for numbers and datetimes.');
85
- }
86
- }
87
- seen.delete(value);
88
- };
89
- visit(filter);
90
- return structuredClone(filter) as Filter;
91
- }
92
-
93
- export interface NativeFilter { queryString?: {query: string; defaultField?: string}; bool?: NativeFilter[]; occur?: string | undefined }
94
-
95
- const queryString = (field: string, query: string) => ({ queryString: { query, defaultField: field } });
96
- // Quote even literal *:*: LambdaDB treats that exact string as match-all with skipSyntax.
97
- const quote = (value: unknown) => `"${String(value).replace(/[\\"]/g, '\\$&')}"`;
98
- const bool = (clauses: NativeFilter[], occur: string): NativeFilter => ({ bool: clauses.map(clause => ({ ...clause, occur })) });
99
- function exists(field: string, type: MetadataType | undefined): NativeFilter {
100
- if (type === 'boolean') return bool([queryString(field, 'true'), queryString(field, 'false')], 'should');
101
- // A bare wildcard is rejected by the native parser's leading-wildcard policy.
102
- return queryString(field, '[* TO *]');
103
- }
104
- function decimal(value: FilterScalar) {
105
- const text = String(value);
106
- if (!text.includes('e')) return text;
107
- // Native numeric queries use DecimalFormat, which does not parse JS's
108
- // lowercase exponent syntax. Expand the shortest round-trippable decimal.
109
- const [mantissa, exponent] = text.split('e') as [string, string];
110
- const sign = mantissa.startsWith('-') ? '-' : '';
111
- const unsigned = sign ? mantissa.slice(1) : mantissa;
112
- const point = (unsigned.indexOf('.') === -1 ? unsigned.length : unsigned.indexOf('.')) + Number(exponent);
113
- const digits = unsigned.replace('.', '');
114
- return sign + (point <= 0 ? `0.${'0'.repeat(-point)}${digits}`
115
- : point >= digits.length ? `${digits}${'0'.repeat(point - digits.length)}`
116
- : `${digits.slice(0, point)}.${digits.slice(point)}`);
117
- }
118
- function equal(field: string, value: FilterScalar, type: MetadataType | undefined): NativeFilter {
119
- const literal = type === 'number' ? decimal(value) : type === 'boolean' ? String(value) : quote(value);
120
- return queryString(field, type === 'number' || type === 'datetime' ? `[${literal} TO ${literal}]` : literal);
121
- }
122
- function nativeFilter(filter: Filter | undefined, schema: MetadataSchema): NativeFilter {
123
- if (filter === undefined) return matchAll();
124
- if (filter.and) return bool(filter.and.map(item => nativeFilter(item, schema)), 'filter');
125
- if (filter.or) return bool(filter.or.map(item => nativeFilter(item, schema)), 'should');
126
- const { op, value } = filter, type = fieldType(filter.field, schema);
127
- const field = filter.field === 'id' ? 'memId' : filter.field;
128
- if (op === 'exists') return value ? exists(field, type) : { bool: [
129
- { ...matchAll(), occur: 'filter' }, { ...exists(field, type), occur: 'must_not' },
130
- ] };
131
- if (op === 'eq') return equal(field, value, type);
132
- if (op === 'neq') return { bool: [
133
- { ...exists(field, type), occur: 'filter' }, { ...equal(field, value, type), occur: 'must_not' },
134
- ] };
135
- if (op === 'in') return bool(value.map(item => equal(field, item, type)), 'should');
136
- const literal = type === 'datetime' ? quote(value) : decimal(value);
137
- const inclusive = ['gte', 'lte'].includes(op);
138
- return queryString(field, `${inclusive ? '[' : '{'}${op.startsWith('g') ? `${literal} TO *` : `* TO ${literal}`}${inclusive ? ']' : '}'}`);
139
- }
140
-
141
- export function compileFilter(filter: unknown, schema: MetadataSchema): NativeFilter {
142
- return nativeFilter(validateFilter(filter, schema), schema);
143
- }
144
-
145
- export function buildFilter(filters: unknown[] = [], excludeIds: string[] = [], schema: MetadataSchema = {}): NativeFilter {
146
- requireInput(Array.isArray(filters) && Array.isArray(excludeIds)
147
- && excludeIds.every(id => typeof id === 'string' && id.length > 0 && id.length <= 512), 'Invalid filters or excluded IDs.');
148
- const clauses = filters.filter(item => item !== undefined).map(item => ({ ...compileFilter(item, schema), occur: 'filter' }));
149
- if (clauses.length === 0) clauses.push({ ...matchAll(), occur: 'filter' });
150
- clauses.push(...excludeIds.map(id => ({ ...equal('memId', id, 'string'), occur: 'must_not' })));
151
- if (clauses.length === 1) { const { occur: _occur, ...clause } = clauses[0]!; return clause; }
152
- return { bool: clauses };
153
- }
154
-
155
- export function buildSearchFilter(filters: unknown[] = [], excludeIds: string[] = [], seenChunkIds: string[] = [], schema: MetadataSchema = {}): NativeFilter {
156
- requireInput(Array.isArray(seenChunkIds) && seenChunkIds.every(id => typeof id === 'string' && id.length > 0),
157
- 'Invalid seen chunk IDs.');
158
- return { bool: [
159
- { ...buildFilter(filters, excludeIds, schema), occur: 'filter' },
160
- ...seenChunkIds.map(id => ({ ...equal('id', id, 'string'), occur: 'must_not' })),
161
- ] };
162
- }
163
-
164
- export function matchesFilter(mem: {id: string; timestamp?: string | undefined; metadata?: Metadata | undefined}, filter: Filter | undefined, schema: MetadataSchema): boolean {
165
- validateFilter(filter, schema);
166
- const visit = (node: Filter | undefined): boolean => {
167
- if (node === undefined) return true;
168
- if (node.and) return node.and.every(visit);
169
- if (node.or) return node.or.some(visit);
170
- const { field, op, value } = node;
171
- const stored = field.startsWith('metadata.') ? mem.metadata?.[field.slice(9)] : mem[field as 'id' | 'timestamp'];
172
- const values = stored === undefined ? [] : Array.isArray(stored) ? stored : [stored];
173
- if (op === 'exists') return (values.length > 0) === value;
174
- const type = fieldType(field, schema);
175
- const convert = (item: unknown) => type === 'datetime' ? dateValue(item) : item;
176
- const same = (expected: FilterScalar) => values.some(item => convert(item) === convert(expected));
177
- if (op === 'eq') return same(value);
178
- if (op === 'neq') return values.length > 0 && !same(value);
179
- if (op === 'in') return value.some(same);
180
- return values.some(item => {
181
- const a = convert(item) as number | bigint, b = convert(value) as number | bigint;
182
- if (op === 'gt') return a > b;
183
- if (op === 'gte') return a >= b;
184
- if (op === 'lt') return a < b;
185
- return a <= b;
186
- });
187
- };
188
- return visit(filter);
189
- }