/*
 * Copyright 2026 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package model

import (
	"github.com/cloudwego/eino/callbacks"
	"github.com/cloudwego/eino/schema"
)

// AgenticConfig is the config for the agentic model.
type AgenticConfig struct {
	// Model is the model name.
	Model string
	// MaxTokens is the max number of output tokens, if reached the max tokens, the model will stop generating.
	MaxTokens int
	// Temperature is the temperature, which controls the randomness of the agentic model.
	Temperature float32
	// TopP is the top p, which controls the diversity of the agentic model.
	TopP float32
}

// AgenticCallbackInput is the input for the agentic model callback.
type AgenticCallbackInput struct {
	// Messages is the agentic messages to be sent to the agentic model.
	Messages []*schema.AgenticMessage
	// Tools is the tools to be used in the agentic model.
	Tools []*schema.ToolInfo
	// Config is the config for the agentic model.
	Config *AgenticConfig
	// Extra is the extra information for the callback.
	Extra map[string]any
}

// AgenticCallbackOutput is the output for the agentic model callback.
type AgenticCallbackOutput struct {
	// Message is the agentic message generated by the agentic model.
	Message *schema.AgenticMessage
	// Config is the config for the agentic model.
	Config *AgenticConfig
	// TokenUsage is the token usage of this request.
	TokenUsage *TokenUsage
	// Extra is the extra information for the callback.
	Extra map[string]any
}

// ConvAgenticCallbackInput converts the callback input to the agentic model callback input.
func ConvAgenticCallbackInput(src callbacks.CallbackInput) *AgenticCallbackInput {
	switch t := src.(type) {
	case *AgenticCallbackInput:
		// when callback is triggered within component implementation,
		// the input is usually already a typed *model.AgenticCallbackInput
		return t
	case []*schema.AgenticMessage:
		// when callback is injected by graph node, not the component implementation itself,
		// the input is the input of Agentic Model interface, which is []*schema.AgenticMessage
		return &AgenticCallbackInput{
			Messages: t,
		}
	default:
		return nil
	}
}

// ConvAgenticCallbackOutput converts the callback output to the agentic model callback output.
func ConvAgenticCallbackOutput(src callbacks.CallbackOutput) *AgenticCallbackOutput {
	switch t := src.(type) {
	case *AgenticCallbackOutput:
		// when callback is triggered within component implementation,
		// the output is usually already a typed *model.AgenticCallbackOutput
		return t
	case *schema.AgenticMessage:
		// when callback is injected by graph node, not the component implementation itself,
		// the output is the output of Agentic Model interface, which is *schema.AgenticMessage
		return &AgenticCallbackOutput{
			Message: t,
		}
	default:
		return nil
	}
}
