teko-chat-sdk 1.0.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,175 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface ChatLabels {
5
+ agentName: string;
6
+ agentStatus: string;
7
+ close: string;
8
+ minimize: string;
9
+ backToChat: string;
10
+ viewContent: string;
11
+ loadingContent: string;
12
+ inputPlaceholder: string;
13
+ send: string;
14
+ emptyState: string;
15
+ }
16
+
17
+ type ChatState = 'bubble' | 'mini' | 'fullscreen';
18
+ interface SuggestOption {
19
+ key: string;
20
+ label: string;
21
+ payload?: Record<string, unknown>;
22
+ }
23
+ interface BFFResponseData {
24
+ conversationId: string;
25
+ message: string;
26
+ intent: string;
27
+ /** Any string — SDK only handles 'show_ui' internally, rest forwarded to onIntent */
28
+ action: string;
29
+ componentKey?: string;
30
+ path?: string;
31
+ suggest?: {
32
+ options: SuggestOption[];
33
+ };
34
+ timestamp: number;
35
+ }
36
+ interface BFFResponse {
37
+ code: number;
38
+ message: string;
39
+ data: BFFResponseData;
40
+ }
41
+ interface SDKRequest {
42
+ conversationId?: string;
43
+ userId?: string;
44
+ message?: string;
45
+ context?: Record<string, unknown>;
46
+ appContext?: Record<string, unknown>;
47
+ timestamp: number;
48
+ }
49
+ interface ChatMessage {
50
+ id: string;
51
+ role: 'user' | 'ai';
52
+ content: string;
53
+ options?: SuggestOption[];
54
+ timestamp: number;
55
+ isStreaming?: boolean;
56
+ }
57
+ interface DebugEvent {
58
+ /** Loại event:
59
+ * - `request`: SDK gửi message/context lên BFF
60
+ * - `response`: BFF trả về response
61
+ * - `intent`: onIntent được trigger (action + componentKey)
62
+ * - `context`: sendContext được gọi từ host app
63
+ */
64
+ type: 'request' | 'response' | 'intent' | 'context';
65
+ timestamp: number;
66
+ payload: unknown;
67
+ }
68
+ /** Frame protocol cho BFF → SDK streaming communication */
69
+ type TransportFrame = {
70
+ type: 'chunk';
71
+ text: string;
72
+ } | {
73
+ type: 'done';
74
+ data: BFFResponseData;
75
+ } | {
76
+ type: 'error';
77
+ message: string;
78
+ };
79
+ interface TekoChatWidgetProps {
80
+ appId: string;
81
+ /** BFF endpoint. Prefix xác định giao thức:
82
+ * - wss:// hoặc ws:// → WebSocket streaming
83
+ * - https:// hoặc http:// → HTTP streaming (ndjson)
84
+ * - chuỗi rỗng hoặc không truyền → mock handler (dev only)
85
+ */
86
+ chatBffUrl: string;
87
+ /** Callback khi BFF trả về intent action (show_ui, navigate, ...) */
88
+ onIntent?: (action: string, componentKey?: string) => void;
89
+ /** Render content cho right panel ở fullscreen state */
90
+ renderRightPanel?: (componentKey: string) => ReactNode;
91
+ /**
92
+ * Inject app-level metadata vào mỗi BFF request.
93
+ * Được gọi tại send-time — luôn resolve giá trị mới nhất.
94
+ * Support sync và async. Field `appContext` bị omit nếu trả về `{}`.
95
+ *
96
+ * @example
97
+ * getAppContext={() => ({ terminalId: '123', sessionKey: store.getKey() })}
98
+ * getAppContext={async () => await myApp.getSessionMeta()}
99
+ */
100
+ getAppContext?: () => Record<string, unknown> | Promise<Record<string, unknown>>;
101
+ /**
102
+ * Custom bubble — SDK inject onClick để open chat.
103
+ * Nếu không truyền, SDK dùng bubble mặc định (bottom-right).
104
+ */
105
+ renderBubble?: (onClick: () => void) => ReactNode;
106
+ /**
107
+ * Vị trí bubble, ảnh hưởng hướng animation fullscreen.
108
+ * @default 'bottom-right'
109
+ */
110
+ bubbleAnchor?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
111
+ /**
112
+ * Layout mode:
113
+ * - `'desktop'` (default): mini popup góc dưới phải, fullscreen split 35/65
114
+ * - `'mobile'`: bottom sheet + single column fullscreen
115
+ *
116
+ * SDK không tự detect viewport — host app tự detect và truyền vào.
117
+ * @default 'desktop'
118
+ */
119
+ layoutMode?: 'desktop' | 'mobile';
120
+ /** Primary color (hex) cho toàn bộ widget. @default '#1a73e8' */
121
+ primaryColor?: string;
122
+ /** Avatar URL cho AI bubble. Mặc định dùng icon robot. */
123
+ botAvatar?: string;
124
+ /** @default 'vi' */
125
+ locale?: 'vi' | 'en';
126
+ /** Override từng label cụ thể thay vì đổi toàn bộ locale */
127
+ labels?: Partial<ChatLabels>;
128
+ /**
129
+ * Offset từ top (px) — dùng khi host app có fixed header.
130
+ * Fullscreen bắt đầu từ offsetTop thay vì top: 0.
131
+ * @default 0
132
+ */
133
+ offsetTop?: number;
134
+ /**
135
+ * Offset từ bottom (px) — dùng khi host app có fixed bottom nav.
136
+ * @default 0
137
+ */
138
+ offsetBottom?: number;
139
+ /** Chiều rộng mini popup (px). @default 360 */
140
+ miniWidth?: number;
141
+ /** Chiều cao mini popup (px). @default 480 */
142
+ miniHeight?: number;
143
+ /**
144
+ * CSS z-index cho bubble và mini popup. Fullscreen backdrop = zIndex + 9.
145
+ * @default 1031
146
+ */
147
+ zIndex?: number;
148
+ /**
149
+ * @internal
150
+ * Nhận mọi SDK event (request/response/intent/context) để debug.
151
+ * Dùng cho DebugPanel hoặc Storybook. Không dùng trong production.
152
+ */
153
+ onDebugEvent?: (event: DebugEvent) => void;
154
+ }
155
+ interface TekoChatWidgetRef {
156
+ sendContext: (data: Record<string, unknown>) => void;
157
+ sendMessage: (text: string) => void;
158
+ open: () => void;
159
+ close: () => void;
160
+ openFullscreen: (componentKey: string) => void;
161
+ closeFullscreen: () => void;
162
+ }
163
+
164
+ declare const TekoChatWidget: react.ForwardRefExoticComponent<TekoChatWidgetProps & react.RefAttributes<TekoChatWidgetRef>>;
165
+
166
+ interface ITransport {
167
+ send(request: SDKRequest): void;
168
+ onChunk(callback: (text: string) => void): void;
169
+ onDone(callback: (data: BFFResponseData) => void): void;
170
+ onError(callback: (error: Error) => void): void;
171
+ connect(): Promise<void>;
172
+ disconnect(): void;
173
+ }
174
+
175
+ export { type BFFResponse, type BFFResponseData, type ChatLabels, type ChatMessage, type ChatState, type DebugEvent, type ITransport, type SDKRequest, type SuggestOption, TekoChatWidget, type TekoChatWidgetProps, type TekoChatWidgetRef, type TransportFrame };
@@ -0,0 +1,175 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface ChatLabels {
5
+ agentName: string;
6
+ agentStatus: string;
7
+ close: string;
8
+ minimize: string;
9
+ backToChat: string;
10
+ viewContent: string;
11
+ loadingContent: string;
12
+ inputPlaceholder: string;
13
+ send: string;
14
+ emptyState: string;
15
+ }
16
+
17
+ type ChatState = 'bubble' | 'mini' | 'fullscreen';
18
+ interface SuggestOption {
19
+ key: string;
20
+ label: string;
21
+ payload?: Record<string, unknown>;
22
+ }
23
+ interface BFFResponseData {
24
+ conversationId: string;
25
+ message: string;
26
+ intent: string;
27
+ /** Any string — SDK only handles 'show_ui' internally, rest forwarded to onIntent */
28
+ action: string;
29
+ componentKey?: string;
30
+ path?: string;
31
+ suggest?: {
32
+ options: SuggestOption[];
33
+ };
34
+ timestamp: number;
35
+ }
36
+ interface BFFResponse {
37
+ code: number;
38
+ message: string;
39
+ data: BFFResponseData;
40
+ }
41
+ interface SDKRequest {
42
+ conversationId?: string;
43
+ userId?: string;
44
+ message?: string;
45
+ context?: Record<string, unknown>;
46
+ appContext?: Record<string, unknown>;
47
+ timestamp: number;
48
+ }
49
+ interface ChatMessage {
50
+ id: string;
51
+ role: 'user' | 'ai';
52
+ content: string;
53
+ options?: SuggestOption[];
54
+ timestamp: number;
55
+ isStreaming?: boolean;
56
+ }
57
+ interface DebugEvent {
58
+ /** Loại event:
59
+ * - `request`: SDK gửi message/context lên BFF
60
+ * - `response`: BFF trả về response
61
+ * - `intent`: onIntent được trigger (action + componentKey)
62
+ * - `context`: sendContext được gọi từ host app
63
+ */
64
+ type: 'request' | 'response' | 'intent' | 'context';
65
+ timestamp: number;
66
+ payload: unknown;
67
+ }
68
+ /** Frame protocol cho BFF → SDK streaming communication */
69
+ type TransportFrame = {
70
+ type: 'chunk';
71
+ text: string;
72
+ } | {
73
+ type: 'done';
74
+ data: BFFResponseData;
75
+ } | {
76
+ type: 'error';
77
+ message: string;
78
+ };
79
+ interface TekoChatWidgetProps {
80
+ appId: string;
81
+ /** BFF endpoint. Prefix xác định giao thức:
82
+ * - wss:// hoặc ws:// → WebSocket streaming
83
+ * - https:// hoặc http:// → HTTP streaming (ndjson)
84
+ * - chuỗi rỗng hoặc không truyền → mock handler (dev only)
85
+ */
86
+ chatBffUrl: string;
87
+ /** Callback khi BFF trả về intent action (show_ui, navigate, ...) */
88
+ onIntent?: (action: string, componentKey?: string) => void;
89
+ /** Render content cho right panel ở fullscreen state */
90
+ renderRightPanel?: (componentKey: string) => ReactNode;
91
+ /**
92
+ * Inject app-level metadata vào mỗi BFF request.
93
+ * Được gọi tại send-time — luôn resolve giá trị mới nhất.
94
+ * Support sync và async. Field `appContext` bị omit nếu trả về `{}`.
95
+ *
96
+ * @example
97
+ * getAppContext={() => ({ terminalId: '123', sessionKey: store.getKey() })}
98
+ * getAppContext={async () => await myApp.getSessionMeta()}
99
+ */
100
+ getAppContext?: () => Record<string, unknown> | Promise<Record<string, unknown>>;
101
+ /**
102
+ * Custom bubble — SDK inject onClick để open chat.
103
+ * Nếu không truyền, SDK dùng bubble mặc định (bottom-right).
104
+ */
105
+ renderBubble?: (onClick: () => void) => ReactNode;
106
+ /**
107
+ * Vị trí bubble, ảnh hưởng hướng animation fullscreen.
108
+ * @default 'bottom-right'
109
+ */
110
+ bubbleAnchor?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
111
+ /**
112
+ * Layout mode:
113
+ * - `'desktop'` (default): mini popup góc dưới phải, fullscreen split 35/65
114
+ * - `'mobile'`: bottom sheet + single column fullscreen
115
+ *
116
+ * SDK không tự detect viewport — host app tự detect và truyền vào.
117
+ * @default 'desktop'
118
+ */
119
+ layoutMode?: 'desktop' | 'mobile';
120
+ /** Primary color (hex) cho toàn bộ widget. @default '#1a73e8' */
121
+ primaryColor?: string;
122
+ /** Avatar URL cho AI bubble. Mặc định dùng icon robot. */
123
+ botAvatar?: string;
124
+ /** @default 'vi' */
125
+ locale?: 'vi' | 'en';
126
+ /** Override từng label cụ thể thay vì đổi toàn bộ locale */
127
+ labels?: Partial<ChatLabels>;
128
+ /**
129
+ * Offset từ top (px) — dùng khi host app có fixed header.
130
+ * Fullscreen bắt đầu từ offsetTop thay vì top: 0.
131
+ * @default 0
132
+ */
133
+ offsetTop?: number;
134
+ /**
135
+ * Offset từ bottom (px) — dùng khi host app có fixed bottom nav.
136
+ * @default 0
137
+ */
138
+ offsetBottom?: number;
139
+ /** Chiều rộng mini popup (px). @default 360 */
140
+ miniWidth?: number;
141
+ /** Chiều cao mini popup (px). @default 480 */
142
+ miniHeight?: number;
143
+ /**
144
+ * CSS z-index cho bubble và mini popup. Fullscreen backdrop = zIndex + 9.
145
+ * @default 1031
146
+ */
147
+ zIndex?: number;
148
+ /**
149
+ * @internal
150
+ * Nhận mọi SDK event (request/response/intent/context) để debug.
151
+ * Dùng cho DebugPanel hoặc Storybook. Không dùng trong production.
152
+ */
153
+ onDebugEvent?: (event: DebugEvent) => void;
154
+ }
155
+ interface TekoChatWidgetRef {
156
+ sendContext: (data: Record<string, unknown>) => void;
157
+ sendMessage: (text: string) => void;
158
+ open: () => void;
159
+ close: () => void;
160
+ openFullscreen: (componentKey: string) => void;
161
+ closeFullscreen: () => void;
162
+ }
163
+
164
+ declare const TekoChatWidget: react.ForwardRefExoticComponent<TekoChatWidgetProps & react.RefAttributes<TekoChatWidgetRef>>;
165
+
166
+ interface ITransport {
167
+ send(request: SDKRequest): void;
168
+ onChunk(callback: (text: string) => void): void;
169
+ onDone(callback: (data: BFFResponseData) => void): void;
170
+ onError(callback: (error: Error) => void): void;
171
+ connect(): Promise<void>;
172
+ disconnect(): void;
173
+ }
174
+
175
+ export { type BFFResponse, type BFFResponseData, type ChatLabels, type ChatMessage, type ChatState, type DebugEvent, type ITransport, type SDKRequest, type SuggestOption, TekoChatWidget, type TekoChatWidgetProps, type TekoChatWidgetRef, type TransportFrame };