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
41 changes: 41 additions & 0 deletions packages/chat-core/src/workbench-message-reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,43 @@ describe('reduceWorkbenchMessages', () => {
])
})

test('finalizes incoming processing blocks on done', () => {
const state = reduceWorkbenchMessages([], {
type: 'assistant_started',
taskId: 1,
subtaskId: 9,
})

const done = reduceWorkbenchMessages(state, {
type: 'assistant_done',
subtaskId: 9,
content: 'Final',
blocks: [
{
id: 'thinking-real',
subtaskId: 9,
type: 'thinking',
content: 'Drafting',
status: 'streaming',
createdAt: 1770000000000,
},
{
id: 'call_1',
subtaskId: 9,
type: 'tool',
toolName: 'bash',
status: 'pending',
createdAt: 1770000001000,
},
],
})

expect(done[0].blocks).toMatchObject([
{ id: 'thinking-real', type: 'thinking', status: 'done' },
{ id: 'call_1', type: 'tool', status: 'done' },
])
})

test('preserves state for unknown runtime actions', () => {
const state: WorkbenchMessage[] = [
{
Expand Down Expand Up @@ -102,7 +139,11 @@ describe('normalizeWorkbenchBlockStatus', () => {
expect(normalizeWorkbenchBlockStatus('streaming')).toBe('streaming')
expect(normalizeWorkbenchBlockStatus('done')).toBe('done')
expect(normalizeWorkbenchBlockStatus('error')).toBe('error')
expect(normalizeWorkbenchBlockStatus('completed')).toBe('done')
expect(normalizeWorkbenchBlockStatus('succeeded')).toBe('done')
expect(normalizeWorkbenchBlockStatus('failed')).toBe('error')
expect(normalizeWorkbenchBlockStatus('running')).toBe('pending')
expect(normalizeWorkbenchBlockStatus('inProgress')).toBe('pending')
expect(normalizeWorkbenchBlockStatus('unsupported')).toBe('pending')
expect(normalizeWorkbenchBlockStatus()).toBe('pending')
})
Expand Down
47 changes: 37 additions & 10 deletions packages/chat-core/src/workbench-message-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function reduceWorkbenchMessages<TAttachment = unknown, TFileChanges = un
...message,
content: action.content ?? message.content,
status: 'done' as const,
blocks: action.blocks ?? finalizeProcessingBlocks(message.blocks),
blocks: finalizeProcessingBlocks(action.blocks ?? message.blocks, 'done'),
fileChanges: action.fileChanges ?? message.fileChanges,
}
: message
Expand All @@ -182,6 +182,7 @@ export function reduceWorkbenchMessages<TAttachment = unknown, TFileChanges = un
status: 'failed' as const,
error: action.error,
errorType: action.errorType,
blocks: finalizeProcessingBlocks(message.blocks, 'error'),
}
: message
)
Expand Down Expand Up @@ -213,14 +214,26 @@ export function reduceWorkbenchMessages<TAttachment = unknown, TFileChanges = un
}

export function normalizeWorkbenchBlockStatus(status?: string): WorkbenchToolBlockStatus {
switch (status) {
const normalizedStatus = status?.trim().toLowerCase().replace(/[\s-]+/g, '_')

switch (normalizedStatus) {
case 'generating_arguments':
case 'pending':
case 'streaming':
case 'done':
case 'error':
return status
return normalizedStatus
case 'completed':
case 'complete':
case 'succeeded':
case 'success':
return 'done'
case 'failed':
case 'failure':
return 'error'
case 'running':
case 'in_progress':
case 'inprogress':
default:
return 'pending'
}
Expand Down Expand Up @@ -274,18 +287,32 @@ function appendThinkingChunk(
function finalizeThinkingBlocks(
blocks: WorkbenchProcessingBlock[] | undefined
): WorkbenchProcessingBlock[] {
return (blocks ?? []).map(block =>
block.type === 'thinking' && block.status === 'streaming'
? { ...block, status: 'done' as const }
: block
)
return finalizeBlocks(blocks)
}

function finalizeBlocks(
blocks: WorkbenchProcessingBlock[] | undefined,
finalStatus?: Extract<WorkbenchToolBlockStatus, 'done' | 'error'>
): WorkbenchProcessingBlock[] {
return (blocks ?? []).map(block => {
if (block.type === 'thinking' && block.status === 'streaming') {
return { ...block, status: 'done' as const }
}

if (!finalStatus || block.status === 'done' || block.status === 'error') {
return block
}

return { ...block, status: finalStatus } as WorkbenchProcessingBlock
})
}

function finalizeProcessingBlocks(
blocks: WorkbenchProcessingBlock[] | undefined
blocks: WorkbenchProcessingBlock[] | undefined,
finalStatus: Extract<WorkbenchToolBlockStatus, 'done' | 'error'> = 'done'
): WorkbenchProcessingBlock[] | undefined {
if (!blocks) return undefined
return finalizeThinkingBlocks(blocks)
return finalizeBlocks(blocks, finalStatus)
}

function mergeProcessingBlock(
Expand Down
10 changes: 10 additions & 0 deletions wework/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"semi": false,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"arrowParens": "avoid",
"endOfLine": "lf"
}
Loading
Loading