pi-roundtable 0.53.5 → 0.54.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/CHANGELOG.md +19 -0
- package/README.md +14 -2
- package/README.zh-TW.md +14 -2
- package/docs/plugins.md +201 -8
- package/package.json +3 -1
- package/runner/Dockerfile +46 -0
- package/runner/Dockerfile.dockerignore +5 -0
- package/src/cli/checks/images.ts +3 -2
- package/src/cli/checks/preset.ts +20 -0
- package/src/cli/checks/runner.ts +45 -0
- package/src/cli/doctor.ts +7 -1
- package/src/core/agents/agent-claim.ts +7 -4
- package/src/core/agents/agent-prompt.ts +11 -2
- package/src/core/agents/helper-workspace.ts +72 -0
- package/src/core/agents/team-options.ts +2 -0
- package/src/core/agents/team-prompt.ts +12 -1
- package/src/core/attachments/attachment-expiry.ts +12 -0
- package/src/core/attachments/attachment-store.ts +20 -5
- package/src/core/attachments/model-images.ts +3 -2
- package/src/core/builtin/agent-server.ts +8 -0
- package/src/core/builtin/polls.ts +67 -0
- package/src/core/builtin/runner.ts +139 -0
- package/src/core/config/config-schema.ts +4 -0
- package/src/core/config/config.ts +49 -185
- package/src/core/config/preset.ts +51 -0
- package/src/core/config/resolve-config.ts +175 -0
- package/src/core/config/runner-config.ts +80 -0
- package/src/core/contract/attachments.ts +2 -0
- package/src/core/contract/polls.ts +71 -0
- package/src/core/contract/runtime.ts +12 -0
- package/src/core/contract/surface.ts +13 -0
- package/src/core/discord/discord-polls.ts +156 -0
- package/src/core/discord/discord-replies.ts +55 -0
- package/src/core/discord/discord-surface.ts +13 -43
- package/src/core/domain/client.ts +6 -2
- package/src/core/domain/errors.ts +5 -0
- package/src/core/domain/sessions.ts +8 -1
- package/src/core/host/define-roundtable.ts +8 -0
- package/src/core/host/host-registration.ts +7 -0
- package/src/core/host/host.ts +8 -0
- package/src/core/i18n/schedules.ts +2 -0
- package/src/core/modules/polls/poll-result-turns.ts +124 -0
- package/src/core/modules/polls/poll-rules.ts +108 -0
- package/src/core/modules/polls/poll-service.ts +477 -0
- package/src/core/modules/polls/poll-store.ts +336 -0
- package/src/core/modules/polls/poll-tools.ts +118 -0
- package/src/core/modules/polls/poll-turn-text.ts +60 -0
- package/src/core/plugin-api/define.ts +9 -2
- package/src/core/plugin-api/holds.ts +2 -0
- package/src/core/plugin-api/plugin.ts +8 -0
- package/src/core/runner/protocol.ts +456 -0
- package/src/core/runner/runner-client.ts +450 -0
- package/src/core/runner/runner-drivers.ts +356 -0
- package/src/core/runner/runner-files.ts +102 -0
- package/src/core/runner/runner-holds.ts +76 -0
- package/src/core/runner/runner-port.ts +77 -0
- package/src/core/runner/runner-probe.ts +53 -0
- package/src/core/runner/runner-prompt.ts +36 -0
- package/src/core/runner/runner-session.ts +131 -0
- package/src/core/runner/runner-tiers.ts +20 -0
- package/src/core/runner/runner-tools.ts +325 -0
- package/src/core/runner/settings.ts +108 -0
- package/src/core/runtime/conversation-sessions.ts +73 -15
- package/src/core/runtime/extensions/approval-card.ts +29 -2
- package/src/core/runtime/extensions/confirmation-gate.ts +24 -63
- package/src/core/runtime/extensions/gate-card.ts +105 -0
- package/src/core/runtime/extensions/yolo.ts +17 -3
- package/src/core/runtime/pi-agent-runtime.ts +37 -68
- package/src/core/runtime/runtime-plugin.ts +9 -0
- package/src/core/runtime/runtime-types.ts +29 -1
- package/src/core/runtime/session-factory.ts +59 -62
- package/src/core/runtime/session-shell.ts +43 -0
- package/src/core/runtime/session-tool-names.ts +78 -0
- package/src/core/runtime/turn-selection.ts +97 -0
- package/src/core/shared/attachment-reader.ts +10 -1
- package/src/core/shared/generate-image-tool.ts +4 -4
- package/src/core/shared/poll-target.ts +14 -0
- package/src/core/shared/workspace-files.ts +42 -4
- package/src/core/testing/fake-polls.ts +131 -0
- package/src/core/testing/fake-runner.ts +146 -0
- package/src/core/testing/surface-contract-approvals.ts +215 -0
- package/src/core/testing/surface-contract-polls.ts +142 -0
- package/src/core/testing/surface-contract.ts +32 -208
- package/src/core/tool-set.snapshot.json +8 -0
- package/src/index.ts +17 -0
- package/src/kit/index.ts +6 -0
- package/src/kit/polls.ts +8 -0
- package/src/runner-server/exec.ts +174 -0
- package/src/runner-server/file-helper.ts +208 -0
- package/src/runner-server/files.ts +93 -0
- package/src/runner-server/host.ts +65 -0
- package/src/runner-server/main.ts +89 -0
- package/src/runner-server/server.ts +318 -0
- package/src/runner-server/shared-dirs.ts +45 -0
- package/src/runner-server/state.ts +293 -0
- package/src/runner-server/transports.ts +187 -0
- package/src/testing.ts +7 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# The runner image: where multi-user conversations' bash, read, edit and write run. It holds no
|
|
2
|
+
# credential and reaches no network; each conversation gets its own uid and a private workspace.
|
|
3
|
+
# Nothing is published: a host builds it once from the installed package's root, for example
|
|
4
|
+
# docker build -t pi-roundtable-runner -f node_modules/pi-roundtable/runner/Dockerfile node_modules/pi-roundtable
|
|
5
|
+
# and runs it with
|
|
6
|
+
# --network none --cap-drop ALL
|
|
7
|
+
# --cap-add CHOWN --cap-add DAC_OVERRIDE --cap-add FOWNER --cap-add SETUID --cap-add SETGID --cap-add KILL
|
|
8
|
+
# --security-opt no-new-privileges --read-only
|
|
9
|
+
# --tmpfs /tmp:mode=0755 --ipc none --tmpfs /run/roundtable-runner:mode=0700
|
|
10
|
+
# --volume <workspaces>:/roundtable-runner/ws --memory 1g --cpus 1 --pids-limit 256
|
|
11
|
+
# /tmp is root's and --ipc none leaves no /dev/shm, so no directory is writable by every uid
|
|
12
|
+
# (each conversation's TMPDIR is its own); the server makes /dev/mqueue root's at start, and
|
|
13
|
+
# makes /tmp and /dev/shm root's too when a container is run without these two flags.
|
|
14
|
+
# A sidecar mounts a directory owned by the bot's account (mode 0700) at /run/roundtable-runner
|
|
15
|
+
# instead of the tmpfs, and the bot reaches runner.sock with the token beside it.
|
|
16
|
+
FROM oven/bun:1.4.4-slim
|
|
17
|
+
|
|
18
|
+
# Tools a conversation can use without a network: shells and text tools, git and jq, and Python
|
|
19
|
+
# with common data and office packages (pandas brings scipy and sympy along). More go here, since
|
|
20
|
+
# nothing can be installed at run time. tini reaps orphaned processes (see the entrypoint).
|
|
21
|
+
RUN apt-get update \
|
|
22
|
+
&& apt-get install -y --no-install-recommends \
|
|
23
|
+
bash dash coreutils findutils grep sed gawk diffutils util-linux procps \
|
|
24
|
+
git jq file less zip unzip xz-utils bzip2 \
|
|
25
|
+
python3 python3-numpy python3-pandas python3-openpyxl python3-matplotlib \
|
|
26
|
+
python3-docx python3-pil \
|
|
27
|
+
fonts-noto-cjk tini \
|
|
28
|
+
&& rm -rf /var/lib/apt/lists/* \
|
|
29
|
+
&& printf '\nfont.sans-serif: Noto Sans CJK TC, Noto Sans CJK JP, DejaVu Sans\n' >> /etc/matplotlibrc
|
|
30
|
+
|
|
31
|
+
# The server runs from its source with the image's Bun; runner/Dockerfile.dockerignore keeps the
|
|
32
|
+
# build context to these files.
|
|
33
|
+
COPY src/core/runner/protocol.ts /opt/roundtable-runner/src/core/runner/protocol.ts
|
|
34
|
+
COPY src/runner-server/ /opt/roundtable-runner/src/runner-server/
|
|
35
|
+
RUN printf '#!/bin/sh\nexec bun /opt/roundtable-runner/src/runner-server/main.ts "$@"\n' \
|
|
36
|
+
> /usr/local/bin/roundtable-runner \
|
|
37
|
+
&& chmod 0755 /usr/local/bin/roundtable-runner \
|
|
38
|
+
&& chmod -R a=rX,u+w /opt/roundtable-runner \
|
|
39
|
+
&& mkdir -p /roundtable-runner/ws /run/roundtable-runner \
|
|
40
|
+
&& chmod 0711 /roundtable-runner/ws \
|
|
41
|
+
&& chmod 0700 /run/roundtable-runner
|
|
42
|
+
|
|
43
|
+
# tini as PID 1 reaps what the runner kills after each call; without it the killed background
|
|
44
|
+
# processes stay as zombies and use up the container's process limit.
|
|
45
|
+
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/roundtable-runner"]
|
|
46
|
+
CMD ["serve"]
|
package/src/cli/checks/images.ts
CHANGED
|
@@ -11,11 +11,12 @@ export async function checkImageProvider(project: Project): Promise<Result> {
|
|
|
11
11
|
const assembled = await project.assembled();
|
|
12
12
|
if (!assembled.ok)
|
|
13
13
|
return { status: "skipped", reason: "the configuration is not valid yet" };
|
|
14
|
+
const mode = `imageTool: ${assembled.value.config.imageTool}`;
|
|
14
15
|
return fillsImages(assembled.value.defined.plugins)
|
|
15
16
|
? ok(
|
|
16
|
-
|
|
17
|
+
`${mode}; a plugin fills the images slot, used for agents' generate_image; the agent server no longer draws their avatars with it`,
|
|
17
18
|
)
|
|
18
19
|
: ok(
|
|
19
|
-
`none; agents are composed characters, and the agent server no longer draws with the images slot. To let agents make pictures with generate_image, fill the slot, as the plugin guide shows: ${GUIDE}`,
|
|
20
|
+
`none; ${mode}; agents are composed characters, and the agent server no longer draws with the images slot. To let agents make pictures with generate_image, fill the slot, as the plugin guide shows: ${GUIDE}`,
|
|
20
21
|
);
|
|
21
22
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Project } from "../project.ts";
|
|
2
|
+
import { ok, type Result, skipped } from "../report.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The configuration's preset and each setting it filled in; a setting the configuration wrote
|
|
6
|
+
* itself is not listed, since it wins over the preset.
|
|
7
|
+
*/
|
|
8
|
+
export async function checkPreset(project: Project): Promise<Result> {
|
|
9
|
+
const assembled = await project.assembled();
|
|
10
|
+
if (!assembled.ok) return skipped("the configuration is not valid yet");
|
|
11
|
+
const { preset } = assembled.value.config;
|
|
12
|
+
if (!preset) return ok("none");
|
|
13
|
+
const set =
|
|
14
|
+
preset.set.length > 0
|
|
15
|
+
? `it set ${preset.set.join(", ")}`
|
|
16
|
+
: "it set nothing; the configuration writes each of its settings";
|
|
17
|
+
return ok(
|
|
18
|
+
`${preset.name}; ${set}; bash, read, edit and write run in the runner for every conversation but the owner's own`,
|
|
19
|
+
);
|
|
20
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
probeRunner,
|
|
3
|
+
type RunnerReach,
|
|
4
|
+
} from "../../core/runner/runner-probe.ts";
|
|
5
|
+
import type { RunnerSettings } from "../../core/runner/settings.ts";
|
|
6
|
+
import type { Project } from "../project.ts";
|
|
7
|
+
import { fail, ok, type Result, skipped } from "../report.ts";
|
|
8
|
+
|
|
9
|
+
/** Looks for the configured runner without starting it; a test hands a stand-in. */
|
|
10
|
+
export type RunnerProbe = (
|
|
11
|
+
settings: RunnerSettings,
|
|
12
|
+
dataDir: string,
|
|
13
|
+
) => Promise<RunnerReach>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Where multi-user conversations' shell and file tools run: the runner's driver, its image or
|
|
17
|
+
* socket, and whether it answers; without `runner` they run as before.
|
|
18
|
+
*/
|
|
19
|
+
export async function checkRunner(
|
|
20
|
+
project: Project,
|
|
21
|
+
probe: RunnerProbe = probeRunner,
|
|
22
|
+
): Promise<Result> {
|
|
23
|
+
const assembled = await project.assembled();
|
|
24
|
+
if (!assembled.ok) return skipped("the configuration is not valid yet");
|
|
25
|
+
const { runner, dataDir } = assembled.value.config;
|
|
26
|
+
if (!runner)
|
|
27
|
+
return ok(
|
|
28
|
+
"none; shell and file tools run on the host as agentServer says. Set runner to run every conversation but the owner's own in a container",
|
|
29
|
+
);
|
|
30
|
+
const where =
|
|
31
|
+
runner.driver === "docker"
|
|
32
|
+
? `docker, image ${runner.image}`
|
|
33
|
+
: `socket ${runner.socket}`;
|
|
34
|
+
const reach = await probe(runner, dataDir);
|
|
35
|
+
if (reach.reachable)
|
|
36
|
+
return ok(
|
|
37
|
+
`${where}: reachable (${reach.detail}); minTier ${runner.minTier}`,
|
|
38
|
+
);
|
|
39
|
+
return fail(
|
|
40
|
+
`${where}: not reachable (${reach.reason})`,
|
|
41
|
+
runner.driver === "docker"
|
|
42
|
+
? `Install Docker and build the image: docker build -t ${runner.image} -f node_modules/pi-roundtable/runner/Dockerfile node_modules/pi-roundtable`
|
|
43
|
+
: "Start the runner sidecar, which listens on that socket and writes its token beside it, in a directory only the bot's account may enter.",
|
|
44
|
+
);
|
|
45
|
+
}
|
package/src/cli/doctor.ts
CHANGED
|
@@ -14,7 +14,9 @@ import { checkEnvironment } from "./checks/environment.ts";
|
|
|
14
14
|
import { checkAccess, checkPrincipals } from "./checks/identity.ts";
|
|
15
15
|
import { checkImageProvider } from "./checks/images.ts";
|
|
16
16
|
import { checkModelLogin } from "./checks/model.ts";
|
|
17
|
+
import { checkPreset } from "./checks/preset.ts";
|
|
17
18
|
import { checkPublicUrl } from "./checks/public-url.ts";
|
|
19
|
+
import { checkRunner, type RunnerProbe } from "./checks/runner.ts";
|
|
18
20
|
import type { Http } from "./http.ts";
|
|
19
21
|
import { type Ports, Project } from "./project.ts";
|
|
20
22
|
import {
|
|
@@ -35,6 +37,8 @@ export interface DoctorInputs {
|
|
|
35
37
|
http: Http;
|
|
36
38
|
/** Also ask the public address to answer, which is only true while the bot runs. */
|
|
37
39
|
reachable: boolean;
|
|
40
|
+
/** Looks for the configured runner; default the docker CLI or the socket. */
|
|
41
|
+
runner?: RunnerProbe;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
export interface DoctorReport {
|
|
@@ -47,7 +51,7 @@ export function buildChecks(
|
|
|
47
51
|
inputs: DoctorInputs,
|
|
48
52
|
project = new Project(inputs.cwd, inputs.ports),
|
|
49
53
|
): Check[] {
|
|
50
|
-
const { cwd, env, bun, ports, database, http, reachable } = inputs;
|
|
54
|
+
const { cwd, env, bun, ports, database, http, reachable, runner } = inputs;
|
|
51
55
|
const discord = new Discord(project, http);
|
|
52
56
|
// A project without Discord asks Discord nothing.
|
|
53
57
|
const ifDiscord = (run: () => Promise<Result>) => async (): Promise<Result> =>
|
|
@@ -68,6 +72,7 @@ export function buildChecks(
|
|
|
68
72
|
},
|
|
69
73
|
{ name: "plugins", offline: true, run: () => checkPlugins(project) },
|
|
70
74
|
{ name: "access", offline: true, run: () => checkAccess(project) },
|
|
75
|
+
{ name: "preset", offline: true, run: () => checkPreset(project) },
|
|
71
76
|
{
|
|
72
77
|
name: "agent tools",
|
|
73
78
|
offline: true,
|
|
@@ -79,6 +84,7 @@ export function buildChecks(
|
|
|
79
84
|
run: () => checkImageProvider(project),
|
|
80
85
|
},
|
|
81
86
|
{ name: "characters", offline: true, run: () => checkCharacters() },
|
|
87
|
+
{ name: "runner", offline: false, run: () => checkRunner(project, runner) },
|
|
82
88
|
{
|
|
83
89
|
name: "PostgreSQL",
|
|
84
90
|
offline: false,
|
|
@@ -22,6 +22,7 @@ import { answersAuthor } from "../routing/peer-bots.ts";
|
|
|
22
22
|
import { outcome } from "../routing/settle-turn.ts";
|
|
23
23
|
import { systemTurn } from "../routing/system-turns.ts";
|
|
24
24
|
import { PERSONAL_TARGET } from "../shared/personal-target.ts";
|
|
25
|
+
import { POLL_TARGET } from "../shared/poll-target.ts";
|
|
25
26
|
import type { AgentServerTeam } from "./agent-team.ts";
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -133,10 +134,12 @@ export function agentClaim(options: AgentClaimOptions): ChannelClaim {
|
|
|
133
134
|
const backgroundRefusal = (
|
|
134
135
|
turn: BackgroundTurn & { speaker: Speaker },
|
|
135
136
|
): string | undefined => {
|
|
136
|
-
// Fail closed: a turn for any target but the owner's never reaches the owner's tools.
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
137
|
+
// Fail closed: a turn for any target but the owner's, or a poll's results, never reaches the owner's tools.
|
|
138
|
+
const poll = turn.target === POLL_TARGET.name;
|
|
139
|
+
if (turn.target !== PERSONAL_TARGET.name && !poll)
|
|
140
|
+
return `the agent server answers only "${PERSONAL_TARGET.name}" and "${POLL_TARGET.name}" background turns`;
|
|
141
|
+
if (team.owns(turn.channel) === "group")
|
|
142
|
+
return poll ? "groups take no poll results" : "groups have no schedules";
|
|
140
143
|
if (options.speakers === "owners" && turn.speaker.tier !== "owner")
|
|
141
144
|
return 'the agent server answers owners only (speakers: "owners")';
|
|
142
145
|
return undefined;
|
|
@@ -31,6 +31,10 @@ export function agentSystemPrompt(input: {
|
|
|
31
31
|
images?: boolean;
|
|
32
32
|
/** "chat-only": the agent has no shell, file tools or workspace, and the prompt says so. Default "full". */
|
|
33
33
|
agentTools?: AgentTools;
|
|
34
|
+
/** Whether the full tools run in the runner's container rather than on the host. Default false. */
|
|
35
|
+
runner?: boolean;
|
|
36
|
+
/** In the runner, the display name of the agent whose workspace a temp knight shares. */
|
|
37
|
+
runnerWith?: string;
|
|
34
38
|
/** Where this conversation is, and the other surfaces the agent has places on. */
|
|
35
39
|
surface?: { label: string; key: string; others: readonly string[] };
|
|
36
40
|
/** Whether the agent has the temp knight tools. */
|
|
@@ -44,14 +48,19 @@ export function agentSystemPrompt(input: {
|
|
|
44
48
|
const o = ownerWords(owner);
|
|
45
49
|
const w = ownerWords(who);
|
|
46
50
|
const guest = input.speaker && input.speaker.tier !== "owner";
|
|
51
|
+
const drawing = input.images
|
|
52
|
+
? " To make a new picture, describe it to generate_image, which saves it and attaches it to your reply."
|
|
53
|
+
: "";
|
|
47
54
|
const parts = [
|
|
48
55
|
(guest && input.guestShared) || shared,
|
|
49
56
|
`You are "${agent.displayName}" (agent name \`${agent.name}\`), one of ${o.name}'s agents in ${o.his} agent server.${placeText(input.surface)} Each agent owns one channel and one conversation; you all share ${o.his} tools and ${o.his} memory.`,
|
|
50
57
|
roleText(agent, coordinator, who),
|
|
51
58
|
`The team: agent_list shows every agent and group; agent_get and agent_update read and improve any agent's prompt, display name, model, and thinking level, yours included; agent_create adds an agent with its own channel; agent_avatar changes an agent's look (class, colors, parts); schedule_list with agent reads another agent's schedules. A message marked as coming from another agent is that agent speaking, not ${w.name}; only ${w.name} approves held actions.`,
|
|
52
59
|
input.agentTools === "chat-only"
|
|
53
|
-
? `You have no shell or file tools and no workspace on ${o.his} machine: you work through the conversation and the tools listed to you.${
|
|
54
|
-
:
|
|
60
|
+
? `You have no shell or file tools and no workspace on ${o.his} machine: you work through the conversation and the tools listed to you.${drawing}`
|
|
61
|
+
: input.runner
|
|
62
|
+
? `Your shell and file tools run in an isolated container, not on ${o.his} machine, ${input.runnerWith ? `in the workspace of "${input.runnerWith}" there, which is your working directory, so you see and change the files it works on` : "in a workspace of your own there, kept across your channel and the rooms you are in, which is your working directory"}. Commands that are destructive, and writes outside the workspace, are held for confirmation: tell ${w.him} exactly what will run and ask ${w.him} to confirm. To show a file you have, attach it to your reply with attach_file or give its path to discord_send_message; never read an image or base64 a file to send it.${drawing}`
|
|
63
|
+
: `Your shell and file tools run on ${o.his} VPS as the user ${shellUser}, in the shared workspace ${workDir}.${input.scratchDir ? ` Write temporary files under the scratch dir ${input.scratchDir} ($TMPDIR), not elsewhere in /tmp; make them with \`mktemp -p "$TMPDIR"\`, since a bare \`mktemp\` ignores $TMPDIR on some systems.` : ""} Commands that are destructive or change the system, and writes outside the workspace${input.scratchDir ? " and the scratch dir" : ""}, are held for ${w.his} confirmation: tell ${w.him} exactly what will run and ask ${w.him} to confirm. To show a file you have, attach it to your reply with attach_file or give its path to discord_send_message; never read an image or base64 a file to send it.${drawing}`,
|
|
55
64
|
];
|
|
56
65
|
if (input.parent)
|
|
57
66
|
parts.push(
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { SurfacePort } from "../contract/surface.ts";
|
|
2
|
+
import type { AgentTurnScope } from "../domain/ports.ts";
|
|
3
|
+
import type { ChannelKey } from "../domain/sessions.ts";
|
|
4
|
+
import type { AgentDirectory } from "../plugin-api/services.ts";
|
|
5
|
+
import { isHelper } from "./agent-rules.ts";
|
|
6
|
+
import { roomOf } from "./team-keys.ts";
|
|
7
|
+
import type { TeamPlaces } from "./team-places.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The home whose runner workspace an agent's turns work in (`AgentSessions.runnerHome`). A temp
|
|
11
|
+
* knight shares its parent's workspace, as it shares the workspace on the host: in every one of
|
|
12
|
+
* its conversations it works in the home its parent's turn had in the conversation that made it
|
|
13
|
+
* (the parent's own home there, a group's room included), so it sees and changes the files that
|
|
14
|
+
* turn left. Once that origin is gone (the agent or group whose conversation it is, else the
|
|
15
|
+
* parent, is archived or deleted), or the parent is, the temp knight works in its own home, as
|
|
16
|
+
* every other agent does in its own.
|
|
17
|
+
*/
|
|
18
|
+
export function runnerHomeOf(
|
|
19
|
+
store: Pick<AgentDirectory, "agent">,
|
|
20
|
+
places: Pick<TeamPlaces, "ownerKind" | "groupOf" | "scopeOf" | "groupScope">,
|
|
21
|
+
scope: AgentTurnScope,
|
|
22
|
+
): ChannelKey {
|
|
23
|
+
const helper = store.agent(scope.name);
|
|
24
|
+
if (!helper || !isHelper(helper) || helper.origin === undefined)
|
|
25
|
+
return scope.home;
|
|
26
|
+
const parent = store.agent(helper.parent as string);
|
|
27
|
+
if (parent?.status !== "active") return scope.home;
|
|
28
|
+
const origin = helper.origin as ChannelKey;
|
|
29
|
+
// Gone when no active agent or group owns the place: archived (a stored place keeps its row),
|
|
30
|
+
// deleted, or the place itself removed.
|
|
31
|
+
if (places.ownerKind(origin) === undefined) return scope.home;
|
|
32
|
+
const group = places.groupOf(origin);
|
|
33
|
+
return group
|
|
34
|
+
? places.groupScope(parent, group, origin).home
|
|
35
|
+
: places.scopeOf(parent, origin).home;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Whether only the owners reach an agent's conversation (`AgentSessions.ownersOnly`), so its turns
|
|
40
|
+
* keep the host's shell on a host with a runner: the agent's own room on a surface that declares
|
|
41
|
+
* its rooms the owners' alone (`ChatSurface.agentRoomsOwnersOnly`, as the web app does). A room of
|
|
42
|
+
* any other surface, or of one `surfaces` does not know, is not. A temp knight's own room too,
|
|
43
|
+
* unless the conversation it was made in, while that is there, is not its parent's room: made in a
|
|
44
|
+
* group's place or a Discord channel, it works for the people there. Not a Discord channel or its
|
|
45
|
+
* threads, whose readers and writers are the server's to say, nor a group's room or seats, a room
|
|
46
|
+
* of several.
|
|
47
|
+
*/
|
|
48
|
+
export function ownersOnlyConversation(
|
|
49
|
+
store: Pick<AgentDirectory, "agent">,
|
|
50
|
+
places: Pick<
|
|
51
|
+
TeamPlaces,
|
|
52
|
+
"rooms" | "ownerKind" | "groupOf" | "scopeOf" | "groupScope"
|
|
53
|
+
>,
|
|
54
|
+
surfaces: Pick<SurfacePort, "of"> | undefined,
|
|
55
|
+
scope: AgentTurnScope,
|
|
56
|
+
): boolean {
|
|
57
|
+
const rooms = places
|
|
58
|
+
.rooms()
|
|
59
|
+
.filter(
|
|
60
|
+
(surface) => surfaces?.of(`${surface}:`)?.agentRoomsOwnersOnly === true,
|
|
61
|
+
);
|
|
62
|
+
const ownRoom = (key: ChannelKey, name: string) => {
|
|
63
|
+
const room = roomOf(key, rooms);
|
|
64
|
+
return room?.kind === "agent" && room.name === name;
|
|
65
|
+
};
|
|
66
|
+
if (!ownRoom(scope.session, scope.name)) return false;
|
|
67
|
+
const helper = store.agent(scope.name);
|
|
68
|
+
if (!helper || !isHelper(helper) || helper.origin === undefined) return true;
|
|
69
|
+
// Where it works while its origin is there (`runnerHomeOf`); its own room once that is gone.
|
|
70
|
+
if (runnerHomeOf(store, places, scope) === scope.home) return true;
|
|
71
|
+
return ownRoom(helper.origin as ChannelKey, helper.parent as string);
|
|
72
|
+
}
|
|
@@ -24,6 +24,8 @@ export interface AgentTeamOptions
|
|
|
24
24
|
promptSections?: () => readonly PromptSection[];
|
|
25
25
|
/** "chat-only": agent turns carry no shell or file tools, and the prompt says so; default "full". */
|
|
26
26
|
agentTools?: AgentTools;
|
|
27
|
+
/** Whether a full-tools host's agents run their shell and file tools in the runner, as the prompt then says. */
|
|
28
|
+
runner?: boolean;
|
|
27
29
|
/** The tools plugins defined for agents, read before each agent turn. */
|
|
28
30
|
pluginTools?: () => readonly string[];
|
|
29
31
|
/** Those of `pluginTools` a temp knight's turns leave out. */
|
|
@@ -2,6 +2,7 @@ import { AgentError } from "../domain/errors.ts";
|
|
|
2
2
|
import type { AgentTurnScope } from "../domain/ports.ts";
|
|
3
3
|
import { agentSystemPrompt, placeOf } from "./agent-prompt.ts";
|
|
4
4
|
import type { Agent } from "./agent-store.ts";
|
|
5
|
+
import { ownersOnlyConversation, runnerHomeOf } from "./helper-workspace.ts";
|
|
5
6
|
import { coordinatorOf } from "./team-keys.ts";
|
|
6
7
|
import type { AgentTeamOptions } from "./team-options.ts";
|
|
7
8
|
import type { TeamTurns } from "./team-turns.ts";
|
|
@@ -26,6 +27,7 @@ export function teamSystemPrompt(
|
|
|
26
27
|
shellUser,
|
|
27
28
|
scratchDir,
|
|
28
29
|
agentTools,
|
|
30
|
+
runner,
|
|
29
31
|
surfaces,
|
|
30
32
|
places,
|
|
31
33
|
} = options;
|
|
@@ -37,6 +39,13 @@ export function teamSystemPrompt(
|
|
|
37
39
|
const group = scope.group ? store.group(scope.group) : undefined;
|
|
38
40
|
const coordinator = coordinatorOf(store, places, options);
|
|
39
41
|
const speaker = turns.speakerOf(scope);
|
|
42
|
+
// A conversation only the owners reach keeps the host's shell on a host with a runner.
|
|
43
|
+
const inRunner =
|
|
44
|
+
runner && !ownersOnlyConversation(store, places, surfaces, scope);
|
|
45
|
+
// A temp knight that shares its parent's workspace in the runner is told whose files it sees.
|
|
46
|
+
const parent = agent.parent ? store.agent(agent.parent) : undefined;
|
|
47
|
+
const shares =
|
|
48
|
+
inRunner && parent && runnerHomeOf(store, places, scope) !== scope.home;
|
|
40
49
|
const base = agentSystemPrompt({
|
|
41
50
|
agent,
|
|
42
51
|
...(speaker ? { speaker } : {}),
|
|
@@ -48,9 +57,11 @@ export function teamSystemPrompt(
|
|
|
48
57
|
shellUser,
|
|
49
58
|
...(scratchDir ? { scratchDir } : {}),
|
|
50
59
|
images,
|
|
51
|
-
...(agent.parent ? { parent
|
|
60
|
+
...(agent.parent ? { parent } : {}),
|
|
61
|
+
...(shares ? { runnerWith: parent.displayName } : {}),
|
|
52
62
|
helpers: options.helpers !== false,
|
|
53
63
|
...(agentTools ? { agentTools } : {}),
|
|
64
|
+
...(inRunner ? { runner } : {}),
|
|
54
65
|
...placeOf(turns.turnChannel(scope), surfaces, places.surfaces()),
|
|
55
66
|
...(group
|
|
56
67
|
? {
|
|
@@ -9,6 +9,8 @@ import { shrinkTally } from "./attachment-tally.ts";
|
|
|
9
9
|
export interface UsedRecord {
|
|
10
10
|
name: string;
|
|
11
11
|
contentType: string;
|
|
12
|
+
/** This upload was an image claimed with modelImages: 0. Older records omit the flag. */
|
|
13
|
+
modelImageHidden?: true;
|
|
12
14
|
/** The owner's directory segment (a hash, never the id), when the core recorded it. */
|
|
13
15
|
principal?: string;
|
|
14
16
|
/** When the turn took the file, in epoch milliseconds. */
|
|
@@ -48,6 +50,16 @@ const isUsedRecord = (value: unknown): value is UsedRecord =>
|
|
|
48
50
|
!isExpiredMark(value) &&
|
|
49
51
|
typeof (value as UsedRecord).name === "string";
|
|
50
52
|
|
|
53
|
+
/** Whether this file's upload was kept for tools but not shown as a model image. */
|
|
54
|
+
export async function wasModelImageHidden(
|
|
55
|
+
dir: string,
|
|
56
|
+
file: string,
|
|
57
|
+
): Promise<boolean> {
|
|
58
|
+
if (!isAttachmentName(file)) return false;
|
|
59
|
+
const record = await readRecord(recordPath(dir, file));
|
|
60
|
+
return isUsedRecord(record) && record.modelImageHidden === true;
|
|
61
|
+
}
|
|
62
|
+
|
|
51
63
|
/** Whether the file's retention period ended and the core removed it. */
|
|
52
64
|
export async function wasExpired(dir: string, file: string): Promise<boolean> {
|
|
53
65
|
if (!isAttachmentName(file)) return false;
|
|
@@ -10,9 +10,10 @@ import {
|
|
|
10
10
|
type TurnAttachmentOptions,
|
|
11
11
|
} from "../contract/attachments.ts";
|
|
12
12
|
import type { ConversationRegistry } from "../conversations/conversation-registry.ts";
|
|
13
|
-
import
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
import {
|
|
14
|
+
MAX_TURN_IMAGES,
|
|
15
|
+
type StoredAttachment,
|
|
16
|
+
type TurnAttachments,
|
|
16
17
|
} from "../domain/attachment.ts";
|
|
17
18
|
import type { ChannelKey } from "../domain/sessions.ts";
|
|
18
19
|
import type { Logger } from "../kernel/log.ts";
|
|
@@ -27,7 +28,7 @@ import {
|
|
|
27
28
|
} from "./attachment-dir.ts";
|
|
28
29
|
import { expireUsedFiles, type UsedRecord } from "./attachment-expiry.ts";
|
|
29
30
|
import { MAX_ATTACHMENT_BYTES, safeFileName } from "./attachment-fetcher.ts";
|
|
30
|
-
import { isAttachmentName } from "./attachment-name.ts";
|
|
31
|
+
import { isAttachmentName, plainContentType } from "./attachment-name.ts";
|
|
31
32
|
import { principalSegment, readTally, writeTally } from "./attachment-tally.ts";
|
|
32
33
|
import { modelImagesOf } from "./model-images.ts";
|
|
33
34
|
|
|
@@ -155,13 +156,23 @@ export class AttachmentStore implements AttachmentPort {
|
|
|
155
156
|
files: readonly string[],
|
|
156
157
|
options: TurnAttachmentOptions = {},
|
|
157
158
|
): Promise<TurnAttachments> {
|
|
159
|
+
const { modelImages = MAX_TURN_IMAGES } = options;
|
|
160
|
+
if (
|
|
161
|
+
!Number.isInteger(modelImages) ||
|
|
162
|
+
modelImages < 0 ||
|
|
163
|
+
modelImages > MAX_TURN_IMAGES
|
|
164
|
+
)
|
|
165
|
+
throw new RangeError(
|
|
166
|
+
`modelImages must be an integer from 0 to ${MAX_TURN_IMAGES}`,
|
|
167
|
+
);
|
|
158
168
|
await this.#admit(channel, principalId);
|
|
159
169
|
const found = await this.#exclusive(principalSegment(principalId), () =>
|
|
160
|
-
this.#claim(channel, principalId, files, options),
|
|
170
|
+
this.#claim(channel, principalId, files, { ...options, modelImages }),
|
|
161
171
|
);
|
|
162
172
|
const { images, failures } = await modelImagesOf(
|
|
163
173
|
found,
|
|
164
174
|
this.#options.logger,
|
|
175
|
+
modelImages,
|
|
165
176
|
);
|
|
166
177
|
return { files: found, images, failures };
|
|
167
178
|
}
|
|
@@ -218,6 +229,10 @@ export class AttachmentStore implements AttachmentPort {
|
|
|
218
229
|
const used: UsedRecord = {
|
|
219
230
|
name: stored.name,
|
|
220
231
|
contentType: stored.contentType,
|
|
232
|
+
...(options.modelImages === 0 &&
|
|
233
|
+
plainContentType(stored.contentType).startsWith("image/")
|
|
234
|
+
? ({ modelImageHidden: true } as const)
|
|
235
|
+
: {}),
|
|
221
236
|
principal: principalSegment(principalId),
|
|
222
237
|
usedAt: (this.#options.now ?? Date.now)(),
|
|
223
238
|
size: stored.size,
|
|
@@ -11,16 +11,17 @@ import {
|
|
|
11
11
|
} from "./image-prep.ts";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
* The first
|
|
14
|
+
* The first `limit` images among the files, prepared as the model accepts them. An image that cannot
|
|
15
15
|
* be decoded is reported as a failure and the turn goes on without it.
|
|
16
16
|
*/
|
|
17
17
|
export async function modelImagesOf(
|
|
18
18
|
files: readonly StoredAttachment[],
|
|
19
19
|
logger: Logger,
|
|
20
|
+
limit = MAX_IMAGES_PER_TURN,
|
|
20
21
|
): Promise<{ images: ModelImage[]; failures: AttachmentFailure[] }> {
|
|
21
22
|
const images: ModelImage[] = [];
|
|
22
23
|
const failures: AttachmentFailure[] = [];
|
|
23
|
-
for (const file of files.filter(isModelImage).slice(0,
|
|
24
|
+
for (const file of files.filter(isModelImage).slice(0, limit)) {
|
|
24
25
|
try {
|
|
25
26
|
images.push(await prepareImage(file));
|
|
26
27
|
} catch (error) {
|
|
@@ -8,6 +8,10 @@ import { AGENT_TOOLS } from "../agents/agent-tools.ts";
|
|
|
8
8
|
import { FileAvatarStudio } from "../agents/avatar-studio.ts";
|
|
9
9
|
import { RelevanceScorer } from "../agents/group-round.ts";
|
|
10
10
|
import { modeOrigins } from "../agents/helper-modes.ts";
|
|
11
|
+
import {
|
|
12
|
+
ownersOnlyConversation,
|
|
13
|
+
runnerHomeOf,
|
|
14
|
+
} from "../agents/helper-workspace.ts";
|
|
11
15
|
import { Routes } from "../agents/routes.ts";
|
|
12
16
|
import { TeamPlaces } from "../agents/team-places.ts";
|
|
13
17
|
import { ownerAttachmentDir } from "../attachments/attachment-dir.ts";
|
|
@@ -277,6 +281,7 @@ export function agentServerPlugin(
|
|
|
277
281
|
seeds: () => context.sessions().seeds,
|
|
278
282
|
promptSections: () => context.sessions().prompt,
|
|
279
283
|
agentTools: context.env.agentTools,
|
|
284
|
+
...(context.env.runner ? { runner: true } : {}),
|
|
280
285
|
pluginTools: () => context.sessions().agentTools,
|
|
281
286
|
pluginWithheldFromHelpers: () =>
|
|
282
287
|
context.sessions().agentToolsWithheldFromHelpers ?? [],
|
|
@@ -339,6 +344,9 @@ export function agentServerPlugin(
|
|
|
339
344
|
modelOf: (name) => built.modelOf(name),
|
|
340
345
|
skills: (name) => built.skillsOf(name),
|
|
341
346
|
turnChannel: (scope) => built.turnChannel(scope),
|
|
347
|
+
runnerHome: (scope) => runnerHomeOf(store, places, scope),
|
|
348
|
+
ownersOnly: (scope) =>
|
|
349
|
+
ownersOnlyConversation(store, places, surfaces, scope),
|
|
342
350
|
...(options.memory ? { memory: options.memory } : {}),
|
|
343
351
|
};
|
|
344
352
|
options.agents?.bind(agentSessions);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { Tier } from "../identity/speakers.ts";
|
|
2
|
+
import type { PollRules } from "../modules/polls/poll-rules.ts";
|
|
3
|
+
import { PollService, type PollTimer } from "../modules/polls/poll-service.ts";
|
|
4
|
+
import { PgPollStore, type PollStore } from "../modules/polls/poll-store.ts";
|
|
5
|
+
import { POLL_TOOLS, pollsExtension } from "../modules/polls/poll-tools.ts";
|
|
6
|
+
import type { RoundtablePlugin } from "../plugin-api/plugin.ts";
|
|
7
|
+
import { POLL_TARGET } from "../shared/poll-target.ts";
|
|
8
|
+
import { fixed } from "./session-tool.ts";
|
|
9
|
+
|
|
10
|
+
/** Every speaker may start a poll and end their own; an owner may end anyone's. */
|
|
11
|
+
const POLL_TIERS: Readonly<Record<string, Tier>> = Object.fromEntries(
|
|
12
|
+
POLL_TOOLS.map((tool) => [tool, "member" as const]),
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
export interface PollsOptions {
|
|
16
|
+
/** The host's rules over the defaults (`POLL_RULES`); for tests and hosts that hold polls tighter. */
|
|
17
|
+
rules?: Partial<PollRules>;
|
|
18
|
+
/** Replaces the store over the host's database; for tests. */
|
|
19
|
+
store?: PollStore;
|
|
20
|
+
/** Replaces the timers; for tests. */
|
|
21
|
+
timer?: PollTimer;
|
|
22
|
+
now?: () => Date;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Native polls on the surfaces that have them (`ChatSurface.polls`): the table, the poll service,
|
|
27
|
+
* `poll_create` and `poll_end` in every session (offered where a selection names them), and
|
|
28
|
+
* `POLL_TARGET`, whose turns bring each poll's results back to its conversation as its starter's.
|
|
29
|
+
*/
|
|
30
|
+
export function pollsPlugin(options: PollsOptions = {}): RoundtablePlugin {
|
|
31
|
+
return {
|
|
32
|
+
name: "polls",
|
|
33
|
+
...(options.store ? {} : { migrations: PgPollStore.migrations() }),
|
|
34
|
+
setup: ({ database, conversations, surfaces, logger }) => {
|
|
35
|
+
const service = new PollService({
|
|
36
|
+
store: options.store ?? PgPollStore.attach(database()),
|
|
37
|
+
surfaces,
|
|
38
|
+
conversations,
|
|
39
|
+
logger,
|
|
40
|
+
...(options.rules ? { rules: options.rules } : {}),
|
|
41
|
+
...(options.timer ? { timer: options.timer } : {}),
|
|
42
|
+
...(options.now ? { now: options.now } : {}),
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
services: [
|
|
46
|
+
{
|
|
47
|
+
name: "polls",
|
|
48
|
+
start: () => service.start(),
|
|
49
|
+
beginDrain: () => service.beginDrain(),
|
|
50
|
+
stop: () => service.stop(),
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
sessionTools: [
|
|
54
|
+
fixed("polls", (session) =>
|
|
55
|
+
pollsExtension(
|
|
56
|
+
service,
|
|
57
|
+
() => session.turnChannel,
|
|
58
|
+
() => session.speaker(),
|
|
59
|
+
),
|
|
60
|
+
),
|
|
61
|
+
],
|
|
62
|
+
toolTiers: POLL_TIERS,
|
|
63
|
+
backgroundTargets: [POLL_TARGET],
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|