#!/usr/bin/env tsx
import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import { ChromaClient } from "chromadb";
import dotenv from "dotenv";
import chalk from "chalk";
dotenv.config();
async function queryChromaDB() {
  const apiKey = process.env.CHROMADB_API_KEY;
  const tenant = process.env.CHROMADB_TENANT;
  const database = process.env.CHROMADB_DATABASE || "stackmemory";
  console.log(chalk.cyan("\u{1F50D} Querying ChromaDB Contexts\n"));
  const client = new ChromaClient({
    ssl: true,
    host: "api.trychroma.com",
    port: 443,
    headers: {
      "X-Chroma-Token": apiKey
    },
    tenant,
    database
  });
  const collections = await client.listCollections();
  console.log(chalk.yellow("\u{1F4DA} Available Collections:"));
  collections.forEach((col) => {
    console.log(`   - ${col.name}`);
  });
  console.log();
  let collection;
  try {
    collection = await client.getCollection({
      name: "claude_context"
    });
    console.log(chalk.green("\u2713 Using collection: claude_context\n"));
  } catch {
    collection = await client.getCollection({
      name: "stackmemory_contexts"
    });
    console.log(chalk.green("\u2713 Using collection: stackmemory_contexts\n"));
  }
  console.log(chalk.yellow("\u{1F4CA} Collection Stats:"));
  const count = await collection.count();
  console.log(`   Total documents: ${count}
`);
  console.log(chalk.cyan("\u{1F4DA} All Stored Contexts:\n"));
  const allDocs = await collection.get({
    limit: 100
  });
  if (allDocs.ids.length > 0) {
    for (let i = 0; i < allDocs.ids.length; i++) {
      const metadata = allDocs.metadatas[i];
      const document = allDocs.documents[i];
      console.log(chalk.green(`${i + 1}. ${allDocs.ids[i]}`));
      console.log(`   Type: ${metadata.type}`);
      console.log(`   Timestamp: ${metadata.timestamp}`);
      console.log(`   User: ${metadata.user_id}`);
      console.log(`   Project: ${metadata.project}`);
      console.log(`   Content: ${document?.substring(0, 100)}...`);
      console.log();
    }
  }
  console.log(chalk.cyan('\n\u{1F50E} Search Results for "TypeScript lint error":\n'));
  const searchResults = await collection.query({
    queryTexts: ["TypeScript lint error fix"],
    nResults: 5
  });
  if (searchResults.ids[0].length > 0) {
    for (let i = 0; i < searchResults.ids[0].length; i++) {
      const metadata = searchResults.metadatas[0][i];
      const document = searchResults.documents[0][i];
      const distance = searchResults.distances[0][i];
      console.log(chalk.yellow(`Match ${i + 1} (distance: ${distance.toFixed(3)}):`));
      console.log(`   ID: ${searchResults.ids[0][i]}`);
      console.log(`   Type: ${metadata.type}`);
      console.log(`   Timestamp: ${metadata.timestamp}`);
      console.log(`   Content: ${document?.substring(0, 150)}...`);
      console.log();
    }
  } else {
    console.log("No results found");
  }
  console.log(chalk.cyan("\n\u{1F4CB} Recent Task Completions:\n"));
  const taskCompletions = await collection.get({
    where: {
      type: "task_complete"
    },
    limit: 10
  });
  if (taskCompletions.ids.length > 0) {
    for (let i = 0; i < taskCompletions.ids.length; i++) {
      const metadata = taskCompletions.metadatas[i];
      const document = taskCompletions.documents[i];
      console.log(chalk.green(`Task ${i + 1}:`));
      console.log(`   ID: ${taskCompletions.ids[i]}`);
      console.log(`   Timestamp: ${metadata.timestamp}`);
      console.log(`   Task ID: ${metadata.task_id}`);
      console.log(`   Duration: ${metadata.duration}ms`);
      console.log(`   Files Changed: ${metadata.files_changed}`);
      console.log(`   Content: ${document?.substring(0, 100)}...`);
      console.log();
    }
  } else {
    console.log("No task completions found");
  }
  console.log(chalk.cyan("\n\u23F0 Periodic Checkpoints:\n"));
  const periodicSaves = await collection.get({
    where: {
      type: "periodic_save"
    },
    limit: 10
  });
  if (periodicSaves.ids.length > 0) {
    for (let i = 0; i < periodicSaves.ids.length; i++) {
      const metadata = periodicSaves.metadatas[i];
      const document = periodicSaves.documents[i];
      console.log(chalk.blue(`Checkpoint ${i + 1}:`));
      console.log(`   Timestamp: ${metadata.timestamp}`);
      console.log(`   Interval: ${metadata.interval}`);
      console.log(`   Active Files: ${metadata.active_files}`);
      console.log(`   Git Status: ${document?.substring(0, 150)}...`);
      console.log();
    }
  } else {
    console.log("No periodic saves found");
  }
  console.log(chalk.cyan("\n\u{1F4A1} Decisions Made:\n"));
  const decisions = await collection.get({
    where: {
      type: "decision_made"
    },
    limit: 10
  });
  if (decisions.ids.length > 0) {
    for (let i = 0; i < decisions.ids.length; i++) {
      const metadata = decisions.metadatas[i];
      const document = decisions.documents[i];
      console.log(chalk.magenta(`Decision ${i + 1}:`));
      console.log(`   Category: ${metadata.category}`);
      console.log(`   Alternatives: ${metadata.alternatives}`);
      console.log(`   Reasoning: ${metadata.reasoning}`);
      console.log(`   Decision: ${document?.substring(0, 200)}...`);
      console.log();
    }
  } else {
    console.log("No decisions found");
  }
  console.log(chalk.cyan("\n\u{1F4C8} Context Types Summary:\n"));
  const types = /* @__PURE__ */ new Map();
  for (let i = 0; i < allDocs.ids.length; i++) {
    const type = allDocs.metadatas[i].type || "unknown";
    types.set(type, (types.get(type) || 0) + 1);
  }
  for (const [type, count2] of types.entries()) {
    console.log(`   ${type}: ${count2} documents`);
  }
}
queryChromaDB().catch(console.error);
//# sourceMappingURL=query-chromadb.js.map
