|
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; |
| 10 | +import tools.jackson.databind.JsonNode; |
| 11 | +import tools.jackson.databind.json.JsonMapper; |
22 | 12 |
|
23 | 13 | import io.github.easy4j.comfy.cli.ComfyCli; |
24 | 14 | import io.github.easy4j.comfy.cli.ComfyCliExecutor; |
25 | 15 | import io.github.easy4j.comfy.cli.ComfyCliResult; |
26 | | -import tools.jackson.databind.JsonNode; |
27 | | -import tools.jackson.databind.json.JsonMapper; |
| 16 | +import io.github.easy4j.comfy.model.ComfyDoctorReport; |
| 17 | +import io.github.easy4j.comfy.model.ComfyJsonEnvelope; |
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 facade over the local first-party {@code comfy} CLI. |
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 |
| 22 | + * <p>Advanced and newly-added CLI commands remain reachable through |
| 23 | + * {@link #cli()} and its raw execute escape hatch. Local MCP is exposed by |
37 | 24 | * {@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 |
43 | 25 | */ |
44 | 26 | public class ComfyClient implements AutoCloseable { |
45 | 27 |
|
46 | | - private static final Logger log = LoggerFactory.getLogger(ComfyClient.class); |
47 | | - private static final JsonMapper MAPPER = new JsonMapper(); |
| 28 | + private static final JsonMapper MAPPER = JsonMapper.builder().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 | | - } |
90 | | - |
91 | | - /** |
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 | | - } |
| 45 | + public ComfyCliResult version() { return cli.version(); } |
| 46 | + public ComfyCliResult help() { return cli.help(); } |
| 47 | + public boolean isAvailable() { return cli.executor().probe(); } |
109 | 48 |
|
110 | 49 | /** |
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. |
| 50 | + * Runs generate with machine-readable output without mutating caller-owned |
| 51 | + * options. |
119 | 52 | */ |
120 | 53 | public JsonNode generateJson(String model, ComfyCli.GenerateOptions options) { |
121 | | - ComfyCli.GenerateOptions jsonOptions = options.json(true); |
| 54 | + Objects.requireNonNull(options, "options"); |
| 55 | + ComfyCli.GenerateOptions jsonOptions = new ComfyCli.GenerateOptions(options).json(true); |
122 | 56 | ComfyCliResult result = cli.generate(model, jsonOptions); |
123 | 57 | if (!result.isSuccess()) { |
124 | 58 | throw new ComfyException("comfy generate failed: exit=" + result.getExitCode() |
125 | 59 | + " stderr=" + result.getStderr()); |
126 | 60 | } |
127 | | - try { |
128 | | - return MAPPER.readTree(result.getStdout()); |
129 | | - } catch (Exception e) { |
130 | | - throw new ComfyException("comfy generate --json printed non-JSON output", e); |
131 | | - } |
| 61 | + return parseJson(result.getStdout(), "comfy generate --json"); |
132 | 62 | } |
133 | 63 |
|
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(); |
| 64 | + /** Runs the CLI's self-describing discovery contract. */ |
| 65 | + public ComfyJsonEnvelope discover() { |
| 66 | + return requireSuccessfulEnvelope(cli.discoverJson(), "comfy --json discover"); |
141 | 67 | } |
142 | 68 |
|
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(); |
| 69 | + /** Runs {@code comfy --json which}. */ |
| 70 | + public ComfyJsonEnvelope which() { |
| 71 | + return requireSuccessfulEnvelope(cli.whichJson(), "comfy --json which"); |
150 | 72 | } |
151 | 73 |
|
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(); |
| 74 | + /** Runs {@code comfy --json env}. */ |
| 75 | + public ComfyJsonEnvelope environment() { |
| 76 | + return requireSuccessfulEnvelope(cli.envJson(), "comfy --json env"); |
159 | 77 | } |
160 | 78 |
|
161 | 79 | /** |
162 | | - * Returns the underlying {@link ComfyCli} for advanced callers. |
163 | | - * |
164 | | - * @return the CLI facade backing this client; never {@code null}. |
| 80 | + * Performs a bounded readiness check using only local CLI calls. No |
| 81 | + * environment values or credentials are copied into the report. |
165 | 82 | */ |
166 | | - public ComfyCli cli() { |
167 | | - return cli; |
| 83 | + public ComfyDoctorReport doctor() { |
| 84 | + boolean available = isAvailable(); |
| 85 | + ComfyCliResult versionResult = available ? cli.version() : new ComfyCliResult(-1, "", "comfy unavailable"); |
| 86 | + boolean versionHealthy = versionResult.isSuccess() && !versionResult.getStdout().trim().isEmpty(); |
| 87 | + String version = versionHealthy ? versionResult.getStdout().trim() : null; |
| 88 | + |
| 89 | + ComfyJsonEnvelope which = null; |
| 90 | + ComfyJsonEnvelope env = null; |
| 91 | + ComfyJsonEnvelope discovery = null; |
| 92 | + if (available) { |
| 93 | + try { which = requireSuccessfulEnvelope(cli.whichJson(), "comfy --json which"); } |
| 94 | + catch (RuntimeException ignored) { } |
| 95 | + try { env = requireSuccessfulEnvelope(cli.envJson(), "comfy --json env"); } |
| 96 | + catch (RuntimeException ignored) { } |
| 97 | + try { discovery = requireSuccessfulEnvelope(cli.discoverJson(), "comfy --json discover"); } |
| 98 | + catch (RuntimeException ignored) { } |
| 99 | + } |
| 100 | + |
| 101 | + String workspace = null; |
| 102 | + boolean workspaceResolved = false; |
| 103 | + if (which != null && which.getData() != null) { |
| 104 | + JsonNode path = which.getData().path("workspace_path"); |
| 105 | + if (!path.isMissingNode() && !path.isNull() && !path.asText().trim().isEmpty()) { |
| 106 | + workspace = path.asText(); |
| 107 | + workspaceResolved = true; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + boolean environmentHealthy = env != null && env.isOk(); |
| 112 | + boolean discoveryHealthy = discovery != null && discovery.isOk(); |
| 113 | + String summary = "cli=" + available |
| 114 | + + ", version=" + versionHealthy |
| 115 | + + ", workspace=" + workspaceResolved |
| 116 | + + ", env=" + environmentHealthy |
| 117 | + + ", discover=" + discoveryHealthy; |
| 118 | + return new ComfyDoctorReport(available, versionHealthy, workspaceResolved, |
| 119 | + environmentHealthy, discoveryHealthy, version, workspace, summary); |
168 | 120 | } |
169 | 121 |
|
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; |
| 122 | + public ComfyCliResult cloudLogin() { return cli.cloudLogin(); } |
| 123 | + public ComfyCliResult setup() { return cli.setupYes(); } |
| 124 | + public ComfyCliResult skillsInstall() { return cli.skillsInstall(); } |
| 125 | + |
| 126 | + public ComfyCli cli() { return cli; } |
| 127 | + public ComfyClientConfig getConfig() { return config; } |
| 128 | + |
| 129 | + public ComfyJsonEnvelope parseEnvelope(String stdout) { |
| 130 | + JsonNode root = parseJson(stdout, "comfy --json"); |
| 131 | + if (!root.isObject()) { |
| 132 | + throw new ComfyException("comfy --json returned a non-object envelope"); |
| 133 | + } |
| 134 | + return new ComfyJsonEnvelope( |
| 135 | + root.path("ok").asBoolean(false), |
| 136 | + textOrNull(root, "command"), |
| 137 | + textOrNull(root, "version"), |
| 138 | + textOrNull(root, "where"), |
| 139 | + root.path("data"), |
| 140 | + root.path("error"), |
| 141 | + root); |
| 142 | + } |
| 143 | + |
| 144 | + private ComfyJsonEnvelope requireSuccessfulEnvelope(ComfyCliResult result, String command) { |
| 145 | + if (!result.isSuccess()) { |
| 146 | + throw new ComfyException(command + " failed: exit=" + result.getExitCode() |
| 147 | + + " stderr=" + result.getStderr()); |
| 148 | + } |
| 149 | + ComfyJsonEnvelope envelope = parseEnvelope(result.getStdout()); |
| 150 | + if (!envelope.isOk()) { |
| 151 | + throw new ComfyException(command + " reported ok=false" |
| 152 | + + (envelope.getErrorHint() == null ? "" : ": " + envelope.getErrorHint())); |
| 153 | + } |
| 154 | + return envelope; |
| 155 | + } |
| 156 | + |
| 157 | + private static JsonNode parseJson(String stdout, String command) { |
| 158 | + try { |
| 159 | + return MAPPER.readTree(stdout); |
| 160 | + } catch (Exception e) { |
| 161 | + throw new ComfyException(command + " printed non-JSON output", e); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static String textOrNull(JsonNode node, String field) { |
| 166 | + JsonNode value = node.path(field); |
| 167 | + return value.isMissingNode() || value.isNull() ? null : value.asText(); |
177 | 168 | } |
178 | 169 |
|
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 | 170 | @Override |
185 | 171 | public void close() { |
| 172 | + // CLI route owns no persistent resource; each invocation is process-scoped. |
186 | 173 | } |
187 | 174 | } |
0 commit comments