Skip to content

Commit 8623e0a

Browse files
committed
use lodash instead of custom utils
1 parent 9f053fb commit 8623e0a

File tree

2 files changed

+6
-60
lines changed

2 files changed

+6
-60
lines changed

common/src/util/messages.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { has, isEqual } from 'lodash'
2+
13
import { buildArray } from './array'
2-
import { deepCopy, deepEqual, hasKey } from './object'
4+
import { deepCopy } from './object'
35
import { getToolCallString } from '../tools/utils'
46

57
import type {
@@ -40,7 +42,7 @@ export function withoutCacheControl<
4042
T extends { providerOptions?: ProviderMetadata },
4143
>(obj: T): T {
4244
const wrapper = deepCopy(obj)
43-
if (hasKey(wrapper.providerOptions?.anthropic?.cacheControl, 'type')) {
45+
if (has(wrapper.providerOptions?.anthropic?.cacheControl, 'type')) {
4446
delete wrapper.providerOptions?.anthropic?.cacheControl?.type
4547
}
4648
if (
@@ -53,7 +55,7 @@ export function withoutCacheControl<
5355
delete wrapper.providerOptions?.anthropic
5456
}
5557

56-
if (hasKey(wrapper.providerOptions?.openrouter?.cacheControl, 'type')) {
58+
if (has(wrapper.providerOptions?.openrouter?.cacheControl, 'type')) {
5759
delete wrapper.providerOptions?.openrouter?.cacheControl?.type
5860
}
5961
if (
@@ -222,7 +224,7 @@ export function convertCbToModelMessages({
222224
if (
223225
lastMessage.keepDuringTruncation !== message.keepDuringTruncation &&
224226
lastMessage.timeToLive !== message.timeToLive &&
225-
!deepEqual(lastMessage.providerOptions, message.providerOptions)
227+
!isEqual(lastMessage.providerOptions, message.providerOptions)
226228
) {
227229
aggregated.push(message)
228230
continue

common/src/util/object.ts

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { isEqual, mapValues, union } from 'lodash'
22

3-
import type { JSONValue } from '../types/json'
4-
53
export const removeUndefinedProps = <T extends object>(obj: T): T => {
64
const newObj: any = {}
75

@@ -132,60 +130,6 @@ export function errorToObject(value: any) {
132130
return value
133131
}
134132

135-
export function hasKey<T extends object, K extends PropertyKey>(
136-
obj: T | undefined | null | string | number | boolean,
137-
key: K,
138-
): obj is T & Record<K, unknown> {
139-
return obj != null && typeof obj === 'object' && key in obj
140-
}
141-
142133
export function deepCopy<T>(obj: T): T {
143134
return JSON.parse(JSON.stringify(obj)) as T
144135
}
145-
146-
export function deepEqual(
147-
a: JSONValue | undefined,
148-
b: JSONValue | undefined,
149-
): boolean {
150-
if (a === b) return true
151-
152-
if (
153-
typeof a === 'string' ||
154-
typeof b === 'string' ||
155-
typeof a === 'boolean' ||
156-
typeof b === 'boolean' ||
157-
typeof a === 'number' ||
158-
typeof b === 'number' ||
159-
a === null ||
160-
b === null ||
161-
a === undefined ||
162-
b === undefined
163-
) {
164-
return false
165-
}
166-
167-
if (Array.isArray(a) || Array.isArray(b)) {
168-
if (!Array.isArray(a)) return false
169-
if (!Array.isArray(b)) return false
170-
171-
if (a.length !== b.length) return false
172-
173-
for (let i = 0; i < a.length; i++) {
174-
if (!deepEqual(a[i], b[i])) return false
175-
}
176-
177-
return true
178-
}
179-
180-
const keysA = Object.keys(a)
181-
const keysB = Object.keys(b)
182-
183-
if (keysA.length !== keysB.length) return false
184-
185-
for (const key of keysA) {
186-
if (!keysB.includes(key)) return false
187-
if (!deepEqual(a[key], b[key])) return false
188-
}
189-
190-
return true
191-
}

0 commit comments

Comments
 (0)