Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions .claude/commands/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Claude Code 数据增强智能助手

这个自定义命令创建了一个智能AI助手,能够在执行任务时自动识别需要数据库支持的部分,先查询相关数据,然后基于数据上下文完成您的任务。**无需任何配置文件,自动发现项目数据库设置!**

## 安装

只需要一个文件:
- **主命令**: `.claude/commands/ask-with-data.md`

该命令会自动检测项目中的数据库配置,支持多种配置文件格式和项目类型。

## 使用方法

在Claude Code中使用斜杠命令:

```
/ask-with-data <您的问题或任务>
```

### 示例用法

```bash
# 基于数据的分析和优化
/ask-with-data 帮我基于当前用户数据优化用户注册流程

# 创建数据驱动的功能
/ask-with-data 创建一个显示最受欢迎API渠道的仪表板

# 问题诊断和修复
/ask-with-data 修复高使用量用户的计费问题

# 数据分析和报告
/ask-with-data 分析为什么错误率上升并提出改进建议

# 业务洞察
/ask-with-data 写一份关于我们顶级客户及其使用模式的报告

# 性能优化
/ask-with-data 帮我了解哪些模型性能不佳

# 代码改进建议
/ask-with-data 基于实际使用数据建议如何优化API性能
```

## 功能特性

### 1. 自动配置发现
命令会自动检测项目中的数据库配置:

- **配置文件**: config.yaml, config.json, .env, application.properties
- **项目类型**: Go, Spring Boot, Node.js等
- **数据库文件**: 自动查找SQLite数据库文件

### 2. 智能任务分析
自动分析您的任务,识别需要数据库支持的部分:

- **用户相关任务**: 涉及用户注册、管理、分析的任务
- **渠道优化**: API渠道性能分析和优化建议
- **使用统计分析**: 流量分析、使用模式识别
- **错误诊断**: 问题分析、故障排查、性能优化
- **业务洞察**: 客户分析、收入优化、趋势预测
- **模型性能**: AI模型效果分析和优化建议

### 3. 数据驱动决策

基于实际数据提供:

- **准确的分析**: 基于真实数据而非假设
- **个性化建议**: 针对您项目的具体情况
- **可行的解决方案**: 结合代码和数据的实用建议
- **量化的洞察**: 具体的数字和趋势分析

### 4. 全栈任务支持
- 数据分析 + 代码实现
- 问题诊断 + 解决方案
- 性能分析 + 优化建议
- 业务洞察 + 功能开发

## 扩展和自定义

### 添加新的数据类型支持

直接编辑 `.claude/commands/ask-with-data.md` 文件中的分析逻辑:

```bash
# 添加新的任务类型检测
if [[ $QUESTION_LOWER =~ (your|task|keywords) ]]; then \
echo "🔍 Detected your custom task type" && QUERIES_TO_RUN="$QUERIES_TO_RUN your_query"; \
fi

# 添加对应的数据查询
"your_query") \
if [ -f "$DB_CONNECTION" ]; then \
sqlite3 "$DB_CONNECTION" "YOUR SQL QUERY HERE;" 2>/dev/null || echo "❌ Query failed"; \
else echo "❌ Database not found"; fi ;;
```

## 故障排除

### 1. 数据库未找到
- 确保SQLite数据库文件存在
- 检查数据库文件路径
- 确认项目结构

### 2. 任务执行失败
- 验证数据库表结构
- 检查SQL语法
- 确认数据库权限
- 检查任务描述是否清晰

## 工作流程

```
用户任务 → Claude Code → /ask-with-data 命令 →
分析任务需求 → 自动发现数据库 → 查询相关数据 →
基于数据上下文执行任务 → 提供完整解决方案
```

该命令设计为智能助手,能够将数据洞察与代码实现完美结合,适用于任何需要数据支持的开发任务。
108 changes: 108 additions & 0 deletions .claude/commands/ask-with-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
description: Intelligent assistant that queries database when needed and completes tasks with data context
argument-hint: "<your question or task>"
allowed-tools: [Bash, Read, Write, Grep, Glob, Edit, MultiEdit]
---

# Data-Enhanced AI Assistant

This intelligent assistant will:
1. Analyze your question or task to identify parts that need database support
2. Auto-discover your project's database configuration
3. Query relevant database data when needed
4. Complete your task using both the database context and codebase knowledge

## Usage
```
/ask-with-data <your question or task>
```

## Examples
```
/ask-with-data Help me optimize the user registration process based on current user data
/ask-with-data Create a dashboard showing our most popular API channels
/ask-with-data Fix the billing issues for users with high usage
/ask-with-data Analyze why error rates increased and suggest improvements
/ask-with-data Write a report on our top customers and their usage patterns
/ask-with-data Help me understand which models are underperforming
```

---

I'll analyze your task and gather any necessary database context to provide you with the best assistance: **$ARGUMENTS**

Let me first check if your task requires database information, and if so, I'll gather that data before proceeding.

!echo "🔍 Analyzing question: $ARGUMENTS" && echo "=================================="

!echo "🔍 Auto-discovering database configuration..." && \
DB_TYPE="" && DB_CONNECTION="" && \
if [ -f "config.yaml" ] || [ -f "config.yml" ]; then echo "📁 Found YAML config file"; fi && \
if [ -f "config.json" ]; then echo "📁 Found JSON config file"; fi && \
if [ -f ".env" ]; then echo "📁 Found .env file"; source .env 2>/dev/null; fi && \
if [ -f "application.properties" ]; then echo "📁 Found Spring properties file"; fi && \
if [ -f "go.mod" ]; then echo "📁 Found Go project"; fi && \
if [ -f "data/new-api.db" ]; then DB_TYPE="sqlite"; DB_CONNECTION="./data/new-api.db"; \
elif [ -f "database.db" ]; then DB_TYPE="sqlite"; DB_CONNECTION="./database.db"; \
elif [ -f "app.db" ]; then DB_TYPE="sqlite"; DB_CONNECTION="./app.db"; \
else DB_TYPE="sqlite"; DB_CONNECTION="./data/new-api.db"; fi && \
echo "✅ Database type: $DB_TYPE" && echo "✅ Connection: $DB_CONNECTION"
Comment on lines +38 to +49

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve database discovery robustness.

The database discovery logic has some limitations:

  • Hardcoded SQLite paths may not work in all environments
  • Always defaults to ./data/new-api.db even if the file doesn't exist
  • Limited to SQLite despite checking for other config file types

Consider enhancing the discovery logic:

-if [ -f "data/new-api.db" ]; then DB_TYPE="sqlite"; DB_CONNECTION="./data/new-api.db"; \
-elif [ -f "database.db" ]; then DB_TYPE="sqlite"; DB_CONNECTION="./database.db"; \
-elif [ -f "app.db" ]; then DB_TYPE="sqlite"; DB_CONNECTION="./app.db"; \
-else DB_TYPE="sqlite"; DB_CONNECTION="./data/new-api.db"; fi && \
+for db_path in "./data/new-api.db" "./database.db" "./app.db"; do \
+  if [ -f "$db_path" ]; then DB_TYPE="sqlite"; DB_CONNECTION="$db_path"; break; fi; \
+done && \
+if [ -z "$DB_CONNECTION" ]; then echo "❌ No SQLite database found"; exit 1; fi && \
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
!echo "🔍 Auto-discovering database configuration..." && \
DB_TYPE="" && DB_CONNECTION="" && \
if [ -f "config.yaml" ] || [ -f "config.yml" ]; then echo "📁 Found YAML config file"; fi && \
if [ -f "config.json" ]; then echo "📁 Found JSON config file"; fi && \
if [ -f ".env" ]; then echo "📁 Found .env file"; source .env 2>/dev/null; fi && \
if [ -f "application.properties" ]; then echo "📁 Found Spring properties file"; fi && \
if [ -f "go.mod" ]; then echo "📁 Found Go project"; fi && \
if [ -f "data/new-api.db" ]; then DB_TYPE="sqlite"; DB_CONNECTION="./data/new-api.db"; \
elif [ -f "database.db" ]; then DB_TYPE="sqlite"; DB_CONNECTION="./database.db"; \
elif [ -f "app.db" ]; then DB_TYPE="sqlite"; DB_CONNECTION="./app.db"; \
else DB_TYPE="sqlite"; DB_CONNECTION="./data/new-api.db"; fi && \
echo "✅ Database type: $DB_TYPE" && echo "✅ Connection: $DB_CONNECTION"
!echo "🔍 Auto-discovering database configuration..." && \
DB_TYPE="" && DB_CONNECTION="" && \
if [ -f "config.yaml" ] || [ -f "config.yml" ]; then echo "📁 Found YAML config file"; fi && \
if [ -f "config.json" ]; then echo "📁 Found JSON config file"; fi && \
if [ -f ".env" ]; then echo "📁 Found .env file"; source .env 2>/dev/null; fi && \
if [ -f "application.properties" ]; then echo "📁 Found Spring properties file"; fi && \
if [ -f "go.mod" ]; then echo "📁 Found Go project"; fi && \
for db_path in "./data/new-api.db" "./database.db" "./app.db"; do \
if [ -f "$db_path" ]; then DB_TYPE="sqlite"; DB_CONNECTION="$db_path"; break; fi; \
done && \
if [ -z "$DB_CONNECTION" ]; then echo "❌ No SQLite database found"; exit 1; fi && \
echo "✅ Database type: $DB_TYPE" && echo "✅ Connection: $DB_CONNECTION"
🤖 Prompt for AI Agents
In .claude/commands/ask-with-data.md around lines 38 to 49, the database
discovery script currently hardcodes SQLite file paths and defaults to a path
even if the file does not exist, limiting robustness and flexibility. Update the
logic to dynamically detect existing database files rather than assuming
defaults, verify file existence before setting DB_CONNECTION, and consider
extending support beyond SQLite by parsing detected config files (YAML, JSON,
.env, etc.) to identify other database types and connection details. This will
make the discovery process more reliable and adaptable to different
environments.


!echo "" && echo "🤖 Analyzing question for relevant data..." && \
QUESTION_LOWER=$(echo "$ARGUMENTS" | tr '[:upper:]' '[:lower:]') && \
QUERIES_TO_RUN="" && \
if [[ $QUESTION_LOWER =~ (user|account|register|signup|用户|注册|账户) ]]; then \
echo "📈 Detected user-related question" && QUERIES_TO_RUN="$QUERIES_TO_RUN users"; \
fi && \
if [[ $QUESTION_LOWER =~ (channel|provider|api|integration|渠道|接口|集成) ]]; then \
echo "🔌 Detected channel-related question" && QUERIES_TO_RUN="$QUERIES_TO_RUN channels"; \
fi && \
if [[ $QUESTION_LOWER =~ (request|usage|traffic|call|请求|使用|流量|调用) ]]; then \
echo "📊 Detected request/usage question" && QUERIES_TO_RUN="$QUERIES_TO_RUN requests"; \
fi && \
if [[ $QUESTION_LOWER =~ (error|fail|problem|issue|错误|失败|问题) ]]; then \
echo "🚨 Detected error-related question" && QUERIES_TO_RUN="$QUERIES_TO_RUN errors"; \
fi && \
if [[ $QUESTION_LOWER =~ (top|best|most|spending|customer|最多|最佳|消费|客户) ]]; then \
echo "🏆 Detected top users question" && QUERIES_TO_RUN="$QUERIES_TO_RUN top_users"; \
fi && \
if [[ $QUESTION_LOWER =~ (model|gpt|claude|gemini|token|模型|令牌) ]]; then \
echo "🤖 Detected model-related question" && QUERIES_TO_RUN="$QUERIES_TO_RUN models"; \
fi && \
if [ -z "$QUERIES_TO_RUN" ]; then \
echo "🔍 No specific keywords detected, running general overview..." && QUERIES_TO_RUN="users channels requests"; \
fi

!echo "" && echo "📋 Running relevant database queries..." && echo "==================================" && \
DB_CONNECTION="./data/new-api.db" && \
for query_type in $QUERIES_TO_RUN; do \
echo "" && echo "📊 Query: $query_type" && echo "----------------------------------------" && \
case "$query_type" in \
"users") \
if [ -f "$DB_CONNECTION" ]; then \
sqlite3 "$DB_CONNECTION" "SELECT COUNT(*) as total_users, COUNT(CASE WHEN created_at > datetime('now', '-30 days') THEN 1 END) as recent_users FROM users;" 2>/dev/null || echo "❌ Query failed"; \
else echo "❌ Database not found"; fi ;; \
"channels") \
if [ -f "$DB_CONNECTION" ]; then \
sqlite3 "$DB_CONNECTION" "SELECT type, COUNT(*) as count, AVG(priority) as avg_priority FROM channels GROUP BY type ORDER BY count DESC;" 2>/dev/null || echo "❌ Query failed"; \
else echo "❌ Database not found"; fi ;; \
"requests") \
if [ -f "$DB_CONNECTION" ]; then \
sqlite3 "$DB_CONNECTION" "SELECT DATE(created_at) as date, COUNT(*) as requests, SUM(quota) as total_quota FROM logs WHERE created_at > datetime('now', '-7 days') GROUP BY DATE(created_at) ORDER BY date;" 2>/dev/null || echo "❌ Query failed"; \
else echo "❌ Database not found"; fi ;; \
"errors") \
if [ -f "$DB_CONNECTION" ]; then \
sqlite3 "$DB_CONNECTION" "SELECT type, COUNT(*) as error_count FROM logs WHERE created_at > datetime('now', '-24 hours') AND (response_time = 0 OR content LIKE '%error%') GROUP BY type ORDER BY error_count DESC;" 2>/dev/null || echo "❌ Query failed"; \
else echo "❌ Database not found"; fi ;; \
"top_users") \
if [ -f "$DB_CONNECTION" ]; then \
sqlite3 "$DB_CONNECTION" "SELECT user_id, SUM(quota) as total_usage, COUNT(*) as request_count FROM logs WHERE created_at > datetime('now', '-30 days') GROUP BY user_id ORDER BY total_usage DESC LIMIT 10;" 2>/dev/null || echo "❌ Query failed"; \
else echo "❌ Database not found"; fi ;; \
"models") \
if [ -f "$DB_CONNECTION" ]; then \
sqlite3 "$DB_CONNECTION" "SELECT model, COUNT(*) as usage_count, AVG(prompt_tokens) as avg_prompt_tokens, AVG(completion_tokens) as avg_completion_tokens FROM logs WHERE created_at > datetime('now', '-7 days') GROUP BY model ORDER BY usage_count DESC;" 2>/dev/null || echo "❌ Query failed"; \
else echo "❌ Database not found"; fi ;; \
esac; \
done

!echo "" && echo "✅ Database analysis complete!" && echo "🎯 Now analyzing your question with this database context..."
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ web/dist
one-api
.DS_Store
tiktoken_cache
.eslintcache
test
.eslintcache
new-api
Loading