Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import com.alibaba.cloud.ai.dataagent.service.agent.AgentService;
import com.alibaba.cloud.ai.dataagent.vo.ApiKeyResponse;
import com.alibaba.cloud.ai.dataagent.vo.ApiResponse;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -32,12 +34,9 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

/**
* Agent Management Controller
*/
/** Agent Management Controller */
@Slf4j
@RestController
@RequestMapping("/api/agent")
Expand All @@ -47,168 +46,117 @@ public class AgentController {

private final AgentService agentService;

/**
* Get agent list
*/
/** Get agent list */
@GetMapping("/list")
public ResponseEntity<List<Agent>> list(@RequestParam(value = "status", required = false) String status,
public List<Agent> list(@RequestParam(value = "status", required = false) String status,
@RequestParam(value = "keyword", required = false) String keyword) {
List<Agent> result;
if (keyword != null && !keyword.trim().isEmpty()) {
if (StringUtils.isNotBlank(keyword)) {
result = agentService.search(keyword);
}
else if (status != null && !status.trim().isEmpty()) {
else if (StringUtils.isNotBlank(status)) {
result = agentService.findByStatus(status);
}
else {
result = agentService.findAll();
}
return ResponseEntity.ok(result);
return result;
}

/**
* Get agent details by ID
*/
/** Get agent details by ID */
@GetMapping("/{id}")
public ResponseEntity<Agent> get(@PathVariable(value = "id") Long id) {
Agent agent = agentService.findById(id);
if (agent == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(agent);
public Agent get(@PathVariable Long id) {
return checkAgentExists(id);
}

/**
* Create agent
*/
/** Create agent */
@PostMapping
public ResponseEntity<Agent> create(@RequestBody Agent agent) {
public Agent create(@RequestBody Agent agent) {
// Set default status
if (agent.getStatus() == null || agent.getStatus().trim().isEmpty()) {
if (StringUtils.isBlank(agent.getStatus())) {
agent.setStatus("draft");
}
Agent saved = agentService.save(agent);
return ResponseEntity.ok(saved);
return agentService.save(agent);
}

/**
* Update agent
*/
/** Update agent */
@PutMapping("/{id}")
public ResponseEntity<Agent> update(@PathVariable(value = "id") Long id, @RequestBody Agent agent) {
if (agentService.findById(id) == null) {
return ResponseEntity.notFound().build();
}
public Agent update(@PathVariable Long id, @RequestBody Agent agent) {
checkAgentExists(id);
agent.setId(id);
Agent updated = agentService.save(agent);
return ResponseEntity.ok(updated);
return agentService.save(agent);
}

/**
* Delete agent
*/
/** Delete agent */
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable(value = "id") Long id) {
if (agentService.findById(id) == null) {
return ResponseEntity.notFound().build();
}
public void delete(@PathVariable Long id) {
checkAgentExists(id);
agentService.deleteById(id);
return ResponseEntity.ok().build();
}

/**
* Publish agent
*/
/** Publish agent */
@PostMapping("/{id}/publish")
public ResponseEntity<Agent> publish(@PathVariable(value = "id") Long id) {
Agent agent = agentService.findById(id);
if (agent == null) {
return ResponseEntity.notFound().build();
}
public Agent publish(@PathVariable Long id) {
Agent agent = checkAgentExists(id);
agent.setStatus("published");
Agent updated = agentService.save(agent);
return ResponseEntity.ok(updated);
return agentService.save(agent);
}

/**
* Offline agent
*/
/** Offline agent */
@PostMapping("/{id}/offline")
public ResponseEntity<Agent> offline(@PathVariable(value = "id") Long id) {
Agent agent = agentService.findById(id);
if (agent == null) {
return ResponseEntity.notFound().build();
}
public Agent offline(@PathVariable Long id) {
Agent agent = checkAgentExists(id);
agent.setStatus("offline");
Agent updated = agentService.save(agent);
return ResponseEntity.ok(updated);
return agentService.save(agent);
}

/**
* Get masked API Key status
*/
/** Get masked API Key status */
@GetMapping("/{id}/api-key")
public ResponseEntity<ApiResponse<ApiKeyResponse>> getApiKey(@PathVariable("id") Long id) {
Agent agent = agentService.findById(id);
if (agent == null) {
return ResponseEntity.notFound().build();
}
public ApiResponse<ApiKeyResponse> getApiKey(@PathVariable Long id) {
Agent agent = checkAgentExists(id);
String masked = agentService.getApiKeyMasked(id);
return ResponseEntity.ok(buildApiKeyResponse(masked, agent.getApiKeyEnabled(), "获取 API Key 成功"));
return buildApiKeyResponse(masked, agent.getApiKeyEnabled(), "获取 API Key 成功");
}

/**
* Generate API Key
*/
/** Generate API Key */
@PostMapping("/{id}/api-key/generate")
public ResponseEntity<ApiResponse<ApiKeyResponse>> generateApiKey(@PathVariable("id") Long id) {
Agent existing = agentService.findById(id);
if (existing == null) {
return ResponseEntity.notFound().build();
}
public ApiResponse<ApiKeyResponse> generateApiKey(@PathVariable Long id) {
checkAgentExists(id);
Agent agent = agentService.generateApiKey(id);
return ResponseEntity.ok(buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "生成 API Key 成功"));
return buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "生成 API Key 成功");
}

/**
* Reset API Key
*/
/** Reset API Key */
@PostMapping("/{id}/api-key/reset")
public ResponseEntity<ApiResponse<ApiKeyResponse>> resetApiKey(@PathVariable("id") Long id) {
Agent existing = agentService.findById(id);
if (existing == null) {
return ResponseEntity.notFound().build();
}
public ApiResponse<ApiKeyResponse> resetApiKey(@PathVariable Long id) {
checkAgentExists(id);
Agent agent = agentService.resetApiKey(id);
return ResponseEntity.ok(buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "重置 API Key 成功"));
return buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "重置 API Key 成功");
}

/**
* Delete API Key
*/
/** Delete API Key */
@DeleteMapping("/{id}/api-key")
public ResponseEntity<ApiResponse<ApiKeyResponse>> deleteApiKey(@PathVariable("id") Long id) {
Agent existing = agentService.findById(id);
if (existing == null) {
return ResponseEntity.notFound().build();
}
public ApiResponse<ApiKeyResponse> deleteApiKey(@PathVariable Long id) {
checkAgentExists(id);
Agent agent = agentService.deleteApiKey(id);
return ResponseEntity.ok(buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "删除 API Key 成功"));
return buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "删除 API Key 成功");
}

/**
* Toggle API Key enable flag
*/
/** Toggle API Key enable flag */
@PostMapping("/{id}/api-key/enable")
public ResponseEntity<ApiResponse<ApiKeyResponse>> toggleApiKey(@PathVariable("id") Long id,
@RequestParam("enabled") boolean enabled) {
Agent existing = agentService.findById(id);
if (existing == null) {
return ResponseEntity.notFound().build();
}
public ApiResponse<ApiKeyResponse> toggleApiKey(@PathVariable Long id, @RequestParam("enabled") boolean enabled) {
checkAgentExists(id);
Agent agent = agentService.toggleApiKey(id, enabled);
return ResponseEntity.ok(buildApiKeyResponse(agent.getApiKey() == null ? null : "****",
agent.getApiKeyEnabled(), "更新 API Key 状态成功"));
return buildApiKeyResponse(agent.getApiKey() == null ? null : "****", agent.getApiKeyEnabled(),
"更新 API Key 状态成功");
}

private Agent checkAgentExists(Long id) {
Agent agent = agentService.findById(id);
if (agent == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "agent with id: %d not found".formatted(id));
}
return agent;
}

private ApiResponse<ApiKeyResponse> buildApiKeyResponse(String apiKey, Integer apiKeyEnabled, String message) {
Expand Down
Loading
Loading