diff --git a/pom.xml b/pom.xml
index 4356e07..adc718b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -292,6 +292,13 @@
+
+
+ com.squareup.okhttp3
+ logging-interceptor
+ ${okhttp3.version}
+
+
com.sendgrid
sendgrid-java
@@ -373,6 +380,15 @@
2.1.3
+
+ org.glassfish.tyrus
+ tyrus-client
+ 2.1.3
+
+
+
+
+
diff --git a/vetti-admin/pom.xml b/vetti-admin/pom.xml
index e758316..a3b0afa 100644
--- a/vetti-admin/pom.xml
+++ b/vetti-admin/pom.xml
@@ -97,6 +97,15 @@
tyrus-client
+
+ com.squareup.okhttp3
+ logging-interceptor
+
+
+
+ org.glassfish.tyrus
+ tyrus-client
+
diff --git a/vetti-admin/src/main/java/com/vetti/socket/MyWebSocketClient.java b/vetti-admin/src/main/java/com/vetti/socket/MyWebSocketClient.java
index 330bc3d..f2b78de 100644
--- a/vetti-admin/src/main/java/com/vetti/socket/MyWebSocketClient.java
+++ b/vetti-admin/src/main/java/com/vetti/socket/MyWebSocketClient.java
@@ -1,7 +1,12 @@
package com.vetti.socket;
+import cn.hutool.json.JSONUtil;
+
import javax.websocket.*;
import java.net.URI;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
// 客户端端点类
@ClientEndpoint
@@ -11,21 +16,51 @@ public class MyWebSocketClient {
@OnOpen
public void onOpen(Session session) {
System.out.println("连接已建立,Session ID: " + session.getId());
- try {
- // 发送消息到服务端
- session.getBasicRemote().sendText("Hello, Server!");
- } catch (Exception e) {
- e.printStackTrace();
- }
+
}
// 收到服务端消息时触发
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("收到服务端消息: " + message);
+ Map map1 = JSONUtil.parseObj(message);
+ Map data = (Map) map1.get("text_response_part");
+ if(map1 != null && "conversation_initiation_metadata".equals(map1.get("type").toString())){
+ try {
+ // 发送消息到服务端
+ Map map = new HashMap<>();
+ map.put("type", "contextual_update");
+ map.put("user_id", session.getId());
+ map.put("text", "你好,只回复一句话");
+ String s = JSONUtil.toJsonStr(map);
+ session.getBasicRemote().sendText(s);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ if(data != null && "stop".equals(data.get("type").toString())&& "agent_chat_response_part".equals(map1.get("type").toString())){
+ try {
+ Thread.sleep(2000);
+ // 发送消息到服务端
+ Map map = new HashMap<>();
+ map.put("type", "contextual_update");
+ map.put("user_id", session.getId());
+ map.put("text", "你能回答我其他的嘛");
+ String s = JSONUtil.toJsonStr(map);
+ session.getBasicRemote().sendText(s);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
// 可根据消息内容做后续处理
}
+// @OnMessage
+// public void onBinary(ByteBuffer buffer) {
+// System.out.println("收到服务端语音流啦: " + buffer);
+// }
// 连接关闭时触发
@OnClose
public void onClose(Session session, CloseReason reason) {
@@ -41,16 +76,18 @@ public class MyWebSocketClient {
public static void main(String[] args) {
// WebSocket服务端地址(示例)
- String serverUri = "ws://vetti.hotake.cn/prod-api/voice-websocket-opus/104";
-
+ String serverUri = "ws://vetti.hotake.cn/prod-api/voice-websocket/elevenLabsAgent/104";
try {
// 获取WebSocket容器
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
// 连接服务端(传入客户端端点实例和服务端URI)
container.connectToServer(new MyWebSocketClient(), new URI(serverUri));
-
- // 阻塞主线程,避免程序退出(实际场景根据需求处理)
- Thread.sleep(60000);
+// // 1. 设置文本消息缓冲区:256KB(测试足够用)
+// container.setDefaultMaxTextMessageBufferSize(2560 * 1024);
+// // 2. 设置二进制消息缓冲区(语音流用)
+// container.setDefaultMaxBinaryMessageBufferSize(2560 * 1024);
+// // 阻塞主线程,避免程序退出(实际场景根据需求处理)
+ Thread.sleep(6000000);
} catch (Exception e) {
e.printStackTrace();
}
diff --git a/vetti-admin/src/main/java/com/vetti/socket/agents/ElevenLabsAgentClient.java b/vetti-admin/src/main/java/com/vetti/socket/agents/ElevenLabsAgentClient.java
deleted file mode 100644
index 1dbb277..0000000
--- a/vetti-admin/src/main/java/com/vetti/socket/agents/ElevenLabsAgentClient.java
+++ /dev/null
@@ -1,80 +0,0 @@
-package com.vetti.socket.agents;
-
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.web.socket.WebSocketSession;
-
-import javax.websocket.ClientEndpointConfig;
-import javax.websocket.ContainerProvider;
-import javax.websocket.WebSocketContainer;
-import java.net.URI;
-import java.util.List;
-import java.util.Map;
-
-@Slf4j
-public class ElevenLabsAgentClient {
-
- private static final String AGENT_WS_URL =
- "wss://api.elevenlabs.io/v1/agents/%s/stream";
-
-
-
- private final ElevenLabsAgentEndpoint endpoint;
-
- public ElevenLabsAgentClient(String traceId, WebSocketSession frontendSession) {
- this.endpoint = new ElevenLabsAgentEndpoint();
- connect(traceId, frontendSession);
- }
-
- private void connect(String traceId, WebSocketSession frontendSession) {
- try {
- log.info("[traceId={}] Connecting to ElevenLabs Agent...", traceId);
-
- WebSocketContainer container =
- ContainerProvider.getWebSocketContainer();
-
- ClientEndpointConfig config =
- ClientEndpointConfig.Builder.create()
- .configurator(new ClientEndpointConfig.Configurator() {
- @Override
- public void beforeRequest(
- Map> headers
- ) {
- headers.put(
- "xi-api-key",
- List.of("sk_dfe2b45e19bf8ad93a71d3a0faa61619a91e817df549d116")
- );
- }
- })
- .build();
-
- config.getUserProperties().put("traceId", traceId);
- config.getUserProperties().put("frontendSession", frontendSession);
-
- container.connectToServer(
- endpoint,
- config,
- URI.create(
- String.format(
- AGENT_WS_URL,
- "9c5cb2f7ba9efb61d0f0eee01427b6e00c6abe92d4754cfb794884ac4d73c79d"
- )
- )
- );
-
- } catch (Exception e) {
- throw new RuntimeException("Connect ElevenLabs failed", e);
- }
- }
-
- public void sendAudio(java.nio.ByteBuffer buffer) {
- endpoint.sendAudio(buffer);
- }
-
- public void sendText(String text) {
- endpoint.sendText(text);
- }
-
- public void close() {
- endpoint.close();
- }
-}
diff --git a/vetti-admin/src/main/java/com/vetti/socket/agents/ElevenLabsAgentEndpoint.java b/vetti-admin/src/main/java/com/vetti/socket/agents/ElevenLabsAgentEndpoint.java
index a55d6af..6c864ce 100644
--- a/vetti-admin/src/main/java/com/vetti/socket/agents/ElevenLabsAgentEndpoint.java
+++ b/vetti-admin/src/main/java/com/vetti/socket/agents/ElevenLabsAgentEndpoint.java
@@ -1,88 +1,81 @@
package com.vetti.socket.agents;
+import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
-import org.springframework.web.socket.BinaryMessage;
-import org.springframework.web.socket.TextMessage;
-import org.springframework.web.socket.WebSocketSession;
-import javax.websocket.*;
+import javax.websocket.Endpoint;
+import javax.websocket.EndpointConfig;
+import javax.websocket.Session;
import java.io.IOException;
import java.nio.ByteBuffer;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Consumer;
+/**
+ * ElevenLabs Agent 客户端端点 - 处理逻辑
+ */
@Slf4j
public class ElevenLabsAgentEndpoint extends Endpoint {
- private Session agentSession;
- private String traceId;
- private WebSocketSession frontendSession;
+ private Session session;
- private final AtomicInteger audioCount = new AtomicInteger();
+ private final Consumer