186 lines
5.2 KiB
TypeScript
186 lines
5.2 KiB
TypeScript
/**
|
|
* Hook: load-context
|
|
*
|
|
* Purpose: Ensures Nova loads the appropriate context before responding
|
|
* to user requests, implementing the progressive disclosure pattern.
|
|
*
|
|
* Trigger: On user prompt submit
|
|
*
|
|
* This hook injects instructions to load context files based on the
|
|
* task type and requirements.
|
|
*/
|
|
|
|
/**
|
|
* IMPLEMENTATION NOTE:
|
|
*
|
|
* Claude Code hooks may differ from the Anthropic SDK shown below.
|
|
* This is a conceptual implementation that demonstrates the intended behavior.
|
|
*
|
|
* For Claude Code, hooks might be configured differently. This file serves as:
|
|
* 1. Documentation of the intended behavior
|
|
* 2. Reference for manual context loading
|
|
* 3. Template for future hook implementation
|
|
*
|
|
* MANUAL ALTERNATIVE:
|
|
* If hooks aren't supported, Nova should manually check at conversation start:
|
|
* - Have I read .claude/context/claude.md in THIS conversation?
|
|
* - If not, read it immediately
|
|
* - Load relevant subsystems based on the task
|
|
*/
|
|
|
|
// Conceptual Hook Implementation
|
|
export default {
|
|
name: 'load-context',
|
|
version: '1.0',
|
|
|
|
/**
|
|
* This function would be called before Nova sees the user's message
|
|
*/
|
|
async onUserPromptSubmit(context: any) {
|
|
// Check if this is the first message in the conversation
|
|
const isFirstMessage = context.conversationHistory.length === 0;
|
|
|
|
// Construct context loading reminder
|
|
const contextReminder = `
|
|
🔄 CONTEXT LOADING PROTOCOL
|
|
|
|
Before responding to this message, follow these steps:
|
|
|
|
1. ✅ Have you read ~/.claude/context/claude.md in THIS conversation?
|
|
- If NO: Read it now with the Read tool
|
|
- If YES: Proceed to step 2
|
|
|
|
2. ✅ Analyze the user's request to identify:
|
|
- Task type (research, report, analysis, etc.)
|
|
- Required skills (web-research, research-report, etc.)
|
|
- Relevant context subsystems (projects, tools, memory)
|
|
|
|
3. ✅ Load ONLY the relevant context files (progressive disclosure):
|
|
- For research tasks: Read context/projects/research/claude.md
|
|
- For web searches: Read skills/web-research/SKILL.md
|
|
- For report generation: Read skills/research-report/SKILL.md
|
|
- If using tools: Read context/tools/claude.md
|
|
- For repeat patterns: Read context/memory/claude.md
|
|
|
|
4. ✅ Proceed with the task using the loaded context
|
|
|
|
DO NOT:
|
|
- Skip reading context files
|
|
- Load all context at once
|
|
- Claim you've read files without actually using Read tool
|
|
- Ignore the progressive disclosure principle
|
|
|
|
---
|
|
User's message:
|
|
`;
|
|
|
|
// For first message, add stronger reminder
|
|
if (isFirstMessage) {
|
|
return {
|
|
...context,
|
|
userMessage: contextReminder + context.userMessage,
|
|
metadata: {
|
|
...context.metadata,
|
|
contextLoadingRequired: true,
|
|
firstMessage: true
|
|
}
|
|
};
|
|
}
|
|
|
|
// For subsequent messages, lighter reminder
|
|
const lightReminder = `
|
|
[System: Remember to load relevant context files if tackling a new type of task]
|
|
|
|
`;
|
|
|
|
return {
|
|
...context,
|
|
userMessage: lightReminder + context.userMessage
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Optional: After Nova responds, check if context was properly loaded
|
|
*/
|
|
async onAssistantResponse(context: any) {
|
|
// This could verify that appropriate Read calls were made
|
|
// and warn if context loading was skipped
|
|
|
|
const readCalls = context.toolCalls?.filter((call: any) =>
|
|
call.tool === 'Read' && call.parameters.file_path.includes('.claude/context')
|
|
) || [];
|
|
|
|
if (context.metadata?.contextLoadingRequired && readCalls.length === 0) {
|
|
console.warn('Warning: Context loading was required but no context files were read');
|
|
}
|
|
|
|
return context;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* CONFIGURATION INSTRUCTIONS:
|
|
*
|
|
* To enable this hook in Claude Code:
|
|
*
|
|
* 1. Check Claude Code documentation for hook configuration
|
|
* 2. Register this hook in the appropriate config file
|
|
* 3. Restart Claude Code or reload configuration
|
|
*
|
|
* If hooks are not supported:
|
|
* - Add instructions to output_style.md
|
|
* - Rely on context/claude.md instructions
|
|
* - Manually prompt for context loading at conversation start
|
|
*/
|
|
|
|
/**
|
|
* TESTING:
|
|
*
|
|
* To test if the hook is working:
|
|
*
|
|
* Test 1: Start new conversation
|
|
* User: "Hello"
|
|
* Expected: Nova should mention reading context/claude.md
|
|
*
|
|
* Test 2: Research task
|
|
* User: "Research quantum computing"
|
|
* Expected: Nova should read context/claude.md, projects/research/, and web-research skill
|
|
*
|
|
* Test 3: Continuation
|
|
* User: "Now make a report"
|
|
* Expected: Nova should read research-report skill (new task type)
|
|
*/
|
|
|
|
/**
|
|
* FALLBACK STRATEGY:
|
|
*
|
|
* If hooks don't work, add this to output_style.md:
|
|
*
|
|
* ```
|
|
* MANDATORY BEHAVIOR:
|
|
* At the start of EVERY conversation, you MUST:
|
|
* 1. Read ~/.claude/context/claude.md
|
|
* 2. Understand available subsystems
|
|
* 3. Load relevant context based on user's request
|
|
*
|
|
* For EVERY new task type, you MUST:
|
|
* 1. Check if you need additional context
|
|
* 2. Read relevant skill or context files
|
|
* 3. Follow the progressive disclosure pattern
|
|
* ```
|
|
*/
|
|
|
|
// Type definitions for reference
|
|
interface ConversationContext {
|
|
userMessage: string;
|
|
conversationHistory: Array<any>;
|
|
metadata?: {
|
|
contextLoadingRequired?: boolean;
|
|
firstMessage?: boolean;
|
|
};
|
|
toolCalls?: Array<{
|
|
tool: string;
|
|
parameters: any;
|
|
}>;
|
|
}
|