14. AI Integration
Use CallAIAsync for direct calls to any OpenAI-compatible model, and CallMilevaAsync for the platform's built-in Mileva agent — which has full access to your application data, tools, and context.
CallAIAsync(messages, options)
async· returnsChatCompletion
Sends a conversation to the connected AI service (OpenAI-compatible) and returns the model's response.
Prop
Type
const result = await context.functions.CallAIAsync(
[
{ role: "system", content: "You summarize customer feedback concisely." },
{ role: "user", content: "Summarize: " + context.record.ComplaintText }
],
{ model: "gpt-4o", temperature: 0.3 }
);
const summary = result.Choices[0].Message.Content;
await context.functions.update("complaints", context.record.ID, { AISummary: summary });
Returned value — a ChatCompletion object. Access the generated text at result.Choices[0].Message.Content:
{
"Id": "chatcmpl-abc123",
"Model": "gpt-4o",
"Choices": [
{
"Index": 0,
"Message": {
"Role": "assistant",
"Content": "Customer reports that the product arrived damaged and requests an immediate replacement or full refund."
},
"FinishReason": "stop"
}
],
"Usage": {
"PromptTokens": 87,
"CompletionTokens": 24,
"TotalTokens": 111
}
}
CallAIWithAttachmentAsync(messages, options, attachment)
async· returnsChatCompletion
Like CallAIAsync but also sends a file attachment (PDF, image, document) to the AI model for multimodal analysis.
Prop
Type
const receiptBase64 = await context.functions.generatePdfString({
templateId: "receipt-view",
recordId: context.record.ID
});
const result = await context.functions.CallAIWithAttachmentAsync(
[
{ role: "system", content: "Extract the total amount and vendor name from the receipt." },
{ role: "user", content: "Please process the attached receipt." }
],
{ model: "gpt-4o" },
receiptBase64
);
log(result.Choices[0].Message.Content);
Returned value — same ChatCompletion shape as CallAIAsync:
{
"Id": "chatcmpl-xyz789",
"Model": "gpt-4o",
"Choices": [
{
"Index": 0,
"Message": {
"Role": "assistant",
"Content": "Vendor: Staples d.o.o. | Total: 127.50 EUR | Date: 2026-03-11"
},
"FinishReason": "stop"
}
],
"Usage": {
"PromptTokens": 412,
"CompletionTokens": 19,
"TotalTokens": 431
}
}
CallMilevaAsync(messages, appSlug)
async· returnsstring
Calls the Mileva service — the platform's built-in AI agent framework. Unlike CallAI, Mileva is configured per-application and can access platform data, tools, and context. Returns a plain string.
Prop
Type
const answer = await context.functions.CallMilevaAsync(
[{ role: "user", content: "What is the total revenue for last quarter?" }],
"finance-agent"
);
log("Mileva says: " + answer);
Returned value — a plain string containing the agent's response:
"Total revenue for Q4 2025 was €248,500 across 312 invoices, representing a 14% increase compared to Q3 2025."