Skip to content

Commit 5765543

Browse files
committed
improvement(utils): adopt toRecord and toRecordOrNull over inline record coercions
1 parent 812f130 commit 5765543

94 files changed

Lines changed: 278 additions & 324 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/main/ipc.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
type TerminalToolArgs,
2828
} from '@sim/terminal-protocol'
2929
import { getErrorMessage } from '@sim/utils/errors'
30-
import { isRecordLike } from '@sim/utils/object'
30+
import { isRecordLike, toRecord } from '@sim/utils/object'
3131
import { PASTE_LIMITS, utf8ByteLength } from '@sim/utils/paste'
3232
import type { BrowserWindow, IpcMainEvent, IpcMainInvokeEvent, WebContents } from 'electron'
3333
import { clipboard, ipcMain, shell } from 'electron'
@@ -856,7 +856,7 @@ export function registerIpcHandlers(deps: IpcDeps): void {
856856
) {
857857
return { ok: false, error: `Unknown browser tool: ${String(tool)}` }
858858
}
859-
const toolParams = isRecordLike(params) ? params : {}
859+
const toolParams = toRecord(params)
860860
return executeTool(
861861
scope,
862862
tool,
@@ -1532,7 +1532,7 @@ export function registerIpcHandlers(deps: IpcDeps): void {
15321532
) {
15331533
return { ok: false, error: `Unknown terminal tool: ${String(tool)}` }
15341534
}
1535-
const call = isRecordLike(params) ? params : {}
1535+
const call = toRecord(params)
15361536
if (!isTerminalOperation(call.operation)) {
15371537
return { ok: false, error: `Unknown terminal operation: ${String(call.operation)}` }
15381538
}

‎apps/sim/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/browser-agent-icon.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useState } from 'react'
44
import { isBrowserToolName } from '@sim/browser-protocol'
55
import { cn } from '@sim/emcn'
66
import { Globe } from '@sim/emcn/icons'
7-
import { isRecordLike } from '@sim/utils/object'
7+
import { isRecordLike, toRecordOrNull } from '@sim/utils/object'
88
import { isBrowserAgentAvailable } from '@/lib/browser-agent/transport'
99
import { useChatSurface } from '@/app/workspace/[workspaceId]/home/components/chat-surface-context'
1010
import type { AgentGroupItem } from '@/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/agent-group-view'
@@ -40,7 +40,7 @@ export function getBrowserAgentFaviconUrl(items: AgentGroupItem[]): string | nul
4040
return typeof params?.url === 'string' ? pageFaviconUrl(params.url) : null
4141
}
4242

43-
const output = result?.success && isRecordLike(result.output) ? result.output : null
43+
const output = result?.success ? toRecordOrNull(result.output) : null
4444
if (output) {
4545
if (isRecordLike(output.activeTab) && typeof output.activeTab.url === 'string') {
4646
return pageFaviconUrl(output.activeTab.url)

‎apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isRecordLike } from '@sim/utils/object'
1+
import { isRecordLike, toRecord } from '@sim/utils/object'
22
import { resolveStreamToolOutcome } from '@/lib/copilot/chat/stream-tool-outcome'
33
import {
44
MothershipStreamV1CompletionStatus,
@@ -230,7 +230,7 @@ function rebindResolvedIntegrationCall(node: ToolNode, toolName: string): void {
230230
* through the `unknown`-typed {@link isRecordLike} guard rather than a double cast.
231231
*/
232232
function payloadRecord(payload: unknown): Record<string, unknown> {
233-
return isRecordLike(payload) ? payload : {}
233+
return toRecord(payload)
234234
}
235235

236236
/** Parses a wire `ts` to epoch ms, or undefined when absent/unparseable. */

‎apps/sim/background/webhook-execution.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { createLogger, type RequestContext, runWithRequestContext } from '@sim/l
1010
import { toError } from '@sim/utils/errors'
1111
import { interruptibleSleep } from '@sim/utils/helpers'
1212
import { generateId } from '@sim/utils/id'
13-
import { isRecordLike } from '@sim/utils/object'
13+
import { isRecordLike, toRecord } from '@sim/utils/object'
1414
import { backoffWithJitter } from '@sim/utils/retry'
1515
import { task, timeout } from '@trigger.dev/sdk'
1616
import { eq } from 'drizzle-orm'
@@ -1091,9 +1091,7 @@ async function executeWebhookJobInternal(
10911091
})
10921092
}
10931093

1094-
const persistedProviderConfig = isRecordLike(resolvedWebhookRecord.providerConfig)
1095-
? resolvedWebhookRecord.providerConfig
1096-
: {}
1094+
const persistedProviderConfig = toRecord(resolvedWebhookRecord.providerConfig)
10971095
const slackStreamConfig =
10981096
payload.provider === 'slack' || payload.provider === 'slack_app'
10991097
? readSlackStreamResponseConfig(persistedProviderConfig)

‎apps/sim/ee/scim/lib/protocol/user-patch.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ScimUserAttributes, ScimUserEmail } from '@sim/db/schema'
2-
import { isRecordLike } from '@sim/utils/object'
2+
import { isRecordLike, toRecord } from '@sim/utils/object'
33
import type { ScimPatchOperation } from '@/lib/api/contracts/scim'
44
import { invalidPath, invalidValue, mutability, noTarget } from '@/ee/scim/lib/protocol/errors'
55
import {
@@ -331,7 +331,7 @@ function applyExtraOperation(
331331
else {
332332
if (!isRecordLike(value)) throw invalidValue(`${path} requires an object value`)
333333
const current = user.extra[extension.schema]
334-
user.extra[extension.schema] = { ...(isRecordLike(current) ? current : {}), ...value }
334+
user.extra[extension.schema] = { ...toRecord(current), ...value }
335335
}
336336
return
337337
}

‎apps/sim/executor/execution/block-executor.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLogger, type Logger } from '@sim/logger'
22
import { describeError } from '@sim/utils/errors'
33
import { sleep } from '@sim/utils/helpers'
4-
import { isRecordLike } from '@sim/utils/object'
4+
import { isRecordLike, toRecord } from '@sim/utils/object'
55
import { DrizzleQueryError } from 'drizzle-orm/errors'
66
import { isTimeoutAbortReason } from '@/lib/core/execution-limits/types'
77
import { redactApiKeys } from '@/lib/core/security/redaction'
@@ -970,7 +970,7 @@ export class BlockExecutor {
970970
}
971971
})()
972972
: mapping
973-
inputs = isRecordLike(parsed) ? parsed : {}
973+
inputs = toRecord(parsed)
974974
}
975975

976976
const result: Record<string, any> = {}

‎apps/sim/hooks/queries/custom-tools.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createLogger } from '@sim/logger'
2-
import { isRecordLike } from '@sim/utils/object'
2+
import { isRecordLike, toRecord } from '@sim/utils/object'
33
import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
44
import { requestJson } from '@/lib/api/client/request'
55
import {
@@ -125,8 +125,8 @@ async function fetchCustomTools(
125125
}
126126

127127
const functionSchema = tool.schema.function
128-
const parameters = isRecordLike(functionSchema.parameters) ? functionSchema.parameters : {}
129-
const properties = isRecordLike(parameters.properties) ? parameters.properties : {}
128+
const parameters = toRecord(functionSchema.parameters)
129+
const properties = toRecord(parameters.properties)
130130
const required = Array.isArray(parameters.required)
131131
? parameters.required.filter((value): value is string => typeof value === 'string')
132132
: undefined

‎apps/sim/hooks/queries/organization.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createLogger } from '@sim/logger'
2-
import { isRecordLike } from '@sim/utils/object'
2+
import { isRecordLike, toRecord } from '@sim/utils/object'
33
import {
44
queryOptions,
55
type UseQueryResult,
@@ -269,7 +269,7 @@ export function useUpdateOrganizationUsageLimit() {
269269
organizationKeys.billing(organizationId),
270270
(old: unknown) => {
271271
if (!isRecordLike(old) || !isRecordLike(old.data)) return old
272-
const usage = isRecordLike(old.data.usage) ? old.data.usage : {}
272+
const usage = toRecord(old.data.usage)
273273
const currentUsage =
274274
readNumber(old.data.currentUsage) ??
275275
readNumber(usage.current) ??

‎apps/sim/lib/auth/connectors/managed-oauth.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createHash } from 'node:crypto'
2-
import { isRecordLike } from '@sim/utils/object'
2+
import { isRecordLike, toRecord } from '@sim/utils/object'
33
import type { OAuth2Tokens } from 'better-auth/oauth2'
44
import type { GenericOAuthConfig } from 'better-auth/plugins'
55
import { OAuth2Client, type TokenPayload } from 'google-auth-library'
@@ -830,7 +830,7 @@ const USER_INFO_MANAGED_OAUTH_CONNECTORS = new Map<string, () => ManagedOAuthCon
830830
},
831831
parse: (profile) => {
832832
const account = asProfileRecord(profile, 'Dropbox')
833-
const name = isRecordLike(account.name) ? account.name : {}
833+
const name = toRecord(account.name)
834834
return withOptionalIdentityFields(
835835
{
836836
providerSubjectId: requireIdentityField(account.account_id, 'Dropbox account id'),
@@ -889,15 +889,15 @@ const USER_INFO_MANAGED_OAUTH_CONNECTORS = new Map<string, () => ManagedOAuthCon
889889
* `bot.owner.user`. A workspace-owned internal integration reports
890890
* `{ type: 'workspace' }` and identifies nobody, which cannot be bound to an invitation.
891891
*/
892-
const bot = isRecordLike(self.bot) ? self.bot : {}
893-
const owner = isRecordLike(bot.owner) ? bot.owner : {}
892+
const bot = toRecord(self.bot)
893+
const owner = toRecord(bot.owner)
894894
if (owner.type !== 'user') {
895895
throw new Error(
896896
'Notion returned a workspace-owned integration, which identifies no person to bind this invitation to'
897897
)
898898
}
899899
const user = asProfileRecord(owner.user, 'Notion')
900-
const person = isRecordLike(user.person) ? user.person : {}
900+
const person = toRecord(user.person)
901901
return withOptionalIdentityFields(
902902
{
903903
providerSubjectId: requireIdentityField(user.id, 'Notion user id'),
@@ -1004,7 +1004,7 @@ const USER_INFO_MANAGED_OAUTH_CONNECTORS = new Map<string, () => ManagedOAuthCon
10041004
scopes: {
10051005
from: 'profile',
10061006
read: (profile) => {
1007-
const metadata = isRecordLike(profile) ? profile : {}
1007+
const metadata = toRecord(profile)
10081008
if (Array.isArray(metadata.scopes)) {
10091009
return metadata.scopes.filter((scope): scope is string => typeof scope === 'string')
10101010
}
@@ -1135,7 +1135,7 @@ const USER_INFO_MANAGED_OAUTH_CONNECTORS = new Map<string, () => ManagedOAuthCon
11351135
parse: (profile) => {
11361136
const envelope = asProfileRecord(profile, 'Asana')
11371137
const user = asProfileRecord(envelope.data, 'Asana')
1138-
const photo = isRecordLike(user.photo) ? user.photo : {}
1138+
const photo = toRecord(user.photo)
11391139
return withOptionalIdentityFields(
11401140
{
11411141
providerSubjectId: requireIdentityField(user.gid, 'Asana user id'),

‎apps/sim/lib/billing/core/reporting-period.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isRecordLike } from '@sim/utils/object'
1+
import { toRecord } from '@sim/utils/object'
22
import { defaultBillingPeriod } from '@/lib/billing/core/billing-period'
33
import { isEnterprise } from '@/lib/billing/plan-helpers'
44

@@ -91,7 +91,7 @@ export function resolveSubscriptionUsagePeriod(
9191
return subscription.usagePeriod
9292
}
9393
if (subscription && isEnterprise(subscription.plan)) {
94-
const metadata = isRecordLike(subscription.metadata) ? subscription.metadata : {}
94+
const metadata = toRecord(subscription.metadata)
9595
const anchor = metadata[ENTERPRISE_REPORTING_PERIOD_ANCHOR_METADATA_KEY]
9696
const interval =
9797
parseBillingInterval(metadata[ENTERPRISE_REPORTING_PERIOD_INTERVAL_METADATA_KEY]) ??

0 commit comments

Comments
 (0)