openclaw-engram 0.5.0 → 1.0.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/README.md +161 -0
- package/dist/context/formatter.js +2 -2
- package/dist/context/formatter.js.map +1 -1
- package/dist/context/tiers.d.ts +26 -0
- package/dist/context/tiers.d.ts.map +1 -0
- package/dist/context/tiers.js +113 -0
- package/dist/context/tiers.js.map +1 -0
- package/dist/hooks/after-tool-call.d.ts +3 -2
- package/dist/hooks/after-tool-call.d.ts.map +1 -1
- package/dist/hooks/after-tool-call.js +4 -3
- package/dist/hooks/after-tool-call.js.map +1 -1
- package/dist/hooks/before-agent-start.d.ts +23 -0
- package/dist/hooks/before-agent-start.d.ts.map +1 -0
- package/dist/hooks/before-agent-start.js +83 -0
- package/dist/hooks/before-agent-start.js.map +1 -0
- package/dist/hooks/before-compaction.d.ts +3 -2
- package/dist/hooks/before-compaction.d.ts.map +1 -1
- package/dist/hooks/before-compaction.js +11 -6
- package/dist/hooks/before-compaction.js.map +1 -1
- package/dist/hooks/before-prompt-build.d.ts +3 -2
- package/dist/hooks/before-prompt-build.d.ts.map +1 -1
- package/dist/hooks/before-prompt-build.js +13 -6
- package/dist/hooks/before-prompt-build.js.map +1 -1
- package/dist/hooks/session-end.d.ts +3 -2
- package/dist/hooks/session-end.d.ts.map +1 -1
- package/dist/hooks/session-end.js +15 -7
- package/dist/hooks/session-end.js.map +1 -1
- package/dist/hooks/session-start.d.ts +9 -5
- package/dist/hooks/session-start.d.ts.map +1 -1
- package/dist/hooks/session-start.js +18 -51
- package/dist/hooks/session-start.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -8
- package/dist/index.js.map +1 -1
- package/dist/security/redactor.d.ts +18 -0
- package/dist/security/redactor.d.ts.map +1 -0
- package/dist/security/redactor.js +128 -0
- package/dist/security/redactor.js.map +1 -0
- package/dist/tools/engram-remember.d.ts.map +1 -1
- package/dist/tools/engram-remember.js +5 -4
- package/dist/tools/engram-remember.js.map +1 -1
- package/dist/tools/engram-search.d.ts.map +1 -1
- package/dist/tools/engram-search.js +8 -1
- package/dist/tools/engram-search.js.map +1 -1
- package/dist/types/openclaw.d.ts +21 -5
- package/dist/types/openclaw.d.ts.map +1 -1
- package/openclaw.plugin.json +2 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# openclaw-engram
|
|
2
|
+
|
|
3
|
+
OpenClaw plugin that connects agents to an [engram](https://github.com/thebtf/engram) persistent memory server for long-term context retention.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- OpenClaw gateway installed and running
|
|
8
|
+
- Engram server running and reachable (default port `37777`)
|
|
9
|
+
- An API token configured on the engram server
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
### Fresh install
|
|
14
|
+
|
|
15
|
+
1. **Temporarily remove the memory slot if configured** (avoids circular validation during install):
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# If plugins.slots.memory is currently set to "engram", clear it first:
|
|
19
|
+
openclaw config set plugins.slots.memory ""
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
2. **Install the plugin:**
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
openclaw plugins install openclaw-engram
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
This runs `npm pack` and extracts the plugin into `~/.openclaw/extensions/engram/`.
|
|
29
|
+
|
|
30
|
+
3. **Configure the engram server connection:**
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
openclaw config set plugins.entries.engram.config.url http://your-engram-server:37777
|
|
34
|
+
openclaw config set plugins.entries.engram.config.token your-api-token
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
4. **Assign as the memory backend:**
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
openclaw config set plugins.slots.memory engram
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
5. **Restart the gateway:**
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
openclaw gateway restart
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Updating
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
openclaw plugins install openclaw-engram
|
|
53
|
+
openclaw gateway restart
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Note on package naming
|
|
57
|
+
|
|
58
|
+
The npm package is `openclaw-engram` but the plugin ID is `engram`.
|
|
59
|
+
OpenClaw may show a cosmetic warning about ID mismatch — this is harmless.
|
|
60
|
+
The plugin registers and operates correctly under the `engram` ID.
|
|
61
|
+
|
|
62
|
+
## Configuration
|
|
63
|
+
|
|
64
|
+
All fields are set under `plugins.entries.engram.config`:
|
|
65
|
+
|
|
66
|
+
| Field | Type | Default | Description |
|
|
67
|
+
|---|---|---|---|
|
|
68
|
+
| `url` | string | `http://localhost:37777` | Engram server base URL |
|
|
69
|
+
| `token` | string | *(required)* | Bearer token for API authentication |
|
|
70
|
+
| `project` | string | *(auto-detected)* | Project scope override; defaults to workspace identity |
|
|
71
|
+
| `contextLimit` | number | `10` | Maximum observations injected per prompt turn |
|
|
72
|
+
| `sessionContextLimit` | number | `20` | Maximum observations injected at session start |
|
|
73
|
+
| `tokenBudget` | number | `2000` | Token budget for context injection (~4 chars/token) |
|
|
74
|
+
| `timeoutMs` | number | `5000` | Per-request HTTP timeout in milliseconds |
|
|
75
|
+
| `autoExtract` | boolean | `true` | Auto-extract observations on compaction and session end |
|
|
76
|
+
| `workspaceDir` | string | `~/.openclaw/workspace/` | Workspace path for `/migrate` when called from channel context (Telegram, Discord) |
|
|
77
|
+
| `logLevel` | string | `warn` | Log verbosity: `debug`, `info`, `warn`, `error` |
|
|
78
|
+
|
|
79
|
+
## What it provides
|
|
80
|
+
|
|
81
|
+
### Hooks
|
|
82
|
+
|
|
83
|
+
The plugin hooks into the agent lifecycle automatically — no configuration needed:
|
|
84
|
+
|
|
85
|
+
| Hook | Effect |
|
|
86
|
+
|---|---|
|
|
87
|
+
| `session_start` | Injects up to `sessionContextLimit` relevant observations into system context |
|
|
88
|
+
| `before_prompt_build` | Injects up to `contextLimit` observations relevant to the current turn |
|
|
89
|
+
| `after_tool_call` | Forwards tool events to engram for self-learning |
|
|
90
|
+
| `before_compaction` | Backfills conversation transcript into engram before context is compacted |
|
|
91
|
+
| `session_end` | Final transcript backfill when the session closes |
|
|
92
|
+
|
|
93
|
+
### Agent tools
|
|
94
|
+
|
|
95
|
+
Available to the agent during a session:
|
|
96
|
+
|
|
97
|
+
| Tool | Description |
|
|
98
|
+
|---|---|
|
|
99
|
+
| `engram_search` | Semantic search across all stored observations |
|
|
100
|
+
| `memory_search` | Alias for `engram_search` (memory-core compatible name) |
|
|
101
|
+
| `engram_remember` | Store a new observation with structured metadata |
|
|
102
|
+
| `memory_store` | Alias for `engram_remember` |
|
|
103
|
+
| `engram_decisions` | Retrieve past architectural decisions by query |
|
|
104
|
+
| `memory_get` | Retrieve a specific observation by ID |
|
|
105
|
+
| `memory_forget` | Delete an observation by ID |
|
|
106
|
+
| `memory_migrate` | Import local `MEMORY.md` / `memory/**/*.md` files into engram |
|
|
107
|
+
|
|
108
|
+
### Slash commands
|
|
109
|
+
|
|
110
|
+
| Command | Description |
|
|
111
|
+
|---|---|
|
|
112
|
+
| `/memory` | Show memory status and search |
|
|
113
|
+
| `/remember <text>` | Store a memory immediately |
|
|
114
|
+
| `/migrate [--dry-run] [--force] [path]` | Import local memory files into engram |
|
|
115
|
+
|
|
116
|
+
### CLI
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
openclaw memory status # Show server health and version
|
|
120
|
+
openclaw memory search <query> # Search engram memory
|
|
121
|
+
openclaw memory store <text> # Store a memory
|
|
122
|
+
openclaw memory migrate # Import local memory files
|
|
123
|
+
openclaw memory migrate --dry-run # Preview without importing
|
|
124
|
+
openclaw memory migrate --force # Re-import already migrated files
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Troubleshooting
|
|
128
|
+
|
|
129
|
+
**"plugin not found: engram" during install**
|
|
130
|
+
|
|
131
|
+
Circular dependency: OpenClaw tries to load the memory plugin before it is installed.
|
|
132
|
+
Fix: clear `plugins.slots.memory` first (step 1 of Installation), install, then re-set the slot.
|
|
133
|
+
|
|
134
|
+
**"plugin id mismatch" warning**
|
|
135
|
+
|
|
136
|
+
The npm package name (`openclaw-engram`) differs from the plugin ID (`engram`).
|
|
137
|
+
This is cosmetic — the plugin loads and works correctly. No action needed.
|
|
138
|
+
|
|
139
|
+
**Plugin loads but no memory is injected into prompts**
|
|
140
|
+
|
|
141
|
+
1. Verify the server is reachable: `openclaw memory status`
|
|
142
|
+
2. Check `url` and `token` are set correctly
|
|
143
|
+
3. Increase `logLevel` to `debug` and restart the gateway to see injection details
|
|
144
|
+
4. Confirm `plugins.slots.memory` is set to `engram`
|
|
145
|
+
|
|
146
|
+
**Context injection is slow or times out**
|
|
147
|
+
|
|
148
|
+
Increase `timeoutMs` (default 5000 ms). Engram failures are non-blocking — if the server is unreachable the agent continues without memory context.
|
|
149
|
+
|
|
150
|
+
## Development
|
|
151
|
+
|
|
152
|
+
Requirements: Node.js >=18, TypeScript >=5.
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
cd plugin/openclaw-engram
|
|
156
|
+
npm install
|
|
157
|
+
npm run build # compile TypeScript → dist/
|
|
158
|
+
npm run typecheck # type-check without emitting
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
The compiled output in `dist/` is what OpenClaw loads. Re-run `npm run build` after any source change and restart the gateway.
|
|
@@ -146,7 +146,7 @@ function applyTokenBudget(observations, tokenBudget) {
|
|
|
146
146
|
return { budgeted, trimmedCount: observations.length - budgeted.length };
|
|
147
147
|
}
|
|
148
148
|
function renderXml(groups) {
|
|
149
|
-
let out = '<
|
|
149
|
+
let out = '<engram-context>\n';
|
|
150
150
|
out += '# Relevant Knowledge From Previous Sessions\n';
|
|
151
151
|
out +=
|
|
152
152
|
'IMPORTANT: Use this information to answer the question directly. Do NOT explore the codebase if the answer is here.\n\n';
|
|
@@ -187,7 +187,7 @@ function renderXml(groups) {
|
|
|
187
187
|
'REMINDER: Before modifying any file mentioned above, call `engram_search(query="path")` to check for additional context. ';
|
|
188
188
|
out +=
|
|
189
189
|
'Before architectural decisions, call `engram_decisions(query="...")`. These engram tools are available and MUST be used.\n';
|
|
190
|
-
out += '</
|
|
190
|
+
out += '</engram-context>\n';
|
|
191
191
|
return out;
|
|
192
192
|
}
|
|
193
193
|
//# sourceMappingURL=formatter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatter.js","sourceRoot":"","sources":["../../src/context/formatter.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAuDH,sCAgDC;AAnFD,MAAM,YAAY,GAA6D;IAC7E,EAAE,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,WAAW,EAAE;IACxC,EAAE,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,2BAA2B,EAAE;IACvD,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,gBAAgB,EAAE;IAC3C,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,iBAAiB,EAAE;CAC7C,CAAC;AAeF,8EAA8E;AAC9E,aAAa;AACb,8EAA8E;AAE9E;;;;;;;;;;GAUG;AACH,SAAgB,aAAa,CAC3B,YAA2B,EAC3B,UAA4B,EAAE;IAE9B,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,IAAI,CAAC;IAEhD,wBAAwB;IACxB,MAAM,IAAI,GAAG,YAAY,CAAC,MAAM,CAC9B,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,KAAK,YAAY,CAC3D,CAAC;IAEF,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,W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SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YAC3C,IAAI,SAAS,KAAK,EAAE;gBAAE,GAAG,IAAI,GAAG,SAAS,MAAM,CAAC;YAEhD,GAAG,EAAE,CAAC;QACR,CAAC;IACH,CAAC;IAED,GAAG,IAAI,SAAS,CAAC;IACjB,GAAG;QACD,2HAA2H,CAAC;IAC9H,GAAG;QACD,4HAA4H,CAAC;IAC/H,GAAG,IAAI,qBAAqB,CAAC;IAC7B,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type ContextTier = 'FULL' | 'TARGETED' | 'MINIMAL' | 'NONE';
|
|
2
|
+
export interface TierConfig {
|
|
3
|
+
/** How often to do a FULL search (every N turns). Default: 8. */
|
|
4
|
+
fullSearchInterval: number;
|
|
5
|
+
/** How often to do a MINIMAL search if no other trigger. Default: 4. */
|
|
6
|
+
minimalInterval: number;
|
|
7
|
+
/** Maximum prompt length (chars) to be classified as NONE. Default: 30. */
|
|
8
|
+
shortPromptMaxChars: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const DEFAULT_TIER_CONFIG: TierConfig;
|
|
11
|
+
export interface TierResult {
|
|
12
|
+
tier: ContextTier;
|
|
13
|
+
tokenBudget: number;
|
|
14
|
+
reason: string;
|
|
15
|
+
}
|
|
16
|
+
export declare class TurnTracker {
|
|
17
|
+
private turnCount;
|
|
18
|
+
private lastPrompt;
|
|
19
|
+
private readonly config;
|
|
20
|
+
constructor(config?: Partial<TierConfig>);
|
|
21
|
+
/** Classify the current turn and return the appropriate tier. */
|
|
22
|
+
classify(prompt: string): TierResult;
|
|
23
|
+
/** Reset tracker (e.g., on new session). */
|
|
24
|
+
reset(): void;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=tiers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tiers.d.ts","sourceRoot":"","sources":["../../src/context/tiers.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,MAAM,CAAC;AAEnE,MAAM,WAAW,UAAU;IACzB,iEAAiE;IACjE,kBAAkB,EAAE,MAAM,CAAC;IAC3B,wEAAwE;IACxE,eAAe,EAAE,MAAM,CAAC;IACxB,2EAA2E;IAC3E,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAED,eAAO,MAAM,mBAAmB,EAAE,UAIjC,CAAC;AAEF,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,WAAW,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;CAChB;AA4DD,qBAAa,WAAW;IACtB,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,UAAU,CAAM;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;gBAExB,MAAM,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC;IAIxC,iEAAiE;IACjE,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU;IA+CpC,4CAA4C;IAC5C,KAAK,IAAI,IAAI;CAId"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TurnTracker = exports.DEFAULT_TIER_CONFIG = void 0;
|
|
4
|
+
exports.DEFAULT_TIER_CONFIG = {
|
|
5
|
+
fullSearchInterval: 8,
|
|
6
|
+
minimalInterval: 4,
|
|
7
|
+
shortPromptMaxChars: 30,
|
|
8
|
+
};
|
|
9
|
+
const TOKEN_BUDGETS = {
|
|
10
|
+
FULL: 2000,
|
|
11
|
+
TARGETED: 1000,
|
|
12
|
+
MINIMAL: 500,
|
|
13
|
+
NONE: 0,
|
|
14
|
+
};
|
|
15
|
+
const RECALL_PHRASES = [
|
|
16
|
+
'remember',
|
|
17
|
+
'recall',
|
|
18
|
+
'what do you know about',
|
|
19
|
+
'prior decision',
|
|
20
|
+
'previous session',
|
|
21
|
+
'what was decided',
|
|
22
|
+
'engram',
|
|
23
|
+
'memory',
|
|
24
|
+
];
|
|
25
|
+
const STOP_WORDS = new Set([
|
|
26
|
+
'the', 'this', 'that', 'with', 'from', 'have', 'been',
|
|
27
|
+
'will', 'would', 'could', 'should', 'about', 'into',
|
|
28
|
+
'what', 'when', 'where', 'which', 'there', 'their',
|
|
29
|
+
'also', 'just', 'more', 'some', 'than', 'them', 'then',
|
|
30
|
+
'very', 'your', 'make', 'like', 'does', 'each', 'only',
|
|
31
|
+
'need', 'want', 'please', 'can', 'are', 'for', 'and',
|
|
32
|
+
'but', 'not', 'you', 'all', 'any', 'her', 'was', 'one',
|
|
33
|
+
'our', 'out', 'has', 'had', 'how', 'its', 'may', 'new',
|
|
34
|
+
'now', 'old', 'see', 'way', 'who', 'did', 'get', 'let',
|
|
35
|
+
'say', 'she', 'too', 'use',
|
|
36
|
+
]);
|
|
37
|
+
function extractSignificantWords(text) {
|
|
38
|
+
return new Set(text
|
|
39
|
+
.toLowerCase()
|
|
40
|
+
.split(/\W+/)
|
|
41
|
+
.filter((w) => w.length > 3 && !STOP_WORDS.has(w)));
|
|
42
|
+
}
|
|
43
|
+
function jaccardSimilarity(a, b) {
|
|
44
|
+
if (a.size === 0 && b.size === 0)
|
|
45
|
+
return 1;
|
|
46
|
+
const intersection = new Set([...a].filter((w) => b.has(w)));
|
|
47
|
+
const union = new Set([...a, ...b]);
|
|
48
|
+
return union.size === 0 ? 1 : intersection.size / union.size;
|
|
49
|
+
}
|
|
50
|
+
function isTopicShift(current, previous) {
|
|
51
|
+
if (!previous)
|
|
52
|
+
return false;
|
|
53
|
+
const currentWords = extractSignificantWords(current);
|
|
54
|
+
const previousWords = extractSignificantWords(previous);
|
|
55
|
+
return jaccardSimilarity(currentWords, previousWords) < 0.3;
|
|
56
|
+
}
|
|
57
|
+
function makeTier(tier, reason) {
|
|
58
|
+
return { tier, tokenBudget: TOKEN_BUDGETS[tier], reason };
|
|
59
|
+
}
|
|
60
|
+
class TurnTracker {
|
|
61
|
+
turnCount = 0;
|
|
62
|
+
lastPrompt = '';
|
|
63
|
+
config;
|
|
64
|
+
constructor(config) {
|
|
65
|
+
this.config = { ...exports.DEFAULT_TIER_CONFIG, ...config };
|
|
66
|
+
}
|
|
67
|
+
/** Classify the current turn and return the appropriate tier. */
|
|
68
|
+
classify(prompt) {
|
|
69
|
+
this.turnCount++;
|
|
70
|
+
const lowerPrompt = prompt.toLowerCase();
|
|
71
|
+
const hasRecallPhrase = RECALL_PHRASES.some((phrase) => lowerPrompt.includes(phrase));
|
|
72
|
+
// Rule 1: First turn always gets full context.
|
|
73
|
+
if (this.turnCount === 1) {
|
|
74
|
+
this.lastPrompt = prompt;
|
|
75
|
+
return makeTier('FULL', 'first turn');
|
|
76
|
+
}
|
|
77
|
+
// Rule 2: Explicit recall phrases trigger FULL.
|
|
78
|
+
if (hasRecallPhrase) {
|
|
79
|
+
this.lastPrompt = prompt;
|
|
80
|
+
return makeTier('FULL', 'explicit recall');
|
|
81
|
+
}
|
|
82
|
+
// Rule 3: Short prompts without a question mark → NONE.
|
|
83
|
+
if (prompt.length <= this.config.shortPromptMaxChars && !prompt.includes('?')) {
|
|
84
|
+
this.lastPrompt = prompt;
|
|
85
|
+
return makeTier('NONE', 'short response');
|
|
86
|
+
}
|
|
87
|
+
// Rule 4: Periodic FULL.
|
|
88
|
+
if (this.turnCount % this.config.fullSearchInterval === 0) {
|
|
89
|
+
this.lastPrompt = prompt;
|
|
90
|
+
return makeTier('FULL', 'periodic full');
|
|
91
|
+
}
|
|
92
|
+
// Rule 5: Periodic MINIMAL.
|
|
93
|
+
if (this.turnCount % this.config.minimalInterval === 0) {
|
|
94
|
+
this.lastPrompt = prompt;
|
|
95
|
+
return makeTier('MINIMAL', 'periodic minimal');
|
|
96
|
+
}
|
|
97
|
+
// Rule 6: Topic shift → TARGETED.
|
|
98
|
+
if (isTopicShift(prompt, this.lastPrompt)) {
|
|
99
|
+
this.lastPrompt = prompt;
|
|
100
|
+
return makeTier('TARGETED', 'topic shift');
|
|
101
|
+
}
|
|
102
|
+
// Default: no trigger.
|
|
103
|
+
this.lastPrompt = prompt;
|
|
104
|
+
return makeTier('NONE', 'no trigger');
|
|
105
|
+
}
|
|
106
|
+
/** Reset tracker (e.g., on new session). */
|
|
107
|
+
reset() {
|
|
108
|
+
this.turnCount = 0;
|
|
109
|
+
this.lastPrompt = '';
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.TurnTracker = TurnTracker;
|
|
113
|
+
//# sourceMappingURL=tiers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tiers.js","sourceRoot":"","sources":["../../src/context/tiers.ts"],"names":[],"mappings":";;;AAWa,QAAA,mBAAmB,GAAe;IAC7C,kBAAkB,EAAE,CAAC;IACrB,eAAe,EAAE,CAAC;IAClB,mBAAmB,EAAE,EAAE;CACxB,CAAC;AAQF,MAAM,aAAa,GAAgC;IACjD,IAAI,EAAE,IAAI;IACV,QAAQ,EAAE,IAAI;IACd,OAAO,EAAE,GAAG;IACZ,IAAI,EAAE,CAAC;CACR,CAAC;AAEF,MAAM,cAAc,GAAG;IACrB,UAAU;IACV,QAAQ;IACR,wBAAwB;IACxB,gBAAgB;IAChB,kBAAkB;IAClB,kBAAkB;IAClB,QAAQ;IACR,QAAQ;CACT,CAAC;AAEF,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC;IACzB,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IACrD,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM;IACnD,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO;IAClD,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IACtD,MAAM,EAAE,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vB,CAAC;CACF;AA9DD,kCA8DC"}
|
|
@@ -7,13 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { EngramRestClient } from '../client.js';
|
|
9
9
|
import type { PluginConfig } from '../config.js';
|
|
10
|
-
import type { AfterToolCallEvent } from '../types/openclaw.js';
|
|
10
|
+
import type { AfterToolCallEvent, PluginHookContext } from '../types/openclaw.js';
|
|
11
11
|
/**
|
|
12
12
|
* Handle the after_tool_call hook.
|
|
13
13
|
*
|
|
14
14
|
* @param event - The after_tool_call event from OpenClaw.
|
|
15
|
+
* @param ctx - The hook context containing agent identity fields.
|
|
15
16
|
* @param client - Shared engram REST client.
|
|
16
17
|
* @param config - Resolved plugin config.
|
|
17
18
|
*/
|
|
18
|
-
export declare function handleAfterToolCall(event: AfterToolCallEvent, client: EngramRestClient, config: PluginConfig): void;
|
|
19
|
+
export declare function handleAfterToolCall(event: AfterToolCallEvent, ctx: PluginHookContext, client: EngramRestClient, config: PluginConfig): void;
|
|
19
20
|
//# sourceMappingURL=after-tool-call.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"after-tool-call.d.ts","sourceRoot":"","sources":["../../src/hooks/after-tool-call.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"after-tool-call.d.ts","sourceRoot":"","sources":["../../src/hooks/after-tool-call.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAKlF;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,kBAAkB,EACzB,GAAG,EAAE,iBAAiB,EACtB,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,YAAY,GACnB,IAAI,CA0BN"}
|
|
@@ -15,16 +15,17 @@ const TOOL_RESULT_MAX_CHARS = 500;
|
|
|
15
15
|
* Handle the after_tool_call hook.
|
|
16
16
|
*
|
|
17
17
|
* @param event - The after_tool_call event from OpenClaw.
|
|
18
|
+
* @param ctx - The hook context containing agent identity fields.
|
|
18
19
|
* @param client - Shared engram REST client.
|
|
19
20
|
* @param config - Resolved plugin config.
|
|
20
21
|
*/
|
|
21
|
-
function handleAfterToolCall(event, client, config) {
|
|
22
|
+
function handleAfterToolCall(event, ctx, client, config) {
|
|
22
23
|
if (!client.isAvailable())
|
|
23
24
|
return;
|
|
24
25
|
if (!config.autoExtract)
|
|
25
26
|
return;
|
|
26
|
-
const agentId =
|
|
27
|
-
const identity = (0, identity_js_1.resolveIdentity)(agentId,
|
|
27
|
+
const agentId = ctx.agentId ?? '';
|
|
28
|
+
const identity = (0, identity_js_1.resolveIdentity)(agentId, ctx.workspaceDir);
|
|
28
29
|
const project = config.project ?? identity.projectId;
|
|
29
30
|
let toolInput;
|
|
30
31
|
let toolResult;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"after-tool-call.js","sourceRoot":"","sources":["../../src/hooks/after-tool-call.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;
|
|
1
|
+
{"version":3,"file":"after-tool-call.js","sourceRoot":"","sources":["../../src/hooks/after-tool-call.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAkBH,kDA+BC;AA7CD,gDAAiD;AAGjD,MAAM,oBAAoB,GAAG,GAAG,CAAC;AACjC,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAElC;;;;;;;GAOG;AACH,SAAgB,mBAAmB,CACjC,KAAyB,EACzB,GAAs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,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* before_agent_start hook — fetches static session context from engram and
|
|
3
|
+
* injects it as appendSystemContext so it is available for the entire session.
|
|
4
|
+
*
|
|
5
|
+
* This hook replaces the context-injection logic that was previously in
|
|
6
|
+
* session_start. The SDK passes agent identity via ctx (not event), so all
|
|
7
|
+
* identity resolution happens here via the ctx parameter.
|
|
8
|
+
*/
|
|
9
|
+
import type { EngramRestClient } from '../client.js';
|
|
10
|
+
import type { PluginConfig } from '../config.js';
|
|
11
|
+
import type { BeforeAgentStartEvent, BeforeAgentStartResult, PluginHookContext, PluginLogger } from '../types/openclaw.js';
|
|
12
|
+
/**
|
|
13
|
+
* Handle the before_agent_start hook.
|
|
14
|
+
*
|
|
15
|
+
* @param event - The before_agent_start event from OpenClaw.
|
|
16
|
+
* @param ctx - The hook context containing agent identity fields.
|
|
17
|
+
* @param client - Shared engram REST client.
|
|
18
|
+
* @param config - Resolved plugin config.
|
|
19
|
+
* @param logger - Optional logger (falls back to console).
|
|
20
|
+
* @returns Append-system-context result, or void if nothing to inject.
|
|
21
|
+
*/
|
|
22
|
+
export declare function handleBeforeAgentStart(event: BeforeAgentStartEvent, ctx: PluginHookContext, client: EngramRestClient, config: PluginConfig, logger?: PluginLogger): Promise<BeforeAgentStartResult | void>;
|
|
23
|
+
//# sourceMappingURL=before-agent-start.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"before-agent-start.d.ts","sourceRoot":"","sources":["../../src/hooks/before-agent-start.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGjD,OAAO,KAAK,EACV,qBAAqB,EACrB,sBAAsB,EACtB,iBAAiB,EACjB,YAAY,EACb,MAAM,sBAAsB,CAAC;AAE9B;;;;;;;;;GASG;AACH,wBAAsB,sBAAsB,CAC1C,KAAK,EAAE,qBAAqB,EAC5B,GAAG,EAAE,iBAAiB,EACtB,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,YAAY,EACpB,MAAM,CAAC,EAAE,YAAY,GACpB,OAAO,CAAC,sBAAsB,GAAG,IAAI,CAAC,CA6DxC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* before_agent_start hook — fetches static session context from engram and
|
|
4
|
+
* injects it as appendSystemContext so it is available for the entire session.
|
|
5
|
+
*
|
|
6
|
+
* This hook replaces the context-injection logic that was previously in
|
|
7
|
+
* session_start. The SDK passes agent identity via ctx (not event), so all
|
|
8
|
+
* identity resolution happens here via the ctx parameter.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.handleBeforeAgentStart = handleBeforeAgentStart;
|
|
12
|
+
const identity_js_1 = require("../identity.js");
|
|
13
|
+
const formatter_js_1 = require("../context/formatter.js");
|
|
14
|
+
/**
|
|
15
|
+
* Handle the before_agent_start hook.
|
|
16
|
+
*
|
|
17
|
+
* @param event - The before_agent_start event from OpenClaw.
|
|
18
|
+
* @param ctx - The hook context containing agent identity fields.
|
|
19
|
+
* @param client - Shared engram REST client.
|
|
20
|
+
* @param config - Resolved plugin config.
|
|
21
|
+
* @param logger - Optional logger (falls back to console).
|
|
22
|
+
* @returns Append-system-context result, or void if nothing to inject.
|
|
23
|
+
*/
|
|
24
|
+
async function handleBeforeAgentStart(event, ctx, client, config, logger) {
|
|
25
|
+
try {
|
|
26
|
+
if (!client.isAvailable())
|
|
27
|
+
return;
|
|
28
|
+
const agentId = ctx.agentId ?? '';
|
|
29
|
+
const identity = (0, identity_js_1.resolveIdentity)(agentId, ctx.workspaceDir);
|
|
30
|
+
const project = config.project ?? identity.projectId;
|
|
31
|
+
const response = await client.getContextInject(agentId, ctx.workspaceDir);
|
|
32
|
+
if (!response || !Array.isArray(response.observations) || response.observations.length === 0) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const { context, injectedIds, trimmedCount } = (0, formatter_js_1.formatContext)(response.observations, { tokenBudget: config.tokenBudget });
|
|
36
|
+
if (trimmedCount > 0) {
|
|
37
|
+
(logger ?? console).warn(`[engram] before-agent-start: trimmed ${trimmedCount} observations to fit token budget`);
|
|
38
|
+
}
|
|
39
|
+
if (!context)
|
|
40
|
+
return;
|
|
41
|
+
// Mark observations as injected (fire-and-forget)
|
|
42
|
+
if (injectedIds.length > 0 && response.sessionId) {
|
|
43
|
+
void client.markInjected(response.sessionId, injectedIds);
|
|
44
|
+
}
|
|
45
|
+
// Initialize session tracking (fire-and-forget)
|
|
46
|
+
const claudeSessionId = ctx.sessionId ?? agentId;
|
|
47
|
+
if (!claudeSessionId) {
|
|
48
|
+
(logger ?? console).warn('[engram] before-agent-start: no sessionId or agentId available — skipping session init');
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
void client.initSession({
|
|
52
|
+
claudeSessionId,
|
|
53
|
+
project,
|
|
54
|
+
prompt: event.initialPrompt,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
(logger ?? console).warn(`[engram] before-agent-start: injected ${injectedIds.length} observations for project ${project}`);
|
|
58
|
+
// Build static instructions + dynamic session context
|
|
59
|
+
const staticInstructions = buildStaticInstructions(project);
|
|
60
|
+
const fullContext = staticInstructions + '\n\n' + context;
|
|
61
|
+
return { appendSystemContext: fullContext };
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
(logger ?? console).error('[engram] hook error:', err);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Build cacheable static instructions injected once per session.
|
|
69
|
+
* These describe available engram capabilities to the agent.
|
|
70
|
+
*/
|
|
71
|
+
function buildStaticInstructions(project) {
|
|
72
|
+
return [
|
|
73
|
+
'# Engram Persistent Memory',
|
|
74
|
+
'',
|
|
75
|
+
'You have access to persistent memory via engram. Available tools:',
|
|
76
|
+
'- `engram_search` — search prior observations, decisions, and patterns',
|
|
77
|
+
'- `engram_remember` — store new observations for future sessions',
|
|
78
|
+
'- `engram_decisions` — query architectural decisions',
|
|
79
|
+
'',
|
|
80
|
+
`Memory is scoped to project "${project}". Use \`engram_remember\` to store important insights, decisions, and discoveries.`,
|
|
81
|
+
].join('\n');
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=before-agent-start.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"before-agent-start.js","sourceRoot":"","sources":["../../src/hooks/before-agent-start.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AAuBH,wDAmEC;AAtFD,gDAAiD;AACjD,0DAAwD;AAQxD;;;;;;;;;GASG;AACI,KAAK,UAAU,sBAAsB,CAC1C,KAA4B,EAC5B,GAAsB,EACtB,MAAwB,EACxB,MAAoB,EACpB,MAAqB;IAErB,IAAI,CAAC;QACH,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE;YAAE,OAAO;QAElC,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC;QAClC,MAAM,QAAQ,GAAG,IAAA,6BAAe,EAAC,OAAO,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC;QAC5D,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,QAAQ,CAAC,SAAS,CAAC;QAErD,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAC5C,OAAO,EACP,GAAG,CAAC,YAAY,CACjB,CAAC;QAEF,IAAI,CAAC,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,QAAQ,CAAC,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC7F,OAAO;QACT,CAAC;QAED,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,GAAG,IAAA,4BAAa,EAC1D,QAAQ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GAGG;AACH,SAAS,uBAAuB,CAAC,OAAe;IAC9C,OAAO;QACL,4BAA4B;QAC5B,EAAE;QACF,mEAAmE;QACnE,wEAAwE;QACxE,kEAAkE;QAClE,sDAAsD;QACtD,EAAE;QACF,gCAAgC,OAAO,qFAAqF;KAC7H,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC"}
|
|
@@ -7,13 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { EngramRestClient } from '../client.js';
|
|
9
9
|
import type { PluginConfig } from '../config.js';
|
|
10
|
-
import type { BeforeCompactionEvent, PluginLogger } from '../types/openclaw.js';
|
|
10
|
+
import type { BeforeCompactionEvent, PluginHookContext, PluginLogger } from '../types/openclaw.js';
|
|
11
11
|
/**
|
|
12
12
|
* Handle the before_compaction hook.
|
|
13
13
|
*
|
|
14
14
|
* @param event - The before_compaction event from OpenClaw.
|
|
15
|
+
* @param ctx - The hook context containing agent identity fields.
|
|
15
16
|
* @param client - Shared engram REST client.
|
|
16
17
|
* @param config - Resolved plugin config.
|
|
17
18
|
*/
|
|
18
|
-
export declare function handleBeforeCompaction(event: BeforeCompactionEvent, client: EngramRestClient, config: PluginConfig, logger?: PluginLogger): void;
|
|
19
|
+
export declare function handleBeforeCompaction(event: BeforeCompactionEvent, ctx: PluginHookContext, client: EngramRestClient, config: PluginConfig, logger?: PluginLogger): void;
|
|
19
20
|
//# sourceMappingURL=before-compaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"before-compaction.d.ts","sourceRoot":"","sources":["../../src/hooks/before-compaction.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,KAAK,EAAE,qBAAqB,EAAuB,YAAY,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"before-compaction.d.ts","sourceRoot":"","sources":["../../src/hooks/before-compaction.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,KAAK,EAAE,qBAAqB,EAAuB,iBAAiB,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAOxH;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,qBAAqB,EAC5B,GAAG,EAAE,iBAAiB,EACtB,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,YAAY,EACpB,MAAM,CAAC,EAAE,YAAY,GACpB,IAAI,CAgCN"}
|
|
@@ -17,26 +17,28 @@ const CONTENT_MAX_CHARS = 6000;
|
|
|
17
17
|
* Handle the before_compaction hook.
|
|
18
18
|
*
|
|
19
19
|
* @param event - The before_compaction event from OpenClaw.
|
|
20
|
+
* @param ctx - The hook context containing agent identity fields.
|
|
20
21
|
* @param client - Shared engram REST client.
|
|
21
22
|
* @param config - Resolved plugin config.
|
|
22
23
|
*/
|
|
23
|
-
function handleBeforeCompaction(event, client, config, logger) {
|
|
24
|
+
function handleBeforeCompaction(event, ctx, client, config, logger) {
|
|
24
25
|
try {
|
|
25
26
|
if (!client.isAvailable())
|
|
26
27
|
return;
|
|
27
28
|
if (!config.autoExtract)
|
|
28
29
|
return;
|
|
29
|
-
const agentId =
|
|
30
|
-
const identity = (0, identity_js_1.resolveIdentity)(agentId,
|
|
30
|
+
const agentId = ctx.agentId ?? '';
|
|
31
|
+
const identity = (0, identity_js_1.resolveIdentity)(agentId, ctx.workspaceDir);
|
|
31
32
|
const project = config.project ?? identity.projectId;
|
|
32
33
|
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
33
34
|
const recent = messages.slice(-MAX_MESSAGES);
|
|
34
35
|
const content = serializeMessages(recent);
|
|
35
36
|
if (!content)
|
|
36
37
|
return;
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
const stripped = stripEngramContext(content);
|
|
39
|
+
const truncated = stripped.length > CONTENT_MAX_CHARS
|
|
40
|
+
? stripped.slice(0, CONTENT_MAX_CHARS)
|
|
41
|
+
: stripped;
|
|
40
42
|
// Fire-and-forget — do not await
|
|
41
43
|
void client.backfillSession({
|
|
42
44
|
session_id: agentId,
|
|
@@ -57,4 +59,7 @@ function serializeMessages(messages) {
|
|
|
57
59
|
.map((m) => `[${m.role}]: ${m.content}`)
|
|
58
60
|
.join('\n\n');
|
|
59
61
|
}
|
|
62
|
+
function stripEngramContext(text) {
|
|
63
|
+
return text.replace(/<engram-context>[\s\S]*?<\/engram-context>/g, '');
|
|
64
|
+
}
|
|
60
65
|
//# sourceMappingURL=before-compaction.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"before-compaction.js","sourceRoot":"","sources":["../../src/hooks/before-compaction.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;
|
|
1
|
+
{"version":3,"file":"before-compaction.js","sourceRoot":"","sources":["../../src/hooks/before-compaction.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAoBH,wDAsCC;AAtDD,gDAAiD;AAGjD,kEAAkE;AAClE,MAAM,YAAY,GAAG,EAAE,CAAC;AACxB,8EAA8E;AAC9E,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAE/B;;;;;;;GAOG;AACH,SAAgB,sBAAsB,CACpC,KAA4B,EAC5B,GAAsB,EACtB,MAAwB,EACxB,MAAoB,EACpB,MAAqB;IAErB,IAAI,CAAC;QACH,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE;YAAE,OAAO;QAClC,IAAI,CAAC,MAAM,CAAC,WAAW;YAAE,OAAO;QAEhC,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC;QAClC,MAAM,QAAQ,GAAG,IAAA,6BAAe,EAAC,OAAO,EAAE,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,IAAI,CAAC,MAAM,CAAC,CAAC;AAClB,CAAC;AAED,SAAS,kBAAkB,CAAC,IAAY;IACtC,OAAO,IAAI,CAAC,OAAO,CAAC,6CAA6C,EAAE,EAAE,CAAC,CAAC;AACzE,CAAC"}
|
|
@@ -7,14 +7,15 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { EngramRestClient } from '../client.js';
|
|
9
9
|
import type { PluginConfig } from '../config.js';
|
|
10
|
-
import type { BeforePromptBuildEvent, PromptBuildResult, PluginLogger } from '../types/openclaw.js';
|
|
10
|
+
import type { BeforePromptBuildEvent, PromptBuildResult, PluginHookContext, PluginLogger } from '../types/openclaw.js';
|
|
11
11
|
/**
|
|
12
12
|
* Handle the before_prompt_build hook.
|
|
13
13
|
*
|
|
14
14
|
* @param event - The before_prompt_build event from OpenClaw.
|
|
15
|
+
* @param ctx - The hook context containing agent identity fields.
|
|
15
16
|
* @param client - Shared engram REST client.
|
|
16
17
|
* @param config - Resolved plugin config.
|
|
17
18
|
* @returns Context to prepend, or void if nothing to inject.
|
|
18
19
|
*/
|
|
19
|
-
export declare function handleBeforePromptBuild(event: BeforePromptBuildEvent, client: EngramRestClient, config: PluginConfig, logger?: PluginLogger): Promise<PromptBuildResult | void>;
|
|
20
|
+
export declare function handleBeforePromptBuild(event: BeforePromptBuildEvent, ctx: PluginHookContext, client: EngramRestClient, config: PluginConfig, logger?: PluginLogger): Promise<PromptBuildResult | void>;
|
|
20
21
|
//# sourceMappingURL=before-prompt-build.d.ts.map
|