@niwa-labs/agent 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 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,3 @@
1
- # Temporary Holding Version
1
+ # @niwa-labs/agent
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Agents as a shared entity: an address, a definition, one main session, a mailbox, memory, tasks, and attachment of external sessions with briefing and digests.
package/package.json CHANGED
@@ -1,6 +1,21 @@
1
1
  {
2
2
  "name": "@niwa-labs/agent",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.0",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./src/index.ts",
8
+ "./records": "./src/records.ts"
9
+ },
10
+ "dependencies": {
11
+ "@niwa-labs/core": "0.0.0",
12
+ "@niwa-labs/history": "0.0.0",
13
+ "@niwa-labs/messages": "0.0.0",
14
+ "@niwa-labs/plugin": "0.0.0",
15
+ "@niwa-labs/requests": "0.0.0",
16
+ "@niwa-labs/script": "0.0.0",
17
+ "@niwa-labs/sessions": "0.0.0",
18
+ "@niwa-labs/tasks": "0.0.0",
19
+ "zod": "^4.6.7"
20
+ }
21
+ }
package/src/digest.ts ADDED
@@ -0,0 +1,93 @@
1
+ import { RECORD_DATA_LIMIT } from "@niwa-labs/core";
2
+ import type { ActionContext, ArtifactStore } from "@niwa-labs/core";
3
+ import { sessionRead } from "@niwa-labs/sessions/records";
4
+ import type { agentDigestRead } from "./records.ts";
5
+
6
+ type Input = ReturnType<typeof agentDigestRead.input.parse>;
7
+ type Read = ReturnType<typeof sessionRead.output.parse>;
8
+ type Part = { upto?: string; text?: string; more: boolean };
9
+
10
+ type Entry = Extract<NonNullable<Read["digest"]>["view"], { mode: "digest" }>["entries"][number];
11
+
12
+ /** About how many tokens one window holds, and how many windows one part reads at most. */
13
+ const BUDGET = 8_000;
14
+ const WINDOWS = 6;
15
+
16
+ /** The line an entry is told as; a thought or a sealed record says nothing to the agent. */
17
+ function line(entry: Entry): string | undefined {
18
+ switch (entry.kind) {
19
+ case "user":
20
+ return `The owner: ${entry.text ?? ""}`;
21
+ case "assistant":
22
+ return `The session: ${entry.text ?? ""}`;
23
+ case "tool":
24
+ return `- ${entry.name ?? "a tool"}${entry.target === undefined ? "" : ` ${entry.target}`}: ${entry.status ?? ""}`;
25
+ case "compaction":
26
+ return `The session compacted its context: ${entry.summary ?? ""}`;
27
+ case "checklist":
28
+ return `Checklist: ${String(entry.done ?? 0)} of ${String(entry.total ?? 0)} done`;
29
+ case "permission":
30
+ return `Permission for ${entry.tool ?? "a tool"}: ${entry.outcome ?? ""}`;
31
+ default:
32
+ return undefined;
33
+ }
34
+ }
35
+
36
+ function readable(read: Read): boolean {
37
+ return read.coverage.status !== "failed" && read.coverage.status !== "unavailable";
38
+ }
39
+
40
+ /** A complete tail read can set a fresh boundary even when its transcript is empty. */
41
+ async function boundary(session: string, ctx: ActionContext): Promise<Part | undefined> {
42
+ const read = await ctx.step(sessionRead, { session, window: { limit: 1 } });
43
+ if (!readable(read)) return undefined;
44
+ const upto = read.records.at(-1)?.locator;
45
+ if (upto === undefined && read.coverage.status !== "empty") return undefined;
46
+ return { ...(upto === undefined ? {} : { upto }), more: false };
47
+ }
48
+
49
+ /** Read one part. A fact leaves room for its envelope and the session prompt that will carry it. */
50
+ export async function readDigest(input: Input, ctx: ActionContext, artifacts: ArtifactStore): Promise<Part | undefined> {
51
+ const { session, from, ended } = input;
52
+ if (input.pending === true) return boundary(session, ctx);
53
+ const event = ended ? "The session ended." : "The session paused.";
54
+ const lines: string[] = [];
55
+ let upto = from;
56
+ let files: NonNullable<Read["digest"]>["files"] | undefined;
57
+ let more = false;
58
+ let bytes = 0;
59
+ const limit = RECORD_DATA_LIMIT / 2;
60
+ for (let window = 0; window < WINDOWS; window++) {
61
+ const read = await ctx.step(sessionRead, { session, mode: "digest", budget: BUDGET, ...(upto === undefined ? {} : { from: upto }), ...(files === undefined ? {} : { files }) });
62
+ const digest = read.digest;
63
+ if (!readable(read) || digest?.view.mode !== "digest") break;
64
+ if (digest.restarted === true) {
65
+ const tail = await boundary(session, ctx);
66
+ if (tail === undefined) return undefined;
67
+ return { ...tail, text: `${event} Its history started over. Read the transcript with session.read of ${session}.` };
68
+ }
69
+ if (digest.upto === null || digest.upto === upto) break;
70
+ const words = digest.view.entries.map(line).filter((value): value is string => value !== undefined).join("\n");
71
+ const size = Buffer.byteLength(JSON.stringify(words));
72
+ if (words && bytes + size > limit && lines.length > 0) {
73
+ more = true;
74
+ break;
75
+ }
76
+ if (words && size > limit) {
77
+ const artifact = artifacts.put(words, "text/plain;charset=utf-8");
78
+ lines.push(`The transcript excerpt is in artifact ${artifact.digest} (${artifact.size} bytes).`);
79
+ } else if (words) {
80
+ lines.push(words);
81
+ }
82
+ bytes += size;
83
+ upto = digest.upto;
84
+ files = digest.files;
85
+ more = digest.next !== null;
86
+ if (!more) break;
87
+ }
88
+ if (upto === from || upto === undefined) return undefined;
89
+ if (lines.length === 0) return { upto, more };
90
+ const head = `${event} What it did${from === undefined ? "" : " since the last digest"}:`;
91
+ if (more) lines.push("(It did more after this: the rest comes with the next digest.)");
92
+ return { upto, text: [head, ...lines].join("\n"), more };
93
+ }