@unieai/rabi-plan-mode 0.0.0-stage → 0.2.1-rc.3
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/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/index.js +418 -0
- package/lib/types/client.d.ts +10 -0
- package/lib/types/client.js +9 -0
- package/lib/types/index.d.ts +146 -0
- package/lib/types/index.js +421 -0
- package/lib/types/types.d.ts +47 -0
- package/lib/types/types.js +10 -0
- package/locale/en.json +5 -0
- package/locale/zh.json +6 -0
- package/package.json +54 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unieai/rabi-plan-mode
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Logged per-agent plan mode with deployment guidance, a direct slash command, and a user-reviewed exit
|
|
4
|
+
|
|
5
|
+
Part of Rabi, the agent harness UnieAI Code runs on. Installed as a dependency of `@unieai/rabi`;
|
|
6
|
+
not meant to be used on its own.
|
|
7
|
+
|
|
8
|
+
See LICENSE for the license and copyright of this package.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import { Service } from "@unieai/cordis";
|
|
2
|
+
import { brandString } from "@unieai/rabi-brand";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { createUserMessage } from "@unieai/rabi-llm";
|
|
5
|
+
import { defineTool } from "@unieai/rabi-tools";
|
|
6
|
+
import { UserQuestionError } from "@unieai/rabi-user-questions";
|
|
7
|
+
//#region lib/types/index.js
|
|
8
|
+
/**
|
|
9
|
+
* Plan mode is logged per-agent collaboration state: while active, a
|
|
10
|
+
* deployment-owned guidance section is included in each model request, and
|
|
11
|
+
* `exit_plan_mode` presents the completed plan for user review, while the
|
|
12
|
+
* `/plan off` command lets a user leave directly. Sandbox mode and approval
|
|
13
|
+
* policy enforce restrictions independently and do not read or write plan
|
|
14
|
+
* state.
|
|
15
|
+
*
|
|
16
|
+
* The `plan` projection folds the session log, so resume and fork restore the
|
|
17
|
+
* state. User selections remain pending until the next accepted in-turn
|
|
18
|
+
* pre-step. The service includes the selected state in the proposed step
|
|
19
|
+
* assembly, then appends `plan/mode` from `agent/pre-step` only when the step
|
|
20
|
+
* is accepted. Same-step request retries reuse their assembly.
|
|
21
|
+
*
|
|
22
|
+
* The exit tool remains registered while plan mode is inactive, so entering
|
|
23
|
+
* or leaving plan mode changes only the prompt section, not the request tool
|
|
24
|
+
* catalog.
|
|
25
|
+
*
|
|
26
|
+
* See packages/plan/plan-mode/README.md.
|
|
27
|
+
*
|
|
28
|
+
* @module @unieai/rabi-plan-mode
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* The model-facing exit tool's name. It stays registered while plan mode is
|
|
32
|
+
* inactive so the request tool catalog is stable across transitions.
|
|
33
|
+
*/
|
|
34
|
+
const EXIT_PLAN_MODE = "exit_plan_mode";
|
|
35
|
+
/** The review question's id, echoed in the answer this tool reads. */
|
|
36
|
+
const REVIEW_ID = "plan-review";
|
|
37
|
+
/** The review question's approve option label. */
|
|
38
|
+
const APPROVE_LABEL = "Approve";
|
|
39
|
+
/** The review question's keep-planning option label. */
|
|
40
|
+
const KEEP_PLANNING_LABEL = "Keep planning";
|
|
41
|
+
const EXIT_DESCRIPTION = "Use only in plan mode. Present your plan for the user's review and, on approval, leave plan mode. The user may approve (carry out the plan from your next step) or keep planning — their feedback comes back in the tool result; revise and present again.";
|
|
42
|
+
/** The plan's first markdown heading (any level), or `undefined` when it has none. */
|
|
43
|
+
function firstHeading(plan) {
|
|
44
|
+
for (const line of plan.split("\n")) {
|
|
45
|
+
const match = /^#{1,6}\s+(.+?)\s*$/.exec(line);
|
|
46
|
+
if (match) return match[1];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Validate deployment-owned plan guidance. Missing, blank, non-string, or
|
|
51
|
+
* unknown fields fail at plugin load rather than being ignored.
|
|
52
|
+
*
|
|
53
|
+
* @param config Raw plugin config.
|
|
54
|
+
* @returns A detached validated config.
|
|
55
|
+
*/
|
|
56
|
+
function resolveConfig(config) {
|
|
57
|
+
const section = config.section;
|
|
58
|
+
if (typeof section !== "string") throw new Error("PlanModeConfig needs a string `section`");
|
|
59
|
+
if (section.trim() === "") throw new Error("PlanModeConfig needs a non-empty `section`");
|
|
60
|
+
const unknown = Object.keys(config).filter((key) => key !== "section");
|
|
61
|
+
if (unknown.length > 0) throw new Error(`PlanModeConfig has unknown key(s) ${unknown.join(", ")} — config is { section }`);
|
|
62
|
+
return { section };
|
|
63
|
+
}
|
|
64
|
+
/** Projection of logged plan selections and committed mode. */
|
|
65
|
+
const planProjectionDefinition = {
|
|
66
|
+
key: "plan",
|
|
67
|
+
stateVersion: 3,
|
|
68
|
+
stateSchema: z.object({
|
|
69
|
+
active: z.boolean(),
|
|
70
|
+
wanted: z.boolean().nullable(),
|
|
71
|
+
running: z.object({
|
|
72
|
+
commandId: z.string(),
|
|
73
|
+
wanted: z.boolean()
|
|
74
|
+
}).strict().nullable(),
|
|
75
|
+
activeAtLastHeader: z.boolean().nullable()
|
|
76
|
+
}).strict(),
|
|
77
|
+
init: () => ({
|
|
78
|
+
active: false,
|
|
79
|
+
wanted: null,
|
|
80
|
+
running: null,
|
|
81
|
+
activeAtLastHeader: null
|
|
82
|
+
}),
|
|
83
|
+
apply: (state, event) => {
|
|
84
|
+
if (event.type === "command/run" && event.data.name === "plan") {
|
|
85
|
+
if (event.data.args === void 0) return state;
|
|
86
|
+
const wanted = event.data.args.trim() !== "off";
|
|
87
|
+
return {
|
|
88
|
+
...state,
|
|
89
|
+
running: {
|
|
90
|
+
commandId: event.data.commandId,
|
|
91
|
+
wanted
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (event.type === "command/done" && event.data.commandId === state.running?.commandId) {
|
|
96
|
+
const wanted = event.data.kind === "success" && state.running.wanted !== state.active ? state.running.wanted : null;
|
|
97
|
+
return {
|
|
98
|
+
...state,
|
|
99
|
+
wanted,
|
|
100
|
+
running: null
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
if (event.type === "plan/mode") return {
|
|
104
|
+
...state,
|
|
105
|
+
active: event.data.active,
|
|
106
|
+
wanted: null
|
|
107
|
+
};
|
|
108
|
+
if (event.type === "request/header") return {
|
|
109
|
+
...state,
|
|
110
|
+
activeAtLastHeader: state.active
|
|
111
|
+
};
|
|
112
|
+
return state;
|
|
113
|
+
},
|
|
114
|
+
wire: {
|
|
115
|
+
viewSchema: z.object({
|
|
116
|
+
active: z.boolean(),
|
|
117
|
+
pending: z.boolean()
|
|
118
|
+
}),
|
|
119
|
+
view: (state) => {
|
|
120
|
+
const wanted = state.running?.wanted ?? state.wanted;
|
|
121
|
+
return {
|
|
122
|
+
active: state.active,
|
|
123
|
+
pending: wanted !== null && wanted !== state.active
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* `ctx.planMode`: owns logged plan state, applies and narrates selected state at step start,
|
|
130
|
+
* the `plan:policy` section, the `/plan` command, and the stable exit tool.
|
|
131
|
+
* Client carriers expose the projection's cropped `{ active, pending }` view.
|
|
132
|
+
*/
|
|
133
|
+
var PlanModeController = class extends Service {
|
|
134
|
+
static inject = [
|
|
135
|
+
"tools",
|
|
136
|
+
"systemPrompt",
|
|
137
|
+
"sessionProjections"
|
|
138
|
+
];
|
|
139
|
+
/** Validated deployment-owned guidance. */
|
|
140
|
+
section;
|
|
141
|
+
/**
|
|
142
|
+
* Latest selection per session awaiting the next accepted in-turn pre-step.
|
|
143
|
+
* `narrate` is true for user selections and false for the exit tool, whose
|
|
144
|
+
* result already narrates the transition.
|
|
145
|
+
*/
|
|
146
|
+
pendingIntents = /* @__PURE__ */ new WeakMap();
|
|
147
|
+
constructor(ctx, config = { section: "" }) {
|
|
148
|
+
super(ctx, "planMode");
|
|
149
|
+
this.section = resolveConfig(config).section;
|
|
150
|
+
let disposed = false;
|
|
151
|
+
ctx.on("agent/pre-step", async ({ agent, signal }, next) => {
|
|
152
|
+
const decision = await next();
|
|
153
|
+
const pending = this.pendingIntents.get(agent.session);
|
|
154
|
+
if (decision.kind === "reject" || signal.aborted || pending === void 0) return decision;
|
|
155
|
+
const narration = this.narration(agent.session, pending.active);
|
|
156
|
+
try {
|
|
157
|
+
this.onBoundary(agent.session);
|
|
158
|
+
} catch (error) {
|
|
159
|
+
ctx.logger.warn("rabi-plan-mode: failed to append selected plan mode at step start: %o", error);
|
|
160
|
+
return decision;
|
|
161
|
+
}
|
|
162
|
+
return !pending.narrate || narration === void 0 ? decision : {
|
|
163
|
+
...decision,
|
|
164
|
+
messages: [...decision.messages, narration]
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
ctx.effect(() => () => {
|
|
168
|
+
disposed = true;
|
|
169
|
+
}, "rabi-plan-mode: close service lifetime");
|
|
170
|
+
ctx.systemPrompt.section({
|
|
171
|
+
name: "plan:policy",
|
|
172
|
+
order: ctx.systemPrompt.getSectionOrder("PLAN_POLICY"),
|
|
173
|
+
text: (context) => {
|
|
174
|
+
if (context.agent === void 0) return "";
|
|
175
|
+
return this.pendingIntents.get(context.agent.session)?.active ?? this.loggedActive(context.agent.session) ? this.section : "";
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
ctx.sessionProjections.register(planProjectionDefinition);
|
|
179
|
+
ctx.inject(["commands"], (commandCtx) => {
|
|
180
|
+
commandCtx.commands.register({
|
|
181
|
+
definitionId: brandString("@unieai/rabi-plan-mode"),
|
|
182
|
+
name: "plan",
|
|
183
|
+
description: "Enter or leave plan mode",
|
|
184
|
+
input: {
|
|
185
|
+
hint: "[off|message]",
|
|
186
|
+
attachments: true
|
|
187
|
+
},
|
|
188
|
+
handler: ({ agent, rawInput, attachments }) => {
|
|
189
|
+
const message = rawInput.trim();
|
|
190
|
+
if (message === "off" && attachments.length > 0) return {
|
|
191
|
+
kind: "error",
|
|
192
|
+
text: "Attachments cannot accompany /plan off."
|
|
193
|
+
};
|
|
194
|
+
if (message === "off") switch (this.set(agent, false)) {
|
|
195
|
+
case "committed": return {
|
|
196
|
+
kind: "success",
|
|
197
|
+
text: "Plan mode off."
|
|
198
|
+
};
|
|
199
|
+
case "queued": return {
|
|
200
|
+
kind: "success",
|
|
201
|
+
text: "Leaving plan mode (applies from the next step)."
|
|
202
|
+
};
|
|
203
|
+
case "cancelled": return {
|
|
204
|
+
kind: "success",
|
|
205
|
+
text: "Plan mode entry cancelled."
|
|
206
|
+
};
|
|
207
|
+
case "noop": return this.loggedActive(agent.session) ? {
|
|
208
|
+
kind: "success",
|
|
209
|
+
text: "Leaving plan mode (applies from the next step)."
|
|
210
|
+
} : {
|
|
211
|
+
kind: "success",
|
|
212
|
+
text: "Plan mode is already inactive."
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
const outcome = this.set(agent, true);
|
|
216
|
+
if (message !== "" || attachments.length > 0) agent.steer(createUserMessage({
|
|
217
|
+
content: [...attachments, ...message === "" ? [] : [{
|
|
218
|
+
type: "text",
|
|
219
|
+
text: message
|
|
220
|
+
}]],
|
|
221
|
+
source: { kind: "user" }
|
|
222
|
+
}));
|
|
223
|
+
return {
|
|
224
|
+
kind: "success",
|
|
225
|
+
text: outcome === "committed" ? "Plan mode on. Use /plan off to leave." : "Entering plan mode (applies from the next step). Use /plan off to leave."
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
ctx.tools.register(defineTool({
|
|
231
|
+
name: EXIT_PLAN_MODE,
|
|
232
|
+
description: EXIT_DESCRIPTION,
|
|
233
|
+
parameters: { plan: {
|
|
234
|
+
type: "string",
|
|
235
|
+
required: true,
|
|
236
|
+
description: "The complete plan, as markdown, starting with a # heading that names it."
|
|
237
|
+
} },
|
|
238
|
+
output: {
|
|
239
|
+
schema: {
|
|
240
|
+
type: "object",
|
|
241
|
+
additionalProperties: false,
|
|
242
|
+
properties: { approved: {
|
|
243
|
+
type: "boolean",
|
|
244
|
+
required: true
|
|
245
|
+
} }
|
|
246
|
+
},
|
|
247
|
+
render: (_args, { approved }) => [{
|
|
248
|
+
type: "text",
|
|
249
|
+
text: approved ? "Plan approved — plan mode exited; carry out the plan starting with your next step." : "The user dismissed the plan review to reply in their own words; plan mode remains active."
|
|
250
|
+
}]
|
|
251
|
+
},
|
|
252
|
+
execute: async (args, exec) => {
|
|
253
|
+
const agent = exec.agent;
|
|
254
|
+
if (agent === void 0) throw new Error(`${EXIT_PLAN_MODE} requires a calling agent (no session to switch)`);
|
|
255
|
+
if (!this.loggedActive(agent.session)) throw new Error(`${EXIT_PLAN_MODE} is only available in plan mode`);
|
|
256
|
+
if (!/^#\s+\S/.test(args.plan.trim())) throw new Error(`${EXIT_PLAN_MODE} requires a non-empty markdown plan starting with a # heading`);
|
|
257
|
+
const interaction = ctx.get("userQuestions");
|
|
258
|
+
if (interaction === void 0) throw new Error("no user-questions channel is available to review the plan; ask the user to switch the session mode instead");
|
|
259
|
+
let answer;
|
|
260
|
+
try {
|
|
261
|
+
answer = await interaction.ask({
|
|
262
|
+
questions: [{
|
|
263
|
+
id: REVIEW_ID,
|
|
264
|
+
header: "Plan review",
|
|
265
|
+
question: "Approve this plan and leave plan mode?",
|
|
266
|
+
detail: args.plan,
|
|
267
|
+
options: [{
|
|
268
|
+
label: APPROVE_LABEL,
|
|
269
|
+
description: "Leave plan mode; the plan is carried out from the next step."
|
|
270
|
+
}, {
|
|
271
|
+
label: KEEP_PLANNING_LABEL,
|
|
272
|
+
description: "Stay in plan mode; feedback goes back to the model."
|
|
273
|
+
}],
|
|
274
|
+
intent: {
|
|
275
|
+
kind: "plan-review",
|
|
276
|
+
approve: APPROVE_LABEL,
|
|
277
|
+
callId: exec.callId
|
|
278
|
+
}
|
|
279
|
+
}],
|
|
280
|
+
agent,
|
|
281
|
+
signal: exec.signal
|
|
282
|
+
});
|
|
283
|
+
} catch (cause) {
|
|
284
|
+
if (cause instanceof UserQuestionError && cause.code === "ASK_CANCELLED") {
|
|
285
|
+
exec.concludeTurn();
|
|
286
|
+
return { approved: false };
|
|
287
|
+
}
|
|
288
|
+
throw cause;
|
|
289
|
+
}
|
|
290
|
+
if (disposed) throw new Error("the plan-mode service was reloaded while the plan was under review; present the plan again");
|
|
291
|
+
const reviewItems = answer.answers.filter((entry) => entry.id === REVIEW_ID);
|
|
292
|
+
const item = reviewItems.length === 1 ? reviewItems[0] : void 0;
|
|
293
|
+
if (item?.selected.length !== 1 || item.selected[0] !== APPROVE_LABEL || item.custom !== void 0) {
|
|
294
|
+
const feedback = item?.custom ?? "";
|
|
295
|
+
throw new Error(feedback === "" ? "The user chose to keep planning; revise the plan and present it again." : `The user chose to keep planning; their feedback: ${feedback}`);
|
|
296
|
+
}
|
|
297
|
+
this.pendingIntents.set(agent.session, {
|
|
298
|
+
active: false,
|
|
299
|
+
narrate: false
|
|
300
|
+
});
|
|
301
|
+
return { approved: true };
|
|
302
|
+
},
|
|
303
|
+
presentCall: (args) => ({
|
|
304
|
+
card: "generic",
|
|
305
|
+
title: firstHeading(args.plan) ?? "Plan",
|
|
306
|
+
kind: "other",
|
|
307
|
+
content: [{
|
|
308
|
+
type: "text",
|
|
309
|
+
text: args.plan
|
|
310
|
+
}]
|
|
311
|
+
}),
|
|
312
|
+
presentResult: (_args, result) => ({
|
|
313
|
+
card: "generic",
|
|
314
|
+
title: "Plan review",
|
|
315
|
+
content: result.content
|
|
316
|
+
})
|
|
317
|
+
}));
|
|
318
|
+
}
|
|
319
|
+
loggedActive(session) {
|
|
320
|
+
return this.planState(session).active;
|
|
321
|
+
}
|
|
322
|
+
hasOpenTurn(session) {
|
|
323
|
+
const state = this.ctx.sessionProjections.stateOf(session, "turnBoundary");
|
|
324
|
+
if (state === void 0) throw new Error("plan-mode requires the turnBoundary session projection");
|
|
325
|
+
return state.openTurnStartSeq !== null;
|
|
326
|
+
}
|
|
327
|
+
loggedActiveAtLastHeader(session) {
|
|
328
|
+
return this.planState(session).activeAtLastHeader ?? void 0;
|
|
329
|
+
}
|
|
330
|
+
/** Read the required plan projection state or fail at the first service access. */
|
|
331
|
+
planState(session) {
|
|
332
|
+
const state = this.ctx.sessionProjections.stateOf(session, "plan");
|
|
333
|
+
if (state === void 0) throw new Error("plan-mode requires the plan session projection");
|
|
334
|
+
return state;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Read the logged plan state and any selected state awaiting the next
|
|
338
|
+
* accepted in-turn pre-step.
|
|
339
|
+
*
|
|
340
|
+
* @param agent The agent to read.
|
|
341
|
+
* @returns Current logged state plus a pending selection, when present.
|
|
342
|
+
*/
|
|
343
|
+
get(agent) {
|
|
344
|
+
const active = this.loggedActive(agent.session);
|
|
345
|
+
const pending = this.pendingIntents.get(agent.session);
|
|
346
|
+
return pending === void 0 ? { active } : {
|
|
347
|
+
active,
|
|
348
|
+
pending: pending.active
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Select whether plan mode should be active. Between turns the method
|
|
353
|
+
* appends the change immediately because no in-turn pre-step will run until
|
|
354
|
+
* another prompt starts a turn. The open-turn fold is the idle signal:
|
|
355
|
+
* agent status stays `running` through post-turn checkpointing, when no
|
|
356
|
+
* further in-turn pre-step runs. During an open turn the selection remains
|
|
357
|
+
* pending until the next accepted in-turn pre-step. Repeated selection of
|
|
358
|
+
* the current or already-pending state is a no-op.
|
|
359
|
+
*
|
|
360
|
+
* @param agent The agent to switch.
|
|
361
|
+
* @param active Whether plan mode should be active.
|
|
362
|
+
* @returns what happened: `committed` (logged now), `queued` (awaiting the
|
|
363
|
+
* next accepted in-turn pre-step), `cancelled` (an opposite pending selection
|
|
364
|
+
* was cleared; the logged state already matches), or `noop` (already in that
|
|
365
|
+
* state).
|
|
366
|
+
*/
|
|
367
|
+
set(agent, active) {
|
|
368
|
+
const session = agent.session;
|
|
369
|
+
if (active === (this.pendingIntents.get(session)?.active ?? this.loggedActive(session))) return "noop";
|
|
370
|
+
if (this.hasOpenTurn(session)) {
|
|
371
|
+
this.pendingIntents.set(session, {
|
|
372
|
+
active,
|
|
373
|
+
narrate: true
|
|
374
|
+
});
|
|
375
|
+
return this.loggedActive(session) === active ? "cancelled" : "queued";
|
|
376
|
+
}
|
|
377
|
+
if (active === this.loggedActive(session)) {
|
|
378
|
+
this.pendingIntents.delete(session);
|
|
379
|
+
return "cancelled";
|
|
380
|
+
}
|
|
381
|
+
session.append("plan/mode", { active });
|
|
382
|
+
this.pendingIntents.delete(session);
|
|
383
|
+
const narration = this.narration(session, active);
|
|
384
|
+
if (narration !== void 0) agent.inject(narration);
|
|
385
|
+
return "committed";
|
|
386
|
+
}
|
|
387
|
+
/** Append one pending selection before the next request assembly. */
|
|
388
|
+
onBoundary(session) {
|
|
389
|
+
const pending = this.pendingIntents.get(session);
|
|
390
|
+
if (pending === void 0) return;
|
|
391
|
+
const target = pending.active;
|
|
392
|
+
if (target === this.loggedActive(session)) {
|
|
393
|
+
this.pendingIntents.delete(session);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
session.append("plan/mode", { active: target });
|
|
397
|
+
this.pendingIntents.delete(session);
|
|
398
|
+
}
|
|
399
|
+
/** Build a user-switch notice when the last logged header described the other mode. */
|
|
400
|
+
narration(session, target) {
|
|
401
|
+
const told = this.loggedActiveAtLastHeader(session);
|
|
402
|
+
if (told === void 0 || told === target) return;
|
|
403
|
+
const text = target ? "The user switched this session to plan mode." : "The user switched this session back to the default mode.";
|
|
404
|
+
return createUserMessage({
|
|
405
|
+
content: [{
|
|
406
|
+
type: "text",
|
|
407
|
+
text
|
|
408
|
+
}],
|
|
409
|
+
source: {
|
|
410
|
+
kind: "plan-mode",
|
|
411
|
+
form: "notice",
|
|
412
|
+
summary: text
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
//#endregion
|
|
418
|
+
export { EXIT_PLAN_MODE, PlanModeController, PlanModeController as default, planProjectionDefinition, resolveConfig };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-namespace projection of the plan domain: a pure re-export of the package's
|
|
3
|
+
* types outlet. Client code imports ONLY the client namespace (repo
|
|
4
|
+
* discipline), so `./client` projects the same single-source content
|
|
5
|
+
* `./types` serves to host consumers — zero duplication.
|
|
6
|
+
*
|
|
7
|
+
* @module @unieai/rabi-plan-mode/client
|
|
8
|
+
*/
|
|
9
|
+
export type * from './types.ts';
|
|
10
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-namespace projection of the plan domain: a pure re-export of the package's
|
|
3
|
+
* types outlet. Client code imports ONLY the client namespace (repo
|
|
4
|
+
* discipline), so `./client` projects the same single-source content
|
|
5
|
+
* `./types` serves to host consumers — zero duplication.
|
|
6
|
+
*
|
|
7
|
+
* @module @unieai/rabi-plan-mode/client
|
|
8
|
+
*/
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan mode is logged per-agent collaboration state: while active, a
|
|
3
|
+
* deployment-owned guidance section is included in each model request, and
|
|
4
|
+
* `exit_plan_mode` presents the completed plan for user review, while the
|
|
5
|
+
* `/plan off` command lets a user leave directly. Sandbox mode and approval
|
|
6
|
+
* policy enforce restrictions independently and do not read or write plan
|
|
7
|
+
* state.
|
|
8
|
+
*
|
|
9
|
+
* The `plan` projection folds the session log, so resume and fork restore the
|
|
10
|
+
* state. User selections remain pending until the next accepted in-turn
|
|
11
|
+
* pre-step. The service includes the selected state in the proposed step
|
|
12
|
+
* assembly, then appends `plan/mode` from `agent/pre-step` only when the step
|
|
13
|
+
* is accepted. Same-step request retries reuse their assembly.
|
|
14
|
+
*
|
|
15
|
+
* The exit tool remains registered while plan mode is inactive, so entering
|
|
16
|
+
* or leaving plan mode changes only the prompt section, not the request tool
|
|
17
|
+
* catalog.
|
|
18
|
+
*
|
|
19
|
+
* See packages/plan/plan-mode/README.md.
|
|
20
|
+
*
|
|
21
|
+
* @module @unieai/rabi-plan-mode
|
|
22
|
+
*/
|
|
23
|
+
import { Context, Service } from '@unieai/cordis';
|
|
24
|
+
import { z as zod } from 'zod';
|
|
25
|
+
import type { Agent } from '@unieai/rabi-agent';
|
|
26
|
+
import type { ContextFormed } from '@unieai/rabi-llm';
|
|
27
|
+
import type { PlanProjection, PlanUnitState } from './types.ts';
|
|
28
|
+
declare module '@unieai/rabi-llm' {
|
|
29
|
+
interface MessageSourceMap {
|
|
30
|
+
'plan-mode': {
|
|
31
|
+
kind: 'plan-mode';
|
|
32
|
+
} & ContextFormed;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export type * from './types.ts';
|
|
36
|
+
declare module '@unieai/rabi-session/types' {
|
|
37
|
+
interface SessionEventMap {
|
|
38
|
+
/**
|
|
39
|
+
* Whether plan mode is in force from this point on: log-only, non-surface,
|
|
40
|
+
* whole-value replace. The last `plan/mode` wins; a log with none folds to
|
|
41
|
+
* inactive through the projection unit's fold.
|
|
42
|
+
*/
|
|
43
|
+
'plan/mode': {
|
|
44
|
+
active: boolean;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
declare module '@unieai/cordis' {
|
|
49
|
+
interface Context {
|
|
50
|
+
planMode: PlanModeController;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The model-facing exit tool's name. It stays registered while plan mode is
|
|
55
|
+
* inactive so the request tool catalog is stable across transitions.
|
|
56
|
+
*/
|
|
57
|
+
export declare const EXIT_PLAN_MODE = "exit_plan_mode";
|
|
58
|
+
/** Deployment-owned plan guidance. */
|
|
59
|
+
export interface PlanModeConfig {
|
|
60
|
+
/** Guidance rendered as the `plan:policy` prompt section while plan mode is active. */
|
|
61
|
+
section: string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Validate deployment-owned plan guidance. Missing, blank, non-string, or
|
|
65
|
+
* unknown fields fail at plugin load rather than being ignored.
|
|
66
|
+
*
|
|
67
|
+
* @param config Raw plugin config.
|
|
68
|
+
* @returns A detached validated config.
|
|
69
|
+
*/
|
|
70
|
+
export declare function resolveConfig(config: PlanModeConfig): PlanModeConfig;
|
|
71
|
+
/** Projection of logged plan selections and committed mode. */
|
|
72
|
+
export declare const planProjectionDefinition: {
|
|
73
|
+
key: "plan";
|
|
74
|
+
stateVersion: number;
|
|
75
|
+
stateSchema: zod.ZodType<PlanUnitState, unknown, zod.core.$ZodTypeInternals<PlanUnitState, unknown>>;
|
|
76
|
+
init: () => {
|
|
77
|
+
active: false;
|
|
78
|
+
wanted: null;
|
|
79
|
+
running: null;
|
|
80
|
+
activeAtLastHeader: null;
|
|
81
|
+
};
|
|
82
|
+
apply: (state: NoInfer<PlanUnitState>, event: import("@unieai/rabi-session").SessionEvent) => PlanUnitState;
|
|
83
|
+
wire: {
|
|
84
|
+
viewSchema: zod.ZodType<PlanProjection, unknown, zod.core.$ZodTypeInternals<PlanProjection, unknown>>;
|
|
85
|
+
view: (state: NoInfer<PlanUnitState>) => {
|
|
86
|
+
active: boolean;
|
|
87
|
+
pending: boolean;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* `ctx.planMode`: owns logged plan state, applies and narrates selected state at step start,
|
|
93
|
+
* the `plan:policy` section, the `/plan` command, and the stable exit tool.
|
|
94
|
+
* Client carriers expose the projection's cropped `{ active, pending }` view.
|
|
95
|
+
*/
|
|
96
|
+
export declare class PlanModeController extends Service {
|
|
97
|
+
static inject: string[];
|
|
98
|
+
/** Validated deployment-owned guidance. */
|
|
99
|
+
private readonly section;
|
|
100
|
+
/**
|
|
101
|
+
* Latest selection per session awaiting the next accepted in-turn pre-step.
|
|
102
|
+
* `narrate` is true for user selections and false for the exit tool, whose
|
|
103
|
+
* result already narrates the transition.
|
|
104
|
+
*/
|
|
105
|
+
private readonly pendingIntents;
|
|
106
|
+
constructor(ctx: Context, config?: PlanModeConfig);
|
|
107
|
+
private loggedActive;
|
|
108
|
+
private hasOpenTurn;
|
|
109
|
+
private loggedActiveAtLastHeader;
|
|
110
|
+
/** Read the required plan projection state or fail at the first service access. */
|
|
111
|
+
private planState;
|
|
112
|
+
/**
|
|
113
|
+
* Read the logged plan state and any selected state awaiting the next
|
|
114
|
+
* accepted in-turn pre-step.
|
|
115
|
+
*
|
|
116
|
+
* @param agent The agent to read.
|
|
117
|
+
* @returns Current logged state plus a pending selection, when present.
|
|
118
|
+
*/
|
|
119
|
+
get(agent: Agent): {
|
|
120
|
+
active: boolean;
|
|
121
|
+
pending?: boolean;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Select whether plan mode should be active. Between turns the method
|
|
125
|
+
* appends the change immediately because no in-turn pre-step will run until
|
|
126
|
+
* another prompt starts a turn. The open-turn fold is the idle signal:
|
|
127
|
+
* agent status stays `running` through post-turn checkpointing, when no
|
|
128
|
+
* further in-turn pre-step runs. During an open turn the selection remains
|
|
129
|
+
* pending until the next accepted in-turn pre-step. Repeated selection of
|
|
130
|
+
* the current or already-pending state is a no-op.
|
|
131
|
+
*
|
|
132
|
+
* @param agent The agent to switch.
|
|
133
|
+
* @param active Whether plan mode should be active.
|
|
134
|
+
* @returns what happened: `committed` (logged now), `queued` (awaiting the
|
|
135
|
+
* next accepted in-turn pre-step), `cancelled` (an opposite pending selection
|
|
136
|
+
* was cleared; the logged state already matches), or `noop` (already in that
|
|
137
|
+
* state).
|
|
138
|
+
*/
|
|
139
|
+
set(agent: Agent, active: boolean): 'committed' | 'queued' | 'cancelled' | 'noop';
|
|
140
|
+
/** Append one pending selection before the next request assembly. */
|
|
141
|
+
private onBoundary;
|
|
142
|
+
/** Build a user-switch notice when the last logged header described the other mode. */
|
|
143
|
+
private narration;
|
|
144
|
+
}
|
|
145
|
+
export default PlanModeController;
|
|
146
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan mode is logged per-agent collaboration state: while active, a
|
|
3
|
+
* deployment-owned guidance section is included in each model request, and
|
|
4
|
+
* `exit_plan_mode` presents the completed plan for user review, while the
|
|
5
|
+
* `/plan off` command lets a user leave directly. Sandbox mode and approval
|
|
6
|
+
* policy enforce restrictions independently and do not read or write plan
|
|
7
|
+
* state.
|
|
8
|
+
*
|
|
9
|
+
* The `plan` projection folds the session log, so resume and fork restore the
|
|
10
|
+
* state. User selections remain pending until the next accepted in-turn
|
|
11
|
+
* pre-step. The service includes the selected state in the proposed step
|
|
12
|
+
* assembly, then appends `plan/mode` from `agent/pre-step` only when the step
|
|
13
|
+
* is accepted. Same-step request retries reuse their assembly.
|
|
14
|
+
*
|
|
15
|
+
* The exit tool remains registered while plan mode is inactive, so entering
|
|
16
|
+
* or leaving plan mode changes only the prompt section, not the request tool
|
|
17
|
+
* catalog.
|
|
18
|
+
*
|
|
19
|
+
* See packages/plan/plan-mode/README.md.
|
|
20
|
+
*
|
|
21
|
+
* @module @unieai/rabi-plan-mode
|
|
22
|
+
*/
|
|
23
|
+
import { Service } from '@unieai/cordis';
|
|
24
|
+
import { brandString } from '@unieai/rabi-brand';
|
|
25
|
+
import { z as zod } from 'zod';
|
|
26
|
+
import { createUserMessage } from '@unieai/rabi-llm';
|
|
27
|
+
import { defineTool } from '@unieai/rabi-tools';
|
|
28
|
+
import { UserQuestionError } from '@unieai/rabi-user-questions';
|
|
29
|
+
/**
|
|
30
|
+
* The model-facing exit tool's name. It stays registered while plan mode is
|
|
31
|
+
* inactive so the request tool catalog is stable across transitions.
|
|
32
|
+
*/
|
|
33
|
+
export const EXIT_PLAN_MODE = 'exit_plan_mode';
|
|
34
|
+
/** The review question's id, echoed in the answer this tool reads. */
|
|
35
|
+
const REVIEW_ID = 'plan-review';
|
|
36
|
+
/** The review question's approve option label. */
|
|
37
|
+
const APPROVE_LABEL = 'Approve';
|
|
38
|
+
/** The review question's keep-planning option label. */
|
|
39
|
+
const KEEP_PLANNING_LABEL = 'Keep planning';
|
|
40
|
+
const EXIT_DESCRIPTION = 'Use only in plan mode. Present your plan for the user\'s review and, on approval, leave plan mode. '
|
|
41
|
+
+ 'The user may approve (carry out the plan from your next step) or keep '
|
|
42
|
+
+ 'planning — their feedback comes back in the tool result; revise and present again.';
|
|
43
|
+
/** The plan's first markdown heading (any level), or `undefined` when it has none. */
|
|
44
|
+
function firstHeading(plan) {
|
|
45
|
+
for (const line of plan.split('\n')) {
|
|
46
|
+
const match = /^#{1,6}\s+(.+?)\s*$/.exec(line);
|
|
47
|
+
if (match)
|
|
48
|
+
return match[1];
|
|
49
|
+
}
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Validate deployment-owned plan guidance. Missing, blank, non-string, or
|
|
54
|
+
* unknown fields fail at plugin load rather than being ignored.
|
|
55
|
+
*
|
|
56
|
+
* @param config Raw plugin config.
|
|
57
|
+
* @returns A detached validated config.
|
|
58
|
+
*/
|
|
59
|
+
export function resolveConfig(config) {
|
|
60
|
+
const section = config.section;
|
|
61
|
+
if (typeof section !== 'string') {
|
|
62
|
+
throw new Error('PlanModeConfig needs a string `section`');
|
|
63
|
+
}
|
|
64
|
+
if (section.trim() === '') {
|
|
65
|
+
throw new Error('PlanModeConfig needs a non-empty `section`');
|
|
66
|
+
}
|
|
67
|
+
const unknown = Object.keys(config).filter(key => key !== 'section');
|
|
68
|
+
if (unknown.length > 0) {
|
|
69
|
+
throw new Error(`PlanModeConfig has unknown key(s) ${unknown.join(', ')} — config is { section }`);
|
|
70
|
+
}
|
|
71
|
+
return { section };
|
|
72
|
+
}
|
|
73
|
+
const planUnitStateSchema = zod.object({
|
|
74
|
+
active: zod.boolean(),
|
|
75
|
+
wanted: zod.boolean().nullable(),
|
|
76
|
+
running: zod.object({
|
|
77
|
+
commandId: zod.string(),
|
|
78
|
+
wanted: zod.boolean(),
|
|
79
|
+
}).strict().nullable(),
|
|
80
|
+
activeAtLastHeader: zod.boolean().nullable(),
|
|
81
|
+
}).strict();
|
|
82
|
+
/** Wire payload schema of the `plan` projection. */
|
|
83
|
+
const planProjectionSchema = zod.object({
|
|
84
|
+
active: zod.boolean(),
|
|
85
|
+
pending: zod.boolean(),
|
|
86
|
+
});
|
|
87
|
+
/** Projection of logged plan selections and committed mode. */
|
|
88
|
+
export const planProjectionDefinition = {
|
|
89
|
+
key: 'plan',
|
|
90
|
+
stateVersion: 3,
|
|
91
|
+
stateSchema: planUnitStateSchema,
|
|
92
|
+
init: () => ({ active: false, wanted: null, running: null, activeAtLastHeader: null }),
|
|
93
|
+
apply: (state, event) => {
|
|
94
|
+
if (event.type === 'command/run' && event.data.name === 'plan') {
|
|
95
|
+
if (event.data.args === undefined)
|
|
96
|
+
return state;
|
|
97
|
+
const wanted = event.data.args.trim() !== 'off';
|
|
98
|
+
return { ...state, running: { commandId: event.data.commandId, wanted } };
|
|
99
|
+
}
|
|
100
|
+
if (event.type === 'command/done' && event.data.commandId === state.running?.commandId) {
|
|
101
|
+
const wanted = event.data.kind === 'success' && state.running.wanted !== state.active
|
|
102
|
+
? state.running.wanted
|
|
103
|
+
: null;
|
|
104
|
+
return { ...state, wanted, running: null };
|
|
105
|
+
}
|
|
106
|
+
if (event.type === 'plan/mode') {
|
|
107
|
+
return { ...state, active: event.data.active, wanted: null };
|
|
108
|
+
}
|
|
109
|
+
if (event.type === 'request/header') {
|
|
110
|
+
return { ...state, activeAtLastHeader: state.active };
|
|
111
|
+
}
|
|
112
|
+
return state;
|
|
113
|
+
},
|
|
114
|
+
wire: {
|
|
115
|
+
viewSchema: planProjectionSchema,
|
|
116
|
+
view: (state) => {
|
|
117
|
+
const wanted = state.running?.wanted ?? state.wanted;
|
|
118
|
+
return { active: state.active, pending: wanted !== null && wanted !== state.active };
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* `ctx.planMode`: owns logged plan state, applies and narrates selected state at step start,
|
|
124
|
+
* the `plan:policy` section, the `/plan` command, and the stable exit tool.
|
|
125
|
+
* Client carriers expose the projection's cropped `{ active, pending }` view.
|
|
126
|
+
*/
|
|
127
|
+
export class PlanModeController extends Service {
|
|
128
|
+
static inject = ['tools', 'systemPrompt', 'sessionProjections'];
|
|
129
|
+
/** Validated deployment-owned guidance. */
|
|
130
|
+
section;
|
|
131
|
+
/**
|
|
132
|
+
* Latest selection per session awaiting the next accepted in-turn pre-step.
|
|
133
|
+
* `narrate` is true for user selections and false for the exit tool, whose
|
|
134
|
+
* result already narrates the transition.
|
|
135
|
+
*/
|
|
136
|
+
pendingIntents = new WeakMap();
|
|
137
|
+
constructor(ctx, config = { section: '' }) {
|
|
138
|
+
super(ctx, 'planMode');
|
|
139
|
+
this.section = resolveConfig(config).section;
|
|
140
|
+
let disposed = false;
|
|
141
|
+
// Pre-step is outside Session.append publication, so it can append the
|
|
142
|
+
// log-only mode event inside an open turn without re-entering the session.
|
|
143
|
+
// A failed append remains pending for a later accepted in-turn pre-step,
|
|
144
|
+
// and policy cannot block the step.
|
|
145
|
+
ctx.on('agent/pre-step', async ({ agent, signal }, next) => {
|
|
146
|
+
const decision = await next();
|
|
147
|
+
const pending = this.pendingIntents.get(agent.session);
|
|
148
|
+
if (decision.kind === 'reject' || signal.aborted || pending === undefined)
|
|
149
|
+
return decision;
|
|
150
|
+
const narration = this.narration(agent.session, pending.active);
|
|
151
|
+
try {
|
|
152
|
+
this.onBoundary(agent.session);
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
ctx.logger.warn('rabi-plan-mode: failed to append selected plan mode at step start: %o', error);
|
|
156
|
+
return decision;
|
|
157
|
+
}
|
|
158
|
+
return !pending.narrate || narration === undefined
|
|
159
|
+
? decision
|
|
160
|
+
: { ...decision, messages: [...decision.messages, narration] };
|
|
161
|
+
});
|
|
162
|
+
ctx.effect(() => () => { disposed = true; }, 'rabi-plan-mode: close service lifetime');
|
|
163
|
+
ctx.systemPrompt.section({
|
|
164
|
+
name: 'plan:policy',
|
|
165
|
+
order: ctx.systemPrompt.getSectionOrder('PLAN_POLICY'),
|
|
166
|
+
text: (context) => {
|
|
167
|
+
if (context.agent === undefined)
|
|
168
|
+
return '';
|
|
169
|
+
const pending = this.pendingIntents.get(context.agent.session);
|
|
170
|
+
return (pending?.active ?? this.loggedActive(context.agent.session)) ? this.section : '';
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
ctx.sessionProjections.register(planProjectionDefinition);
|
|
174
|
+
// The command child activates only when a command registry is composed.
|
|
175
|
+
ctx.inject(['commands'], (commandCtx) => {
|
|
176
|
+
commandCtx.commands.register({
|
|
177
|
+
definitionId: brandString('@unieai/rabi-plan-mode'),
|
|
178
|
+
name: 'plan',
|
|
179
|
+
description: 'Enter or leave plan mode',
|
|
180
|
+
input: { hint: '[off|message]', attachments: true },
|
|
181
|
+
handler: ({ agent, rawInput, attachments }) => {
|
|
182
|
+
const message = rawInput.trim();
|
|
183
|
+
if (message === 'off' && attachments.length > 0) {
|
|
184
|
+
return { kind: 'error', text: 'Attachments cannot accompany /plan off.' };
|
|
185
|
+
}
|
|
186
|
+
if (message === 'off') {
|
|
187
|
+
switch (this.set(agent, false)) {
|
|
188
|
+
case 'committed':
|
|
189
|
+
return { kind: 'success', text: 'Plan mode off.' };
|
|
190
|
+
case 'queued':
|
|
191
|
+
return { kind: 'success', text: 'Leaving plan mode (applies from the next step).' };
|
|
192
|
+
case 'cancelled':
|
|
193
|
+
return { kind: 'success', text: 'Plan mode entry cancelled.' };
|
|
194
|
+
case 'noop':
|
|
195
|
+
// Repeat the queued wording while an exit still awaits the
|
|
196
|
+
// next accepted pre-step; only a truly inactive session reads
|
|
197
|
+
// idempotent.
|
|
198
|
+
return this.loggedActive(agent.session)
|
|
199
|
+
? { kind: 'success', text: 'Leaving plan mode (applies from the next step).' }
|
|
200
|
+
: { kind: 'success', text: 'Plan mode is already inactive.' };
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const outcome = this.set(agent, true);
|
|
204
|
+
if (message !== '' || attachments.length > 0) {
|
|
205
|
+
agent.steer(createUserMessage({
|
|
206
|
+
content: [
|
|
207
|
+
...attachments,
|
|
208
|
+
...(message === '' ? [] : [{ type: 'text', text: message }]),
|
|
209
|
+
],
|
|
210
|
+
source: { kind: 'user' },
|
|
211
|
+
}));
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
kind: 'success',
|
|
215
|
+
text: outcome === 'committed'
|
|
216
|
+
? 'Plan mode on. Use /plan off to leave.'
|
|
217
|
+
: 'Entering plan mode (applies from the next step). Use /plan off to leave.',
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
ctx.tools.register(defineTool({
|
|
223
|
+
name: EXIT_PLAN_MODE,
|
|
224
|
+
description: EXIT_DESCRIPTION,
|
|
225
|
+
parameters: {
|
|
226
|
+
plan: { type: 'string', required: true, description: 'The complete plan, as markdown, starting with a # heading that names it.' },
|
|
227
|
+
},
|
|
228
|
+
output: {
|
|
229
|
+
schema: {
|
|
230
|
+
type: 'object',
|
|
231
|
+
additionalProperties: false,
|
|
232
|
+
properties: {
|
|
233
|
+
approved: { type: 'boolean', required: true },
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
render: (_args, { approved }) => [{
|
|
237
|
+
type: 'text',
|
|
238
|
+
text: approved
|
|
239
|
+
? 'Plan approved — plan mode exited; carry out the plan starting with your next step.'
|
|
240
|
+
: 'The user dismissed the plan review to reply in their own words; plan mode remains active.',
|
|
241
|
+
}],
|
|
242
|
+
},
|
|
243
|
+
execute: async (args, exec) => {
|
|
244
|
+
const agent = exec.agent;
|
|
245
|
+
if (agent === undefined)
|
|
246
|
+
throw new Error(`${EXIT_PLAN_MODE} requires a calling agent (no session to switch)`);
|
|
247
|
+
if (!this.loggedActive(agent.session)) {
|
|
248
|
+
throw new Error(`${EXIT_PLAN_MODE} is only available in plan mode`);
|
|
249
|
+
}
|
|
250
|
+
if (!/^#\s+\S/.test(args.plan.trim())) {
|
|
251
|
+
throw new Error(`${EXIT_PLAN_MODE} requires a non-empty markdown plan starting with a # heading`);
|
|
252
|
+
}
|
|
253
|
+
const interaction = ctx.get('userQuestions');
|
|
254
|
+
if (interaction === undefined) {
|
|
255
|
+
throw new Error('no user-questions channel is available to review the plan; ask the user to switch the session mode instead');
|
|
256
|
+
}
|
|
257
|
+
let answer;
|
|
258
|
+
try {
|
|
259
|
+
answer = await interaction.ask({
|
|
260
|
+
questions: [{
|
|
261
|
+
id: REVIEW_ID,
|
|
262
|
+
header: 'Plan review',
|
|
263
|
+
question: 'Approve this plan and leave plan mode?',
|
|
264
|
+
detail: args.plan,
|
|
265
|
+
options: [
|
|
266
|
+
{ label: APPROVE_LABEL, description: 'Leave plan mode; the plan is carried out from the next step.' },
|
|
267
|
+
{ label: KEEP_PLANNING_LABEL, description: 'Stay in plan mode; feedback goes back to the model.' },
|
|
268
|
+
],
|
|
269
|
+
// Presentation only: a capable UI renders the plan as a review
|
|
270
|
+
// decision instead of a generic question, and answers with one of
|
|
271
|
+
// the labels above either way.
|
|
272
|
+
intent: { kind: 'plan-review', approve: APPROVE_LABEL, callId: exec.callId },
|
|
273
|
+
}],
|
|
274
|
+
agent,
|
|
275
|
+
signal: exec.signal,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
catch (cause) {
|
|
279
|
+
// A dismissed review is not a failed one: the user took the turn back
|
|
280
|
+
// to say something the two options do not cover, so the turn ends
|
|
281
|
+
// here instead of spending a model request on waiting. An abort (turn
|
|
282
|
+
// cancel, provider teardown) keeps its own failure.
|
|
283
|
+
if (cause instanceof UserQuestionError && cause.code === 'ASK_CANCELLED') {
|
|
284
|
+
exec.concludeTurn();
|
|
285
|
+
return { approved: false };
|
|
286
|
+
}
|
|
287
|
+
throw cause;
|
|
288
|
+
}
|
|
289
|
+
// A review may outlive this plugin fiber. Without its pre-step listener,
|
|
290
|
+
// an approved selection could never be appended, so fail and keep planning.
|
|
291
|
+
if (disposed) {
|
|
292
|
+
throw new Error('the plan-mode service was reloaded while the plan was under review; present the plan again');
|
|
293
|
+
}
|
|
294
|
+
const reviewItems = answer.answers.filter(entry => entry.id === REVIEW_ID);
|
|
295
|
+
const item = reviewItems.length === 1 ? reviewItems[0] : undefined;
|
|
296
|
+
if (item?.selected.length !== 1 || item.selected[0] !== APPROVE_LABEL || item.custom !== undefined) {
|
|
297
|
+
const feedback = item?.custom ?? '';
|
|
298
|
+
throw new Error(feedback === ''
|
|
299
|
+
? 'The user chose to keep planning; revise the plan and present it again.'
|
|
300
|
+
: `The user chose to keep planning; their feedback: ${feedback}`);
|
|
301
|
+
}
|
|
302
|
+
// Keep plan guidance for the rest of this assistant tool batch. The
|
|
303
|
+
// silent selection is appended at the next accepted in-turn pre-step,
|
|
304
|
+
// before its request assembly.
|
|
305
|
+
this.pendingIntents.set(agent.session, { active: false, narrate: false });
|
|
306
|
+
return { approved: true };
|
|
307
|
+
},
|
|
308
|
+
presentCall: args => ({
|
|
309
|
+
card: 'generic',
|
|
310
|
+
title: firstHeading(args.plan) ?? 'Plan',
|
|
311
|
+
kind: 'other',
|
|
312
|
+
content: [{ type: 'text', text: args.plan }],
|
|
313
|
+
}),
|
|
314
|
+
presentResult: (_args, result) => ({
|
|
315
|
+
card: 'generic',
|
|
316
|
+
title: 'Plan review',
|
|
317
|
+
content: result.content,
|
|
318
|
+
}),
|
|
319
|
+
}));
|
|
320
|
+
}
|
|
321
|
+
loggedActive(session) {
|
|
322
|
+
return this.planState(session).active;
|
|
323
|
+
}
|
|
324
|
+
hasOpenTurn(session) {
|
|
325
|
+
const state = this.ctx.sessionProjections.stateOf(session, 'turnBoundary');
|
|
326
|
+
if (state === undefined)
|
|
327
|
+
throw new Error('plan-mode requires the turnBoundary session projection');
|
|
328
|
+
return state.openTurnStartSeq !== null;
|
|
329
|
+
}
|
|
330
|
+
loggedActiveAtLastHeader(session) {
|
|
331
|
+
return this.planState(session).activeAtLastHeader ?? undefined;
|
|
332
|
+
}
|
|
333
|
+
/** Read the required plan projection state or fail at the first service access. */
|
|
334
|
+
planState(session) {
|
|
335
|
+
const state = this.ctx.sessionProjections.stateOf(session, 'plan');
|
|
336
|
+
if (state === undefined)
|
|
337
|
+
throw new Error('plan-mode requires the plan session projection');
|
|
338
|
+
return state;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Read the logged plan state and any selected state awaiting the next
|
|
342
|
+
* accepted in-turn pre-step.
|
|
343
|
+
*
|
|
344
|
+
* @param agent The agent to read.
|
|
345
|
+
* @returns Current logged state plus a pending selection, when present.
|
|
346
|
+
*/
|
|
347
|
+
get(agent) {
|
|
348
|
+
const active = this.loggedActive(agent.session);
|
|
349
|
+
const pending = this.pendingIntents.get(agent.session);
|
|
350
|
+
return pending === undefined ? { active } : { active, pending: pending.active };
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Select whether plan mode should be active. Between turns the method
|
|
354
|
+
* appends the change immediately because no in-turn pre-step will run until
|
|
355
|
+
* another prompt starts a turn. The open-turn fold is the idle signal:
|
|
356
|
+
* agent status stays `running` through post-turn checkpointing, when no
|
|
357
|
+
* further in-turn pre-step runs. During an open turn the selection remains
|
|
358
|
+
* pending until the next accepted in-turn pre-step. Repeated selection of
|
|
359
|
+
* the current or already-pending state is a no-op.
|
|
360
|
+
*
|
|
361
|
+
* @param agent The agent to switch.
|
|
362
|
+
* @param active Whether plan mode should be active.
|
|
363
|
+
* @returns what happened: `committed` (logged now), `queued` (awaiting the
|
|
364
|
+
* next accepted in-turn pre-step), `cancelled` (an opposite pending selection
|
|
365
|
+
* was cleared; the logged state already matches), or `noop` (already in that
|
|
366
|
+
* state).
|
|
367
|
+
*/
|
|
368
|
+
set(agent, active) {
|
|
369
|
+
const session = agent.session;
|
|
370
|
+
const pending = this.pendingIntents.get(session);
|
|
371
|
+
const target = pending?.active ?? this.loggedActive(session);
|
|
372
|
+
if (active === target)
|
|
373
|
+
return 'noop';
|
|
374
|
+
if (this.hasOpenTurn(session)) {
|
|
375
|
+
this.pendingIntents.set(session, { active, narrate: true });
|
|
376
|
+
return this.loggedActive(session) === active ? 'cancelled' : 'queued';
|
|
377
|
+
}
|
|
378
|
+
// No open turn: commit now. Delete only after append succeeds so a
|
|
379
|
+
// failed durable write leaves the selection retryable, not dropped.
|
|
380
|
+
if (active === this.loggedActive(session)) {
|
|
381
|
+
this.pendingIntents.delete(session);
|
|
382
|
+
return 'cancelled';
|
|
383
|
+
}
|
|
384
|
+
session.append('plan/mode', { active });
|
|
385
|
+
this.pendingIntents.delete(session);
|
|
386
|
+
const narration = this.narration(session, active);
|
|
387
|
+
if (narration !== undefined)
|
|
388
|
+
agent.inject(narration);
|
|
389
|
+
return 'committed';
|
|
390
|
+
}
|
|
391
|
+
/** Append one pending selection before the next request assembly. */
|
|
392
|
+
onBoundary(session) {
|
|
393
|
+
const pending = this.pendingIntents.get(session);
|
|
394
|
+
if (pending === undefined)
|
|
395
|
+
return;
|
|
396
|
+
const target = pending.active;
|
|
397
|
+
if (target === this.loggedActive(session)) {
|
|
398
|
+
this.pendingIntents.delete(session);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
session.append('plan/mode', { active: target });
|
|
402
|
+
// Delete only after append succeeds so a later accepted in-turn pre-step
|
|
403
|
+
// can retry a failed durable write.
|
|
404
|
+
this.pendingIntents.delete(session);
|
|
405
|
+
}
|
|
406
|
+
/** Build a user-switch notice when the last logged header described the other mode. */
|
|
407
|
+
narration(session, target) {
|
|
408
|
+
const told = this.loggedActiveAtLastHeader(session);
|
|
409
|
+
if (told === undefined || told === target)
|
|
410
|
+
return;
|
|
411
|
+
const text = target
|
|
412
|
+
? 'The user switched this session to plan mode.'
|
|
413
|
+
: 'The user switched this session back to the default mode.';
|
|
414
|
+
return createUserMessage({
|
|
415
|
+
content: [{ type: 'text', text }],
|
|
416
|
+
// The narration is already one sentence, so it is its own summary.
|
|
417
|
+
source: { kind: 'plan-mode', form: 'notice', summary: text },
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
export default PlanModeController;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure types of the plan domain: the ONE home of the `plan` projection-key
|
|
3
|
+
* declaration, free of this package's host-side value imports (cordis,
|
|
4
|
+
* rabi-tools, rabi-agent). Two namespace projections serve it — `./types` for
|
|
5
|
+
* host consumers and `./client` for client aggregates — with zero content
|
|
6
|
+
* duplication.
|
|
7
|
+
*
|
|
8
|
+
* @module @unieai/rabi-plan-mode/types
|
|
9
|
+
*/
|
|
10
|
+
import type { CommandId } from '@unieai/rabi-commands/brand';
|
|
11
|
+
/**
|
|
12
|
+
* The plan projection's wire value. `active` is the logged state in force
|
|
13
|
+
* (the last `plan/mode`, inactive before the first); `pending` is true while
|
|
14
|
+
* a logged `/plan` selection targets a state other than `active`, has not
|
|
15
|
+
* failed through its paired `command/done`, and no later `plan/mode` event has
|
|
16
|
+
* recorded that state. Capability absence (plan-mode not composed) is the
|
|
17
|
+
* key's absence, never a value.
|
|
18
|
+
*/
|
|
19
|
+
export interface PlanProjection {
|
|
20
|
+
active: boolean;
|
|
21
|
+
pending: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** Host state used to derive {@link PlanProjection}. */
|
|
24
|
+
export interface PlanUnitState {
|
|
25
|
+
/** Logged plan mode. */
|
|
26
|
+
active: boolean;
|
|
27
|
+
/** The selection's target mode; null when no selection is outstanding. */
|
|
28
|
+
wanted: boolean | null;
|
|
29
|
+
/** The latest plan command awaiting its paired settlement. */
|
|
30
|
+
running: {
|
|
31
|
+
commandId: CommandId;
|
|
32
|
+
wanted: boolean;
|
|
33
|
+
} | null;
|
|
34
|
+
/** Active state recorded by the latest `request/header`, or null. */
|
|
35
|
+
activeAtLastHeader: boolean | null;
|
|
36
|
+
}
|
|
37
|
+
declare module '@unieai/rabi-session-projection/types' {
|
|
38
|
+
interface SessionProjectionStateMap {
|
|
39
|
+
/** Host plan-mode fold state. */
|
|
40
|
+
plan: PlanUnitState;
|
|
41
|
+
}
|
|
42
|
+
interface SessionProjectionMap {
|
|
43
|
+
/** Plan collaboration state folded from the plan command lifecycle and `plan/mode` events. */
|
|
44
|
+
plan: PlanProjection;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure types of the plan domain: the ONE home of the `plan` projection-key
|
|
3
|
+
* declaration, free of this package's host-side value imports (cordis,
|
|
4
|
+
* rabi-tools, rabi-agent). Two namespace projections serve it — `./types` for
|
|
5
|
+
* host consumers and `./client` for client aggregates — with zero content
|
|
6
|
+
* duplication.
|
|
7
|
+
*
|
|
8
|
+
* @module @unieai/rabi-plan-mode/types
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unieai/rabi-plan-mode",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Logged per-agent plan mode with deployment guidance, a direct slash command, and a user-reviewed exit",
|
|
4
|
+
"version": "0.2.1-rc.3",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"types": "lib/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./lib/types/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./types": {
|
|
17
|
+
"types": "./lib/types/types.d.ts",
|
|
18
|
+
"default": "./lib/types/types.js"
|
|
19
|
+
},
|
|
20
|
+
"./client": {
|
|
21
|
+
"types": "./lib/types/client.d.ts",
|
|
22
|
+
"default": "./lib/types/client.js"
|
|
23
|
+
},
|
|
24
|
+
"./src/*": "./src/*",
|
|
25
|
+
"./package.json": "./package.json",
|
|
26
|
+
"./locale/*.json": "./locale/*.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"locale/*.json",
|
|
30
|
+
"lib/index.js",
|
|
31
|
+
"lib/types/**/*.js",
|
|
32
|
+
"lib/types/**/*.d.ts",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@unieai/rabi-agent": "0.2.1-rc.3",
|
|
38
|
+
"@unieai/rabi-commands": "0.2.1-rc.3",
|
|
39
|
+
"@unieai/rabi-llm": "0.2.1-rc.3",
|
|
40
|
+
"@unieai/rabi-session": "0.2.1-rc.3",
|
|
41
|
+
"@unieai/rabi-session-projection": "0.2.1-rc.3",
|
|
42
|
+
"@unieai/rabi-system-prompt": "0.2.1-rc.3",
|
|
43
|
+
"@unieai/rabi-tools": "0.2.1-rc.3",
|
|
44
|
+
"@unieai/rabi-user-questions": "0.2.1-rc.3",
|
|
45
|
+
"@unieai/cordis": "4.0.5"
|
|
46
|
+
},
|
|
47
|
+
"peerDependenciesMeta": {
|
|
48
|
+
"@unieai/rabi-commands": {
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@unieai/rabi-brand": "0.2.1-rc.3",
|
|
54
|
+
"zod": "4.4.3"
|
|
55
|
+
}
|
|
56
|
+
}
|