File size: 2,559 Bytes
6e38ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { ModelConfigSchema } from "./tabs/agentSettings/modelSelector/modelConfigForms/types";
import { MCPAgentConfigSchema } from "../features/McpServersConfig/types";
import { GeneralSettingsSchema } from "./types";

export function extractZodErrors(error: any): string[] {
  if (!error.errors) return [error.message || String(error)];
  return error.errors.map((e: any) => {
    const path = e.path.length ? `${e.path.join(".")}: ` : "";
    return `${path}${e.message}`;
  });
}

export function validateGeneralSettings(config: any): string[] {
  try {
    GeneralSettingsSchema.parse(config);
    return [];
  } catch (e) {
    return extractZodErrors(e);
  }
}

export function validateMCPAgentsSettings(agents: any[]): string[] {
  const errors: string[] = [];
  if (!Array.isArray(agents)) {
    return ["Agents must be an array."];
  }
  for (let i = 0; i < agents.length; i++) {
    try {
      MCPAgentConfigSchema.parse(agents[i]);
    } catch (e) {
      extractZodErrors(e).forEach((msg) => errors.push(`Agent #${i + 1}: ${msg}`));
    }
  }
  return errors;
}

export function validateAdvancedConfigEditor(editorValue: string, isValidany: (obj: any) => boolean): string[] {
  const errors: string[] = [];
  try {
    const yaml = require('js-yaml');
    const parsed = yaml.load(editorValue || "");
    if (!parsed || typeof parsed !== "object") {
      errors.push("Config is empty or not an object.");
    } else if (!isValidany(parsed)) {
      errors.push("Config is not a valid any.");
    }
  } catch (e) {
    errors.push("Config YAML is invalid.");
  }
  return errors;
}

export function validateModelConfig(config: any): string[] {
  try {
    ModelConfigSchema.parse(config);
    return [];
  } catch (e) {
    return extractZodErrors(e);
  }
}

export function validateModelConfigSettings(modelClientConfigs: Record<string, any> | undefined, requiredKeys: string[]): string[] {
  const errors: string[] = [];
  if (!modelClientConfigs || typeof modelClientConfigs !== 'object') {
    errors.push('Model client configs are missing or invalid.');
    return errors;
  }
  for (const key of requiredKeys) {
    if (!modelClientConfigs[key]) {
      errors.push(`${key}: missing`);
    } else {
      const err = validateModelConfig(modelClientConfigs[key]);
      if (err.length > 0) {
        errors.push(`${key}: ${err.join('; ')}`);
      }
    }
  }
  return errors;
}

export function validateAll(config: any): string[] {
  let errors: string[] = [];
  errors = errors.concat(validateGeneralSettings(config));
  return errors;
}