@niwa-labs/rin 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 +122 -2
- package/package.json +38 -4
- package/src/admission.ts +73 -0
- package/src/attention.ts +441 -0
- package/src/character.ts +171 -0
- package/src/charter.ts +130 -0
- package/src/cockpit/actions.ts +132 -0
- package/src/cockpit/index.tsx +13 -0
- package/src/cockpit/page.tsx +370 -0
- package/src/cockpit/view.ts +114 -0
- package/src/composition.ts +90 -0
- package/src/index.ts +16 -0
- package/src/plugin.ts +1071 -0
- package/src/presence.ts +57 -0
- package/src/records.ts +546 -0
- package/test/attention.test.ts +265 -0
- package/test/character.test.ts +42 -0
- package/test/charter.test.ts +84 -0
- package/test/cockpit-actions.test.ts +68 -0
- package/test/cockpit-view.test.ts +68 -0
- package/test/composition.test.ts +157 -0
- package/test/plugin.test.ts +988 -0
- package/test/presence.test.ts +38 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/** What Rin's page shows, as plain data: the page renders it, and the tests read it without React. */
|
|
2
|
+
|
|
3
|
+
import type { AgentView, AttentionView, ConversationMessage, RuleView } from "./actions.ts";
|
|
4
|
+
|
|
5
|
+
/** The items of her attention, as the page groups them. */
|
|
6
|
+
export interface AttentionGroups {
|
|
7
|
+
/** What waits for him, or is Отправляется. */
|
|
8
|
+
readonly waiting: AttentionView[];
|
|
9
|
+
/** What she told him, now or at a contact. */
|
|
10
|
+
readonly told: AttentionView[];
|
|
11
|
+
/** What was dropped with its occasion or Скрыто. */
|
|
12
|
+
readonly closed: AttentionView[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const newestFirst = (a: AttentionView, b: AttentionView) => b.at - a.at;
|
|
16
|
+
|
|
17
|
+
export function attentionGroups(items: readonly AttentionView[]): AttentionGroups {
|
|
18
|
+
const of = (statuses: readonly AttentionView["status"][]) => items.filter((item) => statuses.includes(item.status)).sort(newestFirst);
|
|
19
|
+
return { waiting: of(["held", "sending"]), told: of(["said", "unsure", "brought"]), closed: of(["dropped", "dismissed"]) };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const WHY: Record<NonNullable<AttentionView["why"]>, string> = {
|
|
23
|
+
"next-contact": "При следующем разговоре",
|
|
24
|
+
"quiet-hours": "После тихих часов",
|
|
25
|
+
ceiling: "Достигнут лимит уведомлений",
|
|
26
|
+
undelivered: "Доставка не удалась",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const STATUS: Record<AttentionView["status"], string> = {
|
|
30
|
+
held: "Отложено",
|
|
31
|
+
sending: "Отправляется",
|
|
32
|
+
unsure: "Доставка не подтверждена",
|
|
33
|
+
said: "Доставлено",
|
|
34
|
+
brought: "Обсуждено при разговоре",
|
|
35
|
+
dropped: "Потеряло актуальность",
|
|
36
|
+
dismissed: "Скрыто",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Why an item stands where it does, and how often its occasion came up. */
|
|
40
|
+
export function whyText(item: AttentionView): string {
|
|
41
|
+
const why = item.status === "held" && item.why !== undefined ? WHY[item.why] : STATUS[item.status];
|
|
42
|
+
return item.count !== undefined && item.count > 1 ? `${why} · повторов: ${item.count}` : why;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The tile on the home page. */
|
|
46
|
+
export function tileText(items: readonly AttentionView[]): string {
|
|
47
|
+
const waiting = attentionGroups(items).waiting.length;
|
|
48
|
+
if (waiting === 0) return "Нет отложенных сообщений";
|
|
49
|
+
return waiting === 1 ? "Одно отложенное сообщение" : `Отложенные сообщения: ${waiting}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** One rule as the page lists it. */
|
|
53
|
+
export interface CharterRow {
|
|
54
|
+
readonly rule: string;
|
|
55
|
+
readonly about: string;
|
|
56
|
+
readonly text: string;
|
|
57
|
+
readonly status: RuleView["status"];
|
|
58
|
+
readonly label: string;
|
|
59
|
+
readonly corrections: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const RULE_ORDER: Record<RuleView["status"], number> = { proposed: 0, ruled: 1, declined: 2, retired: 3 };
|
|
63
|
+
const RULE_LABEL: Record<RuleView["status"], string> = { proposed: "Ждёт подтверждения", ruled: "Действует", declined: "Отклонено", retired: "Снято" };
|
|
64
|
+
|
|
65
|
+
/** Her charter: what waits for his word first, then the rules Действует, then the rest. */
|
|
66
|
+
export function charterRows(rules: readonly RuleView[]): CharterRow[] {
|
|
67
|
+
return rules
|
|
68
|
+
.map((rule, index) => ({ rule, index }))
|
|
69
|
+
.sort((a, b) => RULE_ORDER[a.rule.status] - RULE_ORDER[b.rule.status] || a.index - b.index)
|
|
70
|
+
.map(({ rule }) => ({
|
|
71
|
+
rule: rule.rule,
|
|
72
|
+
about: rule.about,
|
|
73
|
+
text: rule.text,
|
|
74
|
+
status: rule.status,
|
|
75
|
+
label: RULE_LABEL[rule.status],
|
|
76
|
+
corrections: rule.corrections.length,
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The agents she leads: every agent of the host but herself. */
|
|
81
|
+
export function ledAgents<T extends Pick<AgentView, "name">>(agents: readonly T[], self = "rin"): T[] {
|
|
82
|
+
return agents.filter((agent) => agent.name !== self);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const DELIVERY: Record<NonNullable<ConversationMessage["delivery"]>, string> = {
|
|
86
|
+
waiting: "Ждёт обработки",
|
|
87
|
+
delivered: "Доставлено Рин",
|
|
88
|
+
unsent: "Не доставлено",
|
|
89
|
+
"possibly-sent": "Доставка не подтверждена",
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** One line of the conversation: who spoke, what, and for what he wrote, where it stands. */
|
|
93
|
+
export interface ConversationRow {
|
|
94
|
+
readonly turn?: string;
|
|
95
|
+
readonly id: string;
|
|
96
|
+
readonly who: string;
|
|
97
|
+
readonly text: string;
|
|
98
|
+
readonly at: number;
|
|
99
|
+
readonly state?: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The conversation, oldest first as it was written. */
|
|
103
|
+
export function conversationRows(messages: readonly ConversationMessage[]): ConversationRow[] {
|
|
104
|
+
return [...messages]
|
|
105
|
+
.sort((a, b) => a.at - b.at)
|
|
106
|
+
.map((message) => ({
|
|
107
|
+
id: message.id,
|
|
108
|
+
...(message.turn === undefined ? {} : { turn: message.turn }),
|
|
109
|
+
who: message.from === "owner" ? "Вы" : "Рин",
|
|
110
|
+
text: message.text,
|
|
111
|
+
at: message.at,
|
|
112
|
+
...(message.delivery === undefined ? {} : { state: DELIVERY[message.delivery] }),
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rin's composition: the plugins a host installs to have her, in the order it installs them.
|
|
3
|
+
*
|
|
4
|
+
* Rin is built on the agents module and uses memory, commitments and delegation as her tools. What she needs of
|
|
5
|
+
* the host besides is not hers to install: addressing (the binding by which her sessions speak as `agent:rin`),
|
|
6
|
+
* the timers, the workflow module, and the messages module. If Telegram is enabled, she also needs `telegram.send`. A host that lacks the timers, the workflow module or the messages
|
|
7
|
+
* refuses to serve and says what to install; the binding is an option she cannot do without.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const base = defaultComposition({ home, harnesses, agents: { default: RIN_AGENT, roles } });
|
|
11
|
+
* export default [...base, telegram({ ... }), ...rin({ bind: addressingOf(base), role: "assistant", telegram: true })];
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { commitments } from "@niwa-labs/commitments";
|
|
16
|
+
import { delegate } from "@niwa-labs/delegate";
|
|
17
|
+
import type { DelegateOptions } from "@niwa-labs/delegate";
|
|
18
|
+
import { memory } from "@niwa-labs/memory";
|
|
19
|
+
import type { MemoryLimits } from "@niwa-labs/memory";
|
|
20
|
+
import { memoryContext } from "@niwa-labs/memory/records";
|
|
21
|
+
import type { CallerBinding, Plugin } from "@niwa-labs/plugin";
|
|
22
|
+
|
|
23
|
+
import { RIN_AGENT } from "./character.ts";
|
|
24
|
+
import { rinPlugin } from "./plugin.ts";
|
|
25
|
+
import type { RinOptions } from "./plugin.ts";
|
|
26
|
+
|
|
27
|
+
/** What Rin's composition is made with: her own options, and those of her tools. */
|
|
28
|
+
export interface RinCompositionOptions extends RinOptions {
|
|
29
|
+
/** How much her memory holds and shows. */
|
|
30
|
+
readonly memory?: Partial<MemoryLimits>;
|
|
31
|
+
/** How long after its deadline a promise not kept is missed. */
|
|
32
|
+
readonly commitmentGraceMs?: number;
|
|
33
|
+
/** The judges of areas she answers for, and how long each has before the question goes on to the owner. */
|
|
34
|
+
readonly judges?: DelegateOptions["judges"];
|
|
35
|
+
readonly judgeWaitMs?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** What Rin needs of the host besides the plugins of her composition. */
|
|
39
|
+
export const RIN_NEEDS = {
|
|
40
|
+
/** The binding by which her sessions speak as `agent:rin`: the one the messages module is given. */
|
|
41
|
+
addressing: "the caller binding of the sessions and agents modules",
|
|
42
|
+
/** The timers: the end of the owner's quiet hours, and the deadlines of promises. */
|
|
43
|
+
timer: "@niwa-labs/timer",
|
|
44
|
+
/** The workflow module: the digests of attached sessions, and the runs she starts. */
|
|
45
|
+
workflow: "@niwa-labs/workflow",
|
|
46
|
+
/** Message delivery: the owner's messages to her, and an action that tells him something now. */
|
|
47
|
+
delivery: "@niwa-labs/messages",
|
|
48
|
+
telegram: "telegram.send",
|
|
49
|
+
} as const;
|
|
50
|
+
|
|
51
|
+
/** Rin's plugins, to install after the agents module: her memory, commitments, delegation, and Rin herself. */
|
|
52
|
+
export function rin(options: RinCompositionOptions): Plugin[] {
|
|
53
|
+
if (typeof options.bind !== "function") {
|
|
54
|
+
throw new TypeError("rin: she needs the caller binding of the host (addressingOf), so that her sessions speak as agent:rin");
|
|
55
|
+
}
|
|
56
|
+
const { bind } = options;
|
|
57
|
+
return [
|
|
58
|
+
memory({ bind, ...(options.memory === undefined ? {} : { limits: options.memory }) }),
|
|
59
|
+
commitments({ messages: { bind }, ...(options.commitmentGraceMs === undefined ? {} : { graceMs: options.commitmentGraceMs }) }),
|
|
60
|
+
delegate({
|
|
61
|
+
agent: RIN_AGENT,
|
|
62
|
+
messages: { bind },
|
|
63
|
+
context: [memoryContext.name],
|
|
64
|
+
...(options.judges === undefined ? {} : { judges: options.judges }),
|
|
65
|
+
...(options.judgeWaitMs === undefined ? {} : { judgeWaitMs: options.judgeWaitMs }),
|
|
66
|
+
}),
|
|
67
|
+
rinPlugin(options),
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A plugin that hands out the binding of its callers. */
|
|
72
|
+
type Binding = Plugin & { bind(fallback?: CallerBinding): CallerBinding };
|
|
73
|
+
|
|
74
|
+
function bindingPlugin(plugins: readonly Plugin[], name: string): Binding | undefined {
|
|
75
|
+
const found = plugins.find((one) => String(one.name) === name);
|
|
76
|
+
return found !== undefined && typeof (found as Partial<Binding>).bind === "function" ? (found as Binding) : undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The addressing of a host's plugins: the binding of the sessions module wrapped by the agents module's, so a
|
|
81
|
+
* session the agents module started for Rin speaks as `agent:rin` and the owner's client as the owner.
|
|
82
|
+
*/
|
|
83
|
+
export function addressingOf(plugins: readonly Plugin[]): CallerBinding {
|
|
84
|
+
const sessions = bindingPlugin(plugins, "sessions");
|
|
85
|
+
const agents = bindingPlugin(plugins, "agents");
|
|
86
|
+
if (sessions === undefined || agents === undefined) {
|
|
87
|
+
throw new TypeError(`rin: the host has no ${sessions === undefined ? "sessions" : "agents"} module to address her sessions by; install it before her`);
|
|
88
|
+
}
|
|
89
|
+
return agents.bind(sessions.bind());
|
|
90
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rin: an agent living inside niwa, between the owner's main assistant and a companion. Her character, her
|
|
3
|
+
* judgement about what reaches the owner and when, and what his corrections teach her live here; memory,
|
|
4
|
+
* commitments, delegation and the mechanics of an agent come from the packages she is composed of.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export { RIN_NEEDS, addressingOf, rin } from "./composition.ts";
|
|
8
|
+
export type { RinCompositionOptions } from "./composition.ts";
|
|
9
|
+
export { RinRefusal, rinPlugin } from "./plugin.ts";
|
|
10
|
+
export type { RinOptions } from "./plugin.ts";
|
|
11
|
+
export { CORE, HANDS, RIN_AGENT, coreDigest, rinDefinition, workText } from "./character.ts";
|
|
12
|
+
export type { Core, RinDefinitionOptions } from "./character.ts";
|
|
13
|
+
export { DEFAULT_CEILING, DEFAULT_QUIET, inQuietHours, quietEnds } from "./attention.ts";
|
|
14
|
+
export type { Ceiling, QuietHours } from "./attention.ts";
|
|
15
|
+
export { DEFAULT_RULE_AFTER } from "./charter.ts";
|
|
16
|
+
export * from "./records.ts";
|