@mgamadeus/ddd-agents 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ui.js CHANGED
@@ -25,7 +25,8 @@ const CLIENT_TOOL_OUTCOMES = {
25
25
  notDeclared: "NOT_DECLARED",
26
26
  writeAction: "WRITE_ACTION",
27
27
  disabled: "DISABLED",
28
- noEffect: "NO_EFFECT"
28
+ noEffect: "NO_EFFECT",
29
+ unknownFilterValue: "UNKNOWN_FILTER_VALUE"
29
30
  };
30
31
  const SCREEN_ACTION_KINDS = ["setFilters", "sort", "openRow", "switchTab", "press"];
31
32
  const SCREEN_SORT_DIRECTIONS = ["asc", "desc", "none"];
@@ -43,16 +44,65 @@ const formatScreenTargetLine = (target) => {
43
44
  const parts = [target.key, target.group ? `${target.group} › ${target.label}` : target.label];
44
45
  if (target.parameters.length > 0) parts[1] = `${parts[1]} (${target.parameters.join(", ")})`;
45
46
  if (target.tabs.length > 0) parts.push(`tabs ${target.tabs.join(", ")}`);
46
- if (target.filters.length > 0) parts.push(`filters ${target.filters.map((filter) => filter.key).join(", ")}`);
47
+ const filtersOfEveryPage = target.filters.filter((filter) => filter.tabs === void 0);
48
+ if (filtersOfEveryPage.length > 0) parts.push(`filters ${filtersOfEveryPage.map((filter) => filter.key).join(", ")}`);
49
+ const tabsWithFilters = [...new Set(target.filters.flatMap((filter) => filter.tabs ?? []))];
50
+ if (tabsWithFilters.length > 0) parts.push(`tabs with filters ${tabsWithFilters.join(", ")}`);
47
51
  if (target.views.length > 0) parts.push(`views ${target.views.join(", ")}`);
48
52
  return `- ${parts.join(" · ")}`;
49
53
  };
54
+ const MAX_DESCRIBED_FILTER_VALUES = 24;
55
+ const isDescribedScreenTargetFilter = (filter) => filter.values !== void 0 && filter.values.length > 0 || filter.hint !== void 0 && filter.hint !== "";
56
+ const describeScreenFilterValues = (values, multiple) => {
57
+ const sortKeys = [...new Set(values.map((value) => value.replace(/:(asc|desc)$/, "")))];
58
+ const isSort = values.length === sortKeys.length * 2 && sortKeys.every((key) => values.includes(`${key}:asc`) && values.includes(`${key}:desc`));
59
+ const named = isSort ? sortKeys : values;
60
+ const list = `${named.slice(0, MAX_DESCRIBED_FILTER_VALUES).join("|")}${named.length > MAX_DESCRIBED_FILTER_VALUES ? "|…" : ""}`;
61
+ if (isSort) return `one of ${list}, each with :asc or :desc`;
62
+ return multiple ? `one or more of ${list}, comma-separated` : `one of ${list}`;
63
+ };
64
+ const describeScreenTargetFilter = (filter) => {
65
+ const parts = [filter.key];
66
+ if (filter.values !== void 0 && filter.values.length > 0) {
67
+ parts.push(describeScreenFilterValues(filter.values, filter.multiple === true));
68
+ }
69
+ if (filter.hint !== void 0 && filter.hint !== "") parts.push(filter.values && filter.values.length > 0 ? `(${filter.hint})` : filter.hint);
70
+ return parts.join(" ");
71
+ };
72
+ const screenTargetFiltersOfTab = (target, tab) => {
73
+ const ofTab = tab === null ? [] : target.filters.filter((filter) => filter.tabs?.includes(tab));
74
+ const keysOfTab = new Set(ofTab.map((filter) => filter.key));
75
+ return [...target.filters.filter((filter) => filter.tabs === void 0 && !keysOfTab.has(filter.key)), ...ofTab];
76
+ };
77
+ const formatScreenTargetFilterValues = (target) => {
78
+ const lines = [];
79
+ const ofEveryPage = target.filters.filter((filter) => filter.tabs === void 0);
80
+ if (ofEveryPage.some(isDescribedScreenTargetFilter)) {
81
+ lines.push(` filter values: ${ofEveryPage.filter(isDescribedScreenTargetFilter).map(describeScreenTargetFilter).join(" · ")}`);
82
+ }
83
+ const tabs = [...new Set(target.filters.flatMap((filter) => filter.tabs ?? []))];
84
+ for (const tab of tabs) {
85
+ const ofTab = target.filters.filter((filter) => filter.tabs?.includes(tab));
86
+ lines.push(` filters of tab ${tab}: ${ofTab.map(describeScreenTargetFilter).join(" · ")}`);
87
+ }
88
+ return lines.length === 0 ? null : lines.join("\n");
89
+ };
90
+ const findUnknownScreenFilterValue = (filter, value) => {
91
+ if (filter.values === void 0 || filter.values.length === 0 || value === "") return null;
92
+ const parts = filter.multiple ? value.split(",").map((part) => part.trim()) : [value];
93
+ return parts.find((part) => !filter.values?.includes(part)) ?? null;
94
+ };
95
+ const describeUnknownScreenFilterValue = (filter, value) => `${filter.key}=${value.slice(0, 60)} (${describeScreenTargetFilter(filter)})`;
96
+ const MAX_TARGETS_WITH_FILTER_VALUES = 4;
50
97
  const MAX_TARGET_LIST_CHARS = 4500;
51
- const formatScreenTargetList = (targets, searchText) => {
52
- const searchWords = searchWordsOf(searchText);
53
- const lines = targets.map(formatScreenTargetLine).filter((line) => searchWords.every((word) => line.toLowerCase().includes(word)));
54
- if (lines.length === 0) return "No screen matches. Call listScreens without searchText for all of them.";
55
- const header = `Screens this user can open (${lines.length}). Pass the key to openScreen.`;
98
+ const formatScreenTargetShortLine = (target) => {
99
+ const parts = [target.key, target.parameters.length > 0 ? `${target.label} (${target.parameters.join(", ")})` : target.label];
100
+ if (target.tabs.length > 0) parts.push(target.tabs.length === 1 ? "1 tab" : `${target.tabs.length} tabs`);
101
+ if (target.filters.length > 0) parts.push("filters");
102
+ if (target.views.length > 0) parts.push("views");
103
+ return `- ${parts.join(" · ")}`;
104
+ };
105
+ const capTargetLines = (header, lines) => {
56
106
  const kept = [];
57
107
  let chars = header.length;
58
108
  for (const line of lines) {
@@ -65,6 +115,28 @@ const formatScreenTargetList = (targets, searchText) => {
65
115
  }
66
116
  return [header, ...kept].join("\n");
67
117
  };
118
+ const formatScreenTargetList = (targets, searchText) => {
119
+ const searchWords = searchWordsOf(searchText);
120
+ const searchedKey = searchWords.length === 1 ? searchWords[0] : null;
121
+ const isSearchedKey = (target) => target.key.toLowerCase() === searchedKey;
122
+ const matches = targets.map((target) => ({ target, line: formatScreenTargetLine(target) })).filter((match) => searchWords.every((word) => match.line.toLowerCase().includes(word))).sort((first, second) => Number(isSearchedKey(second.target)) - Number(isSearchedKey(first.target)));
123
+ if (matches.length === 0) return "No screen matches. Call listScreens without searchText for all of them.";
124
+ const isNarrowed = matches.length <= MAX_TARGETS_WITH_FILTER_VALUES;
125
+ const withFilterValues = (target, line) => {
126
+ const valuesLine = isNarrowed || isSearchedKey(target) ? formatScreenTargetFilterValues(target) : null;
127
+ return valuesLine === null ? line : `${line}
128
+ ${valuesLine}`;
129
+ };
130
+ const hasDescribedFilters = matches.some((match) => match.target.filters.some((filter) => isDescribedScreenTargetFilter(filter) || filter.tabs !== void 0));
131
+ const opening = `Screens this user can open (${matches.length}). Pass the key to openScreen.`;
132
+ const fullHeader = opening + (hasDescribedFilters && !isNarrowed ? " Filter values of a screen: listScreens with its key as searchText." : "");
133
+ const fullLines = matches.map((match) => withFilterValues(match.target, match.line));
134
+ const fullChars = fullLines.reduce((chars, line) => chars + line.length + 1, fullHeader.length);
135
+ if (isNarrowed || fullChars <= MAX_TARGET_LIST_CHARS) return capTargetLines(fullHeader, fullLines);
136
+ const shortHeader = `${opening} Short lines, the list is long. Tabs, filters and filter values of a screen: listScreens with its key as searchText.`;
137
+ const shortLines = matches.map((match) => isSearchedKey(match.target) ? withFilterValues(match.target, match.line) : formatScreenTargetShortLine(match.target));
138
+ return capTargetLines(shortHeader, shortLines);
139
+ };
68
140
  const TOKEN_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
69
141
  const isClientToolToken = (value) => typeof value === "string" && TOKEN_PATTERN.test(value);
70
142
  const buildTargetRoute = (target, recordIds, tab) => {
@@ -223,12 +295,20 @@ class ClientToolRequestRunner {
223
295
  const tab = isClientToolToken(parameters.tab) ? parameters.tab : null;
224
296
  if (tab !== null && !target.tabs.includes(tab)) return refusal$1(CLIENT_TOOL_OUTCOMES.unknownTab);
225
297
  const filters = {};
298
+ const filtersNotSet = [];
299
+ const filtersOfTab = screenTargetFiltersOfTab(target, tab);
226
300
  for (const filter of Array.isArray(parameters.filters) ? parameters.filters : []) {
227
301
  const [key, ...valueParts] = String(filter).split("=");
228
- if (!target.filters.some((targetFilter) => targetFilter.key === key) || valueParts.length === 0) {
302
+ const targetFilter = filtersOfTab.find((candidate) => candidate.key === key);
303
+ if (!targetFilter || valueParts.length === 0) {
229
304
  return refusal$1(CLIENT_TOOL_OUTCOMES.unknownFilter);
230
305
  }
231
- filters[key] = valueParts.join("=");
306
+ const value = valueParts.join("=");
307
+ if (findUnknownScreenFilterValue(targetFilter, value) !== null) {
308
+ filtersNotSet.push(describeUnknownScreenFilterValue(targetFilter, value));
309
+ continue;
310
+ }
311
+ filters[key] = value;
232
312
  }
233
313
  const recordIds = typeof parameters.recordId === "number" ? [parameters.recordId] : [];
234
314
  if (buildTargetRoute(target, recordIds, tab) === null) return refusal$1(CLIENT_TOOL_OUTCOMES.missingParameter);
@@ -242,6 +322,7 @@ class ClientToolRequestRunner {
242
322
  }
243
323
  const location = await this.adapters.navigator.open(target, openRequest);
244
324
  const details = { route: location.route, title: location.title };
325
+ if (filtersNotSet.length > 0) details.filtersNotSet = filtersNotSet.join("; ");
245
326
  if (parameters.includeScreen !== true) return { outcome: CLIENT_TOOL_OUTCOMES.ok, text: "", details };
246
327
  const snapshot = this.adapters.reader.read({ maxChars: MAX_SCREEN_CHARS });
247
328
  return { outcome: CLIENT_TOOL_OUTCOMES.ok, text: snapshot.markdown, details };
@@ -279,10 +360,15 @@ ${snapshot.markdown}` };
279
360
  const filters = {};
280
361
  for (const entry of entries) {
281
362
  const [key, ...valueParts] = String(entry).split("=");
282
- if (!target.filters.some((targetFilter) => targetFilter.key === key) || valueParts.length === 0) {
363
+ const targetFilter = target.filters.find((candidate) => candidate.key === key);
364
+ if (!targetFilter || valueParts.length === 0) {
283
365
  return { outcome: CLIENT_TOOL_OUTCOMES.unknownFilter, text: `Filters of this screen: ${target.filters.map((filter) => filter.key).join(", ")}.`, details: {} };
284
366
  }
285
- filters[key] = valueParts.join("=").slice(0, MAX_FILTER_VALUE_CHARS);
367
+ const value = valueParts.join("=").slice(0, MAX_FILTER_VALUE_CHARS);
368
+ if (findUnknownScreenFilterValue(targetFilter, value) !== null) {
369
+ return { outcome: CLIENT_TOOL_OUTCOMES.unknownFilterValue, text: `Not a value of this filter: ${describeUnknownScreenFilterValue(targetFilter, value)}.`, details: {} };
370
+ }
371
+ filters[key] = value;
286
372
  }
287
373
  const blocker = navigator.describeBlockers();
288
374
  if (blocker) return { outcome: blocker.outcome, text: blocker.reason, details: {} };
@@ -355,6 +441,44 @@ class AgentUIClient {
355
441
  return this.runner.whenIdle();
356
442
  }
357
443
  }
444
+ const createLatestScreenAdapters = (readAdapters) => {
445
+ const latestAdapters = {
446
+ targets: { listTargets: () => readAdapters().targets.listTargets() },
447
+ navigator: {
448
+ describeBlockers: () => readAdapters().navigator.describeBlockers(),
449
+ open: (target, request) => readAdapters().navigator.open(target, request),
450
+ currentLocation: () => readAdapters().navigator.currentLocation()
451
+ },
452
+ reader: { read: (options) => readAdapters().reader.read(options) },
453
+ highlighter: {
454
+ mark: (references, note) => readAdapters().highlighter.mark(references, note),
455
+ clear: () => readAdapters().highlighter.clear()
456
+ },
457
+ isVisible: () => readAdapters().isVisible()
458
+ };
459
+ Object.defineProperties(latestAdapters.navigator, {
460
+ currentTarget: {
461
+ enumerable: true,
462
+ get: () => readAdapters().navigator.currentTarget ? () => readAdapters().navigator.currentTarget?.() ?? null : void 0
463
+ },
464
+ applyFilters: {
465
+ enumerable: true,
466
+ get: () => {
467
+ const applyFilters = readAdapters().navigator.applyFilters;
468
+ if (!applyFilters) return void 0;
469
+ return (target, filters) => readAdapters().navigator.applyFilters?.(target, filters);
470
+ }
471
+ }
472
+ });
473
+ Object.defineProperties(latestAdapters, {
474
+ picture: { enumerable: true, get: () => readAdapters().picture },
475
+ agentWindow: { enumerable: true, get: () => readAdapters().agentWindow },
476
+ pathGuide: { enumerable: true, get: () => readAdapters().pathGuide },
477
+ actions: { enumerable: true, get: () => readAdapters().actions },
478
+ requestHandlers: { enumerable: true, get: () => readAdapters().requestHandlers }
479
+ });
480
+ return latestAdapters;
481
+ };
358
482
  const useAgentUIClient = (adapters, uiStateSource = null) => {
359
483
  const adaptersRef = useRef(adapters);
360
484
  adaptersRef.current = adapters;
@@ -362,26 +486,7 @@ const useAgentUIClient = (adapters, uiStateSource = null) => {
362
486
  uiStateSourceRef.current = uiStateSource;
363
487
  const clientRef = useRef(null);
364
488
  if (clientRef.current === null) {
365
- const latestAdapters = {
366
- targets: { listTargets: () => adaptersRef.current.targets.listTargets() },
367
- navigator: {
368
- describeBlockers: () => adaptersRef.current.navigator.describeBlockers(),
369
- open: (target, request) => adaptersRef.current.navigator.open(target, request),
370
- currentLocation: () => adaptersRef.current.navigator.currentLocation()
371
- },
372
- reader: { read: (options) => adaptersRef.current.reader.read(options) },
373
- highlighter: {
374
- mark: (references, note) => adaptersRef.current.highlighter.mark(references, note),
375
- clear: () => adaptersRef.current.highlighter.clear()
376
- },
377
- isVisible: () => adaptersRef.current.isVisible()
378
- };
379
- Object.defineProperties(latestAdapters, {
380
- picture: { enumerable: true, get: () => adaptersRef.current.picture },
381
- agentWindow: { enumerable: true, get: () => adaptersRef.current.agentWindow },
382
- pathGuide: { enumerable: true, get: () => adaptersRef.current.pathGuide },
383
- requestHandlers: { enumerable: true, get: () => adaptersRef.current.requestHandlers }
384
- });
489
+ const latestAdapters = createLatestScreenAdapters(() => adaptersRef.current);
385
490
  clientRef.current = new AgentUIClient(latestAdapters, { readUIState: () => uiStateSourceRef.current?.readUIState() ?? null });
386
491
  }
387
492
  return clientRef.current;
@@ -1864,11 +1969,13 @@ export {
1864
1969
  MAX_ANSWER_TEXT_CHARS,
1865
1970
  MAX_BUTTONS,
1866
1971
  MAX_CELL_CHARS,
1972
+ MAX_DESCRIBED_FILTER_VALUES,
1867
1973
  MAX_PICTURE_ELEMENTS,
1868
1974
  MAX_PICTURE_ENCODE_ATTEMPTS,
1869
1975
  MAX_SCREEN_CHARS,
1870
1976
  MAX_SECTION_TEXT_CHARS,
1871
1977
  MAX_TABLE_ROWS,
1978
+ MAX_TARGETS_WITH_FILTER_VALUES,
1872
1979
  MAX_TARGET_LIST_CHARS,
1873
1980
  MAX_VALUE_CHARS,
1874
1981
  MIN_SECONDS_LEFT_TO_START,
@@ -1900,15 +2007,22 @@ export {
1900
2007
  createDomScreenTargetSource,
1901
2008
  createFrameDefaultStyleSource,
1902
2009
  createGuideCounter,
2010
+ describeScreenFilterValues,
2011
+ describeScreenTargetFilter,
2012
+ describeUnknownScreenFilterValue,
1903
2013
  fieldReferenceNameOf,
2014
+ findUnknownScreenFilterValue,
1904
2015
  formatActions,
1905
2016
  formatButtons,
1906
2017
  formatFieldLine,
2018
+ formatScreenTargetFilterValues,
1907
2019
  formatScreenTargetLine,
1908
2020
  formatScreenTargetList,
2021
+ formatScreenTargetShortLine,
1909
2022
  formatSortColumns,
1910
2023
  formatTable,
1911
2024
  isClientToolToken,
2025
+ isDescribedScreenTargetFilter,
1912
2026
  isOpenTab,
1913
2027
  isOpenableByAgent,
1914
2028
  isPressableByAgent,
@@ -1923,6 +2037,7 @@ export {
1923
2037
  readScreenTargetOfMark,
1924
2038
  rowKeyOf,
1925
2039
  e as runClientToolRequestForDevelopment,
2040
+ screenTargetFiltersOfTab,
1926
2041
  slugOf,
1927
2042
  sortDirectionOf,
1928
2043
  useAgentUIClient