memoryblock 0.0.1 → 0.1.0-beta
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/bin/mblk.js +6 -0
- package/dist/bin/mblk.d.ts +3 -0
- package/dist/bin/mblk.d.ts.map +1 -0
- package/dist/bin/mblk.js +339 -0
- package/dist/bin/mblk.js.map +1 -0
- package/dist/cli/commands/create.d.ts +2 -0
- package/dist/cli/commands/create.d.ts.map +1 -0
- package/dist/cli/commands/create.js +48 -0
- package/dist/cli/commands/create.js.map +1 -0
- package/dist/cli/commands/delete.d.ts +5 -0
- package/dist/cli/commands/delete.d.ts.map +1 -0
- package/dist/cli/commands/delete.js +147 -0
- package/dist/cli/commands/delete.js.map +1 -0
- package/dist/cli/commands/init.d.ts +9 -0
- package/dist/cli/commands/init.d.ts.map +1 -0
- package/dist/cli/commands/init.js +209 -0
- package/dist/cli/commands/init.js.map +1 -0
- package/dist/cli/commands/permissions.d.ts +13 -0
- package/dist/cli/commands/permissions.d.ts.map +1 -0
- package/dist/cli/commands/permissions.js +60 -0
- package/dist/cli/commands/permissions.js.map +1 -0
- package/dist/cli/commands/plugin-settings.d.ts +6 -0
- package/dist/cli/commands/plugin-settings.d.ts.map +1 -0
- package/dist/cli/commands/plugin-settings.js +118 -0
- package/dist/cli/commands/plugin-settings.js.map +1 -0
- package/dist/cli/commands/plugins.d.ts +3 -0
- package/dist/cli/commands/plugins.d.ts.map +1 -0
- package/dist/cli/commands/plugins.js +83 -0
- package/dist/cli/commands/plugins.js.map +1 -0
- package/dist/cli/commands/reset.d.ts +8 -0
- package/dist/cli/commands/reset.d.ts.map +1 -0
- package/dist/cli/commands/reset.js +96 -0
- package/dist/cli/commands/reset.js.map +1 -0
- package/dist/cli/commands/server.d.ts +25 -0
- package/dist/cli/commands/server.d.ts.map +1 -0
- package/dist/cli/commands/server.js +295 -0
- package/dist/cli/commands/server.js.map +1 -0
- package/dist/cli/commands/service.d.ts +18 -0
- package/dist/cli/commands/service.d.ts.map +1 -0
- package/dist/cli/commands/service.js +309 -0
- package/dist/cli/commands/service.js.map +1 -0
- package/dist/cli/commands/start.d.ts +11 -0
- package/dist/cli/commands/start.d.ts.map +1 -0
- package/dist/cli/commands/start.js +801 -0
- package/dist/cli/commands/start.js.map +1 -0
- package/dist/cli/commands/status.d.ts +2 -0
- package/dist/cli/commands/status.d.ts.map +1 -0
- package/dist/cli/commands/status.js +78 -0
- package/dist/cli/commands/status.js.map +1 -0
- package/dist/cli/commands/stop.d.ts +9 -0
- package/dist/cli/commands/stop.d.ts.map +1 -0
- package/dist/cli/commands/stop.js +83 -0
- package/dist/cli/commands/stop.js.map +1 -0
- package/dist/cli/commands/web.d.ts +5 -0
- package/dist/cli/commands/web.d.ts.map +1 -0
- package/dist/cli/commands/web.js +63 -0
- package/dist/cli/commands/web.js.map +1 -0
- package/dist/cli/constants.d.ts +38 -0
- package/dist/cli/constants.d.ts.map +1 -0
- package/dist/cli/constants.js +80 -0
- package/dist/cli/constants.js.map +1 -0
- package/dist/cli/logger.d.ts +12 -0
- package/dist/cli/logger.d.ts.map +1 -0
- package/dist/cli/logger.js +40 -0
- package/dist/cli/logger.js.map +1 -0
- package/dist/engine/agent.d.ts +15 -0
- package/dist/engine/agent.d.ts.map +1 -0
- package/dist/engine/agent.js +19 -0
- package/dist/engine/agent.js.map +1 -0
- package/dist/engine/conversation-log.d.ts +35 -0
- package/dist/engine/conversation-log.d.ts.map +1 -0
- package/dist/engine/conversation-log.js +83 -0
- package/dist/engine/conversation-log.js.map +1 -0
- package/dist/engine/cost-tracker.d.ts +52 -0
- package/dist/engine/cost-tracker.d.ts.map +1 -0
- package/dist/engine/cost-tracker.js +110 -0
- package/dist/engine/cost-tracker.js.map +1 -0
- package/dist/engine/gatekeeper.d.ts +20 -0
- package/dist/engine/gatekeeper.d.ts.map +1 -0
- package/dist/engine/gatekeeper.js +43 -0
- package/dist/engine/gatekeeper.js.map +1 -0
- package/dist/engine/memory.d.ts +28 -0
- package/dist/engine/memory.d.ts.map +1 -0
- package/dist/engine/memory.js +69 -0
- package/dist/engine/memory.js.map +1 -0
- package/dist/engine/monitor.d.ts +81 -0
- package/dist/engine/monitor.d.ts.map +1 -0
- package/dist/engine/monitor.js +610 -0
- package/dist/engine/monitor.js.map +1 -0
- package/dist/engine/prompts.d.ts +31 -0
- package/dist/engine/prompts.d.ts.map +1 -0
- package/dist/engine/prompts.js +93 -0
- package/dist/engine/prompts.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/schemas.d.ts +544 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +111 -0
- package/dist/schemas.js.map +1 -0
- package/dist/types.d.ts +188 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/config.d.ts +24 -0
- package/dist/utils/config.d.ts.map +1 -0
- package/dist/utils/config.js +86 -0
- package/dist/utils/config.js.map +1 -0
- package/dist/utils/fs.d.ts +18 -0
- package/dist/utils/fs.d.ts.map +1 -0
- package/dist/utils/fs.js +65 -0
- package/dist/utils/fs.js.map +1 -0
- package/package.json +37 -8
- package/src/bin/mblk.ts +347 -0
- package/src/cli/commands/create.ts +62 -0
- package/src/cli/commands/delete.ts +165 -0
- package/src/cli/commands/init.ts +241 -0
- package/src/cli/commands/permissions.ts +77 -0
- package/src/cli/commands/plugin-settings.ts +111 -0
- package/src/cli/commands/plugins.ts +89 -0
- package/src/cli/commands/reset.ts +97 -0
- package/src/cli/commands/server.ts +327 -0
- package/src/cli/commands/service.ts +300 -0
- package/src/cli/commands/start.ts +843 -0
- package/src/cli/commands/status.ts +90 -0
- package/src/cli/commands/stop.ts +91 -0
- package/src/cli/commands/web.ts +74 -0
- package/src/cli/constants.ts +88 -0
- package/src/cli/logger.ts +48 -0
- package/src/engine/agent.ts +19 -0
- package/src/engine/conversation-log.ts +90 -0
- package/src/engine/cost-tracker.ts +134 -0
- package/src/engine/gatekeeper.ts +53 -0
- package/src/engine/memory.ts +87 -0
- package/src/engine/monitor.ts +719 -0
- package/src/engine/prompts.ts +102 -0
- package/src/index.ts +88 -0
- package/src/schemas.ts +126 -0
- package/src/types.ts +220 -0
- package/src/utils/config.ts +106 -0
- package/src/utils/fs.ts +64 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { LLMAdapter, LLMMessage, TokenUsage } from 'memoryblock';
|
|
2
|
+
import { readTextSafe, atomicWrite } from '../utils/fs.js';
|
|
3
|
+
import { log } from '../cli/logger.js';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Memory Manager: Implements the 80% rule.
|
|
8
|
+
* When the active session hits the threshold, the LLM summarizes its learnings
|
|
9
|
+
* into memory.md, and the session is reborn with fresh context.
|
|
10
|
+
*/
|
|
11
|
+
export class MemoryManager {
|
|
12
|
+
private readonly maxTokens: number;
|
|
13
|
+
private readonly threshold: number; // 0-1
|
|
14
|
+
private accumulatedTokens: number = 0;
|
|
15
|
+
|
|
16
|
+
constructor(maxContextTokens: number, thresholdPercent: number) {
|
|
17
|
+
this.maxTokens = maxContextTokens;
|
|
18
|
+
this.threshold = thresholdPercent / 100;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Track token usage from a response. */
|
|
22
|
+
trackUsage(usage: TokenUsage): void {
|
|
23
|
+
this.accumulatedTokens += usage.totalTokens;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Check if the accumulated tokens exceed the threshold. */
|
|
27
|
+
shouldSummarize(): boolean {
|
|
28
|
+
return this.accumulatedTokens >= this.maxTokens * this.threshold;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Get current token count. */
|
|
32
|
+
getTokenCount(): number {
|
|
33
|
+
return this.accumulatedTokens;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Reset token tracking after a rebirth. */
|
|
37
|
+
reset(): void {
|
|
38
|
+
this.accumulatedTokens = 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Load the block's memory.md. */
|
|
42
|
+
async loadMemory(blockPath: string): Promise<string> {
|
|
43
|
+
return readTextSafe(join(blockPath, 'memory.md'), '');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Summarize the current session and write to memory.md.
|
|
48
|
+
* Asks the LLM to distill its learnings.
|
|
49
|
+
*/
|
|
50
|
+
async summarize(
|
|
51
|
+
adapter: LLMAdapter,
|
|
52
|
+
messages: LLMMessage[],
|
|
53
|
+
blockPath: string,
|
|
54
|
+
): Promise<string> {
|
|
55
|
+
const summarizePrompt: LLMMessage[] = [
|
|
56
|
+
{
|
|
57
|
+
role: 'system',
|
|
58
|
+
content:
|
|
59
|
+
'You are a memory manager. Summarize the conversation into a structured memory document. ' +
|
|
60
|
+
'Include: key decisions made, important context, current goals, progress toward those goals, ' +
|
|
61
|
+
'and what should be done next. Write in markdown format. ' +
|
|
62
|
+
'CRITICAL: Keep the summary under 1500 words. Be concise. Omit pleasantries and redundancy.',
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
role: 'user',
|
|
66
|
+
content:
|
|
67
|
+
'Summarize this conversation for your future self. Focus on what was accomplished, ' +
|
|
68
|
+
'key learnings, and next steps:\n\n' +
|
|
69
|
+
messages
|
|
70
|
+
.filter((m) => m.role !== 'system')
|
|
71
|
+
.map((m) => `[${m.role}]: ${(m.content || '(tool interaction)').slice(0, 300)}`)
|
|
72
|
+
.join('\n'),
|
|
73
|
+
},
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
const response = await adapter.converse(summarizePrompt);
|
|
77
|
+
const summary = response.message.content || '';
|
|
78
|
+
|
|
79
|
+
const memoryContent = `# Monitor Memory\n\n> Last updated: ${new Date().toISOString()}\n\n${summary}\n`;
|
|
80
|
+
await atomicWrite(join(blockPath, 'memory.md'), memoryContent);
|
|
81
|
+
|
|
82
|
+
log.info(`Memory summarized (${this.accumulatedTokens} tokens → rebirth)`);
|
|
83
|
+
this.reset();
|
|
84
|
+
|
|
85
|
+
return memoryContent;
|
|
86
|
+
}
|
|
87
|
+
}
|