The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Keep AI chat thinking signatures valid across turns (#27888)
Charles Bochet9c4b4a6e9abc
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service.tsindex 8822a9e2a8f..343a9e87319 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service.ts@@ -22,6 +22,7 @@ import {import {isDefined,isNonEmptyArray,+ sortKeysDeeply,tipTapDocumentToMarkdown,} from 'twenty-shared/utils';import { type Repository } from 'typeorm';@@ -513,6 +514,9 @@ export class AgentAsyncExecutorService {tools: { ...tools, ...offeredPausingTools },model: registeredModel.model,messages: [...priorModelMessages, ...modelMessages],+ prepareStep: ({ messages }) => ({+ messages: messages.map(sortKeysDeeply),+ }),stopWhen: (step) =>isStepCount(AGENT_CONFIG.MAX_STEPS)(step) ||endsOnPausingToolCall({ steps: step.steps, offeredToolNames }) ||diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.tsindex 98269ec3c04..90d575734f2 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts@@ -24,7 +24,7 @@ import {} from 'twenty-shared/ai';import { type APP_LOCALES } from 'twenty-shared/translations';import { AppPath, FeatureFlagKey } from 'twenty-shared/types';-import { getAppPath, isDefined } from 'twenty-shared/utils';+import { getAppPath, isDefined, sortKeysDeeply } from 'twenty-shared/utils';import { AI_LATENCY_MS_BUCKET_BOUNDARIES } from 'src/engine/core-modules/metrics/constants/ai-latency-ms-bucket-boundaries.constant';import { TOOL_EXECUTION_DURATION_MS_BUCKET_BOUNDARIES } from 'src/engine/core-modules/metrics/constants/tool-execution-duration-ms-bucket-boundaries.constant';@@ -571,7 +571,10 @@ export class ChatExecutionService {stepStartedAt = performance.now();return {- messages: injectCacheBreakpoint(messages, registeredModel.sdkPackage),+ messages: injectCacheBreakpoint(+ messages.map(sortKeysDeeply),+ registeredModel.sdkPackage,+ ),};},onChunk: ({ chunk }) => {diff --git a/packages/twenty-shared/src/utils/__tests__/sortKeysDeeply.test.ts b/packages/twenty-shared/src/utils/__tests__/sortKeysDeeply.test.tsnew file mode 100644index 00000000000..acec66d5f76--- /dev/null+++ b/packages/twenty-shared/src/utils/__tests__/sortKeysDeeply.test.ts@@ -0,0 +1,28 @@+import { sortKeysDeeply } from '@/utils';++describe('sortKeysDeeply', () => {+ it('gives objects with the same entries the same key order at every depth', () => {+ const live = {+ success: true,+ result: { records: [{ name: 'A', id: '1' }] },+ };+ const reloaded = {+ result: { records: [{ id: '1', name: 'A' }] },+ success: true,+ };++ expect(JSON.stringify(sortKeysDeeply(reloaded))).toBe(+ JSON.stringify(sortKeysDeeply(live)),+ );+ });++ it('keeps values, array order and class instances', () => {+ const data = new Uint8Array([1, 2, 3]);+ const value = { ids: ['2', '1'], file: { data } };++ const sorted = sortKeysDeeply(value);++ expect(sorted).toEqual(value);+ expect(sorted.file.data).toBe(data);+ });+});diff --git a/packages/twenty-shared/src/utils/index.ts b/packages/twenty-shared/src/utils/index.tsindex db6cb4e4047..b5b12cdcb66 100644--- a/packages/twenty-shared/src/utils/index.ts+++ b/packages/twenty-shared/src/utils/index.ts@@ -228,6 +228,7 @@ export { getOrderByForFieldMetadataType } from './sort/getOrderByForFieldMetadatexport { getOrderByForRelationField } from './sort/getOrderByForRelationField';export { resolveAddressSortSubField } from './sort/resolveAddressSortSubField';export { resolvePrimaryFullNameSortSubField } from './sort/resolvePrimaryFullNameSortSubField';+export { sortKeysDeeply } from './sortKeysDeeply';export { appendCopySuffix } from './strings/appendCopySuffix';export { camelToKebab } from './strings/camelToKebab';export { camelToSnakeCase } from './strings/camelToSnakeCase';diff --git a/packages/twenty-shared/src/utils/sortKeysDeeply.ts b/packages/twenty-shared/src/utils/sortKeysDeeply.tsnew file mode 100644index 00000000000..3e6e3637bb0--- /dev/null+++ b/packages/twenty-shared/src/utils/sortKeysDeeply.ts@@ -0,0 +1,20 @@+import { isDefined } from '@/utils/validation/isDefined';++const isLiteralObject = (value: unknown): value is Record<string, unknown> =>+ isDefined(value) && Object.getPrototypeOf(value) === Object.prototype;++export const sortKeysDeeply = <TValue>(value: TValue): TValue => {+ if (Array.isArray(value)) {+ return value.map(sortKeysDeeply) as TValue;+ }++ if (!isLiteralObject(value)) {+ return value;+ }++ return Object.fromEntries(+ Object.keys(value)+ .sort()+ .map((key) => [key, sortKeysDeeply(value[key])]),+ ) as TValue;+};