|
| 1 | +package io.github.easy4j.opencli.adapter.browser.doubao; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import io.github.easy4j.opencli.core.OpenCliAdapterChannel; |
| 5 | +import io.github.easy4j.opencli.core.OpenCliArgSupport; |
| 6 | +import io.github.easy4j.opencli.core.OpenCliExecutor; |
| 7 | +import io.github.easy4j.opencli.core.OpenCliResult; |
| 8 | +import io.github.easy4j.opencli.core.OpenCliTypedResult; |
| 9 | +import io.github.easy4j.opencli.parser.OpenCliStdoutJson; |
| 10 | +import io.github.easy4j.opencli.registry.OpenCliAdapterIds; |
| 11 | +import io.github.easy4j.opencli.util.OpenCliLists; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import lombok.Builder; |
| 15 | +import lombok.Data; |
| 16 | +import lombok.RequiredArgsConstructor; |
| 17 | + |
| 18 | +/** |
| 19 | + * OpenCLI {@code doubao} 浏览器适配器 typed 客户端。 |
| 20 | + * |
| 21 | + * <p>命令面与上游 {@code cli-manifest.json} 逐项对齐;位置参数在前、 |
| 22 | + * 旗标在后;全部方法带 {@code more} 原生参数透传。</p> |
| 23 | + * |
| 24 | + * @author <a href="https://github.com/loong10k">Loong Wan</a> |
| 25 | + * @since 3.0.0 |
| 26 | + */ |
| 27 | +@RequiredArgsConstructor |
| 28 | +public final class DoubaoOpenCliClient { |
| 29 | + |
| 30 | + private final OpenCliExecutor executor; |
| 31 | + |
| 32 | + private OpenCliAdapterChannel ch() { |
| 33 | + return new OpenCliAdapterChannel(executor, OpenCliAdapterIds.DOUBAO); |
| 34 | + } |
| 35 | + /** {@code doubao ask} 选项块。 */ |
| 36 | + @Data |
| 37 | + @Builder(toBuilder = true) |
| 38 | + public static class DoubaoAskOptions { |
| 39 | + |
| 40 | + private Integer timeoutSeconds; |
| 41 | + |
| 42 | + /** 追加 {@code -f json}(全局格式旗标)。 */ |
| 43 | + private Boolean jsonOutput; |
| 44 | + |
| 45 | + public void appendTo(List<String> target) { |
| 46 | + if (timeoutSeconds != null) { |
| 47 | + OpenCliArgSupport.addOptionPair(target, "--timeout", String.valueOf(timeoutSeconds)); |
| 48 | + } |
| 49 | + if (Boolean.TRUE.equals(jsonOutput)) { |
| 50 | + target.add("-f"); |
| 51 | + target.add("json"); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + /** {@code doubao ask <text>}。 */ |
| 58 | + public OpenCliResult ask(String text, DoubaoAskOptions options, List<String> more) { |
| 59 | + List<String> args = new ArrayList<>(); |
| 60 | + args.add("ask"); |
| 61 | + args.add(text); |
| 62 | + if (options != null) { |
| 63 | + options.appendTo(args); |
| 64 | + } |
| 65 | + return ch().invoke(OpenCliArgSupport.merge(args, more)); |
| 66 | + } |
| 67 | + |
| 68 | + /** {@code doubao ask <text> -f json},解析为 typed 结果。 */ |
| 69 | + public OpenCliTypedResult<JsonNode> askTyped(String text, DoubaoAskOptions options, List<String> more) { |
| 70 | + DoubaoAskOptions withJson = options; |
| 71 | + if (withJson == null) { |
| 72 | + withJson = DoubaoAskOptions.builder().jsonOutput(true).build(); |
| 73 | + } else if (!Boolean.TRUE.equals(withJson.getJsonOutput())) { |
| 74 | + withJson = withJson.toBuilder().jsonOutput(true).build(); |
| 75 | + } |
| 76 | + return OpenCliStdoutJson.typed(ask(text, withJson, more)); |
| 77 | + } |
| 78 | + |
| 79 | + /** {@code doubao send <text>}(fire-and-forget)。 */ |
| 80 | + public OpenCliResult send(String text, List<String> more) { |
| 81 | + List<String> args = new ArrayList<>(); |
| 82 | + args.add("send"); |
| 83 | + args.add(text); |
| 84 | + return ch().invoke(OpenCliArgSupport.merge(args, more)); |
| 85 | + } |
| 86 | + |
| 87 | + /** {@code doubao read}。 */ |
| 88 | + public OpenCliResult read(List<String> more) { |
| 89 | + return ch().invoke(OpenCliArgSupport.merge(OpenCliLists.of("read"), more)); |
| 90 | + } |
| 91 | + |
| 92 | + /** {@code doubao detail <id>}。 */ |
| 93 | + public OpenCliResult detail(String conversationId, List<String> more) { |
| 94 | + List<String> args = new ArrayList<>(); |
| 95 | + args.add("detail"); |
| 96 | + args.add(conversationId); |
| 97 | + return ch().invoke(OpenCliArgSupport.merge(args, more)); |
| 98 | + } |
| 99 | + |
| 100 | + /** {@code doubao history [--limit N]}。 */ |
| 101 | + public OpenCliResult history(String limit, List<String> more) { |
| 102 | + List<String> args = new ArrayList<>(); |
| 103 | + args.add("history"); |
| 104 | + if (limit != null) { |
| 105 | + OpenCliArgSupport.addOptionPair(args, "--limit", limit); |
| 106 | + } |
| 107 | + return ch().invoke(OpenCliArgSupport.merge(args, more)); |
| 108 | + } |
| 109 | + |
| 110 | + /** {@code doubao meeting-summary <id>}。 */ |
| 111 | + public OpenCliResult meetingSummary(String conversationId, String chapters, List<String> more) { |
| 112 | + List<String> args = new ArrayList<>(); |
| 113 | + args.add("meeting-summary"); |
| 114 | + args.add(conversationId); |
| 115 | + if (chapters != null) { |
| 116 | + OpenCliArgSupport.addOptionPair(args, "--chapters", chapters); |
| 117 | + } |
| 118 | + return ch().invoke(OpenCliArgSupport.merge(args, more)); |
| 119 | + } |
| 120 | + |
| 121 | + /** {@code doubao meeting-transcript <id>}。 */ |
| 122 | + public OpenCliResult meetingTranscript(String conversationId, String download, List<String> more) { |
| 123 | + List<String> args = new ArrayList<>(); |
| 124 | + args.add("meeting-transcript"); |
| 125 | + args.add(conversationId); |
| 126 | + if (download != null) { |
| 127 | + OpenCliArgSupport.addOptionPair(args, "--download", download); |
| 128 | + } |
| 129 | + return ch().invoke(OpenCliArgSupport.merge(args, more)); |
| 130 | + } |
| 131 | + |
| 132 | + /** {@code doubao login [--timeout N]}。 */ |
| 133 | + public OpenCliResult login(Integer timeoutSeconds, List<String> more) { |
| 134 | + List<String> args = new ArrayList<>(); |
| 135 | + args.add("login"); |
| 136 | + if (timeoutSeconds != null) { |
| 137 | + OpenCliArgSupport.addOptionPair(args, "--timeout", String.valueOf(timeoutSeconds)); |
| 138 | + } |
| 139 | + return ch().invoke(OpenCliArgSupport.merge(args, more)); |
| 140 | + } |
| 141 | + |
| 142 | + /** {@code doubao new}。 */ |
| 143 | + public OpenCliResult newChat(List<String> more) { |
| 144 | + return ch().invoke(OpenCliArgSupport.merge(OpenCliLists.of("new"), more)); |
| 145 | + } |
| 146 | + |
| 147 | + /** {@code doubao status}。 */ |
| 148 | + public OpenCliResult status(List<String> more) { |
| 149 | + return ch().invoke(OpenCliArgSupport.merge(OpenCliLists.of("status"), more)); |
| 150 | + } |
| 151 | + |
| 152 | + /** {@code doubao whoami}。 */ |
| 153 | + public OpenCliResult whoami(List<String> more) { |
| 154 | + return ch().invoke(OpenCliArgSupport.merge(OpenCliLists.of("whoami"), more)); |
| 155 | + } |
| 156 | +} |
0 commit comments