Skip to content

Commit 6e0957c

Browse files
committed
refactor: dedupe internal helper glue
1 parent 5f37014 commit 6e0957c

7 files changed

Lines changed: 165 additions & 205 deletions

File tree

‎src/agents/pi-embedded-runner/tool-schema-runtime.ts‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ type ProviderToolSchemaParams<TSchemaType extends TSchema = TSchema, TResult = u
2020
model?: ProviderRuntimeModel;
2121
};
2222

23+
function buildProviderToolSchemaContext<TSchemaType extends TSchema = TSchema, TResult = unknown>(
24+
params: ProviderToolSchemaParams<TSchemaType, TResult>,
25+
provider: string,
26+
) {
27+
return {
28+
config: params.config,
29+
workspaceDir: params.workspaceDir,
30+
env: params.env,
31+
provider,
32+
modelId: params.modelId,
33+
modelApi: params.modelApi,
34+
model: params.model,
35+
tools: params.tools as unknown as AnyAgentTool[],
36+
};
37+
}
38+
2339
/**
2440
* Runs provider-owned tool-schema normalization without encoding provider
2541
* families in the embedded runner.
@@ -34,16 +50,7 @@ export function normalizeProviderToolSchemas<
3450
config: params.config,
3551
workspaceDir: params.workspaceDir,
3652
env: params.env,
37-
context: {
38-
config: params.config,
39-
workspaceDir: params.workspaceDir,
40-
env: params.env,
41-
provider,
42-
modelId: params.modelId,
43-
modelApi: params.modelApi,
44-
model: params.model,
45-
tools: params.tools as unknown as AnyAgentTool[],
46-
},
53+
context: buildProviderToolSchemaContext(params, provider),
4754
});
4855
return Array.isArray(pluginNormalized)
4956
? (pluginNormalized as AgentTool<TSchemaType, TResult>[])
@@ -60,16 +67,7 @@ export function logProviderToolSchemaDiagnostics(params: ProviderToolSchemaParam
6067
config: params.config,
6168
workspaceDir: params.workspaceDir,
6269
env: params.env,
63-
context: {
64-
config: params.config,
65-
workspaceDir: params.workspaceDir,
66-
env: params.env,
67-
provider,
68-
modelId: params.modelId,
69-
modelApi: params.modelApi,
70-
model: params.model,
71-
tools: params.tools as unknown as AnyAgentTool[],
72-
},
70+
context: buildProviderToolSchemaContext(params, provider),
7371
});
7472
if (!Array.isArray(diagnostics)) {
7573
return;

‎src/agents/tools/pdf-tool.model-config.ts‎

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ import {
1212
import { hasAuthForProvider, resolveDefaultModelRef } from "./model-config.helpers.js";
1313
import { coercePdfModelConfig } from "./pdf-tool.helpers.js";
1414

15+
function resolveBundledImageCandidateRefs(params: {
16+
cfg?: OpenClawConfig;
17+
agentDir: string;
18+
filter?: (providerId: string) => boolean;
19+
}): string[] {
20+
return resolveBundledAutoMediaKeyProviders("image")
21+
.filter((providerId) => !params.filter || params.filter(providerId))
22+
.filter((providerId) => hasAuthForProvider({ provider: providerId, agentDir: params.agentDir }))
23+
.map((providerId) => {
24+
const modelId =
25+
resolveProviderVisionModelFromConfig({
26+
cfg: params.cfg,
27+
provider: providerId,
28+
})?.split("/")[1] ??
29+
resolveBundledDefaultMediaModel({
30+
providerId,
31+
capability: "image",
32+
});
33+
return modelId ? `${providerId}/${modelId}` : null;
34+
})
35+
.filter((value): value is string => Boolean(value));
36+
}
37+
1538
export function resolvePdfModelConfigForTool(params: {
1639
cfg?: OpenClawConfig;
1740
agentDir: string;
@@ -51,37 +74,15 @@ export function resolvePdfModelConfigForTool(params: {
5174
capability: "image",
5275
});
5376
const primarySupportsNativePdf = bundledProviderSupportsNativePdfDocument(primary.provider);
54-
const nativePdfCandidates = resolveBundledAutoMediaKeyProviders("image")
55-
.filter((providerId) => bundledProviderSupportsNativePdfDocument(providerId))
56-
.filter((providerId) => hasAuthForProvider({ provider: providerId, agentDir: params.agentDir }))
57-
.map((providerId) => {
58-
const modelId =
59-
resolveProviderVisionModelFromConfig({
60-
cfg: params.cfg,
61-
provider: providerId,
62-
})?.split("/")[1] ??
63-
resolveBundledDefaultMediaModel({
64-
providerId,
65-
capability: "image",
66-
});
67-
return modelId ? `${providerId}/${modelId}` : null;
68-
})
69-
.filter((value): value is string => Boolean(value));
70-
const genericImageCandidates = resolveBundledAutoMediaKeyProviders("image")
71-
.filter((providerId) => hasAuthForProvider({ provider: providerId, agentDir: params.agentDir }))
72-
.map((providerId) => {
73-
const modelId =
74-
resolveProviderVisionModelFromConfig({
75-
cfg: params.cfg,
76-
provider: providerId,
77-
})?.split("/")[1] ??
78-
resolveBundledDefaultMediaModel({
79-
providerId,
80-
capability: "image",
81-
});
82-
return modelId ? `${providerId}/${modelId}` : null;
83-
})
84-
.filter((value): value is string => Boolean(value));
77+
const nativePdfCandidates = resolveBundledImageCandidateRefs({
78+
cfg: params.cfg,
79+
agentDir: params.agentDir,
80+
filter: bundledProviderSupportsNativePdfDocument,
81+
});
82+
const genericImageCandidates = resolveBundledImageCandidateRefs({
83+
cfg: params.cfg,
84+
agentDir: params.agentDir,
85+
});
8586

8687
if (params.cfg?.models?.providers && typeof params.cfg.models.providers === "object") {
8788
for (const [providerKey, providerCfg] of Object.entries(params.cfg.models.providers)) {

‎src/cli/acp-cli.ts‎

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
11
import type { Command } from "commander";
22
import { runAcpClientInteractive } from "../acp/client.js";
3-
import { readSecretFromFile } from "../acp/secret-file.js";
43
import { serveAcpGateway } from "../acp/server.js";
54
import { normalizeAcpProvenanceMode } from "../acp/types.js";
65
import { defaultRuntime } from "../runtime.js";
7-
import { normalizeOptionalString } from "../shared/string-coerce.js";
86
import { formatDocsLink } from "../terminal/links.js";
97
import { theme } from "../terminal/theme.js";
108
import { inheritOptionFromParent } from "./command-options.js";
11-
12-
function resolveSecretOption(params: {
13-
direct?: string;
14-
file?: string;
15-
directFlag: string;
16-
fileFlag: string;
17-
label: string;
18-
}) {
19-
const direct = normalizeOptionalString(params.direct);
20-
const file = normalizeOptionalString(params.file);
21-
if (direct && file) {
22-
throw new Error(`Use either ${params.directFlag} or ${params.fileFlag} for ${params.label}.`);
23-
}
24-
if (file) {
25-
return readSecretFromFile(file, params.label);
26-
}
27-
return direct || undefined;
28-
}
29-
30-
function warnSecretCliFlag(flag: "--token" | "--password") {
31-
defaultRuntime.error(
32-
`Warning: ${flag} can be exposed via process listings. Prefer ${flag}-file or environment variables.`,
33-
);
34-
}
9+
import { resolveGatewayAuthOptions } from "./gateway-secret-options.js";
3510

3611
export function registerAcpCli(program: Command) {
3712
const acp = program.command("acp").description("Run an ACP bridge backed by the Gateway");
@@ -55,26 +30,7 @@ export function registerAcpCli(program: Command) {
5530
)
5631
.action(async (opts) => {
5732
try {
58-
const gatewayToken = resolveSecretOption({
59-
direct: opts.token as string | undefined,
60-
file: opts.tokenFile as string | undefined,
61-
directFlag: "--token",
62-
fileFlag: "--token-file",
63-
label: "Gateway token",
64-
});
65-
const gatewayPassword = resolveSecretOption({
66-
direct: opts.password as string | undefined,
67-
file: opts.passwordFile as string | undefined,
68-
directFlag: "--password",
69-
fileFlag: "--password-file",
70-
label: "Gateway password",
71-
});
72-
if (opts.token) {
73-
warnSecretCliFlag("--token");
74-
}
75-
if (opts.password) {
76-
warnSecretCliFlag("--password");
77-
}
33+
const { gatewayToken, gatewayPassword } = resolveGatewayAuthOptions(opts);
7834
const provenanceMode = normalizeAcpProvenanceMode(opts.provenance as string | undefined);
7935
if (opts.provenance && !provenanceMode) {
8036
throw new Error("Invalid --provenance value. Use off, meta, or meta+receipt.");

‎src/cli/gateway-secret-options.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { readSecretFromFile } from "../acp/secret-file.js";
2+
import { defaultRuntime } from "../runtime.js";
3+
import { normalizeOptionalString } from "../shared/string-coerce.js";
4+
5+
export function resolveGatewaySecretOption(params: {
6+
direct?: unknown;
7+
file?: unknown;
8+
directFlag: string;
9+
fileFlag: string;
10+
label: string;
11+
}): string | undefined {
12+
const direct = normalizeOptionalString(params.direct);
13+
const file = normalizeOptionalString(params.file);
14+
if (direct && file) {
15+
throw new Error(`Use either ${params.directFlag} or ${params.fileFlag} for ${params.label}.`);
16+
}
17+
if (file) {
18+
return readSecretFromFile(file, params.label);
19+
}
20+
return direct || undefined;
21+
}
22+
23+
export function warnGatewaySecretCliFlag(flag: "--token" | "--password"): void {
24+
defaultRuntime.error(
25+
`Warning: ${flag} can be exposed via process listings. Prefer ${flag}-file or environment variables.`,
26+
);
27+
}
28+
29+
export function resolveGatewayAuthOptions(opts: {
30+
token?: unknown;
31+
tokenFile?: unknown;
32+
password?: unknown;
33+
passwordFile?: unknown;
34+
}): {
35+
gatewayToken?: string;
36+
gatewayPassword?: string;
37+
} {
38+
const gatewayToken = resolveGatewaySecretOption({
39+
direct: opts.token,
40+
file: opts.tokenFile,
41+
directFlag: "--token",
42+
fileFlag: "--token-file",
43+
label: "Gateway token",
44+
});
45+
const gatewayPassword = resolveGatewaySecretOption({
46+
direct: opts.password,
47+
file: opts.passwordFile,
48+
directFlag: "--password",
49+
fileFlag: "--password-file",
50+
label: "Gateway password",
51+
});
52+
if (opts.token) {
53+
warnGatewaySecretCliFlag("--token");
54+
}
55+
if (opts.password) {
56+
warnGatewaySecretCliFlag("--password");
57+
}
58+
return { gatewayToken, gatewayPassword };
59+
}

‎src/cli/mcp-cli.ts‎

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Command } from "commander";
2-
import { readSecretFromFile } from "../acp/secret-file.js";
32
import { parseConfigValue } from "../auto-reply/reply/config-value.js";
43
import {
54
listConfiguredMcpServers,
@@ -8,9 +7,11 @@ import {
87
} from "../config/mcp-config.js";
98
import { serveOpenClawChannelMcp } from "../mcp/channel-server.js";
109
import { defaultRuntime } from "../runtime.js";
11-
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
12-
import { normalizeOptionalString } from "../shared/string-coerce.js";
13-
import { normalizeStringifiedOptionalString } from "../shared/string-coerce.js";
10+
import {
11+
normalizeLowercaseStringOrEmpty,
12+
normalizeStringifiedOptionalString,
13+
} from "../shared/string-coerce.js";
14+
import { resolveGatewayAuthOptions } from "./gateway-secret-options.js";
1415

1516
function fail(message: string): never {
1617
defaultRuntime.error(message);
@@ -22,30 +23,6 @@ function printJson(value: unknown): void {
2223
defaultRuntime.writeJson(value);
2324
}
2425

25-
function resolveSecretOption(params: {
26-
direct?: string;
27-
file?: string;
28-
directFlag: string;
29-
fileFlag: string;
30-
label: string;
31-
}) {
32-
const direct = normalizeOptionalString(params.direct);
33-
const file = normalizeOptionalString(params.file);
34-
if (direct && file) {
35-
throw new Error(`Use either ${params.directFlag} or ${params.fileFlag} for ${params.label}.`);
36-
}
37-
if (file) {
38-
return readSecretFromFile(file, params.label);
39-
}
40-
return direct || undefined;
41-
}
42-
43-
function warnSecretCliFlag(flag: "--token" | "--password") {
44-
defaultRuntime.error(
45-
`Warning: ${flag} can be exposed via process listings. Prefer ${flag}-file or environment variables.`,
46-
);
47-
}
48-
4926
export function registerMcpCli(program: Command) {
5027
const mcp = program.command("mcp").description("Manage OpenClaw MCP config and channel bridge");
5128

@@ -65,26 +42,7 @@ export function registerMcpCli(program: Command) {
6542
.option("-v, --verbose", "Verbose logging to stderr", false)
6643
.action(async (opts) => {
6744
try {
68-
const gatewayToken = resolveSecretOption({
69-
direct: opts.token as string | undefined,
70-
file: opts.tokenFile as string | undefined,
71-
directFlag: "--token",
72-
fileFlag: "--token-file",
73-
label: "Gateway token",
74-
});
75-
const gatewayPassword = resolveSecretOption({
76-
direct: opts.password as string | undefined,
77-
file: opts.passwordFile as string | undefined,
78-
directFlag: "--password",
79-
fileFlag: "--password-file",
80-
label: "Gateway password",
81-
});
82-
if (opts.token) {
83-
warnSecretCliFlag("--token");
84-
}
85-
if (opts.password) {
86-
warnSecretCliFlag("--password");
87-
}
45+
const { gatewayToken, gatewayPassword } = resolveGatewayAuthOptions(opts);
8846
const claudeChannelMode = normalizeLowercaseStringOrEmpty(
8947
normalizeStringifiedOptionalString(opts.claudeChannelMode) ?? "auto",
9048
);

0 commit comments

Comments
 (0)