Skip to main content

Availability

Object Hooks Plugin Guide

Object Hooks are a powerful plugin capability that allows you to intercept and control CRUD operations on AI Studio objects before they are saved to the database. This enables validation, enrichment, policy enforcement, and integration with external systems.

Overview

Object Hooks provide plugins with the ability to:
  • Validate objects before creation or modification
  • Reject operations that don’t meet requirements
  • Enrich objects with additional data
  • Enforce policies (security, compliance, naming conventions)
  • Integrate with external systems (ticketing, approval workflows)
  • Audit changes with custom metadata
  • Transform data before it reaches the database

Supported Objects

Object Hooks work with four core AI Studio object types:

Hook Types

Each object type supports six hook types that fire at different points in the lifecycle: Important Notes:
  • before_* hooks can block operations by setting AllowOperation: false
  • after_* hooks are informational only and cannot block operations
  • Only before_create and before_update can modify the object itself
  • All hooks can add plugin metadata that is stored with the object

Implementing Object Hooks

Step 1: Implement ObjectHookHandler Interface

Step 2: Register Your Hooks

Declare which object types and hook types your plugin handles:
Expandable
Priority Ordering:
  • Lower priority numbers execute first (e.g., priority 10 runs before priority 50)
  • Use priorities to control execution order when multiple plugins handle the same hook
  • Common priority ranges:
    • 1-10: Critical validation that should run first
    • 11-49: Standard validation and policy enforcement
    • 50-89: Enrichment and transformation
    • 90-99: Logging and audit (should run last)

Step 3: Handle Hook Invocations

Process the hook request and return a response:
Expandable

Hook Request Structure

The ObjectHookRequest provides context about the operation:
Key Fields:
  • ObjectJson: The object being created/updated/deleted (JSON string)
  • PreviousJson: Previous object state for update operations (compare before/after)
  • ObjectID: 0 for create operations, actual ID for update/delete
  • OperationId: Unique ID for correlating logs and debugging

Hook Response Structure

Your plugin returns an ObjectHookResponse:
Response Patterns:

1. Allow Without Changes

2. Allow With Metadata

3. Block Operation

4. Modify Object (before_create/before_update only)

Expandable

Complete Example: LLM Validator

This example validates LLM objects to enforce security and quality policies:
Expandable

Use Case Examples

1. Enforce HTTPS for LLM Endpoints

Expandable

2. Auto-Enrich Objects with Defaults

Expandable

3. Integration with External Approval System

Expandable

4. Audit Trail with Change Tracking

Expandable

5. Prevent Deletion of Active Resources

Expandable

Best Practices

Validation

  • Fail fast: Perform quick checks first to avoid expensive operations
  • Clear messages: Provide specific, actionable error messages
  • Log verbosely: Use ctx.Services.Logger() for debugging
  • Handle errors: Always check JSON unmarshaling errors

Performance

  • Keep hooks lightweight: Hooks run synchronously in the request path
  • Cache lookups: Use KV storage for repeated validations
  • Timeout external calls: Use context timeouts for external APIs
  • Async for after_ hooks: Use goroutines for non-critical after_ hook work

Security

  • Validate all inputs: Never trust object data
  • Check permissions: Use req.UserId to enforce RBAC
  • Sanitize output: Don’t leak sensitive data in error messages
  • Audit changes: Log all modifications for compliance

Metadata

  • Use consistent keys: Establish naming conventions (e.g., plugin_name:key)
  • Version your metadata: Include plugin version for debugging
  • Don’t overload: Keep metadata concise, use external storage for large data
  • Document schema: Document what metadata your plugin adds

Error Handling

  • Block vs. log: Use AllowOperation: false sparingly
  • Helpful messages: User sees Message field, make it clear
  • Differentiate errors: Use RejectionReason for why it failed
  • Return nil error: Return (response, nil) unless plugin crashes

Debugging Object Hooks

Enable Verbose Logging

Test with hook-test-plugin

The hook-test-plugin example provides a comprehensive testing UI:
Features:
  • Test all 24 hook combinations (4 objects × 6 hook types)
  • Configure behavior per hook (allow/reject/modify/metadata)
  • View real-time hook invocations
  • Automated test runner

Common Issues

Issue: Hook not firing
  • Check GetObjectHookRegistrations() returns correct object types
  • Verify plugin is installed and enabled in AI Studio
  • Check plugin logs for initialization errors
Issue: Changes not persisting
  • Only before_create and before_update can modify objects
  • Must set Modified: true and provide ModifiedObjectJson
  • Verify JSON marshaling succeeds
Issue: Operation blocked unexpectedly
  • Check all registered plugins for the same hook
  • Lower priority plugins run first
  • Review plugin logs for rejection reasons

Manifest Configuration

Object hooks must be declared in your plugin manifest:
See Plugin Manifests Guide for complete manifest documentation.

Complete Working Examples

LLM Validator

examples/plugins/studio/llm-validator/
  • Validates LLM endpoints (HTTPS enforcement)
  • Blocks based on vendor or privacy score
  • Requires description field
  • Adds validation metadata

Hook Test Plugin

examples/plugins/studio/hook-test-plugin/
  • Comprehensive testing of all 24 hook combinations
  • Configurable behavior per hook type
  • Web UI for testing and configuration
  • Automated test runner