-
Notifications
You must be signed in to change notification settings - Fork 3
feat: adding axon sdk methods #758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { Runloop } from '../index'; | ||
| import type * as Core from '../core'; | ||
| import { Stream } from '../streaming'; | ||
| import type { | ||
| AxonView, | ||
| AxonCreateParams, | ||
| AxonPublishParams, | ||
| PublishResultView, | ||
| AxonEventView, | ||
| } from '../resources/axons'; | ||
|
|
||
| /** | ||
| * [Beta] Object-oriented interface for working with Axons. | ||
| * | ||
| * @category Axon | ||
| * | ||
| * @remarks | ||
| * ## Overview | ||
| * | ||
| * The `Axon` class provides a high-level, object-oriented API for managing axons. | ||
| * Axons are event communication channels that support publishing events and subscribing | ||
| * to event streams via server-sent events (SSE). | ||
| * | ||
| * ## Quickstart | ||
| * | ||
| * ```typescript | ||
| * import { RunloopSDK } from '@runloop/api-client'; | ||
| * | ||
| * const runloop = new RunloopSDK(); | ||
| * const axon = await runloop.axon.create(); | ||
| * | ||
| * // Publish an event | ||
| * await axon.publish({ | ||
| * event_type: 'task_complete', | ||
| * origin: 'AGENT_EVENT', | ||
| * payload: JSON.stringify({ result: 'success' }), | ||
| * source: 'my-agent', | ||
| * }); | ||
| * | ||
| * // Subscribe to events | ||
| * const stream = await axon.subscribeSse(); | ||
| * for await (const event of stream) { | ||
| * console.log(event.event_type, event.payload); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export class Axon { | ||
| private client: Runloop; | ||
| private _id: string; | ||
|
|
||
| private constructor(client: Runloop, id: string) { | ||
| this.client = client; | ||
| this._id = id; | ||
| } | ||
|
|
||
| /** | ||
| * [Beta] Create a new Axon. | ||
| * | ||
| * See the {@link AxonOps.create} method for calling this | ||
| * @private | ||
| * | ||
| * @param {Runloop} client - The Runloop client instance | ||
| * @param {AxonCreateParams} [params] - Parameters for creating the axon | ||
| * @param {Core.RequestOptions} [options] - Request options | ||
| * @returns {Promise<Axon>} An {@link Axon} instance | ||
| */ | ||
| static async create( | ||
| client: Runloop, | ||
| params?: AxonCreateParams, | ||
| options?: Core.RequestOptions, | ||
| ): Promise<Axon> { | ||
| const axonData = await client.axons.create(params ?? {}, options); | ||
| return new Axon(client, axonData.id); | ||
| } | ||
|
|
||
| /** | ||
| * Create an Axon instance by ID without retrieving from API. | ||
| * Use getInfo() to fetch the actual data when needed. | ||
| * | ||
| * See the {@link AxonOps.fromId} method for calling this | ||
| * @private | ||
| * | ||
| * @param {Runloop} client - The Runloop client instance | ||
| * @param {string} id - The axon ID | ||
| * @returns {Axon} An {@link Axon} instance | ||
| */ | ||
| static fromId(client: Runloop, id: string): Axon { | ||
| return new Axon(client, id); | ||
| } | ||
|
|
||
| /** | ||
| * Get the axon ID. | ||
| * @returns {string} The axon ID | ||
| */ | ||
| get id(): string { | ||
| return this._id; | ||
| } | ||
|
|
||
| /** | ||
| * [Beta] Get the complete axon data from the API. | ||
| * | ||
| * @param {Core.RequestOptions} [options] - Request options | ||
| * @returns {Promise<AxonView>} The axon data | ||
| */ | ||
| async getInfo(options?: Core.RequestOptions): Promise<AxonView> { | ||
| return this.client.axons.retrieve(this._id, options); | ||
| } | ||
|
|
||
| /** | ||
| * [Beta] Publish an event to this axon. | ||
| * | ||
| * @param {AxonPublishParams} params - Parameters for the event to publish | ||
| * @param {Core.RequestOptions} [options] - Request options | ||
| * @returns {Promise<PublishResultView>} The publish result with sequence number and timestamp | ||
| */ | ||
| async publish(params: AxonPublishParams, options?: Core.RequestOptions): Promise<PublishResultView> { | ||
| return this.client.axons.publish(this._id, params, options); | ||
| } | ||
|
|
||
| /** | ||
| * [Beta] Subscribe to this axon's event stream via server-sent events. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const stream = await axon.subscribeSse(); | ||
| * for await (const event of stream) { | ||
| * console.log(`[${event.source}] ${event.event_type}: ${event.payload}`); | ||
| * } | ||
| * ``` | ||
| * | ||
| * @param {Core.RequestOptions} [options] - Request options | ||
| * @returns {Promise<Stream<AxonEventView>>} An async iterable stream of axon events | ||
| */ | ||
| async subscribeSse(options?: Core.RequestOptions): Promise<Stream<AxonEventView>> { | ||
| return this.client.axons.subscribeSse(this._id, options); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't this drop
defaultHeadersif user provides any headers? we should make thisheaders: {Accept: 'text/event-stream', ...options?.headers,}