@things-factory/ai-dashboard 10.1.277 → 10.1.279
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/package.json +22 -8
- package/client/bootstrap.ts +0 -25
- package/client/components/card-dialogs.ts +0 -445
- package/client/components/card-library.ts +0 -285
- package/client/components/dashboard-card.ts +0 -411
- package/client/components/proposal-preview.ts +0 -95
- package/client/index.ts +0 -5
- package/client/pages/my-dashboard-page.ts +0 -373
- package/client/route.ts +0 -8
- package/client/tsconfig.json +0 -13
- package/client/utils/card-api.ts +0 -99
- package/client/utils/card-words.ts +0 -91
- package/client/utils/examples.ts +0 -15
- package/dist-client/bootstrap.js.map +0 -1
- package/dist-client/components/card-dialogs.js.map +0 -1
- package/dist-client/components/card-library.js.map +0 -1
- package/dist-client/components/dashboard-card.js.map +0 -1
- package/dist-client/components/proposal-preview.js.map +0 -1
- package/dist-client/index.js.map +0 -1
- package/dist-client/pages/my-dashboard-page.js.map +0 -1
- package/dist-client/route.js.map +0 -1
- package/dist-client/tsconfig.tsbuildinfo +0 -1
- package/dist-client/utils/card-api.js.map +0 -1
- package/dist-client/utils/card-words.js.map +0 -1
- package/dist-client/utils/examples.js.map +0 -1
- package/dist-server/index.js.map +0 -1
- package/dist-server/role-tasks.js.map +0 -1
- package/dist-server/service/card/card-access.js.map +0 -1
- package/dist-server/service/card/card-definition.js.map +0 -1
- package/dist-server/service/card/card-names.js.map +0 -1
- package/dist-server/service/card/card-proposal.js.map +0 -1
- package/dist-server/service/card/card-resolver.js.map +0 -1
- package/dist-server/service/card/card-run.js.map +0 -1
- package/dist-server/service/card/card-tool.js.map +0 -1
- package/dist-server/service/card/card-types.js.map +0 -1
- package/dist-server/service/card/dashboard-card.js.map +0 -1
- package/dist-server/service/card/long-text.js.map +0 -1
- package/dist-server/service/index.js.map +0 -1
- package/dist-server/tsconfig.tsbuildinfo +0 -1
- package/server/index.ts +0 -16
- package/server/role-tasks.ts +0 -12
- package/server/service/card/card-access.test.ts +0 -173
- package/server/service/card/card-access.ts +0 -24
- package/server/service/card/card-definition.ts +0 -468
- package/server/service/card/card-intent.test.ts +0 -285
- package/server/service/card/card-names.ts +0 -12
- package/server/service/card/card-proposal.ts +0 -46
- package/server/service/card/card-resolver.ts +0 -440
- package/server/service/card/card-run.test.ts +0 -237
- package/server/service/card/card-run.ts +0 -302
- package/server/service/card/card-tool.ts +0 -368
- package/server/service/card/card-types.ts +0 -145
- package/server/service/card/card-words.test.ts +0 -72
- package/server/service/card/dashboard-card.ts +0 -303
- package/server/service/card/long-text.ts +0 -25
- package/server/service/index.ts +0 -16
- package/test/card-menu.test.ts +0 -50
- package/test/translations.test.ts +0 -42
- package/test/words-not-codes.test.ts +0 -24
- package/tsconfig.json +0 -9
|
@@ -1,468 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* What a dashboard card is - a definition, never its values (operato-application/adr/ADR-0160.md).
|
|
3
|
-
*
|
|
4
|
-
* A card names one read an application declared for the assistant (a list read target, or a list inside a detail
|
|
5
|
-
* read's answer), the conditions it is read with, what is done to its rows (filtered, grouped, ordered, cut) and how
|
|
6
|
-
* the rows are drawn (a number, a table, bars or a line). The assistant writes it once, in a conversation; drawing it
|
|
7
|
-
* runs the definition and asks no model. Nothing a definition holds runs as code: a name it does not know is refused,
|
|
8
|
-
* never passed over.
|
|
9
|
-
*
|
|
10
|
-
* Pure - the rules are tested without a server.
|
|
11
|
-
*/
|
|
12
|
-
import type { DetailList, DetailRead, ReadColumn, ReadTarget } from '@things-factory/ai-assistant'
|
|
13
|
-
|
|
14
|
-
export const CARD_SCHEMA_VERSION = 1
|
|
15
|
-
|
|
16
|
-
export const VIEW_KINDS = ['number', 'table', 'bar', 'line'] as const
|
|
17
|
-
export type ViewKind = (typeof VIEW_KINDS)[number]
|
|
18
|
-
|
|
19
|
-
export const MATCH_OPS = ['eq', 'ne', 'gt', 'gte', 'lt', 'lte', 'in', 'not_in'] as const
|
|
20
|
-
/** The operators whose value is a list - one of these values, none of them. */
|
|
21
|
-
export const LIST_OPS = ['in', 'not_in'] as const
|
|
22
|
-
export type MatchOp = (typeof MATCH_OPS)[number]
|
|
23
|
-
|
|
24
|
-
/** The rows a table shows, and the points a chart draws, at most (ADR-0160 decision 4). */
|
|
25
|
-
export const MAX_TABLE_ROWS = 200
|
|
26
|
-
export const MAX_CHART_POINTS = 50
|
|
27
|
-
/** The AI 작업 패널 draws eight hues and twelve figures; a card that needs more is refused, never folded (decision 5). */
|
|
28
|
-
export const MAX_SERIES = 8
|
|
29
|
-
export const MAX_FIGURES = 12
|
|
30
|
-
|
|
31
|
-
export interface CardSourceRef {
|
|
32
|
-
/** The read's key - `office.cash-transaction`, `office.receivables`. */
|
|
33
|
-
read: string
|
|
34
|
-
/** A detail read's list key - `by-contract`. Absent for a list read target. */
|
|
35
|
-
list?: string
|
|
36
|
-
/** The conditions (a list) or the arguments (a detail read) - a date may be relative (`this-month-start`). */
|
|
37
|
-
where?: Record<string, unknown>
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface CardMatch {
|
|
41
|
-
column: string
|
|
42
|
-
op: MatchOp
|
|
43
|
-
/** A list for `in` and `not_in` (status not_in [APPROVED, REJECTED, WITHDRAWN]); one value otherwise. */
|
|
44
|
-
value: string | number | boolean | (string | number)[]
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface CardRows {
|
|
48
|
-
match?: CardMatch[]
|
|
49
|
-
groupBy?: string
|
|
50
|
-
sortBy?: string
|
|
51
|
-
desc?: boolean
|
|
52
|
-
limit?: number
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface CardView {
|
|
56
|
-
kind: ViewKind
|
|
57
|
-
/** bar · line: the column along the bottom - the grouped column when rows are grouped. */
|
|
58
|
-
label?: string
|
|
59
|
-
/** number · bar · line: the number columns summed or drawn; none counts the rows. table: shown after `columns`. */
|
|
60
|
-
values?: string[]
|
|
61
|
-
/** table: the columns shown, in order. Absent: every column. */
|
|
62
|
-
columns?: string[]
|
|
63
|
-
/** A unit said beside the numbers when no currency says one (`건`, `%`). */
|
|
64
|
-
unit?: string
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export interface CardDefinition {
|
|
68
|
-
schemaVersion: number
|
|
69
|
-
source: CardSourceRef
|
|
70
|
-
rows?: CardRows
|
|
71
|
-
view: CardView
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** A refusal the card and the model both read - a code and what it is about, never prose. */
|
|
75
|
-
export interface CardIssue {
|
|
76
|
-
code: string
|
|
77
|
-
params?: Record<string, unknown>
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** A condition a source takes - its name and type, from the declaration. */
|
|
81
|
-
export interface SourceCondition {
|
|
82
|
-
name: string
|
|
83
|
-
type: string
|
|
84
|
-
values?: string[]
|
|
85
|
-
description?: string
|
|
86
|
-
/** A detail read's argument its query cannot run without. */
|
|
87
|
-
required?: boolean
|
|
88
|
-
/** A string condition that holds a month as YYYY-MM - it takes a relative month (this-month, -5m). */
|
|
89
|
-
format?: 'month'
|
|
90
|
-
/** It takes several values at once - a list read's statuses (`status: [..]`, `statusNot: [..]`). */
|
|
91
|
-
many?: boolean
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** What a card may read from one declaration - the same whichever kind of read it is. */
|
|
95
|
-
export interface CardSource {
|
|
96
|
-
key: string
|
|
97
|
-
kind: 'list' | 'detail'
|
|
98
|
-
labelKey?: string
|
|
99
|
-
description: string
|
|
100
|
-
query: string
|
|
101
|
-
columns: ReadColumn[]
|
|
102
|
-
conditions: SourceCondition[]
|
|
103
|
-
target?: ReadTarget
|
|
104
|
-
detail?: DetailRead
|
|
105
|
-
list?: DetailList
|
|
106
|
-
/** The rows are the viewer's own, whoever looks (내 지출결의, 나의 전략 업무) - ReadTarget.mine · DetailRead.mine. */
|
|
107
|
-
mine?: boolean
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** The source a reference names - or why there is none. */
|
|
111
|
-
export function sourceOf(
|
|
112
|
-
ref: CardSourceRef | undefined,
|
|
113
|
-
readTargetOf: (key: string) => ReadTarget | undefined,
|
|
114
|
-
detailReadOf: (key: string) => DetailRead | undefined
|
|
115
|
-
): { source?: CardSource; issue?: CardIssue } {
|
|
116
|
-
if (!ref || typeof ref.read !== 'string' || !ref.read) return { issue: { code: 'no-source' } }
|
|
117
|
-
const target = readTargetOf(ref.read)
|
|
118
|
-
if (target) {
|
|
119
|
-
if (ref.list) return { issue: { code: 'list-of-a-list', params: { read: ref.read } } }
|
|
120
|
-
return {
|
|
121
|
-
source: {
|
|
122
|
-
key: target.key,
|
|
123
|
-
kind: 'list',
|
|
124
|
-
labelKey: target.labelKey,
|
|
125
|
-
description: target.description,
|
|
126
|
-
query: target.query,
|
|
127
|
-
columns: target.columns,
|
|
128
|
-
conditions: [
|
|
129
|
-
...target.criteria.map(one => ({ name: one.name, type: one.type, values: one.values, description: one.description, ...(one.format ? { format: one.format } : {}), ...(one.many ? { many: true } : {}) })),
|
|
130
|
-
...(target.search ? [{ name: 'text', type: 'string', description: target.search.description }] : [])
|
|
131
|
-
],
|
|
132
|
-
target,
|
|
133
|
-
...(target.mine ? { mine: true } : {})
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
const detail = detailReadOf(ref.read)
|
|
138
|
-
if (!detail) return { issue: { code: 'unknown-read', params: { read: ref.read } } }
|
|
139
|
-
const lists = detail.lists ?? []
|
|
140
|
-
if (!lists.length) return { issue: { code: 'not-a-card-source', params: { read: ref.read } } }
|
|
141
|
-
/* One list needs no name; with several, the card names one. */
|
|
142
|
-
const list = ref.list ? lists.find(one => one.key === ref.list) : lists.length === 1 ? lists[0] : undefined
|
|
143
|
-
if (!list) return { issue: { code: ref.list ? 'unknown-list' : 'list-not-named', params: { read: ref.read, list: ref.list ?? null, lists: lists.map(one => one.key) } } }
|
|
144
|
-
return {
|
|
145
|
-
source: {
|
|
146
|
-
key: detail.key,
|
|
147
|
-
kind: 'detail',
|
|
148
|
-
labelKey: list.labelKey ?? detail.labelKey,
|
|
149
|
-
description: list.description ?? detail.description,
|
|
150
|
-
query: detail.query,
|
|
151
|
-
columns: list.columns,
|
|
152
|
-
conditions: (detail.args ?? []).map(one => ({ name: one.name, type: one.type, values: one.values, description: one.description, required: !!one.required, ...(one.format ? { format: one.format } : {}) })),
|
|
153
|
-
detail,
|
|
154
|
-
list,
|
|
155
|
-
...(detail.mine ? { mine: true } : {})
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/* ── Relative dates (ADR-0160 decision 3) ─────────────────────────────────────────────────────────────────── */
|
|
161
|
-
|
|
162
|
-
/*
|
|
163
|
-
* The words of a period that stays current - the only ones a card keeps:
|
|
164
|
-
* a day today · -30d · this-month-start · last-year-end · -5m-start (the first day of five months ago)
|
|
165
|
-
* a year or a month as an integer this-year · last-month · -5m
|
|
166
|
-
* a month as YYYY-MM this-month · last-month · -5m · this-year-start (its January) · this-year-end
|
|
167
|
-
* "The last six months" is from -5m-start to this-month-end (or a YYYY-MM range from -5m to this-month).
|
|
168
|
-
*/
|
|
169
|
-
const RELATIVE_DATE = /^(today|(this|last|next)-(week|month|quarter|year)-(start|end)|[+-]?\d{1,4}d|-\d{1,2}m-(start|end))$/
|
|
170
|
-
const RELATIVE_PERIOD = /^((this|last|next)-(year|month)|-\d{1,2}m)$/
|
|
171
|
-
const RELATIVE_MONTH = /^((this|last|next)-month|-\d{1,2}m|(this|last|next)-year-(start|end))$/
|
|
172
|
-
/** A value that looks meant as a relative one - checked against the words above, never taken for a literal. */
|
|
173
|
-
const LOOKS_RELATIVE = /^((this|last|next)-|today$|-\d)/
|
|
174
|
-
|
|
175
|
-
export const isRelativeDate = (value: unknown) => typeof value === 'string' && RELATIVE_DATE.test(value)
|
|
176
|
-
export const isRelativePeriod = (value: unknown) => typeof value === 'string' && RELATIVE_PERIOD.test(value)
|
|
177
|
-
export const isRelativeMonth = (value: unknown) => typeof value === 'string' && RELATIVE_MONTH.test(value)
|
|
178
|
-
|
|
179
|
-
const pad = (n: number) => String(n).padStart(2, '0')
|
|
180
|
-
const ymd = (date: Date) => `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`
|
|
181
|
-
const utc = (y: number, m: number, d: number) => new Date(Date.UTC(y, m, d))
|
|
182
|
-
|
|
183
|
-
/** Today in a time zone - `YYYY-MM-DD`; the system's zone when the domain names none or one the runtime does not know. */
|
|
184
|
-
export function todayIn(timeZone: string | undefined, now: Date = new Date()): string {
|
|
185
|
-
try {
|
|
186
|
-
return new Intl.DateTimeFormat('en-CA', { timeZone: timeZone || undefined, year: 'numeric', month: '2-digit', day: '2-digit' }).format(now)
|
|
187
|
-
} catch {
|
|
188
|
-
return new Intl.DateTimeFormat('en-CA', { year: 'numeric', month: '2-digit', day: '2-digit' }).format(now)
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/** A relative date as a day, from `today` - weeks start on Monday, an end is that last day (a range's `to` takes the whole day). */
|
|
193
|
-
export function resolveRelativeDate(value: string, today: string): string {
|
|
194
|
-
const [y, m, d] = today.split('-').map(Number)
|
|
195
|
-
if (value === 'today') return today
|
|
196
|
-
const days = /^([+-]?\d{1,4})d$/.exec(value)
|
|
197
|
-
if (days) return ymd(utc(y, m - 1, d + Number(days[1])))
|
|
198
|
-
const monthsAgo = /^-(\d{1,2})m-(start|end)$/.exec(value)
|
|
199
|
-
if (monthsAgo) return ymd(monthsAgo[2] === 'start' ? utc(y, m - 1 - Number(monthsAgo[1]), 1) : utc(y, m - Number(monthsAgo[1]), 0))
|
|
200
|
-
const [, which, unit, edge] = /^(this|last|next)-(week|month|quarter|year)-(start|end)$/.exec(value)!
|
|
201
|
-
const step = which === 'last' ? -1 : which === 'next' ? 1 : 0
|
|
202
|
-
if (unit === 'week') {
|
|
203
|
-
const weekday = (utc(y, m - 1, d).getUTCDay() + 6) % 7
|
|
204
|
-
const monday = d - weekday + step * 7
|
|
205
|
-
return ymd(utc(y, m - 1, edge === 'start' ? monday : monday + 6))
|
|
206
|
-
}
|
|
207
|
-
if (unit === 'month') return ymd(edge === 'start' ? utc(y, m - 1 + step, 1) : utc(y, m + step, 0))
|
|
208
|
-
if (unit === 'quarter') {
|
|
209
|
-
const first = Math.floor((m - 1) / 3) * 3 + step * 3
|
|
210
|
-
return ymd(edge === 'start' ? utc(y, first, 1) : utc(y, first + 3, 0))
|
|
211
|
-
}
|
|
212
|
-
return ymd(edge === 'start' ? utc(y + step, 0, 1) : utc(y + step, 11, 31))
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/** How many months a relative month moves from this one - `last-month` -1, `-5m` -5. */
|
|
216
|
-
const monthStep = (value: string) => (/^-(\d{1,2})m$/.test(value) ? -Number(value.slice(1, -1)) : value.startsWith('last') ? -1 : value.startsWith('next') ? 1 : 0)
|
|
217
|
-
|
|
218
|
-
/** A relative month as YYYY-MM, from `today` - `this-year-start` is this year's January. */
|
|
219
|
-
export function resolveRelativeMonth(value: string, today: string): string {
|
|
220
|
-
const [y, m] = today.split('-').map(Number)
|
|
221
|
-
const year = /^(this|last|next)-year-(start|end)$/.exec(value)
|
|
222
|
-
if (year) return `${y + (year[1] === 'last' ? -1 : year[1] === 'next' ? 1 : 0)}-${year[2] === 'start' ? '01' : '12'}`
|
|
223
|
-
const month = utc(y, m - 1 + monthStep(value), 1)
|
|
224
|
-
return `${month.getUTCFullYear()}-${pad(month.getUTCMonth() + 1)}`
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* The relative values of a definition's conditions, as of `today` - each by what its condition holds (`conditions`):
|
|
229
|
-
* a month as YYYY-MM, a day, an integer year or month. A year and a month are read from one month: with a month token
|
|
230
|
-
* in the same conditions, `this-year` is that month's year - January's `last-month` with `this-year` is last December,
|
|
231
|
-
* never this year's December.
|
|
232
|
-
*/
|
|
233
|
-
export function resolveWhere(where: Record<string, unknown> | undefined, today: string, conditions: readonly SourceCondition[] = []): Record<string, unknown> {
|
|
234
|
-
const given = where ?? {}
|
|
235
|
-
const formats = new Map(conditions.map(one => [one.name, one.format]))
|
|
236
|
-
const [y, m] = today.split('-').map(Number)
|
|
237
|
-
const monthToken = Object.entries(given).find(([name, value]) => formats.get(name) !== 'month' && typeof value === 'string' && /^((this|last|next)-month|-\d{1,2}m)$/.test(value))?.[1] as string | undefined
|
|
238
|
-
const base = monthToken ? utc(y, m - 1 + monthStep(monthToken), 1) : utc(y, m - 1, 1)
|
|
239
|
-
const baseYear = base.getUTCFullYear()
|
|
240
|
-
const out: Record<string, unknown> = {}
|
|
241
|
-
for (const [name, value] of Object.entries(given)) {
|
|
242
|
-
if (formats.get(name) === 'month' && isRelativeMonth(value)) out[name] = resolveRelativeMonth(value as string, today)
|
|
243
|
-
else if (isRelativeDate(value)) out[name] = resolveRelativeDate(value as string, today)
|
|
244
|
-
else if (isRelativePeriod(value)) {
|
|
245
|
-
const [which, unit] = (value as string).split('-')
|
|
246
|
-
const step = which === 'last' ? -1 : which === 'next' ? 1 : 0
|
|
247
|
-
out[name] = unit === 'year' ? baseYear + step : base.getUTCMonth() + 1
|
|
248
|
-
} else out[name] = value
|
|
249
|
-
}
|
|
250
|
-
return out
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/* ── Checking a definition against its source ─────────────────────────────────────────────────────────────── */
|
|
254
|
-
|
|
255
|
-
/** The columns the rows have after `rows` - a grouped card's group column, its count and its sums. */
|
|
256
|
-
export function shapedColumnNames(source: CardSource, rows: CardRows | undefined): Map<string, ReadColumn> {
|
|
257
|
-
if (!rows?.groupBy) return new Map(source.columns.map(one => [one.name, one]))
|
|
258
|
-
const by = source.columns.find(one => one.name === rows.groupBy)
|
|
259
|
-
const out = new Map<string, ReadColumn>()
|
|
260
|
-
if (by) out.set(by.name, { ...by, type: by.type === 'date' ? 'string' : by.type })
|
|
261
|
-
out.set('count', { name: 'count', type: 'number' })
|
|
262
|
-
for (const column of source.columns.filter(one => one.type === 'number' && one.name !== rows.groupBy)) out.set(column.name, column)
|
|
263
|
-
return out
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const known = (list: readonly string[], value: unknown) => typeof value === 'string' && list.includes(value)
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Whether a definition fits its source - every name it uses is the source's, every value one it takes. Issues, never
|
|
270
|
-
* repairs: a definition the model wrote wrong is sent back with what the source has.
|
|
271
|
-
*/
|
|
272
|
-
export function checkDefinition(definition: CardDefinition | undefined, source: CardSource): CardIssue[] {
|
|
273
|
-
const issues: CardIssue[] = []
|
|
274
|
-
if (!definition || typeof definition !== 'object') return [{ code: 'no-definition' }]
|
|
275
|
-
if (definition.schemaVersion !== CARD_SCHEMA_VERSION) issues.push({ code: 'unknown-schema-version', params: { given: definition.schemaVersion ?? null, known: CARD_SCHEMA_VERSION } })
|
|
276
|
-
|
|
277
|
-
const conditions = new Map(source.conditions.map(one => [one.name, one]))
|
|
278
|
-
const where = definition.source?.where ?? {}
|
|
279
|
-
for (const [name, value] of Object.entries(where)) {
|
|
280
|
-
const condition = conditions.get(name)
|
|
281
|
-
if (!condition && !(source.kind === 'list' && name === 'id')) {
|
|
282
|
-
/* A column put among the conditions - the rows are filtered by it with rows.match instead. */
|
|
283
|
-
const column = source.columns.some(one => one.name === name)
|
|
284
|
-
issues.push({ code: 'unknown-condition', params: { name, conditions: [...conditions.keys()], ...(column ? { isColumn: true, use: `rows.match: [{ column: '${name}', op: 'eq', value: ${JSON.stringify(value)} }]` } : {}) } })
|
|
285
|
-
continue
|
|
286
|
-
}
|
|
287
|
-
if (condition?.format === 'month') {
|
|
288
|
-
if (typeof value === 'string' && LOOKS_RELATIVE.test(value) && !isRelativeMonth(value)) issues.push({ code: 'relative-value-unfit', params: { name, value, takes: 'a month as YYYY-MM, or this-month, last-month, -5m, this-year-start, this-year-end' } })
|
|
289
|
-
continue
|
|
290
|
-
}
|
|
291
|
-
if (typeof value === 'string' && LOOKS_RELATIVE.test(value) && !isRelativeDate(value) && !isRelativePeriod(value)) issues.push({ code: 'unknown-relative-value', params: { name, value } })
|
|
292
|
-
if (isRelativePeriod(value) && condition?.type !== 'integer') issues.push({ code: 'relative-period-on-non-integer', params: { name, value } })
|
|
293
|
-
if (isRelativeDate(value) && condition?.type !== 'date') issues.push({ code: 'relative-date-on-non-date', params: { name, value } })
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/* What the query cannot run without - said before it runs, with what it takes. */
|
|
297
|
-
for (const condition of source.conditions.filter(one => one.required)) {
|
|
298
|
-
const value = where[condition.name]
|
|
299
|
-
if (value === undefined || value === null || value === '') issues.push({ code: 'missing-condition', params: { name: condition.name, ...(condition.values ? { values: condition.values } : {}), ...(condition.description ? { description: condition.description } : {}) } })
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
const columns = new Map(source.columns.map(one => [one.name, one]))
|
|
303
|
-
const columnNames = [...columns.keys()]
|
|
304
|
-
const rows = definition.rows ?? {}
|
|
305
|
-
const scalar = (value: unknown) => typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean'
|
|
306
|
-
for (const match of rows.match ?? []) {
|
|
307
|
-
if (!columns.has(match?.column)) issues.push({ code: 'unknown-column', params: { where: 'match', column: match?.column ?? null, columns: columnNames } })
|
|
308
|
-
if (!known(MATCH_OPS, match?.op)) issues.push({ code: 'unknown-operator', params: { op: match?.op ?? null, ops: MATCH_OPS } })
|
|
309
|
-
else if (known(LIST_OPS, match.op)) {
|
|
310
|
-
if (!Array.isArray(match.value) || !match.value.length || !match.value.every(scalar)) issues.push({ code: 'match-without-values', params: { column: match.column, op: match.op } })
|
|
311
|
-
} else if (match?.value === undefined || match?.value === null || !scalar(match.value)) issues.push({ code: 'match-without-value', params: { column: match?.column ?? null } })
|
|
312
|
-
/*
|
|
313
|
-
* A relative value is read on a date column only - on another it is the literal text 「this-month」, which no row
|
|
314
|
-
* holds: 「이번 달 지출결의 합계」 matched month eq this-month and drew 0 (2026-10-25). A period is the source's
|
|
315
|
-
* condition (a YYYY-MM one takes this-month).
|
|
316
|
-
*/
|
|
317
|
-
const column = columns.get(match?.column)
|
|
318
|
-
const relative = (Array.isArray(match?.value) ? match.value : [match?.value]).filter(one => typeof one === 'string' && LOOKS_RELATIVE.test(one))
|
|
319
|
-
if (column && relative.length) {
|
|
320
|
-
if (column.type !== 'date') issues.push({ code: 'relative-value-on-non-date-column', params: { column: column.name, value: relative[0], periodConditions: source.conditions.filter(one => one.type === 'date' || one.format === 'month' || (one.type === 'integer' && /year|month/i.test(one.name))).map(one => one.name) } })
|
|
321
|
-
else if (relative.some(one => !isRelativeDate(one))) issues.push({ code: 'unknown-relative-value', params: { name: column.name, value: relative.find(one => !isRelativeDate(one)) } })
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
/*
|
|
325
|
-
* Conditions that no row can meet - two `eq` of one column (status 작성중 AND 상신), or an `eq` outside its own `in`.
|
|
326
|
-
* Every match must hold, so these draw nothing, or a number of nothing that looks true (2026-10-25).
|
|
327
|
-
*/
|
|
328
|
-
const equals = new Map<string, Set<string>>()
|
|
329
|
-
for (const match of rows.match ?? []) {
|
|
330
|
-
if (!match || typeof match.column !== 'string') continue
|
|
331
|
-
const values = match.op === 'eq' && scalar(match.value) ? [String(match.value)] : match.op === 'in' && Array.isArray(match.value) ? match.value.map(String) : undefined
|
|
332
|
-
if (!values) continue
|
|
333
|
-
const before = equals.get(match.column)
|
|
334
|
-
const kept = before ? values.filter(one => before.has(one)) : values
|
|
335
|
-
if (before && !kept.length) {
|
|
336
|
-
issues.push({ code: 'match-never-true', params: { column: match.column, use: `one match of ${match.column} with op in and every value it may hold` } })
|
|
337
|
-
continue
|
|
338
|
-
}
|
|
339
|
-
equals.set(match.column, new Set(kept))
|
|
340
|
-
}
|
|
341
|
-
if (rows.groupBy !== undefined) {
|
|
342
|
-
const by = columns.get(rows.groupBy)
|
|
343
|
-
if (!by) issues.push({ code: 'unknown-column', params: { where: 'groupBy', column: rows.groupBy, columns: columnNames } })
|
|
344
|
-
else if (by.type === 'number') issues.push({ code: 'group-by-number', params: { column: rows.groupBy } })
|
|
345
|
-
}
|
|
346
|
-
const shaped = shapedColumnNames(source, rows)
|
|
347
|
-
const shapedNames = [...shaped.keys()]
|
|
348
|
-
if (rows.sortBy !== undefined && !shaped.has(rows.sortBy)) issues.push({ code: 'unknown-column', params: { where: 'sortBy', column: rows.sortBy, columns: shapedNames } })
|
|
349
|
-
if (rows.desc !== undefined && typeof rows.desc !== 'boolean') issues.push({ code: 'desc-not-boolean' })
|
|
350
|
-
|
|
351
|
-
const view = definition.view
|
|
352
|
-
if (!view || !known(VIEW_KINDS, view.kind)) {
|
|
353
|
-
issues.push({ code: 'unknown-view', params: { kind: view?.kind ?? null, kinds: VIEW_KINDS } })
|
|
354
|
-
return issues
|
|
355
|
-
}
|
|
356
|
-
const cap = view.kind === 'table' ? MAX_TABLE_ROWS : view.kind === 'number' ? Infinity : MAX_CHART_POINTS
|
|
357
|
-
if (rows.limit !== undefined && (!Number.isInteger(rows.limit) || rows.limit < 1 || rows.limit > cap)) issues.push({ code: 'limit-out-of-range', params: { limit: rows.limit, max: cap === Infinity ? null : cap } })
|
|
358
|
-
for (const name of view.values ?? []) {
|
|
359
|
-
const column = shaped.get(name)
|
|
360
|
-
if (!column) issues.push({ code: 'unknown-column', params: { where: 'values', column: name, columns: shapedNames } })
|
|
361
|
-
else if (column.type !== 'number') issues.push({ code: 'value-not-a-number', params: { column: name } })
|
|
362
|
-
}
|
|
363
|
-
for (const name of view.columns ?? []) if (!shaped.has(name)) issues.push({ code: 'unknown-column', params: { where: 'columns', column: name, columns: shapedNames } })
|
|
364
|
-
if (view.kind === 'bar' || view.kind === 'line') {
|
|
365
|
-
const label = view.label ?? rows.groupBy
|
|
366
|
-
if (!label) issues.push({ code: 'chart-without-label', params: { columns: shapedNames.filter(one => shaped.get(one)!.type !== 'number') } })
|
|
367
|
-
else if (!shaped.has(label)) issues.push({ code: 'unknown-column', params: { where: 'label', column: label, columns: shapedNames } })
|
|
368
|
-
if (!rows.groupBy && !(view.values ?? []).length) issues.push({ code: 'chart-without-values', params: { columns: shapedNames.filter(one => shaped.get(one)!.type === 'number') } })
|
|
369
|
-
}
|
|
370
|
-
if (view.unit !== undefined && (typeof view.unit !== 'string' || view.unit.length > 20)) issues.push({ code: 'unit-too-long' })
|
|
371
|
-
return issues
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/* ── What the card says it shows, against what it reads ───────────────────────────────────────────────────── */
|
|
375
|
-
|
|
376
|
-
export type PeriodUnit = 'day' | 'week' | 'month' | 'months' | 'year'
|
|
377
|
-
|
|
378
|
-
/*
|
|
379
|
-
* The periods a card's name or line may say, in the languages the module ships the most - a name the dashboard shows
|
|
380
|
-
* every day. 「이번 달 합계」 with no condition of the month summed all time (2026-10-25).
|
|
381
|
-
*/
|
|
382
|
-
const NAMED_PERIODS: [PeriodUnit, RegExp][] = [
|
|
383
|
-
['months', /(최근|지난)\s*\d+\s*개월|\d+\s*개월\s*(간|동안)|\b(last|past)\s+\d+\s+months?\b|過去\s*\d+\s*(ヶ|か|カ)月|近\s*\d+\s*个月/i],
|
|
384
|
-
['month', /이번\s*달|이달|금월|당월|지난\s*달|저번\s*달|전월|다음\s*달|\b(this|last|next)\s+month\b|今月|先月|本月|上个?月/i],
|
|
385
|
-
['year', /올해|금년|당해|작년|지난\s*해|전년도|내년|\b(this|last|next)\s+year\b|今年|昨年|去年/i],
|
|
386
|
-
['week', /이번\s*주|금주|지난\s*주|저번\s*주|다음\s*주|\b(this|last|next)\s+week\b|今週|先週|本周|上周/i],
|
|
387
|
-
['day', /오늘|금일|\btoday\b|今日|今天/i]
|
|
388
|
-
]
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* Words that say when a state is read, never a period it covers - 「내 퇴직금 (오늘 기준)」 is the estimate as it stands,
|
|
392
|
-
* and was refused as 「today」 with no condition of it (2026-10-25).
|
|
393
|
-
*/
|
|
394
|
-
const AS_OF = /(오늘|금일|현재)\s*(기준|시점)|\bas of (today|now)\b|今日(時点|現在)|截至(今天|目前)/gi
|
|
395
|
-
|
|
396
|
-
/** The periods a text says - none for a text without one. */
|
|
397
|
-
export function periodsNamed(text: string | undefined): PeriodUnit[] {
|
|
398
|
-
const given = String(text ?? '').replace(AS_OF, ' ')
|
|
399
|
-
return NAMED_PERIODS.filter(([, words]) => words.test(given)).map(([unit]) => unit)
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/** The units a relative value reads - `this-month-start` a month, `-5m` months back, `-30d` days. */
|
|
403
|
-
function unitsOf(value: unknown): PeriodUnit[] {
|
|
404
|
-
if (typeof value !== 'string') return []
|
|
405
|
-
if (/^-\d{1,2}m(-(start|end))?$/.test(value)) return ['months']
|
|
406
|
-
if (/^today$|^[+-]?\d{1,4}d$/.test(value)) return /^[+-]?([7-9]|1[0-4])d$/.test(value) ? ['day', 'week'] : ['day']
|
|
407
|
-
const unit = /^(this|last|next)-(week|month|quarter|year)/.exec(value)?.[2]
|
|
408
|
-
return unit === 'quarter' ? ['months'] : unit ? [unit as PeriodUnit] : []
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
const FIXED_DAY = /^\d{4}(-\d{2}(-\d{2})?)?$/
|
|
412
|
-
/** A bound held to one day, month or year - `2026-10-01`, `2026-10`, or an integer year or month (`year: 2026, month: 10`). */
|
|
413
|
-
const isFixed = (value: unknown) => typeof value === 'number' || (typeof value === 'string' && FIXED_DAY.test(value))
|
|
414
|
-
|
|
415
|
-
/** The viewer as the definition may name them by mistake - their user id, name and e-mail. */
|
|
416
|
-
export interface ViewerNames {
|
|
417
|
-
id?: string
|
|
418
|
-
name?: string
|
|
419
|
-
email?: string
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Whether a card means what its name says, and reads as whoever looks at it. Issues, never repairs:
|
|
424
|
-
* - a period in the name or line (이번 달, 최근 6개월) with no condition of that period - it would sum all time;
|
|
425
|
-
* - a period said relatively but held to fixed days - it would stop being "this month" next month;
|
|
426
|
-
* - a filter on the viewer's own name or id: a read of the viewer's own rows needs none (it is theirs whoever looks),
|
|
427
|
-
* and another read filtered by one person shows that person to everyone the card is shared with - the viewer's own
|
|
428
|
-
* read (`mine`) is the source instead.
|
|
429
|
-
* `mineSources` are the reads of one's own rows the model may use instead (cardSources).
|
|
430
|
-
*/
|
|
431
|
-
export function checkCardIntent(
|
|
432
|
-
card: { name?: string; description?: string; definition: CardDefinition },
|
|
433
|
-
source: CardSource,
|
|
434
|
-
viewer: ViewerNames = {},
|
|
435
|
-
mineSources: string[] = []
|
|
436
|
-
): CardIssue[] {
|
|
437
|
-
const issues: CardIssue[] = []
|
|
438
|
-
const { definition } = card
|
|
439
|
-
const where = definition.source?.where ?? {}
|
|
440
|
-
const matches = definition.rows?.match ?? []
|
|
441
|
-
const dateColumns = new Set(source.columns.filter(one => one.type === 'date').map(one => one.name))
|
|
442
|
-
const periodConditions = new Set(source.conditions.filter(one => one.type === 'date' || one.format === 'month' || (one.type === 'integer' && /year|month/i.test(one.name))).map(one => one.name))
|
|
443
|
-
|
|
444
|
-
/* The values that bound a period - a period condition's, a date column's match. */
|
|
445
|
-
const bounds = [
|
|
446
|
-
...Object.entries(where).filter(([name]) => periodConditions.has(name)).map(([, value]) => value),
|
|
447
|
-
...matches.filter(one => dateColumns.has(one?.column)).map(one => one.value)
|
|
448
|
-
]
|
|
449
|
-
const named = [...new Set([...periodsNamed(card.name), ...periodsNamed(card.description)])]
|
|
450
|
-
if (named.length) {
|
|
451
|
-
if (!bounds.length) issues.push({ code: 'period-without-condition', params: { said: named, conditions: [...periodConditions], dateColumns: [...dateColumns] } })
|
|
452
|
-
else if (bounds.some(isFixed)) issues.push({ code: 'period-held-to-fixed-days', params: { said: named, given: bounds.filter(isFixed) } })
|
|
453
|
-
else {
|
|
454
|
-
const read = new Set(bounds.flatMap(unitsOf))
|
|
455
|
-
const missing = named.filter(unit => !read.has(unit))
|
|
456
|
-
if (read.size && missing.length) issues.push({ code: 'period-unlike-condition', params: { said: missing, given: bounds } })
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
const own = [viewer.id, viewer.name, viewer.email].filter((one): one is string => !!one && one.trim().length > 1).map(one => one.trim().toLowerCase())
|
|
461
|
-
const isOwn = (value: unknown) => (Array.isArray(value) ? value : [value]).some(one => (typeof one === 'string' || typeof one === 'number') && own.includes(String(one).trim().toLowerCase()))
|
|
462
|
-
const onViewer = [...Object.entries(where).filter(([, value]) => isOwn(value)).map(([name]) => name), ...matches.filter(one => isOwn(one?.value)).map(one => one.column)]
|
|
463
|
-
if (onViewer.length) {
|
|
464
|
-
if (source.mine) issues.push({ code: 'viewer-filter-on-own-read', params: { filters: onViewer } })
|
|
465
|
-
else if (mineSources.length) issues.push({ code: 'viewer-filter-use-own-read', params: { filters: onViewer, mine: mineSources } })
|
|
466
|
-
}
|
|
467
|
-
return issues
|
|
468
|
-
}
|