|
| 1 | +package datadog.communication.serialization.aiguard |
| 2 | + |
| 3 | +import datadog.communication.serialization.EncodingCache |
| 4 | +import datadog.communication.serialization.GrowableBuffer |
| 5 | +import datadog.communication.serialization.msgpack.MsgPackWriter |
| 6 | +import datadog.trace.api.aiguard.AIGuard |
| 7 | +import datadog.trace.test.util.DDSpecification |
| 8 | +import org.msgpack.core.MessagePack |
| 9 | +import org.msgpack.value.Value |
| 10 | + |
| 11 | +import java.nio.charset.StandardCharsets |
| 12 | +import java.util.function.Function |
| 13 | + |
| 14 | +class MessageWriterTest extends DDSpecification { |
| 15 | + |
| 16 | + private EncodingCache encodingCache |
| 17 | + private GrowableBuffer buffer |
| 18 | + private MsgPackWriter writer |
| 19 | + |
| 20 | + void setup() { |
| 21 | + injectSysConfig('ai_guard.enabled', 'true') |
| 22 | + final HashMap<CharSequence, byte[]> cache = new HashMap<>() |
| 23 | + encodingCache = new EncodingCache() { |
| 24 | + @Override |
| 25 | + byte[] encode(CharSequence chars) { |
| 26 | + cache.computeIfAbsent(chars, s -> s.toString().getBytes(StandardCharsets.UTF_8)) |
| 27 | + } |
| 28 | + } |
| 29 | + buffer = new GrowableBuffer(1024) |
| 30 | + writer = new MsgPackWriter(buffer) |
| 31 | + } |
| 32 | + |
| 33 | + void 'test write message'() { |
| 34 | + given: |
| 35 | + final message = AIGuard.Message.message('user', 'What day is today?') |
| 36 | + |
| 37 | + when: |
| 38 | + writer.writeObject(message, encodingCache) |
| 39 | + |
| 40 | + then: |
| 41 | + try (final unpacker = MessagePack.newDefaultUnpacker(buffer.slice())) { |
| 42 | + final value = asStringValueMap(unpacker.unpackValue()) |
| 43 | + value.size() == 2 |
| 44 | + value.role == 'user' |
| 45 | + value.content == 'What day is today?' |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + void 'test write tool call'() { |
| 50 | + given: |
| 51 | + final message = |
| 52 | + AIGuard.Message.assistant( |
| 53 | + AIGuard.ToolCall.toolCall('call_1', 'function_1', 'args_1'), |
| 54 | + AIGuard.ToolCall.toolCall('call_2', 'function_2', 'args_2')) |
| 55 | + |
| 56 | + when: |
| 57 | + writer.writeObject(message, encodingCache) |
| 58 | + |
| 59 | + then: |
| 60 | + try (final unpacker = MessagePack.newDefaultUnpacker(buffer.slice())) { |
| 61 | + final value = asStringKeyMap(unpacker.unpackValue()) |
| 62 | + value.size() == 2 |
| 63 | + asString(value.role) == 'assistant' |
| 64 | + |
| 65 | + final toolCalls = value.get('tool_calls').asArrayValue().list() |
| 66 | + toolCalls.size() == 2 |
| 67 | + |
| 68 | + final firstCall = asStringKeyMap(toolCalls[0]) |
| 69 | + asString(firstCall.id) == 'call_1' |
| 70 | + final firstFunction = asStringValueMap(firstCall.function) |
| 71 | + firstFunction.name == 'function_1' |
| 72 | + firstFunction.arguments == 'args_1' |
| 73 | + |
| 74 | + final secondCall = asStringKeyMap(toolCalls[1]) |
| 75 | + asString(secondCall.id) == 'call_2' |
| 76 | + final secondFunction = asStringValueMap(secondCall.function) |
| 77 | + secondFunction.name == 'function_2' |
| 78 | + secondFunction.arguments == 'args_2' |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + void 'test write tool output'() throws IOException { |
| 83 | + given: |
| 84 | + final message = AIGuard.Message.tool('call_1', 'output') |
| 85 | + |
| 86 | + when: |
| 87 | + writer.writeObject(message, encodingCache) |
| 88 | + |
| 89 | + then: |
| 90 | + try (final unpacker = MessagePack.newDefaultUnpacker(buffer.slice())) { |
| 91 | + final value = asStringValueMap(unpacker.unpackValue()) |
| 92 | + value.size() == 3 |
| 93 | + value.role == 'tool' |
| 94 | + value.tool_call_id == 'call_1' |
| 95 | + value.content == 'output' |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static <K, V> Map<K, V> mapValue( |
| 100 | + final Value values, |
| 101 | + final Function<Value, K> keyMapper, |
| 102 | + final Function<Value, V> valueMapper) { |
| 103 | + return values.asMapValue().entrySet().collectEntries { |
| 104 | + [(keyMapper.apply(it.key)): valueMapper.apply(it.value)] |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static Map<String, Value> asStringKeyMap(final Value values) { |
| 109 | + return mapValue(values, MessageWriterTest::asString, Function.identity()) |
| 110 | + } |
| 111 | + |
| 112 | + private static Map<String, String> asStringValueMap(final Value values) { |
| 113 | + return mapValue(values, MessageWriterTest::asString, MessageWriterTest::asString) |
| 114 | + } |
| 115 | + |
| 116 | + private static String asString(final Value value) { |
| 117 | + return value.asStringValue().asString() |
| 118 | + } |
| 119 | +} |
0 commit comments