Skip to content

Conversation

@sathwik-14
Copy link
Contributor

The helper does interval based polling for checking agent rediness.

This is needed to avoid running tight loops & respect API rate limits

Example

const agent = await waitForAgentReady(client, 'agent-123', {
timeout: 60000, // 1 minute
onUpdate: (status, elapsed) => {
console.log(`Status: ${status} (${elapsed}ms elapsed)`);
}
});

Edge Cases Handled

  • Graceful Cancellation: Supports AbortSignal for canceling long-running polls
  • Timeout Protection: Prevents infinite polling with configurable timeout (default: 3 minutes), checking before each API call to respect time bounds
  • Deployment Failures: Detects terminal failure states (STATUS_FAILED, STATUS_UNDEPLOYMENT_FAILED) and throws structured errors
  • Transitional States: Continues polling through intermediate deployment states without false positives
  • Network Errors: API failures propagate to caller, allowing custom retry/error handling strategies
  • Structured Error Types: Custom AgentTimeoutError and AgentDeploymentError classes enable type-safe error handling with accessible metadata
  • Concurrent Polling: Stateless implementation supports multiple independent polls of the same agent
  • Progress Monitoring: Optional onUpdate callback provides real-time status updates for UI/logging integration

Copy link
Collaborator

@bbatha bbatha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a solid implementation. However please implement it closer to the style of this repo. The #8 is a good reference.

That means that this should be a method on the Agent client class so that it can be used in a fluent style.

Comment on lines 19 to 20
/** Optional callback for progress updates */
onUpdate?: (status: AgentStatus, elapsed: number) => void;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop this

| 'STATUS_UNDEPLOYMENT_FAILED'
| 'STATUS_DELETED';

export interface WaitForAgentOptions {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include the request options

@@ -0,0 +1,91 @@
import Gradient from "@digitalocean/gradient";

export type AgentStatus =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should already be defined in the agent file

- Converted waitForAgentReady to a method on Agent client for fluent usage
- Removed optional onUpdate callback as per review
- Reuse existing AgentStatus type from agents.ts
- Include request options when calling retrieve() API
- Updated sleep helper usage for polling interval

Resolves digitalocean#4
@sathwik-14 sathwik-14 requested a review from bbatha October 21, 2025 10:43
@bbatha
Copy link
Collaborator

bbatha commented Oct 22, 2025

This looks great can you just make sure lints and tests pass then I'll merge it

@bbatha bbatha merged commit 48a9366 into digitalocean:main Oct 22, 2025
5 of 6 checks passed
@bbatha
Copy link
Collaborator

bbatha commented Oct 22, 2025

Resolves #4

@stainless-app stainless-app bot mentioned this pull request Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants