@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,157 @@
|
|
|
1
|
+
// Rin's composition: her plugins install after the agents module, and a host that lacks what she needs refuses
|
|
2
|
+
// to serve and says what to install.
|
|
3
|
+
|
|
4
|
+
import { afterEach, expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { agents } from "@niwa-labs/agent";
|
|
7
|
+
import { agentAttach, agentBriefing, agentGet, agentTell } from "@niwa-labs/agent/records";
|
|
8
|
+
import { actionName, agent, grant, principalOf } from "@niwa-labs/core";
|
|
9
|
+
import type { Address, Caller } from "@niwa-labs/core";
|
|
10
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
11
|
+
import type { Plugin } from "@niwa-labs/plugin";
|
|
12
|
+
import { memory } from "@niwa-labs/memory";
|
|
13
|
+
import { requests } from "@niwa-labs/requests";
|
|
14
|
+
import { FAKE_HARNESS, connect, connectAsOwner, createNiwa, eventually, fakeHarness, messages, sessions, tasks, tempHome, timers, workflow } from "@niwa-labs/testing";
|
|
15
|
+
import { RunFinished, RunStarted } from "@niwa-labs/workflow/records";
|
|
16
|
+
|
|
17
|
+
import { RIN_AGENT, addressingOf, rin, rinAttend, rinAttention, rinConversation, rinNotice, rinPlugin } from "../src/index.ts";
|
|
18
|
+
|
|
19
|
+
const cleanups: Array<() => Promise<void>> = [];
|
|
20
|
+
afterEach(async () => {
|
|
21
|
+
for (const cleanup of cleanups.splice(0).reverse()) await cleanup();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("her plugins are her tools and herself, to install after the agents module", () => {
|
|
25
|
+
const bind = addressingOf([sessions({ harnesses: [] }), agents()]);
|
|
26
|
+
expect(rin({ bind, harness: "claude" }).map((one) => String(one.name))).toEqual(["memory", "commitments", "delegate", "rin"]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("she cannot be composed without the addressing of the host", () => {
|
|
30
|
+
expect(() => addressingOf([agents()])).toThrow("no sessions module");
|
|
31
|
+
expect(() => addressingOf([sessions({ harnesses: [] })])).toThrow("no agents module");
|
|
32
|
+
expect(() => rin({ harness: "claude" } as never)).toThrow("caller binding");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("on a whole host she is the default agent, and what she holds is in her briefing and brought up at an attach", async () => {
|
|
36
|
+
const home = await tempHome();
|
|
37
|
+
cleanups.push(() => home.cleanup());
|
|
38
|
+
const fake = fakeHarness();
|
|
39
|
+
const sessionsModule = sessions({ harnesses: [fake.binding] });
|
|
40
|
+
const agentsModule = agents({ default: RIN_AGENT });
|
|
41
|
+
const addressing = addressingOf([sessionsModule, agentsModule]);
|
|
42
|
+
// A client with the token of a session "rin" stands for her main session here.
|
|
43
|
+
const bind = (caller: Caller): Address | undefined =>
|
|
44
|
+
caller.kind === "client" && caller.principal?.kind === "session" && String(caller.principal.id) === RIN_AGENT ? agent(RIN_AGENT) : addressing(caller);
|
|
45
|
+
const niwa = createNiwa({ home: home.path });
|
|
46
|
+
cleanups.push(() => niwa.close());
|
|
47
|
+
niwa.use(messages({ bind }), requests({ bind }), tasks({ messages: { bind } }), timers([]), sessionsModule, agentsModule, workflow(), ...rin({ bind, harness: FAKE_HARNESS }));
|
|
48
|
+
await niwa.serve();
|
|
49
|
+
const owner = await connectAsOwner(home.path);
|
|
50
|
+
cleanups.push(() => owner.close());
|
|
51
|
+
const rinSession = await connect({ home: home.path, id: "session:rin", token: niwa.issueToken(principalOf("session", RIN_AGENT), { grant: grant(["rin.*"]) }) });
|
|
52
|
+
cleanups.push(() => rinSession.close());
|
|
53
|
+
|
|
54
|
+
const got = await owner.call(agentGet, { agent: RIN_AGENT });
|
|
55
|
+
expect(got.definition.tools).toBe("niwa-only");
|
|
56
|
+
expect(got.definition.hands).toContain("rin.attend");
|
|
57
|
+
|
|
58
|
+
const noticed = await owner.call(rinNotice, { key: "task:t-1", kind: "task", text: "The review task is done" });
|
|
59
|
+
const held = await rinSession.call(rinAttend, { subject: { kind: "occasion", occasion: noticed.occasion! }, decision: "next-contact", text: "The review is done: two comments" });
|
|
60
|
+
expect(held.outcome).toBe("held");
|
|
61
|
+
expect((await owner.call(agentBriefing, { agent: RIN_AGENT })).text).toContain("What Rin holds for you:\n- The review is done: two comments");
|
|
62
|
+
|
|
63
|
+
const attached = await owner.call(agentAttach, { harness: FAKE_HARNESS, native: "s1", cwd: "/work" });
|
|
64
|
+
expect(attached.agent).toBe(RIN_AGENT);
|
|
65
|
+
expect(attached.briefing).toContain("- The review is done: two comments");
|
|
66
|
+
await eventually(async () => (await owner.call(rinAttention, { status: "brought" })).items.length === 1);
|
|
67
|
+
expect((await owner.call(agentBriefing, { agent: RIN_AGENT })).text).toContain("- The review is done: two comments");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/** What Rin needs of a host, each held by a plugin of its own: the agents, the timers, the workflow module, the messages. */
|
|
71
|
+
function needed(options: { telegram?: boolean } = {}): Plugin[] {
|
|
72
|
+
const bind = () => undefined;
|
|
73
|
+
return [
|
|
74
|
+
agents(),
|
|
75
|
+
sessions({ harnesses: [] }),
|
|
76
|
+
timers([]),
|
|
77
|
+
plugin("workflow", (api) => api.declare(RunStarted, RunFinished)),
|
|
78
|
+
messages({ bind }),
|
|
79
|
+
requests({ bind }),
|
|
80
|
+
memory({ bind }),
|
|
81
|
+
rinPlugin({ bind, harness: "claude", ...options }),
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
test("a host with what she needs serves her", async () => {
|
|
86
|
+
const home = await tempHome();
|
|
87
|
+
cleanups.push(() => home.cleanup());
|
|
88
|
+
const niwa = createNiwa({ home: home.path });
|
|
89
|
+
cleanups.push(() => niwa.close());
|
|
90
|
+
niwa.use(...needed());
|
|
91
|
+
await niwa.serve();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
for (const [missing, what] of [
|
|
95
|
+
["timers", /plugin "rin" offers "timer"/],
|
|
96
|
+
["workflow", /"workflow" plugin .*not installed/],
|
|
97
|
+
["messages", /"message\.sent".*no installed plugin declares it/],
|
|
98
|
+
["agents", /"agents" plugin .*not installed/],
|
|
99
|
+
["requests", /plugin "rin" .*request\.ask/],
|
|
100
|
+
["memory", /plugin "rin" .*memory\.consolidate/],
|
|
101
|
+
] as const) {
|
|
102
|
+
test(`a host without ${missing} refuses to serve her and says so`, async () => {
|
|
103
|
+
const home = await tempHome();
|
|
104
|
+
cleanups.push(() => home.cleanup());
|
|
105
|
+
const niwa = createNiwa({ home: home.path });
|
|
106
|
+
cleanups.push(() => niwa.close());
|
|
107
|
+
niwa.use(...needed().filter((one) => String(one.name) !== missing));
|
|
108
|
+
await expect(niwa.serve()).rejects.toThrow(what);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
test("Telegram delivery requires telegram.send when requested", async () => {
|
|
114
|
+
const home = await tempHome();
|
|
115
|
+
cleanups.push(() => home.cleanup());
|
|
116
|
+
const niwa = createNiwa({ home: home.path });
|
|
117
|
+
cleanups.push(() => niwa.close());
|
|
118
|
+
niwa.use(...needed({ telegram: true }));
|
|
119
|
+
await expect(niwa.serve()).rejects.toThrow(/plugin "rin" .*telegram\.send/);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("Cockpit submits a routed message and reads the reply from the session that got the turn", async () => {
|
|
123
|
+
const home = await tempHome();
|
|
124
|
+
cleanups.push(() => home.cleanup());
|
|
125
|
+
const fake = fakeHarness();
|
|
126
|
+
const control = fake.instance.sides.control!;
|
|
127
|
+
const instance = { ...fake.instance, sides: { ...fake.instance.sides, control: { ...control, tools: { allow: control.tools?.allow ?? false, niwaOnly: true } } } };
|
|
128
|
+
const sessionsModule = sessions({ harnesses: [{ ...fake.binding, instance }], niwaMcp: { command: process.execPath, args: ["-e", ""] } });
|
|
129
|
+
const agentsModule = agents({ default: RIN_AGENT });
|
|
130
|
+
const bind = addressingOf([sessionsModule, agentsModule]);
|
|
131
|
+
const niwa = createNiwa({ home: home.path });
|
|
132
|
+
cleanups.push(() => niwa.close());
|
|
133
|
+
niwa.use(messages({ bind }), requests({ bind }), tasks({ messages: { bind } }), timers([]), sessionsModule, agentsModule, workflow(), ...rin({ bind, harness: FAKE_HARNESS }));
|
|
134
|
+
await niwa.serve();
|
|
135
|
+
const owner = await connectAsOwner(home.path);
|
|
136
|
+
cleanups.push(() => owner.close());
|
|
137
|
+
const replyTo = { channel: "cockpit", conversation: "rin" } as const;
|
|
138
|
+
const { message } = await owner.call(agentTell, { agent: "rin", text: "What shipped?", replyTo });
|
|
139
|
+
await eventually(async () => (await owner.call(rinConversation, {})).messages[0]?.delivery !== "waiting", 1_000);
|
|
140
|
+
const deliveries = (await niwa.records.read()).filter(({ type }) => type === "agent.turn.failed");
|
|
141
|
+
expect(deliveries.map(({ data }) => data)).toEqual([]);
|
|
142
|
+
expect((await owner.call(rinConversation, {})).messages[0]?.delivery).toBe("delivered");
|
|
143
|
+
const { session } = await owner.call(agentGet, { agent: "rin" });
|
|
144
|
+
const records = await niwa.records.read();
|
|
145
|
+
const mark = records.findLast((row) => row.type === "session.marked" && (row.data as { session?: string }).session === session)?.data as { mark: string } | undefined;
|
|
146
|
+
if (mark === undefined) throw new Error("the delivered turn must be bound to a session");
|
|
147
|
+
const token = niwa.issueToken(principalOf("session", mark.mark), { grant: grant(["agent.reply"]) });
|
|
148
|
+
const rinSession = await connect({ home: home.path, id: "rin-session", token });
|
|
149
|
+
cleanups.push(() => rinSession.close());
|
|
150
|
+
await expect(owner.call(actionName("agent.reply"), { turn: message, text: "Forged" })).rejects.toThrow("Only the session");
|
|
151
|
+
await rinSession.call(actionName("agent.reply"), { turn: message, text: "The cutover landed." });
|
|
152
|
+
expect((await owner.call(rinConversation, {})).messages).toMatchObject([
|
|
153
|
+
{ turn: message, from: "owner", text: "What shipped?", delivery: "delivered" },
|
|
154
|
+
{ turn: message, from: "rin", text: "The cutover landed." },
|
|
155
|
+
]);
|
|
156
|
+
expect((await owner.call(rinConversation, { conversation: "other" })).messages).toEqual([]);
|
|
157
|
+
});
|