Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(assistant): an answer that hands the talk on to a trip or journey chat ends the turn

Maurice28079197e710
diff --git a/server/src/nest/assistant/assistant-agent.ts b/server/src/nest/assistant/assistant-agent.ts
index 83e7f53c4..2d2f4e9fd 100644
--- a/server/src/nest/assistant/assistant-agent.ts
+++ b/server/src/nest/assistant/assistant-agent.ts
@@ -13,6 +13,8 @@ import { activityOf, foundPlacesOf, googleIdsOf, weatherDayOf, weatherOf } from
import type { TripCard } from './assistant-context';
import { FOLLOW_UPS_ANSWER, followUpsOf } from './assistant-follow-ups';
import { outputText } from './assistant-history';
+import { OPEN_TRIP_CHAT_TOOL } from './assistant-home';
+import { OPEN_JOURNEY_CHAT_TOOL } from './assistant-home-journeys';
import { toolOutputForProvider } from './assistant-output';
import { MAX_CARDS_PER_STEP, MAX_READS_PER_STEP, MAX_STEPS, MAX_WRITES_PER_STEP } from './assistant-tools.constants';
import type { AssistantToolKind, ToolRun } from './assistant-tools.service';
@@ -557,6 +559,12 @@ async function askForTheAnswer(turn: Turn, rowId: number, step: StepRecord & { a
return null;
}
+/**
+ * The calls that only hand the conversation on, to a trip's or a journey's own
+ * chat: a card for the user, nothing the model reads further.
+ */
+const HANDOFF_TOOLS: ReadonlySet<string> = new Set([OPEN_TRIP_CHAT_TOOL, OPEN_JOURNEY_CHAT_TOOL]);
+
/** One step of the turn: the outcome when the turn ends with it, null when the model is asked again. */
async function runStep(turn: Turn, step: number): Promise<AgentOutcome | null> {
const { deps, input, w, signal, history } = turn;
@@ -638,6 +646,19 @@ async function runStep(turn: Turn, step: number): Promise<AgentOutcome | null> {
return outcomeOf(turn, 'aborted');
}
if (held.length > 0) return holdChanges(turn, rowId, { parts, providerState }, held);
+ // A step that wrote its answer and only handed the conversation on is the end of
+ // the turn. Asked once more after the card, the model wrote the same answer again.
+ if (text.trim() && received.every((call) => HANDOFF_TOOLS.has(call.name))) {
+ const followUps = asks.length > 0 ? followUpsOf(asks[asks.length - 1].args) : turn.followUps;
+ if (followUps) {
+ parts.push({ type: 'data-trek-follow-ups', data: followUps });
+ w.followUps(followUps);
+ }
+ await deps.store.saveStep(rowId, { parts, providerState }, 'complete');
+ w.finishStep();
+ finishTurn(turn, 'stop');
+ return outcomeOf(turn, 'stop');
+ }
const { images, modelTexts } = collected;
await carryOn(turn, rowId, { parts, providerState }, { text, calls, images, modelTexts });
return null;
diff --git a/server/tests/unit/nest/assistant/assistant-agent.test.ts b/server/tests/unit/nest/assistant/assistant-agent.test.ts
index 0d89d910a..ae0d75cd2 100644
--- a/server/tests/unit/nest/assistant/assistant-agent.test.ts
+++ b/server/tests/unit/nest/assistant/assistant-agent.test.ts
@@ -65,6 +65,7 @@ const KINDS: Record<string, AssistantToolKind> = {
get_weather: 'read',
get_place_details: 'read',
create_place: 'write',
+ open_trip_chat: 'read',
};
/** What the fake tools answer, per tool; anything else fails. */
@@ -75,6 +76,7 @@ const ANSWERS: Record<string, unknown> = {
},
get_weather: { weather: { temp: 20, main: 'Clouds', description: 'cloudy', type: 'climate' } },
get_place_details: { details: { name: 'Ginkaku-ji' } },
+ open_trip_chat: { offered: true },
};
function fakeTools() {
@@ -1022,4 +1024,54 @@ describe('runAgentLoop', () => {
expect(JSON.stringify(t.chunks)).not.toContain('HM4X2');
expect(JSON.stringify(t.memory.saved)).not.toContain('HM4X2');
});
+
+ it('ASTAGT-048: an answer that hands the talk on to a trip chat ends the turn; the model is not asked to answer twice', async () => {
+ const t = setup([
+ [
+ { type: 'text-delta', delta: 'Your next trip is Florida.' },
+ {
+ type: 'tool-call',
+ call: { id: 'toolu_h', name: 'open_trip_chat', args: { tripId: 1, question: 'Book the Uber' } },
+ },
+ { type: 'finish', reason: 'tool-calls' },
+ ],
+ [{ type: 'text-delta', delta: 'Your next trip is Florida.' }],
+ ]);
+ const outcome = await runAgentLoop(t.deps, t.input, t.writer, new AbortController().signal);
+ expect(outcome.end).toBe('stop');
+ expect(t.requests).toHaveLength(1);
+ expect(t.tools.call).toHaveBeenCalledTimes(1);
+ expect(t.memory.saved).toHaveLength(1);
+ expect(t.memory.saved[0].step.parts.map((part) => part.type)).toEqual(['text', 'tool-call']);
+ });
+
+ it('ASTAGT-049: a hand-on without any answer, or beside a read, still asks the model again', async () => {
+ const alone = setup([
+ [
+ {
+ type: 'tool-call',
+ call: { id: 'toolu_h', name: 'open_trip_chat', args: { tripId: 1, question: 'Book it' } },
+ },
+ { type: 'finish', reason: 'tool-calls' },
+ ],
+ [{ type: 'text-delta', delta: 'Go on in the trip chat.' }],
+ ]);
+ await runAgentLoop(alone.deps, alone.input, alone.writer, new AbortController().signal);
+ expect(alone.requests).toHaveLength(2);
+
+ const withRead = setup([
+ [
+ { type: 'text-delta', delta: 'Let me look.' },
+ { type: 'tool-call', call: { id: 'toolu_r', name: 'list_places', args: { tripId: 1 } } },
+ {
+ type: 'tool-call',
+ call: { id: 'toolu_h', name: 'open_trip_chat', args: { tripId: 1, question: 'Book it' } },
+ },
+ { type: 'finish', reason: 'tool-calls' },
+ ],
+ [{ type: 'text-delta', delta: 'Kinkaku-ji is on the list.' }],
+ ]);
+ await runAgentLoop(withRead.deps, withRead.input, withRead.writer, new AbortController().signal);
+ expect(withRead.requests).toHaveLength(2);
+ });
});