Cortex · Tools and gates

Gates: rewriting arguments

A gate (toolGates) is a function called before every tool call; it can return { allow: true, args } to replace the call’s arguments. Here the model passes another member’s name, and the gate replaces it with the member from the run’s deps. The model’s turns are scripted; no model is called.

Gate and agent
const own: ToolGate<{ member: string }> = (call, run) =>
  call.toolId === 'bookingsOf'
    ? { allow: true, args: { member: run.deps.member } }
    : { allow: true };

const desk = defineAgent<undefined, { member: string }>({
  id: 'desk',
  instructions: 'Help the member with their bookings.',
  tools: [bookingsOf],
  toolGates: [own],
});

const result = await desk.run('What has Linus booked?', { llm, deps: { member: 'grace' } }).result;
Scripted model turns
1  calls bookingsOf {"member":"linus"}
2  writes {"response":"You have two bookings, B-2041 and B-2044. I can only show you your own."}
Output
{
  observation: {
    kind: 'result',
    toolId: 'bookingsOf',
    args: { member: 'grace' },
    result: ['B-2041', 'B-2044'],
  },
  toolResult: '["B-2041","B-2044"]',
}
Output matches the expected value

Runs in this page on the installed package. The input and the expected output are defined by the example.