Add the headless Ask for suggestions send and its server flag

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-04 12:09:43 -08:00
1 parent eb235dd91e
commit f8a0a6bfc7
8 files changed
+264 -16

No files matched your search

+55
View File
@@ -1022,6 +1022,61 @@ export function noteReplyLines(title, text, attachments) {
];
}
// A request for suggestions on a card: a note's action flagged `suggest`, so
// the server records it as one and the page can tell it from a plain note.
export function suggestAction(id, text, attachments) {
return { ...noteAction(id, text, attachments), suggest: true };
}
// The same request as `reply` lines. Its lead differs from `Note on` on
// purpose: that is how the agent tells a request for suggestions from a note.
export function suggestReplyLines(title, text, attachments) {
const list = attachments || [];
return [
`Suggestions wanted on ${title}: ${indentReply(text || "(no note)")}`,
...list.map((a) => ` ${attachmentKind(a) === "file" ? "File" : "Image"}: ${a.path} (${a.name})`),
];
}
/* --------------------------------------------------------- stale picks */
/**
* Staged picks that a re-posted card no longer offers, dropped. A `choice`
* loses a `k` its options no longer carry; a `multi` keeps only the `ks`
* still there. An entry left with nothing meaningful goes. Every other kind,
* and any id not among `items`, stays as it was. `dropped` lists the ids that
* lost a pick. The inputs are never changed.
*/
export function pruneStaged(items, staged) {
const out = { ...(staged || {}) };
const dropped = [];
const byId = new Map((items || []).filter((it) => it && it.id).map((it) => [it.id, it]));
for (const [id, v] of Object.entries(out)) {
const item = byId.get(id);
if (!item || !v || typeof v !== "object" || (item.kind !== "choice" && item.kind !== "multi")) continue;
const keys = new Set((item.options || []).map((o) => o && o.k));
const next = { ...v };
let lost = false;
if (item.kind === "choice" && next.k && !keys.has(next.k)) {
delete next.k;
lost = true;
}
if (item.kind === "multi" && Array.isArray(next.ks)) {
const kept = next.ks.filter((k) => keys.has(k));
if (kept.length !== next.ks.length) {
next.ks = kept;
lost = true;
}
}
if (!lost) continue;
dropped.push(id);
const left = next.k || (Array.isArray(next.ks) && next.ks.length) || next.text || next.verdict || next.skipped;
if (left) out[id] = next;
else delete out[id];
}
return { staged: out, dropped };
}
/* ------------------------------------------------------------ patience */
/**
+2 -2
View File
@@ -19,7 +19,7 @@
import { page, onRedraw, onSave, useHooks, bootData } from "./state.js";
import {
adopt, banner, connect, fillSky, fitGrowBox, loadState, markSent, maybeWelcome, render, renderFooter,
adopt, askSuggestions, banner, connect, fillSky, fitGrowBox, loadState, markSent, maybeWelcome, render, renderFooter,
renderSidebar, renderWhere, save, startClock, startLooks, stagedPayload, syncWorkspaceScope,
wakeHoldsChime, wireAsk, wireCards, wireChrome, wireCleanup, wireDashboard, wireDocs, wireEnding,
wireActivities, wireLoopSetup, wireHelp, launchSkill, openHelpOn, openLoopSetup, wireLooks, wireNotepad, wirePrefs, wireWorkspace,
@@ -36,7 +36,7 @@ import { sound } from "./sound.js";
// and the few calls a screen makes into another are handed to state.js first.
onSave(save);
useHooks({
launchSkill, openHelpOn, openLoopSetup, banner, fitGrowBox, markSent, maybeWelcome, renderFooter, renderSidebar, renderWhere, stagedPayload, syncWorkspaceScope,
launchSkill, openHelpOn, openLoopSetup, askSuggestions, banner, fitGrowBox, markSent, maybeWelcome, renderFooter, renderSidebar, renderWhere, stagedPayload, syncWorkspaceScope,
openPrefs, markSaved, confirmUnsaved, loadAgentsView, wireAgentsView, hasUnsaved, unsavedName, saveOpen, discardOpen, focusOpen,
});
+56 -5
View File
@@ -32,6 +32,8 @@ import {
sentAnswer,
staleLaunchFeedback,
submittedAwaiting,
suggestAction,
suggestReplyLines,
verdictText,
workflowLabel,
workflowTitle,
@@ -63,7 +65,7 @@ import { renderLaunching, renderWaiting } from "./starting.js";
import { attachFiles, heldNotes, noteImages, pendingNotes, removeImage } from "../uploads.js";
import { closeMentions } from "../mention-box.js";
import { buildPager, fitGrowBox, markFor, markMore, renderCard, renderEndItem, renderNotice } from "./cards.js";
import { sendState } from "../send-state.js";
import { sendState, suggestState } from "../send-state.js";
import { renderBriefButton, renderDoc, renderOverview } from "./docs.js";
import { board, renderGitBoard, resetBoard } from "./git-host.js";
import { renderWorkspace, tellFolderIssues } from "./workspace.js";
@@ -1973,10 +1975,7 @@ export function markSent({ sentIds, sentNotes = {}, summaries, values }) {
for (const id of sentIds) {
for (const prefix of ["other-", "text-", "note-"]) delete page.local.drafts[prefix + id];
if (sentNotes[id]) {
delete page.local.drafts["thread-" + id];
const rest = noteImages(id).filter((img) => !sentNotes[id].has(img.key));
if (rest.length) page.local.images[id] = rest;
else delete page.local.images[id];
clearSentNote(id, sentNotes[id]);
} else if (!page.local.images[id]) {
delete page.local.drafts["thread-" + id];
}
@@ -1997,6 +1996,58 @@ export function markSent({ sentIds, sentNotes = {}, summaries, values }) {
save();
}
// A card's note went: its draft goes, and of its attachments only those whose
// `key` is not in `keys` (the ones that did not go with it) stay. Shared by
// Send and the suggestion send.
export function clearSentNote(id, keys) {
delete page.local.drafts["thread-" + id];
const rest = noteImages(id).filter((img) => !keys.has(img.key));
if (rest.length) page.local.images[id] = rest;
else delete page.local.images[id];
}
// The Ask for suggestions chip: one request for suggestions on one card, with
// the card's ready note riding along. Its own send, beside Send: nothing
// staged goes or is cleared, and the card stays open to answer.
export async function askSuggestions(itemId) {
const item = byId(itemId);
if (!item || suggestState(item).disabled) return;
const note = pendingNotes().find((n) => n.id === itemId);
const text = note ? note.text : "";
const images = note ? note.images : [];
try {
const res = await fetch("/submit", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
actions: [suggestAction(itemId, text, images)],
reply: suggestReplyLines(item.title, text, images).join("\n"),
}),
});
if (res.status === 409) {
banner(
"Your last answers are still being picked up. Nothing is lost, and nothing was sent twice. Try again in a moment.",
"info"
);
page.pendingBannerUp = true;
page.pendingResult = true;
} else if (!res.ok) {
const body = await res.json().catch(() => null);
banner(refusedLine({ status: res.status, reason: (body && (body.message || body.error)) || `error ${res.status}` }), "warn");
} else {
// A note held back on an upload did not go, so it stays drafted.
if (note) clearSentNote(itemId, new Set(images.map((i) => i.key)));
save();
banner("Asked for suggestions. Plan2Code is picking it up now.", "good");
setTimeout(() => banner(null), 4000);
}
} catch {
await sendLost();
} finally {
render();
}
}
// The menu card whose staged pick is an option marked `fresh`, if any.
function freshMenuPick(actions) {
for (const a of actions) {
+17 -5
View File
@@ -14,11 +14,14 @@ import {
loopDriving,
loopWaiting,
} from "./selectors.js";
import { finishPaused } from "./answers.js";
import { finishPaused, noteAwaiting } from "./answers.js";
import { page } from "./state.js";
import { pendingNotes } from "./uploads.js";
export function sendState() {
// `hasPayload` says whether there is something to send; left out, it is what
// the main Send would carry. The suggestion chip passes `true`: its request is
// the payload.
export function sendState(hasPayload = stagedCount() || pendingNotes().length > 0) {
// Before `gone`, and before the grace period that can re-enable a send. A
// finished session has nobody left to send to, whatever the socket says.
if (finish())
@@ -48,7 +51,7 @@ export function sendState() {
// "waiting" before asking, but an agent that asks and carries on ("while
// you decide") must not lock the person out of answering for up to an
// hour of quietMinutes.
if (myTurn().length && (stagedCount() || pendingNotes().length)) {
if (myTurn().length && hasPayload) {
return {
disabled: false,
why: "Plan2Code is still working. Your answers wait for it to look.",
@@ -65,7 +68,7 @@ export function sendState() {
// duration printed here freezes between them, and a frozen number
// beside "is it stuck?" makes the page look like the thing it is
// warning about. The exact time already ticks away in the dock.
if (stagedCount() || pendingNotes().length) {
if (hasPayload) {
return { disabled: false, why: "Plan2Code has been busy a while. You can send anyway.", short: "Busy · send anyway" };
}
return openItems().length
@@ -78,6 +81,15 @@ export function sendState() {
}
return { disabled: true, why: "Wait for Plan2Code to finish.", short: agentAdrift() ? "Gone quiet" : "" };
}
if (!stagedCount() && !pendingNotes().length) return { disabled: true, why: "" };
if (!hasPayload) return { disabled: true, why: "" };
return { disabled: false, why: "" };
}
// Whether the Ask for suggestions chip on `item` can fire, and why not. One
// request for suggestions waits at a time per card; otherwise it follows Send.
export function suggestState(item) {
const note = noteAwaiting(item);
if (note && note.suggest === true) return { disabled: true, why: "Plan2Code has your request for suggestions on this one." };
const { disabled, why } = sendState(true);
return { disabled, why: why || (disabled ? "" : "Ask Plan2Code what it would pick here, and why.") };
}