ttc-ai-chat-sdk 1.2.2 → 1.3.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.
- package/README.md +138 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -173,6 +173,144 @@ const models = await assistant.getModels();
|
|
|
173
173
|
await assistant.selectModel("model-id", "llm"); // type: 'llm' | 'stt' | 'tts'
|
|
174
174
|
```
|
|
175
175
|
|
|
176
|
+
## Assistant API (Server-Side)
|
|
177
|
+
|
|
178
|
+
The `AssistantAPI` allows you to manage assistants from your backend. Use it to create assistants, generate tokens, and manage assistant metadata.
|
|
179
|
+
|
|
180
|
+
### Initialize the Assistant API
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { ttc } from "ttc-ai-chat-sdk";
|
|
184
|
+
|
|
185
|
+
const apiKey = "your-api-key";
|
|
186
|
+
const assistantAPI = ttc.assistantAPI(apiKey);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Create an Assistant
|
|
190
|
+
|
|
191
|
+
Create a new assistant for a user and get a chat token for the frontend:
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
const result = await assistantAPI.create(
|
|
195
|
+
"My Assistant", // name
|
|
196
|
+
"user-unique-id-123", // cuid (custom user ID)
|
|
197
|
+
{
|
|
198
|
+
backstory: "You are a helpful shopping assistant",
|
|
199
|
+
emote: "friendly",
|
|
200
|
+
traits: "helpful, knowledgeable",
|
|
201
|
+
metadata: { theme: "dark" }
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
if (result.status === "success") {
|
|
206
|
+
const { chatId, token } = result.data;
|
|
207
|
+
// Send token to frontend
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Generate a New Token
|
|
212
|
+
|
|
213
|
+
Generate a fresh token for an existing assistant:
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
const result = await assistantAPI.generateToken(
|
|
217
|
+
"user-unique-id-123", // cuid
|
|
218
|
+
"30d" // expiry duration
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
if (result.status === "success") {
|
|
222
|
+
const { token } = result.data;
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Validate a Token
|
|
227
|
+
|
|
228
|
+
Check if a token is still valid:
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
const result = await assistantAPI.validateToken(token);
|
|
232
|
+
|
|
233
|
+
if (result.status === "success") {
|
|
234
|
+
// result.data.status: 'valid' | 'expired' | 'error' | 'notFound'
|
|
235
|
+
console.log("Token status:", result.data.status);
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Fetch Assistants
|
|
240
|
+
|
|
241
|
+
Get a list of all assistants or fetch a specific one:
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
// Fetch all assistants (paginated)
|
|
245
|
+
const list = await assistantAPI.fetchMany(1, 10, {});
|
|
246
|
+
|
|
247
|
+
// Fetch a specific assistant by ID or cuid
|
|
248
|
+
const assistant = await assistantAPI.fetch({ cuid: "user-unique-id-123" });
|
|
249
|
+
// or
|
|
250
|
+
const assistant = await assistantAPI.fetch({ id: "assistant-id" });
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Update an Assistant
|
|
254
|
+
|
|
255
|
+
Update assistant metadata or persona:
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
await assistantAPI.update("assistant-id", {
|
|
259
|
+
name: "Updated Name",
|
|
260
|
+
backstory: "New backstory",
|
|
261
|
+
metadata: { theme: "light" }
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Delete an Assistant
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
await assistantAPI.delete("assistant-id");
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### Express Integration (Auth Server)
|
|
272
|
+
|
|
273
|
+
For seamless token management, you can integrate with Express to automatically handle token creation and validation:
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
import express from "express";
|
|
277
|
+
import { ttc } from "ttc-ai-chat-sdk";
|
|
278
|
+
|
|
279
|
+
const app = express();
|
|
280
|
+
|
|
281
|
+
ttc.assistantAPI("your-api-key", {
|
|
282
|
+
express: app,
|
|
283
|
+
endpoint: "/ttc-auth",
|
|
284
|
+
userData: async (req) => {
|
|
285
|
+
// Extract user info from your auth system
|
|
286
|
+
const user = await getUserFromRequest(req);
|
|
287
|
+
return {
|
|
288
|
+
cuid: user.id,
|
|
289
|
+
name: user.name,
|
|
290
|
+
option: {
|
|
291
|
+
backstory: "Helpful assistant for " + user.name,
|
|
292
|
+
metadata: { userId: user.id }
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
app.listen(3000);
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
Then on the frontend, initialize the assistant with the auth URL:
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
const assistant = await ttc.assistant({
|
|
305
|
+
modules: [MyAppModule],
|
|
306
|
+
url: "http://localhost:3000/ttc-auth",
|
|
307
|
+
tokenCb: async () => {
|
|
308
|
+
// Return your platform's auth token
|
|
309
|
+
return localStorage.getItem("authToken");
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
```
|
|
313
|
+
|
|
176
314
|
## Full Documentation
|
|
177
315
|
|
|
178
316
|
For complete API reference, all available methods, and advanced configuration, check out the full SDK documentation.
|