Skip to content

Commit 6620206

Browse files
committed
fix: formatting and imports
1 parent 98d67c9 commit 6620206

4 files changed

Lines changed: 86 additions & 72 deletions

File tree

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/chat/chat.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { type KeyboardEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react'
44
import { createLogger } from '@sim/logger'
5-
import { formatOutputForWorkflow } from '@/lib/core/utils/format-output'
65
import {
76
AlertCircle,
87
ArrowDownToLine,
@@ -25,6 +24,7 @@ import {
2524
} from '@/components/emcn'
2625
import { useSession } from '@/lib/auth/auth-client'
2726
import { cn } from '@/lib/core/utils/cn'
27+
import { formatOutputForWorkflow } from '@/lib/core/utils/format-output'
2828
import {
2929
extractBlockIdFromOutputId,
3030
extractPathFromOutputId,

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/chat/components/chat-message/chat-message.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useMemo } from 'react'
2-
32
import { formatOutputForChat } from '@/lib/core/utils/format-output'
43
import { StreamingIndicator } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/copilot/components/copilot-message/components/smooth-streaming'
54

@@ -95,10 +94,7 @@ const WordWrap = ({ text }: { text: string }) => {
9594
* Renders a chat message with optional file attachments
9695
*/
9796
export function ChatMessage({ message }: ChatMessageProps) {
98-
const formattedContent = useMemo(
99-
() => formatOutputForChat(message.content),
100-
[message.content]
101-
)
97+
const formattedContent = useMemo(() => formatOutputForChat(message.content), [message.content])
10298

10399
const handleAttachmentClick = (attachment: ChatAttachment) => {
104100
const validDataUrl = attachment.dataUrl?.trim()

apps/sim/lib/core/utils/format-output.test.ts

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describe, expect, it } from 'vitest'
22
import {
3-
formatOutputForDisplay,
43
formatOutputForChat,
4+
formatOutputForDisplay,
55
formatOutputForWorkflow,
66
formatOutputRaw,
77
formatOutputSafe,
8-
isOutputSafe
8+
isOutputSafe,
99
} from './format-output'
1010

1111
describe('format-output utilities', () => {
@@ -37,32 +37,22 @@ describe('format-output utilities', () => {
3737
data: {
3838
response: {
3939
message: {
40-
content: 'Deep text'
41-
}
42-
}
43-
}
40+
content: 'Deep text',
41+
},
42+
},
43+
},
4444
}
4545
expect(formatOutputForDisplay(nested)).toBe('Deep text')
4646
})
4747

4848
// Arrays
4949
it('handles arrays of text objects', () => {
50-
const arr = [
51-
{ text: 'Line 1' },
52-
{ text: 'Line 2' },
53-
{ content: 'Line 3' }
54-
]
50+
const arr = [{ text: 'Line 1' }, { text: 'Line 2' }, { content: 'Line 3' }]
5551
expect(formatOutputForDisplay(arr)).toBe('Line 1 Line 2 Line 3')
5652
})
5753

5854
it('handles mixed arrays', () => {
59-
const mixed = [
60-
'String',
61-
{ text: 'Object text' },
62-
123,
63-
null,
64-
{ message: 'Message text' }
65-
]
55+
const mixed = ['String', { text: 'Object text' }, 123, null, { message: 'Message text' }]
6656
expect(formatOutputForDisplay(mixed)).toBe('String Object text 123 Message text')
6757
})
6858

@@ -103,7 +93,7 @@ describe('format-output utilities', () => {
10393
const buffer = Buffer.from('Hello Buffer')
10494
expect(formatOutputForDisplay(buffer)).toBe('Hello Buffer')
10595

106-
const binaryBuffer = Buffer.from([0xFF, 0xFE, 0x00, 0x01])
96+
const binaryBuffer = Buffer.from([0xff, 0xfe, 0x00, 0x01])
10797
expect(formatOutputForDisplay(binaryBuffer)).toBe('[Binary Data]')
10898
})
10999

@@ -118,10 +108,12 @@ describe('format-output utilities', () => {
118108
// Whitespace handling
119109
it('preserves whitespace when requested', () => {
120110
const spaced = 'Line 1\n\nLine 2\t\tTabbed'
121-
expect(formatOutputForDisplay(spaced, { preserveWhitespace: true }))
122-
.toBe('Line 1\n\nLine 2\t\tTabbed')
123-
expect(formatOutputForDisplay(spaced, { preserveWhitespace: false }))
124-
.toBe('Line 1 Line 2 Tabbed')
111+
expect(formatOutputForDisplay(spaced, { preserveWhitespace: true })).toBe(
112+
'Line 1\n\nLine 2\t\tTabbed'
113+
)
114+
expect(formatOutputForDisplay(spaced, { preserveWhitespace: false })).toBe(
115+
'Line 1 Line 2 Tabbed'
116+
)
125117
})
126118

127119
// Mode-specific formatting
@@ -148,7 +140,7 @@ describe('format-output utilities', () => {
148140
const customObj = {
149141
toString() {
150142
return 'Custom String'
151-
}
143+
},
152144
}
153145
expect(formatOutputForDisplay(customObj)).toBe('Custom String')
154146
})
@@ -157,7 +149,7 @@ describe('format-output utilities', () => {
157149
const obj = {
158150
func: () => console.log('test'),
159151
undef: undefined,
160-
sym: Symbol('test')
152+
sym: Symbol('test'),
161153
}
162154
const result = formatOutputForDisplay(obj, { mode: 'raw' })
163155
expect(result).toContain('[Function]')
@@ -214,11 +206,14 @@ describe('format-output utilities', () => {
214206
describe('error handling', () => {
215207
it('handles errors gracefully', () => {
216208
// Create object that throws on property access
217-
const evil = new Proxy({}, {
218-
get() {
219-
throw new Error('Evil object!')
209+
const evil = new Proxy(
210+
{},
211+
{
212+
get() {
213+
throw new Error('Evil object!')
214+
},
220215
}
221-
})
216+
)
222217

223218
const result = formatOutputForDisplay(evil)
224219
expect(result).toContain('[')
@@ -241,29 +236,33 @@ describe('format-output utilities', () => {
241236
describe('real-world LLM outputs', () => {
242237
it('handles OpenAI format', () => {
243238
const openAIResponse = {
244-
choices: [{
245-
message: {
246-
content: 'AI response here'
247-
}
248-
}]
239+
choices: [
240+
{
241+
message: {
242+
content: 'AI response here',
243+
},
244+
},
245+
],
249246
}
250247
expect(formatOutputForDisplay(openAIResponse)).toBe('AI response here')
251248
})
252249

253250
it('handles Anthropic format', () => {
254251
const anthropicResponse = {
255-
content: [{
256-
text: 'Claude response'
257-
}]
252+
content: [
253+
{
254+
text: 'Claude response',
255+
},
256+
],
258257
}
259258
expect(formatOutputForDisplay(anthropicResponse)).toBe('Claude response')
260259
})
261260

262261
it('handles streaming chunks', () => {
263262
const chunk = {
264263
delta: {
265-
content: 'Streaming text'
266-
}
264+
content: 'Streaming text',
265+
},
267266
}
268267
expect(formatOutputForDisplay(chunk)).toBe('Streaming text')
269268
})
@@ -272,11 +271,11 @@ describe('format-output utilities', () => {
272271
const toolOutput = {
273272
result: {
274273
data: {
275-
output: 'Tool execution result'
276-
}
277-
}
274+
output: 'Tool execution result',
275+
},
276+
},
278277
}
279278
expect(formatOutputForDisplay(toolOutput)).toBe('Tool execution result')
280279
})
281280
})
282-
})
281+
})

apps/sim/lib/core/utils/format-output.ts

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ import { createLogger } from '@sim/logger'
33
const logger = createLogger('FormatOutput')
44

55
// Type definitions for better type safety
6-
type TextFieldName = 'text' | 'content' | 'message' | 'body' | 'value' | 'result' | 'output' | 'response' | 'answer' | 'reply' | 'data'
6+
type TextFieldName =
7+
| 'text'
8+
| 'content'
9+
| 'message'
10+
| 'body'
11+
| 'value'
12+
| 'result'
13+
| 'output'
14+
| 'response'
15+
| 'answer'
16+
| 'reply'
17+
| 'data'
718
type FormatMode = 'chat' | 'workflow' | 'raw'
819

920
interface FormatOptions {
@@ -30,8 +41,17 @@ interface TextExtractable {
3041

3142
// Constants
3243
const TEXT_FIELDS: readonly TextFieldName[] = [
33-
'text', 'content', 'message', 'body', 'value', 'result',
34-
'output', 'response', 'answer', 'reply', 'data',
44+
'text',
45+
'content',
46+
'message',
47+
'body',
48+
'value',
49+
'result',
50+
'output',
51+
'response',
52+
'answer',
53+
'reply',
54+
'data',
3555
] as const
3656

3757
const MAX_STRING_LENGTH = 50000
@@ -76,9 +96,7 @@ function extractText(value: unknown, depth = 0): string | null {
7696
}
7797

7898
// For arrays, concatenate all text content
79-
const texts = value
80-
.map(item => extractText(item, depth + 1))
81-
.filter(Boolean)
99+
const texts = value.map((item) => extractText(item, depth + 1)).filter(Boolean)
82100

83101
return texts.length > 0 ? texts.join(' ') : null
84102
}
@@ -140,19 +158,23 @@ function extractText(value: unknown, depth = 0): string | null {
140158
function safeStringify(value: unknown): string {
141159
try {
142160
const seen = new WeakSet()
143-
return JSON.stringify(value, (_, val) => {
144-
if (val === undefined) return '[undefined]'
145-
if (typeof val === 'function') return '[Function]'
146-
if (typeof val === 'symbol') return '[Symbol]'
147-
if (typeof val === 'bigint') return val.toString()
148-
149-
if (typeof val === 'object' && val !== null) {
150-
if (seen.has(val)) return '[Circular]'
151-
seen.add(val)
152-
}
161+
return JSON.stringify(
162+
value,
163+
(_, val) => {
164+
if (val === undefined) return '[undefined]'
165+
if (typeof val === 'function') return '[Function]'
166+
if (typeof val === 'symbol') return '[Symbol]'
167+
if (typeof val === 'bigint') return val.toString()
168+
169+
if (typeof val === 'object' && val !== null) {
170+
if (seen.has(val)) return '[Circular]'
171+
seen.add(val)
172+
}
153173

154-
return val
155-
}, 2)
174+
return val
175+
},
176+
2
177+
)
156178
} catch (error) {
157179
logger.debug('Stringify failed', { error })
158180
return '[Unable to display]'
@@ -163,10 +185,7 @@ function safeStringify(value: unknown): string {
163185
* Format output for display with smart text extraction
164186
* Optimized for performance and common use cases
165187
*/
166-
export function formatOutputForDisplay(
167-
output: unknown,
168-
options: FormatOptions = {}
169-
): string {
188+
export function formatOutputForDisplay(output: unknown, options: FormatOptions = {}): string {
170189
const {
171190
mode = 'chat',
172191
maxLength = MAX_STRING_LENGTH,
@@ -285,4 +304,4 @@ export function formatOutputSafe(output: unknown): string {
285304
}
286305

287306
return formatted
288-
}
307+
}

0 commit comments

Comments
 (0)