@pmate/chat-sdk 0.1.7 → 0.1.8
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/lib/components/ChatInputDesktop.js +7 -2
- package/lib/components/icons.d.ts +1 -0
- package/lib/components/icons.js +3 -0
- package/lib/hooks/useVoiceInputController.js +21 -2
- package/lib/index.d.ts +4 -0
- package/lib/index.js +4 -0
- package/lib/runtime/botAuth.d.ts +32 -0
- package/lib/runtime/botAuth.js +71 -0
- package/lib/runtime/botKey.d.ts +14 -0
- package/lib/runtime/botKey.js +25 -0
- package/lib/runtime/chatClient.d.ts +62 -0
- package/lib/runtime/chatClient.js +209 -0
- package/lib/runtime/index.d.ts +5 -0
- package/lib/runtime/index.js +5 -0
- package/lib/runtime/protocol.d.ts +83 -0
- package/lib/runtime/protocol.js +113 -0
- package/lib/runtime/thread.d.ts +3 -0
- package/lib/runtime/thread.js +18 -0
- package/lib/types/voice.d.ts +1 -0
- package/package.json +7 -1
- package/dist/assets/ChatInput.stories-BABdkDrt.js +0 -1545
- package/dist/assets/Color-AVL7NMMY-D33umUzR.js +0 -1
- package/dist/assets/DocsRenderer-PQXLIZUC-BUYhEk48.js +0 -1243
- package/dist/assets/client-De2ACSn_.js +0 -9
- package/dist/assets/iframe-DVJDoCiO.js +0 -1071
- package/dist/assets/iframe-q28pAgAa.css +0 -1
- package/dist/assets/index-CkuJGuix.js +0 -1
- package/dist/assets/preload-helper-PPVm8Dsz.js +0 -1
- package/dist/assets/react-18-DfLUnvSL.js +0 -1
- package/dist/favicon-wrapper.svg +0 -46
- package/dist/favicon.svg +0 -1
- package/dist/iframe.html +0 -687
- package/dist/index.html +0 -148
- package/dist/index.json +0 -1
- package/dist/nunito-sans-bold-italic.woff2 +0 -0
- package/dist/nunito-sans-bold.woff2 +0 -0
- package/dist/nunito-sans-italic.woff2 +0 -0
- package/dist/nunito-sans-regular.woff2 +0 -0
- package/dist/project.json +0 -1
- package/dist/sb-addons/docs-1/manager-bundle.js +0 -151
- package/dist/sb-addons/docs-1/manager-bundle.js.LEGAL.txt +0 -0
- package/dist/sb-addons/storybook-core-server-presets-0/common-manager-bundle.js +0 -971
- package/dist/sb-addons/storybook-core-server-presets-0/common-manager-bundle.js.LEGAL.txt +0 -0
- package/dist/sb-common-assets/favicon-wrapper.svg +0 -46
- package/dist/sb-common-assets/favicon.svg +0 -1
- package/dist/sb-common-assets/nunito-sans-bold-italic.woff2 +0 -0
- package/dist/sb-common-assets/nunito-sans-bold.woff2 +0 -0
- package/dist/sb-common-assets/nunito-sans-italic.woff2 +0 -0
- package/dist/sb-common-assets/nunito-sans-regular.woff2 +0 -0
- package/dist/sb-manager/globals-module-info.js +0 -799
- package/dist/sb-manager/globals-runtime.js +0 -69791
- package/dist/sb-manager/globals.js +0 -34
- package/dist/sb-manager/runtime.js +0 -13198
- package/dist/vite-inject-mocker-entry.js +0 -2
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { decode, encode } from "@msgpack/msgpack";
|
|
2
|
+
export const ChatMsgOp = {
|
|
3
|
+
CHAT: 1,
|
|
4
|
+
ACK: 3,
|
|
5
|
+
AUTH: 4,
|
|
6
|
+
};
|
|
7
|
+
export const ChatMsgKind = {
|
|
8
|
+
DM: 1,
|
|
9
|
+
GROUP: 2,
|
|
10
|
+
};
|
|
11
|
+
export const ChatRealtimeEvents = {
|
|
12
|
+
Connected: "connected",
|
|
13
|
+
Error: "error",
|
|
14
|
+
Message: "message",
|
|
15
|
+
};
|
|
16
|
+
let msgSeq = 0;
|
|
17
|
+
export function createRuntimeMsg(from, to, opcode, body, kind = ChatMsgKind.DM) {
|
|
18
|
+
const seq = nextMsgSeq();
|
|
19
|
+
const msg = {
|
|
20
|
+
hash: "",
|
|
21
|
+
t: Date.now(),
|
|
22
|
+
from,
|
|
23
|
+
to,
|
|
24
|
+
body,
|
|
25
|
+
kind,
|
|
26
|
+
opcode,
|
|
27
|
+
};
|
|
28
|
+
msg.hash = hashRuntimeMsg(msg, seq);
|
|
29
|
+
return msg;
|
|
30
|
+
}
|
|
31
|
+
export class RuntimeWebsocketClient {
|
|
32
|
+
wsUrl;
|
|
33
|
+
ws = null;
|
|
34
|
+
connected = false;
|
|
35
|
+
listeners = new Map();
|
|
36
|
+
constructor(wsUrl) {
|
|
37
|
+
this.wsUrl = wsUrl;
|
|
38
|
+
this.connect();
|
|
39
|
+
}
|
|
40
|
+
on(topic, handler) {
|
|
41
|
+
const set = this.listeners.get(topic) ?? new Set();
|
|
42
|
+
set.add(handler);
|
|
43
|
+
this.listeners.set(topic, set);
|
|
44
|
+
return () => set.delete(handler);
|
|
45
|
+
}
|
|
46
|
+
isConnected() {
|
|
47
|
+
return this.connected;
|
|
48
|
+
}
|
|
49
|
+
close() {
|
|
50
|
+
this.ws?.close();
|
|
51
|
+
this.ws = null;
|
|
52
|
+
this.connected = false;
|
|
53
|
+
}
|
|
54
|
+
async sendMsg(msg) {
|
|
55
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
56
|
+
throw new Error("Websocket is not connected");
|
|
57
|
+
}
|
|
58
|
+
this.ws.send(encode(msg));
|
|
59
|
+
}
|
|
60
|
+
getConnectionType() {
|
|
61
|
+
return "websocket";
|
|
62
|
+
}
|
|
63
|
+
connect() {
|
|
64
|
+
this.ws = new WebSocket(this.wsUrl);
|
|
65
|
+
this.ws.binaryType = "arraybuffer";
|
|
66
|
+
this.ws.onopen = () => {
|
|
67
|
+
this.connected = true;
|
|
68
|
+
this.emit(ChatRealtimeEvents.Connected, true);
|
|
69
|
+
};
|
|
70
|
+
this.ws.onclose = () => {
|
|
71
|
+
this.connected = false;
|
|
72
|
+
this.emit(ChatRealtimeEvents.Connected, false);
|
|
73
|
+
};
|
|
74
|
+
this.ws.onerror = (event) => {
|
|
75
|
+
this.emit(ChatRealtimeEvents.Error, event);
|
|
76
|
+
};
|
|
77
|
+
this.ws.onmessage = (event) => {
|
|
78
|
+
try {
|
|
79
|
+
this.emit(ChatRealtimeEvents.Message, decodeWireMessage(event.data));
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
this.emit(ChatRealtimeEvents.Error, error);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
emit(topic, value) {
|
|
87
|
+
for (const handler of this.listeners.get(topic) ?? []) {
|
|
88
|
+
handler(value);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function decodeWireMessage(data) {
|
|
93
|
+
if (typeof data === "string") {
|
|
94
|
+
return JSON.parse(data);
|
|
95
|
+
}
|
|
96
|
+
if (data instanceof ArrayBuffer) {
|
|
97
|
+
return decode(new Uint8Array(data));
|
|
98
|
+
}
|
|
99
|
+
if (data instanceof Uint8Array) {
|
|
100
|
+
return decode(data);
|
|
101
|
+
}
|
|
102
|
+
throw new Error("Unsupported websocket message payload");
|
|
103
|
+
}
|
|
104
|
+
function hashRuntimeMsg(msg, seq) {
|
|
105
|
+
const random = typeof crypto !== "undefined" && "randomUUID" in crypto
|
|
106
|
+
? crypto.randomUUID()
|
|
107
|
+
: Math.random().toString(16).slice(2);
|
|
108
|
+
return `msg_${msg.t.toString(36)}_${seq.toString(36)}_${random.replace(/-/g, "")}`;
|
|
109
|
+
}
|
|
110
|
+
function nextMsgSeq() {
|
|
111
|
+
msgSeq = (msgSeq + 1) % Number.MAX_SAFE_INTEGER;
|
|
112
|
+
return msgSeq;
|
|
113
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function pairId(appId, userA, userB) {
|
|
2
|
+
const [left, right] = userA <= userB ? [userA, userB] : [userB, userA];
|
|
3
|
+
return stableHash(`social:v1:pair:${appId}:${left}:${right}`);
|
|
4
|
+
}
|
|
5
|
+
export function dmThreadId(appId, userA, userB) {
|
|
6
|
+
return stableHash(`social:v1:dm:${appId}:${pairId(appId, userA, userB)}`);
|
|
7
|
+
}
|
|
8
|
+
export function groupThreadId(appId, groupId) {
|
|
9
|
+
return stableHash(`social:v1:group:${appId}:${groupId}`);
|
|
10
|
+
}
|
|
11
|
+
function stableHash(value) {
|
|
12
|
+
let hash = 0x811c9dc5;
|
|
13
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
14
|
+
hash ^= value.charCodeAt(index);
|
|
15
|
+
hash = Math.imul(hash, 0x01000193);
|
|
16
|
+
}
|
|
17
|
+
return `h${(hash >>> 0).toString(16).padStart(8, "0")}`;
|
|
18
|
+
}
|
package/lib/types/voice.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export type ChatVoiceConfig = {
|
|
|
11
11
|
asrClient?: ChatASRClient;
|
|
12
12
|
lang?: string;
|
|
13
13
|
minDurationMs?: number;
|
|
14
|
+
transcribeTimeoutMs?: number;
|
|
14
15
|
simulatedTranscript?: string;
|
|
15
16
|
shortRecordingMessage?: string;
|
|
16
17
|
onVoiceSend?: (blob: Blob) => Promise<void> | void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pmate/chat-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Cross-platform PMate chat input components, voice input primitives, and ASR helpers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -19,6 +19,11 @@
|
|
|
19
19
|
"types": "./lib/index.d.ts",
|
|
20
20
|
"import": "./lib/index.js",
|
|
21
21
|
"default": "./lib/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./runtime": {
|
|
24
|
+
"types": "./lib/runtime/index.d.ts",
|
|
25
|
+
"import": "./lib/runtime/index.js",
|
|
26
|
+
"default": "./lib/runtime/index.js"
|
|
22
27
|
}
|
|
23
28
|
},
|
|
24
29
|
"publishConfig": {
|
|
@@ -36,6 +41,7 @@
|
|
|
36
41
|
"@emoji-mart/data": "^1.2.1",
|
|
37
42
|
"@emoji-mart/react": "^1.1.1",
|
|
38
43
|
"@pmate/agent-sdk": "^1.1.2",
|
|
44
|
+
"@msgpack/msgpack": "^3.1.2",
|
|
39
45
|
"jotai": "^2.16.1"
|
|
40
46
|
},
|
|
41
47
|
"peerDependencies": {
|