ESC
Start typing to search...

SDK Reference Overview

The @teleton-agent/sdk package provides 102 methods across 9 namespaces for building Teleton Agent plugins.

Installation

Install the SDK as a dependency in your plugin project:

Install the SDK
npm install @teleton-agent/sdk

The SDK package exports TypeScript type definitions only. There is no runtime code — the actual implementations are injected by the Teleton core platform when your plugin is loaded.

SDK Architecture

The PluginSDK interface is the root object passed to your plugin's tools(sdk) function. It provides namespaced access to all platform capabilities:

PluginSDK Interface
interface PluginSDK {
  readonly version: string;            // SDK version (e.g. "1.0.0")
  readonly ton: TonSDK;               // TON blockchain operations
  readonly telegram: TelegramSDK;     // Telegram messaging & users
  readonly db: Database | null;        // Isolated SQLite database
  readonly config: Record<string, unknown>;      // Sanitized app config
  readonly pluginConfig: Record<string, unknown>; // Plugin-specific config
  readonly secrets: SecretsSDK;        // Secure secret access
  readonly storage: StorageSDK | null; // Key-value storage
  readonly log: PluginLogger;          // Prefixed logger
  readonly bot: BotSDK | null;         // Inline bot mode
}

Each namespace groups related methods. The SDK object is frozen and immutable — you cannot reassign its properties or add new ones. This guarantees isolation between plugins.

Quick Example

A minimal plugin that exposes a single tool to check TON balance:

plugins/balance-checker/index.ts
import type { PluginSDK, SimpleToolDef, PluginManifest } from "@teleton-agent/sdk";

export const manifest: PluginManifest = {
  name: "balance-checker",
  version: "1.0.0",
  description: "Check any TON address balance with USD conversion",
};

export const tools = (sdk: PluginSDK): SimpleToolDef[] => [
  {
    name: "check_balance",
    description: "Check the TON balance and USD value of an address",
    parameters: {
      type: "object",
      properties: {
        address: {
          type: "string",
          description: "TON address to check (EQ... or UQ... format)",
        },
      },
      required: ["address"],
    },
    async execute(params) {
      const address = params.address as string;

      if (!sdk.ton.validateAddress(address)) {
        return { success: false, error: "Invalid TON address format" };
      }

      const [balance, price] = await Promise.all([
        sdk.ton.getBalance(address),
        sdk.ton.getPrice(),
      ]);

      if (!balance) {
        return { success: false, error: "Failed to fetch balance" };
      }

      const usdValue = price
        ? (parseFloat(balance.balance) * price.usd).toFixed(2)
        : "unavailable";

      sdk.log.info(`Balance check for ${address}: ${balance.balance} TON`);

      return {
        success: true,
        data: {
          address,
          balance: balance.balance,
          balanceNano: balance.balanceNano,
          usdValue: `$${usdValue}`,
        },
      };
    },
  },
];

Namespace Map

All SDK namespaces at a glance:

NamespaceMethodsDescription
sdk.ton18Wallet, balance, price, send TON/jettons, NFTs, transaction history, payment verification, unit conversion
sdk.ton.dex6DEX quotes and swaps on STON.fi and DeDust
sdk.ton.dns8.ton domain check, resolve, auctions, bidding, linking, ADNL site records
sdk.telegram51Messages, media, moderation, polls, Stars, gifts, NFT marketplace, stories
sdk.bot7Inline queries, callback handlers, colored keyboards (Layer 222)
sdk.secrets3Secure access to API keys and credentials
sdk.storage5Simple key-value persistence with optional TTL
sdk.log4Structured logging with auto plugin prefix
sdk.dbN/ARaw better-sqlite3 database for advanced SQL

Plugin Manifest

The PluginManifest interface declares your plugin's identity, dependencies, and requirements. Export it as manifest from your plugin entry point.

PluginManifest Interface
interface PluginManifest {
  name: string;                        // Lowercase alphanumeric + hyphens, 1-64 chars
  version: string;                     // Semver (e.g. "1.0.0")
  author?: string;                     // Plugin author
  description?: string;                // Short description (max 256 chars)
  dependencies?: string[];             // Required built-in modules (e.g. ["deals"])
  defaultConfig?: Record<string, unknown>; // Default config values
  sdkVersion?: string;                 // Required SDK version range (e.g. ">=1.0.0")
  secrets?: Record<string, SecretDeclaration>; // Required API keys
  bot?: BotManifest;                   // Bot capabilities (inline, callbacks)
}
Full Manifest Example
import type { PluginManifest } from "@teleton-agent/sdk";

export const manifest: PluginManifest = {
  name: "weather-report",
  version: "2.1.0",
  author: "dev@example.com",
  description: "Real-time weather data via OpenWeatherMap API",
  sdkVersion: ">=1.0.0",
  defaultConfig: {
    units: "metric",
    defaultCity: "Paris",
  },
  secrets: {
    api_key: {
      required: true,
      description: "OpenWeatherMap API key",
    },
    webhook_url: {
      required: false,
      description: "Optional webhook for alerts",
    },
  },
  bot: {
    inline: true,
    callbacks: true,
    rateLimits: {
      inlinePerMinute: 20,
      callbackPerMinute: 40,
    },
  },
};

Tool Definition

Plugins expose tools to the AI agent via the tools(sdk) export. Each tool is a SimpleToolDef object:

SimpleToolDef Interface
interface SimpleToolDef {
  name: string;                // Unique tool name (e.g. "weather_forecast")
  description: string;         // Human-readable description for the LLM
  parameters?: Record<string, unknown>;  // JSON Schema for parameters
  execute: (
    params: Record<string, unknown>,
    context: PluginToolContext
  ) => Promise<ToolResult>;    // Tool executor function
  scope?: ToolScope;           // Visibility scope (default: "always")
  category?: ToolCategory;     // "data-bearing" or "action"
}

ToolResult

Every tool executor must return a ToolResult:

ToolResult Interface
interface ToolResult {
  success: boolean;   // Whether the execution succeeded
  data?: unknown;     // Result data (serialized to JSON for the LLM)
  error?: string;     // Error message if failed
}

PluginToolContext

The context parameter provides runtime information about the current invocation:

PluginToolContext Interface
interface PluginToolContext {
  chatId: string;      // Telegram chat ID where the tool was invoked
  senderId: number;    // Telegram user ID of the sender
  isGroup: boolean;    // Whether this is a group chat (vs DM)
  bridge: unknown;     // TelegramBridge instance
  db: unknown;         // Plugin's isolated SQLite database
  config?: Record<string, unknown>;  // Sanitized bot config
}
Tool With Context
export const tools = (sdk: PluginSDK): SimpleToolDef[] => [
  {
    name: "greet_user",
    description: "Send a personalized greeting to the current chat",
    parameters: {
      type: "object",
      properties: {
        name: { type: "string", description: "Name to greet" },
      },
      required: ["name"],
    },
    scope: "dm-only",
    async execute(params, context) {
      const name = params.name as string;
      await sdk.telegram.sendMessage(
        context.chatId,
        `Hello, ${name}! Welcome to the chat.`
      );
      return { success: true, data: { greeted: name } };
    },
  },
];

Tool Scopes

Each tool can declare a scope that controls when it is visible to the AI agent:

ScopeVisibilityUse Case
"always"All chats (DMs and groups)General-purpose tools with no restrictions
"dm-only"Direct messages onlySensitive operations (wallet, private data)
"group-only"Group chats onlyModeration, polls, group-specific features
"admin-only"Admin users onlyConfiguration, dangerous operations

Scopes are enforced at three levels: tool registration, context filtering (getForContext()), and execution-time validation. An admin can override scopes per-tool via the tool_config database table.

Scope Example
{
  name: "transfer_funds",
  description: "Send TON from the bot wallet to a recipient",
  scope: "admin-only",
  async execute(params, context) {
    const to = params.address as string;
    const amount = params.amount as number;
    const result = await sdk.ton.sendTON(to, amount);
    return { success: true, data: result };
  },
}

Important Notes

  • SDK is frozen and immutable. The PluginSDK object and all its namespaces are Object.freeze()-d at creation time. You cannot reassign properties, add new ones, or modify existing methods. This prevents plugins from interfering with each other.
  • sdk.bot is null if your manifest does not declare bot. You must include a bot: { inline: true } or bot: { callbacks: true } declaration in your manifest to receive a BotSDK instance.
  • sdk.db is null if your plugin does not export migrate(). To use the raw SQLite database, you must export a migrate(db) function from your plugin entry point. The storage namespace (sdk.storage) auto-creates its own _kv table and does not require migrate().
  • sdk.storage is null if no database is available. This only happens if the core platform cannot create a plugin database at all (rare edge case).
  • Secret environment variable naming. Secrets are resolved from environment variables using the pattern ${PLUGIN_NAME_UPPER}_${KEY_UPPER}. For example, a plugin named weather-report with a secret key api_key would look for WEATHER_REPORT_API_KEY in the environment.
  • All async SDK methods can throw PluginSDKError. Always wrap SDK calls in try/catch blocks or handle errors at the tool level. See the Error Handling guide.

Next Steps

Explore the detailed reference for each namespace:

  • TON Blockchain — Wallet, balance, transfers, jettons, NFTs, payment verification
  • DEX Trading — Quotes and swaps on STON.fi and DeDust
  • DNS & Domains — .ton domain management, auctions, linking
  • Telegram — Messages, media, moderation, Stars, gifts
  • Bot SDK — Inline mode, callback handlers, colored keyboards
  • Utilities — Secrets, storage, logging, database
  • Error Handling — Error codes, patterns, debugging