-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToolCall.java
More file actions
88 lines (80 loc) · 2.38 KB
/
Copy pathToolCall.java
File metadata and controls
88 lines (80 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.example.codingagent.model;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
/**
* 工具调用描述。
*/
public record ToolCall(
String toolName,
String argument,
Map<String, Object> arguments
) {
public ToolCall {
arguments = arguments == null
? Map.of()
: Collections.unmodifiableMap(new LinkedHashMap<>(arguments));
}
/**
* 兼容旧协议中的纯字符串参数工具调用。
*
* @param toolName 工具名
* @param argument 字符串参数
*/
public ToolCall(String toolName, String argument) {
this(toolName, argument, Map.of());
}
/**
* 兼容新协议中的结构化参数工具调用。
*
* @param toolName 工具名
* @param arguments 结构化参数
*/
public ToolCall(String toolName, Map<String, Object> arguments) {
this(toolName, null, arguments);
}
/**
* 是否包含可执行的参数载荷。
*
* @return true 表示包含字符串参数或结构化参数
*/
public boolean hasArgumentPayload() {
return StringUtils.isNotBlank(argument) || !arguments.isEmpty();
}
/**
* 读取结构化参数中的字符串字段。
*
* @param key 字段名
* @return 字段值
*/
public String structuredString(String key) {
Object value = arguments.get(key);
if (value == null) {
return null;
}
String text = String.valueOf(value).trim();
return text.isBlank() ? null : text;
}
/**
* 将工具参数统一序列化为工具层可消费的字符串。
*
* @param objectMapper JSON 序列化器
* @return 字符串参数
*/
public String resolveArgument(ObjectMapper objectMapper) {
if (StringUtils.isNotBlank(argument)) {
return argument.trim();
}
if (arguments.isEmpty()) {
return null;
}
try {
return objectMapper.writeValueAsString(arguments);
} catch (JsonProcessingException ex) {
throw new IllegalStateException("结构化工具参数序列化失败: " + toolName, ex);
}
}
}