2025-07-05 11:34:28 -04:00
|
|
|
import { z } from 'zod';
|
2025-08-11 14:26:28 -04:00
|
|
|
import { ViolationTypes, ErrorTypes } from 'librechat-data-provider';
|
|
|
|
|
import type { Agent, TModelsConfig } from 'librechat-data-provider';
|
|
|
|
|
import type { Request, Response } from 'express';
|
2025-07-05 11:34:28 -04:00
|
|
|
|
|
|
|
|
/** Avatar schema shared between create and update */
|
|
|
|
|
export const agentAvatarSchema = z.object({
|
|
|
|
|
filepath: z.string(),
|
|
|
|
|
source: z.string(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/** Base resource schema for tool resources */
|
|
|
|
|
export const agentBaseResourceSchema = z.object({
|
|
|
|
|
file_ids: z.array(z.string()).optional(),
|
|
|
|
|
files: z.array(z.any()).optional(), // Files are populated at runtime, not from user input
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/** File resource schema extends base with vector_store_ids */
|
|
|
|
|
export const agentFileResourceSchema = agentBaseResourceSchema.extend({
|
|
|
|
|
vector_store_ids: z.array(z.string()).optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/** Tool resources schema matching AgentToolResources interface */
|
|
|
|
|
export const agentToolResourcesSchema = z
|
|
|
|
|
.object({
|
|
|
|
|
image_edit: agentBaseResourceSchema.optional(),
|
|
|
|
|
execute_code: agentBaseResourceSchema.optional(),
|
|
|
|
|
file_search: agentFileResourceSchema.optional(),
|
|
|
|
|
ocr: agentBaseResourceSchema.optional(),
|
|
|
|
|
})
|
|
|
|
|
.optional();
|
|
|
|
|
|
|
|
|
|
/** Base agent schema with all common fields */
|
|
|
|
|
export const agentBaseSchema = z.object({
|
|
|
|
|
name: z.string().nullable().optional(),
|
|
|
|
|
description: z.string().nullable().optional(),
|
|
|
|
|
instructions: z.string().nullable().optional(),
|
|
|
|
|
avatar: agentAvatarSchema.nullable().optional(),
|
|
|
|
|
model_parameters: z.record(z.unknown()).optional(),
|
|
|
|
|
tools: z.array(z.string()).optional(),
|
|
|
|
|
agent_ids: z.array(z.string()).optional(),
|
|
|
|
|
end_after_tools: z.boolean().optional(),
|
|
|
|
|
hide_sequential_outputs: z.boolean().optional(),
|
|
|
|
|
artifacts: z.string().optional(),
|
|
|
|
|
recursion_limit: z.number().optional(),
|
|
|
|
|
conversation_starters: z.array(z.string()).optional(),
|
|
|
|
|
tool_resources: agentToolResourcesSchema,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/** Create schema extends base with required fields for creation */
|
|
|
|
|
export const agentCreateSchema = agentBaseSchema.extend({
|
|
|
|
|
provider: z.string(),
|
|
|
|
|
model: z.string().nullable(),
|
|
|
|
|
tools: z.array(z.string()).optional().default([]),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/** Update schema extends base with all fields optional and additional update-only fields */
|
|
|
|
|
export const agentUpdateSchema = agentBaseSchema.extend({
|
|
|
|
|
provider: z.string().optional(),
|
|
|
|
|
model: z.string().nullable().optional(),
|
|
|
|
|
projectIds: z.array(z.string()).optional(),
|
|
|
|
|
removeProjectIds: z.array(z.string()).optional(),
|
|
|
|
|
isCollaborative: z.boolean().optional(),
|
|
|
|
|
});
|
2025-08-11 14:26:28 -04:00
|
|
|
|
|
|
|
|
interface ValidateAgentModelParams {
|
|
|
|
|
req: Request;
|
|
|
|
|
res: Response;
|
|
|
|
|
agent: Agent;
|
|
|
|
|
modelsConfig: TModelsConfig;
|
|
|
|
|
logViolation: (
|
|
|
|
|
req: Request,
|
|
|
|
|
res: Response,
|
|
|
|
|
type: string,
|
|
|
|
|
errorMessage: Record<string, unknown>,
|
|
|
|
|
score?: number | string,
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ValidateAgentModelResult {
|
|
|
|
|
isValid: boolean;
|
|
|
|
|
error?: {
|
|
|
|
|
message: string;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validates an agent's model against the available models configuration.
|
|
|
|
|
* This is a non-middleware version of validateModel that can be used
|
|
|
|
|
* in service initialization flows.
|
|
|
|
|
*
|
|
|
|
|
* @param params - Validation parameters
|
|
|
|
|
* @returns Object indicating whether the model is valid and any error details
|
|
|
|
|
*/
|
|
|
|
|
export async function validateAgentModel(
|
|
|
|
|
params: ValidateAgentModelParams,
|
|
|
|
|
): Promise<ValidateAgentModelResult> {
|
|
|
|
|
const { req, res, agent, modelsConfig, logViolation } = params;
|
|
|
|
|
const { model, provider: endpoint } = agent;
|
|
|
|
|
|
|
|
|
|
if (!model) {
|
|
|
|
|
return {
|
|
|
|
|
isValid: false,
|
|
|
|
|
error: {
|
|
|
|
|
message: `{ "type": "${ErrorTypes.MISSING_MODEL}", "info": "${endpoint}" }`,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!modelsConfig) {
|
|
|
|
|
return {
|
|
|
|
|
isValid: false,
|
|
|
|
|
error: {
|
|
|
|
|
message: `{ "type": "${ErrorTypes.MODELS_NOT_LOADED}" }`,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const availableModels = modelsConfig[endpoint];
|
|
|
|
|
if (!availableModels) {
|
|
|
|
|
return {
|
|
|
|
|
isValid: false,
|
|
|
|
|
error: {
|
|
|
|
|
message: `{ "type": "${ErrorTypes.ENDPOINT_MODELS_NOT_LOADED}", "info": "${endpoint}" }`,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validModel = !!availableModels.find((availableModel) => availableModel === model);
|
|
|
|
|
|
|
|
|
|
if (validModel) {
|
|
|
|
|
return { isValid: true };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { ILLEGAL_MODEL_REQ_SCORE: score = 1 } = process.env ?? {};
|
|
|
|
|
const type = ViolationTypes.ILLEGAL_MODEL_REQUEST;
|
|
|
|
|
const errorMessage = {
|
|
|
|
|
type,
|
|
|
|
|
model,
|
|
|
|
|
endpoint,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await logViolation(req, res, type, errorMessage, score);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
isValid: false,
|
|
|
|
|
error: {
|
|
|
|
|
message: `{ "type": "${ViolationTypes.ILLEGAL_MODEL_REQUEST}", "info": "${endpoint}|${model}" }`,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|