fix(ai) - optimize crud tools (#21133)

- **Add delete many**, `delete_many_{object}` added alongside the
existing `delete_one_{object}`.
- **Uniformize naming**, crud module, type names, and MCP helper
constants renamed for consistency.
- **Optimize tool schema (learn phase)**
  - `find_many(_companies)`: **7 158 → 2 700 tokens**
  - `find_one(_company)`: **280 → 126 tokens**
  -  ....
- Main mechanism: `reused: 'ref'` (line 7 of
`to-tool-json-schema.util.ts`). Zod walks the schema tree, tracks which
Zod schema instances appear more than once, and emits each reused
instance exactly once in `$defs`, replacing all subsequent occurrences
with a `$ref`. Works because filter and value schemas are now extracted
as shared objects.

- **Optimize system prompt (tool catalog)**, DATABASE_CRUD section
restructured to list operation patterns (`find_many_{object}`, …) once +
objects once, instead of the full N×M cross-product of tool names.
- **Optimize execute_tool**, shared record-properties schema (same
`$defs` deduplication applies at call time); introduced `upsert_many`;
added `selectedFields` to `find_*` so the agent only fetches the fields
it needs.
This commit is contained in:
Etienne
2026-06-03 19:57:40 +02:00
committed by GitHub
parent 50c9b68e81
commit 15eaabdbc1
40 changed files with 1610 additions and 633 deletions
@@ -23,14 +23,14 @@ describe('resolveToolInput', () => {
it('should unwrap execute_tool input', () => {
const input = {
toolName: 'find_companies',
toolName: 'find_many_companies',
arguments: { filter: { name: 'Acme' } },
};
const result = resolveToolInput(input, 'execute_tool');
expect(result).toEqual({
resolvedInput: { filter: { name: 'Acme' } },
resolvedToolName: 'find_companies',
resolvedToolName: 'find_many_companies',
});
});
@@ -108,13 +108,13 @@ describe('getToolDisplayMessage', () => {
describe('learn_tools', () => {
it('should show tool names when provided', () => {
const message = getToolDisplayMessage(
{ toolNames: ['find_companies', 'create_task'] },
{ toolNames: ['find_many_companies', 'create_one_task'] },
'learn_tools',
true,
);
expect(message).toContain('Learned');
expect(message).toContain('find_companies, create_task');
expect(message).toContain('find_many_companies, create_one_task');
});
it('should show generic message without tool names', () => {
@@ -182,13 +182,13 @@ describe('getToolDisplayMessage', () => {
describe('execute_tool wrapper', () => {
it('should unwrap execute_tool and display inner tool name', () => {
const message = getToolDisplayMessage(
{ toolName: 'find_companies', arguments: { limit: 10 } },
{ toolName: 'find_many_companies', arguments: { limit: 10 } },
'execute_tool',
true,
);
expect(message).toContain('Ran');
expect(message).toContain('find companies');
expect(message).toContain('find many companies');
});
});
});