<commit_before>package endly

import (
	"errors"
	"fmt"
	"github.com/viant/toolbox/url"
)

var sdkNotFound = errors.New("SDK NOT FUND")

//SdkServiceID represents system sdk
const SdkServiceID = "sdk"

type systemSdkService struct {
	*AbstractService
	jdkService *systemJdkService
	goService  *systemGoService
}

func (s *systemSdkService) updateSessionSdk(context *Context, target *url.Resource, sdkInfo *SystemSdkInfo) error {
	if sdkInfo == nil {
		return nil
	}
	session := context.TerminalSession(target)
	if session == nil {
		return fmt.Errorf("Failed to lookup session %v\n", target.Host())
	}
	session.Mutex.Lock()
	defer session.Mutex.Unlock()
	session.Sdk[sdkInfo.Sdk] = sdkInfo
	return nil
}

func (s *systemSdkService) deploySdk(context *Context, request *SystemSdkSetRequest) error {
	target, err := context.ExpandResource(request.Target)
	if err != nil {
		return nil
	}
	deploymentService, err := context.Service(DeploymentServiceID)
	if err != nil {
		return err
	}
	serviceResponse := deploymentService.Run(context, &DeploymentDeployRequest{
		Target:target,
		AppName:request.Sdk,
		Version: request.Version,
	})
	if serviceResponse.Error != "" {
		return fmt.Errorf("Failed to deploy sdk: %v %v, %v", request.Sdk, request.Version, serviceResponse.Error)
	}
	return nil
}

func (s *systemSdkService) Run(context *Context, request interface{}) *ServiceResponse {
	startEvent := s.Begin(context, request, Pairs("request", request))
	var response = &ServiceResponse{Status: "ok"}
	defer s.End(context)(startEvent, Pairs("response", response))

	var err error
	switch actualRequest := request.(type) {
	case *SystemSdkSetRequest:
		response.Response, err = s.setSdk(context, actualRequest)
		if err == sdkNotFound {
			err = s.deploySdk(context, actualRequest)
			if err == nil {
				response.Response, err = s.setSdk(context, actualRequest)
			}
		}
		if err != nil {
			response.Error = fmt.Sprintf("Failed to run sdk: %v, %v", actualRequest.Sdk, err)
		}
	default:
		response.Error = fmt.Sprintf("Unsupported request type: %T", request)
	}
	if response.Error != "" {
		response.Status = "error"
	}
	return response
}

func (s *systemSdkService) NewRequest(action string) (interface{}, error) {
	switch action {
	case "set":
		return &SystemSdkSetRequest{}, nil

	}
	return s.AbstractService.NewRequest(action)

}


func (s *systemSdkService) checkSdkOnSession(context *Context, target *url.Resource, request *SystemSdkSetRequest, response *SystemSdkSetResponse) bool {
	session := context.TerminalSession(target)
	if session == nil {
		return false
	}
	session.Mutex.RLock()
	defer session.Mutex.RUnlock()
	sdkInfo, has := session.Sdk[request.Sdk]
	if ! has {
		return false
	}
	if sdkInfo.Version == "" && request.Version == "" {<commit_msg>updated system session<commit_after>package endly

import (
	"errors"
	"fmt"
	"github.com/viant/toolbox/url"
)

var sdkNotFound = errors.New("SDK NOT FUND")

//SdkServiceID represents system sdk
const SdkServiceID = "sdk"

type systemSdkService struct {
	*AbstractService
	jdkService *systemJdkService
	goService  *systemGoService
}

func (s *systemSdkService) updateSessionSdk(context *Context, target *url.Resource, sdkInfo *SystemSdkInfo) error {
	if sdkInfo == nil {
		return nil
	}
	session, err := context.TerminalSession(target)
	if err != nil {
		return err
	}
	session.Mutex.Lock()
	defer session.Mutex.Unlock()
	session.Sdk[sdkInfo.Sdk] = sdkInfo
	return nil
}

func (s *systemSdkService) deploySdk(context *Context, request *SystemSdkSetRequest) error {
	target, err := context.ExpandResource(request.Target)
	if err != nil {
		return nil
	}
	deploymentService, err := context.Service(DeploymentServiceID)
	if err != nil {
		return err
	}
	serviceResponse := deploymentService.Run(context, &DeploymentDeployRequest{
		Target:target,
		AppName:request.Sdk,
		Version: request.Version,
	})
	if serviceResponse.Error != "" {
		return fmt.Errorf("Failed to deploy sdk: %v %v, %v", request.Sdk, request.Version, serviceResponse.Error)
	}
	return nil
}

func (s *systemSdkService) Run(context *Context, request interface{}) *ServiceResponse {
	startEvent := s.Begin(context, request, Pairs("request", request))
	var response = &ServiceResponse{Status: "ok"}
	defer s.End(context)(startEvent, Pairs("response", response))

	var err error
	switch actualRequest := request.(type) {
	case *SystemSdkSetRequest:
		response.Response, err = s.setSdk(context, actualRequest)
		if err == sdkNotFound {
			err = s.deploySdk(context, actualRequest)
			if err == nil {
				response.Response, err = s.setSdk(context, actualRequest)
			}
		}
		if err != nil {
			response.Error = fmt.Sprintf("Failed to run sdk: %v, %v", actualRequest.Sdk, err)
		}
	default:
		response.Error = fmt.Sprintf("Unsupported request type: %T", request)
	}
	if response.Error != "" {
		response.Status = "error"
	}
	return response
}

func (s *systemSdkService) NewRequest(action string) (interface{}, error) {
	switch action {
	case "set":
		return &SystemSdkSetRequest{}, nil

	}
	return s.AbstractService.NewRequest(action)

}


func (s *systemSdkService) checkSdkOnSession(context *Context, target *url.Resource, request *SystemSdkSetRequest, response *SystemSdkSetResponse) bool {
	session, err := context.TerminalSession(target)
	if err != nil {
		return false
	}
	session.Mutex.RLock()
	defer session.Mutex.RUnlock()
	sdkInfo, has := session.Sdk[request.Sdk]
	if ! has {
		return false
	}
	if sdkInfo.Version == "" && request.Version == "" {
{'fraction_non_alphanumeric': 0.1258217677136596, 'fraction_numerical': 0.0, 'mean_word_length': 4.808059384941675, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3165557', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1822, 'n_words': 449}