privateer-agent 0.14.3 → 0.14.5
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/privateer-gate.ts +4 -4
- package/package.json +1 -1
- package/src/bridge/engineAdapter.ts +8 -1
- package/src/cli/chat.ts +6 -2
- package/src/config/moat.ts +12 -0
- package/src/config/whatsNew.json +3 -3
- package/src/outbox/cloudOutbox.ts +76 -1
- package/src/permissions/classify.ts +1 -1
- package/src/remote/cargoSave.ts +19 -5
- package/src/remote/liveTaskSession.ts +4 -2
- package/src/remote/remoteBridge.ts +33 -6
- package/src/tools/cargo.ts +46 -12
- package/src/tools/relayFileTools.ts +7 -7
|
@@ -640,10 +640,10 @@ export default function privateerControl(pi: any): void {
|
|
|
640
640
|
// driving. The daemon no longer loads this file, so there is nothing to shadow.
|
|
641
641
|
pi.registerTool?.(makeSendFileTool(bridge));
|
|
642
642
|
pi.registerTool?.(makeSaveAttachmentTool(attachments));
|
|
643
|
-
// save_cargo belongs with them: it
|
|
644
|
-
// app
|
|
645
|
-
//
|
|
646
|
-
// the
|
|
643
|
+
// save_cargo belongs with them: it tries this bridge's attached app first, which hands
|
|
644
|
+
// the app PLAINTEXT to encrypt — the terminal has no master key (cargoSave.ts). With no
|
|
645
|
+
// app attached (remote access off, or nobody driving) it queues the artifact sealed to
|
|
646
|
+
// the account instead, and the next app to open files it.
|
|
647
647
|
pi.registerTool?.(makeSaveCargoTool(bridge));
|
|
648
648
|
// The chart tools, for the same reasons and one more. Same: this bridge, the same
|
|
649
649
|
// "needs a connected app" precondition, the same plaintext-out/app-encrypts round trip
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "privateer-agent",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.5",
|
|
4
4
|
"description": "Privacy-first terminal coding agent — bring your own model across 20 providers (Anthropic, OpenAI, OpenRouter, Google, local Ollama…). Safe-by-default permissions, MCP, sub-agents, workflows, and verifiable TEE inference. Built on the Pi toolkit.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -73,11 +73,17 @@ export function createEngineEventAdapter() {
|
|
|
73
73
|
let turnTotal: UsageTotals = emptyUsage();
|
|
74
74
|
let lastMessage: any;
|
|
75
75
|
let terminalSent = false;
|
|
76
|
+
// Between agent_start and agent_settled. Compaction maps to `step-finish` to keep
|
|
77
|
+
// the app's busy pill lit through an auto-compaction INSIDE a run — but a manual
|
|
78
|
+
// `/compact` runs with no run around it, and a `step-finish` there opened a turn
|
|
79
|
+
// in the app that no `finish` would ever close: the pill spun until Stop.
|
|
80
|
+
let inRun = false;
|
|
76
81
|
|
|
77
82
|
function toEngineEvents(ev: PiSessionEvent): EngineEvent[] {
|
|
78
83
|
switch (ev.type) {
|
|
79
84
|
case "agent_start":
|
|
80
85
|
terminalSent = false;
|
|
86
|
+
inRun = true;
|
|
81
87
|
return [];
|
|
82
88
|
|
|
83
89
|
case "message_update": {
|
|
@@ -136,6 +142,7 @@ export function createEngineEventAdapter() {
|
|
|
136
142
|
}
|
|
137
143
|
|
|
138
144
|
case "agent_settled": {
|
|
145
|
+
inRun = false;
|
|
139
146
|
if (terminalSent) return [];
|
|
140
147
|
terminalSent = true;
|
|
141
148
|
const msg = lastMessage;
|
|
@@ -158,7 +165,7 @@ export function createEngineEventAdapter() {
|
|
|
158
165
|
}
|
|
159
166
|
|
|
160
167
|
case "compaction_start":
|
|
161
|
-
return [{ type: "step-finish" }];
|
|
168
|
+
return inRun ? [{ type: "step-finish" }] : [];
|
|
162
169
|
case "compaction_end":
|
|
163
170
|
return [
|
|
164
171
|
{
|
package/src/cli/chat.ts
CHANGED
|
@@ -349,6 +349,7 @@ async function main() {
|
|
|
349
349
|
return;
|
|
350
350
|
}
|
|
351
351
|
turnActive = true;
|
|
352
|
+
let failure: string | undefined;
|
|
352
353
|
if (remote && echo) console.log(`\n${DIM}⟿ [app] ${text.slice(0, 80)}${RESET}`);
|
|
353
354
|
try {
|
|
354
355
|
// Re-arm the account channel BEFORE the prompt, because nothing downstream can.
|
|
@@ -375,10 +376,13 @@ async function main() {
|
|
|
375
376
|
}
|
|
376
377
|
await session.prompt(mentions.text, mentions.images.length ? { images: mentions.images } : undefined);
|
|
377
378
|
} catch (e) {
|
|
378
|
-
|
|
379
|
+
failure = (e as Error).message || "The turn failed.";
|
|
380
|
+
console.log(`\n${RED}${failure}${RESET}`);
|
|
379
381
|
} finally {
|
|
380
382
|
turnActive = false;
|
|
381
|
-
|
|
383
|
+
// The failure goes to the app too: a throw here (no model, no key, auth
|
|
384
|
+
// precheck) used to print on this console only while the app's pill spun on.
|
|
385
|
+
if (remote) bridge.settleTurn(failure);
|
|
382
386
|
}
|
|
383
387
|
}
|
|
384
388
|
|
package/src/config/moat.ts
CHANGED
|
@@ -306,6 +306,18 @@ export async function buildMoat(opts: MoatOptions): Promise<ExtensionFactory[]>
|
|
|
306
306
|
if (opts.relayFiles) {
|
|
307
307
|
const { makeRelayFileTools } = await import("../tools/relayFileTools.ts");
|
|
308
308
|
factories.push(makeRelayFileTools(opts.relayFiles.bridge, opts.relayFiles.attachments));
|
|
309
|
+
} else {
|
|
310
|
+
// No relay of its own — a harbor run, a channels or ACP session — so no app can ever
|
|
311
|
+
// be attached. save_cargo still exists here, QUEUE-ONLY: it seals the artifact to the
|
|
312
|
+
// account and the next app to open files it (remote/cargoSave.ts). Signed-in only,
|
|
313
|
+
// because the queue is the account's; a signed-out session gets no tool rather than
|
|
314
|
+
// one that refuses every call. The rest of the relay set stays out: send_file needs
|
|
315
|
+
// a live app to receive bytes, and the chart tools READ decrypted content back.
|
|
316
|
+
const { hasCredentials } = await import("../auth/privateer.ts");
|
|
317
|
+
if (hasCredentials()) {
|
|
318
|
+
const { makeSaveCargoTool } = await import("../tools/cargo.ts");
|
|
319
|
+
factories.push((pi: any) => { pi.registerTool?.(makeSaveCargoTool()); });
|
|
320
|
+
}
|
|
309
321
|
}
|
|
310
322
|
|
|
311
323
|
if (opts.resultMedia) {
|
package/src/config/whatsNew.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$comment": "The banner's What's New list, newest first: the two to four things a returning user should notice, not a changelog. `reviewedFor` is the release this list was last looked at for. A release can't ship until it equals package.json's version (scripts/check-whats-new.mjs, run by `npm test` and by release.yml before anything builds), so every release, patches included, is a deliberate look at this list. If nothing new is worth a line, bumping `reviewedFor` alone is the answer. `since` is the release an entry arrived in. `cmd` is shown in the accent colour.",
|
|
3
|
-
"reviewedFor": "0.14.
|
|
3
|
+
"reviewedFor": "0.14.5",
|
|
4
4
|
"items": [
|
|
5
|
+
{ "text": "Save to Cargo with no app attached — it lands on your next app open", "since": "0.14.4" },
|
|
5
6
|
{ "text": "Several privateer -p runs at once no longer sign each other out", "since": "0.14.3" },
|
|
6
7
|
{ "text": "A reply cut off at the output limit picks back up on its own", "since": "0.14.2" },
|
|
7
|
-
{ "text": "Chat channels as a login service: privateer channels install", "since": "0.14.1" }
|
|
8
|
-
{ "text": "Hand off sensitive files: privateer --private", "since": "0.14.0" }
|
|
8
|
+
{ "text": "Chat channels as a login service: privateer channels install", "since": "0.14.1" }
|
|
9
9
|
]
|
|
10
10
|
}
|
|
@@ -26,12 +26,13 @@
|
|
|
26
26
|
// per-instance caches this replaced.
|
|
27
27
|
|
|
28
28
|
import { readFileSync, existsSync, statSync } from "node:fs";
|
|
29
|
-
import { apiRequest, defaultDeviceLabel } from "../auth/privateer.ts";
|
|
29
|
+
import { apiRequest, defaultDeviceLabel, hasCredentials } from "../auth/privateer.ts";
|
|
30
30
|
import { loadAccountSignKey } from "../crypto/accountTrust.ts";
|
|
31
31
|
import { verifyOutboxKey } from "../crypto/accountVerify.ts";
|
|
32
32
|
import { sealJson, seal, decodeAccountPublicKey } from "../crypto/outboxSeal.ts";
|
|
33
33
|
import { routineRelayId, type OutboxKind } from "../routines/store.ts";
|
|
34
34
|
import { MAX_ATTACHMENT_BYTES, type MediaClass, type StagedMedia } from "../routines/resultMedia.ts";
|
|
35
|
+
import type { CargoSaveRequest } from "../remote/cargoSave.ts";
|
|
35
36
|
|
|
36
37
|
export type { OutboxKind };
|
|
37
38
|
|
|
@@ -337,3 +338,77 @@ export async function postOutbox(
|
|
|
337
338
|
return false;
|
|
338
339
|
}
|
|
339
340
|
}
|
|
341
|
+
|
|
342
|
+
// ── Queued Cargo saves ───────────────────────────────────────────────────────
|
|
343
|
+
//
|
|
344
|
+
// `save_cargo` with no app attached (remote/cargoSave.ts has the why). Its own mailbox,
|
|
345
|
+
// /api/outbox/cargo, not an outbox item: every shipped app acks every item it fetches,
|
|
346
|
+
// so an artifact sent as one would be eaten — acked and never filed — by any device
|
|
347
|
+
// still on an older build, and filing has to happen once per account, which needs the
|
|
348
|
+
// server-side claim that mailbox has and this one doesn't.
|
|
349
|
+
|
|
350
|
+
/** Ceiling on the sealed artifact, matching the server's MAX_CARGO_SEALED_BYTES. */
|
|
351
|
+
export const MAX_QUEUED_CARGO_SEALED_BYTES = 2 * 1024 * 1024;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* The plaintext the app opens — treeview client/utils/queuedCargoParse.ts reads exactly
|
|
355
|
+
* this. `type` (not `kind`) names the envelope: `kind` is the artifact's, and the app's
|
|
356
|
+
* reader refuses anything that isn't a cargo envelope rather than guessing.
|
|
357
|
+
*/
|
|
358
|
+
export function queuedCargoEnvelope(req: CargoSaveRequest, at: Date, origin: { id: string; label: string }) {
|
|
359
|
+
return {
|
|
360
|
+
v: 1,
|
|
361
|
+
type: "cargo",
|
|
362
|
+
kind: req.kind,
|
|
363
|
+
...(req.title ? { title: req.title } : {}),
|
|
364
|
+
content: req.content,
|
|
365
|
+
at: at.toISOString(),
|
|
366
|
+
origin,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export type QueuedCargoResult = { ok: true; id: string } | { ok: false; reason: string };
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Seal one artifact to the account and park it for the app to file. Never throws;
|
|
374
|
+
* every failure comes back as a reason written for the model to repeat.
|
|
375
|
+
*/
|
|
376
|
+
export async function postQueuedCargo(req: CargoSaveRequest): Promise<QueuedCargoResult> {
|
|
377
|
+
if (!hasCredentials()) {
|
|
378
|
+
return { ok: false, reason: "this terminal is not signed in to a Privateer account — run /signin, then save again" };
|
|
379
|
+
}
|
|
380
|
+
const pub = await ensureOutboxPub();
|
|
381
|
+
if (!pub) {
|
|
382
|
+
// No pinned account key, or one the published key doesn't verify against. Either way
|
|
383
|
+
// sealing would be to a key nobody has shown belongs to this account — fail closed.
|
|
384
|
+
return {
|
|
385
|
+
ok: false,
|
|
386
|
+
reason:
|
|
387
|
+
"the account's encryption key could not be verified from this terminal — open the Privateer app once (it publishes the key), " +
|
|
388
|
+
"or re-link this terminal from the app, then save again",
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
const sealed = sealJson(pub, queuedCargoEnvelope(req, new Date(), machineOrigin()));
|
|
392
|
+
if (Math.floor((sealed.length * 3) / 4) > MAX_QUEUED_CARGO_SEALED_BYTES) {
|
|
393
|
+
return { ok: false, reason: "the artifact is too large to queue — trim it below 512 KB" };
|
|
394
|
+
}
|
|
395
|
+
try {
|
|
396
|
+
const res = await apiRequest("/api/outbox/cargo", {
|
|
397
|
+
method: "POST",
|
|
398
|
+
headers: { "content-type": "application/json" },
|
|
399
|
+
body: JSON.stringify({ sealed }),
|
|
400
|
+
});
|
|
401
|
+
if (res.ok) {
|
|
402
|
+
const data = (await res.json().catch(() => ({}))) as { id?: unknown };
|
|
403
|
+
return { ok: true, id: typeof data.id === "string" ? data.id : "" };
|
|
404
|
+
}
|
|
405
|
+
if (res.status === 404) {
|
|
406
|
+
return { ok: false, reason: "the Privateer server does not accept queued artifacts yet — attach the app to this terminal and save again" };
|
|
407
|
+
}
|
|
408
|
+
const err = (await res.json().catch(() => ({}))) as { message?: unknown };
|
|
409
|
+
const message = typeof err.message === "string" && err.message ? err.message : `the server answered ${res.status}`;
|
|
410
|
+
return { ok: false, reason: `the server refused to queue it: ${message}` };
|
|
411
|
+
} catch (e) {
|
|
412
|
+
return { ok: false, reason: `the server could not be reached (${(e as Error).message})` };
|
|
413
|
+
}
|
|
414
|
+
}
|
|
@@ -374,7 +374,7 @@ export function classifyToolCall(
|
|
|
374
374
|
tool: toolName,
|
|
375
375
|
kind: "write",
|
|
376
376
|
title: protectedSrc ? "Save a protected file to Cargo in the app" : "Save to Cargo in the Privateer app",
|
|
377
|
-
detail: `${outside || protectedSrc ? abs : src}${kindNote}${titleNote} → the app encrypts it and stores it in Cargo`,
|
|
377
|
+
detail: `${outside || protectedSrc ? abs : src}${kindNote}${titleNote} → the app encrypts it and stores it in Cargo (queued, sealed, if no app is attached)`,
|
|
378
378
|
protected: protectedSrc,
|
|
379
379
|
outside,
|
|
380
380
|
path: abs,
|
package/src/remote/cargoSave.ts
CHANGED
|
@@ -18,10 +18,21 @@
|
|
|
18
18
|
// model-authored one does. Nothing new reaches the server, and no new endpoint
|
|
19
19
|
// exists.
|
|
20
20
|
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
21
|
+
// WHEN NO APP IS ATTACHED. The live round trip needs an app on the other end, and
|
|
22
|
+
// requiring one made save_cargo useless exactly where it was wanted most: a terminal
|
|
23
|
+
// nobody is driving, and every harbor. So with no app attached the artifact is QUEUED
|
|
24
|
+
// instead (outbox/cloudOutbox.ts postQueuedCargo): sealed to the account's X25519
|
|
25
|
+
// outbox public key — the same write-only sealed box Inbox results use, verified
|
|
26
|
+
// against the account signing key pinned at link — and parked at /api/outbox/cargo.
|
|
27
|
+
// The next app to sync opens it and files it through the very same saveCargo()
|
|
28
|
+
// (treeview client/services/queuedCargoService.ts). The master key still never
|
|
29
|
+
// leaves the app, the server still holds only ciphertext, and the stored row is the
|
|
30
|
+
// one the live path would have made. What the queue cannot do is confirm: there is no
|
|
31
|
+
// cargoId until an app files it, and the tool says exactly that.
|
|
32
|
+
//
|
|
33
|
+
// Only an UNATTACHED failure falls back (`unattached: true` below). An app that
|
|
34
|
+
// refused, timed out, or dropped mid-transfer may already have stored the artifact,
|
|
35
|
+
// and queueing it as well is how a user ends up with two copies.
|
|
25
36
|
|
|
26
37
|
/** Cargo artifact kinds — mirrors client/utils/cargoKinds.ts CargoKind. */
|
|
27
38
|
export const CARGO_KINDS = ["webpage", "slides", "game", "pdf", "docx", "md", "sheet"] as const;
|
|
@@ -102,4 +113,7 @@ export interface CargoSaveRequest {
|
|
|
102
113
|
*/
|
|
103
114
|
export type CargoSaveResult =
|
|
104
115
|
| { ok: true; cargoId: string; title: string; storageType: string }
|
|
105
|
-
| { ok: false; reason: string };
|
|
116
|
+
| { ok: false; reason: string; unattached?: true };
|
|
117
|
+
|
|
118
|
+
/** Is this failure "there was no app to ask" — the one a queued save may follow? */
|
|
119
|
+
export const isUnattached = (r: CargoSaveResult): boolean => !r.ok && r.unattached === true;
|
|
@@ -140,6 +140,7 @@ export async function createLiveTaskSession(spec: TaskSpec, deps: LiveTaskDeps):
|
|
|
140
140
|
const runTurn = async (text: string): Promise<void> => {
|
|
141
141
|
if (turnActive || stopped) return;
|
|
142
142
|
turnActive = true;
|
|
143
|
+
let failure: string | undefined;
|
|
143
144
|
lastAnswer = ""; // keep the CLOSING answer, not the whole session
|
|
144
145
|
turnErrored = false;
|
|
145
146
|
try {
|
|
@@ -161,10 +162,11 @@ export async function createLiveTaskSession(spec: TaskSpec, deps: LiveTaskDeps):
|
|
|
161
162
|
await session.prompt(text + note);
|
|
162
163
|
} catch (e) {
|
|
163
164
|
turnErrored = true;
|
|
164
|
-
|
|
165
|
+
failure = (e as Error).message || "The turn failed.";
|
|
166
|
+
deps.log(`live task ${termId} turn error: ${failure}`);
|
|
165
167
|
} finally {
|
|
166
168
|
turnActive = false;
|
|
167
|
-
bridge.settleTurn();
|
|
169
|
+
bridge.settleTurn(failure);
|
|
168
170
|
}
|
|
169
171
|
};
|
|
170
172
|
|
|
@@ -15,7 +15,8 @@ import { noQuarterActive, setNoQuarter } from "../permissions/noQuarter.ts";
|
|
|
15
15
|
// Pure env-var accessors (see config/privacyDisabled.ts) — no Pi, no node builtins,
|
|
16
16
|
// so this stays safe to import from the bridge, which boot-ordered code reaches.
|
|
17
17
|
import { onPrivacyDisabledChange, privacyDisabled, setPrivacyDisabled } from "../config/privacyDisabled.ts";
|
|
18
|
-
import type
|
|
18
|
+
import { emptyUsage, type EngineEvent } from "../engine/events.ts";
|
|
19
|
+
import { redactText } from "../util/redact.ts";
|
|
19
20
|
import type { PermissionRequest } from "../permissions/gate.ts";
|
|
20
21
|
import type { AskOutcome } from "../permissions/modeGate.ts";
|
|
21
22
|
import type { RelayCallbacks } from "./relayClient.ts";
|
|
@@ -196,6 +197,14 @@ export class RemoteBridge {
|
|
|
196
197
|
private turnDriven = false;
|
|
197
198
|
private turnPrompt = "";
|
|
198
199
|
private turnText = "";
|
|
200
|
+
// Did this turn already put finish/aborted/error on the wire? The app lights its
|
|
201
|
+
// working pill the moment it SENDS a prompt and only ever puts it out on one of
|
|
202
|
+
// those three, but Pi's prompt() has paths that never run the agent at all — an
|
|
203
|
+
// extension command or input hook that handles the text, a throw on the
|
|
204
|
+
// no-model / no-key / auth precheck, "compaction in progress". Each returned (or
|
|
205
|
+
// threw into a catch that only printed locally) with no terminal event, and the
|
|
206
|
+
// driver watched a spinner nothing would ever clear. settleTurn closes the gap.
|
|
207
|
+
private turnTerminalSent = false;
|
|
199
208
|
|
|
200
209
|
// Unsubscribes the privacy watcher below. Held so a bridge that outlives its window
|
|
201
210
|
// (the desktop builds one per window, in one process) stops writing to a transport
|
|
@@ -232,6 +241,7 @@ export class RemoteBridge {
|
|
|
232
241
|
this.turnDriven = true;
|
|
233
242
|
this.turnPrompt = text;
|
|
234
243
|
this.turnText = "";
|
|
244
|
+
this.turnTerminalSent = false;
|
|
235
245
|
const attachments = this.pendingAttachments;
|
|
236
246
|
this.pendingAttachments = [];
|
|
237
247
|
this.cfg.onPrompt(text, attachments);
|
|
@@ -460,20 +470,33 @@ export class RemoteBridge {
|
|
|
460
470
|
// ── turn lifecycle + event forwarding ───────────────────────────────────────
|
|
461
471
|
|
|
462
472
|
// Mark the end of a turn so the next (possibly local) turn isn't treated as
|
|
463
|
-
// remote. Call after each driven turn completes
|
|
473
|
+
// remote. Call after each driven turn completes, passing the rejection message
|
|
474
|
+
// if prompt() threw.
|
|
475
|
+
//
|
|
476
|
+
// A DRIVEN turn always leaves here with exactly one terminal event on the wire
|
|
477
|
+
// (see turnTerminalSent): if the session never sent one, this sends it — `error`
|
|
478
|
+
// carrying the throw, or a plain `finish` for a prompt Pi handled without
|
|
479
|
+
// running the agent. Desktop's turnSupervisor emits its own terminal before it
|
|
480
|
+
// settles, so it is seen here and never doubled.
|
|
464
481
|
//
|
|
465
482
|
// Also the one moment we can tell whether the turn had an audience. If the app
|
|
466
483
|
// drove it and is now gone, everything the turn streamed up was dropped by the
|
|
467
484
|
// relay — so hand the answer to the owner for durable delivery rather than letting
|
|
468
485
|
// a completed piece of work evaporate because someone closed their phone.
|
|
469
|
-
settleTurn(): void {
|
|
486
|
+
settleTurn(error?: string): void {
|
|
470
487
|
const driven = this.turnDriven;
|
|
471
488
|
const prompt = this.turnPrompt;
|
|
472
489
|
const content = this.turnText.trim();
|
|
490
|
+
if (driven && !this.turnTerminalSent) {
|
|
491
|
+
this.forwardEvent(error
|
|
492
|
+
? { type: "error", error: redactText(error) }
|
|
493
|
+
: { type: "finish", usage: emptyUsage(), finishReason: "stop" });
|
|
494
|
+
}
|
|
473
495
|
this.remote = false;
|
|
474
496
|
this.turnDriven = false;
|
|
475
497
|
this.turnPrompt = "";
|
|
476
498
|
this.turnText = "";
|
|
499
|
+
this.turnTerminalSent = false;
|
|
477
500
|
if (!driven || !content) return;
|
|
478
501
|
// Unknown (a relay that can't report presence) counts as watched: better to skip
|
|
479
502
|
// delivery than to duplicate something the app already showed in its feed.
|
|
@@ -488,6 +511,7 @@ export class RemoteBridge {
|
|
|
488
511
|
// Keep the driven turn's reply as it streams, in case settleTurn finds nobody
|
|
489
512
|
// was there to read it. Text only: the outbox item is the answer, not a
|
|
490
513
|
// transcript of every tool call.
|
|
514
|
+
if (ev.type === "finish" || ev.type === "aborted" || ev.type === "error") this.turnTerminalSent = true;
|
|
491
515
|
if (this.turnDriven && ev.type === "text" && this.turnText.length < MAX_TURN_CAPTURE) {
|
|
492
516
|
this.turnText += String(ev.text ?? "");
|
|
493
517
|
}
|
|
@@ -522,12 +546,15 @@ export class RemoteBridge {
|
|
|
522
546
|
// way the gate wraps remoteAsk, so a silent or too-old app would wedge the turn
|
|
523
547
|
// forever without one.
|
|
524
548
|
saveCargoRemote = (req: CargoSaveRequest, signal?: AbortSignal): Promise<CargoSaveResult> => {
|
|
525
|
-
|
|
526
|
-
|
|
549
|
+
// The three "nobody to ask" answers carry `unattached`, which is what lets the tool
|
|
550
|
+
// queue the artifact for the app instead (cargoSave.ts). Nothing was sent, so there is
|
|
551
|
+
// no chance a queued copy duplicates one the app stored.
|
|
552
|
+
if (!this.relay) return Promise.resolve({ ok: false, unattached: true, reason: "remote access is not enabled — run /remote-access on and drive this terminal from the Privateer app" });
|
|
553
|
+
if (!this.relay.isConnected()) return Promise.resolve({ ok: false, unattached: true, reason: "the relay is not connected" });
|
|
527
554
|
// hasController is optional on RelayLike; a transport that can't tell is treated as
|
|
528
555
|
// attached, matching how the rest of the bridge reads it.
|
|
529
556
|
if (this.relay.hasController && !this.relay.hasController()) {
|
|
530
|
-
return Promise.resolve({ ok: false, reason: "the Privateer app is not attached to this terminal
|
|
557
|
+
return Promise.resolve({ ok: false, unattached: true, reason: "the Privateer app is not attached to this terminal" });
|
|
531
558
|
}
|
|
532
559
|
const id = randomUUID();
|
|
533
560
|
return new Promise<CargoSaveResult>((resolve) => {
|
package/src/tools/cargo.ts
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
// Cargo: a titled, runnable artifact they can open, edit, download and share
|
|
3
3
|
// from any of their devices, not a path that only exists on this machine.
|
|
4
4
|
//
|
|
5
|
-
// The interesting part is not this file, it's why the save
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
5
|
+
// The interesting part is not this file, it's why the save goes through the app
|
|
6
|
+
// at all — src/remote/cargoSave.ts has that (short version: the terminal holds no
|
|
7
|
+
// master key, and a Cargo row is ciphertext). Two routes follow from it: with an
|
|
8
|
+
// app attached, a live round trip that comes back with the stored row's id; with
|
|
9
|
+
// none, the artifact is sealed to the account and QUEUED for the next app to file.
|
|
10
|
+
// What matters here is the shape that follows:
|
|
9
11
|
//
|
|
10
12
|
// TAKES A PATH, NOT CONTENT. An artifact runs to half a megabyte. Inlining that
|
|
11
13
|
// into a tool call would spend the whole thing in tokens, twice — once when the
|
|
@@ -28,6 +30,11 @@
|
|
|
28
30
|
// generation's posture (media.ts's header is blunt about that). A model that
|
|
29
31
|
// can't tell the two apart will describe one with the other's guarantees, so the
|
|
30
32
|
// description states this one plainly and the success line repeats it.
|
|
33
|
+
//
|
|
34
|
+
// AND WHAT HASN'T HAPPENED YET. A queued save is not a saved one: it is in no
|
|
35
|
+
// Cargo list until an app opens and files it. The queued line says so in as many
|
|
36
|
+
// words, because "saved" would send the user to look for something that isn't
|
|
37
|
+
// there yet.
|
|
31
38
|
|
|
32
39
|
import { Type } from "typebox";
|
|
33
40
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
@@ -39,9 +46,11 @@ import {
|
|
|
39
46
|
isHtmlKind,
|
|
40
47
|
kindForExtension,
|
|
41
48
|
type CargoKind,
|
|
49
|
+
isUnattached,
|
|
42
50
|
type CargoSaveRequest,
|
|
43
51
|
type CargoSaveResult,
|
|
44
52
|
} from "../remote/cargoSave.ts";
|
|
53
|
+
import { postQueuedCargo, type QueuedCargoResult } from "../outbox/cloudOutbox.ts";
|
|
45
54
|
|
|
46
55
|
function text(t: string) {
|
|
47
56
|
return { content: [{ type: "text", text: t }], details: {} };
|
|
@@ -52,6 +61,9 @@ export interface CargoSaveBridge {
|
|
|
52
61
|
saveCargoRemote(req: CargoSaveRequest, signal?: AbortSignal): Promise<CargoSaveResult>;
|
|
53
62
|
}
|
|
54
63
|
|
|
64
|
+
/** Where an artifact goes when no app is attached. Injectable for tests. */
|
|
65
|
+
export type CargoQueue = (req: CargoSaveRequest) => Promise<QueuedCargoResult>;
|
|
66
|
+
|
|
55
67
|
export const CARGO_TOOL_NAMES = ["save_cargo"] as const;
|
|
56
68
|
|
|
57
69
|
/**
|
|
@@ -71,7 +83,11 @@ function kindMatchesExtension(kind: CargoKind, ext: string): boolean {
|
|
|
71
83
|
return implied === "md" && (kind === "md" || kind === "pdf" || kind === "docx");
|
|
72
84
|
}
|
|
73
85
|
|
|
74
|
-
|
|
86
|
+
/**
|
|
87
|
+
* `bridge` is the live route — absent in a session with no relay of its own (a harbor
|
|
88
|
+
* run, a channels session), which then only ever queues. `queue` is the other route.
|
|
89
|
+
*/
|
|
90
|
+
export function makeSaveCargoTool(bridge?: CargoSaveBridge, queue: CargoQueue = postQueuedCargo) {
|
|
75
91
|
return {
|
|
76
92
|
name: "save_cargo",
|
|
77
93
|
label: "Save to Cargo",
|
|
@@ -85,9 +101,11 @@ export function makeSaveCargoTool(bridge: CargoSaveBridge) {
|
|
|
85
101
|
"inlined, since it opens with no network and no sibling files), .md (kind md, pdf or docx — the " +
|
|
86
102
|
"kind picks what Download produces) and .csv (kind sheet). Max 512 KB; other file types cannot be " +
|
|
87
103
|
"Cargo — use send_file_to_client to hand the user an image, video or PDF instead.\n" +
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"
|
|
104
|
+
"Works whether or not the Privateer app is attached. Attached: the app encrypts and stores it now. " +
|
|
105
|
+
"Not attached: the artifact is sealed to the user's account key and queued, and the app files it the " +
|
|
106
|
+
"next time it opens — it is NOT in Cargo until then, so say that rather than that it is saved. Either " +
|
|
107
|
+
"way only the user's app can read it; the server holds ciphertext. That is a stronger guarantee than " +
|
|
108
|
+
"the generate_* tools have — do not describe those the same way.",
|
|
91
109
|
parameters: Type.Object({
|
|
92
110
|
path: Type.String({
|
|
93
111
|
description: "Path of the file to save, relative to cwd or absolute (e.g. 'build/tower-defence.html').",
|
|
@@ -165,11 +183,27 @@ export function makeSaveCargoTool(bridge: CargoSaveBridge) {
|
|
|
165
183
|
}
|
|
166
184
|
if (!content.trim()) return text(`${params.path} has no content to save.`);
|
|
167
185
|
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
signal
|
|
171
|
-
|
|
186
|
+
const req: CargoSaveRequest = { content, kind, title: params.title?.trim() || undefined };
|
|
187
|
+
const res: CargoSaveResult = bridge
|
|
188
|
+
? await bridge.saveCargoRemote(req, signal)
|
|
189
|
+
: { ok: false, unattached: true, reason: "no app is attached to this session" };
|
|
172
190
|
|
|
191
|
+
if (!res.ok && isUnattached(res)) {
|
|
192
|
+
const queued = await queue(req);
|
|
193
|
+
if (!queued.ok) {
|
|
194
|
+
return text(
|
|
195
|
+
`Couldn't save ${basename(target)} to Cargo. No Privateer app is attached (${res.reason}), ` +
|
|
196
|
+
`and queueing it for the app failed: ${queued.reason}.`,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
const label = req.title ? `"${req.title}"` : basename(target);
|
|
200
|
+
return text(
|
|
201
|
+
`Queued ${label} for Cargo (${kind}, ${(stat.size / 1024).toFixed(0)} KB). No Privateer app is attached right now, ` +
|
|
202
|
+
`so it was sealed to the user's account key and parked on the server as ciphertext only their app can open. ` +
|
|
203
|
+
`It will appear in Cargo the next time the Privateer app opens on any of their devices — it is not there yet, ` +
|
|
204
|
+
`so tell the user that rather than that it is saved.`,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
173
207
|
if (!res.ok) return text(`Couldn't save ${basename(target)} to Cargo: ${res.reason}`);
|
|
174
208
|
return text(
|
|
175
209
|
`Saved "${res.title}" to Cargo (${kind}, ${(stat.size / 1024).toFixed(0)} KB, ${res.storageType} storage, id ${res.cargoId}). ` +
|
|
@@ -18,18 +18,18 @@ import { makeChartTools, type ChartOpBridge } from "./charts.ts";
|
|
|
18
18
|
import { makeSaveToLibraryTool, type LibrarySaveBridge } from "./saveToLibrary.ts";
|
|
19
19
|
import type { AttachmentStore } from "../util/attachmentStore.ts";
|
|
20
20
|
|
|
21
|
-
// save_cargo rides with the file pair
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
// save_to_library rides here
|
|
21
|
+
// save_cargo rides with the file pair because, in a session that HAS a relay, it should
|
|
22
|
+
// try the attached app first: that path comes back with the stored row. With no app
|
|
23
|
+
// attached it queues the artifact for the next app to file instead (remote/cargoSave.ts),
|
|
24
|
+
// which is also how a session with no relay at all gets it — config/moat.ts registers a
|
|
25
|
+
// queue-only copy there.
|
|
26
|
+
// save_to_library rides here because it needs a CONNECTED
|
|
27
27
|
// APP rather than a signed-in account, because only the app holds the master key that
|
|
28
28
|
// a Library row's ciphertext is under. Putting it in the moat's media block would give
|
|
29
29
|
// it to every harbor and channels session, where there is no controller and every call
|
|
30
30
|
// would fail; an unattended run already delivers a file a different way, as a sealed
|
|
31
31
|
// attachment on its Inbox result (routines/resultMedia.ts).
|
|
32
|
-
// The chart tools ride here too, for
|
|
32
|
+
// The chart tools ride here too, for save_to_library's reason and one more of
|
|
33
33
|
// their own. Same reason: they need a CONNECTED APP, not a signed-in account, so the
|
|
34
34
|
// moat's media block would put them in every harbor and channels session where there is
|
|
35
35
|
// no controller and every call would fail. Their own reason: unlike cargo they also READ
|