# Autessa CDK

Source: https://docs.autessa.com/docs/cdk

> Define Autessa tables, tools, agents, apps, phone numbers and permissions in Python, TypeScript or Java, then preview and deploy them to one or many tenants.

# Autessa CDK

(Preview)

The deploy API and the Autessa CDK are in preview and not yet released. Names and shapes may change before they are generally available. Projects with React apps can't deploy yet: app files are refused at Autessa's network edge until that is opened for the deploy API.

The Autessa CDK keeps your Autessa setup in a Git repo. Describe tables, tools, agents, apps, phone numbers and permissions in code. Preview every change, then deploy it to one tenant or many.

## When to use it

- You want changes reviewed in pull requests, not clicked in a UI.
- You run separate dev and prod tenants and want them to match.
- You ship the same setup to many customer tenants.
- You want to see when a change gives someone more access, before it ships.

## How it works

1. **Write** resources in code, or `pull` them from an existing tenant.
2. **Build** turns your code into a manifest plus files (tool sources, app bundles).
3. **Diff** asks the server what would change in a stage. Changes that widen access are marked `WIDENS`.
4. **Deploy** applies the change. It is idempotent and matched on each resource's `logicalId`.
5. **Rollback** returns a stage to an earlier deployment.

## Get started

- **Try a complete app.** [autessa-cdk-sample](https://github.com/Autessa-Inc/autessa-cdk-sample) is a help desk built with the CDK: tables with row security, a few tools, a Triage agent, a staff desk and a customer portal. Clone it, run it on your machine with `npx autessa-cdk dev` (no account needed), run its tests, then deploy it to your own tenant.
- **Start with an agent.** [Build an agent in code](/docs/cdk/agents.md) walks through `init agent`, `test`, `chat` and `deploy`.

The same commands run from each language's command line, the Maven and Gradle plugins, the [GitHub Action](/docs/cdk/deploy-action.md) and [Autessa Pipelines](/docs/cdk/pipelines.md).

## Packages

| Language | Package | Run commands with |
| --- | --- | --- |
| Python | `autessa-cdk` (`autessa.cdk`) | `autessa-cdk <command>` |
| TypeScript | `@autessa/cdk` | `npx autessa-cdk <command>` |
| Java | `com.autessa:autessa-cdk` (`com.autessa.cdk`) | The Maven plugin `com.autessa:autessa-cdk-maven-plugin` (`mvn autessa-cdk:<command>`) or the Gradle plugin `com.autessa.cdk` (`gradle autessa<Command>`, for example `autessaDiff`) |

Every language has the same commands: `init`, `test`, `chat`, `dev`, `build`, `diff`, `deploy`, `rollback` and `pull`. The TypeScript CDK also has `adopt-app`. See [Commands](/docs/cdk/commands.md).

The CDK is separate from the feature packages you call from your app (firewall, SecurePass, RAG and the rest). Those come one at a time, or all together as `autessa-sdk` (`@autessa/sdk` on npm). See [Features](/docs/features.md).

## Install

**Python:**

During the preview, install from GitHub (core and agents first):

```bash
pip install "autessa-core @ git+https://github.com/Autessa-Inc/autessa-python#subdirectory=packages/autessa-core"
pip install "autessa-agents @ git+https://github.com/Autessa-Inc/autessa-python#subdirectory=packages/autessa-agents"
pip install "autessa-cdk @ git+https://github.com/Autessa-Inc/autessa-python#subdirectory=packages/autessa-cdk"
```

**TypeScript:**

```bash
npm install --save-dev @autessa/cdk
```

**Java:**

```xml
<dependencies>
  <dependency>
    <groupId>com.autessa</groupId>
    <artifactId>autessa-cdk</artifactId>
    <version>0.1.0-SNAPSHOT</version>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>com.autessa</groupId>
      <artifactId>autessa-cdk-maven-plugin</artifactId>
      <version>0.1.0-SNAPSHOT</version>
    </plugin>
  </plugins>
</build>
```

For Gradle, apply `id("com.autessa.cdk") version "0.1.0-SNAPSHOT"` and add `implementation("com.autessa:autessa-cdk:0.1.0-SNAPSHOT")`.

## A small project

One table, one tool, one agent that uses the tool. The model is a stage variable, so dev and prod can use different values.

**Python:**

```python
# autessa.config.py
from autessa.cdk import Agent, Column, Project, Table, Tool

project = Project("support-desk")
model = project.variable("model", default="small")

tickets = Table(project, "tickets",
    columns=[
        Column("subject", "SHORT_TEXT"),
        Column("body", "LONG_TEXT"),
        Column("status", "SINGLE_SELECT", options=["open", "closed"]),
    ],
    vectorize=["body"])

lookup = Tool(project, "lookupTicket", source="tools/lookup_ticket.as")

Agent(project, "supportAgent",
    instructions="You answer questions about support tickets.",
    tools=[lookup],
    table=tickets,
    model=model)
```

**TypeScript:**

```typescript
// autessa.config.ts
import { Project, Table, Tool, Agent } from "@autessa/cdk";

const project = new Project({ name: "support-desk" });
const model = project.variable("model", { default: "small" });

const tickets = new Table(project, "tickets", {
  columns: [
    { name: "subject", type: "SHORT_TEXT" },
    { name: "body", type: "LONG_TEXT" },
    { name: "status", type: "SINGLE_SELECT", options: ["open", "closed"] },
  ],
  vectorize: ["body"],
});

const lookup = new Tool(project, "lookupTicket", { source: "tools/lookup_ticket.as" });

new Agent(project, "supportAgent", {
  instructions: "You answer questions about support tickets.",
  model: `${model}`,
  tools: [lookup],
  table: tickets,
});

export default project;
```

**Java:**

```java
// src/main/java/com/example/SupportDesk.java
package com.example;

import com.autessa.cdk.*;

public class SupportDesk implements AutessaProject {
  @Override
  public Project define() {
    Project p = Project.create("support-desk");
    Variable model = p.variable("model").defaultValue("small");

    Table tickets = p.table("tickets")
        .column("subject", ColumnType.SHORT_TEXT)
        .column("body", ColumnType.LONG_TEXT)
        .select("status", "open", "closed")
        .vectorize("body");

    Tool lookup = p.tool("lookupTicket").source("tools/lookup_ticket.as");

    p.agent("supportAgent")
        .instructions("You answer questions about support tickets.")
        .model(model.ref())
        .tools(lookup)
        .knowledge(tickets);

    return p;
  }
}
```

List the class in `src/main/resources/META-INF/services/com.autessa.cdk.AutessaProject` (one line: `com.example.SupportDesk`), or name it with `-Dautessa.projectClass`.

The ID you give each resource (`tickets`, `lookupTicket`, `supportAgent`) is its `logicalId`. Keep it stable. Renaming it means delete and create.

## Project layout

```text
autessa.config.py | autessa.config.ts | a Java AutessaProject class | autessa.yaml   # your resources
autessa.stages.yaml                           # the tenants you deploy to
tools/*.as                                    # AutessaScript tool sources
types/*.as                                    # shared AutessaScript types
apps/<name>/                                  # React app sources
.autessa/                                     # build output (add to .gitignore)
```

See [Project files](/docs/cdk/project.md) for every file.

## Deploy keys

The CDK uses a **deploy key**, not a normal API key. See [Keys](/docs/start/keys.md) for which key each package needs.

- A deploy key belongs to one company (tenant).
- It carries scopes: `DEPLOY_DIFF`, `DEPLOY_PULL`, `DEPLOY_APPLY`, `DEPLOY_ROLLBACK`, `MODEL_TURN` for [chatting with a project's agents](/docs/cdk/agents.md#chat-with-it-on-your-machine) on your machine, and `HARNESS_CHAT` for [chatting with them in the stage's tenant](/docs/cdk/agents.md#chat-with-it-in-a-stages-tenant).
- Company admins create, rotate and revoke deploy keys, in Autessa under **Settings → Deploy keys** or from the terminal. In Autessa, `DEPLOY_DIFF` is called **Preview changes**, `DEPLOY_APPLY` **Deploy**, `DEPLOY_PULL` **Download**, `DEPLOY_ROLLBACK` **Roll back**, `MODEL_TURN` **Run agents locally** and `HARNESS_CHAT` **Chat with agents from your machine**.
- Keys are read from environment variables, never from project files.
- Give CI only the scopes it needs. A pull-request job needs `DEPLOY_DIFF` only.

From the terminal, signed in as a company admin:

```bash
autessa deploy-keys create --name "CI main" --scopes DEPLOY_DIFF,DEPLOY_APPLY --stageName production
autessa deploy-keys list
autessa deploy-keys rotate --keyId <keyId>
autessa deploy-keys revoke --keyId <keyId>
```

`create` and `rotate` print the secret once, so save it straight into your CI's secret store. Scripts, agents and apps can't run these commands. See [API access](/docs/cli/api-access.md#deploy-keys) for who can.

## API reference

[Deploy API](/docs/reference/deploy.md) and [Pipelines API](/docs/reference/pipelines.md).

## Examples

- [autessa-cdk-sample](https://github.com/Autessa-Inc/autessa-cdk-sample): a complete help desk app built with the CDK, ready to run locally and deploy to your tenant.
- [autessa-as-code-tenant](https://github.com/Autessa-Inc/autessa-examples/tree/main/examples/autessa-as-code-tenant): a whole tenant in TypeScript and Java, with a stages file and a GitHub workflow.

## Related

- [Keys](/docs/start/keys.md)
- [Build an agent in code](/docs/cdk/agents.md)
- [Project files](/docs/cdk/project.md)
- [Commands](/docs/cdk/commands.md)
- [Multiple tenants](/docs/cdk/tenants.md)
- [Permissions in code](/docs/cdk/permissions.md)
- [Deploy action](/docs/cdk/deploy-action.md)
- [Autessa Pipelines](/docs/cdk/pipelines.md)
- [Marketplace bundles](/docs/guide/marketplace/bundles.md)

