|
1 | | -// Auto-generated stub — replace with real implementation |
2 | | -export {}; |
3 | | -export const postInterClaudeMessage: (target: string, message: string) => Promise<{ ok: boolean; error?: string }> = () => Promise.resolve({ ok: false }); |
| 1 | +import axios from 'axios' |
| 2 | +import { logForDebugging } from '../utils/debug.js' |
| 3 | +import { errorMessage } from '../utils/errors.js' |
| 4 | +import { validateBridgeId } from './bridgeApi.js' |
| 5 | +import { getBridgeAccessToken } from './bridgeConfig.js' |
| 6 | +import { getReplBridgeHandle } from './replBridgeHandle.js' |
| 7 | +import { toCompatSessionId } from './sessionIdCompat.js' |
| 8 | + |
| 9 | +/** |
| 10 | + * Send a plain-text message to another Claude session via the bridge API. |
| 11 | + * |
| 12 | + * Called by SendMessageTool when the target address scheme is "bridge:". |
| 13 | + * Uses the current ReplBridgeHandle to derive the sender identity and |
| 14 | + * the session ingress URL for the POST request. |
| 15 | + * |
| 16 | + * @param target - Target session ID (from the "bridge:<sessionId>" address) |
| 17 | + * @param message - Plain text message content (structured messages are rejected upstream) |
| 18 | + * @returns { ok: true } on success, { ok: false, error } on failure. Never throws. |
| 19 | + */ |
| 20 | +export async function postInterClaudeMessage( |
| 21 | + target: string, |
| 22 | + message: string, |
| 23 | +): Promise<{ ok: true } | { ok: false; error: string }> { |
| 24 | + try { |
| 25 | + const handle = getReplBridgeHandle() |
| 26 | + if (!handle) { |
| 27 | + return { ok: false, error: 'Bridge not connected' } |
| 28 | + } |
| 29 | + |
| 30 | + const normalizedTarget = target.trim() |
| 31 | + if (!normalizedTarget) { |
| 32 | + return { ok: false, error: 'No target session specified' } |
| 33 | + } |
| 34 | + |
| 35 | + const accessToken = getBridgeAccessToken() |
| 36 | + if (!accessToken) { |
| 37 | + return { ok: false, error: 'No access token available' } |
| 38 | + } |
| 39 | + |
| 40 | + const compatTarget = toCompatSessionId(normalizedTarget) |
| 41 | + // Validate against path traversal — same allowlist as bridgeApi.ts |
| 42 | + validateBridgeId(compatTarget, 'target sessionId') |
| 43 | + const from = toCompatSessionId(handle.bridgeSessionId) |
| 44 | + const baseUrl = handle.sessionIngressUrl |
| 45 | + |
| 46 | + const url = `${baseUrl}/v1/sessions/${encodeURIComponent(compatTarget)}/messages` |
| 47 | + |
| 48 | + const response = await axios.post( |
| 49 | + url, |
| 50 | + { |
| 51 | + type: 'peer_message', |
| 52 | + from, |
| 53 | + content: message, |
| 54 | + }, |
| 55 | + { |
| 56 | + headers: { |
| 57 | + Authorization: `Bearer ${accessToken}`, |
| 58 | + 'Content-Type': 'application/json', |
| 59 | + 'anthropic-version': '2023-06-01', |
| 60 | + }, |
| 61 | + timeout: 10_000, |
| 62 | + validateStatus: (s: number) => s < 500, |
| 63 | + }, |
| 64 | + ) |
| 65 | + |
| 66 | + if (response.status === 200 || response.status === 204) { |
| 67 | + logForDebugging( |
| 68 | + `[bridge:peer] Message sent to ${compatTarget} (${response.status})`, |
| 69 | + ) |
| 70 | + return { ok: true } |
| 71 | + } |
| 72 | + |
| 73 | + const detail = |
| 74 | + typeof response.data === 'object' && response.data?.error?.message |
| 75 | + ? response.data.error.message |
| 76 | + : `HTTP ${response.status}` |
| 77 | + logForDebugging(`[bridge:peer] Send failed: ${detail}`) |
| 78 | + return { ok: false, error: detail } |
| 79 | + } catch (err: unknown) { |
| 80 | + const msg = errorMessage(err) |
| 81 | + logForDebugging(`[bridge:peer] postInterClaudeMessage error: ${msg}`) |
| 82 | + return { ok: false, error: msg } |
| 83 | + } |
| 84 | +} |
0 commit comments