AI Actions & Scripts SDK Reference — ApexKit Docs
AI Actions & Scripts SDK Reference The apex.ai and apex.scripts namespaces provide access to Generative AI features, LLM prompt templates with real-time Serv...
# AI Actions & Scripts SDK Reference
The `apex.ai` and `apex.scripts` namespaces provide access to Generative AI features, LLM prompt templates with real-time Server-Sent Events (SSE) token streaming, and server-side script execution and triggers.
---
## 1. AI Actions (LLM Templates)
Securely execute Generative AI actions built on cloud models (such as Gemini, Groq, and OpenAI) with variables and support for streaming response tokens.
### Method Signatures
- `ai.getActions()`
- `ai.createAction(data)`
- `ai.deleteAction(id)`
- `ai.run(slug, variables, onChunk?)`
- `ai.exportActions()`
- `ai.importActions(file)`
- `ai.getSession()`
- `ai.chat(prompt, model)`
- `ai.applySessionChanges()`
- `ai.listPlugins()`
- `ai.editCode(prompt, currentCode, contextType, model)`
### Running an AI Action with Token Streaming (SSE)
You can optionally pass an `onChunk` callback as the third parameter to receive real-time, streamed response tokens directly as they are generated by the model.
```typescript
const response = await apex.ai.run(
'content-editor',
{
prompt: "Correct grammar and spelling errors.",
originalText: "Here are some txt."
},
(token) => {
// Streamed in real-time chunk by chunk
process.stdout.write(token);
}
);
console.log("\nComplete generated text:", response.result);
console.log("Citations/Metadata:", response.metadata);
```
---
## 2. Architect (Collaborative Development AI Sessions)
In sandbox environments, the **AI Architect** allows developers to collaboratively draft schema tables, server scripts, and template styles using natural language.
### Interactive Chat & Applying Session Changes:
```typescript
// 1. Send instruction to the session
const session = await apex.ai.chat(
'Create a comments system with author references',
'gemini-2.5-flash'
);
console.log(`Changes Proposed: ${session.diff_summary}`);
// 2. Commit proposed schema & code changes to the database
await apex.ai.applySessionChanges();
```
---
## 3. Server-Side Scripts & Custom Logic
Execute custom JavaScript or TypeScript scripts directly within the environment. The server uses the sandboxed, Rust-native QuickJS execution engine for maximum isolation and safety.
### Method Signatures
- `scripts.list()`
- `scripts.create(data)`
- `scripts.delete(id)`
- `scripts.run(name, variables)`
- `scripts.export(format?)`
- `scripts.import(file)`
### Executing a Script with Custom Variables:
```typescript
const result = await apex.scripts.run('process-payment', {
amount: 99.95,
currency: "USD"
});
console.log('Script output:', result);
```
---
## 4. UI Templates
Create and manage custom rendered email and layout templates.
### Method Signatures
- `templates.list()`
- `templates.create(data)`
- `templates.update(id, data)`
- `templates.delete(id)`
- `templates.export(format?)`
- `templates.import(file)`
### Working with Templates:
```typescript
// Register a welcome email template
const template = await apex.templates.create({
CLI / Code Reference
typescript
const response = await apex.ai.run(
'content-editor',
{
prompt: "Correct grammar and spelling errors.",
originalText: "Here are some txt."
},
(token) => {
// Streamed in real-time chunk by chunk
process.stdout.write(token);
}
);
console.log("\nComplete generated text:", response.result);
console.log("Citations/Metadata:", response.metadata);Platform Navigation & Sitemap
Architecture & Engine
Documentation & Ecosystem
Project Activity & Vision
Viewing the pre-rendered indexable snapshot of this resource.
Launch Interactive App in ApexKit Hub →