Skip to content

Add Tesla auth provider and toolkit #201

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/code/integrations/tesla/config_provider.engine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
auth:
providers:
- id: default-tesla
description: "The default Tesla provider"
enabled: true
type: oauth2
provider_id: tesla
client_id: ${env:TESLA_CLIENT_ID}
client_secret: ${env:TESLA_CLIENT_SECRET}
21 changes: 21 additions & 0 deletions examples/code/integrations/tesla/custom_auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ArcadeClient } from "@arcade-ai/client";

// Initialize the Arcade client
const client = new ArcadeClient();

// Start the authorization flow for Tesla
// This will redirect the user to Tesla's authorization page if needed
const authResult = await client.auth.start({
provider: "tesla",
scopes: ["vehicle_data", "vehicle_cmds"],
});

// Use the token to make API calls to Tesla Fleet API
const token = authResult.token;

// Now you can use the token to make API calls to Tesla Fleet API
// For example:
// const response = await fetch(
// 'https://fleet-api.prd.eu.vn.cloud.tesla.com/api/1/vehicles',
// { headers: { Authorization: `Bearer ${token}` } }
// );
21 changes: 21 additions & 0 deletions examples/code/integrations/tesla/custom_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from arcade import ArcadeClient

# Initialize the Arcade client
client = ArcadeClient()

# Start the authorization flow for Tesla
# This will redirect the user to Tesla's authorization page if needed
auth_result = client.auth.start(
provider="tesla",
scopes=["vehicle_data", "vehicle_cmds"]
)

# Use the token to make API calls to Tesla Fleet API
token = auth_result.token

# Now you can use the token to make API calls to Tesla Fleet API
# For example:
# response = requests.get(
# "https://fleet-api.prd.eu.vn.cloud.tesla.com/api/1/vehicles",
# headers={"Authorization": f"Bearer {token}"}
# )
59 changes: 59 additions & 0 deletions examples/code/integrations/tesla/custom_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from arcade.tools import tool, ToolContext
from arcade.auth import Tesla
import requests
from typing import Dict, Any, List

@tool(
name="get_vehicle_data",
description="Get data about the user's Tesla vehicles",
auth=Tesla(scopes=["vehicle_data"])
)
def get_vehicle_data(context: ToolContext) -> Dict[str, Any]:
"""
Get a list of vehicles associated with the user's Tesla account.

Returns:
A dictionary containing the vehicle data.
"""
# The token is automatically provided by the Arcade Engine
token = context.authorization.token

# Use the token to call the Tesla Fleet API
headers = {"Authorization": f"Bearer {token}"}
response = requests.get("https://fleet-api.prd.eu.vn.cloud.tesla.com/api/1/vehicles", headers=headers)

# Return the vehicle data
return response.json()

@tool(
name="send_vehicle_command",
description="Send a command to a Tesla vehicle",
auth=Tesla(scopes=["vehicle_cmds"])
)
def send_vehicle_command(
context: ToolContext,
vehicle_id: str,
command: str
) -> Dict[str, Any]:
"""
Send a command to a Tesla vehicle.

Args:
vehicle_id: The ID of the vehicle to send the command to.
command: The command to send (e.g., "wake_up", "door_unlock", "climate_on").

Returns:
A dictionary containing the response from the Tesla API.
"""
# The token is automatically provided by the Arcade Engine
token = context.authorization.token

# Use the token to call the Tesla Fleet API
headers = {"Authorization": f"Bearer {token}"}
response = requests.post(
f"https://fleet-api.prd.eu.vn.cloud.tesla.com/api/1/vehicles/{vehicle_id}/command/{command}",
headers=headers
)

# Return the response
return response.json()
7 changes: 7 additions & 0 deletions pages/home/auth-providers/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ Auth providers enable users to seamlessly and securely allow Arcade tools to acc
link="/home/auth-providers/twitch"
category="Auth"
/>
<ToolCard
name="Tesla"
image="tesla"
summary="Authorize tools and agents with Tesla vehicle data and commands"
link="/home/auth-providers/tesla"
category="Auth"
/>
<ToolCard
name="X"
image="twitter"
Expand Down
112 changes: 112 additions & 0 deletions pages/home/auth-providers/tesla.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Tabs } from "nextra/components";

# Tesla auth provider

<Note>
At this time, Arcade does not offer a default Tesla Auth Provider. To use
Tesla auth, you must create a custom Auth Provider with your own Tesla
OAuth 2.0 credentials as described below.
</Note>

The Tesla auth provider enables tools and agents to call the [Tesla Fleet API](https://developer.tesla.com/docs/fleet-api/getting-started/what-is-fleet-api) on behalf of a user. Behind the scenes, the Arcade Engine and the Tesla auth provider seamlessly manage Tesla OAuth 2.0 authorization for your users.

### What's documented here

This page describes how to use and configure Tesla auth with Arcade.

This auth provider is used by:

- Your [app code](#using-tesla-auth-in-app-code) that needs to call Tesla Fleet API
- Or, your [custom tools](#using-tesla-auth-in-custom-tools) that need to call Tesla Fleet API

## Configuring Tesla auth

### Create a Tesla developer account

- Create a Tesla developer account at the [Tesla Developer Portal](https://developer.tesla.com/)
- Register a new application in the developer console
- Set the redirect URL to: `https://cloud.arcade.dev/api/v1/oauth/callback`
- Request the necessary scopes for your application (vehicle data, commands, etc.)
- Copy the Client ID and Client Secret to use below

Next, add the Tesla app to your Arcade Engine configuration. You can do this in the Arcade Dashboard, or by editing the `engine.yaml` file directly (for a self-hosted instance).

### Configuring Tesla auth with the Arcade Dashboard

1. Navigate to the OAuth section of the Arcade Dashboard and click **Add OAuth Provider**.
2. Select **Tesla** as the provider.
3. Choose a unique **ID** for your provider (e.g. "my-tesla-provider") with an optional **Description**.
4. Enter your **Client ID** and **Client Secret** from your Tesla developer account.
5. Click **Save**.

When you use tools that require Tesla auth using your Arcade account credentials, the Arcade Engine will automatically use this Tesla OAuth provider.

### Configuring Tesla auth in self-hosted Arcade Engine configuration

<Steps>

### Set environment variables

Set the following environment variables:

```bash
export TESLA_CLIENT_ID="<your client ID>"
export TESLA_CLIENT_SECRET="<your client secret>"
```

Or, you can set these values in a `.env` file:

```bash
TESLA_CLIENT_ID="<your client ID>"
TESLA_CLIENT_SECRET="<your client secret>"
```

<Tip>
See [Engine configuration](/home/configure/engine) for more information on how
to set environment variables and configure the Arcade Engine.
</Tip>

### Edit the Engine configuration

Edit the `engine.yaml` file and add a `tesla` item to the `auth.providers` section:

```yaml file=<rootDir>/examples/code/integrations/tesla/config_provider.engine.yaml {3-9}

```

</Steps>

## Using Tesla auth in app code

Use the Tesla auth provider in your own agents and AI apps to get a user token for the Tesla Fleet API. See [authorizing agents with Arcade](/home/auth/how-arcade-helps) to understand how this works.

Use `client.auth.start()` to get a user token for the Tesla Fleet API:

<Tabs items={["Python", "JavaScript"]}>
<Tabs.Tab>

```python file=<rootDir>/examples/code/integrations/tesla/custom_auth.py {8-12}

```

</Tabs.Tab>

<Tabs.Tab>

```javascript file=<rootDir>/examples/code/integrations/tesla/custom_auth.js {8-10}

```

</Tabs.Tab>

</Tabs>

## Using Tesla auth in custom tools

The Arcade [Model API](/home/use-tools/call-tools-with-models) is a convenient way to call language models and automatically invoke tools. You can author your own [custom tools](/home/build-tools/create-a-toolkit) that interact with the Tesla Fleet API.

Use the `Tesla()` auth class to specify that a tool requires authorization with Tesla. The `context.authorization.token` field will be automatically populated with the user's Tesla token:

```python file=<rootDir>/examples/code/integrations/tesla/custom_tool.py {5-6,9-13,23}

```
106 changes: 106 additions & 0 deletions pages/toolkits/social-communication/tesla/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: Tesla Toolkit
description: Control and monitor Tesla vehicles with your agents
---

import { Callout } from 'nextra/components'

# Tesla Toolkit

<Callout type="info">
This toolkit requires the [Tesla auth provider](/home/auth-providers/tesla).
</Callout>

The Tesla toolkit allows your agents to interact with Tesla vehicles through the [Tesla Fleet API](https://developer.tesla.com/docs/fleet-api/getting-started/what-is-fleet-api). With this toolkit, your agents can:

- Get information about Tesla vehicles
- Control vehicle features (climate, doors, etc.)
- Monitor vehicle status and location
- Manage charging settings

## Getting Started

To use the Tesla toolkit, you'll need to:

1. Set up the [Tesla auth provider](/home/auth-providers/tesla)
2. Install the Tesla toolkit

```bash
pip install arcade-tesla
```

## Example Usage

```python
from arcade import ArcadeClient
from arcade.tools import ModelAPI

# Initialize the Arcade client
client = ArcadeClient()

# Create a model API instance with the Tesla toolkit
model_api = ModelAPI(
client=client,
model="gpt-4",
tools=["tesla"]
)

# Run the model with a prompt
response = model_api.run(
"Check the battery level of my Tesla and turn on the climate control if it's below 20%."
)

print(response.content)
```

## Available Tools

The Tesla toolkit provides the following tools:

### get_vehicles

Get a list of vehicles associated with the user's Tesla account.

### get_vehicle_data

Get detailed information about a specific Tesla vehicle, including battery level, climate settings, and more.

### wake_up_vehicle

Wake up a sleeping Tesla vehicle.

### set_climate

Control the climate settings of a Tesla vehicle, including temperature and climate mode.

### control_doors

Lock or unlock the doors of a Tesla vehicle.

### control_windows

Open or close the windows of a Tesla vehicle.

### start_charging

Start charging a Tesla vehicle if it's connected to a charger.

### stop_charging

Stop charging a Tesla vehicle.

### set_charge_limit

Set the charging limit for a Tesla vehicle.

### get_vehicle_location

Get the current location of a Tesla vehicle.

## Authentication

This toolkit uses the [Tesla auth provider](/home/auth-providers/tesla) for authentication. When a user interacts with your agent, they will be prompted to authorize access to their Tesla account if they haven't already done so.

## API Reference

For more details on the Tesla Fleet API, see the [official documentation](https://developer.tesla.com/docs/fleet-api/getting-started/what-is-fleet-api).
Binary file added public/images/icons/tesla.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/components/custom/Toolkits/toolkits-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ const availableTools: Tool[] = [
category: "social",
type: "auth",
},
{
name: "Tesla",
image: "tesla.png",
summary: "Control and monitor Tesla vehicles with your agents",
link: "/toolkits/social-communication/tesla",
category: "social",
type: "auth",
},
{
name: "Twitch",
image: "twitch.png",
Expand Down