File size: 5,882 Bytes
8d3471e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package claude

import (
	"ds2api/internal/toolcall"
	"encoding/json"
	"fmt"
	"strings"

	"ds2api/internal/prompt"
)

func normalizeClaudeMessages(messages []any) []any {
	out := make([]any, 0, len(messages))
	state := &claudeToolCallState{
		nameByID:       map[string]string{},
		lastIDByName:   map[string]string{},
		callIDSequence: 0,
	}
	for _, m := range messages {
		msg, ok := m.(map[string]any)
		if !ok {
			continue
		}
		role := strings.ToLower(strings.TrimSpace(fmt.Sprintf("%v", msg["role"])))
		switch content := msg["content"].(type) {
		case []any:
			textParts := make([]string, 0, len(content))
			flushText := func() {
				if len(textParts) == 0 {
					return
				}
				out = append(out, map[string]any{
					"role":    role,
					"content": strings.Join(textParts, "\n"),
				})
				textParts = textParts[:0]
			}
			for _, block := range content {
				b, ok := block.(map[string]any)
				if !ok {
					continue
				}
				typeStr := strings.ToLower(strings.TrimSpace(fmt.Sprintf("%v", b["type"])))
				switch typeStr {
				case "text":
					if t, ok := b["text"].(string); ok {
						textParts = append(textParts, t)
					}
				case "tool_use":
					if role == "assistant" {
						flushText()
						if toolMsg := normalizeClaudeToolUseToAssistant(b, state); toolMsg != nil {
							out = append(out, toolMsg)
						}
						continue
					}
					if raw := strings.TrimSpace(formatClaudeUnknownBlockForPrompt(b)); raw != "" {
						textParts = append(textParts, raw)
					}
				case "tool_result":
					flushText()
					if toolMsg := normalizeClaudeToolResultToToolMessage(b, state); toolMsg != nil {
						out = append(out, toolMsg)
					}
				default:
					if raw := strings.TrimSpace(formatClaudeUnknownBlockForPrompt(b)); raw != "" {
						textParts = append(textParts, raw)
					}
				}
			}
			flushText()
		default:
			copied := cloneMap(msg)
			out = append(out, copied)
		}
	}
	return out
}

func buildClaudeToolPrompt(tools []any) string {
	toolSchemas := make([]string, 0, len(tools))
	names := make([]string, 0, len(tools))
	for _, t := range tools {
		m, ok := t.(map[string]any)
		if !ok {
			continue
		}
		name, desc, schemaObj := extractClaudeToolMeta(m)
		if name == "" {
			continue
		}
		names = append(names, name)
		schema, _ := json.Marshal(schemaObj)
		toolSchemas = append(toolSchemas, fmt.Sprintf("Tool: %s\nDescription: %s\nParameters: %s", name, desc, schema))
	}
	if len(toolSchemas) == 0 {
		return ""
	}
	return "You have access to these tools:\n\n" +
		strings.Join(toolSchemas, "\n\n") + "\n\n" +
		toolcall.BuildToolCallInstructions(names)
}

//nolint:unused // retained for compatibility with pending Claude tool-result prompt flow.
func formatClaudeToolResultForPrompt(block map[string]any) string {
	if block == nil {
		return ""
	}
	payload := map[string]any{
		"type":    "tool_result",
		"content": block["content"],
	}
	if toolCallID := strings.TrimSpace(fmt.Sprintf("%v", block["tool_use_id"])); toolCallID != "" {
		payload["tool_call_id"] = toolCallID
	} else if toolCallID := strings.TrimSpace(fmt.Sprintf("%v", block["tool_call_id"])); toolCallID != "" {
		payload["tool_call_id"] = toolCallID
	}
	if name := strings.TrimSpace(fmt.Sprintf("%v", block["name"])); name != "" {
		payload["name"] = name
	}
	b, err := json.Marshal(payload)
	if err != nil {
		return strings.TrimSpace(fmt.Sprintf("%v", payload))
	}
	return string(b)
}

func normalizeClaudeToolUseToAssistant(block map[string]any, state *claudeToolCallState) map[string]any {
	if block == nil {
		return nil
	}
	name := strings.TrimSpace(fmt.Sprintf("%v", block["name"]))
	if name == "" {
		return nil
	}
	callID := safeStringValue(block["id"])
	if callID == "" {
		callID = safeStringValue(block["tool_use_id"])
	}
	if callID == "" {
		callID = state.nextID()
	}
	state.nameByID[callID] = name
	state.lastIDByName[strings.ToLower(name)] = callID
	arguments := block["input"]
	if arguments == nil {
		arguments = map[string]any{}
	}
	argsJSON, err := json.Marshal(arguments)
	if err != nil || len(argsJSON) == 0 {
		argsJSON = []byte("{}")
	}
	toolCalls := []any{
		map[string]any{
			"id":   callID,
			"type": "function",
			"function": map[string]any{
				"name":      name,
				"arguments": string(argsJSON),
			},
		},
	}
	return map[string]any{
		"role":       "assistant",
		"content":    prompt.FormatToolCallsForPrompt(toolCalls),
		"tool_calls": toolCalls,
	}
}

func normalizeClaudeToolResultToToolMessage(block map[string]any, state *claudeToolCallState) map[string]any {
	if block == nil {
		return nil
	}
	name := safeStringValue(block["name"])
	toolCallID := safeStringValue(block["tool_use_id"])
	if toolCallID == "" {
		toolCallID = safeStringValue(block["tool_call_id"])
	}
	if toolCallID == "" {
		if name != "" {
			toolCallID = strings.TrimSpace(state.lastIDByName[strings.ToLower(name)])
		}
	}
	if toolCallID == "" {
		toolCallID = state.nextID()
	}
	out := map[string]any{
		"role":         "tool",
		"tool_call_id": toolCallID,
		"content":      normalizeClaudeToolResultContent(block["content"]),
	}
	if name != "" {
		out["name"] = name
		state.nameByID[toolCallID] = name
		state.lastIDByName[strings.ToLower(name)] = toolCallID
	} else if inferred := strings.TrimSpace(state.nameByID[toolCallID]); inferred != "" {
		out["name"] = inferred
	}
	return out
}

func normalizeClaudeToolResultContent(content any) any {
	if text, ok := content.(string); ok {
		return text
	}
	payload := map[string]any{
		"type":    "tool_result",
		"content": content,
	}
	b, err := json.Marshal(sanitizeClaudeBlockForPrompt(payload))
	if err != nil {
		return strings.TrimSpace(fmt.Sprintf("%v", content))
	}
	return string(b)
}

func formatClaudeBlockRaw(block map[string]any) string {
	if block == nil {
		return ""
	}
	b, err := json.Marshal(block)
	if err != nil {
		return strings.TrimSpace(fmt.Sprintf("%v", block))
	}
	return string(b)
}