sona-code 0.1.0
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 +119 -0
- package/dist/cli.d.ts +9 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +2979 -0
- package/dist/cli.js.map +1 -0
- package/dist/compressor.d.ts +84 -0
- package/dist/compressor.d.ts.map +1 -0
- package/dist/compressor.js +237 -0
- package/dist/compressor.js.map +1 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +79 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +81 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +165 -0
- package/dist/middleware.js.map +1 -0
- package/dist/proxy.d.ts +105 -0
- package/dist/proxy.d.ts.map +1 -0
- package/dist/proxy.js +548 -0
- package/dist/proxy.js.map +1 -0
- package/dist/rules.d.ts +30 -0
- package/dist/rules.d.ts.map +1 -0
- package/dist/rules.js +201 -0
- package/dist/rules.js.map +1 -0
- package/dist/session.d.ts +156 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +351 -0
- package/dist/session.js.map +1 -0
- package/package.json +51 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SONA CODE - AI Coding Agent
|
|
3
|
+
*
|
|
4
|
+
* Fast, capable, cost-effective terminal assistant.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* // Simple compression
|
|
8
|
+
* import { compress, compressText } from 'sona-code';
|
|
9
|
+
*
|
|
10
|
+
* const result = compress('It is important to note that this is verbose text.');
|
|
11
|
+
* console.log(result.compressedText); // "This is verbose text."
|
|
12
|
+
* console.log(result.tokenSavings); // ~6 tokens saved
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Middleware for OpenAI
|
|
16
|
+
* import { createMiddleware } from 'sona-code';
|
|
17
|
+
* import OpenAI from 'openai';
|
|
18
|
+
*
|
|
19
|
+
* const middleware = createMiddleware();
|
|
20
|
+
* const openai = middleware.wrapOpenAI(new OpenAI());
|
|
21
|
+
*
|
|
22
|
+
* // All calls now automatically compress prompts
|
|
23
|
+
* const response = await openai.chat.completions.create({
|
|
24
|
+
* model: 'gpt-4',
|
|
25
|
+
* messages: [{ role: 'user', content: 'Your verbose prompt...' }],
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* console.log(middleware.getStats()); // See total savings
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // Middleware for Anthropic
|
|
32
|
+
* import { createMiddleware } from 'sona-code';
|
|
33
|
+
* import Anthropic from '@anthropic-ai/sdk';
|
|
34
|
+
*
|
|
35
|
+
* const middleware = createMiddleware();
|
|
36
|
+
* const anthropic = middleware.wrapAnthropic(new Anthropic());
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // Compress messages directly
|
|
40
|
+
* import { compressMessages } from 'sona-code';
|
|
41
|
+
*
|
|
42
|
+
* const messages = [
|
|
43
|
+
* { role: 'system', content: 'You are a helpful assistant...' },
|
|
44
|
+
* { role: 'user', content: 'It is important to note that...' },
|
|
45
|
+
* ];
|
|
46
|
+
*
|
|
47
|
+
* const compressed = compressMessages(messages);
|
|
48
|
+
*/
|
|
49
|
+
export { SonaCompressor, compress, compressText, compressMessages, type CompressionResult, type CompressorOptions, type Message, } from './compressor.js';
|
|
50
|
+
export { SonaMiddleware, createMiddleware, type MiddlewareStats, } from './middleware.js';
|
|
51
|
+
export { FILLER_RULES, ABBREVIATION_RULES, getAllRules, getRuleCount, type CompressionRule, } from './rules.js';
|
|
52
|
+
export { SonaProxy, startProxy, type ProxyStats, type ProxyOptions, type RequestLogEntry, } from './proxy.js';
|
|
53
|
+
export { SonaSession, createSession, type SessionConfig, type SessionStats, type DocumentStats, type StreamChunk, } from './session.js';
|
|
54
|
+
export { SonaCompressor as default } from './compressor.js';
|
|
55
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAGH,OAAO,EACL,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,gBAAgB,EAChB,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,EACtB,KAAK,OAAO,GACb,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,KAAK,eAAe,GACrB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,WAAW,EACX,YAAY,EACZ,KAAK,eAAe,GACrB,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,SAAS,EACT,UAAU,EACV,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,eAAe,GACrB,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,WAAW,EACX,aAAa,EACb,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,WAAW,GACjB,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,cAAc,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* SONA CODE - AI Coding Agent
|
|
4
|
+
*
|
|
5
|
+
* Fast, capable, cost-effective terminal assistant.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Simple compression
|
|
9
|
+
* import { compress, compressText } from 'sona-code';
|
|
10
|
+
*
|
|
11
|
+
* const result = compress('It is important to note that this is verbose text.');
|
|
12
|
+
* console.log(result.compressedText); // "This is verbose text."
|
|
13
|
+
* console.log(result.tokenSavings); // ~6 tokens saved
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Middleware for OpenAI
|
|
17
|
+
* import { createMiddleware } from 'sona-code';
|
|
18
|
+
* import OpenAI from 'openai';
|
|
19
|
+
*
|
|
20
|
+
* const middleware = createMiddleware();
|
|
21
|
+
* const openai = middleware.wrapOpenAI(new OpenAI());
|
|
22
|
+
*
|
|
23
|
+
* // All calls now automatically compress prompts
|
|
24
|
+
* const response = await openai.chat.completions.create({
|
|
25
|
+
* model: 'gpt-4',
|
|
26
|
+
* messages: [{ role: 'user', content: 'Your verbose prompt...' }],
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* console.log(middleware.getStats()); // See total savings
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // Middleware for Anthropic
|
|
33
|
+
* import { createMiddleware } from 'sona-code';
|
|
34
|
+
* import Anthropic from '@anthropic-ai/sdk';
|
|
35
|
+
*
|
|
36
|
+
* const middleware = createMiddleware();
|
|
37
|
+
* const anthropic = middleware.wrapAnthropic(new Anthropic());
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // Compress messages directly
|
|
41
|
+
* import { compressMessages } from 'sona-code';
|
|
42
|
+
*
|
|
43
|
+
* const messages = [
|
|
44
|
+
* { role: 'system', content: 'You are a helpful assistant...' },
|
|
45
|
+
* { role: 'user', content: 'It is important to note that...' },
|
|
46
|
+
* ];
|
|
47
|
+
*
|
|
48
|
+
* const compressed = compressMessages(messages);
|
|
49
|
+
*/
|
|
50
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
+
exports.default = exports.createSession = exports.SonaSession = exports.startProxy = exports.SonaProxy = exports.getRuleCount = exports.getAllRules = exports.ABBREVIATION_RULES = exports.FILLER_RULES = exports.createMiddleware = exports.SonaMiddleware = exports.compressMessages = exports.compressText = exports.compress = exports.SonaCompressor = void 0;
|
|
52
|
+
// Core exports
|
|
53
|
+
var compressor_js_1 = require("./compressor.js");
|
|
54
|
+
Object.defineProperty(exports, "SonaCompressor", { enumerable: true, get: function () { return compressor_js_1.SonaCompressor; } });
|
|
55
|
+
Object.defineProperty(exports, "compress", { enumerable: true, get: function () { return compressor_js_1.compress; } });
|
|
56
|
+
Object.defineProperty(exports, "compressText", { enumerable: true, get: function () { return compressor_js_1.compressText; } });
|
|
57
|
+
Object.defineProperty(exports, "compressMessages", { enumerable: true, get: function () { return compressor_js_1.compressMessages; } });
|
|
58
|
+
// Middleware exports
|
|
59
|
+
var middleware_js_1 = require("./middleware.js");
|
|
60
|
+
Object.defineProperty(exports, "SonaMiddleware", { enumerable: true, get: function () { return middleware_js_1.SonaMiddleware; } });
|
|
61
|
+
Object.defineProperty(exports, "createMiddleware", { enumerable: true, get: function () { return middleware_js_1.createMiddleware; } });
|
|
62
|
+
// Rules exports (for advanced usage)
|
|
63
|
+
var rules_js_1 = require("./rules.js");
|
|
64
|
+
Object.defineProperty(exports, "FILLER_RULES", { enumerable: true, get: function () { return rules_js_1.FILLER_RULES; } });
|
|
65
|
+
Object.defineProperty(exports, "ABBREVIATION_RULES", { enumerable: true, get: function () { return rules_js_1.ABBREVIATION_RULES; } });
|
|
66
|
+
Object.defineProperty(exports, "getAllRules", { enumerable: true, get: function () { return rules_js_1.getAllRules; } });
|
|
67
|
+
Object.defineProperty(exports, "getRuleCount", { enumerable: true, get: function () { return rules_js_1.getRuleCount; } });
|
|
68
|
+
// Proxy exports
|
|
69
|
+
var proxy_js_1 = require("./proxy.js");
|
|
70
|
+
Object.defineProperty(exports, "SonaProxy", { enumerable: true, get: function () { return proxy_js_1.SonaProxy; } });
|
|
71
|
+
Object.defineProperty(exports, "startProxy", { enumerable: true, get: function () { return proxy_js_1.startProxy; } });
|
|
72
|
+
// Session exports (for continuous document processing)
|
|
73
|
+
var session_js_1 = require("./session.js");
|
|
74
|
+
Object.defineProperty(exports, "SonaSession", { enumerable: true, get: function () { return session_js_1.SonaSession; } });
|
|
75
|
+
Object.defineProperty(exports, "createSession", { enumerable: true, get: function () { return session_js_1.createSession; } });
|
|
76
|
+
// Default export for convenience
|
|
77
|
+
var compressor_js_2 = require("./compressor.js");
|
|
78
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return compressor_js_2.SonaCompressor; } });
|
|
79
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;;;AAEH,eAAe;AACf,iDAQyB;AAPvB,+GAAA,cAAc,OAAA;AACd,yGAAA,QAAQ,OAAA;AACR,6GAAA,YAAY,OAAA;AACZ,iHAAA,gBAAgB,OAAA;AAMlB,qBAAqB;AACrB,iDAIyB;AAHvB,+GAAA,cAAc,OAAA;AACd,iHAAA,gBAAgB,OAAA;AAIlB,qCAAqC;AACrC,uCAMoB;AALlB,wGAAA,YAAY,OAAA;AACZ,8GAAA,kBAAkB,OAAA;AAClB,uGAAA,WAAW,OAAA;AACX,wGAAA,YAAY,OAAA;AAId,gBAAgB;AAChB,uCAMoB;AALlB,qGAAA,SAAS,OAAA;AACT,sGAAA,UAAU,OAAA;AAMZ,uDAAuD;AACvD,2CAOsB;AANpB,yGAAA,WAAW,OAAA;AACX,2GAAA,aAAa,OAAA;AAOf,iCAAiC;AACjC,iDAA4D;AAAnD,wGAAA,cAAc,OAAW"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SONA Middleware
|
|
3
|
+
*
|
|
4
|
+
* Drop-in middleware for OpenAI and Anthropic SDKs.
|
|
5
|
+
* Automatically compresses prompts before sending to API.
|
|
6
|
+
*/
|
|
7
|
+
import { SonaCompressor, CompressorOptions, Message } from './compressor.js';
|
|
8
|
+
export interface MiddlewareStats {
|
|
9
|
+
totalRequests: number;
|
|
10
|
+
totalOriginalTokens: number;
|
|
11
|
+
totalCompressedTokens: number;
|
|
12
|
+
totalTokensSaved: number;
|
|
13
|
+
totalCostSaved: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* SONA Middleware for automatic compression
|
|
17
|
+
*/
|
|
18
|
+
export declare class SonaMiddleware {
|
|
19
|
+
private compressor;
|
|
20
|
+
private stats;
|
|
21
|
+
private enabled;
|
|
22
|
+
constructor(options?: CompressorOptions);
|
|
23
|
+
/**
|
|
24
|
+
* Enable/disable compression
|
|
25
|
+
*/
|
|
26
|
+
setEnabled(enabled: boolean): void;
|
|
27
|
+
/**
|
|
28
|
+
* Check if compression is enabled
|
|
29
|
+
*/
|
|
30
|
+
isEnabled(): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Get compression statistics
|
|
33
|
+
*/
|
|
34
|
+
getStats(): MiddlewareStats;
|
|
35
|
+
/**
|
|
36
|
+
* Reset statistics
|
|
37
|
+
*/
|
|
38
|
+
resetStats(): void;
|
|
39
|
+
/**
|
|
40
|
+
* Wrap OpenAI client to automatically compress messages
|
|
41
|
+
*/
|
|
42
|
+
wrapOpenAI<T extends {
|
|
43
|
+
chat: {
|
|
44
|
+
completions: {
|
|
45
|
+
create: Function;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
}>(client: T): T;
|
|
49
|
+
/**
|
|
50
|
+
* Wrap Anthropic client to automatically compress messages
|
|
51
|
+
*/
|
|
52
|
+
wrapAnthropic<T extends {
|
|
53
|
+
messages: {
|
|
54
|
+
create: Function;
|
|
55
|
+
};
|
|
56
|
+
}>(client: T): T;
|
|
57
|
+
/**
|
|
58
|
+
* Process a single text string
|
|
59
|
+
*/
|
|
60
|
+
processText(text: string): {
|
|
61
|
+
compressed: string;
|
|
62
|
+
stats: {
|
|
63
|
+
originalTokens: number;
|
|
64
|
+
compressedTokens: number;
|
|
65
|
+
saved: number;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Process messages array
|
|
70
|
+
*/
|
|
71
|
+
processMessages(messages: Message[]): Message[];
|
|
72
|
+
/**
|
|
73
|
+
* Get the underlying compressor
|
|
74
|
+
*/
|
|
75
|
+
getCompressor(): SonaCompressor;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create a middleware instance
|
|
79
|
+
*/
|
|
80
|
+
export declare function createMiddleware(options?: CompressorOptions): SonaMiddleware;
|
|
81
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE7E,MAAM,WAAW,eAAe;IAC9B,aAAa,EAAE,MAAM,CAAC;IACtB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,gBAAgB,EAAE,MAAM,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,UAAU,CAAiB;IACnC,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,OAAO,CAAU;gBAEb,OAAO,GAAE,iBAAsB;IAY3C;;OAEG;IACH,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAIlC;;OAEG;IACH,SAAS,IAAI,OAAO;IAIpB;;OAEG;IACH,QAAQ,IAAI,eAAe;IAI3B;;OAEG;IACH,UAAU,IAAI,IAAI;IAUlB;;OAEG;IACH,UAAU,CAAC,CAAC,SAAS;QAAE,IAAI,EAAE;YAAE,WAAW,EAAE;gBAAE,MAAM,EAAE,QAAQ,CAAA;aAAE,CAAA;SAAE,CAAA;KAAE,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC;IA6CnF;;OAEG;IACH,aAAa,CAAC,CAAC,SAAS;QAAE,QAAQ,EAAE;YAAE,MAAM,EAAE,QAAQ,CAAA;SAAE,CAAA;KAAE,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC;IAqCzE;;OAEG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE;YAAE,cAAc,EAAE,MAAM,CAAC;YAAC,gBAAgB,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE;IAmB7H;;OAEG;IACH,eAAe,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE;IAI/C;;OAEG;IACH,aAAa,IAAI,cAAc;CAGhC;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,CAAC,EAAE,iBAAiB,GAAG,cAAc,CAE5E"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* SONA Middleware
|
|
4
|
+
*
|
|
5
|
+
* Drop-in middleware for OpenAI and Anthropic SDKs.
|
|
6
|
+
* Automatically compresses prompts before sending to API.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.SonaMiddleware = void 0;
|
|
10
|
+
exports.createMiddleware = createMiddleware;
|
|
11
|
+
const compressor_js_1 = require("./compressor.js");
|
|
12
|
+
/**
|
|
13
|
+
* SONA Middleware for automatic compression
|
|
14
|
+
*/
|
|
15
|
+
class SonaMiddleware {
|
|
16
|
+
compressor;
|
|
17
|
+
stats;
|
|
18
|
+
enabled;
|
|
19
|
+
constructor(options = {}) {
|
|
20
|
+
this.compressor = new compressor_js_1.SonaCompressor(options);
|
|
21
|
+
this.enabled = true;
|
|
22
|
+
this.stats = {
|
|
23
|
+
totalRequests: 0,
|
|
24
|
+
totalOriginalTokens: 0,
|
|
25
|
+
totalCompressedTokens: 0,
|
|
26
|
+
totalTokensSaved: 0,
|
|
27
|
+
totalCostSaved: 0,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Enable/disable compression
|
|
32
|
+
*/
|
|
33
|
+
setEnabled(enabled) {
|
|
34
|
+
this.enabled = enabled;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Check if compression is enabled
|
|
38
|
+
*/
|
|
39
|
+
isEnabled() {
|
|
40
|
+
return this.enabled;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get compression statistics
|
|
44
|
+
*/
|
|
45
|
+
getStats() {
|
|
46
|
+
return { ...this.stats };
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Reset statistics
|
|
50
|
+
*/
|
|
51
|
+
resetStats() {
|
|
52
|
+
this.stats = {
|
|
53
|
+
totalRequests: 0,
|
|
54
|
+
totalOriginalTokens: 0,
|
|
55
|
+
totalCompressedTokens: 0,
|
|
56
|
+
totalTokensSaved: 0,
|
|
57
|
+
totalCostSaved: 0,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Wrap OpenAI client to automatically compress messages
|
|
62
|
+
*/
|
|
63
|
+
wrapOpenAI(client) {
|
|
64
|
+
const originalCreate = client.chat.completions.create.bind(client.chat.completions);
|
|
65
|
+
const self = this;
|
|
66
|
+
client.chat.completions.create = async function (params) {
|
|
67
|
+
if (!self.enabled || !params.messages) {
|
|
68
|
+
return originalCreate(params);
|
|
69
|
+
}
|
|
70
|
+
// Compress messages
|
|
71
|
+
const originalMessages = params.messages;
|
|
72
|
+
const compressedMessages = self.compressor.compressMessages(originalMessages);
|
|
73
|
+
// Track stats
|
|
74
|
+
self.stats.totalRequests++;
|
|
75
|
+
// Calculate token savings
|
|
76
|
+
let originalTokens = 0;
|
|
77
|
+
let compressedTokens = 0;
|
|
78
|
+
originalMessages.forEach((msg, i) => {
|
|
79
|
+
if (typeof msg.content === 'string') {
|
|
80
|
+
const result = self.compressor.compress(msg.content);
|
|
81
|
+
originalTokens += result.originalTokens;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
compressedMessages.forEach(msg => {
|
|
85
|
+
if (typeof msg.content === 'string') {
|
|
86
|
+
const result = self.compressor.compress(msg.content);
|
|
87
|
+
compressedTokens += result.compressedTokens;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
self.stats.totalOriginalTokens += originalTokens;
|
|
91
|
+
self.stats.totalCompressedTokens += compressedTokens;
|
|
92
|
+
self.stats.totalTokensSaved += (originalTokens - compressedTokens);
|
|
93
|
+
// Call original with compressed messages
|
|
94
|
+
return originalCreate({ ...params, messages: compressedMessages });
|
|
95
|
+
};
|
|
96
|
+
return client;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Wrap Anthropic client to automatically compress messages
|
|
100
|
+
*/
|
|
101
|
+
wrapAnthropic(client) {
|
|
102
|
+
const originalCreate = client.messages.create.bind(client.messages);
|
|
103
|
+
const self = this;
|
|
104
|
+
client.messages.create = async function (params) {
|
|
105
|
+
if (!self.enabled) {
|
|
106
|
+
return originalCreate(params);
|
|
107
|
+
}
|
|
108
|
+
const newParams = { ...params };
|
|
109
|
+
// Compress system prompt
|
|
110
|
+
if (params.system && typeof params.system === 'string') {
|
|
111
|
+
const result = self.compressor.compress(params.system);
|
|
112
|
+
newParams.system = result.compressedText;
|
|
113
|
+
self.stats.totalOriginalTokens += result.originalTokens;
|
|
114
|
+
self.stats.totalCompressedTokens += result.compressedTokens;
|
|
115
|
+
self.stats.totalTokensSaved += result.tokenSavings;
|
|
116
|
+
}
|
|
117
|
+
// Compress messages
|
|
118
|
+
if (params.messages) {
|
|
119
|
+
newParams.messages = self.compressor.compressMessages(params.messages);
|
|
120
|
+
}
|
|
121
|
+
self.stats.totalRequests++;
|
|
122
|
+
return originalCreate(newParams);
|
|
123
|
+
};
|
|
124
|
+
return client;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Process a single text string
|
|
128
|
+
*/
|
|
129
|
+
processText(text) {
|
|
130
|
+
const result = this.compressor.compress(text);
|
|
131
|
+
this.stats.totalRequests++;
|
|
132
|
+
this.stats.totalOriginalTokens += result.originalTokens;
|
|
133
|
+
this.stats.totalCompressedTokens += result.compressedTokens;
|
|
134
|
+
this.stats.totalTokensSaved += result.tokenSavings;
|
|
135
|
+
this.stats.totalCostSaved += result.estimatedCostSavings;
|
|
136
|
+
return {
|
|
137
|
+
compressed: result.compressedText,
|
|
138
|
+
stats: {
|
|
139
|
+
originalTokens: result.originalTokens,
|
|
140
|
+
compressedTokens: result.compressedTokens,
|
|
141
|
+
saved: result.tokenSavings,
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Process messages array
|
|
147
|
+
*/
|
|
148
|
+
processMessages(messages) {
|
|
149
|
+
return this.compressor.compressMessages(messages);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get the underlying compressor
|
|
153
|
+
*/
|
|
154
|
+
getCompressor() {
|
|
155
|
+
return this.compressor;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
exports.SonaMiddleware = SonaMiddleware;
|
|
159
|
+
/**
|
|
160
|
+
* Create a middleware instance
|
|
161
|
+
*/
|
|
162
|
+
function createMiddleware(options) {
|
|
163
|
+
return new SonaMiddleware(options);
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAkMH,4CAEC;AAlMD,mDAA6E;AAU7E;;GAEG;AACH,MAAa,cAAc;IACjB,UAAU,CAAiB;IAC3B,KAAK,CAAkB;IACvB,OAAO,CAAU;IAEzB,YAAY,UAA6B,EAAE;QACzC,IAAI,CAAC,UAAU,GAAG,IAAI,8BAAc,CAAC,OAAO,CAAC,CAAC;QAC9C,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,KAAK,GAAG;YAC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AC,KAAK,CAAC,qBAAqB,IAAI,MAAM,CAAC,gBAAgB,CAAC;QAC5D,IAAI,CAAC,KAAK,CAAC,gBAAgB,IAAI,MAAM,CAAC,YAAY,CAAC;QACnD,IAAI,CAAC,KAAK,CAAC,cAAc,IAAI,MAAM,CAAC,oBAAoB,CAAC;QAEzD,OAAO;YACL,UAAU,EAAE,MAAM,CAAC,cAAc;YACjC,KAAK,EAAE;gBACL,cAAc,EAAE,MAAM,CAAC,cAAc;gBACrC,gBAAgB,EAAE,MAAM,CAAC,gBAAgB;gBACzC,KAAK,EAAE,MAAM,CAAC,YAAY;aAC3B;SACF,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,eAAe,CAAC,QAAmB;QACjC,OAAO,IAAI,CAAC,UAAU,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC;IACpD,CAAC;IAED;;OAEG;IACH,aAAa;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;CACF;AA9KD,wCA8KC;AAED;;GAEG;AACH,SAAgB,gBAAgB,CAAC,OAA2B;IAC1D,OAAO,IAAI,cAAc,CAAC,OAAO,CAAC,CAAC;AACrC,CAAC"}
|
package/dist/proxy.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SONA Transparent Proxy Server
|
|
3
|
+
*
|
|
4
|
+
* Intercepts OpenAI/Anthropic API calls, compresses prompts,
|
|
5
|
+
* and forwards to the real API. Works with ANY tool that uses
|
|
6
|
+
* these SDKs without code changes.
|
|
7
|
+
*/
|
|
8
|
+
export interface ProxyStats {
|
|
9
|
+
startedAt: Date;
|
|
10
|
+
totalRequests: number;
|
|
11
|
+
openaiRequests: number;
|
|
12
|
+
anthropicRequests: number;
|
|
13
|
+
totalOriginalTokens: number;
|
|
14
|
+
totalCompressedTokens: number;
|
|
15
|
+
totalTokensSaved: number;
|
|
16
|
+
estimatedCostSaved: number;
|
|
17
|
+
requestLog: RequestLogEntry[];
|
|
18
|
+
}
|
|
19
|
+
export interface RequestLogEntry {
|
|
20
|
+
timestamp: Date;
|
|
21
|
+
provider: 'openai' | 'anthropic' | 'unknown';
|
|
22
|
+
endpoint: string;
|
|
23
|
+
originalTokens: number;
|
|
24
|
+
compressedTokens: number;
|
|
25
|
+
tokensSaved: number;
|
|
26
|
+
latencyMs: number;
|
|
27
|
+
}
|
|
28
|
+
export interface ProxyOptions {
|
|
29
|
+
port?: number;
|
|
30
|
+
openaiBaseUrl?: string;
|
|
31
|
+
anthropicBaseUrl?: string;
|
|
32
|
+
openaiApiKey?: string;
|
|
33
|
+
anthropicApiKey?: string;
|
|
34
|
+
pricePerMillionTokens?: number;
|
|
35
|
+
maxLogEntries?: number;
|
|
36
|
+
verbose?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* SONA Proxy Server
|
|
40
|
+
*/
|
|
41
|
+
export declare class SonaProxy {
|
|
42
|
+
private server;
|
|
43
|
+
private compressor;
|
|
44
|
+
private options;
|
|
45
|
+
private stats;
|
|
46
|
+
constructor(options?: ProxyOptions);
|
|
47
|
+
private initStats;
|
|
48
|
+
/**
|
|
49
|
+
* Start the proxy server
|
|
50
|
+
*/
|
|
51
|
+
start(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Stop the proxy server
|
|
54
|
+
*/
|
|
55
|
+
stop(): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Get current statistics
|
|
58
|
+
*/
|
|
59
|
+
getStats(): ProxyStats;
|
|
60
|
+
/**
|
|
61
|
+
* Reset statistics
|
|
62
|
+
*/
|
|
63
|
+
resetStats(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Handle incoming request
|
|
66
|
+
*/
|
|
67
|
+
private handleRequest;
|
|
68
|
+
/**
|
|
69
|
+
* Detect which provider the request is for
|
|
70
|
+
*/
|
|
71
|
+
private detectProvider;
|
|
72
|
+
/**
|
|
73
|
+
* Read request body
|
|
74
|
+
*/
|
|
75
|
+
private readBody;
|
|
76
|
+
/**
|
|
77
|
+
* Compress request body based on provider
|
|
78
|
+
*/
|
|
79
|
+
private compressRequestBody;
|
|
80
|
+
/**
|
|
81
|
+
* Forward request to target API
|
|
82
|
+
*/
|
|
83
|
+
private forwardRequest;
|
|
84
|
+
/**
|
|
85
|
+
* Log request to stats
|
|
86
|
+
*/
|
|
87
|
+
private logRequest;
|
|
88
|
+
/**
|
|
89
|
+
* Handle stats API request
|
|
90
|
+
*/
|
|
91
|
+
private handleStatsRequest;
|
|
92
|
+
/**
|
|
93
|
+
* Handle dashboard request
|
|
94
|
+
*/
|
|
95
|
+
private handleDashboard;
|
|
96
|
+
/**
|
|
97
|
+
* Format uptime duration
|
|
98
|
+
*/
|
|
99
|
+
private formatUptime;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Start a proxy server
|
|
103
|
+
*/
|
|
104
|
+
export declare function startProxy(options?: ProxyOptions): Promise<SonaProxy>;
|
|
105
|
+
//# sourceMappingURL=proxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAOH,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,IAAI,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,mBAAmB,EAAE,MAAM,CAAC;IAC5B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,gBAAgB,EAAE,MAAM,CAAC;IACzB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,UAAU,EAAE,eAAe,EAAE,CAAC;CAC/B;AAED,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,IAAI,CAAC;IAChB,QAAQ,EAAE,QAAQ,GAAG,WAAW,GAAG,SAAS,CAAC;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CA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