@animalabs/agent-framework 0.26.1 → 0.27.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 +57 -7
- package/dist/src/agent.d.ts +263 -22
- package/dist/src/agent.d.ts.map +1 -1
- package/dist/src/agent.js +531 -54
- package/dist/src/agent.js.map +1 -1
- package/dist/src/api/server.d.ts.map +1 -1
- package/dist/src/api/server.js +1 -2
- package/dist/src/api/server.js.map +1 -1
- package/dist/src/auto-continue.d.ts +33 -0
- package/dist/src/auto-continue.d.ts.map +1 -0
- package/dist/src/auto-continue.js +35 -0
- package/dist/src/auto-continue.js.map +1 -0
- package/dist/src/code-execution/isolate/scripts.d.ts +11 -0
- package/dist/src/code-execution/isolate/scripts.d.ts.map +1 -1
- package/dist/src/code-execution/isolate/scripts.js +5 -1
- package/dist/src/code-execution/isolate/scripts.js.map +1 -1
- package/dist/src/framework.d.ts +829 -156
- package/dist/src/framework.d.ts.map +1 -1
- package/dist/src/framework.js +3906 -1008
- package/dist/src/framework.js.map +1 -1
- package/dist/src/gate/event-gate.d.ts +309 -46
- package/dist/src/gate/event-gate.d.ts.map +1 -1
- package/dist/src/gate/event-gate.js +2117 -395
- package/dist/src/gate/event-gate.js.map +1 -1
- package/dist/src/gate/index.d.ts +2 -2
- package/dist/src/gate/index.d.ts.map +1 -1
- package/dist/src/gate/index.js +1 -1
- package/dist/src/gate/index.js.map +1 -1
- package/dist/src/gate/recipe-reconcile.d.ts +103 -0
- package/dist/src/gate/recipe-reconcile.d.ts.map +1 -0
- package/dist/src/gate/recipe-reconcile.js +325 -0
- package/dist/src/gate/recipe-reconcile.js.map +1 -0
- package/dist/src/gate/types.d.ts +68 -8
- package/dist/src/gate/types.d.ts.map +1 -1
- package/dist/src/index.d.ts +6 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +5 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcpl/channel-registry.d.ts +433 -113
- package/dist/src/mcpl/channel-registry.d.ts.map +1 -1
- package/dist/src/mcpl/channel-registry.js +1152 -434
- package/dist/src/mcpl/channel-registry.js.map +1 -1
- package/dist/src/mcpl/conversation-router.d.ts +16 -8
- package/dist/src/mcpl/conversation-router.d.ts.map +1 -1
- package/dist/src/mcpl/conversation-router.js +29 -15
- package/dist/src/mcpl/conversation-router.js.map +1 -1
- package/dist/src/mcpl/convert-block.d.ts.map +1 -1
- package/dist/src/mcpl/convert-block.js +14 -2
- package/dist/src/mcpl/convert-block.js.map +1 -1
- package/dist/src/mcpl/hook-orchestrator.d.ts +7 -1
- package/dist/src/mcpl/hook-orchestrator.d.ts.map +1 -1
- package/dist/src/mcpl/hook-orchestrator.js +32 -7
- package/dist/src/mcpl/hook-orchestrator.js.map +1 -1
- package/dist/src/mcpl/index.d.ts +5 -1
- package/dist/src/mcpl/index.d.ts.map +1 -1
- package/dist/src/mcpl/index.js +4 -0
- package/dist/src/mcpl/index.js.map +1 -1
- package/dist/src/mcpl/inference-router.d.ts +14 -2
- package/dist/src/mcpl/inference-router.d.ts.map +1 -1
- package/dist/src/mcpl/inference-router.js +73 -28
- package/dist/src/mcpl/inference-router.js.map +1 -1
- package/dist/src/mcpl/prose-grammar.d.ts +4 -0
- package/dist/src/mcpl/prose-grammar.d.ts.map +1 -1
- package/dist/src/mcpl/prose-grammar.js +6 -0
- package/dist/src/mcpl/prose-grammar.js.map +1 -1
- package/dist/src/mcpl/protocol-family.d.ts.map +1 -1
- package/dist/src/mcpl/protocol-family.js +3 -0
- package/dist/src/mcpl/protocol-family.js.map +1 -1
- package/dist/src/mcpl/push-coalescer.d.ts +20 -2
- package/dist/src/mcpl/push-coalescer.d.ts.map +1 -1
- package/dist/src/mcpl/push-coalescer.js +33 -11
- package/dist/src/mcpl/push-coalescer.js.map +1 -1
- package/dist/src/mcpl/reference-fetcher.d.ts +63 -42
- package/dist/src/mcpl/reference-fetcher.d.ts.map +1 -1
- package/dist/src/mcpl/reference-fetcher.js +177 -145
- package/dist/src/mcpl/reference-fetcher.js.map +1 -1
- package/dist/src/mcpl/references.d.ts +380 -46
- package/dist/src/mcpl/references.d.ts.map +1 -1
- package/dist/src/mcpl/references.js +737 -78
- package/dist/src/mcpl/references.js.map +1 -1
- package/dist/src/mcpl/server-connection.d.ts +82 -3
- package/dist/src/mcpl/server-connection.d.ts.map +1 -1
- package/dist/src/mcpl/server-connection.js +132 -7
- package/dist/src/mcpl/server-connection.js.map +1 -1
- package/dist/src/mcpl/tool-lifecycle.d.ts +11 -0
- package/dist/src/mcpl/tool-lifecycle.d.ts.map +1 -1
- package/dist/src/mcpl/tool-lifecycle.js +21 -0
- package/dist/src/mcpl/tool-lifecycle.js.map +1 -1
- package/dist/src/mcpl/tool-presence.d.ts +259 -0
- package/dist/src/mcpl/tool-presence.d.ts.map +1 -0
- package/dist/src/mcpl/tool-presence.js +628 -0
- package/dist/src/mcpl/tool-presence.js.map +1 -0
- package/dist/src/mcpl/tool-result-normalize.d.ts +9 -3
- package/dist/src/mcpl/tool-result-normalize.d.ts.map +1 -1
- package/dist/src/mcpl/tool-result-normalize.js +14 -6
- package/dist/src/mcpl/tool-result-normalize.js.map +1 -1
- package/dist/src/mcpl/tool-signature.d.ts +39 -0
- package/dist/src/mcpl/tool-signature.d.ts.map +1 -0
- package/dist/src/mcpl/tool-signature.js +292 -0
- package/dist/src/mcpl/tool-signature.js.map +1 -0
- package/dist/src/mcpl/toolset-host.d.ts +200 -0
- package/dist/src/mcpl/toolset-host.d.ts.map +1 -0
- package/dist/src/mcpl/toolset-host.js +621 -0
- package/dist/src/mcpl/toolset-host.js.map +1 -0
- package/dist/src/mcpl/toolsets-module.d.ts +80 -0
- package/dist/src/mcpl/toolsets-module.d.ts.map +1 -0
- package/dist/src/mcpl/toolsets-module.js +431 -0
- package/dist/src/mcpl/toolsets-module.js.map +1 -0
- package/dist/src/mcpl/transport.d.ts +2 -2
- package/dist/src/mcpl/transport.d.ts.map +1 -1
- package/dist/src/mcpl/transport.js +3 -2
- package/dist/src/mcpl/transport.js.map +1 -1
- package/dist/src/mcpl/types.d.ts +21 -1
- package/dist/src/mcpl/types.d.ts.map +1 -1
- package/dist/src/mcpl/types.js.map +1 -1
- package/dist/src/mcpl/visible-content.d.ts +7 -9
- package/dist/src/mcpl/visible-content.d.ts.map +1 -1
- package/dist/src/mcpl/visible-content.js +21 -9
- package/dist/src/mcpl/visible-content.js.map +1 -1
- package/dist/src/module-registry.d.ts +23 -8
- package/dist/src/module-registry.d.ts.map +1 -1
- package/dist/src/module-registry.js +68 -15
- package/dist/src/module-registry.js.map +1 -1
- package/dist/src/modules/discord/index.js +1 -1
- package/dist/src/modules/discord/index.js.map +1 -1
- package/dist/src/modules/history/index.d.ts +2 -1
- package/dist/src/modules/history/index.d.ts.map +1 -1
- package/dist/src/modules/history/index.js +51 -17
- package/dist/src/modules/history/index.js.map +1 -1
- package/dist/src/modules/history/semantic.d.ts +108 -6
- package/dist/src/modules/history/semantic.d.ts.map +1 -1
- package/dist/src/modules/history/semantic.js +273 -57
- package/dist/src/modules/history/semantic.js.map +1 -1
- package/dist/src/modules/workspace/direct-write.d.ts +181 -0
- package/dist/src/modules/workspace/direct-write.d.ts.map +1 -0
- package/dist/src/modules/workspace/direct-write.js +384 -0
- package/dist/src/modules/workspace/direct-write.js.map +1 -0
- package/dist/src/modules/workspace/edit-text.d.ts +26 -12
- package/dist/src/modules/workspace/edit-text.d.ts.map +1 -1
- package/dist/src/modules/workspace/edit-text.js +58 -28
- package/dist/src/modules/workspace/edit-text.js.map +1 -1
- package/dist/src/modules/workspace/effects.d.ts +10 -0
- package/dist/src/modules/workspace/effects.d.ts.map +1 -1
- package/dist/src/modules/workspace/effects.js +1 -1
- package/dist/src/modules/workspace/effects.js.map +1 -1
- package/dist/src/modules/workspace/index.d.ts +246 -27
- package/dist/src/modules/workspace/index.d.ts.map +1 -1
- package/dist/src/modules/workspace/index.js +1216 -236
- package/dist/src/modules/workspace/index.js.map +1 -1
- package/dist/src/modules/workspace/read-page.d.ts +41 -0
- package/dist/src/modules/workspace/read-page.d.ts.map +1 -0
- package/dist/src/modules/workspace/read-page.js +213 -0
- package/dist/src/modules/workspace/read-page.js.map +1 -0
- package/dist/src/modules/workspace/result-text.d.ts +67 -0
- package/dist/src/modules/workspace/result-text.d.ts.map +1 -0
- package/dist/src/modules/workspace/result-text.js +94 -0
- package/dist/src/modules/workspace/result-text.js.map +1 -0
- package/dist/src/modules/workspace/text.d.ts +29 -12
- package/dist/src/modules/workspace/text.d.ts.map +1 -1
- package/dist/src/modules/workspace/text.js +35 -33
- package/dist/src/modules/workspace/text.js.map +1 -1
- package/dist/src/modules/workspace/types.d.ts +41 -17
- package/dist/src/modules/workspace/types.d.ts.map +1 -1
- package/dist/src/modules/workspace/types.js.map +1 -1
- package/dist/src/sent-rows.d.ts +59 -0
- package/dist/src/sent-rows.d.ts.map +1 -0
- package/dist/src/sent-rows.js +145 -0
- package/dist/src/sent-rows.js.map +1 -0
- package/dist/src/silent-heartbeat.d.ts +17 -10
- package/dist/src/silent-heartbeat.d.ts.map +1 -1
- package/dist/src/silent-heartbeat.js.map +1 -1
- package/dist/src/stable-hash.d.ts +3 -0
- package/dist/src/stable-hash.d.ts.map +1 -0
- package/dist/src/stable-hash.js +15 -0
- package/dist/src/stable-hash.js.map +1 -0
- package/dist/src/stored-injections.d.ts +99 -0
- package/dist/src/stored-injections.d.ts.map +1 -0
- package/dist/src/stored-injections.js +106 -0
- package/dist/src/stored-injections.js.map +1 -0
- package/dist/src/text-as-stored.d.ts +4 -3
- package/dist/src/text-as-stored.d.ts.map +1 -1
- package/dist/src/text-as-stored.js +4 -3
- package/dist/src/text-as-stored.js.map +1 -1
- package/dist/src/thinking-drops.d.ts +81 -0
- package/dist/src/thinking-drops.d.ts.map +1 -0
- package/dist/src/thinking-drops.js +103 -0
- package/dist/src/thinking-drops.js.map +1 -0
- package/dist/src/tool-image-ledger.d.ts +9 -8
- package/dist/src/tool-image-ledger.d.ts.map +1 -1
- package/dist/src/tool-image-ledger.js +9 -8
- package/dist/src/tool-image-ledger.js.map +1 -1
- package/dist/src/tool-presentation.d.ts.map +1 -1
- package/dist/src/tool-presentation.js +3 -1
- package/dist/src/tool-presentation.js.map +1 -1
- package/dist/src/tool-result-guard.d.ts +4 -2
- package/dist/src/tool-result-guard.d.ts.map +1 -1
- package/dist/src/tool-result-guard.js +21 -6
- package/dist/src/tool-result-guard.js.map +1 -1
- package/dist/src/tool-result-history.d.ts +186 -21
- package/dist/src/tool-result-history.d.ts.map +1 -1
- package/dist/src/tool-result-history.js +428 -89
- package/dist/src/tool-result-history.js.map +1 -1
- package/dist/src/tune-out/coordinator.d.ts +17 -9
- package/dist/src/tune-out/coordinator.d.ts.map +1 -1
- package/dist/src/tune-out/coordinator.js +84 -37
- package/dist/src/tune-out/coordinator.js.map +1 -1
- package/dist/src/types/agent.d.ts +186 -7
- package/dist/src/types/agent.d.ts.map +1 -1
- package/dist/src/types/events.d.ts +19 -2
- package/dist/src/types/events.d.ts.map +1 -1
- package/dist/src/types/framework.d.ts +24 -3
- package/dist/src/types/framework.d.ts.map +1 -1
- package/dist/src/types/module.d.ts +90 -7
- package/dist/src/types/module.d.ts.map +1 -1
- package/dist/src/types/trace.d.ts +57 -2
- package/dist/src/types/trace.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/agent.ts +593 -60
- package/src/api/server.ts +1 -2
- package/src/auto-continue.ts +42 -0
- package/src/code-execution/isolate/scripts.ts +11 -3
- package/src/framework.ts +4004 -1026
- package/src/gate/event-gate.ts +2315 -389
- package/src/gate/index.ts +2 -1
- package/src/gate/recipe-reconcile.ts +355 -0
- package/src/gate/types.ts +70 -8
- package/src/index.ts +7 -4
- package/src/mcpl/channel-registry.ts +1220 -467
- package/src/mcpl/conversation-router.ts +34 -16
- package/src/mcpl/convert-block.ts +14 -1
- package/src/mcpl/hook-orchestrator.ts +33 -6
- package/src/mcpl/index.ts +6 -1
- package/src/mcpl/inference-router.ts +79 -34
- package/src/mcpl/prose-grammar.ts +7 -0
- package/src/mcpl/protocol-family.ts +2 -0
- package/src/mcpl/push-coalescer.ts +51 -13
- package/src/mcpl/reference-fetcher.ts +201 -162
- package/src/mcpl/references.ts +852 -78
- package/src/mcpl/server-connection.ts +145 -7
- package/src/mcpl/tool-lifecycle.ts +21 -0
- package/src/mcpl/tool-presence.ts +650 -0
- package/src/mcpl/tool-result-normalize.ts +18 -6
- package/src/mcpl/tool-signature.ts +285 -0
- package/src/mcpl/toolset-host.ts +637 -0
- package/src/mcpl/toolsets-module.ts +451 -0
- package/src/mcpl/transport.ts +3 -2
- package/src/mcpl/types.ts +22 -1
- package/src/mcpl/visible-content.ts +22 -9
- package/src/module-registry.ts +77 -24
- package/src/modules/discord/index.ts +1 -1
- package/src/modules/history/index.ts +48 -18
- package/src/modules/history/semantic.ts +308 -54
- package/src/modules/workspace/direct-write.ts +425 -0
- package/src/modules/workspace/edit-text.ts +82 -35
- package/src/modules/workspace/effects.ts +1 -1
- package/src/modules/workspace/index.ts +1274 -241
- package/src/modules/workspace/read-page.ts +222 -0
- package/src/modules/workspace/result-text.ts +119 -0
- package/src/modules/workspace/text.ts +58 -32
- package/src/modules/workspace/types.ts +41 -17
- package/src/sent-rows.ts +162 -0
- package/src/silent-heartbeat.ts +17 -10
- package/src/stable-hash.ts +14 -0
- package/src/stored-injections.ts +131 -0
- package/src/text-as-stored.ts +4 -3
- package/src/thinking-drops.ts +141 -0
- package/src/tool-image-ledger.ts +9 -8
- package/src/tool-presentation.ts +3 -1
- package/src/tool-result-guard.ts +19 -6
- package/src/tool-result-history.ts +488 -88
- package/src/tune-out/coordinator.ts +80 -37
- package/src/types/agent.ts +189 -7
- package/src/types/events.ts +19 -2
- package/src/types/framework.ts +21 -3
- package/src/types/module.ts +86 -7
- package/src/types/trace.ts +58 -2
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCPL RFC-014 (tool presence, toolsets and surfaces): one server's tool
|
|
3
|
+
* presence as the host keeps it.
|
|
4
|
+
*
|
|
5
|
+
* The server's tool list stays its whole truth (RFC §2 principle 1); hints say
|
|
6
|
+
* which tools are static (the API tool list, as before) and which are dynamic
|
|
7
|
+
* (announced in the conversation as they come and go, and called through the
|
|
8
|
+
* host's `act`). Per server this keeps:
|
|
9
|
+
*
|
|
10
|
+
* - the tool definitions (tools/list) and the toolset declarations (manifest);
|
|
11
|
+
* - which declared toolsets and instances are open (toolsets/open results,
|
|
12
|
+
* toolsets/list, hint `toolsets` entries), and which undeclared groups the
|
|
13
|
+
* host opened itself (RFC §5.4: the server is never told);
|
|
14
|
+
* - the server's visibility statements per instance, and definition
|
|
15
|
+
* overrides, from hint changes applied in `rev` order, each at most once.
|
|
16
|
+
* One apply path serves every carrier: a call's, an open's or a close's
|
|
17
|
+
* result, a push's `hints`, a standalone `tools/hints`. `tools/list`
|
|
18
|
+
* (`mcpl/rev`) and `toolsets/list` are statements later changes build on;
|
|
19
|
+
* - which definitions the model has been shown, so a tool's first sentence
|
|
20
|
+
* is said once.
|
|
21
|
+
*
|
|
22
|
+
* Every mutation is bracketed by view() snapshots, and diff() turns the
|
|
23
|
+
* difference into what the model reads. Nothing here does I/O: the
|
|
24
|
+
* controller (toolset-host.ts) feeds it what the connection read.
|
|
25
|
+
*
|
|
26
|
+
* Ported from mcpl-cc-bridge's presence.ts, the reference host.
|
|
27
|
+
*/
|
|
28
|
+
import { objectSignature, renderToolLines, toolDigest, type HintedTool } from './tool-signature.js';
|
|
29
|
+
|
|
30
|
+
export interface ToolsetDecl {
|
|
31
|
+
title?: string;
|
|
32
|
+
summary?: string;
|
|
33
|
+
default?: 'open' | 'closed';
|
|
34
|
+
instances?: boolean;
|
|
35
|
+
options?: unknown;
|
|
36
|
+
}
|
|
37
|
+
export interface Suggestion { toolset: string; instance?: string; action: 'open' | 'close'; reason: string }
|
|
38
|
+
export interface OpenRecord { toolset: string; instance?: string; title?: string }
|
|
39
|
+
/** RFC-014 §5.3: an instance that ended other than by a toolsets/close. */
|
|
40
|
+
export interface Ended { id?: string; toolset: string; instance?: string; reason: string }
|
|
41
|
+
export type Presence = 'static' | 'dynamic';
|
|
42
|
+
|
|
43
|
+
/** The names of the host's meta-tools, as the model calls them. */
|
|
44
|
+
export interface MetaToolNames { open: string; act: string; look: string; close: string }
|
|
45
|
+
|
|
46
|
+
/** Toolset-less dynamic tools live under this key: always "open", visible per the server. */
|
|
47
|
+
export const NO_TOOLSET = '';
|
|
48
|
+
export const keyOf = (toolset: string, instance?: string): string => `${toolset}\u0000${instance ?? ''}`;
|
|
49
|
+
export const splitKey = (key: string): { toolset: string; instance?: string } => {
|
|
50
|
+
const [toolset, instance] = key.split('\u0000');
|
|
51
|
+
return { toolset: toolset ?? '', ...(instance ? { instance } : {}) };
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
interface Entry { title?: string; tools: Map<string, HintedTool> }
|
|
55
|
+
/** What the model may use right now: per key, the visible tools. Open-but-empty keys are present with no tools. */
|
|
56
|
+
export type View = Map<string, Entry>;
|
|
57
|
+
|
|
58
|
+
export interface Change {
|
|
59
|
+
key: string;
|
|
60
|
+
toolset: string;
|
|
61
|
+
instance?: string;
|
|
62
|
+
title?: string;
|
|
63
|
+
opened: boolean;
|
|
64
|
+
closed: boolean;
|
|
65
|
+
retitled: boolean;
|
|
66
|
+
appeared: HintedTool[];
|
|
67
|
+
gone: string[];
|
|
68
|
+
redefined: HintedTool[];
|
|
69
|
+
/** The visible tools after the change. */
|
|
70
|
+
now: HintedTool[];
|
|
71
|
+
}
|
|
72
|
+
export interface ChangeSet { changes: Change[]; suggest: Suggestion[]; notes: string[] }
|
|
73
|
+
|
|
74
|
+
export const emptyChange = (): ChangeSet => ({ changes: [], suggest: [], notes: [] });
|
|
75
|
+
export const isEmptyChange = (c: ChangeSet): boolean => !c.changes.length && !c.suggest.length && !c.notes.length;
|
|
76
|
+
export const joinChanges = (...sets: ChangeSet[]): ChangeSet => ({
|
|
77
|
+
changes: sets.flatMap((s) => s.changes),
|
|
78
|
+
suggest: sets.flatMap((s) => s.suggest),
|
|
79
|
+
notes: sets.flatMap((s) => s.notes),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const isObj = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
83
|
+
const arr = (v: unknown): unknown[] => (Array.isArray(v) ? v : []);
|
|
84
|
+
const str = (v: unknown): string | undefined => (typeof v === 'string' && v ? v : undefined);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* RFC-014 §6.1: revs on a connection are consecutive (1, 2, 3 …), and changes
|
|
88
|
+
* apply in rev order, which on stdio and WebSocket is arrival order.
|
|
89
|
+
* applied: a rev above the last, applied; `skipped` when it isn't exactly
|
|
90
|
+
* the last + 1 (a message was lost unread): recover.
|
|
91
|
+
* behind: at or below the last, not applied: recover.
|
|
92
|
+
*/
|
|
93
|
+
export interface ApplyOutcome { applied: boolean; skipped: boolean; behind: boolean; suggest: Suggestion[] }
|
|
94
|
+
|
|
95
|
+
/** What a hint `definition` may change (RFC §6.1): everything else stays the listed tool's. */
|
|
96
|
+
interface DefOverride { description?: string; inputSchema?: Record<string, unknown>; annotations?: Record<string, unknown> }
|
|
97
|
+
|
|
98
|
+
/** The fields a hint definition may change (RFC §6.1); `name` and all of `_meta` are ignored. */
|
|
99
|
+
export function overrideOf(d: Record<string, unknown>): DefOverride {
|
|
100
|
+
return {
|
|
101
|
+
...(typeof d.description === 'string' ? { description: d.description } : {}),
|
|
102
|
+
...(isObj(d.inputSchema) ? { inputSchema: d.inputSchema } : {}),
|
|
103
|
+
...(isObj(d.annotations) ? { annotations: d.annotations } : {}),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export class ToolPresence {
|
|
108
|
+
/** Hints are in effect on the current connection: the host advertised toolHints and the server did too. */
|
|
109
|
+
active = false;
|
|
110
|
+
declared: Record<string, ToolsetDecl> = {};
|
|
111
|
+
surface: { title?: string; summary?: string } | null = null;
|
|
112
|
+
/** The last rev read on this connection (RFC §6.1); 0 at each connect. */
|
|
113
|
+
rev = 0;
|
|
114
|
+
private tools = new Map<string, HintedTool>();
|
|
115
|
+
private open = new Map<string, OpenRecord>();
|
|
116
|
+
private groups = new Set<string>();
|
|
117
|
+
private visible = new Map<string, Set<string>>();
|
|
118
|
+
/** Definition overrides from hints: per key (an instance's own, or a toolset's), per tool. */
|
|
119
|
+
private defs = new Map<string, Map<string, DefOverride>>();
|
|
120
|
+
/** `ended` ids told to the agent: once per id, across reconnects (RFC §5.3). */
|
|
121
|
+
private toldEnded = new Set<string>();
|
|
122
|
+
private pendingMoves = new Map<string, Presence>();
|
|
123
|
+
private moved = new Map<string, Presence>();
|
|
124
|
+
/** Digests of definitions the model has been shown with their first sentence. */
|
|
125
|
+
readonly shown = new Set<string>();
|
|
126
|
+
|
|
127
|
+
constructor(
|
|
128
|
+
readonly server: string,
|
|
129
|
+
/** The meta-tools' names, for the hints rendered text gives. */
|
|
130
|
+
readonly names: MetaToolNames,
|
|
131
|
+
/** The host's tool policy (enabledTools/disabledTools): a tool it refuses is as if unlisted. */
|
|
132
|
+
private readonly allowed: (tool: string) => boolean = () => true,
|
|
133
|
+
/** How a static tool is named in the API tool list, for `look`. */
|
|
134
|
+
private readonly listedName: (tool: string) => string = (tool) => tool,
|
|
135
|
+
) {}
|
|
136
|
+
|
|
137
|
+
// ── connection lifecycle ──
|
|
138
|
+
|
|
139
|
+
/** A new connection: revs start over; hints count only when both sides advertised toolHints. */
|
|
140
|
+
beginConnection(mcpl: Record<string, unknown> | null | undefined, hostHints: boolean): void {
|
|
141
|
+
this.rev = 0;
|
|
142
|
+
// RFC §6.1: hint _meta and tools/hints from a server that did not advertise toolHints are ignored.
|
|
143
|
+
const active = hostHints && !!mcpl && mcpl.toolHints === true;
|
|
144
|
+
if (!active) {
|
|
145
|
+
if (this.active) this.deactivate();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
this.active = true;
|
|
149
|
+
this.setManifest(mcpl);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Toolset declarations and the surface hint, from initialize or a manifest re-fetch. */
|
|
153
|
+
setManifest(mcpl: Record<string, unknown> | null | undefined): void {
|
|
154
|
+
const prev = this.declared;
|
|
155
|
+
const decl: Record<string, ToolsetDecl> = {};
|
|
156
|
+
if (mcpl && isObj(mcpl.toolsets)) {
|
|
157
|
+
for (const [name, d] of Object.entries(mcpl.toolsets)) {
|
|
158
|
+
if (!isObj(d) || !name) continue;
|
|
159
|
+
decl[name] = {
|
|
160
|
+
...(str(d.title) ? { title: str(d.title) } : {}),
|
|
161
|
+
...(str(d.summary) ? { summary: str(d.summary) } : {}),
|
|
162
|
+
default: d.default === 'open' ? 'open' : 'closed',
|
|
163
|
+
...(d.instances === true ? { instances: true } : {}),
|
|
164
|
+
...(isObj(d.options) ? { options: d.options } : {}),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
this.declared = decl;
|
|
169
|
+
this.surface = mcpl && isObj(mcpl.surface) ? { title: str(mcpl.surface.title), summary: str(mcpl.surface.summary) } : null;
|
|
170
|
+
// RFC §5.1: removing a toolset, or turning its instances on or off, ends
|
|
171
|
+
// its open instances; one that becomes undeclared starts closed under the
|
|
172
|
+
// host's gating.
|
|
173
|
+
for (const [k, r] of [...this.open]) {
|
|
174
|
+
if (!decl[r.toolset] || !!decl[r.toolset]!.instances !== !!prev[r.toolset]?.instances) this.dropKey(k);
|
|
175
|
+
}
|
|
176
|
+
for (const name of Object.keys(prev)) {
|
|
177
|
+
if (decl[name]) continue;
|
|
178
|
+
this.groups.delete(name);
|
|
179
|
+
for (const k of new Set([...this.visible.keys(), ...this.defs.keys()])) if (splitKey(k).toolset === name) this.dropKey(k);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** RFC §5.1: a server that withdraws toolHints is hint-less from then on: every tool static, nothing dynamic presented. */
|
|
184
|
+
deactivate(): void {
|
|
185
|
+
this.active = false;
|
|
186
|
+
this.declared = {};
|
|
187
|
+
this.surface = null;
|
|
188
|
+
this.open.clear();
|
|
189
|
+
this.groups.clear();
|
|
190
|
+
this.visible.clear();
|
|
191
|
+
this.defs.clear();
|
|
192
|
+
this.pendingMoves.clear();
|
|
193
|
+
this.moved.clear();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** tools/list: every definition, visible or not (RFC §4.1). A tool the host's policy refuses is left out. */
|
|
197
|
+
setTools(list: HintedTool[]): void {
|
|
198
|
+
this.tools = new Map(list
|
|
199
|
+
.filter((t) => isObj(t) && typeof t.name === 'string' && this.allowed(t.name))
|
|
200
|
+
.map((t) => [t.name, t]));
|
|
201
|
+
for (const set of this.visible.values()) for (const n of [...set]) if (!this.tools.has(n)) set.delete(n);
|
|
202
|
+
// The list's `mcpl/visible` is the server's word only where nothing else
|
|
203
|
+
// speaks: toolset-less dynamic tools and undeclared groups. For a declared
|
|
204
|
+
// toolset it's an aggregate ("visible in some open instance"); per-instance
|
|
205
|
+
// state comes from toolsets/list and hint changes (RFC §4.1, §5.3).
|
|
206
|
+
for (const t of this.tools.values()) {
|
|
207
|
+
if (this.presenceOf(t.name) !== 'dynamic') continue;
|
|
208
|
+
const ts = this.toolsetOf(t.name);
|
|
209
|
+
if (ts && this.isDeclared(ts)) continue;
|
|
210
|
+
this.setVisible(keyOf(ts ?? NO_TOOLSET), t.name, t._meta?.['mcpl/visible'] !== false);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ── reading ──
|
|
215
|
+
|
|
216
|
+
/** Whether the meta-tools speak for this server: hints in effect and something dynamic. */
|
|
217
|
+
get presents(): boolean {
|
|
218
|
+
if (!this.active) return false;
|
|
219
|
+
if (Object.keys(this.declared).length) return true;
|
|
220
|
+
for (const n of this.tools.keys()) if (this.presenceOf(n) === 'dynamic') return true;
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
get allTools(): HintedTool[] {
|
|
225
|
+
return [...this.tools.values()];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
tool(name: string): HintedTool | undefined {
|
|
229
|
+
return this.tools.get(name);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
toolsetOf(name: string): string | undefined {
|
|
233
|
+
const m = this.tools.get(name)?._meta;
|
|
234
|
+
return m && typeof m['mcpl/toolset'] === 'string' && m['mcpl/toolset'] ? (m['mcpl/toolset'] as string) : undefined;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** static unless hinted dynamic; a toolset wins over any "static" (RFC §4.1); a move applies only at a boundary (applyMoves). */
|
|
238
|
+
presenceOf(name: string): Presence {
|
|
239
|
+
if (!this.active) return 'static';
|
|
240
|
+
if (this.toolsetOf(name)) return 'dynamic';
|
|
241
|
+
const moved = this.moved.get(name);
|
|
242
|
+
if (moved) return moved;
|
|
243
|
+
return this.tools.get(name)?._meta?.['mcpl/presence'] === 'dynamic' ? 'dynamic' : 'static';
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
isDeclared(toolset: string): boolean {
|
|
247
|
+
return Object.hasOwn(this.declared, toolset);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Undeclared toolset names the server's tools are tagged with (RFC §5.4). */
|
|
251
|
+
get undeclaredGroups(): string[] {
|
|
252
|
+
const out = new Set<string>();
|
|
253
|
+
for (const n of this.tools.keys()) {
|
|
254
|
+
const ts = this.toolsetOf(n);
|
|
255
|
+
if (ts && !this.isDeclared(ts)) out.add(ts);
|
|
256
|
+
}
|
|
257
|
+
return [...out];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
openRecords(toolset?: string): OpenRecord[] {
|
|
261
|
+
return [...this.open.values()].filter((r) => toolset === undefined || r.toolset === toolset);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
isOpen(toolset: string, instance?: string): boolean {
|
|
265
|
+
return this.isDeclared(toolset) ? this.open.has(keyOf(toolset, instance)) : this.groups.has(toolset);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
get openGroups(): string[] {
|
|
269
|
+
return [...this.groups];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* The definition the model sees, and the host checks arguments against, for
|
|
274
|
+
* `name` in `key`: the instance's own (a hint definition with `instance`),
|
|
275
|
+
* else the toolset's (one without), else the listed one (RFC §6.1).
|
|
276
|
+
*/
|
|
277
|
+
defOf(key: string, name: string): HintedTool {
|
|
278
|
+
const listed = this.tools.get(name)!;
|
|
279
|
+
const { toolset } = splitKey(key);
|
|
280
|
+
const o = this.defs.get(key)?.get(name) ?? (toolset ? this.defs.get(keyOf(toolset))?.get(name) : undefined);
|
|
281
|
+
return o ? { ...listed, ...o } : listed;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
view(): View {
|
|
285
|
+
const v: View = new Map();
|
|
286
|
+
for (const [key, r] of this.open) v.set(key, { title: r.title, tools: new Map() });
|
|
287
|
+
for (const g of this.groups) if (!this.isDeclared(g)) v.set(keyOf(g), { tools: new Map() });
|
|
288
|
+
for (const name of this.tools.keys()) {
|
|
289
|
+
if (this.presenceOf(name) !== 'dynamic') continue;
|
|
290
|
+
const ts = this.toolsetOf(name);
|
|
291
|
+
if (ts && this.isDeclared(ts)) {
|
|
292
|
+
for (const [key, r] of this.open) if (r.toolset === ts && this.visible.get(key)?.has(name)) v.get(key)!.tools.set(name, this.defOf(key, name));
|
|
293
|
+
} else if (ts) {
|
|
294
|
+
// Two controllers (RFC §4.1, §5.4): the host has the group open AND the server says visible.
|
|
295
|
+
const key = keyOf(ts);
|
|
296
|
+
if (this.groups.has(ts) && this.visible.get(key)?.has(name)) v.get(key)!.tools.set(name, this.defOf(key, name));
|
|
297
|
+
} else {
|
|
298
|
+
const key = keyOf(NO_TOOLSET);
|
|
299
|
+
if (this.visible.get(key)?.has(name)) {
|
|
300
|
+
if (!v.has(key)) v.set(key, { tools: new Map() });
|
|
301
|
+
v.get(key)!.tools.set(name, this.defOf(key, name));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return v;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
diff(before: View, after: View): ChangeSet {
|
|
309
|
+
const out = emptyChange();
|
|
310
|
+
for (const key of new Set([...before.keys(), ...after.keys()])) {
|
|
311
|
+
const b = before.get(key);
|
|
312
|
+
const a = after.get(key);
|
|
313
|
+
const isGroupKey = splitKey(key).toolset !== NO_TOOLSET;
|
|
314
|
+
const appeared: HintedTool[] = [];
|
|
315
|
+
const redefined: HintedTool[] = [];
|
|
316
|
+
for (const [n, d] of a?.tools ?? []) {
|
|
317
|
+
const was = b?.tools.get(n);
|
|
318
|
+
if (!was) appeared.push(d);
|
|
319
|
+
else if (toolDigest(was) !== toolDigest(d)) redefined.push(d);
|
|
320
|
+
}
|
|
321
|
+
const gone = [...(b?.tools.keys() ?? [])].filter((n) => !a?.tools.has(n));
|
|
322
|
+
const opened = isGroupKey && !b && !!a;
|
|
323
|
+
const closed = isGroupKey && !!b && !a;
|
|
324
|
+
const retitled = !!a && !!b && !!a.title && a.title !== b.title;
|
|
325
|
+
if (!appeared.length && !redefined.length && !gone.length && !opened && !closed && !retitled) continue;
|
|
326
|
+
out.changes.push({ key, ...splitKey(key), title: a?.title ?? b?.title, opened, closed, retitled, appeared, gone, redefined, now: [...(a?.tools.values() ?? [])] });
|
|
327
|
+
}
|
|
328
|
+
return out;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ── the one apply path ──
|
|
332
|
+
|
|
333
|
+
/** Apply a HintChanges (RFC §6.1) from any of its carriers, as it is read (see ApplyOutcome). */
|
|
334
|
+
apply(h: unknown): ApplyOutcome {
|
|
335
|
+
const none: ApplyOutcome = { applied: false, skipped: false, behind: false, suggest: [] };
|
|
336
|
+
if (!isObj(h) || typeof h.rev !== 'number' || !Number.isFinite(h.rev)) return none;
|
|
337
|
+
if (h.rev <= this.rev) return { ...none, behind: true };
|
|
338
|
+
const skipped = h.rev !== this.rev + 1;
|
|
339
|
+
this.rev = h.rev;
|
|
340
|
+
return { ...none, applied: true, skipped, suggest: this.applyBody(h) };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* tools/list's `_meta["mcpl/rev"]` (RFC §4.1): the last rev the server had
|
|
345
|
+
* sent when it wrote the list, which takes none itself. Read in order it
|
|
346
|
+
* equals the last rev read; a higher one means a message was lost (true).
|
|
347
|
+
*/
|
|
348
|
+
noteListRev(rev: unknown): boolean {
|
|
349
|
+
if (typeof rev !== 'number' || !Number.isFinite(rev)) return false;
|
|
350
|
+
const lost = rev > this.rev;
|
|
351
|
+
this.rev = Math.max(this.rev, rev);
|
|
352
|
+
return lost;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
private applyBody(h: Record<string, unknown>): Suggestion[] {
|
|
356
|
+
for (const e of arr(h.toolsets)) {
|
|
357
|
+
if (!isObj(e) || !str(e.toolset) || !this.isDeclared(e.toolset as string)) continue; // undeclared toolsets are host-local
|
|
358
|
+
const ts = e.toolset as string;
|
|
359
|
+
const withInstances = !!this.declared[ts]!.instances;
|
|
360
|
+
const instance = withInstances ? str(e.instance) : undefined;
|
|
361
|
+
if (withInstances && !instance) continue; // an instance-less statement about a toolset with instances names nothing
|
|
362
|
+
const key = keyOf(ts, instance);
|
|
363
|
+
// "open" opens, confirms, or (with a new title) retitles (RFC §6.1);
|
|
364
|
+
// it may come from another connection of the same principal (§5.6).
|
|
365
|
+
if (e.state === 'open') this.open.set(key, { toolset: ts, ...(instance ? { instance } : {}), title: str(e.title) ?? this.open.get(key)?.title });
|
|
366
|
+
else if (e.state === 'closed') this.dropKey(key);
|
|
367
|
+
}
|
|
368
|
+
for (const e of arr(h.tools)) {
|
|
369
|
+
if (!isObj(e) || !str(e.name) || !this.tools.has(e.name as string)) continue;
|
|
370
|
+
const name = e.name as string;
|
|
371
|
+
// A toolset's tool is dynamic whatever a hint says (RFC §4.1).
|
|
372
|
+
if ((e.presence === 'static' || e.presence === 'dynamic') && !this.toolsetOf(name)) {
|
|
373
|
+
// RFC §4.2: a move applies at a boundary the host chooses; until then the tool stays where it is.
|
|
374
|
+
if (e.presence !== this.presenceOf(name)) this.pendingMoves.set(name, e.presence);
|
|
375
|
+
else this.pendingMoves.delete(name);
|
|
376
|
+
}
|
|
377
|
+
if (isObj(e.definition) && this.presenceOf(name) === 'dynamic') {
|
|
378
|
+
const key = this.defKeyFor(name, str(e.instance));
|
|
379
|
+
if (key !== null) {
|
|
380
|
+
if (!this.defs.has(key)) this.defs.set(key, new Map());
|
|
381
|
+
this.defs.get(key)!.set(name, overrideOf(e.definition));
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (typeof e.visible === 'boolean') for (const key of this.keysFor(name, str(e.instance))) this.setVisible(key, name, e.visible);
|
|
385
|
+
}
|
|
386
|
+
const suggest: Suggestion[] = [];
|
|
387
|
+
for (const s of arr(h.suggest)) {
|
|
388
|
+
if (!isObj(s) || !str(s.toolset) || (s.action !== 'open' && s.action !== 'close')) continue;
|
|
389
|
+
suggest.push({ toolset: s.toolset as string, ...(str(s.instance) ? { instance: str(s.instance) } : {}), action: s.action, reason: str(s.reason) ?? '' });
|
|
390
|
+
}
|
|
391
|
+
return suggest;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Where a definition lands (RFC §6.1): with `instance`, that instance's own;
|
|
396
|
+
* without, the toolset's (for a toolset without instances, its only one's).
|
|
397
|
+
*/
|
|
398
|
+
private defKeyFor(name: string, instance: string | undefined): string | null {
|
|
399
|
+
const ts = this.toolsetOf(name);
|
|
400
|
+
if (!ts) return keyOf(NO_TOOLSET);
|
|
401
|
+
return keyOf(ts, this.declared[ts]?.instances ? instance : undefined);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Where a visibility statement lands. For a toolset with instances, `instance` is required; a statement without one is ignored. */
|
|
405
|
+
private keysFor(name: string, instance: string | undefined): string[] {
|
|
406
|
+
const ts = this.toolsetOf(name);
|
|
407
|
+
if (!ts) return [keyOf(NO_TOOLSET)];
|
|
408
|
+
const decl = this.declared[ts];
|
|
409
|
+
if (!decl?.instances) return [keyOf(ts)];
|
|
410
|
+
return instance ? [keyOf(ts, instance)] : [];
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
private setVisible(key: string, name: string, on: boolean): void {
|
|
414
|
+
let s = this.visible.get(key);
|
|
415
|
+
if (!s) this.visible.set(key, (s = new Set()));
|
|
416
|
+
if (on) s.add(name);
|
|
417
|
+
else s.delete(name);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private dropKey(key: string): void {
|
|
421
|
+
this.open.delete(key);
|
|
422
|
+
this.visible.delete(key);
|
|
423
|
+
this.defs.delete(key);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** The result of a toolsets/open: the instance is open whatever its hints say. */
|
|
427
|
+
noteOpened(toolset: string, instance: string | undefined, title: string | undefined): void {
|
|
428
|
+
if (!this.isDeclared(toolset)) return;
|
|
429
|
+
const withInstances = !!this.declared[toolset]!.instances;
|
|
430
|
+
if (withInstances && !instance) return;
|
|
431
|
+
const key = keyOf(toolset, withInstances ? instance : undefined);
|
|
432
|
+
this.open.set(key, { toolset, ...(withInstances && instance ? { instance } : {}), title: title ?? this.open.get(key)?.title });
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** toolsets/close succeeded: closed, whatever the hints said. */
|
|
436
|
+
noteClosed(toolset: string, instance: string | undefined): void {
|
|
437
|
+
this.dropKey(keyOf(toolset, this.declared[toolset]?.instances ? instance : undefined));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* toolsets/list (RFC §5.3): what the principal has open, with the full hint
|
|
442
|
+
* state of its dynamic tools, the truth for declared toolsets and dynamic
|
|
443
|
+
* tools outside them. Anything the host believed open and the server doesn't
|
|
444
|
+
* report is closed, never re-opened here: opening can start a new game.
|
|
445
|
+
* Returns what was lost, what the server says ended while no connection was
|
|
446
|
+
* told, its suggestions, and whether its rev skipped one.
|
|
447
|
+
*/
|
|
448
|
+
adopt(result: unknown): { lost: OpenRecord[]; ended: Ended[]; suggest: Suggestion[]; skipped: boolean } {
|
|
449
|
+
const had = [...this.open.values()];
|
|
450
|
+
// A full statement: a dynamic tool not stated visible is invisible.
|
|
451
|
+
// Undeclared groups stay as the host has them open or closed.
|
|
452
|
+
for (const k of new Set([...this.open.keys(), ...this.visible.keys(), ...this.defs.keys()])) {
|
|
453
|
+
if (this.isDeclared(splitKey(k).toolset)) this.dropKey(k);
|
|
454
|
+
else {
|
|
455
|
+
this.visible.delete(k);
|
|
456
|
+
this.defs.delete(k);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
const r = isObj(result) ? result : {};
|
|
460
|
+
for (const o of arr(r.open)) {
|
|
461
|
+
if (!isObj(o) || !str(o.toolset)) continue;
|
|
462
|
+
this.noteOpened(o.toolset as string, str(o.instance), str(o.title));
|
|
463
|
+
}
|
|
464
|
+
let suggest: Suggestion[] = [];
|
|
465
|
+
let skipped = false;
|
|
466
|
+
const h = isObj(r._meta) ? r._meta['mcpl/hints'] : undefined;
|
|
467
|
+
if (isObj(h)) {
|
|
468
|
+
suggest = this.applyBody(h);
|
|
469
|
+
// Its own rev, at its place in order (RFC §6.1).
|
|
470
|
+
if (typeof h.rev === 'number' && Number.isFinite(h.rev)) {
|
|
471
|
+
skipped = h.rev !== this.rev + 1;
|
|
472
|
+
this.rev = Math.max(this.rev, h.rev);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
const ended: Ended[] = [];
|
|
476
|
+
for (const e of arr(r.ended)) {
|
|
477
|
+
if (!isObj(e) || !str(e.toolset)) continue;
|
|
478
|
+
ended.push({ ...(str(e.id) ? { id: str(e.id) } : {}), toolset: e.toolset as string, ...(str(e.instance) ? { instance: str(e.instance) } : {}), reason: str(e.reason) ?? '' });
|
|
479
|
+
}
|
|
480
|
+
return { lost: had.filter((o) => !this.open.has(keyOf(o.toolset, o.instance))), ended, suggest, skipped };
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* What the agent should hear about an adoption: what ended while it was
|
|
485
|
+
* away, each `ended` id once however often the server repeats it (RFC
|
|
486
|
+
* §5.3), and what is closed now.
|
|
487
|
+
*/
|
|
488
|
+
adoptionNotes(lost: OpenRecord[], ended: Ended[]): string[] {
|
|
489
|
+
const notes: string[] = [];
|
|
490
|
+
const endedKeys = new Set(ended.map((e) => keyOf(e.toolset, e.instance)));
|
|
491
|
+
for (const e of ended) {
|
|
492
|
+
if (e.id) {
|
|
493
|
+
if (this.toldEnded.has(e.id)) continue;
|
|
494
|
+
this.toldEnded.add(e.id);
|
|
495
|
+
}
|
|
496
|
+
notes.push(`${this.label(keyOf(e.toolset, e.instance))} ended${e.reason ? `: ${e.reason}` : ''}.`);
|
|
497
|
+
}
|
|
498
|
+
for (const o of lost) {
|
|
499
|
+
if (endedKeys.has(keyOf(o.toolset, o.instance))) continue;
|
|
500
|
+
notes.push(`${this.label(keyOf(o.toolset, o.instance))} is closed: ${this.server} no longer has it open. ${this.openHint(o.toolset)} starts a new one.`);
|
|
501
|
+
}
|
|
502
|
+
return notes;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// ── host-made opens of undeclared groups (RFC §5.4) ──
|
|
506
|
+
|
|
507
|
+
openGroup(g: string): void {
|
|
508
|
+
this.groups.add(g);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
closeGroup(g: string): void {
|
|
512
|
+
this.groups.delete(g);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// ── boundaries ──
|
|
516
|
+
|
|
517
|
+
/** Apply pending static↔dynamic moves (RFC §4.2) at a boundary the host chooses. Returns what moved. */
|
|
518
|
+
applyMoves(): Array<{ name: string; to: Presence }> {
|
|
519
|
+
const out: Array<{ name: string; to: Presence }> = [];
|
|
520
|
+
for (const [name, to] of this.pendingMoves) {
|
|
521
|
+
if (!this.tools.has(name)) continue;
|
|
522
|
+
this.moved.set(name, to);
|
|
523
|
+
out.push({ name, to });
|
|
524
|
+
}
|
|
525
|
+
this.pendingMoves.clear();
|
|
526
|
+
return out;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// ── rendering ──
|
|
530
|
+
|
|
531
|
+
/** The marker a key's announcements begin with, `[server/toolset:instance]`, which a reminder looks for. */
|
|
532
|
+
label(key: string): string {
|
|
533
|
+
const { toolset, instance } = splitKey(key);
|
|
534
|
+
return `[${this.server}${toolset ? `/${toolset}` : ''}${instance ? `:${instance}` : ''}]`;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
callHint(key: string): string {
|
|
538
|
+
const { instance } = splitKey(key);
|
|
539
|
+
return `${this.names.act} {server:"${this.server}"${instance ? `, instance:"${instance}"` : ''}, tool, args}`;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
closeHint(key: string): string | null {
|
|
543
|
+
const { toolset, instance } = splitKey(key);
|
|
544
|
+
if (!toolset) return null;
|
|
545
|
+
return `${this.names.close} {server:"${this.server}", toolset:"${toolset}"${instance ? `, instance:"${instance}"` : ''}}`;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
openHint(toolset: string, instance?: string): string {
|
|
549
|
+
return `${this.names.open} {server:"${this.server}", toolset:"${toolset}"${instance ? `, instance:"${instance}"` : ''}}`;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** The tool block for one key: its label, how to call, and the visible tools (new or changed ones described). */
|
|
553
|
+
toolBlock(key: string, tools: HintedTool[], opts: { describeAll?: boolean; headed?: boolean; title?: string } = {}): string[] {
|
|
554
|
+
const head = opts.headed === false ? '' : `${this.label(key)}${opts.title ? ` ${opts.title}` : ''} `;
|
|
555
|
+
if (!tools.length) return [`${head}no tools available right now.`];
|
|
556
|
+
return [`${head}tools now — ${this.callHint(key)}:`, ...renderToolLines(tools, this.shown, { describeAll: opts.describeAll })];
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** What the model reads for a change set. `bare`: a key whose header the caller already printed. */
|
|
560
|
+
render(cs: ChangeSet, opts: { bare?: string } = {}): string {
|
|
561
|
+
const lines: string[] = [];
|
|
562
|
+
for (const c of cs.changes) {
|
|
563
|
+
const bare = opts.bare === c.key;
|
|
564
|
+
if (c.closed) {
|
|
565
|
+
lines.push(`${this.label(c.key)} closed.${c.gone.length ? ` Now unavailable: ${c.gone.join(', ')}.` : ''}`);
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
const title = (c.opened || c.retitled) && !bare ? c.title : undefined;
|
|
569
|
+
const setChanged = c.opened || c.appeared.length > 0 || c.redefined.length > 0 || c.gone.length > 0;
|
|
570
|
+
if (setChanged) lines.push(...this.toolBlock(c.key, c.now, { headed: !bare, title }));
|
|
571
|
+
else if (title) lines.push(`${this.label(c.key)} ${title}`);
|
|
572
|
+
if (c.gone.length) lines.push(`now unavailable: ${c.gone.join(', ')}`);
|
|
573
|
+
if (c.opened && !bare) {
|
|
574
|
+
const close = this.closeHint(c.key);
|
|
575
|
+
if (close) lines.push(`${close} closes it.`);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
for (const s of cs.suggest) lines.push(this.renderSuggestion(s));
|
|
579
|
+
lines.push(...cs.notes);
|
|
580
|
+
return lines.join('\n');
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** RFC §6.2: a suggestion is shown, never acted on. */
|
|
584
|
+
renderSuggestion(s: Suggestion): string {
|
|
585
|
+
const what = `${s.toolset}${s.instance ? `:${s.instance}` : ''}`;
|
|
586
|
+
const how = s.action === 'open' ? this.openHint(s.toolset, s.instance) : this.closeHint(keyOf(s.toolset, s.instance));
|
|
587
|
+
return `${this.server} suggests ${s.action === 'open' ? 'opening' : 'closing'} ${what}${s.reason ? `: ${s.reason}` : ''} — ${how} if you want to (nothing was done).`;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/** The keys a reminder restates: every open key of this server's view. */
|
|
591
|
+
openKeys(): string[] {
|
|
592
|
+
return [...this.view().keys()];
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** RFC §8.3 reminder: what is open, its current tools (described), how to call and close it. Null when nothing is open. */
|
|
596
|
+
reminder(): string | null {
|
|
597
|
+
const v = this.view();
|
|
598
|
+
if (!v.size) return null;
|
|
599
|
+
const lines: string[] = [];
|
|
600
|
+
for (const [key, e] of v) {
|
|
601
|
+
lines.push(...this.toolBlock(key, [...e.tools.values()], { describeAll: true, title: e.title }));
|
|
602
|
+
const close = this.closeHint(key);
|
|
603
|
+
if (close) lines.push(` ${close} closes it.`);
|
|
604
|
+
}
|
|
605
|
+
return lines.join('\n');
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** look naming this server: what is open (with tools) and what can be opened. */
|
|
609
|
+
catalog(): string {
|
|
610
|
+
const lines: string[] = [];
|
|
611
|
+
lines.push(this.surface ? `${this.server} — ${[this.surface.title, this.surface.summary].filter(Boolean).join(': ')}` : this.server);
|
|
612
|
+
const v = this.view();
|
|
613
|
+
if (v.size) {
|
|
614
|
+
for (const [key, e] of v) {
|
|
615
|
+
lines.push(...this.toolBlock(key, [...e.tools.values()], { describeAll: true, title: e.title }).map((l) => ` ${l}`));
|
|
616
|
+
const close = this.closeHint(key);
|
|
617
|
+
if (close) lines.push(` ${close} closes it.`);
|
|
618
|
+
}
|
|
619
|
+
} else {
|
|
620
|
+
lines.push(' nothing open');
|
|
621
|
+
}
|
|
622
|
+
const openable: string[] = [];
|
|
623
|
+
for (const [name, d] of Object.entries(this.declared)) {
|
|
624
|
+
if (!d.instances && this.isOpen(name)) continue;
|
|
625
|
+
openable.push(
|
|
626
|
+
` ${name}${d.title ? ` — ${d.title}` : ''}${d.summary ? `: ${d.summary}` : ''}` +
|
|
627
|
+
(d.instances ? ' (several can be open at once; name each with instance)' : '') +
|
|
628
|
+
(d.options ? ` · options ${objectSignature(d.options)}` : ''),
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
for (const g of this.undeclaredGroups) {
|
|
632
|
+
if (this.groups.has(g)) continue;
|
|
633
|
+
const names = this.allTools.filter((t) => this.toolsetOf(t.name) === g).map((t) => t.name);
|
|
634
|
+
openable.push(` ${g}: ${names.join(', ')}`);
|
|
635
|
+
}
|
|
636
|
+
if (openable.length) lines.push(`can open — ${this.names.open} {server:"${this.server}", toolset, instance?, options?}:`, ...openable);
|
|
637
|
+
return lines.join('\n');
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/** Where a tool can be called from now, for `look` naming it. */
|
|
641
|
+
toolStatus(tool: string): string {
|
|
642
|
+
if (this.presenceOf(tool) === 'static') return `static — in your tool list as ${this.listedName(tool)}`;
|
|
643
|
+
const where = [...this.view().entries()].filter(([, e]) => e.tools.has(tool)).map(([k]) => k);
|
|
644
|
+
const ts = this.toolsetOf(tool);
|
|
645
|
+
return where.length
|
|
646
|
+
? `available now in ${where.map((k) => this.label(k)).join(', ')} — ${this.names.act} {server:"${this.server}", tool:"${tool}"${where.length > 1 ? ', instance' : ''}, args}`
|
|
647
|
+
: `not available right now${ts ? ` (toolset ${ts})` : ''}`;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
}
|