mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
fix(assistant): send a blind change back only when its arguments do not fit its schema
Sending back every change called before its group was loaded cost a step even when the model had the arguments right, and a valid change was no longer held as the card the user decides on. A blind change is now held as always when its arguments are of the shape the schema asks for, and sent back with the schema loaded only when one is missing or of the wrong kind, such as a number or a list written as text (#2967).
This commit is contained in:
@@ -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.`,
|
||||
};
|
||||
|
||||
@@ -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<string, unknown>, schema: Record<string, unknown>): boolean {
|
||||
const properties = (schema.properties as Record<string, { type?: unknown }> | 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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user