stitchkit-tui 0.3.0 → 0.4.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.
Files changed (42) hide show
  1. package/README.md +61 -16
  2. package/dist/{app-mqzzrp1t.js → app-2zw4ra7n.js} +36 -16
  3. package/dist/build/index.d.ts +42 -0
  4. package/dist/build/index.d.ts.map +1 -0
  5. package/dist/build/index.js +174 -0
  6. package/dist/cli.js +2 -2
  7. package/dist/config.d.ts +8 -0
  8. package/dist/config.d.ts.map +1 -1
  9. package/dist/controller.d.ts.map +1 -1
  10. package/dist/core/index.js +27 -25
  11. package/dist/core/labels.d.ts +4 -0
  12. package/dist/core/labels.d.ts.map +1 -1
  13. package/dist/core/table.d.ts +18 -1
  14. package/dist/core/table.d.ts.map +1 -1
  15. package/dist/gallery/main.js +1 -1
  16. package/dist/gallery/monitor-page.d.ts.map +1 -1
  17. package/dist/{index-kz62sdr9.js → index-1qacnvfd.js} +1 -1
  18. package/dist/index-51qwgt9f.js +172 -0
  19. package/dist/index-ahfc59de.js +495 -0
  20. package/dist/{index-ecpc5qkb.js → index-as4srnct.js} +1 -0
  21. package/dist/{index-z4c2ehg4.js → index-djrp2fey.js} +107 -583
  22. package/dist/index-m0dhvt6x.js +160 -0
  23. package/dist/{index-251fexwh.js → index-qhj2ne64.js} +1 -1
  24. package/dist/{index-fxx9ktw7.js → index-t6mwjbgr.js} +68 -58
  25. package/dist/{index-54sqbm3r.js → index-wcbe2tnk.js} +168 -285
  26. package/dist/index.js +8 -5
  27. package/dist/{run-e1kdpzna.js → run-ea636vch.js} +8 -5
  28. package/dist/run.d.ts.map +1 -1
  29. package/dist/session.d.ts.map +1 -1
  30. package/dist/testing/index.d.ts +50 -0
  31. package/dist/testing/index.d.ts.map +1 -0
  32. package/dist/testing/index.js +110 -0
  33. package/dist/widgets/app.d.ts +77 -0
  34. package/dist/widgets/app.d.ts.map +1 -0
  35. package/dist/widgets/form.d.ts.map +1 -1
  36. package/dist/widgets/index.d.ts +1 -0
  37. package/dist/widgets/index.d.ts.map +1 -1
  38. package/dist/widgets/index.js +20 -7
  39. package/dist/widgets/table.d.ts.map +1 -1
  40. package/dist/widgets/theme.d.ts +7 -1
  41. package/dist/widgets/theme.d.ts.map +1 -1
  42. package/package.json +12 -4
@@ -0,0 +1,160 @@
1
+ // @bun
2
+ import {
3
+ terminalPalettes2,
4
+ resolveTerminalPalette2,
5
+ terminalLabelsEn2
6
+ } from "./index-ahfc59de.js";
7
+
8
+ // src/widgets/context.tsx
9
+ import { createContext, useContext, useMemo } from "react";
10
+ import { jsx } from "@opentui/react/jsx-runtime";
11
+ var LabelsContext = createContext(terminalLabelsEn2);
12
+ function TerminalLabelsProvider2({
13
+ labels,
14
+ children
15
+ }) {
16
+ return /* @__PURE__ */ jsx(LabelsContext.Provider, {
17
+ value: labels,
18
+ children
19
+ });
20
+ }
21
+ function useTerminalLabels2() {
22
+ return useContext(LabelsContext);
23
+ }
24
+ var FocusContext = createContext(null);
25
+ function TerminalFocusProvider2({
26
+ state,
27
+ dispatch,
28
+ children
29
+ }) {
30
+ const value = useMemo(() => ({ state, dispatch }), [state, dispatch]);
31
+ return /* @__PURE__ */ jsx(FocusContext.Provider, {
32
+ value,
33
+ children
34
+ });
35
+ }
36
+ function useTerminalFocusContext2() {
37
+ return useContext(FocusContext);
38
+ }
39
+ function useTerminalFocused2(focusId, focused) {
40
+ const context = useContext(FocusContext);
41
+ if (focused !== undefined)
42
+ return focused;
43
+ if (focusId === undefined || context === null)
44
+ return true;
45
+ return context.state.focused === focusId;
46
+ }
47
+
48
+ // src/widgets/keyboard.ts
49
+ import { useRenderer } from "@opentui/react";
50
+ import { useEffect, useRef } from "react";
51
+ var TerminalKeyPriority2 = Object.freeze({
52
+ input: 300,
53
+ layer: 200,
54
+ widget: 100,
55
+ global: 0
56
+ });
57
+
58
+ class KeyRouter {
59
+ registrations = new Set;
60
+ next = 0;
61
+ constructor(renderer) {
62
+ renderer.keyInput.on("keypress", (key) => this.route(key));
63
+ }
64
+ register(priority, handler) {
65
+ const registration = { order: this.next, priority, handler };
66
+ this.next += 1;
67
+ this.registrations.add(registration);
68
+ return registration;
69
+ }
70
+ unregister(registration) {
71
+ this.registrations.delete(registration);
72
+ }
73
+ route(key) {
74
+ if (key.propagationStopped)
75
+ return;
76
+ const ordered = [...this.registrations].sort((a, b) => b.priority - a.priority || a.order - b.order);
77
+ for (const registration of ordered) {
78
+ if (registration.handler(key)) {
79
+ key.preventDefault();
80
+ key.stopPropagation();
81
+ return;
82
+ }
83
+ }
84
+ }
85
+ }
86
+ var routers = new WeakMap;
87
+ function routerFor(renderer) {
88
+ const existing = routers.get(renderer);
89
+ if (existing)
90
+ return existing;
91
+ const created = new KeyRouter(renderer);
92
+ routers.set(renderer, created);
93
+ return created;
94
+ }
95
+ function useTerminalKeyboard2(handler, options = {}) {
96
+ const renderer = useRenderer();
97
+ const priority = options.priority ?? TerminalKeyPriority2.widget;
98
+ const active = options.active ?? true;
99
+ const latest = useRef(handler);
100
+ latest.current = handler;
101
+ const live = useRef(active);
102
+ live.current = active;
103
+ useEffect(() => {
104
+ const router = routerFor(renderer);
105
+ const entry = router.register(priority, (key) => live.current && latest.current(key));
106
+ return () => router.unregister(entry);
107
+ }, [renderer, priority]);
108
+ }
109
+
110
+ // src/widgets/theme.tsx
111
+ import { TextAttributes } from "@opentui/core";
112
+ import { useRenderer as useRenderer2 } from "@opentui/react";
113
+ import {
114
+ createContext as createContext2,
115
+ useContext as useContext2,
116
+ useEffect as useEffect2,
117
+ useMemo as useMemo2,
118
+ useState
119
+ } from "react";
120
+ import { jsx as jsx2 } from "@opentui/react/jsx-runtime";
121
+ function terminalToneAttributes2(tone) {
122
+ if (tone === "danger" || tone === "warning")
123
+ return TextAttributes.BOLD;
124
+ if (tone === "muted")
125
+ return TextAttributes.DIM;
126
+ return TextAttributes.NONE;
127
+ }
128
+ var PaletteContext = createContext2(terminalPalettes2.dark);
129
+ function useTerminalPalette2() {
130
+ return useContext2(PaletteContext);
131
+ }
132
+ function useTerminalThemeMode2(mode) {
133
+ const renderer = useRenderer2();
134
+ const [reported, setReported] = useState(renderer.themeMode);
135
+ useEffect2(() => {
136
+ if (mode !== "auto")
137
+ return;
138
+ const update = (next) => setReported(next);
139
+ renderer.on("theme_mode", update);
140
+ setReported(renderer.themeMode);
141
+ return () => {
142
+ renderer.off("theme_mode", update);
143
+ };
144
+ }, [mode, renderer]);
145
+ return mode === "auto" ? reported ?? "dark" : mode;
146
+ }
147
+ function TerminalThemeProvider2({
148
+ mode = "auto",
149
+ palette,
150
+ children
151
+ }) {
152
+ const resolved = useTerminalThemeMode2(mode);
153
+ const value = useMemo2(() => resolveTerminalPalette2(resolved, palette), [resolved, palette]);
154
+ return /* @__PURE__ */ jsx2(PaletteContext.Provider, {
155
+ value,
156
+ children
157
+ });
158
+ }
159
+
160
+ export { TerminalLabelsProvider2, useTerminalLabels2, TerminalFocusProvider2, useTerminalFocusContext2, useTerminalFocused2, TerminalKeyPriority2, useTerminalKeyboard2, terminalToneAttributes2, useTerminalPalette2, useTerminalThemeMode2, TerminalThemeProvider2 };
@@ -1,7 +1,7 @@
1
1
  // @bun
2
2
  import {
3
3
  TerminalToneSchema2
4
- } from "./index-z4c2ehg4.js";
4
+ } from "./index-ahfc59de.js";
5
5
 
6
6
  // src/core/notices.ts
7
7
  import { z } from "zod";
@@ -7,16 +7,19 @@ import {
7
7
  moveTerminalCommandSelection2,
8
8
  createTerminalFeedViewport2,
9
9
  reduceTerminalFeedViewport2
10
- } from "./index-kz62sdr9.js";
10
+ } from "./index-1qacnvfd.js";
11
11
  import {
12
12
  createAgentTuiSessionId2,
13
13
  createAgentTuiConversationId2,
14
14
  listAgentTuiSessions2,
15
15
  startAgentTuiSessionHost2
16
- } from "./index-ecpc5qkb.js";
16
+ } from "./index-as4srnct.js";
17
17
  import {
18
- resolveTerminalPalette2
19
- } from "./index-z4c2ehg4.js";
18
+ useTerminalPalette2
19
+ } from "./index-m0dhvt6x.js";
20
+ import {
21
+ runTerminalApp2
22
+ } from "./index-51qwgt9f.js";
20
23
 
21
24
  // src/commands.ts
22
25
  import { z } from "zod";
@@ -207,6 +210,7 @@ async function createAgentTuiController2(input) {
207
210
  listener(current);
208
211
  };
209
212
  let refreshVersion = 0;
213
+ let switchVersion = 0;
210
214
  const refresh = async () => {
211
215
  const targetConversationId = conversationId;
212
216
  const version = ++refreshVersion;
@@ -275,8 +279,8 @@ async function createAgentTuiController2(input) {
275
279
  }
276
280
  });
277
281
  await refresh();
278
- const selected = async () => {
279
- const found = await input.selections.load(conversationId);
282
+ const selected = async (targetConversationId) => {
283
+ const found = await input.selections.load(targetConversationId);
280
284
  if (!found)
281
285
  throw new Error("Select a model before submitting");
282
286
  return found.modelId;
@@ -292,11 +296,15 @@ async function createAgentTuiController2(input) {
292
296
  const text = rawText.trim();
293
297
  if (!text)
294
298
  throw new Error("Agent message is empty");
295
- const modelId = await selected();
299
+ const targetConversationId = conversationId;
300
+ const [modelId, context] = await Promise.all([
301
+ selected(targetConversationId),
302
+ input.context()
303
+ ]);
296
304
  const ticket = input.harness.submit({
297
- conversationId,
305
+ conversationId: targetConversationId,
298
306
  idempotencyKey,
299
- context: await input.context(),
307
+ context,
300
308
  parts: [{ type: "text", text }],
301
309
  metadata: { modelId }
302
310
  });
@@ -319,18 +327,13 @@ async function createAgentTuiController2(input) {
319
327
  },
320
328
  async switchConversation(nextConversationId) {
321
329
  const previousModelId = current.selectedModelId;
322
- conversationId = nextConversationId;
323
- const version = ++refreshVersion;
330
+ const version = ++switchVersion;
331
+ ++refreshVersion;
324
332
  const snapshot = await input.harness.snapshot(nextConversationId);
325
- if (version !== refreshVersion || conversationId !== nextConversationId)
333
+ if (version !== switchVersion)
326
334
  return;
327
- current = {
328
- conversationId: nextConversationId,
329
- snapshot,
330
- streaming: {}
331
- };
332
335
  let nextSelection = await input.selections.load(nextConversationId);
333
- if (version !== refreshVersion || conversationId !== nextConversationId)
336
+ if (version !== switchVersion)
334
337
  return;
335
338
  if (!nextSelection) {
336
339
  const modelId = previousModelId ?? input.catalog.models[0]?.id;
@@ -338,23 +341,37 @@ async function createAgentTuiController2(input) {
338
341
  throw new Error("The Agent TUI model catalog is empty");
339
342
  nextSelection = { modelId, selectedAt: new Date().toISOString() };
340
343
  await input.selections.save(nextConversationId, nextSelection);
341
- if (version !== refreshVersion || conversationId !== nextConversationId)
344
+ if (version !== switchVersion)
342
345
  return;
343
346
  }
347
+ conversationId = nextConversationId;
348
+ ++refreshVersion;
349
+ current = {
350
+ conversationId,
351
+ snapshot,
352
+ streaming: {},
353
+ selectedModelId: nextSelection.modelId
354
+ };
344
355
  await refresh();
345
356
  },
346
357
  async respondToApproval(approved, reason) {
347
358
  const approval = current.pendingApproval;
348
359
  if (!approval)
349
360
  throw new Error("No approval is pending");
361
+ const targetConversationId = approval.conversationId;
362
+ const pinned = pinnedModel(current.snapshot, approval.runId);
363
+ const [modelId, context] = await Promise.all([
364
+ pinned ?? selected(targetConversationId),
365
+ input.context()
366
+ ]);
350
367
  const ticket = await input.harness.respondToApproval({
351
- conversationId,
368
+ conversationId: targetConversationId,
352
369
  approvalId: approval.approvalId,
353
370
  approved,
354
371
  ...reason && { reason },
355
- context: await input.context(),
372
+ context,
356
373
  metadata: {
357
- modelId: pinnedModel(current.snapshot, approval.runId) ?? await selected()
374
+ modelId
358
375
  }
359
376
  });
360
377
  const admission = await ticket.admission;
@@ -537,8 +554,6 @@ function searchAgentTuiModels2(catalog, query) {
537
554
 
538
555
  // src/run.tsx
539
556
  import path4 from "path";
540
- import { createCliRenderer } from "@opentui/core";
541
- import { createRoot } from "@opentui/react";
542
557
 
543
558
  // src/App.tsx
544
559
  import { useKeyboard, useTerminalDimensions } from "@opentui/react";
@@ -1444,6 +1459,12 @@ function AgentTuiApp({
1444
1459
 
1445
1460
  // src/run.tsx
1446
1461
  import { jsx as jsx2 } from "@opentui/react/jsx-runtime";
1462
+ function ThemedAgentTuiApp(props) {
1463
+ return /* @__PURE__ */ jsx2(AgentTuiApp, {
1464
+ ...props,
1465
+ theme: useTerminalPalette2()
1466
+ });
1467
+ }
1447
1468
  function runtimeContext(config) {
1448
1469
  return Promise.resolve(config.context());
1449
1470
  }
@@ -1540,41 +1561,26 @@ async function runAgentTui2(config) {
1540
1561
  conversationId: initialConversationId,
1541
1562
  launchMode: config.initialConversationId === undefined ? "fresh" : "explicit"
1542
1563
  });
1543
- const renderer = await createCliRenderer({ exitOnCtrlC: false });
1544
- const themeMode = config.themeMode === undefined || config.themeMode === "auto" ? await renderer.waitForThemeMode(250) ?? "dark" : config.themeMode;
1545
- const root = createRoot(renderer);
1546
- let closing;
1547
- const onSignal = () => void close();
1548
- const close = () => {
1549
- if (closing)
1550
- return closing;
1551
- process.off("SIGINT", onSignal);
1552
- process.off("SIGTERM", onSignal);
1553
- renderer.destroy();
1554
- closing = (async () => {
1555
- try {
1556
- diagnostics.record({
1557
- schemaVersion: 1,
1558
- occurredAt: new Date().toISOString(),
1559
- type: "host-closed",
1560
- sessionId,
1561
- conversationId: controller.state().conversationId
1562
- });
1563
- await host.close();
1564
- await controller.close();
1565
- } finally {
1566
- await diagnostics.close();
1567
- renderer.destroy();
1568
- }
1569
- })();
1570
- return closing;
1564
+ const closeHost = async () => {
1565
+ try {
1566
+ diagnostics.record({
1567
+ schemaVersion: 1,
1568
+ occurredAt: new Date().toISOString(),
1569
+ type: "host-closed",
1570
+ sessionId,
1571
+ conversationId: controller.state().conversationId
1572
+ });
1573
+ await host.close();
1574
+ await controller.close();
1575
+ } finally {
1576
+ await diagnostics.close();
1577
+ }
1571
1578
  };
1572
- root.render(/* @__PURE__ */ jsx2(AgentTuiApp, {
1579
+ const app = await runTerminalApp2(({ close }) => /* @__PURE__ */ jsx2(ThemedAgentTuiApp, {
1573
1580
  controller,
1574
1581
  catalog,
1575
1582
  ...bundle.conversations && { conversations: bundle.conversations },
1576
1583
  ...config.commands && { commands: config.commands },
1577
- theme: resolveTerminalPalette2(themeMode, config.theme),
1578
1584
  workspace,
1579
1585
  ...config.statusLine !== undefined && { statusLine: config.statusLine },
1580
1586
  sessionId: host.sessionId,
@@ -1594,10 +1600,14 @@ async function runAgentTui2(config) {
1594
1600
  });
1595
1601
  },
1596
1602
  onExit: close
1597
- }));
1598
- process.once("SIGINT", onSignal);
1599
- process.once("SIGTERM", onSignal);
1600
- return { sessionId: host.sessionId, close };
1603
+ }), {
1604
+ themeMode: config.themeMode ?? "auto",
1605
+ ...config.theme && { palette: config.theme },
1606
+ onClose: closeHost,
1607
+ ...config.exitSignals && { exitSignals: config.exitSignals },
1608
+ closeTimeoutMs: config.closeTimeoutMs ?? 5000
1609
+ });
1610
+ return { sessionId: host.sessionId, close: app.close };
1601
1611
  }
1602
1612
 
1603
1613
  export { AgentTuiCommandOutcomeSchema2, defineTuiCommand2, composeTuiCommands2, resolveTuiCommand2, commandCompletions2, selectedCommandCompletion2, moveCommandCompletionSelection2, resolveTuiCommandSubmission2, AgentTuiComposerSchema2, createAgentTuiComposer2, setAgentTuiDraft2, submitAgentTuiComposer2, navigateAgentTuiHistory2, defineAgentTui2, createFileAgentModelSelectionStore2, createAgentTuiController2, AgentTuiDiagnosticEventSchema2, projectAgentTuiRuntimeDiagnostic2, createAgentTuiDiagnosticRecorder2, isAgentTuiExitKey2, AGENT_TUI_MODEL_RESULT_LIMIT2, searchAgentTuiModels2, defaultAgentTuiStatusLine2, projectAgentTuiTranscript2, defaultAgentTuiRecoveryDecision2, resolveAgentTuiInitialConversationId2, runAgentTui2 };