## Base for every tool: one tool per file, each exposing the schema the model ## sees and the code that runs it. The registry in tools.gd instantiates each ## one — instance methods rather than statics, since calling a static through ## a script variable is not dependable across Godot versions. class_name ClaudeGodotTool extends RefCounted ## JSON schema the provider is given: {"name", "description", "input_schema"}. func definition() -> Dictionary: return {} ## Returns {"content": String} plus optional "is_error": bool, and any extra ## keys the dock wants (write_file and edit_file add "diff_ops"). func execute(_input: Dictionary) -> Dictionary: return {"is_error": true, "content": "Tool not implemented."} ## Resolves a tool path. Everything is project-relative (res://) except the ## addon's own corner of user://, which holds memory and sessions — those are ## deliberately outside the project so they aren't committed with the game. ## No other part of user:// is reachable, and .. can't climb out of either. static func resolve_path(path: String) -> String: if path.begins_with(ClaudeGodotMemory.ROOT): return path.simplify_path() if not path.begins_with("res://"): path = "res://" + path.trim_prefix("/") return path.simplify_path() ## Shared by the settings-listing tools. `source` is the ProjectSettings ## singleton or an EditorSettings instance; both expose get_property_list(). static func list_settings(source, filter: String) -> Dictionary: var needle := filter.to_lower() var names: Array = [] for prop in source.get_property_list(): var n: String = prop.get("name", "") if n == "": continue if needle == "" or n.to_lower().find(needle) != -1: names.append(n) names.sort() var truncated := names.size() > 200 if truncated: names = names.slice(0, 200) var out := "\n".join(names) if names.size() > 0 else "(no matches)" if truncated: out += "\n… (truncated, use a more specific filter)" return {"content": out} ## Identifies a file by its leading bytes rather than its extension — a ## screenshot saved as .dat is still a PNG, and a .png that is really text ## should not be announced as an image. Returns {"mime", "label"} for the ## formats the vision APIs accept, or {} for anything else. static func sniff_image(bytes: PackedByteArray) -> Dictionary: if bytes.size() < 12: return {} if _starts_with(bytes, [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]): return {"mime": "image/png", "label": "PNG"} if _starts_with(bytes, [0xFF, 0xD8, 0xFF]): return {"mime": "image/jpeg", "label": "JPEG"} if _starts_with(bytes, [0x47, 0x49, 0x46, 0x38]): return {"mime": "image/gif", "label": "GIF"} # WebP is a RIFF container: "RIFF" .... "WEBP". if _starts_with(bytes, [0x52, 0x49, 0x46, 0x46]) and bytes.slice(8, 12).get_string_from_ascii() == "WEBP": return {"mime": "image/webp", "label": "WebP"} return {} ## Non-image binary: a NUL byte anywhere early is the classic tell, and no ## text file this tool should return will contain one. static func looks_binary(bytes: PackedByteArray) -> bool: var limit: int = mini(bytes.size(), 8192) for i in range(limit): if bytes[i] == 0: return true return false static func _starts_with(bytes: PackedByteArray, prefix: Array) -> bool: if bytes.size() < prefix.size(): return false for i in range(prefix.size()): if bytes[i] != prefix[i]: return false return true