@codemcp/workflows-core 3.1.16
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/.turbo/turbo-build.log +4 -0
- package/LICENSE +674 -0
- package/dist/config-manager.d.ts +24 -0
- package/dist/config-manager.js +68 -0
- package/dist/config-manager.js.map +1 -0
- package/dist/conversation-manager.d.ts +97 -0
- package/dist/conversation-manager.js +367 -0
- package/dist/conversation-manager.js.map +1 -0
- package/dist/database.d.ts +73 -0
- package/dist/database.js +500 -0
- package/dist/database.js.map +1 -0
- package/dist/file-detection-manager.d.ts +53 -0
- package/dist/file-detection-manager.js +221 -0
- package/dist/file-detection-manager.js.map +1 -0
- package/dist/git-manager.d.ts +14 -0
- package/dist/git-manager.js +59 -0
- package/dist/git-manager.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/instruction-generator.d.ts +69 -0
- package/dist/instruction-generator.js +133 -0
- package/dist/instruction-generator.js.map +1 -0
- package/dist/interaction-logger.d.ts +37 -0
- package/dist/interaction-logger.js +87 -0
- package/dist/interaction-logger.js.map +1 -0
- package/dist/logger.d.ts +64 -0
- package/dist/logger.js +283 -0
- package/dist/logger.js.map +1 -0
- package/dist/path-validation-utils.d.ts +51 -0
- package/dist/path-validation-utils.js +202 -0
- package/dist/path-validation-utils.js.map +1 -0
- package/dist/plan-manager.d.ts +65 -0
- package/dist/plan-manager.js +256 -0
- package/dist/plan-manager.js.map +1 -0
- package/dist/project-docs-manager.d.ts +119 -0
- package/dist/project-docs-manager.js +357 -0
- package/dist/project-docs-manager.js.map +1 -0
- package/dist/state-machine-loader.d.ts +60 -0
- package/dist/state-machine-loader.js +235 -0
- package/dist/state-machine-loader.js.map +1 -0
- package/dist/state-machine-types.d.ts +58 -0
- package/dist/state-machine-types.js +7 -0
- package/dist/state-machine-types.js.map +1 -0
- package/dist/state-machine.d.ts +52 -0
- package/dist/state-machine.js +256 -0
- package/dist/state-machine.js.map +1 -0
- package/dist/system-prompt-generator.d.ts +17 -0
- package/dist/system-prompt-generator.js +113 -0
- package/dist/system-prompt-generator.js.map +1 -0
- package/dist/template-manager.d.ts +61 -0
- package/dist/template-manager.js +229 -0
- package/dist/template-manager.js.map +1 -0
- package/dist/transition-engine.d.ts +70 -0
- package/dist/transition-engine.js +240 -0
- package/dist/transition-engine.js.map +1 -0
- package/dist/types.d.ts +56 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/workflow-manager.d.ts +89 -0
- package/dist/workflow-manager.js +466 -0
- package/dist/workflow-manager.js.map +1 -0
- package/package.json +27 -0
- package/src/config-manager.ts +96 -0
- package/src/conversation-manager.ts +492 -0
- package/src/database.ts +685 -0
- package/src/file-detection-manager.ts +302 -0
- package/src/git-manager.ts +64 -0
- package/src/index.ts +28 -0
- package/src/instruction-generator.ts +210 -0
- package/src/interaction-logger.ts +109 -0
- package/src/logger.ts +353 -0
- package/src/path-validation-utils.ts +261 -0
- package/src/plan-manager.ts +323 -0
- package/src/project-docs-manager.ts +522 -0
- package/src/state-machine-loader.ts +308 -0
- package/src/state-machine-types.ts +72 -0
- package/src/state-machine.ts +370 -0
- package/src/system-prompt-generator.ts +122 -0
- package/src/template-manager.ts +321 -0
- package/src/transition-engine.ts +386 -0
- package/src/types.ts +60 -0
- package/src/workflow-manager.ts +601 -0
- package/test/unit/conversation-manager.test.ts +179 -0
- package/test/unit/custom-workflow-loading.test.ts +174 -0
- package/test/unit/directory-linking-and-extensions.test.ts +338 -0
- package/test/unit/file-linking-integration.test.ts +256 -0
- package/test/unit/git-commit-integration.test.ts +91 -0
- package/test/unit/git-manager.test.ts +86 -0
- package/test/unit/install-workflow.test.ts +138 -0
- package/test/unit/instruction-generator.test.ts +247 -0
- package/test/unit/list-workflows-filtering.test.ts +68 -0
- package/test/unit/none-template-functionality.test.ts +224 -0
- package/test/unit/project-docs-manager.test.ts +337 -0
- package/test/unit/state-machine-loader.test.ts +234 -0
- package/test/unit/template-manager.test.ts +217 -0
- package/test/unit/validate-workflow-name.test.ts +150 -0
- package/test/unit/workflow-domain-filtering.test.ts +75 -0
- package/test/unit/workflow-enum-generation.test.ts +92 -0
- package/test/unit/workflow-manager-enhanced-path-resolution.test.ts +369 -0
- package/test/unit/workflow-manager-path-resolution.test.ts +150 -0
- package/test/unit/workflow-migration.test.ts +155 -0
- package/test/unit/workflow-override-by-name.test.ts +116 -0
- package/test/unit/workflow-prioritization.test.ts +38 -0
- package/test/unit/workflow-validation.test.ts +303 -0
- package/test/utils/e2e-test-setup.ts +453 -0
- package/test/utils/run-server-in-dir.sh +27 -0
- package/test/utils/temp-files.ts +308 -0
- package/test/utils/test-access.ts +79 -0
- package/test/utils/test-helpers.ts +286 -0
- package/test/utils/test-setup.ts +78 -0
- package/tsconfig.build.json +21 -0
- package/tsconfig.json +8 -0
- package/vitest.config.ts +18 -0
package/src/database.ts
ADDED
@@ -0,0 +1,685 @@
|
|
1
|
+
/**
|
2
|
+
* Database module for persistent state storage
|
3
|
+
*
|
4
|
+
* Manages SQLite database for conversation state persistence.
|
5
|
+
* Stores minimal state information to survive server restarts.
|
6
|
+
* Also stores interaction logs for auditing and debugging.
|
7
|
+
*/
|
8
|
+
|
9
|
+
import sqlite3 from 'sqlite3';
|
10
|
+
import { mkdir } from 'node:fs/promises';
|
11
|
+
import { dirname } from 'node:path';
|
12
|
+
|
13
|
+
import { join } from 'node:path';
|
14
|
+
import { createLogger } from './logger.js';
|
15
|
+
import type { DevelopmentPhase } from './state-machine.js';
|
16
|
+
import type {
|
17
|
+
ConversationState,
|
18
|
+
InteractionLog,
|
19
|
+
GitCommitConfig,
|
20
|
+
} from './types.js';
|
21
|
+
|
22
|
+
const logger = createLogger('Database');
|
23
|
+
|
24
|
+
// SQLite parameter types
|
25
|
+
type SqliteParam = string | number | boolean | null | undefined | Buffer;
|
26
|
+
type SqliteRow = Record<string, SqliteParam>;
|
27
|
+
type SqliteColumnInfo = {
|
28
|
+
cid: number;
|
29
|
+
name: string;
|
30
|
+
type: string;
|
31
|
+
notnull: number;
|
32
|
+
dflt_value: SqliteParam;
|
33
|
+
pk: number;
|
34
|
+
};
|
35
|
+
|
36
|
+
// Database row validation utilities
|
37
|
+
function validateString(value: SqliteParam, fieldName: string): string {
|
38
|
+
if (typeof value === 'string') {
|
39
|
+
return value;
|
40
|
+
}
|
41
|
+
throw new Error(
|
42
|
+
`Database field '${fieldName}' expected string but got ${typeof value}: ${value}`
|
43
|
+
);
|
44
|
+
}
|
45
|
+
|
46
|
+
function parseJsonSafely(value: SqliteParam, fieldName: string): unknown {
|
47
|
+
if (!value) {
|
48
|
+
return undefined;
|
49
|
+
}
|
50
|
+
const stringValue = validateString(value, fieldName);
|
51
|
+
try {
|
52
|
+
return JSON.parse(stringValue);
|
53
|
+
} catch (error) {
|
54
|
+
throw new Error(
|
55
|
+
`Failed to parse JSON in field '${fieldName}': ${error instanceof Error ? error.message : String(error)}`
|
56
|
+
);
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
function mapRowToInteractionLog(row: SqliteRow): InteractionLog {
|
61
|
+
return {
|
62
|
+
id: typeof row.id === 'number' ? row.id : undefined,
|
63
|
+
conversationId: validateString(row.conversationId, 'conversationId'),
|
64
|
+
toolName: validateString(row.toolName, 'toolName'),
|
65
|
+
inputParams: validateString(row.inputParams, 'inputParams'),
|
66
|
+
responseData: validateString(row.responseData, 'responseData'),
|
67
|
+
currentPhase: validateString(row.currentPhase, 'currentPhase'),
|
68
|
+
timestamp: validateString(row.timestamp, 'timestamp'),
|
69
|
+
isReset: typeof row.isReset === 'number' ? Boolean(row.isReset) : undefined,
|
70
|
+
resetAt: row.resetAt ? validateString(row.resetAt, 'resetAt') : undefined,
|
71
|
+
};
|
72
|
+
}
|
73
|
+
|
74
|
+
export class Database {
|
75
|
+
private db: sqlite3.Database | null = null;
|
76
|
+
private dbPath: string;
|
77
|
+
|
78
|
+
constructor(projectPath: string) {
|
79
|
+
// Store database in .vibe subfolder of the project
|
80
|
+
const vibeDir = join(projectPath, '.vibe');
|
81
|
+
this.dbPath = join(vibeDir, 'conversation-state.sqlite');
|
82
|
+
logger.debug('Database path configured', {
|
83
|
+
projectPath,
|
84
|
+
dbPath: this.dbPath,
|
85
|
+
});
|
86
|
+
}
|
87
|
+
|
88
|
+
/**
|
89
|
+
* Initialize database connection and create tables
|
90
|
+
*/
|
91
|
+
async initialize(): Promise<void> {
|
92
|
+
logger.debug('Initializing database', { dbPath: this.dbPath });
|
93
|
+
|
94
|
+
try {
|
95
|
+
// Ensure directory exists
|
96
|
+
await mkdir(dirname(this.dbPath), { recursive: true });
|
97
|
+
logger.debug('Database directory ensured', {
|
98
|
+
directory: dirname(this.dbPath),
|
99
|
+
});
|
100
|
+
|
101
|
+
// Create database connection
|
102
|
+
this.db = new sqlite3.Database(this.dbPath);
|
103
|
+
logger.debug('Database connection established');
|
104
|
+
|
105
|
+
// Create conversation_states table
|
106
|
+
await this.runQuery(`
|
107
|
+
CREATE TABLE IF NOT EXISTS conversation_states (
|
108
|
+
conversation_id TEXT PRIMARY KEY,
|
109
|
+
project_path TEXT NOT NULL,
|
110
|
+
git_branch TEXT NOT NULL,
|
111
|
+
current_phase TEXT NOT NULL,
|
112
|
+
plan_file_path TEXT NOT NULL,
|
113
|
+
workflow_name TEXT DEFAULT 'waterfall',
|
114
|
+
git_commit_config TEXT, -- JSON string for GitCommitConfig
|
115
|
+
created_at TEXT NOT NULL,
|
116
|
+
updated_at TEXT NOT NULL
|
117
|
+
)
|
118
|
+
`);
|
119
|
+
|
120
|
+
// Create index for efficient lookups
|
121
|
+
await this.runQuery(`
|
122
|
+
CREATE INDEX IF NOT EXISTS idx_project_branch
|
123
|
+
ON conversation_states(project_path, git_branch)
|
124
|
+
`);
|
125
|
+
|
126
|
+
// Create interaction_logs table
|
127
|
+
await this.runQuery(`
|
128
|
+
CREATE TABLE IF NOT EXISTS interaction_logs (
|
129
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
130
|
+
conversation_id TEXT NOT NULL,
|
131
|
+
tool_name TEXT NOT NULL,
|
132
|
+
input_params TEXT NOT NULL,
|
133
|
+
response_data TEXT NOT NULL,
|
134
|
+
current_phase TEXT NOT NULL,
|
135
|
+
timestamp TEXT NOT NULL,
|
136
|
+
is_reset BOOLEAN DEFAULT FALSE,
|
137
|
+
reset_at TEXT,
|
138
|
+
FOREIGN KEY (conversation_id) REFERENCES conversation_states(conversation_id)
|
139
|
+
)
|
140
|
+
`);
|
141
|
+
|
142
|
+
// Create index for efficient lookups of interaction logs
|
143
|
+
await this.runQuery(`
|
144
|
+
CREATE INDEX IF NOT EXISTS idx_interaction_conversation_id
|
145
|
+
ON interaction_logs(conversation_id)
|
146
|
+
`);
|
147
|
+
|
148
|
+
// Run migrations to add any missing columns
|
149
|
+
await this.runMigrations();
|
150
|
+
|
151
|
+
logger.info('Database initialized successfully', { dbPath: this.dbPath });
|
152
|
+
} catch (error) {
|
153
|
+
logger.error('Failed to initialize database', error as Error, {
|
154
|
+
dbPath: this.dbPath,
|
155
|
+
});
|
156
|
+
throw error;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
/**
|
161
|
+
* Helper method to run queries with promises
|
162
|
+
*/
|
163
|
+
private runQuery(sql: string, params: SqliteParam[] = []): Promise<void> {
|
164
|
+
return new Promise((resolve, reject) => {
|
165
|
+
if (!this.db) {
|
166
|
+
reject(new Error('Database not initialized'));
|
167
|
+
return;
|
168
|
+
}
|
169
|
+
|
170
|
+
this.db.run(sql, params, function (err) {
|
171
|
+
if (err) {
|
172
|
+
reject(err);
|
173
|
+
} else {
|
174
|
+
resolve();
|
175
|
+
}
|
176
|
+
});
|
177
|
+
});
|
178
|
+
}
|
179
|
+
|
180
|
+
/**
|
181
|
+
* Helper method to get single row with promises
|
182
|
+
*/
|
183
|
+
private getRow(
|
184
|
+
sql: string,
|
185
|
+
params: SqliteParam[] = []
|
186
|
+
): Promise<SqliteRow | null> {
|
187
|
+
return new Promise((resolve, reject) => {
|
188
|
+
if (!this.db) {
|
189
|
+
reject(new Error('Database not initialized'));
|
190
|
+
return;
|
191
|
+
}
|
192
|
+
|
193
|
+
this.db.get(sql, params, (err, row) => {
|
194
|
+
if (err) {
|
195
|
+
reject(err);
|
196
|
+
} else {
|
197
|
+
resolve(row as SqliteRow | null);
|
198
|
+
}
|
199
|
+
});
|
200
|
+
});
|
201
|
+
}
|
202
|
+
|
203
|
+
/**
|
204
|
+
* Helper method to get multiple rows with promises
|
205
|
+
*/
|
206
|
+
private getAllRows(
|
207
|
+
sql: string,
|
208
|
+
params: SqliteParam[] = []
|
209
|
+
): Promise<SqliteRow[]> {
|
210
|
+
return new Promise((resolve, reject) => {
|
211
|
+
if (!this.db) {
|
212
|
+
reject(new Error('Database not initialized'));
|
213
|
+
return;
|
214
|
+
}
|
215
|
+
|
216
|
+
this.db.all(sql, params, (err, rows) => {
|
217
|
+
if (err) {
|
218
|
+
reject(err);
|
219
|
+
} else {
|
220
|
+
resolve(rows as SqliteRow[]);
|
221
|
+
}
|
222
|
+
});
|
223
|
+
});
|
224
|
+
}
|
225
|
+
|
226
|
+
/**
|
227
|
+
* Get conversation state by ID
|
228
|
+
*/
|
229
|
+
async getConversationState(
|
230
|
+
conversationId: string
|
231
|
+
): Promise<ConversationState | null> {
|
232
|
+
logger.debug('Retrieving conversation state', { conversationId });
|
233
|
+
|
234
|
+
try {
|
235
|
+
const row = await this.getRow(
|
236
|
+
'SELECT * FROM conversation_states WHERE conversation_id = ?',
|
237
|
+
[conversationId]
|
238
|
+
);
|
239
|
+
|
240
|
+
if (!row) {
|
241
|
+
logger.debug('Conversation state not found', { conversationId });
|
242
|
+
return null;
|
243
|
+
}
|
244
|
+
|
245
|
+
const state: ConversationState = {
|
246
|
+
conversationId: validateString(row.conversation_id, 'conversation_id'),
|
247
|
+
projectPath: validateString(row.project_path, 'project_path'),
|
248
|
+
gitBranch: validateString(row.git_branch, 'git_branch'),
|
249
|
+
currentPhase: validateString(
|
250
|
+
row.current_phase,
|
251
|
+
'current_phase'
|
252
|
+
) as DevelopmentPhase,
|
253
|
+
planFilePath: validateString(row.plan_file_path, 'plan_file_path'),
|
254
|
+
workflowName: validateString(row.workflow_name, 'workflow_name'),
|
255
|
+
gitCommitConfig: parseJsonSafely(
|
256
|
+
row.git_commit_config,
|
257
|
+
'git_commit_config'
|
258
|
+
) as GitCommitConfig | undefined,
|
259
|
+
requireReviewsBeforePhaseTransition: Boolean(
|
260
|
+
row.require_reviews_before_phase_transition
|
261
|
+
),
|
262
|
+
createdAt: validateString(row.created_at, 'created_at'),
|
263
|
+
updatedAt: validateString(row.updated_at, 'updated_at'),
|
264
|
+
};
|
265
|
+
|
266
|
+
logger.debug('Conversation state retrieved', {
|
267
|
+
conversationId,
|
268
|
+
currentPhase: state.currentPhase,
|
269
|
+
projectPath: state.projectPath,
|
270
|
+
});
|
271
|
+
|
272
|
+
return state;
|
273
|
+
} catch (error) {
|
274
|
+
logger.error('Failed to retrieve conversation state', error as Error, {
|
275
|
+
conversationId,
|
276
|
+
});
|
277
|
+
throw error;
|
278
|
+
}
|
279
|
+
}
|
280
|
+
|
281
|
+
/**
|
282
|
+
* Save or update conversation state
|
283
|
+
*/
|
284
|
+
async saveConversationState(state: ConversationState): Promise<void> {
|
285
|
+
logger.debug('Saving conversation state', {
|
286
|
+
conversationId: state.conversationId,
|
287
|
+
currentPhase: state.currentPhase,
|
288
|
+
projectPath: state.projectPath,
|
289
|
+
workflowName: state.workflowName,
|
290
|
+
});
|
291
|
+
|
292
|
+
try {
|
293
|
+
await this.runQuery(
|
294
|
+
`INSERT OR REPLACE INTO conversation_states (
|
295
|
+
conversation_id, project_path, git_branch, current_phase,
|
296
|
+
plan_file_path, workflow_name, git_commit_config, require_reviews_before_phase_transition, created_at, updated_at
|
297
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
298
|
+
[
|
299
|
+
state.conversationId,
|
300
|
+
state.projectPath,
|
301
|
+
state.gitBranch,
|
302
|
+
state.currentPhase,
|
303
|
+
state.planFilePath,
|
304
|
+
state.workflowName,
|
305
|
+
state.gitCommitConfig ? JSON.stringify(state.gitCommitConfig) : null,
|
306
|
+
state.requireReviewsBeforePhaseTransition,
|
307
|
+
state.createdAt,
|
308
|
+
state.updatedAt,
|
309
|
+
]
|
310
|
+
);
|
311
|
+
|
312
|
+
logger.info('Conversation state saved successfully', {
|
313
|
+
conversationId: state.conversationId,
|
314
|
+
currentPhase: state.currentPhase,
|
315
|
+
});
|
316
|
+
} catch (error) {
|
317
|
+
logger.error('Failed to save conversation state', error as Error, {
|
318
|
+
conversationId: state.conversationId,
|
319
|
+
});
|
320
|
+
throw error;
|
321
|
+
}
|
322
|
+
}
|
323
|
+
|
324
|
+
/**
|
325
|
+
* Find conversation by project path and git branch
|
326
|
+
*/
|
327
|
+
async findConversationByProject(
|
328
|
+
projectPath: string,
|
329
|
+
gitBranch: string
|
330
|
+
): Promise<ConversationState | null> {
|
331
|
+
const row = await this.getRow(
|
332
|
+
'SELECT * FROM conversation_states WHERE project_path = ? AND git_branch = ?',
|
333
|
+
[projectPath, gitBranch]
|
334
|
+
);
|
335
|
+
|
336
|
+
if (!row) {
|
337
|
+
return null;
|
338
|
+
}
|
339
|
+
|
340
|
+
return {
|
341
|
+
conversationId: validateString(row.conversation_id, 'conversation_id'),
|
342
|
+
projectPath: validateString(row.project_path, 'project_path'),
|
343
|
+
gitBranch: validateString(row.git_branch, 'git_branch'),
|
344
|
+
currentPhase: validateString(
|
345
|
+
row.current_phase,
|
346
|
+
'current_phase'
|
347
|
+
) as DevelopmentPhase,
|
348
|
+
planFilePath: validateString(row.plan_file_path, 'plan_file_path'),
|
349
|
+
workflowName: validateString(row.workflow_name, 'workflow_name'),
|
350
|
+
gitCommitConfig: parseJsonSafely(
|
351
|
+
row.git_commit_config,
|
352
|
+
'git_commit_config'
|
353
|
+
) as GitCommitConfig | undefined,
|
354
|
+
requireReviewsBeforePhaseTransition: Boolean(
|
355
|
+
row.require_reviews_before_phase_transition
|
356
|
+
),
|
357
|
+
createdAt: validateString(row.created_at, 'created_at'),
|
358
|
+
updatedAt: validateString(row.updated_at, 'updated_at'),
|
359
|
+
};
|
360
|
+
}
|
361
|
+
|
362
|
+
/**
|
363
|
+
* Delete conversation state
|
364
|
+
*/
|
365
|
+
async deleteConversationState(conversationId: string): Promise<void> {
|
366
|
+
await this.runQuery(
|
367
|
+
'DELETE FROM conversation_states WHERE conversation_id = ?',
|
368
|
+
[conversationId]
|
369
|
+
);
|
370
|
+
}
|
371
|
+
|
372
|
+
/**
|
373
|
+
* Log an interaction to the database
|
374
|
+
*/
|
375
|
+
async logInteraction(log: InteractionLog): Promise<void> {
|
376
|
+
logger.debug('Logging interaction to database', {
|
377
|
+
conversationId: log.conversationId,
|
378
|
+
toolName: log.toolName,
|
379
|
+
});
|
380
|
+
|
381
|
+
try {
|
382
|
+
await this.runQuery(
|
383
|
+
`INSERT INTO interaction_logs (
|
384
|
+
conversation_id, tool_name, input_params, response_data,
|
385
|
+
current_phase, timestamp
|
386
|
+
) VALUES (?, ?, ?, ?, ?, ?)`,
|
387
|
+
[
|
388
|
+
log.conversationId,
|
389
|
+
log.toolName,
|
390
|
+
log.inputParams,
|
391
|
+
log.responseData,
|
392
|
+
log.currentPhase,
|
393
|
+
log.timestamp,
|
394
|
+
]
|
395
|
+
);
|
396
|
+
|
397
|
+
logger.debug('Interaction logged successfully', {
|
398
|
+
conversationId: log.conversationId,
|
399
|
+
toolName: log.toolName,
|
400
|
+
timestamp: log.timestamp,
|
401
|
+
});
|
402
|
+
} catch (error) {
|
403
|
+
logger.error('Failed to log interaction', error as Error, {
|
404
|
+
conversationId: log.conversationId,
|
405
|
+
});
|
406
|
+
throw error;
|
407
|
+
}
|
408
|
+
}
|
409
|
+
|
410
|
+
/**
|
411
|
+
* Get all interactions for a specific conversation
|
412
|
+
*/
|
413
|
+
async getInteractionsByConversationId(
|
414
|
+
conversationId: string
|
415
|
+
): Promise<InteractionLog[]> {
|
416
|
+
logger.debug('Getting interactions by conversation ID', { conversationId });
|
417
|
+
|
418
|
+
try {
|
419
|
+
const rows = await this.getAllRows(
|
420
|
+
'SELECT * FROM interaction_logs WHERE conversation_id = ? ORDER BY timestamp ASC',
|
421
|
+
[conversationId]
|
422
|
+
);
|
423
|
+
|
424
|
+
const logs: InteractionLog[] = rows.map(row => ({
|
425
|
+
id: typeof row.id === 'number' ? row.id : undefined,
|
426
|
+
conversationId: validateString(row.conversation_id, 'conversation_id'),
|
427
|
+
toolName: validateString(row.tool_name, 'tool_name'),
|
428
|
+
inputParams: validateString(row.input_params, 'input_params'),
|
429
|
+
responseData: validateString(row.response_data, 'response_data'),
|
430
|
+
currentPhase: validateString(
|
431
|
+
row.current_phase,
|
432
|
+
'current_phase'
|
433
|
+
) as DevelopmentPhase,
|
434
|
+
timestamp: validateString(row.timestamp, 'timestamp'),
|
435
|
+
}));
|
436
|
+
|
437
|
+
logger.debug('Retrieved interaction logs', {
|
438
|
+
conversationId,
|
439
|
+
count: logs.length,
|
440
|
+
});
|
441
|
+
|
442
|
+
return logs;
|
443
|
+
} catch (error) {
|
444
|
+
logger.error('Failed to get interaction logs', error as Error, {
|
445
|
+
conversationId,
|
446
|
+
});
|
447
|
+
throw error;
|
448
|
+
}
|
449
|
+
}
|
450
|
+
|
451
|
+
/**
|
452
|
+
* Run database migrations to add new columns
|
453
|
+
*/
|
454
|
+
private async runMigrations(): Promise<void> {
|
455
|
+
logger.debug('Running database migrations');
|
456
|
+
|
457
|
+
try {
|
458
|
+
// Check if interaction_logs table exists first
|
459
|
+
const tables = await this.getAllRows(
|
460
|
+
"SELECT name FROM sqlite_master WHERE type='table' AND name='interaction_logs'"
|
461
|
+
);
|
462
|
+
|
463
|
+
if (tables.length > 0) {
|
464
|
+
// Table exists, check for missing columns
|
465
|
+
const tableInfo = await this.getAllRows(
|
466
|
+
'PRAGMA table_info(interaction_logs)'
|
467
|
+
);
|
468
|
+
const hasIsReset = (tableInfo as unknown as SqliteColumnInfo[]).some(
|
469
|
+
(col: SqliteColumnInfo) => col.name === 'is_reset'
|
470
|
+
);
|
471
|
+
const hasResetAt = (tableInfo as unknown as SqliteColumnInfo[]).some(
|
472
|
+
(col: SqliteColumnInfo) => col.name === 'reset_at'
|
473
|
+
);
|
474
|
+
|
475
|
+
if (!hasIsReset) {
|
476
|
+
logger.info('Adding is_reset column to interaction_logs table');
|
477
|
+
await this.runQuery(
|
478
|
+
'ALTER TABLE interaction_logs ADD COLUMN is_reset BOOLEAN DEFAULT FALSE'
|
479
|
+
);
|
480
|
+
}
|
481
|
+
|
482
|
+
if (!hasResetAt) {
|
483
|
+
logger.info('Adding reset_at column to interaction_logs table');
|
484
|
+
await this.runQuery(
|
485
|
+
'ALTER TABLE interaction_logs ADD COLUMN reset_at TEXT'
|
486
|
+
);
|
487
|
+
}
|
488
|
+
}
|
489
|
+
|
490
|
+
// Check if conversation_states table exists and has workflow_name column
|
491
|
+
const conversationTables = await this.getAllRows(
|
492
|
+
"SELECT name FROM sqlite_master WHERE type='table' AND name='conversation_states'"
|
493
|
+
);
|
494
|
+
|
495
|
+
if (conversationTables.length > 0) {
|
496
|
+
const conversationTableInfo = (await this.getAllRows(
|
497
|
+
'PRAGMA table_info(conversation_states)'
|
498
|
+
)) as SqliteColumnInfo[];
|
499
|
+
const hasWorkflowName = conversationTableInfo.some(
|
500
|
+
(col: SqliteColumnInfo) => col.name === 'workflow_name'
|
501
|
+
);
|
502
|
+
const hasGitCommitConfig = conversationTableInfo.some(
|
503
|
+
(col: SqliteColumnInfo) => col.name === 'git_commit_config'
|
504
|
+
);
|
505
|
+
const hasRequireReviews = conversationTableInfo.some(
|
506
|
+
(col: SqliteColumnInfo) =>
|
507
|
+
col.name === 'require_reviews_before_phase_transition'
|
508
|
+
);
|
509
|
+
|
510
|
+
if (!hasWorkflowName) {
|
511
|
+
logger.info(
|
512
|
+
'Adding workflow_name column to conversation_states table'
|
513
|
+
);
|
514
|
+
await this.runQuery(
|
515
|
+
"ALTER TABLE conversation_states ADD COLUMN workflow_name TEXT DEFAULT 'waterfall'"
|
516
|
+
);
|
517
|
+
}
|
518
|
+
|
519
|
+
if (!hasGitCommitConfig) {
|
520
|
+
logger.info(
|
521
|
+
'Adding git_commit_config column to conversation_states table'
|
522
|
+
);
|
523
|
+
await this.runQuery(
|
524
|
+
'ALTER TABLE conversation_states ADD COLUMN git_commit_config TEXT'
|
525
|
+
);
|
526
|
+
}
|
527
|
+
|
528
|
+
if (!hasRequireReviews) {
|
529
|
+
logger.info(
|
530
|
+
'Adding require_reviews_before_phase_transition column to conversation_states table'
|
531
|
+
);
|
532
|
+
await this.runQuery(
|
533
|
+
'ALTER TABLE conversation_states ADD COLUMN require_reviews_before_phase_transition BOOLEAN DEFAULT FALSE'
|
534
|
+
);
|
535
|
+
}
|
536
|
+
}
|
537
|
+
|
538
|
+
logger.debug('Database migrations completed successfully');
|
539
|
+
} catch (error) {
|
540
|
+
logger.error('Failed to run database migrations', error as Error);
|
541
|
+
throw error;
|
542
|
+
}
|
543
|
+
}
|
544
|
+
|
545
|
+
/**
|
546
|
+
* Soft delete interaction logs for a conversation
|
547
|
+
*/
|
548
|
+
async softDeleteInteractionLogs(
|
549
|
+
conversationId: string,
|
550
|
+
reason?: string
|
551
|
+
): Promise<void> {
|
552
|
+
logger.debug('Soft deleting interaction logs', { conversationId, reason });
|
553
|
+
|
554
|
+
try {
|
555
|
+
const resetAt = new Date().toISOString();
|
556
|
+
await this.runQuery(
|
557
|
+
'UPDATE interaction_logs SET is_reset = TRUE, reset_at = ? WHERE conversation_id = ? AND is_reset = FALSE',
|
558
|
+
[resetAt, conversationId]
|
559
|
+
);
|
560
|
+
|
561
|
+
logger.info('Interaction logs soft deleted successfully', {
|
562
|
+
conversationId,
|
563
|
+
reason,
|
564
|
+
resetAt,
|
565
|
+
});
|
566
|
+
} catch (error) {
|
567
|
+
logger.error('Failed to soft delete interaction logs', error as Error, {
|
568
|
+
conversationId,
|
569
|
+
});
|
570
|
+
throw error;
|
571
|
+
}
|
572
|
+
}
|
573
|
+
|
574
|
+
/**
|
575
|
+
* Get active (non-reset) interaction logs for a conversation
|
576
|
+
*/
|
577
|
+
async getActiveInteractionLogs(
|
578
|
+
conversationId: string
|
579
|
+
): Promise<InteractionLog[]> {
|
580
|
+
logger.debug('Getting active interaction logs', { conversationId });
|
581
|
+
|
582
|
+
try {
|
583
|
+
const rows = await this.getAllRows(
|
584
|
+
'SELECT * FROM interaction_logs WHERE conversation_id = ? AND (is_reset = FALSE OR is_reset IS NULL) ORDER BY timestamp ASC',
|
585
|
+
[conversationId]
|
586
|
+
);
|
587
|
+
|
588
|
+
const logs: InteractionLog[] = rows.map(row =>
|
589
|
+
mapRowToInteractionLog({
|
590
|
+
id: row.id,
|
591
|
+
conversationId: row.conversation_id,
|
592
|
+
toolName: row.tool_name,
|
593
|
+
inputParams: row.input_params,
|
594
|
+
responseData: row.response_data,
|
595
|
+
currentPhase: row.current_phase,
|
596
|
+
timestamp: row.timestamp,
|
597
|
+
})
|
598
|
+
);
|
599
|
+
|
600
|
+
logger.debug('Retrieved active interaction logs', {
|
601
|
+
conversationId,
|
602
|
+
count: logs.length,
|
603
|
+
});
|
604
|
+
|
605
|
+
return logs;
|
606
|
+
} catch (error) {
|
607
|
+
logger.error('Failed to get active interaction logs', error as Error, {
|
608
|
+
conversationId,
|
609
|
+
});
|
610
|
+
throw error;
|
611
|
+
}
|
612
|
+
}
|
613
|
+
|
614
|
+
/**
|
615
|
+
* Get all interaction logs including reset ones for a conversation
|
616
|
+
*/
|
617
|
+
async getAllInteractionLogsIncludingReset(
|
618
|
+
conversationId: string
|
619
|
+
): Promise<InteractionLog[]> {
|
620
|
+
logger.debug('Getting all interaction logs including reset', {
|
621
|
+
conversationId,
|
622
|
+
});
|
623
|
+
|
624
|
+
try {
|
625
|
+
const rows = await this.getAllRows(
|
626
|
+
'SELECT * FROM interaction_logs WHERE conversation_id = ? ORDER BY timestamp ASC',
|
627
|
+
[conversationId]
|
628
|
+
);
|
629
|
+
|
630
|
+
const logs: InteractionLog[] = rows.map(row =>
|
631
|
+
mapRowToInteractionLog({
|
632
|
+
id: row.id,
|
633
|
+
conversationId: row.conversation_id,
|
634
|
+
toolName: row.tool_name,
|
635
|
+
inputParams: row.input_params,
|
636
|
+
responseData: row.response_data,
|
637
|
+
currentPhase: row.current_phase,
|
638
|
+
timestamp: row.timestamp,
|
639
|
+
isReset: row.is_reset,
|
640
|
+
resetAt: row.reset_at,
|
641
|
+
})
|
642
|
+
);
|
643
|
+
|
644
|
+
logger.debug('Retrieved all interaction logs including reset', {
|
645
|
+
conversationId,
|
646
|
+
count: logs.length,
|
647
|
+
resetCount: logs.filter(log => log.isReset).length,
|
648
|
+
});
|
649
|
+
|
650
|
+
return logs;
|
651
|
+
} catch (error) {
|
652
|
+
logger.error(
|
653
|
+
'Failed to get all interaction logs including reset',
|
654
|
+
error as Error,
|
655
|
+
{ conversationId }
|
656
|
+
);
|
657
|
+
throw error;
|
658
|
+
}
|
659
|
+
}
|
660
|
+
|
661
|
+
/**
|
662
|
+
* Close database connection
|
663
|
+
*/
|
664
|
+
async close(): Promise<void> {
|
665
|
+
logger.debug('Closing database connection');
|
666
|
+
|
667
|
+
return new Promise((resolve, reject) => {
|
668
|
+
if (this.db) {
|
669
|
+
this.db.close(err => {
|
670
|
+
if (err) {
|
671
|
+
logger.error('Failed to close database connection', err);
|
672
|
+
reject(err);
|
673
|
+
} else {
|
674
|
+
this.db = null;
|
675
|
+
logger.info('Database connection closed successfully');
|
676
|
+
resolve();
|
677
|
+
}
|
678
|
+
});
|
679
|
+
} else {
|
680
|
+
logger.debug('Database connection already closed');
|
681
|
+
resolve();
|
682
|
+
}
|
683
|
+
});
|
684
|
+
}
|
685
|
+
}
|