Skip to content

Conversation

@x22x22
Copy link

@x22x22 x22x22 commented Oct 14, 2025

This commit introduces a new RFC document that outlines the structured input/output capabilities for the Qwen-Code CLI. It includes the addition of --input-format and --output-format flags, detailing the supported formats (text, stream-json, stream-chunk-json), and describes the integration scenarios, design goals, and error semantics. The document aims to facilitate programmatic integration with third-party systems and improve the overall automation experience.

TLDR

Dive Deeper

Reviewer Test Plan

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - -
Seatbelt - -

Linked issues / bugs

This commit introduces a new RFC document that outlines the structured input/output capabilities for the Qwen-Code CLI. It includes the addition of `--input-format` and `--output-format` flags, detailing the supported formats (`text`, `stream-json`, `stream-chunk-json`), and describes the integration scenarios, design goals, and error semantics. The document aims to facilitate programmatic integration with third-party systems and improve the overall automation experience.

Signed-off-by: x22x22 <[email protected]>
@x22x22 x22x22 changed the title Add RFC for Qwen-Code CLI Output Formats and IPC Stream JSON Capability WIP:Add RFC for Qwen-Code CLI Output Formats and IPC Stream JSON Capability Oct 14, 2025
@x22x22
Copy link
Author

x22x22 commented Oct 14, 2025

@pomelo-nwu @Mingholy

#795

我们在这里聊吧,这样方便看文档,这个PR不用合并。

Let's chat here. It's more convenient to view the documents this way. This PR doesn't need to be merged.

qwen-code-cli-output-format-stream-json-rfc_cn.md

qwen-code-cli-output-format-stream-json-rfc_en

@github-actions github-actions bot added bug status/need-information More information is needed to resolve this issue. labels Oct 14, 2025
x22x22 added 11 commits October 14, 2025 19:36
- Introduced structured input/output capabilities at the CLI layer to support third-party integrations.
- Added `--input-format` and `--output-format` options with support for `text`, `stream-json`, and `stream-chunk-json`.
- Defined JSON Lines output, error semantics, and session metadata.
- Enhanced the heartbeat mechanism for monitoring subprocess health.
- Implemented real-time interrupt capabilities for cancelling ongoing requests.
- Updated documentation to reflect changes and provide examples for usage.
- Updated the Chinese RFC to include new bidirectional control channel events and detailed event mechanism classification.
- Removed the English RFC file as it is no longer needed.
- Enhanced design requirements for In-Process MCP Server support and clarified event handling for third-party integrations.
- Improved clarity on structured input/output capabilities and error handling semantics.
@x22x22
Copy link
Author

x22x22 commented Oct 15, 2025

https://github.com/QwenLM/qwen-code/blob/185b1e61e500be368f7fbf6e5ed56af9a0f46af0/docs/rfc/qwen-code-agent-framework-rfc_clear_cn.md

https://github.com/QwenLM/qwen-code/blob/185b1e61e500be368f7fbf6e5ed56af9a0f46af0/docs/rfc/qwen-code-cli-output-format-stream-json-rfc_clear_cn.md

@pomelo-nwu @Mingholy

RFC写完了,请帮忙看看,可以拉钉钉群讨论,谢谢!

The RFC is done. Please take a look and we can discuss it in the DingTalk group. Thank you!

@x22x22 x22x22 changed the title WIP:Add RFC for Qwen-Code CLI Output Formats and IPC Stream JSON Capability Add RFC for Qwen-Code CLI Output Formats and IPC Stream JSON Capability Oct 15, 2025
@Mingholy
Copy link
Collaborator

https://github.com/QwenLM/qwen-code/blob/185b1e61e500be368f7fbf6e5ed56af9a0f46af0/docs/rfc/qwen-code-agent-framework-rfc_clear_cn.md

https://github.com/QwenLM/qwen-code/blob/185b1e61e500be368f7fbf6e5ed56af9a0f46af0/docs/rfc/qwen-code-cli-output-format-stream-json-rfc_clear_cn.md

@pomelo-nwu @Mingholy

RFC写完了,请帮忙看看,可以拉钉钉群讨论,谢谢!

The RFC is done. Please take a look and we can discuss it in the DingTalk group. Thank you!

Thanks for your contribution! We'll try to review as soon as possible(hopefully within this week) and start a draft later for the feature so that we can push it faster.

@jliu666
Copy link

jliu666 commented Oct 16, 2025

Hello, when will it be implemented, and when is it expected to be equipped with this ability.

@x22x22
Copy link
Author

x22x22 commented Oct 16, 2025

@pomelo-nwu @Mingholy

我顺便用ai翻译了一份英文版本。完整的文档列表如下:

I incidentally translated it into an English version with AI. The complete list of documents is as follows:

@x22x22
Copy link
Author

x22x22 commented Oct 16, 2025

https://github.com/QwenLM/qwen-code/blob/185b1e61e500be368f7fbf6e5ed56af9a0f46af0/docs/rfc/qwen-code-agent-framework-rfc_clear_cn.md
https://github.com/QwenLM/qwen-code/blob/185b1e61e500be368f7fbf6e5ed56af9a0f46af0/docs/rfc/qwen-code-cli-output-format-stream-json-rfc_clear_cn.md
@pomelo-nwu @Mingholy

RFC写完了,请帮忙看看,可以拉钉钉群讨论,谢谢!

The RFC is done. Please take a look and we can discuss it in the DingTalk group. Thank you!

Thanks for your contribution! We'll try to review as soon as possible(hopefully within this week) and start a draft later for the feature so that we can push it faster.

@Mingholy

I will enter the development phase now, because our team is very anxious to use this framework. But it's okay, I will make adjustments according to your suggestions if you have new ideas after you have reviewed it. The final PR submission will be based on your ideas.

@x22x22
Copy link
Author

x22x22 commented Oct 16, 2025

您好,什么时候实施,预计什么时候配备这个能力。

@jliu666

我这边现在就在开发了,但是本项目维护者他们还需要进一步审核,到时候他们审核过了我会根据他们新的意见调整代码再进行pr。我这边预计一周左右可以提供json的输入输出和python版本的sdk。

I am currently in the development phase; however, the project maintainers require further review. Once their review is completed, I will revise the code according to their feedback and proceed with the pull request. We anticipate that the JSON input/output functionality and the Python SDK will be available within approximately one week.

…tion details in Qwen-Code Agent framework documentation
…iling core components, dependencies, and operational guidelines
…ore functionalities and communication protocols
…ed core components, responsibilities, and event flow specifications
…utput specifications and integration scenarios
…on scenarios, consumption strategies, and command coordination guidelines
- Updated session scheduling terminology from "子代理" to "子 Agent" for consistency.
- Added detailed sections on Agentic session capabilities, including session loop, context management, message stream handling, dynamic control interfaces, and transport layer extensions.
- Included examples for quick start, multi-Agent orchestration, embedded MCP tools, and integrated session scaffolding.
- Expanded on debugging and environment injection strategies, emphasizing error handling and permission decision-making.
- Clarified communication patterns and MCP capabilities, with a focus on permission updates and hook configurations.
- Updated core component and responsibilities descriptions for better clarity.
- Improved language for core functions and objectives to enhance readability.
- Revised architecture overview table for consistency and clarity.
- Enhanced event flow descriptions for better understanding of interactions.
- Expanded capability mapping section to clarify current and future capabilities.
- Introduced detailed agent session capabilities, including session loop and context management.
- Added examples for dynamic control interfaces and transport layer extensions.
- Improved logging and observability sections for better clarity on SDK behavior.
- Enhanced configuration injection and settings management details.
- Updated integration model section with clearer examples and descriptions.
- Added support for 'stream-json' input and output formats in the CLI.
- Introduced StreamJsonWriter for handling structured output.
- Enhanced non-interactive CLI to process stream-json formatted messages.
- Implemented parsing of stream-json input and control requests.
- Added tests for stream-json functionality, including user messages and control responses.
- Updated configuration to include input/output format options.
- Improved error handling for invalid stream-json input.
@x22x22
Copy link
Author

x22x22 commented Oct 17, 2025

@pomelo-nwu @Mingholy

我已经完成了最简单版本的json-steam,你们可以根据 "docs/cli/index.md" 体验一下效果,我会接着继续补充完RFC中设计的内容。

I have completed the initial version of json-stream. You may evaluate its performance by referring to "docs/cli/index.md". I will proceed to implement the remaining components as outlined in the RFC specifications.

@Mingholy
Copy link
Collaborator

@pomelo-nwu @Mingholy

我已经完成了最简单版本的json-steam,你们可以根据 "docs/cli/index.md" 体验一下效果,我会接着继续补充完RFC中设计的内容。

I have completed the initial version of json-stream. You may evaluate its performance by referring to "docs/cli/index.md". I will proceed to implement the remaining components as outlined in the RFC specifications.

我看了所有的RFCs和commits,可能会有些分歧:

  1. 我们暂时没有计划在SDK侧引入“Worker池”/“负载均衡”这些概念;
  2. 由于当前的Agent设计还没有完全脱离Gemini-CLI的既有架构,这导致了一些OpenAI协议的message信息在Turn这一层遗失了(如usage),这些信息需要想办法补充(我们也考虑改造以在更靠近contentGenerator的方向写入stdout流,虽然这样做可能存在弊端);
  3. 目前hooks等功能还不支持,虽然我们计划尽可能贴近claude code agent sdk的APIs,但无法做到类型完全一致,且可能存在频繁调整。
  4. 同时,还依赖其他问题的解决,比如很多settings和环境变量配置不生效问题,可能存在的console.log污染stdout问题(在通过ACP集成中出现)等。

我们计划先根据自身需求实现一个最小可用版本,再迭代更多功能支持。基于这些情况,你可以在fork中继续实现SDK部分以满足你的业务需求,让此PR专注于实现--input-format/--output-format

I've reviewed all the RFCs and commits, and there may be some divergences:

  1. We currently have no plans to introduce concepts like "Worker Pool" or "Load Balancing" in the SDK.
  2. Because the current Agent design hasn't completely deviated from the existing Gemini-CLI architecture, some OpenAI protocol messages (such as "usage") are lost at the Turn layer. We need to find a way to replace this information (we're also considering writing to the stdout stream closer to the contentGenerator, although this may have drawbacks).
  3. Features like hooks are not currently supported. While we plan to align closely with the Claude Code Agent SDK API, we can't guarantee complete type consistency, and frequent adjustments are possible.
  4. This also requires resolving other issues, such as the ineffectiveness of many settings and environment variable configurations, and the potential for console.log to pollute stdout (which occurs when integrating with the ACP).

We plan to first implement a minimum viable version based on our needs, and then iterate to add support for more features. Based on these situations, you can continue to implement the SDK part in your fork to meet your business needs, and let this PR focus on implementing --input-format/--output-format.

@github-actions github-actions bot added type/bug Something isn't working as expected and removed bug labels Oct 17, 2025
@x22x22
Copy link
Author

x22x22 commented Oct 17, 2025

@pomelo-nwu @Mingholy

我已经完成了最简单版本的json-steam,你们可以根据 "docs/cli/index.md" 体验一下效果,我会接着继续补充完RFC中设计的内容。

I have completed the initial version of json-stream. You may evaluate its performance by referring to "docs/cli/index.md". I will proceed to implement the remaining components as outlined in the RFC specifications.

我看了所有的RFCs和commits,可能会有些分歧:

  1. 我们暂时没有计划在SDK侧引入“Worker池”/“负载均衡”这些概念;
  2. 由于当前的Agent设计还没有完全脱离Gemini-CLI的既有架构,这导致了一些OpenAI协议的message信息在Turn这一层遗失了(如usage),这些信息需要想办法补充(我们也考虑改造以在更靠近contentGenerator的方向写入stdout流,虽然这样做可能存在弊端);
  3. 目前hooks等功能还不支持,虽然我们计划尽可能贴近claude code agent sdk的APIs,但无法做到类型完全一致,且可能存在频繁调整。
  4. 同时,还依赖其他问题的解决,比如很多settings和环境变量配置不生效问题,可能存在的console.log污染stdout问题(在通过ACP集成中出现)等。

我们计划先根据自身需求实现一个最小可用版本,再迭代更多功能支持。基于这些情况,你可以在fork中继续实现SDK部分以满足你的业务需求,让此PR专注于实现--input-format/--output-format

I've reviewed all the RFCs and commits, and there may be some divergences:

  1. We currently have no plans to introduce concepts like "Worker Pool" or "Load Balancing" in the SDK.
  2. Because the current Agent design hasn't completely deviated from the existing Gemini-CLI architecture, some OpenAI protocol messages (such as "usage") are lost at the Turn layer. We need to find a way to replace this information (we're also considering writing to the stdout stream closer to the contentGenerator, although this may have drawbacks).
  3. Features like hooks are not currently supported. While we plan to align closely with the Claude Code Agent SDK API, we can't guarantee complete type consistency, and frequent adjustments are possible.
  4. This also requires resolving other issues, such as the ineffectiveness of many settings and environment variable configurations, and the potential for console.log to pollute stdout (which occurs when integrating with the ACP).

We plan to first implement a minimum viable version based on our needs, and then iterate to add support for more features. Based on these situations, you can continue to implement the SDK part in your fork to meet your business needs, and let this PR focus on implementing --input-format/--output-format.

@Mingholy

我和你的想法一致,我其实没有打算在本次pr里提交sdk方面的任何内容(python调用的例子除外)。只是 @pomelo-nwu 在上个issus里提到想了解一下我对sdk的想法。所有我才在这里补充对应的RFC的。

另外我新的ref_claude也是参考claude的数据结构设计的,所以目前提交的所有代码都是尽可能和claude的报文靠近的(不是openai)。

hook是一个很重要且很有用的机制,可以在另一个pr里完成。没有hook机制就意味着授权等机制没办法使用。可以暂时先在yolo模式在运行。

@Mingholy

I agree with you. I actually didn't plan to submit any SDK-related content in this pull request (except for the Python call examples). It's just that @pomelo-nwu mentioned in a previous "issues" that he wanted to hear my thoughts on the SDK. So I'm adding the corresponding RFC here.

Also, my new ref_claude is also designed with reference to Claude's data structures. Therefore, all the code submitted so far is as close as possible to Claude's messages (not OpenAI's).

Hooks are an important and useful mechanism that can be implemented in another pull request. Without a hook mechanism, mechanisms like authorization can't be used. For now, you can run it in Yolo mode.

… management

- Introduced StreamJsonControlContext to manage hook callbacks and MCP clients.
- Implemented a prompt queue to handle user prompts asynchronously.
- Refactored handleUserPrompt to support aborting and managing tool calls.
- Added control request handling for permission modes, model setting, and tool usage.
- Enhanced StreamJsonWriter to include session ID in system messages and emit detailed result envelopes.
- Added tests for StreamJsonWriter to verify result emissions and session ID inclusion.
- Updated non-interactive tool executor to support output updates and completion handlers.
@x22x22
Copy link
Author

x22x22 commented Oct 18, 2025

@pomelo-nwu

上次你提到我们可以建立一个钉钉群进行更高效的沟通和协同开发,请问现在可以建群了吗?

Regarding your previous suggestion to establish a DingTalk group for more efficient communication and collaborative development, may I inquire if it is now feasible to proceed with creating the group?

@jliu666
Copy link

jliu666 commented Oct 20, 2025

距离代码合入还有多久,什么时候会发布第一个可用的版本?等不及想用
之前准备二次开发,看到官方计划支持,等一个好消息

我已经尝试此PR的代码,看上去一切运行正常👍

}

return aggregate;
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来对non-interactive mode做了很大的侵入式变更。
个人理解是有一定必要的,但是我们是否可以先保留核心的stream json、control_request/response部分,避免一次性实现太多的变更,也有助于我们在迭代时考虑如何拆分它们到语义更明确的模块中去。

This seems like a fairly intrusive change to non-interactive mode.
IMHO, it's necessary, but I'm wondering if we can retain the core stream JSON and control_request/response components first to avoid making too many changes all at once. This will also help us consider how to split them into modules with clearer semantics as we iterate.

export type StreamJsonEnvelope =
| StreamJsonOutputEnvelope
| StreamJsonInputEnvelope;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

也许我们可以将这些message types单独作为protocol组织,以便于未来的SDK也可以对齐它们。
尽管TS版本的Claude Code Agent SDK并没有显式依赖CLI包,但它们有一个更底层的@anthropic-ai/sdk作为SoT。这和我们的情况有所不同。
想听听你的看法。
另外,也许我们可以直接将它们定义成*Message,而非*Evelope?

Perhaps we can organize these message types as separate protocols so that future SDKs can align with them.
Although the TS version of the Claude Code Agent SDK doesn't explicitly depend on the CLI package, it has a lower-level @anthropic-ai/sdk as its SoT. This differs from our situation.
I'd love to hear your thoughts.
Also, maybe we could just define them as *Message instead of *Evelope?

hookCallbacks: new Map<string, HookCallbackRegistration>(),
registeredHookEvents: new Set<string>(),
mcpClients: new Map<string, { client: Client; config: MCPServerConfig }>(),
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the thought of controlContext:)


function writeEnvelope(envelope: StreamJsonOutputEnvelope): void {
process.stdout.write(`${serializeStreamJsonEnvelope(envelope)}\n`);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input.ts看起来类似一个针对input stream的util。
readStreamJsonInput及它依赖的parseStreamJsonInputFromIterable 都没有用到。
同时handleControlRequestsession.ts中也有重复定义。
writeEnvelope已经通过writer支持了。
extractUserMessageText应该是唯一有效的代码。

鉴于此我觉得我们可以把它和writer一起考虑做成一个stream manipulation的I/O模块作为session的依赖负责read/write,parse/wrap。

input.ts looks like a utility for the input stream.
However, readStreamJsonInput and its dependency parseStreamJsonInputFromIterable are not used.
Also, handleControlRequest is duplicated in session.ts.
writeEnvelope is already supported by writer.
extractUserMessageText should be the only valid code.

Given this, I believe we can consider combining it with writer to form a stream manipulation I/O module as a dependency of session, responsible for reading/writing, parsing/wrapping.

writer: StreamJsonWriter,
controlContext: StreamJsonControlContext,
): Promise<boolean> {
const subtype = envelope.request?.subtype;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看到session中做了很多message routing的工作,有个疑问:既然已经有了处理control_request的controller.ts是否可以放在那边去实现更清晰一些?

Seeing that session does a lot of message routing work, just leave a questiong here in case I'm missing something: since we already have a controller.ts that handles control_request, can we put it there to make the implementation clearer?

Copy link
Collaborator

@Mingholy Mingholy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢贡献!
#838 正在同步上游更新,预计会是一个比较大的变更;我们可以在同步更新结束后合并此PR。
当前PR也包含了比较多内容,建议我们先保留核心的stream json相关功能,将大的PR拆分为几个规模相对可控的PR迭代。

  • 辛苦清理一下docs,我们可以在功能稳定后更新文档
  • examples可以作为示例使用单独PR提交,这些PR不会close
  • RFC docs可以放置在dicussions留档,if you prefer
  • 当前PR还需要做一点重构的工作

google-gemini/gemini-cli#8016 non-interactive模式作为SDK的前置目前还存在一些问题,我们的实现也与上游产生了分叉,因此还需要观察和修复。

Thanks for your contribution!
#838 is being synchronized with upstream updates and is expected to be a significant change. We can merge this PR after the synchronization is completed.
The current pull request (PR) contains quite a bit of content. We recommend that you prioritize the core stream JSON functionality and split the large PR into several manageable iterations.

  • Please take the time to clean up the docs; we'll update them once the functionality is stable.
  • Examples can be submitted as separate PRs; these PRs will not be closed.
  • RFC docs can be archived in dicussions if you prefer.
  • The current PR still requires some refactoring.

google-gemini/gemini-cli#8016 The non-interactive mode, as a pre-SDK implementation, currently has some issues. Our implementation has diverged from the upstream implementation, so we need to monitor and address them.

@x22x22
Copy link
Author

x22x22 commented Oct 22, 2025

感谢贡献! #838 正在同步上游更新,预计会是一个比较大的变更;我们可以在同步更新结束后合并此PR。 当前PR也包含了比较多内容,建议我们先保留核心的stream json相关功能,将大的PR拆分为几个规模相对可控的PR迭代。

  • 辛苦清理一下docs,我们可以在功能稳定后更新文档
  • examples可以作为示例使用单独PR提交,这些PR不会close
  • RFC docs可以放置在dicussions留档,if you prefer
  • 当前PR还需要做一点重构的工作

google-gemini/gemini-cli#8016 non-interactive模式作为SDK的前置目前还存在一些问题,我们的实现也与上游产生了分叉,因此还需要观察和修复。

Thanks for your contribution! #838 is being synchronized with upstream updates and is expected to be a significant change. We can merge this PR after the synchronization is completed. The current pull request (PR) contains quite a bit of content. We recommend that you prioritize the core stream JSON functionality and split the large PR into several manageable iterations.

  • Please take the time to clean up the docs; we'll update them once the functionality is stable.
  • Examples can be submitted as separate PRs; these PRs will not be closed.
  • 如果您愿意,可以将 RFC 文档存档在 discusions 中。
  • 当前的 PR 仍然需要一些重构。

google-gemini/gemini-cli#8016 The non-interactive mode, as a pre-SDK implementation, currently has some issues. Our implementation has diverged from the upstream implementation, so we need to monitor and address them.

@Mingholy

好的,那先等你们合并上游工作结束后我再来重构这个PR。

Sure, then I'll come back to refactor this PR after your upstream work is merged.

@x22x22
Copy link
Author

x22x22 commented Oct 29, 2025

@Mingholy @pomelo-nwu

请问你们的上游合并任务结束了吗?我是否可以继续这个pr相关的工作了?

May I ask if your upstream consolidation task has been completed?May I continue with this PR-related work?

@x22x22
Copy link
Author

x22x22 commented Oct 29, 2025

@pomelo-nwu

请问是否可以拉钉钉群高效的沟通?

May I ask if it's possible to create a DingTalk group for efficient communication?

@pomelo-nwu
Copy link
Collaborator

@x22x22 welcome!
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status/need-information More information is needed to resolve this issue. type/bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants