mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 05:01:15 +00:00
perf(test): keep discord model picker provider runtime lazy
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { serializePayload } from "@buape/carbon";
|
||||
import { ComponentType } from "discord-api-types/v10";
|
||||
import * as modelsCommandModule from "openclaw/plugin-sdk/models-provider-runtime";
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
@@ -21,6 +20,12 @@ import {
|
||||
} from "./model-picker.js";
|
||||
import { createModelsProviderData } from "./model-picker.test-utils.js";
|
||||
|
||||
const buildModelsProviderDataMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("openclaw/plugin-sdk/models-provider-runtime", () => ({
|
||||
buildModelsProviderData: buildModelsProviderDataMock,
|
||||
}));
|
||||
|
||||
type SerializedComponent = {
|
||||
type: number;
|
||||
custom_id?: string;
|
||||
@@ -71,14 +76,12 @@ describe("loadDiscordModelPickerData", () => {
|
||||
it("reuses buildModelsProviderData as source of truth with agent scope", async () => {
|
||||
const expected = createModelsProviderData({ openai: ["gpt-4o"] });
|
||||
const cfg = {} as OpenClawConfig;
|
||||
const spy = vi
|
||||
.spyOn(modelsCommandModule, "buildModelsProviderData")
|
||||
.mockResolvedValue(expected);
|
||||
buildModelsProviderDataMock.mockResolvedValue(expected);
|
||||
|
||||
const result = await loadDiscordModelPickerData(cfg, "support");
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
expect(spy).toHaveBeenCalledWith(cfg, "support");
|
||||
expect(buildModelsProviderDataMock).toHaveBeenCalledTimes(1);
|
||||
expect(buildModelsProviderDataMock).toHaveBeenCalledWith(cfg, "support");
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,11 +12,8 @@ import {
|
||||
import type { APISelectMenuOption } from "discord-api-types/v10";
|
||||
import { ButtonStyle } from "discord-api-types/v10";
|
||||
import { normalizeProviderId } from "openclaw/plugin-sdk/agent-runtime";
|
||||
import {
|
||||
buildModelsProviderData,
|
||||
type ModelsProviderData,
|
||||
} from "openclaw/plugin-sdk/models-provider-runtime";
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import type { ModelsProviderData } from "openclaw/plugin-sdk/models-provider-runtime";
|
||||
|
||||
export const DISCORD_MODEL_PICKER_CUSTOM_ID_KEY = "mdlpk";
|
||||
export const DISCORD_CUSTOM_ID_MAX_CHARS = 100;
|
||||
@@ -142,6 +139,15 @@ export type DiscordModelPickerModelViewParams = {
|
||||
layout?: DiscordModelPickerLayout;
|
||||
};
|
||||
|
||||
let modelsProviderRuntimePromise:
|
||||
| Promise<typeof import("openclaw/plugin-sdk/models-provider-runtime")>
|
||||
| undefined;
|
||||
|
||||
async function loadModelsProviderRuntime() {
|
||||
modelsProviderRuntimePromise ??= import("openclaw/plugin-sdk/models-provider-runtime");
|
||||
return await modelsProviderRuntimePromise;
|
||||
}
|
||||
|
||||
function encodeCustomIdValue(value: string): string {
|
||||
return encodeURIComponent(value);
|
||||
}
|
||||
@@ -544,6 +550,7 @@ export async function loadDiscordModelPickerData(
|
||||
cfg: OpenClawConfig,
|
||||
agentId?: string,
|
||||
): Promise<ModelsProviderData> {
|
||||
const { buildModelsProviderData } = await loadModelsProviderRuntime();
|
||||
return buildModelsProviderData(cfg, agentId);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user