translation-chat-sdk 0.1.6 → 0.1.7
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/README.md +64 -145
- package/dist/components/ChatWidget.d.ts +1 -3
- package/dist/index.d.ts +2 -1
- package/dist/translation-chat-sdk.cjs.js +22 -1
- package/dist/translation-chat-sdk.cjs.js.map +1 -1
- package/dist/translation-chat-sdk.es.js +1196 -917
- package/dist/translation-chat-sdk.es.js.map +1 -1
- package/dist/translation-chat-sdk.umd.js +22 -1
- package/dist/translation-chat-sdk.umd.js.map +1 -1
- package/dist/types.d.ts +0 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,26 +1,71 @@
|
|
|
1
1
|
# Translation Chat SDK (React)
|
|
2
2
|
|
|
3
|
-
React-first SDK for
|
|
3
|
+
React-first SDK for the Translation Studio chat/patch experience. Ships as a Vite-built library (ESM + CJS + UMD) with type-safe hooks, a provider, and a ready-to-use `ChatWidget`.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
|
+
- React Provider + `useChat` hook, no window listeners.
|
|
7
|
+
- Socket.io transport with token provider + context provider.
|
|
8
|
+
- Patch offer accumulation with callback and state exposure.
|
|
9
|
+
- Simple ChatWidget UI with streaming indicators.
|
|
10
|
+
- Build outputs for npm + CDN (jsDelivr-ready `translation-chat-sdk.umd.js`).
|
|
6
11
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
## Usage
|
|
13
|
+
```bash
|
|
14
|
+
npm install
|
|
15
|
+
npm run build
|
|
16
|
+
```
|
|
12
17
|
|
|
13
|
-
|
|
18
|
+
Add to a React app:
|
|
19
|
+
```tsx
|
|
20
|
+
import { ChatProvider, ChatWidget } from 'translation-chat-sdk';
|
|
21
|
+
|
|
22
|
+
<ChatProvider
|
|
23
|
+
config={{
|
|
24
|
+
websocketUrl: process.env.NEXT_PUBLIC_WEBSOCKET_URL!,
|
|
25
|
+
contextProvider: () => ({
|
|
26
|
+
trans_doc_id: currentDocId,
|
|
27
|
+
page_num: currentPage,
|
|
28
|
+
highlighted_region: selection
|
|
29
|
+
}),
|
|
30
|
+
tokenProvider: () => fetch('/api/auth/token', { credentials: 'include' })
|
|
31
|
+
.then(res => res.ok ? res.json() : null)
|
|
32
|
+
.then(data => data?.access_token ?? null),
|
|
33
|
+
onPatchOffer: (offer) => console.log('patch ready', offer)
|
|
34
|
+
}}
|
|
35
|
+
>
|
|
36
|
+
<ChatWidget />
|
|
37
|
+
</ChatProvider>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Local linking to `sarvam-studio-frontend`
|
|
41
|
+
1. From this repo: `npm install && npm run build`.
|
|
42
|
+
2. In `sarvam-studio-frontend/package.json`, set `"translation-chat-sdk": "file:../translation-chat-sdk"`.
|
|
43
|
+
3. Run `npm install` in `sarvam-studio-frontend`.
|
|
44
|
+
4. `npm run dev` and import from `translation-chat-sdk`.
|
|
45
|
+
|
|
46
|
+
## CDN build
|
|
47
|
+
After `npm run build`, publish `dist/translation-chat-sdk.umd.js` to a CDN (e.g., jsDelivr from a git tag). Consumers can `import('https://cdn.../translation-chat-sdk.umd.js')`.
|
|
48
|
+
## Translation Chat SDK (React)
|
|
49
|
+
|
|
50
|
+
React-first SDK for embedding Translation Studio chat in web apps. Ships ESM/CJS/UMD bundles, types, and a minimal widget.
|
|
51
|
+
|
|
52
|
+
### Install (local)
|
|
14
53
|
|
|
15
54
|
```bash
|
|
16
|
-
npm install
|
|
55
|
+
npm install
|
|
56
|
+
npm run build
|
|
17
57
|
```
|
|
18
58
|
|
|
19
|
-
|
|
59
|
+
Add to a consumer app (local path):
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npm install ../translation-chat-sdk
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Usage
|
|
20
66
|
|
|
21
67
|
```tsx
|
|
22
68
|
import { ChatProvider, ChatWidget } from 'translation-chat-sdk';
|
|
23
|
-
import 'translation-chat-sdk/style.css'; // Import styles
|
|
24
69
|
|
|
25
70
|
function App() {
|
|
26
71
|
return (
|
|
@@ -29,161 +74,35 @@ function App() {
|
|
|
29
74
|
websocketUrl: process.env.NEXT_PUBLIC_WEBSOCKET_URL!,
|
|
30
75
|
contextProvider: () => ({
|
|
31
76
|
trans_doc_id: 'doc-123',
|
|
32
|
-
page_num: 1
|
|
33
|
-
highlighted_region: selection // optional
|
|
77
|
+
page_num: 1
|
|
34
78
|
}),
|
|
35
79
|
tokenProvider: async () => {
|
|
36
80
|
const res = await fetch('/api/auth/token', { credentials: 'include' });
|
|
37
81
|
if (!res.ok) return null;
|
|
38
82
|
const { access_token } = await res.json();
|
|
39
83
|
return access_token;
|
|
40
|
-
},
|
|
41
|
-
onPatchOffer: (offer) => {
|
|
42
|
-
console.log('Patch ready:', offer);
|
|
43
84
|
}
|
|
44
85
|
}}
|
|
45
86
|
>
|
|
46
|
-
<ChatWidget height=
|
|
87
|
+
<ChatWidget height={520} />
|
|
47
88
|
</ChatProvider>
|
|
48
89
|
);
|
|
49
90
|
}
|
|
50
91
|
```
|
|
51
92
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
### `<ChatProvider config={...}>`
|
|
55
|
-
|
|
56
|
-
Wraps your app and provides chat context.
|
|
57
|
-
|
|
58
|
-
#### Config Options
|
|
59
|
-
|
|
60
|
-
| Property | Type | Required | Description |
|
|
61
|
-
|----------|------|----------|-------------|
|
|
62
|
-
| `websocketUrl` | `string` | ✅ | WebSocket server URL |
|
|
63
|
-
| `contextProvider` | `() => InstructionContext \| null` | ✅ | Returns current document context |
|
|
64
|
-
| `tokenProvider` | `() => Promise<string \| null>` | ❌ | Async function to get auth token |
|
|
65
|
-
| `onPatchOffer` | `(offer: PatchOfferEventDetail) => void` | ❌ | Callback when patch suggestion is ready |
|
|
66
|
-
| `onPageTranslationResponse` | `(payload) => void` | ❌ | Callback for page translation responses |
|
|
67
|
-
| `onBulkTranslationResponse` | `(payload) => void` | ❌ | Callback for bulk translation responses |
|
|
68
|
-
| `onConnectionChange` | `(state: ConnectionState) => void` | ❌ | Connection state change callback |
|
|
69
|
-
| `reconnectAttempts` | `number` | ❌ | Max reconnection attempts (default: 5) |
|
|
70
|
-
| `reconnectDelayMs` | `number` | ❌ | Delay between reconnects (default: 1500) |
|
|
71
|
-
| `logger` | `Console` | ❌ | Custom logger |
|
|
72
|
-
|
|
73
|
-
### `<ChatWidget />`
|
|
74
|
-
|
|
75
|
-
Pre-built chat UI component.
|
|
76
|
-
|
|
77
|
-
| Prop | Type | Default | Description |
|
|
78
|
-
|------|------|---------|-------------|
|
|
79
|
-
| `title` | `string` | `"AI Assistance"` | Header title |
|
|
80
|
-
| `placeholder` | `string` | `"Ask anything..."` | Input placeholder |
|
|
81
|
-
| `height` | `string` | `"420px"` | Widget height |
|
|
82
|
-
|
|
83
|
-
### `useChat()`
|
|
84
|
-
|
|
85
|
-
Hook to access chat state and actions.
|
|
86
|
-
|
|
87
|
-
```tsx
|
|
88
|
-
const {
|
|
89
|
-
// State
|
|
90
|
-
messages, // ChatMessage[]
|
|
91
|
-
connection, // { connected, connecting, error? }
|
|
92
|
-
latestPatchOffer, // PatchOfferEventDetail | null
|
|
93
|
-
isSubmitting, // boolean
|
|
94
|
-
|
|
95
|
-
// Actions
|
|
96
|
-
submitInstruction, // (instruction: string) => Promise<void>
|
|
97
|
-
submitInstructionWithContext,// (instruction, context) => Promise<void>
|
|
98
|
-
requestChatHistory, // () => Promise<void>
|
|
99
|
-
sendPatchDecision, // (transDocId, pageNum, instrId, 'accept'|'reject') => Promise<void>
|
|
100
|
-
requestPageTranslation, // (transDocId, pageNum) => Promise<boolean>
|
|
101
|
-
requestBulkTranslation, // (transDocId, pageList?) => Promise<boolean>
|
|
102
|
-
cancelBulkTranslation, // (transDocId) => Promise<boolean>
|
|
103
|
-
clearMessages, // () => void
|
|
104
|
-
} = useChat();
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## Types
|
|
108
|
-
|
|
109
|
-
```typescript
|
|
110
|
-
type InstructionContext = {
|
|
111
|
-
trans_doc_id: string;
|
|
112
|
-
page_num: number;
|
|
113
|
-
highlighted_region?: {
|
|
114
|
-
start_offset: number;
|
|
115
|
-
end_offset: number;
|
|
116
|
-
text: string;
|
|
117
|
-
};
|
|
118
|
-
selection_id?: string;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
type ChatMessage = {
|
|
122
|
-
id: string;
|
|
123
|
-
type: 'user' | 'ai-chat' | 'ai-patch' | 'system' | 'loading' | 'error';
|
|
124
|
-
content: string;
|
|
125
|
-
timestamp: Date;
|
|
126
|
-
instructionId?: string;
|
|
127
|
-
pageNum?: number;
|
|
128
|
-
patchData?: PatchData;
|
|
129
|
-
isStreaming?: boolean;
|
|
130
|
-
contextText?: string;
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
type PatchOfferEventDetail = {
|
|
134
|
-
trans_doc_id: string;
|
|
135
|
-
page_num: number;
|
|
136
|
-
instruction_id: string;
|
|
137
|
-
original_text: string;
|
|
138
|
-
highlighted_region?: { start_offset: number; end_offset: number };
|
|
139
|
-
new_suggestion: string;
|
|
140
|
-
is_page_level: boolean;
|
|
141
|
-
selection_id?: string;
|
|
142
|
-
};
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
## CDN Usage
|
|
93
|
+
### CDN / jsDelivr
|
|
146
94
|
|
|
147
|
-
|
|
95
|
+
Build generates `dist/translation-chat-sdk.umd.js`. Publish to npm and load via:
|
|
148
96
|
|
|
149
97
|
```html
|
|
150
98
|
<script src="https://cdn.jsdelivr.net/npm/translation-chat-sdk/dist/translation-chat-sdk.umd.js"></script>
|
|
151
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/translation-chat-sdk/dist/style.css">
|
|
152
|
-
<script>
|
|
153
|
-
const { ChatProvider, ChatWidget, useChat } = TranslationChatSDK;
|
|
154
|
-
</script>
|
|
155
99
|
```
|
|
156
100
|
|
|
157
|
-
|
|
101
|
+
Globals exposed: `TranslationChatSDK.ChatProvider`, `TranslationChatSDK.ChatWidget`, `TranslationChatSDK.useChat`.
|
|
158
102
|
|
|
159
|
-
|
|
160
|
-
# Install dependencies
|
|
161
|
-
npm install
|
|
162
|
-
|
|
163
|
-
# Run dev server
|
|
164
|
-
npm run dev
|
|
165
|
-
|
|
166
|
-
# Build library
|
|
167
|
-
npm run build
|
|
168
|
-
|
|
169
|
-
# Type check
|
|
170
|
-
npm run typecheck
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
## Local Linking
|
|
174
|
-
|
|
175
|
-
For development with `sarvam-studio-frontend`:
|
|
176
|
-
|
|
177
|
-
1. Build the SDK: `npm run build`
|
|
178
|
-
2. In frontend `package.json`: `"translation-chat-sdk": "file:../translation-chat-sdk"`
|
|
179
|
-
3. Run `npm install` in the frontend
|
|
180
|
-
4. Start dev server
|
|
181
|
-
|
|
182
|
-
**Note**: After rebuilding the SDK, you may need to remove duplicate React from the SDK's node_modules:
|
|
183
|
-
```bash
|
|
184
|
-
rm -rf node_modules/react node_modules/react-dom
|
|
185
|
-
```
|
|
103
|
+
### Scripts
|
|
186
104
|
|
|
187
|
-
|
|
105
|
+
- `npm run dev` — local playground via Vite
|
|
106
|
+
- `npm run build` — library build (ESM/CJS/UMD + d.ts)
|
|
107
|
+
- `npm run preview` — preview build
|
|
188
108
|
|
|
189
|
-
MIT
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { ChatWidgetHandle } from '../types';
|
|
3
1
|
import '../styles/chat.css';
|
|
4
2
|
type Props = {
|
|
5
3
|
title?: string;
|
|
6
4
|
placeholder?: string;
|
|
7
5
|
height?: string;
|
|
8
6
|
};
|
|
9
|
-
export declare
|
|
7
|
+
export declare function ChatWidget({ title, placeholder, height }: Props): import("react/jsx-runtime").JSX.Element;
|
|
10
8
|
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './types';
|
|
2
2
|
export { ChatProvider } from './provider/ChatProvider';
|
|
3
3
|
export { useChat } from './hooks/useChat';
|
|
4
4
|
export { ChatWidget } from './components/ChatWidget';
|
|
5
|
+
export { default as style } from './styles/chat.css?inline';
|