Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,42 @@ a:hover {
gap: 6px;
}

.book-travel-submit-cluster {
display: flex;
align-items: center;
gap: 8px;
}

.book-travel-submit-button.ant-btn,
.book-travel-scene-change-button.ant-btn {
height: 32px;
padding: 0 12px;
border-radius: 8px;
font-size: 13px;
}

.book-travel-submit-button.ant-btn {
background: #d97757;
border-color: #d97757;
}

.book-travel-submit-button.ant-btn:not(:disabled):hover {
background: #c4664a;
border-color: #c4664a;
}

.book-travel-scene-change-button.ant-btn {
color: #d97757;
border-color: #e7c3b6;
background: #fff;
}

.book-travel-scene-change-button.ant-btn:not(:disabled):hover {
color: #c4664a;
border-color: #d97757;
background: #fff7f3;
}

.book-travel-composer-actions,
.adventure-composer-actions {
position: absolute;
Expand Down
47 changes: 20 additions & 27 deletions src/__tests__/story-book-travel-mode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,7 @@ describe('Story book-travel mode', () => {
});

it('shows a submitted book-travel action immediately and locks sending while it is processed', async () => {
const classification = createDeferred<{ classification: 'insert-beat' }>();
invokeMock.mockImplementation((command: string, args?: any) => {
if (command === 'classify_book_travel_input') return classification.promise;
if (command === 'start_write_book_travel_insert_beat_stream') {
return Promise.resolve({ runId: 'pending-insert-run' });
}
Expand All @@ -651,18 +649,19 @@ describe('Story book-travel mode', () => {

const inputBox = screen.getByPlaceholderText(/说些什么/);
fireEvent.change(inputBox, { target: { value: '查看窗外动静' } });
const sendButton = document.querySelector('.de-ai-agent-run-button') as HTMLButtonElement;
const sendButton = screen.getByRole('button', { name: '提交行动' });
fireEvent.click(sendButton);

expect(await screen.findByText(/查看窗外动静/)).toBeInTheDocument();

fireEvent.change(inputBox, { target: { value: '继续追问' } });
expect(sendButton).toBeDisabled();
expect(screen.getByRole('button', { name: '换场提交' })).toBeDisabled();

classification.resolve({ classification: 'insert-beat' });
await waitFor(() => {
expect(invokeMock).toHaveBeenCalledWith('start_write_book_travel_insert_beat_stream', expect.anything());
});
expect(invokeMock.mock.calls.filter(([command]) => command === 'classify_book_travel_input')).toHaveLength(0);

fireEvent.change(inputBox, { target: { value: '继续追问' } });
expect(sendButton).toBeDisabled();
Expand All @@ -682,9 +681,7 @@ describe('Story book-travel mode', () => {
});

it('recovers when the scene writer returns JSON with raw newlines inside a string value', async () => {
const classification = createDeferred<{ classification: 'insert-beat' }>();
invokeMock.mockImplementation((command: string, args?: any) => {
if (command === 'classify_book_travel_input') return classification.promise;
if (command === 'start_write_book_travel_insert_beat_stream') {
return Promise.resolve({ runId: 'raw-newline-insert-run' });
}
Expand All @@ -696,11 +693,10 @@ describe('Story book-travel mode', () => {

const inputBox = screen.getByPlaceholderText(/说些什么/);
fireEvent.change(inputBox, { target: { value: '查看窗外动静' } });
const sendButton = document.querySelector('.de-ai-agent-run-button') as HTMLButtonElement;
const sendButton = screen.getByRole('button', { name: '提交行动' });
fireEvent.click(sendButton);

expect(await screen.findByText(/查看窗外动静/)).toBeInTheDocument();
classification.resolve({ classification: 'insert-beat' });
await waitFor(() => {
expect(invokeMock).toHaveBeenCalledWith('start_write_book_travel_insert_beat_stream', expect.anything());
});
Expand Down Expand Up @@ -737,8 +733,8 @@ describe('Story book-travel mode', () => {

const inputBox = screen.getByPlaceholderText(/说些什么/);
fireEvent.change(inputBox, { target: { value: '去正厅见沈家人' } });
const sendButton = document.querySelector('.de-ai-agent-run-button') as HTMLButtonElement;
fireEvent.click(sendButton);
const sendButton = screen.getByRole('button', { name: '提交行动' });
fireEvent.click(screen.getByRole('button', { name: '换场提交' }));

expect(await screen.findByText(/去正厅见沈家人/)).toBeInTheDocument();
expect(screen.getByText('正在识别行动...')).toBeInTheDocument();
Expand Down Expand Up @@ -814,8 +810,8 @@ describe('Story book-travel mode', () => {
renderWithRouter(<Story />);

fireEvent.change(screen.getByPlaceholderText(/说些什么/), { target: { value: '转去偏院搜证' } });
const sendButton = document.querySelector('.de-ai-agent-run-button') as HTMLButtonElement;
fireEvent.click(sendButton);
const sendButton = screen.getByRole('button', { name: '提交行动' });
fireEvent.click(screen.getByRole('button', { name: '换场提交' }));

expect(await screen.findByText(/转去偏院搜证/)).toBeInTheDocument();
await waitFor(() => {
Expand Down Expand Up @@ -1083,8 +1079,13 @@ describe('Story book-travel mode', () => {
renderWithRouter(<Story />);

fireEvent.change(screen.getByPlaceholderText(/说些什么/), { target: { value: '查看状态' } });
const sendButton = document.querySelector('.de-ai-agent-run-button') as HTMLButtonElement;
fireEvent.click(sendButton);
fireEvent.click(screen.getByRole('button', { name: '提交行动' }));
await waitFor(() => expect(invokeMock).toHaveBeenCalledWith('start_write_book_travel_insert_beat_stream', expect.anything()));
expect(invokeMock.mock.calls.filter(([command]) => command === 'classify_book_travel_input')).toHaveLength(0);
expect(await screen.findByText(/她回应了/)).toBeInTheDocument();

fireEvent.change(screen.getByPlaceholderText(/说些什么/), { target: { value: '离开喜房' } });
fireEvent.click(screen.getByRole('button', { name: '换场提交' }));

await waitFor(() => {
expect(invokeMock).toHaveBeenCalledWith('classify_book_travel_input', expect.anything());
Expand All @@ -1107,13 +1108,12 @@ describe('Story book-travel mode', () => {
renderWithRouter(<Story />);

fireEvent.change(screen.getByPlaceholderText(/说些什么/), { target: { value: '查看状态' } });
fireEvent.click(document.querySelector('.de-ai-agent-run-button') as HTMLButtonElement);
fireEvent.click(screen.getByRole('button', { name: '提交行动' }));

await waitFor(() => expect(invokeMock).toHaveBeenCalledWith('start_write_book_travel_insert_beat_stream', expect.anything()));
const writerCall = invokeMock.mock.calls.find(([command]) => command === 'start_write_book_travel_insert_beat_stream');
const classifierCall = invokeMock.mock.calls.find(([command]) => command === 'classify_book_travel_input');
expect(invokeMock.mock.calls.filter(([command]) => command === 'classify_book_travel_input')).toHaveLength(0);
expect(writerCall?.[1].request.systemPrompt).toMatch(/穿书预设正文[\s\S]*穿书场景写手/);
expect(classifierCall?.[1].request.systemPrompt).not.toContain('穿书预设正文');
});

it('passes the selected book-travel input mode prefix into the runtime calls and turn record', async () => {
Expand All @@ -1123,23 +1123,17 @@ describe('Story book-travel mode', () => {

fireEvent.click(screen.getByText('角色行为'));
fireEvent.change(screen.getByPlaceholderText(/做点什么/), { target: { value: '抬手试探门锁' } });
const sendButton = document.querySelector('.de-ai-agent-run-button') as HTMLButtonElement;
fireEvent.click(sendButton);
fireEvent.click(screen.getByRole('button', { name: '提交行动' }));

expect(await screen.findByText('【行为】抬手试探门锁')).toBeInTheDocument();

await waitFor(() => {
expect(invokeMock).toHaveBeenCalledWith(
'classify_book_travel_input',
expect.objectContaining({ userInput: '【行为】抬手试探门锁' }),
);
});
await waitFor(() => {
expect(invokeMock).toHaveBeenCalledWith(
'start_write_book_travel_insert_beat_stream',
expect.objectContaining({ userInput: '【行为】抬手试探门锁' }),
);
});
expect(invokeMock.mock.calls.filter(([command]) => command === 'classify_book_travel_input')).toHaveLength(0);
});

it('keeps scene-writer streaming visible after leaving and returning to the story page', async () => {
Expand All @@ -1155,8 +1149,7 @@ describe('Story book-travel mode', () => {
const firstRender = renderWithRouter(<Story />);

fireEvent.change(screen.getByPlaceholderText(/说些什么/), { target: { value: '查看门外' } });
const sendButton = document.querySelector('.de-ai-agent-run-button') as HTMLButtonElement;
fireEvent.click(sendButton);
fireEvent.click(screen.getByRole('button', { name: '提交行动' }));

await waitFor(() => {
expect(invokeMock).toHaveBeenCalledWith('start_write_book_travel_insert_beat_stream', expect.anything());
Expand Down
71 changes: 50 additions & 21 deletions src/pages/Story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
HistoryOutlined,
ReloadOutlined,
CompassOutlined,
StopOutlined,
PlayCircleOutlined,
DeleteOutlined,
UserOutlined,
Expand Down Expand Up @@ -1338,7 +1337,7 @@ const useStoryView = () => {
}
};

const handleSend = async () => {
const handleSend = async (bookTravelMode: 'continue' | 'scene-check' = 'continue') => {
const trimmed = input.trim();
if (!trimmed || isStreaming || isBookTravelBusy) return;

Expand All @@ -1354,6 +1353,21 @@ const useStoryView = () => {
message.error('素材缺失,请重新装配素材');
return;
}
if (bookTravelMode === 'continue') {
useBookTravelStore.getState().appendTurn({
id: pendingTurnId,
userInput: formattedBookTravelInput,
classification: 'insert-beat',
status: 'writing',
narrativeOutput: '',
stateSnapshot: useBookTravelStore.getState().currentState,
createdBeatIds: [],
});
pendingTurnCreated = true;
scrollToBottomOnce();
await handleInsertBeatStream(formattedBookTravelInput, pendingTurnId);
return;
}
useBookTravelStore.getState().appendTurn({
id: pendingTurnId,
userInput: formattedBookTravelInput,
Expand Down Expand Up @@ -1407,7 +1421,7 @@ const useStoryView = () => {
if (pendingTurnCreated) {
useBookTravelStore.getState().updateTurn(pendingTurnId, {
status: 'error',
failedStage: 'classifying',
failedStage: bookTravelMode === 'continue' ? 'writing' : 'classifying',
narrativeOutput: `处理失败:${String(err)}`,
});
}
Expand Down Expand Up @@ -2155,7 +2169,7 @@ const useStoryView = () => {
onKeyDown={(event) => {
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
void handleSend();
void handleSend('continue');
}
}}
placeholder={
Expand All @@ -2180,7 +2194,7 @@ const useStoryView = () => {

<div />

<div className="agent-send-cluster" style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
<div className="agent-send-cluster book-travel-submit-cluster">
<Tooltip color="#fff" placement="topRight" title={contextTooltip} overlayInnerStyle={{ width: 'max-content', maxWidth: 320, padding: '8px 12px', border: '1px solid #eae6df' }}>
<button
aria-label="查看上下文"
Expand All @@ -2192,22 +2206,37 @@ const useStoryView = () => {
</button>
</Tooltip>

<Tooltip title={isStreaming ? '停止' : isBookTravelBusy ? '正在处理行动' : bookTravelStore.isCompleted ? '当前故事已结局' : '提交行动'}>
<Button
className="de-ai-agent-run-button"
disabled={bookTravelStore.isCompleted || isBookTravelBusy || (!isStreaming && !input.trim())}
icon={isStreaming ? <StopOutlined /> : <PlayCircleOutlined />}
onClick={isStreaming ? handleStop : handleSend}
shape="circle"
type={isStreaming ? "default" : "primary"}
danger={isStreaming}
style={isStreaming ? undefined : {
backgroundColor: '#d97757',
borderColor: '#d97757',
color: '#ffffff'
}}
/>
</Tooltip>
{isStreaming ? (
<Button danger onClick={() => { void handleStop(); }}>
停止
</Button>
) : (
<>
<Tooltip title="留在当前场景,直接交给场景写手。">
<span>
<Button
className="book-travel-submit-button"
disabled={bookTravelStore.isCompleted || isBookTravelBusy || !input.trim()}
onClick={() => { void handleSend('continue'); }}
type="primary"
>
提交行动
</Button>
</span>
</Tooltip>
<Tooltip title="如果时间、地点发生变化,或做出重大决定、触发新事件,提交给 AI 判断是否发生场景变更。">
<span>
<Button
className="book-travel-scene-change-button"
disabled={bookTravelStore.isCompleted || isBookTravelBusy || !input.trim()}
onClick={() => { void handleSend('scene-check'); }}
>
换场提交
</Button>
</span>
</Tooltip>
</>
)}
</div>
</div>
</div>
Expand Down
Loading