Skip to content

Commit 78f614c

Browse files
os-zhuangclaude
andcommitted
feat(service-ai): forward latest user message as ToolExecutionContext.userMessageText
Adds an optional, neutral `userMessageText` to ToolExecutionContext (alongside currentObjectName/currentViewName) and populates it from the most recent user message in the agent chat route (reusing extractMessageText). Lets a tool detect explicit intent — e.g. a confirm/approval — without re-deriving it from the transcript. Cloud's confirm-before-change gate consumes this; the framework stays neutral (no phrase semantics here). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 3afaeed commit 78f614c

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

packages/services/service-ai/src/routes/agent-routes.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ function extractMessageText(content: unknown): string {
2727
return '';
2828
}
2929

30+
/** The text of the most recent user message — forwarded to tools as
31+
* ToolExecutionContext.userMessageText so a tool can detect intent (e.g. an
32+
* explicit confirm/approval) without re-deriving it. Neutral context, like
33+
* currentObjectName; consumers own any semantics. */
34+
function lastUserMessageText(messages: readonly unknown[]): string | undefined {
35+
for (let i = messages.length - 1; i >= 0; i--) {
36+
const m = messages[i] as { role?: string; content?: unknown } | undefined;
37+
if (m?.role !== 'user') continue;
38+
const text = extractMessageText(m.content).trim();
39+
if (text) return text;
40+
}
41+
return undefined;
42+
}
43+
3044
function detectUserLanguage(messages: readonly unknown[]): string | undefined {
3145
let text = '';
3246
for (let i = messages.length - 1; i >= 0; i--) {
@@ -310,6 +324,9 @@ export function buildAgentRoutes(
310324
typeof chatContext?.objectName === 'string' ? chatContext.objectName : undefined,
311325
currentViewName:
312326
typeof chatContext?.viewName === 'string' ? chatContext.viewName : undefined,
327+
// Latest user message text → tools can detect intent (e.g. an
328+
// explicit confirm / approval) without re-deriving it.
329+
userMessageText: lastUserMessageText(rawMessages),
313330
}
314331
: undefined,
315332
};

packages/spec/src/contracts/ai-service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ export interface ToolExecutionContext {
427427
currentObjectName?: string;
428428
/** View the user is currently viewing, if known. */
429429
currentViewName?: string;
430+
/**
431+
* Text of the latest user message (neutral context, like currentObjectName).
432+
* Forwarded so a tool can detect intent — e.g. an explicit confirm/approval —
433+
* without re-deriving it from the transcript. Consumers own any semantics.
434+
*/
435+
userMessageText?: string;
430436
/** Distributed-trace id for cross-service correlation. */
431437
traceId?: string;
432438
/**

0 commit comments

Comments
 (0)