diff --git a/server/src/nest/assistant/assistant-tools.service.ts b/server/src/nest/assistant/assistant-tools.service.ts index 73e0a1745..59f6363f2 100644 --- a/server/src/nest/assistant/assistant-tools.service.ts +++ b/server/src/nest/assistant/assistant-tools.service.ts @@ -59,6 +59,7 @@ import { TRANSIT_CREATE_TOOL, TRANSIT_SEARCH_TOOL, } from './cards/roadtrip/transit-restore'; +import { argsFitSchema } from './tool-args-fit'; import { MikroORM } from '@mikro-orm/core'; import { Client } from '@modelcontextprotocol/sdk/client/index'; import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory'; @@ -350,9 +351,11 @@ export class AssistantToolSession { const group = this.tools.get(name)?.hint.group; if (group && this.unoffered.has(group)) { // A change is never run in the turn but held as a card, and the turn - // ends there: its arguments would only fail once the user applies it, - // with the model long gone. It is sent back for the next step instead. - if (this.tools.get(name)?.kind === 'write') { + // ends there: arguments written without the schema would only fail once + // the user applies it, with the model long gone. Such a call is sent back + // for the next step; one whose arguments already fit is held as always. + const tool = this.tools.get(name); + if (tool?.kind === 'write' && !argsFitSchema(args, modelSchema(name, tool.inputSchema, this.pinArg))) { return { error: `${name} belongs to the "${group}" tools, which were not loaded in this turn, so it did not run. They are loaded now: send the call again with the arguments exactly as its schema describes them.`, }; diff --git a/server/src/nest/assistant/tool-args-fit.ts b/server/src/nest/assistant/tool-args-fit.ts new file mode 100644 index 000000000..90a9e2952 --- /dev/null +++ b/server/src/nest/assistant/tool-args-fit.ts @@ -0,0 +1,38 @@ +/** + * Whether a tool call's arguments are of the shape its JSON schema asks for, as far as + * a blind call can get it wrong: a required argument missing, or a value of the wrong + * kind, such as a number or a list written as text (#2967). Not a full validator: a + * property without a plain `type` (a union, a reference) is taken as it comes, and + * the tool still validates everything when it runs. + */ +export function argsFitSchema(args: Record, schema: Record): boolean { + const properties = (schema.properties as Record | undefined) ?? {}; + const required = Array.isArray(schema.required) ? (schema.required as string[]) : []; + if (required.some((key) => args[key] === undefined)) return false; + return Object.entries(args).every(([key, value]) => { + const declared = properties[key]?.type; + if (value === undefined || declared === undefined) return true; + const types = Array.isArray(declared) ? declared : [declared]; + return types.some((type) => isOfType(value, type)); + }); +} + +function isOfType(value: unknown, type: unknown): boolean { + switch (type) { + case 'null': + return value === null; + case 'integer': + return Number.isInteger(value); + case 'number': + return typeof value === 'number' && Number.isFinite(value); + case 'array': + return Array.isArray(value); + case 'object': + return typeof value === 'object' && value !== null && !Array.isArray(value); + case 'string': + case 'boolean': + return typeof value === type; + default: + return true; + } +} diff --git a/server/tests/unit/nest/assistant/assistant-tools.service.test.ts b/server/tests/unit/nest/assistant/assistant-tools.service.test.ts index 0e20794aa..3dec1022e 100644 --- a/server/tests/unit/nest/assistant/assistant-tools.service.test.ts +++ b/server/tests/unit/nest/assistant/assistant-tools.service.test.ts @@ -446,6 +446,22 @@ describe('an assistant tool session', () => { } }); + it('ASTTOOL-028: a change called before its group was loaded is held as always when its arguments already fit', async () => { + const { userId, tripId } = await ownerWithTrips(); + const session = await tools.open(userId, tripId); + try { + session.specs(); + const fits = { placeId: 530, expected_revision: 3, stop: { name: 'Hut', lat: 47.1, lng: 11.2 } }; + expect(session.pinArgs('insert_tour_stop', fits)).toEqual({ args: fits }); + // Written as text, the same call is still sent back with the schema loaded. + expect(session.pinArgs('create_city_walk', { stations: '[1, 2]' })).toEqual({ + error: expect.stringContaining('did not run'), + }); + } finally { + await session.close(); + } + }); + it('ASTTOOL-026: only the blind call itself is told about the schema; a later failure of the same tool is not', async () => { const { userId, tripId } = await ownerWithTrips(); const session = await tools.open(userId, tripId);