























@@ -1,13 +1,10 @@
11import {
2-BedrockClient,
3-ListFoundationModelsCommand,
2+type BedrockClient,
43type ListFoundationModelsCommandOutput,
5-ListInferenceProfilesCommand,
64type ListInferenceProfilesCommandOutput,
75} from "@aws-sdk/client-bedrock";
86import { createSubsystemLogger } from "openclaw/plugin-sdk/core";
97import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
10-import { resolveAwsSdkEnvVarName } from "openclaw/plugin-sdk/provider-auth-runtime";
118import type {
129BedrockDiscoveryConfig,
1310ModelDefinitionConfig,
@@ -17,6 +14,7 @@ import {
1714normalizeLowercaseStringOrEmpty,
1815normalizeOptionalLowercaseString,
1916} from "openclaw/plugin-sdk/text-runtime";
17+import { resolveBedrockConfigApiKey } from "./discovery-shared.js";
20182119const log = createSubsystemLogger("bedrock-discovery");
2220@@ -149,6 +147,38 @@ type InferenceProfileSummary = NonNullable<
149147ListInferenceProfilesCommandOutput["inferenceProfileSummaries"]
150148>[number];
151149150+type BedrockDiscoverySdk = {
151+createClient(region: string): BedrockClient;
152+createListFoundationModelsCommand(): unknown;
153+createListInferenceProfilesCommand(input: { nextToken?: string }): unknown;
154+};
155+156+async function loadBedrockDiscoverySdk(): Promise<BedrockDiscoverySdk> {
157+const { BedrockClient, ListFoundationModelsCommand, ListInferenceProfilesCommand } =
158+await import("@aws-sdk/client-bedrock");
159+return {
160+createClient: (region) => new BedrockClient({ region }),
161+createListFoundationModelsCommand: () => new ListFoundationModelsCommand({}),
162+createListInferenceProfilesCommand: (input) => new ListInferenceProfilesCommand(input),
163+};
164+}
165+166+function createInjectedClientDiscoverySdk(): BedrockDiscoverySdk {
167+class ListFoundationModelsCommand {
168+constructor(readonly input: Record<string, unknown> = {}) {}
169+}
170+class ListInferenceProfilesCommand {
171+constructor(readonly input: Record<string, unknown> = {}) {}
172+}
173+return {
174+createClient() {
175+throw new Error("clientFactory is required for injected Bedrock discovery commands");
176+},
177+createListFoundationModelsCommand: () => new ListFoundationModelsCommand({}),
178+createListInferenceProfilesCommand: (input) => new ListInferenceProfilesCommand(input),
179+};
180+}
181+152182type BedrockDiscoveryCacheEntry = {
153183expiresAt: number;
154184value?: ModelDefinitionConfig[];
@@ -320,13 +350,14 @@ function resolveBaseModelId(profile: InferenceProfileSummary): string | undefine
320350 */
321351async function fetchInferenceProfileSummaries(
322352client: BedrockClient,
353+createListInferenceProfilesCommand: BedrockDiscoverySdk["createListInferenceProfilesCommand"],
323354): Promise<InferenceProfileSummary[]> {
324355try {
325356const profiles: InferenceProfileSummary[] = [];
326357let nextToken: string | undefined;
327358do {
328359const response: ListInferenceProfilesCommandOutput = await client.send(
329-new ListInferenceProfilesCommand({ nextToken }),
360+createListInferenceProfilesCommand({ nextToken }) as never,
330361);
331362for (const summary of response.inferenceProfileSummaries ?? []) {
332363profiles.push(summary);
@@ -414,14 +445,6 @@ export function resetBedrockDiscoveryCacheForTest(): void {
414445hasLoggedBedrockError = false;
415446}
416447417-export function resolveBedrockConfigApiKey(
418-env: NodeJS.ProcessEnv = process.env,
419-): string | undefined {
420-// When no AWS auth env marker is present, Bedrock should fall back to the
421-// AWS SDK default credential chain instead of persisting a fake apiKey marker.
422-return resolveAwsSdkEnvVarName(env);
423-}
424-425448export async function discoverBedrockModels(params: {
426449region: string;
427450config?: BedrockDiscoveryConfig;
@@ -454,18 +477,24 @@ export async function discoverBedrockModels(params: {
454477}
455478}
456479457-const clientFactory = params.clientFactory ?? ((region: string) => new BedrockClient({ region }));
480+const sdk = params.clientFactory
481+ ? createInjectedClientDiscoverySdk()
482+ : await loadBedrockDiscoverySdk();
483+const clientFactory = params.clientFactory ?? ((region: string) => sdk.createClient(region));
458484const client = clientFactory(params.region);
459485460486const discoveryPromise = (async () => {
461487// Discover foundation models and inference profiles in parallel.
462488// Both API calls are independent, but we need the foundation model data
463489// to resolve inference profile capabilities — so we fetch in parallel,
464490// then build the lookup map before processing profiles.
465-const [foundationResponse, profileSummaries] = await Promise.all([
466-client.send(new ListFoundationModelsCommand({})),
467-fetchInferenceProfileSummaries(client),
491+const [rawFoundationResponse, profileSummaries] = await Promise.all([
492+client.send(sdk.createListFoundationModelsCommand() as never),
493+fetchInferenceProfileSummaries(client, (input) =>
494+sdk.createListInferenceProfilesCommand(input),
495+),
468496]);
497+const foundationResponse = rawFoundationResponse as ListFoundationModelsCommandOutput;
469498470499const discovered: ModelDefinitionConfig[] = [];
471500const seenIds = new Set<string>();
@@ -556,7 +585,7 @@ export async function resolveImplicitBedrockProvider(params: {
556585 ...params.pluginConfig?.discovery,
557586};
558587const enabled = discoveryConfig?.enabled;
559-const hasAwsCreds = resolveAwsSdkEnvVarName(env) !== undefined;
588+const hasAwsCreds = resolveBedrockConfigApiKey(env) !== undefined;
560589if (enabled === false) {
561590return null;
562591}
@@ -581,21 +610,3 @@ export async function resolveImplicitBedrockProvider(params: {
581610 models,
582611};
583612}
584-585-export function mergeImplicitBedrockProvider(params: {
586-existing: ModelProviderConfig | undefined;
587-implicit: ModelProviderConfig;
588-}): ModelProviderConfig {
589-const { existing, implicit } = params;
590-if (!existing) {
591-return implicit;
592-}
593-return {
594- ...implicit,
595- ...existing,
596-models:
597-Array.isArray(existing.models) && existing.models.length > 0
598- ? existing.models
599- : implicit.models,
600-};
601-}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。