Package com.veeva.vault.sdk.api.ai
package com.veeva.vault.sdk.api.ai
This package provides interfaces and classes for AI operations.
2. Create the Aicontexttype record
2. Create the Aitooltype record
Use Case: Defining a Context Type Runtime Handler
The AiContextTypeRuntimeHandler entrypoint can be used to define a handler for
generating context sent to an LLM as part of an agent action execution
@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 the Aicontexttype record
RECREATE Aicontexttype my_context_type__c (
label('My Context Type'),
admin_configurable(true),
runtime_code('Aicontexttyperuntimecode.com.veeva.vault.custom.context.MyContextTypeRuntimeHandler')
);
Use Case: Defining a Tool Type Runtime Handler
The AiToolTypeRuntimeHandler entrypoint can be used to define a handler for
tool spec generation and tool execution when invoked by an LLM
@ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
@AiToolTypeRuntimeHandlerInfo(dynamicToolSpec = false)
public class MyToolTypeRuntimeHandler implements AiToolTypeRuntimeHandler {
@Override
public AiToolTypeExecutionResponse onExecute(AiToolTypeExecuteContext context) {
JsonService jsonService = ServiceLocator.locate(JsonService.class);
RecordService recordService = ServiceLocator.locate(RecordService.class);
// Create a new record and return the created ID
Record newRecord = recordService.newRecord("tool_invocations__c");
String nameInput = context.getInput().getValue("name", JsonValueType.STRING);
newRecord.setValue("name__v", nameInput);
JsonObjectBuilder toolOutput = jsonService.newJsonObjectBuilder();
RecordBatchSaveRequest saveRequest = recordService.newRecordBatchSaveRequestBuilder()
.withRecords(VaultCollections.asList(newRecord))
.build();
recordService.batchSaveRecords(saveRequest)
.onSuccesses(successes -> {
toolOutput.setValue("recordId", successes.get(0).getRecordId());
})
.rollbackOnErrors()
.execute();
return context.newSuccessBuilder(AiToolTypeExecutionResponse.JsonResultBuilder.class)
.withJson(toolOutput.build())
.build();
}
}
2. Create the Aitooltype record
RECREATE Aitooltype my_tool_type__c (
label('My Tool Type'),
admin_configurable(true),
description ('Sample tool to create a Tool Invocations record'),
runtime_code('Aitooltyperuntimecode.com.veeva.vault.custom.context.MyToolTypeRuntimeHandler')
);
Use Case: Defining a Dynamic Tool Type Runtime Handler
The AiToolTypeRuntimeHandler entrypoint can be used to define a handler for
tool spec generation and tool execution when invoked by an LLM
@ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
@AiToolTypeRuntimeHandlerInfo(dynamicToolSpec = true)
public class MyDynamicToolTypeHandler implements AiToolTypeRuntimeHandler {
@Override
public AiToolTypeExecutionResponse onExecute(AiToolTypeExecuteContext context) {
RecordService recordService = ServiceLocator.locate(RecordService.class);
JsonObject inputs = context.getInput();
Map<String, JsonProperty> properties = inputs.getProperties();
Record newCountry = recordService.newRecord("country__v");
properties.forEach((fieldName, property) -> {
newCountry.setValue(fieldName, inputs.getValue(fieldName, JsonValueType.STRING));
});
RecordBatchSaveRequest saveRequest = recordService.newRecordBatchSaveRequestBuilder()
.withRecords(VaultCollections.asList(newCountry))
.build();
recordService.batchSaveRecords(saveRequest)
.onErrors(batchOperationErrors -> {
throw new RollbackException("MYDYNAMICTOOLTYPE_ERROR", batchOperationErrors.get(0).getError().getMessage());
})
.execute();
return context.newSuccessBuilder(AiToolTypeExecutionResponse.TextResultBuilder.class)
.withText("MyDynamicToolTypeHandler executed successfully")
.build();
}
@Override
public AiToolTypeGenerateInputResponse onGenerateInput(AiToolTypeGenerateInputContext context) {
ObjectMetadataService objectMetadataService = ServiceLocator.locate(ObjectMetadataService.class);
ObjectFieldMetadataCollectionRequest request = objectMetadataService.newObjectFieldMetadataCollectionRequestBuilder()
.withObjectName("country__v")
.build();
ObjectFieldMetadataCollectionResponse personMetadata = objectMetadataService.getObjectFieldMetadataCollection(request);
List<ObjectFieldMetadata> fields = personMetadata.getFields();
LlmToolInputObject.Builder inputBuilder = context.newObjectLlmToolItemBuilder();
for (ObjectFieldMetadata field : fields) {
if (!(field.getValueType() == ValueType.STRING)) {
continue;
}
LlmToolInputItem item = context.newSimpleTypeLlmToolItemBuilder()
.withType(LlmToolInputItemType.STRING)
.build();
inputBuilder.addProperty(field.getName(), item);
}
return context.newSuccessBuilder()
.withInput(inputBuilder.build())
.build();
}
}
2. Create the Aitooltype record
RECREATE Aitooltype my_dynamic_tool_type__c (
label('My Dynamic Tool Type'),
admin_configurable(true),
description ('Sample tool to create a country__v record'),
runtime_code('Aitooltyperuntimecode.com.veeva.vault.custom.context.MyDynamicToolTypeHandler')
);
-
ClassDescriptionRepresents the context that an AI Context Type Handler is executing within.Represents an output item of resolved context.Creates an instance of
AiContextTypeOutputItem.Agent Context Type context during resolution execution.Defines machine-readable types for context type resolution errors.Represents the result of Context Type resolutionCreates an instance ofAiContextTypeOutputItem.Builder for creating an errorAiContextTypeResolveResponse.Builder for creating a successAiContextTypeResolveResponse.Defines a Context Type Handler to be invoked at runtime to handle AI context.Indicates the class is anAiContextTypeRuntimeHandler.Represents the source forAiScopeType.DOCUMENTscopes.Builds the source used for document scope.Represents the source forAiScopeType.OBJECTscopes.Builds the source used for object scope.Defines the scope of an AI element.Defines the source of a scope.AiScopeType<T>Defines the types supported asAiScopes.Provides methods for common AI operations.Defines machine-readable types for AI tool execution errors.Represents the context that an AI tool type is executing within.AI tool type context used for execution of a tool.Represents the result of agent tool execution.Creates an instance ofAiToolTypeExecutionResponse.Builder for creating an errorAiToolTypeExecutionResponse.Builder for creating a successfulAiToolTypeExecutionResponsewith JSON result.Builder for creating a successAiToolTypeExecutionResponse.Builder for creating a successfulAiToolTypeExecutionResponsewith text result.Provides context for the AI tool type override input user action.Represents the result of an AI tool spec generation.General builder for aAiToolTypeGenerateInputResponse.Builder for a failedAiToolTypeGenerateInputResponse.Builder for a successfulAiToolTypeGenerateInputResponse.Defines a tool type runtime handler to be invoked when configured against related AI tools.Indicates a class is aAiToolTypeRuntimeHandler.Represents array input for tool execution in LLM Service.Constructs a LLM Input for array types.Represents input for tool execution in LLM Service.Creates an instance ofLlmToolInputItem.Constructs a LLM Input for unstructured types.SupportedLlmToolInputItemtypes.Represents object input for tool execution in LLM Service.Constructs a LLM Input for unstructured types.Indicates that a tool class does not use dynamic tool spec generation.