From 777d5ab3b7a277716eb6cf1f1b840f08d2dc20c6 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 13 Sep 2025 13:07:31 +0300 Subject: [PATCH] refactor: extract WS API into separate file --- packages/commons/src/index.ts | 1 + packages/commons/src/lib/server_api.ts | 123 ------------------------- packages/commons/src/lib/ws_api.ts | 122 ++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 123 deletions(-) create mode 100644 packages/commons/src/lib/ws_api.ts diff --git a/packages/commons/src/index.ts b/packages/commons/src/index.ts index 432990bc0..7bbde59ff 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 114ff4eea..74570c75f 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; - 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 = { - 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; - error?: string; - args?: Record; - } -} diff --git a/packages/commons/src/lib/ws_api.ts b/packages/commons/src/lib/ws_api.ts new file mode 100644 index 000000000..dca860784 --- /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; + 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 = { + 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; + error?: string; + args?: Record; + } +}