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