| 74 | ]); |
| 75 | |
| 76 | export class IpcAdapter implements PlatformAdapter, IpcBroadcaster { |
| 77 | readonly name = "ipc"; |
| 78 | |
| 79 | private agent: IPackAgent | null = null; |
| 80 | private rootDir = ""; |
| 81 | private adapterMap: Map<string, PlatformAdapter> | null = null; |
| 82 | private conversationService: ConversationService | null = null; |
| 83 | private readonly createdChannels = new Set<string>(); |
| 84 | private messageListener?: (message: unknown) => void; |
| 85 | private started = false; |
| 86 | |
| 87 | async start(ctx: AdapterContext): Promise<void> { |
| 88 | // IPC channel only exists when spawned via child_process.fork/spawn with stdio "ipc". |
| 89 | if (typeof process.send !== "function") { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | this.agent = ctx.agent; |
| 94 | this.rootDir = ctx.rootDir; |
| 95 | this.adapterMap = ctx.adapterMap ?? null; |
| 96 | this.conversationService = new ConversationService(ctx.rootDir); |
| 97 | |
| 98 | this.messageListener = (message: unknown) => { |
| 99 | if (!this.isIpcRequest(message)) return; |
| 100 | void this.handleRequest(message); |
| 101 | }; |
| 102 | process.on("message", this.messageListener); |
| 103 | |
| 104 | this.started = true; |
| 105 | console.log("[IpcAdapter] Started"); |
| 106 | } |
| 107 | |
| 108 | async stop(): Promise<void> { |
| 109 | if (this.messageListener) { |
| 110 | process.off("message", this.messageListener); |
| 111 | this.messageListener = undefined; |
| 112 | } |
| 113 | |
| 114 | if (this.started) { |
| 115 | console.log("[IpcAdapter] Stopped"); |
| 116 | } |
| 117 | this.started = false; |
| 118 | } |
| 119 | |
| 120 | notifyReady(port: number): void { |
| 121 | this.sendIpc({ |
| 122 | type: "ready", |
| 123 | port, |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | broadcastInbound( |
| 128 | channelId: string, |
| 129 | platform: string, |
| 130 | sender: { id: string; username: string }, |
| 131 | text: string, |
| 132 | ): void { |
| 133 | this.sendIpc({ |
nothing calls this directly
no outgoing calls
no test coverage detected