@bunny-agent/daemon 0.9.29-beta.6 → 0.9.29-beta.7

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/dist/bundle.mjs CHANGED
@@ -116839,7 +116839,7 @@ var require_core3 = __commonJS({
116839
116839
  return match2 && match2.index === 0;
116840
116840
  }
116841
116841
  var BACKREF_RE = /\[(?:[^\\\]]|\\.)*\]|\(\??|\\([1-9][0-9]*)|\\./;
116842
- function join38(regexps, separator = "|") {
116842
+ function join39(regexps, separator = "|") {
116843
116843
  let numCaptures = 0;
116844
116844
  return regexps.map((regex2) => {
116845
116845
  numCaptures += 1;
@@ -117143,7 +117143,7 @@ var require_core3 = __commonJS({
117143
117143
  this.exec = () => null;
117144
117144
  }
117145
117145
  const terminators = this.regexes.map((el) => el[1]);
117146
- this.matcherRe = langRe(join38(terminators), true);
117146
+ this.matcherRe = langRe(join39(terminators), true);
117147
117147
  this.lastIndex = 0;
117148
117148
  }
117149
117149
  /** @param {string} s */
@@ -168061,14 +168061,14 @@ var require_graceful_fs = __commonJS({
168061
168061
  return close;
168062
168062
  })(fs16.close);
168063
168063
  fs16.closeSync = (function(fs$closeSync) {
168064
- function closeSync2(fd) {
168064
+ function closeSync3(fd) {
168065
168065
  fs$closeSync.apply(fs16, arguments);
168066
168066
  resetQueue();
168067
168067
  }
168068
- Object.defineProperty(closeSync2, previousSymbol, {
168068
+ Object.defineProperty(closeSync3, previousSymbol, {
168069
168069
  value: fs$closeSync
168070
168070
  });
168071
- return closeSync2;
168071
+ return closeSync3;
168072
168072
  })(fs16.closeSync);
168073
168073
  if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || "")) {
168074
168074
  process.on("exit", function() {
@@ -208388,7 +208388,7 @@ function createOpenCodeRunner(options2 = {}) {
208388
208388
 
208389
208389
  // ../../packages/runner-pi/dist/pi-runner.js
208390
208390
  import { appendFileSync as appendFileSync5, existsSync as existsSync28, unlinkSync as unlinkSync6 } from "node:fs";
208391
- import { join as join35 } from "node:path";
208391
+ import { join as join36 } from "node:path";
208392
208392
 
208393
208393
  // ../../node_modules/.pnpm/@mariozechner+pi-ai@0.64.0_ws@8.19.0_zod@4.3.6/node_modules/@mariozechner/pi-ai/dist/index.js
208394
208394
  var dist_exports = {};
@@ -263374,8 +263374,8 @@ var generateImageSchema = {
263374
263374
  },
263375
263375
  quality: {
263376
263376
  type: "string",
263377
- enum: ["standard", "hd"],
263378
- description: "Image quality (OpenAI only). Defaults to standard."
263377
+ enum: ["low", "medium", "high", "auto"],
263378
+ description: "Image quality. Defaults to auto."
263379
263379
  }
263380
263380
  },
263381
263381
  required: ["prompt"],
@@ -263537,7 +263537,7 @@ function buildImageGenerateTool(cwd, imageModelId, baseUrl, apiKey) {
263537
263537
  const p = params;
263538
263538
  const prompt = p.prompt;
263539
263539
  const size = p.size ?? "1024x1024";
263540
- const quality = p.quality ?? "standard";
263540
+ const quality = p.quality ?? "auto";
263541
263541
  const rawFilename = p.filename;
263542
263542
  const filename = rawFilename ? extname2(rawFilename) ? rawFilename : `${rawFilename}.png` : `image_${Date.now()}.png`;
263543
263543
  const filePath = join34(cwd, filename.replace(/[^a-zA-Z0-9_\-./]/g, "_"));
@@ -263575,6 +263575,7 @@ function buildImageGenerateTool(cwd, imageModelId, baseUrl, apiKey) {
263575
263575
  ],
263576
263576
  details: {
263577
263577
  filePath: savedPath,
263578
+ ...json.usage != null ? { usage: { raw: { [imageModelId]: json.usage } } } : {},
263578
263579
  response: json
263579
263580
  }
263580
263581
  };
@@ -263762,6 +263763,7 @@ function buildImageEditTool(cwd, imageModelId, baseUrl, apiKey) {
263762
263763
  ],
263763
263764
  details: {
263764
263765
  filePath: savedPath,
263766
+ ...json.usage != null ? { usage: { raw: { [imageModelId]: json.usage } } } : {},
263765
263767
  response: json
263766
263768
  }
263767
263769
  };
@@ -263778,6 +263780,309 @@ function buildImageEditTool(cwd, imageModelId, baseUrl, apiKey) {
263778
263780
  };
263779
263781
  }
263780
263782
 
263783
+ // ../../packages/runner-pi/dist/session-utils.js
263784
+ import { closeSync as closeSync2, fstatSync, openSync as openSync2, readdirSync as readdirSync12, readSync as readSync2, statSync as statSync13 } from "node:fs";
263785
+ import { join as join35 } from "node:path";
263786
+ var MAX_SESSION_FILE_BYTES = Number(process.env.SANDAGENT_MAX_SESSION_BYTES) || 10 * 1024 * 1024;
263787
+ function resolveSessionPathById(cwd, sessionId) {
263788
+ const tempMgr = SessionManager.create(cwd);
263789
+ const sessionsDir = tempMgr.getSessionDir();
263790
+ try {
263791
+ const suffix = `_${sessionId}.jsonl`;
263792
+ const match2 = readdirSync12(sessionsDir).find((f3) => f3.endsWith(suffix));
263793
+ return match2 ? join35(sessionsDir, match2) : void 0;
263794
+ } catch {
263795
+ return void 0;
263796
+ }
263797
+ }
263798
+ function isSessionFileTooLarge(sessionPath2) {
263799
+ try {
263800
+ return statSync13(sessionPath2).size > MAX_SESSION_FILE_BYTES;
263801
+ } catch {
263802
+ return false;
263803
+ }
263804
+ }
263805
+ function readTailEntries(sessionPath2, tailBytes = 1024 * 1024) {
263806
+ let fd;
263807
+ try {
263808
+ fd = openSync2(sessionPath2, "r");
263809
+ } catch {
263810
+ return [];
263811
+ }
263812
+ try {
263813
+ const fileSize = fstatSync(fd).size;
263814
+ const readStart = Math.max(0, fileSize - tailBytes);
263815
+ const readLen = fileSize - readStart;
263816
+ const buf = Buffer.alloc(readLen);
263817
+ readSync2(fd, buf, 0, readLen, readStart);
263818
+ const tail = buf.toString("utf8");
263819
+ const entries = [];
263820
+ for (const line of tail.split("\n")) {
263821
+ const trimmed = line.trim();
263822
+ if (!trimmed)
263823
+ continue;
263824
+ try {
263825
+ entries.push(JSON.parse(trimmed));
263826
+ } catch {
263827
+ }
263828
+ }
263829
+ return entries;
263830
+ } finally {
263831
+ closeSync2(fd);
263832
+ }
263833
+ }
263834
+ function extractSessionContext(sessionPath2) {
263835
+ const entries = readTailEntries(sessionPath2);
263836
+ if (entries.length === 0)
263837
+ return void 0;
263838
+ for (let i2 = entries.length - 1; i2 >= 0; i2--) {
263839
+ const e2 = entries[i2];
263840
+ if (e2.type === "compaction" && typeof e2.summary === "string") {
263841
+ return e2.summary;
263842
+ }
263843
+ }
263844
+ const recentMessages = [];
263845
+ const MAX_MESSAGES = 6;
263846
+ for (let i2 = entries.length - 1; i2 >= 0 && recentMessages.length < MAX_MESSAGES; i2--) {
263847
+ const e2 = entries[i2];
263848
+ if (e2.type !== "message")
263849
+ continue;
263850
+ const msg = e2.message;
263851
+ if (!msg)
263852
+ continue;
263853
+ if (msg.role !== "user" && msg.role !== "assistant")
263854
+ continue;
263855
+ let text = "";
263856
+ if (typeof msg.content === "string") {
263857
+ text = msg.content;
263858
+ } else if (Array.isArray(msg.content)) {
263859
+ text = msg.content.filter((c) => c.type === "text" && c.text).map((c) => c.text).join("\n");
263860
+ }
263861
+ if (text) {
263862
+ recentMessages.unshift(`[${msg.role}]: ${text}`);
263863
+ }
263864
+ }
263865
+ if (recentMessages.length === 0)
263866
+ return void 0;
263867
+ return "## Previous Session Context (auto-extracted)\n\nThe following is the tail of the previous conversation:\n\n" + recentMessages.join("\n\n");
263868
+ }
263869
+
263870
+ // ../../packages/runner-pi/dist/usage-metadata.js
263871
+ function usageToMessageMetadata(usage) {
263872
+ return {
263873
+ input_tokens: usage.input,
263874
+ output_tokens: usage.output,
263875
+ cache_read_input_tokens: usage.cacheRead,
263876
+ cache_creation_input_tokens: usage.cacheWrite
263877
+ };
263878
+ }
263879
+ function accumulateToolUsage(tally, raw) {
263880
+ for (const [key, row] of Object.entries(raw)) {
263881
+ const existing = tally[key];
263882
+ if (existing) {
263883
+ for (const [field, val] of Object.entries(row)) {
263884
+ if (typeof val === "number")
263885
+ existing[field] = (existing[field] ?? 0) + val;
263886
+ }
263887
+ } else {
263888
+ const nums = {};
263889
+ for (const [field, val] of Object.entries(row)) {
263890
+ if (typeof val === "number")
263891
+ nums[field] = val;
263892
+ }
263893
+ tally[key] = nums;
263894
+ }
263895
+ }
263896
+ }
263897
+ function getUsageFromAgentEndMessages(messages) {
263898
+ for (let i2 = messages.length - 1; i2 >= 0; i2--) {
263899
+ const m2 = messages[i2];
263900
+ if (m2.role === "assistant" && m2.usage != null)
263901
+ return m2.usage;
263902
+ }
263903
+ return void 0;
263904
+ }
263905
+
263906
+ // ../../packages/runner-pi/dist/stream-converter.js
263907
+ function emitStreamError(errorText) {
263908
+ const errorLine = "data: " + JSON.stringify({ type: "error", errorText }) + "\n\n";
263909
+ const finishLine = "data: " + JSON.stringify({ type: "finish", finishReason: "error" }) + "\n\n";
263910
+ return [errorLine, finishLine, "data: [DONE]\n\n"];
263911
+ }
263912
+ function extractToolResultText(result) {
263913
+ if (result !== null && typeof result === "object") {
263914
+ const r2 = result;
263915
+ if (Array.isArray(r2.content) && r2.content.length > 0) {
263916
+ const text = r2.content.filter((c) => c.type === "text" && typeof c.text === "string").map((c) => c.text).join("\n");
263917
+ if (text.length > 0)
263918
+ return text;
263919
+ }
263920
+ }
263921
+ if (typeof result === "string")
263922
+ return result;
263923
+ try {
263924
+ return JSON.stringify(result);
263925
+ } catch {
263926
+ return String(result);
263927
+ }
263928
+ }
263929
+ function sseData(obj) {
263930
+ return "data: " + JSON.stringify(obj) + "\n\n";
263931
+ }
263932
+ var PiAISDKStreamConverter = class {
263933
+ constructor(options2) {
263934
+ this.options = options2;
263935
+ this.messageId = "msg_" + Date.now() + "_" + Math.random().toString(36).slice(2);
263936
+ this.toolUsageTally = {};
263937
+ this.activeTextPartId = null;
263938
+ this.hasStarted = false;
263939
+ this.hasFinished = false;
263940
+ }
263941
+ get finished() {
263942
+ return this.hasFinished;
263943
+ }
263944
+ forceError(errorText) {
263945
+ if (this.hasFinished)
263946
+ return [];
263947
+ return [...this.ensureStart(), ...this.finishError(errorText)];
263948
+ }
263949
+ handleEvent(event, aborted) {
263950
+ if (this.hasFinished)
263951
+ return [];
263952
+ const chunks = [...this.ensureStart()];
263953
+ if (event.type === "message_start") {
263954
+ const msg = event.message;
263955
+ if (msg?.role === "assistant")
263956
+ chunks.push(...this.endTextStreamIfOpen());
263957
+ return chunks;
263958
+ }
263959
+ if (event.type === "message_end")
263960
+ return chunks;
263961
+ if (event.type === "message_update") {
263962
+ const sub = event.assistantMessageEvent;
263963
+ if (sub.type === "text_start")
263964
+ chunks.push(...this.endTextStreamIfOpen(), ...this.openTextStream());
263965
+ else if (sub.type === "text_delta")
263966
+ chunks.push(...this.emitTextDelta(sub.delta));
263967
+ else if (sub.type === "toolcall_start")
263968
+ chunks.push(...this.endTextStreamIfOpen());
263969
+ return chunks;
263970
+ }
263971
+ if (event.type === "tool_execution_start") {
263972
+ chunks.push(...this.endTextStreamIfOpen());
263973
+ chunks.push(sseData({
263974
+ type: "tool-input-start",
263975
+ toolCallId: event.toolCallId,
263976
+ toolName: event.toolName,
263977
+ dynamic: true,
263978
+ providerExecuted: true
263979
+ }), sseData({
263980
+ type: "tool-input-available",
263981
+ toolCallId: event.toolCallId,
263982
+ toolName: event.toolName,
263983
+ input: event.args,
263984
+ dynamic: true,
263985
+ providerExecuted: true
263986
+ }));
263987
+ return chunks;
263988
+ }
263989
+ if (event.type === "tool_execution_end") {
263990
+ const output = this.options.redactText(this.options.normalizeToolOutput(event.result));
263991
+ const raw = event.result?.details?.usage?.raw;
263992
+ if (raw != null)
263993
+ accumulateToolUsage(this.toolUsageTally, raw);
263994
+ chunks.push(sseData({
263995
+ type: "tool-output-available",
263996
+ toolCallId: event.toolCallId,
263997
+ output,
263998
+ isError: event.isError,
263999
+ dynamic: true,
264000
+ providerExecuted: true
264001
+ }));
264002
+ return chunks;
264003
+ }
264004
+ if (event.type === "agent_end") {
264005
+ if (aborted) {
264006
+ chunks.push(...this.finishError("Run aborted by signal."));
264007
+ } else {
264008
+ const errorMsg = this.options.getErrorFromAgentEndMessages(event.messages);
264009
+ if (errorMsg)
264010
+ chunks.push(...this.finishError(errorMsg));
264011
+ else
264012
+ chunks.push(...this.finishSuccess(this.options.getUsageFromAgentEndMessages(event.messages)));
264013
+ }
264014
+ return chunks;
264015
+ }
264016
+ return chunks;
264017
+ }
264018
+ ensureStart() {
264019
+ if (this.hasStarted)
264020
+ return [];
264021
+ this.hasStarted = true;
264022
+ return [
264023
+ sseData({ type: "start", messageId: this.messageId }),
264024
+ sseData({
264025
+ type: "message-metadata",
264026
+ messageMetadata: { sessionId: this.options.sessionId }
264027
+ })
264028
+ ];
264029
+ }
264030
+ newTextPartId() {
264031
+ return "text_" + Date.now() + "_" + Math.random().toString(36).slice(2) + "_" + Math.random().toString(36).slice(2);
264032
+ }
264033
+ openTextStream() {
264034
+ this.activeTextPartId = this.newTextPartId();
264035
+ return [sseData({ type: "text-start", id: this.activeTextPartId })];
264036
+ }
264037
+ emitTextDelta(rawDelta) {
264038
+ const delta = rawDelta ? this.options.redactText(rawDelta) : void 0;
264039
+ if (!delta)
264040
+ return [];
264041
+ const startChunk = this.activeTextPartId == null ? this.openTextStream() : [];
264042
+ return [
264043
+ ...startChunk,
264044
+ sseData({ type: "text-delta", id: this.activeTextPartId, delta })
264045
+ ];
264046
+ }
264047
+ endTextStreamIfOpen() {
264048
+ if (this.activeTextPartId == null)
264049
+ return [];
264050
+ const id = this.activeTextPartId;
264051
+ this.activeTextPartId = null;
264052
+ return [sseData({ type: "text-end", id })];
264053
+ }
264054
+ finishSuccess(usage) {
264055
+ const chunks = [...this.endTextStreamIfOpen()];
264056
+ const raw = {};
264057
+ let chatUsage;
264058
+ if (usage) {
264059
+ const { id } = this.options.model;
264060
+ chatUsage = {
264061
+ type: "chat",
264062
+ ...usageToMessageMetadata(usage)
264063
+ };
264064
+ raw[id] = chatUsage;
264065
+ }
264066
+ for (const [key, tally] of Object.entries(this.toolUsageTally)) {
264067
+ raw[key] = { ...tally };
264068
+ }
264069
+ const finishPayload = {
264070
+ type: "finish",
264071
+ finishReason: "stop"
264072
+ };
264073
+ if (usage) {
264074
+ finishPayload.messageMetadata = { usage: { ...chatUsage, raw } };
264075
+ }
264076
+ chunks.push(sseData(finishPayload), "data: [DONE]\n\n");
264077
+ this.hasFinished = true;
264078
+ return chunks;
264079
+ }
264080
+ finishError(errorText) {
264081
+ this.hasFinished = true;
264082
+ return emitStreamError(errorText);
264083
+ }
264084
+ };
264085
+
263781
264086
  // ../../packages/runner-pi/dist/web-tools.js
263782
264087
  var braveProvider = {
263783
264088
  id: "brave",
@@ -263819,7 +264124,7 @@ ${body}`);
263819
264124
  });
263820
264125
  }
263821
264126
  }
263822
- return results;
264127
+ return { results };
263823
264128
  }
263824
264129
  };
263825
264130
  var tavilyProvider = {
@@ -263854,7 +264159,7 @@ ${body}`);
263854
264159
  });
263855
264160
  }
263856
264161
  }
263857
- return results;
264162
+ return { results };
263858
264163
  }
263859
264164
  };
263860
264165
  var AUTO_DETECT_ORDER = [braveProvider, tavilyProvider];
@@ -264001,7 +264306,7 @@ function buildWebSearchTool(env2) {
264001
264306
  let lastError;
264002
264307
  for (const { provider, apiKey } of providers) {
264003
264308
  try {
264004
- const results = await provider.search({
264309
+ const { results } = await provider.search({
264005
264310
  apiKey,
264006
264311
  query,
264007
264312
  count,
@@ -264009,11 +264314,21 @@ function buildWebSearchTool(env2) {
264009
264314
  freshness,
264010
264315
  signal
264011
264316
  });
264317
+ let fetchedPages = 0;
264012
264318
  if (shouldFetchContent) {
264013
264319
  for (const r2 of results) {
264014
264320
  r2.content = await fetchPageContent(r2.link, signal);
264321
+ fetchedPages += 1;
264015
264322
  }
264016
264323
  }
264324
+ const usage = {
264325
+ raw: {
264326
+ [provider.id]: {
264327
+ requests: 1,
264328
+ fetchedPages
264329
+ }
264330
+ }
264331
+ };
264017
264332
  return {
264018
264333
  content: [
264019
264334
  {
@@ -264021,7 +264336,9 @@ function buildWebSearchTool(env2) {
264021
264336
  text: formatSearchResults(results, provider.label)
264022
264337
  }
264023
264338
  ],
264024
- details: void 0
264339
+ details: {
264340
+ usage
264341
+ }
264025
264342
  };
264026
264343
  } catch (e2) {
264027
264344
  lastError = e2;
@@ -264204,52 +264521,6 @@ function applyModelOverrides(model, provider, optionsEnv) {
264204
264521
  model.baseUrl = anthropicBaseUrl;
264205
264522
  }
264206
264523
  }
264207
- function emitStreamError(errorText) {
264208
- return [
264209
- `data: ${JSON.stringify({ type: "error", errorText })}
264210
-
264211
- `,
264212
- `data: ${JSON.stringify({ type: "finish", finishReason: "error" })}
264213
-
264214
- `,
264215
- "data: [DONE]\n\n"
264216
- ];
264217
- }
264218
- function extractToolResultText(result) {
264219
- if (result !== null && typeof result === "object") {
264220
- const r2 = result;
264221
- if (Array.isArray(r2.content) && r2.content.length > 0) {
264222
- const text = r2.content.filter((c) => c.type === "text" && typeof c.text === "string").map((c) => c.text).join("\n");
264223
- if (text.length > 0) {
264224
- return text;
264225
- }
264226
- }
264227
- }
264228
- if (typeof result === "string")
264229
- return result;
264230
- try {
264231
- return JSON.stringify(result);
264232
- } catch {
264233
- return String(result);
264234
- }
264235
- }
264236
- function usageToMessageMetadata(usage) {
264237
- return {
264238
- input_tokens: usage.input,
264239
- output_tokens: usage.output,
264240
- cache_read_input_tokens: usage.cacheRead,
264241
- cache_creation_input_tokens: usage.cacheWrite
264242
- };
264243
- }
264244
- function getUsageFromAgentEndMessages(messages) {
264245
- for (let i2 = messages.length - 1; i2 >= 0; i2--) {
264246
- const m2 = messages[i2];
264247
- if (m2.role === "assistant" && m2.usage != null) {
264248
- return m2.usage;
264249
- }
264250
- }
264251
- return void 0;
264252
- }
264253
264524
  function getErrorFromAgentEndMessages(messages) {
264254
264525
  for (let i2 = messages.length - 1; i2 >= 0; i2--) {
264255
264526
  const m2 = messages[i2];
@@ -264265,7 +264536,7 @@ function traceRawMessage(debugCwd, data, reset = false, optionsEnv) {
264265
264536
  if (!enabled)
264266
264537
  return;
264267
264538
  try {
264268
- const file = join35(debugCwd, "pi-message-stream-debug.json");
264539
+ const file = join36(debugCwd, "pi-message-stream-debug.json");
264269
264540
  if (reset && existsSync28(file))
264270
264541
  unlinkSync6(file);
264271
264542
  const type = data !== null && typeof data === "object" ? data.type : void 0;
@@ -264334,9 +264605,22 @@ function createPiRunner(options2 = {}) {
264334
264605
  if (resume.includes("/")) {
264335
264606
  return SessionManager.open(resume);
264336
264607
  }
264337
- const sessions = await SessionManager.list(cwd);
264338
- const found = sessions.find((s2) => s2.id === resume);
264339
- return found ? SessionManager.open(found.path) : SessionManager.create(cwd);
264608
+ const sessionPath2 = resolveSessionPathById(cwd, resume);
264609
+ console.error(`${LOG_PREFIX2} resume: id=${resume} path=${sessionPath2 ?? "(not found)"}`);
264610
+ if (sessionPath2) {
264611
+ if (isSessionFileTooLarge(sessionPath2)) {
264612
+ const context = extractSessionContext(sessionPath2);
264613
+ console.error(`${LOG_PREFIX2} session file too large, starting fresh${context ? " (with context)" : ""}`);
264614
+ const newMgr = SessionManager.create(cwd);
264615
+ if (context) {
264616
+ const firstId = newMgr.getEntries()[0]?.id ?? "";
264617
+ newMgr.appendCompaction(context, firstId, 0);
264618
+ }
264619
+ return newMgr;
264620
+ }
264621
+ return SessionManager.open(sessionPath2);
264622
+ }
264623
+ return SessionManager.create(cwd);
264340
264624
  }
264341
264625
  return SessionManager.create(cwd);
264342
264626
  })();
@@ -264394,165 +264678,34 @@ function createPiRunner(options2 = {}) {
264394
264678
  }
264395
264679
  try {
264396
264680
  traceRawMessage(cwd, null, true, options2.env);
264397
- let promptText = userInput;
264398
- let images;
264399
- try {
264400
- if (userInput.startsWith("[") && userInput.endsWith("]")) {
264401
- const parsed = JSON.parse(userInput);
264402
- if (Array.isArray(parsed)) {
264403
- promptText = parsed.filter((p) => p.type === "text").map((p) => p.text).join("\n");
264404
- const imageParts = parsed.filter((p) => p.type === "image");
264405
- if (imageParts.length > 0) {
264406
- images = imageParts.map((p) => ({
264407
- type: "image",
264408
- data: p.data,
264409
- mimeType: p.mimeType
264410
- }));
264411
- }
264681
+ const promptText = userInput;
264682
+ const promptPromise = session.prompt(promptText);
264683
+ const streamConverter = new PiAISDKStreamConverter({
264684
+ sessionId: session.sessionId,
264685
+ model,
264686
+ redactText: (value2) => {
264687
+ if (options2.env && Object.keys(options2.env).length > 0) {
264688
+ return redactSecrets(value2, options2.env);
264412
264689
  }
264413
- }
264414
- } catch (_e2) {
264415
- }
264416
- const promptPromise = session.prompt(promptText, images ? { images } : void 0);
264417
- const messageId = `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`;
264418
- let hasStarted = false;
264419
- let hasFinished = false;
264420
- const imageToolUsage = { input_tokens: 0, output_tokens: 0 };
264421
- const newTextPartId = () => `text_${Date.now()}_${Math.random().toString(36).slice(2)}_${Math.random().toString(36).slice(2)}`;
264422
- let activeTextPartId = null;
264423
- let textStreamOpen = false;
264424
- const endTextStreamIfOpen = function* () {
264425
- if (textStreamOpen && activeTextPartId != null) {
264426
- yield `data: ${JSON.stringify({ type: "text-end", id: activeTextPartId })}
264427
-
264428
- `;
264429
- textStreamOpen = false;
264430
- activeTextPartId = null;
264431
- }
264432
- };
264433
- const beginTextStream = function* () {
264434
- activeTextPartId = newTextPartId();
264435
- yield `data: ${JSON.stringify({ type: "text-start", id: activeTextPartId })}
264436
-
264437
- `;
264438
- textStreamOpen = true;
264439
- };
264440
- const ensureStartEvent = async function* () {
264441
- if (!hasStarted) {
264442
- yield `data: ${JSON.stringify({ type: "start", messageId })}
264443
-
264444
- `;
264445
- yield `data: ${JSON.stringify({
264446
- type: "message-metadata",
264447
- messageMetadata: { sessionId: session.sessionId }
264448
- })}
264449
-
264450
- `;
264451
- hasStarted = true;
264452
- }
264453
- };
264454
- const finishSuccess = async function* (usage) {
264455
- yield* endTextStreamIfOpen();
264456
- const finishPayload = { type: "finish", finishReason: "stop" };
264457
- const hasImageUsage = imageToolUsage.input_tokens > 0 || imageToolUsage.output_tokens > 0;
264458
- if (usage != null || hasImageUsage) {
264459
- const base = usage != null ? usageToMessageMetadata(usage) : {};
264460
- finishPayload.messageMetadata = {
264461
- usage: {
264462
- ...base,
264463
- input_tokens: (base.input_tokens ?? 0) + imageToolUsage.input_tokens,
264464
- output_tokens: (base.output_tokens ?? 0) + imageToolUsage.output_tokens
264465
- }
264466
- };
264467
- }
264468
- yield `data: ${JSON.stringify(finishPayload)}
264469
-
264470
- `;
264471
- yield "data: [DONE]\n\n";
264472
- hasFinished = true;
264473
- };
264474
- const finishError = async function* (errorText) {
264475
- for (const chunk of emitStreamError(errorText)) {
264476
- yield chunk;
264477
- }
264478
- hasFinished = true;
264479
- };
264690
+ return value2;
264691
+ },
264692
+ normalizeToolOutput: extractToolResultText,
264693
+ getUsageFromAgentEndMessages,
264694
+ getErrorFromAgentEndMessages
264695
+ });
264480
264696
  while (!isComplete || eventQueue.length > 0) {
264481
264697
  while (eventQueue.length > 0) {
264482
264698
  const event = eventQueue.shift();
264483
264699
  traceRawMessage(cwd, event, false, options2.env);
264484
- yield* ensureStartEvent();
264485
- if (event.type === "message_start") {
264486
- const msg = event.message;
264487
- if (msg?.role === "assistant") {
264488
- yield* endTextStreamIfOpen();
264489
- }
264490
- } else if (event.type === "message_update") {
264491
- const sub = event.assistantMessageEvent;
264492
- if (sub.type === "text_start") {
264493
- yield* endTextStreamIfOpen();
264494
- yield* beginTextStream();
264495
- } else if (sub.type === "text_delta") {
264496
- let delta = sub.delta;
264497
- if (delta) {
264498
- if (options2.env && Object.keys(options2.env).length > 0) {
264499
- delta = redactSecrets(delta, options2.env);
264500
- }
264501
- if (!textStreamOpen) {
264502
- yield* beginTextStream();
264503
- }
264504
- yield `data: ${JSON.stringify({
264505
- type: "text-delta",
264506
- id: activeTextPartId,
264507
- delta
264508
- })}
264509
-
264510
- `;
264511
- }
264512
- } else if (sub.type === "toolcall_start") {
264513
- yield* endTextStreamIfOpen();
264514
- }
264515
- } else if (event.type === "tool_execution_start") {
264516
- yield* endTextStreamIfOpen();
264517
- yield `data: ${JSON.stringify({ type: "tool-input-start", toolCallId: event.toolCallId, toolName: event.toolName, dynamic: true, providerExecuted: true })}
264518
-
264519
- `;
264520
- yield `data: ${JSON.stringify({ type: "tool-input-available", toolCallId: event.toolCallId, toolName: event.toolName, input: event.args, dynamic: true, providerExecuted: true })}
264521
-
264522
- `;
264523
- } else if (event.type === "tool_execution_end") {
264524
- let output = extractToolResultText(event.result);
264525
- if (options2.env && Object.keys(options2.env).length > 0) {
264526
- output = redactSecrets(output, options2.env);
264527
- }
264528
- if ((event.toolName === "generate_image" || event.toolName === "edit_image") && event.result !== null && typeof event.result === "object") {
264529
- const details = event.result.details;
264530
- const u = details?.response?.usage;
264531
- if (u) {
264532
- imageToolUsage.input_tokens += u.input_tokens ?? 0;
264533
- imageToolUsage.output_tokens += u.output_tokens ?? 0;
264534
- }
264535
- }
264536
- yield `data: ${JSON.stringify({ type: "tool-output-available", toolCallId: event.toolCallId, output, isError: event.isError, dynamic: true, providerExecuted: true })}
264537
-
264538
- `;
264539
- } else if (event.type === "agent_end") {
264540
- if (aborted) {
264541
- yield* finishError("Run aborted by signal.");
264542
- } else {
264543
- const errorMsg = getErrorFromAgentEndMessages(event.messages);
264544
- if (errorMsg) {
264545
- yield* finishError(errorMsg);
264546
- } else {
264547
- const usage = getUsageFromAgentEndMessages(event.messages);
264548
- yield* finishSuccess(usage);
264549
- }
264550
- }
264700
+ const chunks = streamConverter.handleEvent(event, aborted);
264701
+ for (const chunk of chunks) {
264702
+ yield chunk;
264551
264703
  }
264552
264704
  }
264553
- if (aborted && !hasFinished) {
264554
- yield* ensureStartEvent();
264555
- yield* finishError("Run aborted by signal.");
264705
+ if (aborted && !streamConverter.finished) {
264706
+ for (const chunk of streamConverter.forceError("Run aborted by signal.")) {
264707
+ yield chunk;
264708
+ }
264556
264709
  break;
264557
264710
  }
264558
264711
  if (!isComplete && eventQueue.length === 0) {
@@ -264561,22 +264714,24 @@ function createPiRunner(options2 = {}) {
264561
264714
  });
264562
264715
  }
264563
264716
  }
264564
- if (hasFinished) {
264717
+ if (streamConverter.finished) {
264565
264718
  return;
264566
264719
  }
264567
264720
  try {
264568
264721
  await promptPromise;
264569
264722
  } catch (error) {
264570
- if (!hasFinished) {
264571
- yield* ensureStartEvent();
264723
+ if (!streamConverter.finished) {
264572
264724
  const message = error instanceof Error ? error.message : "Pi agent run failed.";
264573
- yield* finishError(message);
264725
+ for (const chunk of streamConverter.forceError(message)) {
264726
+ yield chunk;
264727
+ }
264574
264728
  }
264575
264729
  return;
264576
264730
  }
264577
- if (!hasFinished && session.agent.state.error) {
264578
- yield* ensureStartEvent();
264579
- yield* finishError(session.agent.state.error);
264731
+ if (!streamConverter.finished && session.agent.state.error) {
264732
+ for (const chunk of streamConverter.forceError(session.agent.state.error)) {
264733
+ yield chunk;
264734
+ }
264580
264735
  }
264581
264736
  } finally {
264582
264737
  if (abortSignal) {
@@ -264596,11 +264751,11 @@ function createPiRunner(options2 = {}) {
264596
264751
 
264597
264752
  // ../../packages/runner-harness/dist/session.js
264598
264753
  import { existsSync as existsSync29, mkdirSync as mkdirSync11, readFileSync as readFileSync23, writeFileSync as writeFileSync14 } from "node:fs";
264599
- import { join as join36 } from "node:path";
264754
+ import { join as join37 } from "node:path";
264600
264755
  var DIR = ".bunny-agent";
264601
264756
  var FILE = "session-id";
264602
264757
  function sessionPath(cwd) {
264603
- return join36(cwd, DIR, FILE);
264758
+ return join37(cwd, DIR, FILE);
264604
264759
  }
264605
264760
  function readSessionId(cwd) {
264606
264761
  try {
@@ -264614,28 +264769,28 @@ function readSessionId(cwd) {
264614
264769
  }
264615
264770
  function writeSessionId(cwd, id) {
264616
264771
  try {
264617
- mkdirSync11(join36(cwd, DIR), { recursive: true });
264772
+ mkdirSync11(join37(cwd, DIR), { recursive: true });
264618
264773
  writeFileSync14(sessionPath(cwd), id, "utf8");
264619
264774
  } catch {
264620
264775
  }
264621
264776
  }
264622
264777
 
264623
264778
  // ../../packages/runner-harness/dist/skills.js
264624
- import { existsSync as existsSync30, readdirSync as readdirSync12, statSync as statSync13 } from "node:fs";
264779
+ import { existsSync as existsSync30, readdirSync as readdirSync13, statSync as statSync14 } from "node:fs";
264625
264780
  import { homedir as homedir14 } from "node:os";
264626
- import { join as join37 } from "node:path";
264781
+ import { join as join38 } from "node:path";
264627
264782
  function discoverSkillPaths(cwd) {
264628
264783
  const paths = [];
264629
264784
  for (const base of [
264630
- join37(cwd, "skills"),
264631
- join37(homedir14(), ".bunny-agent", "skills")
264785
+ join38(cwd, "skills"),
264786
+ join38(homedir14(), ".bunny-agent", "skills")
264632
264787
  ]) {
264633
264788
  if (!existsSync30(base))
264634
264789
  continue;
264635
264790
  try {
264636
- for (const entry of readdirSync12(base)) {
264637
- const full = join37(base, entry);
264638
- if (statSync13(full).isDirectory() && existsSync30(join37(full, "SKILL.md"))) {
264791
+ for (const entry of readdirSync13(base)) {
264792
+ const full = join38(base, entry);
264793
+ if (statSync14(full).isDirectory() && existsSync30(join38(full, "SKILL.md"))) {
264639
264794
  paths.push(full);
264640
264795
  }
264641
264796
  }