Pārlūkot izejas kodu

【功能新增】AI:讯飞 PPT API 对接,测试用例完善

xiaoxin 5 mēneši atpakaļ
vecāks
revīzija
ecc3bd281c

+ 27 - 31
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/wenduoduo/api/WddApi.java → yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/wenduoduo/api/WddPptApi.java

@@ -33,7 +33,7 @@ import java.util.function.Predicate;
  * @author xiaoxin
  */
 @Slf4j
-public class WddApi {
+public class WddPptApi {
 
     public static final String BASE_URL = "https://docmee.cn";
 
@@ -49,7 +49,7 @@ public class WddApi {
                 sink.error(new IllegalStateException("[wdd-api] 调用失败!"));
             });
 
-    public WddApi(String baseUrl) {
+    public WddPptApi(String baseUrl) {
         this.webClient = WebClient.builder()
                 .baseUrl(baseUrl)
                 .defaultHeaders((headers) -> headers.setContentType(MediaType.APPLICATION_JSON))
@@ -162,71 +162,67 @@ public class WddApi {
     }
 
     /**
-     * 生成大纲内容
+     * 分页查询PPT模板
      *
-     * @return 大纲内容流
+     * @param token   令牌
+     * @param request 请求体
+     * @return 模板列表
      */
-    public Flux<Map<String, Object>> generateOutlineContent(String token, GenerateOutlineRequest request) {
+    public PagePptTemplateInfo getTemplatePage(String token, TemplateQueryRequest request) {
         return this.webClient.post()
-                .uri("/api/ppt/v2/generateContent")
+                .uri("/api/ppt/templates")
                 .header("token", token)
-                .body(Mono.just(request), GenerateOutlineRequest.class)
+                .bodyValue(request)
                 .retrieve()
                 .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
-                .bodyToFlux(new ParameterizedTypeReference<>() {
-                });
+                .bodyToMono(new ParameterizedTypeReference<PagePptTemplateInfo>() {
+                })
+                .block();
     }
 
     /**
-     * 修改大纲内容
+     * 生成大纲内容
      *
-     * @param id       任务ID
-     * @param markdown 大纲内容markdown
-     * @param question 用户修改建议
      * @return 大纲内容流
      */
-    public Flux<Map<String, Object>> updateOutlineContent(String token, UpdateOutlineRequest request) {
+    public Flux<Map<String, Object>> createOutline(String token, CreateOutlineRequest request) {
         return this.webClient.post()
-                .uri("/api/ppt/v2/updateContent")
+                .uri("/api/ppt/v2/generateContent")
                 .header("token", token)
-                .body(Mono.just(request), UpdateOutlineRequest.class)
+                .body(Mono.just(request), CreateOutlineRequest.class)
                 .retrieve()
                 .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
                 .bodyToFlux(new ParameterizedTypeReference<>() {
                 });
     }
 
-
     /**
-     * 分页查询PPT模板
+     * 修改大纲内容
      *
-     * @param token   令牌
      * @param request 请求体
-     * @return 模板列表
+     * @return 大纲内容流
      */
-    public PagePptTemplateInfo getPptTemplatePage(String token, TemplateQueryRequest request) {
+    public Flux<Map<String, Object>> updateOutline(String token, UpdateOutlineRequest request) {
         return this.webClient.post()
-                .uri("/api/ppt/templates")
+                .uri("/api/ppt/v2/updateContent")
                 .header("token", token)
-                .bodyValue(request)
+                .body(Mono.just(request), UpdateOutlineRequest.class)
                 .retrieve()
                 .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
-                .bodyToMono(new ParameterizedTypeReference<PagePptTemplateInfo>() {
-                })
-                .block();
+                .bodyToFlux(new ParameterizedTypeReference<>() {
+                });
     }
 
-
     /**
      * 生成PPT
      *
      * @return PPT信息
      */
-    public PptInfo generatePptx(String token, GeneratePptxRequest request) {
+    public PptInfo create(String token, CreatePptRequest request) {
         return this.webClient.post()
                 .uri("/api/ppt/v2/generatePptx")
                 .header("token", token)
-                .body(Mono.just(request), GeneratePptxRequest.class)
+                .body(Mono.just(request), CreatePptRequest.class)
                 .retrieve()
                 .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
                 .bodyToMono(ApiResponse.class)
@@ -278,7 +274,7 @@ public class WddApi {
      * 生成大纲内容请求
      */
     @JsonInclude(value = JsonInclude.Include.NON_NULL)
-    public record GenerateOutlineRequest(
+    public record CreateOutlineRequest(
             String id,
             String length,
             String scene,
@@ -303,7 +299,7 @@ public class WddApi {
      * 生成PPT请求
      */
     @JsonInclude(value = JsonInclude.Include.NON_NULL)
-    public record GeneratePptxRequest(
+    public record CreatePptRequest(
             String id,
             String templateId,
             String markdown

+ 766 - 0
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/xunfei/api/XunfeiPptApi.java

@@ -0,0 +1,766 @@
+package cn.iocoder.yudao.framework.ai.core.model.xunfei.api;
+
+import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.http.HttpStatusCode;
+import org.springframework.http.MediaType;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.reactive.function.BodyInserters;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Mono;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+/**
+ * 讯飞智能PPT生成 API
+ * <p>
+ * 对接讯飞:<a href="https://www.xfyun.cn/doc/spark/PPTv2.html">智能 PPT 生成 API</a>
+ *
+ * @author xiaoxin
+ */
+@Slf4j
+public class XunfeiPptApi {
+
+    public static final String BASE_URL = "https://zwapi.xfyun.cn/api/ppt/v2";
+
+    private final WebClient webClient;
+    private final String appId;
+    private final String apiSecret;
+
+    private final Predicate<HttpStatusCode> STATUS_PREDICATE = status -> !status.is2xxSuccessful();
+
+    private final Function<Object, Function<ClientResponse, Mono<? extends Throwable>>> EXCEPTION_FUNCTION =
+            reqParam -> response -> response.bodyToMono(String.class).handle((responseBody, sink) -> {
+                log.error("[xunfei-ppt-api] 调用失败!请求参数:[{}],响应数据: [{}]", reqParam, responseBody);
+                sink.error(new IllegalStateException("[xunfei-ppt-api] 调用失败!"));
+            });
+
+    public XunfeiPptApi(String baseUrl, String appId, String apiSecret) {
+        this.webClient = WebClient.builder()
+                .baseUrl(baseUrl)
+                .build();
+        this.appId = appId;
+        this.apiSecret = apiSecret;
+    }
+
+    /**
+     * 获取签名
+     *
+     * @return 签名信息
+     */
+    private SignatureInfo getSignature() {
+        long timestamp = System.currentTimeMillis() / 1000;
+        String ts = String.valueOf(timestamp);
+        String signature = generateSignature(appId, apiSecret, timestamp);
+        return new SignatureInfo(appId, ts, signature);
+    }
+
+    /**
+     * 生成签名
+     *
+     * @param appId     应用ID
+     * @param apiSecret 应用密钥
+     * @param timestamp 时间戳(秒)
+     * @return 签名
+     */
+    private String generateSignature(String appId, String apiSecret, long timestamp) {
+        try {
+            String auth = md5(appId + timestamp);
+            return hmacSHA1Encrypt(auth, apiSecret);
+        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
+            log.error("[xunfei-ppt-api] 生成签名失败", e);
+            throw new IllegalStateException("[xunfei-ppt-api] 生成签名失败");
+        }
+    }
+
+    /**
+     * HMAC SHA1 加密
+     */
+    private String hmacSHA1Encrypt(String encryptText, String encryptKey)
+            throws NoSuchAlgorithmException, InvalidKeyException {
+        SecretKeySpec keySpec = new SecretKeySpec(
+                encryptKey.getBytes(StandardCharsets.UTF_8), "HmacSHA1");
+
+        Mac mac = Mac.getInstance("HmacSHA1");
+        mac.init(keySpec);
+        byte[] result = mac.doFinal(encryptText.getBytes(StandardCharsets.UTF_8));
+
+        return Base64.getEncoder().encodeToString(result);
+    }
+
+    /**
+     * MD5 哈希
+     */
+    private String md5(String text) throws NoSuchAlgorithmException {
+        MessageDigest md = MessageDigest.getInstance("MD5");
+        byte[] digest = md.digest(text.getBytes(StandardCharsets.UTF_8));
+
+        StringBuilder sb = new StringBuilder();
+        for (byte b : digest) {
+            sb.append(String.format("%02x", b));
+        }
+        return sb.toString();
+    }
+
+    /**
+     * 获取PPT模板列表
+     *
+     * @param style    风格,如"商务"
+     * @param pageSize 每页数量
+     * @return 模板列表
+     */
+    public TemplatePageResponse getTemplatePage(String style, Integer pageSize) {
+        SignatureInfo signInfo = getSignature();
+        Map<String, Object> requestBody = new HashMap<>();
+        requestBody.put("style", style);
+        requestBody.put("pageSize", pageSize != null ? pageSize.toString() : "10");
+
+        return this.webClient.post()
+                .uri("/template/list")
+                .header("appId", signInfo.appId)
+                .header("timestamp", signInfo.timestamp)
+                .header("signature", signInfo.signature)
+                .contentType(MediaType.APPLICATION_JSON)
+                .bodyValue(requestBody)
+                .retrieve()
+                .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(requestBody))
+                .bodyToMono(TemplatePageResponse.class)
+                .block();
+    }
+
+    /**
+     * 创建大纲(通过文本)
+     *
+     * @param query 查询文本
+     * @return 大纲创建响应
+     */
+    public CreateResponse createOutline(String query) {
+        SignatureInfo signInfo = getSignature();
+        MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
+        formData.add("query", query);
+
+        return this.webClient.post()
+                .uri("/createOutline")
+                .header("appId", signInfo.appId)
+                .header("timestamp", signInfo.timestamp)
+                .header("signature", signInfo.signature)
+                .contentType(MediaType.MULTIPART_FORM_DATA)
+                .body(BodyInserters.fromMultipartData(formData))
+                .retrieve()
+                .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData))
+                .bodyToMono(CreateResponse.class)
+                .block();
+    }
+
+
+    /**
+     * 直接创建PPT(简化版 - 通过文本)
+     *
+     * @param query 查询文本
+     * @return 创建响应
+     */
+    public CreateResponse create(String query) {
+        CreatePptRequest request = CreatePptRequest.builder()
+                .query(query)
+                .build();
+        return create(request);
+    }
+
+    /**
+     * 直接创建PPT(简化版 - 通过文件)
+     *
+     * @param file     文件
+     * @param fileName 文件名
+     * @return 创建响应
+     */
+    public CreateResponse create(MultipartFile file, String fileName) {
+        CreatePptRequest request = CreatePptRequest.builder()
+                .file(file)
+                .fileName(fileName)
+                .build();
+        return create(request);
+    }
+
+    /**
+     * 直接创建PPT(完整版)
+     *
+     * @param request 请求参数
+     * @return 创建响应
+     */
+    public CreateResponse create(CreatePptRequest request) {
+        SignatureInfo signInfo = getSignature();
+        MultiValueMap<String, Object> formData = buildCreateFormData(request);
+
+        return this.webClient.post()
+                .uri("/create")
+                .header("appId", signInfo.appId)
+                .header("timestamp", signInfo.timestamp)
+                .header("signature", signInfo.signature)
+                .contentType(MediaType.MULTIPART_FORM_DATA)
+                .body(BodyInserters.fromMultipartData(formData))
+                .retrieve()
+                .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData))
+                .bodyToMono(CreateResponse.class)
+                .block();
+    }
+
+
+    /**
+     * 通过大纲创建PPT(简化版)
+     *
+     * @param outline 大纲内容
+     * @param query   查询文本
+     * @return 创建响应
+     */
+    public CreateResponse createPptByOutline(OutlineData outline, String query) {
+        CreatePptByOutlineRequest request = CreatePptByOutlineRequest.builder()
+                .outline(outline)
+                .query(query)
+                .build();
+        return createPptByOutline(request);
+    }
+
+    /**
+     * 通过大纲创建PPT(完整版)
+     *
+     * @param request 请求参数
+     * @return 创建响应
+     */
+    public CreateResponse createPptByOutline(CreatePptByOutlineRequest request) {
+        SignatureInfo signInfo = getSignature();
+
+        return this.webClient.post()
+                .uri("/createPptByOutline")
+                .header("appId", signInfo.appId)
+                .header("timestamp", signInfo.timestamp)
+                .header("signature", signInfo.signature)
+                .contentType(MediaType.APPLICATION_JSON)
+                .bodyValue(request)
+                .retrieve()
+                .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
+                .bodyToMono(CreateResponse.class)
+                .block();
+    }
+
+    /**
+     * 检查PPT生成进度
+     *
+     * @param sid 任务ID
+     * @return 进度响应
+     */
+    public ProgressResponse checkProgress(String sid) {
+        SignatureInfo signInfo = getSignature();
+
+        return this.webClient.get()
+                .uri(uriBuilder -> uriBuilder
+                        .path("/progress")
+                        .queryParam("sid", sid)
+                        .build())
+                .header("appId", signInfo.appId)
+                .header("timestamp", signInfo.timestamp)
+                .header("signature", signInfo.signature)
+                .retrieve()
+                .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(sid))
+                .bodyToMono(ProgressResponse.class)
+                .block();
+    }
+
+    /**
+     * 签名信息
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    private record SignatureInfo(
+            String appId,
+            String timestamp,
+            String signature
+    ) {
+    }
+
+    /**
+     * 模板列表响应
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record TemplatePageResponse(
+            boolean flag,
+            int code,
+            String desc,
+            Integer count,
+            TemplatePageData data
+    ) {
+    }
+
+    /**
+     * 模板列表数据
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record TemplatePageData(
+            String total,
+            List<TemplateInfo> records,
+            Integer pageNum
+    ) {
+    }
+
+    /**
+     * 模板信息
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record TemplateInfo(
+            String templateIndexId,
+            Integer pageCount,
+            String type,
+            String color,
+            String industry,
+            String style,
+            String detailImage
+    ) {
+    }
+
+    /**
+     * 创建响应
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record CreateResponse(
+            boolean flag,
+            int code,
+            String desc,
+            Integer count,
+            CreateResponseData data
+    ) {
+    }
+
+    /**
+     * 创建响应数据
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record CreateResponseData(
+            String sid,
+            String coverImgSrc,
+            String title,
+            String subTitle,
+            OutlineData outline
+    ) {
+    }
+
+    /**
+     * 大纲数据结构
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record OutlineData(
+            String title,
+            String subTitle,
+            List<Chapter> chapters
+    ) {
+        /**
+         * 章节结构
+         */
+        @JsonInclude(value = JsonInclude.Include.NON_NULL)
+        public record Chapter(
+                String chapterTitle,
+                List<ChapterContent> chapterContents
+        ) {
+            /**
+             * 章节内容
+             */
+            @JsonInclude(value = JsonInclude.Include.NON_NULL)
+            public record ChapterContent(
+                    String chapterTitle
+            ) {
+            }
+        }
+
+        /**
+         * 将大纲对象转换为JSON字符串
+         *
+         * @return 大纲JSON字符串
+         */
+        public String toJsonString() {
+            return JsonUtils.toJsonString(this);
+        }
+    }
+
+    /**
+     * 进度响应
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record ProgressResponse(
+            int code,
+            String desc,
+            ProgressResponseData data
+    ) {
+    }
+
+    /**
+     * 进度响应数据
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record ProgressResponseData(
+            int process,
+            String pptId,
+            String pptUrl,
+            String pptStatus,         // PPT构建状态:building(构建中),done(已完成),build_failed(生成失败)
+            String aiImageStatus,     // ai配图状态:building(构建中),done(已完成)
+            String cardNoteStatus,    // 演讲备注状态:building(构建中),done(已完成)
+            String errMsg,            // 生成PPT的失败信息
+            Integer totalPages,       // 生成PPT的总页数
+            Integer donePages         // 生成PPT的完成页数
+    ) {
+        /**
+         * 是否全部完成
+         *
+         * @return 是否全部完成
+         */
+        public boolean isAllDone() {
+            return "done".equals(pptStatus)
+                    && ("done".equals(aiImageStatus) || aiImageStatus == null)
+                    && ("done".equals(cardNoteStatus) || cardNoteStatus == null);
+        }
+
+        /**
+         * 是否失败
+         *
+         * @return 是否失败
+         */
+        public boolean isFailed() {
+            return "build_failed".equals(pptStatus);
+        }
+
+        /**
+         * 获取进度百分比
+         *
+         * @return 进度百分比
+         */
+        public int getProgressPercent() {
+            if (totalPages == null || totalPages == 0 || donePages == null) {
+                return process; // 兼容旧版返回
+            }
+            return (int) (donePages * 100.0 / totalPages);
+        }
+    }
+
+    /**
+     * 通过大纲创建PPT请求参数
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record CreatePptByOutlineRequest(
+            String query,                // 用户生成PPT要求(最多8000字)
+            String outlineSid,           // 已生成大纲后,响应返回的请求大纲唯一id
+            OutlineData outline,         // 大纲内容
+            String templateId,           // 模板ID
+            String businessId,           // 业务ID(非必传)
+            String author,               // PPT作者名
+            Boolean isCardNote,          // 是否生成PPT演讲备注
+            Boolean search,              // 是否联网搜索
+            String language,             // 语种
+            String fileUrl,              // 文件地址
+            String fileName,             // 文件名(带文件名后缀)
+            Boolean isFigure,            // 是否自动配图
+            String aiImage               // ai配图类型:normal、advanced
+    ) {
+        /**
+         * 创建构建器
+         *
+         * @return 构建器
+         */
+        public static Builder builder() {
+            return new Builder();
+        }
+
+        /**
+         * 构建器类
+         */
+        public static class Builder {
+            private String query;
+            private String outlineSid;
+            private OutlineData outline;
+            private String templateId;
+            private String businessId;
+            private String author;
+            private Boolean isCardNote;
+            private Boolean search;
+            private String language;
+            private String fileUrl;
+            private String fileName;
+            private Boolean isFigure;
+            private String aiImage;
+
+            public Builder query(String query) {
+                this.query = query;
+                return this;
+            }
+
+            public Builder outlineSid(String outlineSid) {
+                this.outlineSid = outlineSid;
+                return this;
+            }
+
+            public Builder outline(OutlineData outline) {
+                this.outline = outline;
+                return this;
+            }
+
+            public Builder templateId(String templateId) {
+                this.templateId = templateId;
+                return this;
+            }
+
+            public Builder businessId(String businessId) {
+                this.businessId = businessId;
+                return this;
+            }
+
+            public Builder author(String author) {
+                this.author = author;
+                return this;
+            }
+
+            public Builder isCardNote(Boolean isCardNote) {
+                this.isCardNote = isCardNote;
+                return this;
+            }
+
+            public Builder search(Boolean search) {
+                this.search = search;
+                return this;
+            }
+
+            public Builder language(String language) {
+                this.language = language;
+                return this;
+            }
+
+            public Builder fileUrl(String fileUrl) {
+                this.fileUrl = fileUrl;
+                return this;
+            }
+
+            public Builder fileName(String fileName) {
+                this.fileName = fileName;
+                return this;
+            }
+
+            public Builder isFigure(Boolean isFigure) {
+                this.isFigure = isFigure;
+                return this;
+            }
+
+            public Builder aiImage(String aiImage) {
+                this.aiImage = aiImage;
+                return this;
+            }
+
+            public CreatePptByOutlineRequest build() {
+                return new CreatePptByOutlineRequest(
+                        query, outlineSid, outline, templateId, businessId, author,
+                        isCardNote, search, language, fileUrl, fileName, isFigure, aiImage
+                );
+            }
+        }
+    }
+
+    /**
+     * 构建创建PPT的表单数据
+     *
+     * @param request 请求参数
+     * @return 表单数据
+     */
+    private MultiValueMap<String, Object> buildCreateFormData(CreatePptRequest request) {
+        MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
+        // 添加请求参数
+        if (request.query() != null) {
+            formData.add("query", request.query());
+        }
+
+        if (request.file() != null) {
+            try {
+                formData.add("file", new ByteArrayResource(request.file().getBytes()) {
+                    @Override
+                    public String getFilename() {
+                        return request.file().getOriginalFilename();
+                    }
+                });
+            } catch (IOException e) {
+                log.error("[xunfei-ppt-api] 文件处理失败", e);
+                throw new IllegalStateException("[xunfei-ppt-api] 文件处理失败", e);
+            }
+        }
+
+        if (request.fileUrl() != null) {
+            formData.add("fileUrl", request.fileUrl());
+        }
+
+        if (request.fileName() != null) {
+            formData.add("fileName", request.fileName());
+        }
+
+        if (request.templateId() != null) {
+            formData.add("templateId", request.templateId());
+        }
+
+        if (request.businessId() != null) {
+            formData.add("businessId", request.businessId());
+        }
+
+        if (request.author() != null) {
+            formData.add("author", request.author());
+        }
+
+        if (request.isCardNote() != null) {
+            formData.add("isCardNote", request.isCardNote().toString());
+        }
+
+        if (request.search() != null) {
+            formData.add("search", request.search().toString());
+        }
+
+        if (request.language() != null) {
+            formData.add("language", request.language());
+        }
+
+        if (request.isFigure() != null) {
+            formData.add("isFigure", request.isFigure().toString());
+        }
+
+        if (request.aiImage() != null) {
+            formData.add("aiImage", request.aiImage());
+        }
+
+        return formData;
+    }
+
+    /**
+     * 直接生成PPT请求参数
+     */
+    @JsonInclude(value = JsonInclude.Include.NON_NULL)
+    public record CreatePptRequest(
+            String query,                // 用户生成PPT要求(最多8000字)
+            MultipartFile file,          // 上传文件
+            String fileUrl,              // 文件地址
+            String fileName,             // 文件名(带文件名后缀)
+            String templateId,           // 模板ID
+            String businessId,           // 业务ID(非必传)
+            String author,               // PPT作者名
+            Boolean isCardNote,          // 是否生成PPT演讲备注
+            Boolean search,              // 是否联网搜索
+            String language,             // 语种
+            Boolean isFigure,            // 是否自动配图
+            String aiImage               // ai配图类型:normal、advanced
+    ) {
+        /**
+         * 创建构建器
+         *
+         * @return 构建器
+         */
+        public static Builder builder() {
+            return new Builder();
+        }
+
+        /**
+         * 构建器类
+         */
+        public static class Builder {
+            private String query;
+            private MultipartFile file;
+            private String fileUrl;
+            private String fileName;
+            private String templateId;
+            private String businessId;
+            private String author;
+            private Boolean isCardNote;
+            private Boolean search;
+            private String language;
+            private Boolean isFigure;
+            private String aiImage;
+
+            public Builder query(String query) {
+                this.query = query;
+                return this;
+            }
+
+            public Builder file(MultipartFile file) {
+                this.file = file;
+                return this;
+            }
+
+            public Builder fileUrl(String fileUrl) {
+                this.fileUrl = fileUrl;
+                return this;
+            }
+
+            public Builder fileName(String fileName) {
+                this.fileName = fileName;
+                return this;
+            }
+
+            public Builder templateId(String templateId) {
+                this.templateId = templateId;
+                return this;
+            }
+
+            public Builder businessId(String businessId) {
+                this.businessId = businessId;
+                return this;
+            }
+
+            public Builder author(String author) {
+                this.author = author;
+                return this;
+            }
+
+            public Builder isCardNote(Boolean isCardNote) {
+                this.isCardNote = isCardNote;
+                return this;
+            }
+
+            public Builder search(Boolean search) {
+                this.search = search;
+                return this;
+            }
+
+            public Builder language(String language) {
+                this.language = language;
+                return this;
+            }
+
+            public Builder isFigure(Boolean isFigure) {
+                this.isFigure = isFigure;
+                return this;
+            }
+
+            public Builder aiImage(String aiImage) {
+                this.aiImage = aiImage;
+                return this;
+            }
+
+            public CreatePptRequest build() {
+                // 验证参数
+                if (query == null && file == null && fileUrl == null) {
+                    throw new IllegalArgumentException("query、file、fileUrl必填其一");
+                }
+                if ((file != null || fileUrl != null) && fileName == null) {
+                    throw new IllegalArgumentException("如果传file或者fileUrl,fileName必填");
+                }
+                return new CreatePptRequest(
+                        query, file, fileUrl, fileName, templateId, businessId, author,
+                        isCardNote, search, language, isFigure, aiImage
+                );
+            }
+        }
+    }
+} 

+ 19 - 19
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/ppt/wdd/WddApiTests.java → yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/ppt/wdd/WddPptApiTests.java

@@ -1,6 +1,6 @@
 package cn.iocoder.yudao.framework.ai.ppt.wdd;
 
-import cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api.WddApi;
+import cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api.WddPptApi;
 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
@@ -11,26 +11,26 @@ import java.util.Objects;
 
 
 /**
- * {@link WddApi} 集成测试
+ * {@link WddPptApi} 集成测试
  *
  * @author xiaoxin
  */
-public class WddApiTests {
+public class WddPptApiTests {
 
-    private final WddApi wddApi = new WddApi("https://docmee.cn");
+    private final WddPptApi wddPptApi = new WddPptApi("https://docmee.cn");
 
 
-    private final String token = "sk_FJo7sKErrrEs5CIZz1";
+    private final String token = "";
 
 
     @Test //获取token
     @Disabled
     public void testCreateApiToken() {
         // 准备参数
-        String apiKey = "ak_RK1rm7TrEv3E3JSWIK";
-        WddApi.CreateTokenRequest request = new WddApi.CreateTokenRequest(apiKey);
+        String apiKey = "";
+        WddPptApi.CreateTokenRequest request = new WddPptApi.CreateTokenRequest(apiKey);
         // 调用方法
-        String token = wddApi.createApiToken(request);
+        String token = wddPptApi.createApiToken(request);
         // 打印结果
         System.out.println(token);
     }
@@ -39,7 +39,7 @@ public class WddApiTests {
     @Test // 创建任务
     @Disabled
     public void testCreateTask() {
-        WddApi.ApiResponse apiResponse = wddApi.createTask(token, 1, "dify 介绍", null);
+        WddPptApi.ApiResponse apiResponse = wddPptApi.createTask(token, 1, "dify 介绍", null);
         System.out.println(apiResponse);
     }
 
@@ -47,9 +47,9 @@ public class WddApiTests {
     @Test // 创建大纲
     @Disabled
     public void testGenerateOutlineRequest() {
-        WddApi.GenerateOutlineRequest request = new WddApi.GenerateOutlineRequest("1901449466163105792", "medium", null, null, null, null);
+        WddPptApi.CreateOutlineRequest request = new WddPptApi.CreateOutlineRequest("1901539019628613632", "medium", null, null, null, null);
         //调用
-        Flux<Map<String, Object>> flux = wddApi.generateOutlineContent(token, request);
+        Flux<Map<String, Object>> flux = wddPptApi.createOutline(token, request);
         StringBuffer contentBuffer = new StringBuffer();
         flux.doOnNext(chunk -> {
             contentBuffer.append(chunk.get("text"));
@@ -66,10 +66,10 @@ public class WddApiTests {
 
     @Test // 修改大纲
     @Disabled
-    public void testUpdateOutlineContentRequest() {
-        WddApi.UpdateOutlineRequest request = new WddApi.UpdateOutlineRequest("1901449466163105792", TEST_OUT_LINE_CONTENT, "精简一点,三个章节即可");
+    public void testUpdateOutlineRequest() {
+        WddPptApi.UpdateOutlineRequest request = new WddPptApi.UpdateOutlineRequest("1901539019628613632", TEST_OUT_LINE_CONTENT, "精简一点,三个章节即可");
         //调用
-        Flux<Map<String, Object>> flux = wddApi.updateOutlineContent(token, request);
+        Flux<Map<String, Object>> flux = wddPptApi.updateOutline(token, request);
         StringBuffer contentBuffer = new StringBuffer();
         flux.doOnNext(chunk -> {
             contentBuffer.append(chunk.get("text"));
@@ -88,10 +88,10 @@ public class WddApiTests {
     @Disabled
     public void testGetPptTemplatePage() {
         // 准备参数
-        WddApi.TemplateQueryRequest.Filter filter = new WddApi.TemplateQueryRequest.Filter(1, null, null, null);
-        WddApi.TemplateQueryRequest request = new WddApi.TemplateQueryRequest(1, 10, filter);
+        WddPptApi.TemplateQueryRequest.Filter filter = new WddPptApi.TemplateQueryRequest.Filter(1, null, null, null);
+        WddPptApi.TemplateQueryRequest request = new WddPptApi.TemplateQueryRequest(1, 10, filter);
         //调用
-        WddApi.PagePptTemplateInfo pptTemplatePage = wddApi.getPptTemplatePage(token, request);
+        WddPptApi.PagePptTemplateInfo pptTemplatePage = wddPptApi.getTemplatePage(token, request);
         // 打印结果
         System.out.println(pptTemplatePage);
     }
@@ -101,9 +101,9 @@ public class WddApiTests {
     @Disabled
     public void testGeneratePptx() {
         // 准备参数
-        WddApi.GeneratePptxRequest request = new WddApi.GeneratePptxRequest("1900913633555255296", "1804885538940116992", TEST_OUT_LINE_CONTENT);
+        WddPptApi.CreatePptRequest request = new WddPptApi.CreatePptRequest("1901539019628613632", "1805081814809960448", TEST_OUT_LINE_CONTENT);
         //调用
-        WddApi.PptInfo pptInfo = wddApi.generatePptx("", request);
+        WddPptApi.PptInfo pptInfo = wddPptApi.create(token, request);
         // 打印结果
         System.out.println(pptInfo);
     }

+ 300 - 0
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/ppt/xunfei/XunfeiPptApiTests.java

@@ -0,0 +1,300 @@
+package cn.iocoder.yudao.framework.ai.ppt.xunfei;
+
+import cn.iocoder.yudao.framework.ai.core.model.xunfei.api.XunfeiPptApi;
+import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+/**
+ * {@link XunfeiPptApi} 集成测试
+ *
+ * @author xiaoxin
+ */
+public class XunfeiPptApiTests {
+
+    // 讯飞API配置信息,实际使用时请替换为您的应用信息
+    private static final String APP_ID = "";
+    private static final String API_SECRET = "";
+
+    private final XunfeiPptApi xunfeiPptApi = new XunfeiPptApi(XunfeiPptApi.BASE_URL, APP_ID, API_SECRET);
+
+    @Test // 获取PPT模板列表
+    @Disabled
+    public void testGetTemplatePage() {
+        // 调用方法
+        XunfeiPptApi.TemplatePageResponse response = xunfeiPptApi.getTemplatePage("商务", 10);
+        // 打印结果
+        System.out.println("模板列表响应:" + JsonUtils.toJsonString(response));
+
+        if (response != null && response.data() != null && response.data().records() != null) {
+            System.out.println("模板总数:" + response.data().total());
+            System.out.println("当前页码:" + response.data().pageNum());
+            System.out.println("模板数量:" + response.data().records().size());
+
+            // 打印第一个模板的信息(如果存在)
+            if (!response.data().records().isEmpty()) {
+                XunfeiPptApi.TemplateInfo firstTemplate = response.data().records().get(0);
+                System.out.println("模板ID:" + firstTemplate.templateIndexId());
+                System.out.println("模板风格:" + firstTemplate.style());
+                System.out.println("模板颜色:" + firstTemplate.color());
+                System.out.println("模板行业:" + firstTemplate.industry());
+            }
+        }
+    }
+
+    @Test // 创建大纲(通过文本)
+    @Disabled
+    public void testCreateOutline() {
+        XunfeiPptApi.CreateResponse response = getCreateResponse();
+        // 打印结果
+        System.out.println("创建大纲响应:" + JsonUtils.toJsonString(response));
+
+        // 保存sid和outline用于后续测试
+        if (response != null && response.data() != null) {
+            System.out.println("sid: " + response.data().sid());
+            if (response.data().outline() != null) {
+                // 使用OutlineData的toJsonString方法
+                System.out.println("outline: " + response.data().outline().toJsonString());
+                // 将outline对象转换为JSON字符串,用于后续createPptByOutline测试
+                String outlineJson = response.data().outline().toJsonString();
+                System.out.println("可用于createPptByOutline的outline字符串: " + outlineJson);
+            }
+        }
+    }
+
+    // 创建大纲(通过文本)
+    private XunfeiPptApi.CreateResponse getCreateResponse() {
+        // 准备参数
+        String param = "智能体平台 Dify 介绍";
+        // 调用方法
+        return xunfeiPptApi.createOutline(param);
+    }
+
+    @Test // 通过大纲创建PPT(完整参数)
+    @Disabled
+    public void testCreatePptByOutlineWithFullParams() {
+        // 创建大纲对象
+        XunfeiPptApi.CreateResponse createResponse = getCreateResponse();
+        // 调用方法
+        XunfeiPptApi.CreateResponse response = xunfeiPptApi.createPptByOutline(createResponse.data().outline(), "精简一些,不要超过6个章节");
+        // 打印结果
+        System.out.println("通过大纲创建PPT响应:" + JsonUtils.toJsonString(response));
+
+        // 保存sid用于后续进度查询
+        if (response != null && response.data() != null) {
+            System.out.println("sid: " + response.data().sid());
+            if (response.data().coverImgSrc() != null) {
+                System.out.println("封面图片: " + response.data().coverImgSrc());
+            }
+        }
+    }
+
+    @Test // 检查PPT生成进度
+    @Disabled
+    public void testCheckProgress() {
+        // 准备参数 - 使用之前创建PPT时返回的sid
+        String sid = "e96dac09f2ec4ee289f029a5fb874ecd"; // 替换为实际的sid
+
+        // 调用方法
+        XunfeiPptApi.ProgressResponse response = xunfeiPptApi.checkProgress(sid);
+        // 打印结果
+        System.out.println("检查进度响应:" + JsonUtils.toJsonString(response));
+
+        // 安全地访问响应数据
+        if (response != null && response.data() != null) {
+            XunfeiPptApi.ProgressResponseData data = response.data();
+
+            // 打印PPT生成状态
+            System.out.println("PPT构建状态: " + data.pptStatus());
+            System.out.println("AI配图状态: " + data.aiImageStatus());
+            System.out.println("演讲备注状态: " + data.cardNoteStatus());
+
+            // 打印进度信息
+            if (data.totalPages() != null && data.donePages() != null) {
+                System.out.println("总页数: " + data.totalPages());
+                System.out.println("已完成页数: " + data.donePages());
+                System.out.println("完成进度: " + data.getProgressPercent() + "%");
+            } else {
+                System.out.println("进度: " + data.process() + "%");
+            }
+
+            // 检查是否完成
+            if (data.isAllDone()) {
+                System.out.println("PPT生成已完成!");
+                System.out.println("PPT下载链接: " + data.pptUrl());
+            }
+            // 检查是否失败
+            else if (data.isFailed()) {
+                System.out.println("PPT生成失败!");
+                System.out.println("错误信息: " + data.errMsg());
+            }
+            // 正在进行中
+            else {
+                System.out.println("PPT生成中,请稍后再查询...");
+            }
+        }
+    }
+
+    @Test // 轮询检查PPT生成进度直到完成
+    @Disabled
+    public void testPollCheckProgress() throws InterruptedException {
+        // 准备参数 - 使用之前创建PPT时返回的sid
+        String sid = "fa36e926f2ed434987fcb4c1f0776ffb"; // 替换为实际的sid
+
+        // 最大轮询次数
+        int maxPolls = 20;
+        // 轮询间隔(毫秒)- 讯飞API限流为3秒一次
+        long pollInterval = 3500;
+
+        for (int i = 0; i < maxPolls; i++) {
+            System.out.println("第" + (i + 1) + "次查询进度...");
+
+            // 调用方法
+            XunfeiPptApi.ProgressResponse response = xunfeiPptApi.checkProgress(sid);
+
+            // 安全地访问响应数据
+            if (response != null && response.data() != null) {
+                XunfeiPptApi.ProgressResponseData data = response.data();
+
+                // 打印进度信息
+                System.out.println("PPT构建状态: " + data.pptStatus());
+                if (data.totalPages() != null && data.donePages() != null) {
+                    System.out.println("完成进度: " + data.donePages() + "/" + data.totalPages()
+                            + " (" + data.getProgressPercent() + "%)");
+                }
+
+                // 检查是否完成
+                if (data.isAllDone()) {
+                    System.out.println("PPT生成已完成!");
+                    System.out.println("PPT下载链接: " + data.pptUrl());
+                    break;
+                }
+                // 检查是否失败
+                else if (data.isFailed()) {
+                    System.out.println("PPT生成失败!");
+                    System.out.println("错误信息: " + data.errMsg());
+                    break;
+                }
+                // 正在进行中,继续轮询
+                else {
+                    System.out.println("PPT生成中,等待" + (pollInterval / 1000) + "秒后继续查询...");
+                    Thread.sleep(pollInterval);
+                }
+            } else {
+                System.out.println("查询失败,等待" + (pollInterval / 1000) + "秒后重试...");
+                Thread.sleep(pollInterval);
+            }
+        }
+    }
+
+    @Test // 直接创建PPT(通过文本)
+    @Disabled
+    public void testCreatePptByText() {
+        // 准备参数
+        String query = "合肥天气趋势分析,包括近5年的气温变化、降水量变化、极端天气事件,以及对城市生活的影响";
+
+        // 调用方法
+        XunfeiPptApi.CreateResponse response = xunfeiPptApi.create(query);
+        // 打印结果
+        System.out.println("直接创建PPT响应:" + JsonUtils.toJsonString(response));
+
+        // 保存sid用于后续进度查询
+        if (response != null && response.data() != null) {
+            System.out.println("sid: " + response.data().sid());
+            if (response.data().coverImgSrc() != null) {
+                System.out.println("封面图片: " + response.data().coverImgSrc());
+            }
+            System.out.println("标题: " + response.data().title());
+            System.out.println("副标题: " + response.data().subTitle());
+        }
+    }
+
+    @Test // 直接创建PPT(通过文件)
+    @Disabled
+    public void testCreatePptByFile() throws IOException {
+        // 准备参数
+        File file = new File("src/test/resources/test.txt"); // 请确保此文件存在
+        MultipartFile multipartFile = convertFileToMultipartFile(file);
+
+        // 调用方法
+        XunfeiPptApi.CreateResponse response = xunfeiPptApi.create(multipartFile, file.getName());
+        // 打印结果
+        System.out.println("通过文件创建PPT响应:" + JsonUtils.toJsonString(response));
+
+        // 保存sid用于后续进度查询
+        if (response != null && response.data() != null) {
+            System.out.println("sid: " + response.data().sid());
+            if (response.data().coverImgSrc() != null) {
+                System.out.println("封面图片: " + response.data().coverImgSrc());
+            }
+            System.out.println("标题: " + response.data().title());
+            System.out.println("副标题: " + response.data().subTitle());
+        }
+    }
+
+    @Test // 直接创建PPT(完整参数)
+    @Disabled
+    public void testCreatePptWithFullParams() throws IOException {
+        // 准备参数
+        String query = "合肥天气趋势分析,包括近5年的气温变化、降水量变化、极端天气事件,以及对城市生活的影响";
+
+        // 创建请求对象
+        XunfeiPptApi.CreatePptRequest request = XunfeiPptApi.CreatePptRequest.builder()
+                .query(query)
+                .language("cn")
+                .isCardNote(true)
+                .search(true)
+                .isFigure(true)
+                .aiImage("advanced")
+                .author("测试用户")
+                .build();
+
+        // 调用方法
+        XunfeiPptApi.CreateResponse response = xunfeiPptApi.create(request);
+        // 打印结果
+        System.out.println("使用完整参数创建PPT响应:" + JsonUtils.toJsonString(response));
+
+        // 保存sid用于后续进度查询
+        if (response != null && response.data() != null) {
+            String sid = response.data().sid();
+            System.out.println("sid: " + sid);
+            if (response.data().coverImgSrc() != null) {
+                System.out.println("封面图片: " + response.data().coverImgSrc());
+            }
+            System.out.println("标题: " + response.data().title());
+            System.out.println("副标题: " + response.data().subTitle());
+
+            // 立即查询一次进度
+            System.out.println("立即查询进度...");
+            XunfeiPptApi.ProgressResponse progressResponse = xunfeiPptApi.checkProgress(sid);
+            if (progressResponse != null && progressResponse.data() != null) {
+                XunfeiPptApi.ProgressResponseData progressData = progressResponse.data();
+                System.out.println("PPT构建状态: " + progressData.pptStatus());
+                if (progressData.totalPages() != null && progressData.donePages() != null) {
+                    System.out.println("完成进度: " + progressData.donePages() + "/" + progressData.totalPages()
+                            + " (" + progressData.getProgressPercent() + "%)");
+                }
+            }
+        }
+    }
+
+    /**
+     * 将File转换为MultipartFile
+     */
+    private MultipartFile convertFileToMultipartFile(File file) throws IOException {
+        FileInputStream input = new FileInputStream(file);
+        return new MockMultipartFile(
+                "file",
+                file.getName(),
+                "text/plain",
+                input.readAllBytes()
+        );
+    }
+
+}