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/agent.ts
'use strict';

import * as db from 'db/index';
import { Agent, CodeExecutionConfigType } from 'struct/agent';
import { InsertResult } from 'struct/db';

import toObjectId from '../lib/misc/toobjectid';

export function AgentCollection(): any {
	return db.db().collection('agents');
}

export function getAgentById(teamId: db.IdOrStr, agentId: db.IdOrStr): Promise<Agent> {
	return AgentCollection().findOne({
		_id: toObjectId(agentId),
		teamId: toObjectId(teamId),
	});
}

export function getAgentsById(teamId: db.IdOrStr, agentIds: db.IdOrStr[]): Promise<Agent[]> {
	return AgentCollection().find({
		_id: {
			$in: agentIds.map(toObjectId),
		},
		teamId: toObjectId(teamId),
	}).toArray();
}

export function getAgentsByTeam(teamId: db.IdOrStr): Promise<Agent[]> {
	return AgentCollection().aggregate([
		{
			$match: {
				teamId: toObjectId(teamId),
			}
		}, {
			$lookup: { from: 'groups', as: 'group', localField: '_id', foreignField: 'agents' }
		}, {
			$addFields: {
				isGroupSet: { $cond: { if: { $gt: [{ $size: '$group' }, 0] }, then: true, else: false } },
			}
		}, {
			$project: {
				isGroupSet: 0,
				tempGroup: 0,
			}
		}
	]).toArray();
}

export function getAgents(teamId: db.IdOrStr, agentIds: db.IdOrStr[]): Promise<Agent[]> {
	return AgentCollection().find({
		teamId: toObjectId(teamId),
		_id: {
			$in: agentIds.map(toObjectId)
		},
	}).toArray();
}

export async function addAgent(agent: Agent): Promise<InsertResult> {
	return AgentCollection().insertOne(agent);
}

export async function addAgents(agents: Agent[]): Promise<InsertResult> {
	return AgentCollection().insertMany(agents);
}

export async function updateAgent(teamId: db.IdOrStr, agentId: db.IdOrStr, agent: Agent): Promise<InsertResult> {
	return AgentCollection().updateOne({
		_id: toObjectId(agentId),
		teamId: toObjectId(teamId),
	}, {
		$set: agent,
	});
}

export function removeAgentsModel(teamId: db.IdOrStr, modelId: db.IdOrStr): Promise<any> {
	return AgentCollection().updateMany({
		teamId: toObjectId(teamId),
		modelId: toObjectId(modelId)
	}, {
		$unset: {
			modelId: '',
		},
	});
}

export function removeAgentsTool(teamId: db.IdOrStr, toolId: db.IdOrStr): Promise<any> {
	return AgentCollection().updateMany({
		teamId: toObjectId(teamId),
		toolIds: toObjectId(toolId)
	}, {
		$pull: {
			toolIds: toObjectId(toolId),
		},
	});
}

export function deleteAgentById(teamId: db.IdOrStr, agentId: db.IdOrStr): Promise<any> {
	return AgentCollection().deleteOne({
		_id: toObjectId(agentId),
		teamId: toObjectId(teamId),
	});
}

export async function getAgentNameMap(teamId: db.IdOrStr, agentIds: db.IdOrStr[] = []): Promise<Agent[]> {
	const agents = await AgentCollection().find({
		teamId: toObjectId(teamId),
		_id: {
			$in: agentIds.map(toObjectId)
		},
	}).toArray();
	return (agents||[]).reduce((acc, x) => {
		acc[x.name] = x?.icon?.filename;
		return acc;
	}, {});
}