File size: 1,949 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { AgentState, VersionRecord } from './types';

export const isActiveVersion = (
  version: VersionRecord,
  currentAgent: AgentState,
  versions: VersionRecord[],
): boolean => {
  if (!versions || versions.length === 0) {
    return false;
  }

  if (!currentAgent) {
    const versionIndex = versions.findIndex(
      (v) =>
        v.name === version.name &&
        v.instructions === version.instructions &&
        v.artifacts === version.artifacts,
    );
    return versionIndex === 0;
  }

  const matchesName = version.name === currentAgent.name;
  const matchesDescription = version.description === currentAgent.description;
  const matchesInstructions = version.instructions === currentAgent.instructions;
  const matchesArtifacts = version.artifacts === currentAgent.artifacts;

  const toolsMatch = () => {
    if (!version.tools && !currentAgent.tools) return true;
    if (!version.tools || !currentAgent.tools) return false;
    if (version.tools.length !== currentAgent.tools.length) return false;

    const sortedVersionTools = [...version.tools].sort();
    const sortedCurrentTools = [...currentAgent.tools].sort();

    return sortedVersionTools.every((tool, i) => tool === sortedCurrentTools[i]);
  };

  const capabilitiesMatch = () => {
    if (!version.capabilities && !currentAgent.capabilities) return true;
    if (!version.capabilities || !currentAgent.capabilities) return false;
    if (version.capabilities.length !== currentAgent.capabilities.length) return false;

    const sortedVersionCapabilities = [...version.capabilities].sort();
    const sortedCurrentCapabilities = [...currentAgent.capabilities].sort();

    return sortedVersionCapabilities.every(
      (capability, i) => capability === sortedCurrentCapabilities[i],
    );
  };

  return (
    matchesName &&
    matchesDescription &&
    matchesInstructions &&
    matchesArtifacts &&
    toolsMatch() &&
    capabilitiesMatch()
  );
};