-
-
Notifications
You must be signed in to change notification settings - Fork 76.1k
Expand file tree
/
Copy pathcapability-cli.ts
More file actions
2408 lines (2312 loc) · 80.7 KB
/
capability-cli.ts
File metadata and controls
2408 lines (2312 loc) · 80.7 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { createWriteStream } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import type { Command } from "commander";
import { resolveAgentDir, resolveDefaultAgentId } from "../agents/agent-scope.js";
import {
listProfilesForProvider,
loadAuthProfileStoreForRuntime,
} from "../agents/auth-profiles.js";
import { updateAuthProfileStoreWithLock } from "../agents/auth-profiles/store.js";
import { resolveMemorySearchConfig } from "../agents/memory-search.js";
import { loadModelCatalog } from "../agents/model-catalog.js";
import { splitTrailingAuthProfile } from "../agents/model-ref-profile.js";
import {
completeWithPreparedSimpleCompletionModel,
prepareSimpleCompletionModelForAgent,
} from "../agents/simple-completion-runtime.js";
import { getRuntimeConfig } from "../config/config.js";
import { resolveAgentModelPrimaryValue } from "../config/model-input.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { callGateway, randomIdempotencyKey } from "../gateway/call.js";
import { buildGatewayConnectionDetailsWithResolvers } from "../gateway/connection-details.js";
import { isLoopbackHost } from "../gateway/net.js";
import { ADMIN_SCOPE } from "../gateway/operator-scopes.js";
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../gateway/protocol/client-info.js";
import { generateImage, listRuntimeImageGenerationProviders } from "../image-generation/runtime.js";
import type {
ImageGenerationBackground,
ImageGenerationOutputFormat,
} from "../image-generation/types.js";
import { buildMediaUnderstandingRegistry } from "../media-understanding/provider-registry.js";
import type { RunMediaUnderstandingFileResult } from "../media-understanding/runtime-types.js";
import {
describeImageFile,
describeImageFileWithModel,
describeVideoFile,
transcribeAudioFile,
} from "../media-understanding/runtime.js";
import { getImageMetadata } from "../media/image-ops.js";
import { detectMime, extensionForMime, normalizeMimeType } from "../media/mime.js";
import { saveMediaBuffer } from "../media/store.js";
import {
createEmbeddingProvider,
registerBuiltInMemoryEmbeddingProviders,
} from "../plugin-sdk/memory-core-bundled-runtime.js";
import {
listMemoryEmbeddingProviders,
registerMemoryEmbeddingProvider,
} from "../plugins/memory-embedding-providers.js";
import { writeRuntimeJson, defaultRuntime, type RuntimeEnv } from "../runtime.js";
import { getProviderEnvVars } from "../secrets/provider-env-vars.js";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalString,
normalizeStringifiedOptionalString,
} from "../shared/string-coerce.js";
import { formatDocsLink } from "../terminal/links.js";
import { theme } from "../terminal/theme.js";
import { canonicalizeSpeechProviderId, listSpeechProviders } from "../tts/provider-registry.js";
import {
getTtsProvider,
getTtsPersona,
listTtsPersonas,
listSpeechVoices,
resolveExplicitTtsOverrides,
resolveTtsConfig,
resolveTtsPrefsPath,
setTtsEnabled,
setTtsPersona,
setTtsProvider,
textToSpeech,
} from "../tts/tts.js";
import { generateVideo, listRuntimeVideoGenerationProviders } from "../video-generation/runtime.js";
import type { VideoGenerationResolution } from "../video-generation/types.js";
import {
isWebFetchProviderConfigured,
resolveWebFetchDefinition,
listWebFetchProviders,
} from "../web-fetch/runtime.js";
import {
isWebSearchProviderConfigured,
listWebSearchProviders,
runWebSearch,
} from "../web-search/runtime.js";
import { runCommandWithRuntime } from "./cli-utils.js";
import { removeCommandByName } from "./program/command-tree.js";
import { collectOption } from "./program/helpers.js";
type CapabilityTransport = "local" | "gateway";
const IMAGE_OUTPUT_FORMATS = ["png", "jpeg", "webp"] as const;
const IMAGE_BACKGROUNDS = ["transparent", "opaque", "auto"] as const;
type CapabilityMetadata = {
id: string;
description: string;
transports: Array<CapabilityTransport>;
flags: string[];
resultShape: string;
};
type CapabilityEnvelope = {
ok: boolean;
capability: string;
transport: CapabilityTransport;
provider?: string;
model?: string;
attempts: Array<Record<string, unknown>>;
inputs?: Array<Record<string, unknown>>;
outputs: Array<Record<string, unknown>>;
ignoredOverrides?: Array<Record<string, unknown>>;
error?: string;
};
const CAPABILITY_METADATA: CapabilityMetadata[] = [
{
id: "model.run",
description: "Run a one-shot inference turn through the selected model provider.",
transports: ["local", "gateway"],
flags: ["--prompt", "--file", "--model", "--local", "--gateway", "--json"],
resultShape: "normalized payloads plus provider/model attribution",
},
{
id: "model.list",
description: "List known models from the model catalog.",
transports: ["local"],
flags: ["--json"],
resultShape: "catalog entries",
},
{
id: "model.inspect",
description: "Inspect one model catalog entry.",
transports: ["local"],
flags: ["--model", "--json"],
resultShape: "single catalog entry",
},
{
id: "model.providers",
description: "List model providers discovered from the catalog.",
transports: ["local"],
flags: ["--json"],
resultShape: "provider ids with counts and defaults",
},
{
id: "model.auth.login",
description: "Run the existing provider auth login flow.",
transports: ["local"],
flags: ["--provider"],
resultShape: "interactive auth result",
},
{
id: "model.auth.logout",
description: "Remove saved auth profiles for one provider.",
transports: ["local"],
flags: ["--provider", "--json"],
resultShape: "removed profile ids",
},
{
id: "model.auth.status",
description: "Show configured model auth state.",
transports: ["local"],
flags: ["--json"],
resultShape: "model status summary",
},
{
id: "image.generate",
description: "Generate raster images with configured image providers.",
transports: ["local"],
flags: [
"--prompt",
"--model",
"--count",
"--size",
"--aspect-ratio",
"--resolution",
"--output",
"--json",
],
resultShape: "saved image files plus attempts",
},
{
id: "image.edit",
description: "Generate edited images from one or more input files.",
transports: ["local"],
flags: [
"--file",
"--prompt",
"--model",
"--size",
"--aspect-ratio",
"--resolution",
"--output-format",
"--background",
"--openai-background",
"--timeout-ms",
"--output",
"--json",
],
resultShape: "saved image files plus attempts",
},
{
id: "image.describe",
description: "Describe one image file through media-understanding providers.",
transports: ["local"],
flags: ["--file", "--prompt", "--model", "--timeout-ms", "--json"],
resultShape: "normalized text output",
},
{
id: "image.describe-many",
description: "Describe multiple image files independently.",
transports: ["local"],
flags: ["--file", "--prompt", "--model", "--timeout-ms", "--json"],
resultShape: "one text output per file",
},
{
id: "image.providers",
description: "List image generation providers.",
transports: ["local"],
flags: ["--json"],
resultShape: "provider ids and defaults",
},
{
id: "audio.transcribe",
description: "Transcribe one audio file.",
transports: ["local"],
flags: ["--file", "--model", "--json"],
resultShape: "normalized text output",
},
{
id: "audio.providers",
description: "List audio transcription providers.",
transports: ["local"],
flags: ["--json"],
resultShape: "provider ids and capabilities",
},
{
id: "tts.convert",
description: "Convert text to speech.",
transports: ["local", "gateway"],
flags: [
"--text",
"--channel",
"--voice",
"--model",
"--output",
"--local",
"--gateway",
"--json",
],
resultShape: "saved audio file plus attempts",
},
{
id: "tts.voices",
description: "List voices for a speech provider.",
transports: ["local"],
flags: ["--provider", "--json"],
resultShape: "voice entries",
},
{
id: "tts.providers",
description: "List speech providers.",
transports: ["local", "gateway"],
flags: ["--local", "--gateway", "--json"],
resultShape: "provider ids, configured state, models, voices",
},
{
id: "tts.personas",
description: "List TTS personas.",
transports: ["local", "gateway"],
flags: ["--local", "--gateway", "--json"],
resultShape: "persona ids, labels, providers, active persona",
},
{
id: "tts.status",
description: "Show gateway-managed TTS state.",
transports: ["gateway"],
flags: ["--gateway", "--json"],
resultShape: "enabled/provider state",
},
{
id: "tts.enable",
description: "Enable TTS in prefs.",
transports: ["local", "gateway"],
flags: ["--local", "--gateway", "--json"],
resultShape: "enabled state",
},
{
id: "tts.disable",
description: "Disable TTS in prefs.",
transports: ["local", "gateway"],
flags: ["--local", "--gateway", "--json"],
resultShape: "enabled state",
},
{
id: "tts.set-provider",
description: "Set the active TTS provider.",
transports: ["local", "gateway"],
flags: ["--provider", "--local", "--gateway", "--json"],
resultShape: "selected provider",
},
{
id: "tts.set-persona",
description: "Set the active TTS persona.",
transports: ["local", "gateway"],
flags: ["--persona", "--off", "--local", "--gateway", "--json"],
resultShape: "selected persona",
},
{
id: "video.generate",
description: "Generate video files with configured video providers.",
transports: ["local"],
flags: [
"--prompt",
"--model",
"--size",
"--aspect-ratio",
"--resolution",
"--duration",
"--audio",
"--watermark",
"--timeout-ms",
"--output",
"--json",
],
resultShape: "saved video files plus attempts",
},
{
id: "video.describe",
description: "Describe one video file through media-understanding providers.",
transports: ["local"],
flags: ["--file", "--model", "--json"],
resultShape: "normalized text output",
},
{
id: "video.providers",
description: "List video generation and description providers.",
transports: ["local"],
flags: ["--json"],
resultShape: "provider ids and defaults",
},
{
id: "web.search",
description: "Run provider-backed web search.",
transports: ["local"],
flags: ["--query", "--provider", "--limit", "--json"],
resultShape: "search provider result",
},
{
id: "web.fetch",
description: "Fetch URL content through configured web fetch providers.",
transports: ["local"],
flags: ["--url", "--provider", "--format", "--json"],
resultShape: "fetch provider result",
},
{
id: "web.providers",
description: "List web search and fetch providers.",
transports: ["local"],
flags: ["--json"],
resultShape: "provider ids grouped by family",
},
{
id: "embedding.create",
description: "Create embeddings through embedding providers.",
transports: ["local"],
flags: ["--text", "--provider", "--model", "--json"],
resultShape: "vectors with provider/model attribution",
},
{
id: "embedding.providers",
description: "List embedding providers.",
transports: ["local"],
flags: ["--json"],
resultShape: "provider ids and default models",
},
];
function findCapabilityMetadata(id: string): CapabilityMetadata | undefined {
return CAPABILITY_METADATA.find((entry) => entry.id === id);
}
function resolveTransport(opts: {
local?: boolean;
gateway?: boolean;
supported: Array<CapabilityTransport>;
defaultTransport: CapabilityTransport;
}): CapabilityTransport {
if (opts.local && opts.gateway) {
throw new Error("Pass only one of --local or --gateway.");
}
if (opts.local) {
if (!opts.supported.includes("local")) {
throw new Error("This command does not support --local.");
}
return "local";
}
if (opts.gateway) {
if (!opts.supported.includes("gateway")) {
throw new Error("This command does not support --gateway.");
}
return "gateway";
}
return opts.defaultTransport;
}
function emitJsonOrText(
runtime: RuntimeEnv,
json: boolean | undefined,
value: unknown,
textFormatter: (value: unknown) => string,
) {
if (json) {
writeRuntimeJson(runtime, value);
return;
}
runtime.log(textFormatter(value));
}
function formatEnvelopeForText(value: unknown): string {
const envelope = value as CapabilityEnvelope;
if (!envelope.ok) {
return `${envelope.capability} failed: ${envelope.error ?? "unknown error"}`;
}
const lines = [
`${envelope.capability} via ${envelope.transport}`,
...(envelope.provider ? [`provider: ${envelope.provider}`] : []),
...(envelope.model ? [`model: ${envelope.model}`] : []),
...(envelope.ignoredOverrides && envelope.ignoredOverrides.length > 0
? [`ignoredOverrides: ${JSON.stringify(envelope.ignoredOverrides)}`]
: []),
`outputs: ${String(envelope.outputs.length)}`,
];
for (const output of envelope.outputs) {
const pathValue = typeof output.path === "string" ? output.path : undefined;
const textValue = typeof output.text === "string" ? output.text : undefined;
if (pathValue) {
lines.push(pathValue);
} else if (textValue) {
lines.push(textValue);
} else {
lines.push(JSON.stringify(output));
}
}
return lines.join("\n");
}
function providerSummaryText(value: unknown): string {
const providers = value as Array<Record<string, unknown>>;
return providers.map((entry) => JSON.stringify(entry)).join("\n");
}
function hasOwnKeys(value: unknown): boolean {
return Boolean(
value && typeof value === "object" && Object.keys(value as Record<string, unknown>).length > 0,
);
}
function resolveSelectedProviderFromModelRef(modelRef: string | undefined): string | undefined {
return resolveModelRefOverride(modelRef).provider;
}
function getAuthProfileIdsForProvider(cfg: OpenClawConfig, providerId: string): string[] {
const agentDir = resolveAgentDir(cfg, resolveDefaultAgentId(cfg));
const store = loadAuthProfileStoreForRuntime(agentDir);
return listProfilesForProvider(store, providerId);
}
function providerHasGenericConfig(params: {
cfg: OpenClawConfig;
providerId: string;
envVars?: string[];
}): boolean {
const modelsProviders = (params.cfg.models?.providers ?? {}) as Record<string, unknown>;
const pluginEntries = (params.cfg.plugins?.entries ?? {}) as Record<string, { config?: unknown }>;
const ttsProviders = (params.cfg.messages?.tts?.providers ?? {}) as Record<string, unknown>;
const envConfigured = (params.envVars ?? []).some((envVar) =>
Boolean(process.env[envVar]?.trim()),
);
return (
getAuthProfileIdsForProvider(params.cfg, params.providerId).length > 0 ||
hasOwnKeys(modelsProviders[params.providerId]) ||
hasOwnKeys(pluginEntries[params.providerId]?.config) ||
hasOwnKeys(ttsProviders[params.providerId]) ||
envConfigured
);
}
async function writeOutputAsset(params: {
buffer: Buffer;
mimeType?: string;
originalFilename?: string;
outputPath?: string;
outputIndex: number;
outputCount: number;
subdir: string;
}) {
if (!params.outputPath) {
const saved = await saveMediaBuffer(
params.buffer,
params.mimeType,
params.subdir,
Number.MAX_SAFE_INTEGER,
params.originalFilename,
);
return { path: saved.path, mimeType: saved.contentType, size: saved.size };
}
const resolvedOutput = path.resolve(params.outputPath);
const parsed = path.parse(resolvedOutput);
const detectedMime =
(await detectMime({
buffer: params.buffer,
headerMime: params.mimeType,
})) ?? params.mimeType;
const requestedMime = normalizeMimeType(await detectMime({ filePath: resolvedOutput }));
const detectedNormalized = normalizeMimeType(detectedMime);
const canonicalDetectedExt = extensionForMime(detectedNormalized);
const fallbackExt = parsed.ext || path.extname(params.originalFilename ?? "") || "";
const ext =
parsed.ext && requestedMime === detectedNormalized
? parsed.ext
: (canonicalDetectedExt ?? fallbackExt);
const filePath =
params.outputCount <= 1
? path.join(parsed.dir, `${parsed.name}${ext}`)
: path.join(parsed.dir, `${parsed.name}-${String(params.outputIndex + 1)}${ext}`);
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, params.buffer);
return {
path: filePath,
mimeType: detectedNormalized ?? params.mimeType,
size: params.buffer.byteLength,
};
}
async function readInputFiles(files: string[]): Promise<Array<{ path: string; buffer: Buffer }>> {
return await Promise.all(
files.map(async (filePath) => ({
path: path.resolve(filePath),
buffer: await fs.readFile(path.resolve(filePath)),
})),
);
}
// Canonicalize a user-supplied `--model` ref against the catalog before
// dispatch (#73715). The catalog lookup is case-insensitive, but the returned
// id keeps the catalog's canonical casing — so genuinely mixed-case canonical
// ids (e.g. `deepseek/DeepSeek-R1`) survive untouched while case-only
// mismatches (e.g. `anthropic/CLAUDE-OPUS-4-7`) get rewritten to the canonical
// `anthropic/claude-opus-4-7` form. Refs that don't match any catalog entry
// (custom configured models, dynamic plugin models) are returned verbatim and
// the downstream resolver decides what to do.
//
// Auth profile suffixes (`<ref>@<profile>`) are case-sensitive — profile keys
// are looked up by exact match — so the suffix is split out, never modified,
// and reattached to the canonicalized ref.
async function canonicalizeCliModelRef(raw: string | undefined): Promise<string | undefined> {
const trimmed = raw?.trim();
if (!trimmed) {
return undefined;
}
const { model, profile } = splitTrailingAuthProfile(trimmed);
if (!model) {
return trimmed;
}
const slash = model.indexOf("/");
if (slash <= 0 || slash === model.length - 1) {
return trimmed;
}
const providerInput = model.slice(0, slash);
const modelInput = model.slice(slash + 1);
const providerKey = providerInput.toLowerCase();
const modelKeyLower = modelInput.toLowerCase();
let catalog: Awaited<ReturnType<typeof loadModelCatalog>>;
try {
catalog = await loadModelCatalog();
} catch {
return trimmed;
}
// Strict match wins: preserves intentionally mixed-case canonical ids.
const exact = catalog.find(
(entry) => entry.provider.toLowerCase() === providerKey && entry.id === modelInput,
);
if (exact) {
return trimmed;
}
const ciMatch = catalog.find(
(entry) =>
entry.provider.toLowerCase() === providerKey && entry.id.toLowerCase() === modelKeyLower,
);
if (!ciMatch) {
return trimmed;
}
const canonical = `${providerInput}/${ciMatch.id}`;
return profile ? `${canonical}@${profile}` : canonical;
}
function resolveModelRefOverride(raw: string | undefined): { provider?: string; model?: string } {
const trimmed = raw?.trim();
if (!trimmed) {
return {};
}
const slash = trimmed.indexOf("/");
if (slash <= 0 || slash === trimmed.length - 1) {
return { model: trimmed };
}
return {
provider: trimmed.slice(0, slash),
model: trimmed.slice(slash + 1),
};
}
function requireProviderModelOverride(
raw: string | undefined,
): { provider: string; model: string } | undefined {
const resolved = resolveModelRefOverride(raw);
if (!raw?.trim()) {
return undefined;
}
if (!resolved.provider || !resolved.model) {
throw new Error("Model overrides must use the form <provider/model>.");
}
return {
provider: resolved.provider,
model: resolved.model,
};
}
function collectModelRunText(content: Array<{ type: string; text?: string }>): string {
return content
.map((block) => (block.type === "text" && typeof block.text === "string" ? block.text : ""))
.join("")
.trim();
}
function requireModelRunPrompt(value: unknown): string {
if (typeof value !== "string" || normalizeOptionalString(value) === undefined) {
throw new Error("--prompt cannot be empty or whitespace-only.");
}
return value;
}
type ModelRunImageFile = {
path: string;
fileName: string;
mimeType: string;
data: string;
};
async function readModelRunImageFiles(files: string[] | undefined): Promise<ModelRunImageFile[]> {
if (!files || files.length === 0) {
return [];
}
return await Promise.all(
files.map(async (filePath) => {
const resolvedPath = path.resolve(filePath);
const buffer = await fs.readFile(resolvedPath);
const mimeType = normalizeMimeType(
await detectMime({
buffer,
filePath: resolvedPath,
}),
);
if (!mimeType?.startsWith("image/")) {
throw new Error(
`Unsupported --file for model run: ${resolvedPath}. Only image files are supported; use infer audio transcribe for audio files.`,
);
}
return {
path: resolvedPath,
fileName: path.basename(resolvedPath),
mimeType,
data: buffer.toString("base64"),
};
}),
);
}
async function runModelRun(params: {
prompt: string;
files?: string[];
model?: string;
transport: CapabilityTransport;
}) {
const cfg = getRuntimeConfig();
const agentId = resolveDefaultAgentId(cfg);
// Canonicalize the user-supplied --model against the model catalog before
// any provider call (#73715). The catalog lookup preserves intentionally
// mixed-case canonical ids (e.g. `deepseek/DeepSeek-R1`,
// `openrouter/qwen/Qwen3-30B-A3B-6bit`) when the typed ref is an exact
// match, and rewrites case-only mismatches (e.g. `anthropic/CLAUDE-OPUS-4-7`
// → `anthropic/claude-opus-4-7`) so both the local and gateway transports
// dispatch the canonical id. Auth profile suffixes are not lowercased.
const modelRef = await canonicalizeCliModelRef(params.model);
const imageFiles = await readModelRunImageFiles(params.files);
const messageContent =
imageFiles.length > 0
? [
{ type: "text" as const, text: params.prompt },
...imageFiles.map((image) => ({
type: "image" as const,
data: image.data,
mimeType: image.mimeType,
})),
]
: params.prompt;
if (params.transport === "local") {
const prepared = await prepareSimpleCompletionModelForAgent({
cfg,
agentId,
modelRef,
allowMissingApiKeyModes: ["aws-sdk"],
skipPiDiscovery: true,
});
if ("error" in prepared) {
throw new Error(prepared.error);
}
if (prepared.selection.provider === "codex") {
throw new Error(
'The codex provider is served by the Codex app-server agent runtime, not the local simple-completion transport. Use an openai/<model> ref with agents.defaults.agentRuntime.id: "codex", run through the gateway, or use /codex commands.',
);
}
const result = await completeWithPreparedSimpleCompletionModel({
model: prepared.model,
auth: prepared.auth,
cfg,
context: {
messages: [
{
role: "user",
content: messageContent,
timestamp: Date.now(),
},
],
},
options: {
maxTokens:
typeof prepared.model.maxTokens === "number" && Number.isFinite(prepared.model.maxTokens)
? prepared.model.maxTokens
: undefined,
},
});
const text = collectModelRunText(result.content);
if (!text) {
throw new Error(
`No text output returned for provider "${prepared.selection.provider}" model "${prepared.selection.modelId}".`,
);
}
return {
ok: true,
capability: "model.run",
transport: "local" as const,
provider: prepared.selection.provider,
model: prepared.selection.modelId,
attempts: [],
...(imageFiles.length > 0
? {
inputs: imageFiles.map((image) => ({
path: image.path,
mimeType: image.mimeType,
})),
}
: {}),
outputs: [
{
text,
mediaUrl: null,
},
],
} satisfies CapabilityEnvelope;
}
const { provider, model } = resolveModelRefOverride(modelRef);
// Provider/model overrides require trusted-operator scope. Use the backend
// shared-secret lane so local gateway smokes do not depend on paired CLI device scopes.
const hasModelOverride = Boolean(provider || model);
const response: {
result?: {
payloads?: Array<{ text?: string; mediaUrl?: string | null; mediaUrls?: string[] }>;
meta?: {
agentMeta?: {
provider?: string;
model?: string;
fallbackAttempts?: Array<Record<string, unknown>>;
};
};
};
} = await callGateway({
method: "agent",
params: {
agentId,
message: params.prompt,
attachments:
imageFiles.length > 0
? imageFiles.map((image) => ({
type: "image",
fileName: image.fileName,
mimeType: image.mimeType,
content: image.data,
}))
: undefined,
provider,
model,
modelRun: true,
promptMode: "none",
cleanupBundleMcpOnRunEnd: true,
idempotencyKey: randomIdempotencyKey(),
},
expectFinal: true,
timeoutMs: 120_000,
clientName: hasModelOverride ? GATEWAY_CLIENT_NAMES.GATEWAY_CLIENT : GATEWAY_CLIENT_NAMES.CLI,
mode: hasModelOverride ? GATEWAY_CLIENT_MODES.BACKEND : GATEWAY_CLIENT_MODES.CLI,
...(hasModelOverride ? { scopes: [ADMIN_SCOPE] } : {}),
});
return {
ok: true,
capability: "model.run",
transport: "gateway" as const,
provider: response?.result?.meta?.agentMeta?.provider,
model: response?.result?.meta?.agentMeta?.model,
attempts: response?.result?.meta?.agentMeta?.fallbackAttempts ?? [],
outputs: (response?.result?.payloads ?? []).map((payload) => ({
text: payload.text,
mediaUrl: payload.mediaUrl,
mediaUrls: payload.mediaUrls,
})),
...(imageFiles.length > 0
? {
inputs: imageFiles.map((image) => ({
path: image.path,
mimeType: image.mimeType,
})),
}
: {}),
} satisfies CapabilityEnvelope;
}
async function buildModelProviders() {
const cfg = getRuntimeConfig();
const catalog = await loadModelCatalog({ config: cfg });
const selectedProvider = resolveSelectedProviderFromModelRef(
resolveAgentModelPrimaryValue(cfg.agents?.defaults?.model),
);
const grouped = new Map<
string,
{
provider: string;
count: number;
defaults: string[];
available: boolean;
configured: boolean;
selected: boolean;
}
>();
for (const entry of catalog) {
const current = grouped.get(entry.provider) ?? {
provider: entry.provider,
count: 0,
defaults: [],
available: true,
configured: providerHasGenericConfig({ cfg, providerId: entry.provider }),
selected: selectedProvider === entry.provider,
};
current.count += 1;
if (current.defaults.length < 3) {
current.defaults.push(entry.id);
}
grouped.set(entry.provider, current);
}
return [...grouped.values()].toSorted((a, b) => a.provider.localeCompare(b.provider));
}
async function runModelAuthStatus() {
const captured: string[] = [];
const { modelsStatusCommand } = await import("../commands/models/list.status-command.js");
await modelsStatusCommand(
{ json: true },
{
log: (...args) => captured.push(args.join(" ")),
error: (message) => {
throw message instanceof Error ? message : new Error(String(message));
},
exit: (code) => {
throw new Error(`exit ${code}`);
},
},
);
const raw = captured.find((line) => line.trim().startsWith("{"));
return raw ? (JSON.parse(raw) as Record<string, unknown>) : {};
}
async function runModelAuthLogout(provider: string) {
const cfg = getRuntimeConfig();
const agentDir = resolveAgentDir(cfg, resolveDefaultAgentId(cfg));
const store = loadAuthProfileStoreForRuntime(agentDir);
const profileIds = listProfilesForProvider(store, provider);
const updated = await updateAuthProfileStoreWithLock({
agentDir,
updater: (nextStore) => {
let changed = false;
for (const profileId of profileIds) {
if (nextStore.profiles[profileId]) {
delete nextStore.profiles[profileId];
changed = true;
}
if (nextStore.usageStats?.[profileId]) {
delete nextStore.usageStats[profileId];
changed = true;
}
}
if (nextStore.order?.[provider]) {
delete nextStore.order[provider];
changed = true;
}
if (nextStore.lastGood?.[provider]) {
delete nextStore.lastGood[provider];
changed = true;
}
return changed;
},
});
if (!updated) {
throw new Error(`Failed to remove saved auth profiles for provider ${provider}.`);
}
return {
provider,
removedProfiles: profileIds,
};
}
async function runImageGenerate(params: {
capability: "image.generate" | "image.edit";
prompt: string;
model?: string;
count?: number;
size?: string;
aspectRatio?: string;
resolution?: "1K" | "2K" | "4K";
outputFormat?: ImageGenerationOutputFormat;
background?: ImageGenerationBackground;
openaiBackground?: ImageGenerationBackground;
file?: string[];
output?: string;
timeoutMs?: number;
}) {
const cfg = getRuntimeConfig();
const agentDir = resolveAgentDir(cfg, resolveDefaultAgentId(cfg));
const inputImages =
params.file && params.file.length > 0
? await Promise.all(
(await readInputFiles(params.file)).map(async (entry) => ({
buffer: entry.buffer,
fileName: path.basename(entry.path),
mimeType:
(await detectMime({ buffer: entry.buffer, filePath: entry.path })) ?? "image/png",
})),
)
: undefined;
const result = await generateImage({
cfg,
agentDir,
prompt: params.prompt,
modelOverride: params.model,
count: params.count,
size: params.size,
aspectRatio: params.aspectRatio,
resolution: params.resolution,
outputFormat: params.outputFormat,
background: params.background,
providerOptions: params.openaiBackground
? { openai: { background: params.openaiBackground } }
: undefined,
timeoutMs: params.timeoutMs,
inputImages,
});
const outputs = await Promise.all(
result.images.map(async (image, index) => {
const written = await writeOutputAsset({
buffer: image.buffer,
mimeType: image.mimeType,
originalFilename: image.fileName,
outputPath: params.output,
outputIndex: index,
outputCount: result.images.length,
subdir: "generated",
});
const metadata = await getImageMetadata(image.buffer).catch(() => undefined);
return {
...written,
width: metadata?.width,
height: metadata?.height,
revisedPrompt: image.revisedPrompt,
};
}),
);
return {
ok: true,