@niwa-labs/sessions 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 +17 -2
- package/package.json +46 -4
- package/src/actions.ts +350 -0
- package/src/ask.ts +230 -0
- package/src/box.ts +87 -0
- package/src/caps.ts +149 -0
- package/src/catalog.ts +146 -0
- package/src/cockpit/actions.ts +52 -0
- package/src/cockpit/index.tsx +36 -0
- package/src/cockpit/pages.tsx +190 -0
- package/src/cockpit/transcript.ts +64 -0
- package/src/confirm.ts +228 -0
- package/src/discover.ts +107 -0
- package/src/identity.ts +64 -0
- package/src/index.ts +1874 -0
- package/src/insert-plugin.ts +263 -0
- package/src/insert-routing.ts +84 -0
- package/src/life.ts +59 -0
- package/src/prompt.ts +50 -0
- package/src/records-entry.ts +91 -0
- package/src/records.ts +395 -0
- package/src/refusal.ts +13 -0
- package/src/reverse.ts +260 -0
- package/src/support.ts +93 -0
- package/src/visible.ts +28 -0
- package/test/access-self-answer.test.ts +197 -0
- package/test/box-socket.test.ts +132 -0
- package/test/cockpit-actions.test.ts +19 -0
- package/test/cockpit-transcript.test.ts +71 -0
- package/test/fixtures/restart-host.ts +175 -0
- package/test/grant-box.test.ts +102 -0
- package/test/insert-permission.test.ts +285 -0
- package/test/restart.test.ts +168 -0
- package/test/sessions-reverse.test.ts +341 -0
- package/test/sessions.test.ts +658 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The niwa side of every insert: the action "insert.report" and the records it writes.
|
|
3
|
+
*
|
|
4
|
+
* An insert runs inside a harness and cannot write to niwa's journal. It calls this action with
|
|
5
|
+
* an insert event, and the host writes the record of that event. The contract is the same for
|
|
6
|
+
* every harness kind: kinds differ in what they learn and how, not in what they say.
|
|
7
|
+
*
|
|
8
|
+
* A tool that asks for permission becomes a question for the owner: the owner is asked, and the
|
|
9
|
+
* answer goes back to the side that asked. The request is made by its name, as a step of the
|
|
10
|
+
* handler: this module does not import the requests module.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
import { OptionIdSchema, action, actionName, record, timestamp } from "@niwa-labs/core";
|
|
16
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
17
|
+
import type { ActionContext, ActionName, Answer, Question, Step, Timestamp } from "@niwa-labs/core";
|
|
18
|
+
import type { Plugin } from "@niwa-labs/plugin";
|
|
19
|
+
import {
|
|
20
|
+
CompactedEventSchema,
|
|
21
|
+
INSERT_REPORT_ACTION,
|
|
22
|
+
InsertEventSchema,
|
|
23
|
+
NestedSpawnedEventSchema,
|
|
24
|
+
OpenedByHandEventSchema,
|
|
25
|
+
PermissionAskedEventSchema,
|
|
26
|
+
SessionEndedEventSchema,
|
|
27
|
+
SessionIdSchema,
|
|
28
|
+
} from "@niwa-labs/harness";
|
|
29
|
+
import type { InsertEvent, PermissionAnswer, PermissionAskedEvent } from "@niwa-labs/harness";
|
|
30
|
+
|
|
31
|
+
import { permissionId, PermissionIdSchema } from "./insert-routing.ts";
|
|
32
|
+
import type { PermissionId, PermissionRouter } from "./insert-routing.ts";
|
|
33
|
+
|
|
34
|
+
/** The name of the action that asks a question and waits for the answer. */
|
|
35
|
+
const REQUEST_ASK: ActionName = actionName("request.ask");
|
|
36
|
+
|
|
37
|
+
/** A nested session was spawned inside a session. */
|
|
38
|
+
export const NestedSpawned = record(
|
|
39
|
+
"insert.nested-spawned",
|
|
40
|
+
NestedSpawnedEventSchema.omit({ kind: true }),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
/** The harness compacted the context of a session. */
|
|
44
|
+
export const Compacted = record("insert.compacted", CompactedEventSchema.omit({ kind: true }));
|
|
45
|
+
|
|
46
|
+
/** The owner opened a session by hand. */
|
|
47
|
+
export const OpenedByHand = record(
|
|
48
|
+
"insert.opened-by-hand",
|
|
49
|
+
OpenedByHandEventSchema.omit({ kind: true }),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
/** A tool asks for permission. */
|
|
53
|
+
export const PermissionAsked = record(
|
|
54
|
+
"insert.permission-asked",
|
|
55
|
+
PermissionAskedEventSchema.omit({ kind: true }),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
/** The harness ended a session. */
|
|
59
|
+
export const SessionEndedByHarness = record("insert.session-ended", SessionEndedEventSchema.omit({ kind: true }));
|
|
60
|
+
|
|
61
|
+
const INSERT_RECORDS = [NestedSpawned, Compacted, OpenedByHand, PermissionAsked, SessionEndedByHarness] as const;
|
|
62
|
+
|
|
63
|
+
/** What the action answers: the record that was written. */
|
|
64
|
+
export const InsertReportOutputSchema = z.object({
|
|
65
|
+
reported: z.literal(true),
|
|
66
|
+
record: z.string(),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export type InsertReportOutput = z.output<typeof InsertReportOutputSchema>;
|
|
70
|
+
|
|
71
|
+
/** The name of the question the owner is asked about a tool that wants to go on. */
|
|
72
|
+
export const PERMISSION_QUESTION = "insert.permission";
|
|
73
|
+
|
|
74
|
+
/** The ids of the options the owner picks from about a tool that asks for permission. */
|
|
75
|
+
const ALLOW = OptionIdSchema.parse("allow");
|
|
76
|
+
const DENY = OptionIdSchema.parse("deny");
|
|
77
|
+
|
|
78
|
+
/** The question about a tool that asks for permission: allow it or deny it. */
|
|
79
|
+
function permissionQuestion(event: PermissionAskedEvent): Question {
|
|
80
|
+
return {
|
|
81
|
+
kind: "permission",
|
|
82
|
+
title: `${event.tool} asks for permission`,
|
|
83
|
+
...(event.detail === undefined ? {} : { detail: event.detail }),
|
|
84
|
+
options: [
|
|
85
|
+
{ id: ALLOW, label: "Allow", flavor: "allow_once" },
|
|
86
|
+
{ id: DENY, label: "Deny", flavor: "reject_once" },
|
|
87
|
+
],
|
|
88
|
+
subject: { tool: event.tool, kind: "tool", target: event.detail ?? "" },
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* What the requests module is asked for.
|
|
94
|
+
*
|
|
95
|
+
* The shape is the contract of the name: this module does not import the requests module,
|
|
96
|
+
* so it declares what it sends, and the requests module checks it.
|
|
97
|
+
*/
|
|
98
|
+
interface RequestAskInput {
|
|
99
|
+
/** Who may answer. */
|
|
100
|
+
to: { readonly kind: "owner" };
|
|
101
|
+
/** The name of the question. */
|
|
102
|
+
question: string;
|
|
103
|
+
/** What is asked: the question, whole. */
|
|
104
|
+
input: Question;
|
|
105
|
+
/** When the question stops waiting. Without one it waits for ever. */
|
|
106
|
+
deadline?: Timestamp;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** What the requests module answers: the answer, with the id of the request. */
|
|
110
|
+
interface RequestAskOutput {
|
|
111
|
+
readonly answer: Answer;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** What the plugin needs to turn a permission question into a question for the owner. */
|
|
115
|
+
export interface InsertPluginOptions {
|
|
116
|
+
/**
|
|
117
|
+
* The routing that carries the answer back to the side that asked.
|
|
118
|
+
*
|
|
119
|
+
* Without it the plugin only reports: a question whose answer has nowhere to go is not asked.
|
|
120
|
+
*/
|
|
121
|
+
readonly router?: PermissionRouter;
|
|
122
|
+
/**
|
|
123
|
+
* How long the owner may take to answer, in milliseconds.
|
|
124
|
+
*
|
|
125
|
+
* A question nobody answers in time ends as expired, and the side is told "deny" for it.
|
|
126
|
+
* Without this the question waits for ever.
|
|
127
|
+
*/
|
|
128
|
+
readonly permissionTimeoutMs?: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The plugin every harness kind with an insert uses. */
|
|
132
|
+
export function insertPlugin(options: InsertPluginOptions = {}): Plugin {
|
|
133
|
+
const router = options.router;
|
|
134
|
+
const timeoutMs = options.permissionTimeoutMs;
|
|
135
|
+
return plugin("insert", (api) => {
|
|
136
|
+
for (const type of INSERT_RECORDS) api.declare(type);
|
|
137
|
+
|
|
138
|
+
api.action(
|
|
139
|
+
action(INSERT_REPORT_ACTION, {
|
|
140
|
+
description:
|
|
141
|
+
"Report what a harness did inside itself: a nested session was spawned, the context was compacted, the owner opened a session by hand, a tool asks for permission, or a session ended.",
|
|
142
|
+
input: InsertEventSchema,
|
|
143
|
+
output: InsertReportOutputSchema,
|
|
144
|
+
// The record is written under the id of the operation, so a report that was cut off
|
|
145
|
+
// may be made again: the same record is found instead of a second one.
|
|
146
|
+
recovery: "repeat",
|
|
147
|
+
effect: "record",
|
|
148
|
+
world: "niwa",
|
|
149
|
+
run: async (event, ctx): Promise<InsertReportOutput> => {
|
|
150
|
+
const written = await write(ctx, event);
|
|
151
|
+
return { reported: true, record: written.id };
|
|
152
|
+
},
|
|
153
|
+
}),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
// Without a routing there is nowhere to carry the answer, so the question stays a record.
|
|
157
|
+
if (router === undefined) return;
|
|
158
|
+
|
|
159
|
+
// The question reaches the owner as a request, and the answer comes back through the
|
|
160
|
+
// routing to the side that asked.
|
|
161
|
+
api.on(PermissionAsked, async (data, step) => {
|
|
162
|
+
const side = router.sideOf(data.session);
|
|
163
|
+
// Nobody can carry the answer: the question stays a record, and nobody waits for it.
|
|
164
|
+
if (side === undefined) return;
|
|
165
|
+
const event: PermissionAskedEvent = { kind: "permission-asked", ...data };
|
|
166
|
+
router.ask(side, event);
|
|
167
|
+
|
|
168
|
+
const id = permissionId(data.session, data.requestId);
|
|
169
|
+
const answer = await askOwner(step, router, event, id, await deadlineOf(step, timeoutMs));
|
|
170
|
+
if (answer === undefined) return;
|
|
171
|
+
|
|
172
|
+
const carried: PermissionAnswer = {
|
|
173
|
+
requestId: data.requestId,
|
|
174
|
+
allow: "ids" in answer && answer.ids[0] === ALLOW,
|
|
175
|
+
};
|
|
176
|
+
await router.answer(data.session, carried);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The question for the owner, and the answer to the side when the question ends without one.
|
|
183
|
+
*
|
|
184
|
+
* A request that is cancelled or has expired ends the wait with a typed error of its own. The
|
|
185
|
+
* side that asked is still waiting for an answer, so the side is told "deny" and why: nobody
|
|
186
|
+
* waits for ever, and the routing keeps no pending question.
|
|
187
|
+
*
|
|
188
|
+
* This module does not import the requests module, so the two errors are told apart by their
|
|
189
|
+
* name. Anything else is not theirs and is thrown again.
|
|
190
|
+
*/
|
|
191
|
+
async function askOwner(
|
|
192
|
+
step: Step,
|
|
193
|
+
router: PermissionRouter,
|
|
194
|
+
event: PermissionAskedEvent,
|
|
195
|
+
id: PermissionId,
|
|
196
|
+
deadline: Timestamp | undefined,
|
|
197
|
+
): Promise<Answer | undefined> {
|
|
198
|
+
try {
|
|
199
|
+
const asked = await step<RequestAskInput, RequestAskOutput>(
|
|
200
|
+
REQUEST_ASK,
|
|
201
|
+
{
|
|
202
|
+
to: { kind: "owner" },
|
|
203
|
+
question: PERMISSION_QUESTION,
|
|
204
|
+
input: permissionQuestion(event),
|
|
205
|
+
...(deadline === undefined ? {} : { deadline }),
|
|
206
|
+
},
|
|
207
|
+
// The step is named by the question itself: the same question is the same step, however
|
|
208
|
+
// many times the report of it is made.
|
|
209
|
+
{ key: id },
|
|
210
|
+
);
|
|
211
|
+
return asked.answer;
|
|
212
|
+
} catch (error) {
|
|
213
|
+
const reason = reasonOf(error);
|
|
214
|
+
if (reason === undefined) throw error;
|
|
215
|
+
// The question ended without an answer, so the side is told "deny" and why it ended. The
|
|
216
|
+
// routing forgets the question: nobody waits for an answer that will never come.
|
|
217
|
+
await router.answer(event.session, {
|
|
218
|
+
requestId: event.requestId,
|
|
219
|
+
allow: false,
|
|
220
|
+
reason,
|
|
221
|
+
});
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* When the question stops waiting, read as a step so a replay reads the same moment again.
|
|
228
|
+
*
|
|
229
|
+
* Without a timeout there is no deadline, and the question waits for ever.
|
|
230
|
+
*/
|
|
231
|
+
async function deadlineOf(step: Step, timeoutMs: number | undefined): Promise<Timestamp | undefined> {
|
|
232
|
+
if (timeoutMs === undefined) return undefined;
|
|
233
|
+
const now = await step.now();
|
|
234
|
+
return timestamp(now + timeoutMs);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Why the wait ended without an answer: "cancelled" or "expired"; nothing else is ours. */
|
|
238
|
+
function reasonOf(error: unknown): "cancelled" | "expired" | undefined {
|
|
239
|
+
if (!(error instanceof Error)) return undefined;
|
|
240
|
+
if (error.name === "RequestCancelled") return "cancelled";
|
|
241
|
+
if (error.name === "RequestExpired") return "expired";
|
|
242
|
+
return undefined;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function write(ctx: ActionContext, event: InsertEvent) {
|
|
246
|
+
switch (event.kind) {
|
|
247
|
+
case "nested-spawned":
|
|
248
|
+
return ctx.emit(NestedSpawned, withoutKind(event));
|
|
249
|
+
case "compacted":
|
|
250
|
+
return ctx.emit(Compacted, withoutKind(event));
|
|
251
|
+
case "opened-by-hand":
|
|
252
|
+
return ctx.emit(OpenedByHand, withoutKind(event));
|
|
253
|
+
case "permission-asked":
|
|
254
|
+
return ctx.emit(PermissionAsked, withoutKind(event));
|
|
255
|
+
case "session-ended":
|
|
256
|
+
return ctx.emit(SessionEndedByHarness, withoutKind(event));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function withoutKind<E extends { kind: string }>(event: E): Omit<E, "kind"> {
|
|
261
|
+
const { kind: _kind, ...data } = event;
|
|
262
|
+
return data;
|
|
263
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Routes a permission answer to the insert side that asked the question.
|
|
3
|
+
*
|
|
4
|
+
* A question is identified by its harness, its session and the harness's own request id, so two
|
|
5
|
+
* harnesses that reuse an id never receive each other's answers. An answer is delivered once.
|
|
6
|
+
*
|
|
7
|
+
* The router holds the live routing only: which side to call. It keeps no answer of its own and
|
|
8
|
+
* no record of what was decided — the journal holds that.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
import type { HarnessId, HarnessInsert, PermissionAnswer, PermissionAskedEvent, SessionId } from "@niwa-labs/harness";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The id of a permission question.
|
|
17
|
+
*
|
|
18
|
+
* It is a function of the harness, the session and the harness's own request id, so the same
|
|
19
|
+
* question is the same id wherever it is named — the request that asks the owner, the step that
|
|
20
|
+
* waits for the answer and the routing that carries it.
|
|
21
|
+
*/
|
|
22
|
+
export const PermissionIdSchema = z.string().min(1).brand<"PermissionId">();
|
|
23
|
+
export type PermissionId = z.output<typeof PermissionIdSchema>;
|
|
24
|
+
|
|
25
|
+
export class PermissionRouter {
|
|
26
|
+
/** The side of each harness: it carries the answers to the questions that harness asked. */
|
|
27
|
+
private readonly sides = new Map<HarnessId, HarnessInsert>();
|
|
28
|
+
/** The side that waits for each question. */
|
|
29
|
+
private readonly asked = new Map<PermissionId, HarnessInsert>();
|
|
30
|
+
|
|
31
|
+
/** The side of a harness. It is asked the questions of that harness and answers them. */
|
|
32
|
+
register(harness: HarnessId, side: HarnessInsert): void {
|
|
33
|
+
this.sides.set(harness, side);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The side that serves a session, when there is one. */
|
|
37
|
+
sideOf(session: SessionId): HarnessInsert | undefined {
|
|
38
|
+
return this.sides.get(session.harness);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Remember which side asked. */
|
|
42
|
+
ask(side: HarnessInsert, event: PermissionAskedEvent): void {
|
|
43
|
+
this.asked.set(permissionId(event.session, event.requestId), side);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Deliver the answer to the side that asked; refuse a question nobody is waiting on. */
|
|
47
|
+
async answer(session: SessionId, answer: PermissionAnswer): Promise<void> {
|
|
48
|
+
const key = permissionId(session, answer.requestId);
|
|
49
|
+
const side = this.asked.get(key);
|
|
50
|
+
if (side === undefined) {
|
|
51
|
+
throw new Error(`no pending permission question "${answer.requestId}" in ${session.harness}`);
|
|
52
|
+
}
|
|
53
|
+
if (side.answerPermission === undefined) {
|
|
54
|
+
throw new Error(`the side that asked "${answer.requestId}" cannot carry answers`);
|
|
55
|
+
}
|
|
56
|
+
this.asked.delete(key);
|
|
57
|
+
await side.answerPermission(answer);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The id of a permission question: the harness, the session and the harness's own request id.
|
|
63
|
+
*
|
|
64
|
+
* It is the same id wherever the question is named — the request that asks the owner, the step
|
|
65
|
+
* that waits for the answer and the routing that carries it — so the journal says which question
|
|
66
|
+
* a record is about.
|
|
67
|
+
*/
|
|
68
|
+
export function permissionId(session: SessionId, requestId: string): PermissionId {
|
|
69
|
+
return PermissionIdSchema.parse(
|
|
70
|
+
[session.harness, session.native, requestId].map(part).join(":"),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* One part of an id.
|
|
76
|
+
*
|
|
77
|
+
* A part may hold anything the harness calls a session or a request, so what would break the id
|
|
78
|
+
* is written as it is: the separator of the id, and what a step key and a record id refuse.
|
|
79
|
+
*/
|
|
80
|
+
function part(value: string): string {
|
|
81
|
+
return value.replace(/[^A-Za-z0-9._-]/g, (char) =>
|
|
82
|
+
`%${char.charCodeAt(0).toString(16).padStart(2, "0")}`,
|
|
83
|
+
);
|
|
84
|
+
}
|
package/src/life.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signs of life: what is known about a session right now.
|
|
3
|
+
*
|
|
4
|
+
* A session is `running`, `idle`, `ended` or `unknown`. After a host restart a turn that was
|
|
5
|
+
* open before the restart proves nothing about now, so the state is `unknown` until a record
|
|
6
|
+
* written by the current process says otherwise. A session is ended while the last word about
|
|
7
|
+
* it is an ended record: a session that was resumed after its control side stopped speaks again.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { StoredRecord, Timestamp } from "@niwa-labs/core";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
SessionActivity,
|
|
14
|
+
SessionAdvisor,
|
|
15
|
+
SessionEnded,
|
|
16
|
+
SessionStarted,
|
|
17
|
+
SessionTurnEnded,
|
|
18
|
+
SessionTurnStarted,
|
|
19
|
+
SessionUnknownVariant,
|
|
20
|
+
} from "./records.ts";
|
|
21
|
+
|
|
22
|
+
/** What is known about a session right now. */
|
|
23
|
+
export type LifeState = "running" | "idle" | "ended" | "unknown";
|
|
24
|
+
|
|
25
|
+
/** The record types that say something about life, as opposed to belonging. */
|
|
26
|
+
export const LIFE_TYPES = [
|
|
27
|
+
SessionStarted.name,
|
|
28
|
+
SessionTurnStarted.name,
|
|
29
|
+
SessionTurnEnded.name,
|
|
30
|
+
SessionActivity.name,
|
|
31
|
+
SessionAdvisor.name,
|
|
32
|
+
SessionUnknownVariant.name,
|
|
33
|
+
SessionEnded.name,
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The state of one session from its life records, in journal order.
|
|
38
|
+
*
|
|
39
|
+
* - `ended` when the newest record is an ended one;
|
|
40
|
+
* - no records: `unknown`;
|
|
41
|
+
* - the last turn-started has no later turn-ended: the turn is open, and it is `running` only
|
|
42
|
+
* when the newest record was written at or after `bootedAt`, the moment this process began;
|
|
43
|
+
* otherwise nothing says what became of the turn, and the state is `unknown`;
|
|
44
|
+
* - otherwise `idle`.
|
|
45
|
+
*/
|
|
46
|
+
export function lifeOf(records: readonly StoredRecord[], bootedAt: Timestamp): LifeState {
|
|
47
|
+
const last = records[records.length - 1];
|
|
48
|
+
if (last === undefined) return "unknown";
|
|
49
|
+
if (last.type === SessionEnded.name) return "ended";
|
|
50
|
+
|
|
51
|
+
let opened = -1;
|
|
52
|
+
let closed = -1;
|
|
53
|
+
for (const record of records) {
|
|
54
|
+
if (record.type === SessionTurnStarted.name) opened = record.seq;
|
|
55
|
+
if (record.type === SessionTurnEnded.name) closed = record.seq;
|
|
56
|
+
}
|
|
57
|
+
if (opened <= closed) return "idle";
|
|
58
|
+
return last.at >= bootedAt ? "running" : "unknown";
|
|
59
|
+
}
|
package/src/prompt.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The prompt as sections.
|
|
3
|
+
*
|
|
4
|
+
* A prompt is sections with an id and a tier — `constant`, `stable`, `volatile` — so several
|
|
5
|
+
* plugins can contribute without breaking each other's cache. The shape is the harness's
|
|
6
|
+
* (`@niwa-labs/harness`); this module turns what an action's input takes into it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
import { PromptSchema, promptFromText } from "@niwa-labs/harness";
|
|
12
|
+
import type { Prompt } from "@niwa-labs/harness";
|
|
13
|
+
|
|
14
|
+
/** What an action's input takes: plain text, or sections. It leaves the schema as sections. */
|
|
15
|
+
export const PromptInputSchema = z
|
|
16
|
+
.union([z.string(), PromptSchema])
|
|
17
|
+
.transform((input, ctx): Prompt => {
|
|
18
|
+
if (typeof input === "string") return promptFromText(input);
|
|
19
|
+
const ids = new Set<string>();
|
|
20
|
+
for (const section of input.sections) {
|
|
21
|
+
if (ids.has(section.id)) {
|
|
22
|
+
ctx.addIssue({
|
|
23
|
+
code: "custom",
|
|
24
|
+
message: `the prompt has two sections with the id "${section.id}"`,
|
|
25
|
+
});
|
|
26
|
+
return z.NEVER;
|
|
27
|
+
}
|
|
28
|
+
ids.add(section.id);
|
|
29
|
+
}
|
|
30
|
+
return input;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export type PromptInput = z.input<typeof PromptInputSchema>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The prompt of whatever the caller gave.
|
|
37
|
+
*
|
|
38
|
+
* Plain text becomes one volatile section in the input schema, before any intercept sees it, so everything
|
|
39
|
+
* downstream deals with sections only. Sections must have distinct ids: an intercept that
|
|
40
|
+
* replaces a section has to be able to say which one it means.
|
|
41
|
+
*/
|
|
42
|
+
export function promptFrom(input: PromptInput): Prompt {
|
|
43
|
+
if (typeof input === "string") return promptFromText(input);
|
|
44
|
+
const ids = new Set<string>();
|
|
45
|
+
for (const section of input.sections) {
|
|
46
|
+
if (ids.has(section.id)) throw new TypeError(`the prompt has two sections with the id "${section.id}"`);
|
|
47
|
+
ids.add(section.id);
|
|
48
|
+
}
|
|
49
|
+
return input;
|
|
50
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The records entry of the sessions module: what another plugin may import from it. The records,
|
|
3
|
+
* the typed references of the actions, the names of the questions it asks and the pure helpers
|
|
4
|
+
* that read a session's identity or life; no factory and nothing that runs. The actions import the
|
|
5
|
+
* records, so this file sits above both and neither imports it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
ActivityToolSchema,
|
|
10
|
+
BelongingSource,
|
|
11
|
+
DeliveryReceiptSchema,
|
|
12
|
+
SESSION_SEND,
|
|
13
|
+
SESSION_TYPES,
|
|
14
|
+
SessionActivity,
|
|
15
|
+
SessionAdvisor,
|
|
16
|
+
SessionBelongs,
|
|
17
|
+
SessionBoxManifest,
|
|
18
|
+
SessionCompacted,
|
|
19
|
+
SessionEnded,
|
|
20
|
+
SessionMarked,
|
|
21
|
+
SessionModel,
|
|
22
|
+
SessionStarted,
|
|
23
|
+
SessionStartReserved,
|
|
24
|
+
SessionTurnEnded,
|
|
25
|
+
SessionTurnStarted,
|
|
26
|
+
SessionUnknownVariant,
|
|
27
|
+
TurnEnding,
|
|
28
|
+
sameAddress,
|
|
29
|
+
} from "./records.ts";
|
|
30
|
+
export type { DeliveryReceipt, SessionSendInput, SessionSendOutput } from "./records.ts";
|
|
31
|
+
export {
|
|
32
|
+
BelongingSchema,
|
|
33
|
+
OpenQuestionSchema,
|
|
34
|
+
SessionEntrySchema,
|
|
35
|
+
SessionRefInput,
|
|
36
|
+
UnreadableSchema,
|
|
37
|
+
harnessGet,
|
|
38
|
+
harnessList,
|
|
39
|
+
sessionAsk,
|
|
40
|
+
sessionBelong,
|
|
41
|
+
sessionGet,
|
|
42
|
+
sessionEnd,
|
|
43
|
+
sessionInterrupt,
|
|
44
|
+
sessionList,
|
|
45
|
+
sessionOpenQuestions,
|
|
46
|
+
sessionRead,
|
|
47
|
+
sessionResume,
|
|
48
|
+
sessionRunning,
|
|
49
|
+
sessionSearch,
|
|
50
|
+
sessionSend,
|
|
51
|
+
sessionStart,
|
|
52
|
+
} from "./actions.ts";
|
|
53
|
+
export { SESSION_QUESTION, SUBMIT_ANSWER_ACTION } from "./ask.ts";
|
|
54
|
+
export { NiwaSessionIdSchema, SessionRefSchema, sessionOf, sessionRef } from "./identity.ts";
|
|
55
|
+
export type { NiwaSessionId, SessionRef } from "./identity.ts";
|
|
56
|
+
export { lifeOf } from "./life.ts";
|
|
57
|
+
export type { LifeState } from "./life.ts";
|
|
58
|
+
export {
|
|
59
|
+
REVERSE_QUESTION,
|
|
60
|
+
REVERSE_TYPES,
|
|
61
|
+
ReverseIdSchema,
|
|
62
|
+
SessionReverseAnswered,
|
|
63
|
+
SessionReverseAsked,
|
|
64
|
+
SessionReverseCancelled,
|
|
65
|
+
} from "./reverse.ts";
|
|
66
|
+
export type { ReverseId } from "./reverse.ts";
|
|
67
|
+
export {
|
|
68
|
+
HARNESS_DISCOVER_ACTION,
|
|
69
|
+
HarnessDiscoverOutputSchema,
|
|
70
|
+
HarnessProposed,
|
|
71
|
+
harnessDiscover,
|
|
72
|
+
proposalIdOf,
|
|
73
|
+
} from "./discover.ts";
|
|
74
|
+
export type { HarnessDiscoverOutput } from "./discover.ts";
|
|
75
|
+
export {
|
|
76
|
+
HARNESS_CONFIRM_QUESTION,
|
|
77
|
+
HarnessConfirmAnswerSchema,
|
|
78
|
+
HarnessConfirmed,
|
|
79
|
+
} from "./confirm.ts";
|
|
80
|
+
export type { HarnessConfirmAnswer } from "./confirm.ts";
|
|
81
|
+
export { harnessSupports } from "./support.ts";
|
|
82
|
+
export {
|
|
83
|
+
Compacted,
|
|
84
|
+
InsertReportOutputSchema,
|
|
85
|
+
NestedSpawned,
|
|
86
|
+
OpenedByHand,
|
|
87
|
+
PERMISSION_QUESTION,
|
|
88
|
+
PermissionAsked,
|
|
89
|
+
SessionEndedByHarness,
|
|
90
|
+
} from "./insert-plugin.ts";
|
|
91
|
+
export type { InsertReportOutput } from "./insert-plugin.ts";
|