- 开发工具
- 代码编辑器
- 数据科学
【免费下载链接】positron
Positron, a next-generation data science IDE
导读
ExecuteRequest是 Positron(下一代数据科学 IDE)中 Kallichore 会话服务对外暴露的核心请求模型,负责描述"在某个运行时会话中执行一段代码"所需的全部参数。本文将基于 ExecuteRequest 模型文档,结合仓库内生成的 OpenAPI 客户端与 supervisor 源码,完整讲解该模型的五个字段、executeCode端点调用方式、返回结果ExecuteReply的解析,以及它在 Positron Runtime API 到 Jupyter 协议之间的实际映射原理。读完本文,你将能够直接调用 Kallichore 客户端执行代码,并理解silent、store_history、stop_on_error、timeout_seconds在真实执行链路中的语义。
ExecuteRequest 是什么
ExecuteRequest是一个描述"在会话中执行代码"的请求对象。它由 Kallichore API 定义生成,位于 extensions/positron-supervisor/src/kcclient/api.ts 中。整个kcclient目录是通过 OpenAPI generator 的typescript-axios模式从 Kallichore 的 API 定义(kallichore.json)自动生成的客户端库,客户端 README 明确说明这些文件不应手工编辑,改动需回归 OpenAPI 定义并重新运行代码生成脚本。
属性一览:五个字段的完整语义
原始文档给出了完整的属性表,这里逐字段展开说明:
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
code | string | 是 | 无 | 要执行的代码正文 |
silent | boolean | 否 | false | 若为true,指示内核静默执行:不在 iopub 上广播、不产生execute_result、execution_count不递增 |
store_history | boolean | 否 | true | 若为true(默认),代码会存入内核历史;临时执行可设为false |
stop_on_error | boolean | 否 | true | 若为true(默认),出错时中止执行队列;为false时,即使本条执行失败,队列中的后续执行请求仍会被处理 |
timeout_seconds | number | 否 | 无 | 等待执行完成的最大秒数;不指定则请求会一直阻塞直到执行结束 |
对应的 TypeScript 接口定义位于 api.ts#L244-L265:
export interface ExecuteRequest { /** * The code to execute */ 'code': string; /** * If true, signals the kernel to execute quietly: no broadcast on iopub, * no execute_result, and the execution_count is not incremented. Defaults to false. */ 'silent'?: boolean; /** * If true (default), the code is stored in the kernel\'s history. * Set to false for throwaway executions. */ 'store_history'?: boolean; /** * If true (default), abort the execution queue on error. If false, queued * execute requests will still be processed even if this one fails. */ 'stop_on_error'?: boolean; /** * Maximum number of seconds to wait for execution to complete. If not * specified, the request will block indefinitely until execution finishes. */ 'timeout_seconds'?: number; }code:唯一必填字段
code是要在目标会话内核中执行的源代码字符串,可以是单行表达式,也可以是多行脚本。请求发出后,内核会像用户在交互式控制台里输入一样执行这段代码。
silent:静默执行的适用场景
当silent为true时,执行过程不会产生面向用户的输出流:不向 iopub 频道广播消息、不产生execute_result结果对象、内核的execution_count计数器也不会递增。这适合"后台探针式"执行——例如检查某个变量是否存在、探测环境状态等不希望污染用户界面的操作。
store_history:是否写入内核历史
默认true,代码会被记录进内核的历史记录,用户后续可以通过上下方向键回溯。对于临时的、一次性丢弃型执行(throwaway executions),可以设置为false,避免历史被无意义代码淹没。
stop_on_error:错误与执行队列的关系
该字段控制错误发生后整个执行队列的走向。Kallichore 的会话层维护着一个执行队列(见 ExecutionQueue 模型文档,包含active、length、pending三个字段):
stop_on_error = true(默认):一旦当前请求执行出错,立即中止整个执行队列,后续排队中的请求不再执行;stop_on_error = false:当前请求失败不影响后续请求,队列继续推进。
timeout_seconds:超时控制
指定最大等待秒数。若不指定,请求会无限期阻塞直到执行完成。指定后,若超过时限仍未完成,服务端会以 HTTP 408(Execution timed out)响应——该状态码在 DefaultApi 文档的 executeCode 响应表中有明确记录。
构造与调用示例
原始文档给出了模型构造示例,这里扩展为完整的端到端调用:
import { ExecuteRequest } from './api'; const instance: ExecuteRequest = { code: 'import pandas as pd\ndf = pd.DataFrame({"a": [1, 2, 3]})\ndf.head()', silent: false, // 默认 false:正常广播输出 store_history: true, // 默认 true:写入内核历史 stop_on_error: true, // 默认 true:出错即中止队列 timeout_seconds: 120, // 可选的超时;不填则无限阻塞 };该对象作为请求体,通过DefaultApi.executeCode(sessionId, executeRequest)发送(详见 DefaultApi.md#executeCode):
import { DefaultApi, Configuration, ExecuteRequest } from './api'; const configuration = new Configuration(); const apiInstance = new DefaultApi(configuration); let sessionId: string; // 目标会话的 ID let executeRequest: ExecuteRequest; // 上述构造的请求对象 const { status, data } = await apiInstance.executeCode( sessionId, executeRequest );从 api.ts#L918-L949 可以看出,executeCode实际发起的是POST /sessions/{session_id}/execute请求,Content-Type为application/json,请求体即序列化后的ExecuteRequest。
响应模型 ExecuteReply 与状态码
执行完成后返回的是ExecuteReply(定义见 api.ts#L203-L239),包含:
status:ok或error(枚举ExecuteReplyStatusEnum);execution_count:内核执行计数器;output:执行过程中按顺序产生的全部输出消息;data(可选):来自execute_result的 MIME 键控字典(执行产生结果值时才有);error_name/error_message/error_traceback(可选):执行失败时的错误信息。
executeCode端点的 HTTP 状态码语义:
| 状态码 | 说明 |
|---|---|
200 | 执行完成 |
400 | 非法请求 |
401 | 未授权 |
404 | 会话不存在 |
408 | 执行超时 |
源码纵深:supervisor 如何把 Runtime API 翻译成 ExecuteRequest
在 Positron 的实际调用链中,ExecuteRequest并非由扩展直接构造,而是由 supervisor 的会话层从 Positron Runtime API 的执行参数转换而来。核心实现在 KallichoreSession.ts#L908-L979:
const request: JupyterExecuteRequest = { code, silent: mode === positron.RuntimeCodeExecutionMode.Silent, store_history: interactiveLike, user_expressions: {}, allow_stdin: true, stop_on_error: errorBehavior === positron.RuntimeErrorBehavior.Stop, };这里可以看到三个关键映射关系:
silent← 执行模式(RuntimeCodeExecutionMode):当模式为Silent时置true。完整枚举定义在 extHostTypes.ts#L3153-L3185:Interactive、NonInteractive、Transient、Silent四种模式,其中只有Silent不展示给用户、不写入历史。store_history← 交互模式:Interactive和Unprocessed(Flow 2 代码片段)会写入历史,其余模式不写。stop_on_error← 错误行为(RuntimeErrorBehavior):Stop时置true,Continue时置false(枚举见 extHostTypes.ts#L3190-L3196)。
值得一提的是,supervisor 层的execute()返回的 Promise 只表示请求已被接受并派发,而不是执行完成——因为内核只有在代码产生结果值时才回发execute_result,对赋值语句、print(...)等无值语句 await 回复会永久挂起(见 KallichoreSession.ts 源码注释)。这与文档中timeout_seconds"不指定则无限阻塞" 的语义相互印证,实际集成时应在客户端侧自行管理超时。
与 Jupyter 协议的对应关系
底层消息实际走的是 Jupyter 协议。supervisor 定义了 jupyter/ExecuteRequest.ts 中的JupyterExecuteRequest接口(含code、silent、store_history、user_expressions、allow_stdin、stop_on_error及 Positron 扩展字段positron),并通过 JupyterMessageType.ts 中的execute_request消息类型发送到 Shell 频道。Kallichore 服务端的ExecuteRequest模型正是对这套 Jupyter 语义的 REST 化封装,二者字段一一对应。
实战建议
- 静默执行:探测环境或后台检查时使用
silent: true,避免干扰用户输出与执行计数; - 临时执行:一次性辅助代码建议
store_history: false,保持内核历史干净; - 批量执行:若希望错误不阻断后续任务,设
stop_on_error: false;若希望失败快速失败、避免连锁错误,保持默认true; - 超时保护:长任务务必设置
timeout_seconds,并处理408响应;同步等待场景注意客户端自身也应设置请求级超时兜底; - 错误解析:
status: 'error'时从error_name、error_message、error_traceback三个字段提取结构化错误信息,output数组则按顺序保留了完整的 stdout/stderr 与展示输出。
相关文档导航
- 客户端库总览(生成说明与注意事项)
- executeCode 端点完整文档
- 执行回复模型 ExecuteReply
- 执行队列模型 ExecutionQueue
- 会话模型总列表
- 开发工具
- 代码编辑器
- 数据科学
【免费下载链接】positron
Positron, a next-generation data science IDE
相关推荐
Positron Supervisor 中的 ExecuteReply:Kallichore 代码执行回执的数据结构全解析
Positron Supervisor 中的 ExecuteReply:Kallichore 代码执行回执的数据结构全解析 导读 ExecuteReply 是
开发工具代码编辑器数据科学使用 Dart Analyzer 包执行代码分析:Analysis Context 与会话机制完全指南
使用 Dart Analyzer 包执行代码分析:Analysis Context 与会话机制完全指南 本篇指南以 pkg/analyzer 官方教程 Perf
编程语言编译器语言运行时标准库开发工具Positron Supervisor 执行输出模型 ExecuteOutput 完全指南:Kallichore 客户端三类输出消息解析
Positron Supervisor 执行输出模型 ExecuteOutput 完全指南:Kallichore 客户端三类输出消息解析 导读 本文围绕 Pos
开发工具代码编辑器数据科学
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考