langfx.js 0.1.0-alpha.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 +202 -0
- package/NOTICE +11 -0
- package/README.md +107 -0
- package/dist/agentic.d.ts +128 -0
- package/dist/agentic.js +265 -0
- package/dist/agentic.js.map +1 -0
- package/dist/cache.d.ts +32 -0
- package/dist/cache.js +135 -0
- package/dist/cache.js.map +1 -0
- package/dist/cancellation.d.ts +9 -0
- package/dist/cancellation.js +70 -0
- package/dist/cancellation.js.map +1 -0
- package/dist/errors.d.ts +40 -0
- package/dist/errors.js +44 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/json-stream.d.ts +73 -0
- package/dist/json-stream.js +222 -0
- package/dist/json-stream.js.map +1 -0
- package/dist/langfunc.d.ts +20 -0
- package/dist/langfunc.js +28 -0
- package/dist/langfunc.js.map +1 -0
- package/dist/language-model.d.ts +78 -0
- package/dist/language-model.js +218 -0
- package/dist/language-model.js.map +1 -0
- package/dist/llms/anthropic.d.ts +30 -0
- package/dist/llms/anthropic.js +365 -0
- package/dist/llms/anthropic.js.map +1 -0
- package/dist/llms/gemini.d.ts +34 -0
- package/dist/llms/gemini.js +380 -0
- package/dist/llms/gemini.js.map +1 -0
- package/dist/llms/images.d.ts +10 -0
- package/dist/llms/images.js +43 -0
- package/dist/llms/images.js.map +1 -0
- package/dist/llms/index.d.ts +7 -0
- package/dist/llms/index.js +4 -0
- package/dist/llms/index.js.map +1 -0
- package/dist/llms/openai.d.ts +30 -0
- package/dist/llms/openai.js +398 -0
- package/dist/llms/openai.js.map +1 -0
- package/dist/llms/transport.d.ts +4 -0
- package/dist/llms/transport.js +90 -0
- package/dist/llms/transport.js.map +1 -0
- package/dist/mapping.d.ts +71 -0
- package/dist/mapping.js +190 -0
- package/dist/mapping.js.map +1 -0
- package/dist/message.d.ts +56 -0
- package/dist/message.js +86 -0
- package/dist/message.js.map +1 -0
- package/dist/python-preview.d.ts +24 -0
- package/dist/python-preview.js +368 -0
- package/dist/python-preview.js.map +1 -0
- package/dist/python-stream.d.ts +15 -0
- package/dist/python-stream.js +29 -0
- package/dist/python-stream.js.map +1 -0
- package/dist/python.d.ts +85 -0
- package/dist/python.js +728 -0
- package/dist/python.js.map +1 -0
- package/dist/query.d.ts +31 -0
- package/dist/query.js +151 -0
- package/dist/query.js.map +1 -0
- package/dist/retry.d.ts +12 -0
- package/dist/retry.js +56 -0
- package/dist/retry.js.map +1 -0
- package/dist/schema/zod.d.ts +4 -0
- package/dist/schema/zod.js +7 -0
- package/dist/schema/zod.js.map +1 -0
- package/dist/schema.d.ts +10 -0
- package/dist/schema.js +7 -0
- package/dist/schema.js.map +1 -0
- package/dist/template.d.ts +14 -0
- package/dist/template.js +75 -0
- package/dist/template.js.map +1 -0
- package/dist/testing/index.d.ts +33 -0
- package/dist/testing/index.js +56 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/tool-call.d.ts +27 -0
- package/dist/tool-call.js +16 -0
- package/dist/tool-call.js.map +1 -0
- package/dist/tools.d.ts +43 -0
- package/dist/tools.js +155 -0
- package/dist/tools.js.map +1 -0
- package/docs/ANTHROPIC.md +43 -0
- package/docs/API_DESIGN.md +200 -0
- package/docs/GEMINI.md +68 -0
- package/docs/IMPLEMENTATION_STATUS.md +77 -0
- package/docs/LIVE_TESTING.md +24 -0
- package/docs/MAPPING.md +54 -0
- package/docs/OPENAI.md +40 -0
- package/docs/PORTING_PLAN.md +135 -0
- package/docs/PROMPT_PARITY.md +379 -0
- package/docs/PYTHON_PROTOCOL.md +109 -0
- package/docs/PYTHON_PROTOCOL_PARITY.md +964 -0
- package/docs/PYTHON_SCHEMA_EVALUATION.md +93 -0
- package/docs/PYTHON_STREAMING_PARITY.md +59 -0
- package/docs/RELEASING.md +25 -0
- package/docs/RETRIES_AND_CACHE.md +56 -0
- package/docs/SESSION_EVENTS.md +40 -0
- package/docs/SOURCE_AUDIT.md +133 -0
- package/docs/STREAMING.md +76 -0
- package/docs/TOOL_STREAMING.md +31 -0
- package/package.json +95 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from './errors.js';
|
|
2
|
+
export * from './message.js';
|
|
3
|
+
export * from './template.js';
|
|
4
|
+
export * from './schema.js';
|
|
5
|
+
export * from './language-model.js';
|
|
6
|
+
export * from './query.js';
|
|
7
|
+
export * from './langfunc.js';
|
|
8
|
+
export * from './agentic.js';
|
|
9
|
+
export * from './tool-call.js';
|
|
10
|
+
export * from './tools.js';
|
|
11
|
+
export * as llms from './llms/index.js';
|
|
12
|
+
export type { JsonPath, JsonStreamEvent, StructuredStreamEvent } from './json-stream.js';
|
|
13
|
+
export { Cache, InMemoryCache } from './cache.js';
|
|
14
|
+
export type { InMemoryCacheOptions } from './cache.js';
|
|
15
|
+
export type { RetryOptions } from './retry.js';
|
|
16
|
+
export { Mapping, LfQuery, MappingExample, MappingError } from './mapping.js';
|
|
17
|
+
export type { MappingOptions, MappingExampleOptions } from './mapping.js';
|
|
18
|
+
export * as python from './python.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * from './errors.js';
|
|
2
|
+
export * from './message.js';
|
|
3
|
+
export * from './template.js';
|
|
4
|
+
export * from './schema.js';
|
|
5
|
+
export * from './language-model.js';
|
|
6
|
+
export * from './query.js';
|
|
7
|
+
export * from './langfunc.js';
|
|
8
|
+
export * from './agentic.js';
|
|
9
|
+
export * from './tool-call.js';
|
|
10
|
+
export * from './tools.js';
|
|
11
|
+
export * as llms from './llms/index.js';
|
|
12
|
+
export { Cache, InMemoryCache } from './cache.js';
|
|
13
|
+
export { Mapping, LfQuery, MappingExample, MappingError } from './mapping.js';
|
|
14
|
+
export * as python from './python.js';
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;AAExC,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAGlD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG9E,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { SamplingUsage } from './language-model.js';
|
|
2
|
+
export type JsonPath = readonly (string | number)[];
|
|
3
|
+
/** Values in progress are unvalidated. Only generationComplete carries T. */
|
|
4
|
+
export type StructuredStreamEvent<T> = JsonStreamEvent | {
|
|
5
|
+
readonly type: 'textDelta';
|
|
6
|
+
readonly delta: string;
|
|
7
|
+
} | {
|
|
8
|
+
readonly type: 'generationComplete';
|
|
9
|
+
readonly result: T;
|
|
10
|
+
readonly usage?: SamplingUsage;
|
|
11
|
+
readonly finishReason?: string;
|
|
12
|
+
} | {
|
|
13
|
+
readonly type: 'parsingFailed';
|
|
14
|
+
readonly error: unknown;
|
|
15
|
+
} | {
|
|
16
|
+
readonly type: 'generationFailed';
|
|
17
|
+
readonly error: unknown;
|
|
18
|
+
} | {
|
|
19
|
+
readonly type: 'cancelled';
|
|
20
|
+
readonly error: unknown;
|
|
21
|
+
};
|
|
22
|
+
export type JsonStreamEvent = {
|
|
23
|
+
readonly type: 'objectStart';
|
|
24
|
+
readonly path: JsonPath;
|
|
25
|
+
readonly kind: 'object' | 'array';
|
|
26
|
+
readonly pythonClass?: string;
|
|
27
|
+
readonly pythonKind?: 'tuple' | 'set';
|
|
28
|
+
} | {
|
|
29
|
+
readonly type: 'objectEnd';
|
|
30
|
+
readonly path: JsonPath;
|
|
31
|
+
readonly kind: 'object' | 'array';
|
|
32
|
+
readonly value: unknown;
|
|
33
|
+
readonly pythonKind?: 'tuple' | 'set';
|
|
34
|
+
} | {
|
|
35
|
+
readonly type: 'fieldStart';
|
|
36
|
+
readonly path: JsonPath;
|
|
37
|
+
} | {
|
|
38
|
+
readonly type: 'stringFieldDelta';
|
|
39
|
+
readonly path: JsonPath;
|
|
40
|
+
readonly delta: string;
|
|
41
|
+
} | {
|
|
42
|
+
readonly type: 'fieldEnd';
|
|
43
|
+
readonly path: JsonPath;
|
|
44
|
+
readonly value: unknown;
|
|
45
|
+
};
|
|
46
|
+
/** Incremental strict JSON parser. No eval, reparsing prefixes, or schema coercion. */
|
|
47
|
+
export declare class JsonStreamParser {
|
|
48
|
+
private readonly maxChars;
|
|
49
|
+
private readonly maxDepth;
|
|
50
|
+
private readonly stack;
|
|
51
|
+
private token;
|
|
52
|
+
private mode;
|
|
53
|
+
private isKey;
|
|
54
|
+
private escaped;
|
|
55
|
+
private unicode;
|
|
56
|
+
private pendingHigh;
|
|
57
|
+
private stringValue;
|
|
58
|
+
private tokenPath;
|
|
59
|
+
private started;
|
|
60
|
+
private done;
|
|
61
|
+
private count;
|
|
62
|
+
private result;
|
|
63
|
+
constructor(maxChars?: number, maxDepth?: number);
|
|
64
|
+
feed(text: string): Generator<JsonStreamEvent>;
|
|
65
|
+
finish(): Generator<JsonStreamEvent>;
|
|
66
|
+
get value(): unknown;
|
|
67
|
+
private beginString;
|
|
68
|
+
private decoded;
|
|
69
|
+
private emitDecoded;
|
|
70
|
+
private finishAtom;
|
|
71
|
+
private close;
|
|
72
|
+
private complete;
|
|
73
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/** Incremental strict JSON parser. No eval, reparsing prefixes, or schema coercion. */
|
|
2
|
+
export class JsonStreamParser {
|
|
3
|
+
maxChars;
|
|
4
|
+
maxDepth;
|
|
5
|
+
stack = [];
|
|
6
|
+
token = '';
|
|
7
|
+
mode;
|
|
8
|
+
isKey = false;
|
|
9
|
+
escaped = false;
|
|
10
|
+
unicode;
|
|
11
|
+
pendingHigh = '';
|
|
12
|
+
stringValue = '';
|
|
13
|
+
tokenPath = [];
|
|
14
|
+
started = false;
|
|
15
|
+
done = false;
|
|
16
|
+
count = 0;
|
|
17
|
+
result;
|
|
18
|
+
constructor(maxChars = 1_048_576, maxDepth = 64) {
|
|
19
|
+
this.maxChars = maxChars;
|
|
20
|
+
this.maxDepth = maxDepth;
|
|
21
|
+
for (const [name, value] of [['maxChars', maxChars], ['maxDepth', maxDepth]]) {
|
|
22
|
+
if (!Number.isSafeInteger(value) || value < 1)
|
|
23
|
+
throw new RangeError(`${name} must be a positive safe integer.`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
*feed(text) {
|
|
27
|
+
this.count += text.length;
|
|
28
|
+
if (this.count > this.maxChars)
|
|
29
|
+
throw new SyntaxError('Structured output exceeds maxChars.');
|
|
30
|
+
for (let i = 0; i < text.length; i++) {
|
|
31
|
+
const c = text[i];
|
|
32
|
+
if (this.mode === 'string') {
|
|
33
|
+
if (this.unicode !== undefined) {
|
|
34
|
+
if (!/[0-9a-fA-F]/.test(c))
|
|
35
|
+
throw new SyntaxError('Invalid JSON Unicode escape.');
|
|
36
|
+
this.unicode += c;
|
|
37
|
+
if (this.unicode.length === 4) {
|
|
38
|
+
yield* this.decoded(String.fromCharCode(parseInt(this.unicode, 16)));
|
|
39
|
+
this.unicode = undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else if (this.escaped) {
|
|
43
|
+
this.escaped = false;
|
|
44
|
+
if (c === 'u')
|
|
45
|
+
this.unicode = '';
|
|
46
|
+
else {
|
|
47
|
+
const escapes = { '"': '"', '\\': '\\', '/': '/', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t' };
|
|
48
|
+
if (!Object.hasOwn(escapes, c))
|
|
49
|
+
throw new SyntaxError('Invalid JSON escape.');
|
|
50
|
+
yield* this.decoded(escapes[c]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (c === '\\')
|
|
54
|
+
this.escaped = true;
|
|
55
|
+
else if (c === '"') {
|
|
56
|
+
if (this.pendingHigh) {
|
|
57
|
+
yield* this.emitDecoded(this.pendingHigh);
|
|
58
|
+
this.pendingHigh = '';
|
|
59
|
+
}
|
|
60
|
+
this.mode = undefined;
|
|
61
|
+
if (this.isKey) {
|
|
62
|
+
const frame = this.stack.at(-1);
|
|
63
|
+
if (frame.keys.has(this.stringValue))
|
|
64
|
+
throw new SyntaxError('Duplicate JSON object key.');
|
|
65
|
+
frame.keys.add(this.stringValue);
|
|
66
|
+
frame.key = this.stringValue;
|
|
67
|
+
frame.state = 'colon';
|
|
68
|
+
}
|
|
69
|
+
else
|
|
70
|
+
yield* this.complete(this.stringValue, this.tokenPath);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
if (c.charCodeAt(0) < 32)
|
|
74
|
+
throw new SyntaxError('Unescaped control character in JSON string.');
|
|
75
|
+
yield* this.decoded(c);
|
|
76
|
+
}
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (this.mode === 'atom') {
|
|
80
|
+
if (!/[\x20\t\r\n,\]}]/.test(c)) {
|
|
81
|
+
this.token += c;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
yield* this.finishAtom();
|
|
85
|
+
// Process this delimiter again, now that the primitive is complete.
|
|
86
|
+
}
|
|
87
|
+
if (/[\x20\t\r\n]/.test(c))
|
|
88
|
+
continue;
|
|
89
|
+
if (this.done)
|
|
90
|
+
throw new SyntaxError('Trailing content after JSON value.');
|
|
91
|
+
const frame = this.stack.at(-1);
|
|
92
|
+
if (frame?.state === 'commaOrEnd') {
|
|
93
|
+
if (c === ',') {
|
|
94
|
+
frame.state = frame.kind === 'object' ? 'key' : 'value';
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (c === (frame.kind === 'object' ? '}' : ']')) {
|
|
98
|
+
yield* this.close();
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
throw new SyntaxError('Expected comma or closing delimiter.');
|
|
102
|
+
}
|
|
103
|
+
if (frame?.state === 'colon') {
|
|
104
|
+
if (c !== ':')
|
|
105
|
+
throw new SyntaxError('Expected colon after JSON key.');
|
|
106
|
+
frame.state = 'value';
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (frame?.state === 'keyOrEnd' || frame?.state === 'key') {
|
|
110
|
+
if (c === '}' && frame.state === 'keyOrEnd') {
|
|
111
|
+
yield* this.close();
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (c !== '"')
|
|
115
|
+
throw new SyntaxError('Expected JSON object key.');
|
|
116
|
+
this.beginString(true, []);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (frame?.state === 'valueOrEnd' && c === ']') {
|
|
120
|
+
yield* this.close();
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const path = Object.freeze(frame ? [...frame.path, frame.kind === 'object' ? frame.key : frame.index] : []);
|
|
124
|
+
if (!frame && this.started)
|
|
125
|
+
throw new SyntaxError('Unexpected JSON value.');
|
|
126
|
+
this.started = true;
|
|
127
|
+
if (path.length)
|
|
128
|
+
yield { type: 'fieldStart', path };
|
|
129
|
+
if (c === '{' || c === '[') {
|
|
130
|
+
if (this.stack.length >= this.maxDepth)
|
|
131
|
+
throw new SyntaxError('Structured output exceeds maxDepth.');
|
|
132
|
+
const kind = c === '{' ? 'object' : 'array';
|
|
133
|
+
this.stack.push({ kind, path, value: kind === 'object' ? {} : [], state: kind === 'object' ? 'keyOrEnd' : 'valueOrEnd', key: '', index: 0, keys: new Set() });
|
|
134
|
+
yield { type: 'objectStart', path, kind };
|
|
135
|
+
}
|
|
136
|
+
else if (c === '"')
|
|
137
|
+
this.beginString(false, path);
|
|
138
|
+
else if (/[\-0-9tfn]/.test(c)) {
|
|
139
|
+
this.mode = 'atom';
|
|
140
|
+
this.token = c;
|
|
141
|
+
this.tokenPath = path;
|
|
142
|
+
}
|
|
143
|
+
else
|
|
144
|
+
throw new SyntaxError('Expected JSON value.');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
*finish() {
|
|
148
|
+
if (this.mode === 'atom')
|
|
149
|
+
yield* this.finishAtom();
|
|
150
|
+
if (!this.done || this.mode || this.stack.length)
|
|
151
|
+
throw new SyntaxError('Incomplete JSON output.');
|
|
152
|
+
}
|
|
153
|
+
get value() {
|
|
154
|
+
if (!this.done)
|
|
155
|
+
throw new SyntaxError('JSON value is incomplete.');
|
|
156
|
+
return this.result;
|
|
157
|
+
}
|
|
158
|
+
beginString(isKey, path) {
|
|
159
|
+
this.mode = 'string';
|
|
160
|
+
this.isKey = isKey;
|
|
161
|
+
this.tokenPath = path;
|
|
162
|
+
this.stringValue = '';
|
|
163
|
+
this.pendingHigh = '';
|
|
164
|
+
}
|
|
165
|
+
*decoded(c) {
|
|
166
|
+
if (this.pendingHigh) {
|
|
167
|
+
const high = this.pendingHigh;
|
|
168
|
+
this.pendingHigh = '';
|
|
169
|
+
if (/^[\uDC00-\uDFFF]$/.test(c)) {
|
|
170
|
+
yield* this.emitDecoded(high + c);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
yield* this.emitDecoded(high);
|
|
174
|
+
}
|
|
175
|
+
if (/^[\uD800-\uDBFF]$/.test(c))
|
|
176
|
+
this.pendingHigh = c;
|
|
177
|
+
else
|
|
178
|
+
yield* this.emitDecoded(c);
|
|
179
|
+
}
|
|
180
|
+
*emitDecoded(value) {
|
|
181
|
+
this.stringValue += value;
|
|
182
|
+
if (!this.isKey)
|
|
183
|
+
yield { type: 'stringFieldDelta', path: this.tokenPath, delta: value };
|
|
184
|
+
}
|
|
185
|
+
*finishAtom() {
|
|
186
|
+
if (!/^(?:true|false|null|-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)$/.test(this.token))
|
|
187
|
+
throw new SyntaxError('Invalid JSON primitive.');
|
|
188
|
+
const value = JSON.parse(this.token);
|
|
189
|
+
if (typeof value === 'number' && !Number.isFinite(value))
|
|
190
|
+
throw new SyntaxError('JSON number exceeds finite range.');
|
|
191
|
+
this.mode = undefined;
|
|
192
|
+
this.token = '';
|
|
193
|
+
yield* this.complete(value, this.tokenPath);
|
|
194
|
+
}
|
|
195
|
+
*close() {
|
|
196
|
+
const frame = this.stack.pop();
|
|
197
|
+
Object.freeze(frame.value);
|
|
198
|
+
yield { type: 'objectEnd', path: frame.path, kind: frame.kind, value: frame.value };
|
|
199
|
+
yield* this.complete(frame.value, frame.path);
|
|
200
|
+
}
|
|
201
|
+
*complete(value, path) {
|
|
202
|
+
// Containers never change after publication; partial events cannot mutate final validation input.
|
|
203
|
+
if (value !== null && typeof value === 'object')
|
|
204
|
+
Object.freeze(value);
|
|
205
|
+
const parent = this.stack.at(-1);
|
|
206
|
+
if (parent) {
|
|
207
|
+
if (parent.kind === 'array')
|
|
208
|
+
parent.value.push(value);
|
|
209
|
+
else
|
|
210
|
+
Object.defineProperty(parent.value, parent.key, { value, enumerable: true, writable: true, configurable: true });
|
|
211
|
+
parent.index++;
|
|
212
|
+
parent.state = 'commaOrEnd';
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
this.result = value;
|
|
216
|
+
this.done = true;
|
|
217
|
+
}
|
|
218
|
+
if (path.length)
|
|
219
|
+
yield { type: 'fieldEnd', path, value };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
//# sourceMappingURL=json-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-stream.js","sourceRoot":"","sources":["../src/json-stream.ts"],"names":[],"mappings":"AAuBA,uFAAuF;AACvF,MAAM,O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AC;YACtH,MAAM,CAAC,KAAK,EAAE,CAAC;YAAC,MAAM,CAAC,KAAK,GAAG,YAAY,CAAC;QAC9C,CAAC;aAAM,CAAC;YAAC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;YAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAAC,CAAC;QACjD,IAAI,IAAI,CAAC,MAAM;YAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IAC3D,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Template } from './template.js';
|
|
2
|
+
import type { Variables } from './template.js';
|
|
3
|
+
import type { Message } from './message.js';
|
|
4
|
+
import type { LanguageModel, CallOptions, StreamChunk } from './language-model.js';
|
|
5
|
+
export interface LangFuncOptions {
|
|
6
|
+
readonly lm?: LanguageModel;
|
|
7
|
+
readonly vars?: Variables;
|
|
8
|
+
}
|
|
9
|
+
export interface LangFuncCallOptions extends CallOptions {
|
|
10
|
+
readonly lm?: LanguageModel;
|
|
11
|
+
readonly vars?: Variables;
|
|
12
|
+
}
|
|
13
|
+
export declare class LangFunc extends Template {
|
|
14
|
+
readonly lm: LanguageModel | undefined;
|
|
15
|
+
constructor(templateOrOptions?: string | LangFuncOptions, options?: LangFuncOptions);
|
|
16
|
+
transformInput(message: Message): Message;
|
|
17
|
+
transformOutput(message: Message): Message;
|
|
18
|
+
call(options?: LangFuncCallOptions): Promise<Message>;
|
|
19
|
+
stream(options?: LangFuncCallOptions): AsyncIterable<StreamChunk>;
|
|
20
|
+
}
|
package/dist/langfunc.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Template } from './template.js';
|
|
2
|
+
export class LangFunc extends Template {
|
|
3
|
+
lm;
|
|
4
|
+
constructor(templateOrOptions, options = {}) {
|
|
5
|
+
const config = typeof templateOrOptions === 'object' ? templateOrOptions : options;
|
|
6
|
+
super(typeof templateOrOptions === 'string' ? templateOrOptions : undefined, config.vars);
|
|
7
|
+
this.lm = config.lm;
|
|
8
|
+
}
|
|
9
|
+
transformInput(message) { return message; }
|
|
10
|
+
transformOutput(message) { return message; }
|
|
11
|
+
async call(options = {}) {
|
|
12
|
+
const { lm = this.lm, vars, ...sampling } = options;
|
|
13
|
+
if (!lm)
|
|
14
|
+
throw new TypeError('LangFunc.call requires a LanguageModel.');
|
|
15
|
+
return this.transformOutput(await lm.call(this.transformInput(this.render(vars)), sampling));
|
|
16
|
+
}
|
|
17
|
+
stream(options = {}) {
|
|
18
|
+
const { lm = this.lm, vars, ...sampling } = options;
|
|
19
|
+
if (!lm)
|
|
20
|
+
throw new TypeError('LangFunc.stream requires a LanguageModel.');
|
|
21
|
+
// A whole-message transform cannot safely be applied to independent deltas.
|
|
22
|
+
if (this.transformOutput !== LangFunc.prototype.transformOutput) {
|
|
23
|
+
throw new TypeError('LangFunc streaming with transformOutput is not supported yet; use call().');
|
|
24
|
+
}
|
|
25
|
+
return lm.stream(this.transformInput(this.render(vars)), sampling);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=langfunc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"langfunc.js","sourceRoot":"","sources":["../src/langfunc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAOzC,MAAM,OAAO,QAAS,SAAQ,QAAQ;IAC3B,EAAE,CAA4B;IACvC,YAAY,iBAA4C,EAAE,OAAO,GAAoB,EAAE;QACrF,MAAM,MAAM,GAAG,OAAO,iBAAiB,KAAK,QAAQ,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,OAAO,CAAC;QACnF,KAAK,CAAC,OAAO,iBAAiB,KAAK,QAAQ,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;QAC1F,IAAI,CAAC,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;IACtB,CAAC;IACD,cAAc,CAAC,OAAgB,IAAa,OAAO,OAAO,CAAC,CAAC,CAAC;IAC7D,eAAe,CAAC,OAAgB,IAAa,OAAO,OAAO,CAAC,CAAC,CAAC;IAC9D,KAAK,CAAC,IAAI,CAAC,OAAO,GAAwB,EAAE;QAC1C,MAAM,EAAE,EAAE,GAAG,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,QAAQ,EAAE,GAAG,OAAO,CAAC;QACpD,IAAI,CAAC,EAAE;YAAE,MAAM,IAAI,SAAS,CAAC,yCAAyC,CAAC,CAAC;QACxE,OAAO,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC/F,CAAC;IACD,MAAM,CAAC,OAAO,GAAwB,EAAE;QACtC,MAAM,EAAE,EAAE,GAAG,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,QAAQ,EAAE,GAAG,OAAO,CAAC;QACpD,IAAI,CAAC,EAAE;YAAE,MAAM,IAAI,SAAS,CAAC,2CAA2C,CAAC,CAAC;QAC1E,4EAA4E;QAC5E,IAAI,IAAI,CAAC,eAAe,KAAK,QAAQ,CAAC,SAAS,CAAC,eAAe,EAAE,CAAC;YAChE,MAAM,IAAI,SAAS,CAAC,2EAA2E,CAAC,CAAC;QACnG,CAAC;QACD,OAAO,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;IACrE,CAAC;CACF"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Cache } from './cache.js';
|
|
2
|
+
import type { RetryOptions } from './retry.js';
|
|
3
|
+
import type { ToolDeclaration, ToolCallStreamEvent } from './tool-call.js';
|
|
4
|
+
import { Message } from './message.js';
|
|
5
|
+
export interface SamplingOptions {
|
|
6
|
+
readonly tools?: readonly ToolDeclaration[];
|
|
7
|
+
readonly toolChoice?: 'auto' | 'any' | 'none' | {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
};
|
|
10
|
+
readonly temperature?: number;
|
|
11
|
+
readonly maxTokens?: number;
|
|
12
|
+
readonly responseSchema?: Readonly<Record<string, unknown>>;
|
|
13
|
+
}
|
|
14
|
+
export interface CallOptions extends SamplingOptions, RetryOptions {
|
|
15
|
+
readonly cache?: Cache | null;
|
|
16
|
+
readonly cacheSeed?: number | null;
|
|
17
|
+
readonly cacheNamespace?: string;
|
|
18
|
+
readonly signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
export interface ModelContext extends SamplingOptions {
|
|
21
|
+
readonly signal: AbortSignal;
|
|
22
|
+
}
|
|
23
|
+
export interface SamplingUsage {
|
|
24
|
+
readonly promptTokens: number;
|
|
25
|
+
readonly completionTokens: number;
|
|
26
|
+
readonly totalTokens: number;
|
|
27
|
+
}
|
|
28
|
+
export interface Sample {
|
|
29
|
+
readonly response: Message;
|
|
30
|
+
}
|
|
31
|
+
export interface SamplingResult {
|
|
32
|
+
readonly cacheHit?: boolean;
|
|
33
|
+
readonly cachedUsage?: SamplingUsage;
|
|
34
|
+
readonly samples: readonly Sample[];
|
|
35
|
+
readonly usage?: SamplingUsage;
|
|
36
|
+
}
|
|
37
|
+
export interface StreamChunk {
|
|
38
|
+
readonly toolEvent?: ToolCallStreamEvent;
|
|
39
|
+
readonly delta: Message;
|
|
40
|
+
readonly aggregated: Message;
|
|
41
|
+
readonly index: number;
|
|
42
|
+
readonly isFinal: boolean;
|
|
43
|
+
readonly usage?: SamplingUsage;
|
|
44
|
+
readonly finishReason?: string;
|
|
45
|
+
}
|
|
46
|
+
export interface ModelCapabilities {
|
|
47
|
+
readonly streaming: boolean;
|
|
48
|
+
readonly tools?: boolean;
|
|
49
|
+
readonly toolStreaming?: boolean;
|
|
50
|
+
readonly nativeStructuredOutput: boolean;
|
|
51
|
+
readonly media: boolean;
|
|
52
|
+
}
|
|
53
|
+
export interface LanguageModelOptions extends Omit<CallOptions, 'signal'> {
|
|
54
|
+
readonly model?: string;
|
|
55
|
+
}
|
|
56
|
+
export declare abstract class LanguageModel {
|
|
57
|
+
private readonly resultCache;
|
|
58
|
+
private readonly responseCache;
|
|
59
|
+
readonly modelId: string;
|
|
60
|
+
readonly cache: Cache | null;
|
|
61
|
+
private readonly cacheScope;
|
|
62
|
+
private readonly runtimeOptions;
|
|
63
|
+
readonly samplingOptions: Readonly<SamplingOptions>;
|
|
64
|
+
readonly capabilities: ModelCapabilities;
|
|
65
|
+
constructor(options?: LanguageModelOptions);
|
|
66
|
+
protected abstract _sample(prompts: readonly Message[], context: ModelContext): Promise<readonly SamplingResult[]>;
|
|
67
|
+
protected _stream(_prompt: Message, _context: ModelContext): AsyncIterable<StreamChunk>;
|
|
68
|
+
sample(prompts: readonly (string | Message)[], options?: CallOptions): Promise<SamplingResult[]>;
|
|
69
|
+
call(prompt: string | Message, options?: CallOptions): Promise<Message>;
|
|
70
|
+
/** Evict the exact entry associated with a response from this model's call(). */
|
|
71
|
+
invalidateCache(response: Message): Promise<void>;
|
|
72
|
+
/** Always returns an async iterable directly, never a Promise. */
|
|
73
|
+
stream(prompt: string | Message, options?: CallOptions): AsyncIterable<StreamChunk>;
|
|
74
|
+
private sampleAttempt;
|
|
75
|
+
private validateResults;
|
|
76
|
+
private context;
|
|
77
|
+
private validate;
|
|
78
|
+
}
|