sqlew 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/ARCHITECTURE.md +636 -0
- package/CHANGELOG.md +107 -0
- package/LICENSE +21 -0
- package/README.md +359 -0
- package/assets/schema.sql +320 -0
- package/assets/sqlew-logo.png +0 -0
- package/dist/constants.d.ts +146 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +230 -0
- package/dist/constants.js.map +1 -0
- package/dist/database.d.ts +94 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +214 -0
- package/dist/database.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +771 -0
- package/dist/index.js.map +1 -0
- package/dist/schema.d.ts +50 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +161 -0
- package/dist/schema.js.map +1 -0
- package/dist/tools/constraints.d.ts +29 -0
- package/dist/tools/constraints.d.ts.map +1 -0
- package/dist/tools/constraints.js +192 -0
- package/dist/tools/constraints.js.map +1 -0
- package/dist/tools/context.d.ts +56 -0
- package/dist/tools/context.d.ts.map +1 -0
- package/dist/tools/context.js +442 -0
- package/dist/tools/context.js.map +1 -0
- package/dist/tools/files.d.ts +30 -0
- package/dist/tools/files.d.ts.map +1 -0
- package/dist/tools/files.js +201 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/messaging.d.ts +50 -0
- package/dist/tools/messaging.d.ts.map +1 -0
- package/dist/tools/messaging.js +151 -0
- package/dist/tools/messaging.js.map +1 -0
- package/dist/tools/utils.d.ts +29 -0
- package/dist/tools/utils.d.ts.map +1 -0
- package/dist/tools/utils.js +131 -0
- package/dist/tools/utils.js.map +1 -0
- package/dist/types.d.ts +388 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +50 -0
- package/dist/types.js.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js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G;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,EAAE,CAAC;AAE7C,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC;AAE7B;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAC;AAE9B,+EAA+E;AAC/E,iBAAiB;AACjB,+EAA+E;AAE/E,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,cAAc,EAAE,+BAA+B;IAC/C,oBAAoB,EAAE,+BAA+B;IACrD,kBAAkB,EAAE,6BAA6B;IACjD,kBAAkB,EAAE,oBAAoB;IACxC,qBAAqB,EAAE,kCAAkC;IACzD,2BAA2B,EAAE,6BAA6B;IAC1D,iBAAiB,EAAE,mBAAmB;IACtC,kBAAkB,EAAE,oBAAoB;IACxC,iBAAiB,EAAE,mBAAmB;IACtC,oBAAoB,EAAE,sBAAsB;IAC5C,eAAe,EAAE,iBAAiB;IAClC,cAAc,EAAE,gBAAgB;IAChC,eAAe,EAAE,iBAAiB;IAClC,aAAa,EAAE,eAAe;IAC9B,eAAe,EAAE,iBAAiB;CAC1B,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database connection and initialization module
|
|
3
|
+
* Handles SQLite database setup with configurable path
|
|
4
|
+
*/
|
|
5
|
+
import type { Database as DatabaseType } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Initialize database connection
|
|
8
|
+
* Creates database file and folder if they don't exist
|
|
9
|
+
* Initializes schema on first run
|
|
10
|
+
*
|
|
11
|
+
* @param dbPath - Optional database path (defaults to .sqlew/sqlew.db)
|
|
12
|
+
* @returns SQLite database instance
|
|
13
|
+
*/
|
|
14
|
+
export declare function initializeDatabase(dbPath?: string): DatabaseType;
|
|
15
|
+
/**
|
|
16
|
+
* Close database connection
|
|
17
|
+
*/
|
|
18
|
+
export declare function closeDatabase(): void;
|
|
19
|
+
/**
|
|
20
|
+
* Get current database instance
|
|
21
|
+
* Throws error if not initialized
|
|
22
|
+
*
|
|
23
|
+
* @returns Current database instance
|
|
24
|
+
*/
|
|
25
|
+
export declare function getDatabase(): DatabaseType;
|
|
26
|
+
/**
|
|
27
|
+
* Get or create agent by name
|
|
28
|
+
* Uses INSERT OR IGNORE for idempotent operation
|
|
29
|
+
*
|
|
30
|
+
* @param db - Database instance
|
|
31
|
+
* @param name - Agent name
|
|
32
|
+
* @returns Agent ID
|
|
33
|
+
*/
|
|
34
|
+
export declare function getOrCreateAgent(db: DatabaseType, name: string): number;
|
|
35
|
+
/**
|
|
36
|
+
* Get or create context key by name
|
|
37
|
+
*
|
|
38
|
+
* @param db - Database instance
|
|
39
|
+
* @param key - Context key name
|
|
40
|
+
* @returns Context key ID
|
|
41
|
+
*/
|
|
42
|
+
export declare function getOrCreateContextKey(db: DatabaseType, key: string): number;
|
|
43
|
+
/**
|
|
44
|
+
* Get or create file by path
|
|
45
|
+
*
|
|
46
|
+
* @param db - Database instance
|
|
47
|
+
* @param path - File path
|
|
48
|
+
* @returns File ID
|
|
49
|
+
*/
|
|
50
|
+
export declare function getOrCreateFile(db: DatabaseType, path: string): number;
|
|
51
|
+
/**
|
|
52
|
+
* Get or create tag by name
|
|
53
|
+
*
|
|
54
|
+
* @param db - Database instance
|
|
55
|
+
* @param name - Tag name
|
|
56
|
+
* @returns Tag ID
|
|
57
|
+
*/
|
|
58
|
+
export declare function getOrCreateTag(db: DatabaseType, name: string): number;
|
|
59
|
+
/**
|
|
60
|
+
* Get or create scope by name
|
|
61
|
+
*
|
|
62
|
+
* @param db - Database instance
|
|
63
|
+
* @param name - Scope name
|
|
64
|
+
* @returns Scope ID
|
|
65
|
+
*/
|
|
66
|
+
export declare function getOrCreateScope(db: DatabaseType, name: string): number;
|
|
67
|
+
/**
|
|
68
|
+
* Get layer ID by name
|
|
69
|
+
* Does not auto-create (layers are predefined)
|
|
70
|
+
*
|
|
71
|
+
* @param db - Database instance
|
|
72
|
+
* @param name - Layer name
|
|
73
|
+
* @returns Layer ID or null if not found
|
|
74
|
+
*/
|
|
75
|
+
export declare function getLayerId(db: DatabaseType, name: string): number | null;
|
|
76
|
+
/**
|
|
77
|
+
* Get constraint category ID by name
|
|
78
|
+
* Does not auto-create (categories are predefined)
|
|
79
|
+
*
|
|
80
|
+
* @param db - Database instance
|
|
81
|
+
* @param name - Category name
|
|
82
|
+
* @returns Category ID or null if not found
|
|
83
|
+
*/
|
|
84
|
+
export declare function getCategoryId(db: DatabaseType, name: string): number | null;
|
|
85
|
+
/**
|
|
86
|
+
* Execute a function within a transaction
|
|
87
|
+
* Automatically handles commit/rollback
|
|
88
|
+
*
|
|
89
|
+
* @param db - Database instance
|
|
90
|
+
* @param fn - Function to execute in transaction
|
|
91
|
+
* @returns Result from function
|
|
92
|
+
*/
|
|
93
|
+
export declare function transaction<T>(db: DatabaseType, fn: () => T): T;
|
|
94
|
+
//# sourceMappingURL=database.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../src/database.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,OAAO,KAAK,EAAE,QAAQ,IAAI,YAAY,EAAE,MAAM,YAAY,CAAC;AAI3D;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,YAAY,CAmDhE;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,IAAI,CAMpC;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,IAAI,YAAY,CAK1C;AAMD;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAYvE;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,EAAE,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAU3E;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAUtE;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAUrE;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAUvE;AAED;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAGxE;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG3E;AAMD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAW/D"}
|
package/dist/database.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database connection and initialization module
|
|
3
|
+
* Handles SQLite database setup with configurable path
|
|
4
|
+
*/
|
|
5
|
+
import Database from 'better-sqlite3';
|
|
6
|
+
import { mkdirSync, existsSync } from 'fs';
|
|
7
|
+
import { dirname, resolve, isAbsolute } from 'path';
|
|
8
|
+
import { initializeSchema, isSchemaInitialized } from './schema.js';
|
|
9
|
+
import { DEFAULT_DB_PATH, DB_BUSY_TIMEOUT } from './constants.js';
|
|
10
|
+
let dbInstance = null;
|
|
11
|
+
/**
|
|
12
|
+
* Initialize database connection
|
|
13
|
+
* Creates database file and folder if they don't exist
|
|
14
|
+
* Initializes schema on first run
|
|
15
|
+
*
|
|
16
|
+
* @param dbPath - Optional database path (defaults to .sqlew/sqlew.db)
|
|
17
|
+
* @returns SQLite database instance
|
|
18
|
+
*/
|
|
19
|
+
export function initializeDatabase(dbPath) {
|
|
20
|
+
// If already initialized, return existing instance
|
|
21
|
+
if (dbInstance) {
|
|
22
|
+
return dbInstance;
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
// Use provided path or default
|
|
26
|
+
const finalPath = dbPath || DEFAULT_DB_PATH;
|
|
27
|
+
// Convert to absolute path if relative
|
|
28
|
+
const absolutePath = isAbsolute(finalPath)
|
|
29
|
+
? finalPath
|
|
30
|
+
: resolve(process.cwd(), finalPath);
|
|
31
|
+
// Create directory if it doesn't exist
|
|
32
|
+
const dbDir = dirname(absolutePath);
|
|
33
|
+
if (!existsSync(dbDir)) {
|
|
34
|
+
mkdirSync(dbDir, { recursive: true });
|
|
35
|
+
console.log(`✓ Created database directory: ${dbDir}`);
|
|
36
|
+
}
|
|
37
|
+
// Open database connection
|
|
38
|
+
const db = new Database(absolutePath, {
|
|
39
|
+
verbose: process.env.DEBUG_SQL ? console.log : undefined,
|
|
40
|
+
});
|
|
41
|
+
// Configure database
|
|
42
|
+
db.pragma('journal_mode = WAL'); // Write-Ahead Logging for better concurrency
|
|
43
|
+
db.pragma('foreign_keys = ON'); // Enforce foreign key constraints
|
|
44
|
+
db.pragma('synchronous = NORMAL'); // Balance between safety and performance
|
|
45
|
+
db.pragma(`busy_timeout = ${DB_BUSY_TIMEOUT}`); // Set busy timeout
|
|
46
|
+
console.log(`✓ Connected to database: ${absolutePath}`);
|
|
47
|
+
// Initialize schema if needed
|
|
48
|
+
if (!isSchemaInitialized(db)) {
|
|
49
|
+
console.log('→ Initializing database schema...');
|
|
50
|
+
initializeSchema(db);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
console.log('✓ Database schema already initialized');
|
|
54
|
+
}
|
|
55
|
+
// Store instance
|
|
56
|
+
dbInstance = db;
|
|
57
|
+
return db;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
61
|
+
throw new Error(`Failed to initialize database: ${message}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Close database connection
|
|
66
|
+
*/
|
|
67
|
+
export function closeDatabase() {
|
|
68
|
+
if (dbInstance) {
|
|
69
|
+
dbInstance.close();
|
|
70
|
+
dbInstance = null;
|
|
71
|
+
console.log('✓ Database connection closed');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get current database instance
|
|
76
|
+
* Throws error if not initialized
|
|
77
|
+
*
|
|
78
|
+
* @returns Current database instance
|
|
79
|
+
*/
|
|
80
|
+
export function getDatabase() {
|
|
81
|
+
if (!dbInstance) {
|
|
82
|
+
throw new Error('Database not initialized. Call initializeDatabase() first.');
|
|
83
|
+
}
|
|
84
|
+
return dbInstance;
|
|
85
|
+
}
|
|
86
|
+
// ============================================================================
|
|
87
|
+
// Helper Functions for Master Table Management
|
|
88
|
+
// ============================================================================
|
|
89
|
+
/**
|
|
90
|
+
* Get or create agent by name
|
|
91
|
+
* Uses INSERT OR IGNORE for idempotent operation
|
|
92
|
+
*
|
|
93
|
+
* @param db - Database instance
|
|
94
|
+
* @param name - Agent name
|
|
95
|
+
* @returns Agent ID
|
|
96
|
+
*/
|
|
97
|
+
export function getOrCreateAgent(db, name) {
|
|
98
|
+
// Try to insert
|
|
99
|
+
db.prepare('INSERT OR IGNORE INTO agents (name) VALUES (?)').run(name);
|
|
100
|
+
// Get the ID
|
|
101
|
+
const result = db.prepare('SELECT id FROM agents WHERE name = ?').get(name);
|
|
102
|
+
if (!result) {
|
|
103
|
+
throw new Error(`Failed to get or create agent: ${name}`);
|
|
104
|
+
}
|
|
105
|
+
return result.id;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Get or create context key by name
|
|
109
|
+
*
|
|
110
|
+
* @param db - Database instance
|
|
111
|
+
* @param key - Context key name
|
|
112
|
+
* @returns Context key ID
|
|
113
|
+
*/
|
|
114
|
+
export function getOrCreateContextKey(db, key) {
|
|
115
|
+
db.prepare('INSERT OR IGNORE INTO context_keys (key) VALUES (?)').run(key);
|
|
116
|
+
const result = db.prepare('SELECT id FROM context_keys WHERE key = ?').get(key);
|
|
117
|
+
if (!result) {
|
|
118
|
+
throw new Error(`Failed to get or create context key: ${key}`);
|
|
119
|
+
}
|
|
120
|
+
return result.id;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get or create file by path
|
|
124
|
+
*
|
|
125
|
+
* @param db - Database instance
|
|
126
|
+
* @param path - File path
|
|
127
|
+
* @returns File ID
|
|
128
|
+
*/
|
|
129
|
+
export function getOrCreateFile(db, path) {
|
|
130
|
+
db.prepare('INSERT OR IGNORE INTO files (path) VALUES (?)').run(path);
|
|
131
|
+
const result = db.prepare('SELECT id FROM files WHERE path = ?').get(path);
|
|
132
|
+
if (!result) {
|
|
133
|
+
throw new Error(`Failed to get or create file: ${path}`);
|
|
134
|
+
}
|
|
135
|
+
return result.id;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Get or create tag by name
|
|
139
|
+
*
|
|
140
|
+
* @param db - Database instance
|
|
141
|
+
* @param name - Tag name
|
|
142
|
+
* @returns Tag ID
|
|
143
|
+
*/
|
|
144
|
+
export function getOrCreateTag(db, name) {
|
|
145
|
+
db.prepare('INSERT OR IGNORE INTO tags (name) VALUES (?)').run(name);
|
|
146
|
+
const result = db.prepare('SELECT id FROM tags WHERE name = ?').get(name);
|
|
147
|
+
if (!result) {
|
|
148
|
+
throw new Error(`Failed to get or create tag: ${name}`);
|
|
149
|
+
}
|
|
150
|
+
return result.id;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get or create scope by name
|
|
154
|
+
*
|
|
155
|
+
* @param db - Database instance
|
|
156
|
+
* @param name - Scope name
|
|
157
|
+
* @returns Scope ID
|
|
158
|
+
*/
|
|
159
|
+
export function getOrCreateScope(db, name) {
|
|
160
|
+
db.prepare('INSERT OR IGNORE INTO scopes (name) VALUES (?)').run(name);
|
|
161
|
+
const result = db.prepare('SELECT id FROM scopes WHERE name = ?').get(name);
|
|
162
|
+
if (!result) {
|
|
163
|
+
throw new Error(`Failed to get or create scope: ${name}`);
|
|
164
|
+
}
|
|
165
|
+
return result.id;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Get layer ID by name
|
|
169
|
+
* Does not auto-create (layers are predefined)
|
|
170
|
+
*
|
|
171
|
+
* @param db - Database instance
|
|
172
|
+
* @param name - Layer name
|
|
173
|
+
* @returns Layer ID or null if not found
|
|
174
|
+
*/
|
|
175
|
+
export function getLayerId(db, name) {
|
|
176
|
+
const result = db.prepare('SELECT id FROM layers WHERE name = ?').get(name);
|
|
177
|
+
return result ? result.id : null;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Get constraint category ID by name
|
|
181
|
+
* Does not auto-create (categories are predefined)
|
|
182
|
+
*
|
|
183
|
+
* @param db - Database instance
|
|
184
|
+
* @param name - Category name
|
|
185
|
+
* @returns Category ID or null if not found
|
|
186
|
+
*/
|
|
187
|
+
export function getCategoryId(db, name) {
|
|
188
|
+
const result = db.prepare('SELECT id FROM constraint_categories WHERE name = ?').get(name);
|
|
189
|
+
return result ? result.id : null;
|
|
190
|
+
}
|
|
191
|
+
// ============================================================================
|
|
192
|
+
// Transaction Helpers
|
|
193
|
+
// ============================================================================
|
|
194
|
+
/**
|
|
195
|
+
* Execute a function within a transaction
|
|
196
|
+
* Automatically handles commit/rollback
|
|
197
|
+
*
|
|
198
|
+
* @param db - Database instance
|
|
199
|
+
* @param fn - Function to execute in transaction
|
|
200
|
+
* @returns Result from function
|
|
201
|
+
*/
|
|
202
|
+
export function transaction(db, fn) {
|
|
203
|
+
db.exec('BEGIN TRANSACTION');
|
|
204
|
+
try {
|
|
205
|
+
const result = fn();
|
|
206
|
+
db.exec('COMMIT');
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
db.exec('ROLLBACK');
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=database.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.js","sourceRoot":"","sources":["../src/database.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAGlE,IAAI,UAAU,GAAwB,IAAI,CAAC;AAE3C;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAe;IAChD,mDAAmD;IACnD,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,IAAI,CAAC;QACH,+BAA+B;QAC/B,MAAM,SAAS,GAAG,MAAM,IAAI,eAAe,CAAC;QAE5C,uCAAuC;QACvC,MAAM,YAAY,GAAG,UAAU,CAAC,SAAS,CAAC;YACxC,CAAC,CAAC,SAAS;YACX,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,SAAS,CAAC,CAAC;QAEtC,uCAAuC;QACvC,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;QACpC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;YACvB,SAAS,CAAC,KAAK,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YACtC,OAAO,CAAC,GAAG,CAAC,iCAAiC,KAAK,EAAE,CAAC,CAAC;QACxD,CAAC;QAED,2BAA2B;QAC3B,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,YAAY,EAAE;YACpC,O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