cencori 0.2.3 → 0.3.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/README.md +140 -140
- package/dist/index.d.mts +26 -8
- package/dist/index.d.ts +26 -8
- package/dist/index.js +71 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +42 -33
package/README.md
CHANGED
|
@@ -1,140 +1,140 @@
|
|
|
1
|
-
# Cencori
|
|
2
|
-
|
|
3
|
-
Official SDK for the Cencori.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install cencori
|
|
9
|
-
# or
|
|
10
|
-
yarn add cencori
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Quick Start
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { CencoriClient } from 'cencori';
|
|
17
|
-
|
|
18
|
-
const cencori = new CencoriClient({
|
|
19
|
-
apiKey: process.env.CENCORI_API_KEY!
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
const response = await cencori.ai.chat({
|
|
23
|
-
messages: [
|
|
24
|
-
{ role: 'user', content: 'Hello, AI!' }
|
|
25
|
-
]
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
console.log(response.content);
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Authentication
|
|
32
|
-
|
|
33
|
-
Get your API key from the [Cencori Dashboard](https://cencori.com/dashboard):
|
|
34
|
-
|
|
35
|
-
1. Create a project
|
|
36
|
-
2. Navigate to API Keys
|
|
37
|
-
3. Generate a new key
|
|
38
|
-
4. Copy and store it securely
|
|
39
|
-
|
|
40
|
-
## API Reference
|
|
41
|
-
|
|
42
|
-
### CencoriClient
|
|
43
|
-
|
|
44
|
-
Initialize the SDK client.
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
const cencori = new CencoriClient({
|
|
48
|
-
apiKey: 'your_api_key_here',
|
|
49
|
-
baseUrl: 'https://cencori.com' // Optional, defaults to production
|
|
50
|
-
});
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
### AI Module
|
|
54
|
-
|
|
55
|
-
#### `ai.chat(params)`
|
|
56
|
-
|
|
57
|
-
Send a chat message to the AI.
|
|
58
|
-
|
|
59
|
-
**Parameters:**
|
|
60
|
-
- `messages`: Array of message objects with `role` ('user' | 'assistant') and `content`
|
|
61
|
-
- `model`: Optional AI model (defaults to 'gemini-1.5-
|
|
62
|
-
- `temperature`: Optional temperature (0-1)
|
|
63
|
-
- `maxOutputTokens`: Optional max tokens for response
|
|
64
|
-
|
|
65
|
-
**Example:**
|
|
66
|
-
|
|
67
|
-
```typescript
|
|
68
|
-
const response = await cencori.ai.chat({
|
|
69
|
-
messages: [
|
|
70
|
-
{ role: 'user', content: 'Explain quantum computing' }
|
|
71
|
-
],
|
|
72
|
-
temperature: 0.7
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
console.log(response.content);
|
|
76
|
-
console.log(response.usage); // Token usage stats
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
## Error Handling
|
|
80
|
-
|
|
81
|
-
The SDK includes custom error classes for common scenarios:
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
import {
|
|
85
|
-
CencoriClient,
|
|
86
|
-
AuthenticationError,
|
|
87
|
-
RateLimitError,
|
|
88
|
-
SafetyError
|
|
89
|
-
} from 'cencori';
|
|
90
|
-
|
|
91
|
-
try {
|
|
92
|
-
const response = await cencori.ai.chat({ messages: [...] });
|
|
93
|
-
} catch (error) {
|
|
94
|
-
if (error instanceof AuthenticationError) {
|
|
95
|
-
console.error('Invalid API key');
|
|
96
|
-
} else if (error instanceof RateLimitError) {
|
|
97
|
-
console.error('Too many requests, please slow down');
|
|
98
|
-
} else if (error instanceof SafetyError) {
|
|
99
|
-
console.error('Content blocked:', error.reasons);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
## TypeScript Support
|
|
105
|
-
|
|
106
|
-
The SDK is written in TypeScript and includes full type definitions.
|
|
107
|
-
|
|
108
|
-
```typescript
|
|
109
|
-
import type { ChatParams, ChatResponse, Message } from 'cencori';
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
## Features
|
|
113
|
-
|
|
114
|
-
- Full TypeScript support with type definitions
|
|
115
|
-
- Built-in authentication
|
|
116
|
-
- Automatic retry logic with exponential backoff
|
|
117
|
-
- Custom error classes
|
|
118
|
-
- Content safety filtering
|
|
119
|
-
- Rate limiting protection
|
|
120
|
-
|
|
121
|
-
## Local Development
|
|
122
|
-
|
|
123
|
-
For local development or testing:
|
|
124
|
-
|
|
125
|
-
```typescript
|
|
126
|
-
const cencori = new CencoriClient({
|
|
127
|
-
apiKey: 'cen_test_...',
|
|
128
|
-
baseUrl: 'http://localhost:3000'
|
|
129
|
-
});
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
## Support
|
|
133
|
-
|
|
134
|
-
- **Documentation**: [docs.cencori.com](https://docs.cencori.com)
|
|
135
|
-
- **Dashboard**: [cencori.com/dashboard](https://cencori.com/dashboard)
|
|
136
|
-
- **GitHub**: [github.com/bolaabanjo/cencori](https://github.com/bolaabanjo/cencori)
|
|
137
|
-
|
|
138
|
-
## License
|
|
139
|
-
|
|
140
|
-
MIT © FohnAI
|
|
1
|
+
# Cencori
|
|
2
|
+
|
|
3
|
+
Official SDK for the Cencori.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install cencori
|
|
9
|
+
# or
|
|
10
|
+
yarn add cencori
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { CencoriClient } from 'cencori';
|
|
17
|
+
|
|
18
|
+
const cencori = new CencoriClient({
|
|
19
|
+
apiKey: process.env.CENCORI_API_KEY!
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const response = await cencori.ai.chat({
|
|
23
|
+
messages: [
|
|
24
|
+
{ role: 'user', content: 'Hello, AI!' }
|
|
25
|
+
]
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
console.log(response.content);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Authentication
|
|
32
|
+
|
|
33
|
+
Get your API key from the [Cencori Dashboard](https://cencori.com/dashboard):
|
|
34
|
+
|
|
35
|
+
1. Create a project
|
|
36
|
+
2. Navigate to API Keys
|
|
37
|
+
3. Generate a new key
|
|
38
|
+
4. Copy and store it securely
|
|
39
|
+
|
|
40
|
+
## API Reference
|
|
41
|
+
|
|
42
|
+
### CencoriClient
|
|
43
|
+
|
|
44
|
+
Initialize the SDK client.
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
const cencori = new CencoriClient({
|
|
48
|
+
apiKey: 'your_api_key_here',
|
|
49
|
+
baseUrl: 'https://cencori.com' // Optional, defaults to production
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### AI Module
|
|
54
|
+
|
|
55
|
+
#### `ai.chat(params)`
|
|
56
|
+
|
|
57
|
+
Send a chat message to the AI.
|
|
58
|
+
|
|
59
|
+
**Parameters:**
|
|
60
|
+
- `messages`: Array of message objects with `role` ('user' | 'assistant') and `content`
|
|
61
|
+
- `model`: Optional AI model (defaults to 'gemini-1.5-flash')
|
|
62
|
+
- `temperature`: Optional temperature (0-1)
|
|
63
|
+
- `maxOutputTokens`: Optional max tokens for response
|
|
64
|
+
|
|
65
|
+
**Example:**
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
const response = await cencori.ai.chat({
|
|
69
|
+
messages: [
|
|
70
|
+
{ role: 'user', content: 'Explain quantum computing' }
|
|
71
|
+
],
|
|
72
|
+
temperature: 0.7
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
console.log(response.content);
|
|
76
|
+
console.log(response.usage); // Token usage stats
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Error Handling
|
|
80
|
+
|
|
81
|
+
The SDK includes custom error classes for common scenarios:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import {
|
|
85
|
+
CencoriClient,
|
|
86
|
+
AuthenticationError,
|
|
87
|
+
RateLimitError,
|
|
88
|
+
SafetyError
|
|
89
|
+
} from 'cencori';
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const response = await cencori.ai.chat({ messages: [...] });
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (error instanceof AuthenticationError) {
|
|
95
|
+
console.error('Invalid API key');
|
|
96
|
+
} else if (error instanceof RateLimitError) {
|
|
97
|
+
console.error('Too many requests, please slow down');
|
|
98
|
+
} else if (error instanceof SafetyError) {
|
|
99
|
+
console.error('Content blocked:', error.reasons);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## TypeScript Support
|
|
105
|
+
|
|
106
|
+
The SDK is written in TypeScript and includes full type definitions.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import type { ChatParams, ChatResponse, Message } from 'cencori';
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Features
|
|
113
|
+
|
|
114
|
+
- Full TypeScript support with type definitions
|
|
115
|
+
- Built-in authentication
|
|
116
|
+
- Automatic retry logic with exponential backoff
|
|
117
|
+
- Custom error classes
|
|
118
|
+
- Content safety filtering
|
|
119
|
+
- Rate limiting protection
|
|
120
|
+
|
|
121
|
+
## Local Development
|
|
122
|
+
|
|
123
|
+
For local development or testing:
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
const cencori = new CencoriClient({
|
|
127
|
+
apiKey: 'cen_test_...',
|
|
128
|
+
baseUrl: 'http://localhost:3000'
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Support
|
|
133
|
+
|
|
134
|
+
- **Documentation**: [docs.cencori.com](https://docs.cencori.com)
|
|
135
|
+
- **Dashboard**: [cencori.com/dashboard](https://cencori.com/dashboard)
|
|
136
|
+
- **GitHub**: [github.com/bolaabanjo/cencori](https://github.com/bolaabanjo/cencori)
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
MIT © FohnAI
|
package/dist/index.d.mts
CHANGED
|
@@ -8,30 +8,46 @@ interface RequestOptions {
|
|
|
8
8
|
headers?: Record<string, string>;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
interface Message {
|
|
12
|
+
role: 'system' | 'user' | 'assistant';
|
|
13
|
+
content: string;
|
|
14
|
+
}
|
|
11
15
|
interface ChatParams {
|
|
12
16
|
messages: Message[];
|
|
13
17
|
model?: string;
|
|
14
18
|
temperature?: number;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
role: 'user' | 'assistant';
|
|
19
|
-
content: string;
|
|
19
|
+
maxTokens?: number;
|
|
20
|
+
stream?: boolean;
|
|
21
|
+
userId?: string;
|
|
20
22
|
}
|
|
21
23
|
interface ChatResponse {
|
|
22
|
-
role: string;
|
|
23
24
|
content: string;
|
|
25
|
+
model: string;
|
|
26
|
+
provider: string;
|
|
24
27
|
usage: {
|
|
25
28
|
prompt_tokens: number;
|
|
26
29
|
completion_tokens: number;
|
|
27
30
|
total_tokens: number;
|
|
28
31
|
};
|
|
32
|
+
cost_usd: number;
|
|
33
|
+
finish_reason: 'stop' | 'length' | 'content_filter' | 'error';
|
|
34
|
+
}
|
|
35
|
+
interface StreamChunk {
|
|
36
|
+
delta: string;
|
|
37
|
+
finish_reason?: 'stop' | 'length' | 'content_filter' | 'error';
|
|
29
38
|
}
|
|
30
|
-
|
|
31
39
|
declare class AIModule {
|
|
32
40
|
private client;
|
|
33
41
|
constructor(client: CencoriClient);
|
|
42
|
+
/**
|
|
43
|
+
* Send a chat completion request (non-streaming)
|
|
44
|
+
*/
|
|
34
45
|
chat(params: ChatParams): Promise<ChatResponse>;
|
|
46
|
+
/**
|
|
47
|
+
* Send a chat completion request with streaming
|
|
48
|
+
* Returns an async generator that yields chunks as they arrive
|
|
49
|
+
*/
|
|
50
|
+
chatStream(params: ChatParams): AsyncGenerator<StreamChunk, void, unknown>;
|
|
35
51
|
}
|
|
36
52
|
|
|
37
53
|
declare class CencoriClient {
|
|
@@ -39,6 +55,8 @@ declare class CencoriClient {
|
|
|
39
55
|
private baseUrl;
|
|
40
56
|
ai: AIModule;
|
|
41
57
|
constructor(config: CencoriConfig);
|
|
58
|
+
getBaseUrl(): string;
|
|
59
|
+
getApiKey(): string;
|
|
42
60
|
request<T>(endpoint: string, options: RequestOptions): Promise<T>;
|
|
43
61
|
}
|
|
44
62
|
|
|
@@ -58,4 +76,4 @@ declare class SafetyError extends CencoriError {
|
|
|
58
76
|
constructor(message?: string, reasons?: string[] | undefined);
|
|
59
77
|
}
|
|
60
78
|
|
|
61
|
-
export { AuthenticationError, CencoriClient, type CencoriConfig, CencoriError, type ChatParams, type ChatResponse, type Message, RateLimitError, type RequestOptions, SafetyError };
|
|
79
|
+
export { AIModule, AuthenticationError, CencoriClient as Cencori, CencoriClient, type CencoriConfig, CencoriError, type ChatParams, type ChatResponse, type Message, RateLimitError, type RequestOptions, SafetyError, type StreamChunk, CencoriClient as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,30 +8,46 @@ interface RequestOptions {
|
|
|
8
8
|
headers?: Record<string, string>;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
interface Message {
|
|
12
|
+
role: 'system' | 'user' | 'assistant';
|
|
13
|
+
content: string;
|
|
14
|
+
}
|
|
11
15
|
interface ChatParams {
|
|
12
16
|
messages: Message[];
|
|
13
17
|
model?: string;
|
|
14
18
|
temperature?: number;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
role: 'user' | 'assistant';
|
|
19
|
-
content: string;
|
|
19
|
+
maxTokens?: number;
|
|
20
|
+
stream?: boolean;
|
|
21
|
+
userId?: string;
|
|
20
22
|
}
|
|
21
23
|
interface ChatResponse {
|
|
22
|
-
role: string;
|
|
23
24
|
content: string;
|
|
25
|
+
model: string;
|
|
26
|
+
provider: string;
|
|
24
27
|
usage: {
|
|
25
28
|
prompt_tokens: number;
|
|
26
29
|
completion_tokens: number;
|
|
27
30
|
total_tokens: number;
|
|
28
31
|
};
|
|
32
|
+
cost_usd: number;
|
|
33
|
+
finish_reason: 'stop' | 'length' | 'content_filter' | 'error';
|
|
34
|
+
}
|
|
35
|
+
interface StreamChunk {
|
|
36
|
+
delta: string;
|
|
37
|
+
finish_reason?: 'stop' | 'length' | 'content_filter' | 'error';
|
|
29
38
|
}
|
|
30
|
-
|
|
31
39
|
declare class AIModule {
|
|
32
40
|
private client;
|
|
33
41
|
constructor(client: CencoriClient);
|
|
42
|
+
/**
|
|
43
|
+
* Send a chat completion request (non-streaming)
|
|
44
|
+
*/
|
|
34
45
|
chat(params: ChatParams): Promise<ChatResponse>;
|
|
46
|
+
/**
|
|
47
|
+
* Send a chat completion request with streaming
|
|
48
|
+
* Returns an async generator that yields chunks as they arrive
|
|
49
|
+
*/
|
|
50
|
+
chatStream(params: ChatParams): AsyncGenerator<StreamChunk, void, unknown>;
|
|
35
51
|
}
|
|
36
52
|
|
|
37
53
|
declare class CencoriClient {
|
|
@@ -39,6 +55,8 @@ declare class CencoriClient {
|
|
|
39
55
|
private baseUrl;
|
|
40
56
|
ai: AIModule;
|
|
41
57
|
constructor(config: CencoriConfig);
|
|
58
|
+
getBaseUrl(): string;
|
|
59
|
+
getApiKey(): string;
|
|
42
60
|
request<T>(endpoint: string, options: RequestOptions): Promise<T>;
|
|
43
61
|
}
|
|
44
62
|
|
|
@@ -58,4 +76,4 @@ declare class SafetyError extends CencoriError {
|
|
|
58
76
|
constructor(message?: string, reasons?: string[] | undefined);
|
|
59
77
|
}
|
|
60
78
|
|
|
61
|
-
export { AuthenticationError, CencoriClient, type CencoriConfig, CencoriError, type ChatParams, type ChatResponse, type Message, RateLimitError, type RequestOptions, SafetyError };
|
|
79
|
+
export { AIModule, AuthenticationError, CencoriClient as Cencori, CencoriClient, type CencoriConfig, CencoriError, type ChatParams, type ChatResponse, type Message, RateLimitError, type RequestOptions, SafetyError, type StreamChunk, CencoriClient as default };
|
package/dist/index.js
CHANGED
|
@@ -20,11 +20,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
AIModule: () => AIModule,
|
|
23
24
|
AuthenticationError: () => AuthenticationError,
|
|
25
|
+
Cencori: () => CencoriClient,
|
|
24
26
|
CencoriClient: () => CencoriClient,
|
|
25
27
|
CencoriError: () => CencoriError,
|
|
26
28
|
RateLimitError: () => RateLimitError,
|
|
27
|
-
SafetyError: () => SafetyError
|
|
29
|
+
SafetyError: () => SafetyError,
|
|
30
|
+
default: () => CencoriClient
|
|
28
31
|
});
|
|
29
32
|
module.exports = __toCommonJS(index_exports);
|
|
30
33
|
|
|
@@ -33,11 +36,66 @@ var AIModule = class {
|
|
|
33
36
|
constructor(client) {
|
|
34
37
|
this.client = client;
|
|
35
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Send a chat completion request (non-streaming)
|
|
41
|
+
*/
|
|
36
42
|
async chat(params) {
|
|
37
|
-
|
|
43
|
+
const response = await this.client.request("/api/ai/chat", {
|
|
38
44
|
method: "POST",
|
|
39
|
-
body: JSON.stringify(params)
|
|
45
|
+
body: JSON.stringify({ ...params, stream: false })
|
|
40
46
|
});
|
|
47
|
+
return response;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Send a chat completion request with streaming
|
|
51
|
+
* Returns an async generator that yields chunks as they arrive
|
|
52
|
+
*/
|
|
53
|
+
async *chatStream(params) {
|
|
54
|
+
const url = `${this.client.getBaseUrl()}/api/ai/chat`;
|
|
55
|
+
const headers = {
|
|
56
|
+
"Content-Type": "application/json",
|
|
57
|
+
"CENCORI_API_KEY": this.client.getApiKey()
|
|
58
|
+
};
|
|
59
|
+
const response = await fetch(url, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers,
|
|
62
|
+
body: JSON.stringify({ ...params, stream: true })
|
|
63
|
+
});
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
const error = await response.json();
|
|
66
|
+
throw new Error(error.error || "Stream request failed");
|
|
67
|
+
}
|
|
68
|
+
if (!response.body) {
|
|
69
|
+
throw new Error("Response body is null");
|
|
70
|
+
}
|
|
71
|
+
const reader = response.body.getReader();
|
|
72
|
+
const decoder = new TextDecoder();
|
|
73
|
+
let buffer = "";
|
|
74
|
+
try {
|
|
75
|
+
while (true) {
|
|
76
|
+
const { done, value } = await reader.read();
|
|
77
|
+
if (done) break;
|
|
78
|
+
buffer += decoder.decode(value, { stream: true });
|
|
79
|
+
const lines = buffer.split("\n");
|
|
80
|
+
buffer = lines.pop() || "";
|
|
81
|
+
for (const line of lines) {
|
|
82
|
+
if (line.trim() === "") continue;
|
|
83
|
+
if (!line.startsWith("data: ")) continue;
|
|
84
|
+
const data = line.slice(6);
|
|
85
|
+
if (data === "[DONE]") {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
const chunk = JSON.parse(data);
|
|
90
|
+
yield chunk;
|
|
91
|
+
} catch (e) {
|
|
92
|
+
console.error("Failed to parse SSE data:", e);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} finally {
|
|
97
|
+
reader.releaseLock();
|
|
98
|
+
}
|
|
41
99
|
}
|
|
42
100
|
};
|
|
43
101
|
|
|
@@ -109,9 +167,16 @@ var CencoriClient = class {
|
|
|
109
167
|
throw new Error("API key is required");
|
|
110
168
|
}
|
|
111
169
|
this.apiKey = config.apiKey;
|
|
112
|
-
this.baseUrl = config.baseUrl || "https://cencori.
|
|
170
|
+
this.baseUrl = config.baseUrl || "https://cencori.com";
|
|
113
171
|
this.ai = new AIModule(this);
|
|
114
172
|
}
|
|
173
|
+
// Public getters for internal use by modules
|
|
174
|
+
getBaseUrl() {
|
|
175
|
+
return this.baseUrl;
|
|
176
|
+
}
|
|
177
|
+
getApiKey() {
|
|
178
|
+
return this.apiKey;
|
|
179
|
+
}
|
|
115
180
|
async request(endpoint, options) {
|
|
116
181
|
const url = `${this.baseUrl}${endpoint}`;
|
|
117
182
|
const headers = {
|
|
@@ -155,7 +220,9 @@ var CencoriClient = class {
|
|
|
155
220
|
};
|
|
156
221
|
// Annotate the CommonJS export names for ESM import in node:
|
|
157
222
|
0 && (module.exports = {
|
|
223
|
+
AIModule,
|
|
158
224
|
AuthenticationError,
|
|
225
|
+
Cencori,
|
|
159
226
|
CencoriClient,
|
|
160
227
|
CencoriError,
|
|
161
228
|
RateLimitError,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/modules/ai.ts","../src/utils.ts","../src/errors.ts","../src/client.ts"],"sourcesContent":["export { CencoriClient } from './client';\r\nexport * from './types';\r\nexport * from './errors';\r\n","import type { ChatParams, ChatResponse } from '../types/ai';\r\nimport type { CencoriClient } from '../client';\r\n\r\nexport class AIModule {\r\n constructor(private client: CencoriClient) { }\r\n\r\n async chat(params: ChatParams): Promise<ChatResponse> {\r\n return this.client.request<ChatResponse>('/api/ai/chat', {\r\n method: 'POST',\r\n body: JSON.stringify(params)\r\n });\r\n }\r\n}\r\n","function sleep(ms: number): Promise<void> {\r\n return new Promise(resolve => setTimeout(resolve, ms));\r\n}\r\n\r\nexport async function fetchWithRetry(\r\n url: string,\r\n options: RequestInit,\r\n maxRetries = 3\r\n): Promise<Response> {\r\n let lastError: Error | null = null;\r\n\r\n for (let attempt = 0; attempt < maxRetries; attempt++) {\r\n try {\r\n const response = await fetch(url, options);\r\n\r\n // Return immediately if request succeeded or if it's a client error (4xx)\r\n if (response.ok || (response.status >= 400 && response.status < 500)) {\r\n return response;\r\n }\r\n\r\n // Retry on 5xx errors\r\n lastError = new Error(`HTTP ${response.status}: ${response.statusText}`);\r\n\r\n // Don't retry on last attempt\r\n if (attempt === maxRetries - 1) {\r\n return response;\r\n }\r\n\r\n // Exponential backoff: 1s, 2s, 4s\r\n await sleep(Math.pow(2, attempt) * 1000);\r\n } catch (error) {\r\n lastError = error instanceof Error ? error : new Error(String(error));\r\n\r\n // Don't retry on last attempt\r\n if (attempt === maxRetries - 1) {\r\n throw lastError;\r\n }\r\n\r\n // Exponential backoff\r\n await sleep(Math.pow(2, attempt) * 1000);\r\n }\r\n }\r\n\r\n throw lastError || new Error('Max retries reached');\r\n}\r\n","export class CencoriError extends Error {\r\n constructor(\r\n message: string,\r\n public statusCode?: number,\r\n public code?: string\r\n ) {\r\n super(message);\r\n this.name = 'CencoriError';\r\n Object.setPrototypeOf(this, CencoriError.prototype);\r\n }\r\n}\r\n\r\nexport class AuthenticationError extends CencoriError {\r\n constructor(message = 'Invalid API key') {\r\n super(message, 401, 'INVALID_API_KEY');\r\n this.name = 'AuthenticationError';\r\n Object.setPrototypeOf(this, AuthenticationError.prototype);\r\n }\r\n}\r\n\r\nexport class RateLimitError extends CencoriError {\r\n constructor(message = 'Rate limit exceeded') {\r\n super(message, 429, 'RATE_LIMIT_EXCEEDED');\r\n this.name = 'RateLimitError';\r\n Object.setPrototypeOf(this, RateLimitError.prototype);\r\n }\r\n}\r\n\r\nexport class SafetyError extends CencoriError {\r\n constructor(message = 'Content safety violation', public reasons?: string[]) {\r\n super(message, 400, 'SAFETY_VIOLATION');\r\n this.name = 'SafetyError';\r\n Object.setPrototypeOf(this, SafetyError.prototype);\r\n }\r\n}\r\n","import type { CencoriConfig, RequestOptions } from './types/common';\r\nimport { AIModule } from './modules/ai';\r\nimport { fetchWithRetry } from './utils';\r\nimport {\r\n CencoriError,\r\n AuthenticationError,\r\n RateLimitError,\r\n SafetyError\r\n} from './errors';\r\n\r\ninterface ErrorResponse {\r\n error?: string;\r\n reasons?: string[];\r\n}\r\n\r\nexport class CencoriClient {\r\n private apiKey: string;\r\n private baseUrl: string;\r\n\r\n public ai: AIModule;\r\n\r\n constructor(config: CencoriConfig) {\r\n if (!config.apiKey) {\r\n throw new Error('API key is required');\r\n }\r\n\r\n this.apiKey = config.apiKey;\r\n this.baseUrl = config.baseUrl || 'https://cencori.vercel.app';\r\n\r\n this.ai = new AIModule(this);\r\n }\r\n\r\n async request<T>(endpoint: string, options: RequestOptions): Promise<T> {\r\n const url = `${this.baseUrl}${endpoint}`;\r\n\r\n const headers: Record<string, string> = {\r\n 'Content-Type': 'application/json',\r\n 'CENCORI_API_KEY': this.apiKey,\r\n ...options.headers\r\n };\r\n\r\n try {\r\n const response = await fetchWithRetry(url, {\r\n method: options.method,\r\n headers,\r\n body: options.body\r\n });\r\n\r\n const data: unknown = await response.json();\r\n\r\n // Handle API errors\r\n if (!response.ok) {\r\n const errorData = data as ErrorResponse;\r\n\r\n if (response.status === 401) {\r\n throw new AuthenticationError(errorData.error || 'Invalid API key');\r\n }\r\n if (response.status === 429) {\r\n throw new RateLimitError(errorData.error || 'Rate limit exceeded');\r\n }\r\n if (response.status === 400 && errorData.reasons) {\r\n throw new SafetyError(errorData.error, errorData.reasons);\r\n }\r\n throw new CencoriError(\r\n errorData.error || 'Request failed',\r\n response.status\r\n );\r\n }\r\n\r\n return data as T;\r\n } catch (error) {\r\n // Re-throw custom errors\r\n if (error instanceof CencoriError) {\r\n throw error;\r\n }\r\n\r\n // Wrap unknown errors\r\n throw new CencoriError(\r\n error instanceof Error ? error.message : 'Unknown error occurred'\r\n );\r\n }\r\n }\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,WAAN,MAAe;AAAA,EAClB,YAAoB,QAAuB;AAAvB;AAAA,EAAyB;AAAA,EAE7C,MAAM,KAAK,QAA2C;AAClD,WAAO,KAAK,OAAO,QAAsB,gBAAgB;AAAA,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AAY,UAAU,OAAO,UAAU,OAAO;AAAA,QAC5D;AACA,cAAM,IAAI;AAAA,UACN,UAAU,SAAS;AAAA,UACnB,SAAS;AAAA,QACb;AAAA,MACJ;AAEA,aAAO;AAAA,IACX,SAAS,OAAO;AAEZ,UAAI,iBAAiB,cAAc;AAC/B,cAAM;AAAA,MACV;AAGA,YAAM,IAAI;AAAA,QACN,iBAAiB,QAAQ,MAAM,UAAU;AAAA,MAC7C;AAAA,IACJ;AAAA,EACJ;AACJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/modules/ai.ts","../src/utils.ts","../src/errors.ts","../src/client.ts"],"sourcesContent":["export { CencoriClient } from './client';\nexport { CencoriClient as Cencori } from './client'; // Alias for convenience\nexport * from './types';\nexport * from './errors';\n\n// Default export for: import Cencori from 'cencori'\nexport { CencoriClient as default } from './client';\n","import type { CencoriClient, ErrorResponse } from '../client';\n\nexport interface Message {\n role: 'system' | 'user' | 'assistant';\n content: string;\n}\n\nexport interface ChatParams {\n messages: Message[];\n model?: string;\n temperature?: number;\n maxTokens?: number;\n stream?: boolean;\n userId?: string;\n}\n\nexport interface ChatResponse {\n content: string;\n model: string;\n provider: string;\n usage: {\n prompt_tokens: number;\n completion_tokens: number;\n total_tokens: number;\n };\n cost_usd: number;\n finish_reason: 'stop' | 'length' | 'content_filter' | 'error';\n}\n\nexport interface StreamChunk {\n delta: string;\n finish_reason?: 'stop' | 'length' | 'content_filter' | 'error';\n}\n\nexport class AIModule {\n constructor(private client: CencoriClient) { }\n\n /**\n * Send a chat completion request (non-streaming)\n */\n async chat(params: ChatParams): Promise<ChatResponse> {\n const response = await this.client.request<ChatResponse>('/api/ai/chat', {\n method: 'POST',\n body: JSON.stringify({ ...params, stream: false }),\n });\n\n return response;\n }\n\n /**\n * Send a chat completion request with streaming\n * Returns an async generator that yields chunks as they arrive\n */\n async *chatStream(params: ChatParams): AsyncGenerator<StreamChunk, void, unknown> {\n const url = `${this.client.getBaseUrl()}/api/ai/chat`;\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n 'CENCORI_API_KEY': this.client.getApiKey(),\n };\n\n const response = await fetch(url, {\n method: 'POST',\n headers,\n body: JSON.stringify({ ...params, stream: true }),\n });\n\n if (!response.ok) {\n const error = await response.json() as ErrorResponse;\n throw new Error(error.error || 'Stream request failed');\n }\n\n if (!response.body) {\n throw new Error('Response body is null');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n const lines = buffer.split('\\n');\n\n // Keep the last incomplete line in the buffer\n buffer = lines.pop() || '';\n\n for (const line of lines) {\n if (line.trim() === '') continue;\n if (!line.startsWith('data: ')) continue;\n\n const data = line.slice(6); // Remove 'data: ' prefix\n\n if (data === '[DONE]') {\n return;\n }\n\n try {\n const chunk = JSON.parse(data) as StreamChunk;\n yield chunk;\n } catch (e) {\n console.error('Failed to parse SSE data:', e);\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n }\n}\n","function sleep(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\nexport async function fetchWithRetry(\n url: string,\n options: RequestInit,\n maxRetries = 3\n): Promise<Response> {\n let lastError: Error | null = null;\n\n for (let attempt = 0; attempt < maxRetries; attempt++) {\n try {\n const response = await fetch(url, options);\n\n // Return immediately if request succeeded or if it's a client error (4xx)\n if (response.ok || (response.status >= 400 && response.status < 500)) {\n return response;\n }\n\n // Retry on 5xx errors\n lastError = new Error(`HTTP ${response.status}: ${response.statusText}`);\n\n // Don't retry on last attempt\n if (attempt === maxRetries - 1) {\n return response;\n }\n\n // Exponential backoff: 1s, 2s, 4s\n await sleep(Math.pow(2, attempt) * 1000);\n } catch (error) {\n lastError = error instanceof Error ? error : new Error(String(error));\n\n // Don't retry on last attempt\n if (attempt === maxRetries - 1) {\n throw lastError;\n }\n\n // Exponential backoff\n await sleep(Math.pow(2, attempt) * 1000);\n }\n }\n\n throw lastError || new Error('Max retries reached');\n}\n","export class CencoriError extends Error {\n constructor(\n message: string,\n public statusCode?: number,\n public code?: string\n ) {\n super(message);\n this.name = 'CencoriError';\n Object.setPrototypeOf(this, CencoriError.prototype);\n }\n}\n\nexport class AuthenticationError extends CencoriError {\n constructor(message = 'Invalid API key') {\n super(message, 401, 'INVALID_API_KEY');\n this.name = 'AuthenticationError';\n Object.setPrototypeOf(this, AuthenticationError.prototype);\n }\n}\n\nexport class RateLimitError extends CencoriError {\n constructor(message = 'Rate limit exceeded') {\n super(message, 429, 'RATE_LIMIT_EXCEEDED');\n this.name = 'RateLimitError';\n Object.setPrototypeOf(this, RateLimitError.prototype);\n }\n}\n\nexport class SafetyError extends CencoriError {\n constructor(message = 'Content safety violation', public reasons?: string[]) {\n super(message, 400, 'SAFETY_VIOLATION');\n this.name = 'SafetyError';\n Object.setPrototypeOf(this, SafetyError.prototype);\n }\n}\n","import type { CencoriConfig, RequestOptions } from './types/common';\nimport { AIModule } from './modules/ai';\nimport { fetchWithRetry } from './utils';\nimport {\n CencoriError,\n AuthenticationError,\n RateLimitError,\n SafetyError\n} from './errors';\n\nexport interface ErrorResponse {\n error?: string;\n reasons?: string[];\n}\n\nexport class CencoriClient {\n private apiKey: string;\n private baseUrl: string;\n\n public ai: AIModule;\n\n constructor(config: CencoriConfig) {\n if (!config.apiKey) {\n throw new Error('API key is required');\n }\n\n this.apiKey = config.apiKey;\n this.baseUrl = config.baseUrl || 'https://cencori.com';\n\n this.ai = new AIModule(this);\n }\n\n // Public getters for internal use by modules\n getBaseUrl(): string {\n return this.baseUrl;\n }\n\n getApiKey(): string {\n return this.apiKey;\n }\n\n async request<T>(endpoint: string, options: RequestOptions): Promise<T> {\n const url = `${this.baseUrl}${endpoint}`;\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n 'CENCORI_API_KEY': this.apiKey,\n ...options.headers\n };\n\n try {\n const response = await fetchWithRetry(url, {\n method: options.method,\n headers,\n body: options.body\n });\n\n const data: unknown = await response.json();\n\n // Handle API errors\n if (!response.ok) {\n const errorData = data as ErrorResponse;\n\n if (response.status === 401) {\n throw new AuthenticationError(errorData.error || 'Invalid API key');\n }\n if (response.status === 429) {\n throw new RateLimitError(errorData.error || 'Rate limit exceeded');\n }\n if (response.status === 400 && errorData.reasons) {\n throw new SafetyError(errorData.error, errorData.reasons);\n }\n throw new CencoriError(\n errorData.error || 'Request failed',\n response.status\n );\n }\n\n return data as T;\n } catch (error) {\n // Re-throw custom errors\n if (error instanceof CencoriError) {\n throw error;\n }\n\n // Wrap unknown errors\n throw new CencoriError(\n error instanceof Error ? error.message : 'Unknown error occurred'\n );\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkCO,IAAM,WAAN,MAAe;AAAA,EAClB,YAAoB,QAAuB;AAAvB;AAAA,EAAyB;AAAA;AAAA;AAAA;AAAA,EAK7C,MAAM,KAAK,QAA2C;AAClD,UAAM,WAAW,MAAM,KAAK,OAAO,QAAsB,gBAAgB;AAAA,MACrE,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU,EAAE,GAAG,QAAQ,QAAQ,MAAM,CAAC;AAAA,IACrD,CAAC;AAED,WAAO;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,WAAW,QAAgE;AAC9E,UAAM,MAAM,GAAG,KAAK,OAAO,WAAW,CAAC;AAEvC,UAAM,UAAkC;AAAA,MACpC,gBAAgB;AAAA,MAChB,mBAAmB,KAAK,OAAO,UAAU;AAAA,IAC7C;AAEA,UAAM,WAAW,MAAM,MAAM,KAAK;AAAA,MAC9B,QAAQ;AAAA,MACR;AAAA,MACA,MAAM,KAAK,UAAU,EAAE,GAAG,QAAQ,QAAQ,KAAK,CAAC;AAAA,IACpD,CAAC;AAED,QAAI,CAAC,SAAS,IAAI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
package/dist/index.mjs
CHANGED
|
@@ -3,11 +3,66 @@ var AIModule = class {
|
|
|
3
3
|
constructor(client) {
|
|
4
4
|
this.client = client;
|
|
5
5
|
}
|
|
6
|
+
/**
|
|
7
|
+
* Send a chat completion request (non-streaming)
|
|
8
|
+
*/
|
|
6
9
|
async chat(params) {
|
|
7
|
-
|
|
10
|
+
const response = await this.client.request("/api/ai/chat", {
|
|
8
11
|
method: "POST",
|
|
9
|
-
body: JSON.stringify(params)
|
|
12
|
+
body: JSON.stringify({ ...params, stream: false })
|
|
10
13
|
});
|
|
14
|
+
return response;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Send a chat completion request with streaming
|
|
18
|
+
* Returns an async generator that yields chunks as they arrive
|
|
19
|
+
*/
|
|
20
|
+
async *chatStream(params) {
|
|
21
|
+
const url = `${this.client.getBaseUrl()}/api/ai/chat`;
|
|
22
|
+
const headers = {
|
|
23
|
+
"Content-Type": "application/json",
|
|
24
|
+
"CENCORI_API_KEY": this.client.getApiKey()
|
|
25
|
+
};
|
|
26
|
+
const response = await fetch(url, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers,
|
|
29
|
+
body: JSON.stringify({ ...params, stream: true })
|
|
30
|
+
});
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
const error = await response.json();
|
|
33
|
+
throw new Error(error.error || "Stream request failed");
|
|
34
|
+
}
|
|
35
|
+
if (!response.body) {
|
|
36
|
+
throw new Error("Response body is null");
|
|
37
|
+
}
|
|
38
|
+
const reader = response.body.getReader();
|
|
39
|
+
const decoder = new TextDecoder();
|
|
40
|
+
let buffer = "";
|
|
41
|
+
try {
|
|
42
|
+
while (true) {
|
|
43
|
+
const { done, value } = await reader.read();
|
|
44
|
+
if (done) break;
|
|
45
|
+
buffer += decoder.decode(value, { stream: true });
|
|
46
|
+
const lines = buffer.split("\n");
|
|
47
|
+
buffer = lines.pop() || "";
|
|
48
|
+
for (const line of lines) {
|
|
49
|
+
if (line.trim() === "") continue;
|
|
50
|
+
if (!line.startsWith("data: ")) continue;
|
|
51
|
+
const data = line.slice(6);
|
|
52
|
+
if (data === "[DONE]") {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const chunk = JSON.parse(data);
|
|
57
|
+
yield chunk;
|
|
58
|
+
} catch (e) {
|
|
59
|
+
console.error("Failed to parse SSE data:", e);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} finally {
|
|
64
|
+
reader.releaseLock();
|
|
65
|
+
}
|
|
11
66
|
}
|
|
12
67
|
};
|
|
13
68
|
|
|
@@ -79,9 +134,16 @@ var CencoriClient = class {
|
|
|
79
134
|
throw new Error("API key is required");
|
|
80
135
|
}
|
|
81
136
|
this.apiKey = config.apiKey;
|
|
82
|
-
this.baseUrl = config.baseUrl || "https://cencori.
|
|
137
|
+
this.baseUrl = config.baseUrl || "https://cencori.com";
|
|
83
138
|
this.ai = new AIModule(this);
|
|
84
139
|
}
|
|
140
|
+
// Public getters for internal use by modules
|
|
141
|
+
getBaseUrl() {
|
|
142
|
+
return this.baseUrl;
|
|
143
|
+
}
|
|
144
|
+
getApiKey() {
|
|
145
|
+
return this.apiKey;
|
|
146
|
+
}
|
|
85
147
|
async request(endpoint, options) {
|
|
86
148
|
const url = `${this.baseUrl}${endpoint}`;
|
|
87
149
|
const headers = {
|
|
@@ -124,10 +186,13 @@ var CencoriClient = class {
|
|
|
124
186
|
}
|
|
125
187
|
};
|
|
126
188
|
export {
|
|
189
|
+
AIModule,
|
|
127
190
|
AuthenticationError,
|
|
191
|
+
CencoriClient as Cencori,
|
|
128
192
|
CencoriClient,
|
|
129
193
|
CencoriError,
|
|
130
194
|
RateLimitError,
|
|
131
|
-
SafetyError
|
|
195
|
+
SafetyError,
|
|
196
|
+
CencoriClient as default
|
|
132
197
|
};
|
|
133
198
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/modules/ai.ts","../src/utils.ts","../src/errors.ts","../src/client.ts"],"sourcesContent":["import type { ChatParams, ChatResponse } from '../types/ai';\r\nimport type { CencoriClient } from '../client';\r\n\r\nexport class AIModule {\r\n constructor(private client: CencoriClient) { }\r\n\r\n async chat(params: ChatParams): Promise<ChatResponse> {\r\n return this.client.request<ChatResponse>('/api/ai/chat', {\r\n method: 'POST',\r\n body: JSON.stringify(params)\r\n });\r\n }\r\n}\r\n","function sleep(ms: number): Promise<void> {\r\n return new Promise(resolve => setTimeout(resolve, ms));\r\n}\r\n\r\nexport async function fetchWithRetry(\r\n url: string,\r\n options: RequestInit,\r\n maxRetries = 3\r\n): Promise<Response> {\r\n let lastError: Error | null = null;\r\n\r\n for (let attempt = 0; attempt < maxRetries; attempt++) {\r\n try {\r\n const response = await fetch(url, options);\r\n\r\n // Return immediately if request succeeded or if it's a client error (4xx)\r\n if (response.ok || (response.status >= 400 && response.status < 500)) {\r\n return response;\r\n }\r\n\r\n // Retry on 5xx errors\r\n lastError = new Error(`HTTP ${response.status}: ${response.statusText}`);\r\n\r\n // Don't retry on last attempt\r\n if (attempt === maxRetries - 1) {\r\n return response;\r\n }\r\n\r\n // Exponential backoff: 1s, 2s, 4s\r\n await sleep(Math.pow(2, attempt) * 1000);\r\n } catch (error) {\r\n lastError = error instanceof Error ? error : new Error(String(error));\r\n\r\n // Don't retry on last attempt\r\n if (attempt === maxRetries - 1) {\r\n throw lastError;\r\n }\r\n\r\n // Exponential backoff\r\n await sleep(Math.pow(2, attempt) * 1000);\r\n }\r\n }\r\n\r\n throw lastError || new Error('Max retries reached');\r\n}\r\n","export class CencoriError extends Error {\r\n constructor(\r\n message: string,\r\n public statusCode?: number,\r\n public code?: string\r\n ) {\r\n super(message);\r\n this.name = 'CencoriError';\r\n Object.setPrototypeOf(this, CencoriError.prototype);\r\n }\r\n}\r\n\r\nexport class AuthenticationError extends CencoriError {\r\n constructor(message = 'Invalid API key') {\r\n super(message, 401, 'INVALID_API_KEY');\r\n this.name = 'AuthenticationError';\r\n Object.setPrototypeOf(this, AuthenticationError.prototype);\r\n }\r\n}\r\n\r\nexport class RateLimitError extends CencoriError {\r\n constructor(message = 'Rate limit exceeded') {\r\n super(message, 429, 'RATE_LIMIT_EXCEEDED');\r\n this.name = 'RateLimitError';\r\n Object.setPrototypeOf(this, RateLimitError.prototype);\r\n }\r\n}\r\n\r\nexport class SafetyError extends CencoriError {\r\n constructor(message = 'Content safety violation', public reasons?: string[]) {\r\n super(message, 400, 'SAFETY_VIOLATION');\r\n this.name = 'SafetyError';\r\n Object.setPrototypeOf(this, SafetyError.prototype);\r\n }\r\n}\r\n","import type { CencoriConfig, RequestOptions } from './types/common';\r\nimport { AIModule } from './modules/ai';\r\nimport { fetchWithRetry } from './utils';\r\nimport {\r\n CencoriError,\r\n AuthenticationError,\r\n RateLimitError,\r\n SafetyError\r\n} from './errors';\r\n\r\ninterface ErrorResponse {\r\n error?: string;\r\n reasons?: string[];\r\n}\r\n\r\nexport class CencoriClient {\r\n private apiKey: string;\r\n private baseUrl: string;\r\n\r\n public ai: AIModule;\r\n\r\n constructor(config: CencoriConfig) {\r\n if (!config.apiKey) {\r\n throw new Error('API key is required');\r\n }\r\n\r\n this.apiKey = config.apiKey;\r\n this.baseUrl = config.baseUrl || 'https://cencori.vercel.app';\r\n\r\n this.ai = new AIModule(this);\r\n }\r\n\r\n async request<T>(endpoint: string, options: RequestOptions): Promise<T> {\r\n const url = `${this.baseUrl}${endpoint}`;\r\n\r\n const headers: Record<string, string> = {\r\n 'Content-Type': 'application/json',\r\n 'CENCORI_API_KEY': this.apiKey,\r\n ...options.headers\r\n };\r\n\r\n try {\r\n const response = await fetchWithRetry(url, {\r\n method: options.method,\r\n headers,\r\n body: options.body\r\n });\r\n\r\n const data: unknown = await response.json();\r\n\r\n // Handle API errors\r\n if (!response.ok) {\r\n const errorData = data as ErrorResponse;\r\n\r\n if (response.status === 401) {\r\n throw new AuthenticationError(errorData.error || 'Invalid API key');\r\n }\r\n if (response.status === 429) {\r\n throw new RateLimitError(errorData.error || 'Rate limit exceeded');\r\n }\r\n if (response.status === 400 && errorData.reasons) {\r\n throw new SafetyError(errorData.error, errorData.reasons);\r\n }\r\n throw new CencoriError(\r\n errorData.error || 'Request failed',\r\n response.status\r\n );\r\n }\r\n\r\n return data as T;\r\n } catch (error) {\r\n // Re-throw custom errors\r\n if (error instanceof CencoriError) {\r\n throw error;\r\n }\r\n\r\n // Wrap unknown errors\r\n throw new CencoriError(\r\n error instanceof Error ? error.message : 'Unknown error occurred'\r\n );\r\n }\r\n }\r\n}\r\n"],"mappings":";AAGO,IAAM,WAAN,MAAe;AAAA,EAClB,YAAoB,QAAuB;AAAvB;AAAA,EAAyB;AAAA,EAE7C,MAAM,KAAK,QAA2C;AAClD,WAAO,KAAK,OAAO,QAAsB,gBAAgB;AAAA,MACrD,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU,MAAM;AAAA,IAC/B,CAAC;AAAA,EACL;AACJ;;;ACZA,SAAS,MAAM,IAA2B;AACtC,SAAO,IAAI,QAAQ,aAAW,WAAW,SAAS,EAAE,CAAC;AACzD;AAEA,eAAsB,eAClB,KACA,SACA,aAAa,GACI;AACjB,MAAI,YAA0B;AAE9B,WAAS,UAAU,GAAG,UAAU,YAAY,WAAW;AACnD,QAAI;AACA,YAAM,WAAW,MAAM,MAAM,KAAK,OAAO;AAGzC,UAAI,SAAS,MAAO,SAAS,UAAU,OAAO,SAAS,SAAS,KAAM;AAClE,eAAO;AAAA,MACX;AAGA,kBAAY,IAAI,MAAM,QAAQ,SAAS,MAAM,KAAK,SAAS,UAAU,EAAE;AAGvE,UAAI,YAAY,aAAa,GAAG;AAC5B,eAAO;AAAA,MACX;AAGA,YAAM,MAAM,KAAK,IAAI,GAAG,OAAO,IAAI,GAAI;AAAA,IAC3C,SAAS,OAAO;AACZ,kBAAY,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC;AA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UAAM,UAAkC;AAAA,MACpC,gBAAgB;AAAA,MAChB,mBAAmB,KAAK;AAAA,MACxB,GAAG,QAAQ;AAAA,IACf;AAEA,QAAI;AACA,YAAM,WAAW,MAAM,eAAe,KAAK;AAAA,QACvC,QAAQ,QAAQ;AAAA,QAChB;AAAA,QACA,MAAM,QAAQ;AAAA,MAClB,CAAC;AAED,YAAM,OAAgB,MAAM,SAAS,KAAK;AAG1C,UAAI,CAAC,SAAS,IAAI;AACd,cAAM,YAAY;AAElB,YAAI,SAAS,WAAW,KAAK;AACzB,gBAAM,IAAI,oBAAoB,UAAU,SAAS,iBAAiB;AAAA,QACtE;AACA,YAAI,SAAS,WAAW,KAAK;AACzB,gBAAM,IAAI,eAAe,UAAU,SAAS,qBAAqB;AAAA,QACrE;AACA,YAAI,SAAS,WAAW,OAAO,UAAU,SAAS;AAC9C,gBAAM,IAAI,YAAY,UAAU,OAAO,UAAU,OAAO;AAAA,QAC5D;AACA,cAAM,IAAI;AAAA,UACN,UAAU,SAAS;AAAA,UACnB,SAAS;AAAA,QACb;AAAA,MACJ;AAEA,aAAO;AAAA,IACX,SAAS,OAAO;AAEZ,UAAI,iBAAiB,cAAc;AAC/B,cAAM;AAAA,MACV;AAGA,YAAM,IAAI;AAAA,QACN,iBAAiB,QAAQ,MAAM,UAAU;AAAA,MAC7C;AAAA,IACJ;AAAA,EACJ;AACJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/modules/ai.ts","../src/utils.ts","../src/errors.ts","../src/client.ts"],"sourcesContent":["import type { CencoriClient, ErrorResponse } from '../client';\n\nexport interface Message {\n role: 'system' | 'user' | 'assistant';\n content: string;\n}\n\nexport interface ChatParams {\n messages: Message[];\n model?: string;\n temperature?: number;\n maxTokens?: number;\n stream?: boolean;\n userId?: string;\n}\n\nexport interface ChatResponse {\n content: string;\n model: string;\n provider: string;\n usage: {\n prompt_tokens: number;\n completion_tokens: number;\n total_tokens: number;\n };\n cost_usd: number;\n finish_reason: 'stop' | 'length' | 'content_filter' | 'error';\n}\n\nexport interface StreamChunk {\n delta: string;\n finish_reason?: 'stop' | 'length' | 'content_filter' | 'error';\n}\n\nexport class AIModule {\n constructor(private client: CencoriClient) { }\n\n /**\n * Send a chat completion request (non-streaming)\n */\n async chat(params: ChatParams): Promise<ChatResponse> {\n const response = await this.client.request<ChatResponse>('/api/ai/chat', {\n method: 'POST',\n body: JSON.stringify({ ...params, stream: false }),\n });\n\n return response;\n }\n\n /**\n * Send a chat completion request with streaming\n * Returns an async generator that yields chunks as they arrive\n */\n async *chatStream(params: ChatParams): AsyncGenerator<StreamChunk, void, unknown> {\n const url = `${this.client.getBaseUrl()}/api/ai/chat`;\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n 'CENCORI_API_KEY': this.client.getApiKey(),\n };\n\n const response = await fetch(url, {\n method: 'POST',\n headers,\n body: JSON.stringify({ ...params, stream: true }),\n });\n\n if (!response.ok) {\n const error = await response.json() as ErrorResponse;\n throw new Error(error.error || 'Stream request failed');\n }\n\n if (!response.body) {\n throw new Error('Response body is null');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n const lines = buffer.split('\\n');\n\n // Keep the last incomplete line in the buffer\n buffer = lines.pop() || '';\n\n for (const line of lines) {\n if (line.trim() === '') continue;\n if (!line.startsWith('data: ')) continue;\n\n const data = line.slice(6); // Remove 'data: ' prefix\n\n if (data === '[DONE]') {\n return;\n }\n\n try {\n const chunk = JSON.parse(data) as StreamChunk;\n yield chunk;\n } catch (e) {\n console.error('Failed to parse SSE data:', e);\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n }\n}\n","function sleep(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\nexport async function fetchWithRetry(\n url: string,\n options: RequestInit,\n maxRetries = 3\n): Promise<Response> {\n let lastError: Error | null = null;\n\n for (let attempt = 0; attempt < maxRetries; attempt++) {\n try {\n const response = await fetch(url, options);\n\n // Return immediately if request succeeded or if it's a client error (4xx)\n if (response.ok || (response.status >= 400 && response.status < 500)) {\n return response;\n }\n\n // Retry on 5xx errors\n lastError = new Error(`HTTP ${response.status}: ${response.statusText}`);\n\n // Don't retry on last attempt\n if (attempt === maxRetries - 1) {\n return response;\n }\n\n // Exponential backoff: 1s, 2s, 4s\n await sleep(Math.pow(2, attempt) * 1000);\n } catch (error) {\n lastError = error instanceof Error ? error : new Error(String(error));\n\n // Don't retry on last attempt\n if (attempt === maxRetries - 1) {\n throw lastError;\n }\n\n // Exponential backoff\n await sleep(Math.pow(2, attempt) * 1000);\n }\n }\n\n throw lastError || new Error('Max retries reached');\n}\n","export class CencoriError extends Error {\n constructor(\n message: string,\n public statusCode?: number,\n public code?: string\n ) {\n super(message);\n this.name = 'CencoriError';\n Object.setPrototypeOf(this, CencoriError.prototype);\n }\n}\n\nexport class AuthenticationError extends CencoriError {\n constructor(message = 'Invalid API key') {\n super(message, 401, 'INVALID_API_KEY');\n this.name = 'AuthenticationError';\n Object.setPrototypeOf(this, AuthenticationError.prototype);\n }\n}\n\nexport class RateLimitError extends CencoriError {\n constructor(message = 'Rate limit exceeded') {\n super(message, 429, 'RATE_LIMIT_EXCEEDED');\n this.name = 'RateLimitError';\n Object.setPrototypeOf(this, RateLimitError.prototype);\n }\n}\n\nexport class SafetyError extends CencoriError {\n constructor(message = 'Content safety violation', public reasons?: string[]) {\n super(message, 400, 'SAFETY_VIOLATION');\n this.name = 'SafetyError';\n Object.setPrototypeOf(this, SafetyError.prototype);\n }\n}\n","import type { CencoriConfig, RequestOptions } from './types/common';\nimport { AIModule } from './modules/ai';\nimport { fetchWithRetry } from './utils';\nimport {\n CencoriError,\n AuthenticationError,\n RateLimitError,\n SafetyError\n} from './errors';\n\nexport interface ErrorResponse {\n error?: string;\n reasons?: string[];\n}\n\nexport class CencoriClient {\n private apiKey: string;\n private baseUrl: string;\n\n public ai: AIModule;\n\n constructor(config: CencoriConfig) {\n if (!config.apiKey) {\n throw new Error('API key is required');\n }\n\n this.apiKey = config.apiKey;\n this.baseUrl = config.baseUrl || 'https://cencori.com';\n\n this.ai = new AIModule(this);\n }\n\n // Public getters for internal use by modules\n getBaseUrl(): string {\n return this.baseUrl;\n }\n\n getApiKey(): string {\n return this.apiKey;\n }\n\n async request<T>(endpoint: string, options: RequestOptions): Promise<T> {\n const url = `${this.baseUrl}${endpoint}`;\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n 'CENCORI_API_KEY': this.apiKey,\n ...options.headers\n };\n\n try {\n const response = await fetchWithRetry(url, {\n method: options.method,\n headers,\n body: options.body\n });\n\n const data: unknown = await response.json();\n\n // Handle API errors\n if (!response.ok) {\n const errorData = data as ErrorResponse;\n\n if (response.status === 401) {\n throw new AuthenticationError(errorData.error || 'Invalid API key');\n }\n if (response.status === 429) {\n throw new RateLimitError(errorData.error || 'Rate limit exceeded');\n }\n if (response.status === 400 && errorData.reasons) {\n throw new SafetyError(errorData.error, errorData.reasons);\n }\n throw new CencoriError(\n errorData.error || 'Request failed',\n response.status\n );\n }\n\n return data as T;\n } catch (error) {\n // Re-throw custom errors\n if (error instanceof CencoriError) {\n throw error;\n }\n\n // Wrap unknown errors\n throw new CencoriError(\n error instanceof Error ? error.message : 'Unknown error occurred'\n );\n }\n }\n}\n"],"mappings":";AAkCO,IAAM,WAAN,MAAe;AAAA,EAClB,YAAoB,QAAuB;AAAvB;AAAA,EAAyB;AAAA;AAAA;AAAA;AAAA,EAK7C,MAAM,KAAK,QAA2C;AAClD,UAAM,WAAW,MAAM,KAAK,OAAO,QAAsB,gBAAgB;AAAA,MACrE,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU,EAAE,GAAG,QAAQ,QAAQ,MAAM,CAAC;AAAA,IACrD,CAAC;AAED,WAAO;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,WAAW,QAAgE;AAC9E,U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UAAU,OAAO,UAAU,OAAO;AAAA,QAC5D;AACA,cAAM,IAAI;AAAA,UACN,UAAU,SAAS;AAAA,UACnB,SAAS;AAAA,QACb;AAAA,MACJ;AAEA,aAAO;AAAA,IACX,SAAS,OAAO;AAEZ,UAAI,iBAAiB,cAAc;AAC/B,cAAM;AAAA,MACV;AAGA,YAAM,IAAI;AAAA,QACN,iBAAiB,QAAQ,MAAM,UAAU;AAAA,MAC7C;AAAA,IACJ;AAAA,EACJ;AACJ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,33 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "cencori",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Official SDK for Cencori",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.mjs",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"files": [
|
|
9
|
-
"dist"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
},
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
}
|
|
33
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "cencori",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Official TypeScript SDK for Cencori - The Security Layer for AI Development",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"keywords": [
|
|
13
|
+
"ai",
|
|
14
|
+
"gateway",
|
|
15
|
+
"cencori",
|
|
16
|
+
"sdk",
|
|
17
|
+
"typescript",
|
|
18
|
+
"openai",
|
|
19
|
+
"anthropic",
|
|
20
|
+
"streaming",
|
|
21
|
+
"security"
|
|
22
|
+
],
|
|
23
|
+
"author": "FohnAI",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/cencori/cencori"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://cencori.com",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/cencori/cencori/issues"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"dev": "tsup --watch",
|
|
36
|
+
"prepublishOnly": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"tsup": "^8.0.0",
|
|
40
|
+
"typescript": "^5.3.0"
|
|
41
|
+
}
|
|
42
|
+
}
|