sdk-js: added cron functionality to match langgraph-api (#863)

* testing

* first draft

* working

* first draft

* fmt

* vadym comments
This commit is contained in:
Isaac Francisco
2024-06-28 16:26:22 -07:00
committed by GitHub
parent 1fe13cb009
commit 281e66ed2e
4 changed files with 114 additions and 0 deletions
+96
View File
@@ -8,6 +8,7 @@ import {
Run,
Thread,
ThreadState,
Cron,
} from "./schema.js";
import { AsyncCaller, AsyncCallerParams } from "./utils/async_caller.mjs";
import { EventSourceParser, createParser } from "eventsource-parser";
@@ -17,6 +18,7 @@ import {
RunsStreamPayload,
RunsWaitPayload,
StreamEvent,
CronsCreatePayload,
} from "./types.mjs";
interface ClientConfig {
@@ -104,6 +106,94 @@ class BaseClient {
}
}
class CronsClient extends BaseClient {
/**
*
* @param threadId The ID of the thread.
* @param assistantId Assistant ID to use for this cron job.
* @param payload Payload for creating a cron job.
* @returns The created background run.
*/
async createForThread(
threadId: string,
assistantId: string,
payload?: CronsCreatePayload,
): Promise<Run> {
const json: Record<string, any> = {
schedule: payload?.schedule,
input: payload?.input,
config: payload?.config,
metadata: payload?.metadata,
assistant_id: assistantId,
interrupt_before: payload?.interruptBefore,
interrupt_after: payload?.interruptAfter,
webhook: payload?.webhook,
};
return this.fetch<Run>(`/threads/${threadId}/runs/crons`, {
method: "POST",
json,
});
}
/**
*
* @param assistantId Assistant ID to use for this cron job.
* @param payload Payload for creating a cron job.
* @returns
*/
async create(
assistantId: string,
payload?: CronsCreatePayload,
): Promise<Run> {
const json: Record<string, any> = {
schedule: payload?.schedule,
input: payload?.input,
config: payload?.config,
metadata: payload?.metadata,
assistant_id: assistantId,
interrupt_before: payload?.interruptBefore,
interrupt_after: payload?.interruptAfter,
webhook: payload?.webhook,
};
return this.fetch<Run>(`/runs/crons`, {
method: "POST",
json,
});
}
/**
*
* @param cronId Cron ID of Cron job to delete.
*/
async delete(cronId: string): Promise<void> {
await this.fetch<void>(`/runs/crons/${cronId}`, {
method: "DELETE",
});
}
/**
*
* @param query Query options.
* @returns List of crons.
*/
async search(query?: {
assistantId?: string;
threadId?: string;
limit?: number;
offset?: number;
}): Promise<Cron[]> {
return this.fetch<Cron[]>("/runs/crons/search", {
method: "POST",
json: {
assistant_id: query?.assistantId ?? undefined,
thread_id: query?.threadId ?? undefined,
limit: query?.limit ?? 10,
offset: query?.offset ?? 0,
},
});
}
}
class AssistantsClient extends BaseClient {
/**
* Get an assistant by ID.
@@ -674,9 +764,15 @@ export class Client {
*/
public runs: RunsClient;
/**
* The client for interacting with cron runs.
*/
public crons: CronsClient;
constructor(config?: ClientConfig) {
this.assistants = new AssistantsClient(config);
this.threads = new ThreadsClient(config);
this.runs = new RunsClient(config);
this.crons = new CronsClient(config);
}
}
+1
View File
@@ -10,4 +10,5 @@ export type {
Run,
Thread,
ThreadState,
Cron,
} from "./schema.js";
+10
View File
@@ -68,6 +68,16 @@ export interface Thread {
metadata: Metadata;
}
export interface Cron {
cron_id: string;
thread_id: Optional<string>;
end_time: Optional<string>;
schedule: string;
created_at: string;
updated_at: string;
payload: Record<string, unknown>;
}
export type DefaultValues = Record<string, unknown>[] | Record<string, unknown>;
export interface ThreadState<ValuesType = DefaultValues> {
+7
View File
@@ -83,4 +83,11 @@ export interface RunsCreatePayload extends RunsInvokePayload {
webhook?: string;
}
export interface CronsCreatePayload extends RunsCreatePayload {
/**
* Schedule for running the Cron Job
*/
schedule: string;
}
export type RunsWaitPayload = RunsStreamPayload;