@astrosheep/square 0.3.26 → 0.3.27
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/extensions/square-pi.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { presentPendingAtBoundary, renderPendingAtBoundary } from '../dist/boundary-presentation.js';
|
|
2
2
|
import { automaticSessionEnd, automaticSessionStart } from '../dist/automatic-session.js';
|
|
3
3
|
import { waitForSessionPending } from '../dist/inbox.js';
|
|
4
|
+
import { lookupSessionBindings } from '../dist/registry.js';
|
|
5
|
+
|
|
6
|
+
const PI_SEND_TIMEOUT_MS = 5_000;
|
|
4
7
|
|
|
5
8
|
export function pendingInbox(inbox) {
|
|
6
9
|
return inbox.filter((item) => item.notifications?.length > 0);
|
|
@@ -52,8 +55,22 @@ export default function squarePiExtension(pi) {
|
|
|
52
55
|
watcherAbort = undefined;
|
|
53
56
|
};
|
|
54
57
|
|
|
58
|
+
const pause = (signal, delayMs) => new Promise((resolve) => {
|
|
59
|
+
const finish = () => {
|
|
60
|
+
signal.removeEventListener('abort', finish);
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
resolve();
|
|
63
|
+
};
|
|
64
|
+
const timer = setTimeout(finish, delayMs);
|
|
65
|
+
signal.addEventListener('abort', finish, { once: true });
|
|
66
|
+
});
|
|
67
|
+
|
|
55
68
|
const wake = async (piContext, token, signal) => {
|
|
56
69
|
while (sessionId !== undefined && token === generation && !signal.aborted) {
|
|
70
|
+
if (lookupSessionBindings(sessionId).length === 0) {
|
|
71
|
+
await pause(signal, 1_000);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
57
74
|
const deferredRetry = retryAfterChange;
|
|
58
75
|
const pending = await waitForSessionPending(sessionId, 30_000, {
|
|
59
76
|
signal,
|
|
@@ -78,10 +95,21 @@ export default function squarePiExtension(pi) {
|
|
|
78
95
|
const delivered = await presentPendingAtBoundary(
|
|
79
96
|
sessionId,
|
|
80
97
|
async (content) => {
|
|
81
|
-
|
|
98
|
+
const send = Promise.resolve(pi.sendMessage(
|
|
82
99
|
{ customType: 'square', content, display: true },
|
|
83
100
|
{ deliverAs: 'steer', triggerTurn: true },
|
|
84
|
-
);
|
|
101
|
+
));
|
|
102
|
+
let timer;
|
|
103
|
+
try {
|
|
104
|
+
await Promise.race([
|
|
105
|
+
send,
|
|
106
|
+
new Promise((_, reject) => {
|
|
107
|
+
timer = setTimeout(() => reject(new Error('Pi native injection timed out')), PI_SEND_TIMEOUT_MS);
|
|
108
|
+
}),
|
|
109
|
+
]);
|
|
110
|
+
} finally {
|
|
111
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
112
|
+
}
|
|
85
113
|
return true;
|
|
86
114
|
},
|
|
87
115
|
);
|
|
@@ -106,7 +134,8 @@ export default function squarePiExtension(pi) {
|
|
|
106
134
|
sessionCwd = ctx.cwd || process.cwd();
|
|
107
135
|
previousSessionId = process.env.SQUARE_PI_SESSION_ID;
|
|
108
136
|
process.env.SQUARE_PI_SESSION_ID = sessionId;
|
|
109
|
-
|
|
137
|
+
joiningContext = undefined;
|
|
138
|
+
void automaticSessionStart('pi', sessionId, sessionCwd).catch(() => undefined);
|
|
110
139
|
watcherAbort = new AbortController();
|
|
111
140
|
const token = generation;
|
|
112
141
|
watcher = wake(ctx, token, watcherAbort.signal).catch(() => undefined);
|
|
@@ -138,7 +167,7 @@ export default function squarePiExtension(pi) {
|
|
|
138
167
|
stopWatcher();
|
|
139
168
|
for (const waiter of settledWaiters) waiter.resolve();
|
|
140
169
|
settledWaiters = [];
|
|
141
|
-
if (sessionId && sessionCwd)
|
|
170
|
+
if (sessionId && sessionCwd) void automaticSessionEnd('pi', sessionId, sessionCwd).catch(() => undefined);
|
|
142
171
|
if (process.env.SQUARE_PI_SESSION_ID === sessionId) {
|
|
143
172
|
if (previousSessionId === undefined) delete process.env.SQUARE_PI_SESSION_ID;
|
|
144
173
|
else process.env.SQUARE_PI_SESSION_ID = previousSessionId;
|