export async function getTools()

in src/lib/server/textGeneration/tools.ts [25:61]


export async function getTools(
	toolsPreference: Array<string>,
	assistant: Pick<Assistant, "rag" | "tools"> | undefined
): Promise<Tool[]> {
	let preferences = toolsPreference;

	if (assistant) {
		if (assistant?.tools?.length) {
			preferences = assistant.tools;

			if (assistantHasWebSearch(assistant)) {
				preferences.push(websearch._id.toString());
			}
		} else {
			if (assistantHasWebSearch(assistant)) {
				return [websearch, directlyAnswer];
			}
			return [directlyAnswer];
		}
	}

	// filter based on tool preferences, add the tools that are on by default
	const activeConfigTools = toolFromConfigs.filter((el) => {
		if (el.isLocked && el.isOnByDefault && !assistant) return true;
		return preferences?.includes(el._id.toString()) ?? (el.isOnByDefault && !assistant);
	});

	// find tool where the id is in preferences
	const activeCommunityTools = await collections.tools
		.find({
			_id: { $in: preferences.map((el) => new ObjectId(el)) },
		})
		.toArray()
		.then((el) => el.map((el) => ({ ...el, call: getCallMethod(el) })));

	return [...activeConfigTools, ...activeCommunityTools];
}