Code Assistant
Overview#
The Code Assistant Tool is an AI System Tool which helps generate or update JavaScript code using the Platform's JavaScript libraries.
How to use the Code Assistant Tool#
To use the Code Assistant Tool, follow these four steps:
Refer to the sections below for a description of each step.
Create an agent#
The first step in using the CodeAssistTool is to create an agent using IafAISvc.createAgents.
Refer to the code below.
[ { "_name": "SP - Code Assistant Agent", "_background": "Assistant to help generate or update JavaScript code using the Twinit JavaScript Libraries", "_userType": "CodeAssistAgent", "_namespaces": [ "{{nsfilter}}" ], "_config": { "_model": "gpt-4o", "_provider": "openai" }, "_tools": [ "CodeAssistTool" ] }]
Create a team#
The second step is to create a team which extends the agent you have created in step 1 and then defines the flow path which the agents should follow. This is done using IafAISvc.createTeam.
Refer to the code below:
{ "_name": "SP - Code Assistant Team", "_namespaces":["{{nsfilter}}"], "_agents": [ { "_userType": "CodeAssistAgent" } ], "_flow": [ { "from": "__start__", "to": "CodeAssistAgent" }, { "from": "CodeAssistAgent", "to": "__end__" } ]}Initiate conversation with the team#
The third step is to initiate a conversation with the team using IafAISvc.createConversation.
Refer to the code below.
{ "_input": { "message": "Add a function that returns a list of NamedUserCollections from Item Service" }, "_teamId": "8758fecb-82ee-49d2-a996-ce79eafcf67d"}Sample Response#
{
"_output": {
"code": "let scriptModule = {
async getNamedUserCollections(input, libraries, ctx) {
const { IafItemSvc } = libraries.PlatformApi;
// Define the query criteria to fetch NamedUserCollections
const criteria = {query: {}};
// Fetch all NamedUserCollections using the getAllNamedUserItems function\n
const namedUserCollections = await IafItemSvc.getAllNamedUserItems(criteria, ctx);
// Return the list of NamedUserCollections
return namedUserCollections;
}
}
export default scriptModule",
"explanation": "### Explanation of the Code\n\nThe provided JavaScript code defines an asynchronous function `getNamedUserCollections` that retrieves a list of NamedUserCollections from the Item Service. Here's a detailed breakdown of the code:\n\n1. **Function Definition**: The function `getNamedUserCollections` is defined as an asynchronous function, which means it can perform asynchronous operations using the `await` keyword.\n\n2. **Parameters**: The function takes three parameters:\n - `input`: This parameter is not used in the function but is included to match the required function signature.\n - `libraries`: This parameter is used to access the `PlatformApi` object, which provides access to the `IafItemSvc` class.\n - `ctx`: This parameter represents the user's context, which includes information such as authorization tokens and namespaces.\n\n3. **Accessing IafItemSvc**: The `IafItemSvc` class is extracted from the `libraries.PlatformApi` object. This class provides methods to interact with the Item Service.\n\n4. **Defining Query Criteria**: A `criteria` object is defined with a `query` property. The query specifies that we want to fetch items with the `_itemClass` set to `'NamedUserCollection'`. This ensures that only NamedUserCollections are retrieved.\n\n5. **Fetching NamedUserCollections**: The `getAllNamedUserItems` method of the `IafItemSvc` class is called with the `criteria` object and the `ctx` context. This method returns a promise that resolves to a list of NamedUserCollections.\n\n6. **Returning the Result**: The function returns the result of the `getAllNamedUserItems` call, which is a list of NamedUserCollections.\n\nThis function provides a straightforward way to retrieve all NamedUserCollections managed by the Item Service, leveraging the platform's API capabilities."
}
Continue the conversation#
If the code is not what you expected or there is an error, use IafAISvc.createConversation to pass an updated prompt or the error into the conversation to get updated results.
Note: Due to the non-deterministic nature of LLMs the generated code will not always be 100 per cent accurate. Review the code that has been returned to ensure it is doing what you want. It is also good practice to ensure that your prompt is as detailed as possible.