* Count tokens for a single parameter field in a tool definition. * @param fields The field definition object * @returns Token count for this field
( fields: Record<string, unknown> | undefined, )
| 210 | * @returns Token count for this field |
| 211 | */ |
| 212 | function countParameterFieldTokens( |
| 213 | fields: Record<string, unknown> | undefined, |
| 214 | ): number { |
| 215 | if (!fields) { |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | let tokens = 0; |
| 220 | const fieldType = fields["type"]; |
| 221 | const fieldDesc = fields["description"]; |
| 222 | const fieldEnum = fields["enum"]; |
| 223 | |
| 224 | if (fieldType && typeof fieldType === "string") { |
| 225 | tokens += 2; // Structure overhead for type |
| 226 | tokens += encode(fieldType).length; |
| 227 | } |
| 228 | |
| 229 | if (fieldDesc && typeof fieldDesc === "string") { |
| 230 | tokens += 2; // Structure overhead for description |
| 231 | tokens += encode(fieldDesc).length; |
| 232 | } |
| 233 | |
| 234 | if (fieldEnum && Array.isArray(fieldEnum) && fieldEnum.length > 0) { |
| 235 | tokens -= 3; |
| 236 | for (const e of fieldEnum) { |
| 237 | tokens += 3; |
| 238 | tokens += typeof e === "string" ? encode(e).length : 5; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return tokens; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Count tokens for a single tool's function definition. |
no test coverage detected