@lov3kaizen/agentsea-structured 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +330 -0
- package/dist/index.d.ts +783 -0
- package/dist/index.js +3210 -0
- package/dist/index.js.map +1 -0
- package/package.json +77 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,783 @@
|
|
|
1
|
+
import { z, ZodError } from 'zod';
|
|
2
|
+
import { EventEmitter } from 'eventemitter3';
|
|
3
|
+
|
|
4
|
+
type ExtractionMode = 'json' | 'tool' | 'prompt' | 'hybrid';
|
|
5
|
+
type StructuredProvider$1 = 'openai' | 'anthropic' | 'google' | 'generic';
|
|
6
|
+
type MessageRole = 'system' | 'user' | 'assistant';
|
|
7
|
+
interface ChatMessage {
|
|
8
|
+
role: MessageRole;
|
|
9
|
+
content: string;
|
|
10
|
+
name?: string;
|
|
11
|
+
}
|
|
12
|
+
interface StructuredRequestOptions<T extends z.ZodType> {
|
|
13
|
+
model: string;
|
|
14
|
+
messages: ChatMessage[];
|
|
15
|
+
response_format: T;
|
|
16
|
+
mode?: ExtractionModeConfig;
|
|
17
|
+
retry?: Partial<RetryConfig>;
|
|
18
|
+
temperature?: number;
|
|
19
|
+
maxTokens?: number;
|
|
20
|
+
options?: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
type ExtractionModeConfig = ExtractionMode | JsonModeConfig | ToolModeConfig | PromptModeConfig | HybridModeConfig;
|
|
23
|
+
interface JsonModeConfig {
|
|
24
|
+
mode: 'json';
|
|
25
|
+
strict?: boolean;
|
|
26
|
+
}
|
|
27
|
+
interface ToolModeConfig {
|
|
28
|
+
mode: 'tool';
|
|
29
|
+
toolName?: string;
|
|
30
|
+
toolDescription?: string;
|
|
31
|
+
}
|
|
32
|
+
interface PromptModeConfig {
|
|
33
|
+
mode: 'prompt';
|
|
34
|
+
format?: 'json-schema' | 'typescript' | 'examples';
|
|
35
|
+
position?: 'system' | 'user' | 'suffix';
|
|
36
|
+
includeExamples?: boolean;
|
|
37
|
+
}
|
|
38
|
+
interface HybridModeConfig {
|
|
39
|
+
mode: 'hybrid';
|
|
40
|
+
fallbackOrder?: ExtractionMode[];
|
|
41
|
+
}
|
|
42
|
+
interface RetryConfig {
|
|
43
|
+
maxAttempts: number;
|
|
44
|
+
backoffMultiplier: number;
|
|
45
|
+
initialDelay: number;
|
|
46
|
+
maxDelay: number;
|
|
47
|
+
retryOn: RetryCondition[];
|
|
48
|
+
fixHints?: FixHintConfig;
|
|
49
|
+
recovery?: RecoveryConfig;
|
|
50
|
+
}
|
|
51
|
+
interface FixHintConfig {
|
|
52
|
+
includeErrors?: boolean;
|
|
53
|
+
includeExamples?: boolean;
|
|
54
|
+
suggestFixes?: boolean;
|
|
55
|
+
customHints?: (errors: ValidationError[], schema: z.ZodType) => string;
|
|
56
|
+
}
|
|
57
|
+
type RetryCondition = 'parse_error' | 'validation_error' | 'incomplete_response' | 'empty_response' | 'rate_limit' | 'timeout';
|
|
58
|
+
interface RecoveryConfig {
|
|
59
|
+
repairJson?: boolean;
|
|
60
|
+
extractFromMarkdown?: boolean;
|
|
61
|
+
coerceTypes?: boolean;
|
|
62
|
+
usePartial?: boolean;
|
|
63
|
+
}
|
|
64
|
+
interface ValidationError {
|
|
65
|
+
path: (string | number)[];
|
|
66
|
+
message: string;
|
|
67
|
+
expected?: string;
|
|
68
|
+
received?: unknown;
|
|
69
|
+
code?: string;
|
|
70
|
+
}
|
|
71
|
+
interface ExtractionResult<T> {
|
|
72
|
+
success: boolean;
|
|
73
|
+
data?: T;
|
|
74
|
+
error?: Error;
|
|
75
|
+
raw?: string;
|
|
76
|
+
metadata: ExtractionMetadata;
|
|
77
|
+
}
|
|
78
|
+
interface ExtractionMetadata {
|
|
79
|
+
totalAttempts: number;
|
|
80
|
+
totalDuration: number;
|
|
81
|
+
finalMode: ExtractionMode;
|
|
82
|
+
tokenUsage?: TokenUsage;
|
|
83
|
+
attempts: ExtractionAttempt[];
|
|
84
|
+
model: string;
|
|
85
|
+
}
|
|
86
|
+
interface TokenUsage {
|
|
87
|
+
promptTokens: number;
|
|
88
|
+
completionTokens: number;
|
|
89
|
+
totalTokens: number;
|
|
90
|
+
}
|
|
91
|
+
interface ExtractionAttempt {
|
|
92
|
+
attempt: number;
|
|
93
|
+
mode: ExtractionMode;
|
|
94
|
+
success: boolean;
|
|
95
|
+
duration: number;
|
|
96
|
+
rawResponse?: string;
|
|
97
|
+
error?: string;
|
|
98
|
+
validationErrors?: ValidationError[];
|
|
99
|
+
}
|
|
100
|
+
interface StructuredClientConfig {
|
|
101
|
+
defaultModel?: string;
|
|
102
|
+
defaultMode?: ExtractionMode;
|
|
103
|
+
defaultRetry?: Partial<RetryConfig>;
|
|
104
|
+
enableFixHints?: boolean;
|
|
105
|
+
validatePartials?: boolean;
|
|
106
|
+
providerOptions?: Record<string, unknown>;
|
|
107
|
+
}
|
|
108
|
+
declare class StructuredError extends Error {
|
|
109
|
+
code: string;
|
|
110
|
+
attempts: ExtractionAttempt[];
|
|
111
|
+
validationErrors?: ValidationError[];
|
|
112
|
+
partial?: unknown;
|
|
113
|
+
constructor(message: string, code: string, attempts: ExtractionAttempt[], validationErrors?: ValidationError[], partial?: unknown);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type SchemaFormat = 'json-schema' | 'typescript' | 'natural' | 'examples';
|
|
117
|
+
interface SchemaPromptOptions {
|
|
118
|
+
format?: SchemaFormat;
|
|
119
|
+
includeDescriptions?: boolean;
|
|
120
|
+
includeExamples?: boolean;
|
|
121
|
+
includeConstraints?: boolean;
|
|
122
|
+
maxDepth?: number;
|
|
123
|
+
indent?: number;
|
|
124
|
+
}
|
|
125
|
+
interface SchemaPrompt {
|
|
126
|
+
text: string;
|
|
127
|
+
format: SchemaFormat;
|
|
128
|
+
jsonSchema?: JsonSchema;
|
|
129
|
+
typeScript?: string;
|
|
130
|
+
}
|
|
131
|
+
interface JsonSchema {
|
|
132
|
+
$schema?: string;
|
|
133
|
+
type?: string | string[];
|
|
134
|
+
properties?: Record<string, JsonSchema>;
|
|
135
|
+
items?: JsonSchema | JsonSchema[];
|
|
136
|
+
required?: string[];
|
|
137
|
+
enum?: unknown[];
|
|
138
|
+
const?: unknown;
|
|
139
|
+
anyOf?: JsonSchema[];
|
|
140
|
+
oneOf?: JsonSchema[];
|
|
141
|
+
allOf?: JsonSchema[];
|
|
142
|
+
not?: JsonSchema;
|
|
143
|
+
if?: JsonSchema;
|
|
144
|
+
then?: JsonSchema;
|
|
145
|
+
else?: JsonSchema;
|
|
146
|
+
description?: string;
|
|
147
|
+
default?: unknown;
|
|
148
|
+
examples?: unknown[];
|
|
149
|
+
minimum?: number;
|
|
150
|
+
maximum?: number;
|
|
151
|
+
exclusiveMinimum?: number;
|
|
152
|
+
exclusiveMaximum?: number;
|
|
153
|
+
minLength?: number;
|
|
154
|
+
maxLength?: number;
|
|
155
|
+
pattern?: string;
|
|
156
|
+
minItems?: number;
|
|
157
|
+
maxItems?: number;
|
|
158
|
+
uniqueItems?: boolean;
|
|
159
|
+
minProperties?: number;
|
|
160
|
+
maxProperties?: number;
|
|
161
|
+
additionalProperties?: boolean | JsonSchema;
|
|
162
|
+
format?: string;
|
|
163
|
+
$ref?: string;
|
|
164
|
+
$defs?: Record<string, JsonSchema>;
|
|
165
|
+
[key: string]: unknown;
|
|
166
|
+
}
|
|
167
|
+
interface SchemaAnalysis {
|
|
168
|
+
rootType: string;
|
|
169
|
+
fieldPaths: string[];
|
|
170
|
+
requiredFields: string[];
|
|
171
|
+
optionalFields: string[];
|
|
172
|
+
hasRecursion: boolean;
|
|
173
|
+
hasUnions: boolean;
|
|
174
|
+
hasArrays: boolean;
|
|
175
|
+
maxDepth: number;
|
|
176
|
+
complexity: number;
|
|
177
|
+
}
|
|
178
|
+
interface SchemaExample {
|
|
179
|
+
value: unknown;
|
|
180
|
+
description?: string;
|
|
181
|
+
isValid: boolean;
|
|
182
|
+
}
|
|
183
|
+
interface FieldInfo {
|
|
184
|
+
path: string;
|
|
185
|
+
name: string;
|
|
186
|
+
zodType: string;
|
|
187
|
+
jsonType: string;
|
|
188
|
+
description?: string;
|
|
189
|
+
required: boolean;
|
|
190
|
+
hasDefault: boolean;
|
|
191
|
+
defaultValue?: unknown;
|
|
192
|
+
constraints: FieldConstraint[];
|
|
193
|
+
children?: FieldInfo[];
|
|
194
|
+
}
|
|
195
|
+
interface FieldConstraint {
|
|
196
|
+
type: string;
|
|
197
|
+
value: unknown;
|
|
198
|
+
description: string;
|
|
199
|
+
}
|
|
200
|
+
interface SchemaWithExamples<T extends z.ZodType> {
|
|
201
|
+
schema: T;
|
|
202
|
+
examples: z.infer<T>[];
|
|
203
|
+
}
|
|
204
|
+
interface ToolDefinition {
|
|
205
|
+
type: 'function';
|
|
206
|
+
function: {
|
|
207
|
+
name: string;
|
|
208
|
+
description: string;
|
|
209
|
+
parameters: JsonSchema;
|
|
210
|
+
strict?: boolean;
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
interface ToolCall {
|
|
214
|
+
id: string;
|
|
215
|
+
name: string;
|
|
216
|
+
arguments: unknown;
|
|
217
|
+
}
|
|
218
|
+
interface SchemaValidationResult<T> {
|
|
219
|
+
success: boolean;
|
|
220
|
+
data?: T;
|
|
221
|
+
errors?: ValidationErrorDetail[];
|
|
222
|
+
}
|
|
223
|
+
interface ValidationErrorDetail {
|
|
224
|
+
path: (string | number)[];
|
|
225
|
+
message: string;
|
|
226
|
+
expected?: string;
|
|
227
|
+
received?: unknown;
|
|
228
|
+
code: string;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
interface ProviderCapabilities {
|
|
232
|
+
jsonMode: boolean;
|
|
233
|
+
strictJsonMode: boolean;
|
|
234
|
+
toolCalling: boolean;
|
|
235
|
+
streaming: boolean;
|
|
236
|
+
systemMessages: boolean;
|
|
237
|
+
maxContextWindow?: number;
|
|
238
|
+
maxOutputTokens?: number;
|
|
239
|
+
}
|
|
240
|
+
interface ProviderAdapter {
|
|
241
|
+
readonly name: string;
|
|
242
|
+
getCapabilities(model: string): ProviderCapabilities;
|
|
243
|
+
supportsJsonMode(model: string): boolean;
|
|
244
|
+
supportsToolCalling(model: string): boolean;
|
|
245
|
+
createCompletion(request: ProviderRequest): Promise<ProviderResponse>;
|
|
246
|
+
createStreamingCompletion(request: ProviderRequest): AsyncIterableIterator<ProviderStreamChunk>;
|
|
247
|
+
formatMessages(messages: ChatMessage[]): unknown;
|
|
248
|
+
formatJsonSchema(schema: JsonSchema): unknown;
|
|
249
|
+
formatToolDefinition(tool: ToolDefinition): unknown;
|
|
250
|
+
}
|
|
251
|
+
interface ProviderRequest {
|
|
252
|
+
model: string;
|
|
253
|
+
messages: ChatMessage[];
|
|
254
|
+
mode: ExtractionMode;
|
|
255
|
+
jsonSchema?: JsonSchema;
|
|
256
|
+
toolDefinition?: ToolDefinition;
|
|
257
|
+
temperature?: number;
|
|
258
|
+
maxTokens?: number;
|
|
259
|
+
stream?: boolean;
|
|
260
|
+
options?: Record<string, unknown>;
|
|
261
|
+
}
|
|
262
|
+
interface ProviderResponse {
|
|
263
|
+
content: string;
|
|
264
|
+
usage?: TokenUsage;
|
|
265
|
+
finishReason?: string;
|
|
266
|
+
toolCalls?: ProviderToolCall[];
|
|
267
|
+
raw?: unknown;
|
|
268
|
+
}
|
|
269
|
+
interface ProviderToolCall {
|
|
270
|
+
id: string;
|
|
271
|
+
name: string;
|
|
272
|
+
arguments: string;
|
|
273
|
+
}
|
|
274
|
+
interface ProviderStreamChunk {
|
|
275
|
+
content: string;
|
|
276
|
+
isFinal: boolean;
|
|
277
|
+
finishReason?: string;
|
|
278
|
+
usage?: TokenUsage;
|
|
279
|
+
toolCallDeltas?: ProviderToolCallDelta[];
|
|
280
|
+
}
|
|
281
|
+
interface ProviderToolCallDelta {
|
|
282
|
+
index: number;
|
|
283
|
+
id?: string;
|
|
284
|
+
name?: string;
|
|
285
|
+
arguments?: string;
|
|
286
|
+
}
|
|
287
|
+
interface OpenAIOptions {
|
|
288
|
+
organization?: string;
|
|
289
|
+
baseURL?: string;
|
|
290
|
+
responseFormat?: {
|
|
291
|
+
type: 'json_object' | 'json_schema';
|
|
292
|
+
json_schema?: {
|
|
293
|
+
name: string;
|
|
294
|
+
schema: JsonSchema;
|
|
295
|
+
strict?: boolean;
|
|
296
|
+
};
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
interface AnthropicOptions {
|
|
300
|
+
baseURL?: string;
|
|
301
|
+
metadata?: {
|
|
302
|
+
user_id?: string;
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
interface GoogleOptions {
|
|
306
|
+
generationConfig?: {
|
|
307
|
+
responseMimeType?: 'application/json';
|
|
308
|
+
responseSchema?: JsonSchema;
|
|
309
|
+
};
|
|
310
|
+
safetySettings?: unknown[];
|
|
311
|
+
}
|
|
312
|
+
type ProviderFactoryOptions = {
|
|
313
|
+
type: 'openai';
|
|
314
|
+
client: unknown;
|
|
315
|
+
options?: OpenAIOptions;
|
|
316
|
+
} | {
|
|
317
|
+
type: 'anthropic';
|
|
318
|
+
client: unknown;
|
|
319
|
+
options?: AnthropicOptions;
|
|
320
|
+
} | {
|
|
321
|
+
type: 'google';
|
|
322
|
+
client: unknown;
|
|
323
|
+
options?: GoogleOptions;
|
|
324
|
+
} | {
|
|
325
|
+
type: 'generic';
|
|
326
|
+
completionFn: GenericCompletionFn;
|
|
327
|
+
};
|
|
328
|
+
type GenericCompletionFn = (messages: ChatMessage[], options: GenericCompletionOptions) => Promise<string>;
|
|
329
|
+
interface GenericCompletionOptions {
|
|
330
|
+
model: string;
|
|
331
|
+
temperature?: number;
|
|
332
|
+
maxTokens?: number;
|
|
333
|
+
systemPrompt?: string;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
interface StreamingOptions {
|
|
337
|
+
yieldPartials?: boolean;
|
|
338
|
+
validatePartials?: boolean;
|
|
339
|
+
minFieldsBeforeYield?: number;
|
|
340
|
+
onFieldComplete?: (path: string, value: unknown) => void;
|
|
341
|
+
onPartial?: (partial: unknown, path: string | null) => void;
|
|
342
|
+
onError?: (error: Error) => void;
|
|
343
|
+
}
|
|
344
|
+
interface StreamingResult<T> {
|
|
345
|
+
partials(): AsyncIterableIterator<Partial<T>>;
|
|
346
|
+
fields(): AsyncIterableIterator<FieldUpdate>;
|
|
347
|
+
final(): Promise<T>;
|
|
348
|
+
onPartial(callback: (partial: Partial<T>, path: string | null) => void): void;
|
|
349
|
+
onField(callback: (path: string, value: unknown) => void): void;
|
|
350
|
+
onComplete(callback: (result: T) => void): void;
|
|
351
|
+
onError(callback: (error: Error) => void): void;
|
|
352
|
+
cancel(): void;
|
|
353
|
+
readonly isComplete: boolean;
|
|
354
|
+
readonly current: Partial<T>;
|
|
355
|
+
}
|
|
356
|
+
interface FieldUpdate {
|
|
357
|
+
path: string;
|
|
358
|
+
value: unknown;
|
|
359
|
+
complete: boolean;
|
|
360
|
+
timestamp: number;
|
|
361
|
+
}
|
|
362
|
+
interface PartialState<T> {
|
|
363
|
+
data: Partial<T>;
|
|
364
|
+
completedFields: string[];
|
|
365
|
+
inProgressFields: string[];
|
|
366
|
+
completionPercent: number;
|
|
367
|
+
isValid: boolean;
|
|
368
|
+
}
|
|
369
|
+
interface JsonToken {
|
|
370
|
+
type: JsonTokenType;
|
|
371
|
+
value: string;
|
|
372
|
+
path: string[];
|
|
373
|
+
complete: boolean;
|
|
374
|
+
}
|
|
375
|
+
type JsonTokenType = 'object_start' | 'object_end' | 'array_start' | 'array_end' | 'key' | 'string' | 'number' | 'boolean' | 'null' | 'colon' | 'comma';
|
|
376
|
+
interface ParserState {
|
|
377
|
+
currentPath: string[];
|
|
378
|
+
depth: number;
|
|
379
|
+
buffer: string;
|
|
380
|
+
partial: Record<string, unknown>;
|
|
381
|
+
inString: boolean;
|
|
382
|
+
inEscape: boolean;
|
|
383
|
+
currentString: string;
|
|
384
|
+
currentKey: string | null;
|
|
385
|
+
containerStack: ('object' | 'array')[];
|
|
386
|
+
}
|
|
387
|
+
interface StreamChunk {
|
|
388
|
+
content: string;
|
|
389
|
+
isFinal: boolean;
|
|
390
|
+
usage?: TokenUsage;
|
|
391
|
+
index: number;
|
|
392
|
+
}
|
|
393
|
+
interface StreamMetadata {
|
|
394
|
+
totalChunks: number;
|
|
395
|
+
totalChars: number;
|
|
396
|
+
startTime: number;
|
|
397
|
+
endTime?: number;
|
|
398
|
+
usage?: TokenUsage;
|
|
399
|
+
model?: string;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
interface StructuredClientEvents {
|
|
403
|
+
'extraction:start': {
|
|
404
|
+
requestId: string;
|
|
405
|
+
mode: ExtractionMode;
|
|
406
|
+
};
|
|
407
|
+
'extraction:attempt': {
|
|
408
|
+
requestId: string;
|
|
409
|
+
attempt: number;
|
|
410
|
+
mode: ExtractionMode;
|
|
411
|
+
};
|
|
412
|
+
'extraction:success': {
|
|
413
|
+
requestId: string;
|
|
414
|
+
data: unknown;
|
|
415
|
+
attempts: number;
|
|
416
|
+
};
|
|
417
|
+
'extraction:error': {
|
|
418
|
+
requestId: string;
|
|
419
|
+
error: Error;
|
|
420
|
+
attempt: number;
|
|
421
|
+
};
|
|
422
|
+
'extraction:retry': {
|
|
423
|
+
requestId: string;
|
|
424
|
+
attempt: number;
|
|
425
|
+
reason: string;
|
|
426
|
+
hints: string[];
|
|
427
|
+
};
|
|
428
|
+
'validation:failed': {
|
|
429
|
+
requestId: string;
|
|
430
|
+
errors: ValidationError[];
|
|
431
|
+
};
|
|
432
|
+
'mode:switch': {
|
|
433
|
+
requestId: string;
|
|
434
|
+
from: ExtractionMode;
|
|
435
|
+
to: ExtractionMode;
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
declare class StructuredClient extends EventEmitter<StructuredClientEvents> {
|
|
439
|
+
private readonly provider;
|
|
440
|
+
private readonly config;
|
|
441
|
+
constructor(provider: ProviderAdapter, config?: Partial<StructuredClientConfig>);
|
|
442
|
+
extract<T extends z.ZodType>(options: StructuredRequestOptions<T>): Promise<ExtractionResult<z.infer<T>>>;
|
|
443
|
+
extractStream<T extends z.ZodType>(options: StructuredRequestOptions<T>, streamingOptions?: StreamingOptions): Promise<StreamingResult<z.infer<T>>>;
|
|
444
|
+
private executeExtraction;
|
|
445
|
+
private extractWithJsonMode;
|
|
446
|
+
private extractWithToolMode;
|
|
447
|
+
private extractWithPromptMode;
|
|
448
|
+
private parseResponse;
|
|
449
|
+
private extractJsonFromContent;
|
|
450
|
+
private addSchemaPromptToMessages;
|
|
451
|
+
private addFixHintsToMessages;
|
|
452
|
+
private resolveMode;
|
|
453
|
+
private shouldRetry;
|
|
454
|
+
private classifyError;
|
|
455
|
+
private shouldSwitchMode;
|
|
456
|
+
private getNextMode;
|
|
457
|
+
private calculateBackoff;
|
|
458
|
+
private delay;
|
|
459
|
+
private generateRequestId;
|
|
460
|
+
private createMetadata;
|
|
461
|
+
private createStructuredError;
|
|
462
|
+
getProviderCapabilities(model: string): ProviderCapabilities;
|
|
463
|
+
supportsMode(mode: ExtractionMode, model: string): boolean;
|
|
464
|
+
}
|
|
465
|
+
declare function createStructuredClient(provider: ProviderAdapter, config?: Partial<StructuredClientConfig>): StructuredClient;
|
|
466
|
+
|
|
467
|
+
declare function zodToJsonSchema<T extends z.ZodType>(schema: T): JsonSchema;
|
|
468
|
+
declare function schemaToPrompt<T extends z.ZodType>(schema: T, options?: SchemaPromptOptions): SchemaPrompt;
|
|
469
|
+
declare function analyzeSchema<T extends z.ZodType>(schema: T, opts: Required<SchemaPromptOptions>, path?: string): FieldInfo[];
|
|
470
|
+
declare function extractFieldInfo(name: string, schema: z.ZodType, path: string, opts: Required<SchemaPromptOptions>): FieldInfo;
|
|
471
|
+
declare function generateExample(schema: JsonSchema, maxDepth: number, depth?: number): unknown;
|
|
472
|
+
declare const SchemaPromptGenerator: {
|
|
473
|
+
toJsonSchema: <T extends z.ZodType>(schema: T, options?: SchemaPromptOptions) => SchemaPrompt;
|
|
474
|
+
toTypeScript: <T extends z.ZodType>(schema: T, options?: SchemaPromptOptions) => SchemaPrompt;
|
|
475
|
+
toNatural: <T extends z.ZodType>(schema: T, options?: SchemaPromptOptions) => SchemaPrompt;
|
|
476
|
+
toExamples: <T extends z.ZodType>(schema: T, options?: SchemaPromptOptions) => SchemaPrompt;
|
|
477
|
+
generateExample: typeof generateExample;
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
declare function validateSchema<T extends z.ZodType>(schema: T, data: unknown): SchemaValidationResult<z.infer<T>>;
|
|
481
|
+
declare function validateSchemaOrThrow<T extends z.ZodType>(schema: T, data: unknown): z.infer<T>;
|
|
482
|
+
declare function validatePartial<T extends z.ZodType>(schema: T, data: unknown, options?: {
|
|
483
|
+
requiredFields?: string[];
|
|
484
|
+
}): SchemaValidationResult<Partial<z.infer<T>>>;
|
|
485
|
+
declare function matchesSchema<T extends z.ZodType>(schema: T, data: unknown): boolean;
|
|
486
|
+
declare function coerceToSchema<T extends z.ZodType>(schema: T, data: unknown): SchemaValidationResult<z.infer<T>>;
|
|
487
|
+
declare function getValidationHints<T extends z.ZodType>(schema: T, data: unknown): string[];
|
|
488
|
+
declare function formatZodErrors(error: ZodError): ValidationErrorDetail[];
|
|
489
|
+
declare const SchemaValidator: {
|
|
490
|
+
validate: typeof validateSchema;
|
|
491
|
+
validateOrThrow: typeof validateSchemaOrThrow;
|
|
492
|
+
validatePartial: typeof validatePartial;
|
|
493
|
+
matches: typeof matchesSchema;
|
|
494
|
+
coerce: typeof coerceToSchema;
|
|
495
|
+
getHints: typeof getValidationHints;
|
|
496
|
+
formatErrors: typeof formatZodErrors;
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
declare function createStreamingResult<T extends z.ZodType>(_client: StructuredClient, provider: ProviderAdapter, options: StructuredRequestOptions<T>, streamingOptions?: StreamingOptions): StreamingResult<z.infer<T>>;
|
|
500
|
+
declare function getPartialState<T>(schema: z.ZodType<T>, partial: Partial<T>): PartialState<T>;
|
|
501
|
+
|
|
502
|
+
interface FieldParseUpdate {
|
|
503
|
+
path: string[];
|
|
504
|
+
value: unknown;
|
|
505
|
+
complete: boolean;
|
|
506
|
+
}
|
|
507
|
+
declare class IncrementalJsonParser {
|
|
508
|
+
private state;
|
|
509
|
+
private result;
|
|
510
|
+
private pendingUpdates;
|
|
511
|
+
constructor();
|
|
512
|
+
feed(chunk: string): FieldParseUpdate[];
|
|
513
|
+
getResult(): Record<string, unknown>;
|
|
514
|
+
reset(): void;
|
|
515
|
+
private createInitialState;
|
|
516
|
+
private parse;
|
|
517
|
+
private parseStringChar;
|
|
518
|
+
private parseChar;
|
|
519
|
+
private processEscape;
|
|
520
|
+
private handleStringComplete;
|
|
521
|
+
private handleObjectStart;
|
|
522
|
+
private handleObjectEnd;
|
|
523
|
+
private handleArrayStart;
|
|
524
|
+
private handleArrayEnd;
|
|
525
|
+
private parseBoolean;
|
|
526
|
+
private parseNull;
|
|
527
|
+
private parseNumber;
|
|
528
|
+
private setValue;
|
|
529
|
+
private getCurrentContainer;
|
|
530
|
+
private setNestedValue;
|
|
531
|
+
private getNestedValue;
|
|
532
|
+
private finalize;
|
|
533
|
+
}
|
|
534
|
+
declare function tokenizeJson(json: string): JsonToken[];
|
|
535
|
+
|
|
536
|
+
interface OpenAIClient {
|
|
537
|
+
chat: {
|
|
538
|
+
completions: {
|
|
539
|
+
create(params: unknown): Promise<OpenAIChatCompletion>;
|
|
540
|
+
};
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
interface OpenAIChatCompletion {
|
|
544
|
+
id: string;
|
|
545
|
+
choices: Array<{
|
|
546
|
+
message: {
|
|
547
|
+
role: string;
|
|
548
|
+
content: string | null;
|
|
549
|
+
tool_calls?: Array<{
|
|
550
|
+
id: string;
|
|
551
|
+
type: string;
|
|
552
|
+
function: {
|
|
553
|
+
name: string;
|
|
554
|
+
arguments: string;
|
|
555
|
+
};
|
|
556
|
+
}>;
|
|
557
|
+
};
|
|
558
|
+
finish_reason: string;
|
|
559
|
+
}>;
|
|
560
|
+
usage?: {
|
|
561
|
+
prompt_tokens: number;
|
|
562
|
+
completion_tokens: number;
|
|
563
|
+
total_tokens: number;
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
declare class OpenAIAdapter implements ProviderAdapter {
|
|
567
|
+
readonly name = "openai";
|
|
568
|
+
private readonly client;
|
|
569
|
+
constructor(client: OpenAIClient, _options?: OpenAIOptions);
|
|
570
|
+
getCapabilities(model: string): ProviderCapabilities;
|
|
571
|
+
supportsJsonMode(model: string): boolean;
|
|
572
|
+
supportsToolCalling(model: string): boolean;
|
|
573
|
+
createCompletion(request: ProviderRequest): Promise<ProviderResponse>;
|
|
574
|
+
createStreamingCompletion(request: ProviderRequest): AsyncIterableIterator<ProviderStreamChunk>;
|
|
575
|
+
formatMessages(messages: ChatMessage[]): unknown;
|
|
576
|
+
formatJsonSchema(schema: JsonSchema): unknown;
|
|
577
|
+
formatToolDefinition(tool: ToolDefinition): unknown;
|
|
578
|
+
private buildRequestParams;
|
|
579
|
+
private parseResponse;
|
|
580
|
+
}
|
|
581
|
+
declare function createOpenAIAdapter(client: OpenAIClient, options?: OpenAIOptions): OpenAIAdapter;
|
|
582
|
+
|
|
583
|
+
interface AnthropicClient {
|
|
584
|
+
messages: {
|
|
585
|
+
create(params: unknown): Promise<AnthropicMessage>;
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
interface AnthropicMessage {
|
|
589
|
+
id: string;
|
|
590
|
+
type: string;
|
|
591
|
+
role: string;
|
|
592
|
+
content: Array<{
|
|
593
|
+
type: 'text' | 'tool_use';
|
|
594
|
+
text?: string;
|
|
595
|
+
id?: string;
|
|
596
|
+
name?: string;
|
|
597
|
+
input?: unknown;
|
|
598
|
+
}>;
|
|
599
|
+
model: string;
|
|
600
|
+
stop_reason: string | null;
|
|
601
|
+
stop_sequence: string | null;
|
|
602
|
+
usage: {
|
|
603
|
+
input_tokens: number;
|
|
604
|
+
output_tokens: number;
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
declare class AnthropicAdapter implements ProviderAdapter {
|
|
608
|
+
readonly name = "anthropic";
|
|
609
|
+
private readonly client;
|
|
610
|
+
private readonly options;
|
|
611
|
+
constructor(client: AnthropicClient, options?: AnthropicOptions);
|
|
612
|
+
getCapabilities(model: string): ProviderCapabilities;
|
|
613
|
+
supportsJsonMode(_model: string): boolean;
|
|
614
|
+
supportsToolCalling(model: string): boolean;
|
|
615
|
+
createCompletion(request: ProviderRequest): Promise<ProviderResponse>;
|
|
616
|
+
createStreamingCompletion(request: ProviderRequest): AsyncIterableIterator<ProviderStreamChunk>;
|
|
617
|
+
formatMessages(messages: ChatMessage[]): unknown;
|
|
618
|
+
formatJsonSchema(schema: JsonSchema): unknown;
|
|
619
|
+
formatToolDefinition(tool: ToolDefinition): unknown;
|
|
620
|
+
private buildRequestParams;
|
|
621
|
+
private parseResponse;
|
|
622
|
+
}
|
|
623
|
+
declare function createAnthropicAdapter(client: AnthropicClient, options?: AnthropicOptions): AnthropicAdapter;
|
|
624
|
+
|
|
625
|
+
interface GoogleClient {
|
|
626
|
+
getGenerativeModel(params: {
|
|
627
|
+
model: string;
|
|
628
|
+
generationConfig?: unknown;
|
|
629
|
+
tools?: unknown[];
|
|
630
|
+
toolConfig?: unknown;
|
|
631
|
+
}): GoogleGenerativeModel;
|
|
632
|
+
}
|
|
633
|
+
interface GoogleGenerativeModel {
|
|
634
|
+
generateContent(request: unknown): Promise<GoogleGenerateContentResult>;
|
|
635
|
+
generateContentStream(request: unknown): Promise<GoogleGenerateContentStreamResult>;
|
|
636
|
+
}
|
|
637
|
+
interface GoogleGenerateContentResult {
|
|
638
|
+
response: GoogleGenerateContentResponse;
|
|
639
|
+
}
|
|
640
|
+
interface GoogleGenerateContentResponse {
|
|
641
|
+
text(): string;
|
|
642
|
+
candidates?: Array<{
|
|
643
|
+
content: {
|
|
644
|
+
parts: Array<{
|
|
645
|
+
text?: string;
|
|
646
|
+
functionCall?: {
|
|
647
|
+
name: string;
|
|
648
|
+
args: Record<string, unknown>;
|
|
649
|
+
};
|
|
650
|
+
}>;
|
|
651
|
+
role: string;
|
|
652
|
+
};
|
|
653
|
+
finishReason?: string;
|
|
654
|
+
}>;
|
|
655
|
+
usageMetadata?: {
|
|
656
|
+
promptTokenCount: number;
|
|
657
|
+
candidatesTokenCount: number;
|
|
658
|
+
totalTokenCount: number;
|
|
659
|
+
};
|
|
660
|
+
functionCalls?(): Array<{
|
|
661
|
+
name: string;
|
|
662
|
+
args: Record<string, unknown>;
|
|
663
|
+
}>;
|
|
664
|
+
}
|
|
665
|
+
interface GoogleGenerateContentStreamResult {
|
|
666
|
+
stream: AsyncIterable<{
|
|
667
|
+
text(): string;
|
|
668
|
+
candidates?: Array<{
|
|
669
|
+
content: {
|
|
670
|
+
parts: Array<{
|
|
671
|
+
text?: string;
|
|
672
|
+
functionCall?: {
|
|
673
|
+
name: string;
|
|
674
|
+
args: Record<string, unknown>;
|
|
675
|
+
};
|
|
676
|
+
}>;
|
|
677
|
+
};
|
|
678
|
+
finishReason?: string;
|
|
679
|
+
}>;
|
|
680
|
+
usageMetadata?: {
|
|
681
|
+
promptTokenCount: number;
|
|
682
|
+
candidatesTokenCount: number;
|
|
683
|
+
totalTokenCount: number;
|
|
684
|
+
};
|
|
685
|
+
}>;
|
|
686
|
+
response: Promise<GoogleGenerateContentResponse>;
|
|
687
|
+
}
|
|
688
|
+
declare class GoogleAdapter implements ProviderAdapter {
|
|
689
|
+
readonly name = "google";
|
|
690
|
+
private readonly client;
|
|
691
|
+
private readonly options;
|
|
692
|
+
constructor(client: GoogleClient, options?: GoogleOptions);
|
|
693
|
+
getCapabilities(model: string): ProviderCapabilities;
|
|
694
|
+
supportsJsonMode(model: string): boolean;
|
|
695
|
+
supportsToolCalling(model: string): boolean;
|
|
696
|
+
createCompletion(request: ProviderRequest): Promise<ProviderResponse>;
|
|
697
|
+
createStreamingCompletion(request: ProviderRequest): AsyncIterableIterator<ProviderStreamChunk>;
|
|
698
|
+
formatMessages(messages: ChatMessage[]): unknown;
|
|
699
|
+
formatJsonSchema(schema: JsonSchema): unknown;
|
|
700
|
+
formatToolDefinition(tool: ToolDefinition): unknown;
|
|
701
|
+
private getModel;
|
|
702
|
+
private formatContents;
|
|
703
|
+
private convertToGoogleSchema;
|
|
704
|
+
private parseResponse;
|
|
705
|
+
}
|
|
706
|
+
declare function createGoogleAdapter(client: GoogleClient, options?: GoogleOptions): GoogleAdapter;
|
|
707
|
+
|
|
708
|
+
type AgentSeaProviderType = 'openai' | 'anthropic' | 'google';
|
|
709
|
+
interface AgentSeaClient {
|
|
710
|
+
readonly provider: AgentSeaProviderType;
|
|
711
|
+
readonly client: unknown;
|
|
712
|
+
readonly model?: string;
|
|
713
|
+
}
|
|
714
|
+
interface StructuredProviderOptions {
|
|
715
|
+
defaultModel?: string;
|
|
716
|
+
defaultMode?: ExtractionModeConfig;
|
|
717
|
+
enableFixHints?: boolean;
|
|
718
|
+
validatePartials?: boolean;
|
|
719
|
+
maxRetries?: number;
|
|
720
|
+
}
|
|
721
|
+
declare class StructuredProvider {
|
|
722
|
+
private readonly clients;
|
|
723
|
+
private readonly adapters;
|
|
724
|
+
private readonly options;
|
|
725
|
+
private defaultProviderName;
|
|
726
|
+
constructor(options?: StructuredProviderOptions);
|
|
727
|
+
registerProvider(name: string, agentSeaClient: AgentSeaClient, setAsDefault?: boolean): this;
|
|
728
|
+
getClient(name?: string): StructuredClient;
|
|
729
|
+
extract<T extends z.ZodType>(schema: T, prompt: string | ChatMessage[], options?: {
|
|
730
|
+
model?: string;
|
|
731
|
+
provider?: string;
|
|
732
|
+
mode?: ExtractionModeConfig;
|
|
733
|
+
temperature?: number;
|
|
734
|
+
maxTokens?: number;
|
|
735
|
+
}): Promise<ExtractionResult<z.infer<T>>>;
|
|
736
|
+
extractStream<T extends z.ZodType>(schema: T, prompt: string | ChatMessage[], options?: {
|
|
737
|
+
model?: string;
|
|
738
|
+
provider?: string;
|
|
739
|
+
mode?: ExtractionModeConfig;
|
|
740
|
+
streaming?: StreamingOptions;
|
|
741
|
+
}): Promise<StreamingResult<z.infer<T>>>;
|
|
742
|
+
createExtractor<T extends z.ZodType>(schema: T, options?: {
|
|
743
|
+
model?: string;
|
|
744
|
+
provider?: string;
|
|
745
|
+
mode?: ExtractionModeConfig;
|
|
746
|
+
}): TypedExtractor<z.infer<T>>;
|
|
747
|
+
private createAdapter;
|
|
748
|
+
}
|
|
749
|
+
declare class TypedExtractor<T> {
|
|
750
|
+
private readonly provider;
|
|
751
|
+
private readonly schema;
|
|
752
|
+
private readonly options?;
|
|
753
|
+
constructor(provider: StructuredProvider, schema: z.ZodType<T>, options?: {
|
|
754
|
+
model?: string;
|
|
755
|
+
provider?: string;
|
|
756
|
+
mode?: ExtractionModeConfig;
|
|
757
|
+
} | undefined);
|
|
758
|
+
extract(prompt: string | ChatMessage[], overrideOptions?: {
|
|
759
|
+
model?: string;
|
|
760
|
+
temperature?: number;
|
|
761
|
+
maxTokens?: number;
|
|
762
|
+
}): Promise<ExtractionResult<T>>;
|
|
763
|
+
extractStream(prompt: string | ChatMessage[], streamingOptions?: StreamingOptions): Promise<StreamingResult<T>>;
|
|
764
|
+
getSchema(): z.ZodType<T>;
|
|
765
|
+
}
|
|
766
|
+
declare function createStructuredProvider(options?: StructuredProviderOptions): StructuredProvider;
|
|
767
|
+
declare const Extractors: {
|
|
768
|
+
list<T extends z.ZodType>(itemSchema: T, options?: {
|
|
769
|
+
minItems?: number;
|
|
770
|
+
maxItems?: number;
|
|
771
|
+
}): z.ZodArray<T>;
|
|
772
|
+
entity<T extends z.ZodRawShape>(shape: T): z.ZodObject<T>;
|
|
773
|
+
classification<T extends [string, ...string[]]>(categories: T, options?: {
|
|
774
|
+
confidence?: boolean;
|
|
775
|
+
}): z.ZodType;
|
|
776
|
+
sentiment(): z.ZodType;
|
|
777
|
+
keyValue(): z.ZodType;
|
|
778
|
+
summary(options?: {
|
|
779
|
+
maxLength?: number;
|
|
780
|
+
}): z.ZodType;
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
export { type AgentSeaProviderType, AnthropicAdapter, type AnthropicOptions, type ChatMessage, type ExtractionAttempt, type ExtractionMetadata, type ExtractionMode, type ExtractionModeConfig, type ExtractionResult, Extractors, type FieldConstraint, type FieldInfo, type FieldParseUpdate, type FieldUpdate, type FixHintConfig, type GenericCompletionFn, type GenericCompletionOptions, GoogleAdapter, type GoogleOptions, type HybridModeConfig, IncrementalJsonParser, type JsonModeConfig, type JsonSchema, type JsonToken, type JsonTokenType, type MessageRole, OpenAIAdapter, type OpenAIOptions, type ParserState, type PartialState, type PromptModeConfig, type ProviderAdapter, type ProviderCapabilities, type ProviderFactoryOptions, type ProviderRequest, type ProviderResponse, type ProviderStreamChunk, type ProviderToolCall, type ProviderToolCallDelta, type RecoveryConfig, type RetryCondition, type RetryConfig, type SchemaAnalysis, type SchemaExample, type SchemaFormat, type SchemaPrompt, SchemaPromptGenerator, type SchemaPromptOptions, type SchemaValidationResult, SchemaValidator, type SchemaWithExamples, type StreamChunk, type StreamMetadata, type StreamingOptions, type StreamingResult, StructuredClient, type StructuredClientConfig, StructuredError, StructuredProvider, type StructuredProviderOptions, type StructuredProvider$1 as StructuredProviderType, type StructuredRequestOptions, type TokenUsage, type ToolCall, type ToolDefinition, type ToolModeConfig, TypedExtractor, type ValidationError, type ValidationErrorDetail, analyzeSchema, coerceToSchema, createAnthropicAdapter, createGoogleAdapter, createOpenAIAdapter, createStreamingResult, createStructuredClient, createStructuredProvider, extractFieldInfo, formatZodErrors, generateExample, getPartialState, getValidationHints, matchesSchema, schemaToPrompt, tokenizeJson, validatePartial, validateSchema, validateSchemaOrThrow, zodToJsonSchema };
|