ohbaby-cli 0.1.1 → 0.1.3

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/dist/bin.js CHANGED
@@ -69,8 +69,8 @@ function createRunCommand(runtime) {
69
69
  // src/cli/commands/serve.ts
70
70
  function normalizePort(port, runtime) {
71
71
  const value = port ?? 4096;
72
- if (!Number.isInteger(value) || value <= 0 || value > 65535) {
73
- runtime.failUsage("--port must be a TCP port between 1 and 65535");
72
+ if (!Number.isInteger(value) || value < 0 || value > 65535) {
73
+ runtime.failUsage("--port must be a TCP port between 0 and 65535");
74
74
  }
75
75
  return value;
76
76
  }
@@ -217,6 +217,7 @@ function createTerminalCommand(runtime) {
217
217
  await host.core.getSnapshot();
218
218
  }
219
219
  const instance = runtime.renderTerminalUi({
220
+ clearOnStart: resume === void 0 && args.continue !== true,
220
221
  client: host.core,
221
222
  subscribeEvents: (handler) => host.callbacks.subscribeEvents(handler)
222
223
  });
@@ -3915,7 +3916,6 @@ function applyTuiEvent(state, event) {
3915
3916
  );
3916
3917
  case "session.updated":
3917
3918
  return rebuildFromCollections(state, {
3918
- activeSessionId: state.activeSessionId ?? event.session.id,
3919
3919
  sessions: upsertById(state.sessions, event.session)
3920
3920
  });
3921
3921
  case "message.appended": {
@@ -4045,7 +4045,13 @@ function applyTuiEvent(state, event) {
4045
4045
  );
4046
4046
  }
4047
4047
  case "command.result.delivered": {
4048
- const next = clearCommandRuntime(state, event.commandRunId);
4048
+ const selectedSessionId = selectedSessionIdFromCommandAction(event.action);
4049
+ const next = selectedSessionId === void 0 ? clearCommandRuntime(state, event.commandRunId) : clearCommandRuntime(
4050
+ rebuildFromCollections(state, {
4051
+ activeSessionId: selectedSessionId
4052
+ }),
4053
+ event.commandRunId
4054
+ );
4049
4055
  if (!event.output || !shouldDisplayCommandOutput(event.output)) {
4050
4056
  return next;
4051
4057
  }
@@ -4099,6 +4105,13 @@ function applyTuiEvent(state, event) {
4099
4105
  }
4100
4106
  return state;
4101
4107
  }
4108
+ function selectedSessionIdFromCommandAction(action) {
4109
+ if (action?.kind !== "session.selected" || !isRecord3(action.data)) {
4110
+ return void 0;
4111
+ }
4112
+ const choiceId = action.data.choiceId;
4113
+ return typeof choiceId === "string" && choiceId.length > 0 ? choiceId : void 0;
4114
+ }
4102
4115
  function setCommandCatalog(state, catalog) {
4103
4116
  return {
4104
4117
  ...state,
@@ -4878,6 +4891,7 @@ var NEW_SESSION_CLEAR_SEQUENCE = "\x1B[2J\x1B[3J\x1B[H";
4878
4891
  var ESC_INTERRUPT_WINDOW_MS = 1500;
4879
4892
  var ESC_INTERRUPT_HINT = "Press Esc again to interrupt";
4880
4893
  function OhbabyTerminalApp({
4894
+ clearOnStart = false,
4881
4895
  client,
4882
4896
  subscribeEvents
4883
4897
  }) {
@@ -4886,6 +4900,8 @@ function OhbabyTerminalApp({
4886
4900
  const catalogRequestSequenceRef = useRef(0);
4887
4901
  const contextRefreshSequenceRef = useRef(0);
4888
4902
  const contextNoticeSequenceRef = useRef(0);
4903
+ const snapshotRefreshSequenceRef = useRef(0);
4904
+ const didClearOnStartRef = useRef(false);
4889
4905
  const disposedRef = useRef(false);
4890
4906
  const [screenGeneration, setScreenGeneration] = useState(0);
4891
4907
  const [commandPanel, setCommandPanel] = useState(
@@ -4897,6 +4913,10 @@ function OhbabyTerminalApp({
4897
4913
  const store = storeRef.current;
4898
4914
  const { exit } = useApp();
4899
4915
  const { write: writeStdout } = useStdout();
4916
+ if (clearOnStart && !didClearOnStartRef.current) {
4917
+ writeStdout(NEW_SESSION_CLEAR_SEQUENCE);
4918
+ didClearOnStartRef.current = true;
4919
+ }
4900
4920
  const activeSessionId = useTuiStoreSelector(
4901
4921
  store,
4902
4922
  (state) => state.activeSessionId
@@ -5179,11 +5199,24 @@ function OhbabyTerminalApp({
5179
5199
  return;
5180
5200
  }
5181
5201
  eventDispatcher.dispatch(tuiEvent);
5182
- if (isNewSessionSelectionEvent(tuiEvent)) {
5202
+ const isNewSessionSelection = isNewSessionSelectionEvent(tuiEvent);
5203
+ if (isNewSessionSelection) {
5204
+ snapshotRefreshSequenceRef.current += 1;
5183
5205
  writeStdout(NEW_SESSION_CLEAR_SEQUENCE);
5184
5206
  setScreenGeneration((current) => current + 1);
5185
5207
  setActiveCommandPanel(null);
5186
5208
  }
5209
+ const selectedExistingSessionId = selectedExistingSessionIdFromEvent(tuiEvent);
5210
+ if (selectedExistingSessionId !== void 0) {
5211
+ const requestSequence2 = snapshotRefreshSequenceRef.current + 1;
5212
+ snapshotRefreshSequenceRef.current = requestSequence2;
5213
+ void client.getSnapshot().then((snapshot) => {
5214
+ if (disposedRef.current || requestSequence2 !== snapshotRefreshSequenceRef.current || snapshot.activeSessionId !== selectedExistingSessionId) {
5215
+ return;
5216
+ }
5217
+ store.replaceSnapshot(snapshot);
5218
+ }).catch(() => void 0);
5219
+ }
5187
5220
  if (tuiEvent.type === "command.result.delivered" && tuiEvent.action?.kind === "app.exit") {
5188
5221
  exit();
5189
5222
  }
@@ -5191,12 +5224,14 @@ function OhbabyTerminalApp({
5191
5224
  void loadCatalog().catch(() => void 0);
5192
5225
  }
5193
5226
  });
5227
+ const requestSequence = snapshotRefreshSequenceRef.current + 1;
5228
+ snapshotRefreshSequenceRef.current = requestSequence;
5194
5229
  void client.getSnapshot().then((snapshot) => {
5195
- if (!disposedRef.current) {
5230
+ if (!disposedRef.current && requestSequence === snapshotRefreshSequenceRef.current) {
5196
5231
  store.replaceSnapshot(snapshot);
5197
5232
  }
5198
5233
  }).catch((caught) => {
5199
- if (!disposedRef.current) {
5234
+ if (!disposedRef.current && requestSequence === snapshotRefreshSequenceRef.current) {
5200
5235
  store.dispatch({
5201
5236
  status: {
5202
5237
  kind: "error",
@@ -5377,6 +5412,17 @@ function isNewSessionSelectionEvent(tuiEvent) {
5377
5412
  const data = tuiEvent.action.data;
5378
5413
  return isStringRecord(data) && data.source === "new";
5379
5414
  }
5415
+ function selectedExistingSessionIdFromEvent(tuiEvent) {
5416
+ if (tuiEvent.type !== "command.result.delivered" || tuiEvent.action?.kind !== "session.selected") {
5417
+ return void 0;
5418
+ }
5419
+ const data = tuiEvent.action.data;
5420
+ if (!isStringRecord(data) || data.source === "new") {
5421
+ return void 0;
5422
+ }
5423
+ const choiceId = data.choiceId;
5424
+ return typeof choiceId === "string" && choiceId.length > 0 ? choiceId : void 0;
5425
+ }
5380
5426
  function isStringRecord(value) {
5381
5427
  return typeof value === "object" && value !== null && !Array.isArray(value);
5382
5428
  }
@@ -5535,6 +5581,7 @@ function renderTerminalUi(options) {
5535
5581
  /* @__PURE__ */ jsx(
5536
5582
  OhbabyTerminalApp,
5537
5583
  {
5584
+ clearOnStart: options.clearOnStart,
5538
5585
  client: options.client,
5539
5586
  subscribeEvents: options.subscribeEvents
5540
5587
  }