crewx-pi-kit 0.1.12 → 0.1.14
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/README.md +1 -1
- package/extensions/crewx-tools.ts +19 -17
- package/extensions/session-context.ts +36 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -151,8 +151,7 @@ function wait(milliseconds: number, signal?: AbortSignal): Promise<void> {
|
|
|
151
151
|
export default function crewxTools(pi: ExtensionAPI) {
|
|
152
152
|
const protectedValues = new Set<string>();
|
|
153
153
|
const deliveredSecretRequests = new Set<string>();
|
|
154
|
-
const
|
|
155
|
-
let computerTail = Promise.resolve();
|
|
154
|
+
const computerCalls = new Set<string>();
|
|
156
155
|
let computerLease: string | undefined;
|
|
157
156
|
let computerTimer: ReturnType<typeof setTimeout> | undefined;
|
|
158
157
|
let computerOperations = Promise.resolve();
|
|
@@ -179,8 +178,8 @@ export default function crewxTools(pi: ExtensionAPI) {
|
|
|
179
178
|
if (computerTimer) clearTimeout(computerTimer);
|
|
180
179
|
computerTimer = setTimeout(async () => {
|
|
181
180
|
if (!computerLease || computerClosed) return;
|
|
182
|
-
try { await updateComputer(
|
|
183
|
-
catch { computerLease = undefined; if (
|
|
181
|
+
try { await updateComputer(computerCalls.size > 0); renewComputer(); }
|
|
182
|
+
catch { computerLease = undefined; if (computerCalls.size > 0) abortComputerAction?.(); }
|
|
184
183
|
}, 20_000);
|
|
185
184
|
computerTimer.unref();
|
|
186
185
|
};
|
|
@@ -188,8 +187,7 @@ export default function crewxTools(pi: ExtensionAPI) {
|
|
|
188
187
|
computerClosed = true;
|
|
189
188
|
if (computerTimer) clearTimeout(computerTimer);
|
|
190
189
|
if (computerLease) await updateComputer(false, true).catch(() => {});
|
|
191
|
-
|
|
192
|
-
computerReleases.clear();
|
|
190
|
+
computerCalls.clear();
|
|
193
191
|
};
|
|
194
192
|
pi.on('agent_start', () => { computerClosed = false; });
|
|
195
193
|
pi.on('agent_end', closeComputer);
|
|
@@ -226,26 +224,30 @@ export default function crewxTools(pi: ExtensionAPI) {
|
|
|
226
224
|
}
|
|
227
225
|
|
|
228
226
|
if (!isComputerTool(event.toolName, event.input)) return;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
227
|
+
// Pi prepares every parallel call before executing any of them. Waiting
|
|
228
|
+
// here for an earlier result deadlocks the whole batch. Admit one computer
|
|
229
|
+
// action and return a retryable tool error for the others instead.
|
|
230
|
+
if (computerCalls.size > 0) {
|
|
231
|
+
return { block: true, terminate: false, reason: 'Another computer action is already prepared in this batch. Wait for its result, then retry this action in the next turn. Computer actions must be sequential.' };
|
|
232
|
+
}
|
|
233
|
+
computerCalls.add(event.toolCallId);
|
|
233
234
|
try { await updateComputer(true); }
|
|
234
235
|
catch (error) {
|
|
235
|
-
computerLease = undefined;
|
|
236
|
+
computerLease = undefined; computerCalls.delete(event.toolCallId);
|
|
236
237
|
return { block: true, terminate: false, reason: error instanceof Error ? error.message : 'Computer ownership is unavailable. No browser action was performed.' };
|
|
237
238
|
}
|
|
238
239
|
abortComputerAction = () => ctx.abort();
|
|
239
|
-
computerReleases.set(event.toolCallId, release);
|
|
240
240
|
renewComputer();
|
|
241
241
|
});
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (release) {
|
|
245
|
-
computerReleases.delete(event.toolCallId);
|
|
242
|
+
const finishComputerCall = async (toolCallId: string) => {
|
|
243
|
+
if (computerCalls.delete(toolCallId)) {
|
|
246
244
|
if (computerLease) await updateComputer(false).catch(() => { computerLease = undefined; });
|
|
247
|
-
release();
|
|
248
245
|
}
|
|
246
|
+
};
|
|
247
|
+
// Also covers a later extension blocking a call after our preflight passed.
|
|
248
|
+
pi.on('tool_execution_end', async (event) => { await finishComputerCall(event.toolCallId); });
|
|
249
|
+
pi.on("tool_result", async (event) => {
|
|
250
|
+
await finishComputerCall(event.toolCallId);
|
|
249
251
|
return {
|
|
250
252
|
content: event.content.map((item) =>
|
|
251
253
|
item.type === "text" ? { ...item, text: scrub(item.text) } : item,
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ContextEvent, ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
2
|
+
|
|
3
|
+
const SNAPSHOT = '\n\n## Workspace snapshot\n\n```json\n';
|
|
4
|
+
|
|
5
|
+
function isEnvelope(text: string): boolean {
|
|
6
|
+
return text.startsWith('# CrewX assignment\n') &&
|
|
7
|
+
text.includes('\n# Operating context\n') &&
|
|
8
|
+
text.includes(SNAPSHOT) && text.endsWith('\n```');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Model-facing projection only: never rewrite Pi's persisted session ledger. */
|
|
12
|
+
export function latestWorkspaceSnapshot(messages: ContextEvent['messages']): ContextEvent['messages'] {
|
|
13
|
+
let latest = -1;
|
|
14
|
+
for (let index = 0; index < messages.length; index++) {
|
|
15
|
+
const message = messages[index];
|
|
16
|
+
if (message.role !== 'user') continue;
|
|
17
|
+
const texts = typeof message.content === 'string'
|
|
18
|
+
? [message.content] : message.content.filter(item => item.type === 'text').map(item => item.text);
|
|
19
|
+
if (texts.some(isEnvelope)) latest = index;
|
|
20
|
+
}
|
|
21
|
+
if (latest < 0) return messages;
|
|
22
|
+
const trim = (text: string): string => isEnvelope(text)
|
|
23
|
+
? text.slice(0, text.lastIndexOf(SNAPSHOT)) +
|
|
24
|
+
'\n\n[Historical CrewX workspace snapshot omitted. Use the latest assignment snapshot; the original task and tool history are retained.]'
|
|
25
|
+
: text;
|
|
26
|
+
return messages.map((message, index) => {
|
|
27
|
+
if (index >= latest || message.role !== 'user') return message;
|
|
28
|
+
return { ...message, content: typeof message.content === 'string'
|
|
29
|
+
? trim(message.content)
|
|
30
|
+
: message.content.map(item => item.type === 'text' ? { ...item, text: trim(item.text) } : item) };
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default function sessionContext(pi: ExtensionAPI): void {
|
|
35
|
+
pi.on('context', event => ({ messages: latestWorkspaceSnapshot(event.messages) }));
|
|
36
|
+
}
|