Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Commit 360e187

Browse files
author
hodge
committed
Release 1.3.3
1 parent 7086dfa commit 360e187

File tree

9 files changed

+157
-19
lines changed

9 files changed

+157
-19
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ dify-java-client
66
<img alt="maven-central" src="https://img.shields.io/badge/Java-17-blue" />
77
</a>
88
<a href="https://central.sonatype.com/artifact/io.github.yuanbaobaoo/dify-java-client" target="_blank">
9-
<img alt="maven-central" src="https://img.shields.io/badge/maven--central-1.3.2-green" />
9+
<img alt="maven-central" src="https://img.shields.io/badge/maven--central-1.3.3-green" />
1010
</a>
1111
</p>
1212

13-
简单易用的 Dify Java客户端
13+
简单易用的 Dify Java客户端,支持在任意Java项目中使用 [Dify](https://dify.ai/) 的工作流、对话助手、Agent、Dataset、文字转语音、语音转文字等开放能力。
1414

1515
中文 | [English](./README_EN.md)
1616

@@ -26,7 +26,7 @@ Dify Version: <= 1.x
2626
<dependency>
2727
<groupId>io.github.yuanbaobaoo</groupId>
2828
<artifactId>dify-java-client</artifactId>
29-
<version>1.3.2</version>
29+
<version>1.3.3</version>
3030
</dependency>
3131
```
3232

README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ dify-java-client
66
<img alt="maven-central" src="https://img.shields.io/badge/Java-17-blue" />
77
</a>
88
<a href="https://central.sonatype.com/artifact/io.github.yuanbaobaoo/dify-java-client" target="_blank">
9-
<img alt="maven-central" src="https://img.shields.io/badge/maven--central-1.3.2-green" />
9+
<img alt="maven-central" src="https://img.shields.io/badge/maven--central-1.3.3-green" />
1010
</a>
1111
</p>
1212

13-
The Simple and Easy-to-Use Dify Java Client
13+
The Simple and Easy-to-Use Dify Java Client, Support the use of [Dify](https://dify.ai/) Workflow, Assistant, Agent, Dataset, text to speech, voice to text, etc...
1414

1515
[中文](./README.md) | English
1616

@@ -26,7 +26,7 @@ Dify Version: <= 1.x
2626
<dependency>
2727
<groupId>io.github.yuanbaobaoo</groupId>
2828
<artifactId>dify-java-client</artifactId>
29-
<version>1.3.2</version>
29+
<version>1.3.3</version>
3030
</dependency>
3131
```
3232

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>io.github.yuanbaobaoo</groupId>
66
<artifactId>dify-java-client</artifactId>
7-
<version>1.3.2</version>
7+
<version>1.3.3</version>
88
<description>The Simple Dify Java Client</description>
99
<url>https://github.com/yuanbaobaoo/dify-java-client</url>
1010

src/main/java/io/github/yuanbaobaoo/dify/DifyHttpClient.java

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package io.github.yuanbaobaoo.dify;
22

33
import com.alibaba.fastjson2.JSON;
4-
import io.github.yuanbaobaoo.dify.types.DifyClientException;
5-
import io.github.yuanbaobaoo.dify.types.DifyException;
6-
import io.github.yuanbaobaoo.dify.types.DifyRoute;
7-
import io.github.yuanbaobaoo.dify.types.HttpMethod;
4+
import io.github.yuanbaobaoo.dify.types.*;
85

96
import java.io.File;
107
import java.io.IOException;
@@ -14,10 +11,7 @@
1411
import java.net.http.HttpResponse;
1512
import java.nio.file.Files;
1613
import java.nio.file.Path;
17-
import java.util.ArrayList;
18-
import java.util.HashMap;
19-
import java.util.Map;
20-
import java.util.UUID;
14+
import java.util.*;
2115
import java.util.concurrent.CompletableFuture;
2216
import java.util.concurrent.ConcurrentHashMap;
2317
import java.util.function.Consumer;
@@ -44,6 +38,7 @@ private DifyHttpClient(String server, String apiKey) {
4438

4539
/**
4640
* 从缓存池中获取一个client对象,如果没有则创建
41+
*
4742
* @param config DifyConfig
4843
*/
4944
public static DifyHttpClient get(DifyConfig config) {
@@ -52,6 +47,7 @@ public static DifyHttpClient get(DifyConfig config) {
5247

5348
/**
5449
* 从缓存池中获取一个client对象,如果没有则创建
50+
*
5551
* @param server Dify Server Address
5652
* @param apiKey Api Key
5753
*/
@@ -61,6 +57,7 @@ public static DifyHttpClient get(String server, String apiKey) {
6157

6258
/**
6359
* 实例化一个新对象
60+
*
6461
* @param server Dify Server Address
6562
* @param apiKey Api Key
6663
*/
@@ -140,6 +137,64 @@ public String requestJson(String url, HttpMethod method, Map<String, Object> que
140137
}
141138
}
142139

140+
/**
141+
* request by application/json content-type. return File bytes
142+
*
143+
* @param route DifyRoute
144+
* @param query query params
145+
* @param params body params
146+
*/
147+
public DifyFile requestFile(DifyRoute route, Map<String, Object> query, Object params) {
148+
return requestFile(route.getUrl(), route.getMethod(), query, params);
149+
}
150+
151+
/**
152+
* request by application/json content-type. return File bytes
153+
*
154+
* @param url API URL
155+
* @param method HTTP METHOD
156+
* @param query query map
157+
* @param params body map
158+
*/
159+
public DifyFile requestFile(String url, HttpMethod method, Map<String, Object> query, Object params) {
160+
try {
161+
HttpRequest.Builder builder = buildRequest(url, query);
162+
163+
if (params == null) {
164+
params = new HashMap<>();
165+
}
166+
167+
if (method == HttpMethod.GET) {
168+
builder.method(method.name(), HttpRequest.BodyPublishers.noBody());
169+
} else {
170+
builder.header("Content-Type", "application/json");
171+
builder.method(method.name(), HttpRequest.BodyPublishers.ofString(JSON.toJSONString(params)));
172+
}
173+
174+
HttpResponse<byte[]> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofByteArray());
175+
176+
// 所有非200响应都被视为dify平台的错误响应,统一返回异常对象
177+
if (response.statusCode() >= 400) {
178+
throw new DifyException(new String(response.body()), response.statusCode());
179+
}
180+
181+
String suffix = null;
182+
String contentType = null;
183+
Optional<String> contentTypeOpt = response.headers().firstValue("Content-Type");
184+
185+
if (contentTypeOpt.isPresent()) {
186+
contentType = contentTypeOpt.get();
187+
suffix = contentType.replaceAll(".*/", "");
188+
}
189+
190+
return DifyFile.builder().type(contentType).suffix(suffix).data(response.body()).build();
191+
} catch (DifyException e) {
192+
throw e;
193+
} catch (Exception e) {
194+
throw new DifyClientException(e);
195+
}
196+
}
197+
143198
/**
144199
* async request by application/json content-type
145200
*
@@ -215,9 +270,9 @@ public String requestMultipart(String url, HttpMethod method, Map<String, Object
215270
}
216271

217272
return response.body();
218-
} catch (DifyException e) {
273+
} catch (DifyException e) {
219274
throw e;
220-
} catch (Exception e) {
275+
} catch (Exception e) {
221276
throw new DifyClientException(e);
222277
}
223278
}

src/main/java/io/github/yuanbaobaoo/dify/app/IDifyBaseClient.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.yuanbaobaoo.dify.app;
22

33
import io.github.yuanbaobaoo.dify.app.types.DifyFileResult;
4+
import io.github.yuanbaobaoo.dify.types.DifyFile;
45
import io.github.yuanbaobaoo.dify.types.DifyRoute;
56

67
import java.io.File;
@@ -61,6 +62,32 @@ public interface IDifyBaseClient {
6162
*/
6263
String audioToText(File file, String user);
6364

65+
/**
66+
* 文字转语音
67+
*
68+
* @param user 用户标识,由开发者定义规则,需保证用户标识在应用内唯一
69+
* @param messageId Dify 生成的文本消息 message-id
70+
*/
71+
DifyFile textToAudioByMessage(String user, String messageId);
72+
73+
/**
74+
* 文字转语音
75+
*
76+
* @param user 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
77+
* @param text 语音生成内容。
78+
*/
79+
DifyFile textToAudio(String user, String text);
80+
81+
/**
82+
* 文字转语音
83+
*
84+
* @param user 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
85+
* @param text 语音生成内容。如果没有传 message-id的话,则会使用这个字段的内容
86+
* @param messageId Dify 生成的文本消息,那么直接传递生成的message-id 即可,后台会通过 message_id 查找相应的内容直接合成语音信息。
87+
* 如果同时传 message_id 和 text,优先使用 message_id。
88+
*/
89+
DifyFile textToAudio(String user, String text, String messageId);
90+
6491
/**
6592
* 发送同步接口请求
6693
*

src/main/java/io/github/yuanbaobaoo/dify/app/impl/BaseClientImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.github.yuanbaobaoo.dify.app.IDifyBaseClient;
88
import io.github.yuanbaobaoo.dify.app.types.DifyFileResult;
99
import io.github.yuanbaobaoo.dify.routes.AppRoutes;
10+
import io.github.yuanbaobaoo.dify.types.DifyFile;
1011
import io.github.yuanbaobaoo.dify.types.HttpMethod;
1112
import io.github.yuanbaobaoo.dify.types.DifyException;
1213
import io.github.yuanbaobaoo.dify.types.DifyRoute;
@@ -96,6 +97,26 @@ public String audioToText(File file, String user) {
9697
return result.getString("text");
9798
}
9899

100+
@Override
101+
public DifyFile textToAudioByMessage(String user, String messageId) {
102+
return textToAudio(user, null, messageId);
103+
}
104+
105+
@Override
106+
public DifyFile textToAudio(String user, String text) {
107+
return textToAudio(user, text, null);
108+
}
109+
110+
@Override
111+
public DifyFile textToAudio(String user, String text, String messageId) {
112+
Map<String, Object> data = new HashMap<>();
113+
data.put("message_id", messageId);
114+
data.put("text", text);
115+
data.put("user", user);
116+
117+
return DifyHttpClient.get(config).requestFile(AppRoutes.TEXT_TO_AUDIO, null, data);
118+
}
119+
99120
@Override
100121
public String requestBlocking(DifyRoute route, Map<String, Object> query, Map<String, Object> params) {
101122
// body请求对象中,强制覆盖 response_mode

src/main/java/io/github/yuanbaobaoo/dify/routes/AppRoutes.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,16 @@ public class AppRoutes {
4747

4848
/**
4949
* Desc: Audio to Text
50-
* Type: ChatFlow、Chat、Agent、Completion
50+
* Type: ChatFlow、Chat、Agent
5151
*/
5252
public static final DifyRoute AUDIO_TO_TEXT = new DifyRoute("/audio-to-text", HttpMethod.POST);
5353

54+
/**
55+
* Text to Audio
56+
* Type: ChatFlow、Chat、Agent、Completion
57+
*/
58+
public static final DifyRoute TEXT_TO_AUDIO = new DifyRoute("/text-to-audio", HttpMethod.POST);
59+
5460
/**
5561
* Desc: run workflow
5662
* Type: workflow
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.yuanbaobaoo.dify.types;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
@Builder
10+
public class DifyFile {
11+
/**
12+
* 返回的文件Content-Type
13+
*/
14+
private String type;
15+
16+
/**
17+
* 文件默认后缀
18+
*/
19+
private String suffix;
20+
21+
/**
22+
* 文件内容
23+
*/
24+
private byte[] data;
25+
}

src/test/java/io/github/yuanbaobaoo/dify/DifyClientTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
import java.util.concurrent.CompletableFuture;
1111

1212
public class DifyClientTest {
13-
IDifyBaseClient client = DifyClientBuilder.base().baseUrl("http://localhost:4000").apiKey("app-").build();
13+
IDifyBaseClient client = DifyClientBuilder
14+
.base()
15+
.baseUrl("https://api.dify.ai/v1")
16+
.apiKey("app-")
17+
.build();
1418

1519
@Test
1620
public void blockTest() {

0 commit comments

Comments
 (0)