@powersync/diagnostics 0.0.0-dev-20261020125654 → 0.0.0-dev-20261112182545
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/README.md +50 -2
- package/dist/devtools/assets/powersync-BXfuXqfT.js +184 -0
- package/dist/devtools/assets/powersync-CpTiA65L.css +1 -0
- package/dist/devtools/powersync.html +24 -0
- package/dist/rozenite.json +14 -0
- package/lib/src/agent.d.ts +3 -1
- package/lib/src/agent.js +36 -8
- package/lib/src/agent.js.map +1 -1
- package/lib/src/client.js +4 -4
- package/lib/src/client.js.map +1 -1
- package/lib/src/definition.js +11 -17
- package/lib/src/definition.js.map +1 -1
- package/lib/src/js-agent.d.ts +6 -13
- package/lib/src/js-agent.js +23 -10
- package/lib/src/js-agent.js.map +1 -1
- package/lib/src/node.d.ts +1 -1
- package/lib/src/node.js +2 -2
- package/lib/src/node.js.map +1 -1
- package/lib/src/page.js +2 -3
- package/lib/src/page.js.map +1 -1
- package/lib/src/react-native/agent-tools.d.ts +3 -0
- package/lib/src/react-native/agent-tools.js +137 -0
- package/lib/src/react-native/agent-tools.js.map +1 -0
- package/lib/src/react-native/protocol.d.ts +22 -0
- package/lib/src/react-native/protocol.js +5 -0
- package/lib/src/react-native/protocol.js.map +1 -0
- package/lib/src/react-native/usePowerSyncDevTools.d.ts +7 -0
- package/lib/src/react-native/usePowerSyncDevTools.js +56 -0
- package/lib/src/react-native/usePowerSyncDevTools.js.map +1 -0
- package/lib/src/react-native.d.ts +7 -0
- package/lib/src/react-native.js +8 -0
- package/lib/src/react-native.js.map +1 -0
- package/lib/src/rpc-types.d.ts +1 -1
- package/lib/src/tools.d.ts +19 -0
- package/lib/src/tools.js +24 -0
- package/lib/src/tools.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +37 -13
- package/src/agent.ts +45 -17
- package/src/client.ts +4 -5
- package/src/definition.ts +11 -17
- package/src/js-agent.ts +33 -27
- package/src/node.ts +3 -3
- package/src/page.ts +2 -4
- package/src/react-native/agent-tools.ts +148 -0
- package/src/react-native/protocol.ts +33 -0
- package/src/react-native/usePowerSyncDevTools.ts +59 -0
- package/src/react-native.ts +14 -0
- package/src/rpc-types.ts +1 -1
- package/src/tools.ts +35 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/diagnostics",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-20261112182545",
|
|
4
4
|
"description": "PowerSync DevTools: a devframe definition that serves live PowerSync diagnostics as a Vite DevTools dock, a node dev server, and MCP tools.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/src/index.js",
|
|
@@ -33,11 +33,16 @@
|
|
|
33
33
|
"./agent": {
|
|
34
34
|
"types": "./lib/src/agent.d.ts",
|
|
35
35
|
"default": "./lib/src/agent.js"
|
|
36
|
+
},
|
|
37
|
+
"./react-native": {
|
|
38
|
+
"types": "./lib/src/react-native.d.ts",
|
|
39
|
+
"default": "./lib/src/react-native.js"
|
|
36
40
|
}
|
|
37
41
|
},
|
|
38
42
|
"author": "PowerSync",
|
|
39
43
|
"license": "Apache-2.0",
|
|
40
44
|
"files": [
|
|
45
|
+
"dist",
|
|
41
46
|
"lib",
|
|
42
47
|
"src"
|
|
43
48
|
],
|
|
@@ -54,9 +59,12 @@
|
|
|
54
59
|
},
|
|
55
60
|
"homepage": "https://docs.powersync.com",
|
|
56
61
|
"peerDependencies": {
|
|
57
|
-
"@powersync/common": "
|
|
58
|
-
"@powersync/web": "
|
|
62
|
+
"@powersync/common": "^2.5.0",
|
|
63
|
+
"@powersync/web": "^2.7.0",
|
|
64
|
+
"@rozenite/agent-bridge": "^2.5.0",
|
|
65
|
+
"@rozenite/plugin-bridge": "^2.5.0",
|
|
59
66
|
"@vitejs/devtools-kit": ">=0.7.5",
|
|
67
|
+
"react": ">=18",
|
|
60
68
|
"vite": ">=7.0.0"
|
|
61
69
|
},
|
|
62
70
|
"peerDependenciesMeta": {
|
|
@@ -66,6 +74,15 @@
|
|
|
66
74
|
"@powersync/web": {
|
|
67
75
|
"optional": true
|
|
68
76
|
},
|
|
77
|
+
"@rozenite/agent-bridge": {
|
|
78
|
+
"optional": true
|
|
79
|
+
},
|
|
80
|
+
"@rozenite/plugin-bridge": {
|
|
81
|
+
"optional": true
|
|
82
|
+
},
|
|
83
|
+
"react": {
|
|
84
|
+
"optional": true
|
|
85
|
+
},
|
|
69
86
|
"@vitejs/devtools-kit": {
|
|
70
87
|
"optional": true
|
|
71
88
|
},
|
|
@@ -74,25 +91,32 @@
|
|
|
74
91
|
}
|
|
75
92
|
},
|
|
76
93
|
"dependencies": {
|
|
77
|
-
"@devframes/agentic": "1.
|
|
78
|
-
"devframe": "1.
|
|
94
|
+
"@devframes/agentic": "^1.2.6",
|
|
95
|
+
"devframe": "^1.2.6",
|
|
79
96
|
"zod": "^4.6.5",
|
|
80
|
-
"@powersync/diagnostics-core": "0.0.0-dev-
|
|
81
|
-
"@powersync/diagnostics-ui": "0.0.0-dev-
|
|
97
|
+
"@powersync/diagnostics-core": "0.0.0-dev-20261112182545",
|
|
98
|
+
"@powersync/diagnostics-ui": "0.0.0-dev-20261112182545"
|
|
82
99
|
},
|
|
83
100
|
"devDependencies": {
|
|
84
|
-
"@devframes/hub": "1.
|
|
101
|
+
"@devframes/hub": "^1.2.6",
|
|
102
|
+
"@rozenite/agent-bridge": "^2.5.0",
|
|
103
|
+
"@rozenite/plugin-bridge": "^2.5.0",
|
|
85
104
|
"@types/node": "^24.0.0",
|
|
105
|
+
"@types/react": "19.2.14",
|
|
86
106
|
"@vitejs/devtools-kit": "^0.7.5",
|
|
107
|
+
"react": "19.3.0",
|
|
87
108
|
"typescript": "^6.0.3",
|
|
88
109
|
"vite": "^7.3.6",
|
|
89
|
-
"
|
|
90
|
-
"@powersync/
|
|
110
|
+
"vue": "3.5.43",
|
|
111
|
+
"@powersync/common": "2.5.0",
|
|
112
|
+
"@powersync/shared-internals": "1.6.0",
|
|
113
|
+
"@powersync/web": "2.7.0"
|
|
91
114
|
},
|
|
92
115
|
"scripts": {
|
|
93
|
-
"build": "tsc -b",
|
|
94
|
-
"build:prod": "tsc -b",
|
|
95
|
-
"
|
|
116
|
+
"build": "tsc -b && pnpm build:rozenite",
|
|
117
|
+
"build:prod": "tsc -b && pnpm build:rozenite",
|
|
118
|
+
"build:rozenite": "tsc -p tsconfig.rozenite.json && vite build --config vite.rozenite.config.ts",
|
|
119
|
+
"clean": "rm -rf dist lib tsconfig.tsbuildinfo",
|
|
96
120
|
"watch": "tsc --build -w"
|
|
97
121
|
}
|
|
98
122
|
}
|
package/src/agent.ts
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
* package's `./agent` entry, for a host that opens the database and embeds the DevTools page itself;
|
|
4
4
|
* the dock and node entries build their agents here too.
|
|
5
5
|
*/
|
|
6
|
-
import type { CommonPowerSyncDatabase
|
|
6
|
+
import type { CommonPowerSyncDatabase } from '@powersync/common';
|
|
7
7
|
import type { CoreDiagnosticsEvent, SdkIntegration, Unsubscribe } from '@powersync/diagnostics-core';
|
|
8
8
|
import { JsAgent, type CoreEventSource } from './js-agent.js';
|
|
9
|
+
import type { BasePowerSyncDatabase } from '@powersync/shared-internals';
|
|
9
10
|
|
|
10
11
|
// The iframe hand-off and the protocol types an embedding host needs, so the host never imports the protocol package.
|
|
11
12
|
export { attachIframe } from '@powersync/diagnostics-core';
|
|
@@ -13,20 +14,18 @@ export type {
|
|
|
13
14
|
ActionRequest,
|
|
14
15
|
AttachIframeOptions,
|
|
15
16
|
DiagnosticsEvent,
|
|
17
|
+
ProtocolInfo,
|
|
16
18
|
SdkIntegration,
|
|
19
|
+
StreamDefinition,
|
|
17
20
|
SyncState
|
|
18
21
|
} from '@powersync/diagnostics-core';
|
|
19
22
|
|
|
20
23
|
/** The sync client broadcasts core diagnostics events on this channel (see `emitDiagnostics`). */
|
|
21
24
|
const CORE_EVENTS_CHANNEL = 'powersync-diagnostics-events';
|
|
22
25
|
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
*/
|
|
27
|
-
interface DiagnosableDatabase extends CommonPowerSyncDatabase {
|
|
28
|
-
readonly connector: PowerSyncBackendConnector | null | undefined;
|
|
29
|
-
readonly connectionOptions: SyncOptions | null | undefined;
|
|
26
|
+
/** The SDK label for the database, or `fallback` on SDK releases that do not report it. */
|
|
27
|
+
export function databaseSdk(database: CommonPowerSyncDatabase, fallback: string): string {
|
|
28
|
+
return (database as Partial<BasePowerSyncDatabase>).sdkName ?? fallback;
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
/** Consumes the core diagnostics events the sync client broadcasts (from a worker or in-process). */
|
|
@@ -44,15 +43,44 @@ class BroadcastCoreEvents implements CoreEventSource {
|
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
45
|
|
|
46
|
+
/** A database that hands core diagnostics events to listeners directly (React Native). */
|
|
47
|
+
interface DiagnosticsListenerDatabase {
|
|
48
|
+
registerDiagnosticsListener(listener: (event: CoreDiagnosticsEvent) => void): () => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function hasDiagnosticsListener(database: object): database is DiagnosticsListenerDatabase {
|
|
52
|
+
return typeof (database as Partial<DiagnosticsListenerDatabase>).registerDiagnosticsListener === 'function';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Consumes the core diagnostics events a database hands to its listeners. */
|
|
56
|
+
class ListenerCoreEvents implements CoreEventSource {
|
|
57
|
+
private removers = new Set<Unsubscribe>();
|
|
58
|
+
|
|
59
|
+
constructor(private readonly database: DiagnosticsListenerDatabase) {}
|
|
60
|
+
|
|
61
|
+
onEvent(handler: (event: CoreDiagnosticsEvent) => void): Unsubscribe {
|
|
62
|
+
const remove = this.database.registerDiagnosticsListener(handler);
|
|
63
|
+
this.removers.add(remove);
|
|
64
|
+
return () => {
|
|
65
|
+
this.removers.delete(remove);
|
|
66
|
+
remove();
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
dispose(): void {
|
|
71
|
+
for (const remove of this.removers) remove();
|
|
72
|
+
this.removers.clear();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function coreEventSource(database: object): CoreEventSource | undefined {
|
|
77
|
+
if (hasDiagnosticsListener(database)) return new ListenerCoreEvents(database);
|
|
78
|
+
if (typeof BroadcastChannel === 'function') return new BroadcastCoreEvents();
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
47
82
|
/** Builds the JavaScript integration over a live database. The agent reads the connection from the database itself. */
|
|
48
83
|
export function createIntegration(database: CommonPowerSyncDatabase, sdk: string): SdkIntegration {
|
|
49
|
-
const db = database as
|
|
50
|
-
return new JsAgent(db, {
|
|
51
|
-
sdk,
|
|
52
|
-
coreEvents: typeof BroadcastChannel === 'function' ? new BroadcastCoreEvents() : undefined,
|
|
53
|
-
connection: {
|
|
54
|
-
getConnector: () => db.connector ?? null,
|
|
55
|
-
getConnectionOptions: () => db.connectionOptions ?? null
|
|
56
|
-
}
|
|
57
|
-
});
|
|
84
|
+
const db = database as BasePowerSyncDatabase;
|
|
85
|
+
return new JsAgent(db, { sdk, coreEvents: coreEventSource(db) });
|
|
58
86
|
}
|
package/src/client.ts
CHANGED
|
@@ -5,11 +5,9 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { DockClientScriptContext } from '@devframes/hub/client';
|
|
7
7
|
import { observeRegisteredDatabases } from '@powersync/web/devtools';
|
|
8
|
-
import { createIntegration } from './agent.js';
|
|
8
|
+
import { createIntegration, databaseSdk } from './agent.js';
|
|
9
9
|
import { createAgentServer } from './agent-server.js';
|
|
10
10
|
|
|
11
|
-
const SDK = '@powersync/web';
|
|
12
|
-
|
|
13
11
|
export default function setup(context: DockClientScriptContext): void {
|
|
14
12
|
const server = createAgentServer(context.rpc);
|
|
15
13
|
let counter = 0;
|
|
@@ -31,7 +29,8 @@ export default function setup(context: DockClientScriptContext): void {
|
|
|
31
29
|
return;
|
|
32
30
|
}
|
|
33
31
|
if (currentId) return;
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
const sdk = databaseSdk(database, '@powersync/web');
|
|
33
|
+
currentId = `${sdk.replace(/^@powersync\//, '')}-${++counter}`;
|
|
34
|
+
void server.serve(currentId, createIntegration(database, sdk), sdk);
|
|
36
35
|
});
|
|
37
36
|
}
|
package/src/definition.ts
CHANGED
|
@@ -25,6 +25,7 @@ import type {
|
|
|
25
25
|
} from '@powersync/diagnostics-core';
|
|
26
26
|
import { DEVFRAME_ID, HEARTBEAT_MS, UI_ROUTE } from './constants.js';
|
|
27
27
|
import type { SetupSdk, SourceInfo } from './rpc-types.js';
|
|
28
|
+
import { ACTION_NAMES, ARGUMENT_DESCRIPTIONS, TOOL_DESCRIPTIONS } from './tools.js';
|
|
28
29
|
|
|
29
30
|
export type { SetupSdk, SourceInfo } from './rpc-types.js';
|
|
30
31
|
|
|
@@ -250,20 +251,13 @@ const sourceIdArg = z
|
|
|
250
251
|
.describe('Id of the attached database, from powersync_sources. Null for the first one.');
|
|
251
252
|
const queryParamsArg = z
|
|
252
253
|
.object({
|
|
253
|
-
sql: z.string().describe(
|
|
254
|
-
params: z.array(z.unknown()).optional().describe(
|
|
254
|
+
sql: z.string().describe(ARGUMENT_DESCRIPTIONS.sql),
|
|
255
|
+
params: z.array(z.unknown()).optional().describe(ARGUMENT_DESCRIPTIONS.params)
|
|
255
256
|
})
|
|
256
257
|
.describe('The query.');
|
|
257
258
|
const actionRequestArg = z
|
|
258
259
|
.object({
|
|
259
|
-
action: z.enum(
|
|
260
|
-
'reconnect',
|
|
261
|
-
'disconnect',
|
|
262
|
-
'clearData',
|
|
263
|
-
'requestCheckpoint',
|
|
264
|
-
'subscribeStream',
|
|
265
|
-
'unsubscribeStream'
|
|
266
|
-
]),
|
|
260
|
+
action: z.enum(ACTION_NAMES),
|
|
267
261
|
args: z
|
|
268
262
|
.object({
|
|
269
263
|
name: z.string(),
|
|
@@ -272,7 +266,7 @@ const actionRequestArg = z
|
|
|
272
266
|
priority: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]).optional()
|
|
273
267
|
})
|
|
274
268
|
.optional()
|
|
275
|
-
.describe(
|
|
269
|
+
.describe(ARGUMENT_DESCRIPTIONS.actionArgs)
|
|
276
270
|
})
|
|
277
271
|
.describe('The action to run.');
|
|
278
272
|
const anyResult = z.unknown();
|
|
@@ -391,7 +385,7 @@ export const definition = defineDevframe({
|
|
|
391
385
|
returns: anyResult,
|
|
392
386
|
agent: {
|
|
393
387
|
description:
|
|
394
|
-
|
|
388
|
+
`${TOOL_DESCRIPTIONS.query} Arguments: arg0 = { sql, params? }, arg1 = database id or null for the first one.`,
|
|
395
389
|
safety: 'action'
|
|
396
390
|
},
|
|
397
391
|
handler: (params, sourceId) => pickSource(sourceId).integration.runQuery(params as QueryParams)
|
|
@@ -406,7 +400,7 @@ export const definition = defineDevframe({
|
|
|
406
400
|
returns: anyResult,
|
|
407
401
|
agent: {
|
|
408
402
|
description:
|
|
409
|
-
|
|
403
|
+
`${TOOL_DESCRIPTIONS.schema} Argument: arg0 = database id or null for the first one.`
|
|
410
404
|
},
|
|
411
405
|
handler: (sourceId) => pickSource(sourceId).integration.getSchema()
|
|
412
406
|
})
|
|
@@ -420,7 +414,7 @@ export const definition = defineDevframe({
|
|
|
420
414
|
returns: anyResult,
|
|
421
415
|
agent: {
|
|
422
416
|
description:
|
|
423
|
-
|
|
417
|
+
`${TOOL_DESCRIPTIONS.info} Argument: arg0 = database id or null for the first one.`
|
|
424
418
|
},
|
|
425
419
|
handler: (sourceId) => pickSource(sourceId).integration.getInfo()
|
|
426
420
|
})
|
|
@@ -434,7 +428,7 @@ export const definition = defineDevframe({
|
|
|
434
428
|
returns: anyResult,
|
|
435
429
|
agent: {
|
|
436
430
|
description:
|
|
437
|
-
|
|
431
|
+
`${TOOL_DESCRIPTIONS.status} Argument: arg0 = database id or null for the first one.`
|
|
438
432
|
},
|
|
439
433
|
handler: async (sourceId) => latestSnapshot(sourceId, 'status')
|
|
440
434
|
})
|
|
@@ -448,7 +442,7 @@ export const definition = defineDevframe({
|
|
|
448
442
|
returns: anyResult,
|
|
449
443
|
agent: {
|
|
450
444
|
description:
|
|
451
|
-
'
|
|
445
|
+
`${TOOL_DESCRIPTIONS['upload-queue']} Argument: arg0 = database id or null for the first one.`
|
|
452
446
|
},
|
|
453
447
|
handler: async (sourceId) => latestSnapshot(sourceId, 'uploadQueue')
|
|
454
448
|
})
|
|
@@ -462,7 +456,7 @@ export const definition = defineDevframe({
|
|
|
462
456
|
returns: anyResult,
|
|
463
457
|
agent: {
|
|
464
458
|
description:
|
|
465
|
-
|
|
459
|
+
`${TOOL_DESCRIPTIONS.action} Arguments: arg0 = { action, args? }, arg1 = database id or null for the first one.`,
|
|
466
460
|
safety: 'destructive'
|
|
467
461
|
},
|
|
468
462
|
handler: (request, sourceId) => pickSource(sourceId).integration.action(request as ActionRequest)
|
package/src/js-agent.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
CommonPowerSyncDatabase,
|
|
3
2
|
LogLevels,
|
|
4
3
|
LogRecord as SdkLogRecord,
|
|
5
|
-
PowerSyncBackendConnector,
|
|
6
4
|
SyncOptions,
|
|
7
5
|
SyncStatus,
|
|
8
6
|
SyncStreamSubscription
|
|
@@ -19,11 +17,14 @@ import type {
|
|
|
19
17
|
SchemaPayload,
|
|
20
18
|
SdkIntegration,
|
|
21
19
|
StreamActionArgs,
|
|
22
|
-
SyncState,
|
|
23
20
|
Unsubscribe,
|
|
24
21
|
UploadQueueState
|
|
25
22
|
} from '@powersync/diagnostics-core';
|
|
26
23
|
import { toStreamStates, toSyncState } from './sync-state.js';
|
|
24
|
+
import type { BasePowerSyncDatabase, InternalConnector } from '@powersync/shared-internals';
|
|
25
|
+
|
|
26
|
+
/** SQL that only reads, so it can run on a read connection. */
|
|
27
|
+
const READ_STATEMENT = /^\s*(select|explain)\b/i;
|
|
27
28
|
|
|
28
29
|
/** Delivers the SQLite core's diagnostics events to the agent. How they arrive is runtime-specific. */
|
|
29
30
|
export interface CoreEventSource {
|
|
@@ -31,24 +32,15 @@ export interface CoreEventSource {
|
|
|
31
32
|
dispose(): void;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
/**
|
|
35
|
-
* Read access to the live connection. The connector and options are not on the database's public
|
|
36
|
-
* interface, so the code that installs the agent (which can see the concrete database) hands them in.
|
|
37
|
-
*/
|
|
38
|
-
export interface ConnectionAccess {
|
|
39
|
-
getConnector(): PowerSyncBackendConnector | null;
|
|
40
|
-
getConnectionOptions(): SyncOptions | null;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
35
|
export interface JsAgentOptions {
|
|
44
36
|
/** A label for the SDK behind this agent, e.g. `@powersync/web`. */
|
|
45
37
|
sdk?: string;
|
|
38
|
+
/** If known, the path to the database */
|
|
39
|
+
databasePath?: string;
|
|
46
40
|
/** Debounce for re-reading bucket stats after internal-table changes. */
|
|
47
41
|
bucketThrottleMs?: number;
|
|
48
42
|
/** Core diagnostics events (per-bucket `target_count`). Omit when core diagnostics are off. */
|
|
49
43
|
coreEvents?: CoreEventSource;
|
|
50
|
-
/** Read access to the live connection; omit if the runtime cannot expose it. */
|
|
51
|
-
connection?: ConnectionAccess;
|
|
52
44
|
}
|
|
53
45
|
|
|
54
46
|
/**
|
|
@@ -63,36 +55,42 @@ export class JsAgent implements SdkIntegration {
|
|
|
63
55
|
/** Debug subscriptions created through `action`, keyed by name + params, so they can be released. */
|
|
64
56
|
private streamHandles = new Map<string, SyncStreamSubscription>();
|
|
65
57
|
// Retained so reconnect / clear work after a disconnect, when the connection no longer exposes them.
|
|
66
|
-
private lastConnector:
|
|
58
|
+
private lastConnector: InternalConnector | null = null;
|
|
67
59
|
private lastConnectionOptions: SyncOptions | null = null;
|
|
68
60
|
// Per-bucket total operation counts from the core diagnostics stream (not stored in ps_buckets).
|
|
69
61
|
private bucketTargets = new Map<string, number>();
|
|
70
62
|
|
|
71
63
|
constructor(
|
|
72
|
-
private db:
|
|
64
|
+
private readonly db: BasePowerSyncDatabase,
|
|
73
65
|
private options: JsAgentOptions = {}
|
|
74
66
|
) {}
|
|
75
67
|
|
|
76
68
|
// --- SdkIntegration ---
|
|
77
69
|
|
|
78
70
|
async runQuery({ sql, params }: QueryParams): Promise<QueryResult> {
|
|
79
|
-
const records = await this.
|
|
71
|
+
const records = await this.queryRecords(sql, params);
|
|
80
72
|
const columns = records.length > 0 ? Object.keys(records[0]) : [];
|
|
81
73
|
return { columns, rows: records.map((record) => columns.map((column) => record[column])) };
|
|
82
74
|
}
|
|
83
75
|
|
|
76
|
+
// Node and React Native open their read connections read-only, so only plain reads go to one.
|
|
77
|
+
// Everything else (INSERT, UPDATE, DELETE, WITH, PRAGMA, …) runs on the write connection.
|
|
78
|
+
private async queryRecords(sql: string, params?: unknown[]): Promise<Record<string, unknown>[]> {
|
|
79
|
+
if (READ_STATEMENT.test(sql)) return this.db.getAll<Record<string, unknown>>(sql, params);
|
|
80
|
+
const result = await this.db.execute(sql, params);
|
|
81
|
+
return (result.rows?._array ?? []) as Record<string, unknown>[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
84
|
async getSchema(): Promise<SchemaPayload> {
|
|
85
85
|
// The core payload: what the client already sends to `powersync_replace_schema`.
|
|
86
86
|
return this.db.schema.toJSON() as SchemaPayload;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
async getInfo(): Promise<ProtocolInfo> {
|
|
90
|
-
const connection = this.options.connection;
|
|
91
|
-
|
|
92
90
|
let endpoint: string | null = null;
|
|
93
91
|
let token: string | null = null;
|
|
94
92
|
try {
|
|
95
|
-
const credentials = await
|
|
93
|
+
const credentials = await this.db.connector?.fetchCredentials?.();
|
|
96
94
|
endpoint = credentials?.endpoint ?? null;
|
|
97
95
|
token = credentials?.token ?? null;
|
|
98
96
|
} catch {
|
|
@@ -114,7 +112,7 @@ export class JsAgent implements SdkIntegration {
|
|
|
114
112
|
// Non-fatal.
|
|
115
113
|
}
|
|
116
114
|
|
|
117
|
-
const connectionOptions =
|
|
115
|
+
const connectionOptions = this.db.connectionOptions ?? null;
|
|
118
116
|
return {
|
|
119
117
|
endpoint,
|
|
120
118
|
token,
|
|
@@ -123,7 +121,8 @@ export class JsAgent implements SdkIntegration {
|
|
|
123
121
|
params: connectionOptions?.params ?? null,
|
|
124
122
|
connected: this.db.currentStatus?.connected ?? false,
|
|
125
123
|
sqliteCoreVersion,
|
|
126
|
-
sdk: this.options.sdk ?? null
|
|
124
|
+
sdk: this.options.sdk ?? null,
|
|
125
|
+
databasePath: this.options.databasePath ?? null
|
|
127
126
|
};
|
|
128
127
|
}
|
|
129
128
|
|
|
@@ -160,7 +159,11 @@ export class JsAgent implements SdkIntegration {
|
|
|
160
159
|
const { name, params, ttl, priority } = request.args as StreamActionArgs;
|
|
161
160
|
// TTL defaults to 0 so a forgotten debug subscription is evicted as soon as it is released.
|
|
162
161
|
const handle = await this.db.syncStream(name, params).subscribe({ ttl: ttl ?? 0, priority });
|
|
163
|
-
|
|
162
|
+
const key = streamKey(name, params);
|
|
163
|
+
// Subscribing again returns another handle. Releasing the old one after the new one holds the stream
|
|
164
|
+
// keeps a single handle, so Unsubscribe releases the subscription.
|
|
165
|
+
this.streamHandles.get(key)?.unsubscribe();
|
|
166
|
+
this.streamHandles.set(key, handle);
|
|
164
167
|
break;
|
|
165
168
|
}
|
|
166
169
|
case 'unsubscribeStream': {
|
|
@@ -345,17 +348,20 @@ export class JsAgent implements SdkIntegration {
|
|
|
345
348
|
}
|
|
346
349
|
|
|
347
350
|
private captureConnection(): void {
|
|
348
|
-
const
|
|
349
|
-
const connector = connection?.getConnector() ?? null;
|
|
351
|
+
const connector = this.db.connector ?? null;
|
|
350
352
|
if (connector) {
|
|
351
353
|
this.lastConnector = connector;
|
|
352
|
-
this.lastConnectionOptions =
|
|
354
|
+
this.lastConnectionOptions = this.db.connectionOptions ?? null;
|
|
353
355
|
}
|
|
354
356
|
}
|
|
355
357
|
|
|
356
358
|
private async reconnect(): Promise<void> {
|
|
357
359
|
if (this.lastConnector) {
|
|
358
|
-
await this.db.connect(
|
|
360
|
+
await this.db.connectionManager.connect(
|
|
361
|
+
this.lastConnector,
|
|
362
|
+
this.lastConnectionOptions ?? {},
|
|
363
|
+
this.db.schema.toJSON()
|
|
364
|
+
);
|
|
359
365
|
}
|
|
360
366
|
}
|
|
361
367
|
}
|
package/src/node.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { McpSetting } from 'devframe';
|
|
|
6
6
|
import { createDevServer } from 'devframe/adapters/dev';
|
|
7
7
|
import { buildOtpAuthUrl } from 'devframe/node/auth';
|
|
8
8
|
import type { CommonPowerSyncDatabase } from '@powersync/common';
|
|
9
|
-
import { createIntegration } from './agent.js';
|
|
9
|
+
import { createIntegration, databaseSdk } from './agent.js';
|
|
10
10
|
import { definition, registerIntegration, setSetupSdks } from './definition.js';
|
|
11
11
|
|
|
12
12
|
export interface EnableDiagnosticsOptions {
|
|
@@ -24,7 +24,7 @@ export interface EnableDiagnosticsOptions {
|
|
|
24
24
|
auth?: boolean;
|
|
25
25
|
/** Open the browser once the server is up. @default false */
|
|
26
26
|
open?: boolean;
|
|
27
|
-
/** A label for the SDK, shown in the UI. @default '
|
|
27
|
+
/** A label for the SDK, shown in the UI. @default the database's SDK name */
|
|
28
28
|
sdk?: string;
|
|
29
29
|
/** The source id shown in the UI. @default 'node-1' */
|
|
30
30
|
id?: string;
|
|
@@ -67,7 +67,7 @@ export async function enablePowerSyncDiagnostics(
|
|
|
67
67
|
db: CommonPowerSyncDatabase,
|
|
68
68
|
options: EnableDiagnosticsOptions = {}
|
|
69
69
|
): Promise<DiagnosticsServer> {
|
|
70
|
-
const sdk = options.sdk ?? '@powersync/node';
|
|
70
|
+
const sdk = options.sdk ?? databaseSdk(db, '@powersync/node');
|
|
71
71
|
const detach = await registerIntegration(options.id ?? 'node-1', createIntegration(db, sdk), sdk);
|
|
72
72
|
setSetupSdks(['node']);
|
|
73
73
|
const auth = options.auth ?? true;
|
package/src/page.ts
CHANGED
|
@@ -11,9 +11,7 @@ import {
|
|
|
11
11
|
type SdkIntegration
|
|
12
12
|
} from '@powersync/diagnostics-core';
|
|
13
13
|
import { observeRegisteredDatabases } from '@powersync/web/devtools';
|
|
14
|
-
import { createIntegration } from './agent.js';
|
|
15
|
-
|
|
16
|
-
const SDK = '@powersync/web';
|
|
14
|
+
import { createIntegration, databaseSdk } from './agent.js';
|
|
17
15
|
|
|
18
16
|
/**
|
|
19
17
|
* Serves the app's first live database to any iframe that asks. Call once per page.
|
|
@@ -29,7 +27,7 @@ export function serveDiagnostics(): () => void {
|
|
|
29
27
|
for (const stop of stops) stop();
|
|
30
28
|
stops.clear();
|
|
31
29
|
void integration?.close();
|
|
32
|
-
integration = next ? createIntegration(next,
|
|
30
|
+
integration = next ? createIntegration(next, databaseSdk(next, '@powersync/web')) : null;
|
|
33
31
|
});
|
|
34
32
|
|
|
35
33
|
const onMessage = (event: MessageEvent) => {
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
import { useRozenitePluginAgentTool, type AgentTool } from '@rozenite/agent-bridge';
|
|
3
|
+
import type {
|
|
4
|
+
ActionRequest,
|
|
5
|
+
QueryParams,
|
|
6
|
+
SdkIntegration,
|
|
7
|
+
SyncState,
|
|
8
|
+
UploadQueueState
|
|
9
|
+
} from '@powersync/diagnostics-core';
|
|
10
|
+
import { ACTION_NAMES, ARGUMENT_DESCRIPTIONS, TOOL_DESCRIPTIONS } from '../tools.js';
|
|
11
|
+
import { PLUGIN_ID } from './protocol.js';
|
|
12
|
+
|
|
13
|
+
// The same tools the MCP endpoint offers on the web, for Rozenite's agent CLI and SDK.
|
|
14
|
+
|
|
15
|
+
const queryTool: AgentTool = {
|
|
16
|
+
name: 'query',
|
|
17
|
+
description: TOOL_DESCRIPTIONS.query,
|
|
18
|
+
inputSchema: {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
sql: { type: 'string', description: ARGUMENT_DESCRIPTIONS.sql },
|
|
22
|
+
params: { type: 'array', description: ARGUMENT_DESCRIPTIONS.params }
|
|
23
|
+
},
|
|
24
|
+
required: ['sql']
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const schemaTool: AgentTool = {
|
|
29
|
+
name: 'schema',
|
|
30
|
+
description: TOOL_DESCRIPTIONS.schema,
|
|
31
|
+
inputSchema: { type: 'object', properties: {} },
|
|
32
|
+
readOnly: true
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const infoTool: AgentTool = {
|
|
36
|
+
name: 'info',
|
|
37
|
+
description: TOOL_DESCRIPTIONS.info,
|
|
38
|
+
inputSchema: { type: 'object', properties: {} },
|
|
39
|
+
readOnly: true
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const statusTool: AgentTool = {
|
|
43
|
+
name: 'status',
|
|
44
|
+
description: TOOL_DESCRIPTIONS.status,
|
|
45
|
+
inputSchema: { type: 'object', properties: {} },
|
|
46
|
+
readOnly: true
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const uploadQueueTool: AgentTool = {
|
|
50
|
+
name: 'upload-queue',
|
|
51
|
+
description: TOOL_DESCRIPTIONS['upload-queue'],
|
|
52
|
+
inputSchema: { type: 'object', properties: {} },
|
|
53
|
+
readOnly: true
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const actionTool: AgentTool = {
|
|
57
|
+
name: 'action',
|
|
58
|
+
description: TOOL_DESCRIPTIONS.action,
|
|
59
|
+
inputSchema: {
|
|
60
|
+
type: 'object',
|
|
61
|
+
properties: {
|
|
62
|
+
action: {
|
|
63
|
+
type: 'string',
|
|
64
|
+
enum: ACTION_NAMES
|
|
65
|
+
},
|
|
66
|
+
args: {
|
|
67
|
+
type: 'object',
|
|
68
|
+
description: ARGUMENT_DESCRIPTIONS.actionArgs
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
required: ['action']
|
|
72
|
+
},
|
|
73
|
+
destructive: true
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
function requireIntegration(integration: SdkIntegration | null): SdkIntegration {
|
|
77
|
+
if (!integration) throw new Error('The PowerSync database is not attached yet.');
|
|
78
|
+
return integration;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Registers the PowerSync agent tools for one database. */
|
|
82
|
+
export function usePowerSyncAgentTools(integration: SdkIntegration | null): void {
|
|
83
|
+
const latest = useRef<{ status: SyncState | null; uploadQueue: UploadQueueState | null }>({
|
|
84
|
+
status: null,
|
|
85
|
+
uploadQueue: null
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// The status tools answer from the latest pushed snapshots, as the MCP tools do.
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
if (!integration) return;
|
|
91
|
+
let stop: (() => void) | null = null;
|
|
92
|
+
let cancelled = false;
|
|
93
|
+
void integration
|
|
94
|
+
.observeEvents((event) => {
|
|
95
|
+
if (event.type === 'status') latest.current.status = event.payload;
|
|
96
|
+
if (event.type === 'uploadQueue') latest.current.uploadQueue = event.payload;
|
|
97
|
+
})
|
|
98
|
+
.then((unsubscribe) => {
|
|
99
|
+
if (cancelled) unsubscribe();
|
|
100
|
+
else stop = unsubscribe;
|
|
101
|
+
});
|
|
102
|
+
return () => {
|
|
103
|
+
cancelled = true;
|
|
104
|
+
stop?.();
|
|
105
|
+
};
|
|
106
|
+
}, [integration]);
|
|
107
|
+
|
|
108
|
+
const enabled = integration != null;
|
|
109
|
+
useRozenitePluginAgentTool({
|
|
110
|
+
pluginId: PLUGIN_ID,
|
|
111
|
+
tool: queryTool,
|
|
112
|
+
enabled,
|
|
113
|
+
handler: (params: QueryParams) => requireIntegration(integration).runQuery(params)
|
|
114
|
+
});
|
|
115
|
+
useRozenitePluginAgentTool({
|
|
116
|
+
pluginId: PLUGIN_ID,
|
|
117
|
+
tool: schemaTool,
|
|
118
|
+
enabled,
|
|
119
|
+
handler: () => requireIntegration(integration).getSchema()
|
|
120
|
+
});
|
|
121
|
+
useRozenitePluginAgentTool({
|
|
122
|
+
pluginId: PLUGIN_ID,
|
|
123
|
+
tool: infoTool,
|
|
124
|
+
enabled,
|
|
125
|
+
handler: () => requireIntegration(integration).getInfo()
|
|
126
|
+
});
|
|
127
|
+
useRozenitePluginAgentTool({
|
|
128
|
+
pluginId: PLUGIN_ID,
|
|
129
|
+
tool: statusTool,
|
|
130
|
+
enabled,
|
|
131
|
+
handler: () => latest.current.status
|
|
132
|
+
});
|
|
133
|
+
useRozenitePluginAgentTool({
|
|
134
|
+
pluginId: PLUGIN_ID,
|
|
135
|
+
tool: uploadQueueTool,
|
|
136
|
+
enabled,
|
|
137
|
+
handler: () => latest.current.uploadQueue
|
|
138
|
+
});
|
|
139
|
+
useRozenitePluginAgentTool({
|
|
140
|
+
pluginId: PLUGIN_ID,
|
|
141
|
+
tool: actionTool,
|
|
142
|
+
enabled,
|
|
143
|
+
handler: async (request: ActionRequest) => {
|
|
144
|
+
await requireIntegration(integration).action(request);
|
|
145
|
+
return { ok: true };
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|