@convokit/server 0.1.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 +114 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +83 -0
- package/dist/index.js.map +1 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# @convokit/server
|
|
2
|
+
|
|
3
|
+
> **ConvoKit is currently in early access.** The waitlist is live — [join at convokit.dev](https://convokit.dev) to get early access.
|
|
4
|
+
|
|
5
|
+
Server SDK for [ConvoKit](https://convokit.dev) — send AI chat messages from your server using your **secret API key**. The key never leaves your backend, making this more secure than using a public key in the browser.
|
|
6
|
+
|
|
7
|
+
**Website:** [convokit.dev](https://convokit.dev)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @convokit/server
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const { createClient } = require('@convokit/server');
|
|
23
|
+
|
|
24
|
+
const client = createClient(process.env.CONVOKIT_SECRET_KEY); // sk_live_...
|
|
25
|
+
|
|
26
|
+
// New conversation
|
|
27
|
+
const { reply, conversation_id } = await client.chat({
|
|
28
|
+
message: 'What are your support hours?',
|
|
29
|
+
});
|
|
30
|
+
console.log(reply);
|
|
31
|
+
|
|
32
|
+
// Follow-up in the same thread
|
|
33
|
+
const { reply: reply2 } = await client.chat({
|
|
34
|
+
message: 'And on weekends?',
|
|
35
|
+
conversationId: conversation_id,
|
|
36
|
+
});
|
|
37
|
+
console.log(reply2);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## TypeScript
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { createClient, type ChatOptions, type ChatResponse } from '@convokit/server';
|
|
46
|
+
|
|
47
|
+
const client = createClient({ apiKey: process.env.CONVOKIT_SECRET_KEY! });
|
|
48
|
+
|
|
49
|
+
const res: ChatResponse = await client.chat({
|
|
50
|
+
message: 'Hello',
|
|
51
|
+
widgetId: 'my-bot',
|
|
52
|
+
chatHistory: [],
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Conversation history
|
|
59
|
+
|
|
60
|
+
Pass previous messages so the AI has context:
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
const { reply } = await client.chat({
|
|
64
|
+
message: 'What did I just ask?',
|
|
65
|
+
chatHistory: [
|
|
66
|
+
{ role: 'user', content: 'What are your support hours?' },
|
|
67
|
+
{ role: 'assistant', content: 'We are open 9am–5pm Monday to Friday.' },
|
|
68
|
+
],
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Format: `{ role: 'user' | 'assistant' | 'system', content: string }`.
|
|
73
|
+
|
|
74
|
+
When you have a `conversationId`, you can omit `chatHistory` — the backend already has the thread.
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## Options
|
|
79
|
+
|
|
80
|
+
| Option | Required | Description |
|
|
81
|
+
|--------|----------|-------------|
|
|
82
|
+
| `message` | Yes | The user message to send |
|
|
83
|
+
| `conversationId` | No | Use for follow-up messages in the same thread |
|
|
84
|
+
| `widgetId` | No | Identifier for grouping (e.g. bot name, channel) |
|
|
85
|
+
| `chatHistory` | No | Previous messages for context |
|
|
86
|
+
|
|
87
|
+
## Response
|
|
88
|
+
|
|
89
|
+
| Field | Description |
|
|
90
|
+
|-------|-------------|
|
|
91
|
+
| `reply` | AI reply text |
|
|
92
|
+
| `conversation_id` | Pass this in the next message to continue the thread |
|
|
93
|
+
| `usage` | Token usage — `prompt_tokens`, `completion_tokens`, `total_tokens` |
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Security
|
|
98
|
+
|
|
99
|
+
- Use a **secret key** (`sk_live_...`) on your server only. Never expose it in browser or client-side code.
|
|
100
|
+
- For browser / widget use, use a **public key** (`pk_live_...`) with the [`@convokit/widget`](https://www.npmjs.com/package/@convokit/widget) package.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## Early access
|
|
105
|
+
|
|
106
|
+
ConvoKit is in early access. Get your API keys and configure your AI at **[convokit.dev](https://convokit.dev)**.
|
|
107
|
+
|
|
108
|
+
The waitlist is live — join now to get early access.
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ConvoKit Server SDK
|
|
3
|
+
* Use your secret API key (sk_live_...) for secure server-to-server chat.
|
|
4
|
+
* Never expose the secret key in the browser — use the widget with a public key instead.
|
|
5
|
+
*/
|
|
6
|
+
/** ConvoKit API URL. */
|
|
7
|
+
export declare const DEFAULT_API_URL = "https://api.convokit.com";
|
|
8
|
+
/**
|
|
9
|
+
* One message in the conversation history.
|
|
10
|
+
* Format: { role: 'user' | 'assistant' | 'system', content: string }
|
|
11
|
+
* Example: user: "What are your hours?", assistant: "We're open 9–5."
|
|
12
|
+
*/
|
|
13
|
+
export interface ChatHistoryItem {
|
|
14
|
+
role: 'user' | 'assistant' | 'system';
|
|
15
|
+
content: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ChatOptions {
|
|
18
|
+
/** User message to send */
|
|
19
|
+
message: string;
|
|
20
|
+
/** Existing conversation ID for follow-up messages; omit for a new conversation */
|
|
21
|
+
conversationId?: string;
|
|
22
|
+
/** Optional widget/session identifier for grouping */
|
|
23
|
+
widgetId?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Previous messages in the thread (for context).
|
|
26
|
+
* Format: array of { role: 'user' | 'assistant' | 'system', content: string }
|
|
27
|
+
* Example: [ { role: 'user', content: 'Hi' }, { role: 'assistant', content: 'Hello!' } ]
|
|
28
|
+
*/
|
|
29
|
+
chatHistory?: ChatHistoryItem[];
|
|
30
|
+
}
|
|
31
|
+
export interface ChatResponse {
|
|
32
|
+
/** AI reply text */
|
|
33
|
+
reply: string;
|
|
34
|
+
/** Conversation ID (use for follow-up messages) */
|
|
35
|
+
conversation_id: string;
|
|
36
|
+
/** Token usage from the model */
|
|
37
|
+
usage: {
|
|
38
|
+
prompt_tokens?: number;
|
|
39
|
+
completion_tokens?: number;
|
|
40
|
+
total_tokens?: number;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface ConvoKitServerConfig {
|
|
44
|
+
/** Secret API key (sk_live_...). Never use in the browser. */
|
|
45
|
+
apiKey: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* ConvoKit Server client for server-to-server chat.
|
|
49
|
+
* Authenticate with your secret key; the key never leaves your server.
|
|
50
|
+
*/
|
|
51
|
+
export declare class ConvoKitServerClient {
|
|
52
|
+
private readonly baseUrl;
|
|
53
|
+
private readonly apiKey;
|
|
54
|
+
constructor(config: ConvoKitServerConfig | string);
|
|
55
|
+
/**
|
|
56
|
+
* Send a message and get an AI reply.
|
|
57
|
+
* Use conversationId for follow-up messages in the same thread.
|
|
58
|
+
*/
|
|
59
|
+
chat(options: ChatOptions): Promise<ChatResponse>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create a ConvoKit server client. Uses the ConvoKit API at https://api.convokit.com.
|
|
63
|
+
* @param apiKey - Secret API key (sk_live_...) — or pass { apiKey }
|
|
64
|
+
*/
|
|
65
|
+
export declare function createClient(apiKey: string): ConvoKitServerClient;
|
|
66
|
+
export declare function createClient(config: ConvoKitServerConfig): ConvoKitServerClient;
|
|
67
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d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ist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ConvoKit Server SDK
|
|
4
|
+
* Use your secret API key (sk_live_...) for secure server-to-server chat.
|
|
5
|
+
* Never expose the secret key in the browser — use the widget with a public key instead.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ConvoKitServerClient = exports.DEFAULT_API_URL = void 0;
|
|
9
|
+
exports.createClient = createClient;
|
|
10
|
+
/** ConvoKit API URL. */
|
|
11
|
+
exports.DEFAULT_API_URL = 'https://api.convokit.com';
|
|
12
|
+
/**
|
|
13
|
+
* ConvoKit Server client for server-to-server chat.
|
|
14
|
+
* Authenticate with your secret key; the key never leaves your server.
|
|
15
|
+
*/
|
|
16
|
+
class ConvoKitServerClient {
|
|
17
|
+
constructor(config) {
|
|
18
|
+
if (typeof config === 'string') {
|
|
19
|
+
this.apiKey = config;
|
|
20
|
+
this.baseUrl = exports.DEFAULT_API_URL;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
this.apiKey = config.apiKey;
|
|
24
|
+
this.baseUrl = exports.DEFAULT_API_URL;
|
|
25
|
+
}
|
|
26
|
+
if (!this.apiKey) {
|
|
27
|
+
throw new Error('ConvoKit server client requires an API key (sk_live_...)');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Send a message and get an AI reply.
|
|
32
|
+
* Use conversationId for follow-up messages in the same thread.
|
|
33
|
+
*/
|
|
34
|
+
async chat(options) {
|
|
35
|
+
const { message, conversationId, widgetId, chatHistory = [] } = options;
|
|
36
|
+
const url = `${this.baseUrl}/api/server/chat`;
|
|
37
|
+
const body = {
|
|
38
|
+
message,
|
|
39
|
+
chat_history: chatHistory,
|
|
40
|
+
};
|
|
41
|
+
if (conversationId)
|
|
42
|
+
body.conversation_id = conversationId;
|
|
43
|
+
if (widgetId)
|
|
44
|
+
body.widget_id = widgetId;
|
|
45
|
+
const res = await fetch(url, {
|
|
46
|
+
method: 'POST',
|
|
47
|
+
headers: {
|
|
48
|
+
'Content-Type': 'application/json',
|
|
49
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify(body),
|
|
52
|
+
});
|
|
53
|
+
const text = await res.text();
|
|
54
|
+
let data;
|
|
55
|
+
try {
|
|
56
|
+
data = JSON.parse(text);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
data = {};
|
|
60
|
+
}
|
|
61
|
+
if (!res.ok) {
|
|
62
|
+
const err = data?.error ?? res.statusText ?? 'Request failed';
|
|
63
|
+
throw new Error(typeof err === 'string' ? err : JSON.stringify(err));
|
|
64
|
+
}
|
|
65
|
+
const out = data;
|
|
66
|
+
if (out == null || typeof out.reply !== 'string') {
|
|
67
|
+
throw new Error('Invalid response format from ConvoKit API');
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
reply: out.reply,
|
|
71
|
+
conversation_id: String(out.conversation_id ?? ''),
|
|
72
|
+
usage: out.usage,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.ConvoKitServerClient = ConvoKitServerClient;
|
|
77
|
+
function createClient(configOrApiKey) {
|
|
78
|
+
if (typeof configOrApiKey === 'string') {
|
|
79
|
+
return new ConvoKitServerClient(configOrApiKey);
|
|
80
|
+
}
|
|
81
|
+
return new ConvoKitServerClient(configOrApiKey);
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAyHH,oCAKC;AA5HD,wBAAwB;AACX,QAAA,eAAe,GAAG,0BAA0B,CAAC;AA6C1D;;;GAGG;AACH,MAAa,oBAAoB;IAI/B,YAAY,MAAqC;QAC/C,IAAI,OAAO,MA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,GAAG,EAAE,CAAC;QACZ,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;YACZ,MAAM,GAAG,GAAI,IAAgC,EAAE,KAAK,IAAI,GAAG,CAAC,UAAU,IAAI,gBAAgB,CAAC;YAC3F,MAAM,IAAI,KAAK,CAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC;QACvE,CAAC;QACD,MAAM,GAAG,GAAG,IAA+B,CAAC;QAC5C,IAAI,GAAG,IAAI,IAAI,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YACjD,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO;YACL,KAAK,EAAE,GAAG,CAAC,KAAe;YAC1B,eAAe,EAAE,MAAM,CAAC,GAAG,CAAC,eAAe,IAAI,EAAE,CAAC;YAClD,KAAK,EAAE,GAAG,CAAC,KAA8B;SAC1C,CAAC;IACJ,CAAC;CACF;AA7DD,oDA6DC;AAQD,SAAgB,YAAY,CAAC,cAA6C;IACxE,IAAI,OAAO,cAAc,KAAK,QAAQ,EAAE,CAAC;QACvC,OAAO,IAAI,oBAAoB,CAAC,cAAc,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,IAAI,oBAAoB,CAAC,cAAc,CAAC,CAAC;AAClD,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@convokit/server",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "ConvoKit server SDK — secure server-to-server chat using your secret API key",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"convokit",
|
|
17
|
+
"chat",
|
|
18
|
+
"ai",
|
|
19
|
+
"server",
|
|
20
|
+
"sdk",
|
|
21
|
+
"api"
|
|
22
|
+
],
|
|
23
|
+
"author": "",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"typescript": "^5.3.3"
|
|
30
|
+
}
|
|
31
|
+
}
|