蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

观察两组新会话键及多行提示词实际到达模型

PYTHONWindows ConPTY、OpenCode V1.19.5、Python 3.12、pywinpty 3.1.2。key_input_probe.py
key_input_probe.py91 行
"""Exercise real terminal key bytes and session switching in OpenCode V1."""

from hashlib import file_digest
from http.server import ThreadingHTTPServer
import json
from pathlib import Path
import sys
from tempfile import TemporaryDirectory
from threading import Thread
from time import monotonic,sleep
import winpty
from rules_lab import Fixture
from tui_trial import get_text,make_profile


def check(executable):
    with TemporaryDirectory(prefix="mbsky-opencode-keybinding-") as tmp, ThreadingHTTPServer(("127.0.0.1",0),Fixture) as model:
        root,env=make_profile(Path(tmp),model)
        env.update(HOME=tmp,USERPROFILE=tmp)
        model.model_contexts=[]
        worker=Thread(target=model.serve_forever,daemon=True);worker.start()
        custom={"keybinds":{"leader":"ctrl+g","session_new":"<leader>n,alt+n","input_newline":"shift+return"}}
        (root/"tui.json").write_text(json.dumps(custom),encoding="utf8")
        process=winpty.PtyProcess.spawn([str(executable),"--pure"],cwd=str(root),env=env,dimensions=(32,110))
        chunks=[]
        def pump():
            while True:
                try:chunks.append(process.read(4096))
                except (OSError,EOFError):return
        monitor=Thread(target=pump,daemon=True);monitor.start()
        def plain():
            return get_text("".join(chunks))
        def wait_for(condition):
            deadline=monotonic()+9
            while monotonic()<deadline:
                if condition():return
                sleep(.06)
            raise AssertionError(plain()[-3400:])
        def requests_since(start):
            return "\n".join(model.model_contexts[start:])
        try:
            wait_for(lambda:"Build" in plain())
            process.write("Warehouse North")
            process.write("\x1b[13;2u")
            process.write("Warehouse South")
            sleep(.55)
            before_enter=len(model.model_contexts)
            process.write("\r")
            wait_for(lambda:"Warehouse South" in requests_since(0))
            context=requests_since(0)
            # The request captures the model-visible contents, not the terminal echo.
            combined="Warehouse North\\nWarehouse South" in context
            start=len(plain())
            process.write("\x07")
            sleep(.14)
            process.write("n")
            wait_for(lambda:"Ask anything" in plain()[start:] or "OpenCode" in plain()[start:])
            checkpoint=len(model.model_contexts)
            process.write("Isolated new warehouse task")
            process.write("\r")
            wait_for(lambda:"Isolated new warehouse task" in requests_since(checkpoint))
            current=requests_since(checkpoint)
            start=len(plain())
            process.write("\x1bn")
            wait_for(lambda:"Ask anything" in plain()[start:] or "OpenCode" in plain()[start:])
            alt_checkpoint=len(model.model_contexts)
            process.write("Alternate key warehouse task")
            process.write("\r")
            wait_for(lambda:"Alternate key warehouse task" in requests_since(alt_checkpoint))
            alt_context=requests_since(alt_checkpoint)
            result=dict(no_submission_before_enter=before_enter==0,multiline_prompt=combined,
                        new_message_sent=True,previous_message_in_new_context="Warehouse North" in current,
                        alternate_key_message_sent=True,previous_message_in_alternate_context="Isolated new warehouse task" in alt_context)
        finally:
            try:
                if process.isalive():
                    process.close(force=True)
            except PermissionError:
                if process.isalive():
                    raise
            finally:
                model.shutdown()
                worker.join(timeout=4)
                monitor.join(timeout=2)
    with executable.open("rb") as f:digest=file_digest(f,"sha256").hexdigest()
    return dict(binary_sha256=digest,version="1.19.5",actual=result)


if __name__=="__main__":
    print(json.dumps(check(Path(sys.argv[1]).resolve()),ensure_ascii=False,indent=2))
本例需在 Windows ConPTY、OpenCode V1.19.5、Python 3.12、pywinpty 3.1.2。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows ConPTY、OpenCode V1.19.5、Python 3.12、pywinpty 3.1.2。

入口文件:projects/opencode-keybindings/key_input_probe.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 key_input_probe.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows ConPTY、OpenCode V1.19.5、Python 3.12、pywinpty 3.1.2。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:OpenCode

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a247.zip

下载课程关联工程包:OpenCode

课程关联工程包(登记关联28章) · 含当前章节或实例 · mbskycom202612a236.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习