Skip to content

ai-statistics claude tool parsing #3519

@victorserbu2709

Description

@victorserbu2709

Why do you need it?

it would be nice if ai-statistics would parse anthropic streaming response to extract tools calls

How could it be?

something similar to

		for _, chunk := range chunks {

			jsonPath = "type"
			jsonObj := gjson.GetBytes(chunk, jsonPath)
			if jsonObj.Exists() {
				value = jsonObj.Value()
			}

			if value == "content_block_start" {

				jsonPath = "content_block.type"
				jsonObj := gjson.GetBytes(chunk, jsonPath)
				if jsonObj.Exists() {
					value = jsonObj.Value()
				}

				if value == "tool_use" {
					tool_usage["type"] = value

					jsonPath = "content_block.id"
					jsonObj = gjson.GetBytes(chunk, jsonPath)
					if jsonObj.Exists() {
						in_tool_block = true
						tool_usage["id"] = jsonObj.String()

						jsonPath = "content_block.name"
						jsonObj = gjson.GetBytes(chunk, jsonPath)
						if jsonObj.Exists() {
							tool_usage["name"] = jsonObj.String()
						}

						jsonPath = "content_block.input"
						jsonObj = gjson.GetBytes(chunk, jsonPath)
						if jsonObj.Exists() {
							var ok bool
							tool_usage_input, ok = jsonObj.Value().(map[string]interface{})
							if !ok {
								log.Infof("cannot get claude tool input object: %s", jsonObj.String())
							}
						}

					}

				}

			} else if value == "content_block_stop" && in_tool_block == true {
				in_tool_block = false
				err := json.Unmarshal([]byte(tool_usage_buffer), &tool_usage_input)
				if err != nil {
					log.Errorf("cannot unmarshall claude tool_usage_buffer: %s", tool_usage_buffer)
				} else {
					tool_usage["input"] = tool_usage_input
				}
			} else if value == "content_block_delta" && in_tool_block == true {
				jsonPath = "delta.partial_json"
				jsonObj := gjson.GetBytes(chunk, jsonPath)
				if jsonObj.Exists() {
					tool_usage_buffer += jsonObj.String()
				}
			}
			value = tool_usage
		}

Other related information

Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions