Skip to content

3. 主题文件 (Topic Files)

3.1 组织结构

.claude/projects/<project-hash>/memory/
├── MEMORY.md                    # 热记忆 (始终加载)
├── debugging.md                 # 调试经验
├── patterns.md                  # 代码模式
├── api-integration.md           # API 集成
├── performance-optimization.md  # 性能优化
└── testing-strategies.md        # 测试策略

3.2 主题文件示例

markdown
# Debugging Guide

## Common Build Errors

### Error: "Cannot find module 'react'"

**Cause**: Dependencies not installed or node_modules corrupted

**Solution**:
\`\`\`bash
rm -rf node_modules package-lock.json
tnpm install
\`\`\`

**Frequency**: Encountered 3 times (2026-03-01, 2026-03-05, 2026-03-10)

### Error: "Type 'string' is not assignable to type 'number'"

**Cause**: API response type mismatch

**Solution**:
\`\`\`typescript
// Add type guard
const parseId = (id: string | number): number => {
  return typeof id === 'string' ? parseInt(id, 10) : id;
};
\`\`\`

**Related Files**:
- `src/api/users.ts:45`
- `src/types/api.ts:12`

## Performance Issues

### Slow Initial Load

**Investigation** (2026-03-08):
- Profiled with React DevTools
- Found: Large bundle size (2.5MB)
- Root cause: Importing entire lodash library

**Solution**:
\`\`\`typescript
// ❌ Before
import _ from 'lodash';

// ✅ After
import debounce from 'lodash/debounce';
import throttle from 'lodash/throttle';
\`\`\`

**Result**: Bundle size reduced to 800KB, load time improved by 60%

## Testing Patterns

### Mocking API Calls

**Pattern**:
\`\`\`typescript
import { rest } from 'msw';
import { setupServer } from 'msw/node';

const server = setupServer(
  rest.get('/api/users', (req, res, ctx) => {
    return res(ctx.json([{ id: 1, name: 'Test User' }]));
  })
);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
\`\`\`

**Used in**: 15 test files

3.3 自动主题提取

typescript
class TopicExtractor {
  // 从对话中提取主题
  async extractTopics(conversation: Message[]): Promise<Topic[]> {
    const prompt = `
Analyze this conversation and extract distinct topics that should be saved to memory.

Conversation:
${conversation.map(m => `${m.role}: ${m.content}`).join('\n\n')}

For each topic, provide:
1. Topic name (e.g., "debugging", "api-integration")
2. Key information to remember
3. Relevance score (0-1)

Return JSON array of topics.
`;

    const response = await llm.complete(prompt);
    return JSON.parse(response);
  }

  // 决定存储位置
  async decideStorage(topic: Topic): Promise<'MEMORY.md' | 'topic-file'> {
    // 高频访问 → MEMORY.md
    if (topic.accessFrequency > 0.7) {
      return 'MEMORY.md';
    }

    // 详细内容 → 主题文件
    if (topic.content.length > 500) {
      return 'topic-file';
    }

    // 默认 → MEMORY.md
    return 'MEMORY.md';
  }
}

前端面试知识库