tryaii-dre 0.2.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 +190 -0
- package/README.md +234 -0
- package/dist/banner.d.ts +24 -0
- package/dist/banner.d.ts.map +1 -0
- package/dist/banner.js +125 -0
- package/dist/banner.js.map +1 -0
- package/dist/benchmarks/index.d.ts +4 -0
- package/dist/benchmarks/index.d.ts.map +1 -0
- package/dist/benchmarks/index.js +3 -0
- package/dist/benchmarks/index.js.map +1 -0
- package/dist/benchmarks/registry.d.ts +69 -0
- package/dist/benchmarks/registry.d.ts.map +1 -0
- package/dist/benchmarks/registry.js +128 -0
- package/dist/benchmarks/registry.js.map +1 -0
- package/dist/benchmarks/standard.d.ts +6 -0
- package/dist/benchmarks/standard.d.ts.map +1 -0
- package/dist/benchmarks/standard.js +115 -0
- package/dist/benchmarks/standard.js.map +1 -0
- package/dist/budget.d.ts +65 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +344 -0
- package/dist/budget.js.map +1 -0
- package/dist/cache/index.d.ts +27 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +63 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/centroids/data/centroids_all-MiniLM-L6-v2.json +1 -0
- package/dist/centroids/data/trainingQueries.json +246 -0
- package/dist/centroids/generator.d.ts +63 -0
- package/dist/centroids/generator.d.ts.map +1 -0
- package/dist/centroids/generator.js +120 -0
- package/dist/centroids/generator.js.map +1 -0
- package/dist/centroids/index.d.ts +3 -0
- package/dist/centroids/index.d.ts.map +1 -0
- package/dist/centroids/index.js +3 -0
- package/dist/centroids/index.js.map +1 -0
- package/dist/centroids/loader.d.ts +87 -0
- package/dist/centroids/loader.d.ts.map +1 -0
- package/dist/centroids/loader.js +236 -0
- package/dist/centroids/loader.js.map +1 -0
- package/dist/classifiers/base.d.ts +56 -0
- package/dist/classifiers/base.d.ts.map +1 -0
- package/dist/classifiers/base.js +42 -0
- package/dist/classifiers/base.js.map +1 -0
- package/dist/classifiers/embedding.d.ts +68 -0
- package/dist/classifiers/embedding.d.ts.map +1 -0
- package/dist/classifiers/embedding.js +0 -0
- package/dist/classifiers/embedding.js.map +1 -0
- package/dist/classifiers/hybrid.d.ts +31 -0
- package/dist/classifiers/hybrid.d.ts.map +1 -0
- package/dist/classifiers/hybrid.js +61 -0
- package/dist/classifiers/hybrid.js.map +1 -0
- package/dist/classifiers/index.d.ts +4 -0
- package/dist/classifiers/index.d.ts.map +1 -0
- package/dist/classifiers/index.js +3 -0
- package/dist/classifiers/index.js.map +1 -0
- package/dist/classifiers/keyword.d.ts +29 -0
- package/dist/classifiers/keyword.d.ts.map +1 -0
- package/dist/classifiers/keyword.js +264 -0
- package/dist/classifiers/keyword.js.map +1 -0
- package/dist/cli.d.ts +15 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +597 -0
- package/dist/cli.js.map +1 -0
- package/dist/client-types.d.ts +101 -0
- package/dist/client-types.d.ts.map +1 -0
- package/dist/client-types.js +5 -0
- package/dist/client-types.js.map +1 -0
- package/dist/client.d.ts +50 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +279 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +45 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +37 -0
- package/dist/config.js.map +1 -0
- package/dist/dashboard/index.d.ts +48 -0
- package/dist/dashboard/index.d.ts.map +1 -0
- package/dist/dashboard/index.js +166 -0
- package/dist/dashboard/index.js.map +1 -0
- package/dist/embeddings/base.d.ts +66 -0
- package/dist/embeddings/base.d.ts.map +1 -0
- package/dist/embeddings/base.js +77 -0
- package/dist/embeddings/base.js.map +1 -0
- package/dist/embeddings/index.d.ts +3 -0
- package/dist/embeddings/index.d.ts.map +1 -0
- package/dist/embeddings/index.js +3 -0
- package/dist/embeddings/index.js.map +1 -0
- package/dist/embeddings/local.d.ts +42 -0
- package/dist/embeddings/local.d.ts.map +1 -0
- package/dist/embeddings/local.js +89 -0
- package/dist/embeddings/local.js.map +1 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/index.d.ts +3 -0
- package/dist/integrations/index.d.ts.map +1 -0
- package/dist/integrations/index.js +2 -0
- package/dist/integrations/index.js.map +1 -0
- package/dist/integrations/openrouter.d.ts +84 -0
- package/dist/integrations/openrouter.d.ts.map +1 -0
- package/dist/integrations/openrouter.js +253 -0
- package/dist/integrations/openrouter.js.map +1 -0
- package/dist/registry/index.d.ts +2 -0
- package/dist/registry/index.d.ts.map +1 -0
- package/dist/registry/index.js +2 -0
- package/dist/registry/index.js.map +1 -0
- package/dist/registry/models.d.ts +76 -0
- package/dist/registry/models.d.ts.map +1 -0
- package/dist/registry/models.js +170 -0
- package/dist/registry/models.js.map +1 -0
- package/dist/registry/presets/defaultModels.json +435 -0
- package/dist/router.d.ts +178 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +259 -0
- package/dist/router.js.map +1 -0
- package/dist/scoring/benchmarks.d.ts +35 -0
- package/dist/scoring/benchmarks.d.ts.map +1 -0
- package/dist/scoring/benchmarks.js +68 -0
- package/dist/scoring/benchmarks.js.map +1 -0
- package/dist/scoring/engine.d.ts +43 -0
- package/dist/scoring/engine.d.ts.map +1 -0
- package/dist/scoring/engine.js +267 -0
- package/dist/scoring/engine.js.map +1 -0
- package/dist/scoring/index.d.ts +6 -0
- package/dist/scoring/index.d.ts.map +1 -0
- package/dist/scoring/index.js +4 -0
- package/dist/scoring/index.js.map +1 -0
- package/dist/scoring/priorities.d.ts +41 -0
- package/dist/scoring/priorities.d.ts.map +1 -0
- package/dist/scoring/priorities.js +49 -0
- package/dist/scoring/priorities.js.map +1 -0
- package/dist/types.d.ts +47 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/cosine.d.ts +10 -0
- package/dist/utils/cosine.d.ts.map +1 -0
- package/dist/utils/cosine.js +18 -0
- package/dist/utils/cosine.js.map +1 -0
- package/dist/utils/math.d.ts +18 -0
- package/dist/utils/math.d.ts.map +1 -0
- package/dist/utils/math.js +54 -0
- package/dist/utils/math.js.map +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level client TypeScript types for TryAii-DRE.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* User priorities for model selection.
|
|
6
|
+
*
|
|
7
|
+
* Each value is on a 1-5 scale:
|
|
8
|
+
* 1 = don't care about this dimension
|
|
9
|
+
* 3 = balanced (default)
|
|
10
|
+
* 5 = this is critical
|
|
11
|
+
*/
|
|
12
|
+
export interface Priorities {
|
|
13
|
+
quality: number;
|
|
14
|
+
cost: number;
|
|
15
|
+
speed: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A single model's score from the routing engine.
|
|
19
|
+
*/
|
|
20
|
+
export interface ModelScore {
|
|
21
|
+
modelId: string;
|
|
22
|
+
finalScore: number;
|
|
23
|
+
qualityScore: number;
|
|
24
|
+
costScore: number;
|
|
25
|
+
speedScore: number;
|
|
26
|
+
reasoning: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Result of routing a prompt, with no API call made.
|
|
30
|
+
*/
|
|
31
|
+
export interface RouteResult {
|
|
32
|
+
/** The top recommended model ID. */
|
|
33
|
+
bestModel: string;
|
|
34
|
+
/** All scored models, sorted by score descending. */
|
|
35
|
+
scores: ModelScore[];
|
|
36
|
+
/** Score of the best model. */
|
|
37
|
+
bestScore: number;
|
|
38
|
+
/** Reasoning for why the top model was chosen. */
|
|
39
|
+
bestReasoning: string;
|
|
40
|
+
/** The priorities that were used for this routing decision. */
|
|
41
|
+
priorities: Priorities;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Response from a chat API call.
|
|
45
|
+
*/
|
|
46
|
+
export interface ChatResponse {
|
|
47
|
+
/** The AI-generated response text. */
|
|
48
|
+
content: string;
|
|
49
|
+
/** TryAii-DRE model ID that was used. */
|
|
50
|
+
modelUsed: string;
|
|
51
|
+
/** OpenRouter model slug that was called. */
|
|
52
|
+
openrouterModel: string;
|
|
53
|
+
/** Reasoning for why this model was chosen. */
|
|
54
|
+
routeReasoning: string;
|
|
55
|
+
/** Token usage information. */
|
|
56
|
+
usage: TokenUsage;
|
|
57
|
+
/** Raw API response data. */
|
|
58
|
+
rawResponse?: Record<string, unknown>;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Token usage from an API call.
|
|
62
|
+
*/
|
|
63
|
+
export interface TokenUsage {
|
|
64
|
+
promptTokens?: number;
|
|
65
|
+
completionTokens?: number;
|
|
66
|
+
totalTokens?: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Options for chat and stream calls.
|
|
70
|
+
*/
|
|
71
|
+
export interface ChatOptions {
|
|
72
|
+
/** Override default priorities for this call. */
|
|
73
|
+
priorities?: Priorities;
|
|
74
|
+
/** System message / system prompt. */
|
|
75
|
+
systemMessage?: string;
|
|
76
|
+
/** Sampling temperature. Default: 0.7. */
|
|
77
|
+
temperature?: number;
|
|
78
|
+
/** Maximum tokens in the response. */
|
|
79
|
+
maxTokens?: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Options for route-only calls.
|
|
83
|
+
*/
|
|
84
|
+
export interface RouteOptions {
|
|
85
|
+
/** Override default priorities for this call. */
|
|
86
|
+
priorities?: Priorities;
|
|
87
|
+
/** Number of top models to return. Default: 5. */
|
|
88
|
+
topK?: number;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Constructor options for DREClient.
|
|
92
|
+
*/
|
|
93
|
+
export interface DREClientOptions {
|
|
94
|
+
/** OpenRouter API key. Falls back to OPENROUTER_API_KEY env var. */
|
|
95
|
+
apiKey?: string;
|
|
96
|
+
/** Default priorities for all routing calls. */
|
|
97
|
+
priorities?: Priorities;
|
|
98
|
+
/** Base URL for OpenRouter API. */
|
|
99
|
+
baseUrl?: string;
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=client-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-types.d.ts","sourceRoot":"","sources":["../src/client-types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;GAOG;AACH,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,oCAAoC;IACpC,SAAS,EAAE,MAAM,CAAC;IAElB,qDAAqD;IACrD,MAAM,EAAE,UAAU,EAAE,CAAC;IAErB,+BAA+B;IAC/B,SAAS,EAAE,MAAM,CAAC;IAElB,kDAAkD;IAClD,aAAa,EAAE,MAAM,CAAC;IAEtB,+DAA+D;IAC/D,UAAU,EAAE,UAAU,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,sCAAsC;IACtC,OAAO,EAAE,MAAM,CAAC;IAEhB,yCAAyC;IACzC,SAAS,EAAE,MAAM,CAAC;IAElB,6CAA6C;IAC7C,eAAe,EAAE,MAAM,CAAC;IAExB,+CAA+C;IAC/C,cAAc,EAAE,MAAM,CAAC;IAEvB,+BAA+B;IAC/B,KAAK,EAAE,UAAU,CAAC;IAElB,6BAA6B;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACvC;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,iDAAiD;IACjD,UAAU,CAAC,EAAE,UAAU,CAAC;IAExB,sCAAsC;IACtC,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB,0CAA0C;IAC1C,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB,sCAAsC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,iDAAiD;IACjD,UAAU,CAAC,EAAE,UAAU,CAAC;IAExB,kDAAkD;IAClD,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,oEAAoE;IACpE,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,gDAAgD;IAChD,UAAU,CAAC,EAAE,UAAU,CAAC;IAExB,mCAAmC;IACnC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-types.js","sourceRoot":"","sources":["../src/client-types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DREClient -- unified high-level client for TryAii-DRE.
|
|
3
|
+
*
|
|
4
|
+
* Wraps prompt-aware model selection (via the `tryaii-dre` core `Router`) and
|
|
5
|
+
* the OpenRouter API into a single class so users do not have to manage
|
|
6
|
+
* separate objects.
|
|
7
|
+
*
|
|
8
|
+
* The SDK delegates routing to the core `Router`, which classifies prompts
|
|
9
|
+
* against benchmark centroids using embeddings -- so selection here IS
|
|
10
|
+
* prompt-aware. The SDK then forwards the chosen model to OpenRouter for
|
|
11
|
+
* completion.
|
|
12
|
+
*
|
|
13
|
+
* Usage:
|
|
14
|
+
* import { DREClient } from 'tryaii-dre';
|
|
15
|
+
*
|
|
16
|
+
* const client = new DREClient({ apiKey: 'sk-or-...' });
|
|
17
|
+
* const response = await client.chat('Write a sorting algorithm');
|
|
18
|
+
* console.log(response.modelUsed, response.content);
|
|
19
|
+
*/
|
|
20
|
+
import type { ChatOptions, ChatResponse, DREClientOptions, RouteOptions, RouteResult } from './client-types.js';
|
|
21
|
+
export declare class DREClient {
|
|
22
|
+
private readonly _apiKey;
|
|
23
|
+
private readonly _baseUrl;
|
|
24
|
+
private readonly _defaultPriorities;
|
|
25
|
+
private readonly _router;
|
|
26
|
+
constructor(options?: DREClientOptions);
|
|
27
|
+
/** Throw early with a clear message when chat/stream is called without an API key. */
|
|
28
|
+
private _ensureApiKey;
|
|
29
|
+
/**
|
|
30
|
+
* Pick the best model for the prompt and the user's priorities, without
|
|
31
|
+
* making an API call.
|
|
32
|
+
*
|
|
33
|
+
* Routing is prompt-aware: the core `Router` embeds the prompt and matches
|
|
34
|
+
* it against benchmark centroids before scoring models. Async because the
|
|
35
|
+
* default embedding provider (`@xenova/transformers`) is async-only.
|
|
36
|
+
*/
|
|
37
|
+
route(prompt: string, options?: RouteOptions): Promise<RouteResult>;
|
|
38
|
+
/**
|
|
39
|
+
* Pick the best model for the prompt and return the AI response.
|
|
40
|
+
*/
|
|
41
|
+
chat(prompt: string, options?: ChatOptions): Promise<ChatResponse>;
|
|
42
|
+
/**
|
|
43
|
+
* Pick the best model for the prompt and stream the response.
|
|
44
|
+
*
|
|
45
|
+
* Yields content chunks as they arrive from the API.
|
|
46
|
+
*/
|
|
47
|
+
stream(prompt: string, options?: ChatOptions): AsyncGenerator<string>;
|
|
48
|
+
private _toSdkResult;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAMH,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EACZ,gBAAgB,EAEhB,YAAY,EACZ,WAAW,EAGZ,MAAM,mBAAmB,CAAC;AA+B3B,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAiB;IACpD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAErB,OAAO,CAAC,EAAE,gBAAgB;IAOtC,sFAAsF;IACtF,OAAO,CAAC,aAAa;IAarB;;;;;;;OAOG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC;IAoBzE;;OAEG;IACG,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAiFxE;;;;OAIG;IACI,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,cAAc,CAAC,MAAM,CAAC;IAwG5E,OAAO,CAAC,YAAY;CAyBrB"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DREClient -- unified high-level client for TryAii-DRE.
|
|
3
|
+
*
|
|
4
|
+
* Wraps prompt-aware model selection (via the `tryaii-dre` core `Router`) and
|
|
5
|
+
* the OpenRouter API into a single class so users do not have to manage
|
|
6
|
+
* separate objects.
|
|
7
|
+
*
|
|
8
|
+
* The SDK delegates routing to the core `Router`, which classifies prompts
|
|
9
|
+
* against benchmark centroids using embeddings -- so selection here IS
|
|
10
|
+
* prompt-aware. The SDK then forwards the chosen model to OpenRouter for
|
|
11
|
+
* completion.
|
|
12
|
+
*
|
|
13
|
+
* Usage:
|
|
14
|
+
* import { DREClient } from 'tryaii-dre';
|
|
15
|
+
*
|
|
16
|
+
* const client = new DREClient({ apiKey: 'sk-or-...' });
|
|
17
|
+
* const response = await client.chat('Write a sorting algorithm');
|
|
18
|
+
* console.log(response.modelUsed, response.content);
|
|
19
|
+
*/
|
|
20
|
+
import { MODEL_ID_TO_OPENROUTER } from './integrations/index.js';
|
|
21
|
+
import { Router } from './router.js';
|
|
22
|
+
import { Priorities } from './scoring/priorities.js';
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Helpers
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
const DEFAULT_PRIORITIES = { quality: 3, cost: 3, speed: 3 };
|
|
27
|
+
function mergePriorities(data) {
|
|
28
|
+
if (!data)
|
|
29
|
+
return DEFAULT_PRIORITIES;
|
|
30
|
+
// Per-field default of 3: a partial dict like { quality: 5 } would otherwise
|
|
31
|
+
// produce Math.round(undefined) === NaN, which survives a `?? 3` fallback.
|
|
32
|
+
const clampField = (value) => {
|
|
33
|
+
const n = Math.round(Number(value));
|
|
34
|
+
return Number.isFinite(n) ? Math.max(1, Math.min(5, n)) : 3;
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
quality: clampField(data.quality),
|
|
38
|
+
cost: clampField(data.cost),
|
|
39
|
+
speed: clampField(data.speed),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function resolveModel(modelId) {
|
|
43
|
+
return MODEL_ID_TO_OPENROUTER[modelId] ?? modelId;
|
|
44
|
+
}
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
// DREClient
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
export class DREClient {
|
|
49
|
+
constructor(options) {
|
|
50
|
+
this._apiKey = options?.apiKey ?? process.env.OPENROUTER_API_KEY ?? '';
|
|
51
|
+
this._baseUrl = options?.baseUrl ?? 'https://openrouter.ai/api/v1';
|
|
52
|
+
this._defaultPriorities = mergePriorities(options?.priorities);
|
|
53
|
+
this._router = new Router();
|
|
54
|
+
}
|
|
55
|
+
/** Throw early with a clear message when chat/stream is called without an API key. */
|
|
56
|
+
_ensureApiKey() {
|
|
57
|
+
if (!this._apiKey) {
|
|
58
|
+
throw new Error('DREClient requires an OpenRouter API key. Pass { apiKey } to the constructor ' +
|
|
59
|
+
'or set the OPENROUTER_API_KEY environment variable.');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// -----------------------------------------------------------------------
|
|
63
|
+
// route -- async, no API call
|
|
64
|
+
// -----------------------------------------------------------------------
|
|
65
|
+
/**
|
|
66
|
+
* Pick the best model for the prompt and the user's priorities, without
|
|
67
|
+
* making an API call.
|
|
68
|
+
*
|
|
69
|
+
* Routing is prompt-aware: the core `Router` embeds the prompt and matches
|
|
70
|
+
* it against benchmark centroids before scoring models. Async because the
|
|
71
|
+
* default embedding provider (`@xenova/transformers`) is async-only.
|
|
72
|
+
*/
|
|
73
|
+
async route(prompt, options) {
|
|
74
|
+
const priorities = mergePriorities(options?.priorities ?? this._defaultPriorities);
|
|
75
|
+
const topK = options?.topK ?? 5;
|
|
76
|
+
const coreResult = await this._router.route(prompt, {
|
|
77
|
+
priorities: Priorities.fromDict(priorities),
|
|
78
|
+
topK,
|
|
79
|
+
});
|
|
80
|
+
if (!coreResult.bestModel) {
|
|
81
|
+
throw new Error('routing returned no model for this prompt');
|
|
82
|
+
}
|
|
83
|
+
return this._toSdkResult(coreResult, priorities);
|
|
84
|
+
}
|
|
85
|
+
// -----------------------------------------------------------------------
|
|
86
|
+
// chat -- async, makes API call
|
|
87
|
+
// -----------------------------------------------------------------------
|
|
88
|
+
/**
|
|
89
|
+
* Pick the best model for the prompt and return the AI response.
|
|
90
|
+
*/
|
|
91
|
+
async chat(prompt, options) {
|
|
92
|
+
this._ensureApiKey();
|
|
93
|
+
const priorities = mergePriorities(options?.priorities ?? this._defaultPriorities);
|
|
94
|
+
const coreResult = await this._router.route(prompt, {
|
|
95
|
+
priorities: Priorities.fromDict(priorities),
|
|
96
|
+
});
|
|
97
|
+
if (!coreResult.bestModel) {
|
|
98
|
+
throw new Error('routing returned no model for this prompt');
|
|
99
|
+
}
|
|
100
|
+
const modelId = coreResult.bestModel;
|
|
101
|
+
const reasoning = coreResult.scores[0]?.reasoning ?? '';
|
|
102
|
+
const openrouterModel = resolveModel(modelId);
|
|
103
|
+
// Build messages
|
|
104
|
+
const messages = [];
|
|
105
|
+
if (options?.systemMessage) {
|
|
106
|
+
messages.push({ role: 'system', content: options.systemMessage });
|
|
107
|
+
}
|
|
108
|
+
messages.push({ role: 'user', content: prompt });
|
|
109
|
+
// Build payload
|
|
110
|
+
const payload = {
|
|
111
|
+
model: openrouterModel,
|
|
112
|
+
messages,
|
|
113
|
+
temperature: options?.temperature ?? 0.7,
|
|
114
|
+
};
|
|
115
|
+
// Only send max_tokens when it is a finite, positive number (0 is not meaningful).
|
|
116
|
+
if (options?.maxTokens != null && Number.isFinite(options.maxTokens) && options.maxTokens > 0) {
|
|
117
|
+
payload.max_tokens = options.maxTokens;
|
|
118
|
+
}
|
|
119
|
+
// Call OpenRouter API
|
|
120
|
+
const response = await fetch(`${this._baseUrl}/chat/completions`, {
|
|
121
|
+
method: 'POST',
|
|
122
|
+
headers: {
|
|
123
|
+
'Authorization': `Bearer ${this._apiKey}`,
|
|
124
|
+
'Content-Type': 'application/json',
|
|
125
|
+
'X-Title': 'tryaii-dre',
|
|
126
|
+
},
|
|
127
|
+
body: JSON.stringify(payload),
|
|
128
|
+
});
|
|
129
|
+
if (!response.ok) {
|
|
130
|
+
const errorText = await response.text();
|
|
131
|
+
throw new Error(`OpenRouter API error (${response.status}): ${errorText}`);
|
|
132
|
+
}
|
|
133
|
+
const data = await response.json();
|
|
134
|
+
const choices = data.choices;
|
|
135
|
+
// OpenRouter can return HTTP 200 with an { error } envelope and no choices.
|
|
136
|
+
// Surface the provider message instead of returning a fake-empty success.
|
|
137
|
+
if (!choices?.length && data.error != null) {
|
|
138
|
+
const err = data.error;
|
|
139
|
+
const message = typeof err === 'string' ? err : err.message ?? 'OpenRouter returned an error';
|
|
140
|
+
throw new Error(`OpenRouter API error: ${message}`);
|
|
141
|
+
}
|
|
142
|
+
const content = choices?.[0]?.message?.content ?? '';
|
|
143
|
+
const rawUsage = data.usage;
|
|
144
|
+
const usage = {
|
|
145
|
+
promptTokens: rawUsage?.prompt_tokens,
|
|
146
|
+
completionTokens: rawUsage?.completion_tokens,
|
|
147
|
+
totalTokens: rawUsage?.total_tokens,
|
|
148
|
+
};
|
|
149
|
+
return {
|
|
150
|
+
content,
|
|
151
|
+
modelUsed: modelId,
|
|
152
|
+
openrouterModel,
|
|
153
|
+
routeReasoning: reasoning,
|
|
154
|
+
usage,
|
|
155
|
+
rawResponse: data,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
// -----------------------------------------------------------------------
|
|
159
|
+
// stream -- async generator
|
|
160
|
+
// -----------------------------------------------------------------------
|
|
161
|
+
/**
|
|
162
|
+
* Pick the best model for the prompt and stream the response.
|
|
163
|
+
*
|
|
164
|
+
* Yields content chunks as they arrive from the API.
|
|
165
|
+
*/
|
|
166
|
+
async *stream(prompt, options) {
|
|
167
|
+
this._ensureApiKey();
|
|
168
|
+
const priorities = mergePriorities(options?.priorities ?? this._defaultPriorities);
|
|
169
|
+
const coreResult = await this._router.route(prompt, {
|
|
170
|
+
priorities: Priorities.fromDict(priorities),
|
|
171
|
+
});
|
|
172
|
+
if (!coreResult.bestModel) {
|
|
173
|
+
throw new Error('routing returned no model for this prompt');
|
|
174
|
+
}
|
|
175
|
+
const modelId = coreResult.bestModel;
|
|
176
|
+
const openrouterModel = resolveModel(modelId);
|
|
177
|
+
// Build messages
|
|
178
|
+
const messages = [];
|
|
179
|
+
if (options?.systemMessage) {
|
|
180
|
+
messages.push({ role: 'system', content: options.systemMessage });
|
|
181
|
+
}
|
|
182
|
+
messages.push({ role: 'user', content: prompt });
|
|
183
|
+
// Build payload
|
|
184
|
+
const payload = {
|
|
185
|
+
model: openrouterModel,
|
|
186
|
+
messages,
|
|
187
|
+
temperature: options?.temperature ?? 0.7,
|
|
188
|
+
stream: true,
|
|
189
|
+
};
|
|
190
|
+
// Only send max_tokens when it is a finite, positive number (0 is not meaningful).
|
|
191
|
+
if (options?.maxTokens != null && Number.isFinite(options.maxTokens) && options.maxTokens > 0) {
|
|
192
|
+
payload.max_tokens = options.maxTokens;
|
|
193
|
+
}
|
|
194
|
+
// Call OpenRouter API with streaming
|
|
195
|
+
const response = await fetch(`${this._baseUrl}/chat/completions`, {
|
|
196
|
+
method: 'POST',
|
|
197
|
+
headers: {
|
|
198
|
+
'Authorization': `Bearer ${this._apiKey}`,
|
|
199
|
+
'Content-Type': 'application/json',
|
|
200
|
+
'X-Title': 'tryaii-dre',
|
|
201
|
+
},
|
|
202
|
+
body: JSON.stringify(payload),
|
|
203
|
+
});
|
|
204
|
+
if (!response.ok) {
|
|
205
|
+
const errorText = await response.text();
|
|
206
|
+
throw new Error(`OpenRouter API error (${response.status}): ${errorText}`);
|
|
207
|
+
}
|
|
208
|
+
if (!response.body) {
|
|
209
|
+
throw new Error('Response body is null -- streaming not supported');
|
|
210
|
+
}
|
|
211
|
+
const reader = response.body.getReader();
|
|
212
|
+
const decoder = new TextDecoder();
|
|
213
|
+
let buffer = '';
|
|
214
|
+
try {
|
|
215
|
+
while (true) {
|
|
216
|
+
const { done, value } = await reader.read();
|
|
217
|
+
if (done)
|
|
218
|
+
break;
|
|
219
|
+
buffer += decoder.decode(value, { stream: true });
|
|
220
|
+
const lines = buffer.split('\n');
|
|
221
|
+
buffer = lines.pop() ?? '';
|
|
222
|
+
for (const line of lines) {
|
|
223
|
+
const trimmed = line.trim();
|
|
224
|
+
if (!trimmed || !trimmed.startsWith('data: '))
|
|
225
|
+
continue;
|
|
226
|
+
const dataStr = trimmed.slice(6);
|
|
227
|
+
if (dataStr === '[DONE]')
|
|
228
|
+
return;
|
|
229
|
+
let chunk;
|
|
230
|
+
try {
|
|
231
|
+
chunk = JSON.parse(dataStr);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
// Skip malformed SSE chunks
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
// OpenRouter can stream an { error } chunk; surface it before reading the delta.
|
|
238
|
+
// Thrown outside the parse try/catch so it is not swallowed as a malformed chunk.
|
|
239
|
+
if (chunk.error != null) {
|
|
240
|
+
const err = chunk.error;
|
|
241
|
+
const message = typeof err === 'string' ? err : err.message ?? 'OpenRouter returned an error';
|
|
242
|
+
throw new Error(`OpenRouter stream error: ${message}`);
|
|
243
|
+
}
|
|
244
|
+
const content = chunk.choices?.[0]?.delta?.content;
|
|
245
|
+
if (content)
|
|
246
|
+
yield content;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
finally {
|
|
251
|
+
reader.releaseLock();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// -----------------------------------------------------------------------
|
|
255
|
+
// internal: convert core RouteResult to SDK shape
|
|
256
|
+
// -----------------------------------------------------------------------
|
|
257
|
+
_toSdkResult(
|
|
258
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
259
|
+
coreResult, priorities) {
|
|
260
|
+
const scores = (coreResult.scores ?? []).map(
|
|
261
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
262
|
+
(s) => ({
|
|
263
|
+
modelId: s.modelId,
|
|
264
|
+
finalScore: s.finalScore,
|
|
265
|
+
qualityScore: s.qualityScore,
|
|
266
|
+
costScore: s.costScore,
|
|
267
|
+
speedScore: s.speedScore,
|
|
268
|
+
reasoning: s.reasoning,
|
|
269
|
+
}));
|
|
270
|
+
return {
|
|
271
|
+
bestModel: coreResult.bestModel ?? '',
|
|
272
|
+
scores,
|
|
273
|
+
bestScore: scores[0]?.finalScore ?? 0,
|
|
274
|
+
bestReasoning: scores[0]?.reasoning ?? '',
|
|
275
|
+
priorities,
|
|
276
|
+
};
|
|
277
|
+
}
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AAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC;oBACnD,IAAI,OAAO;wBAAE,MAAM,OAAO,CAAC;gBAC7B,CAAC;YACH,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,WAAW,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;IAED,0EAA0E;IAC1E,kDAAkD;IAClD,0EAA0E;IAElE,YAAY;IAClB,8DAA8D;IAC9D,UAAe,EACf,UAA0B;QAE1B,MAAM,MAAM,GAAiB,CAAC,UAAU,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,GAAG;QACxD,8DAA8D;QAC9D,CAAC,CAAM,EAAc,EAAE,CAAC,CAAC;YACvB,OAAO,EAAE,CAAC,CAAC,OAAO;YAClB,UAAU,EAAE,CAAC,CAAC,UAAU;YACxB,YAAY,EAAE,CAAC,CAAC,YAAY;YAC5B,SAAS,EAAE,CAAC,CAAC,SAAS;YACtB,UAAU,EAAE,CAAC,CAAC,UAAU;YACxB,SAAS,EAAE,CAAC,CAAC,SAAS;SACvB,CAAC,CACH,CAAC;QAEF,OAAO;YACL,SAAS,EAAE,UAAU,CAAC,SAAS,IAAI,EAAE;YACrC,MAAM;YACN,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,UAAU,IAAI,CAAC;YACrC,aAAa,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,SAAS,IAAI,EAAE;YACzC,UAAU;SACX,CAAC;IACJ,CAAC;CACF"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global configuration for TryAii-DRE.
|
|
3
|
+
*/
|
|
4
|
+
import type { ScoringStrategy } from './types.js';
|
|
5
|
+
/** Default data directory: ~/.tryaii_dre/ */
|
|
6
|
+
export declare const DEFAULT_DATA_DIR: string;
|
|
7
|
+
/** Default embedding model -- small, fast, runs on any modern CPU. */
|
|
8
|
+
export declare const DEFAULT_EMBEDDING_MODEL = "all-MiniLM-L6-v2";
|
|
9
|
+
/** Default embedding dimension for all-MiniLM-L6-v2. */
|
|
10
|
+
export declare const DEFAULT_EMBEDDING_DIMENSION = 384;
|
|
11
|
+
/** Cache configuration. */
|
|
12
|
+
export interface CacheConfig {
|
|
13
|
+
/** Max number of cached embedding vectors. */
|
|
14
|
+
embeddingCacheSize: number;
|
|
15
|
+
/** Max number of cached classification results. */
|
|
16
|
+
classificationCacheSize: number;
|
|
17
|
+
/** Time-to-live for cache entries in seconds. */
|
|
18
|
+
ttlSeconds: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Main configuration object.
|
|
22
|
+
*
|
|
23
|
+
* Can be passed to Router() to override defaults.
|
|
24
|
+
*/
|
|
25
|
+
export interface TryaiiDreConfig {
|
|
26
|
+
/** Embedding model name (sentence-transformers / HuggingFace model). */
|
|
27
|
+
embeddingModel: string;
|
|
28
|
+
/** Where to store centroids, cached models, etc. */
|
|
29
|
+
dataDir: string;
|
|
30
|
+
/** Cache settings. */
|
|
31
|
+
cache: CacheConfig;
|
|
32
|
+
/** Scoring strategy preset. */
|
|
33
|
+
strategy: ScoringStrategy;
|
|
34
|
+
/** OpenAI API key (only needed if using OpenAI embeddings). */
|
|
35
|
+
openaiApiKey: string | undefined;
|
|
36
|
+
/** OpenRouter API key (only needed for active routing integration). */
|
|
37
|
+
openrouterApiKey: string | undefined;
|
|
38
|
+
}
|
|
39
|
+
/** Create a TryaiiDreConfig with safe defaults. */
|
|
40
|
+
export declare function createDefaultConfig(overrides?: Partial<TryaiiDreConfig>): TryaiiDreConfig;
|
|
41
|
+
/** Get the centroids directory path from config. */
|
|
42
|
+
export declare function centroidsDir(config: TryaiiDreConfig): string;
|
|
43
|
+
/** Get the centroid file path for the current embedding model. */
|
|
44
|
+
export declare function centroidFilePath(config: TryaiiDreConfig): string;
|
|
45
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD,6CAA6C;AAC7C,eAAO,MAAM,gBAAgB,QAAiC,CAAC;AAE/D,sEAAsE;AACtE,eAAO,MAAM,uBAAuB,qBAAqB,CAAC;AAE1D,wDAAwD;AACxD,eAAO,MAAM,2BAA2B,MAAM,CAAC;AAE/C,2BAA2B;AAC3B,MAAM,WAAW,WAAW;IAC1B,8CAA8C;IAC9C,kBAAkB,EAAE,MAAM,CAAC;IAC3B,mDAAmD;IACnD,uBAAuB,EAAE,MAAM,CAAC;IAChC,iDAAiD;IACjD,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,wEAAwE;IACxE,cAAc,EAAE,MAAM,CAAC;IAEvB,oDAAoD;IACpD,OAAO,EAAE,MAAM,CAAC;IAEhB,sBAAsB;IACtB,KAAK,EAAE,WAAW,CAAC;IAEnB,+BAA+B;IAC/B,QAAQ,EAAE,eAAe,CAAC;IAE1B,+DAA+D;IAC/D,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IAEjC,uEAAuE;IACvE,gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAC;CACtC;AAED,mDAAmD;AACnD,wBAAgB,mBAAmB,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,GAAG,eAAe,CAczF;AAED,oDAAoD;AACpD,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM,CAE5D;AAED,kEAAkE;AAClE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM,CAGhE"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global configuration for TryAii-DRE.
|
|
3
|
+
*/
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
/** Default data directory: ~/.tryaii_dre/ */
|
|
7
|
+
export const DEFAULT_DATA_DIR = join(homedir(), '.tryaii_dre');
|
|
8
|
+
/** Default embedding model -- small, fast, runs on any modern CPU. */
|
|
9
|
+
export const DEFAULT_EMBEDDING_MODEL = 'all-MiniLM-L6-v2';
|
|
10
|
+
/** Default embedding dimension for all-MiniLM-L6-v2. */
|
|
11
|
+
export const DEFAULT_EMBEDDING_DIMENSION = 384;
|
|
12
|
+
/** Create a TryaiiDreConfig with safe defaults. */
|
|
13
|
+
export function createDefaultConfig(overrides) {
|
|
14
|
+
return {
|
|
15
|
+
embeddingModel: DEFAULT_EMBEDDING_MODEL,
|
|
16
|
+
dataDir: DEFAULT_DATA_DIR,
|
|
17
|
+
cache: {
|
|
18
|
+
embeddingCacheSize: 300,
|
|
19
|
+
classificationCacheSize: 150,
|
|
20
|
+
ttlSeconds: 300,
|
|
21
|
+
},
|
|
22
|
+
strategy: 'balanced',
|
|
23
|
+
openaiApiKey: undefined,
|
|
24
|
+
openrouterApiKey: undefined,
|
|
25
|
+
...overrides,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Get the centroids directory path from config. */
|
|
29
|
+
export function centroidsDir(config) {
|
|
30
|
+
return join(config.dataDir, 'centroids');
|
|
31
|
+
}
|
|
32
|
+
/** Get the centroid file path for the current embedding model. */
|
|
33
|
+
export function centroidFilePath(config) {
|
|
34
|
+
const safeName = config.embeddingModel.replace(/\//g, '__');
|
|
35
|
+
return join(centroidsDir(config), `centroids_${safeName}.json`);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAIjC,6CAA6C;AAC7C,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,aAAa,CAAC,CAAC;AAE/D,sEAAsE;AACtE,MAAM,CAAC,MAAM,uBAAuB,GAAG,kBAAkB,CAAC;AAE1D,wDAAwD;AACxD,MAAM,CAAC,MAAM,2BAA2B,GAAG,GAAG,CAAC;AAqC/C,mDAAmD;AACnD,MAAM,UAAU,mBAAmB,CAAC,SAAoC;IACtE,OAAO;QACL,cAAc,EAAE,uBAAuB;QACvC,OAAO,EAAE,gBAAgB;QACzB,KAAK,EAAE;YACL,kBAAkB,EAAE,GAAG;YACvB,uBAAuB,EAAE,GAAG;YAC5B,UAAU,EAAE,GAAG;SAChB;QACD,QAAQ,EAAE,UAAU;QACpB,YAAY,EAAE,SAAS;QACvB,gBAAgB,EAAE,SAAS;QAC3B,GAAG,SAAS;KACb,CAAC;AACJ,CAAC;AAED,oDAAoD;AACpD,MAAM,UAAU,YAAY,CAAC,MAAuB;IAClD,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;AAC3C,CAAC;AAED,kEAAkE;AAClE,MAAM,UAAU,gBAAgB,CAAC,MAAuB;IACtD,MAAM,QAAQ,GAAG,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC5D,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE,aAAa,QAAQ,OAAO,CAAC,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Self-contained HTML dashboard for an eval run.
|
|
3
|
+
*
|
|
4
|
+
* Reads the same shape that gets written to `summary.json`, so it can be used
|
|
5
|
+
* both at the end of a live run and as a backfill over existing runs.
|
|
6
|
+
*/
|
|
7
|
+
export interface DashboardSummary {
|
|
8
|
+
totalPrompts: number;
|
|
9
|
+
successCount: number;
|
|
10
|
+
errorCount: number;
|
|
11
|
+
distinctModels: number;
|
|
12
|
+
avgRouteMs: number;
|
|
13
|
+
totalRouteMs: number;
|
|
14
|
+
priorities: {
|
|
15
|
+
quality: number;
|
|
16
|
+
cost: number;
|
|
17
|
+
speed: number;
|
|
18
|
+
};
|
|
19
|
+
distribution: Array<{
|
|
20
|
+
model: string;
|
|
21
|
+
count: number;
|
|
22
|
+
pct: number;
|
|
23
|
+
}>;
|
|
24
|
+
byCategory: Array<{
|
|
25
|
+
category: string;
|
|
26
|
+
count: number;
|
|
27
|
+
topModels: Array<{
|
|
28
|
+
model: string;
|
|
29
|
+
count: number;
|
|
30
|
+
pct: number;
|
|
31
|
+
}>;
|
|
32
|
+
topBenchmarks: Array<{
|
|
33
|
+
name: string;
|
|
34
|
+
avgScore: number;
|
|
35
|
+
}>;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Override the relative `summary.json` / `results.jsonl` footer links with
|
|
40
|
+
* absolute URLs. Needed when the dashboard is rendered to a directory
|
|
41
|
+
* (e.g. system temp) that doesn't sit next to the artifacts.
|
|
42
|
+
*/
|
|
43
|
+
export interface DashboardLinks {
|
|
44
|
+
summaryHref?: string;
|
|
45
|
+
resultsHref?: string;
|
|
46
|
+
}
|
|
47
|
+
export declare function renderDashboard(summary: DashboardSummary, inputPath: string, links?: DashboardLinks): string;
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dashboard/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,gBAAgB;IAC/B,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE;QAAE,OAAO,EAAE,MAAM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