Repository navigation
Expand file tree
/
Copy pathapprovalFormatter.ts
More file actions
143 lines (110 loc) · 4.9 KB
/
Copy pathapprovalFormatter.ts
File metadata and controls
143 lines (110 loc) · 4.9 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
132
133
134
135
136
137
138
139
140
141
142
143
import type { AdminForthResource, IAdminForth } from "adminforth";
/** One tool call awaiting approval, normalized out of the provider interrupt payload. */
export type ApprovalRequest = {
toolName: string;
args: Record<string, unknown>;
};
/** Argument keys that address the call itself rather than the data being changed. */
const NON_FIELD_KEYS = new Set(["resourceId", "recordId", "primaryKey", "requiredColumnsToSkip", "meta", "extra"]);
const MAX_VALUE_LENGTH = 80;
const MAX_FIELDS = 12;
function truncate(text: string, limit = MAX_VALUE_LENGTH): string {
return text.length > limit ? `${text.slice(0, limit - 1)}…` : text;
}
function findResource(adminforth: IAdminForth, resourceId?: unknown): AdminForthResource | undefined {
return typeof resourceId === "string"
? (adminforth.config?.resources ?? []).find((resource) => resource.resourceId === resourceId)
: undefined;
}
function resourceLabel(resource: AdminForthResource | undefined, args: Record<string, unknown>): string {
return resource?.label ?? (typeof args.resourceId === "string" ? args.resourceId : "resource");
}
/** `first_name` / `firstName` -> `First name`, for keys the resource does not declare. */
function humanize(name: string): string {
const spaced = name.replace(/[_-]+/g, " ").replace(/([a-z\d])([A-Z])/g, "$1 $2").trim();
return spaced ? `${spaced[0].toUpperCase()}${spaced.slice(1).toLowerCase()}` : name;
}
function formatValue(value: unknown, column?: any): string {
if (value === undefined || value === null || value === "") {
return "empty";
}
if (column?.masked || column?.backendOnly) {
return "••••••";
}
if (typeof value === "boolean") {
return value ? "Yes" : "No";
}
const enumLabel = Array.isArray(column?.enum)
? column.enum.find((item: any) => item.value === value)?.label
: undefined;
if (enumLabel) {
return truncate(String(enumLabel));
}
if (Array.isArray(value)) {
return truncate(value.map((item) => formatValue(item)).join(", "));
}
if (typeof value === "object") {
return truncate(JSON.stringify(value));
}
return truncate(String(value));
}
/** `Brand: Tesla, Price: 45000` — the record payload in the resource's own wording. */
function formatRecord(record: unknown, resource: AdminForthResource | undefined): string {
if (record === null || typeof record !== "object" || Array.isArray(record)) {
return "";
}
const columnsByName = new Map((resource?.columns ?? []).map((column) => [column.name, column]));
const entries = Object.entries(record as Record<string, unknown>)
.filter(([key]) => !NON_FIELD_KEYS.has(key));
const shown = entries.slice(0, MAX_FIELDS).map(([name, value]) => {
const column = columnsByName.get(name);
return `${column?.label || humanize(name)}: ${formatValue(value, column)}`;
});
const hidden = entries.length - shown.length;
const lines = hidden > 0 ? [...shown, `and ${hidden} more`] : shown;
// One field per line: a wide record is unreadable as a single run-on sentence.
return lines.join(",\n");
}
function actionLabel(resource: AdminForthResource | undefined, actionId: unknown): string {
if (typeof actionId !== "string" || !actionId) {
return "action";
}
const action = (resource?.options?.actions ?? []).find((candidate: any) => candidate.id === actionId);
return `"${action?.name || humanize(actionId)}"`;
}
/**
* Describe a tool call awaiting approval in a sentence an admin (not a developer) can
* act on — resource and column labels instead of ids, values instead of raw JSON.
* Falls back to a key/value dump for tool calls it does not recognize.
*/
export function formatApprovalRequest(
adminforth: IAdminForth,
request: ApprovalRequest,
): string {
const { args, toolName } = request;
const resource = findResource(adminforth, args.resourceId);
const label = resourceLabel(resource, args);
switch (toolName) {
case "create_record": {
const fields = formatRecord(args.record, resource);
return `Create a new record in ${label}${fields ? `:\n${fields}` : ""}`;
}
case "update_record": {
const fields = formatRecord(args.record, resource);
return `Update record with id:${formatValue(args.recordId)} in ${label}${fields ? `, set:\n${fields}` : ""}`;
}
case "delete_record":
return `Delete record with id:${formatValue(args.primaryKey)} from ${label} — this cannot be undone`;
case "start_custom_action":
return `Run ${actionLabel(resource, args.actionId)} on record ${formatValue(args.recordId)} in ${label}`;
case "start_custom_bulk_action":
case "start_bulk_action": {
const count = Array.isArray(args.recordIds) ? args.recordIds.length : 0;
return `Run ${actionLabel(resource, args.actionId)} on ${count} record(s) in ${label}`;
}
default: {
const fields = formatRecord(args, resource);
return `${humanize(toolName)}${fields ? `:\n${fields}` : ""}`;
}
}
}