@operad-io/chat 0.3.0 → 0.4.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 +6 -6
- package/src/deliver.ts +53 -0
- package/src/index.ts +184 -3
- package/src/model.ts +340 -13
- package/src/notes.ts +53 -0
- package/src/recap.ts +76 -0
- package/src/reply.ts +441 -22
- package/src/segments.ts +125 -0
- package/src/views.ts +254 -40
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@operad-io/access-match": "0.
|
|
10
|
-
"@operad-io/engine": "0.
|
|
11
|
-
"@operad-io/harness-process": "0.
|
|
9
|
+
"@operad-io/access-match": "0.4.0",
|
|
10
|
+
"@operad-io/engine": "0.4.0",
|
|
11
|
+
"@operad-io/harness-process": "0.4.0"
|
|
12
12
|
},
|
|
13
13
|
"description": "operad: the chat plugin",
|
|
14
14
|
"repository": {
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"directory": "packages/chat"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@operad-io/harness-claude": "0.
|
|
21
|
-
"@operad-io/harness-pi": "0.
|
|
20
|
+
"@operad-io/harness-claude": "0.4.0",
|
|
21
|
+
"@operad-io/harness-pi": "0.4.0"
|
|
22
22
|
},
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"files": [
|
package/src/deliver.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A result a plugin delivers into a chat (ADR 0026 D19): one message in the
|
|
3
|
+
* chat's record, headed with the routine's name, that starts no reply. It is
|
|
4
|
+
* sent to a bound channel like any message of the assistant, and the next
|
|
5
|
+
* message of the chat carries its summary to the model, the way a memory note
|
|
6
|
+
* rides (D18); the whole result stays in the run's record.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { FactWriter, ObjectStore } from "@operad-io/engine";
|
|
10
|
+
import { MESSAGE_FACT, allMessages, chatRef, type DeliveryData } from "./model.ts";
|
|
11
|
+
|
|
12
|
+
/** What a delivery is: the whole result, and what the model carries of it. */
|
|
13
|
+
export interface Delivery extends DeliveryData {
|
|
14
|
+
/** The whole result, shown in the chat and sent to a bound channel. */
|
|
15
|
+
readonly text: string;
|
|
16
|
+
/** Who delivers it, as the message records it: the plugin and the routine. */
|
|
17
|
+
readonly from: { readonly via: string; readonly id: string };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The message id a delivery of one run has, so a delivery repeated after a crash is recorded once. */
|
|
21
|
+
export function deliveryIdOf(runId: string): string {
|
|
22
|
+
return `delivery-${runId}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Records a delivery in chat `chatId`; false when the chat already holds it. */
|
|
26
|
+
export async function deliverResult(
|
|
27
|
+
ctx: { readonly facts: FactWriter; readonly store: ObjectStore },
|
|
28
|
+
chatId: string,
|
|
29
|
+
delivery: Delivery,
|
|
30
|
+
): Promise<boolean> {
|
|
31
|
+
const id = deliveryIdOf(delivery.runId);
|
|
32
|
+
if (allMessages(ctx.store, chatId).some((message) => message.id === id)) return false;
|
|
33
|
+
await ctx.facts.append({
|
|
34
|
+
type: MESSAGE_FACT,
|
|
35
|
+
object: chatRef(chatId),
|
|
36
|
+
data: {
|
|
37
|
+
id,
|
|
38
|
+
role: "delivery",
|
|
39
|
+
from: delivery.from,
|
|
40
|
+
text: delivery.text,
|
|
41
|
+
attachments: [],
|
|
42
|
+
source: { kind: delivery.from.via, ref: null },
|
|
43
|
+
delivery: {
|
|
44
|
+
routine: delivery.routine,
|
|
45
|
+
...(delivery.routineId === undefined || delivery.routineId.length === 0 ? {} : { routineId: delivery.routineId }),
|
|
46
|
+
runId: delivery.runId,
|
|
47
|
+
summary: delivery.summary,
|
|
48
|
+
...(delivery.failed === true ? { failed: true } : {}),
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
return true;
|
|
53
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -19,7 +19,9 @@ import {
|
|
|
19
19
|
accessLevelPath,
|
|
20
20
|
CLOSED_FACT,
|
|
21
21
|
COMMAND_FACT,
|
|
22
|
+
COMPACT_FACT,
|
|
22
23
|
costText,
|
|
24
|
+
currentSession,
|
|
23
25
|
effectiveEffort,
|
|
24
26
|
EFFORT_FACT,
|
|
25
27
|
firstOutputMs,
|
|
@@ -30,7 +32,11 @@ import {
|
|
|
30
32
|
MESSAGE_CHANGE_FACT,
|
|
31
33
|
attachmentOf,
|
|
32
34
|
MODEL_FACT,
|
|
35
|
+
NEW_FACT,
|
|
33
36
|
newMessageId,
|
|
37
|
+
PRIVATE_FACT,
|
|
38
|
+
PRIVATE_OFF_LINE,
|
|
39
|
+
PRIVATE_ON_LINE,
|
|
34
40
|
readChat,
|
|
35
41
|
STOP_FACT,
|
|
36
42
|
stoppedRunAfter,
|
|
@@ -52,6 +58,7 @@ import {
|
|
|
52
58
|
import { keepInterrupted, runReply, withLevelFileDeny, WORKTREE_ALLOW_ALWAYS, WORKTREE_ALLOW_ONCE, WORKTREE_CHOICES, WORKTREE_DENY, WORKTREE_REPLY_EVENT, WORKTREE_YES, worktreeChoiceInfo } from "./reply.ts";
|
|
53
59
|
import { syncQueuedLocked, withQueueLock } from "./queue.ts";
|
|
54
60
|
import { chatCommands, findCommand, helpText, parseCommand, runCommand } from "./command.ts";
|
|
61
|
+
import { AWAY_MINUTES_DEFAULT, recapIdOf, recapOf } from "./recap.ts";
|
|
55
62
|
import { readChatSkills } from "./skills.ts";
|
|
56
63
|
import { chatViews } from "./views.ts";
|
|
57
64
|
|
|
@@ -164,6 +171,8 @@ export type SendContext = Pick<ActionContext, "facts" | "emit" | "actor"> & {
|
|
|
164
171
|
readonly store?: ActionContext["store"] | undefined;
|
|
165
172
|
/** The operad home, for the queue lock. */
|
|
166
173
|
readonly home?: string | undefined;
|
|
174
|
+
/** Minutes without the person that count as away; a message after that gets a recap. Absent or 0: none. */
|
|
175
|
+
readonly awayMinutes?: number | undefined;
|
|
167
176
|
};
|
|
168
177
|
|
|
169
178
|
/**
|
|
@@ -185,9 +194,13 @@ export async function sendMessage(
|
|
|
185
194
|
const known = CHAT_SOURCES.find((name) => name === via);
|
|
186
195
|
const source = ctx.source ?? known ?? OTHER_SOURCE;
|
|
187
196
|
const id = newMessageId();
|
|
197
|
+
// A message of a stretch that is not remembered is marked when it is recorded.
|
|
198
|
+
const privateNow =
|
|
199
|
+
ctx.store !== undefined && chatData((await ctx.store.get(CHAT_TYPE, chatId))?.data ?? {}).private === true;
|
|
188
200
|
const message: MessageData = {
|
|
189
201
|
id,
|
|
190
202
|
role: "user",
|
|
203
|
+
...(privateNow ? { private: true as const } : {}),
|
|
191
204
|
from: { via: ctx.actor.via, id: ctx.actor.id },
|
|
192
205
|
text: note,
|
|
193
206
|
attachments: files.map((path) => attachmentOf(path)),
|
|
@@ -197,6 +210,20 @@ export async function sendMessage(
|
|
|
197
210
|
// The message is a fact first; the event is what starts the answer.
|
|
198
211
|
const record = async (): Promise<void> => {
|
|
199
212
|
await ctx.facts.append({ type: MESSAGE_FACT, object: chatRef(chatId), data: message });
|
|
213
|
+
if (ctx.store !== undefined && ctx.awayMinutes !== undefined && ctx.awayMinutes > 0) {
|
|
214
|
+
// The recap follows the message that shows the person is back, and no
|
|
215
|
+
// event starts from it; the model never gets it.
|
|
216
|
+
const read = readChat(ctx.store, chatId);
|
|
217
|
+
const current = read.messages.find((item) => item.id === id);
|
|
218
|
+
const text = current === undefined ? undefined : recapOf(read.messages, read.facts, current, ctx.awayMinutes * 60_000);
|
|
219
|
+
if (text !== undefined) {
|
|
220
|
+
await ctx.facts.append({
|
|
221
|
+
type: MESSAGE_FACT,
|
|
222
|
+
object: chatRef(chatId),
|
|
223
|
+
data: { id: recapIdOf(id), role: "recap", from: { via: "chat", id: "recap" }, text, attachments: [], source: { kind: "chat", ref: null } },
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
200
227
|
if (ctx.store !== undefined) await syncQueuedLocked(ctx.store, chatId, "action:chat/send");
|
|
201
228
|
};
|
|
202
229
|
if (ctx.home === undefined || ctx.store === undefined) await record();
|
|
@@ -259,7 +286,14 @@ const sendAction: ActionTypeDefinition = {
|
|
|
259
286
|
await ctx.facts.append({ type: COMMAND_FACT, object: chatRef(ctx.object.id), data });
|
|
260
287
|
return result.outcome;
|
|
261
288
|
}
|
|
262
|
-
|
|
289
|
+
const away = ctx.settings.awayMinutes;
|
|
290
|
+
return sendMessage(
|
|
291
|
+
{ ...ctx, awayMinutes: typeof away === "number" ? away : AWAY_MINUTES_DEFAULT },
|
|
292
|
+
ctx.object.id,
|
|
293
|
+
text,
|
|
294
|
+
input.ref,
|
|
295
|
+
files,
|
|
296
|
+
);
|
|
263
297
|
},
|
|
264
298
|
};
|
|
265
299
|
|
|
@@ -633,6 +667,104 @@ const closeAction: ActionTypeDefinition = {
|
|
|
633
667
|
},
|
|
634
668
|
};
|
|
635
669
|
|
|
670
|
+
const newAction: ActionTypeDefinition = {
|
|
671
|
+
name: "new",
|
|
672
|
+
on: CHAT_TYPE,
|
|
673
|
+
command: "Start a new agent session in this chat; the transcript stays. With a goal, the new session starts with a short brief of this one.",
|
|
674
|
+
args: {
|
|
675
|
+
type: "object",
|
|
676
|
+
properties: {
|
|
677
|
+
goal: {
|
|
678
|
+
type: "string",
|
|
679
|
+
description: "What comes next. The new session starts with a brief of this one for that goal.",
|
|
680
|
+
},
|
|
681
|
+
},
|
|
682
|
+
additionalProperties: false,
|
|
683
|
+
},
|
|
684
|
+
when: (state) => !chatData(state).closed,
|
|
685
|
+
why: (object) =>
|
|
686
|
+
chatData(object.data).closed ? `${object.id} is closed; it has no session to start` : undefined,
|
|
687
|
+
async run(ctx, args) {
|
|
688
|
+
const stored = await openChat(ctx, "it has no session to start");
|
|
689
|
+
const chat = chatData(stored.data);
|
|
690
|
+
const read = readChat(ctx.store, ctx.object.id);
|
|
691
|
+
const asked = (args as { readonly goal?: unknown } | undefined)?.goal;
|
|
692
|
+
const goal = typeof asked === "string" ? asked.trim() : "";
|
|
693
|
+
if (goal.length > 0 && ctx.emit === undefined) {
|
|
694
|
+
throw new Error("the brief cannot be made here; operad serve is not running");
|
|
695
|
+
}
|
|
696
|
+
const from = currentSession(chat, read.messages, read.facts);
|
|
697
|
+
if ((await writingReplyNow(read.facts, read.messages, read.entries, ctx.runs)) !== undefined) {
|
|
698
|
+
throw new Error(`chat ${ctx.object.id} is writing a reply; send /new once it ends or /stop it`);
|
|
699
|
+
}
|
|
700
|
+
await ctx.facts.append({
|
|
701
|
+
type: NEW_FACT,
|
|
702
|
+
object: chatRef(ctx.object.id),
|
|
703
|
+
data: {
|
|
704
|
+
by: { via: ctx.actor.via, id: ctx.actor.id },
|
|
705
|
+
...(goal.length === 0 ? {} : { goal }),
|
|
706
|
+
...(goal.length === 0 || from === undefined ? {} : { from }),
|
|
707
|
+
},
|
|
708
|
+
});
|
|
709
|
+
await changeChat(ctx, "action:chat/new", (data) => ({ ...data, session: null }));
|
|
710
|
+
// A kept worker holds the old session in memory; the next reply starts its own.
|
|
711
|
+
await endKeptWorker(ctx.home, chat, ctx.object.id);
|
|
712
|
+
if (goal.length > 0 && ctx.emit !== undefined) {
|
|
713
|
+
// A reply round builds the brief from the old session before anything else, so the
|
|
714
|
+
// brief is made even when no message follows.
|
|
715
|
+
await ctx.emit({
|
|
716
|
+
source: CHAT_SOURCES.find((name) => name === ctx.actor.via) ?? OTHER_SOURCE,
|
|
717
|
+
type: MESSAGE_EVENT,
|
|
718
|
+
key: `new:${newMessageId()}`,
|
|
719
|
+
object: `${CHAT_TYPE}:${ctx.object.id}`,
|
|
720
|
+
});
|
|
721
|
+
return "started a new session; a brief of this one for that goal is being made, and the next message goes to an agent that has the brief";
|
|
722
|
+
}
|
|
723
|
+
return "started a new session; the transcript stays and the next message goes to an agent with no earlier context";
|
|
724
|
+
},
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
/** Why `chat:private` is not offered; absent when it is. Only a bot chat has a memory to keep a stretch out of. */
|
|
728
|
+
function privateRefusal(chat: ChatData, id: string): string | undefined {
|
|
729
|
+
if (chat.closed) return `${id} is closed; it has no stretch to keep out of memory`;
|
|
730
|
+
if (chat.parent === undefined || !chat.parent.startsWith("bot:")) {
|
|
731
|
+
return "only a chat of a bot has a memory to keep a stretch out of";
|
|
732
|
+
}
|
|
733
|
+
return undefined;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
const privateAction: ActionTypeDefinition = {
|
|
737
|
+
name: "private",
|
|
738
|
+
on: CHAT_TYPE,
|
|
739
|
+
command:
|
|
740
|
+
"Start or end a stretch that is not remembered: messages stay in the chat but do not enter the bot's memory.",
|
|
741
|
+
when: (state) => privateRefusal(chatData(state), "") === undefined,
|
|
742
|
+
why: (object) => privateRefusal(chatData(object.data), object.id),
|
|
743
|
+
async run(ctx) {
|
|
744
|
+
const stored = await openChat(ctx, "it has no stretch to keep out of memory");
|
|
745
|
+
const refused = privateRefusal(chatData(stored.data), ctx.object.id);
|
|
746
|
+
if (refused !== undefined) throw new Error(refused);
|
|
747
|
+
const read = readChat(ctx.store, ctx.object.id);
|
|
748
|
+
// The reply being written keeps the access profile it started with, so the
|
|
749
|
+
// stretch changes only between replies, as `/new` does.
|
|
750
|
+
if ((await writingReplyNow(read.facts, read.messages, read.entries, ctx.runs)) !== undefined) {
|
|
751
|
+
throw new Error(`chat ${ctx.object.id} is writing a reply; send /private once it ends or /stop it`);
|
|
752
|
+
}
|
|
753
|
+
const on = chatData(stored.data).private !== true;
|
|
754
|
+
await ctx.facts.append({
|
|
755
|
+
type: PRIVATE_FACT,
|
|
756
|
+
object: chatRef(ctx.object.id),
|
|
757
|
+
data: { on, by: { via: ctx.actor.via, id: ctx.actor.id } },
|
|
758
|
+
});
|
|
759
|
+
// The field is there only while the stretch is on.
|
|
760
|
+
await changeChat(ctx, "action:chat/private", (data) => {
|
|
761
|
+
const kept = Object.fromEntries(Object.entries(data).filter(([key]) => key !== "private"));
|
|
762
|
+
return on ? { ...kept, private: true } : kept;
|
|
763
|
+
});
|
|
764
|
+
return on ? PRIVATE_ON_LINE : PRIVATE_OFF_LINE;
|
|
765
|
+
},
|
|
766
|
+
};
|
|
767
|
+
|
|
636
768
|
const stopAction: ActionTypeDefinition = {
|
|
637
769
|
name: "stop",
|
|
638
770
|
on: CHAT_TYPE,
|
|
@@ -823,6 +955,43 @@ const retryAction: ActionTypeDefinition = {
|
|
|
823
955
|
},
|
|
824
956
|
};
|
|
825
957
|
|
|
958
|
+
/** Why `chat:compact` is not offered; absent when it is. */
|
|
959
|
+
function compactRefusal(chat: ChatData, id: string): string | undefined {
|
|
960
|
+
if (chat.closed) return `${id} is closed; there is no context to compact`;
|
|
961
|
+
if (chat.harness !== "pi") return `the ${chat.harness} agent manages its own context; only a pi chat is compacted here`;
|
|
962
|
+
return undefined;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
const compactAction: ActionTypeDefinition = {
|
|
966
|
+
name: "compact",
|
|
967
|
+
on: CHAT_TYPE,
|
|
968
|
+
command: "Compact this chat's context now, after the agent has written what to remember.",
|
|
969
|
+
when: (state) => compactRefusal(chatData(state), "") === undefined,
|
|
970
|
+
why: (object) => compactRefusal(chatData(object.data), object.id),
|
|
971
|
+
async run(ctx) {
|
|
972
|
+
const stored = await openChat(ctx, "there is no context to compact");
|
|
973
|
+
const refused = compactRefusal(chatData(stored.data), ctx.object.id);
|
|
974
|
+
if (refused !== undefined) throw new Error(refused);
|
|
975
|
+
if (ctx.emit === undefined) {
|
|
976
|
+
throw new Error("the context cannot be compacted here; operad serve is not running");
|
|
977
|
+
}
|
|
978
|
+
await ctx.facts.append({ type: COMPACT_FACT, object: chatRef(ctx.object.id), data: { kind: "request" } });
|
|
979
|
+
const read = readChat(ctx.store, ctx.object.id);
|
|
980
|
+
if ((await writingReplyNow(read.facts, read.messages, read.entries, ctx.runs)) !== undefined) {
|
|
981
|
+
// The reply being written goes round once more when it ends, and compacts then.
|
|
982
|
+
return "the context is compacted when the reply being written ends";
|
|
983
|
+
}
|
|
984
|
+
const known = CHAT_SOURCES.find((name) => name === ctx.actor.via);
|
|
985
|
+
await ctx.emit({
|
|
986
|
+
source: known ?? OTHER_SOURCE,
|
|
987
|
+
type: MESSAGE_EVENT,
|
|
988
|
+
key: `compact:${newMessageId()}`,
|
|
989
|
+
object: `${CHAT_TYPE}:${ctx.object.id}`,
|
|
990
|
+
});
|
|
991
|
+
return "compacting the context";
|
|
992
|
+
},
|
|
993
|
+
};
|
|
994
|
+
|
|
826
995
|
const helpAction: ActionTypeDefinition = {
|
|
827
996
|
name: "help",
|
|
828
997
|
on: CHAT_TYPE,
|
|
@@ -845,7 +1014,7 @@ export function createChatPlugin() {
|
|
|
845
1014
|
name: "chat",
|
|
846
1015
|
kind: "domain",
|
|
847
1016
|
version: pkg.version,
|
|
848
|
-
objects: [{ name: CHAT_TYPE, index: ["parent"], filters: CHAT_FILTERS }],
|
|
1017
|
+
objects: [{ name: CHAT_TYPE, index: ["parent"], filters: CHAT_FILTERS, unlistedStates: ["run"] }],
|
|
849
1018
|
settings: {
|
|
850
1019
|
type: "object",
|
|
851
1020
|
properties: {
|
|
@@ -858,6 +1027,15 @@ export function createChatPlugin() {
|
|
|
858
1027
|
description:
|
|
859
1028
|
"How a transcript labels the model's messages. A bot chat uses its bot's speaker instead.",
|
|
860
1029
|
},
|
|
1030
|
+
awayMinutes: {
|
|
1031
|
+
type: "integer",
|
|
1032
|
+
title: "Away after (minutes)",
|
|
1033
|
+
minimum: 0,
|
|
1034
|
+
default: AWAY_MINUTES_DEFAULT,
|
|
1035
|
+
editable: true,
|
|
1036
|
+
description:
|
|
1037
|
+
"When a person writes to a chat after this long without writing, the chat shows one recap of the routine results and replies that came meanwhile. 0 turns it off.",
|
|
1038
|
+
},
|
|
861
1039
|
},
|
|
862
1040
|
additionalProperties: false,
|
|
863
1041
|
},
|
|
@@ -903,7 +1081,7 @@ export function createChatPlugin() {
|
|
|
903
1081
|
mode: "auto" as const,
|
|
904
1082
|
},
|
|
905
1083
|
],
|
|
906
|
-
actions: [sendAction, withdrawAction, editAction, modelAction, effortAction, accessAction, stopAction, retryAction, statusAction, closeAction, helpAction],
|
|
1084
|
+
actions: [sendAction, withdrawAction, editAction, modelAction, effortAction, accessAction, stopAction, newAction, privateAction, retryAction, compactAction, statusAction, closeAction, helpAction],
|
|
907
1085
|
views: { [CHAT_TYPE]: chatViews },
|
|
908
1086
|
});
|
|
909
1087
|
}
|
|
@@ -912,9 +1090,12 @@ export default createChatPlugin();
|
|
|
912
1090
|
|
|
913
1091
|
export * from "./model.ts";
|
|
914
1092
|
export * from "./notes.ts";
|
|
1093
|
+
export * from "./deliver.ts";
|
|
1094
|
+
export * from "./recap.ts";
|
|
915
1095
|
export * from "./command.ts";
|
|
916
1096
|
export * from "./queue.ts";
|
|
917
1097
|
export * from "./reply.ts";
|
|
918
1098
|
export * from "./skills.ts";
|
|
919
1099
|
export * from "./views.ts";
|
|
920
1100
|
export * from "./tail.ts";
|
|
1101
|
+
export * from "./segments.ts";
|