Fix Slack output dedup and chunk forwarding
This commit is contained in:
@@ -27,6 +27,7 @@ class Bridge:
|
||||
self._channel: str = self.config.allowed_channel_id
|
||||
self._active_target: str | None = None
|
||||
self._last_sent_output: str = ""
|
||||
self._last_sent_fingerprint: str | None = None
|
||||
self._last_input_at = time.monotonic()
|
||||
self._last_output_at = time.monotonic()
|
||||
self._input_idle_reported = False
|
||||
@@ -45,6 +46,9 @@ class Bridge:
|
||||
|
||||
if self.pty and self.pty.is_alive:
|
||||
self.pty.send(text)
|
||||
# 입력 이후 출력은 동일 문자열이어도 한 번 더 전달한다.
|
||||
self._last_sent_output = ""
|
||||
self._last_sent_fingerprint = None
|
||||
self._last_input_at = time.monotonic()
|
||||
self._input_idle_reported = False
|
||||
logger.info("입력 전달: %s", text)
|
||||
@@ -99,6 +103,7 @@ class Bridge:
|
||||
self._channel = channel
|
||||
self._active_target = target
|
||||
self._last_sent_output = ""
|
||||
self._last_sent_fingerprint = None
|
||||
self.pty = PtyManager(
|
||||
self._session_name_for_target(target),
|
||||
cli_name=target,
|
||||
@@ -131,6 +136,7 @@ class Bridge:
|
||||
self.pty = None
|
||||
self._active_target = None
|
||||
self._last_sent_output = ""
|
||||
self._last_sent_fingerprint = None
|
||||
self.slack.send_message(channel, ":electric_plug: 세션 연결이 해제되었습니다.")
|
||||
|
||||
def _poll_output(self) -> None:
|
||||
@@ -147,13 +153,7 @@ class Bridge:
|
||||
self._output_idle_reported = False
|
||||
|
||||
if buffer:
|
||||
# 메시지 길이 제한 적용
|
||||
message = buffer[: self.config.max_message_length].rstrip()
|
||||
if len(buffer) > self.config.max_message_length:
|
||||
message += "\n... (truncated)"
|
||||
if message and message != self._last_sent_output:
|
||||
self.slack.send_message(self._channel, f"```\n{message}\n```")
|
||||
self._last_sent_output = message
|
||||
self._send_output_chunks(buffer)
|
||||
buffer = ""
|
||||
|
||||
output_idle = now - self._last_output_at
|
||||
@@ -191,6 +191,83 @@ class Bridge:
|
||||
# attach 프로세스가 예기치 않게 종료된 경우
|
||||
self.slack.send_message(self._channel, ":warning: 세션 연결이 종료되었습니다.")
|
||||
|
||||
@staticmethod
|
||||
def _split_message(text: str, max_length: int) -> list[str]:
|
||||
"""긴 텍스트를 메시지 길이 제한에 맞게 분할한다."""
|
||||
if max_length <= 0:
|
||||
return [text] if text else []
|
||||
|
||||
lines = text.splitlines(keepends=True)
|
||||
chunks: list[str] = []
|
||||
current = ""
|
||||
|
||||
def flush() -> None:
|
||||
nonlocal current
|
||||
if current:
|
||||
chunks.append(current)
|
||||
current = ""
|
||||
|
||||
for line in lines:
|
||||
if len(line) > max_length:
|
||||
flush()
|
||||
start = 0
|
||||
while start < len(line):
|
||||
piece = line[start : start + max_length]
|
||||
chunks.append(piece)
|
||||
start += max_length
|
||||
continue
|
||||
|
||||
if len(current) + len(line) > max_length:
|
||||
flush()
|
||||
current += line
|
||||
|
||||
flush()
|
||||
return chunks
|
||||
|
||||
def _send_output_chunks(self, text: str) -> None:
|
||||
"""출력을 잘라서 Slack으로 순차 전송한다."""
|
||||
chunks = self._split_message(text, self.config.max_message_length)
|
||||
snapshot = "\n\x00".join(chunks)
|
||||
fingerprint = self._output_fingerprint(snapshot)
|
||||
if not chunks:
|
||||
return
|
||||
if (
|
||||
self._last_sent_fingerprint is not None
|
||||
and fingerprint == self._last_sent_fingerprint
|
||||
):
|
||||
return
|
||||
|
||||
for chunk in chunks:
|
||||
if chunk.endswith("\n"):
|
||||
message = f"```\n{chunk}```"
|
||||
else:
|
||||
message = f"```\n{chunk}\n```"
|
||||
self.slack.send_message(self._channel, message)
|
||||
self._last_sent_output = snapshot
|
||||
self._last_sent_fingerprint = fingerprint
|
||||
|
||||
@staticmethod
|
||||
def _output_fingerprint(text: str) -> str:
|
||||
"""중복 전송 억제를 위한 정규화 지문을 생성한다."""
|
||||
normalized_lines: list[str] = []
|
||||
for raw_line in text.splitlines():
|
||||
line = raw_line.rstrip()
|
||||
if not line.strip():
|
||||
continue
|
||||
|
||||
# tmux 상태줄의 시계/날짜 라인은 프레임마다 변할 수 있어 제외한다.
|
||||
if re.fullmatch(
|
||||
(
|
||||
r'\s*\w*odex\]\s+\d+:[^\[]*'
|
||||
r'\[\d+,\d+\]\s+".+"\s+\d{2}:\d{2}\s+\d{2}-[A-Za-z]{3}-\d{2}'
|
||||
),
|
||||
line,
|
||||
):
|
||||
continue
|
||||
|
||||
normalized_lines.append(line)
|
||||
return "\n".join(normalized_lines)
|
||||
|
||||
def run(self) -> None:
|
||||
"""브릿지를 시작한다."""
|
||||
logging.basicConfig(
|
||||
|
||||
Reference in New Issue
Block a user