@segmentstream/ontology 0.104.0 → 0.106.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 +27 -0
- package/dist/analyze.d.ts +9 -1
- package/dist/analyze.js +7 -3
- package/dist/email.d.ts +30 -0
- package/dist/email.js +97 -0
- package/dist/manifest.d.ts +39 -0
- package/dist/manifest.js +3 -0
- package/dist/migrate/model.d.ts +24 -0
- package/dist/nitro-workflows.d.ts +14 -0
- package/dist/nitro-workflows.js +62 -2
- package/dist/runtime/agents.js +8 -2
- package/dist/runtime/channel-identity-admin.js +6 -2
- package/dist/runtime/resolve-agent-identity.d.ts +1 -1
- package/dist/runtime/resolve-agent-identity.js +56 -4
- package/dist/runtime/user-identity-sql.d.ts +5 -0
- package/dist/runtime/user-identity-sql.js +5 -0
- package/dist/server.d.ts +7 -0
- package/dist/server.js +9 -1
- package/dist/service/agent/protocol.d.ts +1 -0
- package/dist/service/agent/protocol.js +4 -0
- package/dist/service/agent/server.js +2 -1
- package/dist/service/chat/providers/email.d.ts +15 -0
- package/dist/service/chat/providers/email.js +133 -0
- package/dist/service/chat/providers/index.d.ts +5 -1
- package/dist/service/chat/providers/index.js +7 -2
- package/dist/service/chat/providers/provider.d.ts +5 -0
- package/dist/service/runtime-contract.d.ts +5 -0
- package/dist/service/schemas.d.ts +6 -0
- package/dist/service/workflows.d.ts +2 -0
- package/dist/service/workflows.js +14 -1
- package/dist/storage/tables.d.ts +1 -1
- package/dist/storage/tables.js +1 -1
- package/dist/user-identity.d.ts +4 -2
- package/dist/user-identity.js +4 -2
- package/dist/workflow-runtime.d.ts +5 -0
- package/dist/workflow-runtime.js +9 -0
- package/dist/workflow-structure-extract.d.ts +52 -0
- package/dist/workflow-structure-extract.js +1096 -0
- package/dist/workflow-structure.d.ts +141 -0
- package/dist/workflow-structure.js +16 -0
- package/package.json +6 -2
|
@@ -3,11 +3,13 @@ import { Effect, Option, Schema } from "effect";
|
|
|
3
3
|
import { isSqlError } from "effect/unstable/sql/SqlError";
|
|
4
4
|
import { Conflict } from "../errors.js";
|
|
5
5
|
import { channelPolicyOf, signUpRolesOf } from "../manifest.js";
|
|
6
|
-
import { AGENT_IDENTITIES_TABLE, CHANNEL_USERS_TABLE } from "../migrate/storage.js";
|
|
6
|
+
import { AGENT_IDENTITIES_TABLE, CHANNEL_USERS_TABLE, USER_TABLE } from "../migrate/storage.js";
|
|
7
|
+
import { EMAIL_PATTERN } from "../property.js";
|
|
7
8
|
import { countAgentAttempt, effectiveChannelLimits } from "./agent-counters.js";
|
|
8
9
|
import { runNewUserHook } from "./new-user-hook.js";
|
|
9
10
|
import { makeOntologyRuntime, makePrincipalUserWriter } from "./sql-runtime.js";
|
|
10
11
|
import { makeIdentityWriter, USER_IDENTITY_TABLE } from "./user-identities.js";
|
|
12
|
+
import { EMAIL_PROVIDER } from "./user-identity-sql.js";
|
|
11
13
|
class RegistrationCapped extends Schema.TaggedError()("RegistrationCapped", {
|
|
12
14
|
limit: Schema.Int,
|
|
13
15
|
}) {
|
|
@@ -21,7 +23,16 @@ function uniqueFailure(error) {
|
|
|
21
23
|
return sqlError.reason._tag === "UniqueViolation" || cause?.code === "23505";
|
|
22
24
|
}
|
|
23
25
|
/** Account admission shares pairing's lock and creates User plus identity in one transaction. */
|
|
24
|
-
export function resolveAgentIdentity(ontology, sql, store,
|
|
26
|
+
export function resolveAgentIdentity(ontology, sql, store, requested, context = "agent") {
|
|
27
|
+
const mail = requested.provider === EMAIL_PROVIDER;
|
|
28
|
+
// An address is one account whatever its case. Mail the receiving server could not prove may
|
|
29
|
+
// come from anyone: it admits nobody, and the person's existing account acts on none of it.
|
|
30
|
+
const input = mail
|
|
31
|
+
? { ...requested, externalId: requested.externalId.trim().toLowerCase() }
|
|
32
|
+
: requested;
|
|
33
|
+
if (mail && (requested.addressVerified !== true || !EMAIL_PATTERN.test(input.externalId))) {
|
|
34
|
+
return Effect.succeed({ status: "pairing_required" });
|
|
35
|
+
}
|
|
25
36
|
const contact = input.turn || context === "channel_app";
|
|
26
37
|
// A channel `signUp` opens grants these roles; one an open `signIn.channels` opens grants member.
|
|
27
38
|
const signUpRoles = signUpRolesOf(ontology.manifest, input.provider);
|
|
@@ -35,6 +46,41 @@ export function resolveAgentIdentity(ontology, sql, store, input, context = "age
|
|
|
35
46
|
if (identity !== undefined && identity.revokedAt !== null) {
|
|
36
47
|
return { status: "refused", reason: "blocked" };
|
|
37
48
|
}
|
|
49
|
+
if (identity === undefined && mail && contact && input.chat === "private") {
|
|
50
|
+
// The address of a user sign-up created is theirs: a code in the app or proved mail shows
|
|
51
|
+
// the same thing. A member's address is not linked: mail proves its domain, not the
|
|
52
|
+
// mailbox, so the member attaches it themselves.
|
|
53
|
+
const [owner] = yield* sql.unsafe(`SELECT "u"."id"::text AS "id",
|
|
54
|
+
COALESCE(to_jsonb("c") ->> 'signed_up', 'false') = 'true' AS "signed_up",
|
|
55
|
+
EXISTS (SELECT 1 FROM "${USER_IDENTITY_TABLE}" AS "i" WHERE "i"."user_id" = "u"."id"
|
|
56
|
+
AND "i"."provider" = $2 AND "i"."revoked_at" IS NULL) AS "held"
|
|
57
|
+
FROM "${USER_TABLE}" AS "u"
|
|
58
|
+
LEFT JOIN "${CHANNEL_USERS_TABLE}" AS "c" ON "c"."user_id" = "u"."id"
|
|
59
|
+
WHERE "u"."email" = $1 AND "u"."kind" = 'member' LIMIT 1`, [input.externalId, EMAIL_PROVIDER]);
|
|
60
|
+
if (owner !== undefined) {
|
|
61
|
+
if (!owner.signed_up || owner.held) {
|
|
62
|
+
return { status: "pairing_required" };
|
|
63
|
+
}
|
|
64
|
+
const identityId = randomUUID();
|
|
65
|
+
yield* makeIdentityWriter(ontology, sql, {
|
|
66
|
+
action: `${USER_IDENTITY_TABLE}.provision`,
|
|
67
|
+
actionId: randomUUID(),
|
|
68
|
+
}).create({
|
|
69
|
+
id: identityId,
|
|
70
|
+
provider: input.provider,
|
|
71
|
+
issuer: null,
|
|
72
|
+
subject: input.externalId,
|
|
73
|
+
userId: owner.id,
|
|
74
|
+
origin: "channel",
|
|
75
|
+
display: input.display?.trim() || null,
|
|
76
|
+
username: null,
|
|
77
|
+
});
|
|
78
|
+
yield* sql.unsafe(`INSERT INTO "${AGENT_IDENTITIES_TABLE}"
|
|
79
|
+
("id", "provider", "external_id", "user_id", "display", "origin")
|
|
80
|
+
VALUES ($1::uuid, $2, $3, $4::uuid, $5, 'provisioned')`, [identityId, input.provider, input.externalId, owner.id, input.display?.trim() || null]);
|
|
81
|
+
identity = yield* store.findIdentity(input);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
38
84
|
if (identity === undefined) {
|
|
39
85
|
if (!contact ||
|
|
40
86
|
input.chat !== "private" ||
|
|
@@ -63,8 +109,14 @@ export function resolveAgentIdentity(ontology, sql, store, input, context = "age
|
|
|
63
109
|
});
|
|
64
110
|
const user = yield* writer.create({
|
|
65
111
|
kind: "member",
|
|
66
|
-
name: display ||
|
|
67
|
-
|
|
112
|
+
name: display ||
|
|
113
|
+
(username
|
|
114
|
+
? `@${username}`
|
|
115
|
+
: mail
|
|
116
|
+
? input.externalId
|
|
117
|
+
: `${providerName} ${input.externalId}`),
|
|
118
|
+
// Proved mail proves the address: a later code sign-in with it is the same person.
|
|
119
|
+
email: mail ? input.externalId : null,
|
|
68
120
|
identity_provider: null,
|
|
69
121
|
identity_subject: null,
|
|
70
122
|
});
|
|
@@ -11,6 +11,11 @@ export declare const USER_IDENTITY_TABLE = "user_identity";
|
|
|
11
11
|
export declare const USER_IDENTITY_ENTITY_ID = "ent_user_identity";
|
|
12
12
|
/** The provider of email sign-in: the workspace's Neon Auth. */
|
|
13
13
|
export declare const NEON_AUTH_PROVIDER = "neon-auth";
|
|
14
|
+
/**
|
|
15
|
+
* The provider of an address that writes to the agent's mailbox. Its subject is the address in
|
|
16
|
+
* lower case; the agent's mail channel admits it only when the receiving server proved it.
|
|
17
|
+
*/
|
|
18
|
+
export declare const EMAIL_PROVIDER = "email";
|
|
14
19
|
/** The provider of SegmentStream (OIDC) sign-in; the issuer tells the issuing deployment apart. */
|
|
15
20
|
export declare const OIDC_PROVIDER = "oidc";
|
|
16
21
|
/** The legacy `user.identity_provider` of an OIDC binding: `oidc:<issuer>`. */
|
|
@@ -11,6 +11,11 @@ export const USER_IDENTITY_TABLE = "user_identity";
|
|
|
11
11
|
export const USER_IDENTITY_ENTITY_ID = "ent_user_identity";
|
|
12
12
|
/** The provider of email sign-in: the workspace's Neon Auth. */
|
|
13
13
|
export const NEON_AUTH_PROVIDER = "neon-auth";
|
|
14
|
+
/**
|
|
15
|
+
* The provider of an address that writes to the agent's mailbox. Its subject is the address in
|
|
16
|
+
* lower case; the agent's mail channel admits it only when the receiving server proved it.
|
|
17
|
+
*/
|
|
18
|
+
export const EMAIL_PROVIDER = "email";
|
|
14
19
|
/** The provider of SegmentStream (OIDC) sign-in; the issuer tells the issuing deployment apart. */
|
|
15
20
|
export const OIDC_PROVIDER = "oidc";
|
|
16
21
|
/** The legacy `user.identity_provider` of an OIDC binding: `oidc:<issuer>`. */
|
package/dist/server.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type PlatformWorkflows } from "./workflow-runtime.js";
|
|
2
|
+
import type { WorkflowStructures } from "./workflow-structure.js";
|
|
2
3
|
/**
|
|
3
4
|
* Prepares the process for serving the ontology's runs; the starter's `server/plugin.ts` calls it
|
|
4
5
|
* at startup. Until the flow handler loads, which sets the Workflow SDK's base path itself from the
|
|
@@ -21,3 +22,9 @@ export declare function installWorkflowRuntime(): void;
|
|
|
21
22
|
* since the service could not arm the recovery run that guarantees its starts.
|
|
22
23
|
*/
|
|
23
24
|
export declare function registerPlatformWorkflows(workflows: PlatformWorkflows): void;
|
|
25
|
+
/**
|
|
26
|
+
* Registers what the build read of each workflow's code, by workflow id: the plugin
|
|
27
|
+
* `ontologyWorkflows()` generates calls it at startup, and the service serves each declared
|
|
28
|
+
* workflow's structure beside its runs. Without it, the service answers that it has none.
|
|
29
|
+
*/
|
|
30
|
+
export declare function registerWorkflowStructures(structures: WorkflowStructures): void;
|
package/dist/server.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { setWorkflowBasePath } from "@workflow/utils";
|
|
7
7
|
import { deploymentFromEnv, requireDeploymentCapability } from "./deployment.js";
|
|
8
|
-
import { setPlatformWorkflows, setWorkflowSdk, } from "./workflow-runtime.js";
|
|
8
|
+
import { setPlatformWorkflows, setWorkflowSdk, setWorkflowStructures, } from "./workflow-runtime.js";
|
|
9
9
|
import { ONTOLOGY_QUEUE_NAMESPACE, WORKFLOW_BASE_PATH } from "./workflow-topics.js";
|
|
10
10
|
import { installWorkflowArgumentCodecs } from "./service/workflow-argument-serialization.js";
|
|
11
11
|
import { installWorkflowLogging } from "./service/workflow-logging.js";
|
|
@@ -105,3 +105,11 @@ export function installWorkflowRuntime() {
|
|
|
105
105
|
export function registerPlatformWorkflows(workflows) {
|
|
106
106
|
setPlatformWorkflows(workflows);
|
|
107
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Registers what the build read of each workflow's code, by workflow id: the plugin
|
|
110
|
+
* `ontologyWorkflows()` generates calls it at startup, and the service serves each declared
|
|
111
|
+
* workflow's structure beside its runs. Without it, the service answers that it has none.
|
|
112
|
+
*/
|
|
113
|
+
export function registerWorkflowStructures(structures) {
|
|
114
|
+
setWorkflowStructures(structures);
|
|
115
|
+
}
|
|
@@ -31,6 +31,7 @@ export declare const ResolveIdentityRequest: Schema.Struct<{
|
|
|
31
31
|
readonly username: Schema.optionalKey<Schema.String>;
|
|
32
32
|
readonly notice_version: Schema.optionalKey<Schema.String>;
|
|
33
33
|
readonly notice_sha256: Schema.optionalKey<Schema.String>;
|
|
34
|
+
readonly address_verified: Schema.optionalKey<Schema.Boolean>;
|
|
34
35
|
readonly limits: Schema.optionalKey<Schema.Struct<{
|
|
35
36
|
readonly perSender: Schema.optionalKey<Schema.Struct<{
|
|
36
37
|
readonly turnsPerMinute: Schema.optionalKey<Schema.Int>;
|
|
@@ -49,6 +49,10 @@ export const ResolveIdentityRequest = Schema.Struct({
|
|
|
49
49
|
username: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(MAX_DISPLAY_LENGTH))),
|
|
50
50
|
notice_version: Schema.optionalKey(Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(200))),
|
|
51
51
|
notice_sha256: Schema.optionalKey(Schema.String.check(Schema.isPattern(/^[a-f0-9]{64}$/))),
|
|
52
|
+
address_verified: Schema.optionalKey(Schema.Boolean).annotate({
|
|
53
|
+
description: "Whether the receiving server proved the sender's address, as DMARC does for mail. An " +
|
|
54
|
+
"email account is admitted only when it is.",
|
|
55
|
+
}),
|
|
52
56
|
limits: Schema.optionalKey(Schema.Struct({
|
|
53
57
|
perSender: Schema.optionalKey(Schema.Struct({
|
|
54
58
|
turnsPerMinute: Schema.optionalKey(PositiveLimit),
|
|
@@ -182,12 +182,13 @@ function resolveChannelIdentity(store) {
|
|
|
182
182
|
if (Result.isFailure(decoded)) {
|
|
183
183
|
return invalidBody(requestId, schemaIssues(decoded.failure));
|
|
184
184
|
}
|
|
185
|
-
const { external_id: externalId, notice_version, notice_sha256, ...input } = decoded.success;
|
|
185
|
+
const { external_id: externalId, notice_version, notice_sha256, address_verified, ...input } = decoded.success;
|
|
186
186
|
const resolution = yield* store.resolveIdentity({
|
|
187
187
|
...input,
|
|
188
188
|
externalId,
|
|
189
189
|
...(notice_version === undefined ? {} : { noticeVersion: notice_version }),
|
|
190
190
|
...(notice_sha256 === undefined ? {} : { noticeSha256: notice_sha256 }),
|
|
191
|
+
...(address_verified === undefined ? {} : { addressVerified: address_verified }),
|
|
191
192
|
});
|
|
192
193
|
switch (resolution.status) {
|
|
193
194
|
case "existing":
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type ChatProvider, type ChatSend, type ProviderOutcome } from "./provider.js";
|
|
3
|
+
/**
|
|
4
|
+
* One reply in the person's email thread, from the mailbox the ontology's `chat.email` names. It
|
|
5
|
+
* reads the thread's subject and its last message first, so the reply carries `Re:`, `In-Reply-To`
|
|
6
|
+
* and `References` and the person's mail program shows it in the same conversation; then
|
|
7
|
+
* `POST /users/me/messages/send` with the thread id, never retried. A reply sends no attribution
|
|
8
|
+
* and names no sender, so it leaves from the mailbox's own address. `sent` means Gmail accepted it.
|
|
9
|
+
*/
|
|
10
|
+
export declare function sendEmail(input: ChatSend): Effect.Effect<ProviderOutcome>;
|
|
11
|
+
/**
|
|
12
|
+
* Email through a connected mailbox. It only sends replies the agent held for review, in the
|
|
13
|
+
* person's thread: an exact chat post never goes to an inbox.
|
|
14
|
+
*/
|
|
15
|
+
export declare const emailProvider: ChatProvider;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { base64Url, EMAIL_MESSAGE_LIMIT, emailReplyMessage, renderEmailHtml, } from "../../../email.js";
|
|
3
|
+
import { EMAIL_PATTERN } from "../../../property.js";
|
|
4
|
+
import { record, transportFailure, } from "./provider.js";
|
|
5
|
+
/** A Gmail id, of a thread or a message. */
|
|
6
|
+
const GMAIL_ID = /^[A-Za-z0-9_-]{1,512}$/;
|
|
7
|
+
const decoder = new TextDecoder();
|
|
8
|
+
function parse(body) {
|
|
9
|
+
try {
|
|
10
|
+
return record(JSON.parse(decoder.decode(body)));
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** The headers of a thread's message, by lower-case name; the first of each wins. */
|
|
17
|
+
function headersOf(message) {
|
|
18
|
+
const headers = record(record(message).payload).headers;
|
|
19
|
+
const found = new Map();
|
|
20
|
+
for (const header of Array.isArray(headers) ? headers : []) {
|
|
21
|
+
const { name, value } = record(header);
|
|
22
|
+
if (typeof name === "string" && typeof value === "string" && !found.has(name.toLowerCase())) {
|
|
23
|
+
found.set(name.toLowerCase(), value);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return found;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* One reply in the person's email thread, from the mailbox the ontology's `chat.email` names. It
|
|
30
|
+
* reads the thread's subject and its last message first, so the reply carries `Re:`, `In-Reply-To`
|
|
31
|
+
* and `References` and the person's mail program shows it in the same conversation; then
|
|
32
|
+
* `POST /users/me/messages/send` with the thread id, never retried. A reply sends no attribution
|
|
33
|
+
* and names no sender, so it leaves from the mailbox's own address. `sent` means Gmail accepted it.
|
|
34
|
+
*/
|
|
35
|
+
export function sendEmail(input) {
|
|
36
|
+
return Effect.gen(function* () {
|
|
37
|
+
const threadId = input.threadId ?? null;
|
|
38
|
+
const failed = (cause, payload = {}) => ({
|
|
39
|
+
payload,
|
|
40
|
+
status: "failed",
|
|
41
|
+
messageId: null,
|
|
42
|
+
url: null,
|
|
43
|
+
cause,
|
|
44
|
+
});
|
|
45
|
+
if (threadId === null || !GMAIL_ID.test(threadId)) {
|
|
46
|
+
return failed(new Error("An email reply needs the Gmail thread it answers."));
|
|
47
|
+
}
|
|
48
|
+
const thread = yield* input.transport
|
|
49
|
+
.request({
|
|
50
|
+
connection: input.connection,
|
|
51
|
+
api: "gmail",
|
|
52
|
+
method: "GET",
|
|
53
|
+
// Every header of every message, without bodies. The connection proxy refuses a
|
|
54
|
+
// repeated query parameter, so the headers are not named one `metadataHeaders` each.
|
|
55
|
+
path: `/users/me/threads/${threadId}?format=metadata&fields=messages(payload/headers)`,
|
|
56
|
+
headers: [],
|
|
57
|
+
body: new Uint8Array(),
|
|
58
|
+
})
|
|
59
|
+
.pipe(Effect.exit);
|
|
60
|
+
// Reading the thread sends nothing: any failure here is definite.
|
|
61
|
+
if (thread._tag === "Failure" || thread.value.status >= 300) {
|
|
62
|
+
return failed(thread._tag === "Failure"
|
|
63
|
+
? thread.cause
|
|
64
|
+
: new Error(`Gmail answered the thread read with status ${thread.value.status}.`));
|
|
65
|
+
}
|
|
66
|
+
const messages = record(parse(thread.value.body)).messages;
|
|
67
|
+
const list = Array.isArray(messages) ? messages : [];
|
|
68
|
+
const first = headersOf(list[0]);
|
|
69
|
+
const last = headersOf(list.at(-1));
|
|
70
|
+
const raw = emailReplyMessage({
|
|
71
|
+
to: input.destination.external_id,
|
|
72
|
+
subject: first.get("subject") ?? null,
|
|
73
|
+
inReplyTo: last.get("message-id") ?? null,
|
|
74
|
+
references: last.get("references") ?? null,
|
|
75
|
+
text: input.plainText,
|
|
76
|
+
html: input.finalText,
|
|
77
|
+
});
|
|
78
|
+
const payload = { threadId, html: input.finalText, text: input.plainText };
|
|
79
|
+
const result = yield* input.transport
|
|
80
|
+
.request({
|
|
81
|
+
connection: input.connection,
|
|
82
|
+
api: "gmail",
|
|
83
|
+
method: "POST",
|
|
84
|
+
path: "/users/me/messages/send",
|
|
85
|
+
headers: [["content-type", "application/json"]],
|
|
86
|
+
body: new TextEncoder().encode(JSON.stringify({ raw: base64Url(raw), threadId })),
|
|
87
|
+
})
|
|
88
|
+
.pipe(Effect.exit);
|
|
89
|
+
if (result._tag === "Failure") {
|
|
90
|
+
return transportFailure(payload, result);
|
|
91
|
+
}
|
|
92
|
+
const { status } = result.value;
|
|
93
|
+
if (status >= 500 || status === 408 || status === 429) {
|
|
94
|
+
return {
|
|
95
|
+
payload,
|
|
96
|
+
status: "uncertain",
|
|
97
|
+
messageId: null,
|
|
98
|
+
url: null,
|
|
99
|
+
cause: new Error(`Gmail answered with status ${status}.`),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
if (status >= 300) {
|
|
103
|
+
return failed(new Error(`Gmail refused the reply with status ${status}.`), payload);
|
|
104
|
+
}
|
|
105
|
+
const id = parse(result.value.body)?.id;
|
|
106
|
+
return typeof id === "string" && GMAIL_ID.test(id)
|
|
107
|
+
? { payload, status: "sent", messageId: id, url: null }
|
|
108
|
+
: {
|
|
109
|
+
payload,
|
|
110
|
+
status: "uncertain",
|
|
111
|
+
messageId: null,
|
|
112
|
+
url: null,
|
|
113
|
+
cause: new Error("Gmail accepted the reply without its id."),
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Email through a connected mailbox. It only sends replies the agent held for review, in the
|
|
119
|
+
* person's thread: an exact chat post never goes to an inbox.
|
|
120
|
+
*/
|
|
121
|
+
export const emailProvider = {
|
|
122
|
+
provider: "email",
|
|
123
|
+
limit: EMAIL_MESSAGE_LIMIT,
|
|
124
|
+
posts: false,
|
|
125
|
+
isTargetId: (externalId, kind) => kind === "private" && EMAIL_PATTERN.test(externalId),
|
|
126
|
+
isReplyId: () => false,
|
|
127
|
+
render: (markdown, attribution, separator) => {
|
|
128
|
+
const text = attribution === "" ? markdown : `${markdown}${separator}${attribution}`;
|
|
129
|
+
return { finalText: renderEmailHtml(text), plainText: text };
|
|
130
|
+
},
|
|
131
|
+
payload: (finalText) => ({ html: finalText }),
|
|
132
|
+
send: sendEmail,
|
|
133
|
+
};
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import type { OntologyManifest } from "../../../manifest.js";
|
|
2
2
|
import type { ChatProvider } from "./provider.js";
|
|
3
3
|
export type { ChatProvider, ChatSend, ProviderOutcome } from "./provider.js";
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Every provider the platform sends to, by the name the `chat` config uses: exact chat posts and
|
|
6
|
+
* reviewed replies, or, for one whose `posts` is `false`, reviewed replies only.
|
|
7
|
+
*/
|
|
5
8
|
export declare const CHAT_PROVIDERS: {
|
|
6
9
|
readonly telegram: ChatProvider;
|
|
7
10
|
readonly slack: ChatProvider;
|
|
8
11
|
readonly whatsapp: ChatProvider;
|
|
12
|
+
readonly email: ChatProvider;
|
|
9
13
|
};
|
|
10
14
|
export type ChatProviderName = keyof typeof CHAT_PROVIDERS;
|
|
11
15
|
/** The provider a destination's chat is on, when the platform posts to it. */
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
import { emailProvider } from "./email.js";
|
|
1
2
|
import { slackProvider } from "./slack.js";
|
|
2
3
|
import { telegramProvider } from "./telegram.js";
|
|
3
4
|
import { whatsappProvider } from "./whatsapp.js";
|
|
4
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Every provider the platform sends to, by the name the `chat` config uses: exact chat posts and
|
|
7
|
+
* reviewed replies, or, for one whose `posts` is `false`, reviewed replies only.
|
|
8
|
+
*/
|
|
5
9
|
export const CHAT_PROVIDERS = {
|
|
6
10
|
telegram: telegramProvider,
|
|
7
11
|
slack: slackProvider,
|
|
8
12
|
whatsapp: whatsappProvider,
|
|
13
|
+
email: emailProvider,
|
|
9
14
|
};
|
|
10
15
|
const NAMES = Object.keys(CHAT_PROVIDERS);
|
|
11
16
|
const isChatProvider = (name) => Object.hasOwn(CHAT_PROVIDERS, name);
|
|
@@ -19,7 +24,7 @@ export function chatConnection(chat, provider) {
|
|
|
19
24
|
}
|
|
20
25
|
/** The providers the ontology declares a posting connection for: the destinations it posts to. */
|
|
21
26
|
export function declaredChatProviders(chat) {
|
|
22
|
-
return NAMES.filter((name) => chat?.[name] !== undefined);
|
|
27
|
+
return NAMES.filter((name) => chat?.[name] !== undefined && CHAT_PROVIDERS[name].posts !== false);
|
|
23
28
|
}
|
|
24
29
|
/**
|
|
25
30
|
* The providers whose chats the chat tools list, read and resolve: Telegram's, whose chats the
|
|
@@ -38,6 +38,11 @@ export interface ChatProvider {
|
|
|
38
38
|
readonly provider: string;
|
|
39
39
|
/** The most characters the provider accepts in one message, for the rendered and plain text. */
|
|
40
40
|
readonly limit: number;
|
|
41
|
+
/**
|
|
42
|
+
* Whether exact chat posts and the chat tools reach the provider's chats. `false` for one that
|
|
43
|
+
* only sends replies the agent held for review, such as email. Omitted, it is `true`.
|
|
44
|
+
*/
|
|
45
|
+
readonly posts?: boolean;
|
|
41
46
|
/** Whether an id names a chat of this kind on the provider. */
|
|
42
47
|
readonly isTargetId: (externalId: string, kind: Destination["kind"]) => boolean;
|
|
43
48
|
/** Whether an id names a message on the provider that a post may reply to. */
|
|
@@ -314,6 +314,11 @@ export interface ResolveIdentityInput {
|
|
|
314
314
|
readonly username?: string;
|
|
315
315
|
readonly noticeVersion?: string;
|
|
316
316
|
readonly noticeSha256?: string;
|
|
317
|
+
/**
|
|
318
|
+
* Whether the receiving server proved the sender's address, as DMARC with alignment does for
|
|
319
|
+
* mail. An `email` account is admitted, created or linked only when it is.
|
|
320
|
+
*/
|
|
321
|
+
readonly addressVerified?: boolean;
|
|
317
322
|
readonly limits?: {
|
|
318
323
|
readonly perSender?: Partial<AgentLimits["perSender"]>;
|
|
319
324
|
readonly newUsersPerHour?: number;
|
|
@@ -465,6 +465,9 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
465
465
|
readonly whatsapp: Schema.optionalKey<Schema.Struct<{
|
|
466
466
|
readonly connection: Schema.String;
|
|
467
467
|
}>>;
|
|
468
|
+
readonly email: Schema.optionalKey<Schema.Struct<{
|
|
469
|
+
readonly connection: Schema.String;
|
|
470
|
+
}>>;
|
|
468
471
|
readonly attribution: Schema.optionalKey<Schema.Struct<{
|
|
469
472
|
readonly template: Schema.optionalKey<Schema.String>;
|
|
470
473
|
readonly separator: Schema.optionalKey<Schema.String>;
|
|
@@ -738,6 +741,9 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
738
741
|
readonly whatsapp: Schema.optionalKey<Schema.Struct<{
|
|
739
742
|
readonly connection: Schema.String;
|
|
740
743
|
}>>;
|
|
744
|
+
readonly email: Schema.optionalKey<Schema.Struct<{
|
|
745
|
+
readonly connection: Schema.String;
|
|
746
|
+
}>>;
|
|
741
747
|
readonly attribution: Schema.optionalKey<Schema.Struct<{
|
|
742
748
|
readonly template: Schema.optionalKey<Schema.String>;
|
|
743
749
|
readonly separator: Schema.optionalKey<Schema.String>;
|
|
@@ -189,6 +189,8 @@ export declare const SWEEP_PATH = "/api/ontology/workflows/sweep";
|
|
|
189
189
|
* `workflow:<workflow>:start`, or for the platform;
|
|
190
190
|
* - `GET /api/ontology/workflows`, `…/:workflow/runs` and `…/runs/:runId` read the declarations
|
|
191
191
|
* and the SDK's run records for callers holding `workflow:<workflow>:read` and the platform;
|
|
192
|
+
* - `GET /api/ontology/workflows/:workflow/structure`, for the same callers, serves what the build
|
|
193
|
+
* read of the workflow's code (`../workflow-structure.ts`), `null` when it has none;
|
|
192
194
|
* - `GET /api/ontology/workflows/:workflow/starts` lists the starts its action triggers decided,
|
|
193
195
|
* pending and started, for the same callers;
|
|
194
196
|
* - `GET` and `POST /api/ontology/workflows/sweep`, verified with the cron secret, start the
|
|
@@ -25,7 +25,7 @@ import { validate } from "../runtime/validation.js";
|
|
|
25
25
|
import { triggerContext, WorkflowStartFailed, } from "../runtime/workflow-starts.js";
|
|
26
26
|
import { RECOVERY_CONFIRM_MS, RECOVERY_COVERAGE_MARGIN_MS, RECOVERY_FIRST_SWEEP_MS, } from "../start-recovery-contract.js";
|
|
27
27
|
import { nextCronTime, } from "../workflow.js";
|
|
28
|
-
import { platformWorkflows, setWorkflowBinding, WORKFLOW_BASE_PATH, workflowSdk, } from "../workflow-runtime.js";
|
|
28
|
+
import { platformWorkflows, setWorkflowBinding, WORKFLOW_BASE_PATH, workflowSdk, workflowStructures, } from "../workflow-runtime.js";
|
|
29
29
|
import { callbackIdentityToken } from "./workflow-callback.js";
|
|
30
30
|
import { expireAgentTask, startAgentTask, } from "./agent/task-start.js";
|
|
31
31
|
import { CurrentPrincipal, WORKLOAD_TOKEN_TYPE, withPrincipal, workloadKind, } from "./authentication.js";
|
|
@@ -565,6 +565,8 @@ function expected(effect, tags) {
|
|
|
565
565
|
* `workflow:<workflow>:start`, or for the platform;
|
|
566
566
|
* - `GET /api/ontology/workflows`, `…/:workflow/runs` and `…/runs/:runId` read the declarations
|
|
567
567
|
* and the SDK's run records for callers holding `workflow:<workflow>:read` and the platform;
|
|
568
|
+
* - `GET /api/ontology/workflows/:workflow/structure`, for the same callers, serves what the build
|
|
569
|
+
* read of the workflow's code (`../workflow-structure.ts`), `null` when it has none;
|
|
568
570
|
* - `GET /api/ontology/workflows/:workflow/starts` lists the starts its action triggers decided,
|
|
569
571
|
* pending and started, for the same callers;
|
|
570
572
|
* - `GET` and `POST /api/ontology/workflows/sweep`, verified with the cron secret, start the
|
|
@@ -1486,6 +1488,17 @@ export function workflowsLayer(options) {
|
|
|
1486
1488
|
nextCursor: page.value.hasMore ? page.value.cursor : null,
|
|
1487
1489
|
});
|
|
1488
1490
|
})));
|
|
1491
|
+
// What the build read of the workflow's code: its steps, waits, branches and loops.
|
|
1492
|
+
yield* router.add("GET", `${WORKFLOW_BASE_PATH}/workflows/:workflow/structure`, authorized((caller) => Effect.gen(function* () {
|
|
1493
|
+
const params = yield* HttpRouter.params;
|
|
1494
|
+
const found = yield* readable(caller, params.workflow);
|
|
1495
|
+
if (!found.ok) {
|
|
1496
|
+
return found.refused;
|
|
1497
|
+
}
|
|
1498
|
+
const workflowId = compiledWorkflowId(found.workflow.declaration.run);
|
|
1499
|
+
const structure = workflowId === undefined ? undefined : workflowStructures()?.[workflowId];
|
|
1500
|
+
return json({ structure: structure ?? null });
|
|
1501
|
+
})));
|
|
1489
1502
|
yield* router.add("GET", `${WORKFLOW_BASE_PATH}/workflows/:workflow/runs/:runId`, authorized((caller, request) => Effect.gen(function* () {
|
|
1490
1503
|
const requestId = yield* RequestId;
|
|
1491
1504
|
const params = yield* HttpRouter.params;
|
package/dist/storage/tables.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export declare const ATTACHMENTS_TABLE = "_agent_attachments";
|
|
|
6
6
|
* Where an attachment comes from: a chat message on a channel, or a file the agent produced in its
|
|
7
7
|
* sandbox and stores so that an action can keep it.
|
|
8
8
|
*/
|
|
9
|
-
export declare const ATTACHMENT_SOURCES: readonly ["telegram", "slack", "whatsapp", "sandbox"];
|
|
9
|
+
export declare const ATTACHMENT_SOURCES: readonly ["telegram", "slack", "whatsapp", "email", "sandbox"];
|
|
10
10
|
/** The check that limits `source` to {@link ATTACHMENT_SOURCES}; its name is PostgreSQL's default. */
|
|
11
11
|
export declare const ATTACHMENT_SOURCE_CHECK = "_agent_attachments_source_check";
|
|
12
12
|
/**
|
package/dist/storage/tables.js
CHANGED
|
@@ -59,7 +59,7 @@ export const ATTACHMENTS_TABLE = "_agent_attachments";
|
|
|
59
59
|
* Where an attachment comes from: a chat message on a channel, or a file the agent produced in its
|
|
60
60
|
* sandbox and stores so that an action can keep it.
|
|
61
61
|
*/
|
|
62
|
-
export const ATTACHMENT_SOURCES = ["telegram", "slack", "whatsapp", "sandbox"];
|
|
62
|
+
export const ATTACHMENT_SOURCES = ["telegram", "slack", "whatsapp", "email", "sandbox"];
|
|
63
63
|
/** The check that limits `source` to {@link ATTACHMENT_SOURCES}; its name is PostgreSQL's default. */
|
|
64
64
|
export const ATTACHMENT_SOURCE_CHECK = `${ATTACHMENTS_TABLE}_source_check`;
|
|
65
65
|
const SOURCE_CHECK = `CHECK ("source" IN (${ATTACHMENT_SOURCES.map((source) => `'${source}'`).join(", ")}))`;
|
package/dist/user-identity.d.ts
CHANGED
|
@@ -3,9 +3,11 @@ export declare const USER_IDENTITY_ORIGINS: readonly ["invite", "sign_in", "link
|
|
|
3
3
|
export type UserIdentityOrigin = (typeof USER_IDENTITY_ORIGINS)[number];
|
|
4
4
|
/**
|
|
5
5
|
* The chat providers whose accounts an administrator may attach to a member with
|
|
6
|
-
* `user_identity.attach`: the providers the app's agent talks on.
|
|
6
|
+
* `user_identity.attach`: the providers the app's agent talks on. An `email` account is the
|
|
7
|
+
* address a member writes to the agent's mailbox from, which the agent never links to a member
|
|
8
|
+
* by itself.
|
|
7
9
|
*/
|
|
8
|
-
export declare const ATTACHABLE_IDENTITY_PROVIDERS: readonly ["telegram", "whatsapp", "slack"];
|
|
10
|
+
export declare const ATTACHABLE_IDENTITY_PROVIDERS: readonly ["telegram", "whatsapp", "slack", "email"];
|
|
9
11
|
export type AttachableIdentityProvider = (typeof ATTACHABLE_IDENTITY_PROVIDERS)[number];
|
|
10
12
|
/**
|
|
11
13
|
* The built-in record of every external account a member signs in or talks with: their Neon Auth
|
package/dist/user-identity.js
CHANGED
|
@@ -12,9 +12,11 @@ export const USER_IDENTITY_ORIGINS = [
|
|
|
12
12
|
];
|
|
13
13
|
/**
|
|
14
14
|
* The chat providers whose accounts an administrator may attach to a member with
|
|
15
|
-
* `user_identity.attach`: the providers the app's agent talks on.
|
|
15
|
+
* `user_identity.attach`: the providers the app's agent talks on. An `email` account is the
|
|
16
|
+
* address a member writes to the agent's mailbox from, which the agent never links to a member
|
|
17
|
+
* by itself.
|
|
16
18
|
*/
|
|
17
|
-
export const ATTACHABLE_IDENTITY_PROVIDERS = ["telegram", "whatsapp", "slack"];
|
|
19
|
+
export const ATTACHABLE_IDENTITY_PROVIDERS = ["telegram", "whatsapp", "slack", "email"];
|
|
18
20
|
/**
|
|
19
21
|
* The built-in record of every external account a member signs in or talks with: their Neon Auth
|
|
20
22
|
* account, their SegmentStream (OIDC) accounts and their channel accounts, such as Telegram. Every
|
|
@@ -5,6 +5,7 @@ import type { Files } from "./files.js";
|
|
|
5
5
|
import type { OntologyActions, OntologyReads, OntologyWrites } from "./runtime.js";
|
|
6
6
|
import type { StartRecoveryInput, StartRecoveryOutcome } from "./start-recovery-contract.js";
|
|
7
7
|
import type { WorkflowReservation } from "./runtime/workflow-admission.js";
|
|
8
|
+
import type { WorkflowStructures } from "./workflow-structure.js";
|
|
8
9
|
/**
|
|
9
10
|
* The ontology service's workflow runtime, shared through `globalThis` so that every copy of this
|
|
10
11
|
* module a bundler makes sees the same one. This module imports none of the Workflow SDK: the
|
|
@@ -87,6 +88,10 @@ export declare function workflowBinding(): WorkflowBinding | undefined;
|
|
|
87
88
|
export declare function setPlatformWorkflows(workflows: PlatformWorkflows | undefined): void;
|
|
88
89
|
/** The platform's workflow functions as the generated plugin registered them; `undefined` before. */
|
|
89
90
|
export declare function platformWorkflows(): PlatformWorkflows | undefined;
|
|
91
|
+
/** Registers what the build read of each workflow's code; the latest registration wins. */
|
|
92
|
+
export declare function setWorkflowStructures(structures: WorkflowStructures | undefined): void;
|
|
93
|
+
/** The build's workflow structures by workflow id, as the generated plugin registered them. */
|
|
94
|
+
export declare function workflowStructures(): WorkflowStructures | undefined;
|
|
90
95
|
/** The Workflow SDK as `installWorkflowRuntime()` registered it; `undefined` before. */
|
|
91
96
|
export declare function workflowSdk(): WorkflowSdk | undefined;
|
|
92
97
|
/** `run` was called where no step runs: in a `"use workflow"` body or outside any workflow. */
|
package/dist/workflow-runtime.js
CHANGED
|
@@ -17,6 +17,7 @@ export const WORKFLOW_ROUTE_BASE = "/.well-known/workflow/v1";
|
|
|
17
17
|
const BINDING = Symbol.for("@segmentstream/ontology/workflow-binding");
|
|
18
18
|
const SDK = Symbol.for("@segmentstream/ontology/workflow-sdk");
|
|
19
19
|
const PLATFORM = Symbol.for("@segmentstream/ontology/platform-workflows");
|
|
20
|
+
const STRUCTURES = Symbol.for("@segmentstream/ontology/workflow-structures");
|
|
20
21
|
const globals = globalThis;
|
|
21
22
|
/** Registers the Workflow SDK for the service's workflow routes; the first registration stays. */
|
|
22
23
|
export function setWorkflowSdk(sdk) {
|
|
@@ -37,6 +38,14 @@ export function setPlatformWorkflows(workflows) {
|
|
|
37
38
|
export function platformWorkflows() {
|
|
38
39
|
return globals[PLATFORM];
|
|
39
40
|
}
|
|
41
|
+
/** Registers what the build read of each workflow's code; the latest registration wins. */
|
|
42
|
+
export function setWorkflowStructures(structures) {
|
|
43
|
+
globals[STRUCTURES] = structures;
|
|
44
|
+
}
|
|
45
|
+
/** The build's workflow structures by workflow id, as the generated plugin registered them. */
|
|
46
|
+
export function workflowStructures() {
|
|
47
|
+
return globals[STRUCTURES];
|
|
48
|
+
}
|
|
40
49
|
/** The Workflow SDK as `installWorkflowRuntime()` registered it; `undefined` before. */
|
|
41
50
|
export function workflowSdk() {
|
|
42
51
|
return globals[SDK];
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type * as TS from "typescript";
|
|
2
|
+
import { type WorkflowStructure, type WorkflowStructures } from "./workflow-structure.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads what each workflow does from its source, at build time: see `./workflow-structure.ts` for
|
|
5
|
+
* the shape. The Workflow SDK's builder finds the workflow and step functions and gives them ids;
|
|
6
|
+
* this reading walks each workflow function's body in TypeScript's syntax tree and follows the
|
|
7
|
+
* calls it can resolve: steps, the SDK's waits, `agentTask`, and helpers declared in the project.
|
|
8
|
+
* It never runs the code. What it cannot resolve becomes an `unknown` node and clears `complete`.
|
|
9
|
+
*/
|
|
10
|
+
/** The parts of the Workflow SDK's build manifest the reading uses, by file relative to the root. */
|
|
11
|
+
export interface WorkflowBuildManifest {
|
|
12
|
+
readonly steps?: Readonly<Record<string, Readonly<Record<string, {
|
|
13
|
+
readonly stepId: string;
|
|
14
|
+
}>>>>;
|
|
15
|
+
readonly workflows?: Readonly<Record<string, Readonly<Record<string, {
|
|
16
|
+
readonly workflowId: string;
|
|
17
|
+
}>>>>;
|
|
18
|
+
}
|
|
19
|
+
/** Reads a file's text, or `undefined` when it cannot be read. */
|
|
20
|
+
export type SourceReader = (path: string) => string | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* The comment directly above a node, without its markers: the last group of comments not
|
|
23
|
+
* separated from the node, or from each other, by a blank line. JSDoc tags are left out.
|
|
24
|
+
*/
|
|
25
|
+
export declare function leadingComment(ts: typeof TS, node: TS.Node, source: TS.SourceFile): string | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* The structure of one workflow function, `name` in `file`. `stepKey` names a step function the
|
|
28
|
+
* way the runs record it; `agentSteps` are the step keys of `agentTask`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function readWorkflowStructure(input: {
|
|
31
|
+
readonly ts: typeof TS;
|
|
32
|
+
readonly root: string;
|
|
33
|
+
readonly file: string;
|
|
34
|
+
readonly name: string;
|
|
35
|
+
readonly stepKey: (file: string, name: string) => string;
|
|
36
|
+
readonly agentSteps?: ReadonlyArray<string>;
|
|
37
|
+
readonly read?: SourceReader;
|
|
38
|
+
}): WorkflowStructure;
|
|
39
|
+
/**
|
|
40
|
+
* The structures of every workflow in a build's manifest, by workflow id, read from the sources
|
|
41
|
+
* under `root`. The step keys come from the manifest; a step it does not list is keyed as the SDK
|
|
42
|
+
* names a project's own steps. A workflow whose reading throws is left out.
|
|
43
|
+
*/
|
|
44
|
+
export declare function readWorkflowStructures(input: {
|
|
45
|
+
readonly ts: typeof TS;
|
|
46
|
+
readonly root: string;
|
|
47
|
+
readonly manifest: WorkflowBuildManifest;
|
|
48
|
+
/** Files of the build that are not the workspace's: the platform's own workflows. */
|
|
49
|
+
readonly skip?: (file: string) => boolean;
|
|
50
|
+
readonly read?: SourceReader;
|
|
51
|
+
readonly onError?: (workflowId: string, error: unknown) => void;
|
|
52
|
+
}): WorkflowStructures;
|