22
33import io .openkruise .agents .client .e2b .api .SandboxesApi ;
44import io .openkruise .agents .client .e2b .api .invoker .ApiClient ;
5+ import io .openkruise .agents .client .runtime .RuntimeConfig ;
6+ import io .openkruise .agents .client .url .E2BURLBuilder ;
7+ import io .openkruise .agents .client .url .URLBuilder ;
58
69import java .util .HashMap ;
710import java .util .Map ;
@@ -30,7 +33,6 @@ public String getValue() {
3033 private static final String DEFAULT_SCHEME = "https" ;
3134 private static final long DEFAULT_REQUEST_TIMEOUT_MS = 60_000L ;
3235 static final int DEFAULT_SANDBOX_TIMEOUT = 300 ;
33- static final int DEFAULT_RUNTIME_PORT = 49983 ;
3436
3537 private String apiKey ;
3638 private String accessToken ;
@@ -42,19 +44,24 @@ public String getValue() {
4244 private boolean debug ;
4345 private long requestTimeoutMs ;
4446 private int port ;
47+ private int codeInterpreterPort ;
4548 private Map <String , String > headers ;
4649
4750 private volatile ApiClient apiClient ;
4851 private volatile SandboxesApi sandboxesApi ;
4952 private final Object lock = new Object ();
53+
54+ private URLBuilder urlBuilder ;
5055
5156 private ConnectionConfig () {
5257 this .domain = DEFAULT_DOMAIN ;
5358 this .scheme = DEFAULT_SCHEME ;
5459 this .protocol = Protocol .NATIVE ;
5560 this .requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS ;
56- this .port = DEFAULT_RUNTIME_PORT ;
61+ this .port = RuntimeConfig .DEFAULT_RUNTIME_PORT ;
62+ this .codeInterpreterPort = RuntimeConfig .DEFAULT_CODE_INTERPRETER_PORT ;
5763 this .headers = new HashMap <>();
64+ this .urlBuilder = buildURLBuilder ();
5865 }
5966
6067 private ConnectionConfig (ConnectionConfig config ) {
@@ -68,35 +75,82 @@ private ConnectionConfig(ConnectionConfig config) {
6875 this .debug = config .debug ;
6976 this .requestTimeoutMs = config .requestTimeoutMs ;
7077 this .port = config .port ;
78+ this .codeInterpreterPort = config .codeInterpreterPort ;
7179 this .headers = new HashMap <>(config .headers );
80+ this .urlBuilder = buildURLBuilder ();
81+ }
82+
83+ private URLBuilder buildURLBuilder () {
84+ return new E2BURLBuilder .Builder ()
85+ .scheme (scheme )
86+ .domain (domain )
87+ .protocol (protocol )
88+ .runtimePort (port )
89+ .codeInterpreterPort (codeInterpreterPort )
90+ .customApiURL (apiURL )
91+ .customSandboxBaseURL (sandboxBaseURL )
92+ .build ();
7293 }
7394
7495 public static ConnectionConfig create () {
7596 return new Builder ().build ();
7697 }
7798
78- /** API base URL, automatically selects NATIVE/PRIVATE format based on protocol. */
79- public String getAPIURL () {
80- if (apiURL != null && !apiURL .isEmpty ()) {
81- return apiURL ;
99+ /**
100+ * Convert ConnectionConfig to RuntimeConfig for data plane operations.
101+ */
102+ public static RuntimeConfig toRuntimeConfig (ConnectionConfig connectionConfig , String envdAccessToken ) {
103+ if (connectionConfig == null ) {
104+ throw new IllegalArgumentException ("connectionConfig cannot be null" );
82105 }
83- String s = getSchemeOrDefault ();
84- if (protocol == Protocol .PRIVATE ) {
85- return String .format ("%s://%s/kruise/api" , s , domain );
106+
107+ RuntimeConfig .Builder builder = new RuntimeConfig .Builder ();
108+ builder .domain (connectionConfig .getDomain ());
109+ builder .scheme (connectionConfig .getScheme ());
110+ builder .requestTimeoutMs (connectionConfig .getRequestTimeoutMs ());
111+
112+ if (connectionConfig .getApiKey () != null ) {
113+ builder .apiKey (connectionConfig .getApiKey ());
86114 }
87- return String .format ("%s://api.%s" , s , domain );
115+ if (connectionConfig .getHeaders () != null ) {
116+ builder .headers (connectionConfig .getHeaders ());
117+ }
118+ if (envdAccessToken != null && !envdAccessToken .isEmpty ()) {
119+ builder .runtimeToken (envdAccessToken );
120+ }
121+
122+ builder .sandboxPort (connectionConfig .getPort ());
123+ builder .codeInterpreterPort (connectionConfig .getCodeInterpreterPort ());
124+
125+ // Build URLBuilder for E2B
126+ URLBuilder urlBuilder = new E2BURLBuilder .Builder ()
127+ .scheme (connectionConfig .getScheme ())
128+ .domain (connectionConfig .getDomain ())
129+ .protocol (connectionConfig .getProtocol ())
130+ .runtimePort (connectionConfig .getPort ())
131+ .codeInterpreterPort (connectionConfig .getCodeInterpreterPort ())
132+ .customApiURL (connectionConfig .getApiURL ())
133+ .customSandboxBaseURL (connectionConfig .getSandboxBaseURL ())
134+ .build ();
135+
136+ builder .urlBuilder (urlBuilder );
137+
138+ return builder .build ();
139+ }
140+
141+ /** API base URL, automatically selects NATIVE/PRIVATE format based on protocol. */
142+ public String getAPIURL () {
143+ return urlBuilder .buildAPIURL ();
88144 }
89145
90146 /** Envd URL for a specific sandbox, automatically selects NATIVE/PRIVATE format based on protocol. */
91147 public String getSandboxURL (String sandboxID ) {
92- if (sandboxBaseURL != null && !sandboxBaseURL .isEmpty ()) {
93- return sandboxBaseURL ;
94- }
95- String s = getSchemeOrDefault ();
96- if (protocol == Protocol .PRIVATE ) {
97- return String .format ("%s://%s/kruise/%s/%d" , s , domain , sandboxID , port );
98- }
99- return String .format ("%s://%d-%s.%s" , s , port , sandboxID , domain );
148+ return urlBuilder .buildSandboxURL (sandboxID );
149+ }
150+
151+ /** Code Interpreter URL for a specific sandbox, uses codeInterpreterPort (49999). */
152+ public String getCodeInterpreterURL (String sandboxID ) {
153+ return urlBuilder .buildCodeInterpreterURL (sandboxID );
100154 }
101155
102156 /** Shared ApiClient with double-checked locking lazy initialization. */
@@ -156,6 +210,8 @@ private String getSchemeOrDefault() {
156210
157211 public int getPort () {return port ;}
158212
213+ public int getCodeInterpreterPort () {return codeInterpreterPort ;}
214+
159215 public Map <String , String > getHeaders () {return headers ;}
160216
161217 public static class Builder {
@@ -223,6 +279,11 @@ public Builder port(int port) {
223279 return this ;
224280 }
225281
282+ public Builder codeInterpreterPort (int codeInterpreterPort ) {
283+ config .codeInterpreterPort = codeInterpreterPort ;
284+ return this ;
285+ }
286+
226287 public Builder headers (Map <String , String > headers ) {
227288 config .headers .putAll (headers );
228289 return this ;
@@ -234,6 +295,7 @@ public Builder addHeader(String key, String value) {
234295 }
235296
236297 public ConnectionConfig build () {
298+ config .urlBuilder = config .buildURLBuilder ();
237299 return new ConnectionConfig (config );
238300 }
239301 }
0 commit comments