Title: Windows Live Agents: Structuring and Refactoring the code
Slug: windows-live-agents-structuring-and-refactoring-the-code
Date: 2008-01-10 13:33:00
Author: Kartones
Lang: en
Tags: Development, Windows Live Agents, Patterns & Practices
Description: A guide on structuring and refactoring code for Windows Live Agents, including organizing patterns, creating localized responses, and using macros for text substitutions.

<p>In order to implement correctly structured code for a Windows Live Agent, we should remember that the <b>/Shared</b> folder exists and what its purpose is. If we want to access an RSS feed and display its contents, the data access code (at least) should be placed inside Shared.</p> <p>Then, we can parse &amp; format the data, adding localized text, displaying it in different formats (for example currency, or datetimes). The following diagram provides an example for a spanish &amp; english agent:</p> <p><a href="https://images.kartones.net/posts/screenshots/building_a_wla_004.png"><img width="500" src="https://images.kartones.net/posts/screenshots/building_a_wla_004.png"></a><br><i><font size="1">Click on the diagram to enlarge it</font></i></p> <p> </p> <p>Apart from that, I prefer refactoring the included patterns for all "ChitChat" (default conversations the agent can understand and handle), because when developing a multilanguage agent is not always easy to find the equivalent in other languages (at least in English&lt;-&gt;Spanish some translations are not equal). Also, sometimes I want to reuse a set of answers for multiple patterns.</p> <p>So what I do is take a pattern like this from <b>_agent_root_/Spanish/Chat</b> which calls a procedure in <b>/BuddyscriptLib/Spanish/Extensions/Chat/Agent_es.pkg</b>:</p> <p><font face="Courier New">{title="¿Quién me hizo?" category="Spanish - About MACRO_AGENT_NAME_ES" contributor="Microsoft" body_type="buddyscript" editable="1"}<br>answers QuiénHizoEsteAgenteInteractivo<br>  call QuiénHizoEsteAgenteInteractivoAnswer()</font>  </p><p> </p> <p>And do this steps:</p> <ul> <li>Create the package <b>_agent_root_/Spanish/Chat/Specific/AgentAndCreator.pkg</b>  </li><li>Add a reference to Agent_es.pkg (to use the existing matching subpatterns)  </li><li>Create inside it a function inside it with the answers:  <p><font face="Courier New">procedure WhoMadeTheAgentSpanishAnswer()<br>  - He sido creado por MACRO_AGENT_CREATORS_ES<br>  call CambiarTema("mis creadores")</font> </p> </li><li>Create the file <b>_agent_root_/Spanish/Chat/Specific/AgentAndCreator.ddl</b>  </li><li>Cut and paste (from <b>Agent_es.pkg</b>) the pattern, using my own response function:  <p><font face="Courier New">{title="¿Quién me hizo?" category="Spanish - About MACRO_AGENT_NAME_ES" contributor="Microsoft" body_type="buddyscript" editable="1"}<br>answers QuiénHizoEsteAgenteInteractivo<br>  call <b>WhoMadeTheAgentSpanishAnswer</b>()</font></p></li></ul> <p>I will keep the same folder &amp; files structure in the <b>_agent_root_/English/</b>, just using english equivalent procedures (but with same notation), like this:</p> <p><font face="Courier New">{title="Who Made Me" category="English - About MACRO_AGENT_NAME" contributor="Microsoft" body_type="buddyscript" editable="1"}<br>+- Who made MACRO_AGENT_NAME?<br>answers WhoMadeThisInteractiveAgent<br>  call <b>WhoMadeTheAgentEnglishAnswer()</b></font>  </p><p> </p> <p>I personally find much better to have extra subfolders organizing the packages (which I try to limit to as few different topics as possible each one), and by using the same xxxxxxxSpanishAnswer() xxxxxxxEnglishAnswer() I can even assign to someone else the task of modifying the pattern an customizing it to my desire).</p> <p> </p> <p>As a final tip/trick, in <b>/Shared/Global.pkg</b> we can find some macros for default text substitutions, like this one:</p> <p><font face="Courier New">macro COMPANY_NAME           Kartones.Net</font></p> <p>And the spanish localized version:</p> <p><font face="Courier New">macro COMPANY_NAME_ES        Kartones.Net</font></p> <p>Then, they can be used to place the right string anywhere: </p> <p><font face="Courier New">procedure GeneralAgentPresentationEnglishAnswer()<br>  - I'm <b>MACRO_AGENT_NAME</b>, your guide to <b>MACRO_COMPANY_NAME</b>.</font>  </p><p>  </p><p>But what if you want to add a new macro? Well... it is so simple! Add it on <b>/Shared/Global.pkg</b> file:  </p><p><font face="Courier New">macro AGENT_CREATORS         Kartones</font>  </p><p>And just call it from a pattern adding <font face="Courier New">MACRO_</font> before it's name: </p><p><font face="Courier New">procedure WhoMadeTheAgentEnglishAnswer()<br>  - I was built by <b>MACRO_AGENT_CREATORS</b>.<br>  call ChangeSubject("my creators")</font></p>