Data corpuses
Static corpus
Defines a corpus of static documents to be processed and indexed. For details, see Static corpus documentation.
Syntax
corpus({ [title,] urls [, auth] [, include] [, exclude] [, depth] [, maxPages] [, query] [, transforms] [, priority] })
corpus({ [title,] text [, query] [, transforms] [, priority] })
Parameters
| Name | Type | Required/Optional | Description |
|---|---|---|---|
| title | string | Optional | Corpus title. |
| urls | string array | Required | List of URLs from which information must be retrieved. You can define URLs of website folders and pages. |
| auth | JSON object | Optional | Credentials to access resources that require basic authentication. |
| include | string array | Optional | Resources to be obligatory indexed. You can define an array of URLs or use RegEx to specify a rule. |
| exclude | string array | Optional | Resources to be excluded from indexing. You can define an array of URLs or use RegEx to specify a rule. |
| query | function | Optional | Transforms function used to process user queries. |
| transforms | function | Optional | Transforms function used to format the corpus output. |
| depth | integer | Optional | Crawl depth for web and PDF resources. The minimum value is 0. |
| maxPages | integer | Optional | Maximum number of pages and files to index. If not set, only 1 page with the defined URL will be indexed. |
| priority | integer | Optional | Priority level assigned to the corpus. Corpuses with higher priority are considered more relevant. |
Example
corpus({
title: `HTTP corpus`,
urls: [
`https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview`,
`https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages`,
`https://developer.mozilla.org/en-US/docs/Web/HTTP/Session`
],
auth: {username: 'johnsmith', password: 'password'},
include: [/.*\.pdf/],
exclude: [`https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Evolution_of_HTTP`],
query: transforms.queries,
transforms: transforms.answers,
depth: 1,
maxPages: 5,
priority: 0,
});Dynamic corpus
Defines a dynamic corpus of data to be processed. The dynamic corpus allows retrieving JSON data from external data sources and using it to answer user queries in natural language.
Syntax
corpus({ [title,] [input,] query [, output] [, transforms] [, priority] })
Parameters
| Name | Type | Required/Optional | Description |
|---|---|---|---|
| title | string | Optional | Corpus title. |
| input | function | Optional | Function used to populate the Input field of the query transform. |
| query | function | Required | Transforms function used to process user queries and generate code to retrieve necessary data. |
| output | function | Optional | Function used to process obtained data, before it is passed to the transforms function. |
| transforms | function | Optional | Transforms function used to process and format data obtained with the query transform. |
| priority | integer | Optional | Priority level assigned to the corpus. Corpuses with higher priority are considered more relevant. |
Example
corpus({
title: `Infrastructure requests`,
input: project.objects,
query: transforms.vms_queries,
output: project.cleanObjects,
transforms: transforms.vms_answer,
priority: 1
});Corpus with the Puppeteer crawler
Defines a corpus of pages with complex structures or interactions. The corpus with the Puppeteer crawler allows crawling specific page content and dynamically loaded resources.
Syntax
corpus({ [title,] urls, crawler [, auth] [, include] [, exclude] [, depth] [, maxPages] [, query] [, transforms] [, priority] })
Example
corpus({
title: `Slack docs`,
urls: [
`https://slack.com/help/articles/360017938993-What-is-a-channel`,
`https://slack.com/help/articles/205239967-Join-a-channel`,
`https://slack.com/help/articles/201402297-Create-a-channel`,
],
crawler: {
puppeteer: api.defaultCrawler({
waitAfterLoad: 1000,
excludeSelectors: [
`header.header`,
`section.banner`,
`.hidden`,
`.category_list`,
`.article_footer`,
`footer.c-nav--expanded-footer`,
`#onetrust-consent-sdk`
]
}),
},
auth: {username: 'johnsmith', password: 'password'},
include: [/.*\.pdf/],
exclude: [/.*\.zip/],
query: transforms.queries,
transforms: transforms.answers,
depth: 3,
maxPages: 3,
priority: 1
});API Methods
api.createApiStubs
Generates API stub code from one or more OpenAPI specifications using the provided configuration options.
Syntax
api.createApiStubs({ callFun, callFunScript, outScript [, fileUrls] [, postmanIds] [, specUrls] [, convertOperationIdToCamelCase] [, removeDeprecated] [, disableSSL] })
Parameters
| Name | Type | Required/Optional | Description |
|---|---|---|---|
| callFun | string | Required | The name of the function to be used in the generated code. |
| callFunScript | string | Required | The helper script file that contains the API function. |
| outScript | string | Required | The file name where the generated code will be saved. |
| fileUrls | string array | Optional | An array of URLs pointing to OpenAPI specification files. |
| convertOperationIdToCamelCase | boolean | Optional | Converts operation IDs to camelCase in the generated code. (default: true) |
api.sleep
Pauses the execution of the current script for the specified number of milliseconds.
Syntax
api.sleep(time)
Parameters
| Name | Type | Description |
|---|---|---|
| time | integer | Duration for which the script should be paused in milliseconds. |
api.html2md_v2
Converts the HTML content into Markdown format, preserving the structure of the original HTML.
Syntax
api.html2md_v2(html [, url])
Parameters
| Name | Type | Description |
|---|---|---|
| html | HTML string | A string of HTML content to be converted to Markdown. |
| url | URL | The URL of a crawled page. The function uses the URL to replace relative links with the full domain value. |
Transforms
Pre-processes and formats input data according to the defined template.
Syntax
query: transforms.transformName
transform: transforms.transformName
Example
corpus({
title: `Infrastructure requests`,
query: transforms.infrastructure_queries,
transform: transforms.infrastructure_queries,
priority: 1
});Action Transformer
act
Allows controlling the behavior of the Action Transformer: provide UI context, prioritize data, gather additional information and execute tasks.
Syntax
act({[uiContext,] [context,] [fallback,] [execution,] [merge] })
Parameters
| Name | Type | Required/Optional | Description |
|---|---|---|---|
| uiContext | function | Optional | Function used to retrieve the UI context of the page currently open in the app. |
| context | function | Optional | Function used to gather conversational context to pass it to the Action Transformer. |
| fallback | function | Optional | Transforms function used to handle unclear queries that require additional information. |
| execution | function | Optional | Transforms function used to specify what actions the Agentic Interface must take. |
| merge | function | Optional | Transforms function used to combine data from different corpuses. |
Example
act({
uiContext: getUiContext,
context: getContext,
fallback: transforms.act_fallback,
execution: transforms.act_execute,
merge: transforms.act_merge
})Session-specific objects and methods
Session-specific objects and methods are accessible through the predefined p object. They persist throughout a user session.
userData
A runtime object used to store any relevant data. The data in p.userData is available only for the duration of the user session.
authData
A runtime object used to store static data specific to the device or user, such as user credentials or product version.
visual
A runtime object used to store any arbitrary JSON data. Use it to provide dynamic information about the app's state or visual context to the Agentic Interface.
Global objects and methods
project
A global object used to store data that can be accessed by any dialog scripts in the project.
// Script 1
project.config = {initValue: 1};
// Script 2
console.log(`Init value is ${project.config.initValue}`);project API
Allows sending data from the client app to the dialog script or executing script logic without a user command.
Syntax
projectAPI.functionName = function(p, data, callback) {}
Example
projectAPI.setToken = function(p, param, callback) {
if (!param || !param.token) {
callback("error: token is undefined");
}
p.userData.token = param.token;
callback();
};Predefined callbacks
To perform actions at different stages of the dialog lifecycle, use the following predefined callback functions:
onCreateProject
Invoked when the dialog model for the dialog script is built. Use this function for activities that must be accomplished after the creation of the dialog model.
Syntax
onCreateProject(()=> {action})
Example
onCreateProject(() => {
project.drinks = "green tea, black tea, oolong";
});onCreateUser
Invoked when a new user starts a dialog session. Use this function to set up user-specific data.
Syntax
onCreateUser(p => {action})
Example
onCreateUser(p => {
p.userData.name = "John Smith";
});onUserEvent
Invoked when Alan AI emits an event driven by users' interactions with the Agentic Interface.
Syntax
onUserEvent((p, e) => {action})
Example
onUserEvent((p, e) => {
if (e.event == 'firstClick') {
p.play(`Hi, ${p.userData.name}, how can I help you today?`);
}
});Debugging
console.log
Outputs informational messages to Alan AI Studio logs. Use console.log() for debugging purposes.
Syntax
console.log(message)
Example
try {
console.log("This is a debug message");
}
catch (e) {
console.error(e);
}console.error
Outputs error messages to Alan AI Studio logs. Use it to report errors or exceptions that occur during the dialog script execution.
Syntax
console.error(message)
Example
try {
// your code
}
catch (e) {
console.error(e)
}