HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/agentcloud/webapp/src/db/tool.ts
'use strict';

import * as db from 'db/index';
import debug from 'debug';
import toObjectId from 'misc/toobjectid';
import { InsertResult } from 'struct/db';
import GlobalTools from 'struct/globaltools';
import { Tool } from 'struct/tool';

const log = debug('webapp:db:tools');

export function ToolCollection(): any {
	return db.db().collection('tools');
}

export async function initGlobalTools() {
	if (GlobalTools.length === 0) {
		log('No global tools found.');
		return;
	}
	await ToolCollection().deleteMany({ 'data.builtin': true }); //monkey patch until we have a better deployment flow for alpha
	return ToolCollection().bulkWrite(GlobalTools.map(gt => ({
		replaceOne: {
			filter: { 'data.builtin': true, name: gt.name },
			replacement: gt,
			upsert: true,
		}
	})));
}

export function getToolById(teamId: db.IdOrStr, toolId: db.IdOrStr): Promise<Tool> {
	return ToolCollection().findOne({
		_id: toObjectId(toolId),
		$or: [
			{ teamId: toObjectId(teamId) },
			{ 'data.builtin': true },
		],
	});
}

export function getToolsById(teamId: db.IdOrStr, toolIds: db.IdOrStr[]): Promise<Tool[]> {
	return ToolCollection().find({
		_id: {
			$in: toolIds.map(toObjectId),
		},
		$or: [
			{ teamId: toObjectId(teamId) },
			{ 'data.builtin': true },
		],
	}).toArray();
}

export function getToolsByTeam(teamId: db.IdOrStr): Promise<Tool[]> {
	return ToolCollection().find({
		$or: [
			{ teamId: toObjectId(teamId) },
			{ 'data.builtin': true },
		],
	}).toArray();
}

export async function addTool(tool: Tool): Promise<InsertResult> {
	return ToolCollection().insertOne(tool);
}

export async function editTool(teamId: db.IdOrStr, toolId: db.IdOrStr, tool: Partial<Tool>): Promise<InsertResult> {
	return ToolCollection().updateOne({
		_id: toObjectId(toolId),
		teamId: toObjectId(teamId),
	}, {
		$set: tool,
	});
}

export async function editToolsForDatasource(teamId: db.IdOrStr, datasourceId: db.IdOrStr, update: any): Promise<InsertResult> {
	return ToolCollection().updateOne({
		teamId: toObjectId(teamId),
		datasourceId: toObjectId(datasourceId),
	}, {
		$set: update, //Note: any type and not a Partial because we use a mongo dot notation update
	});
}

export function deleteToolById(teamId: db.IdOrStr, toolId: db.IdOrStr): Promise<any> {
	return ToolCollection().deleteOne({
		_id: toObjectId(toolId),
		teamId: toObjectId(teamId),
	});
}