Skip to content

peterfei/ai-agent-team / .claude/skills/thread-manager

thread-manager

Flagged

FileKindSize
.claude/.threads/current-context.mdmarkdown648 B
.gitignoreother76 B
.npmignoreother138 B
CHANGELOG.mdmarkdown1.1 KB
README.mdmarkdown4.7 KB
SKILL.mdskill md1.7 KB
USAGE_GUIDE.mdmarkdown6.7 KB
docs/clt-aliases.mdmarkdown2.2 KB
find-module.jsscript166 B
jest.config.jsscript1.1 KB
models/Xenova/all-MiniLM-L6-v2/config.jsonconfig650 B
models/Xenova/all-MiniLM-L6-v2/onnx/model_quantized.onnxother21.9 MB
models/Xenova/all-MiniLM-L6-v2/special_tokens_map.jsonconfig125 B
models/Xenova/all-MiniLM-L6-v2/tokenizer.jsonconfig695.0 KB
models/Xenova/all-MiniLM-L6-v2/tokenizer_config.jsonconfig366 B
package-lock.jsonmanifest406.8 KB
package.jsonmanifest1.1 KB
scripts/download-model.jsscript1.7 KB
scripts/migrate-embeddings.tsscript3.4 KB
src/core/embedding-service.tsscript4.3 KB
src/core/thread-manager.tsscript14.3 KB
src/core/vector-search-engine.tsscript1.2 KB
src/core/vector-utils.tsscript1.0 KB
src/database/db.tsscript4.5 KB
src/database/file-changes-dao.tsscript2.8 KB
src/database/index.tsscript121 B
src/database/messages-dao.tsscript5.1 KB
src/database/threads-dao.tsscript6.4 KB
src/git/git-integration.tsscript5.6 KB
src/index.tsscript257 B
src/server.tsscript6.1 KB
src/tools/add-message.tsscript1.5 KB
src/tools/create-thread.tsscript1.3 KB
src/tools/delete-thread.tsscript925 B
src/tools/get-current-thread.tsscript2.6 KB
src/tools/get-thread.tsscript1.2 KB
src/tools/list-threads.tsscript1.2 KB
src/tools/search-messages.tsscript4.4 KB
src/tools/switch-thread.tsscript1.4 KB
src/tools/track-file-change.tsscript1.7 KB
src/tools/update-thread.tsscript1.2 KB
src/types/index.tsscript2.6 KB
test-add-message.jsscript2.3 KB
test-output-format.jsscript546 B
tests/integration/file-tracker.test.tsscript3.3 KB
tests/integration/mcp-tools.test.tsscript5.4 KB
tests/integration/search-messages.test.tsscript5.8 KB
tests/unit/__mocks__/embedding-service.tsscript1.3 KB
tests/unit/database.test.tsscript1.6 KB
tests/unit/file-changes-dao.test.tsscript3.6 KB
tests/unit/git-integration.test.tsscript4.9 KB
tests/unit/messages-dao.test.tsscript3.9 KB
tests/unit/thread-manager.test.tsscript9.2 KB
tests/unit/threads-dao.test.tsscript6.0 KB
tests/unit/vector-search.test.tsscript3.7 KB
tsconfig.jsonconfig464 B

src/tools/search-messages.ts

View on GitHub
import { z } from 'zod';
import { ThreadManager } from '../core/thread-manager';
import { ToolDefinition, Message } from '../types';
import { formatDistanceToNow } from 'date-fns';
import { zhCN } from 'date-fns/locale';

// Input schema for the search_messages tool
export const SearchMessagesInputSchema = z.object({
  query: z.string().describe('The natural language query to search for messages.'),
  threadId: z.string().optional().describe('Optional: The ID of the thread to search within. If not provided, searches the current active thread.'),
  topK: z.number().int().min(1).default(5).optional().describe('Optional: The maximum number of similar messages to return. Defaults to 5.'),
  minScore: z.number().min(0).max(1).default(0.6).optional().describe('Optional: The minimum similarity score (0-1) for results to be included. Defaults to 0.6.'),
});

// Tool definition for MCP
export const toolDefinition: ToolDefinition = {
  name: 'search_messages',
  description: 'Semantically searches for relevant messages in the conversation history based on a natural language query.',
  inputSchema: {
    type: 'object',
    properties: {
      query: { type: 'string', description: 'The natural language query to search for messages.' },
      threadId: { type: 'string', description: 'Optional: The ID of the thread to search within. If not provided, searches the current active thread.' },
      topK: { type: 'number', description: 'Optional: The maximum number of similar messages to return. Defaults to 5.' },
      minScore: { type: 'number', description: 'Optional: The minimum similarity score (0-1) for results to be included. Defaults to 0.6.' },
    },
    required: ['query'],
  },
};

// Tool handler function
export async function toolHandler(toolInvocation: { toolName: string, input: any }, threadManager: ThreadManager): Promise<{ message: string, info?: any, isError?: boolean }> {
  const parsedInput = SearchMessagesInputSchema.safeParse(toolInvocation.input);

  if (!parsedInput.success) {
    return {
      message: `Invalid input for search_messages: ${parsedInput.error.errors.map(e => e.message).join(', ')}`,
      isError: true,
    };
  }

  const { query, threadId, topK, minScore } = parsedInput.data;

  try {
    // Determine the thread ID to search within
    let targetThreadId = threadId;
    if (!targetThreadId) {
      const activeThread = await threadManager.getCurrentThread();
      if (activeThread.thread) {
        targetThreadId = activeThread.thread.id;
      } else {
        return {
          message: 'No active thread found and no threadId provided. Cannot perform search.',
          isError: true,
        };
      }
    }

    // Perform the search
    const results = await threadManager.messagesDAOInstance.searchSimilar(query, {
      threadId: targetThreadId,
      topK,
      minScore,
    });

    // Format the results for display
    const formatted = results.map(msg => ({
      id: msg.id,
      role: msg.role,
      content: msg.content.substring(0, 200) + (msg.content.length > 200 ? '...' : ''),
      timestamp: formatDistanceToNow(msg.timestamp, { locale: zhCN, addSuffix: true }),
      score: msg.score.toFixed(3),
      threadId: msg.threadId
    }));

    const message = formatSearchResults(query, formatted);

    return {
      message,
      info: JSON.stringify({
        resultCount: results.length,
        avgScore: results.length > 0 ? (results.reduce((sum, r) => sum + r.score, 0) / results.length).toFixed(3) : '0.000'
      }, null, 2) // 格式化为可读的 JSON 字符串
    };
  } catch (error: any) {
    return {
      message: `Error performing search: ${error.message}`,
      isError: true,
    };
  }
}

/**
 * Formats search results into a human-readable Markdown string.
 */
function formatSearchResults(
  query: string,
  results: Array<{ role: string; content: string; score: string; timestamp: string, id: string, threadId: string }>
): string {
  if (results.length === 0) {
    return `🔍 未找到与 "${query}" 相关的消息。`;
  }

  let output = `### 🔍 搜索结果: "${query}"\n\n`;
  output += `找到 ${results.length} 条相关消息:\n\n`;

  results.forEach((result, index) => {
    const icon = result.role === 'user' ? '👤' : '🤖';
    const shortId = result.id.substring(0, 8);
    output += `**${index + 1}. ${icon} ${result.role}** (相似度: ${result.score}) - ID: \`${shortId}\`\n`;
    output += `🕒 ${result.timestamp}\n`;
    output += `> ${result.content}\n\n`;
  });

  return output;
}
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall