mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-13 01:10:45 +08:00
重构主控编排与运行时预热链路,统一工作区提示词/专家调度协议并补齐 wiki 记忆注入与写回闭环。
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。 Made-with: Cursor
This commit is contained in:
parent
4a23b715a2
commit
dbbe3add6a
14438 changed files with 2693620 additions and 2546 deletions
146
openclaw/extensions/mistral/api.test.ts
Normal file
146
openclaw/extensions/mistral/api.test.ts
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
applyMistralModelCompat,
|
||||
MISTRAL_MODEL_TRANSPORT_PATCH,
|
||||
MISTRAL_SMALL_LATEST_ID,
|
||||
resolveMistralCompatPatch,
|
||||
} from "./api.js";
|
||||
import { contributeMistralResolvedModelCompat } from "./provider-compat.js";
|
||||
|
||||
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters -- Test helper lets assertions ascribe provider compat shape.
|
||||
function readCompat<T>(model: unknown): T | undefined {
|
||||
return (model as { compat?: T }).compat;
|
||||
}
|
||||
|
||||
function supportsStore(model: unknown): boolean | undefined {
|
||||
return readCompat<{ supportsStore?: boolean }>(model)?.supportsStore;
|
||||
}
|
||||
|
||||
function supportsReasoningEffort(model: unknown): boolean | undefined {
|
||||
return readCompat<{ supportsReasoningEffort?: boolean }>(model)?.supportsReasoningEffort;
|
||||
}
|
||||
|
||||
function maxTokensField(model: unknown): "max_completion_tokens" | "max_tokens" | undefined {
|
||||
return readCompat<{ maxTokensField?: "max_completion_tokens" | "max_tokens" }>(model)
|
||||
?.maxTokensField;
|
||||
}
|
||||
|
||||
function reasoningEffortMap(model: unknown): Record<string, string> | undefined {
|
||||
return readCompat<{ reasoningEffortMap?: Record<string, string> }>(model)?.reasoningEffortMap;
|
||||
}
|
||||
|
||||
describe("resolveMistralCompatPatch", () => {
|
||||
it("enables reasoning_effort mapping for mistral-small-latest", () => {
|
||||
expect(resolveMistralCompatPatch({ id: MISTRAL_SMALL_LATEST_ID })).toMatchObject({
|
||||
supportsStore: false,
|
||||
supportsReasoningEffort: true,
|
||||
maxTokensField: "max_tokens",
|
||||
reasoningEffortMap: expect.objectContaining({ high: "high", off: "none" }),
|
||||
});
|
||||
});
|
||||
|
||||
it("disables reasoning_effort for other Mistral model ids", () => {
|
||||
expect(resolveMistralCompatPatch({ id: "mistral-large-latest" })).toEqual({
|
||||
...MISTRAL_MODEL_TRANSPORT_PATCH,
|
||||
supportsReasoningEffort: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyMistralModelCompat", () => {
|
||||
it("applies the Mistral request-shape compat flags", () => {
|
||||
const normalized = applyMistralModelCompat({});
|
||||
expect(supportsStore(normalized)).toBe(false);
|
||||
expect(supportsReasoningEffort(normalized)).toBe(false);
|
||||
expect(maxTokensField(normalized)).toBe("max_tokens");
|
||||
expect(reasoningEffortMap(normalized)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("applies reasoning compat for mistral-small-latest", () => {
|
||||
const normalized = applyMistralModelCompat({ id: MISTRAL_SMALL_LATEST_ID });
|
||||
expect(supportsReasoningEffort(normalized)).toBe(true);
|
||||
expect(reasoningEffortMap(normalized)?.high).toBe("high");
|
||||
expect(reasoningEffortMap(normalized)?.off).toBe("none");
|
||||
});
|
||||
|
||||
it("overrides explicit compat values that would trigger 422s", () => {
|
||||
const normalized = applyMistralModelCompat({
|
||||
compat: {
|
||||
supportsStore: true,
|
||||
supportsReasoningEffort: true,
|
||||
maxTokensField: "max_completion_tokens" as const,
|
||||
},
|
||||
});
|
||||
expect(supportsStore(normalized)).toBe(false);
|
||||
expect(supportsReasoningEffort(normalized)).toBe(false);
|
||||
expect(maxTokensField(normalized)).toBe("max_tokens");
|
||||
});
|
||||
|
||||
it("overrides explicit compat on mistral-small-latest except reasoning enablement", () => {
|
||||
const normalized = applyMistralModelCompat({
|
||||
id: MISTRAL_SMALL_LATEST_ID,
|
||||
compat: {
|
||||
supportsStore: true,
|
||||
supportsReasoningEffort: false,
|
||||
maxTokensField: "max_completion_tokens" as const,
|
||||
},
|
||||
});
|
||||
expect(supportsStore(normalized)).toBe(false);
|
||||
expect(supportsReasoningEffort(normalized)).toBe(true);
|
||||
expect(maxTokensField(normalized)).toBe("max_tokens");
|
||||
});
|
||||
|
||||
it("returns the same object when the compat patch is already present", () => {
|
||||
const model = {
|
||||
compat: {
|
||||
supportsStore: false,
|
||||
supportsReasoningEffort: false,
|
||||
maxTokensField: "max_tokens" as const,
|
||||
},
|
||||
};
|
||||
expect(applyMistralModelCompat(model)).toBe(model);
|
||||
});
|
||||
|
||||
it("returns the same object when mistral-small-latest compat is fully normalized", () => {
|
||||
const model = {
|
||||
id: MISTRAL_SMALL_LATEST_ID,
|
||||
compat: resolveMistralCompatPatch({ id: MISTRAL_SMALL_LATEST_ID }),
|
||||
};
|
||||
expect(applyMistralModelCompat(model)).toBe(model);
|
||||
});
|
||||
|
||||
it("contributes Mistral transport compat for native, provider-family, and hinted custom routes", () => {
|
||||
expect(
|
||||
contributeMistralResolvedModelCompat({
|
||||
modelId: "mistral-large-latest",
|
||||
model: {
|
||||
provider: "mistral",
|
||||
api: "openai-completions",
|
||||
baseUrl: "https://proxy.example/v1",
|
||||
},
|
||||
}),
|
||||
).toEqual(MISTRAL_MODEL_TRANSPORT_PATCH);
|
||||
|
||||
expect(
|
||||
contributeMistralResolvedModelCompat({
|
||||
modelId: "custom-model",
|
||||
model: {
|
||||
provider: "custom-mistral-host",
|
||||
api: "openai-completions",
|
||||
baseUrl: "https://api.mistral.ai/v1",
|
||||
},
|
||||
}),
|
||||
).toEqual(MISTRAL_MODEL_TRANSPORT_PATCH);
|
||||
|
||||
expect(
|
||||
contributeMistralResolvedModelCompat({
|
||||
modelId: "mistralai/mistral-small-3.2",
|
||||
model: {
|
||||
provider: "openrouter",
|
||||
api: "openai-completions",
|
||||
baseUrl: "https://openrouter.ai/api/v1",
|
||||
},
|
||||
}),
|
||||
).toEqual(MISTRAL_MODEL_TRANSPORT_PATCH);
|
||||
});
|
||||
});
|
||||
78
openclaw/extensions/mistral/api.ts
Normal file
78
openclaw/extensions/mistral/api.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
export { buildMistralProvider } from "./provider-catalog.js";
|
||||
export {
|
||||
buildMistralModelDefinition,
|
||||
MISTRAL_BASE_URL,
|
||||
MISTRAL_DEFAULT_MODEL_ID,
|
||||
} from "./model-definitions.js";
|
||||
export {
|
||||
applyMistralConfig,
|
||||
applyMistralProviderConfig,
|
||||
MISTRAL_DEFAULT_MODEL_REF,
|
||||
} from "./onboard.js";
|
||||
|
||||
const MISTRAL_MAX_TOKENS_FIELD = "max_tokens";
|
||||
|
||||
export const MISTRAL_MODEL_TRANSPORT_PATCH = {
|
||||
supportsStore: false,
|
||||
maxTokensField: MISTRAL_MAX_TOKENS_FIELD,
|
||||
} as const satisfies {
|
||||
supportsStore: boolean;
|
||||
maxTokensField: "max_tokens";
|
||||
};
|
||||
|
||||
export const MISTRAL_SMALL_LATEST_REASONING_EFFORT_MAP: Record<string, string> = {
|
||||
off: "none",
|
||||
minimal: "none",
|
||||
low: "high",
|
||||
medium: "high",
|
||||
high: "high",
|
||||
xhigh: "high",
|
||||
adaptive: "high",
|
||||
};
|
||||
|
||||
export const MISTRAL_SMALL_LATEST_ID = "mistral-small-latest";
|
||||
|
||||
export function resolveMistralCompatPatch(model: { id?: string }): {
|
||||
supportsStore: boolean;
|
||||
supportsReasoningEffort: boolean;
|
||||
maxTokensField: "max_tokens";
|
||||
reasoningEffortMap?: Record<string, string>;
|
||||
} {
|
||||
const reasoningEnabled = model.id === MISTRAL_SMALL_LATEST_ID;
|
||||
return {
|
||||
...MISTRAL_MODEL_TRANSPORT_PATCH,
|
||||
supportsReasoningEffort: reasoningEnabled,
|
||||
reasoningEffortMap: reasoningEnabled ? MISTRAL_SMALL_LATEST_REASONING_EFFORT_MAP : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function compatMatchesResolved(
|
||||
compat: Record<string, unknown> | undefined,
|
||||
modelId: string | undefined,
|
||||
): boolean {
|
||||
const expected = resolveMistralCompatPatch({ id: modelId });
|
||||
return (
|
||||
compat?.supportsStore === expected.supportsStore &&
|
||||
compat?.supportsReasoningEffort === expected.supportsReasoningEffort &&
|
||||
compat?.maxTokensField === expected.maxTokensField &&
|
||||
compat?.reasoningEffortMap === expected.reasoningEffortMap
|
||||
);
|
||||
}
|
||||
|
||||
export function applyMistralModelCompat<T extends { compat?: unknown; id?: string }>(model: T): T {
|
||||
const compat =
|
||||
model.compat && typeof model.compat === "object"
|
||||
? (model.compat as Record<string, unknown>)
|
||||
: undefined;
|
||||
if (compatMatchesResolved(compat, model.id)) {
|
||||
return model;
|
||||
}
|
||||
const patch = resolveMistralCompatPatch(model);
|
||||
return {
|
||||
...model,
|
||||
compat: {
|
||||
...compat,
|
||||
...patch,
|
||||
} as T extends { compat?: infer TCompat } ? TCompat : never,
|
||||
} as T;
|
||||
}
|
||||
52
openclaw/extensions/mistral/embedding-provider.ts
Normal file
52
openclaw/extensions/mistral/embedding-provider.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import {
|
||||
createRemoteEmbeddingProvider,
|
||||
normalizeEmbeddingModelWithPrefixes,
|
||||
resolveRemoteEmbeddingClient,
|
||||
type MemoryEmbeddingProvider,
|
||||
type MemoryEmbeddingProviderCreateOptions,
|
||||
} from "openclaw/plugin-sdk/memory-core-host-engine-embeddings";
|
||||
import type { SsrFPolicy } from "openclaw/plugin-sdk/ssrf-runtime";
|
||||
|
||||
export type MistralEmbeddingClient = {
|
||||
baseUrl: string;
|
||||
headers: Record<string, string>;
|
||||
ssrfPolicy?: SsrFPolicy;
|
||||
model: string;
|
||||
};
|
||||
|
||||
export const DEFAULT_MISTRAL_EMBEDDING_MODEL = "mistral-embed";
|
||||
const DEFAULT_MISTRAL_BASE_URL = "https://api.mistral.ai/v1";
|
||||
|
||||
export function normalizeMistralModel(model: string): string {
|
||||
return normalizeEmbeddingModelWithPrefixes({
|
||||
model,
|
||||
defaultModel: DEFAULT_MISTRAL_EMBEDDING_MODEL,
|
||||
prefixes: ["mistral/"],
|
||||
});
|
||||
}
|
||||
|
||||
export async function createMistralEmbeddingProvider(
|
||||
options: MemoryEmbeddingProviderCreateOptions,
|
||||
): Promise<{ provider: MemoryEmbeddingProvider; client: MistralEmbeddingClient }> {
|
||||
const client = await resolveMistralEmbeddingClient(options);
|
||||
|
||||
return {
|
||||
provider: createRemoteEmbeddingProvider({
|
||||
id: "mistral",
|
||||
client,
|
||||
errorPrefix: "mistral embeddings failed",
|
||||
}),
|
||||
client,
|
||||
};
|
||||
}
|
||||
|
||||
export async function resolveMistralEmbeddingClient(
|
||||
options: MemoryEmbeddingProviderCreateOptions,
|
||||
): Promise<MistralEmbeddingClient> {
|
||||
return await resolveRemoteEmbeddingClient({
|
||||
provider: "mistral",
|
||||
options,
|
||||
defaultBaseUrl: DEFAULT_MISTRAL_BASE_URL,
|
||||
normalizeModel: normalizeMistralModel,
|
||||
});
|
||||
}
|
||||
55
openclaw/extensions/mistral/index.ts
Normal file
55
openclaw/extensions/mistral/index.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import { defineSingleProviderPluginEntry } from "openclaw/plugin-sdk/provider-entry";
|
||||
import { applyMistralModelCompat } from "./api.js";
|
||||
import { mistralMediaUnderstandingProvider } from "./media-understanding-provider.js";
|
||||
import { mistralMemoryEmbeddingProviderAdapter } from "./memory-embedding-adapter.js";
|
||||
import { applyMistralConfig, MISTRAL_DEFAULT_MODEL_REF } from "./onboard.js";
|
||||
import { buildMistralProvider } from "./provider-catalog.js";
|
||||
import { contributeMistralResolvedModelCompat } from "./provider-compat.js";
|
||||
|
||||
const PROVIDER_ID = "mistral";
|
||||
export function buildMistralReplayPolicy() {
|
||||
return {
|
||||
sanitizeToolCallIds: true,
|
||||
toolCallIdMode: "strict9" as const,
|
||||
};
|
||||
}
|
||||
|
||||
export default defineSingleProviderPluginEntry({
|
||||
id: PROVIDER_ID,
|
||||
name: "Mistral Provider",
|
||||
description: "Bundled Mistral provider plugin",
|
||||
provider: {
|
||||
label: "Mistral",
|
||||
docsPath: "/providers/models",
|
||||
auth: [
|
||||
{
|
||||
methodId: "api-key",
|
||||
label: "Mistral API key",
|
||||
hint: "API key",
|
||||
optionKey: "mistralApiKey",
|
||||
flagName: "--mistral-api-key",
|
||||
envVar: "MISTRAL_API_KEY",
|
||||
promptMessage: "Enter Mistral API key",
|
||||
defaultModel: MISTRAL_DEFAULT_MODEL_REF,
|
||||
applyConfig: (cfg) => applyMistralConfig(cfg),
|
||||
wizard: {
|
||||
groupLabel: "Mistral AI",
|
||||
},
|
||||
},
|
||||
],
|
||||
catalog: {
|
||||
buildProvider: buildMistralProvider,
|
||||
allowExplicitBaseUrl: true,
|
||||
},
|
||||
matchesContextOverflowError: ({ errorMessage }) =>
|
||||
/\bmistral\b.*(?:input.*too long|token limit.*exceeded)/i.test(errorMessage),
|
||||
normalizeResolvedModel: ({ model }) => applyMistralModelCompat(model),
|
||||
contributeResolvedModelCompat: ({ modelId, model }) =>
|
||||
contributeMistralResolvedModelCompat({ modelId, model }),
|
||||
buildReplayPolicy: () => buildMistralReplayPolicy(),
|
||||
},
|
||||
register(api) {
|
||||
api.registerMemoryEmbeddingProvider(mistralMemoryEmbeddingProviderAdapter);
|
||||
api.registerMediaUnderstandingProvider(mistralMediaUnderstandingProvider);
|
||||
},
|
||||
});
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
createRequestCaptureJsonFetch,
|
||||
installPinnedHostnameTestHooks,
|
||||
} from "../../src/media-understanding/audio.test-helpers.ts";
|
||||
import { mistralMediaUnderstandingProvider } from "./media-understanding-provider.js";
|
||||
|
||||
installPinnedHostnameTestHooks();
|
||||
|
||||
describe("mistralMediaUnderstandingProvider", () => {
|
||||
it("has expected provider metadata", () => {
|
||||
expect(mistralMediaUnderstandingProvider.id).toBe("mistral");
|
||||
expect(mistralMediaUnderstandingProvider.capabilities).toEqual(["audio"]);
|
||||
expect(mistralMediaUnderstandingProvider.transcribeAudio).toBeDefined();
|
||||
});
|
||||
|
||||
it("uses Mistral base URL by default", async () => {
|
||||
const { fetchFn, getRequest } = createRequestCaptureJsonFetch({ text: "bonjour" });
|
||||
|
||||
const result = await mistralMediaUnderstandingProvider.transcribeAudio!({
|
||||
buffer: Buffer.from("audio-bytes"),
|
||||
fileName: "voice.ogg",
|
||||
apiKey: "test-mistral-key",
|
||||
timeoutMs: 5000,
|
||||
fetchFn,
|
||||
});
|
||||
|
||||
expect(getRequest().url).toBe("https://api.mistral.ai/v1/audio/transcriptions");
|
||||
expect(result.text).toBe("bonjour");
|
||||
});
|
||||
|
||||
it("allows overriding baseUrl", async () => {
|
||||
const { fetchFn, getRequest } = createRequestCaptureJsonFetch({ text: "ok" });
|
||||
|
||||
await mistralMediaUnderstandingProvider.transcribeAudio!({
|
||||
buffer: Buffer.from("audio"),
|
||||
fileName: "note.mp3",
|
||||
apiKey: "key",
|
||||
timeoutMs: 1000,
|
||||
baseUrl: "https://custom.mistral.example/v1",
|
||||
fetchFn,
|
||||
});
|
||||
|
||||
expect(getRequest().url).toBe("https://custom.mistral.example/v1/audio/transcriptions");
|
||||
});
|
||||
});
|
||||
21
openclaw/extensions/mistral/media-understanding-provider.ts
Normal file
21
openclaw/extensions/mistral/media-understanding-provider.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import {
|
||||
transcribeOpenAiCompatibleAudio,
|
||||
type MediaUnderstandingProvider,
|
||||
} from "openclaw/plugin-sdk/media-understanding";
|
||||
|
||||
const DEFAULT_MISTRAL_AUDIO_BASE_URL = "https://api.mistral.ai/v1";
|
||||
const DEFAULT_MISTRAL_AUDIO_MODEL = "voxtral-mini-latest";
|
||||
|
||||
export const mistralMediaUnderstandingProvider: MediaUnderstandingProvider = {
|
||||
id: "mistral",
|
||||
capabilities: ["audio"],
|
||||
defaultModels: { audio: DEFAULT_MISTRAL_AUDIO_MODEL },
|
||||
autoPriority: { audio: 50 },
|
||||
transcribeAudio: async (req) =>
|
||||
await transcribeOpenAiCompatibleAudio({
|
||||
...req,
|
||||
baseUrl: req.baseUrl ?? DEFAULT_MISTRAL_AUDIO_BASE_URL,
|
||||
defaultBaseUrl: DEFAULT_MISTRAL_AUDIO_BASE_URL,
|
||||
defaultModel: DEFAULT_MISTRAL_AUDIO_MODEL,
|
||||
}),
|
||||
};
|
||||
35
openclaw/extensions/mistral/memory-embedding-adapter.ts
Normal file
35
openclaw/extensions/mistral/memory-embedding-adapter.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import {
|
||||
isMissingEmbeddingApiKeyError,
|
||||
type MemoryEmbeddingProviderAdapter,
|
||||
} from "openclaw/plugin-sdk/memory-core-host-engine-embeddings";
|
||||
import {
|
||||
createMistralEmbeddingProvider,
|
||||
DEFAULT_MISTRAL_EMBEDDING_MODEL,
|
||||
} from "./embedding-provider.js";
|
||||
|
||||
export const mistralMemoryEmbeddingProviderAdapter: MemoryEmbeddingProviderAdapter = {
|
||||
id: "mistral",
|
||||
defaultModel: DEFAULT_MISTRAL_EMBEDDING_MODEL,
|
||||
transport: "remote",
|
||||
authProviderId: "mistral",
|
||||
autoSelectPriority: 50,
|
||||
allowExplicitWhenConfiguredAuto: true,
|
||||
shouldContinueAutoSelection: isMissingEmbeddingApiKeyError,
|
||||
create: async (options) => {
|
||||
const { provider, client } = await createMistralEmbeddingProvider({
|
||||
...options,
|
||||
provider: "mistral",
|
||||
fallback: "none",
|
||||
});
|
||||
return {
|
||||
provider,
|
||||
runtime: {
|
||||
id: "mistral",
|
||||
cacheKeyData: {
|
||||
provider: "mistral",
|
||||
model: client.model,
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
60
openclaw/extensions/mistral/model-definitions.test.ts
Normal file
60
openclaw/extensions/mistral/model-definitions.test.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildMistralCatalogModels,
|
||||
buildMistralModelDefinition,
|
||||
MISTRAL_DEFAULT_CONTEXT_WINDOW,
|
||||
MISTRAL_DEFAULT_COST,
|
||||
MISTRAL_DEFAULT_MAX_TOKENS,
|
||||
MISTRAL_DEFAULT_MODEL_ID,
|
||||
} from "./model-definitions.js";
|
||||
|
||||
describe("mistral model definitions", () => {
|
||||
it("uses current Pi pricing for the bundled default model", () => {
|
||||
expect(buildMistralModelDefinition()).toMatchObject({
|
||||
id: MISTRAL_DEFAULT_MODEL_ID,
|
||||
contextWindow: MISTRAL_DEFAULT_CONTEXT_WINDOW,
|
||||
maxTokens: MISTRAL_DEFAULT_MAX_TOKENS,
|
||||
cost: MISTRAL_DEFAULT_COST,
|
||||
});
|
||||
|
||||
expect(MISTRAL_DEFAULT_COST).toEqual({
|
||||
input: 0.5,
|
||||
output: 1.5,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("publishes a curated set of current Mistral catalog models", () => {
|
||||
expect(buildMistralCatalogModels()).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: "codestral-latest",
|
||||
input: ["text"],
|
||||
contextWindow: 256000,
|
||||
maxTokens: 4096,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "magistral-small",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 40000,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "mistral-small-latest",
|
||||
reasoning: true,
|
||||
input: ["text", "image"],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 16384,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "pixtral-large-latest",
|
||||
input: ["text", "image"],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 32768,
|
||||
}),
|
||||
]),
|
||||
);
|
||||
});
|
||||
});
|
||||
99
openclaw/extensions/mistral/model-definitions.ts
Normal file
99
openclaw/extensions/mistral/model-definitions.ts
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
import type { ModelDefinitionConfig } from "openclaw/plugin-sdk/provider-model-shared";
|
||||
|
||||
export const MISTRAL_BASE_URL = "https://api.mistral.ai/v1";
|
||||
export const MISTRAL_DEFAULT_MODEL_ID = "mistral-large-latest";
|
||||
export const MISTRAL_DEFAULT_MODEL_REF = `mistral/${MISTRAL_DEFAULT_MODEL_ID}`;
|
||||
export const MISTRAL_DEFAULT_CONTEXT_WINDOW = 262144;
|
||||
export const MISTRAL_DEFAULT_MAX_TOKENS = 16384;
|
||||
export const MISTRAL_DEFAULT_COST = {
|
||||
input: 0.5,
|
||||
output: 1.5,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
};
|
||||
|
||||
const MISTRAL_MODEL_CATALOG = [
|
||||
{
|
||||
id: "codestral-latest",
|
||||
name: "Codestral (latest)",
|
||||
reasoning: false,
|
||||
input: ["text"],
|
||||
cost: { input: 0.3, output: 0.9, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 256000,
|
||||
maxTokens: 4096,
|
||||
},
|
||||
{
|
||||
id: "devstral-medium-latest",
|
||||
name: "Devstral 2 (latest)",
|
||||
reasoning: false,
|
||||
input: ["text"],
|
||||
cost: { input: 0.4, output: 2, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 262144,
|
||||
maxTokens: 32768,
|
||||
},
|
||||
{
|
||||
id: "magistral-small",
|
||||
name: "Magistral Small",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: { input: 0.5, output: 1.5, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 128000,
|
||||
maxTokens: 40000,
|
||||
},
|
||||
{
|
||||
id: "mistral-large-latest",
|
||||
name: "Mistral Large (latest)",
|
||||
reasoning: false,
|
||||
input: ["text", "image"],
|
||||
cost: MISTRAL_DEFAULT_COST,
|
||||
contextWindow: MISTRAL_DEFAULT_CONTEXT_WINDOW,
|
||||
maxTokens: MISTRAL_DEFAULT_MAX_TOKENS,
|
||||
},
|
||||
{
|
||||
id: "mistral-medium-2508",
|
||||
name: "Mistral Medium 3.1",
|
||||
reasoning: false,
|
||||
input: ["text", "image"],
|
||||
cost: { input: 0.4, output: 2, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 262144,
|
||||
maxTokens: 8192,
|
||||
},
|
||||
{
|
||||
id: "mistral-small-latest",
|
||||
name: "Mistral Small (latest)",
|
||||
reasoning: true,
|
||||
input: ["text", "image"],
|
||||
cost: { input: 0.1, output: 0.3, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 128000,
|
||||
maxTokens: 16384,
|
||||
},
|
||||
{
|
||||
id: "pixtral-large-latest",
|
||||
name: "Pixtral Large (latest)",
|
||||
reasoning: false,
|
||||
input: ["text", "image"],
|
||||
cost: { input: 2, output: 6, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 128000,
|
||||
maxTokens: 32768,
|
||||
},
|
||||
] as const satisfies readonly ModelDefinitionConfig[];
|
||||
|
||||
export function buildMistralModelDefinition(): ModelDefinitionConfig {
|
||||
return (
|
||||
MISTRAL_MODEL_CATALOG.find((model) => model.id === MISTRAL_DEFAULT_MODEL_ID) ?? {
|
||||
id: MISTRAL_DEFAULT_MODEL_ID,
|
||||
name: "Mistral Large",
|
||||
reasoning: false,
|
||||
input: ["text", "image"],
|
||||
cost: MISTRAL_DEFAULT_COST,
|
||||
contextWindow: MISTRAL_DEFAULT_CONTEXT_WINDOW,
|
||||
maxTokens: MISTRAL_DEFAULT_MAX_TOKENS,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export function buildMistralCatalogModels(): ModelDefinitionConfig[] {
|
||||
return MISTRAL_MODEL_CATALOG.map((model) =>
|
||||
Object.assign({}, model, { input: [...model.input] }),
|
||||
);
|
||||
}
|
||||
60
openclaw/extensions/mistral/onboard.test.ts
Normal file
60
openclaw/extensions/mistral/onboard.test.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
expectProviderOnboardMergedLegacyConfig,
|
||||
expectProviderOnboardPrimaryAndFallbacks,
|
||||
} from "../../test/helpers/plugins/provider-onboard.js";
|
||||
import { buildMistralModelDefinition as buildBundledMistralModelDefinition } from "./model-definitions.js";
|
||||
import {
|
||||
applyMistralConfig,
|
||||
applyMistralProviderConfig,
|
||||
MISTRAL_DEFAULT_MODEL_REF,
|
||||
} from "./onboard.js";
|
||||
|
||||
describe("mistral onboard", () => {
|
||||
it("adds Mistral provider with correct settings", () => {
|
||||
const cfg = applyMistralConfig({});
|
||||
expect(cfg.models?.providers?.mistral).toMatchObject({
|
||||
baseUrl: "https://api.mistral.ai/v1",
|
||||
api: "openai-completions",
|
||||
});
|
||||
expectProviderOnboardPrimaryAndFallbacks({
|
||||
applyConfig: applyMistralConfig,
|
||||
modelRef: MISTRAL_DEFAULT_MODEL_REF,
|
||||
});
|
||||
});
|
||||
|
||||
it("merges Mistral models and keeps existing provider overrides", () => {
|
||||
const provider = expectProviderOnboardMergedLegacyConfig({
|
||||
applyProviderConfig: applyMistralProviderConfig,
|
||||
providerId: "mistral",
|
||||
providerApi: "openai-completions",
|
||||
baseUrl: "https://api.mistral.ai/v1",
|
||||
legacyApi: "anthropic-messages",
|
||||
legacyModelId: "custom-model",
|
||||
legacyModelName: "Custom",
|
||||
});
|
||||
expect(provider?.models.map((m) => m.id)).toEqual(["custom-model", "mistral-large-latest"]);
|
||||
const mistralDefault = provider?.models.find((model) => model.id === "mistral-large-latest");
|
||||
expect(mistralDefault?.contextWindow).toBe(262144);
|
||||
expect(mistralDefault?.maxTokens).toBe(16384);
|
||||
});
|
||||
|
||||
it("uses the bundled mistral default model definition", () => {
|
||||
const bundled = buildBundledMistralModelDefinition();
|
||||
const cfg = applyMistralProviderConfig({});
|
||||
const defaultModel = cfg.models?.providers?.mistral?.models.find(
|
||||
(model) => model.id === bundled.id,
|
||||
);
|
||||
|
||||
expect(defaultModel).toMatchObject({
|
||||
id: bundled.id,
|
||||
contextWindow: bundled.contextWindow,
|
||||
maxTokens: bundled.maxTokens,
|
||||
});
|
||||
});
|
||||
|
||||
it("adds the expected alias for the default model", () => {
|
||||
const cfg = applyMistralProviderConfig({});
|
||||
expect(cfg.agents?.defaults?.models?.[MISTRAL_DEFAULT_MODEL_REF]?.alias).toBe("Mistral");
|
||||
});
|
||||
});
|
||||
31
openclaw/extensions/mistral/onboard.ts
Normal file
31
openclaw/extensions/mistral/onboard.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import {
|
||||
createDefaultModelPresetAppliers,
|
||||
type OpenClawConfig,
|
||||
} from "openclaw/plugin-sdk/provider-onboard";
|
||||
import {
|
||||
buildMistralModelDefinition,
|
||||
MISTRAL_BASE_URL,
|
||||
MISTRAL_DEFAULT_MODEL_ID,
|
||||
} from "./model-definitions.js";
|
||||
|
||||
export const MISTRAL_DEFAULT_MODEL_REF = `mistral/${MISTRAL_DEFAULT_MODEL_ID}`;
|
||||
|
||||
const mistralPresetAppliers = createDefaultModelPresetAppliers({
|
||||
primaryModelRef: MISTRAL_DEFAULT_MODEL_REF,
|
||||
resolveParams: (_cfg: OpenClawConfig) => ({
|
||||
providerId: "mistral",
|
||||
api: "openai-completions",
|
||||
baseUrl: MISTRAL_BASE_URL,
|
||||
defaultModel: buildMistralModelDefinition(),
|
||||
defaultModelId: MISTRAL_DEFAULT_MODEL_ID,
|
||||
aliases: [{ modelRef: MISTRAL_DEFAULT_MODEL_REF, alias: "Mistral" }],
|
||||
}),
|
||||
});
|
||||
|
||||
export function applyMistralProviderConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||
return mistralPresetAppliers.applyProviderConfig(cfg);
|
||||
}
|
||||
|
||||
export function applyMistralConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||
return mistralPresetAppliers.applyConfig(cfg);
|
||||
}
|
||||
32
openclaw/extensions/mistral/openclaw.plugin.json
Normal file
32
openclaw/extensions/mistral/openclaw.plugin.json
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"id": "mistral",
|
||||
"enabledByDefault": true,
|
||||
"providers": ["mistral"],
|
||||
"providerAuthEnvVars": {
|
||||
"mistral": ["MISTRAL_API_KEY"]
|
||||
},
|
||||
"providerAuthChoices": [
|
||||
{
|
||||
"provider": "mistral",
|
||||
"method": "api-key",
|
||||
"choiceId": "mistral-api-key",
|
||||
"choiceLabel": "Mistral API key",
|
||||
"groupId": "mistral",
|
||||
"groupLabel": "Mistral AI",
|
||||
"groupHint": "API key",
|
||||
"optionKey": "mistralApiKey",
|
||||
"cliFlag": "--mistral-api-key",
|
||||
"cliOption": "--mistral-api-key <key>",
|
||||
"cliDescription": "Mistral API key"
|
||||
}
|
||||
],
|
||||
"contracts": {
|
||||
"memoryEmbeddingProviders": ["mistral"],
|
||||
"mediaUnderstandingProviders": ["mistral"]
|
||||
},
|
||||
"configSchema": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"properties": {}
|
||||
}
|
||||
}
|
||||
15
openclaw/extensions/mistral/package.json
Normal file
15
openclaw/extensions/mistral/package.json
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"name": "@openclaw/mistral-provider",
|
||||
"version": "2026.4.20",
|
||||
"private": true,
|
||||
"description": "OpenClaw Mistral provider plugin",
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@openclaw/plugin-sdk": "workspace:*"
|
||||
},
|
||||
"openclaw": {
|
||||
"extensions": [
|
||||
"./index.ts"
|
||||
]
|
||||
}
|
||||
}
|
||||
10
openclaw/extensions/mistral/provider-catalog.ts
Normal file
10
openclaw/extensions/mistral/provider-catalog.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import type { ModelProviderConfig } from "openclaw/plugin-sdk/provider-model-shared";
|
||||
import { buildMistralCatalogModels, MISTRAL_BASE_URL } from "./model-definitions.js";
|
||||
|
||||
export function buildMistralProvider(): ModelProviderConfig {
|
||||
return {
|
||||
baseUrl: MISTRAL_BASE_URL,
|
||||
api: "openai-completions",
|
||||
models: buildMistralCatalogModels(),
|
||||
};
|
||||
}
|
||||
59
openclaw/extensions/mistral/provider-compat.ts
Normal file
59
openclaw/extensions/mistral/provider-compat.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { resolveProviderRequestCapabilities } from "openclaw/plugin-sdk/provider-http";
|
||||
import { normalizeLowercaseStringOrEmpty, readStringValue } from "openclaw/plugin-sdk/text-runtime";
|
||||
import { MISTRAL_MODEL_TRANSPORT_PATCH } from "./api.js";
|
||||
|
||||
const MISTRAL_MODEL_HINTS = [
|
||||
"mistral",
|
||||
"mistralai",
|
||||
"mixtral",
|
||||
"codestral",
|
||||
"pixtral",
|
||||
"devstral",
|
||||
"ministral",
|
||||
] as const;
|
||||
|
||||
function isMistralModelHint(modelId: string): boolean {
|
||||
const normalized = normalizeLowercaseStringOrEmpty(modelId);
|
||||
return MISTRAL_MODEL_HINTS.some(
|
||||
(hint) =>
|
||||
normalized === hint ||
|
||||
normalized.startsWith(`${hint}/`) ||
|
||||
normalized.startsWith(`${hint}-`) ||
|
||||
normalized.startsWith(`${hint}:`),
|
||||
);
|
||||
}
|
||||
|
||||
export function shouldContributeMistralCompat(params: {
|
||||
modelId: string;
|
||||
model: { api?: unknown; baseUrl?: unknown; provider?: unknown; compat?: unknown };
|
||||
}): boolean {
|
||||
if (params.model.api !== "openai-completions") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const capabilities = resolveProviderRequestCapabilities({
|
||||
provider: readStringValue(params.model.provider),
|
||||
api: "openai-completions",
|
||||
baseUrl: readStringValue(params.model.baseUrl),
|
||||
capability: "llm",
|
||||
transport: "stream",
|
||||
modelId: params.modelId,
|
||||
compat:
|
||||
params.model.compat && typeof params.model.compat === "object"
|
||||
? (params.model.compat as { supportsStore?: boolean })
|
||||
: undefined,
|
||||
});
|
||||
|
||||
return (
|
||||
capabilities.knownProviderFamily === "mistral" ||
|
||||
capabilities.endpointClass === "mistral-public" ||
|
||||
isMistralModelHint(params.modelId)
|
||||
);
|
||||
}
|
||||
|
||||
export function contributeMistralResolvedModelCompat(params: {
|
||||
modelId: string;
|
||||
model: { api?: unknown; baseUrl?: unknown; provider?: unknown; compat?: unknown };
|
||||
}) {
|
||||
return shouldContributeMistralCompat(params) ? MISTRAL_MODEL_TRANSPORT_PATCH : undefined;
|
||||
}
|
||||
1
openclaw/extensions/mistral/test-api.ts
Normal file
1
openclaw/extensions/mistral/test-api.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export { mistralMediaUnderstandingProvider } from "./media-understanding-provider.js";
|
||||
16
openclaw/extensions/mistral/tsconfig.json
Normal file
16
openclaw/extensions/mistral/tsconfig.json
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"extends": "../tsconfig.package-boundary.base.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "."
|
||||
},
|
||||
"include": ["./*.ts", "./src/**/*.ts"],
|
||||
"exclude": [
|
||||
"./**/*.test.ts",
|
||||
"./dist/**",
|
||||
"./node_modules/**",
|
||||
"./src/test-support/**",
|
||||
"./src/**/*test-helpers.ts",
|
||||
"./src/**/*test-harness.ts",
|
||||
"./src/**/*test-support.ts"
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue