An open-source, faster Devin alternative
TypeScript
schedules: SDK + CLI clients (design 015)
Igor Zalutski8cdab4e2eb6e
diff --git a/cmd/oc/internal/client/client.go b/cmd/oc/internal/client/client.goindex cf24cee7..461cd529 100644--- a/cmd/oc/internal/client/client.go+++ b/cmd/oc/internal/client/client.go@@ -181,6 +181,34 @@ func (c *Client) Post(ctx context.Context, path string, body, result interface{}return nil}+// Patch performs a PATCH request with a JSON body and decodes the response.+func (c *Client) Patch(ctx context.Context, path string, body, result interface{}) error {+ var bodyReader io.Reader+ if body != nil {+ data, err := json.Marshal(body)+ if err != nil {+ return err+ }+ bodyReader = bytes.NewReader(data)+ }+ req, err := http.NewRequestWithContext(ctx, "PATCH", c.baseURL+path, bodyReader)+ if err != nil {+ return err+ }+ if body != nil {+ req.Header.Set("Content-Type", "application/json")+ }+ resp, err := c.do(req)+ if err != nil {+ return err+ }+ defer resp.Body.Close()+ if result != nil {+ return json.NewDecoder(resp.Body).Decode(result)+ }+ return nil+}+// GetStream performs a GET request and returns the raw response so the// caller can stream chunks (e.g. SSE log feeds). Caller closes resp.Body.func (c *Client) GetStream(ctx context.Context, path string) (*http.Response, error) {diff --git a/cmd/oc/internal/commands/agent.go b/cmd/oc/internal/commands/agent.goindex c32a110e..3d95c1d8 100644--- a/cmd/oc/internal/commands/agent.go+++ b/cmd/oc/internal/commands/agent.go@@ -173,5 +173,6 @@ var agentCmd = &cobra.Command{func init() {registerAgentCrud()registerAgentDeploy()+ registerAgentSchedules()rootCmd.AddCommand(sessionCmd)}diff --git a/cmd/oc/internal/commands/agent_deploy.go b/cmd/oc/internal/commands/agent_deploy.goindex 90db0d3d..d877ca4e 100644--- a/cmd/oc/internal/commands/agent_deploy.go+++ b/cmd/oc/internal/commands/agent_deploy.go@@ -25,7 +25,17 @@ type manifest struct {Agent struct {ID string `toml:"id"`} `toml:"agent"`- Limits map[string]interface{} `toml:"limits"`+ Limits map[string]interface{} `toml:"limits"`+ Schedules []scheduleDecl `toml:"schedules"` // cron for agents (015); synced on deploy-activate+}++// One agent.toml [[schedules]] entry. Rides the deployment input; the server syncs on activate.+type scheduleDecl struct {+ Name string `toml:"name" json:"name"`+ Cron string `toml:"cron" json:"cron"`+ TZ string `toml:"tz,omitempty" json:"tz,omitempty"`+ Input string `toml:"input" json:"input"`+ Overlap string `toml:"overlap,omitempty" json:"overlap,omitempty"`}type skillFile struct {@@ -268,6 +278,11 @@ var agentDeployCmd = &cobra.Command{if len(skills) > 0 {input["skills"] = skills}+ // [[schedules]] present in agent.toml → carry them so the server syncs on activate (015 §3).+ // Absent → omit the key entirely (no sync; existing schedules untouched).+ if len(m.Schedules) > 0 {+ input["schedules"] = m.Schedules+ }body := map[string]interface{}{"input": input, "activate": !noActivate}if idem, _ := cmd.Flags().GetString("idempotency-key"); idem != "" {body["idempotency_key"] = idemdiff --git a/cmd/oc/internal/commands/agent_schedules.go b/cmd/oc/internal/commands/agent_schedules.gonew file mode 100644index 00000000..749f1f77--- /dev/null+++ b/cmd/oc/internal/commands/agent_schedules.go@@ -0,0 +1,354 @@+package commands++// `oc agent schedule[s]` — cron for agents (design 015). A schedule fires an agent on a cron; each+// firing starts one session on the active revision. Agent addressing follows the house convention:+// --agent <id|name> on every verb, else the cwd agent.toml. Schedules are referenced by NAME (or a+// sch_… id); the name is resolved to an id against the agent.++import (+ "fmt"+ "strings"++ "github.com/opensandbox/opensandbox/cmd/oc/internal/client"+ "github.com/spf13/cobra"+)++// ── Response types (mirror the sessions-api JSON, snake_case) ──++type Schedule struct {+ ID string `json:"id"`+ AgentID string `json:"agent_id"`+ Name string `json:"name"`+ Cron string `json:"cron"`+ TZ *string `json:"tz"`+ Input string `json:"input"`+ Overlap string `json:"overlap"`+ State string `json:"state"`+ NextFireAt string `json:"next_fire_at"`+ LastFiredAt *string `json:"last_fired_at"`+ ConsecutiveFailures int `json:"consecutive_failures"`+ LastError *string `json:"last_error"`+ CreatedAt string `json:"created_at"`+}++type scheduleEnvelope struct {+ Schedule Schedule `json:"schedule"`+}+type scheduleListResp struct {+ Schedules []Schedule `json:"schedules"`+}++type ScheduleRun struct {+ ID string `json:"id"`+ ScheduleID string `json:"schedule_id"`+ ScheduledFor *string `json:"scheduled_for"`+ FiredAt string `json:"fired_at"`+ Outcome string `json:"outcome"`+ SessionID *string `json:"session_id"`+ Error *string `json:"error"`+}+type runEnvelope struct {+ Run ScheduleRun `json:"run"`+}+type runListResp struct {+ Runs []ScheduleRun `json:"runs"`+ NextCursor *string `json:"next_cursor"`+}++// schedStr renders a nullable/empty string field as "-".+func schedStr(p *string) string {+ if p == nil || *p == "" {+ return "-"+ }+ return *p+}++// resolveScheduleID maps a "sch_…" id (used as-is) or a schedule NAME (looked up on the agent) to id.+func resolveScheduleID(cmd *cobra.Command, sc *client.Client, agentID, ref string) (string, error) {+ if strings.HasPrefix(ref, "sch_") {+ return ref, nil+ }+ var list scheduleListResp+ if err := sc.Get(cmd.Context(), "/v3/agents/"+agentID+"/schedules", &list); err != nil {+ return "", err+ }+ for _, s := range list.Schedules {+ if s.Name == ref {+ return s.ID, nil+ }+ }+ return "", fmt.Errorf("no schedule named %q on this agent", ref)+}++var agentSchedulesCmd = &cobra.Command{+ Use: "schedules",+ Short: "List an agent's schedules (cron for agents)",+ Args: cobra.NoArgs,+ RunE: func(cmd *cobra.Command, args []string) error {+ sc, err := sessionsClient(cmd)+ if err != nil {+ return err+ }+ agentID, err := targetAgentID(cmd, sc, nil)+ if err != nil {+ return err+ }+ var resp scheduleListResp+ if err := sc.Get(cmd.Context(), "/v3/agents/"+agentID+"/schedules", &resp); err != nil {+ return err+ }+ printer.Print(resp.Schedules, func() {+ if len(resp.Schedules) == 0 {+ fmt.Println("No schedules.")+ return+ }+ headers := []string{"NAME", "CRON", "TZ", "STATE", "NEXT_FIRE", "FAILS"}+ var rows [][]string+ for _, s := range resp.Schedules {+ rows = append(rows, []string{s.Name, s.Cron, schedStr(s.TZ), s.State, s.NextFireAt, fmt.Sprintf("%d", s.ConsecutiveFailures)})+ }+ printer.Table(headers, rows)+ })+ return nil+ },+}++var agentScheduleCmd = &cobra.Command{+ Use: "schedule",+ Short: "Manage an agent's schedules (create/get/pause/resume/delete/fire/runs)",+}++var agentScheduleCreateCmd = &cobra.Command{+ Use: "create <name>",+ Short: "Create a schedule on an agent",+ Example: " oc agent schedule create morning-sweep --cron \"0 9 * * 1-5\" --input \"Reconcile docs; open a draft PR if anything drifted.\"",+ Args: cobra.ExactArgs(1),+ RunE: func(cmd *cobra.Command, args []string) error {+ sc, err := sessionsClient(cmd)+ if err != nil {+ return err+ }+ agentID, err := targetAgentID(cmd, sc, nil)+ if err != nil {+ return err+ }+ cron, _ := cmd.Flags().GetString("cron")+ input, _ := cmd.Flags().GetString("input")+ tz, _ := cmd.Flags().GetString("tz")+ overlap, _ := cmd.Flags().GetString("overlap")+ if cron == "" {+ return fmt.Errorf("--cron is required")+ }+ if input == "" {+ return fmt.Errorf("--input is required")+ }+ body := map[string]interface{}{"name": args[0], "cron": cron, "input": input}+ if tz != "" {+ body["tz"] = tz+ }+ if overlap != "" {+ body["overlap"] = overlap+ }+ var env scheduleEnvelope+ if err := sc.Post(cmd.Context(), "/v3/agents/"+agentID+"/schedules", body, &env); err != nil {+ return err+ }+ printer.Print(env.Schedule, func() {+ fmt.Printf("Created schedule %s (%s) — next fire %s\n", env.Schedule.Name, env.Schedule.ID, env.Schedule.NextFireAt)+ })+ return nil+ },+}++var agentScheduleGetCmd = &cobra.Command{+ Use: "get <name>",+ Short: "Show a schedule",+ Args: cobra.ExactArgs(1),+ RunE: func(cmd *cobra.Command, args []string) error {+ sc, err := sessionsClient(cmd)+ if err != nil {+ return err+ }+ agentID, err := targetAgentID(cmd, sc, nil)+ if err != nil {+ return err+ }+ sid, err := resolveScheduleID(cmd, sc, agentID, args[0])+ if err != nil {+ return err+ }+ var env scheduleEnvelope+ if err := sc.Get(cmd.Context(), "/v3/agents/"+agentID+"/schedules/"+sid, &env); err != nil {+ return err+ }+ printer.Print(env.Schedule, func() {+ s := env.Schedule+ fmt.Printf("Name: %s\n", s.Name)+ fmt.Printf("ID: %s\n", s.ID)+ fmt.Printf("Cron: %s (%s)\n", s.Cron, schedStr(s.TZ))+ fmt.Printf("State: %s\n", s.State)+ fmt.Printf("Next fire: %s\n", s.NextFireAt)+ fmt.Printf("Last fired: %s\n", schedStr(s.LastFiredAt))+ fmt.Printf("Overlap: %s\n", s.Overlap)+ if s.LastError != nil && *s.LastError != "" {+ fmt.Printf("Last error: %s (failures: %d)\n", *s.LastError, s.ConsecutiveFailures)+ }+ fmt.Printf("Input: %s\n", s.Input)+ })+ return nil+ },+}++// patchPaused PATCHes {paused} and reports the resulting state (pause / resume share this).+func patchPaused(cmd *cobra.Command, args []string, paused bool, verb string) error {+ sc, err := sessionsClient(cmd)+ if err != nil {+ return err+ }+ agentID, err := targetAgentID(cmd, sc, nil)+ if err != nil {+ return err+ }+ sid, err := resolveScheduleID(cmd, sc, agentID, args[0])+ if err != nil {+ return err+ }+ var env scheduleEnvelope+ if err := sc.Patch(cmd.Context(), "/v3/agents/"+agentID+"/schedules/"+sid, map[string]interface{}{"paused": paused}, &env); err != nil {+ return err+ }+ printer.Print(env.Schedule, func() {+ fmt.Printf("%s %s — state %s, next fire %s\n", verb, env.Schedule.Name, env.Schedule.State, env.Schedule.NextFireAt)+ })+ return nil+}++var agentSchedulePauseCmd = &cobra.Command{+ Use: "pause <name>", Short: "Pause a schedule", Args: cobra.ExactArgs(1),+ RunE: func(cmd *cobra.Command, args []string) error { return patchPaused(cmd, args, true, "Paused") },+}+var agentScheduleResumeCmd = &cobra.Command{+ Use: "resume <name>", Short: "Resume a schedule", Args: cobra.ExactArgs(1),+ RunE: func(cmd *cobra.Command, args []string) error { return patchPaused(cmd, args, false, "Resumed") },+}++var agentScheduleDeleteCmd = &cobra.Command{+ Use: "delete <name>", Aliases: []string{"rm"}, Short: "Delete a schedule (run history is kept)", Args: cobra.ExactArgs(1),+ RunE: func(cmd *cobra.Command, args []string) error {+ sc, err := sessionsClient(cmd)+ if err != nil {+ return err+ }+ agentID, err := targetAgentID(cmd, sc, nil)+ if err != nil {+ return err+ }+ sid, err := resolveScheduleID(cmd, sc, agentID, args[0])+ if err != nil {+ return err+ }+ if err := sc.Delete(cmd.Context(), "/v3/agents/"+agentID+"/schedules/"+sid); err != nil {+ return err+ }+ printer.Print(map[string]interface{}{"deleted": args[0]}, func() { fmt.Printf("Deleted schedule %s\n", args[0]) })+ return nil+ },+}++var agentScheduleFireCmd = &cobra.Command{+ Use: "fire <name>", Short: "Test-fire a schedule now (prints the created session)", Args: cobra.ExactArgs(1),+ RunE: func(cmd *cobra.Command, args []string) error {+ sc, err := sessionsClient(cmd)+ if err != nil {+ return err+ }+ agentID, err := targetAgentID(cmd, sc, nil)+ if err != nil {+ return err+ }+ sid, err := resolveScheduleID(cmd, sc, agentID, args[0])+ if err != nil {+ return err+ }+ var env runEnvelope+ if err := sc.Post(cmd.Context(), "/v3/agents/"+agentID+"/schedules/"+sid+"/fire", nil, &env); err != nil {+ return err+ }+ printer.Print(env.Run, func() {+ r := env.Run+ if r.Outcome == "enacted" && r.SessionID != nil {+ fmt.Printf("Fired — session %s\n", *r.SessionID)+ fmt.Printf(" tail it: oc session logs %s\n", *r.SessionID)+ } else {+ fmt.Printf("Fired — outcome %s\n", r.Outcome)+ if r.Error != nil && *r.Error != "" {+ fmt.Printf(" error: %s\n", *r.Error)+ }+ }+ })+ return nil+ },+}++var agentScheduleRunsCmd = &cobra.Command{+ Use: "runs <name>", Short: "Show a schedule's run history (newest first)", Args: cobra.ExactArgs(1),+ RunE: func(cmd *cobra.Command, args []string) error {+ sc, err := sessionsClient(cmd)+ if err != nil {+ return err+ }+ agentID, err := targetAgentID(cmd, sc, nil)+ if err != nil {+ return err+ }+ sid, err := resolveScheduleID(cmd, sc, agentID, args[0])+ if err != nil {+ return err+ }+ limit, _ := cmd.Flags().GetInt("limit")+ path := fmt.Sprintf("/v3/agents/%s/schedules/%s/runs", agentID, sid)+ if limit > 0 {+ path += fmt.Sprintf("?limit=%d", limit)+ }+ var resp runListResp+ if err := sc.Get(cmd.Context(), path, &resp); err != nil {+ return err+ }+ printer.Print(resp.Runs, func() {+ if len(resp.Runs) == 0 {+ fmt.Println("No runs yet.")+ return+ }+ headers := []string{"OUTCOME", "SCHEDULED_FOR", "FIRED", "SESSION", "ERROR"}+ var rows [][]string+ for _, r := range resp.Runs {+ rows = append(rows, []string{r.Outcome, schedStr(r.ScheduledFor), r.FiredAt, schedStr(r.SessionID), schedStr(r.Error)})+ }+ printer.Table(headers, rows)+ })+ return nil+ },+}++func registerAgentSchedules() {+ // Agent addressing: --agent on every verb, else the cwd agent.toml.+ for _, c := range []*cobra.Command{+ agentSchedulesCmd, agentScheduleCreateCmd, agentScheduleGetCmd,+ agentSchedulePauseCmd, agentScheduleResumeCmd, agentScheduleDeleteCmd,+ agentScheduleFireCmd, agentScheduleRunsCmd,+ } {+ c.Flags().String("agent", "", "Agent id or name (else the cwd agent.toml)")+ }+ agentScheduleCreateCmd.Flags().String("cron", "", "5-field cron, e.g. \"0 9 * * 1-5\" (required)")+ agentScheduleCreateCmd.Flags().String("tz", "", "IANA time zone, e.g. Europe/London (optional; UTC if omitted)")+ agentScheduleCreateCmd.Flags().String("input", "", "First user message of every run (required)")+ agentScheduleCreateCmd.Flags().String("overlap", "", "skip (default) | allow")+ agentScheduleRunsCmd.Flags().Int("limit", 0, "Max runs to show")++ agentScheduleCmd.AddCommand(+ agentScheduleCreateCmd, agentScheduleGetCmd, agentSchedulePauseCmd, agentScheduleResumeCmd,+ agentScheduleDeleteCmd, agentScheduleFireCmd, agentScheduleRunsCmd,+ )+ agentCmd.AddCommand(agentSchedulesCmd)+ agentCmd.AddCommand(agentScheduleCmd)+}diff --git a/sdks/typescript/src/agents/agents.ts b/sdks/typescript/src/agents/agents.tsindex 0f67b04f..c45d8372 100644--- a/sdks/typescript/src/agents/agents.ts+++ b/sdks/typescript/src/agents/agents.ts@@ -4,6 +4,7 @@ import {Deployments, Revisions, Activations, Skills, DeploymentSourceResource,type Deployment, type InlineSkillFile,} from "./deployments.js";+import { Schedules } from "./schedules.js";export interface CreateAgentParams {name: string;@@ -68,6 +69,8 @@ export class Agents {readonly skills: Skills;/** Repo linkage for push-to-deploy. */readonly deploymentSource: DeploymentSourceResource;+ /** Cron for agents — schedules that fire a session per slot (015). */+ readonly schedules: Schedules;constructor(private readonly http: Http) {this.deployments = new Deployments(http);@@ -75,6 +78,7 @@ export class Agents {this.activations = new Activations(http);this.skills = new Skills(http);this.deploymentSource = new DeploymentSourceResource(http);+ this.schedules = new Schedules(http);}create(params: CreateAgentParams): Promise<Agent> {diff --git a/sdks/typescript/src/agents/index.ts b/sdks/typescript/src/agents/index.tsindex 403b42ca..22f6a680 100644--- a/sdks/typescript/src/agents/index.ts+++ b/sdks/typescript/src/agents/index.ts@@ -32,6 +32,11 @@ export { Destinations, Deliveries } from "./destinations.js";export type { CreateDestinationParams, UpdateDestinationParams } from "./destinations.js";export { Watches } from "./watches.js";export type { Watch, WatchStatus, WakeOn, CreateWatchParams } from "./watches.js";+export { Schedules } from "./schedules.js";+export type {+ Schedule, ScheduleRun, ScheduleState, RunOutcome, Overlap,+ CreateScheduleParams, UpdateScheduleParams, RunsPage,+} from "./schedules.js";export { verifyWebhook, WebhookVerificationError } from "./webhooks.js";export type { WebhookDelivery, VerifyWebhookOptions } from "./webhooks.js";diff --git a/sdks/typescript/src/agents/schedules.ts b/sdks/typescript/src/agents/schedules.tsnew file mode 100644index 00000000..a0873427--- /dev/null+++ b/sdks/typescript/src/agents/schedules.ts@@ -0,0 +1,87 @@+// Schedules sub-resource of `oc.agents` (design 015) — cron for agents. A schedule fires an agent on+// a cron; each firing starts one session on the active revision with a fixed input. The HTTP layer+// converts camelCase ⟷ snake_case on the wire, so params/results here are camelCase.++import type { Http, Query } from "./http.js";++export type Overlap = "skip" | "allow";+export type ScheduleState = "active" | "paused" | "auto_paused";+export type RunOutcome = "enacted" | "skipped" | "failed";++export interface CreateScheduleParams {+ name: string; // ^[a-z0-9][a-z0-9-]{0,63}$, unique per agent+ cron: string; // 5-field cron (1-minute resolution)+ tz?: string | null; // IANA name; omit for UTC+ input: string; // first user message of every run (≤ 32 KiB)+ overlap?: Overlap; // default "skip"+}++export interface UpdateScheduleParams {+ cron?: string;+ tz?: string | null;+ input?: string;+ overlap?: Overlap;+ paused?: boolean; // true = pause, false = resume (recomputes next fire, resets failures)+}++export interface Schedule {+ id: string; // sch_…+ agentId: string;+ name: string;+ cron: string;+ tz: string | null;+ input: string;+ overlap: Overlap;+ state: ScheduleState;+ nextFireAt: string; // UTC ISO+ lastFiredAt: string | null;+ consecutiveFailures: number;+ lastError: string | null;+ createdAt: string;+ updatedAt: string;+}++export interface ScheduleRun {+ id: string; // srn_…+ scheduleId: string;+ scheduledFor: string | null; // the cron slot; null for a manual fire+ firedAt: string;+ outcome: RunOutcome;+ sessionId: string | null; // chain into oc.sessions.get(sessionId)+ error: string | null;+}++export interface RunsPage { runs: ScheduleRun[]; nextCursor?: string | null; }++/** Cron for agents. Schedules live on the agent — every method takes the agent id first.+ * `oc.agents.schedules.create(agentId, { name, cron, input })`. */+export class Schedules {+ constructor(private readonly http: Http) {}++ create(agentId: string, params: CreateScheduleParams): Promise<{ schedule: Schedule }> {+ return this.http.request("POST", `/agents/${agentId}/schedules`, { body: params });+ }+ list(agentId: string): Promise<{ schedules: Schedule[] }> {+ return this.http.request("GET", `/agents/${agentId}/schedules`);+ }+ get(agentId: string, scheduleId: string): Promise<{ schedule: Schedule }> {+ return this.http.request("GET", `/agents/${agentId}/schedules/${scheduleId}`);+ }+ update(agentId: string, scheduleId: string, params: UpdateScheduleParams): Promise<{ schedule: Schedule }> {+ return this.http.request("PATCH", `/agents/${agentId}/schedules/${scheduleId}`, { body: params });+ }+ delete(agentId: string, scheduleId: string): Promise<void> {+ return this.http.request("DELETE", `/agents/${agentId}/schedules/${scheduleId}`);+ }+ /** Test-fire now — enacts synchronously in any state (paused included), does not advance the cron.+ * Returns the run; branch on `run.outcome` (a failed fire is still a 201 with `outcome:"failed"`). */+ fire(agentId: string, scheduleId: string): Promise<{ run: ScheduleRun }> {+ return this.http.request("POST", `/agents/${agentId}/schedules/${scheduleId}/fire`);+ }+ /** Run history, newest-first. Each run carries `sessionId` to chain into `oc.sessions.get`. */+ runs(agentId: string, scheduleId: string, opts: { cursor?: string; limit?: number } = {}): Promise<RunsPage> {+ return this.http.request("GET", `/agents/${agentId}/schedules/${scheduleId}/runs`, { query: opts as Query });+ }+}++