Explicit text and image output in PTC
PTC 显式文本与图片输出
PTC lets a program acquire and filter intermediate tool results before choosing what the model sees. A final expression or return value can expose incidental data, while automatically attaching every acquired image defeats that selection. Separate screenshot bytes and image-output callbacks also make programs convert images before comparing or displaying them.
English
Problem
PTC lets a program acquire and filter intermediate tool results before choosing what the model sees. A final expression or return value can expose incidental data, while automatically attaching every acquired image defeats that selection. Separate screenshot bytes and image-output callbacks also make programs convert images before comparing or displaying them.
Decision
TypeScript run_code programs select text, objects and images through top-level inspect(...values). Arguments are rendered independently: strings appear directly, and other values use Node's object formatting without depth, array-length or string-length preview limits. Inspection displays undefined properties, Error objects and circular references; requiring lossless JSON for display would interrupt subsequent work after tools have already executed. Images can occur inside arrays or objects. The runtime Image's custom inspection hook collects images reached by Node's formatter; each formatting call owns its collector. One output sequence preserves the relationship between explanatory text and selected images.
The PTC tool registry requests discardCompletion: true, so completion values are discarded before JSON validation and serialization. This is a consumer choice: direct runtime and workflow consumers retain completion values when their protocols require them. The PTC foundation owns dispatch and settlement. The typed-return decision continues to own ordinary JSON binding values, failures and output accounting; this note owns TypeScript output selection and image projection.
The Node environment supplies inspect alongside the complete native console. Native console methods remain available to programs and imported libraries, and their stdout and stderr writes share the captured output sequence. Model instructions direct programs to select output through inspect.
read_image and CUA screenshot bindings expose stored Image values. Readonly attachmentId, path, width and height identify the stored image and its readable execution-world file. The dimensions describe that file's pixels, including the image used for CUA coordinate mapping. The CUA object API returns screenshot Images and text observations directly to the program. The program passes selected values to inspect for display.
The host carries the attachment reference with the program-readable path. The runtime transports image metadata and keeps the attachment reference behind a process-owned image ID. Inspection resolves that ID directly to the registered reference. Image identity is established by the runtime; matching object properties alone do not grant access to an image output. Retained program Images follow their owning process's lifetime.
Nested tool dispatches retain their complete rendered content in the Session log. Successful outer run_code results interleave inspected images with text in emission order, where model history and UI rendering use the same durable attachment references. Successful outer-result replacement remains authoritative over the selected output.
Alternatives considered
Display the completion value alongside printed output. The last expression or return then controls context admission as well as program control flow. Discarding it before validation lets a program finish with an arbitrary internal value while explicitly selecting a smaller visible result.
Automatically forward every acquired image. Acquisition then implies model-context cost, even when the program needs several screenshots to select one. Explicit inspection gives text and images the same selection rule.
Keep a CUA byte-returning facade and image sink. This maintains a separate screenshot representation and a second image-emission path. Returning the runtime's Image lets file reads and screenshots share inspection and preserves the stored attachment reference through display.
Discover images with a separate object traversal. A separate traversal must duplicate Node's handling of collections, symbols and custom formatters and can emit images absent from the text representation. The Image hook ties collection to Node's formatting.
Bounded object previews. Item and string preview limits can omit file contents that a program explicitly selected. Removing those limits keeps selected contents complete within the shared output budget.
Restrict console availability. Missing bindings and incomplete replacements can break program code and imported libraries. The native console preserves Node methods while prompt guidance directs output selection through inspect.
Consequences
Programs explicitly choose the output needed for the next model request, including image comparisons and selected nested results. The upgrade guide owns caller migration. Acquiring an image still performs storage and processing work; selection controls its presentation to the model.
The runtime owns process-local image identity and output ordering; the PTC tool registry owns attachment projection and durable result content. Text and image-reference metadata share the outer output budget. Runtime teardown releases retained image identities, while stored attachments follow the attachment store's lifetime.
中文
问题
PTC 允许程序先获取和筛选中间工具结果,再选择模型看到的内容。末尾表达式或返回值可能暴露偶然产生的数据,而自动附加每张已获取的图片会使这种筛选失效。独立的截图字节和图片输出回调也要求程序先转换图片,再进行比较或展示。
决策
TypeScript run_code 程序通过顶层 inspect(...values) 选择文本、对象和图片。各参数独立渲染:字符串直接展示,其他值使用 Node 的对象格式,不限制预览深度、数组项数或字符串长度。inspect 展示 undefined 属性、Error 对象和循环引用;要求展示值必须是无损 JSON,会在工具已经执行后中断后续工作。图片也可以嵌入数组或对象。运行时 Image 的自定义 inspect 钩子收集 Node 格式化函数访问到的图片;每次格式化调用拥有自己的收集器。同一输出序列保留说明文字与所选图片的对应关系。
PTC 工具注册表请求 discardCompletion: true,在 JSON 校验和序列化前丢弃完成值。这是消费方的选择:直接运行时调用方和工作流消费方在其协议需要时保留完成值。PTC 基础负责分发与结算。类型化返回值决策继续负责普通 JSON 绑定值、失败和输出计量;本记录负责 TypeScript 输出选择与图片投影。
Node 环境同时提供 inspect 和完整的原生 console。程序与导入的库均可使用原生 console 方法,其 stdout 和 stderr 写入共用捕获输出序列。模型指引要求程序通过 inspect 选择输出。
read_image 和 CUA 截图绑定提供已存储的 Image 值。只读的 attachmentId、path、width 和 height 标识已存储图片及其在执行环境中可读取的文件。尺寸描述该文件的像素,也对应 CUA 坐标换算所用的图片。CUA 对象 API将截图的 Image 和观察文本直接返回给程序。程序将选中的值传给 inspect 展示。
宿主将附件引用与程序可读取的路径一起传递。运行时传输图片元数据,并用进程拥有的图片 ID 保存对应的附件引用。展示时,该 ID 直接解析为已登记的引用。图片身份由运行时建立,仅有相同对象属性不能取得图片输出权限。保留在程序中的 Image 跟随其所属进程的生命周期。
嵌套工具分发将完整渲染内容保留在 Session 日志中。成功的外层 run_code 结果按输出顺序穿插显式展示的图片和文本,模型历史和 UI 渲染使用相同的持久附件引用。成功外层结果的替换仍对所选输出具有最终决定权。
考虑过的替代方案
同时展示完成值与打印输出。 末尾表达式或返回值会同时控制上下文纳入和程序控制流。在校验前丢弃完成值,使程序可以用任意内部值结束,并显式选择更小的可见结果。
自动转发每张已获取的图片。 即使程序需要先获取多张截图才能选出一张,获取动作本身也会产生模型上下文成本。显式展示让文本和图片遵循同一选择规则。
保留返回字节的 CUA 对象接口和图片输出回调。 这会维护独立的截图表示和第二条图片输出路径。返回运行时 Image,使文件读取和截图共用展示方式,并在展示过程中保留已存储附件引用。
单独遍历对象以发现图片。 独立遍历必须重复实现 Node 对集合、Symbol 和自定义格式化的处理,还可能输出文本表示中未出现的图片。Image 钩子让收集跟随 Node 的格式化。
限制对象预览。 条目和字符串的预览上限可能省略程序已明确选中的文件内容。取消这些限制,在共享输出预算内完整保留选中的内容。
限制 console 的可用性。 缺失的绑定和不完整的替代实现可能使程序及导入的库出错。原生 console 保留 Node 方法,提示词指引程序通过 inspect 选择输出。
后果
程序显式选择下一次模型请求需要的输出,包括图片比较和嵌套结果中的所选内容。升级指南负责调用方迁移。获取图片仍会执行存储和处理工作,选择控制的是向模型展示的内容。
运行时负责进程内图片身份和输出排序;PTC 工具注册表负责附件投影和持久结果内容。文本和图片引用元数据共用外层输出预算。运行时拆卸释放保留的图片身份,已存储附件则遵循附件存储的生命周期。