@things-factory/ai-assistant 10.1.99 → 10.1.101
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/client/components/entry-review.ts +19 -4
- package/client/components/workspace-view.ts +6 -1
- package/client/utils/entry-proposal.ts +12 -2
- package/dist-client/components/entry-review.d.ts +1 -1
- package/dist-client/components/entry-review.js +21 -5
- package/dist-client/components/entry-review.js.map +1 -1
- package/dist-client/components/workspace-view.d.ts +1 -1
- package/dist-client/components/workspace-view.js +7 -2
- package/dist-client/components/workspace-view.js.map +1 -1
- package/dist-client/tsconfig.tsbuildinfo +1 -1
- package/dist-client/utils/entry-proposal.d.ts +2 -0
- package/dist-client/utils/entry-proposal.js +11 -1
- package/dist-client/utils/entry-proposal.js.map +1 -1
- package/dist-server/service/data-entry/entry-apply.js +27 -1
- package/dist-server/service/data-entry/entry-apply.js.map +1 -1
- package/dist-server/service/data-entry/entry-target.d.ts +69 -6
- package/dist-server/service/data-entry/entry-target.js +159 -12
- package/dist-server/service/data-entry/entry-target.js.map +1 -1
- package/dist-server/service/data-entry/entry-tools.d.ts +13 -1
- package/dist-server/service/data-entry/entry-tools.js +27 -10
- package/dist-server/service/data-entry/entry-tools.js.map +1 -1
- package/dist-server/service/data-entry/entry-update.js +3 -3
- package/dist-server/service/data-entry/entry-update.js.map +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/server/service/data-entry/data-entry-update.test.ts +107 -1
- package/server/service/data-entry/data-entry.test.ts +41 -4
- package/server/service/data-entry/entry-apply.ts +26 -3
- package/server/service/data-entry/entry-target.ts +190 -17
- package/server/service/data-entry/entry-tools.ts +29 -10
- package/server/service/data-entry/entry-update.ts +4 -3
- package/test/entry-proposal.test.ts +11 -0
- package/translations/en.json +9 -1
- package/translations/ja.json +9 -1
- package/translations/ko.json +9 -1
- package/translations/ms.json +9 -1
- package/translations/zh.json +9 -1
|
@@ -12,6 +12,7 @@ export interface EntryRowView {
|
|
|
12
12
|
sources: {
|
|
13
13
|
attachmentId?: string;
|
|
14
14
|
where?: string;
|
|
15
|
+
person?: boolean;
|
|
15
16
|
}[];
|
|
16
17
|
issues: EntryIssueView[];
|
|
17
18
|
/** The server found this row's numbers and dates together on this line of the cited file. */
|
|
@@ -27,6 +28,7 @@ export interface EntryTotalView {
|
|
|
27
28
|
declared?: number;
|
|
28
29
|
declaredFound?: boolean;
|
|
29
30
|
difference?: number;
|
|
31
|
+
declaredBy?: 'model' | 'file';
|
|
30
32
|
}
|
|
31
33
|
export interface EntryRowResult {
|
|
32
34
|
index: number;
|
|
@@ -2,7 +2,17 @@ export function isEntryProposal(proposal) {
|
|
|
2
2
|
return proposal?.kind === 'entry' && Array.isArray(proposal?.rows);
|
|
3
3
|
}
|
|
4
4
|
export function entryReadyCount(proposal) {
|
|
5
|
-
|
|
5
|
+
const rows = proposal?.rows ?? [];
|
|
6
|
+
const blocked = (row) => row.issues?.some(issue => issue.blocking);
|
|
7
|
+
if (!proposal?.groupBy)
|
|
8
|
+
return rows.filter(row => !blocked(row)).length;
|
|
9
|
+
/* Lines of one record go in together - a record with a line kept out is kept out whole, and its lines do not count. */
|
|
10
|
+
const keyOf = (row) => {
|
|
11
|
+
const value = row.values?.[proposal.groupBy];
|
|
12
|
+
return value === undefined || value === null || value === '' ? `row-${row.index}` : String(typeof value === 'object' ? value.id ?? value.text : value);
|
|
13
|
+
};
|
|
14
|
+
const out = new Set(rows.filter(blocked).map(keyOf));
|
|
15
|
+
return rows.filter(row => !out.has(keyOf(row))).length;
|
|
6
16
|
}
|
|
7
17
|
/** The i18n key of an issue - the target's own, or the framework's by code. */
|
|
8
18
|
export function entryIssueKey(issue) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry-proposal.js","sourceRoot":"","sources":["../../client/utils/entry-proposal.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"entry-proposal.js","sourceRoot":"","sources":["../../client/utils/entry-proposal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AC,EAAE,CAAC;YAC5D,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,CAAC;YAC5B,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,CAAC;YAC1B,IAAI,EAAE,gBAAgB,CAAC,MAAM,EAAE,CAAC,CAAC;SAClC,CAAA;IACH,CAAC,CAAA;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,gBAAyB;IACvD,MAAM,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAA;IACxC,KAAK,MAAM,IAAI,IAAI,gBAAgB;QAAE,KAAK,MAAM,GAAG,IAAI,IAAI,IAAI,EAAE;YAAE,IAAI,eAAe,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,GAAG;gBAAE,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,CAAA;IAC9I,OAAO,MAAM,CAAA;AACf,CAAC;AAED,yGAAyG;AACzG,MAAM,UAAU,YAAY,CAAC,QAAa;IACxC,OAAO,GAAG,QAAQ,EAAE,MAAM,IAAI,QAAQ,EAAE,EAAE,IAAI,QAAQ,EAAE,CAAA;AAC1D,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,QAAa;IAC5C,OAAO,eAAe,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,EAAE,KAAK,QAAQ,CAAA;AAC9D,CAAC","sourcesContent":["/*\n * The data entry card's rules and its handler (operato-application/design/ai/data-entry.md §5).\n *\n * The card is a review, not an editor: a correction is said in the conversation and the model proposes\n * again - the newer card replaces the older one. Pressing asks the server to enter the stored rows; the\n * screen sends no rows of its own.\n */\nimport type { ProposalContext, ProposalOutcome } from './assistant-proposals.js'\n\nexport interface EntryIssueView {\n field?: string\n code: string\n messageKey?: string\n params?: Record<string, unknown>\n blocking: boolean\n}\n\nexport interface EntryRowView {\n index: number\n values: Record<string, unknown>\n sources: { attachmentId?: string; where?: string; person?: boolean }[]\n issues: EntryIssueView[]\n /** The server found this row's numbers and dates together on this line of the cited file. */\n evidence?: { attachmentId: string; line: number }\n}\n\nexport interface EntryTotalView {\n field: string\n all: number\n ready: number\n declared?: number\n declaredFound?: boolean\n difference?: number\n declaredBy?: 'model' | 'file'\n}\n\nexport interface EntryRowResult {\n index: number\n ok: boolean\n id?: string\n label?: string\n error?: string\n}\n\nexport interface EntryApplyAnswer extends ProposalOutcome {\n rows?: EntryRowResult[]\n /** Set by the handler from the card - which words the conversation line uses. */\n op?: string\n}\n\nexport function isEntryProposal(proposal: any): boolean {\n return proposal?.kind === 'entry' && Array.isArray(proposal?.rows)\n}\n\nexport function entryReadyCount(proposal: any): number {\n const rows: EntryRowView[] = proposal?.rows ?? []\n const blocked = (row: EntryRowView) => row.issues?.some(issue => issue.blocking)\n if (!proposal?.groupBy) return rows.filter(row => !blocked(row)).length\n /* Lines of one record go in together - a record with a line kept out is kept out whole, and its lines do not count. */\n const keyOf = (row: EntryRowView) => {\n const value: any = row.values?.[proposal.groupBy]\n return value === undefined || value === null || value === '' ? `row-${row.index}` : String(typeof value === 'object' ? value.id ?? value.text : value)\n }\n const out = new Set(rows.filter(blocked).map(keyOf))\n return rows.filter(row => !out.has(keyOf(row))).length\n}\n\n/** The i18n key of an issue - the target's own, or the framework's by code. */\nexport function entryIssueKey(issue: EntryIssueView): string {\n return issue.messageKey || `ai-assistant.entry.issue.${issue.code}`\n}\n\n/** The issues of one cell, and of the row as a whole (no field). */\nexport function entryCellIssues(row: EntryRowView, field?: string): EntryIssueView[] {\n return (row.issues ?? []).filter(issue => (field ? issue.field === field : !issue.field))\n}\n\n/** What stays in the conversation - the model reads it on its next turn, and the person sees what went in. */\nexport function entryOutcomeNote(answer: EntryApplyAnswer, t: (key: string, options?: any) => string): string {\n const refused = (answer.rows ?? []).filter(row => !row.ok)\n const head = answer.op === 'update'\n ? answer.applied\n ? t('ai-assistant.entry.changed', { defaultValue: 'changed {count} records.', count: answer.applied })\n : t('ai-assistant.entry.changed-none', { defaultValue: 'no record was changed.' })\n : answer.applied\n ? t('ai-assistant.entry.entered', { defaultValue: 'entered {count} rows.', count: answer.applied })\n : t('ai-assistant.entry.entered-none', { defaultValue: 'no row was entered.' })\n const lines = refused.slice(0, 20).map(row => t('ai-assistant.entry.row-refused', { defaultValue: 'row {n}: {reason}', n: row.index + 1, reason: row.error || '' }))\n const more = refused.length > 20 ? [t('ai-assistant.entry.more-refused', { defaultValue: '{count} more rows were not entered.', count: refused.length - 20 })] : []\n return [head, ...lines, ...more].join('\\n')\n}\n\n/** The handler every entry card runs - built with the mutation so the rules are tested without a server. */\nexport function entryProposalHandler(\n apply: (sessionId: string, proposalKey: string) => Promise<EntryApplyAnswer>,\n t: (key: string, options?: any) => string\n) {\n return async (proposal: any, context: ProposalContext): Promise<ProposalOutcome> => {\n if (!context.sessionId || !proposal?.key) return { verdict: 'unavailable', errorCode: 'no-conversation' }\n const answer = { ...(await apply(context.sessionId, proposal.key)), op: proposal.op }\n return {\n verdict: answer.verdict,\n ...(answer.errorCode ? { errorCode: answer.errorCode } : {}),\n applied: answer.applied ?? 0,\n missed: answer.missed ?? 0,\n note: entryOutcomeNote(answer, t)\n }\n }\n}\n\n/**\n * The newest card per record, over the conversation's proposals in order - an older one is replaced and\n * cannot be entered (the server refuses it as `superseded`; the card says so before anyone presses).\n */\nexport function latestEntryKeys(proposalsInOrder: any[][]): Map<string, string> {\n const latest = new Map<string, string>()\n for (const list of proposalsInOrder) for (const one of list ?? []) if (isEntryProposal(one) && one.key) latest.set(entryGroupOf(one), one.key)\n return latest\n}\n\n/** A 입력안 and a 수정안 of one record are counted apart - a newer one of the same kind replaces the older. */\nexport function entryGroupOf(proposal: any): string {\n return `${proposal?.target}:${proposal?.op ?? 'create'}`\n}\n\nexport function isChangeProposal(proposal: any): boolean {\n return isEntryProposal(proposal) && proposal.op === 'update'\n}\n"]}
|
|
@@ -57,6 +57,8 @@ async function applyEntryProposal(key, deps, targetOf = entry_target_js_1.entryT
|
|
|
57
57
|
}
|
|
58
58
|
/* Read again from the stored values - the same rules, and today's records for the target's checks. */
|
|
59
59
|
let { rows } = (0, entry_target_js_1.normalizeEntryRows)(target, (proposal.rows ?? []).map((row) => ({ values: row.values, sources: row.sources })));
|
|
60
|
+
/* A reference that was not settled on the card is asked again - never entered as a name. */
|
|
61
|
+
rows = await (0, entry_target_js_1.resolveReferences)(target.fields, rows, deps.execute);
|
|
60
62
|
if (target.check) {
|
|
61
63
|
try {
|
|
62
64
|
rows = (0, entry_target_js_1.withCheckedIssues)(rows, await target.check(rows, { state: deps.state }));
|
|
@@ -67,7 +69,31 @@ async function applyEntryProposal(key, deps, targetOf = entry_target_js_1.entryT
|
|
|
67
69
|
}
|
|
68
70
|
}
|
|
69
71
|
const results = [];
|
|
70
|
-
|
|
72
|
+
if (target.group) {
|
|
73
|
+
/* One call per group; a group with a row kept out stays out whole - an order missing a line is not entered. */
|
|
74
|
+
for (const [, members] of (0, entry_target_js_1.entryGroups)(rows, target.group.by)) {
|
|
75
|
+
const blocked = members.find(entry_target_js_1.isBlocked);
|
|
76
|
+
if (blocked) {
|
|
77
|
+
const issue = blocked.issues.find(one => one.blocking);
|
|
78
|
+
for (const row of members)
|
|
79
|
+
results.push({ index: row.index, ok: false, error: row === blocked ? (issue.field ? `${issue.field}: ${issue.code}` : issue.code) : `group-kept-out (row ${blocked.index + 1})` });
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const answer = await deps.execute(target.mutation, target.group.variables(members));
|
|
84
|
+
const error = answer.errors?.length ? answer.errors.map(one => one.message).join('; ') || 'refused' : undefined;
|
|
85
|
+
const applied = error ? undefined : target.group.appliedOf?.(answer.data, members);
|
|
86
|
+
for (const row of members)
|
|
87
|
+
results.push(error ? { index: row.index, ok: false, error } : { index: row.index, ok: true, ...(applied?.id ? { id: applied.id } : {}), ...(applied?.label ? { label: applied.label } : {}) });
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
for (const row of members)
|
|
91
|
+
results.push({ index: row.index, ok: false, error: error?.message || 'refused' });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
results.sort((a, b) => a.index - b.index);
|
|
95
|
+
}
|
|
96
|
+
for (const row of target.group ? [] : rows) {
|
|
71
97
|
if ((0, entry_target_js_1.isBlocked)(row)) {
|
|
72
98
|
const issue = row.issues.find(one => one.blocking);
|
|
73
99
|
results.push({ index: row.index, ok: false, error: issue.field ? `${issue.field}: ${issue.code}` : issue.code });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry-apply.js","sourceRoot":"","sources":["../../../server/service/data-entry/entry-apply.ts"],"names":[],"mappings":";;AAuDA,8CAYC;AAED,gDAyDC;AAGD,sDAQC;AAzID;;;;;;;;;;;GAWG;AACH,6EAAgG;AAChG,uDAAgF;AAChF,uDAA+M;AAuC/M,MAAM,OAAO,GAAG,CAAC,OAAqC,EAAE,SAAiB,EAAqB,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAA;AAE1J,SAAgB,iBAAiB,CAAC,QAAiC,EAAE,GAAW;IAC9E,IAAI,EAAE,GAAG,CAAC,CAAC,CAAA;IACX,IAAI,QAAa,CAAA;IACjB,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,EAAE;QAC9B,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,SAAS,IAAI,EAAE;YAAE,IAAI,GAAG,EAAE,GAAG,KAAK,GAAG;gBAAE,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,QAAQ,GAAG,GAAG,CAAC,CAAA;IAC7F,CAAC,CAAC,CAAA;IACF,IAAI,CAAC,QAAQ;QAAE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,CAAA;IAC3C,4HAA4H;IAC5H,MAAM,UAAU,GAAG,QAAQ;SACxB,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C,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,IAAI,SAAS,EAAE,CAAC,CAAA;QACnF,CAAC;IACH,CAAC;IAED,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,MAAM,CAAA;IACpD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,OAAO,CAAA;IACvC,MAAM,IAAA,kCAAgB,EACpB,MAAM,EACN,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE,EAAE,EAAE,OAAO,GAAG,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,gBAAgB,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,GAAG,EAAE,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAC5L,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CACtB,CAAA;IACD,OAAO,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAA;AAChK,CAAC","sourcesContent":["/*\n * Writing a proposal the person pressed (operato-application/design/ai/data-entry.md §4).\n *\n * The rows written are the **stored** proposal's, never rows the screen sends: what the person reviewed\n * on the card is what goes in. The server asks everything again - the door, whether a newer card replaced\n * this one, whether it was already answered, the target's checks (records move between proposing and\n * pressing) - and enters each row by running the application's own mutation with the person's request\n * state. Each call is its own request (the door's `@transaction`), so one refused row does not undo the\n * others and each is reported by its place.\n *\n * Dependencies come in as arguments so the rules are tested without a database.\n */\nimport { PROPOSAL_OUTCOME_PREFIX, PROPOSAL_VERDICTS } from '../chat-message/proposal-outcome.js'\nimport { readRecords, stillAsProposed, type ChangeRow } from './entry-update.js'\nimport { changeToolName, entryDoorOf, entryTargetOf, entryToolName, rootFieldOf, isBlocked, recordEntryEvent, normalizeEntryRows, withCheckedIssues, type EntryRow, type EntryTarget } from './entry-target.js'\n\nexport interface StoredProposalMessage {\n id: string\n proposals: any[]\n}\n\nexport interface EntryRowResult {\n index: number\n ok: boolean\n id?: string\n label?: string\n /** A blocking issue kept it out, or the target refused it. */\n error?: string\n}\n\nexport interface EntryApplyOutcome {\n verdict: 'accepted' | 'refused' | 'unavailable'\n errorCode?: string\n applied: number\n missed: number\n rows: EntryRowResult[]\n}\n\nexport interface EntryApplyDeps {\n /** The conversation's messages that carry proposals, oldest first. */\n messages: StoredProposalMessage[]\n /** Whether an outcome line already answers this key. */\n answered: boolean\n passesDoor(door: string): Promise<boolean>\n /**\n * Runs one of the application's queries or mutations with the person's request state - its API, its directives\n * and its own checks. Answers what GraphQL answers.\n */\n execute(document: string, variables: Record<string, unknown>): Promise<{ data?: any; errors?: readonly { message: string }[] }>\n state: any\n report?(message: string, error: unknown): void\n}\n\nconst nothing = (verdict: EntryApplyOutcome['verdict'], errorCode: string): EntryApplyOutcome => ({ verdict, errorCode, applied: 0, missed: 0, rows: [] })\n\nexport function findEntryProposal(messages: StoredProposalMessage[], key: string): { proposal?: any; superseded: boolean } {\n let at = -1\n let proposal: any\n messages.forEach((message, i) => {\n for (const one of message.proposals ?? []) if (one?.key === key) (at = i), (proposal = one)\n })\n if (!proposal) return { superseded: false }\n /* A later card of the same record and the same work replaces this one - pressing both would do the corrected work twice. */\n const superseded = messages\n .slice(at + 1)\n .some(message => (message.proposals ?? []).some(one => one?.kind === 'entry' && one?.target === proposal.target && (one?.op ?? 'create') === (proposal.op ?? 'create')))\n return { proposal, superseded }\n}\n\nexport async function applyEntryProposal(key: string, deps: EntryApplyDeps, targetOf: (key: string) => EntryTarget | undefined = entryTargetOf): Promise<EntryApplyOutcome> {\n const { proposal, superseded } = findEntryProposal(deps.messages, key)\n if (!proposal) return nothing('refused', 'proposal-not-found')\n if (proposal.kind !== 'entry') return nothing('refused', 'not-an-entry-proposal')\n if (superseded) return nothing('refused', 'superseded')\n if (deps.answered) return nothing('refused', 'already-answered')\n\n const target = targetOf(proposal.target)\n if (!target) return nothing('unavailable', 'unknown-entry-target')\n if (proposal.op === 'update') return applyChanges(key, proposal, target, deps)\n const door = entryDoorOf(target.mutation)\n if (!(await deps.passesDoor(door))) {\n await recordEntryEvent(target, { phase: 'apply', tool: entryToolName(target.key), rows: 0, ok: false, error: 'door-closed', detail: { proposalKey: key } }, { state: deps.state })\n return nothing('refused', 'door-closed')\n }\n\n /* Read again from the stored values - the same rules, and today's records for the target's checks. */\n let { rows } = normalizeEntryRows(target, (proposal.rows ?? []).map((row: EntryRow) => ({ values: row.values, sources: row.sources })))\n if (target.check) {\n try {\n rows = withCheckedIssues(rows, await target.check(rows, { state: deps.state }))\n } catch (error) {\n deps.report?.(`[ai-assistant] entry check of '${target.key}' failed`, error)\n return nothing('refused', 'check-failed')\n }\n }\n\n const results: EntryRowResult[] = []\n for (const row of rows) {\n if (isBlocked(row)) {\n const issue = row.issues.find(one => one.blocking)!\n results.push({ index: row.index, ok: false, error: issue.field ? `${issue.field}: ${issue.code}` : issue.code })\n continue\n }\n try {\n const answer = await deps.execute(target.mutation, target.variables(row))\n if (answer.errors?.length) {\n results.push({ index: row.index, ok: false, error: answer.errors.map(one => one.message).join('; ') || 'refused' })\n continue\n }\n const applied = target.appliedOf?.(answer.data, row)\n results.push({ index: row.index, ok: true, ...(applied?.id ? { id: applied.id } : {}), ...(applied?.label ? { label: applied.label } : {}) })\n } catch (error: any) {\n results.push({ index: row.index, ok: false, error: error?.message || 'refused' })\n }\n }\n\n const applied = results.filter(one => one.ok).length\n const missed = results.length - applied\n await recordEntryEvent(\n target,\n { phase: 'apply', tool: entryToolName(target.key), rows: results.length, ok: applied > 0, applied, missed, ...(applied ? {} : { error: 'no-row-entered' }), detail: { proposalKey: key, rows: results } },\n { state: deps.state }\n )\n return applied\n ? { verdict: 'accepted', applied, missed, rows: results }\n : { verdict: 'refused', errorCode: 'no-row-entered', applied, missed, rows: results }\n}\n\n/** An outcome line already answers this key - `requested` alone does not: the dock writes it just before calling here. */\nexport function entryProposalAnswered(key: string, lines: readonly (string | null | undefined)[]): boolean {\n const answered = `${PROPOSAL_OUTCOME_PREFIX}${key}: `\n return lines.some(line => {\n const text = (line ?? '').trim()\n if (!text.startsWith(answered)) return false\n const rest = text.slice(answered.length)\n return PROPOSAL_VERDICTS.some(verdict => rest === verdict || rest.startsWith(`${verdict} `))\n })\n}\n\n/**\n * A pressed 수정안: the doors again, each record read again as the person, a field that moved since the card was\n * made keeps its row out (`changed-since`), and each record is changed by the application's own mutation.\n */\nasync function applyChanges(key: string, proposal: any, target: EntryTarget, deps: EntryApplyDeps): Promise<EntryApplyOutcome> {\n const tool = changeToolName(target.key)\n const update = target.update\n if (!update) return nothing('unavailable', 'target-does-not-change')\n for (const door of [rootFieldOf(update.mutation, 'mutation'), rootFieldOf(update.read.query, 'query')]) {\n if (!(await deps.passesDoor(door))) {\n await recordEntryEvent(target, { phase: 'apply', tool, rows: 0, ok: false, error: 'door-closed', detail: { proposalKey: key } }, { state: deps.state })\n return nothing('refused', 'door-closed')\n }\n }\n\n const stored: ChangeRow[] = proposal.rows ?? []\n let now\n try {\n now = await readRecords(target, stored.map(row => row.id), deps.execute)\n } catch (error) {\n deps.report?.(`[ai-assistant] reading '${target.key}' again failed`, error)\n return nothing('refused', 'read-failed')\n }\n\n const results: EntryRowResult[] = []\n for (const row of stillAsProposed(target, stored, now)) {\n if (row.issues.some(one => one.blocking)) {\n const issue = row.issues.find(one => one.blocking)!\n results.push({ index: row.index, ok: false, error: issue.field ? `${issue.field}: ${issue.code}` : issue.code })\n continue\n }\n try {\n const answer = await deps.execute(update.mutation, update.variables({ id: row.id, record: row.now!, values: row.values }))\n if (answer.errors?.length) {\n results.push({ index: row.index, ok: false, error: answer.errors.map(one => one.message).join('; ') || 'refused' })\n continue\n }\n results.push({ index: row.index, ok: true, id: row.id, ...(row.label ? { label: row.label } : {}) })\n } catch (error: any) {\n results.push({ index: row.index, ok: false, error: error?.message || 'refused' })\n }\n }\n\n const applied = results.filter(one => one.ok).length\n const missed = results.length - applied\n await recordEntryEvent(\n target,\n { phase: 'apply', tool, rows: results.length, ok: applied > 0, applied, missed, ...(applied ? {} : { error: 'no-row-changed' }), detail: { proposalKey: key, op: 'update', rows: results } },\n { state: deps.state }\n )\n return applied ? { verdict: 'accepted', applied, missed, rows: results } : { verdict: 'refused', errorCode: 'no-row-changed', applied, missed, rows: results }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"entry-apply.js","sourceRoot":"","sources":["../../../server/service/data-entry/entry-apply.ts"],"names":[],"mappings":";;AAuDA,8CAYC;AAED,gDAgFC;AAGD,sDAQC;AAhKD;;;;;;;;;;;GAWG;AACH,6EAAgG;AAChG,uDAAgF;AAChF,uDAA+O;AAuC/O,MAAM,OAAO,GAAG,CAAC,OAAqC,EAAE,SAAiB,EAAqB,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAA;AAE1J,SAAgB,iBAAiB,CAAC,QAAiC,EAAE,GAAW;IAC9E,IAAI,EAAE,GAAG,CAAC,CAAC,CAAA;IACX,IAAI,QAAa,CAAA;IACjB,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,EAAE;QAC9B,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,SAAS,IAAI,EAAE;YAAE,IAAI,GAAG,EAAE,GAAG,KAAK,GAAG;gBAAE,CAAC,EAAE,GAAG,C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Writing a proposal the person pressed (operato-application/design/ai/data-entry.md §4).\n *\n * The rows written are the **stored** proposal's, never rows the screen sends: what the person reviewed\n * on the card is what goes in. The server asks everything again - the door, whether a newer card replaced\n * this one, whether it was already answered, the target's checks (records move between proposing and\n * pressing) - and enters each row by running the application's own mutation with the person's request\n * state. Each call is its own request (the door's `@transaction`), so one refused row does not undo the\n * others and each is reported by its place.\n *\n * Dependencies come in as arguments so the rules are tested without a database.\n */\nimport { PROPOSAL_OUTCOME_PREFIX, PROPOSAL_VERDICTS } from '../chat-message/proposal-outcome.js'\nimport { readRecords, stillAsProposed, type ChangeRow } from './entry-update.js'\nimport { changeToolName, entryGroups, resolveReferences, entryDoorOf, entryTargetOf, entryToolName, rootFieldOf, isBlocked, recordEntryEvent, normalizeEntryRows, withCheckedIssues, type EntryRow, type EntryTarget } from './entry-target.js'\n\nexport interface StoredProposalMessage {\n id: string\n proposals: any[]\n}\n\nexport interface EntryRowResult {\n index: number\n ok: boolean\n id?: string\n label?: string\n /** A blocking issue kept it out, or the target refused it. */\n error?: string\n}\n\nexport interface EntryApplyOutcome {\n verdict: 'accepted' | 'refused' | 'unavailable'\n errorCode?: string\n applied: number\n missed: number\n rows: EntryRowResult[]\n}\n\nexport interface EntryApplyDeps {\n /** The conversation's messages that carry proposals, oldest first. */\n messages: StoredProposalMessage[]\n /** Whether an outcome line already answers this key. */\n answered: boolean\n passesDoor(door: string): Promise<boolean>\n /**\n * Runs one of the application's queries or mutations with the person's request state - its API, its directives\n * and its own checks. Answers what GraphQL answers.\n */\n execute(document: string, variables: Record<string, unknown>): Promise<{ data?: any; errors?: readonly { message: string }[] }>\n state: any\n report?(message: string, error: unknown): void\n}\n\nconst nothing = (verdict: EntryApplyOutcome['verdict'], errorCode: string): EntryApplyOutcome => ({ verdict, errorCode, applied: 0, missed: 0, rows: [] })\n\nexport function findEntryProposal(messages: StoredProposalMessage[], key: string): { proposal?: any; superseded: boolean } {\n let at = -1\n let proposal: any\n messages.forEach((message, i) => {\n for (const one of message.proposals ?? []) if (one?.key === key) (at = i), (proposal = one)\n })\n if (!proposal) return { superseded: false }\n /* A later card of the same record and the same work replaces this one - pressing both would do the corrected work twice. */\n const superseded = messages\n .slice(at + 1)\n .some(message => (message.proposals ?? []).some(one => one?.kind === 'entry' && one?.target === proposal.target && (one?.op ?? 'create') === (proposal.op ?? 'create')))\n return { proposal, superseded }\n}\n\nexport async function applyEntryProposal(key: string, deps: EntryApplyDeps, targetOf: (key: string) => EntryTarget | undefined = entryTargetOf): Promise<EntryApplyOutcome> {\n const { proposal, superseded } = findEntryProposal(deps.messages, key)\n if (!proposal) return nothing('refused', 'proposal-not-found')\n if (proposal.kind !== 'entry') return nothing('refused', 'not-an-entry-proposal')\n if (superseded) return nothing('refused', 'superseded')\n if (deps.answered) return nothing('refused', 'already-answered')\n\n const target = targetOf(proposal.target)\n if (!target) return nothing('unavailable', 'unknown-entry-target')\n if (proposal.op === 'update') return applyChanges(key, proposal, target, deps)\n const door = entryDoorOf(target.mutation)\n if (!(await deps.passesDoor(door))) {\n await recordEntryEvent(target, { phase: 'apply', tool: entryToolName(target.key), rows: 0, ok: false, error: 'door-closed', detail: { proposalKey: key } }, { state: deps.state })\n return nothing('refused', 'door-closed')\n }\n\n /* Read again from the stored values - the same rules, and today's records for the target's checks. */\n let { rows } = normalizeEntryRows(target, (proposal.rows ?? []).map((row: EntryRow) => ({ values: row.values, sources: row.sources })))\n /* A reference that was not settled on the card is asked again - never entered as a name. */\n rows = await resolveReferences(target.fields, rows, deps.execute)\n if (target.check) {\n try {\n rows = withCheckedIssues(rows, await target.check(rows, { state: deps.state }))\n } catch (error) {\n deps.report?.(`[ai-assistant] entry check of '${target.key}' failed`, error)\n return nothing('refused', 'check-failed')\n }\n }\n\n const results: EntryRowResult[] = []\n if (target.group) {\n /* One call per group; a group with a row kept out stays out whole - an order missing a line is not entered. */\n for (const [, members] of entryGroups(rows, target.group.by)) {\n const blocked = members.find(isBlocked)\n if (blocked) {\n const issue = blocked.issues.find(one => one.blocking)!\n for (const row of members)\n results.push({ index: row.index, ok: false, error: row === blocked ? (issue.field ? `${issue.field}: ${issue.code}` : issue.code) : `group-kept-out (row ${blocked.index + 1})` })\n continue\n }\n try {\n const answer = await deps.execute(target.mutation, target.group.variables(members))\n const error = answer.errors?.length ? answer.errors.map(one => one.message).join('; ') || 'refused' : undefined\n const applied = error ? undefined : target.group.appliedOf?.(answer.data, members)\n for (const row of members) results.push(error ? { index: row.index, ok: false, error } : { index: row.index, ok: true, ...(applied?.id ? { id: applied.id } : {}), ...(applied?.label ? { label: applied.label } : {}) })\n } catch (error: any) {\n for (const row of members) results.push({ index: row.index, ok: false, error: error?.message || 'refused' })\n }\n }\n results.sort((a, b) => a.index - b.index)\n }\n for (const row of target.group ? [] : rows) {\n if (isBlocked(row)) {\n const issue = row.issues.find(one => one.blocking)!\n results.push({ index: row.index, ok: false, error: issue.field ? `${issue.field}: ${issue.code}` : issue.code })\n continue\n }\n try {\n const answer = await deps.execute(target.mutation, target.variables!(row))\n if (answer.errors?.length) {\n results.push({ index: row.index, ok: false, error: answer.errors.map(one => one.message).join('; ') || 'refused' })\n continue\n }\n const applied = target.appliedOf?.(answer.data, row)\n results.push({ index: row.index, ok: true, ...(applied?.id ? { id: applied.id } : {}), ...(applied?.label ? { label: applied.label } : {}) })\n } catch (error: any) {\n results.push({ index: row.index, ok: false, error: error?.message || 'refused' })\n }\n }\n\n const applied = results.filter(one => one.ok).length\n const missed = results.length - applied\n await recordEntryEvent(\n target,\n { phase: 'apply', tool: entryToolName(target.key), rows: results.length, ok: applied > 0, applied, missed, ...(applied ? {} : { error: 'no-row-entered' }), detail: { proposalKey: key, rows: results } },\n { state: deps.state }\n )\n return applied\n ? { verdict: 'accepted', applied, missed, rows: results }\n : { verdict: 'refused', errorCode: 'no-row-entered', applied, missed, rows: results }\n}\n\n/** An outcome line already answers this key - `requested` alone does not: the dock writes it just before calling here. */\nexport function entryProposalAnswered(key: string, lines: readonly (string | null | undefined)[]): boolean {\n const answered = `${PROPOSAL_OUTCOME_PREFIX}${key}: `\n return lines.some(line => {\n const text = (line ?? '').trim()\n if (!text.startsWith(answered)) return false\n const rest = text.slice(answered.length)\n return PROPOSAL_VERDICTS.some(verdict => rest === verdict || rest.startsWith(`${verdict} `))\n })\n}\n\n/**\n * A pressed 수정안: the doors again, each record read again as the person, a field that moved since the card was\n * made keeps its row out (`changed-since`), and each record is changed by the application's own mutation.\n */\nasync function applyChanges(key: string, proposal: any, target: EntryTarget, deps: EntryApplyDeps): Promise<EntryApplyOutcome> {\n const tool = changeToolName(target.key)\n const update = target.update\n if (!update) return nothing('unavailable', 'target-does-not-change')\n for (const door of [rootFieldOf(update.mutation, 'mutation'), rootFieldOf(update.read.query, 'query')]) {\n if (!(await deps.passesDoor(door))) {\n await recordEntryEvent(target, { phase: 'apply', tool, rows: 0, ok: false, error: 'door-closed', detail: { proposalKey: key } }, { state: deps.state })\n return nothing('refused', 'door-closed')\n }\n }\n\n const stored: ChangeRow[] = proposal.rows ?? []\n let now\n try {\n now = await readRecords(target, stored.map(row => row.id), deps.execute)\n } catch (error) {\n deps.report?.(`[ai-assistant] reading '${target.key}' again failed`, error)\n return nothing('refused', 'read-failed')\n }\n\n const results: EntryRowResult[] = []\n for (const row of stillAsProposed(target, stored, now)) {\n if (row.issues.some(one => one.blocking)) {\n const issue = row.issues.find(one => one.blocking)!\n results.push({ index: row.index, ok: false, error: issue.field ? `${issue.field}: ${issue.code}` : issue.code })\n continue\n }\n try {\n const answer = await deps.execute(update.mutation, update.variables({ id: row.id, record: row.now!, values: row.values }))\n if (answer.errors?.length) {\n results.push({ index: row.index, ok: false, error: answer.errors.map(one => one.message).join('; ') || 'refused' })\n continue\n }\n results.push({ index: row.index, ok: true, id: row.id, ...(row.label ? { label: row.label } : {}) })\n } catch (error: any) {\n results.push({ index: row.index, ok: false, error: error?.message || 'refused' })\n }\n }\n\n const applied = results.filter(one => one.ok).length\n const missed = results.length - applied\n await recordEntryEvent(\n target,\n { phase: 'apply', tool, rows: results.length, ok: applied > 0, applied, missed, ...(applied ? {} : { error: 'no-row-changed' }), detail: { proposalKey: key, op: 'update', rows: results } },\n { state: deps.state }\n )\n return applied ? { verdict: 'accepted', applied, missed, rows: results } : { verdict: 'refused', errorCode: 'no-row-changed', applied, missed, rows: results }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type EntryFieldType = 'string' | 'text' | 'number' | 'integer' | 'date' | 'boolean' | 'enum';
|
|
1
|
+
export type EntryFieldType = 'string' | 'text' | 'number' | 'integer' | 'date' | 'datetime' | 'boolean' | 'enum' | 'reference';
|
|
2
2
|
export interface EntryField {
|
|
3
3
|
name: string;
|
|
4
4
|
type: EntryFieldType;
|
|
@@ -13,12 +13,35 @@ export interface EntryField {
|
|
|
13
13
|
total?: boolean;
|
|
14
14
|
/** An enum's values on the card: `<prefix><value in lowercase>` - `text.expense-payment-` shows PERSONAL_CARD as 개인카드. */
|
|
15
15
|
valueKeyPrefix?: string;
|
|
16
|
+
/**
|
|
17
|
+
* A `reference` field points at another record - a loan, an item, a location. The model knows it by a name or
|
|
18
|
+
* a code; the server finds it through the application's query, as the person, and keeps its id. One match is
|
|
19
|
+
* taken; none or several keep the row out and say so - the server never picks among several.
|
|
20
|
+
*/
|
|
21
|
+
reference?: EntryReference;
|
|
22
|
+
}
|
|
23
|
+
export interface EntryReference {
|
|
24
|
+
query: string;
|
|
25
|
+
/** The query's variables for what the model wrote - "기업은행", "A-100". */
|
|
26
|
+
variables(text: string): Record<string, unknown>;
|
|
27
|
+
/** The records the answer holds that the text names - the application filters, the server does not guess. */
|
|
28
|
+
recordsOf(data: any, text: string): {
|
|
29
|
+
id: string;
|
|
30
|
+
label?: string;
|
|
31
|
+
}[];
|
|
32
|
+
}
|
|
33
|
+
/** A resolved reference, as a row keeps it. */
|
|
34
|
+
export interface ReferenceValue {
|
|
35
|
+
id: string;
|
|
36
|
+
label?: string;
|
|
16
37
|
}
|
|
17
38
|
/** Where a row came from - which file, and where in it. Shown on the card beside the row. */
|
|
18
39
|
export interface EntrySource {
|
|
19
40
|
attachmentId?: string;
|
|
20
|
-
/** "row 3", "the receipt photo"
|
|
41
|
+
/** "row 3", "the receipt photo" - the model's words, in the person's language. */
|
|
21
42
|
where?: string;
|
|
43
|
+
/** The value came from what the person said in the conversation - shown in the screen's own words, never the model's. */
|
|
44
|
+
person?: boolean;
|
|
22
45
|
}
|
|
23
46
|
export interface EntryIssue {
|
|
24
47
|
/** The field it is about. Absent: the row as a whole. */
|
|
@@ -83,10 +106,20 @@ export interface EntryTarget {
|
|
|
83
106
|
* to a person typing it in.
|
|
84
107
|
*/
|
|
85
108
|
mutation: string;
|
|
86
|
-
/** The mutation's variables for one row. */
|
|
87
|
-
variables(row: EntryRow): Record<string, unknown>;
|
|
109
|
+
/** The mutation's variables for one row. Absent when the target enters rows by group (below). */
|
|
110
|
+
variables?(row: EntryRow): Record<string, unknown>;
|
|
88
111
|
/** What the conversation says of an entered row - its id and a short name. */
|
|
89
112
|
appliedOf?(data: any, row: EntryRow): EntryApplied | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* A record with lines - one order, several item lines. Rows that share the value of `by` (the order number) are one
|
|
115
|
+
* record, entered by one call of the mutation with `variables(rows)`. If any row of a group is kept out, the whole
|
|
116
|
+
* group is: an order missing a line must not go in unseen.
|
|
117
|
+
*/
|
|
118
|
+
group?: {
|
|
119
|
+
by: string;
|
|
120
|
+
variables(rows: EntryRow[]): Record<string, unknown>;
|
|
121
|
+
appliedOf?(data: any, rows: EntryRow[]): EntryApplied | undefined;
|
|
122
|
+
};
|
|
90
123
|
/** At most this many rows in one proposal. Default 200. */
|
|
91
124
|
maxRows?: number;
|
|
92
125
|
/**
|
|
@@ -116,13 +149,13 @@ export interface EntryUpdate {
|
|
|
116
149
|
read: {
|
|
117
150
|
query: string;
|
|
118
151
|
variables(ids: string[]): Record<string, unknown>;
|
|
119
|
-
recordsOf(data: any): EntryRecord[];
|
|
152
|
+
recordsOf(data: any, ids: string[]): EntryRecord[];
|
|
120
153
|
};
|
|
121
154
|
/** Finds candidates from what the person said - "last week's 12,400 taxi". */
|
|
122
155
|
find?: {
|
|
123
156
|
query: string;
|
|
124
157
|
variables(criteria: Record<string, unknown>): Record<string, unknown>;
|
|
125
|
-
recordsOf(data: any): EntryRecord[];
|
|
158
|
+
recordsOf(data: any, criteria: Record<string, unknown>): EntryRecord[];
|
|
126
159
|
criteria: EntryField[];
|
|
127
160
|
};
|
|
128
161
|
/** The application's own mutation that changes one record. */
|
|
@@ -191,6 +224,14 @@ export declare function lineHas(line: string, forms: string[]): boolean;
|
|
|
191
224
|
* an id that is not there was an image, or could not be opened.
|
|
192
225
|
*/
|
|
193
226
|
export declare function verifyRowsAgainstSources(rows: EntryRow[], fields: EntryField[], texts: Map<string, string>): EntryRow[];
|
|
227
|
+
/**
|
|
228
|
+
* The total a file prints - a line named 합계 · 총계 · 총액 · total · sum, and the last amount on it. The server
|
|
229
|
+
* reads it itself, so the card can compare even when the model did not say it (2026-10-01: once it did, once not).
|
|
230
|
+
*/
|
|
231
|
+
export declare function printedTotalOf(text: string): {
|
|
232
|
+
value: number;
|
|
233
|
+
line: number;
|
|
234
|
+
} | undefined;
|
|
194
235
|
export interface EntryTotal {
|
|
195
236
|
field: string;
|
|
196
237
|
/** The sum over every row, and over the rows that will be entered. */
|
|
@@ -201,6 +242,8 @@ export interface EntryTotal {
|
|
|
201
242
|
declaredFound?: boolean;
|
|
202
243
|
/** declared - all. */
|
|
203
244
|
difference?: number;
|
|
245
|
+
/** Where the declared total came from - the model's reading, or the server's own from a file's total line. */
|
|
246
|
+
declaredBy?: 'model' | 'file';
|
|
204
247
|
}
|
|
205
248
|
/** The sums of the fields declared `total`, and how they stand against the total the material prints. */
|
|
206
249
|
export declare function entryTotals(rows: EntryRow[], fields: EntryField[], declared: {
|
|
@@ -208,3 +251,23 @@ export declare function entryTotals(rows: EntryRow[], fields: EntryField[], decl
|
|
|
208
251
|
value?: unknown;
|
|
209
252
|
attachmentId?: string;
|
|
210
253
|
} | undefined, texts: Map<string, string>): EntryTotal[];
|
|
254
|
+
/** The doors of the queries a target's reference fields reach - the person must pass them too. */
|
|
255
|
+
export declare function referenceDoorsOf(fields: EntryField[]): string[];
|
|
256
|
+
/**
|
|
257
|
+
* Settles each reference a row names by text, through the application's query as the person. One match keeps its
|
|
258
|
+
* id and name; none or several keep the row out - several with their names, so the person can say which.
|
|
259
|
+
*/
|
|
260
|
+
export declare function resolveReferences<T extends {
|
|
261
|
+
values: Record<string, unknown>;
|
|
262
|
+
issues: EntryIssue[];
|
|
263
|
+
}>(fields: EntryField[], rows: T[], run: (document: string, variables: Record<string, unknown>) => Promise<{
|
|
264
|
+
data?: any;
|
|
265
|
+
errors?: readonly {
|
|
266
|
+
message: string;
|
|
267
|
+
}[];
|
|
268
|
+
}>): Promise<T[]>;
|
|
269
|
+
/** The rows by group, in the order groups first appear - `[key, rows]`. A row without the key is a group of its own. */
|
|
270
|
+
export declare function entryGroups<T extends {
|
|
271
|
+
index: number;
|
|
272
|
+
values: Record<string, unknown>;
|
|
273
|
+
}>(rows: T[], by: string): [string, T[]][];
|
|
@@ -18,7 +18,11 @@ exports.isBlocked = isBlocked;
|
|
|
18
18
|
exports.writtenForms = writtenForms;
|
|
19
19
|
exports.lineHas = lineHas;
|
|
20
20
|
exports.verifyRowsAgainstSources = verifyRowsAgainstSources;
|
|
21
|
+
exports.printedTotalOf = printedTotalOf;
|
|
21
22
|
exports.entryTotals = entryTotals;
|
|
23
|
+
exports.referenceDoorsOf = referenceDoorsOf;
|
|
24
|
+
exports.resolveReferences = resolveReferences;
|
|
25
|
+
exports.entryGroups = entryGroups;
|
|
22
26
|
/*
|
|
23
27
|
* Data entry from material - what an application declares so the assistant can enter its records
|
|
24
28
|
* (operato-application/design/ai/data-entry.md).
|
|
@@ -64,6 +68,8 @@ async function recordEntryEvent(target, event, context) {
|
|
|
64
68
|
}
|
|
65
69
|
}
|
|
66
70
|
exports.DEFAULT_MAX_ROWS = 200;
|
|
71
|
+
/* The model's way of saying a value came from the person - kept only to read older rows and loose words. */
|
|
72
|
+
const PERSON = /person|message|user|사용자|말/i;
|
|
67
73
|
const targets = new Map();
|
|
68
74
|
const KEY = /^[a-z][a-z0-9-]*\.[a-z][a-z0-9-]*$/;
|
|
69
75
|
/** Validates and keeps a target - `registerEntryTarget` (entry-tools.ts) also opens its tool. A malformed declaration is refused at boot, not in a conversation. */
|
|
@@ -76,8 +82,10 @@ function declareEntryTarget(target) {
|
|
|
76
82
|
catch (error) {
|
|
77
83
|
throw new Error(`[ai-assistant] entry target '${target.key}': its mutation ${error?.message ?? 'does not parse'}.`);
|
|
78
84
|
}
|
|
79
|
-
if (typeof target.variables !== 'function')
|
|
80
|
-
throw new Error(`[ai-assistant] entry target '${target.key}' has no variables.`);
|
|
85
|
+
if (typeof target.variables !== 'function' && typeof target.group?.variables !== 'function')
|
|
86
|
+
throw new Error(`[ai-assistant] entry target '${target.key}' has no variables (per row, or per group).`);
|
|
87
|
+
if (target.group && !target.fields.some(field => field.name === target.group.by))
|
|
88
|
+
throw new Error(`[ai-assistant] entry target '${target.key}': group by '${target.group.by}' is not a declared field.`);
|
|
81
89
|
if (target.update) {
|
|
82
90
|
for (const [what, document, operation] of [
|
|
83
91
|
['update.read', target.update.read?.query, 'query'],
|
|
@@ -100,6 +108,14 @@ function declareEntryTarget(target) {
|
|
|
100
108
|
throw new Error(`[ai-assistant] entry target '${target.key}' declares no fields.`);
|
|
101
109
|
const names = new Set();
|
|
102
110
|
for (const field of target.fields) {
|
|
111
|
+
if (field.type === 'reference') {
|
|
112
|
+
try {
|
|
113
|
+
rootFieldOf(field.reference?.query ?? '', 'query');
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
throw new Error(`[ai-assistant] entry target '${target.key}': reference field '${field.name}' ${error?.message ?? 'has no query'}.`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
103
119
|
if (names.has(field.name))
|
|
104
120
|
throw new Error(`[ai-assistant] entry target '${target.key}' declares '${field.name}' twice.`);
|
|
105
121
|
names.add(field.name);
|
|
@@ -165,6 +181,24 @@ function toDate(value) {
|
|
|
165
181
|
return undefined;
|
|
166
182
|
return `${match[1]}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
|
|
167
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* A moment as `YYYY-MM-DDTHH:mm:ss`, with the offset when it was written - "2026-10-01 08:30", "2026.10.1 8:30",
|
|
186
|
+
* an ISO string. Without an offset it stays without one: the application reads it in its own time zone, as it
|
|
187
|
+
* reads a time typed into its own screen.
|
|
188
|
+
*/
|
|
189
|
+
function toDateTime(value) {
|
|
190
|
+
if (typeof value !== 'string')
|
|
191
|
+
return undefined;
|
|
192
|
+
const match = value.trim().match(/^(\d{4})[-./](\d{1,2})[-./](\d{1,2})[ T](\d{1,2}):(\d{2})(?::(\d{2})(?:\.\d+)?)?\s*(Z|[+-]\d{2}:?\d{2})?$/);
|
|
193
|
+
if (!match)
|
|
194
|
+
return undefined;
|
|
195
|
+
const day = toDate(`${match[1]}-${match[2]}-${match[3]}`);
|
|
196
|
+
const [hour, minute, second] = [Number(match[4]), Number(match[5]), Number(match[6] ?? 0)];
|
|
197
|
+
if (!day || hour > 23 || minute > 59 || second > 59)
|
|
198
|
+
return undefined;
|
|
199
|
+
const offset = match[7] ? (match[7] === 'Z' ? 'Z' : match[7].replace(/^([+-]\d{2})(\d{2})$/, '$1:$2')) : '';
|
|
200
|
+
return `${day}T${String(hour).padStart(2, '0')}:${match[5]}:${String(second).padStart(2, '0')}${offset}`;
|
|
201
|
+
}
|
|
168
202
|
function toBoolean(value) {
|
|
169
203
|
if (typeof value === 'boolean')
|
|
170
204
|
return value;
|
|
@@ -192,12 +226,23 @@ function readFieldValue(field, value) {
|
|
|
192
226
|
const read = toDate(value);
|
|
193
227
|
return read === undefined ? { code: 'not-a-date' } : { read };
|
|
194
228
|
}
|
|
229
|
+
case 'datetime': {
|
|
230
|
+
const read = toDateTime(value);
|
|
231
|
+
return read === undefined ? { code: 'not-a-datetime' } : { read };
|
|
232
|
+
}
|
|
195
233
|
case 'boolean': {
|
|
196
234
|
const read = toBoolean(value);
|
|
197
235
|
return read === undefined ? { code: 'not-a-boolean' } : { read };
|
|
198
236
|
}
|
|
199
237
|
case 'enum':
|
|
200
238
|
return field.values.includes(String(value).trim()) ? { read: String(value).trim() } : { code: 'not-in-list' };
|
|
239
|
+
case 'reference':
|
|
240
|
+
/* Resolved already ({ id }), or a name to look up ({ text }) - resolveReferences settles the second. */
|
|
241
|
+
if (value && typeof value === 'object' && typeof value.id === 'string')
|
|
242
|
+
return { read: { id: value.id, ...(value.label ? { label: String(value.label) } : {}) } };
|
|
243
|
+
if (value && typeof value === 'object' && typeof value.text === 'string')
|
|
244
|
+
return { read: { text: value.text } };
|
|
245
|
+
return typeof value === 'string' || typeof value === 'number' ? { read: { text: String(value).trim() } } : { code: 'not-a-reference' };
|
|
201
246
|
default:
|
|
202
247
|
return { read: String(value).trim() };
|
|
203
248
|
}
|
|
@@ -208,6 +253,10 @@ function sameFieldValue(field, a, b) {
|
|
|
208
253
|
return true;
|
|
209
254
|
if (empty(a) || empty(b))
|
|
210
255
|
return false;
|
|
256
|
+
if (field.type === 'reference') {
|
|
257
|
+
const idOf = (v) => (v && typeof v === 'object' ? v.id ?? v.text : String(v));
|
|
258
|
+
return idOf(a) === idOf(b);
|
|
259
|
+
}
|
|
211
260
|
const x = readFieldValue(field, a);
|
|
212
261
|
const y = readFieldValue(field, b);
|
|
213
262
|
return x.read !== undefined && y.read !== undefined ? x.read === y.read : String(a) === String(b);
|
|
@@ -248,11 +297,16 @@ function normalizeEntryRows(target, input) {
|
|
|
248
297
|
}
|
|
249
298
|
const sources = Array.isArray(raw?.sources)
|
|
250
299
|
? raw.sources
|
|
251
|
-
.filter((one) => one && (one.attachmentId || one.where))
|
|
252
|
-
.map((one) =>
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
300
|
+
.filter((one) => one && (one.attachmentId || one.where || one.person === true))
|
|
301
|
+
.map((one) => {
|
|
302
|
+
/* "the person's message" written by the model is the person flag - the screen says it in its own words. */
|
|
303
|
+
if (one.person === true || (!one.attachmentId && typeof one.where === 'string' && PERSON.test(one.where)))
|
|
304
|
+
return { person: true };
|
|
305
|
+
return {
|
|
306
|
+
...(typeof one.attachmentId === 'string' ? { attachmentId: one.attachmentId } : {}),
|
|
307
|
+
...(typeof one.where === 'string' ? { where: one.where.slice(0, 120) } : {})
|
|
308
|
+
};
|
|
309
|
+
})
|
|
256
310
|
: [];
|
|
257
311
|
if (!sources.length)
|
|
258
312
|
issues.push({ code: 'no-source', blocking: false });
|
|
@@ -277,7 +331,6 @@ function isBlocked(row) {
|
|
|
277
331
|
* message (a correction said in the conversation). A photo cannot be read here - the row says so.
|
|
278
332
|
* Names and descriptions are not looked for: the model tidies them, and a tidied name is not a wrong one.
|
|
279
333
|
*/
|
|
280
|
-
const PERSON = /person|message|user|사용자|말/i;
|
|
281
334
|
/** The ways a number or a date is written in a file. */
|
|
282
335
|
function writtenForms(type, value) {
|
|
283
336
|
if (type === 'number' || type === 'integer') {
|
|
@@ -296,6 +349,13 @@ function writtenForms(type, value) {
|
|
|
296
349
|
}
|
|
297
350
|
return [...forms].map(form => (negative ? `-${form}` : form));
|
|
298
351
|
}
|
|
352
|
+
if (type === 'datetime') {
|
|
353
|
+
/* A moment is found by its day and its hh:mm - the file writes seconds and zones its own way. */
|
|
354
|
+
const match = typeof value === 'string' ? value.match(/^(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2})/) : null;
|
|
355
|
+
if (!match)
|
|
356
|
+
return [];
|
|
357
|
+
return writtenForms('date', match[1]);
|
|
358
|
+
}
|
|
299
359
|
if (type === 'date') {
|
|
300
360
|
const match = typeof value === 'string' ? value.match(/^(\d{4})-(\d{2})-(\d{2})$/) : null;
|
|
301
361
|
if (!match)
|
|
@@ -353,7 +413,7 @@ function verifyRowsAgainstSources(rows, fields, texts) {
|
|
|
353
413
|
if (at >= 0)
|
|
354
414
|
return { ...row, evidence: { attachmentId: id, line: at + 1 } };
|
|
355
415
|
}
|
|
356
|
-
const saidByPerson = row.sources.some(source =>
|
|
416
|
+
const saidByPerson = row.sources.some(source => source.person);
|
|
357
417
|
const issues = [...row.issues];
|
|
358
418
|
for (const one of forms) {
|
|
359
419
|
const anywhere = readable.some(id => texts.get(id).split(/\r?\n/).some(line => lineHas(line, one.forms)));
|
|
@@ -365,15 +425,45 @@ function verifyRowsAgainstSources(rows, fields, texts) {
|
|
|
365
425
|
return { ...row, issues };
|
|
366
426
|
});
|
|
367
427
|
}
|
|
428
|
+
/**
|
|
429
|
+
* The total a file prints - a line named 합계 · 총계 · 총액 · total · sum, and the last amount on it. The server
|
|
430
|
+
* reads it itself, so the card can compare even when the model did not say it (2026-10-01: once it did, once not).
|
|
431
|
+
*/
|
|
432
|
+
function printedTotalOf(text) {
|
|
433
|
+
const lines = text.split(/\r?\n/);
|
|
434
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
435
|
+
if (!/(합\s*계|총\s*계|총\s*액|total|sum)/i.test(lines[i]))
|
|
436
|
+
continue;
|
|
437
|
+
const amounts = [...lines[i].matchAll(/-?\d{1,3}(?:,\d{3})+(?:\.\d+)?|-?\d+(?:\.\d+)?/g)].map(match => Number(match[0].replace(/,/g, '')));
|
|
438
|
+
if (amounts.length)
|
|
439
|
+
return { value: amounts[amounts.length - 1], line: i + 1 };
|
|
440
|
+
}
|
|
441
|
+
return undefined;
|
|
442
|
+
}
|
|
368
443
|
/** The sums of the fields declared `total`, and how they stand against the total the material prints. */
|
|
369
444
|
function entryTotals(rows, fields, declared, texts) {
|
|
370
|
-
|
|
371
|
-
|
|
445
|
+
const totalled = fields.filter(field => field.total && (field.type === 'number' || field.type === 'integer'));
|
|
446
|
+
/* With one totalled field and no total from the model, the server looks for the file's own total line. */
|
|
447
|
+
const printed = !declared && totalled.length === 1 ? [...texts.values()].map(printedTotalOf).find(Boolean) : undefined;
|
|
448
|
+
return totalled
|
|
372
449
|
.map(field => {
|
|
373
450
|
const sum = (list) => Math.round(list.reduce((acc, row) => acc + (typeof row.values[field.name] === 'number' ? row.values[field.name] : 0), 0) * 100) / 100;
|
|
374
451
|
const total = { field: field.name, all: sum(rows), ready: sum(rows.filter(row => !isBlocked(row))) };
|
|
375
|
-
|
|
452
|
+
if (printed) {
|
|
453
|
+
total.declared = printed.value;
|
|
454
|
+
total.declaredFound = true;
|
|
455
|
+
total.declaredBy = 'file';
|
|
456
|
+
total.difference = Math.round((printed.value - total.all) * 100) / 100;
|
|
457
|
+
return total;
|
|
458
|
+
}
|
|
459
|
+
/*
|
|
460
|
+
* With one totalled field the model's total is that field's, whatever it named it - on office's second card it
|
|
461
|
+
* wrote the file's column name (이용금액) for amount, and the total was dropped (2026-10-01).
|
|
462
|
+
*/
|
|
463
|
+
const mine = declared && (totalled.length === 1 || (declared.field ?? field.name) === field.name);
|
|
464
|
+
const value = mine ? toNumber(declared.value) : undefined;
|
|
376
465
|
if (value !== undefined) {
|
|
466
|
+
total.declaredBy = 'model';
|
|
377
467
|
total.declared = value;
|
|
378
468
|
total.difference = Math.round((value - total.all) * 100) / 100;
|
|
379
469
|
const text = declared.attachmentId ? texts.get(declared.attachmentId) : undefined;
|
|
@@ -383,4 +473,61 @@ function entryTotals(rows, fields, declared, texts) {
|
|
|
383
473
|
return total;
|
|
384
474
|
});
|
|
385
475
|
}
|
|
476
|
+
/** The doors of the queries a target's reference fields reach - the person must pass them too. */
|
|
477
|
+
function referenceDoorsOf(fields) {
|
|
478
|
+
return [...new Set(fields.filter(field => field.type === 'reference' && field.reference).map(field => rootFieldOf(field.reference.query, 'query')))];
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Settles each reference a row names by text, through the application's query as the person. One match keeps its
|
|
482
|
+
* id and name; none or several keep the row out - several with their names, so the person can say which.
|
|
483
|
+
*/
|
|
484
|
+
async function resolveReferences(fields, rows, run) {
|
|
485
|
+
const references = fields.filter(field => field.type === 'reference' && field.reference);
|
|
486
|
+
if (!references.length)
|
|
487
|
+
return rows;
|
|
488
|
+
const asked = new Map();
|
|
489
|
+
for (const row of rows) {
|
|
490
|
+
for (const field of references) {
|
|
491
|
+
const value = row.values[field.name];
|
|
492
|
+
if (!value || typeof value !== 'object' || typeof value.text !== 'string')
|
|
493
|
+
continue;
|
|
494
|
+
const key = `${field.name}\u0000${value.text}`;
|
|
495
|
+
if (!asked.has(key)) {
|
|
496
|
+
try {
|
|
497
|
+
const answer = await run(field.reference.query, field.reference.variables(value.text));
|
|
498
|
+
asked.set(key, answer.errors?.length ? new Error(answer.errors[0].message) : field.reference.recordsOf(answer.data, value.text) ?? []);
|
|
499
|
+
}
|
|
500
|
+
catch (error) {
|
|
501
|
+
asked.set(key, error instanceof Error ? error : new Error(String(error)));
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
const found = asked.get(key);
|
|
505
|
+
if (found instanceof Error) {
|
|
506
|
+
row.issues.push({ field: field.name, code: 'reference-unreadable', params: { text: value.text }, blocking: true });
|
|
507
|
+
}
|
|
508
|
+
else if (found.length === 1) {
|
|
509
|
+
row.values[field.name] = { id: found[0].id, ...(found[0].label ? { label: found[0].label } : {}) };
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
row.issues.push({
|
|
513
|
+
field: field.name,
|
|
514
|
+
code: found.length ? 'reference-ambiguous' : 'reference-not-found',
|
|
515
|
+
params: { text: value.text, ...(found.length ? { candidates: found.slice(0, 5).map(one => one.label || one.id).join(', ') } : {}) },
|
|
516
|
+
blocking: true
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return rows;
|
|
522
|
+
}
|
|
523
|
+
/** The rows by group, in the order groups first appear - `[key, rows]`. A row without the key is a group of its own. */
|
|
524
|
+
function entryGroups(rows, by) {
|
|
525
|
+
const groups = new Map();
|
|
526
|
+
for (const row of rows) {
|
|
527
|
+
const value = row.values[by];
|
|
528
|
+
const key = value === undefined || value === null || value === '' ? `\u0000row-${row.index}` : String(typeof value === 'object' ? value.id ?? value.text : value);
|
|
529
|
+
groups.set(key, [...(groups.get(key) ?? []), row]);
|
|
530
|
+
}
|
|
531
|
+
return [...groups.entries()];
|
|
532
|
+
}
|
|
386
533
|
//# sourceMappingURL=entry-target.js.map
|