Skip to content

Commit 1681eb3

Browse files
authored
feat: adding axon sdk methods (#758)
1 parent db682a9 commit 1681eb3

9 files changed

Lines changed: 720 additions & 3 deletions

File tree

src/resources/axons.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ export class Axons extends APIResource {
5151
* [Beta] Subscribe to an axon event stream via server-sent events.
5252
*/
5353
subscribeSse(id: string, options?: Core.RequestOptions): APIPromise<Stream<AxonEventView>> {
54-
return this._client.get(`/v1/axons/${id}/subscribe/sse`, { ...options, stream: true }) as APIPromise<
55-
Stream<AxonEventView>
56-
>;
54+
const defaultHeaders = {
55+
Accept: 'text/event-stream',
56+
};
57+
const mergedOptions: Core.RequestOptions = {
58+
headers: defaultHeaders,
59+
...options,
60+
};
61+
return this._client.get(`/v1/axons/${id}/subscribe/sse`, {
62+
...mergedOptions,
63+
stream: true,
64+
}) as APIPromise<Stream<AxonEventView>>;
5765
}
5866
}
5967

src/sdk.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Blueprint, type CreateParams as BlueprintCreateParams } from './sdk/blu
66
import { Snapshot } from './sdk/snapshot';
77
import { StorageObject } from './sdk/storage-object';
88
import { Agent } from './sdk/agent';
9+
import { Axon } from './sdk/axon';
910
import { Scorer } from './sdk/scorer';
1011
import { NetworkPolicy } from './sdk/network-policy';
1112
import { GatewayConfig } from './sdk/gateway-config';
@@ -23,6 +24,7 @@ import type {
2324
import type { BlueprintListParams } from './resources/blueprints';
2425
import type { ObjectCreateParams, ObjectListParams } from './resources/objects';
2526
import type { AgentCreateParams, AgentListParams } from './resources/agents';
27+
import type { AxonCreateParams } from './resources/axons';
2628
import type { ScorerCreateParams, ScorerListParams } from './resources/scenarios/scorers';
2729
import type { NetworkPolicyCreateParams, NetworkPolicyListParams } from './resources/network-policies';
2830
import type { GatewayConfigCreateParams, GatewayConfigListParams } from './resources/gateway-configs';
@@ -369,6 +371,7 @@ type ContentType = ObjectCreateParams['content_type'];
369371
* - `snapshot` - {@link SnapshotOps}
370372
* - `storageObject` - {@link StorageObjectOps}
371373
* - `agent` - {@link AgentOps}
374+
* - `axon` - {@link AxonOps}
372375
* - `scorer` - {@link ScorerOps}
373376
* - `networkPolicy` - {@link NetworkPolicyOps}
374377
* - `gatewayConfig` - {@link GatewayConfigOps}
@@ -433,6 +436,15 @@ export class RunloopSDK {
433436
*/
434437
public readonly agent: AgentOps;
435438

439+
/**
440+
* **Axon Operations** - {@link AxonOps} for creating and accessing {@link Axon} class instances.
441+
*
442+
* [Beta] Axons are event communication channels that support publishing events and subscribing
443+
* to event streams via server-sent events (SSE). Use these operations to create new axons,
444+
* get existing ones by ID, or list all active axons.
445+
*/
446+
public readonly axon: AxonOps;
447+
436448
/**
437449
* **Scorer Operations** - {@link ScorerOps} for creating and accessing {@link Scorer} class instances.
438450
*
@@ -494,6 +506,7 @@ export class RunloopSDK {
494506
this.snapshot = new SnapshotOps(this.api);
495507
this.storageObject = new StorageObjectOps(this.api);
496508
this.agent = new AgentOps(this.api);
509+
this.axon = new AxonOps(this.api);
497510
this.scorer = new ScorerOps(this.api);
498511
this.networkPolicy = new NetworkPolicyOps(this.api);
499512
this.gatewayConfig = new GatewayConfigOps(this.api);
@@ -1484,6 +1497,82 @@ export class AgentOps {
14841497
}
14851498
}
14861499

1500+
/**
1501+
* [Beta] Axon SDK interface for managing axons.
1502+
*
1503+
* @category Axon
1504+
*
1505+
* @remarks
1506+
* ## Overview
1507+
*
1508+
* The `AxonOps` class provides a high-level abstraction for managing axons,
1509+
* which are event communication channels. Axons support publishing events
1510+
* and subscribing to event streams via server-sent events (SSE).
1511+
*
1512+
* ## Usage
1513+
*
1514+
* This interface is accessed via {@link RunloopSDK.axon}. You should construct
1515+
* a {@link RunloopSDK} instance and use it from there:
1516+
*
1517+
* @example
1518+
* ```typescript
1519+
* const runloop = new RunloopSDK();
1520+
* const axon = await runloop.axon.create();
1521+
*
1522+
* // Publish an event
1523+
* await axon.publish({
1524+
* event_type: 'task_complete',
1525+
* origin: 'AGENT_EVENT',
1526+
* payload: JSON.stringify({ result: 'success' }),
1527+
* source: 'my-agent',
1528+
* });
1529+
*
1530+
* // Subscribe to events
1531+
* const stream = await axon.subscribeSse();
1532+
* for await (const event of stream) {
1533+
* console.log(event.event_type, event.payload);
1534+
* }
1535+
* ```
1536+
*/
1537+
export class AxonOps {
1538+
/**
1539+
* @private
1540+
*/
1541+
constructor(private client: RunloopAPI) {}
1542+
1543+
/**
1544+
* [Beta] Create a new axon.
1545+
*
1546+
* @param {AxonCreateParams} [params] - Parameters for creating the axon.
1547+
* @param {Core.RequestOptions} [options] - Request options.
1548+
* @returns {Promise<Axon>} An {@link Axon} instance.
1549+
*/
1550+
async create(params?: AxonCreateParams, options?: Core.RequestOptions): Promise<Axon> {
1551+
return Axon.create(this.client, params, options);
1552+
}
1553+
1554+
/**
1555+
* Get an axon object by its ID.
1556+
*
1557+
* @param {string} id - The ID of the axon.
1558+
* @returns {Axon} An {@link Axon} instance.
1559+
*/
1560+
fromId(id: string): Axon {
1561+
return Axon.fromId(this.client, id);
1562+
}
1563+
1564+
/**
1565+
* [Beta] List all active axons.
1566+
*
1567+
* @param {Core.RequestOptions} [options] - Request options.
1568+
* @returns {Promise<Axon[]>} An array of {@link Axon} instances.
1569+
*/
1570+
async list(options?: Core.RequestOptions): Promise<Axon[]> {
1571+
const result = await this.client.axons.list(options);
1572+
return result.axons.map((axon) => Axon.fromId(this.client, axon.id));
1573+
}
1574+
}
1575+
14871576
/**
14881577
* Scorer SDK interface for managing custom scorers.
14891578
*
@@ -2199,6 +2288,7 @@ export declare namespace RunloopSDK {
21992288
SnapshotOps as SnapshotOps,
22002289
StorageObjectOps as StorageObjectOps,
22012290
AgentOps as AgentOps,
2291+
AxonOps as AxonOps,
22022292
ScorerOps as ScorerOps,
22032293
NetworkPolicyOps as NetworkPolicyOps,
22042294
GatewayConfigOps as GatewayConfigOps,
@@ -2210,6 +2300,7 @@ export declare namespace RunloopSDK {
22102300
Snapshot as Snapshot,
22112301
StorageObject as StorageObject,
22122302
Agent as Agent,
2303+
Axon as Axon,
22132304
Scorer as Scorer,
22142305
NetworkPolicy as NetworkPolicy,
22152306
GatewayConfig as GatewayConfig,
@@ -2229,6 +2320,7 @@ export {
22292320
Snapshot,
22302321
StorageObject,
22312322
Agent,
2323+
Axon,
22322324
Scorer,
22332325
NetworkPolicy,
22342326
McpConfig,

src/sdk/axon.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { Runloop } from '../index';
2+
import type * as Core from '../core';
3+
import { Stream } from '../streaming';
4+
import type {
5+
AxonView,
6+
AxonCreateParams,
7+
AxonPublishParams,
8+
PublishResultView,
9+
AxonEventView,
10+
} from '../resources/axons';
11+
12+
/**
13+
* [Beta] Object-oriented interface for working with Axons.
14+
*
15+
* @category Axon
16+
*
17+
* @remarks
18+
* ## Overview
19+
*
20+
* The `Axon` class provides a high-level, object-oriented API for managing axons.
21+
* Axons are event communication channels that support publishing events and subscribing
22+
* to event streams via server-sent events (SSE).
23+
*
24+
* ## Quickstart
25+
*
26+
* ```typescript
27+
* import { RunloopSDK } from '@runloop/api-client';
28+
*
29+
* const runloop = new RunloopSDK();
30+
* const axon = await runloop.axon.create();
31+
*
32+
* // Publish an event
33+
* await axon.publish({
34+
* event_type: 'task_complete',
35+
* origin: 'AGENT_EVENT',
36+
* payload: JSON.stringify({ result: 'success' }),
37+
* source: 'my-agent',
38+
* });
39+
*
40+
* // Subscribe to events
41+
* const stream = await axon.subscribeSse();
42+
* for await (const event of stream) {
43+
* console.log(event.event_type, event.payload);
44+
* }
45+
* ```
46+
*/
47+
export class Axon {
48+
private client: Runloop;
49+
private _id: string;
50+
51+
private constructor(client: Runloop, id: string) {
52+
this.client = client;
53+
this._id = id;
54+
}
55+
56+
/**
57+
* [Beta] Create a new Axon.
58+
*
59+
* See the {@link AxonOps.create} method for calling this
60+
* @private
61+
*
62+
* @param {Runloop} client - The Runloop client instance
63+
* @param {AxonCreateParams} [params] - Parameters for creating the axon
64+
* @param {Core.RequestOptions} [options] - Request options
65+
* @returns {Promise<Axon>} An {@link Axon} instance
66+
*/
67+
static async create(
68+
client: Runloop,
69+
params?: AxonCreateParams,
70+
options?: Core.RequestOptions,
71+
): Promise<Axon> {
72+
const axonData = await client.axons.create(params ?? {}, options);
73+
return new Axon(client, axonData.id);
74+
}
75+
76+
/**
77+
* Create an Axon instance by ID without retrieving from API.
78+
* Use getInfo() to fetch the actual data when needed.
79+
*
80+
* See the {@link AxonOps.fromId} method for calling this
81+
* @private
82+
*
83+
* @param {Runloop} client - The Runloop client instance
84+
* @param {string} id - The axon ID
85+
* @returns {Axon} An {@link Axon} instance
86+
*/
87+
static fromId(client: Runloop, id: string): Axon {
88+
return new Axon(client, id);
89+
}
90+
91+
/**
92+
* Get the axon ID.
93+
* @returns {string} The axon ID
94+
*/
95+
get id(): string {
96+
return this._id;
97+
}
98+
99+
/**
100+
* [Beta] Get the complete axon data from the API.
101+
*
102+
* @param {Core.RequestOptions} [options] - Request options
103+
* @returns {Promise<AxonView>} The axon data
104+
*/
105+
async getInfo(options?: Core.RequestOptions): Promise<AxonView> {
106+
return this.client.axons.retrieve(this._id, options);
107+
}
108+
109+
/**
110+
* [Beta] Publish an event to this axon.
111+
*
112+
* @param {AxonPublishParams} params - Parameters for the event to publish
113+
* @param {Core.RequestOptions} [options] - Request options
114+
* @returns {Promise<PublishResultView>} The publish result with sequence number and timestamp
115+
*/
116+
async publish(params: AxonPublishParams, options?: Core.RequestOptions): Promise<PublishResultView> {
117+
return this.client.axons.publish(this._id, params, options);
118+
}
119+
120+
/**
121+
* [Beta] Subscribe to this axon's event stream via server-sent events.
122+
*
123+
* @example
124+
* ```typescript
125+
* const stream = await axon.subscribeSse();
126+
* for await (const event of stream) {
127+
* console.log(`[${event.source}] ${event.event_type}: ${event.payload}`);
128+
* }
129+
* ```
130+
*
131+
* @param {Core.RequestOptions} [options] - Request options
132+
* @returns {Promise<Stream<AxonEventView>>} An async iterable stream of axon events
133+
*/
134+
async subscribeSse(options?: Core.RequestOptions): Promise<Stream<AxonEventView>> {
135+
return this.client.axons.subscribeSse(this._id, options);
136+
}
137+
}

src/sdk/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export { Blueprint } from './blueprint';
33
export { Snapshot } from './snapshot';
44
export { StorageObject } from './storage-object';
55
export { Agent } from './agent';
6+
export { Axon } from './axon';
67
export { Execution } from './execution';
78
export { ExecutionResult } from './execution-result';
89
export { Scorer } from './scorer';

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ export type * from './resources/blueprints';
5757

5858
export type * from './resources/agents';
5959

60+
// =============================================================================
61+
// Axon Types
62+
// =============================================================================
63+
64+
export type * from './resources/axons';
65+
6066
// =============================================================================
6167
// Storage Object Types
6268
// =============================================================================

0 commit comments

Comments
 (0)