1- import { Context , Dict } from 'koishi'
1+ import { Awaitable , Context , Dict , Session } from 'koishi'
22import {
33 BasePlatformClient ,
44 PlatformEmbeddingsClient ,
@@ -28,6 +28,7 @@ import { computed, ComputedRef, reactive } from '@vue/reactivity'
2828import { randomUUID } from 'crypto'
2929import { RunnableConfig } from '@langchain/core/runnables'
3030import { ToolMask } from '../agent'
31+ import type { ConversationRecord } from '../../services/conversation_types'
3132
3233export class PlatformService {
3334 private _platformClients : Record < string , BasePlatformClient > = reactive ( { } )
@@ -36,6 +37,7 @@ export class PlatformService {
3637
3738 private _tools : Record < string , ChatLunaTool > = reactive ( { } )
3839 private _tmpTools : Record < string , StructuredTool > = reactive ( { } )
40+ private _toolMaskResolvers : Record < string , ToolMaskResolver > = { }
3941 private _models : Record < string , ModelInfo [ ] > = reactive ( { } )
4042 private _chatChains : Record < string , ChatLunaChainInfo > = reactive ( { } )
4143 private _vectorStore : Record < string , CreateVectorStoreFunction > = reactive (
@@ -218,6 +220,23 @@ export class PlatformService {
218220 return allNames . filter ( ( name ) => ! mask . deny . includes ( name ) )
219221 }
220222
223+ registerToolMaskResolver ( name : string , resolver : ToolMaskResolver ) {
224+ this . _toolMaskResolvers [ name ] = resolver
225+
226+ return ( ) => {
227+ delete this . _toolMaskResolvers [ name ]
228+ }
229+ }
230+
231+ async resolveToolMask ( arg : ToolMaskArg ) {
232+ for ( const name in this . _toolMaskResolvers ) {
233+ const mask = await this . _toolMaskResolvers [ name ] ( arg )
234+ if ( mask ) {
235+ return mask
236+ }
237+ }
238+ }
239+
221240 static buildToolMask ( rule : {
222241 mode ?: 'inherit' | 'all' | 'allow' | 'deny'
223242 allow ?: string [ ]
@@ -453,3 +472,13 @@ declare module 'koishi' {
453472 'chatluna/tool-updated' : ( service : PlatformService ) => void
454473 }
455474}
475+
476+ export interface ToolMaskArg {
477+ session : Session
478+ conversation ?: ConversationRecord
479+ bindingKey ?: string
480+ }
481+
482+ export type ToolMaskResolver = (
483+ arg : ToolMaskArg
484+ ) => Awaitable < ToolMask | undefined >
0 commit comments