primellm 0.2.0 → 1.0.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/dist/errors.js DELETED
@@ -1,93 +0,0 @@
1
- /**
2
- * PrimeLLM SDK Error Classes
3
- *
4
- * Typed errors for better error handling in applications.
5
- */
6
- /**
7
- * Base error class for all PrimeLLM SDK errors
8
- */
9
- export class PrimeLLMError extends Error {
10
- constructor(message, meta) {
11
- super(message);
12
- this.name = 'PrimeLLMError';
13
- this.meta = meta;
14
- }
15
- }
16
- /**
17
- * Authentication failed (401)
18
- */
19
- export class AuthenticationError extends PrimeLLMError {
20
- constructor(message = 'Invalid or missing API key', meta) {
21
- super(message, meta);
22
- this.name = 'AuthenticationError';
23
- }
24
- }
25
- /**
26
- * Insufficient credits (402)
27
- */
28
- export class InsufficientCreditsError extends PrimeLLMError {
29
- constructor(message = 'Insufficient credits', meta) {
30
- super(message, meta);
31
- this.name = 'InsufficientCreditsError';
32
- }
33
- }
34
- /**
35
- * Rate limit exceeded (429)
36
- */
37
- export class RateLimitError extends PrimeLLMError {
38
- constructor(message = 'Rate limit exceeded', meta, retryAfter) {
39
- super(message, meta);
40
- this.name = 'RateLimitError';
41
- this.retryAfter = retryAfter;
42
- }
43
- }
44
- /**
45
- * Resource not found (404)
46
- */
47
- export class NotFoundError extends PrimeLLMError {
48
- constructor(message = 'Resource not found', meta) {
49
- super(message, meta);
50
- this.name = 'NotFoundError';
51
- }
52
- }
53
- /**
54
- * Validation error (400)
55
- */
56
- export class ValidationError extends PrimeLLMError {
57
- constructor(message = 'Invalid request', meta) {
58
- super(message, meta);
59
- this.name = 'ValidationError';
60
- }
61
- }
62
- /**
63
- * Server error (5xx)
64
- */
65
- export class ServerError extends PrimeLLMError {
66
- constructor(message = 'Server error', meta) {
67
- super(message, meta);
68
- this.name = 'ServerError';
69
- }
70
- }
71
- /**
72
- * Map HTTP status code to appropriate error class
73
- */
74
- export function createErrorFromStatus(status, message, detail) {
75
- const meta = { status, detail };
76
- switch (status) {
77
- case 400:
78
- return new ValidationError(message, meta);
79
- case 401:
80
- return new AuthenticationError(message, meta);
81
- case 402:
82
- return new InsufficientCreditsError(message, meta);
83
- case 404:
84
- return new NotFoundError(message, meta);
85
- case 429:
86
- return new RateLimitError(message, meta);
87
- default:
88
- if (status >= 500) {
89
- return new ServerError(message, meta);
90
- }
91
- return new PrimeLLMError(message, meta);
92
- }
93
- }
@@ -1,29 +0,0 @@
1
- /**
2
- * PrimeLLM Streaming Utilities
3
- *
4
- * Async iterator for streaming chat completions.
5
- */
6
- /**
7
- * Chat stream chunk from SSE
8
- */
9
- export interface StreamChunk {
10
- id: string;
11
- object: string;
12
- delta?: {
13
- role?: string;
14
- content?: string;
15
- };
16
- done?: boolean;
17
- finish_reason?: string;
18
- }
19
- /**
20
- * Parse SSE data line to chunk object
21
- */
22
- export declare function parseSSELine(line: string): StreamChunk | null;
23
- /**
24
- * Create async iterator from SSE response stream
25
- *
26
- * @param reader - ReadableStreamDefaultReader from fetch response
27
- */
28
- export declare function streamReader(reader: ReadableStreamDefaultReader<Uint8Array>): AsyncGenerator<StreamChunk, void, unknown>;
29
- //# sourceMappingURL=streaming.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../src/streaming.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,MAAM,WAAW,WAAW;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE;QACJ,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,OAAO,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IACF,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,aAAa,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAe7D;AAED;;;;GAIG;AACH,wBAAuB,YAAY,CAC/B,MAAM,EAAE,2BAA2B,CAAC,UAAU,CAAC,GAChD,cAAc,CAAC,WAAW,EAAE,IAAI,EAAE,OAAO,CAAC,CAyC5C"}
package/dist/streaming.js DELETED
@@ -1,64 +0,0 @@
1
- /**
2
- * PrimeLLM Streaming Utilities
3
- *
4
- * Async iterator for streaming chat completions.
5
- */
6
- /**
7
- * Parse SSE data line to chunk object
8
- */
9
- export function parseSSELine(line) {
10
- if (!line.startsWith('data:')) {
11
- return null;
12
- }
13
- const data = line.slice(5).trim();
14
- if (!data || data === '[DONE]') {
15
- return null;
16
- }
17
- try {
18
- return JSON.parse(data);
19
- }
20
- catch {
21
- return null;
22
- }
23
- }
24
- /**
25
- * Create async iterator from SSE response stream
26
- *
27
- * @param reader - ReadableStreamDefaultReader from fetch response
28
- */
29
- export async function* streamReader(reader) {
30
- const decoder = new TextDecoder();
31
- let buffer = '';
32
- try {
33
- while (true) {
34
- const { done, value } = await reader.read();
35
- if (done) {
36
- break;
37
- }
38
- buffer += decoder.decode(value, { stream: true });
39
- // Process complete lines
40
- const lines = buffer.split('\n');
41
- buffer = lines.pop() || ''; // Keep incomplete line in buffer
42
- for (const line of lines) {
43
- const chunk = parseSSELine(line);
44
- if (chunk) {
45
- yield chunk;
46
- // Check for done
47
- if (chunk.object === 'chat.completion.done' || chunk.done) {
48
- return;
49
- }
50
- }
51
- }
52
- }
53
- // Process remaining buffer
54
- if (buffer) {
55
- const chunk = parseSSELine(buffer);
56
- if (chunk) {
57
- yield chunk;
58
- }
59
- }
60
- }
61
- finally {
62
- reader.releaseLock();
63
- }
64
- }
@@ -1,42 +0,0 @@
1
- /**
2
- * PrimeLLM Token Counter
3
- *
4
- * Simple token estimation using chars/4 approximation.
5
- * Provides adapter point for future tiktoken integration.
6
- */
7
- import { Message } from './types.js';
8
- /**
9
- * Token counter adapter function type
10
- */
11
- export type TokenizerAdapter = (text: string) => number;
12
- /**
13
- * Count tokens in text or messages array
14
- *
15
- * @param input - Text string or array of messages
16
- * @returns Estimated token count
17
- *
18
- * @example
19
- * countTokens("Hello world") // ~3
20
- * countTokens([{role:"user", content:"Hello"}]) // ~2
21
- */
22
- export declare function countTokens(input: string | Message[]): number;
23
- /**
24
- * Set custom tokenizer adapter (for tiktoken or other)
25
- *
26
- * @param adapter - Function that takes text and returns token count
27
- *
28
- * @example
29
- * import { encoding_for_model } from 'tiktoken';
30
- * const enc = encoding_for_model('gpt-4');
31
- * setTokenizerAdapter((text) => enc.encode(text).length);
32
- */
33
- export declare function setTokenizerAdapter(adapter: TokenizerAdapter | null): void;
34
- /**
35
- * Get current tokenizer adapter
36
- */
37
- export declare function getTokenizerAdapter(): TokenizerAdapter | null;
38
- /**
39
- * Reset tokenizer to default (chars/4)
40
- */
41
- export declare function resetTokenizer(): void;
42
- //# sourceMappingURL=tokenizer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tokenizer.d.ts","sourceRoot":"","sources":["../src/tokenizer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;AAOxD;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,EAAE,GAAG,MAAM,CAiB7D;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,GAAG,IAAI,CAE1E;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,gBAAgB,GAAG,IAAI,CAE7D;AAED;;GAEG;AACH,wBAAgB,cAAc,IAAI,IAAI,CAErC"}
package/dist/tokenizer.js DELETED
@@ -1,61 +0,0 @@
1
- /**
2
- * PrimeLLM Token Counter
3
- *
4
- * Simple token estimation using chars/4 approximation.
5
- * Provides adapter point for future tiktoken integration.
6
- */
7
- /**
8
- * Current tokenizer adapter (defaults to simple chars/4)
9
- */
10
- let tokenizerAdapter = null;
11
- /**
12
- * Count tokens in text or messages array
13
- *
14
- * @param input - Text string or array of messages
15
- * @returns Estimated token count
16
- *
17
- * @example
18
- * countTokens("Hello world") // ~3
19
- * countTokens([{role:"user", content:"Hello"}]) // ~2
20
- */
21
- export function countTokens(input) {
22
- let text;
23
- if (Array.isArray(input)) {
24
- text = input.map(m => m.content).join(' ');
25
- }
26
- else {
27
- text = input;
28
- }
29
- // Use custom adapter if set
30
- if (tokenizerAdapter) {
31
- return tokenizerAdapter(text);
32
- }
33
- // Default: chars / 4 (simple approximation)
34
- const chars = text.length;
35
- return Math.max(1, Math.ceil(chars / 4));
36
- }
37
- /**
38
- * Set custom tokenizer adapter (for tiktoken or other)
39
- *
40
- * @param adapter - Function that takes text and returns token count
41
- *
42
- * @example
43
- * import { encoding_for_model } from 'tiktoken';
44
- * const enc = encoding_for_model('gpt-4');
45
- * setTokenizerAdapter((text) => enc.encode(text).length);
46
- */
47
- export function setTokenizerAdapter(adapter) {
48
- tokenizerAdapter = adapter;
49
- }
50
- /**
51
- * Get current tokenizer adapter
52
- */
53
- export function getTokenizerAdapter() {
54
- return tokenizerAdapter;
55
- }
56
- /**
57
- * Reset tokenizer to default (chars/4)
58
- */
59
- export function resetTokenizer() {
60
- tokenizerAdapter = null;
61
- }
package/dist/types.d.ts DELETED
@@ -1,172 +0,0 @@
1
- /**
2
- * PrimeLLM SDK Types
3
- *
4
- * This file contains all the TypeScript types used by the PrimeLLM SDK.
5
- * These types match the response format from the PrimeLLM API.
6
- */
7
- /**
8
- * The role of a message in a conversation.
9
- */
10
- export type ChatRole = "system" | "user" | "assistant";
11
- /**
12
- * A single message in a conversation.
13
- */
14
- export interface ChatMessage {
15
- role: ChatRole;
16
- content: string;
17
- }
18
- /** Alias for ChatMessage */
19
- export type Message = ChatMessage;
20
- /**
21
- * Request body for the /v1/chat endpoint.
22
- */
23
- export interface ChatRequest {
24
- model: string;
25
- messages: ChatMessage[];
26
- stream?: boolean;
27
- metadata?: Record<string, unknown>;
28
- temperature?: number;
29
- max_tokens?: number;
30
- }
31
- /**
32
- * Request body for the /generate endpoint (legacy).
33
- */
34
- export interface GenerateRequest {
35
- model: string;
36
- messages: ChatMessage[];
37
- max_tokens?: number;
38
- temperature?: number;
39
- stream?: boolean;
40
- metadata?: Record<string, unknown>;
41
- }
42
- /**
43
- * Request body for embeddings
44
- */
45
- export interface EmbeddingsRequest {
46
- model?: string;
47
- input: string | string[];
48
- }
49
- export interface ChatChoice {
50
- index: number;
51
- message: ChatMessage;
52
- finish_reason?: string | null;
53
- }
54
- export interface Usage {
55
- prompt_tokens: number;
56
- completion_tokens: number;
57
- total_tokens: number;
58
- }
59
- export interface CreditsInfo {
60
- remaining: number;
61
- cost?: number;
62
- }
63
- export interface ChatResponse {
64
- id: string;
65
- model: string;
66
- created: number;
67
- object?: string;
68
- choices: ChatChoice[];
69
- usage: Usage;
70
- credits?: CreditsInfo;
71
- }
72
- export interface GenerateResponse {
73
- reply: string;
74
- model: string;
75
- tokens_used: number;
76
- cost: number;
77
- credits_remaining: number;
78
- }
79
- /**
80
- * Embedding data item
81
- */
82
- export interface EmbeddingData {
83
- object: string;
84
- embedding: number[];
85
- index: number;
86
- }
87
- /**
88
- * Embeddings response
89
- */
90
- export interface EmbeddingsResponse {
91
- object: string;
92
- data: EmbeddingData[];
93
- model: string;
94
- usage: {
95
- prompt_tokens: number;
96
- total_tokens: number;
97
- };
98
- cost?: number;
99
- credits_remaining?: number;
100
- }
101
- /**
102
- * Model info
103
- */
104
- export interface ModelInfo {
105
- id: string;
106
- object: string;
107
- owned_by: string;
108
- label: string;
109
- description: string;
110
- pricing: {
111
- per_1k_tokens: number;
112
- currency: string;
113
- };
114
- capabilities: string[];
115
- }
116
- /**
117
- * Models list response
118
- */
119
- export interface ModelsResponse {
120
- object: string;
121
- data: ModelInfo[];
122
- }
123
- /**
124
- * Credits response
125
- */
126
- export interface CreditsResponse {
127
- credits: number;
128
- currency: string;
129
- user_id?: number;
130
- }
131
- /**
132
- * API Key info
133
- */
134
- export interface KeyInfo {
135
- id: number;
136
- key_prefix: string;
137
- created_at: number;
138
- revoked: boolean;
139
- active: boolean;
140
- }
141
- /**
142
- * Keys list response
143
- */
144
- export interface KeysResponse {
145
- object: string;
146
- data: KeyInfo[];
147
- max_allowed: number;
148
- }
149
- /**
150
- * Key create response
151
- */
152
- export interface KeyCreateResponse {
153
- id: number;
154
- key: string;
155
- created_at: number;
156
- label?: string;
157
- message: string;
158
- }
159
- /**
160
- * Options for creating a PrimeLLM client.
161
- */
162
- export interface PrimeLLMClientOptions {
163
- /** Your PrimeLLM API key */
164
- apiKey: string;
165
- /** Base URL for the API (default: "https://api.primellm.in") */
166
- baseURL?: string;
167
- /** Request timeout in milliseconds (default: 60000) */
168
- timeoutMs?: number;
169
- /** Max retry attempts for failed requests (default: 3) */
170
- maxRetries?: number;
171
- }
172
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AAEvD;;GAEG;AACH,MAAM,WAAW,WAAW;IACxB,IAAI,EAAE,QAAQ,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACnB;AAED,4BAA4B;AAC5B,MAAM,MAAM,OAAO,GAAG,WAAW,CAAC;AAMlC;;GAEG;AACH,MAAM,WAAW,WAAW;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAA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AC;IACf,IAAI,EAAE,OAAO,EAAE,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACnB;AAMD;;GAEG;AACH,MAAM,WAAW,qBAAqB;IAClC,4BAA4B;IAC5B,MAAM,EAAE,MAAM,CAAC;IAEf,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,uDAAuD;IACvD,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,0DAA0D;IAC1D,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB"}
package/dist/types.js DELETED
@@ -1,7 +0,0 @@
1
- /**
2
- * PrimeLLM SDK Types
3
- *
4
- * This file contains all the TypeScript types used by the PrimeLLM SDK.
5
- * These types match the response format from the PrimeLLM API.
6
- */
7
- export {};