Skip to main content

Availability

The Service API is available to plugins in AI Studio and on Edge Gateways. The Service API lets plugins read and change platform data, such as LLMs, Apps and plugin KV storage. Plugins call it through the Context.Services interface of the unified plugin SDK (pkg/plugin_sdk).

Services by Runtime

The services that a plugin can use depend on its runtime:
  • Universal services, in AI Studio and on Edge Gateways: KV storage, the logger, and the Event Service.
  • Studio Services, in AI Studio: LLMs, tools, plugins, Apps, notifications, resource types, permissions, governed metadata, governance reads and schedules. Refer to Studio Services.
  • RAG and datasource services, in AI Studio: embeddings, document storage, vector search and datasource management. Refer to RAG and Datasource Services.
  • Gateway Services, on Edge Gateways: Apps, LLMs, budget status, model prices, credential validation and App storage. Refer to Gateway Services.

Access Pattern

All services are accessed through the Context.Services interface provided to your plugin handlers:
Expandable

Initialization and Connection Warmup

For Service API access in AI Studio, plugins use a session-based broker pattern. The SDK does most of the setup. For reliable Service API access, warm up the connection as this section shows.

The Connection Warmup Pattern

Critical: The go-plugin broker only accepts ONE connection per broker ID. A plugin can use both the Event Service and the Management Service API. Then the service that dials first succeeds, and the two services share the connection. To ensure reliable Service API access, implement SessionAware and warm up the connection in OnSessionReady:
Expandable

Why Warmup Is Required

Without the warmup pattern, a plugin that uses the Service API during an RPC call can get “timeout waiting for connection info” errors. The causes are:
  1. The broker connection is time-sensitive. Open it early.
  2. A late dial, during an RPC call from the UI, can fail.
  3. Event subscriptions and Service API calls share the same connection. One warmup opens the connection for both.

Legacy Pattern (Still Supported)

The older pattern of extracting the broker ID manually during Initialize still works but is not recommended:
Expandable
Note: The SDK now handles broker ID extraction automatically via OpenSession. You only need to implement SessionAware and warm up the connection.

Universal Services

These services are available in both Studio and Gateway runtimes.

KV Storage

Key-value storage for plugin data:
  • Studio: PostgreSQL-backed, shared across hosts, durable
  • Gateway: Local database, per-instance, ephemeral
The KV namespace belongs to the plugin record. When you delete the plugin in AI Studio, AI Studio also deletes its KV data. If you install the plugin again, it starts with an empty store. In AI Studio, KV calls need the kv.readwrite scope.

Write Data

Returns true if the call created the key, and false if it updated the key. Pass an expiry time (*time.Time) as the last argument, or nil for no expiry. Example:

Write Data with TTL

Writes data with a relative time-to-live. The platform removes expired keys. A read of an expired key returns a not-found error.

Read Data

Returns an error if the key does not exist or has expired. Example:

Delete Data

Returns true if the key existed.

List Keys

ctx.Services.KV().List(ctx, prefix) is not implemented. In both runtimes, it returns the error list operation not yet supported.

Direct SDK Calls

Studio plugins can also call the KV functions of the ai_studio_sdk package: WritePluginKV, WritePluginKVWithTTL, ReadPluginKV and DeletePluginKV. They take the same arguments as the KV() methods.

Logger

Structured logging with key-value pairs:
Example:
Expandable

Scopes

Each Service API call in AI Studio needs a scope. Declare the scopes under permissions.services in the plugin manifest. Refer to Plugin Scopes.

Error Handling

Service API calls return standard Go errors:
Common error types:
  • Permission denied: Missing required scope
  • Not found: Resource doesn’t exist
  • Invalid argument: Bad request parameters
  • Unavailable: Service not ready
Retry failed calls with exponential backoff:
Expandable

Context and Timeouts

Always use contexts with timeouts:

Valid UTF-8 in Protobuf Strings

Protobuf string fields must contain valid UTF-8. The SDK cleans strings at its own gRPC boundary, for example in Call() and PortalCall(). If your plugin puts external data (file content, third-party responses) into a protobuf string field, clean the data first. Otherwise the call fails with string field contains invalid UTF-8.
Both functions replace invalid sequences with the Unicode replacement character (U+FFFD).

Best Practices

  1. Check SDK Initialization:
  2. Check the Runtime: Check ctx.Runtime before you call a runtime-specific service.
  3. Type Assert the Results: Gateway Services and some Studio Services return interface{}. Type assert the result to the correct response type.
  4. Handle Pagination:
    Expandable
  5. Cache Results: Cache data that you read often, in memory or in KV storage, to reduce API calls.
  6. Log Errors: Use ctx.Services.Logger() for consistent structured logs, and check the error of every Service API call.

Complete Examples

For complete working examples of Service API usage:
  • Studio: examples/plugins/studio/service-api-test/ - Comprehensive Studio Services testing
  • Gateway: examples/plugins/gateway/gateway-service-test/ - Gateway Services examples
  • Rate Limiter: examples/plugins/studio/llm-rate-limiter-multiphase/ - Multi-capability plugin with KV storage
  • Scheduler: examples/plugins/studio/scheduler-demo/ - Scheduled tasks with manifest and API patterns