@oobe-protocol-labs/sap-mcp-server 0.9.22 → 0.9.35
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/.env.example +10 -0
- package/CHANGELOG.md +100 -6
- package/README.md +3 -3
- package/USER_DOCS/00_START_HERE.md +9 -1
- package/USER_DOCS/01_HOSTED_REMOTE_MCP.md +1 -1
- package/USER_DOCS/04_CLIENT_CONFIGS.md +3 -3
- package/USER_DOCS/06_DESKTOP_GUI_WIZARD.md +5 -5
- package/dist/config/env.d.ts +18 -0
- package/dist/config/env.d.ts.map +1 -1
- package/dist/config/env.js +14 -2
- package/dist/config/env.js.map +1 -1
- package/dist/config/mcp-client-injection.js +3 -3
- package/dist/config/paths.d.ts +9 -1
- package/dist/config/paths.d.ts.map +1 -1
- package/dist/config/paths.js +15 -1
- package/dist/config/paths.js.map +1 -1
- package/dist/config/wizard.d.ts.map +1 -1
- package/dist/config/wizard.js +3 -1
- package/dist/config/wizard.js.map +1 -1
- package/dist/core/constants.d.ts +1 -1
- package/dist/core/constants.js +1 -1
- package/dist/core/logger.js +1 -1
- package/dist/memory/async-processor.d.ts +107 -0
- package/dist/memory/async-processor.d.ts.map +1 -0
- package/dist/memory/async-processor.js +204 -0
- package/dist/memory/async-processor.js.map +1 -0
- package/dist/memory/auto-record.d.ts +47 -0
- package/dist/memory/auto-record.d.ts.map +1 -0
- package/dist/memory/auto-record.js +133 -0
- package/dist/memory/auto-record.js.map +1 -0
- package/dist/memory/database.d.ts +103 -0
- package/dist/memory/database.d.ts.map +1 -0
- package/dist/memory/database.js +337 -0
- package/dist/memory/database.js.map +1 -0
- package/dist/memory/hermes-bridge.d.ts +70 -0
- package/dist/memory/hermes-bridge.d.ts.map +1 -0
- package/dist/memory/hermes-bridge.js +155 -0
- package/dist/memory/hermes-bridge.js.map +1 -0
- package/dist/memory/index.d.ts +23 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +22 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/memory-store.d.ts +76 -0
- package/dist/memory/memory-store.d.ts.map +1 -0
- package/dist/memory/memory-store.js +231 -0
- package/dist/memory/memory-store.js.map +1 -0
- package/dist/memory/stream-buffer-store.d.ts +69 -0
- package/dist/memory/stream-buffer-store.d.ts.map +1 -0
- package/dist/memory/stream-buffer-store.js +182 -0
- package/dist/memory/stream-buffer-store.js.map +1 -0
- package/dist/memory/tool-call-store.d.ts +68 -0
- package/dist/memory/tool-call-store.d.ts.map +1 -0
- package/dist/memory/tool-call-store.js +189 -0
- package/dist/memory/tool-call-store.js.map +1 -0
- package/dist/memory/types.d.ts +197 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +15 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/memory/utils.d.ts +30 -0
- package/dist/memory/utils.d.ts.map +1 -0
- package/dist/memory/utils.js +47 -0
- package/dist/memory/utils.js.map +1 -0
- package/dist/payments/hosted-tool-eligibility.d.ts.map +1 -1
- package/dist/payments/hosted-tool-eligibility.js +4 -6
- package/dist/payments/hosted-tool-eligibility.js.map +1 -1
- package/dist/payments/monetization-gate.d.ts +39 -0
- package/dist/payments/monetization-gate.d.ts.map +1 -1
- package/dist/payments/monetization-gate.js +165 -11
- package/dist/payments/monetization-gate.js.map +1 -1
- package/dist/payments/pricing.d.ts.map +1 -1
- package/dist/payments/pricing.js +69 -0
- package/dist/payments/pricing.js.map +1 -1
- package/dist/payments/x402-paid-call.js +1 -1
- package/dist/premium/builtin-plugins.d.ts +42 -0
- package/dist/premium/builtin-plugins.d.ts.map +1 -1
- package/dist/premium/builtin-plugins.js +91 -10
- package/dist/premium/builtin-plugins.js.map +1 -1
- package/dist/premium/manifest-builder.d.ts.map +1 -1
- package/dist/premium/manifest-builder.js +43 -7
- package/dist/premium/manifest-builder.js.map +1 -1
- package/dist/premium/meme-radar-capabilities.js +9 -9
- package/dist/premium/meme-radar-capabilities.js.map +1 -1
- package/dist/premium/plugin-validator.d.ts.map +1 -1
- package/dist/premium/plugin-validator.js +62 -1
- package/dist/premium/plugin-validator.js.map +1 -1
- package/dist/premium/provider-bridge.d.ts +25 -1
- package/dist/premium/provider-bridge.d.ts.map +1 -1
- package/dist/premium/provider-bridge.js +130 -20
- package/dist/premium/provider-bridge.js.map +1 -1
- package/dist/premium/session-manager.d.ts.map +1 -1
- package/dist/premium/session-manager.js +12 -3
- package/dist/premium/session-manager.js.map +1 -1
- package/dist/premium/tech-fundamentals-capabilities.js +5 -5
- package/dist/premium/tech-fundamentals-capabilities.js.map +1 -1
- package/dist/premium/trading-capabilities.js +5 -5
- package/dist/premium/trading-capabilities.js.map +1 -1
- package/dist/premium/webhook-engine.d.ts.map +1 -1
- package/dist/premium/webhook-engine.js +7 -1
- package/dist/premium/webhook-engine.js.map +1 -1
- package/dist/prompts/context/sap-agent-context.prompt.js +1 -1
- package/dist/prompts/payments/explain-x402-settlement.prompt.js +1 -1
- package/dist/remote/premium-routes.d.ts.map +1 -1
- package/dist/remote/premium-routes.js +3 -2
- package/dist/remote/premium-routes.js.map +1 -1
- package/dist/remote/public-home/sections.d.ts.map +1 -1
- package/dist/remote/public-home/sections.js +2 -3
- package/dist/remote/public-home/sections.js.map +1 -1
- package/dist/remote/public-home/styles.d.ts +1 -1
- package/dist/remote/public-home/styles.js +2 -2
- package/dist/remote/server.d.ts.map +1 -1
- package/dist/remote/server.js +19 -4
- package/dist/remote/server.js.map +1 -1
- package/dist/server/server-metadata.js +1 -1
- package/dist/strategies/strategy-store.d.ts +82 -0
- package/dist/strategies/strategy-store.d.ts.map +1 -0
- package/dist/strategies/strategy-store.js +165 -0
- package/dist/strategies/strategy-store.js.map +1 -0
- package/dist/tools/agent-start-tool.d.ts.map +1 -1
- package/dist/tools/agent-start-tool.js +97 -10
- package/dist/tools/agent-start-tool.js.map +1 -1
- package/dist/tools/client-sdk-tools.d.ts.map +1 -1
- package/dist/tools/client-sdk-tools.js +152 -9
- package/dist/tools/client-sdk-tools.js.map +1 -1
- package/dist/tools/estimate-tool-cost.js +1 -1
- package/dist/tools/estimate-tool-cost.js.map +1 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +2 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/magicblock-tools.js +5 -5
- package/dist/tools/magicblock-tools.js.map +1 -1
- package/dist/tools/memory-tools.d.ts +26 -0
- package/dist/tools/memory-tools.d.ts.map +1 -0
- package/dist/tools/memory-tools.js +533 -0
- package/dist/tools/memory-tools.js.map +1 -0
- package/dist/tools/perp-tools.d.ts +7 -9
- package/dist/tools/perp-tools.d.ts.map +1 -1
- package/dist/tools/perp-tools.js +386 -367
- package/dist/tools/perp-tools.js.map +1 -1
- package/dist/tools/premium-tools.d.ts.map +1 -1
- package/dist/tools/premium-tools.js +8 -2
- package/dist/tools/premium-tools.js.map +1 -1
- package/dist/tools/profile-tools.d.ts.map +1 -1
- package/dist/tools/profile-tools.js +18 -1
- package/dist/tools/profile-tools.js.map +1 -1
- package/dist/tools/quick-context-tool.d.ts.map +1 -1
- package/dist/tools/quick-context-tool.js +100 -9
- package/dist/tools/quick-context-tool.js.map +1 -1
- package/dist/tools/register-tools.d.ts.map +1 -1
- package/dist/tools/register-tools.js +3 -0
- package/dist/tools/register-tools.js.map +1 -1
- package/dist/tools/sap-sdk-tools.js +3 -3
- package/dist/tools/sap-sdk-tools.js.map +1 -1
- package/dist/tools/session-context-packet.d.ts +19 -0
- package/dist/tools/session-context-packet.d.ts.map +1 -0
- package/dist/tools/session-context-packet.js +256 -0
- package/dist/tools/session-context-packet.js.map +1 -0
- package/dist/tools/skills-tools.d.ts +10 -0
- package/dist/tools/skills-tools.d.ts.map +1 -1
- package/dist/tools/skills-tools.js +19 -0
- package/dist/tools/skills-tools.js.map +1 -1
- package/dist/tools/transaction-tools.d.ts +1 -0
- package/dist/tools/transaction-tools.d.ts.map +1 -1
- package/dist/tools/transaction-tools.js +248 -9
- package/dist/tools/transaction-tools.js.map +1 -1
- package/dist/tools/x402-paid-call-tool.d.ts.map +1 -1
- package/dist/tools/x402-paid-call-tool.js +55 -21
- package/dist/tools/x402-paid-call-tool.js.map +1 -1
- package/dist/wizard-core/desktop-flow.d.ts.map +1 -1
- package/dist/wizard-core/desktop-flow.js +4 -2
- package/dist/wizard-core/desktop-flow.js.map +1 -1
- package/docs/06_PAYMENTS_X402_AND_PAYSH.md +14 -5
- package/docs/07_ENDPOINTS_AND_CLIENTS.md +1 -1
- package/docs/09_TOOLS_SKILLS_AND_AGENT_GUIDE.md +13 -5
- package/docs/13_BOUNTY_PROGRAM_PROPOSAL.md +1 -1
- package/docs/14_DESKTOP_WIZARD_RELEASE.md +3 -3
- package/docs/15_DASHBOARD_SCREENSHARE_SCRIPT.md +1 -1
- package/docs/BRANCH_AND_CI.md +2 -2
- package/docs/README.md +1 -1
- package/package.json +3 -1
- package/server.json +3 -3
- package/skills/README.md +1 -1
- package/skills/sap-agentkit/SKILL.md +1 -2
- package/skills/sap-defi/SKILL.md +31 -2
- package/skills/sap-mcp/SKILL.md +24 -13
- package/skills/sap-mcp/TOOL_REFERENCE.md +2 -4
- package/skills/sap-payments-x402/SKILL.md +15 -3
- package/assets/logos/drift.svg +0 -12
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module memory/hermes-bridge
|
|
3
|
+
* @description Cross-session integration with Hermes Agent session database.
|
|
4
|
+
*
|
|
5
|
+
* If the user has Hermes Agent installed (detected via ~/.hermes/ directory),
|
|
6
|
+
* the memory subsystem can query Hermes session history for cross-session
|
|
7
|
+
* context. This enables the agent to recall relevant conversations from
|
|
8
|
+
* previous Hermes sessions while keeping the data local.
|
|
9
|
+
*
|
|
10
|
+
* If Hermes is not installed, all bridge functions return empty results —
|
|
11
|
+
* the memory subsystem operates standalone.
|
|
12
|
+
*/
|
|
13
|
+
import { existsSync } from 'fs';
|
|
14
|
+
import { join } from 'path';
|
|
15
|
+
import { homedir } from 'os';
|
|
16
|
+
import { createRequire } from 'module';
|
|
17
|
+
import { logger } from '../core/logger.js';
|
|
18
|
+
const require = createRequire(import.meta.url);
|
|
19
|
+
/**
|
|
20
|
+
* @name HermesBridge
|
|
21
|
+
* @description Bridges the SAP MCP memory subsystem with Hermes Agent session data.
|
|
22
|
+
*
|
|
23
|
+
* Detection: checks for ~/.hermes/ directory. If present, the bridge can query
|
|
24
|
+
* the Hermes SQLite session database for relevant past conversations.
|
|
25
|
+
*/
|
|
26
|
+
export class HermesBridge {
|
|
27
|
+
hermesDir;
|
|
28
|
+
hermesDbPath;
|
|
29
|
+
available = null;
|
|
30
|
+
constructor() {
|
|
31
|
+
this.hermesDir = join(homedir(), '.hermes');
|
|
32
|
+
this.hermesDbPath = join(this.hermesDir, 'sessions.db');
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @name isAvailable
|
|
36
|
+
* @description Checks whether Hermes Agent is installed on this machine.
|
|
37
|
+
* Caches the result to avoid repeated filesystem checks.
|
|
38
|
+
*/
|
|
39
|
+
isAvailable() {
|
|
40
|
+
if (this.available !== null)
|
|
41
|
+
return this.available;
|
|
42
|
+
this.available = existsSync(this.hermesDir);
|
|
43
|
+
if (this.available) {
|
|
44
|
+
logger.info('Hermes Agent detected — cross-session bridge enabled', {
|
|
45
|
+
hermesDir: this.hermesDir,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return this.available;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @name getHermesDir
|
|
52
|
+
* @description Returns the Hermes installation directory.
|
|
53
|
+
*/
|
|
54
|
+
getHermesDir() {
|
|
55
|
+
return this.hermesDir;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @name searchSessions
|
|
59
|
+
* @description Searches Hermes session history for relevant past conversations.
|
|
60
|
+
*
|
|
61
|
+
* This is a lightweight bridge — it reads the Hermes session database using
|
|
62
|
+
* the same better-sqlite3 driver (opening a read-only connection). The query
|
|
63
|
+
* uses FTS5 if the Hermes DB has it, otherwise falls back to LIKE search.
|
|
64
|
+
*
|
|
65
|
+
* @param query - Search query (natural language).
|
|
66
|
+
* @param limit - Max results. Default 5.
|
|
67
|
+
* @returns Array of session snippets with timestamps.
|
|
68
|
+
*/
|
|
69
|
+
searchSessions(query, limit = 5) {
|
|
70
|
+
if (!this.isAvailable())
|
|
71
|
+
return [];
|
|
72
|
+
if (!existsSync(this.hermesDbPath))
|
|
73
|
+
return [];
|
|
74
|
+
try {
|
|
75
|
+
// Use dynamic import to avoid loading better-sqlite3 if Hermes is not present.
|
|
76
|
+
const Database = require('better-sqlite3');
|
|
77
|
+
const db = new Database(this.hermesDbPath, { readonly: true });
|
|
78
|
+
try {
|
|
79
|
+
// Try FTS5 search first (if Hermes has it).
|
|
80
|
+
const ftsResults = db.prepare(`SELECT s.id, s.title, substr(m.content, 1, 200) as snippet, s.created_at
|
|
81
|
+
FROM messages m
|
|
82
|
+
JOIN sessions s ON s.id = m.session_id
|
|
83
|
+
WHERE m.content MATCH ?
|
|
84
|
+
ORDER BY s.created_at DESC
|
|
85
|
+
LIMIT ?`).all(query, limit);
|
|
86
|
+
return ftsResults.map(r => ({
|
|
87
|
+
sessionId: r.id,
|
|
88
|
+
title: r.title ?? 'Untitled',
|
|
89
|
+
snippet: r.snippet,
|
|
90
|
+
timestamp: r.created_at,
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// FTS not available — try LIKE search.
|
|
95
|
+
const likeResults = db.prepare(`SELECT s.id, s.title, substr(m.content, 1, 200) as snippet, s.created_at
|
|
96
|
+
FROM messages m
|
|
97
|
+
JOIN sessions s ON s.id = m.session_id
|
|
98
|
+
WHERE m.content LIKE ?
|
|
99
|
+
ORDER BY s.created_at DESC
|
|
100
|
+
LIMIT ?`).all(`%${query}%`, limit);
|
|
101
|
+
return likeResults.map(r => ({
|
|
102
|
+
sessionId: r.id,
|
|
103
|
+
title: r.title ?? 'Untitled',
|
|
104
|
+
snippet: r.snippet,
|
|
105
|
+
timestamp: r.created_at,
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
db.close();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
logger.debug('Hermes bridge query failed', {
|
|
114
|
+
error: error instanceof Error ? error.message : String(error),
|
|
115
|
+
});
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @name getRecentSessions
|
|
121
|
+
* @description Returns recent Hermes sessions for context injection.
|
|
122
|
+
* @param limit - Max sessions. Default 3.
|
|
123
|
+
*/
|
|
124
|
+
getRecentSessions(limit = 3) {
|
|
125
|
+
if (!this.isAvailable())
|
|
126
|
+
return [];
|
|
127
|
+
if (!existsSync(this.hermesDbPath))
|
|
128
|
+
return [];
|
|
129
|
+
try {
|
|
130
|
+
const Database = require('better-sqlite3');
|
|
131
|
+
const db = new Database(this.hermesDbPath, { readonly: true });
|
|
132
|
+
try {
|
|
133
|
+
const results = db.prepare(`SELECT id, title, created_at FROM sessions
|
|
134
|
+
ORDER BY created_at DESC LIMIT ?`).all(limit);
|
|
135
|
+
return results.map(r => ({
|
|
136
|
+
sessionId: r.id,
|
|
137
|
+
title: r.title ?? 'Untitled',
|
|
138
|
+
timestamp: r.created_at,
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
db.close();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return [];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @name hermesBridge
|
|
152
|
+
* @description Singleton instance of the Hermes bridge.
|
|
153
|
+
*/
|
|
154
|
+
export const hermesBridge = new HermesBridge();
|
|
155
|
+
//# sourceMappingURL=hermes-bridge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hermes-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+1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module memory
|
|
3
|
+
* @description SAP MCP local agent memory subsystem — SQLite FTS5 backed.
|
|
4
|
+
*
|
|
5
|
+
* Exports all stores and types for use by the MCP tool registration layer.
|
|
6
|
+
*
|
|
7
|
+
* Architecture:
|
|
8
|
+
* - database.ts: Thread-safe singleton SQLite connection with WAL mode + FTS5.
|
|
9
|
+
* - tool-call-store.ts: Records and searches tool call history.
|
|
10
|
+
* - memory-store.ts: Manages LLM-generated agent memories with relevance decay.
|
|
11
|
+
* - stream-buffer-store.ts: Buffers premium stream events for offline consumption.
|
|
12
|
+
* - types.ts: Type definitions.
|
|
13
|
+
* - utils.ts: Utility functions (truncate, decay, expiry).
|
|
14
|
+
*/
|
|
15
|
+
export { MemoryDatabase, memoryDatabase, DEFAULT_CONFIG } from './database.js';
|
|
16
|
+
export { ToolCallStore, toolCallStore } from './tool-call-store.js';
|
|
17
|
+
export { MemoryStore, memoryStore } from './memory-store.js';
|
|
18
|
+
export { StreamBufferStore, streamBufferStore } from './stream-buffer-store.js';
|
|
19
|
+
export { AsyncMemoryProcessor, asyncMemoryProcessor } from './async-processor.js';
|
|
20
|
+
export { recordToolCall } from './auto-record.js';
|
|
21
|
+
export { HermesBridge, hermesBridge } from './hermes-bridge.js';
|
|
22
|
+
export type { ToolCallRecord, ToolCallOutcome, AgentMemoryRecord, MemoryType, StreamBufferRecord, MemorySearchResult, MemoryQueryOptions, MemoryStats, MemoryPruneResult, MemoryConfig, } from './types.js';
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/memory/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/E,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAChF,OAAO,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAClF,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAChE,YAAY,EACV,cAAc,EACd,eAAe,EACf,iBAAiB,EACjB,UAAU,EACV,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,WAAW,EACX,iBAAiB,EACjB,YAAY,GACb,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module memory
|
|
3
|
+
* @description SAP MCP local agent memory subsystem — SQLite FTS5 backed.
|
|
4
|
+
*
|
|
5
|
+
* Exports all stores and types for use by the MCP tool registration layer.
|
|
6
|
+
*
|
|
7
|
+
* Architecture:
|
|
8
|
+
* - database.ts: Thread-safe singleton SQLite connection with WAL mode + FTS5.
|
|
9
|
+
* - tool-call-store.ts: Records and searches tool call history.
|
|
10
|
+
* - memory-store.ts: Manages LLM-generated agent memories with relevance decay.
|
|
11
|
+
* - stream-buffer-store.ts: Buffers premium stream events for offline consumption.
|
|
12
|
+
* - types.ts: Type definitions.
|
|
13
|
+
* - utils.ts: Utility functions (truncate, decay, expiry).
|
|
14
|
+
*/
|
|
15
|
+
export { MemoryDatabase, memoryDatabase, DEFAULT_CONFIG } from './database.js';
|
|
16
|
+
export { ToolCallStore, toolCallStore } from './tool-call-store.js';
|
|
17
|
+
export { MemoryStore, memoryStore } from './memory-store.js';
|
|
18
|
+
export { StreamBufferStore, streamBufferStore } from './stream-buffer-store.js';
|
|
19
|
+
export { AsyncMemoryProcessor, asyncMemoryProcessor } from './async-processor.js';
|
|
20
|
+
export { recordToolCall } from './auto-record.js';
|
|
21
|
+
export { HermesBridge, hermesBridge } from './hermes-bridge.js';
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/memory/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/E,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAChF,OAAO,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAClF,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module memory/memory-store
|
|
3
|
+
* @description Store for agent memories — LLM-compressed summaries of past interactions.
|
|
4
|
+
*
|
|
5
|
+
* Memories are created by the agent (or auto-generated by the summarize tool)
|
|
6
|
+
* and stored with a relevance score that decays over time. The FTS5 index
|
|
7
|
+
* enables semantic search across all memories for context injection.
|
|
8
|
+
*/
|
|
9
|
+
import type { AgentMemoryRecord, MemoryType, MemorySearchResult, MemoryQueryOptions } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* @name MemoryStore
|
|
12
|
+
* @description Manages agent memory records with relevance decay and FTS5 search.
|
|
13
|
+
*/
|
|
14
|
+
export declare class MemoryStore {
|
|
15
|
+
/**
|
|
16
|
+
* @name record
|
|
17
|
+
* @description Creates a new agent memory entry.
|
|
18
|
+
* @param record - Memory data to record (without id, timestamps, relevance).
|
|
19
|
+
* @param initialRelevance - Initial relevance score 0-1. Default 0.8.
|
|
20
|
+
* @returns The inserted record ID, or -1 if degraded.
|
|
21
|
+
*/
|
|
22
|
+
record(record: Pick<AgentMemoryRecord, 'memoryType' | 'category' | 'summary' | 'sourceToolCalls' | 'expiresAt'>, initialRelevance?: number): number;
|
|
23
|
+
/**
|
|
24
|
+
* @name search
|
|
25
|
+
* @description Full-text search across agent memories using FTS5.
|
|
26
|
+
* Results are ranked by BM25 relevance with decayed score.
|
|
27
|
+
*/
|
|
28
|
+
search(options: MemoryQueryOptions): MemorySearchResult<AgentMemoryRecord & {
|
|
29
|
+
decayedRelevance: number;
|
|
30
|
+
}>;
|
|
31
|
+
/**
|
|
32
|
+
* @name recall
|
|
33
|
+
* @description Returns the most relevant memories for a given category.
|
|
34
|
+
* Used for prompt injection — the agent gets the top N memories ordered
|
|
35
|
+
* by decayed relevance.
|
|
36
|
+
* @param category - Tool category to recall memories for.
|
|
37
|
+
* @param limit - Maximum number of memories. Default 5.
|
|
38
|
+
* @returns Relevant memories sorted by decayed relevance.
|
|
39
|
+
*/
|
|
40
|
+
recall(category: string, limit?: number): AgentMemoryRecord[];
|
|
41
|
+
/**
|
|
42
|
+
* @name count
|
|
43
|
+
* @description Returns the total number of agent memory records.
|
|
44
|
+
*/
|
|
45
|
+
count(): number;
|
|
46
|
+
/**
|
|
47
|
+
* @name memoryTypeBreakdown
|
|
48
|
+
* @description Returns counts grouped by memory type.
|
|
49
|
+
*/
|
|
50
|
+
memoryTypeBreakdown(): Record<MemoryType, number>;
|
|
51
|
+
/**
|
|
52
|
+
* @name lastMemoryAt
|
|
53
|
+
* @description Returns the timestamp of the most recent memory.
|
|
54
|
+
*/
|
|
55
|
+
lastMemoryAt(): string | null;
|
|
56
|
+
/**
|
|
57
|
+
* @name prune
|
|
58
|
+
* @description Removes expired memories and memories with relevance below
|
|
59
|
+
* the threshold after decay.
|
|
60
|
+
* @param minRelevance - Minimum decayed relevance to keep. Default 0.05.
|
|
61
|
+
* @returns Number of memories pruned.
|
|
62
|
+
*/
|
|
63
|
+
prune(minRelevance?: number): number;
|
|
64
|
+
/**
|
|
65
|
+
* @name buildFtsQuery
|
|
66
|
+
* @description Converts a natural language query into a FTS5 MATCH expression.
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
69
|
+
private buildFtsQuery;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @name memoryStore
|
|
73
|
+
* @description Singleton instance of the memory store.
|
|
74
|
+
*/
|
|
75
|
+
export declare const memoryStore: MemoryStore;
|
|
76
|
+
//# sourceMappingURL=memory-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-store.d.ts","sourceRoot":"","sources":["../../src/memory/memory-store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAExG;;;GAGG;AACH,qBAAa,WAAW;IACtB;;;;;;OAMG;IACH,MAAM,CACJ,MAAM,EAAE,IAAI,CAAC,iBAAiB,EAAE,YAAY,GAAG,UAAU,GAAG,SAAS,GAAG,iBAAiB,GAAG,WAAW,CAAC,EACxG,gBAAgB,SAAM,GACrB,MAAM;IAmBT;;;;OAIG;IACH,MAAM,CAAC,OAAO,EAAE,kBAAkB,GAAG,kBAAkB,CAAC,iBAAiB,GAAG;QAAE,gBAAgB,EAAE,MAAM,CAAA;KAAE,CAAC;IAsEzG;;;;;;;;OAQG;IACH,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,SAAI,GAAG,iBAAiB,EAAE;IA2BxD;;;OAGG;IACH,KAAK,IAAI,MAAM;IAQf;;;OAGG;IACH,mBAAmB,IAAI,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC;IAgBjD;;;OAGG;IACH,YAAY,IAAI,MAAM,GAAG,IAAI;IAS7B;;;;;;OAMG;IACH,KAAK,CAAC,YAAY,SAAO,GAAG,MAAM;IA4BlC;;;;OAIG;IACH,OAAO,CAAC,aAAa;CAOtB;AAED;;;GAGG;AACH,eAAO,MAAM,WAAW,aAAoB,CAAC"}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module memory/memory-store
|
|
3
|
+
* @description Store for agent memories — LLM-compressed summaries of past interactions.
|
|
4
|
+
*
|
|
5
|
+
* Memories are created by the agent (or auto-generated by the summarize tool)
|
|
6
|
+
* and stored with a relevance score that decays over time. The FTS5 index
|
|
7
|
+
* enables semantic search across all memories for context injection.
|
|
8
|
+
*/
|
|
9
|
+
import { memoryDatabase } from './database.js';
|
|
10
|
+
import { decayRelevance, isExpired } from './utils.js';
|
|
11
|
+
/**
|
|
12
|
+
* @name MemoryStore
|
|
13
|
+
* @description Manages agent memory records with relevance decay and FTS5 search.
|
|
14
|
+
*/
|
|
15
|
+
export class MemoryStore {
|
|
16
|
+
/**
|
|
17
|
+
* @name record
|
|
18
|
+
* @description Creates a new agent memory entry.
|
|
19
|
+
* @param record - Memory data to record (without id, timestamps, relevance).
|
|
20
|
+
* @param initialRelevance - Initial relevance score 0-1. Default 0.8.
|
|
21
|
+
* @returns The inserted record ID, or -1 if degraded.
|
|
22
|
+
*/
|
|
23
|
+
record(record, initialRelevance = 0.8) {
|
|
24
|
+
if (memoryDatabase.isDegraded())
|
|
25
|
+
return -1;
|
|
26
|
+
memoryDatabase.init();
|
|
27
|
+
const stmt = memoryDatabase.getStatement('insert_memory');
|
|
28
|
+
if (!stmt)
|
|
29
|
+
return -1;
|
|
30
|
+
const result = stmt.run({
|
|
31
|
+
memoryType: record.memoryType,
|
|
32
|
+
category: record.category,
|
|
33
|
+
summary: record.summary,
|
|
34
|
+
sourceToolCalls: record.sourceToolCalls ?? null,
|
|
35
|
+
relevanceScore: Math.max(0, Math.min(1, initialRelevance)),
|
|
36
|
+
expiresAt: record.expiresAt ?? null,
|
|
37
|
+
});
|
|
38
|
+
return result.lastInsertRowid;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @name search
|
|
42
|
+
* @description Full-text search across agent memories using FTS5.
|
|
43
|
+
* Results are ranked by BM25 relevance with decayed score.
|
|
44
|
+
*/
|
|
45
|
+
search(options) {
|
|
46
|
+
if (memoryDatabase.isDegraded()) {
|
|
47
|
+
return { results: [], total: 0, hasMore: false };
|
|
48
|
+
}
|
|
49
|
+
memoryDatabase.init();
|
|
50
|
+
const db = memoryDatabase.getDb();
|
|
51
|
+
const limit = Math.min(options.limit ?? 20, 100);
|
|
52
|
+
const offset = options.offset ?? 0;
|
|
53
|
+
let whereClause = '';
|
|
54
|
+
const params = { limit, offset };
|
|
55
|
+
if (options.memoryType) {
|
|
56
|
+
whereClause += ' AND m.memory_type = @memoryType';
|
|
57
|
+
params['memoryType'] = options.memoryType;
|
|
58
|
+
}
|
|
59
|
+
if (options.category) {
|
|
60
|
+
whereClause += ' AND m.category = @category';
|
|
61
|
+
params['category'] = options.category;
|
|
62
|
+
}
|
|
63
|
+
const ftsQuery = this.buildFtsQuery(options.query);
|
|
64
|
+
const sql = `
|
|
65
|
+
SELECT m.id, m.memory_type, m.category, m.summary, m.source_tool_calls,
|
|
66
|
+
m.relevance_score, m.created_at, m.expires_at, m.last_accessed_at
|
|
67
|
+
FROM agent_memory_fts fts
|
|
68
|
+
JOIN agent_memory m ON m.id = fts.rowid
|
|
69
|
+
WHERE agent_memory_fts MATCH @ftsQuery
|
|
70
|
+
${whereClause}
|
|
71
|
+
ORDER BY ${options.sort === 'newest' ? 'm.created_at DESC' : options.sort === 'oldest' ? 'm.created_at ASC' : 'bm25(agent_memory_fts)'}
|
|
72
|
+
LIMIT @limit OFFSET @offset
|
|
73
|
+
`;
|
|
74
|
+
params['ftsQuery'] = ftsQuery;
|
|
75
|
+
try {
|
|
76
|
+
const rows = db.prepare(sql).all(params);
|
|
77
|
+
const results = rows.map(row => ({
|
|
78
|
+
...row,
|
|
79
|
+
decayedRelevance: decayRelevance(row.relevanceScore, row.createdAt, 0.01),
|
|
80
|
+
}));
|
|
81
|
+
// Filter by minimum relevance (after decay).
|
|
82
|
+
const filtered = options.minRelevance
|
|
83
|
+
? results.filter(r => r.decayedRelevance >= (options.minRelevance ?? 0))
|
|
84
|
+
: results;
|
|
85
|
+
// Filter out expired memories.
|
|
86
|
+
const active = filtered.filter(r => !isExpired(r.expiresAt));
|
|
87
|
+
// Update last_accessed_at for returned memories.
|
|
88
|
+
const updateStmt = memoryDatabase.getStatement('update_memory_access');
|
|
89
|
+
if (updateStmt) {
|
|
90
|
+
for (const r of active) {
|
|
91
|
+
if (r.id)
|
|
92
|
+
updateStmt.run(r.id);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
results: active,
|
|
97
|
+
total: active.length,
|
|
98
|
+
hasMore: false,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return { results: [], total: 0, hasMore: false };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* @name recall
|
|
107
|
+
* @description Returns the most relevant memories for a given category.
|
|
108
|
+
* Used for prompt injection — the agent gets the top N memories ordered
|
|
109
|
+
* by decayed relevance.
|
|
110
|
+
* @param category - Tool category to recall memories for.
|
|
111
|
+
* @param limit - Maximum number of memories. Default 5.
|
|
112
|
+
* @returns Relevant memories sorted by decayed relevance.
|
|
113
|
+
*/
|
|
114
|
+
recall(category, limit = 5) {
|
|
115
|
+
if (memoryDatabase.isDegraded())
|
|
116
|
+
return [];
|
|
117
|
+
memoryDatabase.init();
|
|
118
|
+
const db = memoryDatabase.getDb();
|
|
119
|
+
try {
|
|
120
|
+
const rows = db.prepare(`SELECT * FROM agent_memory
|
|
121
|
+
WHERE category = ? AND (expires_at IS NULL OR expires_at > datetime('now'))
|
|
122
|
+
ORDER BY relevance_score DESC
|
|
123
|
+
LIMIT ?`).all(category, limit);
|
|
124
|
+
// Update last_accessed_at.
|
|
125
|
+
const updateStmt = memoryDatabase.getStatement('update_memory_access');
|
|
126
|
+
if (updateStmt) {
|
|
127
|
+
for (const row of rows) {
|
|
128
|
+
if (row.id)
|
|
129
|
+
updateStmt.run(row.id);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return rows;
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* @name count
|
|
140
|
+
* @description Returns the total number of agent memory records.
|
|
141
|
+
*/
|
|
142
|
+
count() {
|
|
143
|
+
if (memoryDatabase.isDegraded())
|
|
144
|
+
return 0;
|
|
145
|
+
memoryDatabase.init();
|
|
146
|
+
const stmt = memoryDatabase.getStatement('count_memory');
|
|
147
|
+
if (!stmt)
|
|
148
|
+
return 0;
|
|
149
|
+
return stmt.get().count;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* @name memoryTypeBreakdown
|
|
153
|
+
* @description Returns counts grouped by memory type.
|
|
154
|
+
*/
|
|
155
|
+
memoryTypeBreakdown() {
|
|
156
|
+
if (memoryDatabase.isDegraded()) {
|
|
157
|
+
return { lesson: 0, pattern: 0, failure: 0, success: 0 };
|
|
158
|
+
}
|
|
159
|
+
memoryDatabase.init();
|
|
160
|
+
const stmt = memoryDatabase.getStatement('count_memory_by_type');
|
|
161
|
+
if (!stmt)
|
|
162
|
+
return { lesson: 0, pattern: 0, failure: 0, success: 0 };
|
|
163
|
+
const rows = stmt.all();
|
|
164
|
+
const result = { lesson: 0, pattern: 0, failure: 0, success: 0 };
|
|
165
|
+
for (const row of rows) {
|
|
166
|
+
result[row.memory_type] = row.count;
|
|
167
|
+
}
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* @name lastMemoryAt
|
|
172
|
+
* @description Returns the timestamp of the most recent memory.
|
|
173
|
+
*/
|
|
174
|
+
lastMemoryAt() {
|
|
175
|
+
if (memoryDatabase.isDegraded())
|
|
176
|
+
return null;
|
|
177
|
+
memoryDatabase.init();
|
|
178
|
+
const stmt = memoryDatabase.getStatement('last_memory_at');
|
|
179
|
+
if (!stmt)
|
|
180
|
+
return null;
|
|
181
|
+
const row = stmt.get();
|
|
182
|
+
return row?.created_at ?? null;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* @name prune
|
|
186
|
+
* @description Removes expired memories and memories with relevance below
|
|
187
|
+
* the threshold after decay.
|
|
188
|
+
* @param minRelevance - Minimum decayed relevance to keep. Default 0.05.
|
|
189
|
+
* @returns Number of memories pruned.
|
|
190
|
+
*/
|
|
191
|
+
prune(minRelevance = 0.05) {
|
|
192
|
+
if (memoryDatabase.isDegraded())
|
|
193
|
+
return 0;
|
|
194
|
+
memoryDatabase.init();
|
|
195
|
+
const db = memoryDatabase.getDb();
|
|
196
|
+
// Delete expired memories.
|
|
197
|
+
const expiredResult = db.prepare(`DELETE FROM agent_memory WHERE expires_at IS NOT NULL AND expires_at <= datetime('now')`).run();
|
|
198
|
+
// Delete low-relevance memories (check decayed relevance in JS to avoid
|
|
199
|
+
// complex SQL — better-sqlite3 is synchronous so this is fast).
|
|
200
|
+
const allRows = db.prepare(`SELECT id, relevance_score, created_at FROM agent_memory`).all();
|
|
201
|
+
let pruned = expiredResult.changes;
|
|
202
|
+
for (const row of allRows) {
|
|
203
|
+
const decayed = decayRelevance(row.relevance_score, row.created_at, 0.01);
|
|
204
|
+
if (decayed < minRelevance) {
|
|
205
|
+
db.prepare('DELETE FROM agent_memory WHERE id = ?').run(row.id);
|
|
206
|
+
pruned++;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return pruned;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* @name buildFtsQuery
|
|
213
|
+
* @description Converts a natural language query into a FTS5 MATCH expression.
|
|
214
|
+
* @internal
|
|
215
|
+
*/
|
|
216
|
+
buildFtsQuery(query) {
|
|
217
|
+
const escaped = query.replace(/["'*:]/g, ' ').trim();
|
|
218
|
+
if (!escaped)
|
|
219
|
+
return '*';
|
|
220
|
+
const words = escaped.split(/\s+/).filter(w => w.length > 0);
|
|
221
|
+
if (words.length === 1)
|
|
222
|
+
return `"${words[0]}"*`;
|
|
223
|
+
return words.map(w => `"${w}"*`).join(' OR ');
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* @name memoryStore
|
|
228
|
+
* @description Singleton instance of the memory store.
|
|
229
|
+
*/
|
|
230
|
+
export const memoryStore = new MemoryStore();
|
|
231
|
+
//# sourceMappingURL=memory-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-store.js","sourceRoot":"","sources":["../../src/memory/memory-store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAGvD;;;GAGG;AACH,MAAM,OAAO,WAAW;IACtB;;;;;;OAMG;IACH,MAAM,CACJ,MAAwG,EACxG,gBAAgB,GAAG,GAAG;QAEtB,IAAI,cAAc,CAAC,UAAU,EAAE;YAAE,OAAO,CAAC,CAAC,CAAC;QAC3C,cAAc,CAAC,IAAI,EAAE,CAAC;QAEtB,MAAM,IAAI,GAAG,cAAc,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;QAC1D,IAAI,CAAC,IAAI;YAAE,OAAO,CAAC,CAAC,CAAC;QAErB,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC;YACtB,UAAU,EAAE,MAAM,CAAC,UAAU;YAC7B,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,eAAe,EAAE,MAAM,CAAC,eAAe,IAAI,IAAI;YAC/C,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAA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CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module memory/stream-buffer-store
|
|
3
|
+
* @description Store for buffering stream events locally in SQLite.
|
|
4
|
+
*
|
|
5
|
+
* Premium stream events (Pyth price ticks, meme alerts, volatility signals) are
|
|
6
|
+
* persisted locally so the agent can consume them without paid polls. Events
|
|
7
|
+
* are deduplicated by (streamType, eventId) and consumed in FIFO order.
|
|
8
|
+
*/
|
|
9
|
+
import type { StreamBufferRecord } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* @name StreamBufferStore
|
|
12
|
+
* @description Manages local stream event buffering with FIFO consumption.
|
|
13
|
+
*/
|
|
14
|
+
export declare class StreamBufferStore {
|
|
15
|
+
/**
|
|
16
|
+
* @name buffer
|
|
17
|
+
* @description Buffers a stream event. Deduplicates by (streamType, eventId).
|
|
18
|
+
* @param record - Event to buffer.
|
|
19
|
+
* @returns The row ID, or -1 if the event was already buffered (dedup).
|
|
20
|
+
*/
|
|
21
|
+
buffer(record: Omit<StreamBufferRecord, 'id' | 'consumed' | 'createdAt'>): number;
|
|
22
|
+
/**
|
|
23
|
+
* @name consume
|
|
24
|
+
* @description Returns unconsumed events for a stream type and marks them consumed.
|
|
25
|
+
* FIFO order (oldest first). Returns up to `limit` events.
|
|
26
|
+
* @param streamType - Stream type to consume (e.g. 'pyth-price').
|
|
27
|
+
* @param limit - Maximum events to consume. Default 20.
|
|
28
|
+
* @returns Consumed events.
|
|
29
|
+
*/
|
|
30
|
+
consume(streamType: string, limit?: number): StreamBufferRecord[];
|
|
31
|
+
/**
|
|
32
|
+
* @name peek
|
|
33
|
+
* @description Returns unconsumed events without marking them consumed.
|
|
34
|
+
* Used for preview/inspection.
|
|
35
|
+
*/
|
|
36
|
+
peek(streamType: string, limit?: number): StreamBufferRecord[];
|
|
37
|
+
/**
|
|
38
|
+
* @name replay
|
|
39
|
+
* @description Returns all events (consumed + unconsumed) for a stream type
|
|
40
|
+
* within a time range. Used for backtest/analysis.
|
|
41
|
+
* @param streamType - Stream type to replay.
|
|
42
|
+
* @param since - ISO 8601 timestamp. Events created after this time.
|
|
43
|
+
* @param limit - Maximum events. Default 100.
|
|
44
|
+
*/
|
|
45
|
+
replay(streamType: string, since?: string, limit?: number): StreamBufferRecord[];
|
|
46
|
+
/**
|
|
47
|
+
* @name pendingCount
|
|
48
|
+
* @description Returns the number of unconsumed events.
|
|
49
|
+
*/
|
|
50
|
+
pendingCount(): number;
|
|
51
|
+
/**
|
|
52
|
+
* @name evict
|
|
53
|
+
* @description Evicts old consumed events to keep the buffer bounded.
|
|
54
|
+
* @param maxPerType - Maximum events per stream type. Default 10000.
|
|
55
|
+
* @returns Number of events evicted.
|
|
56
|
+
*/
|
|
57
|
+
evict(maxPerType?: number): number;
|
|
58
|
+
/**
|
|
59
|
+
* @name clear
|
|
60
|
+
* @description Clears all events for a stream type.
|
|
61
|
+
*/
|
|
62
|
+
clear(streamType: string): number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @name streamBufferStore
|
|
66
|
+
* @description Singleton instance of the stream buffer store.
|
|
67
|
+
*/
|
|
68
|
+
export declare const streamBufferStore: StreamBufferStore;
|
|
69
|
+
//# sourceMappingURL=stream-buffer-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-buffer-store.d.ts","sourceRoot":"","sources":["../../src/memory/stream-buffer-store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD;;;GAGG;AACH,qBAAa,iBAAiB;IAC5B;;;;;OAKG;IACH,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,kBAAkB,EAAE,IAAI,GAAG,UAAU,GAAG,WAAW,CAAC,GAAG,MAAM;IAqBjF;;;;;;;OAOG;IACH,OAAO,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,SAAK,GAAG,kBAAkB,EAAE;IA4B7D;;;;OAIG;IACH,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,SAAK,GAAG,kBAAkB,EAAE;IAiB1D;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,KAAK,SAAM,GAAG,kBAAkB,EAAE;IAuB7E;;;OAGG;IACH,YAAY,IAAI,MAAM;IAQtB;;;;;OAKG;IACH,KAAK,CAAC,UAAU,SAAS,GAAG,MAAM;IAoBlC;;;OAGG;IACH,KAAK,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM;CAalC;AAED;;;GAGG;AACH,eAAO,MAAM,iBAAiB,mBAA0B,CAAC"}
|