Skip to content

Commit 14caba2

Browse files
committed
feat: add current page context support to agent tools and API requests
1 parent c73e205 commit 14caba2

4 files changed

Lines changed: 54 additions & 21 deletions

File tree

agent/simpleAgent.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ import {
1919
} from "./middleware/sequenceDebug.js";
2020
import type { ApiBasedTool } from "../apiBasedTools.js";
2121
import type { ToolCallEventSink } from "./toolCallEvents.js";
22+
import type { CurrentPageContext } from "./tools/getUserLocation.js";
2223

2324
export const contextSchema = z.object({
2425
adminUser: z.custom<AdminUser>(),
2526
userTimeZone: z.string(),
2627
sessionId: z.string(),
2728
turnId: z.string(),
29+
currentPage: z.custom<CurrentPageContext>().optional(),
2830
httpExtra: z.custom<Partial<HttpExtra>>().optional(),
2931
emitToolCallEvent: z.custom<ToolCallEventSink>(),
3032
});
@@ -231,6 +233,7 @@ export async function callAgent(params: {
231233
customComponentsDir: string;
232234
sessionId: string;
233235
turnId: string;
236+
currentPage?: CurrentPageContext;
234237
httpExtra?: Partial<HttpExtra>;
235238
userTimeZone: string;
236239
emitToolCallEvent: ToolCallEventSink;
@@ -249,6 +252,7 @@ export async function callAgent(params: {
249252
customComponentsDir,
250253
sessionId,
251254
turnId,
255+
currentPage,
252256
httpExtra,
253257
userTimeZone,
254258
emitToolCallEvent,
@@ -293,6 +297,7 @@ export async function callAgent(params: {
293297
userTimeZone,
294298
sessionId,
295299
turnId,
300+
currentPage,
296301
httpExtra,
297302
emitToolCallEvent,
298303
},

agent/tools/getUserLocation.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { tool } from "langchain";
2+
import { z } from "zod";
3+
4+
export type CurrentPageContext = {
5+
path: string;
6+
fullPath: string;
7+
title: string;
8+
url: string;
9+
};
10+
11+
const getUserLocationSchema = z.object({});
12+
13+
export function createGetUserLocationTool() {
14+
return tool(
15+
async (_input, runtime) => {
16+
const currentPage = (runtime.context as { currentPage?: CurrentPageContext }).currentPage;
17+
18+
if (!currentPage) {
19+
return JSON.stringify(
20+
{
21+
status: 404,
22+
message: "Current user location is not available.",
23+
},
24+
null,
25+
2,
26+
);
27+
}
28+
29+
return JSON.stringify(
30+
{
31+
status: 200,
32+
location: currentPage,
33+
},
34+
null,
35+
2,
36+
);
37+
},
38+
{
39+
name: "get_user_location",
40+
description:
41+
"Get the user's current location in the AdminForth UI, including current page path, full path, title, and URL. Call this tool when you do not understand what the user is referring to, especially when they say here, this page, current page, opened page, or otherwise rely on page context.",
42+
schema: getUserLocationSchema,
43+
},
44+
);
45+
}

agent/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createFetchSkillTool } from "./fetchSkill.js";
33
import { createFetchToolSchemaTool } from "./fetchToolSchema.js";
44
import type { ApiBasedTool } from "../../apiBasedTools.js";
55
import { createApiTool } from "./apiTool.js";
6+
import { createGetUserLocationTool } from "./getUserLocation.js";
67

78
export const ALWAYS_AVAILABLE_API_TOOL_NAMES = ["get_resource"] as const;
89

@@ -20,6 +21,7 @@ export async function createAgentTools(
2021

2122
return createApiTool(toolName, apiBasedTool);
2223
}),
24+
createGetUserLocationTool(),
2325
await createFetchSkillTool(customComponentsDir),
2426
await createFetchToolSchemaTool(apiBasedTools),
2527
];

index.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,12 @@ import {
3535
} from "./agent/systemPrompt.js";
3636
import { ALWAYS_AVAILABLE_API_TOOL_NAMES } from "./agent/tools/index.js";
3737
import type { ToolCallEvent } from "./agent/toolCallEvents.js";
38+
import type { CurrentPageContext } from "./agent/tools/getUserLocation.js";
3839

3940
type CurrentPageRequestBody = {
4041
currentPage?: CurrentPageContext;
4142
};
4243

43-
type CurrentPageContext = {
44-
path: string;
45-
fullPath: string;
46-
title: string;
47-
url: string;
48-
};
49-
5044
type SpeechResponseRequestBody = CurrentPageRequestBody & {
5145
audioBase64: string;
5246
filename: string;
@@ -133,18 +127,6 @@ function formatAdminUserPrompt(adminUser: AdminUser, usernameField: string) {
133127
].join("\n");
134128
}
135129

136-
function formatCurrentPagePrompt(currentPage: CurrentPageContext | undefined) {
137-
if (!currentPage) {
138-
return null;
139-
}
140-
141-
return [
142-
"Current user page context for the latest message:",
143-
JSON.stringify(currentPage, null, 2),
144-
"When the user says here, this page, current page, or opened page, treat it as this page.",
145-
].join("\n");
146-
}
147-
148130
function assertRequiredApiTool(
149131
apiBasedTools: Record<string, ApiBasedTool>,
150132
toolName: string,
@@ -339,7 +321,6 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
339321
}
340322
assertRequiredApiTool(apiBasedTools, "update_record");
341323
this.apiBasedTools = apiBasedTools;
342-
const currentPagePrompt = formatCurrentPagePrompt(input.currentPage);
343324
const stream = await callAgent({
344325
name: `adminforth-agent-${this.pluginInstanceId}`,
345326
model,
@@ -348,7 +329,6 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
348329
checkpointer: this.getCheckpointer(),
349330
messages: [
350331
new SystemMessage(systemPrompt),
351-
...(currentPagePrompt ? [new SystemMessage(currentPagePrompt)] : []),
352332
new HumanMessage(input.prompt),
353333
],
354334
adminUser: input.adminUser,
@@ -357,6 +337,7 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
357337
customComponentsDir: this.adminforth.config.customization.customComponentsDir,
358338
sessionId: input.sessionId,
359339
turnId: input.turnId,
340+
currentPage: input.currentPage,
360341
httpExtra: input.httpExtra,
361342
userTimeZone: input.userTimeZone,
362343
emitToolCallEvent: (event) => {

0 commit comments

Comments
 (0)