@fungi.computer/parakeet 0.16.11 → 0.16.12

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"
@@ -138,7 +157,13 @@ function escapeCard(view, review, width, home, now) {
138
157
  confirmation: !offered
139
158
  ? undefined
140
159
  : {
141
- title: `Always allow “${prefix}” in this project?`,
160
+ title: lead(`Always allow “${prefix}” in this project?`, interactionTitleLimit),
161
+ // A standing rule shows exactly what it allows: when the title has
162
+ // to cut the prefix, the whole prefix is a fact on the confirmation.
163
+ facts: `Always allow “${prefix}” in this project?`.length >
164
+ interactionTitleLimit
165
+ ? [{ label: "Command prefix", value: prefix }]
166
+ : undefined,
142
167
  body: lead(review.alwaysAllowCoverage ??
143
168
  `Any Workspace Agent here can then run \`${prefix} …\` without asking.`, Math.max(12, (width - 8) * 2 - 2)),
144
169
  },
@@ -155,7 +180,7 @@ function escapeCard(view, review, width, home, now) {
155
180
  */
156
181
  export function approvalCardCopy(view, review, options) {
157
182
  // A prompt's title is at most sixty characters.
158
- const width = Math.min(options.width, 60);
183
+ const width = Math.min(options.width, interactionTitleLimit);
159
184
  return review === undefined
160
185
  ? connectedToolCard(view, options.now)
161
186
  : escapeCard(view, review, width, options.home, options.now);
@@ -195,6 +220,10 @@ export function approvalRefusal(reason, decision) {
195
220
  return "Couldn't answer this request. Nothing ran. Try again.";
196
221
  }
197
222
  }
223
+ /** What the Agent reads when its approval could not be put in front of the person. */
224
+ const unshowableNote = "The approval request could not be shown to the member, so it was not approved. Nothing ran.";
225
+ /** What the person reads when their request could not be put in front of them. */
226
+ const unshowableToast = "Couldn't show this request, so it was denied. Nothing ran. Ask your Agent to try again.";
198
227
  const failureMessage = "Couldn't send your answer. It may not have been recorded.";
199
228
  /**
200
229
  * Ask the person about one approval through a conversation's decision
@@ -205,6 +234,9 @@ const failureMessage = "Couldn't send your answer. It may not have been recorded
205
234
  */
206
235
  export async function askApproval(interaction, input) {
207
236
  const { view, source } = input;
237
+ // True only while a prompt is being put in front of the person, so a prompt
238
+ // the interaction refuses is kept apart from every other failure.
239
+ let prompting = false;
208
240
  const card = approvalCardCopy(view, source.review, {
209
241
  width: input.width,
210
242
  home: input.home,
@@ -215,10 +247,14 @@ export async function askApproval(interaction, input) {
215
247
  let decision;
216
248
  let denialNote;
217
249
  let familyOn = false;
250
+ // A confirmation the interaction refuses is dropped, never answered for the person.
251
+ let alwaysRefused = false;
252
+ let familyRefused = false;
218
253
  while (decision === undefined) {
254
+ prompting = true;
219
255
  const answer = await interaction.prompt({
220
256
  type: "decision",
221
- message: card.content.title,
257
+ message: lead(card.content.title, interactionTitleLimit),
222
258
  summary: familyOn
223
259
  ? `Automatic approval is on for ${record.description.app?.displayName ?? "this tool"}.`
224
260
  : card.content.summary,
@@ -228,10 +264,10 @@ export async function askApproval(interaction, input) {
228
264
  details: card.content.details?.map((entry) => ({ ...entry })),
229
265
  options: [
230
266
  { value: "approve", label: "Approve once" },
231
- ...(card.always === undefined
267
+ ...(card.always === undefined || alwaysRefused
232
268
  ? []
233
269
  : [{ value: "always", label: card.always }]),
234
- ...(source.family === undefined || familyOn
270
+ ...(source.family === undefined || familyOn || familyRefused
235
271
  ? []
236
272
  : [{ value: "family", label: source.family.label }]),
237
273
  { value: "deny", label: "Deny" },
@@ -243,6 +279,7 @@ export async function askApproval(interaction, input) {
243
279
  blank: "deny",
244
280
  },
245
281
  });
282
+ prompting = false;
246
283
  if (!input.stillCurrent())
247
284
  return { kind: "abandoned" };
248
285
  if (answer.text !== undefined) {
@@ -251,17 +288,32 @@ export async function askApproval(interaction, input) {
251
288
  }
252
289
  else if (answer.selected[0] === "always") {
253
290
  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
- });
291
+ let confirmed;
292
+ try {
293
+ confirmed = await interaction.prompt({
294
+ type: "decision",
295
+ message: lead(confirmation?.title ?? card.content.title, interactionTitleLimit),
296
+ summary: card.confirmWarning,
297
+ facts: confirmation?.facts?.map((fact) => ({ ...fact })),
298
+ body: confirmation?.body,
299
+ options: [
300
+ { value: "back", label: "Back" },
301
+ {
302
+ value: "always-allow",
303
+ label: card.confirmYes ?? "Always allow",
304
+ },
305
+ ],
306
+ escape: "back",
307
+ });
308
+ }
309
+ catch (cause) {
310
+ if (interaction.signal.aborted)
311
+ throw cause;
312
+ // The main question comes back without this option; nothing is denied.
313
+ input.onFailure(cause);
314
+ alwaysRefused = true;
315
+ continue;
316
+ }
265
317
  if (!input.stillCurrent())
266
318
  return { kind: "abandoned" };
267
319
  if (confirmed.selected[0] === "always-allow")
@@ -270,16 +322,26 @@ export async function askApproval(interaction, input) {
270
322
  else if (answer.selected[0] === "family" &&
271
323
  source.family !== undefined) {
272
324
  // 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
- });
325
+ let confirmed;
326
+ try {
327
+ confirmed = await interaction.prompt({
328
+ type: "decision",
329
+ message: lead(`${source.family.label}?`, interactionTitleLimit),
330
+ 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.`,
331
+ options: [
332
+ { value: "back", label: "Back" },
333
+ { value: "family", label: source.family.label },
334
+ ],
335
+ escape: "back",
336
+ });
337
+ }
338
+ catch (cause) {
339
+ if (interaction.signal.aborted)
340
+ throw cause;
341
+ input.onFailure(cause);
342
+ familyRefused = true;
343
+ continue;
344
+ }
283
345
  if (!input.stillCurrent())
284
346
  return { kind: "abandoned" };
285
347
  if (confirmed.selected[0] === "family") {
@@ -311,6 +373,26 @@ export async function askApproval(interaction, input) {
311
373
  catch (cause) {
312
374
  if (interaction.signal.aborted)
313
375
  return { kind: "abandoned" };
376
+ if (prompting) {
377
+ // The question itself could not be shown, so nothing was ever asked and
378
+ // nothing can be approved: end the Agent's call now with a named reason
379
+ // instead of leaving it to its deadline. Reported once.
380
+ let reported = cause;
381
+ try {
382
+ await source.answer({
383
+ recordId: view.record.recordId,
384
+ decision: "deny",
385
+ expectedRevision: view.record.revision,
386
+ denialNote: unshowableNote,
387
+ });
388
+ }
389
+ catch (denyCause) {
390
+ reported = denyCause;
391
+ }
392
+ input.onFailure(reported);
393
+ input.onUnshowable?.(unshowableToast);
394
+ return { kind: "refused", message: failureMessage };
395
+ }
314
396
  input.onFailure(cause);
315
397
  return { kind: "refused", message: failureMessage };
316
398
  }
@@ -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.16.12",
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.26.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.25",
77
77
  "@fungi.computer/mule": "0.3.0",
78
- "@fungi.computer/shiitake": "0.22.0",
78
+ "@fungi.computer/shiitake": "0.22.1",
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",