@draht/router 2026.7.12-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 +90 -0
- package/dist/cli.d.ts +26 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +85 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +31 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +224 -0
- package/dist/config.js.map +1 -0
- package/dist/cost.d.ts +47 -0
- package/dist/cost.d.ts.map +1 -0
- package/dist/cost.js +87 -0
- package/dist/cost.js.map +1 -0
- package/dist/extension.d.ts +27 -0
- package/dist/extension.d.ts.map +1 -0
- package/dist/extension.js +84 -0
- package/dist/extension.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/router.d.ts +63 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +190 -0
- package/dist/router.js.map +1 -0
- package/dist/types.d.ts +48 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +45 -0
- package/dist/types.js.map +1 -0
- package/package.json +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# @draht/router
|
|
2
|
+
|
|
3
|
+
Role-based model routing with direct API calls, automatic fallback, and cost tracking.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The router maps task roles to LLM provider/model combinations with ordered fallback chains. No OpenRouter — direct API calls only for minimal latency.
|
|
8
|
+
|
|
9
|
+
## Configuration
|
|
10
|
+
|
|
11
|
+
Config is loaded from (in priority order):
|
|
12
|
+
1. `.draht/router.json` (project-level)
|
|
13
|
+
2. `~/.draht/router.json` (global)
|
|
14
|
+
3. Built-in defaults
|
|
15
|
+
|
|
16
|
+
### Example `.draht/router.json`
|
|
17
|
+
|
|
18
|
+
```json
|
|
19
|
+
{
|
|
20
|
+
"architect": {
|
|
21
|
+
"primary": { "provider": "anthropic", "model": "claude-opus-4-6" },
|
|
22
|
+
"fallbacks": [{ "provider": "google", "model": "gemini-2.5-pro" }]
|
|
23
|
+
},
|
|
24
|
+
"implement": {
|
|
25
|
+
"primary": { "provider": "anthropic", "model": "claude-sonnet-4-6" },
|
|
26
|
+
"fallbacks": [
|
|
27
|
+
{ "provider": "openai", "model": "gpt-5.2" },
|
|
28
|
+
{ "provider": "deepseek", "model": "deepseek-v3" }
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Default Role Mapping
|
|
35
|
+
|
|
36
|
+
| Role | Primary | Fallbacks |
|
|
37
|
+
|------|---------|-----------|
|
|
38
|
+
| architect | anthropic/claude-opus-4-6 | google/gemini-2.5-pro |
|
|
39
|
+
| implement | anthropic/claude-sonnet-4-6 | openai/gpt-5.2 → deepseek/deepseek-v3 |
|
|
40
|
+
| boilerplate | deepseek/deepseek-v3 | google/gemini-2.5-flash |
|
|
41
|
+
| quick | google/gemini-2.5-flash | deepseek/deepseek-v3 |
|
|
42
|
+
| review | anthropic/claude-sonnet-4-6 | — |
|
|
43
|
+
| docs | openai/gpt-5.2 | anthropic/claude-sonnet-4-6 |
|
|
44
|
+
|
|
45
|
+
## CLI Usage
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# Show current configuration
|
|
49
|
+
draht router show
|
|
50
|
+
|
|
51
|
+
# Set model for a role
|
|
52
|
+
draht router set implement anthropic/claude-sonnet-4-6 --fallback openai/gpt-5.2,deepseek/deepseek-v3
|
|
53
|
+
|
|
54
|
+
# Test resolution (dry-run)
|
|
55
|
+
draht router test
|
|
56
|
+
draht router test architect
|
|
57
|
+
|
|
58
|
+
# View cost summary
|
|
59
|
+
draht router cost
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Fallback Behavior
|
|
63
|
+
|
|
64
|
+
When the primary model fails with a retryable error (429, 5xx, timeout), the router automatically tries the next model in the fallback chain. Non-retryable errors (401, 400) are thrown immediately.
|
|
65
|
+
|
|
66
|
+
## Cost Tracking
|
|
67
|
+
|
|
68
|
+
Every API call is logged to `.draht/cost-log.jsonl` with:
|
|
69
|
+
- Timestamp, role, provider/model
|
|
70
|
+
- Input/output token counts
|
|
71
|
+
- Estimated cost in USD
|
|
72
|
+
- Session ID
|
|
73
|
+
|
|
74
|
+
## Coding Agent Extension
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { createRouterExtension } from "@draht/router/extension";
|
|
78
|
+
|
|
79
|
+
const ext = createRouterExtension();
|
|
80
|
+
const model = ext.selectModel("implementation"); // → anthropic/claude-sonnet-4-6
|
|
81
|
+
const chain = ext.selectModelWithFallbacks("architecture"); // → full fallback chain
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Task types are automatically mapped to roles:
|
|
85
|
+
- `planning`, `architecture` → architect
|
|
86
|
+
- `implementation`, `coding` → implement
|
|
87
|
+
- `scaffold`, `boilerplate` → boilerplate
|
|
88
|
+
- `question`, `quick` → quick
|
|
89
|
+
- `review`, `pr-review` → review
|
|
90
|
+
- `documentation`, `readme` → docs
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ModelRef } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parse "provider/model" string into ModelRef.
|
|
4
|
+
*/
|
|
5
|
+
export declare function parseModelRef(ref: string): ModelRef;
|
|
6
|
+
/**
|
|
7
|
+
* Format ModelRef as "provider/model" string.
|
|
8
|
+
*/
|
|
9
|
+
export declare function formatModelRef(ref: ModelRef): string;
|
|
10
|
+
/**
|
|
11
|
+
* Show current router configuration.
|
|
12
|
+
*/
|
|
13
|
+
export declare function routerShow(projectRoot?: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Set model for a role.
|
|
16
|
+
*/
|
|
17
|
+
export declare function routerSet(role: string, modelRefStr: string, fallbackStrs?: string[], scope?: "project" | "global", projectRoot?: string): void;
|
|
18
|
+
/**
|
|
19
|
+
* Test router resolution — dry-run showing what model would be selected.
|
|
20
|
+
*/
|
|
21
|
+
export declare function routerTest(role?: string, projectRoot?: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Show cost summary.
|
|
24
|
+
*/
|
|
25
|
+
export declare function routerCost(role?: string, logPath?: string): string;
|
|
26
|
+
//# sourceMappingURL=cli.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAc,MAAM,YAAY,CAAC;AAEvD;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CASnD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,QAAQ,GAAG,MAAM,CAEpD;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,CAYvD;AAED;;GAEG;AACH,wBAAgB,SAAS,CACxB,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,YAAY,GAAE,MAAM,EAAO,EAC3B,KAAK,GAAE,SAAS,GAAG,QAAoB,EACvC,WAAW,CAAC,EAAE,MAAM,GAClB,IAAI,CAON;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,CAkBtE;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAoBlE"}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { loadConfig, saveConfig } from "./config.js";
|
|
2
|
+
import { getRoleCosts } from "./cost.js";
|
|
3
|
+
import { ModelRouter } from "./router.js";
|
|
4
|
+
/**
|
|
5
|
+
* Parse "provider/model" string into ModelRef.
|
|
6
|
+
*/
|
|
7
|
+
export function parseModelRef(ref) {
|
|
8
|
+
const slashIndex = ref.indexOf("/");
|
|
9
|
+
if (slashIndex === -1) {
|
|
10
|
+
throw new Error(`Invalid model ref "${ref}" — expected "provider/model"`);
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
provider: ref.slice(0, slashIndex),
|
|
14
|
+
model: ref.slice(slashIndex + 1),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Format ModelRef as "provider/model" string.
|
|
19
|
+
*/
|
|
20
|
+
export function formatModelRef(ref) {
|
|
21
|
+
return `${ref.provider}/${ref.model}`;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Show current router configuration.
|
|
25
|
+
*/
|
|
26
|
+
export function routerShow(projectRoot) {
|
|
27
|
+
const config = loadConfig(projectRoot);
|
|
28
|
+
const lines = ["Model Router Configuration", ""];
|
|
29
|
+
for (const [role, roleConfig] of Object.entries(config)) {
|
|
30
|
+
const primary = formatModelRef(roleConfig.primary);
|
|
31
|
+
const fallbacks = roleConfig.fallbacks.map(formatModelRef);
|
|
32
|
+
const chain = fallbacks.length > 0 ? `${primary} → ${fallbacks.join(" → ")}` : primary;
|
|
33
|
+
lines.push(` ${role.padEnd(14)} ${chain}`);
|
|
34
|
+
}
|
|
35
|
+
return lines.join("\n");
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Set model for a role.
|
|
39
|
+
*/
|
|
40
|
+
export function routerSet(role, modelRefStr, fallbackStrs = [], scope = "project", projectRoot) {
|
|
41
|
+
const config = loadConfig(projectRoot);
|
|
42
|
+
const primary = parseModelRef(modelRefStr);
|
|
43
|
+
const fallbacks = fallbackStrs.map(parseModelRef);
|
|
44
|
+
config[role] = { primary, fallbacks };
|
|
45
|
+
saveConfig(config, scope, projectRoot);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Test router resolution — dry-run showing what model would be selected.
|
|
49
|
+
*/
|
|
50
|
+
export function routerTest(role, projectRoot) {
|
|
51
|
+
const config = loadConfig(projectRoot);
|
|
52
|
+
const router = new ModelRouter(config);
|
|
53
|
+
const lines = ["Router Test (dry-run)", ""];
|
|
54
|
+
const roles = role ? [role] : Object.keys(config);
|
|
55
|
+
for (const r of roles) {
|
|
56
|
+
try {
|
|
57
|
+
const primary = router.resolve(r);
|
|
58
|
+
const all = router.resolveWithFallbacks(r);
|
|
59
|
+
lines.push(` ${r}: ${formatModelRef(primary)} (${all.length} total in chain)`);
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
lines.push(` ${r}: ERROR — ${e instanceof Error ? e.message : String(e)}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return lines.join("\n");
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Show cost summary.
|
|
69
|
+
*/
|
|
70
|
+
export function routerCost(role, logPath) {
|
|
71
|
+
const config = loadConfig();
|
|
72
|
+
const lines = ["Cost Summary", ""];
|
|
73
|
+
const roles = role ? [role] : Object.keys(config);
|
|
74
|
+
for (const r of roles) {
|
|
75
|
+
const costs = getRoleCosts(r, logPath);
|
|
76
|
+
if (costs.totalCost > 0) {
|
|
77
|
+
lines.push(` ${r.padEnd(14)} $${costs.totalCost.toFixed(4)} (${costs.totalInputTokens} in / ${costs.totalOutputTokens} out)`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (lines.length === 2) {
|
|
81
|
+
lines.push(" No cost data recorded yet.");
|
|
82
|
+
}
|
|
83
|
+
return lines.join("\n");
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;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fig, saveConfig } from \"./config.js\";\nimport { getRoleCosts } from \"./cost.js\";\nimport { ModelRouter } from \"./router.js\";\nimport type { ModelRef, RoleConfig } from \"./types.js\";\n\n/**\n * Parse \"provider/model\" string into ModelRef.\n */\nexport function parseModelRef(ref: string): ModelRef {\n\tconst slashIndex = ref.indexOf(\"/\");\n\tif (slashIndex === -1) {\n\t\tthrow new Error(`Invalid model ref \"${ref}\" — expected \"provider/model\"`);\n\t}\n\treturn {\n\t\tprovider: ref.slice(0, slashIndex),\n\t\tmodel: ref.slice(slashIndex + 1),\n\t};\n}\n\n/**\n * Format ModelRef as \"provider/model\" string.\n */\nexport function formatModelRef(ref: ModelRef): string {\n\treturn `${ref.provider}/${ref.model}`;\n}\n\n/**\n * Show current router configuration.\n */\nexport function routerShow(projectRoot?: string): string {\n\tconst config = loadConfig(projectRoot);\n\tconst lines: string[] = [\"Model Router Configuration\", \"\"];\n\n\tfor (const [role, roleConfig] of Object.entries(config)) {\n\t\tconst primary = formatModelRef(roleConfig.primary);\n\t\tconst fallbacks = roleConfig.fallbacks.map(formatModelRef);\n\t\tconst chain = fallbacks.length > 0 ? `${primary} → ${fallbacks.join(\" → \")}` : primary;\n\t\tlines.push(` ${role.padEnd(14)} ${chain}`);\n\t}\n\n\treturn lines.join(\"\\n\");\n}\n\n/**\n * Set model for a role.\n */\nexport function routerSet(\n\trole: string,\n\tmodelRefStr: string,\n\tfallbackStrs: string[] = [],\n\tscope: \"project\" | \"global\" = \"project\",\n\tprojectRoot?: string,\n): void {\n\tconst config = loadConfig(projectRoot);\n\tconst primary = parseModelRef(modelRefStr);\n\tconst fallbacks = fallbackStrs.map(parseModelRef);\n\n\tconfig[role] = { primary, fallbacks } satisfies RoleConfig;\n\tsaveConfig(config, scope, projectRoot);\n}\n\n/**\n * Test router resolution — dry-run showing what model would be selected.\n */\nexport function routerTest(role?: string, projectRoot?: string): string {\n\tconst config = loadConfig(projectRoot);\n\tconst router = new ModelRouter(config);\n\tconst lines: string[] = [\"Router Test (dry-run)\", \"\"];\n\n\tconst roles = role ? [role] : Object.keys(config);\n\n\tfor (const r of roles) {\n\t\ttry {\n\t\t\tconst primary = router.resolve(r);\n\t\t\tconst all = router.resolveWithFallbacks(r);\n\t\t\tlines.push(` ${r}: ${formatModelRef(primary)} (${all.length} total in chain)`);\n\t\t} catch (e) {\n\t\t\tlines.push(` ${r}: ERROR — ${e instanceof Error ? e.message : String(e)}`);\n\t\t}\n\t}\n\n\treturn lines.join(\"\\n\");\n}\n\n/**\n * Show cost summary.\n */\nexport function routerCost(role?: string, logPath?: string): string {\n\tconst config = loadConfig();\n\tconst lines: string[] = [\"Cost Summary\", \"\"];\n\n\tconst roles = role ? [role] : Object.keys(config);\n\n\tfor (const r of roles) {\n\t\tconst costs = getRoleCosts(r, logPath);\n\t\tif (costs.totalCost > 0) {\n\t\t\tlines.push(\n\t\t\t\t` ${r.padEnd(14)} $${costs.totalCost.toFixed(4)} (${costs.totalInputTokens} in / ${costs.totalOutputTokens} out)`,\n\t\t\t);\n\t\t}\n\t}\n\n\tif (lines.length === 2) {\n\t\tlines.push(\" No cost data recorded yet.\");\n\t}\n\n\treturn lines.join(\"\\n\");\n}\n"]}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BUILT_IN_ROLES, type RouterConfig } from "./types.js";
|
|
2
|
+
export { BUILT_IN_ROLES };
|
|
3
|
+
/**
|
|
4
|
+
* Error thrown when config validation fails.
|
|
5
|
+
* Contains all validation issues found.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ConfigValidationError extends Error {
|
|
8
|
+
readonly errors: string[];
|
|
9
|
+
constructor(errors: string[]);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Load router config. Project config overrides global, both fall back to defaults.
|
|
13
|
+
* Validates the merged config and throws ConfigValidationError if invalid.
|
|
14
|
+
*/
|
|
15
|
+
export declare function loadConfig(projectRoot?: string): RouterConfig;
|
|
16
|
+
/**
|
|
17
|
+
* Save router config to project or global scope.
|
|
18
|
+
* Validates the config and throws ConfigValidationError if invalid.
|
|
19
|
+
*/
|
|
20
|
+
export declare function saveConfig(config: RouterConfig, scope: "project" | "global", projectRoot?: string): void;
|
|
21
|
+
/**
|
|
22
|
+
* Merge project config over global config over defaults.
|
|
23
|
+
* Project overrides global per-role.
|
|
24
|
+
*/
|
|
25
|
+
export declare function mergeConfig(project: RouterConfig | null, global: RouterConfig | null): RouterConfig;
|
|
26
|
+
/**
|
|
27
|
+
* Validate a router config.
|
|
28
|
+
* Throws ConfigValidationError if any issues are found.
|
|
29
|
+
*/
|
|
30
|
+
export declare function validateConfig(config: RouterConfig): void;
|
|
31
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAkD,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/G,OAAO,EAAE,cAAc,EAAE,CAAC;AAE1B;;;GAGG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;aACnB,MAAM,EAAE,MAAM,EAAE;IAA5C,YAA4B,MAAM,EAAE,MAAM,EAAE,EAG3C;CACD;AA6CD;;;GAGG;AACH,wBAAgB,UAAU,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,YAAY,CAY7D;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,SAAS,GAAG,QAAQ,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAaxG;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,GAAG,YAAY,CAgBnG;AA0HD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CA+BzD"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir, tmpdir } from "node:os";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { getModels, getProviders } from "@draht/ai/compat";
|
|
5
|
+
import { BUILT_IN_ROLES, DEFAULT_CONFIG } from "./types.js";
|
|
6
|
+
export { BUILT_IN_ROLES };
|
|
7
|
+
/**
|
|
8
|
+
* Error thrown when config validation fails.
|
|
9
|
+
* Contains all validation issues found.
|
|
10
|
+
*/
|
|
11
|
+
export class ConfigValidationError extends Error {
|
|
12
|
+
constructor(errors) {
|
|
13
|
+
super(`Config validation failed:\n${errors.map((e) => ` - ${e}`).join("\n")}`);
|
|
14
|
+
this.errors = errors;
|
|
15
|
+
this.name = "ConfigValidationError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const PROJECT_CONFIG = ".draht/router.json";
|
|
19
|
+
const GLOBAL_CONFIG = join(homedir(), ".draht", "router.json");
|
|
20
|
+
/**
|
|
21
|
+
* Validate that a project root path is safe and doesn't contain path traversal.
|
|
22
|
+
* Throws if the path attempts to escape the current working directory or uses absolute paths to sensitive areas.
|
|
23
|
+
*/
|
|
24
|
+
function validateProjectRoot(projectRoot) {
|
|
25
|
+
// Resolve to absolute path to check for traversal
|
|
26
|
+
const resolved = resolve(projectRoot);
|
|
27
|
+
const cwd = resolve(process.cwd());
|
|
28
|
+
// For relative paths: ensure resolved path is within or equal to cwd
|
|
29
|
+
if (!projectRoot.startsWith("/")) {
|
|
30
|
+
if (!resolved.startsWith(`${cwd}/`) && resolved !== cwd) {
|
|
31
|
+
throw new Error(`Invalid project root: path traversal detected (${projectRoot})`);
|
|
32
|
+
}
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// For absolute paths: allow safe locations (user home, temp dirs, current working directory)
|
|
36
|
+
const home = homedir();
|
|
37
|
+
const tmpDir = tmpdir();
|
|
38
|
+
const safePaths = [home, tmpDir, cwd, "/tmp", "/private/tmp"];
|
|
39
|
+
const isSafe = safePaths.some((safe) => resolved.startsWith(`${safe}/`) || resolved === safe);
|
|
40
|
+
if (isSafe) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
// Reject paths to system directories
|
|
44
|
+
const dangerousPaths = ["/etc", "/usr", "/bin", "/sbin", "/sys", "/proc", "/dev", "/boot"];
|
|
45
|
+
for (const dangerous of dangerousPaths) {
|
|
46
|
+
if (resolved.startsWith(dangerous)) {
|
|
47
|
+
throw new Error(`Invalid project root: access to ${dangerous} is not allowed`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// Reject any other absolute path that's not explicitly safe
|
|
51
|
+
throw new Error(`Invalid project root: absolute path must be within home, temp, or current directory (${projectRoot})`);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Load router config. Project config overrides global, both fall back to defaults.
|
|
55
|
+
* Validates the merged config and throws ConfigValidationError if invalid.
|
|
56
|
+
*/
|
|
57
|
+
export function loadConfig(projectRoot) {
|
|
58
|
+
if (projectRoot) {
|
|
59
|
+
validateProjectRoot(projectRoot);
|
|
60
|
+
}
|
|
61
|
+
const globalConfig = loadFile(GLOBAL_CONFIG);
|
|
62
|
+
const projectPath = projectRoot ? resolve(projectRoot, PROJECT_CONFIG) : resolve(PROJECT_CONFIG);
|
|
63
|
+
const projectConfig = loadFile(projectPath);
|
|
64
|
+
const result = mergeConfig(projectConfig, globalConfig);
|
|
65
|
+
validateConfig(result);
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Save router config to project or global scope.
|
|
70
|
+
* Validates the config and throws ConfigValidationError if invalid.
|
|
71
|
+
*/
|
|
72
|
+
export function saveConfig(config, scope, projectRoot) {
|
|
73
|
+
// Validate before writing to ensure we don't save invalid configs
|
|
74
|
+
validateConfig(config);
|
|
75
|
+
if (scope === "project" && projectRoot) {
|
|
76
|
+
validateProjectRoot(projectRoot);
|
|
77
|
+
}
|
|
78
|
+
const path = scope === "global" ? GLOBAL_CONFIG : projectRoot ? resolve(projectRoot, PROJECT_CONFIG) : resolve(PROJECT_CONFIG);
|
|
79
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
80
|
+
writeFileSync(path, JSON.stringify(config, null, "\t"), "utf-8");
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Merge project config over global config over defaults.
|
|
84
|
+
* Project overrides global per-role.
|
|
85
|
+
*/
|
|
86
|
+
export function mergeConfig(project, global) {
|
|
87
|
+
const base = { ...DEFAULT_CONFIG };
|
|
88
|
+
if (global) {
|
|
89
|
+
for (const [role, config] of Object.entries(global)) {
|
|
90
|
+
base[role] = config;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (project) {
|
|
94
|
+
for (const [role, config] of Object.entries(project)) {
|
|
95
|
+
base[role] = config;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return base;
|
|
99
|
+
}
|
|
100
|
+
function loadFile(path) {
|
|
101
|
+
if (!existsSync(path))
|
|
102
|
+
return null;
|
|
103
|
+
try {
|
|
104
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Validate a ModelRef has non-empty provider and model.
|
|
112
|
+
*/
|
|
113
|
+
function validateModelRef(ref, role, position) {
|
|
114
|
+
const errors = [];
|
|
115
|
+
if (!ref.provider || ref.provider.trim() === "") {
|
|
116
|
+
errors.push(`Empty provider string for role ${role} (${position})`);
|
|
117
|
+
}
|
|
118
|
+
if (!ref.model || ref.model.trim() === "") {
|
|
119
|
+
errors.push(`Empty model string for role ${role} (${position})`);
|
|
120
|
+
}
|
|
121
|
+
return errors;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Validate a RoleConfig has valid structure.
|
|
125
|
+
*/
|
|
126
|
+
function validateRoleConfig(roleConfig, role) {
|
|
127
|
+
const errors = [];
|
|
128
|
+
// Validate primary
|
|
129
|
+
errors.push(...validateModelRef(roleConfig.primary, role, "primary"));
|
|
130
|
+
// Validate fallbacks
|
|
131
|
+
for (let i = 0; i < roleConfig.fallbacks.length; i++) {
|
|
132
|
+
errors.push(...validateModelRef(roleConfig.fallbacks[i], role, `fallback[${i}]`));
|
|
133
|
+
}
|
|
134
|
+
return errors;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Validate a ModelRef against the @draht/ai registry.
|
|
138
|
+
* Returns errors if provider or model is not found.
|
|
139
|
+
*/
|
|
140
|
+
function validateModelRefAgainstRegistry(ref, role, position, validProviders) {
|
|
141
|
+
const errors = [];
|
|
142
|
+
// Skip if empty (already caught by structure validation)
|
|
143
|
+
if (!ref.provider || !ref.model) {
|
|
144
|
+
return errors;
|
|
145
|
+
}
|
|
146
|
+
// Check provider exists
|
|
147
|
+
if (!validProviders.has(ref.provider)) {
|
|
148
|
+
errors.push(`Invalid provider '${ref.provider}' for role ${role} (not in @draht/ai registry)`);
|
|
149
|
+
return errors; // Don't check model if provider is invalid
|
|
150
|
+
}
|
|
151
|
+
// Check model exists for this provider
|
|
152
|
+
const models = getModels(ref.provider);
|
|
153
|
+
const modelExists = models.some((m) => m.id === ref.model);
|
|
154
|
+
if (!modelExists) {
|
|
155
|
+
errors.push(`Invalid model '${ref.model}' for provider '${ref.provider}' in role ${role} (${position})`);
|
|
156
|
+
}
|
|
157
|
+
return errors;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Validate a RoleConfig against the @draht/ai registry.
|
|
161
|
+
*/
|
|
162
|
+
function validateRoleConfigAgainstRegistry(roleConfig, role, validProviders) {
|
|
163
|
+
const errors = [];
|
|
164
|
+
// Validate primary
|
|
165
|
+
errors.push(...validateModelRefAgainstRegistry(roleConfig.primary, role, "primary", validProviders));
|
|
166
|
+
// Validate fallbacks
|
|
167
|
+
for (let i = 0; i < roleConfig.fallbacks.length; i++) {
|
|
168
|
+
errors.push(...validateModelRefAgainstRegistry(roleConfig.fallbacks[i], role, `fallback[${i}]`, validProviders));
|
|
169
|
+
}
|
|
170
|
+
return errors;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Format a ModelRef as "provider/model" for display.
|
|
174
|
+
*/
|
|
175
|
+
function formatModelRefForValidation(ref) {
|
|
176
|
+
return `${ref.provider}/${ref.model}`;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Detect duplicate models in a fallback chain (primary + fallbacks).
|
|
180
|
+
*/
|
|
181
|
+
function detectDuplicateModels(roleConfig, role) {
|
|
182
|
+
const errors = [];
|
|
183
|
+
const allModels = [roleConfig.primary, ...roleConfig.fallbacks];
|
|
184
|
+
const seen = new Set();
|
|
185
|
+
for (const ref of allModels) {
|
|
186
|
+
const key = formatModelRefForValidation(ref);
|
|
187
|
+
if (seen.has(key)) {
|
|
188
|
+
errors.push(`Duplicate model ${key} in fallback chain for role ${role}`);
|
|
189
|
+
}
|
|
190
|
+
seen.add(key);
|
|
191
|
+
}
|
|
192
|
+
return errors;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Validate a router config.
|
|
196
|
+
* Throws ConfigValidationError if any issues are found.
|
|
197
|
+
*/
|
|
198
|
+
export function validateConfig(config) {
|
|
199
|
+
const errors = [];
|
|
200
|
+
// Check all built-in roles are present
|
|
201
|
+
for (const role of BUILT_IN_ROLES) {
|
|
202
|
+
if (!(role in config)) {
|
|
203
|
+
errors.push(`Missing required role: ${role}`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
// Validate structure of each role config
|
|
207
|
+
for (const [role, roleConfig] of Object.entries(config)) {
|
|
208
|
+
errors.push(...validateRoleConfig(roleConfig, role));
|
|
209
|
+
}
|
|
210
|
+
// Cache valid providers for registry validation
|
|
211
|
+
const validProviders = new Set(getProviders());
|
|
212
|
+
// Validate each role config against registry
|
|
213
|
+
for (const [role, roleConfig] of Object.entries(config)) {
|
|
214
|
+
errors.push(...validateRoleConfigAgainstRegistry(roleConfig, role, validProviders));
|
|
215
|
+
}
|
|
216
|
+
// Detect duplicate models in fallback chains
|
|
217
|
+
for (const [role, roleConfig] of Object.entries(config)) {
|
|
218
|
+
errors.push(...detectDuplicateModels(roleConfig, role));
|
|
219
|
+
}
|
|
220
|
+
if (errors.length > 0) {
|
|
221
|
+
throw new ConfigValidationError(errors);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,YAAY,EAAsB,MAAM,kBAAkB,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,cAAc,EAAqD,MAAM,YAAY,CAAC;AAE/G,OAAO,EAAE,cAAc,EAAE,CAAC;AAE1B;;;GAGG;AACH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC/C,YAA4B,MAAgB;QAC3C,KAAK,CAAC,8BAA8B,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;sBADrD,MAAM;QAEjC,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACrC,CAAC;CACD;AAED,MAAM,cAAc,GAAG,oBAAoB,CAAC;AAC5C,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,Q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AAC;IACrF,CAAC;IAED,6CAA6C;IAC7C,KAAK,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACzD,MAAM,CAAC,IAAI,CAAC,GAAG,qBAAqB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC,CAAC;IACzD,CAAC;IAED,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,qBAAqB,CAAC,MAAM,CAAC,CAAC;IACzC,CAAC;AACF,CAAC","sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir, tmpdir } from \"node:os\";\nimport { dirname, join, resolve } from \"node:path\";\nimport { getModels, getProviders, type KnownProvider } from \"@draht/ai/compat\";\nimport { BUILT_IN_ROLES, DEFAULT_CONFIG, type ModelRef, type RoleConfig, type RouterConfig } from \"./types.js\";\n\nexport { BUILT_IN_ROLES };\n\n/**\n * Error thrown when config validation fails.\n * Contains all validation issues found.\n */\nexport class ConfigValidationError extends Error {\n\tconstructor(public readonly errors: string[]) {\n\t\tsuper(`Config validation failed:\\n${errors.map((e) => ` - ${e}`).join(\"\\n\")}`);\n\t\tthis.name = \"ConfigValidationError\";\n\t}\n}\n\nconst PROJECT_CONFIG = \".draht/router.json\";\nconst GLOBAL_CONFIG = join(homedir(), \".draht\", \"router.json\");\n\n/**\n * Validate that a project root path is safe and doesn't contain path traversal.\n * Throws if the path attempts to escape the current working directory or uses absolute paths to sensitive areas.\n */\nfunction validateProjectRoot(projectRoot: string): void {\n\t// Resolve to absolute path to check for traversal\n\tconst resolved = resolve(projectRoot);\n\tconst cwd = resolve(process.cwd());\n\n\t// For relative paths: ensure resolved path is within or equal to cwd\n\tif (!projectRoot.startsWith(\"/\")) {\n\t\tif (!resolved.startsWith(`${cwd}/`) && resolved !== cwd) {\n\t\t\tthrow new Error(`Invalid project root: path traversal detected (${projectRoot})`);\n\t\t}\n\t\treturn;\n\t}\n\n\t// For absolute paths: allow safe locations (user home, temp dirs, current working directory)\n\tconst home = homedir();\n\tconst tmpDir = tmpdir();\n\tconst safePaths = [home, tmpDir, cwd, \"/tmp\", \"/private/tmp\"];\n\tconst isSafe = safePaths.some((safe) => resolved.startsWith(`${safe}/`) || resolved === safe);\n\tif (isSafe) {\n\t\treturn;\n\t}\n\n\t// Reject paths to system directories\n\tconst dangerousPaths = [\"/etc\", \"/usr\", \"/bin\", \"/sbin\", \"/sys\", \"/proc\", \"/dev\", \"/boot\"];\n\tfor (const dangerous of dangerousPaths) {\n\t\tif (resolved.startsWith(dangerous)) {\n\t\t\tthrow new Error(`Invalid project root: access to ${dangerous} is not allowed`);\n\t\t}\n\t}\n\n\t// Reject any other absolute path that's not explicitly safe\n\tthrow new Error(\n\t\t`Invalid project root: absolute path must be within home, temp, or current directory (${projectRoot})`,\n\t);\n}\n\n/**\n * Load router config. Project config overrides global, both fall back to defaults.\n * Validates the merged config and throws ConfigValidationError if invalid.\n */\nexport function loadConfig(projectRoot?: string): RouterConfig {\n\tif (projectRoot) {\n\t\tvalidateProjectRoot(projectRoot);\n\t}\n\n\tconst globalConfig = loadFile(GLOBAL_CONFIG);\n\tconst projectPath = projectRoot ? resolve(projectRoot, PROJECT_CONFIG) : resolve(PROJECT_CONFIG);\n\tconst projectConfig = loadFile(projectPath);\n\n\tconst result = mergeConfig(projectConfig, globalConfig);\n\tvalidateConfig(result);\n\treturn result;\n}\n\n/**\n * Save router config to project or global scope.\n * Validates the config and throws ConfigValidationError if invalid.\n */\nexport function saveConfig(config: RouterConfig, scope: \"project\" | \"global\", projectRoot?: string): void {\n\t// Validate before writing to ensure we don't save invalid configs\n\tvalidateConfig(config);\n\n\tif (scope === \"project\" && projectRoot) {\n\t\tvalidateProjectRoot(projectRoot);\n\t}\n\n\tconst path =\n\t\tscope === \"global\" ? GLOBAL_CONFIG : projectRoot ? resolve(projectRoot, PROJECT_CONFIG) : resolve(PROJECT_CONFIG);\n\n\tmkdirSync(dirname(path), { recursive: true });\n\twriteFileSync(path, JSON.stringify(config, null, \"\\t\"), \"utf-8\");\n}\n\n/**\n * Merge project config over global config over defaults.\n * Project overrides global per-role.\n */\nexport function mergeConfig(project: RouterConfig | null, global: RouterConfig | null): RouterConfig {\n\tconst base = { ...DEFAULT_CONFIG };\n\n\tif (global) {\n\t\tfor (const [role, config] of Object.entries(global)) {\n\t\t\tbase[role] = config;\n\t\t}\n\t}\n\n\tif (project) {\n\t\tfor (const [role, config] of Object.entries(project)) {\n\t\t\tbase[role] = config;\n\t\t}\n\t}\n\n\treturn base;\n}\n\nfunction loadFile(path: string): RouterConfig | null {\n\tif (!existsSync(path)) return null;\n\ttry {\n\t\treturn JSON.parse(readFileSync(path, \"utf-8\")) as RouterConfig;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\n/**\n * Validate a ModelRef has non-empty provider and model.\n */\nfunction validateModelRef(ref: ModelRef, role: string, position: string): string[] {\n\tconst errors: string[] = [];\n\tif (!ref.provider || ref.provider.trim() === \"\") {\n\t\terrors.push(`Empty provider string for role ${role} (${position})`);\n\t}\n\tif (!ref.model || ref.model.trim() === \"\") {\n\t\terrors.push(`Empty model string for role ${role} (${position})`);\n\t}\n\treturn errors;\n}\n\n/**\n * Validate a RoleConfig has valid structure.\n */\nfunction validateRoleConfig(roleConfig: RoleConfig, role: string): string[] {\n\tconst errors: string[] = [];\n\n\t// Validate primary\n\terrors.push(...validateModelRef(roleConfig.primary, role, \"primary\"));\n\n\t// Validate fallbacks\n\tfor (let i = 0; i < roleConfig.fallbacks.length; i++) {\n\t\terrors.push(...validateModelRef(roleConfig.fallbacks[i], role, `fallback[${i}]`));\n\t}\n\n\treturn errors;\n}\n\n/**\n * Validate a ModelRef against the @draht/ai registry.\n * Returns errors if provider or model is not found.\n */\nfunction validateModelRefAgainstRegistry(\n\tref: ModelRef,\n\trole: string,\n\tposition: string,\n\tvalidProviders: Set<string>,\n): string[] {\n\tconst errors: string[] = [];\n\n\t// Skip if empty (already caught by structure validation)\n\tif (!ref.provider || !ref.model) {\n\t\treturn errors;\n\t}\n\n\t// Check provider exists\n\tif (!validProviders.has(ref.provider)) {\n\t\terrors.push(`Invalid provider '${ref.provider}' for role ${role} (not in @draht/ai registry)`);\n\t\treturn errors; // Don't check model if provider is invalid\n\t}\n\n\t// Check model exists for this provider\n\tconst models = getModels(ref.provider as KnownProvider);\n\tconst modelExists = models.some((m) => m.id === ref.model);\n\tif (!modelExists) {\n\t\terrors.push(`Invalid model '${ref.model}' for provider '${ref.provider}' in role ${role} (${position})`);\n\t}\n\n\treturn errors;\n}\n\n/**\n * Validate a RoleConfig against the @draht/ai registry.\n */\nfunction validateRoleConfigAgainstRegistry(\n\troleConfig: RoleConfig,\n\trole: string,\n\tvalidProviders: Set<string>,\n): string[] {\n\tconst errors: string[] = [];\n\n\t// Validate primary\n\terrors.push(...validateModelRefAgainstRegistry(roleConfig.primary, role, \"primary\", validProviders));\n\n\t// Validate fallbacks\n\tfor (let i = 0; i < roleConfig.fallbacks.length; i++) {\n\t\terrors.push(...validateModelRefAgainstRegistry(roleConfig.fallbacks[i], role, `fallback[${i}]`, validProviders));\n\t}\n\n\treturn errors;\n}\n\n/**\n * Format a ModelRef as \"provider/model\" for display.\n */\nfunction formatModelRefForValidation(ref: ModelRef): string {\n\treturn `${ref.provider}/${ref.model}`;\n}\n\n/**\n * Detect duplicate models in a fallback chain (primary + fallbacks).\n */\nfunction detectDuplicateModels(roleConfig: RoleConfig, role: string): string[] {\n\tconst errors: string[] = [];\n\tconst allModels: ModelRef[] = [roleConfig.primary, ...roleConfig.fallbacks];\n\tconst seen = new Set<string>();\n\n\tfor (const ref of allModels) {\n\t\tconst key = formatModelRefForValidation(ref);\n\t\tif (seen.has(key)) {\n\t\t\terrors.push(`Duplicate model ${key} in fallback chain for role ${role}`);\n\t\t}\n\t\tseen.add(key);\n\t}\n\n\treturn errors;\n}\n\n/**\n * Validate a router config.\n * Throws ConfigValidationError if any issues are found.\n */\nexport function validateConfig(config: RouterConfig): void {\n\tconst errors: string[] = [];\n\n\t// Check all built-in roles are present\n\tfor (const role of BUILT_IN_ROLES) {\n\t\tif (!(role in config)) {\n\t\t\terrors.push(`Missing required role: ${role}`);\n\t\t}\n\t}\n\n\t// Validate structure of each role config\n\tfor (const [role, roleConfig] of Object.entries(config)) {\n\t\terrors.push(...validateRoleConfig(roleConfig, role));\n\t}\n\n\t// Cache valid providers for registry validation\n\tconst validProviders = new Set(getProviders());\n\n\t// Validate each role config against registry\n\tfor (const [role, roleConfig] of Object.entries(config)) {\n\t\terrors.push(...validateRoleConfigAgainstRegistry(roleConfig, role, validProviders));\n\t}\n\n\t// Detect duplicate models in fallback chains\n\tfor (const [role, roleConfig] of Object.entries(config)) {\n\t\terrors.push(...detectDuplicateModels(roleConfig, role));\n\t}\n\n\tif (errors.length > 0) {\n\t\tthrow new ConfigValidationError(errors);\n\t}\n}\n"]}
|
package/dist/cost.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { CostEntry } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Default rates applied to unknown models (USD per million tokens).
|
|
4
|
+
* Used when a model is not found in COST_PER_MILLION.
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_RATES: {
|
|
7
|
+
readonly input: 3;
|
|
8
|
+
readonly output: 15;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Estimate cost for a request in USD.
|
|
12
|
+
*
|
|
13
|
+
* Uses model-specific rates from COST_PER_MILLION when available.
|
|
14
|
+
* Unknown models use DEFAULT_RATES: { input: $3, output: $15 } per million tokens.
|
|
15
|
+
*
|
|
16
|
+
* Reasoning tokens are billed at the input token rate - this reflects the
|
|
17
|
+
* computational cost of extended reasoning/thinking during model inference.
|
|
18
|
+
*
|
|
19
|
+
* @param provider - Provider name (e.g., "anthropic")
|
|
20
|
+
* @param model - Model name (e.g., "claude-opus-4-6")
|
|
21
|
+
* @param inputTokens - Number of input tokens
|
|
22
|
+
* @param outputTokens - Number of output tokens
|
|
23
|
+
* @param reasoningTokens - Number of reasoning/thinking tokens (billed at input rate)
|
|
24
|
+
* @returns Estimated cost in USD
|
|
25
|
+
*/
|
|
26
|
+
export declare function estimateCost(provider: string, model: string, inputTokens: number, outputTokens: number, reasoningTokens?: number): number;
|
|
27
|
+
/**
|
|
28
|
+
* Append a cost entry to the JSONL log.
|
|
29
|
+
*/
|
|
30
|
+
export declare function logCost(entry: CostEntry, logPath?: string): void;
|
|
31
|
+
/**
|
|
32
|
+
* Read all cost entries from the log.
|
|
33
|
+
*/
|
|
34
|
+
export declare function readCostLog(logPath?: string): CostEntry[];
|
|
35
|
+
/**
|
|
36
|
+
* Get costs filtered by session ID.
|
|
37
|
+
*/
|
|
38
|
+
export declare function getSessionCosts(sessionId: string, logPath?: string): CostEntry[];
|
|
39
|
+
/**
|
|
40
|
+
* Aggregate costs by role.
|
|
41
|
+
*/
|
|
42
|
+
export declare function getRoleCosts(role: string, logPath?: string): {
|
|
43
|
+
totalCost: number;
|
|
44
|
+
totalInputTokens: number;
|
|
45
|
+
totalOutputTokens: number;
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=cost.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost.d.ts","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAI5C;;;GAGG;AACH,eAAO,MAAM,aAAa;aAAK,KAAK,EAAE,CAAC;aAAE,MAAM,EAAE,EAAE;CAAW,CAAC;AAqB/D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAC3B,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,EACpB,eAAe,SAAI,GACjB,MAAM,CAOR;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAIhE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,EAAE,CAOzD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,EAAE,CAEhF;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC3B,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,MAAM,GACd;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,gBAAgB,EAAE,MAAM,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAA;CAAE,CAO5E"}
|
package/dist/cost.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
const DEFAULT_LOG_PATH = ".draht/cost-log.jsonl";
|
|
4
|
+
/**
|
|
5
|
+
* Default rates applied to unknown models (USD per million tokens).
|
|
6
|
+
* Used when a model is not found in COST_PER_MILLION.
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_RATES = { input: 3, output: 15 };
|
|
9
|
+
/**
|
|
10
|
+
* Rough cost estimates per million tokens (input/output) by provider.
|
|
11
|
+
*/
|
|
12
|
+
const COST_PER_MILLION = {
|
|
13
|
+
"anthropic/claude-opus-4-6": { input: 15, output: 75 },
|
|
14
|
+
"anthropic/claude-sonnet-4-6": { input: 3, output: 15 },
|
|
15
|
+
"google/gemini-2.5-pro": { input: 1.25, output: 10 },
|
|
16
|
+
"google/gemini-2.5-flash": { input: 0.15, output: 0.6 },
|
|
17
|
+
"openai/gpt-5.2": { input: 5, output: 15 },
|
|
18
|
+
"deepseek/deepseek-v3": { input: 0.27, output: 1.1 },
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Calculate cost for a number of tokens at a given rate per million.
|
|
22
|
+
*/
|
|
23
|
+
function calculateTokenCost(tokens, ratePerMillion) {
|
|
24
|
+
return (tokens / 1_000_000) * ratePerMillion;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Estimate cost for a request in USD.
|
|
28
|
+
*
|
|
29
|
+
* Uses model-specific rates from COST_PER_MILLION when available.
|
|
30
|
+
* Unknown models use DEFAULT_RATES: { input: $3, output: $15 } per million tokens.
|
|
31
|
+
*
|
|
32
|
+
* Reasoning tokens are billed at the input token rate - this reflects the
|
|
33
|
+
* computational cost of extended reasoning/thinking during model inference.
|
|
34
|
+
*
|
|
35
|
+
* @param provider - Provider name (e.g., "anthropic")
|
|
36
|
+
* @param model - Model name (e.g., "claude-opus-4-6")
|
|
37
|
+
* @param inputTokens - Number of input tokens
|
|
38
|
+
* @param outputTokens - Number of output tokens
|
|
39
|
+
* @param reasoningTokens - Number of reasoning/thinking tokens (billed at input rate)
|
|
40
|
+
* @returns Estimated cost in USD
|
|
41
|
+
*/
|
|
42
|
+
export function estimateCost(provider, model, inputTokens, outputTokens, reasoningTokens = 0) {
|
|
43
|
+
const key = `${provider}/${model}`;
|
|
44
|
+
const rates = COST_PER_MILLION[key] ?? DEFAULT_RATES;
|
|
45
|
+
// Reasoning tokens are billed at input rate
|
|
46
|
+
const inputCost = calculateTokenCost(inputTokens + reasoningTokens, rates.input);
|
|
47
|
+
const outputCost = calculateTokenCost(outputTokens, rates.output);
|
|
48
|
+
return inputCost + outputCost;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Append a cost entry to the JSONL log.
|
|
52
|
+
*/
|
|
53
|
+
export function logCost(entry, logPath) {
|
|
54
|
+
const path = resolve(logPath ?? DEFAULT_LOG_PATH);
|
|
55
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
56
|
+
appendFileSync(path, `${JSON.stringify(entry)}\n`, "utf-8");
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Read all cost entries from the log.
|
|
60
|
+
*/
|
|
61
|
+
export function readCostLog(logPath) {
|
|
62
|
+
const path = resolve(logPath ?? DEFAULT_LOG_PATH);
|
|
63
|
+
if (!existsSync(path))
|
|
64
|
+
return [];
|
|
65
|
+
return readFileSync(path, "utf-8")
|
|
66
|
+
.split("\n")
|
|
67
|
+
.filter(Boolean)
|
|
68
|
+
.map((line) => JSON.parse(line));
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get costs filtered by session ID.
|
|
72
|
+
*/
|
|
73
|
+
export function getSessionCosts(sessionId, logPath) {
|
|
74
|
+
return readCostLog(logPath).filter((e) => e.sessionId === sessionId);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Aggregate costs by role.
|
|
78
|
+
*/
|
|
79
|
+
export function getRoleCosts(role, logPath) {
|
|
80
|
+
const entries = readCostLog(logPath).filter((e) => e.role === role);
|
|
81
|
+
return {
|
|
82
|
+
totalCost: entries.reduce((sum, e) => sum + e.estimatedCostUsd, 0),
|
|
83
|
+
totalInputTokens: entries.reduce((sum, e) => sum + e.inputTokens, 0),
|
|
84
|
+
totalOutputTokens: entries.reduce((sum, e) => sum + e.outputTokens, 0),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=cost.js.map
|
package/dist/cost.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost.js","sourceRoot":"","sources":["../src/cost.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC9E,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAG7C,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;AAEjD;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAW,CAAC;AAE/D;;GAE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AC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;QACpE,iBAAiB,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC;KACtE,CAAC;AACH,CAAC","sourcesContent":["import { appendFileSync, existsSync, mkdirSync, readFileSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\nimport type { CostEntry } from \"./types.js\";\n\nconst DEFAULT_LOG_PATH = \".draht/cost-log.jsonl\";\n\n/**\n * Default rates applied to unknown models (USD per million tokens).\n * Used when a model is not found in COST_PER_MILLION.\n */\nexport const DEFAULT_RATES = { input: 3, output: 15 } as const;\n\n/**\n * Rough cost estimates per million tokens (input/output) by provider.\n */\nconst COST_PER_MILLION: Record<string, { input: number; output: number }> = {\n\t\"anthropic/claude-opus-4-6\": { input: 15, output: 75 },\n\t\"anthropic/claude-sonnet-4-6\": { input: 3, output: 15 },\n\t\"google/gemini-2.5-pro\": { input: 1.25, output: 10 },\n\t\"google/gemini-2.5-flash\": { input: 0.15, output: 0.6 },\n\t\"openai/gpt-5.2\": { input: 5, output: 15 },\n\t\"deepseek/deepseek-v3\": { input: 0.27, output: 1.1 },\n};\n\n/**\n * Calculate cost for a number of tokens at a given rate per million.\n */\nfunction calculateTokenCost(tokens: number, ratePerMillion: number): number {\n\treturn (tokens / 1_000_000) * ratePerMillion;\n}\n\n/**\n * Estimate cost for a request in USD.\n *\n * Uses model-specific rates from COST_PER_MILLION when available.\n * Unknown models use DEFAULT_RATES: { input: $3, output: $15 } per million tokens.\n *\n * Reasoning tokens are billed at the input token rate - this reflects the\n * computational cost of extended reasoning/thinking during model inference.\n *\n * @param provider - Provider name (e.g., \"anthropic\")\n * @param model - Model name (e.g., \"claude-opus-4-6\")\n * @param inputTokens - Number of input tokens\n * @param outputTokens - Number of output tokens\n * @param reasoningTokens - Number of reasoning/thinking tokens (billed at input rate)\n * @returns Estimated cost in USD\n */\nexport function estimateCost(\n\tprovider: string,\n\tmodel: string,\n\tinputTokens: number,\n\toutputTokens: number,\n\treasoningTokens = 0,\n): number {\n\tconst key = `${provider}/${model}`;\n\tconst rates = COST_PER_MILLION[key] ?? DEFAULT_RATES;\n\t// Reasoning tokens are billed at input rate\n\tconst inputCost = calculateTokenCost(inputTokens + reasoningTokens, rates.input);\n\tconst outputCost = calculateTokenCost(outputTokens, rates.output);\n\treturn inputCost + outputCost;\n}\n\n/**\n * Append a cost entry to the JSONL log.\n */\nexport function logCost(entry: CostEntry, logPath?: string): void {\n\tconst path = resolve(logPath ?? DEFAULT_LOG_PATH);\n\tmkdirSync(dirname(path), { recursive: true });\n\tappendFileSync(path, `${JSON.stringify(entry)}\\n`, \"utf-8\");\n}\n\n/**\n * Read all cost entries from the log.\n */\nexport function readCostLog(logPath?: string): CostEntry[] {\n\tconst path = resolve(logPath ?? DEFAULT_LOG_PATH);\n\tif (!existsSync(path)) return [];\n\treturn readFileSync(path, \"utf-8\")\n\t\t.split(\"\\n\")\n\t\t.filter(Boolean)\n\t\t.map((line) => JSON.parse(line) as CostEntry);\n}\n\n/**\n * Get costs filtered by session ID.\n */\nexport function getSessionCosts(sessionId: string, logPath?: string): CostEntry[] {\n\treturn readCostLog(logPath).filter((e) => e.sessionId === sessionId);\n}\n\n/**\n * Aggregate costs by role.\n */\nexport function getRoleCosts(\n\trole: string,\n\tlogPath?: string,\n): { totalCost: number; totalInputTokens: number; totalOutputTokens: number } {\n\tconst entries = readCostLog(logPath).filter((e) => e.role === role);\n\treturn {\n\t\ttotalCost: entries.reduce((sum, e) => sum + e.estimatedCostUsd, 0),\n\t\ttotalInputTokens: entries.reduce((sum, e) => sum + e.inputTokens, 0),\n\t\ttotalOutputTokens: entries.reduce((sum, e) => sum + e.outputTokens, 0),\n\t};\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { isRetryableError, ModelRouter } from "./router.js";
|
|
2
|
+
import type { ModelRef } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Coding agent extension for automatic model selection based on task type.
|
|
5
|
+
* Integrates with @draht/ai's model registry and streaming API.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createRouterExtension(sessionId?: string): {
|
|
8
|
+
name: string;
|
|
9
|
+
description: string;
|
|
10
|
+
/**
|
|
11
|
+
* Select the best model for a given task type or description.
|
|
12
|
+
*/
|
|
13
|
+
selectModel(taskTypeOrDescription: string): ModelRef;
|
|
14
|
+
/**
|
|
15
|
+
* Select model with full fallback chain.
|
|
16
|
+
*/
|
|
17
|
+
selectModelWithFallbacks(taskTypeOrDescription: string): ModelRef[];
|
|
18
|
+
/**
|
|
19
|
+
* Get the router instance for direct streaming usage.
|
|
20
|
+
*/
|
|
21
|
+
getRouter(): ModelRouter;
|
|
22
|
+
/**
|
|
23
|
+
* Check if an error should trigger fallback.
|
|
24
|
+
*/
|
|
25
|
+
shouldFallback: typeof isRetryableError;
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=extension.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extension.d.ts","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAc,MAAM,YAAY,CAAC;AAkDvD;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,CAAC,EAAE,MAAM;IAKtD,IAAI;IACJ,WAAW;IAEX;;OAEG;IACH,WAAW,wBAAwB,MAAM,GAAG,QAAQ;IAKpD;;OAEG;IACH,wBAAwB,wBAAwB,MAAM,GAAG,QAAQ,EAAE;IAKnE;;OAEG;IACH,SAAS,IAAI,WAAW;IAIxB;;OAEG;IACH,cAAc;EAEf"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { loadConfig } from "./config.js";
|
|
2
|
+
import { isRetryableError, ModelRouter } from "./router.js";
|
|
3
|
+
/**
|
|
4
|
+
* Task type to role mapping for automatic model selection.
|
|
5
|
+
*/
|
|
6
|
+
const TASK_TYPE_TO_ROLE = {
|
|
7
|
+
planning: "architect",
|
|
8
|
+
architecture: "architect",
|
|
9
|
+
design: "architect",
|
|
10
|
+
implementation: "implement",
|
|
11
|
+
coding: "implement",
|
|
12
|
+
feature: "implement",
|
|
13
|
+
bugfix: "implement",
|
|
14
|
+
scaffold: "boilerplate",
|
|
15
|
+
boilerplate: "boilerplate",
|
|
16
|
+
crud: "boilerplate",
|
|
17
|
+
template: "boilerplate",
|
|
18
|
+
question: "quick",
|
|
19
|
+
quick: "quick",
|
|
20
|
+
explain: "quick",
|
|
21
|
+
review: "review",
|
|
22
|
+
"pr-review": "review",
|
|
23
|
+
"code-review": "review",
|
|
24
|
+
documentation: "docs",
|
|
25
|
+
readme: "docs",
|
|
26
|
+
comments: "docs",
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Infer role from task description heuristically.
|
|
30
|
+
*/
|
|
31
|
+
function inferRole(taskDescription) {
|
|
32
|
+
const lower = taskDescription.toLowerCase();
|
|
33
|
+
if (lower.includes("architect") || lower.includes("design") || lower.includes("plan"))
|
|
34
|
+
return "architect";
|
|
35
|
+
if (lower.includes("review") || lower.includes("check") || lower.includes("audit"))
|
|
36
|
+
return "review";
|
|
37
|
+
if (lower.includes("document") || lower.includes("readme") || lower.includes("comment"))
|
|
38
|
+
return "docs";
|
|
39
|
+
if (lower.includes("scaffold") ||
|
|
40
|
+
lower.includes("boilerplate") ||
|
|
41
|
+
lower.includes("crud") ||
|
|
42
|
+
lower.includes("template"))
|
|
43
|
+
return "boilerplate";
|
|
44
|
+
if (lower.includes("what") || lower.includes("how") || lower.includes("why") || lower.includes("explain"))
|
|
45
|
+
return "quick";
|
|
46
|
+
return "implement"; // default
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Coding agent extension for automatic model selection based on task type.
|
|
50
|
+
* Integrates with @draht/ai's model registry and streaming API.
|
|
51
|
+
*/
|
|
52
|
+
export function createRouterExtension(sessionId) {
|
|
53
|
+
const config = loadConfig();
|
|
54
|
+
const router = new ModelRouter(config, sessionId);
|
|
55
|
+
return {
|
|
56
|
+
name: "model-router",
|
|
57
|
+
description: "Role-based model routing with automatic fallback",
|
|
58
|
+
/**
|
|
59
|
+
* Select the best model for a given task type or description.
|
|
60
|
+
*/
|
|
61
|
+
selectModel(taskTypeOrDescription) {
|
|
62
|
+
const role = TASK_TYPE_TO_ROLE[taskTypeOrDescription.toLowerCase()] ?? inferRole(taskTypeOrDescription);
|
|
63
|
+
return router.resolve(role);
|
|
64
|
+
},
|
|
65
|
+
/**
|
|
66
|
+
* Select model with full fallback chain.
|
|
67
|
+
*/
|
|
68
|
+
selectModelWithFallbacks(taskTypeOrDescription) {
|
|
69
|
+
const role = TASK_TYPE_TO_ROLE[taskTypeOrDescription.toLowerCase()] ?? inferRole(taskTypeOrDescription);
|
|
70
|
+
return router.resolveWithFallbacks(role);
|
|
71
|
+
},
|
|
72
|
+
/**
|
|
73
|
+
* Get the router instance for direct streaming usage.
|
|
74
|
+
*/
|
|
75
|
+
getRouter() {
|
|
76
|
+
return router;
|
|
77
|
+
},
|
|
78
|
+
/**
|
|
79
|
+
* Check if an error should trigger fallback.
|
|
80
|
+
*/
|
|
81
|
+
shouldFallback: isRetryableError,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=extension.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG5D;;GAEG;AACH,MAAM,iBAAiB,GAA+B;IACrD,QAAQ,EAAE,WAAW;IACrB,YAAY,EAAE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,OAAO,aAAa,CAAC;IACtB,IAAI,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,SAAS,CAAC;QACxG,OAAO,OAAO,CAAC;IAEhB,OAAO,WAAW,CAAC,CAAC,UAAU;AAC/B,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,SAAkB;IACvD,MAAM,MAAM,GAAG,UAAU,EAAE,CAAC;IAC5B,MAAM,MAAM,GAAG,IAAI,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAElD,OAAO;QACN,IAAI,EAAE,cAAc;QACpB,WAAW,EAAE,kDAAkD;QAE/D;;WAEG;QACH,WAAW,CAAC,qBAA6B;YACxC,MAAM,IAAI,GAAG,iBAAiB,CAAC,qBAAqB,CAAC,WAAW,EAAE,CAAC,IAAI,SAAS,CAAC,qBAAqB,CAAC,CAAC;YACxG,OAAO,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC7B,CAAC;QAED;;WAEG;QACH,wBAAwB,CAAC,qBAA6B;YACrD,MAAM,IAAI,GAAG,iBAAiB,CAAC,qBAAqB,CAAC,WAAW,EAAE,CAAC,IAAI,SAAS,CAAC,qBAAqB,CAAC,CAAC;YACxG,OAAO,MAAM,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;QAC1C,CAAC;QAED;;WAEG;QACH,SAAS;YACR,OAAO,MAAM,CAAC;QACf,CAAC;QAED;;WAEG;QACH,cAAc,EAAE,gBAAgB;KAChC,CAAC;AACH,CAAC","sourcesContent":["import { loadConfig } from \"./config.js\";\nimport { isRetryableError, ModelRouter } from \"./router.js\";\nimport type { ModelRef, RouterRole } from \"./types.js\";\n\n/**\n * Task type to role mapping for automatic model selection.\n */\nconst TASK_TYPE_TO_ROLE: Record<string, RouterRole> = {\n\tplanning: \"architect\",\n\tarchitecture: \"architect\",\n\tdesign: \"architect\",\n\timplementation: \"implement\",\n\tcoding: \"implement\",\n\tfeature: \"implement\",\n\tbugfix: \"implement\",\n\tscaffold: \"boilerplate\",\n\tboilerplate: \"boilerplate\",\n\tcrud: \"boilerplate\",\n\ttemplate: \"boilerplate\",\n\tquestion: \"quick\",\n\tquick: \"quick\",\n\texplain: \"quick\",\n\treview: \"review\",\n\t\"pr-review\": \"review\",\n\t\"code-review\": \"review\",\n\tdocumentation: \"docs\",\n\treadme: \"docs\",\n\tcomments: \"docs\",\n};\n\n/**\n * Infer role from task description heuristically.\n */\nfunction inferRole(taskDescription: string): RouterRole {\n\tconst lower = taskDescription.toLowerCase();\n\n\tif (lower.includes(\"architect\") || lower.includes(\"design\") || lower.includes(\"plan\")) return \"architect\";\n\tif (lower.includes(\"review\") || lower.includes(\"check\") || lower.includes(\"audit\")) return \"review\";\n\tif (lower.includes(\"document\") || lower.includes(\"readme\") || lower.includes(\"comment\")) return \"docs\";\n\tif (\n\t\tlower.includes(\"scaffold\") ||\n\t\tlower.includes(\"boilerplate\") ||\n\t\tlower.includes(\"crud\") ||\n\t\tlower.includes(\"template\")\n\t)\n\t\treturn \"boilerplate\";\n\tif (lower.includes(\"what\") || lower.includes(\"how\") || lower.includes(\"why\") || lower.includes(\"explain\"))\n\t\treturn \"quick\";\n\n\treturn \"implement\"; // default\n}\n\n/**\n * Coding agent extension for automatic model selection based on task type.\n * Integrates with @draht/ai's model registry and streaming API.\n */\nexport function createRouterExtension(sessionId?: string) {\n\tconst config = loadConfig();\n\tconst router = new ModelRouter(config, sessionId);\n\n\treturn {\n\t\tname: \"model-router\",\n\t\tdescription: \"Role-based model routing with automatic fallback\",\n\n\t\t/**\n\t\t * Select the best model for a given task type or description.\n\t\t */\n\t\tselectModel(taskTypeOrDescription: string): ModelRef {\n\t\t\tconst role = TASK_TYPE_TO_ROLE[taskTypeOrDescription.toLowerCase()] ?? inferRole(taskTypeOrDescription);\n\t\t\treturn router.resolve(role);\n\t\t},\n\n\t\t/**\n\t\t * Select model with full fallback chain.\n\t\t */\n\t\tselectModelWithFallbacks(taskTypeOrDescription: string): ModelRef[] {\n\t\t\tconst role = TASK_TYPE_TO_ROLE[taskTypeOrDescription.toLowerCase()] ?? inferRole(taskTypeOrDescription);\n\t\t\treturn router.resolveWithFallbacks(role);\n\t\t},\n\n\t\t/**\n\t\t * Get the router instance for direct streaming usage.\n\t\t */\n\t\tgetRouter(): ModelRouter {\n\t\t\treturn router;\n\t\t},\n\n\t\t/**\n\t\t * Check if an error should trigger fallback.\n\t\t */\n\t\tshouldFallback: isRetryableError,\n\t};\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { formatModelRef, parseModelRef, routerCost, routerSet, routerShow, routerTest, } from "./cli.js";
|
|
2
|
+
export { BUILT_IN_ROLES, ConfigValidationError, loadConfig, mergeConfig, saveConfig, validateConfig, } from "./config.js";
|
|
3
|
+
export { estimateCost, getRoleCosts, getSessionCosts, logCost, readCostLog, } from "./cost.js";
|
|
4
|
+
export { createRouterExtension } from "./extension.js";
|
|
5
|
+
export { ModelRouter } from "./router.js";
|
|
6
|
+
export type { CostEntry, ModelRef, RoleConfig, RouterConfig, RouterRole, } from "./types.js";
|
|
7
|
+
export { DEFAULT_CONFIG } from "./types.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EACd,aAAa,EACb,UAAU,EACV,SAAS,EACT,UAAU,EACV,UAAU,GACV,MAAM,UAAU,CAAC;AAClB,OAAO,EACN,cAAc,EACd,qBAAqB,EACrB,UAAU,EACV,WAAW,EACX,UAAU,EACV,cAAc,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,OAAO,EACP,WAAW,GACX,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,YAAY,EACX,SAAS,EACT,QAAQ,EACR,UAAU,EACV,YAAY,EACZ,UAAU,GACV,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { formatModelRef, parseModelRef, routerCost, routerSet, routerShow, routerTest, } from "./cli.js";
|
|
2
|
+
export { BUILT_IN_ROLES, ConfigValidationError, loadConfig, mergeConfig, saveConfig, validateConfig, } from "./config.js";
|
|
3
|
+
export { estimateCost, getRoleCosts, getSessionCosts, logCost, readCostLog, } from "./cost.js";
|
|
4
|
+
export { createRouterExtension } from "./extension.js";
|
|
5
|
+
export { ModelRouter } from "./router.js";
|
|
6
|
+
export { DEFAULT_CONFIG } from "./types.js";
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EACd,aAAa,EACb,UAAU,EACV,SAAS,EACT,UAAU,EACV,UAAU,GACV,MAAM,UAAU,CAAC;AAClB,OAAO,EACN,cAAc,EACd,qBAAqB,EACrB,UAAU,EACV,WAAW,EACX,UAAU,EACV,cAAc,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,OAAO,EACP,WAAW,GACX,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAQ1C,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export {\n\tformatModelRef,\n\tparseModelRef,\n\trouterCost,\n\trouterSet,\n\trouterShow,\n\trouterTest,\n} from \"./cli.js\";\nexport {\n\tBUILT_IN_ROLES,\n\tConfigValidationError,\n\tloadConfig,\n\tmergeConfig,\n\tsaveConfig,\n\tvalidateConfig,\n} from \"./config.js\";\nexport {\n\testimateCost,\n\tgetRoleCosts,\n\tgetSessionCosts,\n\tlogCost,\n\treadCostLog,\n} from \"./cost.js\";\nexport { createRouterExtension } from \"./extension.js\";\nexport { ModelRouter } from \"./router.js\";\nexport type {\n\tCostEntry,\n\tModelRef,\n\tRoleConfig,\n\tRouterConfig,\n\tRouterRole,\n} from \"./types.js\";\nexport { DEFAULT_CONFIG } from \"./types.js\";\n"]}
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type Api, type Context, type Model, type SimpleStreamOptions, type StreamOptions } from "@draht/ai/compat";
|
|
2
|
+
import type { ModelRef, RouterConfig, RouterRole } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Model router with role-based resolution, automatic fallback, and @draht/ai integration.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ModelRouter {
|
|
7
|
+
private config;
|
|
8
|
+
private sessionId;
|
|
9
|
+
constructor(config?: RouterConfig, sessionId?: string);
|
|
10
|
+
/**
|
|
11
|
+
* Resolve the primary model for a role.
|
|
12
|
+
*/
|
|
13
|
+
resolve(role: RouterRole): ModelRef;
|
|
14
|
+
/**
|
|
15
|
+
* Resolve all models for a role: [primary, ...fallbacks].
|
|
16
|
+
*/
|
|
17
|
+
resolveWithFallbacks(role: RouterRole): ModelRef[];
|
|
18
|
+
/**
|
|
19
|
+
* Resolve a ModelRef to an actual @draht/ai Model object.
|
|
20
|
+
* Looks up in the model registry by provider + model ID.
|
|
21
|
+
*/
|
|
22
|
+
resolveModel(ref: ModelRef): Model<Api> | null;
|
|
23
|
+
/**
|
|
24
|
+
* Stream a completion using the role's model chain.
|
|
25
|
+
* Automatically falls back on retryable errors (429, 5xx, timeout).
|
|
26
|
+
*
|
|
27
|
+
* Mid-stream failures are detected via error events in the stream.
|
|
28
|
+
* Events are buffered and only yielded after successful completion,
|
|
29
|
+
* preventing partial responses from failed providers from leaking through.
|
|
30
|
+
*/
|
|
31
|
+
stream(role: RouterRole, context: Context, options?: StreamOptions): AsyncGenerator<import("@draht/ai").AssistantMessageEvent>;
|
|
32
|
+
/**
|
|
33
|
+
* Stream a simple completion (no tool use) with fallback.
|
|
34
|
+
*
|
|
35
|
+
* Mid-stream failures are detected via error events in the stream.
|
|
36
|
+
* Events are buffered and only yielded after successful completion,
|
|
37
|
+
* preventing partial responses from failed providers from leaking through.
|
|
38
|
+
*/
|
|
39
|
+
streamSimple(role: RouterRole, context: Context, options?: SimpleStreamOptions): AsyncGenerator<import("@draht/ai").AssistantMessageEvent>;
|
|
40
|
+
/**
|
|
41
|
+
* Internal helper for streaming with fallback support.
|
|
42
|
+
* Buffers events and checks for error events to trigger fallback.
|
|
43
|
+
*/
|
|
44
|
+
private streamWithFallback;
|
|
45
|
+
/**
|
|
46
|
+
* Execute a function with automatic fallback on retryable errors.
|
|
47
|
+
* For non-streaming use cases.
|
|
48
|
+
*/
|
|
49
|
+
route<T>(role: RouterRole, fn: (model: ModelRef) => Promise<T>): Promise<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Log a cost entry for tracking.
|
|
52
|
+
*/
|
|
53
|
+
trackCost(role: string, ref: ModelRef, inputTokens: number, outputTokens: number, reasoningTokens?: number): void;
|
|
54
|
+
/**
|
|
55
|
+
* Get current config.
|
|
56
|
+
*/
|
|
57
|
+
getConfig(): RouterConfig;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Check if an error is retryable (rate limit, timeout, server error).
|
|
61
|
+
*/
|
|
62
|
+
export declare function isRetryableError(error: unknown): boolean;
|
|
63
|
+
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.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AAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,eAAe,SAAI,GAAG,IAAI,CAc3G;IAED;;OAEG;IACH,SAAS,IAAI,YAAY,CAExB;CACD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAcxD"}
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { getApiProvider, getModel, } from "@draht/ai/compat";
|
|
2
|
+
import { loadConfig } from "./config.js";
|
|
3
|
+
import { estimateCost, logCost } from "./cost.js";
|
|
4
|
+
/**
|
|
5
|
+
* Model router with role-based resolution, automatic fallback, and @draht/ai integration.
|
|
6
|
+
*/
|
|
7
|
+
export class ModelRouter {
|
|
8
|
+
constructor(config, sessionId) {
|
|
9
|
+
this.config = config ?? loadConfig();
|
|
10
|
+
this.sessionId = sessionId ?? `session-${Date.now()}`;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Resolve the primary model for a role.
|
|
14
|
+
*/
|
|
15
|
+
resolve(role) {
|
|
16
|
+
const roleConfig = this.config[role];
|
|
17
|
+
if (!roleConfig) {
|
|
18
|
+
throw new Error(`Unknown role: ${role}. Available: ${Object.keys(this.config).join(", ")}`);
|
|
19
|
+
}
|
|
20
|
+
return roleConfig.primary;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Resolve all models for a role: [primary, ...fallbacks].
|
|
24
|
+
*/
|
|
25
|
+
resolveWithFallbacks(role) {
|
|
26
|
+
const roleConfig = this.config[role];
|
|
27
|
+
if (!roleConfig) {
|
|
28
|
+
throw new Error(`Unknown role: ${role}`);
|
|
29
|
+
}
|
|
30
|
+
return [roleConfig.primary, ...roleConfig.fallbacks];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Resolve a ModelRef to an actual @draht/ai Model object.
|
|
34
|
+
* Looks up in the model registry by provider + model ID.
|
|
35
|
+
*/
|
|
36
|
+
resolveModel(ref) {
|
|
37
|
+
try {
|
|
38
|
+
return getModel(ref.provider, ref.model) ?? null;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Stream a completion using the role's model chain.
|
|
46
|
+
* Automatically falls back on retryable errors (429, 5xx, timeout).
|
|
47
|
+
*
|
|
48
|
+
* Mid-stream failures are detected via error events in the stream.
|
|
49
|
+
* Events are buffered and only yielded after successful completion,
|
|
50
|
+
* preventing partial responses from failed providers from leaking through.
|
|
51
|
+
*/
|
|
52
|
+
async *stream(role, context, options) {
|
|
53
|
+
yield* this.streamWithFallback(role, context, options, (provider, model, opts) => provider.stream(model, context, opts));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Stream a simple completion (no tool use) with fallback.
|
|
57
|
+
*
|
|
58
|
+
* Mid-stream failures are detected via error events in the stream.
|
|
59
|
+
* Events are buffered and only yielded after successful completion,
|
|
60
|
+
* preventing partial responses from failed providers from leaking through.
|
|
61
|
+
*/
|
|
62
|
+
async *streamSimple(role, context, options) {
|
|
63
|
+
yield* this.streamWithFallback(role, context, options, (provider, model, opts) => provider.streamSimple(model, context, opts));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Internal helper for streaming with fallback support.
|
|
67
|
+
* Buffers events and checks for error events to trigger fallback.
|
|
68
|
+
*/
|
|
69
|
+
async *streamWithFallback(role, _context, options, streamFn) {
|
|
70
|
+
const models = this.resolveWithFallbacks(role);
|
|
71
|
+
let lastError;
|
|
72
|
+
for (const ref of models) {
|
|
73
|
+
const model = this.resolveModel(ref);
|
|
74
|
+
if (!model)
|
|
75
|
+
continue;
|
|
76
|
+
const provider = getApiProvider(model.api);
|
|
77
|
+
if (!provider)
|
|
78
|
+
continue;
|
|
79
|
+
// Buffer events to prevent partial responses from leaking on failure
|
|
80
|
+
const bufferedEvents = [];
|
|
81
|
+
let streamError = null;
|
|
82
|
+
try {
|
|
83
|
+
const stream = streamFn(provider, model, options);
|
|
84
|
+
for await (const event of stream) {
|
|
85
|
+
// Check for error events in the stream (mid-stream failures)
|
|
86
|
+
if (event.type === "error") {
|
|
87
|
+
const errorMessage = event.error.errorMessage ?? "Stream error";
|
|
88
|
+
streamError = new Error(errorMessage);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
bufferedEvents.push(event);
|
|
92
|
+
// If we got a done event, stream completed successfully
|
|
93
|
+
if (event.type === "done") {
|
|
94
|
+
// Yield all buffered events
|
|
95
|
+
for (const bufferedEvent of bufferedEvents) {
|
|
96
|
+
yield bufferedEvent;
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// If we exited the loop without a done event and no error, something is wrong
|
|
102
|
+
if (!streamError) {
|
|
103
|
+
streamError = new Error("Stream ended without completion");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
streamError = error instanceof Error ? error : new Error(String(error));
|
|
108
|
+
}
|
|
109
|
+
// Handle stream error - check if retryable for fallback
|
|
110
|
+
if (streamError) {
|
|
111
|
+
lastError = streamError;
|
|
112
|
+
if (!isRetryableError(streamError)) {
|
|
113
|
+
throw streamError;
|
|
114
|
+
}
|
|
115
|
+
// Continue to next fallback, discarding buffered events
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
throw lastError ?? new Error(`No available model for role: ${role}`);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Execute a function with automatic fallback on retryable errors.
|
|
122
|
+
* For non-streaming use cases.
|
|
123
|
+
*/
|
|
124
|
+
async route(role, fn) {
|
|
125
|
+
const models = this.resolveWithFallbacks(role);
|
|
126
|
+
let lastError;
|
|
127
|
+
for (const model of models) {
|
|
128
|
+
try {
|
|
129
|
+
const result = await fn(model);
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
lastError = error;
|
|
134
|
+
if (!isRetryableError(error)) {
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
throw lastError ?? new Error(`All models failed for role: ${role}`);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Log a cost entry for tracking.
|
|
143
|
+
*/
|
|
144
|
+
trackCost(role, ref, inputTokens, outputTokens, reasoningTokens = 0) {
|
|
145
|
+
const cost = estimateCost(ref.provider, ref.model, inputTokens, outputTokens, reasoningTokens);
|
|
146
|
+
const entry = {
|
|
147
|
+
timestamp: new Date().toISOString(),
|
|
148
|
+
role,
|
|
149
|
+
provider: ref.provider,
|
|
150
|
+
model: ref.model,
|
|
151
|
+
inputTokens,
|
|
152
|
+
outputTokens,
|
|
153
|
+
...(reasoningTokens > 0 && { reasoningTokens }),
|
|
154
|
+
estimatedCostUsd: cost,
|
|
155
|
+
sessionId: this.sessionId,
|
|
156
|
+
};
|
|
157
|
+
logCost(entry);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get current config.
|
|
161
|
+
*/
|
|
162
|
+
getConfig() {
|
|
163
|
+
return this.config;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Check if an error is retryable (rate limit, timeout, server error).
|
|
168
|
+
*/
|
|
169
|
+
export function isRetryableError(error) {
|
|
170
|
+
if (error instanceof Error) {
|
|
171
|
+
const msg = error.message.toLowerCase();
|
|
172
|
+
if (msg.includes("rate limit") || msg.includes("429"))
|
|
173
|
+
return true;
|
|
174
|
+
if (msg.includes("timeout") || msg.includes("timed out") || msg.includes("etimedout"))
|
|
175
|
+
return true;
|
|
176
|
+
if (msg.includes("500") || msg.includes("502") || msg.includes("503") || msg.includes("504"))
|
|
177
|
+
return true;
|
|
178
|
+
if (msg.includes("server error") || msg.includes("internal error"))
|
|
179
|
+
return true;
|
|
180
|
+
if (msg.includes("overloaded") || msg.includes("capacity"))
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
if (error && typeof error === "object" && "status" in error) {
|
|
184
|
+
const status = error.status;
|
|
185
|
+
if (typeof status === "number" && (status === 429 || (status >= 500 && status < 600)))
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
//# sourceMappingURL=router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,cAAc,EACd,QAAQ,GAIR,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGlD;;GAEG;AACH,MAAM,OAAO,WAAW;IAIvB,YAAY,MAAqB,EAAE,SAAkB;QACpD,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,UAAU,EAAE,CAAC;QACrC,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,WAAW,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;IACvD,CAAC;IAED;;OAEG;IACH,OAAO,CAAC,IAAgB;QACvB,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,UAAU,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,gBAAgB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC7F,CAAC;QACD,OAAO,UAAU,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,oBAAoB,CAAC,IAAgB;QACpC,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,UAAU,EAAE,CA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AU,CAAC;YAAE,OAAO,IAAI,CAAC;IACzE,CAAC;IACD,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,QAAQ,IAAI,KAAK,EAAE,CAAC;QAC7D,MAAM,MAAM,GAAI,KAAiC,CAAC,MAAM,CAAC;QACzD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,IAAI,GAAG,IAAI,MAAM,GAAG,GAAG,CAAC,CAAC;YAAE,OAAO,IAAI,CAAC;IACpG,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC","sourcesContent":["import {\n\ttype Api,\n\ttype Context,\n\tgetApiProvider,\n\tgetModel,\n\ttype Model,\n\ttype SimpleStreamOptions,\n\ttype StreamOptions,\n} from \"@draht/ai/compat\";\nimport { loadConfig } from \"./config.js\";\nimport { estimateCost, logCost } from \"./cost.js\";\nimport type { CostEntry, ModelRef, RouterConfig, RouterRole } from \"./types.js\";\n\n/**\n * Model router with role-based resolution, automatic fallback, and @draht/ai integration.\n */\nexport class ModelRouter {\n\tprivate config: RouterConfig;\n\tprivate sessionId: string;\n\n\tconstructor(config?: RouterConfig, sessionId?: string) {\n\t\tthis.config = config ?? loadConfig();\n\t\tthis.sessionId = sessionId ?? `session-${Date.now()}`;\n\t}\n\n\t/**\n\t * Resolve the primary model for a role.\n\t */\n\tresolve(role: RouterRole): ModelRef {\n\t\tconst roleConfig = this.config[role];\n\t\tif (!roleConfig) {\n\t\t\tthrow new Error(`Unknown role: ${role}. Available: ${Object.keys(this.config).join(\", \")}`);\n\t\t}\n\t\treturn roleConfig.primary;\n\t}\n\n\t/**\n\t * Resolve all models for a role: [primary, ...fallbacks].\n\t */\n\tresolveWithFallbacks(role: RouterRole): ModelRef[] {\n\t\tconst roleConfig = this.config[role];\n\t\tif (!roleConfig) {\n\t\t\tthrow new Error(`Unknown role: ${role}`);\n\t\t}\n\t\treturn [roleConfig.primary, ...roleConfig.fallbacks];\n\t}\n\n\t/**\n\t * Resolve a ModelRef to an actual @draht/ai Model object.\n\t * Looks up in the model registry by provider + model ID.\n\t */\n\tresolveModel(ref: ModelRef): Model<Api> | null {\n\t\ttry {\n\t\t\treturn getModel(ref.provider as any, ref.model as any) ?? null;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Stream a completion using the role's model chain.\n\t * Automatically falls back on retryable errors (429, 5xx, timeout).\n\t *\n\t * Mid-stream failures are detected via error events in the stream.\n\t * Events are buffered and only yielded after successful completion,\n\t * preventing partial responses from failed providers from leaking through.\n\t */\n\tasync *stream(\n\t\trole: RouterRole,\n\t\tcontext: Context,\n\t\toptions?: StreamOptions,\n\t): AsyncGenerator<import(\"@draht/ai\").AssistantMessageEvent> {\n\t\tyield* this.streamWithFallback(role, context, options, (provider, model, opts) =>\n\t\t\tprovider.stream(model, context, opts),\n\t\t);\n\t}\n\n\t/**\n\t * Stream a simple completion (no tool use) with fallback.\n\t *\n\t * Mid-stream failures are detected via error events in the stream.\n\t * Events are buffered and only yielded after successful completion,\n\t * preventing partial responses from failed providers from leaking through.\n\t */\n\tasync *streamSimple(\n\t\trole: RouterRole,\n\t\tcontext: Context,\n\t\toptions?: SimpleStreamOptions,\n\t): AsyncGenerator<import(\"@draht/ai\").AssistantMessageEvent> {\n\t\tyield* this.streamWithFallback(role, context, options, (provider, model, opts) =>\n\t\t\tprovider.streamSimple(model, context, opts as SimpleStreamOptions),\n\t\t);\n\t}\n\n\t/**\n\t * Internal helper for streaming with fallback support.\n\t * Buffers events and checks for error events to trigger fallback.\n\t */\n\tprivate async *streamWithFallback<T extends StreamOptions>(\n\t\trole: RouterRole,\n\t\t_context: Context,\n\t\toptions: T | undefined,\n\t\tstreamFn: (\n\t\t\tprovider: NonNullable<ReturnType<typeof getApiProvider>>,\n\t\t\tmodel: Model<Api>,\n\t\t\toptions: T | undefined,\n\t\t) => AsyncIterable<import(\"@draht/ai\").AssistantMessageEvent>,\n\t): AsyncGenerator<import(\"@draht/ai\").AssistantMessageEvent> {\n\t\tconst models = this.resolveWithFallbacks(role);\n\t\tlet lastError: unknown;\n\n\t\tfor (const ref of models) {\n\t\t\tconst model = this.resolveModel(ref);\n\t\t\tif (!model) continue;\n\n\t\t\tconst provider = getApiProvider(model.api);\n\t\t\tif (!provider) continue;\n\n\t\t\t// Buffer events to prevent partial responses from leaking on failure\n\t\t\tconst bufferedEvents: import(\"@draht/ai\").AssistantMessageEvent[] = [];\n\t\t\tlet streamError: Error | null = null;\n\n\t\t\ttry {\n\t\t\t\tconst stream = streamFn(provider, model, options);\n\n\t\t\t\tfor await (const event of stream) {\n\t\t\t\t\t// Check for error events in the stream (mid-stream failures)\n\t\t\t\t\tif (event.type === \"error\") {\n\t\t\t\t\t\tconst errorMessage = event.error.errorMessage ?? \"Stream error\";\n\t\t\t\t\t\tstreamError = new Error(errorMessage);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tbufferedEvents.push(event);\n\n\t\t\t\t\t// If we got a done event, stream completed successfully\n\t\t\t\t\tif (event.type === \"done\") {\n\t\t\t\t\t\t// Yield all buffered events\n\t\t\t\t\t\tfor (const bufferedEvent of bufferedEvents) {\n\t\t\t\t\t\t\tyield bufferedEvent;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// If we exited the loop without a done event and no error, something is wrong\n\t\t\t\tif (!streamError) {\n\t\t\t\t\tstreamError = new Error(\"Stream ended without completion\");\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tstreamError = error instanceof Error ? error : new Error(String(error));\n\t\t\t}\n\n\t\t\t// Handle stream error - check if retryable for fallback\n\t\t\tif (streamError) {\n\t\t\t\tlastError = streamError;\n\t\t\t\tif (!isRetryableError(streamError)) {\n\t\t\t\t\tthrow streamError;\n\t\t\t\t}\n\t\t\t\t// Continue to next fallback, discarding buffered events\n\t\t\t}\n\t\t}\n\n\t\tthrow lastError ?? new Error(`No available model for role: ${role}`);\n\t}\n\n\t/**\n\t * Execute a function with automatic fallback on retryable errors.\n\t * For non-streaming use cases.\n\t */\n\tasync route<T>(role: RouterRole, fn: (model: ModelRef) => Promise<T>): Promise<T> {\n\t\tconst models = this.resolveWithFallbacks(role);\n\t\tlet lastError: unknown;\n\n\t\tfor (const model of models) {\n\t\t\ttry {\n\t\t\t\tconst result = await fn(model);\n\t\t\t\treturn result;\n\t\t\t} catch (error) {\n\t\t\t\tlastError = error;\n\t\t\t\tif (!isRetryableError(error)) {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthrow lastError ?? new Error(`All models failed for role: ${role}`);\n\t}\n\n\t/**\n\t * Log a cost entry for tracking.\n\t */\n\ttrackCost(role: string, ref: ModelRef, inputTokens: number, outputTokens: number, reasoningTokens = 0): void {\n\t\tconst cost = estimateCost(ref.provider, ref.model, inputTokens, outputTokens, reasoningTokens);\n\t\tconst entry: CostEntry = {\n\t\t\ttimestamp: new Date().toISOString(),\n\t\t\trole,\n\t\t\tprovider: ref.provider,\n\t\t\tmodel: ref.model,\n\t\t\tinputTokens,\n\t\t\toutputTokens,\n\t\t\t...(reasoningTokens > 0 && { reasoningTokens }),\n\t\t\testimatedCostUsd: cost,\n\t\t\tsessionId: this.sessionId,\n\t\t};\n\t\tlogCost(entry);\n\t}\n\n\t/**\n\t * Get current config.\n\t */\n\tgetConfig(): RouterConfig {\n\t\treturn this.config;\n\t}\n}\n\n/**\n * Check if an error is retryable (rate limit, timeout, server error).\n */\nexport function isRetryableError(error: unknown): boolean {\n\tif (error instanceof Error) {\n\t\tconst msg = error.message.toLowerCase();\n\t\tif (msg.includes(\"rate limit\") || msg.includes(\"429\")) return true;\n\t\tif (msg.includes(\"timeout\") || msg.includes(\"timed out\") || msg.includes(\"etimedout\")) return true;\n\t\tif (msg.includes(\"500\") || msg.includes(\"502\") || msg.includes(\"503\") || msg.includes(\"504\")) return true;\n\t\tif (msg.includes(\"server error\") || msg.includes(\"internal error\")) return true;\n\t\tif (msg.includes(\"overloaded\") || msg.includes(\"capacity\")) return true;\n\t}\n\tif (error && typeof error === \"object\" && \"status\" in error) {\n\t\tconst status = (error as Record<string, unknown>).status;\n\t\tif (typeof status === \"number\" && (status === 429 || (status >= 500 && status < 600))) return true;\n\t}\n\treturn false;\n}\n"]}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in roles that must be present in every config.
|
|
3
|
+
*/
|
|
4
|
+
export declare const BUILT_IN_ROLES: readonly ["architect", "implement", "boilerplate", "quick", "review", "docs"];
|
|
5
|
+
/**
|
|
6
|
+
* Built-in router roles. Extensible via string type.
|
|
7
|
+
*/
|
|
8
|
+
export type RouterRole = (typeof BUILT_IN_ROLES)[number] | (string & {});
|
|
9
|
+
/**
|
|
10
|
+
* Reference to a specific provider/model combination.
|
|
11
|
+
*/
|
|
12
|
+
export interface ModelRef {
|
|
13
|
+
provider: string;
|
|
14
|
+
model: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Configuration for a single role: primary model + ordered fallback chain.
|
|
18
|
+
*/
|
|
19
|
+
export interface RoleConfig {
|
|
20
|
+
primary: ModelRef;
|
|
21
|
+
fallbacks: ModelRef[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Full router configuration mapping roles to model configs.
|
|
25
|
+
*/
|
|
26
|
+
export type RouterConfig = Record<string, RoleConfig>;
|
|
27
|
+
/**
|
|
28
|
+
* Cost tracking entry written to .draht/cost-log.jsonl.
|
|
29
|
+
*/
|
|
30
|
+
export interface CostEntry {
|
|
31
|
+
timestamp: string;
|
|
32
|
+
role: string;
|
|
33
|
+
provider: string;
|
|
34
|
+
model: string;
|
|
35
|
+
inputTokens: number;
|
|
36
|
+
outputTokens: number;
|
|
37
|
+
/** Reasoning/thinking tokens, billed at input token rate. */
|
|
38
|
+
reasoningTokens?: number;
|
|
39
|
+
estimatedCostUsd: number;
|
|
40
|
+
sessionId: string;
|
|
41
|
+
/** Trajectory id shared by every call (root + sub) within a single RLM session. */
|
|
42
|
+
trajectoryId?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Default role→model configuration.
|
|
46
|
+
*/
|
|
47
|
+
export declare const DEFAULT_CONFIG: RouterConfig;
|
|
48
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,cAAc,YAAI,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAU,CAAC;AAE5G;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAEzE;;GAEG;AACH,MAAM,WAAW,QAAQ;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IAC1B,OAAO,EAAE,QAAQ,CAAC;IAClB,SAAS,EAAE,QAAQ,EAAE,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,SAAS;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,6DAA6D;IAC7D,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gBAAgB,EAAE,MAAM,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,mFAAmF;IACnF,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,YAoC5B,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in roles that must be present in every config.
|
|
3
|
+
*/
|
|
4
|
+
export const BUILT_IN_ROLES = ["architect", "implement", "boilerplate", "quick", "review", "docs"];
|
|
5
|
+
/**
|
|
6
|
+
* Default role→model configuration.
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_CONFIG = {
|
|
9
|
+
architect: {
|
|
10
|
+
primary: { provider: "anthropic", model: "claude-opus-4-6" },
|
|
11
|
+
fallbacks: [{ provider: "google", model: "gemini-2.5-pro" }],
|
|
12
|
+
},
|
|
13
|
+
implement: {
|
|
14
|
+
primary: { provider: "anthropic", model: "claude-sonnet-4-6" },
|
|
15
|
+
fallbacks: [
|
|
16
|
+
{ provider: "openai", model: "gpt-5.2" },
|
|
17
|
+
{ provider: "openrouter", model: "deepseek/deepseek-v3.2" },
|
|
18
|
+
],
|
|
19
|
+
},
|
|
20
|
+
boilerplate: {
|
|
21
|
+
primary: { provider: "openrouter", model: "deepseek/deepseek-v3.2" },
|
|
22
|
+
fallbacks: [{ provider: "google", model: "gemini-2.5-flash" }],
|
|
23
|
+
},
|
|
24
|
+
quick: {
|
|
25
|
+
primary: { provider: "google", model: "gemini-2.5-flash" },
|
|
26
|
+
fallbacks: [{ provider: "openrouter", model: "deepseek/deepseek-v3.2" }],
|
|
27
|
+
},
|
|
28
|
+
review: {
|
|
29
|
+
primary: { provider: "anthropic", model: "claude-sonnet-4-6" },
|
|
30
|
+
fallbacks: [],
|
|
31
|
+
},
|
|
32
|
+
docs: {
|
|
33
|
+
primary: { provider: "openai", model: "gpt-5.2" },
|
|
34
|
+
fallbacks: [{ provider: "anthropic", model: "claude-sonnet-4-6" }],
|
|
35
|
+
},
|
|
36
|
+
"rlm-root": {
|
|
37
|
+
primary: { provider: "anthropic", model: "claude-opus-4-6" },
|
|
38
|
+
fallbacks: [{ provider: "google", model: "gemini-2.5-pro" }],
|
|
39
|
+
},
|
|
40
|
+
"rlm-sub": {
|
|
41
|
+
primary: { provider: "google", model: "gemini-2.5-flash" },
|
|
42
|
+
fallbacks: [{ provider: "openrouter", model: "deepseek/deepseek-v3.2" }],
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAU,CAAC;AA8C5G;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAiB;IAC3C,SAAS,EAAE;QACV,OAAO,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,EAAE,iBAAiB,EAAE;QAC5D,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,gBAAgB,EAAE,CAAC;KAC5D;IACD,SAAS,EAAE;QACV,OAAO,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,EAAE,mBAAmB,EAAE;QAC9D,SAAS,EAAE;YACV,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE;YACxC,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,EAAE,wBAAwB,EAAE;SAC3D;KACD;IACD,WAAW,EAAE;QACZ,OAAO,EAAE,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,EAAE,wBAAwB,EAAE;QACpE,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,kBAAkB,EAAE,CAAC;KAC9D;IACD,KAAK,EAAE;QACN,OAAO,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,kBAAkB,EAAE;QAC1D,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,EAAE,wBAAwB,EAAE,CAAC;KACxE;IACD,MAAM,EAAE;QACP,OAAO,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,EAAE,mBAAmB,EAAE;QAC9D,SAAS,EAAE,EAAE;KACb;IACD,IAAI,EAAE;QACL,OAAO,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE;QACjD,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,EAAE,mBAAmB,EAAE,CAAC;KAClE;IACD,UAAU,EAAE;QACX,OAAO,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,EAAE,iBAAiB,EAAE;QAC5D,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,gBAAgB,EAAE,CAAC;KAC5D;IACD,SAAS,EAAE;QACV,OAAO,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,kBAAkB,EAAE;QAC1D,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,EAAE,wBAAwB,EAAE,CAAC;KACxE;CACD,CAAC","sourcesContent":["/**\n * Built-in roles that must be present in every config.\n */\nexport const BUILT_IN_ROLES = [\"architect\", \"implement\", \"boilerplate\", \"quick\", \"review\", \"docs\"] as const;\n\n/**\n * Built-in router roles. Extensible via string type.\n */\nexport type RouterRole = (typeof BUILT_IN_ROLES)[number] | (string & {});\n\n/**\n * Reference to a specific provider/model combination.\n */\nexport interface ModelRef {\n\tprovider: string;\n\tmodel: string;\n}\n\n/**\n * Configuration for a single role: primary model + ordered fallback chain.\n */\nexport interface RoleConfig {\n\tprimary: ModelRef;\n\tfallbacks: ModelRef[];\n}\n\n/**\n * Full router configuration mapping roles to model configs.\n */\nexport type RouterConfig = Record<string, RoleConfig>;\n\n/**\n * Cost tracking entry written to .draht/cost-log.jsonl.\n */\nexport interface CostEntry {\n\ttimestamp: string;\n\trole: string;\n\tprovider: string;\n\tmodel: string;\n\tinputTokens: number;\n\toutputTokens: number;\n\t/** Reasoning/thinking tokens, billed at input token rate. */\n\treasoningTokens?: number;\n\testimatedCostUsd: number;\n\tsessionId: string;\n\t/** Trajectory id shared by every call (root + sub) within a single RLM session. */\n\ttrajectoryId?: string;\n}\n\n/**\n * Default role→model configuration.\n */\nexport const DEFAULT_CONFIG: RouterConfig = {\n\tarchitect: {\n\t\tprimary: { provider: \"anthropic\", model: \"claude-opus-4-6\" },\n\t\tfallbacks: [{ provider: \"google\", model: \"gemini-2.5-pro\" }],\n\t},\n\timplement: {\n\t\tprimary: { provider: \"anthropic\", model: \"claude-sonnet-4-6\" },\n\t\tfallbacks: [\n\t\t\t{ provider: \"openai\", model: \"gpt-5.2\" },\n\t\t\t{ provider: \"openrouter\", model: \"deepseek/deepseek-v3.2\" },\n\t\t],\n\t},\n\tboilerplate: {\n\t\tprimary: { provider: \"openrouter\", model: \"deepseek/deepseek-v3.2\" },\n\t\tfallbacks: [{ provider: \"google\", model: \"gemini-2.5-flash\" }],\n\t},\n\tquick: {\n\t\tprimary: { provider: \"google\", model: \"gemini-2.5-flash\" },\n\t\tfallbacks: [{ provider: \"openrouter\", model: \"deepseek/deepseek-v3.2\" }],\n\t},\n\treview: {\n\t\tprimary: { provider: \"anthropic\", model: \"claude-sonnet-4-6\" },\n\t\tfallbacks: [],\n\t},\n\tdocs: {\n\t\tprimary: { provider: \"openai\", model: \"gpt-5.2\" },\n\t\tfallbacks: [{ provider: \"anthropic\", model: \"claude-sonnet-4-6\" }],\n\t},\n\t\"rlm-root\": {\n\t\tprimary: { provider: \"anthropic\", model: \"claude-opus-4-6\" },\n\t\tfallbacks: [{ provider: \"google\", model: \"gemini-2.5-pro\" }],\n\t},\n\t\"rlm-sub\": {\n\t\tprimary: { provider: \"google\", model: \"gemini-2.5-flash\" },\n\t\tfallbacks: [{ provider: \"openrouter\", model: \"deepseek/deepseek-v3.2\" }],\n\t},\n};\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@draht/router",
|
|
3
|
+
"version": "2026.7.12-1",
|
|
4
|
+
"description": "Role-based model routing with direct API calls, auto-fallback, and cost tracking",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./extension": {
|
|
18
|
+
"types": "./src/extension.ts",
|
|
19
|
+
"import": "./src/extension.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"clean": "rm -rf dist",
|
|
24
|
+
"build": "tsc -p tsconfig.build.json",
|
|
25
|
+
"test": "bun test"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@draht/ai": "2026.7.12-1"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@draht/coding-agent": "2026.7.12-1"
|
|
32
|
+
}
|
|
33
|
+
}
|