CodeRunParams
コード実行用のパラメータ。
プロパティ:
argv?string[] - コマンドライン引数env?Record<string, string> - 環境変数
コンストラクタ
new CodeRunParams()
new CodeRunParams(): CodeRunParams戻り値:
CodeRunParams
プロセス
サンドボックス内でのプロセスおよびコード実行を扱います。
コンストラクタ
new Process()
new Process( sandboxId: string, clientConfig: Configuration, codeToolbox: SandboxCodeToolbox, toolboxApi: ToolboxApi, getRootDir: () => Promise<string>): Processパラメータ:
sandboxIdstringclientConfigConfigurationcodeToolboxSandboxCodeToolboxtoolboxApiToolboxApigetRootDir() => Promise<string>
戻り値:
Process
メソッド
codeRun()
codeRun( code: string, params?: CodeRunParams,timeout?: number): Promise<ExecuteResponse>適切な言語ランタイムを用いてサンドボックス内でコードを実行します。
パラメータ:
codestring - 実行するコードparams?CodeRunParams - コード実行用のパラメータtimeout?number - 実行完了を待機する最大時間(秒)
戻り値:
Promise<ExecuteResponse>- 以下を含むコード実行結果:- exitCode: 実行の終了ステータス
- result: コードの標準出力
- artifacts:
stdout(result と同じ)およびcharts(matplotlib のチャートメタデータ)を含む ExecutionArtifacts オブジェクト
例:
// TypeScript コードを実行const response = await process.codeRun(` const x = 10; const y = 20; console.log(\`Sum: \${x + y}\`);`);console.log(response.artifacts.stdout); // 出力: Sum: 30// matplotlib を用いた Python コードを実行const response = await process.codeRun(`import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(0, 10, 30)y = np.sin(x)
plt.figure(figsize=(8, 5))plt.plot(x, y, 'b-', linewidth=2)plt.title('Line Chart')plt.xlabel('X-axis (seconds)')plt.ylabel('Y-axis (amplitude)')plt.grid(True)plt.show()`);
if (response.artifacts?.charts) { const chart = response.artifacts.charts[0];
console.log(`Type: ${chart.type}`); console.log(`Title: ${chart.title}`); if (chart.type === ChartType.LINE) { const lineChart = chart as LineChart console.log('X Label:', lineChart.x_label) console.log('Y Label:', lineChart.y_label) console.log('X Ticks:', lineChart.x_ticks) console.log('Y Ticks:', lineChart.y_ticks) console.log('X Tick Labels:', lineChart.x_tick_labels) console.log('Y Tick Labels:', lineChart.y_tick_labels) console.log('X Scale:', lineChart.x_scale) console.log('Y Scale:', lineChart.y_scale) console.log('Elements:') console.dir(lineChart.elements, { depth: null }) }}createSession()
createSession(sessionId: string): Promise<void>サンドボックス内に新しい長時間実行のバックグラウンドセッションを作成します。
セッションはコマンド間で状態を保持するバックグラウンドプロセスであり、関連する複数のコマンドや永続的な環境設定が必要なシナリオに最適です。長時間実行のコマンドを実行し、プロセスの状態を監視できます。
パラメータ:
sessionIdstring - 新しいセッションの一意の識別子
戻り値:
Promise<void>
例:
// 新しいセッションを作成const sessionId = 'my-session';await process.createSession(sessionId);const session = await process.getSession(sessionId);// 作業を実行...await process.deleteSession(sessionId);deleteSession()
deleteSession(sessionId: string): Promise<void>サンドボックスからセッションを削除します。
パラメータ:
sessionIdstring - 削除するセッションの一意の識別子
戻り値:
Promise<void>
例:
// 完了したセッションをクリーンアップawait process.deleteSession('my-session');executeCommand()
executeCommand( command: string, cwd?: string, env?: Record<string, string>,timeout?: number): Promise<ExecuteResponse>サンドボックスでシェルコマンドを実行します。
Parameters:
commandstring - 実行するシェルコマンドcwd?string - コマンド実行時の作業ディレクトリ。未指定の場合はサンドボックスのルートディレクトリを使用します。 既定値はユーザーのルートディレクトリです。env?Record<string, string> - コマンドに設定する環境変数timeout?number - コマンドの完了を待機する最大時間(秒)。0 は無期限に待機します。
Returns:
Promise<ExecuteResponse>- コマンド実行結果。以下を含む:- exitCode: コマンドの終了ステータス
- result: コマンドの標準出力
- artifacts: ExecutionArtifacts オブジェクト(
stdout(result と同じ)とcharts(matplotlib のチャートメタデータ)を含む)
Examples:
// Simple commandconst response = await process.executeCommand('echo "Hello"');console.log(response.artifacts.stdout); // Prints: Hello// Command with working directoryconst result = await process.executeCommand('ls', 'workspace/src');// Command with timeoutconst result = await process.executeCommand('sleep 10', undefined, 5);executeSessionCommand()
executeSessionCommand( sessionId: string, req: SessionExecuteRequest,timeout?: number): Promise<SessionExecuteResponse>既存のセッションでコマンドを実行します。
Parameters:
sessionIdstring - 使用するセッションの一意の識別子reqSessionExecuteRequest - コマンド実行リクエスト。以下を含む:- command: 実行するコマンド
- runAsync: 非同期で実行するかどうか
timeout?number - タイムアウト(秒)
Returns:
Promise<SessionExecuteResponse>- コマンド実行結果。以下を含む:- cmdId: 実行されたコマンドの一意の識別子
- output: コマンド出力(同期実行の場合)
- exitCode: コマンドの終了ステータス(同期実行の場合)
Example:
// Execute commands in sequence, maintaining stateconst sessionId = 'my-session';
// Change directoryawait process.executeSessionCommand(sessionId, { command: 'cd /home/daytona'});
// Run command in new directoryconst result = await process.executeSessionCommand(sessionId, { command: 'pwd'});console.log(result.output); // Prints: /home/daytonagetSession()
getSession(sessionId: string): Promise<Session>サンドボックス内のセッションを取得します。
Parameters:
sessionIdstring - 取得するセッションの一意の識別子
Returns:
Promise<Session>- セッション情報。以下を含む:- sessionId: セッションの一意の識別子
- commands: セッション内で実行されたコマンドの一覧
Example:
const session = await process.getSession('my-session');session.commands.forEach(cmd => { console.log(`Command: ${cmd.command}`);});getSessionCommand()
getSessionCommand(sessionId: string, commandId: string): Promise<Command>セッションで実行された特定のコマンドに関する情報を取得します。
Parameters:
sessionIdstring - セッションの一意の識別子commandIdstring - コマンドの一意の識別子
Returns:
Promise<Command>- コマンド情報。以下を含む:- id: コマンドの一意の識別子
- command: 実行されたコマンド文字列
- exitCode: コマンドの終了ステータス(完了している場合)
Example:
const cmd = await process.getSessionCommand('my-session', 'cmd-123');if (cmd.exitCode === 0) { console.log(`Command ${cmd.command} completed successfully`);}getSessionCommandLogs()
Call Signature
getSessionCommandLogs(sessionId: string, commandId: string): Promise<string>セッションで実行されたコマンドのログを取得します。
Parameters:
sessionIdstring - セッションの一意の識別子commandIdstring - コマンドの一意の識別子
Returns:
Promise<string>- コマンドのログ
Example:
const logs = await process.getSessionCommandLogs('my-session', 'cmd-123');console.log('Command output:', logs);Call Signature
getSessionCommandLogs( sessionId: string, commandId: string,onLogs: (chunk: string) => void): Promise<void>セッションで実行されたコマンドのログを、利用可能になり次第、非同期で取得・処理します。
パラメーター:
sessionIdstring - セッションの一意の識別子commandIdstring - コマンドの一意の識別子onLogs(chunk: string) => void - 各ログチャンクを処理するコールバック関数
戻り値:
Promise<void>
例:
const logs = await process.getSessionCommandLogs('my-session', 'cmd-123', (chunk) => { console.log('Log chunk:', chunk);});listSessions()
listSessions(): Promise<Session[]>サンドボックス内のすべてのアクティブなセッションを一覧表示します。
戻り値:
Promise<Session[]>- アクティブなセッションの配列
例:
const sessions = await process.listSessions();sessions.forEach(session => { console.log(`Session ${session.sessionId}:`); session.commands.forEach(cmd => { console.log(`- ${cmd.command} (${cmd.exitCode})`); });});