@cilow/sdk 0.2.0 → 0.3.0

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 (53) hide show
  1. package/LICENSE +201 -21
  2. package/README.md +109 -279
  3. package/dist/abstain.d.ts +43 -0
  4. package/dist/abstain.d.ts.map +1 -0
  5. package/dist/abstain.js +42 -0
  6. package/dist/abstain.js.map +1 -0
  7. package/dist/adapters/anthropic.d.ts +57 -0
  8. package/dist/adapters/anthropic.d.ts.map +1 -0
  9. package/dist/adapters/anthropic.js +57 -0
  10. package/dist/adapters/anthropic.js.map +1 -0
  11. package/dist/adapters/index.d.ts +16 -0
  12. package/dist/adapters/index.d.ts.map +1 -0
  13. package/dist/adapters/index.js +16 -0
  14. package/dist/adapters/index.js.map +1 -0
  15. package/dist/adapters/langchain.d.ts +62 -0
  16. package/dist/adapters/langchain.d.ts.map +1 -0
  17. package/dist/adapters/langchain.js +68 -0
  18. package/dist/adapters/langchain.js.map +1 -0
  19. package/dist/adapters/memory.d.ts +105 -0
  20. package/dist/adapters/memory.d.ts.map +1 -0
  21. package/dist/adapters/memory.js +105 -0
  22. package/dist/adapters/memory.js.map +1 -0
  23. package/dist/adapters/openai.d.ts +56 -0
  24. package/dist/adapters/openai.d.ts.map +1 -0
  25. package/dist/adapters/openai.js +64 -0
  26. package/dist/adapters/openai.js.map +1 -0
  27. package/dist/adapters/remaining.d.ts +52 -0
  28. package/dist/adapters/remaining.d.ts.map +1 -0
  29. package/dist/adapters/remaining.js +67 -0
  30. package/dist/adapters/remaining.js.map +1 -0
  31. package/dist/client.d.ts +563 -0
  32. package/dist/client.d.ts.map +1 -0
  33. package/dist/client.js +653 -0
  34. package/dist/client.js.map +1 -0
  35. package/dist/errors.d.ts +25 -0
  36. package/dist/errors.d.ts.map +1 -0
  37. package/dist/errors.js +28 -0
  38. package/dist/errors.js.map +1 -0
  39. package/dist/hash.d.ts +13 -0
  40. package/dist/hash.d.ts.map +1 -0
  41. package/dist/hash.js +92 -0
  42. package/dist/hash.js.map +1 -0
  43. package/dist/index.d.ts +17 -404
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +17 -482
  46. package/dist/index.js.map +1 -0
  47. package/dist/types.d.ts +817 -0
  48. package/dist/types.d.ts.map +1 -0
  49. package/dist/types.js +19 -0
  50. package/dist/types.js.map +1 -0
  51. package/package.json +32 -46
  52. package/dist/index.d.mts +0 -407
  53. package/dist/index.mjs +0 -449
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Ergonomic helpers over the abstention contract carried by `recall` / `answer`.
3
+ *
4
+ * On abstain the engine returns `abstained: true`, an empty evidence payload, and a machine
5
+ * `reason_code`. These guards encode the control-flow split so a caller branches on typed state,
6
+ * not raw strings. Calibration quality is an external, versioned measurement.
7
+ */
8
+ import type { ExplainReport, ExplainResponse, ReasonCode, TerminalLayer } from "./types.js";
9
+ /** The minimal shape both `RecallResponse` and `AnswerResponse` satisfy. */
10
+ export interface Abstainable {
11
+ abstained: boolean;
12
+ reason_code?: ReasonCode;
13
+ /** Present on `answer` / `context_pack`; `recall` does not carry it. */
14
+ terminal_layer?: TerminalLayer;
15
+ }
16
+ /**
17
+ * True iff this is a retryable INFRASTRUCTURE abstain (the embedding provider failed), not a
18
+ * calibrated "memory does not know". Memory MAY hold the answer — a retry can succeed once the
19
+ * provider recovers.
20
+ */
21
+ export declare function isInfrastructureAbstain(r: Abstainable): boolean;
22
+ /**
23
+ * Which layer stopped the answer (see `TerminalLayer`), or `undefined` when the read answered or
24
+ * the surface does not carry the field (`recall`).
25
+ */
26
+ export declare function stoppedAt(r: Abstainable): TerminalLayer | undefined;
27
+ /**
28
+ * True iff memory abstained because it genuinely lacks grounded context (`below_threshold` /
29
+ * `no_candidates`). Do NOT fabricate past it; ask the user or search elsewhere.
30
+ */
31
+ export declare function isCalibratedAbstain(r: Abstainable): boolean;
32
+ /**
33
+ * Alias for {@link isInfrastructureAbstain} — a retry may succeed once the provider recovers. A
34
+ * calibrated abstain should NOT be retried verbatim (memory has been asked and does not know).
35
+ */
36
+ export declare function shouldRetry(r: Abstainable): boolean;
37
+ /**
38
+ * True iff an `explain` report says the read cleared the gate: found, not abstained, and
39
+ * `best_nonconformity <= q_hat` (the engine's definition of "grounded"). Accepts the whole
40
+ * `ExplainResponse` (a not-found lookup is never grounded) or a bare `ExplainReport`.
41
+ */
42
+ export declare function isGrounded(r: ExplainResponse | ExplainReport): boolean;
43
+ //# sourceMappingURL=abstain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abstain.d.ts","sourceRoot":"","sources":["../src/abstain.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5F,4EAA4E;AAC5E,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,wEAAwE;IACxE,cAAc,CAAC,EAAE,aAAa,CAAC;CAChC;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,EAAE,WAAW,GAAG,OAAO,CAI/D;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,WAAW,GAAG,aAAa,GAAG,SAAS,CAEnE;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,WAAW,GAAG,OAAO,CAI3D;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,WAAW,GAAG,OAAO,CAEnD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,eAAe,GAAG,aAAa,GAAG,OAAO,CAKtE"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * True iff this is a retryable INFRASTRUCTURE abstain (the embedding provider failed), not a
3
+ * calibrated "memory does not know". Memory MAY hold the answer — a retry can succeed once the
4
+ * provider recovers.
5
+ */
6
+ export function isInfrastructureAbstain(r) {
7
+ return (r.abstained && (r.reason_code === "provider_unavailable" || r.terminal_layer === "provider"));
8
+ }
9
+ /**
10
+ * Which layer stopped the answer (see `TerminalLayer`), or `undefined` when the read answered or
11
+ * the surface does not carry the field (`recall`).
12
+ */
13
+ export function stoppedAt(r) {
14
+ return r.abstained ? r.terminal_layer : undefined;
15
+ }
16
+ /**
17
+ * True iff memory abstained because it genuinely lacks grounded context (`below_threshold` /
18
+ * `no_candidates`). Do NOT fabricate past it; ask the user or search elsewhere.
19
+ */
20
+ export function isCalibratedAbstain(r) {
21
+ return (r.abstained && (r.reason_code === "below_threshold" || r.reason_code === "no_candidates"));
22
+ }
23
+ /**
24
+ * Alias for {@link isInfrastructureAbstain} — a retry may succeed once the provider recovers. A
25
+ * calibrated abstain should NOT be retried verbatim (memory has been asked and does not know).
26
+ */
27
+ export function shouldRetry(r) {
28
+ return isInfrastructureAbstain(r);
29
+ }
30
+ /**
31
+ * True iff an `explain` report says the read cleared the gate: found, not abstained, and
32
+ * `best_nonconformity <= q_hat` (the engine's definition of "grounded"). Accepts the whole
33
+ * `ExplainResponse` (a not-found lookup is never grounded) or a bare `ExplainReport`.
34
+ */
35
+ export function isGrounded(r) {
36
+ const report = "found" in r ? (r.found ? r.report : undefined) : r;
37
+ if (!report || report.abstained)
38
+ return false;
39
+ const n = report.best_nonconformity;
40
+ return typeof n === "number" && n <= report.q_hat;
41
+ }
42
+ //# sourceMappingURL=abstain.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abstain.js","sourceRoot":"","sources":["../src/abstain.ts"],"names":[],"mappings":"AAiBA;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CAAC,CAAc;IACpD,OAAO,CACL,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,CAAC,WAAW,KAAK,sBAAsB,IAAI,CAAC,CAAC,cAAc,KAAK,UAAU,CAAC,CAC7F,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,CAAc;IACtC,OAAO,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,SAAS,CAAC;AACpD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,CAAc;IAChD,OAAO,CACL,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,CAAC,WAAW,KAAK,iBAAiB,IAAI,CAAC,CAAC,WAAW,KAAK,eAAe,CAAC,CAC1F,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,CAAc;IACxC,OAAO,uBAAuB,CAAC,CAAC,CAAC,CAAC;AACpC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,CAAkC;IAC3D,MAAM,MAAM,GAA8B,OAAO,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9F,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,SAAS;QAAE,OAAO,KAAK,CAAC;IAC9C,MAAM,CAAC,GAAG,MAAM,CAAC,kBAAkB,CAAC;IACpC,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC;AACpD,CAAC"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Anthropic SDK binding over {@link CilowMemory}, no `@anthropic-ai/sdk` dependency required.
3
+ *
4
+ * Anthropic has no OpenAI-compatible endpoint, so the base_url swap does NOT apply here (R2.2 of the
5
+ * design doc). This binding uses the explicit Messages tool-use path: register {@link cilowRecallTool}
6
+ * (Anthropic tool schema), execute calls with {@link runRecallTool} into a `tool_result` block, and/or
7
+ * pre-inject grounded context with {@link injectMemory}. Both surface the abstained/grounded branch.
8
+ */
9
+ import type { CilowMemory, RecallResult } from "./memory.js";
10
+ /** Structural shape of an Anthropic tool (Messages API `tools:[...]`). */
11
+ export interface AnthropicTool {
12
+ name: string;
13
+ description: string;
14
+ input_schema: Record<string, unknown>;
15
+ }
16
+ /** Minimal Anthropic message shape (structural — no SDK import). */
17
+ export interface AnthropicMessage {
18
+ role: "user" | "assistant";
19
+ content: string | unknown[];
20
+ }
21
+ /** The Anthropic-format memory-recall tool. Register it in `tools:[cilowRecallTool]`. */
22
+ export declare const cilowRecallTool: AnthropicTool;
23
+ export interface RecallToolInput {
24
+ query: string;
25
+ anchor?: string;
26
+ attribute?: string;
27
+ }
28
+ /** A `tool_result` content block to append to the next `user` message. */
29
+ export interface AnthropicToolResult {
30
+ type: "tool_result";
31
+ tool_use_id: string;
32
+ content: string;
33
+ is_error?: boolean;
34
+ }
35
+ /**
36
+ * Execute one `cilow_recall` tool_use. Returns the `tool_result` block (branch-aware content) plus the
37
+ * raw {@link RecallResult}. An INFRASTRUCTURE abstain is marked `is_error: true` (retryable); a
38
+ * calibrated abstain is a normal result whose content instructs the model not to fabricate.
39
+ */
40
+ export declare function runRecallTool(memory: CilowMemory, toolUseId: string, input: RecallToolInput): Promise<{
41
+ toolResult: AnthropicToolResult;
42
+ result: RecallResult;
43
+ }>;
44
+ /**
45
+ * Pre-inject grounded memory ahead of a `messages.create` call: recall, and if grounded return a
46
+ * `system` string block to prepend plus the branch signal. On an abstain `system` is empty and
47
+ * `grounded` is false — the caller decides whether to proceed on the model's own knowledge or stop.
48
+ */
49
+ export declare function injectMemory(memory: CilowMemory, query: string, opts?: {
50
+ anchor?: string;
51
+ attribute?: string;
52
+ }): Promise<{
53
+ system: string;
54
+ grounded: boolean;
55
+ result: RecallResult;
56
+ }>;
57
+ //# sourceMappingURL=anthropic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/adapters/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,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thropic-format memory-recall tool. Register it in `tools:[cilowRecallTool]`. */
2
+ export const cilowRecallTool = {
3
+ name: "cilow_recall",
4
+ description: "Recall grounded facts from verifiable long-term memory. Returns memory-grounded results, or a " +
5
+ "calibrated abstain when memory genuinely does not know — in which case DO NOT fabricate.",
6
+ input_schema: {
7
+ type: "object",
8
+ properties: {
9
+ query: { type: "string", description: "What to look up in memory." },
10
+ anchor: { type: "string", description: "Optional exact entity hint, e.g. a person's name." },
11
+ attribute: { type: "string", description: "Optional exact predicate hint, e.g. 'employer'." },
12
+ },
13
+ required: ["query"],
14
+ },
15
+ };
16
+ /**
17
+ * Execute one `cilow_recall` tool_use. Returns the `tool_result` block (branch-aware content) plus the
18
+ * raw {@link RecallResult}. An INFRASTRUCTURE abstain is marked `is_error: true` (retryable); a
19
+ * calibrated abstain is a normal result whose content instructs the model not to fabricate.
20
+ */
21
+ export async function runRecallTool(memory, toolUseId, input) {
22
+ const result = await memory.recall(input.query, { anchor: input.anchor, attribute: input.attribute });
23
+ if (result.reason_code === "provider_unavailable") {
24
+ return {
25
+ toolResult: { type: "tool_result", tool_use_id: toolUseId, is_error: true, content: `MEMORY TEMPORARILY UNAVAILABLE: ${result.reason ?? "provider error"} — retry.` },
26
+ result,
27
+ };
28
+ }
29
+ if (result.abstained || !result.grounded) {
30
+ return {
31
+ toolResult: { type: "tool_result", tool_use_id: toolUseId, content: `MEMORY ABSTAINED: ${result.reason ?? "memory does not know"}. Do not fabricate; ask the user or say you don't know.` },
32
+ result,
33
+ };
34
+ }
35
+ const lines = result.results.map((h) => `- ${h.text}`).join("\n");
36
+ return {
37
+ toolResult: { type: "tool_result", tool_use_id: toolUseId, content: `MEMORY-GROUNDED FACTS:\n${lines}` },
38
+ result,
39
+ };
40
+ }
41
+ /**
42
+ * Pre-inject grounded memory ahead of a `messages.create` call: recall, and if grounded return a
43
+ * `system` string block to prepend plus the branch signal. On an abstain `system` is empty and
44
+ * `grounded` is false — the caller decides whether to proceed on the model's own knowledge or stop.
45
+ */
46
+ export async function injectMemory(memory, query, opts = {}) {
47
+ const result = await memory.recall(query, opts);
48
+ if (!result.grounded)
49
+ return { system: "", grounded: false, result };
50
+ const lines = result.results.map((h) => `- ${h.text}`).join("\n");
51
+ return {
52
+ system: `Answer using ONLY these memory-grounded facts. If they are insufficient, say so.\n${lines}`,
53
+ grounded: true,
54
+ result,
55
+ };
56
+ }
57
+ //# sourceMappingURL=anthropic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/adapters/anthropic.ts"],"names":[],"mappings":"AAuBA,yFAAyF;AACzF,MAAM,CAAC,MAAM,eAAe,GAAkB;IAC5C,IAAI,EAAE,cAAc;IACpB,WAAW,EACT,gGAAgG;QAChG,0FAA0F;IAC5F,YAAY,EAAE;QACZ,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE;YACV,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,4BAA4B,EAAE;YACpE,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,mDAAmD,EAAE;YAC5F,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,iDAAiD,EAAE;SAC9F;QACD,QAAQ,EAAE,CAAC,OAAO,CAAC;KACpB;CACF,CAAC;AAgBF;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,MAAmB,EACnB,SAAiB,EACjB,KAAsB;IAEtB,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC;IACtG,IAAI,MAAM,CAAC,WAAW,KAAK,sBAAsB,EAAE,CAAC;QAClD,OAAO;YACL,UAAU,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,WAAW,EAAE,SAAS,EAAE,QA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AI;QACd,MAAM;KACP,CAAC;AACJ,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `@cilow/sdk/adapters` — framework bindings over the ONE {@link CilowMemory} contract.
3
+ *
4
+ * Every binding is a thin shim that maps a framework's memory/retriever/tool interface onto the five
5
+ * contract verbs (`ingest` / `recall` / `answer` / `reset` / `health`) and re-exposes the
6
+ * differentiator unchanged: `abstained` + `reason_code` + the `grounded` signal.
7
+ *
8
+ * Reuse the abstain guards from the root export (`isCalibratedAbstain` / `isInfrastructureAbstain`) to
9
+ * branch on an adapter's `RecallResult` / `AnswerResult`.
10
+ */
11
+ export { CilowMemoryClient, grounded, type CilowMemory, type MemoryHit, type RecallResult, type AnswerResult, type IngestResult, type IngestOptions, type RecallOptions, } from "./memory.js";
12
+ export * as openai from "./openai.js";
13
+ export * as anthropic from "./anthropic.js";
14
+ export * as langchain from "./langchain.js";
15
+ export { REMAINING_ADAPTERS, makeLlamaIndexRetriever, makeAutoGenMemory, type AdapterPlan } from "./remaining.js";
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EACL,iBAAiB,EACjB,QAAQ,EACR,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,aAAa,GACnB,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAC5C,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `@cilow/sdk/adapters` — framework bindings over the ONE {@link CilowMemory} contract.
3
+ *
4
+ * Every binding is a thin shim that maps a framework's memory/retriever/tool interface onto the five
5
+ * contract verbs (`ingest` / `recall` / `answer` / `reset` / `health`) and re-exposes the
6
+ * differentiator unchanged: `abstained` + `reason_code` + the `grounded` signal.
7
+ *
8
+ * Reuse the abstain guards from the root export (`isCalibratedAbstain` / `isInfrastructureAbstain`) to
9
+ * branch on an adapter's `RecallResult` / `AnswerResult`.
10
+ */
11
+ export { CilowMemoryClient, grounded, } from "./memory.js";
12
+ export * as openai from "./openai.js";
13
+ export * as anthropic from "./anthropic.js";
14
+ export * as langchain from "./langchain.js";
15
+ export { REMAINING_ADAPTERS, makeLlamaIndexRetriever, makeAutoGenMemory } from "./remaining.js";
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EACL,iBAAiB,EACjB,QAAQ,GAQT,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAC5C,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,iBAAiB,EAAoB,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * LangChain binding over {@link CilowMemory}, no `langchain` dependency required.
3
+ *
4
+ * Two duck-typed classes that satisfy LangChain's structural contracts so they drop into any chain:
5
+ * - {@link CilowRetriever} mirrors `BaseRetriever` (`getRelevantDocuments(query) → Document[]`).
6
+ * - {@link CilowChatMemory} mirrors `BaseMemory` (`memoryKeys` / `loadMemoryVariables` / `saveContext`
7
+ * / `clear`).
8
+ * The abstention contract is preserved: an abstain yields ZERO documents (never a fabricated one), and
9
+ * the grounded/abstain branch is readable via {@link CilowRetriever.recallWithSignal} or the metadata
10
+ * on each returned document.
11
+ */
12
+ import type { CilowMemory, RecallResult } from "./memory.js";
13
+ /** Structural `Document` (LangChain `@langchain/core/documents`). */
14
+ export interface LcDocument {
15
+ pageContent: string;
16
+ metadata: Record<string, unknown>;
17
+ }
18
+ export interface CilowRetrieverOptions {
19
+ k?: number;
20
+ anchor?: string;
21
+ attribute?: string;
22
+ }
23
+ /** A LangChain-compatible retriever backed by Cilow recall. */
24
+ export declare class CilowRetriever {
25
+ private readonly memory;
26
+ private readonly opts;
27
+ lc_namespace: string[];
28
+ constructor(memory: CilowMemory, opts?: CilowRetrieverOptions);
29
+ /** BaseRetriever entry point. Returns memory-grounded docs, or `[]` on an abstain (no fabrication). */
30
+ getRelevantDocuments(query: string): Promise<LcDocument[]>;
31
+ /** LangChain 0.2+ internal hook — delegates to {@link getRelevantDocuments}. */
32
+ _getRelevantDocuments(query: string): Promise<LcDocument[]>;
33
+ /** Escape hatch: the full {@link RecallResult} so a caller can branch on `abstained`/`reason_code`. */
34
+ recallWithSignal(query: string): Promise<RecallResult>;
35
+ }
36
+ export interface CilowChatMemoryOptions {
37
+ /** The variable name loaded into the prompt (default `"history"`). */
38
+ memoryKey?: string;
39
+ /** Which input key to recall against (default: the sole input). */
40
+ inputKey?: string;
41
+ /** Which output key to persist (default: the sole output). */
42
+ outputKey?: string;
43
+ /** Default subject for saved turns. */
44
+ subjectHint?: string;
45
+ anchor?: string;
46
+ attribute?: string;
47
+ }
48
+ /** A LangChain-compatible `BaseMemory`: recall on load, persist turns on save. */
49
+ export declare class CilowChatMemory {
50
+ private readonly memory;
51
+ private readonly opts;
52
+ private readonly memoryKey;
53
+ constructor(memory: CilowMemory, opts?: CilowChatMemoryOptions);
54
+ get memoryKeys(): string[];
55
+ /** Load grounded history for the prompt. On an abstain the variable is an empty string (honest). */
56
+ loadMemoryVariables(values: Record<string, unknown>): Promise<Record<string, unknown>>;
57
+ /** Persist the turn into memory so future recalls can ground on it. */
58
+ saveContext(inputs: Record<string, unknown>, outputs: Record<string, unknown>): Promise<void>;
59
+ clear(): Promise<void>;
60
+ private pick;
61
+ }
62
+ //# sourceMappingURL=langchain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"langchain.d.ts","sourceRoot":"","sources":["../../src/adapters/langchain.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE7D,qEAAqE;AACrE,MAAM,WAAW,UAAU;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC;AAED,MAAM,WAAW,qBAAqB;IACpC,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,+DAA+D;AAC/D,qBAAa,cAAc;IAIvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAJvB,YAAY,WAA2B;gBAGpB,MAAM,EAAE,WAAW,EACnB,IAAI,GAAE,qBAA0B;IAGnD,uGAAuG;IACjG,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAShE,gFAAgF;IAChF,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAI3D,uGAAuG;IACvG,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CAGvD;AAED,MAAM,WAAW,sBAAsB;IACrC,sEAAsE;IACtE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,mEAAmE;IACnE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8DAA8D;IAC9D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,uCAAuC;IACvC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,kFAAkF;AAClF,qBAAa,eAAe;IAIxB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAJvB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;gBAGhB,MAAM,EAAE,WAAW,EACnB,IAAI,GAAE,sBAA2B;IAKpD,IAAI,UAAU,IAAI,MAAM,EAAE,CAEzB;IAED,oGAAoG;IAC9F,mBAAmB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAO5F,uEAAuE;IACjE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAO7F,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B,OAAO,CAAC,IAAI;CAKb"}
@@ -0,0 +1,68 @@
1
+ /** A LangChain-compatible retriever backed by Cilow recall. */
2
+ export class CilowRetriever {
3
+ memory;
4
+ opts;
5
+ lc_namespace = ["cilow", "retrievers"];
6
+ constructor(memory, opts = {}) {
7
+ this.memory = memory;
8
+ this.opts = opts;
9
+ }
10
+ /** BaseRetriever entry point. Returns memory-grounded docs, or `[]` on an abstain (no fabrication). */
11
+ async getRelevantDocuments(query) {
12
+ const r = await this.memory.recall(query, this.opts);
13
+ if (!r.grounded)
14
+ return [];
15
+ return r.results.map((h) => ({
16
+ pageContent: h.text,
17
+ metadata: { id: h.id, entity: h.entity, score: h.score, grounded: true },
18
+ }));
19
+ }
20
+ /** LangChain 0.2+ internal hook — delegates to {@link getRelevantDocuments}. */
21
+ _getRelevantDocuments(query) {
22
+ return this.getRelevantDocuments(query);
23
+ }
24
+ /** Escape hatch: the full {@link RecallResult} so a caller can branch on `abstained`/`reason_code`. */
25
+ recallWithSignal(query) {
26
+ return this.memory.recall(query, this.opts);
27
+ }
28
+ }
29
+ /** A LangChain-compatible `BaseMemory`: recall on load, persist turns on save. */
30
+ export class CilowChatMemory {
31
+ memory;
32
+ opts;
33
+ memoryKey;
34
+ constructor(memory, opts = {}) {
35
+ this.memory = memory;
36
+ this.opts = opts;
37
+ this.memoryKey = opts.memoryKey ?? "history";
38
+ }
39
+ get memoryKeys() {
40
+ return [this.memoryKey];
41
+ }
42
+ /** Load grounded history for the prompt. On an abstain the variable is an empty string (honest). */
43
+ async loadMemoryVariables(values) {
44
+ const query = this.pick(values, this.opts.inputKey);
45
+ if (!query)
46
+ return { [this.memoryKey]: "" };
47
+ const r = await this.memory.recall(query, { anchor: this.opts.anchor, attribute: this.opts.attribute });
48
+ return { [this.memoryKey]: r.grounded ? r.results.map((h) => h.text).join("\n") : "" };
49
+ }
50
+ /** Persist the turn into memory so future recalls can ground on it. */
51
+ async saveContext(inputs, outputs) {
52
+ const input = this.pick(inputs, this.opts.inputKey);
53
+ const output = this.pick(outputs, this.opts.outputKey);
54
+ const text = [input, output].filter(Boolean).join("\n");
55
+ if (text)
56
+ await this.memory.ingest(text, { subjectHint: this.opts.subjectHint });
57
+ }
58
+ async clear() {
59
+ await this.memory.reset();
60
+ }
61
+ pick(bag, key) {
62
+ if (key)
63
+ return String(bag[key] ?? "");
64
+ const vals = Object.values(bag);
65
+ return vals.length ? String(vals[0]) : "";
66
+ }
67
+ }
68
+ //# sourceMappingURL=langchain.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"langchain.js","sourceRoot":"","sources":["../../src/adapters/langchain.ts"],"names":[],"mappings":"AAyBA,+DAA+D;AAC/D,MAAM,OAAO,cAAc;IAIN;IACA;IAJnB,YAAY,GAAG,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;IAEvC,YACmB,MAAmB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nF,CAAC;IAED,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC5B,CAAC;IAEO,IAAI,CAAC,GAA4B,EAAE,GAAY;QACrD,IAAI,GAAG;YAAE,OAAO,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;QACvC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAChC,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC5C,CAAC;CACF"}
@@ -0,0 +1,105 @@
1
+ /**
2
+ * `CilowMemory` — the ONE adapter contract every framework binding maps onto.
3
+ *
4
+ * It is a tiny, framework-neutral interface over the shipped REST surface (`/v1/memories/*` +
5
+ * the `/v1/chat/completions` proxy). Each framework adapter (OpenAI, Anthropic, LangChain, …) is a
6
+ * ≤150-line shim that maps the framework's memory/retriever/tool interface onto these five calls and
7
+ * re-exposes the differentiator unchanged: `abstained` + `reason_code` + the `grounded` signal.
8
+ *
9
+ * Transport is the existing {@link CilowClient} — the bindings call the SDK, never raw HTTP.
10
+ */
11
+ import { CilowClient, type CilowClientOptions } from "../client.js";
12
+ import type { ReasonCode } from "../types.js";
13
+ import type { Abstainable } from "../abstain.js";
14
+ /** One recalled memory line — `text` is ready to inject; structured fields are kept alongside. */
15
+ export interface MemoryHit {
16
+ id: string;
17
+ text: string;
18
+ entity: string;
19
+ value: string;
20
+ score: number;
21
+ }
22
+ /**
23
+ * The result of {@link CilowMemory.recall}. Carries the abstention contract verbatim so a caller
24
+ * branches on INTENT (`isCalibratedAbstain` / `isInfrastructureAbstain` from `../abstain.js`).
25
+ * `grounded` is the universal drop-in signal: true ⇒ these hits are memory-grounded; false ⇒ the
26
+ * caller must fall back to the model's own parametric knowledge (or abstain).
27
+ */
28
+ export interface RecallResult extends Abstainable {
29
+ results: MemoryHit[];
30
+ grounded: boolean;
31
+ reason?: string;
32
+ reason_code?: ReasonCode;
33
+ margin?: number;
34
+ packedTokens?: number;
35
+ }
36
+ /** The result of {@link CilowMemory.answer} — a synthesized answer + the same grounded/abstain signal. */
37
+ export interface AnswerResult extends Abstainable {
38
+ text: string;
39
+ grounded: boolean;
40
+ reason?: string;
41
+ reason_code?: ReasonCode;
42
+ }
43
+ /** The receipt of an {@link CilowMemory.ingest}. */
44
+ export interface IngestResult {
45
+ count: number;
46
+ ids: string[];
47
+ }
48
+ export interface IngestOptions {
49
+ /** Default subject when the text names none (e.g. a user/session id). */
50
+ subjectHint?: string;
51
+ /** Dedup / idempotency key (single-item ingest). */
52
+ customId?: string;
53
+ }
54
+ export interface RecallOptions {
55
+ /** Max hits to surface (advisory; the packer decides the minimal sufficient set). */
56
+ k?: number;
57
+ /** Fast-path entity hint, e.g. `"Maya"`. */
58
+ anchor?: string;
59
+ /** Fast-path predicate hint, e.g. `"employer"`. */
60
+ attribute?: string;
61
+ }
62
+ /**
63
+ * The single contract. Five verbs; every framework binding targets exactly these.
64
+ * - `ingest` → `/v1/memories/add` (or `/batch`)
65
+ * - `recall` → `/v1/memories/search`
66
+ * - `answer` → `/v1/memories/search` (mode `"answer"`)
67
+ * - `reset` → `/v1/memories/list` + `/delete`
68
+ * - `health` → `GET /health`
69
+ * plus `proxyBaseUrl()` — the drop-in OpenAI-compatible base URL that emits the grounded header.
70
+ */
71
+ export interface CilowMemory {
72
+ ingest(items: string | string[], opts?: IngestOptions): Promise<IngestResult>;
73
+ recall(query: string, opts?: RecallOptions): Promise<RecallResult>;
74
+ answer(query: string, opts?: RecallOptions): Promise<AnswerResult>;
75
+ reset(): Promise<{
76
+ deleted: number;
77
+ }>;
78
+ health(): Promise<boolean>;
79
+ /** The OpenAI-compatible proxy base URL (`…/v1`) to hand a framework's base_url swap. */
80
+ proxyBaseUrl(): string;
81
+ }
82
+ /** Compute the universal grounded signal from an abstention-carrying response + hit count. */
83
+ export declare function grounded(r: {
84
+ abstained: boolean;
85
+ }, hitCount: number): boolean;
86
+ /**
87
+ * The concrete {@link CilowMemory} over a {@link CilowClient}. Construct from client options, or wrap
88
+ * an existing client with {@link CilowMemoryClient.fromClient}.
89
+ */
90
+ export declare class CilowMemoryClient implements CilowMemory {
91
+ private readonly client;
92
+ private readonly baseUrl;
93
+ constructor(client: CilowClient, baseUrl: string);
94
+ static create(opts: CilowClientOptions): CilowMemoryClient;
95
+ static fromClient(client: CilowClient, baseUrl: string): CilowMemoryClient;
96
+ ingest(items: string | string[], opts?: IngestOptions): Promise<IngestResult>;
97
+ recall(query: string, opts?: RecallOptions): Promise<RecallResult>;
98
+ answer(query: string, opts?: RecallOptions): Promise<AnswerResult>;
99
+ reset(): Promise<{
100
+ deleted: number;
101
+ }>;
102
+ health(): Promise<boolean>;
103
+ proxyBaseUrl(): string;
104
+ }
105
+ //# sourceMappingURL=memory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/adapters/memory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjD,kGAAkG;AAClG,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;GAKG;AACH,MAAM,WAAW,YAAa,SAAQ,WAAW;IAC/C,OAAO,EAAE,SAAS,EAAE,CAAC;IACrB,QAAQ,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,W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;IACzF,YAAY,IAAI,MAAM,CAAC;CACxB;AAED,8FAA8F;AAC9F,wBAAgB,QAAQ,CAAC,CAAC,EAAE;IAAE,SAAS,EAAE,OAAO,CAAA;CAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAE7E;AAED;;;GAGG;AACH,qBAAa,iBAAkB,YAAW,WAAW;IAGvC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAFnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAEJ,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM;IAIjE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,kBAAkB,GAAG,iBAAiB;IAI1D,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,GAAG,iBAAiB;IAIpE,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,IAAI,GAAE,aAAkB,GAAG,OAAO,CAAC,YAAY,CAAC;IAgBjF,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,aAAkB,GAAG,OAAO,CAAC,YAAY,CAAC;IAuBtE,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,aAAkB,GAAG,OAAO,CAAC,YAAY,CAAC;IAetE,KAAK,IAAI,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAe3C,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC;IAI1B,YAAY,IAAI,MAAM;CAGvB"}
@@ -0,0 +1,105 @@
1
+ /**
2
+ * `CilowMemory` — the ONE adapter contract every framework binding maps onto.
3
+ *
4
+ * It is a tiny, framework-neutral interface over the shipped REST surface (`/v1/memories/*` +
5
+ * the `/v1/chat/completions` proxy). Each framework adapter (OpenAI, Anthropic, LangChain, …) is a
6
+ * ≤150-line shim that maps the framework's memory/retriever/tool interface onto these five calls and
7
+ * re-exposes the differentiator unchanged: `abstained` + `reason_code` + the `grounded` signal.
8
+ *
9
+ * Transport is the existing {@link CilowClient} — the bindings call the SDK, never raw HTTP.
10
+ */
11
+ import { CilowClient } from "../client.js";
12
+ /** Compute the universal grounded signal from an abstention-carrying response + hit count. */
13
+ export function grounded(r, hitCount) {
14
+ return !r.abstained && hitCount > 0;
15
+ }
16
+ /**
17
+ * The concrete {@link CilowMemory} over a {@link CilowClient}. Construct from client options, or wrap
18
+ * an existing client with {@link CilowMemoryClient.fromClient}.
19
+ */
20
+ export class CilowMemoryClient {
21
+ client;
22
+ baseUrl;
23
+ constructor(client, baseUrl) {
24
+ this.client = client;
25
+ this.baseUrl = baseUrl.replace(/\/+$/, "");
26
+ }
27
+ static create(opts) {
28
+ return new CilowMemoryClient(new CilowClient(opts), opts.baseUrl);
29
+ }
30
+ static fromClient(client, baseUrl) {
31
+ return new CilowMemoryClient(client, baseUrl);
32
+ }
33
+ async ingest(items, opts = {}) {
34
+ const list = Array.isArray(items) ? items : [items];
35
+ if (list.length === 1) {
36
+ const r = await this.client.memories.add(list[0], {
37
+ subjectHint: opts.subjectHint,
38
+ customId: opts.customId,
39
+ });
40
+ return { count: r.count, ids: r.memories.map((m) => m.id) };
41
+ }
42
+ const r = await this.client.memories.batchAdd(list.map((content) => ({ content, subjectHint: opts.subjectHint })));
43
+ const ids = r.results.flatMap((res) => res.memories.map((m) => m.id));
44
+ return { count: ids.length, ids };
45
+ }
46
+ async recall(query, opts = {}) {
47
+ const r = await this.client.memories.search(query, {
48
+ anchor: opts.anchor,
49
+ attribute: opts.attribute,
50
+ });
51
+ const results = (r.results ?? []).slice(0, opts.k ?? r.results?.length ?? 0).map((h) => ({
52
+ id: h.id,
53
+ text: h.text,
54
+ entity: h.entity ?? "",
55
+ value: h.value ?? h.text,
56
+ score: h.score ?? 0,
57
+ }));
58
+ return {
59
+ results,
60
+ abstained: r.abstained,
61
+ grounded: grounded(r, results.length),
62
+ reason: r.reason,
63
+ reason_code: r.reason_code,
64
+ margin: r.margin,
65
+ packedTokens: r.packed_tokens,
66
+ };
67
+ }
68
+ async answer(query, opts = {}) {
69
+ const r = await this.client.memories.search(query, {
70
+ anchor: opts.anchor,
71
+ attribute: opts.attribute,
72
+ mode: "answer",
73
+ });
74
+ return {
75
+ text: r.context_text ?? "",
76
+ abstained: r.abstained,
77
+ grounded: !r.abstained && !!r.context_text,
78
+ reason: r.reason,
79
+ reason_code: r.reason_code,
80
+ };
81
+ }
82
+ async reset() {
83
+ let deleted = 0;
84
+ // Page through active memories and retire each (there is no bulk-reset REST verb).
85
+ for (;;) {
86
+ const page = await this.client.memories.list({ activeOnly: true, limit: 200 });
87
+ if (!page.memories.length)
88
+ break;
89
+ for (const m of page.memories) {
90
+ const res = await this.client.memories.delete(m.id);
91
+ deleted += res.deleted ?? 0;
92
+ }
93
+ if (page.memories.length < 200)
94
+ break;
95
+ }
96
+ return { deleted };
97
+ }
98
+ health() {
99
+ return this.client.health();
100
+ }
101
+ proxyBaseUrl() {
102
+ return `${this.baseUrl}/v1`;
103
+ }
104
+ }
105
+ //# sourceMappingURL=memory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.js","sourceRoot":"","sources":["../../src/adapters/memory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,WAAW,EAA2B,MAAM,cAAc,CAAC;AA6EpE,8FAA8F;AAC9F,MAAM,UAAU,QAAQ,CAAC,CAAyB,EAAE,QAAgB;IAClE,OAAO,CAAC,CAAC,CAAC,SAAS,IAAI,QAAQ,GAAG,CAAC,CAAC;AACtC,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,iBAAiB;IAGC;IAFZ,OAAO,CAAS;IAEjC,YAA6B,MAAmB,EAAE,OAAe;QAApC,WAAM,GAAN,MAAM,CAAa;QAC9C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,CAAC,MAAM,CAAC,IAAwB;QACpC,OAA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