all files / integrations/ webmcp-adapter.js

94.62% Statements 123/130
90.36% Branches 75/83
97.06% Functions 33/34
95.24% Lines 120/126
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 4511×   1× 1× 1× 41× 41× 41× 41× 41×   1× 1×   1× 44× 44× 44× 43× 43×   1×       1× 101× 2×   99× 99×       1× 41× 41×   1× 7× 7× 1× 13×     6×   7×   1× 62× 62× 62× 57×   5× 5×     42×   1× 57× 57× 57× 2× 2×   55× 55×     55×     55× 55× 9×   46× 2×   55× 55× 54× 50× 50× 50× 44×     6× 78×         4×       1×   55× 55× 598× 598×           4× 4× 4× 3× 2×     1×       1×           598× 595× 14×       55×   1× 73× 73×                               2× 2× 1×   1×                                   1×                       2× 2× 1× 1×                                     1×                                             1× 1×                             1× 1×                                                 1× 1×                                                 1× 1×                                                   1× 1×                                                 1× 1×                                         1× 1×                                               1× 1×                                               1× 1×               1× 3×     3× 1×   3× 3×   1×   1×    
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WebMcpAdapter = (function () {
        function WebMcpAdapter(parent) {
            this.registeredTools = [];
            this.parent = parent;
            this.registrationController = typeof AbortController !== 'undefined' ? new AbortController() : undefined;
            this.bindEvents();
            this.registerAutomatically();
        }
        WebMcpAdapter.prototype.getModuleName = function () {
            return 'WebMcpAdapter';
        };
        WebMcpAdapter.prototype.registerAutomatically = function (attempt) {
            var _this = this;
            if (attempt === void 0) { attempt = 0; }
            if (this.getModelContext()) {
                this.parent.notify('registerWebMcpTools', { prefix: '', tools: [], exposedTo: [] });
                return;
            }
            Iif (attempt < 100 && typeof window !== 'undefined') {
                this.registrationTimer = window.setTimeout(function () { return _this.registerAutomatically(attempt + 1); }, 100);
            }
        };
        WebMcpAdapter.prototype.getModelContext = function () {
            if (typeof window !== 'undefined' && window.navigator.modelContext) {
                return window.navigator.modelContext;
            }
            Eif (typeof document !== 'undefined') {
                return document.modelContext;
            }
            return undefined;
        };
        WebMcpAdapter.prototype.bindEvents = function () {
            this.parent.on('getWebMcpTools', this.getWebMcpTools, this);
            this.parent.on('registerWebMcpTools', this.registerWebMcpTools, this);
        };
        WebMcpAdapter.prototype.getWebMcpTools = function (args) {
            var availableTools = this.getAvailableTools();
            if (args.toolNames && args.toolNames.length > 0) {
                var toolNames_1 = args.toolNames;
                args.tools = availableTools.filter(function (tool) { return toolNames_1.indexOf(tool.name) !== -1; });
            }
            else {
                args.tools = availableTools;
            }
            args.tools = this.filterConfiguredTools(args.tools);
        };
        WebMcpAdapter.prototype.filterConfiguredTools = function (tools) {
            var settings = this.parent.webMcpSettings;
            var configuredTools = settings && settings.tools;
            if (!configuredTools || configuredTools.length === 0 || typeof configuredTools[0] !== 'string') {
                return tools;
            }
            var toolNames = configuredTools;
            Iif (!toolNames || toolNames.length === 0) {
                return tools;
            }
            return tools.filter(function (tool) { return toolNames.indexOf(tool.name) !== -1; });
        };
        WebMcpAdapter.prototype.registerWebMcpTools = function (args) {
            var _this = this;
            var modelContext = this.getModelContext();
            if (!modelContext) {
                console.warn('WebMCP is not available in this environment');
                return;
            }
            var settings = this.parent.webMcpSettings;
            Iif (args.tools === undefined && settings && settings.tools) {
                args.tools = settings.tools;
            }
            Iif (args.exposedTo === undefined && settings && settings.exposedTo) {
                args.exposedTo = settings.exposedTo;
            }
            var prefix = '';
            if (args.prefix === undefined && this.parent.element.id) {
                prefix = (settings && settings.name ? settings.name : this.parent.element.id) + '_';
            }
            else if (args.prefix) {
                prefix = args.prefix + '_';
            }
            var toolsToRegister = [];
            if (Array.isArray(args.tools)) {
                if (args.tools.length === 0 || typeof args.tools[0] === 'string') {
                    var toolNames_2 = args.tools;
                    var availableTools = this.getAvailableTools();
                    if (toolNames_2.length === 0) {
                        toolsToRegister = availableTools;
                    }
                    else {
                        toolsToRegister = availableTools.filter(function (tool) {
                            return toolNames_2.length === 0 || toolNames_2.indexOf(tool.name) !== -1;
                        });
                    }
                }
                else {
                    toolsToRegister = args.tools;
                }
            }
            else {
                toolsToRegister = this.getAvailableTools();
            }
            toolsToRegister = this.filterConfiguredTools(toolsToRegister);
            toolsToRegister.forEach(function (tool) {
                var toolName = prefix + tool.name;
                var registrationResult = modelContext.registerTool({
                    name: toolName,
                    title: tool.title,
                    description: tool.description,
                    inputSchema: tool.inputSchema,
                    execute: function (input) {
                        return new Promise(function (resolve, reject) {
                            try {
                                var result = tool.execute(input);
                                if (result && typeof result.then === 'function') {
                                    result.then(resolve).catch(reject);
                                }
                                else {
                                    resolve(result);
                                }
                            }
                            catch (error) {
                                reject(error);
                            }
                        });
                    },
                    annotations: tool.annotations
                }, _this.registrationController ? { signal: _this.registrationController.signal } : undefined);
                if (registrationResult && typeof registrationResult.catch === 'function') {
                    registrationResult.catch(function (error) {
                        console.error('Failed to register tool ' + toolName + ':', error);
                    });
                }
            });
            this.registeredTools = toolsToRegister;
        };
        WebMcpAdapter.prototype.getAvailableTools = function () {
            var _this = this;
            return [
                {
                    name: 'getCardDetails',
                    title: 'Get Card Details',
                    description: 'Returns the card details based on card ID from the board.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            targetId: {
                                type: 'string',
                                description: 'The ID of the card to get details for'
                            }
                        },
                        required: ['targetId']
                    },
                    execute: function (input) {
                        var cardElement = _this.parent.element.querySelector('[data-id="' + input.targetId + '"]');
                        if (cardElement) {
                            return _this.parent.getCardDetails(cardElement);
                        }
                        throw new Error('Card with ID ' + input.targetId + ' not found');
                    }
                },
                {
                    name: 'getColumnData',
                    title: 'Get Column Data',
                    description: 'Returns the column data based on column key input.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            columnKey: {
                                type: 'string',
                                description: 'The key of the column to get data for'
                            }
                        },
                        required: ['columnKey']
                    },
                    execute: function (input) {
                        return _this.parent.getColumnData(input.columnKey);
                    }
                },
                {
                    name: 'getSelectedCards',
                    title: 'Get Selected Cards',
                    description: 'Gets the list of selected cards from the board.',
                    inputSchema: {
                        type: 'object',
                        properties: {}
                    },
                    execute: function () {
                        var selectedCards = _this.parent.getSelectedCards();
                        return selectedCards.map(function (card) {
                            var id = card.getAttribute('data-id');
                            return _this.parent.getCardDetails(card) || { id: id };
                        });
                    }
                },
                {
                    name: 'getSwimlaneData',
                    title: 'Get Swimlane Data',
                    description: 'Returns the cards that belong to the specified swimlane key.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            keyField: {
                                type: 'string',
                                description: 'The swimlane key whose cards should be returned'
                            }
                        },
                        required: ['keyField']
                    },
                    execute: function (input) {
                        return _this.parent.getSwimlaneData(input.keyField);
                    }
                },
                {
                    name: 'openDialog',
                    title: 'Open Dialog',
                    description: 'Opens the Kanban dialog for adding or editing a card.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            action: {
                                type: 'string',
                                enum: ['Add', 'Edit'],
                                description: 'The dialog action to perform'
                            },
                            data: {
                                type: 'object',
                                description: 'Optional card data to edit'
                            }
                        },
                        required: ['action']
                    },
                    execute: function (input) {
                        _this.parent.openDialog(input.action, input.data);
                        return { success: true, message: 'Dialog opened successfully' };
                    },
                    annotations: {
                        readOnlyHint: false
                    }
                },
                {
                    name: 'closeDialog',
                    title: 'Close Dialog',
                    description: 'Closes the currently open Kanban dialog.',
                    inputSchema: {
                        type: 'object',
                        properties: {}
                    },
                    execute: function () {
                        _this.parent.closeDialog();
                        return { success: true, message: 'Dialog closed successfully' };
                    },
                    annotations: {
                        readOnlyHint: false
                    }
                },
                {
                    name: 'addCard',
                    title: 'Add Card',
                    description: 'Adds the new card to the data source of Kanban and layout.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            cardData: {
                                type: 'object',
                                description: 'The card data to add'
                            },
                            index: {
                                type: 'number',
                                description: 'Optional index to insert the card at'
                            }
                        },
                        required: ['cardData']
                    },
                    execute: function (input) {
                        _this.parent.addCard(input.cardData, input.index);
                        return { success: true, message: 'Card added successfully' };
                    },
                    annotations: {
                        readOnlyHint: false
                    }
                },
                {
                    name: 'updateCard',
                    title: 'Update Card',
                    description: 'Updates the changes made in the card object by passing it as a parameter to the data source.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            cardData: {
                                type: 'object',
                                description: 'The card data to update'
                            },
                            index: {
                                type: 'number',
                                description: 'Optional index to update the card at'
                            }
                        },
                        required: ['cardData']
                    },
                    execute: function (input) {
                        _this.parent.updateCard(input.cardData, input.index);
                        return { success: true, message: 'Card updated successfully' };
                    },
                    annotations: {
                        readOnlyHint: false
                    }
                },
                {
                    name: 'deleteCard',
                    title: 'Delete Card',
                    description: 'Deletes the card based on the provided ID or card collection in the argument list.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            cardData: {
                                oneOf: [
                                    { type: 'string', description: 'The ID of the card to delete' },
                                    { type: 'number', description: 'The ID of the card to delete' },
                                    { type: 'object', description: 'Single card object to delete' },
                                    { type: 'array', items: { type: 'object' }, description: 'Collection of card objects to delete' }
                                ],
                                description: 'The card data to delete'
                            }
                        },
                        required: ['cardData']
                    },
                    execute: function (input) {
                        _this.parent.deleteCard(input.cardData);
                        return { success: true, message: 'Card deleted successfully' };
                    },
                    annotations: {
                        readOnlyHint: false
                    }
                },
                {
                    name: 'addColumn',
                    title: 'Add Column',
                    description: 'Add the column to Kanban board dynamically based on the provided column options and index.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            columnOptions: {
                                type: 'object',
                                description: 'The properties to new column that are going to be added in the board'
                            },
                            index: {
                                type: 'number',
                                description: 'The index of column to add the new column'
                            }
                        },
                        required: ['columnOptions', 'index']
                    },
                    execute: function (input) {
                        _this.parent.addColumn(input.columnOptions, input.index);
                        return { success: true, message: 'Column added successfully' };
                    },
                    annotations: {
                        readOnlyHint: false
                    }
                },
                {
                    name: 'deleteColumn',
                    title: 'Delete Column',
                    description: 'Deletes the column based on the provided index value.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            index: {
                                type: 'number',
                                description: 'The index of column to delete the existing column from Kanban board'
                            }
                        },
                        required: ['index']
                    },
                    execute: function (input) {
                        _this.parent.deleteColumn(input.index);
                        return { success: true, message: 'Column deleted successfully' };
                    },
                    annotations: {
                        readOnlyHint: false
                    }
                },
                {
                    name: 'showColumn',
                    title: 'Show Column',
                    description: 'Shows the column from hidden based on the provided key in the columns.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            key: {
                                oneOf: [
                                    { type: 'string' },
                                    { type: 'number' }
                                ],
                                description: 'The hidden column key name to be shown from the hidden state in board'
                            }
                        },
                        required: ['key']
                    },
                    execute: function (input) {
                        _this.parent.showColumn(input.key);
                        return { success: true, message: 'Column shown successfully' };
                    },
                    annotations: {
                        readOnlyHint: false
                    }
                },
                {
                    name: 'hideColumn',
                    title: 'Hide Column',
                    description: 'Hides the column from Kanban board based on the provided key in the columns.',
                    inputSchema: {
                        type: 'object',
                        properties: {
                            key: {
                                oneOf: [
                                    { type: 'string' },
                                    { type: 'number' }
                                ],
                                description: 'The visible column key name to be hidden from the board'
                            }
                        },
                        required: ['key']
                    },
                    execute: function (input) {
                        _this.parent.hideColumn(input.key);
                        return { success: true, message: 'Column hidden successfully' };
                    },
                    annotations: {
                        readOnlyHint: false
                    }
                }
            ];
        };
        WebMcpAdapter.prototype.destroy = function () {
            Iif (this.registrationTimer) {
                window.clearTimeout(this.registrationTimer);
            }
            if (this.registrationController && !this.registrationController.signal.aborted) {
                this.registrationController.abort();
            }
            this.parent.off('getWebMcpTools', this.getWebMcpTools);
            this.parent.off('registerWebMcpTools', this.registerWebMcpTools);
        };
        return WebMcpAdapter;
    }());
    exports.WebMcpAdapter = WebMcpAdapter;
});