Skip to content

How to Create Custom Context Types

To create a custom context type with Vault Java SDK:

  1. Create a Context Type Runtime Handler:

    @ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
    @AiContextTypeRuntimeHandlerInfo(scope = AiScope.DOCUMENT)
    public class MyContextTypeRuntimeHandler implements AiContextTypeRuntimeHandler{
        @Override
        public AiContextTypeResolveResponse onResolve(AiContextTypeResolveContext context){
            AiDocumentScopeSource documentScopeSource = context.getScopeSource(AiScopeType.DOCUMENT);
            DocumentVersion documentVersion = documentScopeSource.getDocumentVersion();
            String documentName = documentVersion.getValue("name__v", ValueType.STRING);
    
            AiContextTypeOutputItem contextOutput = context.newOutputItemBuilder()
                .withText("The current document name is [" + documentName + "]")
                .build();
    
            return context.newSuccessResponseBuilder()
                .appendOutputItem(contextOutput)
                .build();
             }
         }
  2. Create an Aicontexttyperuntimecode MDL record referencing the AiContextTypeRuntimeHandler, and create an Aicontexttype MDL record:

    RECREATE Aicontexttype my_custom_context_type__c (
    label('My Context Type'),
    admin_configurable(true),
    runtime_code('Aicontexttyperuntimecode.com.veeva.vault.custom.context.MyContextTypeRuntimeHandler')
          );

To create a context type compatible with agents of any scope, use AiScope.GLOBAL in the @AiContextTypeRuntimeHandlerInfo annotation.

  1. Create a Context Type Runtime Handler with AiScope.GLOBAL:

    @ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
    @AiContextTypeRuntimeHandlerInfo(scope = AiScope.GLOBAL)
    public class MyGlobalContextTypeRuntimeHandler implements AiContextTypeRuntimeHandler {
        @Override
        public AiContextTypeResolveResponse onResolve(AiContextTypeResolveContext context) {
            return context.newSuccessResponseBuilder().build();
        }
    }
  2. Create an Aicontexttyperuntimecode MDL record referencing the handler, and create an Aicontexttype MDL record:

    RECREATE Aicontexttype my_global_context_type__c (
        label('My Global Context Type'),
        admin_configurable(true),
        runtime_code('Aicontexttyperuntimecode.com.veeva.vault.custom.context.MyGlobalContextTypeRuntimeHandler')
    );

A Global context type can be assigned to object and document agents, making it reusable across agent types without creating a separate context type for each.