• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

graphty-org / graphty-monorepo / 20661584252

02 Jan 2026 03:50PM UTC coverage: 77.924% (+7.3%) from 70.62%
20661584252

push

github

apowers313
ci: fix flakey performance test

13438 of 17822 branches covered (75.4%)

Branch coverage included in aggregate %.

41247 of 52355 relevant lines covered (78.78%)

145534.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.13
/graphty-element/src/ai/commands/AlgorithmCommands.ts
1
/**
1✔
2
 * AlgorithmCommands - Commands for running and listing graph algorithms.
3
 * @module ai/commands/AlgorithmCommands
4
 */
5

6
import { z } from "zod";
9✔
7

8
import { Algorithm } from "../../algorithms/Algorithm";
9✔
9
import type { Graph } from "../../Graph";
10
import type { CommandResult, GraphCommand } from "./types";
11

12
/**
13
 * Schema for runAlgorithm parameters.
14
 */
15
const runAlgorithmParamsSchema = z.object({
9✔
16
    namespace: z.string().describe("The namespace of the algorithm (e.g., 'graphty')"),
9✔
17
    type: z.string().describe("The type of algorithm to run (e.g., 'degree', 'pagerank')"),
9✔
18
});
9✔
19

20
/**
21
 * Schema for listAlgorithms parameters.
22
 */
23
const listAlgorithmsParamsSchema = z.object({
9✔
24
    namespace: z.string().optional().describe("Optional namespace to filter algorithms by"),
9✔
25
});
9✔
26

27
/**
28
 * Run a graph algorithm.
29
 */
30
export const runAlgorithm: GraphCommand = {
9✔
31
    name: "runAlgorithm",
9✔
32
    description:
9✔
33
        "Run a graph algorithm to analyze the graph structure. Available algorithms include degree centrality, pagerank, and others that compute metrics for nodes and edges.",
9✔
34
    parameters: runAlgorithmParamsSchema,
9✔
35
    examples: [
9✔
36
        {
9✔
37
            input: "Calculate the degree of each node",
9✔
38
            params: { namespace: "graphty", type: "degree" },
9✔
39
        },
9✔
40
        {
9✔
41
            input: "Run pagerank algorithm",
9✔
42
            params: { namespace: "graphty", type: "pagerank" },
9✔
43
        },
9✔
44
        {
9✔
45
            input: "Compute centrality metrics",
9✔
46
            params: { namespace: "graphty", type: "degree" },
9✔
47
        },
9✔
48
    ],
9✔
49

50
    async execute(graph: Graph, params: Record<string, unknown>): Promise<CommandResult> {
9✔
51
        const parsed = runAlgorithmParamsSchema.safeParse(params);
4✔
52
        if (!parsed.success) {
4!
53
            return {
×
54
                success: false,
×
55
                message: `Invalid parameters: ${parsed.error.message}`,
×
56
            };
×
57
        }
×
58

59
        const { namespace, type } = parsed.data;
4✔
60

61
        try {
4✔
62
            // Check if algorithm exists
63
            const alg = Algorithm.get(graph, namespace, type);
4✔
64
            if (!alg) {
4✔
65
                const available = Algorithm.getRegisteredAlgorithms(namespace);
2✔
66
                const errorMessage =
2✔
67
                    available.length > 0
2✔
68
                        ? `Available in namespace '${namespace}': ${available.map((a) => a.split(":")[1]).join(", ")}`
1✔
69
                        : `No algorithms found in namespace '${namespace}'. Use listAlgorithms to see available options.`;
1✔
70
                return {
2✔
71
                    success: false,
2✔
72
                    message: `Algorithm not found: ${namespace}:${type}. ${errorMessage}`,
2✔
73
                };
2✔
74
            }
2✔
75

76
            // Run the algorithm
77
            await graph.runAlgorithm(namespace, type);
2✔
78

79
            return {
2✔
80
                success: true,
2✔
81
                message: `Successfully ran ${namespace}:${type} algorithm. Results are now available in node data.`,
2✔
82
                data: {
2✔
83
                    namespace,
2✔
84
                    type,
2✔
85
                    nodeCount: graph.getNodeCount(),
2✔
86
                },
2✔
87
            };
2✔
88
        } catch (error) {
4!
89
            return {
×
90
                success: false,
×
91
                message: `Failed to run algorithm ${namespace}:${type}: ${error instanceof Error ? error.message : String(error)}`,
×
92
            };
×
93
        }
×
94
    },
4✔
95
};
9✔
96

97
/**
98
 * List available graph algorithms.
99
 */
100
export const listAlgorithms: GraphCommand = {
9✔
101
    name: "listAlgorithms",
9✔
102
    description: "List all available graph algorithms that can be run on the graph. Optionally filter by namespace.",
9✔
103
    parameters: listAlgorithmsParamsSchema,
9✔
104
    examples: [
9✔
105
        {
9✔
106
            input: "What algorithms are available?",
9✔
107
            params: {},
9✔
108
        },
9✔
109
        {
9✔
110
            input: "List graphty algorithms",
9✔
111
            params: { namespace: "graphty" },
9✔
112
        },
9✔
113
        {
9✔
114
            input: "Show me the available analysis tools",
9✔
115
            params: {},
9✔
116
        },
9✔
117
    ],
9✔
118

119
    // eslint-disable-next-line @typescript-eslint/require-await
120
    async execute(_graph: Graph, params: Record<string, unknown>): Promise<CommandResult> {
9✔
121
        const parsed = listAlgorithmsParamsSchema.safeParse(params);
3✔
122
        if (!parsed.success) {
3!
123
            return {
×
124
                success: false,
×
125
                message: `Invalid parameters: ${parsed.error.message}`,
×
126
            };
×
127
        }
×
128

129
        const { namespace } = parsed.data;
3✔
130

131
        try {
3✔
132
            const algorithms = Algorithm.getRegisteredAlgorithms(namespace);
3✔
133

134
            if (algorithms.length === 0) {
3!
135
                const noAlgMessage = namespace
×
136
                    ? `No algorithms found in namespace '${namespace}'.`
×
137
                    : "No algorithms are currently registered.";
×
138
                return {
×
139
                    success: true,
×
140
                    message: noAlgMessage,
×
141
                    data: {
×
142
                        algorithms: [],
×
143
                        count: 0,
×
144
                    },
×
145
                };
×
146
            }
×
147

148
            const message = namespace
3✔
149
                ? `Found ${algorithms.length} algorithm(s) in namespace '${namespace}': ${algorithms.join(", ")}`
1✔
150
                : `Found ${algorithms.length} available algorithm(s): ${algorithms.join(", ")}`;
2✔
151

152
            return {
3✔
153
                success: true,
3✔
154
                message,
3✔
155
                data: {
3✔
156
                    algorithms,
3✔
157
                    count: algorithms.length,
3✔
158
                },
3✔
159
            };
3✔
160
        } catch (error) {
3!
161
            return {
×
162
                success: false,
×
163
                message: `Failed to list algorithms: ${error instanceof Error ? error.message : String(error)}`,
×
164
            };
×
165
        }
×
166
    },
3✔
167
};
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc