11import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
22import type { ChannelMessageCapability } from "../../channels/plugins/message-capabilities.js" ;
3+ import {
4+ createDiscordMessageToolComponentsSchema ,
5+ createMessageToolButtonsSchema ,
6+ createSlackMessageToolBlocksSchema ,
7+ createTelegramPollExtraToolSchemas ,
8+ } from "../../channels/plugins/message-tool-schema.js" ;
39import type { ChannelMessageActionName , ChannelPlugin } from "../../channels/plugins/types.js" ;
410import type { MessageActionRunResult } from "../../infra/outbound/message-action-runner.js" ;
5- import { setActivePluginRegistry } from "../../plugins/runtime.js" ;
6- import { createTestRegistry } from "../../test-utils/channel-plugins.js" ;
7- import { createMessageTool } from "./message-tool.js" ;
11+ type CreateMessageTool = typeof import ( "./message-tool.js" ) . createMessageTool ;
12+ type SetActivePluginRegistry = typeof import ( "../../plugins/runtime.js" ) . setActivePluginRegistry ;
13+ type CreateTestRegistry = typeof import ( "../../test-utils/channel-plugins.js" ) . createTestRegistry ;
14+
15+ let createMessageTool : CreateMessageTool ;
16+ let setActivePluginRegistry : SetActivePluginRegistry ;
17+ let createTestRegistry : CreateTestRegistry ;
818
919const mocks = vi . hoisted ( ( ) => ( {
1020 runMessageAction : vi . fn ( ) ,
@@ -50,21 +60,25 @@ function mockSendResult(overrides: { channel?: string; to?: string } = {}) {
5060 } satisfies MessageActionRunResult ) ;
5161}
5262
53- function getToolProperties ( tool : ReturnType < typeof createMessageTool > ) {
63+ function getToolProperties ( tool : ReturnType < CreateMessageTool > ) {
5464 return ( tool . parameters as { properties ?: Record < string , unknown > } ) . properties ?? { } ;
5565}
5666
5767function getActionEnum ( properties : Record < string , unknown > ) {
5868 return ( properties . action as { enum ?: string [ ] } | undefined ) ?. enum ?? [ ] ;
5969}
6070
61- beforeEach ( ( ) => {
71+ beforeEach ( async ( ) => {
72+ vi . resetModules ( ) ;
6273 mocks . runMessageAction . mockReset ( ) ;
6374 mocks . loadConfig . mockReset ( ) . mockReturnValue ( { } ) ;
6475 mocks . resolveCommandSecretRefsViaGateway . mockReset ( ) . mockImplementation ( async ( { config } ) => ( {
6576 resolvedConfig : config ,
6677 diagnostics : [ ] ,
6778 } ) ) ;
79+ ( { setActivePluginRegistry } = await import ( "../../plugins/runtime.js" ) ) ;
80+ ( { createTestRegistry } = await import ( "../../test-utils/channel-plugins.js" ) ) ;
81+ ( { createMessageTool } = await import ( "./message-tool.js" ) ) ;
6882} ) ;
6983
7084function createChannelPlugin ( params : {
@@ -75,6 +89,7 @@ function createChannelPlugin(params: {
7589 actions ?: ChannelMessageActionName [ ] ;
7690 listActions ?: NonNullable < NonNullable < ChannelPlugin [ "actions" ] > [ "listActions" ] > ;
7791 capabilities ?: readonly ChannelMessageCapability [ ] ;
92+ toolSchema ?: NonNullable < NonNullable < ChannelPlugin [ "actions" ] > [ "getToolSchema" ] > ;
7893 messaging ?: ChannelPlugin [ "messaging" ] ;
7994} ) : ChannelPlugin {
8095 const actionCapabilities = params . capabilities ;
@@ -102,6 +117,7 @@ function createChannelPlugin(params: {
102117 ...( actionCapabilities
103118 ? { getCapabilities : ( _params : { cfg : unknown } ) => actionCapabilities }
104119 : { } ) ,
120+ ...( params . toolSchema ? { getToolSchema : params . toolSchema } : { } ) ,
105121 } ,
106122 } ;
107123}
@@ -219,6 +235,17 @@ describe("message tool schema scoping", () => {
219235 blurb : "Telegram test plugin." ,
220236 actions : [ "send" , "react" , "poll" ] ,
221237 capabilities : [ "interactive" , "buttons" ] ,
238+ toolSchema : ( ) => [
239+ {
240+ properties : {
241+ buttons : createMessageToolButtonsSchema ( ) ,
242+ } ,
243+ } ,
244+ {
245+ properties : createTelegramPollExtraToolSchemas ( ) ,
246+ visibility : "all-configured" ,
247+ } ,
248+ ] ,
222249 } ) ;
223250
224251 const discordPlugin = createChannelPlugin ( {
@@ -228,6 +255,11 @@ describe("message tool schema scoping", () => {
228255 blurb : "Discord test plugin." ,
229256 actions : [ "send" , "poll" , "poll-vote" ] ,
230257 capabilities : [ "interactive" , "components" ] ,
258+ toolSchema : ( ) => ( {
259+ properties : {
260+ components : createDiscordMessageToolComponentsSchema ( ) ,
261+ } ,
262+ } ) ,
231263 } ) ;
232264
233265 const slackPlugin = createChannelPlugin ( {
@@ -237,6 +269,11 @@ describe("message tool schema scoping", () => {
237269 blurb : "Slack test plugin." ,
238270 actions : [ "send" , "react" ] ,
239271 capabilities : [ "interactive" , "blocks" ] ,
272+ toolSchema : ( ) => ( {
273+ properties : {
274+ blocks : createSlackMessageToolBlocksSchema ( ) ,
275+ } ,
276+ } ) ,
240277 } ) ;
241278
242279 afterEach ( ( ) => {
@@ -365,6 +402,25 @@ describe("message tool schema scoping", () => {
365402 return telegramCfg ?. actions ?. poll === false ? [ "send" , "react" ] : [ "send" , "react" , "poll" ] ;
366403 } ,
367404 capabilities : [ "interactive" , "buttons" ] ,
405+ toolSchema : ( { cfg } ) => {
406+ const telegramCfg = ( cfg as { channels ?: { telegram ?: { actions ?: { poll ?: boolean } } } } )
407+ . channels ?. telegram ;
408+ return [
409+ {
410+ properties : {
411+ buttons : createMessageToolButtonsSchema ( ) ,
412+ } ,
413+ } ,
414+ ...( telegramCfg ?. actions ?. poll === false
415+ ? [ ]
416+ : [
417+ {
418+ properties : createTelegramPollExtraToolSchemas ( ) ,
419+ visibility : "all-configured" as const ,
420+ } ,
421+ ] ) ,
422+ ] ;
423+ } ,
368424 } ) ;
369425
370426 setActivePluginRegistry (
@@ -393,6 +449,95 @@ describe("message tool schema scoping", () => {
393449 expect ( properties . pollAnonymous ) . toBeUndefined ( ) ;
394450 expect ( properties . pollPublic ) . toBeUndefined ( ) ;
395451 } ) ;
452+
453+ it ( "uses discovery account scope for capability-gated shared fields" , ( ) => {
454+ const scopedInteractivePlugin = createChannelPlugin ( {
455+ id : "telegram" ,
456+ label : "Telegram" ,
457+ docsPath : "/channels/telegram" ,
458+ blurb : "Telegram test plugin." ,
459+ actions : [ "send" ] ,
460+ toolSchema : ( ) => null ,
461+ } ) ;
462+ scopedInteractivePlugin . actions = {
463+ ...scopedInteractivePlugin . actions ,
464+ getCapabilities : ( { accountId } ) => ( accountId === "ops" ? [ "interactive" ] : [ ] ) ,
465+ } ;
466+
467+ setActivePluginRegistry (
468+ createTestRegistry ( [
469+ { pluginId : "telegram" , source : "test" , plugin : scopedInteractivePlugin } ,
470+ ] ) ,
471+ ) ;
472+
473+ const scopedTool = createMessageTool ( {
474+ config : { } as never ,
475+ currentChannelProvider : "telegram" ,
476+ agentAccountId : "ops" ,
477+ } ) ;
478+ const unscopedTool = createMessageTool ( {
479+ config : { } as never ,
480+ currentChannelProvider : "telegram" ,
481+ } ) ;
482+
483+ expect ( getToolProperties ( scopedTool ) . interactive ) . toBeDefined ( ) ;
484+ expect ( getToolProperties ( unscopedTool ) . interactive ) . toBeUndefined ( ) ;
485+ } ) ;
486+
487+ it ( "routes full discovery context into plugin action discovery" , ( ) => {
488+ const seenContexts : Record < string , unknown > [ ] = [ ] ;
489+ const contextPlugin = createChannelPlugin ( {
490+ id : "discord" ,
491+ label : "Discord" ,
492+ docsPath : "/channels/discord" ,
493+ blurb : "Discord context plugin." ,
494+ listActions : ( ctx ) => {
495+ seenContexts . push ( { phase : "listActions" , ...ctx } ) ;
496+ return [ "send" , "react" ] ;
497+ } ,
498+ toolSchema : ( ctx ) => {
499+ seenContexts . push ( { phase : "getToolSchema" , ...ctx } ) ;
500+ return null ;
501+ } ,
502+ } ) ;
503+ contextPlugin . actions = {
504+ ...contextPlugin . actions ,
505+ getCapabilities : ( ctx ) => {
506+ seenContexts . push ( { phase : "getCapabilities" , ...ctx } ) ;
507+ return [ "interactive" ] ;
508+ } ,
509+ } ;
510+
511+ setActivePluginRegistry (
512+ createTestRegistry ( [ { pluginId : "discord" , source : "test" , plugin : contextPlugin } ] ) ,
513+ ) ;
514+
515+ createMessageTool ( {
516+ config : { } as never ,
517+ currentChannelProvider : "discord" ,
518+ currentChannelId : "channel:123" ,
519+ currentThreadTs : "thread-456" ,
520+ currentMessageId : "msg-789" ,
521+ agentAccountId : "ops" ,
522+ agentSessionKey : "agent:alpha:main" ,
523+ sessionId : "session-123" ,
524+ requesterSenderId : "user-42" ,
525+ } ) ;
526+
527+ expect ( seenContexts ) . toContainEqual (
528+ expect . objectContaining ( {
529+ currentChannelProvider : "discord" ,
530+ currentChannelId : "channel:123" ,
531+ currentThreadTs : "thread-456" ,
532+ currentMessageId : "msg-789" ,
533+ accountId : "ops" ,
534+ sessionKey : "agent:alpha:main" ,
535+ sessionId : "session-123" ,
536+ agentId : "alpha" ,
537+ requesterSenderId : "user-42" ,
538+ } ) ,
539+ ) ;
540+ } ) ;
396541} ) ;
397542
398543describe ( "message tool description" , ( ) => {
@@ -405,7 +550,27 @@ describe("message tool description", () => {
405550 label : "BlueBubbles" ,
406551 docsPath : "/channels/bluebubbles" ,
407552 blurb : "BlueBubbles test plugin." ,
408- actions : [ "react" , "renameGroup" , "addParticipant" , "removeParticipant" , "leaveGroup" ] ,
553+ listActions : ( { currentChannelId } ) => {
554+ const all : ChannelMessageActionName [ ] = [
555+ "react" ,
556+ "renameGroup" ,
557+ "addParticipant" ,
558+ "removeParticipant" ,
559+ "leaveGroup" ,
560+ ] ;
561+ const lowered = currentChannelId ?. toLowerCase ( ) ?? "" ;
562+ const isDmTarget =
563+ lowered . includes ( "chat_guid:imessage;-;" ) || lowered . includes ( "chat_guid:sms;-;" ) ;
564+ return isDmTarget
565+ ? all . filter (
566+ ( action ) =>
567+ action !== "renameGroup" &&
568+ action !== "addParticipant" &&
569+ action !== "removeParticipant" &&
570+ action !== "leaveGroup" ,
571+ )
572+ : all ;
573+ } ,
409574 messaging : {
410575 normalizeTarget : ( raw ) => {
411576 const trimmed = raw . trim ( ) . replace ( / ^ b l u e b u b b l e s : / i, "" ) ;
0 commit comments