@abelxiaoxing/cadence 2.0.29 → 2.0.30
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 +1 -1
- package/src/native-build.ts +1 -1
- package/src/service/contracts/workbench.ts +237 -44
- package/src/service-host.mjs +381 -100
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +13 -13
- package/web/dist/manifest.json +7 -7
package/src/service-host.mjs
CHANGED
|
@@ -4072,6 +4072,56 @@ function decodeCadenceConversation(v) {
|
|
|
4072
4072
|
...degraded ? { notice: text(o.notice, 1024, true) } : {}
|
|
4073
4073
|
};
|
|
4074
4074
|
}
|
|
4075
|
+
var STEP_TEXT_BYTES = 16 * 1024;
|
|
4076
|
+
var TARGET_BYTES = 1024;
|
|
4077
|
+
var STEP_READ = 24;
|
|
4078
|
+
function clipText(value, bytes) {
|
|
4079
|
+
const size = (s) => new TextEncoder().encode(JSON.stringify(s)).length;
|
|
4080
|
+
if (size(value) <= bytes)
|
|
4081
|
+
return value;
|
|
4082
|
+
let low = 0, high = Math.min(value.length, bytes);
|
|
4083
|
+
while (low < high) {
|
|
4084
|
+
const mid = Math.ceil((low + high) / 2);
|
|
4085
|
+
if (size(value.slice(0, mid)) <= bytes)
|
|
4086
|
+
low = mid;
|
|
4087
|
+
else
|
|
4088
|
+
high = mid - 1;
|
|
4089
|
+
}
|
|
4090
|
+
return /[\uD800-\uDBFF]/u.test(value.slice(low - 1, low)) ? value.slice(0, low - 1) : value.slice(0, low);
|
|
4091
|
+
}
|
|
4092
|
+
function toolTarget(args) {
|
|
4093
|
+
let value;
|
|
4094
|
+
try {
|
|
4095
|
+
value = JSON.parse(args);
|
|
4096
|
+
} catch {
|
|
4097
|
+
return "";
|
|
4098
|
+
}
|
|
4099
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
4100
|
+
return "";
|
|
4101
|
+
const field = (name) => {
|
|
4102
|
+
const v = Object.hasOwn(value, name) ? value[name] : undefined;
|
|
4103
|
+
return typeof v === "string" ? v.trim() : "";
|
|
4104
|
+
};
|
|
4105
|
+
const command = field("command");
|
|
4106
|
+
if (command)
|
|
4107
|
+
return command.split(`
|
|
4108
|
+
`, 1)[0];
|
|
4109
|
+
return [field("pattern"), field("path")].filter(Boolean).join(" ");
|
|
4110
|
+
}
|
|
4111
|
+
function chatStep(item, parts, more = false) {
|
|
4112
|
+
const tool = item.role === "tool";
|
|
4113
|
+
const text2 = tool ? "" : clipText(item.text, STEP_TEXT_BYTES);
|
|
4114
|
+
return {
|
|
4115
|
+
id: item.id,
|
|
4116
|
+
role: item.role,
|
|
4117
|
+
text: text2,
|
|
4118
|
+
clipped: !tool && (more || text2 !== item.text),
|
|
4119
|
+
name: item.name,
|
|
4120
|
+
target: tool ? clipText(toolTarget(item.args), TARGET_BYTES) : "",
|
|
4121
|
+
error: item.error,
|
|
4122
|
+
parts
|
|
4123
|
+
};
|
|
4124
|
+
}
|
|
4075
4125
|
function decodeChatCursor(value) {
|
|
4076
4126
|
const o = closed(value, ["id", "updatedAt"]);
|
|
4077
4127
|
return { id: decodeId(o.id), updatedAt: integer(o.updatedAt, 0) };
|
|
@@ -4088,13 +4138,23 @@ var statuses = [
|
|
|
4088
4138
|
function decodeWorkbenchRead(v) {
|
|
4089
4139
|
const kind = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
|
|
4090
4140
|
switch (kind) {
|
|
4091
|
-
case "conversation-
|
|
4092
|
-
const o = closed(v, ["kind", "id", "
|
|
4141
|
+
case "conversation-turn": {
|
|
4142
|
+
const o = closed(v, ["kind", "id", "ordinal", "before"]);
|
|
4143
|
+
return {
|
|
4144
|
+
kind,
|
|
4145
|
+
id: decodeId(o.id),
|
|
4146
|
+
ordinal: integer(o.ordinal, 0),
|
|
4147
|
+
before: nullable(o.before, (b) => integer(b, 0))
|
|
4148
|
+
};
|
|
4149
|
+
}
|
|
4150
|
+
case "conversation-step": {
|
|
4151
|
+
const o = closed(v, ["kind", "id", "turnId", "step", "part"]);
|
|
4093
4152
|
return {
|
|
4094
4153
|
kind,
|
|
4095
4154
|
id: decodeId(o.id),
|
|
4096
4155
|
turnId: decodeId(o.turnId),
|
|
4097
|
-
|
|
4156
|
+
step: integer(o.step, 0),
|
|
4157
|
+
part: integer(o.part, 0)
|
|
4098
4158
|
};
|
|
4099
4159
|
}
|
|
4100
4160
|
case "cadence-conversation": {
|
|
@@ -4144,8 +4204,8 @@ function decodeWorkbenchRead(v) {
|
|
|
4144
4204
|
return { kind, taskId: decodeId(o.taskId) };
|
|
4145
4205
|
}
|
|
4146
4206
|
case "conversation": {
|
|
4147
|
-
const o = closed(v, ["kind", "id"
|
|
4148
|
-
return { kind, id: decodeId(o.id)
|
|
4207
|
+
const o = closed(v, ["kind", "id"]);
|
|
4208
|
+
return { kind, id: decodeId(o.id) };
|
|
4149
4209
|
}
|
|
4150
4210
|
default: {
|
|
4151
4211
|
const o = closed(v, ["kind", "path"]);
|
|
@@ -4293,17 +4353,69 @@ function decodeChatTurn(v) {
|
|
|
4293
4353
|
media: omitted ? bool(omitted.media) : false,
|
|
4294
4354
|
other: omitted ? bool(omitted.other) : false
|
|
4295
4355
|
},
|
|
4296
|
-
items: list(o.items, 256,
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
|
|
4356
|
+
items: list(o.items, 256, decodeChatItem)
|
|
4357
|
+
};
|
|
4358
|
+
}
|
|
4359
|
+
function decodeChatItem(v) {
|
|
4360
|
+
const i = closed(v, ["id", "role", "text", "name", "args", "error"]);
|
|
4361
|
+
return {
|
|
4362
|
+
id: text(i.id, 160, true),
|
|
4363
|
+
role: oneOf(i.role, ["user", "assistant", "tool"]),
|
|
4364
|
+
text: text(i.text, 256 * 1024),
|
|
4365
|
+
name: text(i.name, 128),
|
|
4366
|
+
args: text(i.args, 64 * 1024),
|
|
4367
|
+
error: bool(i.error)
|
|
4368
|
+
};
|
|
4369
|
+
}
|
|
4370
|
+
function decodeChatStep(v) {
|
|
4371
|
+
const o = closed(v, [
|
|
4372
|
+
"id",
|
|
4373
|
+
"role",
|
|
4374
|
+
"text",
|
|
4375
|
+
"clipped",
|
|
4376
|
+
"name",
|
|
4377
|
+
"target",
|
|
4378
|
+
"error",
|
|
4379
|
+
"parts"
|
|
4380
|
+
]);
|
|
4381
|
+
const step = {
|
|
4382
|
+
id: text(o.id, 160, true),
|
|
4383
|
+
role: oneOf(o.role, ["user", "assistant", "tool"]),
|
|
4384
|
+
text: text(o.text, STEP_TEXT_BYTES),
|
|
4385
|
+
clipped: bool(o.clipped),
|
|
4386
|
+
name: text(o.name, 128),
|
|
4387
|
+
target: text(o.target, TARGET_BYTES),
|
|
4388
|
+
error: bool(o.error),
|
|
4389
|
+
parts: integer(o.parts, 0)
|
|
4390
|
+
};
|
|
4391
|
+
if (step.role === "tool" && (step.text || step.clipped))
|
|
4392
|
+
throw new ContractError("invalid");
|
|
4393
|
+
return step;
|
|
4394
|
+
}
|
|
4395
|
+
function decodeChatTurnHeader(v) {
|
|
4396
|
+
const o = closed(v, [
|
|
4397
|
+
"id",
|
|
4398
|
+
"epoch",
|
|
4399
|
+
"status",
|
|
4400
|
+
"notice",
|
|
4401
|
+
"omitted",
|
|
4402
|
+
"steps",
|
|
4403
|
+
"stable"
|
|
4404
|
+
]);
|
|
4405
|
+
const omitted = closed(o.omitted, ["thinking", "media", "other"]);
|
|
4406
|
+
const steps = integer(o.steps, 0);
|
|
4407
|
+
return {
|
|
4408
|
+
id: decodeId(o.id),
|
|
4409
|
+
epoch: decodeId(o.epoch),
|
|
4410
|
+
status: oneOf(o.status, statuses),
|
|
4411
|
+
notice: text(o.notice, 1024),
|
|
4412
|
+
omitted: {
|
|
4413
|
+
thinking: bool(omitted.thinking),
|
|
4414
|
+
media: bool(omitted.media),
|
|
4415
|
+
other: bool(omitted.other)
|
|
4416
|
+
},
|
|
4417
|
+
steps,
|
|
4418
|
+
stable: integer(o.stable, 0, steps)
|
|
4307
4419
|
};
|
|
4308
4420
|
}
|
|
4309
4421
|
function decodeChatSummary(v) {
|
|
@@ -4328,13 +4440,6 @@ function decodeChatSummary(v) {
|
|
|
4328
4440
|
function decodeWorkbenchResult(v) {
|
|
4329
4441
|
canonicalJson(v, 512 * 1024);
|
|
4330
4442
|
const k = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
|
|
4331
|
-
if (k === "conversation-transcript") {
|
|
4332
|
-
const o2 = closed(v, ["kind", "conversationId", "page", "turn"]);
|
|
4333
|
-
const turn = decodeChatTurn(o2.turn), page = integer(o2.page, 0);
|
|
4334
|
-
if (!turn.transcript || page >= turn.transcript.pages)
|
|
4335
|
-
throw new ContractError("invalid");
|
|
4336
|
-
return { kind: k, conversationId: decodeId(o2.conversationId), page, turn };
|
|
4337
|
-
}
|
|
4338
4443
|
if (k === "workflow-transcript") {
|
|
4339
4444
|
const o2 = closed(v, ["kind", "runId", "stepId", "turn"]);
|
|
4340
4445
|
return {
|
|
@@ -4442,12 +4547,49 @@ function decodeWorkbenchResult(v) {
|
|
|
4442
4547
|
};
|
|
4443
4548
|
}
|
|
4444
4549
|
if (kind === "conversation") {
|
|
4445
|
-
const o2 = closed(v, ["kind", "summary", "turns"
|
|
4550
|
+
const o2 = closed(v, ["kind", "summary", "turns"]);
|
|
4551
|
+
const summary = nullable(o2.summary, decodeChatSummary), turns = integer(o2.turns, 0);
|
|
4552
|
+
if (!summary && turns)
|
|
4553
|
+
throw new ContractError("invalid");
|
|
4554
|
+
return { kind, summary, turns };
|
|
4555
|
+
}
|
|
4556
|
+
if (kind === "conversation-turn") {
|
|
4557
|
+
const o2 = closed(v, [
|
|
4558
|
+
"kind",
|
|
4559
|
+
"conversationId",
|
|
4560
|
+
"ordinal",
|
|
4561
|
+
"turn",
|
|
4562
|
+
"start",
|
|
4563
|
+
"steps"
|
|
4564
|
+
]);
|
|
4565
|
+
const turn = decodeChatTurnHeader(o2.turn), start = integer(o2.start, 0), steps = list(o2.steps, STEP_READ, decodeChatStep);
|
|
4566
|
+
if (start + steps.length > turn.steps)
|
|
4567
|
+
throw new ContractError("invalid");
|
|
4568
|
+
return {
|
|
4569
|
+
kind,
|
|
4570
|
+
conversationId: decodeId(o2.conversationId),
|
|
4571
|
+
ordinal: integer(o2.ordinal, 0),
|
|
4572
|
+
turn,
|
|
4573
|
+
start,
|
|
4574
|
+
steps
|
|
4575
|
+
};
|
|
4576
|
+
}
|
|
4577
|
+
if (kind === "conversation-step") {
|
|
4578
|
+
const o2 = closed(v, [
|
|
4579
|
+
"kind",
|
|
4580
|
+
"conversationId",
|
|
4581
|
+
"turnId",
|
|
4582
|
+
"step",
|
|
4583
|
+
"part",
|
|
4584
|
+
"item"
|
|
4585
|
+
]);
|
|
4446
4586
|
return {
|
|
4447
4587
|
kind,
|
|
4448
|
-
|
|
4449
|
-
|
|
4450
|
-
|
|
4588
|
+
conversationId: decodeId(o2.conversationId),
|
|
4589
|
+
turnId: decodeId(o2.turnId),
|
|
4590
|
+
step: integer(o2.step, 0),
|
|
4591
|
+
part: integer(o2.part, 0),
|
|
4592
|
+
item: decodeChatItem(o2.item)
|
|
4451
4593
|
};
|
|
4452
4594
|
}
|
|
4453
4595
|
if (kind === "files" || kind === "directories") {
|
|
@@ -8852,6 +8994,7 @@ import { createHash as createHash6, randomUUID as randomUUID8 } from "node:crypt
|
|
|
8852
8994
|
import {
|
|
8853
8995
|
createServer
|
|
8854
8996
|
} from "node:http";
|
|
8997
|
+
import { gzipSync, constants as zlib } from "node:zlib";
|
|
8855
8998
|
import { WebSocketServer } from "ws";
|
|
8856
8999
|
|
|
8857
9000
|
// src/service/workbench-send-refusal.ts
|
|
@@ -8872,12 +9015,24 @@ function fixedHeaders(response) {
|
|
|
8872
9015
|
response.setHeader("X-Frame-Options", "DENY");
|
|
8873
9016
|
response.setHeader("Content-Security-Policy", "default-src 'none'; script-src 'self'; style-src 'self'; style-src-elem 'self'; style-src-attr 'unsafe-inline'; font-src 'self'; img-src 'self'; connect-src 'self'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'");
|
|
8874
9017
|
}
|
|
8875
|
-
function send(response, status, value) {
|
|
9018
|
+
function send(response, status, value, gzip = false) {
|
|
8876
9019
|
if (response.destroyed || response.writableEnded)
|
|
8877
9020
|
return;
|
|
8878
9021
|
response.statusCode = status;
|
|
8879
9022
|
response.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
8880
|
-
|
|
9023
|
+
const body = canonicalJson(value, LIMITS.controlBytes);
|
|
9024
|
+
if (gzip && body.length >= 1024) {
|
|
9025
|
+
response.setHeader("Content-Encoding", "gzip");
|
|
9026
|
+
response.end(gzipSync(body, { level: zlib.Z_BEST_SPEED }));
|
|
9027
|
+
} else
|
|
9028
|
+
response.end(body);
|
|
9029
|
+
}
|
|
9030
|
+
function acceptsGzip(header) {
|
|
9031
|
+
return (header ?? "").split(",").some((entry) => {
|
|
9032
|
+
const [name, ...params] = entry.split(";").map((p) => p.trim());
|
|
9033
|
+
const q = params.find((p) => p.toLowerCase().startsWith("q="));
|
|
9034
|
+
return name.toLowerCase() === "gzip" && (!q || Number(q.slice(2)) > 0);
|
|
9035
|
+
});
|
|
8881
9036
|
}
|
|
8882
9037
|
function unavailable() {
|
|
8883
9038
|
throw new ContractError("unavailable");
|
|
@@ -9263,7 +9418,10 @@ Content-Length: 0\r
|
|
|
9263
9418
|
this.attachment(caller);
|
|
9264
9419
|
const decoded2 = decodeHttpOutput(route.operation, value);
|
|
9265
9420
|
const status = value && typeof value === "object" && "kind" in value && value.kind === "error" && "error" in value ? errorStatus(decodePublicError(value.error).code) : 200;
|
|
9266
|
-
|
|
9421
|
+
const compressible = route.operation === "workbenchRead";
|
|
9422
|
+
if (compressible)
|
|
9423
|
+
res.setHeader("Vary", "Accept-Encoding");
|
|
9424
|
+
send(res, status, decoded2, compressible && acceptsGzip(boundary.headers.get("accept-encoding")));
|
|
9267
9425
|
} catch (e) {
|
|
9268
9426
|
if (createdAttachment)
|
|
9269
9427
|
this.detach(createdAttachment);
|
|
@@ -10333,7 +10491,7 @@ async function createGateway(value, hostUrl, source) {
|
|
|
10333
10491
|
const input = decodeGatewayInput(value), store = new GatewayStore(input.serverConfig, input.stateHome);
|
|
10334
10492
|
const web = loadWebAssets(source?.webDirectory);
|
|
10335
10493
|
const build2 = {
|
|
10336
|
-
contractHash: "
|
|
10494
|
+
contractHash: "d0edc9d93f682dd7c637bd28cdb756b823ad8eafeabebc63479bee1bac5b2b55",
|
|
10337
10495
|
assets: {
|
|
10338
10496
|
sourceHash: web.manifest.sourceHash,
|
|
10339
10497
|
contractHash: web.manifest.contractHash,
|
|
@@ -10801,6 +10959,7 @@ Content-Length: 0\r
|
|
|
10801
10959
|
Origin: b.endpoint
|
|
10802
10960
|
};
|
|
10803
10961
|
for (const key of [
|
|
10962
|
+
"accept-encoding",
|
|
10804
10963
|
"content-type",
|
|
10805
10964
|
"content-length",
|
|
10806
10965
|
"x-cadence-build",
|
|
@@ -10834,7 +10993,9 @@ Content-Length: 0\r
|
|
|
10834
10993
|
res.statusCode = incoming.statusCode ?? 502;
|
|
10835
10994
|
for (const key of [
|
|
10836
10995
|
"content-type",
|
|
10996
|
+
"content-encoding",
|
|
10837
10997
|
"content-disposition",
|
|
10998
|
+
"vary",
|
|
10838
10999
|
"x-cadence-attachment"
|
|
10839
11000
|
]) {
|
|
10840
11001
|
const value2 = incoming.headers[key];
|
|
@@ -24843,24 +25004,110 @@ class ConversationHistory {
|
|
|
24843
25004
|
}
|
|
24844
25005
|
}
|
|
24845
25006
|
|
|
25007
|
+
// src/service/conversation-steps.ts
|
|
25008
|
+
var TARGET_ARGS = 1024 * 1024;
|
|
25009
|
+
var INDEXED_TURNS = 32;
|
|
25010
|
+
function itemStep(item) {
|
|
25011
|
+
const step = chatStep(item, 0);
|
|
25012
|
+
const more = step.clipped || item.role === "tool" && !!(item.args || item.text);
|
|
25013
|
+
return { ...step, parts: more ? 1 : 0 };
|
|
25014
|
+
}
|
|
25015
|
+
|
|
25016
|
+
class ConversationSteps {
|
|
25017
|
+
page;
|
|
25018
|
+
indexes = new Map;
|
|
25019
|
+
constructor(page) {
|
|
25020
|
+
this.page = page;
|
|
25021
|
+
}
|
|
25022
|
+
list(turn) {
|
|
25023
|
+
const settled = !["queued", "running"].includes(turn.status);
|
|
25024
|
+
if (!turn.transcript) {
|
|
25025
|
+
const steps2 = turn.items.map((item, index2) => ({
|
|
25026
|
+
step: itemStep(item),
|
|
25027
|
+
source: { kind: "item", index: index2 }
|
|
25028
|
+
}));
|
|
25029
|
+
return { steps: steps2, stable: settled ? steps2.length : 0 };
|
|
25030
|
+
}
|
|
25031
|
+
const index = this.index(turn.id, turn.transcript.pages);
|
|
25032
|
+
const steps = [
|
|
25033
|
+
...index.steps,
|
|
25034
|
+
...turn.items.filter((item) => !index.ids.has(item.id)).map((item) => ({
|
|
25035
|
+
step: itemStep(item),
|
|
25036
|
+
source: { kind: "preview", id: item.id }
|
|
25037
|
+
}))
|
|
25038
|
+
];
|
|
25039
|
+
return { steps, stable: settled ? steps.length : index.steps.length };
|
|
25040
|
+
}
|
|
25041
|
+
detail(turn, step, part) {
|
|
25042
|
+
const listed = this.list(turn).steps[step];
|
|
25043
|
+
if (!listed || part >= listed.step.parts)
|
|
25044
|
+
throw new ContractError("stale");
|
|
25045
|
+
const source = listed.source;
|
|
25046
|
+
if (source.kind === "pages")
|
|
25047
|
+
return this.page(turn.id, source.first + part);
|
|
25048
|
+
const item = source.kind === "item" ? turn.items[source.index] : turn.items.find((i) => i.id === source.id);
|
|
25049
|
+
if (!item)
|
|
25050
|
+
throw new ContractError("stale");
|
|
25051
|
+
return item;
|
|
25052
|
+
}
|
|
25053
|
+
index(turnId, pages) {
|
|
25054
|
+
let index = this.indexes.get(turnId);
|
|
25055
|
+
this.indexes.delete(turnId);
|
|
25056
|
+
if (!index || index.pages > pages)
|
|
25057
|
+
index = { pages: 0, steps: [], ids: new Set, last: null };
|
|
25058
|
+
for (;index.pages < pages; index.pages++) {
|
|
25059
|
+
const fragment = this.page(turnId, index.pages);
|
|
25060
|
+
const last = index.last;
|
|
25061
|
+
const tail = index.steps.at(-1);
|
|
25062
|
+
if (last && tail && last.item.id === fragment.id) {
|
|
25063
|
+
const text2 = last.item.text + fragment.text, head2 = clipText(text2, STEP_TEXT_BYTES);
|
|
25064
|
+
last.more ||= head2.length < text2.length;
|
|
25065
|
+
last.args = last.args !== null && last.args.length + fragment.args.length <= TARGET_ARGS ? last.args + fragment.args : null;
|
|
25066
|
+
last.item = { ...fragment, text: head2, args: last.args ?? "" };
|
|
25067
|
+
tail.step = chatStep(last.item, tail.step.parts + 1, last.more);
|
|
25068
|
+
continue;
|
|
25069
|
+
}
|
|
25070
|
+
const head = clipText(fragment.text, STEP_TEXT_BYTES);
|
|
25071
|
+
index.last = {
|
|
25072
|
+
item: { ...fragment, text: head },
|
|
25073
|
+
more: head.length < fragment.text.length,
|
|
25074
|
+
args: fragment.args
|
|
25075
|
+
};
|
|
25076
|
+
index.steps.push({
|
|
25077
|
+
step: chatStep(index.last.item, 1, index.last.more),
|
|
25078
|
+
source: { kind: "pages", first: index.pages }
|
|
25079
|
+
});
|
|
25080
|
+
index.ids.add(fragment.id);
|
|
25081
|
+
}
|
|
25082
|
+
this.indexes.set(turnId, index);
|
|
25083
|
+
if (this.indexes.size > INDEXED_TURNS)
|
|
25084
|
+
this.indexes.delete(this.indexes.keys().next().value);
|
|
25085
|
+
return index;
|
|
25086
|
+
}
|
|
25087
|
+
}
|
|
25088
|
+
|
|
24846
25089
|
// src/service/conversation-transcript.ts
|
|
24847
25090
|
var TRANSCRIPT_PAGE_BYTES = 384 * 1024;
|
|
24848
25091
|
var PREVIEW_BYTES = 300 * 1024;
|
|
24849
25092
|
var FIELD_CHARS = 16 * 1024;
|
|
24850
25093
|
var PREVIEW_MARK = `
|
|
24851
|
-
[Preview only;
|
|
25094
|
+
[Preview only; the rest appears once this step completes.]`;
|
|
24852
25095
|
function part(value, offset, chars = FIELD_CHARS) {
|
|
24853
25096
|
let end = Math.min(value.length, offset + chars);
|
|
24854
25097
|
if (end < value.length && /[\uD800-\uDBFF]/u.test(value[end - 1]))
|
|
24855
25098
|
end--;
|
|
24856
25099
|
return value.slice(offset, end);
|
|
24857
25100
|
}
|
|
25101
|
+
function previewHead(head, added) {
|
|
25102
|
+
return head.length > FIELD_CHARS ? head : head + added.slice(0, FIELD_CHARS + 1 - head.length);
|
|
25103
|
+
}
|
|
24858
25104
|
|
|
24859
25105
|
class ConversationTranscript {
|
|
24860
25106
|
turn;
|
|
24861
25107
|
write;
|
|
24862
25108
|
state = { pages: 0 };
|
|
24863
25109
|
workingSizes = new Map;
|
|
25110
|
+
fieldBytes = new Map;
|
|
24864
25111
|
workingBytes = 0;
|
|
24865
25112
|
sizes = new Map;
|
|
24866
25113
|
bytes = 4096;
|
|
@@ -24888,19 +25135,28 @@ class ConversationTranscript {
|
|
|
24888
25135
|
find(id) {
|
|
24889
25136
|
return this.pending.get(id);
|
|
24890
25137
|
}
|
|
24891
|
-
preview(item) {
|
|
25138
|
+
preview(item, known = {}) {
|
|
24892
25139
|
if (this.pending.has(item.id)) {
|
|
24893
|
-
const
|
|
25140
|
+
const cached = this.fieldBytes.get(item.id);
|
|
25141
|
+
const bytes = (field) => known[field]?.bytes ?? cached?.[field] ?? Buffer.byteLength(item[field]);
|
|
25142
|
+
const fields2 = {
|
|
25143
|
+
text: bytes("text"),
|
|
25144
|
+
args: bytes("args"),
|
|
25145
|
+
name: bytes("name")
|
|
25146
|
+
};
|
|
25147
|
+
this.fieldBytes.set(item.id, fields2);
|
|
25148
|
+
const size3 = fields2.text + fields2.args + fields2.name;
|
|
24894
25149
|
this.workingBytes += size3 - (this.workingSizes.get(item.id) ?? 0);
|
|
24895
25150
|
this.workingSizes.set(item.id, size3);
|
|
24896
25151
|
if (this.workingBytes > 2 * LIMITS.detailBytes)
|
|
24897
25152
|
throw new ContractError("capacity");
|
|
24898
25153
|
}
|
|
24899
|
-
const clip = (
|
|
24900
|
-
const
|
|
25154
|
+
const clip = (field) => {
|
|
25155
|
+
const value = item[field];
|
|
25156
|
+
const prefix = part(known[field]?.head ?? value, 0);
|
|
24901
25157
|
return prefix.length < value.length ? prefix + PREVIEW_MARK : value;
|
|
24902
25158
|
};
|
|
24903
|
-
const preview = { ...item, text: clip(
|
|
25159
|
+
const preview = { ...item, text: clip("text"), args: clip("args") };
|
|
24904
25160
|
const size2 = Buffer.byteLength(canonicalJson(preview, PREVIEW_BYTES));
|
|
24905
25161
|
this.bytes += size2 - (this.sizes.get(item.id) ?? 0);
|
|
24906
25162
|
this.sizes.set(item.id, size2);
|
|
@@ -24924,6 +25180,7 @@ class ConversationTranscript {
|
|
|
24924
25180
|
this.preview(item);
|
|
24925
25181
|
this.archive(item);
|
|
24926
25182
|
this.pending.delete(id);
|
|
25183
|
+
this.fieldBytes.delete(id);
|
|
24927
25184
|
this.workingBytes -= this.workingSizes.get(id) ?? 0;
|
|
24928
25185
|
this.workingSizes.delete(id);
|
|
24929
25186
|
}
|
|
@@ -25012,17 +25269,18 @@ class ConversationCapture {
|
|
|
25012
25269
|
}
|
|
25013
25270
|
return item;
|
|
25014
25271
|
}
|
|
25015
|
-
update(item, field, value, maximum) {
|
|
25272
|
+
update(item, field, value, maximum, known = { bytes: Buffer.byteLength(value), head: value }) {
|
|
25273
|
+
const bytes = known.bytes;
|
|
25016
25274
|
if (this.transcript) {
|
|
25017
|
-
if (
|
|
25275
|
+
if (bytes > LIMITS.detailBytes)
|
|
25018
25276
|
throw new ContractError("capacity");
|
|
25019
25277
|
item[field] = value;
|
|
25020
|
-
this.transcript.preview(item);
|
|
25278
|
+
this.transcript.preview(item, { [field]: known });
|
|
25021
25279
|
return;
|
|
25022
25280
|
}
|
|
25023
25281
|
const before = item[field];
|
|
25024
25282
|
try {
|
|
25025
|
-
if (
|
|
25283
|
+
if (bytes > maximum)
|
|
25026
25284
|
throw new ContractError("capacity");
|
|
25027
25285
|
item[field] = value;
|
|
25028
25286
|
canonicalJson(this.turn, TURN_BYTES - 2048);
|
|
@@ -25042,14 +25300,19 @@ class ConversationCapture {
|
|
|
25042
25300
|
this.details.set(e.detailId, {
|
|
25043
25301
|
descriptor: e.descriptor,
|
|
25044
25302
|
sanitizer: new DetailSanitizer(e.descriptor.encoding, this.secrets()),
|
|
25045
|
-
text: ""
|
|
25303
|
+
text: "",
|
|
25304
|
+
bytes: 0,
|
|
25305
|
+
head: ""
|
|
25046
25306
|
});
|
|
25047
25307
|
} else if (e.kind === "detail-chunk" || e.kind === "detail-end") {
|
|
25048
25308
|
const d = this.details.get(e.detailId);
|
|
25049
25309
|
if (!d)
|
|
25050
25310
|
return;
|
|
25051
|
-
|
|
25052
|
-
|
|
25311
|
+
const added = e.kind === "detail-chunk" ? d.sanitizer.write(e.text) : d.sanitizer.end();
|
|
25312
|
+
d.text += added;
|
|
25313
|
+
d.bytes += Buffer.byteLength(added);
|
|
25314
|
+
d.head = previewHead(d.head, added);
|
|
25315
|
+
if (d.bytes > (this.transcript ? LIMITS.detailBytes : 256 * 1024))
|
|
25053
25316
|
throw new ContractError("capacity");
|
|
25054
25317
|
const o = d.descriptor.owner;
|
|
25055
25318
|
if (o.kind === "message") {
|
|
@@ -25057,20 +25320,26 @@ class ConversationCapture {
|
|
|
25057
25320
|
if (role2 === "assistant" && o.stage === "stream") {
|
|
25058
25321
|
this.messageText.set(o.blockId, {
|
|
25059
25322
|
messageId: o.messageId,
|
|
25060
|
-
text: d.text
|
|
25323
|
+
text: d.text,
|
|
25324
|
+
bytes: d.bytes,
|
|
25325
|
+
head: d.head
|
|
25326
|
+
});
|
|
25327
|
+
const blocks = [...this.messageText.values()].filter((block) => block.messageId === o.messageId);
|
|
25328
|
+
this.update(this.item(o.messageId, "assistant"), "text", blocks.length === 1 ? blocks[0].text : blocks.map((block) => block.text).join(""), 256 * 1024, {
|
|
25329
|
+
bytes: blocks.reduce((size2, block) => size2 + block.bytes, 0),
|
|
25330
|
+
head: blocks.reduce((head, block) => previewHead(head, block.head), "")
|
|
25061
25331
|
});
|
|
25062
|
-
this.update(this.item(o.messageId, "assistant"), "text", [...this.messageText.values()].filter((block) => block.messageId === o.messageId).map((block) => block.text).join(""), 256 * 1024);
|
|
25063
25332
|
}
|
|
25064
25333
|
} else if (o.kind === "message-final") {
|
|
25065
25334
|
const item = this.transcript?.find(o.messageId) ?? this.turn.items.find((i) => i.id === o.messageId);
|
|
25066
25335
|
if (item?.role === "assistant")
|
|
25067
|
-
this.update(item, "text", d.text, 256 * 1024);
|
|
25336
|
+
this.update(item, "text", d.text, 256 * 1024, d);
|
|
25068
25337
|
} else if (o.kind === "tool") {
|
|
25069
25338
|
const item = this.item(o.callId, "tool");
|
|
25070
25339
|
if (o.stage === "execution-arguments" || o.stage === "arguments-final")
|
|
25071
|
-
this.update(item, "args", d.text, 64 * 1024);
|
|
25340
|
+
this.update(item, "args", d.text, 64 * 1024, d);
|
|
25072
25341
|
if (o.stage === "result" || o.stage === "update-snapshot")
|
|
25073
|
-
this.update(item, "text", d.text, 256 * 1024);
|
|
25342
|
+
this.update(item, "text", d.text, 256 * 1024, d);
|
|
25074
25343
|
}
|
|
25075
25344
|
if (e.kind === "detail-end")
|
|
25076
25345
|
this.details.delete(e.detailId);
|
|
@@ -25085,7 +25354,7 @@ class ConversationCapture {
|
|
|
25085
25354
|
this.turn.notice = `worker-failure: ${e.code}`;
|
|
25086
25355
|
else if (e.kind === "omitted-block" && e.block.owner.kind !== "context" && e.block.owner.kind !== "handoff")
|
|
25087
25356
|
this.turn.omitted[OMITTED_FLAG[e.block.reason]] = true;
|
|
25088
|
-
if ([...this.details.values()].reduce((size2, detail) => size2 +
|
|
25357
|
+
if ([...this.details.values()].reduce((size2, detail) => size2 + detail.bytes + detail.sanitizer.retainedBytes, 0) > (this.transcript ? 2 * LIMITS.detailBytes : TURN_BYTES))
|
|
25089
25358
|
throw new ContractError("capacity");
|
|
25090
25359
|
if (!this.transcript)
|
|
25091
25360
|
canonicalJson(this.turn, TURN_BYTES);
|
|
@@ -25108,6 +25377,7 @@ class Conversations {
|
|
|
25108
25377
|
cwd;
|
|
25109
25378
|
active = new Map;
|
|
25110
25379
|
summaries = new Map;
|
|
25380
|
+
steps = new ConversationSteps((turnId, page) => this.page(turnId, page));
|
|
25111
25381
|
constructor(o) {
|
|
25112
25382
|
this.o = o;
|
|
25113
25383
|
this.root = join28(o.coordination.directory, "conversations-v1");
|
|
@@ -25274,10 +25544,13 @@ class Conversations {
|
|
|
25274
25544
|
].map(observedFile).join("|");
|
|
25275
25545
|
}
|
|
25276
25546
|
retainedSummary(id) {
|
|
25547
|
+
return this.retained(id).summary;
|
|
25548
|
+
}
|
|
25549
|
+
retained(id) {
|
|
25277
25550
|
const base = [this.metadataPath(id), this.identityPath(id)].map(observedFile).join("|");
|
|
25278
25551
|
const hit = this.summaries.get(id);
|
|
25279
25552
|
if (hit && !this.active.has(hit.summary.head) && hit.key === `${base}|${this.headKey(hit.summary.head, id, hit.count)}`)
|
|
25280
|
-
return { ...hit.summary };
|
|
25553
|
+
return { summary: { ...hit.summary }, count: hit.count };
|
|
25281
25554
|
const meta = this.metadata(id);
|
|
25282
25555
|
if (!meta)
|
|
25283
25556
|
throw new ContractError("corrupt-selected");
|
|
@@ -25294,7 +25567,7 @@ class Conversations {
|
|
|
25294
25567
|
count: meta.count
|
|
25295
25568
|
});
|
|
25296
25569
|
}
|
|
25297
|
-
return summary2;
|
|
25570
|
+
return { summary: summary2, count: meta.count };
|
|
25298
25571
|
}
|
|
25299
25572
|
liveSummary(live2) {
|
|
25300
25573
|
return {
|
|
@@ -25336,64 +25609,70 @@ class Conversations {
|
|
|
25336
25609
|
dir.closeSync();
|
|
25337
25610
|
}
|
|
25338
25611
|
}
|
|
25339
|
-
|
|
25612
|
+
conversation(id) {
|
|
25340
25613
|
this.o.owner.lease.assert();
|
|
25341
|
-
|
|
25342
|
-
|
|
25343
|
-
const live2 = before === null ? [...this.active.values()].find((turn) => turn.conversationId === id) : undefined;
|
|
25614
|
+
if (observedFile(this.metadataPath(id)) === "absent") {
|
|
25615
|
+
const live2 = [...this.active.values()].find((turn) => turn.conversationId === id);
|
|
25344
25616
|
return {
|
|
25345
25617
|
kind: "conversation",
|
|
25346
25618
|
summary: live2 ? this.liveSummary(live2) : null,
|
|
25347
|
-
turns:
|
|
25348
|
-
{
|
|
25349
|
-
...structuredClone(live2.turn),
|
|
25350
|
-
notice: [
|
|
25351
|
-
"Current metadata unavailable; showing the active turn only. No state was reconstructed.",
|
|
25352
|
-
live2.turn.notice
|
|
25353
|
-
].filter(Boolean).join(`
|
|
25354
|
-
`)
|
|
25355
|
-
}
|
|
25356
|
-
] : [],
|
|
25357
|
-
before: null
|
|
25619
|
+
turns: 0
|
|
25358
25620
|
};
|
|
25359
25621
|
}
|
|
25360
|
-
|
|
25361
|
-
|
|
25622
|
+
const { summary: summary2, count: count2 } = this.retained(id);
|
|
25623
|
+
return { kind: "conversation", summary: summary2, turns: count2 };
|
|
25624
|
+
}
|
|
25625
|
+
turnSteps(id, ordinal, before) {
|
|
25626
|
+
this.o.owner.lease.assert();
|
|
25627
|
+
const meta = this.metadata(id);
|
|
25628
|
+
if (!meta)
|
|
25362
25629
|
throw new ContractError("stale");
|
|
25363
|
-
const
|
|
25364
|
-
|
|
25365
|
-
|
|
25366
|
-
|
|
25367
|
-
|
|
25368
|
-
|
|
25369
|
-
bytes += size2;
|
|
25370
|
-
turns.unshift(structuredClone(turn));
|
|
25371
|
-
--end;
|
|
25372
|
-
}
|
|
25630
|
+
const turn = this.turn(this.turnAt(meta, ordinal));
|
|
25631
|
+
const { steps, stable } = this.steps.list(turn);
|
|
25632
|
+
const end = before ?? steps.length;
|
|
25633
|
+
if (end > steps.length)
|
|
25634
|
+
throw new ContractError("stale");
|
|
25635
|
+
const start = Math.max(0, end - STEP_READ);
|
|
25373
25636
|
return {
|
|
25374
|
-
kind: "conversation",
|
|
25375
|
-
|
|
25376
|
-
|
|
25377
|
-
|
|
25637
|
+
kind: "conversation-turn",
|
|
25638
|
+
conversationId: id,
|
|
25639
|
+
ordinal,
|
|
25640
|
+
turn: {
|
|
25641
|
+
id: turn.id,
|
|
25642
|
+
epoch: turn.epoch,
|
|
25643
|
+
status: turn.status,
|
|
25644
|
+
notice: turn.notice,
|
|
25645
|
+
omitted: { ...turn.omitted },
|
|
25646
|
+
steps: steps.length,
|
|
25647
|
+
stable
|
|
25648
|
+
},
|
|
25649
|
+
start,
|
|
25650
|
+
steps: steps.slice(start, end).map((row) => row.step)
|
|
25378
25651
|
};
|
|
25379
25652
|
}
|
|
25380
|
-
|
|
25653
|
+
step(id, turnId, step, part2) {
|
|
25381
25654
|
this.o.owner.lease.assert();
|
|
25382
25655
|
if (!this.hasTurn(id, turnId))
|
|
25383
25656
|
throw new ContractError("invalid");
|
|
25384
|
-
const turn = this.turn(turnId);
|
|
25385
|
-
if (!turn.transcript || page >= turn.transcript.pages)
|
|
25386
|
-
throw new ContractError("stale");
|
|
25387
|
-
const record2 = closed(this.read(this.path(turnId, `transcript/${page}.json`), TRANSCRIPT_PAGE_BYTES), ["turnId", "page", "items"]);
|
|
25388
|
-
if (record2.turnId !== turnId || record2.page !== page)
|
|
25389
|
-
throw new ContractError("corrupt-selected");
|
|
25390
25657
|
return {
|
|
25391
|
-
kind: "conversation-
|
|
25658
|
+
kind: "conversation-step",
|
|
25392
25659
|
conversationId: id,
|
|
25393
|
-
|
|
25394
|
-
|
|
25660
|
+
turnId,
|
|
25661
|
+
step,
|
|
25662
|
+
part: part2,
|
|
25663
|
+
item: { ...this.steps.detail(this.turn(turnId), step, part2) }
|
|
25395
25664
|
};
|
|
25396
25665
|
}
|
|
25666
|
+
page(turnId, page) {
|
|
25667
|
+
const value = this.read(this.path(turnId, `transcript/${integer(page, 0)}.json`), TRANSCRIPT_PAGE_BYTES);
|
|
25668
|
+
if (value === undefined)
|
|
25669
|
+
throw new ContractError("corrupt-selected");
|
|
25670
|
+
const record2 = closed(value, ["turnId", "page", "items"]);
|
|
25671
|
+
const items = list(record2.items, 1, decodeChatItem);
|
|
25672
|
+
if (record2.turnId !== turnId || record2.page !== page || !items.length)
|
|
25673
|
+
throw new ContractError("corrupt-selected");
|
|
25674
|
+
return items[0];
|
|
25675
|
+
}
|
|
25397
25676
|
attempted(conversationId, commandId) {
|
|
25398
25677
|
this.o.owner.lease.assert();
|
|
25399
25678
|
const meta = this.metadata(conversationId);
|
|
@@ -26226,7 +26505,7 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
26226
26505
|
}
|
|
26227
26506
|
decide(c, caller, evidence, recording) {
|
|
26228
26507
|
const { state, file } = this.usable(c.conversationId);
|
|
26229
|
-
const view = this.o.conversations.
|
|
26508
|
+
const view = this.o.conversations.conversation(c.conversationId);
|
|
26230
26509
|
if (!state.enabled || c.revision !== state.revision || !state.proposal || state.proposal.id !== c.proposalId || state.proposal.digest !== c.digest || view.kind !== "conversation" || view.summary?.head !== state.head || ["queued", "running", "unknown"].includes(view.summary.status) || this.active.has(c.conversationId))
|
|
26231
26510
|
throw new ContractError("stale");
|
|
26232
26511
|
const epoch = this.o.owner.lease.registration.epoch;
|
|
@@ -27120,8 +27399,8 @@ class WorkflowHistoryScanner {
|
|
|
27120
27399
|
|
|
27121
27400
|
// src/service/workflows.ts
|
|
27122
27401
|
var OWNER_RUNTIME = Object.freeze({
|
|
27123
|
-
version: "2.0.
|
|
27124
|
-
build: "
|
|
27402
|
+
version: "2.0.30",
|
|
27403
|
+
build: "b33664958cc26420c95cd53aff826f4f0d87cc3edee145ccb9467cad93ec29e2",
|
|
27125
27404
|
entry: fileURLToPath6(import.meta.url),
|
|
27126
27405
|
pid: process.pid,
|
|
27127
27406
|
node: process.versions.node,
|
|
@@ -28639,10 +28918,12 @@ class Workbench {
|
|
|
28639
28918
|
}))
|
|
28640
28919
|
};
|
|
28641
28920
|
}
|
|
28642
|
-
case "conversation-transcript":
|
|
28643
|
-
return this.conversations.transcript(request3.id, request3.turnId, request3.page);
|
|
28644
28921
|
case "conversation":
|
|
28645
|
-
return this.conversations.
|
|
28922
|
+
return this.conversations.conversation(request3.id);
|
|
28923
|
+
case "conversation-turn":
|
|
28924
|
+
return this.conversations.turnSteps(request3.id, request3.ordinal, request3.before);
|
|
28925
|
+
case "conversation-step":
|
|
28926
|
+
return this.conversations.step(request3.id, request3.turnId, request3.step, request3.part);
|
|
28646
28927
|
case "files":
|
|
28647
28928
|
return this.files.list(request3.path);
|
|
28648
28929
|
case "file":
|
|
@@ -28907,7 +29188,7 @@ var processLifetime = {
|
|
|
28907
29188
|
}
|
|
28908
29189
|
};
|
|
28909
29190
|
async function createServiceHost(value, sourceBuild, lifetime) {
|
|
28910
|
-
const embedded = "
|
|
29191
|
+
const embedded = "d0edc9d93f682dd7c637bd28cdb756b823ad8eafeabebc63479bee1bac5b2b55";
|
|
28911
29192
|
if (embedded && sourceBuild)
|
|
28912
29193
|
throw new ContractError("unsupported-context");
|
|
28913
29194
|
const contractHash = decodeHash(embedded ?? sourceBuild?.contractHash);
|