Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func doRequest(app api.AppType, instruction string) {
handleError(app, err)
}
for {
var req *api.Request
switch resp.GetCode() {
case api.ResponseCode_FINISHED:
color.Green("Answer: %s", resp.GetChatResponse().GetMessage())
Expand All @@ -138,7 +139,7 @@ func doRequest(app api.AppType, instruction string) {
fallthrough
case api.ResponseCode_SUCCESS:
rid := uuid.New().String()
resp, err = cli.Chat(context.Background(), &api.Request{
req = &api.Request{
Code: api.RequestCode_FETCH,
RequestId: rid,
ThreadId: resp.ThreadId,
Expand All @@ -148,35 +149,47 @@ func doRequest(app api.AppType, instruction string) {
Instruction: instruction,
},
},
})
if err != nil {
handleError(app, err)
}
case api.ResponseCode_ACTION_REQUIRED:
ar := resp.GetActionResponse()

arr := &api.ActionResultRequest{
ActionId: ar.GetActionId(),
}
switch ar.GetType() {
case api.ActionType_INFORMATION:
fmt.Printf("Action Required: Need more information.\n\n")
fmt.Printf("Request for: %s\n", ar.Message)

reader := bufio.NewReader(os.Stdin)
fmt.Printf("Action Required: %s\n", ar.Message)
arr.ActionResult, _ = reader.ReadString('\n')
if err != nil {
handleError(app, err)
}
case api.ActionType_CONFIRMATION:
println("Action SAFEGUARD")
fmt.Printf("Action Required: Human Approve Action.\n\n")
fmt.Printf("Request for: %s\n\n", ar.Message)
fmt.Printf("Do you approve this action? [y/n]: ")

reader := bufio.NewReader(os.Stdin)
result, _ := reader.ReadString('\n')
if result == "y\n" {
arr.ActionResult = "approved"
} else {
arr.ActionResult = "denied"
}
req = &api.Request{
Code: api.RequestCode_ACTION_RESULT,
RequestId: uuid.New().String(),
ThreadId: resp.ThreadId,
Request: &api.Request_ActionResultRequest{
ActionResultRequest: arr,
},
}
}
resp, err = cli.Chat(context.Background(), &api.Request{
Code: api.RequestCode_ACTION_RESULT,
RequestId: uuid.New().String(),
ThreadId: resp.ThreadId,
Request: &api.Request_ActionResultRequest{
ActionResultRequest: arr,
},
})
}
resp, err = cli.Chat(context.Background(), req)
if err != nil {
handleError(app, err)
}
}

Expand Down
3 changes: 3 additions & 0 deletions cli/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (c *CMDConfig) GetHTTPEndpoint() string {
if c.httpPort == 0 {
c.httpPort = 9141
}
if c.NPIServer == "" {
c.NPIServer = "127.0.0.1"
}
return fmt.Sprintf("%s:%d", c.NPIServer, c.httpPort)
}

Expand Down
1 change: 1 addition & 0 deletions cli/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ func readConfig() {
// os.Exit(-1)
//}
httpClient = resty.New().SetBaseURL("http://" + cfg.GetHTTPEndpoint())
httpClient.SetTimeout(5 * time.Second)
httpClient.SetHeader("Authorization", cfg.APIKey)
}
4 changes: 3 additions & 1 deletion npi/core/callback/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, result: api_pb2.ActionResultRequest):
self.result = result

def is_approved(self):
pass
return self.result.action_result == "approved"

def has_message(self):
pass
Expand All @@ -24,6 +24,8 @@ def __init__(self, msg: str = None, action: api_pb2.ActionRequiredResponse = Non
self.action = action
if action:
self.__type = api_pb2.ResponseCode.ACTION_REQUIRED
else:
self.__type = api_pb2.ResponseCode.MESSAGE
self.__id = str(uuid.uuid4())
self.__future = asyncio.get_event_loop().create_future()

Expand Down
4 changes: 2 additions & 2 deletions npi/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ async def Chat(
thread = self.thread_manager.new_thread(request.chat_request)
response.thread_id = thread.id
response.code = api_pb2.ResponseCode.SUCCESS
# create background task
await asyncio.create_task(self.run(thread))
# ignore task result
_ = asyncio.create_task(self.run(thread))
except Exception as err:
err_msg = ''.join(traceback.format_exception(err))
print(err_msg)
Expand Down