components-contrib/conversation/echo/echo.go

80 lines
2.0 KiB
Go

/*
Copyright 2024 The Dapr 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 echo
import (
"context"
"reflect"
"github.com/dapr/components-contrib/conversation"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/kit/logger"
kmeta "github.com/dapr/kit/metadata"
)
// Echo implement is only for test.
type Echo struct {
model string
logger logger.Logger
}
func NewEcho(logger logger.Logger) conversation.Conversation {
e := &Echo{
logger: logger,
}
return e
}
func (e *Echo) Init(ctx context.Context, meta conversation.Metadata) error {
r := &conversation.ConversationRequest{}
err := kmeta.DecodeMetadata(meta.Properties, r)
if err != nil {
return err
}
e.model = r.Model
return nil
}
func (e *Echo) GetComponentMetadata() (metadataInfo metadata.MetadataMap) {
metadataStruct := conversation.ConversationRequest{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.StateStoreType)
return
}
// Converse returns inputs directly.
func (e *Echo) Converse(ctx context.Context, r *conversation.ConversationRequest) (res *conversation.ConversationResponse, err error) {
outputs := make([]conversation.ConversationResult, 0, len(r.Inputs))
for _, input := range r.Inputs {
outputs = append(outputs, conversation.ConversationResult{
Result: input.Message,
Parameters: r.Parameters,
})
}
res = &conversation.ConversationResponse{
Outputs: outputs,
}
return res, nil
}
func (e *Echo) Close() error {
return nil
}