|
2 | 2 | * Copyright (c) 2018-present, easy-4-java (https://github.com/easy-4-java). |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | 5 | */ |
16 | 6 | package io.github.easy4j.comfy; |
17 | 7 |
|
18 | 8 | import java.util.Objects; |
19 | 9 |
|
20 | | -import org.slf4j.Logger; |
21 | | -import org.slf4j.LoggerFactory; |
22 | | - |
23 | 10 | import io.github.easy4j.comfy.cli.ComfyCli; |
24 | 11 | import io.github.easy4j.comfy.cli.ComfyCliExecutor; |
25 | 12 | import io.github.easy4j.comfy.cli.ComfyCliResult; |
| 13 | +import io.github.easy4j.comfy.model.ComfyCliEnvelope; |
| 14 | +import tools.jackson.databind.DeserializationFeature; |
26 | 15 | import tools.jackson.databind.JsonNode; |
| 16 | +import tools.jackson.databind.ObjectMapper; |
27 | 17 | import tools.jackson.databind.json.JsonMapper; |
28 | 18 |
|
29 | 19 | /** |
30 | | - * High-level Java facade that wraps every local {@code comfy} CLI invocation |
31 | | - * behind ergonomic, strongly-typed methods. |
| 20 | + * High-level Java facade for the local {@code comfy} CLI route. |
32 | 21 | * |
33 | | - * <p>This class is the recommended entry point for the CLI route. It owns a |
34 | | - * single {@link ComfyClientConfig} and a single {@link ComfyCli}, forwarding |
35 | | - * the configured defaults to every call. For the MCP route (spawn |
36 | | - * {@code comfy-mcp} and speak JSON-RPC over stdio) use |
37 | | - * {@code io.github.easy4j.comfy.mcp.ComfyMcpClient}.</p> |
38 | | - * |
39 | | - * @author <a href="https://github.com/loong10k">Loong Wan</a> |
40 | | - * @since 1.0.0 |
41 | | - * @see ComfyClientConfig |
42 | | - * @see ComfyCli |
| 22 | + * <p>The lower-level {@link ComfyCli} mirrors the CLI command tree; this class |
| 23 | + * adds parsed JSON helpers while retaining access to the raw mapper.</p> |
43 | 24 | */ |
44 | 25 | public class ComfyClient implements AutoCloseable { |
45 | 26 |
|
46 | | - private static final Logger log = LoggerFactory.getLogger(ComfyClient.class); |
47 | | - private static final JsonMapper MAPPER = new JsonMapper(); |
| 27 | + private static final ObjectMapper MAPPER = |
| 28 | + JsonMapper.builder().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).build(); |
48 | 29 |
|
49 | 30 | private final ComfyClientConfig config; |
50 | 31 | private final ComfyCli cli; |
51 | 32 |
|
52 | | - /** |
53 | | - * Creates a new client backed by the given configuration. A default |
54 | | - * {@link ComfyCli} and {@link ComfyCliExecutor} are constructed |
55 | | - * automatically. |
56 | | - * |
57 | | - * @param config runtime configuration; must not be {@code null}. |
58 | | - * @throws NullPointerException if {@code config} is {@code null}. |
59 | | - */ |
60 | 33 | public ComfyClient(ComfyClientConfig config) { |
61 | 34 | this.config = Objects.requireNonNull(config, "config"); |
62 | 35 | this.config.validate(); |
63 | 36 | this.cli = new ComfyCli(this.config, new ComfyCliExecutor(this.config)); |
64 | 37 | } |
65 | 38 |
|
66 | | - /** |
67 | | - * Creates a new client that delegates to the supplied {@link ComfyCli}. |
68 | | - * |
69 | | - * <p>This constructor exists primarily for testing — it lets a |
70 | | - * caller substitute a {@link ComfyCli} backed by a mocked executor while |
71 | | - * still using the default behaviour of the surrounding facade.</p> |
72 | | - * |
73 | | - * @param config runtime configuration; must not be {@code null}. |
74 | | - * @param cli the CLI facade to delegate to; must not be {@code null}. |
75 | | - * @throws NullPointerException if either argument is {@code null}. |
76 | | - */ |
77 | 39 | public ComfyClient(ComfyClientConfig config, ComfyCli cli) { |
78 | 40 | this.config = Objects.requireNonNull(config, "config"); |
| 41 | + this.config.validate(); |
79 | 42 | this.cli = Objects.requireNonNull(cli, "cli"); |
80 | 43 | } |
81 | 44 |
|
82 | | - /** |
83 | | - * Runs {@code comfy --version}. |
84 | | - * |
85 | | - * @return the raw CLI invocation result; never {@code null}. |
86 | | - */ |
87 | | - public ComfyCliResult version() { |
88 | | - return cli.version(); |
89 | | - } |
| 45 | + public ComfyCliResult version() { return cli.version(); } |
| 46 | + public ComfyCliResult help() { return cli.help(); } |
| 47 | + public boolean isAvailable() { return cli.executor().probe(); } |
| 48 | + public ComfyCliResult cloudLogin() { return cli.cloudLogin(); } |
| 49 | + public ComfyCliResult setup() { return cli.setupYes(); } |
| 50 | + public ComfyCliResult skillsInstall() { return cli.skillsInstall(); } |
90 | 51 |
|
91 | 52 | /** |
92 | | - * Runs {@code comfy --help}. |
93 | | - * |
94 | | - * @return the raw CLI invocation result; never {@code null}. |
95 | | - */ |
96 | | - public ComfyCliResult help() { |
97 | | - return cli.help(); |
98 | | - } |
99 | | - |
100 | | - /** |
101 | | - * Probes CLI availability with {@code comfy --version} and the configured |
102 | | - * probe timeout. |
103 | | - * |
104 | | - * @return {@code true} when the local CLI is reachable. |
105 | | - */ |
106 | | - public boolean isAvailable() { |
107 | | - return cli.executor().probe(); |
108 | | - } |
109 | | - |
110 | | - /** |
111 | | - * Sends a generation request ({@code comfy generate <model>}) with |
112 | | - * {@code --json} so the standard output can be parsed as JSON. |
113 | | - * |
114 | | - * @param model the generation model alias. |
115 | | - * @param options the generation options; must not be {@code null}. |
116 | | - * @return the parsed JSON root of the {@code --json} output; never |
117 | | - * {@code null}. |
118 | | - * @throws ComfyException when the invocation fails or prints non-JSON. |
| 53 | + * Runs partner generation with command-level JSON output without mutating |
| 54 | + * the caller's reusable options object. |
119 | 55 | */ |
120 | 56 | public JsonNode generateJson(String model, ComfyCli.GenerateOptions options) { |
121 | | - ComfyCli.GenerateOptions jsonOptions = options.json(true); |
122 | | - ComfyCliResult result = cli.generate(model, jsonOptions); |
123 | | - if (!result.isSuccess()) { |
124 | | - throw new ComfyException("comfy generate failed: exit=" + result.getExitCode() |
125 | | - + " stderr=" + result.getStderr()); |
126 | | - } |
| 57 | + Objects.requireNonNull(options, "options"); |
| 58 | + ComfyCliResult result = cli.generate(model, options.copy().json(true)); |
| 59 | + requireSuccess(result, "comfy generate"); |
127 | 60 | try { |
128 | 61 | return MAPPER.readTree(result.getStdout()); |
129 | 62 | } catch (Exception e) { |
130 | 63 | throw new ComfyException("comfy generate --json printed non-JSON output", e); |
131 | 64 | } |
132 | 65 | } |
133 | 66 |
|
134 | | - /** |
135 | | - * Runs {@code comfy cloud login} (browser OAuth). |
136 | | - * |
137 | | - * @return the raw CLI invocation result; never {@code null}. |
138 | | - */ |
139 | | - public ComfyCliResult cloudLogin() { |
140 | | - return cli.cloudLogin(); |
| 67 | + /** Executes any CLI command using the global uniform {@code --json} envelope. */ |
| 68 | + public ComfyCliEnvelope executeJson(String... args) { |
| 69 | + ComfyCliResult result = cli.executeJson(args); |
| 70 | + requireSuccess(result, "comfy --json"); |
| 71 | + if (result.isTruncated()) { |
| 72 | + throw new ComfyException("comfy --json output exceeded maxOutputBytes=" |
| 73 | + + config.getMaxOutputBytes()); |
| 74 | + } |
| 75 | + try { |
| 76 | + return MAPPER.readValue(result.getStdout(), ComfyCliEnvelope.class); |
| 77 | + } catch (Exception e) { |
| 78 | + throw new ComfyException("comfy --json printed an invalid envelope", e); |
| 79 | + } |
141 | 80 | } |
142 | 81 |
|
143 | | - /** |
144 | | - * Runs {@code comfy setup -y} (non-interactive setup). |
145 | | - * |
146 | | - * @return the raw CLI invocation result; never {@code null}. |
147 | | - */ |
148 | | - public ComfyCliResult setup() { |
149 | | - return cli.setupYes(); |
150 | | - } |
| 82 | + public ComfyCliEnvelope environment() { return executeJson("env"); } |
| 83 | + public ComfyCliEnvelope whichJson() { return executeJson("which"); } |
| 84 | + public ComfyCliEnvelope discover() { return executeJson("discover"); } |
151 | 85 |
|
152 | | - /** |
153 | | - * Runs {@code comfy skills install}. |
154 | | - * |
155 | | - * @return the raw CLI invocation result; never {@code null}. |
156 | | - */ |
157 | | - public ComfyCliResult skillsInstall() { |
158 | | - return cli.skillsInstall(); |
159 | | - } |
| 86 | + public ComfyCli cli() { return cli; } |
| 87 | + public ComfyClientConfig getConfig() { return config; } |
160 | 88 |
|
161 | | - /** |
162 | | - * Returns the underlying {@link ComfyCli} for advanced callers. |
163 | | - * |
164 | | - * @return the CLI facade backing this client; never {@code null}. |
165 | | - */ |
166 | | - public ComfyCli cli() { |
167 | | - return cli; |
168 | | - } |
169 | | - |
170 | | - /** |
171 | | - * Returns the runtime configuration used by this client. |
172 | | - * |
173 | | - * @return the configuration; never {@code null}. |
174 | | - */ |
175 | | - public ComfyClientConfig getConfig() { |
176 | | - return config; |
| 89 | + private static void requireSuccess(ComfyCliResult result, String operation) { |
| 90 | + if (!result.isSuccess()) { |
| 91 | + throw new ComfyException(operation + " failed: exit=" + result.getExitCode() |
| 92 | + + " stderr=" + result.getStderr()); |
| 93 | + } |
177 | 94 | } |
178 | 95 |
|
179 | | - /** |
180 | | - * Closes this client. The default implementation is a no-op because the |
181 | | - * underlying {@link ComfyCliExecutor} does not hold any long-lived |
182 | | - * resources. |
183 | | - */ |
184 | 96 | @Override |
185 | 97 | public void close() { |
| 98 | + // CLI route owns no persistent subprocess or executor. |
186 | 99 | } |
187 | 100 | } |
0 commit comments