opencode-model-router 2.1.0 → 2.3.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/README.md +44 -5
  3. package/package.json +4 -1
  4. package/src/commands/output.ts +4 -1
  5. package/src/compat/child-session.ts +43 -0
  6. package/src/compat/v2-client.ts +107 -7
  7. package/src/compat/v2-hooks.ts +174 -47
  8. package/src/escalate/attempt-recorder.ts +263 -0
  9. package/src/escalate/ladder.ts +484 -10
  10. package/src/escalate/resume.ts +208 -0
  11. package/src/escalate/variants.ts +391 -0
  12. package/src/index.ts +557 -61
  13. package/src/router/build-info.ts +151 -0
  14. package/src/router/config.ts +1691 -67
  15. package/src/router/protocol.ts +47 -0
  16. package/src/router/sessions.ts +441 -9
  17. package/src/routing/advisor/findings.ts +706 -0
  18. package/src/routing/advisor/index.ts +606 -0
  19. package/src/routing/classify/backends/host.ts +163 -0
  20. package/src/routing/classify/backends/openai-compatible.ts +275 -0
  21. package/src/routing/classify/backends/shared.ts +741 -0
  22. package/src/routing/classify/backends/typesafe.ts +315 -0
  23. package/src/routing/classify/fences.ts +96 -0
  24. package/src/routing/classify/index.ts +576 -0
  25. package/src/routing/classify/route-line.ts +298 -0
  26. package/src/routing/classify/rules.ts +607 -0
  27. package/src/routing/classify/scrub.ts +214 -0
  28. package/src/routing/classify/state.ts +148 -0
  29. package/src/routing/classify/text.ts +20 -0
  30. package/src/routing/classify/types.ts +1280 -0
  31. package/src/routing/commands/annotate-plan.ts +652 -0
  32. package/src/routing/commands/stats.ts +131 -0
  33. package/src/routing/commands/v1-roles.ts +64 -0
  34. package/src/routing/engine/index.ts +32 -0
  35. package/src/routing/engine/kernel.ts +597 -0
  36. package/src/routing/engine/ladders.ts +561 -0
  37. package/src/routing/engine/plan.ts +404 -0
  38. package/src/routing/engine/protocol-line.ts +133 -0
  39. package/src/routing/engine/simulate.ts +167 -0
  40. package/src/routing/engine/types.ts +270 -0
  41. package/src/routing/file-lock.ts +53 -0
  42. package/src/routing/outcomes/beta.ts +141 -0
  43. package/src/routing/outcomes/cost.ts +313 -0
  44. package/src/routing/outcomes/index.ts +172 -0
  45. package/src/routing/outcomes/ingest.ts +812 -0
  46. package/src/routing/outcomes/persist.ts +1058 -0
  47. package/src/routing/outcomes/stats.ts +602 -0
  48. package/src/routing/outcomes/store.ts +653 -0
  49. package/src/routing/outcomes/types.ts +954 -0
  50. package/src/routing/wire/dispatch.ts +799 -0
  51. package/src/routing/wire/hint.ts +208 -0
  52. package/src/routing/wire/host-info.ts +256 -0
  53. package/src/routing/wire/runtime.ts +259 -0
  54. package/src/v2.ts +15 -1
  55. package/tiers.json +43 -0
@@ -0,0 +1,151 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { dirname, isAbsolute, join, resolve } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ /**
6
+ * Build marker for the liveness probe of the dogfood checkpoints (plan §0.11):
7
+ * the package version plus the git commit the running code was loaded from.
8
+ *
9
+ * There is no build step (package.json has no build script) and the host loads
10
+ * the plugin straight from a checkout, so the commit is read from `.git` at
11
+ * module load time. Everything here is best-effort: any failure yields
12
+ * `"unknown"` and nothing ever throws, because a missing marker must never stop
13
+ * the plugin from loading.
14
+ */
15
+ export interface BuildInfo {
16
+ /** `version` of the plugin's package.json, or `"unknown"`. */
17
+ readonly version: string;
18
+ /** Full commit sha (40 or 64 hex digits), or `"unknown"`. */
19
+ readonly sha: string;
20
+ }
21
+
22
+ export const UNKNOWN = "unknown";
23
+
24
+ const SHA_PATTERN = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/i;
25
+
26
+ /** Plugin root: this file lives in `<root>/src/router/`. */
27
+ function pluginRoot(): string {
28
+ return join(dirname(fileURLToPath(import.meta.url)), "..", "..");
29
+ }
30
+
31
+ /** Text of a file, or `undefined` when it cannot be read. */
32
+ function readText(path: string): string | undefined {
33
+ try {
34
+ return readFileSync(path, "utf-8");
35
+ } catch {
36
+ // Absent or unreadable is the normal case for most of the paths probed here.
37
+ return undefined;
38
+ }
39
+ }
40
+
41
+ /** The sha a `packed-refs` file records for `ref`, if any. */
42
+ function packedRef(packedRefs: string, ref: string): string | undefined {
43
+ for (const line of packedRefs.split(/\r?\n/)) {
44
+ if (line === "" || line.startsWith("#") || line.startsWith("^")) continue;
45
+ const space = line.indexOf(" ");
46
+ if (space > 0 && line.slice(space + 1).trim() === ref) {
47
+ const sha = line.slice(0, space);
48
+ if (SHA_PATTERN.test(sha)) return sha.toLowerCase();
49
+ }
50
+ }
51
+ return undefined;
52
+ }
53
+
54
+ /**
55
+ * Directory that holds `HEAD`: `<root>/.git` itself, or — when `.git` is a file
56
+ * (a linked worktree or a submodule) — the `gitdir:` it points at.
57
+ */
58
+ function resolveGitDir(root: string): string | undefined {
59
+ const dotGit = join(root, ".git");
60
+ const pointer = readText(dotGit);
61
+ if (pointer === undefined) {
62
+ // A directory cannot be read as text; its HEAD file is the evidence.
63
+ return readText(join(dotGit, "HEAD")) !== undefined ? dotGit : undefined;
64
+ }
65
+ const match = /^gitdir:\s*(.+?)\s*$/m.exec(pointer);
66
+ if (!match?.[1]) return undefined;
67
+ return isAbsolute(match[1]) ? match[1] : resolve(root, match[1]);
68
+ }
69
+
70
+ /**
71
+ * Commit sha of the checkout at `root`, read from `.git`: `HEAD`, then the ref
72
+ * file it names (in the worktree's own git dir, then in the common dir that its
73
+ * `commondir` file points at), then `packed-refs`. A detached `HEAD` holds the
74
+ * sha itself. Returns `"unknown"` for anything else, and never throws.
75
+ *
76
+ * Not read: the `reftable` ref format (`git init --ref-format=reftable`), which
77
+ * keeps `HEAD` as the stub `ref: refs/heads/.invalid` and the real refs in binary
78
+ * tables under `.git/reftable/`. Such a checkout reports `"unknown"` (QA-1.1-20).
79
+ */
80
+ export function readGitSha(root: string): string {
81
+ try {
82
+ const gitDir = resolveGitDir(root);
83
+ if (gitDir === undefined) return UNKNOWN;
84
+ const head = readText(join(gitDir, "HEAD"))?.trim();
85
+ if (head === undefined || head === "") return UNKNOWN;
86
+ if (SHA_PATTERN.test(head)) return head.toLowerCase();
87
+ const ref = /^ref:\s*(\S+)$/.exec(head)?.[1];
88
+ if (ref === undefined) return UNKNOWN;
89
+
90
+ const commonText = readText(join(gitDir, "commondir"))?.trim();
91
+ const dirs = [gitDir];
92
+ if (commonText) dirs.push(isAbsolute(commonText) ? commonText : resolve(gitDir, commonText));
93
+ for (const dir of dirs) {
94
+ const loose = readText(join(dir, ref))?.trim();
95
+ if (loose !== undefined && SHA_PATTERN.test(loose)) return loose.toLowerCase();
96
+ }
97
+ for (const dir of dirs) {
98
+ const packed = readText(join(dir, "packed-refs"));
99
+ const sha = packed === undefined ? undefined : packedRef(packed, ref);
100
+ if (sha !== undefined) return sha;
101
+ }
102
+ return UNKNOWN;
103
+ } catch {
104
+ // Unreachable in practice (every read above is guarded); the contract is
105
+ // that this function never throws, so a surprise degrades to "unknown".
106
+ return UNKNOWN;
107
+ }
108
+ }
109
+
110
+ /** `version` of the package.json at `root`, or `"unknown"`. Never throws. */
111
+ export function readPackageVersion(root: string): string {
112
+ try {
113
+ const text = readText(join(root, "package.json"));
114
+ if (text === undefined) return UNKNOWN;
115
+ const version: unknown = (JSON.parse(text) as { version?: unknown } | null)?.version;
116
+ return typeof version === "string" && version !== "" ? version : UNKNOWN;
117
+ } catch {
118
+ // Malformed package.json: the marker degrades, the plugin still loads.
119
+ return UNKNOWN;
120
+ }
121
+ }
122
+
123
+ /** Build info of the checkout at `root`. */
124
+ export function readBuildInfo(root: string): BuildInfo {
125
+ return Object.freeze({ version: readPackageVersion(root), sha: readGitSha(root) });
126
+ }
127
+
128
+ /**
129
+ * Build info of the plugin checkout found by `root()`. If even locating it throws
130
+ * (`import.meta.url` is not a `file:` URL when the module is bundled or loaded
131
+ * remotely, QA-1.1-20), there is no checkout to read: both parts are `"unknown"`.
132
+ */
133
+ export function loadBuildInfo(root: () => string = pluginRoot): BuildInfo {
134
+ try {
135
+ return readBuildInfo(root());
136
+ } catch {
137
+ return Object.freeze({ version: UNKNOWN, sha: UNKNOWN });
138
+ }
139
+ }
140
+
141
+ /** The running plugin's build info, read once when this module loads. */
142
+ export const buildInfo: BuildInfo = loadBuildInfo();
143
+
144
+ /**
145
+ * The marker line `/router` prints: `router: engine=<mode> build=<version>+<sha7>`.
146
+ * `<sha7>` is the first seven digits of the commit, or `unknown`.
147
+ */
148
+ export function formatRouterLine(engine: string, info: BuildInfo = buildInfo): string {
149
+ const sha = info.sha === UNKNOWN ? UNKNOWN : info.sha.slice(0, 7);
150
+ return `router: engine=${engine} build=${info.version}+${sha}`;
151
+ }