Skip to content

Build your personal knowledge base with Trilium Notes

refactor: extract WS API into separate file

Elian Doran777d5ab3b7a2
diff --git a/packages/commons/src/index.ts b/packages/commons/src/index.ts
index 432990bc00..7bbde59ffc 100644
--- a/packages/commons/src/index.ts
+++ b/packages/commons/src/index.ts
@@ -8,3 +8,4 @@ export * from "./lib/mime_type.js";
export * from "./lib/bulk_actions.js";
export * from "./lib/server_api.js";
export * from "./lib/shared_constants.js";
+export * from "./lib/ws_api.js";
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 114ff4eea0..74570c75f2 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -242,126 +242,3 @@ export interface SchemaResponse {
type: string;
}[];
}
-
-export interface EntityChange {
- id?: number | null;
- noteId?: string;
- entityName: string;
- entityId: string;
- entity?: any;
- positions?: Record<string, number>;
- hash: string;
- utcDateChanged?: string;
- utcDateModified?: string;
- utcDateCreated?: string;
- isSynced: boolean | 1 | 0;
- isErased: boolean | 1 | 0;
- componentId?: string | null;
- changeId?: string | null;
- instanceId?: string | null;
-}
-
-export interface EntityRow {
- isDeleted?: boolean;
- content?: Buffer | string;
-}
-
-export interface EntityChangeRecord {
- entityChange: EntityChange;
- entity?: EntityRow;
-}
-
-type TaskStatus<TypeT, DataT, ResultT> = {
- type: "taskProgressCount",
- taskId: string;
- taskType: TypeT;
- data: DataT,
- progressCount: number
-} | {
- type: "taskError",
- taskId: string;
- taskType: TypeT;
- data: DataT;
- message: string;
-} | {
- type: "taskSucceeded",
- taskId: string;
- taskType: TypeT;
- data: DataT;
- result: ResultT;
-}
-
-type TaskDefinitions =
- TaskStatus<"protectNotes", { protect: boolean; }, null>
- | TaskStatus<"importNotes", null, { importedNoteId: string }>
- | TaskStatus<"importAttachments", null, { parentNoteId?: string; importedNoteId: string }>
- | TaskStatus<"deleteNotes", null, null>
- | TaskStatus<"undeleteNotes", null, null>
- | TaskStatus<"export", null, null>
-;
-
-export type TaskType = TaskDefinitions["taskType"];
-
-export interface OpenedFileUpdateStatus {
- entityType: string;
- entityId: string;
- lastModifiedMs?: number;
- filePath: string;
-}
-
-export type WebSocketMessage = TaskDefinitions | {
- type: "ping"
-} | {
- type: "frontend-update",
- data: {
- lastSyncedPush: number,
- entityChanges: EntityChange[]
- }
-} | {
- type: "openNote",
- noteId: string
-} | OpenedFileUpdateStatus & {
- type: "openedFileUpdated"
-} | {
- type: "protectedSessionLogin"
-} | {
- type: "protectedSessionLogout"
-} | {
- type: "toast",
- message: string;
-} | {
- type: "api-log-messages",
- noteId: string,
- messages: string[]
-} | {
- type: "execute-script";
- script: string;
- params: unknown[];
- startNoteId?: string;
- currentNoteId: string;
- originEntityName: string;
- originEntityId?: string | null;
-} | {
- type: "reload-frontend";
- reason: string;
-} | {
- type: "sync-pull-in-progress" | "sync-push-in-progress" | "sync-finished" | "sync-failed";
- lastSyncedPush: number;
-} | {
- type: "consistency-checks-failed"
-} | {
- type: "llm-stream",
- chatNoteId: string;
- done?: boolean;
- error?: string;
- thinking?: string;
- content?: string;
- toolExecution?: {
- action?: string;
- tool?: string;
- toolCallId?: string;
- result?: string | Record<string, any>;
- error?: string;
- args?: Record<string, unknown>;
- }
-}
diff --git a/packages/commons/src/lib/ws_api.ts b/packages/commons/src/lib/ws_api.ts
new file mode 100644
index 0000000000..dca860784d
--- /dev/null
+++ b/packages/commons/src/lib/ws_api.ts
@@ -0,0 +1,122 @@
+export interface EntityChange {
+ id?: number | null;
+ noteId?: string;
+ entityName: string;
+ entityId: string;
+ entity?: any;
+ positions?: Record<string, number>;
+ hash: string;
+ utcDateChanged?: string;
+ utcDateModified?: string;
+ utcDateCreated?: string;
+ isSynced: boolean | 1 | 0;
+ isErased: boolean | 1 | 0;
+ componentId?: string | null;
+ changeId?: string | null;
+ instanceId?: string | null;
+}
+
+export interface EntityRow {
+ isDeleted?: boolean;
+ content?: Buffer | string;
+}
+
+export interface EntityChangeRecord {
+ entityChange: EntityChange;
+ entity?: EntityRow;
+}
+
+type TaskStatus<TypeT, DataT, ResultT> = {
+ type: "taskProgressCount",
+ taskId: string;
+ taskType: TypeT;
+ data: DataT,
+ progressCount: number
+} | {
+ type: "taskError",
+ taskId: string;
+ taskType: TypeT;
+ data: DataT;
+ message: string;
+} | {
+ type: "taskSucceeded",
+ taskId: string;
+ taskType: TypeT;
+ data: DataT;
+ result: ResultT;
+}
+
+type TaskDefinitions =
+ TaskStatus<"protectNotes", { protect: boolean; }, null>
+ | TaskStatus<"importNotes", null, { importedNoteId: string }>
+ | TaskStatus<"importAttachments", null, { parentNoteId?: string; importedNoteId: string }>
+ | TaskStatus<"deleteNotes", null, null>
+ | TaskStatus<"undeleteNotes", null, null>
+ | TaskStatus<"export", null, null>
+;
+
+export type TaskType = TaskDefinitions["taskType"];
+
+export interface OpenedFileUpdateStatus {
+ entityType: string;
+ entityId: string;
+ lastModifiedMs?: number;
+ filePath: string;
+}
+
+export type WebSocketMessage = TaskDefinitions | {
+ type: "ping"
+} | {
+ type: "frontend-update",
+ data: {
+ lastSyncedPush: number,
+ entityChanges: EntityChange[]
+ }
+} | {
+ type: "openNote",
+ noteId: string
+} | OpenedFileUpdateStatus & {
+ type: "openedFileUpdated"
+} | {
+ type: "protectedSessionLogin"
+} | {
+ type: "protectedSessionLogout"
+} | {
+ type: "toast",
+ message: string;
+} | {
+ type: "api-log-messages",
+ noteId: string,
+ messages: string[]
+} | {
+ type: "execute-script";
+ script: string;
+ params: unknown[];
+ startNoteId?: string;
+ currentNoteId: string;
+ originEntityName: string;
+ originEntityId?: string | null;
+} | {
+ type: "reload-frontend";
+ reason: string;
+} | {
+ type: "sync-pull-in-progress" | "sync-push-in-progress" | "sync-finished" | "sync-failed";
+ lastSyncedPush: number;
+} | {
+ type: "consistency-checks-failed"
+} | {
+ type: "llm-stream",
+ chatNoteId: string;
+ done?: boolean;
+ error?: string;
+ thinking?: string;
+ content?: string;
+ toolExecution?: {
+ action?: string;
+ tool?: string;
+ toolCallId?: string;
+ result?: string | Record<string, any>;
+ error?: string;
+ args?: Record<string, unknown>;
+ }
+}