Compare commits
86
Commits
develop
...
v0.1.0-RC.35
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
610b605e44 | ||
|
|
7fbeab3811
|
||
|
|
dc054b34e7
|
||
|
|
86e649e165 | ||
|
|
33c358a90f
|
||
|
|
fae89fff2f
|
||
|
|
47eb69edef
|
||
|
|
ea072700e8 | ||
|
|
bfdf9e7792
|
||
|
|
7f1db4cae1
|
||
|
|
09a176dae6
|
||
|
|
3a6fd1b809 | ||
|
|
d8a1168210
|
||
|
|
55bcf1a509
|
||
|
|
b4bda50ae2 | ||
|
|
a4b2b01612
|
||
|
|
a51fd12799
|
||
|
|
88aec4afec | ||
|
|
ceb31a71b3
|
||
|
|
e3f68bf7ee
|
||
|
|
e37602b71e | ||
|
|
cd1cdaa1c0
|
||
|
|
1ae7bb9cfa
|
||
|
|
6d66deabc9 | ||
|
|
08c8264f23
|
||
|
|
07b06f2070
|
||
|
|
61fca48424 | ||
|
|
d761bd4a7d
|
||
|
|
f1634b0911
|
||
|
|
feab4caf17 | ||
|
|
ba21b72188
|
||
|
|
0a65702dab
|
||
|
|
739ca6981f | ||
|
|
b51dd4dad1
|
||
|
|
c3a03b8d84
|
||
|
|
2d25f0b769
|
||
|
|
c5125f9788
|
||
|
|
3de664e0fd | ||
|
|
2caa634044 | ||
|
|
c77e5b0123
|
||
|
|
f46735b1c0 | ||
|
|
86563c64d2 | ||
|
|
82febe0f19 | ||
|
|
1ea23dfc49
|
||
|
|
9d31de46a6 | ||
|
|
24b0b3b14a
|
||
|
|
1c33e97664 | ||
|
|
fcaf2ded7e
|
||
|
|
2cea8ffd44 | ||
|
|
6b98d5216b
|
||
|
|
6adf390bf8 | ||
|
|
bc92c89665
|
||
|
|
d43402e262 | ||
|
|
fe725ac8ff
|
||
|
|
dfb05d8935 | ||
|
|
523fd0128a
|
||
|
|
47115fe359 | ||
|
|
f2d2b50459
|
||
|
|
8c33db9fcf | ||
|
|
75a5dedf7f
|
||
|
|
c1cceaf4c3 | ||
|
|
f8a114bbb8
|
||
|
|
3a7ff1c659 | ||
|
|
ea81166454
|
||
|
|
cfcc044055 | ||
|
|
620dee488d
|
||
|
|
e23206f1e8 | ||
|
|
c6bae3bcf2
|
||
|
|
2d8459bb24 | ||
|
|
ac06a96787
|
||
|
|
d4a1f64480 | ||
|
|
c28e782075
|
||
|
|
5c76a13245 | ||
|
|
8d1348888d
|
||
|
|
47cc572c36 | ||
|
|
8099f64bf2
|
||
|
|
ac6ec75deb | ||
|
|
1f48a370e3
|
||
|
|
d0ad2aa0fa | ||
|
|
16c8500f59
|
||
|
|
a8c5f586ee | ||
|
|
3dfa775b21
|
||
|
|
67f78fd660 | ||
|
|
ff67c6f7cc | ||
|
|
19551ac2af
|
||
|
|
7263ac627d
|
No files matched your search
@@ -65,6 +65,7 @@ Always use the wrapper: Maven 3.10 cannot read the parent POM. The committed def
|
||||
- Parallel tool results go back in ONE user message, in call order.
|
||||
- Errors (DENIED, NOT_FOUND, INVALID_INPUT, ERROR) are `is_error` results.
|
||||
- Unknown tools and over-limit calls also get error results.
|
||||
- **Who writes a table:** every `assistant_*` table has a decision in `metadata/AssistantTableWrites` (a table without one is not built). A table the assistant writes (messages, turns, tool calls, usage and budget accounting, pending actions, scheduled runs and the like) has no insert, update or delete capability, and the permission checker of every table refuses a write a person may not make through a table route; only `AssistantStore` writes them. A person's save of a settings table sets no field that is not editable: every pre-save customizer starts with `StoredRecords.ofSave`, which takes such fields out of a `USER` save. Never pair a record with its stored row or compare a saved value with a stored one except through `StoredRecords`.
|
||||
- **Keys:** never stored in a table. `assistant_provider.apiKeyEnvName` holds only an allowed environment variable NAME; `apiKeyStatus` is computed on read (last 4 at most) and stripped on save.
|
||||
- **Logging:** never log message content, prompts, tool results or keys. Structured end-of-turn log only.
|
||||
- **Thinking:** thinking is never streamed or stored. The Anthropic adapter keeps the raw assistant message (with signatures) only in memory, as `providerState`, for the tool loop.
|
||||
|
||||
@@ -1,5 +1,38 @@
|
||||
# Changelog
|
||||
|
||||
## Unreleased - The tables the assistant writes are written by nobody else; a person's save sets no field the server decides
|
||||
|
||||
**A host changes nothing, unless it wrote rows of these tables through a table route, or replaced the permission rules of the assistant's tables (see `UPGRADE.md`).**
|
||||
|
||||
- **Every table of the assistant has a decision about who writes it** (`AssistantTableWrites`), by operation. A table without a decision cannot be built, so a new table is decided when it is added.
|
||||
- *Settings, edited by an administrator on a record screen (insert, update, delete):* `assistant_policy`, `assistant_provider`, `assistant_model`, `assistant_model_price`, `assistant_profile`, `assistant_skill`, `assistant_profile_skill`, `assistant_data_access`, `assistant_rag_source`, `assistant_scheduled_task`, `assistant_budget`, `assistant_budget_override`.
|
||||
- *A conversation* (`assistant_conversation`) is started by the assistant, through its own API. Its owner renames, pins, archives and deletes it, also through the table routes (update, delete); it is never added there.
|
||||
- *Written by the assistant only:* what was said and done (`assistant_message`, `assistant_turn`, `assistant_turn_event`, `assistant_tool_call`, `assistant_pending_action`, `assistant_pending_action_claim`, `assistant_feedback`, `assistant_client_error`, `assistant_data_audit`, `assistant_scheduled_run`) and what was spent (`assistant_usage`, `assistant_usage_daily`, `assistant_budget_usage`, `assistant_budget_event`). A rating and a client error are sent to the assistant's own API, which writes them.
|
||||
- **The assistant enforces the decision itself, on any QQQ version.** A table has no capability (insert, update, delete) for what a person may not do to it, so the metadata a screen reads offers no button for it and QQQ defines no bulk action for it. And the permission checker of every table of the assistant refuses the insert, update or delete a table route is about to make, for every person, with 403 and a message that says why: "Rows of Assistant Tool Call are written by the assistant; they cannot be changed." (`AssistantAdminPermissionChecker`, `AssistantAccessPermissionChecker`, and the new `AssistantWrittenByTheAssistantPermissionChecker` for `assistant_scheduled_run`, which needs no permission to read).
|
||||
- **The assistant's own writes are unchanged.** They are made by `AssistantStore`, in code, never through a table route: an answer, a tool call, the accounting of a model call, a proposed change and its decision, a scheduled run, retention. Deleting a conversation still deletes what belongs to it.
|
||||
- **Changed for administrators:** rows of `assistant_message`, `assistant_turn`, `assistant_turn_event`, `assistant_tool_call`, `assistant_feedback`, `assistant_client_error`, `assistant_usage`, `assistant_usage_daily`, `assistant_pending_action` and `assistant_pending_action_claim` had a Delete button on their record screens. They no longer have one. History is removed by the retention process, and a conversation with everything that belongs to it by deleting the conversation.
|
||||
- **A person's save of a settings table sets no field the server decides.** A field that is not editable (the id, the date a row was created, who granted an override, a provider's health and test results, a model's test result, what a knowledge source indexed, a scheduled task's approval, next run and last run) is taken out of a save whose input source is `USER`, which every table route sets, before a rule sees it or it is stored (`StoredRecords.ofSave`, used by every settings customizer; `assistant_policy`, which has no rules of its own, gets `AssistantPersonSaveCustomizer` for it). It is taken out, not refused: a client that sends a whole record back keeps working, and what it sent for such a field changes nothing. The primary key of an update stays (the route takes it from the path), and so do the dates QQQ itself sets on the save. The assistant's own saves set these fields as before.
|
||||
- Tests, through the real routes against the fixture application: `TablesWrittenByTheAssistantTest` (for each of the fourteen tables, the administrator of the assistant and an ordinary user are each refused an insert, an update, an update that changes nothing, an update of the modify date and a delete, with 403, and every row is exactly what it was; the metadata offers no write and no bulk action; a conversation is renamed and deleted by its owner and not added; the assistant's own work still writes its tables), `AssistantTableWritesTest` (every table has a decision and is built to it; the checkers refuse every kind of write and nothing else; every table a person edits takes the server's fields out of a person's save and leaves them in the assistant's own), and server-decided fields in `BudgetEditOverHttpTest`, `RecordEditsOverHttpTest`, `AssistantSettingsTest`, `RagTest` and `ScheduledTasksTest`.
|
||||
|
||||
## Unreleased - Fixed: a setting saved from its record screen is stored, and is held to its rules as it will be stored
|
||||
|
||||
**Nothing for a host to change.**
|
||||
|
||||
- **A budget could not be edited on its record screen.** The screen saves through the UI API's table update route (`PATCH /qqq/v1/table/{tableName}/{primaryKey}`): only the fields that were changed, and the version that was read. The save was answered with 400 ("Invalid scopeType., Invalid period., Invalid metric. ...") and nothing was stored. The record's primary key comes from the request's path, so it reaches a table customizer as text; `AssistantBudgetCustomizer` looked the stored row up by comparing that text with the stored integer key, did not find it, and judged the one changed field as if it were a whole new budget.
|
||||
- **The same mistake was in every settings customizer, and is fixed in all of them.** A budget override, a data access row, a provider, a knowledge source and a scheduled task were refused the same way (400, nothing stored). A profile and a skill were stored, with the defaults of a new row written over what the save had not sent: a profile switched off was switched on and its routing mode set to OFF, a skill switched off was switched on.
|
||||
- **The fix.** The customizers no longer pair a record with its stored row themselves (`ArchitectureTest` keeps it so). One class does it (`StoredRecords`): the key of the record being saved and the key of each stored row are both read as the type of the table's key field before they are compared, so the pairing holds whether the key arrives as text or as a number. The values being saved are laid over the stored row the same way, each read as the type of its field. A value that cannot be read as that type (text in a number field) is refused with 400 and a message that names the field and does not repeat the value.
|
||||
- **The rules are judged on the record as it will be stored, for a save of one field exactly as for a save of all of them:** the host's ceiling on a cost limit (`withBudgetCostLimitCeilingUsd`; a save that only changes a token limit into a cost limit is over the ceiling with the limit already stored), required fields (a save cannot blank the name, the limit, the period, the metric or the scope), the allowed periods, scopes, metrics and actions, scope values, the downgrade model, the thresholds, and for an override the reason, the expiry and "exactly one of extra value or unlimited until". A refusal has the same words for a partial save as for a whole one.
|
||||
- **A change is found by comparing values of the same type.** A scheduled task keeps the approval of its writes and its next run when a save sends a value it already has as text (`enabled=true`), and loses the approval only when its prompt, its user, its credential, its profile or its write mode really changed. A knowledge source keeps its vectors when a save sends the chunk size it already has.
|
||||
- **Nothing a save could not set before can be set now.** The key in the path is the record that is saved, whatever the body says; who granted an override is the session's on insert and is not taken from an update; the approval of a scheduled task's writes is not taken from a save.
|
||||
- **Field-by-field audit** (where a host turns it on for budgets) records the one field that was saved, with its old and its new value. The stored row a rule is judged on is never what is saved.
|
||||
- Tests, all through the real route against the fixture application (`RecordScreen` sends what the screen sends: a multipart `PATCH` of the changed fields with `expectedVersion`): `BudgetEditOverHttpTest` (the limit, the enabled flag, the period, the name, the notes and a threshold, each saved alone with every other field kept; a second save from the stale version is refused with 409 and stores nothing; the ceiling for a partial save, a whole save and a new budget; twenty refusals of a partial save, each storing nothing; a blanked setting gets its default; what the server decides; an override; the audit; a person without `assistant.admin`), `RecordEditsOverHttpTest` (profile, default profile, skill, data access), and one test each in `AssistantSettingsTest` (provider and model), `RagTest` (knowledge source) and `ScheduledTasksTest` (scheduled task). Fourteen of the sixteen fail without the fix. `StoredRecordsTest` pairs a record with its stored row for an integer, a long and a text key, with the key as text and as a number.
|
||||
|
||||
## Unreleased - Every table has a create date and a modify date
|
||||
|
||||
- Nine tables lacked a standard date: `assistant_turn_event`, `assistant_tool_call`, `assistant_feedback`, `assistant_usage`, `assistant_data_audit`, `assistant_budget_event`, `assistant_client_error` and `assistant_pending_action_claim` had `createDate` but no `modifyDate`, and `assistant_budget_usage` had neither. All nine now declare both, `DATE_TIME`, not editable, set by QQQ (`CREATE_DATE` and `MODIFY_DATE` behaviors); every other table already had them.
|
||||
- Every write the QBit makes goes through QQQ's insert and update actions, so an insert sets both dates and an update (an incremented daily usage counter, a decided pending action, a cleared tool result, a scheduled run that ends, the budget accounting row that follows a usage row) moves `modifyDate` and leaves `createDate`. A row of a table whose rows never change (`assistant_pending_action_claim`, `assistant_budget_event`) has `modifyDate` equal to `createDate` exactly: QQQ's two date behaviors read the clock separately, so a new insert customizer (`AssistantInsertOnlyDatesCustomizer`, registered by the QBit on those two tables) makes the two one instant. The other new `modifyDate` values are QQQ's own (turn events, feedback, client errors and the data audit are inserted once, and rewritten only when a transcript is redacted).
|
||||
- `docs/schema-postgres.sql` has a new block at its end (additive; see UPGRADE.md): the columns are nullable and default to the time of the write, so the previous release keeps working against the same database while a host migrates.
|
||||
|
||||
## Unreleased - The assistant can read the files of records, where a host turns that on; a link in an answer is one the application gave
|
||||
|
||||
Design: `docs/file-contents-and-links.md`. **Off by default: an upgrade changes nothing until a host turns file reading on in code (see `UPGRADE.md`).**
|
||||
|
||||
@@ -107,6 +107,7 @@ Always use the wrapper: Maven 3.10 cannot read the parent POM. The committed def
|
||||
- A turn goes on by itself after such a reply at most `maxOutputContinuations` times, each a model call under every limit of any other; then it ends `INCOMPLETE`, never `COMPLETE`, with a notice of what was done and what was not. An `INCOMPLETE` answer stays in the conversation's history.
|
||||
- The output ceiling of one reply is the code's (`models.mainMaxOutputTokens`); a provider row lowers it only down to `minOutputTokensPerRequest` (`TurnEngine.outputCeiling`).
|
||||
- One answer shows a record once and at most `maxRecordCardsPerTurn` record cards and `maxRecordChipsPerTurn` record links (`TurnEngine.recordToolResult`); a confirmation card is never left out. The caps are presentation only: every call still runs and has its row.
|
||||
- **Who writes a table:** every `assistant_*` table has a decision in `metadata/AssistantTableWrites` (a table without one is not built). A table the assistant writes (messages, turns, tool calls, usage and budget accounting, pending actions, scheduled runs and the like) has no insert, update or delete capability, and the permission checker of every table refuses a write a person may not make through a table route; only `AssistantStore` writes them. A person's save of a settings table sets no field that is not editable: every pre-save customizer starts with `StoredRecords.ofSave`, which takes such fields out of a `USER` save. Never pair a record with its stored row or compare a saved value with a stored one except through `StoredRecords`.
|
||||
- **Keys:** never stored in a table. `assistant_provider.apiKeyEnvName` holds only an allowed environment variable NAME; `apiKeyStatus` is computed on read (last 4 at most) and stripped on save.
|
||||
- Scheduled task `credentialEnvName` is a separately allow-listed environment variable NAME. Tokens never enter rows, model context or logs. `credentialStatus` is computed and stripped on save.
|
||||
- Scheduled write approval is invalidated by changes to prompt, run-as user, credential reference, profile or write request. Scheduled writes always require the designated user's panel confirmation.
|
||||
|
||||
+40
@@ -1,5 +1,45 @@
|
||||
# Upgrading qbit-assistant
|
||||
|
||||
## The tables the assistant writes are written by nobody else (Unreleased)
|
||||
|
||||
No schema change and no configuration change. Check three things:
|
||||
|
||||
- **Code of the host that writes a row of a table the assistant writes** (`assistant_message`, `assistant_turn`, `assistant_turn_event`,
|
||||
`assistant_tool_call`, `assistant_pending_action`, `assistant_pending_action_claim`, `assistant_feedback`, `assistant_client_error`,
|
||||
`assistant_data_audit`, `assistant_scheduled_run`, `assistant_usage`, `assistant_usage_daily`, `assistant_budget_usage`,
|
||||
`assistant_budget_event`) **through a table route or the application API, as a person:** it is answered with 403 now. Nothing of the
|
||||
assistant does this. A seed or a migration that writes these tables in code, as the system, is not affected.
|
||||
- **An administrator's habit of deleting single rows of history on a record screen:** the Delete button is gone from those tables. Use
|
||||
the retention process, or delete the conversation.
|
||||
- **A host that replaces the permission rules of the assistant's tables** (through `withTableMetaDataCustomizer`): the refusal is made by
|
||||
the assistant's permission checkers (`AssistantAdminPermissionChecker`, `AssistantAccessPermissionChecker`,
|
||||
`AssistantWrittenByTheAssistantPermissionChecker`). Keep them, or call them from the host's own checker.
|
||||
|
||||
A person's save of a settings table no longer sets a field that is not editable (the id, `createDate`, `grantedBy`, test and health
|
||||
results, index counters, a scheduled task's approval and run fields). A client that sent such a field gets no error; the value is not
|
||||
stored.
|
||||
|
||||
## Every table has create and modify dates (Unreleased)
|
||||
|
||||
Nine tables gain columns: `modify_date` on `assistant_turn_event`, `assistant_tool_call`,
|
||||
`assistant_feedback`, `assistant_usage`, `assistant_data_audit`, `assistant_budget_event`,
|
||||
`assistant_client_error` and `assistant_pending_action_claim`, and `create_date` and `modify_date` on
|
||||
`assistant_budget_usage` (the names an application gets when it derives snake_case backend names from
|
||||
field names; otherwise `modifyDate` and `createDate`). A host that writes its own migrations runs
|
||||
the block at the end of `docs/schema-postgres.sql` ("Table standard") **before** deploying this release:
|
||||
|
||||
- It is safe while the previous release is still running against the same database: the columns are
|
||||
nullable and default to `CURRENT_TIMESTAMP`, so rows the previous release inserts are given a value.
|
||||
On PostgreSQL 11 and later adding a column with that (stable) default does not rewrite the table.
|
||||
- The `UPDATE` statements give existing rows the table's own creation time (`occurred_at` for
|
||||
`assistant_budget_usage`). They touch every row: on a large table (`assistant_usage`,
|
||||
`assistant_tool_call`, `assistant_turn_event`) run each in id ranges, and run them before the new
|
||||
release starts writing, or they overwrite real `modify_date` values.
|
||||
- `assistant_pending_action_claim` and `assistant_budget_event` rows are written once: their `modifyDate` equals their `createDate` and never moves.
|
||||
|
||||
Nothing else changes: no configuration, no code to change. A host whose database is made from
|
||||
the reference DDL, or whose own migrations run the block, needs no customizer.
|
||||
|
||||
## The assistant can read the files of records (Unreleased)
|
||||
|
||||
**Nothing changes on upgrade: reading the content of files is off until a host turns it on in code.** Turned on, the assistant reads the content of a file a record holds, for a person who may open it. An answer keeps no address of the application that the application did not give. No schema change, no permission, no API change. Design: `docs/file-contents-and-links.md`.
|
||||
|
||||
@@ -777,3 +777,37 @@ CREATE TABLE IF NOT EXISTS assistant_pending_action_claim
|
||||
CONSTRAINT assistant_pending_action_claim_action UNIQUE (pending_action_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS assistant_pending_action_claim_user_id ON assistant_pending_action_claim (user_id);
|
||||
|
||||
-- Table standard: every table has create_date and modify_date. Eight tables had only
|
||||
-- create_date and assistant_budget_usage had neither. Additive and safe while the
|
||||
-- previous release is still running against the same database:
|
||||
-- * the columns are nullable and default to the time of the write, so rows the previous
|
||||
-- release inserts (it does not know the columns) are still given a value;
|
||||
-- * on PostgreSQL 11 and later adding a column whose default is CURRENT_TIMESTAMP
|
||||
-- (STABLE, evaluated once at ALTER time) does not rewrite the table and takes only a
|
||||
-- short lock;
|
||||
-- * the UPDATE statements give existing rows the table's own creation time (for
|
||||
-- assistant_budget_usage, occurred_at, the usage row's creation time). They touch every
|
||||
-- row, so on a large table (assistant_usage, assistant_tool_call, assistant_turn_event)
|
||||
-- run each one in id ranges (AND id BETWEEN a AND b). Run them BEFORE the new release
|
||||
-- starts writing: afterwards they would overwrite real modify_date values.
|
||||
-- Safe to re-run.
|
||||
ALTER TABLE assistant_turn_event ADD COLUMN IF NOT EXISTS modify_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
UPDATE assistant_turn_event SET modify_date = create_date WHERE create_date IS NOT NULL AND modify_date IS DISTINCT FROM create_date;
|
||||
ALTER TABLE assistant_tool_call ADD COLUMN IF NOT EXISTS modify_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
UPDATE assistant_tool_call SET modify_date = create_date WHERE create_date IS NOT NULL AND modify_date IS DISTINCT FROM create_date;
|
||||
ALTER TABLE assistant_feedback ADD COLUMN IF NOT EXISTS modify_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
UPDATE assistant_feedback SET modify_date = create_date WHERE create_date IS NOT NULL AND modify_date IS DISTINCT FROM create_date;
|
||||
ALTER TABLE assistant_usage ADD COLUMN IF NOT EXISTS modify_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
UPDATE assistant_usage SET modify_date = create_date WHERE create_date IS NOT NULL AND modify_date IS DISTINCT FROM create_date;
|
||||
ALTER TABLE assistant_data_audit ADD COLUMN IF NOT EXISTS modify_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
UPDATE assistant_data_audit SET modify_date = create_date WHERE create_date IS NOT NULL AND modify_date IS DISTINCT FROM create_date;
|
||||
ALTER TABLE assistant_budget_event ADD COLUMN IF NOT EXISTS modify_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
UPDATE assistant_budget_event SET modify_date = create_date WHERE create_date IS NOT NULL AND modify_date IS DISTINCT FROM create_date;
|
||||
ALTER TABLE assistant_client_error ADD COLUMN IF NOT EXISTS modify_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
UPDATE assistant_client_error SET modify_date = create_date WHERE create_date IS NOT NULL AND modify_date IS DISTINCT FROM create_date;
|
||||
ALTER TABLE assistant_pending_action_claim ADD COLUMN IF NOT EXISTS modify_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
UPDATE assistant_pending_action_claim SET modify_date = create_date WHERE create_date IS NOT NULL AND modify_date IS DISTINCT FROM create_date;
|
||||
ALTER TABLE assistant_budget_usage ADD COLUMN IF NOT EXISTS create_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
ALTER TABLE assistant_budget_usage ADD COLUMN IF NOT EXISTS modify_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
|
||||
UPDATE assistant_budget_usage SET create_date = occurred_at, modify_date = occurred_at WHERE create_date IS DISTINCT FROM occurred_at OR modify_date IS DISTINCT FROM occurred_at;
|
||||
@@ -20,7 +20,7 @@
|
||||
<!-- Multi-module build. The qqq-orb sets the revision here; the modules -->
|
||||
<!-- inherit it, and the flatten plugin resolves it in the published POMs. -->
|
||||
<properties>
|
||||
<revision>0.4.0-SNAPSHOT</revision>
|
||||
<revision>0.1.0-RC.35</revision>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
+5
@@ -46,6 +46,11 @@ public class AssistantAccessPermissionChecker implements CustomPermissionChecker
|
||||
{
|
||||
throw (new QPermissionDeniedException("Not authorized to use the assistant."));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// whoever asks: a table the assistant writes is not written by a person //
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
AssistantTableWrites.refuseWhatAPersonMayNotWrite(actionInput, metaDataWithPermissionRules);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -45,6 +45,11 @@ public class AssistantAdminPermissionChecker implements CustomPermissionChecker
|
||||
{
|
||||
throw (new QPermissionDeniedException("Not authorized to access the assistant admin data."));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// whoever asks: a table the assistant writes is not written by a person //
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
AssistantTableWrites.refuseWhatAPersonMayNotWrite(actionInput, metaDataWithPermissionRules);
|
||||
}
|
||||
|
||||
}
|
||||
+2
-6
@@ -32,9 +32,7 @@ import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.statusmessages.BadInputStatusMessage;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
@@ -52,12 +50,10 @@ public class AssistantBudgetCustomizer implements TableCustomizerInterface
|
||||
@Override
|
||||
public List<QRecord> preInsertOrUpdate(AbstractActionInput input, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
|
||||
{
|
||||
Optional<Map<Serializable, QRecord>> old = oldRecordListToMap("id", oldRecordList);
|
||||
StoredRecords stored = StoredRecords.ofSave(input, records, oldRecordList);
|
||||
for(QRecord record : records)
|
||||
{
|
||||
QRecord merged = new QRecord();
|
||||
old.map(m -> m.get(record.getValue("id"))).ifPresent(o -> merged.setValues(new LinkedHashMap<>(o.getValues())));
|
||||
record.getValues().forEach(merged::setValue);
|
||||
QRecord merged = stored.merged(record);
|
||||
if(merged.getValue("budgetId") != null)
|
||||
{
|
||||
checkOverride(record, merged, input instanceof InsertInput);
|
||||
|
||||
+3
-8
@@ -17,10 +17,7 @@
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import com.kingsrook.qbits.assistant.AssistantRuntime;
|
||||
import com.kingsrook.qbits.assistant.access.DataAccessMode;
|
||||
@@ -53,13 +50,11 @@ public class AssistantDataAccessCustomizer implements TableCustomizerInterface
|
||||
@Override
|
||||
public List<QRecord> preInsertOrUpdate(AbstractActionInput input, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
|
||||
{
|
||||
QInstance qInstance = QContext.getQInstance();
|
||||
Optional<Map<Serializable, QRecord>> old = oldRecordListToMap("id", oldRecordList);
|
||||
QInstance qInstance = QContext.getQInstance();
|
||||
StoredRecords stored = StoredRecords.ofSave(input, records, oldRecordList);
|
||||
for(QRecord record : records)
|
||||
{
|
||||
QRecord merged = new QRecord();
|
||||
old.map(m -> m.get(record.getValue("id"))).ifPresent(o -> merged.setValues(new LinkedHashMap<>(o.getValues())));
|
||||
record.getValues().forEach(merged::setValue);
|
||||
QRecord merged = stored.merged(record);
|
||||
|
||||
String type = merged.getValueString("objectType");
|
||||
String name = merged.getValueString("objectName");
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.actions.customizers.AbstractPreInsertCustomizer;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** For a table whose rows are written once and never change (a decision claim,
|
||||
** a budget event): modifyDate equals createDate, exactly. QQQ's two date
|
||||
** behaviors each read the clock on their own, so left alone the two values of
|
||||
** one insert differ by a few microseconds. This runs after the behaviors and
|
||||
** before the row is stored, and makes the second the same instant as the first.
|
||||
*******************************************************************************/
|
||||
public class AssistantInsertOnlyDatesCustomizer extends AbstractPreInsertCustomizer
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
** After the date behaviors ran (they run before the unique-key checks).
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public WhenToRun getWhenToRun()
|
||||
{
|
||||
return (WhenToRun.BEFORE_UNIQUE_KEY_CHECKS);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** modifyDate = createDate on every record.
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public List<QRecord> apply(List<QRecord> records) throws QException
|
||||
{
|
||||
for(QRecord record : records)
|
||||
{
|
||||
if(record.getValue("createDate") != null)
|
||||
{
|
||||
record.setValue("modifyDate", record.getValue("createDate"));
|
||||
}
|
||||
}
|
||||
return (records);
|
||||
}
|
||||
|
||||
}
|
||||
+24
-3
@@ -165,6 +165,11 @@ public class AssistantMetaDataBuilder
|
||||
{
|
||||
List<QTableMetaData> tables = new ArrayList<>();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
// tables whose rows are written once and never change: modifyDate = createDate //
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
QCodeReference insertOnlyDates = new QCodeReference(AssistantInsertOnlyDatesCustomizer.class);
|
||||
|
||||
QCodeReference conversationCustomizer = new QCodeReference(AssistantConversationCustomizer.class);
|
||||
tables.add(owned(base(AssistantConversation.TABLE_NAME, "Assistant Conversation", "forum", AssistantConversation.class), false)
|
||||
.withRecordLabelFormat("%s")
|
||||
@@ -193,8 +198,11 @@ public class AssistantMetaDataBuilder
|
||||
tables.add(text(readOnly(owned(base(AssistantClientError.TABLE_NAME, "Assistant Client Error", "bug_report", AssistantClientError.class), false)), "message", "stack", "componentStack")
|
||||
.withRecordLabelFormat("%s #%s").withRecordLabelFields("scope", "id"));
|
||||
|
||||
QCodeReference personSave = new QCodeReference(AssistantPersonSaveCustomizer.class);
|
||||
tables.add(base(AssistantPolicy.TABLE_NAME, "Assistant Policy", "policy", AssistantPolicy.class)
|
||||
.withRecordLabelFormat("%s").withRecordLabelFields("name"));
|
||||
.withRecordLabelFormat("%s").withRecordLabelFields("name")
|
||||
.withCustomizer(TableCustomizers.PRE_INSERT_RECORD, personSave)
|
||||
.withCustomizer(TableCustomizers.PRE_UPDATE_RECORD, personSave));
|
||||
|
||||
tables.add(readOnly(owned(base(AssistantUsageDaily.TABLE_NAME, "Assistant Usage (Daily)", "query_stats", AssistantUsageDaily.class), true))
|
||||
.withUniqueKey(new UniqueKey("scope", "userId", "usageDate"))
|
||||
@@ -211,7 +219,8 @@ public class AssistantMetaDataBuilder
|
||||
.withCustomizer(TableCustomizers.POST_QUERY_RECORD, scheduledCustomizer));
|
||||
tables.add(readOnly(owned(base(AssistantScheduledRun.TABLE_NAME, "Scheduled runs", "history", AssistantScheduledRun.class), false))
|
||||
.withoutCapabilities(Capability.TABLE_DELETE)
|
||||
.withPermissionRules(new QPermissionRules().withLevel(PermissionLevel.NOT_PROTECTED))
|
||||
.withPermissionRules(new QPermissionRules().withLevel(PermissionLevel.NOT_PROTECTED)
|
||||
.withCustomPermissionChecker(new QCodeReference(AssistantWrittenByTheAssistantPermissionChecker.class)))
|
||||
.withUniqueKey(new UniqueKey("taskId", "scheduledFor"))
|
||||
.withRecordLabelFormat("Run #%s").withRecordLabelFields("id"));
|
||||
|
||||
@@ -261,6 +270,7 @@ public class AssistantMetaDataBuilder
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
tables.add(readOnly(owned(base(AssistantPendingActionClaim.TABLE_NAME, "Assistant Pending Action Claim", "task_alt", AssistantPendingActionClaim.class), false))
|
||||
.withUniqueKey(new UniqueKey("pendingActionId"))
|
||||
.withCustomizer(TableCustomizers.PRE_INSERT_RECORD, insertOnlyDates)
|
||||
.withRecordLabelFormat("%s #%s").withRecordLabelFields("decision", "pendingActionId"));
|
||||
|
||||
tables.add(readOnly(owned(base(AssistantDataAudit.TABLE_NAME, "Assistant Data Audit", "shield", AssistantDataAudit.class), false))
|
||||
@@ -301,8 +311,18 @@ public class AssistantMetaDataBuilder
|
||||
.withCustomizer(TableCustomizers.PRE_UPDATE_RECORD, budgetCustomizer));
|
||||
tables.add(readOnly(base(AssistantBudgetEvent.TABLE_NAME, "Budget Event", "notifications", AssistantBudgetEvent.class))
|
||||
.withoutCapabilities(Capability.TABLE_DELETE)
|
||||
.withCustomizer(TableCustomizers.PRE_INSERT_RECORD, insertOnlyDates)
|
||||
.withUniqueKey(new UniqueKey("budgetId", "scopeKey", "periodStart", "state")));
|
||||
tables.forEach(AssistantRecordScreens::apply);
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// who writes each table: a table the assistant writes offers a person no //
|
||||
// insert, update or delete (and a table nobody decided about is not built) //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
for(QTableMetaData table : tables)
|
||||
{
|
||||
AssistantTableWrites.apply(table);
|
||||
}
|
||||
return (tables);
|
||||
}
|
||||
|
||||
@@ -491,7 +511,8 @@ public class AssistantMetaDataBuilder
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Only the qbit writes these (admins may still delete rows).
|
||||
** Only the qbit writes these. AssistantTableWrites is where that is decided
|
||||
** for every table, and enforced (it also takes the delete capability away).
|
||||
*******************************************************************************/
|
||||
private static QTableMetaData readOnly(QTableMetaData table)
|
||||
{
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.actions.customizers.TableCustomizerInterface;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.AbstractActionInput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** For a settings table that has no rules of its own to check: a person's save
|
||||
** sets only what a person decides. The fields the server decides (the id, the
|
||||
** dates, every field that is not editable) are taken out of it, as they are
|
||||
** for every other settings table by its own customizer. See
|
||||
** {@link StoredRecords#ofSave}.
|
||||
*******************************************************************************/
|
||||
public class AssistantPersonSaveCustomizer implements TableCustomizerInterface
|
||||
{
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public List<QRecord> preInsertOrUpdate(AbstractActionInput input, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
|
||||
{
|
||||
StoredRecords.ofSave(input, records, oldRecordList);
|
||||
return (records);
|
||||
}
|
||||
}
|
||||
+4
-9
@@ -17,10 +17,7 @@
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import com.kingsrook.qbits.assistant.AssistantRuntime;
|
||||
import com.kingsrook.qbits.assistant.engine.ProfileService;
|
||||
@@ -57,13 +54,11 @@ public class AssistantProfileCustomizer implements TableCustomizerInterface
|
||||
@Override
|
||||
public List<QRecord> preInsertOrUpdate(AbstractActionInput input, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
|
||||
{
|
||||
String table = tableName(input);
|
||||
Optional<Map<Serializable, QRecord>> old = oldRecordListToMap("id", oldRecordList);
|
||||
String table = tableName(input);
|
||||
StoredRecords stored = StoredRecords.ofSave(input, records, oldRecordList);
|
||||
for(QRecord record : records)
|
||||
{
|
||||
QRecord merged = new QRecord();
|
||||
old.map(m -> m.get(record.getValue("id"))).ifPresent(o -> merged.setValues(new LinkedHashMap<>(o.getValues())));
|
||||
record.getValues().forEach(merged::setValue);
|
||||
QRecord merged = stored.merged(record);
|
||||
|
||||
if(AssistantProfile.TABLE_NAME.equals(table))
|
||||
{
|
||||
@@ -139,7 +134,7 @@ public class AssistantProfileCustomizer implements TableCustomizerInterface
|
||||
if(Boolean.TRUE.equals(record.getValueBoolean("isDefault")) && record.getValue("id") != null && record.getErrors().isEmpty())
|
||||
{
|
||||
for(AssistantProfile other : runtime.getStore().query(AssistantProfile.TABLE_NAME,
|
||||
new QQueryFilter().withCriteria("isDefault", QCriteriaOperator.EQUALS, true).withCriteria("id", QCriteriaOperator.NOT_EQUALS, record.getValue("id")), AssistantProfile.class))
|
||||
new QQueryFilter().withCriteria("isDefault", QCriteriaOperator.EQUALS, true).withCriteria("id", QCriteriaOperator.NOT_EQUALS, record.getValueInteger("id")), AssistantProfile.class))
|
||||
{
|
||||
runtime.getStore().updateFields(AssistantProfile.TABLE_NAME, new QRecord().withValue("id", other.getId()).withValue("isDefault", false));
|
||||
}
|
||||
|
||||
+6
-10
@@ -18,10 +18,8 @@ package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import com.kingsrook.qbits.assistant.AssistantRuntime;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantRagSource;
|
||||
@@ -50,7 +48,7 @@ public class AssistantRagSourceCustomizer implements TableCustomizerInterface
|
||||
@Override
|
||||
public List<QRecord> preInsertOrUpdate(AbstractActionInput input, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
|
||||
{
|
||||
Optional<Map<Serializable, QRecord>> old = oldRecordListToMap("id", oldRecordList);
|
||||
StoredRecords stored = StoredRecords.ofSave(input, records, oldRecordList);
|
||||
AssistantRuntime runtime = AssistantRuntime.of(QContext.getQInstance());
|
||||
for(QRecord record : records)
|
||||
{
|
||||
@@ -58,9 +56,7 @@ public class AssistantRagSourceCustomizer implements TableCustomizerInterface
|
||||
{
|
||||
continue;
|
||||
}
|
||||
QRecord merged = new QRecord();
|
||||
old.map(m -> m.get(record.getValue("id"))).ifPresent(o -> merged.setValues(new LinkedHashMap<>(o.getValues())));
|
||||
record.getValues().forEach(merged::setValue);
|
||||
QRecord merged = stored.merged(record);
|
||||
for(var entry : Map.<String, Serializable>of("enabled", true, "chunkChars", 1500, "chunkOverlapChars", 200).entrySet())
|
||||
{
|
||||
if(merged.getValue(entry.getKey()) == null)
|
||||
@@ -88,14 +84,14 @@ public class AssistantRagSourceCustomizer implements TableCustomizerInterface
|
||||
@Override
|
||||
public List<QRecord> postInsertOrUpdate(AbstractActionInput input, List<QRecord> records, Optional<List<QRecord>> oldRecordList) throws QException
|
||||
{
|
||||
Optional<Map<Serializable, QRecord>> old = oldRecordListToMap("id", oldRecordList);
|
||||
StoredRecords stored = StoredRecords.of(input, oldRecordList);
|
||||
AssistantRuntime runtime = AssistantRuntime.of(QContext.getQInstance());
|
||||
if(runtime != null && old.isPresent())
|
||||
if(runtime != null && oldRecordList.isPresent())
|
||||
{
|
||||
for(QRecord record : records)
|
||||
{
|
||||
QRecord previous = old.get().get(record.getValue("id"));
|
||||
if(previous != null && SEMANTIC_FIELDS.stream().anyMatch(name -> record.getValues().containsKey(name) && !Objects.equals(previous.getValue(name), record.getValue(name))))
|
||||
QRecord previous = stored.of(record);
|
||||
if(previous != null && SEMANTIC_FIELDS.stream().anyMatch(name -> record.getValues().containsKey(name) && stored.differs(previous, record, name)))
|
||||
{
|
||||
runtime.getRagService().clear(new AssistantRagSource(previous));
|
||||
}
|
||||
|
||||
+5
-9
@@ -22,10 +22,8 @@ package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import com.kingsrook.qbits.assistant.AssistantRuntime;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantScheduledTask;
|
||||
@@ -52,7 +50,7 @@ public class AssistantScheduledTaskCustomizer implements TableCustomizerInterfac
|
||||
@Override
|
||||
public List<QRecord> preInsertOrUpdate(AbstractActionInput input, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
|
||||
{
|
||||
Optional<Map<Serializable, QRecord>> old = oldRecordListToMap("id", oldRecordList);
|
||||
StoredRecords stored = StoredRecords.ofSave(input, records, oldRecordList);
|
||||
AssistantRuntime runtime = AssistantRuntime.of(QContext.getQInstance());
|
||||
for(QRecord record : records)
|
||||
{
|
||||
@@ -62,14 +60,12 @@ public class AssistantScheduledTaskCustomizer implements TableCustomizerInterfac
|
||||
{
|
||||
continue;
|
||||
}
|
||||
QRecord previous = old.map(rows -> rows.get(record.getValue("id"))).orElse(null);
|
||||
QRecord previous = stored.of(record);
|
||||
for(String field : List.of("writeModeApprovedBy", "writeModeApprovedAt", "writeModeApprovalHash", "nextRunAt", "lastRunAt", "lastRunStatus", "lastRunError"))
|
||||
{
|
||||
record.getValues().remove(field);
|
||||
}
|
||||
QRecord merged = new QRecord();
|
||||
merged.setValues(previous == null ? new LinkedHashMap<>() : new LinkedHashMap<>(previous.getValues()));
|
||||
record.getValues().forEach(merged::setValue);
|
||||
QRecord merged = stored.merged(record);
|
||||
for(var entry : Map.<String, Serializable>of("enabled", false, "timezone", "UTC", "outputMode", "RECORD", "writeModeRequested", false).entrySet())
|
||||
{
|
||||
if(merged.getValue(entry.getKey()) == null)
|
||||
@@ -78,7 +74,7 @@ public class AssistantScheduledTaskCustomizer implements TableCustomizerInterfac
|
||||
record.setValue(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
if(previous == null || ScheduledTasks.APPROVAL_FIELDS.stream().anyMatch(field -> !Objects.equals(previous.getValue(field), merged.getValue(field))))
|
||||
if(previous == null || ScheduledTasks.APPROVAL_FIELDS.stream().anyMatch(field -> stored.differs(previous, merged, field)))
|
||||
{
|
||||
record.setValue("writeModeApprovedBy", null);
|
||||
record.setValue("writeModeApprovedAt", null);
|
||||
@@ -101,7 +97,7 @@ public class AssistantScheduledTaskCustomizer implements TableCustomizerInterfac
|
||||
throw (new QException("The per-run cost cap cannot be negative."));
|
||||
}
|
||||
var next = ScheduledTasks.next(task, runtime.getClock().instant());
|
||||
if(previous == null || List.of("enabled", "cronExpression", "timezone").stream().anyMatch(field -> !Objects.equals(previous.getValue(field), merged.getValue(field))))
|
||||
if(previous == null || List.of("enabled", "cronExpression", "timezone").stream().anyMatch(field -> stored.differs(previous, merged, field)))
|
||||
{
|
||||
record.setValue("nextRunAt", Boolean.TRUE.equals(task.getEnabled()) ? next : null);
|
||||
}
|
||||
|
||||
+9
-13
@@ -79,6 +79,7 @@ public class AssistantSettingsCustomizer implements TableCustomizerInterface
|
||||
public List<QRecord> preInsertOrUpdate(AbstractActionInput input, List<QRecord> records, boolean isPreview, Optional<List<QRecord>> oldRecordList) throws QException
|
||||
{
|
||||
ProviderService service = service();
|
||||
StoredRecords stored = StoredRecords.ofSave(input, records, oldRecordList);
|
||||
if(AssistantModel.TABLE_NAME.equals(tableName(input)))
|
||||
{
|
||||
for(QRecord record : records)
|
||||
@@ -107,7 +108,7 @@ public class AssistantSettingsCustomizer implements TableCustomizerInterface
|
||||
}
|
||||
}
|
||||
}
|
||||
validateFallbacks(records, oldRecordList);
|
||||
validateFallbacks(input, records, oldRecordList);
|
||||
return (records);
|
||||
}
|
||||
if(service == null || !AssistantProvider.TABLE_NAME.equals(tableName(input)))
|
||||
@@ -115,7 +116,6 @@ public class AssistantSettingsCustomizer implements TableCustomizerInterface
|
||||
return (records);
|
||||
}
|
||||
|
||||
Optional<Map<Serializable, QRecord>> old = oldRecordListToMap("id", oldRecordList);
|
||||
for(QRecord record : records)
|
||||
{
|
||||
record.removeValue(ProviderService.KEY_STATUS_FIELD);
|
||||
@@ -124,9 +124,7 @@ public class AssistantSettingsCustomizer implements TableCustomizerInterface
|
||||
continue;
|
||||
}
|
||||
|
||||
QRecord merged = new QRecord();
|
||||
old.map(m -> m.get(record.getValue("id"))).ifPresent(o -> merged.setValues(new java.util.LinkedHashMap<>(o.getValues())));
|
||||
record.getValues().forEach(merged::setValue);
|
||||
QRecord merged = stored.merged(record);
|
||||
|
||||
AssistantProvider provider = new AssistantProvider(merged);
|
||||
if(merged.getValue("kind") == null)
|
||||
@@ -193,12 +191,12 @@ public class AssistantSettingsCustomizer implements TableCustomizerInterface
|
||||
}
|
||||
if(AssistantModel.TABLE_NAME.equals(tableName(input)) && oldRecordList.isPresent())
|
||||
{
|
||||
Map<Serializable, QRecord> previous = oldRecordListToMap("id", oldRecordList).orElse(Map.of());
|
||||
StoredRecords stored = StoredRecords.of(input, oldRecordList);
|
||||
for(QRecord record : records)
|
||||
{
|
||||
QRecord before = previous.get(record.getValue("id"));
|
||||
QRecord before = stored.of(record);
|
||||
if(before != null && List.of("providerId", "modelId", "modelKind", "embeddingDimensions").stream()
|
||||
.anyMatch(name -> record.getValues().containsKey(name) && !java.util.Objects.equals(before.getValue(name), record.getValue(name))))
|
||||
.anyMatch(name -> record.getValues().containsKey(name) && stored.differs(before, record, name)))
|
||||
{
|
||||
for(var source : runtime.getRagService().sources())
|
||||
{
|
||||
@@ -290,7 +288,7 @@ public class AssistantSettingsCustomizer implements TableCustomizerInterface
|
||||
/*******************************************************************************
|
||||
** Validate the resulting graph, including all changes in this save batch.
|
||||
*******************************************************************************/
|
||||
private void validateFallbacks(List<QRecord> records, Optional<List<QRecord>> oldRecords) throws QException
|
||||
private void validateFallbacks(AbstractActionInput input, List<QRecord> records, Optional<List<QRecord>> oldRecords) throws QException
|
||||
{
|
||||
AssistantRuntime runtime = AssistantRuntime.of(QContext.getQInstance());
|
||||
if(runtime == null)
|
||||
@@ -302,13 +300,11 @@ public class AssistantSettingsCustomizer implements TableCustomizerInterface
|
||||
{
|
||||
models.put(model.getId(), model);
|
||||
}
|
||||
Optional<Map<Serializable, QRecord>> old = oldRecordListToMap("id", oldRecords);
|
||||
StoredRecords stored = StoredRecords.of(input, oldRecords);
|
||||
List<AssistantModel> proposed = new java.util.ArrayList<>();
|
||||
for(QRecord record : records)
|
||||
{
|
||||
QRecord merged = new QRecord();
|
||||
old.map(m -> m.get(record.getValue("id"))).ifPresent(o -> merged.setValues(new java.util.LinkedHashMap<>(o.getValues())));
|
||||
record.getValues().forEach(merged::setValue);
|
||||
QRecord merged = stored.merged(record);
|
||||
AssistantModel model = new AssistantModel(merged);
|
||||
proposed.add(model);
|
||||
if(model.getId() != null)
|
||||
|
||||
+294
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudget;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudgetEvent;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudgetOverride;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudgetUsage;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantClientError;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantConversation;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantDataAccess;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantDataAudit;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantFeedback;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantMessage;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantModel;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantModelPrice;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPendingAction;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPendingActionClaim;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPolicy;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantProfile;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantProfileSkill;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantProvider;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantRagSource;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantScheduledRun;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantScheduledTask;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantSkill;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantToolCall;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantTurn;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantTurnEvent;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantUsage;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantUsageDaily;
|
||||
import com.kingsrook.qbits.assistant.persistence.AssistantStore;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QPermissionDeniedException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.AbstractActionInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.replace.ReplaceInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.update.ConditionalUpdateInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.permissions.MetaDataWithPermissionRules;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.Capability;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Who writes each table of the assistant.
|
||||
**
|
||||
** Some tables are settings a person edits on a record screen: budgets,
|
||||
** providers, profiles. The others are written by the assistant itself as it
|
||||
** works: what was asked and answered, what a tool did, what a model call
|
||||
** cost. Those are the record of what happened, and the spending limit is
|
||||
** counted from them: a person changes none of them through a table route,
|
||||
** whoever the person is. An administrator who could edit or delete a row of
|
||||
** the accounting could give themselves any spending limit.
|
||||
**
|
||||
** Every table has a decision here, by operation. A table without one cannot
|
||||
** be built ({@link #apply}), so a new table is decided when it is added. The
|
||||
** decision is enforced by the assistant itself, in two ways that need nothing
|
||||
** of the QQQ version underneath:
|
||||
**
|
||||
** - the table does not have the capability (insert, update, delete) a person
|
||||
** may not use, so the metadata a screen reads offers no button for it and
|
||||
** QQQ defines no bulk action for it;
|
||||
** - the permission checker of every table refuses the write
|
||||
** ({@link #refuseWhatAPersonMayNotWrite}): a table route asks it before it
|
||||
** does anything, with the insert, update or delete it is about to make.
|
||||
**
|
||||
** The assistant's own writes are not asked: they are made by
|
||||
** {@link AssistantStore}, in code, never through a table route.
|
||||
*******************************************************************************/
|
||||
public final class AssistantTableWrites
|
||||
{
|
||||
/*******************************************************************************
|
||||
** What a table route can do to a row.
|
||||
*******************************************************************************/
|
||||
public enum Operation
|
||||
{
|
||||
INSERT(Capability.TABLE_INSERT, "added"),
|
||||
UPDATE(Capability.TABLE_UPDATE, "changed"),
|
||||
DELETE(Capability.TABLE_DELETE, "deleted");
|
||||
|
||||
private final Capability capability;
|
||||
private final String done;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
Operation(Capability capability, String done)
|
||||
{
|
||||
this.capability = capability;
|
||||
this.done = done;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static final Set<Operation> EVERYTHING = Set.copyOf(EnumSet.allOf(Operation.class));
|
||||
private static final Set<Operation> NOTHING = Set.of();
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
// what a person does to each table through a table route. everything else is done by //
|
||||
// the assistant (AssistantStore), in code. //
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
private static final Map<String, Set<Operation>> BY_A_PERSON = Map.ofEntries(
|
||||
////////////////////////////////////////////////////////
|
||||
// settings: an administrator's, on its record screen //
|
||||
////////////////////////////////////////////////////////
|
||||
Map.entry(AssistantPolicy.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantProvider.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantModel.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantModelPrice.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantProfile.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantSkill.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantProfileSkill.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantDataAccess.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantRagSource.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantScheduledTask.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantBudget.TABLE_NAME, EVERYTHING),
|
||||
Map.entry(AssistantBudgetOverride.TABLE_NAME, EVERYTHING),
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
// a conversation is started by the assistant (a question is asked through its own //
|
||||
// API); its owner renames, pins, archives and deletes it, also on a record screen //
|
||||
// (AssistantConversationCustomizer says which fields, and deletes what belongs to //
|
||||
// a deleted conversation) //
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
Map.entry(AssistantConversation.TABLE_NAME, Set.of(Operation.UPDATE, Operation.DELETE)),
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
// what was said and done: written through the assistant's own API and its turn //
|
||||
// engine. a rating and a client error are sent to the assistant's API too, not //
|
||||
// to a table route. //
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
Map.entry(AssistantMessage.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantTurn.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantTurnEvent.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantToolCall.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantPendingAction.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantPendingActionClaim.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantFeedback.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantClientError.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantDataAudit.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantScheduledRun.TABLE_NAME, NOTHING),
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// what was spent: the spending limits are counted from these //
|
||||
//////////////////////////////////////////////////////////////////
|
||||
Map.entry(AssistantUsage.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantUsageDaily.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantBudgetUsage.TABLE_NAME, NOTHING),
|
||||
Map.entry(AssistantBudgetEvent.TABLE_NAME, NOTHING));
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Utility class - no instances.
|
||||
*******************************************************************************/
|
||||
private AssistantTableWrites()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** What a person may do to a table through a table route; null for a table
|
||||
** that is not the assistant's.
|
||||
*******************************************************************************/
|
||||
public static Set<Operation> byAPerson(String tableName)
|
||||
{
|
||||
return (BY_A_PERSON.get(tableName));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Whether a person edits this table on a record screen (anything at all).
|
||||
*******************************************************************************/
|
||||
public static boolean isEditedByAPerson(String tableName)
|
||||
{
|
||||
Set<Operation> operations = byAPerson(tableName);
|
||||
return (operations != null && !operations.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Take from a table every capability a person may not use on it. A table
|
||||
** nobody decided about is not built.
|
||||
*******************************************************************************/
|
||||
static QTableMetaData apply(QTableMetaData table) throws QException
|
||||
{
|
||||
Set<Operation> byAPerson = byAPerson(table.getName());
|
||||
if(byAPerson == null)
|
||||
{
|
||||
throw (new QException("The table " + table.getName() + " has no decision in AssistantTableWrites: say who writes it, a person on a record screen or the assistant."));
|
||||
}
|
||||
for(Operation operation : Operation.values())
|
||||
{
|
||||
if(!byAPerson.contains(operation))
|
||||
{
|
||||
table.withoutCapability(operation.capability);
|
||||
}
|
||||
}
|
||||
return (table);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Refuse an insert, update or delete of a table of the assistant that a
|
||||
** person may not make. Called by the permission checkers of the assistant's
|
||||
** tables, which a table route asks before it does anything. Anything else
|
||||
** (a read, another kind of object, a table that is not the assistant's,
|
||||
** the assistant's own write) is not refused here.
|
||||
*******************************************************************************/
|
||||
static void refuseWhatAPersonMayNotWrite(AbstractActionInput input, MetaDataWithPermissionRules metaData) throws QPermissionDeniedException
|
||||
{
|
||||
Set<Operation> asked = operations(input);
|
||||
if(asked.isEmpty() || !(metaData instanceof QTableMetaData table) || AssistantStore.isOwnWrite(input))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Set<Operation> byAPerson = byAPerson(table.getName());
|
||||
if(byAPerson == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
for(Operation operation : Operation.values())
|
||||
{
|
||||
if(asked.contains(operation) && !byAPerson.contains(operation))
|
||||
{
|
||||
throw (new QPermissionDeniedException("Rows of " + label(table) + " are written by the assistant; they cannot be " + operation.done + "."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The operations an action input is about to make; none for a read.
|
||||
*******************************************************************************/
|
||||
private static Set<Operation> operations(AbstractActionInput input)
|
||||
{
|
||||
if(input instanceof InsertInput)
|
||||
{
|
||||
return (Set.of(Operation.INSERT));
|
||||
}
|
||||
if(input instanceof UpdateInput || input instanceof ConditionalUpdateInput)
|
||||
{
|
||||
return (Set.of(Operation.UPDATE));
|
||||
}
|
||||
if(input instanceof DeleteInput)
|
||||
{
|
||||
return (Set.of(Operation.DELETE));
|
||||
}
|
||||
if(input instanceof ReplaceInput)
|
||||
{
|
||||
return (EVERYTHING);
|
||||
}
|
||||
return (NOTHING);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static String label(QTableMetaData table)
|
||||
{
|
||||
return (table.getLabel() == null ? table.getName() : table.getLabel());
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.actions.permissions.CustomPermissionChecker;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QPermissionDeniedException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.AbstractActionInput;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.permissions.MetaDataWithPermissionRules;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** For a table of the assistant that needs no permission to read (which rows a
|
||||
** person sees is decided by the owner record lock): anybody reads, and nobody
|
||||
** writes what the assistant writes. See {@link AssistantTableWrites}.
|
||||
*******************************************************************************/
|
||||
public class AssistantWrittenByTheAssistantPermissionChecker implements CustomPermissionChecker
|
||||
{
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public void checkPermissionsThrowing(AbstractActionInput actionInput, MetaDataWithPermissionRules metaDataWithPermissionRules) throws QPermissionDeniedException
|
||||
{
|
||||
AssistantTableWrites.refuseWhatAPersonMayNotWrite(actionInput, metaDataWithPermissionRules);
|
||||
}
|
||||
}
|
||||
+280
@@ -0,0 +1,280 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.model.actions.AbstractActionInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.AbstractTableActionInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.QInputSource;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.DynamicDefaultValueBehavior;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.statusmessages.BadInputStatusMessage;
|
||||
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The stored rows QQQ hands a table customizer with an update, matched to
|
||||
** the records being saved.
|
||||
**
|
||||
** A record being saved carries what its caller put in it. A save from a
|
||||
** screen carries text: the primary key comes from the request's path, and
|
||||
** every value of a multipart body is a string. The stored row carries the
|
||||
** types of the table's fields. So a record is never matched to its stored row
|
||||
** by comparing the two keys as they come, and a value being saved is never
|
||||
** compared with a stored one as it comes: both are first read as the type of
|
||||
** their field. This is the one place the customizers do either.
|
||||
*******************************************************************************/
|
||||
final class StoredRecords
|
||||
{
|
||||
private static final String DEFAULT_KEY_FIELD = "id";
|
||||
|
||||
private final QTableMetaData table;
|
||||
private final String keyField;
|
||||
private final Map<Serializable, QRecord> byKey = new HashMap<>();
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private StoredRecords(QTableMetaData table, List<QRecord> stored)
|
||||
{
|
||||
this.table = table;
|
||||
this.keyField = table == null || table.getPrimaryKeyField() == null ? DEFAULT_KEY_FIELD : table.getPrimaryKeyField();
|
||||
for(QRecord row : stored)
|
||||
{
|
||||
Serializable key = key(row);
|
||||
if(key != null)
|
||||
{
|
||||
byKey.put(key, row);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The stored rows of this insert or update (none for an insert).
|
||||
*******************************************************************************/
|
||||
static StoredRecords of(AbstractActionInput input, Optional<List<QRecord>> oldRecordList)
|
||||
{
|
||||
QTableMetaData table = input instanceof AbstractTableActionInput tableInput ? tableInput.getTable() : null;
|
||||
return (new StoredRecords(table, oldRecordList.orElse(List.of())));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The stored rows of a save that is about to be made, after taking out of
|
||||
** a person's records what a person does not decide.
|
||||
**
|
||||
** A field that is not editable is the server's: an id, a date, who granted
|
||||
** an override, a test result, a counter. QQQ's record screens do not offer
|
||||
** such a field, but a request can carry any field, and QQQ stores what a
|
||||
** request carries. So for a save made by a person (its input source is
|
||||
** USER, which every table route sets) every value of a field that is not
|
||||
** editable is taken out of the record before a rule sees it or it is
|
||||
** stored: the stored value stays. A client that sends a whole record back
|
||||
** keeps working, and what it sent for such a field changes nothing.
|
||||
**
|
||||
** Left in: the primary key of an update (it names the row; the route takes
|
||||
** it from the path), and the dates QQQ itself has just set on this save
|
||||
** (the modify date; on an insert the create date too). The assistant's own
|
||||
** saves are not touched: they set these fields.
|
||||
*******************************************************************************/
|
||||
static StoredRecords ofSave(AbstractActionInput input, List<QRecord> records, Optional<List<QRecord>> oldRecordList)
|
||||
{
|
||||
StoredRecords stored = of(input, oldRecordList);
|
||||
if(stored.table != null && input instanceof AbstractTableActionInput tableInput && tableInput.getInputSource() == QInputSource.USER)
|
||||
{
|
||||
boolean update = input instanceof UpdateInput;
|
||||
for(QRecord record : records)
|
||||
{
|
||||
for(String fieldName : new ArrayList<>(record.getValues().keySet()))
|
||||
{
|
||||
if(stored.isTheServers(fieldName, update))
|
||||
{
|
||||
record.removeValue(fieldName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (stored);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Whether a person's save may not set this field.
|
||||
*******************************************************************************/
|
||||
private boolean isTheServers(String fieldName, boolean update)
|
||||
{
|
||||
QFieldMetaData field = table.getFields().get(fieldName);
|
||||
if(field == null || field.getIsEditable() || (update && fieldName.equals(keyField)))
|
||||
{
|
||||
return (false);
|
||||
}
|
||||
DynamicDefaultValueBehavior setByQqq = field.getBehaviorOnlyIfSet(DynamicDefaultValueBehavior.class);
|
||||
return (!(setByQqq == DynamicDefaultValueBehavior.MODIFY_DATE || (!update && setByQqq == DynamicDefaultValueBehavior.CREATE_DATE)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The stored row of a record being saved, or null: for an insert, and for
|
||||
** a key that is no row QQQ read.
|
||||
*******************************************************************************/
|
||||
QRecord of(QRecord record)
|
||||
{
|
||||
Serializable key = key(record);
|
||||
return (key == null ? null : byKey.get(key));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The record as it will be stored: the stored row, with the values being
|
||||
** saved laid over it, each as the type of its field. This is what a rule
|
||||
** is judged on, so that a save of one field is held to the same rules as a
|
||||
** save of the whole record. A value that cannot be read as the type of its
|
||||
** field is refused on the record being saved, and the stored value is kept
|
||||
** here.
|
||||
*******************************************************************************/
|
||||
QRecord merged(QRecord record)
|
||||
{
|
||||
QRecord merged = new QRecord();
|
||||
QRecord stored = of(record);
|
||||
if(stored != null)
|
||||
{
|
||||
merged.setValues(new LinkedHashMap<>(stored.getValues()));
|
||||
}
|
||||
for(Map.Entry<String, Serializable> entry : new ArrayList<>(record.getValues().entrySet()))
|
||||
{
|
||||
try
|
||||
{
|
||||
merged.setValue(entry.getKey(), typed(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
catch(RuntimeException e)
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// the value itself is not repeated: it may be something to keep safe //
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
record.addError(new BadInputStatusMessage("The value given for " + label(entry.getKey()) + " is not valid."));
|
||||
}
|
||||
}
|
||||
return (merged);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Whether two records differ in a field, each value read as the type of
|
||||
** the field (so text and the number it spells are the same, and so are two
|
||||
** decimals of different scale).
|
||||
*******************************************************************************/
|
||||
boolean differs(QRecord one, QRecord other, String fieldName)
|
||||
{
|
||||
return (!same(readable(fieldName, one.getValue(fieldName)), readable(fieldName, other.getValue(fieldName))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static boolean same(Serializable one, Serializable other)
|
||||
{
|
||||
if(one instanceof BigDecimal oneDecimal && other instanceof BigDecimal otherDecimal)
|
||||
{
|
||||
return (oneDecimal.compareTo(otherDecimal) == 0);
|
||||
}
|
||||
return (Objects.equals(one, other));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A key as the type of the key field; null when it is no key of this table.
|
||||
*******************************************************************************/
|
||||
private Serializable key(QRecord record)
|
||||
{
|
||||
Serializable key = readable(keyField, record.getValue(keyField));
|
||||
return (key instanceof Unreadable ? null : key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A value as the type of its field, or a marker that equals nothing when
|
||||
** it cannot be read as that.
|
||||
*******************************************************************************/
|
||||
private Serializable readable(String fieldName, Serializable value)
|
||||
{
|
||||
try
|
||||
{
|
||||
return (typed(fieldName, value));
|
||||
}
|
||||
catch(RuntimeException e)
|
||||
{
|
||||
return (new Unreadable());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A value as the type of its field. A name that is no field of the table
|
||||
** is left as it is.
|
||||
*******************************************************************************/
|
||||
private Serializable typed(String fieldName, Serializable value)
|
||||
{
|
||||
QFieldMetaData field = table == null ? null : table.getFields().get(fieldName);
|
||||
return (field == null || value == null ? value : ValueUtils.getValueAsFieldType(field.getType(), value));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private String label(String fieldName)
|
||||
{
|
||||
QFieldMetaData field = table == null ? null : table.getFields().get(fieldName);
|
||||
return (field == null || field.getLabel() == null ? fieldName : field.getLabel());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A value that could not be read as the type of its field.
|
||||
*******************************************************************************/
|
||||
private static final class Unreadable implements Serializable
|
||||
{
|
||||
}
|
||||
}
|
||||
+34
@@ -59,6 +59,9 @@ public class AssistantBudgetEvent extends QRecordEntity
|
||||
@QField(isEditable = false)
|
||||
private Instant createDate;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant modifyDate;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Default constructor.
|
||||
@@ -356,4 +359,35 @@ public class AssistantBudgetEvent extends QRecordEntity
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for modifyDate
|
||||
*******************************************************************************/
|
||||
public Instant getModifyDate()
|
||||
{
|
||||
return (this.modifyDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public void setModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public AssistantBudgetEvent withModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
+68
@@ -66,6 +66,12 @@ public class AssistantBudgetUsage extends QRecordEntity
|
||||
@QField()
|
||||
private Long cacheWriteTokens;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant createDate;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant modifyDate;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Default constructor.
|
||||
@@ -425,4 +431,66 @@ public class AssistantBudgetUsage extends QRecordEntity
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for createDate
|
||||
*******************************************************************************/
|
||||
public Instant getCreateDate()
|
||||
{
|
||||
return (this.createDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for createDate
|
||||
*******************************************************************************/
|
||||
public void setCreateDate(Instant createDate)
|
||||
{
|
||||
this.createDate = createDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for createDate
|
||||
*******************************************************************************/
|
||||
public AssistantBudgetUsage withCreateDate(Instant createDate)
|
||||
{
|
||||
this.createDate = createDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for modifyDate
|
||||
*******************************************************************************/
|
||||
public Instant getModifyDate()
|
||||
{
|
||||
return (this.modifyDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public void setModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public AssistantBudgetUsage withModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -85,6 +85,9 @@ public class AssistantClientError extends QRecordEntity
|
||||
@QField(isEditable = false)
|
||||
private Instant createDate;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant modifyDate;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@@ -631,4 +634,35 @@ public class AssistantClientError extends QRecordEntity
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for modifyDate
|
||||
*******************************************************************************/
|
||||
public Instant getModifyDate()
|
||||
{
|
||||
return (this.modifyDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public void setModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public AssistantClientError withModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -72,6 +72,9 @@ public class AssistantDataAudit extends QRecordEntity
|
||||
@QField(isEditable = false)
|
||||
private Instant createDate;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant modifyDate;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@@ -477,4 +480,33 @@ public class AssistantDataAudit extends QRecordEntity
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for modifyDate
|
||||
*******************************************************************************/
|
||||
public Instant getModifyDate()
|
||||
{
|
||||
return (this.modifyDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public void setModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public AssistantDataAudit withModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -61,6 +61,9 @@ public class AssistantFeedback extends QRecordEntity
|
||||
@QField(isEditable = false)
|
||||
private Instant createDate;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant modifyDate;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@@ -328,4 +331,35 @@ public class AssistantFeedback extends QRecordEntity
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for modifyDate
|
||||
*******************************************************************************/
|
||||
public Instant getModifyDate()
|
||||
{
|
||||
return (this.modifyDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public void setModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public AssistantFeedback withModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -50,6 +50,9 @@ public class AssistantPendingActionClaim extends QRecordEntity
|
||||
@QField(isEditable = false)
|
||||
private Instant createDate;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant modifyDate;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@@ -224,4 +227,35 @@ public class AssistantPendingActionClaim extends QRecordEntity
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for modifyDate
|
||||
*******************************************************************************/
|
||||
public Instant getModifyDate()
|
||||
{
|
||||
return (this.modifyDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public void setModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public AssistantPendingActionClaim withModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -109,6 +109,9 @@ public class AssistantToolCall extends QRecordEntity
|
||||
@QField(isEditable = false)
|
||||
private Instant createDate;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant modifyDate;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@@ -837,4 +840,36 @@ public class AssistantToolCall extends QRecordEntity
|
||||
this.clientChannel = clientChannel;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for modifyDate
|
||||
*******************************************************************************/
|
||||
public Instant getModifyDate()
|
||||
{
|
||||
return (this.modifyDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public void setModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public AssistantToolCall withModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -59,6 +59,9 @@ public class AssistantTurnEvent extends QRecordEntity
|
||||
@QField(isEditable = false)
|
||||
private Instant createDate;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant modifyDate;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@@ -295,4 +298,35 @@ public class AssistantTurnEvent extends QRecordEntity
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for modifyDate
|
||||
*******************************************************************************/
|
||||
public Instant getModifyDate()
|
||||
{
|
||||
return (this.modifyDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public void setModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public AssistantTurnEvent withModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -90,6 +90,9 @@ public class AssistantUsage extends QRecordEntity
|
||||
@QField(isEditable = false)
|
||||
private Instant createDate;
|
||||
|
||||
@QField(isEditable = false)
|
||||
private Instant modifyDate;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@@ -698,4 +701,35 @@ public class AssistantUsage extends QRecordEntity
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for modifyDate
|
||||
*******************************************************************************/
|
||||
public Instant getModifyDate()
|
||||
{
|
||||
return (this.modifyDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public void setModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for modifyDate
|
||||
*******************************************************************************/
|
||||
public AssistantUsage withModifyDate(Instant modifyDate)
|
||||
{
|
||||
this.modifyDate = modifyDate;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
+11
@@ -162,6 +162,17 @@ public class AssistantStore
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Whether an action input is one this store made (it carries the store's
|
||||
** own reason). A request cannot make one: the reason is set in code, here.
|
||||
*******************************************************************************/
|
||||
public static boolean isOwnWrite(AbstractActionInput input)
|
||||
{
|
||||
return (input != null && TRUSTED_REASON.equals(input.getPermissionBypassReason()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The store only ever touches the QBit's own tables (assistant_*): the
|
||||
** user's application data is reached only through the UI API.
|
||||
|
||||
@@ -115,6 +115,24 @@ class ArchitectureTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A customizer never pairs a record with its stored row itself. The map
|
||||
** QQQ offers for that is keyed by the stored key, and the key of a record
|
||||
** saved from a screen is text: a lookup by the record's own key misses
|
||||
** the row. StoredRecords does the pairing, for every customizer.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void customizersPairARecordWithItsStoredRowOnlyThroughStoredRecords()
|
||||
{
|
||||
ArchRule rule = noClasses().that().resideInAPackage(BASE)
|
||||
.should().callMethodWhere(com.tngtech.archunit.core.domain.JavaCall.Predicates.target(
|
||||
com.tngtech.archunit.core.domain.properties.HasName.Predicates.name("oldRecordListToMap")))
|
||||
.because("a record saved from a screen carries its key as text: metadata.StoredRecords pairs it with its stored row");
|
||||
rule.check(classes);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant;
|
||||
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import com.kingsrook.qbits.assistant.metadata.AssistantMetaDataBuilder;
|
||||
import com.kingsrook.qbits.assistant.testing.BaseAssistantTest;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The reference DDL (docs/schema-postgres.sql) gives every table the QBit
|
||||
** declares the columns create_date and modify_date, for a database made from
|
||||
** it (the creates, then the later additions, as the file says). The
|
||||
** PostgreSQL-only parts are run by SchemaForeignKeyPostgresTest.
|
||||
*******************************************************************************/
|
||||
class TableStandardSchemaTest extends BaseAssistantTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
** Every declared table has both columns after the reference DDL ran.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void everyDeclaredTableHasBothDateColumns() throws Exception
|
||||
{
|
||||
Set<String> declared = new HashSet<>();
|
||||
for(QTableMetaData table : new AssistantMetaDataBuilder(config).buildTables())
|
||||
{
|
||||
declared.add(table.getName());
|
||||
}
|
||||
|
||||
try(Connection connection = DriverManager.getConnection("jdbc:h2:mem:tablestandard" + System.nanoTime() + ";MODE=PostgreSQL;DEFAULT_NULL_ORDERING=HIGH"))
|
||||
{
|
||||
String ddl = Files.readString(Path.of("..", "docs", "schema-postgres.sql"), StandardCharsets.UTF_8);
|
||||
Pattern pattern = Pattern.compile("^(CREATE TABLE|ALTER TABLE)( IF NOT EXISTS)? (assistant_[a-z_]+)\\b(.*)", Pattern.DOTALL);
|
||||
try(Statement statement = connection.createStatement())
|
||||
{
|
||||
for(String sql : AssistantDefaultsChangelogTest.statements(ddl))
|
||||
{
|
||||
Matcher m = pattern.matcher(sql);
|
||||
if(m.find() && (m.group(1).startsWith("CREATE") || m.group(4).strip().startsWith("ADD COLUMN")))
|
||||
{
|
||||
statement.execute(sql.replace("TIMESTAMPTZ", "TIMESTAMP WITH TIME ZONE"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(String table : declared)
|
||||
{
|
||||
Set<String> columns = new HashSet<>();
|
||||
try(Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("SELECT LOWER(column_name) FROM information_schema.columns WHERE LOWER(table_name) = '" + table + "'"))
|
||||
{
|
||||
while(rs.next())
|
||||
{
|
||||
columns.add(rs.getString(1));
|
||||
}
|
||||
}
|
||||
assertThat(columns).as(table + " columns").contains("create_date", "modify_date");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+239
@@ -0,0 +1,239 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import com.kingsrook.qbits.assistant.api.AssistantConversationCustomizer;
|
||||
import com.kingsrook.qbits.assistant.metadata.AssistantTableWrites.Operation;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudget;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantConversation;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantToolCall;
|
||||
import com.kingsrook.qbits.assistant.testing.AssistantTestFixture;
|
||||
import com.kingsrook.qbits.assistant.testing.BaseAssistantTest;
|
||||
import com.kingsrook.qqq.backend.core.actions.customizers.QCodeLoader;
|
||||
import com.kingsrook.qqq.backend.core.actions.customizers.TableCustomizerInterface;
|
||||
import com.kingsrook.qqq.backend.core.actions.customizers.TableCustomizers;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QPermissionDeniedException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.QInputSource;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.replace.ReplaceInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.update.ConditionalUpdateInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.DynamicDefaultValueBehavior;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.Capability;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Every table of the assistant has a decision about who writes it, and the
|
||||
** table as it is built follows the decision. A table that is added without a
|
||||
** decision fails here, and cannot be built at all.
|
||||
*******************************************************************************/
|
||||
class AssistantTableWritesTest extends BaseAssistantTest
|
||||
{
|
||||
private static final Set<Class<?>> CHECKERS = Set.of(AssistantAdminPermissionChecker.class, AssistantAccessPermissionChecker.class, AssistantWrittenByTheAssistantPermissionChecker.class);
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Every table has a decision; a table without one is not built.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void everyTableHasADecisionAndIsBuiltToIt() throws Exception
|
||||
{
|
||||
List<QTableMetaData> tables = assistantTables();
|
||||
assertThat(tables).hasSizeGreaterThanOrEqualTo(27);
|
||||
|
||||
for(QTableMetaData table : tables)
|
||||
{
|
||||
Set<Operation> byAPerson = AssistantTableWrites.byAPerson(table.getName());
|
||||
assertThat(byAPerson).as(table.getName() + " has a decision in AssistantTableWrites").isNotNull();
|
||||
|
||||
assertThat(table.isCapabilityEnabled(qInstance.getBackend(table.getBackendName()), Capability.TABLE_INSERT)).as(table.getName() + " insert").isEqualTo(byAPerson.contains(Operation.INSERT));
|
||||
assertThat(table.isCapabilityEnabled(qInstance.getBackend(table.getBackendName()), Capability.TABLE_UPDATE)).as(table.getName() + " update").isEqualTo(byAPerson.contains(Operation.UPDATE));
|
||||
assertThat(table.isCapabilityEnabled(qInstance.getBackend(table.getBackendName()), Capability.TABLE_DELETE)).as(table.getName() + " delete").isEqualTo(byAPerson.contains(Operation.DELETE));
|
||||
|
||||
QCodeReference checker = table.getPermissionRules().getCustomPermissionChecker();
|
||||
assertThat(checker).as(table.getName() + " has a permission checker that refuses what a person may not write").isNotNull();
|
||||
assertThat(CHECKERS).as(table.getName()).anySatisfy(type -> assertThat(type.getName()).isEqualTo(checker.getName()));
|
||||
}
|
||||
|
||||
assertThatThrownBy(() -> AssistantTableWrites.apply(new QTableMetaData().withName("assistant_added_tomorrow")))
|
||||
.isInstanceOf(QException.class).hasMessageContaining("assistant_added_tomorrow has no decision");
|
||||
assertThat(AssistantTableWrites.byAPerson("page")).as("not a table of the assistant").isNull();
|
||||
assertThat(AssistantTableWrites.isEditedByAPerson("page")).isFalse();
|
||||
assertThat(AssistantTableWrites.isEditedByAPerson(AssistantBudget.TABLE_NAME)).isTrue();
|
||||
assertThat(AssistantTableWrites.isEditedByAPerson(AssistantToolCall.TABLE_NAME)).isFalse();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The checkers refuse every kind of write a table route can ask for, and
|
||||
** nothing else: a read, a table that is not the assistant's and the
|
||||
** assistant's own write are not refused.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void theCheckersRefuseEveryKindOfWriteAndNothingElse() throws Exception
|
||||
{
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(AssistantTestFixture.ADMIN, config));
|
||||
QTableMetaData toolCalls = qInstance.getTable(AssistantToolCall.TABLE_NAME);
|
||||
QTableMetaData conversations = qInstance.getTable(AssistantConversation.TABLE_NAME);
|
||||
QTableMetaData budgets = qInstance.getTable(AssistantBudget.TABLE_NAME);
|
||||
|
||||
for(var input : List.of(new InsertInput(toolCalls.getName()), new UpdateInput(toolCalls.getName()), new DeleteInput(toolCalls.getName()),
|
||||
new ReplaceInput(), new ConditionalUpdateInput(toolCalls.getName())))
|
||||
{
|
||||
assertThatThrownBy(() -> new AssistantAdminPermissionChecker().checkPermissionsThrowing(input, toolCalls)).as(input.getClass().getSimpleName())
|
||||
.isInstanceOf(QPermissionDeniedException.class).hasMessageContaining("Rows of Assistant Tool Call are written by the assistant; they cannot be");
|
||||
assertThatThrownBy(() -> new AssistantWrittenByTheAssistantPermissionChecker().checkPermissionsThrowing(input, toolCalls)).isInstanceOf(QPermissionDeniedException.class);
|
||||
}
|
||||
new AssistantAdminPermissionChecker().checkPermissionsThrowing(new QueryInput(toolCalls.getName()), toolCalls);
|
||||
new AssistantAdminPermissionChecker().checkPermissionsThrowing(null, toolCalls);
|
||||
|
||||
new AssistantAccessPermissionChecker().checkPermissionsThrowing(new UpdateInput(conversations.getName()), conversations);
|
||||
new AssistantAccessPermissionChecker().checkPermissionsThrowing(new DeleteInput(conversations.getName()), conversations);
|
||||
assertThatThrownBy(() -> new AssistantAccessPermissionChecker().checkPermissionsThrowing(new InsertInput(conversations.getName()), conversations))
|
||||
.hasMessage("Rows of Assistant Conversation are written by the assistant; they cannot be added.");
|
||||
assertThatThrownBy(() -> new AssistantAccessPermissionChecker().checkPermissionsThrowing(new ReplaceInput(), conversations))
|
||||
.hasMessageContaining("cannot be added");
|
||||
|
||||
for(var input : List.of(new InsertInput(budgets.getName()), new UpdateInput(budgets.getName()), new DeleteInput(budgets.getName()), new ReplaceInput()))
|
||||
{
|
||||
new AssistantAdminPermissionChecker().checkPermissionsThrowing(input, budgets);
|
||||
}
|
||||
|
||||
QTableMetaData pages = qInstance.getTable("page");
|
||||
new AssistantWrittenByTheAssistantPermissionChecker().checkPermissionsThrowing(new DeleteInput("page"), pages);
|
||||
new AssistantWrittenByTheAssistantPermissionChecker().checkPermissionsThrowing(new DeleteInput(toolCalls.getName()), qInstance.getProcesses().values().iterator().next());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Every table a person edits has a customizer before an insert and before
|
||||
** an update that takes the fields the server decides out of a person's
|
||||
** save: sent with a value for every field that is not editable, the record
|
||||
** it is handed keeps none of them but its key. The assistant's own save
|
||||
** keeps them.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aPersonsSaveOfASettingsTableKeepsNoFieldTheServerDecides() throws Exception
|
||||
{
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(AssistantTestFixture.ADMIN, config));
|
||||
Integer tablesChecked = 0;
|
||||
for(QTableMetaData table : assistantTables())
|
||||
{
|
||||
Set<Operation> byAPerson = AssistantTableWrites.byAPerson(table.getName());
|
||||
if(!byAPerson.contains(Operation.UPDATE))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
tablesChecked++;
|
||||
Optional<TableCustomizerInterface> beforeUpdate = QCodeLoader.getTableCustomizer(table, TableCustomizers.PRE_UPDATE_RECORD.getRole());
|
||||
assertThat(beforeUpdate).as(table.getName() + " has a customizer before an update").isPresent();
|
||||
if(byAPerson.contains(Operation.INSERT))
|
||||
{
|
||||
assertThat(QCodeLoader.getTableCustomizer(table, TableCustomizers.PRE_INSERT_RECORD.getRole())).as(table.getName() + " has a customizer before an insert").isPresent();
|
||||
}
|
||||
|
||||
Set<String> theServers = new TreeSet<>();
|
||||
for(QFieldMetaData field : table.getFields().values())
|
||||
{
|
||||
if(!field.getIsEditable() && !field.getName().equals(table.getPrimaryKeyField())
|
||||
&& field.getBehaviorOnlyIfSet(DynamicDefaultValueBehavior.class) != DynamicDefaultValueBehavior.MODIFY_DATE)
|
||||
{
|
||||
theServers.add(field.getName());
|
||||
}
|
||||
}
|
||||
assertThat(theServers).as(table.getName() + " has fields the server decides").contains("createDate");
|
||||
if(table.getName().equals(AssistantConversation.TABLE_NAME))
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// its own customizer says what its owner changes on a screen, by name //
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
theServers.removeAll(AssistantConversationCustomizer.USER_EDITABLE_FIELDS);
|
||||
}
|
||||
|
||||
for(QInputSource source : QInputSource.values())
|
||||
{
|
||||
QRecord stored = new QRecord().withValue("id", 41);
|
||||
QRecord saved = new QRecord().withValue("id", "41");
|
||||
for(QFieldMetaData field : table.getFields().values())
|
||||
{
|
||||
if(!field.getName().equals("id"))
|
||||
{
|
||||
stored.setValue(field.getName(), TableRows.value(field, 1));
|
||||
}
|
||||
}
|
||||
for(String fieldName : theServers)
|
||||
{
|
||||
saved.setValue(fieldName, (Serializable) TableRows.value(table.getField(fieldName), 2));
|
||||
}
|
||||
UpdateInput input = new UpdateInput(table.getName()).withInputSource(source);
|
||||
input.setRecords(new ArrayList<>(List.of(saved)));
|
||||
try
|
||||
{
|
||||
beforeUpdate.get().preUpdate(input, input.getRecords(), false, Optional.of(List.of(stored)));
|
||||
}
|
||||
catch(QException e)
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
// a rule of the table may refuse the made-up row; what was taken out stays out //
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
assertThat(e).hasMessageNotContaining("NullPointer");
|
||||
}
|
||||
|
||||
if(source == QInputSource.USER)
|
||||
{
|
||||
assertThat(saved.getValues().keySet()).as(table.getName() + ": a person's save keeps no field the server decides").doesNotContainAnyElementsOf(theServers).contains("id");
|
||||
}
|
||||
else if(!table.getName().equals(AssistantConversation.TABLE_NAME))
|
||||
{
|
||||
assertThat(saved.getValues().keySet()).as(table.getName() + ": the assistant's own save sets them").contains("createDate");
|
||||
}
|
||||
}
|
||||
}
|
||||
assertThat(tablesChecked).as("the settings tables and the conversation table").isGreaterThanOrEqualTo(13);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private List<QTableMetaData> assistantTables()
|
||||
{
|
||||
return (qInstance.getTables().values().stream().filter(table -> table.getName().startsWith("assistant_")).toList());
|
||||
}
|
||||
}
|
||||
+463
@@ -0,0 +1,463 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import com.kingsrook.qbits.assistant.AssistantQBitConfig;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudget;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudgetOverride;
|
||||
import com.kingsrook.qbits.assistant.persistence.AssistantStore;
|
||||
import com.kingsrook.qbits.assistant.testing.AssistantTestFixture;
|
||||
import com.kingsrook.qbits.assistant.testing.BaseAssistantTest;
|
||||
import com.kingsrook.qbits.assistant.testing.UiApiTestServer;
|
||||
import com.kingsrook.qbits.assistant.uiapi.UiApiResult;
|
||||
import com.kingsrook.qqq.backend.core.actions.tables.QueryAction;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.audits.AuditLevel;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.audits.QAuditRules;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** An administrator edits a spending limit on its record screen. The screen
|
||||
** saves through the UI API's table update route (see RecordScreen): only the
|
||||
** fields that were changed, with the version that was read. The record's id
|
||||
** comes from the request's path, so it reaches the table customizer as text,
|
||||
** and so does every value.
|
||||
**
|
||||
** The rules a budget is held to are the same for such a partial save as for a
|
||||
** whole one, and they are judged on the stored row with the change applied.
|
||||
*******************************************************************************/
|
||||
class BudgetEditOverHttpTest extends BaseAssistantTest
|
||||
{
|
||||
private static final String ADMIN = AssistantTestFixture.ADMIN;
|
||||
|
||||
private static final String BUDGET = AssistantBudget.TABLE_NAME;
|
||||
private static final String OVERRIDE = AssistantBudgetOverride.TABLE_NAME;
|
||||
|
||||
private static final String OVER_THE_CEILING = "A cost limit must not be more than 1000 US dollars for one period.";
|
||||
|
||||
private RecordScreen screen;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The host's ceiling on a cost limit, and field-by-field audit of budgets:
|
||||
** both are a host's to turn on.
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
protected void customizeConfig(AssistantQBitConfig config)
|
||||
{
|
||||
config.setBudgetCostLimitCeilingUsd(new BigDecimal("1000"));
|
||||
config.setTableMetaDataCustomizer((qInstance, table) ->
|
||||
{
|
||||
if(BUDGET.equals(table.getName()))
|
||||
{
|
||||
table.withAuditRules(new QAuditRules().withAuditLevel(AuditLevel.FIELD));
|
||||
}
|
||||
return (table);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The limit, the enabled flag, the period, the name, the notes and a
|
||||
** threshold, each saved alone from the version that was read: each is
|
||||
** stored, and nothing else of the budget changes.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void oneChangedFieldIsSavedAndEveryOtherFieldIsKept() throws Exception
|
||||
{
|
||||
AssistantBudget budget = weekly();
|
||||
Map<String, Object> expected = screen.read(BUDGET, budget.getId());
|
||||
|
||||
Map<String, String> changes = new LinkedHashMap<>();
|
||||
changes.put("limitValue", "750");
|
||||
changes.put("enabled", "false");
|
||||
changes.put("period", "MONTH");
|
||||
changes.put("name", "Everyone, a month");
|
||||
changes.put("notes", "Raised for the quarter end.");
|
||||
changes.put("warnPercent", "90");
|
||||
|
||||
for(Map.Entry<String, String> change : changes.entrySet())
|
||||
{
|
||||
UiApiResult saved = screen.change(BUDGET, budget.getId(), Map.of(change.getKey(), change.getValue()));
|
||||
assertThat(saved.status()).as("saving only " + change.getKey() + ": " + saved.body()).isEqualTo(200);
|
||||
|
||||
Map<String, Object> after = screen.read(BUDGET, budget.getId());
|
||||
assertThat(after.get(change.getKey())).as(change.getKey() + " was stored").isNotEqualTo(expected.get(change.getKey()));
|
||||
expected.put(change.getKey(), after.get(change.getKey()));
|
||||
expected.put("modifyDate", after.get("modifyDate"));
|
||||
assertThat(after).as("only " + change.getKey() + " changed").isEqualTo(expected);
|
||||
}
|
||||
|
||||
AssistantBudget stored = store().budget(budget.getId());
|
||||
assertThat(stored.getLimitValue()).isEqualByComparingTo("750");
|
||||
assertThat(stored.getEnabled()).isFalse();
|
||||
assertThat(stored.getPeriod()).isEqualTo("MONTH");
|
||||
assertThat(stored.getName()).isEqualTo("Everyone, a month");
|
||||
assertThat(stored.getNotes()).isEqualTo("Raised for the quarter end.");
|
||||
assertThat(stored.getWarnPercent()).isEqualTo(90);
|
||||
assertThat(stored.getScopeType()).isEqualTo("INSTANCE");
|
||||
assertThat(stored.getScopeValue()).isNull();
|
||||
assertThat(stored.getMetric()).isEqualTo("COST_USD");
|
||||
assertThat(stored.getHardCeilingPercent()).isEqualTo(120);
|
||||
assertThat(stored.getAtLimitAction()).isEqualTo("STOP");
|
||||
assertThat(stored.getCreateDate()).isEqualTo(budget.getCreateDate());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Two people read the same budget; the second save is from a version that
|
||||
** is no longer the stored one. It is refused, and stores nothing.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aSecondSaveFromTheStaleVersionIsRefusedAndStoresNothing() throws Exception
|
||||
{
|
||||
AssistantBudget budget = weekly();
|
||||
String version = RecordScreen.version(screen.read(BUDGET, budget.getId()));
|
||||
|
||||
assertThat(screen.save(BUDGET, budget.getId(), Map.of("limitValue", "750"), version).status()).isEqualTo(200);
|
||||
Map<String, Object> afterTheFirst = screen.read(BUDGET, budget.getId());
|
||||
|
||||
UiApiResult stale = screen.save(BUDGET, budget.getId(), Map.of("enabled", "false"), version);
|
||||
assertThat(stale.status()).as(stale.body()).isEqualTo(409);
|
||||
assertThat(screen.read(BUDGET, budget.getId())).as("nothing of the refused save is stored").isEqualTo(afterTheFirst);
|
||||
assertThat(store().budget(budget.getId()).getEnabled()).isTrue();
|
||||
|
||||
assertThat(screen.save(BUDGET, budget.getId(), Map.of("enabled", "false"), RecordScreen.version(afterTheFirst)).status()).as("from the version now stored").isEqualTo(200);
|
||||
assertThat(store().budget(budget.getId()).getEnabled()).isFalse();
|
||||
assertThat(store().budget(budget.getId()).getLimitValue()).isEqualByComparingTo("750");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The host's ceiling holds for a partial save exactly as for a whole one,
|
||||
** with the same message, and it is judged on the budget as it would be
|
||||
** stored: a save that only turns a token limit into a cost limit is over
|
||||
** the ceiling with the limit that is already stored.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aPartialSaveIsHeldToTheHostsCeilingLikeAWholeOne() throws Exception
|
||||
{
|
||||
AssistantBudget budget = weekly();
|
||||
Map<String, Object> before = screen.read(BUDGET, budget.getId());
|
||||
|
||||
UiApiResult partial = screen.change(BUDGET, budget.getId(), Map.of("limitValue", "1000.01"));
|
||||
assertThat(partial.status()).isEqualTo(400);
|
||||
assertThat(partial.body()).contains(OVER_THE_CEILING);
|
||||
|
||||
Map<String, String> whole = new LinkedHashMap<>();
|
||||
for(String field : List.of("name", "enabled", "scopeType", "period", "metric", "warnPercent", "atLimitAction", "hardCeilingPercent"))
|
||||
{
|
||||
whole.put(field, String.valueOf(before.get(field)));
|
||||
}
|
||||
whole.put("limitValue", "1000.01");
|
||||
UiApiResult wholeSave = screen.change(BUDGET, budget.getId(), whole);
|
||||
assertThat(wholeSave.status()).isEqualTo(400);
|
||||
assertThat(message(wholeSave)).as("the same refusal, in the same words").isEqualTo(message(partial));
|
||||
assertThat(screen.read(BUDGET, budget.getId())).as("nothing stored").isEqualTo(before);
|
||||
|
||||
assertThat(screen.change(BUDGET, budget.getId(), Map.of("limitValue", "1000")).status()).as("the ceiling itself is allowed").isEqualTo(200);
|
||||
|
||||
AssistantBudget tokens = store().insert(new AssistantBudget().withName("Tokens, a week").withScopeType("INSTANCE").withPeriod("WEEK")
|
||||
.withMetric("TOKENS").withLimitValue(new BigDecimal("5000000")).withAtLimitAction("STOP"));
|
||||
UiApiResult metricOnly = screen.change(BUDGET, tokens.getId(), Map.of("metric", "COST_USD"));
|
||||
assertThat(metricOnly.status()).as("5,000,000 tokens would become 5,000,000 dollars").isEqualTo(400);
|
||||
assertThat(metricOnly.body()).contains(OVER_THE_CEILING);
|
||||
assertThat(store().budget(tokens.getId()).getMetric()).isEqualTo("TOKENS");
|
||||
|
||||
UiApiResult created = screen.create(BUDGET, Map.of("name", "New", "scopeType", "USER", "scopeValue", "someone", "period", "DAY", "metric", "COST_USD", "limitValue", "2000"));
|
||||
assertThat(created.status()).as("and for a new budget").isEqualTo(400);
|
||||
assertThat(created.body()).contains(OVER_THE_CEILING);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A partial save cannot blank what a budget must have, and cannot leave it
|
||||
** with a value no whole save could give it. Each is refused, and nothing
|
||||
** is stored.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aPartialSaveIsHeldToEveryRuleOfAWholeOne() throws Exception
|
||||
{
|
||||
AssistantBudget budget = weekly();
|
||||
Map<String, Object> before = screen.read(BUDGET, budget.getId());
|
||||
|
||||
Map<Map<String, String>, String> refused = new LinkedHashMap<>();
|
||||
refused.put(Map.of("name", ""), "Name");
|
||||
refused.put(Map.of("limitValue", ""), "Limit");
|
||||
refused.put(Map.of("period", ""), "Period");
|
||||
refused.put(Map.of("metric", ""), "Metric");
|
||||
refused.put(Map.of("scopeType", ""), "Scope Type");
|
||||
refused.put(Map.of("period", "FORTNIGHT"), "Invalid period.");
|
||||
refused.put(Map.of("scopeType", "TEAM"), "Invalid scopeType.");
|
||||
refused.put(Map.of("metric", "REQUESTS"), "Invalid metric.");
|
||||
refused.put(Map.of("atLimitAction", "IGNORE"), "Invalid atLimitAction.");
|
||||
refused.put(Map.of("scopeType", "USER"), "Scope value must be empty for INSTANCE and present for every other scope.");
|
||||
refused.put(Map.of("scopeValue", "someone"), "Scope value must be empty for INSTANCE and present for every other scope.");
|
||||
refused.put(Map.of("scopeType", "PROVIDER", "scopeValue", "987654"), "Scope value must reference an existing provider or model registry row.");
|
||||
refused.put(Map.of("scopeType", "MODEL", "scopeValue", "main"), "Scope value must be a numeric record id.");
|
||||
refused.put(Map.of("downgradeModelId", "987654"), "Downgrade model must reference an existing model registry row.");
|
||||
refused.put(Map.of("limitValue", "0"), "Limit must be positive; token limits must be whole numbers.");
|
||||
refused.put(Map.of("limitValue", "-5"), "Limit must be positive; token limits must be whole numbers.");
|
||||
refused.put(Map.of("limitValue", "a lot"), "Limit Value");
|
||||
refused.put(Map.of("metric", "TOKENS", "limitValue", "10.5"), "Limit must be positive; token limits must be whole numbers.");
|
||||
refused.put(Map.of("warnPercent", "100"), "Warning percent must be 0 through 99 and hard ceiling must be at least 100.");
|
||||
refused.put(Map.of("hardCeilingPercent", "99"), "Warning percent must be 0 through 99 and hard ceiling must be at least 100.");
|
||||
|
||||
for(Map.Entry<Map<String, String>, String> entry : refused.entrySet())
|
||||
{
|
||||
UiApiResult result = screen.change(BUDGET, budget.getId(), entry.getKey());
|
||||
assertThat(result.status()).as("saving only " + entry.getKey() + ": " + result.body()).isEqualTo(400);
|
||||
assertThat(message(result)).as("saving only " + entry.getKey()).contains(entry.getValue()).doesNotContain("a lot");
|
||||
assertThat(screen.read(BUDGET, budget.getId())).as("nothing stored by " + entry.getKey()).isEqualTo(before);
|
||||
}
|
||||
|
||||
UiApiResult scope = screen.change(BUDGET, budget.getId(), Map.of("scopeType", "USER", "scopeValue", "someone"));
|
||||
assertThat(scope.status()).as("a scope and its value together: " + scope.body()).isEqualTo(200);
|
||||
assertThat(store().budget(budget.getId()).getScopeValue()).isEqualTo("someone");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A setting a budget may be without gets its default back when a save
|
||||
** blanks it, as it does on a whole save that leaves it out: a budget is
|
||||
** never stored without a threshold or an action.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aBlankedSettingGetsItsDefault() throws Exception
|
||||
{
|
||||
AssistantBudget budget = weekly();
|
||||
assertThat(screen.change(BUDGET, budget.getId(), Map.of("warnPercent", "50", "hardCeilingPercent", "150", "enabled", "false")).status()).isEqualTo(200);
|
||||
|
||||
UiApiResult blanked = screen.change(BUDGET, budget.getId(), Map.of("warnPercent", "", "hardCeilingPercent", "", "atLimitAction", "", "enabled", ""));
|
||||
assertThat(blanked.status()).as(blanked.body()).isEqualTo(200);
|
||||
AssistantBudget stored = store().budget(budget.getId());
|
||||
assertThat(stored.getWarnPercent()).isEqualTo(80);
|
||||
assertThat(stored.getHardCeilingPercent()).isEqualTo(120);
|
||||
assertThat(stored.getAtLimitAction()).isEqualTo("DOWNGRADE");
|
||||
assertThat(stored.getEnabled()).as("a blanked switch is a limit in force, never no limit").isTrue();
|
||||
assertThat(stored.getLimitValue()).isEqualByComparingTo("500");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** What the server decides stays the server's: a save cannot name another
|
||||
** record in its body, set the date a budget was created or the version it
|
||||
** is saved as, or say who granted an override. A field that is not editable
|
||||
** is taken out of a person's save, whatever QQQ does with it.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aSaveSetsNothingTheServerDecides() throws Exception
|
||||
{
|
||||
AssistantBudget budget = weekly();
|
||||
AssistantBudget other = store().insert(new AssistantBudget().withName("Another").withScopeType("USER").withScopeValue("someone").withPeriod("DAY")
|
||||
.withMetric("COST_USD").withLimitValue(BigDecimal.TEN));
|
||||
Map<String, Object> before = screen.read(BUDGET, budget.getId());
|
||||
|
||||
Map<String, String> changed = new LinkedHashMap<>();
|
||||
changed.put("id", String.valueOf(other.getId()));
|
||||
changed.put("createDate", "2001-01-01T00:00:00Z");
|
||||
changed.put("modifyDate", "2001-01-01T00:00:00Z");
|
||||
changed.put("notes", "Mine.");
|
||||
UiApiResult saved = screen.change(BUDGET, budget.getId(), changed);
|
||||
assertThat(saved.status()).as(saved.body()).isEqualTo(200);
|
||||
|
||||
Map<String, Object> after = screen.read(BUDGET, budget.getId());
|
||||
assertThat(after.get("notes")).isEqualTo("Mine.");
|
||||
assertThat(after.get("createDate")).as("the create date is the server's").isEqualTo(before.get("createDate"));
|
||||
assertThat(Instant.parse(String.valueOf(after.get("modifyDate")))).as("the modify date is the server's").isAfterOrEqualTo(Instant.parse(String.valueOf(before.get("modifyDate"))));
|
||||
assertThat(store().budget(other.getId()).getNotes()).as("the record of the path is the one saved").isNull();
|
||||
|
||||
Instant expiry = Instant.now().plusSeconds(3600);
|
||||
UiApiResult granted = screen.create(OVERRIDE, Map.of("budgetId", String.valueOf(budget.getId()), "reason", "Quarter end", "extraValue", "50",
|
||||
"expiresAt", expiry.toString(), "grantedBy", "somebody-else"));
|
||||
assertThat(granted.status()).as(granted.body()).isEqualTo(200);
|
||||
AssistantBudgetOverride override = store().query(OVERRIDE, new QQueryFilter(), AssistantBudgetOverride.class).get(0);
|
||||
assertThat(override.getGrantedBy()).as("who granted it is the session's").isEqualTo(ADMIN);
|
||||
|
||||
UiApiResult created = screen.create(BUDGET, Map.of("id", "987654", "createDate", "2001-01-01T00:00:00Z", "name", "New", "scopeType", "USER", "scopeValue", "someone",
|
||||
"period", "DAY", "metric", "COST_USD", "limitValue", "5"));
|
||||
assertThat(created.status()).as(created.body()).isEqualTo(200);
|
||||
AssistantBudget fresh = store().query(BUDGET, new QQueryFilter().withCriteria("name", QCriteriaOperator.EQUALS, "New"), AssistantBudget.class).get(0);
|
||||
assertThat(fresh.getId()).as("a new budget gets its id from the server").isNotEqualTo(987654);
|
||||
assertThat(fresh.getCreateDate()).isAfter(Instant.parse("2020-01-01T00:00:00Z"));
|
||||
|
||||
UiApiResult edited = screen.change(OVERRIDE, override.getId(), Map.of("reason", "Quarter end, extended", "grantedBy", "somebody-else"));
|
||||
assertThat(edited.status()).as(edited.body()).isEqualTo(200);
|
||||
AssistantBudgetOverride stored = store().query(OVERRIDE, new QQueryFilter(), AssistantBudgetOverride.class).get(0);
|
||||
assertThat(stored.getGrantedBy()).isEqualTo(ADMIN);
|
||||
assertThat(stored.getReason()).isEqualTo("Quarter end, extended");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** An override of a budget is edited the same way, and held to its rules on
|
||||
** the stored row with the change applied.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void anOverrideIsSavedOneFieldAtATime() throws Exception
|
||||
{
|
||||
AssistantBudget budget = weekly();
|
||||
Instant expiry = Instant.parse("2099-01-01T00:00:00Z");
|
||||
AssistantBudgetOverride override = store().insert(new AssistantBudgetOverride().withBudgetId(budget.getId()).withReason("Quarter end")
|
||||
.withExtraValue(new BigDecimal("50")).withExpiresAt(expiry));
|
||||
Map<String, Object> expected = screen.read(OVERRIDE, override.getId());
|
||||
|
||||
UiApiResult saved = screen.change(OVERRIDE, override.getId(), Map.of("extraValue", "75"));
|
||||
assertThat(saved.status()).as(saved.body()).isEqualTo(200);
|
||||
Map<String, Object> after = screen.read(OVERRIDE, override.getId());
|
||||
expected.put("extraValue", after.get("extraValue"));
|
||||
expected.put("modifyDate", after.get("modifyDate"));
|
||||
assertThat(after).as("only the extra value changed").isEqualTo(expected);
|
||||
AssistantBudgetOverride stored = store().query(OVERRIDE, new QQueryFilter(), AssistantBudgetOverride.class).get(0);
|
||||
assertThat(stored.getExtraValue()).isEqualByComparingTo("75");
|
||||
assertThat(stored.getExpiresAt()).isEqualTo(expiry);
|
||||
assertThat(stored.getBudgetId()).isEqualTo(budget.getId());
|
||||
|
||||
Map<Map<String, String>, String> refused = new LinkedHashMap<>();
|
||||
refused.put(Map.of("reason", ""), "Reason");
|
||||
refused.put(Map.of("expiresAt", ""), "Expires At");
|
||||
refused.put(Map.of("extraValue", ""), "Set exactly one of extra value or unlimited until.");
|
||||
refused.put(Map.of("unlimitedUntil", "2098-01-01T00:00:00Z"), "Set exactly one of extra value or unlimited until.");
|
||||
refused.put(Map.of("extraValue", "-1"), "Extra value must be positive; token values must be whole numbers.");
|
||||
refused.put(Map.of("budgetId", "987654"), "Budget must reference an existing budget.");
|
||||
for(Map.Entry<Map<String, String>, String> entry : refused.entrySet())
|
||||
{
|
||||
UiApiResult result = screen.change(OVERRIDE, override.getId(), entry.getKey());
|
||||
assertThat(result.status()).as("saving only " + entry.getKey() + ": " + result.body()).isEqualTo(400);
|
||||
assertThat(message(result)).as("saving only " + entry.getKey()).contains(entry.getValue());
|
||||
assertThat(screen.read(OVERRIDE, override.getId())).as("nothing stored by " + entry.getKey()).isEqualTo(after);
|
||||
}
|
||||
|
||||
UiApiResult swapped = screen.change(OVERRIDE, override.getId(), Map.of("extraValue", "", "unlimitedUntil", "2098-01-01T00:00:00Z"));
|
||||
assertThat(swapped.status()).as(swapped.body()).isEqualTo(200);
|
||||
UiApiResult late = screen.change(OVERRIDE, override.getId(), Map.of("unlimitedUntil", "2099-06-01T00:00:00Z"));
|
||||
assertThat(late.status()).isEqualTo(400);
|
||||
assertThat(message(late)).contains("Unlimited until cannot be after expiry.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Where a host audits budgets field by field, a save of one field records
|
||||
** that field, with its old and its new value, and no other: the stored row
|
||||
** the rules are judged on is not what is saved.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void theAuditOfAPartialSaveNamesOnlyTheChangedField() throws Exception
|
||||
{
|
||||
AssistantBudget budget = weekly();
|
||||
assertThat(screen.change(BUDGET, budget.getId(), Map.of("limitValue", "750")).status()).isEqualTo(200);
|
||||
|
||||
List<QRecord> details = editDetails(budget.getId());
|
||||
assertThat(details).as("one field was saved, one field is recorded").hasSize(1);
|
||||
assertThat(details.get(0).getValueString("fieldName")).isEqualTo("limitValue");
|
||||
assertThat(new BigDecimal(details.get(0).getValueString("oldValue"))).isEqualByComparingTo("500");
|
||||
assertThat(new BigDecimal(details.get(0).getValueString("newValue"))).isEqualByComparingTo("750");
|
||||
|
||||
assertThat(screen.change(BUDGET, budget.getId(), Map.of("enabled", "false", "notes", "Paused.")).status()).isEqualTo(200);
|
||||
assertThat(editDetails(budget.getId())).extracting(detail -> detail.getValueString("fieldName")).containsExactlyInAnyOrder("limitValue", "enabled", "notes");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A person without assistant.admin is refused at the door, and nothing is
|
||||
** stored.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void onlyAnAdministratorSavesABudget() throws Exception
|
||||
{
|
||||
AssistantBudget budget = weekly();
|
||||
String version = RecordScreen.version(screen.read(BUDGET, budget.getId()));
|
||||
UiApiResult refused = new RecordScreen(UiApiTestServer.serve(qInstance, config), AssistantTestFixture.EDITOR).save(BUDGET, budget.getId(), Map.of("limitValue", "1"), version);
|
||||
assertThat(refused.status()).as(refused.body()).isEqualTo(403);
|
||||
assertThat(store().budget(budget.getId()).getLimitValue()).isEqualByComparingTo("500");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A budget of 500 dollars a week for everyone, and the administrator's
|
||||
** screen.
|
||||
*******************************************************************************/
|
||||
private AssistantBudget weekly() throws Exception
|
||||
{
|
||||
screen = new RecordScreen(UiApiTestServer.serve(qInstance, config), ADMIN);
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(ADMIN, config));
|
||||
AssistantBudget budget = store().insert(new AssistantBudget().withName("Everyone, a week").withScopeType("INSTANCE").withPeriod("WEEK").withMetric("COST_USD")
|
||||
.withLimitValue(new BigDecimal("500")).withAtLimitAction("STOP"));
|
||||
return (store().budget(budget.getId()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private AssistantStore store()
|
||||
{
|
||||
return (config.getRuntime().getStore());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The refusal a screen shows.
|
||||
*******************************************************************************/
|
||||
private static String message(UiApiResult result)
|
||||
{
|
||||
return (String.valueOf(result.json().get("error")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The field lines of the audits of one budget's edits (every audit after
|
||||
** the first, which is the audit of its creation).
|
||||
*******************************************************************************/
|
||||
private List<QRecord> editDetails(Integer budgetId) throws Exception
|
||||
{
|
||||
List<QRecord> audits = new QueryAction().execute(new QueryInput("audit").withFilter(new QQueryFilter()
|
||||
.withCriteria("auditTable.name", QCriteriaOperator.EQUALS, BUDGET).withCriteria("recordId", QCriteriaOperator.EQUALS, budgetId)
|
||||
.withOrderBy(new QFilterOrderBy("id", true)))).getRecords();
|
||||
assertThat(audits).as("the creation and at least one edit are audited").hasSizeGreaterThan(1);
|
||||
return (new QueryAction().execute(new QueryInput("auditDetail").withFilter(new QQueryFilter()
|
||||
.withCriteria("auditId", QCriteriaOperator.IN, audits.stream().skip(1).map(audit -> audit.getValue("id")).toList())
|
||||
.withCriteria("fieldName", QCriteriaOperator.IS_NOT_BLANK))).getRecords());
|
||||
}
|
||||
}
|
||||
+234
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
import com.kingsrook.qbits.assistant.access.DataAccessMode;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantDataAccess;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPolicy;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantProfile;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantSkill;
|
||||
import com.kingsrook.qbits.assistant.persistence.AssistantStore;
|
||||
import com.kingsrook.qbits.assistant.testing.AssistantTestFixture;
|
||||
import com.kingsrook.qbits.assistant.testing.BaseAssistantTest;
|
||||
import com.kingsrook.qbits.assistant.testing.UiApiTestServer;
|
||||
import com.kingsrook.qbits.assistant.uiapi.UiApiResult;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The assistant's settings are records an administrator edits on record
|
||||
** screens, which save only the fields that were changed (see RecordScreen).
|
||||
** A save of one field is held to the rules on the stored row with the change
|
||||
** applied, and leaves every other field as it is: a rule's default is for a
|
||||
** value the row does not have, never for one the save did not send.
|
||||
**
|
||||
** Budgets are in BudgetEditOverHttpTest; providers and models, knowledge
|
||||
** sources and scheduled tasks are with their own tests.
|
||||
*******************************************************************************/
|
||||
class RecordEditsOverHttpTest extends BaseAssistantTest
|
||||
{
|
||||
private RecordScreen screen;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The administrator's screen.
|
||||
*******************************************************************************/
|
||||
@BeforeEach
|
||||
void administrator()
|
||||
{
|
||||
screen = new RecordScreen(UiApiTestServer.serve(qInstance, config), AssistantTestFixture.ADMIN);
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(AssistantTestFixture.ADMIN, config));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A profile that is switched off and routes by heuristic stays so when
|
||||
** only its display name is saved.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aProfileKeepsWhatASaveDidNotSend() throws Exception
|
||||
{
|
||||
AssistantProfile profile = store().insert(new AssistantProfile().withName("analyst").withDisplayName("Analyst").withRoutingMode("HEURISTIC")
|
||||
.withEnabled(false).withIsDefault(false).withPersonality("Brief."));
|
||||
Map<String, Object> expected = screen.read(AssistantProfile.TABLE_NAME, profile.getId());
|
||||
|
||||
UiApiResult saved = screen.change(AssistantProfile.TABLE_NAME, profile.getId(), Map.of("displayName", "Data analyst"));
|
||||
assertThat(saved.status()).as(saved.body()).isEqualTo(200);
|
||||
Map<String, Object> after = screen.read(AssistantProfile.TABLE_NAME, profile.getId());
|
||||
expected.put("displayName", "Data analyst");
|
||||
expected.put("modifyDate", after.get("modifyDate"));
|
||||
assertThat(after).as("only the display name changed").isEqualTo(expected);
|
||||
assertThat(after.get("routingMode")).isEqualTo("HEURISTIC");
|
||||
assertThat(after.get("enabled")).isEqualTo(false);
|
||||
|
||||
UiApiResult routing = screen.change(AssistantProfile.TABLE_NAME, profile.getId(), Map.of("routingMode", "SOMETIMES"));
|
||||
assertThat(routing.status()).isEqualTo(400);
|
||||
assertThat(routing.body()).contains("Routing mode must be OFF, HEURISTIC or CLASSIFIER.");
|
||||
UiApiResult app = screen.change(AssistantProfile.TABLE_NAME, profile.getId(), Map.of("appName", "no-such-app"));
|
||||
assertThat(app.status()).isEqualTo(400);
|
||||
assertThat(screen.read(AssistantProfile.TABLE_NAME, profile.getId())).as("nothing stored").isEqualTo(after);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Marking a profile the default on its screen leaves it the default, and
|
||||
** unmarks the one that was.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void theProfileMarkedDefaultOnItsScreenIsTheOneDefault() throws Exception
|
||||
{
|
||||
AssistantProfile first = store().insert(new AssistantProfile().withName("first").withDisplayName("First").withEnabled(true).withIsDefault(true));
|
||||
AssistantProfile second = store().insert(new AssistantProfile().withName("second").withDisplayName("Second").withEnabled(true).withIsDefault(false));
|
||||
|
||||
UiApiResult saved = screen.change(AssistantProfile.TABLE_NAME, second.getId(), Map.of("isDefault", "true"));
|
||||
assertThat(saved.status()).as(saved.body()).isEqualTo(200);
|
||||
assertThat(store().query(AssistantProfile.TABLE_NAME, new QQueryFilter().withCriteria("isDefault", QCriteriaOperator.EQUALS, true), AssistantProfile.class))
|
||||
.extracting(AssistantProfile::getId).containsExactly(second.getId());
|
||||
assertThat(screen.read(AssistantProfile.TABLE_NAME, first.getId()).get("isDefault")).isEqualTo(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A skill that is switched off stays off when only its description is
|
||||
** saved, and its instructions stay bounded.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aSkillKeepsWhatASaveDidNotSend() throws Exception
|
||||
{
|
||||
AssistantSkill skill = store().insert(new AssistantSkill().withName("summaries").withInstructions("Summarize in three lines.").withEnabled(false));
|
||||
Map<String, Object> expected = screen.read(AssistantSkill.TABLE_NAME, skill.getId());
|
||||
|
||||
UiApiResult saved = screen.change(AssistantSkill.TABLE_NAME, skill.getId(), Map.of("description", "Short summaries"));
|
||||
assertThat(saved.status()).as(saved.body()).isEqualTo(200);
|
||||
Map<String, Object> after = screen.read(AssistantSkill.TABLE_NAME, skill.getId());
|
||||
expected.put("description", "Short summaries");
|
||||
expected.put("modifyDate", after.get("modifyDate"));
|
||||
assertThat(after).as("only the description changed").isEqualTo(expected);
|
||||
assertThat(after.get("enabled")).isEqualTo(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A data access row of a process stays a row of that process when only its
|
||||
** mode is saved, and the mode is one of the three.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aDataAccessRowKeepsWhatASaveDidNotSend() throws Exception
|
||||
{
|
||||
String process = qInstance.getProcesses().keySet().stream().sorted().findFirst().orElseThrow();
|
||||
AssistantDataAccess row = store().insert(new AssistantDataAccess().withObjectType(AssistantDataAccess.TYPE_PROCESS).withObjectName(process).withMode(DataAccessMode.READ_WRITE.name()));
|
||||
Map<String, Object> expected = screen.read(AssistantDataAccess.TABLE_NAME, row.getId());
|
||||
|
||||
UiApiResult saved = screen.change(AssistantDataAccess.TABLE_NAME, row.getId(), Map.of("mode", "NONE"));
|
||||
assertThat(saved.status()).as(saved.body()).isEqualTo(200);
|
||||
Map<String, Object> after = screen.read(AssistantDataAccess.TABLE_NAME, row.getId());
|
||||
expected.put("mode", "NONE");
|
||||
expected.put("modifyDate", after.get("modifyDate"));
|
||||
assertThat(after).as("only the mode changed").isEqualTo(expected);
|
||||
assertThat(after.get("objectType")).isEqualTo(AssistantDataAccess.TYPE_PROCESS);
|
||||
assertThat(config.getRuntime().getDataAccessService().forProcess(process).mode()).as("and it is in force").isEqualTo(DataAccessMode.NONE);
|
||||
|
||||
UiApiResult mode = screen.change(AssistantDataAccess.TABLE_NAME, row.getId(), Map.of("mode", "EVERYTHING"));
|
||||
assertThat(mode.status()).isEqualTo(400);
|
||||
assertThat(mode.body()).contains("Assistant access must be NONE, READ or READ_WRITE.");
|
||||
UiApiResult name = screen.change(AssistantDataAccess.TABLE_NAME, row.getId(), Map.of("objectName", "page"));
|
||||
assertThat(name.status()).as("a table's name on a row of a process").isEqualTo(400);
|
||||
assertThat(screen.read(AssistantDataAccess.TABLE_NAME, row.getId())).as("nothing stored").isEqualTo(after);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The date a row was created is the server's on every settings table: a
|
||||
** save that carries one, with nothing else or with a real change, leaves it
|
||||
** as it is. A policy has no rules of its own, and is held to this like the
|
||||
** others.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aSaveSetsNoFieldTheServerDecides() throws Exception
|
||||
{
|
||||
AssistantPolicy policy = store().insert(new AssistantPolicy().withName("default").withKillSwitch(false));
|
||||
AssistantProfile profile = store().insert(new AssistantProfile().withName("analyst").withDisplayName("Analyst").withEnabled(true).withIsDefault(false));
|
||||
AssistantSkill skill = store().insert(new AssistantSkill().withName("summaries").withInstructions("Summarize in three lines.").withEnabled(true));
|
||||
AssistantDataAccess access = store().insert(new AssistantDataAccess().withObjectType(AssistantDataAccess.TYPE_TABLE).withObjectName("page").withMode(DataAccessMode.READ.name()));
|
||||
|
||||
Map<String, Object> rows = new java.util.LinkedHashMap<>();
|
||||
rows.put(AssistantPolicy.TABLE_NAME, policy.getId());
|
||||
rows.put(AssistantProfile.TABLE_NAME, profile.getId());
|
||||
rows.put(AssistantSkill.TABLE_NAME, skill.getId());
|
||||
rows.put(AssistantDataAccess.TABLE_NAME, access.getId());
|
||||
for(Map.Entry<String, Object> row : rows.entrySet())
|
||||
{
|
||||
Map<String, Object> before = screen.read(row.getKey(), row.getValue());
|
||||
|
||||
UiApiResult alone = screen.saveJson(row.getKey(), row.getValue(), "{\"createDate\":\"2001-01-01T00:00:00Z\",\"id\":987654}");
|
||||
assertThat(alone.status()).as(row.getKey() + ": " + alone.body()).isEqualTo(200);
|
||||
assertThat(screen.read(row.getKey(), row.getValue()).get("createDate")).as(row.getKey() + ": the create date is the server's").isEqualTo(before.get("createDate"));
|
||||
|
||||
UiApiResult withAChange = screen.change(row.getKey(), row.getValue(), Map.of("createDate", "2001-01-01T00:00:00Z", textField(row.getKey()), "changed"));
|
||||
assertThat(withAChange.status()).as(row.getKey() + ": " + withAChange.body()).isEqualTo(200);
|
||||
Map<String, Object> after = screen.read(row.getKey(), row.getValue());
|
||||
assertThat(after.get(textField(row.getKey()))).as(row.getKey() + ": the change is stored").isEqualTo("changed");
|
||||
assertThat(after.get("createDate")).as(row.getKey()).isEqualTo(before.get("createDate"));
|
||||
assertThat(after.get("id")).isEqualTo(before.get("id"));
|
||||
}
|
||||
|
||||
UiApiResult created = screen.createJson(AssistantPolicy.TABLE_NAME, "{\"name\":\"second\",\"id\":987654,\"createDate\":\"2001-01-01T00:00:00Z\"}");
|
||||
assertThat(created.status()).as(created.body()).isEqualTo(200);
|
||||
AssistantPolicy second = store().query(AssistantPolicy.TABLE_NAME, new QQueryFilter().withCriteria("name", QCriteriaOperator.EQUALS, "second"), AssistantPolicy.class).get(0);
|
||||
assertThat(second.getId()).as("a new row gets its id from the server").isNotEqualTo(987654);
|
||||
assertThat(second.getCreateDate()).isAfter(java.time.Instant.parse("2020-01-01T00:00:00Z"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A text field of a settings table that a person edits freely.
|
||||
*******************************************************************************/
|
||||
private static String textField(String tableName)
|
||||
{
|
||||
return (switch(tableName)
|
||||
{
|
||||
case AssistantDataAccess.TABLE_NAME -> "notes";
|
||||
case AssistantProfile.TABLE_NAME -> "greeting";
|
||||
case AssistantSkill.TABLE_NAME -> "description";
|
||||
default -> "killSwitchReason";
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private AssistantStore store()
|
||||
{
|
||||
return (config.getRuntime().getStore());
|
||||
}
|
||||
}
|
||||
+192
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import com.kingsrook.qbits.assistant.uiapi.UiApiResult;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** What a record screen of the application sends when a person reads and
|
||||
** saves a record, over the real UI API routes of the test server:
|
||||
**
|
||||
** - a read is `GET /qqq/v1/table/{tableName}/{primaryKey}`;
|
||||
** - a save is `PATCH /qqq/v1/table/{tableName}/{primaryKey}` with a
|
||||
** multipart body of ONLY the fields that were changed (an empty field
|
||||
** clears a value) and the version that was read as `expectedVersion`;
|
||||
** - a new record is `POST /qqq/v1/table/{tableName}` with a multipart body.
|
||||
**
|
||||
** So the record's key reaches the application as text (it is part of the
|
||||
** path), and so does every value (a multipart field is text).
|
||||
*******************************************************************************/
|
||||
public final class RecordScreen
|
||||
{
|
||||
private final String base;
|
||||
private final String userId;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The screen of one signed-in person, against a served application.
|
||||
*******************************************************************************/
|
||||
public RecordScreen(String base, String userId)
|
||||
{
|
||||
this.base = base;
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The values of a record as the screen reads them.
|
||||
*******************************************************************************/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> read(String tableName, Object id) throws Exception
|
||||
{
|
||||
UiApiResult result = send("GET", "/table/" + tableName + "/" + id, null, null);
|
||||
assertThat(result.status()).as(result.body()).isEqualTo(200);
|
||||
return (new LinkedHashMap<>((Map<String, Object>) ((Map<String, Object>) result.json().get("record")).get("values")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The version the screen sends back with a save: the modify date as read.
|
||||
*******************************************************************************/
|
||||
public static String version(Map<String, Object> values)
|
||||
{
|
||||
return (String.valueOf(values.get("modifyDate")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Read the record, then save only these fields from the version read.
|
||||
*******************************************************************************/
|
||||
public UiApiResult change(String tableName, Object id, Map<String, String> changed) throws Exception
|
||||
{
|
||||
return (save(tableName, id, changed, version(read(tableName, id))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Save only these fields, from this version.
|
||||
*******************************************************************************/
|
||||
public UiApiResult save(String tableName, Object id, Map<String, String> changed, String expectedVersion) throws Exception
|
||||
{
|
||||
String query = expectedVersion == null ? "" : "?expectedVersion=" + URLEncoder.encode(expectedVersion, StandardCharsets.UTF_8);
|
||||
return (multipart("PATCH", "/table/" + tableName + "/" + id + query, changed));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Create a record from these fields.
|
||||
*******************************************************************************/
|
||||
public UiApiResult create(String tableName, Map<String, String> values) throws Exception
|
||||
{
|
||||
return (multipart("POST", "/table/" + tableName, values));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Save with a JSON body, as a client other than the record screen may
|
||||
** ("{}" is an update that changes nothing).
|
||||
*******************************************************************************/
|
||||
public UiApiResult saveJson(String tableName, Object id, String json) throws Exception
|
||||
{
|
||||
return (send("PATCH", "/table/" + tableName + "/" + id, "application/json", json));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Create a record from a JSON body.
|
||||
*******************************************************************************/
|
||||
public UiApiResult createJson(String tableName, String json) throws Exception
|
||||
{
|
||||
return (send("POST", "/table/" + tableName, "application/json", json));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Delete a record.
|
||||
*******************************************************************************/
|
||||
public UiApiResult delete(String tableName, Object id) throws Exception
|
||||
{
|
||||
return (send("DELETE", "/table/" + tableName + "/" + id, null, null));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** What the application tells a screen about a table: the capabilities are
|
||||
** what the screen offers buttons for.
|
||||
*******************************************************************************/
|
||||
public UiApiResult tableMetaData(String tableName) throws Exception
|
||||
{
|
||||
return (send("GET", "/metaData/table/" + tableName, null, null));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private UiApiResult multipart(String method, String path, Map<String, String> fields) throws Exception
|
||||
{
|
||||
String boundary = "----record-screen-" + System.nanoTime();
|
||||
StringBuilder body = new StringBuilder();
|
||||
for(Map.Entry<String, String> field : fields.entrySet())
|
||||
{
|
||||
body.append("--").append(boundary).append("\r\n")
|
||||
.append("Content-Disposition: form-data; name=\"").append(field.getKey()).append("\"\r\n\r\n")
|
||||
.append(field.getValue()).append("\r\n");
|
||||
}
|
||||
body.append("--").append(boundary).append("--\r\n");
|
||||
return (send(method, path, "multipart/form-data; boundary=" + boundary, body.toString()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private UiApiResult send(String method, String path, String contentType, String body) throws Exception
|
||||
{
|
||||
HttpRequest.Builder request = HttpRequest.newBuilder(URI.create(base + "/qqq/v1" + path)).header("Cookie", "sessionId=" + userId).header("X-QQQ-Client", "next-ui");
|
||||
if(contentType != null)
|
||||
{
|
||||
request.header("Content-Type", contentType);
|
||||
}
|
||||
request.method(method, body == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8));
|
||||
HttpResponse<String> response = HttpClient.newHttpClient().send(request.build(), HttpResponse.BodyHandlers.ofString());
|
||||
return (new UiApiResult(response.statusCode(), response.body()));
|
||||
}
|
||||
}
|
||||
+216
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.processes.RunProcessInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A record being saved is paired with its stored row whatever type its key
|
||||
** arrives as: text from a request's path, or the type of the key field from
|
||||
** a caller (or a QQQ version) that already made it so.
|
||||
*******************************************************************************/
|
||||
class StoredRecordsTest
|
||||
{
|
||||
private static final String INTEGER_KEY = "integerKey";
|
||||
private static final String LONG_KEY = "longKey";
|
||||
private static final String TEXT_KEY = "textKey";
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** An update of one of three tables that differ only in the type of their
|
||||
** key.
|
||||
*******************************************************************************/
|
||||
private static UpdateInput update(String tableName)
|
||||
{
|
||||
UpdateInput input = new UpdateInput();
|
||||
input.setTableMetaData(switch(tableName)
|
||||
{
|
||||
case INTEGER_KEY -> table(tableName, "id", QFieldType.INTEGER);
|
||||
case LONG_KEY -> table(tableName, "id", QFieldType.LONG);
|
||||
default -> table(tableName, "code", QFieldType.STRING);
|
||||
});
|
||||
return (input);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static QTableMetaData table(String name, String keyField, QFieldType keyType)
|
||||
{
|
||||
return (new QTableMetaData().withName(name).withPrimaryKeyField(keyField)
|
||||
.withField(new QFieldMetaData(keyField, keyType))
|
||||
.withField(new QFieldMetaData("name", QFieldType.STRING).withLabel("Name"))
|
||||
.withField(new QFieldMetaData("quantity", QFieldType.INTEGER).withLabel("Quantity"))
|
||||
.withField(new QFieldMetaData("price", QFieldType.DECIMAL).withLabel("Price"))
|
||||
.withField(new QFieldMetaData("enabled", QFieldType.BOOLEAN))
|
||||
.withField(new QFieldMetaData("seenAt", QFieldType.DATE_TIME).withLabel("Seen At")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** An integer key and a long key: the stored row is found for the key as
|
||||
** text, as an Integer and as a Long; a key of another row, no key, and
|
||||
** text that is no key find none.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aRecordIsPairedWithItsStoredRowWhateverTypeItsKeyArrivesAs()
|
||||
{
|
||||
for(String tableName : List.of(INTEGER_KEY, LONG_KEY))
|
||||
{
|
||||
Serializable storedKey = tableName.equals(INTEGER_KEY) ? (Serializable) 41 : (Serializable) 41L;
|
||||
QRecord row = new QRecord().withValue("id", storedKey).withValue("name", "Widget");
|
||||
StoredRecords stored = StoredRecords.of(update(tableName), Optional.of(List.of(row, new QRecord().withValue("name", "A row without a key"))));
|
||||
|
||||
for(Serializable key : List.<Serializable>of("41", 41, 41L))
|
||||
{
|
||||
assertThat(stored.of(new QRecord().withValue("id", key))).as(tableName + ", key " + key.getClass().getSimpleName()).isSameAs(row);
|
||||
}
|
||||
assertThat(stored.of(new QRecord().withValue("id", "42"))).isNull();
|
||||
assertThat(stored.of(new QRecord().withValue("id", "forty-one"))).as("text that is no key").isNull();
|
||||
assertThat(stored.of(new QRecord().withValue("name", "No key"))).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A text key is compared as the text it is: "007" is not 7.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aTextKeyIsNeverReadAsANumber()
|
||||
{
|
||||
QRecord row = new QRecord().withValue("code", "007");
|
||||
StoredRecords stored = StoredRecords.of(update(TEXT_KEY), Optional.of(List.of(row)));
|
||||
assertThat(stored.of(new QRecord().withValue("code", "007"))).isSameAs(row);
|
||||
assertThat(stored.of(new QRecord().withValue("code", "7"))).isNull();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The merged record is the stored row with the values being saved laid
|
||||
** over it, each as the type of its field; the record being saved is not
|
||||
** changed, a value sent as nothing blanks the field, and a name that is no
|
||||
** field is passed on as it is.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void theMergedRecordIsTheStoredRowWithTheSavedValuesAsTheirFieldsTypes()
|
||||
{
|
||||
QRecord row = new QRecord().withValue("id", 41).withValue("name", "Widget").withValue("quantity", 5).withValue("price", new BigDecimal("19.99")).withValue("enabled", true);
|
||||
StoredRecords stored = StoredRecords.of(update(INTEGER_KEY), Optional.of(List.of(row)));
|
||||
|
||||
QRecord saved = new QRecord().withValue("id", "41").withValue("quantity", "7").withValue("enabled", "false").withValue("seenAt", "2026-11-20T10:15:30Z")
|
||||
.withValue("name", null).withValue("notAField", "12");
|
||||
QRecord merged = stored.merged(saved);
|
||||
|
||||
assertThat(merged.getValue("id")).isEqualTo(41);
|
||||
assertThat(merged.getValue("quantity")).isEqualTo(7);
|
||||
assertThat(merged.getValue("enabled")).isEqualTo(false);
|
||||
assertThat(merged.getValue("seenAt")).isEqualTo(Instant.parse("2026-11-20T10:15:30Z"));
|
||||
assertThat(merged.getValue("price")).as("kept from the stored row").isEqualTo(new BigDecimal("19.99"));
|
||||
assertThat(merged.getValues()).containsEntry("name", null);
|
||||
assertThat(merged.getValue("notAField")).isEqualTo("12");
|
||||
assertThat(saved.getValue("quantity")).as("the record being saved is as it came").isEqualTo("7");
|
||||
assertThat(saved.getErrors()).isEmpty();
|
||||
assertThat(row.getValue("quantity")).as("and so is the stored row").isEqualTo(5);
|
||||
|
||||
QRecord inserted = stored.merged(new QRecord().withValue("quantity", "3"));
|
||||
assertThat(inserted.getValues()).as("an insert has no stored row").containsOnlyKeys("quantity");
|
||||
assertThat(inserted.getValue("quantity")).isEqualTo(3);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A value that cannot be read as the type of its field is refused on the
|
||||
** record being saved, by the field's name and without the value, and the
|
||||
** merged record keeps the stored value.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aValueThatIsNotOfItsFieldsTypeIsRefused()
|
||||
{
|
||||
QRecord row = new QRecord().withValue("id", 41).withValue("quantity", 5).withValue("enabled", true);
|
||||
StoredRecords stored = StoredRecords.of(update(INTEGER_KEY), Optional.of(List.of(row)));
|
||||
|
||||
QRecord saved = new QRecord().withValue("id", 41).withValue("quantity", "a-great-many").withValue("price", "a-great-many");
|
||||
QRecord merged = stored.merged(saved);
|
||||
|
||||
assertThat(saved.getErrors()).extracting(error -> error.getMessage()).containsExactly("The value given for Quantity is not valid.", "The value given for Price is not valid.");
|
||||
assertThat(merged.getValue("quantity")).isEqualTo(5);
|
||||
assertThat(merged.getValue("price")).isNull();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Two values differ only when they differ as the type of their field: text
|
||||
** and the number it spells do not, nor do two decimals of different scale.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void valuesAreComparedAsTheTypeOfTheirField()
|
||||
{
|
||||
StoredRecords stored = StoredRecords.of(update(INTEGER_KEY), Optional.empty());
|
||||
QRecord row = new QRecord().withValue("quantity", 5).withValue("price", new BigDecimal("19.90")).withValue("enabled", true).withValue("name", "Widget");
|
||||
|
||||
assertThat(stored.differs(row, new QRecord().withValue("quantity", "5"), "quantity")).isFalse();
|
||||
assertThat(stored.differs(row, new QRecord().withValue("quantity", 5L), "quantity")).isFalse();
|
||||
assertThat(stored.differs(row, new QRecord().withValue("quantity", "6"), "quantity")).isTrue();
|
||||
assertThat(stored.differs(row, new QRecord().withValue("price", "19.9"), "price")).isFalse();
|
||||
assertThat(stored.differs(row, new QRecord().withValue("price", "19.91"), "price")).isTrue();
|
||||
assertThat(stored.differs(row, new QRecord().withValue("enabled", "true"), "enabled")).isFalse();
|
||||
assertThat(stored.differs(row, new QRecord().withValue("enabled", "false"), "enabled")).isTrue();
|
||||
assertThat(stored.differs(row, new QRecord(), "name")).as("a value against none").isTrue();
|
||||
assertThat(stored.differs(new QRecord(), new QRecord(), "name")).isFalse();
|
||||
assertThat(stored.differs(row, new QRecord().withValue("quantity", "many"), "quantity")).as("text that is no number is not the number").isTrue();
|
||||
assertThat(stored.differs(new QRecord().withValue("quantity", "many"), new QRecord().withValue("quantity", "many"), "quantity")).as("and is not known to be the same as itself").isTrue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** With no table to read the types from, keys and values are taken as
|
||||
** they come.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void withoutATableValuesAreTakenAsTheyCome()
|
||||
{
|
||||
QRecord row = new QRecord().withValue("id", 41).withValue("name", "Widget");
|
||||
StoredRecords stored = StoredRecords.of(new RunProcessInput(), Optional.of(List.of(row)));
|
||||
|
||||
assertThat(stored.of(new QRecord().withValue("id", 41))).isSameAs(row);
|
||||
assertThat(stored.of(new QRecord().withValue("id", "41"))).isNull();
|
||||
assertThat(stored.merged(new QRecord().withValue("id", 41).withValue("quantity", "7")).getValue("quantity")).isEqualTo("7");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.implementations.memory.MemoryRecordStore;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Rows of the fixture's memory backend, put there and read back without any
|
||||
** action of QQQ: no permission, no customizer, no validation. A test of what
|
||||
** a table route refuses needs a row that is there whatever the routes allow,
|
||||
** and needs to see afterwards exactly what is stored.
|
||||
*******************************************************************************/
|
||||
public final class TableRows
|
||||
{
|
||||
public static final Instant LONG_AGO = Instant.parse("2020-02-02T02:02:02Z");
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Utility class - no instances.
|
||||
*******************************************************************************/
|
||||
private TableRows()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Store one row with a value in every field (the owner in userId, dates
|
||||
** long ago), and return it as stored (with its id).
|
||||
*******************************************************************************/
|
||||
public static QRecord seed(QTableMetaData table, String ownerUserId) throws QException
|
||||
{
|
||||
QRecord record = new QRecord();
|
||||
for(QFieldMetaData field : table.getFields().values())
|
||||
{
|
||||
if(!field.getName().equals(table.getPrimaryKeyField()))
|
||||
{
|
||||
record.setValue(field.getName(), field.getName().equals("userId") ? ownerUserId : value(field, 1));
|
||||
}
|
||||
}
|
||||
InsertInput input = new InsertInput(table.getName()).withRecords(new ArrayList<>(List.of(record)));
|
||||
input.setTableMetaData(table);
|
||||
return (MemoryRecordStore.getInstance().insert(input, true).get(0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A value of the type of a field; a different one for each variant.
|
||||
*******************************************************************************/
|
||||
public static Serializable value(QFieldMetaData field, int variant)
|
||||
{
|
||||
return (switch(field.getType())
|
||||
{
|
||||
case STRING, TEXT, HTML, PASSWORD -> "value " + variant;
|
||||
case INTEGER -> 40 + variant;
|
||||
case LONG -> 4000L + variant;
|
||||
case DECIMAL -> new BigDecimal(variant + ".5");
|
||||
case BOOLEAN -> variant % 2 == 1;
|
||||
case DATE -> LocalDate.of(2020, 2, variant);
|
||||
case TIME -> LocalTime.of(variant, 0);
|
||||
case DATE_TIME -> LONG_AGO.plusSeconds(variant);
|
||||
case BLOB -> ("blob " + variant).getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Every row of a table, as stored.
|
||||
*******************************************************************************/
|
||||
public static List<QRecord> all(QTableMetaData table) throws QException
|
||||
{
|
||||
QueryInput input = new QueryInput(table.getName());
|
||||
input.setTableMetaData(table);
|
||||
return (MemoryRecordStore.getInstance().query(input));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The values of every row of a table, as text: to see that nothing moved.
|
||||
*******************************************************************************/
|
||||
public static List<String> snapshot(QTableMetaData table) throws QException
|
||||
{
|
||||
return (all(table).stream().map(row -> new java.util.TreeMap<>(row.getValues()).toString()).sorted().toList());
|
||||
}
|
||||
}
|
||||
+310
@@ -0,0 +1,310 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.metadata;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import com.kingsrook.qbits.assistant.api.AssistantApi;
|
||||
import com.kingsrook.qbits.assistant.metadata.AssistantTableWrites.Operation;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudgetUsage;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantClientError;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantConversation;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantFeedback;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantMessage;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPendingAction;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPendingActionClaim;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantScheduledRun;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantToolCall;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantTurn;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantTurnEvent;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantUsage;
|
||||
import com.kingsrook.qbits.assistant.persistence.AssistantStore;
|
||||
import com.kingsrook.qbits.assistant.testing.AssistantTestFixture;
|
||||
import com.kingsrook.qbits.assistant.testing.BaseAssistantTest;
|
||||
import com.kingsrook.qbits.assistant.testing.TurnRunner;
|
||||
import com.kingsrook.qbits.assistant.testing.UiApiTestServer;
|
||||
import com.kingsrook.qbits.assistant.uiapi.UiApiResult;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The tables the assistant writes as it works (what was said and done, and
|
||||
** what it cost) are written by nobody else. Through the real table routes, a
|
||||
** person - the administrator of the assistant and an ordinary user alike -
|
||||
** cannot add a row, change one (not even with an update that changes
|
||||
** nothing), or delete one; nothing is stored and nothing moves. The
|
||||
** assistant's own work still writes them.
|
||||
*******************************************************************************/
|
||||
class TablesWrittenByTheAssistantTest extends BaseAssistantTest
|
||||
{
|
||||
private static final String ADMIN = AssistantTestFixture.ADMIN;
|
||||
private static final String EDITOR = AssistantTestFixture.EDITOR;
|
||||
private static final String AUTHOR = AssistantTestFixture.AUTHOR;
|
||||
|
||||
private String base;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@BeforeEach
|
||||
void serve()
|
||||
{
|
||||
base = UiApiTestServer.serve(qInstance, config);
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(ADMIN, config));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Every table the assistant writes, with a row of the administrator's own
|
||||
** and a row of the ordinary user's own in it: each of them is refused an
|
||||
** insert, an update, an update that changes nothing and a delete, for
|
||||
** either row, and afterwards every row is exactly what it was.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aPersonWritesNoRowOfATableTheAssistantWrites() throws Exception
|
||||
{
|
||||
List<String> tables = systemWritten();
|
||||
assertThat(tables).as("the accounting the spending limits are counted from is among them")
|
||||
.contains(AssistantUsage.TABLE_NAME, AssistantBudgetUsage.TABLE_NAME, AssistantToolCall.TABLE_NAME, AssistantScheduledRun.TABLE_NAME);
|
||||
|
||||
for(String tableName : tables)
|
||||
{
|
||||
QTableMetaData table = qInstance.getTable(tableName);
|
||||
QRecord ofTheAdmin = TableRows.seed(table, ADMIN);
|
||||
QRecord ofTheEditor = TableRows.seed(table, EDITOR);
|
||||
List<String> before = TableRows.snapshot(table);
|
||||
|
||||
for(String person : List.of(ADMIN, EDITOR))
|
||||
{
|
||||
RecordScreen screen = new RecordScreen(base, person);
|
||||
for(QRecord row : List.of(ofTheAdmin, ofTheEditor))
|
||||
{
|
||||
Object id = row.getValue("id");
|
||||
String what = tableName + ", as " + person + ", row " + id;
|
||||
Map<String, String> changed = changedValues(table);
|
||||
|
||||
refused(screen.save(tableName, id, changed, null), what + ": update", person, table, "changed");
|
||||
refused(screen.saveJson(tableName, id, "{}"), what + ": update that changes nothing", person, table, "changed");
|
||||
refused(screen.saveJson(tableName, id, "{\"modifyDate\":\"2031-01-01T00:00:00Z\"}"), what + ": update of the modify date", person, table, "changed");
|
||||
refused(screen.delete(tableName, id), what + ": delete", person, table, "deleted");
|
||||
}
|
||||
refused(screen.create(tableName, changedValues(table)), tableName + ", as " + person + ": insert", person, table, "added");
|
||||
refused(screen.createJson(tableName, "{}"), tableName + ", as " + person + ": empty insert", person, table, "added");
|
||||
}
|
||||
|
||||
assertThat(TableRows.snapshot(table)).as(tableName + ": every row is what it was, and there is no new one").isEqualTo(before);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** What the application tells a screen: a table the assistant writes has no
|
||||
** capability to insert, update or delete, so a screen offers no button for
|
||||
** it, and QQQ defines no bulk action for it.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aScreenIsOfferedNoWriteOfATableTheAssistantWrites() throws Exception
|
||||
{
|
||||
RecordScreen screen = new RecordScreen(base, ADMIN);
|
||||
for(String tableName : systemWritten())
|
||||
{
|
||||
UiApiResult metaData = screen.tableMetaData(tableName);
|
||||
assertThat(metaData.status()).as(tableName + ": " + metaData.body()).isEqualTo(200);
|
||||
assertThat(metaData.body()).as(tableName).contains("TABLE_QUERY").doesNotContain("TABLE_INSERT").doesNotContain("TABLE_UPDATE").doesNotContain("TABLE_DELETE");
|
||||
for(String bulk : List.of(".bulkInsert", ".bulkEdit", ".bulkEditWithFile", ".bulkDelete"))
|
||||
{
|
||||
assertThat(qInstance.getProcess(tableName + bulk)).as(tableName + bulk).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
UiApiResult settings = screen.tableMetaData("assistant_budget");
|
||||
assertThat(settings.body()).as("a settings table keeps its buttons").contains("TABLE_INSERT").contains("TABLE_UPDATE").contains("TABLE_DELETE");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A conversation is started by the assistant, never added on a screen. Its
|
||||
** owner still renames it and deletes it through the table routes, and what
|
||||
** belongs to a deleted conversation goes with it.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aConversationIsRenamedAndDeletedByItsOwnerButNotAdded() throws Exception
|
||||
{
|
||||
provider.thenText("Four posts are published.");
|
||||
TurnRunner.Outcome outcome = runner.run(EDITOR, "How many posts are published?");
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(ADMIN, config));
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
// the owner key of a person, as a host sign-in sets it on the session of a screen //
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
AssistantTestFixture.giveKey(EDITOR, config.getOwnerSecurityKeyType(), EDITOR);
|
||||
RecordScreen screen = new RecordScreen(base, EDITOR);
|
||||
|
||||
UiApiResult added = screen.create(AssistantConversation.TABLE_NAME, Map.of("title", "Mine", "userId", EDITOR));
|
||||
assertThat(added.status()).as(added.body()).isEqualTo(403);
|
||||
assertThat(added.body()).contains("Rows of Assistant Conversation are written by the assistant; they cannot be added.");
|
||||
assertThat(store().query(AssistantConversation.TABLE_NAME, new QQueryFilter(), AssistantConversation.class)).hasSize(1);
|
||||
|
||||
AssistantConversation asked = store().query(AssistantConversation.TABLE_NAME, new QQueryFilter(), AssistantConversation.class).get(0);
|
||||
UiApiResult renamed = screen.saveJson(AssistantConversation.TABLE_NAME, outcome.conversationId(), "{\"title\":\"Published posts\",\"totalCostMicros\":0,\"unreadCount\":99}");
|
||||
assertThat(renamed.status()).as(renamed.body()).isEqualTo(200);
|
||||
AssistantConversation conversation = store().query(AssistantConversation.TABLE_NAME, new QQueryFilter(), AssistantConversation.class).get(0);
|
||||
assertThat(conversation.getTitle()).isEqualTo("Published posts");
|
||||
assertThat(conversation.getTotalCostMicros()).as("what it cost is not something a save sets").isEqualTo(asked.getTotalCostMicros());
|
||||
assertThat(conversation.getUnreadCount()).isEqualTo(asked.getUnreadCount());
|
||||
|
||||
UiApiResult givenAway = screen.saveJson(AssistantConversation.TABLE_NAME, outcome.conversationId(), "{\"title\":\"Somebody's\",\"userId\":\"somebody-else\"}");
|
||||
assertThat(givenAway.status()).as("nor its owner: " + givenAway.body()).isGreaterThanOrEqualTo(400);
|
||||
assertThat(store().query(AssistantConversation.TABLE_NAME, new QQueryFilter(), AssistantConversation.class).get(0).getUserId()).isEqualTo(EDITOR);
|
||||
|
||||
assertThat(store().query(AssistantMessage.TABLE_NAME, new QQueryFilter(), AssistantMessage.class)).isNotEmpty();
|
||||
UiApiResult deleted = screen.delete(AssistantConversation.TABLE_NAME, outcome.conversationId());
|
||||
assertThat(deleted.status()).as(deleted.body()).isEqualTo(200);
|
||||
assertThat(store().query(AssistantConversation.TABLE_NAME, new QQueryFilter(), AssistantConversation.class)).isEmpty();
|
||||
assertThat(store().query(AssistantMessage.TABLE_NAME, new QQueryFilter(), AssistantMessage.class)).as("its messages went with it").isEmpty();
|
||||
assertThat(store().query(AssistantTurn.TABLE_NAME, new QQueryFilter(), AssistantTurn.class)).isEmpty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The assistant's own work still writes its tables: an answer with a tool
|
||||
** call and a change that is proposed and confirmed, a rating and a client
|
||||
** error sent to the assistant's own API. (Scheduled runs and budget events
|
||||
** are written in ScheduledTasksTest and BudgetServiceTest.)
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void theAssistantsOwnWorkStillWritesItsTables() throws Exception
|
||||
{
|
||||
provider.thenToolUse("update_record", input("table", "page", "id", "12", "values", Map.of("title", "About us"))).thenText("Please confirm.");
|
||||
TurnRunner.Outcome outcome = runner.run(AUTHOR, "Rename page 12 to About us");
|
||||
Integer pending = outcome.toolCalls().get(0).getPendingActionId();
|
||||
assertThat(pending).as("the change was proposed").isNotNull();
|
||||
|
||||
AssistantApi api = new AssistantApi(config.getRuntime(), AssistantTestFixture.ownerSession(AUTHOR, config));
|
||||
Map<String, Object> confirmed = api.confirmAction(pending);
|
||||
assertThat(confirmed.get("status")).isEqualTo(AssistantPendingAction.STATUS_EXECUTED);
|
||||
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(ADMIN, config));
|
||||
AssistantMessage answer = store().query(AssistantMessage.TABLE_NAME, new QQueryFilter().withCriteria("role", QCriteriaOperator.EQUALS, "ASSISTANT"), AssistantMessage.class).get(0);
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(AUTHOR, config));
|
||||
api.feedback(answer.getId(), Map.of("rating", 1));
|
||||
assertThat(api.reportClientError(Map.of("scope", "panel", "message", "It did not draw."))).containsEntry("accepted", true);
|
||||
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(ADMIN, config));
|
||||
Map<String, Integer> rows = new LinkedHashMap<>();
|
||||
for(String tableName : List.of(AssistantConversation.TABLE_NAME, AssistantMessage.TABLE_NAME, AssistantTurn.TABLE_NAME, AssistantTurnEvent.TABLE_NAME,
|
||||
AssistantToolCall.TABLE_NAME, AssistantPendingAction.TABLE_NAME, AssistantPendingActionClaim.TABLE_NAME, AssistantUsage.TABLE_NAME, AssistantBudgetUsage.TABLE_NAME,
|
||||
AssistantFeedback.TABLE_NAME, AssistantClientError.TABLE_NAME))
|
||||
{
|
||||
rows.put(tableName, TableRows.all(qInstance.getTable(tableName)).size());
|
||||
}
|
||||
assertThat(rows).allSatisfy((tableName, count) -> assertThat(count).as("rows written to " + tableName).isPositive());
|
||||
assertThat(rows.get(AssistantBudgetUsage.TABLE_NAME)).as("one accounting row for every model call").isEqualTo(rows.get(AssistantUsage.TABLE_NAME));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The tables a person writes nothing of.
|
||||
*******************************************************************************/
|
||||
private List<String> systemWritten()
|
||||
{
|
||||
List<String> tables = new ArrayList<>();
|
||||
for(QTableMetaData table : qInstance.getTables().values())
|
||||
{
|
||||
Set<Operation> byAPerson = AssistantTableWrites.byAPerson(table.getName());
|
||||
if(byAPerson != null && byAPerson.isEmpty())
|
||||
{
|
||||
tables.add(table.getName());
|
||||
}
|
||||
}
|
||||
assertThat(tables).hasSizeGreaterThanOrEqualTo(14);
|
||||
return (tables);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A value for every text field of a table (other than its key and owner):
|
||||
** what a request that tried to change a row would carry.
|
||||
*******************************************************************************/
|
||||
private static Map<String, String> changedValues(QTableMetaData table)
|
||||
{
|
||||
Map<String, String> values = new LinkedHashMap<>();
|
||||
for(QFieldMetaData field : table.getFields().values())
|
||||
{
|
||||
if(!field.getName().equals("id") && !field.getName().equals("userId") && (field.getType() == QFieldType.STRING || field.getType() == QFieldType.TEXT))
|
||||
{
|
||||
values.put(field.getName(), "changed");
|
||||
}
|
||||
else if(field.getType() == QFieldType.INTEGER && !field.getName().equals("id"))
|
||||
{
|
||||
values.put(field.getName(), "0");
|
||||
}
|
||||
else if(field.getType() == QFieldType.LONG)
|
||||
{
|
||||
values.put(field.getName(), "0");
|
||||
}
|
||||
}
|
||||
assertThat(values).as("something to change in " + table.getName()).isNotEmpty();
|
||||
return (values);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A request was refused with 403. The administrator, who may do everything
|
||||
** a person may do to the assistant, is told why.
|
||||
*******************************************************************************/
|
||||
private static void refused(UiApiResult result, String what, String person, QTableMetaData table, String done)
|
||||
{
|
||||
assertThat(result.status()).as(what + ": " + result.body()).isEqualTo(403);
|
||||
if(person.equals(ADMIN))
|
||||
{
|
||||
assertThat(result.body()).as(what).contains("Rows of " + table.getLabel() + " are written by the assistant; they cannot be " + done + ".");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private AssistantStore store()
|
||||
{
|
||||
return (config.getRuntime().getStore());
|
||||
}
|
||||
}
|
||||
+329
@@ -0,0 +1,329 @@
|
||||
/*
|
||||
* Copyright 2026 Kingsrook, LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qbits.assistant.persistence;
|
||||
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import com.kingsrook.qbits.assistant.metadata.AssistantMetaDataBuilder;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudgetEvent;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantBudgetUsage;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantClientError;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantDataAudit;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantFeedback;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPendingAction;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPendingActionClaim;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantScheduledRun;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantToolCall;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantTurnEvent;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantUsage;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantUsageDaily;
|
||||
import com.kingsrook.qbits.assistant.spi.ModelUsage;
|
||||
import com.kingsrook.qbits.assistant.testing.AssistantTestFixture;
|
||||
import com.kingsrook.qbits.assistant.testing.BaseAssistantTest;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.DynamicDefaultValueBehavior;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The table standard: every table the QBit declares has a createDate and a
|
||||
** modifyDate (date-time, not editable, set by QQQ), and every write path of
|
||||
** the store sets them: an insert sets both, an update moves modifyDate and
|
||||
** leaves createDate. A row of a table that is only ever inserted (a log, an
|
||||
** audit row, a claim) has a modifyDate equal to its createDate, always.
|
||||
*******************************************************************************/
|
||||
class TableStandardDatesTest extends BaseAssistantTest
|
||||
{
|
||||
private static final String USER = AssistantTestFixture.ADMIN;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Every table the producer declares, found from the metadata and not listed
|
||||
** by hand, has both dates as the standard says.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void everyDeclaredTableHasBothDates() throws Exception
|
||||
{
|
||||
List<QTableMetaData> declared = new AssistantMetaDataBuilder(config).buildTables();
|
||||
assertThat(declared).hasSizeGreaterThan(20);
|
||||
for(QTableMetaData declaredTable : declared)
|
||||
{
|
||||
QTableMetaData table = qInstance.getTable(declaredTable.getName());
|
||||
assertThat(table).as(declaredTable.getName() + " is in the instance").isNotNull();
|
||||
assertDate(table, "createDate", DynamicDefaultValueBehavior.CREATE_DATE);
|
||||
assertDate(table, "modifyDate", DynamicDefaultValueBehavior.MODIFY_DATE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static void assertDate(QTableMetaData table, String name, DynamicDefaultValueBehavior behavior)
|
||||
{
|
||||
QFieldMetaData field = table.getFields().get(name);
|
||||
assertThat(field).as(table.getName() + "." + name).isNotNull();
|
||||
assertThat(field.getType()).as(table.getName() + "." + name + " type").isEqualTo(QFieldType.DATE_TIME);
|
||||
assertThat(field.getIsEditable()).as(table.getName() + "." + name + " editable").isFalse();
|
||||
assertThat(field.getBehaviors()).as(table.getName() + "." + name + " behaviors").contains(behavior);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A usage counter row that is incremented moves its modifyDate each time.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void incrementedUsageCounterMovesModifyDate() throws Exception
|
||||
{
|
||||
AssistantStore store = store();
|
||||
LocalDate day = LocalDate.of(2026, 1, 15);
|
||||
ModelUsage usage = new ModelUsage();
|
||||
|
||||
store.addUsage(USER, day, usage, 10L, true);
|
||||
AssistantUsageDaily first = store.getUserUsage(USER, day);
|
||||
assertThat(first.getCreateDate()).isNotNull();
|
||||
assertThat(first.getModifyDate()).isNotNull();
|
||||
|
||||
pause();
|
||||
store.addUsage(USER, day, usage, 10L, true);
|
||||
AssistantUsageDaily second = store.getUserUsage(USER, day);
|
||||
assertThat(second.getTurnCount()).isEqualTo(2);
|
||||
assertThat(second.getCreateDate()).isEqualTo(first.getCreateDate());
|
||||
assertThat(second.getModifyDate()).isAfter(first.getModifyDate());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Rows written once (a decision claim, a budget event) have modifyDate equal
|
||||
** to createDate, exactly: QQQ's two behaviors read the clock separately, so
|
||||
** the table's insert customizer makes them one instant. Rows of the tables
|
||||
** that are inserted and, only when a transcript is redacted, rewritten, have
|
||||
** both dates set, modifyDate not before createDate.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void insertOnlyRowsHaveEqualDates() throws Exception
|
||||
{
|
||||
AssistantStore store = store();
|
||||
Instant now = Instant.parse("2026-02-03T04:05:06Z");
|
||||
|
||||
for(int i = 0; i < 20; i++)
|
||||
{
|
||||
AssistantBudgetEvent budgetEvent = store.insert(new AssistantBudgetEvent().withBudgetId(1).withScopeKey("k" + i).withPeriodStart(now).withState("WARN")
|
||||
.withUsedValue(BigDecimal.ONE).withLimitValue(BigDecimal.TEN));
|
||||
assertEqualDates(budgetEvent.getCreateDate(), budgetEvent.getModifyDate());
|
||||
|
||||
AssistantPendingActionClaim claim = store.insert(new AssistantPendingActionClaim().withPendingActionId(100 + i).withUserId(USER).withDecision("EXECUTED"));
|
||||
assertEqualDates(claim.getCreateDate(), claim.getModifyDate());
|
||||
}
|
||||
|
||||
AssistantTurnEvent event = store.insert(new AssistantTurnEvent().withTurnId(1).withUserId(USER).withSeq(1).withEventType("text.delta").withDataJson("{}"));
|
||||
assertBothSet(event.getCreateDate(), event.getModifyDate());
|
||||
|
||||
AssistantFeedback feedback = store.insert(new AssistantFeedback().withUserId(USER).withRating(1));
|
||||
assertBothSet(feedback.getCreateDate(), feedback.getModifyDate());
|
||||
|
||||
AssistantClientError error = store.insert(new AssistantClientError().withUserId(USER).withScope("test").withMessage("m"));
|
||||
assertBothSet(error.getCreateDate(), error.getModifyDate());
|
||||
|
||||
AssistantDataAudit audit = store.insert(new AssistantDataAudit().withUserId(USER).withDecision("ALLOWED"));
|
||||
assertBothSet(audit.getCreateDate(), audit.getModifyDate());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static void assertBothSet(Instant createDate, Instant modifyDate)
|
||||
{
|
||||
assertThat(createDate).isNotNull();
|
||||
assertThat(modifyDate).isNotNull().isAfterOrEqualTo(createDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A decision of a pending action: the action moves its modifyDate, and the
|
||||
** claim row is written once with equal dates.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void pendingActionDecisionMovesTheActionAndInsertsTheClaim() throws Exception
|
||||
{
|
||||
AssistantStore store = store();
|
||||
Instant now = Instant.parse("2026-02-03T04:05:06Z");
|
||||
|
||||
AssistantPendingAction action = store.insert(new AssistantPendingAction().withUserId(USER).withToolName("update_record").withStatus(AssistantPendingAction.STATUS_PENDING));
|
||||
assertThat(action.getCreateDate()).isNotNull();
|
||||
assertThat(action.getModifyDate()).isNotNull();
|
||||
|
||||
pause();
|
||||
assertThat(store.decidePendingAction(USER, action.getId(), AssistantPendingAction.STATUS_RUNNING, null, now)).isTrue();
|
||||
|
||||
AssistantPendingAction decided = store.getPendingAction(USER, action.getId());
|
||||
assertThat(decided.getStatus()).isEqualTo(AssistantPendingAction.STATUS_RUNNING);
|
||||
assertThat(decided.getCreateDate()).isEqualTo(action.getCreateDate());
|
||||
assertThat(decided.getModifyDate()).isAfter(action.getModifyDate());
|
||||
|
||||
List<AssistantPendingActionClaim> claims = store.query(AssistantPendingActionClaim.TABLE_NAME,
|
||||
new QQueryFilter().withCriteria("pendingActionId", QCriteriaOperator.EQUALS, action.getId()), AssistantPendingActionClaim.class);
|
||||
assertThat(claims).hasSize(1);
|
||||
assertEqualDates(claims.get(0).getCreateDate(), claims.get(0).getModifyDate());
|
||||
|
||||
pause();
|
||||
store.finishPendingAction(action.getId(), AssistantPendingAction.STATUS_EXECUTED, 200, "done");
|
||||
AssistantPendingAction finished = store.getPendingAction(USER, action.getId());
|
||||
assertThat(finished.getCreateDate()).isEqualTo(action.getCreateDate());
|
||||
assertThat(finished.getModifyDate()).isAfter(decided.getModifyDate());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Clearing a tool call's result (the retention step) is an update.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void clearingAToolResultMovesModifyDate() throws Exception
|
||||
{
|
||||
AssistantStore store = store();
|
||||
|
||||
AssistantToolCall call = store.insert(new AssistantToolCall().withUserId(USER).withToolName("query_records").withResultJson("{}"));
|
||||
assertThat(call.getCreateDate()).isNotNull();
|
||||
assertThat(call.getModifyDate()).isNotNull();
|
||||
|
||||
pause();
|
||||
assertThat(store.clearToolResultsBefore(Instant.now().plusSeconds(60), 10)).isEqualTo(1);
|
||||
|
||||
AssistantToolCall cleared = store.query(AssistantToolCall.TABLE_NAME, new QQueryFilter().withCriteria("id", QCriteriaOperator.EQUALS, call.getId()), AssistantToolCall.class).get(0);
|
||||
assertThat(cleared.getResultJson()).isNull();
|
||||
assertThat(cleared.getCreateDate()).isEqualTo(call.getCreateDate());
|
||||
assertThat(cleared.getModifyDate()).isAfter(call.getModifyDate());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A scheduled run is inserted when it is claimed and updated, as a whole
|
||||
** entity, when it ends.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void scheduledRunUpdateMovesModifyDate() throws Exception
|
||||
{
|
||||
AssistantStore store = store();
|
||||
|
||||
AssistantScheduledRun run = store.insert(new AssistantScheduledRun().withTaskId(1).withUserId(USER).withScheduledFor(Instant.parse("2026-02-03T04:05:06Z")).withStatus("RUNNING"));
|
||||
assertThat(run.getCreateDate()).isNotNull();
|
||||
assertThat(run.getModifyDate()).isNotNull();
|
||||
|
||||
pause();
|
||||
store.update(run.withStatus("SUCCEEDED"));
|
||||
|
||||
AssistantScheduledRun ended = store.query(AssistantScheduledRun.TABLE_NAME, new QQueryFilter().withCriteria("id", QCriteriaOperator.EQUALS, run.getId()), AssistantScheduledRun.class).get(0);
|
||||
assertThat(ended.getStatus()).isEqualTo("SUCCEEDED");
|
||||
assertThat(ended.getCreateDate()).isEqualTo(run.getCreateDate());
|
||||
assertThat(ended.getModifyDate()).isAfter(run.getModifyDate());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The budget accounting row is a projection of a usage row: written with
|
||||
** the usage, and moved when the usage is updated.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void budgetProjectionFollowsItsUsageRow() throws Exception
|
||||
{
|
||||
AssistantStore store = store();
|
||||
|
||||
AssistantUsage usage = store.insert(new AssistantUsage().withUserId(USER).withModel("m").withCostMicros(5L));
|
||||
assertThat(usage.getCreateDate()).isNotNull();
|
||||
assertThat(usage.getModifyDate()).isNotNull();
|
||||
AssistantBudgetUsage projection = projectionOf(store, usage);
|
||||
assertThat(projection.getCreateDate()).isNotNull();
|
||||
assertThat(projection.getModifyDate()).isNotNull();
|
||||
|
||||
pause();
|
||||
store.updateFields(AssistantUsage.TABLE_NAME, new QRecord().withValue("id", usage.getId()).withValue("costMicros", 9L));
|
||||
|
||||
AssistantUsage updated = store.query(AssistantUsage.TABLE_NAME, new QQueryFilter().withCriteria("id", QCriteriaOperator.EQUALS, usage.getId()), AssistantUsage.class).get(0);
|
||||
assertThat(updated.getCreateDate()).isEqualTo(usage.getCreateDate());
|
||||
assertThat(updated.getModifyDate()).isAfter(usage.getModifyDate());
|
||||
|
||||
AssistantBudgetUsage moved = projectionOf(store, usage);
|
||||
assertThat(moved.getCostMicros()).isEqualTo(9L);
|
||||
assertThat(moved.getCreateDate()).isEqualTo(projection.getCreateDate());
|
||||
assertThat(moved.getModifyDate()).isAfter(projection.getModifyDate());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static AssistantBudgetUsage projectionOf(AssistantStore store, AssistantUsage usage) throws Exception
|
||||
{
|
||||
List<AssistantBudgetUsage> rows = store.query(AssistantBudgetUsage.TABLE_NAME, new QQueryFilter().withCriteria("usageId", QCriteriaOperator.EQUALS, usage.getId()), AssistantBudgetUsage.class);
|
||||
assertThat(rows).hasSize(1);
|
||||
return (rows.get(0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static void assertEqualDates(Instant createDate, Instant modifyDate)
|
||||
{
|
||||
assertThat(createDate).isNotNull();
|
||||
assertThat(modifyDate).isNotNull().isEqualTo(createDate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The store, under an owner's session.
|
||||
*******************************************************************************/
|
||||
private AssistantStore store()
|
||||
{
|
||||
QContext.init(qInstance, AssistantTestFixture.ownerSession(USER, config));
|
||||
return (config.getRuntime().getStore());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Let the clock move, so a later write is told from an earlier one.
|
||||
*******************************************************************************/
|
||||
private static void pause() throws InterruptedException
|
||||
{
|
||||
Thread.sleep(25);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -42,6 +42,7 @@ import com.kingsrook.qbits.assistant.spi.EmbeddingResponse;
|
||||
import com.kingsrook.qbits.assistant.spi.ModelPart;
|
||||
import com.kingsrook.qbits.assistant.spi.ModelResponse;
|
||||
import com.kingsrook.qbits.assistant.spi.ModelUsage;
|
||||
import com.kingsrook.qbits.assistant.metadata.RecordScreen;
|
||||
import com.kingsrook.qbits.assistant.testing.AssistantTestFixture;
|
||||
import com.kingsrook.qbits.assistant.testing.BaseAssistantTest;
|
||||
import com.kingsrook.qbits.assistant.testing.UiApiTestServer;
|
||||
@@ -49,6 +50,7 @@ import com.kingsrook.qbits.assistant.tools.KnowledgeTool;
|
||||
import com.kingsrook.qbits.assistant.tools.Toolboxes;
|
||||
import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
|
||||
import com.kingsrook.qbits.assistant.uiapi.UiApiCall;
|
||||
import com.kingsrook.qbits.assistant.uiapi.UiApiResult;
|
||||
import com.kingsrook.qqq.backend.core.actions.ai.AiExposure;
|
||||
import com.kingsrook.qqq.backend.core.actions.ai.AiFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.actions.ai.AiToolOutcome;
|
||||
@@ -230,6 +232,49 @@ class RagTest extends BaseAssistantTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A knowledge source is edited on its record screen one field at a time: a
|
||||
** save of its name is held to the rules on the stored source, and keeps
|
||||
** its vectors; a save that changes what the vectors mean drops them.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aSourceIsSavedOneFieldAtATimeFromItsScreen() throws Exception
|
||||
{
|
||||
RecordScreen screen = new RecordScreen(UiApiTestServer.serve(qInstance, config), AssistantTestFixture.ADMIN);
|
||||
assertThat(config.getRuntime().getRagService().index(source).errors()).isZero();
|
||||
var indexed = vectors.count(source.getId());
|
||||
assertThat(indexed).isPositive();
|
||||
|
||||
Map<String, Object> expected = screen.read(AssistantRagSource.TABLE_NAME, source.getId());
|
||||
UiApiResult renamed = screen.change(AssistantRagSource.TABLE_NAME, source.getId(), Map.of("name", "Blog posts"));
|
||||
assertThat(renamed.status()).as(renamed.body()).isEqualTo(200);
|
||||
Map<String, Object> after = screen.read(AssistantRagSource.TABLE_NAME, source.getId());
|
||||
expected.put("name", "Blog posts");
|
||||
expected.put("modifyDate", after.get("modifyDate"));
|
||||
assertThat(after).as("only the name changed").isEqualTo(expected);
|
||||
assertThat(vectors.count(source.getId())).as("a new name leaves the vectors").isEqualTo(indexed);
|
||||
|
||||
UiApiResult status = screen.saveJson(AssistantRagSource.TABLE_NAME, source.getId(), "{\"indexedChunkCount\":0,\"lastIndexStatus\":\"FAILED\",\"createDate\":\"2001-01-01T00:00:00Z\"}");
|
||||
assertThat(status.status()).as(status.body()).isEqualTo(200);
|
||||
Map<String, Object> afterStatus = screen.read(AssistantRagSource.TABLE_NAME, source.getId());
|
||||
after.put("modifyDate", afterStatus.get("modifyDate"));
|
||||
assertThat(afterStatus).as("what was indexed and when it was created are the server's").isEqualTo(after);
|
||||
|
||||
UiApiResult same = screen.change(AssistantRagSource.TABLE_NAME, source.getId(), Map.of("chunkChars", "1500"));
|
||||
assertThat(same.status()).as(same.body()).isEqualTo(200);
|
||||
assertThat(vectors.count(source.getId())).as("the chunk size it already has, sent as text, is no change").isEqualTo(indexed);
|
||||
|
||||
UiApiResult tooSmall = screen.change(AssistantRagSource.TABLE_NAME, source.getId(), Map.of("chunkChars", "50"));
|
||||
assertThat(tooSmall.status()).isEqualTo(400);
|
||||
assertThat(tooSmall.body()).contains("Chunk size must be 100 to 16000");
|
||||
|
||||
UiApiResult chunks = screen.change(AssistantRagSource.TABLE_NAME, source.getId(), Map.of("chunkChars", "800"));
|
||||
assertThat(chunks.status()).as(chunks.body()).isEqualTo(200);
|
||||
assertThat(vectors.count(source.getId())).as("another chunk size drops the vectors").isZero();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** The session used both for configuration reads and UI API calls.
|
||||
*******************************************************************************/
|
||||
|
||||
+56
@@ -41,6 +41,7 @@ import com.kingsrook.qbits.assistant.RuntimeInternals;
|
||||
import com.kingsrook.qbits.assistant.AssistantQBitConfig;
|
||||
import com.kingsrook.qbits.assistant.AssistantRuntime;
|
||||
import com.kingsrook.qbits.assistant.AssistantSecurity;
|
||||
import com.kingsrook.qbits.assistant.metadata.RecordScreen;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantDataAccess;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPendingAction;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantPolicy;
|
||||
@@ -57,6 +58,7 @@ import com.kingsrook.qbits.assistant.testing.AssistantTestFixture;
|
||||
import com.kingsrook.qbits.assistant.testing.BaseAssistantTest;
|
||||
import com.kingsrook.qbits.assistant.testing.UiApiTestServer;
|
||||
import com.kingsrook.qbits.assistant.uiapi.UiApiCall;
|
||||
import com.kingsrook.qbits.assistant.uiapi.UiApiResult;
|
||||
import com.kingsrook.qqq.backend.core.actions.ai.DataClassification;
|
||||
import com.kingsrook.qqq.backend.core.actions.ai.AiFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.actions.tables.QueryAction;
|
||||
@@ -606,6 +608,60 @@ class ScheduledTasksTest extends BaseAssistantTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A scheduled task is edited on its record screen one field at a time: a
|
||||
** save of its name is held to the rules on the stored task, and leaves the
|
||||
** approval of its writes and its next run as they are; a save of its
|
||||
** prompt takes the approval away.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aTaskIsSavedOneFieldAtATimeFromItsScreen() throws Exception
|
||||
{
|
||||
AssistantScheduledTask task = task();
|
||||
secrets.put("SCHEDULED_TOKEN", AssistantTestFixture.ADMIN);
|
||||
runtime.getStore().updateFields(AssistantScheduledTask.TABLE_NAME, new QRecord().withValue("id", task.getId()).withValue("runAsUserId", AssistantTestFixture.ADMIN).withValue("writeModeRequested", true));
|
||||
runtime.getStore().scheduledUpdate(new QRecord().withValue("id", task.getId()).withValue("nextRunAt", NOW.minusSeconds(60)));
|
||||
action(runtime.getStore().scheduledTask(task.getId()), "Approve");
|
||||
AssistantScheduledTask approved = runtime.getStore().scheduledTask(task.getId());
|
||||
assertThat(ScheduledTasks.writesApproved(approved)).isTrue();
|
||||
|
||||
RecordScreen screen = new RecordScreen(UiApiTestServer.serve(qInstance, config), AssistantTestFixture.ADMIN);
|
||||
UiApiResult renamed = screen.change(AssistantScheduledTask.TABLE_NAME, task.getId(), Map.of("name", "Morning report, weekdays"));
|
||||
assertThat(renamed.status()).as(renamed.body()).isEqualTo(200);
|
||||
AssistantScheduledTask stored = runtime.getStore().scheduledTask(task.getId());
|
||||
assertThat(stored.getName()).isEqualTo("Morning report, weekdays");
|
||||
assertThat(stored.getPrompt()).isEqualTo(approved.getPrompt());
|
||||
assertThat(stored.getCronExpression()).isEqualTo(approved.getCronExpression());
|
||||
assertThat(stored.getTimezone()).isEqualTo(approved.getTimezone());
|
||||
assertThat(stored.getEnabled()).isTrue();
|
||||
assertThat(ScheduledTasks.writesApproved(stored)).as("a new name does not take the approval away").isTrue();
|
||||
assertThat(stored.getWriteModeApprovedBy()).isEqualTo(approved.getWriteModeApprovedBy());
|
||||
assertThat(stored.getNextRunAt()).as("a new name does not move the next run").isEqualTo(NOW.minusSeconds(60));
|
||||
|
||||
UiApiResult same = screen.change(AssistantScheduledTask.TABLE_NAME, task.getId(), Map.of("writeModeRequested", "true", "enabled", "true"));
|
||||
assertThat(same.status()).as(same.body()).isEqualTo(200);
|
||||
assertThat(ScheduledTasks.writesApproved(runtime.getStore().scheduledTask(task.getId()))).as("the values it already has, sent as text, are no change").isTrue();
|
||||
assertThat(runtime.getStore().scheduledTask(task.getId()).getNextRunAt()).isEqualTo(NOW.minusSeconds(60));
|
||||
|
||||
UiApiResult blank = screen.change(AssistantScheduledTask.TABLE_NAME, task.getId(), Map.of("prompt", ""));
|
||||
assertThat(blank.status()).as("a task is not left without a prompt").isEqualTo(400);
|
||||
assertThat(ScheduledTasks.writesApproved(runtime.getStore().scheduledTask(task.getId()))).isTrue();
|
||||
|
||||
UiApiResult prompt = screen.change(AssistantScheduledTask.TABLE_NAME, task.getId(), Map.of("prompt", "Summarize and publish the posts of today"));
|
||||
assertThat(prompt.status()).as(prompt.body()).isEqualTo(200);
|
||||
assertThat(runtime.getStore().scheduledTask(task.getId()).getWriteModeApprovedAt()).as("a changed prompt was not approved").isNull();
|
||||
|
||||
Instant created = runtime.getStore().scheduledTask(task.getId()).getCreateDate();
|
||||
UiApiResult approval = screen.change(AssistantScheduledTask.TABLE_NAME, task.getId(), Map.of("writeModeApprovedBy", "somebody", "writeModeApprovedAt", NOW.toString(), "name", "Mine",
|
||||
"lastRunStatus", "OK", "createDate", "2001-01-01T00:00:00Z"));
|
||||
AssistantScheduledTask afterwards = runtime.getStore().scheduledTask(task.getId());
|
||||
assertThat(afterwards.getWriteModeApprovedBy()).as("an approval is not something a save sets: " + approval.body()).isNull();
|
||||
assertThat(afterwards.getLastRunStatus()).as("nor how its last run ended").isNull();
|
||||
assertThat(afterwards.getCreateDate()).as("nor when it was created").isEqualTo(created);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Application record locks stay effective with a designated-user login.
|
||||
*******************************************************************************/
|
||||
|
||||
+47
@@ -35,6 +35,7 @@ import com.kingsrook.qbits.assistant.home.AssistantHome;
|
||||
import com.kingsrook.qbits.assistant.home.AssistantHomeData;
|
||||
import com.kingsrook.qbits.assistant.home.AssistantHomeView;
|
||||
import com.kingsrook.qbits.assistant.metadata.AssistantRecordScreens;
|
||||
import com.kingsrook.qbits.assistant.metadata.RecordScreen;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantModel;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantModelPrice;
|
||||
import com.kingsrook.qbits.assistant.model.AssistantProvider;
|
||||
@@ -51,6 +52,7 @@ import com.kingsrook.qbits.assistant.testing.AssistantTestFixture;
|
||||
import com.kingsrook.qbits.assistant.testing.UiApiTestServer;
|
||||
import com.kingsrook.qbits.assistant.testing.BaseAssistantTest;
|
||||
import com.kingsrook.qbits.assistant.testing.TurnRunner;
|
||||
import com.kingsrook.qbits.assistant.uiapi.UiApiResult;
|
||||
import com.kingsrook.qqq.backend.core.actions.ai.AiExposure;
|
||||
import com.kingsrook.qqq.backend.core.actions.ai.AiTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.actions.customizers.QCodeLoader;
|
||||
@@ -657,6 +659,51 @@ class AssistantSettingsTest extends BaseAssistantTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** A provider and a registered model are edited on their record screens one
|
||||
** field at a time: a save of one field is held to the rules on the stored
|
||||
** row, and leaves every other field as it is.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void aProviderAndAModelAreSavedOneFieldAtATimeFromTheirScreens() throws Exception
|
||||
{
|
||||
asAdmin();
|
||||
Integer id = providerId();
|
||||
AssistantModel model = new AssistantStore().insert(new AssistantModel().withProviderId(id).withModelId("registered-chat").withEnabled(true).withModelKind("CHAT"));
|
||||
RecordScreen screen = new RecordScreen(UiApiTestServer.serve(qInstance, config), AssistantTestFixture.ADMIN);
|
||||
|
||||
Map<String, Object> expected = screen.read(AssistantProvider.TABLE_NAME, id);
|
||||
UiApiResult renamed = screen.change(AssistantProvider.TABLE_NAME, id, Map.of("name", "Renamed"));
|
||||
assertThat(renamed.status()).as(renamed.body()).isEqualTo(200);
|
||||
Map<String, Object> after = screen.read(AssistantProvider.TABLE_NAME, id);
|
||||
expected.put("name", "Renamed");
|
||||
expected.put("modifyDate", after.get("modifyDate"));
|
||||
assertThat(after).as("only the name changed").isEqualTo(expected);
|
||||
|
||||
UiApiResult status = screen.saveJson(AssistantProvider.TABLE_NAME, id, "{\"healthState\":\"DOWN\",\"lastTestStatus\":\"FAILED\",\"downUntil\":\"2099-01-01T00:00:00Z\",\"createDate\":\"2001-01-01T00:00:00Z\"}");
|
||||
assertThat(status.status()).as(status.body()).isEqualTo(200);
|
||||
Map<String, Object> afterStatus = screen.read(AssistantProvider.TABLE_NAME, id);
|
||||
after.put("modifyDate", afterStatus.get("modifyDate"));
|
||||
assertThat(afterStatus).as("its health, its test result and its dates are the server's").isEqualTo(after);
|
||||
|
||||
UiApiResult unknownType = screen.change(AssistantProvider.TABLE_NAME, id, Map.of("providerType", "nope"));
|
||||
assertThat(unknownType.status()).isEqualTo(400);
|
||||
assertThat(unknownType.body()).contains("not installed");
|
||||
UiApiResult cap = screen.change(AssistantProvider.TABLE_NAME, id, Map.of("monthlyCostCapUsd", "-1"));
|
||||
assertThat(cap.status()).as("a rule of the whole row holds for a save of one field").isEqualTo(400);
|
||||
assertThat(screen.read(AssistantProvider.TABLE_NAME, id)).as("nothing stored").isEqualTo(after);
|
||||
|
||||
Map<String, Object> expectedModel = screen.read(AssistantModel.TABLE_NAME, model.getId());
|
||||
UiApiResult disabled = screen.change(AssistantModel.TABLE_NAME, model.getId(), Map.of("enabled", "false"));
|
||||
assertThat(disabled.status()).as(disabled.body()).isEqualTo(200);
|
||||
Map<String, Object> afterModel = screen.read(AssistantModel.TABLE_NAME, model.getId());
|
||||
expectedModel.put("enabled", false);
|
||||
expectedModel.put("modifyDate", afterModel.get("modifyDate"));
|
||||
assertThat(afterModel).as("only the switch changed").isEqualTo(expectedModel);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Kinds use the selected type and keep their existing labels.
|
||||
*******************************************************************************/
|
||||
|
||||
Reference in new issue
Block a user