forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
140 lines (120 loc) · 3.83 KB
/
Copy pathdb.py
File metadata and controls
140 lines (120 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import sqlite3
from contextlib import contextmanager
import json
# Database setup
DB_PATH = os.getenv("DB_PATH", "plugins/composio/data/composio.db")
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
# Context manager for database connections
@contextmanager
def get_db_connection():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
try:
yield conn
finally:
conn.close()
# Create the necessary tables
def create_db_tables():
with get_db_connection() as conn:
cursor = conn.cursor()
# Table for user credentials (Notion tokens)
cursor.execute(
'''
CREATE TABLE IF NOT EXISTS user_credentials (
uid TEXT PRIMARY KEY,
notion_access_token TEXT,
notion_workspace_id TEXT,
notion_workspace_name TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
'''
)
# Table for extracted memories/facts
cursor.execute(
'''
CREATE TABLE IF NOT EXISTS extracted_memories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
uid TEXT,
source TEXT,
memory_text TEXT,
status TEXT DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (uid) REFERENCES user_credentials(uid)
)
'''
)
conn.commit()
# Functions for Notion credentials management
def store_notion_credentials(uid, access_token, workspace_id, workspace_name):
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute(
'''
INSERT OR REPLACE INTO user_credentials (uid, notion_access_token, notion_workspace_id, notion_workspace_name, updated_at)
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
''',
(uid, access_token, workspace_id, workspace_name),
)
conn.commit()
def get_notion_credentials(uid):
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute('SELECT * FROM user_credentials WHERE uid = ?', (uid,))
result = cursor.fetchone()
return dict(result) if result else None
# Functions for memory management
def store_memory(uid, source, memory_text):
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute(
'''
INSERT INTO extracted_memories (uid, source, memory_text)
VALUES (?, ?, ?)
''',
(uid, source, memory_text),
)
conn.commit()
return cursor.lastrowid
def update_memory_status(memory_id, status):
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute(
'''
UPDATE extracted_memories
SET status = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
''',
(status, memory_id),
)
conn.commit()
def get_pending_memories(uid, limit=100):
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute(
'''
SELECT * FROM extracted_memories
WHERE uid = ? AND status = 'pending'
ORDER BY created_at ASC
LIMIT ?
''',
(uid, limit),
)
results = cursor.fetchall()
return [dict(row) for row in results]
def get_all_memories(uid, limit=100, offset=0):
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute(
'''
SELECT * FROM extracted_memories
WHERE uid = ?
ORDER BY created_at DESC
LIMIT ? OFFSET ?
''',
(uid, limit, offset),
)
results = cursor.fetchall()
return [dict(row) for row in results]