Skip to content

Commit d444bfa

Browse files
j15zclaude
andcommitted
refactor(copilot): tighten subblock serialization helpers
Derive KbTagDefinitionSummary from the canonical TagDefinition instead of restating its fields, make parseJsonArrayValue generic so callers drop their `as T[]` casts, and unexport the three builders helpers that no longer have consumers outside the module now that normalizeSubblockValue fronts them. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0297805 commit d444bfa

5 files changed

Lines changed: 14 additions & 19 deletions

File tree

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/document-tag-entry/document-tag-entry.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ export function DocumentTagEntry({
100100
const currentValue = isPreview ? previewValue : storeValue
101101

102102
const parseTags = (tagValue: unknown): DocumentTag[] =>
103-
(parseJsonArrayValue(tagValue) as DocumentTag[]).map((t) => ({
103+
parseJsonArrayValue<DocumentTag>(tagValue).map((t) => ({
104104
...t,
105105
fieldType: t.fieldType || 'text',
106106
collapsed: t.collapsed ?? false,
107107
}))
108108

109-
const parsedTags = parseTags(currentValue || null)
109+
const parsedTags = parseTags(currentValue)
110110
const tags: DocumentTag[] = parsedTags.length > 0 ? parsedTags : [createDefaultTag()]
111111
const isReadOnly = isPreview || disabled
112112

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/knowledge-tag-filters/knowledge-tag-filters.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ export function KnowledgeTagFilters({
102102
})
103103

104104
const parseFilters = (filterValue: unknown): TagFilter[] =>
105-
(parseJsonArrayValue(filterValue) as TagFilter[]).map((f) => ({
105+
parseJsonArrayValue<TagFilter>(filterValue).map((f) => ({
106106
...f,
107107
fieldType: f.fieldType || 'text',
108108
operator: f.operator || 'eq',
109109
collapsed: f.collapsed ?? false,
110110
}))
111111

112112
const currentValue = isPreview ? previewValue : storeValue
113-
const parsedFilters = parseFilters(currentValue || null)
113+
const parsedFilters = parseFilters(currentValue)
114114
const filters: TagFilter[] = parsedFilters.length > 0 ? parsedFilters : [createDefaultFilter()]
115115
const isReadOnly = isPreview || disabled
116116

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ export function resolvePreviewContextValue(raw: unknown): unknown {
2525
* `edit_workflow` persisted them as raw arrays until it was fixed to re-serialize, so rows
2626
* written by older builds still hold an array. Both shapes are accepted on read.
2727
*
28+
* The element type is asserted, not validated -- callers are responsible for defaulting any
29+
* fields a malformed entry may be missing.
30+
*
2831
* @param value - The stored sub-block value, of unknown shape
2932
* @returns The parsed array, or `[]` when the value is absent, unparseable, or not an array
3033
*/
31-
export function parseJsonArrayValue(value: unknown): unknown[] {
34+
export function parseJsonArrayValue<T>(value: unknown): T[] {
3235
if (!value) return []
3336
let parsed: unknown = value
3437
if (typeof value === 'string') {
@@ -38,5 +41,5 @@ export function parseJsonArrayValue(value: unknown): unknown[] {
3841
return []
3942
}
4043
}
41-
return Array.isArray(parsed) ? parsed : []
44+
return Array.isArray(parsed) ? (parsed as T[]) : []
4245
}

apps/sim/lib/copilot/tools/server/workflow/edit-workflow/builders.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,14 @@ const ARRAY_WITH_ID_SUBBLOCK_TYPES = new Set([
266266
* Subblock keys whose UI components expect a JSON string, not a raw array.
267267
* After normalizeArrayWithIds returns an array, these must be re-stringified.
268268
*/
269-
export const JSON_STRING_SUBBLOCK_KEYS = new Set([
270-
'conditions',
271-
'routes',
272-
'tagFilters',
273-
'documentTags',
274-
])
269+
const JSON_STRING_SUBBLOCK_KEYS = new Set(['conditions', 'routes', 'tagFilters', 'documentTags'])
275270

276271
/**
277272
* Normalizes array subblock values by ensuring each item has a valid UUID.
278273
* The LLM may generate arbitrary IDs like "input-desc-001" or "row-1" which need
279274
* to be converted to proper UUIDs for consistency with UI-created items.
280275
*/
281-
export function normalizeArrayWithIds(value: unknown): any[] {
276+
function normalizeArrayWithIds(value: unknown): any[] {
282277
let arr: any[]
283278

284279
if (Array.isArray(value)) {
@@ -312,7 +307,7 @@ export function normalizeArrayWithIds(value: unknown): any[] {
312307
/**
313308
* Checks if a subblock key should have its array items normalized with UUIDs.
314309
*/
315-
export function shouldNormalizeArrayIds(key: string): boolean {
310+
function shouldNormalizeArrayIds(key: string): boolean {
316311
return ARRAY_WITH_ID_SUBBLOCK_TYPES.has(key)
317312
}
318313

apps/sim/lib/copilot/vfs/serializers.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getCopilotToolDescription } from '@/lib/copilot/tools/descriptions'
22
import { isHosted } from '@/lib/core/config/env-flags'
3+
import type { TagDefinition } from '@/lib/knowledge/types'
34
import { isSubBlockHidden } from '@/lib/workflows/subblocks/visibility'
45
import { isCustomBlockType } from '@/blocks/custom/build-config'
56
import type { BlockConfig, SubBlockConfig } from '@/blocks/types'
@@ -84,11 +85,7 @@ export function serializeRecentExecutions(
8485
}
8586

8687
/** A knowledge base tag definition, reduced to the fields the agent needs to bind a tag filter. */
87-
export interface KbTagDefinitionSummary {
88-
displayName: string
89-
tagSlot: string
90-
fieldType: string
91-
}
88+
export type KbTagDefinitionSummary = Pick<TagDefinition, 'displayName' | 'tagSlot' | 'fieldType'>
9289

9390
/**
9491
* Serialize knowledge base metadata for VFS meta.json.

0 commit comments

Comments
 (0)