@segmentstream/ontology 0.156.0 → 0.158.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -0
- package/README.md +8 -7
- package/dist/agent-chat.d.ts +1 -1
- package/dist/agent-chat.js +8 -8
- package/dist/agent-draft.d.ts +287 -0
- package/dist/{agent-review.js → agent-draft.js} +103 -82
- package/dist/analyze.d.ts +1 -1
- package/dist/analyze.js +71 -14
- package/dist/channel-files.d.ts +70 -0
- package/dist/channel-files.js +58 -0
- package/dist/chat.d.ts +2 -2
- package/dist/earlier-entity-names.d.ts +35 -0
- package/dist/earlier-entity-names.js +88 -0
- package/dist/email.d.ts +2 -0
- package/dist/email.js +1 -0
- package/dist/gmail-send.d.ts +39 -0
- package/dist/gmail-send.js +179 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -1
- package/dist/migrate/plan.js +31 -2
- package/dist/permissions.d.ts +5 -5
- package/dist/permissions.js +13 -10
- package/dist/replicated-entities.d.ts +1 -1
- package/dist/replicated-entities.js +4 -1
- package/dist/replication/bigquery.d.ts +24 -2
- package/dist/replication/bigquery.js +72 -12
- package/dist/runtime/agents.d.ts +1 -1
- package/dist/runtime/agents.js +43 -38
- package/dist/runtime/chat-messages.d.ts +8 -7
- package/dist/runtime/chat-messages.js +11 -10
- package/dist/runtime/service-binding.js +1 -1
- package/dist/service/agent/protocol.d.ts +4 -4
- package/dist/service/agent/protocol.js +7 -7
- package/dist/service/agent/server.js +14 -9
- package/dist/service/chat/envelope.d.ts +12 -12
- package/dist/service/chat/review-delivery.js +23 -22
- package/dist/service/chat/review-revision.d.ts +1 -1
- package/dist/service/chat/review-revision.js +13 -13
- package/dist/service/mcp/runtime-policy.js +1 -1
- package/dist/service/mcp/server.js +9 -1
- package/dist/service/mcp/tools.js +1 -1
- package/dist/service/operations.js +11 -5
- package/dist/service/runtime-contract.d.ts +3 -3
- package/dist/service/service.d.ts +6 -0
- package/dist/service/service.js +21 -3
- package/dist/slack-files.d.ts +53 -0
- package/dist/slack-files.js +183 -0
- package/dist/slack.d.ts +3 -0
- package/dist/slack.js +2 -0
- package/dist/telegram-files.d.ts +81 -0
- package/dist/telegram-files.js +244 -0
- package/dist/telegram.d.ts +3 -1
- package/dist/telegram.js +2 -0
- package/dist/whatsapp-files.d.ts +54 -0
- package/dist/whatsapp-files.js +138 -0
- package/dist/whatsapp.d.ts +3 -0
- package/dist/whatsapp.js +2 -0
- package/dist/workspace-channel.d.ts +20 -0
- package/dist/workspace-channel.js +10 -0
- package/package.json +1 -1
- package/dist/agent-review.d.ts +0 -154
package/dist/permissions.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { currentEntityName } from "./earlier-entity-names.js";
|
|
1
2
|
import { GENERATED_UPSERT_PREFIX, toSnakeCase } from "./names.js";
|
|
2
3
|
/**
|
|
3
4
|
* Permissions are `<entity>:<verb>` strings. The verbs are `read`, the generated `create`, `update`,
|
|
@@ -17,16 +18,16 @@ export const READ_VERB = "read";
|
|
|
17
18
|
* App feedback is a member's own message to the builders: a member reads theirs through a scoped
|
|
18
19
|
* `app_feedback:read`, and only the builders' roles name it unscoped. The built-in sign-in
|
|
19
20
|
* identities hold every member's external accounts, such as their Telegram id and username: only a
|
|
20
|
-
* role that names `user_identity` reads them. The agent's
|
|
21
|
-
*
|
|
22
|
-
*
|
|
21
|
+
* role that names `user_identity` reads them. The agent's drafts hold the workspace's chats with
|
|
22
|
+
* the people it serves, and approving one sends it: only a role that names `agent_draft` reads,
|
|
23
|
+
* approves or rejects them. The workspace's deployed code is no entity, but
|
|
23
24
|
* {@link WORKSPACE_SOURCE_PERMISSION} is covered the same way: `workspace_source:read` or
|
|
24
25
|
* `workspace_source:*` grants it, `*:read` and `*:*` do not. So are the agent's own tools
|
|
25
26
|
* ({@link AGENT_TOOL}): `agent_tool:sandbox` or `agent_tool:*` grants the sandbox, `*:*` does not.
|
|
26
27
|
*/
|
|
27
28
|
export const EXPLICIT_GRANT_ENTITIES = new Set([
|
|
28
29
|
"agent_chat",
|
|
29
|
-
"
|
|
30
|
+
"agent_draft",
|
|
30
31
|
"agent_tool",
|
|
31
32
|
"app_feedback",
|
|
32
33
|
"app_feedback_file",
|
|
@@ -166,10 +167,12 @@ export function grants(granted, required) {
|
|
|
166
167
|
if (held === undefined || wanted === undefined) {
|
|
167
168
|
return false;
|
|
168
169
|
}
|
|
169
|
-
|
|
170
|
+
// @legacy ontology.agent-review-entity-name
|
|
171
|
+
// A credential or caller may still name a built-in entity by its earlier name.
|
|
172
|
+
const entity = currentEntityName(toSnakeCase(wanted.entity));
|
|
170
173
|
return ((held.entity === WILDCARD
|
|
171
174
|
? wildcardEntityCovers(entity, toSnakeCase(wanted.verb))
|
|
172
|
-
: toSnakeCase(held.entity) === entity) &&
|
|
175
|
+
: currentEntityName(toSnakeCase(held.entity)) === entity) &&
|
|
173
176
|
(held.verb === WILDCARD || toSnakeCase(held.verb) === toSnakeCase(wanted.verb)));
|
|
174
177
|
}
|
|
175
178
|
/**
|
|
@@ -194,7 +197,7 @@ function isUserAdministration(required) {
|
|
|
194
197
|
!wildcardEntityCovers(USER_ENTITY, toSnakeCase(wanted.verb)));
|
|
195
198
|
}
|
|
196
199
|
/**
|
|
197
|
-
* Feedback, the agent's
|
|
200
|
+
* Feedback, the agent's drafts, the workspace's code and the agent's own tools always need
|
|
198
201
|
* named grants, including for pipeline workloads: nothing a pipeline runs approves a reply to a
|
|
199
202
|
* person, reads the code it runs from or uses the agent's tools. The workspace's own pipeline code reads sign-in identities, for example to address a
|
|
200
203
|
* member in a chat, but nothing it runs writes them: only the platform does.
|
|
@@ -206,12 +209,12 @@ function workloadWildcardCovers(required) {
|
|
|
206
209
|
}
|
|
207
210
|
return (!isUserAdministration(required) &&
|
|
208
211
|
(wanted === undefined ||
|
|
209
|
-
!["app_feedback", "app_feedback_file", "
|
|
212
|
+
!["app_feedback", "app_feedback_file", "agent_draft", WORKSPACE_SOURCE, AGENT_TOOL].includes(currentEntityName(toSnakeCase(wanted.entity)))));
|
|
210
213
|
}
|
|
211
214
|
/**
|
|
212
215
|
* Whether the principal may do what `required` names: one of its permissions equals it, or covers it
|
|
213
|
-
* through a wildcard. Workload wildcards exclude user administration, feedback, the agent's
|
|
214
|
-
*
|
|
216
|
+
* through a wildcard. Workload wildcards exclude user administration, feedback, the agent's
|
|
217
|
+
* drafts, the workspace's code, the agent's own tools and writes of sign-in identities.
|
|
215
218
|
*/
|
|
216
219
|
export function isAllowed(required, principal) {
|
|
217
220
|
return (
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EntityManifest } from "./manifest.js";
|
|
2
2
|
/**
|
|
3
3
|
* The built-in entities whose records replicate to the warehouse as the workspace's own stored
|
|
4
|
-
* entities do: the chats the agent is in, the
|
|
4
|
+
* entities do: the chats the agent is in, the agent's drafts and the feedback members send
|
|
5
5
|
* the builders, which metrics and warehouse models join to the workspace's data. The user and their
|
|
6
6
|
* sign-in identities stay in the ontology: no metric route passes through the user
|
|
7
7
|
* (`metric-routes.ts`), and an identity is how a person signs in, nothing a query counts.
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The built-in entities whose records replicate to the warehouse as the workspace's own stored
|
|
3
|
-
* entities do: the chats the agent is in, the
|
|
3
|
+
* entities do: the chats the agent is in, the agent's drafts and the feedback members send
|
|
4
4
|
* the builders, which metrics and warehouse models join to the workspace's data. The user and their
|
|
5
5
|
* sign-in identities stay in the ontology: no metric route passes through the user
|
|
6
6
|
* (`metric-routes.ts`), and an identity is how a person signs in, nothing a query counts.
|
|
7
7
|
*/
|
|
8
8
|
export const REPLICATED_BUILT_IN_ENTITIES = new Set([
|
|
9
9
|
"agent_chat",
|
|
10
|
+
"agent_draft",
|
|
11
|
+
// @legacy ontology.agent-review-entity-name
|
|
12
|
+
// The drafts' earlier name, in manifests an earlier SDK built, which the platform still reads.
|
|
10
13
|
"agent_review",
|
|
11
14
|
"app_feedback",
|
|
12
15
|
]);
|
|
@@ -22,10 +22,32 @@ export declare const REPLICATION_TABLE_SCHEMA: readonly [...{
|
|
|
22
22
|
* ranking. The struct separates business field names from the private ranking/deletion columns.
|
|
23
23
|
* Events captured before a field was added lack its key and read as its fill.
|
|
24
24
|
*/
|
|
25
|
-
export declare function replicationViews(project: string, target: ReplicationTarget): {
|
|
25
|
+
export declare function replicationViews(project: string, target: ReplicationTarget): ({
|
|
26
26
|
readonly name: string;
|
|
27
27
|
readonly query: string;
|
|
28
|
-
}
|
|
28
|
+
} | {
|
|
29
|
+
readonly name: string;
|
|
30
|
+
readonly query: string;
|
|
31
|
+
})[];
|
|
32
|
+
/**
|
|
33
|
+
* The views, and for each entity the manifest declares `renamedFrom` on, its own and its
|
|
34
|
+
* relationships' views again under the earlier name, so that warehouse queries written for that
|
|
35
|
+
* name keep working while the entity declares it. A view of that name already in the set wins:
|
|
36
|
+
* while an epoch of the earlier manifest still serves, its view is the real one.
|
|
37
|
+
*/
|
|
38
|
+
export declare function withEarlierNameViews<V extends {
|
|
39
|
+
readonly name: string;
|
|
40
|
+
readonly query: string;
|
|
41
|
+
}>(views: ReadonlyArray<V>, entities: ReadonlyArray<{
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly renamedFrom?: string | undefined;
|
|
44
|
+
readonly relationships: ReadonlyArray<{
|
|
45
|
+
readonly name: string;
|
|
46
|
+
}>;
|
|
47
|
+
}>): Array<V | {
|
|
48
|
+
readonly name: string;
|
|
49
|
+
readonly query: string;
|
|
50
|
+
}>;
|
|
29
51
|
/**
|
|
30
52
|
* A single BigQuery statement observes one selector snapshot across every entity/edge join.
|
|
31
53
|
* Each branch has a literal generation predicate for clustered pruning. Unsupported new columns
|
|
@@ -118,7 +118,35 @@ export function replicationViews(project, target) {
|
|
|
118
118
|
if (new Set(result.map((v) => v.name.toLowerCase())).size !== result.length) {
|
|
119
119
|
throw new Error("Replicated entity and relationship view names collide.");
|
|
120
120
|
}
|
|
121
|
-
|
|
121
|
+
// @legacy ontology.replication-earlier-entity-views
|
|
122
|
+
return withEarlierNameViews(result, target.manifest.entities);
|
|
123
|
+
}
|
|
124
|
+
// @legacy ontology.replication-earlier-entity-views
|
|
125
|
+
/**
|
|
126
|
+
* The views, and for each entity the manifest declares `renamedFrom` on, its own and its
|
|
127
|
+
* relationships' views again under the earlier name, so that warehouse queries written for that
|
|
128
|
+
* name keep working while the entity declares it. A view of that name already in the set wins:
|
|
129
|
+
* while an epoch of the earlier manifest still serves, its view is the real one.
|
|
130
|
+
*/
|
|
131
|
+
export function withEarlierNameViews(views, entities) {
|
|
132
|
+
const names = new Set(views.map((view) => view.name.toLowerCase()));
|
|
133
|
+
const aliases = [];
|
|
134
|
+
for (const entity of entities) {
|
|
135
|
+
const earlier = entity.renamedFrom;
|
|
136
|
+
if (earlier === undefined || earlier === entity.name) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
for (const suffix of ["", ...entity.relationships.map(({ name }) => `_${name}`)]) {
|
|
140
|
+
const view = views.find((candidate) => candidate.name === `${entity.name}${suffix}`);
|
|
141
|
+
const alias = `${earlier}${suffix}`;
|
|
142
|
+
if (view === undefined || names.has(alias.toLowerCase())) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
names.add(alias.toLowerCase());
|
|
146
|
+
aliases.push({ name: alias, query: view.query });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return [...views, ...aliases];
|
|
122
150
|
}
|
|
123
151
|
/**
|
|
124
152
|
* A single BigQuery statement observes one selector snapshot across every entity/edge join.
|
|
@@ -130,6 +158,8 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
|
|
|
130
158
|
const scopes = new Map();
|
|
131
159
|
// A placeholder is typed like the column it stands in for; an untyped NULL reads as INT64.
|
|
132
160
|
const types = new Map();
|
|
161
|
+
// The types an earlier name's view takes from a renamed entity's branches, behind its own.
|
|
162
|
+
const aliasTypes = new Map();
|
|
133
163
|
for (const epoch of epochs) {
|
|
134
164
|
for (const route of epoch.routes) {
|
|
135
165
|
const entity = route.target.manifest.entities.find((item) => item.id === route.entityId);
|
|
@@ -145,12 +175,7 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
|
|
|
145
175
|
?.fields.includes(field.name))
|
|
146
176
|
: [];
|
|
147
177
|
const columns = route.kind === "entity" ? fields.map((field) => field.name) : ["source_id", "target_id"];
|
|
148
|
-
|
|
149
|
-
// Use the latest incarnation's type; older property identities remain policy-gated.
|
|
150
|
-
types.set(`${name}.${field.name}`, columnType(field.storage));
|
|
151
|
-
}
|
|
152
|
-
const entries = scopes.get(name) ?? [];
|
|
153
|
-
entries.push({
|
|
178
|
+
const branch = {
|
|
154
179
|
epoch: epoch.id,
|
|
155
180
|
query: view.query,
|
|
156
181
|
columns,
|
|
@@ -158,13 +183,42 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
|
|
|
158
183
|
kind: route.kind,
|
|
159
184
|
generation: route.target.generation,
|
|
160
185
|
target: route.target,
|
|
161
|
-
}
|
|
162
|
-
|
|
186
|
+
};
|
|
187
|
+
// @legacy ontology.replication-earlier-entity-views
|
|
188
|
+
// A renamed entity's branches also serve its view under the earlier name, beside an earlier
|
|
189
|
+
// epoch's own branches of that view, so the view reads across the selector's switch.
|
|
190
|
+
const earlier = entity.renamedFrom !== undefined && entity.renamedFrom !== entity.name
|
|
191
|
+
? `${entity.renamedFrom}${name.slice(entity.name.length)}`
|
|
192
|
+
: undefined;
|
|
193
|
+
for (const [scope, alias] of [
|
|
194
|
+
[name, false],
|
|
195
|
+
...(earlier === undefined ? [] : [[earlier, true]]),
|
|
196
|
+
]) {
|
|
197
|
+
for (const field of fields) {
|
|
198
|
+
// Use the latest incarnation's type; older property identities remain policy-gated.
|
|
199
|
+
(alias ? aliasTypes : types).set(`${scope}.${field.name}`, columnType(field.storage));
|
|
200
|
+
}
|
|
201
|
+
const entries = scopes.get(scope) ?? [];
|
|
202
|
+
entries.push(alias ? { ...branch, alias: true } : branch);
|
|
203
|
+
scopes.set(scope, entries);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// @legacy ontology.replication-earlier-entity-views
|
|
208
|
+
// An earlier name that is another entity's or relationship's view in some epoch stays that view's.
|
|
209
|
+
for (const [name, branches] of scopes) {
|
|
210
|
+
const routes = new Set(branches
|
|
211
|
+
.filter((branch) => branch.alias === true)
|
|
212
|
+
.map((branch) => `${branch.entityId}:${branch.kind}`));
|
|
213
|
+
if (routes.size > 0 &&
|
|
214
|
+
branches.some((branch) => branch.alias !== true && !routes.has(`${branch.entityId}:${branch.kind}`))) {
|
|
215
|
+
scopes.set(name, branches.filter((branch) => branch.alias !== true));
|
|
163
216
|
}
|
|
164
217
|
}
|
|
165
|
-
|
|
218
|
+
const views = [...scopes].map(([name, branches]) => {
|
|
166
219
|
const columns = [...new Set(branches.flatMap((branch) => branch.columns))];
|
|
167
|
-
const
|
|
220
|
+
const renamedScope = branches.some((branch) => branch.alias === true);
|
|
221
|
+
const empty = (column) => `CAST(NULL AS ${types.get(`${name}.${column}`) ?? aliasTypes.get(`${name}.${column}`) ?? "STRING"})`;
|
|
168
222
|
const missing = `NOT EXISTS(SELECT 1 FROM \`${project}.ontology_internal.serving_selector\` selector
|
|
169
223
|
JOIN \`${project}.ontology_internal.epoch_routes\` route ON route.epoch=selector.epoch
|
|
170
224
|
WHERE selector.singleton AND (${branches
|
|
@@ -193,7 +247,12 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
|
|
|
193
247
|
.map((branch) => `SELECT ${columns
|
|
194
248
|
.map((column) => branch.columns.includes(column) && permitted(branch, column)
|
|
195
249
|
? `data.${identifier(column)}`
|
|
196
|
-
:
|
|
250
|
+
: // @legacy ontology.replication-earlier-entity-views
|
|
251
|
+
// A view under an earlier name reads a column one incarnation of the
|
|
252
|
+
// entity lacks, such as a property's earlier name, as null.
|
|
253
|
+
renamedScope && !branch.columns.includes(column)
|
|
254
|
+
? `${empty(column)} AS ${identifier(column)}`
|
|
255
|
+
: `IF(EXISTS(SELECT 1 FROM \`${project}.ontology_internal.serving_selector\`
|
|
197
256
|
WHERE singleton AND epoch=${literal(branch.epoch)}),
|
|
198
257
|
ERROR('ontology_coverage_unavailable'), ${empty(column)}) AS ${identifier(column)}`)
|
|
199
258
|
.join(", ")}
|
|
@@ -208,4 +267,5 @@ export function shadowReplicationViews(project, epochs, policy = epochs.at(-1)?.
|
|
|
208
267
|
WHERE IF(${missing}, ERROR('ontology_unavailable'), FALSE)`,
|
|
209
268
|
};
|
|
210
269
|
});
|
|
270
|
+
return views;
|
|
211
271
|
}
|
package/dist/runtime/agents.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { type AgentResolution, type AgentStore, type ResolveIdentityInput, type
|
|
|
7
7
|
* `now()`; a pairing is consumed with one `UPDATE … WHERE "consumed_at" IS NULL`, so two concurrent
|
|
8
8
|
* approvals cannot both succeed. Ids that are not UUIDs find nothing rather than fail the cast.
|
|
9
9
|
* Chats the agent sees are `agent_chat` records, written as the platform with history. Holding a
|
|
10
|
-
* reply is admitted, bounded and starts the workflows whose triggers on `
|
|
10
|
+
* reply is admitted, bounded and starts the workflows whose triggers on `agent_draft` fire for it as
|
|
11
11
|
* an action is and does, with the `ActionTransactions` and `WorkflowStarter` it runs with.
|
|
12
12
|
*/
|
|
13
13
|
export declare function sqlAgentStore(ontology: Ontology, sql: SqlClient.SqlClient): AgentStore;
|
package/dist/runtime/agents.js
CHANGED
|
@@ -5,7 +5,7 @@ import { SqlClient } from "effect/unstable/sql";
|
|
|
5
5
|
import { action } from "../action.js";
|
|
6
6
|
import { AgentChat, agentChatKey } from "../agent-chat.js";
|
|
7
7
|
import { chatChannelKind } from "../chat-channel.js";
|
|
8
|
-
import {
|
|
8
|
+
import { addressesOfDraft, AgentDraft, participantsOfDraft, draftAddresses, draftParticipants, } from "../agent-draft.js";
|
|
9
9
|
import { AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, CHANNEL_USERS_TABLE, IDENTITY_NOTICES_TABLE, USER_TABLE, } from "../migrate/storage.js";
|
|
10
10
|
import { inboxChannelsOf, inboxRolesOf, takeoverChannelsOf, takeoverProvidersOf, } from "../manifest.js";
|
|
11
11
|
import { GUEST_ROLE } from "../names.js";
|
|
@@ -105,9 +105,9 @@ function identityOf(row) {
|
|
|
105
105
|
};
|
|
106
106
|
}
|
|
107
107
|
/**
|
|
108
|
-
* The action a held reply's record is written by, `
|
|
108
|
+
* The action a held reply's record is written by, `agent_draft.hold`. Only the agent holds replies,
|
|
109
109
|
* through the reviews route, so it is no action of the entity anyone can call: action triggers on
|
|
110
|
-
* `
|
|
110
|
+
* `agent_draft` itself fire for it, with its input and the review as the result.
|
|
111
111
|
*/
|
|
112
112
|
const HOLD_ACTION = "hold";
|
|
113
113
|
/** The most waiting, and the most decided, held replies of one session its next turn learns about. */
|
|
@@ -122,7 +122,7 @@ const HOLD_DEFINITION = action({
|
|
|
122
122
|
* `now()`; a pairing is consumed with one `UPDATE … WHERE "consumed_at" IS NULL`, so two concurrent
|
|
123
123
|
* approvals cannot both succeed. Ids that are not UUIDs find nothing rather than fail the cast.
|
|
124
124
|
* Chats the agent sees are `agent_chat` records, written as the platform with history. Holding a
|
|
125
|
-
* reply is admitted, bounded and starts the workflows whose triggers on `
|
|
125
|
+
* reply is admitted, bounded and starts the workflows whose triggers on `agent_draft` fire for it as
|
|
126
126
|
* an action is and does, with the `ActionTransactions` and `WorkflowStarter` it runs with.
|
|
127
127
|
*/
|
|
128
128
|
export function sqlAgentStore(ontology, sql) {
|
|
@@ -130,13 +130,13 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
130
130
|
const memberPrincipal = (userId) => resolveMemberById(ontology, userId).pipe(Effect.provideService(SqlClient.SqlClient, sql));
|
|
131
131
|
/**
|
|
132
132
|
* Holds a reply for review once per key, in a transaction bounded and admitted as an action's,
|
|
133
|
-
* and starts the workflows whose triggers on `
|
|
133
|
+
* and starts the workflows whose triggers on `agent_draft` fire for it. `resolve` runs in the
|
|
134
134
|
* transaction, after the key is locked and found unused: it names the reply, or the reply a redraft
|
|
135
135
|
* replaces, which it marks superseded, or `discarded` when there is nothing to replace.
|
|
136
136
|
*/
|
|
137
137
|
const hold = (key, memberId, resolve) => Effect.gen(function* () {
|
|
138
|
-
// The triggers on `
|
|
139
|
-
const triggers = actionTriggers(ontology).filter((trigger) => trigger.entity ===
|
|
138
|
+
// The triggers on `agent_draft` itself, which fire for any of its actions: holding is one.
|
|
139
|
+
const triggers = actionTriggers(ontology).filter((trigger) => trigger.entity === AgentDraft.name &&
|
|
140
140
|
(trigger.action === undefined || trigger.action === HOLD_ACTION));
|
|
141
141
|
const starter = triggers.length > 0
|
|
142
142
|
? Option.getOrUndefined(yield* Effect.serviceOption(WorkflowStarter))
|
|
@@ -144,7 +144,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
144
144
|
// Admitted and bounded as an action's transaction is, by the same gate and limits.
|
|
145
145
|
const admission = Option.getOrUndefined(yield* Effect.serviceOption(ActionTransactions));
|
|
146
146
|
const limits = admission?.limits ?? ACTION_TRANSACTION_LIMITS;
|
|
147
|
-
const qualified = `${
|
|
147
|
+
const qualified = `${AgentDraft.name}.${HOLD_ACTION}`;
|
|
148
148
|
const actionId = randomUUID();
|
|
149
149
|
if (memberId !== undefined && !isRecordId(memberId)) {
|
|
150
150
|
return undefined;
|
|
@@ -169,7 +169,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
169
169
|
? {
|
|
170
170
|
onEdit: (edit) => edits.push(edit),
|
|
171
171
|
onSnapshot: (snapshot) => {
|
|
172
|
-
if (snapshot.entity ===
|
|
172
|
+
if (snapshot.entity === AgentDraft.name) {
|
|
173
173
|
snapshots.set(snapshot.id, {
|
|
174
174
|
before: snapshots.get(snapshot.id)?.before ?? snapshot.before,
|
|
175
175
|
after: snapshot.after,
|
|
@@ -180,7 +180,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
180
180
|
: {}),
|
|
181
181
|
});
|
|
182
182
|
const [found] = yield* runtime.find({
|
|
183
|
-
entity:
|
|
183
|
+
entity: AgentDraft.name,
|
|
184
184
|
options: { where: { key }, limit: 1 },
|
|
185
185
|
});
|
|
186
186
|
if (found !== undefined) {
|
|
@@ -214,24 +214,24 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
214
214
|
},
|
|
215
215
|
});
|
|
216
216
|
const review = yield* runtime.create({
|
|
217
|
-
entity:
|
|
217
|
+
entity: AgentDraft.name,
|
|
218
218
|
values: {
|
|
219
219
|
key: input.key,
|
|
220
|
-
|
|
220
|
+
channel_kind: input.provider,
|
|
221
221
|
channel: input.channel,
|
|
222
222
|
chat_id: input.chatId,
|
|
223
223
|
recipient: input.recipient,
|
|
224
224
|
// A reply to mail names whom it goes to: its person, unless the agent said more.
|
|
225
225
|
...(input.provider === "email"
|
|
226
226
|
? {
|
|
227
|
-
to:
|
|
228
|
-
cc: (input.cc ?? []).length === 0 ? null :
|
|
227
|
+
to: draftAddresses(input.to ?? [input.recipient]),
|
|
228
|
+
cc: (input.cc ?? []).length === 0 ? null : draftAddresses(input.cc ?? []),
|
|
229
229
|
}
|
|
230
230
|
: {}),
|
|
231
231
|
// A reply in a chat several people share is read by the members the agent named.
|
|
232
232
|
participants: (input.participants ?? []).length === 0
|
|
233
233
|
? null
|
|
234
|
-
:
|
|
234
|
+
: draftParticipants(input.participants ?? []),
|
|
235
235
|
chat_kind: chat?.kind === "group" || chat?.kind === "private" ? chat.kind : kindOf(input),
|
|
236
236
|
thread_id: input.threadId === "" ? null : input.threadId,
|
|
237
237
|
reply_to: input.replyTo,
|
|
@@ -251,7 +251,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
251
251
|
});
|
|
252
252
|
if (resolved.kind === "revision") {
|
|
253
253
|
yield* plain.update({
|
|
254
|
-
entity:
|
|
254
|
+
entity: AgentDraft.name,
|
|
255
255
|
id: String(resolved.previous.id),
|
|
256
256
|
values: {
|
|
257
257
|
status: "superseded",
|
|
@@ -261,7 +261,12 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
261
261
|
},
|
|
262
262
|
});
|
|
263
263
|
}
|
|
264
|
-
const result = {
|
|
264
|
+
const result = {
|
|
265
|
+
draft: String(review.id),
|
|
266
|
+
// @legacy ontology.agent-review-entity-name
|
|
267
|
+
review: String(review.id),
|
|
268
|
+
status: "pending",
|
|
269
|
+
};
|
|
265
270
|
let starts = [];
|
|
266
271
|
if (triggers.length > 0) {
|
|
267
272
|
const principal = yield* memberPrincipal(input.memberId);
|
|
@@ -269,7 +274,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
269
274
|
if (principal !== undefined) {
|
|
270
275
|
starts = yield* evaluateTriggers(triggers, {
|
|
271
276
|
ontology,
|
|
272
|
-
entity:
|
|
277
|
+
entity: AgentDraft.name,
|
|
273
278
|
action: HOLD_ACTION,
|
|
274
279
|
definition: HOLD_DEFINITION,
|
|
275
280
|
generated: false,
|
|
@@ -280,12 +285,12 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
280
285
|
result,
|
|
281
286
|
edits,
|
|
282
287
|
held: snapshots,
|
|
283
|
-
load: (id) => runtime.get({ entity:
|
|
288
|
+
load: (id) => runtime.get({ entity: AgentDraft.name, id }).pipe(Effect.map((record) => record), Effect.catchTag("NotFound", () => Effect.succeed(undefined))),
|
|
284
289
|
});
|
|
285
290
|
yield* insertStarts(sql, starts);
|
|
286
291
|
}
|
|
287
292
|
}
|
|
288
|
-
return { review: { id: result.
|
|
293
|
+
return { review: { id: result.draft, status: "pending", created: true }, starts };
|
|
289
294
|
}), {
|
|
290
295
|
qualified,
|
|
291
296
|
actionId,
|
|
@@ -897,7 +902,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
897
902
|
// The reply the redraft replaces, while it still waits for this redraft: a member who
|
|
898
903
|
// decided it, or asked for another redraft since, leaves this one nothing to replace.
|
|
899
904
|
const [previous] = yield* runtime.find({
|
|
900
|
-
entity:
|
|
905
|
+
entity: AgentDraft.name,
|
|
901
906
|
options: { where: { id: input.review }, limit: 1, forUpdate: true },
|
|
902
907
|
});
|
|
903
908
|
if (previous === undefined ||
|
|
@@ -911,18 +916,18 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
911
916
|
previous,
|
|
912
917
|
reply: {
|
|
913
918
|
key: `revision:${input.revision}`,
|
|
914
|
-
provider: String(previous.
|
|
915
|
-
channel: nullableText(previous.channel) ?? String(previous.
|
|
919
|
+
provider: String(previous.channel_kind),
|
|
920
|
+
channel: nullableText(previous.channel) ?? String(previous.channel_kind),
|
|
916
921
|
chatId: String(previous.chat_id),
|
|
917
922
|
recipient: String(previous.recipient),
|
|
918
923
|
// A redraft goes to the people the reply it replaces goes to, and is read by the
|
|
919
924
|
// members who read that one.
|
|
920
|
-
...(
|
|
925
|
+
...(addressesOfDraft(previous.to).length === 0
|
|
921
926
|
? {}
|
|
922
|
-
: { to:
|
|
923
|
-
...(
|
|
927
|
+
: { to: addressesOfDraft(previous.to), cc: addressesOfDraft(previous.cc) }),
|
|
928
|
+
...(participantsOfDraft(previous.participants).length === 0
|
|
924
929
|
? {}
|
|
925
|
-
: { participants:
|
|
930
|
+
: { participants: participantsOfDraft(previous.participants) }),
|
|
926
931
|
threadId: nullableText(previous.thread_id) ?? "",
|
|
927
932
|
replyTo: nullableText(previous.reply_to),
|
|
928
933
|
memberId: String(previous.person_id),
|
|
@@ -940,19 +945,19 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
940
945
|
}
|
|
941
946
|
yield* beginReplicationWrite(sql);
|
|
942
947
|
const runtime = makeOntologyRuntime(ontology, sql, {
|
|
943
|
-
action: `${
|
|
948
|
+
action: `${AgentDraft.name}.revise`,
|
|
944
949
|
actionId: randomUUID(),
|
|
945
950
|
principalId: input.notedBy,
|
|
946
951
|
});
|
|
947
952
|
const [review] = yield* runtime.find({
|
|
948
|
-
entity:
|
|
953
|
+
entity: AgentDraft.name,
|
|
949
954
|
options: { where: { id: input.review }, limit: 1, forUpdate: true },
|
|
950
955
|
});
|
|
951
956
|
if (review === undefined || review.status !== "pending") {
|
|
952
957
|
return false;
|
|
953
958
|
}
|
|
954
959
|
yield* runtime.update({
|
|
955
|
-
entity:
|
|
960
|
+
entity: AgentDraft.name,
|
|
956
961
|
id: String(review.id),
|
|
957
962
|
values: {
|
|
958
963
|
note: input.note,
|
|
@@ -971,12 +976,12 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
971
976
|
}
|
|
972
977
|
yield* beginReplicationWrite(sql);
|
|
973
978
|
const runtime = makeOntologyRuntime(ontology, sql, {
|
|
974
|
-
action: `${
|
|
979
|
+
action: `${AgentDraft.name}.approve`,
|
|
975
980
|
actionId: randomUUID(),
|
|
976
981
|
principalId: null,
|
|
977
982
|
});
|
|
978
983
|
const [review] = yield* runtime.find({
|
|
979
|
-
entity:
|
|
984
|
+
entity: AgentDraft.name,
|
|
980
985
|
options: { where: { id: input.review }, limit: 1, forUpdate: true },
|
|
981
986
|
});
|
|
982
987
|
if (review === undefined || review.status !== "pending") {
|
|
@@ -984,7 +989,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
984
989
|
}
|
|
985
990
|
if (review.revision !== null && review.revision !== undefined) {
|
|
986
991
|
yield* runtime.update({
|
|
987
|
-
entity:
|
|
992
|
+
entity: AgentDraft.name,
|
|
988
993
|
id: String(review.id),
|
|
989
994
|
values: { revision: null, revision_state: null },
|
|
990
995
|
});
|
|
@@ -999,12 +1004,12 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
999
1004
|
}
|
|
1000
1005
|
yield* beginReplicationWrite(sql);
|
|
1001
1006
|
const runtime = makeOntologyRuntime(ontology, sql, {
|
|
1002
|
-
action: `${
|
|
1007
|
+
action: `${AgentDraft.name}.${HOLD_ACTION}`,
|
|
1003
1008
|
actionId: randomUUID(),
|
|
1004
1009
|
principalId: null,
|
|
1005
1010
|
});
|
|
1006
1011
|
const [previous] = yield* runtime.find({
|
|
1007
|
-
entity:
|
|
1012
|
+
entity: AgentDraft.name,
|
|
1008
1013
|
options: { where: { id: input.review }, limit: 1, forUpdate: true },
|
|
1009
1014
|
});
|
|
1010
1015
|
if (previous === undefined ||
|
|
@@ -1014,7 +1019,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
1014
1019
|
return false;
|
|
1015
1020
|
}
|
|
1016
1021
|
yield* runtime.update({
|
|
1017
|
-
entity:
|
|
1022
|
+
entity: AgentDraft.name,
|
|
1018
1023
|
id: String(previous.id),
|
|
1019
1024
|
values: { revision_state: "failed" },
|
|
1020
1025
|
});
|
|
@@ -1027,7 +1032,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
1027
1032
|
// reply reaches the session as the newest one.
|
|
1028
1033
|
const [waiting, decided] = yield* Effect.all([
|
|
1029
1034
|
runtime.find({
|
|
1030
|
-
entity:
|
|
1035
|
+
entity: AgentDraft.name,
|
|
1031
1036
|
options: {
|
|
1032
1037
|
where: { session_id: sessionId, status: "pending" },
|
|
1033
1038
|
orderBy: [{ field: "created_at", direction: "desc" }],
|
|
@@ -1035,7 +1040,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
1035
1040
|
},
|
|
1036
1041
|
}),
|
|
1037
1042
|
runtime.find({
|
|
1038
|
-
entity:
|
|
1043
|
+
entity: AgentDraft.name,
|
|
1039
1044
|
options: {
|
|
1040
1045
|
where: { session_id: sessionId, status: { ne: "pending" } },
|
|
1041
1046
|
orderBy: [{ field: "decided_at", direction: "desc" }],
|
|
@@ -242,12 +242,13 @@ export interface ChatMessageSearch {
|
|
|
242
242
|
}
|
|
243
243
|
/**
|
|
244
244
|
* Who reads, as the chat archive's reader rule takes it: every group message (`groups`, from
|
|
245
|
-
* `agent_chat:read` or membership of the chat), this exact chat's messages
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
245
|
+
* `agent_chat:read` or membership of the chat), this exact chat's messages (`chat`, a chat the
|
|
246
|
+
* ontology checked the reader may read), and the private messages whose `participants` hold
|
|
247
|
+
* `participantId`, row by row. A search of several chats names, by `<channel>:<chat id>`, the
|
|
248
|
+
* groups the reader is in (`groupChats`), read as `groups` reads every group, and the chats the
|
|
249
|
+
* ontology checked (`chats`), each read as `chat` reads the one searched. Every rule reads only
|
|
250
|
+
* what reached the provider: never an internal row, a send still unsent, or one that failed
|
|
251
|
+
* before the provider took it.
|
|
251
252
|
*/
|
|
252
253
|
export interface ChatMessageReader {
|
|
253
254
|
readonly participantId: string | null;
|
|
@@ -299,7 +300,7 @@ export interface ChatMessageStore {
|
|
|
299
300
|
}) => Effect.Effect<boolean>;
|
|
300
301
|
/** The send an `operation` keys, its tombstone included; `undefined` when the key has none. */
|
|
301
302
|
readonly findSend: (operation: string) => Effect.Effect<ChatSend | undefined>;
|
|
302
|
-
/**
|
|
303
|
+
/** Messages as a reader may read them, row by row: only what reached the provider. */
|
|
303
304
|
readonly search: (search: ChatMessageSearch, reader: ChatMessageReader) => Effect.Effect<ChatMessagePage>;
|
|
304
305
|
/**
|
|
305
306
|
* Deletes the messages older than `retentionDays`, in bounded batches, and scrubs a send's row to
|
|
@@ -583,12 +583,6 @@ export function sqlChatMessageStore(sql) {
|
|
|
583
583
|
if (input.q !== undefined && input.q.trim() !== "") {
|
|
584
584
|
conditions.push(`to_tsvector('simple', coalesce("text", '')) @@ plainto_tsquery('simple', ${params.add(input.q, "text")})`);
|
|
585
585
|
}
|
|
586
|
-
// An exact chat reads what reached the provider: never a send still unsent, nor one that
|
|
587
|
-
// failed before the provider took it. One it took and later failed stays, `failed`, so a
|
|
588
|
-
// reader polling after a revision sees the same chat as one reading it whole.
|
|
589
|
-
const shown = `("direction" = 'inbound' OR ("direction" = 'outbound' AND ` +
|
|
590
|
-
`("status" IS NULL OR "status" IN ('sent', 'delivered', 'read') OR ` +
|
|
591
|
-
`("status" = 'failed' AND "message_id" IS NOT NULL))))`;
|
|
592
586
|
// A row's chat as the reader's lists name it.
|
|
593
587
|
const chatKey = `("channel" || ':' || "chat_id")`;
|
|
594
588
|
const groupChats = reader.groupChats ?? [];
|
|
@@ -599,10 +593,9 @@ export function sqlChatMessageStore(sql) {
|
|
|
599
593
|
...(groupChats.length === 0
|
|
600
594
|
? []
|
|
601
595
|
: [`("chat_type" <> 'private' AND ${chatKey} = ANY(${params.add(groupChats, "text[]")}))`]),
|
|
602
|
-
|
|
603
|
-
...(
|
|
604
|
-
|
|
605
|
-
: [`(${chatKey} = ANY(${params.add(chats, "text[]")}) AND ${shown})`]),
|
|
596
|
+
// The chat searched, which the ontology checked: every message of it.
|
|
597
|
+
...(reader.chat ? ["TRUE"] : []),
|
|
598
|
+
...(chats.length === 0 ? [] : [`${chatKey} = ANY(${params.add(chats, "text[]")})`]),
|
|
606
599
|
...(reader.participantId === null
|
|
607
600
|
? []
|
|
608
601
|
: [
|
|
@@ -614,6 +607,14 @@ export function sqlChatMessageStore(sql) {
|
|
|
614
607
|
return Effect.succeed({ messages: [], nextCursor: null });
|
|
615
608
|
}
|
|
616
609
|
conditions.push(`(${rules.join(" OR ")})`);
|
|
610
|
+
// Every rule reads what reached the provider: never an internal row, a send still unsent, nor
|
|
611
|
+
// one that failed before the provider took it; a send becomes readable under the revision its
|
|
612
|
+
// receipt takes. One the provider took and later failed stays, `failed`, and an echo a send
|
|
613
|
+
// took over stays a change, so a reader polling after a revision sees the same chat as one
|
|
614
|
+
// reading it whole.
|
|
615
|
+
conditions.push(`("direction" = 'inbound' OR "replaced_by" IS NOT NULL OR ("direction" = 'outbound' AND ` +
|
|
616
|
+
`("status" IS NULL OR "status" IN ('sent', 'delivered', 'read') OR ` +
|
|
617
|
+
`("status" = 'failed' AND "message_id" IS NOT NULL))))`);
|
|
617
618
|
return page(conditions, params, input);
|
|
618
619
|
};
|
|
619
620
|
const holds = (chat) => rows(chat === undefined
|
|
@@ -258,7 +258,7 @@ export function backendLayer(ontology, blobStorage, workflowStarter, deployment,
|
|
|
258
258
|
resolveIdentity: (input) => agents
|
|
259
259
|
.resolveIdentity(input)
|
|
260
260
|
.pipe(withStarter, Effect.provideService(ActionTransactions, actionTransactions)),
|
|
261
|
-
// Holding a reply starts the workflows triggered on `
|
|
261
|
+
// Holding a reply starts the workflows triggered on `agent_draft`, as an action would.
|
|
262
262
|
...(agents.holdReply === undefined
|
|
263
263
|
? {}
|
|
264
264
|
: {
|