import {
  getConfig,
  requireApiKey
} from "./chunk-KOO5733S.js";

// src/langwatch-api.ts
var LangWatchApiError = class extends Error {
  constructor(message, status, responseBody) {
    super(message);
    this.status = status;
    this.responseBody = responseBody;
    this.name = "LangWatchApiError";
  }
  status;
  responseBody;
};
async function makeRequest(method, path, body) {
  const config = getConfig();
  const url = config.endpoint + path;
  const headers = {
    "X-Auth-Token": requireApiKey()
  };
  if (config.projectId) {
    headers["X-Project-Id"] = config.projectId;
  }
  if (body !== void 0) {
    headers["Content-Type"] = "application/json";
  }
  const response = await fetch(url, {
    method,
    headers,
    ...body !== void 0 ? { body: JSON.stringify(body) } : {}
  });
  if (!response.ok) {
    const responseBody = await response.text();
    throw new LangWatchApiError(
      `LangWatch API error ${response.status}: ${responseBody}`,
      response.status,
      responseBody
    );
  }
  if (response.status === 204 || response.headers?.get("content-length") === "0") {
    return null;
  }
  return response.json();
}
async function searchTraces(params) {
  const { format = "digest", ...rest } = params;
  return makeRequest("POST", "/api/traces/search", {
    ...rest,
    format
  });
}
async function getTraceById(traceId, format = "digest") {
  return makeRequest(
    "GET",
    `/api/traces/${encodeURIComponent(traceId)}?format=${format}`
  );
}
async function getAnalyticsTimeseries(params) {
  return makeRequest(
    "POST",
    "/api/analytics/timeseries",
    params
  );
}
async function listPrompts() {
  return makeRequest("GET", "/api/prompts");
}
async function getPrompt(idOrHandle, options) {
  const params = new URLSearchParams();
  if (options?.version != null) params.set("version", String(options.version));
  if (options?.tag) params.set("tag", options.tag);
  const query = params.toString() ? `?${params}` : "";
  return makeRequest(
    "GET",
    `/api/prompts/${encodeURIComponent(idOrHandle)}${query}`
  );
}
async function createPrompt(data) {
  return makeRequest(
    "POST",
    "/api/prompts",
    data
  );
}
async function updatePrompt(idOrHandle, data) {
  return makeRequest(
    "PUT",
    `/api/prompts/${encodeURIComponent(idOrHandle)}`,
    data
  );
}
async function assignPromptTag({
  idOrHandle,
  tag,
  versionId
}) {
  return makeRequest(
    "PUT",
    `/api/prompts/${encodeURIComponent(idOrHandle)}/tags/${encodeURIComponent(tag)}`,
    { versionId }
  );
}
async function listPromptTags() {
  return makeRequest("GET", "/api/prompts/tags");
}
async function createPromptTag(name) {
  return makeRequest("POST", "/api/prompts/tags", { name });
}
async function renamePromptTag({
  tag,
  name
}) {
  return makeRequest(
    "PUT",
    `/api/prompts/tags/${encodeURIComponent(tag)}`,
    { name }
  );
}
async function deletePromptTag(tag) {
  return makeRequest(
    "DELETE",
    `/api/prompts/tags/${encodeURIComponent(tag)}`
  );
}

export {
  LangWatchApiError,
  makeRequest,
  searchTraces,
  getTraceById,
  getAnalyticsTimeseries,
  listPrompts,
  getPrompt,
  createPrompt,
  updatePrompt,
  assignPromptTag,
  listPromptTags,
  createPromptTag,
  renamePromptTag,
  deletePromptTag
};
//# sourceMappingURL=chunk-CAY57CKF.js.map