@motebit/sdk 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/__tests__/appearance-config.test.d.ts +2 -0
- package/dist/__tests__/appearance-config.test.d.ts.map +1 -0
- package/dist/__tests__/appearance-config.test.js +104 -0
- package/dist/__tests__/appearance-config.test.js.map +1 -0
- package/dist/__tests__/branded-ids.test.d.ts +2 -0
- package/dist/__tests__/branded-ids.test.d.ts.map +1 -0
- package/dist/__tests__/branded-ids.test.js +70 -0
- package/dist/__tests__/branded-ids.test.js.map +1 -0
- package/dist/__tests__/index.test.d.ts +2 -0
- package/dist/__tests__/index.test.d.ts.map +1 -0
- package/dist/__tests__/index.test.js +194 -0
- package/dist/__tests__/index.test.js.map +1 -0
- package/dist/__tests__/provider-mode.test.d.ts +2 -0
- package/dist/__tests__/provider-mode.test.d.ts.map +1 -0
- package/dist/__tests__/provider-mode.test.js +180 -0
- package/dist/__tests__/provider-mode.test.js.map +1 -0
- package/dist/__tests__/provider-resolver.test.d.ts +2 -0
- package/dist/__tests__/provider-resolver.test.d.ts.map +1 -0
- package/dist/__tests__/provider-resolver.test.js +353 -0
- package/dist/__tests__/provider-resolver.test.js.map +1 -0
- package/dist/__tests__/voice-config.test.d.ts +2 -0
- package/dist/__tests__/voice-config.test.d.ts.map +1 -0
- package/dist/__tests__/voice-config.test.js +98 -0
- package/dist/__tests__/voice-config.test.js.map +1 -0
- package/dist/appearance-config.d.ts +55 -0
- package/dist/appearance-config.d.ts.map +1 -0
- package/dist/appearance-config.js +79 -0
- package/dist/appearance-config.js.map +1 -0
- package/dist/credential-types-doc.d.ts +73 -0
- package/dist/credential-types-doc.d.ts.map +1 -0
- package/dist/credential-types-doc.js +2 -0
- package/dist/credential-types-doc.js.map +1 -0
- package/dist/governance-config.d.ts +5 -0
- package/dist/governance-config.d.ts.map +1 -1
- package/dist/governance-config.js +8 -1
- package/dist/governance-config.js.map +1 -1
- package/dist/index.d.ts +55 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -4
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +44 -14
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +55 -18
- package/dist/models.js.map +1 -1
- package/dist/provider-mode.d.ts +109 -0
- package/dist/provider-mode.d.ts.map +1 -0
- package/dist/provider-mode.js +179 -0
- package/dist/provider-mode.js.map +1 -0
- package/dist/provider-resolver.d.ts +179 -0
- package/dist/provider-resolver.d.ts.map +1 -0
- package/dist/provider-resolver.js +224 -0
- package/dist/provider-resolver.js.map +1 -0
- package/dist/risk-labels.d.ts +22 -0
- package/dist/risk-labels.d.ts.map +1 -0
- package/dist/risk-labels.js +28 -0
- package/dist/risk-labels.js.map +1 -0
- package/dist/surface-options.d.ts +34 -0
- package/dist/surface-options.d.ts.map +1 -0
- package/dist/surface-options.js +36 -0
- package/dist/surface-options.js.map +1 -0
- package/dist/voice-config.d.ts +52 -0
- package/dist/voice-config.d.ts.map +1 -0
- package/dist/voice-config.js +61 -0
- package/dist/voice-config.js.map +1 -0
- package/package.json +7 -7
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// === Provider Mode (Three-Mode Architecture) ===
|
|
2
|
+
//
|
|
3
|
+
// User-intent-based provider config — matches the pattern of settlement rails:
|
|
4
|
+
// user picks the capability class, the system resolves the concrete vendor.
|
|
5
|
+
//
|
|
6
|
+
// Flat unions like "ollama" | "anthropic" | "openai" | "hybrid" mix implementation
|
|
7
|
+
// details with user intent. "Hybrid" is an implementation detail, not a user choice.
|
|
8
|
+
// "Ollama" is a vendor name, not a capability class.
|
|
9
|
+
//
|
|
10
|
+
// All surfaces (web, mobile, desktop, cli) persist and exchange this shape.
|
|
11
|
+
// Platform-specific sub-options live inside each mode's discriminated variant.
|
|
12
|
+
// === Migration ===
|
|
13
|
+
//
|
|
14
|
+
// Legacy shapes, in order of historical appearance:
|
|
15
|
+
//
|
|
16
|
+
// Web (apps/web/src/storage.ts):
|
|
17
|
+
// { type: "anthropic" | "openai" | "ollama" | "webllm" | "proxy",
|
|
18
|
+
// apiKey?, model, baseUrl?, proxyToken?, maxTokens?, temperature? }
|
|
19
|
+
//
|
|
20
|
+
// Mobile (apps/mobile/src/mobile-app.ts):
|
|
21
|
+
// { provider: "ollama" | "anthropic" | "openai" | "hybrid" | "proxy" | "local",
|
|
22
|
+
// localBackend?: "apple-fm" | "mlx",
|
|
23
|
+
// model?, apiKey?, ollamaEndpoint?, maxTokens? }
|
|
24
|
+
//
|
|
25
|
+
// Desktop (apps/desktop/src/index.ts):
|
|
26
|
+
// { provider: "anthropic" | "ollama" | "openai" | "proxy" | "hybrid",
|
|
27
|
+
// model?, apiKey?, maxTokens? }
|
|
28
|
+
//
|
|
29
|
+
// CLI (apps/cli/src/config.ts):
|
|
30
|
+
// { default_provider?: "anthropic" | "openai" | "ollama",
|
|
31
|
+
// default_model?, api_key? (in keyring) }
|
|
32
|
+
/**
|
|
33
|
+
* Heuristic: is this URL pointing at a local inference server?
|
|
34
|
+
* Shared across all surfaces so the migration behaves identically everywhere.
|
|
35
|
+
*/
|
|
36
|
+
export function isLocalServerUrl(url) {
|
|
37
|
+
if (!url)
|
|
38
|
+
return false;
|
|
39
|
+
try {
|
|
40
|
+
const u = new URL(url);
|
|
41
|
+
const host = u.hostname.toLowerCase();
|
|
42
|
+
return (host === "localhost" ||
|
|
43
|
+
host === "127.0.0.1" ||
|
|
44
|
+
host === "::1" ||
|
|
45
|
+
host === "0.0.0.0" ||
|
|
46
|
+
host.endsWith(".local") ||
|
|
47
|
+
/^10\./.test(host) ||
|
|
48
|
+
/^192\.168\./.test(host) ||
|
|
49
|
+
/^172\.(1[6-9]|2\d|3[01])\./.test(host));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Migrate any legacy provider config shape to `UnifiedProviderConfig`.
|
|
57
|
+
*
|
|
58
|
+
* Rules:
|
|
59
|
+
* - `hybrid` and `proxy` map to `motebit-cloud` (the product).
|
|
60
|
+
* - `ollama` with a localhost URL maps to `on-device/local-server`.
|
|
61
|
+
* - `ollama` with a remote URL maps to `on-device/local-server` with that endpoint preserved.
|
|
62
|
+
* - `anthropic` / `openai` / `google` map to `byok` with that vendor.
|
|
63
|
+
* - Mobile `local` with `localBackend` maps to `on-device` with that backend.
|
|
64
|
+
* - If input is already a `UnifiedProviderConfig` (has `mode`), it passes through.
|
|
65
|
+
*
|
|
66
|
+
* Returns `null` only if the input is entirely unrecognizable.
|
|
67
|
+
*/
|
|
68
|
+
export function migrateLegacyProvider(legacy) {
|
|
69
|
+
if (!legacy)
|
|
70
|
+
return null;
|
|
71
|
+
// Already-new shape — passthrough with light validation.
|
|
72
|
+
if (legacy.mode === "on-device" || legacy.mode === "motebit-cloud" || legacy.mode === "byok") {
|
|
73
|
+
return legacy;
|
|
74
|
+
}
|
|
75
|
+
const apiKey = legacy.apiKey ?? legacy.api_key;
|
|
76
|
+
const baseUrl = legacy.baseUrl ?? legacy.base_url;
|
|
77
|
+
const proxyToken = legacy.proxyToken ?? legacy.proxy_token;
|
|
78
|
+
const maxTokens = legacy.maxTokens ?? legacy.max_tokens;
|
|
79
|
+
const temperature = legacy.temperature;
|
|
80
|
+
const model = legacy.model ?? legacy.default_model;
|
|
81
|
+
// Normalize the discriminator across shapes.
|
|
82
|
+
const kind = legacy.type ?? legacy.provider ?? legacy.default_provider;
|
|
83
|
+
if (!kind)
|
|
84
|
+
return null;
|
|
85
|
+
switch (kind) {
|
|
86
|
+
case "proxy":
|
|
87
|
+
case "hybrid":
|
|
88
|
+
case "motebit-cloud":
|
|
89
|
+
return {
|
|
90
|
+
mode: "motebit-cloud",
|
|
91
|
+
model,
|
|
92
|
+
proxyToken,
|
|
93
|
+
baseUrl,
|
|
94
|
+
maxTokens,
|
|
95
|
+
temperature,
|
|
96
|
+
};
|
|
97
|
+
case "anthropic":
|
|
98
|
+
return {
|
|
99
|
+
mode: "byok",
|
|
100
|
+
vendor: "anthropic",
|
|
101
|
+
apiKey: apiKey ?? "",
|
|
102
|
+
model,
|
|
103
|
+
baseUrl,
|
|
104
|
+
maxTokens,
|
|
105
|
+
temperature,
|
|
106
|
+
};
|
|
107
|
+
case "openai":
|
|
108
|
+
return {
|
|
109
|
+
mode: "byok",
|
|
110
|
+
vendor: "openai",
|
|
111
|
+
apiKey: apiKey ?? "",
|
|
112
|
+
model,
|
|
113
|
+
baseUrl,
|
|
114
|
+
maxTokens,
|
|
115
|
+
temperature,
|
|
116
|
+
};
|
|
117
|
+
case "google":
|
|
118
|
+
return {
|
|
119
|
+
mode: "byok",
|
|
120
|
+
vendor: "google",
|
|
121
|
+
apiKey: apiKey ?? "",
|
|
122
|
+
model,
|
|
123
|
+
baseUrl,
|
|
124
|
+
maxTokens,
|
|
125
|
+
temperature,
|
|
126
|
+
};
|
|
127
|
+
case "ollama": {
|
|
128
|
+
// Ollama is always a local server — endpoint may or may not be localhost.
|
|
129
|
+
const endpoint = baseUrl ?? legacy.ollamaEndpoint ?? legacy.endpoint;
|
|
130
|
+
return {
|
|
131
|
+
mode: "on-device",
|
|
132
|
+
backend: "local-server",
|
|
133
|
+
model,
|
|
134
|
+
endpoint,
|
|
135
|
+
maxTokens,
|
|
136
|
+
temperature,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
case "webllm":
|
|
140
|
+
return {
|
|
141
|
+
mode: "on-device",
|
|
142
|
+
backend: "webllm",
|
|
143
|
+
model,
|
|
144
|
+
maxTokens,
|
|
145
|
+
temperature,
|
|
146
|
+
};
|
|
147
|
+
case "local": {
|
|
148
|
+
// Mobile legacy: `local` with `localBackend` sub-option.
|
|
149
|
+
const backend = (() => {
|
|
150
|
+
switch (legacy.localBackend) {
|
|
151
|
+
case "apple-fm":
|
|
152
|
+
return "apple-fm";
|
|
153
|
+
case "mlx":
|
|
154
|
+
return "mlx";
|
|
155
|
+
case "local-server":
|
|
156
|
+
return "local-server";
|
|
157
|
+
default:
|
|
158
|
+
return "mlx";
|
|
159
|
+
}
|
|
160
|
+
})();
|
|
161
|
+
return {
|
|
162
|
+
mode: "on-device",
|
|
163
|
+
backend,
|
|
164
|
+
model,
|
|
165
|
+
endpoint: legacy.ollamaEndpoint ?? legacy.endpoint,
|
|
166
|
+
maxTokens,
|
|
167
|
+
temperature,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
default:
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// === Defaults ===
|
|
175
|
+
/** Sensible default when no config has ever been persisted. */
|
|
176
|
+
export function defaultProviderConfig() {
|
|
177
|
+
return { mode: "motebit-cloud" };
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=provider-mode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-mode.js","sourceRoot":"","sources":["../src/provider-mode.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAClD,EAAE;AACF,+EAA+E;AAC/E,4EAA4E;AAC5E,EAAE;AACF,mFAAmF;AACnF,qFAAqF;AACrF,qDAAqD;AACrD,EAAE;AACF,4EAA4E;AAC5E,+EAA+E;AAyE/E,oBAAoB;AACpB,EAAE;AACF,oDAAoD;AACpD,EAAE;AACF,mCAAmC;AACnC,sEAAsE;AACtE,0EAA0E;AAC1E,EAAE;AACF,4CAA4C;AAC5C,oFAAoF;AACpF,2CAA2C;AAC3C,uDAAuD;AACvD,EAAE;AACF,yCAAyC;AACzC,0EAA0E;AAC1E,sCAAsC;AACtC,EAAE;AACF,kCAAkC;AAClC,8DAA8D;AAC9D,gDAAgD;AAEhD;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAA8B;IAC7D,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAC;IACvB,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACvB,MA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,CAAC,CAAC,CAAC;YACb,yDAAyD;YACzD,MAAM,OAAO,GAAG,CAAC,GAAoB,EAAE;gBACrC,QAAQ,MAAM,CAAC,YAAY,EAAE,CAAC;oBAC5B,KAAK,UAAU;wBACb,OAAO,UAAU,CAAC;oBACpB,KAAK,KAAK;wBACR,OAAO,KAAK,CAAC;oBACf,KAAK,cAAc;wBACjB,OAAO,cAAc,CAAC;oBACxB;wBACE,OAAO,KAAK,CAAC;gBACjB,CAAC;YACH,CAAC,CAAC,EAAE,CAAC;YACL,OAAO;gBACL,IAAI,EAAE,WAAW;gBACjB,OAAO;gBACP,KAAK;gBACL,QAAQ,EAAE,MAAM,CAAC,cAAc,IAAI,MAAM,CAAC,QAAQ;gBAClD,SAAS;gBACT,WAAW;aACZ,CAAC;QACJ,CAAC;QAED;YACE,OAAO,IAAI,CAAC;IAChB,CAAC;AACH,CAAC;AAED,mBAAmB;AAEnB,+DAA+D;AAC/D,MAAM,UAAU,qBAAqB;IACnC,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type { UnifiedProviderConfig, ByokVendor, OnDeviceBackend } from "./provider-mode.js";
|
|
2
|
+
/**
|
|
3
|
+
* Google's OpenAI-compatible chat completions endpoint. Used when the user
|
|
4
|
+
* picks Google as a BYOK vendor — Google is dispatched as OpenAI-compat
|
|
5
|
+
* because every cloud client (web, mobile, desktop, cli) already understands
|
|
6
|
+
* the OpenAI wire protocol. Single source of truth.
|
|
7
|
+
*/
|
|
8
|
+
export declare const GOOGLE_OPENAI_COMPAT_URL = "https://generativelanguage.googleapis.com/v1beta/openai";
|
|
9
|
+
/** Canonical Anthropic API base URL. */
|
|
10
|
+
export declare const ANTHROPIC_CANONICAL_URL = "https://api.anthropic.com";
|
|
11
|
+
/** Canonical OpenAI API base URL. */
|
|
12
|
+
export declare const OPENAI_CANONICAL_URL = "https://api.openai.com/v1";
|
|
13
|
+
/** Default Motebit Cloud relay URL. Surfaces may override via env. */
|
|
14
|
+
export declare const DEFAULT_MOTEBIT_CLOUD_URL = "https://api.motebit.com";
|
|
15
|
+
/** Default WebLLM model when none specified — small enough to fit on most devices. */
|
|
16
|
+
export declare const DEFAULT_WEBLLM_MODEL = "Llama-3.2-3B-Instruct-q4f16_1-MLC";
|
|
17
|
+
/**
|
|
18
|
+
* Normalize a local-inference server URL so that it points at the
|
|
19
|
+
* OpenAI-compatible chat completions base path. Auto-appends `/v1` if
|
|
20
|
+
* the URL doesn't already contain it. Idempotent.
|
|
21
|
+
*
|
|
22
|
+
* Every supported local server (Ollama via its OpenAI shim, LM Studio,
|
|
23
|
+
* llama.cpp, Jan, vLLM, text-generation-webui) exposes
|
|
24
|
+
* `{base}/v1/chat/completions`. Users typically type just the host
|
|
25
|
+
* (`http://localhost:11434`) — this helper completes the path so the
|
|
26
|
+
* client can call `{normalized}/chat/completions` directly.
|
|
27
|
+
*
|
|
28
|
+
* Examples:
|
|
29
|
+
* "http://localhost:11434" → "http://localhost:11434/v1"
|
|
30
|
+
* "http://localhost:11434/v1" → "http://localhost:11434/v1"
|
|
31
|
+
* "http://localhost:1234/" → "http://localhost:1234/v1"
|
|
32
|
+
* "http://192.168.1.42:11434" → "http://192.168.1.42:11434/v1"
|
|
33
|
+
* "/api/ollama" → "/api/ollama/v1" (dev proxy path)
|
|
34
|
+
*/
|
|
35
|
+
export declare function normalizeLocalServerEndpoint(url: string): string;
|
|
36
|
+
/** Default chat model for a BYOK vendor. */
|
|
37
|
+
export declare function defaultModelForVendor(vendor: ByokVendor): string;
|
|
38
|
+
/**
|
|
39
|
+
* Canonical (vendor-published) base URL for a BYOK vendor. Surfaces that
|
|
40
|
+
* need to substitute a CORS proxy or dev-mode path receive this canonical
|
|
41
|
+
* URL via the resolver and may rewrite it.
|
|
42
|
+
*/
|
|
43
|
+
export declare function canonicalVendorBaseUrl(vendor: ByokVendor): string;
|
|
44
|
+
/**
|
|
45
|
+
* Discriminated union describing what concrete provider class the surface
|
|
46
|
+
* should instantiate, with all decision logic already resolved. The surface's
|
|
47
|
+
* job is reduced to a transport switch + class construction.
|
|
48
|
+
*/
|
|
49
|
+
export type ProviderSpec = CloudProviderSpec | WebLLMProviderSpec | AppleFoundationModelsSpec | MlxProviderSpec;
|
|
50
|
+
/**
|
|
51
|
+
* Cloud HTTP provider — used for Anthropic, OpenAI, Google (via OpenAI-compat),
|
|
52
|
+
* Motebit Cloud (anthropic protocol via relay), and OpenAI-compat local
|
|
53
|
+
* servers (LM Studio, llama.cpp, etc.).
|
|
54
|
+
*
|
|
55
|
+
* The `wireProtocol` discriminates between Anthropic's messages API and
|
|
56
|
+
* OpenAI's chat completions API. The surface uses this to pick the right
|
|
57
|
+
* concrete class (`AnthropicProvider` or `OpenAIProvider`) to instantiate.
|
|
58
|
+
*/
|
|
59
|
+
export interface CloudProviderSpec {
|
|
60
|
+
kind: "cloud";
|
|
61
|
+
/** Wire protocol family this client speaks. */
|
|
62
|
+
wireProtocol: "anthropic" | "openai";
|
|
63
|
+
/** API key to use. Empty string for motebit-cloud (server injects). */
|
|
64
|
+
apiKey: string;
|
|
65
|
+
model: string;
|
|
66
|
+
/** Already-resolved base URL — env.cloudBaseUrl has been applied. */
|
|
67
|
+
baseUrl: string;
|
|
68
|
+
maxTokens?: number;
|
|
69
|
+
temperature?: number;
|
|
70
|
+
/** Extra HTTP headers (e.g., x-proxy-token for motebit-cloud). */
|
|
71
|
+
extraHeaders?: Record<string, string>;
|
|
72
|
+
}
|
|
73
|
+
/** Browser-only WebLLM provider — runs in-browser via WebGPU. */
|
|
74
|
+
export interface WebLLMProviderSpec {
|
|
75
|
+
kind: "webllm";
|
|
76
|
+
model: string;
|
|
77
|
+
maxTokens?: number;
|
|
78
|
+
temperature?: number;
|
|
79
|
+
}
|
|
80
|
+
/** Apple Foundation Models — iOS 26+ / macOS 26+ only. */
|
|
81
|
+
export interface AppleFoundationModelsSpec {
|
|
82
|
+
kind: "apple-fm";
|
|
83
|
+
model?: string;
|
|
84
|
+
maxTokens?: number;
|
|
85
|
+
}
|
|
86
|
+
/** MLX runtime — Apple Silicon only. */
|
|
87
|
+
export interface MlxProviderSpec {
|
|
88
|
+
kind: "mlx";
|
|
89
|
+
model?: string;
|
|
90
|
+
maxTokens?: number;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Per-surface environment that captures the only things the resolver can't
|
|
94
|
+
* decide on its own: platform-specific URL routing, which on-device backends
|
|
95
|
+
* are physically supported, and motebit-cloud session state.
|
|
96
|
+
*
|
|
97
|
+
* Each surface constructs one of these once (or per call if the session
|
|
98
|
+
* state changes) and passes it to `resolveProviderSpec`. The interface is
|
|
99
|
+
* intentionally narrow — anything that can be computed from a
|
|
100
|
+
* `UnifiedProviderConfig` alone stays inside the resolver.
|
|
101
|
+
*/
|
|
102
|
+
export interface ResolverEnv {
|
|
103
|
+
/**
|
|
104
|
+
* Resolve the actual base URL for a cloud provider given its canonical
|
|
105
|
+
* vendor URL. Most surfaces return `canonical` unchanged. Browser surfaces
|
|
106
|
+
* substitute a CORS proxy for vendors that block direct browser calls.
|
|
107
|
+
* Tauri dev mode substitutes a Vite proxy path.
|
|
108
|
+
*
|
|
109
|
+
* The function form (rather than a static map) lets surfaces compute the
|
|
110
|
+
* substitution lazily based on runtime state (e.g., `isTauri`).
|
|
111
|
+
*/
|
|
112
|
+
cloudBaseUrl(wireProtocol: "anthropic" | "openai", canonical: string): string;
|
|
113
|
+
/**
|
|
114
|
+
* Default URL for the on-device local-server backend when the user
|
|
115
|
+
* hasn't supplied an endpoint. The resolver auto-appends `/v1` via
|
|
116
|
+
* `normalizeLocalServerEndpoint`, so this can be a bare host. Use
|
|
117
|
+
* `localServerBaseUrl` to translate the user-supplied URL into a
|
|
118
|
+
* surface-specific transport URL (e.g., a Vite dev proxy path).
|
|
119
|
+
*/
|
|
120
|
+
defaultLocalServerUrl: string;
|
|
121
|
+
/**
|
|
122
|
+
* Optional translator from a logical local-server URL into the actual URL
|
|
123
|
+
* the provider should call. Most surfaces return the input unchanged.
|
|
124
|
+
* Desktop dev mode substitutes the Vite proxy path `/api/ollama` for any
|
|
125
|
+
* Ollama-shaped logical URL — but the dispatch decision (Ollama-native vs
|
|
126
|
+
* OpenAI-compat) is made BEFORE this substitution, on the logical URL.
|
|
127
|
+
*
|
|
128
|
+
* This separation lets surfaces use proxy/rewrite paths without losing
|
|
129
|
+
* the resolver's ability to dispatch on the user's intent.
|
|
130
|
+
*/
|
|
131
|
+
localServerBaseUrl?(logical: string): string;
|
|
132
|
+
/**
|
|
133
|
+
* Which on-device backends this surface can physically run.
|
|
134
|
+
* - web: webllm, local-server
|
|
135
|
+
* - mobile: apple-fm, mlx, local-server
|
|
136
|
+
* - desktop: local-server (today; could grow)
|
|
137
|
+
* - cli: local-server
|
|
138
|
+
*/
|
|
139
|
+
supportedBackends: ReadonlySet<OnDeviceBackend>;
|
|
140
|
+
/**
|
|
141
|
+
* Base URL for motebit-cloud requests. Each surface resolves this from
|
|
142
|
+
* its own session state / env (proxy session, VITE_PROXY_URL, etc.).
|
|
143
|
+
* Falls back to `DEFAULT_MOTEBIT_CLOUD_URL` if absent.
|
|
144
|
+
*/
|
|
145
|
+
motebitCloudBaseUrl?: string;
|
|
146
|
+
/**
|
|
147
|
+
* Extra HTTP headers to attach to motebit-cloud requests. Typically
|
|
148
|
+
* `{ "x-proxy-token": "..." }` from the surface's proxy session state.
|
|
149
|
+
*/
|
|
150
|
+
motebitCloudHeaders?: Record<string, string>;
|
|
151
|
+
/**
|
|
152
|
+
* Default model for motebit-cloud when the config doesn't specify.
|
|
153
|
+
* Most surfaces leave this undefined (resolver falls back to
|
|
154
|
+
* `DEFAULT_PROXY_MODEL`); the relay session may override based on tier.
|
|
155
|
+
*/
|
|
156
|
+
motebitCloudDefaultModel?: string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Thrown when the user picks an on-device backend that the current surface
|
|
160
|
+
* doesn't physically support (e.g., apple-fm on web). Surfaces should
|
|
161
|
+
* present a graceful error rather than letting it propagate.
|
|
162
|
+
*/
|
|
163
|
+
export declare class UnsupportedBackendError extends Error {
|
|
164
|
+
backend: OnDeviceBackend;
|
|
165
|
+
constructor(backend: OnDeviceBackend);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Resolve a `UnifiedProviderConfig` (the user's choice) against a surface's
|
|
169
|
+
* `ResolverEnv` (its physical capabilities) to produce a normalized
|
|
170
|
+
* `ProviderSpec` (what the surface should instantiate).
|
|
171
|
+
*
|
|
172
|
+
* Pure function. No I/O. No side effects. Tested once, used everywhere.
|
|
173
|
+
*
|
|
174
|
+
* @throws {UnsupportedBackendError} if the chosen on-device backend isn't
|
|
175
|
+
* in `env.supportedBackends`. Surfaces should catch and present a
|
|
176
|
+
* user-facing message.
|
|
177
|
+
*/
|
|
178
|
+
export declare function resolveProviderSpec(config: UnifiedProviderConfig, env: ResolverEnv): ProviderSpec;
|
|
179
|
+
//# sourceMappingURL=provider-resolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-resolver.d.ts","sourceRoot":"","sources":["../src/provider-resolver.ts"],"names":[],"mappings":"AA2CA,OAAO,KAAK,EAAE,qBAAqB,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAI7F;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,4DAA4D,CAAC;AAElG,wCAAwC;AACxC,eAAO,MAAM,uBAAuB,8BAA8B,CAAC;AAEnE,qCAAqC;AACrC,eAAO,MAAM,oBAAoB,8BAA8B,CAAC;AAEhE,sEAAsE;AACtE,eAAO,MAAM,yBAAyB,4BAA4B,CAAC;AAEnE,sFAAsF;AACtF,eAAO,MAAM,oBAAoB,sCAAsC,CAAC;AAIxE;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAKhE;AAED,4CAA4C;AAC5C,wBAAgB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,EAAE,MAAM,GAAG,MAAM,CAAC;IAE9E;;;;;;OAMG;IACH,qBAAqB,EAAE,MAAM,CAAC;IAE9B;;;;;;;;;OASG;IACH,kBAAkB,CAAC,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IAE7C;;;;;;OAMG;IACH,iBAAiB,EAAE,WAAW,CAAC,eAAe,CAAC,CAAC;IAEhD;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAE7B;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE7C;;;;OAIG;IACH,wBAAwB,CAAC,EAAE,MAAM,CAAC;CACnC;AAID;;;;GAIG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;IAC7B,OAAO,EAAE,eAAe;gBAAxB,OAAO,EAAE,eAAe;CAI5C;AAID;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,qBAAqB,EAAE,GAAG,EAAE,WAAW,GAAG,YAAY,CAgGjG"}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// === Provider Resolver — pure dispatch logic, shared across all surfaces ===
|
|
2
|
+
//
|
|
3
|
+
// The four surfaces (web, mobile, desktop, cli) used to each maintain their
|
|
4
|
+
// own copy of "given a UnifiedProviderConfig, build a concrete @motebit/ai-core
|
|
5
|
+
// provider instance." Four implementations of the same decision tree, with
|
|
6
|
+
// only one (web) actually containing the Ollama-vs-OpenAI-compat dispatch
|
|
7
|
+
// heuristic. Adding a vendor meant editing four files. Fixing a bug meant
|
|
8
|
+
// fixing it four times — and historically, only the surface where the bug
|
|
9
|
+
// was reported actually got the fix.
|
|
10
|
+
//
|
|
11
|
+
// This module extracts the decision tree as a pure function. It takes a
|
|
12
|
+
// `UnifiedProviderConfig` and a surface-supplied `ResolverEnv` (which captures
|
|
13
|
+
// the only legitimately-divergent concerns: platform URL routing, available
|
|
14
|
+
// on-device backends, motebit-cloud session state). It returns a `ProviderSpec`
|
|
15
|
+
// — a normalized data shape describing what kind of provider to instantiate
|
|
16
|
+
// with what config. The surface still constructs the concrete class itself
|
|
17
|
+
// (because @motebit/sdk is Layer 0 and can't import @motebit/ai-core's
|
|
18
|
+
// provider classes), but the construction shrinks to a 15-line transport
|
|
19
|
+
// switch with no decision logic.
|
|
20
|
+
//
|
|
21
|
+
// Adding a new vendor: extend ByokVendor, add a case to resolveProviderSpec.
|
|
22
|
+
// Fixing a dispatch bug: fix it once here. Tested in one place.
|
|
23
|
+
//
|
|
24
|
+
// What stays per-surface (and rightly so):
|
|
25
|
+
// - Constructing the actual @motebit/ai-core provider classes
|
|
26
|
+
// - Async init flows (WebLLM model download, native module loading)
|
|
27
|
+
// - API key storage (keyring vs SecureStore vs env vars)
|
|
28
|
+
// - URL substitution that depends on platform (Vite proxy paths, Tauri vs dev)
|
|
29
|
+
//
|
|
30
|
+
// What moves here (the duplication):
|
|
31
|
+
// - The mode → vendor → transport decision tree
|
|
32
|
+
// - The Ollama-vs-OpenAI-compat endpoint heuristic
|
|
33
|
+
// - The Google "uses OpenAI-compat" fact
|
|
34
|
+
// - Default model fallback per vendor
|
|
35
|
+
// - The supported-backends gate
|
|
36
|
+
import { DEFAULT_ANTHROPIC_MODEL, DEFAULT_OPENAI_MODEL, DEFAULT_GOOGLE_MODEL, DEFAULT_OLLAMA_MODEL, DEFAULT_PROXY_MODEL, } from "./models.js";
|
|
37
|
+
// === Constants ===
|
|
38
|
+
/**
|
|
39
|
+
* Google's OpenAI-compatible chat completions endpoint. Used when the user
|
|
40
|
+
* picks Google as a BYOK vendor — Google is dispatched as OpenAI-compat
|
|
41
|
+
* because every cloud client (web, mobile, desktop, cli) already understands
|
|
42
|
+
* the OpenAI wire protocol. Single source of truth.
|
|
43
|
+
*/
|
|
44
|
+
export const GOOGLE_OPENAI_COMPAT_URL = "https://generativelanguage.googleapis.com/v1beta/openai";
|
|
45
|
+
/** Canonical Anthropic API base URL. */
|
|
46
|
+
export const ANTHROPIC_CANONICAL_URL = "https://api.anthropic.com";
|
|
47
|
+
/** Canonical OpenAI API base URL. */
|
|
48
|
+
export const OPENAI_CANONICAL_URL = "https://api.openai.com/v1";
|
|
49
|
+
/** Default Motebit Cloud relay URL. Surfaces may override via env. */
|
|
50
|
+
export const DEFAULT_MOTEBIT_CLOUD_URL = "https://api.motebit.com";
|
|
51
|
+
/** Default WebLLM model when none specified — small enough to fit on most devices. */
|
|
52
|
+
export const DEFAULT_WEBLLM_MODEL = "Llama-3.2-3B-Instruct-q4f16_1-MLC";
|
|
53
|
+
// === Helpers ===
|
|
54
|
+
/**
|
|
55
|
+
* Normalize a local-inference server URL so that it points at the
|
|
56
|
+
* OpenAI-compatible chat completions base path. Auto-appends `/v1` if
|
|
57
|
+
* the URL doesn't already contain it. Idempotent.
|
|
58
|
+
*
|
|
59
|
+
* Every supported local server (Ollama via its OpenAI shim, LM Studio,
|
|
60
|
+
* llama.cpp, Jan, vLLM, text-generation-webui) exposes
|
|
61
|
+
* `{base}/v1/chat/completions`. Users typically type just the host
|
|
62
|
+
* (`http://localhost:11434`) — this helper completes the path so the
|
|
63
|
+
* client can call `{normalized}/chat/completions` directly.
|
|
64
|
+
*
|
|
65
|
+
* Examples:
|
|
66
|
+
* "http://localhost:11434" → "http://localhost:11434/v1"
|
|
67
|
+
* "http://localhost:11434/v1" → "http://localhost:11434/v1"
|
|
68
|
+
* "http://localhost:1234/" → "http://localhost:1234/v1"
|
|
69
|
+
* "http://192.168.1.42:11434" → "http://192.168.1.42:11434/v1"
|
|
70
|
+
* "/api/ollama" → "/api/ollama/v1" (dev proxy path)
|
|
71
|
+
*/
|
|
72
|
+
export function normalizeLocalServerEndpoint(url) {
|
|
73
|
+
const stripped = url.replace(/\/+$/, "");
|
|
74
|
+
// Already contains a /v1 path segment? Leave it alone.
|
|
75
|
+
if (/\/v1(\/|$)/.test(stripped))
|
|
76
|
+
return stripped;
|
|
77
|
+
return `${stripped}/v1`;
|
|
78
|
+
}
|
|
79
|
+
/** Default chat model for a BYOK vendor. */
|
|
80
|
+
export function defaultModelForVendor(vendor) {
|
|
81
|
+
switch (vendor) {
|
|
82
|
+
case "anthropic":
|
|
83
|
+
return DEFAULT_ANTHROPIC_MODEL;
|
|
84
|
+
case "openai":
|
|
85
|
+
return DEFAULT_OPENAI_MODEL;
|
|
86
|
+
case "google":
|
|
87
|
+
return DEFAULT_GOOGLE_MODEL;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Canonical (vendor-published) base URL for a BYOK vendor. Surfaces that
|
|
92
|
+
* need to substitute a CORS proxy or dev-mode path receive this canonical
|
|
93
|
+
* URL via the resolver and may rewrite it.
|
|
94
|
+
*/
|
|
95
|
+
export function canonicalVendorBaseUrl(vendor) {
|
|
96
|
+
switch (vendor) {
|
|
97
|
+
case "anthropic":
|
|
98
|
+
return ANTHROPIC_CANONICAL_URL;
|
|
99
|
+
case "openai":
|
|
100
|
+
return OPENAI_CANONICAL_URL;
|
|
101
|
+
case "google":
|
|
102
|
+
return GOOGLE_OPENAI_COMPAT_URL;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// === Errors ===
|
|
106
|
+
/**
|
|
107
|
+
* Thrown when the user picks an on-device backend that the current surface
|
|
108
|
+
* doesn't physically support (e.g., apple-fm on web). Surfaces should
|
|
109
|
+
* present a graceful error rather than letting it propagate.
|
|
110
|
+
*/
|
|
111
|
+
export class UnsupportedBackendError extends Error {
|
|
112
|
+
backend;
|
|
113
|
+
constructor(backend) {
|
|
114
|
+
super(`On-device backend "${backend}" is not supported on this surface`);
|
|
115
|
+
this.backend = backend;
|
|
116
|
+
this.name = "UnsupportedBackendError";
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// === The resolver ===
|
|
120
|
+
/**
|
|
121
|
+
* Resolve a `UnifiedProviderConfig` (the user's choice) against a surface's
|
|
122
|
+
* `ResolverEnv` (its physical capabilities) to produce a normalized
|
|
123
|
+
* `ProviderSpec` (what the surface should instantiate).
|
|
124
|
+
*
|
|
125
|
+
* Pure function. No I/O. No side effects. Tested once, used everywhere.
|
|
126
|
+
*
|
|
127
|
+
* @throws {UnsupportedBackendError} if the chosen on-device backend isn't
|
|
128
|
+
* in `env.supportedBackends`. Surfaces should catch and present a
|
|
129
|
+
* user-facing message.
|
|
130
|
+
*/
|
|
131
|
+
export function resolveProviderSpec(config, env) {
|
|
132
|
+
switch (config.mode) {
|
|
133
|
+
case "motebit-cloud": {
|
|
134
|
+
// Motebit Cloud speaks the Anthropic wire protocol via the relay.
|
|
135
|
+
// The relay injects the real key server-side, so the spec carries
|
|
136
|
+
// an empty apiKey — every cloud client knows to handle that case.
|
|
137
|
+
const baseUrl = config.baseUrl ?? env.motebitCloudBaseUrl ?? DEFAULT_MOTEBIT_CLOUD_URL;
|
|
138
|
+
const extraHeaders = config.proxyToken !== undefined
|
|
139
|
+
? { "x-proxy-token": config.proxyToken, ...(env.motebitCloudHeaders ?? {}) }
|
|
140
|
+
: env.motebitCloudHeaders;
|
|
141
|
+
return {
|
|
142
|
+
kind: "cloud",
|
|
143
|
+
wireProtocol: "anthropic",
|
|
144
|
+
apiKey: "",
|
|
145
|
+
model: config.model ?? env.motebitCloudDefaultModel ?? DEFAULT_PROXY_MODEL,
|
|
146
|
+
baseUrl,
|
|
147
|
+
maxTokens: config.maxTokens,
|
|
148
|
+
temperature: config.temperature,
|
|
149
|
+
extraHeaders,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
case "byok": {
|
|
153
|
+
// Google is dispatched as OpenAI-compat — its public API is OpenAI-
|
|
154
|
+
// compatible at the canonical Google URL. Anthropic and OpenAI use
|
|
155
|
+
// their own canonical wire protocols.
|
|
156
|
+
const wireProtocol = config.vendor === "anthropic" ? "anthropic" : "openai";
|
|
157
|
+
const canonical = config.baseUrl ?? canonicalVendorBaseUrl(config.vendor);
|
|
158
|
+
return {
|
|
159
|
+
kind: "cloud",
|
|
160
|
+
wireProtocol,
|
|
161
|
+
apiKey: config.apiKey,
|
|
162
|
+
model: config.model ?? defaultModelForVendor(config.vendor),
|
|
163
|
+
baseUrl: env.cloudBaseUrl(wireProtocol, canonical),
|
|
164
|
+
maxTokens: config.maxTokens,
|
|
165
|
+
temperature: config.temperature,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
case "on-device": {
|
|
169
|
+
if (!env.supportedBackends.has(config.backend)) {
|
|
170
|
+
throw new UnsupportedBackendError(config.backend);
|
|
171
|
+
}
|
|
172
|
+
switch (config.backend) {
|
|
173
|
+
case "webllm":
|
|
174
|
+
return {
|
|
175
|
+
kind: "webllm",
|
|
176
|
+
model: config.model ?? DEFAULT_WEBLLM_MODEL,
|
|
177
|
+
maxTokens: config.maxTokens,
|
|
178
|
+
temperature: config.temperature,
|
|
179
|
+
};
|
|
180
|
+
case "apple-fm":
|
|
181
|
+
return {
|
|
182
|
+
kind: "apple-fm",
|
|
183
|
+
model: config.model,
|
|
184
|
+
maxTokens: config.maxTokens,
|
|
185
|
+
};
|
|
186
|
+
case "mlx":
|
|
187
|
+
return {
|
|
188
|
+
kind: "mlx",
|
|
189
|
+
model: config.model,
|
|
190
|
+
maxTokens: config.maxTokens,
|
|
191
|
+
};
|
|
192
|
+
case "local-server": {
|
|
193
|
+
// Every supported local inference server (Ollama, LM Studio,
|
|
194
|
+
// llama.cpp, Jan, vLLM, text-generation-webui, …) exposes an
|
|
195
|
+
// OpenAI-compatible chat completions endpoint at /v1. We dispatch
|
|
196
|
+
// them all through the OpenAI wire protocol with a single client
|
|
197
|
+
// (`OpenAIProvider`). The previous Ollama-vs-OpenAI port heuristic
|
|
198
|
+
// is gone — there's only one code path now.
|
|
199
|
+
//
|
|
200
|
+
// `normalizeLocalServerEndpoint` auto-appends `/v1` so users who
|
|
201
|
+
// type the bare host (`http://localhost:11434`) still work. Users
|
|
202
|
+
// who type the full path (`http://localhost:11434/v1`) are
|
|
203
|
+
// unaffected. Surfaces may further substitute the URL via
|
|
204
|
+
// `localServerBaseUrl` (e.g. desktop dev mode → `/api/ollama`).
|
|
205
|
+
const logicalEndpoint = config.endpoint ?? env.defaultLocalServerUrl;
|
|
206
|
+
const surfaceTransformed = env.localServerBaseUrl?.(logicalEndpoint) ?? logicalEndpoint;
|
|
207
|
+
const actualEndpoint = normalizeLocalServerEndpoint(surfaceTransformed);
|
|
208
|
+
return {
|
|
209
|
+
kind: "cloud",
|
|
210
|
+
wireProtocol: "openai",
|
|
211
|
+
// Local OpenAI-compat servers don't validate the API key, but
|
|
212
|
+
// most clients require a non-empty string. Use a sentinel.
|
|
213
|
+
apiKey: "local",
|
|
214
|
+
model: config.model ?? DEFAULT_OLLAMA_MODEL,
|
|
215
|
+
baseUrl: actualEndpoint,
|
|
216
|
+
maxTokens: config.maxTokens,
|
|
217
|
+
temperature: config.temperature,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
//# sourceMappingURL=provider-resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-resolver.js","sourceRoot":"","sources":["../src/provider-resolver.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,EAAE;AACF,4EAA4E;AAC5E,gFAAgF;AAChF,2EAA2E;AAC3E,0EAA0E;AAC1E,0EAA0E;AAC1E,0EAA0E;AAC1E,qCAAqC;AACrC,EAAE;AACF,wEAAwE;AACxE,+EAA+E;AAC/E,4EAA4E;AAC5E,gFAAgF;AAChF,4EAA4E;AAC5E,2EAA2E;AAC3E,uEAAuE;AACvE,yEAAyE;AACzE,iCAAiC;AACjC,EAAE;AACF,6EAA6E;AAC7E,gEAAgE;AAChE,EAAE;AACF,2CAA2C;AAC3C,gEAAgE;AAChE,sEAAsE;AACtE,2DAA2D;AAC3D,iFAAiF;AACjF,EAAE;AACF,qCAAqC;AACrC,kDAAkD;AAClD,qDAAqD;AACrD,2CAA2C;AAC3C,wCAAwC;AACxC,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AAC;oBACpB,6DAA6D;oBAC7D,6DAA6D;oBAC7D,kEAAkE;oBAClE,iEAAiE;oBACjE,mEAAmE;oBACnE,4CAA4C;oBAC5C,EAAE;oBACF,iEAAiE;oBACjE,kEAAkE;oBAClE,2DAA2D;oBAC3D,0DAA0D;oBAC1D,gEAAgE;oBAChE,MAAM,eAAe,GAAG,MAAM,CAAC,QAAQ,IAAI,GAAG,CAAC,qBAAqB,CAAC;oBACrE,MAAM,kBAAkB,GAAG,GAAG,CAAC,kBAAkB,EAAE,CAAC,eAAe,CAAC,IAAI,eAAe,CAAC;oBACxF,MAAM,cAAc,GAAG,4BAA4B,CAAC,kBAAkB,CAAC,CAAC;oBACxE,OAAO;wBACL,IAAI,EAAE,OAAO;wBACb,YAAY,EAAE,QAAQ;wBACtB,8DAA8D;wBAC9D,2DAA2D;wBAC3D,MAAM,EAAE,OAAO;wBACf,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,oBAAoB;wBAC3C,OAAO,EAAE,cAAc;wBACvB,SAAS,EAAE,MAAM,CAAC,SAAS;wBAC3B,WAAW,EAAE,MAAM,CAAC,WAAW;qBAChC,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical risk level labels for tool governance.
|
|
3
|
+
*
|
|
4
|
+
* The five risk levels are the same across every surface and every
|
|
5
|
+
* storage system — they're the axes the `PolicyGate` scores tools
|
|
6
|
+
* against, and they're what the user picks a preset threshold for.
|
|
7
|
+
* The label strings ("R0 Read", "R1 Draft", …) were drifting across
|
|
8
|
+
* four different files with four different presentation shapes
|
|
9
|
+
* (Record<number, string>, { label, cls }, { label, color, bg }).
|
|
10
|
+
* This module is the authoritative source for the semantic labels.
|
|
11
|
+
*
|
|
12
|
+
* Surfaces still own their own presentation — the CSS class in
|
|
13
|
+
* desktop's chat badge, the color swatches in the mobile approval
|
|
14
|
+
* card — but the label string comes from here.
|
|
15
|
+
*
|
|
16
|
+
* The order is the same as `PolicyGate`'s risk scoring: 0 is
|
|
17
|
+
* read-only, 4 is "touches money." A tool classified above its
|
|
18
|
+
* surface's `requireApprovalAbove` gets a modal; above `denyAbove`
|
|
19
|
+
* gets rejected.
|
|
20
|
+
*/
|
|
21
|
+
export declare const RISK_LABELS: Record<number, string>;
|
|
22
|
+
//# sourceMappingURL=risk-labels.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"risk-labels.d.ts","sourceRoot":"","sources":["../src/risk-labels.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,eAAO,MAAM,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAM9C,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical risk level labels for tool governance.
|
|
3
|
+
*
|
|
4
|
+
* The five risk levels are the same across every surface and every
|
|
5
|
+
* storage system — they're the axes the `PolicyGate` scores tools
|
|
6
|
+
* against, and they're what the user picks a preset threshold for.
|
|
7
|
+
* The label strings ("R0 Read", "R1 Draft", …) were drifting across
|
|
8
|
+
* four different files with four different presentation shapes
|
|
9
|
+
* (Record<number, string>, { label, cls }, { label, color, bg }).
|
|
10
|
+
* This module is the authoritative source for the semantic labels.
|
|
11
|
+
*
|
|
12
|
+
* Surfaces still own their own presentation — the CSS class in
|
|
13
|
+
* desktop's chat badge, the color swatches in the mobile approval
|
|
14
|
+
* card — but the label string comes from here.
|
|
15
|
+
*
|
|
16
|
+
* The order is the same as `PolicyGate`'s risk scoring: 0 is
|
|
17
|
+
* read-only, 4 is "touches money." A tool classified above its
|
|
18
|
+
* surface's `requireApprovalAbove` gets a modal; above `denyAbove`
|
|
19
|
+
* gets rejected.
|
|
20
|
+
*/
|
|
21
|
+
export const RISK_LABELS = {
|
|
22
|
+
0: "R0 Read",
|
|
23
|
+
1: "R1 Draft",
|
|
24
|
+
2: "R2 Write",
|
|
25
|
+
3: "R3 Execute",
|
|
26
|
+
4: "R4 Money",
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=risk-labels.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"risk-labels.js","sourceRoot":"","sources":["../src/risk-labels.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,MAAM,CAAC,MAAM,WAAW,GAA2B;IACjD,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,UAAU;IACb,CAAC,EAAE,UAAU;IACb,CAAC,EAAE,YAAY;IACf,CAAC,EAAE,UAAU;CACd,CAAC"}
|