@fungi.computer/parakeet 0.17.3 → 0.17.5

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.
@@ -1,6 +1,6 @@
1
1
  import type { WhistleInteractionEnd, WhistleRuntime } from "@fungi.computer/whistle";
2
2
  import { type WhistleInteractionHost } from "@fungi.computer/whistle/interaction";
3
- import type { AwaitingItem } from "../core/awaiting-answer.js";
3
+ import { DecisionDraft, type AwaitingItem } from "../core/awaiting-answer.js";
4
4
  /** One interaction a renderer should draw as a dialog or panel. */
5
5
  export type ParakeetOpenInteraction = Readonly<{
6
6
  /** Changes whenever a different interaction opens. */
@@ -95,6 +95,19 @@ export type ParakeetInteractions = Readonly<{
95
95
  /** Bring back the earliest postponed decision, even over a draft. */
96
96
  focus: () => void;
97
97
  }>;
98
+ /**
99
+ * Unfinished approval choices, in memory for this session only. They outlive
100
+ * the card that drew them, so a card that is hidden and drawn again resumes
101
+ * the person's choice instead of starting on the first row.
102
+ */
103
+ drafts: Readonly<{
104
+ /** The draft for one record revision, created on first use. */
105
+ claim: (key: string) => DecisionDraft;
106
+ /** Forget one draft: its record was answered or postponed for good. */
107
+ release: (key: string) => void;
108
+ /** Forget every draft whose key starts with the prefix but is not `keep`. */
109
+ supersede: (prefix: string, keep: string) => void;
110
+ }>;
98
111
  }>;
99
112
  /**
100
113
  * Own how commands talk to the human. An interaction opens only when it asks
@@ -1,4 +1,5 @@
1
1
  import { createWhistleInteractionHost, } from "@fungi.computer/whistle/interaction";
2
+ import { DecisionDraft } from "../core/awaiting-answer.js";
2
3
  /**
3
4
  * Own how commands talk to the human. An interaction opens only when it asks
4
5
  * a question, hands off to a URL or shows progress; a command that just
@@ -15,6 +16,7 @@ export function createParakeetInteractions() {
15
16
  };
16
17
  let sequence = 0;
17
18
  let lastEnd;
19
+ const drafts = new Map();
18
20
  const listeners = new Set();
19
21
  const toastListeners = new Set();
20
22
  const toast = (next) => {
@@ -168,6 +170,24 @@ export function createParakeetInteractions() {
168
170
  toastListeners.delete(listener);
169
171
  };
170
172
  },
173
+ drafts: {
174
+ claim: (key) => {
175
+ const held = drafts.get(key);
176
+ if (held !== undefined)
177
+ return held;
178
+ const created = new DecisionDraft();
179
+ drafts.set(key, created);
180
+ return created;
181
+ },
182
+ release: (key) => {
183
+ drafts.delete(key);
184
+ },
185
+ supersede: (prefix, keep) => {
186
+ for (const key of drafts.keys())
187
+ if (key !== keep && key.startsWith(prefix))
188
+ drafts.delete(key);
189
+ },
190
+ },
171
191
  attach: (present) => present(presenter),
172
192
  lastEnd: () => lastEnd,
173
193
  awaiting: {
@@ -124,6 +124,8 @@ export type DecisionOutcome = Readonly<{
124
124
  }>;
125
125
  /** The state a decision starts in. */
126
126
  export declare function initialDecisionState(): DecisionState;
127
+ /** The state of an approval: nothing selected, so Send and Enter wait for a pick. */
128
+ export declare function unselectedDecisionState(): DecisionState;
127
129
  /** One pending choice, retaining no display metadata or answering authority. */
128
130
  export declare class DecisionDraft {
129
131
  #private;
@@ -131,7 +133,11 @@ export declare class DecisionDraft {
131
133
  constructor();
132
134
  /** Remember option values, which policy rows may move, and unfinished text. */
133
135
  remember(state: DecisionState, prompt: DecisionPrompt): void;
134
- /** Redraw a compatible choice without replaying any submitted answer. */
136
+ /**
137
+ * Redraw a compatible choice without replaying any submitted answer. With
138
+ * nothing remembered, an approval has nothing selected: the person may have
139
+ * chosen something this draft lost, and Send must not guess for them.
140
+ */
135
141
  restore(prompt: DecisionPrompt): DecisionState;
136
142
  }
137
143
  /**
@@ -32,6 +32,10 @@ export function initialDecisionState() {
32
32
  details: false,
33
33
  };
34
34
  }
35
+ /** The state of an approval: nothing selected, so Send and Enter wait for a pick. */
36
+ export function unselectedDecisionState() {
37
+ return { ...initialDecisionState(), highlight: -1 };
38
+ }
35
39
  /** One pending choice, retaining no display metadata or answering authority. */
36
40
  export class DecisionDraft {
37
41
  #current;
@@ -56,9 +60,13 @@ export class DecisionDraft {
56
60
  text: state.other,
57
61
  };
58
62
  }
59
- /** Redraw a compatible choice without replaying any submitted answer. */
63
+ /**
64
+ * Redraw a compatible choice without replaying any submitted answer. With
65
+ * nothing remembered, an approval has nothing selected: the person may have
66
+ * chosen something this draft lost, and Send must not guess for them.
67
+ */
60
68
  restore(prompt) {
61
- const initial = initialDecisionState();
69
+ const initial = unselectedDecisionState();
62
70
  const draft = this.#current;
63
71
  if (draft === undefined)
64
72
  return initial;
@@ -1,7 +1,6 @@
1
1
  import { createWhistleInteractionHost } from "@fungi.computer/whistle/interaction";
2
2
  import { useEffect, useRef, useSyncExternalStore } from "react";
3
3
  import { askApproval } from "./core/approval-decision.js";
4
- import { DecisionDraft } from "./core/awaiting-answer.js";
5
4
  const noApproval = () => undefined;
6
5
  const noSubscription = () => () => undefined;
7
6
  /**
@@ -32,18 +31,30 @@ export function useApprovalAwaiting(options) {
32
31
  view.record.revision,
33
32
  view.canAnswer,
34
33
  ]);
35
- // One record owns its draft through a temporary loss of capability metadata.
36
- // A different record or revision starts a new choice; rights stay live in source.
37
- const draft = useRef();
38
- if (key !== undefined && draft.current?.key !== key)
39
- draft.current = { key, memory: new DecisionDraft() };
34
+ // One record revision owns its draft in the session's store, through any
35
+ // loss of capability metadata and any unmount of this hook. A different
36
+ // revision starts a new choice; rights stay live in source.
37
+ const draftOwner = view === undefined
38
+ ? undefined
39
+ : `${JSON.stringify(["approval", view.record.recordId]).slice(0, -1)},`;
40
+ const draftKey = view === undefined || draftOwner === undefined
41
+ ? undefined
42
+ : JSON.stringify([
43
+ "approval",
44
+ view.record.recordId,
45
+ view.record.revision,
46
+ ]);
40
47
  useEffect(() => {
41
48
  const view = shown.current;
42
49
  if (!enabled || source === undefined || view === undefined)
43
50
  return;
44
- const retained = draft.current;
45
- if (!view.canAnswer || key === undefined || retained?.key !== key)
51
+ if (!view.canAnswer ||
52
+ key === undefined ||
53
+ draftOwner === undefined ||
54
+ draftKey === undefined)
46
55
  return;
56
+ store.drafts.supersede(draftOwner, draftKey);
57
+ const retained = store.drafts.claim(draftKey);
47
58
  const { home, width, noteLimit } = layout.current;
48
59
  const host = createWhistleInteractionHost({ title: "Approval" });
49
60
  const session = host.begin();
@@ -59,7 +70,7 @@ export function useApprovalAwaiting(options) {
59
70
  kind: "approval",
60
71
  arrivedAt: Date.now(),
61
72
  host,
62
- draft: retained.memory,
73
+ draft: retained,
63
74
  });
64
75
  void askApproval(session.interaction, {
65
76
  view,
@@ -81,6 +92,8 @@ export function useApprovalAwaiting(options) {
81
92
  session.settle({ status: "failed", message: outcome.message });
82
93
  else {
83
94
  leave();
95
+ if (outcome.kind === "answered")
96
+ store.drafts.release(draftKey);
84
97
  session.settle(outcome.kind === "answered"
85
98
  ? { status: "handled" }
86
99
  : { status: "cancelled" });
@@ -90,5 +103,5 @@ export function useApprovalAwaiting(options) {
90
103
  leave();
91
104
  host.cancel();
92
105
  };
93
- }, [source, key, enabled, store]);
106
+ }, [source, key, draftOwner, draftKey, enabled, store]);
94
107
  }
@@ -1,5 +1,5 @@
1
1
  import { useRef, useState, useSyncExternalStore } from "react";
2
- import { decisionView, initialDecisionState, reduceDecision, } from "./core/awaiting-answer.js";
2
+ import { decisionView, initialDecisionState, unselectedDecisionState, reduceDecision, } from "./core/awaiting-answer.js";
3
3
  /** The state as the person sees it: the reducer's, over what the host reports. */
4
4
  function visibleState(held, snapshot) {
5
5
  if (snapshot.prompt !== undefined)
@@ -35,14 +35,18 @@ export function useDecision(item, context) {
35
35
  // Several keys can arrive before React renders; each must see the last one.
36
36
  const latest = useRef();
37
37
  const prompt = shown.current;
38
+ // An approval never preselects a choice, however it came to be drawn.
39
+ const start = () => item.kind === "approval"
40
+ ? unselectedDecisionState()
41
+ : initialDecisionState();
38
42
  const held = () => {
39
43
  if (prompt === undefined)
40
- return initialDecisionState();
44
+ return start();
41
45
  if (latest.current?.host !== host ||
42
46
  latest.current.sequence !== prompt.sequence) {
43
47
  const state = latest.current?.host === host
44
- ? initialDecisionState()
45
- : (item.draft?.restore(prompt.prompt) ?? initialDecisionState());
48
+ ? start()
49
+ : (item.draft?.restore(prompt.prompt) ?? start());
46
50
  latest.current = { host, sequence: prompt.sequence, state };
47
51
  }
48
52
  return latest.current.state;
@@ -76,8 +80,11 @@ export function useDecision(item, context) {
76
80
  return;
77
81
  }
78
82
  const outcome = reduceDecision(visibleState(held(), now), intent, prompt.prompt);
79
- remember(outcome.state);
80
83
  const effect = outcome.effect;
84
+ // Backing out of a second question returns to nothing selected.
85
+ remember(item.kind === "approval" && effect?.type === "back"
86
+ ? { ...outcome.state, highlight: -1 }
87
+ : outcome.state);
81
88
  if (effect?.type === "later")
82
89
  context.later();
83
90
  else if (effect?.type === "answer") {
@@ -55,7 +55,9 @@ Remember option values, which policy rows may move, and unfinished text.
55
55
  restore(prompt): DecisionState;
56
56
  ```
57
57
 
58
- Redraw a compatible choice without replaying any submitted answer.
58
+ Redraw a compatible choice without replaying any submitted answer. With nothing
59
+ remembered, an approval has nothing selected: the person may have chosen
60
+ something this draft lost, and Send must not guess for them.
59
61
 
60
62
  ###### Parameters
61
63
 
@@ -394,6 +396,11 @@ type ParakeetInteractions = Readonly<{
394
396
  later: (key) => void;
395
397
  focus: () => void;
396
398
  }>;
399
+ drafts: Readonly<{
400
+ claim: (key) => DecisionDraft;
401
+ release: (key) => void;
402
+ supersede: (prefix, keep) => void;
403
+ }>;
397
404
  }>;
398
405
  ```
399
406
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/parakeet",
3
- "version": "0.17.3",
3
+ "version": "0.17.5",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "private": false,
6
6
  "description": "Standalone React composer for a typed Shiitake Session",
@@ -68,14 +68,14 @@
68
68
  },
69
69
  "dependencies": {
70
70
  "@earendil-works/pi-ai": "1.5.2",
71
- "@fungi.computer/agar": "0.28.0",
71
+ "@fungi.computer/agar": "0.28.1",
72
72
  "@fungi.computer/bird-dog": "0.3.8",
73
73
  "@fungi.computer/cairn": "0.1.0",
74
74
  "@fungi.computer/caps": "0.13.0",
75
75
  "@fungi.computer/gills": "0.4.0",
76
76
  "@fungi.computer/knapsack": "0.4.27",
77
77
  "@fungi.computer/mule": "0.3.0",
78
- "@fungi.computer/shiitake": "0.22.5",
78
+ "@fungi.computer/shiitake": "0.22.7",
79
79
  "@fungi.computer/sigils": "0.3.2",
80
80
  "@fungi.computer/sprite": "0.2.2",
81
81
  "@fungi.computer/sprite-zdog": "0.3.2",