@niwa-labs/messages 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +4 -2
- package/package.json +36 -4
- package/src/actions.ts +48 -0
- package/src/cockpit/index.tsx +182 -0
- package/src/envelope.ts +167 -0
- package/src/index.ts +237 -0
- package/src/records-entry.ts +36 -0
- package/src/records.ts +141 -0
- package/src/render.ts +63 -0
- package/src/text.ts +10 -0
- package/test/cockpit-text.test.ts +22 -0
- package/test/deliver.test.ts +296 -0
- package/test/json-values.test.ts +55 -0
- package/test/list.test.ts +36 -0
- package/test/sender.test.ts +38 -0
- package/tsconfig.json +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Niwa Labs contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @niwa-labs/messages
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Messages between agents, sessions and the owner: an envelope that says who sent it, who it is for and what it carries, sent with `message.send` and recorded in the journal. A message to a session is unfolded into text and delivered through `session.send`.
|
|
4
|
+
|
|
5
|
+
The root entry holds the plugin and its factories; `@niwa-labs/messages/records` holds the envelope schemas, the records, the typed action references and the pure helpers that other plugins may import.
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@niwa-labs/messages",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.ts",
|
|
8
|
+
"./records": "./src/records-entry.ts",
|
|
9
|
+
"./cockpit": "./src/cockpit/index.tsx"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@niwa-labs/cockpit-sdk": "0.0.0",
|
|
13
|
+
"@niwa-labs/core": "0.0.0",
|
|
14
|
+
"@niwa-labs/plugin": "0.0.0",
|
|
15
|
+
"@niwa-labs/sessions": "0.0.0",
|
|
16
|
+
"zod": "^4.6.7",
|
|
17
|
+
"@niwa-labs/ui-kit": "0.0.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@niwa-labs/testing": "0.0.0",
|
|
21
|
+
"@types/react": "^19.3.0",
|
|
22
|
+
"@types/react-dom": "^19.3.0",
|
|
23
|
+
"react": "^19.3.1",
|
|
24
|
+
"react-dom": "^19.3.1"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"react": "^19.3.1",
|
|
28
|
+
"react-dom": "^19.3.1"
|
|
29
|
+
},
|
|
30
|
+
"peerDependenciesMeta": {
|
|
31
|
+
"react": {
|
|
32
|
+
"optional": true
|
|
33
|
+
},
|
|
34
|
+
"react-dom": {
|
|
35
|
+
"optional": true
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/actions.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The actions of the messages module, as typed references: name, description and the two schemas.
|
|
3
|
+
*
|
|
4
|
+
* A caller imports these to call `message.send` or `message.receive` with the input and the output
|
|
5
|
+
* checked by the type checker, on the host and on a client. The module's factories build the real
|
|
6
|
+
* actions from the same references.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
import { Address, actionRef, getRef, idSchema, listRef } from "@niwa-labs/core";
|
|
12
|
+
|
|
13
|
+
import { Message, MessageInput } from "./envelope.ts";
|
|
14
|
+
|
|
15
|
+
/** Send a message to an address. */
|
|
16
|
+
export const messageSendRef = actionRef("message.send", {
|
|
17
|
+
description:
|
|
18
|
+
"Send a message to an address. The envelope carries who sent it, who it is for and what it carries.",
|
|
19
|
+
input: MessageInput,
|
|
20
|
+
output: Message,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/** Record a message that came in from outside. */
|
|
24
|
+
export const messageReceiveRef = actionRef("message.receive", {
|
|
25
|
+
description:
|
|
26
|
+
"Record a message that came in from outside: who sent it, who it is for and what it carries.",
|
|
27
|
+
input: MessageInput,
|
|
28
|
+
output: Message,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/** What a message is, as a list says it: the tag of its body. */
|
|
32
|
+
export const MessageKind = z.enum(["assignment", "update", "question", "answer", "result", "failure"]);
|
|
33
|
+
|
|
34
|
+
/** List the messages: id, description, who and what at a glance. */
|
|
35
|
+
export const messageListRef = listRef("message.list", {
|
|
36
|
+
kind: "message",
|
|
37
|
+
noun: "message",
|
|
38
|
+
detail: "The description is the kind of the message and how its text begins. Messages sent and received are both here, oldest first.",
|
|
39
|
+
filter: { task: idSchema("task").optional() },
|
|
40
|
+
item: { from: Address, to: Address, kind: MessageKind, task: idSchema("task").optional() },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** Get one message by its id, or any text that names one id. */
|
|
44
|
+
export const messageGetRef = getRef("message.get", {
|
|
45
|
+
kind: "message",
|
|
46
|
+
noun: "message",
|
|
47
|
+
entity: Message,
|
|
48
|
+
});
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { definePlugin, EntityLink, PageCommands, PageLink, useCall, type PageProps } from "@niwa-labs/cockpit-sdk";
|
|
2
|
+
import { Badge, CopyValue, DataValue, PageSection, SchemaView } from "@niwa-labs/ui-kit";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
|
|
6
|
+
import { resultValue } from "../text.ts";
|
|
7
|
+
|
|
8
|
+
const kind = z.enum(["assignment", "update", "question", "answer", "result", "failure"]);
|
|
9
|
+
const labels: Record<z.infer<typeof kind>, string> = {
|
|
10
|
+
assignment: "Поручение",
|
|
11
|
+
update: "Сообщение",
|
|
12
|
+
question: "Вопрос",
|
|
13
|
+
answer: "Ответ",
|
|
14
|
+
result: "Результат",
|
|
15
|
+
failure: "Ошибка",
|
|
16
|
+
};
|
|
17
|
+
const address = z.object({ kind: z.string(), id: z.string().optional(), harness: z.string().optional() });
|
|
18
|
+
type Address = z.infer<typeof address>;
|
|
19
|
+
const list = {
|
|
20
|
+
name: "message.list",
|
|
21
|
+
input: z.object({}),
|
|
22
|
+
output: z.object({
|
|
23
|
+
items: z.array(z.object({
|
|
24
|
+
id: z.string(),
|
|
25
|
+
description: z.string(),
|
|
26
|
+
from: address,
|
|
27
|
+
to: address,
|
|
28
|
+
kind,
|
|
29
|
+
task: z.string().optional(),
|
|
30
|
+
})),
|
|
31
|
+
}),
|
|
32
|
+
};
|
|
33
|
+
const get = {
|
|
34
|
+
name: "message.get",
|
|
35
|
+
input: z.object({ id: z.string() }),
|
|
36
|
+
output: z.object({
|
|
37
|
+
id: z.string(),
|
|
38
|
+
from: address,
|
|
39
|
+
to: address,
|
|
40
|
+
body: z.object({
|
|
41
|
+
type: kind,
|
|
42
|
+
text: z.string(),
|
|
43
|
+
kind: z.string().optional(),
|
|
44
|
+
question: z.string().optional(),
|
|
45
|
+
reason: z.string().optional(),
|
|
46
|
+
result: z.unknown().optional(),
|
|
47
|
+
data: z.unknown().optional(),
|
|
48
|
+
}),
|
|
49
|
+
task: z.string().optional(),
|
|
50
|
+
}),
|
|
51
|
+
};
|
|
52
|
+
const refreshOn = { types: ["message.*"] };
|
|
53
|
+
|
|
54
|
+
function who(value: Address): string {
|
|
55
|
+
if (value.kind === "owner") return "Владелец";
|
|
56
|
+
if (value.kind === "session") return [value.harness, value.id].filter(Boolean).join(" · ");
|
|
57
|
+
return value.id ?? value.kind;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function Participant({ value }: { value: Address }) {
|
|
61
|
+
if (value.kind === "task" && value.id) return <EntityLink entity={{ kind: "task", id: value.id }} short />;
|
|
62
|
+
if (value.kind === "session" && value.id) return <EntityLink entity={value.id} label={who(value)} />;
|
|
63
|
+
return <>{who(value)}</>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function Messages() {
|
|
67
|
+
const inbox = useCall(list, {}, { refreshOn });
|
|
68
|
+
const [filter, setFilter] = useState("");
|
|
69
|
+
const query = filter.trim().toLowerCase();
|
|
70
|
+
const rows = inbox.data?.items.toReversed().filter((message) =>
|
|
71
|
+
[message.description, labels[message.kind], message.id, who(message.from), who(message.to), message.task]
|
|
72
|
+
.join(" ").toLowerCase().includes(query),
|
|
73
|
+
);
|
|
74
|
+
return (
|
|
75
|
+
<>
|
|
76
|
+
<label className="flex w-full max-w-lg flex-col gap-1 text-sm">
|
|
77
|
+
Поиск по тексту, участнику или задаче
|
|
78
|
+
<input
|
|
79
|
+
type="search"
|
|
80
|
+
className="w-full rounded-md border bg-background px-3 py-2"
|
|
81
|
+
value={filter}
|
|
82
|
+
onChange={(event) => setFilter(event.target.value)}
|
|
83
|
+
/>
|
|
84
|
+
</label>
|
|
85
|
+
{inbox.isError ? (
|
|
86
|
+
<p role="alert">Не удалось загрузить сообщения.</p>
|
|
87
|
+
) : rows === undefined ? (
|
|
88
|
+
<p role="status">Загружаю сообщения…</p>
|
|
89
|
+
) : rows.length === 0 ? (
|
|
90
|
+
<p>{query ? "По этому запросу ничего не найдено." : "Сообщений пока нет."}</p>
|
|
91
|
+
) : (
|
|
92
|
+
<ul className="divide-y">
|
|
93
|
+
{rows.map((message) => {
|
|
94
|
+
const prefix = message.kind + ":";
|
|
95
|
+
const subject = message.description.startsWith(prefix) ? message.description.slice(prefix.length).trim() : message.description;
|
|
96
|
+
// An older host can cut a result that begins with a JSON object before the closing brace.
|
|
97
|
+
const truncated = message.kind === "result" && /^\{\s*"[^"]+"\s*:/.test(subject) && subject.endsWith("…");
|
|
98
|
+
return (
|
|
99
|
+
<li key={message.id} className="min-w-0 flex flex-col gap-1 py-2">
|
|
100
|
+
<PageLink to={"/messages/" + encodeURIComponent(message.id)} className="text-base font-semibold wrap-anywhere hover:underline">
|
|
101
|
+
{(truncated ? "" : subject) || labels[message.kind]}
|
|
102
|
+
</PageLink>
|
|
103
|
+
<div className="flex min-w-0 flex-wrap items-center gap-2 text-sm text-muted-foreground wrap-anywhere">
|
|
104
|
+
<Badge variant="secondary">{labels[message.kind]}</Badge>
|
|
105
|
+
<span>{who(message.from)} → {who(message.to)}</span>
|
|
106
|
+
{message.task && <EntityLink entity={{ kind: "task", id: message.task }} label="Связанная задача" />}
|
|
107
|
+
</div>
|
|
108
|
+
</li>
|
|
109
|
+
);
|
|
110
|
+
})}
|
|
111
|
+
</ul>
|
|
112
|
+
)}
|
|
113
|
+
</>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function Message({ params }: PageProps<"/messages/$id">) {
|
|
118
|
+
const message = useCall(get, { id: params.id }, { refreshOn });
|
|
119
|
+
if (message.isPending) return <p role="status">Загружаю сообщение…</p>;
|
|
120
|
+
if (message.isError) return <p role="alert">Не удалось загрузить сообщение.</p>;
|
|
121
|
+
const { data } = message;
|
|
122
|
+
const { body } = data;
|
|
123
|
+
const result = body.type === "result" ? resultValue(body.text) : undefined;
|
|
124
|
+
const largeResult = body.type === "result" && body.text.startsWith("The result is ") &&
|
|
125
|
+
body.text.includes("too large to carry in a message: it is kept as an artifact") && data.task !== undefined;
|
|
126
|
+
const attachment = body.type === "result" && (
|
|
127
|
+
body.data === body.text || (result !== undefined && JSON.stringify(body.data) === JSON.stringify(result))
|
|
128
|
+
) ? undefined : body.data;
|
|
129
|
+
return (
|
|
130
|
+
<article className="desk-page">
|
|
131
|
+
<header className="min-w-0 flex flex-col gap-2">
|
|
132
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
133
|
+
<h2 className="text-2xl font-semibold">{labels[body.type]}</h2>
|
|
134
|
+
<CopyValue value={data.id} name="Скопировать номер сообщения" />
|
|
135
|
+
</div>
|
|
136
|
+
{data.from.kind !== "owner" && (
|
|
137
|
+
<PageCommands commands={[{ action: "message.send", label: "Ответить", preset: {
|
|
138
|
+
to: data.from,
|
|
139
|
+
...(data.task && { task: data.task }),
|
|
140
|
+
body: { type: "answer", ...(body.question && { question: body.question }) },
|
|
141
|
+
} }]} />
|
|
142
|
+
)}
|
|
143
|
+
</header>
|
|
144
|
+
{largeResult ? (
|
|
145
|
+
<p>Полный результат доступен в карточке связанной задачи.</p>
|
|
146
|
+
) : result === undefined ? (
|
|
147
|
+
<p className="whitespace-pre-wrap wrap-anywhere">{body.text}</p>
|
|
148
|
+
) : <PageSection title="Итог"><DataValue value={result} /></PageSection>}
|
|
149
|
+
<PageSection title="О сообщении">
|
|
150
|
+
<dl className="nw-fields">
|
|
151
|
+
<div><dt>Отправитель</dt><dd><Participant value={data.from} /></dd></div>
|
|
152
|
+
<div><dt>Получатель</dt><dd><Participant value={data.to} /></dd></div>
|
|
153
|
+
{data.task && <div><dt>Задача</dt><dd><EntityLink entity={{ kind: "task", id: data.task }} /></dd></div>}
|
|
154
|
+
{body.kind && <div><dt>Вид работы</dt><dd>{body.kind}</dd></div>}
|
|
155
|
+
{body.reason && body.reason !== body.text && <div><dt>Причина</dt><dd><DataValue value={body.reason} /></dd></div>}
|
|
156
|
+
</dl>
|
|
157
|
+
</PageSection>
|
|
158
|
+
{body.result !== undefined && <PageSection title="Ожидаемый результат"><SchemaView schema={body.result} /></PageSection>}
|
|
159
|
+
{attachment !== undefined && <PageSection title="Приложение"><DataValue value={attachment} /></PageSection>}
|
|
160
|
+
</article>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export default definePlugin({
|
|
165
|
+
id: "messages",
|
|
166
|
+
setup(ctx) {
|
|
167
|
+
ctx.pages.add({
|
|
168
|
+
path: "/messages",
|
|
169
|
+
title: "Сообщения",
|
|
170
|
+
nav: { order: 32 },
|
|
171
|
+
commandHeading: true,
|
|
172
|
+
component: Messages,
|
|
173
|
+
commands: [{ action: "message.send", label: "Написать сообщение", preset: { body: { type: "update" } } }],
|
|
174
|
+
});
|
|
175
|
+
ctx.pages.add({ path: "/messages/$id", panel: true, title: "Сообщение", component: Message });
|
|
176
|
+
ctx.entities.register("message", {
|
|
177
|
+
label: "Сообщение",
|
|
178
|
+
prefix: "msg",
|
|
179
|
+
href: (id) => "/messages/" + encodeURIComponent(id),
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
});
|
package/src/envelope.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The envelope: a message, and what a message can carry.
|
|
3
|
+
*
|
|
4
|
+
* A message is an envelope sent to an address. It always says who sent it: `from` is not optional,
|
|
5
|
+
* because a message whose sender is unknown cannot be reasoned about.
|
|
6
|
+
*
|
|
7
|
+
* The body is a discriminated union of six kinds: assignment, update, question, answer, result
|
|
8
|
+
* and failure. The kind is the tag, so a reader knows what it holds without guessing.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
import { Address, JsonSchemaValue, JsonValueSchema, idSchema } from "@niwa-labs/core";
|
|
14
|
+
import type { Id } from "@niwa-labs/core";
|
|
15
|
+
|
|
16
|
+
/** The id of a message: `msg-3fa9c2`. Niwa issues it when the message is written; the sender never chooses it. */
|
|
17
|
+
export const MessageId = idSchema("message");
|
|
18
|
+
export type MessageId = Id<"message">;
|
|
19
|
+
|
|
20
|
+
/** The id of the task a message belongs to: the tasks module's own id, so a message needs no import of it. */
|
|
21
|
+
const TaskRef = idSchema("task");
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The shape the result of an assignment must have: a JSON Schema of an object.
|
|
25
|
+
*
|
|
26
|
+
* It is declared by whoever asked for the work and checked when the task is finished (tasks module);
|
|
27
|
+
* a message only carries it, so the one who is to do the work sees it.
|
|
28
|
+
*/
|
|
29
|
+
export const ResultSchema = JsonSchemaValue;
|
|
30
|
+
export type ResultSchema = z.infer<typeof ResultSchema>;
|
|
31
|
+
|
|
32
|
+
/** The text a body carries. */
|
|
33
|
+
const Text = z.string().min(1);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* An assignment: work given to an address.
|
|
37
|
+
*
|
|
38
|
+
* What to do is free text on purpose: the kinds of work are an open label in the core, not a
|
|
39
|
+
* closed list.
|
|
40
|
+
*/
|
|
41
|
+
export const Assignment = z.strictObject({
|
|
42
|
+
type: z.literal("assignment"),
|
|
43
|
+
/** What to do. */
|
|
44
|
+
text: Text,
|
|
45
|
+
/** An open label for the sort of work this is. */
|
|
46
|
+
kind: z.string().optional(),
|
|
47
|
+
/** The shape the result must have, when the sender declared one. */
|
|
48
|
+
result: ResultSchema.optional(),
|
|
49
|
+
/** Anything the sender wants to carry along. */
|
|
50
|
+
data: JsonValueSchema.optional(),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/** An update: what has happened so far. */
|
|
54
|
+
export const Update = z.strictObject({
|
|
55
|
+
type: z.literal("update"),
|
|
56
|
+
text: Text,
|
|
57
|
+
/** Anything the sender wants to carry along. */
|
|
58
|
+
data: JsonValueSchema.optional(),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/** A question: something the receiver is asked. */
|
|
62
|
+
export const Question = z.strictObject({
|
|
63
|
+
type: z.literal("question"),
|
|
64
|
+
text: Text,
|
|
65
|
+
/** The name of the question, when it has one. */
|
|
66
|
+
question: z.string().optional(),
|
|
67
|
+
/** Anything the sender wants to carry along. */
|
|
68
|
+
data: JsonValueSchema.optional(),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/** An answer: the answer to a question. */
|
|
72
|
+
export const Answer = z.strictObject({
|
|
73
|
+
type: z.literal("answer"),
|
|
74
|
+
text: Text,
|
|
75
|
+
/** The question this answers, when it answers one. */
|
|
76
|
+
question: z.string().optional(),
|
|
77
|
+
/** Anything the sender wants to carry along. */
|
|
78
|
+
data: JsonValueSchema.optional(),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
/** A result: the outcome of some work. */
|
|
82
|
+
export const Result = z.strictObject({
|
|
83
|
+
type: z.literal("result"),
|
|
84
|
+
text: Text,
|
|
85
|
+
/** Anything the sender wants to carry along. */
|
|
86
|
+
data: JsonValueSchema.optional(),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/** A failure: the work did not succeed. */
|
|
90
|
+
export const Failure = z.strictObject({
|
|
91
|
+
type: z.literal("failure"),
|
|
92
|
+
text: Text,
|
|
93
|
+
/** What went wrong, when it is worth saying separately from the text. */
|
|
94
|
+
reason: z.string().optional(),
|
|
95
|
+
/** Anything the sender wants to carry along. */
|
|
96
|
+
data: JsonValueSchema.optional(),
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
/** The six kinds of body a message can carry. */
|
|
100
|
+
export const MessageBody = z.discriminatedUnion("type", [
|
|
101
|
+
Assignment,
|
|
102
|
+
Update,
|
|
103
|
+
Question,
|
|
104
|
+
Answer,
|
|
105
|
+
Result,
|
|
106
|
+
Failure,
|
|
107
|
+
]);
|
|
108
|
+
|
|
109
|
+
/** One body. */
|
|
110
|
+
export type MessageBody = z.infer<typeof MessageBody>;
|
|
111
|
+
|
|
112
|
+
/** The kind of a body: what a message carries. */
|
|
113
|
+
export type MessageBodyType = MessageBody["type"];
|
|
114
|
+
|
|
115
|
+
/** Where the owner expects a reply to this agent turn. */
|
|
116
|
+
export const ReplyRoute = z.discriminatedUnion("channel", [
|
|
117
|
+
z.strictObject({ channel: z.literal("cockpit"), conversation: z.string().min(1) }),
|
|
118
|
+
z.strictObject({
|
|
119
|
+
channel: z.literal("telegram"),
|
|
120
|
+
bot: z.string().min(1),
|
|
121
|
+
chat: z.number().int().positive(),
|
|
122
|
+
message: z.number().int().positive(),
|
|
123
|
+
}),
|
|
124
|
+
]);
|
|
125
|
+
export type ReplyRoute = z.infer<typeof ReplyRoute>;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A message: an envelope sent to an address.
|
|
129
|
+
*
|
|
130
|
+
* `from` is always present. `task` is the task the message belongs to, when there is one.
|
|
131
|
+
*/
|
|
132
|
+
export const Message = z.strictObject({
|
|
133
|
+
id: MessageId,
|
|
134
|
+
/** Who sent it. Always present. */
|
|
135
|
+
from: Address,
|
|
136
|
+
/** Who it is sent to. */
|
|
137
|
+
to: Address,
|
|
138
|
+
/** The task this message belongs to, when it belongs to one. */
|
|
139
|
+
task: TaskRef.optional(),
|
|
140
|
+
replyTo: ReplyRoute.optional(),
|
|
141
|
+
/** What the message carries. */
|
|
142
|
+
body: MessageBody,
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
/** One message. */
|
|
146
|
+
export type Message = z.infer<typeof Message>;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The input of the action that sends a message: the envelope without its id.
|
|
150
|
+
*
|
|
151
|
+
* The id is issued by niwa when the message is written, never by its caller: the message is one
|
|
152
|
+
* fact, and the action that writes it is the one that names it.
|
|
153
|
+
*/
|
|
154
|
+
export const MessageInput = z.strictObject({
|
|
155
|
+
/** Who sent it. Optional here: the caller binding may say who the sender is. */
|
|
156
|
+
from: Address.optional(),
|
|
157
|
+
/** Who it is sent to. */
|
|
158
|
+
to: Address,
|
|
159
|
+
/** The task this message belongs to, when it belongs to one. */
|
|
160
|
+
task: TaskRef.optional(),
|
|
161
|
+
replyTo: ReplyRoute.optional(),
|
|
162
|
+
/** What the message carries. */
|
|
163
|
+
body: MessageBody,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
/** The input of the action that sends a message. */
|
|
167
|
+
export type MessageInput = z.infer<typeof MessageInput>;
|