@ordis-dev/ordis 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/LICENSE +21 -0
- package/README.md +192 -0
- package/dist/__tests__/api.test.d.ts +5 -0
- package/dist/__tests__/api.test.d.ts.map +1 -0
- package/dist/__tests__/api.test.js +95 -0
- package/dist/__tests__/api.test.js.map +1 -0
- package/dist/__tests__/cli.test.d.ts +6 -0
- package/dist/__tests__/cli.test.d.ts.map +1 -0
- package/dist/__tests__/cli.test.js +103 -0
- package/dist/__tests__/cli.test.js.map +1 -0
- package/dist/cli/__tests__/cli.test.d.ts +5 -0
- package/dist/cli/__tests__/cli.test.d.ts.map +1 -0
- package/dist/cli/__tests__/cli.test.js +13 -0
- package/dist/cli/__tests__/cli.test.js.map +1 -0
- package/dist/cli.d.ts +7 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +225 -0
- package/dist/cli.js.map +1 -0
- package/dist/core/__tests__/pipeline.test.d.ts +5 -0
- package/dist/core/__tests__/pipeline.test.d.ts.map +1 -0
- package/dist/core/__tests__/pipeline.test.js +334 -0
- package/dist/core/__tests__/pipeline.test.js.map +1 -0
- package/dist/core/__tests__/validator.test.d.ts +5 -0
- package/dist/core/__tests__/validator.test.d.ts.map +1 -0
- package/dist/core/__tests__/validator.test.js +124 -0
- package/dist/core/__tests__/validator.test.js.map +1 -0
- package/dist/core/errors.d.ts +27 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +35 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +7 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/pipeline.d.ts +28 -0
- package/dist/core/pipeline.d.ts.map +1 -0
- package/dist/core/pipeline.js +212 -0
- package/dist/core/pipeline.js.map +1 -0
- package/dist/core/types.d.ts +55 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +5 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/validator.d.ts +19 -0
- package/dist/core/validator.d.ts.map +1 -0
- package/dist/core/validator.js +147 -0
- package/dist/core/validator.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/__tests__/client.test.d.ts +5 -0
- package/dist/llm/__tests__/client.test.d.ts.map +1 -0
- package/dist/llm/__tests__/client.test.js +350 -0
- package/dist/llm/__tests__/client.test.js.map +1 -0
- package/dist/llm/__tests__/prompt-builder.test.d.ts +5 -0
- package/dist/llm/__tests__/prompt-builder.test.d.ts.map +1 -0
- package/dist/llm/__tests__/prompt-builder.test.js +171 -0
- package/dist/llm/__tests__/prompt-builder.test.js.map +1 -0
- package/dist/llm/__tests__/retry.test.d.ts +5 -0
- package/dist/llm/__tests__/retry.test.d.ts.map +1 -0
- package/dist/llm/__tests__/retry.test.js +350 -0
- package/dist/llm/__tests__/retry.test.js.map +1 -0
- package/dist/llm/__tests__/token-counter.test.d.ts +5 -0
- package/dist/llm/__tests__/token-counter.test.d.ts.map +1 -0
- package/dist/llm/__tests__/token-counter.test.js +166 -0
- package/dist/llm/__tests__/token-counter.test.js.map +1 -0
- package/dist/llm/client.d.ts +56 -0
- package/dist/llm/client.d.ts.map +1 -0
- package/dist/llm/client.js +266 -0
- package/dist/llm/client.js.map +1 -0
- package/dist/llm/errors.d.ts +27 -0
- package/dist/llm/errors.d.ts.map +1 -0
- package/dist/llm/errors.js +35 -0
- package/dist/llm/errors.js.map +1 -0
- package/dist/llm/index.d.ts +10 -0
- package/dist/llm/index.d.ts.map +1 -0
- package/dist/llm/index.js +8 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/prompt-builder.d.ts +13 -0
- package/dist/llm/prompt-builder.d.ts.map +1 -0
- package/dist/llm/prompt-builder.js +107 -0
- package/dist/llm/prompt-builder.js.map +1 -0
- package/dist/llm/token-counter.d.ts +82 -0
- package/dist/llm/token-counter.d.ts.map +1 -0
- package/dist/llm/token-counter.js +109 -0
- package/dist/llm/token-counter.js.map +1 -0
- package/dist/llm/types.d.ts +96 -0
- package/dist/llm/types.d.ts.map +1 -0
- package/dist/llm/types.js +5 -0
- package/dist/llm/types.js.map +1 -0
- package/dist/schemas/__tests__/integration.test.d.ts +5 -0
- package/dist/schemas/__tests__/integration.test.d.ts.map +1 -0
- package/dist/schemas/__tests__/integration.test.js +366 -0
- package/dist/schemas/__tests__/integration.test.js.map +1 -0
- package/dist/schemas/__tests__/loader.test.d.ts +5 -0
- package/dist/schemas/__tests__/loader.test.d.ts.map +1 -0
- package/dist/schemas/__tests__/loader.test.js +271 -0
- package/dist/schemas/__tests__/loader.test.js.map +1 -0
- package/dist/schemas/__tests__/validator.test.d.ts +5 -0
- package/dist/schemas/__tests__/validator.test.d.ts.map +1 -0
- package/dist/schemas/__tests__/validator.test.js +592 -0
- package/dist/schemas/__tests__/validator.test.js.map +1 -0
- package/dist/schemas/errors.d.ts +38 -0
- package/dist/schemas/errors.d.ts.map +1 -0
- package/dist/schemas/errors.js +49 -0
- package/dist/schemas/errors.js.map +1 -0
- package/dist/schemas/index.d.ts +8 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +7 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/loader.d.ts +29 -0
- package/dist/schemas/loader.d.ts.map +1 -0
- package/dist/schemas/loader.js +71 -0
- package/dist/schemas/loader.js.map +1 -0
- package/dist/schemas/types.d.ts +75 -0
- package/dist/schemas/types.d.ts.map +1 -0
- package/dist/schemas/types.js +7 -0
- package/dist/schemas/types.js.map +1 -0
- package/dist/schemas/validator.d.ts +12 -0
- package/dist/schemas/validator.d.ts.map +1 -0
- package/dist/schemas/validator.js +211 -0
- package/dist/schemas/validator.js.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token counter tests
|
|
3
|
+
*/
|
|
4
|
+
import { describe, test, expect } from 'vitest';
|
|
5
|
+
import { estimateTokens, TokenCounter } from '../token-counter.js';
|
|
6
|
+
describe('estimateTokens', () => {
|
|
7
|
+
test('returns 0 for empty string', () => {
|
|
8
|
+
expect(estimateTokens('')).toBe(0);
|
|
9
|
+
});
|
|
10
|
+
test('estimates tokens using 4 chars per token', () => {
|
|
11
|
+
// 20 characters = 5 tokens
|
|
12
|
+
expect(estimateTokens('12345678901234567890')).toBe(5);
|
|
13
|
+
});
|
|
14
|
+
test('rounds up for partial tokens', () => {
|
|
15
|
+
// 21 characters = 6 tokens (5.25 rounded up)
|
|
16
|
+
expect(estimateTokens('123456789012345678901')).toBe(6);
|
|
17
|
+
});
|
|
18
|
+
test('handles longer text', () => {
|
|
19
|
+
const text = 'The quick brown fox jumps over the lazy dog. '.repeat(10);
|
|
20
|
+
const tokens = estimateTokens(text);
|
|
21
|
+
expect(tokens).toBeGreaterThan(0);
|
|
22
|
+
// 45 chars * 10 = 450 chars = ~113 tokens
|
|
23
|
+
expect(tokens).toBeCloseTo(113, 0);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe('TokenCounter', () => {
|
|
27
|
+
describe('calculateUsage', () => {
|
|
28
|
+
test('calculates token usage correctly', () => {
|
|
29
|
+
const counter = new TokenCounter({
|
|
30
|
+
maxContextTokens: 4096,
|
|
31
|
+
tokenBudget: {
|
|
32
|
+
system: 1000,
|
|
33
|
+
input: 2000,
|
|
34
|
+
output: 1000,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
// 400 chars = 100 tokens, 800 chars = 200 tokens
|
|
38
|
+
const system = 'a'.repeat(400);
|
|
39
|
+
const input = 'b'.repeat(800);
|
|
40
|
+
const usage = counter.calculateUsage(system, input);
|
|
41
|
+
expect(usage.systemTokens).toBe(100);
|
|
42
|
+
expect(usage.inputTokens).toBe(200);
|
|
43
|
+
expect(usage.outputTokens).toBe(1000);
|
|
44
|
+
expect(usage.totalTokens).toBe(1300);
|
|
45
|
+
expect(usage.maxContextTokens).toBe(4096);
|
|
46
|
+
expect(usage.usagePercent).toBeCloseTo(31.7, 1);
|
|
47
|
+
});
|
|
48
|
+
test('handles empty strings', () => {
|
|
49
|
+
const counter = new TokenCounter();
|
|
50
|
+
const usage = counter.calculateUsage('', '');
|
|
51
|
+
expect(usage.systemTokens).toBe(0);
|
|
52
|
+
expect(usage.inputTokens).toBe(0);
|
|
53
|
+
expect(usage.totalTokens).toBe(1000); // just output tokens
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe('exceedsLimit', () => {
|
|
57
|
+
test('returns true when total exceeds max', () => {
|
|
58
|
+
const counter = new TokenCounter({ maxContextTokens: 100 });
|
|
59
|
+
const usage = counter.calculateUsage('a'.repeat(400), 'b'.repeat(400));
|
|
60
|
+
// 100 + 100 + 1000 = 1200 > 100
|
|
61
|
+
expect(counter.exceedsLimit(usage)).toBe(true);
|
|
62
|
+
});
|
|
63
|
+
test('returns false when under limit', () => {
|
|
64
|
+
const counter = new TokenCounter({ maxContextTokens: 4096 });
|
|
65
|
+
const usage = counter.calculateUsage('a'.repeat(400), 'b'.repeat(400));
|
|
66
|
+
// 100 + 100 + 1000 = 1200 < 4096
|
|
67
|
+
expect(counter.exceedsLimit(usage)).toBe(false);
|
|
68
|
+
});
|
|
69
|
+
test('returns false when exactly at limit', () => {
|
|
70
|
+
const counter = new TokenCounter({ maxContextTokens: 1200 });
|
|
71
|
+
const usage = counter.calculateUsage('a'.repeat(400), 'b'.repeat(400));
|
|
72
|
+
// 100 + 100 + 1000 = 1200 === 1200
|
|
73
|
+
expect(counter.exceedsLimit(usage)).toBe(false);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe('shouldWarn', () => {
|
|
77
|
+
test('returns true when usage exceeds warn threshold', () => {
|
|
78
|
+
const counter = new TokenCounter({
|
|
79
|
+
maxContextTokens: 1000,
|
|
80
|
+
warnThreshold: 90,
|
|
81
|
+
});
|
|
82
|
+
// 360 + 360 + 1000 = 1720 tokens = 172% (way over)
|
|
83
|
+
const usage = counter.calculateUsage('a'.repeat(1440), 'b'.repeat(1440));
|
|
84
|
+
expect(counter.shouldWarn(usage)).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
test('returns false when under warn threshold', () => {
|
|
87
|
+
const counter = new TokenCounter({
|
|
88
|
+
maxContextTokens: 4096,
|
|
89
|
+
warnThreshold: 90,
|
|
90
|
+
});
|
|
91
|
+
// 100 + 100 + 1000 = 1200 = ~29%
|
|
92
|
+
const usage = counter.calculateUsage('a'.repeat(400), 'b'.repeat(400));
|
|
93
|
+
expect(counter.shouldWarn(usage)).toBe(false);
|
|
94
|
+
});
|
|
95
|
+
test('uses 90% default threshold', () => {
|
|
96
|
+
const counter = new TokenCounter({ maxContextTokens: 1000 });
|
|
97
|
+
// Need 900+ tokens to trigger warning
|
|
98
|
+
// 200 + 200 + 1000 = 1400 = 140%
|
|
99
|
+
const usage = counter.calculateUsage('a'.repeat(800), 'b'.repeat(800));
|
|
100
|
+
expect(counter.shouldWarn(usage)).toBe(true);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe('formatUsage', () => {
|
|
104
|
+
test('formats usage information', () => {
|
|
105
|
+
const counter = new TokenCounter({ maxContextTokens: 4096 });
|
|
106
|
+
const usage = counter.calculateUsage('a'.repeat(400), 'b'.repeat(800));
|
|
107
|
+
const formatted = counter.formatUsage(usage);
|
|
108
|
+
expect(formatted).toContain('Token usage:');
|
|
109
|
+
expect(formatted).toContain('1300/4096');
|
|
110
|
+
expect(formatted).toContain('System prompt: 100 tokens');
|
|
111
|
+
expect(formatted).toContain('Input: 200 tokens');
|
|
112
|
+
expect(formatted).toContain('Reserved for output: 1000 tokens');
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
describe('getErrorMessage', () => {
|
|
116
|
+
test('provides detailed error message when limit exceeded', () => {
|
|
117
|
+
const counter = new TokenCounter({ maxContextTokens: 1000 });
|
|
118
|
+
const usage = counter.calculateUsage('a'.repeat(1000), 'b'.repeat(2000));
|
|
119
|
+
// 250 + 500 + 1000 = 1750 tokens
|
|
120
|
+
const message = counter.getErrorMessage(usage);
|
|
121
|
+
expect(message).toContain('Token budget exceeded');
|
|
122
|
+
expect(message).toContain('1750/1000');
|
|
123
|
+
expect(message).toContain('750 over limit');
|
|
124
|
+
expect(message).toContain('System prompt: 250 tokens');
|
|
125
|
+
expect(message).toContain('Input: 500 tokens');
|
|
126
|
+
expect(message).toContain('Suggestions:');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
describe('getWarningMessage', () => {
|
|
130
|
+
test('provides warning message when approaching limit', () => {
|
|
131
|
+
const counter = new TokenCounter({ maxContextTokens: 1000 });
|
|
132
|
+
const usage = counter.calculateUsage('a'.repeat(800), 'b'.repeat(800));
|
|
133
|
+
const message = counter.getWarningMessage(usage);
|
|
134
|
+
expect(message).toContain('⚠');
|
|
135
|
+
expect(message).toContain('Approaching token budget limit');
|
|
136
|
+
expect(message).toContain('System:');
|
|
137
|
+
expect(message).toContain('Input:');
|
|
138
|
+
expect(message).toContain('Output:');
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
describe('configuration', () => {
|
|
142
|
+
test('uses default values when not specified', () => {
|
|
143
|
+
const counter = new TokenCounter();
|
|
144
|
+
const usage = counter.calculateUsage('', '');
|
|
145
|
+
expect(usage.maxContextTokens).toBe(4096);
|
|
146
|
+
expect(usage.outputTokens).toBe(1000);
|
|
147
|
+
});
|
|
148
|
+
test('respects custom maxContextTokens', () => {
|
|
149
|
+
const counter = new TokenCounter({ maxContextTokens: 8192 });
|
|
150
|
+
const usage = counter.calculateUsage('', '');
|
|
151
|
+
expect(usage.maxContextTokens).toBe(8192);
|
|
152
|
+
});
|
|
153
|
+
test('respects custom token budget', () => {
|
|
154
|
+
const counter = new TokenCounter({
|
|
155
|
+
tokenBudget: {
|
|
156
|
+
system: 500,
|
|
157
|
+
input: 1500,
|
|
158
|
+
output: 2000,
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
const usage = counter.calculateUsage('', '');
|
|
162
|
+
expect(usage.outputTokens).toBe(2000);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
//# sourceMappingURL=token-counter.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-counter.test.js","sourceRoot":"","sources":["../../../src/llm/__tests__/token-counter.test.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnE,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC5B,IAAI,CAAC,4BAA4B,EAAE,GAAG,EAAE;QACpC,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,0CAA0C,EAAE,GAAG,EAAE;QAClD,2BAA2B;QAC3B,MAAM,CAAC,cAAc,CAAC,sBAAsB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,8BAA8B,EAAE,GAAG,EAAE;QACtC,6CAA6C;QAC7C,MAAM,CAAC,cAAc,CAAC,uBAAuB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,qBAAqB,EAAE,GAAG,EAAE;QAC7B,MAAM,IAAI,GAAG,+CAA+C,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACxE,MAAM,MAAM,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;QACpC,MAAM,CAAC,MAAM,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC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IAAI,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,8BAA8B,EAAE,GAAG,EAAE;YACtC,MAAM,OAAO,GAAG,IAAI,YAAY,CAAC;gBAC7B,WAAW,EAAE;oBACT,MAAM,EAAE,GAAG;oBACX,KAAK,EAAE,IAAI;oBACX,MAAM,EAAE,IAAI;iBACf;aACJ,CAAC,CAAC;YACH,MAAM,KAAK,GAAG,OAAO,CAAC,cAAc,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;YAE7C,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC1C,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM client - universal client for OpenAI-compatible APIs
|
|
3
|
+
*/
|
|
4
|
+
import type { LLMConfig, LLMRequest, LLMResponse, ExtractionOptions, ExtractionResponse } from './types.js';
|
|
5
|
+
export declare class LLMClient {
|
|
6
|
+
private config;
|
|
7
|
+
private retryConfig;
|
|
8
|
+
private tokenCounter;
|
|
9
|
+
constructor(config: LLMConfig);
|
|
10
|
+
/**
|
|
11
|
+
* Extracts data from text using schema
|
|
12
|
+
*/
|
|
13
|
+
extract(options: ExtractionOptions): Promise<ExtractionResponse>;
|
|
14
|
+
/**
|
|
15
|
+
* Calls chat completion API with retry logic
|
|
16
|
+
*/
|
|
17
|
+
private chatWithRetry;
|
|
18
|
+
/**
|
|
19
|
+
* Checks if error is retryable
|
|
20
|
+
*/
|
|
21
|
+
private isRetryableError;
|
|
22
|
+
/**
|
|
23
|
+
* Calculates delay with exponential backoff and jitter
|
|
24
|
+
*/
|
|
25
|
+
private calculateDelay;
|
|
26
|
+
/**
|
|
27
|
+
* Sleep utility
|
|
28
|
+
*/
|
|
29
|
+
private sleep;
|
|
30
|
+
/**
|
|
31
|
+
* Calls chat completion API
|
|
32
|
+
*/
|
|
33
|
+
chat(request: LLMRequest): Promise<LLMResponse>;
|
|
34
|
+
/**
|
|
35
|
+
* Handles error responses from API
|
|
36
|
+
*/
|
|
37
|
+
private handleErrorResponse;
|
|
38
|
+
/**
|
|
39
|
+
* Parses extraction response from LLM
|
|
40
|
+
*/
|
|
41
|
+
private parseExtractionResponse;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Creates LLM client with common configurations
|
|
45
|
+
*/
|
|
46
|
+
export declare function createLLMClient(config: LLMConfig): LLMClient;
|
|
47
|
+
/**
|
|
48
|
+
* Preset configurations for popular providers
|
|
49
|
+
*/
|
|
50
|
+
export declare const LLMPresets: {
|
|
51
|
+
ollama: (model?: string) => LLMConfig;
|
|
52
|
+
lmStudio: (model?: string) => LLMConfig;
|
|
53
|
+
openai: (apiKey: string, model?: string) => LLMConfig;
|
|
54
|
+
openrouter: (apiKey: string, model?: string) => LLMConfig;
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/llm/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EACR,SAAS,EACT,UAAU,EACV,WAAW,EACX,iBAAiB,EACjB,kBAAkB,EACrB,MAAM,YAAY,CAAC;AAKpB,qBAAa,SAAS;IAClB,OAAO,CAAC,MAAM,CAIZ;IACF,OAAO,CAAC,WAAW,CAKjB;IACF,OAAO,CAAC,YAAY,CAAe;gBAEvB,MAAM,EAAE,SAAS;IAoB7B;;OAEG;IACG,OAAO,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAgDtE;;OAEG;YACW,aAAa;IAiC3B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAWxB;;OAEG;IACH,OAAO,CAAC,cAAc;IAoBtB;;OAEG;IACH,OAAO,CAAC,KAAK;IAIb;;OAEG;IACG,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC;IAgDrD;;OAEG;YACW,mBAAmB;IA2BjC;;OAEG;IACH,OAAO,CAAC,uBAAuB;CAsDlC;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,CAE5D;AAED;;GAEG;AACH,eAAO,MAAM,UAAU;qBACH,MAAM,KAAc,SAAS;uBAK3B,MAAM,KAAmB,SAAS;qBAKnC,MAAM,UAAS,MAAM,KAAmB,SAAS;yBAM7C,MAAM,UAAS,MAAM,KAA0B,SAAS;CAKhF,CAAC"}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM client - universal client for OpenAI-compatible APIs
|
|
3
|
+
*/
|
|
4
|
+
import { LLMError, LLMErrorCodes } from './errors.js';
|
|
5
|
+
import { buildSystemPrompt, buildUserPrompt } from './prompt-builder.js';
|
|
6
|
+
import { TokenCounter } from './token-counter.js';
|
|
7
|
+
export class LLMClient {
|
|
8
|
+
config;
|
|
9
|
+
retryConfig;
|
|
10
|
+
tokenCounter;
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.config = {
|
|
13
|
+
temperature: 0.0,
|
|
14
|
+
maxTokens: 2000,
|
|
15
|
+
timeout: 120000, // 120 seconds for slower local models
|
|
16
|
+
...config,
|
|
17
|
+
};
|
|
18
|
+
this.retryConfig = config.retries || {
|
|
19
|
+
maxRetries: 3,
|
|
20
|
+
initialDelay: 1000,
|
|
21
|
+
maxDelay: 10000,
|
|
22
|
+
backoffFactor: 2,
|
|
23
|
+
};
|
|
24
|
+
this.tokenCounter = new TokenCounter({
|
|
25
|
+
maxContextTokens: config.maxContextTokens,
|
|
26
|
+
tokenBudget: config.tokenBudget,
|
|
27
|
+
warnThreshold: config.warnThreshold,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Extracts data from text using schema
|
|
32
|
+
*/
|
|
33
|
+
async extract(options) {
|
|
34
|
+
const { schema, input, systemPrompt } = options;
|
|
35
|
+
// Build prompts
|
|
36
|
+
const system = systemPrompt || buildSystemPrompt(schema);
|
|
37
|
+
const user = buildUserPrompt(input);
|
|
38
|
+
// Check token budget
|
|
39
|
+
const usage = this.tokenCounter.calculateUsage(system, user);
|
|
40
|
+
// Debug logging if enabled
|
|
41
|
+
if (this.config.debugTokens) {
|
|
42
|
+
console.error('[Token Usage]\n' + this.tokenCounter.formatUsage(usage));
|
|
43
|
+
}
|
|
44
|
+
// Error if over limit
|
|
45
|
+
if (this.tokenCounter.exceedsLimit(usage)) {
|
|
46
|
+
throw new LLMError(this.tokenCounter.getErrorMessage(usage), LLMErrorCodes.TOKEN_LIMIT_EXCEEDED, undefined, { usage });
|
|
47
|
+
}
|
|
48
|
+
// Warn if approaching limit
|
|
49
|
+
if (this.tokenCounter.shouldWarn(usage)) {
|
|
50
|
+
console.warn(this.tokenCounter.getWarningMessage(usage));
|
|
51
|
+
}
|
|
52
|
+
// Create request
|
|
53
|
+
const request = {
|
|
54
|
+
model: this.config.model,
|
|
55
|
+
messages: [
|
|
56
|
+
{ role: 'system', content: system },
|
|
57
|
+
{ role: 'user', content: user },
|
|
58
|
+
],
|
|
59
|
+
temperature: this.config.temperature,
|
|
60
|
+
max_tokens: this.config.maxTokens,
|
|
61
|
+
};
|
|
62
|
+
// Call API with retries
|
|
63
|
+
const response = await this.chatWithRetry(request);
|
|
64
|
+
// Parse response
|
|
65
|
+
return this.parseExtractionResponse(response);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Calls chat completion API with retry logic
|
|
69
|
+
*/
|
|
70
|
+
async chatWithRetry(request) {
|
|
71
|
+
let lastError = null;
|
|
72
|
+
let attempt = 0;
|
|
73
|
+
while (attempt <= this.retryConfig.maxRetries) {
|
|
74
|
+
try {
|
|
75
|
+
return await this.chat(request);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
lastError = error;
|
|
79
|
+
// Don't retry on non-retryable errors
|
|
80
|
+
if (!this.isRetryableError(lastError)) {
|
|
81
|
+
throw lastError;
|
|
82
|
+
}
|
|
83
|
+
// Last attempt - don't wait
|
|
84
|
+
if (attempt === this.retryConfig.maxRetries) {
|
|
85
|
+
throw lastError;
|
|
86
|
+
}
|
|
87
|
+
// Calculate delay with exponential backoff and jitter
|
|
88
|
+
const delay = this.calculateDelay(attempt, lastError);
|
|
89
|
+
// Wait before retry
|
|
90
|
+
await this.sleep(delay);
|
|
91
|
+
attempt++;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
throw lastError;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Checks if error is retryable
|
|
98
|
+
*/
|
|
99
|
+
isRetryableError(error) {
|
|
100
|
+
const retryableCodes = [
|
|
101
|
+
LLMErrorCodes.NETWORK_ERROR,
|
|
102
|
+
LLMErrorCodes.TIMEOUT,
|
|
103
|
+
LLMErrorCodes.RATE_LIMIT,
|
|
104
|
+
LLMErrorCodes.API_ERROR,
|
|
105
|
+
];
|
|
106
|
+
// Don't retry token limit errors
|
|
107
|
+
return retryableCodes.includes(error.code);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Calculates delay with exponential backoff and jitter
|
|
111
|
+
*/
|
|
112
|
+
calculateDelay(attempt, error) {
|
|
113
|
+
// Check for Retry-After header in rate limit errors
|
|
114
|
+
if (error.code === LLMErrorCodes.RATE_LIMIT && error.details?.retryAfter) {
|
|
115
|
+
const retryAfter = Number(error.details.retryAfter) * 1000;
|
|
116
|
+
return Math.min(retryAfter, this.retryConfig.maxDelay);
|
|
117
|
+
}
|
|
118
|
+
// Exponential backoff: initialDelay * (backoffFactor ^ attempt)
|
|
119
|
+
const exponentialDelay = this.retryConfig.initialDelay *
|
|
120
|
+
Math.pow(this.retryConfig.backoffFactor, attempt);
|
|
121
|
+
// Add jitter (random 0-25% of delay)
|
|
122
|
+
const jitter = exponentialDelay * 0.25 * Math.random();
|
|
123
|
+
const delayWithJitter = exponentialDelay + jitter;
|
|
124
|
+
// Cap at maxDelay
|
|
125
|
+
return Math.min(delayWithJitter, this.retryConfig.maxDelay);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Sleep utility
|
|
129
|
+
*/
|
|
130
|
+
sleep(ms) {
|
|
131
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Calls chat completion API
|
|
135
|
+
*/
|
|
136
|
+
async chat(request) {
|
|
137
|
+
const url = `${this.config.baseURL}/chat/completions`;
|
|
138
|
+
const controller = new AbortController();
|
|
139
|
+
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
140
|
+
try {
|
|
141
|
+
const response = await fetch(url, {
|
|
142
|
+
method: 'POST',
|
|
143
|
+
headers: {
|
|
144
|
+
'Content-Type': 'application/json',
|
|
145
|
+
...(this.config.apiKey && { 'Authorization': `Bearer ${this.config.apiKey}` }),
|
|
146
|
+
},
|
|
147
|
+
body: JSON.stringify(request),
|
|
148
|
+
signal: controller.signal,
|
|
149
|
+
});
|
|
150
|
+
clearTimeout(timeoutId);
|
|
151
|
+
if (!response.ok) {
|
|
152
|
+
await this.handleErrorResponse(response);
|
|
153
|
+
}
|
|
154
|
+
const data = await response.json();
|
|
155
|
+
return data;
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
clearTimeout(timeoutId);
|
|
159
|
+
if (error instanceof LLMError) {
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
if (error.name === 'AbortError') {
|
|
163
|
+
throw new LLMError(`Request timeout after ${this.config.timeout}ms`, LLMErrorCodes.TIMEOUT);
|
|
164
|
+
}
|
|
165
|
+
throw new LLMError(`Network error: ${error.message}`, LLMErrorCodes.NETWORK_ERROR, undefined, { originalError: error.message });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Handles error responses from API
|
|
170
|
+
*/
|
|
171
|
+
async handleErrorResponse(response) {
|
|
172
|
+
const status = response.status;
|
|
173
|
+
let errorMessage = `API error: ${status} ${response.statusText}`;
|
|
174
|
+
let errorCode = LLMErrorCodes.API_ERROR;
|
|
175
|
+
const details = {};
|
|
176
|
+
try {
|
|
177
|
+
const errorData = await response.json();
|
|
178
|
+
errorMessage = errorData.error?.message || errorMessage;
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// If we can't parse error JSON, use status text
|
|
182
|
+
}
|
|
183
|
+
if (status === 401 || status === 403) {
|
|
184
|
+
errorCode = LLMErrorCodes.AUTHENTICATION_ERROR;
|
|
185
|
+
}
|
|
186
|
+
else if (status === 429) {
|
|
187
|
+
errorCode = LLMErrorCodes.RATE_LIMIT;
|
|
188
|
+
// Capture Retry-After header if present
|
|
189
|
+
const retryAfter = response.headers.get('Retry-After');
|
|
190
|
+
if (retryAfter) {
|
|
191
|
+
details.retryAfter = retryAfter;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
throw new LLMError(errorMessage, errorCode, status, details);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Parses extraction response from LLM
|
|
198
|
+
*/
|
|
199
|
+
parseExtractionResponse(response) {
|
|
200
|
+
if (!response.choices || response.choices.length === 0) {
|
|
201
|
+
throw new LLMError('No choices in LLM response', LLMErrorCodes.INVALID_RESPONSE);
|
|
202
|
+
}
|
|
203
|
+
const message = response.choices[0].message;
|
|
204
|
+
if (!message || !message.content) {
|
|
205
|
+
throw new LLMError('No content in LLM response', LLMErrorCodes.INVALID_RESPONSE);
|
|
206
|
+
}
|
|
207
|
+
let content = message.content.trim();
|
|
208
|
+
// Remove markdown code blocks if present
|
|
209
|
+
if (content.startsWith('```json')) {
|
|
210
|
+
content = content.replace(/^```json\s*/, '').replace(/\s*```$/, '');
|
|
211
|
+
}
|
|
212
|
+
else if (content.startsWith('```')) {
|
|
213
|
+
content = content.replace(/^```\s*/, '').replace(/\s*```$/, '');
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const parsed = JSON.parse(content);
|
|
217
|
+
if (!parsed.data || typeof parsed.data !== 'object') {
|
|
218
|
+
throw new Error('Response missing data object');
|
|
219
|
+
}
|
|
220
|
+
if (typeof parsed.confidence !== 'number') {
|
|
221
|
+
throw new Error('Response missing confidence score');
|
|
222
|
+
}
|
|
223
|
+
if (!parsed.confidenceByField || typeof parsed.confidenceByField !== 'object') {
|
|
224
|
+
throw new Error('Response missing confidenceByField object');
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
data: parsed.data,
|
|
228
|
+
confidence: parsed.confidence,
|
|
229
|
+
confidenceByField: parsed.confidenceByField,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
throw new LLMError(`Failed to parse LLM response: ${error.message}`, LLMErrorCodes.INVALID_RESPONSE, undefined, { content, error: error.message });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Creates LLM client with common configurations
|
|
239
|
+
*/
|
|
240
|
+
export function createLLMClient(config) {
|
|
241
|
+
return new LLMClient(config);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Preset configurations for popular providers
|
|
245
|
+
*/
|
|
246
|
+
export const LLMPresets = {
|
|
247
|
+
ollama: (model = 'llama3') => ({
|
|
248
|
+
baseURL: 'http://localhost:11434/v1',
|
|
249
|
+
model,
|
|
250
|
+
}),
|
|
251
|
+
lmStudio: (model = 'local-model') => ({
|
|
252
|
+
baseURL: 'http://localhost:1234/v1',
|
|
253
|
+
model,
|
|
254
|
+
}),
|
|
255
|
+
openai: (apiKey, model = 'gpt-4o-mini') => ({
|
|
256
|
+
baseURL: 'https://api.openai.com/v1',
|
|
257
|
+
apiKey,
|
|
258
|
+
model,
|
|
259
|
+
}),
|
|
260
|
+
openrouter: (apiKey, model = 'openai/gpt-4o-mini') => ({
|
|
261
|
+
baseURL: 'https://openrouter.ai/api/v1',
|
|
262
|
+
apiKey,
|
|
263
|
+
model,
|
|
264
|
+
}),
|
|
265
|
+
};
|
|
266
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/llm/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AASH,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAqB,MAAM,aAAa,CAAC;AACzE,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,MAAM,OAAO,SAAS;IACV,MAAM,CAIZ;IACM,WAAW,CAKjB;IACM,YAAY,CAAe;IAEnC,YAAY,MAAiB;QACzB,IAAI,CAAC,MAAM,GAAG;YACV,WAAW,EAAE,GAAG;YAChB,SAAS,EAAE,IAAI;YACf,OAAO,EAAE,MAAM,EAAE,sCAAsC;YACvD,GAAG,MAAM;SACZ,CAAC;QACF,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,OAAO,IAAI;YACjC,UAAU,EAAE,CAAC;YACb,YAAY,EAAE,IAAI;YAClB,QAAQ,EAAE,KAAK;YACf,aAAa,EAAE,CAAC;SACnB,CAAC;QACF,IAAI,CAAC,YAAY,GAAG,IAAI,YAAY,CAAC;YACjC,gBAAgB,EAAE,MAAM,CAAC,gBAAgB;YACzC,WAAW,EAAE,MAAM,CAAC,WAAW;YAC/B,aAAa,EAAE,MAAM,CAAC,aAAa;SACtC,CAAC,CAAC;IACP,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,OAAO,CAAC,OAA0B;QACpC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC;QAEhD,gBAAgB;QAChB,MAAM,MAAM,GAAG,YAAY,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAC;QACzD,MAAM,IAAI,GAA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OAAO,EAAE,EAC3D,aAAa,CAAC,gBAAgB,EAC9B,SAAS,EACT,EAAE,OAAO,EAAE,KAAK,EAAG,KAAe,CAAC,OAAO,EAAE,CAC/C,CAAC;QACN,CAAC;IACL,CAAC;CACJ;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,MAAiB;IAC7C,OAAO,IAAI,SAAS,CAAC,MAAM,CAAC,CAAC;AACjC,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG;IACtB,MAAM,EAAE,CAAC,QAAgB,QAAQ,EAAa,EAAE,CAAC,CAAC;QAC9C,OAAO,EAAE,2BAA2B;QACpC,KAAK;KACR,CAAC;IAEF,QAAQ,EAAE,CAAC,QAAgB,aAAa,EAAa,EAAE,CAAC,CAAC;QACrD,OAAO,EAAE,0BAA0B;QACnC,KAAK;KACR,CAAC;IAEF,MAAM,EAAE,CAAC,MAAc,EAAE,QAAgB,aAAa,EAAa,EAAE,CAAC,CAAC;QACnE,OAAO,EAAE,2BAA2B;QACpC,MAAM;QACN,KAAK;KACR,CAAC;IAEF,UAAU,EAAE,CAAC,MAAc,EAAE,QAAgB,oBAAoB,EAAa,EAAE,CAAC,CAAC;QAC9E,OAAO,EAAE,8BAA8B;QACvC,MAAM;QACN,KAAK;KACR,CAAC;CACL,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM client errors
|
|
3
|
+
*/
|
|
4
|
+
export declare const LLMErrorCodes: {
|
|
5
|
+
readonly NETWORK_ERROR: "NETWORK_ERROR";
|
|
6
|
+
readonly API_ERROR: "API_ERROR";
|
|
7
|
+
readonly TIMEOUT: "TIMEOUT";
|
|
8
|
+
readonly INVALID_RESPONSE: "INVALID_RESPONSE";
|
|
9
|
+
readonly AUTHENTICATION_ERROR: "AUTHENTICATION_ERROR";
|
|
10
|
+
readonly RATE_LIMIT: "RATE_LIMIT";
|
|
11
|
+
readonly TOKEN_LIMIT_EXCEEDED: "TOKEN_LIMIT_EXCEEDED";
|
|
12
|
+
};
|
|
13
|
+
export type LLMErrorCode = (typeof LLMErrorCodes)[keyof typeof LLMErrorCodes];
|
|
14
|
+
export declare class LLMError extends Error {
|
|
15
|
+
code: LLMErrorCode;
|
|
16
|
+
statusCode?: number | undefined;
|
|
17
|
+
details?: Record<string, unknown> | undefined;
|
|
18
|
+
constructor(message: string, code: LLMErrorCode, statusCode?: number | undefined, details?: Record<string, unknown> | undefined);
|
|
19
|
+
toJSON(): {
|
|
20
|
+
name: string;
|
|
21
|
+
message: string;
|
|
22
|
+
code: LLMErrorCode;
|
|
23
|
+
statusCode: number | undefined;
|
|
24
|
+
details: Record<string, unknown> | undefined;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/llm/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,aAAa;;;;;;;;CAQhB,CAAC;AAEX,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,OAAO,aAAa,CAAC,CAAC;AAE9E,qBAAa,QAAS,SAAQ,KAAK;IAGpB,IAAI,EAAE,YAAY;IAClB,UAAU,CAAC,EAAE,MAAM;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;gBAHxC,OAAO,EAAE,MAAM,EACR,IAAI,EAAE,YAAY,EAClB,UAAU,CAAC,EAAE,MAAM,YAAA,EACnB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,YAAA;IAO5C,MAAM;;;;;;;CAST"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM client errors
|
|
3
|
+
*/
|
|
4
|
+
export const LLMErrorCodes = {
|
|
5
|
+
NETWORK_ERROR: 'NETWORK_ERROR',
|
|
6
|
+
API_ERROR: 'API_ERROR',
|
|
7
|
+
TIMEOUT: 'TIMEOUT',
|
|
8
|
+
INVALID_RESPONSE: 'INVALID_RESPONSE',
|
|
9
|
+
AUTHENTICATION_ERROR: 'AUTHENTICATION_ERROR',
|
|
10
|
+
RATE_LIMIT: 'RATE_LIMIT',
|
|
11
|
+
TOKEN_LIMIT_EXCEEDED: 'TOKEN_LIMIT_EXCEEDED',
|
|
12
|
+
};
|
|
13
|
+
export class LLMError extends Error {
|
|
14
|
+
code;
|
|
15
|
+
statusCode;
|
|
16
|
+
details;
|
|
17
|
+
constructor(message, code, statusCode, details) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.statusCode = statusCode;
|
|
21
|
+
this.details = details;
|
|
22
|
+
this.name = 'LLMError';
|
|
23
|
+
Object.setPrototypeOf(this, LLMError.prototype);
|
|
24
|
+
}
|
|
25
|
+
toJSON() {
|
|
26
|
+
return {
|
|
27
|
+
name: this.name,
|
|
28
|
+
message: this.message,
|
|
29
|
+
code: this.code,
|
|
30
|
+
statusCode: this.statusCode,
|
|
31
|
+
details: this.details,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/llm/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,aAAa,EAAE,eAAe;IAC9B,SAAS,EAAE,WAAW;IACtB,OAAO,EAAE,SAAS;IAClB,gBAAgB,EAAE,kBAAkB;IACpC,oBAAoB,EAAE,sBAAsB;IAC5C,UAAU,EAAE,YAAY;IACxB,oBAAoB,EAAE,sBAAsB;CACtC,CAAC;AAIX,MAAM,OAAO,QAAS,SAAQ,KAAK;IAGpB;IACA;IACA;IAJX,YACI,OAAe,EACR,IAAkB,EAClB,UAAmB,EACnB,OAAiC;QAExC,KAAK,CAAC,OAAO,CAAC,CAAC;QAJR,SAAI,GAAJ,IAAI,CAAc;QAClB,eAAU,GAAV,UAAU,CAAS;QACnB,YAAO,GAAP,OAAO,CAA0B;QAGxC,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;IAED,MAAM;QACF,OAAO;YACH,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,OAAO,EAAE,IAAI,CAAC,OAAO;SACxB,CAAC;IACN,CAAC;CACJ"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM module exports
|
|
3
|
+
*/
|
|
4
|
+
export { LLMClient, createLLMClient, LLMPresets } from './client.js';
|
|
5
|
+
export { LLMError, LLMErrorCodes } from './errors.js';
|
|
6
|
+
export { buildSystemPrompt, buildUserPrompt } from './prompt-builder.js';
|
|
7
|
+
export { TokenCounter, estimateTokens } from './token-counter.js';
|
|
8
|
+
export type { LLMConfig, RetryConfig, ChatMessage, LLMRequest, LLMResponse, ExtractionOptions, ExtractionResponse, TokenBudget, } from './types.js';
|
|
9
|
+
export type { TokenUsage, TokenCounterConfig } from './token-counter.js';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAClE,YAAY,EACR,SAAS,EACT,WAAW,EACX,WAAW,EACX,UAAU,EACV,WAAW,EACX,iBAAiB,EACjB,kBAAkB,EAClB,WAAW,GACd,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM module exports
|
|
3
|
+
*/
|
|
4
|
+
export { LLMClient, createLLMClient, LLMPresets } from './client.js';
|
|
5
|
+
export { LLMError, LLMErrorCodes } from './errors.js';
|
|
6
|
+
export { buildSystemPrompt, buildUserPrompt } from './prompt-builder.js';
|
|
7
|
+
export { TokenCounter, estimateTokens } from './token-counter.js';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt builder - generates prompts from schemas
|
|
3
|
+
*/
|
|
4
|
+
import type { Schema } from '../schemas/types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Builds a system prompt for extraction
|
|
7
|
+
*/
|
|
8
|
+
export declare function buildSystemPrompt(schema: Schema): string;
|
|
9
|
+
/**
|
|
10
|
+
* Builds a user prompt with input text
|
|
11
|
+
*/
|
|
12
|
+
export declare function buildUserPrompt(input: string): string;
|
|
13
|
+
//# sourceMappingURL=prompt-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt-builder.d.ts","sourceRoot":"","sources":["../../src/llm/prompt-builder.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAoGxD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAErD"}
|