@agent-marshal/marshal-pi 0.54.0 → 0.55.0
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/package.json +1 -1
- package/src/daemon.ts +24 -15
- package/src/entities.ts +4 -0
- package/src/inbound.ts +18 -0
- package/src/inbox-pull.ts +86 -0
- package/src/index.ts +22 -12
package/package.json
CHANGED
package/src/daemon.ts
CHANGED
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
type SessionNicknameItem,
|
|
42
42
|
} from "./entities.ts";
|
|
43
43
|
import type { Identity } from "./identity.ts";
|
|
44
|
+
import { InboxPuller, type InboxPull } from "./inbox-pull.ts";
|
|
44
45
|
|
|
45
46
|
const LIVENESS_INTERVAL_MS = 5_000;
|
|
46
47
|
const INBOX_PULL_LIMIT = 20;
|
|
@@ -82,7 +83,11 @@ export class MarshalDaemon {
|
|
|
82
83
|
private roster: SessionItem[] = [];
|
|
83
84
|
private nicknames: SessionNicknameItem[] = [];
|
|
84
85
|
|
|
85
|
-
private readonly
|
|
86
|
+
private readonly inbox = new InboxPuller<MessageView>({
|
|
87
|
+
read: (sessionId) => this.readUnread(sessionId),
|
|
88
|
+
ack: async (sessionId, messageIds) => { await this.sendForInbox(ackMessages(sessionId, messageIds)); },
|
|
89
|
+
render: (messages) => this.renderInbox(messages),
|
|
90
|
+
});
|
|
86
91
|
private notifyHandler: NotifyHandler | null = null;
|
|
87
92
|
private livenessTimer: ReturnType<typeof setInterval> | null = null;
|
|
88
93
|
private started = false;
|
|
@@ -254,13 +259,23 @@ export class MarshalDaemon {
|
|
|
254
259
|
return `- [${m.messageId}] ${this.senderLabel(m.fromSessionId)} [response_required=${m.responseRequired ?? false}]: ${m.body}`;
|
|
255
260
|
}
|
|
256
261
|
|
|
257
|
-
|
|
262
|
+
/**
|
|
263
|
+
* Read the unread inbox without acking it. Commit the pull once its text is
|
|
264
|
+
* in the conversation, or release it if it never gets there, so a message is
|
|
265
|
+
* never marked read without having been shown.
|
|
266
|
+
*/
|
|
267
|
+
pullInbox(sessionId: string): Promise<InboxPull | null> {
|
|
258
268
|
if (!this.connected) return Promise.resolve(null);
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
269
|
+
return this.inbox.pull(sessionId).catch(() => null);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Pull and commit in one step: the messages are acked before the caller
|
|
273
|
+
* shows them. Only the live-push path still uses it. */
|
|
274
|
+
async drainInbox(sessionId: string): Promise<string | null> {
|
|
275
|
+
const pull = await this.pullInbox(sessionId);
|
|
276
|
+
if (!pull) return null;
|
|
277
|
+
await pull.commit().catch(() => this.log("inbox ack failed"));
|
|
278
|
+
return pull.text;
|
|
264
279
|
}
|
|
265
280
|
|
|
266
281
|
nicknameFor(sessionId: string): string {
|
|
@@ -275,7 +290,7 @@ export class MarshalDaemon {
|
|
|
275
290
|
|
|
276
291
|
// ── Internals ──────────────────────────────────────────────────────────
|
|
277
292
|
|
|
278
|
-
private async
|
|
293
|
+
private async readUnread(sessionId: string): Promise<MessageView[] | null> {
|
|
279
294
|
let result: ReadMessagesResult | undefined;
|
|
280
295
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
281
296
|
try {
|
|
@@ -293,13 +308,7 @@ export class MarshalDaemon {
|
|
|
293
308
|
if (result.messages && result.messages.length > 0) break;
|
|
294
309
|
if (attempt < 2) await new Promise((r) => setTimeout(r, 100 * (attempt + 1)));
|
|
295
310
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const block = this.renderInbox(result.messages);
|
|
299
|
-
try {
|
|
300
|
-
await this.sendForInbox(ackMessages(sessionId, result.messages.map((m) => m.messageId)));
|
|
301
|
-
} catch { this.log("inbox ack failed"); }
|
|
302
|
-
return block;
|
|
311
|
+
return result?.messages?.length ? result.messages : null;
|
|
303
312
|
}
|
|
304
313
|
|
|
305
314
|
private renderInbox(messages: MessageView[]): string {
|
package/src/entities.ts
CHANGED
|
@@ -70,11 +70,15 @@ export type RoomMemberItem = RoomMember;
|
|
|
70
70
|
export interface NotifyChannelMeta {
|
|
71
71
|
source?: string;
|
|
72
72
|
kind?: string;
|
|
73
|
+
message_id?: string;
|
|
73
74
|
from_session?: string;
|
|
74
75
|
from_nickname?: string;
|
|
75
76
|
to_session?: string;
|
|
76
77
|
response_required?: boolean;
|
|
78
|
+
/** A preview capped at `CONTEXT_BODY_MAX_CHARS`; see `body_truncated`. */
|
|
77
79
|
body?: string;
|
|
80
|
+
/** Set when `body` was clipped. The durable message keeps the full body. */
|
|
81
|
+
body_truncated?: boolean;
|
|
78
82
|
sent_at?: number;
|
|
79
83
|
}
|
|
80
84
|
|
package/src/inbound.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Text a live push injects into the session. Kept free of pi and daemon
|
|
2
|
+
// imports so it can be tested directly.
|
|
3
|
+
|
|
4
|
+
import type { NotifyChannelMeta } from "./entities.ts";
|
|
5
|
+
|
|
6
|
+
/** The steer message for a live push, or `undefined` when the push carries no
|
|
7
|
+
* body (older daemons), in which case the caller drains the inbox instead.
|
|
8
|
+
*
|
|
9
|
+
* The daemon caps `body` at `CONTEXT_BODY_MAX_CHARS` and marks a delivered
|
|
10
|
+
* push read, so a clipped body is never completed by the inbox. Say so, and
|
|
11
|
+
* name the message so the agent can read the rest with marshal_messages. */
|
|
12
|
+
export function livePushText(meta: NotifyChannelMeta, who: string): string | undefined {
|
|
13
|
+
if (!meta.body) return undefined;
|
|
14
|
+
const text = `new message from ${who} [response_required=${meta.response_required ?? false}]: ${meta.body}`;
|
|
15
|
+
if (!meta.body_truncated) return text;
|
|
16
|
+
const id = meta.message_id ? ` (message ${meta.message_id})` : "";
|
|
17
|
+
return `${text}\n[preview truncated; read the full message with marshal_messages${id}]`;
|
|
18
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Inbox pulls that are acked only once their text has been shown.
|
|
2
|
+
//
|
|
3
|
+
// Acking in the same step as the read loses messages whenever the caller ends
|
|
4
|
+
// up not showing what it read: a per-turn pull that loses its timeout race, or
|
|
5
|
+
// an injection that fails. A pull here hands the messages back unacked; the
|
|
6
|
+
// caller commits it once the text is in the conversation, or releases it so
|
|
7
|
+
// the next pull hands the same messages out again.
|
|
8
|
+
|
|
9
|
+
/** Unread messages read but not yet acked. */
|
|
10
|
+
export interface InboxPull {
|
|
11
|
+
/** The rendered messages, ready to inject. */
|
|
12
|
+
readonly text: string;
|
|
13
|
+
/** Ack the messages. Call once `text` has been shown. */
|
|
14
|
+
commit(): Promise<void>;
|
|
15
|
+
/** Give the messages back unacked, so the next pull hands them out again. */
|
|
16
|
+
release(): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Where a pull reads, acks and renders messages. */
|
|
20
|
+
export interface InboxSource<M extends { messageId: string }> {
|
|
21
|
+
read(sessionId: string): Promise<M[] | null>;
|
|
22
|
+
ack(sessionId: string, messageIds: string[]): Promise<void>;
|
|
23
|
+
render(messages: M[]): string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class InboxPuller<M extends { messageId: string }> {
|
|
27
|
+
/** Per session, the ids an outstanding pull holds: still unread, so a read returns them. */
|
|
28
|
+
private readonly held = new Map<string, Set<string>>();
|
|
29
|
+
/** Per session, the pull in flight. Pulls run one at a time, so each sees what the last one holds. */
|
|
30
|
+
private readonly pulling = new Map<string, Promise<unknown>>();
|
|
31
|
+
|
|
32
|
+
private readonly source: InboxSource<M>;
|
|
33
|
+
|
|
34
|
+
constructor(source: InboxSource<M>) {
|
|
35
|
+
this.source = source;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
pull(sessionId: string): Promise<InboxPull | null> {
|
|
39
|
+
const previous = this.pulling.get(sessionId) ?? Promise.resolve();
|
|
40
|
+
const next = previous.catch(() => {}).then(() => this.pullNow(sessionId));
|
|
41
|
+
this.pulling.set(sessionId, next);
|
|
42
|
+
void next.catch(() => {}).finally(() => {
|
|
43
|
+
if (this.pulling.get(sessionId) === next) this.pulling.delete(sessionId);
|
|
44
|
+
});
|
|
45
|
+
return next;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private async pullNow(sessionId: string): Promise<InboxPull | null> {
|
|
49
|
+
const held = this.held.get(sessionId) ?? new Set<string>();
|
|
50
|
+
const messages = (await this.source.read(sessionId))?.filter((m) => !held.has(m.messageId));
|
|
51
|
+
if (!messages?.length) return null;
|
|
52
|
+
// Render before holding anything, so a render that throws holds nothing.
|
|
53
|
+
const text = this.source.render(messages);
|
|
54
|
+
const ids = messages.map((m) => m.messageId);
|
|
55
|
+
for (const id of ids) held.add(id);
|
|
56
|
+
this.held.set(sessionId, held);
|
|
57
|
+
const settle = () => { for (const id of ids) held.delete(id); };
|
|
58
|
+
return {
|
|
59
|
+
text,
|
|
60
|
+
commit: async () => {
|
|
61
|
+
try {
|
|
62
|
+
await this.source.ack(sessionId, ids);
|
|
63
|
+
} finally {
|
|
64
|
+
settle();
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
release: settle,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Pull for a turn, giving up after `timeoutMs` so the turn is never held up.
|
|
74
|
+
* A pull that lands after that is released, not acked: its messages were not
|
|
75
|
+
* shown, so they stay unread for the next turn.
|
|
76
|
+
*/
|
|
77
|
+
export async function pullForTurn(pull: () => Promise<InboxPull | null>, timeoutMs: number): Promise<InboxPull | null> {
|
|
78
|
+
const pending = pull().catch(() => null);
|
|
79
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
80
|
+
const deadline = new Promise<"late">((resolve) => { timer = setTimeout(() => resolve("late"), timeoutMs); });
|
|
81
|
+
const winner = await Promise.race([pending, deadline]);
|
|
82
|
+
clearTimeout(timer);
|
|
83
|
+
if (winner !== "late") return winner;
|
|
84
|
+
void pending.then((landed) => landed?.release());
|
|
85
|
+
return null;
|
|
86
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -26,8 +26,10 @@ import type {
|
|
|
26
26
|
import { Type } from "typebox";
|
|
27
27
|
|
|
28
28
|
import { MarshalDaemon } from "./daemon.ts";
|
|
29
|
+
import { pullForTurn } from "./inbox-pull.ts";
|
|
29
30
|
import { resolveIdentity, type Identity } from "./identity.ts";
|
|
30
31
|
import type { NotifyChannelMeta, SessionItem } from "./entities.ts";
|
|
32
|
+
import { livePushText } from "./inbound.ts";
|
|
31
33
|
|
|
32
34
|
const DEFAULT_ADDRESS = "ws://localhost:6155";
|
|
33
35
|
type MarshalToolResult = AgentToolResult<Record<string, unknown>>;
|
|
@@ -101,7 +103,7 @@ async function init(pi: ExtensionAPI) {
|
|
|
101
103
|
sessionCtx.ui.notify(`marshal: new message from ${who}`, "info");
|
|
102
104
|
}
|
|
103
105
|
|
|
104
|
-
const text = meta
|
|
106
|
+
const text = livePushText(meta, who);
|
|
105
107
|
void (async () => {
|
|
106
108
|
try {
|
|
107
109
|
if (text) {
|
|
@@ -260,9 +262,15 @@ async function init(pi: ExtensionAPI) {
|
|
|
260
262
|
// ── Agent state → marshal status ───────────────────────────────────────
|
|
261
263
|
|
|
262
264
|
pi.on("before_agent_start", async (event, _ctx) => {
|
|
265
|
+
// pi 1.2.0 also fires this for a run started by pi.sendMessage(...,
|
|
266
|
+
// { triggerTurn: true }), such as a live push. That event has messageType
|
|
267
|
+
// "custom" and no prompt. The run continues the last prompt's work, so it
|
|
268
|
+
// keeps that prompt's activity and status.
|
|
269
|
+
const prompt = (event as { prompt?: unknown }).prompt;
|
|
270
|
+
if (typeof prompt !== "string") return;
|
|
263
271
|
resetActivity();
|
|
264
|
-
lastPrompt =
|
|
265
|
-
pushMarshalStatus(`🤔 "${truncate(
|
|
272
|
+
lastPrompt = prompt;
|
|
273
|
+
pushMarshalStatus(`🤔 "${truncate(prompt, 80)}"`);
|
|
266
274
|
});
|
|
267
275
|
|
|
268
276
|
pi.on("agent_start", async (_event, _ctx) => {
|
|
@@ -309,19 +317,21 @@ async function init(pi: ExtensionAPI) {
|
|
|
309
317
|
// Add marshal system prompt context (once per turn, before the LLM call).
|
|
310
318
|
result.systemPrompt = event.systemPrompt + "\n\n" + SYSTEM_PROMPT_BLOCK;
|
|
311
319
|
|
|
312
|
-
//
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
+
// Pull any missed inbox messages — only if the daemon is connected. The
|
|
321
|
+
// pull is acked only once its text goes into this turn; one that misses
|
|
322
|
+
// the deadline is released, so its messages stay unread for the next turn.
|
|
323
|
+
const d = daemon;
|
|
324
|
+
const sid = sessionId;
|
|
325
|
+
if (d && sid && d.isConnected()) {
|
|
326
|
+
d.registerSession(sid); // self-heal a dropped roster row
|
|
327
|
+
const pull = await pullForTurn(() => d.pullInbox(sid), 2000);
|
|
328
|
+
if (pull) {
|
|
320
329
|
result.message = {
|
|
321
330
|
customType: "marshal-inbox",
|
|
322
|
-
content:
|
|
331
|
+
content: pull.text,
|
|
323
332
|
display: true,
|
|
324
333
|
};
|
|
334
|
+
void pull.commit().catch(() => log("inbox ack failed"));
|
|
325
335
|
}
|
|
326
336
|
}
|
|
327
337
|
|