@chatcops/server 0.2.0 → 0.2.1
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/LICENSE +21 -0
- package/README.md +164 -0
- package/package.json +17 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CoderCops
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# @chatcops/server
|
|
2
|
+
|
|
3
|
+
Server-side handler and platform adapters for ChatCops. Handles streaming responses, rate limiting, webhooks, and analytics.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @chatcops/server @chatcops/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### Express
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import express from 'express';
|
|
17
|
+
import { chatcopsMiddleware } from '@chatcops/server';
|
|
18
|
+
|
|
19
|
+
const app = express();
|
|
20
|
+
app.use(express.json());
|
|
21
|
+
|
|
22
|
+
app.post('/chat', chatcopsMiddleware({
|
|
23
|
+
provider: {
|
|
24
|
+
type: 'claude',
|
|
25
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
26
|
+
},
|
|
27
|
+
systemPrompt: 'You are a helpful assistant for our website.',
|
|
28
|
+
cors: '*',
|
|
29
|
+
}));
|
|
30
|
+
|
|
31
|
+
app.listen(3001);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Vercel Edge
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { chatcopsVercelHandler } from '@chatcops/server';
|
|
38
|
+
|
|
39
|
+
const handler = chatcopsVercelHandler({
|
|
40
|
+
provider: { type: 'openai', apiKey: process.env.OPENAI_API_KEY },
|
|
41
|
+
systemPrompt: 'You are a helpful assistant.',
|
|
42
|
+
cors: '*',
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export const POST = (req: Request) => handler(req);
|
|
46
|
+
export const OPTIONS = (req: Request) => handler(req);
|
|
47
|
+
export const config = { runtime: 'edge' };
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Cloudflare Workers
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { chatcopsCloudflareHandler } from '@chatcops/server';
|
|
54
|
+
|
|
55
|
+
export default {
|
|
56
|
+
async fetch(request: Request, env: Env) {
|
|
57
|
+
const handler = chatcopsCloudflareHandler({
|
|
58
|
+
provider: { type: 'claude', apiKey: env.ANTHROPIC_API_KEY },
|
|
59
|
+
systemPrompt: 'You are a helpful assistant.',
|
|
60
|
+
cors: '*',
|
|
61
|
+
});
|
|
62
|
+
return handler(request);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Configuration
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { createChatHandler } from '@chatcops/server';
|
|
71
|
+
import { LeadCaptureTool, FAQKnowledgeSource } from '@chatcops/core';
|
|
72
|
+
|
|
73
|
+
const handler = createChatHandler({
|
|
74
|
+
// AI provider (required)
|
|
75
|
+
provider: {
|
|
76
|
+
type: 'claude', // 'claude' | 'openai' | 'gemini'
|
|
77
|
+
apiKey: 'sk-...',
|
|
78
|
+
model: 'claude-haiku-4-5-20251001',
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
// System prompt (required)
|
|
82
|
+
systemPrompt: 'You are a helpful support assistant.',
|
|
83
|
+
|
|
84
|
+
// Tools (optional)
|
|
85
|
+
tools: [
|
|
86
|
+
new LeadCaptureTool((lead) => {
|
|
87
|
+
console.log('New lead:', lead.email);
|
|
88
|
+
}),
|
|
89
|
+
],
|
|
90
|
+
|
|
91
|
+
// Knowledge base (optional)
|
|
92
|
+
knowledge: [
|
|
93
|
+
new FAQKnowledgeSource([
|
|
94
|
+
{ question: 'What do you do?', answer: 'We build software.' },
|
|
95
|
+
]),
|
|
96
|
+
],
|
|
97
|
+
|
|
98
|
+
// Rate limiting (optional, default: 30 req/60s)
|
|
99
|
+
rateLimit: { maxRequests: 10, windowMs: 60_000 },
|
|
100
|
+
|
|
101
|
+
// Webhooks (optional)
|
|
102
|
+
webhooks: [
|
|
103
|
+
{ url: 'https://hooks.example.com/leads', events: ['lead:captured'] },
|
|
104
|
+
],
|
|
105
|
+
|
|
106
|
+
// Analytics (optional)
|
|
107
|
+
analytics: true,
|
|
108
|
+
|
|
109
|
+
// CORS origin (required)
|
|
110
|
+
cors: '*',
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## SSE Stream Format
|
|
115
|
+
|
|
116
|
+
All adapters produce identical Server-Sent Events:
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
data: {"content":"Hello"}
|
|
120
|
+
data: {"content":" there!"}
|
|
121
|
+
data: {"done":true}
|
|
122
|
+
data: [DONE]
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
On error:
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
data: {"error":"rate_limit","retryAfter":42}
|
|
129
|
+
data: [DONE]
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Error codes: `invalid_request`, `rate_limit`, `provider_error`, `internal_error`.
|
|
133
|
+
|
|
134
|
+
## Rate Limiting
|
|
135
|
+
|
|
136
|
+
In-memory, per-IP. Suitable for single-instance deployments.
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { RateLimiter } from '@chatcops/server';
|
|
140
|
+
|
|
141
|
+
const limiter = new RateLimiter({ maxRequests: 30, windowMs: 60_000 });
|
|
142
|
+
const result = limiter.check('127.0.0.1');
|
|
143
|
+
// { allowed: true } or { allowed: false, retryAfter: 42 }
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Webhooks
|
|
147
|
+
|
|
148
|
+
HMAC-SHA256 signed, with exponential backoff retries (up to 3 attempts).
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { WebhookDispatcher } from '@chatcops/server';
|
|
152
|
+
|
|
153
|
+
const dispatcher = new WebhookDispatcher([
|
|
154
|
+
{ url: 'https://hooks.example.com/chat', events: ['*'], secret: 'my-secret' },
|
|
155
|
+
]);
|
|
156
|
+
|
|
157
|
+
await dispatcher.dispatch('lead:captured', { email: 'user@example.com' });
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Signature sent via `X-ChatCops-Signature` header.
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chatcops/server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Server-side handler and adapters for ChatCops",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -26,16 +26,9 @@
|
|
|
26
26
|
"files": [
|
|
27
27
|
"dist"
|
|
28
28
|
],
|
|
29
|
-
"scripts": {
|
|
30
|
-
"build": "tsc",
|
|
31
|
-
"typecheck": "tsc --noEmit",
|
|
32
|
-
"test": "vitest run",
|
|
33
|
-
"test:watch": "vitest",
|
|
34
|
-
"clean": "rm -rf dist *.tsbuildinfo"
|
|
35
|
-
},
|
|
36
29
|
"dependencies": {
|
|
37
|
-
"
|
|
38
|
-
"
|
|
30
|
+
"zod": "^3.24.0",
|
|
31
|
+
"@chatcops/core": "0.2.1"
|
|
39
32
|
},
|
|
40
33
|
"peerDependencies": {
|
|
41
34
|
"express": "^4.0.0 || ^5.0.0"
|
|
@@ -50,8 +43,20 @@
|
|
|
50
43
|
"vitest": "^3.0.0",
|
|
51
44
|
"@types/express": "^5.0.0"
|
|
52
45
|
},
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "https://github.com/codercops/chatcops",
|
|
49
|
+
"directory": "packages/server"
|
|
50
|
+
},
|
|
53
51
|
"publishConfig": {
|
|
54
52
|
"access": "public"
|
|
55
53
|
},
|
|
56
|
-
"license": "MIT"
|
|
57
|
-
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsc",
|
|
57
|
+
"typecheck": "tsc --noEmit",
|
|
58
|
+
"test": "vitest run",
|
|
59
|
+
"test:watch": "vitest",
|
|
60
|
+
"clean": "rm -rf dist *.tsbuildinfo"
|
|
61
|
+
}
|
|
62
|
+
}
|