Repo: https://github.com/slingr-stack/ai-studio-package
This package integrates with the AI Studio API, allowing you to interact with its features programmatically. It provides convenient methods for making authenticated requests to the AI Studio API and handling webhook events.
The AI Studio package has the following features:
- Authentication using API Token or Email/Password credentials.
- Helper functions to simplify interaction with the AI Studio API.
- Webhook support for receiving real-time updates.
You'll need an AI Studio account to use this package. Configure the package with the appropriate settings described below.
Name: authenticationMethod
Choose how you want to authenticate with the AI Studio API:
- API Token: Use a predefined API token for authentication.
- Credentials: Use your AI Studio email and password to log in. The package will automatically refresh the token when it expires.
Name: apiToken
(Required if authenticationMethod is "API Token") Enter your AI Studio API token. Keep this secure.
Name: email
(Required if authenticationMethod is "Credentials") Your AI Studio email address.
Name: password
(Required if authenticationMethod is "Credentials") Your AI Studio password.
Name: aiStudioBaseUrl
The base URL for the AI Studio API. Defaults to https://aistudio.slingrs.io/prod/runtime/api. Normally, you don't need to modify this setting.
Name: webhookUrl
This is the URL in your Slingr app that AI Studio will send webhooks to. Configure this URL in your AI Studio settings.
Name: checkWebhooksSignature
Enable or disable webhook signature verification. It is recommended to enable this for security.
Name: webhooksSigningSecret
(Required if checkWebhooksSignature is enabled) The shared secret used to sign webhooks. This must match the secret configured in your AI Studio webhook settings.
You can use the following methods to make requests to the AI Studio API:
pkg.aistudio.api.get('/tasks'); // GET request
pkg.aistudio.api.post('/projects', { name: 'My Project' }); // POST request
pkg.aistudio.api.put('/projects/123/action', { name: 'New name' }); // PUT request
pkg.aistudio.api.delete('/projects/123'); // DELETE requestThe package automatically handles authentication and includes the necessary headers.
For more details about making HTTP requests, refer to the HTTP service documentation.
You can easily create a task like this:
let taskId = pkg.aistudio.tasks.execute(projectCode, agentCode, inputs, callbackData, callback, errorCallback); Here, inputs is a map with the inputs needed by the agent. If the input is a file, you need to pass the file ID.
Additionally, you can pass a callback that will be called when the task is ready in an async way:
pkg.aistudio.tasks.execute(projectCode, agentCode, inputs, {}, function(taskId, response, callbackData) {
// do something with the response
}, function(taskId, errors, callbackData) {
// handle error
});Keep in mind that the callback is called async and the context is lost, so any information has to be send through the callback data parameter. By default, we will wait 10 minutes for the response to arrive, otherwise the callback will be removed. You can change the timeout by passing the timeout property insde callbackData.
You can add a new message to a task like this:
pkg.aistudio.tasks.chat(taskId, null, 'The total in the answer does not match. Can you review it?');You can then wait for a response for the task or you can use a callback as well:
pkg.aistudio.tasks.chat(taskId, null, 'The total in the answer does not match. Can you review it?', null, function(taslId, response) {
// do something with the response
});Keep in mind that the callback is called async and the context is lost. By default, we will wait 10 minutes for the response to arrive, otherwise the callback will be removed. You can change the timeout by passing the timeout property insde callbackData.
It is possible to wait for a task to ready like this:
let taskId = pkg.aistudio.tasks.execute(projectCode, agentCode, inputs);
let response = pkg.aistudio.tasks.waitToBeReady(taskId);
log(response);By default, it will wait up to 5 minutes to find the response, but you can change that default using the second param:
let taskId = pkg.aistudio.tasks.execute(projectCode, agentCode, inputs);
let response = pkg.aistudio.tasks.waitToBeReady(taskId, 1000 * 60 * 10);
log(response);If there is an error executing the task, an exception will be thrown when waiting for the task to be ready. Be prepared to handle it.
This event is triggered when AI Studio sends a webhook to your configured URL in the project. The event data contains the raw payload sent by AI Studio.
Example:
sys.logs.info('Webhook received:', event.data.type);As you can see, the type field will indicate the type of event. Look below for the types available.
Type: taskReady
When a task is ready, a webhook is sent with the following information:
sys.logs.info(`Task ID: ${event.data.taskId}`);
sys.logs.info(`Task ID: ${event.data.status}`); // if there is an error, this will be 'error'
sys.logs.info(`Callback executed: ${event.data.callbackExecuted}`);
sys.logs.info(`Response: ${event.data.response}`);
sys.logs.info(`Errors: ${JSON.stringify(event.data.errors)}`); // if status was 'error', you'll find an array of strings with the errorsSlingr is a low-code rapid application development platform that accelerates development, with robust architecture for integrations and executing custom workflows and automation.
This package is licensed under the Apache License 2.0. See the LICENSE file for more details.