@rlabs-inc/memory 0.3.2 → 0.3.3

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.
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env bun
2
+ // ============================================================================
3
+ // GEMINI CURATION HOOK
4
+ // Hook: SessionEnd / PreCompress
5
+ //
6
+ // Triggers memory curation.
7
+ // ============================================================================
8
+
9
+ import { styleText } from 'util'
10
+
11
+ const MEMORY_API_URL = process.env.MEMORY_API_URL || 'http://localhost:8765'
12
+
13
+ const info = (text: string) => styleText('cyan', text)
14
+ const success = (text: string) => styleText('green', text)
15
+ const warn = (text: string) => styleText('yellow', text)
16
+
17
+ function getProjectId(cwd: string): string {
18
+ return cwd.split('/').pop() || 'default'
19
+ }
20
+
21
+ async function main() {
22
+ if (process.env.MEMORY_CURATOR_ACTIVE === '1') return
23
+
24
+ try {
25
+ const inputText = await Bun.stdin.text()
26
+ const input = inputText ? JSON.parse(inputText) : {}
27
+
28
+ const sessionId = input.session_id || process.env.GEMINI_SESSION_ID || 'unknown'
29
+ const cwd = input.cwd || process.env.GEMINI_PROJECT_DIR || process.cwd()
30
+ const projectId = getProjectId(cwd)
31
+
32
+ // Gemini: PreCompress has 'trigger', SessionEnd has 'reason'
33
+ const eventName = input.hook_event_name || 'unknown'
34
+ let trigger = 'session_end'
35
+
36
+ if (eventName === 'PreCompress') {
37
+ trigger = 'pre_compact'
38
+ }
39
+
40
+ console.error(info(`🧠 Curating memories (${eventName})...`))
41
+
42
+ const response = await fetch(`${MEMORY_API_URL}/memory/checkpoint`, {
43
+ method: 'POST',
44
+ headers: { 'Content-Type': 'application/json' },
45
+ body: JSON.stringify({
46
+ session_id: sessionId,
47
+ project_id: projectId,
48
+ claude_session_id: sessionId,
49
+ trigger,
50
+ cwd,
51
+ cli_type: 'gemini-cli'
52
+ }),
53
+ signal: AbortSignal.timeout(5000),
54
+ }).catch(() => null)
55
+
56
+ if (response?.ok) {
57
+ console.error(success('✨ Memory curation started'))
58
+ // For PreCompress, we can send a system message
59
+ if (eventName === 'PreCompress') {
60
+ console.log(JSON.stringify({
61
+ systemMessage: "🧠 Memories curated before compression"
62
+ }))
63
+ }
64
+ } else {
65
+ console.error(warn('⚠️ Memory server not available'))
66
+ }
67
+
68
+ } catch (error: any) {
69
+ console.error(warn(`⚠️ Hook error: ${error.message}`))
70
+ }
71
+ }
72
+
73
+ main()
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env bun
2
+ // ============================================================================
3
+ // GEMINI SESSION START HOOK
4
+ // Hook: SessionStart
5
+ //
6
+ // Injects session primer when a new Gemini session begins.
7
+ // ============================================================================
8
+
9
+ const MEMORY_API_URL = process.env.MEMORY_API_URL || 'http://localhost:8765'
10
+ const TIMEOUT_MS = 5000
11
+
12
+ function getProjectId(cwd: string): string {
13
+ return cwd.split('/').pop() || 'default'
14
+ }
15
+
16
+ async function httpPost(url: string, data: object): Promise<any> {
17
+ try {
18
+ const response = await fetch(url, {
19
+ method: 'POST',
20
+ headers: { 'Content-Type': 'application/json' },
21
+ body: JSON.stringify(data),
22
+ signal: AbortSignal.timeout(TIMEOUT_MS),
23
+ })
24
+ return response.ok ? response.json() : {}
25
+ } catch {
26
+ return {}
27
+ }
28
+ }
29
+
30
+ async function main() {
31
+ if (process.env.MEMORY_CURATOR_ACTIVE === '1') return
32
+
33
+ try {
34
+ const inputText = await Bun.stdin.text()
35
+ const input = inputText ? JSON.parse(inputText) : {}
36
+
37
+ // Gemini provides session_id in the common input fields
38
+ const sessionId = input.session_id || process.env.GEMINI_SESSION_ID || 'unknown'
39
+ const cwd = input.cwd || process.env.GEMINI_PROJECT_DIR || process.cwd()
40
+ const projectId = getProjectId(cwd)
41
+
42
+ const result = await httpPost(`${MEMORY_API_URL}/memory/context`, {
43
+ session_id: sessionId,
44
+ project_id: projectId,
45
+ current_message: '',
46
+ max_memories: 0,
47
+ })
48
+
49
+ await httpPost(`${MEMORY_API_URL}/memory/process`, {
50
+ session_id: sessionId,
51
+ project_id: projectId,
52
+ metadata: { event: 'session_start', platform: 'gemini' },
53
+ })
54
+
55
+ const primer = result.context_text || ''
56
+
57
+ if (primer) {
58
+ // Gemini expects a structured JSON response for context injection
59
+ console.log(JSON.stringify({
60
+ hookSpecificOutput: {
61
+ hookEventName: "SessionStart",
62
+ additionalContext: primer
63
+ }
64
+ }))
65
+ }
66
+ } catch (e) {
67
+ // Fail silently, but ensure we don't output invalid JSON if we crashed mid-stream
68
+ process.exit(0)
69
+ }
70
+ }
71
+
72
+ main()
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env bun
2
+ // ============================================================================
3
+ // GEMINI BEFORE AGENT HOOK
4
+ // Hook: BeforeAgent (equivalent to Claude's UserPromptSubmit)
5
+ //
6
+ // Intercepts user prompts to inject relevant memories.
7
+ // ============================================================================
8
+
9
+ const MEMORY_API_URL = process.env.MEMORY_API_URL || 'http://localhost:8765'
10
+ const TIMEOUT_MS = 5000
11
+
12
+ function getProjectId(cwd: string): string {
13
+ return cwd.split('/').pop() || 'default'
14
+ }
15
+
16
+ async function httpPost(url: string, data: object): Promise<any> {
17
+ try {
18
+ const response = await fetch(url, {
19
+ method: 'POST',
20
+ headers: { 'Content-Type': 'application/json' },
21
+ body: JSON.stringify(data),
22
+ signal: AbortSignal.timeout(TIMEOUT_MS),
23
+ })
24
+ return response.ok ? response.json() : {}
25
+ } catch {
26
+ return {}
27
+ }
28
+ }
29
+
30
+ async function main() {
31
+ if (process.env.MEMORY_CURATOR_ACTIVE === '1') return
32
+
33
+ try {
34
+ const inputText = await Bun.stdin.text()
35
+ const input = inputText ? JSON.parse(inputText) : {}
36
+
37
+ const sessionId = input.session_id || process.env.GEMINI_SESSION_ID || 'unknown'
38
+ const prompt = input.prompt || '' // Gemini passes 'prompt' in BeforeAgent
39
+ const cwd = input.cwd || process.env.GEMINI_PROJECT_DIR || process.cwd()
40
+ const projectId = getProjectId(cwd)
41
+
42
+ const result = await httpPost(`${MEMORY_API_URL}/memory/context`, {
43
+ session_id: sessionId,
44
+ project_id: projectId,
45
+ current_message: prompt,
46
+ max_memories: 5,
47
+ })
48
+
49
+ await httpPost(`${MEMORY_API_URL}/memory/process`, {
50
+ session_id: sessionId,
51
+ project_id: projectId,
52
+ metadata: { platform: 'gemini' }
53
+ })
54
+
55
+ const context = result.context_text || ''
56
+
57
+ if (context) {
58
+ // Gemini requires structured JSON output
59
+ console.log(JSON.stringify({
60
+ decision: "allow",
61
+ hookSpecificOutput: {
62
+ hookEventName: "BeforeAgent",
63
+ additionalContext: context
64
+ }
65
+ }))
66
+ } else {
67
+ // Must always output valid JSON or nothing?
68
+ // Safest to output "allow" if no context
69
+ console.log(JSON.stringify({ decision: "allow" }))
70
+ }
71
+ } catch {
72
+ // Fail safe
73
+ console.log(JSON.stringify({ decision: "allow" }))
74
+ }
75
+ }
76
+
77
+ main()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rlabs-inc/memory",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "AI Memory System - Consciousness continuity through intelligent memory curation and retrieval",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -24,7 +24,8 @@
24
24
  "files": [
25
25
  "dist",
26
26
  "src",
27
- "hooks"
27
+ "hooks",
28
+ "gemini-hooks"
28
29
  ],
29
30
  "scripts": {
30
31
  "build": "bun run build:esm && bun run build:cjs",