Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deepMerge for array of tool objects #42

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/utils/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,64 @@ describe('utils.helpers', () => {
});
});

it('deepMerge tools', () => {
expect(
deepMerge(
{
foo: [
{
type: 'function',
function: {
name: 'foo',
description: 'foo function',
arguments: { bar: 'baz' },
},
},
],
},
{
foo: [
{
type: 'function',
function: {
name: 'foo',
description: 'foo function',
arguments: { bar: 'baz' },
},
},
{
type: 'function',
function: {
name: 'foo2',
description: 'foo function2',
arguments: { bar: 'baz2' },
},
},
],
}
)
).toEqual({
foo: [
{
type: 'function',
function: {
name: 'foo',
description: 'foo function',
arguments: { bar: 'baz' },
},
},
{
type: 'function',
function: {
name: 'foo2',
description: 'foo function2',
arguments: { bar: 'baz2' },
},
},
],
});
});

it('mergeEvents', () => {
expect(mergeEvents(undefined, {})).toEqual({});
expect(mergeEvents({}, undefined)).toEqual({});
Expand Down
51 changes: 50 additions & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
import { deepmerge as deepmergeInit } from '@fastify/deepmerge';
import type { ChatParams } from 'openai-fetch';

/** Improve preview of union types in autocomplete. */
export type Prettify<T> = { [K in keyof T]: T[K] } & {};

type DeepMergeArrayHandler = NonNullable<
NonNullable<Parameters<typeof deepmergeInit>[0]>['mergeArray']
>;

function isToolsArray(
value: unknown[]
): value is NonNullable<ChatParams['tools']> {
return value.every((item) => {
if (typeof item !== 'object' || item === null) return false;
if (
'type' in item &&
typeof item.type === 'string' &&
item.type === 'function' &&
'function' in item &&
typeof item.function === 'object' &&
item.function !== null
) {
return true;
}
return false;
});
}

const deepmergeArray: DeepMergeArrayHandler = (options) => {
const deepmerge = options.deepmerge;
const clone = options.clone;
return function (target, source) {
if (isToolsArray(target) && isToolsArray(source)) {
let i = 0;
const sl = source.length;
const il = Math.max(target.length, source.length);
const result = new Array(il);
for (i = 0; i < il; ++i) {
if (i < sl) {
result[i] = deepmerge(target[i], source[i]);
} else {
result[i] = clone(target[i]);
}
}
return result;
} else {
return [...target, ...source];
}
};
};

type DeepMerge = ReturnType<typeof deepmergeInit>;
export const deepMergeImpl: DeepMerge = deepmergeInit();
export const deepMergeImpl: DeepMerge = deepmergeInit({
mergeArray: deepmergeArray,
});

const deepMergeEventsImpl: DeepMerge = deepmergeInit({
// Note: this is not using a recursive deep merge since it isn't used for events.
Expand Down
Loading