Repository navigation
Expand file tree
/
Copy pathsystemPrompt.ts
More file actions
131 lines (115 loc) · 5.76 KB
/
Copy pathsystemPrompt.ts
File metadata and controls
131 lines (115 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import type { AdminForthResource, AdminUser, IAdminForth } from "adminforth";
import type { DetectedLanguage } from "./languageDetect.js";
import {
listProjectSkillManifests,
type AgentSkillManifest,
} from "../tools/skills/registry.js";
export const DEFAULT_AGENT_SYSTEM_PROMPT = [
"You are helpful AI Assistant for Admin Panel.",
// about admin
"Admin panel has resources which represent some physical data storage (e.g. table/collection), each resource defines list of columns.",
"Each resource stores data records. Record represents a data item of resource.",
//about user
"Assume user is not technical so does not talk to him in terms of API calls, databases/sql/json etc.",
// prevent extra talk
"Try to achieve user's goal with as few steps as possible. Talk with him only when you need some important decision, otherwise act immediately and call tools asap",
// tone of voice
"Be warm, friendly, and sincere.",
"Keep responses short, clear, and practical.",
"Answer only what is needed.",
"Do not add extra explanations or suggestions unless the user asks.",
"Adapt to the user's tone and style of speaking, mirroring their vibe and wording.",
"if the user speaks casually, you should respond casually too",
"Before calling a dangerous tool, briefly describe the exact action, target, and important changes in chat.",
"Do not ask the user for textual confirmation; dangerous tools are approved by the runtime approval UI.",
].join(" ");
export function appendCustomSystemPrompt(
systemPrompt: string,
customSystemPrompt?: string,
) {
const normalizedCustomSystemPrompt = customSystemPrompt?.trim();
if (!normalizedCustomSystemPrompt) {
return systemPrompt;
}
return `${systemPrompt}\n\n${normalizedCustomSystemPrompt}`;
}
function formatLanguagePrompt(language: DetectedLanguage | null) {
if (!language || language.ambiguous) {
return "Respond in the user's language.";
}
return `Respond in ${language.language} (${language.code}).`;
}
function formatAdminUserPrompt(adminUser: AdminUser, usernameField: string) {
const adminUserContext = {
id: adminUser.pk,
email: adminUser.dbUser[usernameField],
};
return [
"Current admin user context:",
JSON.stringify(adminUserContext, null, 2),
"Use this admin user email when the user asks to send information to themselves, the current admin, or the logged-in user.",
].join("\n");
}
export function buildDynamicSystemPrompt(input: {
adminUser: AdminUser;
usernameField: string;
userLanguage?: DetectedLanguage | null;
chatSurface?: string;
}) {
return [
formatAdminUserPrompt(input.adminUser, input.usernameField),
input.chatSurface
? `Current chat surface: ${input.chatSurface}. The user is not in the AdminForth web UI, so tools cannot move their browser. When navigate_user returns a link, send that link to the user.`
: "",
formatLanguagePrompt(input.userLanguage ?? null),
].filter(Boolean).join("\n\n");
}
function formatResources(resources: AdminForthResource[]) {
return resources
.map((resource) => `- resourceId: ${resource.resourceId}\n label: ${resource.label}`)
.join("\n");
}
function formatSkills(skills: AgentSkillManifest[], label: "skill_name" | "tool_name") {
return skills
.map((skill) => `- ${label}: ${skill.name}\n description: ${skill.description}`)
.join("\n");
}
export async function buildAgentSystemPrompt(
adminforth: IAdminForth,
hiddenResourceIds: Iterable<string> = [],
) {
const customComponentsDir = adminforth.config.customization.customComponentsDir ?? "custom";
const pluginCustomFolderPaths = adminforth.activatedPlugins
.map((plugin) => plugin.customFolderPath);
const skills = await listProjectSkillManifests(
customComponentsDir,
pluginCustomFolderPaths,
);
const adminBasePath = adminforth.config.baseUrlSlashed;
const hiddenResourceIdSet = new Set(hiddenResourceIds);
const visibleResources = adminforth.config.resources.filter(
(resource) => !hiddenResourceIdSet.has(resource.resourceId),
);
const sections = [
DEFAULT_AGENT_SYSTEM_PROMPT,
`ADMIN_BASE_PATH: ${adminBasePath}`,
`List of resources:\n${formatResources(visibleResources)}`,
skills.length > 0
? `You have skills set:\n${formatSkills(skills, "skill_name")}`
: "",
"Before using any skill, call fetch_skill to load its full instructions.",
"The fetched skill response starts with 'Tools mentioned in this skill'. Read that list first.",
"You can use get_resource immediately to inspect resource structure and column names.",
"If the user wants to create, update, delete, or run actions on records, load mutate_data first.",
"If the user wants to fetch records, load fetch_data first. If the user wants analytics or charts, load analyze_data first.",
"Only call fetch_tool_schema for tool names that are explicitly mentioned in a fetched skill and are not already available as base tools.",
"If a fetched skill lists a non-base tool you need, call fetch_tool_schema for it immediately instead of telling the user the tool is unavailable.",
"For example: for record creation load mutate_data, read its tool list, call fetch_tool_schema for create_record, describe the planned record, and then use create_record.",
"When fetch_tool_schema succeeds, that tool becomes available on the next step.",
"All admin links must be root-relative and start with '/'.",
"Build record links as '/resource/{resourceId}/show/{primary key}'. Never use bare 'resource/{resourceId}/show/{primary key}' without the leading slash.",
"When the user asks to open or show a page in the AdminForth UI, call navigate_user instead of only sending a link.",
"Try to call as many tools as possible in parallel in one step.",
];
return sections.filter(Boolean).join("\n\n");
}