@goreal-ai/echo-pdk 0.7.1 → 0.8.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/README.md +1 -1
- package/dist/ai-judge/index.d.ts +11 -21
- package/dist/ai-judge/index.d.ts.map +1 -1
- package/dist/ai-judge/index.js +36 -90
- package/dist/ai-judge/index.js.map +1 -1
- package/dist/embeddings/cosine.d.ts +15 -0
- package/dist/embeddings/cosine.d.ts.map +1 -0
- package/dist/embeddings/cosine.js +37 -0
- package/dist/embeddings/cosine.js.map +1 -0
- package/dist/embeddings/index.d.ts +9 -0
- package/dist/embeddings/index.d.ts.map +1 -0
- package/dist/embeddings/index.js +11 -0
- package/dist/embeddings/index.js.map +1 -0
- package/dist/embeddings/openai.d.ts +11 -0
- package/dist/embeddings/openai.d.ts.map +1 -0
- package/dist/embeddings/openai.js +38 -0
- package/dist/embeddings/openai.js.map +1 -0
- package/dist/embeddings/registry.d.ts +13 -0
- package/dist/embeddings/registry.d.ts.map +1 -0
- package/dist/embeddings/registry.js +29 -0
- package/dist/embeddings/registry.js.map +1 -0
- package/dist/embeddings/types.d.ts +35 -0
- package/dist/embeddings/types.d.ts.map +1 -0
- package/dist/embeddings/types.js +8 -0
- package/dist/embeddings/types.js.map +1 -0
- package/dist/embeddings/voyage.d.ts +12 -0
- package/dist/embeddings/voyage.d.ts.map +1 -0
- package/dist/embeddings/voyage.js +39 -0
- package/dist/embeddings/voyage.js.map +1 -0
- package/dist/eval/assertions.d.ts +35 -0
- package/dist/eval/assertions.d.ts.map +1 -0
- package/dist/eval/assertions.js +349 -0
- package/dist/eval/assertions.js.map +1 -0
- package/dist/eval/dataset.d.ts +42 -0
- package/dist/eval/dataset.d.ts.map +1 -0
- package/dist/eval/dataset.js +101 -0
- package/dist/eval/dataset.js.map +1 -0
- package/dist/eval/index.d.ts +14 -0
- package/dist/eval/index.d.ts.map +1 -0
- package/dist/eval/index.js +17 -0
- package/dist/eval/index.js.map +1 -0
- package/dist/eval/loader.d.ts +30 -0
- package/dist/eval/loader.d.ts.map +1 -0
- package/dist/eval/loader.js +170 -0
- package/dist/eval/loader.js.map +1 -0
- package/dist/eval/reporter.d.ts +26 -0
- package/dist/eval/reporter.d.ts.map +1 -0
- package/dist/eval/reporter.js +164 -0
- package/dist/eval/reporter.js.map +1 -0
- package/dist/eval/runner.d.ts +28 -0
- package/dist/eval/runner.d.ts.map +1 -0
- package/dist/eval/runner.js +232 -0
- package/dist/eval/runner.js.map +1 -0
- package/dist/eval/types.d.ts +257 -0
- package/dist/eval/types.d.ts.map +1 -0
- package/dist/eval/types.js +11 -0
- package/dist/eval/types.js.map +1 -0
- package/dist/evaluator/evaluator.d.ts +2 -2
- package/dist/evaluator/evaluator.js +5 -5
- package/dist/evaluator/evaluator.js.map +1 -1
- package/dist/evaluator/index.d.ts +1 -1
- package/dist/evaluator/index.d.ts.map +1 -1
- package/dist/evaluator/index.js +1 -1
- package/dist/evaluator/index.js.map +1 -1
- package/dist/evaluator/operators.d.ts +9 -5
- package/dist/evaluator/operators.d.ts.map +1 -1
- package/dist/evaluator/operators.js +26 -33
- package/dist/evaluator/operators.js.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +38 -20
- package/dist/index.js.map +1 -1
- package/dist/parser/ast.js +1 -1
- package/dist/parser/ast.js.map +1 -1
- package/dist/parser/lexer.d.ts +1 -1
- package/dist/parser/lexer.js +1 -1
- package/dist/project/index.d.ts.map +1 -1
- package/dist/project/index.js +10 -3
- package/dist/project/index.js.map +1 -1
- package/dist/project/types.d.ts +19 -4
- package/dist/project/types.d.ts.map +1 -1
- package/dist/project/types.js +3 -0
- package/dist/project/types.js.map +1 -1
- package/dist/providers/anthropic.d.ts +18 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +151 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/base.d.ts +45 -0
- package/dist/providers/base.d.ts.map +1 -0
- package/dist/providers/base.js +107 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/index.d.ts +14 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +16 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai.d.ts +18 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +109 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/registry.d.ts +80 -0
- package/dist/providers/registry.d.ts.map +1 -0
- package/dist/providers/registry.js +118 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/providers/run-prompt.d.ts +69 -0
- package/dist/providers/run-prompt.d.ts.map +1 -0
- package/dist/providers/run-prompt.js +79 -0
- package/dist/providers/run-prompt.js.map +1 -0
- package/dist/providers/types.d.ts +125 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +9 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/types.d.ts +5 -5
- package/dist/types.d.ts.map +1 -1
- package/package.json +10 -7
- package/scripts/bundle-for-graaljs.mjs +45 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Shared provider utilities
|
|
3
|
+
*
|
|
4
|
+
* Common functions used by all provider implementations:
|
|
5
|
+
* - HTTP helpers (fetchWithTimeout, extractApiError)
|
|
6
|
+
* - Judge/similarity prompt builders and parsers
|
|
7
|
+
* - toLLMProvider adapter for backward compatibility with eval system
|
|
8
|
+
*/
|
|
9
|
+
// =============================================================================
|
|
10
|
+
// HTTP HELPERS
|
|
11
|
+
// =============================================================================
|
|
12
|
+
/**
|
|
13
|
+
* Fetch with an AbortController-based timeout.
|
|
14
|
+
*/
|
|
15
|
+
export async function fetchWithTimeout(url, init, timeoutMs) {
|
|
16
|
+
const controller = new AbortController();
|
|
17
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
18
|
+
try {
|
|
19
|
+
return await fetch(url, { ...init, signal: controller.signal });
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
clearTimeout(timeoutId);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Extract a human-readable error message from an API response.
|
|
27
|
+
*/
|
|
28
|
+
export async function extractApiError(response, providerName) {
|
|
29
|
+
const body = await response.text();
|
|
30
|
+
return `${providerName} API error (${response.status}): ${body}`;
|
|
31
|
+
}
|
|
32
|
+
// =============================================================================
|
|
33
|
+
// JUDGE PROMPTS
|
|
34
|
+
// =============================================================================
|
|
35
|
+
/**
|
|
36
|
+
* Build the prompt for LLM-as-judge evaluation.
|
|
37
|
+
*/
|
|
38
|
+
export function buildJudgePrompt(response, question) {
|
|
39
|
+
return `You are evaluating an LLM response. First provide a brief reasoning (1-2 sentences), then on a new line answer exactly "VERDICT: YES" or "VERDICT: NO".
|
|
40
|
+
|
|
41
|
+
Response to evaluate:
|
|
42
|
+
${response}
|
|
43
|
+
|
|
44
|
+
Question: ${question}`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Parse a judge response into pass/fail + reasoning.
|
|
48
|
+
*/
|
|
49
|
+
export function parseJudgeResponse(text) {
|
|
50
|
+
const trimmed = text.trim();
|
|
51
|
+
const pass = trimmed.toUpperCase().includes('VERDICT: YES');
|
|
52
|
+
const reasoning = trimmed
|
|
53
|
+
.replace(/\n?\s*VERDICT:\s*(YES|NO)\s*$/i, '')
|
|
54
|
+
.trim();
|
|
55
|
+
return { pass, reasoning };
|
|
56
|
+
}
|
|
57
|
+
// =============================================================================
|
|
58
|
+
// SIMILARITY PROMPTS
|
|
59
|
+
// =============================================================================
|
|
60
|
+
/**
|
|
61
|
+
* Build the prompt for semantic similarity scoring.
|
|
62
|
+
*/
|
|
63
|
+
export function buildSimilarityPrompt(textA, textB) {
|
|
64
|
+
return `Rate the semantic similarity between these two texts on a scale of 0.0 to 1.0. Answer with only a number.
|
|
65
|
+
|
|
66
|
+
Text A:
|
|
67
|
+
${textA}
|
|
68
|
+
|
|
69
|
+
Text B:
|
|
70
|
+
${textB}`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Parse a similarity response into a 0.0-1.0 score.
|
|
74
|
+
*/
|
|
75
|
+
export function parseSimilarityResponse(text) {
|
|
76
|
+
const score = parseFloat(text.trim());
|
|
77
|
+
return isNaN(score) ? 0 : Math.max(0, Math.min(1, score));
|
|
78
|
+
}
|
|
79
|
+
// =============================================================================
|
|
80
|
+
// ADAPTER
|
|
81
|
+
// =============================================================================
|
|
82
|
+
/**
|
|
83
|
+
* Adapt an AIProviderInstance to the eval system's LLMProvider interface.
|
|
84
|
+
*
|
|
85
|
+
* The eval runner uses LLMProvider with `complete(prompt, model?)`.
|
|
86
|
+
* This bridges the gap so the new provider system works with existing eval code.
|
|
87
|
+
*/
|
|
88
|
+
export function toLLMProvider(instance) {
|
|
89
|
+
return {
|
|
90
|
+
async complete(prompt, model) {
|
|
91
|
+
const response = await instance.complete([{ role: 'user', content: prompt }], { model });
|
|
92
|
+
return {
|
|
93
|
+
text: response.text,
|
|
94
|
+
tokens: response.tokens,
|
|
95
|
+
latencyMs: response.latencyMs,
|
|
96
|
+
model: response.model,
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
async judge(response, question) {
|
|
100
|
+
return instance.judge(response, question);
|
|
101
|
+
},
|
|
102
|
+
async similarity(textA, textB) {
|
|
103
|
+
return instance.similarity(textA, textB);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/providers/base.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,gFAAgF;AAChF,eAAe;AACf,gFAAgF;AAEhF;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,GAAW,EACX,IAAiB,EACjB,SAAiB;IAEjB,MAAM,UAAU,GAA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ified AI provider system for echo-pdk.
|
|
5
|
+
* Provides discovery, model listing, completion, and high-level prompt execution.
|
|
6
|
+
*/
|
|
7
|
+
export type { ProviderType, ProviderInfo, ProviderConfig, ModelInfo, ChatMessage, CompletionResponse, CompletionOptions, AIProviderInstance, } from './types.js';
|
|
8
|
+
export { isProviderType, getProviders, getProvider, createProvider, listModels, } from './registry.js';
|
|
9
|
+
export { createOpenAIProvider } from './openai.js';
|
|
10
|
+
export { createAnthropicProvider } from './anthropic.js';
|
|
11
|
+
export { runPrompt } from './run-prompt.js';
|
|
12
|
+
export type { RunPromptOptions, RunPromptResult } from './run-prompt.js';
|
|
13
|
+
export { toLLMProvider } from './base.js';
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,SAAS,EACT,WAAW,EACX,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,cAAc,EACd,YAAY,EACZ,WAAW,EACX,cAAc,EACd,UAAU,GACX,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAGzD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAGzE,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Providers module - barrel exports
|
|
3
|
+
*
|
|
4
|
+
* Unified AI provider system for echo-pdk.
|
|
5
|
+
* Provides discovery, model listing, completion, and high-level prompt execution.
|
|
6
|
+
*/
|
|
7
|
+
// Registry
|
|
8
|
+
export { isProviderType, getProviders, getProvider, createProvider, listModels, } from './registry.js';
|
|
9
|
+
// Provider factories (for direct use)
|
|
10
|
+
export { createOpenAIProvider } from './openai.js';
|
|
11
|
+
export { createAnthropicProvider } from './anthropic.js';
|
|
12
|
+
// High-level API
|
|
13
|
+
export { runPrompt } from './run-prompt.js';
|
|
14
|
+
// Utilities (for advanced use / testing)
|
|
15
|
+
export { toLLMProvider } from './base.js';
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAcH,WAAW;AACX,OAAO,EACL,cAAc,EACd,YAAY,EACZ,WAAW,EACX,cAAc,EACd,UAAU,GACX,MAAM,eAAe,CAAC;AAEvB,sCAAsC;AACtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAEzD,iBAAiB;AACjB,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,yCAAyC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview OpenAI provider implementation
|
|
3
|
+
*
|
|
4
|
+
* Implements AIProviderInstance for OpenAI's API:
|
|
5
|
+
* - POST /v1/chat/completions for completions
|
|
6
|
+
* - GET /v1/models for model listing
|
|
7
|
+
* - Judge and similarity via shared prompts
|
|
8
|
+
*/
|
|
9
|
+
import type { AIProviderInstance, ProviderConfig, ProviderInfo } from './types.js';
|
|
10
|
+
export declare const OPENAI_INFO: ProviderInfo;
|
|
11
|
+
/**
|
|
12
|
+
* Create an OpenAI provider instance.
|
|
13
|
+
*
|
|
14
|
+
* @param config - Provider configuration with API key
|
|
15
|
+
* @returns AIProviderInstance for OpenAI
|
|
16
|
+
*/
|
|
17
|
+
export declare function createOpenAIProvider(config: ProviderConfig): AIProviderInstance;
|
|
18
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/providers/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,kBAAkB,EAGlB,cAAc,EACd,YAAY,EACb,MAAM,YAAY,CAAC;AAcpB,eAAO,MAAM,WAAW,EAAE,YAMR,CAAC;AA8BnB;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,cAAc,GAAG,kBAAkB,CA2G/E"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview OpenAI provider implementation
|
|
3
|
+
*
|
|
4
|
+
* Implements AIProviderInstance for OpenAI's API:
|
|
5
|
+
* - POST /v1/chat/completions for completions
|
|
6
|
+
* - GET /v1/models for model listing
|
|
7
|
+
* - Judge and similarity via shared prompts
|
|
8
|
+
*/
|
|
9
|
+
import { fetchWithTimeout, extractApiError, buildJudgePrompt, parseJudgeResponse, buildSimilarityPrompt, parseSimilarityResponse, } from './base.js';
|
|
10
|
+
// =============================================================================
|
|
11
|
+
// PROVIDER INFO
|
|
12
|
+
// =============================================================================
|
|
13
|
+
export const OPENAI_INFO = Object.freeze({
|
|
14
|
+
type: 'openai',
|
|
15
|
+
name: 'OpenAI',
|
|
16
|
+
baseUrl: 'https://api.openai.com',
|
|
17
|
+
defaultModel: 'gpt-4o',
|
|
18
|
+
envKeys: Object.freeze(['OPENAI_API_KEY', 'ECHO_API_KEY']),
|
|
19
|
+
});
|
|
20
|
+
// =============================================================================
|
|
21
|
+
// FACTORY
|
|
22
|
+
// =============================================================================
|
|
23
|
+
/**
|
|
24
|
+
* Create an OpenAI provider instance.
|
|
25
|
+
*
|
|
26
|
+
* @param config - Provider configuration with API key
|
|
27
|
+
* @returns AIProviderInstance for OpenAI
|
|
28
|
+
*/
|
|
29
|
+
export function createOpenAIProvider(config) {
|
|
30
|
+
const apiKey = config.apiKey;
|
|
31
|
+
const baseUrl = config.baseUrl ?? OPENAI_INFO.baseUrl;
|
|
32
|
+
const defaultModel = config.model ?? OPENAI_INFO.defaultModel;
|
|
33
|
+
const timeout = config.timeout ?? 30000;
|
|
34
|
+
if (!apiKey) {
|
|
35
|
+
throw new Error('OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it in config.');
|
|
36
|
+
}
|
|
37
|
+
const headers = {
|
|
38
|
+
'Content-Type': 'application/json',
|
|
39
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
40
|
+
};
|
|
41
|
+
const instance = {
|
|
42
|
+
info: OPENAI_INFO,
|
|
43
|
+
async complete(messages, options) {
|
|
44
|
+
const model = options?.model ?? defaultModel;
|
|
45
|
+
const start = Date.now();
|
|
46
|
+
const body = {
|
|
47
|
+
model,
|
|
48
|
+
messages: messages.map((m) => ({
|
|
49
|
+
role: m.role,
|
|
50
|
+
content: m.content, // string or ContentBlock[] — OpenAI accepts both natively
|
|
51
|
+
})),
|
|
52
|
+
};
|
|
53
|
+
if (options?.temperature !== undefined) {
|
|
54
|
+
body.temperature = options.temperature;
|
|
55
|
+
}
|
|
56
|
+
if (options?.maxTokens !== undefined) {
|
|
57
|
+
body.max_tokens = options.maxTokens;
|
|
58
|
+
}
|
|
59
|
+
const response = await fetchWithTimeout(`${baseUrl}/v1/chat/completions`, {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
headers,
|
|
62
|
+
body: JSON.stringify(body),
|
|
63
|
+
}, timeout);
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
throw new Error(await extractApiError(response, 'OpenAI'));
|
|
66
|
+
}
|
|
67
|
+
const data = (await response.json());
|
|
68
|
+
const text = data.choices[0]?.message?.content ?? '';
|
|
69
|
+
return {
|
|
70
|
+
text,
|
|
71
|
+
tokens: data.usage
|
|
72
|
+
? {
|
|
73
|
+
prompt: data.usage.prompt_tokens,
|
|
74
|
+
completion: data.usage.completion_tokens,
|
|
75
|
+
total: data.usage.total_tokens,
|
|
76
|
+
}
|
|
77
|
+
: undefined,
|
|
78
|
+
latencyMs: Date.now() - start,
|
|
79
|
+
model: data.model ?? model,
|
|
80
|
+
provider: 'openai',
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
async listModels() {
|
|
84
|
+
const response = await fetchWithTimeout(`${baseUrl}/v1/models`, { method: 'GET', headers }, timeout);
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
throw new Error(await extractApiError(response, 'OpenAI'));
|
|
87
|
+
}
|
|
88
|
+
const data = (await response.json());
|
|
89
|
+
return data.data.map((m) => ({
|
|
90
|
+
id: m.id,
|
|
91
|
+
provider: 'openai',
|
|
92
|
+
createdAt: m.created,
|
|
93
|
+
ownedBy: m.owned_by,
|
|
94
|
+
}));
|
|
95
|
+
},
|
|
96
|
+
async judge(responseText, question) {
|
|
97
|
+
const prompt = buildJudgePrompt(responseText, question);
|
|
98
|
+
const result = await instance.complete([{ role: 'user', content: prompt }]);
|
|
99
|
+
return parseJudgeResponse(result.text);
|
|
100
|
+
},
|
|
101
|
+
async similarity(textA, textB) {
|
|
102
|
+
const prompt = buildSimilarityPrompt(textA, textB);
|
|
103
|
+
const result = await instance.complete([{ role: 'user', content: prompt }]);
|
|
104
|
+
return parseSimilarityResponse(result.text);
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
return instance;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=openai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/providers/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AASH,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,EACrB,uBAAuB,GAC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EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;YAC5E,OAAO,uBAAuB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC9C,CAAC;KACF,CAAC;IAEF,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Provider registry
|
|
3
|
+
*
|
|
4
|
+
* Static map of known providers. No runtime registration needed.
|
|
5
|
+
* Adding a new provider = 1 new file + 2 entries here.
|
|
6
|
+
*/
|
|
7
|
+
import type { AIProviderInstance, ModelInfo, ProviderConfig, ProviderInfo, ProviderType } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Check if a string is a valid provider type.
|
|
10
|
+
*
|
|
11
|
+
* Use this to validate user input or config values before passing them
|
|
12
|
+
* to functions that expect a ProviderType.
|
|
13
|
+
*
|
|
14
|
+
* @param value - String to check
|
|
15
|
+
* @returns true if value is a known ProviderType
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const raw = config.provider; // string
|
|
20
|
+
* if (isProviderType(raw)) {
|
|
21
|
+
* createProvider({ type: raw, apiKey: '...' });
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function isProviderType(value: string): value is ProviderType;
|
|
26
|
+
/**
|
|
27
|
+
* Get metadata for all known providers.
|
|
28
|
+
*
|
|
29
|
+
* @returns Array of ProviderInfo for all registered providers
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const providers = getProviders();
|
|
34
|
+
* // → [{ type: 'openai', name: 'OpenAI', ... }, { type: 'anthropic', name: 'Anthropic', ... }]
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function getProviders(): ProviderInfo[];
|
|
38
|
+
/**
|
|
39
|
+
* Get metadata for a specific provider.
|
|
40
|
+
*
|
|
41
|
+
* @param type - Provider type
|
|
42
|
+
* @returns ProviderInfo for the requested provider
|
|
43
|
+
* @throws If provider type is unknown
|
|
44
|
+
*/
|
|
45
|
+
export declare function getProvider(type: ProviderType): ProviderInfo;
|
|
46
|
+
/**
|
|
47
|
+
* Create a configured provider instance.
|
|
48
|
+
*
|
|
49
|
+
* @param config - Provider configuration with type and API key
|
|
50
|
+
* @returns AIProviderInstance ready to use
|
|
51
|
+
* @throws If provider type is unknown or config is invalid
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const provider = createProvider({
|
|
56
|
+
* type: 'openai',
|
|
57
|
+
* apiKey: 'sk-...',
|
|
58
|
+
* model: 'gpt-4o',
|
|
59
|
+
* });
|
|
60
|
+
* const response = await provider.complete([{ role: 'user', content: 'Hello' }]);
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare function createProvider(config: ProviderConfig): AIProviderInstance;
|
|
64
|
+
/**
|
|
65
|
+
* Convenience: list models for a provider type.
|
|
66
|
+
* Creates a temporary provider instance, calls listModels(), and returns the result.
|
|
67
|
+
*
|
|
68
|
+
* @param type - Provider type
|
|
69
|
+
* @param apiKey - API key for authentication
|
|
70
|
+
* @param baseUrl - Optional custom base URL
|
|
71
|
+
* @returns Array of ModelInfo from the live API
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const models = await listModels('openai', 'sk-...');
|
|
76
|
+
* // → [{ id: 'gpt-4o', provider: 'openai', ... }, ...]
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function listModels(type: ProviderType, apiKey: string, baseUrl?: string): Promise<ModelInfo[]>;
|
|
80
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/providers/registry.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,kBAAkB,EAClB,SAAS,EACT,cAAc,EACd,YAAY,EACZ,YAAY,EACb,MAAM,YAAY,CAAC;AA2BpB;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,YAAY,CAEnE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,IAAI,YAAY,EAAE,CAE7C;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,YAAY,GAAG,YAAY,CAM5D;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,cAAc,GAAG,kBAAkB,CAMzE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,UAAU,CAC9B,IAAI,EAAE,YAAY,EAClB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,SAAS,EAAE,CAAC,CAGtB"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Provider registry
|
|
3
|
+
*
|
|
4
|
+
* Static map of known providers. No runtime registration needed.
|
|
5
|
+
* Adding a new provider = 1 new file + 2 entries here.
|
|
6
|
+
*/
|
|
7
|
+
import { createOpenAIProvider, OPENAI_INFO } from './openai.js';
|
|
8
|
+
import { createAnthropicProvider, ANTHROPIC_INFO } from './anthropic.js';
|
|
9
|
+
// =============================================================================
|
|
10
|
+
// REGISTRY
|
|
11
|
+
// =============================================================================
|
|
12
|
+
/** Known provider metadata. */
|
|
13
|
+
const PROVIDERS = {
|
|
14
|
+
openai: OPENAI_INFO,
|
|
15
|
+
anthropic: ANTHROPIC_INFO,
|
|
16
|
+
};
|
|
17
|
+
/** Factory functions for each provider. */
|
|
18
|
+
const FACTORIES = {
|
|
19
|
+
openai: createOpenAIProvider,
|
|
20
|
+
anthropic: createAnthropicProvider,
|
|
21
|
+
};
|
|
22
|
+
/** Set of valid provider type strings for runtime validation. */
|
|
23
|
+
const VALID_TYPES = new Set(Object.keys(PROVIDERS));
|
|
24
|
+
// =============================================================================
|
|
25
|
+
// PUBLIC API
|
|
26
|
+
// =============================================================================
|
|
27
|
+
/**
|
|
28
|
+
* Check if a string is a valid provider type.
|
|
29
|
+
*
|
|
30
|
+
* Use this to validate user input or config values before passing them
|
|
31
|
+
* to functions that expect a ProviderType.
|
|
32
|
+
*
|
|
33
|
+
* @param value - String to check
|
|
34
|
+
* @returns true if value is a known ProviderType
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const raw = config.provider; // string
|
|
39
|
+
* if (isProviderType(raw)) {
|
|
40
|
+
* createProvider({ type: raw, apiKey: '...' });
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function isProviderType(value) {
|
|
45
|
+
return VALID_TYPES.has(value);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get metadata for all known providers.
|
|
49
|
+
*
|
|
50
|
+
* @returns Array of ProviderInfo for all registered providers
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const providers = getProviders();
|
|
55
|
+
* // → [{ type: 'openai', name: 'OpenAI', ... }, { type: 'anthropic', name: 'Anthropic', ... }]
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export function getProviders() {
|
|
59
|
+
return Object.values(PROVIDERS);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Get metadata for a specific provider.
|
|
63
|
+
*
|
|
64
|
+
* @param type - Provider type
|
|
65
|
+
* @returns ProviderInfo for the requested provider
|
|
66
|
+
* @throws If provider type is unknown
|
|
67
|
+
*/
|
|
68
|
+
export function getProvider(type) {
|
|
69
|
+
const info = PROVIDERS[type];
|
|
70
|
+
if (!info) {
|
|
71
|
+
throw new Error(`Unknown provider type: ${type}`);
|
|
72
|
+
}
|
|
73
|
+
return info;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Create a configured provider instance.
|
|
77
|
+
*
|
|
78
|
+
* @param config - Provider configuration with type and API key
|
|
79
|
+
* @returns AIProviderInstance ready to use
|
|
80
|
+
* @throws If provider type is unknown or config is invalid
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const provider = createProvider({
|
|
85
|
+
* type: 'openai',
|
|
86
|
+
* apiKey: 'sk-...',
|
|
87
|
+
* model: 'gpt-4o',
|
|
88
|
+
* });
|
|
89
|
+
* const response = await provider.complete([{ role: 'user', content: 'Hello' }]);
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export function createProvider(config) {
|
|
93
|
+
const factory = FACTORIES[config.type];
|
|
94
|
+
if (!factory) {
|
|
95
|
+
throw new Error(`Unknown provider type: ${config.type}`);
|
|
96
|
+
}
|
|
97
|
+
return factory(config);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Convenience: list models for a provider type.
|
|
101
|
+
* Creates a temporary provider instance, calls listModels(), and returns the result.
|
|
102
|
+
*
|
|
103
|
+
* @param type - Provider type
|
|
104
|
+
* @param apiKey - API key for authentication
|
|
105
|
+
* @param baseUrl - Optional custom base URL
|
|
106
|
+
* @returns Array of ModelInfo from the live API
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* const models = await listModels('openai', 'sk-...');
|
|
111
|
+
* // → [{ id: 'gpt-4o', provider: 'openai', ... }, ...]
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export async function listModels(type, apiKey, baseUrl) {
|
|
115
|
+
const provider = createProvider({ type, apiKey, baseUrl });
|
|
116
|
+
return provider.listModels();
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/providers/registry.ts"],"names":[],"mappings":"AAAA;;;;;GA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MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;AACzB,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,IAAkB,EAClB,MAAc,EACd,OAAgB;IAEhB,MAAM,QAAQ,GAAG,cAAc,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IAC3D,OAAO,QAAQ,CAAC,UAAU,EAAE,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview High-level runPrompt API
|
|
3
|
+
*
|
|
4
|
+
* Renders an echo-pdk template with variables, sends the result to an LLM
|
|
5
|
+
* provider, and returns both the rendered prompt and the LLM response.
|
|
6
|
+
*
|
|
7
|
+
* Import note: We import parse/evaluate/render directly from their modules
|
|
8
|
+
* (not from ../index.ts) to avoid circular dependencies, since index.ts
|
|
9
|
+
* imports from ai-judge which imports from providers.
|
|
10
|
+
*/
|
|
11
|
+
import type { ProviderConfig, CompletionResponse } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Options for runPrompt.
|
|
14
|
+
*/
|
|
15
|
+
export interface RunPromptOptions {
|
|
16
|
+
/** Echo DSL template string */
|
|
17
|
+
template: string;
|
|
18
|
+
/** Variables to substitute in the template */
|
|
19
|
+
variables: Record<string, unknown>;
|
|
20
|
+
/** Provider configuration (type, apiKey, model, etc.) */
|
|
21
|
+
provider: ProviderConfig;
|
|
22
|
+
/** Optional system message prepended to the LLM call */
|
|
23
|
+
systemMessage?: string;
|
|
24
|
+
/** Temperature for LLM call */
|
|
25
|
+
temperature?: number;
|
|
26
|
+
/** Max tokens for LLM response */
|
|
27
|
+
maxTokens?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Result from runPrompt.
|
|
31
|
+
*/
|
|
32
|
+
export interface RunPromptResult {
|
|
33
|
+
/** The rendered prompt (after template + variable resolution) */
|
|
34
|
+
renderedPrompt: string;
|
|
35
|
+
/** The LLM completion response */
|
|
36
|
+
response: CompletionResponse;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Render a template and send it to an LLM in one call.
|
|
40
|
+
*
|
|
41
|
+
* Steps:
|
|
42
|
+
* 1. Parse the Echo DSL template
|
|
43
|
+
* 2. Evaluate conditions with the provided variables
|
|
44
|
+
* 3. Render to a string
|
|
45
|
+
* 4. Send to the specified LLM provider
|
|
46
|
+
* 5. Return both the rendered prompt and the response
|
|
47
|
+
*
|
|
48
|
+
* **Limitation:** Templates using `#ai_gate` conditionals are not supported
|
|
49
|
+
* by this function. Use `createEcho()` with an `aiProvider` config for
|
|
50
|
+
* templates that require AI-evaluated conditions.
|
|
51
|
+
*
|
|
52
|
+
* @param options - Template, variables, and provider configuration
|
|
53
|
+
* @returns The rendered prompt and LLM response
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const result = await runPrompt({
|
|
58
|
+
* template: 'Recommend a {{genre}} movie for {{companions}}.',
|
|
59
|
+
* variables: { genre: 'Comedy', companions: 'Family' },
|
|
60
|
+
* provider: { type: 'openai', apiKey: 'sk-...', model: 'gpt-4o' },
|
|
61
|
+
* });
|
|
62
|
+
* console.log(result.renderedPrompt);
|
|
63
|
+
* // → "Recommend a Comedy movie for Family."
|
|
64
|
+
* console.log(result.response.text);
|
|
65
|
+
* // → "I'd recommend..."
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function runPrompt(options: RunPromptOptions): Promise<RunPromptResult>;
|
|
69
|
+
//# sourceMappingURL=run-prompt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-prompt.d.ts","sourceRoot":"","sources":["../../src/providers/run-prompt.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAMH,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAMrE;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,8CAA8C;IAC9C,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,yDAAyD;IACzD,QAAQ,EAAE,cAAc,CAAC;IACzB,wDAAwD;IACxD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,+BAA+B;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kCAAkC;IAClC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,iEAAiE;IACjE,cAAc,EAAE,MAAM,CAAC;IACvB,kCAAkC;IAClC,QAAQ,EAAE,kBAAkB,CAAC;CAC9B;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CAqCnF"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview High-level runPrompt API
|
|
3
|
+
*
|
|
4
|
+
* Renders an echo-pdk template with variables, sends the result to an LLM
|
|
5
|
+
* provider, and returns both the rendered prompt and the LLM response.
|
|
6
|
+
*
|
|
7
|
+
* Import note: We import parse/evaluate/render directly from their modules
|
|
8
|
+
* (not from ../index.ts) to avoid circular dependencies, since index.ts
|
|
9
|
+
* imports from ai-judge which imports from providers.
|
|
10
|
+
*/
|
|
11
|
+
import { parse } from '../parser/parser.js';
|
|
12
|
+
import { evaluate } from '../evaluator/index.js';
|
|
13
|
+
import { render, formatErrors } from '../renderer/renderer.js';
|
|
14
|
+
import { createProvider } from './registry.js';
|
|
15
|
+
// =============================================================================
|
|
16
|
+
// IMPLEMENTATION
|
|
17
|
+
// =============================================================================
|
|
18
|
+
/**
|
|
19
|
+
* Render a template and send it to an LLM in one call.
|
|
20
|
+
*
|
|
21
|
+
* Steps:
|
|
22
|
+
* 1. Parse the Echo DSL template
|
|
23
|
+
* 2. Evaluate conditions with the provided variables
|
|
24
|
+
* 3. Render to a string
|
|
25
|
+
* 4. Send to the specified LLM provider
|
|
26
|
+
* 5. Return both the rendered prompt and the response
|
|
27
|
+
*
|
|
28
|
+
* **Limitation:** Templates using `#ai_gate` conditionals are not supported
|
|
29
|
+
* by this function. Use `createEcho()` with an `aiProvider` config for
|
|
30
|
+
* templates that require AI-evaluated conditions.
|
|
31
|
+
*
|
|
32
|
+
* @param options - Template, variables, and provider configuration
|
|
33
|
+
* @returns The rendered prompt and LLM response
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const result = await runPrompt({
|
|
38
|
+
* template: 'Recommend a {{genre}} movie for {{companions}}.',
|
|
39
|
+
* variables: { genre: 'Comedy', companions: 'Family' },
|
|
40
|
+
* provider: { type: 'openai', apiKey: 'sk-...', model: 'gpt-4o' },
|
|
41
|
+
* });
|
|
42
|
+
* console.log(result.renderedPrompt);
|
|
43
|
+
* // → "Recommend a Comedy movie for Family."
|
|
44
|
+
* console.log(result.response.text);
|
|
45
|
+
* // → "I'd recommend..."
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export async function runPrompt(options) {
|
|
49
|
+
const { template, variables, provider: providerConfig, systemMessage } = options;
|
|
50
|
+
// 1. Parse
|
|
51
|
+
const parseResult = parse(template);
|
|
52
|
+
if (!parseResult.success || !parseResult.ast) {
|
|
53
|
+
const formattedErrors = formatErrors(template, parseResult.errors);
|
|
54
|
+
throw new Error(`Parse error:\n${formattedErrors}`);
|
|
55
|
+
}
|
|
56
|
+
// 2. Evaluate (resolve conditionals with the given variables)
|
|
57
|
+
const { ast: evaluatedAst } = await evaluate(parseResult.ast, variables, {});
|
|
58
|
+
// 3. Render
|
|
59
|
+
const renderedPrompt = render(evaluatedAst, {
|
|
60
|
+
context: variables,
|
|
61
|
+
config: {},
|
|
62
|
+
trim: false,
|
|
63
|
+
collapseNewlines: true,
|
|
64
|
+
});
|
|
65
|
+
// 4. Create provider and send to LLM
|
|
66
|
+
const instance = createProvider(providerConfig);
|
|
67
|
+
const messages = [];
|
|
68
|
+
if (systemMessage) {
|
|
69
|
+
messages.push({ role: 'system', content: systemMessage });
|
|
70
|
+
}
|
|
71
|
+
messages.push({ role: 'user', content: renderedPrompt });
|
|
72
|
+
const response = await instance.complete(messages, {
|
|
73
|
+
model: providerConfig.model,
|
|
74
|
+
temperature: options.temperature,
|
|
75
|
+
maxTokens: options.maxTokens,
|
|
76
|
+
});
|
|
77
|
+
return { renderedPrompt, response };
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=run-prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-prompt.js","sourceRoot":"","sources":["../../src/providers/run-prompt.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAmC/C,gFAAgF;AAChF,iBAAiB;AACjB,gFAAgF;AAEhF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,OAAyB;IACvD,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,cAAc,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC;IAEjF,WAAW;IACX,MAAM,WAAW,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;IACpC,IAAI,CAAC,WAAW,CAAC,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC;QAC7C,MAA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