File size: 3,543 Bytes
80ffd2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package parser

import "strings"

// BlockInfo contains information about a tool call block
type BlockInfo struct {
	StartIndex int
	TagType    string
}

// FindToolCallBlockAtEnd finds tool call block at the end of text
func FindToolCallBlockAtEnd(text string) *BlockInfo {
	trimmed := strings.TrimRight(text, " \t\n\r")

	// Find first occurrence of each tool call tag
	firstFunctionCalls := strings.Index(trimmed, "<function_calls>")
	firstTool := strings.Index(trimmed, "<tool>")
	firstTools := strings.Index(trimmed, "<tools>")

	// Find the earliest tag
	firstOpenTag := -1
	tagType := ""

	if firstFunctionCalls != -1 {
		firstOpenTag = firstFunctionCalls
		tagType = "function_calls"
	}
	if firstTool != -1 && (firstOpenTag == -1 || firstTool < firstOpenTag) {
		firstOpenTag = firstTool
		tagType = "tool"
	}
	if firstTools != -1 && (firstOpenTag == -1 || firstTools < firstOpenTag) {
		firstOpenTag = firstTools
		tagType = "tools"
	}

	if firstOpenTag == -1 {
		return nil
	}

	// Use stack to find matching close tag
	openTag := "<" + tagType + ">"
	closeTag := "</" + tagType + ">"

	depth := 0
	pos := firstOpenTag
	lastClosePos := -1

	for pos < len(trimmed) {
		nextOpen := strings.Index(trimmed[pos:], openTag)
		nextClose := strings.Index(trimmed[pos:], closeTag)

		if nextOpen == -1 && nextClose == -1 {
			break
		}

		if nextOpen != -1 && (nextClose == -1 || nextOpen < nextClose) {
			// Found open tag
			depth++
			pos = pos + nextOpen + len(openTag)
		} else {
			// Found close tag
			depth--
			if depth == 0 {
				lastClosePos = pos + nextClose + len(closeTag)
			}
			pos = pos + nextClose + len(closeTag)
		}
	}

	// Check if tool call is valid
	if lastClosePos != -1 {
		// Has complete open and close tags - this is valid
		// Accept tool calls even if there's text after them
	} else if depth > 0 {
		// Unclosed tool call, might be in progress (streaming)
		// Allow this case
	} else {
		// No valid tool call found
		return nil
	}

	return &BlockInfo{
		StartIndex: firstOpenTag,
		TagType:    tagType,
	}
}

// HasCompleteToolCall checks if text has complete tool call
func HasCompleteToolCall(text string) bool {
	trimmed := strings.TrimRight(text, " \t\n\r")
	return strings.HasSuffix(trimmed, "</function_calls>") ||
		strings.HasSuffix(trimmed, "</tool>") ||
		strings.HasSuffix(trimmed, "</tools>")
}

// HasIncompleteToolCall checks if text has incomplete tool call
func HasIncompleteToolCall(text string) bool {
	trimmed := strings.TrimRight(text, " \t\n\r")

	// Find first occurrence of each tool call tag
	firstFunctionCalls := strings.Index(trimmed, "<function_calls>")
	firstTool := strings.Index(trimmed, "<tool>")
	firstTools := strings.Index(trimmed, "<tools>")

	firstOpenTag := -1
	tagType := ""

	if firstFunctionCalls != -1 {
		firstOpenTag = firstFunctionCalls
		tagType = "function_calls"
	}
	if firstTool != -1 && (firstOpenTag == -1 || firstTool < firstOpenTag) {
		firstOpenTag = firstTool
		tagType = "tool"
	}
	if firstTools != -1 && (firstOpenTag == -1 || firstTools < firstOpenTag) {
		firstOpenTag = firstTools
		tagType = "tools"
	}

	if firstOpenTag == -1 {
		return false
	}

	// Check if there's a corresponding close tag
	closeTag := "</" + tagType + ">"
	closeIndex := strings.LastIndex(trimmed, closeTag)

	// If no close tag or close tag is before open tag, tool call is incomplete
	return closeIndex == -1 || closeIndex < firstOpenTag
}