pi-pocket 0.28.2 → 0.29.0-beta.2
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/docs/architecture.md +4 -3
- package/docs/customizing.md +1 -1
- package/docs/extensions/linked-sessions.md +2 -0
- package/docs/extensions/policies/api-stability.md +1 -1
- package/docs/extensions/reference/api-versions.md +3 -0
- package/docs/extensions/reference/host.md +18 -4
- package/docs/extensions/reference/pocket.md +12 -1
- package/docs/extensions/reference/types.md +19 -1
- package/docs/extensions/tools.md +18 -0
- package/docs/features.md +5 -5
- package/docs/map.md +6 -4
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/src/server/app.ts +2 -7
- package/src/server/browser/cdp.ts +6 -0
- package/src/server/browser/identity.ts +55 -0
- package/src/server/browser/page.ts +12 -1
- package/src/server/browser.ts +1 -0
- package/src/server/commands.ts +26 -5
- package/src/server/config.ts +10 -0
- package/src/server/custom-providers.ts +304 -23
- package/src/server/docs.ts +10 -3
- package/src/server/extensions/agent-teams.ts +9 -1
- package/src/server/extensions/subagents.ts +51 -3
- package/src/server/host.ts +13 -4
- package/src/server/http/assets.ts +26 -3
- package/src/server/http/io.ts +11 -1
- package/src/server/http/link-routes.ts +4 -1
- package/src/server/http.ts +29 -3
- package/src/server/linked-sessions.ts +92 -9
- package/src/server/project-model.ts +17 -1
- package/types/pocket.d.ts +40 -6
- package/web/app.js +3 -2
- package/web/back.js +27 -6
- package/web/browser-panel.js +23 -12
- package/web/browser-paste.js +13 -0
- package/web/composer.js +7 -3
- package/web/extension-ui.js +17 -2
- package/web/index.html +2 -0
- package/web/pip.js +65 -0
- package/web/running-board.js +38 -36
- package/web/sheets/custom-providers.js +420 -21
- package/web/sheets/memory.js +76 -40
- package/web/store.js +117 -22
- package/web/style.css +176 -13
- package/web/transcript.js +57 -11
|
@@ -132,9 +132,10 @@ export default function createAgentTeams(host: PocketHost, web?: ExtensionWeb) {
|
|
|
132
132
|
|
|
133
133
|
if (args.action === "start") {
|
|
134
134
|
let address: string;
|
|
135
|
+
let answers: boolean | undefined;
|
|
135
136
|
|
|
136
137
|
try {
|
|
137
|
-
({ address } = await linked.start(
|
|
138
|
+
({ address, answers } = await linked.start(
|
|
138
139
|
api.conversationId,
|
|
139
140
|
{
|
|
140
141
|
title: args.title,
|
|
@@ -160,6 +161,13 @@ export default function createAgentTeams(host: PocketHost, web?: ExtensionWeb) {
|
|
|
160
161
|
);
|
|
161
162
|
}
|
|
162
163
|
|
|
164
|
+
// A server older than the switch says nothing of it: its answers come back.
|
|
165
|
+
if (answers === false) {
|
|
166
|
+
return reply(
|
|
167
|
+
`Started linked session ${address} and sent its first message. Its final answer does not come to you by itself, because you turned the answers off: its first message asks it to send the answer, when you asked for one. Do not poll.`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
163
171
|
return reply(
|
|
164
172
|
`Started linked session ${address} and sent its first message. Its final answer comes to you by itself when its run ends; do not ask for it, and do not poll. Send to that address only for more work or a question.`,
|
|
165
173
|
);
|
|
@@ -34,6 +34,7 @@ import {
|
|
|
34
34
|
type SubmissionId,
|
|
35
35
|
type SubmissionRecord,
|
|
36
36
|
type TaskRuntime,
|
|
37
|
+
type ToolRegistration,
|
|
37
38
|
type Tx,
|
|
38
39
|
} from "@earendil-works/pi-durable";
|
|
39
40
|
import { type PendingReport, PlanDoc, REPORT_PREFIX, SubagentsDoc } from "../docs.ts";
|
|
@@ -44,6 +45,12 @@ import { modelLine } from "../model-facts.ts";
|
|
|
44
45
|
import { requestFor } from "../requests.ts";
|
|
45
46
|
import { whyUnanswered } from "../subagent-runs.ts";
|
|
46
47
|
|
|
48
|
+
/** Pi Pocket's options on a tool definition, beside Pi Durable's own. */
|
|
49
|
+
export interface ToolOptions {
|
|
50
|
+
/** Give this tool to every subagent, even one started with a tool list, while its extension runs there. */
|
|
51
|
+
readonly subagents?: "always";
|
|
52
|
+
}
|
|
53
|
+
|
|
47
54
|
// Exclude Agent Teams by name even while it is off; enabling it later must not give nested agents delegation.
|
|
48
55
|
const AgentTeams = defineExtension({ name: "pocket-agent-teams" });
|
|
49
56
|
// Nor does a subagent ask the person (`ask.ts`): it works for another agent, and nobody reads its chat.
|
|
@@ -985,10 +992,22 @@ export default function createSubagents(host: PocketHost, web?: ExtensionWeb) {
|
|
|
985
992
|
).values(),
|
|
986
993
|
]
|
|
987
994
|
: [];
|
|
995
|
+
const removed = [SubagentTools, AgentTeams, Ask];
|
|
996
|
+
// Marked tools this session enables, of the extensions a subagent keeps: a tool it disabled stays out.
|
|
997
|
+
const kept = new Set(
|
|
998
|
+
parent.extensions
|
|
999
|
+
.filter((extension) => !removed.some((each) => each.name === extension.name))
|
|
1000
|
+
.flatMap((extension) => (extension.tools ?? []).map((tool) => tool.name)),
|
|
1001
|
+
);
|
|
1002
|
+
const always = parentTools.filter(
|
|
1003
|
+
(tool) =>
|
|
1004
|
+
kept.has(tool.name) &&
|
|
1005
|
+
(tool as ToolRegistration & ToolOptions).subagents === "always",
|
|
1006
|
+
);
|
|
988
1007
|
const tools =
|
|
989
1008
|
args.tools === undefined
|
|
990
1009
|
? undefined
|
|
991
|
-
: args.tools.map((tool) => {
|
|
1010
|
+
: [...new Set(args.tools)].map((tool) => {
|
|
992
1011
|
const found = parentTools.find((each) => each.name === tool);
|
|
993
1012
|
|
|
994
1013
|
if (found === undefined) {
|
|
@@ -999,12 +1018,21 @@ export default function createSubagents(host: PocketHost, web?: ExtensionWeb) {
|
|
|
999
1018
|
|
|
1000
1019
|
return found;
|
|
1001
1020
|
});
|
|
1021
|
+
|
|
1022
|
+
if (tools !== undefined) {
|
|
1023
|
+
for (const tool of always) {
|
|
1024
|
+
if (!tools.some((each) => each.name === tool.name)) {
|
|
1025
|
+
tools.push(tool);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1002
1030
|
const codemode = parentTools.find((tool) => tool.name === "codemode");
|
|
1003
1031
|
|
|
1004
1032
|
if (
|
|
1005
1033
|
codemode !== undefined &&
|
|
1006
1034
|
tools?.some((tool) => tool.callers?.includes("model") === false) === true &&
|
|
1007
|
-
!tools.
|
|
1035
|
+
!tools.some((tool) => tool.name === codemode.name)
|
|
1008
1036
|
) {
|
|
1009
1037
|
tools.push(codemode);
|
|
1010
1038
|
}
|
|
@@ -1044,8 +1072,28 @@ export default function createSubagents(host: PocketHost, web?: ExtensionWeb) {
|
|
|
1044
1072
|
ownership: { kind: "task", taskId: anchor },
|
|
1045
1073
|
});
|
|
1046
1074
|
|
|
1075
|
+
// With a tool list, keep a fixed list and this session's choices: the tools it names, and the
|
|
1076
|
+
// marked ones, come from this session's extensions. Without one, the host's defaults, as before.
|
|
1077
|
+
const inherited =
|
|
1078
|
+
tools === undefined
|
|
1079
|
+
? undefined
|
|
1080
|
+
: (await tx.doc(AgentDoc, child.id)).extensions;
|
|
1081
|
+
const extensions = Array.isArray(inherited)
|
|
1082
|
+
? inherited
|
|
1083
|
+
.filter((name) => !removed.some((each) => each.name === name))
|
|
1084
|
+
.map((name) => ({ name }))
|
|
1085
|
+
: {
|
|
1086
|
+
...(inherited?.add === undefined
|
|
1087
|
+
? {}
|
|
1088
|
+
: { add: inherited.add.map((name) => ({ name })) }),
|
|
1089
|
+
remove: [
|
|
1090
|
+
...(inherited?.remove ?? []).map((name) => ({ name })),
|
|
1091
|
+
...removed,
|
|
1092
|
+
],
|
|
1093
|
+
};
|
|
1094
|
+
|
|
1047
1095
|
await configure(tx, child.id, {
|
|
1048
|
-
extensions
|
|
1096
|
+
extensions,
|
|
1049
1097
|
instructions: `You are the subagent "${name}". You work for another agent, not directly for a person, although a person may open your conversation and talk to you. Answer requests completely but concisely: your final answer is what gets reported back.`,
|
|
1050
1098
|
...(model === undefined ? {} : { model }),
|
|
1051
1099
|
...(thinking === undefined ? {} : { thinkingLevel: thinking }),
|
package/src/server/host.ts
CHANGED
|
@@ -72,17 +72,17 @@ export interface LinkedSessionsApi {
|
|
|
72
72
|
cwd?: string;
|
|
73
73
|
},
|
|
74
74
|
key: string,
|
|
75
|
-
): Promise<{ session: ConversationId; address: string }>;
|
|
75
|
+
): Promise<{ session: ConversationId; address: string; answers?: boolean }>;
|
|
76
76
|
/**
|
|
77
77
|
* Send `message` to the linked session at address `to`: at its next pause, or with `steer: false` after its run.
|
|
78
|
-
* `answers` says whether its final answer comes back to the source by itself (the source started it
|
|
79
|
-
* message asks it to reply when an answer is needed.
|
|
78
|
+
* `answers` says whether its final answer comes back to the source by itself (the source started it, and did not
|
|
79
|
+
* turn its answers off with `reports`); otherwise the message asks it to reply when an answer is needed.
|
|
80
80
|
*/
|
|
81
81
|
send(
|
|
82
82
|
source: ConversationId,
|
|
83
83
|
request: { to: string; message: string; steer?: boolean },
|
|
84
84
|
key: string,
|
|
85
|
-
): Promise<{ answers
|
|
85
|
+
): Promise<{ answers?: boolean }>;
|
|
86
86
|
/**
|
|
87
87
|
* Stop the run of the linked session at address `to`, and withdraw what waits in its queue. Idle is a no-op.
|
|
88
88
|
* Returns whether it was working.
|
|
@@ -97,6 +97,15 @@ export interface LinkedSessionsApi {
|
|
|
97
97
|
request: { to: string; archived?: boolean },
|
|
98
98
|
key: string,
|
|
99
99
|
): Promise<{ archived: boolean; repeating: number }>;
|
|
100
|
+
/**
|
|
101
|
+
* Set whether `source`, as a lead, takes the run-end answers of the sessions it started (`take`, the default true).
|
|
102
|
+
* Returns the state now.
|
|
103
|
+
*/
|
|
104
|
+
reports(
|
|
105
|
+
source: ConversationId,
|
|
106
|
+
request: { take: boolean },
|
|
107
|
+
key: string,
|
|
108
|
+
): Promise<{ take: boolean }>;
|
|
100
109
|
/** The sessions linked to `source`, at any distance, that the person Pi works for there sees. */
|
|
101
110
|
list(source: ConversationId): Promise<LinkedSession[]>;
|
|
102
111
|
}
|
|
@@ -251,8 +251,17 @@ function preloads(imports: Record<string, string>): string[] {
|
|
|
251
251
|
return [...found];
|
|
252
252
|
}
|
|
253
253
|
|
|
254
|
-
/**
|
|
255
|
-
|
|
254
|
+
/**
|
|
255
|
+
* The app page, with its content security policy, and the modules it loads listed for the browser to fetch at once.
|
|
256
|
+
* `facts`, when the request is signed in, is who and which build (`{ user, build }`, as `hello` says them): the page
|
|
257
|
+
* carries them so a tab can check a session the device kept before `/api/me` answers. Absent when signed out or
|
|
258
|
+
* device caching is off.
|
|
259
|
+
*/
|
|
260
|
+
export function serveApp(
|
|
261
|
+
response: ServerResponse,
|
|
262
|
+
icon: string | null = null,
|
|
263
|
+
facts?: { user: unknown; build: string },
|
|
264
|
+
): void {
|
|
256
265
|
let body: string;
|
|
257
266
|
|
|
258
267
|
try {
|
|
@@ -271,7 +280,21 @@ export function serveApp(response: ServerResponse, icon: string | null = null):
|
|
|
271
280
|
);
|
|
272
281
|
}
|
|
273
282
|
|
|
274
|
-
//
|
|
283
|
+
// Who is signed in and which build this is, for the device cache's check (`usable` in store.js) before `/api/me`.
|
|
284
|
+
// JSON in a script the policy allows by hash: a name or a title cannot close the tag.
|
|
285
|
+
if (facts !== undefined) {
|
|
286
|
+
const json = JSON.stringify(facts).replace(/</g, "\\u003c");
|
|
287
|
+
|
|
288
|
+
body = body.replace(
|
|
289
|
+
"</head>",
|
|
290
|
+
() => `<script type="application/json" id="pocket-known">${json}</script></head>`,
|
|
291
|
+
);
|
|
292
|
+
// The static boot screen must not flash before IndexedDB has its chance. `start` reveals the root once it
|
|
293
|
+
// has either a checked view to paint or no copy (then the normal boot screen, while `/api/me` answers).
|
|
294
|
+
body = body.replace('<div id="app">', '<div id="app" hidden>');
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// The policy is of the page as it is served: the facts above are a script, allowed by their hash.
|
|
275
298
|
const policy = appPolicy(body);
|
|
276
299
|
|
|
277
300
|
try {
|
package/src/server/http/io.ts
CHANGED
|
@@ -65,7 +65,15 @@ export function json(response: ServerResponse, status: number, value: unknown):
|
|
|
65
65
|
/** How long a JSON or form body may take to arrive: a client that stops sending must not hold the connection. */
|
|
66
66
|
const BODY_TIMEOUT_MS = 60_000;
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
/**
|
|
69
|
+
* A request's body, up to `limit` bytes. `signal` is for a caller that stops waiting for it: its abort ends the body's
|
|
70
|
+
* own deadline, which would otherwise keep the process alive and close the connection a minute later.
|
|
71
|
+
*/
|
|
72
|
+
export async function readBody(
|
|
73
|
+
request: IncomingMessage,
|
|
74
|
+
limit: number,
|
|
75
|
+
signal?: AbortSignal,
|
|
76
|
+
): Promise<Buffer> {
|
|
69
77
|
if (Number(request.headers["content-length"] ?? 0) > limit) {
|
|
70
78
|
throw new HttpError(413, "Request too large");
|
|
71
79
|
}
|
|
@@ -75,6 +83,8 @@ export async function readBody(request: IncomingMessage, limit: number): Promise
|
|
|
75
83
|
BODY_TIMEOUT_MS,
|
|
76
84
|
);
|
|
77
85
|
|
|
86
|
+
signal?.addEventListener("abort", () => clearTimeout(timer), { once: true });
|
|
87
|
+
|
|
78
88
|
try {
|
|
79
89
|
const chunks: Buffer[] = [];
|
|
80
90
|
let size = 0;
|
|
@@ -104,13 +104,16 @@ async function readForm(request: IncomingMessage, token: string): Promise<Record
|
|
|
104
104
|
// Past its time, the form is let go of without closing the connection, so the person gets a page that says why;
|
|
105
105
|
// the answer closes the connection after it (`linkRoute`).
|
|
106
106
|
let timer: NodeJS.Timeout | undefined;
|
|
107
|
+
// Ends the read's own deadline when this one wins: nothing waits for that read anymore.
|
|
108
|
+
const stop = new AbortController();
|
|
107
109
|
const tooLate = new Promise<never>((_, reject) => {
|
|
108
110
|
timer = setTimeout(() => {
|
|
109
111
|
request.pause();
|
|
112
|
+
stop.abort();
|
|
110
113
|
reject(new HttpError(408, "The form took too long to arrive."));
|
|
111
114
|
}, linkReads.timeoutMs);
|
|
112
115
|
});
|
|
113
|
-
const read = readBody(request, MAX_FORM);
|
|
116
|
+
const read = readBody(request, MAX_FORM, stop.signal);
|
|
114
117
|
let body: Buffer;
|
|
115
118
|
|
|
116
119
|
// The read that lost the race ends when the connection does; that is no error of anyone's.
|
package/src/server/http.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { readFileSync } from "node:fs";
|
|
|
7
7
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
8
8
|
import { join, normalize, sep } from "node:path";
|
|
9
9
|
import { Auth } from "./auth.ts";
|
|
10
|
+
import { helloUser } from "./config.ts";
|
|
10
11
|
import { describe, HttpError } from "./errors.ts";
|
|
11
12
|
import { createApi } from "./http/api.ts";
|
|
12
13
|
import { artifact } from "./http/artifacts.ts";
|
|
@@ -31,6 +32,22 @@ import { themeFile } from "./http/theme-routes.ts";
|
|
|
31
32
|
/** The version of the app bridge the web app speaks (`web/host.js`). */
|
|
32
33
|
const BRIDGE_VERSION = 1;
|
|
33
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Who is signed in, and this server's build, for the app page (`serveApp`): the same `user` a hello sends, so the tab
|
|
37
|
+
* can check a session the device kept (`usable`) before `/api/me` answers. Absent when signed out or caching is off.
|
|
38
|
+
*/
|
|
39
|
+
function pageFacts(
|
|
40
|
+
request: IncomingMessage,
|
|
41
|
+
auth: Auth,
|
|
42
|
+
app: HttpOptions["app"],
|
|
43
|
+
): { user: ReturnType<typeof helloUser>; build: string } | undefined {
|
|
44
|
+
const user = auth.user(request);
|
|
45
|
+
|
|
46
|
+
return user === undefined || process.env.PI_POCKET_SESSION_CACHE === "off"
|
|
47
|
+
? undefined
|
|
48
|
+
: { user: helloUser(user), build: app.build };
|
|
49
|
+
}
|
|
50
|
+
|
|
34
51
|
export function createHandler(options: HttpOptions) {
|
|
35
52
|
const { app } = options;
|
|
36
53
|
const auth = new Auth(app.config);
|
|
@@ -186,9 +203,14 @@ export function createHandler(options: HttpOptions) {
|
|
|
186
203
|
return sharePage(response);
|
|
187
204
|
}
|
|
188
205
|
|
|
189
|
-
// The web app: index.html for app routes, files from web/ otherwise.
|
|
206
|
+
// The web app: index.html for app routes, files from web/ otherwise. Who is signed in, and this
|
|
207
|
+
// server's build, go in the page (`serveApp`): the tab checks a session the device kept before `/api/me`.
|
|
190
208
|
if (parts.length === 0 || parts[0] === "s") {
|
|
191
|
-
return serveApp(
|
|
209
|
+
return serveApp(
|
|
210
|
+
response,
|
|
211
|
+
iconAddress(request, app.dataDir),
|
|
212
|
+
pageFacts(request, auth, app),
|
|
213
|
+
);
|
|
192
214
|
}
|
|
193
215
|
|
|
194
216
|
const file = normalize(join(WEB, ...parts));
|
|
@@ -198,7 +220,11 @@ export function createHandler(options: HttpOptions) {
|
|
|
198
220
|
}
|
|
199
221
|
|
|
200
222
|
if (file === join(WEB, "index.html")) {
|
|
201
|
-
return serveApp(
|
|
223
|
+
return serveApp(
|
|
224
|
+
response,
|
|
225
|
+
iconAddress(request, app.dataDir),
|
|
226
|
+
pageFacts(request, auth, app),
|
|
227
|
+
);
|
|
202
228
|
}
|
|
203
229
|
|
|
204
230
|
return await serveFile(request, response, file);
|
|
@@ -64,7 +64,10 @@ function tooLong(message: string): void {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
type CallOutcome =
|
|
67
|
+
type CallOutcome =
|
|
68
|
+
| { busy: boolean; stopping?: boolean }
|
|
69
|
+
| { archived: boolean; repeating: number }
|
|
70
|
+
| { take: boolean };
|
|
68
71
|
type CallRecord = { conversationId?: number; by?: string; at: number; result?: CallOutcome };
|
|
69
72
|
|
|
70
73
|
/** The record of call `token` in `calls`, made when there is none, the oldest pruned to make room. */
|
|
@@ -360,7 +363,8 @@ export class LinkedSessions implements LinkedSessionsApi {
|
|
|
360
363
|
* Carry each linked session's end of run to the session that started it: its final answer, or why it failed or
|
|
361
364
|
* stopped. Until the returned function is called. The report goes as a message from the linked session, once for
|
|
362
365
|
* each run, and lands at its lead's next pause; a lead that cannot take it now (plan mode, a spend limit, someone
|
|
363
|
-
* else's turn) is told in a notice instead, so nothing waits in silence.
|
|
366
|
+
* else's turn) is told in a notice instead, so nothing waits in silence. An archived lead, or one that turned its
|
|
367
|
+
* answers off (`reports`), gets nothing: read when the run ends, so it holds for every linked session it started.
|
|
364
368
|
*/
|
|
365
369
|
watch(): () => void {
|
|
366
370
|
return this.#app.events.on("run.ended", (run) => {
|
|
@@ -373,7 +377,9 @@ export class LinkedSessions implements LinkedSessionsApi {
|
|
|
373
377
|
|
|
374
378
|
const lead = meta.linkedFrom.id as unknown as ConversationId;
|
|
375
379
|
|
|
376
|
-
|
|
380
|
+
const leadMeta = this.#app.sessionMeta(lead);
|
|
381
|
+
|
|
382
|
+
if (leadMeta?.archived === true || leadMeta?.linkedReports === false) {
|
|
377
383
|
return;
|
|
378
384
|
}
|
|
379
385
|
|
|
@@ -569,7 +575,7 @@ export class LinkedSessions implements LinkedSessionsApi {
|
|
|
569
575
|
cwd?: string;
|
|
570
576
|
},
|
|
571
577
|
key: string,
|
|
572
|
-
): Promise<{ session: ConversationId; address: string }> {
|
|
578
|
+
): Promise<{ session: ConversationId; address: string; answers: boolean }> {
|
|
573
579
|
return this.#start(source, request, key);
|
|
574
580
|
}
|
|
575
581
|
|
|
@@ -584,7 +590,7 @@ export class LinkedSessions implements LinkedSessionsApi {
|
|
|
584
590
|
},
|
|
585
591
|
key: string,
|
|
586
592
|
actor?: RequestActor,
|
|
587
|
-
): Promise<{ session: ConversationId; address: string }> {
|
|
593
|
+
): Promise<{ session: ConversationId; address: string; answers: boolean }> {
|
|
588
594
|
const app = this.#app;
|
|
589
595
|
|
|
590
596
|
actor?.current();
|
|
@@ -649,7 +655,7 @@ export class LinkedSessions implements LinkedSessionsApi {
|
|
|
649
655
|
);
|
|
650
656
|
actor?.current();
|
|
651
657
|
|
|
652
|
-
return { session, address };
|
|
658
|
+
return { session, address, answers: this.#leads(source, session) };
|
|
653
659
|
}
|
|
654
660
|
|
|
655
661
|
/** Use the normal folder-picker validation for an explicit working directory. */
|
|
@@ -866,8 +872,8 @@ export class LinkedSessions implements LinkedSessionsApi {
|
|
|
866
872
|
/**
|
|
867
873
|
* Send a message to a linked session, by the source's address for it. Taken, not answered: no reply waits. It lands
|
|
868
874
|
* at the target's next pause (`steer`, the default), or after its run (`steer: false`). `answers` says whether the
|
|
869
|
-
* target's final answer comes back to the source by itself: only when the source started it
|
|
870
|
-
* asks the target to reply when an answer is needed.
|
|
875
|
+
* target's final answer comes back to the source by itself: only when the source started it, and did not turn its
|
|
876
|
+
* answers off (`reports`). Otherwise the message asks the target to reply when an answer is needed.
|
|
871
877
|
*/
|
|
872
878
|
async send(
|
|
873
879
|
source: ConversationId,
|
|
@@ -953,11 +959,88 @@ export class LinkedSessions implements LinkedSessionsApi {
|
|
|
953
959
|
return { answers: this.#leads(source, id) };
|
|
954
960
|
}
|
|
955
961
|
|
|
956
|
-
/**
|
|
962
|
+
/**
|
|
963
|
+
* Whether `source` started `target` and takes its answers: then `target`'s end of run reports to `source` by itself
|
|
964
|
+
* (`watch`). False when `source` turned its answers off (`reports`).
|
|
965
|
+
*/
|
|
957
966
|
#leads(source: ConversationId, target: ConversationId): boolean {
|
|
967
|
+
if (this.#app.sessionMeta(source)?.linkedReports === false) {
|
|
968
|
+
return false;
|
|
969
|
+
}
|
|
970
|
+
|
|
958
971
|
return this.#app.sessionMeta(target)?.linkedFrom?.id === Number(source);
|
|
959
972
|
}
|
|
960
973
|
|
|
974
|
+
/**
|
|
975
|
+
* Set whether `source`, as a lead, takes the run-end answers of the sessions it started. The switch is on the lead
|
|
976
|
+
* (`linkedReports` in its meta), so it holds for every one: old, new, archived. The checks of `archive`, on `source`
|
|
977
|
+
* itself. A repeat with the same `key` changes nothing again, and returns what the first call set.
|
|
978
|
+
*/
|
|
979
|
+
async reports(
|
|
980
|
+
source: ConversationId,
|
|
981
|
+
request: { take: boolean },
|
|
982
|
+
key: string,
|
|
983
|
+
): Promise<{ take: boolean }> {
|
|
984
|
+
const take = (request as { take?: unknown } | undefined)?.take;
|
|
985
|
+
|
|
986
|
+
if (typeof take !== "boolean") {
|
|
987
|
+
throw new TypeError("reports needs `take`: true or false.");
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
const app = this.#app;
|
|
991
|
+
const token = callKey(source, key);
|
|
992
|
+
const previous = (await app.harness.snapshot(LinkedSessionCallsDoc, source, context))
|
|
993
|
+
?.items[token];
|
|
994
|
+
const user = this.#person(source, previous?.by);
|
|
995
|
+
|
|
996
|
+
app.requireSteer(user);
|
|
997
|
+
|
|
998
|
+
if (previous?.conversationId !== undefined && previous.conversationId !== Number(source)) {
|
|
999
|
+
throw new HttpError(409, "This call already refers to another linked session.");
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
if (previous?.result !== undefined && "take" in previous.result) {
|
|
1003
|
+
return { take: previous.result.take };
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
await this.#maySend(source, user);
|
|
1007
|
+
|
|
1008
|
+
// The switch and its receipt are one commit: a crash leaves neither.
|
|
1009
|
+
return app.harness.commit(async (tx) => {
|
|
1010
|
+
const record = recordOf(await tx.doc(LinkedSessionCallsDoc, source), token, app.now());
|
|
1011
|
+
|
|
1012
|
+
if (record.conversationId !== undefined && record.conversationId !== Number(source)) {
|
|
1013
|
+
throw new HttpError(409, "This call already refers to another linked session.");
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
if (record.result !== undefined && !("take" in record.result)) {
|
|
1017
|
+
throw new HttpError(409, "This call already did something else.");
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
if (record.result !== undefined) {
|
|
1021
|
+
return { take: record.result.take };
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
const meta = (await tx.doc(SessionsDoc)).items[String(source)];
|
|
1025
|
+
|
|
1026
|
+
if (meta === undefined) {
|
|
1027
|
+
throw new HttpError(404, "Not a session");
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
if (take) {
|
|
1031
|
+
delete meta.linkedReports;
|
|
1032
|
+
} else {
|
|
1033
|
+
meta.linkedReports = false;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
record.conversationId = Number(source);
|
|
1037
|
+
record.by ??= user.id;
|
|
1038
|
+
record.result = { take };
|
|
1039
|
+
|
|
1040
|
+
return { take };
|
|
1041
|
+
}, context);
|
|
1042
|
+
}
|
|
1043
|
+
|
|
961
1044
|
/**
|
|
962
1045
|
* Stop the run of the linked session at address `to`. The same checks as `send`: the person Pi works for, and a
|
|
963
1046
|
* session of this team, including the turn and the spend limit there. A repeat with the same `key` stops nothing
|
|
@@ -13,7 +13,7 @@ import { isThinkingLevel } from "./models.ts";
|
|
|
13
13
|
|
|
14
14
|
/** Fresh global defaults and trust policy for this creation only. Never reload or replace the shared app reader. */
|
|
15
15
|
export function globalModelSettings(cwd: string): SettingsManager {
|
|
16
|
-
const settings =
|
|
16
|
+
const settings = piGlobalSettings(cwd);
|
|
17
17
|
|
|
18
18
|
if (settings.drainErrors().length > 0) {
|
|
19
19
|
throw new HttpError(
|
|
@@ -25,6 +25,22 @@ export function globalModelSettings(cwd: string): SettingsManager {
|
|
|
25
25
|
return settings;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/** The level a person last chose for a model, from Pi's global `modelThinkingLevels`, as Pi's TUI keeps it. */
|
|
29
|
+
export function rememberedLevel(
|
|
30
|
+
settings: SettingsManager,
|
|
31
|
+
provider: string,
|
|
32
|
+
modelId: string,
|
|
33
|
+
): ModelThinkingLevel | undefined {
|
|
34
|
+
const level = settings.getModelThinkingLevel(provider, modelId);
|
|
35
|
+
|
|
36
|
+
return isThinkingLevel(level) ? level : undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Read Pi's global settings fresh, so a level set in the TUI since counts. Pocket never writes them. */
|
|
40
|
+
export function piGlobalSettings(cwd: string): SettingsManager {
|
|
41
|
+
return SettingsManager.create(cwd, getAgentDir(), { projectTrusted: false });
|
|
42
|
+
}
|
|
43
|
+
|
|
28
44
|
/** No project setting, including defaultProjectTrust, can grant trust to itself. Unreadable trust fails closed. */
|
|
29
45
|
export function projectModelTrusted(cwd: string, settings: SettingsManager): boolean {
|
|
30
46
|
try {
|
package/types/pocket.d.ts
CHANGED
|
@@ -94,6 +94,20 @@ export interface Module {
|
|
|
94
94
|
pocketApi?: number;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Pi Pocket's options on a tool definition, beside Pi Durable's own. See the Tools guide.
|
|
99
|
+
* @since 0.29.0
|
|
100
|
+
* @experimental
|
|
101
|
+
*/
|
|
102
|
+
export interface ToolOptions {
|
|
103
|
+
/**
|
|
104
|
+
* Give this tool to every subagent, even one started with a tool list, while its extension runs there.
|
|
105
|
+
* @since 0.29.0
|
|
106
|
+
* @experimental
|
|
107
|
+
*/
|
|
108
|
+
readonly subagents?: "always";
|
|
109
|
+
}
|
|
110
|
+
|
|
97
111
|
// ─── The host ────────────────────────────────────────────────────────────────
|
|
98
112
|
|
|
99
113
|
/**
|
|
@@ -884,7 +898,9 @@ export interface LinkedSessions {
|
|
|
884
898
|
* Starts a session linked to `source`, in `cwd` or the source's folder, and sends it `message` as its first
|
|
885
899
|
* message. `model` is `provider/modelId`; `thinking` is any level (since 0.25.0; before, no higher than the source's). `cwd` is since 0.19.0:
|
|
886
900
|
* an older Pi Pocket ignores it, and starts the session in the source's folder. A blank `cwd` counts as none; a
|
|
887
|
-
* relative one resolves against the server process's folder, not the source's.
|
|
901
|
+
* relative one resolves against the server process's folder, not the source's. `answers` (since 0.29.0) says
|
|
902
|
+
* whether its final answer comes back to the source by itself: false when the source turned its answers off with
|
|
903
|
+
* `reports`. An older Pi Pocket does not return it.
|
|
888
904
|
* @since 0.14.0
|
|
889
905
|
* @experimental
|
|
890
906
|
*/
|
|
@@ -898,12 +914,12 @@ export interface LinkedSessions {
|
|
|
898
914
|
cwd?: string;
|
|
899
915
|
},
|
|
900
916
|
key: string,
|
|
901
|
-
): Promise<{ session: ConversationId; address: string }>;
|
|
917
|
+
): Promise<{ session: ConversationId; address: string; answers?: boolean }>;
|
|
902
918
|
/**
|
|
903
919
|
* Sends `message` to the linked session at the address `to`: at its next pause (since 0.28.0; before, only with
|
|
904
920
|
* `steer: true`), or with `steer: false` after its run. `answers` (since 0.28.0) says whether its final answer comes
|
|
905
921
|
* back to the source by itself, which it does when the source started it; otherwise the message asks it to reply
|
|
906
|
-
* when an answer is needed.
|
|
922
|
+
* when an answer is needed. An older Pi Pocket does not return it.
|
|
907
923
|
* @since 0.14.0
|
|
908
924
|
* @experimental
|
|
909
925
|
*/
|
|
@@ -911,7 +927,7 @@ export interface LinkedSessions {
|
|
|
911
927
|
source: ConversationId,
|
|
912
928
|
request: { to: string; message: string; steer?: boolean },
|
|
913
929
|
key: string,
|
|
914
|
-
): Promise<{ answers
|
|
930
|
+
): Promise<{ answers?: boolean }>;
|
|
915
931
|
/**
|
|
916
932
|
* Stops the run of the linked session at the address `to`, and withdraws what waits in its queue. When it is idle,
|
|
917
933
|
* it has no run to stop, and the call only withdraws the queue. `busy` says whether it was working.
|
|
@@ -930,6 +946,21 @@ export interface LinkedSessions {
|
|
|
930
946
|
request: { to: string; archived?: boolean },
|
|
931
947
|
key: string,
|
|
932
948
|
): Promise<{ archived: boolean; repeating: number }>;
|
|
949
|
+
/**
|
|
950
|
+
* Sets whether `source`, as a lead, takes the final answers of the sessions it started when their runs end.
|
|
951
|
+
* `take: false` turns them off for every one: old, new, archived. Then `send` from `source` returns
|
|
952
|
+
* `answers: false`, and its message asks the target to send the answer. `take: true` turns them back on, the
|
|
953
|
+
* default. `take` says the state now. It needs the same access as `archive`, on `source` itself. A repeat with
|
|
954
|
+
* the same `key` changes nothing again. A `take` that is not a boolean throws a `TypeError`. An older Pi Pocket
|
|
955
|
+
* has no `reports`: check for it.
|
|
956
|
+
* @since 0.29.0
|
|
957
|
+
* @experimental
|
|
958
|
+
*/
|
|
959
|
+
reports(
|
|
960
|
+
source: ConversationId,
|
|
961
|
+
request: { take: boolean },
|
|
962
|
+
key: string,
|
|
963
|
+
): Promise<{ take: boolean }>;
|
|
933
964
|
/**
|
|
934
965
|
* The sessions linked to `source`, at any distance, that the person Pi works for there can see.
|
|
935
966
|
* @since 0.14.0
|
|
@@ -2802,10 +2833,13 @@ export interface Pocket {
|
|
|
2802
2833
|
*/
|
|
2803
2834
|
conversationId(): number | null;
|
|
2804
2835
|
/**
|
|
2805
|
-
* Opens a conversation through normal navigation. The server still checks access.
|
|
2836
|
+
* Opens a conversation through normal navigation. The server still checks access. With `options.artifact`, it then
|
|
2837
|
+
* opens the app's viewer on that artifact of the conversation (at `options.version`, a positive integer, or the
|
|
2838
|
+
* latest). It does not say whether the artifact exists: a missing one fails as in the Artifacts sheet. A bad
|
|
2839
|
+
* `artifact` or `version` is ignored.
|
|
2806
2840
|
* @since 0.16.0
|
|
2807
2841
|
*/
|
|
2808
|
-
openConversation(id: number | string): void;
|
|
2842
|
+
openConversation(id: number | string, options?: { artifact?: string; version?: number }): void;
|
|
2809
2843
|
/**
|
|
2810
2844
|
* Hears which session the tab shows: the listener gets its id, or null, now and on each change. Returns how to stop.
|
|
2811
2845
|
* @since 0.20.0
|
package/web/app.js
CHANGED
|
@@ -265,7 +265,7 @@ function Topbar() {
|
|
|
265
265
|
class="title-open"
|
|
266
266
|
aria-label=${`${title}${folder ? `, in ${folder}` : ""}: session menu`}
|
|
267
267
|
title=${cwd ? `${shortPath(cwd, server?.home)}\nSession menu` : "Session menu"}
|
|
268
|
-
disabled=${!conversation}
|
|
268
|
+
disabled=${!conversation || store.state.me == null}
|
|
269
269
|
onClick=${() => openSheet({ type: "menu" })}
|
|
270
270
|
></button>
|
|
271
271
|
<span class="title-main" data-part="topbar-title" aria-hidden="true">${title}</span>
|
|
@@ -516,7 +516,8 @@ function App() {
|
|
|
516
516
|
: "Pi Pocket";
|
|
517
517
|
}, [state.view.conversation?.title]);
|
|
518
518
|
|
|
519
|
-
|
|
519
|
+
// A checked device view can draw before `/api/me`; with no copy, keep the usual boot screen.
|
|
520
|
+
if (state.me === undefined && state.view.conversation == null) {
|
|
520
521
|
return html`<${Boot}
|
|
521
522
|
caption=${state.notices.some((notice) => notice.level === "error") ? "waiting for the server" : "starting"}
|
|
522
523
|
/>
|