@operad-io/bot 0.4.0 → 0.5.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 CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@operad-io/bot",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
7
7
  },
8
8
  "dependencies": {
9
- "@operad-io/chat": "0.4.0",
10
- "@operad-io/dev": "0.4.0",
11
- "@operad-io/engine": "0.4.0",
12
- "@operad-io/access-match": "0.4.0",
13
- "@operad-io/memory": "0.4.0"
9
+ "@operad-io/chat": "0.5.0",
10
+ "@operad-io/dev": "0.5.0",
11
+ "@operad-io/engine": "0.5.0",
12
+ "@operad-io/access-match": "0.5.0",
13
+ "@operad-io/memory": "0.5.0"
14
14
  },
15
- "description": "operad: the bot plugin",
15
+ "description": "bots with their own memory, skills and routines that you chat with",
16
16
  "repository": {
17
17
  "type": "git",
18
18
  "url": "git+https://github.com/operad-io/operad.git",
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ import { basename, isAbsolute, join, resolve } from "node:path";
16
16
  import { fileURLToPath } from "node:url";
17
17
  import { CHAT_TYPE, MESSAGE_EVENT, mainChatOf, levelRefusal, agentConditionals, type AgentChoices, type ScheduledChat, chatData, createChat, sendMessage, startingEffort, startingModel, titleFromMessage } from "@operad-io/chat";
18
18
  import { definePlugin, isRecord, modelIds, type ActionArgSchema, type ActionArgsSchema, type ActionTypeDefinition, type HarnessCapabilities, type HarnessModelInfo, type HarnessModels, type JsonValue, type ObjectRow, type ObjectStore, type ObjectViews, type PluginStepResult, type SectionContent, type StepContext, type StoredObject } from "@operad-io/engine";
19
+ import { noAgent } from "./no-agent.ts";
19
20
  import {
20
21
  BOT_SETTINGS,
21
22
  DEFAULT_BOT,
@@ -201,6 +202,8 @@ export interface OpenChat {
201
202
  readonly home: string;
202
203
  readonly settings: Readonly<Record<string, JsonValue>>;
203
204
  readonly harnesses: readonly string[];
205
+ /** The harnesses whose agent is not on this machine, for a refusal. */
206
+ readonly missingAgents?: Readonly<Record<string, string>> | undefined;
204
207
  /** What each loaded agent can do; the level a chat starts at must be one its agent carries. */
205
208
  readonly capabilities?: Readonly<Record<string, HarnessCapabilities>> | undefined;
206
209
  readonly bot: string;
@@ -232,6 +235,21 @@ export function defaultAgent(
232
235
  return harnesses.find((name) => (capabilities[name]?.accessLevels ?? []).includes(level)) ?? harnesses[0];
233
236
  }
234
237
 
238
+ /**
239
+ * Why `agent` is not among the ones offered: a loaded harness whose agent is
240
+ * not on this machine is not set up, and says how to set it up.
241
+ */
242
+ export function agentRefusal(
243
+ agent: string,
244
+ harnesses: readonly string[],
245
+ missing: Readonly<Record<string, string>> | undefined,
246
+ ): string {
247
+ const setup = missing?.[agent];
248
+ return setup === undefined
249
+ ? `agent "${agent}" is not loaded; the loaded ones are: ${harnesses.join(", ") || "none"}`
250
+ : `agent "${agent}" is not set up: ${setup}`;
251
+ }
252
+
235
253
  /** Opens a chat on a bot and returns its id. Refuses with a plain message. */
236
254
  export async function openChat(input: OpenChat): Promise<string> {
237
255
  const written = readBots(input.home, input.settings);
@@ -246,12 +264,10 @@ export async function openChat(input: OpenChat): Promise<string> {
246
264
  const harness =
247
265
  input.harness ?? bot.harness ?? defaultAgent(input.harnesses, input.capabilities, startingLevel);
248
266
  if (harness === undefined) {
249
- throw new BotError("no agent is loaded; load one to chat with the bot");
267
+ throw new BotError(noAgent(input.missingAgents, "no agent is loaded; load one to chat with the bot"));
250
268
  }
251
269
  if (!input.harnesses.includes(harness)) {
252
- throw new BotError(
253
- `agent "${harness}" is not loaded; the loaded ones are: ${input.harnesses.join(", ") || "none"}`,
254
- );
270
+ throw new BotError(agentRefusal(harness, input.harnesses, input.missingAgents));
255
271
  }
256
272
  // The bot's configured model and effort must be ones the agent lists; an unlisted one is refused by name.
257
273
  let model: string | undefined;
@@ -457,7 +473,9 @@ const createAction: ActionTypeDefinition = {
457
473
  when: (state, env) => state.removed !== true && (env.harnesses ?? []).length > 0,
458
474
  async why(object, env) {
459
475
  if (object.data.removed === true) return `bot ${object.id} is no longer in the settings`;
460
- if ((env.harnesses ?? []).length === 0) return "no agent is loaded; load one to create a bot";
476
+ if ((env.harnesses ?? []).length === 0) {
477
+ return noAgent(env.missingAgents, "no agent is loaded; load one to create a bot");
478
+ }
461
479
  return undefined;
462
480
  },
463
481
  async run(ctx, args) {
@@ -473,9 +491,7 @@ const createAction: ActionTypeDefinition = {
473
491
  const name = input.name ?? "";
474
492
  const agent = input.agent ?? "";
475
493
  if (!ctx.harnesses.includes(agent)) {
476
- throw new BotError(
477
- `agent "${agent}" is not loaded; the loaded ones are: ${ctx.harnesses.join(", ") || "none"}`,
478
- );
494
+ throw new BotError(agentRefusal(agent, ctx.harnesses, ctx.missingAgents));
479
495
  }
480
496
  try {
481
497
  if (input.model !== undefined) startingModel(agent, ctx.models?.[agent], input.model);
@@ -538,7 +554,9 @@ const chatAction: ActionTypeDefinition = {
538
554
  async why(object, env) {
539
555
  if (object.data.removed === true) return `bot ${object.id} is no longer in the settings`;
540
556
  if (typeof object.data.problem === "string") return `bot ${object.id}: ${object.data.problem}`;
541
- if ((env.harnesses ?? []).length === 0) return `bot ${object.id}: no agent is loaded; load one to chat with it`;
557
+ if ((env.harnesses ?? []).length === 0) {
558
+ return `bot ${object.id}: ${noAgent(env.missingAgents, "no agent is loaded; load one to chat with it")}`;
559
+ }
542
560
  return undefined;
543
561
  },
544
562
  async run(ctx, args) {
@@ -559,6 +577,7 @@ const chatAction: ActionTypeDefinition = {
559
577
  home: ctx.home,
560
578
  settings: ctx.settings,
561
579
  harnesses: ctx.harnesses,
580
+ missingAgents: ctx.missingAgents,
562
581
  capabilities: ctx.capabilities,
563
582
  models: ctx.models,
564
583
  efforts: ctx.efforts,
@@ -610,7 +629,9 @@ const setupDevAction: ActionTypeDefinition = {
610
629
  async why(object, env) {
611
630
  if (object.data.removed === true) return `bot ${object.id} is no longer in the settings`;
612
631
  if (typeof object.data.problem === "string") return `bot ${object.id}: ${object.data.problem}`;
613
- if ((env.harnesses ?? []).length === 0) return `bot ${object.id}: no agent is loaded; load one to chat with it`;
632
+ if ((env.harnesses ?? []).length === 0) {
633
+ return `bot ${object.id}: ${noAgent(env.missingAgents, "no agent is loaded; load one to chat with it")}`;
634
+ }
614
635
  return undefined;
615
636
  },
616
637
  async run(ctx, args) {
@@ -623,6 +644,7 @@ const setupDevAction: ActionTypeDefinition = {
623
644
  home: ctx.home,
624
645
  settings: ctx.settings,
625
646
  harnesses: ctx.harnesses,
647
+ missingAgents: ctx.missingAgents,
626
648
  capabilities: ctx.capabilities,
627
649
  models: ctx.models,
628
650
  efforts: ctx.efforts,
@@ -669,6 +691,7 @@ const setupAction: ActionTypeDefinition = {
669
691
  home: ctx.home,
670
692
  settings: ctx.settings,
671
693
  harnesses: ctx.harnesses,
694
+ missingAgents: ctx.missingAgents,
672
695
  capabilities: ctx.capabilities,
673
696
  models: ctx.models,
674
697
  efforts: ctx.efforts,
@@ -798,7 +821,7 @@ export function createBotPlugin() {
798
821
  // The version comes from package.json so the two cannot drift apart.
799
822
  version: pkg.version,
800
823
  // Patch releases of chat only: a new minor may change what bot relies on.
801
- requires: { plugins: { chat: "~0.4.0" } },
824
+ requires: { plugins: { chat: "~0.5.0" } },
802
825
  settings: BOT_SETTINGS,
803
826
  // A bot page is that bot's settings page; bots live under settings, not in a tab.
804
827
  objectsUnderSettings: true,
package/src/login.ts CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  type StepContext,
19
19
  } from "@operad-io/engine";
20
20
  import { BOT_TYPE, BotError, readBots } from "./bot.ts";
21
+ import { noAgent } from "./no-agent.ts";
21
22
  import { updateBotsFile, validateBots } from "./proposals.ts";
22
23
 
23
24
  /** The step that runs the agent's login and writes the chosen model. */
@@ -172,7 +173,9 @@ export const loginAction: ActionTypeDefinition = {
172
173
  ) !== undefined,
173
174
  async why(object, env) {
174
175
  if (object.data.removed === true) return `bot ${object.id} is no longer in the settings`;
175
- if ((env.harnesses ?? []).length === 0) return `bot ${object.id}: no agent is loaded to log in with`;
176
+ if ((env.harnesses ?? []).length === 0) {
177
+ return `bot ${object.id}: ${noAgent(env.missingAgents, "no agent is loaded to log in with")}`;
178
+ }
176
179
  return undefined;
177
180
  },
178
181
  async run(ctx) {
@@ -0,0 +1,10 @@
1
+ /** The refusal when no agent is offered. */
2
+
3
+ /**
4
+ * `otherwise` when no harness is loaded; else how to set up each loaded one
5
+ * whose agent is not on this machine (ADR 0022 D14).
6
+ */
7
+ export function noAgent(missing: Readonly<Record<string, string>> | undefined, otherwise: string): string {
8
+ const setup = Object.entries(missing ?? {}).map(([name, sentence]) => `${name}: ${sentence}`);
9
+ return setup.length === 0 ? otherwise : `no agent is set up; ${setup.join("; ")}`;
10
+ }