castle-web-cli 0.4.57 → 0.4.59
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-prompts.d.ts +2 -1
- package/dist/agent-prompts.js +18 -3
- package/dist/agent.js +78 -4
- package/package.json +1 -1
package/dist/agent-prompts.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export interface PromptMessage {
|
|
2
2
|
role: "user" | "assistant";
|
|
3
3
|
text: string;
|
|
4
|
+
interrupted?: boolean;
|
|
4
5
|
}
|
|
5
6
|
export interface PromptTask {
|
|
6
7
|
id: string;
|
|
@@ -16,7 +17,7 @@ export declare function buildRouterPrompt(opts: {
|
|
|
16
17
|
instruction: string;
|
|
17
18
|
}): string;
|
|
18
19
|
export declare function userTurnInstruction(opts: {
|
|
19
|
-
|
|
20
|
+
messages: string[];
|
|
20
21
|
interruptedDraft?: string;
|
|
21
22
|
attachments?: string[];
|
|
22
23
|
}): string;
|
package/dist/agent-prompts.js
CHANGED
|
@@ -61,7 +61,14 @@ function renderTranscript(messages) {
|
|
|
61
61
|
if (recent.length === 0)
|
|
62
62
|
return "(no conversation yet)";
|
|
63
63
|
return recent
|
|
64
|
-
.map((m) =>
|
|
64
|
+
.map((m) => {
|
|
65
|
+
if (m.role === "user")
|
|
66
|
+
return `user: ${m.text}`;
|
|
67
|
+
const label = m.interrupted
|
|
68
|
+
? "you (interrupted draft -- not a complete reply)"
|
|
69
|
+
: "you";
|
|
70
|
+
return `${label}: ${m.text}`;
|
|
71
|
+
})
|
|
65
72
|
.join("\n\n");
|
|
66
73
|
}
|
|
67
74
|
function renderTasks(tasks) {
|
|
@@ -94,9 +101,17 @@ Reply now, as "you" in the conversation. Plain reply text only -- no role prefix
|
|
|
94
101
|
export function userTurnInstruction(opts) {
|
|
95
102
|
const parts = [];
|
|
96
103
|
if (opts.interruptedDraft?.trim()) {
|
|
97
|
-
parts.push(`Your previous reply was interrupted mid-stream by the user's new message. Its draft so far (already shown to the user; NO tasks were spawned from it):\n\n${opts.interruptedDraft.trim()}\n\nContinue that line of work AND address the new message -- do
|
|
104
|
+
parts.push(`Your previous reply was interrupted mid-stream by the user's new message. Its draft so far (already shown to the user; NO tasks were spawned from it):\n\n${opts.interruptedDraft.trim()}\n\nContinue that line of work AND address the new message(s) -- do it all in this one reply, spawning whatever tasks are needed.`);
|
|
105
|
+
}
|
|
106
|
+
const msgs = opts.messages.filter((m) => m.trim());
|
|
107
|
+
if (msgs.length <= 1) {
|
|
108
|
+
parts.push(`The user just said:\n\n${msgs[0] ?? ""}`);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
parts.push(`The user sent these messages -- the earlier ones arrived while you were mid-reply and have NOT been answered yet. Address ALL of them in this one reply, in order. Exception: if a later message clearly clarifies, corrects, or takes back an earlier one, follow the later intent and don't redo work the user reversed. By default, cover every message:\n\n${msgs
|
|
112
|
+
.map((t, i) => `${i + 1}. ${t}`)
|
|
113
|
+
.join("\n\n")}`);
|
|
98
114
|
}
|
|
99
|
-
parts.push(`The user just said:\n\n${opts.text}`);
|
|
100
115
|
if (opts.attachments && opts.attachments.length > 0) {
|
|
101
116
|
parts.push(`The user attached image file(s), saved in the deck at: ${opts.attachments.join(", ")}. Open them with your read tool and take them into account; pass the paths along to task agents that need them.`);
|
|
102
117
|
}
|
package/dist/agent.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
// --force. Claude support can slot in later behind runAgentCli.
|
|
17
17
|
import { execFileSync, spawn } from 'child_process';
|
|
18
18
|
import * as fs from 'fs';
|
|
19
|
+
import * as os from 'os';
|
|
19
20
|
import * as path from 'path';
|
|
20
21
|
import { nanoid } from 'nanoid';
|
|
21
22
|
import { WebSocketServer } from 'ws';
|
|
@@ -187,6 +188,56 @@ function toolActivityLabel(ev) {
|
|
|
187
188
|
return 'running a command';
|
|
188
189
|
return 'working';
|
|
189
190
|
}
|
|
191
|
+
// Castle's agent CLI keys, delivered to the sandbox as a file
|
|
192
|
+
// (~/.castle/keys.json) rather than sandbox-wide env -- so an ambient key can't
|
|
193
|
+
// override a user's own subscription login. Falls back to process.env for
|
|
194
|
+
// older sandboxes that still inject the keys as env.
|
|
195
|
+
const CASTLE_KEYS_PATH = path.join(os.homedir(), '.castle', 'keys.json');
|
|
196
|
+
function castleKeys() {
|
|
197
|
+
try {
|
|
198
|
+
return JSON.parse(fs.readFileSync(CASTLE_KEYS_PATH, 'utf8'));
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
return {};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const BACKEND_KEY_ENV = {
|
|
205
|
+
claude: 'ANTHROPIC_API_KEY',
|
|
206
|
+
cursor: 'CURSOR_API_KEY',
|
|
207
|
+
};
|
|
208
|
+
// True when the user has their OWN saved auth for this backend -- a /login, or
|
|
209
|
+
// (for cursor, which reuses one auth.json) any saved creds. When so we do NOT
|
|
210
|
+
// inject Castle's key, so their auth is used and billed to them.
|
|
211
|
+
function backendHasSavedAuth(backend) {
|
|
212
|
+
const home = os.homedir();
|
|
213
|
+
if (backend === 'claude') {
|
|
214
|
+
return fs.existsSync(path.join(home, '.claude', '.credentials.json'));
|
|
215
|
+
}
|
|
216
|
+
if (backend === 'cursor') {
|
|
217
|
+
return fs.existsSync(path.join(home, '.config', 'cursor', 'auth.json'));
|
|
218
|
+
}
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
// Env for an agent spawn: inject Castle's key ONLY when the backend has no saved
|
|
222
|
+
// auth of the user's own. This is what lets internal testers run on their own
|
|
223
|
+
// subscription (log in once in the terminal) instead of Castle's key.
|
|
224
|
+
function envForAgentSpawn(backend) {
|
|
225
|
+
const env = { ...process.env };
|
|
226
|
+
const keyName = BACKEND_KEY_ENV[backend];
|
|
227
|
+
if (!keyName)
|
|
228
|
+
return env;
|
|
229
|
+
if (backendHasSavedAuth(backend)) {
|
|
230
|
+
delete env[keyName];
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
const val = castleKeys()[keyName] ?? process.env[keyName];
|
|
234
|
+
if (val)
|
|
235
|
+
env[keyName] = val;
|
|
236
|
+
else
|
|
237
|
+
delete env[keyName];
|
|
238
|
+
}
|
|
239
|
+
return env;
|
|
240
|
+
}
|
|
190
241
|
// One headless agent CLI run (cursor or claude), normalized to the same
|
|
191
242
|
// delta/activity/result hooks. Cursor: assistant events carrying timestamp_ms
|
|
192
243
|
// are text deltas; the trailing assistant event without one repeats the whole
|
|
@@ -196,7 +247,7 @@ function runAgentCli(opts) {
|
|
|
196
247
|
return new Promise((resolve) => {
|
|
197
248
|
const child = spawn(opts.command, opts.args, {
|
|
198
249
|
cwd: opts.cwd,
|
|
199
|
-
env:
|
|
250
|
+
env: envForAgentSpawn(opts.parser),
|
|
200
251
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
201
252
|
});
|
|
202
253
|
opts.children.add(child);
|
|
@@ -793,7 +844,11 @@ function runRouterTurnIn(ctx, instruction) {
|
|
|
793
844
|
deckLabel: ctx.deckLabel,
|
|
794
845
|
messages: ctx.log.messages
|
|
795
846
|
.filter((m) => m.role !== 'log' && m.id !== message.id && m.status !== 'streaming')
|
|
796
|
-
.map((m) => ({
|
|
847
|
+
.map((m) => ({
|
|
848
|
+
role: m.role,
|
|
849
|
+
text: m.text,
|
|
850
|
+
interrupted: m.interrupted,
|
|
851
|
+
})),
|
|
797
852
|
// Only the live board -- match what the user sees. Hide tasks that are
|
|
798
853
|
// BOTH acknowledged AND finished; an active (running/waiting) task always
|
|
799
854
|
// shows even if somehow acked, so nothing can ever go invisible mid-work.
|
|
@@ -1036,6 +1091,20 @@ export function createAgentServer(opts) {
|
|
|
1036
1091
|
claudeModel: () => settings.claudeModel,
|
|
1037
1092
|
}, instruction);
|
|
1038
1093
|
}
|
|
1094
|
+
// User messages awaiting a reply: everything since the last COMPLETED (done,
|
|
1095
|
+
// not interrupted) assistant message. Usually just the latest, but a rapid
|
|
1096
|
+
// burst leaves several queued -- all must be addressed, not only the last.
|
|
1097
|
+
function pendingUserMessages() {
|
|
1098
|
+
let lastAnswered = -1;
|
|
1099
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1100
|
+
const m = messages[i];
|
|
1101
|
+
if (m.role === 'assistant' && m.status === 'done' && !m.interrupted) {
|
|
1102
|
+
lastAnswered = i;
|
|
1103
|
+
break;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
return messages.slice(lastAnswered + 1).filter((m) => m.role === 'user');
|
|
1107
|
+
}
|
|
1039
1108
|
function handleUserMessage(text, images) {
|
|
1040
1109
|
userEpoch += 1;
|
|
1041
1110
|
const interruptedDraft = interruptRouterRuns();
|
|
@@ -1051,10 +1120,15 @@ export function createAgentServer(opts) {
|
|
|
1051
1120
|
if (attachments.length > 0)
|
|
1052
1121
|
message.attachments = attachments;
|
|
1053
1122
|
log.add(message);
|
|
1123
|
+
// Address every still-unanswered user message (this one plus any earlier
|
|
1124
|
+
// burst messages that interrupted prior turns), not just the latest.
|
|
1125
|
+
const pending = pendingUserMessages();
|
|
1054
1126
|
runRouterTurn(userTurnInstruction({
|
|
1055
|
-
text,
|
|
1127
|
+
messages: pending.map((m) => m.text),
|
|
1056
1128
|
interruptedDraft: interruptedDraft || undefined,
|
|
1057
|
-
attachments:
|
|
1129
|
+
attachments: pending
|
|
1130
|
+
.flatMap((m) => m.attachments ?? [])
|
|
1131
|
+
.map((name) => path.join('.castle', 'agent', 'attachments', name)),
|
|
1058
1132
|
}));
|
|
1059
1133
|
}
|
|
1060
1134
|
function handleTaskAck(id, rejected) {
|