-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexecution.ts
More file actions
109 lines (99 loc) · 3.1 KB
/
Copy pathexecution.ts
File metadata and controls
109 lines (99 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Runloop } from '../index';
import type * as Core from '../core';
import type { DevboxAsyncExecutionDetailView } from '../resources/devboxes/devboxes';
import type { PollingOptions } from '../lib/polling';
import { ExecutionResult } from './execution-result';
/**
* Execution object for tracking async command execution with streaming support.
*/
export class Execution {
private client: Runloop;
private _devboxId: string;
private _executionId: string;
private _initialResult: DevboxAsyncExecutionDetailView;
private _streamingPromise?: Promise<void>;
constructor(
client: Runloop,
devboxId: string,
executionId: string,
initialResult: DevboxAsyncExecutionDetailView,
streamingPromise?: Promise<void>,
) {
this.client = client;
this._devboxId = devboxId;
this._executionId = executionId;
this._initialResult = initialResult;
if (streamingPromise) {
this._streamingPromise = streamingPromise;
}
}
/**
* Send input to the execution's stdin.
*
* @param input - The input to send
* @param options - Request options
*/
async sendStdIn(input: string, options?: Core.RequestOptions): Promise<void> {
await this.client.devboxes.executions.sendStdIn(
this._devboxId,
this._executionId,
{ text: input },
options,
);
}
/**
* Wait for the execution to complete and return the result.
* If streaming callbacks were provided, also waits for all streams to finish.
*
* @param options - Request options with optional polling configuration
* @returns ExecutionResult with stdout, stderr, and exit code
*/
async result(
options?: Core.RequestOptions & {
polling?: Partial<PollingOptions<DevboxAsyncExecutionDetailView>>;
},
): Promise<ExecutionResult> {
// Wait for both command completion and streaming to finish (using allSettled for robustness)
const results = await Promise.allSettled([
this.client.devboxes.waitForCommand(
this._devboxId,
this._executionId,
{ statuses: ['completed'] },
options,
),
this._streamingPromise || Promise.resolve(),
]);
// Extract command result (throw if it failed, ignore streaming errors)
if (results[0].status === 'rejected') {
throw results[0].reason;
}
const finalResult = results[0].value;
return new ExecutionResult(this.client, this._devboxId, this._executionId, finalResult);
}
/**
* Get the current state of the execution.
*/
async getState(options?: Core.RequestOptions): Promise<DevboxAsyncExecutionDetailView> {
return this.client.devboxes.executions.retrieve(this._devboxId, this._executionId, options);
}
/**
* Kill the execution if it's still running.
*
* @param options - Request options
*/
async kill(options?: Core.RequestOptions): Promise<void> {
await this.client.devboxes.executions.kill(this._devboxId, this._executionId, options);
}
/**
* Get the execution ID.
*/
get executionId(): string {
return this._executionId;
}
/**
* Get the devbox ID.
*/
get devboxId(): string {
return this._devboxId;
}
}