Skip to content

Implement AI Agent Hooks interface for VSCode extension integration#40

Merged
bomanaps merged 2 commits intoPatrick-Ehimen:mainfrom
bomanaps:add-AIAgentHooks
Nov 20, 2025
Merged

Implement AI Agent Hooks interface for VSCode extension integration#40
bomanaps merged 2 commits intoPatrick-Ehimen:mainfrom
bomanaps:add-AIAgentHooks

Conversation

@bomanaps
Copy link
Copy Markdown
Collaborator

@bomanaps bomanaps commented Nov 20, 2025

Pull Request

Description

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Other (describe):

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

Screenshots (if applicable)

Summary by Sourcery

Introduce AI Agent Hooks interface into the VSCode extension to enable programmatic AI interactions, exposing methods for executing commands, retrieving context, registering custom functions, and monitoring progress.

New Features:

  • Add AIAgentHooks interface and AIAgentHooksImpl implementation with methods onAICommand, getWorkspaceContext, registerAIFunction, and onProgress
  • Integrate AI hooks into LighthouseVSCodeExtension with initialization, disposal, and getAIAgentHooks() exposure
  • Export AI command-related types (AICommand, AICommandResult, etc.) from extension-core

Enhancements:

  • Add regular polling of ExtensionCore's progress streamer to notify onProgress subscribers

Documentation:

  • Include VERIFY_AI_HOOKS.md guide for verifying AI Agent Hooks integration
  • Add manual verification script test-ai-hooks.js

Tests:

  • Add unit tests in extension.test.ts for AI Agent Hooks exposure and workspace context retrieval

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Nov 20, 2025

Reviewer's Guide

This PR adds a new AI agent hooks interface and implementation to the VSCode extension, integrates it into the extension lifecycle, updates core exports, and provides tests and documentation to verify the new functionality.

Sequence diagram for AI agent executing a command via hooks

sequenceDiagram
    participant AI_Agent
    participant VSCodeExtension as "LighthouseVSCodeExtension"
    participant AIHooks as "AIAgentHooksImpl"
    participant ExtensionCore
    AI_Agent->>VSCodeExtension: getAIAgentHooks()
    VSCodeExtension->>AIHooks: return aiHooks
    AI_Agent->>AIHooks: onAICommand(command, params)
    AIHooks->>ExtensionCore: getAICommandHandler()
    AIHooks->>ExtensionCore: getAIContext()
    AIHooks->>ExtensionCore: handleCommand(aiCommand)
    ExtensionCore-->>AIHooks: AICommandResult
    AIHooks-->>AI_Agent: result.data
Loading

Sequence diagram for registering and executing a custom AI function

sequenceDiagram
    participant AI_Agent
    participant AIHooks as "AIAgentHooksImpl"
    participant ExtensionCore
    AI_Agent->>AIHooks: registerAIFunction(name, handler)
    AIHooks->>ExtensionCore: getAICommandHandler()
    AIHooks->>ExtensionCore: registerHandler(name, handler)
    AI_Agent->>AIHooks: onAICommand(name, params)
    AIHooks->>AIHooks: customHandlers.get(name)
    AIHooks->>AIHooks: handler(aiCommand)
    AIHooks-->>AI_Agent: result.data
Loading

Sequence diagram for subscribing to progress updates via AI Agent Hooks

sequenceDiagram
    participant AI_Agent
    participant AIHooks as "AIAgentHooksImpl"
    participant ExtensionCore
    AI_Agent->>AIHooks: onProgress(callback)
    AIHooks->>AIHooks: add callback to progressCallbacks
    AIHooks->>ExtensionCore: getProgressStreamer()
    ExtensionCore-->>AIHooks: activeStreams
    AIHooks->>AI_Agent: callback(progress)
Loading

Class diagram for new AI Agent Hooks interface and implementation

classDiagram
    class AIAgentHooks {
        <<interface>>
        +onAICommand(command: string, params: Record<string, unknown>): Promise<unknown>
        +getWorkspaceContext(): Promise<WorkspaceContext>
        +registerAIFunction(name: string, handler: AICommandHandlerFunction): void
        +onProgress(callback: (progress: ProgressUpdate) => void): () => void
    }
    class AIAgentHooksImpl {
        +constructor(extensionCore: ExtensionCore)
        +onAICommand(command: string, params: Record<string, unknown>): Promise<unknown>
        +getWorkspaceContext(): Promise<WorkspaceContext>
        +registerAIFunction(name: string, handler: AICommandHandlerFunction): void
        +onProgress(callback: (progress: ProgressUpdate) => void): () => void
        +dispose(): void
        -progressCallbacks: Set<(progress: ProgressUpdate) => void>
        -customHandlers: Map<string, AICommandHandlerFunction>
        -progressCheckInterval: NodeJS.Timeout | null
        -setupProgressListener(): void
        -getAIContext(): Promise<AIContext>
    }
    AIAgentHooksImpl --|> AIAgentHooks
    AIAgentHooksImpl o-- ExtensionCore
    ExtensionCore <.. WorkspaceContext
    ExtensionCore <.. ProgressUpdate
    ExtensionCore <.. AICommand
    ExtensionCore <.. AICommandResult
    ExtensionCore <.. AICommandHandlerFunction
    ExtensionCore <.. AIContext
Loading

Class diagram for LighthouseVSCodeExtension integration of AI Agent Hooks

classDiagram
    class LighthouseVSCodeExtension {
        -aiHooks: AIAgentHooks
        +getAIAgentHooks(): AIAgentHooks
        +dispose(): Promise<void>
        +constructor(context: vscode.ExtensionContext)
    }
    LighthouseVSCodeExtension o-- AIAgentHooks
    LighthouseVSCodeExtension o-- AIAgentHooksImpl
Loading

File-Level Changes

Change Details Files
Introduce AI Agent Hooks interface and implementation
  • Define AIAgentHooks interface with methods for commands, context, registration, and progress
  • Implement AIAgentHooksImpl to route calls through ExtensionCore, manage custom handlers, poll progress, and handle disposal
  • Generate AIContext for commands with session and preferences data
packages/vscode-extension/src/ai/ai-agent-hooks.ts
Integrate AI hooks into VSCode extension lifecycle
  • Add aiHooks property and initialize it in constructor
  • Expose hooks via getAIAgentHooks() method
  • Dispose of hooks in deactivate alongside other resources
packages/vscode-extension/src/extension.ts
Extend extension-core exports for AI types
  • Export AICommand, AICommandResult, AICommandHandlerFunction and related types
packages/extension-core/src/index.ts
Add tests and verification artifacts for AI hooks
  • Update existing extension tests to assert hook availability and context fetching
  • Add dedicated ai-agent-hooks.test.ts for hook behavior
  • Include manual verification guide and test scripts (VERIFY_AI_HOOKS.md, test-ai-hooks.js)
packages/vscode-extension/src/__tests__/extension.test.ts
packages/vscode-extension/src/__tests__/ai-agent-hooks.test.ts
packages/vscode-extension/VERIFY_AI_HOOKS.md
packages/vscode-extension/test-ai-hooks.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Instead of polling extensionCore.getProgressStreamer() every 500ms, consider exposing an event emitter or callback API in ExtensionCore to drive progress updates more efficiently and avoid intervals.
  • The inline generation of agentId and session info in getAIContext makes testing and session reuse harder—consider moving context creation into a configurable factory or allowing overrides for deterministic tests.
  • The large manual verification docs and scripts in the extension package could be moved to a separate dev‐only docs directory or trimmed, so the shipped extension remains focused and lightweight.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Instead of polling `extensionCore.getProgressStreamer()` every 500ms, consider exposing an event emitter or callback API in ExtensionCore to drive progress updates more efficiently and avoid intervals.
- The inline generation of agentId and session info in `getAIContext` makes testing and session reuse harder—consider moving context creation into a configurable factory or allowing overrides for deterministic tests.
- The large manual verification docs and scripts in the extension package could be moved to a separate dev‐only docs directory or trimmed, so the shipped extension remains focused and lightweight.

## Individual Comments

### Comment 1
<location> `packages/vscode-extension/src/ai/ai-agent-hooks.ts:111-118` </location>
<code_context>
+  /**
+   * Register a custom AI-accessible function
+   */
+  registerAIFunction(name: string, handler: AICommandHandlerFunction): void {
+    // Store custom handler
+    this.customHandlers.set(name, handler);
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Potential for handler name collisions in registerAIFunction.

Warn or prevent overwriting if a handler with the same name already exists.

```suggestion
  registerAIFunction(name: string, handler: AICommandHandlerFunction): void {
    // Prevent overwriting existing handler
    if (this.customHandlers.has(name)) {
      console.warn(
        `[AI Agent] Handler with name "${name}" already exists. Registration skipped to prevent overwriting.`
      );
      return;
    }
    // Store custom handler
    this.customHandlers.set(name, handler);

    // Also register with ExtensionCore's AI command handler for consistency
    const aiCommandHandler = this.extensionCore.getAICommandHandler();
    aiCommandHandler.registerHandler(name, handler);
  }
```
</issue_to_address>

### Comment 2
<location> `packages/vscode-extension/src/ai/ai-agent-hooks.ts:173` </location>
<code_context>
+  /**
+   * Get AI context for commands
+   */
+  private async getAIContext(): Promise<AIContext> {
+    const now = new Date();
+    return {
</code_context>

<issue_to_address>
**suggestion (bug_risk):** AIContext agentId and sessionId generation may cause collisions.

Date.now() may generate duplicate IDs if multiple instances are created simultaneously. Use a more reliable unique ID method if these values are critical for tracking or persistence.

Suggested implementation:

```typescript
    return {
      agentId: `agent-${crypto.randomUUID()}`,
      agentType: AgentType.CUSTOM,
      session: {
        sessionId: `session-${crypto.randomUUID()}`,
        startTime: now,
        lastActivity: now,
        duration: 0,
        interactionCount: 0,
        context: {},
      },

```

If `crypto.randomUUID()` is not available in your runtime, you may need to use a UUID library such as `uuid` (npm package). In that case, import and use `v4()` from `uuid` instead.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread packages/vscode-extension/src/ai/ai-agent-hooks.ts
Comment thread packages/vscode-extension/src/ai/ai-agent-hooks.ts
@bomanaps bomanaps merged commit 5b6036b into Patrick-Ehimen:main Nov 20, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant