@polderlabs/bizar-plugin 0.6.2 → 0.8.0

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/index.ts CHANGED
@@ -98,6 +98,7 @@ import type { Plugin, Hooks, PluginInput, PluginOptions } from "@opencode-ai/plu
98
98
  import { createLogger, type Logger } from "./src/logger.js";
99
99
  import { decide, isLogOnlyWarn } from "./src/loop.js";
100
100
  import { fingerprint } from "./src/fingerprint.js";
101
+ import { createDashboardPublisher, type DashboardPublisher } from "./src/dashboard-client.js";
101
102
  import { StateStore, type SessionState } from "./src/state.js";
102
103
  import { LogWriter } from "./src/report.js";
103
104
  import {
@@ -244,6 +245,10 @@ interface RuntimeContext {
244
245
  seenMessageIds: Map<string, Set<string>>;
245
246
  /** sessionID → pending system-transform message, set at warn/escalate. */
246
247
  pendingInjections: Map<string, string>;
248
+ /** v0.7.0-alpha.1 — Dashboard publisher (or null if disabled/not started).
249
+ * Used by the `event` hook to forward opencode session lifecycle
250
+ * events to the v2 dashboard via the @polderlabs/bizar-sdk. */
251
+ dashboardPublisher: DashboardPublisher | null;
247
252
  }
248
253
 
249
254
  /**
@@ -333,10 +338,11 @@ async function init(
333
338
 
334
339
  // --- Background agents (v0.4.2) -----------------------------------------
335
340
 
336
- let instanceManager: InstanceManager | null = null;
337
- let serve: ServeLifecycle | null = null;
338
- let stream: EventStream | null = null;
339
- let bgAvailable = false;
341
+ let instanceManager: InstanceManager | null = null;
342
+ let serve: ServeLifecycle | null = null;
343
+ let stream: EventStream | null = null;
344
+ let dashboardPublisher: DashboardPublisher | null = null;
345
+ let bgAvailable = false;
340
346
 
341
347
  if (readServeDisabled()) {
342
348
  logger.info("bizar: background agents disabled via BIZAR_SERVE_DISABLE=1");
@@ -394,6 +400,44 @@ async function init(
394
400
  });
395
401
  streamHandle = stream;
396
402
 
403
+ // v0.7.0-alpha.1 — Wire dashboard publisher to the EventStream so
404
+ // every opencode SSE event is also published to the v2 dashboard.
405
+ // The publisher gracefully degrades if the dashboard is unreachable
406
+ // (queues, retries, warns; never throws into the plugin).
407
+ try {
408
+ const pub = createDashboardPublisher({
409
+ logger,
410
+ });
411
+ await pub.start();
412
+ dashboardPublisher = pub;
413
+ stream.onEvent((event) => {
414
+ // Translate opencode StreamEvent → DashboardEvent shape.
415
+ // The dashboard only cares about the wire-level (type, properties)
416
+ // so we forward { type, properties } directly. Cast through
417
+ // `unknown` because the opencode event shape doesn't exactly
418
+ // match the SDK's discriminated DashboardEvent — it's a
419
+ // forward-compatible passthrough.
420
+ const dashEvent = {
421
+ type: event.type,
422
+ properties: { ...event },
423
+ } as unknown as Parameters<DashboardPublisher["publish"]>[0];
424
+ void pub.publish(dashEvent);
425
+ });
426
+ // Stop the publisher when the stream is disposed.
427
+ const origDisconnect = stream.disconnect.bind(stream);
428
+ stream.disconnect = async () => {
429
+ await origDisconnect();
430
+ pub.stop();
431
+ };
432
+ logger.info("bizar: dashboard publisher wired to EventStream (v2 protocol)");
433
+ } catch (err) {
434
+ logger.warn(
435
+ `bizar: dashboard publisher failed to start: ${
436
+ err instanceof Error ? err.message : String(err)
437
+ }`,
438
+ );
439
+ }
440
+
397
441
  instanceManager = new InstanceManager({
398
442
  stateStore: bgStateStore,
399
443
  maxConcurrent,
@@ -469,6 +513,7 @@ async function init(
469
513
  directory: input.directory,
470
514
  seenMessageIds: new Map(),
471
515
  pendingInjections: new Map(),
516
+ dashboardPublisher,
472
517
  };
473
518
 
474
519
  return buildHooks(ctx, { instanceManager, bgAvailable });
@@ -772,9 +817,13 @@ function buildHooks(ctx: RuntimeContext, bg: BgDeps): Hooks {
772
817
  const tools = bg.instanceManager
773
818
  ? {
774
819
  ...basePlanTools,
820
+ // v0.8.0 — bg-spawn no longer needs the HTTP client. It
821
+ // spawns an `opencode run` subprocess per agent (see
822
+ // src/opencode-runner.ts). The serve child is still
823
+ // available for the dashboard's v2 protocol and for any
824
+ // TUI/web client that wants to attach to it.
775
825
  bizar_spawn_background: createBgSpawnTool({
776
826
  instanceManager: bg.instanceManager,
777
- http: (bg.instanceManager as unknown as { http: HttpClient }).http,
778
827
  worktree: ctx.worktree,
779
828
  logger: ctx.logger,
780
829
  }),
@@ -859,10 +908,37 @@ function buildHooks(ctx: RuntimeContext, bg: BgDeps): Hooks {
859
908
  // `chat.message` seed, per spec §4.5.1).
860
909
  event: async ({ event }) => {
861
910
  try {
862
- const ev = event as { type?: string; sessionID?: string };
911
+ // v0.7.0-alpha.1 opencode's event object has { type: string,
912
+ // properties: { sessionID: string, ... } }. The legacy plugin
913
+ // assumed `event.sessionID` was top-level (which is wrong), so
914
+ // the hook returned early for every event. We extract from
915
+ // BOTH locations to be robust across opencode versions, and
916
+ // publish to the dashboard regardless.
917
+ const ev = event as {
918
+ type?: string;
919
+ sessionID?: string;
920
+ properties?: { sessionID?: string; [k: string]: unknown };
921
+ };
863
922
  const type = ev.type;
864
- const sessionID = ev.sessionID;
865
- if (!type || !sessionID) return;
923
+ const sessionID = ev.sessionID ?? ev.properties?.sessionID;
924
+ if (!type) return;
925
+
926
+ // v0.7.0-alpha.1 — Forward every opencode event to the dashboard.
927
+ // The plugin SDK does NOT translate opencode events to the SDK's
928
+ // discriminated DashboardEvent shape (it's a forward-compatible
929
+ // passthrough — the dashboard is happy with any {type, properties}
930
+ // event object). The publish is fire-and-forget; failures are
931
+ // logged and swallowed inside the publisher.
932
+ if (ctx.dashboardPublisher !== null) {
933
+ const dashEvent = {
934
+ type,
935
+ properties: { ...ev },
936
+ } as unknown as Parameters<DashboardPublisher["publish"]>[0];
937
+ void ctx.dashboardPublisher.publish(dashEvent);
938
+ }
939
+
940
+ // Legacy logic below: only runs when we have a sessionID.
941
+ if (!sessionID) return;
866
942
 
867
943
  if (type === "session.deleted") {
868
944
  await ctx.stateStore.withLock(sessionID, async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polderlabs/bizar-plugin",
3
- "version": "0.6.2",
3
+ "version": "0.8.0",
4
4
  "description": "Bizar opencode plugin — loop detection, status reporting, handoff signal, background agents, and slash commands + visual plan flow for subagent activity",
5
5
  "type": "module",
6
6
  "main": "./index.ts",
@@ -26,6 +26,7 @@
26
26
  "node": ">=20"
27
27
  },
28
28
  "dependencies": {
29
+ "@polderlabs/bizar-sdk": "*",
29
30
  "zod": "4.1.8"
30
31
  },
31
32
  "devDependencies": {
@@ -107,8 +107,32 @@ export type BackgroundStatus =
107
107
  * - `restartCount` — number of times this instance has been
108
108
  * auto-restarted (not including the original spawn). Default 0.
109
109
  * - `maxRestarts` — cap; default 3.
110
- * - `lastRestartAt` — epoch ms of the most recent auto-restart.
111
- */
110
+ * - `lastRestartAt` — epoch ms of the most recent auto-restart.
111
+ * - `processId` (v0.8.0) — PID of the opencode run subprocess. Set
112
+ * by the opencode-runner after `Bun.spawn` returns. Optional
113
+ * for backward compat.
114
+ * - `exitCode` (v0.8.0) — exit code of the opencode run
115
+ * subprocess. Populated when the process exits.
116
+ * - `runnerState` (v0.8.0) — free-form status from the runner
117
+ * ("starting" | "running" | "done" | "failed" | "killed").
118
+ * Allows the dashboard to show runner-level state separately
119
+ * from the higher-level `status`.
120
+ * - `runnerError` (v0.8.0) — opencode run subprocess error (e.g.
121
+ * "opencode run exited with code 1").
122
+ * - `spawnMessage` (v0.8.0) — "you can continue" message returned
123
+ * to the LLM by the spawn tool. Surfaces in the dashboard.
124
+ * - `spawnNextSteps` (v0.8.0) — list of next-step hints returned
125
+ * to the LLM by the spawn tool.
126
+ * - `sessionIdAt` (v0.8.0) — when the opencode sessionId was
127
+ * first observed in the subprocess stderr.
128
+ * - `runnerStartedAt` / `runnerEndedAt` — when the subprocess
129
+ * started / ended (subset of `startedAt`/`completedAt`).
130
+ * - `spawnedAt` — when the bg-spawn tool recorded the instance.
131
+ * Distinct from `startedAt` because the runner starts a
132
+ * tick or two later.
133
+ * - `exitSignal` — exit signal name (e.g. "SIGTERM",
134
+ * "SIGKILL") if the subprocess was killed.
135
+ */
112
136
  export interface BackgroundState {
113
137
  instanceId: string;
114
138
  sessionId: string;
@@ -153,6 +177,18 @@ export interface BackgroundState {
153
177
  * `{ ok: false }`. Cleared on a successful restart.
154
178
  */
155
179
  restartError?: string;
180
+ // v0.8.0 — process tracking (see opencode-runner.ts).
181
+ processId?: number;
182
+ exitCode?: number;
183
+ runnerState?: string;
184
+ runnerError?: string;
185
+ spawnMessage?: string;
186
+ spawnNextSteps?: string[];
187
+ sessionIdAt?: number;
188
+ runnerStartedAt?: number;
189
+ runnerEndedAt?: number;
190
+ spawnedAt?: number;
191
+ exitSignal?: string;
156
192
  }
157
193
 
158
194
  /**
package/src/background.ts CHANGED
@@ -866,6 +866,19 @@ export class InstanceManager {
866
866
  }
867
867
  }
868
868
 
869
+ /**
870
+ * v0.8.0 — Public version of `_maybeAutoRestart`. The opencode-runner
871
+ * (see src/opencode-runner.ts) calls this from its onExit callback
872
+ * when a `bizar_spawn_background` subprocess exits. Without this,
873
+ * persistent instances would never auto-restart under the new
874
+ * subprocess-based path (the SSE event handler that previously
875
+ * triggered auto-restart is no longer wired up because we don't
876
+ * subscribe to per-session events anymore).
877
+ */
878
+ async maybeAutoRestart(instanceId: string): Promise<void> {
879
+ await this._maybeAutoRestart(instanceId);
880
+ }
881
+
869
882
  /** v0.5.5 — Attempt an auto-restart for a persistent failed instance. */
870
883
  private async _maybeAutoRestart(instanceId: string): Promise<void> {
871
884
  const inst = this.instances.get(instanceId);
package/src/commands.ts CHANGED
@@ -314,7 +314,7 @@ function handleVisualPlan(arg: string, ctx: ParseContext): SlashCommandResult {
314
314
  // No argument — return current state as a dialog
315
315
  return {
316
316
  handled: true,
317
- response: "",
317
+ response: `Visual plan mode is currently ${currentEnabled ? "on" : "off"}.`,
318
318
  dialog: {
319
319
  id: generateId(),
320
320
  title: "Visual Plan",
@@ -334,7 +334,7 @@ function handleVisualPlan(arg: string, ctx: ParseContext): SlashCommandResult {
334
334
  if (lc === "on" || lc === "true" || lc === "1" || lc === "enable") {
335
335
  return {
336
336
  handled: true,
337
- response: "",
337
+ response: `Visual plan mode is now on.`,
338
338
  settingsPatch: { visualPlanEnabled: true },
339
339
  dialog: {
340
340
  id: generateId(),
@@ -353,7 +353,7 @@ function handleVisualPlan(arg: string, ctx: ParseContext): SlashCommandResult {
353
353
  if (lc === "off" || lc === "false" || lc === "0" || lc === "disable") {
354
354
  return {
355
355
  handled: true,
356
- response: "",
356
+ response: `Visual plan mode is now off.`,
357
357
  settingsPatch: { visualPlanEnabled: false },
358
358
  dialog: {
359
359
  id: generateId(),
@@ -372,7 +372,7 @@ function handleVisualPlan(arg: string, ctx: ParseContext): SlashCommandResult {
372
372
  if (lc === "status" || lc === "state" || lc === "?") {
373
373
  return {
374
374
  handled: true,
375
- response: "",
375
+ response: `Visual plan mode is currently ${currentEnabled ? "on" : "off"}.`,
376
376
  dialog: {
377
377
  id: generateId(),
378
378
  title: "Visual Plan",
@@ -444,7 +444,7 @@ function handlePlan(arg: string, ctx: ParseContext): SlashCommandResult {
444
444
  function helpPlan(): SlashCommandResult {
445
445
  return {
446
446
  handled: true,
447
- response: "",
447
+ response: "Plan commands: /plan new <slug> [template] | /plan list | /plan open <slug> | /plan get <slug> | /plan add <slug> | /plan update <slug> <id> | /plan delete <slug> <id> | /plan comment <slug> [id] \"text\" | /plan comments <slug> [id] | /plan status <slug> <status> | /plan wait <slug> [--timeout N]. Run /plan <subcommand> for details.",
448
448
  dialog: {
449
449
  id: generateId(),
450
450
  title: "Plan Commands",
@@ -477,7 +477,7 @@ function handlePlanNew(args: string[], ctx: ParseContext): SlashCommandResult {
477
477
  if (args.length === 0 || args[0] === "") {
478
478
  return {
479
479
  handled: true,
480
- response: "",
480
+ response: "Usage: /plan new <slug> [template]. Available templates: " + KNOWN_TEMPLATES.join(", ") + ".",
481
481
  dialog: {
482
482
  id: generateId(),
483
483
  title: "Create New Plan",
@@ -520,7 +520,7 @@ function handlePlanNew(args: string[], ctx: ParseContext): SlashCommandResult {
520
520
 
521
521
  return {
522
522
  handled: true,
523
- response: "",
523
+ response: `Created plan "${titleCase(slug)}" with the "${resolvedTemplate}" template. Use /plan open ${slug} to open it.`,
524
524
  sideEffect: {
525
525
  kind: "create_plan",
526
526
  slug,
@@ -544,9 +544,26 @@ function handlePlanNew(args: string[], ctx: ParseContext): SlashCommandResult {
544
544
 
545
545
  function handlePlanList(ctx: ParseContext): SlashCommandResult {
546
546
  const slugs = ctx.availablePlanSlugs ?? [];
547
+ if (slugs.length === 0) {
548
+ return {
549
+ handled: true,
550
+ response: "No plans found in the current worktree. Use /plan new <slug> to create one.",
551
+ sideEffect: { kind: "list_plans" },
552
+ dialog: {
553
+ id: generateId(),
554
+ title: "Plans",
555
+ command: "/plan list",
556
+ component: "plan-list",
557
+ data: {
558
+ plans: slugs,
559
+ count: slugs.length,
560
+ },
561
+ },
562
+ };
563
+ }
547
564
  return {
548
565
  handled: true,
549
- response: "",
566
+ response: `Found ${slugs.length} plan(s) (${slugs.length}): ${slugs.join(", ")}.`,
550
567
  sideEffect: { kind: "list_plans" },
551
568
  dialog: {
552
569
  id: generateId(),
@@ -584,7 +601,7 @@ function handlePlanOpen(args: string[], ctx: ParseContext): SlashCommandResult {
584
601
 
585
602
  return {
586
603
  handled: true,
587
- response: "",
604
+ response: `Opening plan "${slug}" at ${url}.`,
588
605
  settingsPatch: { lastUsedSlug: slug },
589
606
  sideEffect: {
590
607
  kind: "open_plan_url",
@@ -618,7 +635,7 @@ function handlePlanGet(args: string[]): SlashCommandResult {
618
635
  }
619
636
  return {
620
637
  handled: true,
621
- response: "",
638
+ response: `Fetching canvas for plan "${slug}"…`,
622
639
  sideEffect: {
623
640
  kind: "tool_invocation",
624
641
  toolName: "bizar_plan_action",
@@ -1078,7 +1095,7 @@ function handleBizar(arg: string, ctx: ParseContext): SlashCommandResult {
1078
1095
  function helpResult(): SlashCommandResult {
1079
1096
  return {
1080
1097
  handled: true,
1081
- response: "",
1098
+ response: "Available commands: /visual-plan [on|off|status], /plan new <slug> [template], /plan list, /plan open <slug>, /plan get <slug>, /plan add <slug>, /plan update <slug> <id>, /plan delete <slug> <id>, /plan comment <slug> [id] \"text\", /plan comments <slug> [id], /plan status <slug> <status>, /plan wait <slug> [--timeout N], /bizar, /bizar <args>, /help. See the dialog for full descriptions.",
1082
1099
  dialog: {
1083
1100
  id: generateId(),
1084
1101
  title: "Bizar Commands",
@@ -0,0 +1,235 @@
1
+ /**
2
+ * dashboard-client.ts
3
+ *
4
+ * v0.7.0-alpha.1 — Plugin-side bridge to the Bizar dashboard via the
5
+ * @polderlabs/bizar-sdk. Replaces (does not remove) the file-based
6
+ * `serve-info.ts` bridge.
7
+ *
8
+ * Usage:
9
+ * import { createDashboardPublisher } from "./dashboard-client.js";
10
+ *
11
+ * const publisher = createDashboardPublisher({
12
+ * logger: myLogger,
13
+ * });
14
+ * await publisher.start();
15
+ * publisher.publish({
16
+ * type: "session.created",
17
+ * properties: { sessionId: "ses_1", agent: "mimir" },
18
+ * });
19
+ *
20
+ * Behavior:
21
+ * - Reads `BIZAR_DASHBOARD_URL` (default http://127.0.0.1:4098).
22
+ * - Reads `BIZAR_DASHBOARD_PASSWORD` first, then falls back to
23
+ * `~/.cache/bizarharness/dash-auth.json` (the file written by the
24
+ * dashboard on first start).
25
+ * - `publish()` is fire-and-forget: returns a Promise that resolves
26
+ * after one HTTP round-trip or rejects on failure. NEVER throws
27
+ * into the caller — failures are logged and swallowed (the plugin
28
+ * must keep running even when the dashboard is down).
29
+ * - `publish()` queues events if the dashboard is unreachable and
30
+ * drains the queue on reconnect (best-effort).
31
+ */
32
+
33
+ import {
34
+ createBizarClient,
35
+ isBizarError,
36
+ type BizarClient,
37
+ type DashboardEvent,
38
+ } from "@polderlabs/bizar-sdk";
39
+ import { readFileSync, existsSync } from "node:fs";
40
+ import { join } from "node:path";
41
+ import { homedir } from "node:os";
42
+
43
+ const DEFAULT_DASHBOARD_URL = "http://127.0.0.1:4098";
44
+ const DEFAULT_AUTH_FILE_PATHS = [
45
+ join(homedir(), ".cache", "bizarharness", "dash-auth.json"),
46
+ join(homedir(), ".cache", "bizar", "dash-auth.json"),
47
+ ];
48
+ /**
49
+ * The auth file paths to search. Override at test-time via
50
+ * `process.env.BIZAR_DASHBOARD_AUTH_FILE` (single file) or
51
+ * `process.env.BIZAR_DASHBOARD_AUTH_FILES` (colon-separated list).
52
+ */
53
+ function getAuthFilePaths(): string[] {
54
+ const single = process.env.BIZAR_DASHBOARD_AUTH_FILE;
55
+ if (single) return [single];
56
+ const multi = process.env.BIZAR_DASHBOARD_AUTH_FILES;
57
+ if (multi) return multi.split(":").filter(Boolean);
58
+ return DEFAULT_AUTH_FILE_PATHS;
59
+ }
60
+
61
+ export interface Logger {
62
+ debug(message: string): void;
63
+ info(message: string): void;
64
+ warn(message: string): void;
65
+ error(message: string): void;
66
+ }
67
+
68
+ export interface DashboardPublisherOptions {
69
+ logger: Logger;
70
+ baseUrl?: string;
71
+ password?: string;
72
+ /** Disable the publisher entirely (e.g. BIZAR_DASHBOARD_DISABLE=1). */
73
+ disabled?: boolean;
74
+ /** Max queued events while the dashboard is unreachable. */
75
+ queueLimit?: number;
76
+ }
77
+
78
+ /**
79
+ * Read the dashboard auth record from one of the candidate paths.
80
+ * Returns null if no usable file exists. Never throws.
81
+ */
82
+ function readDashboardAuth(): { password: string; baseUrl?: string; port?: number } | null {
83
+ for (const candidate of getAuthFilePaths()) {
84
+ if (!existsSync(candidate)) continue;
85
+ try {
86
+ const raw = readFileSync(candidate, "utf8");
87
+ const parsed = JSON.parse(raw) as {
88
+ password?: unknown;
89
+ baseUrl?: unknown;
90
+ port?: unknown;
91
+ };
92
+ if (typeof parsed.password !== "string" || parsed.password.length < 16) continue;
93
+ return {
94
+ password: parsed.password,
95
+ baseUrl: typeof parsed.baseUrl === "string" ? parsed.baseUrl : undefined,
96
+ port: typeof parsed.port === "number" ? parsed.port : undefined,
97
+ };
98
+ } catch {
99
+ // ignore malformed file
100
+ }
101
+ }
102
+ return null;
103
+ }
104
+
105
+ export interface DashboardPublisher {
106
+ start(): Promise<void>;
107
+ publish(event: DashboardEvent): Promise<void>;
108
+ stop(): void;
109
+ /** Whether the publisher is currently configured and ready. */
110
+ isReady(): boolean;
111
+ }
112
+
113
+ export function createDashboardPublisher(
114
+ options: DashboardPublisherOptions,
115
+ ): DashboardPublisher {
116
+ const { logger, disabled = false, queueLimit = 100 } = options;
117
+
118
+ let client: BizarClient | null = null;
119
+ const queue: DashboardEvent[] = [];
120
+ let flushing = false;
121
+
122
+ function resolveConfig(): { baseUrl: string; password: string } | null {
123
+ let baseUrl = options.baseUrl ?? process.env.BIZAR_DASHBOARD_URL;
124
+ if (!baseUrl) {
125
+ const port = process.env.BIZAR_DASHBOARD_PORT;
126
+ baseUrl = port ? `http://127.0.0.1:${port}` : DEFAULT_DASHBOARD_URL;
127
+ }
128
+ const password =
129
+ options.password ??
130
+ process.env.BIZAR_DASHBOARD_PASSWORD ??
131
+ readDashboardAuth()?.password;
132
+
133
+ if (!password) {
134
+ return null;
135
+ }
136
+ return { baseUrl, password };
137
+ }
138
+
139
+ function isReady(): boolean {
140
+ return client !== null;
141
+ }
142
+
143
+ async function start(): Promise<void> {
144
+ if (disabled) {
145
+ logger.debug("bizar: dashboard publisher disabled by config");
146
+ return;
147
+ }
148
+ const cfg = resolveConfig();
149
+ if (!cfg) {
150
+ logger.debug(
151
+ "bizar: dashboard publisher not started (no password in env or auth file)",
152
+ );
153
+ return;
154
+ }
155
+ client = createBizarClient({
156
+ baseUrl: cfg.baseUrl,
157
+ password: cfg.password,
158
+ });
159
+ logger.info(
160
+ `bizar: dashboard publisher started (url=${cfg.baseUrl}, password len=${cfg.password.length})`,
161
+ );
162
+
163
+ // Verify the dashboard is reachable. Non-fatal — publish() will retry
164
+ // and surface connection errors as warnings.
165
+ try {
166
+ const health = await client.health.check();
167
+ if (isBizarError(health)) {
168
+ logger.warn(
169
+ `bizar: dashboard unreachable at ${cfg.baseUrl} (${health.name}); publish events will be queued`,
170
+ );
171
+ }
172
+ } catch (err) {
173
+ logger.warn(
174
+ `bizar: dashboard health check failed: ${err instanceof Error ? err.message : String(err)}`,
175
+ );
176
+ }
177
+
178
+ void flushQueue();
179
+ }
180
+
181
+ async function publish(event: DashboardEvent): Promise<void> {
182
+ if (!client) {
183
+ logger.debug(
184
+ `bizar: dashboard publisher not ready — dropping event ${event.type}`,
185
+ );
186
+ return;
187
+ }
188
+ if (queue.length >= queueLimit) {
189
+ queue.shift();
190
+ logger.warn(
191
+ `bizar: dashboard publisher queue full (${queueLimit}); dropping oldest event`,
192
+ );
193
+ }
194
+ queue.push(event);
195
+ if (!flushing) {
196
+ void flushQueue();
197
+ }
198
+ }
199
+
200
+ async function flushQueue(): Promise<void> {
201
+ if (flushing || !client) return;
202
+ flushing = true;
203
+ try {
204
+ while (queue.length > 0 && client) {
205
+ const next = queue.shift()!;
206
+ try {
207
+ const result = await client.events.publish(next);
208
+ if (isBizarError(result)) {
209
+ logger.warn(
210
+ `bizar: dashboard publish failed (${result.name}); requeueing ${next.type}`,
211
+ );
212
+ queue.unshift(next);
213
+ break;
214
+ }
215
+ } catch (err) {
216
+ logger.warn(
217
+ `bizar: dashboard publish threw: ${err instanceof Error ? err.message : String(err)}`,
218
+ );
219
+ queue.unshift(next);
220
+ break;
221
+ }
222
+ }
223
+ } finally {
224
+ flushing = false;
225
+ }
226
+ }
227
+
228
+ function stop(): void {
229
+ client = null;
230
+ queue.length = 0;
231
+ logger.debug("bizar: dashboard publisher stopped");
232
+ }
233
+
234
+ return { start, publish, stop, isReady };
235
+ }
@@ -120,6 +120,8 @@ export class EventStream {
120
120
  private _logger: Logger;
121
121
  private _http: HttpClient;
122
122
  private _handlers = new Map<string, Set<SessionEventHandler>>();
123
+ /** Global handler invoked for every event regardless of session (v0.7.0). */
124
+ private _globalHandler: SessionEventHandler | null = null;
123
125
  private _connected = false;
124
126
  private _aborted = false;
125
127
  private _abortController: AbortController | null = null;
@@ -128,6 +130,21 @@ export class EventStream {
128
130
  private _reconnectTimer: ReturnType<typeof setTimeout> | null = null;
129
131
  private _connectPromise: Promise<void> | null = null;
130
132
 
133
+ /**
134
+ * Register a global handler invoked for every event (regardless of
135
+ * session). Used to forward events to the dashboard publisher. Returns
136
+ * an unsubscribe function. (v0.7.0-alpha.1 — wired into the v2
137
+ * SDK dashboard-client.ts bridge.)
138
+ */
139
+ onEvent(handler: SessionEventHandler): () => void {
140
+ this._globalHandler = handler;
141
+ return () => {
142
+ if (this._globalHandler === handler) {
143
+ this._globalHandler = null;
144
+ }
145
+ };
146
+ }
147
+
131
148
  constructor(opts: {
132
149
  baseUrl: string;
133
150
  directory: string;
@@ -439,6 +456,21 @@ export class EventStream {
439
456
  }
440
457
 
441
458
  private dispatchToHandlers(sessionID: string, event: StreamEvent): void {
459
+ // v0.7.0-alpha.1 — Forward every event to the global handler
460
+ // (typically the dashboard publisher) BEFORE the per-session dispatch.
461
+ // Failures here are isolated so one bad handler doesn't break the
462
+ // dispatch chain for other subscribers.
463
+ if (this._globalHandler) {
464
+ try {
465
+ this._globalHandler(event);
466
+ } catch (err: unknown) {
467
+ const msg = err instanceof Error ? err.message : String(err);
468
+ this._logger.warn(
469
+ `bizar: SSE global handler threw for session ${sessionID} (type=${event.type}): ${msg}`,
470
+ );
471
+ }
472
+ }
473
+
442
474
  const set = this._handlers.get(sessionID);
443
475
  if (!set || set.size === 0) {
444
476
  this._logger.debug(