@kingcrab/pi-imessage 1.2.0 → 1.2.1
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.
- package/dist/agent/deliver.js +43 -15
- package/package.json +1 -1
package/dist/agent/deliver.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
2
|
-
import { AssistantEntry, } from "@earendil-works/pi-durable";
|
|
3
|
-
import { finalReplyText } from "../extensions/final-text.js";
|
|
2
|
+
import { AssistantEntry, UserEntry, } from "@earendil-works/pi-durable";
|
|
3
|
+
import { finalReplyText, interimText } from "../extensions/final-text.js";
|
|
4
4
|
import { Chats } from "./chats.js";
|
|
5
5
|
import { DirectSends } from "./direct-send.js";
|
|
6
6
|
import { Deliveries } from "./replies.js";
|
|
@@ -83,29 +83,57 @@ async function deliverAnswer(harness, storage, target, answerId, send) {
|
|
|
83
83
|
// Do not overtake an earlier in-flight send in this chat.
|
|
84
84
|
if (Object.values(deliveries.answers).includes("sending"))
|
|
85
85
|
return { decided: false };
|
|
86
|
+
// The same text already went out beside a tool call in this turn: the user has seen it.
|
|
87
|
+
const sends = (await tx.doc(DirectSends)).requests;
|
|
88
|
+
let echoed;
|
|
89
|
+
let turnCursor;
|
|
90
|
+
do {
|
|
91
|
+
const page = await tx.scanEntries({ conversationId, maxEntryId: answerId }, 100, turnCursor);
|
|
92
|
+
for (const entry of page.items) {
|
|
93
|
+
if (UserEntry.is(entry)) {
|
|
94
|
+
turnCursor = undefined;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
if (!AssistantEntry.is(entry) || interimText(entry.model?.[0]) !== text)
|
|
98
|
+
continue;
|
|
99
|
+
const status = sends.find((send) => send.chatGuid === target.chatGuid && send.requestId === `interim:${entry.id}`)?.textStatus;
|
|
100
|
+
if (status === "sent" || status === "sending")
|
|
101
|
+
echoed = status;
|
|
102
|
+
if (echoed)
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
turnCursor = echoed ? undefined : page.next;
|
|
106
|
+
} while (turnCursor);
|
|
107
|
+
// Wait for that send to settle instead of racing it.
|
|
108
|
+
if (echoed === "sending")
|
|
109
|
+
return { decided: false };
|
|
86
110
|
// Finals are claimed in order, so the only earlier unsent ones are held drafts; this answer replaces them.
|
|
87
111
|
const drafts = deliveries.drafts ?? {};
|
|
88
112
|
for (const [previous, draft] of Object.entries(drafts))
|
|
89
113
|
if (draft.status === "held" && Number(previous) < answerId)
|
|
90
114
|
drafts[previous] = { status: "superseded", replacement: answerId };
|
|
91
|
-
deliveries.answers[key] = "sending";
|
|
92
|
-
return { decided: decided(), text };
|
|
115
|
+
deliveries.answers[key] = echoed ?? "sending";
|
|
116
|
+
return { decided: decided(), text, echoed: echoed === "sent" };
|
|
93
117
|
}, BACKGROUND_CONTEXT);
|
|
94
118
|
if (!claim.text)
|
|
95
119
|
return claim.decided;
|
|
96
120
|
let status = "sent";
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
121
|
+
if (claim.echoed)
|
|
122
|
+
console.log("Reply already sent as interim", conversationId, String(answerId));
|
|
123
|
+
else {
|
|
124
|
+
try {
|
|
125
|
+
await send(target.chatGuid, claim.text);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
// A throwing transport may already have sent. Never retry it automatically.
|
|
129
|
+
status = "unknown";
|
|
130
|
+
}
|
|
131
|
+
await harness.commit(async (tx) => {
|
|
132
|
+
(await tx.doc(Deliveries, conversationId)).answers[String(answerId)] = status;
|
|
133
|
+
}, BACKGROUND_CONTEXT);
|
|
134
|
+
if (status === "unknown")
|
|
135
|
+
console.warn("Reply delivery unknown", conversationId, String(answerId));
|
|
103
136
|
}
|
|
104
|
-
await harness.commit(async (tx) => {
|
|
105
|
-
(await tx.doc(Deliveries, conversationId)).answers[String(answerId)] = status;
|
|
106
|
-
}, BACKGROUND_CONTEXT);
|
|
107
|
-
if (status === "unknown")
|
|
108
|
-
console.warn("Reply delivery unknown", conversationId, String(answerId));
|
|
109
137
|
if (target.chat !== undefined) {
|
|
110
138
|
// A queued write never splits a turn that is running in the chat conversation.
|
|
111
139
|
const chat = await harness.conversation(target.chat, BACKGROUND_CONTEXT);
|