-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbridge.js
More file actions
277 lines (255 loc) · 9.19 KB
/
Copy pathbridge.js
File metadata and controls
277 lines (255 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Bridge module entry: settings, WS client, handler dispatch.
import { WsClient } from './ws-client.js';
import { LogTap } from './log-tap.js';
import { CHAT_MACRO_COMMAND } from './chat-macro.js';
import { handlePing } from './handlers/ping.js';
import { handleQueryActor } from './handlers/query-actor.js';
import { handleQueryScene } from './handlers/query-scene.js';
import { handleQueryMacro } from './handlers/query-macro.js';
import { handleQueryJournal } from './handlers/query-journal.js';
import { handleQueryUser } from './handlers/query-user.js';
import { handleLogsSubscribe, handleLogsUnsubscribe } from './handlers/logs.js';
import { handleEval } from './handlers/eval.js';
import { handleDamage } from './handlers/damage.js';
import { WORKSHOP_MACRO_COMMAND } from './ide-macro.js';
const MODULE_ID = 'foundry-bridge';
const MODULE_VERSION = '0.5.0';
const CHAT_MACRO_NAME = 'Open Claude Code Chat';
const WORKSHOP_MACRO_NAME = 'Claude Macro Workshop';
let client = null, logTap = null;
// Subscriber sets survive WS reconnects (client is rebuilt).
const replySubs = new Set(), statusSubs = new Set(), confirmSubs = new Set(), refactorSubs = new Set();
let refactorProvider = null, lastRefactor = null;
const HANDLERS = {
'ping': handlePing,
'query.actor': handleQueryActor,
'query.scene': handleQueryScene,
'query.macro': handleQueryMacro,
'query.journal': handleQueryJournal,
'query.user': handleQueryUser,
'logs.subscribe': handleLogsSubscribe,
'logs.unsubscribe': handleLogsUnsubscribe,
'eval': handleEval,
'damage': handleDamage,
'refactor.get': () => (refactorProvider ? refactorProvider() : { open: false }), // live box, not a cache
};
Hooks.once('init', () => {
game.settings.register(MODULE_ID, 'enabled', {
name: 'FOUNDRY_BRIDGE.SETTINGS.Enabled.Name',
hint: 'FOUNDRY_BRIDGE.SETTINGS.Enabled.Hint',
scope: 'client',
config: true,
type: Boolean,
default: false,
onChange: (value) => onEnabledChange(value),
});
game.settings.register(MODULE_ID, 'relayUrl', {
name: 'FOUNDRY_BRIDGE.SETTINGS.RelayUrl.Name',
hint: 'FOUNDRY_BRIDGE.SETTINGS.RelayUrl.Hint',
scope: 'client',
config: true,
type: String,
default: 'ws://127.0.0.1:7878',
});
});
Hooks.once('ready', () => {
// Tiny in-world debug API for a macro:
// game.modules.get('foundry-bridge').api.status()
const mod = game.modules.get(MODULE_ID);
if (mod) {
mod.api = {
status: () => ({
enabled: game.settings.get(MODULE_ID, 'enabled'),
connected: !!client && client.isOpen(),
relayUrl: game.settings.get(MODULE_ID, 'relayUrl'),
moduleVersion: MODULE_VERSION,
}),
restart: () => {
stopClient();
if (game.settings.get(MODULE_ID, 'enabled')) startClient();
},
isConnected: () => !!client && client.isOpen(),
sendPrompt: (text) => {
if (!client || !client.isOpen()) return null;
const promptId = `p-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
const ok = client.send({ jsonrpc: '2.0', method: 'claude.prompt', params: { promptId, text } });
return ok ? promptId : null;
},
requestStatus: () => {
if (client && client.isOpen()) client.send({ jsonrpc: '2.0', method: 'claude.hello', params: {} });
},
onReply: (cb) => { replySubs.add(cb); return () => replySubs.delete(cb); },
onStatus: (cb) => { statusSubs.add(cb); return () => statusSubs.delete(cb); },
onConfirm: (cb) => { confirmSubs.add(cb); return () => confirmSubs.delete(cb); },
sendConfirmResult: (opId, approved, reason) => {
if (client && client.isOpen()) {
client.send({ jsonrpc: '2.0', method: 'claude.confirm.result', params: { opId, approved: !!approved, reason } });
}
},
onRefactorSet: (cb) => { refactorSubs.add(cb); return () => refactorSubs.delete(cb); },
setRefactorProvider: (fn) => { refactorProvider = (typeof fn === 'function') ? fn : null; },
getLastRefactor: () => lastRefactor,
};
}
ensureMacro(CHAT_MACRO_NAME, CHAT_MACRO_COMMAND, 'icons/svg/chat.svg');
ensureMacro(WORKSHOP_MACRO_NAME, WORKSHOP_MACRO_COMMAND, 'icons/svg/book.svg');
if (game.settings.get(MODULE_ID, 'enabled')) {
startClient();
} else {
console.log('[foundry-bridge] disabled in settings; not connecting.');
}
});
// GM-only, idempotent; refresh only our autoMacro copies.
async function ensureMacro(name, command, img) {
try {
if (!game.user?.isGM) return;
const existing = game.macros.getName(name);
if (existing) {
if (existing.getFlag(MODULE_ID, 'autoMacro') && existing.command !== command) {
await existing.update({ command });
console.log(`[foundry-bridge] refreshed macro "${name}"`);
}
return;
}
await Macro.create({
name, type: 'script', scope: 'global', img, command,
flags: { [MODULE_ID]: { autoMacro: true } },
});
console.log(`[foundry-bridge] created macro "${name}"`);
} catch (err) {
console.error(`[foundry-bridge] failed to ensure macro "${name}":`, err);
}
}
function startClient() {
if (client) return;
// Install log tap before connect; cheap without subscribers.
if (!logTap) {
logTap = new LogTap();
logTap.install();
}
const url = game.settings.get(MODULE_ID, 'relayUrl');
client = new WsClient({
url,
onOpen: onConnected,
onClose: onDisconnected,
onMessage: onMessage,
});
client.start();
}
function stopClient() {
if (!client) return;
client.stop();
client = null;
if (logTap) {
logTap.uninstall();
logTap = null;
}
}
function onEnabledChange(enabled) {
if (enabled) startClient();
else stopClient();
}
function onConnected() {
const helloId = `hello-${Date.now()}`;
client.send({
jsonrpc: '2.0',
method: 'hello',
params: {
userId: game.user.id,
userName: game.user.name,
isGM: !!game.user.isGM,
worldId: game.world.id,
foundryVersion: game.version,
moduleVersion: MODULE_VERSION,
},
id: helloId,
});
ui.notifications?.info(game.i18n.localize('FOUNDRY_BRIDGE.NOTIFY.Connected'));
}
function onDisconnected(info) {
ui.notifications?.warn(game.i18n.localize('FOUNDRY_BRIDGE.NOTIFY.Disconnected'));
console.log(`[foundry-bridge] disconnected: ${info?.reason || ''} (code ${info?.code || ''})`);
}
// Fan out to subscribers; label logs a thrower.
function fanout(subs, payload, label) {
for (const fn of subs) {
try { fn(payload); } catch (err) { if (label) console.error(`[foundry-bridge] ${label} subscriber threw:`, err); }
}
}
async function onMessage(msg) {
// Hello response from relay.
if (typeof msg.id === 'string' && msg.id.startsWith('hello-')) {
if (msg.error) {
console.error('[foundry-bridge] relay refused hello:', msg.error);
ui.notifications?.error(game.i18n.localize('FOUNDRY_BRIDGE.NOTIFY.Refused'));
} else if (msg.result) {
console.log(`[foundry-bridge] relay assigned sessionId=${msg.result.sessionId}, capabilitySet=${msg.result.capabilitySet}`);
}
return;
}
// Relay -> bridge notification (no id): Phase 2 chat channel.
if (msg.method && msg.id == null) {
if (msg.method === 'claude.reply') {
if (replySubs.size === 0) {
ui.notifications?.info(game.i18n.localize('FOUNDRY_BRIDGE.CHAT.ReplyWhileClosed'));
} else {
fanout(replySubs, msg.params || {}, 'reply');
}
return;
}
if (msg.method === 'claude.status') {
fanout(statusSubs, msg.params || {}, null); // status is best-effort
return;
}
if (msg.method === 'claude.confirm') {
const p = msg.params || {};
// No chat box: auto-deny so Claude isn't stuck on timeout.
if (confirmSubs.size === 0) {
client.send({ jsonrpc: '2.0', method: 'claude.confirm.result',
params: { opId: p.opId, approved: false, reason: 'chat-box-closed' } });
} else {
fanout(confirmSubs, p, 'confirm');
}
return;
}
if (msg.method === 'claude.refactor.set') {
const p = msg.params || {};
lastRefactor = { content: p.content ?? '', macroId: p.macroId ?? null, macroName: p.macroName ?? null };
if (refactorSubs.size === 0) {
ui.notifications?.info(game.i18n.localize('FOUNDRY_BRIDGE.WORKSHOP.PushedClosed'));
} else {
fanout(refactorSubs, lastRefactor, 'refactor');
}
return;
}
return; // unknown notification - ignore
}
// Inbound command request (relay -> bridge).
if (msg.method && msg.id != null) {
const handler = HANDLERS[msg.method];
if (!handler) {
client.send({ jsonrpc: '2.0', id: msg.id, error: { code: -32601, message: `unknown method "${msg.method}"` } });
return;
}
const ctx = {
client,
send: (n) => client.send(n),
logTap,
};
try {
const result = await handler(msg.params || {}, ctx);
client.send({ jsonrpc: '2.0', id: msg.id, result });
} catch (err) {
client.send({
jsonrpc: '2.0',
id: msg.id,
error: {
code: err?.code ?? -33002,
message: err?.message || String(err),
data: err?.stack ? { stack: err.stack } : undefined,
},
});
}
}
// Responses to our own requests are ignored (Phase 1 sends none).
}