@synap-core/client 0.1.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.
@@ -0,0 +1,92 @@
1
+ // src/realtime.ts
2
+ var SynapRealtimeClient = class {
3
+ ws = null;
4
+ config;
5
+ reconnectAttempts = 0;
6
+ maxReconnectAttempts;
7
+ reconnectDelay;
8
+ reconnectTimer = null;
9
+ isIntentionallyDisconnected = false;
10
+ constructor(config) {
11
+ this.config = config;
12
+ this.maxReconnectAttempts = config.maxReconnectAttempts ?? 5;
13
+ this.reconnectDelay = config.reconnectDelay ?? 1e3;
14
+ }
15
+ /**
16
+ * Connect to the WebSocket server
17
+ */
18
+ connect() {
19
+ if (this.ws?.readyState === WebSocket.OPEN) {
20
+ return;
21
+ }
22
+ this.isIntentionallyDisconnected = false;
23
+ const wsUrl = this.config.url;
24
+ try {
25
+ this.ws = new WebSocket(wsUrl);
26
+ this.ws.onopen = () => {
27
+ this.reconnectAttempts = 0;
28
+ this.config.onConnect?.();
29
+ };
30
+ this.ws.onmessage = (event) => {
31
+ try {
32
+ const message = JSON.parse(event.data);
33
+ this.config.onMessage?.(message);
34
+ } catch (error) {
35
+ console.error("Failed to parse WebSocket message:", error);
36
+ this.config.onError?.(
37
+ new Error(`Failed to parse message: ${error instanceof Error ? error.message : String(error)}`)
38
+ );
39
+ }
40
+ };
41
+ this.ws.onerror = () => {
42
+ this.config.onError?.(new Error("WebSocket error"));
43
+ };
44
+ this.ws.onclose = () => {
45
+ this.config.onDisconnect?.();
46
+ if (!this.isIntentionallyDisconnected) {
47
+ this.attemptReconnect();
48
+ }
49
+ };
50
+ } catch (error) {
51
+ this.config.onError?.(
52
+ error instanceof Error ? error : new Error(String(error))
53
+ );
54
+ }
55
+ }
56
+ /**
57
+ * Disconnect from the WebSocket server
58
+ */
59
+ disconnect() {
60
+ this.isIntentionallyDisconnected = true;
61
+ if (this.reconnectTimer) {
62
+ clearTimeout(this.reconnectTimer);
63
+ this.reconnectTimer = null;
64
+ }
65
+ if (this.ws) {
66
+ this.ws.close();
67
+ this.ws = null;
68
+ }
69
+ }
70
+ /**
71
+ * Check if currently connected
72
+ */
73
+ isConnected() {
74
+ return this.ws?.readyState === WebSocket.OPEN;
75
+ }
76
+ attemptReconnect() {
77
+ if (this.reconnectAttempts >= this.maxReconnectAttempts) {
78
+ this.config.onError?.(
79
+ new Error(`Max reconnection attempts (${this.maxReconnectAttempts}) reached`)
80
+ );
81
+ return;
82
+ }
83
+ this.reconnectAttempts++;
84
+ const delay = this.reconnectDelay * this.reconnectAttempts;
85
+ this.reconnectTimer = setTimeout(() => {
86
+ this.connect();
87
+ }, delay);
88
+ }
89
+ };
90
+ export {
91
+ SynapRealtimeClient
92
+ };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Type definitions for @synap/client
3
+ *
4
+ * Imports the AppRouter type from @synap/api to ensure type safety.
5
+ */
6
+ import type { AppRouter } from '@synap/api';
7
+ import type { SynapClientConfig } from './core.js';
8
+ export type { AppRouter, SynapClientConfig };
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@synap-core/client",
3
+ "version": "0.1.0",
4
+ "description": "Type-safe client SDK for Synap Core OS",
5
+ "type": "module",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "main": "./dist/index.js",
10
+ "module": "./dist/index.mjs",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.js"
17
+ },
18
+ "./react": {
19
+ "types": "./dist/react.d.ts",
20
+ "import": "./dist/react.js",
21
+ "require": "./dist/react.js"
22
+ },
23
+ "./realtime": {
24
+ "types": "./dist/realtime.d.ts",
25
+ "import": "./dist/realtime.js",
26
+ "require": "./dist/realtime.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "README.md"
32
+ ],
33
+ "dependencies": {
34
+ "@trpc/client": "^11.0.0",
35
+ "zod": "^3.22.0"
36
+ },
37
+ "peerDependencies": {
38
+ "@tanstack/react-query": "^5.0.0",
39
+ "@trpc/react-query": "^11.0.0",
40
+ "react": "^18.0.0"
41
+ },
42
+ "devDependencies": {
43
+ "@synap-core/types": "^0.1.0",
44
+ "@trpc/react-query": "^11.0.0",
45
+ "@trpc/server": "^11.0.0",
46
+ "@types/node": "^20.0.0",
47
+ "@types/react": "^19.2.7",
48
+ "@types/react-dom": "^19.2.3",
49
+ "@vitejs/plugin-react": "^5.1.0",
50
+ "tsup": "^8.0.0",
51
+ "typescript": "^5.3.3",
52
+ "vite": "^7.2.2",
53
+ "@synap/api": "1.0.0"
54
+ },
55
+ "keywords": [
56
+ "synap",
57
+ "trpc",
58
+ "client",
59
+ "sdk",
60
+ "knowledge-management",
61
+ "ai"
62
+ ],
63
+ "license": "MIT",
64
+ "repository": {
65
+ "type": "git",
66
+ "url": "https://github.com/Synap-core/backend-os"
67
+ },
68
+ "scripts": {
69
+ "build": "pnpm build:js && pnpm build:types",
70
+ "build:js": "tsup src/index.ts src/react.ts src/react/provider.tsx src/realtime.ts --format cjs,esm",
71
+ "build:types": "tsc --project tsconfig.json --emitDeclarationOnly --declaration --outDir dist",
72
+ "dev": "tsup src/index.ts --watch --format esm",
73
+ "typecheck": "tsc --noEmit",
74
+ "test": "vitest run",
75
+ "test:watch": "vitest"
76
+ }
77
+ }