autodev-cli 1.4.150 → 1.4.152
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/media/gimp/gimp_mcp_client.py +204 -0
- package/media/gimp/gimp_mcp_server.py +310 -211
- package/out/commands/mcp.js +36 -30
- package/out/commands/mcp.js.map +1 -1
- package/out/configManager.js +54 -0
- package/out/configManager.js.map +1 -1
- package/out/core/gimpClientSource.d.ts +4 -0
- package/out/core/gimpClientSource.js +221 -0
- package/out/core/gimpClientSource.js.map +1 -0
- package/out/core/gimpPluginInstall.d.ts +45 -6
- package/out/core/gimpPluginInstall.js +184 -63
- package/out/core/gimpPluginInstall.js.map +1 -1
- package/out/core/gimpPluginSource.d.ts +4 -0
- package/out/core/gimpPluginSource.js +383 -0
- package/out/core/gimpPluginSource.js.map +1 -0
- package/out/core/mcpCatalog.d.ts +25 -17
- package/out/core/mcpCatalog.js +68 -36
- package/out/core/mcpCatalog.js.map +1 -1
- package/out/core/mcpCatalogMigrate.d.ts +9 -1
- package/out/core/mcpCatalogMigrate.js +34 -22
- package/out/core/mcpCatalogMigrate.js.map +1 -1
- package/package.json +3 -1
|
@@ -1,267 +1,366 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
#
|
|
4
|
-
# gimp_mcp_server.py — AutoDev auto-start GIMP 3.x MCP bridge
|
|
5
|
-
#
|
|
6
|
-
# Derived from libreearth/gimp-mcp @49c46dd (GPL-3.0); modified by AutoDev to
|
|
7
|
-
# auto-start as a GIMP 3.x PERSISTENT extension. The upstream plugin registered a
|
|
8
|
-
# manual "<Image>/Filters/Development/ → Start MCP Server" ImageProcedure that the
|
|
9
|
-
# user had to click; this variant registers a PERSISTENT extension so GIMP starts
|
|
10
|
-
# the socket server automatically on launch — no menu click, no open image needed.
|
|
11
|
-
#
|
|
12
|
-
# The socket / command-marshalling logic (start_server, handle_client,
|
|
13
|
-
# execute_command, GLib.idle_add onto GIMP's main thread) is kept from upstream.
|
|
14
|
-
# ONLY the registration + run() model changed to PERSISTENT + defensive ordering.
|
|
15
|
-
#
|
|
16
|
-
# ---------------------------------------------------------------------------
|
|
17
|
-
# GIMP 3.x plugin layout (REQUIRED): this file must live at
|
|
18
|
-
# <gimp-config>/<ver>/plug-ins/gimp_mcp_server/gimp_mcp_server.py
|
|
19
|
-
# i.e. inside a sub-folder whose name == this file's basename. A loose .py is
|
|
20
|
-
# IGNORED by GIMP 3.x. On Linux/macOS it must ALSO be executable (chmod 0o755).
|
|
21
2
|
# ---------------------------------------------------------------------------
|
|
3
|
+
# gimp_mcp_server.py — vendored, USER-VERIFIED GIMP 3.2 MCP bridge (plug-in).
|
|
22
4
|
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
# Foundation, either version 3 of the License, or (at your option) any later
|
|
29
|
-
# version.
|
|
5
|
+
# Derived from libreearth/gimp-mcp (GPL-3.0; see media/gimp/LICENSE) and fixed by
|
|
6
|
+
# the user / AutoDev for GIMP 3.2 (Store / MSIX). Verified working on real GIMP
|
|
7
|
+
# 3.2.44: a menu-triggered PLUGIN (Tools -> MCP / Filters -> Development -> Start
|
|
8
|
+
# MCP Server) that listens on 127.0.0.1:9876 and drives GIMP. Keep the "GIMP MCP
|
|
9
|
+
# Server" window open — GLib.MainLoop() holds the out-of-process plug-in alive.
|
|
30
10
|
#
|
|
31
|
-
# This
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
# You should have received a copy of the GNU General Public License along with
|
|
36
|
-
# this program. If not, see <https://www.gnu.org/licenses/>. A copy is vendored
|
|
37
|
-
# next to this file as LICENSE.
|
|
11
|
+
# This is the user's verified file; the body below is kept byte-for-byte. The
|
|
12
|
+
# lines in THIS block are a provenance comment only — do not edit the code below.
|
|
13
|
+
# ---------------------------------------------------------------------------
|
|
38
14
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
import json
|
|
42
|
-
import socket
|
|
43
|
-
import threading
|
|
44
|
-
import traceback
|
|
45
|
-
import datetime
|
|
15
|
+
# GIMP MCP Server Plugin (GIMP 3.2)
|
|
16
|
+
# Listens on localhost:9876 so the libreearth gimp-mcp client can drive GIMP.
|
|
46
17
|
|
|
47
18
|
import gi
|
|
48
19
|
gi.require_version('Gimp', '3.0')
|
|
49
20
|
from gi.repository import Gimp
|
|
50
21
|
from gi.repository import GLib
|
|
22
|
+
from gi.repository import Gio
|
|
23
|
+
from gi.repository import GObject
|
|
24
|
+
|
|
25
|
+
import io
|
|
26
|
+
import json
|
|
27
|
+
import os
|
|
28
|
+
import socket
|
|
29
|
+
import sys
|
|
30
|
+
import threading
|
|
31
|
+
import traceback
|
|
51
32
|
|
|
33
|
+
HOST = '127.0.0.1'
|
|
34
|
+
PORT = 9876
|
|
35
|
+
# Store GIMP redirects GLib config dir into the MSIX LocalCache. Also write
|
|
36
|
+
# to the real roaming profile so the log is findable.
|
|
37
|
+
_LOG_PATHS = [
|
|
38
|
+
os.path.join(GLib.get_user_config_dir(), 'GIMP', '3.2', 'gimp_mcp_server.log'),
|
|
39
|
+
os.path.join(os.environ.get('APPDATA', ''), 'GIMP', '3.2', 'gimp_mcp_server.log'),
|
|
40
|
+
]
|
|
52
41
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
42
|
+
_server_lock = threading.Lock()
|
|
43
|
+
_server_started = False
|
|
44
|
+
_exec_globals = None
|
|
45
|
+
_main_loop = None
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _log(msg):
|
|
49
|
+
line = msg.rstrip() + '\n'
|
|
50
|
+
for path in _LOG_PATHS:
|
|
51
|
+
if not path:
|
|
52
|
+
continue
|
|
64
53
|
try:
|
|
65
|
-
|
|
54
|
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
55
|
+
with open(path, 'a', encoding='utf-8') as fh:
|
|
56
|
+
fh.write(line)
|
|
66
57
|
except Exception:
|
|
67
|
-
|
|
58
|
+
pass
|
|
59
|
+
print(msg, flush=True)
|
|
68
60
|
|
|
69
61
|
|
|
70
|
-
|
|
62
|
+
def N_(message):
|
|
63
|
+
return message
|
|
71
64
|
|
|
72
65
|
|
|
73
|
-
def
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
66
|
+
def _(message):
|
|
67
|
+
return GLib.dgettext(None, message)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _serialize(obj):
|
|
71
|
+
if obj is None or isinstance(obj, (str, int, float, bool)):
|
|
72
|
+
return obj
|
|
73
|
+
if isinstance(obj, (list, tuple)):
|
|
74
|
+
return [_serialize(item) for item in obj]
|
|
75
|
+
if isinstance(obj, dict):
|
|
76
|
+
return {str(k): _serialize(v) for k, v in obj.items()}
|
|
77
|
+
data = {'type': obj.__class__.__name__}
|
|
78
|
+
if hasattr(obj, 'get_id'):
|
|
79
|
+
try:
|
|
80
|
+
data['id'] = int(obj.get_id())
|
|
81
|
+
except Exception:
|
|
82
|
+
pass
|
|
83
|
+
elif hasattr(obj, 'ID'):
|
|
84
|
+
data['id'] = obj.ID
|
|
85
|
+
if hasattr(obj, 'get_name'):
|
|
86
|
+
try:
|
|
87
|
+
data['name'] = obj.get_name()
|
|
88
|
+
except Exception:
|
|
89
|
+
pass
|
|
90
|
+
if 'id' not in data and len(data) == 1:
|
|
91
|
+
return str(obj)
|
|
92
|
+
return data
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _resolve_api(api_path):
|
|
96
|
+
parts = [p for p in str(api_path).split('.') if p]
|
|
97
|
+
if parts and parts[0] == 'Gimp':
|
|
98
|
+
parts = parts[1:]
|
|
99
|
+
elif len(parts) >= 3 and parts[0] == 'gi' and parts[1] == 'repository' and parts[2] == 'Gimp':
|
|
100
|
+
parts = parts[3:]
|
|
101
|
+
|
|
102
|
+
aliases = {
|
|
103
|
+
'list_images': 'get_images',
|
|
104
|
+
'list_fonts': 'get_fonts',
|
|
105
|
+
'fonts_get_list': 'get_fonts',
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if not parts:
|
|
109
|
+
return Gimp
|
|
110
|
+
|
|
111
|
+
obj = Gimp
|
|
112
|
+
for part in parts:
|
|
113
|
+
part = aliases.get(part, part)
|
|
114
|
+
obj = getattr(obj, part)
|
|
115
|
+
return obj
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _exec_namespace():
|
|
119
|
+
global _exec_globals
|
|
120
|
+
if _exec_globals is None:
|
|
121
|
+
ns = {
|
|
122
|
+
'__builtins__': __builtins__,
|
|
123
|
+
'__name__': '__main__',
|
|
124
|
+
'Gimp': Gimp,
|
|
125
|
+
'GLib': GLib,
|
|
126
|
+
'Gio': Gio,
|
|
127
|
+
'GObject': GObject,
|
|
128
|
+
'sys': sys,
|
|
129
|
+
'os': os,
|
|
130
|
+
'json': json,
|
|
131
|
+
}
|
|
132
|
+
try:
|
|
133
|
+
gi.require_version('Gegl', '0.4')
|
|
134
|
+
from gi.repository import Gegl
|
|
135
|
+
ns['Gegl'] = Gegl
|
|
136
|
+
except Exception as exc:
|
|
137
|
+
_log(f'Gegl import failed: {exc}')
|
|
138
|
+
try:
|
|
139
|
+
gi.require_version('Babl', '0.1')
|
|
140
|
+
from gi.repository import Babl
|
|
141
|
+
ns['Babl'] = Babl
|
|
142
|
+
except Exception:
|
|
143
|
+
pass
|
|
144
|
+
_exec_globals = ns
|
|
145
|
+
return _exec_globals
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _extract_code(args, kwargs):
|
|
149
|
+
if kwargs.get('code'):
|
|
150
|
+
return str(kwargs['code'])
|
|
151
|
+
if not args:
|
|
152
|
+
return ''
|
|
153
|
+
if len(args) >= 2 and args[0] in ('exec', 'eval', 'pyGObject-console'):
|
|
154
|
+
payload = args[1]
|
|
155
|
+
if isinstance(payload, (list, tuple)):
|
|
156
|
+
return '\n'.join(str(item) for item in payload)
|
|
157
|
+
return str(payload)
|
|
158
|
+
if isinstance(args[0], (list, tuple)):
|
|
159
|
+
return '\n'.join(str(item) for item in args[0])
|
|
160
|
+
return str(args[0])
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _run_python(code):
|
|
164
|
+
ns = _exec_namespace()
|
|
165
|
+
stdout = io.StringIO()
|
|
166
|
+
old_stdout = sys.stdout
|
|
167
|
+
sys.stdout = stdout
|
|
81
168
|
try:
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
169
|
+
exec(code, ns)
|
|
170
|
+
finally:
|
|
171
|
+
sys.stdout = old_stdout
|
|
172
|
+
printed = stdout.getvalue()
|
|
173
|
+
result = ns.get('_result')
|
|
174
|
+
if result is not None:
|
|
175
|
+
return {'printed': printed, 'result': _serialize(result)}
|
|
176
|
+
return {'printed': printed, 'result': None}
|
|
85
177
|
|
|
86
178
|
|
|
87
179
|
class MCPPlugin(Gimp.PlugIn):
|
|
88
|
-
|
|
180
|
+
def do_set_i18n(self, name):
|
|
181
|
+
return False
|
|
182
|
+
|
|
89
183
|
def do_query_procedures(self):
|
|
90
|
-
return [
|
|
184
|
+
return ['plug-in-mcp-server']
|
|
91
185
|
|
|
92
186
|
def do_create_procedure(self, name):
|
|
93
|
-
# PERSISTENT + no args + no menu = GIMP auto-starts this extension at
|
|
94
|
-
# launch. Adding ANY argument, a menu label, or a menu path would turn
|
|
95
|
-
# it back into a user-invoked plug-in and disable auto-start.
|
|
96
187
|
procedure = Gimp.Procedure.new(
|
|
97
|
-
self, name, Gimp.PDBProcType.
|
|
188
|
+
self, name, Gimp.PDBProcType.PLUGIN, self.run, None
|
|
98
189
|
)
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
190
|
+
procedure.set_menu_label(_('Start MCP Server'))
|
|
191
|
+
procedure.set_documentation(
|
|
192
|
+
_('Starts an MCP server to control GIMP externally'),
|
|
193
|
+
_('Listens on 127.0.0.1:9876 for the GIMP MCP client'),
|
|
194
|
+
name,
|
|
195
|
+
)
|
|
196
|
+
procedure.set_attribution('GIMP MCP', 'GIMP MCP', '2026')
|
|
197
|
+
procedure.set_sensitivity_mask(Gimp.ProcedureSensitivityMask.ALWAYS)
|
|
198
|
+
procedure.add_enum_argument(
|
|
199
|
+
'run-mode',
|
|
200
|
+
_('Run mode'),
|
|
201
|
+
_('The run mode'),
|
|
202
|
+
Gimp.RunMode,
|
|
203
|
+
Gimp.RunMode.INTERACTIVE,
|
|
204
|
+
GObject.ParamFlags.READWRITE,
|
|
205
|
+
)
|
|
206
|
+
procedure.add_menu_path('<Image>/Tools/MCP')
|
|
207
|
+
procedure.add_menu_path('<Image>/Filters/Development/')
|
|
112
208
|
return procedure
|
|
113
209
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
DEFENSIVE ORDER (the brick-risk part): call persistent_ready() FIRST.
|
|
119
|
-
Its ABSENCE is what hangs GIMP at startup, so it must run before
|
|
120
|
-
anything that could throw. Everything after it is wrapped so a bind
|
|
121
|
-
failure logs + degrades instead of taking GIMP core down with it.
|
|
122
|
-
"""
|
|
123
|
-
# 1) Hand control back to GIMP core IMMEDIATELY so the UI keeps loading.
|
|
124
|
-
try:
|
|
125
|
-
procedure.persistent_ready()
|
|
126
|
-
log("persistent_ready() OK — extension is live")
|
|
127
|
-
except Exception as e:
|
|
128
|
-
# A binding mismatch on this exact call is the known lock-up cause;
|
|
129
|
-
# log it (do NOT re-raise) so GIMP still finishes launching.
|
|
130
|
-
log("persistent_ready() FAILED (non-fatal): %s\n%s"
|
|
131
|
-
% (e, traceback.format_exc()))
|
|
132
|
-
|
|
133
|
-
# 2) Start the socket server on a daemon thread. A bind failure (port in
|
|
134
|
-
# use, sandbox denial) MUST NOT crash the process or block launch.
|
|
210
|
+
def run(self, procedure, config, data):
|
|
211
|
+
global _main_loop
|
|
212
|
+
started, message = self.ensure_server()
|
|
213
|
+
_log(message)
|
|
135
214
|
try:
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
#
|
|
144
|
-
#
|
|
215
|
+
Gimp.message(message)
|
|
216
|
+
except Exception:
|
|
217
|
+
pass
|
|
218
|
+
if not started:
|
|
219
|
+
return procedure.new_return_values(Gimp.PDBStatusType.EXECUTION_ERROR, GLib.Error(message))
|
|
220
|
+
|
|
221
|
+
# GIMP 3 launches Python plug-ins out of process. Returning from run()
|
|
222
|
+
# would exit that process and kill the socket thread. Stay alive and
|
|
223
|
+
# dispatch GLib.idle_add work (API calls) on this main loop.
|
|
224
|
+
# A visible window is required: otherwise "Start MCP Server" looks like
|
|
225
|
+
# it did not open anything.
|
|
145
226
|
try:
|
|
146
|
-
|
|
147
|
-
except Exception as
|
|
148
|
-
|
|
227
|
+
self._show_status_window(message)
|
|
228
|
+
except Exception as exc:
|
|
229
|
+
_log(f'Status window failed: {exc}\n{traceback.format_exc()}')
|
|
149
230
|
|
|
150
|
-
|
|
231
|
+
_main_loop = GLib.MainLoop()
|
|
232
|
+
_log('Entering GLib.MainLoop to keep MCP server process alive')
|
|
233
|
+
try:
|
|
234
|
+
_main_loop.run()
|
|
235
|
+
except Exception as exc:
|
|
236
|
+
_log(f'MainLoop exited: {exc}')
|
|
151
237
|
return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
|
|
152
238
|
|
|
153
|
-
|
|
239
|
+
def _show_status_window(self, message):
|
|
240
|
+
gi.require_version('Gtk', '3.0')
|
|
241
|
+
gi.require_version('GimpUi', '3.0')
|
|
242
|
+
from gi.repository import Gtk
|
|
243
|
+
from gi.repository import GimpUi
|
|
244
|
+
|
|
245
|
+
GimpUi.init('gimp_mcp_server.py')
|
|
246
|
+
window = Gtk.Window(title='GIMP MCP Server')
|
|
247
|
+
window.set_default_size(420, 160)
|
|
248
|
+
window.set_keep_above(True)
|
|
249
|
+
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=8)
|
|
250
|
+
box.set_border_width(16)
|
|
251
|
+
label = Gtk.Label(label=message + '\n\nLeave this window open while Grok talks to GIMP.')
|
|
252
|
+
label.set_line_wrap(True)
|
|
253
|
+
box.pack_start(label, True, True, 0)
|
|
254
|
+
button = Gtk.Button(label='Stop server')
|
|
255
|
+
button.connect('clicked', lambda *_: window.destroy())
|
|
256
|
+
box.pack_start(button, False, False, 0)
|
|
257
|
+
window.add(box)
|
|
258
|
+
window.connect('destroy', self._on_status_closed)
|
|
259
|
+
window.show_all()
|
|
260
|
+
window.present()
|
|
261
|
+
_log('Status window opened')
|
|
262
|
+
|
|
263
|
+
def _on_status_closed(self, *_args):
|
|
264
|
+
global _main_loop
|
|
265
|
+
_log('Status window closed; stopping MCP server')
|
|
266
|
+
if _main_loop is not None:
|
|
267
|
+
_main_loop.quit()
|
|
268
|
+
|
|
269
|
+
def ensure_server(self):
|
|
270
|
+
global _server_started
|
|
271
|
+
with _server_lock:
|
|
272
|
+
if _server_started:
|
|
273
|
+
return True, f'MCP Server already listening on {HOST}:{PORT}'
|
|
274
|
+
try:
|
|
275
|
+
thread = threading.Thread(target=self.start_server, daemon=True)
|
|
276
|
+
thread.start()
|
|
277
|
+
_server_started = True
|
|
278
|
+
return True, f'MCP Server started on {HOST}:{PORT}'
|
|
279
|
+
except Exception as exc:
|
|
280
|
+
return False, f'Failed to start MCP Server: {exc}'
|
|
281
|
+
|
|
154
282
|
def start_server(self):
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
sock = None
|
|
283
|
+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
284
|
+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
158
285
|
try:
|
|
159
|
-
sock
|
|
160
|
-
sock.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
except Exception as e:
|
|
165
|
-
# Bind/listen failed (e.g. port already taken by Blender's MCP or a
|
|
166
|
-
# second GIMP). Log + bail out of the thread; GIMP keeps running.
|
|
167
|
-
log("bind/listen failed (non-fatal): %s\n%s" % (e, traceback.format_exc()))
|
|
168
|
-
try:
|
|
169
|
-
if sock is not None:
|
|
170
|
-
sock.close()
|
|
171
|
-
except Exception:
|
|
172
|
-
pass
|
|
286
|
+
sock.bind((HOST, PORT))
|
|
287
|
+
sock.listen(8)
|
|
288
|
+
_log(f'Server listening on {HOST}:{PORT}')
|
|
289
|
+
except Exception as exc:
|
|
290
|
+
_log(f'Bind/listen failed: {exc}')
|
|
173
291
|
return
|
|
174
292
|
|
|
175
293
|
while True:
|
|
176
294
|
try:
|
|
177
295
|
client, addr = sock.accept()
|
|
178
|
-
|
|
179
|
-
threading.Thread(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
except Exception as e:
|
|
183
|
-
log("accept error: %s" % e)
|
|
296
|
+
_log(f'Connected to {addr}')
|
|
297
|
+
threading.Thread(target=self.handle_client, args=(client,), daemon=True).start()
|
|
298
|
+
except Exception as exc:
|
|
299
|
+
_log(f'Server accept error: {exc}')
|
|
184
300
|
break
|
|
185
|
-
|
|
186
|
-
sock.close()
|
|
187
|
-
except Exception:
|
|
188
|
-
pass
|
|
301
|
+
sock.close()
|
|
189
302
|
|
|
190
303
|
def handle_client(self, client):
|
|
191
304
|
buffer = b''
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
305
|
+
try:
|
|
306
|
+
client.settimeout(120)
|
|
307
|
+
while True:
|
|
308
|
+
chunk = client.recv(65536)
|
|
309
|
+
if not chunk:
|
|
196
310
|
break
|
|
197
|
-
buffer +=
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
311
|
+
buffer += chunk
|
|
312
|
+
while buffer:
|
|
313
|
+
try:
|
|
314
|
+
command = json.loads(buffer.decode('utf-8'))
|
|
315
|
+
buffer = b''
|
|
316
|
+
except json.JSONDecodeError:
|
|
317
|
+
break
|
|
202
318
|
GLib.idle_add(self.execute_command, command, client)
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
client.close()
|
|
211
|
-
except Exception:
|
|
212
|
-
pass
|
|
319
|
+
except Exception as exc:
|
|
320
|
+
_log(f'Client error: {exc}')
|
|
321
|
+
finally:
|
|
322
|
+
try:
|
|
323
|
+
client.close()
|
|
324
|
+
except Exception:
|
|
325
|
+
pass
|
|
213
326
|
|
|
214
327
|
def execute_command(self, command, client):
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
response = {}
|
|
328
|
+
cmd_type = command.get('type')
|
|
329
|
+
params = command.get('params', {}) or {}
|
|
218
330
|
try:
|
|
219
|
-
cmd_type
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if hasattr(result, "ID"):
|
|
234
|
-
result = {"id": result.ID}
|
|
235
|
-
elif isinstance(result, list):
|
|
236
|
-
result = [self.serialize_gimp_object(item) for item in result]
|
|
331
|
+
if cmd_type == 'ping':
|
|
332
|
+
response = {'status': 'success', 'result': 'pong'}
|
|
333
|
+
elif cmd_type == 'exec':
|
|
334
|
+
code = _extract_code(params.get('args', []), params.get('kwargs', {}))
|
|
335
|
+
if not code:
|
|
336
|
+
code = str(params.get('code', ''))
|
|
337
|
+
response = {'status': 'success', 'result': _run_python(code)}
|
|
338
|
+
elif cmd_type == 'call_api':
|
|
339
|
+
api_path = params.get('api_path', '')
|
|
340
|
+
args = params.get('args', []) or []
|
|
341
|
+
kwargs = params.get('kwargs', {}) or {}
|
|
342
|
+
if api_path in ('exec', 'eval', '__exec__'):
|
|
343
|
+
response = {'status': 'success', 'result': _run_python(_extract_code(args, kwargs))}
|
|
237
344
|
else:
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
345
|
+
method = _resolve_api(api_path)
|
|
346
|
+
result = method(*args, **kwargs)
|
|
347
|
+
response = {'status': 'success', 'result': _serialize(result)}
|
|
241
348
|
else:
|
|
242
|
-
response = {
|
|
243
|
-
except Exception as
|
|
244
|
-
response = {
|
|
245
|
-
|
|
349
|
+
response = {'status': 'error', 'message': f'Unknown command: {cmd_type}'}
|
|
350
|
+
except Exception as exc:
|
|
351
|
+
response = {
|
|
352
|
+
'status': 'error',
|
|
353
|
+
'message': str(exc),
|
|
354
|
+
'traceback': traceback.format_exc(),
|
|
355
|
+
}
|
|
356
|
+
_log(f'Command error: {response["message"]}\n{response["traceback"]}')
|
|
246
357
|
|
|
247
358
|
try:
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
def serialize_gimp_object(self, obj):
|
|
254
|
-
try:
|
|
255
|
-
if hasattr(obj, "ID"):
|
|
256
|
-
return {"id": obj.ID, "type": obj.__class__.__name__}
|
|
257
|
-
except Exception:
|
|
258
|
-
pass
|
|
259
|
-
return str(obj)
|
|
359
|
+
payload = json.dumps(response).encode('utf-8')
|
|
360
|
+
client.sendall(payload)
|
|
361
|
+
except Exception as exc:
|
|
362
|
+
_log(f'Response send failed: {exc}')
|
|
363
|
+
return False
|
|
260
364
|
|
|
261
365
|
|
|
262
|
-
|
|
263
|
-
Gimp.main(MCPPlugin.__gtype__, sys.argv)
|
|
264
|
-
except Exception as _e:
|
|
265
|
-
# If GIMP's main dispatch itself fails, log it — never let the plug-in
|
|
266
|
-
# process take the launch down with an unhandled traceback.
|
|
267
|
-
log("Gimp.main failed: %s\n%s" % (_e, traceback.format_exc()))
|
|
366
|
+
Gimp.main(MCPPlugin.__gtype__, sys.argv)
|