compress-lightreach 1.0.8 → 1.0.9
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 +27 -8
- package/dist/api-client.d.ts +22 -0
- package/dist/api-client.js +62 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Compress Light Reach
|
|
2
2
|
|
|
3
|
-
**
|
|
3
|
+
**OpenAI-compatible LLM routing + compression SDK (superset responses with LightReach metadata)**
|
|
4
4
|
|
|
5
5
|
[](https://badge.fury.io/js/compress-lightreach)
|
|
6
6
|
[](https://nodejs.org/)
|
|
@@ -52,7 +52,7 @@ const result = await client.complete({
|
|
|
52
52
|
tags: { team: 'backend', environment: 'production' },
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
-
console.log(result.
|
|
55
|
+
console.log(result.choices[0].message.content);
|
|
56
56
|
console.log(`Selected: ${result.routing_info?.selected_model}`);
|
|
57
57
|
console.log(`Token savings: ${result.compression_stats.token_savings}`);
|
|
58
58
|
```
|
|
@@ -162,7 +162,7 @@ const result = await client.complete({
|
|
|
162
162
|
compress_output: true,
|
|
163
163
|
});
|
|
164
164
|
|
|
165
|
-
console.log(result.
|
|
165
|
+
console.log(result.choices[0].message.content);
|
|
166
166
|
```
|
|
167
167
|
|
|
168
168
|
### With Compression Config
|
|
@@ -189,7 +189,7 @@ const result = await client.complete({
|
|
|
189
189
|
tags: { team: 'backend', environment: 'production' },
|
|
190
190
|
});
|
|
191
191
|
|
|
192
|
-
console.log(result.
|
|
192
|
+
console.log(result.choices[0].message.content);
|
|
193
193
|
console.log(`Model used: ${result.routing_info?.selected_model}`);
|
|
194
194
|
```
|
|
195
195
|
|
|
@@ -276,8 +276,21 @@ For direct synchronous calls, use `completeSync()` instead.
|
|
|
276
276
|
|
|
277
277
|
```typescript
|
|
278
278
|
{
|
|
279
|
-
|
|
280
|
-
|
|
279
|
+
id: string; // OpenAI-style completion id
|
|
280
|
+
object: "chat.completion";
|
|
281
|
+
created: number; // Unix timestamp
|
|
282
|
+
model: string;
|
|
283
|
+
choices: Array<{
|
|
284
|
+
index: number;
|
|
285
|
+
message: { role: "assistant"; content: string | null; tool_calls?: any[] };
|
|
286
|
+
finish_reason: string | null;
|
|
287
|
+
}>;
|
|
288
|
+
usage: {
|
|
289
|
+
prompt_tokens: number;
|
|
290
|
+
completion_tokens: number;
|
|
291
|
+
total_tokens: number;
|
|
292
|
+
};
|
|
293
|
+
content: string; // Alias of choices[0].message.content
|
|
281
294
|
compression_stats: {
|
|
282
295
|
compression_enabled: boolean;
|
|
283
296
|
original_tokens: number;
|
|
@@ -307,6 +320,12 @@ For direct synchronous calls, use `completeSync()` instead.
|
|
|
307
320
|
hle_source: 'tag' | 'global' | 'none';
|
|
308
321
|
};
|
|
309
322
|
warnings?: string[];
|
|
323
|
+
lightreach?: { // Namespaced LightReach metadata extension
|
|
324
|
+
compression_stats?: object;
|
|
325
|
+
llm_stats?: object;
|
|
326
|
+
routing_info?: object;
|
|
327
|
+
latency_ms?: number | null;
|
|
328
|
+
};
|
|
310
329
|
|
|
311
330
|
// Convenience aliases
|
|
312
331
|
tokens_saved?: number;
|
|
@@ -454,7 +473,7 @@ const result = await client.complete({
|
|
|
454
473
|
tags: { team: 'content', environment: 'production' },
|
|
455
474
|
});
|
|
456
475
|
|
|
457
|
-
console.log(result.
|
|
476
|
+
console.log(result.choices[0].message.content);
|
|
458
477
|
console.log(`Model used: ${result.routing_info?.selected_model}`);
|
|
459
478
|
console.log(`Token savings: ${result.compression_stats.token_savings} tokens`);
|
|
460
479
|
console.log(`Compression ratio: ${(result.compression_stats.compression_ratio * 100).toFixed(2)}%`);
|
|
@@ -472,7 +491,7 @@ const result = await client.complete({
|
|
|
472
491
|
compress_output: true,
|
|
473
492
|
});
|
|
474
493
|
|
|
475
|
-
console.log(result.
|
|
494
|
+
console.log(result.choices[0].message.content);
|
|
476
495
|
```
|
|
477
496
|
|
|
478
497
|
### Example 3: Multi-turn Conversation
|
package/dist/api-client.d.ts
CHANGED
|
@@ -28,7 +28,27 @@ export interface DecompressResponse {
|
|
|
28
28
|
processing_time_ms: number;
|
|
29
29
|
}
|
|
30
30
|
export interface CompleteResponse {
|
|
31
|
+
id: string;
|
|
32
|
+
object: 'chat.completion';
|
|
33
|
+
created: number;
|
|
34
|
+
model: string;
|
|
35
|
+
choices: Array<{
|
|
36
|
+
index: number;
|
|
37
|
+
message: {
|
|
38
|
+
role: 'assistant';
|
|
39
|
+
content: string | null;
|
|
40
|
+
tool_calls?: Array<Record<string, any>>;
|
|
41
|
+
};
|
|
42
|
+
finish_reason: string | null;
|
|
43
|
+
}>;
|
|
44
|
+
usage: {
|
|
45
|
+
prompt_tokens: number;
|
|
46
|
+
completion_tokens: number;
|
|
47
|
+
total_tokens: number;
|
|
48
|
+
};
|
|
31
49
|
content: string;
|
|
50
|
+
decompressed_response?: string;
|
|
51
|
+
text?: string;
|
|
32
52
|
compression_stats: {
|
|
33
53
|
compression_enabled: boolean;
|
|
34
54
|
original_tokens: number;
|
|
@@ -75,6 +95,7 @@ export interface CompleteResponse {
|
|
|
75
95
|
model_hle?: number | null;
|
|
76
96
|
input_price_per_million?: number | null;
|
|
77
97
|
output_price_per_million?: number | null;
|
|
98
|
+
lightreach?: Record<string, any>;
|
|
78
99
|
}
|
|
79
100
|
export type MessageRole = 'system' | 'developer' | 'user' | 'assistant';
|
|
80
101
|
export interface Message {
|
|
@@ -152,6 +173,7 @@ export declare class PcompresslrAPIClient {
|
|
|
152
173
|
private session;
|
|
153
174
|
constructor(apiKey?: string, apiUrl?: string, timeout?: number);
|
|
154
175
|
private makeRequest;
|
|
176
|
+
private toOpenAISupersetResponse;
|
|
155
177
|
/**
|
|
156
178
|
* Create async /complete job (POST /api/v1/complete/jobs).
|
|
157
179
|
*/
|
package/dist/api-client.js
CHANGED
|
@@ -141,6 +141,65 @@ class PcompresslrAPIClient {
|
|
|
141
141
|
throw new APIRequestError(`Request failed: ${errorMessage}`);
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
|
+
toOpenAISupersetResponse(raw) {
|
|
145
|
+
const response = (raw && typeof raw === 'object') ? { ...raw } : {};
|
|
146
|
+
const llmStats = (response.llm_stats && typeof response.llm_stats === 'object') ? response.llm_stats : {};
|
|
147
|
+
const routingInfo = (response.routing_info && typeof response.routing_info === 'object') ? response.routing_info : {};
|
|
148
|
+
const content = (typeof response.content === 'string' && response.content) ||
|
|
149
|
+
(typeof response.decompressed_response === 'string' && response.decompressed_response) ||
|
|
150
|
+
(typeof response.text === 'string' && response.text) ||
|
|
151
|
+
'';
|
|
152
|
+
const model = response.model ||
|
|
153
|
+
response.model_used ||
|
|
154
|
+
routingInfo.selected_model ||
|
|
155
|
+
llmStats.model ||
|
|
156
|
+
'lightreach';
|
|
157
|
+
const promptTokens = Number(llmStats.input_tokens ?? 0) || 0;
|
|
158
|
+
const completionTokens = Number(llmStats.output_tokens ?? 0) || 0;
|
|
159
|
+
const totalTokens = Number(llmStats.total_tokens ?? (promptTokens + completionTokens)) || (promptTokens + completionTokens);
|
|
160
|
+
const finishReason = llmStats.finish_reason ?? 'stop';
|
|
161
|
+
const message = { role: 'assistant', content };
|
|
162
|
+
if (Array.isArray(response.tool_calls) && response.tool_calls.length > 0) {
|
|
163
|
+
message.tool_calls = response.tool_calls;
|
|
164
|
+
if (!content)
|
|
165
|
+
message.content = null;
|
|
166
|
+
}
|
|
167
|
+
response.id = String(response.id || `chatcmpl-${Math.random().toString(16).slice(2)}${Date.now().toString(16)}`);
|
|
168
|
+
response.object = 'chat.completion';
|
|
169
|
+
response.created = Number(response.created || Math.floor(Date.now() / 1000));
|
|
170
|
+
response.model = String(model);
|
|
171
|
+
response.choices = Array.isArray(response.choices)
|
|
172
|
+
? response.choices
|
|
173
|
+
: [{ index: 0, message, finish_reason: finishReason }];
|
|
174
|
+
response.usage = (response.usage && typeof response.usage === 'object')
|
|
175
|
+
? response.usage
|
|
176
|
+
: {
|
|
177
|
+
prompt_tokens: promptTokens,
|
|
178
|
+
completion_tokens: completionTokens,
|
|
179
|
+
total_tokens: totalTokens,
|
|
180
|
+
};
|
|
181
|
+
response.content = content;
|
|
182
|
+
if (response.decompressed_response === undefined)
|
|
183
|
+
response.decompressed_response = content;
|
|
184
|
+
if (response.text === undefined)
|
|
185
|
+
response.text = content;
|
|
186
|
+
response.lightreach = {
|
|
187
|
+
content,
|
|
188
|
+
compression_stats: response.compression_stats,
|
|
189
|
+
llm_stats: response.llm_stats,
|
|
190
|
+
routing_info: response.routing_info,
|
|
191
|
+
warnings: response.warnings,
|
|
192
|
+
tokens_saved: response.tokens_saved,
|
|
193
|
+
tokens_used: response.tokens_used,
|
|
194
|
+
compression_ratio: response.compression_ratio,
|
|
195
|
+
cost_estimate: response.cost_estimate,
|
|
196
|
+
savings_estimate: response.savings_estimate,
|
|
197
|
+
provider_used: response.provider_used,
|
|
198
|
+
model_used: response.model_used,
|
|
199
|
+
latency_ms: llmStats.latency_ms ?? null,
|
|
200
|
+
};
|
|
201
|
+
return response;
|
|
202
|
+
}
|
|
144
203
|
/**
|
|
145
204
|
* Create async /complete job (POST /api/v1/complete/jobs).
|
|
146
205
|
*/
|
|
@@ -193,7 +252,7 @@ class PcompresslrAPIClient {
|
|
|
193
252
|
const st = await this.getCompleteJob(jobId);
|
|
194
253
|
if (st.status === 'succeeded') {
|
|
195
254
|
if (st.result)
|
|
196
|
-
return st.result;
|
|
255
|
+
return this.toOpenAISupersetResponse(st.result);
|
|
197
256
|
throw new APIRequestError('Async job succeeded but result was missing.');
|
|
198
257
|
}
|
|
199
258
|
if (st.status === 'failed' || st.status === 'canceled') {
|
|
@@ -285,7 +344,8 @@ class PcompresslrAPIClient {
|
|
|
285
344
|
data.auto_select_by_hle = request.auto_select_by_hle;
|
|
286
345
|
if (request.same_provider_only !== undefined)
|
|
287
346
|
data.same_provider_only = request.same_provider_only;
|
|
288
|
-
|
|
347
|
+
const raw = await this.makeRequest('/api/v2/complete', data, 'POST');
|
|
348
|
+
return this.toOpenAISupersetResponse(raw);
|
|
289
349
|
}
|
|
290
350
|
/**
|
|
291
351
|
* Messages-first complete with intelligent model selection.
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Compress Light Reach -
|
|
3
|
+
* Compress Light Reach - OpenAI-compatible routing + compression SDK.
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.PcompresslrAPIError = exports.APIRequestError = exports.RateLimitError = exports.APIKeyError = exports.PcompresslrAPIClient = exports.Pcompresslr = exports.LightReach = exports.__version__ = void 0;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compress-lightreach",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.9",
|
|
4
|
+
"description": "OpenAI-compatible LLM routing and compression SDK with LightReach metadata extensions",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"bin": {
|