@abelxiaoxing/cadence 2.0.49 → 2.1.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/README.md +5 -3
- package/README.zh-CN.md +5 -2
- package/docs/conversation-mode.md +26 -6
- package/docs/history.md +15 -2
- package/package.json +4 -1
- package/src/cadence-instructions.ts +5 -5
- package/src/cadence-mode.ts +49 -7
- package/src/coordinator-tools.mjs +39 -5
- package/src/model-preview.mjs +4 -1
- package/src/native-build.ts +1 -1
- package/src/sdk-worker.mjs +80 -59
- package/src/service/contracts/models.ts +6 -0
- package/src/service/contracts/workbench.ts +218 -17
- package/src/service/contracts/workflow.ts +309 -30
- package/src/service-host.mjs +4588 -2279
- package/src/session-entries.mjs +3 -1
- package/src/session-file.mjs +26 -0
- package/src/session-rename.mjs +94 -0
- package/src/session-sharing.ts +3 -1
- package/src/worker-inputs.mjs +144 -0
- package/src/worker-toolkit.mjs +26 -1
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +31 -22
- package/web/dist/manifest.json +7 -7
package/src/session-entries.mjs
CHANGED
|
@@ -6,5 +6,7 @@ export const CADENCE_MODE_ENTRY = "cadence-conversation-mode/1";
|
|
|
6
6
|
/** Marks a session the browser created; Pi ignores it in the model context. */
|
|
7
7
|
export const WEBUI_SESSION_ENTRY = "cadence-webui/1";
|
|
8
8
|
|
|
9
|
-
/** Browser command/user identity and finalized message boundary, not model
|
|
9
|
+
/** Browser command/user identity and finalized message boundary, not model
|
|
10
|
+
* context: `{commandId, userId}`, with `run` naming the reply's own command
|
|
11
|
+
* when the message was sent into a reply that was already running. */
|
|
10
12
|
export const BROWSER_COMMAND_ENTRY = "cadence-browser-command/1";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** The one change a Cadence child makes to a Pi session file before Pi opens it
|
|
2
|
+
* to append, shared by the SDK worker and the rename child. */
|
|
3
|
+
import {
|
|
4
|
+
closeSync,
|
|
5
|
+
constants,
|
|
6
|
+
fstatSync,
|
|
7
|
+
openSync,
|
|
8
|
+
readSync,
|
|
9
|
+
writeSync,
|
|
10
|
+
} from "node:fs";
|
|
11
|
+
|
|
12
|
+
/** A torn final line (a writer killed mid-line) would swallow the next entry. */
|
|
13
|
+
export function terminateLastLine(file) {
|
|
14
|
+
const fd = openSync(
|
|
15
|
+
file,
|
|
16
|
+
constants.O_RDWR | constants.O_APPEND | (constants.O_NOFOLLOW ?? 0),
|
|
17
|
+
);
|
|
18
|
+
try {
|
|
19
|
+
const size = fstatSync(fd).size;
|
|
20
|
+
const last = Buffer.alloc(1);
|
|
21
|
+
if (size > 0 && readSync(fd, last, 0, 1, size - 1) === 1 && last[0] !== 10)
|
|
22
|
+
writeSync(fd, "\n");
|
|
23
|
+
} finally {
|
|
24
|
+
closeSync(fd);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Fixed-purpose child of the owner: records one name in one existing Pi session
|
|
2
|
+
// through Pi's own SessionManager, as the terminal's /name does, then exits.
|
|
3
|
+
// No model, credentials, tools, extensions or network. The owner launches it
|
|
4
|
+
// under its lease and confirms the name by reading the session back.
|
|
5
|
+
import { closeSync, constants, openSync, readSync } from "node:fs";
|
|
6
|
+
import { SessionManager } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { terminateLastLine } from "./session-file.mjs";
|
|
8
|
+
|
|
9
|
+
const INPUT_BYTES = 16 * 1024;
|
|
10
|
+
/** Only the identity header is read here; Pi reads the rest. */
|
|
11
|
+
const HEADER_BYTES = 64 * 1024;
|
|
12
|
+
/** Pi's session format this child appends to, as the owner's reader requires. */
|
|
13
|
+
const SESSION_VERSION = 3;
|
|
14
|
+
|
|
15
|
+
class Refusal extends Error {}
|
|
16
|
+
const refuse = (reason) => {
|
|
17
|
+
throw new Refusal(reason);
|
|
18
|
+
};
|
|
19
|
+
/** The identity line of an existing session file. Pi would start a new session
|
|
20
|
+
* in an empty file and rewrite an older format, so neither is ever opened. */
|
|
21
|
+
const header = (file) => {
|
|
22
|
+
const fd = openSync(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
23
|
+
try {
|
|
24
|
+
const buffer = Buffer.alloc(HEADER_BYTES);
|
|
25
|
+
const read = readSync(fd, buffer, 0, buffer.length, 0);
|
|
26
|
+
const end = buffer.subarray(0, read).indexOf(10);
|
|
27
|
+
return end < 0
|
|
28
|
+
? undefined
|
|
29
|
+
: JSON.parse(buffer.subarray(0, end).toString("utf8"));
|
|
30
|
+
} finally {
|
|
31
|
+
closeSync(fd);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
try {
|
|
35
|
+
if (
|
|
36
|
+
process.env.CADENCE_SUBAGENT_CHILD !== "1" ||
|
|
37
|
+
process.env.PI_OFFLINE !== "1"
|
|
38
|
+
)
|
|
39
|
+
refuse("guards-required");
|
|
40
|
+
const chunks = [];
|
|
41
|
+
let bytes = 0;
|
|
42
|
+
for await (const chunk of process.stdin) {
|
|
43
|
+
bytes += chunk.length;
|
|
44
|
+
if (bytes > INPUT_BYTES) refuse("input-bound");
|
|
45
|
+
chunks.push(chunk);
|
|
46
|
+
}
|
|
47
|
+
let input;
|
|
48
|
+
try {
|
|
49
|
+
input = JSON.parse(
|
|
50
|
+
new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks)),
|
|
51
|
+
);
|
|
52
|
+
} catch {
|
|
53
|
+
refuse("input");
|
|
54
|
+
}
|
|
55
|
+
const keys = ["id", "dir", "file", "cwd", "name"];
|
|
56
|
+
if (
|
|
57
|
+
!input ||
|
|
58
|
+
typeof input !== "object" ||
|
|
59
|
+
Array.isArray(input) ||
|
|
60
|
+
Object.keys(input).length !== keys.length ||
|
|
61
|
+
keys.some((key) => typeof input[key] !== "string") ||
|
|
62
|
+
!input.id ||
|
|
63
|
+
!input.dir ||
|
|
64
|
+
!input.file ||
|
|
65
|
+
!input.cwd
|
|
66
|
+
)
|
|
67
|
+
refuse("input");
|
|
68
|
+
const { id, dir, file, cwd, name } = input;
|
|
69
|
+
let identity;
|
|
70
|
+
try {
|
|
71
|
+
identity = header(file);
|
|
72
|
+
} catch {
|
|
73
|
+
refuse("session-unreadable");
|
|
74
|
+
}
|
|
75
|
+
if (
|
|
76
|
+
!identity ||
|
|
77
|
+
typeof identity !== "object" ||
|
|
78
|
+
identity.type !== "session" ||
|
|
79
|
+
identity.id !== id ||
|
|
80
|
+
typeof identity.version !== "number" ||
|
|
81
|
+
identity.version < SESSION_VERSION
|
|
82
|
+
)
|
|
83
|
+
refuse("session-mismatch");
|
|
84
|
+
terminateLastLine(file);
|
|
85
|
+
const manager = SessionManager.open(file, dir, cwd);
|
|
86
|
+
if (manager.getSessionId() !== id) refuse("session-mismatch");
|
|
87
|
+
manager.appendSessionInfo(name);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
// A fixed word only: never a path, the name or an exception's text.
|
|
90
|
+
process.exitCode = 1;
|
|
91
|
+
process.stderr.write(
|
|
92
|
+
`session-rename: ${error instanceof Refusal ? error.message : "failed"}\n`,
|
|
93
|
+
);
|
|
94
|
+
}
|
package/src/session-sharing.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/** Tells a terminal user when the browser continued the session Pi has open.
|
|
2
2
|
* Pi holds its own copy of an open session, so the browser's messages appear
|
|
3
3
|
* here only after the session is resumed again, and a message sent here first
|
|
4
|
-
* continues from where the terminal is, as a new branch.
|
|
4
|
+
* continues from where the terminal is, as a new branch. A name recorded
|
|
5
|
+
* from the browser continues nothing and is not announced. Readonly: it never
|
|
5
6
|
* writes the session or reloads it. */
|
|
6
7
|
import {
|
|
7
8
|
closeSync,
|
|
@@ -81,6 +82,7 @@ export function registerSessionSharing(pi: ExtensionAPI): void {
|
|
|
81
82
|
: {};
|
|
82
83
|
if (
|
|
83
84
|
o.type !== "session" &&
|
|
85
|
+
o.type !== "session_info" &&
|
|
84
86
|
typeof o.id === "string" &&
|
|
85
87
|
!ctx.sessionManager.getEntry(o.id)
|
|
86
88
|
)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/** Messages a browser sends into a running conversation turn, handed over by
|
|
2
|
+
* the owner on the worker's private IPC channel. A steer goes to Pi at once,
|
|
3
|
+
* which delivers it at its next opportunity; every other message waits here
|
|
4
|
+
* and becomes the next prompt once the reply has ended normally. Nothing here
|
|
5
|
+
* outlives the worker, and a message is identified by the order Pi was given
|
|
6
|
+
* it in, never by its text. */
|
|
7
|
+
import { closed, decodeId, LIMITS } from "./worker-public.mjs";
|
|
8
|
+
import { own } from "./worker-redaction.mjs";
|
|
9
|
+
|
|
10
|
+
/** Whether the reply an `agent_end` closes failed or was aborted; an event
|
|
11
|
+
* that cannot be read counts as failed. */
|
|
12
|
+
function unsuccessful(event) {
|
|
13
|
+
try {
|
|
14
|
+
const messages = own(event, "messages");
|
|
15
|
+
for (let i = messages.length - 1; i >= 0; i--)
|
|
16
|
+
if (own(messages[i], "role") === "assistant")
|
|
17
|
+
return ["error", "aborted"].includes(own(messages[i], "stopReason"));
|
|
18
|
+
return false;
|
|
19
|
+
} catch {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** `fail(category)` reports a broken channel or sequence; the caller aborts. */
|
|
25
|
+
export function conversationInputs(channel, session, fail) {
|
|
26
|
+
/** Handed to Pi as steers, in the order it delivers them. */
|
|
27
|
+
const steering = [];
|
|
28
|
+
/** Not handed to Pi: each becomes a prompt of its own, oldest first. */
|
|
29
|
+
const waiting = [];
|
|
30
|
+
const known = new Set();
|
|
31
|
+
let open = true;
|
|
32
|
+
/** A reply's request failed for good: no steer until another one starts. */
|
|
33
|
+
let held = false;
|
|
34
|
+
const recall = () => {
|
|
35
|
+
if (!steering.length) return;
|
|
36
|
+
session.clearQueue();
|
|
37
|
+
waiting.unshift(...steering.splice(0));
|
|
38
|
+
};
|
|
39
|
+
const steer = (input) => {
|
|
40
|
+
// Listed before Pi has it, so its delivery always finds it here.
|
|
41
|
+
steering.push(input);
|
|
42
|
+
let disposition;
|
|
43
|
+
session
|
|
44
|
+
.prompt(input.text, {
|
|
45
|
+
streamingBehavior: "steer",
|
|
46
|
+
expandPromptTemplates: false,
|
|
47
|
+
preflightResult: (value) => {
|
|
48
|
+
disposition = value;
|
|
49
|
+
// Pi takes a steer only into a running reply; anything else is
|
|
50
|
+
// work this worker did not sequence.
|
|
51
|
+
if (value !== "queued") fail("input-not-queued");
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
.then(
|
|
55
|
+
() => {
|
|
56
|
+
if (disposition === undefined) fail("input-not-queued");
|
|
57
|
+
},
|
|
58
|
+
() => {
|
|
59
|
+
// Pi took nothing (it is compacting, for one): the message waits.
|
|
60
|
+
const at = steering.indexOf(input);
|
|
61
|
+
if (at < 0) return;
|
|
62
|
+
steering.splice(at, 1);
|
|
63
|
+
if (open) waiting.push(input);
|
|
64
|
+
},
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
const receive = (message) => {
|
|
68
|
+
// Credential and coordinator replies share this channel.
|
|
69
|
+
const kind =
|
|
70
|
+
message && typeof message === "object" ? message.kind : undefined;
|
|
71
|
+
if (kind !== "input" && kind !== "input-cancel") return;
|
|
72
|
+
try {
|
|
73
|
+
if (kind === "input-cancel") {
|
|
74
|
+
const id = decodeId(closed(message, ["kind", "id"]).id);
|
|
75
|
+
const at = waiting.findIndex((input) => input.id === id);
|
|
76
|
+
if (at >= 0) waiting.splice(at, 1);
|
|
77
|
+
channel.send({ kind, id, removed: at >= 0 }, () => {});
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const row = closed(message, ["kind", "id", "mode", "text"]);
|
|
81
|
+
const input = { id: decodeId(row.id), text: row.text };
|
|
82
|
+
if (
|
|
83
|
+
!["steer", "queue"].includes(row.mode) ||
|
|
84
|
+
typeof input.text !== "string" ||
|
|
85
|
+
!input.text.trim() ||
|
|
86
|
+
Buffer.byteLength(input.text) > LIMITS.workerRequestBytes ||
|
|
87
|
+
known.has(input.id)
|
|
88
|
+
)
|
|
89
|
+
throw new Error("Invalid input");
|
|
90
|
+
known.add(input.id);
|
|
91
|
+
// The reply is ending or has failed: the owner records it undelivered.
|
|
92
|
+
if (!open) return;
|
|
93
|
+
if (row.mode === "steer" && !held && session.isStreaming) steer(input);
|
|
94
|
+
else waiting.push(input);
|
|
95
|
+
} catch {
|
|
96
|
+
fail("invalid-input");
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
channel.on("message", receive);
|
|
100
|
+
return {
|
|
101
|
+
/** Pi's own events, as `session.subscribe` reports them. A reply that
|
|
102
|
+
* failed for good must not continue into the steers Pi still holds: they
|
|
103
|
+
* come back to wait, and whether anything further is delivered is decided
|
|
104
|
+
* when the prompt returns (Pi may yet recover by compacting). */
|
|
105
|
+
event(event) {
|
|
106
|
+
if (event.type === "agent_start") held = false;
|
|
107
|
+
else if (
|
|
108
|
+
event.type === "agent_end" &&
|
|
109
|
+
event.willRetry !== true &&
|
|
110
|
+
unsuccessful(event)
|
|
111
|
+
) {
|
|
112
|
+
held = true;
|
|
113
|
+
recall();
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
/** The input whose message Pi has just delivered into the running reply. */
|
|
117
|
+
delivered() {
|
|
118
|
+
return steering.shift();
|
|
119
|
+
},
|
|
120
|
+
/** After a reply ended normally: the message to prompt with next. A steer
|
|
121
|
+
* Pi never looked at again waits first. Without one the turn is over and
|
|
122
|
+
* nothing further is taken. */
|
|
123
|
+
next() {
|
|
124
|
+
if (!open) return undefined;
|
|
125
|
+
recall();
|
|
126
|
+
const input = waiting.shift();
|
|
127
|
+
if (!input) open = false;
|
|
128
|
+
return input;
|
|
129
|
+
},
|
|
130
|
+
/** The turn failed or is being stopped: nothing further is handed to Pi
|
|
131
|
+
* or prompted. A steer Pi had already taken from its queue may still be
|
|
132
|
+
* written before the reply aborts, so the steers keep their order here
|
|
133
|
+
* and that message is still marked as its own. */
|
|
134
|
+
stop() {
|
|
135
|
+
open = false;
|
|
136
|
+
waiting.length = 0;
|
|
137
|
+
if (steering.length) session.clearQueue();
|
|
138
|
+
},
|
|
139
|
+
close() {
|
|
140
|
+
open = false;
|
|
141
|
+
channel.removeListener("message", receive);
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
package/src/worker-toolkit.mjs
CHANGED
|
@@ -92,6 +92,13 @@ export function createWorkerToolkit(options) {
|
|
|
92
92
|
Type.Literal("blocked"),
|
|
93
93
|
]),
|
|
94
94
|
summary: Type.String({ minLength: 1, maxLength: 8192 }),
|
|
95
|
+
lessons: Type.Optional(
|
|
96
|
+
Type.Array(Type.String({ minLength: 1, maxLength: 400 }), {
|
|
97
|
+
maxItems: 5,
|
|
98
|
+
description:
|
|
99
|
+
"Usually omitted. Only what this work taught that the code does not show and a later worker here would have to rediscover: a constraint between parts, a trap you hit, a convention. One sentence each, naming the file or symbol. The owner checks each before keeping it.",
|
|
100
|
+
}),
|
|
101
|
+
),
|
|
95
102
|
}),
|
|
96
103
|
async execute(_id, input) {
|
|
97
104
|
if (
|
|
@@ -99,12 +106,30 @@ export function createWorkerToolkit(options) {
|
|
|
99
106
|
!["completed", "blocked"].includes(input.status) ||
|
|
100
107
|
typeof input.summary !== "string" ||
|
|
101
108
|
!input.summary.trim() ||
|
|
102
|
-
Object.keys(input).some(
|
|
109
|
+
Object.keys(input).some(
|
|
110
|
+
(key) => !["status", "summary", "lessons"].includes(key),
|
|
111
|
+
) ||
|
|
112
|
+
(input.lessons !== undefined &&
|
|
113
|
+
(!Array.isArray(input.lessons) ||
|
|
114
|
+
input.lessons.length > 5 ||
|
|
115
|
+
input.lessons.some(
|
|
116
|
+
(lesson) =>
|
|
117
|
+
typeof lesson !== "string" ||
|
|
118
|
+
!lesson.trim() ||
|
|
119
|
+
lesson.length > 400,
|
|
120
|
+
)))
|
|
103
121
|
)
|
|
104
122
|
throw new Error("Invalid assignment report");
|
|
105
123
|
const report = {
|
|
106
124
|
status: input.status,
|
|
107
125
|
summary: sanitizeText(input.summary, options.secrets ?? []),
|
|
126
|
+
...(input.lessons?.length
|
|
127
|
+
? {
|
|
128
|
+
lessons: input.lessons.map((lesson) =>
|
|
129
|
+
sanitizeText(lesson, options.secrets ?? []),
|
|
130
|
+
),
|
|
131
|
+
}
|
|
132
|
+
: {}),
|
|
108
133
|
};
|
|
109
134
|
const text = JSON.stringify(report);
|
|
110
135
|
if (!report.summary.trim() || Buffer.byteLength(text) > 16384)
|