(code: string)
| 79 | * because it is free here and useful to log. |
| 80 | */ |
| 81 | export const parseToolCallCode = (code: string): ParsedToolCall | null => { |
| 82 | const match = TOOL_CALL_CODE.exec(code.trim()); |
| 83 | if (!match) return null; |
| 84 | |
| 85 | const [, root, serializedRole, serializedRest, serializedArgs] = match; |
| 86 | if (root === undefined || serializedRest === undefined || serializedArgs === undefined) |
| 87 | return null; |
| 88 | |
| 89 | const args = decodeArgs(serializedArgs); |
| 90 | if (Option.isNone(args)) return null; |
| 91 | |
| 92 | const path = decodePath(`${root}${serializedRest}`); |
| 93 | |
| 94 | if (serializedRole === undefined) return { path, args: args.value }; |
| 95 | |
| 96 | // The role is a JSON string literal for the same reason the args are a JSON |
| 97 | // literal: it decodes or it was not the proxy's emission. |
| 98 | const role = decodeRole(serializedRole); |
| 99 | if (Option.isNone(role) || role.value.length === 0) return null; |
| 100 | |
| 101 | return { path, role: role.value, args: args.value }; |
| 102 | }; |
| 103 | |
| 104 | const TOOL_PATH_IDENTIFIER = /^[A-Za-z_$][\w$]*$/; |
| 105 | const TOOL_PATH_SEGMENT = /^[A-Za-z_$][\w$-]*$/; |
no test coverage detected