@segmentstream/ontology 0.122.0 → 0.124.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 +25 -14
- package/dist/action.d.ts +11 -6
- package/dist/agent-chat.d.ts +30 -15
- package/dist/agent-chat.js +66 -2
- package/dist/analyze.d.ts +4 -0
- package/dist/analyze.js +63 -13
- package/dist/app-feedback.js +6 -5
- package/dist/chat.d.ts +10 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/manifest.d.ts +60 -7
- package/dist/manifest.js +37 -6
- package/dist/migrate/apply.js +5 -17
- package/dist/migrate/model.d.ts +24 -0
- package/dist/replication/baseline.d.ts +1 -2
- package/dist/replication/baseline.js +3 -2
- package/dist/replication/legacy-baseline.d.ts +1 -2
- package/dist/replication/legacy-baseline.js +3 -6
- package/dist/replication/migrate.d.ts +1 -10
- package/dist/replication/migrate.js +0 -37
- package/dist/replication/model.d.ts +6 -9
- package/dist/replication/model.js +13 -47
- package/dist/replication/schema.js +3 -0
- package/dist/replication/shadow.d.ts +7 -3
- package/dist/replication/shadow.js +9 -9
- package/dist/replication/store.d.ts +6 -2
- package/dist/replication/store.js +21 -46
- package/dist/runtime/agents.js +16 -6
- package/dist/runtime/generated-actions.js +5 -0
- package/dist/runtime/service-binding.d.ts +5 -1
- package/dist/runtime/service-binding.js +14 -5
- package/dist/service/agent/server.js +1 -0
- package/dist/service/chat/delivery.js +3 -0
- package/dist/service/mcp/server.js +5 -5
- package/dist/service/runtime-contract.d.ts +11 -1
- package/dist/service/schemas.d.ts +6 -0
- package/dist/storage/memory.d.ts +100 -0
- package/dist/storage/memory.js +281 -0
- package/dist/storage/sessions.d.ts +2 -2
- package/package.json +1 -1
|
@@ -511,10 +511,6 @@ export const refreshShadowCapturePolicy = (sql) => sql.unsafe(`UPDATE _ontology_
|
|
|
511
511
|
FROM _ontology_replication_targets t,jsonb_each(t.capture_bindings) binding,
|
|
512
512
|
jsonb_each_text(binding.value) field WHERE t.capturing
|
|
513
513
|
AND t.source_id=(SELECT source_id FROM _ontology_replication WHERE id)
|
|
514
|
-
AND NOT EXISTS (
|
|
515
|
-
SELECT 1 FROM _ontology_replication state,jsonb_array_elements(state.exclusions) exclusion
|
|
516
|
-
WHERE exclusion->>'entityId'=binding.key AND
|
|
517
|
-
(COALESCE((exclusion->>'disabled')::boolean,false) OR exclusion->'fields' ? field.value))
|
|
518
514
|
GROUP BY binding.key
|
|
519
515
|
) union_fields
|
|
520
516
|
) selections),'{}'::jsonb) WHERE id`);
|
|
@@ -581,15 +577,19 @@ export const settleRetiredSegment = (sql, segment, committed) => sql.unsafe(`UPD
|
|
|
581
577
|
committed_at=CASE WHEN $3='committed' THEN now() ELSE committed_at END
|
|
582
578
|
WHERE id=$1::uuid AND job_id=$2 AND state='pending' AND epoch IN (
|
|
583
579
|
SELECT id FROM _ontology_replication_epochs WHERE state IN ('retired','cancelled'))`, [segment.id, segment.job_id, committed ? "committed" : "failed"]);
|
|
584
|
-
/**
|
|
585
|
-
|
|
580
|
+
/**
|
|
581
|
+
* Policy-only refresh is independent of build leases and cannot confer publication eligibility.
|
|
582
|
+
* Every change of the active target's projection selects a new active generation, which fences
|
|
583
|
+
* a delayed install of an earlier projection.
|
|
584
|
+
*/
|
|
585
|
+
export const shadowPolicyViewOwnership = (sql, generation, source) => sql
|
|
586
586
|
.unsafe(`SELECT EXISTS(SELECT 1 FROM _ontology_replication
|
|
587
587
|
WHERE id AND enabled AND NOT destination_repair_pending AND active_generation=$1::uuid
|
|
588
|
-
AND source_id=$2::uuid
|
|
588
|
+
AND source_id=$2::uuid) AS valid`, [generation, source])
|
|
589
589
|
.pipe(Effect.map((rows) => rows[0].valid));
|
|
590
|
-
export const recordShadowPolicyViews = (sql, generation, source
|
|
590
|
+
export const recordShadowPolicyViews = (sql, generation, source) => sql.unsafe(`UPDATE _ontology_replication_epochs SET policy_installed=true WHERE id=$1::uuid
|
|
591
591
|
AND EXISTS(SELECT 1 FROM _ontology_replication WHERE id AND enabled AND NOT destination_repair_pending
|
|
592
|
-
AND active_generation=$1::uuid AND source_id=$2::uuid
|
|
592
|
+
AND active_generation=$1::uuid AND source_id=$2::uuid)`, [generation, source]);
|
|
593
593
|
/** Used after reading a destination view's etag, before its conditional DDL update. */
|
|
594
594
|
export const shadowViewOwnership = (sql, id) => sql
|
|
595
595
|
.unsafe(`SELECT EXISTS(SELECT 1 FROM _ontology_replication_epochs e
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import type { SqlClient } from "effect/unstable/sql";
|
|
3
|
-
import { type ReplicationEvent, type ReplicationSelection
|
|
3
|
+
import { type ReplicationEvent, type ReplicationSelection } from "./model.js";
|
|
4
4
|
import { advanceReplicationBaseline, ReplicationBaselineFailed, type ReplicationTarget } from "./baseline.js";
|
|
5
5
|
export type { ReplicationTarget } from "./baseline.js";
|
|
6
6
|
export declare const startReplication: (sql: SqlClient.SqlClient, input: {
|
|
7
7
|
readonly generation: string;
|
|
8
|
-
readonly exclusions?: ReadonlyArray<ReplicationExclusion>;
|
|
9
8
|
readonly selection?: ReadonlyArray<ReplicationSelection>;
|
|
10
9
|
readonly newSource?: boolean;
|
|
11
10
|
}) => Effect.Effect<ReplicationTarget, import("effect/unstable/sql/SqlError").SqlError | ReplicationBaselineFailed | import("../index.js").CatalogInvalid | import("./legacy-baseline.js").ReplicationBaselineFailed, never>;
|
|
12
11
|
export declare const activeReplicationTarget: (sql: SqlClient.SqlClient) => Effect.Effect<ReplicationTarget | undefined, import("effect/unstable/sql/SqlError").SqlError, never>;
|
|
12
|
+
/**
|
|
13
|
+
* Disables source capture under the lifecycle fence: the state a preview branch's binding, or an
|
|
14
|
+
* earlier release's retired pause, leaves behind. Starting replication resumes such a source with a
|
|
15
|
+
* fresh incarnation; nothing in the platform offers capture as an opt-out.
|
|
16
|
+
*/
|
|
13
17
|
export declare const pauseReplication: (sql: SqlClient.SqlClient) => Effect.Effect<void, import("effect/unstable/sql/SqlError").SqlError, never>;
|
|
14
18
|
export declare const replicationTarget: (sql: SqlClient.SqlClient, generation: string) => Effect.Effect<ReplicationTarget | undefined, import("effect/unstable/sql/SqlError").SqlError, never>;
|
|
15
19
|
export { advanceReplicationBaseline };
|
|
@@ -9,18 +9,10 @@ import { readCatalog } from "../migrate/apply.js";
|
|
|
9
9
|
import { historyTableName, quoteIdentifier as qi } from "../migrate/storage.js";
|
|
10
10
|
import { REPLICATION_BATCH_BYTES, REPLICATION_BATCH_SIZE, publishMessage, replicationEventId, resolveReplicationSelection, } from "./model.js";
|
|
11
11
|
import { createReplicationBaseline, advanceReplicationBaseline, ReplicationBaselineFailed, } from "./baseline.js";
|
|
12
|
-
import {
|
|
12
|
+
import { migratedReplicationFills } from "./migrate.js";
|
|
13
13
|
import { refreshReplicationProjection } from "./evolution.js";
|
|
14
14
|
import { REPLICATION_TARGET_FILLS, REPLICATION_TARGET_METADATA } from "./schema.js";
|
|
15
15
|
import { TARGET_MANIFEST_HASH, storedTargets } from "./stored-manifests.js";
|
|
16
|
-
const replicationPolicyKey = (policy) => JSON.stringify(policy
|
|
17
|
-
.map((item) => ({
|
|
18
|
-
entityId: item.entityId,
|
|
19
|
-
disabled: item.disabled ?? false,
|
|
20
|
-
fields: [...new Set(item.fields ?? [])].sort(),
|
|
21
|
-
relationships: [...new Set(item.relationships ?? [])].sort(),
|
|
22
|
-
}))
|
|
23
|
-
.sort((a, b) => a.entityId.localeCompare(b.entityId)));
|
|
24
16
|
export const startReplication = (sql, input) => Effect.gen(function* () {
|
|
25
17
|
const manifest = yield* readCatalog({ sql });
|
|
26
18
|
if (!manifest) {
|
|
@@ -29,27 +21,31 @@ export const startReplication = (sql, input) => Effect.gen(function* () {
|
|
|
29
21
|
});
|
|
30
22
|
}
|
|
31
23
|
const version = yield* protocol(sql);
|
|
32
|
-
|
|
24
|
+
// @legacy ontology.replication-exclusion-remnants
|
|
25
|
+
// A source can still record the exclusions of retired per-workspace export policies. Clearing
|
|
26
|
+
// them is a policy change through the same scoped recovery and cutover as any other.
|
|
27
|
+
const [state] = yield* sql.unsafe(`SELECT enabled,
|
|
28
|
+
COALESCE(to_jsonb(_ontology_replication)->'exclusions','[]'::jsonb)<>'[]'::jsonb AS restricted
|
|
29
|
+
FROM _ontology_replication WHERE id`);
|
|
33
30
|
const requested = yield* replicationTarget(sql, input.generation);
|
|
34
31
|
const previous = version >= 6 && requested ? ((yield* activeReplicationTarget(sql)) ?? requested) : requested;
|
|
35
|
-
const
|
|
36
|
-
const persisted = replicationPolicyKey(policy) === replicationPolicyKey(state?.exclusions ?? []);
|
|
32
|
+
const restricted = state?.restricted === true;
|
|
37
33
|
const paused = version >= 6 && state?.enabled === false;
|
|
38
34
|
if (previous &&
|
|
39
35
|
!paused &&
|
|
40
|
-
|
|
36
|
+
!restricted &&
|
|
41
37
|
previous.manifest.hash === manifest.hash &&
|
|
42
38
|
input.selection === undefined) {
|
|
43
39
|
return previous;
|
|
44
40
|
}
|
|
45
41
|
const selection = input.selection ??
|
|
46
42
|
(yield* Effect.try({
|
|
47
|
-
try: () => resolveReplicationSelection(manifest
|
|
48
|
-
catch: (cause) => new ReplicationBaselineFailed({ message: "Invalid replication
|
|
43
|
+
try: () => resolveReplicationSelection(manifest),
|
|
44
|
+
catch: (cause) => new ReplicationBaselineFailed({ message: "Invalid replication projection.", cause }),
|
|
49
45
|
}));
|
|
50
46
|
if (previous &&
|
|
51
47
|
(paused ||
|
|
52
|
-
|
|
48
|
+
restricted ||
|
|
53
49
|
previous.manifest.hash !== manifest.hash ||
|
|
54
50
|
canonicalJson(previous.selection) !== canonicalJson(selection))) {
|
|
55
51
|
if (version >= 5) {
|
|
@@ -67,10 +63,9 @@ export const startReplication = (sql, input) => Effect.gen(function* () {
|
|
|
67
63
|
}
|
|
68
64
|
const fills = yield* migratedReplicationFills(sql, current, manifest, selection);
|
|
69
65
|
yield* refreshReplicationProjection(sql, current, { ...current, manifest, selection, fills }, undefined, { resuming: version >= 6 && capture?.enabled === false });
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
]);
|
|
66
|
+
// @legacy ontology.replication-exclusion-remnants
|
|
67
|
+
// An older backend release still reads the retired exclusions; record that none remain.
|
|
68
|
+
yield* sql.unsafe("UPDATE _ontology_replication SET selection=$1::jsonb,exclusions='[]'::jsonb,enabled=true WHERE id", [JSON.stringify(Object.fromEntries(selection.map((item) => [item.entityId, item])))]);
|
|
74
69
|
if (version >= 6) {
|
|
75
70
|
yield* sql.unsafe(`UPDATE _ontology_replication_epochs SET policy_installed=false
|
|
76
71
|
WHERE id=(SELECT active_generation FROM _ontology_replication WHERE id)`);
|
|
@@ -121,6 +116,11 @@ export const activeReplicationTarget = (sql) => Effect.gen(function* () {
|
|
|
121
116
|
: yield* sql.unsafe(statement);
|
|
122
117
|
return rows[0];
|
|
123
118
|
});
|
|
119
|
+
/**
|
|
120
|
+
* Disables source capture under the lifecycle fence: the state a preview branch's binding, or an
|
|
121
|
+
* earlier release's retired pause, leaves behind. Starting replication resumes such a source with a
|
|
122
|
+
* fresh incarnation; nothing in the platform offers capture as an opt-out.
|
|
123
|
+
*/
|
|
124
124
|
export const pauseReplication = (sql) => sql.withTransaction(Effect.gen(function* () {
|
|
125
125
|
yield* lockReplicationLifecycle(sql);
|
|
126
126
|
yield* sql.unsafe("UPDATE _ontology_replication SET enabled = false WHERE id");
|
|
@@ -166,7 +166,7 @@ export const pendingReplicationEvents = (sql, target, options = {}) => Effect.ge
|
|
|
166
166
|
ORDER BY revision,event_id LIMIT ${REPLICATION_BATCH_SIZE}`, shadow
|
|
167
167
|
? [target.generation, options.bulk ?? false, options.through ?? null]
|
|
168
168
|
: [target.generation]);
|
|
169
|
-
// An idle pass ends here: the bindings
|
|
169
|
+
// An idle pass ends here: the wire bindings below shape only queued events.
|
|
170
170
|
if (references.length === 0) {
|
|
171
171
|
return [];
|
|
172
172
|
}
|
|
@@ -196,10 +196,6 @@ export const pendingReplicationEvents = (sql, target, options = {}) => Effect.ge
|
|
|
196
196
|
? yield* sql.unsafe("SELECT capture_bindings,capture_versions FROM _ontology_replication_targets WHERE generation=$1::uuid", [target.generation])
|
|
197
197
|
: [];
|
|
198
198
|
const batchBytes = options.bulk ? 32_000_000 : REPLICATION_BATCH_BYTES;
|
|
199
|
-
const [policy] = shadow
|
|
200
|
-
? yield* sql.unsafe("SELECT exclusions FROM _ontology_replication WHERE id")
|
|
201
|
-
: [];
|
|
202
|
-
const suppressed = [];
|
|
203
199
|
let size = 32;
|
|
204
200
|
const result = [];
|
|
205
201
|
for (const reference of references) {
|
|
@@ -216,23 +212,6 @@ export const pendingReplicationEvents = (sql, target, options = {}) => Effect.ge
|
|
|
216
212
|
const bindings = wire.capture_versions?.find((version) => version.through === undefined || BigInt(event.revision) <= BigInt(version.through))?.bindings ?? wire.capture_bindings;
|
|
217
213
|
event = shadowWireEvent(event, bindings, target.selection);
|
|
218
214
|
}
|
|
219
|
-
const restriction = policy?.exclusions.find((item) => item.entityId === event.entity_id);
|
|
220
|
-
if (restriction?.disabled ||
|
|
221
|
-
(event.record_kind.startsWith("edge:") &&
|
|
222
|
-
restriction?.relationships?.includes(event.record_kind.slice(5)))) {
|
|
223
|
-
suppressed.push(reference.id);
|
|
224
|
-
continue;
|
|
225
|
-
}
|
|
226
|
-
if (restriction && event.record_kind === "entity" && !event.deleted) {
|
|
227
|
-
const fields = new Set([
|
|
228
|
-
...(restriction.fields ?? []),
|
|
229
|
-
...(restriction.relationships ?? []).map((name) => `${name}_id`),
|
|
230
|
-
]);
|
|
231
|
-
event = {
|
|
232
|
-
...event,
|
|
233
|
-
payload: JSON.stringify(Object.fromEntries(Object.entries(JSON.parse(event.payload)).filter(([key]) => !fields.has(wire?.capture_bindings?.[event.entity_id]?.[key] ?? key)))),
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
215
|
const bytes = Buffer.byteLength(JSON.stringify(publishMessage(event))) + 1;
|
|
237
216
|
if (bytes + 32 > REPLICATION_BATCH_BYTES) {
|
|
238
217
|
return yield* Effect.die(new Error("A replication event exceeds the publish request limit."));
|
|
@@ -243,10 +222,6 @@ export const pendingReplicationEvents = (sql, target, options = {}) => Effect.ge
|
|
|
243
222
|
size += bytes;
|
|
244
223
|
result.push({ historyId: reference.id, event });
|
|
245
224
|
}
|
|
246
|
-
if (suppressed.length) {
|
|
247
|
-
yield* sql.unsafe(`UPDATE _ontology_replication_delivery SET suppressed_at=now()
|
|
248
|
-
WHERE generation=$1::uuid AND event_id IN (SELECT value::uuid FROM jsonb_array_elements_text($2::jsonb))`, [target.generation, JSON.stringify(suppressed)]);
|
|
249
|
-
}
|
|
250
225
|
return result;
|
|
251
226
|
});
|
|
252
227
|
/** Lost publish responses deliberately leave the same immutable IDs pending. */
|
package/dist/runtime/agents.js
CHANGED
|
@@ -6,7 +6,7 @@ import { AgentChat } from "../agent-chat.js";
|
|
|
6
6
|
import { chatChannelKind } from "../chat-channel.js";
|
|
7
7
|
import { AgentReview } from "../agent-review.js";
|
|
8
8
|
import { AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, CHANNEL_USERS_TABLE, IDENTITY_NOTICES_TABLE, USER_TABLE, } from "../migrate/storage.js";
|
|
9
|
-
import { inboxRolesOf } from "../manifest.js";
|
|
9
|
+
import { inboxRolesOf, takeoverChannelsOf, takeoverProvidersOf } from "../manifest.js";
|
|
10
10
|
import { GUEST_ROLE } from "../names.js";
|
|
11
11
|
import { MAX_QUERY_LIMIT } from "../query.js";
|
|
12
12
|
import { beginReplicationWrite } from "../replication/transaction.js";
|
|
@@ -547,6 +547,11 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
547
547
|
title: String(chat.title),
|
|
548
548
|
purpose: nullableText(chat.purpose),
|
|
549
549
|
whenToUse: nullableText(chat.when_to_use),
|
|
550
|
+
// A member of staff took the chat over: the agent records the person and answers none.
|
|
551
|
+
...((chat.handled_by_id ?? null) !== null &&
|
|
552
|
+
takeoverChannelsOf(ontology.manifest).has(input.provider)
|
|
553
|
+
? { handled: true }
|
|
554
|
+
: {}),
|
|
550
555
|
};
|
|
551
556
|
}));
|
|
552
557
|
}).pipe(Effect.orDie),
|
|
@@ -694,9 +699,14 @@ export function resolveChannelSignIn(ontology, sql, input) {
|
|
|
694
699
|
*/
|
|
695
700
|
function inInbox(ontology, input, resolution) {
|
|
696
701
|
const roles = inboxRolesOf(ontology.manifest);
|
|
697
|
-
|
|
698
|
-
input.chat
|
|
699
|
-
resolution.member.principal.roles.some((role) => roles.has(role))
|
|
700
|
-
|
|
701
|
-
|
|
702
|
+
if ((resolution.status !== "existing" && resolution.status !== "created") ||
|
|
703
|
+
input.chat !== "private" ||
|
|
704
|
+
!resolution.member.principal.roles.some((role) => roles.has(role))) {
|
|
705
|
+
return resolution;
|
|
706
|
+
}
|
|
707
|
+
// Staff may take chats over on a channel of this account's provider: the agent reports every
|
|
708
|
+
// message, and the report, which names the channel, says whether a member of staff holds the chat.
|
|
709
|
+
return takeoverProvidersOf(ontology.manifest).has(input.provider)
|
|
710
|
+
? { ...resolution, inbox: true, takeover: true }
|
|
711
|
+
: { ...resolution, inbox: true };
|
|
702
712
|
}
|
|
@@ -68,6 +68,11 @@ function valueFields(ontology, entity, { mode }) {
|
|
|
68
68
|
: Schema.optionalKey(field.nullable ? Schema.NullOr(value) : value);
|
|
69
69
|
}
|
|
70
70
|
for (const link of entity.links.values()) {
|
|
71
|
+
// A built-in entity's relationships change only through the platform's own actions, such as
|
|
72
|
+
// agent_chat's takeOver: its update takes its unmanaged properties alone.
|
|
73
|
+
if (link.via === "key" && entity.resolved.builtIn) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
71
76
|
if (link.via === "key") {
|
|
72
77
|
const target = reference(link, relationshipDescription(entity, link));
|
|
73
78
|
const required = mode === "create" && !link.optional;
|
|
@@ -7,7 +7,11 @@ import type { Ontology } from "../ontology.js";
|
|
|
7
7
|
import { OntologyBackend, type OntologyServiceRuntime } from "../service/runtime-contract.js";
|
|
8
8
|
import { SignInLinks } from "./sign-in-links.js";
|
|
9
9
|
import { WorkflowStarter } from "./workflow-starts.js";
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* The service's backend on the runtime, over the SQL client in context. `blobStorage` is the
|
|
12
|
+
* deployment's file storage, or a test's `memoryFileStorage().blobStorage`; without it, actions
|
|
13
|
+
* that upload, stage or open files are refused.
|
|
14
|
+
*/
|
|
11
15
|
export declare function backendLayer(ontology: Ontology, blobStorage?: Redacted.Redacted<BlobStorageBinding>, workflowStarter?: WorkflowStarter["Service"], deployment?: DeploymentObservation, signInLinks?: SignInLinks["Service"]): Layer.Layer<OntologyBackend, never, SqlClient.SqlClient>;
|
|
12
16
|
/**
|
|
13
17
|
* `@effect/sql-pg` on the pooled `ontology_app` connection string, with the TLS server name a
|
|
@@ -3,6 +3,7 @@ import { randomUUID } from "node:crypto";
|
|
|
3
3
|
import { Effect, Layer, Redacted } from "effect";
|
|
4
4
|
import { SqlClient } from "effect/unstable/sql";
|
|
5
5
|
import { isSqlError } from "effect/unstable/sql/SqlError";
|
|
6
|
+
import { memoryFileStore } from "../storage/memory.js";
|
|
6
7
|
import { neonBlobProvider } from "../storage/neon.js";
|
|
7
8
|
import { maintainBlobStorage } from "../storage/maintenance.js";
|
|
8
9
|
import { attachmentRuntime } from "../storage/attachments.js";
|
|
@@ -42,7 +43,11 @@ function rejectedAs(operation) {
|
|
|
42
43
|
...(rejection.diagnostic === undefined ? {} : { diagnostic: rejection.diagnostic }),
|
|
43
44
|
}), Effect.as(undefined));
|
|
44
45
|
}
|
|
45
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* The service's backend on the runtime, over the SQL client in context. `blobStorage` is the
|
|
48
|
+
* deployment's file storage, or a test's `memoryFileStorage().blobStorage`; without it, actions
|
|
49
|
+
* that upload, stage or open files are refused.
|
|
50
|
+
*/
|
|
46
51
|
export function backendLayer(ontology, blobStorage, workflowStarter, deployment, signInLinks) {
|
|
47
52
|
return Layer.effect(OntologyBackend, Effect.gen(function* () {
|
|
48
53
|
const sql = yield* SqlClient.SqlClient;
|
|
@@ -134,10 +139,14 @@ export function backendLayer(ontology, blobStorage, workflowStarter, deployment,
|
|
|
134
139
|
.pipe(Effect.orDie);
|
|
135
140
|
}
|
|
136
141
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
142
|
+
// A test's in-memory store answers its own transfer grants; a deployment's binding names
|
|
143
|
+
// the branch storage of its database.
|
|
144
|
+
const memory = memoryFileStore(blobStorage);
|
|
145
|
+
const provider = memory?.provider ??
|
|
146
|
+
(binding
|
|
147
|
+
? yield* Effect.acquireRelease(Effect.sync(() => neonBlobProvider(binding, deployment)), (provider) => Effect.sync(() => provider.close?.()))
|
|
148
|
+
: undefined);
|
|
149
|
+
const blobs = provider ? blobRuntime(ontology, sql, provider, memory?.fetch) : undefined;
|
|
141
150
|
const attachments = provider ? attachmentRuntime(ontology, sql, provider) : undefined;
|
|
142
151
|
return OntologyBackend.of({
|
|
143
152
|
workflowAdmissions: {
|
|
@@ -210,6 +210,7 @@ function resolveChannelIdentity(store, agentTools) {
|
|
|
210
210
|
notice_due: resolution.noticeDue,
|
|
211
211
|
on_new_user_pending: resolution.onNewUserPending,
|
|
212
212
|
...(resolution.inbox === true ? { inbox: true } : {}),
|
|
213
|
+
...(resolution.takeover === true ? { takeover: true } : {}),
|
|
213
214
|
});
|
|
214
215
|
case "limited":
|
|
215
216
|
return json({
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { validateTelegramPreviews } from "../../telegram.js";
|
|
2
|
+
import { takeoverChannelsOf } from "../../manifest.js";
|
|
2
3
|
import { randomUUID } from "node:crypto";
|
|
3
4
|
import { Cause, Effect } from "effect";
|
|
4
5
|
import { chatOperationId, chatPreviewDigest, } from "../../chat.js";
|
|
@@ -43,6 +44,7 @@ agent = {}) {
|
|
|
43
44
|
}
|
|
44
45
|
const attribution = ontology.config.chat?.attribution;
|
|
45
46
|
const separator = attribution?.separator ?? "\n\n";
|
|
47
|
+
const takeoverChannels = takeoverChannelsOf(ontology.manifest);
|
|
46
48
|
const providers = listedChatProviders(ontology.manifest.chat);
|
|
47
49
|
const destination = ontology.manifest.chat?.telegram?.connection;
|
|
48
50
|
const destinationPreviews = previews.settings?.connection === destination ? previews : { ownOrigins: previews.ownOrigins };
|
|
@@ -228,6 +230,7 @@ agent = {}) {
|
|
|
228
230
|
fetch: agent.fetch ?? ((request) => fetch(request)),
|
|
229
231
|
};
|
|
230
232
|
return {
|
|
233
|
+
takeoverChannels,
|
|
231
234
|
sendReview: reviewSender({
|
|
232
235
|
ontology,
|
|
233
236
|
connections,
|
|
@@ -627,9 +627,9 @@ export const FEEDBACK_INSTRUCTIONS = "Feedback: this app accepts feedback for it
|
|
|
627
627
|
"something works badly or asks for something new. Do not offer what you can do yourself; offer " +
|
|
628
628
|
"once per problem. Before submitting, look for the user's open app_feedback with the same " +
|
|
629
629
|
"subject. Submit with app_feedback_submit only after the user agrees to the title and summary you " +
|
|
630
|
-
"show in their language.
|
|
631
|
-
"
|
|
632
|
-
"
|
|
630
|
+
"show in their language. When the user asks about their feedback, read only their own: filter " +
|
|
631
|
+
"app_feedback by reporter, their user id from me, since builders can read everyone's. Tell " +
|
|
632
|
+
"them the builders' response, then call app_feedback_acknowledge with its responded_at.";
|
|
633
633
|
export const AUTOMATIC_FAILURE_REPORT_INSTRUCTIONS = "Feedback: During requested work, this workspace preauthorizes minimal technical failure reports. " +
|
|
634
634
|
"Automatically record unexpected or persistent tool/connection failures through app_feedback_submit; " +
|
|
635
635
|
"first reuse the caller's open report with the same subject. Include only the operation, safe technical " +
|
|
@@ -637,8 +637,8 @@ export const AUTOMATIC_FAILURE_REPORT_INSTRUCTIONS = "Feedback: During requested
|
|
|
637
637
|
"reason and that the report was recorded in one or two sentences; do not merely offer to report it, " +
|
|
638
638
|
"and never claim it was recorded before a successful submission or existing-item read. " +
|
|
639
639
|
"Feature/change requests and sharing task contents or files still require the member's agreement " +
|
|
640
|
-
"to the title and summary.
|
|
641
|
-
"acknowledge only the response you
|
|
640
|
+
"to the title and summary. Asked about their feedback, read only the caller's own items " +
|
|
641
|
+
"(reporter is their user id from me) and acknowledge only the response you relayed.";
|
|
642
642
|
/**
|
|
643
643
|
* Fixed guidance keeps initialization independent of ontology size: the feedback paragraph depends
|
|
644
644
|
* only on whether the caller may run `app_feedback_submit`.
|
|
@@ -528,6 +528,11 @@ export type AgentResolution = {
|
|
|
528
528
|
* reports the chat (`POST /api/ontology/agent/chats/seen`), which registers it.
|
|
529
529
|
*/
|
|
530
530
|
readonly inbox?: true;
|
|
531
|
+
/**
|
|
532
|
+
* Staff may take the chat over on its channel (`inbox.takeover.channels`): the agent reports
|
|
533
|
+
* every message, and answers it only while the report does not say the chat is `handled`.
|
|
534
|
+
*/
|
|
535
|
+
readonly takeover?: true;
|
|
531
536
|
} | {
|
|
532
537
|
readonly status: "pairing_required";
|
|
533
538
|
} | {
|
|
@@ -544,12 +549,17 @@ declare const IdentityInUse_base: Schema.Class<IdentityInUse, Schema.TaggedStruc
|
|
|
544
549
|
/** An account already belongs to another member; approval must roll back without consuming the code. */
|
|
545
550
|
export declare class IdentityInUse extends IdentityInUse_base {
|
|
546
551
|
}
|
|
547
|
-
/** A
|
|
552
|
+
/** A chat the agent saw, as `POST /api/ontology/agent/chats/seen` answers. */
|
|
548
553
|
export interface SeenChat {
|
|
549
554
|
readonly id: string;
|
|
550
555
|
readonly title: string;
|
|
551
556
|
readonly purpose: string | null;
|
|
552
557
|
readonly whenToUse: string | null;
|
|
558
|
+
/**
|
|
559
|
+
* A person's private chat a member of staff took over: the agent records the message and answers
|
|
560
|
+
* none of it until they hand it back.
|
|
561
|
+
*/
|
|
562
|
+
readonly handled?: true;
|
|
553
563
|
}
|
|
554
564
|
/**
|
|
555
565
|
* Where the app's agent keeps pairing codes and the identities they create: the runtime's agent
|
|
@@ -547,6 +547,9 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
547
547
|
}>>;
|
|
548
548
|
readonly inbox: Schema.optionalKey<Schema.Struct<{
|
|
549
549
|
readonly roles: Schema.$Array<Schema.String>;
|
|
550
|
+
readonly takeover: Schema.optionalKey<Schema.Struct<{
|
|
551
|
+
readonly channels: Schema.$Array<Schema.String>;
|
|
552
|
+
}>>;
|
|
550
553
|
}>>;
|
|
551
554
|
readonly metrics: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
552
555
|
readonly name: Schema.String;
|
|
@@ -873,6 +876,9 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
873
876
|
}>>>;
|
|
874
877
|
readonly inbox: Schema.toEncoded<Schema.optionalKey<Schema.Struct<{
|
|
875
878
|
readonly roles: Schema.$Array<Schema.String>;
|
|
879
|
+
readonly takeover: Schema.optionalKey<Schema.Struct<{
|
|
880
|
+
readonly channels: Schema.$Array<Schema.String>;
|
|
881
|
+
}>>;
|
|
876
882
|
}>>>;
|
|
877
883
|
readonly metrics: Schema.toEncoded<Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
878
884
|
readonly name: Schema.String;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Effect, Redacted } from "effect";
|
|
2
|
+
import type { BlobUpload, BlobValue } from "../blob.js";
|
|
3
|
+
import { type ActionError } from "../errors.js";
|
|
4
|
+
import type { OpenFile } from "../files.js";
|
|
5
|
+
import type { Principal } from "../runtime.js";
|
|
6
|
+
import { OntologyBackend } from "../service/runtime-contract.js";
|
|
7
|
+
import { type BlobProvider, type BlobStorageBinding } from "./provider.js";
|
|
8
|
+
export interface MemoryFileStorageOptions {
|
|
9
|
+
/** The workspace's storage quota in bytes, which uploads count against. 1 GiB by default. */
|
|
10
|
+
readonly quotaBytes?: number;
|
|
11
|
+
}
|
|
12
|
+
/** A file to upload for an action's `uploads.for(entity, property)` input. */
|
|
13
|
+
export interface MemoryFileUpload {
|
|
14
|
+
/** Who uploads: the member or service user that then runs the action, with its roles stored. */
|
|
15
|
+
readonly principal: Principal;
|
|
16
|
+
/** The entity and file property of the action's `uploads.for` input. */
|
|
17
|
+
readonly entity: string;
|
|
18
|
+
readonly property: string;
|
|
19
|
+
/** The action that consumes the ticket, on `entity`. */
|
|
20
|
+
readonly action: string;
|
|
21
|
+
/** The record the action changes. Omit it for a record the action creates. */
|
|
22
|
+
readonly recordId?: string;
|
|
23
|
+
/** Names the record the action creates; the idempotency key by default, without `recordId`. */
|
|
24
|
+
readonly createIntent?: string;
|
|
25
|
+
/** The upload's and the action's idempotency key, a UUID; a new one by default. */
|
|
26
|
+
readonly idempotencyKey?: string;
|
|
27
|
+
readonly filename: string;
|
|
28
|
+
readonly contentType: string;
|
|
29
|
+
/** The file's bytes; a string is stored as UTF-8. */
|
|
30
|
+
readonly bytes: Uint8Array | string;
|
|
31
|
+
}
|
|
32
|
+
/** A completed upload, ready for the action it was made for. */
|
|
33
|
+
export interface UploadedFile {
|
|
34
|
+
/**
|
|
35
|
+
* The ticket an `uploads.for` input takes: `{ uploadId }` in an entity handle's action call,
|
|
36
|
+
* `{ upload_id: ticket.uploadId }` in an action's JSON input over HTTP or `runAction`.
|
|
37
|
+
*/
|
|
38
|
+
readonly ticket: BlobUpload;
|
|
39
|
+
/** The action options that consume the ticket: its idempotency key and, for a new record, create intent. */
|
|
40
|
+
readonly options: {
|
|
41
|
+
readonly idempotencyKey: string;
|
|
42
|
+
readonly createIntent?: string;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** A stored file revision read back, with its bytes. */
|
|
46
|
+
export interface StoredFile {
|
|
47
|
+
readonly file: BlobValue;
|
|
48
|
+
readonly bytes: Uint8Array;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* File storage held in memory, for tests that run the ontology in process. The backend built on
|
|
52
|
+
* it uploads, stages, opens and downloads files as on a deployment, with the same permission
|
|
53
|
+
* checks and upload lifecycle in the database; only the bytes stay in this process.
|
|
54
|
+
*/
|
|
55
|
+
export interface MemoryFileStorage {
|
|
56
|
+
/**
|
|
57
|
+
* The file storage to run the backend on: `backendLayer(ontology, files.blobStorage)`, or the
|
|
58
|
+
* `blobStorage` of `createOntologyService`'s configuration, which then also needs a
|
|
59
|
+
* `storageMaintenanceKey`.
|
|
60
|
+
*/
|
|
61
|
+
readonly blobStorage: Redacted.Redacted<BlobStorageBinding>;
|
|
62
|
+
/**
|
|
63
|
+
* Answers the transfer grants the store signs, as storage does: `PUT` stores an upload's bytes
|
|
64
|
+
* and `GET` returns a file's. A test that uploads over HTTP sends the bytes with it, such as
|
|
65
|
+
* `createBlobStorageClient(transport, files.fetch)` of the App SDK.
|
|
66
|
+
*/
|
|
67
|
+
readonly fetch: typeof fetch;
|
|
68
|
+
/**
|
|
69
|
+
* Uploads `bytes` as `principal` for `entity.action`'s `uploads.for(entity, property)` input
|
|
70
|
+
* and completes the upload: what an app's upload does, through the backend in context, which
|
|
71
|
+
* must run on this store. It checks the action's permission, the property's file policy and
|
|
72
|
+
* the quota as the service does.
|
|
73
|
+
*/
|
|
74
|
+
readonly upload: (input: MemoryFileUpload) => Effect.Effect<UploadedFile, ActionError, OntologyBackend>;
|
|
75
|
+
/**
|
|
76
|
+
* Reads a stored file revision back as `principal`, with the entity's read permission and
|
|
77
|
+
* scopes checked as `Files.open` checks them: the record's current file, or `revisionId`.
|
|
78
|
+
*/
|
|
79
|
+
readonly read: (input: OpenFile & {
|
|
80
|
+
readonly principal: Principal;
|
|
81
|
+
}) => Effect.Effect<StoredFile, ActionError, OntologyBackend>;
|
|
82
|
+
}
|
|
83
|
+
interface MemoryStore {
|
|
84
|
+
readonly provider: BlobProvider["Service"];
|
|
85
|
+
readonly fetch: typeof fetch;
|
|
86
|
+
}
|
|
87
|
+
/** The in-memory store `blobStorage` names, when `memoryFileStorage` made it. */
|
|
88
|
+
export declare function memoryFileStore(blobStorage: Redacted.Redacted<BlobStorageBinding> | undefined): MemoryStore | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* File storage held in memory, for tests that run an ontology's actions with files in process:
|
|
91
|
+
* `uploads.for` inputs, `Files.stage`, `Files.open`, downloads and the agent's attachments. Each
|
|
92
|
+
* call makes an empty store.
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* const files = memoryFileStorage();
|
|
96
|
+
* const backend = backendLayer(ontology, files.blobStorage).pipe(Layer.provide(database));
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function memoryFileStorage(options?: MemoryFileStorageOptions): MemoryFileStorage;
|
|
100
|
+
export {};
|