()
| 66 | } |
| 67 | |
| 68 | function createSkillImprovementHook() { |
| 69 | let lastAnalyzedCount = 0 |
| 70 | let lastAnalyzedIndex = 0 |
| 71 | |
| 72 | const config: ApiQueryHookConfig<SkillUpdate[]> = { |
| 73 | name: 'skill_improvement', |
| 74 | |
| 75 | async shouldRun(context) { |
| 76 | if (context.querySource !== 'repl_main_thread') { |
| 77 | return false |
| 78 | } |
| 79 | |
| 80 | if (!findProjectSkill()) { |
| 81 | return false |
| 82 | } |
| 83 | |
| 84 | // Only run every TURN_BATCH_SIZE user messages |
| 85 | const userCount = count(context.messages, m => m.type === 'user') |
| 86 | if (userCount - lastAnalyzedCount < TURN_BATCH_SIZE) { |
| 87 | return false |
| 88 | } |
| 89 | |
| 90 | lastAnalyzedCount = userCount |
| 91 | return true |
| 92 | }, |
| 93 | |
| 94 | buildMessages(context) { |
| 95 | const projectSkill = findProjectSkill()! |
| 96 | // Only analyze messages since the last check — the skill definition |
| 97 | // provides enough context for the classifier to understand corrections |
| 98 | const newMessages = context.messages.slice(lastAnalyzedIndex) |
| 99 | lastAnalyzedIndex = context.messages.length |
| 100 | |
| 101 | return [ |
| 102 | createUserMessage({ |
| 103 | content: `You are analyzing a conversation where a user is executing a skill (a repeatable process). |
| 104 | Your job: identify if the user's recent messages contain preferences, requests, or corrections that should be permanently added to the skill definition for future runs. |
| 105 | |
| 106 | <skill_definition> |
| 107 | ${projectSkill.content} |
| 108 | </skill_definition> |
| 109 | |
| 110 | <recent_messages> |
| 111 | ${formatRecentMessages(newMessages)} |
| 112 | </recent_messages> |
| 113 | |
| 114 | Look for: |
| 115 | - Requests to add, change, or remove steps: "can you also ask me X", "please do Y too", "don't do Z" |
| 116 | - Preferences about how steps should work: "ask me about energy levels", "note the time", "use a casual tone" |
| 117 | - Corrections: "no, do X instead", "always use Y", "make sure to..." |
| 118 | |
| 119 | Ignore: |
| 120 | - Routine conversation that doesn't generalize (one-time answers, chitchat) |
| 121 | - Things the skill already does |
| 122 | |
| 123 | Output a JSON array inside <updates> tags. Each item: {"section": "which step/section to modify or 'new step'", "change": "what to add/modify", "reason": "which user message prompted this"}. |
| 124 | Output <updates>[]</updates> if no updates are needed.`, |
| 125 | }), |
no test coverage detected