principles-disciple 1.148.0 → 1.149.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/dist/index.d.ts +1 -1
- package/dist/index.js +4 -5
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/dist/core/principle-tree-ledger-adapter.d.ts +0 -32
- package/dist/core/principle-tree-ledger-adapter.js +0 -125
- package/dist/service/central-database.d.ts +0 -138
- package/dist/service/central-database.js +0 -731
- package/dist/service/central-sync-service.d.ts +0 -8
- package/dist/service/central-sync-service.js +0 -71
package/dist/index.d.ts
CHANGED
|
@@ -45,6 +45,6 @@ declare const plugin: {
|
|
|
45
45
|
description: string;
|
|
46
46
|
register(api: OpenClawPluginApi): void;
|
|
47
47
|
};
|
|
48
|
-
export { PrincipleTreeLedgerAdapter } from '
|
|
48
|
+
export { PrincipleTreeLedgerAdapter } from '@principles/core/runtime-v2';
|
|
49
49
|
export { loadFeatureFlagFromWorkspace, isRecord };
|
|
50
50
|
export default plugin;
|
package/dist/index.js
CHANGED
|
@@ -31,7 +31,6 @@ import { CorrectionObserverService } from './service/correction-observer-service
|
|
|
31
31
|
import { InternalizationAutoConsumerService } from './service/internalization-auto-consumer-service.js';
|
|
32
32
|
import { TrajectoryService } from './service/trajectory-service.js';
|
|
33
33
|
import { PDTaskService } from './core/pd-task-service.js';
|
|
34
|
-
import { CentralSyncService } from './service/central-sync-service.js';
|
|
35
34
|
import { ensureWorkspaceTemplates } from './core/init.js';
|
|
36
35
|
import { migrateDirectoryStructure } from './core/migration.js';
|
|
37
36
|
import { migrateStaleWorkspaceGuidance } from './core/workspace-guidance-migrator.js';
|
|
@@ -408,9 +407,6 @@ const plugin = {
|
|
|
408
407
|
const guardedPdTask = guardService('service:pd-task', PDTaskService, api.logger);
|
|
409
408
|
if (guardedPdTask)
|
|
410
409
|
api.registerService(guardedPdTask);
|
|
411
|
-
const guardedCentralSync = guardService('service:central-sync', CentralSyncService, api.logger);
|
|
412
|
-
if (guardedCentralSync)
|
|
413
|
-
api.registerService(guardedCentralSync);
|
|
414
410
|
const guardedAutoConsumer = guardService('service:internalization-auto-consumer', InternalizationAutoConsumerService, api.logger);
|
|
415
411
|
if (guardedAutoConsumer)
|
|
416
412
|
api.registerService(guardedAutoConsumer);
|
|
@@ -745,7 +741,10 @@ const plugin = {
|
|
|
745
741
|
});
|
|
746
742
|
}
|
|
747
743
|
};
|
|
748
|
-
|
|
744
|
+
// PrincipleTreeLedgerAdapter is exported from @principles/core/runtime-v2 (canonical,
|
|
745
|
+
// consolidated in PRI-459). The plugin-local duplicate was removed; re-export the core
|
|
746
|
+
// symbol so any external consumer importing from the plugin still resolves it.
|
|
747
|
+
export { PrincipleTreeLedgerAdapter } from '@principles/core/runtime-v2';
|
|
749
748
|
/* istanbul ignore next — test exports for evolution worker gate */
|
|
750
749
|
export { loadFeatureFlagFromWorkspace, isRecord };
|
|
751
750
|
export default plugin;
|
package/openclaw.plugin.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"id": "principles-disciple",
|
|
3
3
|
"name": "Principles Disciple",
|
|
4
4
|
"description": "Evolutionary programming agent framework with strategic guardrails and reflection loops.",
|
|
5
|
-
"version": "1.
|
|
5
|
+
"version": "1.149.0",
|
|
6
6
|
"activation": {
|
|
7
7
|
"onCapabilities": [
|
|
8
8
|
"hook"
|
package/package.json
CHANGED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { LedgerAdapter, LedgerPrincipleEntry } from '@principles/core/runtime-v2';
|
|
2
|
-
/**
|
|
3
|
-
* PrincipleTreeLedgerAdapter — bridges 11-field LedgerPrincipleEntry to 18+ field LedgerPrinciple.
|
|
4
|
-
*
|
|
5
|
-
* This adapter maintains an in-memory idempotency map. Create once and reuse
|
|
6
|
-
* across calls within the same process lifetime. Do not create a new instance
|
|
7
|
-
* per intake call.
|
|
8
|
-
*/
|
|
9
|
-
export declare class PrincipleTreeLedgerAdapter implements LedgerAdapter {
|
|
10
|
-
#private;
|
|
11
|
-
constructor(opts: {
|
|
12
|
-
stateDir: string;
|
|
13
|
-
});
|
|
14
|
-
/**
|
|
15
|
-
* Write a probation entry to the ledger.
|
|
16
|
-
*
|
|
17
|
-
* Idempotency: if the same candidateId was already written, returns the
|
|
18
|
-
* existing entry without writing again.
|
|
19
|
-
*
|
|
20
|
-
* @throws CandidateIntakeError with code LEDGER_WRITE_FAILED on write failure.
|
|
21
|
-
*/
|
|
22
|
-
writeProbationEntry(entry: LedgerPrincipleEntry): LedgerPrincipleEntry;
|
|
23
|
-
/**
|
|
24
|
-
* Check if a ledger entry already exists for a given candidate.
|
|
25
|
-
*
|
|
26
|
-
* Queries both the in-memory Map (fast path for same-process repeat calls)
|
|
27
|
-
* and the ledger file (for cross-process idempotency across CLI invocations).
|
|
28
|
-
*
|
|
29
|
-
* @returns The existing LedgerPrincipleEntry if found, null otherwise.
|
|
30
|
-
*/
|
|
31
|
-
existsForCandidate(candidateId: string): LedgerPrincipleEntry | null;
|
|
32
|
-
}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { CandidateIntakeError, INTAKE_ERROR_CODES } from '@principles/core/runtime-v2';
|
|
2
|
-
import { addPrincipleToLedger, loadLedger } from './principle-tree-ledger.js';
|
|
3
|
-
const VALID_EVALUABILITIES = ['deterministic', 'weak_heuristic', 'manual_only'];
|
|
4
|
-
/**
|
|
5
|
-
* PrincipleTreeLedgerAdapter — bridges 11-field LedgerPrincipleEntry to 18+ field LedgerPrinciple.
|
|
6
|
-
*
|
|
7
|
-
* This adapter maintains an in-memory idempotency map. Create once and reuse
|
|
8
|
-
* across calls within the same process lifetime. Do not create a new instance
|
|
9
|
-
* per intake call.
|
|
10
|
-
*/
|
|
11
|
-
export class PrincipleTreeLedgerAdapter {
|
|
12
|
-
#stateDir;
|
|
13
|
-
#entryMap = new Map();
|
|
14
|
-
constructor(opts) {
|
|
15
|
-
this.#stateDir = opts.stateDir;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Write a probation entry to the ledger.
|
|
19
|
-
*
|
|
20
|
-
* Idempotency: if the same candidateId was already written, returns the
|
|
21
|
-
* existing entry without writing again.
|
|
22
|
-
*
|
|
23
|
-
* @throws CandidateIntakeError with code LEDGER_WRITE_FAILED on write failure.
|
|
24
|
-
*/
|
|
25
|
-
writeProbationEntry(entry) {
|
|
26
|
-
const candidateId = this.#extractCandidateId(entry.sourceRef);
|
|
27
|
-
const existing = this.#entryMap.get(candidateId);
|
|
28
|
-
if (existing)
|
|
29
|
-
return existing;
|
|
30
|
-
const ledgerPrinciple = this.#expandToLedgerPrinciple(entry, candidateId);
|
|
31
|
-
try {
|
|
32
|
-
addPrincipleToLedger(this.#stateDir, ledgerPrinciple);
|
|
33
|
-
}
|
|
34
|
-
catch (err) {
|
|
35
|
-
throw new CandidateIntakeError(INTAKE_ERROR_CODES.LEDGER_WRITE_FAILED, `Failed to write principle ${entry.id} to ledger: ${String(err)}`, { cause: err });
|
|
36
|
-
}
|
|
37
|
-
this.#entryMap.set(candidateId, entry);
|
|
38
|
-
return entry;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Check if a ledger entry already exists for a given candidate.
|
|
42
|
-
*
|
|
43
|
-
* Queries both the in-memory Map (fast path for same-process repeat calls)
|
|
44
|
-
* and the ledger file (for cross-process idempotency across CLI invocations).
|
|
45
|
-
*
|
|
46
|
-
* @returns The existing LedgerPrincipleEntry if found, null otherwise.
|
|
47
|
-
*/
|
|
48
|
-
existsForCandidate(candidateId) {
|
|
49
|
-
// Fast path: check in-memory Map (covers same-process repeat calls)
|
|
50
|
-
const cached = this.#entryMap.get(candidateId);
|
|
51
|
-
if (cached)
|
|
52
|
-
return cached;
|
|
53
|
-
// Cross-process path: check ledger file by derivedFromPainIds
|
|
54
|
-
const ledger = loadLedger(this.#stateDir);
|
|
55
|
-
const found = Object.values(ledger.tree.principles).find((p) => p.derivedFromPainIds.includes(candidateId));
|
|
56
|
-
if (!found)
|
|
57
|
-
return null;
|
|
58
|
-
// Reconstruct LedgerPrincipleEntry from LedgerPrinciple fields
|
|
59
|
-
// Note: sourceRef, artifactRef, taskRef are not stored in LedgerPrinciple,
|
|
60
|
-
// so we cannot fully reconstruct the original LedgerPrincipleEntry.
|
|
61
|
-
// Return minimal entry sufficient for idempotency signaling.
|
|
62
|
-
return {
|
|
63
|
-
id: found.id,
|
|
64
|
-
title: '',
|
|
65
|
-
status: 'probation',
|
|
66
|
-
sourceRef: `candidate://${candidateId}`,
|
|
67
|
-
artifactRef: '',
|
|
68
|
-
taskRef: '',
|
|
69
|
-
text: found.text,
|
|
70
|
-
triggerPattern: found.triggerPattern,
|
|
71
|
-
action: found.action,
|
|
72
|
-
evaluability: 'weak_heuristic',
|
|
73
|
-
createdAt: found.createdAt,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Extract candidateId from sourceRef by stripping the 'candidate://' prefix.
|
|
78
|
-
*
|
|
79
|
-
* @internal
|
|
80
|
-
*/
|
|
81
|
-
#extractCandidateId(sourceRef) {
|
|
82
|
-
if (sourceRef.startsWith('candidate://')) {
|
|
83
|
-
return sourceRef.slice('candidate://'.length);
|
|
84
|
-
}
|
|
85
|
-
return sourceRef;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Expand an 11-field LedgerPrincipleEntry to an 18+ field LedgerPrinciple.
|
|
89
|
-
*
|
|
90
|
-
* Applies the field expansion table from CONTEXT.md:
|
|
91
|
-
* - status: 'probation' → 'candidate'
|
|
92
|
-
* - triggerPattern/action: pass through (empty string if absent)
|
|
93
|
-
* - All defaults applied per CONTEXT.md expansion table
|
|
94
|
-
* - sourceRef/artifactRef/taskRef are NOT written to LedgerPrinciple (not stored)
|
|
95
|
-
* - title is NOT written to LedgerPrinciple — intentionally excluded; title
|
|
96
|
-
* is available in LedgerPrincipleEntry but LedgerPrinciple has no title field
|
|
97
|
-
*
|
|
98
|
-
* @internal
|
|
99
|
-
*/
|
|
100
|
-
#expandToLedgerPrinciple(entry, candidateId) {
|
|
101
|
-
if (!VALID_EVALUABILITIES.includes(entry.evaluability)) {
|
|
102
|
-
throw new CandidateIntakeError(INTAKE_ERROR_CODES.INPUT_INVALID, `Invalid evaluability value: ${entry.evaluability}. Must be one of: ${VALID_EVALUABILITIES.join(', ')}`);
|
|
103
|
-
}
|
|
104
|
-
const result = {
|
|
105
|
-
id: entry.id,
|
|
106
|
-
version: 1,
|
|
107
|
-
text: entry.text,
|
|
108
|
-
triggerPattern: entry.triggerPattern ?? '',
|
|
109
|
-
action: entry.action ?? '',
|
|
110
|
-
status: 'candidate',
|
|
111
|
-
evaluability: entry.evaluability,
|
|
112
|
-
priority: 'P1',
|
|
113
|
-
scope: 'general',
|
|
114
|
-
valueScore: 0,
|
|
115
|
-
adherenceRate: 0,
|
|
116
|
-
painPreventedCount: 0,
|
|
117
|
-
derivedFromPainIds: [candidateId],
|
|
118
|
-
ruleIds: [],
|
|
119
|
-
conflictsWithPrincipleIds: [],
|
|
120
|
-
createdAt: entry.createdAt,
|
|
121
|
-
updatedAt: entry.createdAt,
|
|
122
|
-
};
|
|
123
|
-
return result;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
export interface WorkspaceInfo {
|
|
2
|
-
name: string;
|
|
3
|
-
path: string;
|
|
4
|
-
lastSync: string | null;
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* Central database that aggregates data from all agent workspaces.
|
|
8
|
-
* Stored in ~/.openclaw/.central/ (NOT in memory/ which is for embeddings)
|
|
9
|
-
*/
|
|
10
|
-
export declare class CentralDatabase {
|
|
11
|
-
private readonly dbPath;
|
|
12
|
-
private readonly db;
|
|
13
|
-
private readonly workspaces;
|
|
14
|
-
private _closed;
|
|
15
|
-
/** Whether this connection has been closed. Used by the singleton to auto-reopen. */
|
|
16
|
-
get isClosed(): boolean;
|
|
17
|
-
constructor(dbPath?: string);
|
|
18
|
-
dispose(): void;
|
|
19
|
-
private static tableExists;
|
|
20
|
-
private initSchema;
|
|
21
|
-
private discoverWorkspaces;
|
|
22
|
-
/**
|
|
23
|
-
* Sync data from a single workspace into the central database
|
|
24
|
-
*/
|
|
25
|
-
syncWorkspace(workspaceName: string): number;
|
|
26
|
-
syncEnabled(): Map<string, number>;
|
|
27
|
-
/**
|
|
28
|
-
* Sync all workspaces (legacy method - syncs all regardless of config)
|
|
29
|
-
*/
|
|
30
|
-
syncAll(): Map<string, number>;
|
|
31
|
-
private getEnabledWorkspaceFilter;
|
|
32
|
-
/**
|
|
33
|
-
* Get aggregated overview stats (only from enabled workspaces)
|
|
34
|
-
*/
|
|
35
|
-
getOverviewStats(): {
|
|
36
|
-
totalSessions: number;
|
|
37
|
-
totalToolCalls: number;
|
|
38
|
-
totalFailures: number;
|
|
39
|
-
totalPainEvents: number;
|
|
40
|
-
totalCorrections: number;
|
|
41
|
-
totalThinkingEvents: number;
|
|
42
|
-
totalSamples: number;
|
|
43
|
-
pendingSamples: number;
|
|
44
|
-
approvedSamples: number;
|
|
45
|
-
rejectedSamples: number;
|
|
46
|
-
workspaceCount: number;
|
|
47
|
-
enabledWorkspaceCount: number;
|
|
48
|
-
workspaceNames: string[];
|
|
49
|
-
enabledWorkspaceNames: string[];
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* Get daily trend data
|
|
53
|
-
*/
|
|
54
|
-
getDailyTrend(days?: number): {
|
|
55
|
-
day: string;
|
|
56
|
-
toolCalls: number;
|
|
57
|
-
failures: number;
|
|
58
|
-
userCorrections: number;
|
|
59
|
-
thinkingTurns: number;
|
|
60
|
-
}[];
|
|
61
|
-
/**
|
|
62
|
-
* Get top regressions
|
|
63
|
-
*/
|
|
64
|
-
getTopRegressions(limit?: number): {
|
|
65
|
-
toolName: string;
|
|
66
|
-
errorType: string;
|
|
67
|
-
occurrences: number;
|
|
68
|
-
}[];
|
|
69
|
-
/**
|
|
70
|
-
* Get thinking model stats
|
|
71
|
-
*/
|
|
72
|
-
getThinkingModelStats(): {
|
|
73
|
-
totalModels: number;
|
|
74
|
-
activeModels: number;
|
|
75
|
-
models: {
|
|
76
|
-
modelId: string;
|
|
77
|
-
hits: number;
|
|
78
|
-
coverageRate: number;
|
|
79
|
-
}[];
|
|
80
|
-
};
|
|
81
|
-
/**
|
|
82
|
-
* Get workspace list
|
|
83
|
-
*/
|
|
84
|
-
getWorkspaces(): WorkspaceInfo[];
|
|
85
|
-
getWorkspaceConfigs(): {
|
|
86
|
-
workspaceName: string;
|
|
87
|
-
enabled: boolean;
|
|
88
|
-
displayName: string | null;
|
|
89
|
-
syncEnabled: boolean;
|
|
90
|
-
}[];
|
|
91
|
-
updateWorkspaceConfig(workspaceName: string, updates: {
|
|
92
|
-
enabled?: boolean;
|
|
93
|
-
displayName?: string | null;
|
|
94
|
-
syncEnabled?: boolean;
|
|
95
|
-
}): void;
|
|
96
|
-
isWorkspaceEnabled(workspaceName: string): boolean;
|
|
97
|
-
getEnabledWorkspaces(): WorkspaceInfo[];
|
|
98
|
-
addCustomWorkspace(name: string, workspacePath: string): void;
|
|
99
|
-
removeWorkspace(workspaceName: string): void;
|
|
100
|
-
getGlobalConfig(key: string): string | null;
|
|
101
|
-
setGlobalConfig(key: string, value: string): void;
|
|
102
|
-
/**
|
|
103
|
-
* Clear all aggregated data (for testing/reset)
|
|
104
|
-
*/
|
|
105
|
-
clearAll(): void;
|
|
106
|
-
/**
|
|
107
|
-
* Get total task outcomes count across enabled workspaces (D-02)
|
|
108
|
-
*/
|
|
109
|
-
getTaskOutcomes(): number;
|
|
110
|
-
/**
|
|
111
|
-
* Get total principle events count across enabled workspaces (D-03)
|
|
112
|
-
*/
|
|
113
|
-
getPrincipleEventCount(): number;
|
|
114
|
-
/**
|
|
115
|
-
* Get sample counts grouped by review_status across enabled workspaces (D-06)
|
|
116
|
-
*/
|
|
117
|
-
getSampleCountersByStatus(): Record<string, number>;
|
|
118
|
-
/**
|
|
119
|
-
* Get top N most recent pending/approved samples across all enabled workspaces (D-04)
|
|
120
|
-
*/
|
|
121
|
-
getSamplePreview(limit?: number): {
|
|
122
|
-
sampleId: string;
|
|
123
|
-
sessionId: string;
|
|
124
|
-
workspace: string;
|
|
125
|
-
qualityScore: number;
|
|
126
|
-
reviewStatus: string;
|
|
127
|
-
createdAt: string;
|
|
128
|
-
}[];
|
|
129
|
-
/**
|
|
130
|
-
* Get the most recent lastSync timestamp across all workspaces (D-05)
|
|
131
|
-
*/
|
|
132
|
-
getMostRecentSync(): string | null;
|
|
133
|
-
}
|
|
134
|
-
export declare function getCentralDatabase(): CentralDatabase;
|
|
135
|
-
/**
|
|
136
|
-
* Reset the singleton instance. Used for testing.
|
|
137
|
-
*/
|
|
138
|
-
export declare function resetCentralDatabase(): void;
|
|
@@ -1,731 +0,0 @@
|
|
|
1
|
-
import Database from 'better-sqlite3';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import os from 'os';
|
|
5
|
-
import { WorkspaceNotFoundError } from '../config/index.js';
|
|
6
|
-
const CENTRAL_DB_DIR = '.central';
|
|
7
|
-
const CENTRAL_DB_NAME = 'aggregated.db';
|
|
8
|
-
/**
|
|
9
|
-
* Central database that aggregates data from all agent workspaces.
|
|
10
|
-
* Stored in ~/.openclaw/.central/ (NOT in memory/ which is for embeddings)
|
|
11
|
-
*/
|
|
12
|
-
export class CentralDatabase {
|
|
13
|
-
dbPath;
|
|
14
|
-
db;
|
|
15
|
-
workspaces = [];
|
|
16
|
-
_closed = false;
|
|
17
|
-
/** Whether this connection has been closed. Used by the singleton to auto-reopen. */
|
|
18
|
-
get isClosed() {
|
|
19
|
-
return this._closed;
|
|
20
|
-
}
|
|
21
|
-
constructor(dbPath) {
|
|
22
|
-
const openClawDir = os.homedir();
|
|
23
|
-
this.dbPath = dbPath
|
|
24
|
-
|| (process.env.PD_CENTRAL_DB_PATH ? path.resolve(process.env.PD_CENTRAL_DB_PATH) : null)
|
|
25
|
-
|| path.join(openClawDir, '.openclaw', CENTRAL_DB_DIR, CENTRAL_DB_NAME);
|
|
26
|
-
// Ensure directory exists
|
|
27
|
-
fs.mkdirSync(path.dirname(this.dbPath), { recursive: true });
|
|
28
|
-
this.db = new Database(this.dbPath);
|
|
29
|
-
this.db.pragma('journal_mode = WAL');
|
|
30
|
-
this.db.pragma('synchronous = NORMAL');
|
|
31
|
-
this.initSchema();
|
|
32
|
-
this.discoverWorkspaces();
|
|
33
|
-
}
|
|
34
|
-
dispose() {
|
|
35
|
-
this.db.close();
|
|
36
|
-
this._closed = true;
|
|
37
|
-
}
|
|
38
|
-
static tableExists(db, tableName) {
|
|
39
|
-
const result = db.prepare(`
|
|
40
|
-
SELECT name FROM sqlite_master WHERE type='table' AND name=?
|
|
41
|
-
`).get(tableName);
|
|
42
|
-
return !!result;
|
|
43
|
-
}
|
|
44
|
-
initSchema() {
|
|
45
|
-
this.db.exec(`
|
|
46
|
-
CREATE TABLE IF NOT EXISTS schema_version (
|
|
47
|
-
version INTEGER NOT NULL
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
CREATE TABLE IF NOT EXISTS workspaces (
|
|
51
|
-
name TEXT PRIMARY KEY,
|
|
52
|
-
path TEXT NOT NULL,
|
|
53
|
-
last_sync TEXT
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
CREATE TABLE IF NOT EXISTS workspace_config (
|
|
57
|
-
workspace_name TEXT PRIMARY KEY,
|
|
58
|
-
enabled INTEGER NOT NULL DEFAULT 1,
|
|
59
|
-
display_name TEXT,
|
|
60
|
-
sync_enabled INTEGER NOT NULL DEFAULT 1,
|
|
61
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
62
|
-
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
CREATE TABLE IF NOT EXISTS global_config (
|
|
66
|
-
key TEXT PRIMARY KEY,
|
|
67
|
-
value TEXT NOT NULL,
|
|
68
|
-
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
CREATE TABLE IF NOT EXISTS aggregated_sessions (
|
|
72
|
-
session_id TEXT PRIMARY KEY,
|
|
73
|
-
workspace TEXT NOT NULL,
|
|
74
|
-
started_at TEXT NOT NULL,
|
|
75
|
-
updated_at TEXT NOT NULL
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
CREATE TABLE IF NOT EXISTS aggregated_tool_calls (
|
|
79
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
80
|
-
workspace TEXT NOT NULL,
|
|
81
|
-
session_id TEXT NOT NULL,
|
|
82
|
-
tool_name TEXT NOT NULL,
|
|
83
|
-
outcome TEXT NOT NULL,
|
|
84
|
-
duration_ms INTEGER,
|
|
85
|
-
error_type TEXT,
|
|
86
|
-
error_message TEXT,
|
|
87
|
-
created_at TEXT NOT NULL
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
CREATE TABLE IF NOT EXISTS aggregated_pain_events (
|
|
91
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
92
|
-
workspace TEXT NOT NULL,
|
|
93
|
-
session_id TEXT NOT NULL,
|
|
94
|
-
source TEXT NOT NULL,
|
|
95
|
-
score REAL NOT NULL,
|
|
96
|
-
reason TEXT,
|
|
97
|
-
created_at TEXT NOT NULL
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
CREATE TABLE IF NOT EXISTS aggregated_user_corrections (
|
|
101
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
102
|
-
workspace TEXT NOT NULL,
|
|
103
|
-
session_id TEXT NOT NULL,
|
|
104
|
-
correction_cue TEXT,
|
|
105
|
-
created_at TEXT NOT NULL
|
|
106
|
-
);
|
|
107
|
-
|
|
108
|
-
CREATE TABLE IF NOT EXISTS aggregated_principle_events (
|
|
109
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
110
|
-
workspace TEXT NOT NULL,
|
|
111
|
-
principle_id TEXT,
|
|
112
|
-
event_type TEXT NOT NULL,
|
|
113
|
-
created_at TEXT NOT NULL
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
CREATE TABLE IF NOT EXISTS aggregated_thinking_events (
|
|
117
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
118
|
-
workspace TEXT NOT NULL,
|
|
119
|
-
session_id TEXT NOT NULL,
|
|
120
|
-
model_id TEXT NOT NULL,
|
|
121
|
-
matched_pattern TEXT NOT NULL,
|
|
122
|
-
created_at TEXT NOT NULL
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
CREATE TABLE IF NOT EXISTS aggregated_correction_samples (
|
|
126
|
-
sample_id TEXT PRIMARY KEY,
|
|
127
|
-
workspace TEXT NOT NULL,
|
|
128
|
-
session_id TEXT NOT NULL,
|
|
129
|
-
bad_assistant_turn_id INTEGER NOT NULL,
|
|
130
|
-
quality_score REAL,
|
|
131
|
-
review_status TEXT,
|
|
132
|
-
created_at TEXT NOT NULL
|
|
133
|
-
);
|
|
134
|
-
|
|
135
|
-
CREATE TABLE IF NOT EXISTS aggregated_task_outcomes (
|
|
136
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
137
|
-
workspace TEXT NOT NULL,
|
|
138
|
-
session_id TEXT NOT NULL,
|
|
139
|
-
task_id TEXT,
|
|
140
|
-
outcome TEXT NOT NULL,
|
|
141
|
-
created_at TEXT NOT NULL
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
CREATE TABLE IF NOT EXISTS sync_log (
|
|
145
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
146
|
-
workspace TEXT NOT NULL,
|
|
147
|
-
synced_at TEXT NOT NULL,
|
|
148
|
-
records_synced INTEGER NOT NULL
|
|
149
|
-
);
|
|
150
|
-
|
|
151
|
-
-- Indexes for performance
|
|
152
|
-
CREATE INDEX IF NOT EXISTS idx_tool_calls_workspace ON aggregated_tool_calls(workspace);
|
|
153
|
-
CREATE INDEX IF NOT EXISTS idx_tool_calls_outcome ON aggregated_tool_calls(outcome);
|
|
154
|
-
CREATE INDEX IF NOT EXISTS idx_tool_calls_created ON aggregated_tool_calls(created_at);
|
|
155
|
-
CREATE INDEX IF NOT EXISTS idx_pain_workspace ON aggregated_pain_events(workspace);
|
|
156
|
-
CREATE INDEX IF NOT EXISTS idx_pain_created ON aggregated_pain_events(created_at);
|
|
157
|
-
CREATE INDEX IF NOT EXISTS idx_thinking_workspace ON aggregated_thinking_events(workspace);
|
|
158
|
-
CREATE INDEX IF NOT EXISTS idx_thinking_model ON aggregated_thinking_events(model_id);
|
|
159
|
-
CREATE INDEX IF NOT EXISTS idx_corrections_workspace ON aggregated_correction_samples(workspace);
|
|
160
|
-
CREATE INDEX IF NOT EXISTS idx_sessions_workspace ON aggregated_sessions(workspace);
|
|
161
|
-
`);
|
|
162
|
-
}
|
|
163
|
-
discoverWorkspaces() {
|
|
164
|
-
const openClawDir = os.homedir();
|
|
165
|
-
const workspacesDir = path.join(openClawDir, '.openclaw');
|
|
166
|
-
this.workspaces.length = 0;
|
|
167
|
-
if (!fs.existsSync(workspacesDir))
|
|
168
|
-
return;
|
|
169
|
-
const entries = fs.readdirSync(workspacesDir);
|
|
170
|
-
for (const entry of entries) {
|
|
171
|
-
if (entry.startsWith('workspace-') && entry !== 'workspace') {
|
|
172
|
-
const workspacePath = path.join(workspacesDir, entry);
|
|
173
|
-
const stat = fs.statSync(workspacePath);
|
|
174
|
-
if (stat.isDirectory()) {
|
|
175
|
-
this.workspaces.push({
|
|
176
|
-
name: entry,
|
|
177
|
-
path: workspacePath,
|
|
178
|
-
lastSync: null,
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Sync data from a single workspace into the central database
|
|
186
|
-
*/
|
|
187
|
-
syncWorkspace(workspaceName) {
|
|
188
|
-
const workspace = this.workspaces.find(w => w.name === workspaceName);
|
|
189
|
-
if (!workspace) {
|
|
190
|
-
throw new WorkspaceNotFoundError(workspaceName);
|
|
191
|
-
}
|
|
192
|
-
const trajectoryDbPath = path.join(workspace.path, '.state', 'trajectory.db');
|
|
193
|
-
if (!fs.existsSync(trajectoryDbPath)) {
|
|
194
|
-
return 0;
|
|
195
|
-
}
|
|
196
|
-
const sourceDb = new Database(trajectoryDbPath, { readonly: true });
|
|
197
|
-
let totalSynced = 0;
|
|
198
|
-
try {
|
|
199
|
-
// Sync sessions
|
|
200
|
-
const sessions = sourceDb.prepare(`
|
|
201
|
-
SELECT session_id, started_at, updated_at FROM sessions
|
|
202
|
-
`).all();
|
|
203
|
-
const insertSession = this.db.prepare(`
|
|
204
|
-
INSERT OR REPLACE INTO aggregated_sessions (session_id, workspace, started_at, updated_at)
|
|
205
|
-
VALUES (?, ?, ?, ?)
|
|
206
|
-
`);
|
|
207
|
-
for (const s of sessions) {
|
|
208
|
-
insertSession.run(s.session_id, workspaceName, s.started_at, s.updated_at);
|
|
209
|
-
totalSynced++;
|
|
210
|
-
}
|
|
211
|
-
// Sync tool_calls
|
|
212
|
-
const toolCalls = sourceDb.prepare(`
|
|
213
|
-
SELECT session_id, tool_name, outcome, duration_ms, error_type, error_message, created_at
|
|
214
|
-
FROM tool_calls
|
|
215
|
-
`).all();
|
|
216
|
-
const insertTool = this.db.prepare(`
|
|
217
|
-
INSERT INTO aggregated_tool_calls
|
|
218
|
-
(workspace, session_id, tool_name, outcome, duration_ms, error_type, error_message, created_at)
|
|
219
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
220
|
-
`);
|
|
221
|
-
for (const t of toolCalls) {
|
|
222
|
-
insertTool.run(workspaceName, t.session_id, t.tool_name, t.outcome, t.duration_ms, t.error_type, t.error_message, t.created_at);
|
|
223
|
-
totalSynced++;
|
|
224
|
-
}
|
|
225
|
-
// Sync pain_events
|
|
226
|
-
const painEvents = sourceDb.prepare(`
|
|
227
|
-
SELECT session_id, source, score, reason, created_at FROM pain_events
|
|
228
|
-
`).all();
|
|
229
|
-
const insertPain = this.db.prepare(`
|
|
230
|
-
INSERT INTO aggregated_pain_events (workspace, session_id, source, score, reason, created_at)
|
|
231
|
-
VALUES (?, ?, ?, ?, ?, ?)
|
|
232
|
-
`);
|
|
233
|
-
for (const p of painEvents) {
|
|
234
|
-
insertPain.run(workspaceName, p.session_id, p.source, p.score, p.reason, p.created_at);
|
|
235
|
-
totalSynced++;
|
|
236
|
-
}
|
|
237
|
-
// Sync user corrections
|
|
238
|
-
const corrections = sourceDb.prepare(`
|
|
239
|
-
SELECT session_id, correction_cue, created_at FROM user_turns
|
|
240
|
-
WHERE correction_detected = 1
|
|
241
|
-
`).all();
|
|
242
|
-
const insertCorr = this.db.prepare(`
|
|
243
|
-
INSERT INTO aggregated_user_corrections (workspace, session_id, correction_cue, created_at)
|
|
244
|
-
VALUES (?, ?, ?, ?)
|
|
245
|
-
`);
|
|
246
|
-
for (const c of corrections) {
|
|
247
|
-
insertCorr.run(workspaceName, c.session_id, c.correction_cue, c.created_at);
|
|
248
|
-
totalSynced++;
|
|
249
|
-
}
|
|
250
|
-
// Sync principle_events
|
|
251
|
-
const principles = sourceDb.prepare(`
|
|
252
|
-
SELECT principle_id, event_type, created_at FROM principle_events
|
|
253
|
-
`).all();
|
|
254
|
-
const insertPrinciple = this.db.prepare(`
|
|
255
|
-
INSERT INTO aggregated_principle_events (workspace, principle_id, event_type, created_at)
|
|
256
|
-
VALUES (?, ?, ?, ?)
|
|
257
|
-
`);
|
|
258
|
-
for (const p of principles) {
|
|
259
|
-
insertPrinciple.run(workspaceName, p.principle_id, p.event_type, p.created_at);
|
|
260
|
-
totalSynced++;
|
|
261
|
-
}
|
|
262
|
-
// Sync thinking_model_events (may not exist in older workspaces)
|
|
263
|
-
if (CentralDatabase.tableExists(sourceDb, 'thinking_model_events')) {
|
|
264
|
-
const thinking = sourceDb.prepare(`
|
|
265
|
-
SELECT session_id, model_id, matched_pattern, created_at FROM thinking_model_events
|
|
266
|
-
`).all();
|
|
267
|
-
const insertThinking = this.db.prepare(`
|
|
268
|
-
INSERT INTO aggregated_thinking_events (workspace, session_id, model_id, matched_pattern, created_at)
|
|
269
|
-
VALUES (?, ?, ?, ?, ?)
|
|
270
|
-
`);
|
|
271
|
-
for (const t of thinking) {
|
|
272
|
-
insertThinking.run(workspaceName, t.session_id, t.model_id, t.matched_pattern, t.created_at);
|
|
273
|
-
totalSynced++;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
// Sync correction_samples
|
|
277
|
-
const samples = sourceDb.prepare(`
|
|
278
|
-
SELECT sample_id, session_id, bad_assistant_turn_id, quality_score, review_status, created_at
|
|
279
|
-
FROM correction_samples
|
|
280
|
-
`).all();
|
|
281
|
-
const insertSample = this.db.prepare(`
|
|
282
|
-
INSERT OR REPLACE INTO aggregated_correction_samples
|
|
283
|
-
(sample_id, workspace, session_id, bad_assistant_turn_id, quality_score, review_status, created_at)
|
|
284
|
-
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
285
|
-
`);
|
|
286
|
-
for (const s of samples) {
|
|
287
|
-
insertSample.run(s.sample_id, workspaceName, s.session_id, s.bad_assistant_turn_id, s.quality_score, s.review_status, s.created_at);
|
|
288
|
-
totalSynced++;
|
|
289
|
-
}
|
|
290
|
-
// Sync task_outcomes
|
|
291
|
-
const outcomes = sourceDb.prepare(`
|
|
292
|
-
SELECT session_id, task_id, outcome, created_at FROM task_outcomes
|
|
293
|
-
`).all();
|
|
294
|
-
const insertOutcome = this.db.prepare(`
|
|
295
|
-
INSERT INTO aggregated_task_outcomes (workspace, session_id, task_id, outcome, created_at)
|
|
296
|
-
VALUES (?, ?, ?, ?, ?)
|
|
297
|
-
`);
|
|
298
|
-
for (const o of outcomes) {
|
|
299
|
-
insertOutcome.run(workspaceName, o.session_id, o.task_id, o.outcome, o.created_at);
|
|
300
|
-
totalSynced++;
|
|
301
|
-
}
|
|
302
|
-
// Update last sync time
|
|
303
|
-
this.db.prepare(`
|
|
304
|
-
INSERT OR REPLACE INTO workspaces (name, path, last_sync)
|
|
305
|
-
VALUES (?, ?, datetime('now'))
|
|
306
|
-
`).run(workspaceName, workspace.path);
|
|
307
|
-
// Log sync
|
|
308
|
-
this.db.prepare(`
|
|
309
|
-
INSERT INTO sync_log (workspace, synced_at, records_synced)
|
|
310
|
-
VALUES (?, datetime('now'), ?)
|
|
311
|
-
`).run(workspaceName, totalSynced);
|
|
312
|
-
return totalSynced;
|
|
313
|
-
}
|
|
314
|
-
finally {
|
|
315
|
-
sourceDb.close();
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
syncEnabled() {
|
|
319
|
-
const results = new Map();
|
|
320
|
-
for (const ws of this.getEnabledWorkspaces()) {
|
|
321
|
-
try {
|
|
322
|
-
const count = this.syncWorkspace(ws.name);
|
|
323
|
-
results.set(ws.name, count);
|
|
324
|
-
}
|
|
325
|
-
catch (error) {
|
|
326
|
-
console.error(`Failed to sync workspace ${ws.name}:`, error);
|
|
327
|
-
results.set(ws.name, 0);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
return results;
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* Sync all workspaces (legacy method - syncs all regardless of config)
|
|
334
|
-
*/
|
|
335
|
-
syncAll() {
|
|
336
|
-
const results = new Map();
|
|
337
|
-
for (const ws of this.workspaces) {
|
|
338
|
-
try {
|
|
339
|
-
const count = this.syncWorkspace(ws.name);
|
|
340
|
-
results.set(ws.name, count);
|
|
341
|
-
}
|
|
342
|
-
catch (error) {
|
|
343
|
-
console.error(`Failed to sync workspace ${ws.name}:`, error);
|
|
344
|
-
results.set(ws.name, 0);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
return results;
|
|
348
|
-
}
|
|
349
|
-
getEnabledWorkspaceFilter() {
|
|
350
|
-
const enabled = this.getWorkspaceConfigs().filter(c => c.enabled && c.syncEnabled);
|
|
351
|
-
if (enabled.length === 0)
|
|
352
|
-
return "''";
|
|
353
|
-
return enabled.map(c => `'${c.workspaceName.replace(/'/g, "''")}'`).join(', ');
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Get aggregated overview stats (only from enabled workspaces)
|
|
357
|
-
*/
|
|
358
|
-
getOverviewStats() {
|
|
359
|
-
const filter = this.getEnabledWorkspaceFilter();
|
|
360
|
-
const totalSessions = this.db.prepare(`
|
|
361
|
-
SELECT COUNT(DISTINCT session_id) as count FROM aggregated_sessions
|
|
362
|
-
WHERE workspace IN (${filter})
|
|
363
|
-
`).get();
|
|
364
|
-
const toolStats = this.db.prepare(`
|
|
365
|
-
SELECT
|
|
366
|
-
COUNT(*) as total,
|
|
367
|
-
SUM(CASE WHEN outcome = 'failure' THEN 1 ELSE 0 END) as failures
|
|
368
|
-
FROM aggregated_tool_calls
|
|
369
|
-
WHERE workspace IN (${filter})
|
|
370
|
-
`).get();
|
|
371
|
-
const painEvents = this.db.prepare(`
|
|
372
|
-
SELECT COUNT(*) as count FROM aggregated_pain_events
|
|
373
|
-
WHERE workspace IN (${filter})
|
|
374
|
-
`).get();
|
|
375
|
-
const corrections = this.db.prepare(`
|
|
376
|
-
SELECT COUNT(*) as count FROM aggregated_user_corrections
|
|
377
|
-
WHERE workspace IN (${filter})
|
|
378
|
-
`).get();
|
|
379
|
-
const thinkingEvents = this.db.prepare(`
|
|
380
|
-
SELECT COUNT(*) as count FROM aggregated_thinking_events
|
|
381
|
-
WHERE workspace IN (${filter})
|
|
382
|
-
`).get();
|
|
383
|
-
const sampleStats = this.db.prepare(`
|
|
384
|
-
SELECT
|
|
385
|
-
COUNT(*) as total,
|
|
386
|
-
SUM(CASE WHEN review_status = 'pending' THEN 1 ELSE 0 END) as pending,
|
|
387
|
-
SUM(CASE WHEN review_status = 'approved' THEN 1 ELSE 0 END) as approved,
|
|
388
|
-
SUM(CASE WHEN review_status = 'rejected' THEN 1 ELSE 0 END) as rejected
|
|
389
|
-
FROM aggregated_correction_samples
|
|
390
|
-
WHERE workspace IN (${filter})
|
|
391
|
-
`).get();
|
|
392
|
-
const workspaces = this.db.prepare(`
|
|
393
|
-
SELECT name FROM workspaces ORDER BY name
|
|
394
|
-
`).all();
|
|
395
|
-
const enabledConfigs = this.getWorkspaceConfigs().filter(c => c.enabled && c.syncEnabled);
|
|
396
|
-
const enabledWorkspaceNames = enabledConfigs.map(c => c.workspaceName);
|
|
397
|
-
return {
|
|
398
|
-
totalSessions: totalSessions.count,
|
|
399
|
-
totalToolCalls: toolStats.total,
|
|
400
|
-
totalFailures: toolStats.failures || 0,
|
|
401
|
-
totalPainEvents: painEvents.count,
|
|
402
|
-
totalCorrections: corrections.count,
|
|
403
|
-
totalThinkingEvents: thinkingEvents.count,
|
|
404
|
-
totalSamples: sampleStats.total,
|
|
405
|
-
pendingSamples: sampleStats.pending || 0,
|
|
406
|
-
approvedSamples: sampleStats.approved || 0,
|
|
407
|
-
rejectedSamples: sampleStats.rejected || 0,
|
|
408
|
-
workspaceCount: workspaces.length,
|
|
409
|
-
enabledWorkspaceCount: enabledConfigs.length,
|
|
410
|
-
workspaceNames: workspaces.map(w => w.name),
|
|
411
|
-
enabledWorkspaceNames,
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* Get daily trend data
|
|
416
|
-
*/
|
|
417
|
-
getDailyTrend(days = 7) {
|
|
418
|
-
const cutoffDate = new Date();
|
|
419
|
-
cutoffDate.setDate(cutoffDate.getDate() - days);
|
|
420
|
-
const [cutoffStr] = cutoffDate.toISOString().split('T');
|
|
421
|
-
const toolDaily = this.db.prepare(`
|
|
422
|
-
SELECT
|
|
423
|
-
substr(created_at, 1, 10) as day,
|
|
424
|
-
COUNT(*) as tool_calls,
|
|
425
|
-
SUM(CASE WHEN outcome = 'failure' THEN 1 ELSE 0 END) as failures
|
|
426
|
-
FROM aggregated_tool_calls
|
|
427
|
-
WHERE substr(created_at, 1, 10) >= ?
|
|
428
|
-
GROUP BY substr(created_at, 1, 10)
|
|
429
|
-
ORDER BY day
|
|
430
|
-
`).all(cutoffStr);
|
|
431
|
-
const correctionsDaily = this.db.prepare(`
|
|
432
|
-
SELECT
|
|
433
|
-
substr(created_at, 1, 10) as day,
|
|
434
|
-
COUNT(*) as corrections
|
|
435
|
-
FROM aggregated_user_corrections
|
|
436
|
-
WHERE substr(created_at, 1, 10) >= ?
|
|
437
|
-
GROUP BY substr(created_at, 1, 10)
|
|
438
|
-
`).all(cutoffStr);
|
|
439
|
-
const thinkingDaily = this.db.prepare(`
|
|
440
|
-
SELECT
|
|
441
|
-
substr(created_at, 1, 10) as day,
|
|
442
|
-
COUNT(*) as thinking_turns
|
|
443
|
-
FROM aggregated_thinking_events
|
|
444
|
-
WHERE substr(created_at, 1, 10) >= ?
|
|
445
|
-
GROUP BY substr(created_at, 1, 10)
|
|
446
|
-
`).all(cutoffStr);
|
|
447
|
-
// Merge all trends
|
|
448
|
-
const dayMap = new Map();
|
|
449
|
-
for (const t of toolDaily) {
|
|
450
|
-
dayMap.set(t.day, {
|
|
451
|
-
day: t.day,
|
|
452
|
-
toolCalls: t.tool_calls,
|
|
453
|
-
failures: t.failures || 0,
|
|
454
|
-
userCorrections: 0,
|
|
455
|
-
thinkingTurns: 0,
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
for (const c of correctionsDaily) {
|
|
459
|
-
const existing = dayMap.get(c.day);
|
|
460
|
-
if (existing) {
|
|
461
|
-
existing.userCorrections = c.corrections;
|
|
462
|
-
}
|
|
463
|
-
else {
|
|
464
|
-
dayMap.set(c.day, {
|
|
465
|
-
day: c.day,
|
|
466
|
-
toolCalls: 0,
|
|
467
|
-
failures: 0,
|
|
468
|
-
userCorrections: c.corrections,
|
|
469
|
-
thinkingTurns: 0,
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
for (const t of thinkingDaily) {
|
|
474
|
-
const existing = dayMap.get(t.day);
|
|
475
|
-
if (existing) {
|
|
476
|
-
existing.thinkingTurns = t.thinking_turns;
|
|
477
|
-
}
|
|
478
|
-
else {
|
|
479
|
-
dayMap.set(t.day, {
|
|
480
|
-
day: t.day,
|
|
481
|
-
toolCalls: 0,
|
|
482
|
-
failures: 0,
|
|
483
|
-
userCorrections: 0,
|
|
484
|
-
thinkingTurns: t.thinking_turns,
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
return Array.from(dayMap.values()).sort((a, b) => a.day.localeCompare(b.day));
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* Get top regressions
|
|
492
|
-
*/
|
|
493
|
-
getTopRegressions(limit = 5) {
|
|
494
|
-
return this.db.prepare(`
|
|
495
|
-
SELECT
|
|
496
|
-
tool_name as toolName,
|
|
497
|
-
error_type as errorType,
|
|
498
|
-
COUNT(*) as occurrences
|
|
499
|
-
FROM aggregated_tool_calls
|
|
500
|
-
WHERE outcome = 'failure' AND error_type IS NOT NULL
|
|
501
|
-
GROUP BY tool_name, error_type
|
|
502
|
-
ORDER BY occurrences DESC
|
|
503
|
-
LIMIT ?
|
|
504
|
-
`).all(limit);
|
|
505
|
-
}
|
|
506
|
-
/**
|
|
507
|
-
* Get thinking model stats
|
|
508
|
-
*/
|
|
509
|
-
getThinkingModelStats() {
|
|
510
|
-
const totalModels = this.db.prepare(`
|
|
511
|
-
SELECT COUNT(DISTINCT model_id) as count FROM aggregated_thinking_events
|
|
512
|
-
`).get();
|
|
513
|
-
// Consider a model "active" if it has events in the last 7 days
|
|
514
|
-
const recentDate = new Date();
|
|
515
|
-
recentDate.setDate(recentDate.getDate() - 7);
|
|
516
|
-
const recentStr = recentDate.toISOString();
|
|
517
|
-
const activeModels = this.db.prepare(`
|
|
518
|
-
SELECT COUNT(DISTINCT model_id) as count FROM aggregated_thinking_events
|
|
519
|
-
WHERE created_at >= ?
|
|
520
|
-
`).get(recentStr);
|
|
521
|
-
const totalToolCalls = this.db.prepare(`
|
|
522
|
-
SELECT COUNT(*) as count FROM aggregated_tool_calls
|
|
523
|
-
`).get();
|
|
524
|
-
const models = this.db.prepare(`
|
|
525
|
-
SELECT
|
|
526
|
-
model_id as modelId,
|
|
527
|
-
COUNT(*) as hits
|
|
528
|
-
FROM aggregated_thinking_events
|
|
529
|
-
GROUP BY model_id
|
|
530
|
-
ORDER BY hits DESC
|
|
531
|
-
`).all();
|
|
532
|
-
return {
|
|
533
|
-
totalModels: totalModels.count,
|
|
534
|
-
activeModels: activeModels.count,
|
|
535
|
-
models: models.map(m => ({
|
|
536
|
-
...m,
|
|
537
|
-
coverageRate: totalToolCalls.count > 0 ? m.hits / totalToolCalls.count : 0,
|
|
538
|
-
})),
|
|
539
|
-
};
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* Get workspace list
|
|
543
|
-
*/
|
|
544
|
-
getWorkspaces() {
|
|
545
|
-
return this.db.prepare(`
|
|
546
|
-
SELECT name, path, last_sync as lastSync FROM workspaces ORDER BY name
|
|
547
|
-
`).all();
|
|
548
|
-
}
|
|
549
|
-
getWorkspaceConfigs() {
|
|
550
|
-
const configs = this.db.prepare(`
|
|
551
|
-
SELECT workspace_name, enabled, display_name, sync_enabled
|
|
552
|
-
FROM workspace_config
|
|
553
|
-
ORDER BY workspace_name
|
|
554
|
-
`).all();
|
|
555
|
-
return configs.map(c => ({
|
|
556
|
-
workspaceName: c.workspace_name,
|
|
557
|
-
enabled: c.enabled === 1,
|
|
558
|
-
displayName: c.display_name,
|
|
559
|
-
syncEnabled: c.sync_enabled === 1,
|
|
560
|
-
}));
|
|
561
|
-
}
|
|
562
|
-
updateWorkspaceConfig(workspaceName, updates) {
|
|
563
|
-
const existing = this.db.prepare(`
|
|
564
|
-
SELECT workspace_name FROM workspace_config WHERE workspace_name = ?
|
|
565
|
-
`).get(workspaceName);
|
|
566
|
-
if (existing) {
|
|
567
|
-
const setClauses = ['updated_at = datetime(\'now\')'];
|
|
568
|
-
const params = [];
|
|
569
|
-
if (updates.enabled !== undefined) {
|
|
570
|
-
setClauses.push('enabled = ?');
|
|
571
|
-
params.push(updates.enabled ? 1 : 0);
|
|
572
|
-
}
|
|
573
|
-
if (updates.displayName !== undefined) {
|
|
574
|
-
setClauses.push('display_name = ?');
|
|
575
|
-
params.push(updates.displayName);
|
|
576
|
-
}
|
|
577
|
-
if (updates.syncEnabled !== undefined) {
|
|
578
|
-
setClauses.push('sync_enabled = ?');
|
|
579
|
-
params.push(updates.syncEnabled ? 1 : 0);
|
|
580
|
-
}
|
|
581
|
-
params.push(workspaceName);
|
|
582
|
-
this.db.prepare(`
|
|
583
|
-
UPDATE workspace_config SET ${setClauses.join(', ')} WHERE workspace_name = ?
|
|
584
|
-
`).run(...params);
|
|
585
|
-
}
|
|
586
|
-
else {
|
|
587
|
-
this.db.prepare(`
|
|
588
|
-
INSERT INTO workspace_config (workspace_name, enabled, display_name, sync_enabled)
|
|
589
|
-
VALUES (?, ?, ?, ?)
|
|
590
|
-
`).run(workspaceName, updates.enabled !== undefined ? (updates.enabled ? 1 : 0) : 1, updates.displayName ?? null, updates.syncEnabled !== undefined ? (updates.syncEnabled ? 1 : 0) : 1);
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
isWorkspaceEnabled(workspaceName) {
|
|
594
|
-
const config = this.db.prepare(`
|
|
595
|
-
SELECT enabled, sync_enabled FROM workspace_config WHERE workspace_name = ?
|
|
596
|
-
`).get(workspaceName);
|
|
597
|
-
if (!config)
|
|
598
|
-
return true;
|
|
599
|
-
return config.enabled === 1 && config.sync_enabled === 1;
|
|
600
|
-
}
|
|
601
|
-
getEnabledWorkspaces() {
|
|
602
|
-
return this.workspaces.filter(ws => this.isWorkspaceEnabled(ws.name));
|
|
603
|
-
}
|
|
604
|
-
addCustomWorkspace(name, workspacePath) {
|
|
605
|
-
if (!this.workspaces.find(ws => ws.name === name)) {
|
|
606
|
-
this.workspaces.push({ name, path: workspacePath, lastSync: null });
|
|
607
|
-
this.db.prepare(`
|
|
608
|
-
INSERT INTO workspaces (name, path, last_sync) VALUES (?, ?, NULL)
|
|
609
|
-
`).run(name, workspacePath);
|
|
610
|
-
this.db.prepare(`
|
|
611
|
-
INSERT INTO workspace_config (workspace_name, enabled, display_name, sync_enabled)
|
|
612
|
-
VALUES (?, 1, ?, 1)
|
|
613
|
-
`).run(name, name);
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
removeWorkspace(workspaceName) {
|
|
617
|
-
this.updateWorkspaceConfig(workspaceName, { enabled: false, syncEnabled: false });
|
|
618
|
-
}
|
|
619
|
-
getGlobalConfig(key) {
|
|
620
|
-
const result = this.db.prepare(`
|
|
621
|
-
SELECT value FROM global_config WHERE key = ?
|
|
622
|
-
`).get(key);
|
|
623
|
-
return result?.value ?? null;
|
|
624
|
-
}
|
|
625
|
-
setGlobalConfig(key, value) {
|
|
626
|
-
this.db.prepare(`
|
|
627
|
-
INSERT OR REPLACE INTO global_config (key, value, updated_at)
|
|
628
|
-
VALUES (?, ?, datetime('now'))
|
|
629
|
-
`).run(key, value);
|
|
630
|
-
}
|
|
631
|
-
/**
|
|
632
|
-
* Clear all aggregated data (for testing/reset)
|
|
633
|
-
*/
|
|
634
|
-
clearAll() {
|
|
635
|
-
this.db.exec(`
|
|
636
|
-
DELETE FROM aggregated_sessions;
|
|
637
|
-
DELETE FROM aggregated_tool_calls;
|
|
638
|
-
DELETE FROM aggregated_pain_events;
|
|
639
|
-
DELETE FROM aggregated_user_corrections;
|
|
640
|
-
DELETE FROM aggregated_principle_events;
|
|
641
|
-
DELETE FROM aggregated_thinking_events;
|
|
642
|
-
DELETE FROM aggregated_correction_samples;
|
|
643
|
-
DELETE FROM aggregated_task_outcomes;
|
|
644
|
-
DELETE FROM workspaces;
|
|
645
|
-
DELETE FROM sync_log;
|
|
646
|
-
`);
|
|
647
|
-
}
|
|
648
|
-
/**
|
|
649
|
-
* Get total task outcomes count across enabled workspaces (D-02)
|
|
650
|
-
*/
|
|
651
|
-
getTaskOutcomes() {
|
|
652
|
-
const filter = this.getEnabledWorkspaceFilter();
|
|
653
|
-
const row = this.db.prepare(`
|
|
654
|
-
SELECT COUNT(*) as count FROM aggregated_task_outcomes
|
|
655
|
-
WHERE workspace IN (${filter})
|
|
656
|
-
`).get();
|
|
657
|
-
return row?.count ?? 0;
|
|
658
|
-
}
|
|
659
|
-
/**
|
|
660
|
-
* Get total principle events count across enabled workspaces (D-03)
|
|
661
|
-
*/
|
|
662
|
-
getPrincipleEventCount() {
|
|
663
|
-
const filter = this.getEnabledWorkspaceFilter();
|
|
664
|
-
const row = this.db.prepare(`
|
|
665
|
-
SELECT COUNT(*) as count FROM aggregated_principle_events
|
|
666
|
-
WHERE workspace IN (${filter})
|
|
667
|
-
`).get();
|
|
668
|
-
return row?.count ?? 0;
|
|
669
|
-
}
|
|
670
|
-
/**
|
|
671
|
-
* Get sample counts grouped by review_status across enabled workspaces (D-06)
|
|
672
|
-
*/
|
|
673
|
-
getSampleCountersByStatus() {
|
|
674
|
-
const filter = this.getEnabledWorkspaceFilter();
|
|
675
|
-
const rows = this.db.prepare(`
|
|
676
|
-
SELECT review_status, COUNT(*) as count
|
|
677
|
-
FROM aggregated_correction_samples
|
|
678
|
-
WHERE workspace IN (${filter})
|
|
679
|
-
GROUP BY review_status
|
|
680
|
-
`).all();
|
|
681
|
-
return Object.fromEntries(rows.map(r => [r.review_status, r.count]));
|
|
682
|
-
}
|
|
683
|
-
/**
|
|
684
|
-
* Get top N most recent pending/approved samples across all enabled workspaces (D-04)
|
|
685
|
-
*/
|
|
686
|
-
getSamplePreview(limit = 5) {
|
|
687
|
-
const filter = this.getEnabledWorkspaceFilter();
|
|
688
|
-
const rows = this.db.prepare(`
|
|
689
|
-
SELECT sample_id, session_id, workspace, quality_score, review_status, created_at
|
|
690
|
-
FROM aggregated_correction_samples
|
|
691
|
-
WHERE workspace IN (${filter})
|
|
692
|
-
AND review_status IN ('pending', 'approved')
|
|
693
|
-
ORDER BY created_at DESC
|
|
694
|
-
LIMIT ?
|
|
695
|
-
`).all(limit);
|
|
696
|
-
return rows.map(r => ({
|
|
697
|
-
sampleId: r.sample_id,
|
|
698
|
-
sessionId: r.session_id,
|
|
699
|
-
workspace: r.workspace,
|
|
700
|
-
qualityScore: r.quality_score ?? 0,
|
|
701
|
-
reviewStatus: r.review_status ?? 'pending',
|
|
702
|
-
createdAt: r.created_at,
|
|
703
|
-
}));
|
|
704
|
-
}
|
|
705
|
-
/**
|
|
706
|
-
* Get the most recent lastSync timestamp across all workspaces (D-05)
|
|
707
|
-
*/
|
|
708
|
-
getMostRecentSync() {
|
|
709
|
-
const row = this.db.prepare(`
|
|
710
|
-
SELECT MAX(last_sync) as lastSync FROM workspaces
|
|
711
|
-
`).get();
|
|
712
|
-
return row?.lastSync ?? null;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
// Singleton instance
|
|
716
|
-
let centralDbInstance = null;
|
|
717
|
-
export function getCentralDatabase() {
|
|
718
|
-
if (!centralDbInstance || centralDbInstance.isClosed) {
|
|
719
|
-
centralDbInstance = new CentralDatabase();
|
|
720
|
-
}
|
|
721
|
-
return centralDbInstance;
|
|
722
|
-
}
|
|
723
|
-
/**
|
|
724
|
-
* Reset the singleton instance. Used for testing.
|
|
725
|
-
*/
|
|
726
|
-
export function resetCentralDatabase() {
|
|
727
|
-
if (centralDbInstance && !centralDbInstance.isClosed) {
|
|
728
|
-
centralDbInstance.dispose();
|
|
729
|
-
}
|
|
730
|
-
centralDbInstance = null;
|
|
731
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CentralSyncService - Periodically sync workspace data to central database.
|
|
3
|
-
*
|
|
4
|
-
* Ensures thinking_model_events and other workspace data are aggregated
|
|
5
|
-
* into the central database for cross-workspace queries and WebUI display.
|
|
6
|
-
*/
|
|
7
|
-
import type { OpenClawPluginService } from '../openclaw-sdk.js';
|
|
8
|
-
export declare const CentralSyncService: OpenClawPluginService;
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CentralSyncService - Periodically sync workspace data to central database.
|
|
3
|
-
*
|
|
4
|
-
* Ensures thinking_model_events and other workspace data are aggregated
|
|
5
|
-
* into the central database for cross-workspace queries and WebUI display.
|
|
6
|
-
*/
|
|
7
|
-
import { CentralDatabase } from './central-database.js';
|
|
8
|
-
import { WORKFLOW_TTL_MS } from '../config/defaults/runtime.js';
|
|
9
|
-
let syncInterval = null;
|
|
10
|
-
let logger = undefined;
|
|
11
|
-
let centralDb = null;
|
|
12
|
-
async function runSyncCycle() {
|
|
13
|
-
if (!centralDb) {
|
|
14
|
-
logger?.warn?.('[PD:CentralSync] CentralDatabase not initialized, skipping sync');
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
try {
|
|
18
|
-
const results = centralDb.syncAll();
|
|
19
|
-
const totalSynced = Array.from(results.values()).reduce((sum, count) => sum + count, 0);
|
|
20
|
-
const workspacesSynced = Array.from(results.entries())
|
|
21
|
-
.filter(([, count]) => count > 0)
|
|
22
|
-
.map(([name, count]) => `${name}:${count}`)
|
|
23
|
-
.join(', ');
|
|
24
|
-
if (totalSynced > 0) {
|
|
25
|
-
logger?.info?.(`[PD:CentralSync] Synced ${totalSynced} records from workspaces: ${workspacesSynced}`);
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
logger?.debug?.(`[PD:CentralSync] No new records to sync`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
catch (err) {
|
|
32
|
-
logger?.error?.(`[PD:CentralSync] Sync failed: ${String(err)}`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
export const CentralSyncService = {
|
|
36
|
-
id: 'principles-central-sync',
|
|
37
|
-
async start(ctx) {
|
|
38
|
-
const { logger: ctxLogger, config } = ctx;
|
|
39
|
-
logger = ctxLogger;
|
|
40
|
-
const { intervals } = config;
|
|
41
|
-
const intervalMs = intervals?.central_sync_ms ?? WORKFLOW_TTL_MS;
|
|
42
|
-
// Initialize CentralDatabase
|
|
43
|
-
centralDb = new CentralDatabase();
|
|
44
|
-
// Initial sync on start
|
|
45
|
-
logger?.info?.(`[PD:CentralSync] Starting with interval ${intervalMs}ms`);
|
|
46
|
-
await runSyncCycle();
|
|
47
|
-
// Schedule periodic sync
|
|
48
|
-
syncInterval = setInterval(runSyncCycle, intervalMs);
|
|
49
|
-
// Don't keep the process alive just for this timer
|
|
50
|
-
syncInterval.unref();
|
|
51
|
-
logger?.info?.(`[PD:CentralSync] Service started, syncing every ${intervalMs / 1000}s`);
|
|
52
|
-
},
|
|
53
|
-
async stop(ctx) {
|
|
54
|
-
if (syncInterval) {
|
|
55
|
-
clearInterval(syncInterval);
|
|
56
|
-
syncInterval = null;
|
|
57
|
-
}
|
|
58
|
-
// Final sync on stop
|
|
59
|
-
if (centralDb) {
|
|
60
|
-
try {
|
|
61
|
-
centralDb.syncAll();
|
|
62
|
-
ctx.logger?.info?.(`[PD:CentralSync] Final sync completed`);
|
|
63
|
-
}
|
|
64
|
-
catch (err) {
|
|
65
|
-
ctx.logger?.error?.(`[PD:CentralSync] Final sync failed: ${String(err)}`);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
centralDb = null;
|
|
69
|
-
ctx.logger?.info?.(`[PD:CentralSync] Service stopped`);
|
|
70
|
-
},
|
|
71
|
-
};
|