-
Notifications
You must be signed in to change notification settings - Fork 312
Initial implementation of the AI Guard SDK #9628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
manuel-alvarez-alvarez
merged 10 commits into
master
from
malvarez/ai-guard-sdk-evaluator
Oct 5, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
669d027
Initial implementation of the AI Guard SDK
manuel-alvarez-alvarez a19e07f
Apply PR feedback
manuel-alvarez-alvarez 84372ea
Apply PR feedback (2)
manuel-alvarez-alvarez 3806253
Apply PR feedback (3)
manuel-alvarez-alvarez 2f158f4
Add smoke test to fully validate the SDK
manuel-alvarez-alvarez c3d2911
Fix header name
manuel-alvarez-alvarez 4a9b722
Ensure we properly serialize messages in the meta struct
manuel-alvarez-alvarez 5e00829
Fix javadoc comment
manuel-alvarez-alvarez 8f443fc
Fix coverage
manuel-alvarez-alvarez 14397b2
Improve message writer tests
manuel-alvarez-alvarez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 18 additions & 10 deletions
28
communication/src/main/java/datadog/communication/serialization/Codec.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...tion/src/main/java/datadog/communication/serialization/custom/aiguard/FunctionWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package datadog.communication.serialization.custom.aiguard; | ||
|
||
import datadog.communication.serialization.EncodingCache; | ||
import datadog.communication.serialization.ValueWriter; | ||
import datadog.communication.serialization.Writable; | ||
import datadog.trace.api.aiguard.AIGuard; | ||
|
||
public class FunctionWriter implements ValueWriter<AIGuard.ToolCall.Function> { | ||
|
||
@Override | ||
public void write( | ||
final AIGuard.ToolCall.Function function, | ||
final Writable writable, | ||
final EncodingCache encodingCache) { | ||
writable.startMap(2); | ||
writable.writeString("name", encodingCache); | ||
writable.writeString(function.getName(), encodingCache); | ||
writable.writeString("arguments", encodingCache); | ||
writable.writeString(function.getArguments(), encodingCache); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...ation/src/main/java/datadog/communication/serialization/custom/aiguard/MessageWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package datadog.communication.serialization.custom.aiguard; | ||
|
||
import datadog.communication.serialization.EncodingCache; | ||
import datadog.communication.serialization.ValueWriter; | ||
import datadog.communication.serialization.Writable; | ||
import datadog.trace.api.aiguard.AIGuard; | ||
import datadog.trace.util.Strings; | ||
import java.util.List; | ||
|
||
public class MessageWriter implements ValueWriter<AIGuard.Message> { | ||
|
||
@Override | ||
public void write( | ||
final AIGuard.Message value, final Writable writable, final EncodingCache encodingCache) { | ||
final int[] size = {0}; | ||
final boolean hasRole = isNotBlank(value.getRole(), size); | ||
final boolean hasContent = isNotBlank(value.getContent(), size); | ||
final boolean hasToolCallId = isNotBlank(value.getToolCallId(), size); | ||
final boolean hasToolCalls = isNotEmpty(value.getToolCalls(), size); | ||
writable.startMap(size[0]); | ||
writeString(hasRole, "role", value.getRole(), writable, encodingCache); | ||
writeString(hasContent, "content", value.getContent(), writable, encodingCache); | ||
writeString(hasToolCallId, "tool_call_id", value.getToolCallId(), writable, encodingCache); | ||
writeToolCallArray(hasToolCalls, "tool_calls", value.getToolCalls(), writable, encodingCache); | ||
} | ||
|
||
private static void writeString( | ||
final boolean present, | ||
final String key, | ||
final String value, | ||
final Writable writable, | ||
final EncodingCache encodingCache) { | ||
if (present) { | ||
writable.writeString(key, encodingCache); | ||
writable.writeString(value, encodingCache); | ||
} | ||
} | ||
|
||
private static void writeToolCallArray( | ||
final boolean present, | ||
final String key, | ||
final List<AIGuard.ToolCall> values, | ||
final Writable writable, | ||
final EncodingCache encodingCache) { | ||
if (present) { | ||
writable.writeString(key, encodingCache); | ||
writable.writeObject(values, encodingCache); | ||
} | ||
} | ||
|
||
private static boolean isNotBlank(final String value, final int[] nonBlankCount) { | ||
final boolean hasText = Strings.isNotBlank(value); | ||
if (hasText) { | ||
nonBlankCount[0]++; | ||
} | ||
return hasText; | ||
} | ||
|
||
private static boolean isNotEmpty(final List<?> value, final int[] nonEmptyCount) { | ||
final boolean nonEmpty = value != null && !value.isEmpty(); | ||
if (nonEmpty) { | ||
nonEmptyCount[0]++; | ||
} | ||
return nonEmpty; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...tion/src/main/java/datadog/communication/serialization/custom/aiguard/ToolCallWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package datadog.communication.serialization.custom.aiguard; | ||
|
||
import datadog.communication.serialization.EncodingCache; | ||
import datadog.communication.serialization.ValueWriter; | ||
import datadog.communication.serialization.Writable; | ||
import datadog.trace.api.aiguard.AIGuard; | ||
|
||
public class ToolCallWriter implements ValueWriter<AIGuard.ToolCall> { | ||
|
||
@Override | ||
public void write( | ||
final AIGuard.ToolCall value, final Writable writable, final EncodingCache encodingCache) { | ||
writable.startMap(2); | ||
writable.writeString("id", encodingCache); | ||
writable.writeString(value.getId(), encodingCache); | ||
writable.writeString("function", encodingCache); | ||
writable.writeObject(value.getFunction(), encodingCache); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
...tion/src/test/groovy/datadog/communication/serialization/aiguard/MessageWriterTest.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package datadog.communication.serialization.aiguard | ||
|
||
import datadog.communication.serialization.EncodingCache | ||
import datadog.communication.serialization.GrowableBuffer | ||
import datadog.communication.serialization.msgpack.MsgPackWriter | ||
import datadog.trace.api.aiguard.AIGuard | ||
import datadog.trace.test.util.DDSpecification | ||
import org.msgpack.core.MessagePack | ||
import org.msgpack.value.Value | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.util.function.Function | ||
|
||
class MessageWriterTest extends DDSpecification { | ||
|
||
private EncodingCache encodingCache | ||
private GrowableBuffer buffer | ||
private MsgPackWriter writer | ||
|
||
void setup() { | ||
injectSysConfig('ai_guard.enabled', 'true') | ||
final HashMap<CharSequence, byte[]> cache = new HashMap<>() | ||
encodingCache = new EncodingCache() { | ||
@Override | ||
byte[] encode(CharSequence chars) { | ||
cache.computeIfAbsent(chars, s -> s.toString().getBytes(StandardCharsets.UTF_8)) | ||
} | ||
} | ||
buffer = new GrowableBuffer(1024) | ||
writer = new MsgPackWriter(buffer) | ||
} | ||
|
||
void 'test write message'() { | ||
given: | ||
final message = AIGuard.Message.message('user', 'What day is today?') | ||
|
||
when: | ||
writer.writeObject(message, encodingCache) | ||
|
||
then: | ||
try (final unpacker = MessagePack.newDefaultUnpacker(buffer.slice())) { | ||
final value = asStringValueMap(unpacker.unpackValue()) | ||
value.size() == 2 | ||
value.role == 'user' | ||
value.content == 'What day is today?' | ||
} | ||
} | ||
|
||
void 'test write tool call'() { | ||
given: | ||
final message = | ||
AIGuard.Message.assistant( | ||
AIGuard.ToolCall.toolCall('call_1', 'function_1', 'args_1'), | ||
AIGuard.ToolCall.toolCall('call_2', 'function_2', 'args_2')) | ||
|
||
when: | ||
writer.writeObject(message, encodingCache) | ||
|
||
then: | ||
try (final unpacker = MessagePack.newDefaultUnpacker(buffer.slice())) { | ||
final value = asStringKeyMap(unpacker.unpackValue()) | ||
value.size() == 2 | ||
asString(value.role) == 'assistant' | ||
|
||
final toolCalls = value.get('tool_calls').asArrayValue().list() | ||
toolCalls.size() == 2 | ||
|
||
final firstCall = asStringKeyMap(toolCalls[0]) | ||
asString(firstCall.id) == 'call_1' | ||
final firstFunction = asStringValueMap(firstCall.function) | ||
firstFunction.name == 'function_1' | ||
firstFunction.arguments == 'args_1' | ||
|
||
final secondCall = asStringKeyMap(toolCalls[1]) | ||
asString(secondCall.id) == 'call_2' | ||
final secondFunction = asStringValueMap(secondCall.function) | ||
secondFunction.name == 'function_2' | ||
secondFunction.arguments == 'args_2' | ||
} | ||
} | ||
|
||
void 'test write tool output'() throws IOException { | ||
given: | ||
final message = AIGuard.Message.tool('call_1', 'output') | ||
|
||
when: | ||
writer.writeObject(message, encodingCache) | ||
|
||
then: | ||
try (final unpacker = MessagePack.newDefaultUnpacker(buffer.slice())) { | ||
final value = asStringValueMap(unpacker.unpackValue()) | ||
value.size() == 3 | ||
value.role == 'tool' | ||
value.tool_call_id == 'call_1' | ||
value.content == 'output' | ||
} | ||
} | ||
|
||
private static <K, V> Map<K, V> mapValue( | ||
final Value values, | ||
final Function<Value, K> keyMapper, | ||
final Function<Value, V> valueMapper) { | ||
return values.asMapValue().entrySet().collectEntries { | ||
[(keyMapper.apply(it.key)): valueMapper.apply(it.value)] | ||
} | ||
} | ||
|
||
private static Map<String, Value> asStringKeyMap(final Value values) { | ||
return mapValue(values, MessageWriterTest::asString, Function.identity()) | ||
} | ||
|
||
private static Map<String, String> asStringValueMap(final Value values) { | ||
return mapValue(values, MessageWriterTest::asString, MessageWriterTest::asString) | ||
} | ||
|
||
private static String asString(final Value value) { | ||
return value.asStringValue().asString() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar | ||
|
||
plugins { | ||
id 'com.gradleup.shadow' | ||
} | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
apply from: "$rootDir/gradle/version.gradle" | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
dependencies { | ||
api libs.slf4j | ||
implementation libs.moshi | ||
implementation libs.okhttp | ||
|
||
api project(':dd-trace-api') | ||
implementation project(':internal-api') | ||
implementation project(':communication') | ||
|
||
testImplementation project(':utils:test-utils') | ||
testImplementation('org.skyscreamer:jsonassert:1.5.3') | ||
testImplementation('com.fasterxml.jackson.core:jackson-databind:2.20.0') | ||
} | ||
|
||
tasks.named("shadowJar", ShadowJar) { | ||
dependencies deps.excludeShared | ||
} | ||
|
||
tasks.named("jar", Jar) { | ||
archiveClassifier = 'unbundled' | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.