@miadi/tide 0.6.9 → 0.6.10

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/client.ts +82 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miadi/tide",
3
- "version": "0.6.9",
3
+ "version": "0.6.10",
4
4
  "description": "Typed client boundary for the ironsilk `tide` runtime. Daemon Unix-socket transport with CLI fallback.",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/src/client.ts CHANGED
@@ -523,6 +523,88 @@ export async function openCompanion(opts: CompanionOpenOptions): Promise<TideCom
523
523
  }
524
524
  }
525
525
 
526
+ // ── Open one agent session by its id (jgwill/Miadi#868) ─────────────────────
527
+
528
+ export interface AgentOpenOptions {
529
+ /** A Claude session id, or its first 8 characters or more. */
530
+ session: string
531
+ /** Resume it when it does not run, instead of returning the plan. */
532
+ run?: boolean
533
+ timeoutMs?: number
534
+ }
535
+
536
+ /** `tide agents open --format json`: where the session runs, or how it is resumed. */
537
+ export interface TideAgentOpenPlan {
538
+ session_id: string
539
+ /** `running`: `pane_id` is its pane and nothing starts. `resume`: the plan. `refused`: `reason` says why. */
540
+ state: "running" | "resume" | "refused"
541
+ reason: string
542
+ kind: string
543
+ name: string
544
+ cwd: string
545
+ launch_alias: string
546
+ /** The tmux session it runs in, or the one it is resumed in. */
547
+ tmux_session: string
548
+ /** The pane it runs in; after a run, the pane it was resumed in. */
549
+ pane_id: string
550
+ command: string[]
551
+ how: string
552
+ /** `new-session`, or `new-window` in a session of its name that exists. */
553
+ target: "" | "new-session" | "new-window"
554
+ tmux_commands: string[]
555
+ last_seen: string
556
+ executed: boolean
557
+ }
558
+
559
+ export interface TideAgentOpenResult {
560
+ /** 0 an answer (running, a plan, or resumed), 4 refused, 2 the controller gate or an unknown session, 1 tmux failed. */
561
+ exitCode: number
562
+ plan: TideAgentOpenPlan | null
563
+ /** The runtime's own words when it printed no JSON. */
564
+ message?: string
565
+ }
566
+
567
+ export const AGENT_SESSION_KEY = /^[0-9a-f]{8}[0-9a-f-]{0,28}$/
568
+
569
+ /**
570
+ * Open one agent session by its id: the pane it runs in, or resumed in a tmux session of the
571
+ * name it last ran under (ironsilk ≥ 0.9.70, `tide agents open`). Dry run unless `run`.
572
+ * Exit 4 (refused) still prints the plan and comes back as an answer.
573
+ */
574
+ export async function openAgentSession(opts: AgentOpenOptions): Promise<TideAgentOpenResult> {
575
+ const session = opts.session.trim().toLowerCase()
576
+ if (!AGENT_SESSION_KEY.test(session)) {
577
+ throw new TideError(`session must be a session id or its first 8 characters or more: ${JSON.stringify(opts.session)}`)
578
+ }
579
+ const args = ["agents", "open", session, "--format", "json"]
580
+ if (opts.run) args.push("--run")
581
+ const bin = await resolveTideBin()
582
+ const timeoutMs = opts.timeoutMs ?? 60_000
583
+ const env = opts.run ? { ...process.env, TIDE_ALLOW_NON_INTERACTIVE_RUN: "1" } : process.env
584
+ const startedAt = Date.now()
585
+ const parse = (stdout: string): TideAgentOpenPlan | null => {
586
+ try {
587
+ const plan = JSON.parse(stdout) as TideAgentOpenPlan
588
+ return plan && typeof plan === "object" && typeof plan.state === "string" ? plan : null
589
+ } catch {
590
+ return null
591
+ }
592
+ }
593
+ try {
594
+ const res = await pExecFile(bin, args, { timeout: timeoutMs, env, maxBuffer: 1024 * 1024 })
595
+ return { exitCode: 0, plan: parse(res.stdout) }
596
+ } catch (err) {
597
+ const e = err as { code?: unknown; killed?: boolean; stdout?: string; stderr?: string }
598
+ if (typeof e.code === "number" && !e.killed) {
599
+ const plan = parse(e.stdout ?? "")
600
+ return plan
601
+ ? { exitCode: e.code, plan }
602
+ : { exitCode: e.code, plan: null, message: `${e.stdout ?? ""}${e.stderr ?? ""}`.trim() }
603
+ }
604
+ throw classifyExecFailure(bin, args, err, Date.now() - startedAt, timeoutMs)
605
+ }
606
+ }
607
+
526
608
  // ── Direct send-keys (simple, robust steer transport) ───────────────────────
527
609
  //
528
610
  // The operator cockpit's primary steer path. Instead of the heavier reviewed