Skip to content

Commit 9d35b0f

Browse files
Remove duplicate helper for block timing
1 parent d57d5b4 commit 9d35b0f

3 files changed

Lines changed: 14 additions & 21 deletions

File tree

apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
PersistedFileAttachment,
1212
PersistedMessage,
1313
} from '@/lib/copilot/chat/persisted-message'
14-
import { normalizeMessage } from '@/lib/copilot/chat/persisted-message'
14+
import { normalizeMessage, withBlockTiming } from '@/lib/copilot/chat/persisted-message'
1515
import { resolveStreamToolOutcome } from '@/lib/copilot/chat/stream-tool-outcome'
1616
import { MOTHERSHIP_CHAT_API_PATH, STREAM_STORAGE_KEY } from '@/lib/copilot/constants'
1717
import type {
@@ -699,15 +699,6 @@ function parseStreamBatchResponse(value: unknown): StreamBatchResponse {
699699
}
700700
}
701701

702-
function withBlockTiming(
703-
persisted: Record<string, unknown>,
704-
src: ContentBlock
705-
): Record<string, unknown> {
706-
if (typeof src.timestamp === 'number') persisted.timestamp = src.timestamp
707-
if (typeof src.endedAt === 'number') persisted.endedAt = src.endedAt
708-
return persisted
709-
}
710-
711702
function toRawPersistedContentBlock(block: ContentBlock): Record<string, unknown> | null {
712703
const persisted = toRawPersistedContentBlockBody(block)
713704
return persisted ? withBlockTiming(persisted, block) : null

apps/sim/lib/copilot/chat/display-message.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
ToolCallStatus,
1717
} from '@/app/workspace/[workspaceId]/home/types'
1818
import type { PersistedContentBlock, PersistedMessage } from './persisted-message'
19+
import { withBlockTiming } from './persisted-message'
1920

2021
const STATE_TO_STATUS: Record<string, ToolCallStatus> = {
2122
[MothershipStreamV1ToolOutcome.success]: ToolCallStatus.success,
@@ -43,12 +44,6 @@ function toToolCallInfo(block: PersistedContentBlock): ToolCallInfo | undefined
4344
}
4445
}
4546

46-
function withBlockTiming(block: ContentBlock, src: PersistedContentBlock): ContentBlock {
47-
if (typeof src.timestamp === 'number') block.timestamp = src.timestamp
48-
if (typeof src.endedAt === 'number') block.endedAt = src.endedAt
49-
return block
50-
}
51-
5247
function toDisplayBlock(block: PersistedContentBlock): ContentBlock | undefined {
5348
const displayed = toDisplayBlockBody(block)
5449
return displayed ? withBlockTiming(displayed, block) : undefined

apps/sim/lib/copilot/chat/persisted-message.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,20 @@ function resolveToolState(block: ContentBlock): PersistedToolState {
8787
return tc.status as PersistedToolState
8888
}
8989

90-
function withBlockTiming<T extends PersistedContentBlock>(
91-
persisted: T,
90+
/**
91+
* Copy `timestamp` / `endedAt` from a source object onto a target object.
92+
* Shared by every block mapper (persist, display, snapshot) so the timing
93+
* metadata that drives the `Thought for Ns` chip survives the full
94+
* persist → normalize → display round-trip — and one rule lives in one place.
95+
*/
96+
export function withBlockTiming<T>(
97+
target: T,
9298
src: { timestamp?: number; endedAt?: number }
9399
): T {
94-
if (typeof src.timestamp === 'number') persisted.timestamp = src.timestamp
95-
if (typeof src.endedAt === 'number') persisted.endedAt = src.endedAt
96-
return persisted
100+
const writable = target as { timestamp?: number; endedAt?: number }
101+
if (typeof src.timestamp === 'number') writable.timestamp = src.timestamp
102+
if (typeof src.endedAt === 'number') writable.endedAt = src.endedAt
103+
return target
97104
}
98105

99106
function mapContentBlock(block: ContentBlock): PersistedContentBlock {

0 commit comments

Comments
 (0)