omnicell-router 0.2.0 → 0.3.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.
Files changed (102) hide show
  1. package/backend/dist/budget/budget-manager.d.ts +25 -0
  2. package/backend/dist/budget/budget-manager.js +54 -0
  3. package/backend/dist/cli/clients/index.js +2 -0
  4. package/backend/dist/cli/clients/omp.d.ts +53 -0
  5. package/backend/dist/cli/clients/omp.js +664 -0
  6. package/backend/dist/cli/clients/pi.js +79 -32
  7. package/backend/dist/cli/index.js +2 -2
  8. package/backend/dist/cli/types.d.ts +1 -1
  9. package/backend/dist/config/governance-limits.d.ts +68 -0
  10. package/backend/dist/config/governance-limits.js +261 -0
  11. package/backend/dist/config/index.js +1 -1
  12. package/backend/dist/config/types.d.ts +15 -5
  13. package/backend/dist/flywheel/collector.d.ts +74 -0
  14. package/backend/dist/flywheel/collector.js +235 -0
  15. package/backend/dist/flywheel/dataset.d.ts +130 -0
  16. package/backend/dist/flywheel/dataset.js +638 -0
  17. package/backend/dist/flywheel/decision-capture.d.ts +68 -0
  18. package/backend/dist/flywheel/decision-capture.js +85 -0
  19. package/backend/dist/flywheel/prune.d.ts +133 -0
  20. package/backend/dist/flywheel/prune.js +495 -0
  21. package/backend/dist/flywheel/schema.d.ts +108 -0
  22. package/backend/dist/flywheel/schema.js +117 -0
  23. package/backend/dist/flywheel/trainer.d.ts +84 -0
  24. package/backend/dist/flywheel/trainer.js +198 -0
  25. package/backend/dist/harness/adapters/amp.js +1 -0
  26. package/backend/dist/harness/adapters/claude.js +1 -0
  27. package/backend/dist/harness/adapters/cline.js +3 -1
  28. package/backend/dist/harness/adapters/codex.js +3 -1
  29. package/backend/dist/harness/adapters/cursor.js +1 -0
  30. package/backend/dist/harness/adapters/declarative.d.ts +3 -1
  31. package/backend/dist/harness/adapters/declarative.js +15 -1
  32. package/backend/dist/harness/adapters/dsh.js +1 -1
  33. package/backend/dist/harness/adapters/gemini-cli.js +1 -0
  34. package/backend/dist/harness/adapters/github-copilot.js +1 -0
  35. package/backend/dist/harness/adapters/index.d.ts +11 -3
  36. package/backend/dist/harness/adapters/index.js +48 -1
  37. package/backend/dist/harness/adapters/omp.d.ts +11 -0
  38. package/backend/dist/harness/adapters/omp.js +22 -0
  39. package/backend/dist/harness/adapters/opencode.js +1 -0
  40. package/backend/dist/harness/adapters/pi.js +2 -1
  41. package/backend/dist/harness/adapters/types.d.ts +19 -4
  42. package/backend/dist/harness/adapters/windsurf.js +1 -0
  43. package/backend/dist/harness/adapters/zed.js +3 -1
  44. package/backend/dist/harness/hosts.js +1 -0
  45. package/backend/dist/harness/translation.js +12 -4
  46. package/backend/dist/harness/types.d.ts +2 -2
  47. package/backend/dist/harness/writers/skill.d.ts +1 -1
  48. package/backend/dist/opencode/agents.d.ts +7 -0
  49. package/backend/dist/opencode/agents.js +22 -3
  50. package/backend/dist/opencode/catalog/custom-store.d.ts +50 -0
  51. package/backend/dist/opencode/catalog/custom-store.js +144 -0
  52. package/backend/dist/opencode/credentials/push.d.ts +38 -0
  53. package/backend/dist/opencode/credentials/push.js +116 -0
  54. package/backend/dist/opencode/credentials/reconcile.d.ts +49 -0
  55. package/backend/dist/opencode/credentials/reconcile.js +216 -0
  56. package/backend/dist/opencode/sync.d.ts +29 -0
  57. package/backend/dist/opencode/sync.js +186 -0
  58. package/backend/dist/pipeline/account-selection.d.ts +101 -0
  59. package/backend/dist/pipeline/account-selection.js +112 -0
  60. package/backend/dist/pipeline/orchestrator.js +34 -3
  61. package/backend/dist/providers/systemone.d.ts +29 -0
  62. package/backend/dist/providers/systemone.js +170 -0
  63. package/backend/dist/ratelimit/duration.d.ts +24 -0
  64. package/backend/dist/ratelimit/duration.js +43 -0
  65. package/backend/dist/ratelimit/governance-runtime.d.ts +121 -0
  66. package/backend/dist/ratelimit/governance-runtime.js +183 -0
  67. package/backend/dist/ratelimit/index.d.ts +4 -0
  68. package/backend/dist/ratelimit/index.js +4 -0
  69. package/backend/dist/ratelimit/limiter.d.ts +157 -0
  70. package/backend/dist/ratelimit/limiter.js +244 -0
  71. package/backend/dist/ratelimit/sliding-window-counter.d.ts +63 -0
  72. package/backend/dist/ratelimit/sliding-window-counter.js +103 -0
  73. package/backend/dist/ratelimit/subject.d.ts +26 -0
  74. package/backend/dist/ratelimit/subject.js +28 -0
  75. package/backend/dist/resilience/account-state.d.ts +84 -0
  76. package/backend/dist/resilience/account-state.js +228 -0
  77. package/backend/dist/router/layer1-classifier.d.ts +111 -0
  78. package/backend/dist/router/layer1-classifier.js +467 -0
  79. package/backend/dist/router/layer2-judge.d.ts +111 -0
  80. package/backend/dist/router/layer2-judge.js +373 -0
  81. package/backend/dist/router/retry-escalation.d.ts +89 -0
  82. package/backend/dist/router/retry-escalation.js +99 -0
  83. package/backend/dist/routes/flywheel.d.ts +3 -0
  84. package/backend/dist/routes/flywheel.js +268 -0
  85. package/backend/dist/routes/harness.js +4 -1
  86. package/backend/dist/routes/systemone.d.ts +3 -0
  87. package/backend/dist/routes/systemone.js +90 -0
  88. package/backend/dist/server.d.ts +0 -15
  89. package/backend/dist/server.js +40 -43
  90. package/backend/dist/session/session-manager.d.ts +15 -0
  91. package/backend/dist/session/session-manager.js +17 -0
  92. package/backend/dist/session/session-row.d.ts +78 -0
  93. package/backend/dist/session/session-row.js +52 -0
  94. package/backend/dist/surfaces/governance-envelope.d.ts +34 -0
  95. package/backend/dist/surfaces/governance-envelope.js +47 -0
  96. package/backend/dist/utils/token-estimator.d.ts +7 -0
  97. package/backend/dist/utils/token-estimator.js +20 -0
  98. package/bin/ocr.js +2 -2
  99. package/frontend/dist/assets/{index-DvXKvNxl.js → index-B7GKsUyU.js} +264 -244
  100. package/frontend/dist/assets/{index-1o7qI-KJ.css → index-DLKLOkQJ.css} +1 -1
  101. package/frontend/dist/index.html +2 -2
  102. package/package.json +1 -1
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Console session-row contract — the wire shape of `GET /api/admin/sessions`
3
+ * and `GET /api/admin/sessions/:id` (ADR-0002 §3 "Console surface").
4
+ *
5
+ * Live rows (session entity + traces) and trace-reconstructed rows (after a
6
+ * gateway restart: traces alone) both target this one shape, so the console can
7
+ * never show two different vocabularies for the same table — and the frontend
8
+ * `SessionRecord` mirrors it 1:1 by this name.
9
+ *
10
+ * WHY A WHITELIST, NEVER `...session`: `ConversationSession` is gateway-internal
11
+ * routing state. `pinnedModel`/`pinnedProvider` are the session's ANCHOR — which
12
+ * is NOT the model that last served it (a failover landing is) — and
13
+ * `origin.clientIp` is a client fingerprint that no console surface asks for.
14
+ * Spreading the entity published both; projecting field by field makes the next
15
+ * internal field invisible by default instead of leaking on the next edit.
16
+ */
17
+ import type { ConversationSession } from './session-manager.js';
18
+ import type { ExecutionTrace, TraceTracker } from '../trace/tracker.js';
19
+ import type { CaptureKeyIdentity } from '../capture/recorder.js';
20
+ /**
21
+ * How the client addressed a session's turns, derived from persisted traces
22
+ * (observability reconstruction — never routing state):
23
+ * - 'auto' : every turn named an auto alias (bare auto or a tier-forced
24
+ * lite/plus/pro/ultra) — the ratchet owns the tier.
25
+ * - 'explicit': every turn named a concrete registered model — the client owns
26
+ * the choice, the tier column has no lock to show.
27
+ * - 'combo' : every turn went through a combo — the composition IS the
28
+ * decision; the executed member's tier is not a session ceiling.
29
+ * - 'mixed' : the conversation switched entry modes across turns.
30
+ */
31
+ export type SessionEntryMode = 'auto' | 'explicit' | 'combo' | 'mixed';
32
+ /** Classify a session from its traces (chronological order irrelevant). */
33
+ export declare function deriveSessionEntryMode(traces: ExecutionTrace[]): SessionEntryMode;
34
+ /**
35
+ * The trace-derived half of a row, reused verbatim by both builders so a live
36
+ * row and a reconstructed row can never aggregate differently.
37
+ */
38
+ type SessionAggregates = ReturnType<TraceTracker['getSessionAggregates']>;
39
+ /**
40
+ * The entity-only half of a row — the ONLY fields a live session entity may
41
+ * contribute. `Pick` + an explicit literal makes the compiler reject both a
42
+ * dropped field and a field that is not on the list, so the whitelist cannot
43
+ * silently rot: adding an internal field to `ConversationSession` does not
44
+ * publish it, and deleting one of these is a compile error, not a `undefined`
45
+ * cell in the console.
46
+ */
47
+ type SessionEntityView = Pick<ConversationSession, 'id' | 'maxTier' | 'createdAt' | 'lastActiveAt' | 'turnCount' | 'historyTiers' | 'firstUserMessage' | 'boundKeyHashPrefix' | 'boundAt'>;
48
+ /** Whitelist projection of a live session entity. Never spread the entity. */
49
+ export declare function projectSessionEntity(s: ConversationSession): SessionEntityView;
50
+ /**
51
+ * Execution view of a session: the model that actually served its LAST turn —
52
+ * the registered id exactly as registered (`provider/model` for
53
+ * catalog/tier-derived models, a bare id for hand-configured ones) — plus the
54
+ * provider that served it, which is authoritative for both shapes. Deliberately
55
+ * NOT the routing pin (ADR-0002 §3): a failover landing is not the session's
56
+ * anchor, and a reconstructed row has no anchor at all. Both `null` when the
57
+ * session has no trace yet (a session entity exists from birth, but only a
58
+ * served turn produces a trace).
59
+ */
60
+ interface CurrentModelView {
61
+ currentModel: string | null;
62
+ currentProvider: string | null;
63
+ }
64
+ /** Single source of the execution-view derivation (live + reconstructed). */
65
+ export declare function currentModelOf(traces: ExecutionTrace[]): CurrentModelView;
66
+ /** One console session row. */
67
+ export interface SessionRow extends SessionEntityView, SessionAggregates {
68
+ /** Gateway-internal loopback row — derived from the id, never stored. */
69
+ internal: boolean;
70
+ entryMode: SessionEntryMode;
71
+ currentModel: string | null;
72
+ currentProvider: string | null;
73
+ /** Distinct client API-key identities, write order (first = display anchor). */
74
+ apiKeys?: CaptureKeyIdentity[];
75
+ /** Distinct client User-Agents, write order (console display only). */
76
+ clients?: string[];
77
+ }
78
+ export {};
@@ -0,0 +1,52 @@
1
+ /** Classify a session from its traces (chronological order irrelevant). */
2
+ export function deriveSessionEntryMode(traces) {
3
+ // Virtual auto aliases: bare auto plus the four tier-forced aliases
4
+ // (lite/plus/pro/ultra). Routing runs these through the ratchet path (the
5
+ // tier aliases rewrite force_tier before routing), so a tier-forced session
6
+ // owns a tier lock — classifying it as explicit would strip the tier badge.
7
+ const AUTO_ALIAS_MODELS = new Set(['auto', 'lite', 'plus', 'pro', 'ultra']);
8
+ let sawAuto = false;
9
+ let sawExplicit = false;
10
+ let sawCombo = false;
11
+ for (const t of traces) {
12
+ // Combo turns are the only ones carrying this reasonCode (the orchestrator
13
+ // sets it on the bypass decision and the trace copies it verbatim).
14
+ if (t.routing?.reasonCode === 'combo')
15
+ sawCombo = true;
16
+ else {
17
+ // Routing-name matching trims once while request.model is stored
18
+ // verbatim (surface contract) — reconstruct the routing view, not the
19
+ // bytes: a padded alias routed as the alias must classify as the alias.
20
+ const requested = (t.request?.model ?? '').trim();
21
+ if (!requested || AUTO_ALIAS_MODELS.has(requested))
22
+ sawAuto = true;
23
+ else
24
+ sawExplicit = true;
25
+ }
26
+ }
27
+ if (Number(sawAuto) + Number(sawExplicit) + Number(sawCombo) !== 1)
28
+ return 'mixed';
29
+ return sawCombo ? 'combo' : sawExplicit ? 'explicit' : 'auto';
30
+ }
31
+ /** Whitelist projection of a live session entity. Never spread the entity. */
32
+ export function projectSessionEntity(s) {
33
+ return {
34
+ id: s.id,
35
+ maxTier: s.maxTier,
36
+ createdAt: s.createdAt,
37
+ lastActiveAt: s.lastActiveAt,
38
+ turnCount: s.turnCount,
39
+ historyTiers: s.historyTiers,
40
+ firstUserMessage: s.firstUserMessage,
41
+ boundKeyHashPrefix: s.boundKeyHashPrefix,
42
+ boundAt: s.boundAt,
43
+ };
44
+ }
45
+ /** Single source of the execution-view derivation (live + reconstructed). */
46
+ export function currentModelOf(traces) {
47
+ const last = traces[traces.length - 1];
48
+ return {
49
+ currentModel: last?.execution.modelUsed ?? null,
50
+ currentProvider: last?.execution.provider ?? null,
51
+ };
52
+ }
@@ -0,0 +1,34 @@
1
+ import type { Verdict } from '../ratelimit/index.js';
2
+ import type { ProtocolSurface } from './types.js';
3
+ /**
4
+ * Wire shapes for inbound governance denials (design §8: the 429 + Retry-After
5
+ * and 402 bodies are protocol-compatible on ALL FOUR inference surfaces).
6
+ * The status mapping is the ENFORCE policy half (design §3): `costUsd` denial
7
+ * → 402 contract refusal (NEVER 429 — a budget is not a throttle, retrying
8
+ * will not help), any other denial → 429 + Retry-After.
9
+ *
10
+ * These bodies deliberately do NOT route through each surface's `errorBody`:
11
+ * that helper is the legacy parse-400/execution-catch envelope with its own
12
+ * status→type mapping (invalid_request_error / api_error), and "preserved,
13
+ * not fixed" applies to what it already answers — governance refusals are NEW
14
+ * behavior, so they carry the provider-real rate/budget types directly:
15
+ * chat / responses / systemone → OpenAI style {error:{message,type,code}}
16
+ * anthropic → Anthropic style {type:'error',error:{type,message}}
17
+ * Key insertion order mirrors each surface's existing error emitter (wire-byte
18
+ * consistency with the surfaces/ family).
19
+ */
20
+ /** The engine's denial arm — the envelope helper's input (Verdict narrowed). */
21
+ export type GovernanceDenial = Extract<Verdict, {
22
+ allowed: false;
23
+ }>;
24
+ export type GovernanceRejection = {
25
+ status: number;
26
+ body: unknown;
27
+ headers: Record<string, string>;
28
+ };
29
+ /**
30
+ * Render one denial verdict into the caller's protocol dialect. The message
31
+ * carries the rule id verbatim (§5 reason culture: operator maps the refusal
32
+ * straight back to the YAML `limits:` line — zero translation).
33
+ */
34
+ export declare function governanceRejection(surface: ProtocolSurface, denial: GovernanceDenial, keyName: string): GovernanceRejection;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Render one denial verdict into the caller's protocol dialect. The message
3
+ * carries the rule id verbatim (§5 reason culture: operator maps the refusal
4
+ * straight back to the YAML `limits:` line — zero translation).
5
+ */
6
+ export function governanceRejection(surface, denial, keyName) {
7
+ const isCost = denial.limitedBy === 'costUsd';
8
+ const retryAfterSec = Math.max(1, Math.ceil(denial.retryAfterMs / 1000));
9
+ // Retry-After rides the 429 only: a sliding-window budget has a next-open
10
+ // moment, but it is NOT a retry contract (top-up / renegotiation is the
11
+ // recovery), so answering 402 with "retry in Ns" would teach clients to
12
+ // hammer — design §1: over-limit rejects, no queueing.
13
+ const headers = isCost ? {} : { 'retry-after': String(retryAfterSec) };
14
+ const message = isCost
15
+ ? `Budget exhausted for API key '${keyName}' (rule ${denial.rule}). This is a contract refusal, not a throttle: retrying will not help — the window recovers as recorded spend ages out, or the gateway operator raises the limit.`
16
+ : `Rate limit exceeded for API key '${keyName}' (rule ${denial.rule}). Retry after ${retryAfterSec}s.`;
17
+ if (surface.wire === 'anthropic') {
18
+ // Anthropic's own vocabulary: 429 rate_limit_error / 402 billing_error.
19
+ // 'insufficient_quota' is NOT an Anthropic error type — emitting it here
20
+ // would break SDK type-parsing on the /v1/messages wire. The error object
21
+ // is {type, message} with no code slot (mirrors surfaces/anthropic.ts).
22
+ return {
23
+ status: isCost ? 402 : 429,
24
+ body: {
25
+ type: 'error',
26
+ error: {
27
+ type: isCost ? 'billing_error' : 'rate_limit_error',
28
+ message,
29
+ },
30
+ },
31
+ headers,
32
+ };
33
+ }
34
+ // OpenAI-compatible family (chat / responses / systemone): message/type/code,
35
+ // insertion order matching surfaces/chat.ts chatErrorBody.
36
+ return {
37
+ status: isCost ? 402 : 429,
38
+ body: {
39
+ error: {
40
+ message,
41
+ type: isCost ? 'insufficient_quota' : 'rate_limit_error',
42
+ code: isCost ? 'insufficient_quota' : 'rate_limit_exceeded',
43
+ },
44
+ },
45
+ headers,
46
+ };
47
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Fast, language-neutral token estimator:
3
+ * ~1.5 chars per token for CJK scripts, ~4 chars per token for Latin, Cyrillic, Greek, Arabic, etc.
4
+ */
5
+ export declare class TokenEstimator {
6
+ static estimate(text: string): number;
7
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Fast, language-neutral token estimator:
3
+ * ~1.5 chars per token for CJK scripts, ~4 chars per token for Latin, Cyrillic, Greek, Arabic, etc.
4
+ */
5
+ export class TokenEstimator {
6
+ static estimate(text) {
7
+ let cjkCount = 0;
8
+ let otherCount = 0;
9
+ for (let i = 0; i < text.length; i++) {
10
+ const code = text.charCodeAt(i);
11
+ if (code >= 0x4e00 && code <= 0x9fff) {
12
+ cjkCount++;
13
+ }
14
+ else {
15
+ otherCount++;
16
+ }
17
+ }
18
+ return Math.ceil(cjkCount / 1.5 + otherCount / 4);
19
+ }
20
+ }
package/bin/ocr.js CHANGED
@@ -1,2 +1,2 @@
1
- #!/usr/bin/env node
2
- import '../backend/dist/cli/index.js';
1
+ #!/usr/bin/env node
2
+ import '../backend/dist/cli/index.js';