@fungi.computer/parakeet 0.16.11 → 0.17.0

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.
@@ -16,6 +16,7 @@ import { memo, useCallback, useEffect, useId, useLayoutEffect, useRef, useState,
16
16
  import { Textarea } from "@fungi.computer/caps/components/textarea";
17
17
  import { formatHints } from "../commands/hints.js";
18
18
  import { awaitingTakeover } from "../core/awaiting-answer.js";
19
+ import { useAskedTakeover } from "../use-asked-takeover.js";
19
20
  import { completionFieldProps, completionKeyAction } from "./CompletionMenu.js";
20
21
  import { DecisionComposer } from "./DecisionComposer.js";
21
22
  import { SlashMenu } from "./SlashMenu.js";
@@ -323,7 +324,7 @@ export const ChatComposer = memo(function ChatComposer({ queuedRecall, onImagesC
323
324
  const fileInput = useRef(null);
324
325
  const { busy } = presentations[status];
325
326
  // A waiting decision takes the composer over an empty draft, or on request.
326
- const takeover = slash === undefined ? undefined : awaitingTakeover(slash.awaiting, draft);
327
+ const takeover = useAskedTakeover(slash === undefined ? undefined : awaitingTakeover(slash.awaiting, draft));
327
328
  const takeoverKey = takeover?.key;
328
329
  const seenTakeover = useRef(undefined);
329
330
  const hadFocus = useRef(false);
@@ -200,4 +200,9 @@ export declare function askApproval(interaction: WhistleInteraction, input: Read
200
200
  stillCurrent: () => boolean;
201
201
  /** An error reached the person as a refusal; report its cause. */
202
202
  onFailure: (cause: unknown) => void;
203
+ /**
204
+ * A prompt could not be shown at all, so the request was denied for the
205
+ * person: the one message to show them, in plain words.
206
+ */
207
+ onUnshowable?: ((message: string) => void) | undefined;
203
208
  }>): Promise<ApprovalOutcome>;
@@ -1,3 +1,4 @@
1
+ import { interactionTitleLimit } from "@fungi.computer/whistle/wire";
1
2
  const shorten = (value, width) => {
2
3
  if (value.length <= width)
3
4
  return value;
@@ -46,13 +47,31 @@ function appApprovalTitle(app) {
46
47
  }
47
48
  function connectedToolCard(view, now) {
48
49
  const { record } = view;
50
+ const app = record.description.app;
51
+ const wanted = app === undefined
52
+ ? `Allow ${record.module} · ${record.operation}?`
53
+ : appApprovalTitle(app);
54
+ // An App's own words can run past a title. Then the title stays short and the
55
+ // whole action moves to a fact, so a long description never costs the person
56
+ // the question: a prompt over the limit is refused and nothing is shown.
57
+ const fits = wanted.length <= interactionTitleLimit;
58
+ const subject = lead(app?.displayName ?? record.module, 28);
59
+ const title = fits
60
+ ? wanted
61
+ : lead(`Allow ${subject} to do this?`, interactionTitleLimit);
62
+ const action = app === undefined
63
+ ? `${record.module}.${record.operation}`
64
+ : app.operationLabel;
49
65
  return {
50
66
  content: {
51
- title: record.description.app === undefined
52
- ? `Allow ${record.module} · ${record.operation}?`
53
- : appApprovalTitle(record.description.app),
54
- summary: `${record.requester.displayName} wants to use a connected tool.`,
67
+ title,
68
+ summary: app === undefined
69
+ ? `${record.requester.displayName} wants to use a connected tool.`
70
+ : record.access === "write"
71
+ ? `${record.requester.displayName} wants to make a change in ${app.displayName}.`
72
+ : `${record.requester.displayName} wants to act outside Fungi through ${app.displayName}.`,
55
73
  facts: [
74
+ ...(fits ? [] : [{ label: "Action", value: action }]),
56
75
  {
57
76
  label: "Effect",
58
77
  value: (record.access === "write"
@@ -63,7 +82,13 @@ function connectedToolCard(view, now) {
63
82
  : ""),
64
83
  },
65
84
  ],
66
- details: entries([undoDetail(view), ...recordDetails(view, now)]),
85
+ // Nothing backs "can be undone" for an App write, so only a warning shows.
86
+ details: entries([
87
+ ...(record.reversibility === "irreversible-external"
88
+ ? [undoDetail(view)]
89
+ : []),
90
+ ...recordDetails(view, now),
91
+ ]),
67
92
  },
68
93
  };
69
94
  }
@@ -138,7 +163,13 @@ function escapeCard(view, review, width, home, now) {
138
163
  confirmation: !offered
139
164
  ? undefined
140
165
  : {
141
- title: `Always allow “${prefix}” in this project?`,
166
+ title: lead(`Always allow “${prefix}” in this project?`, interactionTitleLimit),
167
+ // A standing rule shows exactly what it allows: when the title has
168
+ // to cut the prefix, the whole prefix is a fact on the confirmation.
169
+ facts: `Always allow “${prefix}” in this project?`.length >
170
+ interactionTitleLimit
171
+ ? [{ label: "Command prefix", value: prefix }]
172
+ : undefined,
142
173
  body: lead(review.alwaysAllowCoverage ??
143
174
  `Any Workspace Agent here can then run \`${prefix} …\` without asking.`, Math.max(12, (width - 8) * 2 - 2)),
144
175
  },
@@ -155,7 +186,7 @@ function escapeCard(view, review, width, home, now) {
155
186
  */
156
187
  export function approvalCardCopy(view, review, options) {
157
188
  // A prompt's title is at most sixty characters.
158
- const width = Math.min(options.width, 60);
189
+ const width = Math.min(options.width, interactionTitleLimit);
159
190
  return review === undefined
160
191
  ? connectedToolCard(view, options.now)
161
192
  : escapeCard(view, review, width, options.home, options.now);
@@ -195,6 +226,10 @@ export function approvalRefusal(reason, decision) {
195
226
  return "Couldn't answer this request. Nothing ran. Try again.";
196
227
  }
197
228
  }
229
+ /** What the Agent reads when its approval could not be put in front of the person. */
230
+ const unshowableNote = "The approval request could not be shown to the member, so it was not approved. Nothing ran.";
231
+ /** What the person reads when their request could not be put in front of them. */
232
+ const unshowableToast = "Couldn't show this request, so it was denied. Nothing ran. Ask your Agent to try again.";
198
233
  const failureMessage = "Couldn't send your answer. It may not have been recorded.";
199
234
  /**
200
235
  * Ask the person about one approval through a conversation's decision
@@ -205,6 +240,9 @@ const failureMessage = "Couldn't send your answer. It may not have been recorded
205
240
  */
206
241
  export async function askApproval(interaction, input) {
207
242
  const { view, source } = input;
243
+ // True only while a prompt is being put in front of the person, so a prompt
244
+ // the interaction refuses is kept apart from every other failure.
245
+ let prompting = false;
208
246
  const card = approvalCardCopy(view, source.review, {
209
247
  width: input.width,
210
248
  home: input.home,
@@ -215,10 +253,14 @@ export async function askApproval(interaction, input) {
215
253
  let decision;
216
254
  let denialNote;
217
255
  let familyOn = false;
256
+ // A confirmation the interaction refuses is dropped, never answered for the person.
257
+ let alwaysRefused = false;
258
+ let familyRefused = false;
218
259
  while (decision === undefined) {
260
+ prompting = true;
219
261
  const answer = await interaction.prompt({
220
262
  type: "decision",
221
- message: card.content.title,
263
+ message: lead(card.content.title, interactionTitleLimit),
222
264
  summary: familyOn
223
265
  ? `Automatic approval is on for ${record.description.app?.displayName ?? "this tool"}.`
224
266
  : card.content.summary,
@@ -228,10 +270,10 @@ export async function askApproval(interaction, input) {
228
270
  details: card.content.details?.map((entry) => ({ ...entry })),
229
271
  options: [
230
272
  { value: "approve", label: "Approve once" },
231
- ...(card.always === undefined
273
+ ...(card.always === undefined || alwaysRefused
232
274
  ? []
233
275
  : [{ value: "always", label: card.always }]),
234
- ...(source.family === undefined || familyOn
276
+ ...(source.family === undefined || familyOn || familyRefused
235
277
  ? []
236
278
  : [{ value: "family", label: source.family.label }]),
237
279
  { value: "deny", label: "Deny" },
@@ -243,6 +285,7 @@ export async function askApproval(interaction, input) {
243
285
  blank: "deny",
244
286
  },
245
287
  });
288
+ prompting = false;
246
289
  if (!input.stillCurrent())
247
290
  return { kind: "abandoned" };
248
291
  if (answer.text !== undefined) {
@@ -251,17 +294,32 @@ export async function askApproval(interaction, input) {
251
294
  }
252
295
  else if (answer.selected[0] === "always") {
253
296
  const confirmation = card.confirmation;
254
- const confirmed = await interaction.prompt({
255
- type: "decision",
256
- message: confirmation?.title ?? card.content.title,
257
- summary: card.confirmWarning,
258
- body: confirmation?.body,
259
- options: [
260
- { value: "back", label: "Back" },
261
- { value: "always-allow", label: card.confirmYes ?? "Always allow" },
262
- ],
263
- escape: "back",
264
- });
297
+ let confirmed;
298
+ try {
299
+ confirmed = await interaction.prompt({
300
+ type: "decision",
301
+ message: lead(confirmation?.title ?? card.content.title, interactionTitleLimit),
302
+ summary: card.confirmWarning,
303
+ facts: confirmation?.facts?.map((fact) => ({ ...fact })),
304
+ body: confirmation?.body,
305
+ options: [
306
+ { value: "back", label: "Back" },
307
+ {
308
+ value: "always-allow",
309
+ label: card.confirmYes ?? "Always allow",
310
+ },
311
+ ],
312
+ escape: "back",
313
+ });
314
+ }
315
+ catch (cause) {
316
+ if (interaction.signal.aborted)
317
+ throw cause;
318
+ // The main question comes back without this option; nothing is denied.
319
+ input.onFailure(cause);
320
+ alwaysRefused = true;
321
+ continue;
322
+ }
265
323
  if (!input.stillCurrent())
266
324
  return { kind: "abandoned" };
267
325
  if (confirmed.selected[0] === "always-allow")
@@ -270,16 +328,26 @@ export async function askApproval(interaction, input) {
270
328
  else if (answer.selected[0] === "family" &&
271
329
  source.family !== undefined) {
272
330
  // A standing rule, like Always allow: it needs its own confirmation.
273
- const confirmed = await interaction.prompt({
274
- type: "decision",
275
- message: `${source.family.label}?`,
276
- summary: `All ${record.description.app?.displayName ?? "this tool"} requests that ${record.access === "write" ? "change data in Fungi" : "reach outside Fungi"} will run without asking, not just this action.`,
277
- options: [
278
- { value: "back", label: "Back" },
279
- { value: "family", label: source.family.label },
280
- ],
281
- escape: "back",
282
- });
331
+ let confirmed;
332
+ try {
333
+ confirmed = await interaction.prompt({
334
+ type: "decision",
335
+ message: lead(`${source.family.label}?`, interactionTitleLimit),
336
+ summary: `All ${record.description.app?.displayName ?? "this tool"} requests that ${record.access === "write" ? "change data in Fungi" : "reach outside Fungi"} will run without asking, not just this action.`,
337
+ options: [
338
+ { value: "back", label: "Back" },
339
+ { value: "family", label: source.family.label },
340
+ ],
341
+ escape: "back",
342
+ });
343
+ }
344
+ catch (cause) {
345
+ if (interaction.signal.aborted)
346
+ throw cause;
347
+ input.onFailure(cause);
348
+ familyRefused = true;
349
+ continue;
350
+ }
283
351
  if (!input.stillCurrent())
284
352
  return { kind: "abandoned" };
285
353
  if (confirmed.selected[0] === "family") {
@@ -311,6 +379,26 @@ export async function askApproval(interaction, input) {
311
379
  catch (cause) {
312
380
  if (interaction.signal.aborted)
313
381
  return { kind: "abandoned" };
382
+ if (prompting) {
383
+ // The question itself could not be shown, so nothing was ever asked and
384
+ // nothing can be approved: end the Agent's call now with a named reason
385
+ // instead of leaving it to its deadline. Reported once.
386
+ let reported = cause;
387
+ try {
388
+ await source.answer({
389
+ recordId: view.record.recordId,
390
+ decision: "deny",
391
+ expectedRevision: view.record.revision,
392
+ denialNote: unshowableNote,
393
+ });
394
+ }
395
+ catch (denyCause) {
396
+ reported = denyCause;
397
+ }
398
+ input.onFailure(reported);
399
+ input.onUnshowable?.(unshowableToast);
400
+ return { kind: "refused", message: failureMessage };
401
+ }
314
402
  input.onFailure(cause);
315
403
  return { kind: "refused", message: failureMessage };
316
404
  }
@@ -19,4 +19,6 @@ export declare function useApprovalAwaiting(options: Readonly<{
19
19
  noteLimit: number;
20
20
  /** An error that reached the person as a refusal. */
21
21
  onFailure: (cause: unknown) => void;
22
+ /** The one message when the request could not be shown and was denied. */
23
+ onUnshowable?: ((message: string) => void) | undefined;
22
24
  }>): void;
@@ -11,6 +11,8 @@ const noSubscription = () => () => undefined;
11
11
  */
12
12
  export function useApprovalAwaiting(options) {
13
13
  const { source, store, enabled, home, width, noteLimit, onFailure } = options;
14
+ const unshowable = useRef(options.onUnshowable);
15
+ unshowable.current = options.onUnshowable;
14
16
  // Only a different record, revision or right restarts the question: a
15
17
  // resize, a fresh callback or a reread of the same revision must not move
16
18
  // the person back to the first choice or reorder the queue.
@@ -62,6 +64,9 @@ export function useApprovalAwaiting(options) {
62
64
  onFailure: (cause) => {
63
65
  failure.current(cause);
64
66
  },
67
+ onUnshowable: (message) => {
68
+ unshowable.current?.(message);
69
+ },
65
70
  }).then((outcome) => {
66
71
  if (outcome.kind === "refused")
67
72
  // The refusal stays in the composer; the record rereads when it changes.
@@ -0,0 +1,9 @@
1
+ import type { AwaitingItem } from "./core/awaiting-answer.js";
2
+ /**
3
+ * The waiting item the composer may be taken over for: one that has asked its
4
+ * question. A queued item whose conversation has not asked anything has
5
+ * nothing to draw, and taking the composer over for it leaves a blank bar and
6
+ * no way to type. Once an item has asked, it keeps the composer through its
7
+ * answer, whose card shows the sending state.
8
+ */
9
+ export declare function useAskedTakeover(candidate: AwaitingItem | undefined): AwaitingItem | undefined;
@@ -0,0 +1,25 @@
1
+ import { useRef, useSyncExternalStore } from "react";
2
+ const noSubscription = () => () => undefined;
3
+ const noSnapshot = () => undefined;
4
+ /**
5
+ * The waiting item the composer may be taken over for: one that has asked its
6
+ * question. A queued item whose conversation has not asked anything has
7
+ * nothing to draw, and taking the composer over for it leaves a blank bar and
8
+ * no way to type. Once an item has asked, it keeps the composer through its
9
+ * answer, whose card shows the sending state.
10
+ */
11
+ export function useAskedTakeover(candidate) {
12
+ const snapshot = useSyncExternalStore(candidate?.host.subscribe ?? noSubscription, candidate === undefined ? noSnapshot : candidate.host.snapshot);
13
+ const asked = useRef(new Set());
14
+ if (candidate === undefined) {
15
+ asked.current.clear();
16
+ return undefined;
17
+ }
18
+ // Only the item in front is remembered; one that left the queue is forgotten.
19
+ for (const key of asked.current)
20
+ if (key !== candidate.key)
21
+ asked.current.delete(key);
22
+ if (snapshot?.prompt !== undefined)
23
+ asked.current.add(candidate.key);
24
+ return asked.current.has(candidate.key) ? candidate : undefined;
25
+ }
@@ -1910,10 +1910,10 @@ an answer is only sent while the record is still the one that was shown.
1910
1910
 
1911
1911
  #### Parameters
1912
1912
 
1913
- | Parameter | Type |
1914
- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1915
- | `interaction` | `WhistleInteraction` |
1916
- | `input` | `Readonly`\<\{ `view`: [`ApprovalFacts`](#approvalfacts); `source`: [`ApprovalSource`](#approvalsource); `home`: `string`; `width`: `number`; `now`: `number`; `noteLimit`: `number`; `stillCurrent`: () => `boolean`; `onFailure`: (`cause`) => `void`; \}\> |
1913
+ | Parameter | Type |
1914
+ | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1915
+ | `interaction` | `WhistleInteraction` |
1916
+ | `input` | `Readonly`\<\{ `view`: [`ApprovalFacts`](#approvalfacts); `source`: [`ApprovalSource`](#approvalsource); `home`: `string`; `width`: `number`; `now`: `number`; `noteLimit`: `number`; `stillCurrent`: () => `boolean`; `onFailure`: (`cause`) => `void`; `onUnshowable?`: (`message`) => `void`; \}\> |
1917
1917
 
1918
1918
  #### Returns
1919
1919
 
@@ -3344,9 +3344,9 @@ postpones it: the record stays pending in the ledger.
3344
3344
 
3345
3345
  #### Parameters
3346
3346
 
3347
- | Parameter | Type |
3348
- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
3349
- | `options` | `Readonly`\<\{ `source`: [`ApprovalSource`](#approvalsource) \| `undefined`; `store`: [`ParakeetInteractions`](#parakeetinteractions); `enabled`: `boolean`; `home`: `string`; `width`: `number`; `noteLimit`: `number`; `onFailure`: (`cause`) => `void`; \}\> |
3347
+ | Parameter | Type |
3348
+ | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
3349
+ | `options` | `Readonly`\<\{ `source`: [`ApprovalSource`](#approvalsource) \| `undefined`; `store`: [`ParakeetInteractions`](#parakeetinteractions); `enabled`: `boolean`; `home`: `string`; `width`: `number`; `noteLimit`: `number`; `onFailure`: (`cause`) => `void`; `onUnshowable?`: (`message`) => `void`; \}\> |
3350
3350
 
3351
3351
  #### Returns
3352
3352
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/parakeet",
3
- "version": "0.16.11",
3
+ "version": "0.17.0",
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.25.1",
71
+ "@fungi.computer/agar": "0.27.0",
72
72
  "@fungi.computer/bird-dog": "0.3.8",
73
73
  "@fungi.computer/cairn": "0.1.0",
74
- "@fungi.computer/caps": "0.12.7",
74
+ "@fungi.computer/caps": "0.12.8",
75
75
  "@fungi.computer/gills": "0.4.0",
76
- "@fungi.computer/knapsack": "0.4.24",
76
+ "@fungi.computer/knapsack": "0.4.26",
77
77
  "@fungi.computer/mule": "0.3.0",
78
- "@fungi.computer/shiitake": "0.22.0",
78
+ "@fungi.computer/shiitake": "0.22.2",
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",