Skip to content

Commit 43cdf37

Browse files
author
BitsAdmin
committed
Merge branch 'volc_sdk_20251204' into 'integration_2025-12-04_1090536471554'
feat: [development task] ark runtime (1900799) See merge request iaasng/volcengine-java-sdk!787
2 parents cb4f057 + f7f36ec commit 43cdf37

File tree

13 files changed

+309
-41
lines changed

13 files changed

+309
-41
lines changed

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/Const.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class Const {
88
public static final String SERVER_REQUEST_HEADER = "X-Request-Id";
99
public static final String REQUEST_MODEL = "X-Request-Model";
1010
public static final String REQUEST_BOT = "X-Request-Bot";
11+
public static final String REQUEST_PROJECT_NAME = "X-Project-Name";
1112
public static final String RETRY_AFTER = "Retry-After";
1213
public static final String REQUEST_BOT_ID = "botId";
1314
public static final Integer DEFAULT_MANDATORY_REFRESH_TIMEOUT = 10 * 60; // 10 min
@@ -16,6 +17,7 @@ public class Const {
1617

1718
public static final String RESOURCE_TYPE_BOT = "bot";
1819
public static final String RESOURCE_TYPE_ENDPOINT = "endpoint";
20+
public static final String RESOURCE_TYPE_PRESETENDPOINT = "presetendpoint";
1921

2022
public static final String CONTEXT_MODE_SESSION = "session";
2123
public static final String CONTEXT_MODE_COMMON_PREFIX = "common_prefix";

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/interceptor/ArkResourceStsAuthenticationInterceptor.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,40 @@ public ArkResourceStsAuthenticationInterceptor(String ak, String sk, String regi
5151
@Override
5252
public Response intercept(Chain chain) throws IOException {
5353
Request request = chain.request();
54-
String requestResourceType = getRequestResourceType(request);
5554
String requestResourceId = getRequestResourceId(request);
55+
String requestResourceType = getRequestResourceType(request, requestResourceId);
56+
String projectName = getProjectName(request);
57+
58+
if (requestResourceType.equalsIgnoreCase(Const.RESOURCE_TYPE_PRESETENDPOINT) && StringUtils.isBlank(projectName)) {
59+
throw new ArkException("project name is required for preset endpoint");
60+
}
5661

5762
if (request.url().url().getPath().contains("contents/generations") || request.url().url().getPath().contains("images/generations")) {
5863
throw new ArkException("content generation currently does not support ak&sk authentication, use api_key instead.");
5964
}
6065

6166
Request newRequest = chain.request()
6267
.newBuilder()
63-
.header("Authorization", "Bearer " + getResourceStsToken(requestResourceType, requestResourceId))
68+
.header("Authorization", "Bearer " + getResourceStsToken(requestResourceType, requestResourceId, projectName))
6469
.build();
6570
return chain.proceed(newRequest);
6671
}
6772

68-
private String getRequestResourceType(Request request) {
73+
private String getRequestResourceType(Request request, String requestResourceId) {
6974
if (StringUtils.isNotBlank(request.header(Const.REQUEST_BOT))) {
7075
return Const.RESOURCE_TYPE_BOT;
7176
}
72-
return Const.RESOURCE_TYPE_ENDPOINT;
77+
78+
if (StringUtils.isNotBlank(requestResourceId) && requestResourceId.startsWith("ep-m-")) {
79+
return Const.RESOURCE_TYPE_PRESETENDPOINT;
80+
}
81+
82+
if (StringUtils.isNotBlank(requestResourceId) && requestResourceId.startsWith("ep-")) {
83+
return Const.RESOURCE_TYPE_ENDPOINT;
84+
}
85+
86+
// for model id
87+
return Const.RESOURCE_TYPE_PRESETENDPOINT;
7388
}
7489

7590
private String getRequestResourceId(Request request) {
@@ -79,8 +94,15 @@ private String getRequestResourceId(Request request) {
7994
return request.header(Const.REQUEST_MODEL);
8095
}
8196

82-
private String getResourceStsToken(String resourceType, String resourceId) {
83-
refresh(resourceType, resourceId);
97+
private String getProjectName(Request request) {
98+
if (StringUtils.isNotBlank(request.header(Const.REQUEST_PROJECT_NAME))) {
99+
return request.header(Const.REQUEST_PROJECT_NAME);
100+
}
101+
return "";
102+
}
103+
104+
private String getResourceStsToken(String resourceType, String resourceId, String projectName) {
105+
refresh(resourceType, resourceId, projectName);
84106

85107
ArkResourceStsTokenInfo tokenInfo = this.resourceStsTokens.get(getResourceKey(resourceType, resourceId));
86108
if (tokenInfo == null) {
@@ -89,7 +111,7 @@ private String getResourceStsToken(String resourceType, String resourceId) {
89111
return tokenInfo.getToken();
90112
}
91113

92-
private void refresh(String resourceType, String resourceId) {
114+
private void refresh(String resourceType, String resourceId, String projectName) {
93115
if (!need_refresh(resourceType, resourceId, this.advisoryRefreshTimeout)) {
94116
return;
95117
}
@@ -101,7 +123,7 @@ private void refresh(String resourceType, String resourceId) {
101123

102124
try {
103125
boolean isMandatoryRefresh = need_refresh(resourceType, resourceId, this.mandatoryRefreshTimeout);
104-
protectedRefresh(resourceType, resourceId, isMandatoryRefresh);
126+
protectedRefresh(resourceType, resourceId, isMandatoryRefresh, projectName);
105127
} finally {
106128
lock.writeLock().unlock();
107129
}
@@ -111,7 +133,7 @@ private void refresh(String resourceType, String resourceId) {
111133
if (!need_refresh(resourceType, resourceId, this.mandatoryRefreshTimeout)) {
112134
return;
113135
}
114-
protectedRefresh(resourceType, resourceId, true);
136+
protectedRefresh(resourceType, resourceId, true, projectName);
115137
} finally {
116138
lock.writeLock().unlock();
117139
}
@@ -127,12 +149,12 @@ private boolean need_refresh(String resourceType, String resourceId, Integer ref
127149
return tokenInfo.getExpiredTime() - System.currentTimeMillis() / 1000 < refresh_in;
128150
}
129151

130-
private void protectedRefresh(String resourceType, String resourceId, boolean isMandatory) {
152+
private void protectedRefresh(String resourceType, String resourceId, boolean isMandatory, String projectName) {
131153
this.resourceStsTokens.compute(getResourceKey(resourceType, resourceId), new BiFunction<String, ArkResourceStsTokenInfo, ArkResourceStsTokenInfo>() {
132154
@Override
133155
public ArkResourceStsTokenInfo apply(String s, ArkResourceStsTokenInfo stringIntegerPair) {
134156
try {
135-
ArkResourceStsTokenInfo tokenInfo = getToken(resourceType, resourceId, Const.DEFAULT_STS_TIMEOUT);
157+
ArkResourceStsTokenInfo tokenInfo = getToken(resourceType, resourceId, Const.DEFAULT_STS_TIMEOUT, projectName);
136158
return tokenInfo;
137159
} catch (ApiException e) {
138160
if (isMandatory) {
@@ -145,17 +167,20 @@ public ArkResourceStsTokenInfo apply(String s, ArkResourceStsTokenInfo stringInt
145167
}
146168

147169
private ArkResourceStsTokenInfo getEndpointToken(String endpointId, Integer ttl) throws ApiException {
148-
return getToken("endpoint", endpointId, ttl);
170+
return getToken("endpoint", endpointId, ttl, "");
149171
}
150172

151-
private ArkResourceStsTokenInfo getToken(String resourceType, String resourceId, Integer ttl) throws ApiException {
173+
private ArkResourceStsTokenInfo getToken(String resourceType, String resourceId, Integer ttl, String projectName) throws ApiException {
152174
if (ttl < this.advisoryRefreshTimeout * 2) {
153175
throw new ArkException("ttl should not be under " + this.advisoryRefreshTimeout * 2 + " seconds.");
154176
}
155177

156178
GetApiKeyRequest r = new GetApiKeyRequest();
157179
r.durationSeconds(ttl);
158180
r.resourceType(resourceType);
181+
if (StringUtils.isNotBlank(projectName)) {
182+
r.projectName(projectName);
183+
}
159184
List<String> list = new ArrayList<>();
160185
list.add(resourceId);
161186
r.resourceIds(list);

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/interceptor/EncryptionInterceptor.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
import java.net.URI;
1010
import java.net.URISyntaxException;
1111

12-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1312

1413
import java.security.GeneralSecurityException;
15-
import java.security.Security;
1614
import java.util.HashMap;
1715
import java.util.Map;
1816

@@ -35,10 +33,6 @@ public class EncryptionInterceptor implements Interceptor {
3533
private final String baseUrl;
3634
private final ObjectMapper mapper = new ObjectMapper();
3735

38-
static {
39-
Security.addProvider(new BouncyCastleProvider());
40-
}
41-
4236
public EncryptionInterceptor(String apiKey, String baseUrl) {
4337
this.baseUrl = baseUrl;
4438
this.apiKey = apiKey;
@@ -112,21 +106,6 @@ private Map<String, Object> parseRequestBody(RequestBody body) throws IOExceptio
112106
});
113107
}
114108

115-
/**
116-
* 非加密模式处理 - 直接转发请求
117-
*/
118-
private Response proceedWithoutEncryption(Chain chain, Request request, Map<String, Object> requestBodyJson) throws IOException {
119-
String modifiedRequestBodyStr = mapper.writeValueAsString(requestBodyJson);
120-
RequestBody modifiedBody = RequestBody.create(
121-
MediaType.get("application/json"),
122-
modifiedRequestBodyStr
123-
);
124-
Request modifiedRequest = request.newBuilder()
125-
.method(request.method(), modifiedBody)
126-
.build();
127-
return chain.proceed(modifiedRequest);
128-
}
129-
130109
/**
131110
* 加密请求体内容
132111
*/
@@ -554,4 +533,4 @@ private String getEncryptedContentFromStreamChoice(Map<String, Object> choice) {
554533
Map<String, Object> delta = (Map<String, Object>) choice.get("delta");
555534
return (String) delta.get("content");
556535
}
557-
}
536+
}

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/model/content/generation/CreateContentGenerationTaskRequest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public class CreateContentGenerationTaskRequest {
2020
@JsonProperty("return_last_frame")
2121
private Boolean returnLastFrame;
2222

23+
@JsonProperty("service_tier")
24+
private String serviceTier;
25+
26+
@JsonProperty("execution_expires_after")
27+
private Long executionExpiresAfter;
28+
2329
public CreateContentGenerationTaskRequest() {
2430
}
2531

@@ -67,13 +73,31 @@ public void setReturnLastFrame(Boolean returnLastFrame) {
6773
this.returnLastFrame = returnLastFrame;
6874
}
6975

76+
public String getServiceTier() {
77+
return serviceTier;
78+
}
79+
80+
public void setServiceTier(String serviceTier) {
81+
this.serviceTier = serviceTier;
82+
}
83+
84+
public Long getExecutionExpiresAfter() {
85+
return executionExpiresAfter;
86+
}
87+
88+
public void setExecutionExpiresAfter(Long executionExpiresAfter) {
89+
this.executionExpiresAfter = executionExpiresAfter;
90+
}
91+
7092
@Override
7193
public String toString() {
7294
return "CreateContentGenerationTaskRequest{" +
7395
"model='" + model + '\'' +
7496
", content=" + content +
7597
", callbackUrl='" + callbackUrl + '\'' +
7698
", returnLastFrame=" + returnLastFrame +
99+
", serviceTier='" + serviceTier + '\'' +
100+
", executionExpiresAfter=" + executionExpiresAfter +
77101
'}';
78102
}
79103

@@ -86,6 +110,8 @@ public static class Builder {
86110
private List<Content> content;
87111
private String callbackUrl;
88112
private Boolean returnLastFrame;
113+
private String serviceTier;
114+
private Long executionExpiresAfter;
89115

90116
private Builder() {
91117
}
@@ -110,12 +136,24 @@ public Builder returnLastFrame(Boolean returnLastFrame) {
110136
return this;
111137
}
112138

139+
public Builder serviceTier(String serviceTier) {
140+
this.serviceTier = serviceTier;
141+
return this;
142+
}
143+
144+
public Builder executionExpiresAfter(Long executionExpiresAfter) {
145+
this.executionExpiresAfter = executionExpiresAfter;
146+
return this;
147+
}
148+
113149
public CreateContentGenerationTaskRequest build() {
114150
CreateContentGenerationTaskRequest createContentGenerationTaskRequest = new CreateContentGenerationTaskRequest();
115151
createContentGenerationTaskRequest.setModel(model);
116152
createContentGenerationTaskRequest.setContent(content);
117153
createContentGenerationTaskRequest.setCallbackUrl(callbackUrl);
118154
createContentGenerationTaskRequest.setReturnLastFrame(returnLastFrame);
155+
createContentGenerationTaskRequest.setServiceTier(serviceTier);
156+
createContentGenerationTaskRequest.setExecutionExpiresAfter(executionExpiresAfter);
119157
return createContentGenerationTaskRequest;
120158
}
121159
}

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/model/content/generation/GetContentGenerationTaskResponse.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public class GetContentGenerationTaskResponse {
5151
@JsonProperty("revised_prompt")
5252
private String revisedPrompt;
5353

54+
@JsonProperty("service_tier")
55+
private String serviceTier;
56+
57+
@JsonProperty("execution_expires_after")
58+
private java.lang.Long executionExpiresAfter;
59+
5460
public String getId() {
5561
return id;
5662
}
@@ -163,6 +169,22 @@ public void setRevisedPrompt(String revisedPrompt) {
163169
this.revisedPrompt = revisedPrompt;
164170
}
165171

172+
public String getServiceTier() {
173+
return serviceTier;
174+
}
175+
176+
public void setServiceTier(String serviceTier) {
177+
this.serviceTier = serviceTier;
178+
}
179+
180+
public java.lang.Long getExecutionExpiresAfter() {
181+
return executionExpiresAfter;
182+
}
183+
184+
public void setExecutionExpiresAfter(java.lang.Long executionExpiresAfter) {
185+
this.executionExpiresAfter = executionExpiresAfter;
186+
}
187+
166188
@JsonIgnoreProperties(ignoreUnknown = true)
167189
public static class Content {
168190

@@ -274,6 +296,8 @@ public String toString() {
274296
", updatedAt=" + updatedAt +
275297
", seed=" + seed +
276298
", revisedPrompt=" + revisedPrompt +
299+
", serviceTier='" + serviceTier + '\'' +
300+
", executionExpiresAfter=" + executionExpiresAfter +
277301
'}';
278302
}
279303
}

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/model/content/generation/ListContentGenerationTasksRequest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class ListContentGenerationTasksRequest {
1818
private String status;
1919
private String model;
2020
private List<String> taskIds;
21+
private String serviceTier;
2122

2223
public ListContentGenerationTasksRequest() {
2324
}
@@ -70,6 +71,14 @@ public void setTaskIds(List<String> taskIds) {
7071
this.taskIds = taskIds;
7172
}
7273

74+
public String getServiceTier() {
75+
return serviceTier;
76+
}
77+
78+
public void setServiceTier(String serviceTier) {
79+
this.serviceTier = serviceTier;
80+
}
81+
7382
@Override
7483
public String toString() {
7584
return "ListContentGenerationTasksRequest{" +
@@ -91,6 +100,7 @@ public static class Builder {
91100
private String status;
92101
private String model;
93102
private final List<String> taskIds = new ArrayList<>();
103+
private String serviceTier;
94104

95105
public Builder pageNum(java.lang.Integer pageNum) {
96106
this.pageNum = pageNum;
@@ -112,6 +122,11 @@ public Builder model(String model) {
112122
return this;
113123
}
114124

125+
public Builder serviceTier(String serviceTier) {
126+
this.serviceTier = serviceTier;
127+
return this;
128+
}
129+
115130
public Builder taskIds(List<String> taskIds) {
116131
this.taskIds.clear();
117132
this.taskIds.addAll(taskIds);
@@ -124,7 +139,9 @@ public Builder addTaskId(String taskId) {
124139
}
125140

126141
public ListContentGenerationTasksRequest build() {
127-
return new ListContentGenerationTasksRequest(pageNum, pageSize, status, model, taskIds);
142+
ListContentGenerationTasksRequest req = new ListContentGenerationTasksRequest(pageNum, pageSize, status, model, taskIds);
143+
req.setServiceTier(serviceTier);
144+
return req;
128145
}
129146
}
130147
}

0 commit comments

Comments
 (0)