Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
399 lines (362 loc) · 13.9 KB
/
Copy pathindex.ts
File metadata and controls
399 lines (362 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* Muse model provider plugin for OpenClaw, billed to the Muse Code
* monthly subscription instead of API credits.
*
* Authentication is a Meta device-code login. OpenClaw stores the resulting
* credential as an auth profile; a copy is cached for existing installations.
*
* Wire: Meta Model API (`https://api.meta.ai/v1`) over the Responses API,
* same as the official `@openclaw/meta-provider` (which is API-key billed).
* This plugin exists for subscription billing; provider id `muse-code` is
* intentionally distinct from the official `meta` id so both can coexist.
*/
import {
defineSingleProviderPluginEntry,
} from "openclaw/plugin-sdk/provider-entry";
import type {
ModelApi,
ModelDefinitionConfig,
ModelProviderConfig,
} from "openclaw/plugin-sdk/provider-model-types";
import type { ProviderAuthContext, ProviderAuthMethod, ProviderRuntimeModel } from "openclaw/plugin-sdk/plugin-entry";
import { ENV_VAR, defaultCachePath } from "./src/config.js";
import { deviceAuthorize, pollToken, mintKey, writeCache } from "./src/auth.js";
import { getLiveModelIdsCached, resolveKey } from "./src/catalog.js";
import { syncEnvFromCache } from "./src/key.js";
import { museCodeBaselineModels } from "./src/baseline.models.js";
import { startMuseCodeAdapter, type RunningAdapter } from "./src/adapter.js";
// OpenClaw resolves the provider key from MUSE_CODE_SUB_TOKEN. Keep that env
// value aligned with the login cache without overwriting an explicit value.
syncEnvFromCache();
/** Meta Model API base URL for Responses API traffic. */
export const META_BASE_URL = "https://api.meta.ai/v1";
/**
* Env switch that keeps the loopback proxy but relays requests unchanged.
*
* The provider keeps pointing at the adapter so a base URL observed by the
* host stays valid when the wire rules are switched off.
*/
export const ADAPTER_ENV_VAR = "MUSE_CODE_ADAPTER";
/** Adapter surface the plugin needs; tests stub it without binding a port. */
export type MuseCodeAdapterHandle = Pick<RunningAdapter, "baseUrl" | "close">;
/**
* Starts the loopback adapter. `passthrough` keeps the proxy in place while
* disabling the wire rules (`MUSE_CODE_ADAPTER=off`).
*/
export type MuseCodeAdapterFactory = (options: {
passthrough: boolean;
log: (message: string) => void;
}) => Promise<MuseCodeAdapterHandle>;
/** Minimal logger surface used for adapter diagnostics. */
type AdapterLogger = {
debug?: (message: string) => void;
warn?: (message: string) => void;
};
const defaultAdapterFactory: MuseCodeAdapterFactory = (options) =>
startMuseCodeAdapter({ ...options, minOutputTokens: 1024 });
let adapterFactory: MuseCodeAdapterFactory = defaultAdapterFactory;
let adapterLogger: AdapterLogger | undefined;
let adapterStart: Promise<MuseCodeAdapterHandle | undefined> | undefined;
let adapterBaseUrl: string | undefined;
function adapterPassthrough(): boolean {
return (process.env[ADAPTER_ENV_VAR] ?? "").trim().toLowerCase() === "off";
}
/**
* Start the request adapter once per process.
*
* Meta's subscription gate rejects flat client tool declarations that collide
* with Muse Code's builtin tool names (HTTP 403, `client_attestation_denied`).
* The adapter aliases exactly those names on the wire and restores them in the
* answer, so the host keeps its own tools. A bind failure leaves the provider
* on the direct endpoint, which is the behavior before this plugin version.
*/
export function ensureMuseCodeAdapter(logger?: AdapterLogger): Promise<MuseCodeAdapterHandle | undefined> {
if (logger !== undefined) adapterLogger = logger;
adapterStart ??= adapterFactory({
passthrough: adapterPassthrough(),
log: (message: string) => adapterLogger?.debug?.(message),
}).then(
(adapter) => {
adapterBaseUrl = adapter.baseUrl;
return adapter;
},
(error: unknown) => {
const detail = error instanceof Error ? error.message : String(error);
adapterLogger?.warn?.(`muse-code: local request adapter unavailable; using ${META_BASE_URL} (${detail})`);
return undefined;
},
);
return adapterStart;
}
/** Base URL every provider and model entry must use: adapter when running, else direct. */
export function museCodeBaseUrl(): string {
return adapterBaseUrl ?? META_BASE_URL;
}
/** Stop the adapter if it started; safe to call more than once. */
export async function closeMuseCodeAdapter(): Promise<void> {
const start = adapterStart;
adapterStart = undefined;
adapterBaseUrl = undefined;
if (start === undefined) return;
const adapter = await start.catch(() => undefined);
await adapter?.close().catch(() => undefined);
}
/** Test-only: replace the adapter starter and forget the started instance. */
export function __setAdapterFactoryForTests(factory?: MuseCodeAdapterFactory): void {
adapterFactory = factory ?? defaultAdapterFactory;
adapterStart = undefined;
adapterBaseUrl = undefined;
adapterLogger = undefined;
}
async function currentBaseUrl(): Promise<string> {
await ensureMuseCodeAdapter();
return museCodeBaseUrl();
}
/**
* Known Muse Spark context window (official catalog: input + output share
* the budget). Used as the default when a row omits context_length.
*/
const MUSE_CONTEXT_WINDOW = 1_048_576;
/**
* Conservative output cap: Meta publishes no exact max-output figure, so
* stay within a sane ceiling instead of advertising the full window.
*/
const MAX_OUTPUT_TOKENS = 131_072;
/** Default model for setup flows: standard tier, never contributor. */
export const DEFAULT_MODEL_ID = "muse-spark-1.3";
/** Row shape of the static baseline. */
export type MuseCodeModelRow = {
id?: unknown;
name?: unknown;
context_length?: unknown;
};
/**
* Maps a Muse Spark baseline row to an OpenClaw model definition.
*
* Every model uses the Responses API transport. Contributor-tier variants
* keep working when selected explicitly (see README for the training-data
* disclosure); nothing in this plugin ever selects them implicitly.
*/
export function projectModel(
row: MuseCodeModelRow,
baseUrl: string = museCodeBaseUrl(),
): ModelDefinitionConfig | null {
const id = typeof row.id === "string" && row.id.length > 0 ? row.id : null;
if (!id) return null;
const rawName = row.name;
const name = typeof rawName === "string" && rawName.length > 0 ? rawName : id;
const rawCtx = row.context_length;
const contextWindow =
typeof rawCtx === "number" && rawCtx > 0 ? rawCtx : MUSE_CONTEXT_WINDOW;
const api: ModelApi = "openai-responses";
return {
id,
name,
api,
baseUrl,
reasoning: true,
// Matches the official Meta provider: text + image input. Other upstream
// modalities are not OpenClaw model-manifest input values.
input: ["text", "image"],
// Subscription billing has no per-token price; set to zero to avoid
// fabricating prices.
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow,
maxTokens: Math.min(contextWindow, MAX_OUTPUT_TOKENS),
};
}
/**
* Builds a provider config from model rows (live ids or static baseline).
* Live rows carry ids only, so names default to the id and the context
* window to the family default; inference is unaffected.
*/
export function providerFromRows(
rows: MuseCodeModelRow[],
baseUrl: string = museCodeBaseUrl(),
): ModelProviderConfig {
const models = rows
.map((row) => projectModel(row, baseUrl))
.filter((m): m is ModelDefinitionConfig => m !== null);
return {
baseUrl,
api: "openai-responses",
models,
};
}
async function buildProvider(): Promise<ModelProviderConfig> {
// The provider entry is what the host routes inference with, so it must
// carry the adapter origin (or the direct endpoint when binding failed).
const baseUrl = await currentBaseUrl();
// Live first (TTL-cached, key re-resolved per call so a cache refresh is
// picked up), static baseline as fallback. Discovery itself stays direct:
// the adapter relays every non-inference path untouched anyway.
const key = resolveKey();
if (key) {
const live = await getLiveModelIdsCached(META_BASE_URL, key).catch(() => null);
if (live && live.length > 0) {
return providerFromRows(live.map((id) => ({ id })), baseUrl);
}
}
return providerFromRows(museCodeBaselineModels, baseUrl);
}
async function buildStaticProvider(): Promise<ModelProviderConfig> {
return providerFromRows(museCodeBaselineModels, await currentBaseUrl());
}
/** Strips a leading `<provider>/` prefix from a runtime model id when present. */
function stripProviderModelPrefix(provider: string, modelId: string): string {
const prefix = `${provider}/`;
return modelId.startsWith(prefix) ? modelId.slice(prefix.length) : modelId;
}
/**
* Resolves muse-code models missing from the static baseline.
*
* Ids in the snapshot resolve with baseline fidelity; unknown ids receive
* a conservative provider-neutral definition so newly published models keep
* working without a baseline refresh.
*/
export function resolveMuseCodeDynamicModel(ctx: {
provider?: string;
modelId: string;
}): ProviderRuntimeModel | null {
const provider = ctx.provider ?? "muse-code";
const modelId = stripProviderModelPrefix(provider, ctx.modelId);
const row = museCodeBaselineModels.find((entry) => entry.id === modelId);
const projected = row ? projectModel(row) : null;
const contextWindow = projected?.contextWindow ?? MUSE_CONTEXT_WINDOW;
return {
id: modelId,
name: projected?.name ?? modelId,
api: "openai-responses",
provider,
baseUrl: museCodeBaseUrl(),
reasoning: true,
input: ["text", "image"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow,
maxTokens: Math.min(contextWindow, MAX_OUTPUT_TOKENS),
};
}
function checkAuth(ctx: ProviderAuthContext): void {
ctx.signal?.throwIfAborted();
ctx.assertCurrent?.();
}
const deviceLogin: ProviderAuthMethod = {
id: "device-code",
label: "Muse Code browser login",
hint: "Sign in with your Meta account in a browser",
kind: "device_code",
wizard: {
choiceId: "muse-code-device-code",
choiceLabel: "Muse Code browser login (unofficial)",
groupId: "muse-code",
groupLabel: "Muse Code",
methodId: "device-code",
},
async run(ctx) {
checkAuth(ctx);
if (ctx.secretInputMode === "ref") {
throw new Error("Muse Code login requires a local credential cache and does not support reference-only storage.");
}
const device = await deviceAuthorize(ctx.signal);
const url = device.verification_uri_complete || device.verification_uri;
checkAuth(ctx);
if (ctx.prompter.deviceCode) {
await ctx.prompter.deviceCode({
title: "Authorize Muse Code",
code: device.user_code,
expiresInMinutes: Math.max(1, Math.ceil(device.expires_in / 60)),
message: `Open ${url} and approve the code.`,
});
} else {
await ctx.prompter.note(`Open ${url}\nEnter code: ${device.user_code}`, "Authorize Muse Code");
}
checkAuth(ctx);
try {
await ctx.openUrl(url);
} catch {
checkAuth(ctx);
await ctx.prompter.note("The browser did not open. Use the URL shown above.", "Muse Code");
}
checkAuth(ctx);
const access = await pollToken(device.device_code, device.interval, device.expires_in, ctx.signal);
checkAuth(ctx);
const credentials = await mintKey(access, ctx.signal);
checkAuth(ctx);
await writeCache(credentials, defaultCachePath());
syncEnvFromCache();
checkAuth(ctx);
return {
profiles: [{
profileId: "muse-code:default",
credential: { type: "api_key", provider: "muse-code", key: credentials.apiKey },
...(ctx.secretInputMode === "plaintext" ? {} : {
secretStorage: { kind: "store", namePrefix: "MUSE_CODE" },
}),
}],
...(!ctx.credentialOnly ? { defaultModel: `muse-code/${DEFAULT_MODEL_ID}` } : {}),
notes: ["Model access depends on Meta's subscription policy."],
};
},
};
/** Host surface touched by the plugin entry; the SDK forwards it as unknown. */
type PluginHostApi = {
logger?: AdapterLogger;
lifecycle?: {
registerRuntimeLifecycle?: (registration: {
id: string;
dispose?: () => void | Promise<void>;
}) => void;
onDispose?: (dispose: () => void | Promise<void>) => unknown;
};
/** Deprecated flat form kept as a fallback for older hosts. */
registerRuntimeLifecycle?: (registration: {
id: string;
dispose?: () => void | Promise<void>;
}) => void;
};
/**
* Register the adapter lifecycle on the host API.
*
* Prefers the grouped `lifecycle` facade, then the deprecated flat method and
* the instance `onDispose` hook, so the loopback listener is released with the
* plugin on every host generation that exposes at least one of them.
*/
function registerAdapterDisposal(host: PluginHostApi): void {
const dispose = () => closeMuseCodeAdapter();
const registration = { id: "muse-code-request-adapter", dispose };
const lifecycle = host.lifecycle;
if (typeof lifecycle?.registerRuntimeLifecycle === "function") {
lifecycle.registerRuntimeLifecycle(registration);
return;
}
if (typeof host.registerRuntimeLifecycle === "function") {
host.registerRuntimeLifecycle(registration);
return;
}
lifecycle?.onDispose?.(dispose);
}
export default defineSingleProviderPluginEntry({
id: "muse-code",
name: "Muse Code",
description:
"Muse models (api.meta.ai) model provider billed to the Muse Code monthly subscription.",
provider: {
label: "Muse Code",
docsPath: "/providers/muse-code",
envVars: [ENV_VAR],
extraAuth: [deviceLogin],
catalog: {
buildProvider,
buildStaticProvider,
},
resolveDynamicModel: resolveMuseCodeDynamicModel,
},
register(api: unknown) {
const host = (api ?? {}) as PluginHostApi;
// Start while the provider is registered: the catalog callbacks await the
// same promise, so the entry they publish already carries the loopback
// origin (or the direct one when the port cannot be bound).
void ensureMuseCodeAdapter(host.logger);
registerAdapterDisposal(host);
},
});