-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbridge.js
More file actions
346 lines (322 loc) · 12.2 KB
/
Copy pathbridge.js
File metadata and controls
346 lines (322 loc) · 12.2 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Foundry-Claude Bridge - main module entry. Registers settings, opens (when
// enabled) a WebSocket connection to the local relay, dispatches inbound
// JSON-RPC requests through the handler set, and emits outbound notifications
// (e.g. logs.entry) when the relay subscribes.
import { WsClient } from './ws-client.js';
import { LogTap } from './log-tap.js';
import { CHAT_MACRO_COMMAND } from './chat-macro.js';
import { registerModeSettings, settingsSnapshot } from './settings-def.js';
import { AagmSettingsMenu } from './settings-menu.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 { handleLootPending, handleLootRestore } from './handlers/loot.js';
const MODULE_ID = 'foundry-bridge';
const MODULE_VERSION = '0.9.2';
const CHAT_MACRO_NAME = 'Open Claude Code Chat';
let client = null;
let logTap = null;
let wasConnected = false;
// Phase 2 chat channel: the auto-created macro's Dialog registers here so it
// receives relay -> bridge `claude.reply` / `claude.status` notifications.
// Module-scoped so they survive WS reconnects (client is rebuilt; these aren't).
const replySubs = new Set();
const statusSubs = new Set();
// DESIGN §9 confirmation gate: the chat box registers here to render
// claude.confirm cards and send the human's decision back.
const confirmSubs = new Set();
// §13.3 chain grant/gate/end events for the box.
const chainSubs = new Set();
// §14 tab table pushes (claude.tabs) for the box.
const tabsSubs = new Set();
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,
// Claude Loot Watchdog rescue queue (the only allowed path to that journal).
'loot.pending': handleLootPending,
'loot.restore': handleLootRestore,
};
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',
});
// §13.1 hidden world settings + sanctioned submenu.
registerModeSettings();
game.settings.registerMenu(MODULE_ID, 'aagmSettings', {
name: 'FOUNDRY_BRIDGE.SETTINGS.MenuName',
label: 'FOUNDRY_BRIDGE.SETTINGS.MenuLabel',
hint: 'FOUNDRY_BRIDGE.SETTINGS.MenuHint',
icon: 'fas fa-robot',
type: AagmSettingsMenu,
restricted: true,
});
});
Hooks.once('ready', () => {
// The bridge is a GM-only surface. Players must never connect to (or even
// notice) the relay - no api, no macros, no WS client, no notifications.
if (!game.user.isGM) return;
// Expose a tiny in-world API for debug from 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, tabId) => {
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, tabId } });
return ok ? promptId : null;
},
// §14: close rides the prompt path so the loop cannot miss it.
closeTab: (tabId) => {
if (!client || !client.isOpen()) return;
const promptId = `close-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
client.send({ jsonrpc: '2.0', method: 'claude.prompt', params: { promptId, text: '/close', tabId } });
},
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); },
onChain: (cb) => { chainSubs.add(cb); return () => chainSubs.delete(cb); },
onTabs: (cb) => { tabsSubs.add(cb); return () => tabsSubs.delete(cb); },
sendConfirmResult: (opId, approved, reason) => {
if (client && client.isOpen()) {
client.send({ jsonrpc: '2.0', method: 'claude.confirm.result', params: { opId, approved: !!approved, reason } });
}
},
cancelChain: (chainId) => {
if (client && client.isOpen()) {
client.send({ jsonrpc: '2.0', method: 'claude.chain.cancel', params: { chainId } });
}
},
syncSettings: () => {
if (client && client.isOpen()) {
client.send({ jsonrpc: '2.0', method: 'settings.sync', params: settingsSnapshot() });
}
},
};
}
ensureMacro(CHAT_MACRO_NAME, CHAT_MACRO_COMMAND, 'icons/svg/chat.svg');
if (game.settings.get(MODULE_ID, 'enabled')) {
startClient();
} else {
console.log('[foundry-bridge] disabled in settings; not connecting.');
}
});
// The module provisions its GUI macros itself. GM-only (players don't drive
// Claude Code), idempotent by name so a reload doesn't pile up copies. The
// command body is kept in sync on reload, but only for macros WE created
// (autoMacro flag) - never a hand-rolled macro that merely shares the name.
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;
// 'enabled' is client-scoped (localStorage), so a player logging in on a
// browser where a GM once enabled the bridge would otherwise start it.
if (!game.user?.isGM) return;
// Install the log tap before connecting so reconnect-time output is captured.
// The tap is cheap when no subscribers are registered.
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() {
wasConnected = true;
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,
settings: settingsSnapshot(),
},
id: helloId,
});
ui.notifications?.info(game.i18n.localize('FOUNDRY_BRIDGE.NOTIFY.Connected'));
}
function onDisconnected(info) {
// Every failed reconnect attempt also lands here; toasting each backoff
// tick is spam. Only a real drop of an established connection notifies.
if (wasConnected) {
ui.notifications?.warn(game.i18n.localize('FOUNDRY_BRIDGE.NOTIFY.Disconnected'));
}
wasConnected = false;
console.log(`[foundry-bridge] disconnected: ${info?.reason || ''} (code ${info?.code || ''})`);
}
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 {
for (const fn of replySubs) {
try { fn(msg.params || {}); } catch (err) { console.error('[foundry-bridge] reply subscriber threw:', err); }
}
}
return;
}
if (msg.method === 'claude.status') {
for (const fn of statusSubs) {
try { fn(msg.params || {}); } catch (err) { /* status is best-effort */ }
}
return;
}
if (msg.method === 'claude.chain') {
for (const fn of chainSubs) {
try { fn(msg.params || {}); } catch (err) { /* progress is best-effort */ }
}
return;
}
if (msg.method === 'claude.tabs') {
for (const fn of tabsSubs) {
try { fn(msg.params || {}); } catch (err) { /* table resync is best-effort */ }
}
return;
}
if (msg.method === 'claude.listener.refused') {
// §13.2 lock tripped: a second loop tried to drain the box. Toast so
// it is visible whether or not the box is open.
ui.notifications?.warn(game.i18n.localize('FOUNDRY_BRIDGE.NOTIFY.SecondListener'));
return;
}
if (msg.method === 'claude.confirm') {
const p = msg.params || {};
// No open chat box = no human to approve. Auto-deny so Claude isn't left
// waiting on the relay timeout. (A pending write must never default open.)
if (confirmSubs.size === 0) {
client.send({ jsonrpc: '2.0', method: 'claude.confirm.result',
params: { opId: p.opId, approved: false, reason: 'chat-box-closed' } });
} else {
for (const fn of confirmSubs) {
try { fn(p); } catch (err) { console.error('[foundry-bridge] confirm subscriber threw:', err); }
}
}
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,
},
});
}
}
// Anything else (responses to requests we sent - Phase 1 doesn't initiate any) is ignored.
}