Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ private static long block(ContentBlock block) {
return 12
+ text(use.getId())
+ text(use.getName())
+ text(JsonUtils.getJsonCodec().toJson(use.getInput()))
+ text(use.getContent());
+ text(JsonUtils.resolveToolCallArgsJson(use));
}
if (block instanceof ToolResultBlock result) {
long tokens = 12 + text(result.getId()) + text(result.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2024-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.agentscope.harness.agent.context;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.agentscope.core.message.Msg;
import io.agentscope.core.message.MsgRole;
import io.agentscope.core.message.ThinkingBlock;
import io.agentscope.core.message.ToolUseBlock;
import io.agentscope.core.middleware.ModelCallInput;
import io.agentscope.core.model.Model;
import io.agentscope.core.util.JsonUtils;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

class ContextTokenEstimatorTest {
private static final Map<String, Object> INPUT = Map.of("content", "<html>".repeat(4000));

@Test
void streamedArgumentsAreCountedOnce() {
String raw = JsonUtils.getJsonCodec().toJson(INPUT);
long expected = 35 + (long) Math.ceil(raw.length() / 2.5);
assertEquals(expected, estimate(tool(INPUT, null)));
assertEquals(expected, estimate(tool(INPUT, raw)));
assertEquals(expected, estimate(tool(Map.of(), raw)));
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {"{}", " { } ", "{broken", "[]"})
void missingEmptyOrInvalidRawArgumentsFallBackToInput(String raw) {
assertEquals(estimate(tool(INPUT, null)), estimate(tool(INPUT, raw)));
}

@Test
void validRawArgumentsTakePrecedenceOverParsedInput() {
String raw = "{\"path\":\"actual.html\"}";
assertEquals(estimate(tool(Map.of(), raw)), estimate(tool(INPUT, raw)));
}

@Test
void malformedArgumentsAreSanitized() {
var malformed =
ToolUseBlock.builder()
.id("tool-1")
.name("write_file")
.input(INPUT)
.content("{broken")
.metadata(Map.of(ToolUseBlock.METADATA_MALFORMED_ARGUMENTS, true))
.build();
assertEquals(estimate(tool(Map.of(), "{}")), estimate(malformed));
}

@Test
void thinkingRemainsInTheEstimate() {
var tool = tool(INPUT, null);
String thinking = "think".repeat(1000);
var msg =
Msg.builder()
.role(MsgRole.ASSISTANT)
.name("assistant")
.content(ThinkingBlock.builder().thinking(thinking).build(), tool)
.build();
assertEquals(estimate(tool) + 2000, estimate(msg));
}

private ToolUseBlock tool(Map<String, Object> input, String content) {
return ToolUseBlock.builder()
.id("tool-1")
.name("write_file")
.input(input)
.content(content)
.build();
}

private long estimate(ToolUseBlock tool) {
return estimate(
Msg.builder().role(MsgRole.ASSISTANT).name("assistant").content(tool).build());
}

private long estimate(Msg msg) {
var input = new ModelCallInput(List.of(msg), List.of(), null, (Model) null);
return ContextTokenEstimator.approximate().estimate(input).tokens();
}
}
Loading