@codetunezstudios/token-kit 0.1.0-beta.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 +258 -0
- package/dist/index.d.mts +232 -0
- package/dist/index.d.ts +232 -0
- package/dist/index.js +359 -0
- package/dist/index.mjs +321 -0
- package/package.json +77 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
|
|
4
|
+
// src/types.ts
|
|
5
|
+
var TokenKitAPIError = class extends Error {
|
|
6
|
+
constructor(message, code, statusCode, details) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "TokenKitAPIError";
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.statusCode = statusCode;
|
|
11
|
+
this.details = details;
|
|
12
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/client.ts
|
|
17
|
+
var TokenKitClient = class {
|
|
18
|
+
constructor(config) {
|
|
19
|
+
if (!config.apiKey) {
|
|
20
|
+
throw new Error("API key is required");
|
|
21
|
+
}
|
|
22
|
+
this.apiKey = config.apiKey;
|
|
23
|
+
this.client = axios.create({
|
|
24
|
+
baseURL: config.baseUrl || "https://api.token-kit.com/api/v1",
|
|
25
|
+
timeout: config.timeout || 6e4,
|
|
26
|
+
headers: {
|
|
27
|
+
"Content-Type": "application/json",
|
|
28
|
+
"Authorization": `Bearer ${this.apiKey}`
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
this.client.interceptors.response.use(
|
|
32
|
+
(response) => response,
|
|
33
|
+
(error) => {
|
|
34
|
+
return Promise.reject(this.handleError(error));
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Send a chat completion request
|
|
40
|
+
*/
|
|
41
|
+
async chat(userToken, messages, options = {}) {
|
|
42
|
+
try {
|
|
43
|
+
const response = await this.client.post("/llm/chat", {
|
|
44
|
+
userToken,
|
|
45
|
+
messages,
|
|
46
|
+
model: options.model,
|
|
47
|
+
maxTokens: options.maxTokens,
|
|
48
|
+
temperature: options.temperature
|
|
49
|
+
});
|
|
50
|
+
if (!response.data.success || !response.data.data) {
|
|
51
|
+
throw new TokenKitAPIError(
|
|
52
|
+
response.data.error?.message || "Chat request failed",
|
|
53
|
+
response.data.error?.code || "CHAT_ERROR",
|
|
54
|
+
response.status,
|
|
55
|
+
response.data.error?.details
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return response.data.data;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof TokenKitAPIError) {
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
throw this.handleError(error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Validate a user token
|
|
68
|
+
*/
|
|
69
|
+
async validateToken(userToken) {
|
|
70
|
+
try {
|
|
71
|
+
const response = await this.client.post(
|
|
72
|
+
"/tokens/validate",
|
|
73
|
+
{ userToken }
|
|
74
|
+
);
|
|
75
|
+
if (!response.data.success || !response.data.data) {
|
|
76
|
+
throw new TokenKitAPIError(
|
|
77
|
+
response.data.error?.message || "Token validation failed",
|
|
78
|
+
response.data.error?.code || "VALIDATION_ERROR",
|
|
79
|
+
response.status,
|
|
80
|
+
response.data.error?.details
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return response.data.data;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error instanceof TokenKitAPIError) {
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
throw this.handleError(error);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get list of available models
|
|
93
|
+
*/
|
|
94
|
+
async getModels() {
|
|
95
|
+
try {
|
|
96
|
+
const response = await this.client.get("/llm/models");
|
|
97
|
+
if (!response.data.success || !response.data.data) {
|
|
98
|
+
throw new TokenKitAPIError(
|
|
99
|
+
response.data.error?.message || "Failed to fetch models",
|
|
100
|
+
response.data.error?.code || "MODELS_ERROR",
|
|
101
|
+
response.status,
|
|
102
|
+
response.data.error?.details
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
return response.data.data.models;
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (error instanceof TokenKitAPIError) {
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
throw this.handleError(error);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Handle axios errors and convert to TokenKitAPIError
|
|
115
|
+
*/
|
|
116
|
+
handleError(error) {
|
|
117
|
+
const isAxios = axios.isAxiosError(error) || typeof error === "object" && error !== null && "isAxiosError" in error;
|
|
118
|
+
if (isAxios) {
|
|
119
|
+
const axiosError = error;
|
|
120
|
+
if (axiosError.code === "ECONNABORTED") {
|
|
121
|
+
return new TokenKitAPIError(
|
|
122
|
+
"Request timeout",
|
|
123
|
+
"TIMEOUT",
|
|
124
|
+
504
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
if (axiosError.code === "ECONNREFUSED" || !axiosError.response) {
|
|
128
|
+
return new TokenKitAPIError(
|
|
129
|
+
"Unable to connect to Token-Kit API",
|
|
130
|
+
"NETWORK_ERROR",
|
|
131
|
+
503
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
if (axiosError.response?.data?.error) {
|
|
135
|
+
return new TokenKitAPIError(
|
|
136
|
+
axiosError.response.data.error.message,
|
|
137
|
+
axiosError.response.data.error.code,
|
|
138
|
+
axiosError.response.status,
|
|
139
|
+
axiosError.response.data.error.details
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
return new TokenKitAPIError(
|
|
143
|
+
axiosError.message || "API request failed",
|
|
144
|
+
"API_ERROR",
|
|
145
|
+
axiosError.response?.status
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
if (error instanceof Error) {
|
|
149
|
+
return new TokenKitAPIError(
|
|
150
|
+
error.message,
|
|
151
|
+
"UNKNOWN_ERROR"
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return new TokenKitAPIError(
|
|
155
|
+
"An unknown error occurred",
|
|
156
|
+
"UNKNOWN_ERROR"
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// src/index.ts
|
|
162
|
+
var TokenKit = class {
|
|
163
|
+
/**
|
|
164
|
+
* Create a new TokenKit instance
|
|
165
|
+
*
|
|
166
|
+
* @param config - Configuration options
|
|
167
|
+
* @param config.apiKey - Your developer API key
|
|
168
|
+
* @param config.baseUrl - API base URL (optional, defaults to production)
|
|
169
|
+
* @param config.timeout - Request timeout in ms (optional, defaults to 60000)
|
|
170
|
+
*/
|
|
171
|
+
constructor(config) {
|
|
172
|
+
this.userToken = null;
|
|
173
|
+
this.client = new TokenKitClient(config);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Set the user token for subsequent requests
|
|
177
|
+
* This allows you to omit the userToken parameter in other methods
|
|
178
|
+
*
|
|
179
|
+
* @param userToken - The user's token
|
|
180
|
+
*/
|
|
181
|
+
setUserToken(userToken) {
|
|
182
|
+
this.userToken = userToken;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Send a chat completion request
|
|
186
|
+
*
|
|
187
|
+
* @param userTokenOrMessages - User token string OR array of messages (if user token was set)
|
|
188
|
+
* @param messagesOrOptions - Array of messages OR chat options (if first param is user token)
|
|
189
|
+
* @param options - Chat options (only if first two params are provided)
|
|
190
|
+
* @returns Chat completion response
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* // With explicit user token
|
|
195
|
+
* const response = await tokenKit.chat('user-token', [
|
|
196
|
+
* { role: 'user', content: 'What is Token-Kit?' }
|
|
197
|
+
* ]);
|
|
198
|
+
*
|
|
199
|
+
* // With pre-set user token
|
|
200
|
+
* tokenKit.setUserToken('user-token');
|
|
201
|
+
* const response = await tokenKit.chat([
|
|
202
|
+
* { role: 'user', content: 'What is Token-Kit?' }
|
|
203
|
+
* ]);
|
|
204
|
+
*
|
|
205
|
+
* // With options
|
|
206
|
+
* const response = await tokenKit.chat('user-token', [
|
|
207
|
+
* { role: 'system', content: 'You are a helpful assistant.' },
|
|
208
|
+
* { role: 'user', content: 'Hello!' }
|
|
209
|
+
* ], {
|
|
210
|
+
* model: 'gpt-4',
|
|
211
|
+
* maxTokens: 200,
|
|
212
|
+
* temperature: 0.8
|
|
213
|
+
* });
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
async chat(userTokenOrMessages, messagesOrOptions, options) {
|
|
217
|
+
let userToken;
|
|
218
|
+
let messages;
|
|
219
|
+
let chatOptions;
|
|
220
|
+
if (typeof userTokenOrMessages === "string") {
|
|
221
|
+
userToken = userTokenOrMessages;
|
|
222
|
+
messages = messagesOrOptions;
|
|
223
|
+
chatOptions = options;
|
|
224
|
+
} else {
|
|
225
|
+
if (!this.userToken) {
|
|
226
|
+
throw new Error("User token not set. Call setUserToken() first or pass userToken to chat().");
|
|
227
|
+
}
|
|
228
|
+
userToken = this.userToken;
|
|
229
|
+
messages = userTokenOrMessages;
|
|
230
|
+
chatOptions = messagesOrOptions;
|
|
231
|
+
}
|
|
232
|
+
if (!messages || messages.length === 0) {
|
|
233
|
+
throw new Error("Messages array cannot be empty");
|
|
234
|
+
}
|
|
235
|
+
return this.client.chat(userToken, messages, chatOptions);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Validate a user token and check balance
|
|
239
|
+
*
|
|
240
|
+
* @param userToken - User token to validate (optional if already set)
|
|
241
|
+
* @returns Token validation response with balance and limits
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* const validation = await tokenKit.validateToken('user-token');
|
|
246
|
+
* console.log('Balance:', validation.balance);
|
|
247
|
+
* console.log('Daily spending:', validation.dailySpending);
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
async validateToken(userToken) {
|
|
251
|
+
const token = userToken || this.userToken;
|
|
252
|
+
if (!token) {
|
|
253
|
+
throw new Error("User token not set. Call setUserToken() first or pass userToken to validateToken().");
|
|
254
|
+
}
|
|
255
|
+
return this.client.validateToken(token);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Get balance for the current user token
|
|
259
|
+
*
|
|
260
|
+
* @param userToken - User token (optional if already set)
|
|
261
|
+
* @returns Current token balance
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* const balance = await tokenKit.getBalance('user-token');
|
|
266
|
+
* console.log(`You have ${balance} tokens remaining`);
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
async getBalance(userToken) {
|
|
270
|
+
const validation = await this.validateToken(userToken);
|
|
271
|
+
return validation.balance || 0;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Get list of available LLM models
|
|
275
|
+
*
|
|
276
|
+
* @returns Array of model names
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* const models = await tokenKit.getModels();
|
|
281
|
+
* console.log('Available models:', models);
|
|
282
|
+
* // ['gpt-3.5-turbo', 'gpt-4', 'gpt-4-turbo']
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
async getModels() {
|
|
286
|
+
return this.client.getModels();
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Helper method to create a simple user message
|
|
290
|
+
*
|
|
291
|
+
* @param content - Message content
|
|
292
|
+
* @returns Message object
|
|
293
|
+
*/
|
|
294
|
+
static user(content) {
|
|
295
|
+
return { role: "user", content };
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Helper method to create a system message
|
|
299
|
+
*
|
|
300
|
+
* @param content - Message content
|
|
301
|
+
* @returns Message object
|
|
302
|
+
*/
|
|
303
|
+
static system(content) {
|
|
304
|
+
return { role: "system", content };
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Helper method to create an assistant message
|
|
308
|
+
*
|
|
309
|
+
* @param content - Message content
|
|
310
|
+
* @returns Message object
|
|
311
|
+
*/
|
|
312
|
+
static assistant(content) {
|
|
313
|
+
return { role: "assistant", content };
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
var index_default = TokenKit;
|
|
317
|
+
export {
|
|
318
|
+
TokenKit,
|
|
319
|
+
TokenKitAPIError,
|
|
320
|
+
index_default as default
|
|
321
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codetunezstudios/token-kit",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"description": "Official TypeScript SDK for token-kit - AI token infrastructure for developers",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"import": "./dist/index.mjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
22
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
23
|
+
"test": "jest",
|
|
24
|
+
"test:watch": "jest --watch",
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"lint": "eslint src --ext .ts",
|
|
27
|
+
"prepublishOnly": "npm run build"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"token-kit",
|
|
31
|
+
"ai",
|
|
32
|
+
"llm",
|
|
33
|
+
"openai",
|
|
34
|
+
"gpt",
|
|
35
|
+
"claude",
|
|
36
|
+
"gemini",
|
|
37
|
+
"tokens",
|
|
38
|
+
"sdk",
|
|
39
|
+
"typescript",
|
|
40
|
+
"ai-tokens",
|
|
41
|
+
"pay-per-use",
|
|
42
|
+
"developer-tools",
|
|
43
|
+
"api"
|
|
44
|
+
],
|
|
45
|
+
"author": "Codetunez Studios",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "https://github.com/codetunez-studios/token-kit"
|
|
50
|
+
},
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/codetunez-studios/token-kit/issues"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://token-kit.com",
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"axios": "^1.6.5"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@types/express": "^5.0.6",
|
|
63
|
+
"@types/jest": "^29.5.11",
|
|
64
|
+
"@types/node": "^20.10.6",
|
|
65
|
+
"@typescript-eslint/eslint-plugin": "^6.17.0",
|
|
66
|
+
"@typescript-eslint/parser": "^6.17.0",
|
|
67
|
+
"eslint": "^8.56.0",
|
|
68
|
+
"express": "^5.2.1",
|
|
69
|
+
"jest": "^29.7.0",
|
|
70
|
+
"ts-jest": "^29.1.1",
|
|
71
|
+
"tsup": "^8.0.1",
|
|
72
|
+
"typescript": "^5.3.3"
|
|
73
|
+
},
|
|
74
|
+
"engines": {
|
|
75
|
+
"node": ">=18"
|
|
76
|
+
}
|
|
77
|
+
}
|