Skip to content

非公式 / UnofficialCloudflare, Inc. 非提携。アカウント情報は入力しないでください。Not affiliated with Cloudflare, Inc. Do not enter Cloudflare credentials here.Official docsこのサイトについてプライバシー

インタープリター

最終更新 Markdown で表示Agent セットアップ

メソッドは、Sandbox のサブクラスに withInterpreter を付けたあと、sandbox.interpreter 上にあります。メソッド名は安定版のインタープリターと同じです。runCode はプレーンなシリアライズ可能なデータを返します。取り付けと初回実行は コードインタープリター を参照してください。

createCodeContext()

createCodeContext(options?: CreateContextOptions): Promise<CodeContext>

CreateContextOptions

createCodeContext は次のオプションを受け取ります。

フィールド 型 説明
language "python" | "javascript" | "typescript" インタープリターの言語。デフォルト: python。
cwd string 作業ディレクトリ。デフォルト: /workspace。

CodeContext

作成したコンテキストには次のフィールドがあります。

フィールド 型 説明
id string 現在のコンテナ内のコンテキスト ID
language string コンテキストの言語
cwd string 作業ディレクトリ
createdAt Date 作成日時
lastUsed Date 最終使用日時

runCode()

runCode(code: string, options?: RunCodeOptions): Promise<ExecutionResult>

RunCodeOptions

runCode は次のオプションを受け取ります。コールバックのフィールドは runCode にだけ適用されます。

フィールド 型 説明
context CodeContext 使うコンテキスト。省略した場合は、language 向けのデフォルトコンテキストを使います。
language "python" | "javascript" | "typescript" デフォルトコンテキストの作成または選択時に使います。デフォルト: python。
onStdout (output: OutputMessage) => void | Promise<void> 実行中の stdout チャンクごとに呼び出されます
onStderr (output: OutputMessage) => void | Promise<void> 実行中の stderr チャンクごとに呼び出されます
onResult (result: ResultData) => void | Promise<void> リッチな結果(プレーンデータ)ごとに呼び出されます
onError (error: ExecutionError) => void | Promise<void> 実行エラーが報告されたときに呼び出されます

OutputMessage

interface OutputMessage {
	text: string;
	timestamp: number;
}

ExecutionResult

interface ExecutionResult {
	code: string;
	logs: {
		stdout: string[];
		stderr: string[];
	};
	error?: ExecutionError;
	executionCount?: number;
	results: ResultData[];
}

ResultData には、ランタイムが生成した場合、text、html、png、jpeg、svg、latex、markdown、json、chart などのプレーンなフィールドが含まれることがあります。

ExecutionError

interface ExecutionError {
	name: string;
	message: string;
	traceback: string[];
	lineNumber?: number;
}

runCodeStream()

runCodeStream(
	code: string,
	options?: RunCodeOptions,
): Promise<ReadableStream<Uint8Array>>

実行イベントの SSE バイトストリームを返します。TypeScript の型は context と language について RunCodeOptions を再利用しますが、ストリーム経路では onStdout、onStderr、onResult、onError は呼び出しません。代わりに SSE 本文を消費してください。ストリームをキャンセルすると、実行中のランが中断されることがあります。

listCodeContexts()

listCodeContexts(): Promise<CodeContext[]>

deleteCodeContext()

deleteCodeContext(contextId: string): Promise<void>

エラー

インタープリターの失敗は、InterpreterNotReadyError、ContextNotFoundError、CodeExecutionError として表面化することがあります。Errors API と エラーと復旧 を参照してください。

Python には -python コンテナイメージバリアントが必要です。Worker パッケージとコンテナイメージは、同じプレビュー系統からデプロイしてください。

関連情報

役に立ちましたか?