@mcp-abap-adt/llm-agent-libs 30.22.0 → 30.24.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/README.md +37 -2
- package/dist/coordinator/dialogue/coordinator-prompt.d.ts +2 -0
- package/dist/coordinator/dialogue/coordinator-prompt.d.ts.map +1 -0
- package/dist/coordinator/dialogue/coordinator-prompt.js +7 -0
- package/dist/coordinator/dialogue/coordinator-prompt.js.map +1 -0
- package/dist/coordinator/dialogue/dialogue-session.d.ts +8 -1
- package/dist/coordinator/dialogue/dialogue-session.d.ts.map +1 -1
- package/dist/coordinator/dialogue/dialogue-session.js +130 -43
- package/dist/coordinator/dialogue/dialogue-session.js.map +1 -1
- package/dist/coordinator/dialogue/index.d.ts +3 -0
- package/dist/coordinator/dialogue/index.d.ts.map +1 -1
- package/dist/coordinator/dialogue/index.js +3 -0
- package/dist/coordinator/dialogue/index.js.map +1 -1
- package/dist/coordinator/dialogue/plan-author-prompt.d.ts +3 -0
- package/dist/coordinator/dialogue/plan-author-prompt.d.ts.map +1 -0
- package/dist/coordinator/dialogue/plan-author-prompt.js +17 -0
- package/dist/coordinator/dialogue/plan-author-prompt.js.map +1 -0
- package/dist/coordinator/dialogue/planner-prompt.d.ts +1 -1
- package/dist/coordinator/dialogue/planner-prompt.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-prompt.js +6 -6
- package/dist/coordinator/dialogue/planner-scope.d.ts +3 -1
- package/dist/coordinator/dialogue/planner-scope.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-scope.js.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.js +154 -12
- package/dist/coordinator/dialogue/planner-tools-client.js.map +1 -1
- package/dist/coordinator/dialogue/profile-plan-author.d.ts +13 -0
- package/dist/coordinator/dialogue/profile-plan-author.d.ts.map +1 -0
- package/dist/coordinator/dialogue/profile-plan-author.js +161 -0
- package/dist/coordinator/dialogue/profile-plan-author.js.map +1 -0
- package/dist/coordinator/dialogue/profile-resolver.d.ts +4 -0
- package/dist/coordinator/dialogue/profile-resolver.d.ts.map +1 -1
- package/dist/coordinator/dialogue/profile-resolver.js +4 -1
- package/dist/coordinator/dialogue/profile-resolver.js.map +1 -1
- package/dist/coordinator/dialogue/structured-plan.d.ts +1 -1
- package/dist/coordinator/dialogue/structured-plan.d.ts.map +1 -1
- package/dist/coordinator/dialogue/structured-plan.js +2 -2
- package/dist/coordinator/dialogue/structured-plan.js.map +1 -1
- package/dist/coordinator/dialogue/task-board.d.ts.map +1 -1
- package/dist/coordinator/dialogue/task-board.js +30 -5
- package/dist/coordinator/dialogue/task-board.js.map +1 -1
- package/dist/coordinator/dialogue/tool-trace-render.d.ts +4 -0
- package/dist/coordinator/dialogue/tool-trace-render.d.ts.map +1 -0
- package/dist/coordinator/dialogue/tool-trace-render.js +58 -0
- package/dist/coordinator/dialogue/tool-trace-render.js.map +1 -0
- package/dist/pipeline/handlers/tool-loop-core.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-loop-core.js +9 -3
- package/dist/pipeline/handlers/tool-loop-core.js.map +1 -1
- package/dist/pipeline/handlers/tool-loop.d.ts +0 -25
- package/dist/pipeline/handlers/tool-loop.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-loop.js +2 -25
- package/dist/pipeline/handlers/tool-loop.js.map +1 -1
- package/dist/policy/mixed-tool-call-handler.d.ts.map +1 -1
- package/dist/policy/mixed-tool-call-handler.js +12 -4
- package/dist/policy/mixed-tool-call-handler.js.map +1 -1
- package/dist/subagent/smart-agent-subagent.d.ts.map +1 -1
- package/dist/subagent/smart-agent-subagent.js +103 -81
- package/dist/subagent/smart-agent-subagent.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -83,6 +83,40 @@ Use `@mcp-abap-adt/llm-usage` for buffered collection or implement
|
|
|
83
83
|
transports/sinks, shutdown and loss semantics. Usage records exclude content;
|
|
84
84
|
request logging is a separate API with different data and retention needs.
|
|
85
85
|
|
|
86
|
+
## Dialogue coordination and planning
|
|
87
|
+
|
|
88
|
+
Pass an `IPlanAuthor` to `DialogueSession.open({ planAuthor, ... })` to separate
|
|
89
|
+
planning from conversation. Profile mode selects `prompts.coordinator ??
|
|
90
|
+
DIALOGUE_COORDINATOR_SYSTEM`; a supplied planner instance should use that coordinator
|
|
91
|
+
prompt itself. Without `planAuthor`, the existing planner mode is preserved.
|
|
92
|
+
|
|
93
|
+
`ProfilePlanAuthor({ source, runner, notes? })` implements the stateless core
|
|
94
|
+
`IPlanAuthor`: `create({ goal, context?, references? }, options?)` and
|
|
95
|
+
`revise({ plan, change, brief? }, options?)` return a typed plan or author error.
|
|
96
|
+
It selects `plannerCreate` / `plannerRevise` from source prompts, falling back to
|
|
97
|
+
`PLAN_CREATE_SYSTEM` / `PLAN_REVISE_SYSTEM`, and calls `runner.preparePlanAuthor`
|
|
98
|
+
for a fresh tool loop. This runner method is optional; when absent, the author
|
|
99
|
+
returns `unavailable` with `profile runner cannot prepare a plan author`.
|
|
100
|
+
The host enforces read-only knowledge tools and excludes
|
|
101
|
+
SAP tools at definition and execution time. Revisions retain completed work;
|
|
102
|
+
invalid output leaves the session plan unchanged.
|
|
103
|
+
|
|
104
|
+
`PlannerToolsClient` exposes `plan_create`, `plan_revise` only with an author,
|
|
105
|
+
plus `plan_get`, `plan_update`, `delegate_task`, `get_task`, `list_tasks`,
|
|
106
|
+
`stop_task`, `search_knowledge`, `new_request` and `deliver`. `plan_create` stores
|
|
107
|
+
a new authored plan; `plan_revise` merges a revision of the current structured
|
|
108
|
+
plan. `plan_get` reads filtered, paged steps; `plan_update` edits without an LLM
|
|
109
|
+
call. `get_plan` / `update_plan` have been removed without aliases.
|
|
110
|
+
|
|
111
|
+
External coordinators can use `session.callTool(name, args, { signal? }?)` without
|
|
112
|
+
a planner turn or planner agent. It returns `Result<McpToolResult, McpError>`,
|
|
113
|
+
persists state and emits normal plan/task signals. Busy sessions refuse calls
|
|
114
|
+
with `isError: true` rather than queueing them; `new_request` / `deliver` are
|
|
115
|
+
turn-only. Task events stay in persisted `pendingEvents` without waking the LLM;
|
|
116
|
+
the next user `send()` consumes them and resumes dialogue mode. These events are
|
|
117
|
+
separate from pending replies read by `takePending()`. See the
|
|
118
|
+
[dialogue guide](../../docs/PIPELINES.md#dialogue-planner) for details.
|
|
119
|
+
|
|
86
120
|
## Dialogue wait mode
|
|
87
121
|
|
|
88
122
|
`DialogueSession.open` accepts `waitTickMs` (default 30,000) and `longTaskMs`
|
|
@@ -115,8 +149,9 @@ legacy API. `profileTimeoutMs` defaults to 3000 milliseconds per source resoluti
|
|
|
115
149
|
The source implements the public `IAgentProfileSource` contract: `list()` returns
|
|
116
150
|
enabled names, descriptions and result kinds; `get(name)` performs an exact,
|
|
117
151
|
case-sensitive lookup and distinguishes missing from disabled records; `getPrompts()`
|
|
118
|
-
returns optional
|
|
119
|
-
|
|
152
|
+
returns optional `workerBase`, `planner`, `coordinator`, `plannerCreate` and
|
|
153
|
+
`plannerRevise` text. Empty strings are explicit text; absent fields select the
|
|
154
|
+
corresponding library defaults. Errors fail closed.
|
|
120
155
|
Do not use a positive TTL or stale-while-revalidate: check current revision and
|
|
121
156
|
availability on every lookup. Immutable revision bodies may then be cached by the host.
|
|
122
157
|
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const DIALOGUE_COORDINATOR_SYSTEM = "You coordinate the conversation and orchestrate work.\nTalk to the user, clarify missing information, and answer from task results.\nChoose the next action by calling an available tool. Use plan_create to create a plan,\nplan_revise for substantial changes, and plan_update for small edits.\nDelegate work to worker profiles, track tasks, and review their results before answering.\nDo not execute worker steps yourself.";
|
|
2
|
+
//# sourceMappingURL=coordinator-prompt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinator-prompt.d.ts","sourceRoot":"","sources":["../../../src/coordinator/dialogue/coordinator-prompt.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,2BAA2B,yaAKF,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const DIALOGUE_COORDINATOR_SYSTEM = `You coordinate the conversation and orchestrate work.
|
|
2
|
+
Talk to the user, clarify missing information, and answer from task results.
|
|
3
|
+
Choose the next action by calling an available tool. Use plan_create to create a plan,
|
|
4
|
+
plan_revise for substantial changes, and plan_update for small edits.
|
|
5
|
+
Delegate work to worker profiles, track tasks, and review their results before answering.
|
|
6
|
+
Do not execute worker steps yourself.`;
|
|
7
|
+
//# sourceMappingURL=coordinator-prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinator-prompt.js","sourceRoot":"","sources":["../../../src/coordinator/dialogue/coordinator-prompt.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,2BAA2B,GAAG;;;;;sCAKL,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CallOptions, DialogueSignals, DialogueState, IAgentProfileRunner, IAgentProfileSource, IDialogueStateStore, IPlannerContextBuilder, ISignalEmitter, IUsageRecorder, Message, OnPartial, PlannerMessage, Result } from '@mcp-abap-adt/llm-agent';
|
|
1
|
+
import type { CallOptions, DialogueSignals, DialogueState, IAgentProfileRunner, IAgentProfileSource, IDialogueStateStore, IPlanAuthor, IPlannerContextBuilder, ISignalEmitter, IUsageRecorder, McpError, McpToolResult, Message, OnPartial, PlannerMessage, Result } from '@mcp-abap-adt/llm-agent';
|
|
2
2
|
import { type TokenUsage } from '@mcp-abap-adt/llm-agent';
|
|
3
3
|
import { type PlannerRetrievalOptions } from './planner-retrieval.js';
|
|
4
4
|
import { type TaskBoardOptions } from './task-board.js';
|
|
@@ -50,6 +50,7 @@ export interface DialogueSessionOptions extends PlannerRetrievalOptions {
|
|
|
50
50
|
maxItemsPerStep?: number;
|
|
51
51
|
taskOutputChars?: number;
|
|
52
52
|
planner?: PlannerAgent;
|
|
53
|
+
planAuthor?: IPlanAuthor;
|
|
53
54
|
profileSource?: IAgentProfileSource;
|
|
54
55
|
profileRunner?: IAgentProfileRunner;
|
|
55
56
|
profileTimeoutMs?: number;
|
|
@@ -67,6 +68,9 @@ export declare class DialogueSession {
|
|
|
67
68
|
private readonly resultEvents;
|
|
68
69
|
private readonly signals;
|
|
69
70
|
private turnActive;
|
|
71
|
+
private externalCoordinator;
|
|
72
|
+
private deferredEvents;
|
|
73
|
+
private toolActive?;
|
|
70
74
|
private turnUsage?;
|
|
71
75
|
private lastActivity?;
|
|
72
76
|
private lastPlan?;
|
|
@@ -101,9 +105,12 @@ export declare class DialogueSession {
|
|
|
101
105
|
private updateWaitClock;
|
|
102
106
|
private waitTick;
|
|
103
107
|
private liveEvents;
|
|
108
|
+
private enqueueEvent;
|
|
104
109
|
private constructor();
|
|
105
110
|
static open(opts: DialogueSessionOptions): Promise<DialogueSession>;
|
|
106
111
|
send(text: string, opts?: Omit<PlannerTurnOptions, 'sessionId'>): Promise<PlannerMessage>;
|
|
112
|
+
private createPlannerScope;
|
|
113
|
+
callTool(name: string, args: Record<string, unknown>, opts?: Pick<CallOptions, 'signal' | 'usage' | 'trace'>): Promise<Result<McpToolResult, McpError>>;
|
|
107
114
|
private schedule;
|
|
108
115
|
private drain;
|
|
109
116
|
private turn;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dialogue-session.d.ts","sourceRoot":"","sources":["../../../src/coordinator/dialogue/dialogue-session.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,WAAW,EACX,eAAe,EACf,aAAa,EACb,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,sBAAsB,EACtB,cAAc,EACd,cAAc,EACd,OAAO,EACP,SAAS,EAET,cAAc,EAEd,MAAM,EAIP,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAIL,KAAK,UAAU,EAChB,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"dialogue-session.d.ts","sourceRoot":"","sources":["../../../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scopedUsage, } from '@mcp-abap-adt/llm-agent';
|
|
3
|
+
import { DIALOGUE_COORDINATOR_SYSTEM } from './coordinator-prompt.js';
|
|
3
4
|
import { appendRequestMessage, boundRequestTranscript, DefaultPlannerContextBuilder, renderPlannerRetrieval, } from './planner-context-builder.js';
|
|
4
5
|
import { PlannerMemoryWriter } from './planner-memory.js';
|
|
5
6
|
import { composePlannerPrompt, DIALOGUE_PLANNER_SYSTEM, } from './planner-prompt.js';
|
|
6
7
|
import { retrievePlannerContext, validateRetrievalOptions, } from './planner-retrieval.js';
|
|
7
8
|
import { runInPlannerScope } from './planner-scope.js';
|
|
9
|
+
import { PlannerToolsClient } from './planner-tools-client.js';
|
|
8
10
|
import { formatRequestTime, validateTimeZone } from './request-time.js';
|
|
9
11
|
import { applyTaskEvent, checkSourcesCurrent, currentAttemptIds, deliveryChecksPassed, planView, readySteps, recomputePlanStatus, resetStaleCheck, restoreLegacyStep, restoreSuccessfulWork, resultKind, } from './structured-plan.js';
|
|
10
12
|
import { TaskBoard } from './task-board.js';
|
|
@@ -15,6 +17,10 @@ export class DialogueSession {
|
|
|
15
17
|
resultEvents = [];
|
|
16
18
|
signals;
|
|
17
19
|
turnActive = false;
|
|
20
|
+
externalCoordinator = false;
|
|
21
|
+
// Outcomes deferred while no coordinator turn runs accompany the next user.
|
|
22
|
+
deferredEvents = [];
|
|
23
|
+
toolActive;
|
|
18
24
|
turnUsage;
|
|
19
25
|
lastActivity;
|
|
20
26
|
lastPlan;
|
|
@@ -31,7 +37,7 @@ export class DialogueSession {
|
|
|
31
37
|
this.updateWaitClock();
|
|
32
38
|
const activity = {
|
|
33
39
|
turnActive: this.turnActive,
|
|
34
|
-
queuedInputs: this.users.length + this.events.length,
|
|
40
|
+
queuedInputs: this.users.length + this.events.length + this.deferredEvents.length,
|
|
35
41
|
running: this.board
|
|
36
42
|
.list()
|
|
37
43
|
.filter((t) => t.state === 'queued' || t.state === 'running').length,
|
|
@@ -233,7 +239,7 @@ export class DialogueSession {
|
|
|
233
239
|
continue;
|
|
234
240
|
this.longNotifiedAt.set(task.id, now);
|
|
235
241
|
delete task.usage;
|
|
236
|
-
this.
|
|
242
|
+
this.enqueueEvent({ kind: 'long_running', task });
|
|
237
243
|
}
|
|
238
244
|
this.changed();
|
|
239
245
|
if (this.events.length)
|
|
@@ -243,6 +249,24 @@ export class DialogueSession {
|
|
|
243
249
|
return events.filter((event) => event.kind !== 'long_running' ||
|
|
244
250
|
this.board.get(event.task.id)?.state === 'running');
|
|
245
251
|
}
|
|
252
|
+
enqueueEvent(event) {
|
|
253
|
+
if (this.externalCoordinator) {
|
|
254
|
+
const index = event.kind === 'long_running'
|
|
255
|
+
? this.deferredEvents.findIndex((pending) => pending.kind === 'long_running' &&
|
|
256
|
+
pending.task.id === event.task.id)
|
|
257
|
+
: -1;
|
|
258
|
+
if (index >= 0)
|
|
259
|
+
this.deferredEvents[index] = event;
|
|
260
|
+
else
|
|
261
|
+
this.deferredEvents.push(event);
|
|
262
|
+
}
|
|
263
|
+
else
|
|
264
|
+
this.events.push(event);
|
|
265
|
+
this.changed();
|
|
266
|
+
if (this.externalCoordinator)
|
|
267
|
+
void this.persist();
|
|
268
|
+
this.schedule();
|
|
269
|
+
}
|
|
246
270
|
constructor(opts) {
|
|
247
271
|
this.opts = opts;
|
|
248
272
|
validateTimeZone(opts.timeZone ?? 'UTC');
|
|
@@ -304,9 +328,7 @@ export class DialogueSession {
|
|
|
304
328
|
return;
|
|
305
329
|
if (event.kind !== 'long_running')
|
|
306
330
|
this.outcomeVersion++;
|
|
307
|
-
this.
|
|
308
|
-
this.changed();
|
|
309
|
-
this.schedule();
|
|
331
|
+
this.enqueueEvent(event);
|
|
310
332
|
},
|
|
311
333
|
});
|
|
312
334
|
}
|
|
@@ -337,6 +359,7 @@ export class DialogueSession {
|
|
|
337
359
|
session.plan.question =
|
|
338
360
|
'Awaiting user response (restored legacy state)';
|
|
339
361
|
session.pending = structuredClone(state.pending);
|
|
362
|
+
session.deferredEvents = structuredClone(state.pendingEvents ?? []);
|
|
340
363
|
session.requests = (state.requests ?? [
|
|
341
364
|
{
|
|
342
365
|
id: 'r1',
|
|
@@ -418,13 +441,96 @@ export class DialogueSession {
|
|
|
418
441
|
if (this.closing)
|
|
419
442
|
return Promise.reject(new Error('dialogue session closed'));
|
|
420
443
|
return new Promise((resolve, reject) => {
|
|
444
|
+
this.externalCoordinator = false;
|
|
421
445
|
this.users.push({ text, opts, resolve, reject });
|
|
422
446
|
this.changed();
|
|
423
447
|
this.schedule();
|
|
424
448
|
});
|
|
425
449
|
}
|
|
450
|
+
createPlannerScope(request, options = {}) {
|
|
451
|
+
return {
|
|
452
|
+
planAuthor: this.opts.planAuthor,
|
|
453
|
+
authorOptions: {
|
|
454
|
+
usage: options.usage ?? this.opts.usage ?? this.opts.board.usage,
|
|
455
|
+
sessionId: this.opts.id,
|
|
456
|
+
trace: options.trace,
|
|
457
|
+
},
|
|
458
|
+
newRequest: () => {
|
|
459
|
+
throw new Error('not available outside a coordinator turn');
|
|
460
|
+
},
|
|
461
|
+
deliver: () => {
|
|
462
|
+
throw new Error('not available outside a coordinator turn');
|
|
463
|
+
},
|
|
464
|
+
ownTask: (id) => {
|
|
465
|
+
const owner = request ??
|
|
466
|
+
this.openRequest ??
|
|
467
|
+
this.beginRequest(this.board.get(id)?.goal ?? '', false);
|
|
468
|
+
this.taskRequests.set(id, owner.id);
|
|
469
|
+
},
|
|
470
|
+
recordAction: () => this.changed(),
|
|
471
|
+
board: this.board,
|
|
472
|
+
signal: options.signal,
|
|
473
|
+
requestStartedAt: request?.openedAt,
|
|
474
|
+
maxItemsPerStep: this.opts.maxItemsPerStep,
|
|
475
|
+
taskSteps: this.taskSteps,
|
|
476
|
+
getPlan: () => this.plan,
|
|
477
|
+
setPlan: (plan, status = 'active', question) => {
|
|
478
|
+
this.plan =
|
|
479
|
+
typeof plan === 'string'
|
|
480
|
+
? { version: 1, steps: [], note: plan, status, question }
|
|
481
|
+
: plan;
|
|
482
|
+
this.changed();
|
|
483
|
+
},
|
|
484
|
+
searchKnowledge: async (query, collection, k) => {
|
|
485
|
+
const sources = this.opts.plannerKnowledge ?? [];
|
|
486
|
+
if (collection !== undefined &&
|
|
487
|
+
!sources.some((source) => source.name === collection))
|
|
488
|
+
throw new Error(`unknown knowledge collection ${collection}`);
|
|
489
|
+
const result = await retrievePlannerContext({
|
|
490
|
+
...this.opts,
|
|
491
|
+
plannerMemory: undefined,
|
|
492
|
+
plannerKnowledge: sources.filter((source) => collection === undefined || source.name === collection),
|
|
493
|
+
knowledgeK: k,
|
|
494
|
+
}, this.opts.id, request?.id ?? this.opts.id, query, false, options, (message) => this.warn(message));
|
|
495
|
+
return renderPlannerRetrieval({
|
|
496
|
+
memory: [],
|
|
497
|
+
knowledge: result.knowledge,
|
|
498
|
+
notes: [],
|
|
499
|
+
}).join('\n');
|
|
500
|
+
},
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
callTool(name, args, opts = {}) {
|
|
504
|
+
const refuse = (message) => Promise.resolve({
|
|
505
|
+
ok: true,
|
|
506
|
+
value: { isError: true, content: [{ type: 'text', text: message }] },
|
|
507
|
+
});
|
|
508
|
+
if (opts.signal?.aborted)
|
|
509
|
+
return refuse('aborted');
|
|
510
|
+
if (this.turnActive || this.active || this.users.length)
|
|
511
|
+
return refuse('session is busy with a coordinator turn');
|
|
512
|
+
if (this.toolActive)
|
|
513
|
+
return refuse('session is busy with a tool call');
|
|
514
|
+
if (this.closing)
|
|
515
|
+
return refuse('dialogue session closed');
|
|
516
|
+
if (name === 'new_request' || name === 'deliver')
|
|
517
|
+
return refuse('not available outside a coordinator turn');
|
|
518
|
+
this.externalCoordinator = true;
|
|
519
|
+
const operation = runInPlannerScope(this.createPlannerScope(this.openRequest, opts), () => new PlannerToolsClient().callTool(name, args));
|
|
520
|
+
this.toolActive = operation.finally(async () => {
|
|
521
|
+
this.changed();
|
|
522
|
+
if (!opts.signal?.aborted)
|
|
523
|
+
await this.persist();
|
|
524
|
+
this.toolActive = undefined;
|
|
525
|
+
if (this.users.length)
|
|
526
|
+
this.schedule();
|
|
527
|
+
});
|
|
528
|
+
return this.toolActive;
|
|
529
|
+
}
|
|
426
530
|
schedule() {
|
|
427
|
-
if (this.active || this.closing)
|
|
531
|
+
if (this.active || this.toolActive || this.closing)
|
|
532
|
+
return;
|
|
533
|
+
if (this.externalCoordinator && !this.users.length)
|
|
428
534
|
return;
|
|
429
535
|
// Defer draining so synchronous task events are batched together.
|
|
430
536
|
this.active = Promise.resolve()
|
|
@@ -436,12 +542,14 @@ export class DialogueSession {
|
|
|
436
542
|
});
|
|
437
543
|
}
|
|
438
544
|
async drain() {
|
|
439
|
-
while (!this.closing &&
|
|
545
|
+
while (!this.closing &&
|
|
546
|
+
(this.users.length || (!this.externalCoordinator && this.events.length))) {
|
|
440
547
|
const user = this.users.shift();
|
|
441
|
-
const events = user ?
|
|
548
|
+
const events = this.liveEvents(user ? this.deferredEvents.splice(0) : this.events.splice(0));
|
|
442
549
|
if (!user && !events.length)
|
|
443
550
|
continue;
|
|
444
|
-
const
|
|
551
|
+
const notes = this.eventNotes(events);
|
|
552
|
+
const text = user ? [user.text, notes].filter(Boolean).join('\n') : notes;
|
|
445
553
|
const trigger = user ? 'user' : 'event';
|
|
446
554
|
this.turnActive = true;
|
|
447
555
|
this.changed();
|
|
@@ -478,7 +586,7 @@ export class DialogueSession {
|
|
|
478
586
|
const previousRequest = this.openRequest;
|
|
479
587
|
const previousPlan = this.plan;
|
|
480
588
|
const waitingAtEntry = this.plan?.status === 'waiting_user';
|
|
481
|
-
let request = this.beginRequest(text, user !== undefined);
|
|
589
|
+
let request = this.beginRequest(text, user !== undefined && consumed.length === 0);
|
|
482
590
|
const entryTurnOk = request.lastTurnOk;
|
|
483
591
|
request.lastTurnOk = false;
|
|
484
592
|
const turnStartedAt = this.now();
|
|
@@ -575,7 +683,10 @@ export class DialogueSession {
|
|
|
575
683
|
if (!runner)
|
|
576
684
|
throw new Error('profileRunner is required');
|
|
577
685
|
planner = await this.board.profiles.call((options) => runner.preparePlanner({
|
|
578
|
-
systemPrompt: composePlannerPrompt(
|
|
686
|
+
systemPrompt: composePlannerPrompt(this.opts.planAuthor
|
|
687
|
+
? (discovery.prompts.coordinator ??
|
|
688
|
+
DIALOGUE_COORDINATOR_SYSTEM)
|
|
689
|
+
: (discovery.prompts.planner ?? DIALOGUE_PLANNER_SYSTEM)),
|
|
579
690
|
}, options), { signal: user?.opts.signal });
|
|
580
691
|
}
|
|
581
692
|
if (!planner)
|
|
@@ -626,6 +737,10 @@ export class DialogueSession {
|
|
|
626
737
|
}
|
|
627
738
|
const contentPartials = [];
|
|
628
739
|
const result = await runInPlannerScope({
|
|
740
|
+
...this.createPlannerScope(request, {
|
|
741
|
+
...user?.opts,
|
|
742
|
+
usage: this.turnUsage,
|
|
743
|
+
}),
|
|
629
744
|
beforeTool: (name) => {
|
|
630
745
|
if ((reserved || restarting) &&
|
|
631
746
|
(name !== 'new_request' || !reserved || restartDelivered))
|
|
@@ -664,12 +779,6 @@ export class DialogueSession {
|
|
|
664
779
|
partialText = '';
|
|
665
780
|
contentPartials.length = 0;
|
|
666
781
|
},
|
|
667
|
-
ownTask: (id) => this.taskRequests.set(id, request.id),
|
|
668
|
-
board: this.board,
|
|
669
|
-
signal: user?.opts.signal,
|
|
670
|
-
requestStartedAt: request.openedAt,
|
|
671
|
-
maxItemsPerStep: this.opts.maxItemsPerStep,
|
|
672
|
-
taskSteps: this.taskSteps,
|
|
673
782
|
statusRead: () => {
|
|
674
783
|
if (waitStop.signal.aborted)
|
|
675
784
|
return;
|
|
@@ -679,36 +788,11 @@ export class DialogueSession {
|
|
|
679
788
|
}
|
|
680
789
|
lastReadVersion = this.outcomeVersion;
|
|
681
790
|
},
|
|
682
|
-
getPlan: () => this.plan,
|
|
683
|
-
setPlan: (plan, status = 'active', question) => {
|
|
684
|
-
this.plan =
|
|
685
|
-
typeof plan === 'string'
|
|
686
|
-
? { version: 1, steps: [], note: plan, status, question }
|
|
687
|
-
: plan;
|
|
688
|
-
this.changed();
|
|
689
|
-
},
|
|
690
791
|
recordAction: (action, didStart) => {
|
|
691
792
|
actions.push(action);
|
|
692
793
|
started ||= didStart === true;
|
|
693
794
|
this.changed();
|
|
694
795
|
},
|
|
695
|
-
searchKnowledge: async (query, collection, k) => {
|
|
696
|
-
const sources = this.opts.plannerKnowledge ?? [];
|
|
697
|
-
if (collection !== undefined &&
|
|
698
|
-
!sources.some((source) => source.name === collection))
|
|
699
|
-
throw new Error(`unknown knowledge collection ${collection}`);
|
|
700
|
-
const result = await retrievePlannerContext({
|
|
701
|
-
...this.opts,
|
|
702
|
-
plannerMemory: undefined,
|
|
703
|
-
plannerKnowledge: sources.filter((source) => collection === undefined || source.name === collection),
|
|
704
|
-
knowledgeK: k,
|
|
705
|
-
}, this.opts.id, request.id, query, false, { ...user?.opts, usage: this.turnUsage }, (message) => this.warn(message));
|
|
706
|
-
return renderPlannerRetrieval({
|
|
707
|
-
memory: [],
|
|
708
|
-
knowledge: result.knowledge,
|
|
709
|
-
notes: [],
|
|
710
|
-
}).join('\n');
|
|
711
|
-
},
|
|
712
796
|
}, () => callPlanner.process(messages, {
|
|
713
797
|
...user?.opts,
|
|
714
798
|
toolRoundGuard: (calls) => {
|
|
@@ -991,6 +1075,7 @@ export class DialogueSession {
|
|
|
991
1075
|
taskSteps: Object.fromEntries(this.taskSteps),
|
|
992
1076
|
taskRequests: Object.fromEntries(this.taskRequests),
|
|
993
1077
|
pending: this.pending,
|
|
1078
|
+
pendingEvents: this.deferredEvents,
|
|
994
1079
|
savedAt: this.savedAt,
|
|
995
1080
|
});
|
|
996
1081
|
}
|
|
@@ -999,6 +1084,7 @@ export class DialogueSession {
|
|
|
999
1084
|
return this.closePromise;
|
|
1000
1085
|
this.closing = true;
|
|
1001
1086
|
this.events = [];
|
|
1087
|
+
this.deferredEvents = [];
|
|
1002
1088
|
for (const user of this.users.splice(0))
|
|
1003
1089
|
user.reject(new Error('dialogue session closed'));
|
|
1004
1090
|
this.changed();
|
|
@@ -1007,10 +1093,11 @@ export class DialogueSession {
|
|
|
1007
1093
|
}
|
|
1008
1094
|
async finishClose() {
|
|
1009
1095
|
// Stop immediately, then stop again for tasks delegated by the settling turn.
|
|
1010
|
-
await Promise.all([this.board.stopAll(), this.active]);
|
|
1096
|
+
await Promise.all([this.board.stopAll(), this.active, this.toolActive]);
|
|
1011
1097
|
await this.board.stopAll();
|
|
1012
1098
|
await this.memoryWriter?.close();
|
|
1013
1099
|
this.events = [];
|
|
1100
|
+
this.deferredEvents = [];
|
|
1014
1101
|
this.pending = [];
|
|
1015
1102
|
await this.persistence;
|
|
1016
1103
|
try {
|