File size: 7,811 Bytes
07af8f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { Request, Response } from 'express';
import { ApiResponse, CloudServer, CloudTool } from '../types/index.js';
import {
  getCloudServers,
  getCloudServerByName,
  getCloudServerTools,
  callCloudServerTool,
  getCloudCategories,
  getCloudTags,
  searchCloudServers,
  filterCloudServersByCategory,
  filterCloudServersByTag,
} from '../services/cloudService.js';

// Get all cloud market servers
export const getAllCloudServers = async (_: Request, res: Response): Promise<void> => {
  try {
    const cloudServers = await getCloudServers();
    const response: ApiResponse<CloudServer[]> = {
      success: true,
      data: cloudServers,
    };
    res.json(response);
  } catch (error) {
    console.error('Error getting cloud market servers:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to get cloud market servers';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

// Get a specific cloud market server by name
export const getCloudServer = async (req: Request, res: Response): Promise<void> => {
  try {
    const { name } = req.params;
    if (!name) {
      res.status(400).json({
        success: false,
        message: 'Server name is required',
      });
      return;
    }

    const server = await getCloudServerByName(name);
    if (!server) {
      res.status(404).json({
        success: false,
        message: 'Cloud server not found',
      });
      return;
    }

    // Fetch tools for this server
    try {
      const tools = await getCloudServerTools(server.server_key);
      server.tools = tools;
    } catch (toolError) {
      console.warn(`Failed to fetch tools for server ${server.name}:`, toolError);
      // Continue without tools
    }

    const response: ApiResponse<CloudServer> = {
      success: true,
      data: server,
    };
    res.json(response);
  } catch (error) {
    console.error('Error getting cloud market server:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to get cloud market server';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

// Get all cloud market categories
export const getAllCloudCategories = async (_: Request, res: Response): Promise<void> => {
  try {
    const categories = await getCloudCategories();
    const response: ApiResponse<string[]> = {
      success: true,
      data: categories,
    };
    res.json(response);
  } catch (error) {
    console.error('Error getting cloud market categories:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to get cloud market categories';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

// Get all cloud market tags
export const getAllCloudTags = async (_: Request, res: Response): Promise<void> => {
  try {
    const tags = await getCloudTags();
    const response: ApiResponse<string[]> = {
      success: true,
      data: tags,
    };
    res.json(response);
  } catch (error) {
    console.error('Error getting cloud market tags:', error);
    const errorMessage = error instanceof Error ? error.message : 'Failed to get cloud market tags';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

// Search cloud market servers
export const searchCloudServersByQuery = async (req: Request, res: Response): Promise<void> => {
  try {
    const query = req.query.query as string;
    if (!query || query.trim() === '') {
      res.status(400).json({
        success: false,
        message: 'Search query is required',
      });
      return;
    }

    const servers = await searchCloudServers(query);
    const response: ApiResponse<CloudServer[]> = {
      success: true,
      data: servers,
    };
    res.json(response);
  } catch (error) {
    console.error('Error searching cloud market servers:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to search cloud market servers';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

// Get cloud market servers by category
export const getCloudServersByCategory = async (req: Request, res: Response): Promise<void> => {
  try {
    const { category } = req.params;
    if (!category) {
      res.status(400).json({
        success: false,
        message: 'Category is required',
      });
      return;
    }

    const servers = await filterCloudServersByCategory(category);
    const response: ApiResponse<CloudServer[]> = {
      success: true,
      data: servers,
    };
    res.json(response);
  } catch (error) {
    console.error('Error getting cloud market servers by category:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to get cloud market servers by category';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

// Get cloud market servers by tag
export const getCloudServersByTag = async (req: Request, res: Response): Promise<void> => {
  try {
    const { tag } = req.params;
    if (!tag) {
      res.status(400).json({
        success: false,
        message: 'Tag is required',
      });
      return;
    }

    const servers = await filterCloudServersByTag(tag);
    const response: ApiResponse<CloudServer[]> = {
      success: true,
      data: servers,
    };
    res.json(response);
  } catch (error) {
    console.error('Error getting cloud market servers by tag:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to get cloud market servers by tag';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

// Get tools for a specific cloud server
export const getCloudServerToolsList = async (req: Request, res: Response): Promise<void> => {
  try {
    // Decode URL-encoded parameter to handle slashes in server name
    const serverName = decodeURIComponent(req.params.serverName);
    if (!serverName) {
      res.status(400).json({
        success: false,
        message: 'Server name is required',
      });
      return;
    }

    const tools = await getCloudServerTools(serverName);
    const response: ApiResponse<CloudTool[]> = {
      success: true,
      data: tools,
    };
    res.json(response);
  } catch (error) {
    console.error('Error getting cloud server tools:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to get cloud server tools';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

// Call a tool on a cloud server
export const callCloudTool = async (req: Request, res: Response): Promise<void> => {
  try {
    // Decode URL-encoded parameters to handle slashes in server/tool names
    const serverName = decodeURIComponent(req.params.serverName);
    const toolName = decodeURIComponent(req.params.toolName);
    const { arguments: args } = req.body;

    if (!serverName) {
      res.status(400).json({
        success: false,
        message: 'Server name is required',
      });
      return;
    }

    if (!toolName) {
      res.status(400).json({
        success: false,
        message: 'Tool name is required',
      });
      return;
    }

    const result = await callCloudServerTool(serverName, toolName, args || {});
    const response: ApiResponse = {
      success: true,
      data: result,
    };
    res.json(response);
  } catch (error) {
    console.error('Error calling cloud server tool:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to call cloud server tool';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};