@smithers-orchestrator/memory 0.16.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.
- package/LICENSE +21 -0
- package/package.json +46 -0
- package/src/MemoryFact.ts +9 -0
- package/src/MemoryLayerConfig.ts +5 -0
- package/src/MemoryMessage.ts +9 -0
- package/src/MemoryNamespace.ts +6 -0
- package/src/MemoryNamespaceKind.ts +1 -0
- package/src/MemoryProcessor.js +5 -0
- package/src/MemoryProcessor.ts +9 -0
- package/src/MemoryProcessorConfig.ts +3 -0
- package/src/MemoryService.js +10 -0
- package/src/MemoryServiceApi.js +5 -0
- package/src/MemoryServiceApi.ts +49 -0
- package/src/MemoryThread.ts +8 -0
- package/src/MessageHistoryConfig.ts +4 -0
- package/src/SemanticRecallConfig.ts +7 -0
- package/src/Summarizer.js +26 -0
- package/src/TaskMemoryConfig.ts +15 -0
- package/src/TokenLimiter.js +29 -0
- package/src/TtlGarbageCollector.js +23 -0
- package/src/WorkingMemoryConfig.ts +10 -0
- package/src/createMemoryLayer.js +30 -0
- package/src/index.d.ts +616 -0
- package/src/index.js +39 -0
- package/src/memoryFactReads.js +2 -0
- package/src/memoryFactWrites.js +2 -0
- package/src/memoryMessageSaves.js +2 -0
- package/src/memoryRecallDuration.js +7 -0
- package/src/memoryRecallQueries.js +2 -0
- package/src/metrics.js +5 -0
- package/src/namespaceToString.js +9 -0
- package/src/parseNamespace.js +18 -0
- package/src/processors.js +7 -0
- package/src/react-types.ts +1 -0
- package/src/schema.js +38 -0
- package/src/service.js +7 -0
- package/src/store/MemoryStore.js +5 -0
- package/src/store/MemoryStore.ts +68 -0
- package/src/store/MemoryStoreDb.js +6 -0
- package/src/store/MemoryStoreLive.js +306 -0
- package/src/store/MemoryStoreService.js +10 -0
- package/src/store/createMemoryStore.js +13 -0
- package/src/store/createMemoryStoreLayer.js +13 -0
- package/src/store/index.js +9 -0
- package/src/types.js +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 William Cory
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@smithers-orchestrator/memory",
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"description": "Persistent and semantic memory services for Smithers",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./src/index.d.ts",
|
|
10
|
+
"import": "./src/index.js",
|
|
11
|
+
"default": "./src/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./store": {
|
|
14
|
+
"types": "./src/index.d.ts",
|
|
15
|
+
"import": "./src/store/index.js",
|
|
16
|
+
"default": "./src/store/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./*": {
|
|
19
|
+
"types": "./src/index.d.ts",
|
|
20
|
+
"import": "./src/*.js",
|
|
21
|
+
"default": "./src/*.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"src/"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"drizzle-orm": "^0.45.1",
|
|
29
|
+
"effect": "^3.21.0",
|
|
30
|
+
"zod": "^4.3.6",
|
|
31
|
+
"@smithers-orchestrator/db": "0.16.0",
|
|
32
|
+
"@smithers-orchestrator/driver": "0.16.0",
|
|
33
|
+
"@smithers-orchestrator/errors": "0.16.0",
|
|
34
|
+
"@smithers-orchestrator/observability": "0.16.0",
|
|
35
|
+
"@smithers-orchestrator/scheduler": "0.16.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/bun": "latest",
|
|
39
|
+
"typescript": "~5.9.3"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup --dts-only",
|
|
43
|
+
"test": "bun test tests",
|
|
44
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type MemoryNamespaceKind = "workflow" | "agent" | "user" | "global";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import type { SmithersError } from "@smithers-orchestrator/errors";
|
|
3
|
+
import type { MemoryStore } from "./store/MemoryStore";
|
|
4
|
+
|
|
5
|
+
export type MemoryProcessor = {
|
|
6
|
+
name: string;
|
|
7
|
+
process: (store: MemoryStore) => Promise<void>;
|
|
8
|
+
processEffect: (store: MemoryStore) => Effect.Effect<void, SmithersError>;
|
|
9
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Context } from "effect";
|
|
2
|
+
/** @typedef {import("./MemoryServiceApi.ts").MemoryServiceApi} MemoryServiceApi */
|
|
3
|
+
|
|
4
|
+
const MemoryServiceBase =
|
|
5
|
+
/** @type {Context.TagClass<MemoryService, "MemoryService", MemoryServiceApi>} */ (
|
|
6
|
+
/** @type {unknown} */ (Context.Tag("MemoryService")())
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
export class MemoryService extends MemoryServiceBase {
|
|
10
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import type { SmithersError } from "@smithers-orchestrator/errors";
|
|
3
|
+
import type { MemoryNamespace } from "./MemoryNamespace";
|
|
4
|
+
import type { MemoryFact } from "./MemoryFact";
|
|
5
|
+
import type { MemoryThread } from "./MemoryThread";
|
|
6
|
+
import type { MemoryMessage } from "./MemoryMessage";
|
|
7
|
+
import type { MemoryStore } from "./store/MemoryStore";
|
|
8
|
+
|
|
9
|
+
export type MemoryServiceApi = {
|
|
10
|
+
readonly getFact: (
|
|
11
|
+
ns: MemoryNamespace,
|
|
12
|
+
key: string,
|
|
13
|
+
) => Effect.Effect<MemoryFact | undefined, SmithersError>;
|
|
14
|
+
readonly setFact: (
|
|
15
|
+
ns: MemoryNamespace,
|
|
16
|
+
key: string,
|
|
17
|
+
value: unknown,
|
|
18
|
+
ttlMs?: number,
|
|
19
|
+
) => Effect.Effect<void, SmithersError>;
|
|
20
|
+
readonly deleteFact: (
|
|
21
|
+
ns: MemoryNamespace,
|
|
22
|
+
key: string,
|
|
23
|
+
) => Effect.Effect<void, SmithersError>;
|
|
24
|
+
readonly listFacts: (
|
|
25
|
+
ns: MemoryNamespace,
|
|
26
|
+
) => Effect.Effect<MemoryFact[], SmithersError>;
|
|
27
|
+
readonly createThread: (
|
|
28
|
+
ns: MemoryNamespace,
|
|
29
|
+
title?: string,
|
|
30
|
+
) => Effect.Effect<MemoryThread, SmithersError>;
|
|
31
|
+
readonly getThread: (
|
|
32
|
+
threadId: string,
|
|
33
|
+
) => Effect.Effect<MemoryThread | undefined, SmithersError>;
|
|
34
|
+
readonly deleteThread: (
|
|
35
|
+
threadId: string,
|
|
36
|
+
) => Effect.Effect<void, SmithersError>;
|
|
37
|
+
readonly saveMessage: (
|
|
38
|
+
msg: Omit<MemoryMessage, "createdAtMs"> & { createdAtMs?: number },
|
|
39
|
+
) => Effect.Effect<void, SmithersError>;
|
|
40
|
+
readonly listMessages: (
|
|
41
|
+
threadId: string,
|
|
42
|
+
limit?: number,
|
|
43
|
+
) => Effect.Effect<MemoryMessage[], SmithersError>;
|
|
44
|
+
readonly countMessages: (
|
|
45
|
+
threadId: string,
|
|
46
|
+
) => Effect.Effect<number, SmithersError>;
|
|
47
|
+
readonly deleteExpiredFacts: () => Effect.Effect<number, SmithersError>;
|
|
48
|
+
readonly store: MemoryStore;
|
|
49
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
/** @typedef {import("./MemoryProcessor.ts").MemoryProcessor} MemoryProcessor */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {{ run: (prompt: string) => Promise<any> }} agent
|
|
6
|
+
* @returns {MemoryProcessor}
|
|
7
|
+
*/
|
|
8
|
+
export function Summarizer(agent) {
|
|
9
|
+
/**
|
|
10
|
+
* @param {MemoryStore} store
|
|
11
|
+
* @returns {Effect.Effect<void, SmithersError>}
|
|
12
|
+
*/
|
|
13
|
+
function processEffect(store) {
|
|
14
|
+
return Effect.gen(function* () {
|
|
15
|
+
// Summarizer operates on a specific thread's messages, compressing
|
|
16
|
+
// older messages into a summary. Without a thread context, it logs
|
|
17
|
+
// and returns. This is a structural placeholder.
|
|
18
|
+
yield* Effect.logInfo("Summarizer: configured — operates at thread level");
|
|
19
|
+
}).pipe(Effect.annotateLogs({ processor: "Summarizer" }), Effect.withLogSpan("memory:processor:summarizer"));
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
name: "Summarizer",
|
|
23
|
+
process: (store) => Effect.runPromise(processEffect(store)),
|
|
24
|
+
processEffect,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { MemoryNamespace } from "./MemoryNamespace";
|
|
2
|
+
|
|
3
|
+
export type TaskMemoryConfig = {
|
|
4
|
+
namespace?: string | MemoryNamespace;
|
|
5
|
+
recall?: {
|
|
6
|
+
namespace?: MemoryNamespace;
|
|
7
|
+
query?: string;
|
|
8
|
+
topK?: number;
|
|
9
|
+
};
|
|
10
|
+
remember?: {
|
|
11
|
+
namespace?: MemoryNamespace;
|
|
12
|
+
key?: string;
|
|
13
|
+
};
|
|
14
|
+
threadId?: string;
|
|
15
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
/** @typedef {import("./MemoryProcessor.ts").MemoryProcessor} MemoryProcessor */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {number} maxTokens
|
|
6
|
+
* @returns {MemoryProcessor}
|
|
7
|
+
*/
|
|
8
|
+
export function TokenLimiter(maxTokens) {
|
|
9
|
+
// Rough approximation: 1 token ~= 4 characters
|
|
10
|
+
const charBudget = maxTokens * 4;
|
|
11
|
+
/**
|
|
12
|
+
* @param {MemoryStore} store
|
|
13
|
+
* @returns {Effect.Effect<void, SmithersError>}
|
|
14
|
+
*/
|
|
15
|
+
function processEffect(store) {
|
|
16
|
+
return Effect.gen(function* () {
|
|
17
|
+
// Token limiter operates at the thread level; without a specific thread
|
|
18
|
+
// context it logs and returns. In practice, this processor is invoked
|
|
19
|
+
// with a store that wraps a specific thread. For now, this is a no-op
|
|
20
|
+
// placeholder that documents the intended behaviour.
|
|
21
|
+
yield* Effect.logInfo(`TokenLimiter: configured for ${maxTokens} tokens (${charBudget} chars) — operates at thread level`);
|
|
22
|
+
}).pipe(Effect.annotateLogs({ processor: "TokenLimiter", maxTokens }), Effect.withLogSpan("memory:processor:token-limiter"));
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
name: "TokenLimiter",
|
|
26
|
+
process: (store) => Effect.runPromise(processEffect(store)),
|
|
27
|
+
processEffect,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
/** @typedef {import("./MemoryProcessor.ts").MemoryProcessor} MemoryProcessor */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @returns {MemoryProcessor}
|
|
6
|
+
*/
|
|
7
|
+
export function TtlGarbageCollector() {
|
|
8
|
+
/**
|
|
9
|
+
* @param {MemoryStore} store
|
|
10
|
+
* @returns {Effect.Effect<void, SmithersError>}
|
|
11
|
+
*/
|
|
12
|
+
function processEffect(store) {
|
|
13
|
+
return Effect.gen(function* () {
|
|
14
|
+
const deleted = yield* store.deleteExpiredFactsEffect();
|
|
15
|
+
yield* Effect.logInfo(`TtlGarbageCollector: deleted ${deleted} expired facts`);
|
|
16
|
+
}).pipe(Effect.annotateLogs({ processor: "TtlGarbageCollector" }), Effect.withLogSpan("memory:processor:ttl-gc"));
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
name: "TtlGarbageCollector",
|
|
20
|
+
process: (store) => Effect.runPromise(processEffect(store)),
|
|
21
|
+
processEffect,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { MemoryNamespace } from "./MemoryNamespace";
|
|
3
|
+
|
|
4
|
+
export type WorkingMemoryConfig<
|
|
5
|
+
T extends z.ZodObject<z.ZodRawShape> = z.ZodObject<z.ZodRawShape>,
|
|
6
|
+
> = {
|
|
7
|
+
schema?: T;
|
|
8
|
+
namespace: MemoryNamespace;
|
|
9
|
+
ttlMs?: number;
|
|
10
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Effect, Layer } from "effect";
|
|
2
|
+
import { createMemoryStoreLayer } from "./store/createMemoryStoreLayer.js";
|
|
3
|
+
import { MemoryStoreService } from "./store/MemoryStoreService.js";
|
|
4
|
+
import { MemoryService } from "./MemoryService.js";
|
|
5
|
+
/** @typedef {import("./MemoryLayerConfig.ts").MemoryLayerConfig} MemoryLayerConfig */
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {MemoryLayerConfig} config
|
|
9
|
+
* @returns {Layer.Layer<MemoryService, never, never>}
|
|
10
|
+
*/
|
|
11
|
+
export function createMemoryLayer(config) {
|
|
12
|
+
return Layer.effect(MemoryService, Effect.map(MemoryStoreService, (store) => ({
|
|
13
|
+
// Working memory
|
|
14
|
+
getFact: (ns, key) => store.getFactEffect(ns, key),
|
|
15
|
+
setFact: (ns, key, value, ttlMs) => store.setFactEffect(ns, key, value, ttlMs),
|
|
16
|
+
deleteFact: (ns, key) => store.deleteFactEffect(ns, key),
|
|
17
|
+
listFacts: (ns) => store.listFactsEffect(ns),
|
|
18
|
+
// Threads & messages
|
|
19
|
+
createThread: (ns, title) => store.createThreadEffect(ns, title),
|
|
20
|
+
getThread: (threadId) => store.getThreadEffect(threadId),
|
|
21
|
+
deleteThread: (threadId) => store.deleteThreadEffect(threadId),
|
|
22
|
+
saveMessage: (msg) => store.saveMessageEffect(msg),
|
|
23
|
+
listMessages: (threadId, limit) => store.listMessagesEffect(threadId, limit),
|
|
24
|
+
countMessages: (threadId) => store.countMessagesEffect(threadId),
|
|
25
|
+
// Maintenance
|
|
26
|
+
deleteExpiredFacts: () => store.deleteExpiredFactsEffect(),
|
|
27
|
+
// Access underlying store
|
|
28
|
+
store,
|
|
29
|
+
}))).pipe(Layer.provide(createMemoryStoreLayer(config.db)));
|
|
30
|
+
}
|