@operad-io/chat 0.1.0 → 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/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@operad-io/
|
|
10
|
-
"@operad-io/
|
|
9
|
+
"@operad-io/access-match": "0.2.0",
|
|
10
|
+
"@operad-io/engine": "0.2.0",
|
|
11
|
+
"@operad-io/harness-process": "0.2.0"
|
|
11
12
|
},
|
|
12
13
|
"description": "operad: the chat plugin",
|
|
13
14
|
"repository": {
|
|
@@ -15,6 +16,10 @@
|
|
|
15
16
|
"url": "git+https://github.com/operad-io/operad.git",
|
|
16
17
|
"directory": "packages/chat"
|
|
17
18
|
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@operad-io/harness-claude": "0.2.0",
|
|
21
|
+
"@operad-io/harness-pi": "0.2.0"
|
|
22
|
+
},
|
|
18
23
|
"license": "MIT",
|
|
19
24
|
"files": [
|
|
20
25
|
"src",
|
package/src/command.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/** Slash commands in a chat: their names, their arguments and the line each leaves. */
|
|
2
|
+
|
|
3
|
+
import { messageOf, type ActionArgSchema, type ActionArgType, type ActionContext, type ActionInfo } from "@operad-io/engine";
|
|
4
|
+
import { CHAT_TYPE } from "./model.ts";
|
|
5
|
+
|
|
6
|
+
/** A message that is a command: `/<name>` and the rest of the text. */
|
|
7
|
+
export interface ParsedCommand {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
/** The command as typed, without surrounding whitespace. */
|
|
10
|
+
readonly text: string;
|
|
11
|
+
/** What follows the name, without the whitespace that separated it. */
|
|
12
|
+
readonly rest: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A command an action declares, with the name this chat types it by. */
|
|
16
|
+
export interface ChatCommand {
|
|
17
|
+
readonly info: ActionInfo;
|
|
18
|
+
readonly name: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** What running a command gives: its line, and whether the action ran. */
|
|
22
|
+
export interface CommandOutcome {
|
|
23
|
+
readonly outcome: string;
|
|
24
|
+
readonly ok: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const COMMAND = /^\s*\/([A-Za-z0-9_-]+)(?:\s+([\s\S]*))?$/;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The command a message is, or undefined when it is an ordinary message. Leading
|
|
31
|
+
* whitespace is ignored; a slash later in the text, or an unknown name, leaves
|
|
32
|
+
* it a message.
|
|
33
|
+
*/
|
|
34
|
+
export function parseCommand(text: string): ParsedCommand | undefined {
|
|
35
|
+
const match = COMMAND.exec(text);
|
|
36
|
+
const name = match?.[1];
|
|
37
|
+
if (name === undefined) return undefined;
|
|
38
|
+
return { name, text: text.trim(), rest: match?.[2] ?? "" };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The name each declared command is typed by, with the clash rule applied. */
|
|
42
|
+
function resolvedNames(actions: readonly ActionInfo[]): ReadonlyMap<string, string> {
|
|
43
|
+
const groups = new Map<string, ActionInfo[]>();
|
|
44
|
+
for (const info of actions) {
|
|
45
|
+
const group = groups.get(info.action) ?? [];
|
|
46
|
+
group.push(info);
|
|
47
|
+
groups.set(info.action, group);
|
|
48
|
+
}
|
|
49
|
+
const names = new Map<string, string>();
|
|
50
|
+
for (const [short, group] of groups) {
|
|
51
|
+
for (const info of group) {
|
|
52
|
+
const name =
|
|
53
|
+
group.length === 1 || info.plugin === "chat"
|
|
54
|
+
? short
|
|
55
|
+
: `${info.plugin}_${info.action}`;
|
|
56
|
+
names.set(`${info.plugin}:${info.action}`, name);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return names;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Every command this chat has, in the order the actions are declared. */
|
|
63
|
+
export function chatCommands(actions: readonly ActionInfo[]): readonly ChatCommand[] {
|
|
64
|
+
const declared = actions.filter(
|
|
65
|
+
(info) =>
|
|
66
|
+
info.on === CHAT_TYPE &&
|
|
67
|
+
typeof info.command === "string" &&
|
|
68
|
+
info.command.trim().length > 0,
|
|
69
|
+
);
|
|
70
|
+
const names = resolvedNames(declared);
|
|
71
|
+
return declared.map((info) => ({
|
|
72
|
+
info,
|
|
73
|
+
name: names.get(`${info.plugin}:${info.action}`) ?? info.action,
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The command a chat has by this typed name, or undefined. */
|
|
78
|
+
export function findCommand(
|
|
79
|
+
commands: readonly ChatCommand[],
|
|
80
|
+
name: string,
|
|
81
|
+
): ChatCommand | undefined {
|
|
82
|
+
return commands.find((command) => command.name === name);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function valueOf(text: string, schema: ActionArgSchema): unknown {
|
|
86
|
+
const type: ActionArgType | undefined = schema.type;
|
|
87
|
+
if (type === "number" || type === "integer") {
|
|
88
|
+
return /^-?(?:\d+|\d*\.\d+)$/.test(text) ? Number(text) : text;
|
|
89
|
+
}
|
|
90
|
+
if (type === "boolean") {
|
|
91
|
+
return text === "true" ? true : text === "false" ? false : text;
|
|
92
|
+
}
|
|
93
|
+
return text;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The arguments a command's text gives. They are positional in the order the
|
|
98
|
+
* action declares its properties: one property takes the rest of the line, and
|
|
99
|
+
* with more than one the last takes what is left.
|
|
100
|
+
*/
|
|
101
|
+
function argsOf(command: ChatCommand, rest: string): Record<string, unknown> {
|
|
102
|
+
const schema = command.info.args;
|
|
103
|
+
const properties = schema?.properties ?? {};
|
|
104
|
+
const names = Object.keys(properties);
|
|
105
|
+
const text = rest.trim();
|
|
106
|
+
if (names.length === 0) {
|
|
107
|
+
if (text.length > 0) throw new Error(`\`/${command.name}\` takes no text after it`);
|
|
108
|
+
return {};
|
|
109
|
+
}
|
|
110
|
+
const parts = text.length === 0 ? [] : text.split(/\s+/);
|
|
111
|
+
const args: Record<string, unknown> = {};
|
|
112
|
+
names.forEach((name, index) => {
|
|
113
|
+
const property = properties[name];
|
|
114
|
+
const last = index === names.length - 1;
|
|
115
|
+
const value =
|
|
116
|
+
last && parts.length >= names.length
|
|
117
|
+
? parts.slice(index).join(" ")
|
|
118
|
+
: parts[index];
|
|
119
|
+
if (value === undefined || property === undefined) return;
|
|
120
|
+
args[name] = valueOf(value, property);
|
|
121
|
+
});
|
|
122
|
+
return args;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** One line of what an action returned, whatever its shape. */
|
|
126
|
+
function outcomeText(value: unknown): string {
|
|
127
|
+
if (typeof value === "string") {
|
|
128
|
+
const text = value.trim();
|
|
129
|
+
return text.length === 0 ? "done" : text;
|
|
130
|
+
}
|
|
131
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
132
|
+
if (value === undefined || value === null) return "done";
|
|
133
|
+
return JSON.stringify(value);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Runs the command's action, or says why it did not run. An action that asks
|
|
138
|
+
* for confirmation runs only when the text after the name is `yes`; anything
|
|
139
|
+
* else refuses without changing anything.
|
|
140
|
+
*/
|
|
141
|
+
export async function runCommand(
|
|
142
|
+
ctx: ActionContext,
|
|
143
|
+
command: ChatCommand,
|
|
144
|
+
rest: string,
|
|
145
|
+
): Promise<CommandOutcome> {
|
|
146
|
+
try {
|
|
147
|
+
if (ctx.act === undefined) {
|
|
148
|
+
return { ok: false, outcome: "refused: commands cannot run here" };
|
|
149
|
+
}
|
|
150
|
+
if (command.info.confirm === true) {
|
|
151
|
+
if (rest.trim() !== "yes") {
|
|
152
|
+
return {
|
|
153
|
+
ok: false,
|
|
154
|
+
outcome: `send \`/${command.name} yes\` to go ahead`,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const result = await ctx.act(command.info.name, {}, { yes: true });
|
|
158
|
+
return { ok: true, outcome: outcomeText(result.value) };
|
|
159
|
+
}
|
|
160
|
+
const result = await ctx.act(command.info.name, argsOf(command, rest));
|
|
161
|
+
return { ok: true, outcome: outcomeText(result.value) };
|
|
162
|
+
} catch (error) {
|
|
163
|
+
return { ok: false, outcome: `refused: ${messageOf(error)}` };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** A skill `/help` lists: its name and the description from its `SKILL.md`. */
|
|
168
|
+
export interface HelpSkill {
|
|
169
|
+
readonly name: string;
|
|
170
|
+
readonly description?: string | undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* What `/help` says: every command this chat can run now, with its own
|
|
175
|
+
* description, then the skills it can type by name. A skill whose name is a
|
|
176
|
+
* command is left out: the command wins when it is typed.
|
|
177
|
+
*/
|
|
178
|
+
export function helpText(
|
|
179
|
+
commands: readonly ChatCommand[],
|
|
180
|
+
skills: readonly HelpSkill[] = [],
|
|
181
|
+
): string {
|
|
182
|
+
const available = commands.filter((command) => command.info.available !== false);
|
|
183
|
+
const lines = available.map(
|
|
184
|
+
(command) =>
|
|
185
|
+
`- \`/${command.name}\` — ${(command.info.command ?? "").replace(/\s+/g, " ").trim()}`,
|
|
186
|
+
);
|
|
187
|
+
const names = new Set(commands.map((command) => command.name));
|
|
188
|
+
const listed = skills.filter((skill) => !names.has(skill.name));
|
|
189
|
+
if (listed.length > 0) {
|
|
190
|
+
lines.push("", "Skills:");
|
|
191
|
+
for (const skill of listed) {
|
|
192
|
+
lines.push(
|
|
193
|
+
`- \`/${skill.name}\` — skill${skill.description === undefined ? "" : `: ${skill.description}`}`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return [`What you can type with \`/\` here:`, ...lines].join("\n");
|
|
198
|
+
}
|