Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 7x 7x 7x 67x 121x 121x 119x 2x 153x 148x 148x 148x 148x 279x 279x 275x 275x 272x 3x 4x 577x 852x 275x 36x 239x 1x 239x 432x 3x 429x 429x 6x 423x 423x 48x 423x 51x 423x 148x 148x 1x 147x 147x 147x 6x 147x 7x 147x 5x 147x | /*
Copyright (C) 2026 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { DiagramEditExecution } from '../types/ai-config.types';
import { GigaChatFunctionSchema } from '../types/gigachat.types';
import { RequestWithBenefits } from '@back/controllers/gigachat';
import { DocumentConfigType } from './types/DocumentConfigType';
import { ToolConfigType } from './types/ToolConfigType';
import { ToolInterface } from './types/ToolInterface';
import { ToolTypes } from './types/ToolTypes';
import { getPluginTool } from './pluginToolRegistry';
import { getLoggerWithTag } from '@global/logger/v2/logger.mjs';
type ToolMap = Record<string, ToolInterface>;
const DEFAULT_TOOL_TYPE = ToolTypes.jsonata;
const LOGGER_NAME = 'ToolManager';
const logger = getLoggerWithTag(LOGGER_NAME);
export class ToolManager {
private tools: ToolMap;
constructor() {
this.tools = {} as ToolMap;
}
registerTool(tool: ToolInterface) {
const { type } = tool;
if (!this.tools[type]) {
this.tools[type] = tool;
} else {
logger.warn(() => `Tool with type "${type}" already exist`);
}
}
async callTool(
name: string,
args: any,
toolConfig: any,
sessionConfig: any,
sessionId: string,
request?: RequestWithBenefits,
diagramEdit?: DiagramEditExecution
): Promise<unknown> {
const validatedToolConfig = this.validateToolConfig(toolConfig);
const validatedDocumentConfig = this.validateDocumentConfig(sessionConfig);
if (diagramEdit !== undefined) validatedDocumentConfig.diagramEdit = diagramEdit;
const tool = this.findTool(validatedToolConfig.type);
return tool.execute(
args,
toolConfig,
validatedDocumentConfig,
sessionId,
request
);
}
getToolSchema(rawToolConfig: any): GigaChatFunctionSchema | undefined {
try {
const config = this.validateToolConfig(rawToolConfig);
const schema = this.isPluginTool(config.type)
? this.getPluginToolSchema(config)
: this.getOwnToolSchema(config);
if(schema && typeof schema === 'object') {
return schema;
}
logger.warn(() => `Invalid tool schema: ${JSON.stringify(rawToolConfig)}`);
} catch (e) {
return;
}
}
// Свои инструменты экземпляра, затем реестр плагинов — при каждом обращении, без копирования
private findTool(type: string): ToolInterface | undefined {
return this.getOwnTool(type) || getPluginTool(type);
}
// Только собственные свойства: тип вроде "constructor" не должен находить Object.prototype
private getOwnTool(type: string): ToolInterface | undefined {
return Object.prototype.hasOwnProperty.call(this.tools, type) ? this.tools[type] : undefined;
}
private isPluginTool(type: string): boolean {
return !this.getOwnTool(type) && !!getPluginTool(type);
}
private getOwnToolSchema(config: ToolConfigType): GigaChatFunctionSchema | undefined {
return config?.schema ? config?.schema : this.tools?.[config.type]?.schema;
}
// Схему инструмента плагина сценарий не переопределяет
private getPluginToolSchema(config: ToolConfigType): GigaChatFunctionSchema | undefined {
if (config.schema) {
logger.warn(() => `Scenario schema is ignored for plugin tool "${config.type}"`);
}
return getPluginTool(config.type)?.schema;
}
private validateToolConfig(rawToolConfig: any): ToolConfigType {
if (!(rawToolConfig && typeof rawToolConfig === 'object')) {
throw new Error('Invalid tool config format');
}
const toolType =
typeof rawToolConfig?.type === 'string'
? rawToolConfig?.type
: DEFAULT_TOOL_TYPE;
if (!this.findTool(toolType)) {
throw new Error(
`Tool with type "${rawToolConfig.type}" is not registered`
);
}
const config = {
type: toolType
};
if (rawToolConfig?.query) {
Object.assign(config, { query: rawToolConfig?.query });
}
if (rawToolConfig?.schema) {
Object.assign(config, { schema: rawToolConfig?.schema });
}
return config as ToolConfigType;
}
private validateDocumentConfig(sessionConfig: any): DocumentConfigType {
const config: any = {};
if (!(sessionConfig && typeof sessionConfig === 'object')) {
return config;
}
const { jsonataOrigin: origin, jsonataParams: params, profile, diagramWorkflow } = sessionConfig;
if (diagramWorkflow === true) config.diagramWorkflow = true;
if (origin && (typeof origin === 'string' || typeof origin === 'object')) {
config.origin = origin;
}
if (params && typeof params === 'object') {
config.params = params;
}
if(profile) {
config.profile = profile;
}
return config;
}
}
|