@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
package/src/presence.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the owner is working now: the native sessions he attached to an agent, as their watch last saw them.
|
|
3
|
+
*
|
|
4
|
+
* Attaching a session is the owner's own act, so an attached session that is neither paused nor over is him at
|
|
5
|
+
* work. A pause is only the transcript going quiet; progress written after it says he is back.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Row } from "./attention.ts";
|
|
9
|
+
|
|
10
|
+
/** A session the owner is working in now. */
|
|
11
|
+
export interface Working {
|
|
12
|
+
readonly session: string;
|
|
13
|
+
readonly cwd: string;
|
|
14
|
+
readonly agent: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The record types the fold reads. */
|
|
18
|
+
export const PRESENCE_TYPES = ["agent.attached", "agent.detached", "history.session.paused", "history.session.ended", "history.session.seen", "session.progress"] as const;
|
|
19
|
+
|
|
20
|
+
/** `<harness> <native>`, the way an attachment names a session. */
|
|
21
|
+
function nameOf(session: unknown): string | undefined {
|
|
22
|
+
const id = session as { harness?: unknown; native?: unknown } | undefined;
|
|
23
|
+
return typeof id?.harness === "string" && typeof id.native === "string" ? `${id.harness} ${id.native}` : undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The attached sessions the owner is working in, in the order they were attached. */
|
|
27
|
+
export function workingNow(rows: readonly Row[]): Working[] {
|
|
28
|
+
const attached = new Map<string, { cwd: string; agent: string; active: boolean }>();
|
|
29
|
+
for (const row of rows) {
|
|
30
|
+
const data = row.data as { agent?: unknown; session?: unknown; cwd?: unknown };
|
|
31
|
+
switch (row.type) {
|
|
32
|
+
case "agent.attached":
|
|
33
|
+
if (typeof data.session === "string" && typeof data.cwd === "string" && typeof data.agent === "string") {
|
|
34
|
+
attached.delete(data.session);
|
|
35
|
+
attached.set(data.session, { cwd: data.cwd, agent: data.agent, active: true });
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
case "agent.detached":
|
|
39
|
+
if (typeof data.session === "string") attached.delete(data.session);
|
|
40
|
+
break;
|
|
41
|
+
case "history.session.ended": {
|
|
42
|
+
const name = nameOf(data.session);
|
|
43
|
+
if (name !== undefined) attached.delete(name);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case "history.session.paused":
|
|
47
|
+
case "history.session.seen":
|
|
48
|
+
case "session.progress": {
|
|
49
|
+
const name = nameOf(data.session);
|
|
50
|
+
const one = name === undefined ? undefined : attached.get(name);
|
|
51
|
+
if (one !== undefined) one.active = row.type !== "history.session.paused";
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return [...attached].filter(([, one]) => one.active).map(([session, one]) => ({ session, cwd: one.cwd, agent: one.agent }));
|
|
57
|
+
}
|
package/src/records.ts
ADDED
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Rin writes and the actions she serves: her attention (occasions, her decisions on them, what she holds for
|
|
3
|
+
* the owner and what she says to him), the corrections she learns from, and her charter.
|
|
4
|
+
*
|
|
5
|
+
* Nothing here runs; the plugin (`plugin.ts`) serves the actions and folds the records.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
|
|
10
|
+
import { Address, actionRef, record } from "@niwa-labs/core";
|
|
11
|
+
import { AgentName, PromptSectionInput, PromptSectionOutput, TurnAuthor, TurnId } from "@niwa-labs/agent/records";
|
|
12
|
+
|
|
13
|
+
import { charterGuard, correctionStatus, editGuard, hasProposal, occasionGuard, ruleStatus } from "./admission.ts";
|
|
14
|
+
|
|
15
|
+
/** An occasion: something that happened and may matter to the owner, kept as one while it holds. */
|
|
16
|
+
export const OccasionId = z.string().regex(/^occ-[0-9a-f]{10}$/);
|
|
17
|
+
export type OccasionId = z.output<typeof OccasionId>;
|
|
18
|
+
|
|
19
|
+
/** One thing Rin means to tell the owner: held for later, said, or brought up at a contact. */
|
|
20
|
+
export const ItemId = z.string().regex(/^att-[0-9a-f]{10}$/);
|
|
21
|
+
export type ItemId = z.output<typeof ItemId>;
|
|
22
|
+
|
|
23
|
+
/** What the owner did to a note of hers: changed its words, took it back, or erased it. */
|
|
24
|
+
export const NoteChange = z.object({ memory: z.string().min(1), note: z.string().min(1), change: z.enum(["corrected", "retracted", "erased"]) });
|
|
25
|
+
export type NoteChange = z.output<typeof NoteChange>;
|
|
26
|
+
|
|
27
|
+
/** One correction of the owner's. */
|
|
28
|
+
export const CorrectionId = z.string().regex(/^cor-[0-9a-f]{10}$/);
|
|
29
|
+
export type CorrectionId = z.output<typeof CorrectionId>;
|
|
30
|
+
|
|
31
|
+
/** One change of herself she asked the owner to approve. */
|
|
32
|
+
export const EditId = z.string().regex(/^edit-[0-9a-f]{10}$/);
|
|
33
|
+
export type EditId = z.output<typeof EditId>;
|
|
34
|
+
|
|
35
|
+
/** One rule of her charter, from the moment it is proposed. */
|
|
36
|
+
export const RuleId = z.string().regex(/^rule-[0-9a-f]{10}$/);
|
|
37
|
+
export type RuleId = z.output<typeof RuleId>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* What an occasion is known by: the same key while it holds is the same occasion (`ci:pr-101`, `task:t-7`).
|
|
41
|
+
* Lowercase words, digits and `:./_-`.
|
|
42
|
+
*/
|
|
43
|
+
export const OccasionKey = z.string().regex(/^[a-z0-9][a-z0-9:./_-]{0,127}$/, "an occasion key is lowercase letters, digits and ':./_-', at most 128");
|
|
44
|
+
|
|
45
|
+
/** The kind of an occasion, as a person reads it in a list: `ci`, `task`, `agent`, `question`. */
|
|
46
|
+
export const OccasionKind = z.string().regex(/^[a-z][a-z0-9-]{0,31}$/, "an occasion kind is one lowercase word, at most 32");
|
|
47
|
+
|
|
48
|
+
/** What a correction or a rule is about: a word or a dotted path (`attention.ci`, `tone`, `memory`). */
|
|
49
|
+
export const Topic = z.string().regex(/^[a-z][a-z0-9.-]{0,63}$/, "a topic is lowercase letters, digits, '.' and '-', at most 64");
|
|
50
|
+
export type Topic = z.output<typeof Topic>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* What Rin may decide about something that happened:
|
|
54
|
+
*
|
|
55
|
+
* - `silent` — nothing, and that is a decision;
|
|
56
|
+
* - `note` — keep it, for herself;
|
|
57
|
+
* - `next-contact` — tell him the next time he is in touch;
|
|
58
|
+
* - `now` — tell him now;
|
|
59
|
+
* - `insist` — raise again what she already told him, by a rule of her charter.
|
|
60
|
+
*/
|
|
61
|
+
export const Decision = z.enum(["silent", "note", "next-contact", "now", "insist"]);
|
|
62
|
+
export type Decision = z.output<typeof Decision>;
|
|
63
|
+
|
|
64
|
+
/** What a decision is about: an occasion of her attention, or a turn of her inbox (a task's outcome, a digest). */
|
|
65
|
+
export const Subject = z.discriminatedUnion("kind", [
|
|
66
|
+
z.object({ kind: z.literal("occasion"), occasion: OccasionId }),
|
|
67
|
+
z.object({ kind: z.literal("turn"), turn: TurnId }),
|
|
68
|
+
]);
|
|
69
|
+
export type Subject = z.output<typeof Subject>;
|
|
70
|
+
|
|
71
|
+
/** Why something waits instead of being said. */
|
|
72
|
+
export const HoldReason = z.enum(["next-contact", "quiet-hours", "ceiling", "undelivered"]);
|
|
73
|
+
export type HoldReason = z.output<typeof HoldReason>;
|
|
74
|
+
|
|
75
|
+
/** How the owner came in touch: he wrote to her, or attached a session of his. */
|
|
76
|
+
export const Contact = z.discriminatedUnion("kind", [
|
|
77
|
+
z.object({ kind: z.literal("message"), message: z.string().min(1) }),
|
|
78
|
+
z.object({ kind: z.literal("attach"), session: z.string().min(1) }),
|
|
79
|
+
]);
|
|
80
|
+
export type Contact = z.output<typeof Contact>;
|
|
81
|
+
|
|
82
|
+
const Text = z.string().min(1).max(2000);
|
|
83
|
+
|
|
84
|
+
// ---- attention ------------------------------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
const OccasionRevision = z.object({ occasion: OccasionId, key: OccasionKey });
|
|
87
|
+
const Notice = z.object({ key: OccasionKey, notice: z.string() });
|
|
88
|
+
const ActiveOccasion = OccasionRevision.extend({ notice: z.string(), text: Text, from: TurnAuthor });
|
|
89
|
+
|
|
90
|
+
const openChanged = { code: "occasion-changed", reason: "The open occasion changed" };
|
|
91
|
+
/** Something happened that may matter to the owner: a new occasion. */
|
|
92
|
+
export const OccasionOpened = record(
|
|
93
|
+
"rin.occasion.opened",
|
|
94
|
+
ActiveOccasion.extend({ kind: OccasionKind }),
|
|
95
|
+
occasionGuard(
|
|
96
|
+
(state) => state.open === null ? undefined : openChanged,
|
|
97
|
+
(state, data) => ({ ...state, open: data.occasion }),
|
|
98
|
+
),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
/** The same thing happened again while its occasion holds. */
|
|
102
|
+
export const OccasionRepeated = record(
|
|
103
|
+
"rin.occasion.repeated",
|
|
104
|
+
ActiveOccasion,
|
|
105
|
+
occasionGuard(
|
|
106
|
+
(state, data) => state.open === data.occasion ? undefined : openChanged,
|
|
107
|
+
(state) => state,
|
|
108
|
+
),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
/** An occasion no longer holds. What waits about it is dropped. */
|
|
112
|
+
export const OccasionEnded = record(
|
|
113
|
+
"rin.occasion.ended",
|
|
114
|
+
ActiveOccasion.partial({ text: true }),
|
|
115
|
+
occasionGuard(
|
|
116
|
+
(state, data) => state.open === data.occasion ? undefined : openChanged,
|
|
117
|
+
(state) => ({ ...state, open: null }),
|
|
118
|
+
),
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
/** An end notice reached an occasion key with no open occasion. */
|
|
122
|
+
export const OccasionIgnored = record(
|
|
123
|
+
"rin.occasion.ignored",
|
|
124
|
+
Notice,
|
|
125
|
+
occasionGuard(
|
|
126
|
+
(state) => state.open === null ? undefined : openChanged,
|
|
127
|
+
(state) => state,
|
|
128
|
+
),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
/** Claim delivery of the fact for this occasion before calling the agent. */
|
|
132
|
+
export const OccasionClaimed = record(
|
|
133
|
+
"rin.occasion.claimed",
|
|
134
|
+
OccasionRevision.extend({ text: z.string() }),
|
|
135
|
+
occasionGuard(
|
|
136
|
+
(state, data) => state.open !== data.occasion ? openChanged : state.notifications.has(data.occasion)
|
|
137
|
+
? { code: "occasion-already-claimed", reason: "Delivery of this occasion was already claimed" } : undefined,
|
|
138
|
+
(state, data) => ({ ...state, notifications: new Map(state.notifications).set(data.occasion, "claimed") }),
|
|
139
|
+
),
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
export const OccasionNotified = record(
|
|
143
|
+
"rin.occasion.notified",
|
|
144
|
+
OccasionRevision,
|
|
145
|
+
occasionGuard(
|
|
146
|
+
(state, data) => state.notifications.get(data.occasion) === "claimed"
|
|
147
|
+
? undefined : { code: "occasion-unclaimed", reason: "There is no outstanding delivery claim for this occasion" },
|
|
148
|
+
(state, data) => ({ ...state, notifications: new Map(state.notifications).set(data.occasion, "notified") }),
|
|
149
|
+
),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
/** Rin decided what to do about something, with what she would tell him and the rule she insists by. */
|
|
153
|
+
export const AttentionDecided = record(
|
|
154
|
+
"rin.attention.decided",
|
|
155
|
+
z.object({ subject: Subject, decision: Decision, text: Text.optional(), rule: RuleId.optional() }),
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
/** Something she means to tell him waits: for the next contact, or for the end of his quiet hours. */
|
|
159
|
+
export const AttentionHeld = record(
|
|
160
|
+
"rin.attention.held",
|
|
161
|
+
z.object({
|
|
162
|
+
item: ItemId,
|
|
163
|
+
subject: Subject,
|
|
164
|
+
text: Text,
|
|
165
|
+
until: z.enum(["contact", "quiet-end"]),
|
|
166
|
+
why: HoldReason,
|
|
167
|
+
rule: RuleId.optional(),
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
/** She is telling him now: what counts against the safety ceiling, from the moment it was decided. */
|
|
172
|
+
export const AttentionSending = record(
|
|
173
|
+
"rin.attention.sending",
|
|
174
|
+
z.object({ item: ItemId, subject: Subject, text: Text, rule: RuleId.optional() }),
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
/** She tried to tell him now and cannot tell whether it reached him: it is not said again, and it does not wait. */
|
|
178
|
+
export const AttentionUnsure = record(
|
|
179
|
+
"rin.attention.unsure",
|
|
180
|
+
z.object({ item: ItemId, subject: Subject, text: Text, why: z.string().min(1), rule: RuleId.optional() }),
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
/** She told him, now, on a channel. */
|
|
184
|
+
export const AttentionSaid = record(
|
|
185
|
+
"rin.attention.said",
|
|
186
|
+
z.object({ item: ItemId, subject: Subject, text: Text, channel: z.string().min(1), rule: RuleId.optional() }),
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
/** What waited was brought to him when he came in touch. */
|
|
190
|
+
export const AttentionBrought = record("rin.attention.brought", z.object({ items: z.array(ItemId).min(1), contact: Contact }));
|
|
191
|
+
|
|
192
|
+
/** What waited was dropped: the occasion it was about no longer holds. */
|
|
193
|
+
export const AttentionDropped = record("rin.attention.dropped", z.object({ item: ItemId, occasion: OccasionId }));
|
|
194
|
+
|
|
195
|
+
/** The owner waved away something she told him or held for him. */
|
|
196
|
+
export const AttentionDismissed = record("rin.attention.dismissed", z.object({ item: ItemId, reason: z.string().min(1).optional() }));
|
|
197
|
+
|
|
198
|
+
// ---- corrections and the charter ------------------------------------------------------------------------------
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The owner corrected her: he said "not like that", changed or took back a note of hers, cancelled what she
|
|
202
|
+
* decided or promised, turned down or left unanswered what she asked, or waved away what she told him.
|
|
203
|
+
*/
|
|
204
|
+
export const Corrected = record(
|
|
205
|
+
"rin.corrected",
|
|
206
|
+
z.object({
|
|
207
|
+
correction: CorrectionId,
|
|
208
|
+
about: Topic,
|
|
209
|
+
what: Text,
|
|
210
|
+
/** Who wrote the correction down: the owner himself, Rin from his words, or niwa from what he did. */
|
|
211
|
+
by: TurnAuthor,
|
|
212
|
+
/** The record of his that it rests on: his message, or the record of what he did. */
|
|
213
|
+
source: z.string().min(1).optional(),
|
|
214
|
+
/**
|
|
215
|
+
* The note of her memory it is about, and what he did to it. The note's words are not copied here: they are
|
|
216
|
+
* read from memory's records when the correction is shown, and not shown for a note erased since.
|
|
217
|
+
*/
|
|
218
|
+
note: NoteChange.optional(),
|
|
219
|
+
}),
|
|
220
|
+
charterGuard(
|
|
221
|
+
(state, data) => state.corrections.has(data.correction) ? { code: "correction-known", reason: "This correction already exists" } : undefined,
|
|
222
|
+
(state, data) => ({ ...state, corrections: new Map(state.corrections).set(data.correction, "new") }),
|
|
223
|
+
),
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
/** Corrections about one thing repeat: Rin is asked whether a rule of her charter should cover them. */
|
|
227
|
+
export const CharterDue = record(
|
|
228
|
+
"rin.charter.due",
|
|
229
|
+
z.object({ about: Topic, corrections: z.array(CorrectionId).min(1) }),
|
|
230
|
+
charterGuard(
|
|
231
|
+
(state, data) => hasProposal(state) || data.corrections.some((id) => state.corrections.get(id) !== "new")
|
|
232
|
+
? { code: "corrections-changed", reason: "These corrections can no longer make a rule due" } : undefined,
|
|
233
|
+
(state, data) => correctionStatus(state, data.corrections, "asked"),
|
|
234
|
+
),
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
/** A rule of the charter was proposed to the owner, with the corrections it covers. */
|
|
238
|
+
export const CharterProposed = record(
|
|
239
|
+
"rin.charter.proposed",
|
|
240
|
+
z.object({ rule: RuleId, about: Topic, text: Text, corrections: z.array(CorrectionId), by: TurnAuthor }),
|
|
241
|
+
charterGuard(
|
|
242
|
+
(state, data) => {
|
|
243
|
+
if (state.rules.has(data.rule)) return { code: "rule-known", reason: "This rule already exists" };
|
|
244
|
+
if (data.by.kind === "agent" && hasProposal(state)) return { code: "open-proposal", reason: `A rule about ${data.about} already waits for the owner's word` };
|
|
245
|
+
if (data.corrections.some((id) => !state.corrections.has(id) || state.corrections.get(id) === "covered")) {
|
|
246
|
+
return { code: "corrections-changed", reason: "Only uncovered corrections about this topic can be attached" };
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
(state, data) => ({
|
|
250
|
+
...correctionStatus(state, data.corrections, "covered"),
|
|
251
|
+
rules: new Map(state.rules).set(data.rule, "proposed"),
|
|
252
|
+
}),
|
|
253
|
+
),
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
/** The owner confirmed a rule: it is part of the charter from now on. */
|
|
257
|
+
export const CharterRuled = record("rin.charter.ruled", z.object({ rule: RuleId, about: Topic, by: Address }), ruleStatus("ruled"));
|
|
258
|
+
|
|
259
|
+
/** A proposed rule did not become part of the charter: the owner said no, the question was dropped, or something else than he answered it. */
|
|
260
|
+
export const CharterDeclined = record(
|
|
261
|
+
"rin.charter.declined",
|
|
262
|
+
z.object({ rule: RuleId, about: Topic, why: z.enum(["declined", "cancelled", "expired", "not-his"]) }),
|
|
263
|
+
ruleStatus("declined"),
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
/** The owner took a rule out of the charter. */
|
|
267
|
+
export const CharterRetired = record("rin.charter.retired", z.object({ rule: RuleId, about: Topic, reason: z.string().min(1).optional() }), ruleStatus("retired"));
|
|
268
|
+
|
|
269
|
+
// ---- changing herself -----------------------------------------------------------------------------------------
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Rin asked to install a plugin of her own group: the module, and the hash of the file as it was when she asked.
|
|
273
|
+
* The install is pinned to that hash, so what the owner approves is the file that is installed.
|
|
274
|
+
*/
|
|
275
|
+
export const SelfEditMode = z.enum(["off", "ask", "free"]);
|
|
276
|
+
export type SelfEditMode = z.output<typeof SelfEditMode>;
|
|
277
|
+
const Hash = z.string().regex(/^[a-f0-9]{64}$/);
|
|
278
|
+
const RinModule = z.string().regex(/^rin(?:\/[a-z][a-z0-9-]*)+$/);
|
|
279
|
+
const EditProposal = z.object({ edit: EditId, plugin: RinModule, module: z.string().min(1), hash: Hash, why: Text, mode: z.enum(["ask", "free"]) });
|
|
280
|
+
const EditOutcome = z.enum(["installed", "declined", "cancelled", "expired", "not-his", "failed", "unknown"]);
|
|
281
|
+
|
|
282
|
+
export const SelfEditProposed = record(
|
|
283
|
+
"rin.self-edit.proposed",
|
|
284
|
+
EditProposal,
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
/** The approved installation now owns the right to try the effect for this plugin. */
|
|
288
|
+
export const SelfEditClaimed = record(
|
|
289
|
+
"rin.self-edit.claimed",
|
|
290
|
+
z.object({ plugin: z.string(), edit: EditId, hash: Hash }),
|
|
291
|
+
editGuard(
|
|
292
|
+
(current) => current === null ? undefined : { code: "edit-in-progress", reason: "An installation is already in progress" },
|
|
293
|
+
(_current, data) => data.edit,
|
|
294
|
+
),
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
/** How a change of herself ended: installed, or why it was not. */
|
|
298
|
+
export const SelfEditSettled = record(
|
|
299
|
+
"rin.self-edit.settled",
|
|
300
|
+
z.object({ edit: EditId, plugin: z.string(), outcome: EditOutcome, detail: z.string().optional() }),
|
|
301
|
+
editGuard(
|
|
302
|
+
(current, data) => current === data.edit || (current === null && data.outcome !== "installed" && data.outcome !== "unknown")
|
|
303
|
+
? undefined : { code: "edit-unclaimed", reason: "This installation holds no claim" },
|
|
304
|
+
(current, data) => current === data.edit ? null : current,
|
|
305
|
+
),
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
/** The record types Rin declares. */
|
|
309
|
+
export const RIN_TYPES = [
|
|
310
|
+
OccasionOpened,
|
|
311
|
+
OccasionRepeated,
|
|
312
|
+
OccasionEnded,
|
|
313
|
+
OccasionIgnored,
|
|
314
|
+
OccasionClaimed,
|
|
315
|
+
OccasionNotified,
|
|
316
|
+
AttentionDecided,
|
|
317
|
+
AttentionHeld,
|
|
318
|
+
AttentionSending,
|
|
319
|
+
AttentionUnsure,
|
|
320
|
+
AttentionSaid,
|
|
321
|
+
AttentionBrought,
|
|
322
|
+
AttentionDropped,
|
|
323
|
+
AttentionDismissed,
|
|
324
|
+
Corrected,
|
|
325
|
+
CharterDue,
|
|
326
|
+
CharterProposed,
|
|
327
|
+
CharterRuled,
|
|
328
|
+
CharterDeclined,
|
|
329
|
+
CharterRetired,
|
|
330
|
+
SelfEditProposed,
|
|
331
|
+
SelfEditClaimed,
|
|
332
|
+
SelfEditSettled,
|
|
333
|
+
] as const;
|
|
334
|
+
|
|
335
|
+
// ---- actions --------------------------------------------------------------------------------------------------
|
|
336
|
+
|
|
337
|
+
/** One rule of the charter as it is read. */
|
|
338
|
+
export const RuleItem = z.object({
|
|
339
|
+
rule: RuleId,
|
|
340
|
+
about: Topic,
|
|
341
|
+
text: Text,
|
|
342
|
+
status: z.enum(["proposed", "ruled", "declined", "retired"]),
|
|
343
|
+
corrections: z.array(CorrectionId),
|
|
344
|
+
});
|
|
345
|
+
export type RuleItem = z.output<typeof RuleItem>;
|
|
346
|
+
|
|
347
|
+
/** One correction as it is read. */
|
|
348
|
+
export const CorrectionItem = z.object({
|
|
349
|
+
correction: CorrectionId,
|
|
350
|
+
about: Topic,
|
|
351
|
+
what: Text,
|
|
352
|
+
by: TurnAuthor,
|
|
353
|
+
at: z.number(),
|
|
354
|
+
/** The rule that covers it, once one was proposed for it. */
|
|
355
|
+
rule: RuleId.optional(),
|
|
356
|
+
});
|
|
357
|
+
export type CorrectionItem = z.output<typeof CorrectionItem>;
|
|
358
|
+
|
|
359
|
+
/** Where one thing she meant to tell him stands. */
|
|
360
|
+
export const ItemStatus = z.enum(["held", "sending", "unsure", "said", "brought", "dropped", "dismissed"]);
|
|
361
|
+
export type ItemStatus = z.output<typeof ItemStatus>;
|
|
362
|
+
|
|
363
|
+
/** One thing she meant to tell him, as it is read. */
|
|
364
|
+
export const AttentionItem = z.object({
|
|
365
|
+
item: ItemId,
|
|
366
|
+
subject: Subject,
|
|
367
|
+
text: Text,
|
|
368
|
+
status: ItemStatus,
|
|
369
|
+
/** What it waits for, while it is held. */
|
|
370
|
+
until: z.enum(["contact", "quiet-end"]).optional(),
|
|
371
|
+
why: HoldReason.optional(),
|
|
372
|
+
/** The kind of the occasion it is about, and how many times the occasion came up. */
|
|
373
|
+
kind: OccasionKind.optional(),
|
|
374
|
+
count: z.number().int().positive().optional(),
|
|
375
|
+
rule: RuleId.optional(),
|
|
376
|
+
at: z.number(),
|
|
377
|
+
});
|
|
378
|
+
export type AttentionItem = z.output<typeof AttentionItem>;
|
|
379
|
+
|
|
380
|
+
/** An occasion that holds, as it is read. */
|
|
381
|
+
export const OccasionItem = z.object({
|
|
382
|
+
occasion: OccasionId,
|
|
383
|
+
key: OccasionKey,
|
|
384
|
+
kind: OccasionKind,
|
|
385
|
+
text: Text,
|
|
386
|
+
count: z.number().int().positive(),
|
|
387
|
+
decision: Decision.optional(),
|
|
388
|
+
at: z.number(),
|
|
389
|
+
});
|
|
390
|
+
export type OccasionItem = z.output<typeof OccasionItem>;
|
|
391
|
+
|
|
392
|
+
export const rinNotice = actionRef("rin.notice", {
|
|
393
|
+
description:
|
|
394
|
+
"Tells Rin that something happened which may matter to the owner. The key names the occasion: while an occasion of the same key holds, a repeat is counted on it and does not reach her again. `ends: true` says the occasion no longer holds (the build is green again); what she held about it is dropped. A new occasion reaches her inbox as a fact, and she decides what to do about it.",
|
|
395
|
+
input: z.object({ key: OccasionKey, kind: OccasionKind, text: Text, ends: z.boolean().optional() }),
|
|
396
|
+
output: z.object({ occasion: OccasionId.optional(), status: z.enum(["opened", "repeated", "ended", "none"]) }),
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
export const rinAttend = actionRef("rin.attend", {
|
|
400
|
+
description:
|
|
401
|
+
"Rin's decision about something that happened: an occasion of her attention, or a turn of her inbox. `silent` does nothing; `note` keeps it for herself; `next-contact` holds what to tell the owner until he is next in touch; `now` tells him now; `insist` raises again what she already told him, and must name a confirmed rule of her charter. Give `text`, what he is to read, for anything she tells him. Something said now waits instead during his quiet hours, or when the safety ceiling is reached, and the answer says so.",
|
|
402
|
+
input: z.object({ subject: Subject, decision: Decision, text: Text.optional(), rule: RuleId.optional() }),
|
|
403
|
+
output: z.object({
|
|
404
|
+
decision: Decision,
|
|
405
|
+
/** `sending`: it is on its way to him; if it cannot reach him, it is held for his next contact and she is told. */
|
|
406
|
+
outcome: z.enum(["kept", "held", "sending"]),
|
|
407
|
+
item: ItemId.optional(),
|
|
408
|
+
why: HoldReason.optional(),
|
|
409
|
+
/** When it is held until the end of his quiet hours: that moment. */
|
|
410
|
+
until: z.number().optional(),
|
|
411
|
+
}),
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
export const rinContext = actionRef("rin.context", {
|
|
415
|
+
description:
|
|
416
|
+
"What Rin weighs before she speaks: whether the owner is working now (his attached sessions), whether these are his quiet hours, how much of the safety ceiling is left, her charter, the occasions that hold and what she holds for his next contact.",
|
|
417
|
+
input: z.object({}),
|
|
418
|
+
output: z.object({
|
|
419
|
+
at: z.number(),
|
|
420
|
+
working: z.array(z.object({ session: z.string(), cwd: z.string(), agent: AgentName })),
|
|
421
|
+
quiet: z.object({ now: z.boolean(), from: z.string(), to: z.string(), ends: z.number().optional() }),
|
|
422
|
+
ceiling: z.object({ count: z.number().int(), windowMs: z.number().int(), left: z.number().int() }),
|
|
423
|
+
charter: z.array(RuleItem),
|
|
424
|
+
occasions: z.array(OccasionItem),
|
|
425
|
+
held: z.array(AttentionItem),
|
|
426
|
+
}),
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
export const rinAttention = actionRef("rin.attention.list", {
|
|
430
|
+
description: "What Rin meant to tell the owner, newest first: what she holds, what she said, what was brought up, dropped or waved away.",
|
|
431
|
+
input: z.object({ status: ItemStatus.optional() }),
|
|
432
|
+
output: z.object({ items: z.array(AttentionItem), occasions: z.array(OccasionItem) }),
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
export const rinDismiss = actionRef("rin.attention.dismiss", {
|
|
436
|
+
description: "The owner waves away something Rin told him or holds for him: it is not brought up again, and she learns from it as from a correction.",
|
|
437
|
+
input: z.object({ item: ItemId, reason: z.string().min(1).optional() }),
|
|
438
|
+
output: z.object({ item: ItemId, correction: CorrectionId }),
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
export const rinCorrect = actionRef("rin.correct", {
|
|
442
|
+
description:
|
|
443
|
+
"Writes down a correction of the owner's: what she did that he did not want, and what he wants instead, as one plain sentence that still makes sense a month from now. `about` is what it concerns (`attention.ci`, `tone`, `memory`). The owner may write one himself; Rin writes one only from his own words and gives the id of the record that holds them as `source`.",
|
|
444
|
+
input: z.object({ about: Topic, what: Text, source: z.string().min(1).optional() }),
|
|
445
|
+
output: z.object({ correction: CorrectionId }),
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
export const rinCharterPropose = actionRef("rin.charter.propose", {
|
|
449
|
+
description:
|
|
450
|
+
"Proposes a rule of Rin's charter: how the owner wants her to act, in one sentence, with what it is about and the corrections it covers. It becomes part of the charter only once the owner confirms it; a rule the owner proposes himself stands at once.",
|
|
451
|
+
input: z.object({ about: Topic, text: Text, corrections: z.array(CorrectionId).default([]) }),
|
|
452
|
+
output: z.object({ rule: RuleId, status: z.enum(["proposed", "ruled"]) }),
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
export const rinCharterRetire = actionRef("rin.charter.retire", {
|
|
456
|
+
description: "The owner takes a rule out of Rin's charter.",
|
|
457
|
+
input: z.object({ rule: RuleId, reason: z.string().min(1).optional() }),
|
|
458
|
+
output: z.object({ rule: RuleId }),
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
export const rinCharter = actionRef("rin.charter", {
|
|
462
|
+
description: "Rin's charter: the rules the owner confirmed, the ones proposed to him, and the corrections they grew from.",
|
|
463
|
+
input: z.object({}),
|
|
464
|
+
output: z.object({ rules: z.array(RuleItem), corrections: z.array(CorrectionItem) }),
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
export const rinSelfInstall = actionRef("rin.self.install", {
|
|
468
|
+
description:
|
|
469
|
+
"Proposes installing a plugin of Rin’s group (rin/*) from a module file. Say what the plugin does and why. The host’s selfEdit setting controls whether the owner must approve the specific file hash.",
|
|
470
|
+
input: z.object({ plugin: z.string().min(1), module: z.string().min(1), why: Text }),
|
|
471
|
+
output: z.object({ edit: EditId, hash: z.string() }),
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
/** The core action that installs a plugin from a file, as the handler that carries out an approved change calls it. */
|
|
475
|
+
export const pluginInstall = actionRef("plugin.install", {
|
|
476
|
+
description: "Installs a plugin from a module file, or replaces the installed plugin of that name; the hash pins the file.",
|
|
477
|
+
input: z.object({ plugin: z.string().min(1), module: z.string().min(1), hash: z.string().min(1).optional() }),
|
|
478
|
+
output: z.object({ plugin: z.string(), version: z.string(), hash: z.string() }),
|
|
479
|
+
target: (input) => input.plugin,
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
export const rinSelfHistory = actionRef("rin.self.history", {
|
|
483
|
+
description: "Rin's proposed and completed module installations and their current status.",
|
|
484
|
+
input: z.object({}),
|
|
485
|
+
output: z.object({
|
|
486
|
+
mode: SelfEditMode,
|
|
487
|
+
edits: z.array(EditProposal.extend({ at: z.number(), status: z.union([EditOutcome, z.enum(["pending", "installing"])]), detail: z.string().optional() })),
|
|
488
|
+
installed: z.array(z.object({ plugin: z.string(), hash: Hash, version: z.string() })),
|
|
489
|
+
}),
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
export const rinInbox = actionRef("rin.inbox", {
|
|
493
|
+
description:
|
|
494
|
+
"What reached Rin's inbox, newest first, with the id of each turn: a task's outcome, a promise falling due, the digest of an attached session, a message. `rin.attend` on a turn names it by that id. By default only the turns she has not decided about.",
|
|
495
|
+
input: z.object({ decided: z.boolean().optional(), limit: z.number().int().positive().max(100).default(20) }),
|
|
496
|
+
output: z.object({
|
|
497
|
+
turns: z.array(
|
|
498
|
+
z.object({
|
|
499
|
+
turn: TurnId,
|
|
500
|
+
kind: z.enum(["message", "fact"]),
|
|
501
|
+
from: TurnAuthor,
|
|
502
|
+
/** The first lines of what it says. */
|
|
503
|
+
text: z.string(),
|
|
504
|
+
decided: z.boolean(),
|
|
505
|
+
}),
|
|
506
|
+
),
|
|
507
|
+
}),
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
/** Where a message of the owner's to Rin stands: waiting in her inbox, in her session, or not delivered. */
|
|
511
|
+
export const Delivery = z.enum(["waiting", "delivered", "unsent", "possibly-sent"]);
|
|
512
|
+
export type Delivery = z.output<typeof Delivery>;
|
|
513
|
+
|
|
514
|
+
export const rinConversation = actionRef("rin.conversation", {
|
|
515
|
+
description:
|
|
516
|
+
"The conversation between the owner and Rin, oldest first, the last of it: what he wrote to her, with where each message stands (waiting in her inbox, delivered to her session, not delivered), and what she wrote to him.",
|
|
517
|
+
input: z.object({ conversation: z.string().min(1).default("rin"), limit: z.number().int().positive().max(200).default(50) }),
|
|
518
|
+
output: z.object({
|
|
519
|
+
messages: z.array(
|
|
520
|
+
z.object({
|
|
521
|
+
id: z.string(),
|
|
522
|
+
turn: TurnId.optional(),
|
|
523
|
+
from: z.enum(["owner", "rin"]),
|
|
524
|
+
text: z.string(),
|
|
525
|
+
at: z.number(),
|
|
526
|
+
/** For what he wrote: where it stands. */
|
|
527
|
+
delivery: Delivery.optional(),
|
|
528
|
+
}),
|
|
529
|
+
),
|
|
530
|
+
}),
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
export const rinBriefing = actionRef("rin.briefing", {
|
|
534
|
+
description: "Rin's section of a briefing: what she held for the owner's next contact, and the rules of her charter that wait for his word.",
|
|
535
|
+
input: z.object({ agent: AgentName }),
|
|
536
|
+
output: z.object({ text: z.string() }),
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
export const rinPromptCharter = actionRef("rin.prompt.charter", {
|
|
540
|
+
description: "Rin's charter as a section of the prompt of her sessions: the rules the owner confirmed, as they stand when a session starts.",
|
|
541
|
+
input: PromptSectionInput,
|
|
542
|
+
output: PromptSectionOutput,
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
/** The actions of Rin's attention and charter, for her hands. */
|
|
546
|
+
export const RIN_ACTIONS = [rinNotice, rinAttend, rinInbox, rinConversation, rinContext, rinAttention, rinCorrect, rinCharterPropose, rinCharter, rinSelfInstall] as const;
|