pgkiln 0.50.0 → 0.52.0

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.
Files changed (104) hide show
  1. package/SECURITY.md +7 -0
  2. package/db/migrations/119_workspace_home.sql +30 -0
  3. package/db/migrations/120_datasets.sql +87 -0
  4. package/db/migrations/121_ai_agents.sql +94 -0
  5. package/db/migrations/122_team_development.sql +186 -0
  6. package/db/migrations/123_workflow_outcomes.sql +407 -0
  7. package/db/migrations/124_component_details.sql +51 -0
  8. package/docs/apex-feature-parity.md +15 -12
  9. package/docs/guide/01-installation.md +9 -3
  10. package/docs/guide/03-builder.md +211 -11
  11. package/docs/guide/04-pages-and-regions.md +96 -22
  12. package/docs/guide/05-items.md +7 -1
  13. package/docs/guide/06-processing.md +123 -8
  14. package/docs/guide/07-dynamic-actions.md +34 -0
  15. package/docs/guide/09-reference.md +2 -1
  16. package/docs/guide/11-from-apex.md +2 -1
  17. package/docs/guide/12-development.md +70 -11
  18. package/docs/guide/20-ai-agents.md +4 -2
  19. package/examples/hr/README.md +2 -0
  20. package/examples/hr/hr_63_datasets.sql +29 -0
  21. package/examples/hr/hr_64_ai_agent.sql +97 -0
  22. package/examples/hr/hr_65_ask_reports.sql +21 -0
  23. package/examples/hr/hr_66_team_development.sql +58 -0
  24. package/examples/hr/hr_67_workflow_outcomes.sql +113 -0
  25. package/examples/hr/hr_68_component_details.sql +96 -0
  26. package/package.json +1 -1
  27. package/public/app.css +46 -0
  28. package/public/app.js +134 -20
  29. package/public/builder.css +116 -0
  30. package/public/builder.js +47 -0
  31. package/src/ai/chat.ts +18 -18
  32. package/src/ai/service.ts +35 -11
  33. package/src/ai/types.ts +7 -2
  34. package/src/app.ts +11 -1
  35. package/src/builder/advisor.ts +6 -3
  36. package/src/builder/ai-agents.ts +372 -0
  37. package/src/builder/ai-builder.ts +3 -3
  38. package/src/builder/ai.ts +13 -9
  39. package/src/builder/assistant.ts +22 -7
  40. package/src/builder/components.ts +38 -9
  41. package/src/builder/datasets.ts +472 -0
  42. package/src/builder/designer.ts +8 -3
  43. package/src/builder/feedback.ts +6 -1
  44. package/src/builder/home.ts +12 -7
  45. package/src/builder/instance.ts +2 -0
  46. package/src/builder/locks.ts +7 -1
  47. package/src/builder/region-help.ts +2 -2
  48. package/src/builder/reporter.ts +52 -3
  49. package/src/builder/routes.ts +7 -4
  50. package/src/builder/shared.ts +1 -1
  51. package/src/builder/subscriptions.ts +1 -0
  52. package/src/builder/team.ts +991 -0
  53. package/src/builder/ui.ts +8 -5
  54. package/src/builder/workflow-migrate.ts +210 -0
  55. package/src/builder/workflows.ts +29 -6
  56. package/src/builder/workspace-home.ts +285 -0
  57. package/src/builder/workspace-transfer.ts +1 -1
  58. package/src/builder/workspaces.ts +7 -4
  59. package/src/cli/replace.ts +7 -1
  60. package/src/datasets.ts +367 -0
  61. package/src/i18n/ar.ts +33 -0
  62. package/src/i18n/cs.ts +33 -0
  63. package/src/i18n/da.ts +33 -0
  64. package/src/i18n/de.ts +33 -0
  65. package/src/i18n/el.ts +33 -0
  66. package/src/i18n/es.ts +33 -0
  67. package/src/i18n/fi.ts +33 -0
  68. package/src/i18n/fr.ts +33 -0
  69. package/src/i18n/he.ts +33 -0
  70. package/src/i18n/it.ts +33 -0
  71. package/src/i18n/ja.ts +33 -0
  72. package/src/i18n/ko.ts +33 -0
  73. package/src/i18n/nb.ts +33 -0
  74. package/src/i18n/pl.ts +33 -0
  75. package/src/i18n/pt.ts +33 -0
  76. package/src/i18n/ru.ts +33 -0
  77. package/src/i18n/sv.ts +33 -0
  78. package/src/i18n/tr.ts +33 -0
  79. package/src/i18n/uk.ts +33 -0
  80. package/src/i18n/zh.ts +33 -0
  81. package/src/i18n.ts +66 -0
  82. package/src/instance.ts +3 -0
  83. package/src/metadata.ts +9 -3
  84. package/src/release-check.ts +139 -0
  85. package/src/reporting-app.ts +101 -0
  86. package/src/runtime/ai-agents.ts +365 -0
  87. package/src/runtime/ai-filter.ts +385 -107
  88. package/src/runtime/assistant.ts +64 -11
  89. package/src/runtime/context.ts +2 -0
  90. package/src/runtime/data-reporter.ts +199 -32
  91. package/src/runtime/ir-phrases.ts +812 -0
  92. package/src/runtime/ir-plan.ts +449 -0
  93. package/src/runtime/items.ts +1 -1
  94. package/src/runtime/regions.ts +6 -2
  95. package/src/runtime/render.ts +1 -1
  96. package/src/runtime/report.ts +1 -1
  97. package/src/runtime/tasks.ts +63 -12
  98. package/src/session.ts +2 -0
  99. package/src/subscriptions.ts +2 -1
  100. package/src/task-events.ts +232 -0
  101. package/src/team-samples.ts +74 -0
  102. package/src/team.ts +342 -0
  103. package/src/workflow.ts +135 -18
  104. package/src/workspace-export.ts +39 -4
@@ -2,27 +2,43 @@ import type { FastifyInstance } from 'fastify';
2
2
  import { generate } from '../ai/service.ts';
3
3
  import { AiError } from '../ai/types.ts';
4
4
  import { applyBinds } from '../binds.ts';
5
- import { appTx, owner } from '../db.ts';
6
- import { html, type Raw } from '../html.ts';
5
+ import { appTx, owner, savepoint } from '../db.ts';
6
+ import { esc, html, raw, type Raw } from '../html.ts';
7
7
  import type { Region } from '../metadata.ts';
8
8
  import { logActivity, saveState } from '../session.ts';
9
9
  import { checkPageAccess, computeVisibility, Forbidden } from './authz.ts';
10
+ import { REPORT_CHART_KINDS } from './charts.ts';
10
11
  import { bindValues, dbg, publicError, stripSemicolon, type PageContext } from './context.ts';
11
- import { fieldsOf, headingOf, isNumeric, opLabel, OPERATORS, visibleColumns } from './report.ts';
12
+ import { applyPlan, checkPlan, chipsOf, ChipRefused, editChip, planIsEmpty, removeChip, type Chip, type Column, type Kind, type Plan } from './ir-plan.ts';
13
+ import { parseQuestion, type Lookup } from './ir-phrases.ts';
14
+ import { AGGREGATES, fieldsOf, headingOf, HIGHLIGHT_COLORS, isNumeric, opLabel, OPERATORS, q, reportState, visibleColumns, withComputations } from './report.ts';
12
15
  import { resolveRestRegion } from './rest-sources.ts';
13
16
  import { loadContext, simplePage, txContext, type Req } from './routes.ts';
14
17
 
15
- // Natural-language filters on an interactive report (APEX: NL2IR). With
18
+ // "Ask in your own words" on an interactive report (APEX: AI interactive
19
+ // reports, 26.1/26.2). With
16
20
  // "ai_filter": {"service": "HR_ASSISTANT", "placeholder": "…", "public": false}
17
- // in a report region's config, an "Ask in your own words" box sits above the
18
- // report. The question goes to the AI service with the report's columns
19
- // (names, headings, kinds) and the report's operators; the answer is a
20
- // structured output (filters, a search text, a sort) that is checked
21
- // against those columns and operators and becomes the report's ordinary
22
- // URL parameters (r<id>_f, r<id>_q, r<id>_s, r<id>_d). So the model can only
23
- // produce what the user could set by hand in the Actions menu: the report's
24
- // own code builds the SQL (values as escaped literals) and runs it as the
25
- // application's role.
21
+ // in a report region's config (or "ai_filter": {} for the built-in phrases
22
+ // only), a question box sits above the report. The question becomes the
23
+ // report's own settings: filters, highlights, a search, a sort, a control
24
+ // break, aggregates, group by, a pivot, a chart and computed columns.
25
+ //
26
+ // With an AI service the question goes to it with the report's shown columns
27
+ // (names, headings, kinds) and the allowed operators and functions; the
28
+ // answer is a structured output. Without one (or when the application may
29
+ // not use it) a rule-based parser reads common phrases (ir-phrases.ts).
30
+ // Either answer is checked against the report's shown columns and whitelists
31
+ // (ir-plan.ts) and becomes the report's ordinary URL parameters, so the model
32
+ // or the parser can only set what the user could set by hand in the Actions
33
+ // menu: the report's own code builds the SQL (values as escaped literals) and
34
+ // runs it as the application's role.
35
+ //
36
+ // The settings in force are listed under the box as chips, each with Edit (a
37
+ // small form) and Remove; Undo returns to the settings before the last
38
+ // change. All are buttons of one POST form (CSRF-checked), so they work
39
+ // without JavaScript; the undo history is kept in the session.
40
+
41
+ export { type Column } from './ir-plan.ts';
26
42
 
27
43
  export interface AiFilterConfig {
28
44
  service?: string;
@@ -31,22 +47,27 @@ export interface AiFilterConfig {
31
47
  }
32
48
 
33
49
  export const MAX_QUESTION_CHARS = 500;
34
- const MAX_FILTERS = 5;
35
50
  const SERVICE = /^[A-Za-z][A-Za-z0-9_]{0,59}$/;
51
+ const MAX_PARAMS = 4000;
52
+ const MAX_UNDO = 5;
53
+ const MAX_CANDIDATES = 30;
54
+ const MAX_LOOKUP_COLUMNS = 12;
36
55
 
37
56
  export function aiFilterOf(r: Region): AiFilterConfig | null {
38
57
  const c = r.config?.ai_filter;
39
58
  if (r.type !== 'report' || r.config?.interactive === false || r.config?.searchable === false) return null;
40
- return c && typeof c === 'object' && !Array.isArray(c) && typeof c.service === 'string' && SERVICE.test(c.service) ? (c as AiFilterConfig) : null;
59
+ if (!c || typeof c !== 'object' || Array.isArray(c)) return null;
60
+ if (c.service !== undefined && c.service !== null && (typeof c.service !== 'string' || !SERVICE.test(c.service))) return null;
61
+ return c as AiFilterConfig;
41
62
  }
42
63
 
43
64
  /** Problems with a report region's ai_filter setting (builder). */
44
65
  export function aiFilterProblems(c: unknown): string[] {
45
66
  if (c === undefined || c === null) return [];
46
- if (typeof c !== 'object' || Array.isArray(c)) return ['"ai_filter" is an object: {"service": "NAME"}.'];
67
+ if (typeof c !== 'object' || Array.isArray(c)) return ['"ai_filter" is an object: {} (built-in phrases) or {"service": "NAME"}.'];
47
68
  const x = c as Record<string, unknown>;
48
69
  const out: string[] = [];
49
- if (typeof x.service !== 'string' || !SERVICE.test(x.service)) out.push('"ai_filter.service": the name of an AI service.');
70
+ if (x.service !== undefined && x.service !== null && (typeof x.service !== 'string' || !SERVICE.test(x.service))) out.push('"ai_filter.service": the name of an AI service (or leave it out for the built-in phrases).');
50
71
  if (x.placeholder !== undefined && typeof x.placeholder !== 'string') out.push('"ai_filter.placeholder" is text.');
51
72
  if (x.public !== undefined && typeof x.public !== 'boolean') out.push('"ai_filter.public" is true or false.');
52
73
  return out;
@@ -55,169 +76,426 @@ export function aiFilterProblems(c: unknown): string[] {
55
76
  const mayUse = (ctx: PageContext, c: AiFilterConfig) => ctx.user !== 'nobody' || ctx.app.authentication === 'none' || c.public === true;
56
77
 
57
78
  /** May the application use the service (enabled and allowed)? */
58
- async function available(appId: number, service: string) {
79
+ async function available(appId: number, service: string | undefined) {
80
+ if (!service) return false;
59
81
  return !!(await owner.one(
60
82
  `select 1 from meta.ai_service s join meta.app_ai_service x on x.service_id = s.id and x.app_id = $1 where s.name = $2 and s.enabled`,
61
83
  [appId, service.toUpperCase()],
62
84
  ));
63
85
  }
64
86
 
65
- /** The "Ask in your own words" box above a report ('' when the report has none). */
66
- export async function renderAiFilter(ctx: PageContext, r: Region): Promise<Raw | ''> {
67
- const c = aiFilterOf(r);
68
- if (!c || !mayUse(ctx, c) || !(await available(ctx.app.id, c.service!))) return '';
69
- const t = ctx.locale.t;
70
- const form = `aif${r.id}`;
71
- // the user's last question is in the box: never share a cached copy of the region between users
72
- ctx.userBound = true;
73
- ctx.detached.push(html`<form id="${form}" method="post" action="${ctx.base}/${ctx.page.page_no}/report/${r.id}/ask">
74
- <input type="hidden" name="__csrf" value="${ctx.session.csrf_token}"><input type="hidden" name="params" value="${ctx.params.toString().slice(0, 4000)}"></form>`);
75
- return html`<div class="ai-filter">
76
- <label class="label" for="${form}_q">${t('ai_filter.label')}</label>
77
- <div class="ai-filter-row">
78
- <input id="${form}_q" name="question" form="${form}" maxlength="${MAX_QUESTION_CHARS}" required value="${ctx.session.state[`__AIQ${r.id}`] ?? ''}" placeholder="${c.placeholder ? ctx.locale.tr(c.placeholder) : t('ai_filter.placeholder')}">
79
- <button class="btn" form="${form}" data-busy="${t('assistant.thinking')}">${t('ai_filter.ask')}</button>
80
- </div>
81
- </div>`;
82
- }
83
-
84
- type Kind = 'number' | 'date' | 'text' | 'boolean';
85
87
  const DATE_OIDS = new Set([1082, 1114, 1184]);
86
88
  const kindOf = (oid: number): Kind => (isNumeric(oid) ? 'number' : DATE_OIDS.has(oid) ? 'date' : oid === 16 ? 'boolean' : 'text');
87
89
 
88
- interface Column {
89
- name: string;
90
- label: string;
91
- kind: Kind;
92
- /** position in the report's query (ORDER BY n) */
93
- pos: number;
90
+ /**
91
+ * The columns a question may use: the report's shown columns (not "hidden")
92
+ * and its computed columns, from the query with the given URL parameters.
93
+ * Needs ctx.client.
94
+ */
95
+ async function askColumns(ctx: PageContext, r: Region, params: URLSearchParams) {
96
+ await resolveRestRegion(ctx, r);
97
+ const st = reportState({ ...ctx, params } as PageContext, r);
98
+ const base = stripSemicolon(applyBinds(r.source ?? 'select 1', bindValues(ctx)));
99
+ const { src, errors } = await withComputations(ctx, r, base, st);
100
+ const fields = await fieldsOf(ctx, src);
101
+ const computed = new Set(st.computations.map((c) => c.name.toLowerCase()));
102
+ const baseCount = src === base ? fields.length : (await fieldsOf(ctx, base)).length;
103
+ const columns = visibleColumns(r, fields).map(({ f, i }): Column => ({
104
+ name: f.name, label: headingOf(r, f.name, ctx.locale.tr), kind: kindOf(f.dataTypeID), pos: i + 1,
105
+ ...(i >= baseCount && computed.has(f.name.toLowerCase()) ? { computed: true } : {}),
106
+ }));
107
+ return { columns, fieldCount: fields.length, src, errors };
108
+ }
109
+
110
+ /** Stored values of the report's text columns matching the candidates (a query parameter): for "only clerks". */
111
+ function valueLookup(ctx: PageContext, columns: Column[], src: string): Lookup {
112
+ return async (column, candidates) => {
113
+ const cols = columns.filter((c) => c.kind === 'text' && !c.computed && (column === null || c.name === column)).slice(0, MAX_LOOKUP_COLUMNS);
114
+ const cands = [...new Set(candidates.map((x) => x.slice(0, 200).replace(/\0/g, '')).filter(Boolean))].slice(0, MAX_CANDIDATES);
115
+ if (!cols.length || !cands.length) return null;
116
+ const c = ctx.client!;
117
+ const res = await savepoint(c, () =>
118
+ c.query({
119
+ text: `select x.i, x.v from (\n${src}\n) "__q" cross join lateral (values ${cols.map((col, i) => `(${i}, ${q(col.name)}::text)`).join(', ')}) as x(i, v)
120
+ where lower(x.v) = any($1::text[]) order by length(x.v) desc limit 1`,
121
+ values: [cands],
122
+ }),
123
+ );
124
+ const row = res.rows[0] as { i: number; v: string } | undefined;
125
+ return row ? { column: cols[row.i].name, value: row.v, candidate: row.v.toLowerCase() } : null;
126
+ };
94
127
  }
95
128
 
96
- /** The structured output asked for: filters, search and sort over the given columns. */
97
- export function filterSchema(columns: Column[]) {
129
+ // ------------------------------------------------------------------ the AI service's answer
130
+
131
+ /** The structured output asked for: every setting the report has, over the given columns. */
132
+ export function planSchema(columns: Column[]) {
98
133
  const names = columns.map((c) => c.name);
134
+ const ops = Object.keys(OPERATORS);
135
+ const fns = Object.keys(AGGREGATES);
136
+ const agg = { type: 'object', properties: { function: { type: 'string', enum: fns }, column: { type: 'string', enum: names } }, required: ['function', 'column'], additionalProperties: false };
99
137
  return {
100
138
  type: 'object',
101
139
  properties: {
140
+ reset: { type: 'boolean', description: 'True to clear the current settings first ("start over"); otherwise false.' },
102
141
  filters: {
103
142
  type: 'array',
104
- description: `At most ${MAX_FILTERS} conditions; all must hold.`,
143
+ description: 'At most 5 conditions; all must hold. A filter on a column replaces that column\'s current filters.',
105
144
  items: {
106
145
  type: 'object',
107
146
  properties: {
108
147
  column: { type: 'string', enum: names },
109
- operator: { type: 'string', enum: Object.keys(OPERATORS) },
148
+ operator: { type: 'string', enum: ops },
110
149
  value: { type: 'string', description: 'The value to compare with (a date as YYYY-MM-DD, a number without thousands separators); empty for null and not_null.' },
111
150
  },
112
151
  required: ['column', 'operator', 'value'],
113
152
  additionalProperties: false,
114
153
  },
115
154
  },
155
+ highlights: {
156
+ type: 'array',
157
+ description: 'Rows to color (not to filter), at most 5.',
158
+ items: {
159
+ type: 'object',
160
+ properties: { column: { type: 'string', enum: names }, operator: { type: 'string', enum: ops }, value: { type: 'string' }, color: { type: 'string', enum: HIGHLIGHT_COLORS } },
161
+ required: ['column', 'operator', 'value', 'color'],
162
+ additionalProperties: false,
163
+ },
164
+ },
116
165
  search: { type: 'string', description: 'Words to look for in every column, only when no column filter fits; otherwise empty.' },
117
166
  sort_column: { type: 'string', enum: ['', ...names], description: 'The column to sort by, or empty.' },
118
167
  sort_descending: { type: 'boolean' },
168
+ control_break: { type: 'string', enum: ['', ...names], description: 'A column to group the rows under headings by, or empty.' },
169
+ aggregates: { type: 'array', description: 'Totals under the report (sum and avg only on number columns).', items: agg },
170
+ group_by: { type: 'array', description: 'Up to 3 columns for a Group By view (with group_functions); empty for none.', items: { type: 'string', enum: names } },
171
+ group_functions: { type: 'array', description: 'The functions per group (with group_by).', items: agg },
172
+ pivot: {
173
+ type: ['object', 'null'],
174
+ description: 'A pivot view: rows by one column, columns by another, a function of a value column; null for none.',
175
+ properties: { row: { type: 'string', enum: names }, column: { type: 'string', enum: names }, function: { type: 'string', enum: fns }, value: { type: 'string', enum: names } },
176
+ required: ['row', 'column', 'function', 'value'],
177
+ additionalProperties: false,
178
+ },
179
+ chart: {
180
+ type: ['object', 'null'],
181
+ description: 'A chart view: a label column and a function of a value column; null for none.',
182
+ properties: { type: { type: 'string', enum: REPORT_CHART_KINDS }, label: { type: 'string', enum: names }, function: { type: 'string', enum: fns }, value: { type: 'string', enum: names } },
183
+ required: ['type', 'label', 'function', 'value'],
184
+ additionalProperties: false,
185
+ },
186
+ computed_columns: {
187
+ type: 'array',
188
+ description: 'New columns computed from the others, at most 5.',
189
+ items: {
190
+ type: 'object',
191
+ properties: {
192
+ name: { type: 'string', description: 'A short name (letters, digits, spaces).' },
193
+ expression: { type: 'string', description: 'An expression over the columns by name: numbers, \'text\', + - * / ||, parentheses and abs ceil floor round trunc mod power upper lower initcap length trim substr left right replace concat coalesce nullif greatest least. Not SQL.' },
194
+ },
195
+ required: ['name', 'expression'],
196
+ additionalProperties: false,
197
+ },
198
+ },
119
199
  },
120
- required: ['filters', 'search', 'sort_column', 'sort_descending'],
200
+ required: ['reset', 'filters', 'highlights', 'search', 'sort_column', 'sort_descending', 'control_break', 'aggregates', 'group_by', 'group_functions', 'pivot', 'chart', 'computed_columns'],
121
201
  additionalProperties: false,
122
202
  };
123
203
  }
124
204
 
205
+ /** (sprint 36 name) the structured output's schema */
206
+ export const filterSchema = planSchema;
207
+
125
208
  const OPERATOR_TEXT: Record<string, string> = {
126
209
  eq: 'equals', ne: 'differs from', contains: 'contains the text (case-insensitive)', not_contains: 'does not contain the text',
127
210
  gt: 'is greater than (later than, for dates)', ge: 'is at least', lt: 'is less than (earlier than, for dates)', le: 'is at most',
128
211
  null: 'is empty', not_null: 'is not empty',
129
212
  };
130
213
 
131
- export function filterSystem(columns: Column[], today: string) {
214
+ export function planSystem(columns: Column[], today: string, current: string) {
132
215
  return [
133
- 'You turn a question about a report into the report\'s filters, search and sort. Answer only with the requested JSON.',
216
+ 'You turn a request about a report into the report\'s settings: filters, highlights, a search, a sort, a control break, totals, a group by, a pivot, a chart and computed columns. Answer only with the requested JSON; leave out what wasn\'t asked for (empty lists, empty strings, null).',
134
217
  'Use only these columns (name: heading, kind):',
135
- ...columns.map((c) => `- ${c.name}: ${c.label.replace(/\s+/g, ' ')}, ${c.kind}`),
218
+ ...columns.map((c) => `- ${c.name}: ${c.label.replace(/\s+/g, ' ')}, ${c.kind}${c.computed ? ' (computed)' : ''}`),
136
219
  'Operators:',
137
220
  ...Object.keys(OPERATORS).map((op) => `- ${op}: ${OPERATOR_TEXT[op] ?? op}`),
221
+ `Functions: ${Object.keys(AGGREGATES).join(', ')} (sum and avg need a number column).`,
138
222
  `Today is ${today}. For "this year", "last month" and the like, use date bounds with ge and le.`,
139
- 'Text values are compared as they are stored: if unsure of the exact spelling, prefer contains. When the question asks for something the columns can\'t express, leave it out.',
223
+ 'Text values are compared as they are stored: if unsure of the exact spelling, prefer contains. When the request asks for something the columns can\'t express, leave it out.',
224
+ current ? `The report's current settings (the request may refine them): ${current}` : 'The report has no settings yet.',
140
225
  ].join('\n');
141
226
  }
142
227
 
143
- interface Answer {
144
- filters: { column: string; operator: string; value: string }[];
145
- search: string;
146
- sort_column: string;
147
- sort_descending: boolean;
228
+ export const filterSystem = (columns: Column[], today: string) => planSystem(columns, today, '');
229
+
230
+ /** (sprint 36 name) the answer checked: only known columns, operators and functions */
231
+ export const checkAnswer = (json: unknown, columns: Column[]) => checkPlan(json, columns);
232
+
233
+ // ------------------------------------------------------------------ texts
234
+
235
+ type T = PageContext['locale']['t'];
236
+
237
+ function describePlan(p: Plan, t: T, label: (n: string) => string) {
238
+ return [
239
+ ...(p.reset ? [t('report.reset')] : []),
240
+ ...p.filters.map((f) => `${label(f.column)} ${opLabel(t, f.operator)}${f.value ? ` ${f.value}` : ''}`),
241
+ ...(p.search ? [`"${p.search}"`] : []),
242
+ ...(p.sort ? [t('ai_filter.sort', { column: `${p.sort.label}${p.desc ? ' ↓' : ''}` })] : []),
243
+ ...p.highlights.map((h) => `${t('report.highlight')}: ${label(h.column)} ${opLabel(t, h.operator)}${h.value ? ` ${h.value}` : ''}`),
244
+ ...(p.breakCol ? [`${t('report.break')}: ${label(p.breakCol)}`] : []),
245
+ ...p.aggregates.map((a) => `${t(`agg.${a.fn}`)}: ${label(a.column)}`),
246
+ ...(p.groupBy.length ? [`${t('report.group_by')}: ${p.groupBy.map(label).join(', ')}`] : []),
247
+ ...p.groupFunctions.map((a) => `${t(`agg.${a.fn}`)}: ${label(a.column)}`),
248
+ ...(p.pivot ? [`${t('report.pivot')}: ${label(p.pivot.row)} × ${label(p.pivot.column)}`] : []),
249
+ ...(p.chart ? [`${t('report.chart')}: ${t(`chart.${p.chart.kind}`)}, ${label(p.chart.label)}`] : []),
250
+ ...p.computed.map((c) => `${c.name} = ${c.expr}`),
251
+ ].join('; ');
148
252
  }
149
253
 
150
- /** The model's answer checked: only known columns and operators, short values. */
151
- export function checkAnswer(json: unknown, columns: Column[]) {
152
- const a = (json ?? {}) as Partial<Answer>;
153
- const byName = new Map(columns.map((c) => [c.name, c]));
154
- const filters = (Array.isArray(a.filters) ? a.filters : [])
155
- .filter((f) => f && typeof f === 'object' && byName.has(f.column) && Object.hasOwn(OPERATORS, f.operator) && typeof f.value === 'string')
156
- .slice(0, MAX_FILTERS)
157
- .map((f) => ({ column: f.column, operator: f.operator, value: OPERATORS[f.operator].noValue ? '' : f.value.replace(/[\r\n]+/g, ' ').trim().slice(0, 200) }))
158
- .filter((f) => OPERATORS[f.operator].noValue || f.value !== '');
159
- const search = typeof a.search === 'string' ? a.search.replace(/[\r\n]+/g, ' ').trim().slice(0, 200) : '';
160
- const sort = typeof a.sort_column === 'string' ? byName.get(a.sort_column) ?? null : null;
161
- return { filters, search, sort, desc: a.sort_descending === true };
254
+ /** A chip's text (plain: it is escaped where it is used). */
255
+ function chipText(chip: Chip, t: T, label: (n: string) => string, byPos: (pos: number) => string) {
256
+ const cond = (column: string, op: string, value: string) => `${label(column)} ${opLabel(t, op)}${OPERATORS[op]?.noValue ? '' : ` ${value}`}`;
257
+ switch (chip.kind) {
258
+ case 'search': return `${t('report.search_chip')}${chip.column ? ` ${label(chip.column)}:` : ''} ${chip.value}`;
259
+ case 'filter': return cond(chip.column, chip.op, chip.value);
260
+ case 'highlight': return `${t('report.highlight')} (${t(`color.${chip.color}`)}): ${cond(chip.column, chip.op, chip.value)}`;
261
+ case 'sort': return t('ai_filter.sort', { column: `${byPos(chip.pos)}${chip.desc ? ' ↓' : ''}` });
262
+ case 'break': return `${t('report.break')}: ${label(chip.column)}`;
263
+ case 'aggregate': return `${t(`agg.${chip.fn}`)}: ${label(chip.column)}`;
264
+ case 'compute': return `${chip.name} = ${chip.expr}`;
265
+ case 'group': return `${t('report.group_by')}: ${label(chip.column)}`;
266
+ case 'groupfn': return t('ai_filter.per_group', { what: `${t(`agg.${chip.fn}`)}: ${label(chip.column)}` });
267
+ case 'pivot': return `${t('report.pivot')}: ${label(chip.row)} × ${label(chip.column)}, ${t(`agg.${chip.fn}`)}: ${label(chip.value)}`;
268
+ case 'chart': return `${t('report.chart')}: ${t(`chart.${chip.type}`)}, ${label(chip.label)}, ${t(`agg.${chip.fn}`)}: ${label(chip.value)}`;
269
+ case 'hide': return `${t('report.hidden_column')}: ${label(chip.column)}`;
270
+ case 'area': case 'near': return t('report.map_area');
271
+ }
272
+ }
273
+
274
+ // ------------------------------------------------------------------ rendering
275
+
276
+ /** The "Ask in your own words" box above a report, with the report's settings as chips ('' when the report has none). */
277
+ export async function renderAiFilter(ctx: PageContext, r: Region): Promise<Raw | ''> {
278
+ const c = aiFilterOf(r);
279
+ if (!c || !mayUse(ctx, c)) return '';
280
+ const t = ctx.locale.t;
281
+ const form = `aif${r.id}`;
282
+ const chipForm = `aic${r.id}`;
283
+ // the user's last question is in the box: never share a cached copy of the region between users
284
+ ctx.userBound = true;
285
+ const params = ctx.params.toString().slice(0, MAX_PARAMS);
286
+ ctx.detached.push(html`<form id="${form}" method="post" action="${ctx.base}/${ctx.page.page_no}/report/${r.id}/ask">
287
+ <input type="hidden" name="__csrf" value="${ctx.session.csrf_token}"><input type="hidden" name="params" value="${params}"></form>`);
288
+
289
+ // the chips: the report's settings in the URL, each with Edit and Remove
290
+ const chips = chipsOf(ctx.params, r.id);
291
+ let columns: Column[] = [];
292
+ let computeErrors: Awaited<ReturnType<typeof askColumns>>['errors'] = [];
293
+ if (chips.length && ctx.client)
294
+ try {
295
+ ({ columns, errors: computeErrors } = await savepoint(ctx.client, () => askColumns(ctx, r, ctx.params)));
296
+ } catch {
297
+ // the report shows its own error; the chips can still be removed
298
+ }
299
+ const label = (n: string) => columns.find((x) => x.name === n)?.label ?? headingOf(r, n, ctx.locale.tr);
300
+ const byPos = (pos: number) => columns.find((x) => x.pos === pos)?.label ?? `#${pos}`;
301
+ const undo = !!ctx.session.state[`__AIU${r.id}`];
302
+ if (chips.length || undo) {
303
+ ctx.askChips ??= new Set();
304
+ ctx.askChips.add(r.id);
305
+ ctx.detached.push(html`<form id="${chipForm}" method="post" action="${ctx.base}/${ctx.page.page_no}/report/${r.id}/chips">
306
+ <input type="hidden" name="__csrf" value="${ctx.session.csrf_token}"><input type="hidden" name="params" value="${params}">${chips.map((ch, i) => html`<input type="hidden" name="k${i}" value="${ch.key}">`)}</form>`);
307
+ }
308
+ const f = (i: number, n: string) => `e${i}_${n}`;
309
+ const id = (i: number, n: string) => `${chipForm}_${i}_${n}`;
310
+ const field = (i: number, n: string, text: string, control: Raw) => html`<span class="ask-field"><label for="${id(i, n)}">${text}</label>${control}</span>`;
311
+ const colSelect = (i: number, n: string, text: string, selected: string, only: (c: Column) => boolean = () => true) =>
312
+ field(i, n, text, html`<select id="${id(i, n)}" name="${f(i, n)}" form="${chipForm}">${columns.filter(only).map((x) => html`<option value="${x.name}"${x.name === selected ? raw(' selected') : ''}>${x.label}</option>`)}</select>`);
313
+ const listSelect = (i: number, n: string, text: string, selected: string, options: [string, string][]) =>
314
+ field(i, n, text, html`<select id="${id(i, n)}" name="${f(i, n)}" form="${chipForm}">${options.map(([v, l]) => html`<option value="${v}"${v === selected ? raw(' selected') : ''}>${l}</option>`)}</select>`);
315
+ const input = (i: number, n: string, text: string, value: string, max = 200) =>
316
+ field(i, n, text, html`<input id="${id(i, n)}" name="${f(i, n)}" form="${chipForm}" value="${value}" maxlength="${max}">`);
317
+ const ops = (i: number, selected: string) => listSelect(i, 'b', t('report.operator'), selected, Object.keys(OPERATORS).map((o) => [o, opLabel(t, o)]));
318
+ const fns = (i: number, n: string, selected: string) => listSelect(i, n, t('report.function'), selected, Object.keys(AGGREGATES).map((x) => [x, t(`agg.${x}`)]));
319
+ const editor = (ch: Chip, i: number): Raw | '' => {
320
+ if (!columns.length) return '';
321
+ switch (ch.kind) {
322
+ case 'search': return input(i, 'a', t('report.search'), ch.value);
323
+ case 'filter': return html`${colSelect(i, 'a', t('report.column'), ch.column)}${ops(i, ch.op)}${input(i, 'c', t('report.value'), ch.value)}`;
324
+ case 'highlight': return html`${colSelect(i, 'a', t('report.column'), ch.column)}${ops(i, ch.op)}${input(i, 'c', t('report.value'), ch.value)}${listSelect(i, 'd', t('report.color'), ch.color, HIGHLIGHT_COLORS.map((x) => [x, t(`color.${x}`)]))}`;
325
+ case 'sort': return html`${colSelect(i, 'a', t('report.column'), columns.find((x) => x.pos === ch.pos)?.name ?? '', (x) => x.pos > 0)}${listSelect(i, 'b', t('report.sort'), ch.desc ? 'desc' : 'asc', [['asc', t('report.sort_asc')], ['desc', t('report.sort_desc')]])}`;
326
+ case 'break': case 'group': return colSelect(i, 'a', t('report.column'), ch.column);
327
+ case 'aggregate': case 'groupfn': return html`${fns(i, 'a', ch.fn)}${colSelect(i, 'b', t('report.column'), ch.column)}`;
328
+ case 'pivot': return html`${colSelect(i, 'a', t('report.pivot_row'), ch.row)}${colSelect(i, 'b', t('report.pivot_column'), ch.column)}${fns(i, 'c', ch.fn)}${colSelect(i, 'd', t('report.value_column'), ch.value)}`;
329
+ case 'chart': return html`${listSelect(i, 'a', t('report.chart_type'), ch.type, REPORT_CHART_KINDS.map((k) => [k, t(`chart.${k}`)]))}${colSelect(i, 'b', t('report.label_column'), ch.label)}${fns(i, 'c', ch.fn)}${colSelect(i, 'd', t('report.value_column'), ch.value)}`;
330
+ case 'compute': return html`${input(i, 'a', t('report.compute_name'), ch.name, 40)}${input(i, 'b', t('report.compute_expr'), ch.expr, 500)}`;
331
+ default: return '';
332
+ }
333
+ };
334
+ const items = chips.map((ch, i) => {
335
+ const text = chipText(ch, t, label, byPos);
336
+ const edit = editor(ch, i);
337
+ // a computed column that doesn't parse is marked (the report says why); a highlight shows its color
338
+ const err = ch.kind === 'compute' ? computeErrors.find((x) => x.c.raw === ch.raw) : undefined;
339
+ return html`<li class="chip ask-chip${err ? ' chip-error' : ''}"${err ? raw(` title="${esc(err.message)}"`) : ''}>${ch.kind === 'highlight' ? html`<span class="swatch hl-${ch.color}" aria-hidden="true"></span>` : ''}<span class="ask-chip-text">${text}</span>${edit
340
+ ? html`<details class="ask-edit"><summary aria-label="${t('ai_filter.edit')}: ${text}">${t('ai_filter.edit')}</summary><div class="ask-edit-body">${edit}<button class="btn" name="edit" value="${i}" form="${chipForm}">${t('report.apply')}</button></div></details>`
341
+ : ''}<button class="ask-chip-x" name="remove" value="${i}" form="${chipForm}" aria-label="${t('ai_filter.remove', { what: text })}" title="${t('report.remove')}">×</button></li>`;
342
+ });
343
+ return html`<div class="ai-filter">
344
+ <label class="label" for="${form}_q">${t('ai_filter.label')}</label>
345
+ <div class="ai-filter-row">
346
+ <input id="${form}_q" name="question" form="${form}" maxlength="${MAX_QUESTION_CHARS}" required value="${ctx.session.state[`__AIQ${r.id}`] ?? ''}" placeholder="${c.placeholder ? ctx.locale.tr(c.placeholder) : t('ai_filter.placeholder')}">
347
+ <button class="btn" form="${form}" data-busy="${t('assistant.thinking')}">${t('ai_filter.ask')}</button>
348
+ ${undo ? html`<button class="btn" name="undo" value="1" form="${chipForm}">${t('ai_filter.undo')}</button>` : ''}
349
+ </div>
350
+ ${chips.length ? html`<ul class="chips ask-chips" aria-label="${t('ai_filter.chips')}">${items}</ul>` : ''}
351
+ </div>`;
352
+ }
353
+
354
+ // ------------------------------------------------------------------ routes
355
+
356
+ /** Remember the settings before a change (Undo), at most MAX_UNDO steps. */
357
+ function pushUndo(ctx: PageContext, rid: number, before: string) {
358
+ const k = `__AIU${rid}`;
359
+ let stack: unknown;
360
+ try {
361
+ stack = JSON.parse(ctx.session.state[k] ?? '[]');
362
+ } catch {
363
+ stack = [];
364
+ }
365
+ const list = Array.isArray(stack) ? stack.filter((x): x is string => typeof x === 'string') : [];
366
+ list.push(before.slice(0, MAX_PARAMS));
367
+ ctx.session.state[k] = JSON.stringify(list.slice(-MAX_UNDO));
368
+ }
369
+
370
+ function popUndo(ctx: PageContext, rid: number): string | null {
371
+ const k = `__AIU${rid}`;
372
+ let stack: unknown;
373
+ try {
374
+ stack = JSON.parse(ctx.session.state[k] ?? '[]');
375
+ } catch {
376
+ stack = [];
377
+ }
378
+ if (!Array.isArray(stack) || !stack.length) return null;
379
+ const prev = stack.pop();
380
+ ctx.session.state[k] = stack.length ? JSON.stringify(stack) : null;
381
+ return typeof prev === 'string' ? prev : null;
162
382
  }
163
383
 
164
384
  export async function aiFilterRoutes(app: FastifyInstance) {
165
- app.post('/a/:alias/:page/report/:id/ask', async (req: Req, reply) => {
385
+ type Reply = Parameters<typeof loadContext>[1];
386
+ /** The request's context, region and settings, or null when a refusal was sent. */
387
+ const prepare = async (req: Req, reply: Reply, what: string) => {
166
388
  const ctx = await loadContext(req, reply);
167
- if (!ctx) return;
389
+ if (!ctx) return null;
168
390
  const t = ctx.locale.t;
169
391
  const body = req.body ?? {};
170
392
  const refuse = (detail: string) => {
171
393
  logActivity({ appId: ctx.app.id, pageNo: ctx.page.page_no, username: ctx.user, event: 'forbidden', ip: ctx.ip, detail });
172
394
  return simplePage(reply, 403, t('error.access_denied'), t(detail.includes('csrf') ? 'error.session_reload' : 'error.report_unavailable'), `${ctx.base}/${ctx.page.page_no}`, ctx.locale);
173
395
  };
174
- if (typeof body.__csrf !== 'string' || body.__csrf !== ctx.session.csrf_token) return refuse('ai filter: csrf');
396
+ if (typeof body.__csrf !== 'string' || body.__csrf !== ctx.session.csrf_token) return { refused: refuse(`${what}: csrf`) };
175
397
  const r = ctx.page.regions.find((x) => x.id === Number(req.params.id) && x.type === 'report');
176
398
  const conf = r ? aiFilterOf(r) : null;
177
- if (!r || !conf) return refuse(`ai filter: region ${req.params.id} on page ${ctx.page.page_no}`);
178
- if (!mayUse(ctx, conf)) return refuse(`ai filter: region ${r.id}: not signed in`);
179
- const params = new URLSearchParams(typeof body.params === 'string' ? body.params.slice(0, 4000) : '');
399
+ if (!r || !conf) return { refused: refuse(`${what}: region ${req.params.id} on page ${ctx.page.page_no}`) };
400
+ if (!mayUse(ctx, conf)) return { refused: refuse(`${what}: region ${r.id}: not signed in`) };
401
+ const params = new URLSearchParams(typeof body.params === 'string' ? body.params.slice(0, MAX_PARAMS) : '');
402
+ return { ctx, r, conf, body, params, refuse };
403
+ };
404
+ /** Inside the application's transaction, with the page's access and the region's condition checked. */
405
+ const inRegion = <X>(ctx: PageContext, r: Region, fn: () => Promise<X>) =>
406
+ appTx(txContext(ctx), async (c) => {
407
+ ctx.client = c;
408
+ try {
409
+ await checkPageAccess(ctx);
410
+ ctx.vis = await computeVisibility(ctx);
411
+ if (!ctx.vis.regions.has(r.id)) throw new Forbidden('region');
412
+ return await fn();
413
+ } finally {
414
+ ctx.client = undefined;
415
+ }
416
+ });
417
+ const back = (reply: Reply, ctx: PageContext, rid: number, params: URLSearchParams) => {
418
+ const qs = params.toString();
419
+ return reply.redirect(`${ctx.base}/${ctx.page.page_no}${qs ? `?${qs}` : ''}#R${rid}`, 303);
420
+ };
421
+
422
+ app.post('/a/:alias/:page/report/:id/ask', async (req: Req, reply) => {
423
+ const p = await prepare(req, reply, 'ai filter');
424
+ if (!p) return;
425
+ if ('refused' in p) return p.refused;
426
+ const { ctx, r, conf, body, params, refuse } = p;
427
+ const t = ctx.locale.t;
428
+ const before = params.toString();
180
429
  const question = typeof body.question === 'string' ? body.question.replace(/\s+/g, ' ').trim() : '';
181
430
  try {
182
431
  if (!question || question.length > MAX_QUESTION_CHARS) throw new AiError('config', t('assistant.too_long', { max: String(MAX_QUESTION_CHARS) }));
183
432
  ctx.session.state[`__AIQ${r.id}`] = question;
184
- const columns = await appTx(txContext(ctx), async (c) => {
185
- ctx.client = c;
186
- await checkPageAccess(ctx);
187
- ctx.vis = await computeVisibility(ctx);
188
- if (!ctx.vis.regions.has(r.id)) throw new Forbidden('region');
189
- await resolveRestRegion(ctx, r);
190
- const fields = await fieldsOf(ctx, stripSemicolon(applyBinds(r.source ?? 'select 1', bindValues(ctx))));
191
- return visibleColumns(r, fields).map(({ f, i }): Column => ({ name: f.name, label: headingOf(r, f.name, ctx.locale.tr), kind: kindOf(f.dataTypeID), pos: i + 1 }));
433
+ const useService = await available(ctx.app.id, conf.service);
434
+ const { columns, fieldCount, parsed } = await inRegion(ctx, r, async () => {
435
+ const { columns, fieldCount, src } = await askColumns(ctx, r, params);
436
+ // without an AI service: the built-in phrases, values looked up in the report's own rows
437
+ const parsed = useService ? null : await parseQuestion(question, columns, ctx.locale.lang, valueLookup(ctx, columns, src));
438
+ return { columns, fieldCount, parsed };
192
439
  });
193
- ctx.client = undefined;
194
- const res = await generate(conf.service!, { system: filterSystem(columns, new Date().toISOString().slice(0, 10)), prompt: question, schema: filterSchema(columns), maxTokens: 2000 }, {
195
- appId: ctx.app.id, pageNo: ctx.page.page_no, user: ctx.user, source: 'nl2ir', debug: (l, x) => dbg(ctx, l, 'ai', x),
196
- });
197
- const a = checkAnswer(res.json, columns);
198
- dbg(ctx, 6, 'ai', () => `report filters from the question: ${JSON.stringify(a)}`);
199
- if (!a.filters.length && !a.search && !a.sort) throw new AiError('output', t('ai_filter.nothing'));
200
- const key = (k: string) => `r${r.id}_${k}`;
201
- for (const k of ['f', 'q', 's', 'd', 'p', 'k']) params.delete(key(k));
202
- for (const f of a.filters) params.append(key('f'), `${f.column}|${f.operator}|${f.value}`);
203
- if (a.search) params.set(key('q'), a.search);
204
- if (a.sort) {
205
- params.set(key('s'), String(a.sort.pos));
206
- if (a.desc) params.set(key('d'), 'desc');
207
- }
208
440
  const label = (name: string) => columns.find((c) => c.name === name)?.label ?? name;
209
- const what = [
210
- ...a.filters.map((f) => `${label(f.column)} ${opLabel(t, f.operator)}${f.value ? ` ${f.value}` : ''}`),
211
- ...(a.search ? [`"${a.search}"`] : []),
212
- ...(a.sort ? [t('ai_filter.sort', { column: `${a.sort.label}${a.desc ? ' ↓' : ''}` })] : []),
213
- ].join('; ');
214
- ctx.session.state.__FLASH = t('ai_filter.applied', { what });
441
+ let plan: Plan;
442
+ if (parsed) {
443
+ plan = checkPlan(parsed.plan, columns, fieldCount);
444
+ dbg(ctx, 6, 'ai', () => `report settings from the phrases: ${JSON.stringify(plan)}; not understood: ${JSON.stringify(parsed.left)}`);
445
+ if (planIsEmpty(plan)) throw new AiError('output', [t('ai_filter.no_match'), ...(parsed.left.length ? [t('ai_filter.not_understood', { what: parsed.left.join('; ') })] : [])].join(' '));
446
+ } else {
447
+ const current = chipsOf(params, r.id).map((ch) => chipText(ch, t, label, (pos) => columns.find((c) => c.pos === pos)?.label ?? `#${pos}`)).join('; ');
448
+ const res = await generate(conf.service!, { system: planSystem(columns, new Date().toISOString().slice(0, 10), current), prompt: question, schema: planSchema(columns), maxTokens: 3000 }, {
449
+ appId: ctx.app.id, pageNo: ctx.page.page_no, user: ctx.user, source: 'nl2ir', debug: (l, x) => dbg(ctx, l, 'ai', x),
450
+ });
451
+ plan = checkPlan(res.json, columns, fieldCount);
452
+ dbg(ctx, 6, 'ai', () => `report settings from the question: ${JSON.stringify(plan)}`);
453
+ if (planIsEmpty(plan)) throw new AiError('output', t('ai_filter.nothing'));
454
+ }
455
+ applyPlan(params, r.id, plan);
456
+ if (params.toString() !== before) pushUndo(ctx, r.id, before);
457
+ ctx.session.state.__FLASH = [t('ai_filter.applied', { what: describePlan(plan, t, label) }), ...(parsed?.left.length ? [t('ai_filter.not_understood', { what: parsed.left.join('; ') })] : [])].join(' ');
215
458
  } catch (e) {
216
459
  if (e instanceof Forbidden) return refuse(`ai filter: region ${r.id} on page ${ctx.page.page_no}`);
217
460
  ctx.session.state.__FLASH_ERROR = e instanceof AiError ? e.message : await publicError(ctx, e, 'AI report filter');
218
461
  }
219
462
  await saveState(ctx.session);
220
- const q = params.toString();
221
- return reply.redirect(`${ctx.base}/${ctx.page.page_no}${q ? `?${q}` : ''}#R${r.id}`, 303);
463
+ return back(reply, ctx, r.id, params);
464
+ });
465
+
466
+ // a chip's Edit or Remove, or Undo
467
+ app.post('/a/:alias/:page/report/:id/chips', async (req: Req, reply) => {
468
+ const p = await prepare(req, reply, 'report chips');
469
+ if (!p) return;
470
+ if ('refused' in p) return p.refused;
471
+ const { ctx, r, body, params, refuse } = p;
472
+ const t = ctx.locale.t;
473
+ const before = params.toString();
474
+ if (body.undo !== undefined) {
475
+ const prev = popUndo(ctx, r.id);
476
+ await saveState(ctx.session);
477
+ return back(reply, ctx, r.id, prev === null ? params : new URLSearchParams(prev));
478
+ }
479
+ const chips = chipsOf(params, r.id);
480
+ const which = typeof body.edit === 'string' ? body.edit : typeof body.remove === 'string' ? body.remove : '';
481
+ const i = /^\d{1,3}$/.test(which) ? Number(which) : -1;
482
+ const chip = chips[i];
483
+ // the chip must be the one the form showed (its key), or the form was tampered with
484
+ if (!chip || body[`k${i}`] !== chip.key) return refuse(`report chips: region ${r.id}: chip ${which.slice(0, 10)}`);
485
+ try {
486
+ if (typeof body.edit === 'string') {
487
+ const field = (n: string) => (typeof body[`e${i}_${n}`] === 'string' ? (body[`e${i}_${n}`] as string) : '');
488
+ const { columns } = await inRegion(ctx, r, () => askColumns(ctx, r, params));
489
+ editChip(params, r.id, chip, { a: field('a'), b: field('b'), c: field('c'), d: field('d'), e: field('e') }, columns, (fn) => t('ai_filter.needs_number', { fn: t(`agg.${fn}`) }));
490
+ } else removeChip(params, r.id, chip);
491
+ } catch (e) {
492
+ if (e instanceof Forbidden || (e instanceof ChipRefused && e.tampered)) return refuse(`report chips: region ${r.id}: ${e.message}`);
493
+ ctx.session.state.__FLASH_ERROR = e instanceof ChipRefused ? t('ai_filter.invalid', { why: e.message }) : await publicError(ctx, e, 'report chips');
494
+ await saveState(ctx.session);
495
+ return back(reply, ctx, r.id, new URLSearchParams(before));
496
+ }
497
+ if (params.toString() !== before) pushUndo(ctx, r.id, before);
498
+ await saveState(ctx.session);
499
+ return back(reply, ctx, r.id, params);
222
500
  });
223
501
  }