zebflow 0.11.6

Reactive web automation platform — pipelines, templates, and real-time collaboration in a single binary.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import { useState, useEffect, useRef, cx } from "zeb/react";
import { useWindowEvent } from "zeb/use";
import Button from "@/components/ui/button";
import Checkbox from "@/components/ui/checkbox";
import Toggle from "@/components/ui/toggle";
import DropdownMenuContent from "@/components/ui/dropdown-menu-content";
import { SessionIcon } from "@/pages/project-studio/components/icons";
import { useStudioChrome } from "@/pages/project-studio/components/studio-chrome-context";

const GUIDE_CLIENTS = [
  { id: "claude-code", label: "Claude Code" },
  { id: "cursor",      label: "Cursor" },
  { id: "codex",       label: "Codex" },
  { id: "gemini",      label: "Gemini" },
  { id: "opencode",    label: "OpenCode" },
  { id: "cline",       label: "Cline" },
];

function buildGuideSnippet(clientId, mcpUrl, token) {
  const url = mcpUrl ?? "<MCP_URL>";
  const tok = token ?? "<TOKEN>";
  const jsonBlock = `{\n  "mcpServers": {\n    "zebflow": {\n      "url": "${url}",\n      "headers": {\n        "Authorization": "Bearer ${tok}"\n      }\n    }\n  }\n}`;
  switch (clientId) {
    case "claude-code":
      return `claude mcp add --transport http zebflow \\\n  ${url} \\\n  --header "Authorization: Bearer ${tok}"`;
    case "cursor":
      return `// ~/.cursor/mcp.json\n${jsonBlock}`;
    case "codex":
      return `// ~/.codex/config.json\n${jsonBlock}`;
    case "gemini":
      return `// ~/.gemini/settings.json\n${jsonBlock}`;
    case "opencode":
      return `# ~/.config/opencode/config.toml\n[mcp.zebflow]\ntype   = "remote"\nurl    = "${url}"\n\n[mcp.zebflow.headers]\nAuthorization = "Bearer ${tok}"`;
    case "cline":
      return `// Cline → MCP Servers → Add Server (HTTP)\n// or in VS Code settings.json:\n${JSON.stringify({ "cline.mcpServers": { zebflow: { url, headers: { Authorization: `Bearer ${tok}` } } } }, null, 2)}`;
    default:
      return jsonBlock;
  }
}

// Only capabilities that are actually enforced by a real MCP tool
const DEFAULT_CAPABILITIES = [
  // Core
  { key: "project.read",       label: "Project Read",      defaultOn: true  },
  // Pipelines
  { key: "pipelines.read",     label: "Pipelines Read",    defaultOn: true  },
  { key: "pipelines.write",    label: "Pipelines Write",   defaultOn: false },
  { key: "pipelines.execute",  label: "Pipelines Execute", defaultOn: false },
  // Templates
  { key: "templates.read",     label: "Templates Read",    defaultOn: true  },
  { key: "templates.write",    label: "Templates Write",   defaultOn: false },
  { key: "templates.create",   label: "Templates Create",  defaultOn: false },
  { key: "templates.delete",   label: "Templates Delete",  defaultOn: false },
  // Files
  { key: "files.write",        label: "Files Write",       defaultOn: false },
  // Tables / DB
  { key: "tables.read",        label: "Tables Read",       defaultOn: true  },
  { key: "tables.write",       label: "Tables Write",      defaultOn: false },
  // Credentials
  { key: "credentials.read",   label: "Credentials Read",  defaultOn: false },
  { key: "credentials.write",  label: "Credentials Write", defaultOn: false },
  // Settings
  { key: "settings.read",      label: "Settings Read",     defaultOn: true  },
  { key: "settings.write",     label: "Settings Write",    defaultOn: false },
];

function formatSessionTime(value) {
  if (!value) return "Not generated";
  try {
    return new Date(value * 1000).toLocaleString();
  } catch (_) {
    return "Unknown";
  }
}

function formatAutoReset(value) {
  if (!value) return "Manual";
  const seconds = Number(value);
  if (!Number.isFinite(seconds) || seconds <= 0) return "Manual";
  if (seconds < 3600) return `${Math.round(seconds / 60)} min`;
  if (seconds < 86400) return `${Math.round(seconds / 3600)} hr`;
  return `${Math.round(seconds / 86400)} day`;
}

export function SessionPanel({ owner, project }) {
  const { activePanel, openHeaderPanel } = useStudioChrome();
  const [open, setOpen] = useState(false);
  const [loading, setLoading] = useState(false);
  const [activating, setActivating] = useState(false);
  const [enabled, setEnabled] = useState(false);
  const [token, setToken] = useState(null);
  const [mcpUrl, setMcpUrl] = useState(null);
  const [createdAt, setCreatedAt] = useState(null);
  const [autoResetSeconds, setAutoResetSeconds] = useState(null);
  const [rotationEpoch, setRotationEpoch] = useState(0);
  const [capabilities, setCapabilities] = useState(
    DEFAULT_CAPABILITIES.filter((c) => c.defaultOn).map((c) => c.key)
  );
  const [resetting, setResetting] = useState(false);
  const [copied, setCopied] = useState(false);
  const [urlCopied, setUrlCopied] = useState(false);
  const [error, setError] = useState("");
  const [guideClient, setGuideClient] = useState("claude-code");
  const [guideCopied, setGuideCopied] = useState(false);
  const [deactivating, setDeactivating] = useState(false);
  const sessionDetailsRef = useRef(null);

  const sessionUrl = `/api/projects/${owner}/${project}/mcp/session`;

  function applySession(s) {
    setEnabled(!!s?.enabled);
    setToken(s?.token ?? null);
    setMcpUrl(s?.mcp_url ?? null);
    setCreatedAt(s?.created_at ?? null);
    setAutoResetSeconds(s?.auto_reset_seconds ?? null);
    setRotationEpoch(s?.rotation_epoch ?? 0);
    if (s?.capabilities?.length) setCapabilities(s.capabilities);
  }

  async function fetchSession() {
    if (!owner || !project) return;
    setLoading(true);
    setError("");
    try {
      const res = await fetch(sessionUrl, { headers: { Accept: "application/json" } });
      const data = await res.json().catch(() => ({}));
      if (data?.ok && data?.session) {
        applySession(data.session);
      }
    } catch (_) {}
    setLoading(false);
  }

  useEffect(() => {
    fetchSession();
  }, []);

  useEffect(() => {
    if (open) fetchSession();
  }, [open]);

  useEffect(() => {
    const d = sessionDetailsRef.current;
    if (activePanel !== "session" && d?.open) {
      d.open = false;
      setOpen(false);
    }
  }, [activePanel]);

  useWindowEvent("rwe:nav", () => {
    const d = sessionDetailsRef.current;
    if (d?.open) {
      d.open = false;
      setOpen(false);
    }
  });

  async function handleToggle(e) {
    const next = e.target.checked;
    setError("");

    if (!token) {
      if (!next) return;
      setLoading(true);
      try {
        const res = await fetch(sessionUrl, {
          method: "POST",
          headers: { "Content-Type": "application/json", Accept: "application/json" },
          body: JSON.stringify({ capabilities }),
        });
        const data = await res.json().catch(() => ({}));
        if (data?.ok && data?.session) {
          applySession(data.session);
        } else {
          setError(data?.error?.message ?? "Failed to create session");
        }
      } catch (_) {
        setError("Network error");
      }
      setLoading(false);
    } else {
      setEnabled(next);
      try {
        const res = await fetch(sessionUrl, {
          method: next ? "POST" : "PUT",
          headers: { "Content-Type": "application/json", Accept: "application/json" },
          body: JSON.stringify(next ? { capabilities } : { enabled: false }),
        });
        const data = await res.json().catch(() => ({}));
        if (!res.ok || data?.ok === false) {
          setEnabled(!next);
          setError(data?.error?.message ?? "Failed to update session");
        } else if (data?.session) {
          applySession(data.session);
        }
      } catch (_) {
        setEnabled(!next);
        setError("Failed to update session");
      }
    }
  }

  async function handleDeactivate() {
    if (deactivating) return;
    setDeactivating(true);
    setError("");
    try {
      await fetch(sessionUrl, {
        method: "PUT",
        headers: { "Content-Type": "application/json", Accept: "application/json" },
        body: JSON.stringify({ enabled: false }),
      });
      setEnabled(false);
    } catch (_) {
      setError("Failed to deactivate session");
    }
    setDeactivating(false);
  }

  async function handleActivate() {
    if (activating) return;
    setActivating(true);
    setError("");
    try {
      const res = await fetch(sessionUrl, {
        method: "POST",
        headers: { "Content-Type": "application/json", Accept: "application/json" },
        body: JSON.stringify({ capabilities }),
      });
      const data = await res.json().catch(() => ({}));
      if (data?.ok && data?.session) {
        applySession(data.session);
      } else {
        setError(data?.error?.message ?? "Failed to activate session");
      }
    } catch (_) {
      setError("Failed to activate session");
    }
    setActivating(false);
  }

  async function handleCapabilityChange(key, checked) {
    const next = checked
      ? [...capabilities, key]
      : capabilities.filter((c) => c !== key);
    setCapabilities(next);

    if (token && enabled) {
      try {
        const res = await fetch(sessionUrl, {
          method: "POST",
          headers: { "Content-Type": "application/json", Accept: "application/json" },
          body: JSON.stringify({ capabilities: next }),
        });
        const data = await res.json().catch(() => ({}));
        if (!res.ok || data?.ok === false) {
          setError(data?.error?.message ?? "Failed to update permissions");
        } else if (data?.session) {
          applySession(data.session);
        }
      } catch (_) {
        setError("Failed to update permissions");
      }
    }
  }

  async function handleResetToken() {
    if (resetting) return;
    setResetting(true);
    setError("");
    try {
      const res = await fetch(`${sessionUrl}/reset-token`, {
        method: "POST",
        headers: { Accept: "application/json" },
      });
      const data = await res.json().catch(() => ({}));
      if (data?.ok && data?.session) {
        applySession(data.session);
      } else {
        setError(data?.error?.message ?? "Failed to reset token");
      }
    } catch (_) {
      setError("Network error");
    }
    setResetting(false);
  }

  async function handleCopyToken() {
    if (!token) return;
    try {
      await navigator.clipboard.writeText(token);
      setCopied(true);
      setTimeout(() => setCopied(false), 2000);
    } catch (_) {}
  }

  async function handleCopyUrl() {
    if (!mcpUrl) return;
    try {
      await navigator.clipboard.writeText(mcpUrl);
      setUrlCopied(true);
      setTimeout(() => setUrlCopied(false), 2000);
    } catch (_) {}
  }

  const labelCls = "text-[0.65rem] font-semibold tracking-widest text-muted-foreground uppercase";
  const sectionCls = "px-4 py-3 border-b border-border";
  const rotationLabel = rotationEpoch ? formatSessionTime(rotationEpoch) : "Off";

  return (
    <details ref={sessionDetailsRef} className="relative inline-block group" data-dropdown-menu="true">
      <summary
        className="list-none cursor-pointer outline-none relative flex h-9 items-center justify-center gap-1.5 rounded-none bg-[#ed752e] px-2.5 font-mono text-[0.68rem] font-semibold tracking-widest text-white hover:bg-[#f6863c]"
        onClick={() => {
          if (!open) openHeaderPanel("session");
          setOpen((o) => !o);
        }}
      >
        <SessionIcon className="h-4 w-4" />
        <span>MCP</span>
        {enabled && <span className="absolute bottom-0 right-0 w-[7px] h-[7px] bg-green-500 pointer-events-none" />}
      </summary>

      <DropdownMenuContent
        align="right"
        className="w-[760px] border-border bg-card"
      >
        {/* ── Header ── */}
        <div className="flex items-center justify-between gap-3 px-4 py-3 border-b border-border">
          <div className="min-w-0 flex-1">
            <div className="flex items-center gap-2">
              <p className="text-sm font-semibold text-foreground">MCP Session</p>
              <span className={cx(
                "inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[0.6rem] font-semibold tracking-wide",
                enabled
                  ? "bg-green-900/40 text-green-400 border border-green-800/60"
                  : "bg-accent text-muted-foreground border border-border"
              )}>
                <span className={cx("w-1.5 h-1.5 rounded-full", enabled ? "bg-green-500" : "bg-gray-500")} />
                {enabled ? "Active" : "Inactive"}
              </span>
            </div>
            <p className="text-[0.68rem] text-muted-foreground mt-0.5 leading-snug">
              Remote control for LLM agents (Cursor, Claude Code, etc.)
            </p>
          </div>
          <div className="flex items-center gap-2 shrink-0">
            {!enabled && (
              <Button
                variant="outline"
                size="xs"
                onClick={handleActivate}
                disabled={activating}
                className="text-green-400 border-green-800/50 hover:bg-green-900/20 hover:text-green-300"
              >
                {activating ? "Activating…" : "Activate"}
              </Button>
            )}
            {enabled && token && (
              <Button
                variant="outline"
                size="xs"
                onClick={handleDeactivate}
                disabled={deactivating}
                className="text-red-400 border-red-800/50 hover:bg-red-900/20 hover:text-red-300"
              >
                {deactivating ? "Deactivating…" : "Deactivate"}
              </Button>
            )}
            <Toggle
              checked={enabled}
              onChange={handleToggle}
              disabled={loading}
              aria-label="Enable MCP session"
            />
          </div>
        </div>

        {/* ── Split body ── */}
        <div className="flex items-stretch">

          {/* Left column */}
          <div className="flex-1 min-w-0 flex flex-col">

            {/* Token */}
            <div className={sectionCls}>
              <div className="flex items-center justify-between mb-1.5">
                <label htmlFor="mcp-token-input" className={labelCls}>Token</label>
                {token && (
                  <Button
                    variant="ghost"
                    size="xs"
                    onClick={handleResetToken}
                    disabled={resetting}
                    className="text-[0.68rem] text-muted-foreground hover:text-foreground"
                  >
                    {resetting ? "Resetting…" : "↺ Reset"}
                  </Button>
                )}
              </div>
              <div className="flex gap-1.5">
                <input
                  id="mcp-token-input"
                  readOnly
                  value={token ?? ""}
                  placeholder={loading ? "Loading…" : "Enable to generate token"}
                  title={token ?? undefined}
                  className="mcp-token-input flex-1"
                />
                <Button
                  variant={copied ? "default" : "outline"}
                  size="sm"
                  onClick={handleCopyToken}
                  disabled={!token}
                  className="shrink-0"
                  aria-label="Copy token to clipboard"
                >
                  {copied ? "✓" : "Copy"}
                </Button>
              </div>
              <div className="mt-2 grid grid-cols-3 gap-2 text-[0.64rem] text-muted-foreground">
                <div>
                  <span className="block uppercase tracking-widest">Created</span>
                  <span className="block truncate text-foreground">{formatSessionTime(createdAt)}</span>
                </div>
                <div>
                  <span className="block uppercase tracking-widest">Auto reset</span>
                  <span className="block truncate text-foreground">{formatAutoReset(autoResetSeconds)}</span>
                </div>
                <div>
                  <span className="block uppercase tracking-widest">Rotation epoch</span>
                  <span className="block truncate text-foreground">{rotationLabel}</span>
                </div>
              </div>
              {token && (
                <p className="mt-1.5 text-[0.65rem] leading-snug text-muted-foreground">
                  Reset invalidates existing MCP clients immediately.
                </p>
              )}
            </div>

            {/* MCP URL */}
            <div className={sectionCls}>
              <label htmlFor="mcp-url-input" className={cx(labelCls, "block mb-1.5")}>MCP URL</label>
              <div className="flex gap-1.5">
                <input
                  id="mcp-url-input"
                  readOnly
                  value={mcpUrl ?? ""}
                  title={mcpUrl ?? undefined}
                  placeholder="Enable session to get URL"
                  className="mcp-token-input flex-1"
                />
                <Button
                  variant={urlCopied ? "default" : "outline"}
                  size="sm"
                  onClick={handleCopyUrl}
                  disabled={!mcpUrl}
                  className="shrink-0"
                  aria-label="Copy MCP URL"
                >
                  {urlCopied ? "✓" : "URL"}
                </Button>
              </div>
            </div>

            {/* Install Guide */}
            <div className={cx(sectionCls, "border-b-0 flex-1")}>
              <p className={cx(labelCls, "mb-2")}>Install Guide</p>
              <div className="mcp-guide-tabs">
                {GUIDE_CLIENTS.map((c) => (
                  <button
                    key={c.id}
                    onClick={() => setGuideClient(c.id)}
                    className={cx("mcp-guide-tab", guideClient === c.id && "is-active")}
                  >{c.label}</button>
                ))}
              </div>
              <pre className="mcp-guide-snippet">{buildGuideSnippet(guideClient, mcpUrl, token)}</pre>
              <Button
                variant="outline"
                size="xs"
                className="mcp-guide-copy-btn"
                onClick={async () => {
                  try {
                    await navigator.clipboard.writeText(buildGuideSnippet(guideClient, mcpUrl, token));
                    setGuideCopied(true);
                    setTimeout(() => setGuideCopied(false), 2000);
                  } catch (_) {}
                }}
              >{guideCopied ? "✓ Copied" : "Copy"}</Button>
            </div>

          </div>{/* /left */}

          {/* Divider */}
          <div className="w-px bg-border flex-shrink-0" />

          {/* Right column: Permissions */}
          <div className="w-[340px] flex-shrink-0 p-3 flex flex-col">
            <p className={cx(labelCls, "mb-2.5")}>Permissions</p>
            <div className="grid grid-cols-2 gap-x-2 gap-y-0.5 overflow-y-auto max-h-[380px]">
              {DEFAULT_CAPABILITIES.map((cap) => (
                <Checkbox
                  key={cap.key}
                  label={cap.label}
                  checked={capabilities.includes(cap.key)}
                  onChange={(e) => handleCapabilityChange(cap.key, e.target.checked)}
                />
              ))}
            </div>
          </div>

        </div>{/* /split */}

        {/* Error */}
        {error && (
          <p className="px-4 pb-3 text-[0.68rem] text-red-400 border-t border-border" role="alert">{error}</p>
        )}
      </DropdownMenuContent>
    </details>
  );
}