## CodeRunParams

コード実行用のパラメータ。

**プロパティ**:

- `argv?` _string\[\]_ - コマンドライン引数
- `env?` _Record\<string, string\>_ - 環境変数
    

### コンストラクタ

#### new CodeRunParams()

```ts
new CodeRunParams(): CodeRunParams
```

**戻り値**:

- `CodeRunParams`
## プロセス

サンドボックス内でのプロセスおよびコード実行を扱います。

### コンストラクタ

#### new Process()

```ts
new Process(
   sandboxId: string, 
   clientConfig: Configuration, 
   codeToolbox: SandboxCodeToolbox, 
   toolboxApi: ToolboxApi, 
   getRootDir: () => Promise<string>): Process
```

**パラメータ**:

- `sandboxId` _string_
- `clientConfig` _Configuration_
- `codeToolbox` _SandboxCodeToolbox_
- `toolboxApi` _ToolboxApi_
- `getRootDir` _\(\) =\> Promise\<string\>_

**戻り値**:

- `Process`

### メソッド

#### codeRun()

```ts
codeRun(
   code: string, 
   params?: CodeRunParams, 
timeout?: number): Promise<ExecuteResponse>
```

適切な言語ランタイムを用いてサンドボックス内でコードを実行します。

**パラメータ**:

- `code` _string_ - 実行するコード
- `params?` _CodeRunParams_ - コード実行用のパラメータ
- `timeout?` _number_ - 実行完了を待機する最大時間（秒）

**戻り値**:

- `Promise<ExecuteResponse>` - 以下を含むコード実行結果:
    - exitCode: 実行の終了ステータス
    - result: コードの標準出力
    - artifacts: `stdout`（result と同じ）および `charts`（matplotlib のチャートメタデータ）を含む ExecutionArtifacts オブジェクト

**例:**

```ts
// TypeScript コードを実行
const response = await process.codeRun(`
  const x = 10;
  const y = 20;
  console.log(\`Sum: \${x + y}\`);
`);
console.log(response.artifacts.stdout);  // 出力: Sum: 30
```

```ts
// matplotlib を用いた Python コードを実行
const response = await process.codeRun(`
import matplotlib.pyplot as plt
import 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()

```ts
createSession(sessionId: string): Promise<void>
```

サンドボックス内に新しい長時間実行のバックグラウンドセッションを作成します。

セッションはコマンド間で状態を保持するバックグラウンドプロセスであり、関連する複数のコマンドや永続的な環境設定が必要なシナリオに最適です。長時間実行のコマンドを実行し、プロセスの状態を監視できます。

**パラメータ**:

- `sessionId` _string_ - 新しいセッションの一意の識別子

**戻り値**:

- `Promise<void>`

**例:**

```ts
// 新しいセッションを作成
const sessionId = 'my-session';
await process.createSession(sessionId);
const session = await process.getSession(sessionId);
// 作業を実行...
await process.deleteSession(sessionId);
```

***

#### deleteSession()

```ts
deleteSession(sessionId: string): Promise<void>
```

サンドボックスからセッションを削除します。

**パラメータ**:

- `sessionId` _string_ - 削除するセッションの一意の識別子

**戻り値**:

- `Promise<void>`

**例:**

```ts
// 完了したセッションをクリーンアップ
await process.deleteSession('my-session');
```

***

#### executeCommand()

```ts
executeCommand(
   command: string, 
   cwd?: string, 
   env?: Record<string, string>, 
timeout?: number): Promise<ExecuteResponse>
```

サンドボックスでシェルコマンドを実行します。

**Parameters**:

- `command` _string_ - 実行するシェルコマンド
- `cwd?` _string_ - コマンド実行時の作業ディレクトリ。未指定の場合はサンドボックスのルートディレクトリを使用します。
    既定値はユーザーのルートディレクトリです。
- `env?` _Record\<string, string\>_ - コマンドに設定する環境変数
- `timeout?` _number_ - コマンドの完了を待機する最大時間（秒）。0 は無期限に待機します。

**Returns**:

- `Promise<ExecuteResponse>` - コマンド実行結果。以下を含む:
    - exitCode: コマンドの終了ステータス
    - result: コマンドの標準出力
    - artifacts: ExecutionArtifacts オブジェクト（`stdout`（result と同じ）と `charts`（matplotlib のチャートメタデータ）を含む）

**Examples:**

```ts
// Simple command
const response = await process.executeCommand('echo "Hello"');
console.log(response.artifacts.stdout);  // Prints: Hello
```

```ts
// Command with working directory
const result = await process.executeCommand('ls', 'workspace/src');
```

```ts
// Command with timeout
const result = await process.executeCommand('sleep 10', undefined, 5);
```

***

#### executeSessionCommand()

```ts
executeSessionCommand(
   sessionId: string, 
   req: SessionExecuteRequest, 
timeout?: number): Promise<SessionExecuteResponse>
```

既存のセッションでコマンドを実行します。

**Parameters**:

- `sessionId` _string_ - 使用するセッションの一意の識別子
- `req` _SessionExecuteRequest_ - コマンド実行リクエスト。以下を含む:
    - command: 実行するコマンド
    - runAsync: 非同期で実行するかどうか
- `timeout?` _number_ - タイムアウト（秒）

**Returns**:

- `Promise<SessionExecuteResponse>` - コマンド実行結果。以下を含む:
    - cmdId: 実行されたコマンドの一意の識別子
    - output: コマンド出力（同期実行の場合）
    - exitCode: コマンドの終了ステータス（同期実行の場合）

**Example:**

```ts
// Execute commands in sequence, maintaining state
const sessionId = 'my-session';

// Change directory
await process.executeSessionCommand(sessionId, {
  command: 'cd /home/daytona'
});

// Run command in new directory
const result = await process.executeSessionCommand(sessionId, {
  command: 'pwd'
});
console.log(result.output);  // Prints: /home/daytona
```

***

#### getSession()

```ts
getSession(sessionId: string): Promise<Session>
```

サンドボックス内のセッションを取得します。

**Parameters**:

- `sessionId` _string_ - 取得するセッションの一意の識別子

**Returns**:

- `Promise<Session>` - セッション情報。以下を含む:
    - sessionId: セッションの一意の識別子
    - commands: セッション内で実行されたコマンドの一覧

**Example:**

```ts
const session = await process.getSession('my-session');
session.commands.forEach(cmd => {
  console.log(`Command: ${cmd.command}`);
});
```

***

#### getSessionCommand()

```ts
getSessionCommand(sessionId: string, commandId: string): Promise<Command>
```

セッションで実行された特定のコマンドに関する情報を取得します。

**Parameters**:

- `sessionId` _string_ - セッションの一意の識別子
- `commandId` _string_ - コマンドの一意の識別子

**Returns**:

- `Promise<Command>` - コマンド情報。以下を含む:
    - id: コマンドの一意の識別子
    - command: 実行されたコマンド文字列
    - exitCode: コマンドの終了ステータス（完了している場合）

**Example:**

```ts
const cmd = await process.getSessionCommand('my-session', 'cmd-123');
if (cmd.exitCode === 0) {
  console.log(`Command ${cmd.command} completed successfully`);
}
```

***

#### getSessionCommandLogs()

##### Call Signature

```ts
getSessionCommandLogs(sessionId: string, commandId: string): Promise<string>
```

セッションで実行されたコマンドのログを取得します。

**Parameters**:

- `sessionId` _string_ - セッションの一意の識別子
- `commandId` _string_ - コマンドの一意の識別子

**Returns**:

- `Promise<string>` - コマンドのログ

**Example:**

```ts
const logs = await process.getSessionCommandLogs('my-session', 'cmd-123');
console.log('Command output:', logs);
```

##### Call Signature

```ts
getSessionCommandLogs(
   sessionId: string, 
   commandId: string, 
onLogs: (chunk: string) => void): Promise<void>
```

セッションで実行されたコマンドのログを、利用可能になり次第、非同期で取得・処理します。

**パラメーター**:

- `sessionId` _string_ - セッションの一意の識別子
- `commandId` _string_ - コマンドの一意の識別子
- `onLogs` _\(chunk: string\) =\> void_ - 各ログチャンクを処理するコールバック関数

**戻り値**:

- `Promise<void>`

**例:**

```ts
const logs = await process.getSessionCommandLogs('my-session', 'cmd-123', (chunk) => {
  console.log('Log chunk:', chunk);
});
```

***

#### listSessions()

```ts
listSessions(): Promise<Session[]>
```

サンドボックス内のすべてのアクティブなセッションを一覧表示します。

**戻り値**:

- `Promise<Session[]>` - アクティブなセッションの配列

**例:**

```ts
const sessions = await process.listSessions();
sessions.forEach(session => {
  console.log(`Session ${session.sessionId}:`);
  session.commands.forEach(cmd => {
    console.log(`- ${cmd.command} (${cmd.exitCode})`);
  });
});
```
