Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/update playbook mask #201

Merged
merged 2 commits into from
Jul 10, 2024
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
65 changes: 43 additions & 22 deletions src/dfcx_scrapi/core/playbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,40 @@ def __init__(
@staticmethod
def build_instructions(
instructions: List[str]) -> List[types.Playbook.Step]:
"""Helper method to create the playbook instruction set protos."""
final_instructions = types.Playbook.Instruction(steps=[])

if not isinstance(instructions, list):
raise TypeError(
"Instructions must be provided as a List of strings.")

all_steps = []
for instruction in instructions:
all_steps.append(types.Playbook.Step(text=instruction))
else:
for instruction in instructions:
final_instructions.steps.append(
types.Playbook.Step(text=instruction)
)

return final_instructions

def process_playbook_kwargs(
self,
playbook: types.Playbook,
**kwargs):
"""Process incoming kwargs and create the proper update mask."""
paths = []
for key, value in kwargs.items():
if key in ["instruction", "instructions"]:
instructions = self.build_instructions(value)
setattr(playbook, "instruction", instructions)
paths.append("instruction")
else:
setattr(playbook, key, value)
paths.append(key)

# paths = kwargs.keys()
mask = field_mask_pb2.FieldMask(paths=paths)

return all_steps
return playbook, mask

def set_default_playbook(self, playbook_id: str):
"""Sets the default Playbook for the Agent."""
Expand Down Expand Up @@ -164,7 +189,6 @@ def create_playbook(
self,
agent_id: str,
obj: types.Playbook = None,
instructions: List[str] = None,
**kwargs,
):
"""Create a Dialogflow CX Playbook with given display name.
Expand All @@ -184,34 +208,34 @@ def create_playbook(
request.parent = agent_id

if obj:
playbook_obj = obj
playbook_obj.name = ""
playbook = obj
playbook.name = ""
else:
playbook_obj = types.Playbook()
playbook = types.Playbook()

# set optional args as playbook attributes
for key, value in kwargs.items():
if key == "instructions":
instructions = self.build_instructions(instructions)
setattr(playbook_obj, "instructions", instructions)
else:
setattr(playbook_obj, key, value)

request.playbook = playbook_obj
playbook, _ = self.process_playbook_kwargs(playbook, **kwargs)

request.playbook = playbook
response = self.playbooks_client.create_playbook(request)

return response

@scrapi_base.api_call_counter_decorator
def update_playbook(
self, playbook_id: str, obj: types.Playbook = None, **kwargs
self,
playbook_id: str,
obj: types.Playbook = None,
**kwargs
) -> types.Playbook:
"""Update a single specific CX Playbook object.

Args:
playbook_id: CX Playbook ID in the proper format
obj: (Optional) a single CX Playbook object of types.Playbook
overwrite_instructions: if True this will overwrite all instructions
for the specific playbook. By default this is set to False and will
append new instructions.

Returns:
A copy of the updated Playbook object
Expand All @@ -223,11 +247,8 @@ def update_playbook(
else:
playbook = self.get_playbook(playbook_id)

# set playbook attributes to args
for key, value in kwargs.items():
setattr(playbook, key, value)
paths = kwargs.keys()
mask = field_mask_pb2.FieldMask(paths=paths)
# set optional args as playbook attributes
playbook, mask = self.process_playbook_kwargs(playbook, **kwargs)

response = self.playbooks_client.update_playbook(
playbook=playbook, update_mask=mask)
Expand Down
4 changes: 2 additions & 2 deletions tests/dfcx_scrapi/core/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import pytest
from unittest.mock import patch
from dfcx_scrapi.core.tools import Tools
from google.cloud.dialogflow_v3alpha1 import types
from google.cloud.dialogflow_v3alpha1 import services
from google.cloud.dialogflowcx_v3beta1 import types
from google.cloud.dialogflowcx_v3beta1 import services

@pytest.fixture
def test_config():
Expand Down
Loading