@operad-io/chat 0.0.0-stage → 0.0.1
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 +95 -2
- package/package.json +22 -4
- package/src/index.ts +372 -0
- package/src/model.ts +717 -0
- package/src/reply.ts +687 -0
- package/src/tail.ts +107 -0
- package/src/views.ts +444 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yinghai Zhao
|
|
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,96 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@operad-io/chat`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `chat` plugin: a long conversation with an agent, and how a message is
|
|
4
|
+
answered. It comes with operad. It knows nothing of repositories or bots:
|
|
5
|
+
the plugin that opens a chat gives every field (`createChat`).
|
|
6
|
+
|
|
7
|
+
## The object
|
|
8
|
+
|
|
9
|
+
A chat has a parent (a full object id), an agent (the name of a harness plugin,
|
|
10
|
+
fixed once the chat exists), a working directory, an access profile, skills, a
|
|
11
|
+
title, the agent's own session reference (never shown), bound channels, and
|
|
12
|
+
whether it is closed. Its ids are `C-0001` upward. Messages are not fields: each
|
|
13
|
+
is a `chat.message` fact on the chat (who, when, text, attachment paths, the
|
|
14
|
+
source and its reference). An assistant message also names the messages it answers
|
|
15
|
+
(`replyTo`), the run it belongs to (`runId`) and the session after it. Closing a
|
|
16
|
+
chat appends a `chat.closed` fact and marks the object closed; the messages stay
|
|
17
|
+
in its transcript.
|
|
18
|
+
|
|
19
|
+
What the agent did during an answer is recorded too, as `chat.entry` facts with
|
|
20
|
+
the same `runId` as the reply: intermediate text, thinking, a tool call (its call
|
|
21
|
+
id, name, input summary, result, start, duration, the first 4 KB of its output,
|
|
22
|
+
the file holding the whole output and the first line of an error) and the
|
|
23
|
+
half-written reply of a run that was stopped. Increments are not stored. Output
|
|
24
|
+
longer than 4 KB goes to a file under the chat's own directory (under
|
|
25
|
+
`attachments/chat/<id>/tool-output/`), and secret values are masked before a
|
|
26
|
+
fact or a file is written. A rerun skips a tool call whose id was already recorded and does not add
|
|
27
|
+
the same unfinished text again.
|
|
28
|
+
|
|
29
|
+
Each reply run also records a `chat.run` fact with its run id, so the transcript
|
|
30
|
+
can find the log of the step that runs the agent. While that run lasts, the
|
|
31
|
+
`transcript` section returns a `tail`: the text and thinking being written and
|
|
32
|
+
the tool calls that started and did not end, read from the appended part of that
|
|
33
|
+
log, with secrets masked. It also returns `mark`, and a read with `after: <mark>`
|
|
34
|
+
returns only what was recorded later.
|
|
35
|
+
|
|
36
|
+
## `chat:send <chat> --note <text>`
|
|
37
|
+
|
|
38
|
+
Records the message and sends the event `message` from the source named by the
|
|
39
|
+
client (`ios`, `telegram`, `cli`; any other client counts as `cli`), keyed by the
|
|
40
|
+
message id and about the chat. Three default rules start `chat:reply` with `auto`
|
|
41
|
+
on that event. A closed chat refuses the action and adds no message.
|
|
42
|
+
|
|
43
|
+
## `chat:model <chat> --model <name>`
|
|
44
|
+
|
|
45
|
+
Changes the model the chat's next reply uses. The argument is one of the models the chat's agent lists (its plugin declares them); a closed chat, or an agent that lists none, does not offer the action. A `chat.model` fact records who changed it, from what and to what. A chat whose model its agent cannot take fails the reply with a message naming the model; it never falls back to another one.
|
|
46
|
+
|
|
47
|
+
## `chat:effort <chat> --effort <level>`
|
|
48
|
+
|
|
49
|
+
Changes the effort the chat's next reply uses. The argument is one of the levels the chat's agent lists (its plugin declares them; pi's thinking levels are its efforts); a closed chat, or an agent that lists none, does not offer the action. A `chat.effort` fact records who changed it, from what and to what. A chat whose effort its agent cannot take fails the reply with a message naming it; it never falls back to another one. The row shows the effort when it is not the agent's default.
|
|
50
|
+
|
|
51
|
+
## `chat:close <chat>`
|
|
52
|
+
|
|
53
|
+
Asks for confirmation and records a `chat.closed` fact naming who closed it. A
|
|
54
|
+
closed chat's row reads `closed` and is final; it offers no actions, and a
|
|
55
|
+
message is refused with a short line saying the chat is closed. The transcript
|
|
56
|
+
stays readable. Chats have the filters `open` (the default) and `closed`, with
|
|
57
|
+
the kernel's `all` after `open`, so lists hide closed chats unless asked.
|
|
58
|
+
|
|
59
|
+
## `chat:reply`
|
|
60
|
+
|
|
61
|
+
One step, `chat/reply`, and a loop in the flow. The step builds the prompt from
|
|
62
|
+
the unanswered messages in the facts (never from the event), continues the
|
|
63
|
+
chat's session, records the reply as a fact before it counts as done, updates the
|
|
64
|
+
session reference and looks again for messages that arrived meanwhile: if there
|
|
65
|
+
are some its result is `more` and the flow runs it again. The flow's `maxVisits`
|
|
66
|
+
(20) ends a loop that never catches up. A question that ran out of grace ends the
|
|
67
|
+
step `waiting`; the answer comes back with the session.
|
|
68
|
+
|
|
69
|
+
The agent gets `operadTools: { home }`, so it calls operad through MCP tools and
|
|
70
|
+
never through a shell.
|
|
71
|
+
|
|
72
|
+
In a chat whose access profile is `readOnly`, an agent question about a
|
|
73
|
+
command, a write or an edit becomes the worktree question (`yes`/`no`) and
|
|
74
|
+
never a one-time permission. The question names the call: the command, or the
|
|
75
|
+
path and whether it is a write or an edit; a long command is cut, and the ask
|
|
76
|
+
keeps the whole call. A `yes` sends the event `chat.worktree`; the plugin that
|
|
77
|
+
owns the working directory acts on it (`dev` opens `work/chat-<id>`). A `no`
|
|
78
|
+
tells the agent the call was refused because the chat is read-only, and that it
|
|
79
|
+
should answer in the chat or use read-only commands.
|
|
80
|
+
|
|
81
|
+
## The `transcript` section
|
|
82
|
+
|
|
83
|
+
A live section that pages over the message and entry facts, so the command line,
|
|
84
|
+
the TUI and the app show a chat with no client change. A turn's entries show
|
|
85
|
+
before its reply: one line per tool call with what ran and how long it took, how
|
|
86
|
+
a failed, blocked or denied call ended, the agent's text with its speaker, and
|
|
87
|
+
thinking as a block quote. Tool output is not shown. Radian chats show the tool
|
|
88
|
+
lines only. Pages count messages, and a turn's entries stay on the page of its
|
|
89
|
+
reply.
|
|
90
|
+
|
|
91
|
+
## The speaker
|
|
92
|
+
|
|
93
|
+
The transcript labels the model's messages with a speaker name, read each time
|
|
94
|
+
it is drawn: `plugins.chat.settings.speaker`, `Operad Bot` when not set. A
|
|
95
|
+
chat's parent object may carry a `speaker` of its own (a bot does), and that
|
|
96
|
+
name is used instead. Messages already recorded get a changed name too.
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/chat",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./src/index.ts"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@operad-io/engine": "0.0.1",
|
|
10
|
+
"@operad-io/harness-process": "0.0.1"
|
|
11
|
+
},
|
|
12
|
+
"description": "operad: the chat plugin (in development)",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/operad-io/operad.git",
|
|
16
|
+
"directory": "packages/chat"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"files": [
|
|
20
|
+
"src",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
]
|
|
24
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/** The chat plugin: a long conversation with an agent, and how a message is answered. */
|
|
2
|
+
|
|
3
|
+
import { defineFlow, definePlugin, type ActionArgsSchema, type ActionContext, type ActionTypeDefinition } from "@operad-io/engine";
|
|
4
|
+
import {
|
|
5
|
+
CHAT_FILTERS,
|
|
6
|
+
CHAT_SOURCES,
|
|
7
|
+
OTHER_SOURCE,
|
|
8
|
+
CHAT_TYPE,
|
|
9
|
+
chatData,
|
|
10
|
+
chatRef,
|
|
11
|
+
CLOSED_FACT,
|
|
12
|
+
EFFORT_FACT,
|
|
13
|
+
MESSAGE_EVENT,
|
|
14
|
+
MESSAGE_FACT,
|
|
15
|
+
attachmentOf,
|
|
16
|
+
MODEL_FACT,
|
|
17
|
+
newMessageId,
|
|
18
|
+
type ClosedData,
|
|
19
|
+
type EffortChangedData,
|
|
20
|
+
type ModelChangedData,
|
|
21
|
+
type MessageData,
|
|
22
|
+
} from "./model.ts";
|
|
23
|
+
import { runReply } from "./reply.ts";
|
|
24
|
+
import { chatViews } from "./views.ts";
|
|
25
|
+
|
|
26
|
+
/** How many rounds one reply run may take before it stops (each round is one visit). */
|
|
27
|
+
export const REPLY_MAX_VISITS = 20;
|
|
28
|
+
|
|
29
|
+
/** The flow a message starts: answer, and go round again while messages came meanwhile. */
|
|
30
|
+
export const replyFlow = defineFlow({
|
|
31
|
+
name: "chat:reply",
|
|
32
|
+
entry: "reply",
|
|
33
|
+
maxVisits: REPLY_MAX_VISITS,
|
|
34
|
+
steps: [
|
|
35
|
+
{
|
|
36
|
+
id: "reply",
|
|
37
|
+
kind: "plugin",
|
|
38
|
+
params: { use: "chat/reply", params: {} },
|
|
39
|
+
replay: "unsafe",
|
|
40
|
+
next: {
|
|
41
|
+
done: "done",
|
|
42
|
+
more: "reply",
|
|
43
|
+
waiting: "person",
|
|
44
|
+
failed: "failed",
|
|
45
|
+
interrupted: "cancelled",
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
// A question ran out of grace. The answer sends the run back to the
|
|
50
|
+
// reply, which continues the session with it.
|
|
51
|
+
id: "person",
|
|
52
|
+
kind: "wait_person",
|
|
53
|
+
params: {
|
|
54
|
+
prompt: "The agent asked a question.",
|
|
55
|
+
choices: ["allow_once", "reject_once", "yes", "no"],
|
|
56
|
+
lateChoices: ["allow_always"],
|
|
57
|
+
},
|
|
58
|
+
replay: "safe",
|
|
59
|
+
next: {
|
|
60
|
+
allow_once: "reply",
|
|
61
|
+
allow_always: "reply",
|
|
62
|
+
reject_once: "reply",
|
|
63
|
+
yes: "reply",
|
|
64
|
+
no: "reply",
|
|
65
|
+
interrupted: "cancelled",
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
{ id: "done", kind: "end", params: { status: "done" }, replay: "safe" },
|
|
69
|
+
{ id: "failed", kind: "end", params: { status: "failed" }, replay: "safe" },
|
|
70
|
+
{ id: "cancelled", kind: "end", params: { status: "cancelled" }, replay: "safe" },
|
|
71
|
+
],
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/** What {@link sendMessage} needs of an action's context. */
|
|
75
|
+
export type SendContext = Pick<ActionContext, "facts" | "emit" | "actor">;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Records a user message on chat `chatId` and, when events can be routed, starts
|
|
79
|
+
* the reply: what `chat:send` does. An action that creates a chat with a first
|
|
80
|
+
* message calls it for the new chat. Returns the line to show.
|
|
81
|
+
*/
|
|
82
|
+
export async function sendMessage(
|
|
83
|
+
ctx: SendContext,
|
|
84
|
+
chatId: string,
|
|
85
|
+
note: string,
|
|
86
|
+
ref?: string,
|
|
87
|
+
files: readonly string[] = [],
|
|
88
|
+
): Promise<string> {
|
|
89
|
+
const via = ctx.actor.via;
|
|
90
|
+
// The message records who sent it as it is; the event uses a known source, or
|
|
91
|
+
// `other` for a client this plugin does not know, so it is still answered.
|
|
92
|
+
const known = CHAT_SOURCES.find((name) => name === via);
|
|
93
|
+
const source = known ?? OTHER_SOURCE;
|
|
94
|
+
const id = newMessageId();
|
|
95
|
+
const message: MessageData = {
|
|
96
|
+
id,
|
|
97
|
+
role: "user",
|
|
98
|
+
from: { via: ctx.actor.via, id: ctx.actor.id },
|
|
99
|
+
text: note,
|
|
100
|
+
attachments: files.map((path) => attachmentOf(path)),
|
|
101
|
+
source: { kind: via, ref: ref ?? null },
|
|
102
|
+
};
|
|
103
|
+
// The message is a fact first; the event is what starts the answer.
|
|
104
|
+
await ctx.facts.append({ type: MESSAGE_FACT, object: chatRef(chatId), data: message });
|
|
105
|
+
if (ctx.emit === undefined) return `recorded ${id}; it is answered when operad serve runs`;
|
|
106
|
+
await ctx.emit({ source, type: MESSAGE_EVENT, key: id, object: `${CHAT_TYPE}:${chatId}` });
|
|
107
|
+
return `sent ${id}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const sendAction: ActionTypeDefinition = {
|
|
111
|
+
name: "send",
|
|
112
|
+
on: CHAT_TYPE,
|
|
113
|
+
args: {
|
|
114
|
+
type: "object",
|
|
115
|
+
properties: {
|
|
116
|
+
note: {
|
|
117
|
+
type: "string",
|
|
118
|
+
role: "note",
|
|
119
|
+
description: "The message. Optional when files are sent.",
|
|
120
|
+
},
|
|
121
|
+
attachments: {
|
|
122
|
+
type: "array",
|
|
123
|
+
items: { type: "string", format: "file" },
|
|
124
|
+
description: "Files sent with the message, as uploaded by the client.",
|
|
125
|
+
},
|
|
126
|
+
ref: {
|
|
127
|
+
type: "string",
|
|
128
|
+
minLength: 1,
|
|
129
|
+
description: "The message's id where it came from, such as a Telegram message id.",
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
additionalProperties: false,
|
|
133
|
+
},
|
|
134
|
+
when: (state) => !chatData(state).closed,
|
|
135
|
+
why: (object) =>
|
|
136
|
+
chatData(object.data).closed ? `${object.id} is closed; it takes no messages` : undefined,
|
|
137
|
+
async run(ctx, args) {
|
|
138
|
+
const stored = await ctx.store.get(CHAT_TYPE, ctx.object.id);
|
|
139
|
+
if (stored === undefined) throw new Error(`there is no chat ${ctx.object.id}`);
|
|
140
|
+
if (chatData(stored.data).closed) {
|
|
141
|
+
throw new Error(`chat ${ctx.object.id} is closed; it takes no messages`);
|
|
142
|
+
}
|
|
143
|
+
const input = args as {
|
|
144
|
+
readonly note?: string;
|
|
145
|
+
readonly ref?: string;
|
|
146
|
+
readonly attachments?: readonly string[];
|
|
147
|
+
};
|
|
148
|
+
const files = input.attachments ?? [];
|
|
149
|
+
if ((input.note ?? "").trim().length === 0 && files.length === 0) {
|
|
150
|
+
throw new Error("a message needs text or an attachment; pass --note <text>");
|
|
151
|
+
}
|
|
152
|
+
return sendMessage(ctx, ctx.object.id, input.note ?? "", input.ref, files);
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/** The models the chat's agent lists, as the enum of `chat:model`. */
|
|
157
|
+
function modelArgs(models: readonly string[]): ActionArgsSchema {
|
|
158
|
+
return {
|
|
159
|
+
type: "object",
|
|
160
|
+
properties: {
|
|
161
|
+
model: {
|
|
162
|
+
type: "string",
|
|
163
|
+
...(models.length === 0 ? {} : { enum: [...models] }),
|
|
164
|
+
description: "The model the next reply uses.",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
required: ["model"],
|
|
168
|
+
additionalProperties: false,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const modelAction: ActionTypeDefinition = {
|
|
173
|
+
name: "model",
|
|
174
|
+
on: CHAT_TYPE,
|
|
175
|
+
args: modelArgs([]),
|
|
176
|
+
argsFor: (object, env) => modelArgs(env.models?.[chatData(object.data).harness]?.list ?? []),
|
|
177
|
+
when: (state, env) =>
|
|
178
|
+
!chatData(state).closed && (env.models?.[chatData(state).harness]?.list.length ?? 0) > 0,
|
|
179
|
+
why: (object, env) => {
|
|
180
|
+
const chat = chatData(object.data);
|
|
181
|
+
if (chat.closed) return `${object.id} is closed; its model cannot change`;
|
|
182
|
+
if ((env.models?.[chat.harness]?.list.length ?? 0) === 0) {
|
|
183
|
+
return `the agent "${chat.harness}" offers no model to pick`;
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
},
|
|
187
|
+
async run(ctx, args) {
|
|
188
|
+
const stored = await ctx.store.get(CHAT_TYPE, ctx.object.id);
|
|
189
|
+
if (stored === undefined) throw new Error(`there is no chat ${ctx.object.id}`);
|
|
190
|
+
const chat = chatData(stored.data);
|
|
191
|
+
if (chat.closed) throw new Error(`chat ${ctx.object.id} is closed; its model cannot change`);
|
|
192
|
+
const model = (args as { readonly model: string }).model;
|
|
193
|
+
const offered = ctx.models[chat.harness]?.list ?? [];
|
|
194
|
+
if (!offered.includes(model)) {
|
|
195
|
+
throw new Error(
|
|
196
|
+
`the agent "${chat.harness}" cannot use the model "${model}"; it offers: ${offered.join(", ") || "none"}`,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
if (chat.model === model) return `chat ${ctx.object.id} already uses ${model}`;
|
|
200
|
+
const changed: ModelChangedData = {
|
|
201
|
+
by: { via: ctx.actor.via, id: ctx.actor.id },
|
|
202
|
+
from: chat.model ?? null,
|
|
203
|
+
to: model,
|
|
204
|
+
};
|
|
205
|
+
await ctx.facts.append({
|
|
206
|
+
type: MODEL_FACT,
|
|
207
|
+
object: chatRef(ctx.object.id),
|
|
208
|
+
data: changed,
|
|
209
|
+
});
|
|
210
|
+
// Read again: a reply may have moved the session while the fact was written.
|
|
211
|
+
const fresh = await ctx.store.get(CHAT_TYPE, ctx.object.id);
|
|
212
|
+
if (fresh !== undefined) {
|
|
213
|
+
await ctx.store.put(CHAT_TYPE, ctx.object.id, { ...fresh.data, model }, "action:chat/model");
|
|
214
|
+
}
|
|
215
|
+
return `chat ${ctx.object.id} now uses ${model}; the next reply uses it`;
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/** The efforts the chat's agent lists, as the enum of `chat:effort`. */
|
|
220
|
+
function effortArgs(efforts: readonly string[]): ActionArgsSchema {
|
|
221
|
+
return {
|
|
222
|
+
type: "object",
|
|
223
|
+
properties: {
|
|
224
|
+
effort: {
|
|
225
|
+
type: "string",
|
|
226
|
+
...(efforts.length === 0 ? {} : { enum: [...efforts] }),
|
|
227
|
+
description: "The effort the next reply uses.",
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
required: ["effort"],
|
|
231
|
+
additionalProperties: false,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const effortAction: ActionTypeDefinition = {
|
|
236
|
+
name: "effort",
|
|
237
|
+
on: CHAT_TYPE,
|
|
238
|
+
args: effortArgs([]),
|
|
239
|
+
argsFor: (object, env) => effortArgs(env.efforts?.[chatData(object.data).harness]?.list ?? []),
|
|
240
|
+
when: (state, env) =>
|
|
241
|
+
!chatData(state).closed && (env.efforts?.[chatData(state).harness]?.list.length ?? 0) > 0,
|
|
242
|
+
why: (object, env) => {
|
|
243
|
+
const chat = chatData(object.data);
|
|
244
|
+
if (chat.closed) return `${object.id} is closed; its effort cannot change`;
|
|
245
|
+
if ((env.efforts?.[chat.harness]?.list.length ?? 0) === 0) {
|
|
246
|
+
return `the agent "${chat.harness}" offers no effort to pick`;
|
|
247
|
+
}
|
|
248
|
+
return undefined;
|
|
249
|
+
},
|
|
250
|
+
async run(ctx, args) {
|
|
251
|
+
const stored = await ctx.store.get(CHAT_TYPE, ctx.object.id);
|
|
252
|
+
if (stored === undefined) throw new Error(`there is no chat ${ctx.object.id}`);
|
|
253
|
+
const chat = chatData(stored.data);
|
|
254
|
+
if (chat.closed) throw new Error(`chat ${ctx.object.id} is closed; its effort cannot change`);
|
|
255
|
+
const effort = (args as { readonly effort: string }).effort;
|
|
256
|
+
const offered = ctx.efforts[chat.harness];
|
|
257
|
+
const list = offered?.list ?? [];
|
|
258
|
+
if (!list.includes(effort)) {
|
|
259
|
+
throw new Error(
|
|
260
|
+
`the agent "${chat.harness}" cannot use the effort "${effort}"; it offers: ${list.join(", ") || "none"}`,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
if (chat.effort === effort) return `chat ${ctx.object.id} already uses ${effort}`;
|
|
264
|
+
const changed: EffortChangedData = {
|
|
265
|
+
by: { via: ctx.actor.via, id: ctx.actor.id },
|
|
266
|
+
from: chat.effort ?? offered?.default ?? null,
|
|
267
|
+
to: effort,
|
|
268
|
+
};
|
|
269
|
+
await ctx.facts.append({
|
|
270
|
+
type: EFFORT_FACT,
|
|
271
|
+
object: chatRef(ctx.object.id),
|
|
272
|
+
data: changed,
|
|
273
|
+
});
|
|
274
|
+
// Read again: a reply may have moved the session while the fact was written.
|
|
275
|
+
const fresh = await ctx.store.get(CHAT_TYPE, ctx.object.id);
|
|
276
|
+
if (fresh !== undefined) {
|
|
277
|
+
const fallback = offered?.default;
|
|
278
|
+
await ctx.store.put(
|
|
279
|
+
CHAT_TYPE,
|
|
280
|
+
ctx.object.id,
|
|
281
|
+
{
|
|
282
|
+
...fresh.data,
|
|
283
|
+
effort,
|
|
284
|
+
...(fallback === undefined || fallback === null ? {} : { effortDefault: fallback }),
|
|
285
|
+
},
|
|
286
|
+
"action:chat/effort",
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
return `chat ${ctx.object.id} now uses effort ${effort}; the next reply uses it`;
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const closeAction: ActionTypeDefinition = {
|
|
294
|
+
name: "close",
|
|
295
|
+
on: CHAT_TYPE,
|
|
296
|
+
confirm: true,
|
|
297
|
+
ends: true,
|
|
298
|
+
when: (state) => !chatData(state).closed,
|
|
299
|
+
why: (object) =>
|
|
300
|
+
chatData(object.data).closed ? `${object.id} is closed; nothing to close` : undefined,
|
|
301
|
+
async run(ctx) {
|
|
302
|
+
const stored = await ctx.store.get(CHAT_TYPE, ctx.object.id);
|
|
303
|
+
if (stored === undefined) throw new Error(`there is no chat ${ctx.object.id}`);
|
|
304
|
+
if (chatData(stored.data).closed) return `chat ${ctx.object.id} is closed; nothing to close`;
|
|
305
|
+
const closed: ClosedData = {
|
|
306
|
+
by: { via: ctx.actor.via, id: ctx.actor.id },
|
|
307
|
+
};
|
|
308
|
+
await ctx.facts.append({ type: CLOSED_FACT, object: chatRef(ctx.object.id), data: closed });
|
|
309
|
+
// Read again: a reply may have moved the session while the fact was written.
|
|
310
|
+
const fresh = await ctx.store.get(CHAT_TYPE, ctx.object.id);
|
|
311
|
+
if (fresh !== undefined && !chatData(fresh.data).closed) {
|
|
312
|
+
await ctx.store.put(CHAT_TYPE, ctx.object.id, { ...fresh.data, closed: true }, "action:chat/close");
|
|
313
|
+
}
|
|
314
|
+
return `closed ${ctx.object.id}`;
|
|
315
|
+
},
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/** Builds the chat plugin. */
|
|
319
|
+
export function createChatPlugin() {
|
|
320
|
+
return definePlugin({
|
|
321
|
+
name: "chat",
|
|
322
|
+
kind: "domain",
|
|
323
|
+
version: "0.0.0",
|
|
324
|
+
objects: [{ name: CHAT_TYPE, index: ["parent"], filters: CHAT_FILTERS }],
|
|
325
|
+
settings: {
|
|
326
|
+
type: "object",
|
|
327
|
+
properties: {
|
|
328
|
+
speaker: {
|
|
329
|
+
type: "string",
|
|
330
|
+
minLength: 1,
|
|
331
|
+
default: "Operad Bot",
|
|
332
|
+
editable: true,
|
|
333
|
+
description:
|
|
334
|
+
"How a transcript labels the model's messages. A bot chat uses its bot's speaker instead.",
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
additionalProperties: false,
|
|
338
|
+
},
|
|
339
|
+
steps: [
|
|
340
|
+
{
|
|
341
|
+
name: "reply",
|
|
342
|
+
// The harness's question comes back as `resumeSession` and `askAnswer`.
|
|
343
|
+
resumable: true,
|
|
344
|
+
// The harness and the chat's `refresh` step are named by the chat.
|
|
345
|
+
callsNamedByData: true,
|
|
346
|
+
results: ["done", "more", "waiting", "failed", "interrupted"],
|
|
347
|
+
// The harness runs inside this step and may take as long as a task's work.
|
|
348
|
+
timeoutSecs: 8 * 3600,
|
|
349
|
+
run: runReply,
|
|
350
|
+
},
|
|
351
|
+
],
|
|
352
|
+
flows: [replyFlow],
|
|
353
|
+
// A message from a client or a channel is answered without anyone starting
|
|
354
|
+
// the answer; a user rule for the same source and type replaces these.
|
|
355
|
+
rules: [...CHAT_SOURCES, OTHER_SOURCE].map((source) => ({
|
|
356
|
+
source,
|
|
357
|
+
type: MESSAGE_EVENT,
|
|
358
|
+
start: { flow: "chat:reply" },
|
|
359
|
+
object: "$event.object",
|
|
360
|
+
mode: "auto" as const,
|
|
361
|
+
})),
|
|
362
|
+
actions: [sendAction, modelAction, effortAction, closeAction],
|
|
363
|
+
views: { [CHAT_TYPE]: chatViews },
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export default createChatPlugin();
|
|
368
|
+
|
|
369
|
+
export * from "./model.ts";
|
|
370
|
+
export * from "./reply.ts";
|
|
371
|
+
export * from "./views.ts";
|
|
372
|
+
export * from "./tail.ts";
|