@sybilion/uilib 1.0.31 → 1.0.32
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/dist/esm/index.js +2 -1
- package/dist/esm/mini-app/miniAppChatBridge.js +45 -0
- package/dist/esm/mini-app/miniAppProtocol.js +52 -5
- package/dist/esm/types/src/mini-app/index.d.ts +3 -2
- package/dist/esm/types/src/mini-app/miniAppChatBridge.d.ts +6 -0
- package/dist/esm/types/src/mini-app/miniAppProtocol.d.ts +30 -0
- package/package.json +1 -1
- package/src/mini-app/index.ts +7 -0
- package/src/mini-app/miniAppChatBridge.ts +55 -0
- package/src/mini-app/miniAppProtocol.ts +83 -0
package/dist/esm/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export { MINIAPP_CHANNEL, MINIAPP_VERSION, applyThemeToDocument, buildDataRequestMessage, buildReadyMessage, isTrustedMiniAppParentMessage, parseDataResponseMessage, parseThemeSyncMessage, resolveParentOriginFromReferrer } from './mini-app/miniAppProtocol.js';
|
|
1
|
+
export { MINIAPP_CHANNEL, MINIAPP_VERSION, applyThemeToDocument, buildChatSendMessage, buildDataRequestMessage, buildReadyMessage, isTrustedMiniAppParentMessage, parseChatSendResultMessage, parseDataResponseMessage, parseThemeSyncMessage, resolveParentOriginFromReferrer } from './mini-app/miniAppProtocol.js';
|
|
2
2
|
export { createMiniAppDataClient } from './mini-app/miniAppDataClient.js';
|
|
3
3
|
export { getDefaultMiniAppThemeConfig } from './mini-app/miniAppThemeConfig.js';
|
|
4
|
+
export { sendChatMessage } from './mini-app/miniAppChatBridge.js';
|
|
4
5
|
export { MiniAppRoot, useMiniAppShellTheme } from './mini-app/MiniAppRoot.js';
|
|
5
6
|
export { ChatContext, ChatProvider, useChat, useChats, useChatsForDataset, useChatsForScopeId, useCurrentChat } from './contexts/chat-context.js';
|
|
6
7
|
export { AnalysesSelector } from './components/ui/AnalysesSelector/AnalysesSelector.js';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { resolveParentOriginFromReferrer, buildChatSendMessage, isTrustedMiniAppParentMessage, parseChatSendResultMessage } from './miniAppProtocol.js';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
4
|
+
/**
|
|
5
|
+
* Ask the Sybilion host to send a chat message with its auth token.
|
|
6
|
+
* Does not update host ChatSheet state — same session as `chatId` on the agent only.
|
|
7
|
+
*/
|
|
8
|
+
async function sendChatMessage(chatId, message) {
|
|
9
|
+
if (typeof window === 'undefined' || window.parent === window) {
|
|
10
|
+
throw new Error('sendChatMessage requires an embedded mini-app (iframe)');
|
|
11
|
+
}
|
|
12
|
+
const requestId = crypto.randomUUID();
|
|
13
|
+
const target = resolveParentOriginFromReferrer();
|
|
14
|
+
const payload = { requestId, chatId, message };
|
|
15
|
+
const msg = buildChatSendMessage(payload);
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const onMessage = (event) => {
|
|
18
|
+
if (!isTrustedMiniAppParentMessage(event))
|
|
19
|
+
return;
|
|
20
|
+
const parsed = parseChatSendResultMessage(event.data);
|
|
21
|
+
if (!parsed || parsed.requestId !== requestId)
|
|
22
|
+
return;
|
|
23
|
+
window.removeEventListener('message', onMessage);
|
|
24
|
+
clearTimeout(timer);
|
|
25
|
+
if (parsed.ok === true) {
|
|
26
|
+
resolve(parsed.result);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
reject(new Error(parsed.error));
|
|
30
|
+
};
|
|
31
|
+
window.addEventListener('message', onMessage);
|
|
32
|
+
const timer = setTimeout(() => {
|
|
33
|
+
window.removeEventListener('message', onMessage);
|
|
34
|
+
reject(new Error('Mini-app chat request timed out'));
|
|
35
|
+
}, DEFAULT_TIMEOUT_MS);
|
|
36
|
+
if (target) {
|
|
37
|
+
window.parent.postMessage(msg, target);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
window.parent.postMessage(msg, '*');
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { sendChatMessage };
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* postMessage protocol for Sybilion workspace mini-apps (iframe child).
|
|
3
|
-
* Keep in sync with sybilion-client `src/workspace/miniAppBridge.ts` (channel, version, payloads).
|
|
4
|
-
*/
|
|
5
1
|
const MINIAPP_CHANNEL = 'sybilion.miniapp';
|
|
6
2
|
const MINIAPP_VERSION = 1;
|
|
7
3
|
function isRecord(v) {
|
|
@@ -70,6 +66,14 @@ function buildDataRequestMessage(payload) {
|
|
|
70
66
|
payload,
|
|
71
67
|
};
|
|
72
68
|
}
|
|
69
|
+
function buildChatSendMessage(payload) {
|
|
70
|
+
return {
|
|
71
|
+
channel: MINIAPP_CHANNEL,
|
|
72
|
+
version: MINIAPP_VERSION,
|
|
73
|
+
type: 'CHAT_SEND',
|
|
74
|
+
payload,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
73
77
|
/** Parse parent → child DATA_RESPONSE. */
|
|
74
78
|
function parseDataResponseMessage(data) {
|
|
75
79
|
if (!isRecord(data))
|
|
@@ -102,5 +106,48 @@ function parseDataResponseMessage(data) {
|
|
|
102
106
|
}
|
|
103
107
|
return base;
|
|
104
108
|
}
|
|
109
|
+
/** Parse parent → child CHAT_SEND_RESULT (shell uses `buildChatSendResultMessage`). */
|
|
110
|
+
function parseChatSendResultMessage(data) {
|
|
111
|
+
if (!isRecord(data))
|
|
112
|
+
return null;
|
|
113
|
+
if (data.channel !== MINIAPP_CHANNEL)
|
|
114
|
+
return null;
|
|
115
|
+
if (data.version !== MINIAPP_VERSION)
|
|
116
|
+
return null;
|
|
117
|
+
if (data.type !== 'CHAT_SEND_RESULT')
|
|
118
|
+
return null;
|
|
119
|
+
const payload = data.payload;
|
|
120
|
+
if (!isRecord(payload))
|
|
121
|
+
return null;
|
|
122
|
+
const requestId = payload.requestId;
|
|
123
|
+
if (typeof requestId !== 'string' || requestId.length === 0)
|
|
124
|
+
return null;
|
|
125
|
+
if (payload.ok === true) {
|
|
126
|
+
const result = payload.result;
|
|
127
|
+
if (!isRecord(result))
|
|
128
|
+
return null;
|
|
129
|
+
const responseText = result.response;
|
|
130
|
+
if (typeof responseText !== 'string')
|
|
131
|
+
return null;
|
|
132
|
+
const sidRaw = result.session_id;
|
|
133
|
+
if (sidRaw !== undefined && sidRaw !== null && typeof sidRaw !== 'string')
|
|
134
|
+
return null;
|
|
135
|
+
const session_id = typeof sidRaw === 'string' ? sidRaw : null;
|
|
136
|
+
return {
|
|
137
|
+
requestId,
|
|
138
|
+
ok: true,
|
|
139
|
+
result: {
|
|
140
|
+
response: responseText,
|
|
141
|
+
session_id,
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (payload.ok === false) {
|
|
146
|
+
if (typeof payload.error !== 'string')
|
|
147
|
+
return null;
|
|
148
|
+
return { requestId, ok: false, error: payload.error };
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
105
152
|
|
|
106
|
-
export { MINIAPP_CHANNEL, MINIAPP_VERSION, applyThemeToDocument, buildDataRequestMessage, buildReadyMessage, isTrustedMiniAppParentMessage, parseDataResponseMessage, parseThemeSyncMessage, resolveParentOriginFromReferrer };
|
|
153
|
+
export { MINIAPP_CHANNEL, MINIAPP_VERSION, applyThemeToDocument, buildChatSendMessage, buildDataRequestMessage, buildReadyMessage, isTrustedMiniAppParentMessage, parseChatSendResultMessage, parseDataResponseMessage, parseThemeSyncMessage, resolveParentOriginFromReferrer };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
export { applyThemeToDocument, buildDataRequestMessage, buildReadyMessage, MINIAPP_CHANNEL, MINIAPP_VERSION, parseDataResponseMessage, parseThemeSyncMessage, isTrustedMiniAppParentMessage, resolveParentOriginFromReferrer, } from './miniAppProtocol';
|
|
2
|
-
export type { MiniAppMessageReady, MiniAppMessageThemeSync, MiniAppMessageDataRequest, MiniAppMessageDataResponse, MiniAppDataRequestPayload, MiniAppDataResponsePayload, MiniAppDataOp, ThemeSyncPayload, } from './miniAppProtocol';
|
|
1
|
+
export { applyThemeToDocument, buildChatSendMessage, buildDataRequestMessage, buildReadyMessage, MINIAPP_CHANNEL, MINIAPP_VERSION, parseChatSendResultMessage, parseDataResponseMessage, parseThemeSyncMessage, isTrustedMiniAppParentMessage, resolveParentOriginFromReferrer, } from './miniAppProtocol';
|
|
2
|
+
export type { MiniAppMessageReady, MiniAppMessageThemeSync, MiniAppMessageDataRequest, MiniAppMessageDataResponse, MiniAppMessageChatSend, MiniAppMessageChatSendResult, MiniAppDataRequestPayload, MiniAppDataResponsePayload, MiniAppChatSendPayload, MiniAppChatSendResultPayload, MiniAppDataOp, ThemeSyncPayload, } from './miniAppProtocol';
|
|
3
3
|
export { createMiniAppDataClient } from './miniAppDataClient';
|
|
4
4
|
export type { MiniAppDataClientOptions, MiniAppDataClient, } from './miniAppDataClient';
|
|
5
5
|
export type { MiniAppDataset, MiniAppDriversComparisonSnapshot, MiniAppForecastMap, MiniAppPerformanceBundle, } from './miniAppDataTypes';
|
|
6
6
|
export { getDefaultMiniAppThemeConfig } from './miniAppThemeConfig';
|
|
7
7
|
export type { MiniAppThemeConfig } from './miniAppThemeConfig';
|
|
8
|
+
export { sendChatMessage } from './miniAppChatBridge';
|
|
8
9
|
export { MiniAppRoot, useMiniAppShellTheme } from './MiniAppRoot';
|
|
9
10
|
export type { MiniAppRootProps, MiniAppShellContextValue } from './MiniAppRoot';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ChatResponse } from '#uilib/types/chat-api.types';
|
|
2
|
+
/**
|
|
3
|
+
* Ask the Sybilion host to send a chat message with its auth token.
|
|
4
|
+
* Does not update host ChatSheet state — same session as `chatId` on the agent only.
|
|
5
|
+
*/
|
|
6
|
+
export declare function sendChatMessage(chatId: string, message: string): Promise<ChatResponse>;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* postMessage protocol for Sybilion workspace mini-apps (iframe child).
|
|
3
3
|
* Keep in sync with sybilion-client `src/workspace/miniAppBridge.ts` (channel, version, payloads).
|
|
4
4
|
*/
|
|
5
|
+
import type { ChatResponse } from '#uilib/types/chat-api.types';
|
|
5
6
|
export declare const MINIAPP_CHANNEL: "sybilion.miniapp";
|
|
6
7
|
export declare const MINIAPP_VERSION: 1;
|
|
7
8
|
export type ThemeSyncPayload = {
|
|
@@ -46,6 +47,32 @@ export type MiniAppMessageDataResponse = {
|
|
|
46
47
|
type: 'DATA_RESPONSE';
|
|
47
48
|
payload: MiniAppDataResponsePayload;
|
|
48
49
|
};
|
|
50
|
+
export type MiniAppChatSendPayload = {
|
|
51
|
+
requestId: string;
|
|
52
|
+
chatId: string;
|
|
53
|
+
message: string;
|
|
54
|
+
};
|
|
55
|
+
export type MiniAppMessageChatSend = {
|
|
56
|
+
channel: typeof MINIAPP_CHANNEL;
|
|
57
|
+
version: typeof MINIAPP_VERSION;
|
|
58
|
+
type: 'CHAT_SEND';
|
|
59
|
+
payload: MiniAppChatSendPayload;
|
|
60
|
+
};
|
|
61
|
+
export type MiniAppChatSendResultPayload = {
|
|
62
|
+
requestId: string;
|
|
63
|
+
ok: true;
|
|
64
|
+
result: ChatResponse;
|
|
65
|
+
} | {
|
|
66
|
+
requestId: string;
|
|
67
|
+
ok: false;
|
|
68
|
+
error: string;
|
|
69
|
+
};
|
|
70
|
+
export type MiniAppMessageChatSendResult = {
|
|
71
|
+
channel: typeof MINIAPP_CHANNEL;
|
|
72
|
+
version: typeof MINIAPP_VERSION;
|
|
73
|
+
type: 'CHAT_SEND_RESULT';
|
|
74
|
+
payload: MiniAppChatSendResultPayload;
|
|
75
|
+
};
|
|
49
76
|
/** Parse parent → child THEME_SYNC (shell uses `buildThemeSyncMessage`). */
|
|
50
77
|
export declare function parseThemeSyncMessage(data: unknown): ThemeSyncPayload | null;
|
|
51
78
|
/** Child → parent READY (optional `appId` for telemetry). */
|
|
@@ -55,5 +82,8 @@ export declare function applyThemeToDocument(mode: 'light' | 'dark'): void;
|
|
|
55
82
|
/** Only accept shell messages that appear to come from the real embedding parent. */
|
|
56
83
|
export declare function isTrustedMiniAppParentMessage(event: MessageEvent): boolean;
|
|
57
84
|
export declare function buildDataRequestMessage(payload: MiniAppDataRequestPayload): MiniAppMessageDataRequest;
|
|
85
|
+
export declare function buildChatSendMessage(payload: MiniAppChatSendPayload): MiniAppMessageChatSend;
|
|
58
86
|
/** Parse parent → child DATA_RESPONSE. */
|
|
59
87
|
export declare function parseDataResponseMessage(data: unknown): MiniAppDataResponsePayload | null;
|
|
88
|
+
/** Parse parent → child CHAT_SEND_RESULT (shell uses `buildChatSendResultMessage`). */
|
|
89
|
+
export declare function parseChatSendResultMessage(data: unknown): MiniAppChatSendResultPayload | null;
|
package/package.json
CHANGED
package/src/mini-app/index.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export {
|
|
2
2
|
applyThemeToDocument,
|
|
3
|
+
buildChatSendMessage,
|
|
3
4
|
buildDataRequestMessage,
|
|
4
5
|
buildReadyMessage,
|
|
5
6
|
MINIAPP_CHANNEL,
|
|
6
7
|
MINIAPP_VERSION,
|
|
8
|
+
parseChatSendResultMessage,
|
|
7
9
|
parseDataResponseMessage,
|
|
8
10
|
parseThemeSyncMessage,
|
|
9
11
|
isTrustedMiniAppParentMessage,
|
|
@@ -14,8 +16,12 @@ export type {
|
|
|
14
16
|
MiniAppMessageThemeSync,
|
|
15
17
|
MiniAppMessageDataRequest,
|
|
16
18
|
MiniAppMessageDataResponse,
|
|
19
|
+
MiniAppMessageChatSend,
|
|
20
|
+
MiniAppMessageChatSendResult,
|
|
17
21
|
MiniAppDataRequestPayload,
|
|
18
22
|
MiniAppDataResponsePayload,
|
|
23
|
+
MiniAppChatSendPayload,
|
|
24
|
+
MiniAppChatSendResultPayload,
|
|
19
25
|
MiniAppDataOp,
|
|
20
26
|
ThemeSyncPayload,
|
|
21
27
|
} from './miniAppProtocol';
|
|
@@ -32,5 +38,6 @@ export type {
|
|
|
32
38
|
} from './miniAppDataTypes';
|
|
33
39
|
export { getDefaultMiniAppThemeConfig } from './miniAppThemeConfig';
|
|
34
40
|
export type { MiniAppThemeConfig } from './miniAppThemeConfig';
|
|
41
|
+
export { sendChatMessage } from './miniAppChatBridge';
|
|
35
42
|
export { MiniAppRoot, useMiniAppShellTheme } from './MiniAppRoot';
|
|
36
43
|
export type { MiniAppRootProps, MiniAppShellContextValue } from './MiniAppRoot';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ChatResponse } from '#uilib/types/chat-api.types';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
buildChatSendMessage,
|
|
5
|
+
isTrustedMiniAppParentMessage,
|
|
6
|
+
parseChatSendResultMessage,
|
|
7
|
+
resolveParentOriginFromReferrer,
|
|
8
|
+
} from './miniAppProtocol';
|
|
9
|
+
|
|
10
|
+
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Ask the Sybilion host to send a chat message with its auth token.
|
|
14
|
+
* Does not update host ChatSheet state — same session as `chatId` on the agent only.
|
|
15
|
+
*/
|
|
16
|
+
export async function sendChatMessage(
|
|
17
|
+
chatId: string,
|
|
18
|
+
message: string,
|
|
19
|
+
): Promise<ChatResponse> {
|
|
20
|
+
if (typeof window === 'undefined' || window.parent === window) {
|
|
21
|
+
throw new Error('sendChatMessage requires an embedded mini-app (iframe)');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const requestId = crypto.randomUUID();
|
|
25
|
+
const target = resolveParentOriginFromReferrer();
|
|
26
|
+
const payload = { requestId, chatId, message };
|
|
27
|
+
const msg = buildChatSendMessage(payload);
|
|
28
|
+
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const onMessage = (event: MessageEvent) => {
|
|
31
|
+
if (!isTrustedMiniAppParentMessage(event)) return;
|
|
32
|
+
const parsed = parseChatSendResultMessage(event.data);
|
|
33
|
+
if (!parsed || parsed.requestId !== requestId) return;
|
|
34
|
+
window.removeEventListener('message', onMessage);
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
if (parsed.ok === true) {
|
|
37
|
+
resolve(parsed.result);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
reject(new Error(parsed.error));
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
window.addEventListener('message', onMessage);
|
|
44
|
+
const timer = setTimeout(() => {
|
|
45
|
+
window.removeEventListener('message', onMessage);
|
|
46
|
+
reject(new Error('Mini-app chat request timed out'));
|
|
47
|
+
}, DEFAULT_TIMEOUT_MS);
|
|
48
|
+
|
|
49
|
+
if (target) {
|
|
50
|
+
window.parent.postMessage(msg, target);
|
|
51
|
+
} else {
|
|
52
|
+
window.parent.postMessage(msg, '*');
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* postMessage protocol for Sybilion workspace mini-apps (iframe child).
|
|
3
3
|
* Keep in sync with sybilion-client `src/workspace/miniAppBridge.ts` (channel, version, payloads).
|
|
4
4
|
*/
|
|
5
|
+
import type { ChatResponse } from '#uilib/types/chat-api.types';
|
|
5
6
|
|
|
6
7
|
export const MINIAPP_CHANNEL = 'sybilion.miniapp' as const;
|
|
7
8
|
export const MINIAPP_VERSION = 1 as const;
|
|
@@ -61,6 +62,38 @@ export type MiniAppMessageDataResponse = {
|
|
|
61
62
|
payload: MiniAppDataResponsePayload;
|
|
62
63
|
};
|
|
63
64
|
|
|
65
|
+
export type MiniAppChatSendPayload = {
|
|
66
|
+
requestId: string;
|
|
67
|
+
chatId: string;
|
|
68
|
+
message: string;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type MiniAppMessageChatSend = {
|
|
72
|
+
channel: typeof MINIAPP_CHANNEL;
|
|
73
|
+
version: typeof MINIAPP_VERSION;
|
|
74
|
+
type: 'CHAT_SEND';
|
|
75
|
+
payload: MiniAppChatSendPayload;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export type MiniAppChatSendResultPayload =
|
|
79
|
+
| {
|
|
80
|
+
requestId: string;
|
|
81
|
+
ok: true;
|
|
82
|
+
result: ChatResponse;
|
|
83
|
+
}
|
|
84
|
+
| {
|
|
85
|
+
requestId: string;
|
|
86
|
+
ok: false;
|
|
87
|
+
error: string;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export type MiniAppMessageChatSendResult = {
|
|
91
|
+
channel: typeof MINIAPP_CHANNEL;
|
|
92
|
+
version: typeof MINIAPP_VERSION;
|
|
93
|
+
type: 'CHAT_SEND_RESULT';
|
|
94
|
+
payload: MiniAppChatSendResultPayload;
|
|
95
|
+
};
|
|
96
|
+
|
|
64
97
|
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
65
98
|
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
66
99
|
}
|
|
@@ -133,6 +166,17 @@ export function buildDataRequestMessage(
|
|
|
133
166
|
};
|
|
134
167
|
}
|
|
135
168
|
|
|
169
|
+
export function buildChatSendMessage(
|
|
170
|
+
payload: MiniAppChatSendPayload,
|
|
171
|
+
): MiniAppMessageChatSend {
|
|
172
|
+
return {
|
|
173
|
+
channel: MINIAPP_CHANNEL,
|
|
174
|
+
version: MINIAPP_VERSION,
|
|
175
|
+
type: 'CHAT_SEND',
|
|
176
|
+
payload,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
136
180
|
/** Parse parent → child DATA_RESPONSE. */
|
|
137
181
|
export function parseDataResponseMessage(
|
|
138
182
|
data: unknown,
|
|
@@ -162,3 +206,42 @@ export function parseDataResponseMessage(
|
|
|
162
206
|
}
|
|
163
207
|
return base;
|
|
164
208
|
}
|
|
209
|
+
|
|
210
|
+
/** Parse parent → child CHAT_SEND_RESULT (shell uses `buildChatSendResultMessage`). */
|
|
211
|
+
export function parseChatSendResultMessage(
|
|
212
|
+
data: unknown,
|
|
213
|
+
): MiniAppChatSendResultPayload | null {
|
|
214
|
+
if (!isRecord(data)) return null;
|
|
215
|
+
if (data.channel !== MINIAPP_CHANNEL) return null;
|
|
216
|
+
if (data.version !== MINIAPP_VERSION) return null;
|
|
217
|
+
if (data.type !== 'CHAT_SEND_RESULT') return null;
|
|
218
|
+
const payload = data.payload;
|
|
219
|
+
if (!isRecord(payload)) return null;
|
|
220
|
+
const requestId = payload.requestId;
|
|
221
|
+
if (typeof requestId !== 'string' || requestId.length === 0) return null;
|
|
222
|
+
|
|
223
|
+
if (payload.ok === true) {
|
|
224
|
+
const result = payload.result;
|
|
225
|
+
if (!isRecord(result)) return null;
|
|
226
|
+
const responseText = result.response;
|
|
227
|
+
if (typeof responseText !== 'string') return null;
|
|
228
|
+
const sidRaw = result.session_id;
|
|
229
|
+
if (sidRaw !== undefined && sidRaw !== null && typeof sidRaw !== 'string')
|
|
230
|
+
return null;
|
|
231
|
+
const session_id: string | null =
|
|
232
|
+
typeof sidRaw === 'string' ? sidRaw : null;
|
|
233
|
+
return {
|
|
234
|
+
requestId,
|
|
235
|
+
ok: true,
|
|
236
|
+
result: {
|
|
237
|
+
response: responseText,
|
|
238
|
+
session_id,
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if (payload.ok === false) {
|
|
243
|
+
if (typeof payload.error !== 'string') return null;
|
|
244
|
+
return { requestId, ok: false, error: payload.error };
|
|
245
|
+
}
|
|
246
|
+
return null;
|
|
247
|
+
}
|