fix(workshop-chat): 도구를 부른 턴의 응답이 두 번 보이던 문제 — 서버가 모든 라운드의 token을 흘려보내는데 화면이 final 이벤트를 무시하고 이어 붙이기만 했음. final이 오면 말풍선을 최종 텍스트로 교체

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-09-30 16:29:14 +09:00
1 parent f5e23118bf
commit 55683c95d2
2 files changed
+73 -5

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
/**
* workshop-chat.js applyStreamEvent — 스트림 이벤트를 말풍선에 반영
*
* 사건(2026-09-30): 작업실 채팅에서 도구를 부른 턴의 응답이 두 번 보였다. 서버는 도구 호출 전 라운드의
* token도 그대로 흘려보내는데, 화면은 final 이벤트를 무시하고 이어 붙이기만 했다.
*/
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'fs';
import path from 'path';
const src = fs.readFileSync(path.join(__dirname, '../web-ui/js/app/workshop-chat.js'), 'utf8');
const m = /function applyStreamEvent\(st,ev\)\{[\s\S]*?\n\}\n/.exec(src);
if (!m) throw new Error('applyStreamEvent를 찾지 못했습니다');
// eslint-disable-next-line no-new-func
const applyStreamEvent: (st: any, ev: any) => boolean = new Function(`${m[0]}; return applyStreamEvent;`)();
const run = (events: any[]) => { const st = { textBuf: '', thinkingActive: false }; for (const e of events) applyStreamEvent(st, e); return st; };
describe('applyStreamEvent', () => {
test('토큰은 이어 붙인다(기본 스트리밍)', () => {
assert.equal(run([{ type: 'token', text: '안녕' }, { type: 'text_delta', text: '하세요' }]).textBuf, '안녕하세요');
});
test('도구 호출 전 문장과 최종 답이 겹쳐 나와도 final로 한 번만 보인다 — 사건의 재현', () => {
const st = run([
{ type: 'token', text: '주문 완료하셨군요! ' }, // 1라운드: 도구 호출 전 말
{ type: 'tool_call', name: 'workshop_project' },
{ type: 'token', text: '주문 완료하셨군요! ' }, // 2라운드: 최종 답
{ type: 'token', text: '수량을 업데이트했습니다.' },
{ type: 'final', text: '주문 완료하셨군요! 수량을 업데이트했습니다.' },
]);
assert.equal(st.textBuf, '주문 완료하셨군요! 수량을 업데이트했습니다.');
});
test('final이 없으면(옛 서버·중단) 스트리밍된 내용을 그대로 둔다', () => {
assert.equal(run([{ type: 'token', text: 'ab' }, { type: 'tool_call' }]).textBuf, 'ab');
});
test('빈 final은 무시한다 — 스트림으로 받은 답을 지우지 않는다', () => {
assert.equal(run([{ type: 'token', text: '내용' }, { type: 'final', text: ' ' }]).textBuf, '내용');
assert.equal(run([{ type: 'token', text: '내용' }, { type: 'final' }]).textBuf, '내용');
});
test('thinking_status는 생각 중 표시만 바꾸고 텍스트는 건드리지 않는다', () => {
const st = run([{ type: 'token', text: 'x' }, { type: 'thinking_status', active: true }]);
assert.equal(st.thinkingActive, true);
assert.equal(st.textBuf, 'x');
const done = run([{ type: 'thinking_status', active: true }, { type: 'final', text: '끝' }]);
assert.equal(done.thinkingActive, false);
});
test('알 수 없는 이벤트는 상태를 바꾸지 않는다', () => {
assert.equal(applyStreamEvent({ textBuf: '', thinkingActive: false }, { type: 'info' }), false);
assert.equal(applyStreamEvent({ textBuf: '', thinkingActive: false }, null), false);
});
});
+16 -5
View File
@@ -13,6 +13,19 @@ function getSessionId(){
}
let _chatLoadSeq=0;
// 스트림 이벤트를 말풍선 상태에 반영한다(2026-09-30).
// 서버는 모든 라운드의 token을 그대로 흘려보낸다 — 모델이 도구를 부르기 전에 한 말과 도구 결과를 본 뒤의
// 최종 답이 한 말풍선에 이어 붙어 "응답이 두 번 나온다"(작업실에서 주문 등록 후 신고). 서버가 답을 확정하면
// 최종 텍스트를 final 이벤트로 따로 보내므로(server.ts), 그때 말풍선 내용을 그 텍스트로 교체한다.
// 메인 채팅(chat-sse.js)도 final로 통째 교체한다. 상태가 바뀌었으면 true.
function applyStreamEvent(st,ev){
if(!ev)return false;
if(ev.type==='token'||ev.type==='text_delta'){st.textBuf+=(ev.text||'');return true;}
if(ev.type==='thinking_status'){st.thinkingActive=!!ev.active;return true;}
if(ev.type==='final'&&typeof ev.text==='string'&&ev.text.trim()){st.textBuf=ev.text;st.thinkingActive=false;return true;}
return false;
}
async function loadHistory(){
const sid=getSessionId();
const seq=++_chatLoadSeq;
@@ -180,6 +193,7 @@ async function callChat(message){
// 것처럼 보인다 — 'thinking_status' 이벤트로 그 구간만 "생각중…"을 잠깐 보여준다. textBuf
// 자체엔 안 섞고(진짜 답변 텍스트만 유지) 렌더링할 때만 붙였다 뗀다.
let thinkingActive=false;
const streamState={textBuf:'',thinkingActive:false};
function renderBubble(){
if(!bubble)return;
const suffix=thinkingActive?(textBuf?'\n\n':'')+'*(생각 중…)*':'';
@@ -195,11 +209,8 @@ async function callChat(message){
for(const part of sseParts){
if(!part.startsWith('data: '))continue;
let ev;try{ev=JSON.parse(part.slice(6));}catch{continue;}
if(ev.type==='token'||ev.type==='text_delta'){
textBuf+=(ev.text||'');
renderBubble();
}else if(ev.type==='thinking_status'){
thinkingActive=!!ev.active;
if(applyStreamEvent(streamState,ev)){
textBuf=streamState.textBuf;thinkingActive=streamState.thinkingActive;
renderBubble();
}
}