|
|
@ -72,7 +72,7 @@ public class AiController { |
|
|
*/ |
|
|
*/ |
|
|
@GetMapping("/assistant_app/chat/sync") |
|
|
@GetMapping("/assistant_app/chat/sync") |
|
|
public String doChatWithAssistantAppSync(String message, String chatId, Long roleId, String accountId, String systemPrompt) { |
|
|
public String doChatWithAssistantAppSync(String message, String chatId, Long roleId, String accountId, String systemPrompt) { |
|
|
AccountRoleContext context = resolveAccountRole(toLong(accountId), roleId); |
|
|
|
|
|
|
|
|
AccountRoleContext context = resolveAccountRole(accountId, roleId); |
|
|
bindConversation(chatId, context); |
|
|
bindConversation(chatId, context); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
return assistantApp.doChat(message, chatId, resolveSystemPrompt(scope, systemPrompt)); |
|
|
return assistantApp.doChat(message, chatId, resolveSystemPrompt(scope, systemPrompt)); |
|
|
@ -88,7 +88,7 @@ public class AiController { |
|
|
*/ |
|
|
*/ |
|
|
@GetMapping(value = "/assistant_app/chat/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
|
|
@GetMapping(value = "/assistant_app/chat/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
|
|
public Flux<String> doChatWithLoveAppSSE(String message, String chatId, Long roleId, String accountId, String systemPrompt) { |
|
|
public Flux<String> doChatWithLoveAppSSE(String message, String chatId, Long roleId, String accountId, String systemPrompt) { |
|
|
AccountRoleContext context = resolveAccountRole(toLong(accountId), roleId); |
|
|
|
|
|
|
|
|
AccountRoleContext context = resolveAccountRole(accountId, roleId); |
|
|
bindConversation(chatId, context); |
|
|
bindConversation(chatId, context); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
return assistantApp.doChatByStream(message, chatId, resolveSystemPrompt(scope, systemPrompt)); |
|
|
return assistantApp.doChatByStream(message, chatId, resolveSystemPrompt(scope, systemPrompt)); |
|
|
@ -104,7 +104,7 @@ public class AiController { |
|
|
*/ |
|
|
*/ |
|
|
@GetMapping(value = "/assistant_app/chat/server_sent_event") |
|
|
@GetMapping(value = "/assistant_app/chat/server_sent_event") |
|
|
public Flux<ServerSentEvent<String>> doChatWithAssistantAppServerSentEvent(String message, String chatId, Long roleId, String accountId, String systemPrompt) { |
|
|
public Flux<ServerSentEvent<String>> doChatWithAssistantAppServerSentEvent(String message, String chatId, Long roleId, String accountId, String systemPrompt) { |
|
|
AccountRoleContext context = resolveAccountRole(toLong(accountId), roleId); |
|
|
|
|
|
|
|
|
AccountRoleContext context = resolveAccountRole(accountId, roleId); |
|
|
bindConversation(chatId, context); |
|
|
bindConversation(chatId, context); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
return assistantApp.doChatByStream(message, chatId, resolveSystemPrompt(scope, systemPrompt)) |
|
|
return assistantApp.doChatByStream(message, chatId, resolveSystemPrompt(scope, systemPrompt)) |
|
|
@ -123,7 +123,7 @@ public class AiController { |
|
|
*/ |
|
|
*/ |
|
|
@GetMapping(value = "/assistant_app/chat/sse_emitter") |
|
|
@GetMapping(value = "/assistant_app/chat/sse_emitter") |
|
|
public SseEmitter doChatWithAssistantAppServerSseEmitter(String message, String chatId, Long roleId, String accountId, String systemPrompt) { |
|
|
public SseEmitter doChatWithAssistantAppServerSseEmitter(String message, String chatId, Long roleId, String accountId, String systemPrompt) { |
|
|
AccountRoleContext context = resolveAccountRole(toLong(accountId), roleId); |
|
|
|
|
|
|
|
|
AccountRoleContext context = resolveAccountRole(accountId, roleId); |
|
|
bindConversation(chatId, context); |
|
|
bindConversation(chatId, context); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
// 创建一个超时时间较长的 SseEmitter |
|
|
// 创建一个超时时间较长的 SseEmitter |
|
|
@ -155,7 +155,7 @@ public class AiController { |
|
|
*/ |
|
|
*/ |
|
|
@GetMapping("/assistant_app/chat/rag/sync") |
|
|
@GetMapping("/assistant_app/chat/rag/sync") |
|
|
public String doChatWithRagSync(String message, String chatId, String rewriteStrategy, Long roleId, String accountId, Long categoryId, String categoryIds, String systemPrompt) { |
|
|
public String doChatWithRagSync(String message, String chatId, String rewriteStrategy, Long roleId, String accountId, Long categoryId, String categoryIds, String systemPrompt) { |
|
|
AccountRoleContext context = resolveAccountRole(toLong(accountId), roleId); |
|
|
|
|
|
|
|
|
AccountRoleContext context = resolveAccountRole(accountId, roleId); |
|
|
bindConversation(chatId, context); |
|
|
bindConversation(chatId, context); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
String sys = resolveSystemPrompt(scope, systemPrompt); |
|
|
String sys = resolveSystemPrompt(scope, systemPrompt); |
|
|
@ -182,7 +182,7 @@ public class AiController { |
|
|
*/ |
|
|
*/ |
|
|
@GetMapping(value = "/assistant_app/chat/rag/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
|
|
@GetMapping(value = "/assistant_app/chat/rag/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
|
|
public Flux<String> doChatWithRagSSE(String message, String chatId, String rewriteStrategy, Long roleId, String accountId, Long categoryId, String categoryIds, String systemPrompt) { |
|
|
public Flux<String> doChatWithRagSSE(String message, String chatId, String rewriteStrategy, Long roleId, String accountId, Long categoryId, String categoryIds, String systemPrompt) { |
|
|
AccountRoleContext context = resolveAccountRole(toLong(accountId), roleId); |
|
|
|
|
|
|
|
|
AccountRoleContext context = resolveAccountRole(accountId, roleId); |
|
|
bindConversation(chatId, context); |
|
|
bindConversation(chatId, context); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
String sys = resolveSystemPrompt(scope, systemPrompt); |
|
|
String sys = resolveSystemPrompt(scope, systemPrompt); |
|
|
@ -202,7 +202,7 @@ public class AiController { |
|
|
@GetMapping("/assistant_app/rag/sources") |
|
|
@GetMapping("/assistant_app/rag/sources") |
|
|
public Map<String, Object> getRagSources(String message, String chatId, String rewriteStrategy, |
|
|
public Map<String, Object> getRagSources(String message, String chatId, String rewriteStrategy, |
|
|
Long roleId, String accountId, Long categoryId, String categoryIds) { |
|
|
Long roleId, String accountId, Long categoryId, String categoryIds) { |
|
|
AccountRoleContext context = resolveAccountRole(toLong(accountId), roleId); |
|
|
|
|
|
|
|
|
AccountRoleContext context = resolveAccountRole(accountId, roleId); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
RoleScope scope = customerServiceRoleService.getRoleScope(context.roleId()); |
|
|
if (message == null || message.isBlank() || isKbDenied(scope) || shouldBypassKnowledgeRetrieval(message)) { |
|
|
if (message == null || message.isBlank() || isKbDenied(scope) || shouldBypassKnowledgeRetrieval(message)) { |
|
|
return Map.of("success", true, "data", List.of()); |
|
|
return Map.of("success", true, "data", List.of()); |
|
|
@ -264,8 +264,8 @@ public class AiController { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** 命中角色时用角色人设,否则用客户端兜底人设。 */ |
|
|
/** 命中角色时用角色人设,否则用客户端兜底人设。 */ |
|
|
private AccountRoleContext resolveAccountRole(Long accountId, Long fallbackRoleId) { |
|
|
|
|
|
AccountScope accountScope = customerAccountService.getAccountScope(accountId); |
|
|
|
|
|
|
|
|
private AccountRoleContext resolveAccountRole(String accountId, Long fallbackRoleId) { |
|
|
|
|
|
AccountScope accountScope = customerAccountService.getOrCreateAccountScope(accountId, fallbackRoleId); |
|
|
Long effectiveRoleId = accountScope.hasAccount() && accountScope.roleId() != null |
|
|
Long effectiveRoleId = accountScope.hasAccount() && accountScope.roleId() != null |
|
|
? accountScope.roleId() |
|
|
? accountScope.roleId() |
|
|
: fallbackRoleId; |
|
|
: fallbackRoleId; |
|
|
@ -316,15 +316,4 @@ public class AiController { |
|
|
private record AccountRoleContext(Long accountId, Long roleId) { |
|
|
private record AccountRoleContext(Long accountId, Long roleId) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** 将字符串 accountId 安全转为 Long,非数字字符串返回 null */ |
|
|
|
|
|
private static Long toLong(String value) { |
|
|
|
|
|
if (value == null || value.isBlank()) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
return Long.valueOf(value.trim()); |
|
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |