@evandu/super-pi 0.1.238 → 0.1.240

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.
@@ -2054,6 +2054,11 @@ export class Store {
2054
2054
  ...(options?.delivery ? { delivery: options.delivery } : {}),
2055
2055
  };
2056
2056
  conversation.messages.push(message);
2057
+ if (options?.prioritizeWaiting && message.delivery === "waiting") {
2058
+ const waiting = this.waitingMessages(conversation);
2059
+ const lastSending = waiting.reduce((last, item, index) => item.delivery === "sending" ? index : last, -1);
2060
+ this.moveWaitingMessage(conversation, waiting, waiting.length - 1, lastSending + 1);
2061
+ }
2057
2062
  conversation.updatedAt = message.createdAt;
2058
2063
  await this.persist();
2059
2064
  return structuredClone(message);
@@ -2815,6 +2820,13 @@ export class Store {
2815
2820
  }
2816
2821
  if (target === from)
2817
2822
  return structuredClone(message);
2823
+ this.moveWaitingMessage(conversation, waiting, from, target);
2824
+ await this.persistMessages(conversation);
2825
+ return structuredClone(message);
2826
+ }
2827
+ moveWaitingMessage(conversation, waiting, from, target) {
2828
+ if (from === target)
2829
+ return;
2818
2830
  // Permute THE WAITING SLOTS rather than splicing a message to an index. The waiting messages are
2819
2831
  // usually a suffix of the array, but they are not guaranteed to be one - an assistant message sits
2820
2832
  // between two of them once a steer has been delivered mid-turn - and an offset derived from "where in
@@ -2822,36 +2834,13 @@ export class Store {
2822
2834
  // place is total: it cannot land a message outside the group, and it moves nothing that is not waiting.
2823
2835
  const slots = waiting.map((item) => conversation.messages.indexOf(item));
2824
2836
  const reordered = [...waiting];
2825
- reordered.splice(from, 1);
2837
+ const [message] = reordered.splice(from, 1);
2826
2838
  reordered.splice(target, 0, message);
2827
2839
  slots.forEach((slot, order) => { conversation.messages[slot] = reordered[order]; });
2828
- await this.persistMessages(conversation);
2829
- return structuredClone(message);
2830
2840
  }
2831
2841
  /**
2832
- * Pi has just delivered one steered message: unmark it and open the answer to it, in ONE write.
2833
- *
2834
- * Oldest rather than matched on text, and that is forced on us by Pi rather than chosen: Pi identifies
2835
- * its own queued entry by `indexOf` on the message text (`agent-session.ts:1123`), so two identical
2836
- * steered messages are ONE entry to it and no host-side matching could do better. Position is sufficient
2837
- * anyway because Pi's steering queue is FIFO and Super Pi only ever steers from the front of the waiting
2838
- * group.
2839
- *
2840
- * ONE WRITE, because the reader must never see the intermediate: a message that has stopped waiting but
2841
- * has no answer under it yet is the exact broken state this refactor exists to remove, and a browser
2842
- * that broadcast between the two halves would put it right back on screen.
2843
- *
2844
- * WHAT IS DIFFERENT FROM BEFORE, and is the whole point: the message used to be DELETED here. It is now
2845
- * merely unmarked, with an assistant message spliced in after it. It was on screen the whole time - it
2846
- * was written into the transcript when the user pressed send, long before Pi ever saw it - and it stays
2847
- * exactly where it was, now with the agent answering it. Deleting it is what made a steered reply appear
2848
- * with no question above it.
2849
- *
2850
- * `messageId` says WHICH message Pi picked up, for the case where position cannot answer it. A message
2851
- * handed to Pi's follow-up queue was never CLAIMED here - it is still `waiting`, because Super Pi keeps
2852
- * it in its own list so the user can still withdraw it - and a conversation can hold a `waiting` message
2853
- * and a `sending` one at the same time. The caller resolves the delivery by the text Pi announced,
2854
- * which is the same text Pi itself matched on.
2842
+ * Open the reply to a Pi steer and clear its delivery state in one write.
2843
+ * The text match and the FIFO fallback both require a claimed `sending` row.
2855
2844
  */
2856
2845
  async deliverWaitingMessage(conversationId, agentIdentity, messageId) {
2857
2846
  const conversation = this.requireConversation(conversationId);
@@ -2865,7 +2854,7 @@ export class Store {
2865
2854
  if (index < 0)
2866
2855
  return undefined;
2867
2856
  const user = conversation.messages[index];
2868
- if (user.role !== "user" && user.role !== "tool")
2857
+ if ((user.role !== "user" && user.role !== "tool") || user.delivery !== "sending")
2869
2858
  return undefined;
2870
2859
  delete user.delivery;
2871
2860
  const assistant = { id: id("message"), role: "assistant", content: "", createdAt: now(), ...(agentIdentity ? { agentIdentity: structuredClone(agentIdentity) } : {}) };
@@ -0,0 +1,22 @@
1
+ export type ContentSearchHit = {
2
+ path: string;
3
+ name: string;
4
+ line: number;
5
+ column: number;
6
+ text: string;
7
+ };
8
+ export type ContentSearchSummary = {
9
+ complete: boolean;
10
+ truncated: boolean;
11
+ reasons: string[];
12
+ };
13
+ export type ContentSearchEvent = {
14
+ type: "matches";
15
+ hits: ContentSearchHit[];
16
+ } | {
17
+ type: "done";
18
+ summary: ContentSearchSummary;
19
+ } | {
20
+ type: "error";
21
+ error: string;
22
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -10,28 +10,50 @@ export function findMentionToken(text, caret, selectionEnd = caret) {
10
10
  const remaining = match[1] !== undefined ? /^[^"\n]*(?:"|$)/u.exec(tail)?.[0] : /^[^\s@"\n]*/u.exec(tail)?.[0];
11
11
  return { start, end: caret + (remaining?.length ?? 0), query: match[1] ?? match[2] ?? "" };
12
12
  }
13
- /** Ordered-character matching supports abbreviations such as sct -> src/client/theme.tsx. */
13
+ var MatchRank;
14
+ (function (MatchRank) {
15
+ MatchRank[MatchRank["ExactPath"] = 0] = "ExactPath";
16
+ MatchRank[MatchRank["FoldedPath"] = 1] = "FoldedPath";
17
+ MatchRank[MatchRank["ExactName"] = 2] = "ExactName";
18
+ MatchRank[MatchRank["NamePrefix"] = 3] = "NamePrefix";
19
+ MatchRank[MatchRank["NameSubstring"] = 4] = "NameSubstring";
20
+ MatchRank[MatchRank["NameSubsequence"] = 5] = "NameSubsequence";
21
+ MatchRank[MatchRank["PathSubstring"] = 6] = "PathSubstring";
22
+ MatchRank[MatchRank["PathSubsequence"] = 7] = "PathSubsequence";
23
+ })(MatchRank || (MatchRank = {}));
24
+ function subsequenceScore(text, query, rank) {
25
+ let start = -1;
26
+ let end = 0;
27
+ for (const character of query) {
28
+ const position = text.indexOf(character, end);
29
+ if (position < 0)
30
+ return;
31
+ if (start < 0)
32
+ start = position;
33
+ end = position + character.length;
34
+ }
35
+ // Count gaps within the match. Long directories should never improve a sparse match.
36
+ return [rank, end - start - query.length, start];
37
+ }
14
38
  function fuzzyScore(path, query) {
15
- const name = path.slice(path.lastIndexOf("/") + 1);
16
39
  if (path === query)
17
- return -1;
40
+ return [MatchRank.FoldedPath];
41
+ const name = path.slice(path.lastIndexOf("/") + 1);
18
42
  if (name === query)
19
- return 0;
20
- if (name.startsWith(query))
21
- return 10 + name.length / 1000;
22
- if (name.includes(query))
23
- return 20 + name.indexOf(query) / 100;
24
- if (path.includes(query))
25
- return 30 + path.indexOf(query) / 100;
26
- let position = -1, gaps = 0;
27
- for (const character of query) {
28
- const next = path.indexOf(character, position + 1);
29
- if (next < 0)
30
- return undefined;
31
- gaps += next - position - 1;
32
- position = next;
33
- }
34
- return 50 + gaps / Math.max(1, path.length);
43
+ return [MatchRank.ExactName];
44
+ const nameStart = name.indexOf(query);
45
+ if (nameStart === 0)
46
+ return [MatchRank.NamePrefix, name.length];
47
+ if (nameStart > 0)
48
+ return [MatchRank.NameSubstring, nameStart, name.length];
49
+ const nameMatch = subsequenceScore(name, query, MatchRank.NameSubsequence);
50
+ if (nameMatch)
51
+ return nameMatch;
52
+ const pathStart = path.indexOf(query);
53
+ return pathStart >= 0 ? [MatchRank.PathSubstring, pathStart] : subsequenceScore(path, query, MatchRank.PathSubsequence);
54
+ }
55
+ function compareScores(a, b) {
56
+ return a[0] - b[0] || (a[1] ?? 0) - (b[1] ?? 0) || (a[2] ?? 0) - (b[2] ?? 0);
35
57
  }
36
58
  export function rankPathMatches(entries, query) {
37
59
  const normalized = query.trim().replaceAll("\\", "/");
@@ -41,11 +63,21 @@ export function rankPathMatches(entries, query) {
41
63
  if (!entry.path || entry.path.startsWith("/") || /^[a-z]:/iu.test(entry.path) || entry.path.split("/").includes(".."))
42
64
  continue;
43
65
  const path = entry.path.toLowerCase();
44
- const score = needle ? entry.path === normalized ? -2 : fuzzyScore(path, needle) : 0;
66
+ const score = !needle || entry.path === normalized
67
+ ? [MatchRank.ExactPath] : fuzzyScore(path, needle);
45
68
  if (score !== undefined)
46
69
  matches.push({ entry, score, depth: entry.path.split("/").length });
47
70
  }
48
- matches.sort((a, b) => a.score - b.score || a.depth - b.depth || Number(b.entry.kind === "directory") - Number(a.entry.kind === "directory") || (a.entry.path < b.entry.path ? -1 : a.entry.path > b.entry.path ? 1 : 0));
71
+ matches.sort((a, b) => {
72
+ const order = compareScores(a.score, b.score);
73
+ if (order)
74
+ return order;
75
+ const directoriesFirst = Number(b.entry.kind === "directory") - Number(a.entry.kind === "directory");
76
+ // Keep files ahead of tied fuzzy directory suggestions in path mentions.
77
+ return (a.score[0] === MatchRank.PathSubsequence ? -directoriesFirst : 0)
78
+ || a.depth - b.depth || directoriesFirst
79
+ || (a.entry.path < b.entry.path ? -1 : a.entry.path > b.entry.path ? 1 : 0);
80
+ });
49
81
  return matches.map(item => item.entry);
50
82
  }
51
83
  export function matchPathMentions(entries, query, limit = 30) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evandu/super-pi",
3
- "version": "0.1.238",
3
+ "version": "0.1.240",
4
4
  "description": "Web control plane and companion for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",