@operad-io/chat 0.1.1 → 0.2.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/package.json +8 -3
- package/src/command.ts +198 -0
- package/src/index.ts +595 -66
- package/src/model.ts +791 -83
- package/src/notes.ts +194 -0
- package/src/queue.ts +93 -0
- package/src/reply.ts +1144 -122
- package/src/skills.ts +173 -0
- package/src/tail.ts +33 -8
- package/src/views.ts +1177 -90
package/src/skills.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The skills a chat can use: the ones its settings give it, read from their
|
|
3
|
+
* `SKILL.md` files the same way a harness reads them. A skill can be typed as
|
|
4
|
+
* `/name`; an operad command of the same name wins, and a harness that does not
|
|
5
|
+
* load skills itself gets the skill's body in the message instead.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
9
|
+
import { join, resolve } from "node:path";
|
|
10
|
+
import { parseObjectId, type StoredObject, type ViewSkill } from "@operad-io/engine";
|
|
11
|
+
import {
|
|
12
|
+
MAX_PROMPT_DESCRIPTION_CHARS,
|
|
13
|
+
readGivenSkills,
|
|
14
|
+
skillDescription,
|
|
15
|
+
} from "@operad-io/harness-process";
|
|
16
|
+
import { parseCommand } from "./command.ts";
|
|
17
|
+
import type { ChatData } from "./model.ts";
|
|
18
|
+
|
|
19
|
+
/** One skill of a chat, with the path of its `SKILL.md`. */
|
|
20
|
+
export interface ChatSkill {
|
|
21
|
+
readonly name: string;
|
|
22
|
+
/** The description from its `SKILL.md`, on one line and cut to the menu cap. */
|
|
23
|
+
readonly description?: string | undefined;
|
|
24
|
+
/** The directory that holds the `SKILL.md`. */
|
|
25
|
+
readonly path: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Reads an object as a view does, through its type's `prepare` when it has one. */
|
|
29
|
+
export type ObjectReader = (type: string, id: string) => Promise<StoredObject | undefined>;
|
|
30
|
+
|
|
31
|
+
/** Whether a path is a directory; a path that cannot be read is not one. */
|
|
32
|
+
export function isDirectory(path: string): boolean {
|
|
33
|
+
try {
|
|
34
|
+
return statSync(path).isDirectory();
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Whether a directory holds a skill's `SKILL.md`. */
|
|
41
|
+
export function hasSkill(path: string): boolean {
|
|
42
|
+
try {
|
|
43
|
+
return statSync(join(path, "SKILL.md")).isFile();
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Adds a path to a list once, under its resolved path. */
|
|
50
|
+
export function added(list: string[], path: string): void {
|
|
51
|
+
const normal = resolve(path);
|
|
52
|
+
if (!list.includes(normal)) list.push(normal);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The skills directly inside a directory: each child that holds a SKILL.md. */
|
|
56
|
+
export function skillsInside(parent: string): string[] {
|
|
57
|
+
if (!isDirectory(parent)) return [];
|
|
58
|
+
return readdirSync(parent, { withFileTypes: true })
|
|
59
|
+
.map((entry) => join(parent, entry.name))
|
|
60
|
+
.filter((path) => hasSkill(path))
|
|
61
|
+
.sort((left, right) => left.localeCompare(right));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The description on one line, cut at the same cap the harness prompt uses. */
|
|
65
|
+
function shortDescription(description: string): string | undefined {
|
|
66
|
+
const oneLine = description.replace(/\s+/g, " ").trim();
|
|
67
|
+
if (oneLine.length === 0) return undefined;
|
|
68
|
+
if (oneLine.length <= MAX_PROMPT_DESCRIPTION_CHARS) return oneLine;
|
|
69
|
+
return `${oneLine.slice(0, MAX_PROMPT_DESCRIPTION_CHARS).trimEnd()}...`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A skill file's body after its frontmatter, or the whole text when it has none. */
|
|
73
|
+
function bodyOf(content: string): string | undefined {
|
|
74
|
+
const text = content.charCodeAt(0) === 0xfeff ? content.slice(1) : content;
|
|
75
|
+
const lines = text.split(/\r?\n/);
|
|
76
|
+
if (lines[0]?.trim() !== "---") {
|
|
77
|
+
const body = text.trim();
|
|
78
|
+
return body.length === 0 ? undefined : body;
|
|
79
|
+
}
|
|
80
|
+
for (let index = 1; index < lines.length; index += 1) {
|
|
81
|
+
if (lines[index]?.trim() !== "---") continue;
|
|
82
|
+
const body = lines.slice(index + 1).join("\n").trim();
|
|
83
|
+
return body.length === 0 ? undefined : body;
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The skill directories a chat is given: its parent's when it names some, else its own. */
|
|
89
|
+
export async function chatSkillPaths(
|
|
90
|
+
chat: ChatData,
|
|
91
|
+
get: ObjectReader,
|
|
92
|
+
): Promise<readonly string[]> {
|
|
93
|
+
const parent = chat.parent;
|
|
94
|
+
if (parent !== undefined) {
|
|
95
|
+
const ref = parseObjectId(parent);
|
|
96
|
+
if (ref !== undefined) {
|
|
97
|
+
const owner = await get(ref.type, ref.id);
|
|
98
|
+
const skills = owner?.data.skills;
|
|
99
|
+
if (Array.isArray(skills)) {
|
|
100
|
+
return skills.filter((path): path is string => typeof path === "string");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return chat.skills;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Reads the skills directly from the directories a chat is given. */
|
|
108
|
+
export function readSkills(paths: readonly string[]): readonly ChatSkill[] {
|
|
109
|
+
return readGivenSkills(paths).skills.map((skill) => {
|
|
110
|
+
let description: string | undefined;
|
|
111
|
+
try {
|
|
112
|
+
const content = readFileSync(join(skill.path, "SKILL.md"), "utf8");
|
|
113
|
+
const found = skillDescription(content);
|
|
114
|
+
description = found === undefined ? undefined : shortDescription(found);
|
|
115
|
+
} catch {
|
|
116
|
+
description = undefined;
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
name: skill.name,
|
|
120
|
+
...(description === undefined ? {} : { description }),
|
|
121
|
+
path: skill.path,
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The skills a chat can use now, with its parent's current list when it has one. */
|
|
127
|
+
export async function readChatSkills(
|
|
128
|
+
chat: ChatData,
|
|
129
|
+
get: ObjectReader,
|
|
130
|
+
): Promise<readonly ChatSkill[]> {
|
|
131
|
+
return readSkills(await chatSkillPaths(chat, get));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The skills as a view carries them: names and descriptions, never file paths. */
|
|
135
|
+
export function skillSummaries(skills: readonly ChatSkill[]): readonly ViewSkill[] {
|
|
136
|
+
return skills.map((skill) => ({
|
|
137
|
+
name: skill.name,
|
|
138
|
+
...(skill.description === undefined ? {} : { description: skill.description }),
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The body of one skill, read from its `SKILL.md`. */
|
|
143
|
+
export function readSkillBody(skill: ChatSkill): string | undefined {
|
|
144
|
+
try {
|
|
145
|
+
return bodyOf(readFileSync(join(skill.path, "SKILL.md"), "utf8"));
|
|
146
|
+
} catch {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The message a harness receives for `/name rest`: unchanged for a harness that
|
|
153
|
+
* loads skills itself, and otherwise the skill's body after a line naming it,
|
|
154
|
+
* then the rest of the line. An unknown name stays an ordinary message.
|
|
155
|
+
*/
|
|
156
|
+
export function skillMessage(
|
|
157
|
+
text: string,
|
|
158
|
+
skills: readonly ChatSkill[],
|
|
159
|
+
loadsSkills: boolean,
|
|
160
|
+
): string {
|
|
161
|
+
if (loadsSkills) return text;
|
|
162
|
+
const parsed = parseCommand(text);
|
|
163
|
+
if (parsed === undefined) return text;
|
|
164
|
+
const skill = skills.find((item) => item.name === parsed.name);
|
|
165
|
+
if (skill === undefined) return text;
|
|
166
|
+
const body = readSkillBody(skill);
|
|
167
|
+
const rest = parsed.rest.trim();
|
|
168
|
+
return [
|
|
169
|
+
`The skill "${skill.name}" follows.`,
|
|
170
|
+
...(body === undefined ? [] : [body]),
|
|
171
|
+
...(rest.length === 0 ? [] : [rest]),
|
|
172
|
+
].join("\n\n");
|
|
173
|
+
}
|
package/src/tail.ts
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
|
|
9
9
|
import { closeSync, fstatSync, openSync, readSync } from "node:fs";
|
|
10
10
|
import { join } from "node:path";
|
|
11
|
-
import {
|
|
11
|
+
import { isRecord } from "@operad-io/engine";
|
|
12
|
+
import { READY_LINE, emptyFold, foldLines, type FoldEntry, type FoldState } from "@operad-io/harness-process";
|
|
12
13
|
|
|
13
14
|
/** How many bytes one read call takes from the file. */
|
|
14
15
|
export const TAIL_READ_CHUNK = 1024 * 1024;
|
|
@@ -31,6 +32,10 @@ export function agentLogPath(storeRoot: string, harness: string, runId: string):
|
|
|
31
32
|
export interface TailRead {
|
|
32
33
|
readonly state: FoldState;
|
|
33
34
|
readonly bytesRead: number;
|
|
35
|
+
/** When the last stamped line in the log was received, in epoch milliseconds. */
|
|
36
|
+
readonly lastAt?: number | undefined;
|
|
37
|
+
/** Whether the log has the ready line that says the agent session is open. */
|
|
38
|
+
readonly ready: boolean;
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
interface Saved {
|
|
@@ -41,6 +46,8 @@ interface Saved {
|
|
|
41
46
|
/** The partial last line read so far, kept so it is not read again. */
|
|
42
47
|
readonly pending: Buffer;
|
|
43
48
|
readonly state: FoldState;
|
|
49
|
+
readonly lastAt: number | undefined;
|
|
50
|
+
readonly ready: boolean;
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
/**
|
|
@@ -55,10 +62,15 @@ const saved = new Map<string, Saved>();
|
|
|
55
62
|
* The fold of a whole log file, reading only what was appended since the last
|
|
56
63
|
* call for the path; undefined when there is no file. A file that is shorter
|
|
57
64
|
* than what was read, or is another file, is read again from its start. A line
|
|
58
|
-
* still being written is kept as it is and completed by the next call. Finished
|
|
59
|
-
*
|
|
65
|
+
* still being written is kept as it is and completed by the next call. Finished
|
|
66
|
+
* entries are kept until `held` says the caller's facts hold them, so text or a
|
|
67
|
+
* call never leaves the tail before its fact is visible, and a long run does not
|
|
68
|
+
* keep every entry it ever finished.
|
|
60
69
|
*/
|
|
61
|
-
export function foldLogTail(
|
|
70
|
+
export function foldLogTail(
|
|
71
|
+
path: string,
|
|
72
|
+
held?: (entry: FoldEntry) => boolean,
|
|
73
|
+
): TailRead | undefined {
|
|
62
74
|
let fd: number;
|
|
63
75
|
try {
|
|
64
76
|
fd = openSync(path, "r");
|
|
@@ -73,6 +85,8 @@ export function foldLogTail(path: string): TailRead | undefined {
|
|
|
73
85
|
let pending = keep ? before.pending : Buffer.alloc(0);
|
|
74
86
|
let bytesRead = 0;
|
|
75
87
|
let position = keep ? before.offset : 0;
|
|
88
|
+
let lastAt = keep ? before.lastAt : undefined;
|
|
89
|
+
let ready = keep ? before.ready : false;
|
|
76
90
|
while (position < stat.size) {
|
|
77
91
|
const chunk = Buffer.allocUnsafe(Math.min(TAIL_READ_CHUNK, stat.size - position));
|
|
78
92
|
const count = readSync(fd, chunk, 0, chunk.length, position);
|
|
@@ -85,22 +99,33 @@ export function foldLogTail(path: string): TailRead | undefined {
|
|
|
85
99
|
const values: unknown[] = [];
|
|
86
100
|
for (const line of pending.subarray(0, end).toString("utf8").split("\n")) {
|
|
87
101
|
if (line.length === 0) continue;
|
|
102
|
+
let value: unknown;
|
|
88
103
|
try {
|
|
89
|
-
|
|
104
|
+
value = JSON.parse(line);
|
|
90
105
|
} catch {
|
|
91
106
|
// Not one of ours.
|
|
107
|
+
continue;
|
|
92
108
|
}
|
|
109
|
+
values.push(value);
|
|
110
|
+
if (!isRecord(value)) continue;
|
|
111
|
+
const at = value["receivedAt"];
|
|
112
|
+
if (typeof at === "number" && Number.isFinite(at)) lastAt = at;
|
|
113
|
+
if (value["type"] === READY_LINE) ready = true;
|
|
93
114
|
}
|
|
94
|
-
state =
|
|
115
|
+
state = foldLines(values, state);
|
|
95
116
|
pending = pending.subarray(end + 1);
|
|
96
117
|
}
|
|
118
|
+
if (held !== undefined) {
|
|
119
|
+
const entries = state.entries.filter((entry) => !held(entry));
|
|
120
|
+
if (entries.length !== state.entries.length) state = { ...state, entries };
|
|
121
|
+
}
|
|
97
122
|
saved.delete(path);
|
|
98
|
-
saved.set(path, { dev: stat.dev, ino: stat.ino, offset: position, pending, state });
|
|
123
|
+
saved.set(path, { dev: stat.dev, ino: stat.ino, offset: position, pending, state, lastAt, ready });
|
|
99
124
|
for (const old of saved.keys()) {
|
|
100
125
|
if (saved.size <= TAIL_CACHE_LOGS) break;
|
|
101
126
|
saved.delete(old);
|
|
102
127
|
}
|
|
103
|
-
return { state, bytesRead };
|
|
128
|
+
return { state, bytesRead, ...(lastAt === undefined ? {} : { lastAt }), ready };
|
|
104
129
|
} finally {
|
|
105
130
|
closeSync(fd);
|
|
106
131
|
}
|