import { agentTool } from "./utils-iC0Uutcf.js";
import { z } from "zod";

//#region src/tools/search-events/utils.ts
function getStringValue(event, key, defaultValue = "") {
	const value = event[key];
	return typeof value === "string" ? value : defaultValue;
}
function getNumberValue(event, key) {
	const value = event[key];
	return typeof value === "number" ? value : void 0;
}
function isAggregateQuery(fields) {
	return fields.some((field) => field.includes("(") && field.includes(")"));
}
async function fetchCustomAttributes(apiService, organizationSlug, dataset, projectId, timeParams) {
	const customAttributes = {};
	const fieldTypes = {};
	if (dataset === "errors") {
		const tagsResponse = await apiService.listTags({
			organizationSlug,
			dataset: "events",
			project: projectId,
			statsPeriod: "14d",
			useCache: true,
			useFlagsBackend: true
		});
		for (const tag of tagsResponse) if (tag.key && !tag.key.startsWith("sentry:")) customAttributes[tag.key] = tag.name || tag.key;
	} else {
		const itemType = dataset === "logs" ? "logs" : "spans";
		const attributesResponse = await apiService.listTraceItemAttributes({
			organizationSlug,
			itemType,
			project: projectId,
			statsPeriod: "14d"
		});
		for (const attr of attributesResponse) if (attr.key && !attr.key.startsWith("sentry:")) {
			customAttributes[attr.key] = attr.name || attr.key;
			if (attr.type && (attr.type === "string" || attr.type === "number")) fieldTypes[attr.key] = attr.type;
		}
	}
	return {
		attributes: customAttributes,
		fieldTypes
	};
}
/**
* Create a tool for the agent to query available attributes by dataset
* The tool is pre-bound with the API service and organization configured for the appropriate region
*/
function createDatasetAttributesTool(options) {
	const { apiService, organizationSlug, projectId } = options;
	return agentTool({
		description: "Query available attributes and fields for a specific Sentry dataset to understand what data is available",
		parameters: z.object({ dataset: z.enum([
			"spans",
			"errors",
			"logs"
		]).describe("The dataset to query attributes for") }),
		execute: async ({ dataset }) => {
			const { BASE_COMMON_FIELDS, DATASET_FIELDS, RECOMMENDED_FIELDS, NUMERIC_FIELDS } = await import("./tools/search-events/config.js");
			const { attributes: customAttributes, fieldTypes } = await fetchCustomAttributes(apiService, organizationSlug, dataset, projectId);
			const allFields = {
				...BASE_COMMON_FIELDS,
				...DATASET_FIELDS[dataset],
				...customAttributes
			};
			const recommendedFields = RECOMMENDED_FIELDS[dataset];
			const allFieldTypes = { ...fieldTypes };
			const staticNumericFields = NUMERIC_FIELDS[dataset] || /* @__PURE__ */ new Set();
			for (const field of staticNumericFields) allFieldTypes[field] = "number";
			return `Dataset: ${dataset}

Available Fields (${Object.keys(allFields).length} total):
${Object.entries(allFields).slice(0, 50).map(([key, desc]) => `- ${key}: ${desc}`).join("\n")}
${Object.keys(allFields).length > 50 ? `\n... and ${Object.keys(allFields).length - 50} more fields` : ""}

Recommended Fields for ${dataset}:
${recommendedFields.basic.map((f) => `- ${f}`).join("\n")}

Field Types (CRITICAL for aggregate functions):
${Object.entries(allFieldTypes).slice(0, 30).map(([key, type]) => `- ${key}: ${type}`).join("\n")}
${Object.keys(allFieldTypes).length > 30 ? `\n... and ${Object.keys(allFieldTypes).length - 30} more fields` : ""}

IMPORTANT: Only use numeric aggregate functions (avg, sum, min, max, percentiles) with numeric fields. Use count() or count_unique() for non-numeric fields.

Use this information to construct appropriate queries for the ${dataset} dataset.`;
		}
	});
}

//#endregion
export { createDatasetAttributesTool, fetchCustomAttributes, getNumberValue, getStringValue, isAggregateQuery };
//# sourceMappingURL=utils-CwvZS30K.js.map