⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.
TypeScript agenticagileai-agentsbug-reportingbug-trackerganttissue-trackerjirajira-alternativekanbanlinearlinear-alternativeopen-sourceproduct-managementproject-managementroadmapself-hostedsprint-planningteam-collaborationtrello-alternative
perf(api): agent tools declare each numeric argument once (#783)
Pasqual Troncone862063c8557d
diff --git a/apps/api/src/modules/agents/core/__tests__/integration/route-tools.test.ts b/apps/api/src/modules/agents/core/__tests__/integration/route-tools.test.tsindex f5c986b9..03edadc3 100644--- a/apps/api/src/modules/agents/core/__tests__/integration/route-tools.test.ts+++ b/apps/api/src/modules/agents/core/__tests__/integration/route-tools.test.ts@@ -277,6 +277,30 @@ describe('internal agent route tools', () => {}});+ // The tool definitions go to the model on every step; a numeric argument written as a+ // numeric string or a number would be paid for twice.+ it('gives the model every numeric argument as a number only', async () => {+ const { asOwner } = await setup();+ const tools = await toolsFor(+ asOwner,+ AGENT_ACTIONS.map((tool) => tool.key),+ );+ const schemas = Object.entries(tools).map(([key, tool]) => {+ const input = tool.inputSchema as unknown as { getJsonSchema(): unknown };+ return [key, JSON.stringify(input.getJsonSchema())] as const;+ });+ expect(schemas.length).toBeGreaterThan(0);++ for (const [key, schema] of schemas) {+ expect({ key, numericString: /"format":"(numeric|integer)"/.test(schema) }).toEqual({+ key,+ numericString: false,+ });+ }+ const getIssue = schemas.find(([key]) => key === 'get_issue')?.[1];+ expect(JSON.parse(getIssue!).properties.issueId).toEqual({ type: 'number' });+ });+// The catalog states which role an action needs, which it can only do when it// carries the permission the route behind the action asserts.it('reports the permission of the route behind each action', () => {diff --git a/apps/api/src/modules/agents/core/__tests__/unit/numeric-schema.test.ts b/apps/api/src/modules/agents/core/__tests__/unit/numeric-schema.test.tsnew file mode 100644index 00000000..a6cd47d4--- /dev/null+++ b/apps/api/src/modules/agents/core/__tests__/unit/numeric-schema.test.ts@@ -0,0 +1,80 @@+import { describe, it, expect } from 'bun:test';+import { t } from 'elysia';+import { collapseNumericSchema } from '../../runtime/tools/numeric-schema';++// Runs on Elysia's own output, so an Elysia upgrade that changes the shape fails here+// instead of silently disabling the collapse.++describe('collapseNumericSchema', () => {+ it('collapses a plain Numeric field to just its number branch', () => {+ const schema = t.Object({ limit: t.Optional(t.Numeric({ description: 'Max results.' })) });+ expect(collapseNumericSchema(schema)).toEqual({+ type: 'object',+ properties: { limit: { description: 'Max results.', type: 'number' } },+ });+ });++ it('keeps real constraints and drops the string branch default of 0', () => {+ const schema = t.Object({+ count: t.Numeric({ minimum: 0, maximum: 200, description: 'Page size.' }),+ });+ expect(collapseNumericSchema(schema)).toEqual({+ type: 'object',+ required: ['count'],+ properties: {+ count: { minimum: 0, maximum: 200, description: 'Page size.', type: 'number' },+ },+ });+ });++ it('collapses an array of Integer (labelIds) at the items level', () => {+ const schema = t.Object({+ labelIds: t.Optional(t.Array(t.Integer(), { description: 'Label ids to attach.' })),+ });+ expect(collapseNumericSchema(schema)).toEqual({+ type: 'object',+ properties: {+ labelIds: {+ description: 'Label ids to attach.',+ type: 'array',+ items: { type: 'integer' },+ },+ },+ });+ });++ it('collapses the inner numeric anyOf but leaves the outer null union real', () => {+ // typeId/cycleId's shape: t.Nullable(t.Integer(...)) nests the numeric anyOf+ // inside a real union with {type: 'null'}. Only the inner one collapses.+ const schema = t.Object({+ typeId: t.Optional(t.Nullable(t.Integer({ description: 'Issue type id, or null.' }))),+ });+ expect(collapseNumericSchema(schema)).toEqual({+ type: 'object',+ properties: {+ typeId: {+ nullable: true,+ anyOf: [{ description: 'Issue type id, or null.', type: 'integer' }, { type: 'null' }],+ },+ },+ });+ });++ // The collapse rebuilds nodes as plain objects, dropping TypeBox's Symbol tag; compare via JSON,+ // which is all the model sees.+ function asJson(value: unknown): unknown {+ return JSON.parse(JSON.stringify(value));+ }++ it('leaves a real string-or-null union untouched', () => {+ const schema = t.Object({ note: t.Nullable(t.String({ description: 'A note.' })) });+ expect(collapseNumericSchema(schema)).toEqual(asJson(schema));+ });++ it('leaves a literal enum union untouched', () => {+ const schema = t.Object({+ state: t.Union([t.Literal('open'), t.Literal('all')]),+ });+ expect(collapseNumericSchema(schema)).toEqual(asJson(schema));+ });+});diff --git a/apps/api/src/modules/agents/core/runtime/tools/numeric-schema.ts b/apps/api/src/modules/agents/core/runtime/tools/numeric-schema.tsnew file mode 100644index 00000000..f7eceeb4--- /dev/null+++ b/apps/api/src/modules/agents/core/runtime/tools/numeric-schema.ts@@ -0,0 +1,57 @@+// t.Numeric()/t.Integer() render as a string|number anyOf repeating every constraint, in each+// tool definition on every step. Agents only need the number: dispatch.ts stringifies query args.+function isNumericStringBranch(node: unknown): boolean {+ if (typeof node !== 'object' || node === null) return false;+ const branch = node as Record<string, unknown>;+ if (branch.type !== 'string') return false;+ if (branch.format !== 'numeric' && branch.format !== 'integer') return false;+ for (const key of Object.keys(branch)) {+ if (key !== 'type' && key !== 'format' && key !== 'default') return false;+ }+ return !('default' in branch) || branch.default === 0;+}++function isNumericBranch(node: unknown): node is Record<string, unknown> {+ if (typeof node !== 'object' || node === null) return false;+ const type = (node as Record<string, unknown>).type;+ return type === 'number' || type === 'integer';+}++// Recurses through every nested schema position; only the exact string+number pair collapses,+// so enums, literal unions and nullable unions (whose inner anyOf collapses first) stay.+export function collapseNumericSchema(node: unknown): unknown {+ if (Array.isArray(node)) return node.map(collapseNumericSchema);+ if (typeof node !== 'object' || node === null) return node;++ const out: Record<string, unknown> = {};+ for (const [key, value] of Object.entries(node as Record<string, unknown>)) {+ if (+ key === 'properties' &&+ typeof value === 'object' &&+ value !== null &&+ !Array.isArray(value)+ ) {+ const properties: Record<string, unknown> = {};+ for (const [name, propSchema] of Object.entries(value as Record<string, unknown>)) {+ properties[name] = collapseNumericSchema(propSchema);+ }+ out[key] = properties;+ } else if (key === 'items' || key === 'additionalProperties' || key === 'not') {+ out[key] = collapseNumericSchema(value);+ } else if (key === 'anyOf' || key === 'oneOf' || key === 'allOf') {+ out[key] = Array.isArray(value) ? value.map(collapseNumericSchema) : value;+ } else {+ out[key] = value;+ }+ }++ const branches = out.anyOf;+ if (!Array.isArray(branches) || branches.length !== 2) return out;+ const [first, second] = branches;+ let numberBranch: Record<string, unknown>;+ if (isNumericStringBranch(first) && isNumericBranch(second)) numberBranch = second;+ else if (isNumericStringBranch(second) && isNumericBranch(first)) numberBranch = first;+ else return out;+ const { anyOf: _anyOf, ...rest } = out;+ return { ...rest, ...numberBranch };+}diff --git a/apps/api/src/modules/agents/core/runtime/tools/route-tools.ts b/apps/api/src/modules/agents/core/runtime/tools/route-tools.tsindex 11d98db5..b09d3d03 100644--- a/apps/api/src/modules/agents/core/runtime/tools/route-tools.ts+++ b/apps/api/src/modules/agents/core/runtime/tools/route-tools.ts@@ -21,6 +21,7 @@ import {toolMeta,type ToolMeta,} from './catalog';+import { collapseNumericSchema } from './numeric-schema';// The tools an internal agent uses to work in its project, built from the routes// tagged with mcpTool() — the same table the MCP endpoint serves. A tool call is@@ -49,7 +50,7 @@ function jsonSchemaInput(schema: McpInputSchema): z.ZodType<Record<string, unknoreturn {[AI_SDK_SCHEMA]: true,_type: undefined,- jsonSchema: schema,+ jsonSchema: collapseNumericSchema(schema),validate: undefined,} as unknown as z.ZodType<Record<string, unknown>>;}