mem-os-python 1.6.27__py3-none-any.whl
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.
- mem_os_python-1.6.27.dist-info/METADATA +1032 -0
- mem_os_python-1.6.27.dist-info/RECORD +253 -0
- mem_os_python-1.6.27.dist-info/WHEEL +4 -0
- mem_os_python-1.6.27.dist-info/entry_points.txt +2 -0
- mem_os_python-1.6.27.dist-info/licenses/LICENSE +21 -0
- memos/__init__.py +16 -0
- memos/_js/adapters/anthropic.d.ts +279 -0
- memos/_js/adapters/anthropic.d.ts.map +1 -0
- memos/_js/adapters/anthropic.js +348 -0
- memos/_js/adapters/anthropic.js.map +1 -0
- memos/_js/adapters/base.d.ts +10 -0
- memos/_js/adapters/base.d.ts.map +1 -0
- memos/_js/adapters/base.js +2 -0
- memos/_js/adapters/base.js.map +1 -0
- memos/_js/adapters/openai.d.ts +264 -0
- memos/_js/adapters/openai.d.ts.map +1 -0
- memos/_js/adapters/openai.js +342 -0
- memos/_js/adapters/openai.js.map +1 -0
- memos/_js/apps/explorer.d.ts +244 -0
- memos/_js/apps/explorer.d.ts.map +1 -0
- memos/_js/apps/explorer.js +738 -0
- memos/_js/apps/explorer.js.map +1 -0
- memos/_js/citations.d.ts +65 -0
- memos/_js/citations.d.ts.map +1 -0
- memos/_js/citations.js +143 -0
- memos/_js/citations.js.map +1 -0
- memos/_js/cli-browse.d.ts +3 -0
- memos/_js/cli-browse.d.ts.map +1 -0
- memos/_js/cli-browse.js +129 -0
- memos/_js/cli-browse.js.map +1 -0
- memos/_js/cli-completion.d.ts +31 -0
- memos/_js/cli-completion.d.ts.map +1 -0
- memos/_js/cli-completion.js +340 -0
- memos/_js/cli-completion.js.map +1 -0
- memos/_js/cli-help.d.ts +18 -0
- memos/_js/cli-help.d.ts.map +1 -0
- memos/_js/cli-help.js +357 -0
- memos/_js/cli-help.js.map +1 -0
- memos/_js/cli.d.ts +28 -0
- memos/_js/cli.d.ts.map +1 -0
- memos/_js/cli.js +2880 -0
- memos/_js/cli.js.map +1 -0
- memos/_js/compression.d.ts +25 -0
- memos/_js/compression.d.ts.map +1 -0
- memos/_js/compression.js +319 -0
- memos/_js/compression.js.map +1 -0
- memos/_js/confidence-machine.d.ts +99 -0
- memos/_js/confidence-machine.d.ts.map +1 -0
- memos/_js/confidence-machine.js +222 -0
- memos/_js/confidence-machine.js.map +1 -0
- memos/_js/context-pack.d.ts +506 -0
- memos/_js/context-pack.d.ts.map +1 -0
- memos/_js/context-pack.js +1435 -0
- memos/_js/context-pack.js.map +1 -0
- memos/_js/contradictions.d.ts +160 -0
- memos/_js/contradictions.d.ts.map +1 -0
- memos/_js/contradictions.js +255 -0
- memos/_js/contradictions.js.map +1 -0
- memos/_js/dashboard.d.ts +30 -0
- memos/_js/dashboard.d.ts.map +1 -0
- memos/_js/dashboard.js +302 -0
- memos/_js/dashboard.js.map +1 -0
- memos/_js/ebbinghaus.d.ts +29 -0
- memos/_js/ebbinghaus.d.ts.map +1 -0
- memos/_js/ebbinghaus.js +32 -0
- memos/_js/ebbinghaus.js.map +1 -0
- memos/_js/embedding-queue.d.ts +190 -0
- memos/_js/embedding-queue.d.ts.map +1 -0
- memos/_js/embedding-queue.js +316 -0
- memos/_js/embedding-queue.js.map +1 -0
- memos/_js/embeddings.d.ts +187 -0
- memos/_js/embeddings.d.ts.map +1 -0
- memos/_js/embeddings.js +633 -0
- memos/_js/embeddings.js.map +1 -0
- memos/_js/entity-extraction.d.ts +54 -0
- memos/_js/entity-extraction.d.ts.map +1 -0
- memos/_js/entity-extraction.js +325 -0
- memos/_js/entity-extraction.js.map +1 -0
- memos/_js/entity-index.d.ts +93 -0
- memos/_js/entity-index.d.ts.map +1 -0
- memos/_js/entity-index.js +165 -0
- memos/_js/entity-index.js.map +1 -0
- memos/_js/event-memory.d.ts +147 -0
- memos/_js/event-memory.d.ts.map +1 -0
- memos/_js/event-memory.js +290 -0
- memos/_js/event-memory.js.map +1 -0
- memos/_js/external-import.d.ts +98 -0
- memos/_js/external-import.d.ts.map +1 -0
- memos/_js/external-import.js +669 -0
- memos/_js/external-import.js.map +1 -0
- memos/_js/fidelity.d.ts +367 -0
- memos/_js/fidelity.d.ts.map +1 -0
- memos/_js/fidelity.js +911 -0
- memos/_js/fidelity.js.map +1 -0
- memos/_js/graph-expansion.d.ts +119 -0
- memos/_js/graph-expansion.d.ts.map +1 -0
- memos/_js/graph-expansion.js +213 -0
- memos/_js/graph-expansion.js.map +1 -0
- memos/_js/graph-mermaid.d.ts +13 -0
- memos/_js/graph-mermaid.d.ts.map +1 -0
- memos/_js/graph-mermaid.js +32 -0
- memos/_js/graph-mermaid.js.map +1 -0
- memos/_js/graph.d.ts +170 -0
- memos/_js/graph.d.ts.map +1 -0
- memos/_js/graph.js +482 -0
- memos/_js/graph.js.map +1 -0
- memos/_js/harness.d.ts +63 -0
- memos/_js/harness.d.ts.map +1 -0
- memos/_js/harness.js +93 -0
- memos/_js/harness.js.map +1 -0
- memos/_js/importance.d.ts +118 -0
- memos/_js/importance.d.ts.map +1 -0
- memos/_js/importance.js +144 -0
- memos/_js/importance.js.map +1 -0
- memos/_js/index.d.ts +51 -0
- memos/_js/index.d.ts.map +1 -0
- memos/_js/index.js +53 -0
- memos/_js/index.js.map +1 -0
- memos/_js/mcp.d.ts +58 -0
- memos/_js/mcp.d.ts.map +1 -0
- memos/_js/mcp.js +1853 -0
- memos/_js/mcp.js.map +1 -0
- memos/_js/memory.d.ts +1762 -0
- memos/_js/memory.d.ts.map +1 -0
- memos/_js/memory.js +6283 -0
- memos/_js/memory.js.map +1 -0
- memos/_js/procedural.d.ts +74 -0
- memos/_js/procedural.d.ts.map +1 -0
- memos/_js/procedural.js +125 -0
- memos/_js/procedural.js.map +1 -0
- memos/_js/project-config.d.ts +68 -0
- memos/_js/project-config.d.ts.map +1 -0
- memos/_js/project-config.js +59 -0
- memos/_js/project-config.js.map +1 -0
- memos/_js/proof-of-forgetting.d.ts +57 -0
- memos/_js/proof-of-forgetting.d.ts.map +1 -0
- memos/_js/proof-of-forgetting.js +80 -0
- memos/_js/proof-of-forgetting.js.map +1 -0
- memos/_js/provenance.d.ts +122 -0
- memos/_js/provenance.d.ts.map +1 -0
- memos/_js/provenance.js +182 -0
- memos/_js/provenance.js.map +1 -0
- memos/_js/quarantine-commands.d.ts +23 -0
- memos/_js/quarantine-commands.d.ts.map +1 -0
- memos/_js/quarantine-commands.js +42 -0
- memos/_js/quarantine-commands.js.map +1 -0
- memos/_js/quarantine.d.ts +120 -0
- memos/_js/quarantine.d.ts.map +1 -0
- memos/_js/quarantine.js +873 -0
- memos/_js/quarantine.js.map +1 -0
- memos/_js/redact.d.ts +12 -0
- memos/_js/redact.d.ts.map +1 -0
- memos/_js/redact.js +26 -0
- memos/_js/redact.js.map +1 -0
- memos/_js/repl.d.ts +57 -0
- memos/_js/repl.d.ts.map +1 -0
- memos/_js/repl.js +295 -0
- memos/_js/repl.js.map +1 -0
- memos/_js/rerank-local.d.ts +61 -0
- memos/_js/rerank-local.d.ts.map +1 -0
- memos/_js/rerank-local.js +210 -0
- memos/_js/rerank-local.js.map +1 -0
- memos/_js/retain-filter.d.ts +98 -0
- memos/_js/retain-filter.d.ts.map +1 -0
- memos/_js/retain-filter.js +232 -0
- memos/_js/retain-filter.js.map +1 -0
- memos/_js/retrieval.d.ts +265 -0
- memos/_js/retrieval.d.ts.map +1 -0
- memos/_js/retrieval.js +1018 -0
- memos/_js/retrieval.js.map +1 -0
- memos/_js/revert.d.ts +77 -0
- memos/_js/revert.d.ts.map +1 -0
- memos/_js/revert.js +252 -0
- memos/_js/revert.js.map +1 -0
- memos/_js/scope.d.ts +30 -0
- memos/_js/scope.d.ts.map +1 -0
- memos/_js/scope.js +71 -0
- memos/_js/scope.js.map +1 -0
- memos/_js/semantic-screen.d.ts +152 -0
- memos/_js/semantic-screen.d.ts.map +1 -0
- memos/_js/semantic-screen.js +290 -0
- memos/_js/semantic-screen.js.map +1 -0
- memos/_js/session-hooks.d.ts +127 -0
- memos/_js/session-hooks.d.ts.map +1 -0
- memos/_js/session-hooks.js +482 -0
- memos/_js/session-hooks.js.map +1 -0
- memos/_js/storage/sqlite.d.ts +427 -0
- memos/_js/storage/sqlite.d.ts.map +1 -0
- memos/_js/storage/sqlite.js +2428 -0
- memos/_js/storage/sqlite.js.map +1 -0
- memos/_js/sync.d.ts +213 -0
- memos/_js/sync.d.ts.map +1 -0
- memos/_js/sync.js +389 -0
- memos/_js/sync.js.map +1 -0
- memos/_js/tamper-log.d.ts +98 -0
- memos/_js/tamper-log.d.ts.map +1 -0
- memos/_js/tamper-log.js +327 -0
- memos/_js/tamper-log.js.map +1 -0
- memos/_js/temporal.d.ts +92 -0
- memos/_js/temporal.d.ts.map +1 -0
- memos/_js/temporal.js +536 -0
- memos/_js/temporal.js.map +1 -0
- memos/_js/time-travel.d.ts +38 -0
- memos/_js/time-travel.d.ts.map +1 -0
- memos/_js/time-travel.js +46 -0
- memos/_js/time-travel.js.map +1 -0
- memos/_js/timestamp-format.d.ts +18 -0
- memos/_js/timestamp-format.d.ts.map +1 -0
- memos/_js/timestamp-format.js +52 -0
- memos/_js/timestamp-format.js.map +1 -0
- memos/_js/triple-extraction.d.ts +31 -0
- memos/_js/triple-extraction.d.ts.map +1 -0
- memos/_js/triple-extraction.js +116 -0
- memos/_js/triple-extraction.js.map +1 -0
- memos/_js/types.d.ts +2474 -0
- memos/_js/types.d.ts.map +1 -0
- memos/_js/types.js +20 -0
- memos/_js/types.js.map +1 -0
- memos/_js/updater.d.ts +95 -0
- memos/_js/updater.d.ts.map +1 -0
- memos/_js/updater.js +268 -0
- memos/_js/updater.js.map +1 -0
- memos/_js/version.d.ts +17 -0
- memos/_js/version.d.ts.map +1 -0
- memos/_js/version.js +34 -0
- memos/_js/version.js.map +1 -0
- memos/_js/vision/clip-query-cache.d.ts +58 -0
- memos/_js/vision/clip-query-cache.d.ts.map +1 -0
- memos/_js/vision/clip-query-cache.js +79 -0
- memos/_js/vision/clip-query-cache.js.map +1 -0
- memos/_js/vision/clip.d.ts +113 -0
- memos/_js/vision/clip.d.ts.map +1 -0
- memos/_js/vision/clip.js +224 -0
- memos/_js/vision/clip.js.map +1 -0
- memos/_js/vision/fake-clip.d.ts +55 -0
- memos/_js/vision/fake-clip.d.ts.map +1 -0
- memos/_js/vision/fake-clip.js +122 -0
- memos/_js/vision/fake-clip.js.map +1 -0
- memos/_js/vision/index.d.ts +15 -0
- memos/_js/vision/index.d.ts.map +1 -0
- memos/_js/vision/index.js +13 -0
- memos/_js/vision/index.js.map +1 -0
- memos/_js/vision/store.d.ts +137 -0
- memos/_js/vision/store.d.ts.map +1 -0
- memos/_js/vision/store.js +383 -0
- memos/_js/vision/store.js.map +1 -0
- memos/adapters/__init__.py +0 -0
- memos/adapters/crewai.py +385 -0
- memos/adapters/langchain.py +301 -0
- memos/adapters/ollama.py +396 -0
- memos/server/__init__.py +0 -0
- memos/server/main.py +547 -0
- memos/server/routes.py +360 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MemOS Anthropic Adapter — first-class memory integration for the
|
|
3
|
+
* Anthropic SDK (Claude).
|
|
4
|
+
*
|
|
5
|
+
* Wraps a MemOS instance and exposes helpers that map cleanly onto the
|
|
6
|
+
* Anthropic Messages API: a memory-backed tool definition, system-prompt
|
|
7
|
+
* context injection, automatic fact extraction from conversations, and
|
|
8
|
+
* simple store/forget helpers.
|
|
9
|
+
*
|
|
10
|
+
* The adapter has **zero hard dependencies** on the `@anthropic-ai/sdk`
|
|
11
|
+
* package. Every exported message/tool/content-block shape is structurally
|
|
12
|
+
* compatible with the corresponding SDK types, so they can be passed
|
|
13
|
+
* straight into `client.messages.create(...)` with no glue layer. Install
|
|
14
|
+
* `@anthropic-ai/sdk` only if you want to call the model; the adapter
|
|
15
|
+
* itself only needs MemOS.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import Anthropic from "@anthropic-ai/sdk";
|
|
20
|
+
* import { createAnthropicMemory } from "@mem-os/sdk/anthropic";
|
|
21
|
+
*
|
|
22
|
+
* const { plugin } = await createAnthropicMemory({ dbPath: "./my-app.db" });
|
|
23
|
+
* const client = new Anthropic();
|
|
24
|
+
*
|
|
25
|
+
* const message = await client.messages.create({
|
|
26
|
+
* model: "claude-3-5-sonnet-20241022",
|
|
27
|
+
* max_tokens: 1024,
|
|
28
|
+
* system: "You are a helpful assistant.",
|
|
29
|
+
* messages: [{ role: "user", content: "What theme do I like?" }],
|
|
30
|
+
* tools: [plugin.memoryTool],
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @module @memos/adapters/anthropic
|
|
35
|
+
*/
|
|
36
|
+
import { MemOS } from "../index.js";
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// Plugin
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
/**
|
|
41
|
+
* Anthropic SDK memory plugin backed by MemOS.
|
|
42
|
+
*
|
|
43
|
+
* Wrap an existing MemOS instance, or use {@link createAnthropicMemory} to
|
|
44
|
+
* construct both in one call. The plugin does not call the Anthropic API
|
|
45
|
+
* itself — it prepares memories (as tools, tool-result blocks, and
|
|
46
|
+
* system-prompt strings) for you to feed into your own
|
|
47
|
+
* `client.messages.create` calls.
|
|
48
|
+
*/
|
|
49
|
+
export class MemOSAnthropicPlugin {
|
|
50
|
+
/** The wrapped MemOS instance. */
|
|
51
|
+
memos;
|
|
52
|
+
opts;
|
|
53
|
+
/**
|
|
54
|
+
* Create a new Anthropic memory plugin.
|
|
55
|
+
*
|
|
56
|
+
* @param memos - An initialised MemOS instance.
|
|
57
|
+
* @param opts - Plugin options. All fields optional with sensible defaults.
|
|
58
|
+
*/
|
|
59
|
+
constructor(memos, opts = {}) {
|
|
60
|
+
this.memos = memos;
|
|
61
|
+
this.opts = {
|
|
62
|
+
maxContextMemories: opts.maxContextMemories ?? 5,
|
|
63
|
+
autoExtractFacts: opts.autoExtractFacts ?? true,
|
|
64
|
+
minMessageLength: opts.minMessageLength ?? 15,
|
|
65
|
+
source: opts.source ?? "user_input",
|
|
66
|
+
minConfidence: opts.minConfidence ?? 0.6,
|
|
67
|
+
namespace: opts.namespace ?? "default",
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Anthropic tool definition that lets the model search the user's memory.
|
|
72
|
+
* Pass it via the `tools` parameter of `client.messages.create`.
|
|
73
|
+
*
|
|
74
|
+
* When the model invokes it (a `tool_use` content block), call
|
|
75
|
+
* {@link searchMemories} and return the `tool_result` block (built via
|
|
76
|
+
* the result's `toToolResult(toolUseId)`) as a `user` message.
|
|
77
|
+
*/
|
|
78
|
+
get memoryTool() {
|
|
79
|
+
return {
|
|
80
|
+
name: "search_memories",
|
|
81
|
+
description: "Search the user's persistent MemOS memory for relevant facts, " +
|
|
82
|
+
"preferences, and context. Call this before answering personal or " +
|
|
83
|
+
"preference questions, or when prior context seems relevant.",
|
|
84
|
+
input_schema: {
|
|
85
|
+
type: "object",
|
|
86
|
+
properties: {
|
|
87
|
+
query: {
|
|
88
|
+
type: "string",
|
|
89
|
+
description: "Natural-language search query.",
|
|
90
|
+
},
|
|
91
|
+
limit: {
|
|
92
|
+
type: "number",
|
|
93
|
+
description: "Maximum number of memories to return.",
|
|
94
|
+
default: 5,
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
required: ["query"],
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Extract facts from a conversation and store them in MemOS.
|
|
103
|
+
*
|
|
104
|
+
* Runs MemOS' local rule-based fact extractor over the conversation,
|
|
105
|
+
* persisting any candidate facts whose confidence meets the configured
|
|
106
|
+
* `minConfidence` threshold. Near-duplicates are skipped automatically
|
|
107
|
+
* when embeddings are available.
|
|
108
|
+
*
|
|
109
|
+
* @param conversation - The messages to extract facts from.
|
|
110
|
+
* @returns The extraction result (candidate facts, stored ids, dup count).
|
|
111
|
+
*/
|
|
112
|
+
async addMessages(conversation) {
|
|
113
|
+
const candidates = conversation.filter((m) => m.content.trim().length >= this.opts.minMessageLength);
|
|
114
|
+
if (candidates.length === 0) {
|
|
115
|
+
return { facts: [], storedIds: [], duplicates: 0 };
|
|
116
|
+
}
|
|
117
|
+
return this.memos.extractFacts(candidates, {
|
|
118
|
+
autoStore: true,
|
|
119
|
+
minConfidence: this.opts.minConfidence,
|
|
120
|
+
namespace: this.opts.namespace,
|
|
121
|
+
dedupe: true,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Search memories and format the results for Anthropic tool use.
|
|
126
|
+
*
|
|
127
|
+
* @param query - Natural-language search query.
|
|
128
|
+
* @param opts - Search options (limit, type, namespace).
|
|
129
|
+
* @returns An object containing the ranked memory entries, a JSON string,
|
|
130
|
+
* and a `toToolResult(toolUseId)` factory that builds a `tool_result`
|
|
131
|
+
* content block ready to send back as a `user` message.
|
|
132
|
+
*/
|
|
133
|
+
async searchMemories(query, opts = {}) {
|
|
134
|
+
const filter = {
|
|
135
|
+
query,
|
|
136
|
+
limit: opts.limit ?? this.opts.maxContextMemories,
|
|
137
|
+
};
|
|
138
|
+
if (opts.type)
|
|
139
|
+
filter.type = opts.type;
|
|
140
|
+
if (opts.namespace)
|
|
141
|
+
filter.namespace = opts.namespace;
|
|
142
|
+
const results = await this.memos.search(filter);
|
|
143
|
+
const memories = results.map((r) => this.toEntry(r));
|
|
144
|
+
const content = memories.length > 0
|
|
145
|
+
? JSON.stringify(memories, null, 2)
|
|
146
|
+
: "No relevant memories found.";
|
|
147
|
+
return {
|
|
148
|
+
query,
|
|
149
|
+
memories,
|
|
150
|
+
content,
|
|
151
|
+
toToolResult: (toolUseId) => ({
|
|
152
|
+
type: "tool_result",
|
|
153
|
+
tool_use_id: toolUseId,
|
|
154
|
+
content,
|
|
155
|
+
}),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Build a system-prompt string of relevant memories for injection.
|
|
160
|
+
*
|
|
161
|
+
* Optionally extracts & stores facts from the recent `messages` first,
|
|
162
|
+
* then searches MemOS using the most recent user message and returns the
|
|
163
|
+
* matches as a formatted block ready to pass as the top-level `system`
|
|
164
|
+
* parameter of `client.messages.create`.
|
|
165
|
+
*
|
|
166
|
+
* @param messages - Recent conversation messages.
|
|
167
|
+
* @param opts - Context options (limit, whether to extract).
|
|
168
|
+
* @returns A memory-context string, or `""` if nothing relevant was found.
|
|
169
|
+
*/
|
|
170
|
+
async getMemoryContext(messages, opts = {}) {
|
|
171
|
+
const shouldExtract = opts.extract ?? this.opts.autoExtractFacts;
|
|
172
|
+
if (shouldExtract && messages.length > 0) {
|
|
173
|
+
await this.addMessages(messages);
|
|
174
|
+
}
|
|
175
|
+
const lastUser = [...messages].reverse().find((m) => m.role === "user");
|
|
176
|
+
const query = lastUser?.content.trim() ?? "";
|
|
177
|
+
if (!query)
|
|
178
|
+
return "";
|
|
179
|
+
const results = await this.memos.search({
|
|
180
|
+
query,
|
|
181
|
+
limit: opts.limit ?? this.opts.maxContextMemories,
|
|
182
|
+
});
|
|
183
|
+
if (results.length === 0)
|
|
184
|
+
return "";
|
|
185
|
+
const lines = ["Relevant memories from prior conversations:"];
|
|
186
|
+
results.forEach((m, i) => {
|
|
187
|
+
lines.push(` ${i + 1}. [${m.node.type}] ${m.node.content}`);
|
|
188
|
+
});
|
|
189
|
+
return lines.join("\n");
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Manually store a memory.
|
|
193
|
+
*
|
|
194
|
+
* @param content - Text to remember.
|
|
195
|
+
* @param opts - Additional memory parameters (type, tags, importance…).
|
|
196
|
+
* @returns The created memory node.
|
|
197
|
+
*/
|
|
198
|
+
async remember(content, opts = {}) {
|
|
199
|
+
const { node } = await this.memos.store(content, {
|
|
200
|
+
...opts,
|
|
201
|
+
source: opts.source ?? this.opts.source,
|
|
202
|
+
namespace: opts.namespace ?? this.opts.namespace,
|
|
203
|
+
});
|
|
204
|
+
return node;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Search for relevant memories.
|
|
208
|
+
*
|
|
209
|
+
* @param query - Search query.
|
|
210
|
+
* @param limit - Maximum number of results. @default 5
|
|
211
|
+
* @returns Scored memory nodes.
|
|
212
|
+
*/
|
|
213
|
+
async recall(query, limit = 5) {
|
|
214
|
+
return this.memos.search({ query, limit });
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Permanently forget a memory.
|
|
218
|
+
*
|
|
219
|
+
* @param memoryId - ID of the memory to delete.
|
|
220
|
+
* @returns `true` if the memory existed and was deleted.
|
|
221
|
+
*/
|
|
222
|
+
async forget(memoryId) {
|
|
223
|
+
return this.memos.forget(memoryId);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Remove all memories and edges from the underlying MemOS instance.
|
|
227
|
+
*
|
|
228
|
+
* This is destructive and cannot be undone.
|
|
229
|
+
*/
|
|
230
|
+
async clearMemories() {
|
|
231
|
+
await this.memos.clear();
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Return the total number of stored memories.
|
|
235
|
+
*
|
|
236
|
+
* @returns The current memory count.
|
|
237
|
+
*/
|
|
238
|
+
getMemoryCount() {
|
|
239
|
+
return this.memos.count;
|
|
240
|
+
}
|
|
241
|
+
/** Convert a scored memory into a JSON-friendly entry. */
|
|
242
|
+
toEntry(scored) {
|
|
243
|
+
return {
|
|
244
|
+
id: scored.node.id,
|
|
245
|
+
content: scored.node.content,
|
|
246
|
+
summary: scored.node.summary,
|
|
247
|
+
type: scored.node.type,
|
|
248
|
+
score: scored.score,
|
|
249
|
+
tags: scored.node.tags,
|
|
250
|
+
trustScore: scored.node.trustScore,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// ---------------------------------------------------------------------------
|
|
255
|
+
// Convenience constructor
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
/**
|
|
258
|
+
* Create a MemOS instance and an Anthropic memory plugin in one call.
|
|
259
|
+
*
|
|
260
|
+
* The MemOS instance is created with `config`, initialised, and wrapped in a
|
|
261
|
+
* {@link MemOSAnthropicPlugin}. Plugin-level options go under `config.plugin`.
|
|
262
|
+
*
|
|
263
|
+
* @param config - MemOS config plus optional `plugin` options.
|
|
264
|
+
* @returns The initialised `memos` instance and bound `plugin`.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* const { memos, plugin } = await createAnthropicMemory({
|
|
269
|
+
* dbPath: "./my-app.db",
|
|
270
|
+
* plugin: { maxContextMemories: 8 },
|
|
271
|
+
* });
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
export async function createAnthropicMemory(config = {}) {
|
|
275
|
+
const { plugin: pluginOpts, ...memosConfig } = config;
|
|
276
|
+
const memos = new MemOS(memosConfig);
|
|
277
|
+
await memos.init();
|
|
278
|
+
const plugin = new MemOSAnthropicPlugin(memos, pluginOpts);
|
|
279
|
+
return { memos, plugin };
|
|
280
|
+
}
|
|
281
|
+
// ---------------------------------------------------------------------------
|
|
282
|
+
// Usage example
|
|
283
|
+
// ---------------------------------------------------------------------------
|
|
284
|
+
/*
|
|
285
|
+
* End-to-end example using the official `@anthropic-ai/sdk`.
|
|
286
|
+
*
|
|
287
|
+
* npm install @anthropic-ai/sdk
|
|
288
|
+
*
|
|
289
|
+
* import Anthropic from "@anthropic-ai/sdk";
|
|
290
|
+
* import { createAnthropicMemory } from "@mem-os/sdk/anthropic";
|
|
291
|
+
*
|
|
292
|
+
* // 1. Boot MemOS + the plugin.
|
|
293
|
+
* const { memos, plugin } = await createAnthropicMemory({
|
|
294
|
+
* dbPath: "./my-app.db",
|
|
295
|
+
* plugin: { maxContextMemories: 6 },
|
|
296
|
+
* });
|
|
297
|
+
*
|
|
298
|
+
* const client = new Anthropic();
|
|
299
|
+
*
|
|
300
|
+
* // 2. Teach it something. addMessages() extracts & stores facts.
|
|
301
|
+
* await plugin.addMessages([
|
|
302
|
+
* { role: "user", content: "I prefer dark mode in all my apps." },
|
|
303
|
+
* { role: "assistant", content: "Got it — dark mode it is." },
|
|
304
|
+
* ]);
|
|
305
|
+
*
|
|
306
|
+
* // 3a. Context-injection style: fold memories into the system prompt.
|
|
307
|
+
* // Anthropic takes `system` as a top-level param, not a message role.
|
|
308
|
+
* const systemCtx = await plugin.getMemoryContext([
|
|
309
|
+
* { role: "user", content: "What theme do I like?" },
|
|
310
|
+
* ]);
|
|
311
|
+
* const a = await client.messages.create({
|
|
312
|
+
* model: "claude-3-5-sonnet-20241022",
|
|
313
|
+
* max_tokens: 1024,
|
|
314
|
+
* system: `You are a helpful assistant.\n\n${systemCtx}`,
|
|
315
|
+
* messages: [{ role: "user", content: "What theme do I like?" }],
|
|
316
|
+
* });
|
|
317
|
+
* console.log(a.content[0]); // → "dark mode"
|
|
318
|
+
*
|
|
319
|
+
* // 3b. Tool style: let the model call search_memories itself.
|
|
320
|
+
* const tools = [plugin.memoryTool];
|
|
321
|
+
* const res = await client.messages.create({
|
|
322
|
+
* model: "claude-3-5-sonnet-20241022",
|
|
323
|
+
* max_tokens: 1024,
|
|
324
|
+
* messages: [{ role: "user", content: "What theme do I like?" }],
|
|
325
|
+
* tools,
|
|
326
|
+
* });
|
|
327
|
+
* const toolUse = res.content.find((b) => b.type === "tool_use");
|
|
328
|
+
* if (toolUse && toolUse.name === "search_memories") {
|
|
329
|
+
* const { query } = toolUse.input as { query: string };
|
|
330
|
+
* const { toToolResult } = await plugin.searchMemories(query);
|
|
331
|
+
* const followUp = await client.messages.create({
|
|
332
|
+
* model: "claude-3-5-sonnet-20241022",
|
|
333
|
+
* max_tokens: 1024,
|
|
334
|
+
* messages: [
|
|
335
|
+
* { role: "user", content: "What theme do I like?" },
|
|
336
|
+
* { role: "assistant", content: res.content },
|
|
337
|
+
* { role: "user", content: [toToolResult(toolUse.id)] },
|
|
338
|
+
* ],
|
|
339
|
+
* tools,
|
|
340
|
+
* });
|
|
341
|
+
* console.log(followUp.content[0]); // → "dark mode"
|
|
342
|
+
* }
|
|
343
|
+
*
|
|
344
|
+
* // 4. Housekeeping helpers.
|
|
345
|
+
* console.log(plugin.getMemoryCount()); // number of stored memories
|
|
346
|
+
* await plugin.clearMemories(); // wipe everything
|
|
347
|
+
*/
|
|
348
|
+
//# sourceMappingURL=anthropic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/adapters/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AA2JpC,8EAA8E;AAC9E,SAAS;AACT,8EAA8E;AAE9E;;;;;;;;GAQG;AACH,MAAM,OAAO,oBAAoB;IAC/B,kCAAkC;IACzB,KAAK,CAAQ;IAEL,IAAI,CAOnB;IAEF;;;;;OAKG;IACH,YAAY,KAAY,EAAE,IAAI,GAAgC,EAAE;QAC9D,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG;YACV,kBAAkB,EAAE,IAAI,CAAC,kBAAkB,IAAI,CAAC;YAChD,gBAAgB,EAAE,IAAI,CAAC,gBAAgB,IAAI,IAAI;YAC/C,gBAAgB,EAAE,IAAI,CAAC,gBAAgB,IAAI,EAAE;YAC7C,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,YAAY;YACnC,aAAa,EAAE,IAAI,CAAC,aAAa,IAAI,GAAG;YACxC,SAAS,EAAE,IAAI,CAAC,SAAS,IAAI,SAAS;SACvC,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACH,IAAI,UAAU;QACZ,OAAO;YACL,IAAI,EAAE,iBAAiB;YACvB,WAAW,EACT,gEAAgE;gBAChE,mEAAmE;gBACnE,6DAA6D;YAC/D,YAAY,EAAE;gBACZ,IAAI,E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G"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage adapter base interface.
|
|
3
|
+
*
|
|
4
|
+
* Re-exports the {@link StorageAdapter} contract from `types.ts` so that
|
|
5
|
+
* backend implementations can import from `@memos/adapters/base`.
|
|
6
|
+
*
|
|
7
|
+
* @module @memos/adapters/base
|
|
8
|
+
*/
|
|
9
|
+
export type { StorageAdapter } from "../types.js";
|
|
10
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/adapters/base.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/adapters/base.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MemOS OpenAI Adapter — first-class memory integration for the OpenAI SDK.
|
|
3
|
+
*
|
|
4
|
+
* Wraps a MemOS instance and exposes helpers that map cleanly onto the
|
|
5
|
+
* OpenAI Chat Completions API: a memory-backed function tool, system-prompt
|
|
6
|
+
* context injection, automatic fact extraction from conversations, and
|
|
7
|
+
* simple store/forget helpers.
|
|
8
|
+
*
|
|
9
|
+
* The adapter has **zero hard dependencies** on the `openai` package. Every
|
|
10
|
+
* exported message/tool shape is structurally compatible with the
|
|
11
|
+
* corresponding `openai` types (`ChatCompletionMessageParam`,
|
|
12
|
+
* `ChatCompletionTool`, `ChatCompletionToolMessageParam`), so they can be
|
|
13
|
+
* passed straight into `openai.chat.completions.create(...)` with no glue
|
|
14
|
+
* layer. Install `openai` only if you want to call the model; the adapter
|
|
15
|
+
* itself only needs MemOS.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import OpenAI from "openai";
|
|
20
|
+
* import { createOpenAIMemory } from "@mem-os/sdk/openai";
|
|
21
|
+
*
|
|
22
|
+
* const { plugin } = await createOpenAIMemory({ dbPath: "./my-app.db" });
|
|
23
|
+
* const openai = new OpenAI();
|
|
24
|
+
*
|
|
25
|
+
* const completion = await openai.chat.completions.create({
|
|
26
|
+
* model: "gpt-4o",
|
|
27
|
+
* messages: [{ role: "user", content: "What theme do I like?" }],
|
|
28
|
+
* tools: [plugin.memoryTool],
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @module @memos/adapters/openai
|
|
33
|
+
*/
|
|
34
|
+
import { MemOS } from "../index.js";
|
|
35
|
+
import type { ConversationMessage, CreateMemoryInput, ExtractFactsResult, MemoryNode, MemorySource, MemoryType, MemOSConfig, ScoredMemory } from "../index.js";
|
|
36
|
+
/** Chat message role, compatible with the OpenAI Chat Completions API. */
|
|
37
|
+
export type OpenAIRole = "system" | "user" | "assistant" | "tool";
|
|
38
|
+
/**
|
|
39
|
+
* Minimal OpenAI-compatible chat message.
|
|
40
|
+
*
|
|
41
|
+
* Structurally compatible with `openai.ChatCompletionMessageParam` for the
|
|
42
|
+
* plain-text cases this adapter produces and consumes.
|
|
43
|
+
*/
|
|
44
|
+
export interface OpenAIChatMessage {
|
|
45
|
+
role: OpenAIRole;
|
|
46
|
+
content: string;
|
|
47
|
+
name?: string;
|
|
48
|
+
tool_call_id?: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A `tool`-role message returned to the model after a tool call.
|
|
52
|
+
* Compatible with `openai.ChatCompletionToolMessageParam`.
|
|
53
|
+
*
|
|
54
|
+
* Note: `tool_call_id` is left optional here; set it to the id of the
|
|
55
|
+
* `tool_call` you are responding to before sending.
|
|
56
|
+
*/
|
|
57
|
+
export interface OpenAIToolMessage {
|
|
58
|
+
role: "tool";
|
|
59
|
+
content: string;
|
|
60
|
+
tool_call_id?: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A function-tool definition registrable via the `tools` parameter of
|
|
64
|
+
* `openai.chat.completions.create`. Compatible with
|
|
65
|
+
* `openai.ChatCompletionTool`.
|
|
66
|
+
*/
|
|
67
|
+
export interface OpenAIToolDefinition {
|
|
68
|
+
type: "function";
|
|
69
|
+
function: {
|
|
70
|
+
name: string;
|
|
71
|
+
description: string;
|
|
72
|
+
/** A JSON Schema object describing the function parameters. */
|
|
73
|
+
parameters: Record<string, unknown>;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A single memory rendered as a plain JSON object, suitable for returning
|
|
78
|
+
* as a tool result.
|
|
79
|
+
*/
|
|
80
|
+
export interface OpenAIMemoryEntry {
|
|
81
|
+
id: string;
|
|
82
|
+
content: string;
|
|
83
|
+
summary: string;
|
|
84
|
+
type: MemoryType;
|
|
85
|
+
/** Relevance score from the search. */
|
|
86
|
+
score: number;
|
|
87
|
+
tags: string[];
|
|
88
|
+
trustScore: number;
|
|
89
|
+
}
|
|
90
|
+
/** Options for {@link MemOSOpenAIPlugin.searchMemories}. */
|
|
91
|
+
export interface OpenAISearchMemoriesOptions {
|
|
92
|
+
/** Maximum number of memories to return. @default 5 */
|
|
93
|
+
limit?: number;
|
|
94
|
+
/** Restrict results to a memory type. */
|
|
95
|
+
type?: MemoryType;
|
|
96
|
+
/** Restrict results to a namespace. */
|
|
97
|
+
namespace?: string;
|
|
98
|
+
}
|
|
99
|
+
/** Options for {@link MemOSOpenAIPlugin.getMemoryContext}. */
|
|
100
|
+
export interface OpenAIGetMemoryContextOptions {
|
|
101
|
+
/** Maximum memories to inject. @default 5 */
|
|
102
|
+
limit?: number;
|
|
103
|
+
/** Whether to extract & store facts from `messages` first. @default true */
|
|
104
|
+
extract?: boolean;
|
|
105
|
+
}
|
|
106
|
+
/** Result returned by {@link MemOSOpenAIPlugin.searchMemories}. */
|
|
107
|
+
export interface OpenAIMemorySearchResult {
|
|
108
|
+
/** The query that was searched. */
|
|
109
|
+
query: string;
|
|
110
|
+
/** Ranked memory entries. */
|
|
111
|
+
memories: OpenAIMemoryEntry[];
|
|
112
|
+
/** A ready-to-send `tool` message containing the JSON-encoded memories. */
|
|
113
|
+
toolMessage: OpenAIToolMessage;
|
|
114
|
+
}
|
|
115
|
+
/** Options for {@link MemOSOpenAIPlugin}. */
|
|
116
|
+
export interface MemOSOpenAIPluginOptions {
|
|
117
|
+
/** Maximum memories to inject into context / return from a search. @default 5 */
|
|
118
|
+
maxContextMemories?: number;
|
|
119
|
+
/**
|
|
120
|
+
* Automatically extract & store facts when `addMessages` or
|
|
121
|
+
* `getMemoryContext` are called. @default true
|
|
122
|
+
*/
|
|
123
|
+
autoExtractFacts?: boolean;
|
|
124
|
+
/** Minimum message length (chars) to consider for fact extraction. @default 15 */
|
|
125
|
+
minMessageLength?: number;
|
|
126
|
+
/** Provenance source tag for memories created by this adapter. @default "user_input" */
|
|
127
|
+
source?: MemorySource;
|
|
128
|
+
/** Minimum confidence for an extracted fact to be stored. @default 0.6 */
|
|
129
|
+
minConfidence?: number;
|
|
130
|
+
/** Namespace to store/search memories in (experimental). @default "default" */
|
|
131
|
+
namespace?: string;
|
|
132
|
+
}
|
|
133
|
+
/** Configuration for {@link createOpenAIMemory}. */
|
|
134
|
+
export interface OpenAIMemoryConfig extends MemOSConfig {
|
|
135
|
+
/** Plugin-level options. */
|
|
136
|
+
plugin?: MemOSOpenAIPluginOptions;
|
|
137
|
+
}
|
|
138
|
+
/** Result of {@link createOpenAIMemory}. */
|
|
139
|
+
export interface CreateOpenAIMemoryResult {
|
|
140
|
+
/** The initialised MemOS instance. */
|
|
141
|
+
memos: MemOS;
|
|
142
|
+
/** The OpenAI memory plugin bound to that instance. */
|
|
143
|
+
plugin: MemOSOpenAIPlugin;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* OpenAI SDK memory plugin backed by MemOS.
|
|
147
|
+
*
|
|
148
|
+
* Wrap an existing MemOS instance, or use {@link createOpenAIMemory} to
|
|
149
|
+
* construct both in one call. The plugin does not call the OpenAI API
|
|
150
|
+
* itself — it prepares memories (as tools, tool messages, and system-prompt
|
|
151
|
+
* strings) for you to feed into your own `openai.chat.completions` calls.
|
|
152
|
+
*/
|
|
153
|
+
export declare class MemOSOpenAIPlugin {
|
|
154
|
+
/** The wrapped MemOS instance. */
|
|
155
|
+
readonly memos: MemOS;
|
|
156
|
+
private readonly opts;
|
|
157
|
+
/**
|
|
158
|
+
* Create a new OpenAI memory plugin.
|
|
159
|
+
*
|
|
160
|
+
* @param memos - An initialised MemOS instance.
|
|
161
|
+
* @param opts - Plugin options. All fields optional with sensible defaults.
|
|
162
|
+
*/
|
|
163
|
+
constructor(memos: MemOS, opts?: MemOSOpenAIPluginOptions);
|
|
164
|
+
/**
|
|
165
|
+
* OpenAI function-tool definition that lets the model search the user's
|
|
166
|
+
* memory. Pass it via the `tools` parameter of
|
|
167
|
+
* `openai.chat.completions.create`.
|
|
168
|
+
*
|
|
169
|
+
* When the model invokes it, call {@link searchMemories} and return the
|
|
170
|
+
* `toolMessage` field (after setting `tool_call_id`) as a tool message.
|
|
171
|
+
*/
|
|
172
|
+
get memoryTool(): OpenAIToolDefinition;
|
|
173
|
+
/**
|
|
174
|
+
* Extract facts from a conversation and store them in MemOS.
|
|
175
|
+
*
|
|
176
|
+
* Runs MemOS' local rule-based fact extractor over the conversation,
|
|
177
|
+
* persisting any candidate facts whose confidence meets the configured
|
|
178
|
+
* `minConfidence` threshold. Near-duplicates are skipped automatically
|
|
179
|
+
* when embeddings are available.
|
|
180
|
+
*
|
|
181
|
+
* @param conversation - The messages to extract facts from.
|
|
182
|
+
* @returns The extraction result (candidate facts, stored ids, dup count).
|
|
183
|
+
*/
|
|
184
|
+
addMessages(conversation: ConversationMessage[]): Promise<ExtractFactsResult>;
|
|
185
|
+
/**
|
|
186
|
+
* Search memories and format the results for OpenAI tool use.
|
|
187
|
+
*
|
|
188
|
+
* @param query - Natural-language search query.
|
|
189
|
+
* @param opts - Search options (limit, type, namespace).
|
|
190
|
+
* @returns An object containing the ranked memory entries and a
|
|
191
|
+
* ready-to-send `tool` message (JSON-encoded memories). Set
|
|
192
|
+
* `toolMessage.tool_call_id` to the model's `tool_call` id before
|
|
193
|
+
* returning it.
|
|
194
|
+
*/
|
|
195
|
+
searchMemories(query: string, opts?: OpenAISearchMemoriesOptions): Promise<OpenAIMemorySearchResult>;
|
|
196
|
+
/**
|
|
197
|
+
* Build a system-prompt string of relevant memories for injection.
|
|
198
|
+
*
|
|
199
|
+
* Optionally extracts & stores facts from the recent `messages` first,
|
|
200
|
+
* then searches MemOS using the most recent user message and returns the
|
|
201
|
+
* matches as a formatted block ready to prepend to the `system` prompt.
|
|
202
|
+
*
|
|
203
|
+
* @param messages - Recent conversation messages.
|
|
204
|
+
* @param opts - Context options (limit, whether to extract).
|
|
205
|
+
* @returns A memory-context string, or `""` if nothing relevant was found.
|
|
206
|
+
*/
|
|
207
|
+
getMemoryContext(messages: ConversationMessage[], opts?: OpenAIGetMemoryContextOptions): Promise<string>;
|
|
208
|
+
/**
|
|
209
|
+
* Manually store a memory.
|
|
210
|
+
*
|
|
211
|
+
* @param content - Text to remember.
|
|
212
|
+
* @param opts - Additional memory parameters (type, tags, importance…).
|
|
213
|
+
* @returns The created memory node.
|
|
214
|
+
*/
|
|
215
|
+
remember(content: string, opts?: Omit<CreateMemoryInput, "content">): Promise<MemoryNode>;
|
|
216
|
+
/**
|
|
217
|
+
* Search for relevant memories.
|
|
218
|
+
*
|
|
219
|
+
* @param query - Search query.
|
|
220
|
+
* @param limit - Maximum number of results. @default 5
|
|
221
|
+
* @returns Scored memory nodes.
|
|
222
|
+
*/
|
|
223
|
+
recall(query: string, limit?: number): Promise<ScoredMemory[]>;
|
|
224
|
+
/**
|
|
225
|
+
* Permanently forget a memory.
|
|
226
|
+
*
|
|
227
|
+
* @param memoryId - ID of the memory to delete.
|
|
228
|
+
* @returns `true` if the memory existed and was deleted.
|
|
229
|
+
*/
|
|
230
|
+
forget(memoryId: string): Promise<boolean>;
|
|
231
|
+
/**
|
|
232
|
+
* Remove all memories and edges from the underlying MemOS instance.
|
|
233
|
+
*
|
|
234
|
+
* This is destructive and cannot be undone.
|
|
235
|
+
*/
|
|
236
|
+
clearMemories(): Promise<void>;
|
|
237
|
+
/**
|
|
238
|
+
* Return the total number of stored memories.
|
|
239
|
+
*
|
|
240
|
+
* @returns The current memory count.
|
|
241
|
+
*/
|
|
242
|
+
getMemoryCount(): number;
|
|
243
|
+
/** Convert a scored memory into a JSON-friendly entry. */
|
|
244
|
+
private toEntry;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Create a MemOS instance and an OpenAI memory plugin in one call.
|
|
248
|
+
*
|
|
249
|
+
* The MemOS instance is created with `config`, initialised, and wrapped in a
|
|
250
|
+
* {@link MemOSOpenAIPlugin}. Plugin-level options go under `config.plugin`.
|
|
251
|
+
*
|
|
252
|
+
* @param config - MemOS config plus optional `plugin` options.
|
|
253
|
+
* @returns The initialised `memos` instance and bound `plugin`.
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```ts
|
|
257
|
+
* const { memos, plugin } = await createOpenAIMemory({
|
|
258
|
+
* dbPath: "./my-app.db",
|
|
259
|
+
* plugin: { maxContextMemories: 8 },
|
|
260
|
+
* });
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
export declare function createOpenAIMemory(config?: OpenAIMemoryConfig): Promise<CreateOpenAIMemoryResult>;
|
|
264
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/adapters/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,KAAK,EACV,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,UAAU,EACV,YAAY,EACZ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