Skip to content

5. Skill 注入策略

5.1 注入时机

typescript
class SkillInjector {
  // 在系统提示词中注入
  async injectIntoSystemPrompt(skills: Skill[]): Promise<string> {
    if (skills.length === 0) return '';

    const sections = skills.map(skill => `
<skill name="${skill.name}">
${skill.content}
</skill>
`);

    return `
<available_skills>
The following skills are available for this task:

${sections.join('\n')}

Use these skills as reference for best practices and workflows.
</available_skills>
`;
  }

  // 在用户消息后注入
  async injectAfterUserMessage(
    userMessage: string,
    skills: Skill[]
  ): Promise<Message[]> {
    const messages: Message[] = [
      { role: 'user', content: userMessage }
    ];

    if (skills.length > 0) {
      messages.push({
        role: 'user',
        content: `
<system>
Detected relevant skills: ${skills.map(s => s.name).join(', ')}

${await this.injectIntoSystemPrompt(skills)}
</system>
`
      });
    }

    return messages;
  }

  // 动态注入:在对话中途加载
  async injectDynamic(
    conversation: Message[],
    newSkill: Skill
  ): Promise<Message[]> {
    return [
      ...conversation,
      {
        role: 'user',
        content: `
<system>
Loading additional skill: ${newSkill.name}

${newSkill.content}
</system>
`
      }
    ];
  }
}

5.2 优先级管理

typescript
class SkillPriorityManager {
  // 计算 skill 优先级
  calculatePriority(skill: Skill, context: Context): number {
    let priority = 0;

    // 1. 匹配分数 (50%)
    priority += skill.matchScore * 0.5;

    // 2. 使用频率 (20%)
    const usageFreq = this.getUsageFrequency(skill.name);
    priority += usageFreq * 0.2;

    // 3. 最近使用 (15%)
    const recency = this.getRecency(skill.name);
    priority += recency * 0.15;

    // 4. 用户偏好 (15%)
    const preference = this.getUserPreference(skill.name);
    priority += preference * 0.15;

    return priority;
  }

  // 解决冲突:多个 skills 匹配时
  resolveConflicts(skills: Skill[]): Skill[] {
    // 1. 按优先级排序
    const sorted = skills.sort((a, b) =>
      this.calculatePriority(b, this.context) -
      this.calculatePriority(a, this.context)
    );

    // 2. 检查依赖关系
    const withDeps = this.resolveDependencies(sorted);

    // 3. 限制数量(避免上下文过载)
    return withDeps.slice(0, 3);
  }

  // 解析依赖
  private resolveDependencies(skills: Skill[]): Skill[] {
    const result: Skill[] = [];
    const loaded = new Set<string>();

    for (const skill of skills) {
      // 先加载依赖
      if (skill.dependencies) {
        for (const dep of skill.dependencies) {
          if (!loaded.has(dep)) {
            const depSkill = this.loadSkill(dep);
            result.push(depSkill);
            loaded.add(dep);
          }
        }
      }

      // 再加载自己
      if (!loaded.has(skill.name)) {
        result.push(skill);
        loaded.add(skill.name);
      }
    }

    return result;
  }
}

前端面试知识库