An open-source, faster Devin alternative
TypeScript
sdk: event subscription, delivery and event input types
Igor Zalutski98c389b464d9
diff --git a/docs/reference/typescript-sdk.mdx b/docs/reference/typescript-sdk.mdxindex 79162df9..47d9d05d 100644--- a/docs/reference/typescript-sdk.mdx+++ b/docs/reference/typescript-sdk.mdx@@ -997,3 +997,19 @@ export` files. They carry no behavior.| `MemorySavedEvent` | The `data` of a `memory.saved` [session event](/agents/events) || `MemoryErrorCode` | The codes the memory routes return in `{ error: { code, message } }` || `StartSessionOnDocumentParams`, `StartSessionOnDocumentResult` | The helper's parameters and result |++### Event subscription types++The shapes of the [event subscription](/agents/api#event-subscriptions)+routes and of the input they deliver, as types only.++| Type | Shape |+| --- | --- |+| `OutcomeEventType` | `"turn.completed" \| "turn.failed" \| "turn.cancelled"` |+| `SessionDestination` | `{ type: "session", sessionId }` |+| `CreateEventSubscriptionBody` | Body of `POST /projects/<id>/event-subscriptions`: `agentId?`, `events`, `destination`, `environment?` |+| `EventSubscription` | A subscription as the routes return it |+| `OutcomeEvent` | The delivered outcome: `id`, `type`, `sessionId`, `turnId`, `agentId`, `occurredAt`, `reason?`, `error?`, `result?` |+| `EventInput` | `{ source: "event", text?, event }`, what the receiving agent's `useInput()` returns |+| `TurnOutcomeDelivery` | One entry of a turn's `deliveries` on `GET /sessions/<id>` |+| `EventSubscriptionErrorCode` | The codes the subscription routes return in `{ error: { code, message } }` |diff --git a/sdks/typescript/src/agents/event-subscriptions.test.ts b/sdks/typescript/src/agents/event-subscriptions.test.tsnew file mode 100644index 00000000..ba6576a2--- /dev/null+++ b/sdks/typescript/src/agents/event-subscriptions.test.ts@@ -0,0 +1,63 @@+import { describe, expect, expectTypeOf, it } from "vitest";++import type {+ CreateEventSubscriptionBody,+ EventInput,+ EventSubscription,+ OutcomeEvent,+ TurnOutcomeDelivery,+} from "./index.js";++// Types only: what has to compile for a caller to type a subscription+// request, its response, a turn's deliveries, and the delivered input.+describe("event subscription types", () => {+ it("shape a subscription request and its response", () => {+ const body: CreateEventSubscriptionBody = {+ agentId: "worker",+ events: ["turn.completed", "turn.failed"],+ destination: { type: "session", sessionId: "ses_coordinator" },+ environment: "development",+ };+ const subscription: EventSubscription = {+ id: "evs_1",+ projectId: "prj_1",+ ...body,+ createdAt: "2026-09-10T12:00:00.000Z",+ };+ expectTypeOf(subscription.destination.type).toEqualTypeOf<"session">();+ expect(subscription.events).toContain("turn.failed");+ // @ts-expect-error a public HTTPS destination is not a destination type.+ const https: CreateEventSubscriptionBody["destination"] = { type: "https", url: "https://example.com" };+ expect(https.type).toBe("https");+ });++ it("narrow a delivered outcome from the agent's input", () => {+ const event: OutcomeEvent = {+ id: "event_9",+ type: "turn.completed",+ sessionId: "ses_worker",+ turnId: "turn-1",+ agentId: "worker",+ occurredAt: "2026-09-10T12:00:00.000Z",+ result: { text: "Done.", truncated: false },+ };+ const input: EventInput = { source: "event", event };+ expectTypeOf(input.source).toEqualTypeOf<"event">();+ expect(input.event.result?.text).toBe("Done.");+ const delivery: TurnOutcomeDelivery = {+ id: "evs_1:event_9",+ subscriptionId: "evs_1",+ eventId: "event_9",+ eventType: "turn.completed",+ destination: { type: "session", sessionId: "ses_coordinator" },+ status: "delivered",+ attempt: 1,+ receipt: { sessionId: "ses_coordinator", turnId: "turn-7" },+ updatedAt: "2026-09-10T12:01:01.000Z",+ };+ expectTypeOf(delivery.error).toEqualTypeOf<+ "subscription_unavailable" | "target_missing" | "target_ended" | "delivery_failed" | undefined+ >();+ expect(delivery.receipt?.turnId).toBe("turn-7");+ });+});diff --git a/sdks/typescript/src/agents/event-subscriptions.ts b/sdks/typescript/src/agents/event-subscriptions.tsnew file mode 100644index 00000000..dbcc1e2a--- /dev/null+++ b/sdks/typescript/src/agents/event-subscriptions.ts@@ -0,0 +1,102 @@+// Event subscription types — the shapes of the management API documented at+// docs/agents/api.mdx ("Event subscriptions") and the input an agent reads+// through `useInput()` when a subscribed outcome is delivered to it.+//+// These are types only. The subscription routes live on the project-scoped+// Serverless Agents surface (`/api/managed-agents/projects/<id>/event-subscriptions`),+// which this package does not call. Use them to type the bodies you send+// with your own HTTP client and the objects it returns.++import type { MemoryEnvironment } from "./memory.js";++/** The turn outcomes a subscription can select. Turn outcomes, not session ends. */+export type OutcomeEventType = "turn.completed" | "turn.failed" | "turn.cancelled";++/** Where a subscription delivers: a session of an agent in the same project. */+export interface SessionDestination {+ type: "session";+ sessionId: string;+}++/** Body of `POST /projects/<id>/event-subscriptions`. */+export interface CreateEventSubscriptionBody {+ /** Exact source agent id; omitted selects every agent in the project. */+ agentId?: string;+ /** At least one outcome type. */+ events: OutcomeEventType[];+ destination: SessionDestination;+ /** The environment whose outcomes are delivered; the destination session runs in it. */+ environment?: MemoryEnvironment;+}++/** A subscription as the routes return it. Immutable once created. */+export interface EventSubscription {+ id: string;+ projectId: string;+ agentId?: string;+ environment?: MemoryEnvironment;+ events: OutcomeEventType[];+ destination: SessionDestination;+ createdAt: string;+}++/**+ * A source turn's recorded outcome as the receiving agent reads it from+ * `useInput().event` when `source` is `"event"`. Identifiers and outcome+ * only; the included agent output is data, not instructions.+ */+export interface OutcomeEvent {+ /** The source session's own event id for the terminal `turn.*` event. */+ id: string;+ type: OutcomeEventType;+ sessionId: string;+ turnId: string;+ agentId: string;+ occurredAt: string;+ /** Why the turn failed or was cancelled, when the source recorded a reason. */+ reason?: string;+ /** The failure message, bounded, when the turn failed. */+ error?: string;+ /** The final assistant message of a completed turn; `truncated` when it was cut to fit. */+ result?: { text: string; truncated?: boolean };+}++/** The input of a turn an event subscription started, as the agent's `useInput()` returns it. */+export interface EventInput {+ source: "event";+ /** A one-line description of the outcome, for agents that only read `text`. */+ text?: string;+ event: OutcomeEvent;+}++/**+ * One outcome's delivery to one subscription, as `GET /sessions/<id>`+ * lists it under the source turn's `deliveries`.+ */+export interface TurnOutcomeDelivery {+ /** `<subscriptionId>:<eventId>`, also the idempotency key of the turn it starts. */+ id: string;+ subscriptionId: string;+ eventId: string;+ eventType: OutcomeEventType;+ destination: SessionDestination;+ status: "pending" | "delivered" | "failed";+ attempt: number;+ /** The turn the destination admitted; the same for a retried delivery. */+ receipt?: { sessionId: string; turnId: string };+ nextAttemptAt?: string;+ /**+ * `subscription_unavailable` (deleted before delivery), `target_missing`,+ * `target_ended`, or `delivery_failed` for any other failure.+ */+ error?: "subscription_unavailable" | "target_missing" | "target_ended" | "delivery_failed";+ updatedAt: string;+}++/** Error codes the subscription routes return in `{ error: { code, message } }`. */+export type EventSubscriptionErrorCode =+ | "invalid_event_subscription"+ | "project_scope_violation"+ | "destination_session_not_found"+ | "destination_session_ended"+ | "event_subscription_not_found";diff --git a/sdks/typescript/src/agents/index.ts b/sdks/typescript/src/agents/index.tsindex e7801674..58a3ad5d 100644--- a/sdks/typescript/src/agents/index.ts+++ b/sdks/typescript/src/agents/index.ts@@ -56,6 +56,10 @@ export { verifyWebhook, WebhookVerificationError } from "./webhooks.js";export type { WebhookDelivery, VerifyWebhookOptions } from "./webhooks.js";export { startSessionOnDocument, sessionIdempotencyKey } from "./memory-sessions.js";export type { StartSessionOnDocumentParams, StartSessionOnDocumentResult } from "./memory-sessions.js";+export type {+ OutcomeEventType, SessionDestination, CreateEventSubscriptionBody, EventSubscription,+ OutcomeEvent, EventInput, TurnOutcomeDelivery, EventSubscriptionErrorCode,+} from "./event-subscriptions.js";export type {MemoryEnvironment, MemoryAccess, MemoryAgentWrites, MemoryBinding, MemoryBindings,SessionMemoryBinding, MemoryWriter, MemoryDocumentMeta, MemoryDocument, MemoryDocumentPage,