fix(assistant): leave room for the attached file tools when loading tool groups

A trip turn with an attached file adds read_attached_file and keep_attached_file to the session's tools, so loading groups now stops two short of the 128 tools OpenAI and Azure accept in one request.
This commit is contained in:
Maurice
2026-11-28 00:32:43 +01:00
parent 6bbe597719
commit 7c6189f504
3 changed files with 33 additions and 3 deletions
@@ -25,6 +25,9 @@ export const MAX_CARDS_PER_STEP = 12;
*/
export const MAX_OFFERED_TOOLS = 128;
/** Tools a trip turn may offer beside its session's own (read_attached_file, keep_attached_file), which loading groups leaves room for. */
export const TURN_ADDED_TOOLS = 2;
/** What a tool answer may cost the conversation, as JSON text, after it was cut down to its structure. */
export const MAX_TOOL_OUTPUT_CHARS = 40_000;
@@ -44,6 +44,7 @@ import {
PINNED_ARG,
PREVIEW_BOOK_PAGE_TOOL,
REMEMBER_TOOL,
TURN_ADDED_TOOLS,
} from './assistant-tools.constants';
import type { ToolCards } from './cards/card-types';
import {
@@ -476,8 +477,9 @@ ${hint}`,
/**
* Loads groups as the ones used last, then unloads the ones used longest
* ago until the step offers no more than MAX_OFFERED_TOOLS. Groups asked for
* together that do not fit even alone are left out from the last one asked.
* ago until the step, with the tools its turn adds, offers no more than
* MAX_OFFERED_TOOLS. Groups asked for together that do not fit even alone
* are left out from the last one asked.
*/
private use(groups: readonly string[]): { unloaded: string[]; refused: string[] } {
for (const group of groups) {
@@ -490,7 +492,7 @@ ${hint}`,
this.loaded.delete(group);
this.unoffered.delete(group);
};
while (this.offeredSpecs().length > MAX_OFFERED_TOOLS) {
while (this.offeredSpecs().length > MAX_OFFERED_TOOLS - TURN_ADDED_TOOLS) {
const oldest = [...this.loaded].find((group) => !groups.includes(group));
if (oldest) {
drop(oldest);
@@ -5,6 +5,8 @@
* offered 130 in its first step).
*/
import { ADDON_IDS } from '../../../../src/addons';
import type { AssistantAttachmentSummary } from '../../../../src/db/repositories/AssistantAttachments.repository';
import { attachedPort } from '../../../../src/nest/assistant/assistant-attachment-tools';
import { ASSISTANT_TOOL_GROUPS } from '../../../../src/nest/assistant/assistant-tool-offer';
import { MAX_OFFERED_TOOLS } from '../../../../src/nest/assistant/assistant-tools.constants';
import {
@@ -56,6 +58,14 @@ async function within<T>(
const names = (session: AssistantToolSession) => session.specs().map((spec) => spec.name);
/** A turn with a readable attached file in a trip that keeps files, so both attachment tools come on top. */
const ATTACHED_TURN = {
attached: [{ id: 'att-1', text_status: 'ok' } as AssistantAttachmentSummary],
sentTo: 'model',
keeps: true,
read: () => Promise.resolve({ ok: true, value: '' }),
};
interface LoadAnswer {
loaded: string[];
tools: string[];
@@ -156,6 +166,21 @@ describe('the tools one step offers', () => {
}
});
it('ASTCAP-005b: a trip turn with an attached file stays within the limit whatever the model loads', async () => {
const steps = await within(tripSession(), async (session) => {
const port = attachedPort(session, ATTACHED_TURN);
const counts: Array<{ group: string; offered: number }> = [];
for (const group of GROUPS) {
await session.call('use_addon_tools', { groups: [group] });
counts.push({ group, offered: port.specs().length });
}
await session.call('use_addon_tools', { groups: GROUPS });
counts.push({ group: 'all', offered: port.specs().length });
return counts;
});
for (const { group, offered } of steps) expect(offered, group).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);
});
it('ASTCAP-006: a journey chat offers no more than the limit on either of its pages', async () => {
const { user } = await makeUser(orm);
const journey = await makeJourney(orm, user.id, { title: 'Lisbon' });