@comput/pi-intercom 0.10.0 → 0.11.1

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/src/index.ts CHANGED
@@ -98,7 +98,13 @@ export default function intercomExtension(pi: ExtensionAPI): void {
98
98
  })();
99
99
  try { await task.promise; } finally { if (monitorTask === task) monitorTask = undefined; }
100
100
  }
101
- pi.on('session_start', async (_event, ctx) => {
101
+ let starting = false;
102
+ async function startSession(ctx: ExtensionContext, activate = false): Promise<void> {
103
+ if (starting) throw new Error('PiIntercom activation already in progress');
104
+ starting = true;
105
+ try { await initializeSession(ctx, activate); } finally { starting = false; }
106
+ }
107
+ async function initializeSession(ctx: ExtensionContext, activate: boolean): Promise<void> {
102
108
  const started = ++generation;
103
109
  activity.reset();
104
110
  const oldRuntime = runtime;
@@ -106,12 +112,29 @@ export default function intercomExtension(pi: ExtensionAPI): void {
106
112
  await oldRuntime?.close();
107
113
  if (started !== generation) return;
108
114
  context = ctx;
109
- if (!['win32', 'linux'].includes(process.platform) || ctx.mode !== 'tui') throw new Error('PiIntercom requires Windows or Linux interactive Pi. No resources started.');
110
- if (!ctx.isProjectTrusted()) throw new Error('PiIntercom requires project trust before honoring shared project configuration.');
115
+ if (!['win32', 'linux'].includes(process.platform) || ctx.mode !== 'tui') {
116
+ if (!activate) return;
117
+ throw new Error('PiIntercom requires Windows or Linux interactive Pi. No resources started.');
118
+ }
119
+ if (!ctx.isProjectTrusted()) {
120
+ if (!activate) return;
121
+ throw new Error('PiIntercom requires project trust before honoring shared project configuration.');
122
+ }
111
123
  // Flags become available at session_start, not extension factory time. Never
112
124
  // infer workspace authority from ancestors or inherited environment values.
113
125
  const workspaceRoot = pi.getFlag('intercom-root');
114
126
  if (workspaceRoot !== undefined && typeof workspaceRoot !== 'string') throw new Error('PiIntercom: intercom-root must be an explicit absolute path.');
127
+ let existing: ConfigStore | undefined;
128
+ try {
129
+ existing = workspaceRoot !== undefined
130
+ ? await ConfigStore.workspace(ctx.cwd, workspaceRoot)
131
+ : await ConfigStore.discover(ctx.cwd);
132
+ } catch (error) {
133
+ if (activate) throw error;
134
+ ctx.ui.notify(String(error), 'error'); return;
135
+ }
136
+ if (started !== generation) return;
137
+ if (!existing && !activate) return;
115
138
  runtime = new Intercom({
116
139
  cwd: ctx.cwd,
117
140
  sessionId: () => context!.sessionManager.getSessionId(),
@@ -128,7 +151,7 @@ export default function intercomExtension(pi: ExtensionAPI): void {
128
151
  },
129
152
  notify: text => ctx.ui.notify(text, 'info'),
130
153
  }, {
131
- launch: launcher.launch, store: root => new PiConfigStore(root), workspaceRoot,
154
+ launch: launcher.launch, store: root => new PiConfigStore(root), workspaceRoot, requireExistingConfig: !activate,
132
155
  monitor: {
133
156
  open: request => ensureMonitorPane({ ...request,
134
157
  script: fileURLToPath(new URL('../dist/monitor.js', import.meta.url)),
@@ -152,7 +175,7 @@ export default function intercomExtension(pi: ExtensionAPI): void {
152
175
  });
153
176
  const current = runtime;
154
177
  try { await current.start(); }
155
- catch (e) { if (runtime === current) runtime = undefined; ctx.ui.notify(String(e), 'error'); return; }
178
+ catch (e) { if (runtime === current) runtime = undefined; if (activate) throw e; ctx.ui.notify(String(e), 'error'); return; }
156
179
  if (started !== generation) { await current.close(); return; }
157
180
  try {
158
181
  if ((await current.state()).me?.coordinator) {
@@ -168,6 +191,20 @@ export default function intercomExtension(pi: ExtensionAPI): void {
168
191
  if (started !== generation) return;
169
192
  ctx.ui.notify(String(error), 'warning');
170
193
  }
194
+ }
195
+ pi.on('session_start', async (_event, ctx) => { await startSession(ctx); });
196
+ pi.registerTool({
197
+ name: 'intercom_activate', label: 'Activate Intercom', exposure: 'direct',
198
+ description: 'Explicitly enable Intercom in this project. If no config exists, create it and register this session as coordinator. Otherwise connect using the existing config; never replace its coordinator. Does not launch workers. Use only when the user asks to activate Intercom.',
199
+ parameters: Type.Object({}),
200
+ async execute(_id, _args, signal, _update, ctx) {
201
+ signal?.throwIfAborted();
202
+ if (!runtime) await startSession(ctx, true);
203
+ signal?.throwIfAborted();
204
+ if (!runtime) throw new Error('Intercom activation interrupted; inspect session state before retrying');
205
+ const state = await runtime.state();
206
+ return { content: [{ type: 'text', text: JSON.stringify({ active: true, configPath: runtime.store.file, role: state.me?.coordinator ? 'coordinator' : state.me ? 'worker' : 'anonymous' }) }], details: {} };
207
+ },
171
208
  });
172
209
  pi.on('session_shutdown', async () => {
173
210
  generation++;
@@ -229,7 +266,7 @@ export default function intercomExtension(pi: ExtensionAPI): void {
229
266
  exposure: operation === 'report_handoff' ? 'model-only' : 'direct',
230
267
  async execute(_id, args, signal, _update, ctx) {
231
268
  signal?.throwIfAborted(); context = ctx;
232
- if (!runtime) throw new Error('PiIntercom not initialized; inspect startup error.');
269
+ if (!runtime) throw new Error('PiIntercom not initialized (inactive). Ask the user to activate this project with intercom_activate; inspect any startup error first.');
233
270
  const current = runtime, started = generation;
234
271
  if (operation === 'close_monitor') {
235
272
  const state = await current.state();
package/src/launcher.ts CHANGED
@@ -66,8 +66,8 @@ export function launchers(options: LauncherOptions) {
66
66
  };
67
67
  try {
68
68
  // Injection is a trusted test seam; production verifies hello/status
69
- // for core 0.1.18 once, without reconnecting between launch steps.
70
- return await (options.coreCall ? operation(options.coreCall) : withCoreSession(operation, { endpoint: env.HC_SOCKET }));
69
+ // and the APIs needed for launch once, without reconnecting between steps.
70
+ return await (options.coreCall ? operation(options.coreCall) : withCoreSession(operation, { endpoint: env.HC_SOCKET, requiredMethods: ['terminal.get', 'terminal.run', 'terminal.label', 'doc.get', 'doc.set'] }));
71
71
  } catch (e) {
72
72
  if (attempted) throw Object.assign(new Error('HC worker launch or placement failed; outcome unknown, terminal/process and app tab may remain; no automatic cleanup/retry/replacement or legacy fallback'), { outcomeUncertain: true });
73
73
  fail(`HC launch preflight failed: ${String(e)}; no fallback or launch attempted`);
package/src/runtime.ts CHANGED
@@ -41,6 +41,8 @@ export interface RuntimeOptions {
41
41
  monitor?: MonitorOperations;
42
42
  /** Explicit launcher-provided root must equal canonical cwd; no parent-root exception. */
43
43
  workspaceRoot?: string;
44
+ /** Adapter startup must not recreate a config removed after discovery. */
45
+ requireExistingConfig?: boolean;
44
46
  }
45
47
  export class Intercom {
46
48
  store!: ConfigStore;
@@ -99,6 +101,7 @@ export class Intercom {
99
101
  ? await ConfigStore.workspace(this.host.cwd, this.options.workspaceRoot)
100
102
  : await ConfigStore.discover(this.host.cwd);
101
103
  assertValid();
104
+ if (!discovered && this.options.requireExistingConfig) fail('project config missing; explicit activation required');
102
105
  const root = discovered?.root ?? await realpath(this.host.cwd);
103
106
  assertValid();
104
107
  this.store = this.options.store?.(root) ?? discovered ?? new ConfigStore(root);