@brainpilot/runtime 0.0.9 → 0.0.10
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/events.d.ts +18 -1
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +16 -0
- package/dist/events.js.map +1 -1
- package/dist/extensions/agent-status.d.ts +13 -11
- package/dist/extensions/agent-status.d.ts.map +1 -1
- package/dist/extensions/agent-status.js +37 -21
- package/dist/extensions/agent-status.js.map +1 -1
- package/dist/extensions/trace-reminder.d.ts +7 -19
- package/dist/extensions/trace-reminder.d.ts.map +1 -1
- package/dist/extensions/trace-reminder.js +57 -33
- package/dist/extensions/trace-reminder.js.map +1 -1
- package/dist/mas-agent.d.ts +2 -0
- package/dist/mas-agent.d.ts.map +1 -1
- package/dist/mas-agent.js +54 -3
- package/dist/mas-agent.js.map +1 -1
- package/dist/personas.d.ts.map +1 -1
- package/dist/personas.js +66 -14
- package/dist/personas.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +22 -9
- package/dist/server.js.map +1 -1
- package/dist/session-manager.d.ts +40 -1
- package/dist/session-manager.d.ts.map +1 -1
- package/dist/session-manager.js +116 -28
- package/dist/session-manager.js.map +1 -1
- package/dist/tools/kb/paths.d.ts +24 -0
- package/dist/tools/kb/paths.d.ts.map +1 -0
- package/dist/tools/kb/paths.js +69 -0
- package/dist/tools/kb/paths.js.map +1 -0
- package/dist/tools/kb/python.d.ts +2 -0
- package/dist/tools/kb/python.d.ts.map +1 -0
- package/dist/tools/kb/python.js +42 -0
- package/dist/tools/kb/python.js.map +1 -0
- package/dist/tools/kb/retrieve.d.ts +20 -0
- package/dist/tools/kb/retrieve.d.ts.map +1 -0
- package/dist/tools/kb/retrieve.js +122 -0
- package/dist/tools/kb/retrieve.js.map +1 -0
- package/dist/tools/kb/search-papers.d.ts +39 -0
- package/dist/tools/kb/search-papers.d.ts.map +1 -0
- package/dist/tools/kb/search-papers.js +188 -0
- package/dist/tools/kb/search-papers.js.map +1 -0
- package/dist/tools/kb/sidecar.d.ts +11 -0
- package/dist/tools/kb/sidecar.d.ts.map +1 -0
- package/dist/tools/kb/sidecar.js +161 -0
- package/dist/tools/kb/sidecar.js.map +1 -0
- package/dist/tools/kb/store.d.ts +45 -0
- package/dist/tools/kb/store.d.ts.map +1 -0
- package/dist/tools/kb/store.js +157 -0
- package/dist/tools/kb/store.js.map +1 -0
- package/dist/tools/kb/tools.d.ts +24 -0
- package/dist/tools/kb/tools.d.ts.map +1 -0
- package/dist/tools/kb/tools.js +192 -0
- package/dist/tools/kb/tools.js.map +1 -0
- package/dist/tools/system-tools.d.ts.map +1 -1
- package/dist/tools/system-tools.js +41 -6
- package/dist/tools/system-tools.js.map +1 -1
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/events.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AgUiEvent } from "@brainpilot/protocol";
|
|
1
|
+
import type { AgUiEvent, CompactionReason } from "@brainpilot/protocol";
|
|
2
2
|
interface Ctx {
|
|
3
3
|
sessionId: string;
|
|
4
4
|
agentName?: string;
|
|
@@ -58,6 +58,23 @@ export declare const ev: {
|
|
|
58
58
|
recoverable?: boolean;
|
|
59
59
|
}): AgUiEvent;
|
|
60
60
|
custom(ctx: Ctx, name: string, value: unknown): AgUiEvent;
|
|
61
|
+
/**
|
|
62
|
+
* Pi SDK auto/manual compaction — the runtime translates Pi's internal
|
|
63
|
+
* `compaction_start` / `compaction_end` events onto the AG-UI CUSTOM channel
|
|
64
|
+
* so clients can render a "context being compacted" indicator instead of
|
|
65
|
+
* silently seeing the assistant's history collapse. Wire form:
|
|
66
|
+
* { type:"CUSTOM", name:"compaction", value: { op:"start", reason, ... } }
|
|
67
|
+
*/
|
|
68
|
+
compactionStart(ctx: Ctx, reason: CompactionReason): AgUiEvent;
|
|
69
|
+
compactionEnd(ctx: Ctx, v: {
|
|
70
|
+
reason: CompactionReason;
|
|
71
|
+
aborted: boolean;
|
|
72
|
+
willRetry: boolean;
|
|
73
|
+
errorMessage?: string;
|
|
74
|
+
tokensBefore?: number;
|
|
75
|
+
estimatedTokensAfter?: number;
|
|
76
|
+
firstKeptEntryId?: string;
|
|
77
|
+
}): AgUiEvent;
|
|
61
78
|
};
|
|
62
79
|
export {};
|
|
63
80
|
//# sourceMappingURL=events.d.ts.map
|
package/dist/events.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACV,SAAS,EAET,gBAAgB,EAEjB,MAAM,sBAAsB,CAAC;AAO9B,UAAU,GAAG;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAgBD,wBAAgB,YAAY,IAAI,MAAM,CAErC;AACD,wBAAgB,QAAQ,IAAI,MAAM,CAEjC;AAED,eAAO,MAAM,EAAE;oBACG,GAAG,gBAAgB,MAAM,GAAG,SAAS;qBAGpC,GAAG,WAAW,OAAO,GAAG,SAAS;kBAGpC,GAAG,WAAW,MAAM,kBAA2B,SAAS;0BAGhD,GAAG,aAAa,MAAM,kBAAuB,SAAS;4BAGpD,GAAG,aAAa,MAAM,SAAS,MAAM,GAAG,SAAS;wBAGrD,GAAG,aAAa,MAAM,GAAG,SAAS;IAGtD;;;;;;;;;OASG;0BACmB,GAAG,aAAa,MAAM,SAAS,MAAM,kBAAuB,SAAS;+BAGhE,GAAG,aAAa,MAAM,GAAG,SAAS;iCAGhC,GAAG,aAAa,MAAM,SAAS,MAAM,GAAG,SAAS;6BAGrD,GAAG,a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,OAAO,CAAC;QACjB,SAAS,EAAE,OAAO,CAAC;QACnB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,oBAAoB,CAAC,EAAE,MAAM,CAAC;QAC9B,gBAAgB,CAAC,EAAE,MAAM,CAAC;KAC3B,GACA,SAAS;CAIb,CAAC"}
|
package/dist/events.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* `agent_name` + `_ts`; `run_id` when known.
|
|
9
9
|
*/
|
|
10
10
|
import { randomUUID } from "node:crypto";
|
|
11
|
+
import { CUSTOM_EVENT } from "@brainpilot/protocol";
|
|
11
12
|
function ts() {
|
|
12
13
|
return new Date().toISOString();
|
|
13
14
|
}
|
|
@@ -147,5 +148,20 @@ export const ev = {
|
|
|
147
148
|
custom(ctx, name, value) {
|
|
148
149
|
return { type: "CUSTOM", ...envelope(ctx), name, value };
|
|
149
150
|
},
|
|
151
|
+
/**
|
|
152
|
+
* Pi SDK auto/manual compaction — the runtime translates Pi's internal
|
|
153
|
+
* `compaction_start` / `compaction_end` events onto the AG-UI CUSTOM channel
|
|
154
|
+
* so clients can render a "context being compacted" indicator instead of
|
|
155
|
+
* silently seeing the assistant's history collapse. Wire form:
|
|
156
|
+
* { type:"CUSTOM", name:"compaction", value: { op:"start", reason, ... } }
|
|
157
|
+
*/
|
|
158
|
+
compactionStart(ctx, reason) {
|
|
159
|
+
const value = { op: "start", reason };
|
|
160
|
+
return { type: "CUSTOM", ...envelope(ctx), name: CUSTOM_EVENT.COMPACTION, value };
|
|
161
|
+
},
|
|
162
|
+
compactionEnd(ctx, v) {
|
|
163
|
+
const value = { op: "end", ...v };
|
|
164
|
+
return { type: "CUSTOM", ...envelope(ctx), name: CUSTOM_EVENT.COMPACTION, value };
|
|
165
|
+
},
|
|
150
166
|
};
|
|
151
167
|
//# sourceMappingURL=events.js.map
|
package/dist/events.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAOzC,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,SAAS,EAAE;IACT,OAAO,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AAClC,CAAC;AASD,SAAS,QAAQ,CAAC,GAAQ;IAOxB,MAAM,CAAC,GAAgC,EAAE,UAAU,EAAE,GAAG,CAAC,SAAS,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC;IAChF,IAAI,GAAG,CAAC,SAAS,KAAK,SAAS;QAAE,CAAC,CAAC,UAAU,GAAG,GAAG,CAAC,SAAS,CAAC;IAC9D,IAAI,GAAG,CAAC,KAAK,KAAK,SAAS;QAAE,CAAC,CAAC,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC;IAClD,IAAI,GAAG,CAAC,QAAQ,KAAK,SAAS;QAAE,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,QAAQ,CAAC;IAC3D,OAAO,CAAC,CAAC;AACX,CAAC;AAED,MAAM,UAAU,YAAY;IAC1B,OAAO,OAAO,UAAU,EAAE,EAAE,CAAC;AAC/B,CAAC;AACD,MAAM,UAAU,QAAQ;IACtB,OAAO,OAAO,UAAU,EAAE,EAAE,CAAC;AAC/B,CAAC;AAED,MAAM,CAAC,MAAM,EAAE,GAAG;IAChB,UAAU,CAAC,GAAQ,EAAE,WAAoB;QACvC,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,aAAa,EAAE,WAAW,EAAe,CAAC;IAC5F,CAAC;IACD,WAAW,CAAC,GAA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CAAC,UAAU;YAC1B,MAAM,EAAE,GAAG,CAAC,MAAM;SACN,CAAC;IACjB,CAAC;IACD,aAAa,CACX,SAAiB,EACjB,KAA6C,EAC7C,OAAe,EACf,IAAkE;QAElE,OAAO;YACL,IAAI,EAAE,gBAAgB;YACtB,UAAU,EAAE,SAAS;YACrB,KAAK,EAAE,IAAI,EAAE,KAAK;YAClB,KAAK;YACL,OAAO;YACP,OAAO,EAAE,IAAI,EAAE,OAAO;YACtB,SAAS,EAAE,EAAE,EAAE;YACf,WAAW,EAAE,IAAI,EAAE,WAAW,IAAI,IAAI;SAC1B,CAAC;IACjB,CAAC;IACD,MAAM,CAAC,GAAQ,EAAE,IAAY,EAAE,KAAc;QAC3C,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,KAAK,EAAe,CAAC;IACxE,CAAC;IACD;;;;;;OAMG;IACH,eAAe,CAAC,GAAQ,EAAE,MAAwB;QAChD,MAAM,KAAK,GAAyB,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;QAC5D,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,YAAY,CAAC,UAAU,EAAE,KAAK,EAAe,CAAC;IACjG,CAAC;IACD,aAAa,CACX,GAAQ,EACR,CAQC;QAED,MAAM,KAAK,GAAuB,EAAE,EAAE,EAAE,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC;QACtD,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,YAAY,CAAC,UAAU,EAAE,KAAK,EAAe,CAAC;IACjG,CAAC;CACF,CAAC"}
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
* - `context` handler receives `{ messages }` (a safe-to-mutate copy) and may
|
|
20
20
|
* return `{ messages }` to replace what the model sees this turn.
|
|
21
21
|
* - Because the host appends a NEW block each turn, we first STRIP any block we
|
|
22
|
-
* injected on a previous turn (identified by the
|
|
23
|
-
*
|
|
22
|
+
* injected on a previous turn (identified by the status-block opener) so the
|
|
23
|
+
* model only ever sees the latest snapshot, not a pile of old ones.
|
|
24
24
|
*
|
|
25
25
|
* Only the real factory loads this — the mock has no Pi event loop, so the
|
|
26
26
|
* behavioural injection is verified in real mode. The pure block renderer
|
|
@@ -36,13 +36,14 @@ export interface AgentStatusLine {
|
|
|
36
36
|
unread: number;
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
-
* Build the team-status block as
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
39
|
+
* Build the team-status block as an internal coordination hint. The wording
|
|
40
|
+
* tells the model not to surface this block to the user, while still showing
|
|
41
|
+
* active work or pending agent replies that may affect coordination. Returns ""
|
|
42
|
+
* when there is nothing to report (caller skips injection).
|
|
43
43
|
*
|
|
44
44
|
* `lines` should already be filtered by the caller (trace agent excluded,
|
|
45
|
-
* stopped agents excluded
|
|
45
|
+
* stopped agents excluded, idle agents with no pending messages excluded).
|
|
46
|
+
* Order is preserved.
|
|
46
47
|
*/
|
|
47
48
|
export declare function renderAgentStatusBlock(lines: readonly AgentStatusLine[]): string;
|
|
48
49
|
/** Minimal structural surface of an agent the collector reads. */
|
|
@@ -53,10 +54,11 @@ export interface StatusAgentLike {
|
|
|
53
54
|
}
|
|
54
55
|
/**
|
|
55
56
|
* Collect the status lines for a team snapshot from the live agents (#97).
|
|
56
|
-
* Includes every agent — INCLUDING the principal, so it sees
|
|
57
|
-
* except the trace agent (an internal recorder)
|
|
58
|
-
* (destroyed; irrelevant to coordination)
|
|
59
|
-
* messages
|
|
57
|
+
* Includes every active or pending agent — INCLUDING the principal, so it sees
|
|
58
|
+
* its own backlog — except the trace agent (an internal recorder), any stopped
|
|
59
|
+
* agent (destroyed; irrelevant to coordination), and idle agents with no unread
|
|
60
|
+
* messages. `unreadOf` returns the number of messages still queued unread in
|
|
61
|
+
* that agent's inbox. Order follows iteration.
|
|
60
62
|
*/
|
|
61
63
|
export declare function collectAgentStatusLines(agents: Iterable<StatusAgentLike>, unreadOf: (name: string) => number): AgentStatusLine[];
|
|
62
64
|
/** Minimal structural surface of Pi's ExtensionAPI we depend on. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-status.d.ts","sourceRoot":"","sources":["../../src/extensions/agent-status.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,2DAA2D;AAC3D,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,8DAA8D;IAC9D,MAAM,EAAE,MAAM,CAAC;IACf,kEAAkE;IAClE,MAAM,EAAE,MAAM,CAAC;CAChB;
|
|
1
|
+
{"version":3,"file":"agent-status.d.ts","sourceRoot":"","sources":["../../src/extensions/agent-status.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,2DAA2D;AAC3D,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,8DAA8D;IAC9D,MAAM,EAAE,MAAM,CAAC;IACf,kEAAkE;IAClE,MAAM,EAAE,MAAM,CAAC;CAChB;AAOD;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,SAAS,eAAe,EAAE,GAAG,MAAM,CAqBhF;AAED,kEAAkE;AAClE,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,QAAQ,CAAC,eAAe,CAAC,EACjC,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GACjC,eAAe,EAAE,CAUnB;AAED,oEAAoE;AACpE,UAAU,gBAAgB;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAChD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AACD,UAAU,cAAc;IACtB,EAAE,CACA,KAAK,EAAE,SAAS,EAChB,OAAO,EAAE,CAAC,CAAC,EAAE;QACX,QAAQ,EAAE,gBAAgB,EAAE,CAAC;KAC9B,KAAK;QAAE,QAAQ,EAAE,gBAAgB,EAAE,CAAA;KAAE,GAAG,IAAI,GAC5C,IAAI,CAAC;CACT;AAED,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,YAAY,EAAE,MAAM,MAAM,CAAC;CAC5B;AAeD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,eAAe,GAAG,CAAC,EAAE,EAAE,cAAc,KAAK,IAAI,CAoBtF"}
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
* - `context` handler receives `{ messages }` (a safe-to-mutate copy) and may
|
|
20
20
|
* return `{ messages }` to replace what the model sees this turn.
|
|
21
21
|
* - Because the host appends a NEW block each turn, we first STRIP any block we
|
|
22
|
-
* injected on a previous turn (identified by the
|
|
23
|
-
*
|
|
22
|
+
* injected on a previous turn (identified by the status-block opener) so the
|
|
23
|
+
* model only ever sees the latest snapshot, not a pile of old ones.
|
|
24
24
|
*
|
|
25
25
|
* Only the real factory loads this — the mock has no Pi event loop, so the
|
|
26
26
|
* behavioural injection is verified in real mode. The pure block renderer
|
|
@@ -28,38 +28,48 @@
|
|
|
28
28
|
* a fake `pi`.
|
|
29
29
|
*/
|
|
30
30
|
/** Opening tag — also the marker used to recognise a block we injected before. */
|
|
31
|
-
const TAG_OPEN = "<
|
|
32
|
-
const TAG_CLOSE = "</
|
|
31
|
+
const TAG_OPEN = "<internal_agent_status>";
|
|
32
|
+
const TAG_CLOSE = "</internal_agent_status>";
|
|
33
|
+
const LEGACY_TAG_OPEN = "<agent_status>";
|
|
33
34
|
/**
|
|
34
|
-
* Build the team-status block as
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
35
|
+
* Build the team-status block as an internal coordination hint. The wording
|
|
36
|
+
* tells the model not to surface this block to the user, while still showing
|
|
37
|
+
* active work or pending agent replies that may affect coordination. Returns ""
|
|
38
|
+
* when there is nothing to report (caller skips injection).
|
|
38
39
|
*
|
|
39
40
|
* `lines` should already be filtered by the caller (trace agent excluded,
|
|
40
|
-
* stopped agents excluded
|
|
41
|
+
* stopped agents excluded, idle agents with no pending messages excluded).
|
|
42
|
+
* Order is preserved.
|
|
41
43
|
*/
|
|
42
44
|
export function renderAgentStatusBlock(lines) {
|
|
43
|
-
|
|
45
|
+
const visibleLines = lines.filter((l) => l.status !== "idle" || l.unread > 0);
|
|
46
|
+
if (visibleLines.length === 0)
|
|
44
47
|
return "";
|
|
45
|
-
const body =
|
|
48
|
+
const body = visibleLines
|
|
46
49
|
.map((l) => {
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
const parts = [];
|
|
51
|
+
if (l.status !== "idle")
|
|
52
|
+
parts.push(`status=${l.status}`);
|
|
53
|
+
if (l.unread > 0) {
|
|
54
|
+
parts.push(`${l.unread} pending message${l.unread === 1 ? "" : "s"}`);
|
|
55
|
+
}
|
|
56
|
+
return `- ${l.name}: ${parts.join(", ")}`;
|
|
50
57
|
})
|
|
51
58
|
.join("\n");
|
|
52
59
|
return (`${TAG_OPEN}\n` +
|
|
53
|
-
`
|
|
60
|
+
`Internal coordination state. Use this only to decide whether to wait for pending agent work. ` +
|
|
61
|
+
`Never mention, quote, or summarize it in user-facing text.\n` +
|
|
62
|
+
`Pending or active agents:\n` +
|
|
54
63
|
`${body}\n` +
|
|
55
64
|
`${TAG_CLOSE}`);
|
|
56
65
|
}
|
|
57
66
|
/**
|
|
58
67
|
* Collect the status lines for a team snapshot from the live agents (#97).
|
|
59
|
-
* Includes every agent — INCLUDING the principal, so it sees
|
|
60
|
-
* except the trace agent (an internal recorder)
|
|
61
|
-
* (destroyed; irrelevant to coordination)
|
|
62
|
-
* messages
|
|
68
|
+
* Includes every active or pending agent — INCLUDING the principal, so it sees
|
|
69
|
+
* its own backlog — except the trace agent (an internal recorder), any stopped
|
|
70
|
+
* agent (destroyed; irrelevant to coordination), and idle agents with no unread
|
|
71
|
+
* messages. `unreadOf` returns the number of messages still queued unread in
|
|
72
|
+
* that agent's inbox. Order follows iteration.
|
|
63
73
|
*/
|
|
64
74
|
export function collectAgentStatusLines(agents, unreadOf) {
|
|
65
75
|
const lines = [];
|
|
@@ -68,14 +78,20 @@ export function collectAgentStatusLines(agents, unreadOf) {
|
|
|
68
78
|
continue;
|
|
69
79
|
if (a.status === "stopped")
|
|
70
80
|
continue;
|
|
71
|
-
|
|
81
|
+
const unread = unreadOf(a.name);
|
|
82
|
+
if (a.status === "idle" && unread === 0)
|
|
83
|
+
continue;
|
|
84
|
+
lines.push({ name: a.name, status: a.status, unread });
|
|
72
85
|
}
|
|
73
86
|
return lines;
|
|
74
87
|
}
|
|
75
88
|
/** True for a `user` message whose text is a status block we injected earlier. */
|
|
76
89
|
function isStatusMessage(m) {
|
|
77
90
|
return (m.role === "user" &&
|
|
78
|
-
m.content.some((c) =>
|
|
91
|
+
m.content.some((c) => {
|
|
92
|
+
const text = c.text ?? "";
|
|
93
|
+
return (c.type === "text" && (text.startsWith(TAG_OPEN) || text.startsWith(LEGACY_TAG_OPEN)));
|
|
94
|
+
}));
|
|
79
95
|
}
|
|
80
96
|
/**
|
|
81
97
|
* Build the extension factory for one agent. The returned function is what Pi
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-status.js","sourceRoot":"","sources":["../../src/extensions/agent-status.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAWH,kFAAkF;AAClF,MAAM,QAAQ,GAAG,
|
|
1
|
+
{"version":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AC,YAAY,EAAE,CAAC;YAClC,oEAAoE;YACpE,wEAAwE;YACxE,sBAAsB;YACtB,MAAM,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;YAC/D,MAAM,WAAW,GAAG,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC;YAE1D,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,oEAAoE;gBACpE,iEAAiE;gBACjE,OAAO,WAAW,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;YAC1D,CAAC;YAED,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YAC1E,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC;QAChC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
* - 意图二 (expert results flow back): if an expert run produced work but never
|
|
9
9
|
* send_message'd the principal, nudge; if it still won't, the host writes a
|
|
10
10
|
* fallback note into the principal's mailbox so the PI never dead-waits.
|
|
11
|
-
* - 意图三 (PI keeps to delegation): if the principal
|
|
12
|
-
* directly
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* - 意图三 (PI keeps to delegation): if the principal did substantive work
|
|
12
|
+
* directly (a write/run/external tool, not a read or coordination call)
|
|
13
|
+
* without delegating, nudge it at run end via a followUp (same channel as
|
|
14
|
+
* 意图一/二) — the tool result itself is left untouched.
|
|
15
15
|
*
|
|
16
16
|
* 意图一 and 意图二 are ORTHOGONAL checks (B): an expert can owe both a trace
|
|
17
17
|
* and a reply. They limit independently (A — separate counters), but when BOTH
|
|
@@ -29,8 +29,9 @@
|
|
|
29
29
|
* {deliverAs:"followUp"})` inside `agent_end`: it releases the current stop
|
|
30
30
|
* and starts a NEW agent loop (a fresh agent_start) — which is why the
|
|
31
31
|
* anti-loop counters must NOT reset on agent_start.
|
|
32
|
-
* -
|
|
33
|
-
*
|
|
32
|
+
* - Host-injected text (every followUp this file sends) is wrapped in a paired
|
|
33
|
+
* `[SYSTEM-MESSAGE:kind] … [/SYSTEM-MESSAGE]` marker via the `SYS()` helper, so
|
|
34
|
+
* a consumer can strip it with one regex and the wrapping has a single source.
|
|
34
35
|
*/
|
|
35
36
|
import type { AgentRole } from "../types.js";
|
|
36
37
|
/** Minimal structural surface of Pi's ExtensionAPI we depend on. */
|
|
@@ -44,19 +45,6 @@ interface PiExtensionApi {
|
|
|
44
45
|
toolName: string;
|
|
45
46
|
isError: boolean;
|
|
46
47
|
}) => void): void;
|
|
47
|
-
on(event: "tool_result", handler: (e: {
|
|
48
|
-
toolName: string;
|
|
49
|
-
isError: boolean;
|
|
50
|
-
content: Array<{
|
|
51
|
-
type: string;
|
|
52
|
-
text?: string;
|
|
53
|
-
}>;
|
|
54
|
-
}) => {
|
|
55
|
-
content: Array<{
|
|
56
|
-
type: "text";
|
|
57
|
-
text: string;
|
|
58
|
-
}>;
|
|
59
|
-
} | void): void;
|
|
60
48
|
on(event: "agent_end", handler: (e: AgentEndLike) => void): void;
|
|
61
49
|
sendUserMessage(content: string, options?: {
|
|
62
50
|
deliverAs?: "steer" | "followUp";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trace-reminder.d.ts","sourceRoot":"","sources":["../../src/extensions/trace-reminder.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"trace-reminder.d.ts","sourceRoot":"","sources":["../../src/extensions/trace-reminder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,KAAK,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Error(e) {
|
|
|
12
12
|
return false;
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
15
|
+
* Tools a principal may call directly WITHOUT it counting as "doing the work
|
|
16
|
+
* itself" (意图三 exemption): information-gathering / read-only tools plus the
|
|
17
|
+
* management/coordination tools. Anything NOT in this set — a write/edit/run, or
|
|
18
|
+
* any external MCP tool (domain work a principal should delegate) — counts as
|
|
19
|
+
* substantive work and arms the delegate reminder.
|
|
17
20
|
*/
|
|
18
|
-
const
|
|
21
|
+
const PI_ALLOWED_TOOLS = new Set([
|
|
22
|
+
// information-gathering / read-only. These MUST be Pi's real emitted tool
|
|
23
|
+
// names, which are lowercase (see BUILTIN_TOOL_CONFIG in tools/system-tools.ts:
|
|
24
|
+
// principal = ["read","write","edit","bash","grep","find","glob","ls"]). Names
|
|
25
|
+
// are matched case-insensitively below, but keep them lowercase to match the
|
|
26
|
+
// source of truth. (The principal has no WebFetch/WebSearch tool — omitted.)
|
|
27
|
+
"read",
|
|
28
|
+
"grep",
|
|
29
|
+
"glob",
|
|
30
|
+
"ls",
|
|
31
|
+
"find",
|
|
32
|
+
// management / coordination
|
|
19
33
|
"create_agent",
|
|
20
34
|
"destroy_agent",
|
|
21
35
|
"record_trace",
|
|
@@ -23,13 +37,22 @@ const MGMT_TOOLS = new Set([
|
|
|
23
37
|
"ask_user",
|
|
24
38
|
"get_trace_graph",
|
|
25
39
|
]);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Paired marker wrapping EVERY host-injected message, so a consumer can strip it
|
|
42
|
+
* with a single regex (`\[SYSTEM-MESSAGE.*?\][\s\S]*?\[/SYSTEM-MESSAGE\]`) and the
|
|
43
|
+
* wrapping style has one source of truth. `kind` sub-tags the message for
|
|
44
|
+
* filtering/metrics; the namespace is intentionally generic (not "REMINDER") so
|
|
45
|
+
* future host injections of any sort reuse the same envelope.
|
|
46
|
+
*/
|
|
47
|
+
const SYS = (kind, body) => `[SYSTEM-MESSAGE:${kind}] ${body} [/SYSTEM-MESSAGE]`;
|
|
48
|
+
const TRACE_REMINDER = SYS("trace", "You did substantive work this run but have not called record_trace. " +
|
|
49
|
+
"If this step is worth recording, call record_trace before finishing.");
|
|
50
|
+
const EXPERT_REPLY_REMINDER = SYS("reply", 'You have not returned your result to the Principal via send_message(to="principal", ...). ' +
|
|
51
|
+
"Please report back, otherwise the Principal will not receive your output.");
|
|
52
|
+
const MERGED_REMINDER = SYS("merged", "Before finishing this run: " +
|
|
53
|
+
'(1) return your result via send_message(to="principal", ...); ' +
|
|
54
|
+
"(2) record the key decision via record_trace.");
|
|
55
|
+
const DELEGATE_REMINDER = SYS("delegate", "As the Principal, substantive work should be delegated to an expert rather than done directly.");
|
|
33
56
|
/**
|
|
34
57
|
* Build the extension factory for one agent. The returned function is what Pi
|
|
35
58
|
* calls with the per-session `ExtensionAPI`.
|
|
@@ -45,21 +68,25 @@ export function makeTraceReminderExt(deps) {
|
|
|
45
68
|
let traced = false;
|
|
46
69
|
let replied = false;
|
|
47
70
|
let delegated = false;
|
|
48
|
-
|
|
71
|
+
// 意图三: did the principal do substantive (non-read, non-coordination) work
|
|
72
|
+
// directly this run? Set in tool_execution_end, read in agent_end.
|
|
73
|
+
let didSubstantiveWork = false;
|
|
49
74
|
// ⚠️ Cross-run-CHAIN counters — MUST NOT reset on agent_start/turn_start nor
|
|
50
75
|
// at the top of agent_end. sendUserMessage(followUp) starts a NEW agent loop
|
|
51
76
|
// (verified: a followUp fires a fresh agent_start) whose end re-enters
|
|
52
77
|
// agent_end; if these were cleared we would re-remind forever. They are reset
|
|
53
78
|
// ONLY at the terminal exits below (dimension satisfied, or already reminded
|
|
54
|
-
// once → fallback/let-go). Decoupled per dimension (A) so a trace reminder
|
|
55
|
-
// and a
|
|
79
|
+
// once → fallback/let-go). Decoupled per dimension (A) so a trace reminder,
|
|
80
|
+
// a reply reminder, and a delegate reminder limit independently. delegate
|
|
81
|
+
// moved here (from a run-scoped flag) because it now fires via followUp too.
|
|
56
82
|
let traceRemindCount = 0;
|
|
57
83
|
let replyRemindCount = 0;
|
|
84
|
+
let delegateRemindCount = 0;
|
|
58
85
|
pi.on("agent_start", () => {
|
|
59
86
|
traced = false;
|
|
60
87
|
replied = false;
|
|
61
88
|
delegated = false;
|
|
62
|
-
|
|
89
|
+
didSubstantiveWork = false;
|
|
63
90
|
});
|
|
64
91
|
// tool_execution_start: nothing required for accounting (we key off
|
|
65
92
|
// tool_execution_end which carries success/failure). Kept as a no-op anchor
|
|
@@ -74,25 +101,12 @@ export function makeTraceReminderExt(deps) {
|
|
|
74
101
|
replied = true;
|
|
75
102
|
if (t === "create_agent")
|
|
76
103
|
delegated = true;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
if (deps.role === "principal" &&
|
|
85
|
-
!delegated &&
|
|
86
|
-
!MGMT_TOOLS.has(e.toolName) &&
|
|
87
|
-
delegateRemindCount < 1) {
|
|
88
|
-
delegateRemindCount++;
|
|
89
|
-
suffix += `\n${DELEGATE_REMINDER}`;
|
|
90
|
-
}
|
|
91
|
-
if (!suffix)
|
|
92
|
-
return; // no rewrite — leave the result untouched
|
|
93
|
-
// Rewrite by appending to the existing text content; the tool still ran.
|
|
94
|
-
const text = e.content.map((c) => (c.type === "text" ? c.text ?? "" : "")).join("");
|
|
95
|
-
return { content: [{ type: "text", text: `${text}${suffix}` }] };
|
|
104
|
+
// 意图三: any successful call NOT in the allow-set (a write/run, or an
|
|
105
|
+
// external MCP/domain tool) is the principal doing the work itself. Match
|
|
106
|
+
// case-insensitively — Pi emits builtins lowercase, but normalizing here
|
|
107
|
+
// keeps the exemption working if a tool ever surfaces in another case.
|
|
108
|
+
if (deps.role === "principal" && !PI_ALLOWED_TOOLS.has(t.toLowerCase()))
|
|
109
|
+
didSubstantiveWork = true;
|
|
96
110
|
});
|
|
97
111
|
pi.on("agent_end", (e) => {
|
|
98
112
|
if (deps.role === "trace")
|
|
@@ -135,6 +149,16 @@ export function makeTraceReminderExt(deps) {
|
|
|
135
149
|
pi.sendUserMessage(TRACE_REMINDER, { deliverAs: "followUp" });
|
|
136
150
|
return;
|
|
137
151
|
}
|
|
152
|
+
// 意图三 (PI delegation): the principal did substantive work itself this run
|
|
153
|
+
// without delegating. Nudge once via followUp (NOT merged with trace —
|
|
154
|
+
// separate limiter, worst case one extra round-trip). Cross-run-chain
|
|
155
|
+
// limited like the others. Only reachable for a principal once its trace
|
|
156
|
+
// dimension is settled (a principal has no reply dimension).
|
|
157
|
+
if (deps.role === "principal" && didSubstantiveWork && !delegated && delegateRemindCount < 1) {
|
|
158
|
+
delegateRemindCount++;
|
|
159
|
+
pi.sendUserMessage(DELEGATE_REMINDER, { deliverAs: "followUp" });
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
138
162
|
// Nothing left to nudge (each lapsed dimension was already reminded once).
|
|
139
163
|
// Terminal handling per dimension:
|
|
140
164
|
// - reply has a host fallback so the principal never dead-waits;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trace-reminder.js","sourceRoot":"","sources":["../../src/extensions/trace-reminder.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"trace-reminder.js","sourceRoot":"","sources":["../../src/extensions/trace-reminder.ts"],"names":[],"mappings":"AA0DA,0EAA0E;AAC1E,SAAS,YAAY,CAAC,CAAe;IACnC,MAAM,IAAI,GAAG,CAAC,CAAC,QAAQ,CAAC;IACxB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IACvC,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1C,MAAM,CAAC,GAAG,IAA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AE,CAAC,CAAC;gBAC9D,OAAO;YACT,CAAC;YAED,0EAA0E;YAC1E,uEAAuE;YACvE,sEAAsE;YACtE,yEAAyE;YACzE,6DAA6D;YAC7D,IAAI,IAAI,CAAC,IAAI,KAAK,WAAW,IAAI,kBAAkB,IAAI,CAAC,SAAS,IAAI,mBAAmB,GAAG,CAAC,EAAE,CAAC;gBAC7F,mBAAmB,EAAE,CAAC;gBACtB,EAAE,CAAC,eAAe,CAAC,iBAAiB,EAAE,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC,CAAC;gBACjE,OAAO;YACT,CAAC;YAED,2EAA2E;YAC3E,mCAAmC;YACnC,kEAAkE;YAClE,+DAA+D;YAC/D,oEAAoE;YACpE,IAAI,SAAS,EAAE,CAAC;gBACd,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAC5B,gBAAgB,GAAG,CAAC,CAAC;YACvB,CAAC;YACD,IAAI,SAAS,EAAE,CAAC;gBACd,gBAAgB,GAAG,CAAC,CAAC;YACvB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/mas-agent.d.ts
CHANGED
|
@@ -118,6 +118,8 @@ export declare class MasAgent {
|
|
|
118
118
|
*/
|
|
119
119
|
abort(): Promise<void>;
|
|
120
120
|
stop(): void;
|
|
121
|
+
/** Emit a compaction-related system_message with the agent tag pre-filled. */
|
|
122
|
+
private emitCompactionSystemMessage;
|
|
121
123
|
private recordError;
|
|
122
124
|
/** Translate one Pi event into zero or more AG-UI events (§6). */
|
|
123
125
|
private onPiEvent;
|
package/dist/mas-agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mas-agent.d.ts","sourceRoot":"","sources":["../src/mas-agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,sBAAsB,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AAc,CAA6B;IACnD;;;;;OAKG;IACH,OAAO,CAAC,aAAa,CAA4B;gBAEpB,IAAI,EAAE,YAAY;IAS/C,IAAI,MAAM,IAAI,WAAW,CAExB;IAED;;;;OAIG;IACH,IAAI,aAAa,IAAI,cAAc,GAAG,SAAS,CAE9C;IAED;;;;OAIG;IACH,KAAK,IAAI,UAAU;IAInB;;;OAGG;IACH,SAAS,CAAC,CAAC,EAAE,UAAU,GAAG,SAAS,GAAG,IAAI;IAI1C,wCAAwC;IACxC,KAAK,IAAI,UAAU;IAQnB,OAAO,CAAC,SAAS;IAajB;;;OAGG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YASrB,SAAS;IAkCvB;;;;;;;;;;OAUG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAgB5B,IAAI,IAAI,IAAI;IAMZ,8EAA8E;IAC9E,OAAO,CAAC,2BAA2B;IAMnC,OAAO,CAAC,WAAW;IAgBnB,kEAAkE;IAClE,OAAO,CAAC,SAAS;IA0KjB,OAAO,CAAC,gBAAgB;CA+CzB;AAuBD,YAAY,EAAE,SAAS,EAAE,CAAC"}
|
package/dist/mas-agent.js
CHANGED
|
@@ -193,6 +193,10 @@ export class MasAgent {
|
|
|
193
193
|
this.session.dispose();
|
|
194
194
|
this.setStatus("stopped");
|
|
195
195
|
}
|
|
196
|
+
/** Emit a compaction-related system_message with the agent tag pre-filled. */
|
|
197
|
+
emitCompactionSystemMessage(level, message, details) {
|
|
198
|
+
this.bus.emit(ev.systemMessage(this.sessionId, level, message, { agent: this.name, details, recoverable: true }));
|
|
199
|
+
}
|
|
196
200
|
recordError(message, details, raw) {
|
|
197
201
|
const prev = this.lastError?.consecutiveCount ?? 0;
|
|
198
202
|
this.lastError = { message, timestamp: new Date().toISOString(), consecutiveCount: prev + 1 };
|
|
@@ -214,13 +218,51 @@ export class MasAgent {
|
|
|
214
218
|
case "agent_end":
|
|
215
219
|
case "turn_start":
|
|
216
220
|
case "turn_end":
|
|
217
|
-
case "compaction_start":
|
|
218
|
-
case "compaction_end":
|
|
219
221
|
case "queue_update":
|
|
220
|
-
// Internal / suppressed (§6 table: turn_*,
|
|
222
|
+
// Internal / suppressed (§6 table: turn_*, queue_update not exposed).
|
|
221
223
|
// Behavioural reactions to turn_*/agent_end now live in the Pi-native
|
|
222
224
|
// trace-reminder extension, not here.
|
|
223
225
|
return;
|
|
226
|
+
// Pi auto-compacts when context nears the model window (threshold) or an
|
|
227
|
+
// overflow error came back. Surface both edges on the AG-UI CUSTOM channel
|
|
228
|
+
// (structured, name:"compaction") plus a `system_message` so text-only UIs
|
|
229
|
+
// see them too.
|
|
230
|
+
case "compaction_start": {
|
|
231
|
+
const cs = e;
|
|
232
|
+
const reason = normalizeCompactionReason(cs.reason);
|
|
233
|
+
this.bus.emit(ev.compactionStart(ctx, reason));
|
|
234
|
+
this.emitCompactionSystemMessage("info", `🗜️ Agent ${this.name} 正在压缩上下文 (${COMPACTION_REASON_LABEL[reason]})…`);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
case "compaction_end": {
|
|
238
|
+
const ce = e;
|
|
239
|
+
const reason = normalizeCompactionReason(ce.reason);
|
|
240
|
+
const { aborted, willRetry, errorMessage } = ce;
|
|
241
|
+
const r = ce.result;
|
|
242
|
+
this.bus.emit(ev.compactionEnd(ctx, {
|
|
243
|
+
reason,
|
|
244
|
+
aborted: Boolean(aborted),
|
|
245
|
+
willRetry: Boolean(willRetry),
|
|
246
|
+
errorMessage,
|
|
247
|
+
tokensBefore: r?.tokensBefore,
|
|
248
|
+
estimatedTokensAfter: r?.estimatedTokensAfter,
|
|
249
|
+
firstKeptEntryId: r?.firstKeptEntryId,
|
|
250
|
+
}));
|
|
251
|
+
const label = COMPACTION_REASON_LABEL[reason];
|
|
252
|
+
if (errorMessage) {
|
|
253
|
+
this.emitCompactionSystemMessage("warning", `⚠️ Agent ${this.name} 上下文压缩失败 (${label})`, errorMessage);
|
|
254
|
+
}
|
|
255
|
+
else if (aborted) {
|
|
256
|
+
this.emitCompactionSystemMessage("info", `🛑 Agent ${this.name} 上下文压缩已中止`);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
const delta = r?.tokensBefore !== undefined && r?.estimatedTokensAfter !== undefined
|
|
260
|
+
? `:${r.tokensBefore.toLocaleString()} → ~${r.estimatedTokensAfter.toLocaleString()} tokens`
|
|
261
|
+
: "";
|
|
262
|
+
this.emitCompactionSystemMessage("info", `✅ Agent ${this.name} 上下文压缩完成 (${label})${delta}`);
|
|
263
|
+
}
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
224
266
|
case "message_start": {
|
|
225
267
|
const msg = e;
|
|
226
268
|
if (msg.message?.role === "assistant") {
|
|
@@ -354,6 +396,15 @@ export class MasAgent {
|
|
|
354
396
|
}
|
|
355
397
|
}
|
|
356
398
|
}
|
|
399
|
+
/** Coerce Pi's `reason` field to the wire enum. Defaults to "manual" on unknown. */
|
|
400
|
+
function normalizeCompactionReason(r) {
|
|
401
|
+
return r === "threshold" || r === "overflow" ? r : "manual";
|
|
402
|
+
}
|
|
403
|
+
const COMPACTION_REASON_LABEL = {
|
|
404
|
+
manual: "手动",
|
|
405
|
+
threshold: "接近上下文上限",
|
|
406
|
+
overflow: "上下文溢出",
|
|
407
|
+
};
|
|
357
408
|
function safeStringify(v) {
|
|
358
409
|
if (v === undefined || v === null)
|
|
359
410
|
return "";
|